[PR #3866] [MERGED] Sweep dangling connection references when a connection is deleted #6134

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/3866
Author: @kh0p
Created: 7/20/2026
Status: ✅ Merged
Merged: 7/20/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/cleanup-dangling-connection-references


📝 Commits (4)

  • 23b335c Sweep dangling connection references when a connection is deleted
  • 9d210c3 Merge branch 'staging' into fix/cleanup-dangling-connection-references
  • 94dea0a Address connection cleanup review feedback
  • 41a2dd4 Merge branch 'staging' into fix/cleanup-dangling-connection-references

📊 Changes

2 files changed (+184 additions, -0 deletions)

View changed files

➕ packages/server/src/services/storage/connection-reference-cleanup.ts (+170 -0)
📝 packages/server/src/services/storage/connections.storage.ts (+14 -0)

📄 Description

Linked issue

Closes #3865

Why this change

Deleting a connection leaves every stored reference to its id untouched — chats.connectionId, agentConfigs.connectionId, connections.embeddingConnectionId, and 15+ distinct *ConnectionId fields inside chats.metadata (illustratorPromptConnectionId, gameImageConnectionId, gameSceneConnectionId, sceneVideoConnectionId, imageGenConnectionId, agentOverrides.<type>.imageConnectionId, and more). #2683 / PR #2730 fixed the general per-agent connection resolution path to fail gracefully on a dangling id, but that fix didn't (and structurally couldn't, being a resolution-time guard) prevent the dangling reference from existing in the first place, and at least one other path (illustratorPromptConnectionId read directly in agent-resolution.ts's illustrator special case and its near-duplicate in retry-agents-route.ts) doesn't fail gracefully at all — reproduced firsthand, a stale reference there silently misrouted a post-processing agent turn onto an unrelated, much larger model and turned a normal ~3 minute turn into over an hour.

Rather than adding another one-off guard to that specific path (and every future path that reads one of these fields), this removes the dangling reference at the source: the moment a connection is deleted.

What changed

  • Added sweepDanglingConnectionReferences() (services/storage/connection-reference-cleanup.ts), called from connections.storage.ts's remove().
  • Clears chats.connectionId and agentConfigs.connectionId directly, and connections.embeddingConnectionId on any other connection.
  • Sweeps chats.metadata and agentConfigs.settings recursively: any JSON key matching /ConnectionId$/, at any nesting depth, is treated as a connection reference and nulled if it matches the deleted id.
  • Deliberately generic rather than a hardcoded field list — matches the naming convention already used consistently for every such field in this codebase, so it stays correct as new *ConnectionId settings get added without this file needing to track them.
  • Logs a one-line summary (chatsUpdated / agentsUpdated / connectionsUpdated) when the sweep actually clears something, silent no-op otherwise.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

This is a backend-only, non-UI change, so the container/UI-mode checklist items above don't apply (left unticked rather than claimed). What I did verify directly:

  • tsc --noEmit and a full tsc build against a real, dependency-installed checkout — both pass with zero errors.
  • End-to-end against a running instance: created a throwaway connection, referenced it four different ways on a chat (chat.connectionId, metadata.illustratorPromptConnectionId, metadata.gameImageConnectionId, and the nested metadata.agentOverrides.custom-akistar-forge.imageConnectionId), deleted the connection via the API, then re-fetched the chat and confirmed all four references were nulled. Repeated with a fresh connection/chat pair to rule out a fluke.
  • Confirmed the pre-existing #2683/#2730 general-path fix is present and working on current staging before scoping this PR to the gap it left.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

N/A — backend-only change, no UI surface.

Summary by CodeRabbit

  • Bug Fixes
    • Deleting a connection now automatically clears references to it across chats, agent configurations, and related connection settings.
    • Nested connection references in metadata and settings are safely removed, preventing stale or invalid configuration links.
    • Cleanup activity is tracked to improve visibility into affected records.

🔄 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/3866 **Author:** [@kh0p](https://github.com/kh0p) **Created:** 7/20/2026 **Status:** ✅ Merged **Merged:** 7/20/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/cleanup-dangling-connection-references` --- ### 📝 Commits (4) - [`23b335c`](https://github.com/Pasta-Devs/Marinara-Engine/commit/23b335c56a1ed5ee5de8aff12a44f79f4df5d935) Sweep dangling connection references when a connection is deleted - [`9d210c3`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9d210c36dc4497d85fa557e56e81f17dbbf1b79a) Merge branch 'staging' into fix/cleanup-dangling-connection-references - [`94dea0a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/94dea0ae2ab5ba367db27dfdc1d28308403f4c34) Address connection cleanup review feedback - [`41a2dd4`](https://github.com/Pasta-Devs/Marinara-Engine/commit/41a2dd48093c7e34195d57c13fd6f5015c71e4f1) Merge branch 'staging' into fix/cleanup-dangling-connection-references ### 📊 Changes **2 files changed** (+184 additions, -0 deletions) <details> <summary>View changed files</summary> ➕ `packages/server/src/services/storage/connection-reference-cleanup.ts` (+170 -0) 📝 `packages/server/src/services/storage/connections.storage.ts` (+14 -0) </details> ### 📄 Description ## Linked issue Closes #3865 ## Why this change Deleting a connection leaves every stored reference to its id untouched — `chats.connectionId`, `agentConfigs.connectionId`, `connections.embeddingConnectionId`, and 15+ distinct `*ConnectionId` fields inside `chats.metadata` (`illustratorPromptConnectionId`, `gameImageConnectionId`, `gameSceneConnectionId`, `sceneVideoConnectionId`, `imageGenConnectionId`, `agentOverrides.<type>.imageConnectionId`, and more). #2683 / PR #2730 fixed the general per-agent connection resolution path to fail gracefully on a dangling id, but that fix didn't (and structurally couldn't, being a resolution-time guard) prevent the dangling reference from existing in the first place, and at least one other path (`illustratorPromptConnectionId` read directly in `agent-resolution.ts`'s illustrator special case and its near-duplicate in `retry-agents-route.ts`) doesn't fail gracefully at all — reproduced firsthand, a stale reference there silently misrouted a post-processing agent turn onto an unrelated, much larger model and turned a normal ~3 minute turn into over an hour. Rather than adding another one-off guard to that specific path (and every future path that reads one of these fields), this removes the dangling reference at the source: the moment a connection is deleted. ## What changed - Added `sweepDanglingConnectionReferences()` (`services/storage/connection-reference-cleanup.ts`), called from `connections.storage.ts`'s `remove()`. - Clears `chats.connectionId` and `agentConfigs.connectionId` directly, and `connections.embeddingConnectionId` on any other connection. - Sweeps `chats.metadata` and `agentConfigs.settings` recursively: any JSON key matching `/ConnectionId$/`, at any nesting depth, is treated as a connection reference and nulled if it matches the deleted id. - Deliberately generic rather than a hardcoded field list — matches the naming convention already used consistently for every such field in this codebase, so it stays correct as new `*ConnectionId` settings get added without this file needing to track them. - Logs a one-line summary (`chatsUpdated` / `agentsUpdated` / `connectionsUpdated`) when the sweep actually clears something, silent no-op otherwise. ## Validation - [x] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes This is a backend-only, non-UI change, so the container/UI-mode checklist items above don't apply (left unticked rather than claimed). What I did verify directly: - `tsc --noEmit` and a full `tsc` build against a real, dependency-installed checkout — both pass with zero errors. - End-to-end against a running instance: created a throwaway connection, referenced it four different ways on a chat (`chat.connectionId`, `metadata.illustratorPromptConnectionId`, `metadata.gameImageConnectionId`, and the nested `metadata.agentOverrides.custom-akistar-forge.imageConnectionId`), deleted the connection via the API, then re-fetched the chat and confirmed all four references were nulled. Repeated with a fresh connection/chat pair to rule out a fluke. - Confirmed the pre-existing #2683/#2730 general-path fix is present and working on current staging before scoping this PR to the gap it left. ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) N/A — backend-only change, no UI surface. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Deleting a connection now automatically clears references to it across chats, agent configurations, and related connection settings. * Nested connection references in metadata and settings are safely removed, preventing stale or invalid configuration links. * Cleanup activity is tracked to improve visibility into affected records. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:07:23 +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#6134
No description provided.