Skip to content

Fix/compaction serialize returns - #173

Open
pnihalani26 wants to merge 2 commits into
CMU-313:mainfrom
pnihalani26:fix/compaction-serialize-returns
Open

Fix/compaction serialize returns#173
pnihalani26 wants to merge 2 commits into
CMU-313:mainfrom
pnihalani26:fix/compaction-serialize-returns

Conversation

@pnihalani26

@pnihalani26 pnihalani26 commented Sep 8, 2026

Copy link
Copy Markdown

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?

  • Pulled the assistant-message logic out into its own small function, serializeAssistantPart, so the text vs reasoning vs tool call handling stands on its own.
  • Replaced the long if-chain with a lookup table and the value is a small function that formats just that kind.
  • Serialize is now two lines, they look up the formatter for this message's kind and run it, or return an empty string if there isn't one. That takes it from 6 returns down to 1.

How do your changes improve maintainability? Did you consider alternatives?

  1. A switch statement — but that still has one return per case, so Qlty would still flag "many returns," and it wouldn't fix the nested assistant logic.
  2. Only extracting serializeAssistantPart and keeping the if chain so that lowers complexity but serialize would still have 6 returns.
  3. Using Effect's Match helper, a plain lookup table is simpler and matches the other helpers already in the file

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

Screenshot 2026-09-07 at 9 58 40 PM

Attach a screenshot showing the tests that cover the change passing during CI
image 2 - shows the tests that cover the change

Screenshot 2026-09-07 at 10 03 02 PM

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.
Screenshot 2026-09-07 at 10 02 12 PM

extra image 4 - shows bun test passing
Screenshot 2026-09-07 at 10 14 24 PM

extra image 5 - shows bun lint passing
Screenshot 2026-09-07 at 10 17 18 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant