diff --git a/README.md b/README.md index 604bfa942a..4f987f38d6 100644 --- a/README.md +++ b/README.md @@ -238,7 +238,7 @@ Notes: - `kimi` reports runtime-info at bootstrap only (static headers can't carry per-request window/model values); subagent tool calls are routed by the proxy's outbound tool-use witness ring (#1685) — no model-visible conversation id. - `hermes`'s native plugin is Python: it points hermes' httpx stack at the proxy via env vars after a health check and stamps per-request headers through an `llm_request` middleware. - `codex` is the one client a plugin install cannot make self-sufficient: codex routes model traffic via env only (no config-file routing seam for the default ChatGPT-login provider — a managed `model_providers` block would force API-key auth and drop subscription login), and an MCP server cannot inject env into its parent. `bili plugin install codex` writes a single `[mcp_servers.bili]` block into `~/.codex/config.toml` (command = node, args = dist/mcp.js) exposing the four ACP tools; at session start the shell resolves a proxy — env `BILI_MCP_PROXY` > the live-instance record (any lane's proxy or a `bili start` daemon) > the 8787 user-zone default (#1660 removed the install-time origin bake, #403) — nothing reachable → `tools/list` fails with -32003. So: start bili first (`bili start` or any client's lane proxy), export HTTPS_PROXY yourself if you also want compression, or use `bili codex` for the zero-config full posture. Mechanics: [CLIENTS.md](CLIENTS.md#codex-openai-codex-cli). -- `claude` has a native posture (#964): managed settings block + `SessionStart` hook + MCP shell; the hook rides the self-managed port zone (#1660) and re-pins the managed URL to the live origin each session, so port drift self-heals. Since #1821 the MCP shell also watches the proxy mid-session (30 s tick × 3 failures) and respawns a dead lane proxy itself, so model requests recover within ~90 s instead of a new session (same semantics as the zcode lane). Opt out with `BILI_NATIVE_CLAUDE=0` (passthrough). Mechanics: [TECHNICAL-NOTES.md](TECHNICAL-NOTES.md). **Known limitation (#2290):** the Claude Desktop app's Code tab (`CLAUDE_CODE_ENTRYPOINT=claude-desktop`) sets its own `ANTHROPIC_BASE_URL` on its embedded Claude Code, overriding the managed one — desktop sessions bypass the proxy entirely while the managed block's `DISABLE_AUTO_COMPACT=1` still applies, so they get neither bili compression nor native auto-compaction. The hook warns loudly at session start (and records it for `bili doctor`, which also flags machines where Claude Desktop is installed alongside the native lane); terminal sessions are unaffected. **Field-tested 2026-10-06 (#2290):** putting `HTTPS_PROXY` in the settings `env` block produced zero CONNECT traffic to a live MITM-enabled proxy on Claude Desktop 2.19675.1 / embedded CC 2.1.288 (Windows 11) — the variable reaches no network stack there, matching the CLI behavior of that era — note that newer builds (CC 2.1.301+) DO honour `HTTPS_PROXY` on the CLI, which is why `bili claude` routes via the `/bili/` base-URL rewrite — the launcher gives this lane no proxy and no CA at all, and a child that brings a proxy of its own gets the loopback hosts merged into `NO_PROXY`/`no_proxy` so that leg stays direct regardless of its proxy behavior (#3446). As of that version there is no usable routing seam on the desktop Code tab; the lane stays tracked in [#2290](https://github.com/ranxianglei/billion-context/issues/2290) in case upstream changes behavior. Desktop-only users can restore native auto-compact with `bili plugin remove claude`. +- `claude` has a native posture (#964): managed settings block + `SessionStart` hook + MCP shell; the hook rides the self-managed port zone (#1660) and re-pins the managed URL to the live origin each session, so port drift self-heals. Since #1821 the MCP shell also watches the proxy mid-session (30 s tick × 3 failures) and respawns a dead lane proxy itself, so model requests recover within ~90 s instead of a new session (same semantics as the zcode lane). Opt out with `BILI_NATIVE_CLAUDE=0` (passthrough). Mechanics: [TECHNICAL-NOTES.md](TECHNICAL-NOTES.md). **Known limitation (#2290):** the Claude Desktop app's Code tab (`CLAUDE_CODE_ENTRYPOINT=claude-desktop`) sets its own `ANTHROPIC_BASE_URL` on its embedded Claude Code, overriding the managed one — desktop sessions bypass the proxy entirely while the managed block's `DISABLE_AUTO_COMPACT=1` still applies, so they get neither bili compression nor native auto-compaction. The hook warns loudly at session start (and records it for `bili doctor`, which also flags machines where Claude Desktop is installed alongside the native lane); terminal sessions are unaffected. **Field-tested 2026-10-06 (#2290):** putting `HTTPS_PROXY` in the settings `env` block produced zero CONNECT traffic to a live MITM-enabled proxy on Claude Desktop 2.19675.1 / embedded CC 2.1.288 (Windows 11) — the variable reaches no network stack there, matching the CLI behavior of that era — note that newer builds (CC 2.1.301+) DO honour `HTTPS_PROXY` on the CLI, which is why `bili claude` routes via the `/bili/` base-URL rewrite — the launcher gives this lane no proxy and no CA at all, and a child that brings a proxy of its own gets the loopback hosts merged into `NO_PROXY`/`no_proxy` so that leg stays direct regardless of its proxy behavior (#3446). As of that version there is no usable routing seam on the desktop Code tab; the lane stays tracked in [#2290](https://github.com/ranxianglei/billion-context/issues/2290) in case upstream changes behavior. Desktop-only users can restore native auto-compact with `bili plugin remove claude`. **Desktop threads exposure (#3744, field report on embedded CC 2.1.299):** the app enables Anthropic server-side threads only when `CLAUDE_CODE_TETHER_LIVE` (tri-state env) is unset, `ANTHROPIC_BASE_URL` is unset or hosts `api.anthropic.com`, the provider is firstParty, and `CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS` is off. So terminal sessions routed through bili never use threads (base URL ≠ official → full-history requests take the classic compression path), while desktop under cert-MITM forces `ANTHROPIC_BASE_URL=https://api.anthropic.com` → CC believes it talks to the official API directly → threads turn on (with global cache scope) — the hardest lane, which is what the threads reforge (#2648/#2701/#3569) exists for. `CLAUDE_CODE_TETHER_LIVE=0` would disable threads, but the main conversation still carries `scope:"global"` and the non-threads passthrough rule (#2650) forwards it uncompressed anyway (untested). - `zcode` has a native posture (#1145): managed `~/.zcode/cli/config.json` block + per-session provider `baseURL` rewrite. Full mechanics: [CLIENTS.md](CLIENTS.md). - `jcode` and `aider` have no native mode (no plugin/MCP/tool-injection seam: #962, #1048) — use `bili jcode` / `bili aider`. - `copilot`, `amp` and `goose` are launcher-only (#1049); goose cannot be cert-MITMed (rustls trusts no CA file) and rides plain-HTTP base-URL redirects instead. diff --git a/README.zh-CN.md b/README.zh-CN.md index 353bbdc3ab..7541d60ea3 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -189,7 +189,7 @@ pi / omp / kimi / claude 没有客户端侧通道 —— 它们的配置条目 - OpenCode 旧会话、V1/V2 插件形态与全部注意事项:[OpenCode](CLIENTS.zh-CN.md#opencode)。 - `kimi` 仅在自举时上报 runtime-info(静态头无法承载逐请求窗口/模型值);子代理工具调用由代理的出站 tool_use 见证环路由(#1685)——模型看不到任何会话 id。 - `hermes` 的原生插件是 Python:健康检查通过后用环境变量把 hermes 的 httpx 栈指向代理,并经 `llm_request` 中间件打逐请求头。 -- `claude` 有**原生姿态**(#964):受管 settings 块 + `SessionStart` hook + MCP shell;hook 骑自管端口区(#1660),每会话把受管 URL 重钉到存活 origin,端口漂移自愈。#1821 起 MCP shell 还在会话内守护代理(30s × 3 次失败),代理死亡时自行重生,lane 内模型请求 ~90s 内恢复而无需新开会话(与 zcode 车道同语义)。`BILI_NATIVE_CLAUDE=0` 退出(passthrough)。机制:[TECHNICAL-NOTES.zh-CN.md](TECHNICAL-NOTES.zh-CN.md)。**已知限制(#2290):** Claude Desktop 应用的 Code 标签页(`CLAUDE_CODE_ENTRYPOINT=claude-desktop`)会为其内嵌的 Claude Code 设置自己的 `ANTHROPIC_BASE_URL`,覆盖受管值——桌面会话完全绕过代理,而受管块的 `DISABLE_AUTO_COMPACT=1` 仍然生效,因此既没有 bili 压缩、也没有原生自动压缩。hook 会在会话开始时大声警告并记录下来供 `bili doctor` 展示(检测到本机装有 Claude Desktop 时,该 lane 也会显示 ⚠️ 提示);终端会话不受影响。**真机验证(2026-10-06,#2290):** 把 `HTTPS_PROXY` 写进 settings 的 `env` 块后,在 Claude Desktop 2.19675.1 / 内嵌 CC 2.1.288(Windows 11)上对一台开启 MITM 的存活代理**零 CONNECT 流量**——该变量没有到达应用内任何网络栈,与该时期的 CLI 行为一致——注意新版构建(CC 2.1.301 起)在 CLI 上会遵守 `HTTPS_PROXY`,这正是 `bili claude` 走 `/bili/` base-URL 改写的原因——launcher 不再为该 lane 设置代理、也不再设置 CA,而自带代理的子进程会把 loopback 主机合并进 `NO_PROXY`/`no_proxy`,确保无论子进程的代理行为如何,这一腿都保持直连(#3446)。截至该版本,桌面 Code 标签页不存在可用的路由缝隙;该 lane 继续在 [#2290](https://github.com/ranxianglei/billion-context/issues/2290) 跟踪,若上游行为变化再议。仅用桌面的用户可用 `bili plugin remove claude` 恢复原生自动压缩。 +- `claude` 有**原生姿态**(#964):受管 settings 块 + `SessionStart` hook + MCP shell;hook 骑自管端口区(#1660),每会话把受管 URL 重钉到存活 origin,端口漂移自愈。#1821 起 MCP shell 还在会话内守护代理(30s × 3 次失败),代理死亡时自行重生,lane 内模型请求 ~90s 内恢复而无需新开会话(与 zcode 车道同语义)。`BILI_NATIVE_CLAUDE=0` 退出(passthrough)。机制:[TECHNICAL-NOTES.zh-CN.md](TECHNICAL-NOTES.zh-CN.md)。**已知限制(#2290):** Claude Desktop 应用的 Code 标签页(`CLAUDE_CODE_ENTRYPOINT=claude-desktop`)会为其内嵌的 Claude Code 设置自己的 `ANTHROPIC_BASE_URL`,覆盖受管值——桌面会话完全绕过代理,而受管块的 `DISABLE_AUTO_COMPACT=1` 仍然生效,因此既没有 bili 压缩、也没有原生自动压缩。hook 会在会话开始时大声警告并记录下来供 `bili doctor` 展示(检测到本机装有 Claude Desktop 时,该 lane 也会显示 ⚠️ 提示);终端会话不受影响。**真机验证(2026-10-06,#2290):** 把 `HTTPS_PROXY` 写进 settings 的 `env` 块后,在 Claude Desktop 2.19675.1 / 内嵌 CC 2.1.288(Windows 11)上对一台开启 MITM 的存活代理**零 CONNECT 流量**——该变量没有到达应用内任何网络栈,与该时期的 CLI 行为一致——注意新版构建(CC 2.1.301 起)在 CLI 上会遵守 `HTTPS_PROXY`,这正是 `bili claude` 走 `/bili/` base-URL 改写的原因——launcher 不再为该 lane 设置代理、也不再设置 CA,而自带代理的子进程会把 loopback 主机合并进 `NO_PROXY`/`no_proxy`,确保无论子进程的代理行为如何,这一腿都保持直连(#3446)。截至该版本,桌面 Code 标签页不存在可用的路由缝隙;该 lane 继续在 [#2290](https://github.com/ranxianglei/billion-context/issues/2290) 跟踪,若上游行为变化再议。仅用桌面的用户可用 `bili plugin remove claude` 恢复原生自动压缩。**桌面端 Threads 暴露面(#3744,内嵌 CC 2.1.299 真机报告):** 应用仅在 `CLAUDE_CODE_TETHER_LIVE`(三态环境变量)未设置、`ANTHROPIC_BASE_URL` 未设置或 host 正是 `api.anthropic.com`、provider 为 firstParty、且 `CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS` 未开启时才启用 Anthropic 服务端 threads。因此走 bili 的终端会话永远不会开 threads(base URL ≠ 官方 → 全量历史请求走经典压缩路径);而桌面端在 cert-MITM 下被宿主强制 `ANTHROPIC_BASE_URL=https://api.anthropic.com` → CC 自认为直连官方 → 开启 threads(还带 global cache scope)——这正是 threads reforge(#2648/#2701/#3569)要对付的最难一条路。设 `CLAUDE_CODE_TETHER_LIVE=0` 能关 threads,但主对话仍带 `scope:"global"`,会被非 threads 放行规则(#2650)整条放行、照样不压缩(未实测)。 - `zcode` 有**原生姿态**(#1145):受管 `~/.zcode/cli/config.json` 块 + 每会话 provider `baseURL` 改写。完整机制:[CLIENTS.zh-CN.md](CLIENTS.zh-CN.md)。 - `omp` 是自拉起原生插件;`codex` 性质不同 —— 它是 MCP shell 工具面配套:codex 的模型流量只能经 env 路由(默认 ChatGPT-登录 provider 没有可改写的配置缝 —— managed `model_providers` 块会强制 API-key 认证、废掉订阅登录),而 MCP 子进程无法向父进程注入 env,所以插件安装既不拉代理、也永远路由不了 codex 本体流量。`bili plugin install codex` 在 `~/.codex/config.toml` 写入 `[mcp_servers.bili]`(command = node,args = dist/mcp.js)注册四个 ACP 工具,会话启动时解析代理:env `BILI_MCP_PROXY` > 活实例登记(任一 lane 的代理或 `bili start` 守护)> 8787 用户区默认(#1660 去掉了安装时烘焙 origin,#403)—— 全不可达则 `tools/list` 报 -32003。结论:**先起 bili**(`bili start` 或任一客户端的 lane 代理),想要压缩再自行导出 HTTPS_PROXY;零配置全功能用 `bili codex`。机制:[CLIENTS.zh-CN.md](CLIENTS.zh-CN.md#codexopenai-codex-cli)。 - `jcode`、`aider` 无原生模式(无插件/MCP/工具注入接缝:#962、#1048)—— 用 `bili jcode` / `bili aider`。 diff --git a/src/server.ts b/src/server.ts index 3030b92888..af6bb72729 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1760,6 +1760,11 @@ export type Prepared = { * for a fresh create) before the forward. Fail-open: an unsuccessful * execution leaves `body` (the original continue) untouched. */ threadsReforge?: import("./server/threads-lane.js").ThreadsReforgePlan; + /** [#3744] set when handle.ts swapped a continue for a reforged create. + * If THAT create is rejected upstream, forward() rolls the mirror back + * to `prevMeta` (the server-side thread never changed) and arms the + * lane's retry cooldown instead of looping summarize→rebuild→400. */ + threadsReforgeSwapped?: { prevMeta: import("./server/threads-lane.js").ThreadsMirrorMeta }; }; diff --git a/src/server/handle.ts b/src/server/handle.ts index 1ec676fcd4..f790dfe1dc 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -2555,6 +2555,10 @@ async function handleRequest( ); if (outcome.ok && outcome.body !== undefined) { prepared.body = outcome.body; + // #3744: if THIS swapped create is rejected upstream, + // forward() rolls the mirror back to the pre-reforge + // view and arms the lane's cooldown. + prepared.threadsReforgeSwapped = { prevMeta: prepared.threadsReforge.meta }; // The old thread is dead: no id in it can ever be a // useful retarget again (#2701 tail tracking). resetThreadTailTracking(session); diff --git a/src/server/relay.ts b/src/server/relay.ts index f9362f9818..bd6249dd2f 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -67,6 +67,7 @@ import { buildForwardHeaders, connectionNamedHeaders, RESPONSE_ONLY_STRIP_HEADER import { bodyDumpEnabled, logDumpFailure } from "./observability.js"; import { effectiveAbsorbBlock } from "./prepare-responses.js"; import { awaitDrain, bufferToStream, dumpStreamToFile, pipeThrough, readStreamToBuffer } from "./stream-io.js"; +import { noteReforgeCreateRejected, observeThreadsAssistantReply } from "./threads-lane.js"; import { buildForwardTarget, outboundPayloadBreakdown, registerRequestAbort, beginStreamKeepalive, effectiveTokenCount, googlePathKind, imageBillingFor, imageTokenCapFor, inboundResignCredential, outboundContextEstimates, repairResponsesAssistantOrdering, resolveUpstream, stripKernelSummaries, usageGradeInputBaseline, DOOMED_PROBE_ABSOLUTE_BYTES, DOOMED_PROBE_BYTES_PER_TOKEN, type Prepared } from "../server.js"; /** Infer the wire protocol from the request path for compat-role rewrites on @@ -1559,6 +1560,11 @@ async function forwardCaptured( } } } + // #3744: a reforged create that upstream REJECTS must not loop — roll + // the mirror back to its pre-reforge view and cool the side-call down. + if (prepared?.session && prepared.threadsReforgeSwapped) { + noteReforgeCreateRejected(prepared.session.id, prepared.threadsReforgeSwapped.prevMeta, upstream.status, (level, msg) => log(level, msg)); + } // #604: relay/gateway 5xx — no usage report will arrive, so arm the // emergency shrink with a local estimate of the wire body we just sent // (see armFailureShrink for the deadlock this breaks). A generic relay @@ -1887,6 +1893,20 @@ async function forwardCaptured( responseBody = a; dumpRaw = dumpStreamToFile(b, opts.dumpSse, `${Date.now()}-${safeSessionId(prepared.session.id)}-raw.sse`); } + // #3744: threads turns hold state server-side the client never re-sends — + // mirror the model's reply from a tee'd branch of the SAME bytes the + // client receives (byte-verbatim otherwise; fail-open inside). + if (prepared !== null && prepared.threadsLane && responseBody !== null) { + const [toClient, toCapture] = responseBody.tee(); + responseBody = toClient; + observeThreadsAssistantReply( + prepared.session.id, + toCapture, + (upstream.headers.get("content-type") ?? "").includes("text/event-stream"), + clientAbort.signal, + (level, msg) => log(level, msg), + ); + } // #3129: v1internal response envelope — unwrap upstream frames so every // branch below (compress loop, strip pipes, JSON rewriter) reads a standard // GenerateContentResponse, and wrap every frame written back to the client diff --git a/src/server/threads-lane.ts b/src/server/threads-lane.ts index 0218e2557e..3be9cd9ac9 100644 --- a/src/server/threads-lane.ts +++ b/src/server/threads-lane.ts @@ -14,7 +14,10 @@ // 1. CAPTURE (always on, unified sidecar permitting): every create/continue // is mirrored into the unified store (`session_ext` keys // `threads:mirror:*`) — create replaces the mirror (it is the client's -// authoritative snapshot), continue appends its delta. Storage is +// authoritative snapshot), continue appends its delta — and every model +// reply is appended from the forwarded response stream (#3744: the +// thread holds state server-side that the client never re-sends, so +// without it the mirror is blind to half the conversation). Storage is // append-only chunks under the 4 MiB per-value cap. // 2. BUDGET: the mirror estimate (shape tokens + post-fold entries + // folded summary) against the configured window. The wire itself is @@ -105,6 +108,34 @@ const TRANSCRIPT_BYTE_CAP = 400_000; * re-uploading its transcript on every continue: the next attempt lands * after this window instead (#3569). */ const SUMMARY_RETRY_COOLDOWN_MS = 10 * 60_000; +/** #3744: headers excluded from the side-call passthrough. Everything else + * the client sent rides along verbatim — under subscription OAuth the + * upstream only accepts the summarizer request when it looks like the + * client's own traffic (full header set, original system prefix intact). */ +const SIDE_CALL_STRIP_HEADERS = new Set([ + // hop-by-hop / framing — meaningless or harmful on a fresh connection + "connection", "keep-alive", "proxy-authenticate", "proxy-authorization", + "te", "trailers", "transfer-encoding", "upgrade", + // per-target / per-body — must describe THIS call, not the client's + "host", "content-length", "accept-encoding", "expect", +]); +/** One-shot per-request ids — replaying them on a different request got the + * side-call 429'd under subscription OAuth even with full header passthrough + * (#3744, variant A → B). */ +const SIDE_CALL_ONE_SHOT_HEADERS = new Set([ + "x-client-request-id", "anthropic-dispatch-id", "x-claude-code-prompt-id", +]); +/** Non-streaming replies are buffered whole for the mirror, capped so a + * runaway body cannot turn the observer into a media store. */ +const JSON_CAPTURE_BYTE_CAP = 8_000_000; +/** A completed reply whose blocks were all un-replayable (thinking / + * redacted_thinking only, or an abort mid-thinking) still occupies an + * assistant slot in the server-side thread. The mirror records it as this + * self-describing block so every later reforged create satisfies the + * system-placement rule (a mid-conversation `system` must be followed by an + * `assistant` message or end the array) instead of 400-ing on a permanent + * ordering gap (#3744 review of #3746). */ +const EMPTY_REPLY_PLACEHOLDER = { type: "text", text: "[threads-lane] model reply carried no replayable content (thinking-only or aborted)" }; const SUMMARIZER_SYSTEM = [ "You are compressing an older slice of a coding-agent conversation so it can be replaced by your summary.", @@ -127,6 +158,8 @@ let mirrorStore: SessionStore | undefined; const warnedSessions = new Set(); /** sessionId → epoch-ms until which the summary side-call is suppressed. */ const reforgeCooldownUntil = new Map(); +/** In-flight reply captures (the observer runs async on a tee'd branch). */ +const pendingCaptures = new Set>(); function laneStore(): SessionStore { if (!mirrorStore) mirrorStore = new SessionStore({ enabled: true }); @@ -138,6 +171,13 @@ export function __resetThreadsLaneForTests(): void { mirrorStore = undefined; warnedSessions.clear(); reforgeCooldownUntil.clear(); + pendingCaptures.clear(); +} + +/** Test seam: resolve when every in-flight reply capture has persisted (or + * given up), so mirror reads are deterministic. */ +export function __awaitThreadsCapturesForTests(): Promise { + return Promise.allSettled([...pendingCaptures]).then(() => undefined); } export function isThreadsBody(parsed: unknown): boolean { @@ -264,6 +304,150 @@ export function captureThreadsTurn( return { available: true, meta, entries }; } +interface AssistantBlockAcc { + type?: string; + text?: string; + id?: string; + name?: string; + json?: string; +} + +/** Reassemble the model's reply for one threads turn from the forwarded + * response and append it to the mirror. The thread itself holds state + * server-side that the client NEVER re-sends (continues carry only deltas), + * so without this the mirror is blind to every assistant turn: budgets + * undercount, summaries lose half the conversation, and tail rings end in + * `user system user system …` shapes a fresh create rejects with a + * placement 400 (#3744). Runs on a tee'd branch of the client-bound stream; + * fail-open — any error skips the append, never breaks the turn. */ +export function observeThreadsAssistantReply( + sessionId: string, + body: ReadableStream, + sse: boolean, + signal: AbortSignal, + log: (level: string, msg: string) => void, +): void { + const capture: Promise = (async () => { + try { + const reader = body.getReader(); + const decoder = new TextDecoder(); + let buf = ""; + const accs = new Map(); + let sawEvent = false; + let jsonBytes = 0; + const processFrame = (frame: string): void => { + const datas: string[] = []; + for (const line of frame.split("\n")) { + if (line.startsWith("data:")) datas.push(line.slice(5).trimStart()); + } + if (datas.length === 0) return; + let ev: Record; + try { ev = JSON.parse(datas.join("\n")) as Record; } catch { return; } + sawEvent = true; + const type = typeof ev.type === "string" ? ev.type : ""; + const index = typeof ev.index === "number" ? ev.index : -1; + if (type === "content_block_start") { + const cb = (ev.content_block ?? {}) as Record; + accs.set(index, { + type: typeof cb.type === "string" ? cb.type : undefined, + ...(typeof cb.text === "string" ? { text: cb.text } : {}), + ...(typeof cb.id === "string" ? { id: cb.id } : {}), + ...(typeof cb.name === "string" ? { name: cb.name } : {}), + }); + } else if (type === "content_block_delta" && index >= 0) { + const acc = accs.get(index); + if (!acc) return; + const delta = (ev.delta ?? {}) as Record; + if (delta.type === "text_delta" && typeof delta.text === "string") { + acc.text = (acc.text ?? "") + delta.text; + } else if (delta.type === "input_json_delta" && typeof delta.partial_json === "string") { + acc.json = (acc.json ?? "") + delta.partial_json; + } + } + }; + try { + while (!signal.aborted) { + const { done, value } = await reader.read(); + if (done) break; + const chunk = decoder.decode(value, { stream: true }); + if (sse) { + buf += chunk.replace(/\r\n/g, "\n"); + let sep: number; + while ((sep = buf.indexOf("\n\n")) !== -1) { + processFrame(buf.slice(0, sep)); + buf = buf.slice(sep + 2); + } + if (buf.length > 1_000_000) buf = ""; // undelimited runaway frame — drop the fragment + } else { + jsonBytes += Buffer.byteLength(chunk, "utf8"); + if (jsonBytes <= JSON_CAPTURE_BYTE_CAP) buf += chunk; + } + } + } finally { + try { await reader.cancel(); } catch { /* already closed/released */ } + } + buf += decoder.decode(); // flush a trailing multi-byte character split across the final chunk + let content: unknown[] | null; + if (sse) { + content = []; + for (const acc of accs.values()) { + // thinking/redacted_thinking are signature-verified scratch + // (#1960 family) and other block types are not replay-safe — + // only text/tool_use enter the mirror. + if (acc.type === "text") { + content.push({ type: "text", text: acc.text ?? "" }); + } else if (acc.type === "tool_use") { + let input: unknown = {}; + if (acc.json) { try { input = JSON.parse(acc.json); } catch { input = {}; } } + content.push({ type: "tool_use", id: acc.id ?? "", name: acc.name ?? "", input }); + } + } + // sawEvent distinguishes "the reply existed but nothing + // replayable survived the filter" (placeholder — see + // EMPTY_REPLY_PLACEHOLDER) from "we observed nothing at all" + // (honest skip: an abort before the first event proves no + // reply bytes reached us). + if (!sawEvent) { + content = null; + } else if (content.length === 0) { + content = [EMPTY_REPLY_PLACEHOLDER]; + } + } else { + try { + const json = JSON.parse(buf) as { content?: unknown }; + if (!Array.isArray(json.content)) { + content = null; // not a well-formed reply body — record nothing + } else { + content = json.content.filter((b) => { + if (typeof b !== "object" || b === null) return false; + const t = (b as { type?: unknown }).type; + return t === "text" || t === "tool_use"; + }) as unknown[]; + if (content.length === 0) content = [EMPTY_REPLY_PLACEHOLDER]; + } + } catch { + content = null; + } + } + if (content === null) return; + const entry: ThreadsMirrorEntry = { role: "assistant", content }; + const sized = serializeEntry(entry); + const pushEntry: ThreadsMirrorEntry = sized.ok + ? sized.value + : { role: "assistant", content: `[threads-lane] oversized assistant reply elided from the mirror (${sized.bytes} bytes)` }; + const store = laneStore(); + const mirror = loadMirror(store, sessionId); + if (!mirror.available) return; + const entries = [...mirror.entries, pushEntry]; + persistMirror(store, sessionId, { ...mirror.meta, count: entries.length }, entries, log); + } catch { + // fail-open: mirroring must never break the turn (#2926 posture) + } + })(); + pendingCaptures.add(capture); + void capture.finally(() => pendingCaptures.delete(capture)); +} + /** Mirror estimate: shape + folded summary + post-fold entries. The wire * body is useless here (a continue is always small) — this is the lane's * only budget truth. */ @@ -336,14 +520,38 @@ function renderTranscript(entries: ThreadsMirrorEntry[]): string { return parts.join("\n\n"); } +/** The captured client system when usable as-is (non-empty string or block + * array) — otherwise undefined and the caller falls back to the hand-built + * billing/summarizer system. */ +function capturedSystemVerbatim(system: unknown): unknown { + if (typeof system === "string") return system.trim() !== "" ? system : undefined; + if (Array.isArray(system)) return system.length > 0 ? system : undefined; + return undefined; +} + function buildSummaryRequestBody(shape: ThreadsMirrorShape | undefined, model: string | undefined, meta: ThreadsMirrorMeta, entries: ThreadsMirrorEntry[], cut: number, billingBlock: { type: "text"; text: string } | undefined): Record { + const transcript = renderTranscript(entries.slice(Math.min(meta.foldedBefore, cut), cut)); + const verbatim = capturedSystemVerbatim(shape?.system); + if (verbatim !== undefined) { + // #3744 variant B (proven against subscription OAuth): the client's + // original system rides untouched — billing attribution, host identity + // and cache-control anchors all intact — and the summarizer + // instructions join the USER message instead of the system slot. + return { + model: model ?? shape?.model ?? "claude-sonnet-4-5", + max_tokens: SUMMARY_MAX_TOKENS, + stream: false, + system: verbatim, + messages: [{ role: "user", content: `${transcript}\n\n---\n\n${SUMMARIZER_SYSTEM}` }], + }; + } const system: unknown[] = billingBlock !== undefined ? [billingBlock, { type: "text", text: SUMMARIZER_SYSTEM }] : [{ type: "text", text: SUMMARIZER_SYSTEM }]; return { model: model ?? shape?.model ?? "claude-sonnet-4-5", max_tokens: SUMMARY_MAX_TOKENS, stream: false, system, - messages: [{ role: "user", content: renderTranscript(entries.slice(Math.min(meta.foldedBefore, cut), cut)) }], + messages: [{ role: "user", content: transcript }], }; } @@ -383,6 +591,27 @@ function armReforgeCooldown(sessionId: string): void { for (const [id, until] of reforgeCooldownUntil) if (id !== sessionId && until < staleBefore) reforgeCooldownUntil.delete(id); } +/** #3744: the swapped create reached upstream and was REJECTED — roll the + * mirror back to its pre-reforge view (the server-side thread never changed) + * and arm the same cooldown the side-call failures use, so the next continue + * does not immediately re-summarize + re-rebuild into the same wall. */ +export function noteReforgeCreateRejected( + sessionId: string, + prevMeta: ThreadsMirrorMeta, + status: number, + log: (level: string, msg: string) => void, +): void { + try { + const store = laneStore(); + const mirror = loadMirror(store, sessionId); + if (mirror.available) persistMirror(store, sessionId, { ...prevMeta }, mirror.entries, log); + } catch { + // fail-open: the cooldown below still breaks the loop + } + armReforgeCooldown(sessionId); + log("warn", `[${sessionId}] [threads] reforged create rejected by upstream (HTTP ${status}) — mirror rolled back to pre-reforge view, summary side-call cooling down (#3744)`); +} + /** Execute an armed reforge: one summarizer side-call (client's auth, aimed * at the SAME endpoint + proxy decision as the main forward — #3569), then * swap the continue for a fresh create. Fail-open — any error returns @@ -410,15 +639,17 @@ export async function executeThreadsReforge( } try { const summaryReq = buildSummaryRequestBody(meta.shape, typeof parsed.model === "string" ? parsed.model : undefined, meta, entries, cut, billingBlock); - const headers: Record = { "content-type": "application/json" }; - const passThrough = (name: string): void => { - const v = req.headers[name]; - if (typeof v === "string") headers[name] = v; - }; - passThrough("x-api-key"); - passThrough("authorization"); - passThrough("anthropic-version"); - passThrough("anthropic-beta"); + // #3744 variant B: the side-call must look like the client's own + // traffic — pass EVERY client header through except hop-by-hop / + // per-body framing and the one-shot per-request ids; bili's own + // x-bili-* markers never leave the proxy. + const headers: Record = {}; + for (const [name, value] of Object.entries(req.headers)) { + if (SIDE_CALL_STRIP_HEADERS.has(name) || SIDE_CALL_ONE_SHOT_HEADERS.has(name) || name.startsWith("x-bili-")) continue; + if (typeof value === "string") headers[name] = value; + else if (Array.isArray(value)) headers[name] = value.join(", "); + } + headers["content-type"] = "application/json"; // Same outbound-fetch contract as every other bili call (#551 idle // watchdog, direct-dispatcher fallback, manual redirects); the proxy // decision rides the dispatcher exactly like the main forward does. diff --git a/tests/threads-lane.test.ts b/tests/threads-lane.test.ts index 83b0aba3aa..19870253ac 100644 --- a/tests/threads-lane.test.ts +++ b/tests/threads-lane.test.ts @@ -2,10 +2,16 @@ // // Threads state lives server-side; a `continue` wire body is delta-only no // matter how big the thread grew. The lane mirrors every turn into the -// unified store (session_ext threads:mirror:*) and, when the mirror crosses -// the window on a continue (rollout anthropicThreadsReforge on), swaps that -// continue for a fresh `create` carrying [summary, tail ring, delta] — the -// only channel that can deliver a fold to a server-side thread. +// unified store (session_ext threads:mirror:*) — INCLUDING the model's reply, +// reassembled from the forwarded response stream (#3744) — and, when the +// mirror crosses the window on a continue (rollout anthropicThreadsReforge +// on), swaps that continue for a fresh `create` carrying [summary, tail ring, +// delta] — the only channel that can deliver a fold to a server-side thread. +// #3744 additions: the summary side-call ships the client's own header set + +// original system (variant B), the fake upstream enforces the real +// system-placement rule on creates (so a missing-assistant regression 400s +// exactly like production), and a rejected reforged create rolls the mirror +// back and cools the lane down instead of looping. import test from "node:test"; import assert from "node:assert/strict"; @@ -21,7 +27,8 @@ import { SessionStore, _setStoreForTest } from "../src/persist.js"; import { _resetSessionsForTest } from "../src/session.js"; import { _resetPluginStateForTest } from "../src/plugin.js"; import { resetToolRingForTest } from "../src/tool-ring.js"; -import { __resetThreadsLaneForTests, type ThreadsMirrorMeta } from "../src/server/threads-lane.js"; +import { __awaitThreadsCapturesForTests, __resetThreadsLaneForTests, type ThreadsMirrorMeta } from "../src/server/threads-lane.js"; +import { firstAnthropicThreadsPlacementViolation } from "./wire-contract-fakes.ts"; const testRoot = mkdtempSync(path.join(os.tmpdir(), "bili-threads-lane-")); @@ -33,9 +40,14 @@ const blockStop = (index: number) => ev("content_block_stop", { type: "content_b const messageDelta = () => ev("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { input_tokens: 900, output_tokens: 3 } }); const messageStop = ev("message_stop", { type: "message_stop" }); -const sseReply = (res: http.ServerResponse, id: string, text: string): void => { +const toolBlockStart = (index: number) => ev("content_block_start", { type: "content_block_start", index, content_block: { type: "tool_use", id: `toolu_${index}`, name: "Bash", input: {} } }); +const toolJsonDelta = (index: number, partial: string) => ev("content_block_delta", { type: "content_block_delta", index, delta: { type: "input_json_delta", partial_json: partial } }); + +const sseReply = (res: http.ServerResponse, id: string, text: string, withToolUse: boolean): void => { res.writeHead(200, { "content-type": "text/event-stream" }); - res.end([messageStart(id), blockStart(0), blockDelta(0, text), blockStop(0), messageDelta(), messageStop].join("")); + const frames = [messageStart(id), blockStart(0), blockDelta(0, text.slice(0, Math.ceil(text.length / 2))), blockDelta(0, text.slice(Math.ceil(text.length / 2))), blockStop(0)]; + if (withToolUse) frames.push(toolBlockStart(1), toolJsonDelta(1, '{"command":'), toolJsonDelta(1, '"ls"}'), blockStop(1)); + res.end([...frames, messageDelta(), messageStop].join("")); }; interface HarnessOpts { @@ -44,14 +56,29 @@ interface HarnessOpts { failFirstSideCall?: boolean; /** Status the failed first side-call answers with (default 500). */ sideCallFailStatus?: number; + /** Every thread create after the first is rejected (reforged-create + * failure lane, #3744). */ + failReforgedCreate?: boolean; + /** Streamed replies carry a text block AND a tool_use block (input JSON + * split across two deltas), exercising the SSE reassembly (#3744). */ + streamToolUse?: boolean; + /** Streamed replies carry ONLY a thinking block (nothing replayable) — + * the placeholder-capture lane (#3744 review). */ + streamThinkingOnly?: boolean; + /** Non-stream threads replies answer with this content array. */ + nonStreamContent?: unknown[]; } interface Harness { proxyUrl: string; - post: (body: Record, sid?: string) => Promise<{ status: number; body: unknown }>; + post: (body: Record, sid?: string, headers?: Record) => Promise<{ status: number; body: unknown }>; forwarded: Record[]; forwardedPaths: string[]; - readMirror: (sid: string) => { meta?: ThreadsMirrorMeta; error?: string }; + /** Headers the fake saw on each summary side-call (variant-B check). */ + sideCallHeaders: Record[]; + readMirror: (sid: string) => { meta?: ThreadsMirrorMeta; entries?: Record[]; error?: string }; + /** Drain in-flight reply captures so mirror reads are deterministic. */ + settle: () => Promise; close: () => Promise; } @@ -67,7 +94,9 @@ async function harness(storageMode: "full" | "legacy", opts: HarnessOpts = {}): _setStoreForTest(store); const forwarded: Record[] = []; const forwardedPaths: string[] = []; + const sideCallHeaders: Record[] = []; let sideCalls = 0; + let creates = 0; const upstream = http.createServer((req, res) => { const urlPath = (req.url ?? "/").split("?")[0]; const chunks: Buffer[] = []; @@ -85,21 +114,55 @@ async function harness(storageMode: "full" | "legacy", opts: HarnessOpts = {}): } const body = JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record; forwarded.push(body); + const threadType = (body.thread as { type?: string } | undefined)?.type; const stream = body.stream === true; const isSideCall = !stream && body.thread === undefined; - if (isSideCall) sideCalls++; + if (isSideCall) { + sideCalls++; + sideCallHeaders.push({ ...req.headers } as Record); + } if (isSideCall && opts.failFirstSideCall && sideCalls === 1) { const status = opts.sideCallFailStatus ?? 500; res.writeHead(status, { "content-type": "application/json" }); res.end(JSON.stringify({ type: "error", error: { type: status === 404 ? "not_found_error" : "api_error", message: "boom" } })); return; } + if (threadType === "create") creates++; + // #3744: enforce the real system-placement rule on full-history + // creates — the assistant-blind mirror's `user system user system` + // rebuild must 400 here exactly like it did in production. + if (threadType === "create") { + const violation = firstAnthropicThreadsPlacementViolation(Array.isArray(body.messages) ? body.messages : []); + if (violation >= 0) { + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ type: "error", error: { type: "invalid_request_error", message: `messages.${violation}: role 'system' must precede an 'assistant' message or end the array` } })); + return; + } + } + if (opts.failReforgedCreate && threadType === "create" && creates > 1) { + res.writeHead(400, { "content-type": "application/json" }); + res.end(JSON.stringify({ type: "error", error: { type: "invalid_request_error", message: "simulated reforged-create rejection" } })); + return; + } if (stream) { - sseReply(res, `msg_${forwarded.length}`, "answer " + forwarded.length); + if (opts.streamThinkingOnly === true) { + res.writeHead(200, { "content-type": "text/event-stream" }); + const id = `msg_${forwarded.length}`; + const frames = [ + messageStart(id), + ev("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "thinking", thinking: "" } }), + ev("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "thinking_delta", thinking: "hmm" } }), + blockStop(0), + ]; + res.end([...frames, messageDelta(), messageStop].join("")); + return; + } + sseReply(res, `msg_${forwarded.length}`, "answer " + forwarded.length, opts.streamToolUse === true); return; } + const isThreadsReply = threadType !== undefined; res.writeHead(200, { "content-type": "application/json" }); - res.end(JSON.stringify({ id: `msg_${forwarded.length}`, role: "assistant", content: [{ type: "text", text: "SUMMARY-" + sideCalls }], usage: { input_tokens: 100, output_tokens: 10 } })); + res.end(JSON.stringify({ id: `msg_${forwarded.length}`, role: "assistant", content: isThreadsReply ? opts.nonStreamContent ?? [{ type: "text", text: "THREAD-REPLY" }] : [{ type: "text", text: "SUMMARY-" + sideCalls }], usage: { input_tokens: 100, output_tokens: 10 } })); }); }); upstream.listen(0, "127.0.0.1"); @@ -109,8 +172,12 @@ async function harness(storageMode: "full" | "legacy", opts: HarnessOpts = {}): const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstreamUrl, routes: { [upstreamUrl]: { models: { "claude-test": { context: window } } } }, modelContextLimit: window, kernelConfig: defaultConfig(window), compress: { injectTool: true, injectNudge: false }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] } } as ProxyOptions); if (!(proxy as unknown as { listening?: boolean }).listening) await once(proxy, "listening"); const proxyUrl = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; - const post = async (body: Record, sid = "threads-1") => { - const response = await fetch(`${proxyUrl}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", "x-api-key": "test-key", "anthropic-version": "2023-06-01", "x-acp-session": sid }, body: JSON.stringify(body) }); + // Drain the previous turn's reply capture BEFORE sending the next one — + // real clients are sequential per conversation, and the drain makes the + // mirror state deterministic at every assert point (#3744). + const post = async (body: Record, sid = "threads-1", extraHeaders: Record = {}) => { + await __awaitThreadsCapturesForTests(); + const response = await fetch(`${proxyUrl}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", "x-api-key": "test-key", "anthropic-version": "2023-06-01", "x-acp-session": sid, ...extraHeaders }, body: JSON.stringify(body) }); const text = await response.text(); let parsed: unknown = text; try { @@ -120,10 +187,16 @@ async function harness(storageMode: "full" | "legacy", opts: HarnessOpts = {}): } return { status: response.status, body: parsed }; }; - const readMirror = (sid: string): { meta?: ThreadsMirrorMeta; error?: string } => { + const readMirror = (sid: string): { meta?: ThreadsMirrorMeta; entries?: Record[]; error?: string } => { try { const meta = store.getSessionExt(sid, "threads:mirror:meta"); - return { meta: meta as ThreadsMirrorMeta | undefined }; + const entries: Record[] = []; + for (let i = 0; ; i++) { + const raw = store.getSessionExt(sid, `threads:mirror:chunk:${String(i).padStart(4, "0")}`) as { entries?: Record[] } | null | undefined; + if (raw === undefined || raw === null || !Array.isArray(raw.entries)) break; + entries.push(...raw.entries); + } + return { meta: meta as ThreadsMirrorMeta | undefined, entries }; } catch (err) { return { error: String(err) }; } @@ -133,7 +206,9 @@ async function harness(storageMode: "full" | "legacy", opts: HarnessOpts = {}): post, forwarded, forwardedPaths, + sideCallHeaders, readMirror, + settle: () => __awaitThreadsCapturesForTests(), close: async () => { await new Promise((resolve) => proxy.close(() => resolve())); store.closeQuietly(); @@ -185,11 +260,14 @@ test("threads lane: create and continue pass through byte-faithfully while the m assert.equal(r2.status, 200); assert.equal((h.forwarded[1] as { thread?: { type?: string } }).thread?.type, "continue"); assert.equal((h.forwarded[1] as { messages?: unknown[] }).messages?.length, 1, "continue delta forwarded as-is"); + await h.settle(); const mirror = h.readMirror("threads-1"); assert.ok(mirror.meta, "mirror meta persisted"); - assert.equal(mirror.meta?.count, 3, "create(2) + continue delta(1)"); + assert.equal(mirror.meta?.count, 5, "create(2) + reply(1) + continue delta(1) + reply(1) — replies mirrored from the response stream (#3744)"); assert.deepEqual(mirror.meta?.shape?.system, CLIENT_SYSTEM, "create shape captured"); assert.deepEqual(mirror.meta?.shape?.tools, CLIENT_TOOLS); + const roles = (mirror.entries ?? []).map((e) => e.role); + assert.deepEqual(roles, ["user", "assistant", "assistant", "user", "assistant"], "captured replies interleave the client turns"); } finally { await h.close(); } @@ -413,16 +491,186 @@ test("threads lane: a re-create under the same session id resets bili's own fold { role: "user", content: turnText(3, 400) }, { role: "assistant", content: "a3" }, ])); await h.post(continueBody([{ role: "user", content: turnText(4, 400) }], "msg_1")); + await h.settle(); const afterReforge = h.readMirror("threads-1"); assert.ok((afterReforge.meta?.foldedBefore ?? 0) > 0, "the over-window continue was reforged (fold point recorded)"); await h.post(createBody([ { role: "user", content: turnText(1) }, { role: "assistant", content: "b1" }, { role: "user", content: turnText(2) }, { role: "assistant", content: "b2" }, ])); + await h.settle(); const afterRecreate = h.readMirror("threads-1"); assert.equal(afterRecreate.meta?.foldedBefore, 0, "re-create resets the fold point to zero"); assert.equal(afterRecreate.meta?.foldedSummary, undefined, "re-create drops the stale summary"); - assert.equal(afterRecreate.meta?.count, 4, "mirror holds only the fresh snapshot"); + assert.equal(afterRecreate.meta?.count, 5, "mirror holds only the fresh snapshot (+ its captured reply)"); + } finally { + delete process.env.BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE; + await h.close(); + } +}); + +test("threads lane: streamed replies are mirrored — text + tool_use reassembled from SSE (#3744)", async () => { + const h = await harness("full", { streamToolUse: true }); + try { + await h.post(createBody([{ role: "user", content: "run the tests" }])); + await h.settle(); + const mirror = h.readMirror("threads-1"); + assert.equal(mirror.meta?.count, 2, "client turn + captured reply"); + const reply = (mirror.entries ?? [])[1] as { role: string; content: { type: string; text?: string; name?: string; input?: unknown }[] }; + assert.equal(reply.role, "assistant"); + assert.equal(reply.content[0]?.type, "text"); + assert.equal(reply.content[0]?.text, "answer 1", "text reassembled across split deltas"); + assert.equal(reply.content[1]?.type, "tool_use"); + assert.equal(reply.content[1]?.name, "Bash"); + assert.deepEqual(reply.content[1]?.input, { command: "ls" }, "tool input reassembled from input_json_delta fragments"); + } finally { + await h.close(); + } +}); + +test("threads lane: non-stream replies are mirrored, thinking blocks filtered (#3744/#1960)", async () => { + const h = await harness("full", { nonStreamContent: [ + { type: "thinking", thinking: "scratch" }, + { type: "text", text: "done" }, + { type: "tool_use", id: "toolu_9", name: "Bash", input: { command: "pwd" } }, + ] }); + try { + const r = await h.post({ ...createBody([{ role: "user", content: "hi" }]), stream: false }); + assert.equal(r.status, 200); + await h.settle(); + const mirror = h.readMirror("threads-1"); + const reply = (mirror.entries ?? [])[1] as { role: string; content: { type: string }[] }; + assert.equal(reply.role, "assistant"); + assert.deepEqual(reply.content.map((b) => b.type), ["text", "tool_use"], "signature-verified thinking never enters the mirror"); + } finally { + await h.close(); + } +}); + +test("threads lane: a thinking-only non-stream reply mirrors a placeholder assistant slot (#3744 review)", async () => { + const h = await harness("full", { nonStreamContent: [ + { type: "thinking", thinking: "only scratch" }, + { type: "redacted_thinking", data: "xxx" }, + ] }); + try { + await h.post({ ...createBody([{ role: "user", content: "hi" }]), stream: false }); + await h.settle(); + const mirror = h.readMirror("threads-1"); + const reply = (mirror.entries ?? [])[1] as { role: string; content: { type: string; text?: string }[] }; + assert.equal(reply.role, "assistant", "the turn still occupies an assistant slot in the mirror — without it every later reforged create 400s on the placement rule"); + assert.equal(reply.content[0]?.type, "text"); + assert.match(String(reply.content[0]?.text ?? ""), /\[threads-lane\] model reply carried no replayable content/, "self-describing placeholder, not a silent gap"); + } finally { + await h.close(); + } +}); + +test("threads lane: a thinking-only streamed reply mirrors a placeholder assistant slot (#3744 review)", async () => { + const h = await harness("full", { streamThinkingOnly: true }); + try { + await h.post(createBody([{ role: "user", content: "hi" }])); + await h.settle(); + const mirror = h.readMirror("threads-1"); + const reply = (mirror.entries ?? [])[1] as { role: string; content: { type: string; text?: string }[] }; + assert.equal(reply.role, "assistant", "SSE path: sawEvent but nothing replayable → placeholder, not a permanent ordering gap"); + assert.match(String(reply.content[0]?.text ?? ""), /\[threads-lane\] model reply carried no replayable content/); + } finally { + await h.close(); + } +}); + +test("threads lane: summary side-call ships the client's own headers + original system (variant B, #3744)", async () => { + process.env.BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE = "1"; + const h = await harness("full", { window: 5000 }); + const clientHdrs = { "anthropic-beta": "context-management-2025-06-27", "user-agent": "claude-desktop/2.1.299", "x-claude-code-prompt-id": "prompt-abc", "anthropic-dispatch-id": "disp-xyz" }; + try { + await h.post(createBody([ + { role: "user", content: turnText(1, 400) }, { role: "assistant", content: "a1" }, + { role: "user", content: turnText(2, 400) }, { role: "assistant", content: "a2" }, + { role: "user", content: turnText(3, 400) }, { role: "assistant", content: "a3" }, + ]), "threads-1", clientHdrs); + const r = await h.post(continueBody([{ role: "user", content: turnText(4, 400) }], "msg_1"), "threads-1", { ...clientHdrs, "x-client-request-id": "req-123" }); + assert.equal(r.status, 200); + const sideCall = h.forwarded[1] as { system?: unknown; messages?: { role?: string; content?: unknown }[] }; + assert.deepEqual(sideCall.system, CLIENT_SYSTEM, "the client's original system rides the side-call verbatim (attribution + identity intact)"); + assert.equal(sideCall.messages?.[0]?.role, "user"); + const userText = typeof sideCall.messages?.[0]?.content === "string" ? sideCall.messages[0].content : ""; + assert.match(userText, /compressing an older slice/, "summarizer instructions moved into the user message"); + assert.ok(!JSON.stringify(sideCall.system).includes("compressing an older slice"), "summarizer no longer occupies the system slot"); + const hdrs = h.sideCallHeaders.at(-1); + assert.ok(hdrs, "side-call observed by the upstream"); + assert.equal(hdrs?.["x-api-key"], "test-key", "client auth passes through"); + assert.equal(hdrs?.["anthropic-beta"], "context-management-2025-06-27", "client beta header passes through"); + assert.equal(hdrs?.["user-agent"], "claude-desktop/2.1.299", "client UA passes through"); + assert.equal(hdrs?.["x-claude-code-prompt-id"], undefined, "one-shot prompt id stripped"); + assert.equal(hdrs?.["anthropic-dispatch-id"], undefined, "one-shot dispatch id stripped"); + assert.equal(hdrs?.["x-client-request-id"], undefined, "one-shot request id stripped"); + } finally { + delete process.env.BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE; + await h.close(); + } +}); + +test("threads lane: CC-style mid-conversation systems no longer break the reforged create (placement 400, #3744)", async () => { + process.env.BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE = "1"; + const h = await harness("full", { window: 5000 }); + try { + // Pin the validator both directions (#3617 gate discipline): the old + // assistant-blind rebuild violates the rule; the captured-reply shape + // satisfies it. +assert.ok(firstAnthropicThreadsPlacementViolation([{ role: "user" }, { role: "user" }, { role: "system" }, { role: "user" }, { role: "system" }]) >= 0, "assistant-blind shape is a placement violation"); + assert.equal(firstAnthropicThreadsPlacementViolation([{ role: "user" }, { role: "system" }, { role: "assistant" }, { role: "user" }, { role: "system" }]), -1, "captured-reply shape is legal"); + // CC 2.1.299 style: each continue delta carries a mid-conversation + // `system`; the assistant reply lives server-side until captured. + await h.post(createBody([ + { role: "user", content: turnText(1, 350) }, { role: "assistant", content: "a1" }, + { role: "user", content: turnText(2, 350) }, { role: "assistant", content: "a2" }, + ])); + await h.post(continueBody([{ role: "user", content: turnText(3, 350) }, { role: "system", content: "ctx-3" }], "msg_1")); + // This continue crosses the window → reforge. Pre-fix, the rebuilt + // create was `user system user system …` and 400'd here. + const r = await h.post(continueBody([{ role: "user", content: turnText(4, 350) }, { role: "system", content: "ctx-4" }], "msg_1")); + assert.equal(r.status, 200, "reforged create accepted — every system is followed by a captured assistant reply"); + const reforged = h.forwarded.at(-1) as { thread?: { type?: string }; messages?: { role?: string }[] }; + assert.equal(reforged.thread?.type, "create", "the continue was swapped for a fresh create"); + const msgs = reforged.messages ?? []; + assert.ok(msgs.some((m) => m.role === "assistant"), "captured assistant replies ride the rebuilt create"); + assert.equal(firstAnthropicThreadsPlacementViolation(msgs), -1, "rebuilt array satisfies the placement rule"); + } finally { + delete process.env.BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE; + await h.close(); + } +}); + +test("threads lane: a rejected reforged create rolls the mirror back and cools down (#3744)", async () => { + process.env.BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE = "1"; + const h = await harness("full", { window: 5000, failReforgedCreate: true }); + try { + await h.post(createBody([ + { role: "user", content: turnText(1, 350) }, { role: "assistant", content: "a1" }, + { role: "user", content: turnText(2, 350) }, { role: "assistant", content: "a2" }, + ])); + await h.post(continueBody([{ role: "user", content: turnText(3, 350) }], "msg_1")); + await h.settle(); + const preReforge = h.readMirror("threads-1"); + assert.equal(preReforge.meta?.foldedBefore, 0, "nothing folded yet"); + const countBefore = preReforge.meta?.count ?? 0; + // This continue crosses the window; the swapped create is rejected. + const r = await h.post(continueBody([{ role: "user", content: turnText(4, 350) }], "msg_1")); + assert.equal(r.status, 400, "the rejection passes through to the client"); + await h.settle(); + const after = h.readMirror("threads-1"); + assert.equal(after.meta?.foldedBefore, 0, "mirror rolled back — the server-side thread never changed"); + assert.equal(after.meta?.foldedSummary, undefined, "stale summary dropped"); + assert.equal(after.meta?.count, countBefore + 1, "the client's delta stays mirrored; only the fold point rolled back"); + // Cooldown holds: the next over-window continue forwards plain — no + // doomed summarize→rebuild→400 round. + const before = h.forwarded.length; + const r2 = await h.post(continueBody([{ role: "user", content: turnText(5, 350) }], "msg_1")); + assert.equal(r2.status, 200); + assert.equal(h.forwarded.length, before + 1, "cooldown active: no second side-call within the window"); + const next = h.forwarded.at(-1) as { thread?: { type?: string } }; + assert.equal(next.thread?.type, "continue", "fail-open still holds while cooling down"); } finally { delete process.env.BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE; await h.close(); diff --git a/tests/wire-contract-fakes.ts b/tests/wire-contract-fakes.ts index c2aba2eda3..84755aa76b 100644 --- a/tests/wire-contract-fakes.ts +++ b/tests/wire-contract-fakes.ts @@ -29,6 +29,15 @@ interface WireRule { } export const WIRE_RULES: readonly WireRule[] = [ + { + id: "WC-022", + wire: "anthropic", + lane: "anthropic-threads", + summary: + "on the Anthropic Threads API a full-history `create` rejects with 400 'messages.N: role \"system\" must precede an \"assistant\" message or end the array' when any `system` message is NOT followed later by an `assistant` message and does not end the array — CC 2.1.299 sends a mid-conversation `system` each turn and model replies live server-side in the Thread, so bili's reforge rebuild must include the captured assistant replies between client turns (an assistant-blind mirror rebuilds `user system user system …`, which is exactly the rejected shape)", + provenance: + "bili #3744 production 400 (Claude Desktop embedded CC 2.1.299, subscription OAuth, bili v0.1.201, opt-in BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE=1): the reforged create 400'd with 'messages.2: role \"system\" must precede an \"assistant\" message or end the array'; the reporter's mirror dump showed user 184 / system 184 / assistant 182 with tail `usausausa…usausus`. Lane-scoped: enforcement lives in the lane's own fake upstream (tests/threads-lane.test.ts, which 400s violating creates using firstAnthropicThreadsPlacementViolation exported below, pinned both directions), not the shared anthropic VALIDATOR — the classic /v1/messages lane never builds these arrays.", + }, { id: "WC-017", wire: "responses", @@ -742,6 +751,26 @@ export function vertexErrorEnvelope(streaming: boolean, message: string): { stat return { status: 400, body: streaming ? JSON.stringify([err]) : JSON.stringify(err) }; } +/** WC-022, lane-scoped (anthropic-threads): index of the first placement violation — a `system` + * message is only legal when some LATER message has role `assistant`, or it ends the array. + * Returns -1 when the array satisfies the rule. Shared by the lane's fake upstream + * (tests/threads-lane.test.ts) and the ledger gate below. */ +export function firstAnthropicThreadsPlacementViolation(messages: unknown[]): number { + for (let i = 0; i < messages.length; i++) { + if ((messages[i] as { role?: unknown } | null)?.role !== "system") continue; + const followedByAssistant = messages.slice(i + 1).some((m) => (m as { role?: unknown } | null)?.role === "assistant"); + if (!followedByAssistant && i !== messages.length - 1) return i; + } + return -1; +} + +/** WC-022: validate a threads `create` message array against the production placement rule. */ +export function validateAnthropicThreadsCreate(messages: unknown): string[] { + if (!Array.isArray(messages)) return []; + const at = firstAnthropicThreadsPlacementViolation(messages); + return at >= 0 ? [`WC-022 messages.${at}: role 'system' must precede an 'assistant' message or end the array`] : []; +} + const PATH_MATCHERS: Record boolean> = { anthropic: (u) => u.startsWith("/v1/messages"), "openai-chat": (u) => u.startsWith("/v1/chat/completions"), diff --git a/tests/wire-contract.test.ts b/tests/wire-contract.test.ts index 244468554e..e2f6b1497e 100644 --- a/tests/wire-contract.test.ts +++ b/tests/wire-contract.test.ts @@ -53,7 +53,7 @@ import { RULE_TOOL_GOOGLE, retrieveToolsFor, } from "../src/compress-tool.ts"; -import { WIRE_RULES, WIRE_HEADER_RULES, VALIDATORS, startFakeUpstream, validateResponsesWsCreate, validateCodexResponsesWsCreate, validateGoSessionHeaders, validateVertexAnthropicBody, vertexAnthropicPathInfo, vertexErrorEnvelope, type Wire } from "./wire-contract-fakes.ts"; +import { WIRE_RULES, WIRE_HEADER_RULES, VALIDATORS, startFakeUpstream, validateResponsesWsCreate, validateCodexResponsesWsCreate, validateGoSessionHeaders, validateVertexAnthropicBody, vertexAnthropicPathInfo, vertexErrorEnvelope, validateAnthropicThreadsCreate, type Wire } from "./wire-contract-fakes.ts"; type ToolShape = Record; @@ -246,6 +246,11 @@ test("wire-contract ledger: every lane-scoped rule has a live enforcement clause Array.isArray(JSON.parse(vertexErrorEnvelope(streaming === true, "x").body)) ? ["WC-021 stream errors are array-wrapped"] : [], body: true, }, + // WC-022 is the anthropic-threads lane (#3744): a `system` message is only legal when some LATER + // message is `assistant`, or it ends the array — so the probe array must be a NON-terminal + // system with no assistant behind it (a trailing system would be legal and trip nothing). + // End-to-end enforcement lives in tests/threads-lane.test.ts, which 400s violating creates. + { ruleId: "WC-022", validate: validateAnthropicThreadsCreate, body: [{ role: "user" }, { role: "system" }, { role: "user" }] }, ]; for (const { ruleId, validate, body } of laneProbes) { assert.ok(validate(body).some(v => v.startsWith(ruleId)), `lane-scoped rule ${ruleId} has no live enforcement clause in its lane fake`);