mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #3038] [Issue]: Changing a connection's provider via PATCH can leave two connections marked defaultForAgents, making agent selection ambiguous #1606
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#1606
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @kolacheee on GitHub (Jun 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3038
Originally assigned to: @SpicyMarinara on GitHub.
What's wrong
The "at most one non-image connection is the default for agents" invariant is only enforced when a PATCH body includes
defaultForAgents. If a raw API call changes only a connection'sprovider(moving it between the image-generation and non-image categories) without re-sendingdefaultForAgents, the dedupe block is skipped. A connection that was the image-generation default can become a non-image default while a legitimate text default already exists — sogetDefaultForAgents()now finds two candidates and returns whichever its unordered query yields first. Agents may resolve to the wrong connection. (The ConnectionEditor UI always sendsdefaultForAgents, so this is reachable only via the raw API.)How to reproduce (try it yourself)
defaultForAgents='true'.image_generationconnection B withdefaultForAgents='true'(allowed — separate category).PATCH /api/connections/{B.id}with body{"provider":"openai"}only (nodefaultForAgents).You'll see: B's
effectiveProviderbecomesopenaibut the unset-others block is skipped (becausedata.defaultForAgentsis undefined), so B keepsdefaultForAgents='true'. Now both A and B are non-image defaults;getDefaultForAgents()returns whichever the unordered query yields first.Expected: after the provider change, exactly one non-image connection remains the default for agents.
Where it is
packages/server/src/services/storage/connections.storage.ts: theupdate()dedupe block gated onif (data.defaultForAgents !== undefined)(lines 136-156;effectiveProviderat line 118), andgetDefaultForAgents()'s unorderedselect+.find(lines 37-42). Reachable viaconnections.routes.ts:260-265(createConnectionSchema.partial().parse). The UI always sendsdefaultForAgentsatpackages/client/src/components/connections/ConnectionEditor.tsx:462.What's happening
The category-uniqueness enforcement is gated on
data.defaultForAgentsbeing supplied, but a provider change alone moves a row between the image/non-image categories, invalidating the previously-maintained invariant without triggering re-enforcement.getDefaultForAgents()then resolves the ambiguity with an unordered.find(), so the chosen row is non-deterministic.Suggested fix
Enforce the invariant on a provider change too. In
connections.storage.ts:neto the drizzle import (line 4):import { eq, desc, and, ne } from "drizzle-orm";if (data.defaultForAgents !== undefined) { ... }block, add anelse ifthat runs only whendefaultForAgentswas NOT sent and the edited row is already a default, and which excludes the edited row's own id so it stays the sole default in its new category:This keeps the edited row's
defaultForAgents='true'and demotes any conflicting default in the category it just moved into, in both directions (covered by branching oneffectiveProvider).Regression avoided: do NOT just reuse the existing dedupe blocks for the provider-change case. Those blocks unset every default in the target category without excluding the edited row, and rely on the subsequent
updateFields.defaultForAgents = "true"write to re-set it — which does not run whendefaultForAgentsisn't in the body. Reusing them as-is would demote the edited row itself, leaving ZERO defaults (a different but equally wrong bug). The new block excludes the edited row's own id and runs only whendefaultForAgentsis absent, so it never double-runs with the existing block. Optionally add.orderBy(desc(apiConnections.updatedAt))togetDefaultForAgents()/getDefaultForImageGeneration()for deterministic resolution of any pre-existing duplicate state — but that alone is insufficient, since it leaves twodefaultForAgents='true'rows persisted.AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.
@SpicyMarinara commented on GitHub (Jun 29, 2026):
Claiming this for the current staging sweep in draft PR #3053 (branch
codex/issue-sweep-3033-3052).@SpicyMarinara commented on GitHub (Jun 29, 2026):
Fixed in PR #3053 on branch
codex/issue-sweep-3033-3052(commitbfb1bc76). Closing this issue as addressed; the PR remains open for staging review/merge.