Skip to content

perf(store): batch supersession - one pass per file for decay and gates (#61) - #102

Merged
abogartz merged 3 commits into
abogartz:mainfrom
WhollyMindless:fix/batch-supersede
Oct 25, 2026
Merged

abogartz merged 3 commits into
abogartz:mainfrom
WhollyMindless:fix/batch-supersede

Conversation

@WhollyMindless

Copy link
Copy Markdown
Contributor

What

decay and the write-gate's drift/canonical neutralization loops called markSuperseded once 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 adds markManySuperseded(entries): Promise<number> in src/memory.ts and routes all three loops through it: one lock acquisition, one read, one snapshot, one publish per file touched, whatever the row count.

markSuperseded stays exported with its exact boolean contract, now a one-entry wrapper. Single-call sites (viewer edits, merge loser) keep using it.

Semantics preserved:

  • Idempotence moves inside the lock: the batch re-reads each file, so a row superseded out-of-band between the caller's scan and the batch's pass is skipped, never re-marked. Re-running a batch marks 0 rows and writes nothing (byte-identical file, no new undo point — pinned).
  • Conservation: an out-of-band marker is never overwritten (pinned).
  • Racing appends: the batch publishes under the store lock, so an append landing mid-batch survives (pinned with a real concurrent Promise.all race).
  • Duplicate ts in one batch: first entry's fields win (mirrors row-by-row first-call-wins).

Deliberate deviations (all in evidence-bookkeeping, none in store state)

  1. decay_applied's mem= field lists the attempted ts, not only the marked ones (per-row code listed marked). The retired= count is still exact.
  2. The gate's superseded_by evidence 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.
  3. A ts that 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 markManySuperseded replaced 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 1.3.10 Bun 1.4.4
Linux (Ubuntu 24, full bun run test) 381 pass / 0 fail 381 pass / 0 fail
Windows full suite main-parity fail-set only main-parity fail-set only

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:280 dh.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 lint clean. Gate/scenario/decay/undo/portable targeted suites green.

Refs #61.


Model-assisted work: Qwen3-flash-next (analysis and implementation assistance).

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 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.

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 === 0 returns before snapshotBefore/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 ts in one batch: first entry wins, by Map insertion order — genuinely mirrors row-by-row. ✓
  • Lock shape: one withStoreLock per file, sequential, released before the next; runWriteGate is not itself inside a store section, so there is no nesting/deadlock surface added. ✓
  • markSuperseded as a (await markManySuperseded([…])) > 0 wrapper preserves the boolean contract for the viewer and memory_merge_review callers. ✓

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 lock

and pass since to writeAtomic. (Same shape still missing at src/hits.ts:70, pre-existing, not yours — I'll track it.)

Two accuracy notes

  1. 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()) with withStoreLock inside, 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 computes new 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.
  2. decay now double-snapshots: the pre-pass snapshotFiles([...new Set(stale.map(m => m.file))]) plus the batch's snapshotBefore(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 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.

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 === 0 returns before snapshotBefore/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 ts in one batch: first entry wins, by Map insertion order — genuinely mirrors row-by-row. ✓
  • Lock shape: one withStoreLock per file, sequential, released before the next; runWriteGate is not itself inside a store section, so there is no nesting/deadlock surface added. ✓
  • markSuperseded as a (await markManySuperseded([…])) > 0 wrapper preserves the boolean contract for the viewer and memory_merge_review callers. ✓

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 lock

and pass since to writeAtomic. (Same shape still missing at src/hits.ts:70, pre-existing, not yours — I'll track it.)

Two accuracy notes

  1. 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()) with withStoreLock inside, 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 computes new 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.
  2. decay now double-snapshots: the pre-pass snapshotFiles([...new Set(stale.map(m => m.file))]) plus the batch's snapshotBefore(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
abogartz dismissed stale reviews from themself October 25, 2026 21:43

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.

@abogartz
abogartz merged commit 4eb0204 into abogartz:main Oct 25, 2026
3 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.13.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@WhollyMindless
WhollyMindless deleted the fix/batch-supersede branch October 26, 2026 00:27
WhollyMindless added a commit to WhollyMindless/opencode-self-improvement that referenced this pull request Oct 26, 2026
…, 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.
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