[PR #4669] [MERGED] fix: clean up deleted avatars and retain summary history #6461

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/4669
Author: @SpicyMarinara
Created: 8/6/2026
Status: ✅ Merged
Merged: 8/6/2026
Merged by: @SpicyMarinara

Base: staging ← Head: agent/4668-avatar-file-cleanup


📝 Commits (6)

  • 2f72f9c chore: start issue 4668 avatar cleanup
  • 5b77673 fix avatar cleanup after card deletion
  • 334b236 address avatar review and retain summary history
  • e0c5da1 fix: address avatar upload and summary review feedback
  • a1c62ea fix: make avatar persistence atomic
  • 57fa69f fix: snapshot current persona atomically

📊 Changes

10 files changed (+823 additions, -178 deletions)

View changed files

📝 CHANGELOG.md (+2 -0)
📝 package.json (+2 -1)
📝 packages/server/src/routes/admin.routes.ts (+49 -6)
📝 packages/server/src/routes/characters.routes.ts (+31 -6)
📝 packages/server/src/routes/chats.routes.ts (+2 -4)
➕ packages/server/src/services/image/avatar-file-lifecycle.ts (+184 -0)
📝 packages/server/src/services/storage/characters.storage.ts (+211 -159)
📝 packages/shared/src/utils/chat-summary-entries.ts (+14 -0)
➕ scripts/regressions/avatar-file-deletion.regression.ts (+251 -0)
📝 scripts/regressions/open-issues.regression.ts (+77 -2)

📄 Description

Linked issue

Closes #4668

Why this change

  • Character and Persona deletion removed database rows but never released their files under data/avatars. Danger Zone used the same database-only deletion path, and card-version rows meant one deleted card could leave several historical avatar files behind.
  • In Roleplay mode, combining Chat Summaries discarded the selected source entries, preventing users from viewing or reactivating the original summaries later.

What changed

  • Capture current and card-version avatar paths before individual or Danger Zone Character/Persona deletion, then unlink each local file after its final database reference disappears.
  • Preserve Professor Mari and files still referenced by another card, card version, or Character group; clean up deleted Character-group avatar references during Danger Zone Character deletion.
  • Accept only the local /api/avatars/file/<filename> route for deletion; external URLs, NPC paths, invalid encodings, nested paths, and traversal paths are ignored.
  • Serialize avatar reference writes and final-reference cleanup with one process-wide lifecycle lock, preventing delete-versus-duplicate races from leaving broken references.
  • Keep source Chat Summary entries after combining them, mark them inactive, and insert the combined summary at their former position.
  • Add deterministic regression coverage and Unreleased changelog entries for both behavior fixes.

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: pnpm regression:avatar-delete passed after the review changes.
  • Automated: pnpm check passed (localization, lint/type checks, server/client production builds).
  • Automated: the executable Chat Summary history regression passed independently, covering inactive retained sources, updated timestamps, insertion order, untouched entries, and compiled output.
  • The broader open-issues.regression.ts currently stops earlier on an unrelated pre-existing mobile-keyboard source-distance assertion on staging; the new runtime behavior was verified independently.
  • No browser verification performed; both changes are server-side persistence/filesystem behavior.

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 (see CONTRIBUTING.md § Translated documentation)

  • Version/release files updated (only if this PR includes a version bump)

  • Added two Unreleased changelog entries. No user-facing documentation, translation, or version metadata changes are needed.

UI evidence (if applicable)

  • Not applicable; no UI layout or copy changed.

Summary by CodeRabbit

  • Bug Fixes

    • Local Character and Persona avatar files are now automatically removed when their cards are deleted.
    • Avatars retained only by deleted card-version history are also cleaned up.
    • Shared avatars remain protected until no remaining cards or groups reference them.
    • External, invalid, missing, and protected avatar files are handled safely.
    • Combined Chat Summary entries are now preserved as inactive history instead of being deleted.
  • Tests

    • Added regression coverage for avatar cleanup, shared-file preservation, path safety, and inactive Chat Summary history.

🔄 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/4669 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 8/6/2026 **Status:** ✅ Merged **Merged:** 8/6/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `agent/4668-avatar-file-cleanup` --- ### 📝 Commits (6) - [`2f72f9c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2f72f9cb1dcdb959966bafc5f7459e4395e25285) chore: start issue 4668 avatar cleanup - [`5b77673`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5b776731c0730e596671fd8fbd3f109037a22563) fix avatar cleanup after card deletion - [`334b236`](https://github.com/Pasta-Devs/Marinara-Engine/commit/334b2366f4a92f75c0d5d54ce0be7c741f9b7b5b) address avatar review and retain summary history - [`e0c5da1`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e0c5da11fcbb5cf4135be4907cab34a8ff1f9722) fix: address avatar upload and summary review feedback - [`a1c62ea`](https://github.com/Pasta-Devs/Marinara-Engine/commit/a1c62eabcf2885432702713bec9bb7498e109e70) fix: make avatar persistence atomic - [`57fa69f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/57fa69f756628d37899acbd3a536980190d2fad8) fix: snapshot current persona atomically ### 📊 Changes **10 files changed** (+823 additions, -178 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+2 -0) 📝 `package.json` (+2 -1) 📝 `packages/server/src/routes/admin.routes.ts` (+49 -6) 📝 `packages/server/src/routes/characters.routes.ts` (+31 -6) 📝 `packages/server/src/routes/chats.routes.ts` (+2 -4) ➕ `packages/server/src/services/image/avatar-file-lifecycle.ts` (+184 -0) 📝 `packages/server/src/services/storage/characters.storage.ts` (+211 -159) 📝 `packages/shared/src/utils/chat-summary-entries.ts` (+14 -0) ➕ `scripts/regressions/avatar-file-deletion.regression.ts` (+251 -0) 📝 `scripts/regressions/open-issues.regression.ts` (+77 -2) </details> ### 📄 Description ## Linked issue Closes #4668 ## Why this change - Character and Persona deletion removed database rows but never released their files under `data/avatars`. Danger Zone used the same database-only deletion path, and card-version rows meant one deleted card could leave several historical avatar files behind. - In Roleplay mode, combining Chat Summaries discarded the selected source entries, preventing users from viewing or reactivating the original summaries later. ## What changed - Capture current and card-version avatar paths before individual or Danger Zone Character/Persona deletion, then unlink each local file after its final database reference disappears. - Preserve Professor Mari and files still referenced by another card, card version, or Character group; clean up deleted Character-group avatar references during Danger Zone Character deletion. - Accept only the local `/api/avatars/file/<filename>` route for deletion; external URLs, NPC paths, invalid encodings, nested paths, and traversal paths are ignored. - Serialize avatar reference writes and final-reference cleanup with one process-wide lifecycle lock, preventing delete-versus-duplicate races from leaving broken references. - Keep source Chat Summary entries after combining them, mark them inactive, and insert the combined summary at their former position. - Add deterministic regression coverage and Unreleased changelog entries for both behavior fixes. ## 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: `pnpm regression:avatar-delete` passed after the review changes. - Automated: `pnpm check` passed (localization, lint/type checks, server/client production builds). - Automated: the executable Chat Summary history regression passed independently, covering inactive retained sources, updated timestamps, insertion order, untouched entries, and compiled output. - The broader `open-issues.regression.ts` currently stops earlier on an unrelated pre-existing mobile-keyboard source-distance assertion on `staging`; the new runtime behavior was verified independently. - No browser verification performed; both changes are server-side persistence/filesystem behavior. ## 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 (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) - Added two Unreleased changelog entries. No user-facing documentation, translation, or version metadata changes are needed. ## UI evidence (if applicable) - Not applicable; no UI layout or copy changed. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Local Character and Persona avatar files are now automatically removed when their cards are deleted. - Avatars retained only by deleted card-version history are also cleaned up. - Shared avatars remain protected until no remaining cards or groups reference them. - External, invalid, missing, and protected avatar files are handled safely. - Combined Chat Summary entries are now preserved as inactive history instead of being deleted. - **Tests** - Added regression coverage for avatar cleanup, shared-file preservation, path safety, and inactive Chat Summary history. <!-- 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:10:39 +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#6461
No description provided.