Repository navigation
fix: a memory write must not bust the prompt cache unless bytes move (#162) - #164
Conversation
…162) Saving a memory cost the next request a full re-prefill of the conversation. Two independent defects put volatile bytes at the front of the system prompt: 1. refreshEpoch versioned on the STORE FINGERPRINT, so any promoted row — including one the session is never shown — rebuilt the epoch, emitted epoch_recompute reason=store_changed and counted an invalidation. 2. systemDigest pushed "N durable memories; latest: <ts>", rebuilt from fresh rows on every request, so the prefix changed after every write even when the relevance block was byte-identical. The epoch now freezes the WHOLE memory section (digest + frontier + relevance) and the authority for "did the prefix change?" is the rendered bytes. A write that changes nothing visible adopts the new fingerprint, rides the same bytes, emits nothing and busts nothing; a write that does change what you see still costs exactly one re-prefill, and the following turn re-warms. The digest carries no count and no timestamp — both moved to surfaces where they are exact and never ride a prompt (badge, /memory, memory_status, viewer). Because the frozen section is now what rides, the panel reports it: CONTEXT gains "memory section Nc ... riding the prompt", since the budgeted block alone understated what the request actually carries. The bench measured only the relevance block, which is precisely how the old claim survived review. It now runs three arms over the FULL section and reports cache busts (turns whose riding bytes differ from the previous turn), including a faithful reproduction of the pre-fix digest: A naive 20 recomputes / 19 busts, B pre-#162 4 / 3, C shipped 4 / 3 — equal on this workload because all three synthetic writes happen to be visible; the invisible-write case is pinned by test/epoch.test.ts, which fails against B. Red-green: "a write that changes nothing rendered costs ZERO cache busts" and "the standing digest is byte-identical across stores of different size and age" both fail on main and pass here. Gate: 615 pass / 0 fail unit, 11 pass e2e, tsc clean, biome exit 0, bench byte-identical across runs.
|
🎉 This PR is included in version 0.14.16 🎉 The release is available on: Your semantic-release bot 📦🚀 |
|
I reproduced two qualifications to the “write the session is not shown → 0 busts” row on the PR head (
The byte comparison counted a real change in both cases. The distinction is what caused it: the written row alone doesn't determine whether the prefix moves. If adopting fresh convergence and ranking on the next store change is intended, a qualification in the guarantee and a regression test for that timing would capture it. I measured injected bytes and local recompute events only. Live provider cache misses, latency and cost remain unmeasured. |
What
Saving a memory cost the next request a full re-prefill of the conversation. Fixes #162.
The write was never the slow part (
gate_start → write_promotedis 4–44 ms, zero model calls). The stall is the request after it: the memory section sits at the front of the system prompt, so one changed byte there invalidates the provider's cached prefix and the whole history is re-billed as fresh input.Two independent defects put volatile bytes on that prefix:
refreshEpochversioned on the store fingerprint, so any promoted row — including one the session is never shown — rebuilt the epoch, emittedepoch_recompute reason=store_changed, and counted an invalidation.systemDigestpushed"N durable memories; latest: <ts>", rebuilt from fresh rows on every request, so the prefix changed after every write even when the relevance block was byte-identical.Change
The epoch now freezes the whole memory section — standing digest + open-thread frontier + convergence line + relevance block — and the authority for "did the cache-critical prefix change?" is the rendered bytes, never the fingerprint.
storeVersionstays as a cheap pre-check so ordinary turns skip re-scoring and the use-ledger write.noteHits("inject")moved after adoption: a discarded rebuild must not re-credit rows that already rode the frozen epoch.Volatile facts are banned from the cached prefix. The digest no longer carries a row count or a latest timestamp; those numbers live on surfaces where they are exact and never ride a prompt — the footer badge,
/memory,memory_status, the viewer.Honesty fixes in the same blast radius
CONTEXTgainsmemory section Nc ≈ M tok riding the prompt (digest + frontier + the block above), becauseinjectBudgetcaps only the relevance block.StatusPayload.sectionCharsis optional, so pre-existing status files print no phantom line.cacheLinesno longer claims "each memory write between turns costs exactly one".Why the old claim survived review
bench/inject-cost.tsmeasured only the relevance block — byte-identical between store changes, so the claim looked proven — while the volatile digest that actually rode the wire was never measured. The bench now runs three arms over the full section and reports cache busts (a turn whose riding bytes differ from the previous turn), including a faithful reproduction of the pre-fix digest:B and C tie on this workload on purpose: all three synthetic writes happen to be visible, because adding any row shifts document frequencies and can reorder the top-5. The invisible-write case — the common one — is pinned by the unit tests instead, and the bench says so rather than implying a win it did not measure.
Test evidence
Red-green, verified by reverting
src/only:a write that changes nothing rendered costs ZERO cache busts— fails on mainthe standing digest is byte-identical across stores of different size and age— fails on maina write that IS injected busts exactly once, then the new bytes freeze,two writes before the next request produce ONE bust, not two, and the panel line intest/status.test.ts.Gate: 615 unit pass / 0 fail, 11 e2e pass / 0 fail,
tsc --noEmitclean,biome lintexit 0, bench byte-identical across runs.Follow-up worth a decision
A session pays a bust for memories it wrote itself — facts it already has in its own transcript. Freezing the section for the session's lifetime and refreshing at compaction / new session would make the guarantee absolute. That needs carve-outs for removals (a blocklisted or redacted row must stop riding even mid-session) and for rows this session did not author. Tracking that separately.
Closes #162.