Skip to content

Commit 622eec9

Browse files
authored
fix(ui): stage every gateway-honored agent model shape instead of silently dropping input (#123995)
The agents-page fallback editor gated staging on a resolvable primary model, but the gateway resolver honors { fallbacks } with no primary (agent-scope.ts, including the explicit empty-array-disables-global contract). With a fully implicit default model, typing a fallback chip cleared the input and staged nothing — no chip, no error, no write. Clearing the primary had the sibling bug: removeFormValue deleted the whole model node including authored agent fallbacks. One stageModelShape owner now writes the smallest representable shape (bare string, { primary, fallbacks }, { fallbacks }, or removal), and both entry points share existingModelParts instead of duplicating existing-shape probing.
1 parent ed5e489 commit 622eec9

2 files changed

Lines changed: 114 additions & 41 deletions

File tree

‎ui/src/pages/agents/model-config.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,67 @@ describe("agent model config", () => {
6060
expect(runtimeConfig.state.configForm?.agents).not.toHaveProperty("list");
6161
runtimeConfig.dispose();
6262
});
63+
64+
it("stages fallbacks without a primary when no model is authored anywhere", async () => {
65+
// Fully implicit default model: typing a fallback chip must stage the
66+
// { fallbacks }-only shape the gateway honors, not silently no-op.
67+
const runtimeConfig = createRuntimeConfig({
68+
agents: { entries: { main: { default: true } } },
69+
});
70+
await runtimeConfig.ensureLoaded();
71+
72+
stageAgentModelFallbacks(runtimeConfig, "main", ["openai/gpt-5.4"]);
73+
74+
expect(runtimeConfig.state.configForm).toEqual({
75+
agents: {
76+
entries: {
77+
main: { default: true, model: { fallbacks: ["openai/gpt-5.4"] } },
78+
},
79+
},
80+
});
81+
runtimeConfig.dispose();
82+
});
83+
84+
it("keeps authored fallbacks when the primary model is cleared", async () => {
85+
const runtimeConfig = createRuntimeConfig({
86+
agents: {
87+
defaults: { model: { primary: "openai/gpt-5.4" } },
88+
entries: {
89+
main: {
90+
default: true,
91+
model: { primary: "anthropic/claude-sonnet-4-6", fallbacks: ["openai/gpt-5.4"] },
92+
},
93+
},
94+
},
95+
});
96+
await runtimeConfig.ensureLoaded();
97+
98+
stageAgentPrimaryModel(runtimeConfig, "main", null);
99+
100+
expect(runtimeConfig.state.configForm).toEqual({
101+
agents: {
102+
defaults: { model: { primary: "openai/gpt-5.4" } },
103+
entries: {
104+
main: { default: true, model: { fallbacks: ["openai/gpt-5.4"] } },
105+
},
106+
},
107+
});
108+
runtimeConfig.dispose();
109+
});
110+
111+
it("still removes the model node when clearing a primary with no fallbacks", async () => {
112+
const runtimeConfig = createRuntimeConfig({
113+
agents: {
114+
entries: { main: { default: true, model: "anthropic/claude-sonnet-4-6" } },
115+
},
116+
});
117+
await runtimeConfig.ensureLoaded();
118+
119+
stageAgentPrimaryModel(runtimeConfig, "main", null);
120+
121+
expect(runtimeConfig.state.configForm).toEqual({
122+
agents: { entries: { main: { default: true } } },
123+
});
124+
runtimeConfig.dispose();
125+
});
63126
});

‎ui/src/pages/agents/model-config.ts‎

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,44 @@ function modelEntry(target: AgentConfigEntryTarget) {
2121
};
2222
}
2323

24+
// Stage the smallest config shape that expresses the selection. The gateway
25+
// resolver honors a bare string, { primary, fallbacks }, and { fallbacks }
26+
// with no primary (agent-scope.ts); staging must write all three or an
27+
// authored piece of the selection silently disappears.
28+
function stageModelShape(
29+
runtimeConfig: RuntimeConfig,
30+
path: Array<string | number>,
31+
primary: string | null,
32+
fallbacks: string[] | null,
33+
) {
34+
if (primary && fallbacks) {
35+
runtimeConfig.patchForm(path, { primary, fallbacks });
36+
} else if (primary) {
37+
runtimeConfig.patchForm(path, primary);
38+
} else if (fallbacks) {
39+
runtimeConfig.patchForm(path, { fallbacks });
40+
} else {
41+
runtimeConfig.removeFormValue(path);
42+
}
43+
}
44+
45+
function existingModelParts(existing: unknown): {
46+
primary: string | null;
47+
fallbacks: string[] | null;
48+
} {
49+
if (typeof existing === "string") {
50+
return { primary: existing.trim() || null, fallbacks: null };
51+
}
52+
if (existing && typeof existing === "object") {
53+
const record = existing as { primary?: unknown; fallbacks?: unknown };
54+
return {
55+
primary: typeof record.primary === "string" ? record.primary.trim() || null : null,
56+
fallbacks: Array.isArray(record.fallbacks) ? (record.fallbacks as string[]) : null,
57+
};
58+
}
59+
return { primary: null, fallbacks: null };
60+
}
61+
2462
/** Stage a primary-model change; clearing falls back to the inherited default. */
2563
export function stageAgentPrimaryModel(
2664
runtimeConfig: RuntimeConfig,
@@ -32,17 +70,9 @@ export function stageAgentPrimaryModel(
3270
return;
3371
}
3472
const entry = modelEntry(target);
35-
if (!modelId) {
36-
runtimeConfig.removeFormValue(entry.path);
37-
} else if (entry.existing && typeof entry.existing === "object") {
38-
const fallbacks = (entry.existing as { fallbacks?: unknown }).fallbacks;
39-
runtimeConfig.patchForm(entry.path, {
40-
primary: modelId,
41-
...(Array.isArray(fallbacks) ? { fallbacks } : {}),
42-
});
43-
} else {
44-
runtimeConfig.patchForm(entry.path, modelId);
45-
}
73+
// Clearing the primary must not delete authored agent fallbacks: the
74+
// { fallbacks }-only shape stays representable.
75+
stageModelShape(runtimeConfig, entry.path, modelId, existingModelParts(entry.existing).fallbacks);
4676
}
4777

4878
/** Stage fallback-list edits, preserving the effective primary model shape. */
@@ -54,40 +84,20 @@ export function stageAgentModelFallbacks(
5484
const config = currentConfigObject(runtimeConfig.state);
5585
const normalized = normalizeStringEntries(fallbacks);
5686
const resolved = resolveAgentConfig(config, agentId);
57-
const primary =
58-
resolveModelPrimary(resolved.entry?.model) ?? resolveModelPrimary(resolved.defaults?.model);
5987
const effective = resolveEffectiveModelFallbacks(resolved.entry?.model, resolved.defaults?.model);
6088
const existingTarget = runtimeConfig.agentEntry(agentId);
61-
const target =
62-
normalized.length > 0
63-
? primary
64-
? (existingTarget ?? runtimeConfig.agentEntry(agentId, { ensure: true }))
65-
: null
66-
: (effective?.length ?? 0) > 0 || existingTarget
67-
? (existingTarget ?? runtimeConfig.agentEntry(agentId, { ensure: true }))
68-
: null;
89+
const mustWrite = normalized.length > 0 || (effective?.length ?? 0) > 0 || existingTarget;
90+
const target = mustWrite
91+
? (existingTarget ?? runtimeConfig.agentEntry(agentId, { ensure: true }))
92+
: null;
6993
if (!target) {
7094
return;
7195
}
7296
const entry = modelEntry(target);
73-
const currentPrimary =
74-
typeof entry.existing === "string"
75-
? entry.existing.trim()
76-
: entry.existing &&
77-
typeof entry.existing === "object" &&
78-
typeof (entry.existing as { primary?: unknown }).primary === "string"
79-
? (entry.existing as { primary: string }).primary.trim()
80-
: "";
81-
if (normalized.length === 0) {
82-
if (currentPrimary || primary) {
83-
runtimeConfig.patchForm(entry.path, currentPrimary || primary);
84-
} else {
85-
runtimeConfig.removeFormValue(entry.path);
86-
}
87-
} else if (currentPrimary || primary) {
88-
runtimeConfig.patchForm(entry.path, {
89-
primary: currentPrimary || primary,
90-
fallbacks: normalized,
91-
});
92-
}
97+
const primary =
98+
existingModelParts(entry.existing).primary ??
99+
resolveModelPrimary(resolved.entry?.model) ??
100+
resolveModelPrimary(resolved.defaults?.model) ??
101+
null;
102+
stageModelShape(runtimeConfig, entry.path, primary, normalized.length > 0 ? normalized : null);
93103
}

0 commit comments

Comments
 (0)