proxy icon indicating copy to clipboard operation
proxy copied to clipboard

fix: prevent DeepSeek cache invalidation from mid-conversation system-role messages

Open hachimi-ak-ioi opened this issue 5 months ago โ€ข 1 comments

Problem

When users route Claude Code through oc-go-cc to DeepSeek models on OpenCode Go, they experience sporadic bursts of massive cache miss โ€” up to 400K tokens billed at full cache-miss rate with no visible errors. The cache hit rate collapses from ~95% to near 0% on affected requests, then recovers on subsequent turns.

Root cause

Two independent discoveries that compound into the same effect:

1. DeepSeek reorders system-role messages

DeepSeek V3.x and V4 internally reorder all {"role": "system"} messages to the front of the effective prompt. This is unlike OpenAI/Anthropic, which keep messages in their original array order.

2. Claude Code injects periodic system reminders mid-conversation

Claude Code periodically appends {"role": "system"} messages to the conversation (e.g. "The task tools haven't been used recently..."). These land at the end of the messages array via the Anthropic protocol, and oc-go-cc was forwarding them as-is.

When these combine, DeepSeek reorders the newly-appended system message to the front, shifting every user/assistant/tool message that follows it. The byte offset of all subsequent content changes, DeepSeek's prefix-based KV cache sees no match, and the entire conversation is re-processed at full cache-miss cost.

Evidence

  • 14-turn trace capture shows system messages scattered at indices 2, 18, 34, 51, 65, 77, 97, โ€ฆ throughout the messages array
  • Billing dashboard confirmed two requests at 11:48 and 11:49 with ~395K tokens each billed entirely at cache-miss rate ($0.055/request)
  • After fix: system message prefix hash is identical across all 14 consecutive turns; no cache-miss spikes on dashboard

Fix

Convert {"role": "system"} messages that are not part of the top-level system prompt into {"role": "user"} messages wrapped in <system-reminder> XML tags. This preserves semantic intent while preventing DeepSeek from reordering them past the conversational history. The prefix cache stays stable across turns.

Testing

  • Built, tested (all 57 existing tests pass), and deployed to production
  • Verified 14+ consecutive turns with identical system-message hash
  • OpenCode Go billing dashboard no longer shows anomalous cache-miss spikes

hachimi-ak-ioi avatar Jun 16 '26 04:06 hachimi-ak-ioi

Code Review Roast ๐Ÿ”ฅ

Verdict: No Issues Found | Recommendation: Merge

Well, look at that โ€” the developers actually listened. The string-concat-in-a-loop anti-pattern has been exorcised, and tool_use blocks are now handled instead of silently dropped. I was reaching for my flamethrower and they went and fixed the damn code.

๐ŸŽ‰ Best part: The Gemini and Responses transformers now use strings.Builder for text concatenation (O(n) instead of O(nยฒ)), and tool_use blocks are properly converted to text placeholders. This is the performance-review equivalent of actually passing a sobriety test.

๐Ÿ’€ Worst part: Nothing to roast here. The fixes are clean and consistent with the existing codebase patterns. I am... disappointed. No tears of joy, no rage, just clean code.

๐Ÿ“Š Overall: Like a chef who burned down their last kitchen but somehow learned to cook โ€” the same code that had me sharpening my claws now gets a pass. All previous issues resolved.

Files Reviewed (2 files)
  • internal/transformer/gemini.go - 2 fixes applied (tool_use + strings.Builder)
  • internal/transformer/responses.go - 2 fixes applied (tool_use + strings.Builder)
Previous Review Summaries (2 snapshots, latest commit 00dd980)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 00dd980)

Verdict: 4 Issues Found | Recommendation: Address before merge

Overview

Severity Count
๐Ÿšจ critical 0
โš ๏ธ warning 2
๐Ÿ’ก suggestion 2
๐Ÿค nitpick 0
Issue Details (click to expand)
File Line Severity Issue
internal/transformer/gemini.go 61 suggestion String concatenation in loop (O(nยฒ) performance)
internal/transformer/responses.go 52 suggestion String concatenation in loop (O(nยฒ) performance)
internal/transformer/gemini.go 46 warning Missing tool_use case in switch statement
internal/transformer/responses.go 56 warning Missing tool_use handling, role mapping issues

๐Ÿ† Best part: The DeepSeek regression tests finally landed. The strings.Builder fix was applied and tests cover the rewrite logic. I'm genuinely surprised โ€” I had my flamethrower all warmed up.

๐Ÿ’€ Worst part: Two brand new transformer files (gemini.go, responses.go) that copy-paste the string-concat-in-a-loop anti-pattern from the pre-fix code, while also forgetting to handle tool_use blocks. Nothing says 'production ready' like silently dropping function calls.

๐Ÿ“Š Overall: Like hiring a chef who burned down their last kitchen. The DeepSeek work is solid now, but the new transformers are missing critical ingredients.

Files Reviewed (4 files)
  • internal/transformer/request.go - Previous issue resolved (DeepSeek tests added)
  • internal/transformer/gemini.go - 2 issues (string concat + missing tool_use)
  • internal/transformer/responses.go - 2 issues (string concat + missing tool_use)
  • internal/transformer/request_test.go - New DeepSeek regression tests

Fix these issues in Kilo Cloud

Previous review (commit 338db5e)

Verdict: 1 Issue Found | Recommendation: Address before merge

Overview

Severity Count
๐Ÿšจ critical 0
โš ๏ธ warning 0
๐Ÿ’ก suggestion 1
๐Ÿค nitpick 0
Issue Details (click to expand)
File Line Roast
internal/transformer/request.go 347 The DeepSeek-only system-message rewrite has no regression tests, which is the exact code path that protects users from another expensive cache-miss bonfire.

Fix these issues in Kilo Cloud


๐Ÿ† Best part: The latest diff is actually narrowly gated behind isDeepSeekModel() and uses strings.Builder; previous feedback landed, and it did not arrive wearing clown shoes.

๐Ÿ’€ Worst part: The cache-miss fix itself is untested. That's like installing a fire suppression system and then refusing to light a match during inspection.

๐Ÿ“Š Overall: Useful fix, but add regression coverage before merge so the next refactor doesn't quietly reintroduce the billing flamethrower.

Files Reviewed (1 files)
  • internal/transformer/request.go - 1 issue

Reviewed by laguna-m.1-20260312:free ยท 1,745,447 tokens

kilo-code-bot[bot] avatar Jun 16 '26 05:06 kilo-code-bot[bot]