[GH-ISSUE #5613] Deleting any gallery image makes the chat_images table permanently resident #3108

Closed
opened 2026-10-21 05:59:41 +07:00 by bsakaguchi · 1 comment
Owner

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

Originally assigned to: @kolacheee on GitHub.

What happens

galleryFileHasReferences (packages/server/src/services/image/gallery-file-lifecycle.ts:88-93) asks "does any chat still reference this physical file?" with eq(chatImages.filePath, filePath). Because filePath is neither the shard column nor the primary key, the lazy store cannot scope the query, so the first call permanently converts chat_images to fully resident (Lazy table chat_images is now fully resident in the log).

It is called from every image-deletion path in the app — chat gallery, character/persona images, global gallery, cascade deletion (chats.storage.ts:884/:892/:931/:944, characters.routes.ts ×6, global-gallery.routes.ts:270, chat-gallery-cascade-deletion.ts:195, mari-images.service.ts:1364). So on any install where the user has ever deleted an image, the chat_images table ignores the memory cap for the rest of the process.

Why a simple scoping fix doesn't work

Unlike the #5611 sites, this one is cross-chat by design: shared files must only be unlinked when the last referencing chat lets go. Adding a chatId conjunct would make the check wrong and delete files other chats still use.

Possible directions

  • Maintain a small filePath → reference count (or filePath → owner set) index, updated on chat_images insert/remove, so the liveness check never needs a table scan.
  • Or answer the question from the per-chat shard files on disk without loading them into the resident set.

Found during the residual-lease audit for #5611 / #5592.

Originally created by @kolacheee on GitHub (Aug 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/5613 Originally assigned to: @kolacheee on GitHub. **What happens** `galleryFileHasReferences` (`packages/server/src/services/image/gallery-file-lifecycle.ts:88-93`) asks "does **any** chat still reference this physical file?" with `eq(chatImages.filePath, filePath)`. Because `filePath` is neither the shard column nor the primary key, the lazy store cannot scope the query, so the first call permanently converts `chat_images` to fully resident (`Lazy table chat_images is now fully resident` in the log). It is called from every image-deletion path in the app — chat gallery, character/persona images, global gallery, cascade deletion (`chats.storage.ts:884/:892/:931/:944`, `characters.routes.ts` ×6, `global-gallery.routes.ts:270`, `chat-gallery-cascade-deletion.ts:195`, `mari-images.service.ts:1364`). So on any install where the user has ever deleted an image, the `chat_images` table ignores the memory cap for the rest of the process. **Why a simple scoping fix doesn't work** Unlike the #5611 sites, this one is cross-chat *by design*: shared files must only be unlinked when the last referencing chat lets go. Adding a chatId conjunct would make the check wrong and delete files other chats still use. **Possible directions** - Maintain a small `filePath → reference count` (or `filePath → owner set`) index, updated on chat_images insert/remove, so the liveness check never needs a table scan. - Or answer the question from the per-chat shard files on disk without loading them into the resident set. Found during the residual-lease audit for #5611 / #5592.
bsakaguchi 2026-10-21 05:59:41 +07:00
Author
Owner

@kolacheee commented on GitHub (Aug 29, 2026):

Taking this one (kolacheee, agent-assisted) — going with the shard-peek direction now that #5616 established the pattern: answer the cross-chat reference question from resident memory plus direct shard-file reads, loading a unit only when a shard can't be trusted. PR incoming.

<!-- gh-comment-id:5459481514 --> @kolacheee commented on GitHub (Aug 29, 2026): Taking this one (kolacheee, agent-assisted) — going with the shard-peek direction now that #5616 established the pattern: answer the cross-chat reference question from resident memory plus direct shard-file reads, loading a unit only when a shard can't be trusted. PR incoming.
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#3108
No description provided.