Refactor (packages/app/src/context/server-session-v2-reducer.ts): Function with many parameters - #189
Open
zrusekko-cmu wants to merge 3 commits into
Open
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:
#139
Full path to the refactored file:
packages/app/src/context/server-session-v2-reducer.tsWhat do you think this file does?
(Your answer does not have to be 100% correct; give a reasonable, evidence‑based guess.)
From my understanding, the purpose of the file
server-session-v2-reducer.tsis that it is used to keep track of a session's message history as new events come in. The file processes events like text, reasoning, and tool calls and updates the conversation state accordingly.What is the scope of your refactoring within that file?
(Name specific functions/blocks/regions touched.)
The scope of my refactoring within the file
server-session-v2-reducer.tsis that I refactored theupdateContenthelper function to group its four targeting parameters (messageID,sessionID,type, andordinal) into a singletargetobject parameter. TheupdateContenthelper function is used by thesession.text.delta,session.text.ended,session.reasoning.delta, andsession.reasoning.endedcases increateV2SessionReducer, so I updated those four call sites to use the refactored parameter structure as well.Which Qlty‑reported issue did you address?
(Name the rule/metric and include the BEFORE value; e.g., “Cognitive Complexity 18 in render()”.)
The Qlty-reported issue that I addressed was: Function with many parameters (count = 6) for
updateContentat line 463. Specifically:2. Refactoring
How did the specific issue you chose impact the codebase’s maintainability?
The
updateContenthelper function accepted six parameters, making function calls harder to read and making the function interface more cumbersome to understand and maintain.What changes did you make to resolve the issue?
To resolve this issue, I introduced an
UpdateContentTarget<T>interface bundlingmessageID,sessionID,type, andordinal, changedupdateContentto accept three parameters:(source, target, apply), instead of six separate arguments, updated all four call sites increateV2SessionReducerto pass atargetobject with the relevant fields.How do your changes improve maintainability? Did you consider alternatives?
My changes improved maintainability because grouping the related targeting information into one object makes the function signature clearer and makes call sites easier to read and extend. I considered splitting
updateContentinto separate functions for text content and reasoning content, but chose thetargetobject because it keeps the shared logic in one place while directly addressing the parameter count issue.3. Validation
How did you validate that the change is correct?
To validate that the change is correct, I ran the existing test suite and added new tests covering the
reasoningbranch, ordinal-targeting among multiple same-type content blocks, and mixed-type isolation (to confirm the bundledtargetfields are still threaded through correctly), then confirmedbun lintandbun testpass and re-ran Qlty to confirm the smell is resolved.Screenshot of running

bun linton changed files:Attach a screenshot of the test coverage showing the lines were executed by the tests.
Before changes:

After changes:

Attach a screenshot showing the tests that cover the change passing during CI
CI Workflow from Commit:

CI Workflow Run History:

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

After changes:
