You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
🤖 feat: add auto-compaction with progressive warnings (#683)
## Stack
1. #685
1. #683 ⬅ This PR
1. #670
1. #650 (base)
## Summary
Adds automatic context compaction that triggers at 70% usage, with
progressive countdown warnings starting at 60%.
<img width="905" height="155" alt="image"
src="https://github.com/user-attachments/assets/b0db20c5-c377-44bb-891c-f8ddadd561c8"
/>
<img width="891" height="194" alt="image"
src="https://github.com/user-attachments/assets/6385cfd2-5e3c-45ec-afce-935dae56ad1a"
/>
Relates to #651.
## Key Changes
**Auto-Compaction:**
- Triggers automatically when current context usage reaches 70% of
model's context window
- Queues user's message to send after compaction completes
- Includes image parts in continue messages
**Progressive Warnings:**
- Shows countdown at 60-69% usage: "Context left until Auto-Compact: X%
remaining"
- Shows urgent message at 70%+: "⚠️ Approaching context limit. Next
message will trigger auto-compaction."
**Implementation:**
- New `shouldAutoCompact()` utility centralizes threshold logic with
configurable constants
- Returns `{ shouldShowWarning, usagePercentage, thresholdPercentage }`
- Uses **last usage entry** (current context size) to match UI token
meter display
- Excludes historical usage from threshold check to prevent infinite
compaction loops
- `ContinueMessage` type now includes optional `imageParts`
## Technical Details
**Usage Calculation:**
The auto-compaction check uses the most recent usage entry from
`usageHistory` to calculate the current context size. This matches the
percentage displayed in the UI token meter and correctly handles
post-compaction scenarios:
- **Before compaction**: Last entry represents full context → triggers
at 70% correctly
- **After compaction**: Last entry excludes historical usage → resets to
actual context size
- **Historical usage preserved**: Remains in usage history for cost
tracking, but not used for threshold calculations
This prevents the infinite loop where post-compaction workspaces would
continuously re-compact because historical usage tokens were being
included in the threshold check.
## Future Work
Future PRs will add user settings to configure auto-compaction
(enable/disable, custom threshold).
_Generated with `mux`_
0 commit comments