Skip to content

fix: learn-on-failure ladder entry for placement-only system 400s (#1996) - #2014

Merged
ranxianglei merged 2 commits into
masterfrom
wt-1996-ework-daemon
Oct 4, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
wt-1996-ework-daemon

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #1996
Also repairs the pre-existing master typecheck breakage blocking every CI job: #2013 (separate labeled commit).

Mechanism (#1996)

Production 400 from vLLM serving a Qwen model over /v1/responses:
chat_template.jinja … Jinja Exception: System message must be at the beginning. — the model's Jinja guard rejects any literal role:"system" that is not the first chat-producing item (or any second system). vLLM passes role-bearing Responses items through verbatim (_construct_message_from_response_item returns dict items with a role key unchanged), so only a literal system can trip it.

bili never emits such items itself (front block and compression anchors ride as developer, nudges/separators as user, top-level instructions stripped). The offenders are client-origin mid-history system items forwarded verbatim by the plugin-mode position-preserving pass-through (#1638) — OMP reshapes its history around host compaction so one of those sits off-head post-compression. That trigger is client-side; the bili defect is what happened next:

The #552/#583 learn-on-failure role ladder never fired because its entry detector (detectRoleRejection) requires a named role in the error body — this error names none (invalid_prompt / invalid_request_error don't match either). Result: permanent 400 loop on every request of the session (two sessions in the log, retries hours apart).

Fix

Behavior changes (disclosure)

  1. NEW: on openai/responses wires, a 400 whose body matches the system-placement markers AND whose wire carries an off-head/duplicate literal system now triggers up to ONE extra retry hop (system→user). Old behavior: such 400s were relayed verbatim on every request (permanent failure loop). All other 400 paths unchanged.
  2. On repair success, {"system":"user"} is remembered in session-scoped learnedCompatRoles (not persisted config) and a permanent-config snippet is logged — same mechanism as billion-context: 400 Bad Request when codex sends role: "developer" messages — request a request-body compat transform hook #552/compat auto-fix: developer→system retry can still 400 on system-at-index-0 backends (#377 class); add second-chance developer→user learn #583.
  3. NEW: WC-014 gate in the fakes — tests that forward off-head/duplicate literal system items through startFakeUpstream("responses") now get a 400 (intended: ledger only grows).

No config-surface changes (no new fields/env/flags), no version bump, acp-kernel pin untouched.

Verification (targeted, local)

  • npm run typecheck — clean (was red on master pre-repair; see master npm run typecheck red — 4 test files break tsc, all CI test jobs fail #2013)
  • node --import tsx --test tests/compat-roles.test.ts tests/wire-contract.test.ts tests/e2e-responses-offhead-system.test.ts tests/responses-plugin-dev-position.test.ts — 39/39 pass
  • node --import tsx --test tests/issue1987-preflight-baseline-anchor.test.ts tests/issue1993-preflight-400.test.ts tests/ws-bridge.test.ts — 9/9 pass
  • npm run build — success
  • Full regression left to CI.

@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: wt-1996-ework-daemon (9453c7e)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2064-pr.2014.13f66d0

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr2014.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR #2014 review — 可合并(待人工合并)

审查基础:PR head 4fb0e6e5(与 master 持平,0 behind),逐行审完 10 文件 diff(+275/-15);本地定向验证(typecheck clean + 53/53 定向测试)+ GitHub CI 全矩阵核对。

① 方向关:方向正确,打在根因上。#1996 的缺陷是 learn-on-failure ladder 的入口条件过窄——detectRoleRejection 要求错误文本点名具体 role,而 vLLM+Qwen 的 placement 错误("System message must be at the beginning.")不点名任何 role,#552/#583 整套机制空转,每个压缩后请求永久 400 循环。修法是在决策点(ladder 入口)加双门:placement marker ∧ wire body 确实携带 off-head/重复 system 形状(hasOffHeadSystem),直接以 {role:"system"} 进 ladder、跳过 identity hop 做单次 system→user 修复。修的是产生缺陷的机制,不是转发路径上的字节过滤——符合「疏不堵」原则。

② 回归清点(逐项判定):

③ 代码关:hasOffHeadSystem 资格判定与 applyCompatRolesJson 精确镜像(同一 item 过滤规则),返回 true 时 system→user hop 保证 rewritten≥1,不会白发请求;virtual-leading(top-level instructions)分支是防御性的——bili 在 rebuild 时已 strip instructions(src/server.ts:4785、src/loop/adapter-responses.ts:321),即便在未 strip 路径触发,修复结果(instructions 成为唯一 leading system)也合法。集成点用 wireBody(#1900 契约:实际发出的字节)+ 无条件 armed 的 compatProtocol;remember() 复用现有 learnedCompatRoles session 机制并同步请求内 resend。fake 的 WC-014 错误文本本身命中 detectSystemPlacementError marker——validation-parity 闭环,e2e 里的 400 真实驱动了 ladder 而非绕过它。

④ 测试有效性:单测有牙——钉住 detectRoleRejection(prod)=null(gap 本身)+ detectSystemPlacementError(prod)=true(生产错误串逐字取自日志附件),形状表覆盖两 wire 及边界(instructions virtual-leading、tool item 前导、developer 干净、不可解析)。e2e hermetic、无 gate、port 0(确定性)、XDG 隔离,覆盖全生命周期:客户端见透明 200、原始 forward 保持 offender 原样(faithful wire)、恰好一次修复重试、turn 2 主动携带 learned mapping 干净通过(无第二次 violation)。若 ladder 入口回归,turn 1 会 400,测试抓得住——真回归覆盖,非恒真断言。

⑤ diff 整洁度:每行都属于 #1996 或 #2013(两个线性 commit、独立标注),无空白/lockfile/version churn。干净。


结构化七问

  1. 是什么问题:vLLM 服务的 Qwen Jinja 模板拒绝非首位/重复的字面 system 消息;OMP 把宿主压缩 checkpoint 作为 mid-history system 项转发,bili 按 [Bug] bili 启动 OMP 后缓存命中率 82.7%,连续请求多次骤降至 4992 cached tokens #1638 位置保真透传;learn-on-failure ladder 因错误不点名 role 而不触发 → 永久 400 循环。
  2. 什么原因导致的:ladder 入口条件要求错误点名 role(detectRoleRejection);placement-only 错误无名可点,机制空转,尽管 wire 本身就携带违规形状。
  3. 怎么修复的:双门入口(placement marker ∧ wire 形状证明)让这类 400 以 {role:"system"} 直接进入现有 ladder,跳过 identity hop 做单次 system→user 修复,经现有 learnedCompatRoles 按 session 记住;另按 [CI/testing] wire-contract anti-regression system: validation-parity fake upstreams + golden schema snapshots + constraint ledger + staged 4-protocol x 3-mode client smoke matrix #1304 纪律补 WC-014 ledger 条目 + validation-parity 条款。
  4. 是否有回归问题:无(见 ② 逐项)。CI 全矩阵绿(16/16:ubuntu/windows × node 22/24、e2e、registry e2e、build、version-guard、全部 real-client lane);本地定向验证 53/53 + typecheck clean。
  5. 是否破坏缓存:否。改写只作用于出站字节(billion-context: 400 Bad Request when codex sends role: "developer" messages — request a request-body compat transform hook #552 既有模式);turn 2 主动改写的 prefix 与 turn 1 被接受的 retry 字节一致,上游 prefix cache 保持;kernel/session 状态在 wire 前计算,不受影响。
  6. 是否改了设计方向:否——同一模块/同一机制内为现有 ladder 增加第二个入口条件;wire-constraint ledger 纪律照守(只增不删)。
  7. 是否引入新配置:否。复用现有 session 级 learnedCompatRoles metadata + 日志输出可选固化的 config snippet(既有模式)。无新 env/config/默认值。

行为变更披露(PR 描述已声明,核实无误):

  • NEW:openai/responses wire 上,匹配 placement marker 且携带 off-head/重复字面 system 的 400 → 至多一次额外重试 hop;旧行为:原样透传 = 永久失败循环;其余 400 路径不变;
  • 修复成功后 {"system":"user"} 记入 session 级 learnedCompatRoles(不落盘 config),日志给出可选固化 snippet;
  • NEW:validation-parity fake 中 WC-014 门——向 startFakeUpstream("responses") 转发 off-head/重复字面 system 的测试现在会收到 400(有意,已验证无既有测试命中)。

收工判定:✅ 可合并。按 §7.4,本 PR 触碰请求管道行为(新增 live retry hop),留人工合并。CI 已全绿(mergeable_state: blocked 与 CI 无关——16 项 check 全部 success,应为分支保护等待人工动作)。请手动合并:#2014

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

压缩的时候system消息不在最前面报错

1 participant