Feature Request: Systematic prefix-cache stability — learn from deepseek-reasonix's 99%+ cache hit architecture
Problem
CodeWhale already has prefix-cache awareness in its DNA — the "volatile-content-last invariant" in prompts.rs, byte-stable assistant message tests in client.rs, and a cache-hit-percent footer chip. But these are best-effort conventions, not a systematic invariant enforced at the architecture level.
deepseek-reasonix takes a different approach: it treats prefix-cache stability as a hard architectural invariant, not a guideline. The result is 85–99%+ cache hit rates in real sessions, translating to ~50× cost reduction on DeepSeek's pricing (¥0.02/1M cached vs ¥1/1M uncached).
What deepseek-reasonix does that CodeWhale could adopt
1. Byte-stable prompt construction as first-class invariant
Reasonix divides every request into three rigid zones that never shift:
- Prefix (pinned): system prompt, tool definitions, persistent memory — hashed at construction, never mutated mid-session
- Log (append-only): conversation history — only appended, never reordered or edited
- Scratch (ephemeral): per-turn metadata — wiped at every turn boundary
Any code path that would mutate the prefix or reorder the log is rejected at the framework level, not caught in review.
This contrasts with CodeWhale's current approach where the volatile-content boundary is documented in comments but not enforced — a stray edit_file to instructions or an unlucky /compact can silently bust the cache.
2. Cross-session cache persistence
Reasonix sessions can be left running — the prefix stays stable across sessions because the pinned zone structure is deterministic. Reopening a session reconstructs the identical prefix from configuration, so the first API call of a new session can still hit the cache if the session was running recently.
3. Cache-first cost visibility
Reasonix surfaces cache economics directly: every turn shows cache hit rate %, estimated cost with/without caching, and cumulative savings. CodeWhale's footer chip (red <40%, yellow <80%) is a good start but doesn't show the cost impact.
Existing CodeWhale foundation (good news — not starting from zero)
| Component | What exists |
|---|---|
prompts.rs:614 |
Volatile-content-last invariant (documented) |
client.rs:1529 |
Byte-stable assistant message test |
client.rs / ui.rs |
prompt_cache_hit_tokens / prompt_cache_miss_tokens tracked per turn |
| Footer | Cache hit % chip with color thresholds |
| System prompt | Layered most-static-first for DeepSeek KV cache |
What's missing
- No architectural enforcement — the volatile-content boundary is a comment, not a compile-time or runtime gate
- No prefix hashing — we can't detect when the prefix has been mutated and warn
- No cross-session prefix reuse — restarting CodeWhale invalidates the entire cache
- No cost-equivalent visibility — cache hit % is shown but without translating to actual ¥ saved
/compactbusts cache — the compaction relay intentionally rewrites the prefix, and there's no strategy to mitigate the cost
Suggested approach
Phase A — Harden existing invariants (low risk)
- Add a compile-time check or runtime assertion that system prompt construction is deterministic
- Warn (footer yellow) when
prompt_cache_hit_tokensdrops significantly between turns - Add a
/cache statscommand showing cumulative cache savings in ¥
Phase B — Prefix zone enforcement (medium)
- Formally split prompt construction into
PinnedPrefix/AppendLog/TurnScratchzones - Hash the pinned prefix at session start — warn if any subsequent turn sends a different prefix
- Reject code paths that mutate the log instead of appending
Phase C — Cross-session cache persistence (ambitious)
- When session restarts within DeepSeek's cache TTL (~5-15 min), reconstruct the identical pinned prefix
- This requires deterministic system prompt generation (no timestamp-dependent blocks in the pinned zone, etc.)
Related
- #1207
[auto] cost_saving— existing cost-conscious routing - #1676 Dual mode — cost savings via Flash/Pro routing
- #2026 Whale-size naming — user-facing model/cost understanding
Questions for discussion
- Would a formal
PinnedPrefix/AppendLog/TurnScratchsplit be acceptable, or is it too invasive for the prompt construction pipeline? - Cross-session cache persistence requires moving time-dependent blocks (like current date) into the scratch zone — acceptable trade-off?
- Should this be a standalone initiative or folded into #1676 as a companion cost-saving pillar?
Thanks @encyc. This is useful design input, and I agree with the central framing: cache stability should become an enforceable contract, not only a prompt-construction convention.
After checking current main, a few Phase A pieces are already present or further along than the issue suggests:
- prefix inspection already computes stable/static and reusable request prefix hashes;
/cacheexposes recent DeepSeek hit/miss telemetry;- the footer distinguishes provider cache hit rate from suspected prefix churn and can show savings context;
- compaction already has explicit guards because rewriting history/prefix is expensive for V4 cache reuse.
The missing piece is still the hard contract: a typed PinnedPrefix / AppendLog / TurnScratch boundary, with a stable hash and a warning or refusal path when code mutates the pinned zone unexpectedly.
I would not rush that into v0.8.48, because it changes prompt/session architecture. I am marking this as cache-maximalism / v0.9-scale design input. The low-risk 0.8.x slice I would accept sooner is narrower: expose prefix hash/drift in /cache stats and warn when the stable-prefix hash changes unexpectedly between turns.
@Hmbown with #2336 now merged (the 0.8.x /cache stats slice), thinking about the v0.9 hard-contract work we discussed. Here's a concrete breakdown based on the current codebase:
Current state (post-#2336)
prefix_cache.rs—PrefixStabilityManagerfingerprints system+tool prefix, detects drift, emits events. Observation only, no enforcement.chat.rs—split_system_layersalready partitions the system prompt into Static/History/Dynamic layers for inspection.prompts.rs— 2242-line prompt builder, dynamic content viaPromptSessionContext, no zone concept.turn_loop.rs— callscheck_and_updatebefore each request, drift triggers an event but doesn't block the request.- Compaction has explicit guards but doesn't distinguish zones.
Proposed 5-phase plan
Phase 1 — Typed Zone Structs
New types wrapping the three regions:
PinnedPrefix— system prompt text + tool catalog,freeze()→FrozenPrefixwith immutable hashAppendLog—Vec<Message>append-only wrapper (onlypush(), no insert/remove/get_mut)TurnScratch— per-turn metadata, cleared each turn boundary
Phase 2 — Zone Builder
Wire the types into the request construction pipeline:
build_pinned_prefix()frombuild_system_prompt()output + tool catalog- Replace current
MessageRequestassembly inturn_loop.rswithThreeZoneRequest
Phase 3 — Mutation Guard
Upgrade from observation to enforcement:
FrozenPrefix::verify()called before each request- Feature gate
prefix_cache_strict(default: warn-only for v0.9, migrate to refuse in v0.10) - Debug-build checksums on
AppendLogto detect history mutation
Phase 4 — Compaction Alignment
- Compaction declares which zones were affected
- Guarantee compaction only touches
AppendLog, neverPinnedPrefix
Phase 5 — TUI / /cache stats Upgrade
- Three-zone status display (green/yellow/red per zone)
/cache zonessubcommand
Questions:
- Does the 5-phase shape look right to you, or would you prefer a single landed PR?
- For Phase 3, should the strict mode be a
[prefix_cache] strict = trueconfig toggle or compile-time feature flag? - Any concern with the naming
PinnedPrefix/AppendLog/TurnScratch— or prefer something closer to Reasonix naming?
Happy to start with Phase 1 as a draft PR whenever you think the timing is right.