Skip to content

fix: a memory write must not bust the prompt cache unless bytes move (#162) - #164

Merged
abogartz merged 1 commit into
mainfrom
fix/162-cache-stable-prefix
Oct 28, 2026
Merged

abogartz merged 1 commit into
mainfrom
fix/162-cache-stable-prefix

Conversation

@abogartz

Copy link
Copy Markdown
Owner

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_promoted is 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:

  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.

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.

Situation Before After
Write the session is not shown bust (1 re-prefill) 0 busts — adopts the new fingerprint, rides the same bytes, emits nothing
Write that changes what is shown bust exactly 1 bust, then re-warm (freshness still wins)
Two writes before the next request 1 bust 1 bust (compared once per request)
Redaction / blocklist of a riding row refreshed next request unchanged — still refreshed

storeVersion stays 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

  • The panel understated what rides: CONTEXT gains memory section Nc ≈ M tok riding the prompt (digest + frontier + the block above), because injectBudget caps only the relevance block. StatusPayload.sectionChars is optional, so pre-existing status files print no phantom line.
  • cacheLines no longer claims "each memory write between turns costs exactly one".

Why the old claim survived review

bench/inject-cost.ts measured 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:

Policy Recomputes Cache busts Injected chars (20 turns)
A — recompute per prompt, uncapped (naive) 20 19 60996
B — block frozen on store fingerprint, digest rebuilt per turn (pre-#162) 4 3 55478
C — section frozen on rendered bytes (shipped here) 4 3 55258

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 main
  • the standing digest is byte-identical across stores of different size and age — fails on main
  • plus a 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 in test/status.test.ts.

Gate: 615 unit pass / 0 fail, 11 e2e pass / 0 fail, tsc --noEmit clean, biome lint exit 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.

…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.
@abogartz
abogartz merged commit 939e1b2 into main Oct 28, 2026
4 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.14.16 🎉

The release is available on:

Your semantic-release bot 📦🚀

@abogartz
abogartz deleted the fix/162-cache-stable-prefix branch November 19, 2026 05:52
@WhollyMindless

Copy link
Copy Markdown
Contributor

I reproduced two qualifications to the “write the session is not shown → 0 busts” row on the PR head (2072297) and on main (c34abe1). In both cases the newly written row stayed out of the prompt. The write caused a rebuild that picked up an earlier change elsewhere.

  • Convergence count. Start session A with one tracked session, then submit a prompt in session B. A’s next context is byte-identical. After an unrelated write, A records one store_changed recompute. On main I compared the entire injected text: the only difference was “1 sessions tracked” becoming “2 sessions tracked”. This also happened with A’s own promoted write. With no B prompt, the same self-write preserved every byte.
  • Use-based ranking. Give six equally matching rows five slots. Record a recall hit on the omitted row, refresh the test ledger cache to simulate its TTL, then write an unrelated row. The previously omitted row replaces one of the five at the next context. In the main control, refreshing the cache and making the same write without the hit preserved the full prefix.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

P0: every memory write busts the prompt cache — the memory prefix carries bytes the model never needed

2 participants