Skip to content

Commit d1fcc65

Browse files
committed
fix(tools): enforce routed tool choice policy
1 parent 4ef5350 commit d1fcc65

13 files changed

Lines changed: 213 additions & 47 deletions

File tree

‎src/adapters/openai-chat.ts‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { AdapterRequest, ProviderAdapter } from "./base";
22
import type { AdapterEvent, OcxAssistantMessage, OcxContentPart, OcxMessage, OcxParsedRequest, OcxProviderConfig, OcxTextContent, OcxThinkingContent, OcxToolCall, OcxUsage } from "../types";
3-
import { isAllowedToolChoice, modelInList, namespacedToolName, resolveToolChoiceWireName, toolAllowedByChoice } from "../types";
3+
import { isAllowedToolChoice, modelInList, namespacedToolName, resolveToolChoiceWireName, toolChoiceToolPredicate } from "../types";
44
import { mapReasoningEffort, modelRecordValue } from "../reasoning-effort";
55
import { debugProviderDiagnostic } from "../lib/debug";
66
import { sseFieldValue } from "../lib/sse-decoder";
@@ -612,12 +612,7 @@ function normalizeXaiToolParameters(parameters: unknown): Record<string, unknown
612612

613613
function toolsToChatFormat(parsed: OcxParsedRequest, provider: OcxProviderConfig): unknown[] | undefined {
614614
if (!parsed.context.tools || parsed.context.tools.length === 0) return undefined;
615-
const allowed = isAllowedToolChoice(parsed.options.toolChoice)
616-
? new Set(parsed.options.toolChoice.allowedTools)
617-
: undefined;
618-
const tools = allowed
619-
? parsed.context.tools.filter(t => toolAllowedByChoice(t, allowed))
620-
: parsed.context.tools;
615+
const tools = parsed.context.tools.filter(toolChoiceToolPredicate(parsed.options.toolChoice));
621616
if (tools.length === 0) return undefined;
622617
const xaiTarget = isXaiSchemaTarget(provider);
623618
const formatted = tools.flatMap(t => {

‎src/adapters/tool-catalog-nudge.ts‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import {
2-
isAllowedToolChoice,
32
namespacedToolName,
4-
toolAllowedByChoice,
5-
toolChoiceAliases,
3+
toolChoiceToolPredicate,
64
type OcxRequestOptions,
75
type OcxTool,
86
type OcxProviderConfig,
@@ -18,13 +16,6 @@ function uniqueNames(names: readonly string[]): string[] {
1816
return [...new Set(names.filter(name => name.trim().length > 0))];
1917
}
2018

21-
function toolChoiceAllows(tool: Pick<OcxTool, "namespace" | "name">, toolChoice: OcxRequestOptions["toolChoice"] | undefined): boolean {
22-
if (!toolChoice || toolChoice === "auto" || toolChoice === "required") return true;
23-
if (toolChoice === "none") return false;
24-
if (isAllowedToolChoice(toolChoice)) return toolAllowedByChoice(tool, new Set(toolChoice.allowedTools));
25-
return toolChoiceAliases(tool).includes(toolChoice.name);
26-
}
27-
2819
function isOpenAIOrChatGPTHost(hostname: string): boolean {
2920
return hostname === "openai.com"
3021
|| hostname.endsWith(".openai.com")
@@ -65,7 +56,7 @@ export function buildNonOpenAIToolCatalogNudgeForTools(
6556
toWireName: (tool: Pick<OcxTool, "namespace" | "name">) => string = tool => namespacedToolName(tool.namespace, tool.name),
6657
): string | undefined {
6758
const visibleNames = tools
68-
?.filter(tool => toolChoiceAllows(tool, toolChoice))
59+
?.filter(toolChoiceToolPredicate(toolChoice))
6960
.map(toWireName);
7061
return buildNonOpenAIToolCatalogNudgeFromNames(visibleNames);
7162
}

‎src/images/loop.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { existsSync } from "node:fs";
1515
import { pathToFileURL } from "node:url";
1616
import { createAdapterEventQueue } from "../adapters/run-turn-queue";
1717
import type { AdapterEvent, OcxMessage, OcxParsedRequest, OcxProviderContinuationState, OcxRequestOptions, OcxThinkingContent, OcxUsage, RateLimitRetryPolicy } from "../types";
18-
import { namespacedToolName } from "../types";
18+
import { namespacedToolName, toolChoiceToolPredicate } from "../types";
1919
import type { AttemptRecoveryKind } from "../usage/log";
2020
import { bridgeToResponsesSSE } from "../bridge";
2121
import { clearableDeadline, idleDeadline } from "../lib/abort";
@@ -662,7 +662,9 @@ export async function runWithImageBridge(deps: ImageBridgeDeps): Promise<Respons
662662
const toolNsMap = new Map<string, { namespace: string; name: string }>();
663663
const freeform = new Set<string>();
664664
const toolSearch = new Set<string>();
665+
const toolAllowed = toolChoiceToolPredicate(parsed.options.toolChoice);
665666
for (const t of parsed.context.tools ?? []) {
667+
if (!toolAllowed(t)) continue;
666668
if (t.namespace) toolNsMap.set(namespacedToolName(t.namespace, t.name), { namespace: t.namespace, name: t.name });
667669
if (t.freeform) freeform.add(t.name);
668670
if (t.toolSearch) toolSearch.add(t.name);

‎src/images/plan.ts‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { OcxConfig, OcxParsedRequest, OcxProviderConfig } from "../types";
2+
import { toolChoiceToolPredicate } from "../types";
23
import type { ImageBridgePlan, VideoBridgePlan } from "./types";
34
import { resolveEnvValue } from "../config";
45
import { getProviderRegistryEntry } from "../providers/registry";
@@ -46,6 +47,10 @@ export async function planImageBridge(
4647
): Promise<ImageBridgePlan | undefined> {
4748
if (config.images?.bridgeEnabled !== true) return undefined;
4849
if (!parsed._imageGeneration) return undefined;
50+
const toolNames = new Set(parsed._imageGeneration.toolNames);
51+
toolNames.add(IMAGE_GEN_TOOL_NAME);
52+
const toolAllowed = toolChoiceToolPredicate(parsed.options.toolChoice);
53+
if (![...toolNames].some(name => toolAllowed({ name }))) return undefined;
4954
// Don't intercept for OpenAI native passthrough
5055
const host = (() => { try { return new URL(routedProvider.baseUrl).hostname; } catch { return ""; } })();
5156
if (host === "api.openai.com") return undefined;
@@ -57,9 +62,7 @@ export async function planImageBridge(
5762
const registryEntry = getProviderRegistryEntry("xai");
5863
const pinnedBaseUrl = (registryEntry?.baseUrl ?? "https://api.x.ai/v1").replace(/\/+$/, "");
5964
// The synthetic tool injected into the conversation is named IMAGE_GEN_TOOL_NAME,
60-
// which is what the model will actually call. Merge it with any original hosted tool names.
61-
const toolNames = new Set(parsed._imageGeneration.toolNames);
62-
toolNames.add(IMAGE_GEN_TOOL_NAME);
65+
// which is what the model will actually call. toolNames also retains authorized hosted aliases.
6366
const original = parsed._imageGeneration.originalTool;
6467
const hostedSize = typeof original?.size === "string" ? original.size : undefined;
6568
const hostedQuality = typeof original?.quality === "string" ? original.quality : undefined;
@@ -95,16 +98,6 @@ export async function planVideoBridge(
9598
routedProvider: OcxProviderConfig,
9699
): Promise<VideoBridgePlan | undefined> {
97100
if (config.images?.videoBridgeEnabled !== true) return undefined;
98-
// Don't intercept for OpenAI native passthrough
99-
const host = (() => { try { return new URL(routedProvider.baseUrl).hostname; } catch { return ""; } })();
100-
if (host === "api.openai.com") return undefined;
101-
const found = findXaiProvider(config);
102-
if (!found) return undefined;
103-
const token = resolveXaiImageApiKey(found.provider);
104-
if (!token) return undefined;
105-
// Pin the baseUrl to the registry entry, ignoring any config-level baseUrl override.
106-
const registryEntry = getProviderRegistryEntry("xai");
107-
const pinnedBaseUrl = (registryEntry?.baseUrl ?? "https://api.x.ai/v1").replace(/\/+$/, "");
108101
const toolNames = new Set<string>();
109102
toolNames.add(VIDEO_GEN_TOOL_NAME);
110103
// Collect any existing function tools whose name matches a video_gen alias
@@ -118,6 +111,18 @@ export async function planVideoBridge(
118111
toolNames.add(fnName);
119112
}
120113
}
114+
const toolAllowed = toolChoiceToolPredicate(parsed.options?.toolChoice);
115+
if (![...toolNames].some(name => toolAllowed({ name }))) return undefined;
116+
// Don't intercept for OpenAI native passthrough
117+
const host = (() => { try { return new URL(routedProvider.baseUrl).hostname; } catch { return ""; } })();
118+
if (host === "api.openai.com") return undefined;
119+
const found = findXaiProvider(config);
120+
if (!found) return undefined;
121+
const token = resolveXaiImageApiKey(found.provider);
122+
if (!token) return undefined;
123+
// Pin the baseUrl to the registry entry, ignoring any config-level baseUrl override.
124+
const registryEntry = getProviderRegistryEntry("xai");
125+
const pinnedBaseUrl = (registryEntry?.baseUrl ?? "https://api.x.ai/v1").replace(/\/+$/, "");
121126
const timeoutMs = clampImageTimeoutMs(config.images?.videoTimeoutMs);
122127
const keepRaw = config.images?.artifactsKeepCount;
123128
const artifactsKeepCount =

‎src/server/responses/collaboration.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
} from "../../combos";
2727
import { isInjectionDebugEnabled } from "../../lib/debug-settings";
2828
import { injectionDebugLog } from "../../lib/injection-debug-log";
29-
import { modelInList, namespacedToolName } from "../../types";
29+
import { modelInList, namespacedToolName, toolChoiceToolPredicate } from "../../types";
3030
import type { AdapterEvent, OcxConfig, OcxParsedRequest, OcxProviderConfig, OcxProviderContinuationState, OcxUsage } from "../../types";
3131
import {
3232
forceRefreshOAuthAccessSnapshot,
@@ -108,7 +108,10 @@ export function buildToolBridgeMaps(parsed: OcxParsedRequest, budget?: Translato
108108
const toolNsMap = new Map<string, { namespace: string; name: string }>();
109109
const freeformToolNames = new Set<string>();
110110
const toolSearchToolNames = new Set<string>();
111+
const toolAllowed = toolChoiceToolPredicate(parsed.options.toolChoice);
111112
for (const t of parsed.context.tools ?? []) {
113+
// Upstream output is untrusted: only restore calls for tools the caller authorized.
114+
if (!toolAllowed(t)) continue;
112115
if (t.namespace) {
113116
const wireName = namespacedToolName(t.namespace, t.name);
114117
budget?.chargeRetained(new TextEncoder().encode(JSON.stringify([wireName, t.namespace, t.name])).byteLength, { kind: "retained_collectors" });

‎src/types.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,19 @@ export function isAllowedToolChoice(value: OcxToolChoice | undefined): value is
223223
return typeof value === "object" && value !== null && "allowedTools" in value;
224224
}
225225

226+
/** Compile the request's tool-choice policy into a reusable advertisement/restoration predicate. */
227+
export function toolChoiceToolPredicate(
228+
choice: OcxToolChoice | undefined,
229+
): (tool: Pick<OcxTool, "namespace" | "name">) => boolean {
230+
if (!choice || choice === "auto" || choice === "required") return () => true;
231+
if (choice === "none") return () => false;
232+
if (isAllowedToolChoice(choice)) {
233+
const allowed = new Set(choice.allowedTools);
234+
return tool => toolAllowedByChoice(tool, allowed);
235+
}
236+
return tool => toolChoiceAliases(tool).includes(choice.name);
237+
}
238+
226239
export interface OcxRequestOptions {
227240
maxOutputTokens?: number;
228241
temperature?: number;

‎src/web-search/index.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { OcxConfig, OcxParsedRequest, OcxProviderConfig } from "../types";
2-
import { modelInList } from "../types";
2+
import { modelInList, toolChoiceToolPredicate } from "../types";
33
import type { SidecarSettings } from "./executor";
44
import type { ResolvedOpenAiForwardSidecar } from "../providers/openai-sidecar";
55
import { getAccountSet } from "../oauth/store";
66
import { DEFAULT_STALL_TIMEOUT_SEC } from "../stall-timeout";
7+
import { buildWebSearchTool, extractHostedWebSearch, WEB_SEARCH_TOOL_NAME } from "./synthetic-tool";
78

89
export { runWithWebSearch } from "./loop";
9-
export { buildWebSearchTool, extractHostedWebSearch, WEB_SEARCH_TOOL_NAME } from "./synthetic-tool";
10+
export { buildWebSearchTool, extractHostedWebSearch, WEB_SEARCH_TOOL_NAME };
1011
export { runAnthropicWebSearch, parseAnthropicSidecarSSE } from "./anthropic-executor";
1112

1213
const DEFAULT_SIDECAR_MODEL = "gpt-5.6-luna";
@@ -146,6 +147,7 @@ export function planWebSearch(
146147
openAiSidecar?: ResolvedOpenAiForwardSidecar,
147148
): SidecarPlan | undefined {
148149
if (!parsed._webSearch || isPassthrough) return undefined;
150+
if (!toolChoiceToolPredicate(parsed.options.toolChoice)(buildWebSearchTool())) return undefined;
149151
const cfg = config.webSearchSidecar ?? {};
150152
if (cfg.enabled === false) return undefined;
151153
const timeoutMs = cfg.timeoutMs ?? DEFAULT_TIMEOUT_MS;

‎src/web-search/loop.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { AdapterRequest, IncomingMeta, ProviderAdapter } from "../adapters/base";
22
import type { AdapterEvent, OcxMessage, OcxParsedRequest, OcxProviderConfig, OcxThinkingContent, OcxUsage, RateLimitRetryPolicy } from "../types";
3-
import { namespacedToolName } from "../types";
3+
import { namespacedToolName, toolChoiceToolPredicate } from "../types";
44
import type { AttemptRecoveryKind } from "../usage/log";
55
import { bridgeToResponsesSSE } from "../bridge";
66
import { runWebSearch, type SidecarOutcome, type SidecarOutcomeRecorder, type SidecarSettings } from "./executor";
@@ -693,7 +693,9 @@ export async function runWithWebSearch(deps: WebSearchLoopDeps): Promise<Respons
693693
const toolNsMap = new Map<string, { namespace: string; name: string }>();
694694
const freeform = new Set<string>();
695695
const toolSearch = new Set<string>();
696+
const toolAllowed = toolChoiceToolPredicate(parsed.options.toolChoice);
696697
for (const t of parsed.context.tools ?? []) {
698+
if (!toolAllowed(t)) continue;
697699
if (t.namespace) toolNsMap.set(namespacedToolName(t.namespace, t.name), { namespace: t.namespace, name: t.name });
698700
if (t.freeform) freeform.add(t.name);
699701
if (t.toolSearch) toolSearch.add(t.name);

‎tests/images/plan.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ describe("planImageBridge", () => {
9292
expect(plan!.auth.baseUrl).toBe("https://api.x.ai/v1");
9393
});
9494

95+
test("tool_choice cannot arm an excluded image sidecar", async () => {
96+
const cfg = makeConfig({ xai: { baseUrl: "https://api.x.ai", apiKey: "test-token" } }, { bridgeEnabled: true });
97+
const parsed = makeParsed(true);
98+
99+
parsed.options.toolChoice = "none";
100+
expect(await planImageBridge(cfg, parsed, routed)).toBeUndefined();
101+
parsed.options.toolChoice = { name: "read_file" };
102+
expect(await planImageBridge(cfg, parsed, routed)).toBeUndefined();
103+
parsed.options.toolChoice = { allowedTools: ["read_file"], mode: "required" };
104+
expect(await planImageBridge(cfg, parsed, routed)).toBeUndefined();
105+
106+
parsed.options.toolChoice = { name: "image_gen" };
107+
expect(await planImageBridge(cfg, parsed, routed)).toBeDefined();
108+
109+
parsed._imageGeneration?.toolNames.add("generate_image");
110+
parsed.options.toolChoice = { name: "generate_image" };
111+
expect(await planImageBridge(cfg, parsed, routed)).toBeDefined();
112+
});
113+
95114
test("xAI provider with OAuth only (no API key) → undefined (API-key-only bridge)", async () => {
96115
tokenResult = "fake-oauth-123";
97116
const cfg = makeConfig({ xai: { baseUrl: "https://api.x.ai" } }, { bridgeEnabled: true });

‎tests/reasoning-effort.test.ts‎

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,28 @@ describe("provider-specific reasoning effort mapping", () => {
329329
expect(body).not.toHaveProperty("tool_choice");
330330
});
331331

332-
test("OpenAI-compatible chat keeps tool_choice when tools are present", () => {
332+
test("OpenAI-compatible chat omits tools and tool_choice when tool_choice is none", () => {
333+
const provider: OcxProviderConfig = {
334+
adapter: "openai-chat",
335+
baseUrl: "https://api.neuralwatt.com/v1",
336+
};
337+
338+
const req = createOpenAIChatAdapter(provider).buildRequest({
339+
modelId: "glm-5.2",
340+
context: {
341+
messages: [{ role: "user", content: "hello", timestamp: 0 }],
342+
tools: [{ name: "read_secret", description: "Read", parameters: { type: "object" } }],
343+
},
344+
stream: false,
345+
options: { toolChoice: "none" },
346+
});
347+
const body = JSON.parse(req.body as string) as Record<string, unknown>;
348+
349+
expect(body).not.toHaveProperty("tools");
350+
expect(body).not.toHaveProperty("tool_choice");
351+
});
352+
353+
test("OpenAI-compatible chat advertises only the named tool when the provider downgrades the selector", () => {
333354
const provider: OcxProviderConfig = {
334355
adapter: "openai-chat",
335356
baseUrl: "https://api.moonshot.ai/v1",
@@ -340,14 +361,20 @@ describe("provider-specific reasoning effort mapping", () => {
340361
modelId: "kimi-k2.7-code",
341362
context: {
342363
messages: [{ role: "user", content: "hello", timestamp: 0 }],
343-
tools: [{ name: "run_tests", description: "Run tests", parameters: { type: "object", properties: {} } }],
364+
tools: [
365+
{ name: "run_tests", description: "Run tests", parameters: { type: "object", properties: {} } },
366+
{ name: "read_secret", description: "Read", parameters: { type: "object", properties: {} } },
367+
],
344368
},
345369
stream: false,
346370
options: { toolChoice: { name: "run_tests" } },
347371
});
348-
const body = JSON.parse(req.body as string) as Record<string, unknown>;
372+
const body = JSON.parse(req.body as string) as {
373+
tools: Array<{ function: { name: string } }>;
374+
tool_choice: string;
375+
};
349376

350-
expect(body).toHaveProperty("tools");
377+
expect(body.tools.map(tool => tool.function.name)).toEqual(["run_tests"]);
351378
expect(body.tool_choice).toBe("auto");
352379
});
353380

@@ -411,18 +438,30 @@ describe("provider-specific reasoning effort mapping", () => {
411438
modelId: "umans-kimi-k2.7",
412439
context: {
413440
messages: [{ role: "user", content: "run it", timestamp: 0 }],
414-
tools: [{
415-
namespace: "functions",
416-
name: "exec_command",
417-
description: "Run a command",
418-
parameters: { type: "object", properties: { cmd: { type: "string" } }, required: ["cmd"] },
419-
}],
441+
tools: [
442+
{
443+
namespace: "functions",
444+
name: "exec_command",
445+
description: "Run a command",
446+
parameters: { type: "object", properties: { cmd: { type: "string" } }, required: ["cmd"] },
447+
},
448+
{
449+
namespace: "mcp__secrets",
450+
name: "read_secret",
451+
description: "Read",
452+
parameters: { type: "object" },
453+
},
454+
],
420455
},
421456
stream: false,
422457
options: { toolChoice: { name: "functions.exec_command" } },
423458
});
424-
const body = JSON.parse(req.body as string) as { tool_choice: { function: { name: string } } };
459+
const body = JSON.parse(req.body as string) as {
460+
tools: Array<{ function: { name: string } }>;
461+
tool_choice: { function: { name: string } };
462+
};
425463

464+
expect(body.tools.map(tool => tool.function.name)).toEqual(["functions__exec_command"]);
426465
expect(body.tool_choice.function.name).toBe("functions__exec_command");
427466
});
428467

0 commit comments

Comments
 (0)