Fix/compaction serialize returns - #173
Open
pnihalani26 wants to merge 2 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.
Refactor (packages/core/src/session/compaction.ts): Simplify Message Serialization
1. Issue
Link to the associated GitHub issue:
#155
Full path to the refactored file:
packages/core/src/session/compaction.ts
What do you think this file does?
This file manages session compaction, it serializes past session messages into a plain text transcript and builds a summarization prompt so that the LLM can compress old conversation history into a short summary once the session grows too large for the model's context window.
What is the scope of your refactoring within that file?
I refactored the serialize function which was in line 86 and extracted a new helper, serializeAssistantPart, from its former assistant message branch.
Which Qlty‑reported issue did you address?
"Function with many returns" (count = 6) in serialize, at packages/core/src/session/compaction.ts:86.
2. Refactoring
How did the specific issue you chose impact the codebase’s maintainability?
Serialize in compaction.ts was one big function with six different return points and a lot of nested "if it's this kind of message, do this" checks. Qlty flagged it for too many returns (6). The assistant message case was the worst part and inside it there was another set of if checks for whether the content was text, reasoning, or a tool call, and whether the tool passed or failed. All of that logic sat in one place, so you had to understand the whole function to safely change any single case, and adding a new message type meant making an already-crowded function bigger.
What changes did you make to resolve the issue?
How do your changes improve maintainability? Did you consider alternatives?
3. Validation
How did you validate that the change is correct?
I added a describe("serialize") block in that calls serialize directly with one message of every kind and checks the exact string it returns. Since the tests assert the precise output for each branch, they would catch any wording or formatting change the refactor might have introduced. I ran bun test ./test/session-compaction.test.ts locally and all cases pass, and bun lint is clean for both files. I also re-ran qlty smells packages/core/src/session/compaction.ts and the "many returns" warnings for serialize are gone.
Attach a screenshot of the test coverage showing the lines were executed by the tests.
image 1 - shows the test coverage compaction.ts which is the file i refactored
Attach a screenshot showing the tests that cover the change passing during CI
image 2 - shows the tests that cover the change
Attach a screenshot of
qlty smells --no-snippets <full/path/to/file.ts>showing fewer reported issues after the changes.image 3 - shows the less amount of qlty smells after.

extra image 4 - shows bun test passing

extra image 5 - shows bun lint passing
