Skip to content

fix(store): the batch supersession + ledger rotation get the fileIdentity anchor (#106) - #112

Merged
abogartz merged 2 commits into
abogartz:mainfrom
WhollyMindless:fix/batch-publish-anchor
Oct 26, 2026
Merged

abogartz merged 2 commits into
abogartz:mainfrom
WhollyMindless:fix/batch-publish-anchor

Conversation

@WhollyMindless

Copy link
Copy Markdown
Contributor

Fixes #106 - both sites, both paths the issue names.

What

markManySuperseded (from #102) and hits.ts rotateIfNeeded (pre-existing) published whole-file read-modify-writes without the fileIdentity/since anchor #89 made the standard. On Windows, mandatory locking refuses the handover rename while any reader still holds the destination - so the batch died outright on an ordinary retirement (writeAtomic's no-anchor path is exactly one rename), and the ledger rotation died silently, swallowed by recordHits' telemetry catch: a lost halving with no signal, the ring growing past its cap.

The fix is the issue's, verbatim: const since = await fileIdentity(file) inside the lock before readLines (and before the ledger read), threaded to writeAtomic. With the anchor, the healthy Windows shape - blocked rename, file unmoved - retries and lands inside the ~2s window; the dangerous shape - an unlocked append landing mid-window - aborts honestly with the destination intact, exactly as stampRow already did.

Tests (red-first on the windows leg)

Three pins reusing #89's held() machinery (openSync(file, "r") shares reads/writes but blocks replacement):

  • batch: retirement against a reader handle lands (pre-fix: throws the retirement away - red on 4627d04-era leg for the same reason fix(store): make the store's file semantics hold on Windows (+ a windows-latest CI leg) #89's stampRow fix existed).
  • batch: degraded append mid-window aborts the publish; the append survives, nothing half-published, and a retry of the batch recovers fully (pre-fix: red on the recovery leg - the retry can't land while the handle lives, which is precisely what an anchored retry is for).
  • ledger: rotation against a held ledger returns true and halves the file (pre-fix: false + silently over-cap - red).

POSIX takes neither path (rename swaps under readers, the retry is IS_WINDOWS-gated) - the three tests are skipIf(!win32); the red leg is test-windows CI.

Verified

Bun 1.3.10 Bun 1.4.4
Windows full suite 486 pass / 0 fail new pins + neighborhood 21/21
Linux (WSL2, full bun run test) 480 pass / 0 fail (8 win32 skips) 480 pass / 0 fail

tsc --noEmit clean. The zero-fail Windows number finally agrees with the store's promises because #89 is in the tree - nice time to be doing this follow-up.

assisted by Qwen3-flash-next

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

Verdict: APPROVED

Independently verified, not trusting the body.

Mechanism traced end to end. handoverRename (src/logfmt.ts:157-180) fails → Windows+anchored only → ≤2s retry, every pass re-checks fileIdentity(file) !== since → throw. Both sites now capture the anchor before their read inside the lock: markManySuperseded (src/memory.ts:381, before readLines) matches stampRow's established shape (memory.ts:347); rotateIfNeeded (src/hits.ts:71) runs inside withStoreLock(hitsFile(), …) (hits.ts:114-118), so the ledger anchor is taken under the lock too. Exactly what #106 prescribed.

Red-first is a construction proof, and I confirmed it by reading the pre-fix path: writeAtomic without since → handoverRename hits if (!IS_WINDOWS || since === undefined) throw err → one rename, one throw. There is no retry for the old shape to have reached; both batch tests and the ledger test could only have been red. The ledger test's cap arithmetic checks out (40 000 rows asserted > 2 000 000 bytes against MAX_HITS_BYTES = 2_000_000), and the assertions distinguish halve-vs-no-halve (lines.length < filler.length + 1) rather than just "no throw".

POSIX inertness is the right claim, and I checked the trap: on POSIX the first rename always succeeds, so the anchor never fires — the new tests are skipIf(!win32) and this platform sees no new failure mode. On macOS full suite: 480 pass / 11 skip / 0 fail, tsc --noEmit clean.

Residual (accepted, matches #106's scope): a rotation that aborts on a genuine mid-window append is still swallowed by recordHits' telemetry catch — the halving is skipped silently, though now without ever clobbering the append. That is the pre-existing "telemetry never breaks retrieval" trade, unchanged.

CI: test-windows passed on this branch (486-era leg); the three pins only execute there.

@abogartz
abogartz merged commit 1c1a81c into abogartz:main Oct 26, 2026
4 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.14.2 🎉

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.

store: batch supersession publishes without the fileIdentity anchor #89 made standard

2 participants