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.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",
Expand Down
55 changes: 34 additions & 21 deletions kernel/src/wire/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" &&
Expand Down Expand Up @@ -400,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";
}
Expand All @@ -430,37 +436,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;
Expand All @@ -476,6 +486,9 @@ function healIllegalSystemPositions(out: AnthropicWireMessage[]): void {
out[j] = { role: "user", content: kept };
prevNonSystem = j;
}
for (const d of movedDirectives) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里把所有被移动的 directive 无差别 push 到末尾。history 里有多条遗迹 directive 时(本单现场 medium/high 两条,当前 variant 是 low),它们在末尾相邻,随后被 prepare-anthropic 的 dedupeAdjacentOutputConfig 折叠成最后一条 high,覆盖当前 effort。建议移动前按「同 run 保留最后一个」折叠,或确认该覆盖可接受并补测试。

out.push(d);
}
}

/** #5438/#5432 kernel-side insertion helper (single source of truth for the
Expand Down
20 changes: 14 additions & 6 deletions kernel/tests/issue4292-orphan-system-guard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -179,17 +181,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", () => {
Expand Down
15 changes: 8 additions & 7 deletions kernel/tests/issue5432-follow-side-heal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;
}
Expand Down Expand Up @@ -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: [],
Expand Down
131 changes: 131 additions & 0 deletions kernel/tests/issue7270-directive-only-precede-heal.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
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.

Loading