From a279baf0d87d5102e95bc8e0179a12a05ad424e8 Mon Sep 17 00:00:00 2001 From: Gong Liu <1360187968@qq.com> Date: Tue, 10 Nov 2026 01:24:25 +0800 Subject: [PATCH 1/3] kernel: reuse the client's leading message role when rebuilding the Responses input (for #6063) --- kernel/src/wire/responses.ts | 13 ++++- kernel/tests/responses-head-role.test.ts | 71 ++++++++++++++++++++++++ src/loop/adapter-responses.ts | 2 +- src/server/prepare-responses.ts | 4 +- 4 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 kernel/tests/responses-head-role.test.ts diff --git a/kernel/src/wire/responses.ts b/kernel/src/wire/responses.ts index 36c49e040b..f329611102 100644 --- a/kernel/src/wire/responses.ts +++ b/kernel/src/wire/responses.ts @@ -78,6 +78,13 @@ export type ResponsesProjection = { customToolCallIds: Set; layout: ResponseLayoutSlot[]; stringInput?: { original: string; coreId: string }; + /** Role of the client's leading system/developer message when it was hoisted + * into `systemParts`. Hosts re-inject that block as one head message; they + * must reuse the client's role instead of hardcoding `developer`, or a model + * that declares `compat.supportsDeveloperRole: false` rejects the request + * (DeepSeek answers HTTP 422 unknown variant `developer`). Undefined means + * the body carried no head message — keep the historical `developer`. */ + headRole?: "system" | "developer"; /** Reasoning items dropped because ACP_REASONING_KEEP=none. 0 by default — * reasoning is normally routed through the compression pipeline so it is * hidden automatically once its turn is summarized. */ @@ -247,6 +254,7 @@ export function responsesToCore( }; } let sysDevHead = true; + let headRole: "system" | "developer" | undefined; for (const [wireIndex, raw] of body.input.entries()) { const item = normalizeEasyInputItem(raw); let coreId: string | undefined; @@ -287,6 +295,7 @@ export function responsesToCore( const text = messageContent(message.content); if (message.role === "system" || message.role === "developer") { if (sysDevHead) { + headRole ??= message.role; systemParts.push(text); idx++; continue; @@ -645,6 +654,7 @@ export function responsesToCore( customToolCallIds, layout, droppedReasoning, + ...(headRole ? { headRole } : {}), }; } @@ -1005,6 +1015,7 @@ export function coreToResponses( export function injectResponsesDeveloperMessage( input: string | ResponseInputItem[], content: string, + role: "system" | "developer" = "developer", ): ResponseInputItem[] { const items: ResponseInputItem[] = typeof input === "string" @@ -1012,7 +1023,7 @@ export function injectResponsesDeveloperMessage( : [...input]; let index = 0; while (items[index]?.type === "additional_tools") index++; - items.splice(index, 0, { type: "message", role: "developer", content }); + items.splice(index, 0, { type: "message", role, content }); return items; } diff --git a/kernel/tests/responses-head-role.test.ts b/kernel/tests/responses-head-role.test.ts new file mode 100644 index 0000000000..ebeca8e218 --- /dev/null +++ b/kernel/tests/responses-head-role.test.ts @@ -0,0 +1,71 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { + responsesToCore, + injectResponsesDeveloperMessage, +} from "../src/wire/responses.js"; +import type { + ResponseInputItem, + ResponseInputMessage, + ResponsesRequestBody, +} from "../src/wire/responses.js"; + +function body(input: ResponseInputItem[]): ResponsesRequestBody { + return { model: "test-model", input } as ResponsesRequestBody; +} + +function msg(role: "system" | "developer" | "user", text: string): ResponseInputMessage { + return { type: "message", role, content: text } as ResponseInputMessage; +} + +const userMsg = msg("user", "hello"); + +function injectedRole( + input: ResponseInputItem[], + role?: "system" | "developer", +): string { + const out = injectResponsesDeveloperMessage( + [userMsg] as ResponseInputItem[], + "block", + role ?? "developer", + ); + assert.equal(out[0].type, "message"); + return (out[0] as { role: string }).role; +} + +test("#6063 responsesToCore keeps the client's leading message role", () => { + const sys = responsesToCore(body([msg("system", "s"), userMsg])); + assert.equal(sys.headRole, "system"); + + const dev = responsesToCore(body([msg("developer", "d"), userMsg])); + assert.equal(dev.headRole, "developer"); + + // Leading run collapses into systemParts in both cases: the block text is + // unchanged, only the role the host re-injects has to survive. + assert.deepEqual(sys.systemParts, ["s"]); + assert.deepEqual(dev.systemParts, ["d"]); +}); + +test("#6063 a mid-history system message does not become the head role", () => { + const projection = responsesToCore( + body([userMsg, msg("system", "mid"), userMsg]), + ); + assert.equal(projection.headRole, undefined); + assert.deepEqual(projection.systemParts, []); +}); + +test("#6063 bodies without a head message keep the developer default", () => { + const projection = responsesToCore(body([userMsg])); + assert.equal(projection.headRole, undefined); + assert.equal(injectedRole([userMsg], projection.headRole), "developer"); +}); + +test("#6063 injection reuses the recorded head role", () => { + const projection = responsesToCore(body([msg("system", "s"), userMsg])); + assert.equal(injectedRole([userMsg], projection.headRole), "system"); +}); + +test("#6063 injection still defaults to developer without an explicit role", () => { + const out = injectResponsesDeveloperMessage([userMsg] as ResponseInputItem[], "block"); + assert.equal((out[0] as { role: string }).role, "developer"); +}); diff --git a/src/loop/adapter-responses.ts b/src/loop/adapter-responses.ts index 725ef581ba..18e609e4b2 100644 --- a/src/loop/adapter-responses.ts +++ b/src/loop/adapter-responses.ts @@ -338,7 +338,7 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp const devParts = projection && projection.systemParts.length > 0 ? [...projection.systemParts, systemPrompt] : [systemPrompt]; - const withDev = injectResponsesDeveloperMessage(inputItems, devParts.join("\n\n---\n\n")); + const withDev = injectResponsesDeveloperMessage(inputItems, devParts.join("\n\n---\n\n"), projection?.headRole ?? "developer"); const finalInput = notes && notes.length > 0 ? [...withDev, ...notes.map((text) => ({ type: "message" as const, role: "user" as const, content: text }))] : withDev; diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index c1b9e81d18..0453487697 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -459,7 +459,7 @@ reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStr const devContent = devParts.join("\n\n---\n\n"); responsesDevContent = devContent; if (forgedSummaries.length > 0) log("debug", `[${sessionId}] [inject] ${forgedSummaries.length} captured summary block(s) re-injected into developer message`); - rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent); + rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent, projection.headRole ?? "developer"); if (!knobNoInjectTool() && injectTools) { // #1712: decompress's startId/endId execute only on CCR-armed // sessions (#1179) — serve the no-range schema otherwise. @@ -473,7 +473,7 @@ reconcileFoldCoverage(session, msgs, { mode: foldMode, sessionId, log, markerStr const devContent = [...projection.systemParts, ...forgedSummaries, ...(tagsOnlyPrompt !== "" ? [tagsOnlyPrompt] : []), ...(genBoundaryActive && !isCompactionTrigger ? [GENERATION_BOUNDARY_NOTE] : [])].join("\n\n---\n\n"); responsesDevContent = devContent; if (forgedSummaries.length > 0) log("debug", `[${sessionId}] [inject] ${forgedSummaries.length} captured summary block(s) re-injected into developer message`); - rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent); + rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent, projection.headRole ?? "developer"); } if (sysNotes.length > 0) { const inputItems: ResponseInputItem[] = typeof rebuiltInput === "string" From 5ac2983c5ca6ec2c9fb7b5f4d097ec90568c9bec Mon Sep 17 00:00:00 2001 From: Gong Liu <1360187968@qq.com> Date: Tue, 10 Nov 2026 01:24:25 +0800 Subject: [PATCH 2/3] kernel: version 0.0.191 --- kernel/package-lock.json | 4 ++-- kernel/package.json | 2 +- package-lock.json | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/kernel/package-lock.json b/kernel/package-lock.json index 181659f569..5439a5aa59 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.190", + "version": "0.0.191", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.190", + "version": "0.0.191", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/kernel/package.json b/kernel/package.json index 07bf3f94eb..160acc27c1 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.190", + "version": "0.0.191", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", diff --git a/package-lock.json b/package-lock.json index 0c53d00816..9a3d581f1c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.190", + "version": "0.0.191", "dev": true, "license": "MIT", "devDependencies": { From 951f2f6b01ca0c0fb599cde8a7df738b2d91d736 Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 10 Nov 2026 02:34:41 +0800 Subject: [PATCH 3/3] fix: teach the #552 role-rejection ladder to recognize serde 422 rejections (for #6063) DeepSeek-class serde backends answer a role rejection with 422 and the deserialization text 'messages[0].role: unknown variant `developer`' instead of 400 'Invalid role: developer'. The learn-on-failure ladder only admitted 400s (relay funnel + both detectors) and its patterns required the rejection keyword BEFORE the field name, so such clients never self-healed and every request kept failing upstream. Widen the relay funnel gate and detectRoleRejection/detectSystemPlacementError to 400/422, and add a serde-shaped pattern (field path first, 'unknown variant' after). Every existing conservative gate stays intact: stopword guard on the captured token, placement-marker requirement for the placement detector, rewritten===0 short-circuit, hard cap of original+2 attempts, and the original error body forwarded verbatim after a failed retry. --- src/compat-roles.ts | 31 ++++++++++------- src/server/relay.ts | 14 ++++---- tests/compat-roles.test.ts | 70 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 18 deletions(-) diff --git a/src/compat-roles.ts b/src/compat-roles.ts index 511bd17476..9d63cc5e98 100644 --- a/src/compat-roles.ts +++ b/src/compat-roles.ts @@ -80,16 +80,22 @@ const ROLE_CAPTURE_STOPWORDS = new Set([ const ROLE_REJECTION_PATTERNS = [ /(?:invalid|unknown|unsupported|unrecognized|unexpected)[a-z ]{0,24}?role\s*[:=]?\s*["'`]?([a-z][a-z0-9_-]{1,31})["'`]?/i, /role\s*[:=]?\s*["'`]?([a-z][a-z0-9_-]{1,31})["'`]?\s+(?:is\s+)?not\s+(?:supported|allowed|recognized|valid|accepted)/i, + // Rust serde deserialization shape (DeepSeek official API, #6063): the + // field path comes FIRST and "unknown variant" after — the reverse word + // order of pattern 1, so it needs its own rule: + // messages[0].role: unknown variant `developer`, expected one of ... + /role\s*:\s*(?:unknown|invalid|unsupported)\s+variant\s*["'`]?([a-z][a-z0-9_-]{1,31})["'`]?/i, ]; -/** Detect an upstream 400 that exists only because of a message role the - * provider does not support (e.g. codex ≥0.153 sends "developer"; - * converting backends answer 400 "Invalid role: developer"). Conservative by - * design: status must be 400, the body must blame a role, and the captured - * token must look like a role name. Returns the offending role so the - * caller can auto-rewrite it to a supported one. */ +/** Detect an upstream 400/422 that exists only because of a message role the + * provider does not support (e.g. codex ≥0.153 sends "developer"; converting + * backends answer 400 "Invalid role: developer", serde-based ones such as + * DeepSeek answer 422 "messages[0].role: unknown variant `developer`", #6063). + * Conservative by design: status must be 400 or 422, the body must blame a + * role, and the captured token must look like a role name. Returns the + * offending role so the caller can auto-rewrite it to a supported one. */ export function detectRoleRejection(status: number, bodyText: string): { role: string } | null { - if (status !== 400) return null; + if (status !== 400 && status !== 422) return null; const head = bodyText.slice(0, 4096); for (const re of ROLE_REJECTION_PATTERNS) { const m = re.exec(head); @@ -107,12 +113,13 @@ export function detectRoleRejection(status: number, bodyText: string): { role: s * #377. Distinct from detectRoleRejection, which names an unsupported ROLE; * here the role is fine and the POSITION/count is the problem, so there is no * role to capture — just a boolean gate for the second-chance developer→user - * hop. Conservative: requires a 400 plus the literal word "system" plus one - * strong placement/multiplicity marker, so unrelated 400s (quota, overflow, - * missing-system, auth) never trip it. Quotes/brackets are normalized away so - * phrasings like "Only one 'system' message…" still match. */ + * hop. Conservative: requires a 400/422 (#552 ladder gate) plus the literal + * word "system" plus one strong placement/multiplicity marker, so unrelated + * errors (quota, overflow, missing-system, auth) never trip it. + * Quotes/brackets are normalized away so phrasings like "Only one 'system' + * message…" still match. */ export function detectSystemPlacementError(status: number, bodyText: string): boolean { - if (status !== 400) return false; + if (status !== 400 && status !== 422) return false; const head = bodyText.slice(0, 4096); if (!/system/i.test(head)) return false; const norm = head.replace(/['"`[\](){}]/g, " "); diff --git a/src/server/relay.ts b/src/server/relay.ts index 976ba5a33c..6e4a3312a9 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -1307,15 +1307,17 @@ async function forwardCaptured( } // #552 learn-on-failure: a converting upstream that rejects a role (codex // ≥0.153 sends "developer"; vLLM/SGLang-style backends answer 400 - // "Invalid role: developer") gets ONE auto-retry with the offending role - // rewritten to "system". On success the mapping is remembered on the - // session (never written to config — the log carries the permanent - // per-provider snippet instead). On failure the original response - // continues downstream verbatim. 400 bodies are small (buffered below). + // "Invalid role: developer", serde-based ones such as DeepSeek answer 422 + // "messages[0].role: unknown variant `developer`", #6063) gets ONE + // auto-retry with the offending role rewritten to "system". On success the + // mapping is remembered on the session (never written to config — the log + // carries the permanent per-provider snippet instead). On failure the + // original response continues downstream verbatim. Error bodies for these + // request-validation statuses are small (buffered below). if ( compatProtocol && typeof wireBody === "string" && - upstreamResult.response.status === 400 && + (upstreamResult.response.status === 400 || upstreamResult.response.status === 422) && upstreamResult.response.body ) { let roleErrText: string | null = null; diff --git a/tests/compat-roles.test.ts b/tests/compat-roles.test.ts index 31906d893b..5d3e79be44 100644 --- a/tests/compat-roles.test.ts +++ b/tests/compat-roles.test.ts @@ -280,6 +280,18 @@ test("detectRoleRejection: extracts the offending role, conservative otherwise", assert.equal(detectRoleRejection(400, '[{"loc":["body","messages",0,"role"],"msg":"Input should be \'system\', \'user\', \'tool\' or \'assistant\'"}]'), null); }); +test("detectRoleRejection: serde deserialization shape on 400 and 422 (#6063 DeepSeek)", () => { + const serde = JSON.stringify({ code: "invalid_request_error", message: "Failed to deserialize the JSON body into the target type: messages[0].role: unknown variant `developer`, expected one of `system`, `user`, `assistant`, `tool`, `latest_reminder` at line 1 column 57" }); + assert.deepEqual(detectRoleRejection(422, serde), { role: "developer" }); + assert.deepEqual(detectRoleRejection(400, serde), { role: "developer" }); + // Unrelated bodies stay out on the newly admitted 422 as well. + assert.equal(detectRoleRejection(422, '{"error":{"message":"insufficient quota"}}'), null); + // Stopword capture stays conservative (pattern order cannot skip it). + assert.equal(detectRoleRejection(422, 'messages[0].role: unknown variant `must`'), null); + // Non-validation statuses never enter the detector. + assert.equal(detectRoleRejection(500, serde), null); +}); + test("detectSystemPlacementError: flags #377-class placement 400s, ignores role-name/quota/overflow/auth", () => { const positive = [ "Only one 'system' message is allowed and it must be at the beginning", @@ -306,6 +318,9 @@ test("detectSystemPlacementError: flags #377-class placement 400s, ignores role- assert.equal(detectSystemPlacementError(400, msg), false, `should NOT detect: ${msg}`); } assert.equal(detectSystemPlacementError(401, "Multiple system messages found"), false); + // #6063: serde backends report validation errors as 422; the ladder gate + // admits both statuses, so the placement detector must as well. + assert.equal(detectSystemPlacementError(422, "Multiple system messages found"), true); }); function roleRejectingUpstream(): Promise<{ server: http.Server; seen: string[][] }> { @@ -394,6 +409,61 @@ test("e2e #552 F: retry that still fails passes the original 400 through verbati } }); +function serdeRejectingUpstream(): Promise<{ server: http.Server; seen: string[][] }> { + const seen: string[][] = []; + const server = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + let roles: string[] = []; + try { + const parsed = JSON.parse(Buffer.concat(chunks).toString("utf8")) as { input?: Array<{ role?: string }>; messages?: Array<{ role?: string }> }; + roles = [...(parsed.input ?? []), ...(parsed.messages ?? [])].map((m) => m.role ?? "?"); + } catch { /* ignore */ } + seen.push(roles); + if (roles.includes("developer")) { + // DeepSeek official API shape (#6063): serde deserialization 422. + res.writeHead(422, { "content-type": "application/json" }); + res.end(JSON.stringify({ code: "invalid_request_error", message: "Failed to deserialize the JSON body into the target type: messages[0].role: unknown variant `developer`, expected one of `system`, `user`, `assistant`, `tool`, `latest_reminder` at line 1 column 57" })); + } else { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: true })); + } + }); + }); + return new Promise((resolve) => server.listen(0, "127.0.0.1", () => resolve({ server, seen }))); +} + +test("e2e #6063: serde 422 role rejection auto-retries and learns like the #552 400 path", async () => { + const { server: upstream, seen } = await serdeRejectingUpstream(); + const harness = await startProxy(upstream, { compatJson: `{"providers":{}}` }); + try { + const payload = JSON.stringify({ model: "test", input: [{ type: "message", role: "developer", content: "be terse" }] }); + const res1 = await fetch(`http://127.0.0.1:${harness.port}/v1/responses`, { + method: "POST", + headers: { "content-type": "application/json", "x-session-id": "compat-roles-serde-learn" }, + body: payload, + }); + assert.equal(res1.status, 200, "client sees a transparent 200 after the auto-retry"); + const res2 = await fetch(`http://127.0.0.1:${harness.port}/v1/responses`, { + method: "POST", + headers: { "content-type": "application/json", "x-session-id": "compat-roles-serde-learn" }, + body: payload, + }); + assert.equal(res2.status, 200); + // 3 upstream hits total: req1 rejected (developer), req1-retried (system), + // req2 rewritten BEFORE fetch via the learned session map (system). + assert.equal(seen.length, 3, `expected 3 upstream hits, got ${JSON.stringify(seen)}`); + assert.deepEqual(seen[0], ["developer"], "first hit carries the client's developer role (no compat configured)"); + assert.deepEqual(seen[1], ["system"], "auto-retry rewrote developer→system on the 422 serde rejection"); + assert.deepEqual(seen[2], ["system"], "second request skipped the 422 round-trip (session learned)"); + } finally { + await harness.stop(); + harness.cleanup(); + await close(upstream); + } +}); + function rolesOf(body: unknown): string[] { const b = body as { input?: Array<{ role?: string }>; messages?: Array<{ role?: string }> }; return [...(b.input ?? []), ...(b.messages ?? [])].map((m) => m.role ?? "?");