Skip to content

Commit 0d9a3c8

Browse files
committed
Merge remote-tracking branch 'origin/dev' into lane/pr-8074b
2 parents 968a481 + f7c2d32 commit 0d9a3c8

4 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎src/server/responses-undeclared-tool-guard.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
normalizeDeclaredToolName,
1515
} from "../types";
1616
import { replaceSseDataPayload, sseDataPayload, type SseBlockRewrite } from "./sse-payload-rewrite";
17+
import { TOOL_SEARCH_FUNCTION_NAME } from "../responses/tool-search-compat";
1718

1819
/** Item types the client executes through a request-declared wire name. */
1920
export const CLIENT_EXECUTED_CALL_TYPES: ReadonlySet<string> = new Set([
@@ -189,6 +190,28 @@ export function collectDeclaredWireToolNames(body: unknown, options?: { canonica
189190
return names;
190191
}
191192

193+
/**
194+
* Function names that translated adapters give to nameless hosted declarations in this catalog.
195+
* The parser exposes a declared `{type:"tool_search"}` to Chat-style providers as the function
196+
* `tool_search`, so only a declaration present in this (current-turn) body authorizes that name.
197+
*/
198+
export function collectTranslatedHostedToolNames(body: unknown): Set<string> {
199+
const names = new Set<string>();
200+
if (!isPlainObject(body)) return names;
201+
const specGroups: unknown[] = [body.tools];
202+
if (Array.isArray(body.input)) {
203+
for (const item of body.input) {
204+
if (isPlainObject(item) && item.type === "additional_tools") specGroups.push(item.tools);
205+
}
206+
}
207+
for (const specs of specGroups) {
208+
if (Array.isArray(specs) && specs.some(spec => isPlainObject(spec) && spec.type === "tool_search")) {
209+
names.add(TOOL_SEARCH_FUNCTION_NAME);
210+
}
211+
}
212+
return names;
213+
}
214+
192215
/**
193216
* Collects explicitly declared bare wire tool names from a Responses request body.
194217
*

‎src/server/responses/request-prepare.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ import {
158158
currentTurnWireToolCatalogBody,
159159
hasExplicitEmptyToolCatalog,
160160
collectDeclaredWireToolNames,
161+
collectTranslatedHostedToolNames,
161162
collectDeclaredBareCustomWireToolNames,
162163
} from "../responses-undeclared-tool-guard";
163164
import { streamingContextOverflowResponse } from "./context-overflow";
@@ -498,10 +499,12 @@ export async function prepareResponsesRequest(
498499
const catalogBody = options.directRequest && parsed.context.tools?.length
499500
? { tools: parsed.context.tools.map(tool => ({ type: "function", name: tool.name })) } : parsed._rawBody;
500501
const currentCatalog = currentTurnWireToolCatalogBody(catalogBody, parsed._replayPrefixLen);
502+
// A hosted tool declared this turn authorizes the function name its translation uses.
503+
const translatedHostedNames = collectTranslatedHostedToolNames(currentCatalog);
501504
clientToolCatalogRestriction = {
502-
declaredToolNames: collectDeclaredWireToolNames(currentCatalog),
505+
declaredToolNames: new Set([...collectDeclaredWireToolNames(currentCatalog), ...translatedHostedNames]),
503506
bareCustomToolNames: collectDeclaredBareCustomWireToolNames(currentCatalog),
504-
canonicalToolNames: collectDeclaredWireToolNames(currentCatalog, { canonicalOnly: true }),
507+
canonicalToolNames: new Set([...collectDeclaredWireToolNames(currentCatalog, { canonicalOnly: true }), ...translatedHostedNames]),
505508
// Ordinary Responses validation stays with its delivery owner; replay cannot add authority.
506509
enforceDeclaredToolNames: (inboundWire === "responses" && (parsed._replayPrefixLen ?? 0) > 0)
507510
|| options.clientDeclaredEmptyToolCatalog === true

‎structure/transports/response-turn-membership.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ tool names for output normalization. Responses enforces membership in that catal
2424
Chat and Messages enforce it when the caller explicitly empties a catalog, while
2525
omitted and partial catalogs retain deferred delivery. Declarations prepended while
2626
expanding `previous_response_id` remain history and cannot grant current-turn membership.
27+
A nameless hosted declaration that translated adapters expose as a function (`tool_search`)
28+
adds that function name to the snapshot only when the current turn declares it; a replayed
29+
declaration still grants nothing.
2730
Delivery intersects current names with the existing selection-filtered bridge names, so
2831
`tool_choice` cannot be widened by the snapshot. Ordinary Responses validation retains its
2932
delivery owner and diagnostics; the additional guard enforces explicit-empty catalogs and

‎tests/responses/explicit-empty-tool-catalog-history.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const REFUSAL_CODE = "undeclared_tool_call";
2121
const userInput = { role: "user", content: [{ type: "input_text", text: "fixture request" }] };
2222
const tool = (name: string) => ({ type: "function", name, parameters: { type: "object", properties: {} } });
2323
const additional = (names: string[]) => ({ type: "additional_tools", role: "developer", tools: names.map(tool) });
24+
const hostedToolSearch = { type: "additional_tools", role: "developer", tools: [{ type: "tool_search" }] };
2425

2526
let home: ReturnType<typeof createTempHome>;
2627
let releaseHome: (() => void) | undefined;
@@ -162,6 +163,29 @@ describe("explicit empty catalog current-turn and continuation boundaries", () =
162163
}
163164
}
164165

166+
for (const lane of ["adapter", "runTurn"] as const) for (const wire of [undefined, "chat"] as const) {
167+
for (const current of [false, true]) {
168+
test(`${lane}, ${wire ?? "responses"}: ${current ? "current" : "replayed"} hosted tool_search ${current ? "authorizes" : "cannot authorize"} its translated call`, async () => {
169+
const config = setup(lane);
170+
const prior = await post(config, wire, false, { input: [hostedToolSearch, userInput] });
171+
expect(prior.status).toBe("completed");
172+
expect(dispatched[0]!.tools).toContain("tool_search");
173+
nextCall = "tool_search";
174+
const response = await post(config, wire, false, {
175+
previous_response_id: prior.id, tools: [], input: [...(current ? [hostedToolSearch] : []), userInput],
176+
});
177+
expect(dispatched[1]!.replayPrefix).toBeGreaterThan(0);
178+
expect(dispatched[1]!.tools).toContain("tool_search");
179+
if (!current) assertRefusedAndUncached(response);
180+
else {
181+
expect(response.status).toBe("completed");
182+
expect(response.error).toBeUndefined();
183+
expect(JSON.stringify(response.output)).toContain("tool_search");
184+
}
185+
});
186+
}
187+
}
188+
165189
for (const lane of ["adapter", "runTurn"] as const) for (const wire of ["chat", "anthropic"] as const) {
166190
for (const stream of [false, true]) {
167191
const label = `${lane}, ${wire}, ${stream ? "SSE" : "JSON"}`;

0 commit comments

Comments
 (0)