Skip to content

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

Closed

Description

@abogartz

Follow-up on #162 (shipped in 0.14.16). #162 made the epoch freeze the whole memory section and version it on rendered bytes, so a write the session is never shown now costs nothing. This closes the remaining half: a session still pays a full re-prefill for memories it wrote itself.

The rule

A session never pays for bytes it is not currently riding.
A pure addition this session authored costs zero busts. Anything that removes or rewrites a row that is riding busts. Anything this session did not author busts.

Why: the cost is almost entirely self-inflicted

Lifetime dogfood evidence (memory/evidence/*.logfmt, 35 sessions with busts):

Measurement Value
epoch_recompute reason=store_changed (lifetime) 145
write_promoted (lifetime) 245
Busts attributable to the session's own writes 134 = 92%

The last row is a min-bound (a session cannot bust more often than it wrote, and its own writes are the only store changes it provably made), but per-session it tracks 1:1 — busts=11/own=15, 10/11, 10/10, 9/9. So the dominant cache cost of this plugin is a session being billed to re-read facts it just produced.

Why "already in the conversation" is mostly right — and where it isn't

A row this session promoted is in its own transcript. Re-showing it is a cost with no information gain. That argument is strong, and it is the whole rule above.

It does not extend to three cases, which is why the rule is split by what happened to a riding row rather than by "was it me":

  1. A correction is not "already known." If a riding row is superseded (memory_update, memory_forget, consolidation, decay), the model knows the old fact. Freezing makes the store silently vouch for something it has already retired.
  2. Not everything in the section is yours. The frontier is mostly other sessions' open threads — that is the feature. Plus concurrent sessions/worktrees, viewer edits, memory_import, memory_undo, memory_merge_review. None of it is in this context, and a resident opencode serve lives for days.
  3. Additions are not always additive. A new open thread can push an older one out of the budget-capped frontier, and any new row shifts document frequencies, which can reorder or evict a riding row in the relevance block. Losing information the session was riding is a change, not an addition.

On the blocklist carve-out — the objection I raised, and why it costs nothing

I initially argued that freezing must exempt removals so a human can pull a poisoned memory mid-session. Two findings retire that as a tradeoff:

  • It never happens. On the dogfood host blocklist.logfmt does not exist — zero entries, lifetime. deletions.logfmt has 4. The only way to block a memory is POST /block in the viewer (src/viewer/server.ts:261); there is no plugin tool for it, so it requires deliberately opening the viewer and clicking.
  • And the rule does not ask us to choose. Blocking, redacting, forgetting or superseding a row removes it from what is rendering. Removals bust unconditionally under the rule above, authorship included. So the security path stays exactly as responsive as it is today while the 92% case goes free. There is no user-experience-versus-safety decision to make here.

(Worth recording honestly: even today's immediate removal is weaker protection than the framing implies. If a poisoned row rode the section, the model already read it and its output is already in the transcript — dropping it from the prefix stops the re-assertion of the instruction on every subsequent request, it does not un-ring the bell. The next session starts a fresh epoch and is protected regardless.)

Design

Authorship predicate. Rows persist ts,type,scope,content,issue,tags,kw,status,repo,repoPath — no author. Two options:

  • (chosen) An in-process sid → Set<rowTs>, filled where a write is promoted, living in src/state.ts under the same SessionLru so it is bounded and evicted with the rest of the session state. The window that matters is exactly the window this map is alive. A server restart forgets authorship, which degrades to today's behavior (one bust), never to a wrong freeze.
  • (rejected) A persisted by= field: it would need a store-schema change, migration, and privacy review (it records which session wrote what) to fix only the restart edge case.

Freeze test. At adoption, store with the epoch the set of row ids that actually rendered (sources) — frontier rows plus budget-surviving relevance rows; the digest and the convergence line are row-independent. On a store change, render the candidate section and freeze (adopt the new fingerprint, ride the old bytes, emit nothing) iff both:

  1. cached.sources − built.sources is empty — nothing that was riding stopped riding (this is what makes removals always bust), and
  2. the section re-rendered excluding rows this session authored is byte-identical to the same key stored with the frozen epoch — i.e. every byte that moved is attributable to this session's own additions.

(2) is checked by rendering twice on the store-changed path only; there is no model call in it, so the extra render is microseconds against a multi-second re-prefill.

Acceptance criteria

  • memory_remember of a row that IS selected for injection → next request rides the OLD bytes, no epoch_recompute, invalidations unchanged. (Today: busts.)
  • memory_remember of an open thread that evicts a riding frontier row → busts (loss of riding information).
  • memory_update / memory_forget / blocklist / hand-redaction of a riding row → busts on the next request, same as today. Existing tests in test/epoch.test.ts stay green.
  • A row promoted by a different session (and a viewer edit, memory_import, memory_undo) → busts.
  • After a compaction the epoch is dropped, so the session sees everything it missed; a new session always renders fresh. Pinned by existing tests.
  • /memory CONTEXT and memory_status state the guarantee accurately: additions by this session are free; corrections and other writers land on the next request.
  • Bench gains an arm showing the self-authored-addition case at 0 busts, measured on the full section.
  • Gate green: bun run gate (tsc, biome, unit, e2e).

Out of scope

Persisting authorship. Tail-adjacent injection (injecting relevance after the latest turn instead of in the system prefix) — that is the only way to get per-turn relevance freshness and zero busts, and it trades away the invisibility the current design chose deliberately, so it is a separate product decision.

Activity

  1. added
    enhancement
    retrieval
    on Oct 28, 2026
  2. abogartz commented on Oct 28, 2026

    @abogartz
    OwnerAuthor

    Implemented on fix/riding-bytes. Three things changed between the pre-registered design and the code, recorded here rather than silently.

    1. The freeze test is simpler than the design's two conditions

    The design said: check cached.sources − built.sources is empty (nothing riding disappeared) and re-render excluding self-authored rows. The second check subsumes the first, so only it is implemented:

    Freeze iff re-rendering the section from the current rows minus every row this session authored since the frozen bytes were produced reproduces the frozen bytes exactly.

    A removal of a riding row cannot be reproduced by that render (the row is gone from the store, so subtracting my additions does not bring it back), so removals, redactions, blocklist, forget, supersession and decay all still bust without needing a separate sources set. That also removed the need to plumb a row-id out-param through systemDigest.

    2. Self-authored eviction does NOT bust — and that is better than the design said

    The design listed "a new open thread pushes an older riding row out of the capped frontier" as a change that must bust. It does not, under this test — and on reflection it should not: freezing never removes anything from what the session is riding. The session keeps riding the older row; the eviction only materialises at the next real adoption. Freezing cannot lose information for the current session, it can only be stale, so the eviction case costs a bust for no benefit.

    3. A hole the design did not have: a topic change between the freeze and the write

    Found while making the bench show the win. The epoch deliberately does not re-score when only the prompt changes (test/epoch.test.ts pins that as intended). So if the session pivots to a new subject and then writes a memory, the counterfactual rendered against the current prompt 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. That is the cost/benefit inversion from the issue text, reappearing through the back door.

    Fix: the epoch stores the prompt its bytes were scored against, and the counterfactual re-scores with that prompt. Pinned by a topic change plus a self-authored write still costs nothing, which is red without the prompt pinning.

    Authorship: in-process, as chosen

    noteAuthored(sid, ts) fires at the single place a row is created on behalf of a named session (runWriteGate, after appendMemory resolves the ts). The viewer's own writes are deliberately not attributed, so a human edit always looks foreign and always lands. Registry lives in src/state.ts under the same SESSION_CAP LRU as the other session ledgers; resetState() clears it.

    Measured

    Policy Recomputes Cache busts Injected chars (20 turns)
    A — naive, recompute per prompt 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

    One contract this changed outside the plugin: the use ledger

    e2e/ledger.test.ts asserted that a session writing two facts earned src=inject 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 before both rows looked used. That is the correct reading — a row nobody ever retrieved is dead weight — but it means neverRetrieved counts will rise after this ships. Anyone reading that number as a quality signal should know why.

    Boundary to keep 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 rather than a theoretical one.

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

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

    @abogartz-opencode-self-improvement

    🎉 This issue has been resolved in version 0.14.17 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

  5. abogartz commented on Oct 28, 2026

    @abogartz
    OwnerAuthor

    Live measurement: a memory write, before and after

    Ran the experiment on the dogfood host while the versions happened to change underneath it, which produced a clean A/B for free.

    The write that busted — 0.14.16 (#162 shipped, #165 not yet)

    23:19:31.678Z  write_promoted   scope=bench/cache-probe
    23:19:31.831Z  epoch_recompute  reason=store_changed      ← 153 ms later
    

    Same shape as the three earlier writes in that session (21:59:20.177 → .273, 22:22:54.399 → .495, 23:08:01.505 → .576): the promoted write is followed within ~70–155 ms by a bust, and every bust costs the next request a full re-prefill of the conversation.

    The write that did not — 0.14.17 (#165)

    23:23:00.804Z  write_promoted   scope=bench/cache-probe
                                     (no epoch_recompute, then or on the next requests)
    

    Session counters across that write: invalidations 0, sectionChars held at 3108, cache-served share unchanged at 95.7%.

    Ruling out the boring explanation

    A write that renders nothing already cost nothing under #162, so a non-event proves nothing unless the row would have moved bytes. Checked by rendering this session's memory section from a copy of the live store, with and without the probe rows, using the plugin's own systemDigest:

    probe rows found:               2  (,open)
    section WITH my probe rows:     3392 chars
    section WITHOUT them:           3107 chars
    probe text renders?             true
    bytes differ (=> pre-#165 bust): true
    

    It was written as an open thread deliberately, so it enters the frontier block regardless of prompt wording — 285 chars of genuinely new prefix. Under 0.14.16 that is a bust. Under 0.14.17 the session kept riding its old 3108 chars.

    Cleanup: both probe rows removed; audited in deletions.logfmt, snapshots retained for memory_undo. Removing them caused no bust either — they had never ridden the prefix, so the subtraction still reproduced the frozen bytes exactly. The "removals always bust" guardrail applies to rows that are riding and is covered by test/epoch.test.ts, not by this live run.

    What happened, in order

    #162 — epoch versions on rendered bytes, volatile digest facts removed merged → 0.14.16
    #165 — a session never pays for bytes it is not riding merged → 0.14.17
    Lifetime busts attributable to a session's own writes, measured beforehand 134 of 145 (92%)
    Same class of write, after 0 busts

    Bench, full memory section, deterministic: naive 19 busts → pre-#162 3 → pre-#165 3 → shipped 0, with 12.3% fewer injected characters than naive.

    Two things worth keeping in sight

    Merged is not live. 0.14.17 hit npm at 23:13:53Z. The host restarted at 23:15 and still ran 0.14.16 — the @latest plugin cache resolved to the install directory from 16:14, and nothing re-fetched. It only picked 0.14.17 up at a second restart at ~23:20. So "we shipped it" and "your sessions feel it" are separated by a restart, and sometimes two. Anyone validating a plugin fix against their own host should confirm the resolved version rather than assume it.

    The metric that moved on purpose. A writing session no longer earns src=inject use-ledger credit for rows it is never shown, so neverRetrieved in /memory and memory_status rises after 0.14.17. That is the ledger telling the truth — those rows genuinely were not retrieved — but it will look like a quality regression to anyone who doesn't know the date.

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

    enhancementreleasedretrieval

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions