Skip to content

Commit 7e64286

Browse files
committed
fix(deepseek): fail closed on unmatched tool results
1 parent 104d960 commit 7e64286

4 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎docs-site/src/content/docs/reference/adapters.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ of the HTTP retry loop.
6161
- DeepSeek's stateless Responses parser receives provider-scoped history normalization: hook-injected
6262
context moves after an unambiguous tool-call/result batch. Parallel calls remain grouped before
6363
their matching outputs so every call stays in the reasoning-bearing assistant turn. Tolerant
64-
providers and ambiguous duplicate call IDs keep their original input order.
64+
providers and ambiguous duplicate, missing, or out-of-order call IDs keep their original input order.
6565

6666
- `forward` URL → `{baseUrl}/responses`. A `key` provider defaults to the legacy `{baseUrl}/v1/responses` construction.
6767
- A `key` provider may set a validated relative `responsesPath`; the adapter removes one trailing slash from `baseUrl` and sends `{trimmedBaseUrl}{responsesPath}`. For Ark Agent Plan, use `baseUrl: "https://ark.cn-beijing.volces.com/api/plan/v3"` with `responsesPath: "/responses"`.

‎src/adapters/openai-responses.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ function repairOrphanedInputItems(body: unknown, dropReasoning: boolean): unknow
548548
* - 검토한 주요 대안: Disable parallel calls (DeepSeek always enables them); duplicate reasoning per call; reorder each pair; or normalize the complete unambiguous call batch.
549549
* - 선택한 방식: Treat calls emitted before the first matched result as one batch, emit all calls followed by their matched outputs, and preserve intervening non-tool items immediately after the batch.
550550
* - 다른 대안 대신 이 방식을 선택한 이유: Batch normalization matches the Responses parallel-call shape without fabricating reasoning, while the provider gate and unique-pair requirement keep the blast radius narrow.
551-
* - 장점, 단점 및 영향: DeepSeek keeps one reasoning-bearing assistant turn for parallel calls and still accepts hook-interleaved single calls; tolerant providers stay byte/order equivalent, and ambiguous duplicate ids are not guessed.
551+
* - 장점, 단점 및 영향: DeepSeek keeps one reasoning-bearing assistant turn for parallel calls and still accepts hook-interleaved single calls; tolerant providers stay byte/order equivalent, and duplicate, missing, or backwards call/result pairs are not guessed.
552552
*/
553553
function normalizeResponsesToolResultAdjacency(body: unknown): unknown {
554554
if (!isPlainObject(body) || !Array.isArray(body.input)) return body;
@@ -579,7 +579,7 @@ function normalizeResponsesToolResultAdjacency(body: unknown): unknown {
579579
const pairs: Array<{ callIndex: number; outputIndex: number }> = [];
580580
for (const [key, callIndices] of calls) {
581581
const outputIndices = outputs.get(key);
582-
if (!outputIndices) continue;
582+
if (!outputIndices) return body;
583583
if (callIndices.length !== 1 || outputIndices.length !== 1) return body;
584584
const callIndex = callIndices[0]!;
585585
const outputIndex = outputIndices[0]!;

‎structure/04_transports-and-sidecars.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ Calls emitted before the first matched output stay together as one assistant bat
384384
their outputs in call order; hook-injected messages that split the batch move after it without being
385385
dropped. This preserves #1292's single-call adjacency repair without splitting a same-turn parallel
386386
batch away from its preceding plaintext reasoning (#1477). Tolerant providers never enter this pass,
387-
and duplicate or backwards call/result pairs are left for the upstream to reject rather than guessed.
387+
and duplicate, missing, or backwards call/result pairs are left for the upstream to reject rather than guessed.
388388

389389
[Decision Log]
390390
- 목적과 의도: Preserve DeepSeek reasoning replay for parallel tool calls while retaining the provider-scoped repair for hook-interleaved results.

‎tests/deepseek-inbound-wire.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,28 @@ describe("stateless Responses upstreams get no stateful parameters", () => {
906906
expect(body.input).toEqual(input);
907907
});
908908

909+
test("DeepSeek fails closed when a collected call has no matching result", () => {
910+
const callA = { type: "function_call", call_id: "call_a", name: "read_file", arguments: "{}" };
911+
const callB = { type: "function_call", call_id: "call_b", name: "read_file", arguments: "{}" };
912+
const outputB = { type: "function_call_output", call_id: "call_b", output: "B" };
913+
const injected = { type: "message", role: "developer", content: [{ type: "input_text", text: "context" }] };
914+
const input = [callA, callB, injected, outputB];
915+
916+
const body = buildBody(deepseekProvider(), { input }) as { input: unknown[] };
917+
expect(body.input).toEqual(input);
918+
});
919+
920+
test("DeepSeek fails closed when a collected call/result pair is backwards", () => {
921+
const callA = { type: "function_call", call_id: "call_a", name: "read_file", arguments: "{}" };
922+
const callB = { type: "function_call", call_id: "call_b", name: "read_file", arguments: "{}" };
923+
const outputB = { type: "function_call_output", call_id: "call_b", output: "B" };
924+
const outputA = { type: "function_call_output", call_id: "call_a", output: "A" };
925+
const input = [callB, callA, outputB, outputA];
926+
927+
const body = buildBody(deepseekProvider(), { input }) as { input: unknown[] };
928+
expect(body.input).toEqual(input);
929+
});
930+
909931
test("tolerant Responses providers keep interleaved tool history unchanged", () => {
910932
const provider: OcxProviderConfig = {
911933
adapter: "openai-responses",

0 commit comments

Comments
 (0)