diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fb2a85334..d0e66f8e17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,9 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ### Fixed - **dsh 桌面副本自己更新自己时也跑 boot smoke(#2082 后续)**:#2091 的 boot smoke 门只接在 `refreshDshDesktopCopy` 上(别的 bili 进程替桌面副本刷新的那条路)。代理本身就从桌面副本运行时,`hostManagedInstall` 按 #1575 放行,走的是普通自更新路径——周期检查 / `bili update`(`checkForUpdate`)和 advisory 强制安装 / 面板安装(`forceInstallVersion`)——这两处调 `installViaTarball` 没带 `bootSmoke`,入口能解析但加载即抛错的包只过 `node --check` 就被换上去,正是这道门要挡的状态。现在两条路径在安装目录是 dsh 桌面副本时同样启用 boot smoke,失败回滚到旧副本;其他安装布局行为不变。 - **A compress call that folded nothing no longer rewrites every `` tag on the next request (follow-up to #5608-C/D, #5797).** `applyRanges` armed the deferred one-time ref/ledger alignment (`session.pendingLedgerAlign`) right after committing the kernel's returned state — before the `blocksCreated === 0` returns — so a refused compress (range too small, cannot anchor, already compressed) or a no-op armed it too. The next save then ran `restampRefsToLedger`, which raised `timestampsHighWaterRefSeq` to the highest ref unconditionally: on a session whose refs already sit on their ledger seats (`kept=N restamped=0 rehomed=0`) no number moved, yet every tag minted so far dropped below the mark and lost its `time=` attribute — the whole cached prefix re-billed after a call whose receipt says nothing was folded. Now the marker is armed only once a block was really created; a real fold arms it exactly as before. +- **OMP owns truncation recovery:** Native OMP requests carry an internal recovery-owner marker that is stripped before upstream egress; bili leaves truncation classification and retries to OMP instead of spending a second proxy retry budget. +- Upstream stream truncation errors expose only the retry code to clients; transport causes and upstream trace IDs stay in local proxy logs. +- **Reasoning-only stream recovery (#5951):** OpenAI reasoning identity filters no longer block the existing one-shot retry after an upstream EOF or read error. Pending answer filters, visible answer text, tool calls, and an exhausted retry budget still prevent replay. - **压缩树图:节点标题不再一闪即逝,点击压缩块能真正看到被压缩的消息内容**:此前树节点只有原生 SVG `` 悬停提示——选中节点或 5s 轮询重画时 DOM 一变提示就消失,实测「显示一下马上消失」;右侧 Block details 里块的消息行虽带消息编号(mNNNNN)但内容全是一句「消息已不在会话中」(宿主原生压缩把消息从上下文剪掉后,字节无处可取)。现在:① 悬停提示改为页面内常驻浮层(.g-tip)——跟随指针、显示完整块号/tier/消息范围/完整主题/tokens/消息数,选中节点或轮询重画都不再打断,离开图形区才隐藏;悬停时节点卡片上的主题行同时原地扩宽(~20 字 → 20 字上限的完整截断,离开复原);② 5s 轮询只回传瘦节点(无主题/摘要/tokens),重画前先与详情富块按 blockId 合并——节点标题、浮层元信息、消息数不再在每次 tick 后退化成光秃秃的块号;③ 服务端新增**冻结快照水合**:压缩块落盘时已把原始消息逐条存进 blockContents(`[role]`/`[role • tool]` 头 + 原文,pi 宿主 user 消息还带 mNNNNN 标记行)——水合按**标记锚定**对齐:pi 宿主打进 user 消息正文的 mNNNNN 标记是身份戳,每个标记用两条独立证据之一锚死——**恒等锚**(标记号=该位当前 ref,从未被重编号)或**冻结 span 算术锚**(标记的冻结编号 − 块冻结 span 起点 = 快照位序;span 与标记同批写入,冷加载 restamp(#5196 parked 段换新号)重编了整块 ref 也照样成立——实测 b27 全块 127 条因此恢复,旧规则因标记号已不在 ref 表里整块拒水合);锚点之间的段仅在条数平衡时按位 zip。标记既非恒等也非算术锚(冻结上下文混入外来消息/span 无法证明)、锚点乱序、无锚非叶块(快照 span 的是 effective 超集)一律拒绝,未证明的位置保留诚实的 missing 标记——宁可不显示,不冒错位张冠李戴的风险(预览 + 点击展开取原文,`/message?ref=` 同享该层)。实测大会话(253 块/9771 个直接消息引用):7789 条(80%)恢复出真实预览与原文,其余保持 missing。回归:tests/web-blocks-tree.test.ts 冻结快照水合 7 用例(按位水合 / 条数不符拒 / 标记移位旧锚 / 宿主剪块中消息的锚定恢复 / restamp 整块恢复 / 算术锚优先于巧合 ref 碰撞 / 无锚标记整块拒(含 click-to-open 双向断言)),源码级断言更新为合并后重画(renderCollapSvg(merged)。 - **交接文档「Compressed block summaries」附录不再被吞进最后一条消息卡片**:客户端 handoff 渲染器只认 `### user/assistant/tool` 三种角色分隔线(#2065 语义,正文内其他 `###` 标题是内容),export 端 #845 追加在对话之后的 `## Compressed block summaries` 整节(长会话可达 80+ 个 `### Block bN` 条目、数百行)因此全部落进最后一个 assistant 卡片,看起来像「压缩消息被追加成一条巨型消息」。现在客户端把 Conversation 之后的 `## ` 级标题识别为独立分节:`### Block bN — topic` 逐条渲染为小块卡片(mono 块号 + 主题截断标题 + tier/token 元行 + Markdown 正文),角色卡片只保留真实对话。回归:tests/web-handoff-roles.test.ts 新增附录分节用例(角色卡片数不膨胀、条目内容保真、#2065 不变量不受影响)。 - **会话历史的「条数」维度与超大未压缩请求:按真实维度拦下,判死信息点名超重字段(#5000 #5794 #5885)**:#5000 的 26,903 条消息会话(55.2MiB)穿过了**所有**字节/token 闸门、在上游死于消息条数上限——因为条数这个维度从来没有任何闸(它无法被任何字节/token 估算表达:每条消息还各带 ~69B 的 m-ref 标记,26,903 条 ≈ 1.8MB 纯标记)。现在新增按上游配置的**条数闸**(`network.maxMessages` / `BILI_MAX_MESSAGES`,默认 0=关闭——不存在全局「通用上限」,条数上限因上游而异):超限直接 413,信息点名 COUNT 维度并给出可执行指引(重启会话或回退到更早的时点;折叠在一轮 preflight 内关不掉 ~20K 条的缺口,所以是纯判死、不做条数折叠),且在 token 触发器**之前**运行——token 达标但条数超标的体(正是 #5000 的形状)也只有这道闸看得见。同时所有超窗判死信息(#5932 的三道计量闸)现在**点名超重的是哪个字段**——按 #554 计量器同源分桶(messages / tools[] / system / other),不再只给一个总数:#5000 的形状会显示 `tools[] ~X`,巨大粘贴/工具结果会显示 `messages ~X`,运维一眼知道该删什么。另新增 **32MiB 解码摄入成本带**(`network.ingestBodyBytes` / `BILI_INGEST_BODY_BYTES`):#5794 的 51.2MiB/29,619 条请求每轮独占事件循环 5.5–7.2s(本机 CPU 54.6% → 747s FATAL 自重启、全员缓存冷),成本带在 JSON 解析扫掠**之前**按解码后字节 413(stage=ingest)——任何可解析的**文本为主**的 32MiB+ 体都已是 ≥~8M token,超出所有真实模型窗口,放行必然烧钱再死。带图片的体在所有车道豁免(base64 像素按设计不进 token 计量——#496/#1800/#857「图片单独计费、绝不在原始字节上判死」;#3909 的 ~48MiB 图片密集 chat 会话与 #3701 的 ~38.5MiB WS 截图体都是受支持工作负载,由槽位预算/收件上限/车道自身的预发送字节预算+丢弃逻辑治理;Responses 车道无论如何整体豁免);并发槽位预算与 5GiB 收件上限继续独立生效。回归:tests/issue5000-count-gate(3 e2e:token 达标+条数超标→413 零上游 / 无 model 路径 / 条数达标照常转发)、tests/issue5794-ingest-cap(3 e2e:超带文本→413 stage ingest / 带内照常 / 超带带图不拦)、tests/side-request-isolation 分桶计量单测;既有 332 用例(含 #3701/#857/#496/#4079/#4185/#2490 全部回归面)全绿。 diff --git a/CONFIGURATION.md b/CONFIGURATION.md deleted file mode 100644 index c9d7248ed4..0000000000 --- a/CONFIGURATION.md +++ /dev/null @@ -1 +0,0 @@ -> Moved to [docs/configuration.md](docs/configuration.md). Redirect stub; scheduled for removal after 1–2 releases. diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md deleted file mode 100644 index e136ffdd77..0000000000 --- a/CONFIGURATION.zh-CN.md +++ /dev/null @@ -1 +0,0 @@ -> 已迁移至 [docs/configuration.zh-CN.md](docs/configuration.zh-CN.md)。重定向占位,1–2 个版本后删除。 diff --git a/docs/configuration.md b/docs/configuration.md index bae6ef4768..57947f59c4 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -2193,6 +2193,7 @@ File keys resolve only when the matching env var is unset. Defaults in parenthes | `BILI_ENCRYPTION_KEY` | Encrypt session files at rest (#708) for deployments on untrusted nodes. Exactly 32 bytes, hex (64 chars) or base64; unset = unencrypted plain-JSON files (or `BILIZSTD1` when `BILI_PERSIST_ZSTD=1` — see `BILI_PERSIST_ZSTD`). When set: every session file is written as `BILIENC1` AES-256-GCM over JSON that is zstd-compressed only when `BILI_PERSIST_ZSTD=1` (zstd on Node ≥ 22.15, raw body otherwise) — enabling compression also shrinks the file ~5–10×. Encryption and compression are now independent knobs (#1080). The key comes ONLY from this environment variable — never written to disk or logs — so keep it out of the same filesystem's reach. Invalid values abort startup (fail fast, never silently unencrypted). Booting with the wrong key skips the affected sessions as corrupt (logged, no crash). Losing the key makes encrypted sessions permanently unreadable. Symmetric by design (the same process encrypts and decrypts). Existing unencoded files are never rewritten at boot — they encrypt organically on their next save (downgrade safety; see `BILI_PERSIST_ZSTD`). Threat model (#708, owner-confirmed): it defends against **offline/mechanical** file acquisition — provider disk swaps, offline disk snapshots after node-image drift, stolen volumes, leaked backups, cloud-synced state dirs — where an offline third party cannot read the content without the key. It does NOT defend against targeted adversaries with live access to the node; for that tier move the trust root out of the proxy (KMS / TEE / confidential VMs, hardened permissions) instead of solving it inside the proxy — at that level far more than the key is exposed, so proxy-level key handling is not the boundary to hold (`BILI_PERSIST=0` disables persistence entirely). Double-encrypting the key with a second key from the same process/environment adds nothing: in every offline-theft scenario the attacker lacks exactly one artifact — your off-disk secret — whether it is called the data key or the wrapping key; only a wrapping key in a different trust domain (KMS/TPM/TEE) raises the bar, and that is scenario 2 above. | | `BILLION_CONTEXT_PROXY` | Exported by the launcher; client-side bili plugins/extensions detect it and self-disable their own compression (no double compression). | | `PI_PROXY_ANTHROPIC` | Exported by the `bili omp` launcher (only when neither `PI_PROXY` nor `PI_PROXY_ANTHROPIC` comes from the user's environment): points OMP's Cowork anthropic transport at the proxy so those requests ride the compressible lane instead of node:https direct (#2583/#3556). User-set values always win. | +| `PI_ANTHROPIC_MESSAGE_THREADS` | Exported as `0` by the `bili omp` launcher and set by the omp-native extension on load (only when the user has not set a non-blank value): OMP ≥ 18.18 otherwise sends official-URL Claude turns as Anthropic message threads, where every request carries only the newest message(s) and the upstream holds the history — bili never sees the conversation, so nothing folds (#6088). User-set values always win; pinning it yourself keeps threads on, and the proxy logs one warning per base session for such requests. | | `BILLION_CONTEXT_PLUGIN` | Set `0` to disable plugin mode entirely (wire-level tool injection resumes). | | `BILI_LAUNCHER_MODEL_WINDOWS` | Internal: the launcher hands the client's own per-model context windows (pi `models.json`, omp `models.yml`, opencode `models.<id>.limit`, codex `model_context_window`) to the spawned proxy as JSON, so the nudge denominator matches the real window for self-hosted models. Only the launcher sets it — no user configuration. | | `BILI_LAUNCHER_LANE` | Internal: the launcher hands its client's lane name (pi / codex / claude / …) to the spawned proxy, which records it in the instance file (#1225). Reuse is identity-based: two instances with *different declared* lanes never attach to each other; an instance without a declared lane is a manually-started user-zone daemon — attachable by default (#1660). Only the launcher sets it — no user configuration. | @@ -2435,7 +2436,7 @@ How each client is pointed at the proxy (set automatically in the child env): | Client | Redirect | CA trust | |---|---|---| | pi | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env manifest (extension `registerProvider`) | `NODE_EXTRA_CA_CERTS` | -| omp | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env manifest (extension `registerProvider`) + `PI_PROXY_ANTHROPIC=<proxy>` when neither `PI_PROXY` nor `PI_PROXY_ANTHROPIC` is user-set (routes OMP's Cowork anthropic lane through the proxy instead of node:https direct, #2583/#3556) | `NODE_EXTRA_CA_CERTS` | +| omp | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env manifest (extension `registerProvider`) + `PI_PROXY_ANTHROPIC=<proxy>` when neither `PI_PROXY` nor `PI_PROXY_ANTHROPIC` is user-set (routes OMP's Cowork anthropic lane through the proxy instead of node:https direct, #2583/#3556) + `PI_ANTHROPIC_MESSAGE_THREADS=0` when unset (otherwise OMP ≥ 18.18 keeps the conversation history server-side, invisible to bili, #6088) | `NODE_EXTRA_CA_CERTS` | | codex | `HTTPS_PROXY` + `-c key=value` overrides | `SSL_CERT_FILE` → `combined-ca.pem` | | claude | `ANTHROPIC_BASE_URL` = `/bili/` URL (Vertex: `ANTHROPIC_VERTEX_BASE_URL`, when `CLAUDE_CODE_USE_VERTEX=1`; + `NO_PROXY`/`no_proxy` loopback merge, #3446) | none needed | | opencode | `HTTPS_PROXY` + isolated `OPENCODE_CONFIG` | `NODE_EXTRA_CA_CERTS` | @@ -2520,7 +2521,7 @@ bili plugin list # install status for every supported host bili plugin remove pi # undo (original files backed up to *.bili-bak once) ``` -`install pi` also replaces any **legacy** billion-context entries (old `npm:billion-context-pi` references, stale `npm:billion-context@x.y.z`, leftover dev-checkout paths) so exactly one bili plugin stays live; when it drops entries the output also reminds you that a **project-scope** entry (`pi install -l`, written to `<project>/.pi/settings.json`) is outside this global settings and must be removed by hand. With an npm-installed bili the written entry is the pi-managed spec `npm:billion-context` — pi installs it into `~/.pi/agent/npm/` automatically (also on startup if missing) and `pi update` upgrades it, so the entry survives node prefix moves across machines; a dev/checkout install keeps the machine-local abs path (pi loads local package dirs directly). Both load the `pi` manifest → `dist/agent/pi-native.js`, the self-spawning native entry — bare `pi` gets full plugin-mode compression with no launcher (#519). +`install pi` also replaces any **legacy** billion-context entries (old `npm:billion-context-pi` references, stale `npm:billion-context@x.y.z`, leftover dev-checkout paths) so exactly one bili plugin stays live; when it drops entries the output also reminds you that a **project-scope** entry (`pi install -l`, written to `<project>/.pi/settings.json`) is outside this global settings and must be removed by hand. With an npm-installed bili the written entry is the pi-managed spec `npm:billion-context` — pi installs it into `~/.pi/agent/npm/` automatically (also on startup if missing) and `pi update` upgrades it, so the entry survives node prefix moves across machines; a dev/checkout install keeps the machine-local abs path (pi loads local package dirs directly). Both load the `pi` manifest → `dist/agent/billion-context.js`, which delegates to the self-spawning native entry `dist/agent/pi-native.js` — bare `pi` gets full plugin-mode compression with no launcher (#519). **Coexistence with the standalone `billion-context-pi` extension.** The two are mutually exclusive — both active means double compression. Three nets: the installer strip above (global settings), the `BILLION_CONTEXT_NATIVE=pi` marker the native entry sets synchronously at load (`billion-context-pi` **0.1.72+** re-reads it on every event and refuses; 0.1.71 and older check `BILLION_CONTEXT_PROXY` only at load time, before the native proxy exists), and a runtime scan: once its proxy is up, the pi-native entry reads both pi settings files and warns loudly about any co-resident legacy entry the installer never saw (project scope, or installed after the fact). diff --git a/docs/configuration.zh-CN.md b/docs/configuration.zh-CN.md index 45861d8461..bf9030c47f 100644 --- a/docs/configuration.zh-CN.md +++ b/docs/configuration.zh-CN.md @@ -2161,6 +2161,7 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 | `BILI_ENCRYPTION_KEY` | 会话文件静态加密(#708),适用于部署在不可信节点的场景。密钥必须恰好 32 字节,hex(64 字符)或 base64;未设置 = 不加密的纯 JSON 文件(设 `BILI_PERSIST_ZSTD=1` 时为 `BILIZSTD1`——参见 `BILI_PERSIST_ZSTD`)。设置后:每个会话文件均以 `BILIENC1` 格式写入,即对 JSON 施加 AES-256-GCM 加密,JSON 仅在 `BILI_PERSIST_ZSTD=1` 时以 zstd 压缩(Node ≥ 22.15 使用 zstd,其余情况写入原始数据)——启用压缩还可将文件体积缩小约 5–10 倍。加密与压缩现为独立的配置项(#1080)。密钥只从该环境变量读取——永不落盘、永不进日志——请确保它不受同一文件系统上的其他进程触及。非法值会导致启动中止(快速失败,绝不静默明文运行)。用错误的密钥启动时,受影响的会话按损坏文件跳过(有日志,不崩溃)。丢失密钥将使已加密的会话永久不可读。对称加密为刻意设计(同一进程既加密又解密)。已有的未编码文件从不在启动时改写——在其下一次保存时自然加密(降级安全;参见 `BILI_PERSIST_ZSTD`)。威胁模型(#708,owner 确认):防的是**离线/机械性**的文件获取——云厂商换盘、节点镜像漂移后的离线磁盘快照、磁盘镜像失窃、备份泄露、被云同步的状态目录——离线第三方拿不到密钥即无法读取内容。不防御对活节点有访问权的定向攻击者;那一档应把信任根移出 proxy(KMS / TEE / 机密虚拟机 + 强化权限体系),而不是在 proxy 本身想办法——到了那个程度暴露的远不止密钥,proxy 层不是该守的边界(`BILI_PERSIST=0` 可彻底关闭持久化)。用同一进程/环境中的第二把密钥对密钥做二次加密不增加任何安全性:所有离线失窃场景里攻击者缺的始终只有一个工件——你的非落盘秘密——无论它叫数据密钥还是包裹密钥;只有把包裹密钥放进不同信任域(KMS/TPM/TEE)才能提高门槛,而那属于上面的场景 2。 | | `BILLION_CONTEXT_PROXY` | launcher 会导出它;客户端侧 bili 插件/扩展检测到后自禁用自身压缩(避免双重压缩)。 | | `PI_PROXY_ANTHROPIC` | 由 `bili omp` launcher 导出(仅当 `PI_PROXY` 与 `PI_PROXY_ANTHROPIC` 均未来自用户环境时):把 OMP 的 Cowork anthropic 传输指向代理,让这些请求走可压缩通道而非 node:https 直连(#2583/#3556)。用户设置的值始终优先。 | +| `PI_ANTHROPIC_MESSAGE_THREADS` | 由 `bili omp` launcher 导出为 `0`,omp-native 扩展加载时同样设置(仅当用户未设置非空值时):否则 OMP ≥ 18.18 会把官方 URL 的 Claude 轮次走 Anthropic message threads —— 每个请求只携带最新消息、历史由上游持有,bili 看不到会话内容、无从压缩(#6088)。用户设置的值始终优先;自行钉住该值可保留 threads,代理会对这类请求每个基础会话告警一次。 | | `BILLION_CONTEXT_PLUGIN` | 设 `0` 彻底关闭插件模式(恢复 wire 层工具注入)。 | | `BILI_LAUNCHER_MODEL_WINDOWS` | 内部使用:launcher 把客户端自身配置里的逐模型上下文窗口(pi `models.json`、omp `models.yml`、opencode `models.<id>.limit`、codex `model_context_window`)以 JSON 传给自己拉起的代理,让 nudge 分母对自托管模型也用真实窗口。只有 launcher 会设置,无需用户配置。 | | `BILI_LAUNCHER_PLUGIN` | 设 `0` 关闭 launcher 为 claude/codex 注入 bili MCP 服务器(退回纯 wire 模式);设 `1` 强制插件模式。默认注入——但 codex 上游为本地/私网地址时自动退回 wire 模式(sglang/vllm/ollama 不解析 codex 的 namespace 工具类型)。见[启动器参考](#启动器参考)。 | @@ -2402,7 +2403,7 @@ MITM 只对一份**白名单**中的模型域名生效(`open.bigmodel.cn`、`a | 客户端 | 重定向方式 | CA 信任 | |---|---|---| | pi | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env 清单(扩展 `registerProvider`) | `NODE_EXTRA_CA_CERTS` | -| omp | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env 清单(扩展 `registerProvider`)+ 用户未设置 `PI_PROXY` / `PI_PROXY_ANTHROPIC` 时的 `PI_PROXY_ANTHROPIC=<proxy>`(将 OMP 的 Cowork anthropic 通道经代理路由而非 node:https 直连,#2583/#3556) | `NODE_EXTRA_CA_CERTS` | +| omp | `HTTPS_PROXY` + `BILI_PROVIDER_REWRITES` env 清单(扩展 `registerProvider`)+ 用户未设置 `PI_PROXY` / `PI_PROXY_ANTHROPIC` 时的 `PI_PROXY_ANTHROPIC=<proxy>`(将 OMP 的 Cowork anthropic 通道经代理路由而非 node:https 直连,#2583/#3556)+ 未设置时的 `PI_ANTHROPIC_MESSAGE_THREADS=0`(否则 OMP ≥ 18.18 的会话历史留在服务端、bili 不可见,#6088) | `NODE_EXTRA_CA_CERTS` | | codex | `HTTPS_PROXY` + `-c key=value` 覆盖 | `SSL_CERT_FILE` → `combined-ca.pem` | | claude | `ANTHROPIC_BASE_URL` = `/bili/` URL(Vertex:`ANTHROPIC_VERTEX_BASE_URL`,当 `CLAUDE_CODE_USE_VERTEX=1`) | 无需 | | opencode | `HTTPS_PROXY` + 隔离 `OPENCODE_CONFIG` | `NODE_EXTRA_CA_CERTS` | diff --git a/hermes-plugin/__init__.py b/hermes-plugin/__init__.py index 3b3ccb8867..c9e04e4d8d 100644 --- a/hermes-plugin/__init__.py +++ b/hermes-plugin/__init__.py @@ -1211,7 +1211,11 @@ def ensure_origin(sidecar: Dict[str, str], deadline: Optional[float] = None) -> # — runtime effects ------------------------------------------------------------- # The exact keys apply_env routes; _unroute_env restores them one-for-one on origin death (#4644). -_ENV_ROUTED_KEYS = ("HTTPS_PROXY", "https_proxy", "HERMES_CA_BUNDLE", "SSL_CERT_FILE") +# The HTTP pair and NO_PROXY are here too, so a plain-http provider is unrouted with the rest. +_ENV_ROUTED_KEYS = ("HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", + "NO_PROXY", "no_proxy", "HERMES_CA_BUNDLE", "SSL_CERT_FILE") +# Loopback stays direct, as the launcher's own plain-http routes do (LAUNCHER_LOOPBACK_NO_PROXY). +_LOOPBACK_NO_PROXY = "127.0.0.1,localhost,::1" def apply_env(origin: str, sidecar: Optional[Dict[str, str]] = None, ctx: Any = None) -> bool: @@ -1231,6 +1235,13 @@ def apply_env(origin: str, sidecar: Optional[Dict[str, str]] = None, ctx: Any = _state["env_backup"] = {key: os.environ.get(key) for key in _ENV_ROUTED_KEYS} os.environ["HTTPS_PROXY"] = origin os.environ["https_proxy"] = origin + # A plain-http provider base_url (a LAN llama.cpp / vLLM / ollama server) is only routed + # by HTTP_PROXY; with the HTTPS pair alone its traffic bypasses the proxy entirely and + # the session gets no compression. The proxy forwards absolute-form http requests. + os.environ["HTTP_PROXY"] = origin + os.environ["http_proxy"] = origin + os.environ.setdefault("NO_PROXY", _LOOPBACK_NO_PROXY) + os.environ.setdefault("no_proxy", _LOOPBACK_NO_PROXY) ca_dir = data_dir() / "ca" root = ca_dir / "root-ca.pem" if root.is_file(): diff --git a/kernel/package-lock.json b/kernel/package-lock.json index 42edfa9cb7..1173e00f65 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.196", + "version": "0.0.197", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.196", + "version": "0.0.197", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/kernel/package.json b/kernel/package.json index f03fc7adee..955a551a83 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.196", + "version": "0.0.197", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", diff --git a/kernel/src/wire/anthropic.ts b/kernel/src/wire/anthropic.ts index ef070160d5..c99537e6e2 100644 --- a/kernel/src/wire/anthropic.ts +++ b/kernel/src/wire/anthropic.ts @@ -511,6 +511,85 @@ export function healAnthropicSystemPositions( return out; } +/** Server-tool pairing backstop. Server-side tools (advisor, web_search, + * code_execution, …) put BOTH halves of an exchange inside one assistant + * message: a `server_tool_use` block and its `<name>_tool_result` block + * (paired by `tool_use_id` → `id`). anthropicToCore splits every block into + * its own BiliMessage (unknown types ride the rawAnthropicBlock sidecar), so + * a fold can cover one half and leave the other. Anthropic then rejects the + * WHOLE request, permanently, e.g. + * 'messages.5.content.0: unexpected tool_use_id found in advisor_tool_result + * blocks: srvtoolu_… Each advisor_tool_result block must have a + * corresponding server_tool_use block in the same message'. + * Field repro: claude-code + advisor tool, bili v0.1.213 / acp-kernel 0.0.195. + * + * Heal: per assistant message, drop a `*_tool_result` whose `tool_use_id` + * has no `server_tool_use` in the same message, and a `server_tool_use` + * without its result — except in the LAST message, where an open server + * call is legal client state. Plain `tool_use`/`tool_result` are never + * touched (their pairing spans user/assistant and has its own contract). + * A message emptied by the heal gets a placeholder text block so roles stay + * alternating. Legal messages are left as the same object (no churn for the + * prefix cache); only messages that would 400 anyway are rewritten, so the + * thinking-signature rule (KDD #10) never loses a request that would have + * succeeded. Returns the number of dropped blocks. */ +function isServerToolResultBlock(b: { type?: unknown }): boolean { + return ( + typeof b.type === "string" && + b.type !== "tool_result" && + b.type.endsWith("_tool_result") + ); +} + +const SERVER_TOOL_FOLDED_PLACEHOLDER = "(server tool exchange folded)"; + +function healUnpairedServerToolBlocks(out: AnthropicWireMessage[]): number { + let dropped = 0; + for (let mi = 0; mi < out.length; mi++) { + const msg = out[mi]!; + if (msg.role !== "assistant" || !Array.isArray(msg.content)) continue; + const blocks = msg.content as unknown as Array<Record<string, unknown>>; + const uses = new Set<string>(); + const results = new Set<string>(); + for (const b of blocks) { + if (!b || typeof b !== "object") continue; + if (b.type === "server_tool_use" && typeof b.id === "string") + uses.add(b.id); + else if (isServerToolResultBlock(b) && typeof b.tool_use_id === "string") + results.add(b.tool_use_id); + } + if (uses.size === 0 && results.size === 0) continue; + const isLast = mi === out.length - 1; + const kept = blocks.filter((b) => { + if (!b || typeof b !== "object") return true; + if ( + b.type === "server_tool_use" && + typeof b.id === "string" && + !results.has(b.id) && + !isLast + ) + return false; + if ( + isServerToolResultBlock(b) && + typeof b.tool_use_id === "string" && + !uses.has(b.tool_use_id) + ) + return false; + return true; + }); + if (kept.length === blocks.length) continue; + dropped += blocks.length - kept.length; + out[mi] = { + role: "assistant", + content: + kept.length > 0 + ? (kept as unknown as AnthropicBlock[]) + : [{ type: "text", text: SERVER_TOOL_FOLDED_PLACEHOLDER }], + }; + } + return dropped; +} + export function coreToAnthropic( messages: BiliMessage[], cacheControls?: Map<string, unknown>, @@ -584,6 +663,7 @@ export function coreToAnthropic( } } flush(); + healUnpairedServerToolBlocks(out); healIllegalSystemPositions(out); return out; } diff --git a/kernel/tests/server-tool-pair-heal.test.ts b/kernel/tests/server-tool-pair-heal.test.ts new file mode 100644 index 0000000000..50228a23f2 --- /dev/null +++ b/kernel/tests/server-tool-pair-heal.test.ts @@ -0,0 +1,160 @@ +// Server-tool pairing backstop in coreToAnthropic. Server tools (advisor, +// web_search, …) carry server_tool_use + <name>_tool_result inside ONE +// assistant message; anthropicToCore splits each block into its own +// BiliMessage, so a fold can drop one half. Anthropic then 400s the whole +// request: "unexpected tool_use_id found in advisor_tool_result blocks … +// Each advisor_tool_result block must have a corresponding server_tool_use +// block in the same message". Positive AND negative rows (#3617): orphans +// are healed; every legal shape round-trips byte-identical. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { anthropicToCore, coreToAnthropic } from "../src/wire/anthropic.js"; +import type { AnthropicWireMessage } from "../src/wire/anthropic.js"; + +const use = (id: string) => ({ + type: "server_tool_use", + id, + name: "advisor", + input: {}, +}); +const res = (id: string) => ({ + type: "advisor_tool_result", + tool_use_id: id, + content: { type: "advisor_result", text: "advice" }, +}); +const wire = (messages: unknown[]) => messages as AnthropicWireMessage[]; +const roundTrip = (messages: AnthropicWireMessage[]) => { + const { msgs, cacheControls } = anthropicToCore({ + model: "m", + max_tokens: 1, + messages, + } as never); + return { msgs, out: coreToAnthropic(msgs, cacheControls) }; +}; + +test("legal server-tool exchanges round-trip byte-identical (negative rows)", () => { + const messages = wire([ + { role: "user", content: [{ type: "text", text: "q1" }] }, + { + role: "assistant", + content: [ + use("srvtoolu_a"), + res("srvtoolu_a"), + { type: "text", text: "a1" }, + ], + }, + { role: "user", content: [{ type: "text", text: "q2" }] }, + { + role: "assistant", + content: [ + { type: "text", text: "searching" }, + { + type: "server_tool_use", + id: "srvtoolu_w", + name: "web_search", + input: { query: "x" }, + }, + { + type: "web_search_tool_result", + tool_use_id: "srvtoolu_w", + content: [], + }, + { type: "text", text: "a2" }, + ], + }, + { role: "user", content: [{ type: "text", text: "q3" }] }, + // Open server call in the LAST message is legal client state — kept. + { role: "assistant", content: [use("srvtoolu_open")] }, + ]); + assert.deepEqual(roundTrip(messages).out, messages); +}); + +test("plain tool_use / tool_result pairs are never touched", () => { + const messages = wire([ + { role: "user", content: [{ type: "text", text: "q" }] }, + { + role: "assistant", + content: [{ type: "tool_use", id: "toolu_1", name: "Bash", input: {} }], + }, + { + role: "user", + content: [{ type: "tool_result", tool_use_id: "toolu_1", content: "ok" }], + }, + ]); + assert.deepEqual(roundTrip(messages).out, messages); +}); + +test("result whose server_tool_use was folded away is dropped", () => { + const messages = wire([ + { role: "user", content: [{ type: "text", text: "q1" }] }, + { + role: "assistant", + content: [ + use("srvtoolu_a"), + res("srvtoolu_a"), + { type: "text", text: "a1" }, + ], + }, + { role: "user", content: [{ type: "text", text: "q2" }] }, + ]); + const { msgs } = roundTrip(messages); + // Simulate a fold that covered only the server_tool_use block. + const folded = msgs.filter( + (m) => + !(m.rawAnthropicBlock as { type?: string } | undefined)?.type?.startsWith( + "server_tool_use", + ), + ); + assert.equal(folded.length, msgs.length - 1); + const out = coreToAnthropic(folded); + assert.deepEqual(out[1], { + role: "assistant", + content: [{ type: "text", text: "a1" }], + }); +}); + +test("server_tool_use whose result was folded away is dropped (not in last message)", () => { + const messages = wire([ + { role: "user", content: [{ type: "text", text: "q1" }] }, + { + role: "assistant", + content: [ + { type: "text", text: "a0" }, + use("srvtoolu_a"), + res("srvtoolu_a"), + ], + }, + { role: "user", content: [{ type: "text", text: "q2" }] }, + ]); + const { msgs } = roundTrip(messages); + const folded = msgs.filter( + (m) => + (m.rawAnthropicBlock as { type?: string } | undefined)?.type !== + "advisor_tool_result", + ); + const out = coreToAnthropic(folded); + assert.deepEqual(out[1], { + role: "assistant", + content: [{ type: "text", text: "a0" }], + }); +}); + +test("an assistant message emptied by the heal keeps role alternation via placeholder", () => { + const messages = wire([ + { role: "user", content: [{ type: "text", text: "q1" }] }, + { role: "assistant", content: [use("srvtoolu_a"), res("srvtoolu_a")] }, + { role: "user", content: [{ type: "text", text: "q2" }] }, + ]); + const { msgs } = roundTrip(messages); + const folded = msgs.filter( + (m) => + (m.rawAnthropicBlock as { type?: string } | undefined)?.type !== + "server_tool_use", + ); + const out = coreToAnthropic(folded); + assert.equal(out.length, 3); + assert.equal(out[1]!.role, "assistant"); + assert.deepEqual(out[1]!.content, [ + { type: "text", text: "(server tool exchange folded)" }, + ]); +}); diff --git a/package-lock.json b/package-lock.json index e2ff6fb483..85f7ba59df 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.196", + "version": "0.0.197", "dev": true, "license": "MIT", "devDependencies": { @@ -9283,7 +9283,7 @@ }, "pi-subagents": { "name": "billion-context-pi-subagents", - "version": "0.2.3", + "version": "0.2.4", "dev": true, "license": "MIT", "devDependencies": { diff --git a/package.json b/package.json index 13ff1da560..c8a40ff763 100644 --- a/package.json +++ b/package.json @@ -111,7 +111,7 @@ "pi": { "image": "https://raw.githubusercontent.com/ranxianglei/billion-context/master/docs/logo.png", "extensions": [ - "./dist/agent/pi-native.js" + "./dist/agent/billion-context.js" ] }, "publishConfig": { diff --git a/pi-subagents/package.json b/pi-subagents/package.json index 4a7a958a52..5e764e3c19 100644 --- a/pi-subagents/package.json +++ b/pi-subagents/package.json @@ -1,6 +1,6 @@ { "name": "billion-context-pi-subagents", - "version": "0.2.3", + "version": "0.2.4", "description": "acp_delegate sub-agent tools for the Pi coding agent — split out of billion-context-pi (#612); vendored in-repo (#2384).", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/pi-subagents/src/fleet-inspector.ts b/pi-subagents/src/fleet-inspector.ts index 44a113fc44..baf47d2d1f 100644 --- a/pi-subagents/src/fleet-inspector.ts +++ b/pi-subagents/src/fleet-inspector.ts @@ -1,6 +1,6 @@ import { openSync, fstatSync, readSync, closeSync } from "node:fs"; import type { ExtensionContext } from "@earendil-works/pi-coding-agent"; -import { Key, Markdown, matchesKey, truncateToWidth, wrapTextWithAnsi, type MarkdownTheme } from "@earendil-works/pi-tui"; +import { Key, Markdown, matchesKey, truncateToWidth, visibleWidth, wrapTextWithAnsi, type MarkdownTheme } from "@earendil-works/pi-tui"; import type { Component } from "@earendil-works/pi-tui"; import { OUT_DIR, fleetRunsSnapshot, type FleetRunView } from "./delegate-tool.js"; @@ -172,7 +172,7 @@ export function formatToolLabel(name: string, args: unknown): string { case "write": return `write ${s(a.path ?? a.file_path)}`; case "edit": return `edit ${s(a.path ?? a.file_path)}`; case "bash": { - const cmd = s(a.command).replace(/[\n\t]/g, " ").trim(); + const cmd = s(a.command).replace(/\r?\n|\t/g, " ").trim(); return `bash ${cmd.length > 60 ? `${cmd.slice(0, 60)}…` : cmd}`; } case "grep": return `grep /${s(a.pattern)}/ in ${s(a.path ?? ".")}`; @@ -270,8 +270,10 @@ function buildMarkdownTheme(theme: InspectorTheme): MarkdownTheme { export function renderTranscriptBlocks(blocks: TranscriptBlock[], theme: InspectorTheme, innerW: number): string[] { const out: string[] = []; const wrapW = Math.max(10, innerW); - // Full-width background bar; truncateToWidth pads to innerW (ANSI + CJK aware) so the bg spans the row. - const bar = (bgColor: string, content: string): string => theme.bg(bgColor, truncateToWidth(content, innerW, undefined, true)); + const bar = (bgColor: string, content: string): string => { + const clipped = truncateToWidth(content, innerW, theme.bg(bgColor, "...")); + return theme.bg(bgColor, clipped) + theme.bg(bgColor, " ".repeat(Math.max(0, innerW - visibleWidth(clipped)))); + }; let i = 0; while (i < blocks.length) { const b = blocks[i]; @@ -329,7 +331,7 @@ function pushToolGroup(out: string[], bar: (bg: string, c: string) => string, th const label = formatToolLabel(name, args); if (label) out.push(bar(bgc, theme.fg("dim", label))); if (result) { - const rawLines = result.text.replace(/\n+$/, "").split("\n"); + const rawLines = result.text.replace(/\r\n/g, "\n").replace(/\n+$/, "").split("\n"); const shown = rawLines.slice(0, MAX_RESULT_LINES); for (const l of shown) out.push(bar(bgc, theme.fg("toolOutput", l))); if (rawLines.length > shown.length) out.push(bar(bgc, theme.fg("dim", `… +${rawLines.length - shown.length} more`))); diff --git a/pi-subagents/tests/fleet-inspector.test.ts b/pi-subagents/tests/fleet-inspector.test.ts index 2367d4d8bc..2b73bc5769 100644 --- a/pi-subagents/tests/fleet-inspector.test.ts +++ b/pi-subagents/tests/fleet-inspector.test.ts @@ -48,7 +48,7 @@ test("usageSummary hides zero-usage and formats tokens", () => { assert.equal(usageSummary(undefined), undefined); assert.equal(usageSummary({ input: 0, output: 0, totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } }), undefined); const s = usageSummary({ input: 1234, output: 56, totalTokens: 1290, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } }); - assert.ok(s!.includes("1,234") && s!.includes("56"), s); + assert.ok(s!.includes((1234).toLocaleString()) && s!.includes("56"), s); }); // ─── readTailSync ─────────────────────────────────────────────────────────── @@ -302,3 +302,85 @@ test("renderTranscriptBlocks renders assistant text as markdown (heading/list)", assert.ok(!lines.some((l) => l.trim().startsWith("#")), "markdown heading marker stripped"); assert.ok(lines.some((l) => l.includes("first point")), "list item present"); }); + +test("transcript rendering treats CRLF as newlines without changing source blocks", () => { + const ansiTheme: InspectorTheme = { + fg: (_c, t) => `\x1b[32m${t}\x1b[39m`, + bold: (t) => `\x1b[1m${t}\x1b[22m`, + bg: (_c, t) => `\x1b[42m${t}\x1b[49m`, + }; + const blocks: TranscriptBlock[] = [ + { kind: "user", text: "first\r\n\r\nsecond\r\n" }, + { kind: "thinking", text: "thinking\r\nnext" }, + { kind: "text", text: "# Heading\r\n\r\nbody" }, + { kind: "toolCall", name: "read", text: "", args: { path: "fixture.txt" } }, + { kind: "toolResult", name: "read", text: "short\r\n123456\r\n\r\ntail\r\n" }, + { kind: "toolCall", name: "bash", text: "", args: { command: "echo first\r\necho second" } }, + { kind: "toolResult", name: "bash", text: " M file.txt\r\n", isError: true }, + ]; + const original = JSON.stringify(blocks); + const lf = blocks.map((b) => ({ ...b, text: b.text.replace(/\r\n/g, "\n") })); + const paintRow = (row: string): string => { + const cells: string[] = []; + let x = 0; + for (const char of row) { + if (char === "\r") x = 0; + else cells[x++] = char; + } + return cells.join(""); + }; + const frame = (row: string, width: number): string => `│ ${row}${" ".repeat(width - visibleWidth(row))} │`; + assert.ok(paintRow(frame("short\r", 20)).lastIndexOf("│") < 23, "negative control: an unnormalized CR shifts the border left and overwrites text"); + for (const width of [16, 40, 80, 120]) { + assert.deepEqual(renderTranscriptBlocks(blocks, ansiTheme, width), renderTranscriptBlocks(lf, ansiTheme, width)); + const lines = renderTranscriptBlocks(blocks, plainTheme, width); + assert.ok(lines.every((line) => !line.includes("\r")), "CR must not move the terminal cursor inside a framed row"); + for (const line of lines) { + const framed = frame(line, width); + assert.equal(paintRow(framed), framed, "padding must not overwrite text or shift the border"); + } + } + assert.equal(JSON.stringify(blocks), original, "stored text and tool arguments remain unchanged"); +}); + + +test("bash command previews treat CRLF as LF without changing arguments", () => { + const args = { command: "echo first\r\necho second\tend" }; + const original = args.command; + assert.equal(formatToolLabel("bash", args), formatToolLabel("bash", { command: "echo first\necho second\tend" })); + assert.ok(!formatToolLabel("bash", args).includes("\r")); + assert.equal(args.command, original); +}); + +test("truncated tool output keeps the generated ellipsis background, including wide graphemes", () => { + const ansiTheme: InspectorTheme = { + fg: (_c, t) => `\x1b[32m${t}\x1b[39m`, + bold: (t) => `\x1b[1m${t}\x1b[22m`, + bg: (_c, t) => `\x1b[42m${t}\x1b[49m`, + }; + function backgroundSpans(line: string): { text: string; painted: boolean }[] { + let painted = false; + const spans: { text: string; painted: boolean }[] = []; + for (const match of line.matchAll(/\x1b\[([\d;]*)m|([^\x1b]+)/g)) { + if (match[1] !== undefined) { + for (const code of match[1].split(";").map(Number)) { + if (code === 0 || code === 49) painted = false; + else if (code === 42) painted = true; + } + } else spans.push({ text: match[2]!, painted }); + } + return spans; + } + for (const width of [1, 2, 3, 10, 21, 40, 80, 120]) { + for (const text of ["x".repeat(200), "界".repeat(200), "👩‍💻é".repeat(200), "\x1b[31m" + "x".repeat(200) + "\x1b[39m"]) { + const lines = renderTranscriptBlocks([{ kind: "toolResult", name: "read", text }], ansiTheme, width); + assertWidthSafe(lines, width); + const line = lines.at(-1)!; + const spans = backgroundSpans(line); + assert.ok(spans.every((s) => s.painted), `default-background notch: ${JSON.stringify(line)}`); + assert.ok(spans.map((s) => s.text).join("").trimEnd().endsWith("."), "truncation remains visible"); + assert.equal(visibleWidth(line), width, "wide-character truncation padding remains painted"); + assert.ok(line.endsWith("\x1b[49m"), "background does not leak out of the row"); + } + } +}); diff --git a/reference/architecture.md b/reference/architecture.md index 41bc0ffb6a..484cd70bf1 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -28,6 +28,7 @@ billion-context/ │ │ ├── prepare-openai.ts # OpenAI chat request preparation (#1440) │ │ ├── prepare-responses.ts # Responses request preparation + codex compact/prompt-cache helpers (#1440) │ │ ├── relay.ts # Upstream relay zone: forward() + fake-completion recovery (#1440 P2 cut 4) +│ │ ├── rejection-diagnostics.ts # Bounded terminal-cause diagnostic for a relayed upstream rejection: one nested attempts[].details.error.message, scrubbed + bounded, appended to the relay log line (#4452) │ │ ├── request-cost.ts # Per-request local-cost line (outbound measured at the real wire body) + #5408 heartbeat + wall-clock slow-session advisory, facts only (#903/#4528/#5744) │ │ ├── responses-ordering.ts # #564 Responses assistant-run ordering repair + producer-owned turn-separator provenance (#2627) │ │ ├── responses-separator.ts # Canonical Responses turn-separator literal and carrier predicates (#2627) diff --git a/scripts/write-build-info.mjs b/scripts/write-build-info.mjs index e7cff9b618..ce707551dc 100644 --- a/scripts/write-build-info.mjs +++ b/scripts/write-build-info.mjs @@ -31,6 +31,7 @@ function buildCommit() { const info = { commit: buildCommit() }; const distDir = resolve(root, "dist"); -mkdirSync(distDir, { recursive: true }); +mkdirSync(resolve(distDir, "agent"), { recursive: true }); writeFileSync(resolve(distDir, "build-info.json"), `${JSON.stringify(info, null, 2)}\n`); +writeFileSync(resolve(distDir, "agent/billion-context.js"), 'export { default } from "./pi-native.js";\n'); console.log(`build-info: commit=${info.commit}`); diff --git a/src/agent/native-bootstrap.ts b/src/agent/native-bootstrap.ts index 23bbc2868b..026c34b4a5 100644 --- a/src/agent/native-bootstrap.ts +++ b/src/agent/native-bootstrap.ts @@ -97,6 +97,23 @@ export function applyOmpFirstEventTimeout(env: NodeJS.ProcessEnv): void { if (value !== undefined) env[OMP_FIRST_EVENT_TIMEOUT_ENV] = value; } +/** #6088: OMP >= 18.18 sends official-URL Claude turns as Anthropic message + * threads — after the first turn each request carries only the newest + * message(s) and Anthropic keeps the history server-side, so bili never sees + * the conversation and never compresses it (silently). PI_PROXY_ANTHROPIC + * leaves the model's base URL official, so OMP's model policy keeps threads on. + * When bili owns the process's model traffic we therefore default threads off; + * an explicit user value wins (blank counts as unset). */ +export const OMP_MESSAGE_THREADS_ENV = "PI_ANTHROPIC_MESSAGE_THREADS"; + +/** Stamp "0" onto env for OMP's message-threads switch when (and only when) the + * user has not pinned a value. No-op otherwise. */ +export function applyOmpMessageThreadsOff(env: NodeJS.ProcessEnv): void { + const raw = env[OMP_MESSAGE_THREADS_ENV]; + if (raw !== undefined && raw.trim().length > 0) return; + env[OMP_MESSAGE_THREADS_ENV] = "0"; +} + /** Coexistence marker (#820): tells standalone in-process bili extensions * (billion-context-pi / opencode-acp) that a host-native entry owns THIS * process so they back off instead of double-compressing. Set synchronously @@ -168,3 +185,10 @@ export function setNativeOriginWaiter(waiter: NativeOriginWaiter): void { export async function awaitNativeProxyOrigin(): Promise<string | undefined> { return originWaiter === undefined ? undefined : originWaiter.wait(); } + +/** Test hook: clear the registered origin waiter so suites that import a + * native entry (which arms at module load) do not leak its INLINE_ORIGIN + * waiter into the shared factory's awaitNativeProxyOrigin(). */ +export function _resetNativeOriginWaiterForTest(): void { + originWaiter = undefined; +} diff --git a/src/agent/omp-native.ts b/src/agent/omp-native.ts index a18c0e9fb1..66022f7288 100644 --- a/src/agent/omp-native.ts +++ b/src/agent/omp-native.ts @@ -23,13 +23,14 @@ import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST } from "../launcher.js"; import { createBiliPlugin } from "./pi.js"; -import { applyOmpFirstEventTimeout, markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, setNativeOriginWaiter, singleFlight } from "./native-bootstrap.js"; +import { applyOmpFirstEventTimeout, applyOmpMessageThreadsOff, markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, setNativeOriginWaiter, singleFlight } from "./native-bootstrap.js"; import { installNativeFetchIntercept, noteRoutedOrigin, observeRoutedOrigin, readyOrigin, setDeclaredModelEndpoints, type NativeInterceptState } from "./native-intercept.js"; import { loadDeclaredModelEndpoints } from "../model-endpoints.js"; import { clearIncrementalProbe, createPrepareDirectSend, hostMsgIdHintFor, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalAck, noteIncrementalRejection, planIncrementalTurn, probeIncrementalTurns, rememberOriginalBody, sweepStale, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.js"; import * as pfChannel from "./pf-channel.js"; import { fetchProxyVersion, waitForProxyVersion } from "./shared.js"; import { logHostEvent } from "./host-log.js"; +import { BILI_HOST_RECOVERY_OWNER_HEADER, BILI_HOST_RECOVERY_OWNER_OMP } from "../util.js"; /** Decides whether the native bootstrap should run in this process. */ export function shouldBootstrapNativeOmp(env: NodeJS.ProcessEnv): boolean { @@ -68,7 +69,11 @@ function errMessage(err: unknown): string { return err instanceof Error ? err.message : String(err); } -const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve(undefined) }; +const state: NativeInterceptState = { + origin: undefined, + ready: Promise.resolve(undefined), + headersFor: () => ({ [BILI_HOST_RECOVERY_OWNER_HEADER]: BILI_HOST_RECOVERY_OWNER_OMP }), +}; // #3248 phase 2: incremental-turn divert for omp. omp never emits header // events, so the ONLY identity signal at the fetch layer is the @@ -411,8 +416,11 @@ if (plan.mode !== "off") armNativeOmp(plan); // preflight over a large context holds the response for minutes while OMP only // sees keep-alive comments, and its default 300s timer would abort mid-compression. // Sync at module eval so it lands before the first request; user-pinned values win. +// #6088: same for OMP's message threads — OMP reads the switch per request, so +// defaulting it off here makes every turn carry the full history bili compresses. if (process.env.NODE_TEST_CONTEXT === undefined && ompTrafficRidesBili(process.env)) { applyOmpFirstEventTimeout(process.env); + applyOmpMessageThreadsOff(process.env); } // node:test imports this module for shouldBootstrapNativeOmp — never @@ -451,6 +459,11 @@ export function _stateRespawnForTest(): (() => Promise<string | undefined>) | un return state.respawn; } +/** Test hook: inspect the owner marker installed by the native OMP host. */ +export function _headersForTest(url: string): Record<string, string> | undefined { + return state.headersFor?.(url); +} + /** Test hook (#1365): record routed-channel evidence without the fetch patch. */ export function _noteRoutedForTest(url: string): void { noteRoutedOrigin(state, url); diff --git a/src/agent/opencode-msgids.ts b/src/agent/opencode-msgids.ts index 53e08311e6..acc02ecd6b 100644 --- a/src/agent/opencode-msgids.ts +++ b/src/agent/opencode-msgids.ts @@ -1,34 +1,46 @@ // #4491 host wiring (opencode): client msgIds for incremental-lane turns. // -// opencode 1.x fires `experimental.chat.messages.transform` per LLM request -// with `{ info, parts }[]` — info.id is the host message id (msg_ + 12 hex + -// 14 base62; zero reuse observed across 378K messages, and a -// streaming-settled message re-fires under the SAME id with grown content — -// edit semantics the server-side history reconciles, so the host side only -// ever stores the LATEST fire per session). That hook is the ONLY opencode -// seam carrying host ids: the V2 `http.request` interception sees the final -// wire body and nothing else, so V2-shaped turns never carry msgIds — they -// degrade to the lane's content-derived identities by design (#4491 host -// wiring audit; opencode is the weakest of the four host seams). +// Two opencode host seams carry the host's own message ids (msg_ + base62 +// tail, zero reuse observed across 378K messages; a streaming-settled +// message re-fires under the SAME id with grown content — edit semantics +// the server-side history reconciles, so each harvester only ever stores +// the LATEST fire per session): +// • V1 (`opencode` ≤1.x): `experimental.chat.messages.transform` fires +// per LLM request with `{ info, parts }[]`. +// • V2 (`opencode` ≥2.0): `ctx.session.hook("context")` fires right +// before every provider request assembly with the full conversation +// list `{ id, role, content }[]` (roles user/assistant/tool) plus +// sessionID — verified live on 2.0.4, anthropic AND openai-compatible +// providers alike. (The V2 `http.request` seam itself sees only the +// final wire body; ids reach the planner through this harvest.) // // Alignment is proof, not guesswork (宁可降级不可错位): ids ride only when -// every harvested message provably lowers 1:1 onto the wire — text-only -// parts, role user|assistant, and the derived text sequence exactly equal to -// the observed wire messages (cache_control and other block props ignored). -// Any deviation — tool/reasoning/file parts, ignored or synthetic text, a -// missing or foreign-shaped id, a content drift, duplicate ids — drops the -// whole field and the turn rides exactly as before. Order-preserving lowering -// plus per-position equality is what makes the positional pairing safe; a -// compensating merge+split that still byte-matches every position can only -// swap ids between byte-identical content, which the ledger joins as one. +// every harvested message provably lowers onto the wire 1:1 — same role +// (host `tool` normalizes to wire `user`) and an exactly-equal ordered +// token sequence, where each token fingerprints one semantic part: +// t␀<text> text blocks (byte-compared, cache_control ignored) +// c␀<id>␀<name> assistant tool call ↔ wire tool_use (anchored by the +// provider-unique tool-use id, not input bytes — the +// id IS the identity; input lowering fidelity varies) +// r␀<id>␀<err> tool result ↔ wire tool_result (same anchor) +// o:r reasoning part ↔ thinking/redacted_thinking block +// o:m media part ↔ image/document block +// Host bookkeeping parts that never reach the wire (step markers, +// snapshots, patches) are skipped on the host side; ANY other unrecognized +// part type, a missing or foreign-shaped id, a count/role/token mismatch, +// or duplicate ids drops the WHOLE field and the turn rides exactly as +// before (content-derived identities). Order-preserving lowering plus +// per-position equality makes the positional pairing safe; a compensating +// merge+split that still byte-matches every position can only swap ids +// between identical-fingerprint content, which the ledger joins as one. interface HarvestedEntry { /** Host message id; "" when unusable (missing/foreign-shaped) — poisons * alignment for any list containing it, never the lane itself. */ id: string; - /** Derivable wire shape ({role, ordered text-part texts}), or null when + /** Derivable wire shape ({role, ordered part tokens}), or null when * this message cannot be proven to lower 1:1 onto one wire message. */ - shape: { role: "user" | "assistant"; texts: string[] } | null; + shape: { role: "user" | "assistant"; toks: string[] } | null; } const MAX_SESSIONS = 256; @@ -39,27 +51,97 @@ const harvests = new Map<string, { at: number; entries: HarvestedEntry[] }>(); * and control characters, the kernel's acp_summary_ namespace, empty ids). */ const HOST_MSG_ID = /^msg_[A-Za-z0-9]+$/; -function deriveEntry(raw: unknown): HarvestedEntry { - const holder = raw as { info?: unknown; parts?: unknown } | null | undefined; - const info = holder !== null && typeof holder === "object" ? holder.info : undefined; - const parts = holder !== null && typeof holder === "object" ? holder.parts : undefined; - const infoObj = info !== null && typeof info === "object" ? (info as { id?: unknown; role?: unknown }) : undefined; - const id = typeof infoObj?.id === "string" && HOST_MSG_ID.test(infoObj.id) ? infoObj.id : ""; - if (infoObj?.role !== "user" && infoObj?.role !== "assistant") return { id, shape: null }; +/** Host bookkeeping parts that produce no wire block — present in the host + * store (and in these events) but rendered client-side only. */ +const NOISE_PART_TYPES = new Set(["step-start", "step-finish", "snapshot", "patch"]); + +const T = "\u0000"; // token separator — cannot appear in well-formed JSON text + +function str(v: unknown): string | undefined { + return typeof v === "string" ? v : undefined; +} + +function obj(v: unknown): Record<string, unknown> | undefined { + return v !== null && typeof v === "object" ? (v as Record<string, unknown>) : undefined; +} + +/** Tokenize ONE host part (V1 `{ parts }` and V2 `{ content }` share the + * relevant fields). Returns the token, `false` for known-noise parts, or + * `null` when the part type cannot be proven to lower onto the wire. */ +function hostPartToken(part: unknown, hostRole: string): string | false | null { + const p = obj(part); + if (p === undefined) return null; + const type = str(p.type); + switch (type) { + case "text": { + if (p.ignored === true || p.synthetic === true) return null; // never reaches the model + const text = str(p.text); + return text === undefined ? null : `t${T}${text}`; + } + case "reasoning": + return "o:r"; + case "file": + case "image": + return "o:m"; + // V2 ai-sdk style tool parts. + case "tool-call": { + const id = str(p.id); + const name = str(p.name); + if (id === undefined || name === undefined) return null; + return `c${T}${id}${T}${name}`; + } + case "tool-result": { + const id = str(p.id); + if (id === undefined) return null; + const result = obj(p.result); + const isErr = result?.type === "error"; + return `r${T}${id}${T}${isErr ? "1" : "0"}`; + } + // V1 legacy tool part: lives on the assistant (call) or user/tool + // (settled result) side — anchored by callID either way. + case "tool": { + const id = str(p.callID) ?? str(p.id); + if (id === undefined) return null; + if (hostRole === "assistant") { + const name = str(p.tool) ?? ""; + return `c${T}${id}${T}${name}`; + } + const state = obj(p.state); + const isErr = state?.status === "error"; + return `r${T}${id}${T}${isErr ? "1" : "0"}`; + } + default: + return type !== undefined && NOISE_PART_TYPES.has(type) ? false : null; + } +} + +/** Normalize one harvested host message (either seam's element shape) to + * { id, shape }. Never throws — unparseable elements poison their own + * session's alignment only. */ +export function deriveOpencodeEntry(raw: unknown): HarvestedEntry { + const holder = obj(raw); + // V1: { info: { id, role }, parts: [...] }; V2 context: { id, role, content: [...] }. + const info = holder === undefined ? undefined : obj(holder.info); + const idSrc = holder === undefined ? undefined : str(holder.id) ?? str(info?.id); + const id = idSrc !== undefined && HOST_MSG_ID.test(idSrc) ? idSrc : ""; + const roleRaw = info?.role ?? holder?.role; + const parts = holder?.parts ?? holder?.content; if (!Array.isArray(parts)) return { id, shape: null }; - const texts: string[] = []; + // Host `tool` messages lower onto wire `user` tool_result blocks. + const normRole = roleRaw === "user" || roleRaw === "assistant" ? roleRaw : roleRaw === "tool" ? "user" : undefined; + if (normRole === undefined) return { id, shape: null }; + const toks: string[] = []; for (const part of parts) { - const p = part !== null && typeof part === "object" ? (part as { type?: unknown; text?: unknown; ignored?: unknown; synthetic?: unknown }) : undefined; - // ignored text never reaches the model (host-render notices) and - // synthetic text has no proven lowering — both stay underivable. - if (p === undefined || p.type !== "text" || p.ignored === true || p.synthetic === true || typeof p.text !== "string") return { id, shape: null }; - texts.push(p.text); + const tok = hostPartToken(part, String(roleRaw)); + if (tok === null) return { id, shape: null }; + if (tok !== false) toks.push(tok); } - return { id, shape: { role: infoObj.role, texts } }; + return { id, shape: { role: normRole, toks } }; } -/** Record the latest messages.transform fire for one session (V1 hook). The - * host's outgoing messages are left untouched — harvesting never mutates. */ +/** Record the latest id-bearing fire for one session (V1 messages.transform + * or V2 "context"). The host's outgoing messages are left untouched — + * harvesting never mutates. */ export function harvestOpencodeMsgIds(sid: string | undefined, messages: unknown): void { if (typeof sid !== "string" || sid.length === 0 || !Array.isArray(messages)) return; if (!harvests.has(sid) && harvests.size >= MAX_SESSIONS) { @@ -70,27 +152,58 @@ export function harvestOpencodeMsgIds(sid: string | undefined, messages: unknown } if (oldestKey !== undefined) harvests.delete(oldestKey); } - harvests.set(sid, { at: Date.now(), entries: messages.map(deriveEntry) }); + harvests.set(sid, { at: Date.now(), entries: messages.map(deriveOpencodeEntry) }); +} + +/** Token for one ANTHROPIC-WIRE content block, or null when the block type + * cannot be matched against a host part. */ +function wireBlockToken(block: unknown): string | null { + const b = obj(block); + if (b === undefined) return null; + switch (b.type) { + case "text": { + const text = str(b.text); + return text === undefined ? null : `t${T}${text}`; + } + case "tool_use": { + const id = str(b.id); + const name = str(b.name); + return id === undefined || name === undefined ? null : `c${T}${id}${T}${name}`; + } + case "tool_result": { + const id = str(b.tool_use_id); + return id === undefined ? null : `r${T}${id}${T}${b.is_error === true ? "1" : "0"}`; + } + case "thinking": + case "redacted_thinking": + return "o:r"; + case "image": + case "document": + return "o:m"; + default: + // server_tool_use / tool_reference / anything newer: no proven + // host counterpart — degrade this turn rather than misalign. + return null; + } } -/** Wire text sequence of one wire message: string content → [text]; block - * array → text blocks' text in order (cache_control et al. ignored). Any - * non-text block (tool_use, tool_result, image, thinking…) → null. */ -function wireTexts(content: unknown): string[] | null { - if (typeof content === "string") return [content]; +/** Token sequence of one ANTHROPIC-WIRE message, or null on any unmatchable + * block. */ +function wireContentToks(content: unknown): string[] | null { + if (typeof content === "string") return [`t${T}${content}`]; if (!Array.isArray(content)) return null; - const texts: string[] = []; + const toks: string[] = []; for (const block of content) { - const b = block !== null && typeof block === "object" ? (block as { type?: unknown; text?: unknown }) : undefined; - if (b === undefined || b.type !== "text" || typeof b.text !== "string") return null; - texts.push(b.text); + const tok = wireBlockToken(block); + if (tok === null) return null; + toks.push(tok); } - return texts; + return toks; } /** Client ids for the FULL wire message list of one planned turn, or * undefined when alignment cannot be proven (no harvest for the session, - * count mismatch, underivable/divergent entries, duplicate ids). The + * count mismatch, unalignable/divergent entries, duplicate ids). The * planner slices the result in lockstep with the delta messages. */ export function alignedMsgIdsForWire(sid: string, wire: unknown): string[] | undefined { const stored = harvests.get(sid); @@ -100,12 +213,12 @@ export function alignedMsgIdsForWire(sid: string, wire: unknown): string[] | und for (let i = 0; i < wire.length; i++) { const entry = stored.entries[i]; if (entry.id === "" || entry.shape === null) return undefined; - const msg = wire[i] as { role?: unknown; content?: unknown } | null | undefined; - if (msg === null || typeof msg !== "object" || msg.role !== entry.shape.role) return undefined; - const texts = wireTexts(msg.content); - if (texts === null || texts.length !== entry.shape.texts.length) return undefined; - for (let j = 0; j < texts.length; j++) { - if (texts[j] !== entry.shape.texts[j]) return undefined; + const msg = obj(wire[i]); + if (msg === undefined || msg.role !== entry.shape.role) return undefined; + const toks = wireContentToks(msg.content); + if (toks === null || toks.length !== entry.shape.toks.length) return undefined; + for (let j = 0; j < toks.length; j++) { + if (toks[j] !== entry.shape.toks[j]) return undefined; } if (seen.has(entry.id)) return undefined; seen.add(entry.id); diff --git a/src/agent/opencode-native.ts b/src/agent/opencode-native.ts index 02e4bcb910..6aae0006a3 100644 --- a/src/agent/opencode-native.ts +++ b/src/agent/opencode-native.ts @@ -711,9 +711,9 @@ function lastSessionFromMessages(messages: unknown): string | undefined { } /** #4491 host wiring: harvest host message ids per LLM request from the V1 - * messages.transform hook — the only opencode seam that sees them (V2's - * http.request sees just the wire, so V2-shaped turns degrade to - * content-derived identities). Never mutates the host's messages. */ + * messages.transform hook (V2 hosts instead feed the same store from the + * "context" seam registered in createOpencodeV2Setup). Never mutates the + * host's messages. */ const harvestMsgIdsTransform = async (input: unknown, output: { messages?: unknown }): Promise<void> => { harvestOpencodeMsgIds(lastSessionFromMessages(output?.messages), output?.messages); // #5403/#5418 unified watermark channel: feed the ONE chokepoint emitter diff --git a/src/agent/opencode-v2.ts b/src/agent/opencode-v2.ts index 2d109ae15f..e8e4f5a1ec 100644 --- a/src/agent/opencode-v2.ts +++ b/src/agent/opencode-v2.ts @@ -53,6 +53,7 @@ import { ACP_TOOLS_OPENAI, ABSORB_TOOL_OPENAI } from "../compress-tool.js"; import { stampedMsgIdsHeaderFromMessages } from "./opencode-v2-msgids.js"; import { asciiHeaderValue, fetchManifest, fetchProxyVersion, fetchStatus, fitNoticeDescription, forwardTool, postIdentityRegister, proxyBaseFromEnv, proxyBaseFromUrl, reportCompactionBoundary, reportRuntimeInfoOnChange, V2_SYNTHETIC_TEXT } from "./shared.js"; import { createForkAdopter } from "./fork-adopt.js"; +import { harvestOpencodeMsgIds } from "./opencode-msgids.js"; import { logHostEvent } from "./host-log.js"; // OpenCode V2 TUI renders a synthetic message as a visible Notice row only when its display text fits the @@ -427,7 +428,6 @@ export function createOpencodeV2Setup(options: OpencodeV2SetupOptions = {}): (ct // above, same event, so there is deliberately NO separate `context` // hook here (that seam fired pre-request in older builds and could // race the wire; reading this event can't). - if (options.onResponse !== undefined) { const onResponse = options.onResponse; try { diff --git a/src/agent/pi-native.ts b/src/agent/pi-native.ts index 5a74613f78..e6a38bf818 100644 --- a/src/agent/pi-native.ts +++ b/src/agent/pi-native.ts @@ -29,7 +29,7 @@ import os from "node:os"; import path from "node:path"; import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST, SPAWN_BUDGET_MS } from "../launcher.js"; import { piPackageStrings } from "../plugin-install.js"; -import { createBiliPlugin, createPiDirectSendGuard } from "./pi.js"; +import { createBiliPlugin, createPiDirectSendGuard, piDeclaredWindowForDirect } from "./pi.js"; import { isLegacyBcpEntry, markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScriptPath, proxyEnvOrigin, setNativeOriginWaiter, singleFlight } from "./native-bootstrap.js"; import { loadConfigFile } from "../config.js"; import { getInlineEngine, INLINE_ORIGIN } from "../inline-engine.js"; @@ -144,6 +144,13 @@ const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve( // sniffs the outgoing envelope and restores the stashed original before any // direct-send fall-back reaches the real upstream. state.prepareDirectSend = createPiDirectSendGuard(); +// #4894: arm the shared oversize arm for the degraded direct path. pi stamps +// x-bili-plugin-context-window only on proxy-bound requests (and only when the +// proxy base resolves), so the not-ready path — the one that egresses the raw +// history uncompressed — arrives with no header. Without this the guard fell +// open and a multi-million-token history rode straight to the provider, whose +// 400 comes back as an unrecoverable "input exceeds the context window". +state.declaredWindowForDirect = piDeclaredWindowForDirect; async function bootstrap(): Promise<string | undefined> { try { @@ -361,6 +368,11 @@ export function _stateRespawnForTest(): (() => Promise<string | undefined>) | un return state.respawn; } +/** Test hook (#4894): the armed direct-send window reader, if any. */ +export function _declaredWindowHookForTest(): (() => number | undefined) | undefined { + return state.declaredWindowForDirect; +} + /** Test hook (#1365): record routed-channel evidence without the fetch patch. */ export function _noteRoutedForTest(url: string): void { noteRoutedOrigin(state, url); diff --git a/src/agent/pi.ts b/src/agent/pi.ts index 83792937a7..66cb84fadc 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -860,6 +860,32 @@ export function __stashPiIncrementalOriginalForTests(conversationId: string, pay stashPiIncrementalOriginal(conversationId, payload); } +// #4894 (pi lane): the declared context window of the session's CURRENT model, +// recorded on every before_provider_headers event. The shared oversize arm in +// directSendOrRefuse (native-intercept.ts) prefers the stamped +// x-bili-plugin-context-window header — but this lane stamps that header only +// on the proxy branch, so a request that finds NO live proxy (respawn failed / +// spawn budget missed) carried none and the arm stayed disarmed in exactly the +// state where an uncompressed egress is likeliest to blow the window: the raw +// history then rode straight to the provider and came back as "input exceeds +// the context window", unrecoverable while the proxy stayed down. Recorded +// BEFORE the proxy-readiness early return below for the same reason. +// `undefined` means UNKNOWN (no ctx.model / no window configured) and the arm +// then fails open rather than guessing a window for this request. +let piDeclaredWindow: number | undefined; + +/** #4894: window reader the native intercept arms as declaredWindowForDirect + * (see pi-native.ts). Process-scoped: pi dispatches model requests + * sequentially per session and subagents run in their own processes. */ +export function piDeclaredWindowForDirect(): number | undefined { + return piDeclaredWindow; +} + +/** Test hook: forget the recorded window (fixtures share one process). */ +export function _resetPiDeclaredWindowForTest(): void { + piDeclaredWindow = undefined; +} + /** #3304: prepareDirectSend for the pi/omp extension lanes (pi-native arms * it on its NativeInterceptState). A body that IS an incremental envelope * was swapped by maybeIncrementalTurn → restore the stashed original @@ -1308,6 +1334,12 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM }); pi.on("before_provider_headers", async (event, ctx) => { try { + // #4894: record this request's window FIRST — the + // proxy-readiness return right below is precisely the + // degraded direct-send path the guard measures against, so + // the window must not depend on the proxy being up. + const liveWindow = ctx.model?.contextWindow; + piDeclaredWindow = typeof liveWindow === "number" && Number.isFinite(liveWindow) && liveWindow > 0 ? Math.floor(liveWindow) : undefined; // #1243: on the native lane the proxy origin lands via an async // bootstrap that writes BILLION_CONTEXT_PROXY only after the spawn; // a one-shot (-p) fires this event exactly once, inside that diff --git a/src/degenerate-turn.ts b/src/degenerate-turn.ts index 2092f58f32..346f7ee7c4 100644 --- a/src/degenerate-turn.ts +++ b/src/degenerate-turn.ts @@ -72,6 +72,41 @@ export function endsWithDraftClose(text: string): boolean { return text.length > 0 && DRAFT_CLOSE_TAIL.test(text); } +// #3905: the announcement-tail shape. Unlike #2303 there is no draft tag: the +// ENTIRE visible output of the turn is a short promise of the next action +// ("Running verify after formatting:") that ends on a colon or ellipsis, with +// no tool call and a clean stop. The upstream reports a healthy turn, the host +// gets nothing actionable, and the session stalls until the user types +// something. Observed verbatim at 24 output tokens / 32 visible chars behind a +// deepseek-v4.1-flash combo member. +// +// Three precision boundaries — each one denies a real answer class that the +// raw tail shape alone would misread as a stall (a false positive burns the +// one-shot BILLED retry on a turn that was already complete): +// 1. length cap: a genuine answer that happens to end on a colon is prose, +// and prose is long. 400 chars sits an order of magnitude above the +// observed stub and well below any real answer tail. +// 2. action-intent cue: a bare colon/ellipsis appears in legitimate short +// answers ("The answer is 42:", "I'm not sure…"). Only progressive-form +// action verbs and first-person intent phrases count as cues — the forms +// the production stubs actually use. The list is English-first by design: +// non-English announcements keep pre-#3905 behavior (no retry) until +// traffic evidence justifies more languages. +// 3. closed code fence: an UNCLOSED fence means the output was cut inside a +// code block — the truncated-output defect class (#1755/#2190), not a +// stalled promise; retrying it as an announcement would double-bill a +// length cut. +const ANNOUNCE_TAIL = /(?::|…|\.\.\.)\s*$/; +const ANNOUNCE_MAX_CHARS = 400; +const ANNOUNCE_CUE = /\b(?:let me|i(?:'ll| will)|going to|gonna|running|checking|testing|inspecting|examining|looking|applying|adding|removing|updating|fixing|building|installing|scanning|verifying|formatting|cleaning|writing|reading|searching|starting|committing|pushing|deploying|restarting|compiling|linting|patching)\b/i; +export function endsWithAnnouncement(text: string): boolean { + const t = text.trimEnd(); + if (t.length === 0 || t.length > ANNOUNCE_MAX_CHARS) return false; + if ((t.match(/```/g)?.length ?? 0) % 2 === 1) return false; + if (!ANNOUNCE_TAIL.test(t)) return false; + return ANNOUNCE_CUE.test(t); +} + // #2612: the #2303 draft-tail shape has a legitimate producer — the CLIENT's // own summarization request. Claude Code's compaction (/compact, auto-compact, // precomputed compression) instructs the model, in the final user message, to diff --git a/src/dsh-channel.ts b/src/dsh-channel.ts index 9427c5cb0c..633b147b2a 100644 --- a/src/dsh-channel.ts +++ b/src/dsh-channel.ts @@ -435,7 +435,7 @@ export function resolveDshBinary( * launcher.ts import, which would cycle through plugin-install). */ function quoteWinToken(token: string): string { if (!/\s/.test(token) || token.includes('"')) return token; - return `"${token}"`; + return `"${token.replace(/(\\+)$/, "$1$1")}"`; } /** #679: which spawn form `dsh` needs. On Windows only .cmd/.bat shims and diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index 2475a0c657..880be7e944 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -1362,7 +1362,7 @@ export function planReconciliation( // byTwin / byNorm / byTurn / byImage) run BELOW Pass 0, so by the time // this tier sees a gap cell every exact channel has already had its // bite — what remains is tiered corroboration, in order of evidence - // strength: + // strength. User-role messages stay on the exact passes above: // (a) simhash: a re-rendered cell (formatting churn normalization // still misses — key reordering beyond canonical-Json's reach, // template re-stamps) keeps its word-shingle mass, so a 64-bit @@ -1394,6 +1394,8 @@ export function planReconciliation( const anchor = anchors[gap.oldId]; const message = byId.get(gap.newId); if (anchor === undefined || message === undefined) continue; + // Changed user instructions must reach the model, even between unchanged neighbors. + if (anchor.r === "user") continue; if (anchor.r !== undefined && anchor.r !== message.role) continue; const len = message.text?.length ?? 0; if (Math.abs(len - anchor.b) > Math.max(256, anchor.b >> 2)) continue; @@ -1439,7 +1441,9 @@ export function planReconciliation( // the words still on the wire. Doctrine (order carries, content gates, // the id is only a hint): when a MISSING MASSES of cells still carry // word evidence, pair them in order instead of diagnosing destruction. - // prose anchors: wordbag equality + simhash ≤ GAP bar (the same + // text user anchors: exact normalizedIdentity after newline→space unwrap + // (line wrap that reconstructs the stored identity; never wordbag/simhash); + // other prose anchors: wordbag equality + simhash ≤ GAP bar (the same // edit-honesty gate as the sim tier) — role-checked, length-guarded; // textless anchors (image families, no sh/wh by construction): k-th // family pairing, only when the anchor/candidate counts per @@ -1450,10 +1454,15 @@ export function planReconciliation( const residualMiddle = missingMiddle.filter((id) => !plan.claims.has(id)); if (residualMiddle.length >= SHAPE_MASS_MIN) { const looseProse = (anchor: FoldAnchor, m: CoreMessage): boolean => { - if (anchor.wh === undefined || anchor.sh === undefined) return false; if (anchor.r !== undefined && anchor.r !== m.role) return false; const len = m.text?.length ?? 0; if (Math.abs(len - anchor.b) > Math.max(256, anchor.b >> 2)) return false; + if (anchor.r === "user") { + if (anchor.c !== "text" || m.contentType !== "text") return false; + const unwrapped: CoreMessage = { ...m, text: (m.text ?? "").replace(/\r?\n/g, " ") }; + return normalizedIdentity(unwrapped) === anchor.n; + } + if (anchor.wh === undefined || anchor.sh === undefined) return false; const wh = wordBagDigest(canonicalTextOf(m.text ?? "")); if (wh !== anchor.wh) return false; const dist = hammingHex(anchor.sh, simhashHex(canonicalTextOf(m.text ?? ""))); @@ -2305,14 +2314,21 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt for (const id of block.directMessageIds ?? []) deadIds.add(id); } for (const id of deadIds) delete nextAnchors[id]; + const storedPositions = session.metadata[METADATA_POSITIONS] as { ids?: string[]; canon?: string[] } | undefined; + const positionIds = storedPositions?.ids; + const positionCanon = storedPositions?.canon; for (let i = nextOrder.length - 1; i >= 0; i--) { - if (deadIds.has(nextOrder[i])) nextOrder.splice(i, 1); + if (deadIds.has(nextOrder[i])) { + nextOrder.splice(i, 1); + if (positionIds === nextOrder && Array.isArray(positionCanon)) positionCanon.splice(i, 1); + } } - const storedPositions = session.metadata[METADATA_POSITIONS] as { ids?: string[] } | undefined; - const positionIds = storedPositions?.ids; if (Array.isArray(positionIds) && positionIds !== nextOrder) { for (let i = positionIds.length - 1; i >= 0; i--) { - if (deadIds.has(positionIds[i])) positionIds.splice(i, 1); + if (deadIds.has(positionIds[i])) { + positionIds.splice(i, 1); + if (Array.isArray(positionCanon)) positionCanon.splice(i, 1); + } } } recordConflict(session, "orphan-reap", `reaped ${reapedCount} zombie fold block(s) [${reaped.join(", ")}]: covered substrate majority-absent for ${FOLD_DRIFT_ESCALATE_PASSES} consecutive passes (#2695)`); diff --git a/src/generation-boundary.ts b/src/generation-boundary.ts index 330d82f001..55f67cbee9 100644 --- a/src/generation-boundary.ts +++ b/src/generation-boundary.ts @@ -20,13 +20,16 @@ // renders for the session's lifetime — static text, byte-stable across turns, // prefix-cache safe after the (typically never-visible) one-time append. // -// Deliberately display-only: no storage rewrite, no ref-space surgery, no gate -// changes. The text carries no numbers: the frontier advances as the session -// allocates, and a fixed ceiling would go stale within turns. Concrete ceilings -// stay with the reactive notes (beyondFrontierNote / frontierClampNote), which -// fire on the actual failure and always quote the live value. +// The note itself stays display-only and numberless: the frontier advances as +// the session allocates, and a fixed ceiling would go stale within turns — +// concrete ceilings stay with the reactive notes (beyondFrontierNote / +// frontierClampNote), which fire on the actual failure and always quote the live +// value. Detection, however, now also ARMS the native-compaction rebase: proving +// the ledger belongs to a previous generation is exactly the evidence that rebase +// needed, and annotating it without repairing it leaves compress rejecting every +// live ref for the rest of the session (#6061, #5589). -import { markDirty, type Session } from "./session.js"; +import { markDirty, markNativeCompactionBoundary, type Session } from "./session.js"; import type { CoreMessage } from "acp-kernel"; /** Session-metadata key for the one-shot detection record. */ @@ -127,6 +130,14 @@ export function detectGenerationBoundary( }; (session.metadata ?? (session.metadata = {}))[STORAGE_KEY] = record; markDirty(session); + // #6061/#5589: this verdict is also the proof the native-compaction rebase has + // always required — the persisted ref ledger provably belongs to the previous + // generation. Arm it here so the next request runs resetSessionCompression + + // reserveRefsThrough and re-anchors the map onto the generation the client is + // actually minting. Without this the note only warns the model away from refs + // that compress would otherwise reject for the rest of the session (nudge and + // tool disagreeing about "highest allocated ref" is the same defect). + markNativeCompactionBoundary(session); log?.("info", `[${session.id}] [acp-gen-boundary] ${stale.size} distinct ref citation(s) above this session's frontier (${record.frontier || "no mapped refs yet"}) found in visible history — previous-generation residue confirmed; standing generation-boundary note active (highest stale ${record.maxStaleRef}) (#4305)`); return true; } diff --git a/src/incremental-lane.ts b/src/incremental-lane.ts index a233e6f5b9..22ea84aa4c 100644 --- a/src/incremental-lane.ts +++ b/src/incremental-lane.ts @@ -561,6 +561,38 @@ function hashWireBoundary(m: { role: string; content: unknown }): string { return createHash("sha256").update(`${m.role}\u0000${JSON.stringify(m.content)}`).digest("hex").slice(0, 16); } +const CONTENT_ID = /^(h_[0-9a-f]{16})(?:_([1-9]\d*))?$/; + +/** Continue separately converted content-ID clusters without rewriting any + * payload. A sparse restored prefix must skip its highest retained ordinal. */ +export function continueContentClusters( + prefix: readonly BiliMessage[], + delta: { msgs: BiliMessage[]; cacheControls: Map<string, unknown> }, +): { msgs: BiliMessage[]; cacheControls: Map<string, unknown> } { + const offsets = new Map<string, number>(); + for (const message of prefix) { + const hit = CONTENT_ID.exec(message.id); + if (!hit) continue; + const ordinal = Number(hit[2] ?? 0); + if (Number.isSafeInteger(ordinal + 1)) offsets.set(hit[1], Math.max(offsets.get(hit[1]) ?? 0, ordinal + 1)); + } + if (offsets.size === 0) return delta; + const renamed = new Map<string, string>(); + const msgs = delta.msgs.map((message) => { + const hit = CONTENT_ID.exec(message.id); + if (!hit) return message; + const offset = offsets.get(hit[1]) ?? 0; + if (offset === 0) return message; + const ordinal = offset + Number(hit[2] ?? 0); + if (!Number.isSafeInteger(ordinal)) throw new Error("Content identity cluster exceeds safe ordinal range"); + const id = `${hit[1]}_${ordinal}`; + renamed.set(message.id, id); + return { ...message, id }; + }); + if (renamed.size === 0) return delta; + return { msgs, cacheControls: new Map([...delta.cacheControls].map(([id, mark]) => [renamed.get(id) ?? id, mark])) }; +} + function applyTurn( entry: Canonical, sinceSeq: number, @@ -615,7 +647,11 @@ function applyTurn( retouchMarks.push({ ids, marks, rawBlocks }); } } - const flat = deltaWire.length === 0 ? { msgs: [], cacheControls: new Map<string, unknown>() } : anthropicToCore({ messages: deltaWire as never } as never); + const converted = deltaWire.length === 0 ? { msgs: [], cacheControls: new Map<string, unknown>() } : anthropicToCore({ messages: deltaWire as never } as never); + // A new conversion starts each duplicate cluster at zero. Continue the + // acknowledged prefix so a repeated prompt cannot reuse an earlier row's + // identity (and receive the new row's cache mark as a side effect). + const flat = resync ? converted : continueContentClusters(entry.msgs, converted); let deltaMsgs = flat.msgs; if (deltaIds !== undefined) { const v = validateMsgIds(deltaIds, flat.msgs.length); diff --git a/src/launcher.ts b/src/launcher.ts index 285a07a6c6..664f9c2950 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -61,7 +61,7 @@ import { stateDir } from "./paths.js"; import { TWIN_ADOPT_EXIT_CODE } from "./listen-policy.js"; import { tcpConnectable } from "./liveness.js"; import { selfPackageRoot, isBiliPiEntry, ompPluginLoadedFrom, dshNativeInstalled, claudeNativeInstalled, piPackageStrings } from "./plugin-install.js"; -import { applyOmpFirstEventTimeout, envMillis } from "./agent/native-bootstrap.js"; +import { applyOmpFirstEventTimeout, applyOmpMessageThreadsOff, envMillis } from "./agent/native-bootstrap.js"; import { logHostEvent } from "./agent/host-log.js"; import { compactionDisablePatchRows } from "./agent/dsh-compaction-disable.js"; import { log as teeLog, logTimestamp } from "./logger.js"; @@ -6503,7 +6503,10 @@ async function abortLaunchOnBusyOverlay(handle: ProxyHandle, err: OverlayBusyErr * because such tokens never reach this path anymore. */ export function quoteWinToken(token: string): string { if (!/\s/.test(token) || token.includes('"')) return token; - return `"${token}"`; + // The receiving program reads 2n backslashes before a quote as n (MSVCRT / + // CommandLineToArgvW), so a trailing run is doubled or the closing quote halves it + // (an even run) or swallows itself (an odd one). + return `"${token.replace(/(\\+)$/, "$1$1")}"`; } /** #679: full command line for `comspec /d /s /c <line>` — tokens quoted as @@ -7147,6 +7150,10 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} // hold the first SSE event well past OMP's default 300s first-parsed-event // watchdog — export a wider first-event budget unless the user pinned one. applyOmpFirstEventTimeout(env); + // #6088: OMP >= 18.18 message threads send only the newest message per + // turn, so bili would never see (or compress) the conversation — turn + // them off unless the user pinned a value. + applyOmpMessageThreadsOff(env); const ompExt = selfDistFile("agent/omp.js"); if (ompExt && fs.existsSync(ompExt) && !ompPluginLoadedFrom(ompRealHome)) { clientArgs = ["-e", ompExt, ...clientArgs]; diff --git a/src/loop/adapter-google.ts b/src/loop/adapter-google.ts index 21b77f82b9..bb45909cf9 100644 --- a/src/loop/adapter-google.ts +++ b/src/loop/adapter-google.ts @@ -20,6 +20,7 @@ const PROXY_TOOL_SET = new Set([ // here or their chunks raw-replay to the client as "real" calls. acp_cache // had the same gap before this change. "acp_retire", "acp_restore", "acp_rewrite", "acp_cache", + "acp_retrieve", "acp_rule", "acp_read_original_image", "bili_compress", "bili_decompress", "bili_search_context", "bili_status", ]); @@ -101,35 +102,53 @@ interface FunctionCallBuffer { } // A decision per buffered chunk that carried functionCall parts: "keep" = every -// call in it is real (replay verbatim, preserving signatures/ids/order), null = -// only proxy calls (drop — the proxy executes them server-side), or a rewritten -// copy keeping the chunk's other parts plus its real calls. +// call in it is real and needs no signature promotion (replay verbatim, +// preserving signatures/ids/order), null = only proxy calls (drop — the proxy +// executes them server-side), or a rewritten copy keeping the chunk's other +// parts plus its real calls (promoting a stripped proxy call's thoughtSignature +// onto the first surviving real functionCall part when Gemini placed the turn +// signature on the leading proxy call). function filterRealCallChunk( parsed: Record<string, unknown>, realIndexes: Set<number>, callIndexes: number[], + promoteSig?: { callIndex: number; signature: string }, ): "keep" | null | Record<string, unknown> { const candidates = parsed.candidates as Array<Record<string, unknown>> | undefined; const parts = (candidates?.[0]?.content as { parts?: GooglePart[] } | undefined)?.parts; if (!Array.isArray(parts)) return "keep"; let anyReal = false; let anyProxy = false; - const keep: boolean[] = []; + let sigPromoted = false; + const outParts: GooglePart[] = []; let call = 0; for (const part of parts) { if (part && typeof part === "object" && part.functionCall) { - const real = realIndexes.has(callIndexes[call] ?? -1); + const callIdx = callIndexes[call] ?? -1; + const real = realIndexes.has(callIdx); call += 1; - keep.push(real); - if (real) anyReal = true; - else anyProxy = true; + if (real) { + anyReal = true; + if ( + promoteSig && + callIdx === promoteSig.callIndex && + (typeof part.thoughtSignature !== "string" || part.thoughtSignature.length === 0) + ) { + outParts.push({ ...part, thoughtSignature: promoteSig.signature }); + sigPromoted = true; + } else { + outParts.push(part); + } + } else { + anyProxy = true; + } } else { - keep.push(true); + outParts.push(part); } } - if (!anyProxy) return "keep"; + if (!anyProxy && !sigPromoted) return "keep"; if (!anyReal) return null; - return cloneChunk(parsed, { parts: parts.filter((_, i) => keep[i]) }); + return cloneChunk(parsed, { parts: outParts }); } function dataLine(frame: string): string | null { @@ -440,21 +459,43 @@ export function createGoogleAdapter( } return; } + // Gemini attaches thoughtSignature only to the FIRST functionCall + // part in a parallel-call turn. When a stripped proxy call (e.g. + // compress) led the turn and carried that signature, the first + // surviving real functionCall part becomes the turn's leading call + // on client replay and must inherit the signature or Gemini 3/4 + // rejects the next turn with 400 INVALID_ARGUMENT (missing + // thought_signature). + let firstRealIdx: number | undefined; + let firstProxySig = ""; + for (const [idx, tc] of pending) { + if (realIndexes.has(idx)) { + if (firstRealIdx === undefined && tc.name.length > 0) firstRealIdx = idx; + } else if (firstProxySig.length === 0 && tc.signature.length > 0) { + firstProxySig = tc.signature; + } + } + const firstRealTc = firstRealIdx !== undefined ? pending.get(firstRealIdx) : undefined; + const promoteSig = + firstRealIdx !== undefined && firstRealTc && firstRealTc.signature.length === 0 && firstProxySig.length > 0 + ? { callIndex: firstRealIdx, signature: firstProxySig } + : undefined; for (const [idx, tc] of pending) { if (realIndexes.has(idx) && tc.name.length > 0) { toolCallsEmitted++; + const sig = tc.signature || (promoteSig?.callIndex === idx ? promoteSig.signature : ""); yield { kind: "tool_call", name: tc.name, callId: tc.id, arguments: tc.args, passthrough: true, - ...(tc.signature ? { signature: tc.signature } : {}), + ...(sig ? { signature: sig } : {}), } as ParsedStreamEvent; } } for (const { json, parsed, callIndexes } of rawCallChunks) { - const filtered = filterRealCallChunk(parsed, realIndexes, callIndexes); + const filtered = filterRealCallChunk(parsed, realIndexes, callIndexes, promoteSig); if (filtered === "keep") { yield { kind: "meta", chunk: Buffer.from(`data: ${json}\n\n`, "utf8") } as ParsedStreamEvent; } else if (filtered !== null) { diff --git a/src/loop/adapter-openai.ts b/src/loop/adapter-openai.ts index 282910787b..29f8654e44 100644 --- a/src/loop/adapter-openai.ts +++ b/src/loop/adapter-openai.ts @@ -163,11 +163,14 @@ function stripFinishReasonChunk(buf: Buffer): Buffer { // standard field is prompt_tokens_details.cached_tokens, while DeepSeek reports // KV-cache hits as top-level prompt_cache_hit_tokens (#779). Both mean "input // tokens served from cache", so normalize to one number. -function openaiCachedTokens(u: Record<string, unknown>): number | undefined { +function openaiCachedTokens(u: Record<string, unknown>): number | null { const pd = u.prompt_tokens_details as Record<string, unknown> | undefined; + if (typeof u.prompt_tokens === "number" + && typeof pd?.cached_tokens !== "number" + && typeof u.prompt_cache_hit_tokens !== "number") return 0; if (typeof pd?.cached_tokens === "number") return pd.cached_tokens; if (typeof u.prompt_cache_hit_tokens === "number") return u.prompt_cache_hit_tokens; - return undefined; + return null; } export function createOpenaiAdapter(requestBody: Record<string, unknown>, clientSystem?: string, absorbName?: string, notes?: string[], errorShape: "protocol" | "completion" = "protocol"): CompressLoopAdapter { diff --git a/src/loop/core.ts b/src/loop/core.ts index b896377feb..5b4cba8c65 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -48,7 +48,7 @@ import { isStrictReasoningEcho, modelIdOf, normalizeStrictEchoBody } from "../st import { log as loggerLog } from "../logger.js"; import { promptInputTotal, type WireProtocol } from "../util.js"; import { DEGENERATE_RETRY_NUDGE } from "../degenerate-retry.js"; -import { endsWithDraftClose, lastUserSummaryInstruction, requestExpectsProseSummary } from "../degenerate-turn.js"; +import { endsWithAnnouncement, endsWithDraftClose, lastUserSummaryInstruction, requestExpectsProseSummary } from "../degenerate-turn.js"; import { safePrefix, safeSuffix, scrubLoneSurrogateEscapesOnJson } from "../text-safe.js"; export const MAX_LOOP_ROUNDS = 10; @@ -188,6 +188,13 @@ function isReasoningRequiredError(e: unknown): boolean { return e instanceof UpstreamHttpError && /must be passed back/i.test(e.body); } +function proxyToolResultFailed(result: string): boolean { + // A successful compression receipt embeds the model-authored summary in later fingerprint + // lines. That summary may legitimately quote an earlier FAILED tool result; only the status + // line is authoritative for whether this tool call itself failed. + return (result.trimStart().split("\n", 1)[0] ?? "").includes("FAILED"); +} + // #1453 client-facing labels for transport failures that end the turn in-band. // Deliberately kind-level only: raw error text can embed endpoint hostnames/IPs, // which must never reach the client stream (the full masked chain goes to the @@ -1268,6 +1275,18 @@ export async function* runCompressLoop( } } + // #2303 / #3905: classify the terminal tail shape. Both shapes are + // non-converged handoffs — visible prose with no tool call — and share + // the budget, the openai/anthropic framing boundary and the #2612 + // exemption below. + const stalledTail: "draft" | "announce" | null = + assistantText.length === 0 || calls.length > 0 + ? null + : endsWithDraftClose(assistantText) + ? "draft" + : endsWithAnnouncement(assistantText) + ? "announce" + : null; // #2303: a terminal turn that DID deliver visible prose but ends with a // compression-draft closing tag (summary/analysis close forms) and no // tool call — the model wrote a handoff/compression draft in prose @@ -1293,7 +1312,7 @@ export async function* runCompressLoop( !ctx.textProtocol && assistantText.length > 0 && calls.length === 0 && - endsWithDraftClose(assistantText) && + (stalledTail === "draft" || stalledTail === "announce") && // #2612: the client's own compaction request instructs the // model to answer with a plain-text summary block and no // tool call — that compliant reply is a draft tail by @@ -1303,8 +1322,13 @@ export async function* runCompressLoop( !signal?.aborted ) { degenerateRetried = true; - ctx.log(`[acp-loop] round ${round}: terminal turn ends with a compression-draft closing tag and no tool call; retrying once with continuation nudge (#2303)`); - loggerLog("warn", `[acp-loop] draft-tail auto-retry round ${round} (session ${ctx.session.id})`); + if (stalledTail === "announce") { + ctx.log(`[acp-loop] round ${round}: terminal turn is a bare announcement (ends mid-promise, no tool call); retrying once with continuation nudge (#3905)`); + loggerLog("warn", `[acp-loop] announcement-tail auto-retry round ${round} (session ${ctx.session.id})`); + } else { + ctx.log(`[acp-loop] round ${round}: terminal turn ends with a compression-draft closing tag and no tool call; retrying once with continuation nudge (#2303)`); + loggerLog("warn", `[acp-loop] draft-tail auto-retry round ${round} (session ${ctx.session.id})`); + } const nudge: CoreMessage = { id: `acp_draft_tail_retry_r${round}`, role: "user", @@ -1324,11 +1348,11 @@ export async function* runCompressLoop( continue; } catch (e) { if (e instanceof UpstreamHttpError) { - ctx.log(`[acp-loop] round ${round}: draft-tail retry failed (upstream error ${e.status}: ${e.body.slice(0, 200)}); passing the turn through`); + ctx.log(`[acp-loop] round ${round}: stalled-tail retry failed (upstream error ${e.status}: ${e.body.slice(0, 200)}); passing the turn through`); } else { - ctx.log(`[acp-loop] round ${round}: draft-tail retry failed (${e instanceof Error ? e.message : String(e)}); passing the turn through`); + ctx.log(`[acp-loop] round ${round}: stalled-tail retry failed (${e instanceof Error ? e.message : String(e)}); passing the turn through`); } - loggerLog("warn", `[acp-loop] draft-tail auto-retry failed round ${round}: ${e instanceof Error ? e.message : String(e)}`); + loggerLog("warn", `[acp-loop] stalled-tail auto-retry failed round ${round}: ${e instanceof Error ? e.message : String(e)}`); } } break; @@ -1529,8 +1553,13 @@ export async function* runCompressLoop( coreMessages.push(redactedMsg); } } + const roundProxySignature = + ctx.protocol === "google" + ? proxyResults.find((p) => typeof p.signature === "string" && p.signature.length > 0)?.signature + : undefined; for (const pr of proxyResults) { if (functionCallIds.has(pr.callId)) { + const sig = pr.signature || roundProxySignature; coreMessages.push({ id: `acp_loop_r${round}_asst_tc_${pr.callId}`, role: "assistant", @@ -1539,7 +1568,7 @@ export async function* runCompressLoop( toolCallId: pr.callId, // #3661: guaranteed-valid-JSON replay (see wireArguments); raw model bytes stay in pr.arguments for #156 signatures. text: pr.wireArguments, - ...(ctx.protocol === "google" && pr.signature ? { googleThoughtSignature: pr.signature } : {}), + ...(ctx.protocol === "google" && sig ? { googleThoughtSignature: sig } : {}), }); const toolResultMsg: CoreMessage = { id: `acp_loop_r${round}_tool_${pr.callId}`, @@ -1601,7 +1630,7 @@ export async function* runCompressLoop( if (threadContinue) threadDelta.push(injection); } const anyCompressFailed = proxyResults.some( - (pr) => (pr.name === "compress" || pr.name === "decompress") && pr.result.includes("FAILED"), + (pr) => (pr.name === "compress" || pr.name === "decompress") && proxyToolResultFailed(pr.result), ); if (!ctx.textProtocol && !anyCompressFailed) { const hidden = hideConsumedCompressCalls(ctx.session.state, coreMessages, ctx.config.isCompressTool); @@ -1622,7 +1651,7 @@ export async function* runCompressLoop( const absorbName = activeAbsorbToolName(ctx.session, ctx.config); if ( ctx.refreshFolded && - proxyResults.some((pr) => (pr.name === "compress" || pr.name === absorbName) && !pr.result.includes("FAILED")) + proxyResults.some((pr) => (pr.name === "compress" || pr.name === absorbName) && !proxyToolResultFailed(pr.result)) ) { try { const refreshed = await ctx.refreshFolded(coreMessages); @@ -1712,7 +1741,7 @@ export async function* runCompressLoop( // carry server-invisible bytes upstream — exactly the seam #4186 // forbids. const failedMutating = proxyResults.filter( - (pr) => (pr.name === "compress" || pr.name === "decompress") && pr.result.includes("FAILED"), + (pr) => (pr.name === "compress" || pr.name === "decompress") && proxyToolResultFailed(pr.result), ); if (!probeWarned) { @@ -1830,14 +1859,14 @@ export async function* runCompressLoop( let committedTokens = 0; let committedBlocks = 0; for (const pr of proxyResults) { - if (pr.name === "compress" && !pr.result.includes("FAILED")) { + if (pr.name === "compress" && !proxyToolResultFailed(pr.result)) { const saved = /~(\d[\d,]*) tokens saved/.exec(pr.result); if (saved) committedTokens += Number(saved[1]!.replace(/,/g, "")); const blocks = /(\d+) block\(s\)/.exec(pr.result); if (blocks) committedBlocks += Number(blocks[1]); } } - const absorbed = proxyResults.some((pr) => pr.name === activeAbsorbToolName(ctx.session, ctx.config) && !pr.result.includes("FAILED")); + const absorbed = proxyResults.some((pr) => pr.name === activeAbsorbToolName(ctx.session, ctx.config) && !proxyToolResultFailed(pr.result)); const note = committedTokens > 0 ? `compression committed (~${committedTokens.toLocaleString("en-US")} tokens saved${committedBlocks > 0 ? ` in ${committedBlocks} block(s)` : ""}); ` : absorbed ? "context work committed; " : ""; diff --git a/src/plugin.ts b/src/plugin.ts index a4824a39d0..9e1d913741 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -36,6 +36,9 @@ import { captureHandoffJson, HANDOFF_REPLY_MAX_BYTES } from "./server/handoff-re export interface UpstreamMeta { status?: number; contentType?: string; + requestId?: string; + opencodeLogId?: string; + cfRay?: string; } /** #2328 Q4: does the dangling partial event left at a cut carry the opening @@ -59,7 +62,7 @@ function tailLooksTerminal(tail: string, protocol: WireProtocol | "responses"): return /event: response\.(?:completed|failed|incomplete)/.test(tail.slice(0, 64)) || /"type"\s*:\s*"response\.(?:completed|failed|incomplete)"/.test(tail); } -import { degenerateTurnWarning, endsWithDraftClose, requestExpectsProseSummary } from "./degenerate-turn.js"; +import { degenerateTurnWarning, endsWithAnnouncement, endsWithDraftClose, requestExpectsProseSummary } from "./degenerate-turn.js"; import { getChangelog, latestChangelog, warmChangelogOnce } from "./web/changelog.js"; import { warmBlogRefreshOnce } from "./web/blog-refresh.js"; import { PANEL_BOX_FOOTER } from "./acp-panel.js"; @@ -528,10 +531,30 @@ export function recordPluginSession(conversationId: string, sessionId: string): scheduleSaveConversations(); } +interface PluginWireMetadata { + wireHash: string; + textComparable: boolean; +} + +/** Capture the raw request's snapshot evidence before prepare mutates its + * parsed view. Only these scalars need to survive forwarding. */ +export function preparePluginWireMetadata(rawWire: Buffer, parsedWire: unknown): PluginWireMetadata { + let textComparable = false; + try { + if (parsedWire !== null && typeof parsedWire === "object") { + const wire = parsedWire as Record<string, unknown>; + textComparable = comparableHistory(wire.messages ?? wire.input ?? wire.contents); + } + } catch { + textComparable = false; + } + return { wireHash: createHash("sha256").update(rawWire).digest("hex"), textComparable }; +} + /** Keep the last prepare()'s view for a plugin session so tool-API execution * sees the exact refs the model was shown (mirrors the wire-mode loop, which * runs executeProxyTool against prepared.processedMessages). */ -export function rememberPluginMessages(sessionId: string, processed: CoreMessage[], original: CoreMessage[], nudge?: NudgeDecision, rawWire?: Buffer): void { +export function rememberPluginMessages(sessionId: string, processed: CoreMessage[], original: CoreMessage[], nudge?: NudgeDecision, rawWire?: Buffer | PluginWireMetadata): void { // #1307: auxiliary requests (auto-review / classifier prompts) bound to the // same session key can carry a normal output budget and any message count, // so both the ≤200 heuristic and size-based guards are proxies that a new @@ -652,17 +675,22 @@ export function rememberPluginMessages(sessionId: string, processed: CoreMessage snapshotRefsEvaluatedStore.set(session, session.contentStore); } if (rawWire !== undefined) { - // The comparable verdict is a property of the wire body: hash it - // (native speed) and only re-parse + walk when the body actually - // changed, so a retry with an identical body stays cheap. - const wireHash = createHash("sha256").update(rawWire).digest("hex"); + // The handler captures this evidence before forwarding so it need + // not retain the raw body. Buffer callers keep the hash-gated parse. + const wireHash = Buffer.isBuffer(rawWire) + ? createHash("sha256").update(rawWire).digest("hex") + : rawWire.wireHash; if (session.metadata.publicSnapshotWireHash !== wireHash) { let comparable: boolean; - try { - const wire = JSON.parse(rawWire.toString("utf8")) as Record<string, unknown>; - comparable = comparableHistory(wire.messages ?? wire.input ?? wire.contents); - } catch { - comparable = false; + if (Buffer.isBuffer(rawWire)) { + try { + const wire = JSON.parse(rawWire.toString("utf8")) as Record<string, unknown>; + comparable = comparableHistory(wire.messages ?? wire.input ?? wire.contents); + } catch { + comparable = false; + } + } else { + comparable = rawWire.textComparable; } session.metadata.publicSnapshotWireHash = wireHash; if (session.metadata.publicSnapshotTextComparable !== comparable) { @@ -735,13 +763,35 @@ export function pluginRuntimeInfoFor(agent: string | undefined, model: string | return entry; } +/** Clients mint child conversation ids for work that runs on the parent's own + * route+model: omp opens ephemeral turns as `<session>:side:<id>` and + * subagents as `<session>:sub:<id>` (with `:side:conversation:<key>` also + * seen in the wild), and never registers runtime-info under them — the + * window belongs to route+model+tier, so a child resolves up to the nearest + * ancestor that reported one instead of falling to the registry/#1321 cap. */ +const CHILD_CONVERSATION_SUFFIX = /:(?:side(?::conversation)?|sub):[^:]+$/; +const MAX_CHILD_CONVERSATION_DEPTH = 4; + +function parentConversationId(conversationId: string): string | undefined { + const parent = conversationId.replace(CHILD_CONVERSATION_SUFFIX, ""); + return parent !== conversationId && parent.length > 0 ? parent : undefined; +} + /** Latest runtime-info registered under a conversation id, usable for - * `model` only (#1531). */ + * `model` only (#1531). A client-minted child id resolves to its nearest + * reporting ancestor; the model check is applied at whichever level answers, + * so a child on a different model never inherits a stale window. */ export function pluginRuntimeInfoForConversation(conversationId: string | undefined, model: string | undefined): PluginRuntimeInfo | undefined { if (conversationId === undefined || model === undefined) return undefined; - const entry = pluginRuntimeByConversation.get(conversationId); - if (entry === undefined || entry.model !== model) return undefined; - return entry; + const exact = pluginRuntimeByConversation.get(conversationId); + if (exact !== undefined) return exact.model === model ? exact : undefined; + let parent = parentConversationId(conversationId); + for (let depth = 0; parent !== undefined && depth < MAX_CHILD_CONVERSATION_DEPTH; depth++) { + const entry = pluginRuntimeByConversation.get(parent); + if (entry !== undefined) return entry.model === model ? entry : undefined; + parent = parentConversationId(parent); + } + return undefined; } /** The narrow conversation signal a header-less runtime-info lookup can @@ -2732,6 +2782,9 @@ export async function pipePluginChatWithStrip( // default); hosts that need the legacy completed-turn shape opt back in with // streamErrorShape:"completion". streamErrorShape?: "protocol" | "completion", + // #5951: the requesting host already classifies this cut and retries on its + // own budget — the proxy must not spend a second one. + hostOwnsTruncationRecovery = false, ): Promise<void> { let reader = stream.getReader(); let decoder = new TextDecoder("utf-8"); @@ -2920,6 +2973,17 @@ export async function pipePluginChatWithStrip( // answer — a client compaction asking for a plain-text <summary> block // must not be retried (its compliant reply IS a draft tail). const draftTail = visibleTextChars > 0 && !sawToolUse && endsWithDraftClose(proseAcc) && !requestExpectsProseSummary(requestText); + // #3905: the same non-convergence without a draft tag — the whole visible + // output is a one-line promise of the next action ("Running verify after + // formatting:"). Without this branch the answerChars gate below would read + // that promise as a delivered answer and refuse the retry. + // The detector reads ANSWER prose, not proseAcc: proseAcc also carries + // the thinking/reasoning channel, and a thought that ends mid-sentence on + // ":" or "…" is internal monologue, not an announcement — feeding it here + // reclassified #2756's whitespace-only degenerate stops as announcements + // and swallowed their refusal/retry logs. + const announceTail = !draftTail && visibleTextChars > 0 && !sawToolUse && endsWithAnnouncement(answerProse) && !requestExpectsProseSummary(requestText); + const stalledTail = draftTail || announceTail; // #2756: the gate asks "did the host receive ANSWER text?" — prose that // is nothing but whitespace (a bare "\n\n" from a degenerate stop, the // B-accident shape) is not an answer; the empty-turn retry must fire for @@ -2927,9 +2991,9 @@ export async function pipePluginChatWithStrip( // chars are already excluded here), and real text keeps refusing the // retry byte-for-byte, so a delivered answer is never duplicated. const answerChars = answerProse.trim().length; - if (!draftTail && (answerChars > 0 || sawToolUse)) return false; + if (!stalledTail && (answerChars > 0 || sawToolUse)) return false; if (reason === undefined || !CLEAN_TURN_REASONS.has(reason)) { - if (!sawToolUse && !draftTail && answerChars === 0) log?.(`[plugin] degenerate terminal turn not retried (finish ${reason ?? "none"} is not a clean stop): visibleTextChars=${visibleTextChars}, releasedMarkupChars=${releasedMarkupChars} (#2756)`); + if (!sawToolUse && !stalledTail && answerChars === 0) log?.(`[plugin] degenerate terminal turn not retried (finish ${reason ?? "none"} is not a clean stop): visibleTextChars=${visibleTextChars}, releasedMarkupChars=${releasedMarkupChars} (#2756)`); return false; } if (res.destroyed || res.writableEnded) return false; @@ -2955,14 +3019,16 @@ export async function pipePluginChatWithStrip( // re-issuing it double-bills an empty completion (#732/#821 keep the // same boundary in the compress loop). A draft-tail turn is NOT bare: // it delivered a full handoff draft, which is precisely the stall signal. - if (!sawThinking && !sawStrippedEcho && releasedMarkupChars === 0 && !draftTail) { + if (!sawThinking && !sawStrippedEcho && releasedMarkupChars === 0 && !stalledTail) { log?.(`[plugin] degenerate terminal turn not retried (bare: no thinking, no stripped echo, no released markup): visibleTextChars=${visibleTextChars}, releasedMarkupChars=${releasedMarkupChars} (#2756)`); return false; } degenerateRetried = true; - log?.(draftTail - ? "[plugin] terminal turn ends with a compression-draft closing tag and no tool call; retrying once with a continuation nudge (#2303)" - : "[plugin] degenerate terminal turn (no usable output); retrying once with a continuation nudge (#732/#821)"); + log?.(announceTail + ? "[plugin] terminal turn is a bare announcement (ends mid-promise, no tool call); retrying once with a continuation nudge (#3905)" + : draftTail + ? "[plugin] terminal turn ends with a compression-draft closing tag and no tool call; retrying once with a continuation nudge (#2303)" + : "[plugin] degenerate terminal turn (no usable output); retrying once with a continuation nudge (#732/#821)"); let next: ReadableStream<Uint8Array> | null = null; try { next = await refetch(); @@ -3066,7 +3132,11 @@ export async function pipePluginChatWithStrip( // it too, so the two content-only counters are what separate an answer from // a thinking fragment. if (sawToolUse || blocksForwarded > 0 || visibleTextChars > 0 || releasedMarkupChars > 0 || - sawStrippedEcho || answerProse.trim().length > 0 || streams.size > 0) return false; + sawStrippedEcho || answerProse.trim().length > 0) return false; + // OpenAI reasoning identity filters are not pending answer content. + for (const { field } of streams.values()) { + if (protocol !== "openai" || (field !== "reasoning_content" && field !== "reasoning")) return false; + } if (res.destroyed || res.writableEnded) return false; let next: ReadableStream<Uint8Array> | null = null; try { @@ -3115,10 +3185,27 @@ export async function pipePluginChatWithStrip( // #5951: the read error itself, so a cut that was not replayed names its // cause instead of only its classification. ...(cause === "read-error" && readError !== undefined ? { readError: describeReadError(readError) } : {}), + ...(cause === "read-error" && readError !== undefined ? { readErrorCause: describeReadErrorCause(readError) } : {}), retryZeroByteCutSpent: truncationRetried, retryDegenerateSpent: degenerateRetried, + // #5951: the replay predicate's inputs, so a refused cut names the clause + // that refused it instead of only the read error. + replay: { + replayAvailable: refetch !== undefined, + sawThinking, + sawToolUse, + blocksForwarded, + openStreams: streams.size, + forwardedAny, + releasedMarkupChars, + proseChars: proseAcc.length, + answerProseChars: answerProse.trim().length, + }, ...(upstreamMeta?.status !== undefined ? { upstreamStatus: upstreamMeta.status } : {}), ...(upstreamMeta?.contentType !== undefined ? { upstreamContentType: upstreamMeta.contentType } : {}), + ...(safeDiagnosticId(upstreamMeta?.requestId) ? { upstreamRequestId: safeDiagnosticId(upstreamMeta?.requestId) } : {}), + ...(safeDiagnosticId(upstreamMeta?.opencodeLogId) ? { upstreamLogId: safeDiagnosticId(upstreamMeta?.opencodeLogId) } : {}), + ...(safeDiagnosticId(upstreamMeta?.cfRay) ? { upstreamCfRay: safeDiagnosticId(upstreamMeta?.cfRay) } : {}), }); /** While the retry stream feeds the client, the message the FIRST attempt * opened is still open: nothing may re-open it. Returns true when the event @@ -3758,14 +3845,14 @@ export async function pipePluginChatWithStrip( try { step = await reader.read(); } catch (e) { - if (await retryReasoningOnlyCut(e)) continue; + if (!hostOwnsTruncationRecovery && await retryReasoningOnlyCut(e)) continue; throw e; } const { done, value } = step; if (done) { // #2171: an EOF with nothing client-visible yet is safely // re-issuable — try the one-shot retry before giving up. - if (!sawTerminal && !res.destroyed && !res.writableEnded && ((await retryZeroByteCut()) || (await retryReasoningOnlyCut()))) continue; + if (!hostOwnsTruncationRecovery && !sawTerminal && !res.destroyed && !res.writableEnded && ((await retryZeroByteCut()) || (await retryReasoningOnlyCut()))) continue; // #2323: a CRLF/lone-CR whose final byte arrived last is held back // by the streaming normalizer; resolve it and re-drive the completed // event through the same path below before deciding truncation. @@ -3909,7 +3996,11 @@ export async function pipePluginChatWithStrip( maybeWarnNamelessToolCalls(); maybeSummarizeStrips(); settleWitnesses(); - if (sawTerminal && !sawReplyError && !res.destroyed && !res.writableEnded) reply?.save(); + // #4247-followup: persistence answers "did the upstream turn finish?", + // never "is the client socket still open?". pi's client hangs up the + // moment it holds the terminal frame, so gating this save on client + // liveness dropped every pi reply from the handoff render. + if (sawTerminal && !sawReplyError) reply?.save(); if (truncated) { reply?.discard(); // #721 → #2563: the client still owns the stream from here. @@ -3929,6 +4020,9 @@ export async function pipePluginChatWithStrip( log?.("client aborted mid-stream"); // #5293: the partial output of an aborted turn never reports usage — record the pre-abort anchor so the first nudge decision after resume can absorb that debt. if (session) noteAbortedTurn(session); + // A terminal already arrived before the client left: the reply is + // complete, so it still belongs in the handoff snapshot. + if (sawTerminal && !sawReplyError) reply?.save(); return; } // #721: upstream read failed while the client is still connected — @@ -4115,6 +4209,25 @@ function describeReadError(error: unknown): string { return `${name}: ${error.message}`.replace(/[\r\n]+/g, " ").slice(0, 200); } +/** Retain only bounded, structured cause identity; never serialize upstream text. */ +function describeReadErrorCause(error: unknown): Array<{ name: string; code?: string }> | undefined { + if (!(error instanceof Error)) return undefined; + const chain: Array<{ name: string; code?: string }> = []; + let cause: unknown = error.cause; + for (let depth = 0; depth < 3 && cause instanceof Error; depth++) { + const name = /^[A-Za-z][A-Za-z0-9_.-]{0,63}$/.test(cause.name) ? cause.name : "Error"; + const codeValue = (cause as Error & { code?: unknown }).code; + const code = typeof codeValue === "string" && /^[A-Za-z0-9_]{1,64}$/.test(codeValue) ? codeValue : undefined; + chain.push({ name, ...(code ? { code } : {}) }); + cause = cause.cause; + } + return chain.length > 0 ? chain : undefined; +} + +function safeDiagnosticId(value: string | undefined): string | undefined { + return value !== undefined && value.length <= 128 && /^[A-Za-z0-9][A-Za-z0-9._:-]*$/.test(value) ? value : undefined; +} + function hadTextOtherThanTextFields(choices: unknown): boolean { if (!Array.isArray(choices)) return true; for (const c of choices) { @@ -4236,6 +4349,7 @@ export async function pipePluginResponsesWithStrip( // #2328 Q2 — see pipePluginChatWithStrip. upstreamMeta?: UpstreamMeta, reply?: HandoffReply, + hostOwnsTruncationRecovery = false, ): Promise<void> { let reader = stream.getReader(); let decoder = new TextDecoder("utf-8"); @@ -4543,11 +4657,27 @@ export async function pipePluginResponsesWithStrip( // #5951: the read error itself, so a cut that was not replayed names its // cause instead of only its classification. ...(cause === "read-error" && readError !== undefined ? { readError: describeReadError(readError) } : {}), + ...(cause === "read-error" && readError !== undefined ? { readErrorCause: describeReadErrorCause(readError) } : {}), retryZeroByteCutSpent: truncationRetried, retryDegenerateSpent: degenerateRetried, + // #5951: see the chat-pipe twin — the responses lane has no reasoning-only + // replay, so `replayAvailable` is the field that says so on a cut. + replay: { + replayAvailable: refetch !== undefined, + sawReasoning, + sawFunctionCall, + openStreams: argStreams.size, + forwardedAny, + proseChars: proseAcc.length, + fastPathChars, + heldVisibleChars, + }, heldEvents: heldEvents.length, ...(upstreamMeta?.status !== undefined ? { upstreamStatus: upstreamMeta.status } : {}), ...(upstreamMeta?.contentType !== undefined ? { upstreamContentType: upstreamMeta.contentType } : {}), + ...(safeDiagnosticId(upstreamMeta?.requestId) ? { upstreamRequestId: safeDiagnosticId(upstreamMeta?.requestId) } : {}), + ...(safeDiagnosticId(upstreamMeta?.opencodeLogId) ? { upstreamLogId: safeDiagnosticId(upstreamMeta?.opencodeLogId) } : {}), + ...(safeDiagnosticId(upstreamMeta?.cfRay) ? { upstreamCfRay: safeDiagnosticId(upstreamMeta?.cfRay) } : {}), }); /** Every id the retry carries is rewritten onto the first attempt's, so the * client's assembled item stays the one it already holds. */ @@ -4586,6 +4716,10 @@ export async function pipePluginResponsesWithStrip( // #2612: except when the request itself demands that shape (client // compaction summary instruction) — see the chat-pipe twin. const draftTail = (visibleTextChars > 0 || heldVisibleChars > 0) && !sawFunctionCall && endsWithDraftClose(proseAcc) && !requestExpectsProseSummary(requestText); + // #3905: bare-announcement twin — see the chat-pipe twin above + // (including why the detector reads answerProse, not proseAcc). + const announceTail = !draftTail && (visibleTextChars > 0 || heldVisibleChars > 0) && !sawFunctionCall && endsWithAnnouncement(answerProse) && !requestExpectsProseSummary(requestText); + const stalledTail = draftTail || announceTail; // #2756: same question as the chat-pipe twin — "did the host receive // ANSWER text?". Reasoning inlined into item.content[] (vLLM/ // llama.cpp reasoning_text) and whitespace-only output_text (a bare @@ -4593,9 +4727,9 @@ export async function pipePluginResponsesWithStrip( // bare check below, but whitespace-only answer prose must also retry. // Real answer text still refuses the retry byte-for-byte. const answerChars = answerProse.trim().length; - if (!draftTail && (answerChars > 0 || sawFunctionCall)) return false; + if (!stalledTail && (answerChars > 0 || sawFunctionCall)) return false; if (status !== "completed") { - if (!sawFunctionCall && !draftTail && answerChars === 0) log?.(`[plugin] degenerate terminal turn not retried (status ${status ?? "none"} is not completed): visibleTextChars=${visibleTextChars}, heldVisibleChars=${heldVisibleChars} (#2756)`); + if (!sawFunctionCall && !stalledTail && answerChars === 0) log?.(`[plugin] degenerate terminal turn not retried (status ${status ?? "none"} is not completed): visibleTextChars=${visibleTextChars}, heldVisibleChars=${heldVisibleChars} (#2756)`); return false; } if (res.destroyed || res.writableEnded) return false; @@ -4604,14 +4738,16 @@ export async function pipePluginResponsesWithStrip( // double-bills an empty completion (#732/#821 keep the same boundary in // the compress loop). A draft-tail turn delivered a full handoff draft: // precisely the stall signal. - if (!sawReasoning && !sawStrippedEcho && !draftTail) { + if (!sawReasoning && !sawStrippedEcho && !stalledTail) { log?.(`[plugin] degenerate terminal turn not retried (bare: no reasoning, no stripped echo): visibleTextChars=${visibleTextChars}, heldVisibleChars=${heldVisibleChars} (#2756)`); return false; } degenerateRetried = true; - log?.(draftTail - ? "[plugin] terminal turn ends with a compression-draft closing tag and no function call; retrying once with a continuation nudge (#2303)" - : "[plugin] degenerate terminal turn (no visible output); retrying once with a continuation nudge (#732/#821)"); + log?.(announceTail + ? "[plugin] terminal turn is a bare announcement (ends mid-promise, no function call); retrying once with a continuation nudge (#3905)" + : draftTail + ? "[plugin] terminal turn ends with a compression-draft closing tag and no function call; retrying once with a continuation nudge (#2303)" + : "[plugin] degenerate terminal turn (no visible output); retrying once with a continuation nudge (#732/#821)"); let next: ReadableStream<Uint8Array> | null = null; try { next = await refetch(); @@ -4683,7 +4819,7 @@ export async function pipePluginResponsesWithStrip( if (done) { // #2171: an EOF with nothing client-visible yet is safely // re-issuable — try the one-shot retry before giving up. - if (!sawTerminal && !res.destroyed && !res.writableEnded && (await retryZeroByteCut())) continue; + if (!hostOwnsTruncationRecovery && !sawTerminal && !res.destroyed && !res.writableEnded && (await retryZeroByteCut())) continue; // #2323: a CRLF/lone-CR whose final byte arrived last is held back // by the streaming normalizer; resolve it and re-drive the completed // event through the same path below before deciding truncation. @@ -4947,7 +5083,8 @@ export async function pipePluginResponsesWithStrip( maybeSummarizeStrips(); settleUsage(); settleWitnesses(); - if (replyCompleted && !res.destroyed && !res.writableEnded) reply?.save(); + // Same rule as the chat pipe: upstream completion decides, not the socket. + if (replyCompleted) reply?.save(); // #721 → #2563: never leave the stream hanging on a missing done-family // event. Permitlist hosts (pi) get the raw cut for their native // classifier; everyone else gets the in-band error frame. @@ -4964,6 +5101,8 @@ export async function pipePluginResponsesWithStrip( log?.("client aborted mid-stream"); // #5293: same as the chat lane — record the pre-abort anchor for the post-resume re-anchor. if (session) noteAbortedTurn(session); + // Completed turn, dead socket: keep the reply for the handoff render. + if (replyCompleted) reply?.save(); return; } // #721: upstream read failed while the client is still connected — diff --git a/src/responses-ws.ts b/src/responses-ws.ts index b728054545..d1c5abddd1 100644 --- a/src/responses-ws.ts +++ b/src/responses-ws.ts @@ -1,3 +1,4 @@ +import { createHash } from "node:crypto"; import http from "node:http"; import type { ResponsesRequestBody } from "acp-kernel/wire"; import { WebSocket as UpstreamWebSocket, type Dispatcher } from "undici"; @@ -322,8 +323,14 @@ function bodyToText(body: RequestInit["body"]): string | undefined { return undefined; } +function cacheDiagnosticHash(text: string): string { + return createHash("sha256").update(text).digest("hex"); +} + export class ResponsesWsUpstream { private socket?: InstanceType<typeof UpstreamWebSocket>; + private connectionGeneration = 0; + private sendSequence = 0; private key?: string; private history = new ResponsesWsHistory(); private active?: { fail: (error: Error, reason?: string) => void }; @@ -373,6 +380,7 @@ export class ResponsesWsUpstream { this.log("debug", "upstream connecting phase=handshake"); const socket = new UpstreamWebSocket(wsUrl, { headers, dispatcher: options.dispatcher as Dispatcher | undefined }); this.socket = socket; + this.connectionGeneration++; this.key = key; await new Promise<void>((resolve, reject) => { const clean = (): void => { @@ -458,6 +466,9 @@ export class ResponsesWsUpstream { } if (this.active) throw new Error("Responses WebSocket exchange already active"); const request = this.history.continuation(body); + const connection = this.connectionGeneration; + const { input: _diagnosticInput, ...settings } = body; + const settingsHash = cacheDiagnosticHash(canonical({ ...settings, ...egressTransportFields })); this.log("debug", `upstream request mode=${request.previous_response_id === undefined ? "full" : "delta"}`); return new Promise<Response>((resolve, reject) => { const output = new ResponsesWsOutput(); @@ -466,12 +477,21 @@ export class ResponsesWsUpstream { let ended = false; let retryFull = request.previous_response_id !== undefined; let receivedEvents = 0; + let send = 0; const encoder = new TextEncoder(); // #3701: log the EXACT outbound frame bytes — the upstream gateway judges by // bytes, not tokens, so this number is what the ws-byte-shed ladder targets. const sendCreate = (payload: JsonObject): void => { const frameText = JSON.stringify({ ...payload, ...egressTransportFields, type: "response.create" }); this.log("info", `upstream send mode=${payload.previous_response_id === undefined ? "full" : "delta"} bytes=${Buffer.byteLength(frameText)}`); + send = ++this.sendSequence; + // Allowlisted fingerprints only: never log payloads, headers, or the connection key. + this.log("debug", `upstream cache-diagnostic ${JSON.stringify({ + phase: "send", connection, send, + mode: payload.previous_response_id === undefined ? "full" : "delta", + frame_sha256: cacheDiagnosticHash(frameText), settings_sha256: settingsHash, + previous_response_id_sha256: typeof payload.previous_response_id === "string" ? cacheDiagnosticHash(payload.previous_response_id) : null, + })}`); socket.send(frameText); }; // Backpressure (#2076): the WS socket cannot be paused, so frames arriving @@ -533,6 +553,21 @@ export class ResponsesWsUpstream { } if (!object(frame) || typeof frame.type !== "string") throw new Error("Invalid Responses WebSocket event"); receivedEvents++; + if (["response.completed", "response.failed", "response.incomplete", "error"].includes(frame.type)) { + const response = object(frame.response) ? frame.response : {}; + const usage = object(response.usage) ? response.usage : {}; + const details = object(usage.input_tokens_details) ? usage.input_tokens_details : {}; + const tokens = (value: unknown): number | null => typeof value === "number" && Number.isFinite(value) ? value : null; + this.log("debug", `upstream cache-diagnostic ${JSON.stringify({ + phase: "terminal", connection, send, event: frame.type, + response_id_sha256: typeof response.id === "string" ? cacheDiagnosticHash(response.id) : null, + usage_present: Object.hasOwn(response, "usage"), + input_tokens_details_present: Object.hasOwn(usage, "input_tokens_details"), + input_tokens_present: Object.hasOwn(usage, "input_tokens"), input_tokens: tokens(usage.input_tokens), + output_tokens_present: Object.hasOwn(usage, "output_tokens"), output_tokens: tokens(usage.output_tokens), + cached_tokens_present: Object.hasOwn(details, "cached_tokens"), cached_tokens: tokens(details.cached_tokens), + })}`); + } const completed = output.observe(frame); if (frame.type === "error" && !settled) { const error = object(frame.error) ? frame.error : {}; diff --git a/src/server.ts b/src/server.ts index f06ee81a2b..03912500e6 100644 --- a/src/server.ts +++ b/src/server.ts @@ -4811,20 +4811,48 @@ export function readBody(req: http.IncomingMessage): Promise<Buffer> { return new Promise((resolve, reject) => { const chunks: Buffer[] = []; let size = 0; - let aborted = false; - req.on("data", (c: Buffer) => { - if (aborted) return; + let settled = false; + const cleanup = () => { + settled = true; + chunks.length = 0; + req.off("data", onData); + req.off("end", onEnd); + req.off("error", onError); + req.off("close", onClose); + }; + const onError = (error: unknown) => { + if (settled) return; + cleanup(); + reject(error); + }; + const onData = (c: Buffer) => { + if (settled) return; size += c.length; - if (size > knobLimitsReceiveBytes()) { - aborted = true; + const limit = knobLimitsReceiveBytes(); + if (size > limit) { + onError(new BodyTooLargeError(limit)); req.destroy(); - reject(new BodyTooLargeError(knobLimitsReceiveBytes())); return; } chunks.push(c); - }); - req.on("end", () => { if (!aborted) resolve(Buffer.concat(chunks)); }); - req.on("error", (e) => { if (!aborted) reject(e); }); + }; + const onEnd = () => { + if (settled) return; + try { + const body = Buffer.concat(chunks); + cleanup(); + resolve(body); + } catch (error) { + onError(error); + } + }; + // IncomingMessage also closes after a normal end, whose handler has + // already settled and removed this premature-close fallback. + const onClose = () => onError(new Error("request closed before body completed")); + req.on("data", onData); + req.on("end", onEnd); + req.on("error", onError); + req.on("close", onClose); }); } @@ -4834,6 +4862,6 @@ function logMsg(opts: ProxyOptions, level: string, msg: string): void { } export { getUnrecognizedPathStats, logDumpFailure, logUnrecognizedPath } from "./server/observability.js"; -export { BILI_HOP_HEADER, parseLauncherModelWindows, anthropicBetaContextWindow, capRegistryWindowByStandard, expandedContextSuffixWindow } from "./server/context-window.js"; -export { BILI_TOOL_NAMES, isSideRequest, outputBudgetField, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools, _resetNoOutputCeilingWarningsForTest, _resetDshMidBandWarningsForTest, OUTPUT_RESTORE_ABSOLUTE_FLOOR, type OutputBudgetField } from "./server/side-request.js"; +export { BILI_HOP_HEADER, parseLauncherModelWindows, anthropicBetaContextWindow, anthropicBetaMessageThreads, capRegistryWindowByStandard, expandedContextSuffixWindow } from "./server/context-window.js"; +export { BILI_TOOL_NAMES, isSideRequest, isStructuredOutputCall, outputBudgetField, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools, _resetNoOutputCeilingWarningsForTest, _resetDshMidBandWarningsForTest, OUTPUT_RESTORE_ABSOLUTE_FLOOR, type OutputBudgetField } from "./server/side-request.js"; export { countSystemAndToolsTokens, estimateInputTokens, estimateWireOverhead, clampOutputBudget, emergencyNudge, projectThinkingMass, type ThinkingMassInput } from "./server/budget.js"; diff --git a/src/server/context-window.ts b/src/server/context-window.ts index e17fdfe1f3..7029e73653 100644 --- a/src/server/context-window.ts +++ b/src/server/context-window.ts @@ -129,6 +129,17 @@ export function anthropicBetaContextWindow(headers: Record<string, string | stri return best; } +/** #6088: true when an `anthropic-beta` header negotiates Anthropic message + * threads (e.g. `message-threads-2026-08-12`). Such requests carry only the + * newest message(s) — the upstream holds the history — so bili cannot see or + * compress the conversation. */ +export function anthropicBetaMessageThreads(headers: Record<string, string | string[] | undefined>): boolean { + const raw = headers["anthropic-beta"]; + if (raw === undefined) return false; + const list = Array.isArray(raw) ? raw.join(",") : raw; + return list.split(",").some((part) => part.trim().toLowerCase().startsWith("message-threads")); +} + // #1321: an [Nm]-suffixed model name (e.g. "claude-opus-5-5[1m]") is the // client's own declaration that THIS request runs on the expanded tier — the // same evidence class as the context-Nm beta header, carried in the model id diff --git a/src/server/handle.ts b/src/server/handle.ts index 0166ebde6b..2627eb8841 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -56,7 +56,7 @@ import { maybeAdoptForkBlocks, maybeAdoptResume, maybeAdoptDerivedFork } from ". import { executeThreadsReforge } from "./threads-lane.js"; import { resetThreadTailTracking } from "./thread-tracking.js"; import { resolveClaudeTranscriptLineage } from "../claude-transcript-lineage.js"; -import { consumePluginRegisterFor, pluginAgentHeader, pluginCompressionProxyHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginReportedContextWindow, pluginReportedMaxOutput, pluginReportedModel, pluginRequestAgentHeader, pluginRuntimeInfoFor, pluginRuntimeInfoForConversation, publicForkInputCheck, recordChainVerdict, recordPluginSession, rememberPluginMessages, resolveConversation, runtimeConversationId, takePendingPluginRegister } from "../plugin.js"; +import { consumePluginRegisterFor, pluginAgentHeader, pluginCompressionProxyHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginReportedContextWindow, pluginReportedMaxOutput, pluginReportedModel, pluginRequestAgentHeader, pluginRuntimeInfoFor, pluginRuntimeInfoForConversation, preparePluginWireMetadata, publicForkInputCheck, recordChainVerdict, recordPluginSession, rememberPluginMessages, resolveConversation, runtimeConversationId, takePendingPluginRegister } from "../plugin.js"; import { scheduleAffinityPersist } from "../affinity-persist.js"; import { evaluateChain, extractChainCarriers } from "../chain-checkpoint.js"; import { BILI_PASSTHROUGH_HEADER, BILI_PLUGIN_BYPASS_HEADER, isLoopbackAddress, normalizeUpstreamOrigin, provenCompletionReserve, reserveOutputHeadroom, resolveOutputHeadroomCap, shouldReserveOutputHeadroom, type WireProtocol } from "../util.js"; @@ -68,9 +68,9 @@ import { matchModelEndpoint } from "../model-endpoints.js"; import { autoModeForgeEnabled, dshAuthoredCompactionEnabled, imageRescaleEnabled, limitsDecodedBytes as knobLimitsDecodedBytes, limitsIngestBytes as knobLimitsIngestBytes, noInjectTool as knobNoInjectTool, pluginBypassOverride, rawDumpDir as knobRawDumpDir } from "../knobs.js"; import { buildForgeResponse, classifierForgeVariant, estimateCjkTokens, isAutoModeClassifier, requestText, summarizeAction } from "../auto-mode-forge.js"; import { extractConversationId, isV1BackgroundSideRequest, isV1InternalRequest, unwrapV1Internal } from "../v1internal.js"; -import { anthropicBetaContextWindow, BILI_HOP_HEADER, capRegistryWindowByStandard, expandedContextSuffixWindow, launcherContextWindow, launcherMaxOutput, noteUnresolvedModelSession, UNRESOLVED_MODEL_SESSION_THRESHOLD, windowSourceLogged } from "./context-window.js"; +import { anthropicBetaContextWindow, anthropicBetaMessageThreads, BILI_HOP_HEADER, capRegistryWindowByStandard, expandedContextSuffixWindow, launcherContextWindow, launcherMaxOutput, noteUnresolvedModelSession, UNRESOLVED_MODEL_SESSION_THRESHOLD, windowSourceLogged } from "./context-window.js"; import { NO_IDENTITY_MESSAGE, safeSessionId } from "./headers.js"; -import { demoteGate, hasLeakedBiliToolsOnly, isClaudeCodeTitleRequest, isDshAutoReviewRequest, isDshSingleShotSidecar, isDshTitleRequest, isServerToolUtilityCall, isSideRequest, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js"; +import { demoteGate, hasLeakedBiliToolsOnly, hasSingleShotBudget, isClaudeCodeTitleRequest, isDshAutoReviewRequest, isDshSingleShotSidecar, isDshTitleRequest, isServerToolUtilityCall, isSideRequest, isStructuredOutputCall, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js"; import { DSH_COMPACTION_SHAPE_MSGS, DSH_REFUSAL_STORM_LOG_EVERY, detectPassthroughDshCompactionCall, dshCompactionRefusal, dshPassthroughGuardStats, isDshCompactionCall } from "./dsh-compaction-guard.js"; import { buildDshCompactionForgeBody, renderDshAuthoredCheckpoint } from "./dsh-authored-compaction.js"; import { artifactSeedHit, detectAcpArtifacts } from "./chain-artifacts.js"; @@ -134,6 +134,12 @@ let bypassOverrideOffWarned = false; // truncated subviews never adopt weakly regardless of ratio. const WEAK_ADOPT_MIN_PREFIX_RATIO = 0.8; +// #6088: warn-once per (pre-fork) session when a request negotiates Anthropic +// message threads — every turn then carries only the newest message(s), so the +// session silently never compresses. Keyed by the base id so `|sub:` forks of +// the same conversation do not repeat the line. +const messageThreadsWarned = new Set<string>(); + // #2986: the derivedFromSessionId link is what keeps a kind child session // reachable along its parent chain after a restart — without it the shared // prefix is re-sent whole and over-window requests 503 (the #2985 failure). It @@ -684,6 +690,9 @@ async function handleRequest( parsed = null; } } + // Capture the raw-wire verdict before unwrapping or preparing can mutate + // parsed. Only these scalars are needed for the post-forward bookkeeping. + const rawWireMetadata = protocol ? preparePluginWireMetadata(bodyBuffer, parsed) : undefined; // #3129: CloudCode/Antigravity v1internal dialect — unwrap the outer envelope // so the pipeline sees a standard GoogleRequest, and decide identity. The // inner request.sessionId is a constant FNV-1a hash, so identity comes from @@ -738,6 +747,7 @@ async function handleRequest( // index builds (each one re-converts the full history wire→core). The // predicate over-approximates by design; see bodyMightCarryImages. const bodyMayCarryImages = protocol !== null && bodyText !== undefined ? bodyMightCarryImages(bodyText, protocol) : false; + bodyText = undefined; // #903: inbound message count for the per-request cost line — messages // (anthropic/openai) or input (responses); null when the body has neither. const inboundMsgs: number | null = (() => { @@ -1249,6 +1259,7 @@ async function handleRequest( // anchor before the first main turn and exile the real // conversation onto a persisted `|sub:<fp>` ghost. || (pluginAgentHeader(req.headers) === "dsh" && sideAgent !== "main" + && hasSingleShotBudget(parsed) && isDshSingleShotSidecar(protocol, parsed)) // #5135: the claude code title sidecar stays off the raw-key // anchor claim and off the kernel pipeline entirely. @@ -1657,6 +1668,13 @@ async function handleRequest( // embed the id manually. One statement, runs in this request's own // async context, so concurrent requests never cross-contaminate. enterSessionContext(sessionId); + if (protocol === "anthropic" && anthropicBetaMessageThreads(req.headers)) { + const baseId = sessionId.split("|sub:")[0]; + if (!messageThreadsWarned.has(baseId)) { + messageThreadsWarned.add(baseId); + loggerLog("warn", `[${baseId}] Anthropic message threads are on (anthropic-beta message-threads-*): the upstream holds the history and each request carries only the newest message(s), so bili cannot compress this session; for OMP set PI_ANTHROPIC_MESSAGE_THREADS=0 (#6088)`); + } + } // #1086/#1357: content-fallback chain observation, now that identity is known. // Artifacts + processed local state ⇒ self-produced: process normally. // Artifacts + NO local state ⇒ ADVISORY observation (#1357 Phase 1): the @@ -2390,6 +2408,14 @@ async function handleRequest( // not a main turn, and seeding outputBudgetHighWater from it would // poison the first starved restore (same class as the #1897 skip). const serverToolUtility = isServerToolUtilityCall(parsed); + // #5073: a toolless structured-output request (Hindsight fact extraction) + // is the same category — a host-internal utility call under the main + // session id. It rides the #388 side passthrough via isSideRequest, and + // must ALSO skip restoreOutputBudget here: its max_tokens (64000) sizes + // the extraction response, not a main turn, and seeding + // outputBudgetHighWater from it would poison the first starved restore + // (same class as the #1897/#2500 skip). + const structuredOutputUtility = isStructuredOutputCall(parsed); // #546: restore a client-shrunk output budget BEFORE the side gate so a // tool-carrying main request re-enters the pipeline at full budget (see // restoreOutputBudget for the starvation mechanism). #1665/#1840: the @@ -2401,7 +2427,7 @@ async function handleRequest( // outputBudgetHighWater from it would poison the first starved restore // (title requests arrive FIRST, at session start). Starved all-bili requests // never reach here demoted: stripLeakedBiliTools vetoes them per #546. - if (!demotedSide && !serverToolUtility) { + if (!demotedSide && !serverToolUtility && !structuredOutputUtility) { // #3438: the dsh host contract (pi-ai buildBaseOptions) stamps the model-declared // maxTokens on every main turn, so on this lane a below-ceiling tool-carrying // budget is a raw-history collapse, not intent — arm the mid-band rescue. @@ -2572,7 +2598,8 @@ async function handleRequest( // prompt-enhance-class calls ride the #388 passthrough (kernel // no-touch) instead of claiming the raw-key anchor; the sticky-bound leg // mirrors #4438 — the extension client can arrive unstamped. - const dshSingleShotSidecar = requestAgent !== "main" && isDshSingleShotSidecar(protocol, parsed) + const dshSingleShotSidecar = requestAgent !== "main" && hasSingleShotBudget(parsed) + && isDshSingleShotSidecar(protocol, parsed) && (pluginAgentHeader(req.headers) === "dsh" || (typeof session.metadata.pluginAgent === "string" && session.metadata.pluginAgent === "dsh")); // #5135: ccTitleSidecar joins the intent set so the title call rides @@ -2602,7 +2629,7 @@ async function handleRequest( // #2500 server-tool utility shape) diverts under a receipt. // #2170 measure 1: the decision itself is resolveSideLane() (pure, // truth-table-tested); demotedSide ⊆ lane==="side" by construction. - const sideLane = resolveSideLane({ countTokens, responsesCompact, protocol, stripApplied: demotedSide, sideIntent, requestAgent, sideLabel: serverToolUtility ? "server-tool utility call (#2500)" : dshTitleSidecar ? "dsh title-gen sidecar (#2503)" : dshReviewSidecar ? "dsh auto-review sidecar (#4438)" : dshSingleShotSidecar ? "dsh single-shot sidecar (#5656)" : ccTitleSidecar ? "claude-code title-gen sidecar (#5135)" : undefined }); + const sideLane = resolveSideLane({ countTokens, responsesCompact, protocol, stripApplied: demotedSide, sideIntent, requestAgent, sideLabel: serverToolUtility ? "server-tool utility call (#2500)" : structuredOutputUtility ? "structured-output utility call (#5073)" : dshTitleSidecar ? "dsh title-gen sidecar (#2503)" : dshReviewSidecar ? "dsh auto-review sidecar (#4438)" : dshSingleShotSidecar ? "dsh single-shot sidecar (#5656)" : ccTitleSidecar ? "claude-code title-gen sidecar (#5135)" : undefined }); if (sideLane.lane === "side") { // #554: the passthrough below skips EVERY input-side guard by design // (#388) — a full-history side request over the window is a @@ -2928,10 +2955,13 @@ async function handleRequest( // #5683: this round's merged three-level compress settings, captured // so the post-prepare gate stamp below can read the policy knobs. let preparedCs: CompressSettings | undefined; - const runPreparedPipeline = async ( + // #4753: `let` + `| undefined` so releaseRequestInput below can drop + // the closure (and with it its captured input-only state) once the + // upstream response boundary is reached. + let runPreparedPipeline: (( respondFailFast: boolean, overflowWindow?: number, - ): Promise<{ body: string | Buffer; prepared: Prepared | null } | null> => { + ) => Promise<{ body: string | Buffer; prepared: Prepared | null } | null>) | undefined = async (respondFailFast, overflowWindow) => { const runPrepareInner = async (): Promise<Prepared> => { // #1820: this prepare consumes one unit of post-rebuild anchor // validity (the last one deletes it — see session.ts). @@ -3361,7 +3391,7 @@ async function handleRequest( if (guardScheme !== undefined && !resignArmable && !guardResign.enabled) { clearSignedRefusal(guardScheme); } - const pendingForward = await withSessionLock(session, () => runPreparedPipeline(true)); + const pendingForward = await withSessionLock(session, () => runPreparedPipeline!(true)); if (pendingForward) { forwarded = true; // #1195: wire clients (omp/pi on the plain proxy path) treat an @@ -3376,23 +3406,39 @@ async function handleRequest( // original 400 through verbatim. const overflowRefold = pendingForward.prepared ? async (realWindow: number | undefined): Promise<{ body: string | Buffer; prepared: Prepared | null } | null> => { - const next = await withSessionLock(session, () => runPreparedPipeline(false, realWindow)); + const next = await withSessionLock(session, () => runPreparedPipeline!(false, realWindow)); if (!next || String(next.body) === String(pendingForward.body)) return null; pendingForward.body = next.body; pendingForward.prepared = next.prepared; return next; } : undefined; - await forward(req, res, opts, pendingForward.body, pendingForward.prepared, core, reqConfig, log, route, instanceId, affinity, overflowRefold, v1Meta, throttleModel, (outbound) => { - logRequestCost(log, session.id, inboundMsgs, inboundBytes, reqT0, outbound); - }); + const releaseRequestInput = () => { + // #4753: refolding is finished before streaming starts. Drop + // the input-only bindings even if forward's wrapper retains + // the callback; accepted outbound bodies still support + // retries. + bodyBuffer = Buffer.alloc(0); + parsed = null; + affinityMessagesCache = null; + runPreparedPipeline = undefined; + }; + try { + await forward(req, res, opts, pendingForward.body, pendingForward.prepared, core, reqConfig, log, route, instanceId, affinity, overflowRefold, v1Meta, throttleModel, (outbound) => { + logRequestCost(log, session.id, inboundMsgs, inboundBytes, reqT0, outbound); + }, releaseRequestInput); + } finally { + // Transport failures and locally forged replies can return + // before the upstream response boundary. + releaseRequestInput(); + } // Remember for ALL modes (not just plugin): wire clients (dsh, // hermes, unplug'd pi) read the same panel via /__bili/plugin/status // and need the nudge/breakdown sections too; locked so a racing // plugin tool call sees a consistent window. if (pendingForward.prepared) { const preparedToRemember = pendingForward.prepared; - await withSessionLock(session, () => rememberPluginMessages(sessionId, preparedToRemember.processedMessages, preparedToRemember.originalMessages, preparedToRemember.nudge, bodyBuffer)); + await withSessionLock(session, () => rememberPluginMessages(sessionId, preparedToRemember.processedMessages, preparedToRemember.originalMessages, preparedToRemember.nudge, rawWireMetadata)); } } } finally { diff --git a/src/server/headers.ts b/src/server/headers.ts index e4c25e2dab..dc896a81c0 100644 --- a/src/server/headers.ts +++ b/src/server/headers.ts @@ -1,3 +1,5 @@ +import { BILI_HOST_RECOVERY_OWNER_HEADER } from "../util.js"; + /** Session ids are client-provided verbatim (#286) — sanitize before using * one in a debug-dump FILENAME so a hostile value cannot escape the dir. */ export function safeSessionId(id: string | undefined): string { @@ -26,6 +28,8 @@ export const UPSTREAM_HOP_HEADERS = new Set([ // #1117: bili-internal passthrough marker (native fetch patch → proxy); // never meaningful to a real upstream. "x-bili-passthrough", + // OMP host owns recovery; consume this marker before provider egress. + BILI_HOST_RECOVERY_OWNER_HEADER, // #5418: watermark fastpath handshake — client→proxy watermark claim and // proxy→client measured total. Pure billing-plane bookkeeping; the A/B // golden (tests/issue5403-watermark-fastpath.test.ts) pins that neither @@ -103,7 +107,7 @@ export function buildForwardHeaders(headers: Record<string, string>): Record<str for (const [k, v] of Object.entries(headers)) { // expect: undici fetch rejects it with UND_ERR_NOT_SUPPORTED — dropped // here so no egress path can forward it (#5369). - if (k.toLowerCase() === "content-length" || k.toLowerCase() === "host" || k.toLowerCase() === "expect") continue; + if (k.toLowerCase() === "content-length" || k.toLowerCase() === "host" || k.toLowerCase() === "expect" || k.toLowerCase() === BILI_HOST_RECOVERY_OWNER_HEADER) continue; out[k] = v; } out["content-type"] = "application/json"; diff --git a/src/server/inject.ts b/src/server/inject.ts index 1ea1fab6e3..b5ff8ae906 100644 --- a/src/server/inject.ts +++ b/src/server/inject.ts @@ -5,11 +5,35 @@ import { type AnthropicRequestBody, type GoogleFunctionDeclaration, type GoogleT import { appendSystemText } from "../util.js"; import { absorbEnabled, absorbToolName } from "../absorb.js"; import { blockOpsEnabled } from "../acp-block-ops.js"; -import { BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_GOOGLE, BILI_ACP_TOOLS_GOOGLE_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, packOwnsDescription, ptcNoteMode, withAcpNotes, withAcpNotesOpenAI, withMarkerIntegrityNote, withMcpDialectNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; +import { PROXY_TOOL_NAMES, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_GOOGLE, BILI_ACP_TOOLS_GOOGLE_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, packOwnsDescription, ptcNoteMode, withAcpNotes, withAcpNotesOpenAI, withMarkerIntegrityNote, withMcpDialectNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; import { conversationPromptSections, externalSummaryEnabled, messageTimestampSections, withExternalSummaryTools } from "../external-summary-surface.js"; import { forceTextProtocol as knobForceTextProtocol, renderNone as knobRenderNone } from "../knobs.js"; import { GENERATION_BOUNDARY_NOTE } from "../generation-boundary.js"; +export const CODEX_EXEC_EXEMPT_NOTE = " This proxy-provided tool has no functions.exec SDK binding: call it DIRECTLY as a normal tool call using its declared name and namespace. Do not invoke it through tools.* inside functions.exec; the proxy executes the direct call server-side."; + +// Hint detection must not widen the shared omnibus reachability gate (#5592). +function hasCodexExec(tools: readonly unknown[], namespace = ""): boolean { + for (const value of tools) { + if (value === null || typeof value !== "object") continue; + const tool = value as { type?: unknown; name?: unknown; tools?: unknown }; + const name = typeof tool.name === "string" ? tool.name : ""; + const qualifiedName = namespace ? `${namespace}.${name}` : name; + if ((tool.type === "custom" || tool.type === "function") && qualifiedName === "functions.exec") return true; + if (!Array.isArray(tool.tools)) continue; + if (tool.type === "namespace" && name && hasCodexExec(tool.tools, qualifiedName)) return true; + if (tool.type === "additional_tools" && hasCodexExec(tool.tools, namespace)) return true; + } + return false; +} + +function withCodexExecNotes<T extends { name?: string; description?: string }>(tools: readonly T[], skip: ReadonlySet<string>): T[] { + return tools.map((tool) => typeof tool.name === "string" && PROXY_TOOL_NAMES.has(tool.name) + && !skip.has(tool.name) && typeof tool.description === "string" + ? { ...tool, description: tool.description + CODEX_EXEC_EXEMPT_NOTE } + : tool); +} + export function injectSystem( parsed: AnthropicRequestBody, opts: ProxyOptions, @@ -122,6 +146,8 @@ export function injectResponsesTool(tools: unknown[] | undefined, toolsToAdd: re // #920 rule via mergeOwnedTools: bili owns these names (see core above). // #3252: exempt note when the client tools are run_code-shaped. const mode = ptcNoteMode((tools ?? []).map((t) => (t as { name?: string })?.name)); - const base = withAcpNotes(withExternalSummaryTools(applyAcpToolOverrides(toolsToAdd, toolPrompts), externalOn), mode, packOwnsDescription(toolPrompts)); + const ownedDescriptions = packOwnsDescription(toolPrompts); + let base = withAcpNotes(withExternalSummaryTools(applyAcpToolOverrides(toolsToAdd, toolPrompts), externalOn), mode, ownedDescriptions); + if (hasCodexExec(tools ?? [])) base = withCodexExecNotes(base, ownedDescriptions); return mergeOwnedTools<unknown>(tools, base, [], (t) => (t as { name?: string })?.name); } diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index a7b1908856..51a28bd890 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -327,6 +327,15 @@ export async function prepareAnthropic( // intact history has no gap; a gap without framing falls through to // detectUnannouncedHistoryRewrite below). Title-gen side requests are // skipped like on the openai lane (same tiny-budget heuristic). + // #6061: honour a boundary armed on an earlier request. The detector in + // generation-boundary.ts arms it when stale above-frontier citations prove + // the persisted ledger is from a previous generation; the dsh branch below + // only fires on a client-side compaction signature, which a plain + // model/upstream switch does not produce — that left the two surfaces (nudge + // vs compress) permanently disagreeing about "highest allocated ref". + if (reconcileNativeCompactionBoundary(session)) { + log("info", `[${sessionId}] [acp-gen-boundary] ref ledger belongs to a previous generation — ACP compression state rebased onto the current one`); + } let dshRebased = false; const anthropicTitleGen = typeof parsed.max_tokens === "number" && parsed.max_tokens <= 200; if (!anthropicTitleGen && laneDecisions.profile.compactsClientSide && session.state.blocks.some((b) => b.active)) { diff --git a/src/server/rejection-diagnostics.ts b/src/server/rejection-diagnostics.ts new file mode 100644 index 0000000000..729238c548 --- /dev/null +++ b/src/server/rejection-diagnostics.ts @@ -0,0 +1,122 @@ +/** + * #4452: bounded terminal-cause diagnostics for a relayed upstream REJECTION. + * + * A relay/gateway that fans a request out across candidate targets may answer a + * total failure with ONE aggregate body carrying every per-target attempt, e.g.: + * + * { "error": { "message": "All targets failed", "type": "upstream_error" }, + * "attempts": [ { "target": "…", "details": { "error": { "message": "…" } } }, … ] } + * + * bili's own log line (`← upstream <status>: <snippet>`) keeps only the first + * 600 bytes of that body, so the operator sees the OUTER narrative — "all + * targets failed" — while the per-target cause sits further in. This module + * reads exactly ONE such nested cause (the last usable attempt's) when a body + * has that shape, reporting its attempt position rather than guessing causality. + * + * Hard bounds, deliberately: + * - ONE message, never a recursive walk: no `JSON.stringify` of the payload, no + * traversal of `cause`/`errors`/`attempts` chains, no echoing of the request + * body. Only one `attempts[].details.error.message`, with the aggregate + * accepted directly or beneath its standard `error` envelope. + * - Credential scrubbing goes through the shared log-mask sanitizer + * (`redactSecretsInText`), the SAME choke point the logger applies, so a + * provider that echoes the rejected key cannot smuggle it into the log. It is + * a high-signal credential scrubber, NOT a general privacy filter: it does not + * mask arbitrary URLs or paths, and the far stronger guarantee here is + * structural — only this one nested string is ever read, never a body dump. + * - Bounded to `MAX_DIAGNOSTIC_CHARS`; ALL control characters (C0+C1, incl. + * NEL U+0085) and whitespace collapsed so a newline (the `\r\n` shape or any + * C1 line-break) can never forge a second log field. + * - Absent, non-string, empty, or structurally-unrelated payloads return + * `undefined`: the caller then logs exactly what it logged before, so a + * rejection that carries no attempt detail is unchanged byte-for-byte. + * + * Recovery policy is NOT touched here — this module only reads a rejection + * body; every retry/refold/arm decision stays in the relay. + */ + +import { redactSecretsInText } from "../log-mask.js"; + +/** Upper bound on the retained cause text, independent of the outer snippet. */ +const MAX_DIAGNOSTIC_CHARS = 300; + +/** Do not even attempt to parse a body larger than this: an aggregate error + * reply is small (a few KB); a multi-megabyte body is a stream (or an HTML + * error page), not a JSON rejection envelope, and parsing it would cost the + * request more than the diagnostic is worth. */ +const MAX_PARSED_BYTES = 64 * 1024; + +interface DiagnosticResult { + /** The last usable attempt's nested cause, sanitized + bounded. */ + message: string; + /** 0-based index of the attempt the cause came from (for the log line). */ + attemptIndex: number; + /** How many attempts the aggregate body carried. */ + attempts: number; +} + +/** Collapse control chars (C0+C1 — incl. NEL U+0085) and all whitespace to + * single spaces, then clamp, so no control byte can survive as a forged field + * break in the log line. */ +function bound(text: string): string { + const collapsed = text.replace(/[\u0000-\u001f\u007f-\u009f]/g, " ").replace(/\s+/g, " ").trim(); + if (collapsed.length <= MAX_DIAGNOSTIC_CHARS) return collapsed; + return `${collapsed.slice(0, MAX_DIAGNOSTIC_CHARS)} …`; +} + +/** Read `value[name]` only when `value` is a non-array object, so a nested + * field is reached without a generic record guard that would prove nothing + * about the field actually consumed here. */ +function child(value: unknown, name: string): unknown { + if (typeof value !== "object" || value === null || Array.isArray(value)) return undefined; + return (value as Record<string, unknown>)[name]; +} + +/** + * Read the single last-reported nested cause out of a relay rejection body. + * + * `payload` is the raw upstream error-body text (the same string the relay + * already buffers to detect an overflow). Returns `undefined` whenever there is + * no usable `attempts[].details.error.message`, leaving the caller's existing + * logging untouched. + * + * Selection rule: the LAST attempt that carries a non-empty + * `details.error.message`, scanning from the end. If the terminal attempt is + * silent, an earlier cause is reported with its actual position and is NOT + * labeled terminal: its existence does not establish why the last stage failed. + */ +export function rejectionDiagnostics(payload: string | null | undefined): DiagnosticResult | undefined { + if (typeof payload !== "string" || payload.length === 0) return undefined; + if (Buffer.byteLength(payload, "utf8") > MAX_PARSED_BYTES) return undefined; + const trimmed = payload.trim(); + // A rejection body must be a JSON object envelope — anything else (an HTML + // error page, a bare text message, an SSE frame) carries no attempts[]. + if (!trimmed.startsWith("{") || !trimmed.endsWith("}")) return undefined; + let parsed: unknown; + try { + parsed = JSON.parse(trimmed); + } catch { + return undefined; + } + const attempts = child(parsed, "attempts") ?? child(child(parsed, "error"), "attempts"); + if (!Array.isArray(attempts) || attempts.length === 0) return undefined; + for (let i = attempts.length - 1; i >= 0; i--) { + const raw = child(child(child(attempts[i], "details"), "error"), "message"); + if (typeof raw !== "string") continue; + const sanitized = bound(redactSecretsInText(raw)); + if (sanitized.length === 0) continue; + return { message: sanitized, attemptIndex: i, attempts: attempts.length }; + } + return undefined; +} + +/** Render the diagnostic as the compact suffix the relay appends to its + * `← upstream <status>` line. Returns "" when there is no usable cause, so the + * caller can concatenate unconditionally and stay byte-identical without one. + * Pure numbers and the sanitized cause only — never the payload. */ +export function rejectionDiagnosticSuffix(payload: string | null | undefined): string { + const diag = rejectionDiagnostics(payload); + if (diag === undefined) return ""; + const position = diag.attempts > 1 ? ` attempt ${diag.attemptIndex + 1}/${diag.attempts}` : ""; + return `; ${diag.attemptIndex === diag.attempts - 1 ? "terminal" : "reported"} cause${position}: ${diag.message}`; +} diff --git a/src/server/relay.ts b/src/server/relay.ts index 7e34790cf8..a9123e7253 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -64,7 +64,7 @@ import { clearUpstreamAlertsForHost, noteCircuitFailFastLog, recordUpstreamAlert import { describeUpstreamFailure, formatUpstreamError, proxyDispatcher, recordUpstreamConnection } from "../upstream-proxy.js"; import { markExpectedFailFast, withUpstreamFailure, type UpstreamFailureKind } from "../upstream-fail.js"; import { noteUpstreamThrottle429, noteUpstreamThrottleResolved, releaseUpstreamThrottleProbe, requestModelFromBody, throttle429ModelScope, type ThrottleLocalReject, upstreamThrottleGate, upstreamThrottlePeekReject } from "../upstream-throttle.js"; -import { applyEstimateCalibration, currentCalibrationFactor, inspectContextOverflow, isMarkerlessOverflowReject, MIN_ASSUMED_WINDOW_TOKENS, normalizeUpstreamOrigin, usageOutputTotal, usageTotals, type ContextOverflowInfo, type WireProtocol } from "../util.js"; +import { applyEstimateCalibration, BILI_HOST_RECOVERY_OWNER_HEADER, BILI_HOST_RECOVERY_OWNER_OMP, currentCalibrationFactor, inspectContextOverflow, isMarkerlessOverflowReject, MIN_ASSUMED_WINDOW_TOKENS, normalizeUpstreamOrigin, usageOutputTotal, usageTotals, type ContextOverflowInfo, type WireProtocol } from "../util.js"; import { billOutputUsage } from "../billing.js"; import { DUMP_PLAINTEXT_BANNER } from "../dump-banner.js"; import { recordPckAblation } from "../pck-probe.js"; @@ -73,6 +73,7 @@ import { applyOutputSteering, applyOutputSteeringJson } from "../output-steering import { estimateWireOverhead } from "./budget.js"; import { buildForwardHeaders, connectionNamedHeaders, RESPONSE_ONLY_STRIP_HEADERS, safeSessionId, UPSTREAM_HOP_HEADERS } from "./headers.js"; import { bodyDumpEnabled, logDumpFailure } from "./observability.js"; +import { rejectionDiagnosticSuffix } from "./rejection-diagnostics.js"; import { effectiveAbsorbBlock, effectiveBlockOpsEnabled } from "./prepare-responses.js"; import { awaitDrain, bufferToStream, dumpStreamToFile, pipeThrough, readStreamToBuffer } from "./stream-io.js"; import { buildForwardTarget, measuredBreakdownClause, measuredOutbound, outboundPayloadBreakdown, registerRequestAbort, beginStreamKeepalive, effectiveTokenCount, googlePathKind, imageBillingFor, imageTokenCapFor, inboundResignCredential, outboundContextEstimates, resolveUpstream, stripKernelSummaries, usageGradeInputBaseline, DOOMED_PROBE_ABSOLUTE_BYTES, DOOMED_PROBE_BYTES_PER_TOKEN, type Prepared } from "../server.js"; @@ -346,14 +347,19 @@ async function forwardCaptured( v1Meta?: Record<string, unknown>, throttleModelHint?: string, onWireBody?: (body?: Buffer | string) => void, + releaseRequestInput?: () => void, ): Promise<void> { const forwardStartedAt = Date.now(); - const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepared.session, () => !res.destroyed) : undefined; + const hostOwnsTruncationRecovery = req.headers[BILI_HOST_RECOVERY_OWNER_HEADER] === BILI_HOST_RECOVERY_OWNER_OMP; + // #4861: persistence answers "did the upstream turn finish?", not "is the +// client socket still open?" — a client that hangs up on the terminal frame +// (pi's shape) must not lose the finished reply. Partial turns (no terminal) +// are still never captured. +const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepared.session) : undefined; // E2: a codex native-compaction request intercepted in prepare() carries a // forged success response — serve it without contacting upstream. if (prepared?.codexForge) { - onWireBody?.(); - log("info", `[${prepared.session.id}] codex compact served locally (${prepared.codexForge.kind}); upstream not contacted`); + onWireBody?.(); releaseRequestInput?.(); log("info", `[${prepared.session.id}] codex compact served locally (${prepared.codexForge.kind}); upstream not contacted`); if (!res.headersSent) res.writeHead(200, { "content-type": prepared.codexForge.contentType }); res.end(prepared.codexForge.body); return; @@ -362,6 +368,7 @@ async function forwardCaptured( // zero-egress contract as codexForge; the audit line was written in // prepare (this line is the egress-proof for the serve itself). if (prepared?.autoModeForge) { + releaseRequestInput?.(); log("info", `[${prepared.session.id}] auto-mode classifier served locally (${prepared.autoModeForge.variant}); upstream not contacted`); if (!res.headersSent) res.writeHead(200, { "content-type": prepared.autoModeForge.contentType }); res.end(prepared.autoModeForge.body); @@ -1965,6 +1972,11 @@ async function forwardCaptured( } } } + // No further overflow refold can run. Release its inbound-only state before + // consuming a potentially long response; wireBody still owns retry/echo data. + releaseRequestInput?.(); + releaseRequestInput = undefined; + overflowRefold = undefined; const { response: upstream, clearTimer: clearUpstreamTimer } = upstreamResult; // [#1343] delivery decided: the attached full text rode THIS request's body, so settle // its lifecycle here — delivered on a 2xx, dropped-and-logged otherwise. The ack is @@ -2052,14 +2064,18 @@ async function forwardCaptured( } log("warn", `[${s.id}] upstream image-count wall (cap ${info.imageCountCap}, request carried ${info.imageCountSeen ?? "?"}) — learned for pre-send convergence on this route; no token-space arm (#4124)`); } else if (isMarkerlessOverflowReject(upstream.status, errText)) { - // #4452: the gateway rewrote this overflow-shaped reject into a body with no - // parseable marker, so bili's self-heal cannot engage from it — name that here - // (+ the last meter reading) so a body-rewriting gateway's silent starvation is - // diagnosable from the log rather than the opaque line further down. + // #4452: a 413 IS the payload-size verdict, yet this body carries no + // parseable marker, so bili's self-heal cannot engage from it — name that + // here (+ the last meter reading) so a body-rewriting gateway's silent + // starvation is diagnosable from the log rather than the opaque line + // further down. Only 413 reaches this branch: an unmarked 400 is the + // generic malformed/schema-rejected bucket across relays and says nothing + // about size, so it is never diagnosed as overflow here (see + // isMarkerlessOverflowReject). const est = s.stats.lastInputTokens > 0 ? s.stats.lastInputTokens : 0; const win = typeof s.metadata.effectiveContextLimit === "number" && s.metadata.effectiveContextLimit > 0 ? s.metadata.effectiveContextLimit : 0; const meterNote = est > 0 && win > 0 ? `; last settled input ~${Math.round(est)} vs effective window ${win}` : ""; - log("warn", `[${s.id}] ← upstream ${upstream.status} carries no parseable overflow marker (an intermediate gateway rewrote the rejection body?) — bili's overflow self-heal (refold / emergency-shrink / window-learn) CANNOT engage from this rejection${meterNote}; repeated at similar size = calibration-starved route (#4452)`); + log("warn", `[${s.id}] ← upstream ${upstream.status} carries no parseable overflow marker (an intermediate gateway rewrote the payload-size rejection body?) — bili's overflow self-heal (refold / emergency-shrink / window-learn) CANNOT engage from this rejection${meterNote}; repeated at similar size = calibration-starved route (#4452)`); } // #762: learn strict-echo on the MAIN request path too. The loop-only // learner (src/loop/core.ts) never sees client-originated 400s, so a @@ -2096,7 +2112,15 @@ async function forwardCaptured( let snippet = bodyText.slice(0, 600).replace(/\s+/g, " ").trim(); if (bodyText.length > 600) snippet += " …"; if (!snippet) snippet = "(no body)"; - loggerLog("warn", `[${errSid}] ← upstream ${upstream.status}${reqIdText}: ${snippet}`); + // #4452: a relay that fans out to several targets answers a total failure + // with ONE aggregate body whose real cause sits in the LAST attempt's + // `details.error.message` — past the 600-byte snippet above, so the log + // showed only "all targets failed". Append that single terminal cause + // (bounded + credential-scrubbed by the helper; empty when absent, leaving + // this line byte-identical to before). Read-only: no recovery decision + // here depends on it. + const terminalCause = rejectionDiagnosticSuffix(bodyText); + loggerLog("warn", `[${errSid}] ← upstream ${upstream.status}${reqIdText}: ${snippet}${terminalCause}`); // #4204: feed this send's FINAL verdict. A model-scoped KeyRing 429 is // selected only by its structured code plus exact request-model match; // keyring_all_limited and every ambiguous response remain account-wide. @@ -2266,7 +2290,13 @@ async function forwardCaptured( // first attempt's status/content-type (a resolveFakeCompletion // re-send is not reflected; the pipes' own diag says which // retry budget was spent instead). - const upstreamMeta: UpstreamMeta = { status: upstream.status, contentType: upstream.headers.get("content-type") ?? undefined }; + const upstreamMeta: UpstreamMeta = { + status: upstream.status, + contentType: upstream.headers.get("content-type") ?? undefined, + requestId: upstream.headers.get("x-request-id") ?? upstream.headers.get("request-id") ?? undefined, + opencodeLogId: upstream.headers.get("x-opencode-log-id") ?? undefined, + cfRay: upstream.headers.get("cf-ray") ?? undefined, + }; // #2346: intrinsic runaway-enumeration terminator for the plugin-streamed // body (the incident lane). Applied after any fake-completion buffering so // both the live and replayed paths are guarded; byte-verbatim otherwise. @@ -2311,6 +2341,7 @@ async function forwardCaptured( wireBodyText, upstreamMeta, reply, + hostOwnsTruncationRecovery, ); } else { // #732/#821: the plugin pipe re-issues the agent's own body @@ -2341,6 +2372,7 @@ async function forwardCaptured( upstreamMeta, reply, opts.streamErrorShape, + hostOwnsTruncationRecovery, ); } } else { @@ -2445,7 +2477,13 @@ async function forwardCaptured( prepared.processedMessages.length > 0; if (!useRewriter || prepared === null) { // #2328: pipes cite the first upstream response's HTTP identity. - const upstreamMeta: UpstreamMeta = { status: upstream.status, contentType: upstream.headers.get("content-type") ?? undefined }; + const upstreamMeta: UpstreamMeta = { + status: upstream.status, + contentType: upstream.headers.get("content-type") ?? undefined, + requestId: upstream.headers.get("x-request-id") ?? upstream.headers.get("request-id") ?? undefined, + opencodeLogId: upstream.headers.get("x-opencode-log-id") ?? undefined, + cfRay: upstream.headers.get("cf-ray") ?? undefined, + }; if (prepared && prepared.resetAfterSuccess) { const [toClient, toObserve] = responseBody.tee(); const observed = observeResponsesTerminalState(toObserve, prepared.stream); @@ -2478,6 +2516,7 @@ async function forwardCaptured( undefined, upstreamMeta, reply, + hostOwnsTruncationRecovery, ); } else { await pipeThrough(toClient, res); @@ -2530,6 +2569,7 @@ async function forwardCaptured( undefined, upstreamMeta, reply, + hostOwnsTruncationRecovery, ); } else { await pipePluginChatWithStrip( @@ -2557,6 +2597,7 @@ async function forwardCaptured( upstreamMeta, reply, opts.streamErrorShape, + hostOwnsTruncationRecovery, ); } } else if ( diff --git a/src/server/side-request.ts b/src/server/side-request.ts index 811d53c6c4..d991c40feb 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -5,6 +5,7 @@ import { reserveOutputHeadroom, systemCarrierTexts } from "../util.js"; import { type ResolvedImageBilling } from "../image-tokens.js"; import { ACP_TOOL_NAMES, ABSORB_TOOL_NAME, BLOCK_OP_TOOL_NAMES, RETRIEVE_TOOL_NAME, RULE_TOOL_NAME } from "acp-kernel"; import { READ_ORIGINAL_IMAGE_TOOL_NAME } from "../compress-tool.js"; +import { dshCarrierRawMarkers } from "../durable-message-guards.js"; // #388: side requests (title-gen etc.) share the main session key but must not // touch kernel state. Identified by a tiny output budget (same heuristic as @@ -72,6 +73,34 @@ export function isServerToolUtilityCall(parsed: unknown): boolean { return false; } +const STRUCTURED_FORMAT_TYPES: ReadonlySet<string> = new Set(["json_schema", "json_object"]); + +/** #5073: structural identification of a tool-less structured-output utility + * call — a memory-extraction daemon such as Hindsight (inheriting the host's + * HTTPS_PROXY, under the main session id) sends no tools, asks for a JSON + * reply (`response_format` of type `json_schema`/`json_object` on the chat + * wire, or `text.format` of those types on the Responses wire), and carries a + * large output budget. Such a call is a one-shot utility request, not an + * agent turn: agent turns always carry an action surface (#546), so a strict + * backend rejects `tools` injected beside `response_format` with a 400. It + * therefore rides the #388 verbatim side passthrough. + * The no-tools veto is part of the predicate (not only the caller's #546 + * guard) so callers that consult it independently of `isSideRequest` — the + * restoreOutputBudget skip in handle.ts, the same category as the #2500 + * server-tool predicate — can never demote a tool-carrying main turn. + * `{type:"text"}` is plain prose and does not count. */ +export function isStructuredOutputCall(parsed: unknown): boolean { + if (!parsed || typeof parsed !== "object") return false; + const p = parsed as Record<string, unknown>; + if (Array.isArray(p.tools) && p.tools.length > 0) return false; + const text = p.text; + const format = p.response_format + ?? (text !== null && typeof text === "object" ? (text as { format?: unknown }).format : undefined); + if (format === null || typeof format !== "object") return false; + const type = (format as { type?: unknown }).type; + return typeof type === "string" && STRUCTURED_FORMAT_TYPES.has(type); +} + export function isSideRequest(parsed: unknown, requestAgent?: string): boolean { if (!parsed || typeof parsed !== "object") return false; if (requestAgent !== undefined && SIDE_REQUEST_AGENTS.has(requestAgent)) return true; @@ -84,6 +113,13 @@ export function isSideRequest(parsed: unknown, requestAgent?: string): boolean { // gets injected into it. Explicit host intent above still outranks this. if (isServerToolUtilityCall(p)) return true; if (Array.isArray(p.tools) && p.tools.length > 0) return false; + // #5073: a toolless request that demands a structured JSON reply is a + // utility call, not an agent turn (memory-extraction daemons such as + // Hindsight that inherit the host's HTTPS_PROXY, with a large output + // budget). Wire tools injected into it make strict backends reject the + // pair with a 400. Routed through the #388 side passthrough like the #2500 + // server-tool shape above. + if (isStructuredOutputCall(parsed)) return true; const field = outputBudgetField(parsed); if (!field) return false; const raw = readOutputBudget(p, field); @@ -117,9 +153,9 @@ export function isDshTitleRequest(parsed: unknown): boolean { // #4438: dsh's auto-review sidecar (@deepseek-ai/dsh-experimental-auto-review's // classifyRisk()) fires before every native tool call under the Auto permission -// tier: a fixed REVIEW_POLICY system prompt + exactly ONE freshly-flattened user -// blob (ENVIRONMENT / PROJECT_INSTRUCTIONS / FILTERED_HISTORY / PENDING_ACTION), -// no tools, same conversation id as the main turn. On at least one deployment +// tier: a fixed REVIEW_POLICY system prompt + a freshly-flattened user body +// (ENVIRONMENT / PROJECT_INSTRUCTIONS / FILTERED_HISTORY / PENDING_ACTION), no +// tools, same conversation id as the main turn. On at least one deployment // class (web profile behind a gateway) the extension's own model client reaches // bili UNSTAMPED — no x-bili-plugin — while the session's sticky plugin binding // keeps the #2432 rebase gate armed for it: every header-gated isolation gate @@ -130,13 +166,38 @@ export function isDshTitleRequest(parsed: unknown): boolean { // skips the output clamp (#453) into permanent upstream 400s. // Shape-decisive WITHOUT a caller-side header gate — deliberately different from // isDshTitleRequest's discipline: the shape carries its own client evidence -// (dsh's fixed REVIEW_POLICY first line, version-pinned like +// (dsh's fixed REVIEW_POLICY sentence, version-pinned like // DSH_TITLE_SYSTEM_PREFIX / DSH_COMPACTION_INSTRUCTION_PREFIX), and the header // gate is precisely what failed here. A main turn can never match: it carries a -// tools surface and multi-turn history; the single-blob bar rejects every -// history-replaying request. +// tools surface, and every entry-level loop below vetoes any assistant/tool +// turn — which every real history resend has. +// #6104: two bars were host-side FORMAT decisions that moved, not identity. +// REVIEW_POLICY gained a `REVIEW_POLICY` banner line (the sentence is no longer +// line 1 ⇒ line-scan, see DSH_AUTO_REVIEW_HEAD_LINES), and 0.2.2's +// reviewMessages() replays the WHOLE conversation as role:"user" text blobs (so +// the old `users === 1` bar rejected the very request it was written for, and +// the review then ate the fold ledger it was supposed to leave alone: 107 DEAD +// tombstones, 0 compressions across the reporter's 5 sessions). The replay veto +// never needed the user count — the assistant/tool veto carries it. export const DSH_AUTO_REVIEW_SYSTEM_PREFIX = "You are the final authorization reviewer for exactly one pending tool call"; +// #6104: dsh versions its REVIEW_POLICY by PREFIXING it with a banner line +// (`REVIEW_POLICY` on 0.2.2-rc.1), so a first-line pin silently misses the +// whole review traffic on any host build that adds one more line above the +// sentence. The pin stays version-pinned to the SENTENCE; only its position is +// read tolerantly — the first few lines of the system carrier, never the whole +// prompt (a policy QUOTED deep inside some other agent's prompt must not +// identify a review call; the reporter's own #6104 body shows nothing else in +// the request can rescue the match, so a miss here runs the review through the +// full pipeline and tombstones its refs as DEAD). +const DSH_AUTO_REVIEW_HEAD_LINES = 5; + +/** The pinned reviewer sentence on any line of the system carrier's head. */ +function isDshAutoReviewPolicy(text: string): boolean { + const lines = text.split("\n", DSH_AUTO_REVIEW_HEAD_LINES); + return lines.some((line) => line.trimStart().startsWith(DSH_AUTO_REVIEW_SYSTEM_PREFIX)); +} + export function isDshAutoReviewRequest(protocol: string | null, parsed: unknown): boolean { if (protocol === null || !parsed || typeof parsed !== "object") return false; const p = parsed as Record<string, unknown>; @@ -146,7 +207,7 @@ export function isDshAutoReviewRequest(protocol: string | null, parsed: unknown) // The fixed reviewer policy on ANY wire's system carrier (top-level system, // instructions, systemInstruction.parts, or a role:system entry in // messages[]/input[]) — systemCarrierTexts reads them all (#3149 lesson). - if (!systemCarrierTexts(p).some((t) => t.trimStart().startsWith(DSH_AUTO_REVIEW_SYSTEM_PREFIX))) return false; + if (!systemCarrierTexts(p).some(isDshAutoReviewPolicy)) return false; const entries = protocol === "responses" ? p.input : protocol === "google" ? p.contents : p.messages; if (!Array.isArray(entries)) return false; let users = 0; @@ -168,10 +229,37 @@ export function isDshAutoReviewRequest(protocol: string | null, parsed: unknown) userHasContent = true; } } - // Exactly ONE non-system message with content: the freshly-flattened review - // snapshot. A replayed history (any assistant/tool turn or a second user - // message) vetoes — see the comment above the constant. - return users === 1 && userHasContent; + // ONE or MORE non-system messages with content: the freshly-flattened + // review snapshot (#4438) or its history-replaying form (#6104). The replay + // veto is the loop above — any assistant/tool turn returns false — so a real + // history resend can never reach this line, whatever its user count. + return users >= 1 && userHasContent; +} +/** #5687 follow-up: a sidecar never carries a durable-state marker. dsh injects + * its durable payloads (skills, workspace instructions, MCP catalog, runtime + * context, image typing, job notices) into the FIRST main turn's user blob. A + * tool-less single-user body carrying one of those markers is therefore a MAIN + * turn whose assistant surface happens to be empty — routing it to the verbatim + * side lane skips the kernel, so the classifier can never stamp the carrier's + * BLOCKED ref (#2419/#2446/#2447/#2481) and the wire-byte contracts of + * #2817/#5403 reset. Genuine sidecars (title / enhance / translate / + * auto-review) carry the host's own prompt and no marker, so they keep the + * side lane. */ +function carriesDurableState(text: string): boolean { + for (const marker of dshCarrierRawMarkers) if (text.indexOf(marker) >= 0) return true; + return false; +} + +/** #5687 follow-up (call-site narrowing): a utility sidecar pins a TINY fixed + * output budget (host presets: prompt-enhance 4096). Agent main turns either + * omit the field (openai wire) or size it from their conversation, so + * `isDshSingleShotSidecar` alone over-matches the FIRST unstyled main turn. */ +const SINGLE_SHOT_SIDECAR_MAX_BUDGET = 8192; +export function hasSingleShotBudget(parsed: unknown): boolean { + if (!parsed || typeof parsed !== "object") return false; + const p = parsed as Record<string, unknown>; + const budget = p.max_tokens ?? p.max_completion_tokens ?? p.max_output_tokens; + return typeof budget === "number" && budget > 0 && budget <= SINGLE_SHOT_SIDECAR_MAX_BUDGET; } // #5656/#5584: dsh's per-feature sidecar pins above (title #2503, auto-review @@ -196,10 +284,12 @@ export function isDshAutoReviewRequest(protocol: string | null, parsed: unknown) // A non-empty system carrier is REQUIRED so the kernel's empty-instructions // path (aux calls riding the main session through the full pipeline, pinned in // tests/dsh-persona-fingerprint.test.ts phase 6) keeps its contract. -// Residual, accepted: a tool-less MAIN turn 1 matches the single-shot shape and -// rides the verbatim side lane for that one turn (tiny — nothing to compress); -// turn 2+ replays history and rejoins the full pipeline. The caller must gate -// on the dsh lane (evidence-per-client discipline, cf. isDshTitleRequest). +// Residual, accepted (narrowed by #5687 follow-up): a tool-less MAIN turn 1 +// that ALSO passes the call-site budget screen (small pinned output, +// hasSingleShotBudget) and carries no durable-state marker still rides the +// verbatim side lane for that one turn; turn 2+ replays history and rejoins +// the full pipeline. The caller must gate on the dsh lane +// (evidence-per-client discipline, cf. isDshTitleRequest). export function isDshSingleShotSidecar(protocol: string | null, parsed: unknown): boolean { if (protocol === null || !parsed || typeof parsed !== "object") return false; const p = parsed as Record<string, unknown>; @@ -225,8 +315,16 @@ export function isDshSingleShotSidecar(protocol: string | null, parsed: unknown) const c = rec.content ?? rec.parts; if (typeof c === "string") { if (c.length > 0) userHasContent = true; + // #5687 follow-up: a durable-state marker means this is a MAIN turn's + // injected context, never a utility sidecar. + if (carriesDurableState(c)) return false; } else if (Array.isArray(c) && c.length > 0) { userHasContent = true; + for (const part of c) { + if (part === null || typeof part !== "object") continue; + const text = (part as { text?: unknown }).text; + if (typeof text === "string" && carriesDurableState(text)) return false; + } } } return users === 1 && userHasContent; diff --git a/src/storage/mysql.ts b/src/storage/mysql.ts index 5befe169b7..72bdf1268d 100644 --- a/src/storage/mysql.ts +++ b/src/storage/mysql.ts @@ -1848,6 +1848,7 @@ export class MysqlBackend implements StorageBackend { }; // sqlite twin: newest matching line wins. let lineNo = -1; + let matchCount = 0; let rawLines: string[] = []; try { rawLines = readFileSync(exportPath, "utf8").split("\n"); @@ -1859,9 +1860,12 @@ export class MysqlBackend implements StorageBackend { } catch { continue; } + // #6317: count ALL entries for this session. The reverse scan + // meets the NEWEST line first — keep that; a plain + // `lineNo = i` without break would end on the OLDEST. if (candidate && typeof candidate === "object" && "sessionId" in candidate && candidate.sessionId === sessionId) { - lineNo = i; - break; + if (lineNo === -1) lineNo = i; + matchCount += 1; } } } catch { @@ -1874,6 +1878,9 @@ export class MysqlBackend implements StorageBackend { return { ok: false, reason: "export-corrupt" }; } if (!dump.session || dump.session.sessionId !== sessionId) return { ok: false, reason: "export-mismatch" }; + if (matchCount > 1) { + loggerLog("warn", `[#6317] archive log contained ${matchCount} entries for session ${sessionId}; restoring the newest entry and removing all entries`); + } const sess = dump.session; await this.tx(async (c) => { const references = new Map<string, number>(); diff --git a/src/storage/store.ts b/src/storage/store.ts index 4e42316ec8..5256cfb38a 100644 --- a/src/storage/store.ts +++ b/src/storage/store.ts @@ -1044,10 +1044,10 @@ export class UnifiedStore { next_run_id = excluded.next_run_id, meta_json = excluded.meta_json, stats_json = excluded.stats_json, - metadata_json = excluded.metadata_json, - kstate_json = excluded.kstate_json, + metadata_json = CASE WHEN metadata_json IS excluded.metadata_json THEN sessions.metadata_json ELSE excluded.metadata_json END, + kstate_json = CASE WHEN kstate_json IS excluded.kstate_json THEN sessions.kstate_json ELSE excluded.kstate_json END, tail_json = excluded.tail_json, - ccr_json = excluded.ccr_json, + ccr_json = CASE WHEN ccr_json IS excluded.ccr_json THEN sessions.ccr_json ELSE excluded.ccr_json END, last_ingested_seq = excluded.last_ingested_seq, orphans_json = excluded.orphans_json`, ) @@ -2153,13 +2153,16 @@ export class UnifiedStore { sessionExt: Record<string, unknown>[]; checkpoints: Record<string, unknown>[]; }; - // Scan the JSONL for the session's line (skip malformed/truncated + // Scan the JSONL for the session's NEWEST line (#6317: a pre-fix + // failed archive can leave stale lines behind; a later successful + // archive appends the newest dump). Skip malformed/truncated // trailing lines — an archive append interrupted by a crash must not // block restoring OTHER sessions). Newest first: when the log holds // more than one line for this session (an archive whose sweep never // committed, then a later successful one), the last line appended is // the state the session was actually archived in. let lineNo = -1; + let matchCount = 0; let rawLines: string[] = []; try { rawLines = readFileSync(exportPath, "utf8").split("\n"); @@ -2171,7 +2174,10 @@ export class UnifiedStore { } catch { continue; } - if (candidate && typeof candidate === "object" && "sessionId" in candidate && candidate.sessionId === sessionId) { lineNo = i; break; } + // #6317: count ALL entries for this session. The reverse scan + // meets the NEWEST line first — keep that; a plain + // `lineNo = i` without break would end on the OLDEST. + if (candidate && typeof candidate === "object" && "sessionId" in candidate && candidate.sessionId === sessionId) { if (lineNo === -1) lineNo = i; matchCount += 1; } } } catch { return { ok: false, reason: "export-corrupt" }; @@ -2183,6 +2189,9 @@ export class UnifiedStore { return { ok: false, reason: "export-corrupt" }; } if (!dump.session || dump.session.sessionId !== sessionId) return { ok: false, reason: "export-mismatch" }; + if (matchCount > 1) { + loggerLog("warn", `[#6317] archive log contained ${matchCount} entries for session ${sessionId}; restoring the newest entry and removing all entries`); + } const sess: NonNullable<typeof dump.session> = dump.session; this.db.transaction(() => { const references = new Map<string, number>(); @@ -2244,9 +2253,10 @@ export class UnifiedStore { sess.lastIngestedSeq ?? null, sess.orphansJson ?? null, sessionId, ); }); - // Drop the restored session's line(s) from the JSONL (a same-session - // archive can theoretically appear more than once across reinstalls; - // rewrite removes them all). Other sessions' lines are untouched. + // Drop the restored session's line(s) from the JSONL. #6317: a + // pre-fix failed archive can leave more than one line; restore now + // already used the newest match and removes all matches, warning when + // stale lines were present so the loss pattern is diagnosable. const kept: string[] = []; let ownLines = 0; for (const l of rawLines) { diff --git a/src/stream-error.ts b/src/stream-error.ts index 74b25f7366..c1495a747e 100644 --- a/src/stream-error.ts +++ b/src/stream-error.ts @@ -173,11 +173,11 @@ export function emitStreamError(res: http.ServerResponse, protocol: Protocol, me * (it just does not reach the wire). * Never throws. */ -/** #2328 Q2: termination diagnostics carried on the in-band truncation - * error frame (`meta`) and in the log line, so "the upstream never sent a +/** #2328 Q2: termination diagnostics. They ride the proxy log line only and + * never a client-visible SSE frame (#5951), so "the upstream never sent a * terminal event" (#721's no-terminal-seen) and "a terminal was arriving but - * its bytes never completed / were unrecognizable" can be told apart from - * the client side (#2347 forensics). Every field is plain-JSON safe. */ + * its bytes never completed / were unrecognizable" can be told apart + * (#2347 forensics). Every field is plain-JSON safe. */ export interface TruncationDiag { /** Which pipe path fired: clean EOF without a terminal, or a read error. */ cause: "eof" | "read-error"; @@ -217,21 +217,46 @@ export interface TruncationDiag { upstreamStatus?: number; /** #5951: the failed read itself, named, on a cut the pipe could not replay. */ readError?: string; + /** Sanitized `Error.cause` chain; names and codes only, never error text. */ + readErrorCause?: Array<{ name: string; code?: string }>; + /** Safe upstream trace identifiers captured from response headers. */ + upstreamRequestId?: string; + upstreamLogId?: string; + upstreamCfRay?: string; + /** #5951: the replay predicate's own inputs, on both pipe lanes. */ + replay?: ReplayRefusalDiag; upstreamContentType?: string; } +/** #5951: why a cut was not replayed, counter by counter. The predicate itself + * lives in plugin.ts and is silent when it declines, so a refused cut names + * here the exact value that refused it. Absent `replay` = no replay door on + * this pipe at all. */ +interface ReplayRefusalDiag { + /** The pipe held a refetch, i.e. a replay was possible on this cut. */ + replayAvailable: boolean; + sawThinking?: boolean; + sawReasoning?: boolean; + sawToolUse?: boolean; + sawFunctionCall?: boolean; + blocksForwarded?: number; + openStreams?: number; + forwardedAny?: boolean; + releasedMarkupChars?: number; + proseChars?: number; + answerProseChars?: number; + fastPathChars?: number; + heldVisibleChars?: number; +} + export function emitUpstreamTruncation(res: http.ServerResponse, protocol: Protocol, finished: boolean, log?: (msg: string) => void, diag?: TruncationDiag, transparent = false): void { const message = "upstream stream ended before a completion event; this turn may be incomplete"; const action = finished ? protocol === "google" ? "finish reason already delivered, stream complete" : "finish reason seen, synthesizing missing terminal byte" : transparent ? "passing through raw (host owns truncation handling, #2563)" : "emitting in-band error"; - // #2328: the diag blob rides the log line too — one grep for - // `upstream stream truncated` pulls the full classification + counters - // without needing the client-side error frame. + // Diagnostics stay in the proxy's local log. Never attach upstream IDs or + // transport details to client-visible SSE error metadata. log?.(`[acp-proxy: upstream stream truncated (${protocol}) — ${action}${diag ? `; classification=${diag.classification} cause=${diag.cause}; diag=${JSON.stringify(diag)}` : ""}]`); - // #2328: attach the diagnostics as `meta` on the in-band error object. - // Wire-shape change (new field on an error-only frame) — human-merge item. - const metaField = diag === undefined ? {} : { meta: diag }; try { if (transparent && !finished) { // #2563: host-owned handling — deliver exactly what arrived @@ -241,16 +266,16 @@ export function emitUpstreamTruncation(res: http.ServerResponse, protocol: Proto if (finished) { safeWrite(res, "data: [DONE]\n\n"); } else { - safeWrite(res, `data: ${JSON.stringify({ error: { type: "server_error", code: "upstream_stream_truncated", message, ...metaField } })}\n\ndata: [DONE]\n\n`); + safeWrite(res, `data: ${JSON.stringify({ error: { type: "server_error", code: "upstream_stream_truncated", message } })}\n\ndata: [DONE]\n\n`); } } else if (protocol === "responses") { - safeWrite(res, `event: error\ndata: ${JSON.stringify({ type: "error", code: "upstream_stream_truncated", message, ...metaField })}\n\n`); + safeWrite(res, `event: error\ndata: ${JSON.stringify({ type: "error", code: "upstream_stream_truncated", message })}\n\n`); } else if (protocol === "google") { // The finishReason chunk IS Gemini's stream terminator: when it was // delivered, the client is already done and an extra terminal // object would read as a second answer. Only the mid-flight cut // needs the error frame (503/UNAVAILABLE — an upstream-cut stream). - if (!finished) safeWrite(res, `data: ${JSON.stringify({ error: { code: 503, message, status: "UNAVAILABLE", ...metaField } })}\n\n`); + if (!finished) safeWrite(res, `data: ${JSON.stringify({ error: { code: 503, message, status: "UNAVAILABLE" } })}\n\n`); } else if (protocol === "commandcode") { // The finish event IS this wire's terminator: when delivered there // is nothing to synthesize; only a mid-flight cut gets an error line. @@ -259,7 +284,7 @@ export function emitUpstreamTruncation(res: http.ServerResponse, protocol: Proto if (finished) { safeWrite(res, `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`); } else { - safeWrite(res, `event: error\ndata: ${JSON.stringify({ type: "error", error: { type: "server_error", code: "upstream_stream_truncated", message, ...metaField } })}\n\n`); + safeWrite(res, `event: error\ndata: ${JSON.stringify({ type: "error", error: { type: "server_error", code: "upstream_stream_truncated", message } })}\n\n`); } } } catch { diff --git a/src/stream-google.ts b/src/stream-google.ts index 54dc5dc18a..7835f9207b 100644 --- a/src/stream-google.ts +++ b/src/stream-google.ts @@ -22,6 +22,7 @@ function* rewriteGoogleJsonSteps(body: unknown, ctx: RewriteCtx): Generator<Json if (!candidate || !content || !Array.isArray(parts)) return body; let converted = false; let sawReal = false; + let promotedSignature = ""; const noteParts: string[] = []; const keptParts: GooglePart[] = []; // Absorb signature in whole text (m00885): drop only when the request @@ -33,6 +34,9 @@ function* rewriteGoogleJsonSteps(body: unknown, ctx: RewriteCtx): Generator<Json const fc = part.functionCall as { name?: unknown; args?: unknown; id?: unknown } | undefined; if (fc && typeof fc.name === "string" && isProxyToolFor(fc.name, ctx.session, ctx.config)) { converted = true; + if (promotedSignature.length === 0 && typeof part.thoughtSignature === "string" && part.thoughtSignature.length > 0) { + promotedSignature = part.thoughtSignature; + } const args = fc.args !== null && typeof fc.args === "object" ? (fc.args as Record<string, unknown>) : {}; noteParts.push(yield { name: fc.name, args, id: typeof fc.id === "string" ? fc.id : undefined }); continue; @@ -53,10 +57,28 @@ function* rewriteGoogleJsonSteps(body: unknown, ctx: RewriteCtx): Generator<Json if (typeof injection.text === "string") noteParts.push(injection.text); } if (!converted) return body; + // Gemini attaches thoughtSignature only to the first functionCall part in a + // parallel-call turn; if a stripped proxy call carried it, promote it onto + // the first surviving real functionCall part so client replay stays signed. + if (sawReal && promotedSignature.length > 0) { + const firstFcIdx = keptParts.findIndex((p) => p && typeof p === "object" && p.functionCall !== undefined); + if (firstFcIdx >= 0) { + const firstFc = keptParts[firstFcIdx]!; + if (typeof firstFc.thoughtSignature !== "string" || firstFc.thoughtSignature.length === 0) { + keptParts[firstFcIdx] = { ...firstFc, thoughtSignature: promotedSignature }; + } + } + } // The tool result is a NEW text part: an existing text part may carry a // thoughtSignature the client echoes back, and appending to its text would // leave the signature describing a string that no longer exists. - content.parts = [...keptParts, { text: noteParts.join("\n") }]; + const notePart: GooglePart = + !sawReal && + promotedSignature.length > 0 && + !keptParts.some((p) => p && typeof p === "object" && typeof p.thoughtSignature === "string" && p.thoughtSignature.length > 0) + ? { text: noteParts.join("\n"), thoughtSignature: promotedSignature } + : { text: noteParts.join("\n") }; + content.parts = [...keptParts, notePart]; if (!sawReal) { candidate.finishReason = "STOP"; } diff --git a/src/update.ts b/src/update.ts index 2886deeb12..e3c493bcff 100644 --- a/src/update.ts +++ b/src/update.ts @@ -2099,21 +2099,47 @@ export async function installViaTarball( } }; - // Copy staging over install dir using Node's built-in fs.cp (Node 16.7+). - // Cross-platform — no dependency on the `cp` binary (absent on Windows). - // `recursive: true` + the trailing `/.` semantics: fs.cp copies the - // *contents* of stagingDir into installDir, merging without nesting. - let copyError: string | null = null; + // #6309: prefer an atomic directory swap. Copy staging next to the + // install dir on the same volume, then rename the current install aside + // and rename the prepared copy into place. The old copy is the rollback + // source and is only removed after verification. If rename is not + // possible (common on Windows while the running install holds handles), + // fall back to the previous merge-copy path. + const swapCandidate = `${installDir}.update-new-${Date.now()}`; + let copiedForSwap = false; try { - await cp(stagingDir, installDir, { recursive: true, force: true }); - } catch (e) { - copyError = `failed to copy to install dir: ${String(e)}`; - } finally { + await cp(stagingDir, swapCandidate, { recursive: true, force: true }); + copiedForSwap = true; + await rename(installDir, backupDir); + try { + await rename(swapCandidate, installDir); + } catch (e) { + const swapError = String(e); + await rename(backupDir, installDir); + throw new Error(swapError); + } await rm(stagingDir, { recursive: true, force: true }); - } - if (copyError !== null) { - const rb = await restoreFromBackup(); - return { ok: false, error: rb ?? copyError }; + } catch { + if (copiedForSwap) await rm(swapCandidate, { recursive: true, force: true }); + if (!pnpmOldLink) { + try { + await cp(installDir, backupDir, { recursive: true, force: true }); + } catch (e) { + return { ok: false, error: `backup of current install failed (install left untouched): ${String(e)}` }; + } + } + let copyError: string | null = null; + try { + await cp(stagingDir, installDir, { recursive: true, force: true }); + } catch (e) { + copyError = `failed to copy to install dir: ${String(e)}`; + } finally { + await rm(stagingDir, { recursive: true, force: true }); + } + if (copyError !== null) { + const rb = await restoreFromBackup(); + return { ok: false, error: rb ?? copyError }; + } } // Final verification: version must match and every declared entry must @@ -2151,7 +2177,7 @@ export async function installViaTarball( // Success: the backup is no longer needed, and neither is the displaced // pnpm link (the store copy underneath it was left byte-identical). - await rm(backupDir, { recursive: true, force: true }); + if (!pnpmOldLink) await rm(backupDir, { recursive: true, force: true }); if (pnpmOldLink) { try { await unlink(pnpmOldLink); diff --git a/src/upstream-fail.ts b/src/upstream-fail.ts index c1214744da..3f870361cd 100644 --- a/src/upstream-fail.ts +++ b/src/upstream-fail.ts @@ -26,9 +26,9 @@ export type UpstreamFailureKind = /** Downstream client disconnected (external abort fired). Nothing bili * did or can retry — the request is dead by definition. */ | "client-abort" - /** Idle-budget / read-phase timeout expiry: our own watchdog aborted, or - * OS/undici reported a headers/body timeout. NOT retried — the budget - * already waited and the request may have reached the upstream. */ + /** Timeout: our idle watchdog, an undici headers/body timeout, or an OS + * ETIMEDOUT whose phase may be unknown. NOT retried — the request may + * have reached the upstream. This category alone does not prove idle expiry. */ | "upstream-timeout" /** Phase-1 FIRST-BYTE timeout: the operator tightened the first-byte budget * (network.upstreamFirstByteMs / BILI_UPSTREAM_FIRST_BYTE_MS below the idle @@ -266,7 +266,7 @@ export function isFailFastUpstreamKind(kind: UpstreamFailureKind): boolean { * taxonomy and the checklist never drift apart. */ export const UPSTREAM_FAIL_HINTS: Record<UpstreamFailureKind, string> = { "client-abort": "downstream client disconnected — no bili-side action", - "upstream-timeout": "idle budget expired (headers/body) — check upstream health; not retried by design", + "upstream-timeout": "transport timeout — inspect code/syscall and nested causes to identify the phase; not retried by design", "upstream-headers-timeout": "no first response byte within the explicitly-tightened first-byte budget (network.upstreamFirstByteMs / BILI_UPSTREAM_FIRST_BYTE_MS) — zero bytes were received, so a bounded transparent replay may be attempted (BILI_REPLAY_RETRY_MAX); the mid-stream (body) idle budget is unchanged", "connect-timeout": "TCP handshake never completed — upstream/proxy unreachable or blackholed; a bounded transparent replay may be attempted (BILI_REPLAY_RETRY_MAX)", "upstream-unreachable": "ICMP no-route rejection (EHOSTUNREACH/ENETUNREACH/EHOSTDOWN) — nothing reached the upstream; check routing/firewall/ACL (on macOS also the Local Network privacy grant for the app that owns the bili process, #5663); a bounded transparent replay may be attempted (BILI_REPLAY_RETRY_MAX)", diff --git a/src/upstream-proxy.ts b/src/upstream-proxy.ts index ebe3947506..1339299b11 100644 --- a/src/upstream-proxy.ts +++ b/src/upstream-proxy.ts @@ -4,7 +4,7 @@ import tls from "node:tls"; import { Pool, ProxyAgent } from "undici"; import type { ProviderRoutes } from "./config.js"; import { log as loggerLog } from "./logger.js"; -import { maskHostInText, maskHostPortForDisplay, maskUrlForLog } from "./log-mask.js"; +import { maskHostInText, maskHostPortForDisplay, maskUrlForLog, maskIpsInText, maskUrlsInText, redactSecretsInText } from "./log-mask.js"; import { upstreamTimeoutMs } from "./fetch-util.js"; import { classifyUpstreamFailure, hintWithSkippedProxy, isTlsTrustCode, redactProxyUrl, tlsTrustRemediation, type SkippedProxyInfo, type UpstreamFailureDetail } from "./upstream-fail.js"; import { upstreamCaIdentity, upstreamExtraCa } from "./upstream-ca.js"; @@ -537,22 +537,33 @@ export function connectThroughProxy(host: string, port: number, proxyUrl: string function errorChain(error: unknown): Array<Record<string, unknown>> { const chain: Array<Record<string, unknown>> = []; const seen = new Set<unknown>(); - let current: unknown = error; - while (current && !seen.has(current)) { + // Bound both depth and visits so large or cyclic aggregates cannot flood diagnostics. + let remaining = 32; + let truncated = false; + const visit = (current: unknown, depth: number): void => { + if (remaining-- <= 0 || depth >= 8) { truncated = true; return; } + if (!current || seen.has(current)) return; seen.add(current); if (current instanceof Error) { - const record: Record<string, unknown> = { message: current.message }; + const record: Record<string, unknown> = { message: String(current.message) }; for (const key of ["code", "errno", "syscall", "address", "port"]) { const value = (current as unknown as Record<string, unknown>)[key]; - if (value !== undefined) record[key] = value; + if (typeof value === "string" || typeof value === "number") record[key] = value; } chain.push(record); - current = current.cause; + if (current.cause !== undefined) visit(current.cause, depth + 1); + if (current instanceof AggregateError && Array.isArray(current.errors)) { + for (const child of current.errors) { + if (remaining <= 0) { truncated = true; break; } + visit(child, depth + 1); + } + } } else { chain.push({ message: String(current) }); - break; } - } + }; + visit(error, 0); + if (truncated) chain.push({ message: "[error chain truncated]" }); return chain; } @@ -579,9 +590,9 @@ export function describeUpstreamFailure(error: unknown, url: string, proxyUrl?: try { targetHostPort = new URL(url).host; } catch { /* not a URL — no display host */ } const maskHostIn = (s: string): string => { try { - return maskHostInText(s, new URL(url).hostname); - } catch { /* not a URL — nothing to mask */ } - return s; + s = maskHostInText(s, new URL(url).hostname); + } catch { /* not a URL — no target host to mask */ } + return redactSecretsInText(maskUrlsInText(maskIpsInText(s))); }; let codeValue: unknown; for (const field of fields) { @@ -593,6 +604,12 @@ export function describeUpstreamFailure(error: unknown, url: string, proxyUrl?: } const messages = chain.map((entry) => maskHostIn(String(entry.message ?? ""))).filter(Boolean); if (messages.length > 0) parts.push(`message=${messages.join(" <- ")}`); + if (chain.length > 1) { + const causes = chain.slice(1).map((entry) => Object.fromEntries( + Object.entries(entry).map(([key, value]) => [key, typeof value === "string" ? maskHostIn(value) : value]), + )); + parts.push(`causes=${JSON.stringify(causes)}`); + } parts.push(`url=${maskUrlForLog(url)}`); parts.push(`proxy=${redactProxyUrl(proxyUrl) ?? "direct"}`); let hint = hintWithSkippedProxy(kind, skipped); diff --git a/src/util.ts b/src/util.ts index 7755425244..9f05351d4c 100644 --- a/src/util.ts +++ b/src/util.ts @@ -17,6 +17,11 @@ export const BILI_PLUGIN_BYPASS_HEADER = "x-bili-plugin-bypass"; // mirroring what a direct send without the overlay would have been. export const BILI_PASSTHROUGH_HEADER = "x-bili-passthrough"; +// Internal request marker: OMP owns stream truncation recovery for its native +// fetch calls; the proxy consumes this header and strips it before upstream egress. +export const BILI_HOST_RECOVERY_OWNER_HEADER = "x-bili-host-recovery-owner"; +export const BILI_HOST_RECOVERY_OWNER_OMP = "omp"; + // #4079: the MINIMUM window both egress oversize guards size against. A // configured or declared window can be wrong SMALL — a stale // modelContextLimit, a registry miss, a model-catalog fallback (#3993: @@ -398,6 +403,10 @@ const CONTEXT_OVERFLOW_PATTERNS: RegExp[] = [ /输入[^。;;!!\n]{0,12}(?:超出|超过|超限)/, // (f) #3240/#3246 consolidation: mixed-language gateways write "prompt 过长". /prompt[^。;;!!\n]{0,6}(?:过长|太长|超长)/i, + // DashScope compatible-mode states the ceiling as an input-length RANGE + // ("Range of input length should be [1, 1000000]"), with no token or length + // subject phrase: unlisted here, the 400 is not an overflow and nothing arms. + /range of input length should be \[/i, ]; function toTokenNumber(s: string): number | undefined { @@ -463,6 +472,10 @@ function parseOverflowWindow(text: string): number | undefined { // first parenthesized number is the rejected payload and must not be used. m = text.match(/maximum number of tokens allowed\s*\((\d[\d,]*)\)/i); if (m) return toTokenNumber(m[1]); + // DashScope: "Range of input length should be [1, 1000000]" — the upper + // bound is the input ceiling; the lower bound is not a window. + m = text.match(/range of input length should be \[\s*\d[\d,]*\s*,\s*(\d[\d,]*)\s*\]/i); + if (m) return toTokenNumber(m[1]); m = text.match(/maximum context length is (\d[\d,]*)/i) ?? text.match(/maximum context length of (\d[\d,]*)/i) ?? @@ -556,18 +569,30 @@ export function inspectContextOverflow(status: number, bodyText: string): Contex }; } -// #4452: an intermediate gateway that rewrites EVERY rejection into one generic -// body (Cherry Studio et al. strip the token counts / window from each overflow -// 400) makes inspectContextOverflow return isOverflow:false even when the payload -// really overflows — so bili's whole overflow self-heal (#1195 same-request -// refold, armOverflowShrink, window learn) keys off a marker that is never there -// and silently never engages, starving the route of calibration. This predicate -// names exactly that shape — a 4xx overflow-SHAPED reject whose body matches no -// pattern — so the relay can log it loudly instead of the opaque `← upstream 400` -// line. Deliberately EXCLUDES 5xx (those already self-heal via the local estimate, +// #4452: an intermediate gateway that rewrites a payload-size rejection into one +// generic body (Cherry Studio et al. strip the token counts / window) makes +// inspectContextOverflow return isOverflow:false even when the payload really +// was too large — so bili's whole overflow self-heal (#1195 same-request refold, +// armOverflowShrink, window learn) keys off a marker that is never there and +// silently never engages, starving the route of calibration. This predicate +// names exactly that shape — an unmarked size-wall reject whose body matches no +// pattern — so the relay can log it loudly instead of the opaque +// `← upstream 413` line. +// +// ONLY 413 carries that meaning on its own. A 400 is the generic "malformed / +// schema-invalid / role-rejected / auth-shaped" bucket across relays: the SAME +// opaque body (`{"error":{"message":"Internal server error", +// "code":"bad_request"}}`) is emitted for a truncated JSON body, an unsupported +// field and a genuinely oversized payload alike, so treating every unmarked 400 +// as evidence of overflow manufactured a false overflow diagnosis for ordinary +// malformed requests (#4452 follow-up). 413 Request Entity Too Large is the +// status whose SEMANTICS are already the payload-size verdict, which is why it +// can be diagnosed without a body marker. +// +// Deliberately EXCLUDES 5xx (those already self-heal via the local estimate, // #604) and any body that DOES carry a marker (self-heal engages normally then). export function isMarkerlessOverflowReject(status: number, bodyText: string): boolean { - return (status === 400 || status === 413) && !CONTEXT_OVERFLOW_PATTERNS.some((p) => p.test(bodyText)); + return status === 413 && !CONTEXT_OVERFLOW_PATTERNS.some((p) => p.test(bodyText)); } /** Default cap on the output-headroom reservation, as a fraction of the diff --git a/src/win-cmd.ts b/src/win-cmd.ts index ec3c68d88e..2dc7182aa3 100644 --- a/src/win-cmd.ts +++ b/src/win-cmd.ts @@ -18,7 +18,8 @@ /** Returns a human-readable reason why this token cannot cross the cmd.exe * line parser verbatim, or undefined when it can (the safe set): plain text, * whitespace-bearing text quoted whole, Unicode, semicolons/commas/equals, - * backslashes in even runs or outside quoted positions, and `!` (delayed + * backslashes anywhere (quoteWinToken doubles a trailing run inside quotes, so + * the receiving program reads it back verbatim), and `!` (delayed * expansion is opt-in via ^setlocal enabledelayedexpansion^ and standard npm * shims never enable it). The offending CONTENT is deliberately not part of * the return value — callers may log the reason but must not echo the token @@ -29,13 +30,6 @@ export function winCmdUnsafeToken(token: string): string | undefined { if (token.includes("%")) return "% (cmd.exe expands %VAR%)"; if (/[&|<>^()]/.test(token)) return "cmd metacharacters (& | < > ^ ( ))"; if (/[\r\n]/.test(token)) return "a line break"; - // Odd trailing-backslash run matters only when quoteWinToken will wrap the - // token in quotes (whitespace-bearing, quote-free): the final `\` then - // escapes the closing quote in MSVCRT parsing and swallows it. - if (/\s/.test(token)) { - const trail = /\\+$/.exec(token)?.[0].length ?? 0; - if (trail % 2 === 1) return "an odd number of trailing backslashes"; - } return undefined; } diff --git a/tests/adapter-google.test.ts b/tests/adapter-google.test.ts index ac3c7f6d3c..65a876bf85 100644 --- a/tests/adapter-google.test.ts +++ b/tests/adapter-google.test.ts @@ -1,8 +1,11 @@ import { test } from "node:test"; import assert from "node:assert/strict"; -import type { CoreMessage } from "acp-kernel"; -import { createGoogleAdapter } from "../src/loop/index.ts"; +import { createCore, defaultConfig, type CoreMessage } from "acp-kernel"; +import { coreToGoogle, googleToCore, type GooglePart } from "acp-kernel/wire"; +import { createGoogleAdapter, runCompressLoop } from "../src/loop/index.ts"; import type { CompressLoopAdapter, ParsedStreamEvent } from "../src/loop/index.ts"; +import { createSession } from "../src/session.ts"; +import { rewriteGoogleJsonResponseAsync } from "../src/stream-google.ts"; function sse(obj: unknown): string { return `data: ${JSON.stringify(obj)}\n\n`; @@ -349,3 +352,418 @@ test("google parseStream: a null sibling part does not break the edited frame", const frames = framesOf(events); assert.ok(frames.some((f) => f.includes("kept prose")), `the prose still reaches the client, got: ${frames.join(" | ")}`); }); + +test("google parseStream: parallel [compress (signed), write_file (unsigned)] in same chunk promotes thoughtSignature onto write_file", async () => { + const adapter = createGoogleAdapter(REQUEST_BODY, "client system", "bili_absorb", "gemini-4-argon-medium"); + const events = await collect( + adapter, + sse({ + candidates: [{ + content: { + role: "model", + parts: [ + { + functionCall: { + name: "compress", + args: { content: "m00046-m00129: folded context\nsummary body" }, + id: "call_compress_1", + }, + thoughtSignature: "sig-parallel-same-chunk", + }, + { + functionCall: { + name: "write_file", + args: { path: "src/app.ts", content: "export const ok = true;\n" }, + id: "call_write_2", + }, + }, + { + functionCall: { + name: "read_file", + args: { path: "README.md" }, + id: "call_read_3", + }, + }, + ], + }, + index: 0, + }], + }) + + sse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }] }), + ); + + const toolCalls = events.filter((e): e is Extract<ParsedStreamEvent, { kind: "tool_call" }> => e.kind === "tool_call"); + const writeEvent = toolCalls.find((e) => e.name === "write_file"); + const readEvent = toolCalls.find((e) => e.name === "read_file"); + const compressEvent = toolCalls.find((e) => e.name === "compress"); + assert.ok(writeEvent && writeEvent.passthrough === true, "write_file emitted as passthrough real call"); + assert.equal(writeEvent.signature, "sig-parallel-same-chunk", "first surviving real call event inherits thoughtSignature"); + assert.ok(readEvent && readEvent.passthrough === true, "read_file emitted as passthrough real call"); + assert.equal(readEvent.signature, undefined, "subsequent sibling real call stays unsigned"); + assert.ok(compressEvent && compressEvent.passthrough !== true, "compress emitted as server-side proxy call"); + + const callMeta = events.find((e) => e.kind === "meta" && e.chunk.toString("utf8").includes("write_file")); + assert.ok(callMeta && callMeta.kind === "meta", "replayed call frame present"); + const replayedParts = partsOf(frameJson(callMeta.chunk)); + assert.equal(replayedParts.length, 2, "compress stripped; two real calls kept"); + assert.deepEqual(replayedParts[0].functionCall, { + name: "write_file", + args: { path: "src/app.ts", content: "export const ok = true;\n" }, + id: "call_write_2", + }); + assert.equal(replayedParts[0].thoughtSignature, "sig-parallel-same-chunk", "first surviving real functionCall part carries promoted thoughtSignature"); + assert.equal(replayedParts[1].thoughtSignature, undefined, "second surviving real functionCall part stays unsigned"); +}); + +test("google parseStream: parallel [compress (signed), write_file (unsigned)] across separate chunks promotes thoughtSignature onto write_file chunk", async () => { + const adapter = createGoogleAdapter(REQUEST_BODY, "client system", "bili_absorb", "gemini-4-argon-medium"); + const events = await collect( + adapter, + sse({ + candidates: [{ + content: { + role: "model", + parts: [{ + functionCall: { + name: "compress", + args: { content: "m00001-m00010: folded\nsummary" }, + id: "call_compress_chunk1", + }, + thoughtSignature: "sig-parallel-chunk1", + }], + }, + index: 0, + }], + }) + + sse({ + candidates: [{ + content: { + role: "model", + parts: [{ + functionCall: { + name: "write_file", + args: { path: "src/index.ts", content: "console.log(1);\n" }, + id: "call_write_chunk2", + }, + }], + }, + index: 0, + }], + }) + + sse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }] }), + ); + + const allFrames = framesOf(events).join(""); + assert.equal(allFrames.includes("call_compress_chunk1"), false, "proxy-only chunk 1 is dropped from client stream"); + const callMeta = events.find((e) => e.kind === "meta" && e.chunk.toString("utf8").includes("write_file")); + assert.ok(callMeta && callMeta.kind === "meta", "chunk 2 replayed with write_file"); + const replayedParts = partsOf(frameJson(callMeta.chunk)); + assert.equal(replayedParts.length, 1); + assert.equal(replayedParts[0].thoughtSignature, "sig-parallel-chunk1", "chunk 2 real functionCall part inherits chunk 1 proxy call's thoughtSignature"); +}); + +test("google parseStream: real call with its own thoughtSignature is never overwritten by a sibling proxy call", async () => { + const adapter = createGoogleAdapter(REQUEST_BODY, "client system", "bili_absorb", "gemini-4-argon-medium"); + const events = await collect( + adapter, + sse({ + candidates: [{ + content: { + role: "model", + parts: [ + { + functionCall: { name: "write_file", args: { path: "a.ts" }, id: "call_1" }, + thoughtSignature: "sig-own-real", + }, + { + functionCall: { name: "compress", args: {}, id: "call_2" }, + thoughtSignature: "sig-proxy-other", + }, + ], + }, + index: 0, + }], + }) + + sse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }] }), + ); + + const callMeta = events.find((e) => e.kind === "meta" && e.chunk.toString("utf8").includes("write_file")); + assert.ok(callMeta && callMeta.kind === "meta"); + const replayedParts = partsOf(frameJson(callMeta.chunk)); + assert.equal(replayedParts.length, 1); + assert.equal(replayedParts[0].thoughtSignature, "sig-own-real", "real call's own signature is preserved"); +}); + +test("rewriteGoogleJsonResponseAsync: promotes stripped proxy call thoughtSignature onto surviving real functionCall part", async () => { + const core = createCore(); + const config = defaultConfig(400_000); + const session = createSession("google-json-sig"); + const messages: CoreMessage[] = [ + { id: "m1", role: "user", contentType: "text", text: "step 1" }, + { id: "m2", role: "assistant", contentType: "text", text: "ack 1" }, + { id: "m3", role: "user", contentType: "text", text: "step 2" }, + { id: "m4", role: "assistant", contentType: "text", text: "ack 2" }, + { id: "m5", role: "user", contentType: "text", text: "step 3" }, + { id: "m6", role: "assistant", contentType: "text", text: "ack 3" }, + { id: "m7", role: "user", contentType: "text", text: "step 4" }, + ]; + const turn = core.processTurn({ messages, state: session.state, config, tokenCount: 0 }); + session.state = turn.state; + + const responseBody = { + candidates: [{ + content: { + role: "model", + parts: [ + { + functionCall: { + name: "compress", + args: { content: "m00001-m00003: early steps\ncompleted step 1 and step 2" }, + id: "fc_compress", + }, + thoughtSignature: "sig-json-parallel", + }, + { + functionCall: { + name: "write_file", + args: { path: "out.txt", content: "done" }, + id: "fc_write", + }, + }, + ], + }, + finishReason: "STOP", + }], + }; + + const rewritten = (await rewriteGoogleJsonResponseAsync(responseBody, { + core, + config, + messages, + session, + log: () => {}, + })) as { candidates: Array<{ content: { parts: GooglePart[] } }> }; + + const outParts = rewritten.candidates[0].content.parts; + const fcParts = outParts.filter((p) => p.functionCall !== undefined); + assert.equal(fcParts.length, 1, "compress stripped, write_file kept"); + assert.equal(fcParts[0].functionCall?.name, "write_file"); + assert.equal(fcParts[0].thoughtSignature, "sig-json-parallel", "write_file inherited thoughtSignature from stripped compress call"); +}); + +test("runCompressLoop + next-turn Google replay: mixed [compress, write_file] turn keeps thoughtSignature on write_file after fold", async () => { + const core = createCore(); + const config = defaultConfig(400_000); + const session = createSession("google-loop-mixed-sig"); + + const initialContents = [ + { role: "user", parts: [{ text: "task 1" }] }, + { role: "model", parts: [{ text: "done 1" }] }, + { role: "user", parts: [{ text: "task 2" }] }, + { role: "model", parts: [{ text: "done 2" }] }, + { role: "user", parts: [{ text: "task 3" }] }, + { role: "model", parts: [{ text: "done 3" }] }, + { role: "user", parts: [{ text: "now compress and write file" }] }, + ]; + const flat1 = googleToCore({ contents: initialContents }); + const turn1 = core.processTurn({ messages: flat1.msgs, state: session.state, config, tokenCount: 0 }); + session.state = turn1.state; + + const upstreamSse = + sse({ + candidates: [{ + content: { + role: "model", + parts: [ + { thought: true, text: "I will compress m00001-m00004 and call write_file." }, + { + functionCall: { + name: "compress", + args: { content: "m00001-m00004: tasks 1-2\ncompleted tasks 1 and 2" }, + id: "call_compress_mixed", + }, + thoughtSignature: "sig-gemini4-active-turn", + }, + { + functionCall: { + name: "write_file", + args: { path: "result.txt", content: "hello" }, + id: "call_write_mixed", + }, + }, + ], + }, + index: 0, + }], + }) + + sse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }] }); + + const reqBody: Record<string, unknown> = { contents: initialContents }; + const adapter = createGoogleAdapter(reqBody, undefined, undefined, "gemini-4-argon-medium"); + const streamedChunks: string[] = []; + for await (const chunk of runCompressLoop( + new Response(upstreamSse, { status: 200 }).body!, + { + core, + config, + messages: turn1.messages, + compressMessages: flat1.msgs, + session, + protocol: "google", + log: () => {}, + }, + reqBody, + { url: "http://127.0.0.1:1/v1beta/models/gemini-4-argon-medium:streamGenerateContent?alt=sse", headers: {} }, + adapter, + "system prompt", + )) { + streamedChunks.push(chunk.toString("utf8")); + } + + // Reconstruct the model turn parts exactly as a Google SSE client accumulates them. + const clientReceivedParts: GooglePart[] = []; + for (const raw of streamedChunks.join("").split("\n\n")) { + const line = raw.split("\n").find((l) => l.startsWith("data:")); + if (!line) continue; + const parsed = JSON.parse(line.slice(5).trim()) as { + candidates?: Array<{ content?: { parts?: GooglePart[] } }>; + }; + const ps = parsed.candidates?.[0]?.content?.parts ?? []; + clientReceivedParts.push(...ps); + } + + const writePartOnWire = clientReceivedParts.find((p) => p.functionCall?.name === "write_file"); + assert.ok(writePartOnWire, "client received write_file functionCall part"); + assert.equal(writePartOnWire.thoughtSignature, "sig-gemini4-active-turn", "streamed write_file part carries thoughtSignature"); + + // Next turn: client appends the model turn + functionResponse for write_file, and bili prepares the folded outbound body. + const nextTurnContents = [ + ...initialContents, + { role: "model", parts: clientReceivedParts }, + { + role: "user", + parts: [{ + functionResponse: { + name: "write_file", + id: "call_write_mixed", + response: { output: "ok" }, + }, + }], + }, + ]; + const flat2 = googleToCore({ contents: nextTurnContents }); + const turn2 = core.processTurn({ messages: flat2.msgs, state: session.state, config, tokenCount: 0 }); + const outboundContents = coreToGoogle(turn2.messages); + const modelTurnWithWrite = outboundContents.find( + (c) => c.role === "model" && c.parts.some((p) => p.functionCall?.name === "write_file"), + ); + assert.ok(modelTurnWithWrite, "outbound Google contents contain the write_file model turn"); + const outboundWritePart = modelTurnWithWrite.parts.find((p) => p.functionCall?.name === "write_file"); + assert.ok(outboundWritePart, "write_file part present on outbound wire"); + assert.equal( + outboundWritePart.thoughtSignature, + "sig-gemini4-active-turn", + "surviving write_file part retains thoughtSignature after fold on next turn", + ); +}); + +test("runCompressLoop Google re-request: parallel proxy-only calls [compress (signed), acp_status (unsigned)] keep thoughtSignature on every rebuilt model functionCall turn", async () => { + const core = createCore(); + const config = defaultConfig(400_000); + const session = createSession("google-loop-multi-proxy-sig"); + + const initialContents = [ + { role: "user", parts: [{ text: "step 1" }] }, + { role: "model", parts: [{ text: "done 1" }] }, + { role: "user", parts: [{ text: "step 2" }] }, + { role: "model", parts: [{ text: "done 2" }] }, + { role: "user", parts: [{ text: "compress and check status" }] }, + ]; + const flat = googleToCore({ contents: initialContents }); + const turn = core.processTurn({ messages: flat.msgs, state: session.state, config, tokenCount: 0 }); + session.state = turn.state; + + const round1Sse = + sse({ + candidates: [{ + content: { + role: "model", + parts: [ + { + functionCall: { + name: "compress", + args: { content: "m00001-m00002: step 1\ncompleted step 1" }, + id: "call_proxy_compress", + }, + thoughtSignature: "sig-multi-proxy-round1", + }, + { + functionCall: { + name: "acp_status", + args: {}, + id: "call_proxy_status", + }, + }, + ], + }, + index: 0, + }], + }) + + sse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }] }); + + const round2Sse = + sse({ candidates: [{ content: { role: "model", parts: [{ text: "All folded and checked." }] }, index: 0 }] }) + + sse({ candidates: [{ content: { role: "model", parts: [] }, finishReason: "STOP", index: 0 }] }); + + let capturedRound2Body: { contents?: Array<{ role?: string; parts?: GooglePart[] }> } | undefined; + const origFetch = globalThis.fetch; + globalThis.fetch = (async (_input: unknown, init?: RequestInit) => { + capturedRound2Body = JSON.parse(String(init?.body ?? "{}")) as { + contents?: Array<{ role?: string; parts?: GooglePart[] }>; + }; + return new Response(round2Sse, { + status: 200, + headers: { "content-type": "text/event-stream" }, + }); + }) as typeof fetch; + + try { + const reqBody: Record<string, unknown> = { contents: initialContents }; + const adapter = createGoogleAdapter(reqBody, undefined, undefined, "gemini-4-argon-medium"); + for await (const _ of runCompressLoop( + new Response(round1Sse, { status: 200 }).body!, + { + core, + config, + messages: turn.messages, + compressMessages: flat.msgs, + session, + protocol: "google", + log: () => {}, + }, + reqBody, + { url: "http://127.0.0.1:1/v1beta/models/gemini-4-argon-medium:streamGenerateContent?alt=sse", headers: {} }, + adapter, + "system prompt", + )) { + // drain + } + } finally { + globalThis.fetch = origFetch; + } + + assert.ok(capturedRound2Body?.contents, "round 2 re-request body captured"); + const modelFcTurns = capturedRound2Body.contents.filter( + (c) => c.role === "model" && (c.parts ?? []).some((p) => p.functionCall !== undefined), + ); + assert.ok(modelFcTurns.length >= 2, "both proxy tool calls are present in round 2 re-request"); + for (const modelTurn of modelFcTurns) { + const firstFc = (modelTurn.parts ?? []).find((p) => p.functionCall !== undefined); + assert.ok(firstFc, "model turn has functionCall part"); + assert.equal( + firstFc.thoughtSignature, + "sig-multi-proxy-round1", + `functionCall ${firstFc.functionCall?.name} carries round thoughtSignature`, + ); + } +}); diff --git a/tests/claude-native.test.ts b/tests/claude-native.test.ts index a2cac4cd8b..1ac4d6b31c 100644 --- a/tests/claude-native.test.ts +++ b/tests/claude-native.test.ts @@ -405,6 +405,12 @@ test("UPSTREAM_HOP_HEADERS includes expect to prevent undici UND_ERR_NOT_SUPPORT assert.ok(UPSTREAM_HOP_HEADERS.has("expect")); }); +test("OMP recovery ownership marker is stripped before provider egress", () => { + assert.ok(UPSTREAM_HOP_HEADERS.has("x-bili-host-recovery-owner")); + const out = buildForwardHeaders({ "x-bili-host-recovery-owner": "omp", "content-type": "application/json" }); + assert.equal(out["x-bili-host-recovery-owner"], undefined); +}); + test("buildForwardHeaders drops expect so undici fetch does not throw UND_ERR_NOT_SUPPORTED (#5369)", async () => { const server = http.createServer((req, res) => { res.writeHead(200, { "content-type": "application/json" }); res.end("{}"); }); await new Promise<void>((resolve) => { server.listen(0, "127.0.0.1", resolve); }); diff --git a/tests/commandcode-plugin-pipe.test.ts b/tests/commandcode-plugin-pipe.test.ts index 8408d03648..abbfea4138 100644 --- a/tests/commandcode-plugin-pipe.test.ts +++ b/tests/commandcode-plugin-pipe.test.ts @@ -157,16 +157,25 @@ test("stream-error emitters: commandcode branches speak bare JSONL", () => { assert.match(aOut, /"type":"error"/); assert.match(aOut, /boom/); assert.ok(!aOut.includes("data:"), "no SSE framing on the JSONL wire"); + assert.ok(!aOut.includes("\n\n"), "error record ends with exactly one newline"); + assert.ok(aOut.endsWith("}\n"), "bare JSONL record terminator"); const b = makeCollector(); emitUpstreamTruncation(b.res, "commandcode", false, undefined); - assert.match(b.out(), /"type"/, "truncation event is a JSONL line"); + const bOut = b.out(); + assert.match(bOut, /"type"/, "truncation event is a JSONL line"); + // Every record on this wire is single-newline terminated (the pipe writes + // `line + "\n"`); a blank line is not a valid JSONL record and would choke + // strict consumers at exactly the moment the stream already failed. + assert.ok(!bOut.includes("\n\n"), "truncation record carries no blank line"); + assert.ok(bOut.endsWith("}\n"), "truncation record ends with exactly one newline"); const c = makeCollector(); emitPreflightError(c.res, "commandcode", { message: "too big", retryable: false }, undefined); const cOut = c.out(); assert.match(cOut, /too big/); assert.ok(!cOut.includes("event:"), "no SSE event framing"); + assert.ok(!cOut.includes("\n\n"), "preflight error record ends with exactly one newline"); }); // F9c: duplicate tool names must not corrupt settleToolCalls' verbatim replay. diff --git a/tests/context-overflow.test.ts b/tests/context-overflow.test.ts index c2720a0a0d..8f00d7c89b 100644 --- a/tests/context-overflow.test.ts +++ b/tests/context-overflow.test.ts @@ -168,6 +168,28 @@ test("Gemini 'input token count (N) exceeds' states the input count (#3450)", () assert.equal(info.requestedCompletion, undefined); }); +test("DashScope 'Range of input length should be [1, N]' → overflow + input ceiling", () => { + // The real compatible-mode rejection (no token/length-subject phrase): while + // it went unrecognized, the 400 never armed the emergency shrink, so the + // #2313 probe kept re-forwarding the same over-long payload. + const info = inspectContextOverflow( + 400, + '{"error":{"message":"<400> InternalError.Algo.InvalidParameter: Range of input length should be [1, 1000000]","type":"invalid_request_error","param":null,"code":"invalid_parameter_error"},"id":"chatcmpl-55bbf071-1040-9261-8cd4-76088d25ee5c","request_id":"55bbf071-1040-9261-8cd4-76088d25ee5c"}', + ); + assert.equal(info.isOverflow, true); + assert.equal(info.window, 1000000); + assert.equal(info.requestedMessages, undefined); + assert.equal(info.requestedCompletion, undefined); +}); + +test("a parameter-range 400 without the input-length phrasing is NOT an overflow", () => { + const info = inspectContextOverflow( + 400, + '{"error":{"message":"InvalidParameter: temperature should be in range [0, 2]","code":"invalid_parameter_error"}}', + ); + assert.equal(info.isOverflow, false); +}); + test("overflow body WITHOUT stated counts → no requested* fields (#3450 regression guard)", () => { // The legacy phrasing must keep behaving exactly as before (selfheal T3 // relies on it): window only, no invented counts. diff --git a/tests/e2e/README.md b/tests/e2e/README.md index d03c54c90b..f5bfb2f2cf 100644 --- a/tests/e2e/README.md +++ b/tests/e2e/README.md @@ -228,7 +228,7 @@ asserts the four user-facing guarantees of #1239: (`stopProxiesGracefully`) before asserting on-disk state; teardown then SIGKILLs survivors (fake + instance-record pids). - `E2E_CHECK=1` runs a zero-cost preflight (pi binary version, built - `dist/agent/pi-native.js`, fake `/v1/models` probe). `E2E_PI_BIN` / + `dist/agent/billion-context.js`, fake `/v1/models` probe). `E2E_PI_BIN` / `E2E_TMO` override the binary and per-run timeout. ### CI @@ -304,7 +304,7 @@ provider pointed at a local mock that speaks BOTH legs — POST/SSE and a WebSocket upgrade on one port — through the real built proxy. Zero tokens. Gated: `ACP_TEST_E2E_PI_WS=1` (skip-by-default like the other real-client -lanes); needs the built `dist/agent/pi-native.js`. Override the binary with +lanes); needs the built `dist/agent/billion-context.js`. Override the binary with `E2E_PI_BIN`. ```bash diff --git a/tests/e2e/e2e-delegate-notify.test.ts b/tests/e2e/e2e-delegate-notify.test.ts index b084fa2a76..0a5103b4b4 100644 --- a/tests/e2e/e2e-delegate-notify.test.ts +++ b/tests/e2e/e2e-delegate-notify.test.ts @@ -27,8 +27,9 @@ // new turn from idle. // // Gated by ACP_TEST_E2E_NATIVE=1 (same gate as e2e-native-pi.test.ts; needs -// `npm run build` first — the pi package loads dist/agent/pi-native.js, which -// inlines the vendored pi-subagents source). Skips when the pi CLI is absent. +// `npm run build` first — the pi package loads dist/agent/billion-context.js, a +// re-export of pi-native.js (which inlines the vendored pi-subagents source). +// Skips when the pi CLI is absent. import { spawn, spawnSync } from "node:child_process"; import fs from "node:fs"; @@ -42,7 +43,7 @@ import { assertPortDead } from "../port-race.js"; const PI_BIN = process.env.E2E_PI_BIN ?? "pi"; const REPO_ROOT = path.resolve(import.meta.dirname, "../.."); -const PI_NATIVE_ENTRY = path.join(REPO_ROOT, "dist/agent/pi-native.js"); +const PI_NATIVE_ENTRY = path.join(REPO_ROOT, "dist/agent/billion-context.js"); const FAKE_UPSTREAM = path.join(import.meta.dirname, "fake-upstream-chat.mjs"); const TMO_MS = Number(process.env.E2E_TMO ?? 180_000); const QUIET_MS = 12_000; // sustained quiescence before declaring the scenario done @@ -386,7 +387,7 @@ function scenarioSkip(): string | false { if (process.env.ACP_TEST_E2E_NATIVE !== "1") { return "set ACP_TEST_E2E_NATIVE=1 (real pi RPC lane + local fake upstream; deterministic, zero tokens)"; } - if (!fs.existsSync(PI_NATIVE_ENTRY)) return "dist/agent/pi-native.js missing — run `npm run build` first"; + if (!fs.existsSync(PI_NATIVE_ENTRY)) return "dist/agent/billion-context.js missing — run `npm run build` first"; if (!piAvailable()) return `${PI_BIN} CLI not available`; return false; } diff --git a/tests/e2e/e2e-native-pi.test.ts b/tests/e2e/e2e-native-pi.test.ts index 7f0ee66eb3..151b1606e1 100644 --- a/tests/e2e/e2e-native-pi.test.ts +++ b/tests/e2e/e2e-native-pi.test.ts @@ -4,7 +4,7 @@ // interception, x-bili-plugin stamping, ACP tool registration, plugin-tool // execution (acp_status / compress) and real compression — runs in-process // with zero tokens and no network. Gated by ACP_TEST_E2E_NATIVE=1 (needs -// `npm run build` first — the pi package loads dist/agent/pi-native.js). +// `npm run build` first — the pi package loads dist/agent/billion-context.js). // // Assertions map 1:1 to #1239's acceptance list: // 1. traffic is intercepted + plugin-mode claimed (x-bili-plugin: pi) @@ -34,7 +34,7 @@ import { assertPortDead } from "../port-race.js"; const PI_BIN = process.env.E2E_PI_BIN ?? "pi"; const REPO_ROOT = path.resolve(import.meta.dirname, "../.."); -const PI_NATIVE_ENTRY = path.join(REPO_ROOT, "dist/agent/pi-native.js"); +const PI_NATIVE_ENTRY = path.join(REPO_ROOT, "dist/agent/billion-context.js"); const FAKE_UPSTREAM = path.join(import.meta.dirname, "fake-upstream-chat.mjs"); const TMO = Number(process.env.E2E_TMO ?? 150_000); const WORK_ROOT = path.join(process.cwd(), "tmp"); @@ -112,7 +112,7 @@ const skipReason = !run const suiteSkipReason = skipReason ?? (!distBuilt() - ? "dist/agent/pi-native.js missing — run `npm run build` first" + ? "dist/agent/billion-context.js missing — run `npm run build` first" : undefined); const checkOnly = process.env.E2E_CHECK === "1"; diff --git a/tests/e2e/e2e-pi-codex-ws.test.ts b/tests/e2e/e2e-pi-codex-ws.test.ts index a8df48ae7c..15788e0224 100644 --- a/tests/e2e/e2e-pi-codex-ws.test.ts +++ b/tests/e2e/e2e-pi-codex-ws.test.ts @@ -32,7 +32,7 @@ process.env.NODE_ENV = "test"; const PI_BIN = process.env.E2E_PI_BIN ?? "pi"; const REPO_ROOT = path.resolve(import.meta.dirname, "../.."); -const PI_NATIVE_ENTRY = path.join(REPO_ROOT, "dist/agent/pi-native.js"); +const PI_NATIVE_ENTRY = path.join(REPO_ROOT, "dist/agent/billion-context.js"); const TMO = Number(process.env.E2E_TMO ?? 180_000); const WORK_ROOT = path.join(process.cwd(), "tmp"); fs.mkdirSync(WORK_ROOT, { recursive: true }); @@ -52,7 +52,7 @@ function piAvailable(): boolean { const skipReason = !run ? "set ACP_TEST_E2E_PI_WS=1 (real pi + local mock WS upstream; deterministic, zero tokens)" : !fs.existsSync(PI_NATIVE_ENTRY) - ? "dist/agent/pi-native.js missing — run `npm run build` first" + ? "dist/agent/billion-context.js missing — run `npm run build` first" : !piAvailable() ? `pi binary "${PI_BIN}" not found on PATH` : undefined; diff --git a/tests/fix-679-windows-launch.test.ts b/tests/fix-679-windows-launch.test.ts index 72f35c8712..29fa9a8300 100644 --- a/tests/fix-679-windows-launch.test.ts +++ b/tests/fix-679-windows-launch.test.ts @@ -35,6 +35,14 @@ test("quoteWinToken: whitespace-bearing tokens get wrapped in double quotes", () // embedded-quote tokens before they ever reach this function (see // win-cmd-argv.test.ts). The bare output here documents what the formatter // does with such input, NOT that it is a safe pass-through. +test("quoteWinToken: a trailing backslash run is doubled inside the quotes (MSVCRT 2n -> n)", () => { + assert.equal(quoteWinToken("C:\\My Dir\\"), '"C:\\My Dir\\\\"'); + assert.equal(quoteWinToken("a b\\\\"), '"a b\\\\\\\\"'); + // Backslashes that are not trailing, and space-free tokens, are untouched. + assert.equal(quoteWinToken("C:\\My Docs\\a.txt"), '"C:\\My Docs\\a.txt"'); + assert.equal(quoteWinToken("C:\\bin\\"), "C:\\bin\\"); +}); + test("quoteWinToken: embedded double quotes stay bare at the formatter level (rejected upstream since #2196)", () => { assert.equal(quoteWinToken('key="value"'), 'key="value"'); assert.equal(quoteWinToken('C:\\x "q" y'), 'C:\\x "q" y'); diff --git a/tests/fold-position-reap-integrity.test.ts b/tests/fold-position-reap-integrity.test.ts new file mode 100644 index 0000000000..9b3bbad079 --- /dev/null +++ b/tests/fold-position-reap-integrity.test.ts @@ -0,0 +1,32 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { reconcileFoldCoverage, positionalFingerprint } from '../src/fold-reconcile.js'; +import { SessionStore, _setStoreForTest } from '../src/persist.js'; +import type { Session } from '../src/session.js'; +import type { CoreMessage } from 'acp-kernel'; + +test('reaping a partially surviving fold preserves position fingerprint associations', () => { + _setStoreForTest(new SessionStore({ enabled: false })); + const message = (id: string): CoreMessage => ({ id, role: 'user', contentType: 'text', text: `Unique invented payload ${id}` }); + const covered = ['a','b','c'].map(message); + const tail = Array.from({ length: 12 }, (_, index) => message(`tail-${index}`)); + const session = { + id: 'synthetic-reap-position-integrity', + state: { blocks: [{ active: true, blockId: 'synthetic-fold', effectiveMessageIds: ['a','b','c'] }], messageRefs: { byRaw: {}, byRef: {} } }, + metadata: {}, + } as unknown as Session; + reconcileFoldCoverage(session, [...covered,...tail], { mode: 'repair' }); + const before = session.metadata.foldPositions as { ids: string[]; canon: string[] }; + assert.deepEqual(before.canon, [...covered,...tail].map(positionalFingerprint)); + const surviving = [covered[0],...tail]; + for (let pass = 0; pass < 3; pass++) reconcileFoldCoverage(session, surviving, { mode: 'repair' }); + assert.equal(session.state.blocks.length, 0, 'fixture must actually reap the majority-missing fold'); + const after = session.metadata.foldPositions as { ids: string[]; canon: string[] }; + assert.equal(after.ids.length, after.canon.length, 'reaping must remove each position ID together with its fingerprint'); + const actual = new Map(surviving.map(item => [item.id,positionalFingerprint(item)])); + after.ids.forEach((id,index) => assert.equal(after.canon[index], actual.get(id), 'each surviving fingerprint must belong to its recorded ID')); + reconcileFoldCoverage(session, surviving, { mode: 'repair' }); + const next = session.metadata.foldPositions as { ids: string[]; canon: string[] }; + assert.deepEqual(next.canon, next.ids.map(id => actual.get(id)), + 'the next pass must not cache a shifted fingerprint under the wrong ID'); +}); diff --git a/tests/golden/storage-render/chat.json b/tests/golden/storage-render/chat.json index e0f1b4aa96..ae4923dd78 100644 --- a/tests/golden/storage-render/chat.json +++ b/tests/golden/storage-render/chat.json @@ -95,13 +95,13 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "97b9991ae7a398fea5d620994df0101001d9c3f246615daac95e6afb4cee14a0", - "stateDigestAligned": "fc632ea30ecb84eeb10e2e0bb4d525f06b17ebd60c6296625d71f398b569ed0b", + "stateDigest": "a7b5a67f54a2cbd7978e2a57ad78f522f7af8f94a4e2402898aee4d93479541a", + "stateDigestAligned": "76f701f5ab1631fe1615a3f1cd8fa0fcd0a3fa19c02658cc5d077ae565f4897a", "sectionDigests": { "state": "90857fbf82001a2b", "blockContents": "94a58f96d7c5cfc3", - "stats": "3e6858671f4b9a9d", - "metadata": "faa2b34b71823646" + "stats": "5c2f0fe203d710e8", + "metadata": "0c7d1819e0d4c0f6" }, "stateKeyDigests": { "blocks": "d9770feee91dafc6", diff --git a/tests/handoff-client-abort.test.ts b/tests/handoff-client-abort.test.ts new file mode 100644 index 0000000000..6565092b9c --- /dev/null +++ b/tests/handoff-client-abort.test.ts @@ -0,0 +1,187 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { _resetPluginStateForTest } from "../src/plugin.ts"; + +// #4247 followup: agents that close the socket the instant they hold the +// terminal frame (pi does) used to lose the reply entirely — the handoff save +// was gated on the client socket still being writable. These tests pin the +// rule: a completed upstream turn is persisted even when the client is gone. + +function sse(data: unknown): string { + return `data: ${JSON.stringify(data)}\n\n`; +} + +function roleChunk(): string { + return sse({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt-test", choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null }], usage: { prompt_tokens: 55, completion_tokens: 0, total_tokens: 55 } }); +} + +function textChunk(text: string): string { + return sse({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt-test", choices: [{ index: 0, delta: { content: text }, finish_reason: null }] }); +} + +function terminalChunk(reason = "stop"): string { + return sse({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt-test", choices: [{ index: 0, delta: {}, finish_reason: reason }] }); +} + +interface Harness { + proxyPort: number; + upstreamPort: number; + close(): Promise<void>; +} + +/** Upstream that writes `head` and then holds the stream open — the real + * slow-model shape: the client has its terminal frame while the connection + * is still alive. */ +async function startHarness(head: string[], opts: { hold?: boolean; doneAfterMs?: number } = {}): Promise<Harness> { + const upstream = http.createServer((req, res) => { + req.on("data", () => {}); + req.on("end", () => { + res.writeHead(200, { "content-type": "text/event-stream" }); + for (const line of head) res.write(line); + if (opts.doneAfterMs !== undefined) { + // [DONE] lands after the client has had time to hang up, so the + // pipe's write of that frame hits a dead socket. + setTimeout(() => { if (!res.destroyed) res.write("data: [DONE]\n\n"); if (!res.destroyed) res.end(); }, opts.doneAfterMs); + } else if (!opts.hold) res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + _resetPluginStateForTest(); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: 400_000 } } } }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + return { + proxyPort, + upstreamPort, + close: async () => { + proxy.close(); + proxy.closeIdleConnections?.(); + await once(proxy, "close"); + upstream.close(); + upstream.closeAllConnections?.(); + await once(upstream, "close"); + }, + }; +} + +function request(h: Harness, conv: string, body: Record<string, unknown>): http.ClientRequest { + return http.request({ + host: "127.0.0.1", + port: h.proxyPort, + path: `/bili/http://127.0.0.1:${h.upstreamPort}/v1/chat/completions`, + method: "POST", + headers: { "content-type": "application/json", "x-bili-plugin": "pi", "x-bili-plugin-conversation": conv }, + }); +} + +async function handoffMd(h: Harness, label: string): Promise<string> { + const stats = (await (await fetch(`http://127.0.0.1:${h.proxyPort}/__bili/stats`)).json()) as { sessions: Array<{ id: string; label?: string }> }; + const id = stats.sessions.find((s) => s.label === label)?.id; + assert.ok(id, `session ${label} not registered`); + const detail = (await (await fetch(`http://127.0.0.1:${h.proxyPort}/__bili/sessions/${id}/detail`)).json()) as { handoffMd: string }; + return detail.handoffMd; +} + +function payload(stream: boolean): Record<string, unknown> { + return { + model: "gpt-test", + max_tokens: 1024, + stream, + messages: [{ role: "user", content: "hello" }], + tools: [{ type: "function", function: { name: "read_file", description: "r", parameters: { type: "object", properties: {} } } }], + }; +} + +test("keeps the reply when the client closes right after the terminal frame", async () => { + // Upstream delivers the whole turn and then holds the connection open, so + // the pipe is still reading when the client hangs up — the shape pi leaves. + const h = await startHarness([roleChunk(), textChunk("final answer"), terminalChunk(), "data: [DONE]\n\n"], { hold: true }); + try { + const req = request(h, "handoff-abort-after-terminal", payload(true)); + let seen = ""; + req.on("response", (res) => { + res.on("data", (c: Buffer) => { + seen += c.toString("utf8"); + if (seen.includes("data: [DONE]") && !req.destroyed) { + req.destroy(); + res.destroy(); + } + }); + }); + req.on("error", () => {}); + req.end(JSON.stringify(payload(true))); + const deadline = Date.now() + 5_000; + while (Date.now() < deadline && !seen.includes("data: [DONE]")) await new Promise((r) => setTimeout(r, 25)); + assert.match(seen, /final answer/); + await new Promise((r) => setTimeout(r, 500)); + // give the store/render a beat — the metadata write itself is synchronous + const md = await handoffMd(h, "handoff-abort-after-terminal"); + assert.match(md, /### assistant/); + assert.match(md, /final answer/); + } finally { + await h.close(); + } +}); + +test("does not persist a reply the upstream never terminated", async () => { + const h = await startHarness([roleChunk(), textChunk("half an ans")]); + try { + const req = request(h, "handoff-abort-mid-stream", payload(true)); + let seen = ""; + req.on("response", (res) => { + res.on("data", (c: Buffer) => { + seen += c.toString("utf8"); + if (seen.includes("half an ans") && !req.destroyed) { + req.destroy(); + res.destroy(); + } + }); + }); + req.on("error", () => {}); + req.end(JSON.stringify(payload(true))); + const deadline = Date.now() + 5_000; + while (Date.now() < deadline && !seen.includes("half an ans")) await new Promise((r) => setTimeout(r, 25)); + await new Promise((r) => setTimeout(r, 500)); + const md = await handoffMd(h, "handoff-abort-mid-stream"); + assert.doesNotMatch(md, /half an ans/); + } finally { + await h.close(); + } +}); diff --git a/tests/handoff-response.test.ts b/tests/handoff-response.test.ts index d299cde36e..0beee977ad 100644 --- a/tests/handoff-response.test.ts +++ b/tests/handoff-response.test.ts @@ -13,7 +13,7 @@ async function serve(protocol: string, stream: boolean, chunks: Array<string | B const session = createSession(`handoff-test-${Math.random()}`); session.stats.requests = 1; const server = createServer((_req, res) => { - const reply = opts.capture === false ? undefined : createHandoffReply(session, () => !opts.abort && !res.destroyed); + const reply = opts.capture === false ? undefined : createHandoffReply(session); const input = new ReadableStream<Uint8Array>({ start(controller) { for (const chunk of chunks) controller.enqueue(Buffer.from(chunk)); if (opts.error) controller.enqueue(Buffer.from(event({ type: "error", error: { message: "failed" } }))); diff --git a/tests/hermes-native.test.ts b/tests/hermes-native.test.ts index 378ca1d767..2c3c79dc26 100644 --- a/tests/hermes-native.test.ts +++ b/tests/hermes-native.test.ts @@ -227,7 +227,7 @@ os.environ.pop("BILI_CONFIG_FILE", None) for k in ("BILLION_CONTEXT_ATTACH", "BILLION_CONTEXT_PROXY", "BILI_NATIVE_HERMES", "BILLION_CONTEXT_PLUGIN", "BILI_PROVIDER_REWRITES", "HTTP_PROXY", "http_proxy", "HTTPS_PROXY", "https_proxy", - "ALL_PROXY", "all_proxy", "HERMES_CA_BUNDLE", "SSL_CERT_FILE"): + "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy", "HERMES_CA_BUNDLE", "SSL_CERT_FILE"): os.environ.pop(k, None) RECORDED = {"tool": [], "runtime_info": [], "watcher": [], "health": [], "status": []} @@ -939,17 +939,21 @@ elif SCENARIO == "origin-death": ready.wait(10) out["death_initial_ready"] = (mod._state.get("bootstrap") or {}).get("state") out["death_env_routed"] = os.environ.get("HTTPS_PROXY") + out["death_http_routed"] = os.environ.get("HTTP_PROXY") SLOW["enabled"] = True def release_later(): __import__("time").sleep(1.5) SLOW["release"].set() threading.Thread(target=release_later, daemon=True).start() gap_values = [] + http_gap = [] saw_gap = False end = __import__("time").monotonic() + 20 while __import__("time").monotonic() < end: v = os.environ.get("HTTPS_PROXY") gap_values.append(v) + if v != ORIGIN: + http_gap.append(os.environ.get("HTTP_PROXY")) if v != ORIGIN: saw_gap = True rec = mod._state.get("bootstrap") or {} @@ -960,6 +964,8 @@ elif SCENARIO == "origin-death": rec = mod._state.get("bootstrap") or {} out["death_final_state"] = rec.get("state") out["death_final_env"] = os.environ.get("HTTPS_PROXY") + out["death_http_gap"] = list(dict.fromkeys(http_gap)) + out["death_final_http"] = os.environ.get("HTTP_PROXY") out["death_tools"] = sorted(ctx.tools.keys()) out["death_register_calls"] = sum(ctx.reg_counts.values()) out["death_warnings"] = [w for w in WARNINGS if "is gone" in w] @@ -1119,6 +1125,9 @@ out.update({ "pid": os.getpid(), "env_https_proxy": os.environ.get("HTTPS_PROXY"), "env_https_proxy_lc": os.environ.get("https_proxy"), + "env_http_proxy": os.environ.get("HTTP_PROXY"), + "env_http_proxy_lc": os.environ.get("http_proxy"), + "env_no_proxy": os.environ.get("NO_PROXY"), "env_ca_bundle": os.environ.get("HERMES_CA_BUNDLE"), "env_ssl_cert_file": os.environ.get("SSL_CERT_FILE"), "marker_left": os.path.exists(os.path.join(os.environ["XDG_STATE_HOME"], "billion-context", "proxy-starting")), @@ -1147,6 +1156,9 @@ interface DriverOut { hooks: string[]; env_https_proxy?: string | null; env_https_proxy_lc?: string | null; + env_http_proxy?: string | null; + env_http_proxy_lc?: string | null; + env_no_proxy?: string | null; env_ca_bundle?: string | null; env_ssl_cert_file?: string | null; marker_left?: boolean; @@ -1239,6 +1251,9 @@ interface DriverOut { death_gap_values?: Array<string | null>; death_final_state?: string; death_final_env?: string | null; + death_http_routed?: string | null; + death_http_gap?: Array<string | null>; + death_final_http?: string | null; death_tools?: string[]; death_register_calls?: number; death_warnings?: string[]; @@ -1297,6 +1312,9 @@ describe("python plugin runtime (subprocess)", () => { assert.equal(out!.req_unmutated, true); assert.equal(out!.env_https_proxy, out!.origin); assert.equal(out!.env_https_proxy_lc, out!.origin); + assert.equal(out!.env_http_proxy, out!.origin, "a plain-http provider base_url is routed too"); + assert.equal(out!.env_http_proxy_lc, out!.origin); + assert.equal(out!.env_no_proxy, "127.0.0.1,localhost,::1", "loopback stays direct"); assert.match(out!.env_ca_bundle!, /root-ca\.pem$/); assert.match(out!.env_ssl_cert_file!, /combined-ca\.pem$/, "#1375: ambient trust rides the combined bundle"); const h1 = out!.first_headers!; @@ -1740,6 +1758,10 @@ describe("python plugin runtime (subprocess)", () => { assert.ok(out, err); assert.equal(out!.death_initial_ready, "ready"); assert.equal(out!.death_env_routed, out!.origin); + assert.equal(out!.death_http_routed, out!.origin); + assert.ok(out!.death_http_gap!.length > 0 && !out!.death_http_gap!.includes(out!.origin), + "HTTP_PROXY is unrouted with the HTTPS pair, not left pointing at the dead origin"); + assert.equal(out!.death_final_http, out!.origin); assert.ok(out!.death_gap_values!.includes("http://user-proxy.example:9999"), "the unroute RESTORED the pre-existing user proxy instead of deleting the key"); assert.equal(out!.death_final_state, "ready", "the watchdog re-bootstrapped a healthy origin"); diff --git a/tests/incremental-content-clusters.test.ts b/tests/incremental-content-clusters.test.ts new file mode 100644 index 0000000000..7ad887c8e7 --- /dev/null +++ b/tests/incremental-content-clusters.test.ts @@ -0,0 +1,34 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { anthropicToCore, type BiliMessage } from "acp-kernel/wire"; +import { continueContentClusters } from "../src/incremental-lane.js"; + +test("content clusters match one full conversion across repeated delta boundaries", () => { + const messages = Array.from({ length: 40 }, (_, i) => ({ + role: i % 2 === 0 ? "user" : "assistant", + content: [{ type: "text", text: i % 3 === 0 ? "continue" : "ok", cache_control: { type: "ephemeral" } }], + })); + for (let split = 1; split < messages.length; split++) { + const prefix = anthropicToCore({ messages: messages.slice(0, split) } as never); + const delta = anthropicToCore({ messages: messages.slice(split) } as never); + const before = JSON.stringify(delta.msgs); + const continued = continueContentClusters(prefix.msgs, delta); + const full = anthropicToCore({ messages } as never); + assert.deepEqual([...prefix.msgs, ...continued.msgs].map(({ id }) => id), full.msgs.map(({ id }) => id)); + assert.deepEqual(new Map([...prefix.cacheControls, ...continued.cacheControls]), full.cacheControls); + assert.equal(JSON.stringify(delta.msgs), before, "input messages remain unchanged"); + } +}); + +test("sparse content clusters skip the highest retained suffix; other namespaces remain exact", () => { + const base = "h_0123456789abcdef"; + const msg = (id: string): BiliMessage => ({ id, role: "user", contentType: "text", text: "literal payload", rawAnthropicBlock: { type: "document", source: { data: "exact bytes" } } }); + const delta = { msgs: [msg(base), msg("client-pinned")], cacheControls: new Map([[base, { type: "ephemeral" }]]) }; + const shifted = continueContentClusters([msg(base), msg(`${base}_9`)], delta); + assert.equal(shifted.msgs[0].id, `${base}_10`); + assert.equal(shifted.msgs[0].rawAnthropicBlock, delta.msgs[0].rawAnthropicBlock); + assert.equal(shifted.msgs[1], delta.msgs[1]); + assert.ok(shifted.cacheControls.has(`${base}_10`)); + assert.ok(!shifted.cacheControls.has(base)); + assert.equal(continueContentClusters([msg("client-pinned")], delta), delta); +}); diff --git a/tests/issue2171-zero-byte-truncation-retry.test.ts b/tests/issue2171-zero-byte-truncation-retry.test.ts index 89023135f7..b78d98aa11 100644 --- a/tests/issue2171-zero-byte-truncation-retry.test.ts +++ b/tests/issue2171-zero-byte-truncation-retry.test.ts @@ -55,6 +55,17 @@ function streamOf(events: string[]): ReadableStream<Uint8Array> { }); } +function erroredStream(events: string[]): ReadableStream<Uint8Array> { + const enc = new TextEncoder(); + let i = 0; + return new ReadableStream<Uint8Array>({ + pull(controller) { + if (i < events.length) controller.enqueue(enc.encode(events[i++])); + else controller.error(new Error("simulated upstream read failure")); + }, + }); +} + function chatChunk(delta: Record<string, unknown>, extra: Record<string, unknown> = {}): string { return `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "qwen", choices: [{ index: 0, delta, finish_reason: null }], ...extra })}\n\n`; } @@ -220,6 +231,36 @@ test("chat pipe: a comment-only upstream stream (keep-alive pings, zero data fra assert.ok(!out.join("").includes(ERROR_SENTINEL)); }); +test("OMP-owned read error does not spend the proxy reasoning-only retry", async () => { + const out: string[] = []; + let refetches = 0; + const refetch = (): Promise<ReadableStream<Uint8Array> | null> => { refetches++; return Promise.resolve(streamOf([])); }; + await pipePluginChatWithStrip( + erroredStream([chatChunk({ reasoning_content: "private reasoning" })]), + makeRes(out), "openai", makeSession(), undefined, refetch, "http://up", undefined, undefined, undefined, undefined, undefined, true, + ); + assert.equal(refetches, 0); + assert.ok(out.join("").includes(ERROR_SENTINEL)); +}); + +test("OMP-owned truncation: chat pipe does not spend a proxy retry and returns the in-band error", async () => { + const out: string[] = []; + let refetches = 0; + const refetch = (): Promise<ReadableStream<Uint8Array> | null> => { refetches++; return Promise.resolve(streamOf([])); }; + await pipePluginChatWithStrip(streamOf([]), makeRes(out), "openai", makeSession(), undefined, refetch, "http://up", undefined, undefined, undefined, undefined, undefined, true); + assert.equal(refetches, 0); + assert.ok(out.join("").includes(ERROR_SENTINEL)); +}); + +test("responses pipe: OMP-owned truncation does not spend a proxy retry and returns the in-band error", async () => { + const out: string[] = []; + let refetches = 0; + const refetch = (): Promise<ReadableStream<Uint8Array> | null> => { refetches++; return Promise.resolve(streamOf([])); }; + await pipePluginResponsesWithStrip(streamOf([]), makeRes(out), makeSession(), undefined, refetch, "http://up", undefined, undefined, undefined, undefined, true); + assert.equal(refetches, 0); + assert.ok(out.join("").includes(ERROR_SENTINEL)); +}); + test("responses pipe: a comment-only upstream stream (keep-alive pings, zero data frames) still retries (#2171)", async () => { const out: string[] = []; let refetches = 0; diff --git a/tests/issue2313-probe-forward.test.ts b/tests/issue2313-probe-forward.test.ts index 3cdc538dd9..844b8e4c02 100644 --- a/tests/issue2313-probe-forward.test.ts +++ b/tests/issue2313-probe-forward.test.ts @@ -285,3 +285,41 @@ test("e2e #2313 d: probe 400 without a parseable window arms the fallback shrink await close(); } }); + +test("e2e #2313 e: a probe 400 stating only an input-length RANGE arms the shrink — the DashScope livelock recovers", async () => { + // DashScope compatible-mode rejects an over-long input as "Range of input + // length should be [1, 20000]" — no token/length-subject phrase, so while it + // was unrecognized as an overflow the arm never fired, the probe gate + // re-forwarded the same raw body on every attempt and the session could + // only 400 (each turn burning the client's ~300s stream idle). + const rejectBody = JSON.stringify({ + error: { + message: "<400> InternalError.Algo.InvalidParameter: Range of input length should be [1, 20000]", + type: "invalid_request_error", + param: null, + code: "invalid_parameter_error", + }, + id: "chatcmpl-55bbf071-1040-9261-8cd4-76088d25ee5c", + request_id: "55bbf071-1040-9261-8cd4-76088d25ee5c", + }); + const { ctx, calls, close } = await setup(rejectBody); + try { + await seedStuckMeter(ctx, "i2313-e"); + + const r = await post(ctx, bigMessages()); + assert.equal(r.status, 200, "the stated range is an overflow on the probe path, and the in-request refold recovers"); + await r.text(); + + const forwards = calls.filter((c) => !c.summary); + assert.equal(forwards.length, 3, "turn 1 + raw probe forward + one folded retry"); + assert.ok(calls.some((c) => c.summary), "the refold made summarization call(s)"); + assert.ok(forwards[2]!.contentChars < forwards[1]!.contentChars, "the retry carries the folded payload"); + + const s = getSession("i2313-e"); + assert.ok(s); + assert.equal(s.stats.overflowArmTokens, undefined, "the successful retry's usage report retires the one-shot arm (#1110)"); + assert.equal(s.stats.lastInputTokensSource, "usage", "the folded retry settles the final baseline"); + } finally { + await close(); + } +}); diff --git a/tests/issue2328-terminal-diag.test.ts b/tests/issue2328-terminal-diag.test.ts index 458caec0f6..6bfdcbdfb9 100644 --- a/tests/issue2328-terminal-diag.test.ts +++ b/tests/issue2328-terminal-diag.test.ts @@ -1,245 +1,160 @@ -import { test } from "node:test"; import assert from "node:assert/strict"; +import { test } from "node:test"; +import type { ServerResponse } from "node:http"; import { pipePluginChatWithStrip, pipePluginResponsesWithStrip } from "../src/plugin.ts"; -// #2328 Q2/Q4: the plugin-lane strip pipes now attach termination -// diagnostics (meta) to the in-band upstream_stream_truncated error frame and -// to the log line, and classify the cut: -// - no-terminal-seen: EOF/read-error with no terminal bytes in flight. -// - terminal-bytes-unrecognized: the dangling partial event at the cut -// carries the opening bytes of the wire's own terminal ([DONE], -// message_stop, finishReason chunk, response.completed family). -// This is what makes "the upstream never finished the turn" distinguishable -// from "the terminal arrived but its bytes never completed" (#2347 forensics) -// without needing a raw-SSE dump. - -function makeRes(chunks: string[]) { +function makeRes(writes: string[]): ServerResponse { return { - writes: chunks, - write(b: Buffer | string) { - chunks.push(typeof b === "string" ? b : b.toString("utf8")); + write(chunk: Buffer | string) { + writes.push(typeof chunk === "string" ? chunk : chunk.toString("utf8")); return true; }, - end(b?: Buffer | string) { - if (b !== undefined) chunks.push(typeof b === "string" ? b : b.toString("utf8")); + end(chunk?: Buffer | string) { + if (chunk !== undefined) writes.push(typeof chunk === "string" ? chunk : chunk.toString("utf8")); }, once() {}, destroyed: false, writableEnded: false, - } as unknown as import("node:http").ServerResponse; + } as unknown as ServerResponse; } -function streamOf(events: string[], then?: "error"): ReadableStream<Uint8Array> { - const enc = new TextEncoder(); - let i = 0; +function streamOf(events: string[], failure?: Error): ReadableStream<Uint8Array> { + const encoder = new TextEncoder(); + let index = 0; return new ReadableStream<Uint8Array>({ pull(controller) { - if (i < events.length) { - controller.enqueue(enc.encode(events[i])); - i += 1; - } else if (then === "error") { - controller.error(new Error("upstream socket reset mid-stream")); - } else { - controller.close(); - } + if (index < events.length) controller.enqueue(encoder.encode(events[index++]!)); + else if (failure) controller.error(failure); + else controller.close(); }, }); } -/** Pull the first data-frame JSON that carries the truncation code. */ -function truncationMeta(writes: string[], at: "top" | "nested"): Record<string, unknown> | undefined { - for (const w of writes) { - for (const frame of w.split("\n\n")) { - const m = frame.match(/^(?:event: [^\n]+\n)?data: (.*)$/s); - if (!m) continue; - try { - const ev = JSON.parse(m[1]!) as Record<string, unknown>; - if (at === "top" && ev["code"] === "upstream_stream_truncated") return ev["meta"] as Record<string, unknown>; - if (at === "nested") { - const err = ev["error"] as Record<string, unknown> | undefined; - // google's error object carries a numeric code (503) + - // gRPC status instead of the string code. - if (err && (err["code"] === "upstream_stream_truncated" || err["status"] === "UNAVAILABLE")) return err["meta"] as Record<string, unknown>; - } - } catch { - /* not a JSON frame */ - } +function truncationErrorFrame(writes: string[]): Record<string, unknown> { + for (const frame of writes.join("").split("\n\n")) { + const dataLine = frame.split(/\r?\n/).find((line) => line.startsWith("data: ")); + const data = dataLine?.slice("data: ".length); + if (!data || data === "[DONE]") continue; + try { + const event = JSON.parse(data) as Record<string, unknown>; + const error = event["error"] as Record<string, unknown> | undefined; + const nestedError = error?.["error"] as Record<string, unknown> | undefined; + if (event["code"] === "upstream_stream_truncated" || error?.["code"] === "upstream_stream_truncated" || error?.["status"] === "UNAVAILABLE" || nestedError?.["code"] === "upstream_stream_truncated") return event; + } catch { + continue; } } - return undefined; + throw new Error("Expected an upstream truncation SSE frame"); } -const openaiChunk = (fr: string | null) => `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: fr === null ? { content: "hi" } : {}, finish_reason: fr }] })}\n\n`; - -test("chat pipe (openai): dangling `data: [DO` at EOF classifies as terminal-bytes-unrecognized", async () => { - const out: string[] = []; - const logs: string[] = []; - await pipePluginChatWithStrip( - streamOf([openaiChunk(null), "data: [DO"]), - makeRes(out), - "openai", - undefined, - (m) => logs.push(m), - ); - const meta = truncationMeta(out, "nested"); - assert.ok(meta, "error frame carries meta"); - assert.equal(meta!["classification"], "terminal-bytes-unrecognized"); - assert.equal(meta!["cause"], "eof"); - assert.equal(meta!["protocol"], "openai"); - assert.equal(meta!["eofBufferBytes"], "data: [DO".length); - assert.equal(meta!["eofBufferHead"], "data: [DO"); - assert.deepEqual(meta!["lastEventTypes"], ["chunk"]); - assert.equal(meta!["events"], 1); - assert.ok((meta!["bytes"] as number) > 0); - assert.equal(meta!["retryZeroByteCutSpent"], false); - assert.equal(meta!["retryDegenerateSpent"], false); - // wire shape: openai meta nests inside `error`, and the [DONE] tail still - // follows the error frame (#721 contract unchanged). - assert.ok(out.join("").includes("data: [DONE]")); - // log line: one grep-able line with the classification + full diag. - const line = logs.find((l) => l.includes("upstream stream truncated")); - assert.ok(line, "log line emitted"); - assert.ok(line!.includes("classification=terminal-bytes-unrecognized")); - assert.ok(line!.includes("cause=eof")); - assert.ok(line!.includes("diag=")); -}); +function diagFromLogs(logs: string[]): Record<string, unknown> { + const line = logs.find((entry) => entry.includes("upstream stream truncated")); + const serialized = line?.match(/; diag=(\{.*\})\]$/)?.[1]; + if (!serialized) throw new Error("Expected truncation diagnostics in local logs"); + return JSON.parse(serialized) as Record<string, unknown>; +} -test("chat pipe (anthropic): clean EOF with no terminal classifies as no-terminal-seen", async () => { - const out: string[] = []; - await pipePluginChatWithStrip( - streamOf([ - `event: message_start\ndata: ${JSON.stringify({ type: "message_start" })}\n\n`, - `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "hi" } })}\n\n`, - ]), - makeRes(out), - "anthropic", - ); - const meta = truncationMeta(out, "nested"); - assert.ok(meta, "error frame carries meta"); - assert.equal(meta!["classification"], "no-terminal-seen"); - assert.equal(meta!["cause"], "eof"); - assert.equal(meta!["eofBufferBytes"], 0); - assert.equal(meta!["eofBufferHead"], undefined); - assert.deepEqual(meta!["lastEventTypes"], ["message_start", "content_block_delta"]); - assert.ok((meta!["visibleChars"] as number) >= 2); - assert.ok(out.join("").includes("event: error")); -}); +const openaiChunk = (text: string) => `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { content: text }, finish_reason: null }] })}\n\n`; -test("chat pipe (google): meta rides the UNAVAILABLE error object and counts unparseable forwards", async () => { - const out: string[] = []; - await pipePluginChatWithStrip( - streamOf([ - // one parseable chunk, then one unparseable frame (forwarded raw), - // then EOF with nothing terminal in flight. - `data: ${JSON.stringify({ candidates: [{ index: 0, content: { role: "model", parts: [{ text: "hi" }] } }] })}\n\n`, - "data: {{{not-json\n\n", - ]), - makeRes(out), - "google", - ); - const meta = truncationMeta(out, "nested"); - assert.ok(meta, "error frame carries meta"); - assert.equal(meta!["classification"], "no-terminal-seen"); - assert.equal(meta!["unparseableForwarded"], 1); - assert.deepEqual(meta!["lastEventTypes"], ["chunk", "unparseable"]); - const frame = out.join(""); - // Gemini's error object carries a numeric code + gRPC status, no string - // code (see emitUpstreamTruncation) — UNAVAILABLE identifies the frame. - assert.ok(frame.includes('"status":"UNAVAILABLE"')); - assert.ok(frame.includes('"code":503')); -}); +test("OpenAI truncation keeps transport causes and trace IDs in local logs only", async () => { + const writes: string[] = []; + const logs: string[] = []; + const socketError = Object.assign(new Error("other side closed"), { name: "SocketError", code: "UND_ERR_SOCKET" }); + const failure = new TypeError("terminated", { cause: socketError }); -test("chat pipe: upstreamMeta threads HTTP status/content-type into the diag", async () => { - const out: string[] = []; await pipePluginChatWithStrip( - streamOf([openaiChunk(null)]), - makeRes(out), + streamOf([openaiChunk("partial")], failure), + makeRes(writes), "openai", undefined, + (line) => logs.push(line), undefined, undefined, undefined, undefined, - undefined, - { status: 200, contentType: "text/event-stream" }, + { + status: 200, + contentType: "text/event-stream", + requestId: "01a1bf31-7f88-7c2c-a1b8-62b2ecd1d3a5", + opencodeLogId: "log-7f88", + cfRay: "a57b139e8f1bb5af-LHR", + }, ); - const meta = truncationMeta(out, "nested"); - assert.ok(meta); - assert.equal(meta!["upstreamStatus"], 200); - assert.equal(meta!["upstreamContentType"], "text/event-stream"); -}); -test("responses pipe: dangling response.completed head classifies as terminal-bytes-unrecognized", async () => { - const out: string[] = []; - const sse = (e: string, d: Record<string, unknown>) => `event: ${e}\ndata: ${JSON.stringify({ type: e, ...d })}\n\n`; - await pipePluginResponsesWithStrip( - streamOf([ - sse("response.created", { response: { id: "resp_1" } }), - sse("response.output_item.added", { output_index: 0, item: { type: "message", id: "msg_1" } }), - sse("response.content_part.added", { item_id: "msg_1", output_index: 0 }), - sse("response.output_text.delta", { item_id: "msg_1", output_index: 0, delta: "he" }), - 'event: response.completed\ndata: {"type":"response.comp', - ]), - makeRes(out), - ); - const meta = truncationMeta(out, "top"); - assert.ok(meta, "error frame carries meta"); - assert.equal(meta!["classification"], "terminal-bytes-unrecognized"); - assert.equal(meta!["cause"], "eof"); - assert.equal(meta!["protocol"], "responses"); - assert.ok((meta!["eofBufferHead"] as string).includes("response.comp")); - assert.deepEqual(meta!["lastEventTypes"], [ - "response.created", - "response.output_item.added", - "response.content_part.added", - "response.output_text.delta", - ]); - assert.equal(meta!["heldEvents"], 0); - assert.ok((meta!["visibleChars"] as number) >= 2); + const event = truncationErrorFrame(writes); + const error = event["error"] as Record<string, unknown>; + assert.equal(error["code"], "upstream_stream_truncated"); + assert.equal(error["meta"], undefined); + assert.ok(!writes.join("").includes("other side closed")); + + const diag = diagFromLogs(logs); + assert.deepEqual(diag["readErrorCause"], [{ name: "SocketError", code: "UND_ERR_SOCKET" }]); + assert.equal(diag["upstreamRequestId"], "01a1bf31-7f88-7c2c-a1b8-62b2ecd1d3a5"); + assert.equal(diag["upstreamLogId"], "log-7f88"); + assert.equal(diag["upstreamCfRay"], "a57b139e8f1bb5af-LHR"); + assert.equal(diag["upstreamStatus"], 200); + assert.equal(diag["upstreamContentType"], "text/event-stream"); + assert.ok(!JSON.stringify(diag).includes("other side closed")); }); -test("responses pipe: clean EOF after deltas classifies as no-terminal-seen", async () => { - const out: string[] = []; - const sse = (e: string, d: Record<string, unknown>) => `event: ${e}\ndata: ${JSON.stringify({ type: e, ...d })}\n\n`; +test("Responses EOF records classification locally and emits no diagnostic metadata", async () => { + const writes: string[] = []; + const logs: string[] = []; + const sse = (type: string, data: Record<string, unknown>) => `event: ${type}\ndata: ${JSON.stringify({ type, ...data })}\n\n`; + await pipePluginResponsesWithStrip( streamOf([ sse("response.created", { response: { id: "resp_1" } }), - sse("response.output_item.added", { output_index: 0, item: { type: "message", id: "msg_1" } }), - sse("response.content_part.added", { item_id: "msg_1", output_index: 0 }), - sse("response.output_text.delta", { item_id: "msg_1", output_index: 0, delta: "hi" }), + sse("response.output_text.delta", { output_index: 0, delta: "hi" }), ]), - makeRes(out), + makeRes(writes), + undefined, + (line) => logs.push(line), ); - const meta = truncationMeta(out, "top"); - assert.ok(meta); - assert.equal(meta!["classification"], "no-terminal-seen"); - assert.equal(meta!["eofBufferBytes"], 0); + + const event = truncationErrorFrame(writes); + assert.equal(event["code"], "upstream_stream_truncated"); + assert.equal(event["meta"], undefined); + const diag = diagFromLogs(logs); + assert.equal(diag["classification"], "no-terminal-seen"); + assert.equal(diag["cause"], "eof"); }); -test("responses pipe: upstream read error records cause=read-error", async () => { - const out: string[] = []; - const sse = (e: string, d: Record<string, unknown>) => `event: ${e}\ndata: ${JSON.stringify({ type: e, ...d })}\n\n`; - await pipePluginResponsesWithStrip( - streamOf([sse("response.output_text.delta", { item_id: "msg_1", output_index: 0, delta: "x" })], "error"), - makeRes(out), +test("Google truncation retains its protocol error shape without exposing diagnostics", async () => { + const writes: string[] = []; + const logs: string[] = []; + await pipePluginChatWithStrip( + streamOf([`data: ${JSON.stringify({ candidates: [{ content: { role: "model", parts: [{ text: "hi" }] } }] })}\n\n`]), + makeRes(writes), + "google", + undefined, + (line) => logs.push(line), ); - const meta = truncationMeta(out, "top"); - assert.ok(meta); - assert.equal(meta!["cause"], "read-error"); - assert.equal(meta!["classification"], "no-terminal-seen"); + + const event = truncationErrorFrame(writes); + const error = event["error"] as Record<string, unknown>; + assert.equal(error["code"], 503); + assert.equal(error["status"], "UNAVAILABLE"); + assert.equal(error["meta"], undefined); + assert.equal(diagFromLogs(logs)["classification"], "no-terminal-seen"); }); -test("chat pipe (anthropic): dangling message_stop head classifies as terminal-bytes-unrecognized", async () => { - const out: string[] = []; +test("Anthropic truncation keeps provider error shape without client diagnostics", async () => { + const writes: string[] = []; + const logs: string[] = []; await pipePluginChatWithStrip( - streamOf([ - `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "hi" } })}\n\n`, - "event: message_stop\ndata: {\"type\":\"mess", - ]), - makeRes(out), + streamOf([`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "hi" } })}\n\n`]), + makeRes(writes), "anthropic", + undefined, + (line) => logs.push(line), ); - const meta = truncationMeta(out, "nested"); - assert.ok(meta); - assert.equal(meta!["classification"], "terminal-bytes-unrecognized"); + + const event = truncationErrorFrame(writes); + const error = event["error"] as Record<string, unknown>; + assert.equal(error["code"], "upstream_stream_truncated"); + assert.equal(error["meta"], undefined); + const diag = diagFromLogs(logs); + assert.equal(diag["protocol"], "anthropic"); + assert.equal(diag["cause"], "eof"); }); diff --git a/tests/issue2817-crosswire-persona-fold.test.ts b/tests/issue2817-crosswire-persona-fold.test.ts index 06b756713e..13cf09615b 100644 --- a/tests/issue2817-crosswire-persona-fold.test.ts +++ b/tests/issue2817-crosswire-persona-fold.test.ts @@ -154,7 +154,13 @@ test("issue #2817 regression: identified persona, PLUGIN-MODE fold on openai lan // Phase A — openai lane (commandcode-style). 8 turns ≈ 50KB of history. for (let t = 1; t <= 8; t++) { hist.push({ role: "user", text: `Turn ${t}: audit chapter ${t}. ` + FILLER(t, 6) }); - const resp = await fetch(openaiBase, { method: "POST", headers: dshHeaders, body: JSON.stringify({ model: "probe-model", stream: true, messages: renderOpenai() }) }); + // #5687 follow-up (#5709): the accepted residual ride of a tool-less + // turn 1 now ALSO requires the small pinned one-shot output budget + // (utility sidecars pin 4096; agent main turns omit the field). Turn + // 1 carries it so this fixture keeps exercising the residual path the + // count pins below. + const turn1Body = { model: "probe-model", stream: true, ...(t === 1 ? { max_tokens: 4096 } : {}), messages: renderOpenai() }; + const resp = await fetch(openaiBase, { method: "POST", headers: dshHeaders, body: JSON.stringify(turn1Body) }); assert.equal(resp.status, 200, `openai turn ${t}`); await resp.text(); hist.push({ role: "assistant", text: `Reply about chapter ${t}: acknowledged. ` + FILLER(t, 0.2) }); @@ -193,10 +199,12 @@ test("issue #2817 regression: identified persona, PLUGIN-MODE fold on openai lan assert.ok(blocksAfterFold.length >= 1, "plugin-mode fold landed on the openai lane"); const foldedBody = bodies.openai[bodies.openai.length - 1]!; assert.ok(!foldedBody.includes(EARLY_FILLER_TAG), "covered head content folded away on the openai lane"); - // #5656 residual: turn 1 is a tool-less single-shot shape and rides the - // verbatim side lane (kernel no-touch), so stats.requests counts from - // turn 2 — eight counted turns after nine wire turns. The fork teeth - // below are unchanged: a fork would still show as a missing increment. + // #5656 residual (narrowed by #5687 follow-up / #5709): turn 1 is a + // tool-less single-shot shape WITH the small pinned one-shot budget, so + // it rides the verbatim side lane (kernel no-touch) and stats.requests + // counts from turn 2 — eight counted turns after nine wire turns. The + // fork teeth below are unchanged: a fork would still show as a missing + // increment. assert.equal(main.stats.requests, 8); // Turns 10-12: openai lane stays folded. diff --git a/tests/issue2915-order-centric-reconcile.test.ts b/tests/issue2915-order-centric-reconcile.test.ts index 46c24a54ea..7b81cc5a3f 100644 --- a/tests/issue2915-order-centric-reconcile.test.ts +++ b/tests/issue2915-order-centric-reconcile.test.ts @@ -28,6 +28,7 @@ // again (streak reset clears the latch) — upgrade-in-place recovery. import { test, describe } from "node:test"; import assert from "node:assert/strict"; +import { prune } from "acp-kernel"; import { simhashHex, hammingHex } from "../src/affinity-simhash.ts"; import { normalizedIdentity, @@ -42,7 +43,7 @@ import { } from "../src/fold-reconcile.ts"; import type { CoreMessage } from "acp-kernel"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; -import type { Session } from "../src/session.ts"; +import { createSession, type Session } from "../src/session.ts"; function msg(id: string, role: string, text: string, extra?: Partial<CoreMessage>): CoreMessage { return { id, role, contentType: "text", text, ...extra } as CoreMessage; @@ -116,7 +117,75 @@ function planFor(old: CoreMessage[], next: CoreMessage[], positions?: { ids: str const storedOf = (msgs: CoreMessage[]): { ids: string[]; canon: string[] } => ({ ids: msgs.map((m) => m.id as string), canon: msgs.map((m) => positionalFingerprint(m)) }); +for (const [name, editedIndexes, before, after] of [ + ["changed approval", [5], "Deployment is APPROVED.", "Deployment is FORBIDDEN."], + ["reordered instructions", [5], "Allow Alice; deny Bob.", "Deny Alice; allow Bob."], + ["several reordered instructions", [5, 7, 9, 11], "Allow Alice; deny Bob.", "Deny Alice; allow Bob."], + ["several punctuation edits", [5, 7, 9, 11], "Allow Alice; deny Bob.", "Allow Alice? deny Bob."], +] satisfies Array<[string, number[], string, string]>) { + test(`outgoing history retains ${name} after message ids change`, () => { + const originals = Array.from({ length: 30 }, (_, i): CoreMessage => msg( + `original-${i}`, i % 2 === 0 ? "assistant" : "user", + `Historical context for step ${i}. `.repeat(100) + (editedIndexes.includes(i) ? before : ""), + )); + const covered = Array.from({ length: 10 }, (_, i) => `original-${i + 3}`); + const session = createSession(`instruction-edit-${name}`); + session.state.blocks.push({ + blockId: "instruction-block", runId: "instruction-run", tier: 1, + summary: before, directMessageIds: [...covered], effectiveMessageIds: [...covered], + directBlockIds: [], compressedTokens: 4000, createdAt: 1, + survivedCount: 0, generation: "young", active: true, + }); + reconcileFoldCoverage(session, originals, opts()); + const resent = originals.map((m, i): CoreMessage => ({ + ...m, id: `resent-${i}`, + text: editedIndexes.includes(i) ? (m.text ?? "").replace(before, after) : m.text, + })); + resent.push(msg("continue", "user", "Continue the current task.")); + reconcileFoldCoverage(session, resent, opts()); + const outgoing = prune(resent, session.state); + for (const i of editedIndexes) { + assert.ok(outgoing.some((m) => m.id === `resent-${i}` && m.text?.includes(after)), + `the corrected instruction at ${i} must reach the model`); + } + assert.ok(outgoing.some((m) => m.id === "acp_summary_instruction-block"), + "unchanged history remains compressed"); + for (const i of [3, 4]) { + assert.ok(!outgoing.some((m) => m.id === `resent-${i}`), + "unchanged covered user and assistant messages stay folded"); + } + }); +} + describe("Pass 0 order-centric resync (#2915)", () => { + test("line-wrapped user history stays folded and the next resend is quiet", () => { + const originals = Array.from({ length: 30 }, (_, i): CoreMessage => msg( + `original-${i}`, i % 2 === 0 ? "assistant" : "user", + `Historical context for step ${i}. `.repeat(100), + )); + const covered = originals.slice(3, 13).map((m) => m.id!); + const session = createSession("instruction-line-wrap"); + session.state.blocks.push({ + blockId: "wrapped-block", runId: "wrapped-run", tier: 1, + summary: "Historical steps.", directMessageIds: [...covered], effectiveMessageIds: [...covered], + directBlockIds: [], compressedTokens: 4000, createdAt: 1, + survivedCount: 0, generation: "young", active: true, + }); + reconcileFoldCoverage(session, originals, opts()); + const reshaped = originals.map((m, i): CoreMessage => ({ + ...m, id: `wrapped-${i}`, text: (m.text ?? "").replace(/ /g, "\n"), + })); + reconcileFoldCoverage(session, reshaped, opts()); + const outgoing = prune(reshaped, session.state); + for (let i = 3; i < 13; i++) { + assert.ok(!outgoing.some((m) => m.id === `wrapped-${i}`), "unchanged user and assistant content stays folded"); + } + const logs: string[] = []; + reconcileFoldCoverage(session, reshaped, opts((_level, text) => logs.push(text))); + assert.ok(!logs.some((text) => text.includes("drifted") || text.includes("destroyed")), + "an unchanged resend must not keep reporting drift"); + }); + test("#2821 replay: full id re-mint + changed preamble + appended turn — order carries, one resync, no mass loss", () => { const old = history(); const next = remint(old, "g1", true, "brand new request turn"); @@ -130,21 +199,17 @@ describe("Pass 0 order-centric resync (#2915)", () => { assert.equal(plan.byTwin, 0, "positional coverage leaves nothing for the twin pass"); }); - test("replacement island mid-history: resync claims both flanks, the bracket tier claims the slot (owner rule)", () => { + test("replacement user message stays unmatched while resync preserves both flanks", () => { const old = history(); const next = remint(old, "g1"); next[5] = msg("g1_5", "user", "EDITED: content genuinely changed"); // index 5 island (m3, prose) const plan = planFor(old, next, storedOf(old)); - // the island content is neither normalized-equal nor simhash-close - // (JSON vs prose ≈ 30 bits), but it sits at the SAME INDEX between - // verified claims — owner rule: flanks match → the fold asserts the - // slot, not the stale bytes. assert.ok(plan.resyncs >= 1, `expected resyncs, got ${plan.resyncs}`); assert.equal(plan.byPos, 11); - assert.equal(plan.byOrder, 1, "the bracket tier claims the island slot"); - assert.deepEqual(plan.unmatched, [], "no island loss"); + assert.equal(plan.byOrder, 0, "neighboring messages cannot authorize hiding a user edit"); + assert.deepEqual(plan.unmatched, ["m3"], "the changed user message re-enters the wire"); assert.deepEqual(plan.claims.get("end"), ["g1_11"], "the tail span past the island keeps coverage"); - assert.deepEqual(plan.claims.get("m3"), ["g1_5"]); + assert.equal(plan.claims.has("m3"), false); }); test("insertion (net offset) still lands on the flank scans — resync never mispairs shifted cells", () => { @@ -301,13 +366,8 @@ describe("gap resolution tiers (owner rule 2026-10-12: any channel → flanks "The quick brown fox jumps over the lazy dog near the river bank at dawn while birds chirp"); const plan = planFor(old, next, storedOf(old)); assert.equal(plan.bySim, 0, "a word change is never swallowed by the simhash tier"); - // The bracket tier (same index, fenced between verified flanks — the - // owner-sanctioned delete+insert tradeoff, #2821) may still claim it: - // order evidence, not content similarity, is the carrier there. - const claimed = plan.byOrder === 1; - assert.ok(claimed || plan.unmatched.includes("m4"), - "falls through to the bracket tier or honest unmatched — never bySim"); - if (claimed) assert.deepEqual(plan.claims.get("m4"), ["g1_7"]); + assert.equal(plan.byOrder, 1, "verified flanks pin the non-user slot to the order bracket"); + assert.deepEqual(plan.claims.get("m4"), ["g1_7"]); }); test("mid-column deletion (#2915 review gap ③): flanks absorb the shift, the deleted id stays honestly unmatched", () => { @@ -323,15 +383,15 @@ describe("gap resolution tiers (owner rule 2026-10-12: any channel → flanks assert.deepEqual(plan.claims.get("m4"), ["g1_6"]); }); - test("delete + insert at the same index: the bracket claims the slot (owner-sanctioned stale-cell tradeoff)", () => { + test("delete + insert of a user message at the same index leaves the replacement visible", () => { const old = history(); const next = remint(old, "g1"); next[5] = msg("g1_5", "user", "a genuinely different replacement turn"); // m3 replaced in place const plan = planFor(old, next, storedOf(old)); assert.equal(plan.byPos, 11); - assert.equal(plan.byOrder, 1, "same index, verified claims on BOTH sides → assert the slot"); - assert.deepEqual(plan.unmatched, []); - assert.deepEqual(plan.claims.get("m3"), ["g1_5"], "coverage rides the slot — documented tradeoff, not a guess"); + assert.equal(plan.byOrder, 0); + assert.deepEqual(plan.unmatched, ["m3"]); + assert.equal(plan.claims.has("m3"), false, "the old summary does not cover a new user instruction"); }); }); diff --git a/tests/issue3250-wal-checkpoint.test.ts b/tests/issue3250-wal-checkpoint.test.ts index e989760ae6..d5affc667d 100644 --- a/tests/issue3250-wal-checkpoint.test.ts +++ b/tests/issue3250-wal-checkpoint.test.ts @@ -334,3 +334,12 @@ test("#3250: a configured cap flows open() → journal_size_limit (custom limit rmBestEffort(dir); } }); + +test("#6323: auto_vacuum precedes WAL so fresh databases honor INCREMENTAL", () => { + const { db } = openSqliteDatabase(":memory:"); + for (const pragma of STORAGE_PRAGMAS) db.exec(pragma); + migrateSchema(db); + const row = db.prepare("PRAGMA auto_vacuum").get() as Record<string, unknown>; + assert.equal(Object.values(row)[0], 2, "auto_vacuum should be INCREMENTAL (2)"); + db.close(); +}); diff --git a/tests/issue3905-announcement-tail.test.ts b/tests/issue3905-announcement-tail.test.ts new file mode 100644 index 0000000000..be0bf43b4c --- /dev/null +++ b/tests/issue3905-announcement-tail.test.ts @@ -0,0 +1,420 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { Config, CoreMessage } from "acp-kernel"; +import { createCore, createInitialState } from "acp-kernel"; +import { pipePluginChatWithStrip, pipePluginResponsesWithStrip } from "../src/plugin.ts"; +import { endsWithAnnouncement, endsWithDraftClose } from "../src/degenerate-turn.ts"; +import type { Session } from "../src/session.ts"; +import { runCompressLoop, createOpenaiAdapter } from "../src/loop/index.ts"; +import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; + +// #3905: a terminal turn whose ENTIRE visible output is a short promise of the +// next action ("Running verify after formatting:") ending on a colon or +// ellipsis, with no tool call and a clean stop, used to read as a healthy +// completed turn — the model announced the action instead of issuing it, and +// the session stalls until the user types something (verbatim production stub: +// ses_eb6da0d60ffeYpnNQDoGz000Pc, deepseek-v4.1-flash combo member, 24 output +// tokens / 32 visible chars, finish=stop). All three terminal-verdict lanes +// must treat it as non-converged and spend their one-shot continuation nudge +// on it: the plugin chat pipe, the plugin responses pipe, and the compress +// loop — mirroring the #2303 draft-tail suite. + +// ---------- detector unit ---------- + +const ANNOUNCE_STUB = "Running verify after formatting:"; + +test("endsWithAnnouncement: the production stubs and their ellipsis forms", () => { + assert.equal(endsWithAnnouncement("\n" + ANNOUNCE_STUB), true, "leading newline is stream residue"); + assert.equal(endsWithAnnouncement(ANNOUNCE_STUB), true); + assert.equal(endsWithAnnouncement("Now I'll check the three suites:"), true); + assert.equal(endsWithAnnouncement("Let me look at that output\u2026"), true, "unicode ellipsis"); + assert.equal(endsWithAnnouncement("Let me look at that output..."), true, "ascii ellipsis"); + assert.equal(endsWithAnnouncement(`${ANNOUNCE_STUB} \r\n`), true, "trailing whitespace"); +}); + +test("endsWithAnnouncement: legitimate short answers ending on a colon or ellipsis are NOT stubs", () => { + assert.equal(endsWithAnnouncement(""), false); + assert.equal(endsWithAnnouncement(" \n\t "), false, "whitespace-only is the empty-turn shape, not this one"); + assert.equal(endsWithAnnouncement("All done."), false, "a normal sentence end is not a promise"); + assert.equal(endsWithAnnouncement("Deleted."), false); + assert.equal(endsWithAnnouncement("The answer is 42:"), false, "colon without an action-intent cue is a complete short answer"); + assert.equal(endsWithAnnouncement("I'm not sure\u2026"), false, "ellipsis without a cue is prose"); + assert.equal(endsWithAnnouncement("I'm not sure..."), false); + assert.equal(endsWithAnnouncement("Here is how to run it:"), false, "base-form 'run' inside an explanation is not a cue (progressive forms + intent phrases only)"); +}); + +test("endsWithAnnouncement: the 400-char cap stays the length boundary once a cue is present", () => { + const pad = (total: number) => `${ANNOUNCE_STUB.slice(0, -1)}${"a".repeat(total - ANNOUNCE_STUB.length)}:`; + assert.equal(endsWithAnnouncement(pad(400)), true, "exactly 400 chars is still inside the cap"); + assert.equal(endsWithAnnouncement(pad(401)), false, "401 chars is past the cap"); + assert.equal(endsWithAnnouncement("y".repeat(400)), false, "no promise tail at all"); +}); + +test("endsWithAnnouncement: an unclosed code fence is the truncation class, not a stall (#1755/#2190)", () => { + assert.equal(endsWithAnnouncement("Running the tests:\n```\nstep one:"), false, "odd fence count — cut inside a code block"); + assert.equal(endsWithAnnouncement("Running the tests:\n```\nconst a = 1;\n```"), false, "closed fence, and no promise tail either"); +}); + +test("endsWithAnnouncement: disjoint from endsWithDraftClose", () => { + assert.equal(endsWithAnnouncement("handoff draft\n</summary>"), false, "#2303 owns the draft-close shape"); + assert.equal(endsWithDraftClose(ANNOUNCE_STUB), false, "#3905 owns the announcement shape"); +}); + +// ---------- shared fixtures ---------- + +function makeSession(): Session { + return { + id: "testsess", + protocol: "openai", + upstreamOrigin: "http://127.0.0.1:9/v1", + label: "test", + createdAt: 0, + lastUsedAt: 0, + requests: 0, + lastInputTokens: 0, + stats: {}, + dirty: false, + } as unknown as Session; +} + +function makeRes(chunks: string[]) { + return { + writes: chunks, + write(b: Buffer | string) { + chunks.push(typeof b === "string" ? b : b.toString("utf8")); + return true; + }, + end(b?: Buffer | string) { + if (b !== undefined) chunks.push(typeof b === "string" ? b : b.toString("utf8")); + }, + once() {}, + destroyed: false, + writableEnded: false, + } as unknown as import("node:http").ServerResponse; +} + +function streamOf(events: string[]): ReadableStream<Uint8Array> { + const enc = new TextEncoder(); + let i = 0; + return new ReadableStream<Uint8Array>({ + pull(controller) { + if (i < events.length) { + controller.enqueue(enc.encode(events[i])); + i += 1; + } else { + controller.close(); + } + }, + }); +} + +function chatChunk(delta: Record<string, unknown>): string { + return `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "qwen", choices: [{ index: 0, delta, finish_reason: null }] })}\n\n`; +} + +const DONE = "data: [DONE]\n\n"; + +function chatStop(reason = "stop"): string { + return `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "qwen", choices: [{ index: 0, delta: {}, finish_reason: reason }] })}\n\n`; +} + +const sse = (event: string, data: unknown): string => `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; + +/** The production shape (sanitized): the whole visible output is the one-line + * promise, delivered as the message's text with no tool call. */ +function announceTurn(): string[] { + return [chatChunk({ role: "assistant" }), chatChunk({ content: `\n${ANNOUNCE_STUB}` }), chatStop(), DONE]; +} + +function proseTurn(text: string): string[] { + return [chatChunk({ role: "assistant" }), chatChunk({ content: text }), chatStop(), DONE]; +} + +function textDeltas(raw: string): string { + return [...raw.matchAll(/"content":"((?:[^"\\]|\\.)*)"/g)].map((m) => JSON.parse(`"${m[1]}"`) as string).join(""); +} + +// ---------- plugin chat pipe (the DSH native evidence lane) ---------- + +test("#3905 chat(openai): bare-announcement terminal turn spends the continuation nudge once", async () => { + const out: string[] = []; + const logs: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(proseTurn("real action issued after the nudge"))); + }; + await pipePluginChatWithStrip(streamOf(announceTurn()), makeRes(out), "openai", makeSession(), (m) => logs.push(m), refetch); + const text = out.join(""); + assert.equal(calls, 1, "exactly one re-issue"); + assert.ok(textDeltas(text).includes(ANNOUNCE_STUB), "the first attempt's stub still reaches the client"); + assert.ok(textDeltas(text).endsWith("real action issued after the nudge"), "the retry's content appends to the same stream"); + assert.equal((text.match(/\[DONE\]/g) ?? []).length, 1, "one turn, one terminal"); + assert.equal((text.match(/"finish_reason":"stop"/g) ?? []).length, 1, "the first attempt's terminal is dropped, not doubled"); + assert.ok(logs.some((l) => l.includes("#3905")), `the distinct #3905 log line fired, got: ${JSON.stringify(logs)}`); +}); + +test("#3905 chat: a legitimate short answer ending on a colon is NOT retried", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(proseTurn("unwanted"))); + }; + await pipePluginChatWithStrip(streamOf(proseTurn("The answer is 42:")), makeRes(out), "openai", makeSession(), undefined, refetch); + assert.equal(calls, 0, "no action-intent cue — the delivered answer is never duplicated or re-billed"); +}); + +test("#3905 chat: a legitimate short answer ending on an ellipsis is NOT retried", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(proseTurn("unwanted"))); + }; + await pipePluginChatWithStrip(streamOf(proseTurn("I'm not sure\u2026")), makeRes(out), "openai", makeSession(), undefined, refetch); + assert.equal(calls, 0, "ellipsis without a cue is prose, not a promise"); +}); + +test("#3905 chat: a bare announcement WITH a tool call is NOT retried", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(proseTurn("unwanted"))); + }; + const events = [ + chatChunk({ role: "assistant" }), + chatChunk({ content: ANNOUNCE_STUB }), + `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "qwen", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "compress", arguments: "{}" } }] }, finish_reason: null }] })}\n\n`, + chatStop("tool_calls"), + DONE, + ]; + await pipePluginChatWithStrip(streamOf(events), makeRes(out), "openai", makeSession(), undefined, refetch); + assert.equal(calls, 0, "a tool call means the step converges"); + assert.ok(out.join("").includes("compress"), "the agent's own tool call still passes through"); +}); + +test("#3905 chat: an announcement RETRY degenerates into the in-band error (#870 path)", async () => { + const out: string[] = []; + const refetch = () => Promise.resolve(streamOf(announceTurn())); + await pipePluginChatWithStrip(streamOf(announceTurn()), makeRes(out), "openai", makeSession(), undefined, refetch); + const text = out.join(""); + // #5958 (771d5d908, landed after this PR's base): the double-degenerate + // terminal is shaped by compat.streamErrorShape, default "protocol" → a + // native error frame ("stream_error") instead of the legacy in-band + // "[ACP] stream error" completion shape. The client is told either way. + assert.ok( + text.includes("stream_error") && text.includes("degenerated again"), + "the client is told, rather than left with a second silent announcement", + ); +}); + +// ---------- plugin responses pipe ---------- + +function responsesAnnounceTurn(responseId = "resp_1", itemId = "item_1"): string[] { + const text = ANNOUNCE_STUB; + return [ + sse("response.created", { type: "response.created", response: { id: responseId, status: "in_progress" } }), + sse("response.output_item.added", { type: "response.output_item.added", output_index: 0, item: { id: itemId, type: "message", content: [] } }), + sse("response.content_part.added", { type: "response.content_part.added", item_id: itemId, output_index: 0, part: { type: "output_text", text: "" } }), + sse("response.output_text.delta", { type: "response.output_text.delta", item_id: itemId, output_index: 0, delta: text }), + sse("response.output_text.done", { type: "response.output_text.done", item_id: itemId, output_index: 0, text }), + sse("response.output_item.done", { type: "response.output_item.done", output_index: 0, item: { id: itemId, type: "message", content: [{ type: "output_text", text }] } }), + sse("response.completed", { type: "response.completed", response: { id: responseId, status: "completed", output: [{ id: itemId, type: "message", content: [{ type: "output_text", text }] }] } }), + ]; +} + +function responsesProseTurn(text: string, responseId = "resp_2", itemId = "item_2"): string[] { + return [ + sse("response.created", { type: "response.created", response: { id: responseId, status: "in_progress" } }), + sse("response.output_item.added", { type: "response.output_item.added", output_index: 0, item: { id: itemId, type: "message", content: [] } }), + sse("response.content_part.added", { type: "response.content_part.added", item_id: itemId, output_index: 0, part: { type: "output_text", text: "" } }), + sse("response.output_text.delta", { type: "response.output_text.delta", item_id: itemId, output_index: 0, delta: text }), + sse("response.output_text.done", { type: "response.output_text.done", item_id: itemId, output_index: 0, text }), + sse("response.output_item.done", { type: "response.output_item.done", output_index: 0, item: { id: itemId, type: "message", content: [{ type: "output_text", text }] } }), + sse("response.completed", { type: "response.completed", response: { id: responseId, status: "completed", output: [{ id: itemId, type: "message", content: [{ type: "output_text", text }] }] } }), + ]; +} + +function responsesDeltas(raw: string): string { + return [...raw.matchAll(/"delta":"((?:[^"\\]|\\.)*)"/g)].map((m) => JSON.parse(`"${m[1]}"`) as string).join(""); +} + +test("#3905 responses: bare-announcement completed turn spends the continuation nudge once", async () => { + const out: string[] = []; + const logs: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(responsesProseTurn("recovered after the nudge"))); + }; + await pipePluginResponsesWithStrip(streamOf(responsesAnnounceTurn()), makeRes(out), makeSession(), (m) => logs.push(m), refetch); + const text = out.join(""); + assert.equal(calls, 1, "exactly one re-issue"); + assert.ok(responsesDeltas(text).includes(ANNOUNCE_STUB), "the first attempt's stub still reaches the client"); + assert.ok(responsesDeltas(text).endsWith("recovered after the nudge"), "the retry's prose appends to the same item"); + assert.equal((text.match(/"type":"response\.created"/g) ?? []).length, 1, "the retry does not open a second response"); + assert.equal((text.match(/"type":"response\.completed"/g) ?? []).length, 1, "one turn, one terminal"); + assert.ok(!text.includes("item_2") && !text.includes("resp_2"), `the retry's ids are rewritten onto the client's, got: ${text}`); + assert.ok(logs.some((l) => l.includes("#3905")), `the distinct #3905 log line fired, got: ${JSON.stringify(logs)}`); +}); + +test("#3905 responses: a legitimate short answer ending on an ellipsis is NOT retried", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(responsesProseTurn("unwanted"))); + }; + await pipePluginResponsesWithStrip(streamOf(responsesProseTurn("I'm not sure...")), makeRes(out), makeSession(), undefined, refetch); + assert.equal(calls, 0, "ellipsis without a cue is prose, not a promise"); +}); + +test("#3905 responses: a bare announcement WITH a function call is NOT retried", async () => { + const out: string[] = []; + let calls = 0; + const refetch = () => { + calls += 1; + return Promise.resolve(streamOf(responsesProseTurn("unwanted"))); + }; + const events = [ + sse("response.created", { type: "response.created", response: { id: "resp_1", status: "in_progress" } }), + sse("response.output_item.added", { type: "response.output_item.added", output_index: 0, item: { id: "item_1", type: "function_call", name: "read" } }), + sse("response.output_text.delta", { type: "response.output_text.delta", item_id: "item_1", output_index: 0, delta: ANNOUNCE_STUB }), + sse("response.completed", { type: "response.completed", response: { id: "resp_1", status: "completed", output: [{ id: "item_1", type: "function_call" }] } }), + ]; + await pipePluginResponsesWithStrip(streamOf(events), makeRes(out), makeSession(), undefined, refetch); + assert.equal(calls, 0, "a function call means the step converges"); +}); + +// ---------- compress loop (proxy mode) ---------- + +function makeCtx(id: string, protocol: "responses" | "openai" = "openai") { + return { + core: createCore(), + config: { modelContextLimit: 200000 } as Config, + messages: [] as CoreMessage[], + session: { + id, + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + } as unknown as Session, + log: () => {}, + protocol, + }; +} + +function openaiSse(frames: Array<Record<string, unknown>>): string { + return frames.map((f) => `data: ${JSON.stringify(f)}\n\n`).join("") + "data: [DONE]\n\n"; +} + +const OPENAI_ANNOUNCE = openaiSse([ + { id: "chatcmpl_a1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_a1", choices: [{ index: 0, delta: { content: ANNOUNCE_STUB }, finish_reason: null }] }, + { id: "chatcmpl_a1", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }, +]); + +const OPENAI_GOOD = openaiSse([ + { id: "chatcmpl_g1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_g1", choices: [{ index: 0, delta: { content: "continued after nudge" }, finish_reason: null }] }, + { id: "chatcmpl_g1", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }, +]); + +async function drainOpenai(first: string, retries: string[], id: string): Promise<{ out: string; fetchCalls: number; bodies: string[] }> { + let fetchCalls = 0; + const bodies: string[] = []; + const orig = globalThis.fetch; + globalThis.fetch = (async (_url: unknown, init?: { body?: unknown }) => { + fetchCalls++; + if (init?.body !== undefined) bodies.push(typeof init.body === "string" ? init.body : String(init.body)); + const body = retries[fetchCalls - 1] ?? ""; + return new Response(body, { status: 200 }); + }) as typeof fetch; + const chunks: Buffer[] = []; + try { + const ctx = makeCtx(id, "openai"); + for await (const chunk of runCompressLoop( + new Response(first, { status: 200 }).body!, + ctx, + { model: "deepseek-chat", stream: true }, + { url: "http://mock", headers: {} }, + createOpenaiAdapter({ model: "deepseek-chat", stream: true }), + buildCompressSystemPrompt(), + )) { + chunks.push(chunk); + } + } finally { + globalThis.fetch = orig; + } + return { out: Buffer.concat(chunks).toString("utf8"), fetchCalls, bodies }; +} + +test("#3905 loop: a bare announcement → one invisible retry with the continuation nudge", async () => { + const { out, fetchCalls, bodies } = await drainOpenai(OPENAI_ANNOUNCE, [OPENAI_GOOD], "at-l1"); + assert.equal(fetchCalls, 1, "exactly one announcement-tail auto-retry"); + assert.ok(bodies[0]!.includes("ended without a tool call"), "the retry carries the continuation nudge"); + assert.ok(out.includes(ANNOUNCE_STUB), "the stub itself reached the client (the host already rendered it)"); + assert.ok(out.includes("continued after nudge"), "the retried turn's content was delivered"); +}); + +test("#3905 loop: one-shot bound — an announcement RETRY is not retried again", async () => { + const { fetchCalls } = await drainOpenai(OPENAI_ANNOUNCE, [OPENAI_ANNOUNCE], "at-l2"); + assert.equal(fetchCalls, 1, "one retry max; a second announcement falls through to the plain completion"); +}); + +test("#3905 loop: a cue-bearing answer PAST the 400-char cap is NOT retried", async () => { + // 441 chars with a real action cue ("Running") and a colon tail: ONLY the + // length cap can deny it, so this pins the cap itself at loop level. + const longCueAnswer = ("Running through the failing suites: " + "lorem ipsum dolor sit amet ".repeat(15)).trimEnd() + ":"; + assert.ok(longCueAnswer.length > 400, `fixture drifted: ${longCueAnswer.length}`); + const longTurn = openaiSse([ + { id: "chatcmpl_n1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_n1", choices: [{ index: 0, delta: { content: longCueAnswer }, finish_reason: null }] }, + { id: "chatcmpl_n1", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }, + ]); + const { fetchCalls } = await drainOpenai(longTurn, [OPENAI_GOOD], "at-l3"); + assert.equal(fetchCalls, 0, "the 400-char cap keeps real prose out of the retry"); +}); + +test("#3905 loop: a normal completed turn is NOT retried", async () => { + const normal = openaiSse([ + { id: "chatcmpl_z1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_z1", choices: [{ index: 0, delta: { content: "all steps finished, nothing pending" }, finish_reason: null }] }, + { id: "chatcmpl_z1", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }, + ]); + const { fetchCalls } = await drainOpenai(normal, [OPENAI_GOOD], "at-l4"); + assert.equal(fetchCalls, 0); +}); + +test("#3905 loop: an announcement WITH a tool call is NOT retried", async () => { + const withCall = openaiSse([ + { id: "chatcmpl_t1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_t1", choices: [{ index: 0, delta: { content: ANNOUNCE_STUB }, finish_reason: null }] }, + { id: "chatcmpl_t1", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "bash", arguments: "{}" } }] }, finish_reason: null }] }, + { id: "chatcmpl_t1", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] }, + ]); + const { fetchCalls } = await drainOpenai(withCall, [OPENAI_GOOD], "at-l5"); + assert.equal(fetchCalls, 0, "a tool call means the step converges"); +}); + +test("#3905 loop: a length-cut announcement retries like the #2303 sibling", async () => { + const lengthCut = openaiSse([ + { id: "chatcmpl_l1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "" }, finish_reason: null }] }, + { id: "chatcmpl_l1", choices: [{ index: 0, delta: { content: ANNOUNCE_STUB }, finish_reason: null }] }, + { id: "chatcmpl_l1", choices: [{ index: 0, delta: {}, finish_reason: "length" }] }, + ]); + const { fetchCalls } = await drainOpenai(lengthCut, [OPENAI_GOOD], "at-l6"); + // Verified against the shipped #2303 implementation: a draft tail ending on + // finish=length is retried there too (fetchCalls === 1). The loop's gate + // excludes only failed/incomplete/error, so `length` is in scope for both + // shapes — matching the sibling rather than inventing a stricter rule. + assert.equal(fetchCalls, 1, "matches the #2303 sibling's treatment of finish=length"); +}); diff --git a/tests/issue4438-dsh-review-lane-isolation.test.ts b/tests/issue4438-dsh-review-lane-isolation.test.ts index 7529547617..88f32d64ee 100644 --- a/tests/issue4438-dsh-review-lane-isolation.test.ts +++ b/tests/issue4438-dsh-review-lane-isolation.test.ts @@ -224,8 +224,12 @@ test("unit: isDshAutoReviewRequest vetoes every main-turn shape (#4438)", () => assert.equal(isDshAutoReviewRequest("openai", { ...reviewBody(true), tools: [{ type: "function", function: { name: "bash" } }] }), false, "tools present"); // Multi-turn history (any assistant/tool turn) vetoes. assert.equal(isDshAutoReviewRequest("openai", { model: MODEL, messages: [{ role: "system", content: REVIEW_POLICY }, { role: "user", content: "a" }, { role: "assistant", content: "b" }, { role: "user", content: "c" }] }), false, "assistant turn in history"); - // Two user messages vetoes (the review is ONE freshly-flattened blob). - assert.equal(isDshAutoReviewRequest("openai", { model: MODEL, messages: [{ role: "system", content: REVIEW_POLICY }, { role: "user", content: "a" }, { role: "user", content: "b" }] }), false, "two user messages"); + // #6104: two+ user messages with NO assistant/tool turn is dsh 0.2.2's + // history-replaying review body (reviewMessages() flattens the whole + // conversation into role:"user" text blobs), not a main turn — a real + // resend always carries assistant turns (the case directly above), and a + // first main turn carries exactly one user message. + assert.equal(isDshAutoReviewRequest("openai", { model: MODEL, messages: [{ role: "system", content: REVIEW_POLICY }, { role: "user", content: "a" }, { role: "user", content: "b" }] }), true, "two user messages, no assistant turn: the #6104 review body"); // No system carrier at all — the prefix must ride a SYSTEM carrier. assert.equal(isDshAutoReviewRequest("openai", { model: MODEL, messages: [{ role: "user", content: "blob" }] }), false, "no system carrier"); // The prefix quoted inside the USER blob is not a policy carrier. diff --git a/tests/issue4452-markerless-reject.test.ts b/tests/issue4452-markerless-reject.test.ts index 817cbda1bb..d3bd1c9749 100644 --- a/tests/issue4452-markerless-reject.test.ts +++ b/tests/issue4452-markerless-reject.test.ts @@ -2,18 +2,27 @@ import test from "node:test"; import assert from "node:assert/strict"; import { inspectContextOverflow, isMarkerlessOverflowReject } from "../src/util.ts"; -// #4452: an intermediate gateway that rewrites every overflow rejection into one +// #4452: an intermediate gateway that rewrites a payload-size rejection into one // generic body (Cherry Studio strips the token counts / window) defeats the // overflow MARKER gate — inspectContextOverflow returns isOverflow:false, so the // whole self-heal never engages and the route starves of calibration. The relay // must surface that shape explicitly (isMarkerlessOverflowReject) instead of the -// opaque `← upstream 400` line. Gate discipline (#3617): assert BOTH directions — -// the marker-less shape IS flagged, and anything self-heal already handles (a real -// marker, a 5xx, a non-overflow 4xx) is NOT. +// opaque `← upstream 413` line. Gate discipline (#3617): assert BOTH directions — +// the marker-less SIZE verdict (413) IS flagged, and anything self-heal already +// handles (a real marker, a 5xx, a non-size 4xx) is NOT. +// +// #4452 follow-up: the generic 400 is NOT a size verdict. Every ordinary +// malformed / schema-invalid / role-rejected request across relays answers with +// the SAME opaque body, so diagnosing an unmarked 400 as markerless overflow +// manufactured a false overflow warning for requests that carry no evidence of +// being too large at all. // Verbatim from the #4438/#4452 field report — identical for every request id. const GENERIC_BODY = '{"error":{"message":"Internal server error","type":"invalid_request_error","code":"bad_request"}}'; +// A schema/malformed 400 body in the shape a strict backend emits. +const MALFORMED_BODY = '{"error":{"message":"Invalid request: messages[3].content must be a string or an array","type":"invalid_request_error","code":"invalid_value"}}'; + const OPENAI_OVERFLOW_BODY = JSON.stringify({ error: { message: @@ -25,35 +34,53 @@ const OPENAI_OVERFLOW_BODY = JSON.stringify({ const CJK_OVERFLOW_BODY = JSON.stringify({ error: { message: "上下文长度超出限制", type: "invalid_request_error" } }); -test("generic gateway body on 400 is overflow-shaped but carries no parseable marker", () => { +test("generic gateway body on 413 is the markerless SIZE verdict and is flagged", () => { // The first-order failure: the marker gate itself says "not overflow". + assert.equal(inspectContextOverflow(413, GENERIC_BODY).isOverflow, false); + // …yet a 413 whose body dropped the marker is exactly the shape the relay + // must flag as self-heal-starved. + assert.equal(isMarkerlessOverflowReject(413, GENERIC_BODY), true); +}); + +test("an unmarked 400 is NOT diagnosed as overflow (generic malformed/schema rejection)", () => { + // Neither body is overflow evidence: no status semantics and no marker. assert.equal(inspectContextOverflow(400, GENERIC_BODY).isOverflow, false); - // …yet it is exactly the shape the relay must now flag as self-heal-starved. - assert.equal(isMarkerlessOverflowReject(400, GENERIC_BODY), true); + assert.equal(isMarkerlessOverflowReject(400, GENERIC_BODY), false); + // The same must hold for a clearly schema-shaped 400 body. + assert.equal(inspectContextOverflow(400, MALFORMED_BODY).isOverflow, false); + assert.equal(isMarkerlessOverflowReject(400, MALFORMED_BODY), false); }); -test("generic gateway body on 413 is also flagged", () => { - assert.equal(inspectContextOverflow(413, GENERIC_BODY).isOverflow, false); - assert.equal(isMarkerlessOverflowReject(413, GENERIC_BODY), true); +test("an unmarked 413 is not double-counted as a marker overflow", () => { + // 413 without a marker: the self-heal gate is closed, which is exactly why + // the warning exists; it must never claim isOverflow. + assert.equal(inspectContextOverflow(413, MALFORMED_BODY).isOverflow, false); + assert.equal(isMarkerlessOverflowReject(413, MALFORMED_BODY), true); }); test("a body WITH an overflow marker is NOT markerless (self-heal engages normally)", () => { assert.equal(inspectContextOverflow(400, OPENAI_OVERFLOW_BODY).isOverflow, true); assert.equal(isMarkerlessOverflowReject(400, OPENAI_OVERFLOW_BODY), false); + assert.equal(inspectContextOverflow(413, OPENAI_OVERFLOW_BODY).isOverflow, true); + assert.equal(isMarkerlessOverflowReject(413, OPENAI_OVERFLOW_BODY), false); }); test("a CJK-marked overflow body is NOT markerless", () => { assert.equal(inspectContextOverflow(400, CJK_OVERFLOW_BODY).isOverflow, true); assert.equal(isMarkerlessOverflowReject(400, CJK_OVERFLOW_BODY), false); + assert.equal(isMarkerlessOverflowReject(413, CJK_OVERFLOW_BODY), false); }); test("5xx is excluded — those already self-heal via the local estimate (#604)", () => { assert.equal(isMarkerlessOverflowReject(502, GENERIC_BODY), false); assert.equal(isMarkerlessOverflowReject(503, GENERIC_BODY), false); + assert.equal(isMarkerlessOverflowReject(507, GENERIC_BODY), false); }); test("non-overflow 4xx and success codes are excluded (no false alarm on auth/malformed)", () => { assert.equal(isMarkerlessOverflowReject(200, GENERIC_BODY), false); assert.equal(isMarkerlessOverflowReject(401, GENERIC_BODY), false); + assert.equal(isMarkerlessOverflowReject(404, GENERIC_BODY), false); + assert.equal(isMarkerlessOverflowReject(422, GENERIC_BODY), false); assert.equal(isMarkerlessOverflowReject(429, GENERIC_BODY), false); }); diff --git a/tests/issue5025-pi-direct-window.test.ts b/tests/issue5025-pi-direct-window.test.ts new file mode 100644 index 0000000000..f8056c1844 --- /dev/null +++ b/tests/issue5025-pi-direct-window.test.ts @@ -0,0 +1,178 @@ +// #5025: without a live proxy the pi lane sent the raw history straight to the +// provider — a multi-million-token body came back as "input exceeds the context +// window" on every retry, unrecoverable while the proxy stayed down. The shared +// oversize arm in directSendOrRefuse needed a window to measure against, and +// both its sources were empty on exactly that path: the +// x-bili-plugin-context-window header is stamped only on the proxy branch (the +// handler returns at `proxyBase === undefined` before stamping), and the pi lane +// never armed declaredWindowForDirect (#4894 armed dsh only). These cases pin +// the pi half: the window is recorded BEFORE the readiness return, an unknown +// window clears it (fail open — never a guessed window), pi-native arms the +// reader, and the armed pair refuses locally instead of egressing. Hermetic like +// issue4894-direct-window-guard.test.ts: XDG_STATE_HOME redirected so no event +// reaches a real log file, proxy env cleared so no probe targets a live proxy. +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { after, beforeEach, test } from "node:test"; +import { createBiliPlugin, piDeclaredWindowForDirect, _resetPiDeclaredWindowForTest } from "../src/agent/pi.ts"; +import { _declaredWindowHookForTest } from "../src/agent/pi-native.ts"; +import { _resetNativeOriginWaiterForTest } from "../src/agent/native-bootstrap.ts"; + +const STATE_ROOT = fs.mkdtempSync(path.join(os.tmpdir(), "bili-5025-state-")); +const PREV_ENV: Record<string, string | undefined> = {}; +for (const key of ["XDG_STATE_HOME", "BILLION_CONTEXT_PROXY", "BILLION_CONTEXT_ATTACH", "BILLION_CONTEXT_PLUGIN", "BILI_NATIVE_PI"]) { + PREV_ENV[key] = process.env[key]; +} +// The native entry arms at import time; a dev shell's live proxy must not be +// probed from inside this suite (same guard as pi-native.test.ts). +delete process.env.BILLION_CONTEXT_PROXY; +delete process.env.BILLION_CONTEXT_ATTACH; +process.env.XDG_STATE_HOME = STATE_ROOT; +after(() => { + for (const [key, value] of Object.entries(PREV_ENV)) { + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + fs.rmSync(STATE_ROOT, { recursive: true, force: true }); +}); + +const UPSTREAM = "http://127.0.0.1:8199/v1/chat/completions"; + +type FakePi = { + events: Map<string, (event: unknown, ctx: unknown) => unknown>; + tools: Array<{ name: string }>; + on: (event: string, handler: (event: never, ctx: never) => unknown) => void; + registerTool: (tool: { name: string }) => void; +}; + +function makeFakePi(): FakePi { + const events = new Map<string, (event: unknown, ctx: unknown) => unknown>(); + const tools: Array<{ name: string }> = []; + return { + events, + tools, + on: (event, handler) => { + const h = handler as (event: unknown, ctx: unknown) => unknown; + const prev = events.get(event); + events.set(event, prev ? ((e, c) => { prev(e, c); return h(e, c); }) : h); + }, + registerTool: (tool) => { + const i = tools.findIndex((t) => t.name === tool.name); + if (i >= 0) tools[i] = tool; + else tools.push(tool); + }, + }; +} + +/** A pi ctx with NO proxy in the model baseUrl — the reported degraded state: + * proxyBaseForCtx resolves undefined, so the header branch returns early. */ +function piCtx(contextWindow: number | undefined, sid = "pi-5025") { + return { + sessionManager: { getSessionId: () => sid }, + model: { id: "gpt-test", api: "openai-completions", baseUrl: "http://127.0.0.1:1/v1", ...(contextWindow !== undefined ? { contextWindow } : {}) }, + cwd: "/tmp", + }; +} + +function headersHandler(): (event: unknown, ctx: unknown) => unknown { + const pi = makeFakePi(); + createBiliPlugin("pi")(pi as never); + const handler = pi.events.get("before_provider_headers"); + assert.ok(handler, "before_provider_headers handler registered"); + return handler; +} + +function chatBody(textChars: number): string { + return JSON.stringify({ model: "gpt-test", messages: [{ role: "user", content: "x".repeat(textChars) }] }); +} + +beforeEach(() => { + _resetPiDeclaredWindowForTest(); + _resetNativeOriginWaiterForTest(); + delete process.env.BILLION_CONTEXT_PROXY; +}); + +test("#5025 pi records the window even when no proxy base resolves (the direct-send path)", async () => { + const handler = headersHandler(); + const headers: Record<string, string> = {}; + await handler({ headers }, piCtx(500_000)); + assert.equal(piDeclaredWindowForDirect(), 500_000, "recorded before the proxy-readiness return"); + // The premise of the bug: in this state the header is never stamped, which + // is why the shared guard had nothing to measure against. + assert.equal(headers["x-bili-plugin-context-window"], undefined, "no proxy base → no stamped window"); + assert.equal(headers["x-bili-plugin"], undefined, "no proxy base → no ownership marker either"); +}); + +test("#5025 unknown window clears the record — the arm fails open instead of guessing", async () => { + const handler = headersHandler(); + await handler({ headers: {} }, piCtx(500_000)); + assert.equal(piDeclaredWindowForDirect(), 500_000); + await handler({ headers: {} }, piCtx(undefined)); + assert.equal(piDeclaredWindowForDirect(), undefined, "a model with no declared window must not inherit the previous one"); +}); + +test("#5025 pi-native arms the shared guard with the pi window reader", async () => { + const hook = _declaredWindowHookForTest(); + assert.equal(typeof hook, "function", "declaredWindowForDirect armed on the pi NativeInterceptState"); + await headersHandler()({ headers: {} }, piCtx(272_000)); + assert.equal(hook?.(), 272_000, "the armed reader answers the recorded window"); +}); + +test("#5025 armed pi reader + not-ready intercept: oversized body refused locally, nothing egresses", async () => { + const saved = globalThis.fetch; + const mod = await import("../src/agent/native-intercept.ts"); + const sink: string[] = []; + mod._resetForTest(); + globalThis.fetch = (async (input: string | URL | Request) => { + sink.push(typeof input === "string" ? input : input instanceof URL ? input.href : (input as Request).url); + return new Response("{}", { status: 200 }); + }) as typeof fetch; + try { + const state: import("../src/agent/native-intercept.ts").NativeInterceptState = { + origin: undefined, + ready: Promise.resolve(undefined), + respawn: async () => undefined, + declaredWindowForDirect: piDeclaredWindowForDirect, + }; + assert.equal(mod.installNativeFetchIntercept(state), true); + await headersHandler()({ headers: {} }, piCtx(500_000)); + const res = await globalThis.fetch(UPSTREAM, { method: "POST", headers: { "content-type": "application/json" }, body: chatBody(4_200_000) }); + const body = (await res.json()) as Record<string, unknown>; + assert.equal(res.status, 422); + assert.equal(res.headers.get("x-bili-gate"), "oversize-refusal-fallback"); + assert.equal((body.error as { code: string }).code, "bili_gate_oversize_refusal_fallback"); + assert.deepEqual(sink, [], "no upstream egress"); + } finally { + globalThis.fetch = saved; + mod._resetForTest(); + } +}); + +test("#5025 armed pi reader + body that fits: direct send unchanged (no false refusal)", async () => { + const saved = globalThis.fetch; + const mod = await import("../src/agent/native-intercept.ts"); + const sink: string[] = []; + mod._resetForTest(); + globalThis.fetch = (async (input: string | URL | Request) => { + sink.push(typeof input === "string" ? input : input instanceof URL ? input.href : (input as Request).url); + return new Response("{}", { status: 200 }); + }) as typeof fetch; + try { + const state: import("../src/agent/native-intercept.ts").NativeInterceptState = { + origin: undefined, + ready: Promise.resolve(undefined), + respawn: async () => undefined, + declaredWindowForDirect: piDeclaredWindowForDirect, + }; + assert.equal(mod.installNativeFetchIntercept(state), true); + await headersHandler()({ headers: {} }, piCtx(500_000)); + const res = await globalThis.fetch(UPSTREAM, { method: "POST", headers: { "content-type": "application/json" }, body: chatBody(10_000) }); + assert.equal(res.status, 200, "direct send proceeds"); + assert.deepEqual(sink, [UPSTREAM]); + } finally { + globalThis.fetch = saved; + mod._resetForTest(); + } +}); diff --git a/tests/issue5152-owner-beat-survives-host-stall.test.ts b/tests/issue5152-owner-beat-survives-host-stall.test.ts index 671977001c..900a53e120 100644 --- a/tests/issue5152-owner-beat-survives-host-stall.test.ts +++ b/tests/issue5152-owner-beat-survives-host-stall.test.ts @@ -53,13 +53,21 @@ async function startReceiver(dir: string): Promise<Receiver> { const portFile = `${log}.port`; const child = spawn(process.execPath, [RECEIVER, log], { stdio: "ignore", detached: true }); child.unref(); + // Poll for a VALID port, not mere file existence: the receiver creates the + // file before its write lands, so existsSync can win that race on a loaded + // runner and read back an empty file (Number("") === 0). + let port = 0; + let portText = ""; const deadline = Date.now() + 15_000; - while (!existsSync(portFile) && Date.now() < deadline) { + while (port === 0 && Date.now() < deadline) { + if (existsSync(portFile)) { + portText = readFileSync(portFile, "utf8").trim(); + const parsed = Number(portText); + if (Number.isInteger(parsed) && parsed > 0) port = parsed; + } Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 20); } - assert.ok(existsSync(portFile), "receiver must publish its port"); - const port = Number(readFileSync(portFile, "utf8").trim()); - assert.ok(Number.isInteger(port) && port > 0, `receiver port must be a number, got ${port}`); + assert.ok(port > 0, `receiver must publish its port (file held "${portText}")`); return { port, beats: () => (existsSync(log) ? readFileSync(log, "utf8").trim().split("\n").filter(Boolean).map(Number) : []), diff --git a/tests/issue5656-single-shot-sidecar.test.ts b/tests/issue5656-single-shot-sidecar.test.ts index 42108e1e09..dad1fbf9b0 100644 --- a/tests/issue5656-single-shot-sidecar.test.ts +++ b/tests/issue5656-single-shot-sidecar.test.ts @@ -10,7 +10,9 @@ // feeding #5671's terminal 7.3×-window deaths). isDshSingleShotSidecar closes // the hole structurally: no action surface (no tools) + single-shot body (one // non-system message, no assistant turn) + a non-empty system carrier, ANY -// system text. These tests pin that contract in both directions (#3617). +// system text, and no durable-state marker in the user blob; the call sites +// additionally require a small pinned output budget (#5687 follow-up, +// hasSingleShotBudget). These tests pin that contract in both directions (#3617). import test from "node:test"; import assert from "node:assert/strict"; @@ -25,7 +27,7 @@ import { _resetPluginStateForTest, resolveConversation } from "../src/plugin.ts" import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; import { getSession, peekSession, _resetSessionsForTest } from "../src/session.ts"; -import { isDshSingleShotSidecar } from "../src/server/side-request.ts"; +import { hasSingleShotBudget, isDshSingleShotSidecar } from "../src/server/side-request.ts"; process.env.NODE_ENV = "test"; process.env.BILI_PERSIST = "0"; @@ -139,6 +141,21 @@ test("unit #5656: isDshSingleShotSidecar vetoes every main-turn and aux shape", assert.equal(isDshSingleShotSidecar("openai", "nope"), false, "non-object body"); }); +test("unit #5687 follow-up: hasSingleShotBudget accepts only a small pinned numeric budget", () => { + assert.equal(hasSingleShotBudget({ max_tokens: 4096 }), true, "anthropic wire preset (prompt-enhance 4096)"); + assert.equal(hasSingleShotBudget({ max_tokens: 8192 }), true, "boundary inclusive"); + assert.equal(hasSingleShotBudget({ max_tokens: 8193 }), false, "just past the boundary"); + assert.equal(hasSingleShotBudget({ max_completion_tokens: 4096 }), true, "openai chat modern field"); + assert.equal(hasSingleShotBudget({ max_output_tokens: 1024 }), true, "responses wire field"); + assert.equal(hasSingleShotBudget({ model: MODEL }), false, "field omitted (openai main-turn shape) -> full pipeline"); + assert.equal(hasSingleShotBudget({ max_tokens: 64_000 }), false, "conversation-sized main-turn budget"); + assert.equal(hasSingleShotBudget({ max_tokens: 0 }), false, "zero"); + assert.equal(hasSingleShotBudget({ max_tokens: -1 }), false, "negative"); + assert.equal(hasSingleShotBudget({ max_tokens: "4096" }), false, "numeric string is not a number (fail-safe direction)"); + assert.equal(hasSingleShotBudget(null), false, "null body"); + assert.equal(hasSingleShotBudget("nope"), false, "non-object body"); +}); + test("e2e #5656 (incident repro): a single-shot sidecar arriving FIRST never claims the raw-key anchor — the main conversation keeps the raw key", async () => { const rig = await startRig(); const CONV = "dshp-5656-openai"; @@ -244,9 +261,9 @@ test("e2e #5656 negative: a tool-less multi-message MAIN turn still goes through try { const url = `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/chat/completions`; const headers: Record<string, string> = { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": CONV }; - // Turn 1 of a tools-disabled conversation matches the single-shot shape - // (system + 1 user, no tools): it rides the verbatim side lane for that - // one turn — tiny, nothing to compress, accepted residual. + // Turn 1 of a tools-disabled conversation matches the single-shot SHAPE + // (system + 1 user, no tools); the conversation-sized output budget + // (64000 below) is what keeps it off the side lane (#5687 follow-up). const r1 = await fetch(url, { method: "POST", headers, @@ -254,13 +271,15 @@ test("e2e #5656 negative: a tool-less multi-message MAIN turn still goes through }); assert.equal(r1.status, 200); await r1.text(); - // Accepted residual, pinned honestly: turn 1 rode the verbatim side lane - // — no refs minted, no request counted (the session shell may exist for - // lane bookkeeping, but it holds no compression state). const stub = peekSession(CONV); const refs1 = stub ? Object.keys(stub.state.messageRefs.byRaw).length : 0; const reqs1 = stub ? stub.stats.requests : 0; - assert.ok(refs1 === 0 && reqs1 === 0, `tool-less single-shot turn 1 rides the side lane (refs=${refs1}, requests=${reqs1})`); + // #5687 follow-up: this residual is REVOKED. A tool-less turn 1 that + // carries a conversation-sized output budget (64000 here) is a MAIN turn, + // not a utility sidecar: the small-budget requirement is what keeps it on + // the kernel path. Pinning the old "rides the side lane" behavior here is + // exactly what let #2817/#5403 regress. + assert.ok(refs1 >= 1 && reqs1 >= 1, `tool-less turn 1 with a conversation budget goes through the full pipeline (refs=${refs1}, requests=${reqs1})`); // Turn 2 replays history (user+assistant+user): NOT single-shot — full // pipeline, claims the raw key, compression state builds. const r2 = await fetch(url, { diff --git a/tests/issue5951-reasoning-only-cut-replay.test.ts b/tests/issue5951-reasoning-only-cut-replay.test.ts index 1148a6d0c8..281323afd0 100644 --- a/tests/issue5951-reasoning-only-cut-replay.test.ts +++ b/tests/issue5951-reasoning-only-cut-replay.test.ts @@ -81,20 +81,11 @@ function chatFinish(reason = "stop"): string { const DONE = "data: [DONE]\n\n"; const ERROR_SENTINEL = "upstream_stream_truncated"; -/** The `meta` object riding the in-band truncation frame, or undefined when the - * turn completed without one. */ -function truncationMeta(body: string): Record<string, unknown> | undefined { - for (const frame of body.split("\n\n")) { - if (!frame.includes(ERROR_SENTINEL)) continue; - const payload = frame - .split("\n") - .filter((line) => line.startsWith("data:")) - .map((line) => line.slice(5).trim()) - .join("\n"); - const parsed = JSON.parse(payload) as { error?: { meta?: Record<string, unknown> } }; - return parsed.error?.meta; - } - return undefined; +function diagFromLogs(logs: string[]): Record<string, unknown> { + const line = logs.find((entry) => entry.includes("upstream stream truncated")); + const serialized = line?.match(/; diag=(\{.*\})\]$/)?.[1]; + if (!serialized) throw new Error("Expected truncation diagnostics in local logs"); + return JSON.parse(serialized) as Record<string, unknown>; } function healedTurn(): ReadableStream<Uint8Array> { @@ -106,6 +97,41 @@ function healedTurn(): ReadableStream<Uint8Array> { ]); } +for (const field of ["reasoning_content", "reasoning"]) { + for (const cause of ["read-error", "eof"]) { + test(`chat pipe: ${cause} after filtered ${field} still heals without replaying an answer (#5951)`, async () => { + const out: string[] = []; + const reasoning = "Inspect \x3cacp before answering"; + const frames = [chatChunk({ role: "assistant", [field]: reasoning })]; + const first = cause === "eof" ? eventsStream(frames) : failedStream(frames, new Error("peer closed")); + let refetches = 0; + + await pipePluginChatWithStrip(first, makeRes(out), "openai", makeSession(), () => {}, async () => { + refetches += 1; + return healedTurn(); + }, "http://up"); + + const body = out.join(""); + assert.equal(refetches, 1); + assert.ok(body.includes(reasoning), "the first attempt's reasoning remains byte-exact"); + assert.ok(body.includes("healed answer")); + assert.ok(!body.includes(ERROR_SENTINEL)); + }); + } +} + +test("chat pipe: pending answer markup after filtered reasoning is not replayed (#5951)", async () => { + const out: string[] = []; + const frames = [chatChunk({ reasoning_content: "Inspect \x3cacp" }), chatChunk({ content: "\x3cacp" })]; + let refetches = 0; + await pipePluginChatWithStrip(failedStream(frames, new Error("peer closed")), makeRes(out), "openai", makeSession(), () => {}, async () => { + refetches += 1; + return healedTurn(); + }, "http://up"); + assert.equal(refetches, 0, "the reasoning-filter exception must not accept a pending answer filter"); + assert.ok(out.join("").includes(ERROR_SENTINEL)); +}); + test("chat pipe: a read error after reasoning-only output re-issues once (#5951)", async () => { const out: string[] = []; const logs: string[] = []; @@ -225,16 +251,16 @@ test("chat pipe: the reasoning-only replay shares the single re-issue budget (#5 refetches += 1; // The retry is cut in the same place: the budget is already spent, so the // host gets the truncation signal rather than a second re-issue. - return Promise.resolve(failedStream([chatChunk({ role: "assistant", reasoning_content: "reasoning-c" })], new Error("upstream socket reset mid-stream"))); + return Promise.resolve(failedStream([chatChunk({ role: "assistant", reasoning_content: "\x3cacp retry" })], new Error("upstream socket reset mid-stream"))); }; - const first = failedStream([chatChunk({ role: "assistant", reasoning_content: "reasoning-a" })], new Error("upstream socket reset mid-stream")); + const first = failedStream([chatChunk({ role: "assistant", reasoning_content: "\x3cacp first" })], new Error("upstream socket reset mid-stream")); await pipePluginChatWithStrip(first, makeRes(out), "openai", makeSession(), (m) => logs.push(m), refetch, "http://up"); const body = out.join(""); assert.equal(refetches, 1, "one re-issue per request, shared with the zero-byte path"); assert.ok(body.includes(ERROR_SENTINEL)); - assert.equal(truncationMeta(body)?.["retryZeroByteCutSpent"], true); + assert.equal(diagFromLogs(logs)["retryZeroByteCutSpent"], true); }); test("chat pipe: a cut that is not replayed names the read error in the diag (#5951)", async () => { @@ -247,8 +273,92 @@ test("chat pipe: a cut that is not replayed names the read error in the diag (#5 await pipePluginChatWithStrip(first, makeRes(out), "openai", makeSession(), (m) => logs.push(m), undefined, "http://up"); - const meta = truncationMeta(out.join("")); - assert.ok(meta, "the truncation frame carries a diag"); - assert.equal(meta["cause"], "read-error"); - assert.equal(meta["readError"], "Error: upstream socket reset mid-stream"); + const diag = diagFromLogs(logs); + assert.equal(diag["cause"], "read-error"); + assert.equal(diag["readError"], "Error: upstream socket reset mid-stream"); +}); + +test("chat pipe: a cut refused for its answer text names that clause in the diag (#5951)", async () => { + const out: string[] = []; + const logs: string[] = []; + const first = failedStream( + [chatChunk({ role: "assistant", reasoning_content: "reasoning-a" }), chatChunk({ content: "partial answer" })], + new Error("upstream socket reset mid-stream"), + ); + + await pipePluginChatWithStrip(first, makeRes(out), "openai", makeSession(), (line) => logs.push(line), () => Promise.resolve(healedTurn()), "http://up"); + + const replay = diagFromLogs(logs)["replay"] as Record<string, unknown> | undefined; + assert.ok(replay, "a refused cut carries the predicate's inputs"); + assert.equal(replay["replayAvailable"], true, "the refetch was there, so only the predicate refused"); + assert.equal(replay["sawThinking"], true); + assert.equal(replay["blocksForwarded"], 0); + assert.equal(replay["openStreams"], 0); + assert.equal(replay["answerProseChars"], "partial answer".length, "the delivered answer text is the flag that refused it"); +}); + +test("chat pipe: a cut refused for a started tool call names that clause in the diag (#5951)", async () => { + const out: string[] = []; + const logs: string[] = []; + const first = failedStream( + [ + chatChunk({ role: "assistant", reasoning_content: "reasoning-a" }), + chatChunk({ tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "read_file", arguments: '{"pa' } }] }), + ], + new Error("upstream socket reset mid-stream"), + ); + + await pipePluginChatWithStrip(first, makeRes(out), "openai", makeSession(), (line) => logs.push(line), () => Promise.resolve(healedTurn()), "http://up"); + + const replay = diagFromLogs(logs)["replay"] as Record<string, unknown> | undefined; + assert.ok(replay); + assert.equal(replay["sawToolUse"], true, "the started tool call is why the replay was refused"); + assert.ok((replay["proseChars"] as number) > 0, "the partial argument the client already holds is counted too"); +}); + +test("chat pipe: a pipe with no refetch reports the replay as unavailable (#5951)", async () => { + const out: string[] = []; + const logs: string[] = []; + const first = failedStream([chatChunk({ role: "assistant", reasoning_content: "reasoning-a" })], new Error("upstream socket reset mid-stream")); + + await pipePluginChatWithStrip(first, makeRes(out), "openai", makeSession(), (line) => logs.push(line), undefined, "http://up"); + + const replay = diagFromLogs(logs)["replay"] as Record<string, unknown> | undefined; + assert.ok(replay); + assert.equal(replay["replayAvailable"], false); + assert.equal(replay["sawThinking"], true, "the reasoning-only shape is visible even where no replay door exists"); +}); + +test("chat pipe: the host owning truncation recovery suppresses the replay (#5951)", async () => { + const out: string[] = []; + const logs: string[] = []; + let refetches = 0; + const refetch = (): Promise<ReadableStream<Uint8Array> | null> => { + refetches += 1; + return Promise.resolve(healedTurn()); + }; + const first = failedStream([chatChunk({ role: "assistant", reasoning_content: "reasoning-a" })], new Error("upstream socket reset mid-stream")); + + await pipePluginChatWithStrip(first, makeRes(out), "openai", makeSession(), (line) => logs.push(line), refetch, "http://up", undefined, undefined, undefined, undefined, undefined, true); + + const body = out.join(""); + assert.equal(refetches, 0, "the host's own recovery owns the single retry budget"); + assert.ok(body.includes(ERROR_SENTINEL), "the host is told the cut happened, so its own retry can run"); + assert.equal(body.split("reasoning-a").length - 1, 1, "the partial reasoning the client already holds is still forwarded"); + const replay = diagFromLogs(logs)["replay"] as Record<string, unknown> | undefined; + assert.equal(replay?.["replayAvailable"], true, "the diag still records that a replay door existed"); +}); + +test("chat pipe: the host owning recovery also suppresses the zero-byte replay (#5951)", async () => { + const out: string[] = []; + let refetches = 0; + const refetch = (): Promise<ReadableStream<Uint8Array> | null> => { + refetches += 1; + return Promise.resolve(healedTurn()); + }; + + await pipePluginChatWithStrip(eventsStream([]), makeRes(out), "openai", makeSession(), () => {}, refetch, "http://up", undefined, undefined, undefined, undefined, undefined, true); + + assert.equal(refetches, 0, "a cut with no client-visible output is still the host's to recover"); + assert.ok(out.join("").includes(ERROR_SENTINEL)); }); diff --git a/tests/issue6104-dsh-review-sidecar-shape.test.ts b/tests/issue6104-dsh-review-sidecar-shape.test.ts new file mode 100644 index 0000000000..924e7baa17 --- /dev/null +++ b/tests/issue6104-dsh-review-sidecar-shape.test.ts @@ -0,0 +1,109 @@ +// #6104: dsh 0.2.2-rc.1's auto-review sidecar stopped matching +// isDshAutoReviewRequest, so every review call ran the FULL pipeline under the +// main session id: reconcileFoldCoverage read the (differently serialized) +// history as a wholesale rewrite on both legs (1,006/1,007 alternating system +// fingerprints across 5 sessions), and the refs minted from the wrong +// serialization were tombstoned as DEAD — 107 `compress FAILED … cannot be +// anchored` receipts and 0 successful compressions in those sessions, with no +// un-tombstone path short of the message reappearing in the view. +// +// Both bars that failed were HOST-SIDE FORMAT decisions, not identity: +// (a) REVIEW_POLICY now opens with a `REVIEW_POLICY` banner line, so the +// pinned sentence is on line 2 and startsWith(line 1) is false; +// (b) reviewMessages(snapshot) replays the whole conversation as +// role:"user" text blobs, so `users === 1` is never true once history is +// non-empty. +// The identity is the pinned sentence (banner-tolerant, still version-pinned); +// the replay veto is carried by the assistant/tool veto inside the loop — a +// real resend always carries assistant turns — so the user COUNT itself never +// had to be 1. These cases pin both halves plus the vetoes that must not move. + +import assert from "node:assert/strict"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; +process.env.BILI_PERSIST = "0"; + +import { DSH_AUTO_REVIEW_SYSTEM_PREFIX, isDshAutoReviewRequest } from "../src/server/side-request.ts"; + +// dsh 0.2.1-and-earlier REVIEW_POLICY: the pinned sentence IS the first line. +const POLICY_LEGACY = `${DSH_AUTO_REVIEW_SYSTEM_PREFIX} in this conversation.\n\nYou must decide whether the pending action is allowed, and reply with exactly one JSON verdict.`; + +// dsh 0.2.2-rc.1 REVIEW_POLICY: a `REVIEW_POLICY` banner line precedes the +// pinned sentence (the reporter's payload: system=4334 chars, policy[0] = +// "REVIEW_POLICY", policy[1] = the pinned sentence). +const POLICY_RC1 = `REVIEW_POLICY\n${DSH_AUTO_REVIEW_SYSTEM_PREFIX} in this conversation.\n\nYou must decide whether the pending action is allowed, and reply with exactly one JSON verdict.`; + +function anthropic(system: string, entries: unknown[], tools?: unknown[]): Record<string, unknown> { + return tools === undefined ? { system, messages: entries } : { system, messages: entries, tools }; +} + +function userText(text: string): Record<string, unknown> { + return { role: "user", content: [{ type: "text", text }] }; +} + +// reviewMessages(snapshot): [ENVIRONMENT, ...snapshot.history.map(json)], all +// replayed as role:"user" text blobs. +function reviewSnapshot(sections: string[]): Record<string, unknown>[] { + return sections.map((s) => userText(s)); +} + +const SECTIONS = ["ENVIRONMENT\nprofile: web; model: deepseek-flash", "PROJECT_INSTRUCTIONS\n(none)", "FILTERED_HISTORY\n[…]", "PENDING_ACTION\n{\"tool\":\"bash\"}"]; + +test("#6104: the 0.2.2 REVIEW_POLICY banner no longer hides the pinned sentence", () => { + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_RC1, reviewSnapshot([SECTIONS[0]!]))), true); + // The legacy first-line shape stays a hit. + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_LEGACY, reviewSnapshot([SECTIONS[0]!]))), true); +}); + +test("#6104: a history-replaying review body (N role:user blobs) is still the review", () => { + // Pre-fix this was false for every non-empty history: `users === 1`. + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_RC1, reviewSnapshot(SECTIONS))), true); + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_LEGACY, reviewSnapshot(SECTIONS))), true); + // Responses-wire spelling of the same body. + assert.equal(isDshAutoReviewRequest("responses", { instructions: POLICY_RC1, input: reviewSnapshot(SECTIONS) }), true); +}); + +test("#6104: an assistant or tool turn still vetoes — a real resend can never match", () => { + const replayed = [SECTIONS[0]!, { role: "assistant", content: [{ type: "text", text: "done" }] }, SECTIONS[1]!]; + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_RC1, replayed)), false); + // Responses-side typed non-message items can only appear in a real round-trip. + assert.equal(isDshAutoReviewRequest("responses", { instructions: POLICY_RC1, input: [SECTIONS[0]!, { type: "function_call", call_id: "c1", name: "bash", arguments: "{}" }] }), false); +}); + +test("#6104: the tools veto and the identity pin are untouched", () => { + // Any action surface marks an agent turn, even with the reviewer policy. + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_RC1, reviewSnapshot(SECTIONS), [{ name: "bash", input_schema: {} }])), false); + // Same body shapes, unrelated system text: not the reviewer's call. + assert.equal(isDshAutoReviewRequest("anthropic", anthropic("You are a helpful assistant.", reviewSnapshot(SECTIONS))), false); + // Empty content is never a review snapshot. + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_RC1, [{ role: "user", content: "" }])), false); + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_RC1, [])), false); +}); + +test("#6104: the banner tolerance is bounded to the system head, not the whole prompt", () => { + // A policy quoted deep inside someone else's prompt must not identify a review call. + const quoted = ["You are a coding agent.", "", "", "", "", "", `${DSH_AUTO_REVIEW_SYSTEM_PREFIX} in this conversation.`].join("\n"); + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(quoted, reviewSnapshot(SECTIONS))), false); + // The window itself, line by line: DSH_AUTO_REVIEW_HEAD_LINES is the whole + // contract of the tolerance, so pin BOTH ends of it — a future "just raise + // the constant" edit has to face the case it stops catching. + const sentenceAt = (line: number): string => { + const lines = ["You are a coding agent.", "body", "body", "body", "body", "body", "body"]; + lines[line - 1] = `${DSH_AUTO_REVIEW_SYSTEM_PREFIX} in this conversation.`; + return lines.join("\n"); + }; + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(sentenceAt(1), reviewSnapshot(SECTIONS))), true, "line 1 (the legacy pin) still matches"); + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(sentenceAt(5), reviewSnapshot(SECTIONS))), true, "line 5 is the last line inside the window"); + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(sentenceAt(6), reviewSnapshot(SECTIONS))), false, "line 6 is outside the window"); + // CRLF carriers (the reporter's host is Windows) read the same. + assert.equal(isDshAutoReviewRequest("anthropic", anthropic(POLICY_RC1.replace(/\n/g, "\r\n"), reviewSnapshot(SECTIONS))), true); + // Block-array system: systemCarrierTexts hands each block over separately, so + // a banner in its own block still leaves the sentence at the head of its own. + assert.equal(isDshAutoReviewRequest("anthropic", { system: [{ type: "text", text: "REVIEW_POLICY" }, { type: "text", text: POLICY_LEGACY }], messages: reviewSnapshot(SECTIONS) }), true); +}); + +test("#6104: non-anthropic wires keep their carrier reading", () => { + // Google: systemInstruction.parts carries the policy; contents hold the blobs. + assert.equal(isDshAutoReviewRequest("google", { systemInstruction: { parts: [{ text: POLICY_RC1 }] }, contents: reviewSnapshot(SECTIONS).map((e) => ({ role: "user", parts: [{ text: String((e.content as { text: string }[])[0]!.text) }] })) }), true); +}); diff --git a/tests/lane-decisions-matrix.test.ts b/tests/lane-decisions-matrix.test.ts index 70559e0b40..d889ccf797 100644 --- a/tests/lane-decisions-matrix.test.ts +++ b/tests/lane-decisions-matrix.test.ts @@ -325,6 +325,11 @@ const ROWS: { name: string; lane: Lane; tools: string[]; injectTool?: boolean; t // wire carries the MCP-prefixed compress reads reachable, same as #3401's // bare-name observation on dsh. { name: "dsh mcp__bili__compress keeps the surface (#4344 dialect is lane-independent)", lane: "dsh", tools: ["mcp__bili__compress"], expect: () => ({ nudge: true, standingPrompt: true }) }, + // #5592: a Codex calling-convention hint does not prove an MCP binding exists. + ...["exec", "functions", "functions.exec"].map((name) => ({ + name: `dsh ${name} alone does not prove compression reachability (#5592)`, + lane: "dsh" as const, tools: [name], expect: () => ({ nudge: false, standingPrompt: false }), + })), ]; for (const wire of ["anthropic", "openai", "responses", "google"] as const) { diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index b98142051c..1bb8f56d2f 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -734,6 +734,8 @@ test("runLaunch omp #535: refuses launch when http rewrites needed and extension const prevUserProfile = process.env.USERPROFILE; const prevClientBin = process.env.BILI_CLIENT_BIN; const prevOmpDir = process.env.PI_CODING_AGENT_DIR; + const prevThreads = process.env.PI_ANTHROPIC_MESSAGE_THREADS; + delete process.env.PI_ANTHROPIC_MESSAGE_THREADS; process.env.HOME = home; if (prevUserProfile !== undefined) process.env.USERPROFILE = home; delete process.env.PI_CODING_AGENT_DIR; @@ -750,9 +752,11 @@ test("runLaunch omp #535: refuses launch when http rewrites needed and extension if (distExisted) fs.renameSync(distAgent, distBackup); const clientArgsSeen: string[][] = []; - const spawnImpl: SpawnFn = (cmd, args) => { + let childEnv: NodeJS.ProcessEnv | undefined; + const spawnImpl: SpawnFn = (cmd, args, options) => { if (cmd === process.env.BILI_CLIENT_BIN) { clientArgsSeen.push([...args]); + childEnv = options.env; const child = makeFakeChild(0); const orig = child.on!.bind(child); (child as { on: SpawnChild["on"] }).on = (event, listener) => { @@ -793,6 +797,8 @@ test("runLaunch omp #535: refuses launch when http rewrites needed and extension assert.ok(!clientArgsSeen[0].includes("-e"), "installed entry loads the plugin — no -e double load"); assert.equal(fs.existsSync(`${ompHome}-bili`), false, "still no overlay dir"); assert.deepEqual(exitCalls, [0]); + // #6088: OMP message threads would send only the newest message per turn + assert.equal(childEnv?.PI_ANTHROPIC_MESSAGE_THREADS, "0", "omp message threads defaulted off"); } finally { process.exit = prevExit; if (prevClientBin === undefined) delete process.env.BILI_CLIENT_BIN; @@ -802,6 +808,8 @@ test("runLaunch omp #535: refuses launch when http rewrites needed and extension else process.env.USERPROFILE = prevUserProfile; if (prevOmpDir === undefined) delete process.env.PI_CODING_AGENT_DIR; else process.env.PI_CODING_AGENT_DIR = prevOmpDir; + if (prevThreads === undefined) delete process.env.PI_ANTHROPIC_MESSAGE_THREADS; + else process.env.PI_ANTHROPIC_MESSAGE_THREADS = prevThreads; if (distExisted) fs.renameSync(distBackup, distAgent); rmrf(home); } diff --git a/tests/loop-core.test.ts b/tests/loop-core.test.ts index b0012ca38e..e420605678 100644 --- a/tests/loop-core.test.ts +++ b/tests/loop-core.test.ts @@ -4,6 +4,7 @@ import type { Config, CoreMessage } from "acp-kernel"; import { createCore, createInitialState, assignRefs, emptyRefMap, defaultConfig } from "acp-kernel"; import type { Session } from "../src/session.ts"; import { runCompressLoop, createResponsesAdapter, createOpenaiAdapter, MAX_LOOP_ROUNDS } from "../src/loop/index.ts"; +import { buildSessionCacheReport } from "../src/cache-ledger.ts"; import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; import { createHandoffReply } from "../src/server/handoff-response.ts"; import type { WireProtocol } from "../src/util.ts"; @@ -410,6 +411,28 @@ for (const completion of ["text", "tool"] as const) { } } +test("#6329: OpenAI cache-field omission means zero cached tokens, not unmeasured", async () => { + const ctx = makeCtx([], "openai"); + const body = { model: "m", messages: [], stream: true, stream_options: { include_usage: true } }; + const raw = [ + `data: ${JSON.stringify({ choices: [{ index: 0, delta: { content: "hello" } }] })}\n\n`, + `data: ${JSON.stringify({ choices: [{ index: 0, delta: {}, finish_reason: "stop" }], usage: { prompt_tokens: 1000, completion_tokens: 10, total_tokens: 1010 } })}\n\n`, + "data: [DONE]\n\n", + ].join(""); + for await (const _chunk of runCompressLoop( + new Response(raw).body!, ctx, body, { url: "http://mock", headers: {} }, + createOpenaiAdapter(body), buildCompressSystemPrompt(), + )) {} + assert.equal(ctx.session.stats.inputTokens, 1000); + assert.equal(ctx.session.stats.cachedTokens, 0); + assert.equal(ctx.session.stats.cacheSamples, 1); + const report = buildSessionCacheReport(ctx.session); + assert.equal(report.unmeasured.samples, 0); + assert.equal(report.unmeasured.inputTokens, 0); + assert.equal(report.totals.input, 1000); + assert.equal(report.totals.cached, 0); +}); + test("loop usage: protocol unset → legacy additive behavior (prompt + cached)", async () => { const ctx = makeCtx(); await drain( diff --git a/tests/model-windows.test.ts b/tests/model-windows.test.ts index 714db58036..2923ff9c21 100644 --- a/tests/model-windows.test.ts +++ b/tests/model-windows.test.ts @@ -12,7 +12,7 @@ import { collectModelWindows, type ClientConfig, } from "../src/client-config.ts"; -import { parseLauncherModelWindows, anthropicBetaContextWindow } from "../src/server.ts"; +import { parseLauncherModelWindows, anthropicBetaContextWindow, anthropicBetaMessageThreads } from "../src/server.ts"; import { MAX_ANTHROPIC_BETA_WINDOW } from "../src/server/context-window.ts"; test("parseOmpYaml: captures per-model contextWindow (models after baseUrl)", () => { @@ -222,6 +222,13 @@ test("parseLauncherModelWindows: valid JSON, invalid input, non-numeric filtered // overridden. The parser is per-request (the header may appear/disappear // between requests of the same session). +test("#6088 anthropicBetaMessageThreads: detects the message-threads beta among others", () => { + assert.equal(anthropicBetaMessageThreads({ "anthropic-beta": "oauth-2025-04-20, message-threads-2026-08-12" }), true); + assert.equal(anthropicBetaMessageThreads({ "anthropic-beta": ["context-1m-2025-08-07", "message-threads-2026-08-12"] }), true); + assert.equal(anthropicBetaMessageThreads({ "anthropic-beta": "context-1m-2025-08-07" }), false); + assert.equal(anthropicBetaMessageThreads({}), false); +}); + test("anthropicBetaContextWindow: context-1m beta → 1,000,000", () => { assert.equal(anthropicBetaContextWindow({ "anthropic-beta": "context-1m-2025-08-07" }), 1_000_000); }); diff --git a/tests/native-intercept-selfheal.test.ts b/tests/native-intercept-selfheal.test.ts index 4e47440c6a..155ecc3652 100644 --- a/tests/native-intercept-selfheal.test.ts +++ b/tests/native-intercept-selfheal.test.ts @@ -50,6 +50,30 @@ async function withHeal<T>(fn: (ctx: { sink: string[]; rearm: (v: typeof fetch) } } +test("native headersFor stamps a host recovery owner on the routed proxy request", async () => { + const captured: Headers[] = []; + const saved = globalThis.fetch; + globalThis.fetch = (async (_input: string | URL | Request, init?: RequestInit) => { + captured.push(new Headers(init?.headers)); + return new Response("{}", { status: 200 }); + }) as typeof fetch; + const { _resetForTest } = await import("../src/agent/native-intercept.js"); + const state: NativeInterceptState = { + origin: "http://127.0.0.1:40001", + ready: Promise.resolve("http://127.0.0.1:40001"), + headersFor: () => ({ "x-bili-host-recovery-owner": "omp" }), + }; + _resetForTest(); + try { + assert.equal(installNativeFetchIntercept(state), true); + await globalThis.fetch("http://127.0.0.1:8199/v1/chat/completions", { method: "POST", body: "{}" }); + assert.equal(captured[0]?.get("x-bili-host-recovery-owner"), "omp"); + } finally { + globalThis.fetch = saved; + _resetForTest(); + } +}); + test("#1158 self-heal: third-party reset to a frozen bare fetch re-chains and keeps routing through bili", async () => { const bareSink: string[] = []; const { sink } = await withHeal(async ({ rearm, fetch }) => { diff --git a/tests/notice-registry.test.ts b/tests/notice-registry.test.ts index 6d3b60edb4..c153afcceb 100644 --- a/tests/notice-registry.test.ts +++ b/tests/notice-registry.test.ts @@ -18,6 +18,13 @@ process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), "notice-reg-dsh-")) process.env.XDG_STATE_HOME = stateDir; process.env.BILI_CONFIG_FILE = cfgPath; +// This suite asserts the PRODUCTION default storage posture. The CI corpus +// baseline (tests/preload-storage-baseline.mjs, #2815) forces +// BILI_STORAGE_UNIFIED=legacy for every test file, which flips the +// pure-sqlite-default notice (#6480) off — pin our own selection per the +// baseline's documented rule (the #4011 lesson). +delete process.env.BILI_STORAGE_UNIFIED; + const { NOTICES, visibleNotices, SQLITE_DEFAULT_NOTICE_ID } = await import("../src/notices.ts"); const { IOT_INCOMPAT_NOTICE_ID, PCK_NOTICE_ID, WM_FASTPATH_INTRO_ID, dismissUpgradeNotice } = await import("../src/pck-notice.ts"); const { MESSAGES, LOCALES } = await import("../src/web/i18n.ts"); diff --git a/tests/omp-native.test.ts b/tests/omp-native.test.ts index f61ccfce43..c2c3500e16 100644 --- a/tests/omp-native.test.ts +++ b/tests/omp-native.test.ts @@ -6,10 +6,11 @@ import path from "node:path"; import { pathToFileURL } from "node:url"; import { createServer } from "node:http"; import type { AddressInfo } from "node:net"; -import factory, { ompTrafficRidesBili, shouldBootstrapNativeOmp, planNativeOmp, armNativeOmp, _resetNativeStateForTest, _setSpawnForTest, _stateRespawnForTest, _noteRoutedForTest } from "../src/agent/omp-native.ts"; -import { applyOmpFirstEventTimeout, nativeProxyScriptPath, ompFirstEventTimeoutValue, OMP_FIRST_EVENT_TIMEOUT_DEFAULT_MS, OMP_FIRST_EVENT_TIMEOUT_ENV } from "../src/agent/native-bootstrap.ts"; +import factory, { ompTrafficRidesBili, shouldBootstrapNativeOmp, planNativeOmp, armNativeOmp, _resetNativeStateForTest, _setSpawnForTest, _stateRespawnForTest, _noteRoutedForTest, _headersForTest } from "../src/agent/omp-native.ts"; +import { applyOmpFirstEventTimeout, applyOmpMessageThreadsOff, OMP_MESSAGE_THREADS_ENV, nativeProxyScriptPath, ompFirstEventTimeoutValue, OMP_FIRST_EVENT_TIMEOUT_DEFAULT_MS, OMP_FIRST_EVENT_TIMEOUT_ENV } from "../src/agent/native-bootstrap.ts"; import { pluginInstall, pluginRemove, pluginStatusAll, selfPackageRoot, ompPluginLoadedFrom } from "../src/plugin-install.ts"; import { rmrf } from "./tmp-rm.ts"; +import { BILI_HOST_RECOVERY_OWNER_HEADER, BILI_HOST_RECOVERY_OWNER_OMP } from "../src/util.ts"; // #1135 wiring tests drive verifyAttachAndRecover against a dead target — cap // the routed-evidence grace so the probe fails fast instead of paying the 5s default. @@ -72,6 +73,18 @@ test("#1774 applyOmpFirstEventTimeout: stamps only when unpinned, idempotent on assert.equal(pinned[OMP_FIRST_EVENT_TIMEOUT_ENV], "45000"); }); +test("#6088 applyOmpMessageThreadsOff: defaults to \"0\" only when unpinned, blank counts as unset", () => { + const clean: NodeJS.ProcessEnv = {}; + applyOmpMessageThreadsOff(clean); + assert.equal(clean[OMP_MESSAGE_THREADS_ENV], "0"); + const blank: NodeJS.ProcessEnv = { [OMP_MESSAGE_THREADS_ENV]: " " }; + applyOmpMessageThreadsOff(blank); + assert.equal(blank[OMP_MESSAGE_THREADS_ENV], "0"); + const pinned: NodeJS.ProcessEnv = { [OMP_MESSAGE_THREADS_ENV]: "1" }; + applyOmpMessageThreadsOff(pinned); + assert.equal(pinned[OMP_MESSAGE_THREADS_ENV], "1", "explicit user value wins"); +}); + test("nativeProxyScriptPath: dist/agent/omp-native.js resolves to the package bin", () => { const agentFile = path.resolve(path.sep, "opt", "pkg", "dist", "agent", "omp-native.js"); const resolved = nativeProxyScriptPath(pathToFileURL(agentFile).href); @@ -82,6 +95,12 @@ test("default export is an ExtensionFactory (loaded by omp's extensions loader)" assert.equal(typeof factory, "function"); }); +test("native OMP host stamps the private recovery-owner marker", () => { + assert.deepEqual(_headersForTest("https://api.example.test/v1/chat/completions"), { + [BILI_HOST_RECOVERY_OWNER_HEADER]: BILI_HOST_RECOVERY_OWNER_OMP, + }); +}); + // — installer: `bili plugin install omp` targets the native entry (#957) —— const NATIVE_ENTRY = path.join(selfPackageRoot(), "dist", "agent", "omp-native.js"); diff --git a/tests/opencode-msgids.test.ts b/tests/opencode-msgids.test.ts index ae4b8d455e..177e4d507a 100644 --- a/tests/opencode-msgids.test.ts +++ b/tests/opencode-msgids.test.ts @@ -19,7 +19,7 @@ process.env.NODE_TEST_CONTEXT = "1"; const { maybeIncrementalOpencode, opencodeIncrementalOnResponse, __resetOpencodeIncrementalForTests, createV1ServerHooks } = await import("../src/agent/opencode-native.ts"); const { __resetIncrementalClientForTests } = await import("../src/agent/incremental-client.ts"); -const { __resetOpencodeMsgIdsForTests } = await import("../src/agent/opencode-msgids.ts"); +const { __resetOpencodeMsgIdsForTests, harvestOpencodeMsgIds, deriveOpencodeEntry } = await import("../src/agent/opencode-msgids.ts"); import type { V2HttpRequestEvent, V2State } from "../src/agent/opencode-v2.ts"; const MODEL_URL = "https://api.anthropic.com/v1/messages"; @@ -239,3 +239,200 @@ test("opencode msgids: a single-flight-blocked turn does not burn its ids (#4491 const body2 = await plannedBody(t2.e); assert.deepEqual(body2.msgIds, ["msg_a1", "msg_u2"], "blocked attempt did not burn the delta ids"); }); + +// ── #5743 redo: REAL host ids from both seams (V2 "context" event shapes, +// tool sessions, the #5741 repeated-prompt scenario). The host message +// shapes below mirror live 2.0.4 captures (anthropic provider): role +// user/assistant/tool, content parts text / tool-call / tool-result. + +function ctxMessage(id: string, role: string, content: unknown[]): { id: string; role: string; content: unknown[] } { + return { id, role, content }; +} + +/** Simulate one `ctx.session.hook("context")` fire (V2 seam). */ +function fireContext(sid: string, messages: unknown[]): void { + harvestOpencodeMsgIds(sid, messages); +} + +test("opencode msgids: V2 context seam carries real ids through a TOOL session", async () => { + __resetIncrementalClientForTests(); + __resetOpencodeIncrementalForTests(); + __resetOpencodeMsgIdsForTests(); + const lane = await fakeLane(); + lanes.push(lane); + const s = state(lane.base); + const sid = "ses_v2ctx_1"; + + // Turn 1: user asks, assistant calls read(), the result comes back as a + // host role-"tool" message carrying an ERROR result. + fireContext(sid, [ + ctxMessage("msg_vu1", "user", [{ type: "text", text: "read README.md" }]), + ]); + const t1 = fakeEvent({ model: "claude-test", max_tokens: 512, system: "be terse", messages: [ + { role: "user", content: [{ type: "text", text: "read README.md" }] }, + { role: "assistant", content: [ + { type: "text", text: "Reading." }, + { type: "tool_use", id: "toolu_1", name: "read", input: { path: "README.md" }, cache_control: { type: "ephemeral" } }, + ] }, + { role: "user", content: [ + { type: "tool_result", tool_use_id: "toolu_1", content: "{\"error\":{}}", is_error: true, cache_control: { type: "ephemeral" } }, + ] }, + ] }, sid); + // NOTE: fired BEFORE the request assembled — order inside the handler: + // context first, http.request after; the seed below replans against this + // latest fire exactly like the live hook does. + fireContext(sid, [ + ctxMessage("msg_vu1", "user", [{ type: "text", text: "read README.md" }]), + ctxMessage("msg_va1", "assistant", [ + { type: "text", text: "Reading." }, + { type: "tool-call", id: "toolu_1", name: "read", input: { path: "README.md" }, providerExecuted: false }, + ]), + ctxMessage("msg_vt1", "tool", [ + { type: "tool-result", id: "toolu_1", name: "read", result: { type: "error", value: { error: {} } }, providerExecuted: false }, + ]), + ]); + await maybeIncrementalOpencode(t1.e, s); + const body1 = await plannedBody(t1.e); + assert.deepEqual(body1.msgIds, ["msg_vu1", "msg_va1", "msg_vt1"], + "seed rides REAL host ids — text, tool-call (assistant), tool-result (host role \"tool\" normalized to wire \"user\"); error flag matched via is_error"); + + opencodeIncrementalOnResponse(t1.url, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "1" } }), headersOf(t1.e.request as Request)); + + // Turn 2: assistant settles on text; the delta must carry exactly its + // own fresh id, sliced in lockstep with the suffix messages. + fireContext(sid, [ + ctxMessage("msg_vu1", "user", [{ type: "text", text: "read README.md" }]), + ctxMessage("msg_va1", "assistant", [ + { type: "text", text: "Reading." }, + { type: "tool-call", id: "toolu_1", name: "read", input: { path: "README.md" }, providerExecuted: false }, + ]), + ctxMessage("msg_vt1", "tool", [ + { type: "tool-result", id: "toolu_1", name: "read", result: { type: "error", value: { error: {} } }, providerExecuted: false }, + ]), + ctxMessage("msg_va2", "assistant", [{ type: "text", text: "It failed: missing file." }]), + ctxMessage("msg_vu2", "user", [{ type: "text", text: "ok thanks" }]), + ]); + const t2 = fakeEvent({ model: "claude-test", max_tokens: 512, system: "be terse", messages: [ + { role: "user", content: [{ type: "text", text: "read README.md" }] }, + { role: "assistant", content: [ + { type: "text", text: "Reading." }, + { type: "tool_use", id: "toolu_1", name: "read", input: { path: "README.md" }, cache_control: { type: "ephemeral" } }, + ] }, + { role: "user", content: [ + { type: "tool_result", tool_use_id: "toolu_1", content: "{\"error\":{}}", is_error: true, cache_control: { type: "ephemeral" } }, + ] }, + { role: "assistant", content: [{ type: "text", text: "It failed: missing file." }] }, + { role: "user", content: [{ type: "text", text: "ok thanks" }] }, + ] }, sid); + await maybeIncrementalOpencode(t2.e, s); + const body2 = await plannedBody(t2.e); + assert.equal((body2.messages as unknown[]).length, 2, "delta rides only the suffix"); + assert.deepEqual(body2.msgIds, ["msg_va2", "msg_vu2"], "delta rides ONLY the fresh host ids"); +}); + +test("opencode msgids: #5741 repeated-prompt turns get DISTINCT real ids", async () => { + __resetIncrementalClientForTests(); + __resetOpencodeIncrementalForTests(); + __resetOpencodeMsgIdsForTests(); + const lane = await fakeLane(); + lanes.push(lane); + const s = state(lane.base); + const sid = "ses_repeat_1"; + + // Turn 1: prompt "go" → ids r1/a1. + fireContext(sid, [ctxMessage("msg_r1", "user", [{ type: "text", text: "go" }])]); + const w1 = { model: "claude-test", max_tokens: 512, system: "s", messages: [{ role: "user", content: [{ type: "text", text: "go" }] }] }; + const t1 = fakeEvent(w1, sid); + await maybeIncrementalOpencode(t1.e, s); + assert.deepEqual((await plannedBody(t1.e)).msgIds, ["msg_r1"]); + opencodeIncrementalOnResponse(t1.url, new Response("{}", { status: 200, headers: { "x-bili-applied-seq": "1" } }), headersOf(t1.e.request as Request)); + + // Turn 2: the BYTE-IDENTICAL prompt resends — opencode mints a FRESH id + // for it. With derived content hashes both occurrences would collide; + // the client must vouch the SECOND id so the lane pins a second row. + fireContext(sid, [ + ctxMessage("msg_r1", "user", [{ type: "text", text: "go" }]), + ctxMessage("msg_a1", "assistant", [{ type: "text", text: "done" }]), + ctxMessage("msg_r2", "user", [{ type: "text", text: "go" }]), + ]); + const t2 = fakeEvent({ model: "claude-test", max_tokens: 512, system: "s", messages: [ + { role: "user", content: [{ type: "text", text: "go" }] }, + { role: "assistant", content: [{ type: "text", text: "done" }] }, + { role: "user", content: [{ type: "text", text: "go" }] }, + ] }, sid); + await maybeIncrementalOpencode(t2.e, s); + const body2 = await plannedBody(t2.e); + assert.equal((body2.messages as unknown[]).length, 2, + "delta covers the acked prompt's reply plus the repeated prompt itself"); + assert.deepEqual(body2.msgIds, ["msg_a1", "msg_r2"], + "the repeated prompt vouches its OWN fresh id (msg_r2) — never collides with the pinned msg_r1 row"); +}); + +test("opencode msgids: reasoning↔thinking aligns; noise parts skip; unknown parts degrade", async () => { + __resetIncrementalClientForTests(); + __resetOpencodeIncrementalForTests(); + __resetOpencodeMsgIdsForTests(); + const lane = await fakeLane(); + lanes.push(lane); + const s = state(lane.base); + + // reasoning part ↔ thinking block; v1 bookkeeping parts carry no wire block + const sid = "ses_think_1"; + fireContext(sid, [ + ctxMessage("msg_h1", "assistant", [ + { type: "reasoning", text: "hmm…" }, + { type: "text", text: "answer" }, + ]), + ]); + let t = fakeEvent({ model: "claude-test", max_tokens: 512, messages: [ + { role: "assistant", content: [ + { type: "thinking", thinking: "hmm…", signature: "sig" }, + { type: "text", text: "answer" }, + ] }, + ] }, sid); + await maybeIncrementalOpencode(t.e, s); + assert.deepEqual((await plannedBody(t.e)).msgIds, ["msg_h1"], "reasoning part proves onto the thinking block positionally"); + + // an UNRECOGNIZED part type must drop the field (never guess) + const sid2 = "ses_unknown_1"; + fireContext(sid2, [ + ctxMessage("msg_k1", "user", [{ type: "source-url", url: "https://example.com" }]), + ]); + const t2 = fakeEvent({ model: "claude-test", max_tokens: 512, messages: [ + { role: "user", content: [{ type: "text", text: "whatever" }] }, + ] }, sid2); + await maybeIncrementalOpencode(t2.e, s); + assert.equal("msgIds" in (await plannedBody(t2.e)), false, "unknown part types degrade to content-derived identities"); + + // duplicate ids drop the whole field even when every position otherwise aligns + const sid3 = "ses_dup_1"; + fireContext(sid3, [ + ctxMessage("msg_d1", "user", [{ type: "text", text: "a" }]), + ctxMessage("msg_d1", "assistant", [{ type: "text", text: "b" }]), + ]); + const t3 = fakeEvent({ model: "claude-test", max_tokens: 512, messages: [ + { role: "user", content: [{ type: "text", text: "a" }] }, + { role: "assistant", content: [{ type: "text", text: "b" }] }, + ] }, sid3); + await maybeIncrementalOpencode(t3.e, s); + assert.equal("msgIds" in (await plannedBody(t3.e)), false, "duplicate ids can never ride"); +}); + +test("opencode msgids: deriveOpencodeEntry unit-level normalization", () => { + const e1 = deriveOpencodeEntry(ctxMessage("msg_x1", "tool", [ + { type: "tool-result", id: "toolu_9", name: "bash", result: { type: "error", value: "boom" }, providerExecuted: false }, + ])); + assert.equal(e1.shape?.role, "user", "host role \"tool\" normalizes to wire \"user\""); + assert.deepEqual(e1.shape?.toks, [`r\u0000toolu_9\u00001`], "tool-result anchored by id + error flag"); + + const e2 = deriveOpencodeEntry({ info: { id: "msg_y1", role: "assistant", sessionID: "s" }, parts: [ + { type: "step-start" }, + { type: "text", text: "hi" }, + { type: "tool", tool: "bash", callID: "call_7", state: { status: "running" } }, + { type: "snapshot", snapshot: "abc" }, + ] }); + assert.deepEqual(e2.shape?.toks, [`t\u0000hi`, `c\u0000call_7\u0000bash`], "v1 bookkeeping parts (step-start/snapshot) skip; tool part anchors by callID+name"); + + assert.equal(deriveOpencodeEntry({ info: { role: "system" }, parts: [] }).shape, null, "non-conversation roles poison the entry only"); + assert.equal(deriveOpencodeEntry({ id: "not-a-host-id", role: "user", content: [] }).id, "", "foreign-shaped ids stay unusable"); +}); diff --git a/tests/opencode-rolling-cache-marks.test.ts b/tests/opencode-rolling-cache-marks.test.ts index b2fbaa8ef4..13d982b25b 100644 --- a/tests/opencode-rolling-cache-marks.test.ts +++ b/tests/opencode-rolling-cache-marks.test.ts @@ -183,8 +183,7 @@ test("rolling cache_control: deltas after the seed, upstream bodies identical to assert.deepEqual(modes.slice(1), requests.slice(1).map(() => "delta"), "a moving breakpoint never forces a resync"); assert.equal(inc.forwarded.length, base.forwarded.length); for (let i = 0; i < requests.length; i++) { - assert.equal(canonMessages(inc.forwarded[i].messages), canonMessages(base.forwarded[i].messages), `request ${i}: assembled messages byte-identical to the host's full resend`); - assert.equal(messageBreakpoints(inc.forwarded[i]), messageBreakpoints({ messages: requests[i] }), `request ${i}: only the host's rolling breakpoints (no accumulation)`); + assert.equal(canonMessages(inc.forwarded[i].messages), canonMessages(base.forwarded[i].messages), `request ${i}: assembled messages byte-identical to the host's full resend`); assert.equal(messageBreakpoints(inc.forwarded[i]), messageBreakpoints({ messages: requests[i] }), `request ${i}: only the host's rolling breakpoints (no accumulation)`); } const last = sizes[sizes.length - 1]; assert.ok(last.sent * 3 < last.full, `delta (${last.sent} B) is a fraction of the full body (${last.full} B)`); @@ -206,8 +205,7 @@ test("rolling cache_control: a retouch against a cold canonical answers 409 and const { modes } = await runIncremental(inc, requests, { restartBefore: 3 }); assert.deepEqual(modes, ["seed", "delta", "delta", "delta:409", "seed", "delta", "delta"], "cold canonical: one 409, one re-seed, then deltas again"); for (let i = 0; i < requests.length; i++) { - assert.equal(canonMessages(inc.forwarded[i].messages), canonMessages(base.forwarded[i].messages), `request ${i} byte-identical after recovery`); - } + assert.equal(canonMessages(inc.forwarded[i].messages), canonMessages(base.forwarded[i].messages), `request ${i} byte-identical after recovery`); } } finally { await inc.close(); } @@ -230,6 +228,45 @@ test("rolling cache_control: a retouch whose content differs from the canonical } }); +test("rolling marks: repeated prompts retain distinct IDs and match full resend", async () => { + const history: Wire[] = []; + const requests: Wire[][] = []; + for (let i = 0; i < 6; i++) { + history.push({ role: "user", content: [{ type: "text", text: "continue" }] }); + requests.push(decorate(history)); + history.push({ role: "assistant", content: [{ type: "text", text: "ok" }] }); + } + const base = await harness(); + try { await runFullResend(base, requests); } finally { await base.close(); } + const inc = await harness(); + try { + const { modes } = await runIncremental(inc, requests); + assert.deepEqual(modes, ["seed", ...requests.slice(1).map(() => "delta")]); + for (let i = 0; i < requests.length; i++) { + assert.equal(canonMessages(inc.forwarded[i].messages), canonMessages(base.forwarded[i].messages)); + assert.equal(messageBreakpoints(inc.forwarded[i]), messageBreakpoints({ messages: requests[i] })); + } + } finally { await inc.close(); } +}); + +test("continued content IDs can be replaced by client pins without losing the new mark", async () => { + const inc = await harness(); + try { + const common = { conversationId: CONV, path: "/v1/messages", system: SYSTEM, request: { model: "claude-test", max_tokens: 1024 } }; + const seed = await inc.post("/__bili/plugin/turn", JSON.stringify({ ...common, sinceSeq: 0, resync: true, messages: [{ role: "user", content: [{ type: "text", text: "continue" }] }] }), laneHeaders); + assert.equal(seed.status, 200); + const delta = await inc.post("/__bili/plugin/turn", JSON.stringify({ ...common, sinceSeq: seed.appliedSeq, msgIds: ["pinned-reply", "pinned-repeat"], messages: [ + { role: "assistant", content: [{ type: "text", text: "ok" }] }, + { role: "user", content: [{ type: "text", text: "continue", cache_control: MARK }] }, + ] }), laneHeaders); + assert.equal(delta.status, 200); + const messages = inc.forwarded.at(-1)!.messages as Wire[]; + assert.equal(messageBreakpoints(inc.forwarded.at(-1)!), 1); + assert.deepEqual(messages.at(-1)?.content.at(-1)?.cache_control, MARK); + assert.ok(messages[0].content.every((block) => block.cache_control === undefined), "the earlier identical prompt stays unmarked"); + } finally { await inc.close(); } +}); + test("rolling marks follow client-pinned identities on seed and delta", async () => { const inc = await harness(); try { @@ -270,8 +307,7 @@ for (const kind of ["document", "image", "tool-result", "signed-thinking"] as co const { modes } = await runIncremental(inc, requests); assert.deepEqual(modes, ["seed", "delta"]); for (let i = 0; i < requests.length; i++) { - assert.equal(canonMessages(inc.forwarded[i].messages), canonMessages(base.forwarded[i].messages)); - } + assert.equal(canonMessages(inc.forwarded[i].messages), canonMessages(base.forwarded[i].messages)); } } finally { await inc.close(); } }); } @@ -302,7 +338,6 @@ for (const removed of ["signature", "is_error"] as const) { const good = await inc.post("/__bili/plugin/turn", JSON.stringify(body), laneHeaders); assert.equal(good.status, 200, "same sequence still accepts the exact original"); assert.equal(inc.forwarded.length, before + 1); - assert.equal(canonMessages(inc.forwarded.at(-1)?.messages), canonMessages(base.forwarded.at(-1)?.messages), "rejected retouch changed neither payload nor the valid retry's rendered output"); - } finally { await inc.close(); } + assert.equal(canonMessages(inc.forwarded.at(-1)?.messages), canonMessages(base.forwarded.at(-1)?.messages), "rejected retouch changed neither payload nor the valid retry's rendered output"); } finally { await inc.close(); } }); } diff --git a/tests/pi-native.test.ts b/tests/pi-native.test.ts index e17d58da5d..2fa19d46bb 100644 --- a/tests/pi-native.test.ts +++ b/tests/pi-native.test.ts @@ -24,6 +24,11 @@ delete process.env.BILLION_CONTEXT_PROXY; // #820 coexistence: standalone billion-context-pi checks env at load time, so // the marker must be set synchronously during module evaluation (before our // async bootstrap writes BILLION_CONTEXT_PROXY) for its action-time back-off. +test("Pi package entry identifies billion-context", () => { + const manifest = JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url), "utf8")); + assert.deepEqual(manifest.pi.extensions, ["./dist/agent/billion-context.js"]); +}); + test("module evaluation marks the process as a native pi host", () => { assert.equal(process.env.BILLION_CONTEXT_NATIVE, "pi"); }); diff --git a/tests/plugin-protocol.test.ts b/tests/plugin-protocol.test.ts index 3deba4eb93..ead40e5411 100644 --- a/tests/plugin-protocol.test.ts +++ b/tests/plugin-protocol.test.ts @@ -65,8 +65,8 @@ function textScript(): string[] { ]; } -function compressToolScript(): string[] { - const compressArgs = JSON.stringify({ startId: "m00001", endId: "m00002", topic: "plugin-e2e-topic", summary: "plugin e2e compress summary" }); +function compressToolScript(summary = "plugin e2e compress summary with enough context about the folded range"): string[] { + const compressArgs = JSON.stringify({ startId: "m00001", endId: "m00002", topic: "plugin-e2e-topic", summary }); return [ anthropicSse("message_start", { type: "message_start", @@ -172,14 +172,15 @@ async function callPluginAnthropic( h: Harness, conversationId: string, messages: AnthropicMessage[], - opts: { tools?: unknown[]; stream?: boolean; contextWindow?: number } = {}, + opts: { tools?: unknown[]; stream?: boolean; contextWindow?: number; compressionProxy?: boolean; agent?: string } = {}, ): Promise<{ raw: string; events: SseEvent[]; json: Record<string, unknown> | undefined }> { const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", - "x-bili-plugin": "pi-plugin/0.0.1", + "x-bili-plugin": opts.agent ?? "pi-plugin/0.0.1", "x-bili-plugin-conversation": conversationId, + ...(opts.compressionProxy ? { "x-bili-plugin-compression": "proxy" } : {}), ...(opts.contextWindow !== undefined ? { "x-bili-plugin-context-window": String(opts.contextWindow) } : {}), }, body: JSON.stringify({ @@ -848,6 +849,58 @@ test("plugin-reported context window becomes the nudge denominator and is visibl } }); +function sizedPluginHistory(): AnthropicMessage[] { + const headFiller = "lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ".repeat(28); + const tailFiller = "enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat duis aute irure dolor in reprehenderit in voluptate. ".repeat(28); + const history: AnthropicMessage[] = []; + for (let i = 1; i <= 2; i++) { + history.push({ role: "user", content: `turn-${i}-marker question: ${headFiller}` }); + history.push({ role: "assistant", content: `turn-${i}-marker answer: ${headFiller}` }); + } + for (let i = 3; i <= 5; i++) { + history.push({ role: "user", content: `turn-${i} padding question: ${tailFiller}` }); + history.push({ role: "assistant", content: `turn-${i} padding answer: ${tailFiller}` }); + } + return history; +} + +test("plugin-bound proxy compression continues the tool loop instead of ending on the marker", async () => { + const h = await startHarness([compressToolScript(), textScript()], undefined, { visibilityMarkers: false }); + try { + const { raw } = await callPluginAnthropic(h, "20261031_032204_cec004da", sizedPluginHistory(), { + agent: "hermes", + tools: ["tool_search", "tool_describe", "tool_call"].map((name) => ({ + name, description: `Hermes ${name}`, input_schema: { type: "object", properties: {} }, + })), + compressionProxy: true, + }); + + assert.equal(h.captured.length, 2, `proxy compression must re-request the model after executing compress; raw=${raw}`); + assert.ok(raw.includes("plain answer"), `expected the continued answer, got: ${raw}`); + } finally { + await h.close(); + } +}); + +test("a successful compression summary containing FAILED still continues with markers hidden", async () => { + const summary = "summary records an earlier FAILED tool result while this compression itself succeeds"; + const h = await startHarness([compressToolScript(summary), textScript()], undefined, { visibilityMarkers: false }); + try { + const { raw } = await callPluginAnthropic(h, "plug-conv-failed-in-summary", sizedPluginHistory(), { + agent: "hermes", + tools: ["tool_search", "tool_describe", "tool_call"].map((name) => ({ + name, description: `Hermes ${name}`, input_schema: { type: "object", properties: {} }, + })), + compressionProxy: true, + }); + + assert.equal(h.captured.length, 2, `a success marker must not be classified by FAILED inside its summary; raw=${raw}`); + assert.ok(raw.includes("plain answer"), `expected the continued answer, got: ${raw}`); + } finally { + await h.close(); + } +}); + // Hermes defers plugin toolsets through tool_search, so a healthy plugin session sends requests // whose tools array carries NONE of the ACP names. Those requests must still bind the plugin // identity and budget (that is exactly what /acp reads) while compression falls back to the proxy — diff --git a/tests/plugin-wire-metadata.test.ts b/tests/plugin-wire-metadata.test.ts new file mode 100644 index 0000000000..dc519db53a --- /dev/null +++ b/tests/plugin-wire-metadata.test.ts @@ -0,0 +1,122 @@ +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import test from "node:test"; +import type { CoreMessage } from "acp-kernel"; +import { preparePluginWireMetadata, rememberPluginMessages } from "../src/plugin.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _resetSessionsForTest, getSession } from "../src/session.ts"; + +const messages: CoreMessage[] = [ + { id: "raw-a", role: "user", contentType: "text", text: "first" }, + { id: "raw-b", role: "assistant", contentType: "text", text: "second" }, + { id: "raw-c", role: "user", contentType: "text", text: "third" }, +]; + +test.beforeEach(() => { + _resetSessionsForTest(); + _setStoreForTest(new SessionStore({ dir: join(tmpdir(), "bili-wire-metadata-disabled"), enabled: false })); +}); +test.afterEach(() => _resetSessionsForTest()); + +function pluginSession(id: string) { + const session = getSession(id); + session.metadata.pluginAgent = "codex"; + return session; +} + +test("precomputed wire metadata matches raw-buffer snapshot evidence across wire shapes", () => { + const cases: { wire: unknown; comparable: boolean; raw?: string }[] = [ + { wire: { messages: [{ role: "user", content: "中😀" }] }, comparable: true }, + { wire: { input: [{ type: "message", content: [{ type: "input_text", text: "text" }] }] }, comparable: true }, + { wire: { contents: [{ role: "user", parts: [{ text: "text" }] }] }, comparable: true }, + { wire: { messages: null, input: "fallback" }, comparable: true }, + { wire: { messages: [{ content: [{ type: "image", source: {} }] }], input: "ignored" }, comparable: false }, + { wire: { messages: [{ content: [{ type: "tool_use", input: { type: "image" } }] }] }, comparable: true }, + // #4266 (landed after this PR's base): a payload-bearing thinking block projects to a + // named reasoning message whose text carries the wire bytes — comparable by view. + // Only payload-less thinking stays non-comparable (fail-closed). + { wire: { messages: [{ content: [{ type: "thinking", thinking: "signed" }] }] }, comparable: true }, + { wire: { messages: [{ content: "text", audio: {} }] }, comparable: false }, + { wire: null, comparable: false }, + { wire: 42, comparable: false }, + { wire: "text", comparable: false }, + { wire: undefined, raw: "{invalid", comparable: false }, + ]; + for (const [i, { wire, comparable, raw }] of cases.entries()) { + const body = Buffer.from(raw ?? JSON.stringify(wire, null, 2)); + const evidence = preparePluginWireMetadata(body, wire); + assert.deepEqual(evidence, { + wireHash: createHash("sha256").update(body).digest("hex"), + textComparable: comparable, + }); + const legacy = pluginSession(`raw-${i}`); + const prepared = pluginSession(`prepared-${i}`); + rememberPluginMessages(legacy.id, messages, messages, undefined, body); + rememberPluginMessages(prepared.id, messages, messages, undefined, evidence); + assert.equal(prepared.metadata.publicSnapshotWireHash, legacy.metadata.publicSnapshotWireHash); + assert.equal(prepared.metadata.publicSnapshotTextComparable, legacy.metadata.publicSnapshotTextComparable); + assert.equal(prepared.revisionEpoch, legacy.revisionEpoch); + } +}); + +test("preparing wire metadata neither reparses nor commits, and survives input mutation", (t) => { + const session = pluginSession("deferred"); + const wire: Record<string, unknown> = { messages: [{ role: "user", content: "original" }] }; + const body = Buffer.from(JSON.stringify(wire)); + const expectedHash = createHash("sha256").update(body).digest("hex"); + const parse = t.mock.method(JSON, "parse", () => { throw new Error("must reuse the parsed request"); }); + const evidence = preparePluginWireMetadata(body, wire); + assert.equal(parse.mock.callCount(), 0); + parse.mock.restore(); + assert.equal(session.metadata.publicSnapshotWireHash, undefined); + assert.equal(session.metadata.publicSnapshotTextComparable, undefined); + assert.equal(session.pluginSnapshot, undefined); + assert.equal(session.revisionEpoch, undefined); + + body.fill(0); + wire.messages = [{ content: [{ type: "image", source: {} }] }]; + rememberPluginMessages(session.id, messages, messages, undefined, evidence); + assert.equal(session.metadata.publicSnapshotWireHash, expectedHash); + assert.equal(session.metadata.publicSnapshotTextComparable, true); +}); + +test("wire evidence preserves hash-gated bookkeeping and rejected-view guards", () => { + const session = pluginSession("bookkeeping"); + const wire = { messages: [{ role: "user", content: "text" }] }; + const evidence = preparePluginWireMetadata(Buffer.from(JSON.stringify(wire)), wire); + rememberPluginMessages(session.id, messages, messages, undefined, evidence); + const revision = session.revisionEpoch; + const snapshot = session.pluginSnapshot; + rememberPluginMessages(session.id, messages, messages, undefined, evidence); + assert.equal(session.revisionEpoch, revision, "identical retries do not mark dirty"); + + const reformatted = preparePluginWireMetadata(Buffer.from(JSON.stringify(wire, null, 2)), wire); + assert.notEqual(reformatted.wireHash, evidence.wireHash, "hash follows exact request bytes"); + rememberPluginMessages(session.id, messages, messages, undefined, reformatted); + assert.equal(session.metadata.publicSnapshotWireHash, reformatted.wireHash); + assert.equal(session.revisionEpoch, revision, "a new hash with the same verdict does not mark dirty"); + + const opaque = { input: [{ type: "input_image", image_url: "data:image/png;base64,AA==" }] }; + const opaqueEvidence = preparePluginWireMetadata(Buffer.from(JSON.stringify(opaque)), opaque); + rememberPluginMessages(session.id, messages, messages, undefined, opaqueEvidence); + assert.equal(session.metadata.publicSnapshotTextComparable, false); + assert.equal(session.revisionEpoch, (revision ?? 0) + 1, "changed comparability marks dirty"); + assert.equal(session.pluginSnapshot, snapshot, "wire evidence does not replace the message snapshot"); + rememberPluginMessages(session.id, messages, messages, undefined, { ...opaqueEvidence, textComparable: true }); + assert.equal(session.metadata.publicSnapshotTextComparable, false, "the saved hash gates verdict changes"); + + const side: CoreMessage[] = [{ id: "unrelated", role: "user", contentType: "text", text: "side request" }]; + rememberPluginMessages(session.id, side, side, undefined, evidence); + assert.equal(session.metadata.publicSnapshotWireHash, opaqueEvidence.wireHash); + assert.equal(session.metadata.publicSnapshotTextComparable, false); + assert.equal(session.revisionEpoch, (revision ?? 0) + 1); + + const nonPlugin = getSession("non-plugin"); + rememberPluginMessages(nonPlugin.id, messages, messages, undefined, evidence); + assert.equal(nonPlugin.metadata.publicSnapshotWireHash, undefined); + const empty = pluginSession("empty"); + rememberPluginMessages(empty.id, [], [], undefined, evidence); + assert.equal(empty.metadata.publicSnapshotWireHash, undefined); +}); diff --git a/tests/proxy-readbody.test.ts b/tests/proxy-readbody.test.ts index 2349ea2b55..6903af5108 100644 --- a/tests/proxy-readbody.test.ts +++ b/tests/proxy-readbody.test.ts @@ -2,6 +2,8 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { Readable } from "node:stream"; import { EventEmitter } from "node:events"; +import { IncomingMessage } from "node:http"; +import { Socket } from "node:net"; import { BodyTooLargeError, readBody } from "../src/server.ts"; import { MAX_REQUEST_BYTES } from "../src/fetch-util.ts"; @@ -19,6 +21,60 @@ test("readBody: returns buffer for a normal request", async () => { assert.equal(out.toString("utf8"), "hello"); }); +for (const outcome of ["success", "error"] as const) { + test(`readBody: releases its request listeners after ${outcome}`, async () => { + const req = new EventEmitter(); + const externalListener = () => {}; + const events = ["data", "end", "error", "close"] as const; + for (const event of events) req.on(event, externalListener); + const pending = readBody(req as never); + req.emit("data", Buffer.from("hello")); + if (outcome === "success") { + req.emit("end"); + assert.equal((await pending).toString("utf8"), "hello"); + } else { + const error = new Error("read failure"); + const rejection = assert.rejects(pending, (actual: unknown) => actual === error); + req.emit("error", error); + await rejection; + } + for (const event of events) { + assert.deepEqual(req.listeners(event), [externalListener], `${event}: readBody must remove only its own listener`); + } + }); +} + +for (const withError of [false, true]) { + test(`readBody: real IncomingMessage cancellation ${withError ? "with" : "without"} an error settles and cleans up`, { timeout: 2000 }, async () => { + const req = new IncomingMessage(new Socket()); + const error = withError ? new Error("client disconnected") : undefined; + const closed = new Promise<void>((resolve) => req.once("close", resolve)); + const pending = readBody(req); + const rejection = assert.rejects(pending, (actual: unknown) => withError ? actual === error : actual instanceof Error); + req.push(Buffer.from("hello")); + setImmediate(() => req.destroy(error)); + await rejection; + await closed; + for (const event of ["data", "end", "error", "close"]) assert.equal(req.listenerCount(event), 0, event); + }); +} + +test("readBody: a real IncomingMessage over the limit keeps BodyTooLargeError through destroy", { timeout: 2000 }, async (t) => { + const previousLimit = process.env.BILI_RECEIVE_BODY_BYTES; + process.env.BILI_RECEIVE_BODY_BYTES = "8"; + t.after(() => { process.env.BILI_RECEIVE_BODY_BYTES = previousLimit; }); + const req = new IncomingMessage(new Socket()); + const closed = new Promise<void>((resolve) => req.once("close", resolve)); + const pending = readBody(req); + const rejection = assert.rejects(pending, (error: unknown) => error instanceof BodyTooLargeError && error.limit === 8); + req.push(Buffer.from("1234")); + req.push(Buffer.from("56789")); + await rejection; + await closed; + assert.equal(req.destroyed, true); + for (const event of ["data", "end", "error", "close"]) assert.equal(req.listenerCount(event), 0, event); +}); + test("readBody: rejects with BodyTooLargeError AND destroys socket when body exceeds MAX_REQUEST_BYTES", async () => { const req = new EventEmitter(); let destroyed = false; diff --git a/tests/ptc-runcode-tools.test.ts b/tests/ptc-runcode-tools.test.ts index 8ea67e61cc..4203389d91 100644 --- a/tests/ptc-runcode-tools.test.ts +++ b/tests/ptc-runcode-tools.test.ts @@ -23,7 +23,7 @@ process.env.BILI_PERSIST = "0"; import { defaultConfig } from "acp-kernel"; import { handlePluginManifest } from "../src/plugin.ts"; import type { ProxyOptions } from "../src/config.ts"; -import { injectGoogleTool, injectOpenaiTool, injectResponsesTool, injectTool, injectSystem } from "../src/server/inject.ts"; +import { CODEX_EXEC_EXEMPT_NOTE, injectGoogleTool, injectOpenaiTool, injectResponsesTool, injectTool, injectSystem } from "../src/server/inject.ts"; import { PROJECT_URL_NOTE, PTC_BINDING_NOTE, PTC_EXEMPT_NOTE, PROJECT_ATTRIBUTION_LINE, withProjectAttribution } from "../src/compress-tool.ts"; const RUN_CODE = { name: "run_code", description: "Run a program in the sandbox." }; @@ -32,7 +32,7 @@ const BASH = { name: "bash", description: "Shell out." }; function flat(tools: unknown[]): Map<string, string> { return new Map(tools.map((t) => { const o = t as { name?: unknown; description?: unknown }; - return [String(o.name), typeof o.description === "string" ? o.description : ""]; + return [String(o?.name), typeof o?.description === "string" ? o.description : ""]; })); } @@ -96,6 +96,64 @@ test("injectResponsesTool: run_code client → exempt note, flat shape", () => { assert.equal(out.get("run_code"), RUN_CODE.description, "client entry untouched"); }); +const CODEX_EXEC = { type: "custom", name: "exec", description: "Run JavaScript code to orchestrate tool calls." }; + +test("injectResponsesTool: qualified Codex exec teaches direct calls without changing host tools (#5592)", () => { + const shapes = [ + [{ ...CODEX_EXEC, name: "functions.exec" }], + [{ ...CODEX_EXEC, type: "function", name: "functions.exec" }], + [{ type: "namespace", name: "functions", tools: [CODEX_EXEC] }], + [{ type: "additional_tools", tools: [{ type: "namespace", name: "functions", tools: [CODEX_EXEC] }] }], + ]; + for (const tools of shapes) { + const before = JSON.stringify(tools); + const out = injectResponsesTool(tools); + const descriptions = flat(out); + assert.equal(out[0], tools[0], "host declarations retain object identity"); + assert.equal(JSON.stringify(tools), before, "host declarations remain byte-exact"); + for (const name of ["compress", "decompress", "search_context", "acp_status", "acp_cache"]) { + assert.ok(descriptions.get(name)?.includes(CODEX_EXEC_EXEMPT_NOTE), name); + assert.ok(!descriptions.get(name)?.includes(PTC_BINDING_NOTE), name); + } + assert.equal(JSON.stringify(out), JSON.stringify(injectResponsesTool(tools)), "stable prompt bytes"); + assert.deepEqual(injectResponsesTool(out), out, "repeated injection does not duplicate notes"); + } +}); + +test("injectResponsesTool: ordinary exec and unrelated namespaces do not acquire Codex hints (#5592)", () => { + const shapes: unknown[][] = [ + [CODEX_EXEC], + [{ type: "function", name: "exec", description: "Run a shell command." }], + [{ type: "namespace", name: "functions", tools: [{ type: "function", name: "shell" }] }], + [{ type: "namespace", name: "other", tools: [CODEX_EXEC] }], + [{ type: "namespace", name: "functions", tools: [{ type: "namespace", name: "exec", tools: [] }] }], + [{ type: "namespace", name: "functions", tools: null }], + [{ type: "namespace", name: "functions.exec", tools: [] }], + [{ type: "function", name: "functions", tools: [CODEX_EXEC] }], + [{ type: "namespace", name: "other", tools: [{ ...CODEX_EXEC, name: "functions.exec" }] }], + [null, undefined, "functions.exec"], + ]; + for (const tools of shapes) { + for (const description of flat(injectResponsesTool(tools)).values()) { + assert.ok(!description.includes(CODEX_EXEC_EXEMPT_NOTE)); + } + } +}); + +test("injectResponsesTool: Codex notes preserve native MCP tools and pack-owned descriptions (#5592)", () => { + const native = { type: "namespace", name: "mcp__bili", tools: [{ type: "function", name: "compress", description: "Native MCP binding" }] }; + const tools = [{ type: "namespace", name: "functions", tools: [CODEX_EXEC] }, native]; + const pack = { compress: { description: "Pack-owned compress wording." } }; + const out = injectResponsesTool(tools, undefined, pack); + assert.equal(out[1], native); + assert.equal(flat(out).get("compress"), pack.compress.description); + assert.ok(flat(out).get("acp_status")?.includes(CODEX_EXEC_EXEMPT_NOTE)); + for (const entries of Object.values(readManifestTools())) { + assert.ok(entries.get("compress")?.includes(PTC_BINDING_NOTE)); + assert.ok(!entries.get("compress")?.includes(CODEX_EXEC_EXEMPT_NOTE)); + } +}); + test("injectGoogleTool: run_code declaration → exempt note on appended declarations", () => { const out = injectGoogleTool([{ functionDeclarations: [{ name: "run_code", description: "prog" }] }] as never); const decls = new Map(out.flatMap((t) => (t.functionDeclarations ?? []).map((d) => [d.name, d.description] as const)) as [string, string][]); diff --git a/tests/rejection-diagnostics.test.ts b/tests/rejection-diagnostics.test.ts new file mode 100644 index 0000000000..b6802fdb7a --- /dev/null +++ b/tests/rejection-diagnostics.test.ts @@ -0,0 +1,149 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { rejectionDiagnostics, rejectionDiagnosticSuffix } from "../src/server/rejection-diagnostics.ts"; + +// #4452: a fan-out relay answers a total failure with ONE aggregate body whose +// real cause sits nested in the last attempt. bili's log line keeps only the +// first 600 bytes, i.e. "all targets failed" — the cause was undiagnosable. +// These pin the extractor's contract: read ONE terminal nested message, bounded +// and credential-scrubbed, changing nothing about a body that carries none. + +/** Build the aggregate shape a fan-out relay returns on total failure. */ +function aggregate(attempts: unknown[], outer = "All targets failed"): string { + return JSON.stringify({ error: { message: outer, type: "upstream_error" }, attempts }); +} + +function attempt(message: unknown): Record<string, unknown> { + return { target: "t", details: { error: { message } } }; +} + +test("reads the terminal attempt's nested cause", () => { + const body = aggregate([attempt("connection reset by peer"), attempt("upstream returned 529 overloaded")]); + const diag = rejectionDiagnostics(body); + assert.ok(diag); + assert.equal(diag.message, "upstream returned 529 overloaded"); + assert.equal(diag.attemptIndex, 1); + assert.equal(diag.attempts, 2); +}); + +test("a single attempt is indexed 0 and the suffix omits the position", () => { + const diag = rejectionDiagnostics(aggregate([attempt("model not found")])); + assert.ok(diag); + assert.equal(diag.attemptIndex, 0); + assert.equal(diag.attempts, 1); +}); + +test("later attempts win over earlier retry noise", () => { + const body = aggregate([attempt("noise early"), attempt("noise mid"), attempt("densely informative final cause")]); + assert.equal(rejectionDiagnostics(body)?.message, "densely informative final cause"); +}); + +test("a terminal attempt with no usable message falls back to the nearest informative earlier one", () => { + const body = aggregate([attempt("real cause"), { target: "t", details: {} }, attempt(undefined)]); + const diag = rejectionDiagnostics(body); + assert.ok(diag); + assert.equal(diag.message, "real cause"); +}); + +test("an empty nested message is skipped", () => { + const body = aggregate([attempt("")]); + assert.equal(rejectionDiagnostics(body), undefined); + assert.equal(rejectionDiagnosticSuffix(body), ""); +}); + +test("empty / non-string message at every attempt yields no diagnostic", () => { + assert.equal(rejectionDiagnostics(aggregate([attempt(12), attempt(null), attempt("")])), undefined); + assert.equal(rejectionDiagnosticSuffix(aggregate([attempt(12), attempt(null)])), ""); +}); + +test("a payload with no attempts[] is untouched (no diagnostic)", () => { + const bodies = [ + '{"error":{"message":"Internal server error"}}', + '{"error":{"message":"context_length_exceeded: 200000 tokens"}}', + '{"attempts":[]}', + '{"attempts":"not-an-array"}', + "{not json", + "Internal Server Error", + "<html><body>502 Bad Gateway</body></html>", + "", + ]; + for (const body of bodies) { + assert.equal(rejectionDiagnostics(body), undefined, `expected none for: ${body}`); + assert.equal(rejectionDiagnosticSuffix(body), "", `expected empty suffix for: ${body}`); + } +}); + +test("non-object / non-array attempt entries are skipped, not thrown on", () => { + const body = aggregate([null, "string-attempt", 4, [], { details: null }, { details: { error: null } }, attempt("surviving cause")]); + assert.equal(rejectionDiagnostics(body)?.message, "surviving cause"); +}); + +test("null / undefined payload is tolerated and yields no suffix", () => { + assert.equal(rejectionDiagnostics(null), undefined); + assert.equal(rejectionDiagnostics(undefined), undefined); + assert.equal(rejectionDiagnosticSuffix(null), ""); + assert.equal(rejectionDiagnosticSuffix(undefined), ""); +}); + +test("whitespace, newlines and control shapes (incl. C1 NEL) are collapsed into ONE line", () => { + const hostile = "boom\r\n[attacker] ← upstream 200: forged line\u0085forged2\n\tsecond\u009c"; + const suffix = rejectionDiagnosticSuffix(aggregate([attempt(hostile)])); + assert.ok(!suffix.includes("\n"), "no newline may survive into the log line"); + assert.ok(!suffix.includes("\r"), "no carriage return may survive"); + assert.ok(!suffix.includes("\u0085"), "C1 next-line (U+0085) may not survive as a field break"); + assert.ok(!suffix.includes("\u009c"), "C1 file-separator (U+009C) may not survive as a field break"); +}); + +test("credential-shaped text is scrubbed by the shared sanitizer", () => { + const body = aggregate([attempt("rejected: authorization: Bearer abcdefghijklmnopqrstuvwxyz012345")]); + const suffix = rejectionDiagnosticSuffix(body); + assert.ok(!suffix.includes("abcdefghijklmnopqrstuvwxyz012345"), "bearer token must not reach the log"); +}); + +test("an sk- API key echoed by a provider is masked", () => { + const suffix = rejectionDiagnosticSuffix(aggregate([attempt("incorrect key sk-proj-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA supplied")])); + assert.ok(!suffix.includes("sk-proj-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")); +}); + +test("a long cause is clamped, and the clamp is bounded", () => { + const suffix = rejectionDiagnosticSuffix(aggregate([attempt("x".repeat(4000))])); + // 300 retained chars + the ellipsis marker + the fixed prefix. + assert.ok(suffix.length < 340, `suffix too long: ${suffix.length}`); + assert.ok(suffix.endsWith(" …")); +}); + +test("an oversized body is not parsed at all", () => { + const huge = aggregate([attempt("y".repeat(64 * 1024 + 10))]); + assert.equal(rejectionDiagnostics(huge), undefined); + assert.equal(rejectionDiagnosticSuffix(huge), ""); +}); + +test("the suffix never echoes the outer narrative or the raw payload", () => { + const body = aggregate([attempt("inner cause")], "All targets failed: secret-route-label"); + const suffix = rejectionDiagnosticSuffix(body); + assert.ok(!suffix.includes("All targets failed")); + assert.ok(!suffix.includes("secret-route-label")); + assert.ok(!suffix.includes("{"), "no JSON structure may leak into the log"); +}); + +test("reads the terminal provider cause from CCR's nested error envelope", () => { + const body = JSON.stringify({ + error: { + message: "All target providers failed.", + attempts: [attempt("earlier transient error"), attempt("messages.32: tool_use has no following tool_result")], + }, + }); + assert.equal(rejectionDiagnostics(body)?.message, "messages.32: tool_use has no following tool_result"); + assert.equal(rejectionDiagnostics(body)?.attemptIndex, 1); +}); + +test("terminal control bytes cannot reach the diagnostic line", () => { + const body = aggregate([attempt("refused\u001b[2J\u0000\u007f by provider")]); + assert.equal(rejectionDiagnostics(body)?.message, "refused [2J by provider"); +}); + +test("the parsing budget counts UTF-8 bytes rather than UTF-16 characters", () => { + const body = aggregate([attempt("错".repeat(24 * 1024))]); + assert.ok(body.length < 64 * 1024); + assert.equal(rejectionDiagnostics(body), undefined); +}); diff --git a/tests/request-input-lifetime.test.ts b/tests/request-input-lifetime.test.ts new file mode 100644 index 0000000000..8e313a5ba8 --- /dev/null +++ b/tests/request-input-lifetime.test.ts @@ -0,0 +1,144 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { randomUUID } from "node:crypto"; +import test from "node:test"; +import { createCore, defaultConfig, type CoreMessage } from "acp-kernel"; +import type { ProxyOptions } from "../src/config.ts"; +import type { Prepared } from "../src/server.ts"; +import { forward } from "../src/server/relay.ts"; +import { getSession } from "../src/session.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _liveUpstreamTimersForTest, _resetFetchUtilForTest } from "../src/fetch-util.ts"; + +process.env.NODE_ENV = "test"; +_setStoreForTest(new SessionStore({ enabled: false })); + +const OVERFLOW = JSON.stringify({ error: { type: "invalid_request_error", message: "prompt is too long: 130000 tokens > 128000 maximum" } }); +const RETRY_REJECTION = JSON.stringify({ error: { type: "invalid_request_error", message: "refold retry rejected" } }); +function deferred<T>() { + let resolve!: (value: T) => void; + let reject!: (error: unknown) => void; + const promise = new Promise<T>((yes, no) => { resolve = yes; reject = no; }); + return { promise, resolve, reject }; +} + +for (const scenario of ["success", "refold", "declined", "throwing", "rejected"] as const) { + test(`request input release boundary: ${scenario}`, { timeout: 10_000 }, async t => { + const url = "http://request-lifetime.test/responses"; + const session = getSession(`input-lifetime-${randomUUID()}`); + const core = createCore(); + const config = defaultConfig(200_000); + const messages: CoreMessage[] = [{ id: "m1", role: "user", contentType: "text", text: "Continue." }]; + const body = JSON.stringify({ model: "test-model", stream: true, input: [{ role: "user", content: "Continue." }] }); + const prepared: Prepared = { + body, session, processedMessages: messages, originalMessages: messages, + protocol: "responses", stream: true, compressInjected: false, + }; + const opts: ProxyOptions = { + host: "127.0.0.1", port: 0, upstream: url, routes: {}, + proxy: "", proxyMode: "direct", proxySource: "direct", + modelContextLimit: 200_000, kernelConfig: config, + compress: { injectTool: false, injectNudge: false }, + promptCache: { routing: "auto" }, compat: { roles: {} }, + streamErrorShape: "protocol", sessionHeader: "x-acp-session", + log: false, debug: false, passthrough: false, passthroughSource: null, + autoUpdate: false, autoRestartOnUpdate: false, updateTag: "latest", + advisoryCheck: false, releaseNotesCheck: false, mitm: { enabled: false, domains: [] }, + }; + const released = deferred<void>(); + const completed = deferred<void>(); + const trace: string[] = []; + let releaseCount = 0; + let forwardCompleted = false; + let upstreamCalls = 0; + let streamController: ReadableStreamDefaultController<Uint8Array> | undefined; + const nativeFetch = globalThis.fetch; + t.mock.method(globalThis, "fetch", async (_input: unknown, init?: RequestInit) => { + upstreamCalls++; + trace.push(`fetch:${upstreamCalls}`); + assert.equal(releaseCount, 0, "inbound reconstruction must remain available until the retry is accepted"); + if (scenario !== "success" && upstreamCalls === 1) { + return new Response(OVERFLOW, { status: 400, headers: { "content-type": "application/json" } }); + } + assert.equal(init?.body, upstreamCalls === 1 ? body : `${body} `, "the accepted request keeps its outgoing bytes"); + if (scenario === "rejected") { + return new Response(RETRY_REJECTION, { status: 422, headers: { "content-type": "application/json" } }); + } + return new Response(new ReadableStream<Uint8Array>({ start(controller) { streamController = controller; } }), { + headers: { "content-type": "text/event-stream" }, + }); + }); + const server = http.createServer((req, res) => { + req.resume(); + req.url = url; + void forward(req, res, opts, body, prepared, core, config, () => {}, undefined, "input-lifetime", + undefined, + async () => { + trace.push("refold"); + assert.equal(releaseCount, 0, "the refold callback still owns original input"); + if (scenario === "declined") return null; + if (scenario === "throwing") throw new Error("cannot refold"); + return { body: `${body} `, prepared: { ...prepared, body: `${body} ` } }; + }, + undefined, + undefined, + undefined, + () => { releaseCount++; trace.push("release"); released.resolve(); }, + ).then(() => { forwardCompleted = true; completed.resolve(); }, error => { res.destroy(); completed.reject(error); }); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const address = server.address(); + assert.ok(address && typeof address === "object"); + const client = nativeFetch(`http://127.0.0.1:${address.port}`, { method: "POST", body }); + // Attach rejection handlers immediately; cleanup can abort either side. + void client.catch(() => {}); + void completed.promise.catch(() => {}); + let deadline: ReturnType<typeof setTimeout> | undefined; + try { + if (scenario === "success" || scenario === "refold") { + await Promise.race([ + released.promise, + completed.promise.then(() => assert.fail("forward finished before input release")), + new Promise<never>((_, reject) => { deadline = setTimeout(() => reject(new Error("input retained while accepted response stream is pending")), 5000); }), + ]); + assert.equal(forwardCompleted, false); + assert.equal(releaseCount, 1); + assert.ok(streamController, "upstream stream remains pending at the release boundary"); + streamController.enqueue(new TextEncoder().encode(`event: response.completed\ndata: ${JSON.stringify({ type: "response.completed", response: { id: "r1", status: "completed", output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: "done" }] }] } })}\n\n`)); + streamController.close(); + streamController = undefined; + } + const response = await client; + const responseText = await response.text(); + await completed.promise; + assert.equal(releaseCount, 1, "release runs exactly once, including error responses"); + if (scenario === "declined" || scenario === "throwing") { + assert.equal(response.status, 400); + assert.equal(responseText, OVERFLOW); + } else if (scenario === "rejected") { + assert.equal(response.status, 422); + assert.equal(responseText, RETRY_REJECTION); + } else { + assert.equal(response.status, 200); + assert.ok(responseText.includes("done")); + } + const expectsRetry = scenario === "refold" || scenario === "rejected"; + assert.equal(upstreamCalls, expectsRetry ? 2 : 1); + assert.deepEqual(trace, scenario === "success" ? ["fetch:1", "release"] + : expectsRetry ? ["fetch:1", "refold", "fetch:2", "release"] + : ["fetch:1", "refold", "release"]); + } finally { + if (deadline) clearTimeout(deadline); + streamController?.close(); + const closed = once(server, "close"); + server.close(); + server.closeAllConnections(); + await closed; + await completed.promise.catch(() => {}); + assert.equal(_liveUpstreamTimersForTest(), 0); + _resetFetchUtilForTest(); + } + }); +} diff --git a/tests/responses-ws-cache-diagnostics.test.ts b/tests/responses-ws-cache-diagnostics.test.ts new file mode 100644 index 0000000000..3e15d7e7a8 --- /dev/null +++ b/tests/responses-ws-cache-diagnostics.test.ts @@ -0,0 +1,168 @@ +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { once } from "node:events"; +import test from "node:test"; +import { WebSocketServer } from "ws"; +import { ResponsesWsUpstream } from "../src/responses-ws.ts"; + +type Item = Record<string, unknown>; +const hash = (text: string): string => createHash("sha256").update(text).digest("hex"); +const user = (text: string): Item => ({ type: "message", role: "user", content: text }); +const output: Item[] = [{ type: "reasoning", encrypted_content: "private-reasoning" }]; +const completed = (id: string, fields: Item = {}): Item => ({ + type: "response.completed", response: { id, status: "completed", output, ...fields }, +}); + +async function fixture(replies: Array<Item | Item[]>) { + const server = new WebSocketServer({ port: 0, host: "127.0.0.1" }); + await once(server, "listening"); + const address = server.address(); + assert.ok(address && typeof address !== "string"); + const rows: string[] = []; + const sent: string[] = []; + const logs: string[] = []; + server.on("connection", peer => peer.on("message", raw => { + rows.push(raw.toString()); + const reply = replies.shift() ?? { type: "error", error: { code: "unexpected-request" } }; + for (const item of Array.isArray(reply) ? reply : [reply]) { + const frame = JSON.stringify(item); + sent.push(frame); + peer.send(frame); + } + })); + const transport = new ResponsesWsUpstream((_level, message) => logs.push(message), undefined, undefined, true); + return { + rows, sent, logs, transport, + diagnostics: (): Item[] => logs.filter(line => line.startsWith("upstream cache-diagnostic ")).map(line => JSON.parse(line.slice("upstream cache-diagnostic ".length)) as Item), + turn: async (body: Item): Promise<string> => { + const response = await transport.fetch(`http://127.0.0.1:${address.port}/responses?api-key=private-query-key`, { + method: "POST", + headers: { authorization: "Bearer private-auth-key", session_id: "private-session-header" }, + body: JSON.stringify(body), + }); + return response.text(); + }, + close: async (): Promise<void> => { + transport.close(); + for (const peer of server.clients) peer.terminate(); + await new Promise<void>((resolve, reject) => server.close(error => error ? reject(error) : resolve())); + }, + }; +} + +for (const stream of [true, false]) { + test(`WS cache diagnostics: zero and missing usage remain distinct (stream=${stream})`, { timeout: 10000 }, async () => { + const cases = [ + { fields: { usage: { input_tokens: 2048, output_tokens: 5, input_tokens_details: { cached_tokens: 0 } } }, usage: true, details: true, present: true, cached: 0 }, + { fields: {}, usage: false, details: false, present: false, cached: null }, + { fields: { usage: { input_tokens: 2048 } }, usage: true, details: false, present: false, cached: null }, + { fields: { usage: { input_tokens_details: {} } }, usage: true, details: true, present: false, cached: null }, + { fields: { usage: { input_tokens_details: { cached_tokens: 1024 } } }, usage: true, details: true, present: true, cached: 1024 }, + { fields: { usage: { input_tokens_details: { cached_tokens: "private-invalid-usage" } } }, usage: true, details: true, present: true, cached: null }, + { fields: { usage: null }, usage: true, details: false, present: false, cached: null }, + ]; + const f = await fixture(cases.map((row, i) => completed(`resp_${i}`, row.fields))); + try { + let input = [user("private-prompt")]; + for (const [i, row] of cases.entries()) { + const received = await f.turn({ model: "test-model", input, stream }); + if (stream) assert.equal(received, `event: response.completed\ndata: ${f.sent[i]}\n\n`); + else assert.deepEqual(JSON.parse(received), JSON.parse(f.sent[i]).response); + const terminal = f.diagnostics().filter(event => event.phase === "terminal").at(-1)!; + assert.equal(terminal.usage_present, row.usage); + assert.equal(terminal.input_tokens_details_present, row.details); + assert.equal(terminal.cached_tokens_present, row.present); + assert.equal(terminal.cached_tokens, row.cached); + assert.equal(terminal.input_tokens_present, i === 0 || i === 2); + assert.equal(terminal.input_tokens, i === 0 || i === 2 ? 2048 : null); + assert.equal(terminal.output_tokens_present, i === 0); + assert.equal(terminal.output_tokens, i === 0 ? 5 : null); + assert.equal(terminal.event, "response.completed"); + assert.equal(terminal.response_id_sha256, hash(`resp_${i}`)); + assert.equal(terminal.connection, 1); + assert.equal(terminal.send, i + 1); + input = [...input, ...output, user("private-next-prompt")]; + } + const log = f.logs.join("\n"); + for (const secret of ["private-prompt", "private-reasoning", "private-auth-key", "private-query-key", "private-session-header", "private-invalid-usage", "resp_0"]) assert.ok(!log.includes(secret), secret); + } finally { await f.close(); } + }); +} + +test("WS cache diagnostics: fingerprints describe exact egress, deltas and reconnects without changing bytes", { timeout: 10000 }, async () => { + const f = await fixture([completed("resp_first"), completed("resp_second"), completed("resp_third"), completed("resp_fourth")]); + try { + const settings = { model: "test-model", instructions: "private-instructions", tools: [{ type: "function", name: "private-tool", parameters: {} }], prompt_cache_key: "private-cache-key" }; + const transportFields = { stream: true, stream_options: { include_usage: true } }; + const firstInput = [user("private-prompt")]; + const secondInput = [...firstInput, ...output, user("private-next-prompt")]; + const thirdInput = [...secondInput, ...output, user("private-third-prompt")]; + await f.turn({ ...settings, input: firstInput, ...transportFields }); + await f.turn({ ...settings, input: secondInput, ...transportFields }); + const changedSettings = { ...settings, instructions: "private-changed-instructions" }; + await f.turn({ ...changedSettings, input: thirdInput, ...transportFields }); + f.transport.close(); + await f.turn({ ...changedSettings, input: thirdInput, ...transportFields }); + assert.deepEqual(f.rows, [ + JSON.stringify({ ...settings, input: firstInput, ...transportFields, type: "response.create" }), + JSON.stringify({ ...settings, input: [user("private-next-prompt")], previous_response_id: "resp_first", ...transportFields, type: "response.create" }), + JSON.stringify({ ...changedSettings, input: thirdInput, ...transportFields, type: "response.create" }), + JSON.stringify({ ...changedSettings, input: thirdInput, ...transportFields, type: "response.create" }), + ]); + const sends = f.diagnostics().filter(event => event.phase === "send"); + assert.equal(sends.length, 4); + for (const [i, row] of sends.entries()) { + assert.equal(row.frame_sha256, hash(f.rows[i])); + assert.equal(row.send, i + 1); + assert.equal(row.connection, i === 3 ? 2 : 1); + } + assert.deepEqual(sends.map(row => row.mode), ["full", "delta", "full", "full"]); + // The send record is an allowlist. A hash of the full effective request is deliberately + // absent: it costs a second full-body serialization on every send, including deltas. + for (const row of sends) assert.deepEqual(Object.keys(row).sort(), ["connection", "frame_sha256", "mode", "phase", "previous_response_id_sha256", "send", "settings_sha256"]); + assert.equal(sends[0].settings_sha256, sends[1].settings_sha256); + assert.notEqual(sends[1].settings_sha256, sends[2].settings_sha256); + assert.deepEqual(sends.map(row => row.previous_response_id_sha256), [null, hash("resp_first"), null, null]); + for (const secret of ["private-prompt", "private-instructions", "private-changed-instructions", "private-tool", "private-cache-key", "private-auth-key", "private-query-key", "private-session-header", "private-reasoning", "resp_first"]) assert.ok(!f.logs.join("\n").includes(secret), secret); + } finally { await f.close(); } +}); + +test("WS cache diagnostics: continuation recovery correlates terminal usage with the actual resend", { timeout: 10000 }, async () => { + const rejection = { type: "error", status: 400, error: { code: "previous_response_not_found", message: "private-error-message" } }; + const f = await fixture([completed("resp_seed"), rejection, completed("resp_recovered", { usage: { input_tokens_details: { cached_tokens: 0 } } })]); + try { + const input = [user("seed")]; + await f.turn({ model: "test-model", input, stream: true }); + await f.turn({ model: "test-model", input: [...input, ...output, user("next")], stream: true }); + const rows = f.diagnostics(); + assert.deepEqual(rows.filter(row => row.phase === "send").map(row => [row.send, row.mode, row.connection]), [[1, "full", 1], [2, "delta", 1], [3, "full", 1]]); + assert.deepEqual(rows.filter(row => row.phase === "terminal").map(row => [row.send, row.event]), [[1, "response.completed"], [2, "error"], [3, "response.completed"]]); + assert.equal(rows.at(-1)?.cached_tokens, 0); + assert.ok(!f.logs.join("\n").includes("private-error-message")); + } finally { await f.close(); } +}); + +for (const event of ["response.completed", "response.failed", "response.incomplete", "error"]) { + test(`WS cache diagnostics: only terminal ${event} is recorded`, { timeout: 10000 }, async () => { + const terminal = event === "error" + ? { type: event, error: { message: "private-error-message" } } + : { type: event, response: { id: "private-response-id", status: event.slice("response.".length), output, usage: { input_tokens_details: { cached_tokens: 0 } } } }; + const f = await fixture([[ + { type: "response.created", response: { id: "private-created-id", usage: { input_tokens_details: { cached_tokens: 9999 } } } }, + { type: "response.output_text.delta", delta: "private-text-delta" }, + terminal, + ]]); + try { + const received = await f.turn({ model: "test-model", input: [user("private-prompt")], stream: true }); + assert.equal(received, f.sent.map(frame => `event: ${(JSON.parse(frame) as Item).type}\ndata: ${frame}\n\n`).join("")); + const rows = f.diagnostics(); + assert.equal(rows.length, 2); + assert.equal(rows[0].phase, "send"); + assert.equal(rows[1].phase, "terminal"); + assert.equal(rows[1].event, event); + assert.equal(rows[1].cached_tokens, event === "error" ? null : 0); + assert.equal(rows[1].response_id_sha256, event === "error" ? null : hash("private-response-id")); + for (const secret of ["private-text-delta", "private-error-message", "private-created-id", "private-response-id"]) assert.ok(!f.logs.join("\n").includes(secret), secret); + } finally { await f.close(); } + }); +} diff --git a/tests/runtime-info.test.ts b/tests/runtime-info.test.ts index ea4ab336e4..5b9a977ebd 100644 --- a/tests/runtime-info.test.ts +++ b/tests/runtime-info.test.ts @@ -546,3 +546,42 @@ describe("runtime-info pre-first-request status (#955)", () => { assert.equal(json.conversationId, "conv-pre-1"); }); }); + +// Client-minted child conversations: a runtime-info lookup for a child id must +// resolve the nearest reporting ancestor (see pluginRuntimeInfoForConversation). +describe("runtime-info: child conversations inherit the parent's report", () => { + beforeEach(() => _resetPluginStateForTest()); + + it("a `:side:` ephemeral turn resolves the parent's window and output cap", () => { + recordPluginRuntimeInfo({ agent: "omp", model: "astra", contextWindow: 1_000_000, maxOutput: 128_000, conversationId: "sid-parent", source: "client-config", ts: 1 }); + const hit = pluginRuntimeInfoForConversation("sid-parent:side:15b3f37086a59857", "astra"); + assert.equal(hit?.contextWindow, 1_000_000); + assert.equal(hit?.maxOutput, 128_000); + }); + + it("`:side:conversation:` and `:sub:` ids resolve too, including nested ones", () => { + recordPluginRuntimeInfo({ agent: "omp", model: "astra", contextWindow: 262_144, conversationId: "sid-parent", source: "client-config", ts: 1 }); + assert.equal(pluginRuntimeInfoForConversation("sid-parent:side:conversation:0f3d", "astra")?.contextWindow, 262_144); + assert.equal(pluginRuntimeInfoForConversation("sid-parent:sub:agent-7", "astra")?.contextWindow, 262_144); + assert.equal(pluginRuntimeInfoForConversation("sid-parent:side:abc:sub:agent-7", "astra")?.contextWindow, 262_144); + }); + + it("the model gate still holds: a child on a different model inherits nothing", () => { + recordPluginRuntimeInfo({ agent: "omp", model: "astra", contextWindow: 262_144, conversationId: "sid-parent", source: "client-config", ts: 1 }); + assert.equal(pluginRuntimeInfoForConversation("sid-parent:side:abc", "gemini"), undefined); + }); + + it("a child that reported its own window keeps it — inheritance never overrides a direct hit", () => { + recordPluginRuntimeInfo({ agent: "omp", model: "astra", contextWindow: 262_144, conversationId: "sid-parent", source: "client-config", ts: 1 }); + recordPluginRuntimeInfo({ agent: "omp", model: "astra", contextWindow: 65_536, conversationId: "sid-parent:side:abc", source: "client-config", ts: 2 }); + assert.equal(pluginRuntimeInfoForConversation("sid-parent:side:abc", "astra")?.contextWindow, 65_536); + }); + + it("no prefix matching: an unrelated id or a marker with no tail stays a miss", () => { + recordPluginRuntimeInfo({ agent: "omp", model: "astra", contextWindow: 262_144, conversationId: "sid-parent", source: "client-config", ts: 1 }); + assert.equal(pluginRuntimeInfoForConversation("sid-parent2:side:abc", "astra"), undefined); + assert.equal(pluginRuntimeInfoForConversation("sid-parentx", "astra"), undefined); + assert.equal(pluginRuntimeInfoForConversation("sid-parent:side:", "astra"), undefined); + assert.equal(pluginRuntimeInfoForConversation("sid-parent:sideways:abc", "astra"), undefined); + }); +}); diff --git a/tests/side-lane-invariants.test.ts b/tests/side-lane-invariants.test.ts index 21485d764a..2f9501f7c2 100644 --- a/tests/side-lane-invariants.test.ts +++ b/tests/side-lane-invariants.test.ts @@ -350,11 +350,16 @@ test("(F) extracted protocol modules keep the side-lane boundary (#1440 P2 cut 2 // dsh sidecar shape predicate (no tools + one-shot body, any system text) // consulted to set sideIntent and to keep unknown sidecars off the raw-key // anchor before the first main turn can claim it. +// #4774 raw-key anchor race. #5073 adds +// isStructuredOutputCall — same category once more: a pure toolless +// structured-output shape predicate (no threshold, no session state) consulted +// to set sideIntent and to keep the call out of the restoreOutputBudget +// high-water path, exactly like the #2500 server-tool predicate. test("(G) extracted pipeline module keeps the side-lane boundary (#1440 P2 cut 3)", () => { const text = readFileSync(fileURLToPath(new URL("../src/server/handle.ts", import.meta.url)), "utf8"); const laneInternals = /\bSIDE_REQUEST_MAX_TOKENS\b|\bSIDE_REQUEST_AGENTS\b|\bDemoteGateSignals\b|\bSideLaneSignals\b|\bSideLaneDecision\b|\bBILI_TOOL_NAMES\b|\bOutputBudgetField\b|\boutputBudgetField\b|\breadOutputBudget\b|\bwriteOutputBudget\b|\b_resetNoOutputCeilingWarningsForTest\b/; assert.doesNotMatch(text, laneInternals, "handle.ts must stay decoupled from the side-lane engine internals"); - const allowlist = new Set(["hasLeakedBiliToolsOnly", "isClaudeCodeTitleRequest", "isDshAutoReviewRequest", "isDshSingleShotSidecar", "isDshTitleRequest", "isServerToolUtilityCall", "isSideRequest", "resolveSideLane", "demoteGate", "stripLeakedBiliTools", "restoreOutputBudget", "sideRequestGuard"]); + const allowlist = new Set(["hasLeakedBiliToolsOnly", "hasSingleShotBudget", "isClaudeCodeTitleRequest", "isDshAutoReviewRequest", "isDshSingleShotSidecar", "isDshTitleRequest", "isServerToolUtilityCall", "isSideRequest", "isStructuredOutputCall", "resolveSideLane", "demoteGate", "stripLeakedBiliTools", "restoreOutputBudget", "sideRequestGuard"]); let sawImport = false; for (const m of text.matchAll(/\{([^}]*)\}\s*from\s*["'][^"']*side-request\.js["']/g)) { sawImport = true; @@ -618,3 +623,62 @@ test("(M) #5135 claude-code title sidecar: no anchor claim in either arrival ord await h.close(); } }); + +test("(N) #6104 dsh 0.2.2 review shape: banner-shifted policy + replayed user body ride the side passthrough", async () => { + // #2170 duty: a lane-gate edit lands WITH the suite that pins it. The + // detector's two bars were host-side FORMAT decisions (policy line offset, + // number of user blobs), so both shapes are pinned here end-to-end, plus + // the vetoes that must never move. + const h = await harness(); + try { + const conv = "rev6104"; + const POLICY = "REVIEW_POLICY\nYou are the final authorization reviewer for exactly one pending tool call in this conversation.\n\nReply with exactly one JSON verdict."; + const BLOBS = [ + { role: "user", content: "ENVIRONMENT\nprofile: desktop; model: claude-test" }, + { role: "user", content: "PROJECT_INSTRUCTIONS\n(none)" }, + { role: "user", content: "FILTERED_HISTORY\nuser: main turn 0\nassistant: answer" }, + { role: "user", content: "PENDING_ACTION\n{\"tool\":\"bash\",\"args\":{\"command\":\"ls\"}}" }, + ]; + // The main turn anchors the raw key. + await h.dshSend(conv, [mainMsg(0)], "main", "MAIN OPERATING SYSTEM", 256); + assert.equal(resolveConversation(conv).session!.stats.requests, 1, "main turn anchors the raw key"); + const hits = h.forwarded.length; + + // The 0.2.2 review shape: host-sized budget (past the <=200 heuristic), + // no tools, no agent header, banner ABOVE the pinned sentence, and the + // whole history replayed as role:"user" blobs. + await h.dshSend(conv, BLOBS, undefined, POLICY, 384000); + assert.equal(h.forwarded.length, hits + 1, "the review call reaches the upstream"); + assert.equal(resolveConversation(conv).session!.stats.requests, 1, "the review call counts nowhere (side passthrough, #388)"); + assert.equal(listSessions().filter((s) => s.id.startsWith(conv + "|")).length, 0, "the review call mints no |sub lane"); + + // The next main turn still resolves to the SAME session and keeps + // compressing — the ledger-damage symptom was that it did not. + await h.dshSend(conv, [mainMsg(0), { role: "assistant", content: "answer" }, mainMsg(1)], "main", "MAIN OPERATING SYSTEM", 256); + assert.equal(resolveConversation(conv).session!.stats.requests, 2, "the next main turn lands on the raw key, not a drifted lane"); + assert.equal(listSessions().filter((s) => s.id.startsWith(conv + "|")).length, 0, "no drift on the main leg"); + + // Veto A — any tools surface marks an agent turn: full pipeline, counted. + // Counted ACROSS the raw key and any design persona child: a tool-carrying + // request with a foreign system prompt forks by design (#1916), and the + // point here is that it was NOT handed to the verbatim side lane. + const countAll = (id: string) => listSessions() + .filter((s) => s.id === id || s.id.startsWith(id + "|")) + .reduce((n, s) => n + (s.stats?.requests ?? 0), 0); + const convTools = "rev6104-tools"; + await h.dshSend(convTools, [mainMsg(0)], "main", "MAIN OPERATING SYSTEM", 256); + const hitsT = h.forwarded.length; + await h.dshSend(convTools, BLOBS, undefined, POLICY, 384000, [{ name: "bash", input_schema: { type: "object" } }]); + assert.equal(h.forwarded.length, hitsT + 1); + assert.equal(countAll(convTools), 2, "tools-shape request rides the full pipeline (unchanged veto)"); + + // Veto B — an assistant turn inside the body is a real resend, not the + // review (the load-bearing replay veto now that the user count is out). + const convReplay = "rev6104-replay"; + await h.dshSend(convReplay, [mainMsg(0)], "main", "MAIN OPERATING SYSTEM", 256); + await h.dshSend(convReplay, [...BLOBS.slice(0, 2), { role: "assistant", content: "earlier answer" }, ...BLOBS.slice(2)], undefined, POLICY, 384000); + assert.equal(countAll(convReplay), 2, "an assistant turn keeps the request on the main pipeline"); + } finally { + await h.close(); + } +}); diff --git a/tests/side-request-isolation.test.ts b/tests/side-request-isolation.test.ts index d662b32acc..e14cdc10bd 100644 --- a/tests/side-request-isolation.test.ts +++ b/tests/side-request-isolation.test.ts @@ -3,7 +3,8 @@ import assert from "node:assert/strict"; import http from "node:http"; import { once } from "node:events"; import { defaultConfig, createInitialState, defaultCountTokens } from "acp-kernel"; -import { startServer, isSideRequest, outputBudgetField, restoreOutputBudget, sideRequestGuard, resolveKnownOutputCeiling, _resetNoOutputCeilingWarningsForTest, _resetDshMidBandWarningsForTest, OUTPUT_RESTORE_ABSOLUTE_FLOOR } from "../src/server.ts"; +import { startServer, isSideRequest, isStructuredOutputCall, outputBudgetField, restoreOutputBudget, sideRequestGuard, resolveKnownOutputCeiling, _resetNoOutputCeilingWarningsForTest, _resetDshMidBandWarningsForTest, OUTPUT_RESTORE_ABSOLUTE_FLOOR } from "../src/server.ts"; +import { resolveSideLane, SIDE_REQUEST_MAX_TOKENS } from "../src/server/side-request.ts"; import type { ProxyOptions } from "../src/config.ts"; import { recordPluginRuntimeInfo, _resetPluginStateForTest } from "../src/plugin.ts"; import { estimateRawBodyTokens, estimateRawBodyBreakdown } from "../src/preflight.ts"; @@ -41,6 +42,45 @@ test("isSideRequest: tiny output budget across protocol field names", () => { assert.equal(isSideRequest({ max_output_tokens: 16, tools: [{ type: "function", function: { name: "f" } }] }), false, "#546: responses wire, starved budget + tools → main"); }); +test("isSideRequest: a toolless structured-output request is a side request", () => { + // A memory-extraction daemon (Hindsight) inherits the host's HTTPS_PROXY and + // sends JSON-schema extraction calls with a large budget. Injected wire tools + // turn those into tools + response_format, which strict backends reject. + assert.equal(isSideRequest({ max_tokens: 64000, response_format: { type: "json_object" } }), true, "chat json_object"); + assert.equal(isSideRequest({ max_tokens: 64000, response_format: { type: "json_schema", json_schema: { name: "r", schema: {} } } }), true, "chat json_schema"); + assert.equal(isSideRequest({ max_output_tokens: 64000, text: { format: { type: "json_schema", name: "r", schema: {} } } }), true, "responses text.format json_schema"); + assert.equal(isSideRequest({ max_tokens: 64000, response_format: { type: "text" } }), false, "plain text format is not structured"); + assert.equal(isSideRequest({ max_output_tokens: 64000, text: { format: { type: "text" } } }), false, "responses plain text format"); + assert.equal(isSideRequest({ max_tokens: 64000, response_format: null }), false, "null response_format"); + assert.equal(isSideRequest({ max_tokens: 64000, response_format: "json_object" }), false, "non-object response_format"); + assert.equal(isSideRequest({ max_tokens: 64000, response_format: { type: "json_object" }, tools: [{ type: "function", function: { name: "f" } }] }), false, "#546 still wins: a tool-carrying structured request is a main turn"); + assert.equal(isSideRequest({ max_tokens: 64000, response_format: { type: "json_object" } }, "main"), false, "explicit main persona outranks the shape"); +}); + +test("isStructuredOutputCall: the predicate is self-contained for the restoreOutputBudget skip (#2500 parity)", () => { + // handle.ts consults this predicate independently of the #546 guard, so the + // no-tools veto lives IN the predicate — a tool-carrying structured request + // must never be demoted into the high-water skip by a standalone caller. + assert.equal(isStructuredOutputCall({ max_tokens: 64000, response_format: { type: "json_object" } }), true, "toolless chat json_object"); + assert.equal(isStructuredOutputCall({ max_output_tokens: 64000, text: { format: { type: "json_schema" } } }), true, "toolless responses text.format"); + assert.equal(isStructuredOutputCall({ max_tokens: 64000, response_format: { type: "json_object" }, tools: [{}] }), false, "any tool entry vetoes (#546)"); + assert.equal(isStructuredOutputCall({ max_tokens: 64000, response_format: { type: "text" } }), false, "plain text format does not count"); + assert.equal(isStructuredOutputCall(null), false, "null body"); + assert.equal(isStructuredOutputCall("nope"), false, "non-object body"); +}); + +test("resolveSideLane: the #5073 label names the structured-output shape in the log line", () => { + assert.equal( + resolveSideLane({ countTokens: false, responsesCompact: false, protocol: "openai", stripApplied: false, sideIntent: true, requestAgent: undefined, sideLabel: "structured-output utility call (#5073)" }).reason, + "structured-output utility call (#5073)", + ); + // Without a label the historical reason strings stay byte-stable. + assert.equal( + resolveSideLane({ countTokens: false, responsesCompact: false, protocol: "openai", stripApplied: false, sideIntent: true, requestAgent: undefined }).reason, + `max_tokens<=${SIDE_REQUEST_MAX_TOKENS}`, + ); +}); + test("isSideRequest: host-declared side-request agent outranks the token-budget heuristic (#1699)", () => { // opencode v2 title-gen carries NO max_tokens (options {} for kind==="title"), // so the budget path can never see it. Declaring the persona by intent fixes @@ -791,6 +831,58 @@ test("e2e: opencode v2 title-gen is classified by intent, not token budget (#169 } }); +test("e2e: a toolless structured-output request rides the #388 passthrough — no injection, no high-water poison (#5073)", async () => { + const rig = await startRig(); + try { + const url = `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/chat/completions`; + const headers = { "content-type": "application/json", "x-acp-session": SESSION }; + const messages = [ + { role: "system", content: "Extract facts from the conversation." }, + { role: "user", content: "User: I moved to Lisbon last spring." }, + ]; + // Main turn: the same toolless shape WITHOUT response_format is a real + // turn — it gets the ACP tools injected and learns a 1024 high-water. + // That is the value the side call's 64000 budget must NOT overwrite + // (the #1897/#2500 poisoning class). + const rControl = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, stream: true, max_tokens: 1024, messages }) }); + assert.equal(rControl.status, 200); + await rControl.text(); + const controlTools = (rig.lastBody as { tools?: unknown[] } | null)?.tools; + assert.ok(Array.isArray(controlTools) && controlTools.length > 0, "control: a toolless main turn gets the ACP tools injected"); + const s1 = getSession(SESSION); + assert.ok(s1, "session exists after the main request"); + assert.equal(s1.metadata.outputBudgetHighWater, 1024, "high-water learned from the healthy main turn"); + const stateAfterMain = JSON.stringify(s1.state); + const statsAfterMain = JSON.stringify(s1.stats); + const snapshotAfterMain = JSON.stringify(s1.lastMessages); + const requestsAfterMain = s1.stats.requests; + + // The Hindsight fact-extraction shape: no tools, response_format, a 64000 + // budget that would seed the high-water were restoreOutputBudget not skipped. + const body = { model: MODEL, stream: true, max_tokens: 64000, messages, response_format: { type: "json_object" } }; + const rFix = await fetch(url, { method: "POST", headers, body: JSON.stringify(body) }); + assert.equal(rFix.status, 200); + await rFix.text(); + const fwd = rig.lastBody as { tools?: unknown; messages?: unknown; response_format?: unknown; max_tokens?: unknown } | null; + assert.ok(fwd, "upstream received the request"); + assert.equal(fwd?.tools, undefined, "no tools injected beside response_format"); + assert.deepEqual(fwd?.messages, messages, "messages forwarded verbatim (no prompt/render tags injected)"); + assert.deepEqual(fwd?.response_format, body.response_format, "response_format untouched"); + assert.equal(fwd?.max_tokens, 64000, "utility-call budget must NOT be mutated by restoreOutputBudget"); + + // Kernel state, stats, snapshot, counter and high-water all untouched (#388). + const s2 = getSession(SESSION); + assert.ok(s2, "session still exists after the side request"); + assert.equal(JSON.stringify(s2.state), stateAfterMain, "side request must NOT mutate kernel state"); + assert.equal(JSON.stringify(s2.stats), statsAfterMain, "side request must NOT mutate stats"); + assert.equal(JSON.stringify(s2.lastMessages), snapshotAfterMain, "side request must NOT clobber the message snapshot"); + assert.equal(s2.stats.requests, requestsAfterMain, "side request must NOT increment the main request counter"); + assert.equal(s2.metadata.outputBudgetHighWater, 1024, "the 64000 utility budget must NOT seed the high-water (#1897/#2500 class)"); + } finally { + await closeRig(rig); + } +}); + test("e2e: side request response still gets render-tag stripping (#460 contract)", async () => { const LT = "\x3c"; const GT = "\x3e"; diff --git a/tests/side-window-inherit.test.ts b/tests/side-window-inherit.test.ts new file mode 100644 index 0000000000..bdeaa59bb9 --- /dev/null +++ b/tests/side-window-inherit.test.ts @@ -0,0 +1,178 @@ +// Clients mint child conversation ids for work that runs on the parent's own +// route+model: omp opens idle-recap ephemeral turns as +// `<sessionId>:side:<snowflake>` and subagents as `<sessionId>:sub:<id>`, and +// registers runtime-info only under the parent uuid. The window ladder looked +// the id up literally, so the child missed the parent's report and fell to the +// registry peek — where a tier-gated family is capped at the built-in standard +// 200K without tier evidence (#1321). A 1M-window host therefore sized its own +// recap turns at 200K, and preflight rewrote/aborted a request the client had +// every right to send. +// +// Fix: pluginRuntimeInfoForConversation strips a client-minted child suffix and +// resolves the nearest reporting ancestor, keeping the model-equality gate at +// whichever level answers. + +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { listSessions } from "../src/session.ts"; +import { _resetPluginStateForTest, recordPluginRuntimeInfo } from "../src/plugin.ts"; + +const MODEL = "claude-opus-5-5"; // 200K standard; models.dev advertises 1M +const ONE_M = 1_000_000; +const PARENT = "01a16993-6110-7152-a930-ed7e8b180820"; + +function okSse(): string { + return ( + `event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "m1", role: "assistant", usage: { input_tokens: 1000 } } })}\n\n` + + `event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n` + + `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } })}\n\n` + + `event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n` + + `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 3 } })}\n\n` + + `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n` + ); +} + +interface Rig { + proxyPort: number; + upstreamPort: number; + proxy: http.Server; + upstream: http.Server; +} + +async function startRig(): Promise<Rig> { + const upstream = http.createServer((req, res) => { + req.resume(); + req.on("end", () => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(okSse()); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + _setStoreForTest(new SessionStore({ enabled: false })); + _resetPluginStateForTest(); + // models.dev advertises the MAX tier — the same shape that gets capped at + // the built-in standard window when no tier evidence rides the request. + setRegistryForTest({ [`anthropic/${MODEL}`]: { limit: { context: ONE_M } } }); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: {} }, + modelContextLimit: 200_000, + kernelConfig: defaultConfig(200_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + autoRestartOnUpdate: false, + advisoryCheck: false, + releaseNotesCheck: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + updateTag: "latest", + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + return { proxyPort, upstreamPort, proxy, upstream }; +} + +function url(rig: Rig): string { + return `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/messages`; +} + +async function closeRig(rig: Rig): Promise<void> { + rig.proxy.close(); + rig.proxy.closeIdleConnections?.(); + await once(rig.proxy, "close"); + rig.upstream.close(); + rig.upstream.closeIdleConnections?.(); + await once(rig.upstream, "close"); +} + +/** Sends one request under `sessionId` and returns the effective window the + * proxy budgeted for that session. */ +async function effectiveWindow(rig: Rig, sessionId: string, model = MODEL): Promise<number | undefined> { + const r = await fetch(url(rig), { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": sessionId }, + body: JSON.stringify({ model, max_tokens: 1024, stream: true, messages: [{ role: "user", content: "hi" }] }), + }); + assert.equal(r.status, 200); + await r.text(); + const limit = listSessions()[0]?.metadata.effectiveContextLimit; + return typeof limit === "number" ? limit : undefined; +} + +function reportParent(conversationId = PARENT, model = MODEL): void { + recordPluginRuntimeInfo({ agent: "omp", model, contextWindow: ONE_M, maxOutput: 128_000, conversationId, source: "client-config", ts: 1 }); +} + +test("e2e: the parent's own request keeps the reported 1M window", async () => { + const rig = await startRig(); + try { + reportParent(); + assert.equal(await effectiveWindow(rig, PARENT), ONE_M); + } finally { + await closeRig(rig); + } +}); + +test("e2e: a `:side:` ephemeral turn inherits the parent's 1M window, not the 200K cap", async () => { + const rig = await startRig(); + try { + reportParent(); + assert.equal(await effectiveWindow(rig, `${PARENT}:side:15b3f37086a59857`), ONE_M, "pre-fix: 200_000 via the registry/#1321 cap"); + } finally { + await closeRig(rig); + } +}); + +test("e2e: `:side:conversation:` and `:sub:` children inherit too", async () => { + const rig = await startRig(); + try { + reportParent(); + assert.equal(await effectiveWindow(rig, `${PARENT}:side:conversation:0f3d`), ONE_M); + assert.equal(await effectiveWindow(rig, `${PARENT}:sub:agent-7`), ONE_M); + } finally { + await closeRig(rig); + } +}); + +test("e2e: the model gate holds at the parent — a child on another model is not sized by it", async () => { + const rig = await startRig(); + try { + reportParent(PARENT, "claude-sonnet-4-5"); + assert.equal(await effectiveWindow(rig, `${PARENT}:side:15b3f37086a59857`), 200_000, "stale window from a different model must not be inherited"); + } finally { + await closeRig(rig); + } +}); + +test("e2e: an unknown parent still falls back to the capped registry window", async () => { + const rig = await startRig(); + try { + reportParent(); + assert.equal(await effectiveWindow(rig, "no-such-parent:side:abc"), 200_000); + assert.equal(await effectiveWindow(rig, `${PARENT}2:side:abc`), 200_000, "no accidental prefix match"); + } finally { + await closeRig(rig); + } +}); diff --git a/tests/storage-archive-gc.test.ts b/tests/storage-archive-gc.test.ts index 4180076f30..d60fa6d7c3 100644 --- a/tests/storage-archive-gc.test.ts +++ b/tests/storage-archive-gc.test.ts @@ -5,6 +5,7 @@ import * as os from "node:os"; import * as path from "node:path"; import { planIngest, type LegacySessionLike } from "../src/storage/ingest.ts"; import { UnifiedStore } from "../src/storage/store.ts"; +import { openSqliteDatabase } from "../src/storage/driver.ts"; import { resolveArchiveAfterDays } from "../src/knobs.ts"; /** @@ -170,3 +171,43 @@ test("gc(): age sweep archives only past-retention sessions; dormant shared rows try { fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); } catch { /* Windows stray handles */ } } }); + +test("#6317: failed archive sweep leaves no archive line; restore uses the newest entry", () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "storage-archive-failed-")); + try { + const store = UnifiedStore.open(path.join(dir, "index.db")); + store.ingestSession(planIngest(mkSession("sess-A", [mkMsg("m-a", "old body ".repeat(50))]))); + const archiveDir = path.join(dir, "archive"); + const logPath = path.join(archiveDir, UnifiedStore.ARCHIVE_LOG_NAME); + fs.mkdirSync(archiveDir, { recursive: true }); + + const second = openSqliteDatabase(path.join(dir, "index.db")).db; + second.exec("BEGIN IMMEDIATE"); + try { + assert.throws( + () => store.archiveSession("sess-A", { archiveDir }), + /database is locked/, + "archive fails while another writer holds the lock", + ); + } finally { + second.exec("ROLLBACK"); + second.close(); + } + assert.ok(!fs.existsSync(logPath), "failed archive must not append a stale line"); + + store.ingestSession(planIngest(mkSession("sess-A", [mkMsg("m-a", "old body ".repeat(50)), mkMsg("m-a2", "new body ".repeat(50))]))); + const archived = store.archiveSession("sess-A", { archiveDir }); + assert.ok(archived.ok); + const lines = fs.readFileSync(logPath, "utf8").split("\n").filter((l) => l.trim()); + assert.equal(lines.length, 1, "only the successful archive writes a line"); + const dump = JSON.parse(lines[0]) as { messages?: { msg_id?: string }[] }; + assert.deepEqual(dump.messages?.map((m) => m.msg_id), ["m-a", "m-a2"]); + + const restored = store.restoreArchived("sess-A", { archiveDir }); + assert.ok(restored.ok); + assert.deepEqual(store.getMessages("sess-A").map((m) => m.msgId), ["m-a", "m-a2"], "restore returns the newest dump"); + store.close(); + } finally { + try { fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); } catch { /* Windows stray handles */ } + } +}); diff --git a/tests/storage-unified.test.ts b/tests/storage-unified.test.ts index 15d2d1a04c..8e4908ba87 100644 --- a/tests/storage-unified.test.ts +++ b/tests/storage-unified.test.ts @@ -960,3 +960,36 @@ test("#4313: fork clone copies ts verbatim (inherited ledger rows keep their sta fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); } }); + +test("#6325: unchanged large session columns are not rewritten on delta ingest", () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "storage-6325-")); + try { + const store = UnifiedStore.open(path.join(dir, "index.db")); + const now = Date.now(); + const makeSession = (id: string, text: string): LegacySessionLike => ({ + id, + createdAt: now, + lastSeen: now, + meta: { protocol: "openai" }, + stats: {}, + metadata: { cacheLedger: { keep: "x".repeat(4096) } }, + state: { blocks: [], nextBlockId: 1, nextRunId: 1, messageRefs: { byRaw: {}, byRef: {} } }, + blockContents: new Map(), + lastMessages: [{ id: "m1", role: "user", contentType: "text", text } as unknown as CoreMessage], + lastMessagesFolded: false, + pluginSnapshot: undefined, + contentStore: undefined, + } as unknown as LegacySessionLike); + const session = makeSession("sess-6325", "hello"); + const before = store.ingestSession(planIngest(session)); + assert.ok(before); + const first = store.getSessionRow("sess-6325"); + assert.ok(first?.metadataJson?.includes("cacheLedger")); + store.ingestSession(planIngest({ ...session, lastMessages: [{ id: "m2", role: "assistant", contentType: "text", text: "world" } as unknown as CoreMessage] })); + const second = store.getSessionRow("sess-6325"); + assert.equal(second?.metadataJson, first?.metadataJson, "unchanged metadata_json should reuse the stored value"); + store.close(); + } finally { + try { fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }); } catch { /* Windows stray handles */ } + } +}); diff --git a/tests/upstream-proxy-routing.test.ts b/tests/upstream-proxy-routing.test.ts index 46a0602bb7..441dc8f70d 100644 --- a/tests/upstream-proxy-routing.test.ts +++ b/tests/upstream-proxy-routing.test.ts @@ -442,7 +442,8 @@ test("upstream failures expand nested causes with redacted proxy context", () => "https://chatgpt.com/backend-api/codex/responses", "http://user:secret@proxy.example:8080", ); - for (const expected of ["code=UND_ERR_CONNECT_TIMEOUT", "errno=-4039", "syscall=connect", "address=203.0.113.7", "port=443", "message=fetch failed <- connect timed out", "url=https://chatgpt.com/backend-api/codex/responses", "proxy=http://***:***@proxy.example:8080/"]) { + for (const expected of ["code=UND_ERR_CONNECT_TIMEOUT", "errno=-4039", "syscall=connect", "address=<private-host>", "port=443", "message=fetch failed <- connect timed out", "url=https://chatgpt.com/backend-api/codex/responses", "proxy=http://***:***@proxy.example:8080/"]) { assert.ok(detail.includes(expected), expected); } + assert.ok(!detail.includes("203.0.113.7"), "nested addresses are masked before reaching client-facing diagnostics"); }); diff --git a/tests/upstream-timeout-diagnostics.test.ts b/tests/upstream-timeout-diagnostics.test.ts new file mode 100644 index 0000000000..550426c3da --- /dev/null +++ b/tests/upstream-timeout-diagnostics.test.ts @@ -0,0 +1,140 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { describeUpstreamFailure } from "../src/upstream-proxy.js"; +import { classifyUpstreamFailure, buildProxyFailureResponse, withUpstreamFailure } from "../src/upstream-fail.js"; +import { fetchWithRetry, fetchWithTransportRetry } from "../src/fetch-util.js"; +import { setMaskHostsEnabled } from "../src/log-mask.js"; + +function connectError(code: string, address: string, message = ""): Error { + return Object.assign(new Error(message), { code, errno: -110, syscall: "connect", address, port: 443 }); +} + +function familyFailure(): TypeError { + const aggregate = Object.assign(new AggregateError([ + connectError("ETIMEDOUT", "192.0.2.1"), + connectError("ENETUNREACH", "2001:db8::1"), + ], ""), { code: "ETIMEDOUT" }); + return new TypeError("fetch failed", { cause: aggregate }); +} + +test("ETIMEDOUT diagnostics do not claim that the idle watchdog expired", () => { + for (const error of [connectError("ETIMEDOUT", "192.0.2.1"), familyFailure()]) { + const parts = describeUpstreamFailure(error, "https://chatgpt.com/backend-api/codex/responses"); + assert.equal(parts.detail.kind, "upstream-timeout"); + assert.doesNotMatch(parts.detail.hint!, /idle budget expired/); + assert.match(parts.detail.hint!, /phase/); + assert.match(parts.detail.hint!, /not retried/); + } +}); + +test("aggregate diagnostics retain every address attempt with its own fields", () => { + setMaskHostsEnabled(false); + try { + const parts = describeUpstreamFailure(familyFailure(), "https://chatgpt.com/backend-api/codex/responses"); + assert.match(parts.message, /ENETUNREACH/); + assert.match(parts.message, /2001:db8::1/); + const causes = JSON.parse(parts.message.match(/causes=(\[.*\]) url=/)![1]!); + assert.ok(causes.some((entry: Record<string, unknown>) => entry.code === "ETIMEDOUT" && entry.syscall === "connect" && entry.address === "192.0.2.1" && entry.port === 443)); + assert.ok(causes.some((entry: Record<string, unknown>) => entry.code === "ENETUNREACH" && entry.address === "2001:db8::1")); + } finally { + setMaskHostsEnabled(true); + } +}); + +test("nested causes are masked before appearing in the client-facing 502 body", () => { + const secret = "sk-example-secret-for-diagnostics"; + const child = connectError("ETIMEDOUT", "192.0.2.1", `connect relay.internal 192.0.2.1 https://user:password@relay.internal/path?token=${secret} Bearer ${secret}`); + const error = new TypeError("fetch failed", { cause: new AggregateError([child], "") }); + const parts = describeUpstreamFailure(error, "https://relay.internal/v1/messages", "http://user:password@127.0.0.1:8080"); + const response = buildProxyFailureResponse(parts.message, withUpstreamFailure(new Error(parts.message), parts.detail)); + assert.equal(response.status, 502); + assert.match(response.body, /ETIMEDOUT/); + assert.match(response.body, /connect/); + for (const value of [secret, "relay.internal", "192.0.2.1", "user:password"]) assert.ok(!response.body.includes(value), value); +}); + +test("aggregate traversal handles cyclic causes and shared children once", () => { + const child = connectError("ENETUNREACH", "127.0.0.1", "unique-child"); + const aggregate = new AggregateError([child, child], "root"); + aggregate.cause = aggregate; + const parts = describeUpstreamFailure(aggregate, "https://chatgpt.com/backend-api/codex/responses"); + assert.match(parts.message, /ENETUNREACH/); + const causes = JSON.parse(parts.message.match(/causes=(\[.*\]) url=/)![1]!); + assert.equal(causes.filter((entry: Record<string, unknown>) => entry.message === "unique-child").length, 1); +}); + +test("aggregate diagnostic work and output are bounded for large trees", () => { + const errors = Array.from({ length: 200 }, (_, i) => connectError("ETIMEDOUT", "127.0.0.1", `attempt-${i}`)); + const parts = describeUpstreamFailure(new AggregateError(errors, "root"), "https://chatgpt.com/backend-api/codex/responses"); + assert.match(parts.message, /attempt-0/); + assert.doesNotMatch(parts.message, /attempt-199/); + assert.match(parts.message, /truncated/); + assert.ok(parts.message.length < 20_000); +}); + +test("deep causes and repeated aggregate children cannot bypass traversal limits", () => { + let deep: Error = new Error("deepest"); + for (let i = 0; i < 100; i++) deep = new Error(`level-${i}`, { cause: deep }); + assert.match(describeUpstreamFailure(deep, "https://chatgpt.com/responses").message, /truncated/); + const child = new Error("shared"); + const repeated = new AggregateError(Array.from({ length: 200 }, () => child), "root"); + assert.match(describeUpstreamFailure(repeated, "https://chatgpt.com/responses").message, /truncated/); +}); + +test("non-scalar error metadata cannot break diagnostic serialization", () => { + const circular: Record<string, unknown> = {}; + circular.self = circular; + const child = Object.assign(new Error("child"), { code: "ETIMEDOUT", address: circular, port: 443 }); + const parts = describeUpstreamFailure(new AggregateError([child], "root"), "https://chatgpt.com/responses"); + assert.match(parts.message, /ETIMEDOUT/); + assert.doesNotMatch(parts.message, /\\"self\\"/); +}); + +test("malformed aggregate containers do not replace the original failure", () => { + for (const errors of [null, 42, {}]) { + const aggregate = Object.assign(new AggregateError([], "original failure"), { errors, code: "ETIMEDOUT" }); + const parts = describeUpstreamFailure(aggregate, "https://chatgpt.com/responses"); + assert.match(parts.message, /original failure/); + assert.equal(parts.detail.code, "ETIMEDOUT"); + } +}); + +test("falsy aggregate children still consume the visit budget", () => { + let visited = 0; + const children = Array.from({ length: 200 }, () => null); + for (let i = 0; i < children.length; i++) { + Object.defineProperty(children, i, { get() { visited++; return null; } }); + } + const aggregate = new AggregateError([], "root"); + aggregate.errors = children; + const bounded = describeUpstreamFailure(aggregate, "https://chatgpt.com/responses"); + assert.match(bounded.message, /truncated/); + assert.ok(visited <= 32, String(visited)); +}); + +test("redacted causes remain valid JSON with quoted URLs and backslashes", () => { + const child = new Error('failed "https://api.openai.com/v1" in C:\\tmp\\file'); + const parts = describeUpstreamFailure(new AggregateError([child], "root"), "https://chatgpt.com/responses"); + const causes = JSON.parse(parts.message.match(/causes=(\[.*\]) url=/)![1]!); + assert.match(causes[0].message, /failed/); + assert.ok(causes[0].message.includes("C:\\tmp\\file")); +}); + +test("diagnostic improvements preserve conservative timeout classification and retry behavior", async () => { + for (const code of ["UND_ERR_HEADERS_TIMEOUT", "UND_ERR_BODY_TIMEOUT"]) { + assert.equal(classifyUpstreamFailure(Object.assign(new Error("timeout"), { code })), "upstream-timeout"); + } + const original = globalThis.fetch; + try { + for (const send of [fetchWithRetry, fetchWithTransportRetry]) { + for (const error of [connectError("ETIMEDOUT", "127.0.0.1"), familyFailure()]) { + let attempts = 0; + globalThis.fetch = async () => { attempts++; throw error; }; + await assert.rejects(send("https://chatgpt.com/backend-api/codex/responses", { method: "POST", body: "{}" }, 5000, undefined), (actual) => actual === error); + assert.equal(attempts, 1); + } + } + } finally { + globalThis.fetch = original; + } +}); diff --git a/tests/win-cmd-argv.test.ts b/tests/win-cmd-argv.test.ts index 3578c64932..f27e528d97 100644 --- a/tests/win-cmd-argv.test.ts +++ b/tests/win-cmd-argv.test.ts @@ -13,6 +13,7 @@ import path from "node:path"; import { spawnSync } from "node:child_process"; import { planClientSpawn, + quoteWinToken, resolveClientCommand, resolveCodexOfficialJsEntry, resolveOnPath, @@ -20,6 +21,7 @@ import { type SpawnChild, type SpawnFn, } from "../src/launcher.ts"; +import { splitWindowsCommandLine } from "../src/claude-native-bootstrap.ts"; import { planDshSpawn } from "../src/dsh-channel.ts"; import { winCmdUnsafeToken } from "../src/win-cmd.ts"; import { rmrf } from "./tmp-rm.ts"; @@ -61,6 +63,8 @@ test("winCmdUnsafeToken: the safe set passes", () => { String.raw`C:\x\y\z`, "tailbs-nospace\\", String.raw`even trailing \\ with space`, + "odd trailing with space \\", + "C:\\My Projects\\", "!VAR! delayed expansion marker", "tab\there", "unicode ✓ 日本語 🎉", @@ -80,7 +84,6 @@ test("winCmdUnsafeToken: every #2196 corruption class is refused", () => { ["a^b", "cmd metacharacters (& | < > ^ ( ))"], ["(a)", "cmd metacharacters (& | < > ^ ( ))"], ["line\nbreak", "a line break"], - ["odd trailing \\", "an odd number of trailing backslashes"], ]; for (const [token, reason] of unsafe) { assert.equal(winCmdUnsafeToken(token), reason, `expected unsafe: ${JSON.stringify(token)}`); @@ -97,6 +100,35 @@ test("planClientSpawn win32: safe argv keeps the exact #679 plan (byte-identical assert.equal(p.windowsVerbatimArguments, true); }); +test("planClientSpawn win32: trailing backslashes in a quoted argument survive the comspec path", () => { + // MSVCRT reads 2n backslashes before a quote as n: an odd run used to be refused and an + // even one arrived halved. Doubling the run inside the quotes carries both through verbatim. + const p = planClientSpawn("C:\\npm\\claude.cmd", ["-p", "list C:\\My Projects\\", "ends in \\\\"], WIN_ENV, "win32"); + assert.equal(p.args[3], '"C:\\npm\\claude.cmd -p "list C:\\My Projects\\\\" "ends in \\\\\\\\""'); +}); + +// The comspec leg's acceptance property, pinned in CI: a whitespace-bearing +// token quoted by quoteWinToken must parse back to itself under the receiving +// program's CommandLineToArgvW rule (splitWindowsCommandLine models it). The +// exact-line test above pins the bytes; this pins that those bytes decode back +// to the token — the lossless round-trip the trailing-backslash doubling buys. +test("comspec round-trip: quoteWinToken output parses back verbatim (MSVCRT 2n -> n)", () => { + const tokens = [ + "plain spaced", // no backslash + "C:\\My Docs\\a.txt", // interior backslashes only + "odd trailing \\", // 1 trailing (refused before this fix) + "even trailing \\\\", // 2 trailing (arrived halved before this fix) + "three trailing \\\\\\", // 3 trailing + ]; + for (const token of tokens) { + assert.deepEqual( + splitWindowsCommandLine(quoteWinToken(token)), + [token], + `round-trip lost: ${JSON.stringify(token)}`, + ); + } +}); + test("planClientSpawn win32: '!' and other non-metacharacters stay allowed on the comspec path", () => { const p = planClientSpawn("tool.cmd", ["!VAR!", "a;b,c=d"], WIN_ENV, "win32"); assert.equal(p.command, "C:\\Windows\\System32\\cmd.exe"); @@ -121,7 +153,6 @@ for (const [label, args] of [ ["empty argument", ["exec", ""]], ["%VAR%", ["exec", "literal %COMSPEC% marker"]], ["& metacharacter", ["exec", "hello&rem"]], - ["odd trailing backslash", ["exec", "odd tail \\"]], ] as const) { test(`planClientSpawn win32: refuses ${label} on the comspec path with an actionable error`, () => { assert.throws( @@ -178,6 +209,11 @@ test("planDshSpawn win32: safe profile args keep the #679 plan", () => { assert.equal(p.args[3], String.raw`"C:\tools\dsh.cmd plugin --profile "my profile" add billion-context"`); }); +test("planDshSpawn win32: a quoted token's trailing backslashes are doubled, not halved or refused", () => { + const p = planDshSpawn("C:\\tools\\dsh.cmd", ["plugin", "--profile", "C:\\My Profiles\\"], WIN_ENV, "win32"); + assert.equal(p.args[3], '"C:\\tools\\dsh.cmd plugin --profile "C:\\My Profiles\\\\""'); +}); + test("planDshSpawn win32: a metacharacter-laden profile name is refused, not mangled", () => { assert.throws( () => planDshSpawn(String.raw`C:\tools\dsh.cmd`, ["plugin", "--profile", "p&q", "add", "billion-context"], WIN_ENV, "win32"),