Skip to content

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

Closed

Description

@abogartz

Symptom

A session goes quiet for several seconds immediately after any memory write, then recovers. It looks like the whole conversation is being re-sent to the model. It is: the write busts the provider prompt cache, so the next request re-prefills the entire history uncached.

This directly contradicts the headline promise. README.md:102 ("the provider's prompt cache survives ordinary turns"), README.md:115 ("byte-identical between store changes so the prompt cache stays warm"), and the badge/panel copy memory never invalidated your cache prefix are all false for the turn after a write.

Receipts (dogfood host, 2026-10-28, 20 promoted writes)

The write itself is not the problem:

Measurement Value
gate_start → write_promoted latency 4 / 15 / 27 / 44 ms (min / median / p90 / max)
epoch_recompute events 34
… with reason=store_changed (= cache busts) 21
Gap between write_promoted and its epoch_recompute 0.1 – 1.5 s

The write gate is zero-model, so no hidden LLM call is involved. The stall is entirely provider-side re-prefill of a changed prefix.

Root cause — three independent volatile bytes in the cache-critical prefix

  1. The epoch versions on the store fingerprint, not on rendered output. refreshEpoch (src/index.ts:1156-1158) compares storeVersion(mems) (src/inject.ts:377-389, a sha256 over every active row) and rebuilds on any difference. A promoted row that renders nothing at all still forces a new epoch, still emits epoch_recompute reason=store_changed, and still increments stats.invalidations. Three of today's store_changed recomputes produced a block of identical length (849c) — the current version key cannot even tell us whether the bytes moved.

  2. The standing digest is volatile and is rebuilt on every request. systemDigest (src/inject.ts:295-299) pushes "{mems.length} durable memories; latest: {latest ts}", called with freshly loaded rows in every session.context (src/index.ts:1263-1267). So any promoted row changes the system prefix even when the relevance block is untouched. This is the bust that survives fixing (1).

  3. The frontier block shifts with every open row. cappedFrontier (src/inject.ts:237-270) is newest-first and budget-capped, so a new open thread reorders/re-trims it. Here the injected content genuinely changed — one bust is correct and intended.

Why it matters

Injection rides the front of the system prompt, so a single changed byte invalidates everything after it, including the whole conversation history. On a long session that is the most expensive thing the plugin can do to a turn, and it is paid once per write. The plugin already measures this honestly (stats.invalidations, the yellow badge dot) — the numbers are simply worse than the product claims.

Proposed fix

Make the memory prefix cache-stable by construction, and make the invalidation counter mean exactly what it says.

  1. Version the epoch on rendered bytes. Build the full memory prefix (digest + relevance block + frontier), then compare that string. Unchanged → keep the epoch, emit no epoch_recompute, do not increment invalidations. The store fingerprint stays useful as a cheap pre-check, but it must stop being the authority.
  2. Remove volatile facts from the prefix. Drop latest: <ts> and the row count from the digest line, or freeze them with the epoch so they are stable until the injected content actually changes. Both facts already live on surfaces where exactness is free and cache-neutral: the footer badge, /memory, memory_status, and the viewer.
  3. Keep the honest bust. A write that changes injected content must still produce exactly one invalidation on the next request. Freshness beats cache warmth; the goal is no gratuitous busts, not no busts.

Acceptance criteria

  • Unit test: write a row that is not selected for injection → next session.context produces byte-identical prefix, epoch_recompute not emitted, invalidations unchanged.
  • Unit test: write a row that IS injected → exactly one epoch_recompute reason=store_changed and invalidations + 1.
  • Unit test: two writes in the same turn → at most one bust on the following request (the prefix is compared once per request, not once per write).
  • bun run bench extended to report busts per policy, not just injected chars; the shipped policy must show 0 busts for store changes that do not alter the block.
  • README cost model + badge/panel copy updated to the real guarantee ("writes that change what you see cost one re-prefill; the rest cost nothing") — and the claim must be reproducible by the bench.
  • Existing gate green: bun test, bun run lint, bunx tsc --noEmit.

Out of scope

Batching/deferring writes, lowering store-read cost, and the separate evidence-line bloat issue (unbounded evidence_refs).

Activity

  1. added
    bug
    retrieval
    on Oct 28, 2026
  2. added a commit that references this issue on Oct 28, 2026
  3. abogartz-opencode-self-improvement commented on Oct 28, 2026

    @abogartz-opencode-self-improvement

    🎉 This issue has been resolved in version 0.14.16 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugreleasedretrieval

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions