mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #5620] [MERGED] fix(storage): answer the gallery reference check without leasing chat_images #6853
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#6853
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/5620
Author: @kolacheee
Created: 8/29/2026
Status: ✅ Merged
Merged: 8/29/2026
Merged by: @kolacheee
Base:
staging← Head:fix/5613-reference-check-without-lease📝 Commits (6)
c1f11f5fix(storage): answer the gallery reference check without leasing chat_images2a934cdstyle: format the gallery-file-lifecycle regression1afac0cdocs: add changelog entries for the residual-lease fixes (#5611/#5612/#5613)7488bd7fix(storage): close the reference-check gaps found by adversarial review2c452c9refactor(storage): delete the now-dead unscoped listByFilePath5524fc7fix(storage): gate negative reference answers on stable residency📊 Changes
5 files changed (+580 additions, -14 deletions)
View changed files
📝
CHANGELOG.md(+3 -0)📝
packages/server/src/db/file-backed-store.ts(+53 -2)📝
packages/server/src/services/image/gallery-file-lifecycle.ts(+237 -8)📝
packages/server/src/services/storage/gallery.storage.ts(+0 -4)➕
scripts/regressions/gallery-file-lifecycle.regression.ts(+287 -0)📄 Description
Closes #5613. The last of the three residual-lease findings from the #5592 close-out audit (#5611 → #5614, #5612 → #5616).
Why
galleryFileHasReferencesasks a question that is cross-chat by design — "does any chat still reference this physical file before we unlink it?" — so itschat_imagesquery could not carry a chatId and permanently converted the whole table to fully resident on the first image deletion anywhere in the app (chat gallery, character/persona images, global gallery, cascade deletion). On any install where the user had ever deleted an image,MARINARA_MAX_RESIDENT_CHATSsilently stopped boundingchat_imagesfor the rest of the process. A conjunct can't fix this one — scoping the query to one chat would delete files other chats still show.What changed
The
chat_imageshalf of the check (the other three tables are eager and unchanged) now answers in layers, guided by the failure asymmetry — a false "referenced" merely keeps an orphan file on disk, while a false "unreferenced" would delete a file another chat still displays:filePath(or dbName-formfile_path) matches is a reference, regardless of which chat owns it — so cross-chat references, stray rows included, are found without loading anyone. Peek results are memoized across calls keyed on the table's write generation plus the resident set (same generation + same residency ⇒ no shard file can have changed), because the deletion flows call this once or twice per image and would otherwise pay a full synchronous shard-directory read each time..bakfrom an interrupted flush) is handed to the real loader by key — decoded from the filename when the decode round-trips through the encoder, or attributed by matching the encoding over the eagerchatstable when the filename is the undecodable%hhash form — loading exactly that unit through the full recovery ladder and short-circuiting on a hit.decodeShardKeyis the new best-effort inverse of the filename encoding (the store itself never decodes — rows carry their own keys).getResidentLazyRows()accessor — not per-unit queries. This is what makes memory authoritative in both directions (an unflushed new row counts; an unflushed delete does not resurrect through its stale shard file — load-bearing, since the delete flow checks within the flush debounce), covers rows no per-key query can express (null/absent/non-stringchatIdrows living in the pinned orphan unit), and, running last, makes mid-call interleavings safe: anything a concurrent request writes during this function's awaits lands in memory and is seen.Eager mode and an already-leased table keep the original plain query, which is complete there and leases nothing new. The scan is sorted for cross-filesystem determinism and classifies directory entries with the store's own
isShardDataFileName, so artifact files the store ignores can't disable cleanup.Housekeeping: #5614 merged under this branch during review, so
gallery.storage.ts's now-dead unscopedlistByFilePathis deleted here too — the final unscopedchat_imagesquery in the storage layer.Regression
New suite
gallery-file-lifecycle.regression.ts: a cross-chat reference found by peek with zero unit loads and zero lease (the headline — A/B-verified red pre-fix on exactly the lease assert), dbName-form rows, corrupt-shard and lone-.bakloader handoffs (each loading only its own unit), a malformed-row shard answering correctly while its repair handoff runs, a full-sweep miss loading nothing new, both memory-authority directions (unflushed insert counts / unflushed delete doesn't resurrect), the realunlinkGalleryFileIfUnreferencedflow deleting an unreferenced file and keeping a referenced one, and a control block proving the check still answers correctly when the table was leased by some other path. Self-skips underMARINARA_EAGER_STORAGE.An adversarial verification pass (three independent reviewers: false-negative construction, decode/scan mechanics, caller integration + suite validity) ran before opening this PR and caught six real problems in the first draft — including a reproduced false negative in the catastrophic direction (orphan-unit rows with null/absent/non-string
chatIdwere invisible to the per-unit query loop, and their files were physically unlinked while live rows referenced them). All six are fixed and pinned by the suite; thegetResidentLazyRowsmemory scan is the structural fix for the worst one. The reviewers also fuzz-testeddecodeShardKeyround-trip fidelity (10,047 keys: exact for every well-formed id; the only lossy classes are encoder-side and pre-existing) and verified the load-bearing store invariants by execution. Full findings summarized in a comment below.Validation done
tsc --noEmitall packages ✅gallery-file-lifecycle(lazy + eager-skip) ✅,gallery-recovery✅,lazy-chat-units✅,message-sharding✅Manual verification
MARINARA_MAX_RESIDENT_CHATSset and confirm the log never printsLazy table chat_images is now fully residentdata/gallery/Summary by CodeRabbit
Bug Fixes
Tests
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.