Skip to content

[PR] fix: collapse adjacent output_config effort directives after fold reconstruction (#5688) - #5690

Merged
ranxianglei merged 4 commits into
ranxianglei:masterfrom
john08281:fix/5688-adjacent-output-config
Nov 5, 2026
Merged

ranxianglei merged 4 commits into
ranxianglei:masterfrom
john08281:fix/5688-adjacent-output-config

Conversation

@john08281

Copy link
Copy Markdown
Contributor

Fixes the deterministic 400 reported in #5688.

What

  • New egress helper dedupeAdjacentOutputConfig: collapses runs of adjacent {role:"system", content:[], output_config:{...}} effort directives down to the last one (later effort wins — same semantics the client's trailing active directive relies on).
  • Wired in prepare-anthropic.ts after fold reconstruction, before the outbound body is serialized. Clean histories hit an identity fast path (input array returned unchanged, no copy, no scan cost beyond one pass).
  • Fires a warn log line when a collapse actually happened, so the defect stays observable in production while being repaired.

Why this shape

Evidence chain from the issue: the client's own history (197 messages, all directives) passes OpenRouter verbatim; the same history through the proxy after a fold 400s with Two adjacent output_config directives are not allowed at messages[2].output_config. The proxy never creates or edits output_config — the adjacency appears when reconstruction removes the message between two directives.

Detection is deliberately narrow (empty-content system message carrying an output_config object — exactly the directive shape). Text-bearing system messages and empty system messages without output_config are never touched, verified by tests.

Tests

tests/issue5688-adjacent-output-config.test.ts (node:test, project convention): clean-path identity, two-adjacent collapse, three-run collapse, text-bearing system untouched, no-output_config system untouched, absent-content directive detected, non-adjacent preserved. 7/7 pass via node --import ./tests/preload-test-env.mjs --import tsx --test; tsc --noEmit -p . clean.

Scope note

Only the Anthropic lane is wired (that's where the directive exists today). If other lanes grow per-turn effort directives (OpenAI configuration_update, Responses items), the same helper applies — left unwired per minimal-change discipline.

…onstruction (ranxianglei#5688)

Fold reconstruction can remove the message sitting between two per-turn
effort directives (reasoning hygiene, CCR excerpting, range trimming),
leaving them adjacent. OpenRouter's Anthropic Messages surface rejects
that shape with a hard 400 — "Two adjacent output_config directives are
not allowed" — and the session fails deterministically on every retry.

Repair at egress: collapse runs of adjacent
{role:"system", content:[], output_config} directives down to the last
(later effort wins, matching the trailing active-directive semantics).
Detection is narrowly the directive shape; text-bearing system messages
are never touched. Clean histories return the input array unchanged.
@ranxianglei ranxianglei changed the title fix: collapse adjacent output_config effort directives after fold reconstruction (#5688) [PR] fix: collapse adjacent output_config effort directives after fold reconstruction (#5688) Nov 4, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

查重:不重复。 多组关键词("output_config"、"adjacent directives"、"effort 400"、"adjacent system merge egress")检索 open+closed issue/PR:无既有条目记录本缺陷,也无在途修复 PR。最近亲为 #4570/#5437(mid-conversation system 位置违规 400 家族)——不同症状(位置规则 vs 相邻规则)、不同机制(位置漂移 vs fold 重建把两条豁免 directive 顶成相邻),且那两条规则明确豁免 directive-only 形态;本线程是该缺陷的唯一实现。出站前二次查重(我自己的修复提交之前):同样无冲突。

标签:bug / pi / scope/wiring / p1 —— 修复 #5690 的 p1 缺陷;pi 车道(OpenRouter anthropic-messages);硬失败点在出站管线。

审核结论:方向对、方案对、测试有牙——但首版漏了同一机制的两个出网点(nudge-decide 旁路调用、loop round-2 重建)。我已直接补进本 PR 分支(两个增量标注 commit),原 commit 与作者署名完整保留。

① 是什么问题
fold 之后 bili 重建的出站历史里可能出现两条相邻的 pi per-turn effort directive({role:"system",content:[],output_config:{...}}),OpenRouter 对该形状硬拒(Two adjacent output_config directives are not allowed)→ 确定性 400、会话死循环。客户端自身历史(含全部 directive)直连 OpenRouter 通过(issue test 3)——相邻性完全由 proxy 侧 fold 重建产生。

② 什么原因导致的
客户端在每条带 effort 的 assistant 消息前 + 尾部各发一条 directive,正常时两条 directive 之间总有 assistant 消息。fold 重建删掉/合并两条 directive 之间的消息后把它们顶成相邻。proxy 从不创建或改写 output_config——已核 kernel 侧 verbatim 保证:kernel/src/wire/anthropic.ts:307-322(directive 走 rawAnthropicMessage sidecar)+ 345-364(coreToAnthropic 逐字重放,空 content 即使落 cache_control mark 也回 raw)。所以这是序列整形缺陷,不是字段损坏缺陷;producer 有多个(reasoning hygiene / CCR excerpting / range trimming / empty pruning),逐个堵会漏。

③ 影响范围与严重程度
所有走 anthropic-wire 车道 + 上游执行该规则(今日为 OpenRouter)的 pi 会话(及未来同类 directive 客户端),触发条件 = fold 覆盖两条 directive 之间的 span。p1:fold 状态持久、每次重试原样重发 → 确定性 400 死循环,会话在人工干预前完全不可用。

④ 怎么修复的
出站层不变量修复(疏而非堵各 producer):通用 helper dedupeAdjacentOutputConfig 把相邻 directive run 折叠到最后一条(later effort wins——与客户端 trailing active directive 依赖的语义一致;相邻两条之间无任何内容,前一条被后一条完全覆盖,折叠无损)。检测刻意窄(system + 空/缺 content + output_config 对象 = 恰好 directive 形状),text-bearing system 与普通空 system 永不触碰;干净历史走 identity fast path(返回输入数组同一引用,零拷贝零扫描成本)。接线覆盖全部三个 anthropic-wire 出网点(PR 原版只覆盖一个,见下)。

⑤ 是否有回归问题(既有行为清单逐项)

  • 干净历史稳态出站:字节不变(identity fast path,测试钉了数组引用相等)✓
  • nudge-decide 旁路调用(新功能:增加模型完全决策的压缩时机nudge触发 #2228 decide ladder):PR 原版 sideBody(prepare-anthropic.ts:533)在 dedupe 之前构建——存在相邻时该旁路模型调用同样 400(后果有限:当轮跳过 nudge 注入、自愈,但同签名 400 污染日志且白白烧一次调用)→ 我的补充 commit 已修(sideBody 构建处套同一 helper)
  • loop round-2 重建 / post-compress fold-refresh 重请求:relay.ts:2215 把 core messages(prepared.processedMessages,仍含两条相邻 directive core)喂给压缩 loop,adapter-anthropic buildRequest(loop/adapter-anthropic.ts:310)从 cores 经 coreToAnthropic 重渲 → 每次 re-request(盲截断重试、429 重试、in-turn fold-refresh)都把相邻性带回线上——代理模式下这正是 fold 后的第一失败点,PR 原版未覆盖 → 我的补充 commit 已修(buildRequest 处套同一 helper;helper 抽成叶子模块 src/dedupe-adjacent-output-config.ts 打破 server.ts↔loop 导入环,server.ts 保留 re-export,现有 import 与测试零改动)
  • warnAnthropicThinkingPairs 第二参 rebuiltMessages→dedupedMessages:更准(按实际出站 warn),仅折叠发生时差异 ✓
  • kernel transform 失败 fallback(catch 分支转发未处理体):相对 PR 基线行为不变 ✓
  • 早退 passthrough 路径(classifier passthrough、global-scope system [Bug][claude MITM] 注入 ACP 工具破坏宿主请求:scope:"global" 缓存前缀 400 + Threads continue 指纹不符(thread_fingerprint_mismatch) #2648):逐字转发客户端字节、不经 dedupe——与该路径「untouched」契约一致 ✓
  • plugin-render(Plugin lanes: pin identity to client msgids, render outbound from the server-owned history | 插件车道服务端正史:消息身份钉在客户端 id 上,出站前缀由正史渲染 #4491 S4)渲染的前缀由客户端拼回下一请求、仍过 prepare-anthropic → 下游被覆盖 ✓
  • incremental lane(dsh threads):dsh 不发这类 directive(pi 特有功能),与 PR body scope note 一致 ✓
  • 高危模式逐项:无语义漂移(干净路径字节不变)、无 fail-fast 变吞错、无阈值/时序/输出格式漂移(仅缺陷场景出站字节变化,即修复目标本身)、其余 lane(openai/responses/google)有意不接 ✓
  • 未触碰 kernel/(无需 kernel-change 声明)、未触碰配置面 ✓

⑥ 是否破坏缓存
否。fold 后首轮出站即为折叠后的稳定形态,此后每轮字节相同 → 前缀缓存稳定(该轮的前缀本来就被 fold 打断)。主请求与 loop 侧用同一 helper、同一折叠结果 → round-1/round-2 前缀反而保持一致,避免跨轮 prefix mismatch。pf-* 水位通道与 marker-strip 继承不受影响(不经过此判定点)。

⑦ 是否改了设计方向
否。修复与既有「最终出站数组 heal」惯例同层(kernel 的 healIllegalSystemPositions 在 coreToAnthropic 内跑、host 侧 #4715/#5441 同层),不动 kernel 门禁、不改数据流;helper 抽叶子模块是纯重构独立 commit(行为不变)。

⑧ 是否引入了新的配置
无。无新 env/config/默认值。

测试有效性(质量关):7 个单测有牙——identity fast path 钉数组引用相等(抓得住「拷贝了但内容没变」这种假实现)、2/3-run 折叠 deepEqual 验证 keep-latter、负行钉住「text-bearing system 不碰 / 无 output_config 空 system 不动 / 非相邻保留」。本地定向验证:4 文件 19/19 绿(本文件 + issue2499 round-2 cache + anthropic-round2-stream + issue3004 decide selfheal),tsc --noEmit 干净;全量交远程 CI。诚实边界:没有新增穿透 prepareAnthropic 端到端断言「fold 重建后 wire 数组无相邻」的 hermetic e2e(helper 层有单测钉住、接线层靠代码走读 + 相关套件不回归佐证);真实环境 A/B(pi+OpenRouter 原始复现)建议在合并后用原会话验证一次。

我对本 PR 的操作(增量 commit,已推送到你的 fork 分支 john08281:fix/5688-adjacent-output-config,原 commit 与署名完好):

  • 8cab8fe45 refactor: extract dedupeAdjacentOutputConfig to its own module(破导入环,行为不变)
  • 95ddfa482 fix: apply adjacent output_config dedupe to nudge-decide side body and loop round-2 rebuild(补同一机制的两个漏网出网点)
  • 附带清理:推送核验时我在 ranxianglei/billion-context 误建了同名 stray 分支,已立即删除,对 PR 零影响。

CI:head 95ddfa4 上运行中(回帖时镜像缓存尚未登记 check 结果,落地后我在此线程补结论)。

判定:CI 转绿即可合(我不能代合,请自行合并:https://github.com/ranxianglei/billion-context/pull/5690)。

@ranxianglei ranxianglei added bug Something isn't working p1 pi scope/wiring labels Nov 4, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

更正上一条的 CI 行(我之前写「运行中」,不准确):

本 PR 来自外部 fork(head = john08281:fix/5688-adjacent-output-config),而本仓库对 fork PR 启用了 workflow 审批门禁——GitHub Actions 在维护者点「Approve and run」之前根本不会启动。核对证据:

  • 我推上去的 head 95ddfa48289 与原作者 head 3403429dfc3 的 check-runs 都是 total 0;
  • 仓库最近 30 条 workflow runs 全部来自 base 仓 ranxianglei/billion-context,没有一条来自该 fork。

所以镜像缓存里一直是空 checks——不是我的代码没触发,是平台层门禁拦住了。这一步需要人工动作:维护者在 PR 页点一次「Approve and run」(或把分支落到 base 仓再开 PR),CI 才会跑起来给出全量绿/红判定。

在此之前,本地已完成的定向验证(非全量、按 §7.2 纪律)供参考:

  • 4 个定向测试文件 19/19 绿(本文件 7 + issue2499 round-2 cache + anthropic-round2-stream + issue3004 decide selfheal);
  • tsc --noEmit 干净。

结论不变:代码侧我认为可合;但全量回归判定要等你解锁 CI 后由远端给出。 我不能代点审批、也不能代合——两者都留给你:PR #5690 。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

🔶 需要决策

背景一句话:本 PR(外部 fork 提交)的 CI 被仓库的 fork-workflow 审批门禁拦住,维护者不点「Approve and run」它就永远不会跑,全量回归判定因此悬空。

待决问题:如何解锁本 PR 的远端 CI?

选项:

  • A. 你在 PR 页点一次「Approve and run」——CI 直接在 head 95ddfa48289(含我的两个补充 commit)上跑全套。代价:一次点击;fork 流程原样保留,作者署名不动。
  • B. 我把这 3 个 commit 落到 base 仓分支并把 PR head 指过去(可代做)——CI 自动触发。代价:PR head 离开作者的 fork(commit 内容与署名仍在历史里),作者 fork 里的分支停在旧 sha(无害)。
  • C. 跳过 CI 直接合——代价:放弃全量回归门禁,只靠本地定向证据(19/19 + tsc 干净),不推荐。

我的推荐:A(最小动作、零副作用)。不决定的默认行为:PR 保持 open、判定停留在「代码侧可合、待 CI」,我不再重复催促,等你解锁后我补 CI 结论。

… path (ranxianglei#5688)

The unit suite pins the helper in isolation; nothing pinned the wiring —
deleting the call site at the final assembly would have stayed green.
Hermetic A/B rig (mock upstream that enforces OpenRouter's adjacency rule)
drives a real /bili/ request: positive direction asserts the collapsed
single directive (keep-latter) reaches upstream with a 200, negative
direction asserts the healthy pi shape passes through untouched.

Added during PR review of this branch.

@buyiking01 buyiking01 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed the diff against master (did not check out or run the suite — note this branch reports no CI checks).

Direction looks right. "Later wins" matches the per-turn directive semantics: the earlier directive's assistant message is gone after fold reconstruction, so it has no effect left to preserve. Keeping the referential-equality fast path (removed > 0 ? out : messages) is also the right call for downstream consumers.

Two things I'd change before merge:

1. src/server/prepare-anthropic.ts:125-127 — the warn will repeat on every request.
Dedupe only happens at egress, and rebuiltMessages is re-derived from session history each turn. As long as the fold produces the same adjacency, this log("warn", ...) fires per request for the whole session. Suggest reporting once per session (or dropping to debug) so this doesn't drown out other warnings.

2. src/dedupe-adjacent-output-config.ts:34-37 — the shape test is asymmetric.

  • Misses: content: [{ type: "text", text: "" }] is a non-empty array, so it returns false; if upstream treats that as a directive shape too, the adjacency survives.
  • Over-matches: content: null (or any non-array/non-string value) falls through all three checks and return true, so two system messages that should both be kept would collapse into one.

An explicit allowlist would be safer: only content === undefined, [], or "" count as a directive.

Optional (P3): the comment claims "every anthropic-wire egress surface", but coreToAnthropic( is also called from src/loop/core.ts, src/incremental-lane.ts, src/server/budget.ts, src/plugin-render.ts, src/image-rescale.ts. The current three sites look sufficient, but this is structurally "a future egress will silently miss the repair". Consider moving the repair to the fold-reconstruction choke point and keeping egress for assertion/logging only.

Test gaps worth adding: content: "" and content: null expectations (the latter is currently unspecified), and a "system message with text + output_config must be preserved" case.

Since the branch has no CI, locally running npm run typecheck plus node --test tests/issue5688-adjacent-output-config.test.ts tests/issue5688-egress-wiring.test.ts before merge seems worthwhile.

@buyiking01

Copy link
Copy Markdown

Correction to the review above — the line numbers I quoted were diff offsets, not file lines. Corrected references (head 2b3ef9f):

  • src/server/prepare-anthropic.ts:606-609 — the dedupe call and the per-request warn (the nudge side-body site is at :535).
  • src/dedupe-adjacent-output-config.ts:27-31 — the shape checks that isDirective makes; the referential-equality fast path is :43.

Substance unchanged.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

收到更正——我在 head 2b3ef9f 上逐条核过,四处引用全部准确:

  • src/server/prepare-anthropic.ts:606(dedupe 调用)、:607-609(warn 分支)✓;nudge side-body 站点 :535 ✓
  • src/dedupe-adjacent-output-config.ts:27-31(isDirective 四个形状判定)✓;:43(return removed > 0 ? out : messages; 引用相等 fast path)✓

实质结论不变。另注意到第 4 个 commit(2b3ef9f,test-only)补上了我 review 里点名的缺口:tests/issue5688-egress-wiring.test.ts 用真 startServer + 逐字复刻 OpenRouter 相邻校验的 mock upstream,把 dedupe 钉在完整 prepare 路径上(正例:相邻折叠为后者、5→4、keep-latter;负例:健康 pi 形状历史七条全过、identity fast path)。本地定向跑 2/2 绿,tsc --noEmit 干净。

CI 状态不变:仍卡在 fork-workflow 审批门禁(见上一条 🔶,等你「Approve and run」或选 B)。解锁后我补全量结论。

@ranxianglei
ranxianglei merged commit 3f822d1 into ranxianglei:master Nov 5, 2026
55 checks passed
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.1039-master.3f822d1 (rolling master tag).

npm install -g billion-context@0.0.1039-master.3f822d1  # 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants