Extract compaction into a dedicated pkg/compaction package#2101
Extract compaction into a dedicated pkg/compaction package#2101dgageot merged 1 commit intodocker:mainfrom
Conversation
There was a problem hiding this comment.
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/compactionpackage with pure functions and comprehensive tests - Refactored
pkg/runtimecode 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.Summaryand 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.
|
Where is the |
|
|
Yes but there is no compaction.Compactor in the code, that's why I'm
confused
…On Fri, Mar 13, 2026 at 11:15 PM Arnaud Héritier ***@***.***> wrote:
*aheritier* left a comment (docker/docker-agent#2101)
<#2101 (comment)>
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).
—
Reply to this email directly, view it on GitHub
<#2101 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAYMXLTMWDWDB6WIJZH7NT4QSCBFAVCNFSM6AAAAACWRQD7AWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DANJYGMYDQMZRHE>
.
You are receiving this because your review was requested.Message ID:
***@***.***>
|
|
:::writing{variant=“chat_message” id=“27514”} 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. |
f29a0bb to
20ff7e0
Compare
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
20ff7e0 to
fce935f
Compare
|
I updated the comment |
|
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. |
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.
embedded prompt files.
LocalRuntime and a standalone runSummarization function.
Assisted-By: docker-agent