Refactor (packages/opencode/src/session/retry.ts): Function with high complexity - #38
Open
yswcyswc wants to merge 2 commits into
Open
Refactor (packages/opencode/src/session/retry.ts): Function with high complexity#38yswcyswc wants to merge 2 commits into
yswcyswc wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
P1B: Starter Task: Refactoring PR
Use this pull request template to briefly answer the questions below in one to two sentences each.
Feel free to delete this text at the top after filling out the template.
1. Issue
Link to the associated GitHub issue: #35
Full path to the refactored file:
packages/opencode/src/session/retry.tsWhat do you think this file does?
(Your answer does not have to be 100% correct; give a reasonable, evidence‑based guess.)
I think this file defines the retry policy for failed session and provider requests. It determines whether an error is retryable, calculates the waiting time from provider response headers or exponential backoff, and updates the retry status and attempt count before the next request.
What is the scope of your refactoring within that file?
(Name specific functions/blocks/regions touched.)
I refactored the
delay()function and extracted its retry-header parsing and exponential-backoff calculations into theretryAfter()andbackoff()helper functions. I also removed an unused import identified by the linter because there was a warning there.Which Qlty‑reported issue did you address?
(Name the rule/metric and include the BEFORE value; e.g., “Cognitive Complexity 18 in render()”.)
I addressed the Qlty issue
Function with high complexity (count = 22): delay, originally reported at line 44.2. Refactoring
How did the specific issue you chose impact the codebase’s maintainability?
The original
delay()combined header detection, parsing of multiple header formats and timer limits through several conditional branches. This made its control flow difficult to understand and modify safely.What changes did you make to resolve the issue?
I changed
delay()into a short coordinator that selects either a provider-specified retry delay or exponential backoff. I moved those calculations into the focusedretryAfter()andbackoff()helper functions and used early returns to reduce nesting.How do your changes improve maintainability? Did you consider alternatives?
The new helper functions separate header parsing from exponential-backoff calculation, giving each function a single, clearly named responsibility. This reduces nesting, makes each calculation easier to test and modify independently, and keeps
delay()easy to follow and I considered only flattening the original conditionals with early returns, but extraction created clearer boundaries between the two calculations.3. Validation
How did you validate that the change is correct?
I ran the focused
session.retry.delaytests covering missing headers,retry-after-ms, numeric and HTTP-dateretry-aftervalues, invalid hints, exponential backoff, and timer caps. I also ran focused linting, type checking, coverage, Qlty, and GitHub CI to confirm that behavior was preserved and the targeteddelay()complexity smell was removed.Attach a screenshot of the test coverage showing the lines were executed by the tests.
Line-level HTML coverage for

retry.ts; the refactoreddelay(),backoff(), andretryAfter()lines are shown as executed:Bun coverage summary showing 100% function coverage and 98.55% line coverage for

src/session/retry.ts; the only uncovered lines are 162–163, outside the refactored code:Attach a screenshot showing the tests that cover the change passing during CI

GitHub Actions passed all three checks for the latest commit, including unit tests, smoke tests, and type checking:
Attach a screenshot of


qlty smells --no-snippets <full/path/to/file.ts>showing fewer reported issues after the changes.Before refactoring, Qlty reported
Function with high complexity (count = 22): delay:After refactoring, the complexity smell for
delay()is no longer reported, and the file’s total complexity decreased from 84 to 70:Additional local validation:



bun lintandbun testpassing locallyLocal lint completed with 0 warnings and 0 errors for the refactored file:
The targeted retry test suite passed all 52 tests with 0 failures:
The focused
session.retry.delaytests passed with 0 failures: