🤖 fix: prevent compaction infinite loop and stale usage display #886
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Two related bugs were causing compaction to loop forever:
Bug 1: Message queue overwrote compaction metadata
When a user typed a follow-up message while a compaction was queued, the queue would overwrite
latestOptions, losing themuxMetadatathat marks the message as acompaction-request. The compaction would then be sent as a normal message, never triggering actual history replacement.Fix: Follow-up messages added after a compaction request are now merged into the compaction's
continueMessagefield rather than overwriting options. This preserves compaction metadata and sends follow-ups after compaction completes.Bug 2: Usage display showed pre-compaction context
After compaction, the summary message's usage field contained the pre-compaction context size (since that's what was sent to generate the summary). This inflated the context window display, making it appear usage was still high and triggering auto-compaction again.
Fix: Skip compacted messages when computing
lastContextUsage. The summary's usage reflects historical cost, not current context.Generated with
mux