Skip to content

Commit f9c224b

Browse files
committed
fix(routing): resolve model-keyed lookups the way the runtime resolves them
Two evidence surfaces read per-model config with a bare map lookup while the runtime resolves the same keys through modelRecordValue, so family and case overrides were invisible to them and a prototype-shaped id resolved an Object.prototype member instead of missing. Routing capability evidence therefore gave gpt-oss:120b the provider-wide 8k window instead of the gpt-oss family's 131072, and ignored noVisionModels - values that select candidates, not just logs. The Lab behavior report missed the same overrides, and "constructor" resolved to Object.prototype.constructor, which made jcsStringify throw and silently dropped Lab subjects. Exact-own maps (modelPreferHostedTools, modelOpenRouterRouting) deliberately do not family-spread; that boundary is pinned by tests. Both patches are @ntdatt812's work from #2100 and #2077, applied unchanged. Closes #2100 Closes #2077
1 parent 8d828bd commit f9c224b

4 files changed

Lines changed: 345 additions & 12 deletions

File tree

‎src/routing/capability.ts‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* how that affects eligibility.
1111
*/
1212

13-
import type { OcxConfig } from "../types";
13+
import { modelInList, type OcxConfig } from "../types";
1414
import { isCanonicalOpenAiForwardProvider, OPENAI_CODEX_PROVIDER_ID } from "../providers/openai-tiers";
1515
import { serviceTierSupportForModel } from "../providers/service-tier";
1616
import { PROVIDER_REGISTRY } from "../providers/registry";
@@ -21,6 +21,7 @@ import {
2121
nativeReasoningEfforts,
2222
} from "../codex/catalog/metadata";
2323
import { readCatalog, readCodexCatalogPath } from "../codex/catalog/parsing";
24+
import { modelRecordValue } from "../reasoning-effort";
2425
import { statSync } from "node:fs";
2526
import type { RouteCapabilityEvidence } from "./trace";
2627

@@ -159,9 +160,14 @@ export function candidateCapabilityEvidence(
159160
const catalogRow = cachedCatalogModels().find(model => model.provider === providerName && model.id === modelId);
160161
const isNative = providerName === OPENAI_CODEX_PROVIDER_ID && !modelId.includes("/");
161162

162-
const rawContextWindow = provider?.modelContextWindows?.[modelId]
163+
// `modelRecordValue`, not a bare lookup: every runtime reader of these three maps
164+
// resolves them that way, so a `gpt-oss` entry covers `gpt-oss:120b`. Reading raw
165+
// made the evidence disagree with the resolver it claims to describe — and for the
166+
// window it did not even degrade to unknown, it fell through to the provider-wide
167+
// value, which is a definite wrong answer rather than an absent one.
168+
const rawContextWindow = modelRecordValue(provider?.modelContextWindows, modelId)
163169
?? provider?.contextWindow
164-
?? registryEntry?.modelContextWindows?.[modelId]
170+
?? modelRecordValue(registryEntry?.modelContextWindows, modelId)
165171
?? catalogRow?.contextWindow
166172
?? (isNative ? nativeOpenAiContextWindow(modelId, nativeContextLimits(config)) : undefined);
167173
// Native rows go through the accessor (raise-to-ceiling + opt-in). Routed rows keep
@@ -170,10 +176,21 @@ export function candidateCapabilityEvidence(
170176
? (nativeOpenAiContextWindow(modelId, nativeContextLimits(config)) ?? rawContextWindow)
171177
: rawContextWindow;
172178

173-
const modalities = provider?.modelInputModalities?.[modelId]
174-
?? registryEntry?.modelInputModalities?.[modelId]
175-
?? catalogRow?.inputModalities
176-
?? (isNative ? nativeInputModalities(modelId) : undefined);
179+
// `noVisionModels` is checked before the modality chain because that is the order
180+
// `isModelTextOnly` uses: it matches the no-vision list and returns true before it
181+
// ever reads `modelInputModalities` (`src/vision/index.ts:32`). So a `gpt-oss`
182+
// no-vision entry beats an exact `gpt-oss:120b` entry that lists "image", and
183+
// deriving `image` from the modality chain alone reported vision on a model the
184+
// runtime refuses it for. That matters more here than on the CLI surface fixed in
185+
// #2086: routing *acts* on this evidence, so it would select the candidate for image
186+
// work that execution then rejects.
187+
const noVision = modelInList(provider?.noVisionModels, modelId);
188+
const modalities = noVision
189+
? ["text"]
190+
: (modelRecordValue(provider?.modelInputModalities, modelId)
191+
?? modelRecordValue(registryEntry?.modelInputModalities, modelId)
192+
?? catalogRow?.inputModalities
193+
?? (isNative ? nativeInputModalities(modelId) : undefined));
177194
const image = Array.isArray(modalities)
178195
? modalities.includes("image")
179196
: undefined;
@@ -196,8 +213,8 @@ export function candidateCapabilityEvidence(
196213
|| provider?.parallelToolCalls === true
197214
|| undefined;
198215

199-
const reasoningEfforts = provider?.modelReasoningEfforts?.[modelId]
200-
?? registryEntry?.modelReasoningEfforts?.[modelId]
216+
const reasoningEfforts = modelRecordValue(provider?.modelReasoningEfforts, modelId)
217+
?? modelRecordValue(registryEntry?.modelReasoningEfforts, modelId)
201218
?? (isNative ? nativeReasoningEfforts(modelId) : undefined);
202219

203220
const tierSupport = provider

‎src/routing/compatibility/behavior.ts‎

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { modelRecordValue } from "../../reasoning-effort";
12
import { modelInList } from "../../types";
23
import type { OcxConfig, OcxProviderConfig } from "../../types";
34
import { PROVIDER_REGISTRY } from "../../providers/registry";
@@ -51,8 +52,45 @@ function includesModel(list: string[] | undefined, modelId: string): boolean {
5152
return modelInList(list, modelId);
5253
}
5354

55+
/**
56+
* Per-model override lookup for the nine family-aware report rows.
57+
*
58+
* Delegates to modelRecordValue so the report reads these maps the way the
59+
* runtime does -- own properties only, then the pre-colon family, then a
60+
* case-folded key. A bare index disagreed on all three: it missed the
61+
* `gpt-oss` entry ollama-cloud's `gpt-oss:120b` actually resolves, missed a
62+
* differently-cased key, and walked the prototype chain, so a routed model id
63+
* of `constructor`/`toString` yielded an Object.prototype function. That last
64+
* one made buildBehaviorFingerprintV1 throw ("unsupported value type
65+
* function"); the caller catches it (`src/routing/compatibility/subject.ts:125`)
66+
* and returns no route, so the subject is silently dropped -- and the linker
67+
* contract says implementations do not throw.
68+
*
69+
* Not every override map belongs here. `modelPreferHostedTools` and
70+
* `modelOpenRouterRouting` are exact-own at runtime and go through
71+
* `exactOwnValue` below; widening those to the family would be this same bug
72+
* with the sign flipped.
73+
*/
5474
function modelValue<T>(map: Record<string, T> | undefined, modelId: string): T | undefined {
55-
return map?.[modelId];
75+
return modelRecordValue(map, modelId);
76+
}
77+
78+
/**
79+
* Exact, own-property lookup for the two maps the runtime resolves that way.
80+
*
81+
* `modelPreferHostedTools` and `modelOpenRouterRouting` are deliberately exact: the
82+
* adapter reads the first through `hasOwnProperty`
83+
* (`src/adapters/openai-responses.ts:1001`) and the second through `Object.hasOwn`
84+
* (`src/providers/openrouter-routing.ts:89`), and the type documents the first as
85+
* "Exact-model hosted tools" (`src/types.ts:1584`). Sending them through
86+
* `modelRecordValue` would make the report say a `gpt-oss` entry applies to
87+
* `gpt-oss:120b` when the adapter will never apply it -- the same divergence this
88+
* file exists to remove, pointed the other way. A bare index is not the answer
89+
* either: it walks the prototype chain, which is the bug `modelValue` just fixed.
90+
* Neither existing primitive is right for these two, so this is the third one.
91+
*/
92+
function exactOwnValue<T>(map: Record<string, T> | undefined, modelId: string): T | undefined {
93+
return map !== undefined && Object.hasOwn(map, modelId) ? map[modelId] : undefined;
5694
}
5795

5896
const CREDENTIAL_HEADER = /(authorization|api[-_]?key|token|secret|credential|cookie)/i;
@@ -69,7 +107,7 @@ function nonCredentialHeaderDigest(
69107
}
70108

71109
function effectiveOpenRouterRouting(effective: OcxProviderConfig, modelId: string) {
72-
return effective.modelOpenRouterRouting?.[modelId] ?? effective.openRouterRouting;
110+
return exactOwnValue(effective.modelOpenRouterRouting, modelId) ?? effective.openRouterRouting;
73111
}
74112

75113
/**
@@ -184,7 +222,7 @@ export function resolveProductionBehaviorValues(
184222
effective.parallelToolCalls ?? (upstreamProtocol === "openai-chat"),
185223
),
186224
"tools.hostedPreference": behaviorRow("provider_config", {
187-
tools: modelValue(effective.modelPreferHostedTools, modelId) ?? [],
225+
tools: exactOwnValue(effective.modelPreferHostedTools, modelId) ?? [],
188226
}),
189227
"tools.builtinNameEscaping": behaviorRow("provider_config", effective.escapeBuiltinToolNames === true),
190228
"cache.forwarding": behaviorRow("provider_config", effective.promptCacheKey === true),
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { candidateCapabilityEvidence } from "../src/routing/capability";
3+
import { PROVIDER_REGISTRY } from "../src/providers/registry";
4+
import { modelRecordValue } from "../src/reasoning-effort";
5+
import { isModelTextOnly } from "../src/vision";
6+
import type { OcxConfig, OcxProviderConfig } from "../src/types";
7+
8+
/**
9+
* `candidateCapabilityEvidence` describes what the resolver will do with a candidate,
10+
* so it has to match the resolver. Every runtime reader of `modelContextWindows`,
11+
* `modelInputModalities` and `modelReasoningEfforts` goes through `modelRecordValue`
12+
* (`src/reasoning-effort.ts:108`, `src/server/effort-policy.ts:122`,
13+
* `src/vision/index.ts:34`, `src/codex/catalog/provider-fetch.ts:612`), which accepts a
14+
* family entry for a tagged id. This file pins the evidence to that same rule.
15+
*
16+
* The window matters most: a bare lookup did not degrade to unknown there, it fell
17+
* through to the provider-wide `contextWindow` — a definite wrong answer, which the
18+
* module's own "unknown is not zero" contract is written to avoid.
19+
*/
20+
21+
function providerWithFamilyEntries(): OcxProviderConfig {
22+
return {
23+
adapter: "openai-chat",
24+
baseUrl: "https://example.test/v1",
25+
contextWindow: 8_000,
26+
models: ["gpt-oss:120b"],
27+
modelContextWindows: { "gpt-oss": 131_072 },
28+
modelInputModalities: { "gpt-oss": ["text"] },
29+
modelReasoningEfforts: { "gpt-oss": ["low", "high"] },
30+
} as unknown as OcxProviderConfig;
31+
}
32+
33+
function configFor(provider: OcxProviderConfig): OcxConfig {
34+
return { providers: { custom: provider } } as unknown as OcxConfig;
35+
}
36+
37+
describe("candidateCapabilityEvidence model matching", () => {
38+
test("a family entry covers its tagged siblings, as the resolver does", () => {
39+
const provider = providerWithFamilyEntries();
40+
41+
// Ground truth first: what the runtime itself resolves off this config.
42+
expect(modelRecordValue(provider.modelContextWindows, "gpt-oss:120b")).toBe(131_072);
43+
expect(isModelTextOnly(provider, "gpt-oss:120b")).toBe(true);
44+
45+
const evidence = candidateCapabilityEvidence(configFor(provider), "custom", "gpt-oss:120b");
46+
expect(evidence.contextWindow).toBe(131_072);
47+
expect(evidence.image).toBe(false);
48+
expect(evidence.reasoningEfforts).toEqual(["low", "high"]);
49+
});
50+
51+
test("the window does not fall through to the provider-wide value", () => {
52+
// The specific regression: the provider-wide 8_000 is not "unknown", it is a
53+
// definite answer belonging to a different model, and routing would act on it.
54+
// Asserted as the exact expected number rather than `not.toBe(8_000)`, which
55+
// would also pass for `undefined` or any other wrong value.
56+
const evidence = candidateCapabilityEvidence(
57+
configFor(providerWithFamilyEntries()),
58+
"custom",
59+
"gpt-oss:120b",
60+
);
61+
expect(evidence.contextWindow).toBe(131_072);
62+
});
63+
64+
test("an exact entry still wins over the family entry", () => {
65+
const provider = {
66+
...providerWithFamilyEntries(),
67+
modelContextWindows: { "gpt-oss": 131_072, "gpt-oss:20b": 32_000 },
68+
} as unknown as OcxProviderConfig;
69+
expect(candidateCapabilityEvidence(configFor(provider), "custom", "gpt-oss:20b").contextWindow)
70+
.toBe(32_000);
71+
});
72+
73+
test("an unrelated model still falls back to the provider-wide window", () => {
74+
const evidence = candidateCapabilityEvidence(
75+
configFor(providerWithFamilyEntries()),
76+
"custom",
77+
"some-other-model",
78+
);
79+
expect(evidence.contextWindow).toBe(8_000);
80+
expect(evidence.reasoningEfforts).toBeUndefined();
81+
});
82+
83+
test("a registry entry covers its tagged siblings with no provider configured", () => {
84+
// The three registry lookups (capability.ts lines 170/180/206) are a separate branch
85+
// from the configured-provider ones above: they are only reached when the provider is
86+
// absent from the config, which every other case here supplies.
87+
const registryEntry = PROVIDER_REGISTRY.find(entry => entry.id === "xai");
88+
if (!registryEntry) throw new Error("fixture drift: no `xai` entry in PROVIDER_REGISTRY");
89+
90+
// Pin the fixture's shape rather than its values, so registry churn does not turn
91+
// into a false failure here while real drift still does.
92+
const family = "grok-4.6";
93+
expect(registryEntry.modelContextWindows?.[family]).toBeNumber();
94+
expect(registryEntry.modelInputModalities?.[family]).toBeArray();
95+
expect(registryEntry.modelReasoningEfforts?.[family]).toBeArray();
96+
97+
const emptyConfig = { providers: {} } as unknown as OcxConfig;
98+
const evidence = candidateCapabilityEvidence(emptyConfig, "xai", `${family}:latest`);
99+
100+
expect(evidence.contextWindow).toBe(registryEntry.modelContextWindows![family]);
101+
expect(evidence.image).toBe(registryEntry.modelInputModalities![family].includes("image"));
102+
expect(evidence.reasoningEfforts).toEqual(registryEntry.modelReasoningEfforts![family]);
103+
});
104+
105+
test("noVisionModels beats an exact modality entry, as isModelTextOnly does", () => {
106+
// `isModelTextOnly` matches the no-vision list and returns true before it ever
107+
// reads `modelInputModalities`, so the `gpt-oss` no-vision entry wins over an
108+
// exact `gpt-oss:120b` entry listing "image". Evidence that disagrees here is
109+
// worse than a wrong window: routing selects the candidate for image work and
110+
// execution then refuses it.
111+
const provider = {
112+
...providerWithFamilyEntries(),
113+
noVisionModels: ["gpt-oss"],
114+
modelInputModalities: { "gpt-oss:120b": ["text", "image"] },
115+
} as unknown as OcxProviderConfig;
116+
117+
// Ground truth first: the resolver this evidence claims to describe says text-only.
118+
expect(isModelTextOnly(provider, "gpt-oss:120b")).toBe(true);
119+
120+
const evidence = candidateCapabilityEvidence(configFor(provider), "custom", "gpt-oss:120b");
121+
expect(evidence.image).toBe(false);
122+
});
123+
124+
test("a model outside noVisionModels keeps its declared image modality", () => {
125+
// The negative half: the no-vision check must not spread to models the list does
126+
// not cover, or the fix would trade a false positive for a false negative.
127+
const provider = {
128+
...providerWithFamilyEntries(),
129+
models: ["gpt-oss:120b", "llava:13b"],
130+
noVisionModels: ["gpt-oss"],
131+
modelInputModalities: { "llava:13b": ["text", "image"] },
132+
} as unknown as OcxProviderConfig;
133+
134+
expect(isModelTextOnly(provider, "llava:13b")).toBe(false);
135+
expect(candidateCapabilityEvidence(configFor(provider), "custom", "llava:13b").image).toBe(true);
136+
});
137+
138+
test("a prototype-shaped model id resolves nothing", () => {
139+
// modelRecordValue uses hasOwnProperty; a bare lookup would return Object.prototype
140+
// members here and hand routing a function as evidence.
141+
for (const modelId of ["constructor", "toString", "valueOf", "hasOwnProperty"]) {
142+
const evidence = candidateCapabilityEvidence(
143+
configFor(providerWithFamilyEntries()),
144+
"custom",
145+
modelId,
146+
);
147+
expect(evidence.contextWindow).toBe(8_000);
148+
expect(evidence.reasoningEfforts).toBeUndefined();
149+
expect(evidence.image).toBeUndefined();
150+
}
151+
});
152+
});

0 commit comments

Comments
 (0)