[GH-ISSUE #3044] [Issue]: Activating a non-existent/stale persona id deactivates ALL personas and still returns success, leaving no active persona #1613

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong
The "set active persona" path first clears the active flag on EVERY persona (an UPDATE with no WHERE), then sets it true only on the row matching the given id. There is no existence check in either the route or the storage method. If the id doesn't exist — a persona deleted in another tab, a stale client cache, a typo, or any direct API caller — the first UPDATE deactivates all personas and the second matches zero rows, leaving the system with NO active persona. The route still returns HTTP 200 { success: true }. Every server path that falls back to the globally-active persona (personas.find(p => p.isActive === "true")) then silently gets undefined and drops the user's persona from the prompt until a valid persona is manually re-activated. (Chats that pin an explicit chat.personaId are unaffected, which is why severity is bounded.)

How to reproduce (try it yourself)

  1. Have several personas with one active.
  2. PUT /api/characters/personas//activate.
  3. GET /api/characters/personas/list, then start/continue a chat with no explicit chat.personaId.

You'll see: the response is 200 { success: true }, but no persona has isActive === "true", and the global-active fallback resolves to undefined so the user's persona is omitted from the prompt.
Expected: a non-existent id returns 404 and leaves the existing active persona untouched.

Where it is

  • packages/server/src/services/storage/characters.storage.ts:428-433 — setActivePersona does db.update(personas).set({ isActive: "false" }) (no WHERE), then .set({ isActive: "true", ... }).where(eq(personas.id, id)). No existence guard.
  • packages/server/src/routes/characters.routes.ts:925-928 — PUT /personas/:id/activate calls storage.setActivePersona(req.params.id) and returns { success: true } unconditionally. This is the only caller of setActivePersona.
  • Downstream fallback example: packages/server/src/routes/chats.routes.ts:386-394 (buildPersonaSnapshotForChat returns null when nothing is active). The same isActive === "true" fallback appears in ~15 sites (generate/prompts/scene/encounter routes, awareness.service.ts, etc.).

What's happening
The deactivate-all UPDATE runs before validating the target exists, and the activate UPDATE is a no-op when the id is absent. On the default file-native backend, an UPDATE with no condition matches every row (evaluateCondition(undefined) returns true), and eq(id, badId) matches zero — identical to the SQLite path. The sibling DELETE and gallery routes for personas already do getPersona + 404, so the missing guard here is an inconsistency, not intent.

Suggested fix
Guard the route before any rows are touched, mirroring the existing DELETE /personas/:id route exactly (reply param, path-segment guard, getPersona + 404). Keep setActivePersona unchanged:

app.put<{ Params: { id: string } }>("/personas/:id/activate", async (req, reply) => {
  const { id } = req.params;
  if (isUnsafePathSegment(id)) {
    return reply.status(400).send({ error: "Invalid persona id" });
  }
  const persona = await storage.getPersona(id);
  if (!persona) return reply.status(404).send({ error: "Persona not found" });
  await storage.setActivePersona(id);
  return { success: true };
});

This ensures the unconditional deactivate-all is never reached for a non-existent id, so the table can never be left with zero active personas while the route reports success. Regression notes: the valid-id happy path is unchanged. The client hook useActivatePersona (use-characters.ts:649-655) has no onError; on a 404 it simply skips its cache invalidation instead of refetching into a corrupted state — strictly better than the previous false 200. setActivePersona has exactly one caller, so the route-level guard fully closes the hole with no storage change or transaction needed. Equivalently, the guard could live in setActivePersona (returning false, with the route honoring it via 404), mirroring chat-presets.storage.ts/themes.storage.ts setActive; the load-bearing part is checking existence before clearing any flag.


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/3044 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** The "set active persona" path first clears the active flag on EVERY persona (an UPDATE with no WHERE), then sets it true only on the row matching the given id. There is no existence check in either the route or the storage method. If the id doesn't exist — a persona deleted in another tab, a stale client cache, a typo, or any direct API caller — the first UPDATE deactivates all personas and the second matches zero rows, leaving the system with NO active persona. The route still returns HTTP 200 `{ success: true }`. Every server path that falls back to the globally-active persona (`personas.find(p => p.isActive === "true")`) then silently gets undefined and drops the user's persona from the prompt until a valid persona is manually re-activated. (Chats that pin an explicit `chat.personaId` are unaffected, which is why severity is bounded.) **How to reproduce (try it yourself)** 1. Have several personas with one active. 2. PUT /api/characters/personas/<non-existent-or-just-deleted-id>/activate. 3. GET /api/characters/personas/list, then start/continue a chat with no explicit `chat.personaId`. You'll see: the response is 200 `{ success: true }`, but no persona has `isActive === "true"`, and the global-active fallback resolves to undefined so the user's persona is omitted from the prompt. Expected: a non-existent id returns 404 and leaves the existing active persona untouched. **Where it is** - `packages/server/src/services/storage/characters.storage.ts:428-433` — `setActivePersona` does `db.update(personas).set({ isActive: "false" })` (no WHERE), then `.set({ isActive: "true", ... }).where(eq(personas.id, id))`. No existence guard. - `packages/server/src/routes/characters.routes.ts:925-928` — PUT /personas/:id/activate calls `storage.setActivePersona(req.params.id)` and returns `{ success: true }` unconditionally. This is the only caller of `setActivePersona`. - Downstream fallback example: `packages/server/src/routes/chats.routes.ts:386-394` (`buildPersonaSnapshotForChat` returns null when nothing is active). The same `isActive === "true"` fallback appears in ~15 sites (generate/prompts/scene/encounter routes, awareness.service.ts, etc.). **What's happening** The deactivate-all UPDATE runs before validating the target exists, and the activate UPDATE is a no-op when the id is absent. On the default file-native backend, an UPDATE with no condition matches every row (`evaluateCondition(undefined)` returns true), and `eq(id, badId)` matches zero — identical to the SQLite path. The sibling DELETE and gallery routes for personas already do `getPersona` + 404, so the missing guard here is an inconsistency, not intent. **Suggested fix** Guard the route before any rows are touched, mirroring the existing DELETE /personas/:id route exactly (reply param, path-segment guard, getPersona + 404). Keep `setActivePersona` unchanged: ```ts app.put<{ Params: { id: string } }>("/personas/:id/activate", async (req, reply) => { const { id } = req.params; if (isUnsafePathSegment(id)) { return reply.status(400).send({ error: "Invalid persona id" }); } const persona = await storage.getPersona(id); if (!persona) return reply.status(404).send({ error: "Persona not found" }); await storage.setActivePersona(id); return { success: true }; }); ``` This ensures the unconditional deactivate-all is never reached for a non-existent id, so the table can never be left with zero active personas while the route reports success. Regression notes: the valid-id happy path is unchanged. The client hook `useActivatePersona` (use-characters.ts:649-655) has no `onError`; on a 404 it simply skips its cache invalidation instead of refetching into a corrupted state — strictly better than the previous false 200. `setActivePersona` has exactly one caller, so the route-level guard fully closes the hole with no storage change or transaction needed. Equivalently, the guard could live in `setActivePersona` (returning false, with the route honoring it via 404), mirroring `chat-presets.storage.ts`/`themes.storage.ts` `setActive`; the load-bearing part is checking existence before clearing any flag. --- *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:4834989271 --> @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:4835089201 --> @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#1613
No description provided.