[PR #4276] [MERGED] Store generated gallery images once and cascade deletion #6314

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/4276
Author: @SpicyMarinara
Created: 7/29/2026
Status: ✅ Merged
Merged: 7/30/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/gallery-delete-cascade-4275


📝 Commits (6)

  • 9f1b23d link generated gallery copies to chat images
  • 864ca25 cascade chat gallery image deletion
  • c0c70b2 store generated gallery images once
  • e89fbab address gallery lifecycle review feedback
  • 7b09c95 reuse gallery path lifecycle resolver
  • 0f931e5 Merge remote-tracking branch 'origin/staging' into fix/gallery-delete-cascade-4275

📊 Changes

19 files changed (+1014 additions, -200 deletions)

View changed files

📝 package.json (+1 -0)
📝 packages/server/src/db/schema/gallery.ts (+4 -0)
📝 packages/server/src/routes/characters.routes.ts (+52 -28)
📝 packages/server/src/routes/gallery.routes.ts (+34 -33)
📝 packages/server/src/routes/generate.routes.ts (+4 -1)
📝 packages/server/src/routes/generate/retry-agents-route.ts (+2 -1)
📝 packages/server/src/routes/global-gallery.routes.ts (+2 -10)
📝 packages/server/src/services/generation/conversation-selfie-command-runtime.ts (+2 -1)
➕ packages/server/src/services/image/chat-gallery-cascade-deletion.ts (+205 -0)
➕ packages/server/src/services/image/gallery-file-lifecycle.ts (+176 -0)
📝 packages/server/src/services/image/generated-image-entity-gallery.ts (+57 -71)
📝 packages/server/src/services/image/image-generation.ts (+18 -4)
📝 packages/server/src/services/mari-db/mari-images.service.ts (+52 -21)
📝 packages/server/src/services/noodle/noodle-vision.ts (+11 -22)
📝 packages/server/src/services/storage/character-gallery.storage.ts (+40 -1)
📝 packages/server/src/services/storage/chats.storage.ts (+26 -6)
📝 packages/server/src/services/storage/gallery.storage.ts (+4 -0)
📝 packages/server/src/services/storage/persona-gallery.storage.ts (+40 -1)
➕ scripts/regressions/gallery-cascade-deletion.regression.ts (+284 -0)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote the staging branch or a same-repository hotfix/* branch to main.

Linked issue

Closes #4275

Why this change

  • A generated image shown in the chat, character, and persona galleries was physically copied into every gallery. Large images could therefore consume three or more times their actual size.
  • Those independent copies had no durable provenance, so explicitly deleting the originating chat-gallery image left stale entity-gallery files and metadata behind.
  • Gallery membership is a metadata concern; it should not require duplicating the image bytes.

What changed

  • Store multi-gallery generated images once under a canonical gallery/shared/ path.
  • Point the chat, depicted-character, and persona gallery rows at that same file while retaining source chat-image provenance.
  • Resolve the stored metadata path when serving chat, character, persona, card asset, Noodle vision, and Professor Mari image URLs, while preserving legacy owner-folder paths.
  • Treat gallery deletion as releasing a reference. The physical file is unlinked only after the final chat, character, persona, or global-gallery reference is gone.
  • When a chat or entity is deleted wholesale, keep a shared file if another gallery still references it and clean it once the final reference disappears.
  • When the originating chat-gallery item is explicitly deleted, remove its generated entity-gallery references as well.
  • Detect pre-fix 2.3.5 physical copies conservatively: metadata must match, creation times must be within 60 seconds, and file bytes must have the same SHA-256 digest.
  • Reject unsafe stored paths and never unlink outside the gallery root.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

Automated validation completed:

  • pnpm check
  • pnpm --filter @marinara-engine/server lint
  • pnpm regression:gallery-delete
  • pnpm regression:noodle

The focused gallery regression proves that chat, two character galleries, and two persona galleries all reference one physical file; deleting individual entity-gallery references preserves it; deleting the final reference removes it; and explicit source deletion removes linked metadata plus exact legacy copies. Negative controls preserve different metadata, different bytes, old creation timestamps, copies linked to another source image, and files outside the gallery root.

pnpm regression:issues currently stops before this feature area on the pre-existing stale expectation at scripts/regressions/open-issues.regression.ts:330: the runtime includes favorites: [] while the assertion expects only folders and assignments. This PR does not modify that fixture.

Manual verification still needed:

  • Generate an image involving a character and persona, then confirm it appears in all relevant galleries while only one file exists under data/gallery/shared/.
  • Delete it from one entity gallery and confirm the chat/other gallery views still render it.
  • Delete the originating chat-gallery item and confirm all generated gallery references disappear and the shared file is removed.
  • Delete a chat while retaining a character/persona gallery reference and confirm the shared image remains available there.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

Not applicable; this is a server storage lifecycle fix.

Summary by CodeRabbit

  • New Features

    • Gallery images can now be shared across chat, character, and persona galleries without duplicating files on disk.
    • Gallery asset URLs are served using stored image metadata to improve reliable resolution between shared and owned locations.
  • Bug Fixes

    • Gallery deletions now remove only unreferenced physical files, preserving any remaining shared references.
    • Chat and entity removals no longer risk deleting physical gallery files that are still in use elsewhere.
    • Stronger protection during cleanup/serving prevents invalid or unsafe paths from being acted on.

🔄 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/4276 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 7/29/2026 **Status:** ✅ Merged **Merged:** 7/30/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/gallery-delete-cascade-4275` --- ### 📝 Commits (6) - [`9f1b23d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9f1b23d6b3230646db7507068cadfe802310f819) link generated gallery copies to chat images - [`864ca25`](https://github.com/Pasta-Devs/Marinara-Engine/commit/864ca25621ae2ac137cda70ff5e281016b64efd2) cascade chat gallery image deletion - [`c0c70b2`](https://github.com/Pasta-Devs/Marinara-Engine/commit/c0c70b265915ad70131fd66a3efc563bf6bebb3e) store generated gallery images once - [`e89fbab`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e89fbabbcbe02262469ade47b200b13a7a09228d) address gallery lifecycle review feedback - [`7b09c95`](https://github.com/Pasta-Devs/Marinara-Engine/commit/7b09c95e59e1cc095922cb3fad1783ab0ddca7ef) reuse gallery path lifecycle resolver - [`0f931e5`](https://github.com/Pasta-Devs/Marinara-Engine/commit/0f931e55e105baafb7b469d94d91d22525c017f1) Merge remote-tracking branch 'origin/staging' into fix/gallery-delete-cascade-4275 ### 📊 Changes **19 files changed** (+1014 additions, -200 deletions) <details> <summary>View changed files</summary> 📝 `package.json` (+1 -0) 📝 `packages/server/src/db/schema/gallery.ts` (+4 -0) 📝 `packages/server/src/routes/characters.routes.ts` (+52 -28) 📝 `packages/server/src/routes/gallery.routes.ts` (+34 -33) 📝 `packages/server/src/routes/generate.routes.ts` (+4 -1) 📝 `packages/server/src/routes/generate/retry-agents-route.ts` (+2 -1) 📝 `packages/server/src/routes/global-gallery.routes.ts` (+2 -10) 📝 `packages/server/src/services/generation/conversation-selfie-command-runtime.ts` (+2 -1) ➕ `packages/server/src/services/image/chat-gallery-cascade-deletion.ts` (+205 -0) ➕ `packages/server/src/services/image/gallery-file-lifecycle.ts` (+176 -0) 📝 `packages/server/src/services/image/generated-image-entity-gallery.ts` (+57 -71) 📝 `packages/server/src/services/image/image-generation.ts` (+18 -4) 📝 `packages/server/src/services/mari-db/mari-images.service.ts` (+52 -21) 📝 `packages/server/src/services/noodle/noodle-vision.ts` (+11 -22) 📝 `packages/server/src/services/storage/character-gallery.storage.ts` (+40 -1) 📝 `packages/server/src/services/storage/chats.storage.ts` (+26 -6) 📝 `packages/server/src/services/storage/gallery.storage.ts` (+4 -0) 📝 `packages/server/src/services/storage/persona-gallery.storage.ts` (+40 -1) ➕ `scripts/regressions/gallery-cascade-deletion.regression.ts` (+284 -0) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote the `staging` branch or a same-repository `hotfix/*` branch to `main`. ## Linked issue Closes #4275 ## Why this change - A generated image shown in the chat, character, and persona galleries was physically copied into every gallery. Large images could therefore consume three or more times their actual size. - Those independent copies had no durable provenance, so explicitly deleting the originating chat-gallery image left stale entity-gallery files and metadata behind. - Gallery membership is a metadata concern; it should not require duplicating the image bytes. ## What changed - Store multi-gallery generated images once under a canonical `gallery/shared/` path. - Point the chat, depicted-character, and persona gallery rows at that same file while retaining source chat-image provenance. - Resolve the stored metadata path when serving chat, character, persona, card asset, Noodle vision, and Professor Mari image URLs, while preserving legacy owner-folder paths. - Treat gallery deletion as releasing a reference. The physical file is unlinked only after the final chat, character, persona, or global-gallery reference is gone. - When a chat or entity is deleted wholesale, keep a shared file if another gallery still references it and clean it once the final reference disappears. - When the originating chat-gallery item is explicitly deleted, remove its generated entity-gallery references as well. - Detect pre-fix 2.3.5 physical copies conservatively: metadata must match, creation times must be within 60 seconds, and file bytes must have the same SHA-256 digest. - Reject unsafe stored paths and never unlink outside the gallery root. ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` ### Manual verification notes Automated validation completed: - `pnpm check` - `pnpm --filter @marinara-engine/server lint` - `pnpm regression:gallery-delete` - `pnpm regression:noodle` The focused gallery regression proves that chat, two character galleries, and two persona galleries all reference one physical file; deleting individual entity-gallery references preserves it; deleting the final reference removes it; and explicit source deletion removes linked metadata plus exact legacy copies. Negative controls preserve different metadata, different bytes, old creation timestamps, copies linked to another source image, and files outside the gallery root. `pnpm regression:issues` currently stops before this feature area on the pre-existing stale expectation at `scripts/regressions/open-issues.regression.ts:330`: the runtime includes `favorites: []` while the assertion expects only `folders` and `assignments`. This PR does not modify that fixture. Manual verification still needed: - Generate an image involving a character and persona, then confirm it appears in all relevant galleries while only one file exists under `data/gallery/shared/`. - Delete it from one entity gallery and confirm the chat/other gallery views still render it. - Delete the originating chat-gallery item and confirm all generated gallery references disappear and the shared file is removed. - Delete a chat while retaining a character/persona gallery reference and confirm the shared image remains available there. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) Not applicable; this is a server storage lifecycle fix. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Gallery images can now be shared across chat, character, and persona galleries without duplicating files on disk. * Gallery asset URLs are served using stored image metadata to improve reliable resolution between shared and owned locations. * **Bug Fixes** * Gallery deletions now remove only unreferenced physical files, preserving any remaining shared references. * Chat and entity removals no longer risk deleting physical gallery files that are still in use elsewhere. * Stronger protection during cleanup/serving prevents invalid or unsafe paths from being acted on. <!-- 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:09:14 +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#6314
No description provided.