Skip to content

[PR] batch-3: 6 P1 fixes from the community's open PRs (verbatim cherry-picks, authorship preserved) - #7107

Merged
ranxianglei merged 10 commits into
masterfrom
batch/p1-batch-2026-11-18-b3
Nov 18, 2026
Merged

ranxianglei merged 10 commits into
masterfrom
batch/p1-batch-2026-11-18-b3

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

batch-3: 6 P1 fixes from the community's open PRs (verbatim cherry-picks, authorship preserved)

本 PR 把 6 个社区提交的 P1 修复原样收进来(逐 commit cherry-pick -x,作者、commit message、测试全部保留)。每个修复独立评审过(对照 master 读全部源码 + 分支上跑测试 + master 上验证新测试确实红),文件互不重叠,零冲突。

每个 PR 是干嘛的

1. #7097 → #7096 [codex] compaction 门控只看实测证据

  • 问题:90% 健康度门控用 statusInputBaseline,该函数在没有实测 usage 时会替换成本地估计值。估计值可能把门控推错方向:估计偏高 → 错误放行上游自己跑 foreign compaction;估计偏低 → 错误由 bili 伪造。
  • 改法:门控改用新函数 evidenceInputBaseline(src/session.ts:961),只接受 usage / overflow-arm 来源的实测值;statusInputBaseline 其余调用方(status/ACP 读数)行为不变。
  • 评审结论:SAFE。副作用(从未被测量的会话 baseline=0 → 一律 bili 伪造 compaction)正是该 issue 要的行为——伪造 compaction 只是让历史缩小,无害。分支 41/41 测试绿,其中 3 个新测试在 master 上红。

2. #6918 → #6915 [claude-code] 拒收 message-threads beta

  • 问题:Claude Code 开着 message-threads-* beta 时发 thread delta 请求,走 threads lane 无法压缩。
  • 改法:对「bili 确实要压缩的 CC 会话」(cc session header + 线上 bili 工具或 MCP 注册 + anthropic + threads body)返回 400,body 精确匹配 Anthropic 标准 beta 报错形状 → CC 按文档自动去掉 beta 重试 → 变全量请求进正常压缩路径。非目标流量(无 cc header、bili 不压缩的 CC 会话)字节级不变。
  • 评审结论:SAFE,两个已记录的次要观察:count_tokens 带 threads beta 也会 400(CC 视为非致命);与 [PR] fix: claude MCP shim follows a Remote Control child's session id #6920 配对生效(见下)。147 行测试覆盖 create/continue/重试/非目标流量。

3. #6920 → #6915 [claude-code] MCP shim 跟随 RC 子会话重绑

4. #6869 → #6861 [claude] 固定客户端缓存标记,中途历史字节稳定

  • 问题:CC 每轮把 cache_control 挪到最后一条消息上,bili 只重发当前请求的标记集合 → 上一轮被标记的消息丢失 cache_control 字节 → 每轮请求都在改写中途历史 → 前缀缓存塌到只剩 system(实测单请求命中 8.7%)。
  • 改法:pinClientMessageMarks(src/loop/cache-control.ts,+95 行):见到客户端标记就固定重发,消息被折叠才退休(liveIds 过滤,session metadata 存储,无跨会话/跨 lane 泄漏路径);预算 = 4 − system 标记 − tools 标记,溢出只淘汰最旧的 pinned(永不动当前标记)。plugin mode(宿主自管标记)刻意不动。
  • 评审结论:SAFE。5 个测试含「turn-1 消息在 turn-2 序列化字节级相同」e2e。已记录局限:CC 常规 1 system + 1 tools 标记时 messageCap=2,最多多保 ~1 个中途 checkpoint——相对今天的 0 个仍是严格改善。

5. #6797 → #6796 [pi] 协商完整 Anthropic 请求

  • 问题:Pi ≥1.5.1 对 bili 路由的模型发 native thread delta,但 Pi 的指纹在 bili 改写之前算 → tool-result 续轮 top-level system 错 → 可复现的上游 429。
  • 改法:pi plugin 在 session_start / model_select(+before_agent_start 兜底重试)用官方 setModel 设 session 级 compat.supportsMessageThreads=false。双门:确有 bili 路由声明(proxyBaseForCtx,kill-switch 关闭则不触发)且流量确实被改写(MITM 白名单/proxy origin/拦截器)。模型对象克隆后修改,共享 catalog 不被污染;setModel 拒绝/抛错 → warn + 原样。
  • 评审结论:SAFE。直接 Anthropic-OAuth、未路由模型、OMP、旧版 host 全部不受影响(测试逐行钉死)。28/29 绿,唯一失败在 master 同样失败(pi-native 环境噪音)。

6. #7009 → #6459 [opencode] preflight 折叠会被本地 egress 熔断挡的探针

  • 问题:billion-context 卡死问题 —— 上报材料 usage获取不到导致上下文估计失误 #2313 的「仅估计触发 → 转发一次采集上游证据」路径,可能把本地 egress fuse 反正会 413 的 body 发出去,白白烧掉每次增长一次的证据预算。
  • 改法:转发前做一次与 fuse 逐 token 镜像的 measured 检查(同 window、同 max(declared, MIN_ASSUMED_WINDOW_TOKENS)、同 method/sidePassthrough 门、同严格 >)。命中即走正常压缩折叠。
  • 评审结论:SAFE。不能掩盖真实上游故障(被跳过的探针本来就会被本地 413,上游从未见过);不写 LocalRefusalRecord、不改 doom-gate 语义;side-lane 结构性不可达(新测试钉死)。7/7 绿,2 个新测试在 master 红。

著作权

本 PR 合并后

以下 PR 可以按 superseded 关闭(内容已整体进来):

验证

  • npm run typecheck ✅ / npm run build ✅(Node v25.9.0)
  • 11 个新/改测试文件 + 相邻套件:166/166 通过(8 skipped 为平台条件)
  • 新测试在 master 上逐一验证为红(回归检测力确认)

Fixes #7096, Fixes #6915, Fixes #6861, Fixes #6796, Fixes #6459

ReineLan and others added 10 commits November 18, 2026 10:16
…ompresses

Claude Code uses Message Threads through the proxy: each continue carries
only the new messages, so bili can neither nudge nor fold it (#2701). For
a Claude Code session bili compresses, bili now answers a threads request
with the 400 Anthropic gives for an unknown beta header. Claude Code drops
the header and threading for the session and retries with the full
history, which bili folds as usual.

A session counts as one bili compresses when its bili MCP tools are on
the wire, or, since Claude Code defers MCP tools behind tool search, when
bili's MCP server registered its x-claude-code-session-id. Only Claude
Code is declined: another Anthropic-wire client may not drop threads on
the 400. Requests without bili's tools keep the threads lane.

Fixes #6915

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 96274c7)
…/clear

Claude Code /clear (or /new) starts a NEW session id but keeps the stdio MCP
child running, so the spawn-time CLAUDE_CODE_SESSION_ID env (mcp.ts's
CONVERSATION_FROM_ENV) goes stale and neither recovery path fires: the #656 404
adoption needs the bound id to be unknown (it is still registered + active, so
calls succeed against it) and the #4168 409 re-bind needs a witness conflict
(none occurs). Tools silently run against the OLD session and the NEW session
never enters plugin mode.

The claude entry now injects a resolver that reads the host's CURRENT session
id from <CLAUDE_CONFIG_DIR|~/.claude>/sessions/<host-pid>.json (rewritten on
/clear; the host pid is stable for this child's lifetime). On each tools/call
the shim re-reads it and, when it differs from the bound id, re-binds +
re-registers so the new session enters plugin mode. Identity-bound hosts only;
dist/mcp.js (codex/zcode/kimi) is untouched — the resolver is injected solely
by the claude entry.

Closes #6361

(cherry picked from commit 029b7e0)
A Remote Control child (claude --print --sdk-url … --resume=<session
url>) spawns the shim under a fresh CLAUDE_CODE_SESSION_ID, then sends
its model requests under the remote session's id. The shim registered
the spawn id, so the proxy never saw the session as one bili compresses.

The #6361 re-bind ran only on a bili tool call, and found the host only
by a process named claude. The claude entry now re-checks the host's
sessions file on a 1 s timer from start-up (watchClaudeSession), and
isClaudeHostArgv accepts a native install path
<data dir>/claude/versions/<x.y.z>, which is how Remote Control spawns
its children.

Fixes #6917

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit b521f47)
…6861)

Claude Code stamps a cache_control breakpoint on its last message and
moves it as the conversation grows. anthropicToCore harvests those marks
into a per-request map that coreToAnthropic re-emits verbatim, so the
message that carried the breakpoint last turn loses it and its bytes
rewrite mid-history on every request, collapsing the prefix cache.

Pin client-managed breakpoints cumulatively, the same discipline as
bili's own marks: a marked message stays marked until it folds away,
within the 4-breakpoint budget. Proxy mode only; in plugin mode the host
owns the rolling marks.

(cherry picked from commit 753d7a3)
Fixes #6796. Preserve bili compression by disabling incompatible native thread deltas through Pi session-local model compatibility.

(cherry picked from commit 7d790d9)
…otiation

The session_start and model_select handlers are fire-and-forget, so a
throwing pi.setModel propagated as an unhandled promise rejection that
could crash the host process. Catch and log it, matching the OMP repin
convention; the before_agent_start call site keeps its own guard as
defense in depth. Update the reject-case test to assert the error is
handled instead of propagated.

(cherry picked from commit 5cac542)
…e from the side lane

The preflight egress-fuse probe block keys on !prepared.sidePassthrough, but
side requests never reach preflight — handle.ts forwards them with a minimal
prepared before the preflight call, and bodies over the effective window are
already rejected locally by the side payload guard (#554, 413
side_request_payload_too_large). Pin that exact shape (stuck estimate meter +
body above the fuse's 1M floor) so a future change that routes side traffic
through preflight, or makes the new block apply to side, fails here.

Lane-suite discipline per #2170: the lane-symbol line in src/server.ts
(#7009) lands with the dedicated side-lane suite.

(cherry picked from commit 27d0349)
@ranxianglei ranxianglei added the ci:full Run the FULL CI matrix (all e2e lanes + 12-leg test matrix) on this PR label Nov 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: batch/p1-batch-2026-11-18-b3 (572967f)

Latest build — install this exact version

npm install -g billion-context@0.0.10700-pr.7107.847bfd4

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

Recent builds (commit → version)

commit version build
572967f billion-context@0.0.10700-pr.7107.847bfd4 build

Download artifact

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

Install instructions are refreshed on each push.

@ranxianglei ranxianglei changed the title batch-3: 6 P1 fixes from the community's open PRs (verbatim cherry-picks, authorship preserved) [PR] batch-3: 6 P1 fixes from the community's open PRs (verbatim cherry-picks, authorship preserved) Nov 18, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷

查重: 不重复。这是 owner 开的批量收编 PR(batch-3),把 6 个社区 P1 修复原样 cherry-pick 进来。我核对了 open/closed 的 [PR] 列表:源 PR(#7097/#6918/#6920/#6869/#6797/#7009)与 #6478 都是被本 PR 收编的对象,不是并行重复;另一个 batch(wave-11 #6830)明确排除了 #6797/#6818,与本批次文件/修复无重叠。

审核方式: 拉取 PR 分支逐 commit 读源码 + 对照 master 验证既有行为 + 核对 cherry-pick 逐字性 + 查 CI 状态。

核对结果:

八问(批量整体):

  1. 是什么问题: 6 个社区 P1 缺陷的批量修复——①codex compaction 90% 门控误用本地估计值(方向可能推错)②CC 开 message-threads beta 时走 threads lane 无法压缩 ③CC /clear 或 RC 收养后 MCP shim 仍持旧 session id ④CC 每轮挪 cache_control 导致中途历史字节改写、前缀缓存塌到只剩 system(实测 8.7%)⑤Pi ≥1.5.1 发 native thread delta 导致可复现上游 429 ⑥billion-context 卡死问题 —— 上报材料 usage获取不到导致上下文估计失误 #2313「仅估计触发→转发一次采证据」可能把本地 egress fuse 反正会 413 的 body 发出去白烧预算。
  2. 什么原因导致的: 六个独立根因(各源 PR 已详述)。批量收编的理由:文件互不重叠、各自独立评审过、回归测试各自在 master 上验证为红。
  3. 影响范围与严重程度: 各自影响对应客户端车道(codex / claude-code / pi / opencode),均为 P1(可用性缺陷 + 缓存泄露类)。非目标流量(无对应 header/工具/门控)字节级不变。
  4. 怎么修复的: 每个都是最小定向改动,遵循既有模式——①门控改用只认实测证据的 evidenceInputBaseline(statusInputBaseline 其余调用方行为不变)②对「bili 确实压缩的 CC 会话」的 threads 请求返回 Anthropic 标准 beta 400,CC 按文档去 beta 重试 ③claude entry 注入 resolver 读宿主 sessions 文件,id 变化即重绑+重注册(1s 轮询 + tools/call 双触发,IDENTITY_BINDING 门控)④pinClientMessageMarks 累积固定客户端标记,消息折叠才退休,4-breakpoint 预算内溢出淘汰 ⑤pi 在 session_start/model_select 用官方 setModel 设 session 级 compat.supportsMessageThreads=false(双门 + 模型克隆 + setModel 拒绝/抛错兜底)⑥转发前做一次与 egress fuse 逐 token 镜像的 measured 检查,命中即走正常折叠。
  5. 是否有回归问题: 逐个审过 6 个修复的既有行为,未发现无意回归。重点核查的高危项:①statusInputBaseline 重构为共享 evidenceInputBaseline,其余调用方(status/ACP 读数)字节级行为不变;②threads 400 门控极窄(cc session id + bili 工具/MCP 注册 + anthropic + threads body + message-threads beta),consumePluginRegisterFor 是 LRU 刷新非删除(安全),declaresMcpBiliToolsOnWire 是严格 MCP 证据;③MCP 重绑仅 IDENTITY_BINDING(claude)生效,codex/zcode/kimi 的 dist/mcp.js 不动,timer unref() 不阻塞退出,文件读全程防御性;④缓存标记固定仅 proxy 模式 client-managed 会话,plugin 模式(宿主自管标记)刻意不动;⑤pi 双门(bili 路由声明 + 流量确被改写)+ 模型克隆不污染共享 catalog + setModel 拒绝/抛错 warn 后原样;⑥preflight 镜像与 relay egress fuse 的 measured 维度逐 token 一致(同 window、同 max(declared, MIN_ASSUMED_WINDOW_TOKENS)、同 method/sidePassthrough 门、同严格 >),fall-through 正确落到折叠路径。一处 body 措辞与代码不符(非代码 bug): body 说「溢出只淘汰最旧的 pinned」,代码实际淘汰最新的 pinned-only 标记(kept 从尾向头找第一个非当前标记)——代码是对的(seam 尽量靠尾部、稳定前缀最长),body 措辞反了,建议订正。
  6. 是否破坏缓存: 不破坏。Fix feat: OpenAI Responses API support (/v1/responses) #4 本身是缓存改善(固定客户端标记让中途历史字节稳定,前缀更稳),不引入缓存失效;其余修复不涉及缓存/重试/增量游标/去重键。
  7. 是否改了设计方向: 无架构/边界/数据流转向。六者都是定向修复,遵循既有模式与门控纪律。
  8. 是否引入了新的配置: 无新 config key / env / 默认值。Fix feat: OpenAI Responses API support (/v1/responses) #4 用 session metadata(anthropicClientCacheMarks,按会话隔离,无跨会话/lane 泄漏),Fix feat: persistent + namespaced sessions, multi-tenant session isolation, stream robustness #5 用既有 setModel/compat 机制。

合并判定: 代码层面可合——六个修复各自正确、门控清晰、回归测试有牙、无无意回归、无 kernel/config 变更。但有两点须 owner 把关: ①CI 仍在跑,合并须等 test 各 shard 绿;②这是 6 合 1 的批量 PR(触碰 src/server.ts 热文件 + 两处 wire 行为变更),按 §7.4 不属于 auto-merge 范畴,且批量收编后 6 个源 PR + #6478 要按 superseded 关闭——合并动作与后续关单请 owner 执行。

@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.1230-master.4b77eab (rolling master tag).

npm install -g billion-context@0.0.1230-master.4b77eab  # 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