X Tutup
Skip to content

fix(chatd): enable compaction between steps and re-enter after summarization#22640

Merged
kylecarbs merged 1 commit intomainfrom
fix/chat-compaction-between-steps
Mar 5, 2026
Merged

fix(chatd): enable compaction between steps and re-enter after summarization#22640
kylecarbs merged 1 commit intomainfrom
fix/chat-compaction-between-steps

Conversation

@kylecarbs
Copy link
Member

Problem

Three bugs with chat summarization (compaction) share a single root cause: ReloadMessages was never wired up in the production chatloop.Run() call.

Bug 1: Compaction never fires between steps

The inline compaction guard in chatloop.go requires both Compaction and ReloadMessages to be non-nil:

if opts.Compaction != nil && opts.ReloadMessages != nil {

Since ReloadMessages was only set in tests, inline compaction was dead code in production. Long multi-step turns could blow through the context window.

Bug 2: Compaction only occurs at end of turn

The post-run safety net doesn't check ReloadMessages, so it was the only compaction path that fired:

if !alreadyCompacted && opts.Compaction != nil { // no ReloadMessages check

This meant compaction only happened once, after the entire agent turn finished.

Bug 3: Agent stops after summarization

After post-run compaction, Run() unconditionally returned nil. processChat then set the chat status to waiting (done). The agent never had a chance to continue with its fresh summarized context.

Fix

  1. Wire up ReloadMessages in chatd.go: reloads persisted messages from the database and re-applies system prompts (subagent instruction, workspace AGENTS.md).

  2. Wrap the step loop in an outer compaction loop: when compaction fires on the model's final step (compactedOnFinalStep), reload messages and continue the outer loop so the agent re-enters with summarized context.

  3. Track compactedOnFinalStep to distinguish inline compaction on the last step (needs re-entry) from inline compaction mid-loop followed by more tool-call steps (agent already consumed the compacted context, no re-entry needed).

  4. Add maxCompactionRetries = 3 to prevent infinite compaction loops.

Testing

  • All 7 existing compaction tests pass unchanged.
  • Added PostRunCompactionReEntersStepLoop test: verifies that when a text-only response triggers compaction, the outer loop re-enters and the agent makes a second stream call with fresh context.

…ization

Three bugs with chat summarization are fixed:

1. Compaction never fired between steps because ReloadMessages was not
   wired up in the chatloop.Run() call in chatd.go. The inline
   compaction guard requires both Compaction and ReloadMessages to be
   non-nil, but ReloadMessages was only set in tests.

2. Compaction only occurred at the end of an agent turn because the
   post-run safety net was the only compaction path that worked in
   production (it doesn't check ReloadMessages). The inline path was
   dead code.

3. After post-run compaction, the agent stopped instead of continuing
   with its fresh summarized context. Run() returned nil unconditionally,
   and processChat set the status to waiting.

Changes:
- Wire up ReloadMessages in chatd.go to reload persisted messages from
  the database and re-apply system prompts after compaction.
- Wrap the step loop in an outer compaction loop that re-enters when
  compaction fires on the model's final step (compactedOnFinalStep),
  giving the agent a chance to continue with summarized context.
- Add maxCompactionRetries (3) to prevent infinite compaction loops.
- Track stoppedByModel and compactedOnFinalStep to only re-enter when
  the agent hasn't already consumed the compacted context.
- Add PostRunCompactionReEntersStepLoop test.
@kylecarbs kylecarbs force-pushed the fix/chat-compaction-between-steps branch from 5f765db to c4c20bf Compare March 5, 2026 02:24
@kylecarbs kylecarbs merged commit 5630390 into main Mar 5, 2026
24 checks passed
@kylecarbs kylecarbs deleted the fix/chat-compaction-between-steps branch March 5, 2026 03:28
@github-actions github-actions bot locked and limited conversation to collaborators Mar 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

X Tutup