mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #5616] [MERGED] fix(storage): stop boot gallery recovery from loading every chat with images #6850
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#6850
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5616
Author: @kolacheee
Created: 8/29/2026
Status: ✅ Merged
Merged: 8/29/2026
Merged by: @kolacheee
Base:
staging← Head:fix/5612-gallery-recovery-boot-walk📝 Commits (4)
f7267e7fix(storage): stop boot gallery recovery from loading every chat with imagese129e0afix(storage): harden the gallery-recovery peek per adversarial reviewa6ff745style: format the gallery-recovery regression7ec2c14fix(storage): hand malformed shard rows to the loader so repair can run📊 Changes
3 files changed (+391 additions, -16 deletions)
View changed files
📝
packages/server/src/db/file-backed-store.ts(+30 -0)📝
packages/server/src/services/storage/gallery-recovery.ts(+111 -16)➕
scripts/regressions/gallery-recovery.regression.ts(+250 -0)📄 Description
Closes #5612. Part of the #5592 close-out (follows #5597/#5603; sibling of #5614).
Why
The startup gallery-recovery scan (re-registering orphaned image files) queried
chat_imagesonce perdata/gallery/<chatId>directory. Every query was correctly chatId-scoped — which is exactly why no query audit ever flagged it — but any row read pulls that chat's entire storage unit (all 17 lazy tables' shards) into memory. On an install where most chats have generated images, boot walked nearly every chat unit into memory before the server even started listening, quietly reproducing the eager-boot footprint #5592 removed, and leaving eviction to claw it back down afterward.What changed
For a chat whose unit is not resident, the scan now reads the chat's
chat_imagesshard file straight off disk instead of going through the store. This is sound because a non-resident unit can hold no unflushed state: writes force the unit resident first, and eviction only runs at the tail of a successful flush and skips units with pending state — so an evicted unit's shard file is always the current state.The peek is deliberately conservative: anything it cannot fully trust (unreadable file, valid-JSON-but-non-array root, a lone
.bakfrom an interrupted flush) falls back to the normal scoped query, so the store's full recovery ladder stays in charge of corruption — and only that one chat's unit loads. Same for a genuine orphan: the insert goes through the normal path, loading just that chat.Net effect at boot:
chatslookup + one shard-file readchat_imagesshardMARINARA_EAGER_STORAGEAlso exports
isLazyUnitTable()and agetFullyResidentLazyTables()controller accessor from the store (the peek must not run against an eager, whole-table-leased, or otherwise fully-resident table, where memory — not disk — is the truth; the accessor is line-identical to the one #5614 adds, so whichever merges second rebases cleanly).GALLERY_DIRmoved from a module-load constant to a call-timegetDataDir()so the function honors the environment it runs in.Regression
New suite
gallery-recovery.regression.ts(auto-discovered by the runner): six chats covering fully-recorded (must load nothing — the headline assert), orphaned file (loads exactly that unit, preserves the recorded row), corrupt shard with valid.bak(loader handoff recovers the row, no duplicate insert), no shard at all (recovers, shard created on flush), empty directory and deleted-chat directory (no storage access), untouched shards byte-identical after flush, and an idempotent second run. A/B-verified: red against the pre-fix scan (fully-recorded chats' units load), green with the fix. Self-skips underMARINARA_EAGER_STORAGElike the sibling lazy suite.An adversarial verification pass (three independent reviewers on the residency invariant, peek-vs-load semantics, and regression validity) ran before opening this PR and caught five real problems in the first draft, all fixed and now pinned by the suite:
chat_id/file_path) are a loader-accepted on-disk shape the peek read as "no rows" — every recorded image in such a shard would have been re-registered as a duplicate. The peek now hands any non-canonical row shape to the loader (chat-g fixture).chat_imageswere ever fully leased, memory (with unflushed writes) is the truth, not the files — the peek now checksgetFullyResidentLazyTables()first. Not reachable in today's boot (verified: nothing before the scan toucheschat_images), fixed defensively.DATA_DIRat import time, so it scanned a nonexistent default dir instead of the fixture. The suite now uses dynamic imports after the env assignments; the re-run fails pre-fix on exactly the headline "no unit loads" assert.One accepted limitation, documented in-code: a row misfiled into another chat's shard file is invisible to the peek until its host unit loads, so its image can be re-registered as a duplicate. This matches lazy scoped-query semantics generally — the pre-fix scan only avoided it by loading every unit, which is the exact behavior being removed. The reviewers also confirmed the load-bearing invariants clean by execution: eviction only at the successful-flush tail, no write path that leaves a non-resident unit dirty, transaction rollback re-pairing, and byte-identical path/key derivation between the peek and the store.
Validation done
tsc --noEmitall packages ✅--check --end-of-line auto) on the three changed files ✅ (repo-widepnpm checkprettier step fails on this Windows box for all files — #5598, pre-existing)gallery-recovery(lazy + eager-skip) ✅,lazy-chat-units✅,message-sharding✅Manual verification
.pngintodata/gallery/<some-chat>/, restart, and confirm it appears in that chat's gallery ([gallery-recovery] Recovered 1 orphaned gallery image(s)in the log)MARINARA_MAX_RESIDENT_CHATSset), watch boot memory: it should no longer climb toward the old eager-boot level during startup on image-heavy installsSummary by CodeRabbit
Bug Fixes
Diagnostics
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.