Repository navigation
[PR] kernel: move illegally-positioned directive-only systems to the trailing run (#7270) #7281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ranxianglei
merged 5 commits into
master
from
2026-11-19_kernel-directive-only-precede-heal
Nov 19, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7e53417
kernel: move illegally-positioned directive-only systems to the trail…
b805e43
kernel: bump version to 0.0.200 (rebase onto master 0.0.199; resolve …
88f6780
kernel: sync package-lock.json version to 0.0.200 (lock-sync with the…
2975a63
kernel: update stale directive-only 'exempt anywhere' wording after t…
9a880e4
fix: sync root package-lock.json kernel version to 0.0.200 (lock-sync)
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
kernel/tests/issue7270-directive-only-precede-heal.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 保留最后一个」折叠,或确认该覆盖可接受并补测试。