Skip to content

Commit e851219

Browse files
committed
fix(deepseek): fail closed on unmatched or reversed tool results
1 parent 7e64286 commit e851219

8 files changed

Lines changed: 73 additions & 2 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ interface ProviderAdapter {
5151
先立って、同じキーで同一リクエストを待機して再送します。カスタム `runTurn` トランスポートは
5252
HTTP リトライ ループの対象外です。
5353

54+
- DeepSeek のステートレス Responses パーサーは、プロバイダーにスコープされた履歴正規化を受けます: フックで
55+
注入されたコンテキストは、あいまいさのない tool-call/result バッチの後に移動します。並列呼び出しは、
56+
それぞれの出力の前にグループ化されたままなので、すべての呼び出しが推論を含むアシスタントターンにとどまり
57+
ます。寛容なプロバイダーと、重複・欠落・順序不正の call ID は元の入力順を保持します。
58+
5459
- `forward` URL → `{baseUrl}/responses`。`key` provider はデフォルトで従来の `{baseUrl}/v1/responses` 構築を使います。
5560
- `key` provider は検証済みの相対 `responsesPath` を設定できます。adapter は `baseUrl` 末尾の `/` を 1 つ除き、`{trimmedBaseUrl}{responsesPath}` に送信します。Ark Agent Plan では `baseUrl: "https://ark.cn-beijing.volces.com/api/plan/v3"` と `responsesPath: "/responses"` を使います。
5661
- `forward` モードでは安全なヘッダー許可リスト(`FORWARD_HEADERS`)だけを中継します。authorization、ChatGPT account id、OpenAI beta/originator/session ヘッダーが対象です。この ChatGPT ログイン経路は [サイドカー](/ja/guides/sidecars/) にも使われます。

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ interface ProviderAdapter {
5858
같은 키로 동일 요청을 대기 후 재전송합니다. 커스텀 `runTurn` 전송은 HTTP 재시도 루프에
5959
포함되지 않습니다.
6060

61+
- DeepSeek의 stateless Responses 파서는 제공자 범위의 기록 정규화를 받습니다: 훅으로
62+
주입된 컨텍스트는 명확한 tool-call/result 배치 뒤로 이동합니다. 병렬 호출은 각 결과 앞에
63+
함께 묶여 있어 모든 호출이 추론을 담은 어시스턴트 턴에 남습니다. 관대한 제공자와 중복되거나
64+
누락되거나 순서가 잘못된 call ID는 원래 입력 순서를 유지합니다.
65+
6166
- `forward` URL → `{baseUrl}/responses`. `key` provider는 기본적으로 기존 `{baseUrl}/v1/responses` 구성을 사용합니다.
6267
- `key` provider는 검증된 상대 `responsesPath`를 설정할 수 있습니다. adapter는 `baseUrl` 끝의 `/` 하나를 제거하고 `{trimmedBaseUrl}{responsesPath}`로 전송합니다. Ark Agent Plan은 `baseUrl: "https://ark.cn-beijing.volces.com/api/plan/v3"`와 `responsesPath: "/responses"`를 사용합니다.
6368
- `forward` 모드에서는 안전한 헤더 허용 목록(`FORWARD_HEADERS`)만 중계합니다. authorization,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ interface ProviderAdapter {
6262
том же ключе, как и в переводимом пути `openai-chat`/Anthropic. Пользовательские транспорты
6363
`runTurn` в цикл HTTP-повторов не входят.
6464

65+
- Stateless-парсер DeepSeek Responses получает нормализацию истории на уровне провайдера:
66+
контекст, внедрённый хуком, переносится после однозначного батча call/result. Параллельные вызовы
67+
остаются сгруппированными перед своими результатами, поэтому каждый вызов сохраняет свой
68+
assistant-ход с рассуждениями. Толерантные провайдеры и неоднозначные (дублирующиеся,
69+
отсутствующие или неупорядоченные) идентификаторы call сохраняют исходный порядок входа.
70+
6571
- URL для `forward` → `{baseUrl}/responses`. Провайдер с `key` по умолчанию сохраняет прежнее построение `{baseUrl}/v1/responses`.
6672
- Провайдер с `key` может задать проверенный относительный `responsesPath`: адаптер удаляет один завершающий `/` из `baseUrl` и отправляет запрос на `{trimmedBaseUrl}{responsesPath}`. Для Ark Agent Plan используйте `baseUrl: "https://ark.cn-beijing.volces.com/api/plan/v3"` и `responsesPath: "/responses"`.
6773
- В режиме `forward` ретранслируется только безопасный allowlist заголовков (`FORWARD_HEADERS`):

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ interface ProviderAdapter {
5454
会等待并先于其他处理或故障转移,在相同 key 上重放完全相同请求,与翻译后的
5555
`openai-chat`/Anthropic 请求路径一致。自定义 `runTurn` 传输不在 HTTP 重试循环之内。
5656

57+
- DeepSeek 的 stateless Responses parser 会收到按 provider 范围的历史归一化:hook 注入的上下文会移动到
58+
明确的 tool-call/result 批次之后。并行调用保持在其对应输出之前分组,因此每个调用都留在承载
59+
推理的 assistant 回合中。宽容的 provider 和歧义的(重复、缺失或乱序的)call ID 保留原始输入顺序。
60+
5761
- `forward` URL → `{baseUrl}/responses`。`key` provider 默认保留原有的 `{baseUrl}/v1/responses` 构造。
5862
- `key` provider 可设置经过验证的相对 `responsesPath`;adapter 会移除 `baseUrl` 末尾的一个 `/`,并向 `{trimmedBaseUrl}{responsesPath}` 发送请求。Ark Agent Plan 使用 `baseUrl: "https://ark.cn-beijing.volces.com/api/plan/v3"` 和 `responsesPath: "/responses"`。
5963
- `forward` 模式只会转发安全的 header allowlist(`FORWARD_HEADERS`):authorization、ChatGPT

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ interface ProviderAdapter {
4343
**不經轉換**地流式傳回。
4444
**認證:** `forward`(轉發呼叫方 header)或 `key`。
4545

46+
- DeepSeek 的 stateless Responses parser 會收到按 provider 範圍的歷史正規化:hook 注入的內容會移動到
47+
明確的 tool-call/result 批次之後。並行呼叫保持在其對應輸出之前分組,因此每個呼叫都留在承載
48+
推理的 assistant 回合中。寬容的 provider 和歧義的(重複、缺失或亂序的)call ID 保留原始輸入順序。
49+
4650
- `forward` URL → `{baseUrl}/responses`。`key` provider 預設保留原有的 `{baseUrl}/v1/responses` 構造。
4751
- `key` provider 可設定經過驗證的相對 `responsesPath`;adapter 會移除 `baseUrl` 末尾的一個 `/`,並向 `{trimmedBaseUrl}{responsesPath}` 傳送請求。Ark Agent Plan 使用 `baseUrl: "https://ark.cn-beijing.volces.com/api/plan/v3"` 和 `responsesPath: "/responses"`。
4852
- `forward` 模式只會轉發安全的 header allowlist(`FORWARD_HEADERS`):authorization、ChatGPT

‎src/adapters/openai-responses.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,13 @@ function normalizeResponsesToolResultAdjacency(body: unknown): unknown {
586586
if (outputIndex <= callIndex) return body;
587587
pairs.push({ callIndex, outputIndex });
588588
}
589+
// Reject any collected output that lacks exactly one matching call. A lone or
590+
// duplicated output is ambiguous, and normalizing on top of it could sever a
591+
// result from the reasoning-bearing call turn it belongs to.
592+
for (const [key, outputIndices] of outputs) {
593+
const callIndices = calls.get(key);
594+
if (!callIndices || callIndices.length !== 1 || outputIndices.length !== 1) return body;
595+
}
589596
pairs.sort((left, right) => left.callIndex - right.callIndex);
590597

591598
const movedIndices = new Set<number>();
@@ -600,6 +607,13 @@ function normalizeResponsesToolResultAdjacency(body: unknown): unknown {
600607
next += 1;
601608
}
602609

610+
// Within one reasoning turn the outputs must appear in the same order as their
611+
// calls. If they are reversed, normalizing would fabricate a new output order;
612+
// leave the ambiguous history untouched instead.
613+
for (let groupIndex = 1; groupIndex < group.length; groupIndex += 1) {
614+
if (group[groupIndex]!.outputIndex < group[groupIndex - 1]!.outputIndex) return body;
615+
}
616+
603617
const batch = [
604618
...group.map(pair => input[pair.callIndex]),
605619
...group.map(pair => input[pair.outputIndex]),

‎src/types.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,8 +1228,8 @@ export interface OcxProviderConfig {
12281228
statelessResponses?: boolean;
12291229
/**
12301230
* Responses upstream whose parser requires an unambiguous call batch and its matched
1231-
* result batch to remain contiguous. Intervening messages are preserved after the
1232-
* batch, and parallel calls stay together with the reasoning turn that produced them.
1231+
* result batch to remain contiguous. Hook-injected context that splits the batch is
1232+
* preserved after it, and parallel calls stay together with the reasoning turn that produced them.
12331233
*/
12341234
requiresAdjacentResponsesToolResults?: boolean;
12351235
/**

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,39 @@ describe("stateless Responses upstreams get no stateful parameters", () => {
927927
const body = buildBody(deepseekProvider(), { input }) as { input: unknown[] };
928928
expect(body.input).toEqual(input);
929929
});
930+
test("DeepSeek leaves reversed outputs in their original order", () => {
931+
const callA = { type: "function_call", call_id: "call_a", name: "read_file", arguments: "{}" };
932+
const callB = { type: "function_call", call_id: "call_b", name: "read_file", arguments: "{}" };
933+
const outputB = { type: "function_call_output", call_id: "call_b", output: "B" };
934+
const outputA = { type: "function_call_output", call_id: "call_a", output: "A" };
935+
const injected = { type: "message", role: "developer", content: [{ type: "input_text", text: "context" }] };
936+
const input = [callA, callB, injected, outputB, outputA];
937+
938+
const body = buildBody(deepseekProvider(), { input }) as { input: unknown[] };
939+
expect(body.input).toEqual(input);
940+
});
941+
942+
test("DeepSeek keeps a valid pair in order when an unrelated output has no matching call", () => {
943+
// The orphan-output repair runs before the normalizer and flattens the unmatched
944+
// tool result into a user message, so the matched pair is still normalized and the
945+
// unmatchable output is never forwarded as a raw tool output.
946+
const callA = { type: "function_call", call_id: "call_a", name: "read_file", arguments: "{}" };
947+
const outputA = { type: "function_call_output", call_id: "call_a", output: "A" };
948+
const injected = { type: "message", role: "developer", content: [{ type: "input_text", text: "context" }] };
949+
const orphanOutput = { type: "function_call_output", call_id: "call_orphan", output: "orphan" };
950+
const tail = { type: "message", role: "user", content: [{ type: "input_text", text: "continue" }] };
951+
const input = [callA, injected, outputA, orphanOutput, tail];
952+
953+
const body = buildBody(deepseekProvider(), { input }) as { input: unknown[] };
954+
expect(body.input[0]).toEqual(callA);
955+
expect(body.input[1]).toEqual(outputA);
956+
expect(body.input).toContainEqual(injected);
957+
expect(body.input).toContainEqual(tail);
958+
expect(body.input.some(item => item.type === "function_call_output")).toBe(true);
959+
// The orphan output must not be forwarded raw; it is flattened into a user message.
960+
expect(body.input.some(item => item.type === "function_call_output" && item.call_id === "call_orphan")).toBe(false);
961+
});
962+
930963

931964
test("tolerant Responses providers keep interleaved tool history unchanged", () => {
932965
const provider: OcxProviderConfig = {

0 commit comments

Comments
 (0)