[PR #8130] [MERGED] fix(avatars): preserve frozen chat identity files during cleanup #8117

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/8130
Author: @SpicyMarinara
Created: 10/23/2026
Status: ✅ Merged
Merged: 10/23/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/frozen-avatar-gc


📝 Commits (7)

  • 753448e chore: track frozen avatar cleanup fix
  • b181f6c fix(avatars): preserve frozen identity history during cleanup
  • e92d694 fix(avatars): stop history scans after candidates are protected
  • 4607345 test(avatars): prove cleanup transaction prevents eviction
  • df361f2 docs(avatars): explain cleanup consistency boundaries
  • f1fb191 fix: scan frozen avatar history outside write gates
  • 8fb9503 Merge commit '1ffb916dfb' into fix/frozen-avatar-gc

📊 Changes

7 files changed (+1050 additions, -133 deletions)

View changed files

📝 packages/server/src/db/file-backed-store.ts (+12 -4)
📝 packages/server/src/routes/chats.routes.ts (+39 -29)
📝 packages/server/src/routes/generate.routes.ts (+32 -28)
📝 packages/server/src/services/image/avatar-file-lifecycle.ts (+204 -72)
➕ scripts/regressions/avatar-frozen-identity-cleanup.regression.ts (+431 -0)
➕ scripts/regressions/file-store-load-heal-generation.regression.mjs (+261 -0)
📝 scripts/regressions/persona-api-decoded-reads.regression.ts (+71 -0)

📄 Description

Linked issue

Closes #8112

Why this change

Avatar cleanup can delete a persona image that saved messages still use through frozen identities. Inactive swipes and restorable message trash can lose their images too. Cleanup must also respect pending historical-identity changes and rollback without blocking chat writes throughout a cold-history scan.

What changed

  • Protect frozen persona avatars in saved messages, every historical swipe, and restorable message-trash snapshots across Storage Optimization and final-reference deletion. Current card/version/group references retain their protection.
  • Capture committed resident candidate references and existing history generations in a short transaction, then inspect cold shards asynchronously outside both the storage write gate and avatar lifecycle lock. Keep only candidate paths and one parsed shard at a time; no chat warmup or full-table lease is introduced.
  • After cold I/O, acquire the lifecycle lock and final transaction, reread current card references, and check history generations before counting or unlinking. Concurrent history writes, rollback, or load-created repairs invalidate the scan and retain candidates for a later cleanup. Recheck before each unlink.
  • Extend the existing generation counter to cover lazy-load healing, canonical replacements, and actual quarantine moves. Clean reads do not invalidate snapshots; cleanup itself remains read-only and does not repair or quarantine history.
  • Keep identity resolution, historical reassignment, and their write/rollback protected by the lifecycle lock. Reference-deleting mutations release it after the mutation completes, before staged cleanup starts. Preserve avatar URL validation, the upload grace period, and reclamation of genuine orphans.

No schema migration, release metadata, dependency, user-facing documentation, or UI copy changes are needed.

Validation

Local macOS arm64 / Node 24.22.1:

  • pnpm install --frozen-lockfile and pnpm check passed before the staging refresh. After merging staging at 1ffb916df, frozen installation with the official pinned pnpm 12.15.0 and all twelve focused files passed again; the refreshed full check is recorded below.
  • Twelve focused regression files passed: frozen identity cleanup, existing avatar deletion, persona API decoded reads, character identity, persona dialogue, both message-trash suites, transaction isolation, lazy chat units, both gallery lifecycle/recovery suites, and the new load-heal generation fixture.
  • Controlled cold-read pauses prove all three cleanup entrypoints allow both unrelated DB writes and real lifecycle-lock/message writers to complete before the scan resumes. Writes to each history table, rollback, and newly added current-card references retain candidate files. A real flush evicts resident data during the pause while already captured frozen references remain protected.
  • The prior whole-scan implementation fails the new concurrency regression at scan must not hold the write gate during cold I/O; restored current source passes. The initial collector and historical-reassignment API had separate data-loss negative controls earlier in this PR.
  • Eight real-store load-generation cases cover clean reads, stray rehoming, canonical replacement, malformed/unreadable quarantine, nonempty/empty backup recovery, and staged GC reading C-before/A-after while a real load+flush moves the only frozen reference between those shards. Existing isolated counter-removal controls fail for each newly covered boundary.
  • Existing persona-reassignment API proof still passes: a failed last-reference replacement races cleanup, rolls back, retains the old PNG, and serves it successfully afterward.
  • The current caller/lock-order audit found no ambient transaction around the staged public cleanup entrypoints. Initial capture never requests the avatar lock; final work takes avatar lock then transaction. Mutation callbacks finish before cleanup releases/reacquires that lock.

Fresh full local CodeRabbit review completed with 0 findings across all seven PR files, including the load-heal counter fixture. It reviewed the complete Avatar diff against original base 957cb117; all seven reviewed files are SHA256-identical after merging current staging 1ffb916df. Final integration head: 8fb95039021cf9c3203c300d110e1a14a820bfd0. The full pnpm check also passed at that integration head under pinned pnpm 12.15.0 (one existing client hook-dependency warning, zero errors).

Earlier review findings and their source-based dispositions remain in the PR discussion. This revision replaces the previously accepted whole-scan write-gate tradeoff: cold history I/O now runs outside both gates. Earlier eviction assumptions no longer describe the design; captured resident references plus final generation validation are the safety boundary.

Manual verification

  • Manually replace/remove an unversioned persona avatar, run Storage Optimization, and verify old chat identities still display it.
  • Manually switch historical swipes and restore a deleted message after cleanup.
  • Manually reassign historical personas in a large chat while another request runs cleanup.
  • Build/run a container if container-specific confirmation is required.

Proof boundaries

The automated storage/API cases use disposable data and injected failures, not real user data or live providers. Prior desktop shell smoke passed 7/7; this revision changes only server storage/lifecycle behavior, so it does not add new browser or physical-device proof. Earlier hosted checks apply to their recorded heads; new-head CI and Dottore Review must pass before merge. One focused test initially hit another test's default-storage writer lease before its assertions; its unchanged isolated-runner replay passed. Cold scans can still take time and final checks/unlinks serialize briefly with writers; no latency SLA is claimed. Images already deleted by earlier cleanup need restoration from a backup.


🔄 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/8130 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/23/2026 **Status:** ✅ Merged **Merged:** 10/23/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/frozen-avatar-gc` --- ### 📝 Commits (7) - [`753448e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/753448ee4f55f9e46835231e2a389559bc58a88e) chore: track frozen avatar cleanup fix - [`b181f6c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b181f6c08554e36c6a57d8f5b33deafce6a5ebc0) fix(avatars): preserve frozen identity history during cleanup - [`e92d694`](https://github.com/Pasta-Devs/Marinara-Engine/commit/e92d6943ed7e627405a4fc6802dd00e2c9bd83c5) fix(avatars): stop history scans after candidates are protected - [`4607345`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4607345c42663c9a76c9001fa43c60e22457da5d) test(avatars): prove cleanup transaction prevents eviction - [`df361f2`](https://github.com/Pasta-Devs/Marinara-Engine/commit/df361f2f58c7b0be765ceacb87ef37009c1ebc1d) docs(avatars): explain cleanup consistency boundaries - [`f1fb191`](https://github.com/Pasta-Devs/Marinara-Engine/commit/f1fb19188c5f9d6003819aca6757f5ce91306c52) fix: scan frozen avatar history outside write gates - [`8fb9503`](https://github.com/Pasta-Devs/Marinara-Engine/commit/8fb95039021cf9c3203c300d110e1a14a820bfd0) Merge commit '1ffb916dfb32a3400fdca2fb65616d078fa6018e' into fix/frozen-avatar-gc ### 📊 Changes **7 files changed** (+1050 additions, -133 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/db/file-backed-store.ts` (+12 -4) 📝 `packages/server/src/routes/chats.routes.ts` (+39 -29) 📝 `packages/server/src/routes/generate.routes.ts` (+32 -28) 📝 `packages/server/src/services/image/avatar-file-lifecycle.ts` (+204 -72) ➕ `scripts/regressions/avatar-frozen-identity-cleanup.regression.ts` (+431 -0) ➕ `scripts/regressions/file-store-load-heal-generation.regression.mjs` (+261 -0) 📝 `scripts/regressions/persona-api-decoded-reads.regression.ts` (+71 -0) </details> ### 📄 Description ## Linked issue Closes #8112 ## Why this change Avatar cleanup can delete a persona image that saved messages still use through frozen identities. Inactive swipes and restorable message trash can lose their images too. Cleanup must also respect pending historical-identity changes and rollback without blocking chat writes throughout a cold-history scan. ## What changed - Protect frozen persona avatars in saved messages, every historical swipe, and restorable message-trash snapshots across Storage Optimization and final-reference deletion. Current card/version/group references retain their protection. - Capture committed resident candidate references and existing history generations in a short transaction, then inspect cold shards asynchronously outside both the storage write gate and avatar lifecycle lock. Keep only candidate paths and one parsed shard at a time; no chat warmup or full-table lease is introduced. - After cold I/O, acquire the lifecycle lock and final transaction, reread current card references, and check history generations before counting or unlinking. Concurrent history writes, rollback, or load-created repairs invalidate the scan and retain candidates for a later cleanup. Recheck before each unlink. - Extend the existing generation counter to cover lazy-load healing, canonical replacements, and actual quarantine moves. Clean reads do not invalidate snapshots; cleanup itself remains read-only and does not repair or quarantine history. - Keep identity resolution, historical reassignment, and their write/rollback protected by the lifecycle lock. Reference-deleting mutations release it after the mutation completes, before staged cleanup starts. Preserve avatar URL validation, the upload grace period, and reclamation of genuine orphans. No schema migration, release metadata, dependency, user-facing documentation, or UI copy changes are needed. ## Validation Local macOS arm64 / Node 24.22.1: - `pnpm install --frozen-lockfile` and `pnpm check` passed before the staging refresh. After merging `staging` at `1ffb916df`, frozen installation with the official pinned pnpm 12.15.0 and all twelve focused files passed again; the refreshed full check is recorded below. - Twelve focused regression files passed: frozen identity cleanup, existing avatar deletion, persona API decoded reads, character identity, persona dialogue, both message-trash suites, transaction isolation, lazy chat units, both gallery lifecycle/recovery suites, and the new load-heal generation fixture. - Controlled cold-read pauses prove all three cleanup entrypoints allow both unrelated DB writes and real lifecycle-lock/message writers to complete before the scan resumes. Writes to each history table, rollback, and newly added current-card references retain candidate files. A real flush evicts resident data during the pause while already captured frozen references remain protected. - The prior whole-scan implementation fails the new concurrency regression at `scan must not hold the write gate during cold I/O`; restored current source passes. The initial collector and historical-reassignment API had separate data-loss negative controls earlier in this PR. - Eight real-store load-generation cases cover clean reads, stray rehoming, canonical replacement, malformed/unreadable quarantine, nonempty/empty backup recovery, and staged GC reading C-before/A-after while a real load+flush moves the only frozen reference between those shards. Existing isolated counter-removal controls fail for each newly covered boundary. - Existing persona-reassignment API proof still passes: a failed last-reference replacement races cleanup, rolls back, retains the old PNG, and serves it successfully afterward. - The current caller/lock-order audit found no ambient transaction around the staged public cleanup entrypoints. Initial capture never requests the avatar lock; final work takes avatar lock then transaction. Mutation callbacks finish before cleanup releases/reacquires that lock. Fresh full local CodeRabbit review completed with **0 findings across all seven PR files**, including the load-heal counter fixture. It reviewed the complete Avatar diff against original base `957cb117`; all seven reviewed files are SHA256-identical after merging current staging `1ffb916df`. Final integration head: `8fb95039021cf9c3203c300d110e1a14a820bfd0`. The full `pnpm check` also passed at that integration head under pinned pnpm 12.15.0 (one existing client hook-dependency warning, zero errors). Earlier review findings and their source-based dispositions remain in the PR discussion. This revision replaces the previously accepted whole-scan write-gate tradeoff: cold history I/O now runs outside both gates. Earlier eviction assumptions no longer describe the design; captured resident references plus final generation validation are the safety boundary. ## Manual verification - [ ] Manually replace/remove an unversioned persona avatar, run Storage Optimization, and verify old chat identities still display it. - [ ] Manually switch historical swipes and restore a deleted message after cleanup. - [ ] Manually reassign historical personas in a large chat while another request runs cleanup. - [ ] Build/run a container if container-specific confirmation is required. ## Proof boundaries The automated storage/API cases use disposable data and injected failures, not real user data or live providers. Prior desktop shell smoke passed 7/7; this revision changes only server storage/lifecycle behavior, so it does not add new browser or physical-device proof. Earlier hosted checks apply to their recorded heads; new-head CI and Dottore Review must pass before merge. One focused test initially hit another test's default-storage writer lease before its assertions; its unchanged isolated-runner replay passed. Cold scans can still take time and final checks/unlinks serialize briefly with writers; no latency SLA is claimed. Images already deleted by earlier cleanup need restoration from a backup. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-24 04:10:31 +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#8117
No description provided.