From 7e53417a5dae77ab128139bddaa0ed521817abb4 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 19 Nov 2026 17:18:10 +0800 Subject: [PATCH 1/5] kernel: move illegally-positioned directive-only systems to the trailing run (#7270) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit kernel-change: the ANY-position exemption for directive-only systems (content:[] + output_config) does not hold on every upstream. A real Anthropic endpoint rejected system(directive)→user adjacency with the same 'must precede an assistant' 400 as content-bearing systems, permanently bricking the session after a degenerate fold. The fix moves an illegally-positioned directive-only system to the trailing run instead of skipping it, preserving the effort/tool-control semantics. Closes #7270 --- kernel/src/wire/anthropic.ts | 44 +++--- .../issue4292-orphan-system-guard.test.ts | 16 ++- ...ue7270-directive-only-precede-heal.test.ts | 131 ++++++++++++++++++ 3 files changed, 170 insertions(+), 21 deletions(-) create mode 100644 kernel/tests/issue7270-directive-only-precede-heal.test.ts diff --git a/kernel/src/wire/anthropic.ts b/kernel/src/wire/anthropic.ts index c99537e6e2..c77f16f274 100644 --- a/kernel/src/wire/anthropic.ts +++ b/kernel/src/wire/anthropic.ts @@ -364,10 +364,15 @@ function rebuildSystemMessage( } // #4292 hardening, kept for #4457's backstop: a directive-only system -// (empty content + output_config) is accepted at ANY position by -// Anthropic's mid-conversation-system rule, so it is never healed; demoting -// it would mint an empty-content user message that 400s on its own -// (mirrors the reporter's isAnthropicDirectiveOnlySystem). +// (empty content + output_config) carries effort/tool-control semantics that +// a user re-voice would destroy (demoting mints an empty-content user message +// that 400s on its own). #7270: the "accepted at ANY position" assumption does +// NOT hold on every upstream — a real Anthropic endpoint rejected +// `system(content:[]+output_config) → user` adjacency with the same +// 'must precede an assistant' 400 as content-bearing systems. healIllegalSystemPositions +// therefore no longer skips these unconditionally: it moves a directive-only +// system to the trailing run when its PRECEDE side is illegal, preserving the +// directive semantics while landing it in a legal position. function isDirectiveOnlySystem(e: AnthropicWireMessage): boolean { return ( e.role === "system" && @@ -430,37 +435,41 @@ function legitimatesFollowSide(m: AnthropicWireMessage | undefined): boolean { function healIllegalSystemPositions(out: AnthropicWireMessage[]): void { let prevNonSystem = -1; + // #7270: directive-only systems in an illegal PRECEDE position are moved to + // the trailing run (preserving effort/tool-control semantics) instead of + // being skipped unconditionally. + const movedDirectives: AnthropicWireMessage[] = []; for (let j = 0; j < out.length; j++) { const e = out[j]; if (!e) continue; if (e.role !== "system") { - // prevNonSystem indexes an earlier slot only — splices below happen - // strictly after it, demotions keep length — so the index stays valid - // through the mutating pass. prevNonSystem = j; continue; } - if (isDirectiveOnlySystem(e)) continue; + if (isDirectiveOnlySystem(e)) { + let k = j + 1; + while (k < out.length && out[k]!.role === "system") k++; + const next = out[k]; + const badPrecede = !!next && next.role !== "assistant"; + if (badPrecede) { + out.splice(j, 1); + movedDirectives.push(e); + j--; + } + continue; + } let k = j + 1; while (k < out.length && out[k]!.role === "system") k++; const next = out[k]; const badPrecede = !!next && next.role !== "assistant"; - // #5438/follow side: the nearest non-system predecessor must be a user - // or a server-tool-result-terminated assistant. Out[prevNonSystem] is - // post-heal state by construction (left-to-right pass), which is exactly - // what the API will see. const badFollow = !legitimatesFollowSide(out[prevNonSystem]); if (!badPrecede && !badFollow) continue; if (typeof e.content === "string") { - // An empty string demoted to user would mint an empty-content user - // message, which 400s on its own — drop instead. if (e.content === "") { out.splice(j, 1); j--; } else { out[j] = { role: "user", content: e.content }; - // The demoted entry is a user NOW — later run members are judged - // against post-heal state, exactly what the API will see. prevNonSystem = j; } continue; @@ -476,6 +485,9 @@ function healIllegalSystemPositions(out: AnthropicWireMessage[]): void { out[j] = { role: "user", content: kept }; prevNonSystem = j; } + for (const d of movedDirectives) { + out.push(d); + } } /** #5438/#5432 kernel-side insertion helper (single source of truth for the diff --git a/kernel/tests/issue4292-orphan-system-guard.test.ts b/kernel/tests/issue4292-orphan-system-guard.test.ts index 18242a3951..585890dcfa 100644 --- a/kernel/tests/issue4292-orphan-system-guard.test.ts +++ b/kernel/tests/issue4292-orphan-system-guard.test.ts @@ -179,17 +179,23 @@ test("sidecar system at array end stays verbatim", () => { assert.deepEqual(roundTrip({ messages }), messages); }); -test("directive-only system adjacent to the anchor stays system (legal anywhere)", () => { - // Re-voicing a pure effort marker would mint an empty-content user that 400s alone. +test("directive-only system adjacent to the anchor is moved to the trailing run (#7270)", () => { + // #7270: the anchor renders as a user on the wire, so a directive-only + // system immediately before it is in an illegal PRECEDE position (followed + // by a non-assistant). It is moved to the trailing run instead of being + // skipped, preserving the effort semantics while landing in a legal spot. const dir = { role: "system", content: [], output_config: { effort: "high" } } as unknown as AnthropicSystemMessage; const rebuilt = coreToAnthropic([ { id: "u0", role: "user", contentType: "text", text: "q1" }, { id: "s1", role: "system", contentType: "text", text: "[effort]", rawAnthropicMessage: dir }, anchor("a1", "[Compressed conversation section] x"), ] as never); - assert.equal(rebuilt[1]!.role, "system"); - assert.deepEqual(rebuilt[1]!.content, []); - assert.equal(rebuilt[2]!.role, "user"); + assert.equal(rebuilt.length, 3); + assert.equal(rebuilt[0]!.role, "user"); + assert.equal(rebuilt[1]!.role, "user"); + assert.equal(rebuilt[2]!.role, "system"); + assert.deepEqual(rebuilt[2]!.content, []); + assert.equal((rebuilt[2]! as { output_config?: { effort?: string } }).output_config?.effort, "high"); }); test("system separated from a later anchor by a user core is healed too", () => { diff --git a/kernel/tests/issue7270-directive-only-precede-heal.test.ts b/kernel/tests/issue7270-directive-only-precede-heal.test.ts new file mode 100644 index 0000000000..0480db3c2d --- /dev/null +++ b/kernel/tests/issue7270-directive-only-precede-heal.test.ts @@ -0,0 +1,131 @@ +// #7270: a directive-only system (content:[] + output_config) in an illegal +// PRECEDE position (followed by a non-assistant) was skipped unconditionally +// by healIllegalSystemPositions on the assumption that Anthropic accepts it +// "at any position". A real upstream rejected `system(directive) → user` +// with the same 'must precede an assistant' 400 as content-bearing systems, +// permanently bricking the session after a degenerate fold. The fix moves an +// illegally-positioned directive-only system to the trailing run instead of +// skipping it, preserving the effort/tool-control semantics. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { coreToAnthropic } from "../src/wire/anthropic.js"; +import type { AnthropicWireMessage } from "../src/wire/anthropic.js"; +import type { BiliMessage } from "../src/wire/bili-message.js"; + +function dir(effort: string): BiliMessage { + return { + id: "d" + effort, + role: "system", + contentType: "text", + text: "[effort:" + effort + "]", + rawAnthropicMessage: { + role: "system", + content: [], + output_config: { effort }, + }, + } as BiliMessage; +} + +function u(id: string, text: string): BiliMessage { + return { id, role: "user", contentType: "text", text }; +} + +function a(id: string, text: string): BiliMessage { + return { id, role: "assistant", contentType: "text", text }; +} + +// --- the field regression shape (user, directive, user) -------------------- + +test("#7270: directive-only system followed by user is moved to the trailing run", () => { + const out = coreToAnthropic([ + u("u1", "hello"), + dir("medium"), + u("u2", "world"), + ] as never); + // The directive-only system must NOT sit between two users. + assert.equal(out.length, 3); + assert.equal(out[0]!.role, "user"); + assert.equal(out[1]!.role, "user"); + assert.equal(out[2]!.role, "system"); + const d = out[2]! as { output_config?: { effort?: string } }; + assert.equal(d.output_config?.effort, "medium", "effort value preserved"); + assert.deepEqual(out[2]!.content, [], "content stays empty"); +}); + +test("#7270: directive-only system followed by assistant stays in place (legal)", () => { + const out = coreToAnthropic([ + u("u1", "hello"), + dir("high"), + a("a1", "ok"), + ] as never); + assert.equal(out.length, 3); + assert.equal(out[0]!.role, "user"); + assert.equal(out[1]!.role, "system"); + assert.equal(out[2]!.role, "assistant"); + const d = out[1]! as { output_config?: { effort?: string } }; + assert.equal(d.output_config?.effort, "high"); +}); + +// --- the real 6-message outbound shape from the issue ----------------------- + +test("#7270: real 6-message post-fold shape — no system→user adjacency", () => { + // Mirrors the field shape: user, dir(medium), user, dir(high), user, system(text) + // The last system has content (not directive-only) so it stays a system via rawAnthropicMessage. + const tailSys: BiliMessage = { + id: "s5", + role: "system", + contentType: "text", + text: "tail system", + rawAnthropicMessage: { role: "system", content: [{ type: "text", text: "tail system" }] }, + } as BiliMessage; + const out = coreToAnthropic([ + u("u0", "q with images"), + dir("medium"), + u("u2", "followup"), + dir("high"), + u("u4", "another"), + tailSys, + ] as never); + // All systems must be trailing (no non-system after any system). + const sysIdx: number[] = []; + for (let i = 0; i < out.length; i++) { + if (out[i]!.role === "system") sysIdx.push(i); + } + assert.ok(sysIdx.length >= 2, "at least the two directive systems must survive"); + for (const i of sysIdx) { + for (let j = i + 1; j < out.length; j++) { + assert.equal(out[j]!.role, "system", `system at ${i} must be followed only by systems, found ${out[j]!.role} at ${j}`); + } + } + // The two directive-only systems preserve their effort values. + const dirs = out.filter((m) => m.role === "system" && Array.isArray(m.content) && m.content.length === 0) as Array<{ output_config?: { effort?: string } }>; + assert.equal(dirs.length, 2); + assert.equal(dirs[0]!.output_config?.effort, "medium"); + assert.equal(dirs[1]!.output_config?.effort, "high"); +}); + +// --- negative guards: legal shapes stay byte-identical ----------------------- + +test("legal: directive-only system at array end stays verbatim", () => { + const out = coreToAnthropic([ + u("u1", "hello"), + a("a1", "ok"), + dir("low"), + ] as never); + assert.equal(out.length, 3); + assert.equal(out[2]!.role, "system"); + const d = out[2]! as { output_config?: { effort?: string } }; + assert.equal(d.output_config?.effort, "low"); + assert.deepEqual(out[2]!.content, []); +}); + +test("legal: directive-only system between user and assistant stays verbatim", () => { + const out = coreToAnthropic([ + u("u1", "hello"), + dir("medium"), + a("a1", "ok"), + ] as never); + assert.equal(out[1]!.role, "system"); + const d = out[1]! as { output_config?: { effort?: string } }; + assert.equal(d.output_config?.effort, "medium"); +}); From b805e43acb86f220e99d6636ab44baaba985b89e Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 19 Nov 2026 17:37:07 +0800 Subject: [PATCH 2/5] kernel: bump version to 0.0.200 (rebase onto master 0.0.199; resolve the 0.0.199 collision with #7252's kernel bump) --- kernel/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/package.json b/kernel/package.json index f65ce78fdd..d20ac004ac 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.199", + "version": "0.0.200", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", From 88f67800c0f15703bff7ca79fdbcd2d6163de1c3 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 19 Nov 2026 17:40:02 +0800 Subject: [PATCH 3/5] kernel: sync package-lock.json version to 0.0.200 (lock-sync with the 0.0.200 bump) --- kernel/package-lock.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/package-lock.json b/kernel/package-lock.json index 3e25df6ca0..bb7bd588bb 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.199", + "version": "0.0.200", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.199", + "version": "0.0.200", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", From 2975a63da30fbd2500a5498729713bfc806d2040 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 19 Nov 2026 17:56:32 +0800 Subject: [PATCH 4/5] kernel: update stale directive-only 'exempt anywhere' wording after the #7270 precede-heal MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Drive-by (review of #7281): the PR updated the isDirectiveOnlySystem comment but left three stale spots that now contradict the new behavior — - anthropic.ts heal comment: 'accepted at ANY position, so it is never healed' - issue5432 orphanSystems helper: still exempted directive-only on BOTH sides (a divergent mirror of the old behavior; the real heal now checks the PRECEDE side for them). Made the helper judge directive-only systems on the precede side only, matching healIllegalSystemPositions. - issue5432 test name + issue4292 header comment: 'exempt at any position' / 'directive-only anywhere'. All 53 targeted system-position tests still pass; kernel typecheck clean. --- kernel/src/wire/anthropic.ts | 11 ++++++----- .../tests/issue4292-orphan-system-guard.test.ts | 4 +++- kernel/tests/issue5432-follow-side-heal.test.ts | 15 ++++++++------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/kernel/src/wire/anthropic.ts b/kernel/src/wire/anthropic.ts index c77f16f274..0600abd7ae 100644 --- a/kernel/src/wire/anthropic.ts +++ b/kernel/src/wire/anthropic.ts @@ -405,11 +405,12 @@ function isDirectiveOnlySystem(e: AnthropicWireMessage): boolean { // predecessor (array start) follows the existing contract and is untouched. // Demotion re-voices to user with tool_addition/tool_removal blocks stripped // first (WC-022 forbids them outside system) and drops the entry when -// nothing remains; a -// directive-only system (empty content + output_config) is accepted at ANY -// position, so it is never healed (and never legitimizes a neighbour). Fresh -// objects only — sidecars stay intact for future verbatim emission once -// re-legitimized. +// nothing remains. A directive-only system (empty content + output_config) is +// never re-voiced — a demotion would mint an empty-content user message that +// 400s on its own — but per #7270 it is no longer exempt at illegal PRECEDE +// positions: it is moved to the trailing run there instead (see +// isDirectiveOnlySystem above). Fresh objects only — sidecars stay intact for +// future verbatim emission once re-legitimized. function isToolUpdateBlock(b: { type?: unknown }): boolean { return b.type === "tool_addition" || b.type === "tool_removal"; } diff --git a/kernel/tests/issue4292-orphan-system-guard.test.ts b/kernel/tests/issue4292-orphan-system-guard.test.ts index 585890dcfa..a34b9f0187 100644 --- a/kernel/tests/issue4292-orphan-system-guard.test.ts +++ b/kernel/tests/issue4292-orphan-system-guard.test.ts @@ -17,7 +17,9 @@ import type { BiliMessage } from "../src/wire/bili-message.js"; // system whose first non-system follower is neither an assistant nor the // array end is healed — re-voiced to user with tool_update blocks stripped // (WC-022), dropped when nothing remains. Legal layouts (assistant next, -// array end, trailing runs, directive-only anywhere) round-trip verbatim. +// array end, trailing runs, directive-only at a legal position) round-trip +// verbatim; a directive-only system at an illegal PRECEDE position is moved +// to the trailing run instead (#7270). function sys(content: AnthropicSystemMessage["content"]): AnthropicSystemMessage { return { role: "system", content }; diff --git a/kernel/tests/issue5432-follow-side-heal.test.ts b/kernel/tests/issue5432-follow-side-heal.test.ts index 07004744a9..8255b473b2 100644 --- a/kernel/tests/issue5432-follow-side-heal.test.ts +++ b/kernel/tests/issue5432-follow-side-heal.test.ts @@ -84,18 +84,19 @@ function a(id: string, text: string): BiliMessage { * enforces (follow side: nearest non-system predecessor must be a user or a * server-tool-result-terminated assistant; no predecessor at array start is * legal by contract. Precede side: run's first non-system follower must be - * an assistant or the array end). Directive-only exempt anywhere. */ + * an assistant or the array end). Directive-only systems (#7270) are judged + * on the PRECEDE side only — the real heal does not constrain their follow + * side — so they are flagged when their nearest non-system follower is a + * non-assistant (the heal moves them to the trailing run). */ function orphanSystems(messages: AnthropicWireMessage[]): number[] { const bad: number[] = []; for (let i = 0; i < messages.length; i++) { const m = messages[i]; if (m.role !== "system") continue; - if ( + const isDir = Array.isArray(m.content) && m.content.length === 0 && - (m as { output_config?: unknown }).output_config != null - ) - continue; + (m as { output_config?: unknown }).output_config != null; let k = i + 1; while (k < messages.length && messages[k]!.role === "system") k++; const next = messages[k]; @@ -116,7 +117,7 @@ function orphanSystems(messages: AnthropicWireMessage[]): number[] { typeof lastType === "string" && lastType.endsWith("tool_result")); const nextOk = !next || next.role === "assistant"; - if (!predOk || !nextOk) bad.push(i); + if (isDir ? !nextOk : !predOk || !nextOk) bad.push(i); } return bad; } @@ -334,7 +335,7 @@ test("gate (both directions): same real shape violating the PRECEDE side is heal assert.deepEqual(rebuilt[2]!.content, [{ type: "text", text: "delta" }]); }); -test("directive-only system behind an assistant stays system (exempt at any position)", () => { +test("directive-only system at a legal position (array end) stays system", () => { const dir = { role: "system", content: [], From 9a880e4ef33a0c55501509f0badf5b35d11d497f Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 19 Nov 2026 18:04:49 +0800 Subject: [PATCH 5/5] fix: sync root package-lock.json kernel version to 0.0.200 (lock-sync) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rebasing #7281 onto master's kernel 0.0.199→0.0.200 bump left the root package-lock.json's kernel entry at 0.0.199. The kernel/ own lockfile was synced (88f67800c0f) but the root lock was not, so lock-sync's 'npm install --package-lock-only' + 'git diff --exit-code -- package-lock.json' failed. Regenerated the root lockfile to record kernel 0.0.200. --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 6fedef6af7..dd0c2f32d2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -51,7 +51,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.199", + "version": "0.0.200", "dev": true, "license": "MIT", "devDependencies": {