GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(core): summarize the whole conversation during compaction by neriousy · Pull Request #57091 · anomalyco/opencode

Conversation

@neriousy

@neriousy neriousy commented Oct 19, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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.

  1. 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
  1. 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
  1. 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.

All reactions
Filip Hejmowski added 2 commits October 19, 2026 15:43
Filip Hejmowski
fix(core): summarize the whole conversation during compaction
9654340
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.
Filip Hejmowski
test(core): drop redundant compaction prefix test
61d14f7
opencode-agent[bot]
opencode-agent Bot reviewed Oct 19, 2026
View reviewed changes

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

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.

All reactions
Comment thread packages/core/src/session/compaction.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Filip Hejmowski and others added 5 commits October 19, 2026 16:13
Filip Hejmowski
fix(core): shrink only history before the retained tail
41b675c
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.
@neriousy
Merge branch 'dev' into compaction-cache-prefix
2c4cd34
Filip Hejmowski
fix(core): send prior history unchanged before flattening compaction
ea73760
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.
Filip Hejmowski
Merge remote-tracking branch 'origin/compaction-cache-prefix' into co…
9eb8981
…mpaction-cache-prefix
Filip Hejmowski
feat(core): keep 15% of the window free before compacting
ad8d7e3
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%.
@neriousy neriousy mentioned this pull request Oct 19, 2026
test(core): bound advance by real time instead of steps #57119
Merged
3 of 6 tasks
@neriousy
neriousy merged commit fd65892 into dev Oct 19, 2026
25 of 26 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@neriousy
neriousy deleted the compaction-cache-prefix branch October 19, 2026 17:02
@kobzevvv kobzevvv mentioned this pull request Oct 20, 2026
chore(upstream): порт v2.1.0 — 26 в порт, 25 на решение gpt-mem/opencode-mem#168
Closed
8 of 10 tasks
@neriousy neriousy mentioned this pull request Oct 20, 2026
feat(core): add experimental background compaction summaries #57444
Merged
2 of 6 tasks
This was referenced Oct 20, 2026
chore(upstream): порт v2.1.1 — 11 в порт, 21 на решение gpt-mem/opencode-mem#185
Open
chore(upstream): порт v2.1.2 — 20 в порт, 26 на решение gpt-mem/opencode-mem#189
Open
@reynold-hu reynold-hu mentioned this pull request Oct 21, 2026
fix(core): bound auto-compaction when the buffer exceeds the window #56786
Closed
3 of 6 tasks
@bornmw

bornmw commented Oct 22, 2026

Copy link
Copy Markdown

this works

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bornmw bornmw mentioned this pull request Oct 22, 2026
fix(core): keep the prompt cache when compaction retains sent messages #54107
Closed
3 of 6 tasks
@kobzevvv kobzevvv mentioned this pull request Oct 22, 2026
chore(upstream): порт v2.1.3 — 33 в порт, 122 на решение gpt-mem/opencode-mem#207
Open
5 tasks
@xxwtiancai xxwtiancai mentioned this pull request Oct 25, 2026
compaction: manual compaction keeps full conversation in next request #54910
Open
@ivanfc ivanfc mentioned this pull request Oct 30, 2026
[BUG] V2: Auto-compaction accepts task-empty summary after shortened transcript, silently losing active objective (Ollama/Qwen) #60663
Open
@lihaokun lihaokun mentioned this pull request Nov 5, 2026
fix: 分批回移上游 V1/共享底层修复(错误分类、配置、MCP 与 ripgrep) lihaokun/opencode#108
Open
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@opencode-agent opencode-agent[bot] opencode-agent[bot] left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

V2: summary compaction still reads almost nothing from the prompt cache, even right after a warm request

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@neriousy @bornmw
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.