GeezerVison is showing github.com — fix(core): summarize the whole conversation during compaction by neriousy · Pull Request #57091 · anomalyco/opencode
Conversation
neriousy
commented
Oct 19, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Issue for this PR
Fixes #52761
Type of change
- Bug fix
- New feature
- Refactor / code improvement
- Documentation
What does this PR do?
Summary compaction used to send the model only the older part of the conversation (everything before the retained keep tail), then append the summary prompt. That request diverges from the normal request right where the tail starts, so it can only reuse cache entries for the older prefix. Once those entries age out (the latest request's prefix is the only one kept warm), compaction pays full cache-write price for the whole history.
This PR sends the whole current conversation to the summary request, so it extends the latest primary request's prefix exactly. The retained tail is still chosen the same way and kept verbatim beside the summary afterward; the summary now also covers it.
What the summary request sends to the model. Green = same as the last normal request, so it is read from the prompt cache. Red = only cached if a request ending at that point was sent recently. Gray = new.
- Last normal request before compaction (just sent, so all of it is cached):
flowchart LR
a1["system + tools"]:::cached --> a2["older history"]:::cached --> a3["recent tail"]:::cached
classDef cached fill:#d3f9d8,stroke:#2b8a3e,color:#000
classDef risk fill:#ffe3e3,stroke:#c92a2a,color:#000
classDef fresh fill:#f1f3f5,stroke:#868e96,color:#000
classDef sum fill:#e7f5ff,stroke:#1971c2,color:#000
classDef tail fill:#fff3bf,stroke:#e67700,color:#000
Loading
- Before: summary request
flowchart LR
b1["system + tools"]:::cached --> b2["older history<br/>(different prefix: cached only if<br/>a request ended here recently)"]:::risk --> b3["summary prompt"]:::fresh
classDef cached fill:#d3f9d8,stroke:#2b8a3e,color:#000
classDef risk fill:#ffe3e3,stroke:#c92a2a,color:#000
classDef fresh fill:#f1f3f5,stroke:#868e96,color:#000
classDef sum fill:#e7f5ff,stroke:#1971c2,color:#000
classDef tail fill:#fff3bf,stroke:#e67700,color:#000
Loading
- After: summary request
flowchart LR
c1["system + tools"]:::cached --> c2["older history"]:::cached --> c3["recent tail"]:::cached --> c4["summary prompt"]:::fresh
classDef cached fill:#d3f9d8,stroke:#2b8a3e,color:#000
classDef risk fill:#ffe3e3,stroke:#c92a2a,color:#000
classDef fresh fill:#f1f3f5,stroke:#868e96,color:#000
classDef sum fill:#e7f5ff,stroke:#1971c2,color:#000
classDef tail fill:#fff3bf,stroke:#e67700,color:#000
Loading
In the aged runs below, the before request only matched the system prompt and tools (cache reads stayed at exactly 7,937 tokens on GPT-6.1 Sol and 13,056 on Opus 5.5); the whole history was written to the cache again.
What the next request starts with after compaction:
Before:
flowchart LR
d1["summary of older history"]:::sum --> d2["recent tail, verbatim"]:::tail
classDef cached fill:#d3f9d8,stroke:#2b8a3e,color:#000
classDef risk fill:#ffe3e3,stroke:#c92a2a,color:#000
classDef fresh fill:#f1f3f5,stroke:#868e96,color:#000
classDef sum fill:#e7f5ff,stroke:#1971c2,color:#000
classDef tail fill:#fff3bf,stroke:#e67700,color:#000
Loading
After:
flowchart LR
e1["summary of older history + recent tail"]:::sum --> e2["recent tail, verbatim"]:::tail
classDef cached fill:#d3f9d8,stroke:#2b8a3e,color:#000
classDef risk fill:#ffe3e3,stroke:#c92a2a,color:#000
classDef fresh fill:#f1f3f5,stroke:#868e96,color:#000
classDef sum fill:#e7f5ff,stroke:#1971c2,color:#000
classDef tail fill:#fff3bf,stroke:#e67700,color:#000
Loading
The checkpoint layout is unchanged; the summary now also reflects the latest exchange, which is still kept word for word beside it.
One follow-on fix: automatic compaction triggers past the size ceiling, and the request-size guard aimed the summary request at that same ceiling, so a full-history request would always get flattened to text and lose the prefix anyway. The first attempt now aims at the window minus the summary reserve (16k), so the request goes out unchanged whenever the summary still fits. Overflow retries keep their existing shrink targets.
If the whole conversation does not fit the room left for the summary, it falls back to the previous behavior. Flattening only ever shrinks the history before the tail, so it never drops exchanges that would end up in neither the summary nor the tail. Windows where 15% is under the 16k minimum (below ~107k) trigger right at that room, so they always take the fallback.
flowchart TD
start["Summary request"] --> q1{"Whole conversation fits<br/>window minus 16k?"}
q1 -- yes --> whole["Send the whole conversation unchanged<br/>(reuses the last request's cache)"]
q1 -- no --> q2{"History before the tail<br/>fits the compaction ceiling?"}
q2 -- yes --> older["Send the history before the tail unchanged<br/>(previous behavior)"]
q2 -- no --> flat["Flatten the history before the tail to text,<br/>dropping its oldest exchanges"]
classDef cached fill:#d3f9d8,stroke:#2b8a3e,color:#000
classDef old fill:#fff3bf,stroke:#e67700,color:#000
classDef risk fill:#ffe3e3,stroke:#c92a2a,color:#000
class whole cached
class older old
class flat risk
Loading
Reserve: 10% → 15% (still at least 16k). The whole conversation only goes out unchanged if it fits window - 16k, so the gap between the compaction trigger and that limit is what absorbs a large step (e.g. a big tool result) past the trigger. At 10% that gap is 4k on a 200k window and zero below 160k. Replaying real step growth from local sessions (p99 single-step growth ~22k tokens):
| Window |
Falls back at 10% |
Falls back at 15% |
Compactions per long session, 10% → 15% |
| 200k |
~45% |
~11% |
1.44 → 1.53 |
| 272k |
~16% |
~2% |
1.23 → 1.32 |
| 400k |
~6% |
0% |
|
| 1M |
~0% |
0% |
|
Windows below ~107k are still at the 16k minimum and always use the fallback, which is the previous behavior.
Plugins: compaction request hooks now also see the retained tail in messages.
Tradeoff: each compaction request now includes the tail (~15k tokens by default), so when the older prefix is still warm it costs slightly more (2–12% in the large immediate runs below).
How did you verify your code works?
bun typecheck and the core compaction/runner suites (session-compaction, session-runner, session-native-compaction, config/compaction, session-compaction-transport): all pass. The existing prefix-preservation runner test now also asserts the retained tail is in the summary request; added tests that compaction hooks see the full history on updates and that a request past the ceiling but within the window is sent unchanged.
- Live benchmark against a local
serve dev server, opencode/ models, one session per model per scenario: build history with fixed synthetic prompts, then /compact. "Aged" sends four short follow-ups 100s apart first, so older prefixes fall out of a 5m cache while the latest stays warm. Same script on origin/dev and on this branch.
Compaction-request cache hit rate (cache read / total input) and cost:
| Scenario |
Model |
Before |
After |
| ~65k, aged |
GPT-6.1 Sol |
14.5% / $0.119 |
99.1% / $0.009 |
| ~65k, aged |
Claude Opus 5.5 |
16.4% / $0.344 |
99.1% / $0.031 |
| ~65k, aged |
DeepSeek V4 Pro |
90.5% / $0.017 |
99.1% / $0.012 |
| ~65k, aged |
Grok 4.7 |
85.9% / $0.044 |
99.1% / $0.039 |
| ~240k, immediate |
GPT-6.1 Sol |
99.7% / $0.025 |
99.7% / $0.026 |
| ~240k, immediate |
Claude Opus 5.5 |
99.7% / $0.077 |
99.8% / $0.080 |
| ~240k, immediate |
DeepSeek V4 Pro |
99.8% / $0.035 |
99.8% / $0.037 |
| ~240k, immediate |
Grok 4.7 |
99.7% / $0.233 |
99.7% / $0.262 |
| ~240k, aged |
GPT-6.1 Sol |
3.5% / $0.555 |
99.8% / $0.027 |
| ~240k, aged |
Claude Opus 5.5 |
3.8% / $1.684 |
99.8% / $0.083 |
| ~240k, aged |
DeepSeek V4 Pro |
99.2% / $0.039 |
99.8% / $0.038 |
| ~240k, aged |
Grok 4.7 |
94.1% / $0.295 |
99.8% / $0.266 |
DeepSeek and Grok keep older prefixes cached longer, so they degrade less before this change. These are single samples, so small cost differences are within noise.
Screenshots / recordings
N/A
Checklist
- I have tested my changes locally
- I have not included unrelated changes in this PR
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Send the full current history to the summary request so it extends the primary request's cached prefix, while still retaining the recent tail verbatim beside the summary. The first attempt is sent unchanged whenever the window still fits the summary, so automatic compaction past the ceiling does not flatten the request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requesting changes: this fixes the cold-cache compaction, but there is a regression in the shrink path.
How I checked it: I ran the TUI from source with the simulated model (three prompts, then /compact) and logged the request bodies.
- On the base, the summary request stopped before the latest exchange and wasn't a prefix of the previous request.
- With this PR, it is exactly the previous request's messages plus the summary prompt.
The 3 new tests fail on the base and pass here. The core compaction and runner suites pass, and the packages/core type check is clean.
Public API: unchanged. One behaviour change for plugins: compaction request hooks now also see the retained tail.
Concern: when the summary request has to be shrunk, older history can now be left out of the summary entirely (see the inline comment). In that case it should fall back to summarising only the older part, as before.
Fixes #52761
Related: #48542, #54107
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
A summary request that has to be flattened has lost its cached prefix, so it falls back to the history before the tail, aimed at the previous target. Otherwise dropping the oldest exchanges to make room for the tail lost the exchanges just before it from both the summary and the tail.
When the whole conversation does not fit the room left for the summary, send the history before the retained tail unchanged if it fits, as before this change, and flatten only when neither fits. On windows where 10% is under the 16k minimum, the trigger equals that room, so every automatic compaction flattened the request and lost the cache match the previous code kept.
Raise the default automatic compaction reserve from 10% to 15% (still at least 16k). On 200k-400k windows the extra margin above the summary room absorbs most single-step jumps past the trigger, so the whole conversation is sent unchanged and reuses the prompt cache more often. Replaying real session step growth: at 200k, summary requests that cannot send the whole conversation drop from ~45% to ~11%; at 272k from ~16% to ~2%.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Oct 20, 2026
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
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.
|