Skip to content

Commit 56cca2f

Browse files
Ingwannulidge-jun
authored andcommitted
fix(deepseek): normalize tool result adjacency
1 parent 2495de3 commit 56cca2f

7 files changed

Lines changed: 126 additions & 1 deletion

File tree

‎src/adapters/openai-responses.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,65 @@ function repairOrphanedInputItems(body: unknown, dropReasoning: boolean): unknow
538538
return changed ? { ...body, input: repaired } : body;
539539
}
540540

541+
/**
542+
* Make unambiguous Responses tool pairs adjacent for upstream parsers that require it.
543+
*
544+
* [Decision Log]
545+
* - 목적과 의도: Keep Codex hook-injected developer context without letting it make a strict upstream reject the matching tool result.
546+
* - 기존 구현 및 제약 조건: The orphan repair verifies only pair presence; globally reordering valid history would change tolerant providers unnecessarily.
547+
* - 검토한 주요 대안: Reorder every Responses request, drop the intervening message, or gate a lossless reorder behind provider capability metadata.
548+
* - 선택한 방식: Reorder only unique call/result pairs for providers that explicitly require adjacency, preserving every intervening item immediately after the result.
549+
* - 다른 대안 대신 이 방식을 선택한 이유: The provider gate limits semantic blast radius, while refusing ambiguous duplicate ids avoids guessing which result belongs to which call.
550+
* - 장점, 단점 및 영향: DeepSeek receives the adjacency its parser requires; tolerant providers stay byte/order equivalent. Ambiguous duplicate ids still fail upstream rather than being silently rewritten.
551+
*/
552+
function normalizeResponsesToolResultAdjacency(body: unknown): unknown {
553+
if (!isPlainObject(body) || !Array.isArray(body.input)) return body;
554+
const input = body.input;
555+
const calls = new Map<string, number[]>();
556+
const outputs = new Map<string, number[]>();
557+
558+
const appendIndex = (map: Map<string, number[]>, key: string, index: number): void => {
559+
const existing = map.get(key);
560+
if (existing) existing.push(index);
561+
else map.set(key, [index]);
562+
};
563+
564+
for (let index = 0; index < input.length; index += 1) {
565+
const item = input[index];
566+
if (!isPlainObject(item) || typeof item.call_id !== "string" || item.call_id.length === 0) continue;
567+
if (item.type === "function_call" || item.type === "local_shell_call") {
568+
appendIndex(calls, `function:${item.call_id}`, index);
569+
} else if (item.type === "custom_tool_call") {
570+
appendIndex(calls, `custom:${item.call_id}`, index);
571+
} else if (item.type === "function_call_output") {
572+
appendIndex(outputs, `function:${item.call_id}`, index);
573+
} else if (item.type === "custom_tool_call_output") {
574+
appendIndex(outputs, `custom:${item.call_id}`, index);
575+
}
576+
}
577+
578+
const movedOutputIndices = new Set<number>();
579+
const outputAfterCall = new Map<number, unknown>();
580+
for (const [key, callIndices] of calls) {
581+
const outputIndices = outputs.get(key);
582+
if (callIndices.length !== 1 || outputIndices?.length !== 1) continue;
583+
const callIndex = callIndices[0]!;
584+
const outputIndex = outputIndices[0]!;
585+
if (outputIndex === callIndex + 1) continue;
586+
movedOutputIndices.add(outputIndex);
587+
outputAfterCall.set(callIndex, input[outputIndex]);
588+
}
589+
if (movedOutputIndices.size === 0) return body;
590+
591+
const normalized: unknown[] = [];
592+
for (let index = 0; index < input.length; index += 1) {
593+
if (movedOutputIndices.has(index)) continue;
594+
normalized.push(input[index]);
595+
if (outputAfterCall.has(index)) normalized.push(outputAfterCall.get(index));
596+
}
597+
return { ...body, input: normalized };
598+
}
599+
541600
/**
542601
* Remove `previous_response_id` before forwarding. Two triggers:
543602
* - the proxy expanded the request into a full input replay (the id is now redundant), or
@@ -1158,6 +1217,9 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig):
11581217
if (forward || stateless) {
11591218
outBody = repairOrphanedInputItems(outBody, unexpandedMiss);
11601219
}
1220+
if (provider.requiresAdjacentResponsesToolResults === true) {
1221+
outBody = normalizeResponsesToolResultAdjacency(outBody);
1222+
}
11611223
if (forward) {
11621224
outBody = stripUnsupportedForwardParams(outBody);
11631225
} else {

‎src/config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ const providerConfigSchema = z.object({
613613
apiKeyTransport: z.enum(["x-api-key", "bearer"]).optional(),
614614
responsesPath: z.string().min(1).optional(),
615615
statelessResponses: z.boolean().optional(),
616+
requiresAdjacentResponsesToolResults: z.boolean().optional(),
616617
supportsServiceTier: z.boolean().optional(),
617618
preserveResponsesReasoningContent: z.boolean().optional(),
618619
allowPrivateNetwork: z.boolean().optional(),

‎src/providers/derive.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ export function providerConfigSeed(entry: ProviderRegistryEntry): OcxProviderCon
152152
...(entry.promptCacheKey !== undefined ? { promptCacheKey: entry.promptCacheKey } : {}),
153153
...(entry.responsesPath !== undefined ? { responsesPath: entry.responsesPath } : {}),
154154
...(entry.statelessResponses !== undefined ? { statelessResponses: entry.statelessResponses } : {}),
155+
...(entry.requiresAdjacentResponsesToolResults !== undefined
156+
? { requiresAdjacentResponsesToolResults: entry.requiresAdjacentResponsesToolResults }
157+
: {}),
155158
...(entry.autoToolChoiceOnlyModels ? { autoToolChoiceOnlyModels: [...entry.autoToolChoiceOnlyModels] } : {}),
156159
...(entry.preserveReasoningContentModels ? { preserveReasoningContentModels: [...entry.preserveReasoningContentModels] } : {}),
157160
...(entry.reasoningSplitModels ? { reasoningSplitModels: [...entry.reasoningSplitModels] } : {}),
@@ -323,6 +326,9 @@ export function enrichProviderFromRegistry(name: string, prov: OcxProviderConfig
323326
// learned this route still gets backfilled.
324327
if (prov.responsesPath === undefined && seed.responsesPath !== undefined) prov.responsesPath = seed.responsesPath;
325328
if (prov.statelessResponses === undefined && seed.statelessResponses !== undefined) prov.statelessResponses = seed.statelessResponses;
329+
if (prov.requiresAdjacentResponsesToolResults === undefined && seed.requiresAdjacentResponsesToolResults !== undefined) {
330+
prov.requiresAdjacentResponsesToolResults = seed.requiresAdjacentResponsesToolResults;
331+
}
326332
// Registry-only metadata (never seeded into saved config): backfill straight from
327333
// the entry so an explicit user value stays distinguishable from the default.
328334
if (prov.supportsServiceTier === undefined && entry.supportsServiceTier !== undefined) prov.supportsServiceTier = entry.supportsServiceTier;

‎src/providers/registry.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ export interface ProviderRegistryEntry {
186186
* replay miss are repaired rather than forwarded.
187187
*/
188188
statelessResponses?: boolean;
189+
/**
190+
* Responses parser requires a matched tool result directly after its call. This is
191+
* seeded/backfilled like other fixed upstream wire-contract capabilities.
192+
*/
193+
requiresAdjacentResponsesToolResults?: boolean;
189194
/**
190195
* Registry default for the provider's Responses `service_tier` support; see
191196
* `OcxProviderConfig.supportsServiceTier`. Registry-only: backfilled (never
@@ -1359,6 +1364,9 @@ export const PROVIDER_REGISTRY: readonly ProviderRegistryEntry[] = [
13591364
// "The API is stateless: responses and conversations are not stored on the
13601365
// server." https://api-docs.deepseek.com/api/create-response/
13611366
statelessResponses: true,
1367+
// DeepSeek rejects a valid Codex continuation when hook-provided developer
1368+
// context is persisted between a call and its matching result (#1292).
1369+
requiresAdjacentResponsesToolResults: true,
13621370
/* [Decision Log]
13631371
- 목적: DeepSeek V4 thinking mode multi-turn/tool-call requests must replay prior assistant reasoning_content.
13641372
- 대안 분석: Globally preserve reasoning_content for all OpenAI-compatible models; preserve it for legacy deepseek-reasoner too; mark only V4 thinking models in registry metadata.

‎src/router.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ function routedProviderConfig(providerName: string, provider: OcxProviderConfig)
307307
...(provider.responsesPath === undefined && registryEntry.responsesPath !== undefined
308308
? { responsesPath: registryEntry.responsesPath }
309309
: {}),
310+
...(provider.requiresAdjacentResponsesToolResults === undefined
311+
&& registryEntry.requiresAdjacentResponsesToolResults !== undefined
312+
? { requiresAdjacentResponsesToolResults: registryEntry.requiresAdjacentResponsesToolResults }
313+
: {}),
310314
...(provider.supportsServiceTier === undefined && registryEntry.supportsServiceTier !== undefined
311315
? { supportsServiceTier: registryEntry.supportsServiceTier }
312316
: {}),

‎src/types.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,12 @@ export interface OcxProviderConfig {
11581158
* forwarded to an upstream that cannot resolve their pair.
11591159
*/
11601160
statelessResponses?: boolean;
1161+
/**
1162+
* Responses upstream whose parser requires each tool result to immediately follow
1163+
* its matching call. When enabled, only unambiguous matched pairs are reordered;
1164+
* intervening messages are preserved after the result instead of being dropped.
1165+
*/
1166+
requiresAdjacentResponsesToolResults?: boolean;
11611167
/**
11621168
* Whether this provider's Responses route honours the OpenAI `service_tier`
11631169
* parameter. Tri-state: `true` lets fast mode inject/remove the field (an unset

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* assert the captured upstream URL, which is externally observable.
1313
*/
1414
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
15-
import { providerConfigSeed } from "../src/providers/derive";
15+
import { enrichProviderFromRegistry, providerConfigSeed } from "../src/providers/derive";
1616
import { getProviderRegistryEntry, PROVIDER_REGISTRY } from "../src/providers/registry";
1717
import { createResponsesPassthroughAdapter as createResponsesPassthroughAdapterProduction } from "../src/adapters/openai-responses";
1818
import { resolveWireProtocolOverride } from "../src/server/adapter-resolve";
@@ -439,7 +439,45 @@ describe("stateless Responses upstreams get no stateful parameters", () => {
439439

440440
test("the seed and backfill carry the capability, and only for declaring entries", () => {
441441
expect(providerConfigSeed(getProviderRegistryEntry("deepseek")!).statelessResponses).toBe(true);
442+
expect(providerConfigSeed(getProviderRegistryEntry("deepseek")!).requiresAdjacentResponsesToolResults).toBe(true);
442443
expect(providerConfigSeed(getProviderRegistryEntry("cerebras")!).statelessResponses).toBeUndefined();
444+
expect(providerConfigSeed(getProviderRegistryEntry("cerebras")!).requiresAdjacentResponsesToolResults).toBeUndefined();
445+
446+
const stale = deepseekProvider();
447+
delete stale.requiresAdjacentResponsesToolResults;
448+
enrichProviderFromRegistry("deepseek", stale);
449+
expect(stale.requiresAdjacentResponsesToolResults).toBe(true);
450+
});
451+
452+
test("DeepSeek makes a matched tool result adjacent without dropping injected developer context", () => {
453+
const call = { type: "function_call", call_id: "call_plan", name: "shell_command", arguments: "{}" };
454+
const injected = {
455+
type: "message",
456+
role: "developer",
457+
content: [{ type: "input_text", text: "[planning-with-files] ACTIVE PLAN" }],
458+
};
459+
const output = { type: "function_call_output", call_id: "call_plan", output: "Exit code: 0" };
460+
const tail = { type: "message", role: "user", content: [{ type: "input_text", text: "continue" }] };
461+
462+
const body = buildBody(deepseekProvider(), { input: [call, injected, output, tail] }) as { input: unknown[] };
463+
expect(body.input).toEqual([call, output, injected, tail]);
464+
});
465+
466+
test("tolerant Responses providers keep interleaved tool history unchanged", () => {
467+
const provider: OcxProviderConfig = {
468+
adapter: "openai-responses",
469+
baseUrl: "https://api.openai.example/v1",
470+
authMode: "key",
471+
apiKey: "sk-test",
472+
};
473+
const input = [
474+
{ type: "function_call", call_id: "call_plan", name: "shell_command", arguments: "{}" },
475+
{ type: "message", role: "developer", content: [{ type: "input_text", text: "plan" }] },
476+
{ type: "function_call_output", call_id: "call_plan", output: "done" },
477+
];
478+
479+
const body = buildBody(provider, { input }) as { input: unknown[] };
480+
expect(body.input).toEqual(input);
443481
});
444482

445483
test("a replay miss does not forward an orphaned tool result", () => {

0 commit comments

Comments
 (0)