opencode-goal-plugin icon indicating copy to clipboard operation
opencode-goal-plugin copied to clipboard

[Bug]: V2: different system prompts between context and compaction hooks invalidate prompt-cache reuse, even without an active goal

Open yankedi opened this issue 1 month ago • 1 comments

What happened?

The plugin injects systemReminder(locale) into ordinary model requests through the V2 context hook, but does not inject the same reminder through the compaction hook. This changes the system prefix before the conversation history and can invalidate prompt-cache reuse.

On my local backend, the request immediately before compaction reused 229,453 of 230,265 input tokens. The compaction request reused 0 and reprocessed all 244,678 input tokens, taking approximately 30 minutes for input processing alone.

I checked the installed 0.1.60 bundle and the current upstream implementation:

https://github.com/prevalentWare/opencode-goal-plugin/blob/cf22feaaa7ab3367053a9a99913cbc40e5c3224e/src/server.ts#L3284-L3313

The hooks behave differently:

  • context unconditionally appends systemReminder(locale), including when the session has no goal.
  • compaction returns immediately when getGoal() returns null.
  • With a goal, compaction appends a different compactionContext(goal, locale) system block, rather than the same reminder.

Captured request comparison confirmed that:

  • The ordinary system message contained 14,668 characters.
  • The compaction system message contained 13,179 characters.
  • The missing 1,489 characters were exactly one newline plus the plugin's 1,488-character goal policy.
  • Tool definitions were identical between the two requests.

I also reproduced the no-goal mismatch offline by executing the actual installed hook registrations in an isolated Node VM with getGoal() returning null. The generated policy matched the missing text exactly.

This may also increase costs on cloud providers offering discounted cached input or charging for cache writes, although I have not measured cloud billing for this plugin.

Related reports:

  • https://github.com/anomalyco/opencode/issues/52761#issuecomment-6087386072
  • https://github.com/DietrichGebert/ponytail/issues/1086

These describe the same class of context/compaction hook inconsistency in another plugin.

Expected behavior

Ordinary and compaction requests should preserve the same shared system prefix where possible, allowing the backend to reuse previously processed conversation history.

This should also hold when the plugin is loaded but the session has no goal.

The static policy should be injected consistently and idempotently through both hooks. Compaction-only goal snapshots should ideally be placed in trailing compaction context rather than changing the system prefix before the history.

Goal information must still be preserved in the summary.

Steps to reproduce

  1. Load @prevalentware/opencode-goal-plugin 0.1.60 in OpenCode V2.
  2. Use a backend that exposes cached-input usage or prompt-reuse statistics.
  3. Start a session without creating a goal and grow a conversation.
  4. Confirm that an ordinary follow-up reuses most of the preceding input.
  5. Trigger compaction while the preceding conversation is still warm.
  6. Capture and compare the ordinary and compaction requests:
    • The ordinary system prompt includes "OpenCode goal mode policy".
    • The compaction system prompt omits that block.
  7. Compare cached/reused tokens and input-processing time.

An offline reproduction of the installed hooks also shows the mismatch: starting with identical base-system parts and getGoal() returning null, the context hook appends the policy while the compaction hook leaves the system unchanged.

I have not yet run an end-to-end backend A/B with a patched plugin, so no specific post-fix speedup is claimed.

Plugin version

0.1.60

OpenCode version

2.0.13

Operating system

NixOS / Linux x86_64

Model/provider in use

strata/coder — local Strata 0.1.44.1, OpenAI-compatible Chat Completions, Qwen3.8-Flash-Next Coder IQ1_M; 262144-token context, int8 KV, reasoning_effort=medium

Plugin options


Relevant goal state or logs

Consecutive engine records:

Ordinary request:
prompt 230265 tokens = 229453 reused + 812 read in 29199 ms,
286 generated in 19513 ms

Compaction request:
prompt 244678 tokens = 0 reused + 244678 read in 1825261 ms (134.1 tok/s),
4488 generated in 266852 ms (16.8 tok/s)

Compaction end-to-end duration: 2092.436 seconds (~34m52s)
Input processing: 1825.261 seconds (~30m25s)
Summary generation: 266.852 seconds (~4m27s)

Captured system-message comparison:
ordinary_system_chars = 14668
compaction_system_chars = 13179
removed_chars = 1489
removed_text = newline + exact systemReminder("en") output
tool_definitions_identical = true

Offline execution of actual installed hook registrations:
{
  "goal": null,
  "ordinary_system_parts": 2,
  "compaction_system_parts": 1,
  "policy_chars": 1488,
  "captured_removed_chars": 1489,
  "exact_policy_match": true
}

Installed package: @prevalentware/[email protected]
Upstream source checked: cf22feaaa7ab3367053a9a99913cbc40e5c3224e

Offline reproduction did not call a model or modify persisted goal state.
Cloud billing impact has not been measured.

yankedi avatar Oct 15 '26 04:10 yankedi

i am sorry i use ai to repo this issue,might be difficult to read

yankedi avatar Oct 15 '26 04:10 yankedi