[GH-ISSUE #3043] [Issue]: Deleting a character leaves its id orphaned inside every folder/group (no membership cleanup), inflating folder member counts #1611

Closed
opened 2026-10-21 05:40:40 +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/3043

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong
When you delete a character, its id is never removed from the character folders/groups it belonged to. Group membership is stored as a plain JSON array of ids with no database foreign key, so the cascade that cleans up versions and gallery images can't reach it, and nothing in the delete path scrubs it manually. The deleted character stays listed as a member of every folder forever. The most visible symptom: in the Characters panel, a folder's member count and its "Delete Folder" confirmation count the phantom member, so the dialog says e.g. "Its 3 characters will stay in the library" when only 2 actually exist. The stale id also accumulates permanently in stored JSON and survives into backups/exports.

How to reproduce (try it yourself)

  1. Create a character folder/group containing characters A, B, and C (POST /api/characters/groups with characterIds:[A,B,C], or via the Characters panel folder UI).
  2. Delete character C (DELETE /api/characters/:id).
  3. GET /api/characters/groups/:id, and open the folder's "Delete Folder" confirmation in the Characters panel.

You'll see: the group's characterIds JSON still contains C's id, and the folder's member count / delete-folder dialog still says "3 characters" even though only A and B remain in the library.
Expected: C's id is removed from the folder on delete; the member count and dialog say "2 characters."

Where it is

  • packages/server/src/services/storage/characters.storage.ts:291-293 — remove(id) only runs db.delete(characters), no group scrub.
  • packages/server/src/routes/characters.routes.ts:488-498 — DELETE /:id removes the gallery dir and calls storage.remove, no cleanup.
  • packages/server/src/db/schema/characters.ts:92-101 — characterGroups.characterIds = text("character_ids").notNull().default("[]"), a denormalized JSON array with no .references()/FK.
  • Client symptom: packages/client/src/components/panels/CharactersPanel.tsx:402-417 (parses memberIds with no existence filter) and :503-509 (uses group.memberIds.length for the count and the "Its N characters will stay in the library" message).

What's happening
characterGroups.characterIds is a denormalized JSON array string, so SQLite onDelete cascade (and the file-backed store's row-FK-only cascade) structurally cannot edit it. storage.remove deletes only the characters row, and there is no application-level cleanup step. updateGroup only rewrites characterIds when the client explicitly sends it, so the dangling id persists indefinitely. The actual member render list (CharactersPanel.tsx:841-842) filters against live characters, but the count and dialog read the raw parsed array, producing the inflated number.

Suggested fix
Scrub the deleted id out of every character group inside storage.remove (covers both the DELETE route and the import-rollback caller). Only rewrite groups that actually contained the id, and parse defensively so one malformed group row can't abort the delete:

async remove(id: string) {
  await db.delete(characters).where(eq(characters.id, id));
  // characterGroups.characterIds is a denormalized JSON array (no FK/cascade) — scrub manually.
  const groups = await this.listGroups();
  for (const group of groups) {
    let memberIds: string[];
    try {
      memberIds = typeof group.characterIds === "string" ? JSON.parse(group.characterIds) : [];
    } catch {
      continue; // leave a corrupt group untouched rather than aborting the delete
    }
    if (!Array.isArray(memberIds) || !memberIds.includes(id)) continue;
    await this.updateGroup(group.id, { characterIds: memberIds.filter((cid) => cid !== id) });
  }
}

Regression notes: updateGroup always bumps updatedAt, and listGroups sorts by desc(updatedAt), so writing back EVERY group would silently reorder folders and dirty the file-backed store on every delete — the includes(id) guard limits writes to affected folders only. The try/catch mirrors the server's existing typeof === "string" ? JSON.parse(...) : [] idiom (chats.routes.ts) so a single legacy/corrupt group value can't throw and abort deletion of an unrelated character. Empty folders are already valid, and chats store their own copied characterIds snapshot, so scrubbing the group preset doesn't touch any chat. Do NOT add id-existence validation to createGroup/updateGroup — that would reject valid out-of-order import/restore flows. The same orphan pattern exists for persona groups and is filed as a separate issue.

Related: the same orphaned-membership pattern affects PERSONA folders/groups, filed as a sibling issue (removePersona 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/3043 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** When you delete a character, its id is never removed from the character folders/groups it belonged to. Group membership is stored as a plain JSON array of ids with no database foreign key, so the cascade that cleans up versions and gallery images can't reach it, and nothing in the delete path scrubs it manually. The deleted character stays listed as a member of every folder forever. The most visible symptom: in the Characters panel, a folder's member count and its "Delete Folder" confirmation count the phantom member, so the dialog says e.g. "Its 3 characters will stay in the library" when only 2 actually exist. The stale id also accumulates permanently in stored JSON and survives into backups/exports. **How to reproduce (try it yourself)** 1. Create a character folder/group containing characters A, B, and C (POST /api/characters/groups with characterIds:[A,B,C], or via the Characters panel folder UI). 2. Delete character C (DELETE /api/characters/:id). 3. GET /api/characters/groups/:id, and open the folder's "Delete Folder" confirmation in the Characters panel. You'll see: the group's characterIds JSON still contains C's id, and the folder's member count / delete-folder dialog still says "3 characters" even though only A and B remain in the library. Expected: C's id is removed from the folder on delete; the member count and dialog say "2 characters." **Where it is** - `packages/server/src/services/storage/characters.storage.ts:291-293` — `remove(id)` only runs `db.delete(characters)`, no group scrub. - `packages/server/src/routes/characters.routes.ts:488-498` — DELETE /:id removes the gallery dir and calls `storage.remove`, no cleanup. - `packages/server/src/db/schema/characters.ts:92-101` — `characterGroups.characterIds = text("character_ids").notNull().default("[]")`, a denormalized JSON array with no `.references()`/FK. - Client symptom: `packages/client/src/components/panels/CharactersPanel.tsx:402-417` (parses memberIds with no existence filter) and `:503-509` (uses `group.memberIds.length` for the count and the "Its N characters will stay in the library" message). **What's happening** `characterGroups.characterIds` is a denormalized JSON array string, so SQLite `onDelete` cascade (and the file-backed store's row-FK-only cascade) structurally cannot edit it. `storage.remove` deletes only the `characters` row, and there is no application-level cleanup step. `updateGroup` only rewrites `characterIds` when the client explicitly sends it, so the dangling id persists indefinitely. The actual member render list (CharactersPanel.tsx:841-842) filters against live characters, but the count and dialog read the raw parsed array, producing the inflated number. **Suggested fix** Scrub the deleted id out of every character group inside `storage.remove` (covers both the DELETE route and the import-rollback caller). Only rewrite groups that actually contained the id, and parse defensively so one malformed group row can't abort the delete: ```ts async remove(id: string) { await db.delete(characters).where(eq(characters.id, id)); // characterGroups.characterIds is a denormalized JSON array (no FK/cascade) — scrub manually. const groups = await this.listGroups(); for (const group of groups) { let memberIds: string[]; try { memberIds = typeof group.characterIds === "string" ? JSON.parse(group.characterIds) : []; } catch { continue; // leave a corrupt group untouched rather than aborting the delete } if (!Array.isArray(memberIds) || !memberIds.includes(id)) continue; await this.updateGroup(group.id, { characterIds: memberIds.filter((cid) => cid !== id) }); } } ``` Regression notes: `updateGroup` always bumps `updatedAt`, and `listGroups` sorts by `desc(updatedAt)`, so writing back EVERY group would silently reorder folders and dirty the file-backed store on every delete — the `includes(id)` guard limits writes to affected folders only. The `try/catch` mirrors the server's existing `typeof === "string" ? JSON.parse(...) : []` idiom (chats.routes.ts) so a single legacy/corrupt group value can't throw and abort deletion of an unrelated character. Empty folders are already valid, and chats store their own copied characterIds snapshot, so scrubbing the group preset doesn't touch any chat. Do NOT add id-existence validation to createGroup/updateGroup — that would reject valid out-of-order import/restore flows. The same orphan pattern exists for persona groups and is filed as a separate issue. *Related: the same orphaned-membership pattern affects PERSONA folders/groups, filed as a sibling issue (`removePersona` 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:4834988981 --> @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:4835088870 --> @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#1611
No description provided.