Skip to content

Commit 4498fb9

Browse files
authored
Merge pull request #4157 from lidge-jun/lane-a/1-4129
Maintainer integration on dev under the MAINTAINERS.md policy that lets a maintainer with maintain or admin access land a PR on dev without a second approval, recording the decision and the exact-head CI evidence. Lane A item 1 of the round-2 stack planned in devlog/_plan/260910_post249_round2/020_4129_shadow_combo_failover.md. Exact-head CI at 421aea8, verified by exit code: gh run view 34413236789 --exit-status -> 0 Cross-platform CI gh run view 34413236854 --exit-status -> 0 React Doctor gh run view 34413400622 --exit-status -> 0 Enforce PR target branch gh run view 34413400681 --exit-status -> 0 PR hygiene Twenty-eight checks SUCCESS at that SHA, none failed, none cancelled, none pending. NOT RUN: bun run test, bun run typecheck, bun run build, bun run lint:gui, bun run privacy:scan, bun install. The maintainer set a no-local-suite constraint for this round and remote CI is the only gate.
2 parents a7509fe + 421aea8 commit 4498fb9

2 files changed

Lines changed: 152 additions & 0 deletions

File tree

‎src/server/responses/core.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3303,6 +3303,30 @@ async function handleResponsesInner(
33033303
}
33043304
}
33053305
}
3306+
// A shadow-call replacement that names a COMBO is routing policy, not the identity of any
3307+
// one pick. The late intercept site below resolves it through routeModel/tryPickComboModel,
3308+
// which collapses the table to a single target while still tagging `routeKind: "combo"`, so
3309+
// the combo gate on the next line never fires, handleComboResponses never runs, and 429/5xx
3310+
// hops — which only exist inside that loop — are unreachable (#4129). Rewrite the selector
3311+
// here instead, before comboIdFromRawBody reads `model`, and identify the combo by CONFIG
3312+
// LOOKUP so the check can never observe a one-candidate collapse.
3313+
if (!options.comboAttempt && body && typeof body === "object" && !Array.isArray(body)) {
3314+
const shadowIntercept = config.shadowCallIntercept;
3315+
const rawShadowModel = (body as { model?: unknown }).model;
3316+
if (shadowIntercept?.enabled && shadowIntercept.model && typeof rawShadowModel === "string"
3317+
&& isShadowSourceModel(rawShadowModel, shadowIntercept.sourceModels)) {
3318+
const shadowComboId = resolveComboId(config, shadowIntercept.model);
3319+
if (shadowComboId && Object.hasOwn(config.combos ?? {}, shadowComboId)) {
3320+
(body as Record<string, unknown>).model = shadowIntercept.model;
3321+
// Same rule as the late intercept site: record the operator-configured prefix that
3322+
// matched, never the caller's raw model string. Matching is by prefix, so the raw
3323+
// value is caller-controlled and reaches usage.jsonl and /api/logs.
3324+
logCtx.shadowCallRewrittenFrom = sanitizeLogMetadataString(
3325+
shadowSourceModelPrefix(rawShadowModel, shadowIntercept.sourceModels),
3326+
);
3327+
}
3328+
}
3329+
}
33063330
const comboId = !options.comboAttempt ? comboIdFromRawBody(body, config) : null;
33073331
if (comboId && Object.hasOwn(config.combos ?? {}, comboId)) {
33083332
options.onRequestBodyRead?.();

‎tests/responses/responses-shadow-intercept.test.ts‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,134 @@ describe("shadow call intercept request path (issue #311)", () => {
250250
});
251251
});
252252

253+
/**
254+
* A shadow-call replacement naming a COMBO used to run exactly one attempt and never enter
255+
* the failover loop (#4129). Two cooperating causes: the combo gate reads the UN-rewritten
256+
* body, where the model is still the bare helper slug, and the late intercept resolved the
257+
* replacement through routeModel/tryPickComboModel, which collapses the combo table to a
258+
* single target while still tagging routeKind "combo" — so the reported "combo route, one
259+
* attempt" was a collapsed native pick, and 429/5xx hops (which only exist inside
260+
* handleComboResponses) were unreachable.
261+
*/
262+
function comboInterceptConfig(
263+
targets: Array<{ provider: string; model: string }>,
264+
shadowCallIntercept: Record<string, unknown> = { enabled: true, model: "combo/shadow" },
265+
): OcxConfig {
266+
return {
267+
port: 0,
268+
defaultProvider: "xai",
269+
providers: {
270+
xai: {
271+
adapter: "openai-chat",
272+
baseUrl: "https://api.x.ai/v1",
273+
authMode: "key",
274+
apiKey: "test-xai-key",
275+
},
276+
alt: {
277+
adapter: "openai-chat",
278+
baseUrl: "https://alt.example/v1",
279+
authMode: "key",
280+
apiKey: "test-alt-key",
281+
},
282+
},
283+
combos: {
284+
shadow: { strategy: "failover", targets },
285+
},
286+
shadowCallIntercept,
287+
} as unknown as OcxConfig;
288+
}
289+
290+
function chatOk(text: string): Response {
291+
return Response.json({
292+
choices: [{ index: 0, message: { role: "assistant", content: text }, finish_reason: "stop" }],
293+
usage: { prompt_tokens: 1, completion_tokens: 1 },
294+
});
295+
}
296+
297+
describe("a combo shadow-call target enters the failover loop (#4129)", () => {
298+
test("a helper call rewritten to a combo hops past a 429 to the second target", async () => {
299+
const urls: string[] = [];
300+
const logCtx: RequestLogContext = { model: "", provider: "" };
301+
globalThis.fetch = (async (url: unknown) => {
302+
urls.push(String(url));
303+
return urls.length === 1
304+
? Response.json({ error: { message: "rate limited" } }, { status: 429 })
305+
: chatOk("ok");
306+
}) as typeof fetch;
307+
308+
const config = comboInterceptConfig([
309+
{ provider: "xai", model: "grok-4.5" },
310+
{ provider: "alt", model: "grok-4.5" },
311+
]);
312+
const response = await post(config, "gpt-5.6-luna", "turn", logCtx);
313+
314+
expect(response.ok).toBe(true);
315+
// The whole point: two upstream attempts, in configured order.
316+
expect(urls).toHaveLength(2);
317+
expect(urls[0]).toContain("api.x.ai");
318+
expect(urls[1]).toContain("alt.example");
319+
expect(logCtx.provider).toBe("combo");
320+
expect(logCtx.comboId).toBe("shadow");
321+
expect(logCtx.routeDecision?.routeKind).toBe("combo");
322+
expect(logCtx.shadowCallRewrittenFrom).toBe("gpt-5.6-luna");
323+
const attempts = (logCtx.attempts ?? []) as Array<{ provider?: string; model?: string }>;
324+
expect(attempts).toHaveLength(2);
325+
expect(attempts.map(a => `${a.provider}/${a.model}`))
326+
.toEqual(["xai/grok-4.5", "alt/grok-4.5"]);
327+
});
328+
329+
test("a combo whose first target intersects the source still routes as a combo", async () => {
330+
const urls: string[] = [];
331+
const logCtx: RequestLogContext = { model: "", provider: "" };
332+
globalThis.fetch = (async (url: unknown) => {
333+
urls.push(String(url));
334+
return chatOk("ok");
335+
}) as typeof fetch;
336+
337+
// The #2706 self-target shape: the source model routes to xai, and the combo's FIRST
338+
// target is that same provider+model. shadowCallTargetsIntersect is therefore true for
339+
// the collapsed one-candidate pick, which is what used to suppress the intercept
340+
// outright and leave the request on a plain native route.
341+
const config = comboInterceptConfig(
342+
[
343+
{ provider: "xai", model: "custom-helper" },
344+
{ provider: "alt", model: "grok-4.5" },
345+
],
346+
{ enabled: true, model: "combo/shadow", sourceModels: ["custom-helper"] },
347+
);
348+
const response = await post(config, "custom-helper", "turn", logCtx);
349+
350+
expect(response.ok).toBe(true);
351+
// A healthy first target still costs exactly one upstream call.
352+
expect(urls).toHaveLength(1);
353+
expect(urls[0]).toContain("api.x.ai");
354+
expect(logCtx.provider).toBe("combo");
355+
expect(logCtx.comboId).toBe("shadow");
356+
expect(logCtx.routeDecision?.routeKind).toBe("combo");
357+
// Red before the fix: shouldInterceptShadowCall saw the collapsed pick as a self-target,
358+
// skipped the rewrite, and the request left as a plain native route with no marker.
359+
expect(logCtx.shadowCallRewrittenFrom).toBe("custom-helper");
360+
});
361+
362+
test("a non-combo replacement still takes the ordinary late intercept", async () => {
363+
const urls: string[] = [];
364+
const logCtx: RequestLogContext = { model: "", provider: "" };
365+
globalThis.fetch = (async (url: unknown) => {
366+
urls.push(String(url));
367+
return chatOk("ok");
368+
}) as typeof fetch;
369+
370+
const config = comboInterceptConfig([{ provider: "xai", model: "grok-4.5" }]);
371+
config.shadowCallIntercept = { enabled: true, model: "xai/grok-4.5" };
372+
const response = await post(config, "gpt-5.6-luna", "turn", logCtx);
373+
374+
expect(response.ok).toBe(true);
375+
expect(urls).toHaveLength(1);
376+
expect(logCtx.comboId).toBeUndefined();
377+
expect(logCtx.shadowCallRewrittenFrom).toBe("gpt-5.6-luna");
378+
});
379+
});
380+
253381
/**
254382
* The GUI badge/tooltip used to hard-code "5.4-mini", so it kept naming a model
255383
* Codex no longer sends. The management API is the single source of truth for

0 commit comments

Comments
 (0)