Repository navigation
fix: publish daily memory appends atomically - #203
Conversation
abogartz
left a comment
There was a problem hiding this comment.
Verdict: APPROVE — the trade is worth having; no blocking findings.
Adversarial review at head 7c1dfc4 (base bf9f4d2). I evaluated this as a scope question, not just a diff question: is a whole-file publish per daily append a capability the project should have? Yes — it is the write-side completion of the policy this project has already chosen twice: #197 made writers fail loud rather than destroy silently, and #181/#187 made store reads whole-file-atomic for exactly the same partial-visibility family. The remaining in-place append was the last way this store produced a torn row, and "concurrency reader partial-line" is a CI failure I independently measured as ambient earlier this week (attributed with base rates, not gut) — this PR turns that flake into a contract.
Body claims checked against the tree
- One read, one write: the ts-uniqueness read (
existingTs, now a pure function over text,src/memory.ts:218) feeds on the samepriorbytes the publish writes; only caller is the append path itself. So the marginal cost is genuinely one whole-file write, as the bench models. - Newline normalization: concatenation adds a terminator only when
prioris nonempty and unterminated;parseLinedrops the empty split piece. Pinned by the static-unterminated-row test. - Safety envelope: publish goes through the store's established
writeAtomic+fileIdentitypath with a per-call unique temp andunlink-on-failure;LOCK_WRITEis fail-closed on this base (src/lock.ts:63-64), so the feared shape — an unlocked read-modify-write past the 5 s budget clobbering concurrent state — cannot occur; contention degrades to a visible refusal.appendMemoryplus the pre-existing in-lock whole-file rewrites are the only day-file writers;logDeletion/evidence/hits/blocklist/tombstones append to their own files, exactly the excluded set in the body and README. - Windows availability trade: README states it plainly (refusal after retry window, old bytes intact, retry on release), and the Windows test first establishes the OS premise on the live runtime (raw rename into the held handle must fail) before asserting the refusal, the intact bytes, no stray
.tmp/.lock, and post-release success. That's the right way to pin a platform premise. - Scope honesty:
Refs #198, notFixes; README keeps the broader read boundary open; no crash-durability or uncooperative-writer claim.
Verification (macOS, Bun 1.4.3; Windows legs via CI)
- Red-on-base measured: both deterministic publication tests fail on unchanged
bf9f4d2(merged-row corruption;intercepted=falsesince base never touches a temp), Windows case skipped as expected. - Focused at head: 9 pass / 1 skip / 0 fail — your Linux/WSL numbers exactly; the strengthened reader test ran 10× clean at head (no self-introduced flake).
- Bench re-verified from raw JSONL (independent recomputation, 30 samples per cell): 50 MiB medians Windows 105.5 → 136.6 ms, WSL 100.7 → 124.3 ms — your numbers to one decimal; committed
summary.jsonmatches; methodology's limitations section (warm cache, one host, no durability claim) matches the body's hedging. At realistic day sizes the cost is +0.3–0.9 ms. - Full gate at head: 831 pass / 12 skip / 0 fail on macOS = base 829/11/0 + 2 macOS-runnable tests + the Windows skip; your Windows-gate 832/11 is the same arithmetic with the third test running. E2E 11/0, typecheck clean.
- CI on
7c1dfc4: lint / test / smoke / test-windows all green;mergeStateStatus CLEAN. - On the record: I could not force the broken-side partial-line locally — the strengthened classifier ran green 10× on the base tree here (idle FS; consistent with my #189-round measurement that this family resisted 150 local forced runs and only bit under CI contention). The justification is the deterministic mechanism tests plus the CI history, not a local reproduction, and the PR does not claim one.
- Merge-order note for whoever merges: #202 also edits
README.md, but in a different region (~70 lines apart, common basebf9f4d2) — order-independent, no conflict expected.
One observation, not a nit: the lock window now spans a whole-file read + write; at the 50 MiB extreme a contended writer can wait toward the 5 s budget before failing closed. That is loud-refusal territory, not loss territory, precisely because #197 is in the base — this change is only safe on top of that policy, so it should stay that way.
Not merging — the user merges by hand.
|
🎉 This PR is included in version 0.15.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
A concurrent reader can observe a daily memory file midway through an in-place append, even though writers hold the store lock. This publishes the complete previous bytes plus the new row through the existing atomic replacement path. A static last row without a trailing newline also survives the next append as a separate row.
Refs #198. This is a deliberately narrow daily-memory fix, not closure of the whole issue: evidence, use, rejection, blocklist, and deletion/audit append paths remain unchanged. Built on main bf9f4d2, including #197's fail-closed writer policy. readLines behavior is unchanged.
Tradeoffs for review
Timestamp uniqueness already required a full day-file read; this adds a full day-file write. On Windows, a real reader handle that prevents replacement now causes a refused append after the existing retry window, with the old bytes intact. Retrying after releasing the handle succeeds. The former in-place append could succeed under that handle. The test explicitly establishes this OS/runtime premise.
Bench (Bun 1.4.4, one physical host, Windows plus Ubuntu WSL/ext4): at 50 MiB, median append time was 105.5 → 136.6 ms on Windows and 100.7 → 124.3 ms on Linux/WSL. These are synthetic warm-cache measurements, not throughput or latency guarantees. Each mode/size/platform has 30 timed samples across three fresh processes; sizes are 0.1, 1, 10, and 50 MiB. All runs preserved the original bytes and every row. The script, raw measurements, aggregation and reproduction instructions are in
bench/append-cost-198.tsandbench/append-publication-198/.Validation
No claim of crash durability or a fix for uncooperative writers. Existing atomic replacement and cooperative-lock semantics still apply.
This is an option for your consideration. It is completely fine to decline or close it if the write cost or Windows availability tradeoff is not what you want; the investigation and measurements can stand on their own. No merge is requested until you are comfortable with that contract.