[PR #5607] [MERGED] fix(storage): re-merge load-created healing marks on transaction rollback #6845

Closed
opened 2026-10-23 04:14:14 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5607
Author: @kolacheee
Created: 8/28/2026
Status: ✅ Merged
Merged: 8/28/2026
Merged by: @kolacheee

Base: staging ← Head: fix/5606-tx-load-heal-marks


📝 Commits (1)

  • f734c49 fix(storage): re-merge load-created healing marks on transaction rollback

📊 Changes

3 files changed (+99 additions, -0 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/server/src/db/file-backed-store.ts (+49 -0)
📝 scripts/regressions/lazy-chat-units.regression.ts (+49 -0)

📄 Description

Why

Closes #5606 — the pre-existing data-loss corner found during #5603's adversarial verification (reproduced on the parent commit with eviction disabled, so it has been latent since #5597 merged). A chat shard file holding another chat's misfiled rows — the exact state the store's self-healing exists to repair — could lose those rows' only on-disk copy if the unit loaded inside a transaction that rolled back.

What

A lazy unit load creates its healing marks in one synchronous block: dirty keys for every row's real shard, plus a stale mark on the stray-holding file. The flush's write-then-unlink ordering is only safe because those travel together. Rollback broke the pairing: it restored dirtyShards/dirtyTables from the pre-transaction snapshot (erasing the load's keys) while the stale mark — correctly never rolled back — survived alone. The next flush rewrote the host file canonically without the stray, and the stray's own shard was skipped by the clean-shard fast path.

Loads are deliberately not transaction mutations — their rows survive rollback via the existing snapshot mirror. Now their marks do too: the transaction context records load-created dirty keys/tables (all three creation sites: shard-file heal, empty-.bak stale, duplicate drop), and rollback re-merges them. The invariant restored: a stale mark never reaches a flush without the dirty keys it was created with — the same pairing rule the flush-capture work in #5597/#5603 enforced for in-flight flushes, now enforced across rollbacks.

Regressions

New block in lazy-chat-units.regression.ts staging the exact interleaving (misfiled row whose canonical shard file exists — the file-absent variant is masked by the recreate-if-missing rule; mid-transaction load; forced rollback; flush) asserting the misfiled row is re-homed rather than erased. Verified red against the pre-fix store on exactly that assertion. Full lazy suite plus message-sharding in both lazy and eager modes green locally.

Test plan

  • pnpm check on a non-Windows checkout
  • pnpm regression
  • Manually verify: normal chat use, a failed/aborted request mid-chat, then restart — no message loss

Summary by CodeRabbit

  • Bug Fixes

    • Improved storage recovery during transaction rollbacks.
    • Prevented chat rows loaded from misplaced storage files from being lost.
    • Ensured recovered rows are moved to the correct location and removed from stale locations after saving.
  • Tests

    • Added regression coverage for storage recovery following a rolled-back transaction.
  • Documentation

    • Documented the fix in the unreleased changelog.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/5607 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 8/28/2026 **Status:** ✅ Merged **Merged:** 8/28/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `fix/5606-tx-load-heal-marks` --- ### 📝 Commits (1) - [`f734c49`](https://github.com/Pasta-Devs/Marinara-Engine/commit/f734c49aedec4353c79f256dd550d7980613cb4c) fix(storage): re-merge load-created healing marks on transaction rollback ### 📊 Changes **3 files changed** (+99 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/server/src/db/file-backed-store.ts` (+49 -0) 📝 `scripts/regressions/lazy-chat-units.regression.ts` (+49 -0) </details> ### 📄 Description ## Why Closes #5606 — the pre-existing data-loss corner found during #5603's adversarial verification (reproduced on the parent commit with eviction disabled, so it has been latent since #5597 merged). A chat shard file holding another chat's misfiled rows — the exact state the store's self-healing exists to repair — could lose those rows' **only on-disk copy** if the unit loaded inside a transaction that rolled back. ## What A lazy unit load creates its healing marks in one synchronous block: dirty keys for every row's real shard, plus a stale mark on the stray-holding file. The flush's write-then-unlink ordering is only safe because those travel together. Rollback broke the pairing: it restored `dirtyShards`/`dirtyTables` from the pre-transaction snapshot (erasing the load's keys) while the stale mark — correctly never rolled back — survived alone. The next flush rewrote the host file canonically without the stray, and the stray's own shard was skipped by the clean-shard fast path. Loads are deliberately not transaction mutations — their rows survive rollback via the existing snapshot mirror. Now their marks do too: the transaction context records load-created dirty keys/tables (all three creation sites: shard-file heal, empty-`.bak` stale, duplicate drop), and rollback re-merges them. The invariant restored: **a stale mark never reaches a flush without the dirty keys it was created with** — the same pairing rule the flush-capture work in #5597/#5603 enforced for in-flight flushes, now enforced across rollbacks. ## Regressions New block in `lazy-chat-units.regression.ts` staging the exact interleaving (misfiled row whose canonical shard file exists — the file-absent variant is masked by the recreate-if-missing rule; mid-transaction load; forced rollback; flush) asserting the misfiled row is re-homed rather than erased. Verified red against the pre-fix store on exactly that assertion. Full lazy suite plus message-sharding in both lazy and eager modes green locally. ## Test plan - [x] `pnpm check` on a non-Windows checkout - [x] `pnpm regression` - [x] Manually verify: normal chat use, a failed/aborted request mid-chat, then restart — no message loss <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved storage recovery during transaction rollbacks. * Prevented chat rows loaded from misplaced storage files from being lost. * Ensured recovered rows are moved to the correct location and removed from stale locations after saving. * **Tests** * Added regression coverage for storage recovery following a rolled-back transaction. * **Documentation** * Documented the fix in the unreleased changelog. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:14:14 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#6845
No description provided.