Skip to content

fix: read complete snapshots and store files - #187

Merged
abogartz merged 4 commits into
abogartz:mainfrom
WhollyMindless:fix/complete-store-reads
Oct 30, 2026
Merged

abogartz merged 4 commits into
abogartz:mainfrom
WhollyMindless:fix/complete-store-reads

Conversation

@WhollyMindless

@WhollyMindless WhollyMindless commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #184; follows the merged #181 readLines correction.

Snapshots, restore, unblock, hit-ledger rotation, import, and status now read complete file contents without combining cached BunFile size metadata with a later text/json read. Snapshots copy raw bytes before claiming the destination, preserving BOMs and newlines. Rotation uses stat only as a threshold hint and reads complete contents after its existing identity anchor. JSON readers retain BOM compatibility.

Missing snapshots are skipped, missing live restore contents remain empty, missing blocklists return false, missing imports retain their explanatory error, and status remains fail-soft. Other store read failures propagate. Locks, containment checks, identity-before-content anchors, and anchored Windows rename retries remain in place. This does not make concurrent in-place writes into atomic snapshots.

Validation (Windows / Bun 1.4.4): six caller-level real-file replacement regressions fail against main and pass here. The seam returns a real cached BunFile only for the target path; bytes are not mocked. The ten-case file also covers missing files, non-ENOENT failures, and JSON BOM compatibility. Full gate on b4a09f6 passes typecheck, lint, 715 unit tests (12 existing skips), and 11 real-host end-to-end tests. All four hosted checks passed on 843d69e. All four hosted checks now also pass on b4a09f6, including Windows. The maintainer independently verified the previous head on macOS/arm64 (Bun 1.4.3), as detailed in the approval review; our local validation uses Windows/Bun 1.4.4.

This is independent of #185 and #186. #186 also touches portable.ts for identity capture; a merge-tree check of the two heads is clean. Please feel free to decline or close this proposal if you prefer smaller pieces or another approach.

Windows CI follow-up: the original head passed all nine new reader cases but timed out in the existing ACL lock fixture. The follow-up commit gives only that test a 30-second outer deadline for subprocess setup/cleanup; its two-second lock assertion, permission-error assertion, and no-writer assertion remain intact. No production lock budget or retry policy changed. The exact slow phase was not shown by CI; all four hosted checks now pass on 843d69e, including Windows.

Review follow-up (b4a09f6): a no-mock runtime-premise test primes a five-byte BunFile, atomically replaces it with a distinct 37-byte payload, verifies the complete disk contents, and asserts two cached text reads return the cropped new prefix. It deliberately fails when Bun fixes this behavior so the readers and regression seams must be re-audited. No runtime implementation changed in this follow-up. The unresolved Windows ACL phase timing is tracked on #68 (#68 (comment)). The import writer-resolution comment cleanup is now addressed on #186 in 72edbb3; a merge-tree check with this head is clean, and error precedence remains unchanged.

@WhollyMindless

WhollyMindless commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor Author

The Windows failure on 14dd130 was the existing ACL lock test exceeding Bun's outer five-second deadline (reported duration 6.70s). All nine new complete-reader tests passed. No assertion failure identifies the lock itself as slow, and the log does not distinguish ACL setup, lock work, or cleanup as the source of the elapsed time.

The original ACL test passes ten local repetitions with all four assertions exercised. Pushed 843d69e, retaining its two-second lock-duration assertion, permission-error check, and requirement that the writer never runs, while giving only this test a 30-second outer budget for its whoami/icacls subprocesses and cleanup. Production lock timeouts, retries, and other test deadlines remain unchanged. Fresh hosted Windows CI must validate the adjustment before treating this PR as ready.

Local validation of the new head: full gate passes typecheck, lint, 714 unit tests (12 existing skips), and 11 real-host end-to-end tests. The ACL case passed in 131 ms.

CI update: all four hosted checks now pass on 843d69e, including Windows. The PR is clean and awaits maintainer review.

@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 — real data loss, reproduced independently on macOS. Nits are non-blocking.

Verified on macOS/arm64 with local Bun 1.4.3 (CI pins 1.4.4), in a clean worktree per head, plus a combined worktree with main and #186.

The defect is real on this platform, and I reproduced it without mocks

  • Mechanism. A BunFile primed by exists() at 5 bytes, then a 37-byte replacement renamed over the path, returns "BBBBB" from .text() — the new file's truncated prefix, not stale-but-valid old content. So the loss mode is a partial row, and a second .text() on the same handle stays cropped.

  • Differential through the real caller. snapshotFiles([dayFile]) with an atomic replacement renamed in during the call, N=300:

    • origin/main → 300/300 snapshots cropped (240 bytes of a 20,095-byte file, final row absent).
    • this branch → 0/300 cropped, 300/300 complete single-version copies.

    That is #184's undo-snapshot loss, measured. (My first race classifier was confounded because the replacement shared the original's prefix; the numbers above come from the corrected design where the first byte differs.)

  • I could not force a cropped publish through removeBlock here: 400 iterations with a concurrent renamer produced 0 rows truncated mid-value on both trees. So for the other five sites the claim stands on source shape + your deterministic probes, not on a measured rate from me — which is how #184 words it too.

The anchors were not covering the anchored sites

handoverRename (src/logfmt.ts:159-175) renames unconditionally on the first attempt and consults since only to decide whether a Windows retry is safe. So removeBlock, rotateIfNeeded and restoreSnapshot were exposed on POSIX exactly like the unanchored snapshot copy. Fixing all six rather than only snapshotFiles is correct, not scope creep.

Sweep is complete for this defect class

Every site that primes exists()/.size and later reads through that same handle is converted. Remaining Bun.file(...) uses either create and consume the handle in one expression (init.ts:99, resolver.ts:317, lock.ts:213, viewer/server.ts:132, index.ts:1644, command.ts:18) or use metadata for display only (undo.ts:71-72 lists bytes/mtime; resolver.ts:420 is an existence test). Nothing left in the shape.

Per-caller error policy preserved

Checked each function, not just the diff: snapshot skip / restore empty-live / blocklist false / rotation no-op / import keeps import file not found / status still fail-soft through its catch-all. Non-ENOENT now propagates from the mutating callers and reads as an error instead of "not there" (the EISDIR case) — louder in the right direction. Copying the Buffer rather than decoded text also means invalid UTF-8 in a day file survives a restore instead of becoming U+FFFD, and reading before claimSnapshot stops a failed read from burning an O_EXCL snapshot name.

Gates

  • Branch: typecheck clean, lint exit 0, bun test ./test/ 714 pass / 12 skip / 0 fail (matches the body). One full-suite run failed a timing-conditional control in the #40 negative-controls file at its 5 s budget — that was three suites competing for the machine; it passes serially and in isolation, and nothing in this PR touches it.
  • test/complete-store-reads.test.ts dropped onto origin/main: 7 of 9 fail; on the branch 9/9 pass.
  • Combined main + #186 + #187: merges clean, typecheck/lint clean, 719 pass / 12 skip / 0 fail, real-host e2e 11 pass / 0 fail.
  • CI 4/4 on 843d69e. I confirmed the only failure on the first head 14dd130 was the Windows ACL fixture at 6700 ms, and src/lock.ts is untouched here.

On the ACL budget: raising the outer test budget to 30 s is acceptable — the property (ran === false, it failed, < 2000 ms, permission message) is asserted inside it, so the fixture can no longer pass by being slow but it still cannot pass by being wrong.

Nits (none blocking)

  1. Nothing pins the premise. The six replacement tests are red on main today, but no test asserts the mechanism itself. If Bun fixes the stale size, or if a refactor stops calling Bun.file with the exact path string the seam intercepts, the seam quietly stops mattering and all six pass vacuously. One test asserting "a cached handle's .text() after a rename returns the truncated prefix", commented fails when Bun fixes this — re-audit the readers, would keep it honest. The repo already pins behaviour this way for Bun.write({append:true}).
  2. After #186 lands, importMemories resolves the writer first and then reads the file, so the #152 comment block ("…so this throws before a single row is gated") ends up sitting below both the resolution and the read. No behaviour issue (nothing mutates before the throw), just a comment that no longer sits where the reasoning is.
  3. The slow phase in the Windows ACL fixture is still unidentified (you say so plainly). Worth a line on the test-integrity list so a 30 s budget doesn't become permanent by accident.

@WhollyMindless

Copy link
Copy Markdown
Contributor Author

Thanks for the independent macOS verification and the corrected race-classifier details. Pushed b4a09f6 for nit 1: the new no-mock test pins the cached handle returning the replacement's truncated prefix twice, while a direct disk read verifies the complete replacement. Its comment explicitly asks for a reader/negative-control re-audit when Bun fixes the premise.

Local Windows/Bun 1.4.4: 10/10 focused tests, full gate green (715 unit passes, 12 existing skips, 11 real-host end-to-end passes, typecheck and lint). Runtime implementation is unchanged; fresh CI is pending.

For nit 3, recorded the still-unidentified ACL phase timing and measurement plan on the existing test-integrity list: #68 (comment). This avoids treating the larger fixture budget as a completed diagnosis.

For nit 2, #186 is still open. I recorded the import-comment cleanup for integration after it lands; the owner rejection should remain after file validation/read, preserving the error precedence you verified. No change to that ordering is proposed here.

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

Round 2: APPROVED at b4a09f6 — the premise guard is exactly what I wanted. Green for merge.

The only change since the head I approved is the 16-line premise test; no src/ change.

What I checked on it:

  • It pins the mechanism, not just the symptom. A cached handle primed at 5 bytes returns "BBBBB" after a 37-byte replacement, asserted twice so a first-read special case cannot hide behind a warm handle — and the distinct-prefix construction means truncation can never be mistaken for a legitimate old-version read. That was the exact confound in my own first attempt at this differential, so I am glad it is written down in the test rather than left in a comment.
  • Correct polarity for a premise test. It passes on origin/main (file on main: 3 pass / 7 fail, and the passing three are this premise test plus the two controls), which is right: it documents Bun's behaviour rather than the fix. The comment naming what to do when it goes red — re-audit the readers instead of deleting the assertion — is what keeps a future maintainer from "fixing" it by inversion.
  • The premise now holds on all three runtimes, not just mine. My reproduction was macOS/Bun 1.4.3; your CI 4/4 on b4a09f6 (Ubuntu + Windows, Bun 1.4.4) is the first evidence that the cached-size crop is identical there, since this test executes the raw runtime on every leg.

Gates at b4a09f6: typecheck clean, lint exit 0, bun test ./test/ 715 pass / 12 skip / 0 fail (714 + the new premise test). Combined with #186 at 72edbb3 on top of main: merges clean, typecheck/lint clean, 722 pass / 12 skip / 0 fail, real-host e2e 11 pass / 0 fail. Six clean consecutive runs here, serial and concurrent; the single red I saw in one 3-way parallel batch did not reproduce in five further concurrent runs and is the pre-existing timing-conditional control, not anything in this diff.

No open asks. Merging this closes #184.

@abogartz
abogartz merged commit d400901 into abogartz:main Oct 30, 2026
4 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.14.23 🎉

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.

Follow-up to #181: avoid cached-size reads in snapshots and remaining store readers

2 participants