Skip to content

fix(core): keep the goal reminder out of the cache tail - #65788

Merged
neriousy merged 1 commit into
devfrom
goal-reminder-cache
Nov 23, 2026
Merged

neriousy merged 1 commit into
devfrom
goal-reminder-cache

Conversation

@neriousy

@neriousy neriousy commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

hint: if you need to wait for this pull request to be reviewed, merged, or closed before continuing, use this instead of polling:
gh event listen --resource #65788 --event com.github.pull_request.review.submitted.v1,com.github.pull_request.state.changed.v1

  • It prints the first event as a JSON line on stdout and exits; run it again to wait for the next one.
  • Add --timeout to stop waiting after a while, for example --timeout 30m; it then exits with status 8.
  • Events carry no details; read the pull request to see what changed.
  • It misses changes made before it starts, so read the pull request once it is running.
  • See gh event listen --help for other event types.
  • Event subscriptions are in preview and may not be enabled for your account.

Issue for this PR

Closes #

Type of change

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

What does this PR do?

after a compaction, an active goal adds an unsaved reminder (<system-update>A goal is active…) on every step until the next continuation restates the objective. it went at the end of the messages, and anthropic's last cache point (messages: { tail: 1 } in packages/ai/src/cache-policy.ts) landed right on it. the next step has assistant + tool where the reminder was, so the previous step's cache entry never matched again. only tools + system hit the cache, and the whole compacted history got re-written at cache-write price (1.25x instead of 0.1x) for the rest of that goal round

it fires on most mid-turn compactions in long goal rounds. with background summaries (default) the part kept word for word starts ~60k tokens before the limit, so the kickoff / continuation usually falls out of it

fix: put the reminder right after the first message (the compaction summary) instead of at the tail. same content, still not saved, still stops once a continuation lands. one cache miss is left when the reminder goes away at the continuation, so once per compaction instead of every step

How did you verify your code works?

ran the post-compaction goal turn (6 steps, tool calls) through applyCachePolicy with an anthropic model and checked if the previous step's last cache point is still a prefix of the next request

before

Step Messages (📍 = last cache point) Previous step's cache entry reused?
1 summary → reminder → prompt 📍 n/a (first step after compaction)
2 summary → prompt → assistant → tool → reminder 📍 no
3 summary → prompt → assistant → tool → assistant → tool → reminder 📍 no
4 summary → prompt → … → assistant → tool → reminder 📍 no
5 summary → prompt → … → assistant → tool → reminder 📍 no
6 summary → prompt → … → assistant → tool → reminder 📍 no

after

Step Messages (📍 = last cache point) Previous step's cache entry reused?
1 summary → reminder → prompt 📍 n/a (first step after compaction)
2 summary → reminder → prompt → assistant → tool 📍 yes
3 summary → reminder → prompt → assistant → tool → assistant → tool 📍 yes
4 summary → reminder → prompt → … → assistant → tool 📍 yes
5 summary → reminder → prompt → … → assistant → tool 📍 yes
6 summary → reminder → prompt → … → assistant → tool 📍 yes

for reference, the same run without a goal reuses the cache on every step, same as "after"

  • added a test in packages/core/test/plugin/goal.test.ts that checks each step's request starts with the previous one and has the reminder exactly once. fails on dev, passes here
  • bun run script/test.ts test/plugin/goal.test.ts --rerun-each 5 80/80, bun typecheck in core clean

Live run (opencode/claude-opus-5-5)

same goal task twice, on dev and on this branch: read 24 source files one per step. compaction forced at ~65k tokens (compaction.buffer: "93%", keep.tokens: 2000) so it lands mid-turn and the kickoff falls out of the kept part. tokens straight from the assistant messages

before compaction (steps 1-12) both runs are the same: ~426k cached read, ~66k written, $0.43 / $0.44

after compaction, same 9 steps (13-21):

cache read cache write cost
dev 98,913 210,963 $1.09
this PR 289,789 60,827 $0.38

per step on dev, cache read stays stuck at 11,826 (tools + system) and the write grows with the history: 7k → 10k → 15k → 17k → 25k → 34k → 43k → 46k. on this PR, read grows (17k → 28k → … → 56k) and the write is just the new tool result (1-10k)

so ~2.9x on those 9 steps, and it gets worse the longer the round runs: last step was $0.235 vs $0.057 (~4x)

Screenshots / recordings

n/a

Checklist

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

Added by the opencode bot (AI-generated).

Related: #62554, #63498

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks sound. I ran the new test against the base sources and it fails there: step 2's request diverges from step 1's because the reminder sits at the tail. With this change all 16 goal plugin tests pass, and typecheck:tsc in packages/core is clean. This checks the requests OpenCode builds, not billed cache reads from a live provider.

Public API: unchanged (only where the plugin's request-only reminder goes in the context hook).

One thing for maintainers: #63498 changes the same context hook in goal.ts and fixes this differently. It marks the reminder as one-off (transient) text so the tail cache point skips it, which also avoids the one cache miss this approach leaves when the reminder goes away. It also covers the Plan reminder and the other one-off prompts from #62554. The two will conflict, so it's worth deciding which one to take.

Related: #62554, #63498

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.

1 participant