Skip to content

Conversation

@ammar-agent
Copy link
Collaborator

Problem

When compaction completes, there's a race condition that can cause the compaction summary to be lost from context:

  1. StreamManager emits stream-end then continues to deletePartial/updateHistory
  2. CompactionHandler runs (async), clears history, appends summary
  3. sendQueuedMessages triggers commitToHistory
  4. commitToHistory finds stale partial.json (not yet deleted by StreamManager)
  5. The stale partial has the OLD historySequence from pre-compaction
  6. commitToHistory appends this stale message to history, corrupting context

Solution

CompactionHandler now deletes partial.json BEFORE clearing history. This ensures any concurrent commitToHistory calls find no partial and become no-ops.

Changes

  • src/node/services/compactionHandler.ts - Delete partial before compaction
  • src/node/services/agentSession.ts - Pass partialService to CompactionHandler
  • src/node/services/compactionHandler.test.ts - Test for the fix

Generated with mux

When compaction completes, there's a race between:
- StreamManager continuing to deletePartial/updateHistory after emitting stream-end
- CompactionHandler clearing history and appending summary
- sendQueuedMessages triggering commitToHistory

If commitToHistory runs before StreamManager deletes partial.json, it finds the
stale partial with the OLD historySequence and appends it to history, corrupting
the context and losing the compaction summary.

Fix: CompactionHandler now deletes partial.json BEFORE clearing history, ensuring
any concurrent commitToHistory calls find no partial and become no-ops.

_Generated with mux_
@chatgpt-codex-connector
Copy link

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@ammario ammario merged commit ad7e163 into main Dec 9, 2025
19 checks passed
@ammario ammario deleted the compaction-summary-loss-fix branch December 9, 2025 04:39
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.

2 participants