Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/server/responses-ordering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,32 @@ export interface SeparatorManifest {
body: string;
}

/** The Responses hosted-call item types `responsesToCore` flattens into
* `contentType: "reasoning"` cores (kernel/src/wire/responses.ts). On the wire
* these are tool-call items, NOT reasoning items: a `web_search_call` must stay
* in the same assistant run as its `reasoning` item (the server rejects a
* `web_search_call` "without its required 'reasoning' item" once a user
* separator splits the run). Classifying them as a second reasoning would
* insert a spurious separator and break the run (#7296). */
const RESPONSES_HOSTED_CALL_TYPES = new Set([
"computer_call",
"computer_call_output",
"file_search_call",
"web_search_call",
"image_generation_call",
"code_interpreter_call",
"mcp_call",
]);

/** True when a `contentType: "reasoning"` core is actually a flattened hosted
* call (its `rawResponsesItem` sidecar is a hosted-call item), not a real
* reasoning item. Only the sidecar's wire `type` decides — never the core text
* or id, which the codec re-derives and a client could shape. */
function isHostedCallCore(m: CoreMessage): boolean {
const raw = (m as BiliMessage).rawResponsesItem as { type?: unknown } | undefined;
return typeof raw?.type === "string" && RESPONSES_HOSTED_CALL_TYPES.has(raw.type);
}

// #564: folding + stripKernelSummaries can merge two assistant turns into one
// run, which Responses rejects (run order reasoning* -> message* ->
// function_call*, <=1 reasoning). Rebuild boundaries from the ORIGINAL history
Expand Down Expand Up @@ -187,7 +213,13 @@ export function repairResponsesAssistantOrdering(
seenReasoning = false;
continue;
}
const kind = m.contentType === "reasoning" ? "reasoning" : m.contentType === "tool-call" ? "tool-call" : "message";
// A flattened hosted call carries contentType "reasoning" but is a
// tool-call on the wire (#7296): classify it as tool-call (phase 2) so a
// `reasoning -> web_search_call` run stays intact instead of getting a
// spurious user separator between the two.
const kind = m.contentType === "reasoning"
? (isHostedCallCore(m) ? "tool-call" : "reasoning")
: m.contentType === "tool-call" ? "tool-call" : "message";
const r = runOf.get(m.id);
if (kind === "reasoning" && r !== undefined && runHasBody.get(r) && survivorCount.get(r) === 1) continue;
if ((kind === "reasoning" && (phase > 0 || seenReasoning)) || (kind === "message" && phase === 2)) pushSeparator();
Expand Down
55 changes: 55 additions & 0 deletions tests/issue564-responses-ordering.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ const asstMsg = (id: string, text: string): CoreMessage => ({ id, role: "assista
const reasoningMsg = (id: string, enc: string): CoreMessage & { rawResponsesItem: Record<string, unknown> } => ({ id, role: "assistant", contentType: "reasoning", text: id, rawResponsesItem: { type: "reasoning", id, encrypted_content: enc, summary: [] } });
const toolCall = (id: string, callId: string, name: string, args: string): CoreMessage => ({ id, role: "assistant", contentType: "tool-call", toolCallId: callId, toolName: name, text: args });
const toolResult = (id: string, callId: string, out: string): CoreMessage => ({ id, role: "tool", contentType: "tool-result", toolCallId: callId, text: out });
// #7296: the kernel flattens a hosted call (web_search_call, file_search_call,
// mcp_call, ...) into a contentType:"reasoning" core whose rawResponsesItem is
// the original wire item. coreToResponses re-emits that raw item verbatim.
const hostedCall = (id: string, type: string, extra: Record<string, unknown> = {}): CoreMessage & { rawResponsesItem: Record<string, unknown> } =>
({ id, role: "assistant", contentType: "reasoning", text: `opaque:${id}`, rawResponsesItem: { type, id, status: "completed", ...extra } });

function wireItems(core: CoreMessage[]): Item[] {
return coreToResponses(core) as Item[];
Expand Down Expand Up @@ -263,6 +268,56 @@ test("#564: a surviving acp_summary carrier is untouched (no spurious separator)
assertValidAssistantOrdering(wireItems(out), "carrier");
});

test("#7296: a hosted web_search_call stays in the same run as its reasoning (no spurious separator)", () => {
// The kernel flattens hosted calls into contentType:"reasoning" cores. The
// ordering repair must classify them as tool-call (phase 2), NOT as a second
// reasoning — otherwise it inserts a user separator between the reasoning and
// the hosted call, splitting the assistant run and making the server reject
// the web_search_call "without its required 'reasoning' item".
const original: CoreMessage[] = [
userMsg("u1", "Find a public reference."),
reasoningMsg("rs_fixture", "ENC_RS"),
hostedCall("ws_fixture", "web_search_call", { action: { type: "search", query: "fixture query" } }),
];
const out = repairResponsesAssistantOrdering([...original], original);
assert.equal(out.slots.length, 0, "no separator inserted between reasoning and hosted call");
assert.deepEqual(out.messages.map((m) => m.id), ["u1", "rs_fixture", "ws_fixture"], "nothing added or dropped");
const items = wireItems(out.messages);
assert.deepEqual(
items.map((i) => (i.type === "message" ? `message:${i.role}` : i.type)),
["message:user", "reasoning", "web_search_call"],
"reasoning + hosted call render as one intact assistant run",
);
assertValidAssistantOrdering(items, "#7296 hosted-call");
});

test("#7296: the separator still lands between distinct runs, not before a hosted call", () => {
// Two original assistant runs (split by a user message that folding removes):
// run 1 = reasoning + hosted call, run 2 = reasoning + message. The repair
// must keep run 1 intact (no separator between its reasoning and hosted call)
// and still separate run 2 (separator before run 2's reasoning).
const original: CoreMessage[] = [
userMsg("u1", "q1"),
reasoningMsg("rs1", "ENC_1"), hostedCall("ws1", "web_search_call", { action: { type: "search", query: "q" } }),
userMsg("u2", "q2"),
reasoningMsg("rs2", "ENC_2"), asstMsg("tx2", "BODY-2"),
];
const folded: CoreMessage[] = [
userMsg("u1", "q1"),
reasoningMsg("rs1", "ENC_1"), hostedCall("ws1", "web_search_call", { action: { type: "search", query: "q" } }),
reasoningMsg("rs2", "ENC_2"), asstMsg("tx2", "BODY-2"),
];
const out = repairResponsesAssistantOrdering(folded, original);
const ids = out.messages.map((m) => m.id);
assert.equal(out.slots.length, 1, "exactly one separator, between the two runs");
const sep = ids.find((id) => id.startsWith("acp_turn_sep_"));
assert.ok(sep, "a separator was minted");
const sepIdx = ids.indexOf(sep!);
assert.equal(ids[sepIdx - 1], "ws1", "separator comes right after the hosted call (run 1 intact)");
assert.equal(ids[sepIdx + 1], "rs2", "separator comes right before run 2's reasoning");
assertValidAssistantOrdering(wireItems(out.messages), "#7296 distinct-runs");
});

// --- #564/#2627 producer-owned provenance -------------------------------
// A separator is outbound-only and, on the wire, byte-identical to a client
// user message carrying the same text (coreToResponses emits
Expand Down
Loading