Skip to content

perf: a session never pays a cache bust for bytes it is not riding (#165) - #166

Merged
abogartz merged 1 commit into
mainfrom
fix/riding-bytes
Oct 28, 2026
Merged

abogartz merged 1 commit into
mainfrom
fix/riding-bytes

Conversation

@abogartz

Copy link
Copy Markdown
Owner

What

Follow-up on #162 (shipped in 0.14.16). #162 stopped a write the session is never shown from busting the prompt cache. This stops the other half, which is the bulk of the cost: a session was billed a full re-prefill to be shown memories it wrote itself.

Lifetime dogfood evidence behind this: 145 store_changed busts across 35 sessions, 134 of them (92%) attributable to the session's own writes — facts the model produced, already sitting in its own transcript.

Implements #165.

Change

The epoch now also carries the rows this session had authored when the riding bytes were rendered, and the prompt those bytes were scored against. On a store change it re-renders the section with every row authored since then taken back out:

Freeze iff that reproduction equals the frozen bytes — i.e. the entire difference is attributable to the session's own additions.

Situation Before After
memory_remember of a row that would be selected 1 bust 0 busts
Several self-authored writes in a row 1 bust each 0 busts, all of them
Topic change, then a self-authored write 1 bust 0 busts
Another session writes a row busts busts
Riding row corrected / forgotten / superseded / decayed busts busts
Riding row blocklisted or hand-redacted (even one you authored) busts busts
Viewer edit, memory_import, memory_undo busts busts

Authorship is in-process (src/state.ts, under the shared SESSION_CAP LRU), recorded at the single place a row is created on behalf of a named session — runWriteGate, after appendMemory returns the ts. The viewer's own writes are deliberately not attributed, so a human edit always looks foreign and always lands. A lost entry (server restart) degrades to today's behavior — one bust — never to a wrong freeze.

Two corrections to the design in the issue

Recorded rather than absorbed silently:

  1. Self-authored eviction does not need to bust. The issue listed "a new open thread pushes an older riding row out of the capped frontier" as a change that must bust. It does not, and on inspection it should not: freezing never removes anything from what a session rides, so it cannot lose information for that session — the eviction only materialises at the next real adoption. The old condition would have cost a bust for nothing.
  2. A hole the issue did not anticipate: a topic change between the freeze and the write. The epoch deliberately does not re-score when only the prompt changes (an existing test pins that as intended). Rendering the counterfactual against the current prompt therefore produced a different block than the frozen bytes, and the session paid a bust for a refresh the plugin refuses to perform on a prompt alone — the exact cost/benefit inversion P0: every memory write busts the prompt cache — the memory prefix carries bytes the model never needed #162 described, reappearing through the back door. The epoch now stores the prompt its bytes were scored against and the counterfactual uses it. Pinned by a topic change plus a self-authored write still costs nothing, which is red without the prompt pinning.

Measured

Full memory section (digest + frontier + block), deterministic, byte-identical across runs:

Policy Recomputes Cache busts Injected chars (20 turns)
A — recompute per prompt, uncapped (naive) 20 19 60996
B — pre-#162 (digest rebuilt per turn) 4 3 55478
C — pre-#165 (section frozen on rendered bytes) 4 3 55258
D — shipped (riding bytes) 1 0 53500

The three synthetic writes are made by the measured session, which is how memory actually gets written. All three change what that session would be shown, so B and C each pay for them; D pays nothing.

One contract this changes outside the plugin

e2e/ledger.test.ts asserted that a session writing two facts earned src=inject ledger credit for the block that then carried them. It no longer does — because it is no longer shown them. The test now asserts that explicitly (injected.length === 1: the next session is the first to be shown them), and the store report consequently shows honest dead weight — 1 retrieved, 1 never retrieved (50%) — where both rows previously looked used.

This means neverRetrieved counts rise after it ships. That is the correct reading (a row nobody retrieved is dead weight), but anyone using that number as a quality signal should know why it moved.

Boundary worth keeping in view

A very long-lived session that writes constantly will not see its own rows injected until a foreign change or a compaction drops the epoch. That is the requested trade and compaction is the existing reset, but it is a real staleness window, not a theoretical one.

Test evidence

Five new cases in test/epoch.test.ts. Exactly one is red on main — a memory this session wrote itself costs ZERO cache busts, the behavior change. The other four are deliberately green before and after, pinning that foreign writes, corrections of riding rows, hand-redactions and topic pivots behave as required:

  • a memory this session wrote itself costs ZERO cache busts — red on main
  • a topic change plus a self-authored write still costs nothing
  • a memory ANOTHER session wrote still lands on the next request
  • correcting a row the session is riding still busts, even its own
  • a hand-redaction of a row this session authored still busts

Plus the updated e2e/ledger.test.ts, run against a real opencode run --standalone host.

Gate: 620 unit pass / 12 skip / 0 fail, 11 e2e pass / 0 fail, tsc --noEmit clean, biome lint exit 0, bench byte-identical across runs.

Closes #165.

)

#162 stopped a write the session is never SHOWN from busting. This stops the
other half, which is the bulk of the cost: a session was billed a full
re-prefill to be shown memories it wrote itself. Lifetime dogfood evidence:
145 store_changed busts across 35 sessions, 134 of them (92%) attributable to
the session's own writes — a fact the model produced, already in its transcript.

The epoch now also stores the rows this session had authored when the riding
bytes were rendered, plus the prompt those bytes were scored against. On a store
change it re-renders the section with every row authored SINCE then taken back
out; if that reproduces the frozen bytes exactly, the entire difference is the
session's own additions and the bytes keep riding, with no epoch_recompute and
no invalidation. Anything else survives the subtraction and still busts: a
riding row removed or rewritten (blocklist, hand-redaction, forget, supersession,
decay), another session's row, a viewer edit, an import, a moved convergence
line. Authorship is in-process (src/state.ts, under the shared SESSION_CAP LRU),
filled at the one place a row is created on behalf of a named session; the
viewer's writes are deliberately unattributed, so a human edit always lands. A
lost entry degrades to today's behavior, never to a wrong freeze.

Two things the design in #165 got wrong and this corrects:

  - Eviction by a self-authored row does not need to bust. Freezing never
    removes anything from what a session rides, so it cannot lose information;
    the eviction only materialises at the next real adoption.
  - The counterfactual must re-score with the prompt the frozen bytes came from.
    Scoring it with the current prompt made a topic change followed by a write
    look like a content change, billing the session for a refresh the epoch
    deliberately refuses to perform on a prompt alone.

Bench (full section, deterministic): A naive 20 recomputes / 19 busts, B pre-#162
4 / 3, C pre-#165 4 / 3, D shipped 1 / 0 and 12.3% fewer chars than naive.

Contract change outside the plugin: a writing session no longer earns
src=inject ledger credit for rows it is no longer shown, so e2e/ledger.test.ts
now asserts that (and the store report shows honest dead weight, 1 retrieved /
1 never, where both rows previously looked used). neverRetrieved counts rise
after this; the number is now telling the truth about what was retrieved.

Tests: 5 new cases in test/epoch.test.ts (one red on main — the self-authored
addition; the rest pin that foreign writes, corrections of riding rows and
hand-redactions still land), and the ledger e2e updated to the new contract.
Gate: 620 pass / 12 skip / 0 fail unit, 11 pass e2e, tsc clean, biome exit 0.
@abogartz
abogartz merged commit 511aa22 into main Oct 28, 2026
4 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.14.17 🎉

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A session pays a re-prefill for memories it wrote itself — 92% of lifetime cache busts (follow-up on #162)

1 participant