Repository navigation
perf(store): batch supersession - one pass per file for decay and gates (#61) - #102
Conversation
decay and the gate's drift/canonical loops called markSuperseded per row: N full lock/read/rewrite/snapshot/publish cycles and N undo points for one logical operation. markManySuperseded takes the whole set and does ONE lock/read/snapshot/publish PER FILE touched. Idempotence and the already-marked skip move inside the lock (the batch re-reads, so a row superseded out-of-band since the caller's scan is skipped, never clobbered). markSuperseded becomes a one-entry wrapper with its exact boolean contract. Refs abogartz#61.
abogartz
left a comment
There was a problem hiding this comment.
Adversarial review (macOS, Bun 1.4.3). The batch is correct; one merge-order consequence needs a line of code before it lands.
What I verified myself
Your red-check reproduces exactly. I replaced markManySuperseded with a faithful per-row simulation (stampRow per entry, break on first hit — the pre-fix shape) and ran the suite:
error: expect(received).toBe(expected)
Expected: 2
Received: 6
(fail) … rows across two day files retire with ONE snapshot per touched file, not per row
5 pass / 1 fail
Restored: 6 pass / 0 fail. Single failure, right reason, matching the body.
Semantics, read and checked against stampRow:
- Idempotence moved inside the lock:
touched === 0returns beforesnapshotBefore/writeAtomic, so a repeat run writes nothing and mints no undo point. ✓ - Conservation: the per-line skip is
typeof parsed.superseded_by === "string", so an out-of-band marker is never rewritten. ✓ - Duplicate
tsin one batch: first entry wins, byMapinsertion order — genuinely mirrors row-by-row. ✓ - Lock shape: one
withStoreLockper file, sequential, released before the next;runWriteGateis not itself inside a store section, so there is no nesting/deadlock surface added. ✓ markSupersededas a(await markManySuperseded([…])) > 0wrapper preserves the boolean contract for the viewer andmemory_merge_reviewcallers. ✓
Gates: lint clean, tsc --noEmit clean, bun run test → 446 pass / 5 skip / 0 fail. CI lint/test/smoke green. Also merged all four open PRs (89 → 101 → 102 → 103) in one worktree: no conflicts, 480 pass / 0 fail.
Ask — the identity anchor that #89 just made load-bearing
src/memory.ts publishes the batch with writeAtomic(file, lines.join("\n") + "\n") — no third argument. Every other whole-file store write now takes the destination's fileIdentity from before its read (stampRow, prune, deleteMemory, removeBlock, restoreSnapshot), because #89's Windows handover retry only proceeds while that anchor still matches.
The path you replaced went through stampRow, which #89 anchors. So as merged, decay and both gate neutralization loops become the only whole-file publishes in the store with no staleness anchor: on Windows a handover blocked by a reader handle fails hard instead of waiting, and nothing proves the destination is still the file the batch derived its lines from. This is invisible on Linux — the retry is gated to IS_WINDOWS — which is exactly why it needs to be fixed in the code rather than reasoned about.
One line, on the rebase onto post-#89 main:
const since = await fileIdentity(file); // before readLines, inside the lockand pass since to writeAtomic. (Same shape still missing at src/hits.ts:70, pre-existing, not yours — I'll track it.)
Two accuracy notes
- The body oversells the win slightly. "one lock acquisition, one read, one snapshot, one publish per file touched" — the loop is
for (const file of await memoryFiles())withwithStoreLockinside, so a batch acquires the lock and reads every day file in the store, whether or not it holds a victim. Snapshot and publish are per touched file; lock and read are per file scanned. That is still a large win over N×files, but decay already computesnew Set(stale.map((m) => m.file))for its own snapshot pass, so accepting that set would cut the acquisitions from "all day files" to "the files with victims" — the remaining 90% of the saving. Worth a sentence in the body either way. - decay now double-snapshots: the pre-pass
snapshotFiles([...new Set(stale.map(m => m.file))])plus the batch'ssnapshotBefore(file)= two copies per touched file per decay run. Defensible as the crash belt the comment describes; just confirm that's intended rather than an oversight left behind by the refactor.
Status
Requesting changes for the since anchor and the body's lock/read claim. Both are small; the design itself — batch under one lock per file, idempotence re-checked under the lock, single undo point — is right, and the deviation list in the body is honest. Needs gh pr update-branch 102 regardless: it is BEHIND main under the strict required-checks policy.
abogartz
left a comment
There was a problem hiding this comment.
Adversarial review (macOS, Bun 1.4.3). The batch is correct; one merge-order consequence needs a line of code before it lands.
What I verified myself
Your red-check reproduces exactly. I replaced markManySuperseded with a faithful per-row simulation (stampRow per entry, break on first hit — the pre-fix shape) and ran the suite:
error: expect(received).toBe(expected)
Expected: 2
Received: 6
(fail) … rows across two day files retire with ONE snapshot per touched file, not per row
5 pass / 1 fail
Restored: 6 pass / 0 fail. Single failure, right reason, matching the body.
Semantics, read and checked against stampRow:
- Idempotence moved inside the lock:
touched === 0returns beforesnapshotBefore/writeAtomic, so a repeat run writes nothing and mints no undo point. ✓ - Conservation: the per-line skip is
typeof parsed.superseded_by === "string", so an out-of-band marker is never rewritten. ✓ - Duplicate
tsin one batch: first entry wins, byMapinsertion order — genuinely mirrors row-by-row. ✓ - Lock shape: one
withStoreLockper file, sequential, released before the next;runWriteGateis not itself inside a store section, so there is no nesting/deadlock surface added. ✓ markSupersededas a(await markManySuperseded([…])) > 0wrapper preserves the boolean contract for the viewer andmemory_merge_reviewcallers. ✓
Gates: lint clean, tsc --noEmit clean, bun run test → 446 pass / 5 skip / 0 fail. CI lint/test/smoke green. Also merged all four open PRs (89 → 101 → 102 → 103) in one worktree: no conflicts, 480 pass / 0 fail.
Ask — the identity anchor that #89 just made load-bearing
src/memory.ts publishes the batch with writeAtomic(file, lines.join("\n") + "\n") — no third argument. Every other whole-file store write now takes the destination's fileIdentity from before its read (stampRow, prune, deleteMemory, removeBlock, restoreSnapshot), because #89's Windows handover retry only proceeds while that anchor still matches.
The path you replaced went through stampRow, which #89 anchors. So as merged, decay and both gate neutralization loops become the only whole-file publishes in the store with no staleness anchor: on Windows a handover blocked by a reader handle fails hard instead of waiting, and nothing proves the destination is still the file the batch derived its lines from. This is invisible on Linux — the retry is gated to IS_WINDOWS — which is exactly why it needs to be fixed in the code rather than reasoned about.
One line, on the rebase onto post-#89 main:
const since = await fileIdentity(file); // before readLines, inside the lockand pass since to writeAtomic. (Same shape still missing at src/hits.ts:70, pre-existing, not yours — I'll track it.)
Two accuracy notes
- The body oversells the win slightly. "one lock acquisition, one read, one snapshot, one publish per file touched" — the loop is
for (const file of await memoryFiles())withwithStoreLockinside, so a batch acquires the lock and reads every day file in the store, whether or not it holds a victim. Snapshot and publish are per touched file; lock and read are per file scanned. That is still a large win over N×files, but decay already computesnew Set(stale.map((m) => m.file))for its own snapshot pass, so accepting that set would cut the acquisitions from "all day files" to "the files with victims" — the remaining 90% of the saving. Worth a sentence in the body either way. - decay now double-snapshots: the pre-pass
snapshotFiles([...new Set(stale.map(m => m.file))])plus the batch'ssnapshotBefore(file)= two copies per touched file per decay run. Defensible as the crash belt the comment describes; just confirm that's intended rather than an oversight left behind by the refactor.
Status
Requesting changes for the since anchor and the body's lock/read claim. Both are small; the design itself — batch under one lock per file, idempotence re-checked under the lock, single undo point — is right, and the deviation list in the body is honest. Needs gh pr update-branch 102 regardless: it is BEHIND main under the strict required-checks policy.
Dismissed to land by maintainer decision (2026-10-25): the missing fileIdentity anchor is real and is tracked as #106, along with the same gap in src/hits.ts. Not a merge blocker.
|
🎉 This PR is included in version 0.13.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
…, six unpinned invariants Provenance: src 7bfe99b, Windows, Bun 1.3.10, baseline 483/0/5. Delta vs the 7c4a22d run: publish-rename-to-truncate survived->killed (the pin abogartz#89 shipped was credited); lock-restore-failopen + viewer-snapshot-grammar-off flipped kill->survive (both re-confirmed 0/3 quiet); supersede-rmw-nolock became unstable 1/3. Both flips' killer files unchanged between the trees - abogartz#89/abogartz#102 moved the ground.
What
decayand the write-gate's drift/canonical neutralization loops calledmarkSupersededonce per row — for N retired rows that's N full lock/read/rewrite/snapshot/publish cycles against the same file(s), N undo points for one logical operation, and N interleaving windows where a concurrent run re-pays the other's work. This addsmarkManySuperseded(entries): Promise<number>insrc/memory.tsand routes all three loops through it: one lock acquisition, one read, one snapshot, one publish per file touched, whatever the row count.markSupersededstays exported with its exact boolean contract, now a one-entry wrapper. Single-call sites (viewer edits, merge loser) keep using it.Semantics preserved:
Promise.allrace).tsin one batch: first entry's fields win (mirrors row-by-row first-call-wins).Deliberate deviations (all in evidence-bookkeeping, none in store state)
decay_applied'smem=field lists the attempted ts, not only the marked ones (per-row code listed marked). Theretired=count is still exact.superseded_byevidence lines became all-or-nothing per neutralization set instead of per-row marked-checks: a row marked out-of-band between the gate's scan and the batch publish gets an evidence line though this run didn't mark it. Vanishingly narrow window; store state is identical either way.tsthat appears in several day files (should not exist — append mints unique ts) retires in every file now; the per-row loop retired only the first. The batch's behavior is the more honest tombstone; both are conservation-safe.Tests (
test/supersede-batch.test.ts, red-first)Six tests: snapshot economy (2 touched files → 2 snapshots, not 6 — the regression gate), idempotent re-run (writes nothing, byte-identical), out-of-band marker conservation, concurrent-append survival, wrapper boolean contract (absent/true/false-on-retired), duplicate-ts precedence.
Red-check: with
markManySupersededreplaced by a faithful per-row simulation (the pre-fix behavior), exactly the snapshot-economy test fails (Expected: 2, got 6) and the rest hold — the suite fails with the fix absent, for the right reason.Verified
bun run test)The Windows fail-set (resolver trio
GATE-1/S1/S-edge+ load-sensitive torn-read races) was proven pre-existing by running a main control on the same machine in the same window — the GATE-1 stack is byte-identical (resolver.ts:280dh.close()TypeError, the one #89 fixes). On a quiet rerun Linux hit the known torn-read guard flake once (fixed by #89) and went clean.tsc --noEmit+biome lintclean. Gate/scenario/decay/undo/portable targeted suites green.Refs #61.
Model-assisted work: Qwen3-flash-next (analysis and implementation assistance).