[GH-ISSUE #3865] Deleting a connection leaves dangling references across chat metadata and agent settings #2089

Closed
opened 2026-10-21 05:45:17 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @kh0p on GitHub (Jul 20, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3865

Originally assigned to: @SpicyMarinara on GitHub.

Summary

Deleting a connection (DELETE /api/connections/:id) removes only the connection row itself. Every stored reference to that id elsewhere is left untouched: chats.connectionId, agentConfigs.connectionId, connections.embeddingConnectionId, and the large family of *ConnectionId keys stored inside chats.metadata (illustratorPromptConnectionId, gameImageConnectionId, gameSceneConnectionId, sceneVideoConnectionId, imageGenConnectionId, agentOverrides.<type>.imageConnectionId, and others).

Expected behavior

Deleting a connection should clear (null out) every stored reference to it, so no feature is left pointing at an id that no longer resolves to anything.

Actual behavior

References survive the delete. What happens next depends on which code path reads the dangling id:

  • Some paths handle it gracefully (this looks like the intent of #2683 / PR #2730 for the general per-agent connection path — confirmed present and working on current staging).
  • services/generation/illustrator-prompt-runtime.ts (resolveIllustratorPromptRuntime, used by Gallery "Generate Selfie" and the Conversation /selfie command) throws synchronously when the id doesn't resolve — surfaced as a 400 to the caller, so at least fails fast.
  • Other paths that read chatMetadata.illustratorPromptConnectionId (e.g. the agentType === "illustrator" special case in agent-resolution.ts / its near-duplicate in retry-agents-route.ts) do not fail fast in practice. Reproduced firsthand: a chat with a stale illustratorPromptConnectionId had one post-processing agent turn silently route through an unrelated, much larger local model instead of the intended (or default-for-agents) connection, taking over an hour instead of the usual ~3 minutes for that turn. Clearing the stale metadata field immediately fixed it; re-introducing the same stale id reproduced the multi-minute misroute again.

Steps to reproduce

  1. Create a connection, note its id.
  2. Reference it from a chat: set it as the chat's main connection, and/or add it to chat metadata as illustratorPromptConnectionId or gameImageConnectionId (no dedicated UI control for these two currently exists; they're set via PATCH /api/chats/:id/metadata, but any chat that went through a setup wizard step touching these fields at the time can end up with one).
  3. Delete the connection.
  4. Observe every one of those references still holds the deleted id — GET /api/chats/:id still shows the stale ids in connectionId and metadata.
  5. Depending on which agent/feature reads the stale id next turn, behavior ranges from a fast, clear error to an unbounded misroute (see above).

Suggested fix

Sweep and null out every stored reference to a connection's id at delete time, in connections.storage.ts's remove(). Given how many distinct *ConnectionId fields exist (15+ across chat metadata alone, per a repo-wide grep) and that new ones get added as features grow, a generic sweep — treat any JSON key matching /ConnectionId$/ inside chats.metadata and agentConfigs.settings, at any nesting depth, as a reference and null it if it matches — seems more durable than hardcoding the current field list. Direct columns (chats.connectionId, agentConfigs.connectionId, connections.embeddingConnectionId) should be cleared the same way.

I have a working patch for this (generic recursive sweep, verified end-to-end against a running install with a connection referenced four different ways including a nested agentOverrides.<type>.imageConnectionId) and will open a PR against staging.

Environment

  • Marinara Engine version: staging @ current (confirmed PR #2730 / #2683 fix already present)
  • Install type: source
  • OS + version: Windows (code-level defect, likely platform-independent)
Originally created by @kh0p on GitHub (Jul 20, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3865 Originally assigned to: @SpicyMarinara on GitHub. ## Summary Deleting a connection (`DELETE /api/connections/:id`) removes only the connection row itself. Every stored reference to that id elsewhere is left untouched: `chats.connectionId`, `agentConfigs.connectionId`, `connections.embeddingConnectionId`, and the large family of `*ConnectionId` keys stored inside `chats.metadata` (`illustratorPromptConnectionId`, `gameImageConnectionId`, `gameSceneConnectionId`, `sceneVideoConnectionId`, `imageGenConnectionId`, `agentOverrides.<type>.imageConnectionId`, and others). ## Expected behavior Deleting a connection should clear (null out) every stored reference to it, so no feature is left pointing at an id that no longer resolves to anything. ## Actual behavior References survive the delete. What happens next depends on which code path reads the dangling id: - Some paths handle it gracefully (this looks like the intent of #2683 / PR #2730 for the general per-agent connection path — confirmed present and working on current staging). - `services/generation/illustrator-prompt-runtime.ts` (`resolveIllustratorPromptRuntime`, used by Gallery "Generate Selfie" and the Conversation `/selfie` command) throws synchronously when the id doesn't resolve — surfaced as a 400 to the caller, so at least fails fast. - Other paths that read `chatMetadata.illustratorPromptConnectionId` (e.g. the `agentType === "illustrator"` special case in `agent-resolution.ts` / its near-duplicate in `retry-agents-route.ts`) do not fail fast in practice. Reproduced firsthand: a chat with a stale `illustratorPromptConnectionId` had one post-processing agent turn silently route through an unrelated, much larger local model instead of the intended (or default-for-agents) connection, taking over an hour instead of the usual ~3 minutes for that turn. Clearing the stale metadata field immediately fixed it; re-introducing the same stale id reproduced the multi-minute misroute again. ## Steps to reproduce 1. Create a connection, note its id. 2. Reference it from a chat: set it as the chat's main connection, and/or add it to chat metadata as `illustratorPromptConnectionId` or `gameImageConnectionId` (no dedicated UI control for these two currently exists; they're set via `PATCH /api/chats/:id/metadata`, but any chat that went through a setup wizard step touching these fields at the time can end up with one). 3. Delete the connection. 4. Observe every one of those references still holds the deleted id — `GET /api/chats/:id` still shows the stale ids in `connectionId` and `metadata`. 5. Depending on which agent/feature reads the stale id next turn, behavior ranges from a fast, clear error to an unbounded misroute (see above). ## Suggested fix Sweep and null out every stored reference to a connection's id at delete time, in `connections.storage.ts`'s `remove()`. Given how many distinct `*ConnectionId` fields exist (15+ across chat metadata alone, per a repo-wide grep) and that new ones get added as features grow, a generic sweep — treat any JSON key matching `/ConnectionId$/` inside `chats.metadata` and `agentConfigs.settings`, at any nesting depth, as a reference and null it if it matches — seems more durable than hardcoding the current field list. Direct columns (`chats.connectionId`, `agentConfigs.connectionId`, `connections.embeddingConnectionId`) should be cleared the same way. I have a working patch for this (generic recursive sweep, verified end-to-end against a running install with a connection referenced four different ways including a nested `agentOverrides.<type>.imageConnectionId`) and will open a PR against `staging`. ## Environment - Marinara Engine version: staging @ current (confirmed PR #2730 / #2683 fix already present) - Install type: source - OS + version: Windows (code-level defect, likely platform-independent)
bsakaguchi 2026-10-21 05:45:17 +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#2089
No description provided.