Skip to content

Conversation

@ethanndickson
Copy link
Member

@ethanndickson ethanndickson commented Nov 28, 2025

Summary

Automatically triggers compaction when live token usage approaches the context window limit during streaming. This prevents the AI from hitting context limit errors mid-response.

Changes

Force Compaction Logic

  • Extended checkAutoCompaction() to return shouldForceCompact flag
  • Uses liveUsage (real-time streaming tokens) with fallback to lastUsage
  • Triggers when remaining tokens ≤ 5200 (2× expected compaction output)
  • Works even with empty usageHistory (first message streaming)

AIView Integration

  • New useEffect monitors shouldForceCompact during active streams
  • Tracks triggered stream ID to prevent duplicate compactions
  • Sends compaction request with "Continue with current task" as follow-up

Queue & Restore Fixes

  • Moved restore-to-input from stream-abort handler to IPC interruptStream handler
    • User interrupts (Ctrl+C) still restore queued messages to input
    • Internal aborts (compaction flow) preserve queue for follow-up

Shared Constants

  • DEFAULT_COMPACTION_WORD_TARGET = 2000
  • WORDS_TO_TOKENS_RATIO = 1.3
  • FORCE_COMPACTION_TOKEN_BUFFER = 5200 (derived: 2 × 2000 × 1.3)

Testing

  • 31 unit tests for checkAutoCompaction including force compaction scenarios
  • Manually tested by asking the agent to read a large file in 22 chunks whilst summarising each chunk. The process completed successfully even with a force-compaction triggering mid-task.

Generated with mux

@ethanndickson ethanndickson force-pushed the force-compaction-interrupt branch from 41d3661 to 22443ea Compare December 1, 2025 01:23
Add force compaction that triggers automatically when live token usage
approaches the context window limit during streaming. This prevents the
AI from hitting context limit errors mid-response.

Key changes:

1. Auto-compaction check extended with shouldForceCompact flag
   - Uses liveUsage (real-time streaming) or falls back to lastUsage
   - Triggers when remaining tokens <= FORCE_COMPACTION_TOKEN_BUFFER
   - Works even with empty usageHistory (first message streaming)

2. AIView triggers force compaction during active streams
   - Tracks triggered stream to prevent duplicate compactions
   - Sends compaction request with 'Continue with current task' message

3. Queue restore moved from stream-abort to IPC interrupt handler
   - Prevents continueMessage from bouncing back to input on internal aborts
   - User-initiated interrupts still restore queued messages to input

4. Shared constants for compaction parameters
   - DEFAULT_COMPACTION_WORD_TARGET = 2000
   - WORDS_TO_TOKENS_RATIO = 1.3
   - FORCE_COMPACTION_TOKEN_BUFFER = 5200 (2x expected output)
@ethanndickson ethanndickson force-pushed the force-compaction-interrupt branch from 22443ea to ea4d8fc Compare December 1, 2025 01:25
@ethanndickson
Copy link
Member Author

@codex review

@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. Keep them coming!

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@ethanndickson ethanndickson added this pull request to the merge queue Dec 1, 2025
github-merge-queue bot pushed a commit that referenced this pull request Dec 1, 2025
## Summary

Automatically triggers compaction when live token usage approaches the
context window limit during streaming. This prevents the AI from hitting
context limit errors mid-response.

## Changes

### Force Compaction Logic
- Extended `checkAutoCompaction()` to return `shouldForceCompact` flag
- Uses `liveUsage` (real-time streaming tokens) with fallback to
`lastUsage`
- Triggers when remaining tokens ≤ 5200 (2× expected compaction output)
- Works even with empty `usageHistory` (first message streaming)

### AIView Integration
- New `useEffect` monitors `shouldForceCompact` during active streams
- Tracks triggered stream ID to prevent duplicate compactions
- Sends compaction request with "Continue with current task" as
follow-up

### Queue & Restore Fixes
- **Moved restore-to-input** from `stream-abort` handler to IPC
`interruptStream` handler
  - User interrupts (Ctrl+C) still restore queued messages to input
  - Internal aborts (compaction flow) preserve queue for follow-up

### Shared Constants
- `DEFAULT_COMPACTION_WORD_TARGET = 2000`
- `WORDS_TO_TOKENS_RATIO = 1.3`
- `FORCE_COMPACTION_TOKEN_BUFFER = 5200` (derived: 2 × 2000 × 1.3)

## Testing

- 31 unit tests for `checkAutoCompaction` including force compaction
scenarios
- Manually tested by asking the agent to read a large file in 22 chunks
whilst summarising each chunk. The process completed successfully even
with a force-compaction triggering mid-task.

---
_Generated with `mux`_
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 1, 2025
@ethanndickson ethanndickson added this pull request to the merge queue Dec 1, 2025
Merged via the queue into main with commit 7d668fe Dec 1, 2025
13 checks passed
@ethanndickson ethanndickson deleted the force-compaction-interrupt branch December 1, 2025 03:34
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.

1 participant