Skip to content

[PR] kernel: reuse the client's leading message role when rebuilding the Responses input - #6064

Merged
ranxianglei merged 3 commits into
ranxianglei:masterfrom
gongfpp:fix/responses-head-role
Nov 10, 2026
Merged

ranxianglei merged 3 commits into
ranxianglei:masterfrom
gongfpp:fix/responses-head-role

Conversation

@gongfpp

@gongfpp gongfpp commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

kernel-change: responsesToCore drops the role of the client's leading system/developer message and injectResponsesDeveloperMessage hardcodes role: "developer", so a client that must send system (DeepSeek-backed models with compat.supportsDeveloperRole: false) gets its head rewritten into a role the upstream rejects with HTTP 422. The fix records the hoisted role on the projection and reuses it; it is a literal-vs-client-value defect, not a design change, and ships with a reproducing regression test (mechanical-bug exemption, rule 1).

Closes #6063

What breaks

A /v1/responses client that declares compat.supportsDeveloperRole: false sends input[0].role = "system" on purpose. responsesToCore hoists that item into systemParts but keeps only the text, and the two rebuild sites re-inject the merged block with a hardcoded role: "developer". DeepSeek answers HTTP 422 messages[0].role: unknown variant 'developer', killing the turn, while the same request that skips the bili rewrite succeeds — exactly the asymmetry in the report.

Measured on master 4446301 with a two-line probe:

client head role=system    -> headRole: undefined | injected: developer
client head role=developer -> headRole: undefined | injected: developer
no head message            -> headRole: undefined | injected: developer

Fix

  • kernel/src/wire/responses.ts: ResponsesProjection gains headRole?: "system" | "developer", set (??=) only while the leading system/developer run is still being hoisted — mid-history system items keep their existing sidecar path and never become the head role. injectResponsesDeveloperMessage takes an optional role parameter, defaulting to developer so every existing caller keeps its old bytes.
  • src/server/prepare-responses.ts and src/loop/adapter-responses.ts: pass projection.headRole ?? "developer" at all three injection sites.

Same probe after the change:

client head role=system    -> headRole: system    | injected: system
client head role=developer -> headRole: developer | injected: developer
no head message            -> headRole: undefined | injected: developer

Both compression modes are covered: the HTTP/proxy path goes through prepare-responses.ts (two sites, compress-on and tags-only) and the ACP/plugin path through adapter-responses.ts. When the body carries no head message at all the field stays undefined, so behaviour is byte-identical to before — no upstream that accepts developer starts seeing system.

Fix — part 2: the #552 learn-on-failure ladder recognises serde 422 rejections

Part 1 makes the request correct in the first place; this half teaches the proxy to learn from the failure for clients that still send developer. Against DeepSeek-class serde backends that learning never fired: the relay funnel (src/server/relay.ts) only admitted 400s into the ladder, both detectors in src/compat-roles.ts required status 400, and neither regex matched Rust serde wording — the field path comes first and unknown variant after, the reverse word order of pattern 1. Every such request kept failing upstream with no self-heal.

  • src/server/relay.ts: the funnel gate admits 400 or 422 (request-validation error bodies for these statuses are small and are already buffered below the gate).
  • src/compat-roles.ts: detectRoleRejection and detectSystemPlacementError accept 400/422; ROLE_REJECTION_PATTERNS gains a serde-shaped third rule (role … : unknown|invalid|unsupported variant <name>).
  • All conservative gates stay 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.

Tests

  • New kernel/tests/responses-head-role.test.ts (5 cases: hoisted system, hoisted developer, mid-history system must not become the head role, no-head default, explicit-role injection). Reverting only kernel/src/wire/responses.ts turns two of them red; the default-role guards stay green, which is the point.
  • Part 2 in tests/compat-roles.test.ts: unit rows for the serde shape on 400/422 plus stopword / non-validation-status negatives, a 422 row for the placement detector, and an e2e that mirrors billion-context: 400 Bad Request when codex sends role: "developer" messages — request a request-body compat transform hook #552-E against a serde-422 upstream (auto-retry developer→system, session learns, second request skips the round-trip). node --import tsx --test tests/compat-roles.test.ts → 21 passed, 0 failed.
  • npm --prefix kernel test → 1633 passed, 0 failed.
  • bili suite, responses area (tests/*response*.test.ts, 36 files) → 226 passed, 1 skipped; two e2e cases (e2e chat relay … marker protocol, Responses WS: a title-persona side request …) tripped under 36-file concurrency and pass when run individually — I am not claiming a clean full-concurrency run.
  • tsc --noEmit clean in both kernel/ and the repo root.
  • kernel/package.json bumped to 0.0.191 with the lock files synced, per the kernel boundary.

Part 2 was originally offered as a separate change; it lands here instead under the one-fix-PR-per-thread rule — same issue (#6063), same symptom (this very 422), different mechanism layer (request rewrite vs learn-on-failure). Together the PR covers both halves of the report: the request goes out right, and clients that still send developer self-heal instead of failing every turn.

@ranxianglei ranxianglei changed the title kernel: reuse the client's leading message role when rebuilding the Responses input [PR] kernel: reuse the client's leading message role when rebuilding the Responses input Nov 9, 2026
@ranxianglei ranxianglei added bug Something isn't working dsh p1 scope/wiring labels Nov 9, 2026
…de 422 rejections (for ranxianglei#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.
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

CI auto-fix run (attempt 1/3): docs-sync fixed and verified green.

What was broken: docs-sync runs node tools/gen-config-docs.mjs generate on the PR merge ref and fails on any drift between the generated artifacts and the committed ones. On head dc0e0dd it flagged exactly 4 files:

Fix: ran the generator on the PR head; the output matched the CI failure diff byte-for-byte (same hunks, same blob hashes), so I committed the four regenerated files as 14a6d860 (docs: regenerate config docs from seed for persist.maxDelayMs (docs-sync drift)). Before pushing I confirmed git diff --exit-code on the four tracked files was clean against the fresh generator output. Push discipline followed: fetch + rebase onto the remote head first (no concurrent commits at that point), normal push, no force-push.

Concurrent work handled: while CI was starting, a second commit abf9e4c8 landed on the branch above mine (prose correction to the manual ### persist Description section, en+zh — outside the generated blocks). I re-ran the generator on abf9e4c8: zero drift, so no further push was needed.

Live verification (mirror cache, this run): head abf9e4c8 — docs-sync: success; 0 failures out of 30 checks; all guard/static/test-import lanes green; 5 long-running test shards still in progress (they were skipped on the previous head because docs-sync gated them via needs:). If one of those turns red, it's a separate wake — nothing failing right now.


八问(中文结论)

  1. 是什么问题:docs-sync 检查失败——PR 新增的 persist.maxDelayMs 配置项在四个由生成器产出的文档文件里与种子(process.yaml)不一致。
  2. 什么原因导致的:PR 作者把索引表行手工写进了 configuration.md(en+zh),用的是早期草稿措辞,与自己种子里的 desc_en/desc_zh 不一致;且加完种子后没有重新跑 gen-config-docs.mjs,导致 website/main.js 和 website/docs/index.html 的 tt.persist 摘要行缺了 maxDelayMs 5000。根因是「手工编辑了本应由生成器维护的产物」,不是内容分歧。
  3. 影响范围与严重程度:仅文档展示层——配置参考页与网站文档会显示过时/缺失的旋钮描述;无任何运行时行为影响,严重度为体验瑕疵(CI 门禁红)。
  4. 怎么修复的:在 PR 分支上直接跑官方生成器 node tools/gen-config-docs.mjs generate,提交四个重新生成的文件(14a6d860),以种子为唯一事实源收敛漂移;推送前本地验证生成输出与已提交状态零 diff。
  5. 是否有回归问题:无。改动只落在生成器产物内,逐字节等于生成器当前输出;PR 其余手工文档段落(persist 节说明、smartctl 观测配方、env 表行)不在生成块内,未被触碰。默认值不变(maxDelayMs 默认 5000 = 原硬编码值)。
  6. 是否破坏缓存:不涉及(纯文档文件)。
  7. 是否改了设计方向:不涉及。
  8. 是否引入了新的配置:不引入新配置——persist.maxDelayMs / BILI_PERSIST_MAX_DELAY_MS 是本 PR 原有内容,本次只修文档同步;无新 env、无默认值变更、无迁移。

判定:docs-sync 已绿并实测确认;当前 30 项检查零失败,仅剩 5 个长时 test 分片在跑。本次自动修复任务完成。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b)

⚠️ Misposted comment disclosure: the previous comment (floor 5, "CI auto-fix run … docs-sync fixed") does not belong to this PR. It is the CI auto-fix report for upstream PR #6066 (persist: wire debounce/max-delay knobs into session-order & prefix-affinity writers, mirror thread #6068) — I resolved the wrong thread number when posting. It has no bearing on this kernel head-role PR and no action from you is implied by its content. Please ignore or delete it; leaving closure to you per agent policy.

@ranxianglei
ranxianglei merged commit 3cae710 into ranxianglei:master Nov 10, 2026
56 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.1137-master.3cae710 (rolling master tag).

npm install -g billion-context@0.0.1137-master.3cae710  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working dsh p1 scope/wiring

Projects

None yet

2 participants