fix(core): keep the prompt cache when compaction retains sent messages by AnnoyingTechnology · Pull Request #54107 · anomalyco/opencode · GitHub
Skip to content

fix(core): keep the prompt cache when compaction retains sent messages - #54107

Draft
AnnoyingTechnology wants to merge 1 commit into
anomalyco:devfrom
AnnoyingTechnology:compaction-full-prefix
Draft

AnnoyingTechnology wants to merge 1 commit into
anomalyco:devfrom
AnnoyingTechnology:compaction-full-prefix

Conversation

@AnnoyingTechnology

@AnnoyingTechnology AnnoyingTechnology commented Oct 9, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #52761
Fixes #52761

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Summary compaction sends only split.older. recent starts at a user message and usually holds exchanges the provider has already seen, so the summary request is a truncated copy of the last request instead of an extension of it. Caches that only match at their own breakpoints (Anthropic) or at saved recurrent states (hybrid models on SGLang) miss, and the older part is billed or prefilled again.

When the compaction model is the session model and there is room, the summary request now runs through the latest assistant reply, so it extends the last request. Unanswered trailing input stays out and the stored recent is unchanged. If that request is rejected as too long or too large, or the summary hits the output limit, it falls back to the existing older-only path. Both attempts share one retry allowance, and the auto Started event is published once.

Known limitation: after a cut-off first reply, the mini TUI shows the discarded draft before the final summary, as the template-reminder retry already does. The stored summary is correct. This does not explain the OpenAI rows in #52761.

How did you verify your code works?

  • New tests fail on v2 and pass here: wire-level prefix for manual and auto on OpenAI Chat, Responses and Anthropic Messages; fallback after a cut-off reply, without room, over the input limit; one retry allowance across the fallback; configured session model vs another model.
  • bun run check passes. packages/core passes except "isolates global home and XDG roots", which fails the same way on v2 on this machine.
  • Live on a local hybrid model (Qwen3.8 Flash): the summary request is now the previous request plus the reply and prompt (before: 3 of its 8 messages). That engine kept a state at the old cut, so it showed no cache difference. Not tested on Anthropic or SGLang.

Screenshots / recordings

Not applicable.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Patch, tests and live runs by Claude Opus 5.5 (via Claude Code), for @AnnoyingTechnology. Reviewed by GPT 6.1 Sol (xhigh, via Codex).

🤖 Generated with Claude Code

Summary compaction sent only the older part. When the retained recent
part held exchanges the provider had already seen, that request was a
truncated copy of the session's last request, and breakpoint or
recurrent-state caches missed on it. Summarize through the latest
assistant reply when it fits, and fall back to the older part otherwise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

@thdxr
thdxr changed the base branch from v2 to dev October 10, 2026 19:38
@bornmw

bornmw commented Oct 11, 2026 •

Copy link
Copy Markdown

Wow, it does work! I've hit multiple auto-compactions running this branch, and cache hit rate stays at 1 until the compacted (new) prompt is sent to the model.
For 262k context is saves many minutes on local and a lot of $$$ on subscriptions.
I hope it gets merged

@bornmw

bornmw commented Oct 22, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
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.

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

2 participants