[GH-ISSUE #3048] [Issue]: Deleting a persona leaves its id orphaned inside every persona folder/group, overcounting members in the delete-folder dialog #1617

Closed
opened 2026-10-21 05:40:52 +07:00 by bsakaguchi · 2 comments
Owner

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong
Deleting a persona never removes its id from the persona folders/groups it belonged to. personaGroups.personaIds is a plain JSON array string with no foreign key, so there's no cascade and the delete path does no manual cleanup — the deleted persona stays listed as a member forever. In the Personas panel the folder member badge and the "Delete Folder" confirmation are driven by the raw parsed group.memberIds.length, so after a delete the dialog overcounts (e.g. "Its 3 personas will stay in the library" when only 2 exist) while the expanded member list (which filters against live personas) shows 2 — a visible inconsistency. The dangling id also persists in stored JSON and survives into backups/exports and the mari-db persona_groups.personaIds JSON sync.

How to reproduce (try it yourself)

  1. Create a persona folder/group with personas P1, P2, P3 (POST /api/characters/persona-groups with personaIds:[P1,P2,P3], or via the Personas panel folders).
  2. Delete persona P3 (DELETE /api/characters/personas/:id).
  3. GET /api/characters/persona-groups/:id, then open that folder's "Delete Folder" confirmation.

You'll see: the personaIds JSON still contains P3, and the badge/dialog say "3 personas" while the expanded member list shows only P1 and P2.
Expected: P3's id is removed on delete; the badge and dialog say "2 personas."

Where it is

  • packages/server/src/services/storage/characters.storage.ts:435-437 — removePersona(id) only runs db.delete(personas).
  • packages/server/src/routes/characters.routes.ts:930-944 — DELETE /personas/:id removes the gallery dir and calls storage.removePersona, no group cleanup.
  • packages/server/src/db/schema/characters.ts:103-111 — personaGroups.personaIds = text("persona_ids").notNull().default("[]"), denormalized JSON array, no FK (contrast personaCardVersions.personaId cascade at :74-78).
  • Client symptom: packages/client/src/components/panels/PersonasPanel.tsx:284-289 (delete-folder dialog uses raw count) and :743-746 (folder-header badge), inconsistent with :773-775 (member list filters via personaMap).

What's happening
personaGroups.personaIds is denormalized JSON with no referential integrity, so no DB/file-store cascade reaches it and no application-level scrub runs. updatePersonaGroup only rewrites personaIds when the client sends it, so the dangling id persists indefinitely.

Suggested fix
Scrub the deleted id out of every persona group inside removePersona, so all callers benefit, mirroring the character-group fix. Parse defensively (rows store personaIds as a JSON STRING, not an array) and only rewrite groups that actually contained the id:

async removePersona(id: string) {
  await db.delete(personas).where(eq(personas.id, id));
  // personaGroups.personaIds is an unconstrained JSON-text column (no FK/cascade) — prune manually.
  const groups = await this.listPersonaGroups();
  for (const group of groups) {
    let memberIds: string[];
    try {
      memberIds = JSON.parse(group.personaIds) as string[];
    } catch {
      continue; // skip malformed membership; never let it abort the delete
    }
    if (!Array.isArray(memberIds) || !memberIds.includes(id)) continue;
    await this.updatePersonaGroup(group.id, { personaIds: memberIds.filter((pid) => pid !== id) });
  }
}

This reuses the existing listPersonaGroups() reader and updatePersonaGroup() writer (which already JSON.stringifies the array). Regression notes: the includes(id) guard means only affected folders are rewritten, so untouched folders keep their updatedAt and ordering (listPersonaGroups/PersonasPanel order by desc(updatedAt)) and the file-backed store isn't needlessly dirtied. The try/catch keeps one malformed group from aborting the persona delete. Client consumers already tolerate dangling ids defensively, so this is strictly corrective. Persona versioning/images already cascade via FK and are untouched. The character-group equivalent is filed separately.

Related: the same orphaned-membership pattern affects CHARACTER folders/groups, filed as a sibling issue (remove needs the same scrub).


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3048 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** Deleting a persona never removes its id from the persona folders/groups it belonged to. `personaGroups.personaIds` is a plain JSON array string with no foreign key, so there's no cascade and the delete path does no manual cleanup — the deleted persona stays listed as a member forever. In the Personas panel the folder member badge and the "Delete Folder" confirmation are driven by the raw parsed `group.memberIds.length`, so after a delete the dialog overcounts (e.g. "Its 3 personas will stay in the library" when only 2 exist) while the expanded member list (which filters against live personas) shows 2 — a visible inconsistency. The dangling id also persists in stored JSON and survives into backups/exports and the mari-db `persona_groups.personaIds` JSON sync. **How to reproduce (try it yourself)** 1. Create a persona folder/group with personas P1, P2, P3 (POST /api/characters/persona-groups with personaIds:[P1,P2,P3], or via the Personas panel folders). 2. Delete persona P3 (DELETE /api/characters/personas/:id). 3. GET /api/characters/persona-groups/:id, then open that folder's "Delete Folder" confirmation. You'll see: the personaIds JSON still contains P3, and the badge/dialog say "3 personas" while the expanded member list shows only P1 and P2. Expected: P3's id is removed on delete; the badge and dialog say "2 personas." **Where it is** - `packages/server/src/services/storage/characters.storage.ts:435-437` — `removePersona(id)` only runs `db.delete(personas)`. - `packages/server/src/routes/characters.routes.ts:930-944` — DELETE /personas/:id removes the gallery dir and calls `storage.removePersona`, no group cleanup. - `packages/server/src/db/schema/characters.ts:103-111` — `personaGroups.personaIds = text("persona_ids").notNull().default("[]")`, denormalized JSON array, no FK (contrast personaCardVersions.personaId cascade at :74-78). - Client symptom: `packages/client/src/components/panels/PersonasPanel.tsx:284-289` (delete-folder dialog uses raw count) and `:743-746` (folder-header badge), inconsistent with `:773-775` (member list filters via personaMap). **What's happening** `personaGroups.personaIds` is denormalized JSON with no referential integrity, so no DB/file-store cascade reaches it and no application-level scrub runs. `updatePersonaGroup` only rewrites `personaIds` when the client sends it, so the dangling id persists indefinitely. **Suggested fix** Scrub the deleted id out of every persona group inside `removePersona`, so all callers benefit, mirroring the character-group fix. Parse defensively (rows store `personaIds` as a JSON STRING, not an array) and only rewrite groups that actually contained the id: ```ts async removePersona(id: string) { await db.delete(personas).where(eq(personas.id, id)); // personaGroups.personaIds is an unconstrained JSON-text column (no FK/cascade) — prune manually. const groups = await this.listPersonaGroups(); for (const group of groups) { let memberIds: string[]; try { memberIds = JSON.parse(group.personaIds) as string[]; } catch { continue; // skip malformed membership; never let it abort the delete } if (!Array.isArray(memberIds) || !memberIds.includes(id)) continue; await this.updatePersonaGroup(group.id, { personaIds: memberIds.filter((pid) => pid !== id) }); } } ``` This reuses the existing `listPersonaGroups()` reader and `updatePersonaGroup()` writer (which already JSON.stringifies the array). Regression notes: the `includes(id)` guard means only affected folders are rewritten, so untouched folders keep their `updatedAt` and ordering (`listPersonaGroups`/PersonasPanel order by `desc(updatedAt)`) and the file-backed store isn't needlessly dirtied. The `try/catch` keeps one malformed group from aborting the persona delete. Client consumers already tolerate dangling ids defensively, so this is strictly corrective. Persona versioning/images already cascade via FK and are untouched. The character-group equivalent is filed separately. *Related: the same orphaned-membership pattern affects CHARACTER folders/groups, filed as a sibling issue (`remove` needs the same scrub).* --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Claiming this for the current staging sweep in draft PR #3053 (branch codex/issue-sweep-3033-3052).

<!-- gh-comment-id:4834990412 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Claiming this for the current staging sweep in draft PR #3053 (branch `codex/issue-sweep-3033-3052`).
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in PR #3053 on branch codex/issue-sweep-3033-3052 (commit bfb1bc76). Closing this issue as addressed; the PR remains open for staging review/merge.

<!-- gh-comment-id:4835090442 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in PR #3053 on branch `codex/issue-sweep-3033-3052` (commit bfb1bc76). Closing this issue as addressed; the PR remains open for staging review/merge.
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#1617
No description provided.