feat(compaction): hard-compact mid-turn when token threshold is reached - #70
Conversation
Previously auto-compaction only fired at the natural end of a turn (AI response with no tool calls). Long tool-using sessions could run many AI<->tool round-trips well above the configured threshold before the model finally returned without tool calls, sending the full over-threshold conversation on every call. Now AddToolResultMessage also checks the threshold (using token usage from the prior AI response) and compacts before the next AI call. This spot is API-safe: the assistant tool_use and the user tool_result are both already in the conversation, so the pair is preserved in the summary and there is no orphaned tool_use. Worst-case overshoot is one extra tool round. A small maybeCompact helper consolidates the threshold-check + log + swallow pattern shared between finalizeAIResponse (end-of-turn) and AddToolResultMessage (mid-turn).
|
Claude finished @Anthony-Bible's task in 2m 14s —— View job PR Review: feat(compaction): hard-compact mid-turn when token threshold is reached
SummaryThe core design is sound. Moving the compaction trigger into Issues1. Redundant threshold re-check in
|
There was a problem hiding this comment.
Pull request overview
This PR improves conversation auto-compaction behavior during tool-using turns by triggering compaction immediately after tool results are appended, preventing long multi-round tool cycles from repeatedly sending over-threshold conversation history to the model.
Changes:
- Run compaction mid-turn from
AddToolResultMessagewhen the last recorded token usage meets the configured threshold. - Add a
maybeCompacthelper to centralize the threshold-check + log + swallow pattern shared by end-of-turn and mid-turn compaction sites. - Add tests covering (1) compaction triggering after tool results are added and (2) non-fatal behavior when compaction fails mid-turn.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/domain/service/conversation_service.go | Adds mid-turn compaction after tool results and factors shared compaction logic into maybeCompact. |
| internal/domain/service/conversation_service_test.go | Adds tests validating mid-turn compaction behavior and non-fatal handling on compaction failure. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- finalizeAIResponse: drop redundant shouldCompact bool; rely on maybeCompact's internal threshold check so both sites use the same pattern. - Test mock: detect compaction summary requests by matching "CONVERSATION TO SUMMARIZE" in the prompt instead of coupling to the tools-nil implementation detail of compactConversation. - Split callCount into turnCalls/summaryCalls and assert both in the two new tests, so an unexpected double-compaction or duplicate AI hit fails the test. - Add an assertion that GetTokenUsage returns (1000, 10000) after compaction to guard against forgetting to reset sessionTokenUsage (which would cause threshold thrash on the next response).
|
Claude finished @Anthony-Bible's task in 2m 8s —— View job PR Review: feat(compaction): hard-compact mid-turn when token threshold is reached
All Prior Feedback AddressedThe follow-up commit (
The Remaining Observations1. Content-string detection still couples to
|
Previously auto-compaction only fired at the natural end of a turn (AI response with no tool calls). Long tool-using sessions could run many AI<->tool round-trips well above the configured threshold before the model finally returned without tool calls, sending the full over-threshold conversation on every call.
Now AddToolResultMessage also checks the threshold (using token usage from the prior AI response) and compacts before the next AI call. This spot is API-safe: the assistant tool_use and the user tool_result are both already in the conversation, so the pair is preserved in the summary and there is no orphaned tool_use. Worst-case overshoot is one extra tool round.
A small maybeCompact helper consolidates the threshold-check + log + swallow pattern shared between finalizeAIResponse (end-of-turn) and AddToolResultMessage (mid-turn).