Why
V2 sessions previously grew until the provider rejected the request. This adds automatic compaction while preserving the durable transcript and avoiding provider-native history replay after the conversation prefix changes.
Invariant: compaction changes the model representation, never the durable conversation record.
Stage 1: Detect pressure before calling the provider
The runner assembles the complete candidate request first: System Context, projected conversation, tools, and the selected model's output allowance. It estimates that request and compares it with the model window after reserving absolute headroom.
sequenceDiagram
participant User
participant Runner
participant Budget
participant Summarizer
participant Model
User->>Runner: durable prompt admitted
Runner->>Budget: estimate complete candidate request
Budget-->>Runner: exceeds safe input budget
Runner->>Summarizer: summarize older serialized history
Summarizer-->>Runner: structured textual summary
Runner->>Model: checkpoint plus pending prompt
Model-->>User: continued answer
Loading
The trigger is:
estimated request > context limit - max(output allowance, compaction.buffer)
The reserve is model-aware and absolute rather than a fixed percentage. Before publishing a compaction attempt, the runner also verifies that the summarizer request itself fits in the model window. Summary output is capped at 4,096 tokens.
Compression line: budget the next complete request, not the previous turn.
Stage 2: Build one portable textual checkpoint
Older history is summarized with the existing structured contract:
## Goal
## Constraints & Preferences
## Progress
### Done
### In Progress
### Blocked
## Key Decisions
## Next Steps
## Critical Context
## Relevant Files
Recent context is retained by token budget and serialized as plain text. The completed checkpoint lowers as one ordinary user-compatible message, clearly labelled as historical context rather than new instructions.
sequenceDiagram
participant History
participant Selector
participant Summarizer
participant Checkpoint
History->>Selector: previous checkpoint plus new messages
Selector-->>Summarizer: older serialized context
Summarizer-->>Checkpoint: replacement structured summary
Selector-->>Checkpoint: token-bounded recent context
Checkpoint-->>History: one portable textual message
Loading
The checkpoint deliberately does not replay retained assistant, reasoning, or tool messages in provider-native form. This avoids carrying Anthropic or Bedrock signatures, encrypted reasoning, and provider tool-protocol state across a changed prefix.
Invariant: nothing signed or encrypted by a provider survives the compaction boundary.
Stage 3: Activate only a completed checkpoint
Started is a durable operational fact, Delta is transient, and only Ended changes active context.
sequenceDiagram
participant Runner
participant Events
participant Projection
participant Epoch
Runner->>Events: Started v1 (durable)
Runner-->>Events: Delta (transient)
alt summary succeeds
Runner->>Events: Ended v2 (durable)
Events->>Projection: append checkpoint
Projection->>Epoch: request baseline replacement
else summary fails or is empty
Runner-->>Projection: leave prior history active
end
Loading
The V1 completed-event decoder remains registered for stored beta-event replay compatibility. Failed, interrupted, oversized, or empty-summary attempts never project a checkpoint.
Invariant: Started records an attempt; Ended changes context.
Stage 4: Preserve durability while shortening active context
Compaction appends to history rather than deleting or rewriting it. The read side uses the latest completed checkpoint as the active-context boundary.
sequenceDiagram
participant Store
participant FullHistory
participant ActiveContext
Store-->>FullHistory: user, prior messages, checkpoint, continuation
Store-->>ActiveContext: latest checkpoint, continuation
FullHistory-->>ActiveContext: same durable source, different projection
Loading
After a successful real-model canary, persisted state was:
full history: [user, compaction, assistant]
active context: [compaction, assistant]
summary: 788 characters
recent context: 4,800 characters
The post-compaction answer correctly recovered CERULEAN-IBIS, eu-west-3, and rollback code 7319 using opencode/mimo-v2.5-free.
Configuration
This PR does not add compaction.buffer or compaction.keep.tokens; those V2 options already existed. It changes the retention contract by removing V2 compaction.keep.turns, making recent-context retention token-only.
{
"compaction": {
"auto": true,
"prune": true,
"keep": {
"tokens": 2000,
},
"buffer": 10000,
},
}
auto enables automatic pressure-triggered compaction.
prune retains the existing pruning configuration.
keep.tokens bounds recent history serialized into the checkpoint.
buffer reserves absolute context headroom before the provider call.
keep.turns is removed because one token-heavy turn can defeat a turn-count policy.
V1 configuration continues to migrate as follows:
V1 compaction.reserved -> V2 compaction.buffer
V1 compaction.preserve_recent_tokens -> V2 compaction.keep.tokens
V1 compaction.tail_turns -> removed; no V2 equivalent
V1 runtime behavior remains intact; V1 and V2 now share the summary prompt builder and conservative token estimator.
User Experience
The TUI keeps the first-class compaction message in synchronized state but renders only a small Auto Compaction or Compaction divider. It does not expose the summary or serialized recent context in the visible transcript.
Real-Model Lessons
The canary also exercised two important safety branches:
- A declared 12,000-token context could not fit the roughly 15,000-token summarizer request. Preflight declined compaction before
Started, leaving history unchanged.
opencode/deepseek-v4-flash-free returned no textual summary. The runner rejected the empty result and did not publish Ended, so no checkpoint activated.
- With a 30,000-token declared context and
opencode/mimo-v2.5-free, summary generation, checkpoint activation, and continuation all completed successfully.
Compression line: if no valid textual summary completes, no history boundary moves.
Verification
- Core typecheck
- OpenCode typecheck
- JavaScript SDK typecheck
- 111 focused Core tests
- 53 focused OpenCode tests, 4 existing skips
- Workspace pre-push typecheck: 22/22 packages
- File-scoped oxlint: no errors
git diff --check
- Real-provider canary with
opencode/mimo-v2.5-free
Deferred
- Typed provider-overflow recovery with one bounded retry
- Explicit public manual-compaction operation
- Deterministic old tool-result pruning
- Model-specific tokenizers beyond the shared conservative estimator