Skip to content

Conversation

@ammar-agent
Copy link
Collaborator

Problem

When force compaction triggers during streaming and includes a "Continue with the current task" message, a second compaction could trigger immediately after the first completed.

Root Cause

The force compaction guard was keyed by activeStreamMessageId, which changes when streams change. The guard was reset when canInterrupt became false (stream ends). This caused:

  1. Force compaction triggers with activeStreamMessageId='msg-1'
  2. Compaction stream starts with new activeStreamMessageId='compact-msg'
  3. Compaction completes, stream ends, canInterrupt becomes false
  4. Reset effect runs: forceCompactionTriggeredRef = null
  5. Continue message stream starts: activeStreamMessageId='continue-msg'
  6. Guard check: null !== 'continue-msg' → fails → double compact!

Fix

Use a simple boolean flag that only resets when shouldForceCompact becomes false (i.e., when compaction successfully reduced context usage). This directly encodes the intended semantics: "don't re-trigger until usage actually drops."

Changes

  • Extract useForceCompaction hook for clean separation and testability
  • Use boolean guard instead of message ID tracking
  • Reset guard only when shouldForceCompact becomes false
  • Add comprehensive tests verifying:
    • Basic trigger conditions
    • Double-compact prevention (the main bug fix)
    • Guard reset after successful compaction
    • Rapid prop change handling

Testing

bun test src/browser/hooks/useForceCompaction.test.ts

 8 pass
 0 fail

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.

The force compaction guard was incorrectly keyed by activeStreamMessageId,
which changes when the compaction stream starts. This caused the guard to fail
when the continue message stream started after compaction completed, because:

1. Force compaction triggers with activeStreamMessageId='msg-1'
2. Compaction stream starts with new activeStreamMessageId='compact-msg'
3. Compaction completes, stream ends, canInterrupt becomes false
4. Reset effect runs: forceCompactionTriggeredRef = null
5. Continue message stream starts: activeStreamMessageId='continue-msg'
6. Guard check: null !== 'continue-msg' → fails → DOUBLE COMPACT

Fix: Use a simple boolean flag that only resets when shouldForceCompact
becomes false (i.e., when compaction successfully reduced context usage).
This ensures we block all compaction attempts until usage actually drops.

Changes:
- Extract useForceCompaction hook for clean separation and testability
- Use boolean guard instead of message ID tracking
- Reset guard only when shouldForceCompact becomes false
- Add comprehensive tests verifying:
  - Basic trigger conditions (shouldForceCompact, canInterrupt, isCompacting)
  - Double-compact prevention (the main bug fix)
  - Guard reset after successful compaction
  - Rapid prop change handling

_Generated with mux_
@ammar-agent ammar-agent force-pushed the fix-double-compact-messages branch from 7aef3f5 to a5339dd Compare December 7, 2025 18:09
@ammario ammario merged commit 2c5f8b0 into main Dec 7, 2025
19 checks passed
@ammario ammario deleted the fix-double-compact-messages branch December 7, 2025 18:15
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