Refactor (packages/opencode/src/session/instruction.ts): Function with high complexity - #98
Open
zachkfan wants to merge 3 commits into
Open
Refactor (packages/opencode/src/session/instruction.ts): Function with high complexity#98zachkfan wants to merge 3 commits into
zachkfan wants to merge 3 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
1. Issue
Link to the associated GitHub issue:
#96
Full path to the refactored file:
packages/opencode/src/session/instruction.tsWhat do you think this file does?
(Your answer does not have to be 100% correct; give a reasonable, evidence‑based guess.)
I think what the file is doing is helping to manage instruction files, such as AGENTS.md, CONTEXT.md, etc. It manages these different documents that get injected into the AI agent's context to make sure that it actually follows project convention. This is based on the different functions that also exist in the file, such as systemPaths, which go through all the global and project based instructions files, that were defined earlier in the file as well.
What is the scope of your refactoring within that file?
(Name specific functions/blocks/regions touched.)
Only
extract()(previously lines 17–31) was rewritten, and a new helperloadedPaths()was added directly beneath it. No other function in the file was touched (10 insertions, 14 deletions).Which Qlty‑reported issue did you address?
(Name the rule/metric and include the BEFORE value; e.g., "Cognitive Complexity 18 in render()".)
Function with high complexity (count = 27): extractat line 17, together withDeeply nested control flow (level = 5)at line 26. The file's BEFORE total wasHigh total complexity (count = 75).2. Refactoring
How did the specific issue you chose impact the codebase's maintainability?
Five levels of nesting meant six conditions had to be held in mind to know when the innermost line ran, which is why a 15-line function scored 27 on a metric that penalizes nesting multiplicatively. Iteration, filtering, and accumulation were tangled together, so no single line expressed which parts actually count and edits had to be made inside the nest.
What changes did you make to resolve the issue?
Replaced three nested
forloops and four nestedifguards with a flatflatMappipeline, moving the per-part filtering into aloadedPaths()helper that uses early-return guard clauses. Also collapsed the redundant!loaded || !Array.isArray(loaded)to!Array.isArray(loaded), sinceArray.isArrayalready returns false fornull/undefined.How do your changes improve maintainability? Did you consider alternatives?
Splitting the traversal from the filtering rule cuts nesting from 5 levels to 2 and gives the previously-implicit "which parts contribute paths" rule a name. I rejected a single .filter() chain because a boolean filter loses TypeScript's narrowing through the part union, and rejected several smaller helpers because AGENTS.md warns against preemptive single-use extraction.
3. Validation
How did you validate that the change is correct?
bun test test/session/instruction.test.tspasses 9/9 (0 fail) andbun typecheckis clean;bun lintproduces no new warnings (repo totals identical before and after). I also confirmed the return type is stillSet<string>rather than widening toSet<any>, and that Qlty no longer reports either target smell (file total 75 → 52).Bun test:

Bun Lint (There was an Identical 698 Warnings and 2 Errors from before changes)

Attach a screenshot of the test coverage showing the lines were executed by the tests.

Uncovered lines mentioned are not lines affected by my changes.
Attach a screenshot showing the tests that cover the change passing during CI


Attach a screenshot of


qlty smells --no-snippets <full/path/to/file.ts>showing fewer reported issues after the changes.Before:
After: