Skip to content

fix(inject): refresh the frozen memory block when its rows change - #65

Merged
abogartz merged 2 commits into
abogartz:mainfrom
WhollyMindless:fix/frozen-epoch
Oct 24, 2026
Merged

abogartz merged 2 commits into
abogartz:mainfrom
WhollyMindless:fix/frozen-epoch

Conversation

@WhollyMindless

@WhollyMindless WhollyMindless commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

The frozen relevance block keeps sending a memory after it's been redacted or blocked. I found this while reviewing 0.8.0, and it's unchanged in 0.8.1.

The problem: the epoch fingerprint is ${activeCount}:${maxActiveTs}, and it's only re-checked in the prompt hook. That causes two failures:

  • Hand redaction. Editing a leaked secret out of the day file leaves both the row count and the newest ts unchanged. The old text keeps going out on every model request until some unrelated write changes the count or ts.
  • Blocking. Blocking a poisoned row does change the count, but nothing looks until the next user prompt. During a tool loop, every request still carries the blocked instruction.

On 0.8.0, with the repro:

  • after a hand redaction and a new prompt, the block still carries the secret;
  • after a block, a request in the same turn still carries the poisoned row.

With this PR, neither does.

Change:

  • storeVersion hashes each visible row's ts and its rendered formatMemory line, so edits, redactions, block/unblock, forget and supersede all change it.
  • The prompt hook records the latest prompt per session and refreshes the epoch. The request-time context hook re-checks it before every model request, re-scoring against that prompt. A removal therefore takes effect on the very next request. (Thanks for checking the prompt-before-context order against a real 2.0.26 turn.)
  • If nothing visible changed, the block is byte-identical, so the prompt cache survives exactly as before.
  • Cost: this adds one cache miss per out-of-band edit that keeps the row's ts, such as a hand redaction. The digest's latest: is the max ts, so it doesn't change in that case, which is exactly why the stale block survived before. Blocks and mid-loop removals also now invalidate on the next request rather than the next prompt, so the invalidation counter ticks slightly more often. That's the intended trade, correctness over one cache miss, but it isn't free.
  • A single loaded row set per request is shared by the fingerprint, relevance scoring and the digest, so there's no extra store read.

Tests (test/epoch.test.ts):

  • A hand redaction without a new prompt drops the old text on the next request.
  • Block and unblock take effect on the next request.
  • An unchanged store gives a byte-identical block.
  • storeVersion changes on a same-ts content edit.

With the source change reverted, the new tests fail.

Checks (Linux, Bun 1.4.4, on 0.8.1): bun test ./test/ 286 pass, 4 skip, 0 fail. bun run test:e2e against opencode 2.0.26: 9 pass. tsc --noEmit is clean, and biome lint exits 0 with warnings unchanged from main. It uses node:crypto SHA-256 and nothing platform-specific.

Follow-up: the options object for systemDigest is in #66, and the bounded per-session maps (#75) are in #77.

🤖 Generated with Claude Code

The frozen injection block was keyed on (active row count, newest ts). A
hand redaction, a viewer edit or a blocklist entry changes neither, so the
old text (a leaked secret or a blocked instruction) kept going out on
every model request until the next prompt that also changed the count.

- storeVersion hashes each visible row's ts and rendered form, so edits,
  redactions, block/unblock, forget and supersede all change it.
- The epoch is re-checked in the request-time context hook, not only at
  prompt time, so a removal takes effect on the very next request. The
  latest prompt is kept per session for re-scoring.
- If nothing visible changed, the block stays byte-identical. The digest
  already changes on every write, so this adds no extra cache misses.
- One loaded row set per request is shared by the fingerprint, relevance
  scoring and the digest.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@abogartz abogartz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve — the staleness is real and reproduced; the restructure is safe against the one host-ordering assumption it depends on. Three wording/robustness items below; none block merge for me.

What I re-verified on 0.8.1 (main @ 4643ec4)

  • Hand redaction: seeded a row containing a key, prompted, saw the key injected; rewrote the day file in place (same ts, same row count) → the old key still rode the next request, and still rode it after a fresh prompt. Fixed on your branch.
  • Mid-loop block: row injected → addBlock(ts) → the poisoned row still rode the next request with no new prompt. Fixed on your branch.
  • The host-ordering assumption your restructure depends on (session.prompt must run before session.context, otherwise a store change during a tool loop gets re-scored against the previous prompt): I probed a real opencode v2.0.26 turn with a scripted tool loop. Observed order was setup → session.prompt → session.context → tool.before → session.context, i.e. prompt-first and one context per model request. latestPrompt.set() then refreshEpoch() in the prompt hook is therefore correct, and the loop re-scores against the current prompt.
  • Cache stability: three consecutive requests with an unchanged store produced byte-identical injected parts (same sha256). Cost: 200 storeVersion() passes on a 60-row store = 26 ms on main vs 32 ms here, ~0.03 ms per request. Not a concern.
  • 290 pass / 4 skip / 0 fail at head; 3 of your tests fail against main's source; tsc clean; biome warnings identical to main.

Please fix (wording / bookkeeping)

  1. "The digest line … already changes on every write and sits before this block, so this adds no new cache misses" is not true for the case you are fixing. The digest's latest: is the max ts, so a same-ts hand-edit leaves the digest unchanged — which is exactly why the stale block survived. Your new fingerprint changes there, so this PR does add one cache bust per out-of-band edit. That is the correct trade (correctness over one bust), but please say it plainly instead of claiming zero cost.
  2. latestPrompt and epoch are unbounded per session. latestPrompt holds the full prompt text of every session a long-lived server has ever seen, and nothing evicts on session end/archive (compaction only). src/repokey.ts already has SESSION_CAP = 64 for exactly this shape of leak — cap these the same way or evict together.
  3. Positional-parameter collision with #66. You add systemDigest(input, output, rows?), #66 adds systemDigest(input, output, budget). A naive merge picks one side and #66's own test then fails tsc with TS2345: Argument of type 'number' is not assignable to parameter of type 'Memory[]' (I merged all six PRs to check; a working resolution is systemDigest(input, output, rows?, budget?) + mems, budget at the call site). Please convert both to one options object — systemDigest(input, output, { rows, budget }) — so this class of merge error is impossible rather than merely caught by CI.

Note

refreshEpoch keeps the single-load-per-request property you claim (the same rows feed storeVersion, relevantKnowledge and systemDigest). flushStatus still does its own loadAll on a recompute, unchanged from main.

@abogartz
abogartz merged commit 30eb09b into abogartz:main Oct 24, 2026
3 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.8.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

@WhollyMindless

Copy link
Copy Markdown
Contributor Author

Thanks for merging. Your three points:

  1. I corrected the cost wording in the description: this adds one cache miss per same-ts out-of-band edit, because the digest's latest: doesn't move then.
  2. The bounded maps (Per-session caches (epoch, latestPrompt, sessionStats) grow unbounded in a long-lived server #75) are in fix(inject): bound per-session state in a long-lived server #77: one 64-session LRU evicts epoch, latestPrompt, sessionStats and measuredTurns together. An evicted session gets no block until its next prompt, then a fresh first epoch.
  3. The options object is in fix(inject): hold the frontier block and contextual pulls to injectBudget #66, which converts systemDigest's positional rows into { rows, frontierBudget }.

abogartz pushed a commit that referenced this pull request Oct 24, 2026
One 64-session LRU (same cap as repokey.ts) evicts epoch, latestPrompt,
sessionStats and measuredTurns together. It is touched in the prompt and
context hooks and in message.updated. An evicted session gets no relevance
block until its next prompt, then a fresh "first" epoch, so it never sees
stale state. Follow-up to #65, where latestPrompt made the leak worse.

Closes #75.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
abogartz-opencode-self-improvement Bot pushed a commit that referenced this pull request Oct 24, 2026
## [0.8.6](v0.8.5...v0.8.6) (2026-10-24)

### Bug Fixes

* **inject:** bound per-session state in a long-lived server ([ee67acd](ee67acd)), closes [#65](#65)
@WhollyMindless
WhollyMindless deleted the fix/frozen-epoch branch October 26, 2026 00:27
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.

2 participants