[GH-ISSUE #2329] [Issue]: NPC avatar files leak on re-upload and are never cleaned on chat delete #1155

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

Originally created by @Romuromylus on GitHub (Jun 5, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2329

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: Low — Disk leak. NPC avatar files leak in two ways: update_npc_avatar writes a new file on every regeneration without removing the prior one, and delete_chat_with_messages never cleans a chat's avatars/npc/<chatId>-* files. The character-avatar path deliberately removes the old file, so this is an inconsistent omission.

Expected behavior

Re-uploading an NPC avatar should remove the prior file for the same chat+name; deleting a chat should remove its NPC avatar files — mirroring update_character_avatar_inner, which fetches the previous record and removes the old file after writing the new one.

Actual behavior

src-tauri/src/commands/storage/avatars.rs:569-590 (update_npc_avatar):

let stored = persist_image_upload(state, "avatars/npc", &format!("{chat_id}-{name}"), &body, "avatar")?; // prior file never removed; chat delete never cleans avatars/npc

persist_image_upload (media_uploads.rs:30-38) builds a timestamped filename and runs it through unique_file_path (media_uploads.rs:363-383), which returns a non-colliding -N suffixed path when the target exists — so it never overwrites; every regen orphans the prior file. Contrast update_character_avatar_inner (avatars.rs:33/83), which removes the previous file.

delete_chat_with_messages (chats.rs:1120-1166) cleans tracker snapshots, gallery, and message rows but never scans/removes avatars/npc/* for the deleted chat ids. NPC avatar paths live inline in metadata.gameNpcs/gameState.presentCharacters, not as tracked top-level record fields, so there is no managed-file cleanup hook — all NPC avatars of a deleted game chat orphan permanently.

Steps to reproduce

  1. In game mode, regenerate an NPC's avatar several times → old files pile up; or
  2. Delete a game chat that had NPC avatars → all its avatars/npc files orphan.

Impact area

  • Game mode
  • Tauri/Rust storage, assets, LLM, integrations, security, imports, or HTTP runtime

Environment

  • Branch or commit: refactor @ b0e5561d
  • Run mode: Tauri desktop (also server mode)
  • OS + version: not relevant (static source audit)
  • Node + pnpm versions: not relevant
  • Rust version, if relevant: not relevant
  • Remote Runtime URL configured? not relevant (applies to both)
  • Provider, model, or integration, if relevant: game-mode NPC portraits

Logs, screenshots, or video

Static source audit; cited to file:line above.

Regression notes

Found via static audit of the refactor branch; not verified against main. Pure disk-space accumulation scaling with NPC-portrait churn, hence low.

Additional context

Suggested fix: in update_npc_avatar, look up the prior NPC avatar path for the same chatId+name and remove it after the new write (mirror remove_avatar_file_preserving_persona_snapshots). In delete_chat_with_messages, best-effort scan avatars/npc/ and remove files whose name starts with each deleted {chat_id}- prefix.

Template check

Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 5, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2329 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: Low — Disk leak.** NPC avatar files leak in two ways: `update_npc_avatar` writes a new file on every regeneration without removing the prior one, and `delete_chat_with_messages` never cleans a chat's `avatars/npc/<chatId>-*` files. The character-avatar path deliberately removes the old file, so this is an inconsistent omission. ## Expected behavior Re-uploading an NPC avatar should remove the prior file for the same chat+name; deleting a chat should remove its NPC avatar files — mirroring `update_character_avatar_inner`, which fetches the previous record and removes the old file after writing the new one. ## Actual behavior `src-tauri/src/commands/storage/avatars.rs:569-590` (`update_npc_avatar`): ```rust let stored = persist_image_upload(state, "avatars/npc", &format!("{chat_id}-{name}"), &body, "avatar")?; // prior file never removed; chat delete never cleans avatars/npc ``` `persist_image_upload` (`media_uploads.rs:30-38`) builds a timestamped filename and runs it through `unique_file_path` (`media_uploads.rs:363-383`), which returns a non-colliding `-N` suffixed path when the target exists — so it never overwrites; every regen orphans the prior file. Contrast `update_character_avatar_inner` (`avatars.rs:33`/`83`), which removes the previous file. `delete_chat_with_messages` (`chats.rs:1120-1166`) cleans tracker snapshots, gallery, and message rows but never scans/removes `avatars/npc/*` for the deleted chat ids. NPC avatar paths live inline in `metadata.gameNpcs`/`gameState.presentCharacters`, not as tracked top-level record fields, so there is no managed-file cleanup hook — all NPC avatars of a deleted game chat orphan permanently. ## Steps to reproduce 1. In game mode, regenerate an NPC's avatar several times → old files pile up; or 2. Delete a game chat that had NPC avatars → all its `avatars/npc` files orphan. ## Impact area - [x] Game mode - [x] Tauri/Rust storage, assets, LLM, integrations, security, imports, or HTTP runtime ## Environment - Branch or commit: refactor @ b0e5561d - Run mode: Tauri desktop (also server mode) - OS + version: not relevant (static source audit) - Node + pnpm versions: not relevant - Rust version, if relevant: not relevant - Remote Runtime URL configured? not relevant (applies to both) - Provider, model, or integration, if relevant: game-mode NPC portraits ## Logs, screenshots, or video Static source audit; cited to file:line above. ## Regression notes Found via static audit of the refactor branch; not verified against main. Pure disk-space accumulation scaling with NPC-portrait churn, hence low. ## Additional context Suggested fix: in `update_npc_avatar`, look up the prior NPC avatar path for the same chatId+name and remove it after the new write (mirror `remove_avatar_file_preserving_persona_snapshots`). In `delete_chat_with_messages`, best-effort scan `avatars/npc/` and remove files whose name starts with each deleted `{chat_id}-` prefix. ## Template check Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:28:04 +07:00
Author
Owner

@Romuromylus commented on GitHub (Jun 6, 2026):

Fixed by #2402, merged into refactor.

<!-- gh-comment-id:4637948547 --> @Romuromylus commented on GitHub (Jun 6, 2026): Fixed by #2402, merged into `refactor`.
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#1155
No description provided.