Skip to content
Merged
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
4 changes: 2 additions & 2 deletions kernel/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kernel/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
13 changes: 12 additions & 1 deletion kernel/src/wire/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ export type ResponsesProjection = {
customToolCallIds: Set<string>;
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. */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -645,6 +654,7 @@ export function responsesToCore(
customToolCallIds,
layout,
droppedReasoning,
...(headRole ? { headRole } : {}),
};
}

Expand Down Expand Up @@ -1005,14 +1015,15 @@ export function coreToResponses(
export function injectResponsesDeveloperMessage(
input: string | ResponseInputItem[],
content: string,
role: "system" | "developer" = "developer",
): ResponseInputItem[] {
const items: ResponseInputItem[] =
typeof input === "string"
? [{ type: "message", role: "user", content: input }]
: [...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;
}

Expand Down
71 changes: 71 additions & 0 deletions kernel/tests/responses-head-role.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 19 additions & 12 deletions src/compat-roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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, " ");
Expand Down
2 changes: 1 addition & 1 deletion src/loop/adapter-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/server/prepare-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"
Expand Down
14 changes: 8 additions & 6 deletions src/server/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
70 changes: 70 additions & 0 deletions tests/compat-roles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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[][] }> {
Expand Down Expand Up @@ -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 ?? "?");
Expand Down
Loading