Skip to content

Commit 6f8b1d6

Browse files
committed
fix(config): honor registry wire defaults and own-property lookup for hosted-tool preferences
Two defects the plan audit found in this change, both reproduced before fixing. Validation disagreed with routing. `modelPreferHostedTools` resolved the effective wire from `registry.adapter` and an explicit `modelAdapters` entry, then stopped. At request time `resolveModelAdapter()` consults the registry's per-model `modelWireDefaults` before falling back to the provider-wide adapter. DeepSeek routes `deepseek-v4-flash` over native Responses for a Responses inbound while its provider-wide wire stays openai-chat, so a valid preference for that model was rejected at config load with "requires the openai-responses wire" — a config the runtime would have honored. The validator now walks the same order. Inherited keys threw before dispatch. `provider.modelPreferHostedTools?. [modelId]` walks the prototype chain, so a routed model id of `constructor` or `toString` resolved to a function and threw `TypeError: ... .includes is not a function` inside `preferConfiguredHostedTools`, failing the request. Lookup is now own-property only and requires an array. Both regressions were driven red first: the DeepSeek case produced the exact rejection above, and the inherited-key case produced the exact TypeError. 247 tests pass across the four affected files, typecheck clean.
1 parent b3329ed commit 6f8b1d6

4 files changed

Lines changed: 81 additions & 7 deletions

File tree

‎src/adapters/openai-responses.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,16 @@ function preferConfiguredHostedTools(
651651
selectedModelId?: string,
652652
): unknown {
653653
// A virtual model's advertised id takes precedence over its resolved wire-model id.
654-
const preferredTools = (selectedModelId ? provider.modelPreferHostedTools?.[selectedModelId] : undefined)
655-
?? provider.modelPreferHostedTools?.[modelId];
654+
// Read own properties only: a routed model id of `constructor`/`toString` would
655+
// otherwise resolve to an inherited Object.prototype function and throw on the
656+
// membership test below, failing the request before it is dispatched.
657+
const preferenceMap = provider.modelPreferHostedTools;
658+
const ownPreference = (key: string | undefined): string[] | undefined => {
659+
if (!key || !preferenceMap || !Object.prototype.hasOwnProperty.call(preferenceMap, key)) return undefined;
660+
const entry = preferenceMap[key];
661+
return Array.isArray(entry) ? entry : undefined;
662+
};
663+
const preferredTools = ownPreference(selectedModelId) ?? ownPreference(modelId);
656664
if (!preferredTools?.includes(HOSTED_IMAGE_GENERATION_TOOL) || !isPlainObject(body)) return body;
657665

658666
const stripGroup = (tools: unknown[]): unknown[] => {

‎src/config.ts‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
type OcxProviderConfig,
3737
} from "./types";
3838
import { isCanonicalOpenAiForwardProvider, OPENAI_CODEX_PROVIDER_ID } from "./providers/openai-tiers";
39-
import { getProviderRegistryEntry } from "./providers/registry";
39+
import { getProviderRegistryEntry, providerModelWireDefault } from "./providers/registry";
4040
import { resolveOpenAiVirtualModel } from "./providers/openai-virtual-models";
4141
import { parseDesktopProfile } from "./claude/desktop-profile";
4242
import { isCodexReasoningEffort, modelRecordValue } from "./reasoning-effort";
@@ -712,7 +712,7 @@ export function modelPreferHostedToolsConfigError(
712712
value: unknown,
713713
field: string,
714714
providerName: string,
715-
provider: { adapter?: unknown; authMode?: unknown; modelAdapters?: unknown },
715+
provider: { adapter?: unknown; authMode?: unknown; modelAdapters?: unknown; baseUrl?: unknown },
716716
): string | null {
717717
if (value === undefined) return null;
718718
if (!value || typeof value !== "object" || Array.isArray(value)) return `${field} must be a plain object`;
@@ -732,9 +732,30 @@ export function modelPreferHostedToolsConfigError(
732732
const pinned = pinnedWireAdapter(providerName, modelId);
733733
if (pinned) return pinned;
734734
const requestedWire = requestedWireFor(modelId);
735-
return typeof requestedWire === "string" && MODEL_ADAPTER_OVERRIDE_ALLOWED.has(requestedWire)
736-
? requestedWire
737-
: currentWire;
735+
if (typeof requestedWire === "string" && MODEL_ADAPTER_OVERRIDE_ALLOWED.has(requestedWire)) {
736+
return requestedWire;
737+
}
738+
// No explicit override: fall back to the registry's per-model wire default before
739+
// the provider-wide adapter, because that is the order `resolveModelAdapter()`
740+
// uses at request time (src/server/adapter-resolve.ts:38-48). Skipping it rejected
741+
// preferences the runtime would have honored — DeepSeek routes `deepseek-v4-flash`
742+
// over native Responses for a Responses inbound while the provider-wide wire stays
743+
// openai-chat. Hosted-tool preferences only apply to Responses traffic, so the
744+
// inbound to ask about is "responses".
745+
const registryDefault = typeof currentWire === "string" && typeof provider.baseUrl === "string"
746+
? providerModelWireDefault(
747+
providerName,
748+
{
749+
baseUrl: provider.baseUrl,
750+
adapter: currentWire,
751+
...(typeof provider.authMode === "string" ? { authMode: provider.authMode as OcxProviderConfig["authMode"] } : {}),
752+
},
753+
modelId,
754+
MODEL_ADAPTER_OVERRIDE_ALLOWED,
755+
"responses",
756+
)
757+
: undefined;
758+
return registryDefault ?? currentWire;
738759
};
739760
for (const [key, entry] of entries) {
740761
if (!key.trim()) return `${field} keys must be nonblank model ids`;

‎tests/config.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,25 @@ describe("opencodex config defaults", () => {
10311031
});
10321032
expect(readConfigDiagnostics().error).toBeNull();
10331033

1034+
// A registry `modelWireDefaults` entry that selects openai-responses for a
1035+
// Responses inbound must be honored by validation, exactly as the runtime
1036+
// honors it. DeepSeek's preset routes `deepseek-v4-flash` over native
1037+
// Responses for a Responses inbound while the provider-wide wire stays
1038+
// openai-chat; validating from `registry.adapter` alone rejected a
1039+
// preference the runtime would have accepted.
1040+
writeConfig({
1041+
port: 12345,
1042+
providers: {
1043+
deepseek: {
1044+
adapter: "openai-chat",
1045+
baseUrl: "https://api.deepseek.com/v1",
1046+
modelPreferHostedTools: { "deepseek-v4-flash": ["image_generation"] },
1047+
},
1048+
},
1049+
defaultProvider: "deepseek",
1050+
});
1051+
expect(readConfigDiagnostics().error).toBeNull();
1052+
10341053
// Registry providers route through their registry wire, not this persisted adapter.
10351054
writeConfig({
10361055
port: 12345,

‎tests/openai-responses-passthrough.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,32 @@ describe("OpenAI Responses hosted-tool name conflicts", () => {
11211121
expect(body.tool_choice).toEqual({ type: "image_generation" });
11221122
});
11231123

1124+
test("an inherited Object.prototype key is not read as a preference", () => {
1125+
// `provider.modelPreferHostedTools?.[modelId]` walks the prototype chain, so a
1126+
// routed model literally named `constructor` or `toString` yielded a function
1127+
// and threw on `.includes` before the request was ever dispatched.
1128+
const adapter = createResponsesPassthroughAdapter({
1129+
...keyedProvider,
1130+
modelPreferHostedTools: { "provider-image-model": ["image_generation"] },
1131+
});
1132+
for (const inheritedKey of ["constructor", "toString", "hasOwnProperty"]) {
1133+
const request = adapter.buildRequest({
1134+
modelId: inheritedKey,
1135+
context: { messages: [] },
1136+
stream: true,
1137+
options: {},
1138+
_rawBody: {
1139+
model: inheritedKey,
1140+
tools: [{ type: "image_generation" }],
1141+
tool_choice: { type: "function", name: "image_gen.imagegen" },
1142+
},
1143+
}, meta);
1144+
const body = JSON.parse(request.body) as { tool_choice: unknown };
1145+
// Unconfigured model: ordinary normalization applies, the hosted preference does not.
1146+
expect(body.tool_choice).toEqual({ type: "function", name: "image_gen.imagegen" });
1147+
}
1148+
});
1149+
11241150
test("configured model rewrites a custom image-gen selector", () => {
11251151
const adapter = createResponsesPassthroughAdapter({
11261152
...keyedProvider,

0 commit comments

Comments
 (0)