Skip to content

Anthropic Threads lane: unified-store mirror + over-budget reforge into fresh creates (#2926, #2884) - #3332

Merged
ranxianglei merged 4 commits into
masterfrom
2026-10-15_threads-reforge
Oct 16, 2026
Merged

ranxianglei merged 4 commits into
masterfrom
2026-10-15_threads-reforge

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

Anthropic Threads moves conversation state server-side: a create carries the full history once, every later continue carries only a delta (previous_message_id), and the API offers no way to rewrite what already sits in the thread. bili's core lever — rewriting the resent history — therefore does not exist on this wire. Today threads turns hit the #2648 global-scope stand-down (Claude Code's main loop runs threads with a global-scope system block), forwarding byte-faithfully but with folding silently disabled for the whole session (#2884) and no path to ever shrink the thread.

This PR adds the threads lane (#2926):

  1. Capture (always on wherever the unified sidecar lives): every create/continue is mirrored into session_ext (threads:mirror:meta + threads:mirror:chunk:NNNN, packed under the 4 MiB per-value cap; oversized entries elided — bulk media belongs in the CAS). A create replaces the mirror (it is the client's authoritative snapshot) and captures the session "shape" (model/system/tools/max_tokens); a continue appends its delta.
  2. Passthrough: all turns forward byte-faithfully — no marker injection (markers would live in the thread forever), no preflight (a continue body is delta-only and says nothing about the real thread size; the mirror is the lane's only budget truth). The lane claims the turn before the [Bug][claude MITM] 注入 ACP 工具破坏宿主请求:scope:"global" 缓存前缀 400 + Threads continue 指纹不符(thread_fingerprint_mismatch) #2648 global-scope stand-down, so CC threads sessions stop silently losing folding ([Bug][claude MITM] The global-scope passthrough in #2650 lets the entire Claude Code main conversation pass through unprocessed, rendering compression completely non-functional on Claude Code (Note: inconsistent injection within the same Thread causes fingerprint mismatch) | [Bug][claude MITM] #2650 的 global-scope 放行把 Claude Code 主对话整条放行,Claude Code 上压缩完全失效(附:同一 Thread 注入不一致导致 fingerprint mismatch) #2884).
  3. Reforge (rollout anthropicThreadsReforge, default 0%): when the mirror estimate (shape + summary + post-fold entries) crosses the window on a continue, bili swaps that continue for a fresh create carrying [summary of the folded prefix, verbatim tail ring (last 3 user turns — cut on a user-turn boundary so tool_use/tool_result pairs never split), this turn's delta]. One summarizer side-call rides the client's auth headers and the same upstream; the client's system/tools are copied byte-faithfully from the captured shape — host instructions never enter the summary. Because the message-id chain IS the thread identity, the client's next continue (echoing the new tail id) lands in the new thread with zero redirection; a late continue into the dead old thread is already handled by [Bug][claude MITM] Threads 下 bili 压缩回路在 continue 轮次全面失效:nudge 失明、mid-turn compress 400(previous_message_id already continued)、折叠只能在 create 送达 #2701 retargeting semantics.
  4. Fail-open everywhere: legacy-only storage (no unified sidecar) captures nothing and never reforges; a failed/empty summary side-call forwards the original continue untouched; mirror write failures degrade the lane to passthrough for that session with one warn.

Gating

rollout.features.anthropicThreadsReforge: true / BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE=1 (default 0% — capture rides along whenever the sidecar is available, the reforge swap is what the flag gates).

Tests (tests/threads-lane.test.ts, 7/7)

Regression: issue2701 (10), issue833/golden-storage-render, persist-tail + storage-unified (23), tsc clean, unused-export gate clean.

Part of #2926; supersedes the stand-down half of #2888 for the threads case.

prepared by an agent under the local git identity (ranxianglei), human merge required

…and reforge over-budget continues into fresh creates (#2926)

Threads state lives server-side; a continue body is delta-only no matter
how big the thread grew, so the history-rewrite lever does not exist on
this wire. The lane:

- captures every create/continue into session_ext (threads:mirror:*),
  create being the client's authoritative full snapshot;
- forwards all turns byte-faithfully with preflight skipped (the wire
  body says nothing about the real thread size — the mirror is the only
  budget truth);
- on a continue whose mirror estimate crosses the window, with rollout
  feature anthropicThreadsReforge on (default 0%), swaps it for a fresh
  create carrying [summary, tail ring (last 3 user turns), delta] — one
  summarizer side-call on the client's auth, client system/tools copied
  byte-faithfully; the message-id chain carries the client into the new
  thread with zero redirection;
- claims the turn before the #2648 global-scope stand-down, which used
  to swallow whole Claude Code threads sessions (#2884);
- fails open everywhere: legacy-only storage and failed side-calls
  degrade to pure passthrough.

Fixes #2884 (with the lane claiming threads turns ahead of the stand-down).
Part of #2926.
@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-15_threads-reforge (2e37d77)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.3940-pr.3332.7b95504

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-pr3332.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] 🏷 (ai: vllm-qwen/qwen3.8-27b)

查重结论:不重复。 全仓检索(open+closed,按失败签名「threads / reforge / unified-store mirror」匹配)确认本 PR 是唯一做 threads-reforge 的 PR,无并行实现。关联关系(非重复):

判定:症状(threads 无法折叠)与机制(reforge into fresh create)均为本 PR 独有,#2888 是相邻但不同的失败域(gate 谓词),不构成 100% 重复,保持各自打开、互相链接。

正在做独立审核(方向关 → 回归关 → 代码关 → 测试有效性 → diff 整洁度),结论稍后按结构化八问给出。

awork added 2 commits October 16, 2026 12:03
…d can't leak a stale fold point

A threads `create` is the client's authoritative snapshot, but captureThreadsTurn
spread the prior meta (`{ ...meta, shape, shapeTokens }`), carrying any earlier
foldedBefore/foldedSummary forward under the same session id. If a session id is
re-created after an earlier thread was reforged (e.g. a resume), the next reforge's
ringCutIndex treats unsummarized new-thread entries as already folded and drops them
from both the summary transcript and the tail ring — silent loss of early turns.

Reset bili's own fold tracking on create via freshMeta(); the stale fold state now
cannot survive an authoritative replace. Adds a regression test that fails without
the reset (re-create after a reforge must yield foldedBefore=0 / no summary).

Part of #2926.
The test already pulls mkdtempSync/rmSync from node:fs by name; the default
`import fs` was only kept alive by a `void fs;` to satisfy the unused-import
gate. Remove both.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

PR #3332 审核结论(独立审核,非复述 issue 结论)

先说处置:我在分支上补了 2 个 commit 并已 push(71406c88b..2e37d7720)——修掉一个真实的数据丢失边界 + 一处死代码,并给修复加了带牙齿的回归测试。本地定向验证 tests/threads-lane.test.ts 8/8 绿、tsc --noEmit 干净;CI 会在新 head 上重跑(我盯结果)。


结构化八问

  1. 是什么问题:Anthropic Threads 把会话状态放服务端——create 带全量历史一次、之后 continue 只带增量(previous_message_id)、API 无法改写已入线程的内容。bili 的核心杠杆(改写重发历史)在这条线上不存在。CC 主循环用 global-scope system 跑 threads,撞上 [Bug][claude MITM] 注入 ACP 工具破坏宿主请求:scope:"global" 缓存前缀 400 + Threads continue 指纹不符(thread_fingerprint_mismatch) #2648 全局作用域 stand-down,整个会话静默禁用折叠([Bug][claude MITM] The global-scope passthrough in #2650 lets the entire Claude Code main conversation pass through unprocessed, rendering compression completely non-functional on Claude Code (Note: inconsistent injection within the same Thread causes fingerprint mismatch) | [Bug][claude MITM] #2650 的 global-scope 放行把 Claude Code 主对话整条放行,Claude Code 上压缩完全失效(附:同一 Thread 注入不一致导致 fingerprint mismatch) #2884),线程永远缩不下去。本 PR 加 threads lane:镜像捕获 + 字节忠实透传 + 超窗把 continue 重铸成全新 create(summary + 尾环 + 本回合增量)。

  2. 什么原因导致的:根因是「服务端线程不可改写」与「bili 只能改写重发历史」的机制错配,不是某个函数写错;[Bug][claude MITM] The global-scope passthrough in #2650 lets the entire Claude Code main conversation pass through unprocessed, rendering compression completely non-functional on Claude Code (Note: inconsistent injection within the same Thread causes fingerprint mismatch) | [Bug][claude MITM] #2650 的 global-scope 放行把 Claude Code 主对话整条放行,Claude Code 上压缩完全失效(附:同一 Thread 注入不一致导致 fingerprint mismatch) #2884 只是这个错配的表象(stand-down 让折叠静默关闭)。方向判断正确:reforge 用唯一能向服务端线程投递折叠的通道(新 create,id 链即线程身份→零重定向)去治根因,不是在透传层把症状抹掉(疏不堵)。与 fix: anthropic global-scope gate — narrow predicate + default threads-lane passthrough cap (#2884) #2888 同家族但机制不同(fix: anthropic global-scope gate — narrow predicate + default threads-lane passthrough cap (#2884) #2888 收窄 [Bug][claude MITM] 注入 ACP 工具破坏宿主请求:scope:"global" 缓存前缀 400 + Threads continue 指纹不符(thread_fingerprint_mismatch) #2648 谓词 + 透传上限;Anthropic Threads lane: unified-store mirror + over-budget reforge into fresh creates (#2926, #2884) #3332 做镜像+重铸),PR 已声明取代 fix: anthropic global-scope gate — narrow predicate + default threads-lane passthrough cap (#2884) #2888 的 stand-down 半区。

  3. 影响范围与严重程度:仅命中 Anthropic Threads API 客户端(现实里基本就是 Claude Code,global-scope)。默认 rollout 0% 时只多写镜像(session_ext)、对客户端行为零变化;开启 flag 后超窗 continue 会被换成新 create。严重度:默认关闭 = 无外部影响;开启后若真实 Anthropic 不接受重铸 create 的形态,该次请求失败但 fail-open 退回原 continue。非阻塞。

  4. 怎么修复的(指我这次补的修复):captureThreadsTurn 的 create 分支原来 { ...meta, shape, shapeTokens } 会把同一 session id 上更早的 foldedBefore/foldedSummary 带过来。create 是客户端权威快照,必须重置 bili 自己的折叠追踪——否则「一个 session id 在早前线程被重铸过之后又发新 create」(如 resume)时,下一次 reforge 的 ringCutIndex 会把未摘要的新线程条目当成已折叠,从 summary 和尾环两边都丢掉 → 早期轮次静默丢失。改成 { ...freshMeta(), shape, shapeTokens },并加回归测试(无此重置时该测试红,已实测确认有牙)。另删测试里冗余的 import fs + void fs(死代码)。

  5. 是否有回归问题(逐项核对 diff 触碰路径承担的既有行为):

  6. 是否破坏缓存:不破坏。新增的是 session_ext 下 threads:mirror:* 键(运行时状态,additive,非持久化格式/版本变更);prompt cache 前缀不受影响(线程透传字节级稳定,summary 头是新 create 的第一条 user 消息,不进前缀锚点)。version 未动(功能未发布)。

  7. 是否改了设计方向:是,新增一条 lane(镜像+重铸)作为服务端线程的唯一折叠通道,与「顺序为中心」「疏不堵」一致(提供能投递折叠的通道,而非透传层过滤)。未污染边界:kernel/ 未触碰、pi-subagents/ 未触碰。

  8. 是否引入了新的配置:rollout.features.anthropicThreadsReforge(env BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE),默认 0%(off)。沿用既有 rollout 约定,config-surface-guard 已过。捕获随 sidecar 常在,flag 只门控重铸换体。


测试有效性(④):原 7 个都是真测试(真起 server + mock upstream,断言有牙:字节忠实 / 镜像累积 / side-call 形态 / create 换体 / system-tools 逐字 / summary 头 / delta 尾 / fail-open),非恒真。缺口:ring-cut 的「用户边界切分、tool_use/tool_result 不成对拆分」不变量未被测(现有用例全是 string content)——见下方 rollout 前置项。

rollout 开启前置(非合并阻塞,默认 0% 安全):

diff 整洁度(⑤/⑤b):原 PR diff 干净(+846/-1,每行都在目的内)。我顺手修了死代码(fs import)。遗留小注(非阻塞,建议后续):summary 请求构造里硬编码 "claude-sonnet-4-5" 与内联 max_tokens:8192(魔法值,fail-open 兜底,建议改具名常量);.slice(0,2000) 三处重复。

自动合并判定:不符合 §7.4(feat + 改线/消息形态 + 会话绑定逻辑)→ 人工合并,与「human merge required」一致。


结论:可合(默认 0%,安全)。 建议 owner 现在批准合并;把上面 #2/#3 作为「开启 rollout flag」前的独立门禁步骤,而非合并前提。CI 在新 head 2e37d7720 上重跑中,红了我就定向排查。

@ranxianglei
ranxianglei merged commit f94ee8f into master Oct 16, 2026
46 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.495-master.f94ee8f (rolling master tag).

npm install -g billion-context@0.0.495-master.f94ee8f  # 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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant