Skip to content

Commit a165564

Browse files
committed
fix(management): validate the sidecar pair against the submitted backend
Both web-search sidecar writers resolved the backend for the model pair check with a ternary that handled only openai, anthropic, and null, then fell back to the STORED backend for everything else. The accepted union has five members, so a submitted `gemini` never matched an arm and was validated against whatever was already saved. That is two defects, not one. With a stored openai backend the dashboard could not save a Gemini pair the picker itself offered (#2457, the reported 400), and symmetrically `backend=gemini` with `model=gpt-5.6-luna` was accepted with 200 because it was checked against openai. Writing the same pair directly into config.json always worked, which is what narrowed this to the write gate. Both routes now resolve the submitted backend across the whole union. The union check above each site has already rejected unknown literals, so a surviving string is a member; Array.includes does not narrow, so the cast carries that proof. The two null policies are deliberately NOT unified: on /api/sidecar-settings null unsets the backend and unset resolves to openai, while on /api/claude-code null drops the override and inherits the global backend. Collapsing them into one helper would silently change what clearing a Claude override means. The executor is untouched. resolveSidecarBackend and planWebSearch already handled Gemini, and tests/gemini-web-search.test.ts still passes unchanged. Closes #2457
1 parent 240ac38 commit a165564

3 files changed

Lines changed: 147 additions & 10 deletions

File tree

‎src/server/management/agent-settings-routes.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import {
6060
webSearchCandidateRows,
6161
webSearchModelIsRejected,
6262
webSearchModelRejection,
63+
type WebSearchBackend,
6364
} from "./web-search-sidecar-options";
6465
import { drainAndShutdown } from "../lifecycle";
6566
import { filterRequestLogs, getRequestLogEntries, type RequestLogEntry } from "../request-log";
@@ -1118,13 +1119,18 @@ export async function handleAgentSettingsRoutes(ctx: ManagementContext): Promise
11181119
if (field === "webSearchSidecar"
11191120
&& (section.model !== undefined || section.backend !== undefined)) {
11201121
const stored = config.claudeCode?.webSearchSidecar;
1121-
const effectiveBackend = section.backend === "anthropic"
1122-
? "anthropic"
1123-
: section.backend === "openai"
1124-
? "openai"
1125-
: section.backend === null
1126-
? config.webSearchSidecar?.backend ?? "openai"
1127-
: stored?.backend ?? config.webSearchSidecar?.backend ?? "openai";
1122+
// Validate against the SUBMITTED backend across the whole union, not
1123+
// just openai/anthropic (#2457). allowedBackends above already refused
1124+
// unknown literals; Array.includes does not narrow, hence the cast.
1125+
// null keeps its own meaning here — drop the override and inherit the
1126+
// global backend — which is deliberately NOT the sidecar-settings rule.
1127+
const submittedBackend = section.backend;
1128+
const effectiveBackend = typeof submittedBackend === "string"
1129+
&& allowedBackends.includes(submittedBackend)
1130+
? submittedBackend as WebSearchBackend
1131+
: submittedBackend === null
1132+
? config.webSearchSidecar?.backend ?? "openai"
1133+
: stored?.backend ?? config.webSearchSidecar?.backend ?? "openai";
11281134
const effectiveModel = section.model === ""
11291135
? config.webSearchSidecar?.model
11301136
: typeof section.model === "string"

‎src/server/management/config-routes.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -665,9 +665,18 @@ export async function handleConfigRoutes(ctx: ManagementContext): Promise<Respon
665665

666666
if (body.webSearch) {
667667
const pairTouched = body.webSearch.model !== undefined || body.webSearch.backend !== undefined;
668-
const effectiveBackend = body.webSearch.backend === "anthropic"
669-
? "anthropic"
670-
: body.webSearch.backend === "openai" || body.webSearch.backend === null
668+
// Validate against the backend the caller SUBMITTED, across the whole
669+
// union — not just openai/anthropic (#2457). The union check above has
670+
// already refused unknown literals, so a surviving string is a member;
671+
// Array.includes does not narrow, hence the cast. Falling back to the
672+
// stored backend for xai/gemini/exa both rejected legal pairs and
673+
// accepted illegal ones: a submitted gemini was checked against a stored
674+
// openai. null means "unset the backend", and unset resolves to openai.
675+
const submittedBackend = body.webSearch.backend;
676+
const effectiveBackend = typeof submittedBackend === "string"
677+
&& WEB_SEARCH_BACKENDS_UNION.includes(submittedBackend as never)
678+
? submittedBackend as typeof WEB_SEARCH_BACKENDS_UNION[number]
679+
: submittedBackend === null
671680
? "openai"
672681
: config.webSearchSidecar?.backend ?? "openai";
673682
const effectiveModel = typeof body.webSearch.model === "string"

‎tests/sidecar-settings-web-search-gate.test.ts‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,88 @@ describe("HTTP contract on /api/sidecar-settings", () => {
194194
});
195195
});
196196

197+
// #2457: the picker offers every union backend, but the pair check used to
198+
// collapse the union into openai/anthropic and fall back to the STORED backend
199+
// for anything else. A submitted `gemini` was therefore validated as an OpenAI
200+
// model and 400'd, while writing the identical pair straight into config.json
201+
// worked. The executor was never the problem; only the write gate was.
202+
const antigravityOAuth: OcxProviderConfig = {
203+
adapter: "google",
204+
baseUrl: "https://cloudcode-pa.googleapis.com",
205+
authMode: "oauth",
206+
};
207+
208+
function armAntigravity(): void {
209+
accountSets["google-antigravity"] = {
210+
accounts: [{ id: "acct-antigravity" }],
211+
activeAccountId: "acct-antigravity",
212+
};
213+
const set = accountSets["google-antigravity"] as unknown as {
214+
accounts: Array<{ id: string; credential?: { projectId: string } }>;
215+
};
216+
set.accounts[0].credential = { projectId: "proj-1" };
217+
}
218+
219+
describe("submitted backend drives the pair check (#2457)", () => {
220+
test("PUT switches openai/luna to a gemini pair the picker offers", async () => {
221+
armAntigravity();
222+
managementRows = [{ provider: "google-antigravity", id: "gemini-3.7-flash", disabled: false }];
223+
const cfg = config({
224+
providers: { openai: forward, claude: anthropicOAuth, "google-antigravity": antigravityOAuth },
225+
webSearchSidecar: { backend: "openai", model: "gpt-5.6-luna" },
226+
});
227+
const response = await sidecarSettings(cfg, {
228+
method: "PUT",
229+
body: { webSearch: { backend: "gemini", model: "gemini-3.7-flash" } },
230+
});
231+
expect(response.status).toBe(200);
232+
expect(cfg.webSearchSidecar).toMatchObject({ backend: "gemini", model: "gemini-3.7-flash" });
233+
});
234+
235+
test("PUT accepts a gemini pair when no sidecar is configured at all", async () => {
236+
armAntigravity();
237+
managementRows = [{ provider: "google-antigravity", id: "gemini-3.7-flash", disabled: false }];
238+
const cfg = config({
239+
providers: { openai: forward, claude: anthropicOAuth, "google-antigravity": antigravityOAuth },
240+
});
241+
const response = await sidecarSettings(cfg, {
242+
method: "PUT",
243+
body: { webSearch: { backend: "gemini", model: "gemini-3.7-flash" } },
244+
});
245+
expect(response.status).toBe(200);
246+
expect(cfg.webSearchSidecar).toMatchObject({ backend: "gemini", model: "gemini-3.7-flash" });
247+
});
248+
249+
test("PUT still rejects a gemini model submitted without its backend", async () => {
250+
armAntigravity();
251+
managementRows = [{ provider: "google-antigravity", id: "gemini-3.7-flash", disabled: false }];
252+
const cfg = config({
253+
providers: { openai: forward, claude: anthropicOAuth, "google-antigravity": antigravityOAuth },
254+
webSearchSidecar: { backend: "openai", model: "gpt-5.6-luna" },
255+
});
256+
const response = await sidecarSettings(cfg, {
257+
method: "PUT",
258+
body: { webSearch: { model: "gemini-3.7-flash" } },
259+
});
260+
expect(response.status).toBe(400);
261+
expect(cfg.webSearchSidecar).toEqual({ backend: "openai", model: "gpt-5.6-luna" });
262+
});
263+
264+
test("PUT still rejects a real mismatch inside the widened union", async () => {
265+
armAntigravity();
266+
managementRows = [{ provider: "google-antigravity", id: "gemini-3.7-flash", disabled: false }];
267+
const cfg = config({
268+
providers: { openai: forward, claude: anthropicOAuth, "google-antigravity": antigravityOAuth },
269+
});
270+
const response = await sidecarSettings(cfg, {
271+
method: "PUT",
272+
body: { webSearch: { backend: "gemini", model: "gpt-5.6-luna" } },
273+
});
274+
expect(response.status).toBe(400);
275+
expect(cfg.webSearchSidecar).toBeUndefined();
276+
});
277+
});
278+
197279
describe("xSearch config round-trip (review High)", () => {
198280
test("PUT validates doc limits (400) and persists+echoes a valid block; GET carries it; null clears", async () => {
199281
usableCodexAccounts.add(MAIN_CODEX_ACCOUNT_ID);
@@ -285,3 +367,43 @@ describe("xSearch config round-trip (review High)", () => {
285367
expect(cfg.webSearchSidecar).toEqual({ backend: "openai" });
286368
});
287369
});
370+
371+
// The Claude override is the second writer named in web-search-sidecar-options.ts:
372+
// "a gate on one route and a stale copy on the other is the same as no gate at
373+
// all." It carried the same collapsed ternary, so it needs the same proof (#2457).
374+
describe("claude-code webSearchSidecar override honors the submitted backend (#2457)", () => {
375+
async function claudeCode(cfg: OcxConfig, body: unknown): Promise<Response> {
376+
const url = new URL("http://localhost/api/claude-code");
377+
const request = new Request(url, {
378+
method: "PUT",
379+
headers: { "content-type": "application/json" },
380+
body: JSON.stringify(body),
381+
});
382+
const response = await handleManagementAPI(request, url, cfg);
383+
if (!response) throw new Error("claude-code route did not handle the request");
384+
return response;
385+
}
386+
387+
function geminiConfig(): OcxConfig {
388+
armAntigravity();
389+
managementRows = [{ provider: "google-antigravity", id: "gemini-3.7-flash", disabled: false }];
390+
return config({
391+
providers: { openai: forward, claude: anthropicOAuth, "google-antigravity": antigravityOAuth },
392+
claudeCode: { webSearchSidecar: { backend: "openai", model: "gpt-5.6-luna" } },
393+
});
394+
}
395+
396+
test("PUT persists a gemini override over a stored openai pair", async () => {
397+
const cfg = geminiConfig();
398+
const response = await claudeCode(cfg, { webSearchSidecar: { backend: "gemini", model: "gemini-3.7-flash" } });
399+
expect(response.status).toBe(200);
400+
expect(cfg.claudeCode?.webSearchSidecar).toMatchObject({ backend: "gemini", model: "gemini-3.7-flash" });
401+
});
402+
403+
test("PUT still rejects a mismatched override pair", async () => {
404+
const cfg = geminiConfig();
405+
const response = await claudeCode(cfg, { webSearchSidecar: { backend: "gemini", model: "gpt-5.6-luna" } });
406+
expect(response.status).toBe(400);
407+
expect(cfg.claudeCode?.webSearchSidecar).toEqual({ backend: "openai", model: "gpt-5.6-luna" });
408+
});
409+
});

0 commit comments

Comments
 (0)