Skip to content

Extract compaction into a dedicated pkg/compaction package#2101

Merged
dgageot merged 1 commit intodocker:mainfrom
dgageot:extract-compaction
Mar 14, 2026
Merged

Extract compaction into a dedicated pkg/compaction package#2101
dgageot merged 1 commit intodocker:mainfrom
dgageot:extract-compaction

Conversation

@dgageot
Copy link
Member

@dgageot dgageot commented Mar 13, 2026

Extract compaction logic into dedicated pkg/compaction package

Move session compaction helpers (EstimateMessageTokens, ShouldCompact,
HasConversationMessages, BuildPrompt, SystemPrompt) out of pkg/runtime
into the new pkg/compaction package so the logic can be reused and
tested independently.

  • Add pkg/compaction with compaction.go, compaction_test.go, and
    embedded prompt files.
  • Remove sessionCompactor struct; replace with doCompact method on
    LocalRuntime and a standalone runSummarization function.
  • Simplify compactIfNeeded and RunStream to call compaction.ShouldCompact.
  • Delete pkg/runtime/prompts/compaction-{system,user}.txt (moved).
  • Remove estimateMessageTokens and its tests from pkg/runtime.

Assisted-By: docker-agent

@dgageot dgageot requested a review from a team as a code owner March 13, 2026 22:01
Copy link

@docker-agent docker-agent bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Assessment: 🟢 APPROVE

This PR successfully extracts compaction logic into a well-structured, testable package with a clean separation of concerns. The refactoring maintains the existing behavior while improving code organization and testability.

Analysis

The review examined:

  • New pkg/compaction package with pure functions and comprehensive tests
  • Refactored pkg/runtime code that delegates to the new package
  • Token estimation and compaction threshold logic
  • Error handling and event emission patterns

All hypotheses were investigated and dismissed:

  • Shallow copy concern: The current implementation correctly handles the Cost field, and tests verify no mutation of original messages
  • Summary conflation: The semantic separation is maintained - summaries are stored in session.Item.Summary and converted to synthetic user messages appropriately
  • Error handling: The defer pattern ensures completion events are always emitted, even on errors
  • Timing change: The compaction logic remains at the same points in the execution flow; only the implementation was refactored to use the new package

Strengths

✅ Clean API with pure functions (no side effects in compaction.Compactor)
✅ Comprehensive test coverage (12 new tests)
✅ Proper separation of concerns (compaction logic vs runtime side effects)
✅ Maintains backward compatibility in behavior
✅ Well-documented code with clear function contracts

No issues found in the changed code.

@rumpl
Copy link
Member

rumpl commented Mar 13, 2026

Where is the Compactor?

@aheritier
Copy link
Contributor

The runtime's sessionCompactor becomes a thin adapter that delegates to compaction.Compactor and handles runtime-specific side-effects (events, session store persistence, token count updates).

@rumpl
Copy link
Member

rumpl commented Mar 13, 2026 via email

@aheritier aheritier self-requested a review March 13, 2026 22:41
@quencs
Copy link

quencs commented Mar 13, 2026

:::writing{variant=“chat_message” id=“27514”}
Good catch — there actually isn’t a compaction.Compactor type in the code.

That wording came from the automated review summary and was imprecise. The PR separates the compaction logic into the pkg/compaction package, while sessionCompactor in the runtime acts as a thin adapter that calls into that package and handles runtime-specific side effects (events, persistence, token updates).

So the separation is at the package level, not via a Compactor struct.
:::

@dgageot dgageot force-pushed the extract-compaction branch from f29a0bb to 20ff7e0 Compare March 14, 2026 08:01
Move session compaction helpers (EstimateMessageTokens, ShouldCompact,
HasConversationMessages, BuildPrompt, SystemPrompt) out of pkg/runtime
into the new pkg/compaction package so the logic can be reused and
tested independently.

- Add pkg/compaction with compaction.go, compaction_test.go, and
  embedded prompt files.
- Remove sessionCompactor struct; replace with doCompact method on
  LocalRuntime and a standalone runSummarization function.
- Simplify compactIfNeeded and RunStream to call compaction.ShouldCompact.
- Delete pkg/runtime/prompts/compaction-{system,user}.txt (moved).
- Remove estimateMessageTokens and its tests from pkg/runtime.

Assisted-By: docker-agent
@dgageot dgageot force-pushed the extract-compaction branch from 20ff7e0 to fce935f Compare March 14, 2026 08:03
@dgageot
Copy link
Member Author

dgageot commented Mar 14, 2026

I updated the comment

@dgageot dgageot merged commit 0d76412 into docker:main Mar 14, 2026
4 checks passed
@quencs
Copy link

quencs commented Mar 14, 2026

Thanks for the refactor and for merging this.

Extracting the compaction logic into a dedicated package makes the overall structure much clearer. Keeping the runtime layer focused on orchestration while isolating the compaction logic improves separation of concerns and should make the code easier to maintain and test going forward.

Nice improvement to the architecture.

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.

4 participants