Repository navigation
fix(responses): fail closed for direct Chat routes when mandatory hosted tool_choice is unrepresentable #810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
tests/responses/responses-direct-chat-tool-choice.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { afterEach, beforeEach, expect, test } from "bun:test"; | ||
| import { clearResponseStateForTests, flushResponseState } from "../../src/responses/state"; | ||
| import { closeRequestHistoryIndex } from "../../src/routing/history/indexer"; | ||
| import { handleResponses } from "../../src/server/responses"; | ||
| import type { RequestLogContext } from "../../src/server/request-log"; | ||
| import type { OcxConfig } from "../../src/types"; | ||
| import { acquireOwnedSpendHome } from "../helpers/owned-spend-home"; | ||
| import { createTempHome } from "../helpers/temp-home"; | ||
|
|
||
| const clientTool = { type: "function", name: "shell", parameters: { type: "object", properties: {} } }; | ||
| const mcpTool = { type: "mcp", server_label: "remote", server_url: "https://mcp.fixture.invalid/mcp" }; | ||
| const mcpChoice = { type: "allowed_tools", mode: "required", tools: [{ type: "mcp", server_label: "remote", name: "shell" }] }; | ||
| const originalFetch = globalThis.fetch; | ||
| let home: ReturnType<typeof createTempHome>; | ||
| let releaseHome: () => void; | ||
|
|
||
| beforeEach(() => { | ||
| home = createTempHome("ocx-direct-chat-choice-"); | ||
| releaseHome = acquireOwnedSpendHome(); | ||
| clearResponseStateForTests(); | ||
| globalThis.fetch = (() => { throw new Error("unexpected network access in tool-choice fixture"); }) as typeof fetch; | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| globalThis.fetch = originalFetch; | ||
| await flushResponseState(); | ||
| clearResponseStateForTests(); | ||
| closeRequestHistoryIndex(); | ||
| releaseHome(); | ||
| home.remove(); | ||
| }); | ||
|
|
||
| function fixture() { | ||
| const sent: Array<{ provider: string; body: Record<string, unknown> }> = []; | ||
| const send = (provider: string) => (async (_input, init) => { | ||
| sent.push({ provider, body: JSON.parse(String(init?.body)) }); | ||
| return Response.json(provider === "chat" ? { | ||
| id: "chat_fixture", object: "chat.completion", model: "model", | ||
| choices: [{ index: 0, finish_reason: "tool_calls", message: { role: "assistant", content: null, | ||
| tool_calls: [{ id: "call_fixture", type: "function", function: { name: "shell", arguments: "{}" } }], | ||
| } }], | ||
| usage: { prompt_tokens: 2, completion_tokens: 1, total_tokens: 3 }, | ||
| } : { | ||
| id: "resp_fixture", object: "response", status: "completed", model: "model", | ||
| output: [{ id: "computer_fixture", type: "computer_call", status: "completed" }], | ||
| usage: { input_tokens: 2, output_tokens: 1, total_tokens: 3 }, | ||
| }); | ||
| }) as typeof fetch; | ||
| const config: OcxConfig = { | ||
| port: 0, defaultProvider: "chat", multiAgentGuidanceEnabled: false, | ||
| providers: { | ||
| chat: { adapter: "openai-chat", baseUrl: "https://api.openai.com/v1", authMode: "key", | ||
| apiKey: "fixture-key", models: ["model"], liveModels: false, fetch: send("chat") }, | ||
| responses: { adapter: "openai-responses", baseUrl: "https://responses.fixture.invalid/v1", authMode: "key", | ||
| apiKey: "fixture-key", models: ["model"], liveModels: false, fetch: send("responses") }, | ||
| }, | ||
| routingProfiles: { tools: { candidates: [{ provider: "chat", model: "model" }, { provider: "responses", model: "model" }] } }, | ||
| }; | ||
| return { config, sent }; | ||
| } | ||
|
|
||
| function request(toolChoice: unknown, tools: unknown[], model = "chat/model"): Request { | ||
| return new Request("http://localhost/v1/responses", { | ||
| method: "POST", | ||
| headers: { "content-type": "application/json" }, | ||
| body: JSON.stringify({ model, input: "Use the selected tool.", stream: false, store: false, tools, tool_choice: toolChoice }), | ||
| }); | ||
| } | ||
|
|
||
| test("direct translated Chat rejects an unrepresentable computer_use choice before send", async () => { | ||
| const { config, sent } = fixture(); | ||
| const response = await handleResponses(request({ type: "computer_use" }, [{ type: "computer_use" }, clientTool]), config, {}); | ||
| expect(response.status).toBe(400); | ||
| expect(await response.text()).toContain("mandatory tool_choice cannot be preserved"); | ||
| expect(sent).toEqual([]); | ||
| }); | ||
|
|
||
| test("native Chat cannot replace a required named MCP tool with a same-named client function", async () => { | ||
| const { config, sent } = fixture(); | ||
| const response = await handleResponses(request(mcpChoice, [mcpTool, clientTool]), config, {}); | ||
| expect(response.status, await response.text()).toBe(400); | ||
| expect(sent).toEqual([]); | ||
| }); | ||
|
|
||
| test.each(["required", { type: "function", name: "shell" }, { | ||
| type: "allowed_tools", mode: "required", tools: [{ type: "function", name: "shell" }], | ||
| }])("representable client tool choice %j reaches Chat unchanged", async choice => { | ||
| const { config, sent } = fixture(); | ||
| const response = await handleResponses(request(choice, [clientTool]), config, {}); | ||
| const body = await response.json(); | ||
| expect(response.status).toBe(200); | ||
| expect(body.status).toBe("completed"); | ||
| expect(sent).toHaveLength(1); | ||
| expect(sent[0]!.provider).toBe("chat"); | ||
| expect(sent[0]!.body.tool_choice).toEqual(choice === "required" ? "required" : { type: "function", function: { name: "shell" } }); | ||
| }); | ||
|
|
||
| test("a policy skips incompatible Chat and preserves the mandatory selector for Responses", async () => { | ||
| const { config, sent } = fixture(); | ||
| const choice = { type: "computer_use" }; | ||
| const log: RequestLogContext = {}; | ||
| const response = await handleResponses(request(choice, [{ type: "computer_use" }, clientTool], "policy/tools"), config, log); | ||
| expect(response.status, await response.text()).toBe(200); | ||
| expect(log.routeDecision?.selected.provider).toBe("chat"); | ||
| expect(sent.map(row => row.provider)).toEqual(["responses"]); | ||
| expect(sent[0]!.body.tool_choice).toEqual(choice); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Single-candidate policy returns an internal 409 for an invalid tool choice
When a policy has only an incompatible Chat candidate, this branch returns a marked 409. With no fallback candidate, the client receives
policy_candidate_unavailableinstead of a tool-choice 400.Learn more
A policy candidate refusal is an in-process marker used to authorize the next policy attempt. handleResponsesWithPolicyFallback returns the original response when no further eligible candidate exists. Thus a policy with one Chat candidate exposes the internal 409, while a direct route reports a client-facing 400 for the same mandatory choice.
Example: A profile containing only
chat/modelreceivestool_choice: {"type":"computer_use"}. No request is sent, but the client receives409 policy_candidate_unavailablerather than400 invalid_request_errorexplaining the incompatible choice.Recommended fix: Preserve a client-facing tool-choice refusal for exhaustion in
handleResponsesWithPolicyFallback, while retaining the local marker to authorize hops. Distinguish this new refusal from other existing policy candidate failures so their behavior does not change.Was this helpful? React with 👍 or 👎 to provide feedback.