[GH-ISSUE #5601] A shard file with a valid non-array JSON root loads as empty — no quarantine, and a valid .bak is never consulted #3104

Closed
opened 2026-10-21 05:59:38 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @kolacheee on GitHub (Aug 28, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5601

Originally assigned to: @kolacheee on GitHub.

What happens

A shard file whose content is syntactically valid JSON but not an array — for example {} or {"rows": [...]} after a hand edit, a partial download, or an external tool writing the wrong shape — loads as zero rows with no error, no quarantine, and no backup recovery. The chat (or other entity) silently appears empty, and because JSON parsing succeeded on the primary, a perfectly valid .bak sitting right next to it is never consulted.

Why

parseJsonFile only falls back to .bak when parsing fails. Both shard-loading paths in packages/server/src/db/file-backed-store.ts then guard the root with Array.isArray(value) ? value : [] — a non-array root becomes an empty list, so malformedRowCount is 0 - 0 = 0 and every recovery branch (preserve-source, quarantine, dirty-marking for self-heal) is skipped. This is not new: the eager boot loop and the lazy per-unit loader added in #5597 share the exact same pattern (the lazy path deliberately replicated the eager pipeline), and the boot harvest skips such files the same way. Surfaced by CodeRabbit while reviewing #5597.

Try it yourself

echo '{}' > <storage>/tables/messages/<some-chat>.json

then start the server and open that chat: it renders empty, no log line mentions the file, and nothing is quarantined. Restore the array content and the messages return.

Suggested direction

Treat a non-array root as corruption in one place rather than three: teach parseJsonFile (or a thin wrapper used by the shard paths) to accept a root validator, so a non-array root on the primary triggers the existing .bak fallback, and when both are bad, the existing quarantine path. That keeps eager, lazy, and harvest behavior aligned automatically. The subtlety worth a decision: today .bak is only read when the primary fails to parse — extending "fails" to "fails validation" is the actual fix, and it touches the shared recovery primitive used by every table, which is why this is a standalone change with its own tests rather than a rider on #5597.

How verified

Code reading on the #5597 branch (the Array.isArray(value) ? value : [] guard in the eager boot loop, the lazy loadShardFileSync, and the boot harvest) plus the repro above. Behavior is identical with and without MARINARA_EAGER_STORAGE=1, confirming it predates the lazy-loading work.

Originally created by @kolacheee on GitHub (Aug 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5601 Originally assigned to: @kolacheee on GitHub. **What happens** A shard file whose content is syntactically valid JSON but not an array — for example `{}` or `{"rows": [...]}` after a hand edit, a partial download, or an external tool writing the wrong shape — loads as **zero rows with no error, no quarantine, and no backup recovery**. The chat (or other entity) silently appears empty, and because JSON parsing *succeeded* on the primary, a perfectly valid `.bak` sitting right next to it is never consulted. **Why** `parseJsonFile` only falls back to `.bak` when parsing fails. Both shard-loading paths in `packages/server/src/db/file-backed-store.ts` then guard the root with `Array.isArray(value) ? value : []` — a non-array root becomes an empty list, so `malformedRowCount` is `0 - 0 = 0` and every recovery branch (preserve-source, quarantine, dirty-marking for self-heal) is skipped. This is not new: the eager boot loop and the lazy per-unit loader added in #5597 share the exact same pattern (the lazy path deliberately replicated the eager pipeline), and the boot harvest skips such files the same way. Surfaced by CodeRabbit while reviewing #5597. **Try it yourself** ```bash echo '{}' > <storage>/tables/messages/<some-chat>.json ``` then start the server and open that chat: it renders empty, no log line mentions the file, and nothing is quarantined. Restore the array content and the messages return. **Suggested direction** Treat a non-array root as corruption in one place rather than three: teach `parseJsonFile` (or a thin wrapper used by the shard paths) to accept a root validator, so a non-array root on the primary triggers the existing `.bak` fallback, and when both are bad, the existing quarantine path. That keeps eager, lazy, and harvest behavior aligned automatically. The subtlety worth a decision: today `.bak` is only read when the primary fails to *parse* — extending "fails" to "fails validation" is the actual fix, and it touches the shared recovery primitive used by every table, which is why this is a standalone change with its own tests rather than a rider on #5597. **How verified** Code reading on the #5597 branch (the `Array.isArray(value) ? value : []` guard in the eager boot loop, the lazy `loadShardFileSync`, and the boot harvest) plus the repro above. Behavior is identical with and without `MARINARA_EAGER_STORAGE=1`, confirming it predates the lazy-loading work.
bsakaguchi 2026-10-21 05:59:38 +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#3104
No description provided.