[PR #5610] [MERGED] fix(storage): treat valid-JSON non-array shard roots as corruption #6846

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

📋 Pull Request Information

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

Base: staging ← Head: fix/5601-non-array-roots


📝 Commits (2)

  • cf6bae2 fix(storage): treat valid-JSON non-array shard roots as corruption
  • 7c48582 fix(storage): quarantine an unrecoverable monolith during migration

📊 Changes

3 files changed (+156 additions, -9 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/server/src/db/file-backed-store.ts (+49 -9)
📝 scripts/regressions/message-sharding.regression.ts (+106 -0)

📄 Description

Why

Closes #5601 (found during #5597's review cycle; the behavior is identical in the pre-lazy eager loader, so it has existed since sharding). echo '{}' > <chat-shard>.json made that chat silently render empty — no log, no quarantine — even with a perfectly good .bak next to it.

Independent of #5609 — based directly on staging; the two touch disjoint regions and merge in either order.

What

parseJsonFile gains an optional root validator whose failure throws inside the same read step as JSON.parse — so shape corruption reuses the existing recovery ladder verbatim: primary fails → .bak is tried (and validated); both bad → fallback with unreadablePaths, which the existing downstream machinery turns into a .corrupt-* quarantine. All five row-array call sites pass Array.isArray: the eager boot loop, the lazy unit loader, the flat-table loop, the messages harvest (so id-scoped queries see backup-recovered rows too), and the monolith migration read. The manifest reader deliberately keeps its tolerant handling — its consumers already treat junk defensively and its root is an object.

Recovery composes with the existing heal: a backup-recovered shard is dirtied, the next flush rewrites the primary from the recovered rows with backup-refresh suppressed, so the corrupt primary can never clobber the good .bak (same #4708-era contract).

Regressions

Two new blocks in message-sharding.regression.ts (runs in both lazy and eager modes): non-array primary + valid backup → rows recover, id-scoped harvest reads work, healing flush rewrites the primary and never touches the backup; non-array primary with no backup → quarantined under .corrupt-* instead of silently kept. Verified red against the pre-fix store on the issue's exact symptom. Lazy suite, shutdown, and slurp lanes green.

Test plan

  • pnpm check on a non-Windows checkout
  • pnpm regression
  • Manually verify the issue's repro: overwrite a chat's messages shard with {}, restart — with a .bak present the chat recovers; without one the file lands in .corrupt-* and the log says so

Summary by CodeRabbit

  • Bug Fixes

    • Improved recovery for storage files containing valid JSON with an unexpected structure.
    • Restores data from valid backups when available and repairs affected files during subsequent saves.
    • Safely quarantines unusable files when no valid backup exists, preventing data from being treated as empty.
  • Tests

    • Added regression coverage for malformed shard files and backup recovery scenarios.
  • Documentation

    • Documented handling of valid-JSON storage corruption.

🔄 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/5610 **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/5601-non-array-roots` --- ### 📝 Commits (2) - [`cf6bae2`](https://github.com/Pasta-Devs/Marinara-Engine/commit/cf6bae2b6ecb57245d659c11131669950dbbf3ad) fix(storage): treat valid-JSON non-array shard roots as corruption - [`7c48582`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7c48582f01fdfc11862b35cf4db3ec1ce9b7fbc0) fix(storage): quarantine an unrecoverable monolith during migration ### 📊 Changes **3 files changed** (+156 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/server/src/db/file-backed-store.ts` (+49 -9) 📝 `scripts/regressions/message-sharding.regression.ts` (+106 -0) </details> ### 📄 Description ## Why Closes #5601 (found during #5597's review cycle; the behavior is identical in the pre-lazy eager loader, so it has existed since sharding). `echo '{}' > <chat-shard>.json` made that chat silently render empty — no log, no quarantine — even with a perfectly good `.bak` next to it. Independent of #5609 — based directly on staging; the two touch disjoint regions and merge in either order. ## What `parseJsonFile` gains an optional root validator whose failure throws inside the same read step as `JSON.parse` — so shape corruption reuses the existing recovery ladder verbatim: primary fails → `.bak` is tried (and validated); both bad → fallback with `unreadablePaths`, which the existing downstream machinery turns into a `.corrupt-*` quarantine. All five row-array call sites pass `Array.isArray`: the eager boot loop, the lazy unit loader, the flat-table loop, the messages harvest (so id-scoped queries see backup-recovered rows too), and the monolith migration read. The manifest reader deliberately keeps its tolerant handling — its consumers already treat junk defensively and its root is an object. Recovery composes with the existing heal: a backup-recovered shard is dirtied, the next flush rewrites the primary from the recovered rows with backup-refresh suppressed, so the corrupt primary can never clobber the good `.bak` (same #4708-era contract). ## Regressions Two new blocks in `message-sharding.regression.ts` (runs in both lazy and eager modes): non-array primary + valid backup → rows recover, id-scoped harvest reads work, healing flush rewrites the primary and never touches the backup; non-array primary with no backup → quarantined under `.corrupt-*` instead of silently kept. Verified red against the pre-fix store on the issue's exact symptom. Lazy suite, shutdown, and slurp lanes green. ## Test plan - [x] `pnpm check` on a non-Windows checkout - [x] `pnpm regression` - [x] Manually verify the issue's repro: overwrite a chat's messages shard with `{}`, restart — with a `.bak` present the chat recovers; without one the file lands in `.corrupt-*` and the log says so <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Improved recovery for storage files containing valid JSON with an unexpected structure. - Restores data from valid backups when available and repairs affected files during subsequent saves. - Safely quarantines unusable files when no valid backup exists, preventing data from being treated as empty. - **Tests** - Added regression coverage for malformed shard files and backup recovery scenarios. - **Documentation** - Documented handling of valid-JSON storage corruption. <!-- 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:15 +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#6846
No description provided.