diff --git a/docs/clients.md b/docs/clients.md index a73702a880..540b46d34e 100644 --- a/docs/clients.md +++ b/docs/clients.md @@ -130,9 +130,13 @@ Two lanes, same plugin (#941): registers a cordis `internal/config` overlay (vendored from deshark42/dsh-compaction-basic-disable, MIT) that marks the preset's `compaction-basic` and `command-compact` rows `disabled: true` while - bili owns the lane, and the preset gate rows in the bundle patch make - each Web preset wait for that overlay's service before it can register. - Native + bili owns the lane. The four Web presets used to WAIT for that overlay's + service through an entry-level `inject`, which is fail-closed (no + optional dependency, no timeout, no fallback): a missing gate service + left them pending forever and bricked session creation while the host + still booted. Since #7111 they no longer wait — a preset mounted before + the gate entry activates is re-submitted once it does, so losing the gate + service costs the overlay, never the session. Native auto-compaction is OFF in these profiles too (and `/compact` is unavailable there — that is EXPECTED, not a bug (#4002): bili owns compression on the lane, so the preset's compact command and its @@ -141,10 +145,9 @@ Two lanes, same plugin (#941): ACP compression is unaffected. Since #4147 the overlay matches STRUCTURALLY — any entry whose resolved `config.plugins` nests `compaction-basic`/`command-compact` rows gets them disabled, custom - and profile-local presets exactly like the factory ones (the four - gate-row ids in `dsh.bundle.patch.yml` are ordering insurance for the - factory roster, not the coverage boundary — a profile shipping other - preset ids is covered by the structural walk). The one-time `[dsh-client]` + and profile-local presets exactly like the factory ones (a profile + shipping other preset ids is covered by the structural walk; there is no + id list to keep in sync since #7111). The one-time `[dsh-client]` boot note names the actual entries the overlay rewrote (#3962), so a patch-row miss on a renamed roster is visible without reading the host loader log. The overlay rides an @@ -171,8 +174,12 @@ Two lanes, same plugin (#941): separate always-on `bili-compaction-gate` entry: disabling or kill-switching (`BILLION_CONTEXT_PLUGIN=0`) `bili-native` hands the lane back to STOCK dsh behavior — presets boot normally and native - auto-compaction returns; compression is simply off. Do NOT disable - `bili-compaction-gate` itself — that re-creates the brick. To fully + auto-compaction returns; compression is simply off. Do NOT disable + `bili-compaction-gate` itself: since #7111 that no longer bricks session + creation (the presets are fail-open), but it leaves native compaction + ARMED while bili owns the lane — a dsh checkpoint landing outside bili's + knowledge destroys the compression substrate (the #1729 wire-level + refusal is only the backstop). To fully uninstall, remove `billion-context` from the profile's `dsh.profile.bundles` (and its dependency) and restart: the whole patch layer disappears and `compaction-basic` returns to stock diff --git a/docs/clients.zh-CN.md b/docs/clients.zh-CN.md index 9848dc3567..6b3ec80a45 100644 --- a/docs/clients.zh-CN.md +++ b/docs/clients.zh-CN.md @@ -16,8 +16,8 @@ - **桌面应用(Electron 宿主):** 同一插件也运行在 deepseek-harness **desktop** 应用内(经其应用内插件管理器安装)。此时 bootstrap 在应用进程内拉起代理:该进程的 `process.execPath` 是 Electron 二进制而非 Node,GUI 的 PATH 又不含常规安装目录 —— `resolveNodeRuntime`(#819/#1429)先探测众所周知的安装位置(`/opt/homebrew/bin`、`/usr/local/bin`、Volta 等),再回退到以纯 Node 方式运行应用自身二进制(`ELECTRON_RUN_AS_NODE=1`,强制注入子进程 env),于是无需 PATH 里有独立 Node 也能零配置压缩;设 `BILLION_CONTEXT_NODE` 可强制指定某个 Node(优先于两者)。#1429 之前该路径在 spawn 前抛错,所有会话静默降级为直连(不压缩),只能从 bili.log 里发现。 - **自动更新保持各 profile 同步:** 刷新有两个触发器 —— 全局自更新完成后,以及**profile 自己的代理**在周期检查里发现注册表有新版本时(全局 bili 从不运行也一样刷新,#1196 —— 插件市场安装的用户往往根本没有全局安装)。两种触发器都扫描 `~/.dsh/profiles/*/package.json`,把注册表钉住的 `billion-context` 依赖刷新到目标版本(全局触发器刷到新全局版本,自触发刷到注册表最新),统一经 dsh 自己的 `plugin add` 通道,绝不在位覆盖 —— 加载的插件与代理从此不再漂移(#953);钉在本地源的 profile 不动。刷新是尽力而为,失败下个周期重试,绝不会让代理或更新本身失败。 - **已报告:Profile 安装下部分传输零代理流量(#1158,调查中):** dsh `llm-pi-ai` 层的部分传输服务会话**从未有任何模型请求到达代理**(日志无 `processTurn`,调用 bili 工具返回 404 "no model request has arrived"),而同宿主的其他 provider 正常。根因仍在用运行时证据定性 —— 候选:传输层 fetch 形态(SDK 注入 fetch / 非全局 dispatcher)或宿主侧归属缺口导致流量未被 takeover gate 认领。检测:此类情况会打一次性 `[plugin] NO MODEL REQUESTS seen for conversation …` 告警,dsh 插件还会把归属 gate 放行的每个端点各记一条日志(每进程一次)。期间可靠规避:改用 `bili dsh` 启动 —— 启动器的 settings overlay 会把那些 provider 的 `baseURL` 重写为 `/bili/` URL,无论传输层使用哪种 fetch、归属状态如何,流量都必然过代理。 -- **Web profile 压缩(#1772,#3354 解决):** 当 profile 的 bundles 含 `@deepseek-ai/dsh-web-app` 时,真正运行的 `compaction-basic` 实例位于 agent preset(`preset-standard.config.plugins`)内部 —— dsh 的 patch 引擎只按 id 索引顶层条目与真 group(`group: true` 且 config 为数组)的子项,preset 内的嵌套行任何 patch 层都无法**按 id 直寻**。于是 bundle 里的 `auto: false` 落在 web-app 已禁用的宿主平面行上、什么都没改。#3354 起 bili 在**加载期**补上这一环:一个**常驻**的 bundle 条目 `bili-compaction-gate`(与 `bili-native` 并列的独立 cordis 条目,由同一组 patch 层挂载,因此 `bili-native` 被禁用或 kill-switch 时它仍然存活,#3662)注册一个 cordis `internal/config` overlay(移植自 deshark42/dsh-compaction-basic-disable,MIT),在 bili 持有该泳道期间把 preset 里的 `compaction-basic` 与 `command-compact` 行标记为 `disabled: true`;#4147 起该匹配是**结构化**的 —— 任何 entry 的 `config.plugins` 里嵌套了这两个包的行都会被禁用,自定义/本地 preset 与官方 preset 一视同仁(`dsh.bundle.patch.yml` 里的四个门控 id 只是对官方 roster 的顺序保险,不是覆盖边界);bundle patch 里的 preset 门控行让每个 Web preset 在注册前等待该 overlay 的服务。这些 profile 里原生自动压缩同样关闭(且 `/compact` 在那里不可用 —— 这是**预期行为而非 bug**(#4002):该泳道的压缩由 bili 接管,preset 的 compact 命令与自动压缩被有意一并撤下;按设计**没有**重新打开的开关(#3651/#3666),见 configuration.md);ACP 压缩不受影响。启动的一次性 `[dsh-client]` 日志会列出 overlay 实际改写过的 entry(#3962),patch 行落空(如 roster 改名)无需翻宿主 loader 日志即可见。overlay 依赖 dsh 的内部钩子、对版本敏感 —— 若在新/旧 dsh 构建上仍见到 dsh 压缩 checkpoint 落地,回退到 #2474 配方:profile 层 patch(你 profile 自己的 `cordis.patch.yml`,在所有 bundle 之后生效)用同 id + **完整快照** `config` 整体覆盖 preset-standard —— 从 `dsh --profile --dump-config` 抄下完整的 preset config,在其 `compaction-basic` 行下加 `config.auto: false`,把结果追加进 `cordis.patch.yml`。Config 替换是**整体语义**(片段会静默丢掉一切未重述的键 —— 不只是其他插件,连 preset 自己的 `id`/`order` 身份也会丢,可能使 roster 无法识别该 preset);`- insert:` 同 id 则是在末尾追加一条死行而非合并。dsh 升级 preset 后快照冻结 roster、需重抄一次。此类 profile 启动时插件打一条一次性 `[dsh-client]` 日志,说明两条路径。 -- **禁用与移除插件的区别(#3662):** #3662 之前,禁用 `bili-native`(dsh 插件管理器,或任何 patch 层里写 `disabled: true`)会把门控服务的**唯一**提供方一并移除,而 preset 行仍在等它 —— 四个 Web preset 永久 pending,新建/恢复会话全部报 `Unknown agent preset: standard`。现在该服务住在独立的常驻条目 `bili-compaction-gate` 里:禁用或 kill-switch(`BILLION_CONTEXT_PLUGIN=0`)`bili-native` 只是把泳道交还 dsh **原生行为** —— preset 正常注册、原生自动压缩恢复,bili 压缩则完全关闭。请勿禁用 `bili-compaction-gate` 本身 —— 那会重新造成「砖」。要彻底卸载:从 profile 的 `dsh.profile.bundles`(及其依赖)里移除 `billion-context` 后重启 —— 整个 patch 层消失,`compaction-basic` 回到原生默认 `auto: true`。若卡在 #3662 之前的版本且插件已被禁用:升级即自愈(patch 层按 bundles 里列出的包装载,与 `bili-native` 的启用状态无关);手动解砖是在 HOME 层 patch `$DSH_HOME/cordis.patch.yml`(`DSH_HOME` 未设时为 `~/.dsh/cordis.patch.yml`)追加四行 `- id: preset-standard` / `preset-ptc` / `preset-minimal` / `preset-cordis`,各配 `inject: null` —— 顶层键为整体替换,清掉 pending 锁。 +- **Web profile 压缩(#1772,#3354 解决):** 当 profile 的 bundles 含 `@deepseek-ai/dsh-web-app` 时,真正运行的 `compaction-basic` 实例位于 agent preset(`preset-standard.config.plugins`)内部 —— dsh 的 patch 引擎只按 id 索引顶层条目与真 group(`group: true` 且 config 为数组)的子项,preset 内的嵌套行任何 patch 层都无法**按 id 直寻**。于是 bundle 里的 `auto: false` 落在 web-app 已禁用的宿主平面行上、什么都没改。#3354 起 bili 在**加载期**补上这一环:一个**常驻**的 bundle 条目 `bili-compaction-gate`(与 `bili-native` 并列的独立 cordis 条目,由同一组 patch 层挂载,因此 `bili-native` 被禁用或 kill-switch 时它仍然存活,#3662)注册一个 cordis `internal/config` overlay(移植自 deshark42/dsh-compaction-basic-disable,MIT),在 bili 持有该泳道期间把 preset 里的 `compaction-basic` 与 `command-compact` 行标记为 `disabled: true`;#4147 起该匹配是**结构化**的 —— 任何 entry 的 `config.plugins` 里嵌套了这两个包的行都会被禁用,自定义/本地 preset 与官方 preset 一视同仁(#7111 起不再有需要同步的门控 id 列表);preset 不再等待该 overlay 的服务 —— 条目级 `inject` 是 fail-closed(无可选依赖、无超时、无降级),服务一旦缺席四个 preset 就永久 pending、会话无法创建而宿主照常启动;现在改由 gate 条目在激活后对已挂载的 preset 重新提交配置,丢失该服务只损失 overlay,不损失会话。这些 profile 里原生自动压缩同样关闭(且 `/compact` 在那里不可用 —— 这是**预期行为而非 bug**(#4002):该泳道的压缩由 bili 接管,preset 的 compact 命令与自动压缩被有意一并撤下;按设计**没有**重新打开的开关(#3651/#3666),见 configuration.md);ACP 压缩不受影响。启动的一次性 `[dsh-client]` 日志会列出 overlay 实际改写过的 entry(#3962),patch 行落空(如 roster 改名)无需翻宿主 loader 日志即可见。overlay 依赖 dsh 的内部钩子、对版本敏感 —— 若在新/旧 dsh 构建上仍见到 dsh 压缩 checkpoint 落地,回退到 #2474 配方:profile 层 patch(你 profile 自己的 `cordis.patch.yml`,在所有 bundle 之后生效)用同 id + **完整快照** `config` 整体覆盖 preset-standard —— 从 `dsh --profile --dump-config` 抄下完整的 preset config,在其 `compaction-basic` 行下加 `config.auto: false`,把结果追加进 `cordis.patch.yml`。Config 替换是**整体语义**(片段会静默丢掉一切未重述的键 —— 不只是其他插件,连 preset 自己的 `id`/`order` 身份也会丢,可能使 roster 无法识别该 preset);`- insert:` 同 id 则是在末尾追加一条死行而非合并。dsh 升级 preset 后快照冻结 roster、需重抄一次。此类 profile 启动时插件打一条一次性 `[dsh-client]` 日志,说明两条路径。 +- **禁用与移除插件的区别(#3662):** #3662 之前,禁用 `bili-native`(dsh 插件管理器,或任何 patch 层里写 `disabled: true`)会把门控服务的**唯一**提供方一并移除,而 preset 行仍在等它 —— 四个 Web preset 永久 pending,新建/恢复会话全部报 `Unknown agent preset: standard`。现在该服务住在独立的常驻条目 `bili-compaction-gate` 里:禁用或 kill-switch(`BILLION_CONTEXT_PLUGIN=0`)`bili-native` 只是把泳道交还 dsh **原生行为** —— preset 正常注册、原生自动压缩恢复,bili 压缩则完全关闭。请勿禁用 `bili-compaction-gate` 本身:#7111 起禁用它不再造成「砖」(preset 已 fail-open),但会让原生压缩在 bili 持有泳道期间保持**激活** —— dsh 的 checkpoint 落在 bili 不知情处会摧毁压缩基底(#1729 的 wire 级拒绝只是兜底)。要彻底卸载:从 profile 的 `dsh.profile.bundles`(及其依赖)里移除 `billion-context` 后重启 —— 整个 patch 层消失,`compaction-basic` 回到原生默认 `auto: true`。若卡在 #3662 之前的版本且插件已被禁用:升级即自愈(patch 层按 bundles 里列出的包装载,与 `bili-native` 的启用状态无关);手动解砖是在 HOME 层 patch `$DSH_HOME/cordis.patch.yml`(`DSH_HOME` 未设时为 `~/.dsh/cordis.patch.yml`)追加四行 `- id: preset-standard` / `preset-ptc` / `preset-minimal` / `preset-cordis`,各配 `inject: null` —— 顶层键为整体替换,清掉 pending 锁。 - **全局副本在加载期抢占 profile 副本(#2891):** dsh 解析 `dsh.profile.bundles` 里的裸包名 `billion-context` 时是 installation-anchor-first —— 先在自己的安装树(全局 npm root)里找,再找 profile 的 `node_modules`。因此只要机器上存在一份全局 `billion-context`,dsh 就加载**全局那份**,profile 里装的被静默忽略 —— 不崩溃、无日志。两者版本不一致时,实际运行的可能与 profile 声明的不同(例如 #1590 之前的全局没有 `dsh.client` web 面,设置页菜单凭空消失)。这一事实默认情况下处处不可见:`bili doctor` 现在会按 profile 逐一标记任何版本不一致的全局/本地对(#2891);用 `dsh --profile --dump-config`(读该 bundle 的 `name:` 行)可核对 dsh 实际加载了哪份。 `bili dsh` 启动下插件**附看**(attach)启动器的代理(不二次拉起)。裸上游 URL 与 spawn 模式一样重写为 `/bili/`(回环代理目标永不被代理 env 拦截,等于直接绕开 MITM);已经路由的 `/bili/` 前缀请求原样放行、只盖章。已知局限:手动 `/compact` 没有 dsh 侧事件钩子,其边界交给内核的自然 ingest diff(自动压缩现已处处关闭 —— 宿主平面经 bundle patch,web profile 的 preset 经 #3354 加载期 overlay;见上方 Web profile 说明)。 diff --git a/dsh.bundle.patch.yml b/dsh.bundle.patch.yml index d7b815f249..2cecbf72ce 100644 --- a/dsh.bundle.patch.yml +++ b/dsh.bundle.patch.yml @@ -5,19 +5,23 @@ # #1772/#2474/#3354: the `auto: false` below targets the HOST-PLANE # compaction-basic row. In profiles bundling @deepseek-ai/dsh-web-app that row # is disabled and the running instance lives inside the agent presets, whose -# nested rows no patch layer can address by id — the preset gate rows below -# make each Web preset wait for the load-time overlay the bili-compaction-gate -# entry registers (vendored from deshark42/dsh-compaction-basic-disable, MIT, -# #3354; see src/agent/dsh-compaction-disable.ts). A profile-level full-snapshot -# override of preset-standard (#2474) remains the fallback for dsh builds -# whose internal 'internal/config' hook moved. +# nested rows no patch layer can address by id. The bili-compaction-gate entry +# rewrites those rows through the load-time overlay (vendored from +# deshark42/dsh-compaction-basic-disable, MIT, #3354; see +# src/agent/dsh-compaction-disable.ts) and, since #7111, also reconciles the +# presets that were already mounted before it activated. A profile-level +# full-snapshot override of preset-standard (#2474) remains the fallback for +# dsh builds whose internal 'internal/config' hook moved. +# #7111: the four preset rows are deliberately NOT gated on the gate entry any +# more. An entry-level inject is fail-closed — cordis has no optional +# dependency, no timeout and no fallback — so any missing gate service left all +# four presets pending forever (agent-presets/not-found on session create) +# while the host still booted and served its UI. Losing the gate service now +# costs the overlay, not the session. # #3662: the gate lives in its OWN always-on entry, not in bili-native — -# disabling or kill-switching bili-native must leave the gate service alive so -# the presets fall back to STOCK dsh compaction instead of pending forever -# (the old wiring bricked session creation on exactly that path). Do NOT -# disable the bili-compaction-gate entry itself: it carries the service the -# presets wait on; to uninstall, remove billion-context from -# dsh.profile.bundles instead. +# disabling or kill-switching bili-native must leave the gate service and the +# overlay alive so the presets fall back to STOCK dsh compaction. To uninstall, +# remove billion-context from dsh.profile.bundles. - insert: - id: bili-native name: billion-context @@ -26,17 +30,6 @@ - id: compaction-basic config: auto: false -# #3354: keep in sync with writeDshAcpPatch in src/launcher.ts — entry inject -# lists are REPLACED by patches, not concatenated; if a future dsh ships its -# own inject list on these entries, merge manually in BOTH layers. -- id: preset-standard - inject: [biliBasicCompactionPolicy] -- id: preset-ptc - inject: [biliBasicCompactionPolicy] -- id: preset-minimal - inject: [biliBasicCompactionPolicy] -- id: preset-cordis - inject: [biliBasicCompactionPolicy] # #2605: DSH's default retry whitelist omits MALFORMED_RESPONSE, so one # sampling flake that breaks a tool-call argument JSON kills the whole turn # unretried. A patch replaces the targeted row's WHOLE config (no deep diff --git a/src/agent/dsh-compaction-disable.ts b/src/agent/dsh-compaction-disable.ts index 7f997f8b47..fbe31295f3 100644 --- a/src/agent/dsh-compaction-disable.ts +++ b/src/agent/dsh-compaction-disable.ts @@ -50,32 +50,15 @@ const DSH_COMPACTION_BASIC_PACKAGE = "@deepseek-ai/dsh-compaction-basic"; const DSH_COMMAND_COMPACT_PACKAGE = "@deepseek-ai/dsh-command-compact"; /** The agent-preset entry whose resolved config carries the plugin rows. */ const DSH_AGENT_PRESET_PACKAGE = "@deepseek-ai/dsh-agent-preset"; -/** Gate service name referenced by the preset `inject:` entries in - * dsh.bundle.patch.yml and the launcher overlay patch. A preset cannot - * register until this service is provided, so the middleware below is - * guaranteed registered before any protected preset resolves its config even - * when sibling imports finish out of order. #3662: the provider is the - * always-on gate entry (dsh-compaction-gate.ts), NOT bili-native — disabling - * or kill-switching bili-native must keep the latch satisfiable so the - * presets fall back to stock instead of pending forever; only disabling the - * gate entry itself leaves them pending (see the bundle patch header). +/** Gate service name the bundle patch and the launcher overlay used to gate + * the four Web presets on. #7111: that gate is gone — an entry-level inject is + * fail-closed (no optional dependency, no timeout, no fallback), so a missing + * service bricked session creation instead of degrading compression. The name + * is still published as a presence marker (see `dsh-compaction-gate.ts`), so + * older profiles keep activating and external diagnostics keep resolving it. * Coexistence with the upstream plugin is safe: its service name differs and * both overlays are idempotent (disabled:true twice = disabled:true). */ export const BILI_COMPACTION_DISABLE_SERVICE = "biliBasicCompactionPolicy"; -/** The four Web agent presets the overlay must reach (dsh-web-app roster; - * preset-minimal ships no compaction group today — the row is a no-op there - * and future-proofs it if one lands). */ -const DSH_WEB_PRESET_IDS = ["preset-standard", "preset-ptc", "preset-minimal", "preset-cordis"] as const; - -/** The bundle-patch rows gating each Web preset on the overlay service — the - * single source of truth shared by the launcher overlay string and pinned - * against dsh.bundle.patch.yml by tests (the two layers must stay in sync, - * cf. #2605). Entry inject lists are REPLACED by patches, not concatenated — - * if a future dsh ships its own inject list on these entries, merge manually - * here AND in both patch layers. */ -export function compactionDisablePatchRows(): string { - return DSH_WEB_PRESET_IDS.map((id) => `- id: ${id}\n inject: [${BILI_COMPACTION_DISABLE_SERVICE}]\n`).join(""); -} // #3662: process-level verdict between the two entries. They are SEPARATE // bundles in the dsh host (separate module instances — module state cannot @@ -291,6 +274,17 @@ export type CompactionDisableHost = { }; type MiddlewareSelf = { entry?: { options?: { name?: unknown } } | undefined }; +/** Entry option name carried by the fiber that owns a config resolution. + * The host dispatches `internal/config` as `waterfall('internal/config', + * fiber, next)` — the first argument is the event NAME, so cordis binds no + * `this` (vendor/cordis/src/events.ts:163 → thisArg null) and the listener + * receives `(fiber, next)`. Reading `this` therefore always yields + * undefined; the name lives on the first argument. */ +export function entryNameOf(fiber: unknown): string | undefined { + const name = (fiber as MiddlewareSelf | undefined)?.entry?.options?.name; + return typeof name === "string" && name.length > 0 ? name : undefined; +} + /** Register the load-time overlay + publish the gate. Runs from the always-on * gate entry's apply() (dsh-compaction-gate.ts, #3662) — NOT from bili-native, * whose disable/kill-switch must not take the service with it. The provide @@ -302,7 +296,7 @@ type MiddlewareSelf = { entry?: { options?: { name?: unknown } } | undefined }; export function registerCompactionDisable(ctx: CompactionDisableHost): void { if (typeof ctx.on === "function") { // Preset entries must carry the gate: sibling imports may finish out of order. - ctx.on("internal/config", function (this: MiddlewareSelf, _raw: unknown, next: () => unknown): unknown { + ctx.on("internal/config", function (fiber: unknown, next: () => unknown): unknown { const resolved = next(); // #3662: honor the process verdict — without an active declaration // from bili-native every row passes through stock. @@ -316,7 +310,7 @@ export function registerCompactionDisable(ctx: CompactionDisableHost): void { if (!Array.isArray(config.plugins)) return resolved; const compactionRows = countCompactionRows(config.plugins); if (compactionRows === 0) return resolved; - const rawName = this?.entry?.options?.name; + const rawName = entryNameOf(fiber); const entryName = typeof rawName === "string" ? rawName : "unnamed-entry"; noteGateCoverage(entryName.slice(0, 120), compactionRows, entryName === DSH_AGENT_PRESET_PACKAGE); return { ...config, plugins: disableBasicCompaction(config.plugins) }; @@ -346,3 +340,70 @@ export function detectCompactionOverlayGap(ctx: CompactionDisableHost): Compacti if (typeof ctx.reflect?.provide !== "function") return "no-service-provide"; return null; } +type HostPlaneEntry = { + options?: { id?: unknown; name?: unknown; disabled?: unknown; config?: unknown } | undefined; +}; + +/** Rows whose `auto:false` the bundle patch no longer owns in the COMPOSED + * tree: a dsh patch replaces a row's whole `config`, and `compaction-basic` + * defaults `auto` to true, so any later layer that restates this row (a hand + * written profile patch, a later bundle, a partial #2474 snapshot) silently + * re-arms native auto-compaction. Disabled rows are stock web-profile + * behaviour and are not reported. Returns the offending entry ids so the + * caller can alarm; empty = the declaration survived. */ +export function hostPlaneRowsMissingAutoFalse(entries: Iterable): string[] { + const lost: string[] = []; + for (const raw of entries) { + if (raw === null || typeof raw !== "object") continue; + const options = (raw as HostPlaneEntry).options; + if (options === undefined || options.name !== DSH_COMPACTION_BASIC_PACKAGE) continue; + if (options.disabled === true) continue; + const config = options.config; + const auto = config !== null && typeof config === "object" && !Array.isArray(config) + ? (config as { auto?: unknown }).auto + : undefined; + if (auto === false) continue; + lost.push(typeof options.id === "string" && options.id.length > 0 ? options.id : DSH_COMPACTION_BASIC_PACKAGE); + } + return lost; +} + +/** One-line alarm for the rows hostPlaneRowsMissingAutoFalse reported. */ +export function hostPlaneOverrideLostWarning(entryIds: readonly string[]): string { + const rows = entryIds.map((id) => JSON.stringify(id)).join(", "); + return `bili: the host-plane compaction-basic override is GONE (entry ${rows}): a later patch layer replaced that row's whole config, so "auto" fell back to its default true and dsh native auto-compaction is ARMED again. Restate "auto: false" in the LAST patch row that touches id compaction-basic, or remove that override (a landed checkpoint destroys the fold base).`; +} + +type ReconcileEntry = { + options?: { id?: unknown; name?: unknown; config?: unknown } | undefined; + update?: (options: { config: unknown }, create?: boolean, force?: boolean) => unknown; +}; + +/** #7111: fail-open replacement for the retired preset inject gate. The four + * Web presets are mounted before this entry activates, so their resolved + * config already carries live compaction rows; re-submitting that config + * restarts each entry and the `internal/config` middleware above rewrites the + * rows exactly as it does on a cold resolve. Entries that never nest a + * compaction row, and hosts whose entries expose no `update`, are left alone. + * Returns how many entries were re-submitted (0 = nothing to do, which is the + * only outcome a missing gate service can now produce). */ +export function reconcileLoadedPresets(entries: Iterable): number { + let touched = 0; + for (const raw of entries) { + if (raw === null || typeof raw !== "object") continue; + const entry = raw as ReconcileEntry; + const config = entry.options?.config; + if (config === null || typeof config !== "object" || Array.isArray(config)) continue; + const plugins = (config as { plugins?: unknown }).plugins; + if (!Array.isArray(plugins) || countCompactionRows(plugins) === 0) continue; + if (typeof entry.update !== "function") continue; + const next = { ...(config as Record), plugins: disableBasicCompaction(plugins) }; + try { + entry.update({ config: next }); + touched += 1; + } catch { + // A failing reconcile must never take the gate entry down with it. + } + } + return touched; +} diff --git a/src/agent/dsh-compaction-gate.ts b/src/agent/dsh-compaction-gate.ts index bf64452f19..83de97d1db 100644 --- a/src/agent/dsh-compaction-gate.ts +++ b/src/agent/dsh-compaction-gate.ts @@ -8,13 +8,43 @@ // service atomically (the #3354 ordering guarantee survives the split); // WHETHER the transform actually disables rows is decided per process by the // verdict bili-native declares — see dsh-compaction-disable.ts. -import { registerCompactionDisable, type CompactionDisableHost } from "./dsh-compaction-disable.js"; +import { + hostPlaneOverrideLostWarning, + hostPlaneRowsMissingAutoFalse, + reconcileLoadedPresets, + registerCompactionDisable, + type CompactionDisableHost, +} from "./dsh-compaction-disable.js"; +import { logHostEvent } from "./host-log.js"; -type PluginContext = CompactionDisableHost; +/** What the #7113 scan and the #7111 reconcile need from the host loader. + * Deliberately not a hard dependency: an entry-level inject is fail-closed + * (no optional dependency, no timeout, no fallback), and a required + * `loader` would restore exactly the failure these changes remove (#3662, + * #7111). */ +type LoaderFace = { loader?: { entries?: () => Iterable } }; + +type GateContext = CompactionDisableHost & { + inject?: (deps: string[], callback: (inner: LoaderFace) => void) => unknown; +}; export const name = "bili-compaction-gate"; export const inject: string[] = []; -export function apply(ctx: PluginContext): void { +/** Boot-time alarm text for a profile whose host-plane override is gone + * (null = the declaration survived, or the row is stock-disabled). */ +export function hostPlaneWarningFor(inner: LoaderFace): string | null { + const entries = inner.loader?.entries?.(); + if (entries === undefined) return null; + const lost = hostPlaneRowsMissingAutoFalse(entries); + return lost.length === 0 ? null : hostPlaneOverrideLostWarning(lost); +} + +export function apply(ctx: GateContext): void { registerCompactionDisable(ctx); + ctx.inject?.(["loader"], (inner) => { + reconcileLoadedPresets(inner.loader?.entries?.() ?? []); + const warning = hostPlaneWarningFor(inner); + if (warning !== null) logHostEvent("warn", "bili-compaction-gate", warning); + }); } diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index 8654a7b2b7..53d1fcad4e 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -524,12 +524,15 @@ function modelInfoRetryCooldownMs(): number { const parsed = raw === undefined ? Number.NaN : Number(raw); return Number.isFinite(parsed) && parsed >= 0 ? parsed : MODEL_INFO_RETRY_COOLDOWN_MS; } +// How long after a resolve starts a dispatch may wait on it before its first +// fetch; a slow or hung resolver must never stall the stream beyond this. +const MODEL_INFO_DISPATCH_WAIT_MS = 2_000; const modelInfo: { byKey: Map; - refreshingKeys: Set; + refreshingKeys: Map>; retryAtByKey: Map; services?: { llm?: PluginContext["llm"]; agentDefaultModel?: PluginContext["agentDefaultModel"] }; -} = { byKey: new Map(), refreshingKeys: new Set(), retryAtByKey: new Map() }; +} = { byKey: new Map(), refreshingKeys: new Map(), retryAtByKey: new Map() }; function modelInfoKey(provider: string, model: string): string { return `${provider}\u0000${model}`; @@ -554,8 +557,12 @@ function normalizeModelTarget(provider: unknown, model: unknown): { provider: st // #2381: the model THIS request routes to — the session's own live header // first (exact at fetch time, correct across per-session model switches), the // host's global default selection only as fallback. Any miss degrades to -// pre-#2381 behavior; it never yields a wrong model. +// pre-#2381 behavior; it never yields a wrong model. Inside an llm/stream +// dispatch the model that dispatch itself streams to wins (auxiliary calls +// may run on a different model than the session's). function sessionModelTargetOf(ctx: PluginContext): { provider: string; model: string } | undefined { + const scoped = dispatchScope.getStore()?.modelTarget; + if (scoped !== undefined) return scoped; try { const init = ctx.agents?.currentInitiator?.(); const header = init?.session?.requestHeader?.(); @@ -613,7 +620,10 @@ export function bodyOverWindowDetail( return sharedBodyOverWindowDetail(body?.bytes, rawBodyStringOf(init), declaredWindow); } -function refreshModelInfo(origin: string | undefined, target?: { provider: string; model: string }): void { +// Returns a wait handle for the target's in-flight resolve (settles when it +// lands, or MODEL_INFO_DISPATCH_WAIT_MS after it started), undefined when +// there is nothing to wait for. +function refreshModelInfo(origin: string | undefined, target?: { provider: string; model: string }): Promise | undefined { const svc = modelInfo.services; if (svc === undefined) return; const sel = target ?? globalModelTarget(svc); @@ -628,7 +638,7 @@ function refreshModelInfo(origin: string | undefined, target?: { provider: strin if (cached !== undefined && cached.contextWindow !== undefined) return; const retryAt = modelInfo.retryAtByKey.get(key); if (cached !== undefined && retryAt !== undefined && Date.now() < retryAt) return; - if (modelInfo.refreshingKeys.has(key)) return; + if (modelInfo.refreshingKeys.has(key)) return modelInfo.refreshingKeys.get(key); // The host registers llm as a service object, so resolveModelInfo needs its // receiver; called detached below, this threw // "Cannot read properties of undefined (reading 'resolveModelInfoFor')" @@ -640,9 +650,8 @@ function refreshModelInfo(origin: string | undefined, target?: { provider: strin evictModelInfoEntries(); return; } - modelInfo.refreshingKeys.add(key); modelInfo.retryAtByKey.set(key, Date.now() + modelInfoRetryCooldownMs()); - void Promise.resolve() + const flight = Promise.resolve() .then(() => resolve(provider, model)) .then((info) => { // Per-key commit (#2381): the entry belongs to its OWN key, so a @@ -682,6 +691,9 @@ function refreshModelInfo(origin: string | undefined, target?: { provider: strin }).catch(() => {}); } }); + const waitable = Promise.race([flight, new Promise((done) => { setTimeout(done, MODEL_INFO_DISPATCH_WAIT_MS).unref?.(); })]); + modelInfo.refreshingKeys.set(key, waitable); + return waitable; } function errMessage(err: unknown): string { @@ -1247,6 +1259,7 @@ interface LlmDispatchScope { sid: string; msgIds: string[] | undefined; purpose?: string; + modelTarget?: { provider: string; model: string }; } const dispatchScope = new AsyncLocalStorage(); @@ -2094,7 +2107,7 @@ export async function apply(ctx: PluginContext): Promise { if (typeof ctx.inject === "function" && typeof ctx.on === "function") { const onEvent = ctx.on.bind(ctx); ctx.inject(["llm"], () => { - onEvent("llm/stream", (options: { sessionId?: unknown; messages?: unknown; purpose?: unknown }, next: () => AsyncIterable) => { + onEvent("llm/stream", (options: { sessionId?: unknown; messages?: unknown; purpose?: unknown; provider?: unknown; model?: unknown }, next: () => AsyncIterable) => { const sid = typeof options?.sessionId === "string" && options.sessionId.length > 0 ? options.sessionId : undefined; if (sid === undefined) return next(); // #4491: capture the host message ids riding the same options @@ -2104,7 +2117,12 @@ export async function apply(ctx: PluginContext): Promise { // (dsh sets it on every auxiliary call; conversation leaves it // unset). Absent/odd values simply stay off the turn. const purpose = typeof options?.purpose === "string" && options.purpose.length > 0 && options.purpose.length <= 64 && /^[\x21-\x7e]+$/.test(options.purpose) ? options.purpose : undefined; - const scope: LlmDispatchScope = { sid, msgIds: hostMsgIdsOf(options?.messages), purpose }; + // Size the dispatch against the model IT streams to (auxiliary + // calls may use another model than the session header names). + // GenerateOptions declares provider + model at the top level. + const modelTarget = normalizeModelTarget(options?.provider, options?.model) + ?? sessionModelTargetOf(ctx) ?? globalModelTarget(modelInfo.services); + const scope: LlmDispatchScope = { sid, msgIds: hostMsgIdsOf(options?.messages), purpose, modelTarget }; // #5403/#5418 v3: feed the per-slot id sequence into the // watermark channel (the fetch chokepoint emits count + // tail-ids; core = pf-channel.ts) — "?" slots ride id-less @@ -2118,7 +2136,14 @@ export async function apply(ctx: PluginContext): Promise { // latter; each also clears inherited fetch-chain re-entry // state so a new operation never looks like mid-descent // re-entry (#5614). - return runLlmDispatch(scope, () => wrapScopedIterable(next(), scope)); + // A model's first dispatch would otherwise fetch before its + // window resolved and go out without it; wait (bounded) first. + const pending = refreshModelInfo(register.base ?? state.origin, modelTarget); + const stream: () => AsyncIterable = pending === undefined ? next : async function* () { + await pending; + yield* next(); + }; + return runLlmDispatch(scope, () => wrapScopedIterable(stream(), scope)); }); }); } diff --git a/src/agent/fetch-chain/index.ts b/src/agent/fetch-chain/index.ts index b516b10e1b..6680c34d97 100644 --- a/src/agent/fetch-chain/index.ts +++ b/src/agent/fetch-chain/index.ts @@ -15,6 +15,56 @@ function isOwnChain(v: unknown): boolean { return typeof v === "function" && Object.prototype.hasOwnProperty.call(v, CHAIN_MARKER); } +/** #6829: pi-web-access's marker for "a proxy fetch is installed". 0.45.x set + * `__piWebAccessProxyFetch` unconditionally; from 0.46.0 it only propagates + * that flag when the wrapped fetch already has it, and marks its own wrapper + * with `__piWebAccessSharedProxyFetch` instead. Recognize BOTH names, or the + * #2435 coexistence branch is unreachable for 0.46+ installs. */ +function isPiWebAccessFetch(v: unknown): boolean { + return typeof v === "function" && (Reflect.get(v, "__piWebAccessProxyFetch") === true || Reflect.get(v, "__piWebAccessSharedProxyFetch") === true); +} + +/** #6829: pseudo-origin served in-process by the inline engine. Owned by + * src/inline-engine.ts (`INLINE_ORIGIN`); duplicated here because the chain + * module must stay free of the server graph (inline-engine pulls in + * server.js, which would cycle back through the agent lanes). If the origin + * ever changes, update both. */ +const INLINE_ORIGIN = "http://billion-context.inline"; + +const INLINE_HOSTNAME = "billion-context.inline"; + +/** Exact-origin classifier for the inline pseudo-origin. A prefix match + * would also match lookalikes (billion-context.inline.evil.com), an https + * URL, a non-default port, or a URL carrying credentials — none of which is + * the inline engine. The hostname comparison is case-insensitive (URL + * normalises it), the scheme must be plain http, and only the default + * port (:80, or no port) counts. */ +export function isInlineOriginUrl(url: string): boolean { + let parsed: URL; + try { + parsed = new URL(url); + } catch { + return false; + } + return ( + parsed.protocol === "http:" && + parsed.hostname === INLINE_HOSTNAME && + (parsed.port === "" || parsed.port === "80") && + parsed.username === "" && + parsed.password === "" + ); +} + +function fetchUrlOf(input: string | URL | Request): string | undefined { + try { + if (typeof input === "string") return input; + if (input instanceof URL) return input.href; + return input.url; + } catch { + return undefined; + } +} + /** #1158 escape hatch: `BILI_RECLAIM_FETCH_PATCH=0` keeps the classic direct * install — a third-party re-arm (dsh-http-proxy refresh) then wins and * bili stops seeing model traffic (documented degradation, visible instead @@ -147,9 +197,10 @@ export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch) // Windows). The depth check below is the proof of such nesting: an // OUR link called from within our own dispatch can only be reached // through a foreign wrapper that captured one of our older tops. - // A recognized pi-web-access link still owns proxy routing even after - // bili dispatched, so retain it at depth 1. Its older bili links must - // still be able to truncate unknown wrapper history. + // A recognized pi-web-access link (either marker, #6829) still owns + // proxy routing even after bili dispatched, so retain it at depth 1. + // Its older bili links must still be able to truncate unknown wrapper + // history. // ponytail: unknown wrappers retain the #1662 cutoff; preserving their // deeper hooks requires a shared chain protocol. const patched = async (input: string | URL | Request, init?: RequestInit): Promise => { @@ -161,9 +212,22 @@ export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch) if (chainHop.getStore() === patched) { return dispatchDepth.run((dispatchDepth.getStore() ?? 0) + 1, () => dispatchBody(input, init)); } + // #6841 rework: classify FIRST. An INLINE_ORIGIN request is served + // in-process at a fresh depth 1 instead of being handed down the + // chain, so it can never reach a foreign wrapper sitting between + // this link and the inline link (the pi-web-access wrapper with a + // proxy scope on curls it before the #1662 depth-1 fix ever runs). + // It cannot loop: once served, the request is never sent down the + // chain again, so it terminates here. + if (isInlineOriginUrl(fetchUrlOf(input) ?? "")) { + return dispatchDepth.run(1, () => dispatchBody(input, init)); + } const depth = dispatchDepth.getStore(); if (depth === 1) { - if (Reflect.get(ds, "__piWebAccessProxyFetch") === true) return send(input, init); + if (isPiWebAccessFetch(ds)) return send(input, init); + // #6841 rework: INLINE_ORIGIN requests never reach this branch + // any more — the classify-first check above serves them + // in-process instead, so the old prefix route-down is gone. const anchor = observedFetches.find((f) => !knownDeadFetches.has(f)); if (anchor !== undefined && anchor !== ds) { if (!warnedReentry) { @@ -184,6 +248,12 @@ export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch) Object.defineProperty(chain, "__piWebAccessProxyFetch", { get: () => Reflect.get(ds, "__piWebAccessProxyFetch"), }); + // #6829: forward the 0.46+ shared marker too, so pi-web-access's own + // install guard can see through the chain that a proxy is already in + // place and does not re-install on every web tool call. + Object.defineProperty(chain, "__piWebAccessSharedProxyFetch", { + get: () => Reflect.get(ds, "__piWebAccessSharedProxyFetch"), + }); return chain; }; @@ -223,7 +293,7 @@ export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch) // leaves; "evict" was the pre-self-heal framing and is dropped // from the wording. Unknown wrappers and any LATER (repeat / // abnormal) marked install keep the full diagnostic below. - const webAccess = Reflect.get(v, "__piWebAccessProxyFetch") === true; + const webAccess = isPiWebAccessFetch(v); // Visibility (#1158): a third-party install used to be silent — // log it so "un-routed by a third party" is diagnosable. Past // REARM_LIMIT the WARNING stops (log spam), but the adoption diff --git a/src/agent/native-intercept.ts b/src/agent/native-intercept.ts index a8999d8690..b6f39689e7 100644 --- a/src/agent/native-intercept.ts +++ b/src/agent/native-intercept.ts @@ -6,8 +6,9 @@ // at extension load always wins. The patch is surgical — it rewrites ONLY // model-API shaped URLs and leaves every other request untouched. -import { installFetchChain, isDeadClosureError, resetFetchChainForTest } from "./fetch-chain/index.js"; +import { installFetchChain, isDeadClosureError, isInlineOriginUrl, resetFetchChainForTest } from "./fetch-chain/index.js"; import { logHostEvent } from "./host-log.js"; +import { serveInlineOrigin } from "./pi-inline.js"; import { envMillis } from "./native-bootstrap.js"; import { matchModelEndpoint, type ModelEndpointPattern } from "../model-endpoints.js"; @@ -1579,7 +1580,25 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState): // network again. Only ever contains origins we ourselves observed dying. const replacedOrigins = new Set(); - installFetchChain((send) => async (input: string | URL | Request, init?: RequestInit): Promise => { + installFetchChain((chainSend) => async (input: string | URL | Request, init?: RequestInit): Promise => { + // #6841 rework: never hand pseudo-origin traffic to a downstream + // that may be a foreign wrapper. The pi-web-access wrapper sits + // ABOVE the inline link; with its proxy scope on it curls an + // INLINE_ORIGIN URL before bili's chain ever sees it, so the #1662 + // depth-1 fix never runs on that path. Bili cannot observe the + // wrapper's scope state from outside, so the rule is unconditional: + // every INLINE_ORIGIN send from this dispatch is served in-process + // by the inline transport instead. Non-inline sends pass through + // untouched, and when the inline transport was never installed the + // serve returns undefined and the send goes down the chain as today. + const send = async (input: string | URL | Request, init?: RequestInit): Promise => { + const url = fetchUrlOf(input); + if (url !== undefined && isInlineOriginUrl(url)) { + const served = await serveInlineOrigin(input, init); + if (served !== undefined) return served; + } + return chainSend(input, init); + }; const url = fetchUrlOf(input); // #3394: resolve the ACTIVE state per request instead of capturing the // first installer's — a hot-reimport swapped in a fresh instance whose diff --git a/src/agent/opencode-v2.ts b/src/agent/opencode-v2.ts index 8cb898c601..1189a6e3c9 100644 --- a/src/agent/opencode-v2.ts +++ b/src/agent/opencode-v2.ts @@ -512,7 +512,7 @@ export function createOpencodeV2Setup(options: OpencodeV2SetupOptions = {}): (ct name: t.name, description: t.description, input: t.inputSchema, - options: { codemode: false, permission: "allow" }, + options: { codemode: false }, execute: makeToolExecutor(t.name), }); } @@ -555,7 +555,7 @@ export function createOpencodeV2Setup(options: OpencodeV2SetupOptions = {}): (ct name: t.name, description: t.description, input: t.input, - options: { codemode: false, permission: "allow" }, + options: { codemode: false }, execute: makeToolExecutor(t.name), }); } diff --git a/src/agent/pi-inline.ts b/src/agent/pi-inline.ts index 7a5846b65f..876d2dc708 100644 --- a/src/agent/pi-inline.ts +++ b/src/agent/pi-inline.ts @@ -6,7 +6,7 @@ // through its loopback-gated admin routes. The fetch captured below the link // is the host's PRE-PATCH fetch — handed to the engine as its upstream // transport so pipeline egress never re-enters the chain. -import { installFetchChain } from "./fetch-chain/index.js"; +import { installFetchChain, isInlineOriginUrl } from "./fetch-chain/index.js"; import { getInlineEngine, INLINE_ORIGIN, type InlineEngine } from "../inline-engine.js"; import { logHostEvent } from "./host-log.js"; @@ -26,6 +26,55 @@ function urlOf(input: string | URL | Request): string | undefined { } } +/** Serve an INLINE_ORIGIN request in-process via the inline engine, using + * the pre-patch fetch captured at install as the engine's upstream + * transport. Returns undefined when the inline transport was never + * installed, or when the URL is not addressed to the inline origin — the + * caller then falls back to the chain send (today's behavior). The native + * intercept calls this for the INLINE_ORIGIN targets it produces, so a + * foreign wrapper sitting between the intercept and this link can never see + * (and, with a pi-web-access proxy scope active, curl) pseudo-origin + * traffic (#6841 rework). */ +export async function serveInlineOrigin(input: string | URL | Request, init?: RequestInit): Promise { + if (!installed) return undefined; + const url = urlOf(input); + if (url === undefined || !isInlineOriginUrl(url)) return undefined; + const upstream = rawSend; + if (upstream === undefined) return undefined; + const signal = init?.signal !== undefined ? init.signal : input instanceof Request ? input.signal : undefined; + signal?.throwIfAborted(); + try { + const engine = await getInlineEngine(); + signal?.throwIfAborted(); + return await engine.dispatch(input, init, upstream); + } catch (err) { + // Caller-side cancel is not an engine failure — propagate it: + // degrading would consume the one-shot warning and re-send a + // request the caller already abandoned. + signal?.throwIfAborted(); + if (err instanceof Error && err.name === "AbortError") throw err; + const msg = err instanceof Error ? err.message : String(err); + if (url.startsWith(MODEL_PREFIX)) { + // Startup or dispatch failed: try the direct fallback by + // stripping the pseudo-origin prefix (the embedded + // upstream URL is absolute) and send direct, uncompressed. + if (!degradeWarned) { + degradeWarned = true; + logHostEvent("error", "[native-client]", `[bili-native] inline request failed — attempting direct model fallback (uncompressed): ${msg}`); + } + const target = url.slice(MODEL_PREFIX.length); + if (input instanceof Request) { + const direct = new Request(target, new Request(input, init)); + // Keep transport-specific init fields without serializing the body twice + // (a second FormData serialization would choose a different boundary). + return await upstream(direct, init && { ...init, headers: undefined, body: undefined }); + } + return await upstream(target, init); + } + throw err; + } +} + /** Install the inline transport as the innermost link of the fetch chain. * Must run before installNativeFetchIntercept (chain order = install order). */ export function installInlineTransport(): boolean { @@ -34,40 +83,9 @@ export function installInlineTransport(): boolean { installed = true; rawSend = send; return async (input: string | URL | Request, init?: RequestInit): Promise => { - const url = urlOf(input); - if (url === undefined || !url.startsWith(INLINE_ORIGIN)) return send(input, init); - const signal = init?.signal !== undefined ? init.signal : input instanceof Request ? input.signal : undefined; - signal?.throwIfAborted(); - try { - const engine = await getInlineEngine(); - signal?.throwIfAborted(); - return await engine.dispatch(input, init, rawSend); - } catch (err) { - // Caller-side cancel is not an engine failure — propagate it: - // degrading would consume the one-shot warning and re-send a - // request the caller already abandoned. - signal?.throwIfAborted(); - if (err instanceof Error && err.name === "AbortError") throw err; - const msg = err instanceof Error ? err.message : String(err); - if (url.startsWith(MODEL_PREFIX)) { - // Startup or dispatch failed: try the direct fallback by - // stripping the pseudo-origin prefix (the embedded - // upstream URL is absolute) and send direct, uncompressed. - if (!degradeWarned) { - degradeWarned = true; - logHostEvent("error", "[native-client]", `[bili-native] inline request failed — attempting direct model fallback (uncompressed): ${msg}`); - } - const target = url.slice(MODEL_PREFIX.length); - if (input instanceof Request) { - const direct = new Request(target, new Request(input, init)); - // Keep transport-specific init fields without serializing the body twice - // (a second FormData serialization would choose a different boundary). - return await send(direct, init && { ...init, headers: undefined, body: undefined }); - } - return await send(target, init); - } - throw err; - } + const served = await serveInlineOrigin(input, init); + if (served !== undefined) return served; + return send(input, init); }; }); return true; diff --git a/src/agent/shared.ts b/src/agent/shared.ts index 86f6217231..5d423ab50b 100644 --- a/src/agent/shared.ts +++ b/src/agent/shared.ts @@ -255,14 +255,15 @@ export type RuntimeInfoReport = { source?: string; }; -// #2943: SessionStart hooks (zcode/kimi) are short-lived child processes the +// #2943: SessionStart hooks (zcode/kimi/claude) are short-lived child processes the // client waits on. The old tail `void main().finally(() => process.exit(0))` // force-exited while an async handle was still open/mid-close — on Windows -// Node 24.14.x that races libuv's win async-handle close path and aborts with +// early Node 24.x (24.14.x/24.15.x confirmed) that races libuv's win +// async-handle close path and aborts with // `assert(!(handle->flags & UV_HANDLE_CLOSING))` (src/win/async.c) BEFORE any // stdout is written, silently killing the whole integration (newer Node ships -// the libuv endgame restructure that removed this race, which is why only -// 24.14.x hit it). Never call process.exit() on the normal path: tear down the +// the libuv endgame restructure that removed this race; 24.21+ is clean). +// Never call process.exit() on the normal path: tear down the // one handle we always open (stdin) and let the loop drain to a NATURAL exit // 0. An unref'd backstop force-exits ONLY if something unexpected keeps the // loop alive — by then every handle has long since settled, so the forced exit diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index abd5888910..ff2564e08a 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -1932,7 +1932,7 @@ export function noteOutboundContextEstimates( export function settleUsageReport( session: Session, - s: { total: number; reportedCached: number | null; output?: number; protocol?: string; upstream?: string; creation?: number }, + s: { total: number; reportedCached: number | null; output?: number; protocol?: string; upstream?: string; creation?: number; context?: number }, localTextEstimate?: number, ): void { // #793: a zero-total sample carries no information (gateway placeholder or @@ -1960,7 +1960,9 @@ export function settleUsageReport( // Net out this turn's compress credit: the post-compress re-request // re-sends the unfolded history, so its usage report over-reports the // context the NEXT request will actually carry (see stream.ts applyRanges). - session.stats.lastInputTokens = Math.max(0, s.total - (session.stats.compressCreditTokens ?? 0)); + // An advisor reply bills several model calls; the context is what its last one read. + const contextTotal = s.context !== undefined && s.context > 0 ? s.context : s.total; + session.stats.lastInputTokens = Math.max(0, contextTotal - (session.stats.compressCreditTokens ?? 0)); // #1569: calibration anchor for estimate-grade turns — written ONLY by // real upstream usage reports (all three response shapes funnel here), // never by estimate-grade samples or arming paths; dropped at native- @@ -1992,7 +1994,7 @@ export function settleUsageReport( // only ever overflows never settles a usage report, so without this the // local estimator's calibre never self-corrects and the same "fits" // verdict re-forwards the same doomed body forever. - ingestCalibrationPair(session, s.total, s.upstream); + ingestCalibrationPair(session, contextTotal, s.upstream); // #1820: a real usage-grade sample supersedes the post-rebuild meter // anchor immediately — no need to wait out its prepare budget. clearPostRebuildAnchor(session); diff --git a/src/claude-native-bootstrap.ts b/src/claude-native-bootstrap.ts index 938c339976..38853b1e3e 100644 --- a/src/claude-native-bootstrap.ts +++ b/src/claude-native-bootstrap.ts @@ -33,7 +33,7 @@ import { mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { execFileSync } from "node:child_process"; import { ensureProxyRunning, LAUNCHER_DEFAULT_HOST } from "./launcher.js"; import { resolveClaudeNativePort } from "./config.js"; -import { postIdentityRegister } from "./agent/shared.js"; +import { postIdentityRegister, runSessionStartHook } from "./agent/shared.js"; import { lanePreferredPort } from "./instance.js"; import { claudeNativeInstalled, isBiliClaudeBaseUrl, repinClaudeManagedBaseUrl } from "./plugin-install.js"; import { claudeRoutingWarningFile } from "./paths.js"; @@ -545,26 +545,23 @@ export function hookMainWithDeps(deps: { runImpl?: (payload: string) => Promise; exit?: (code: number) => void; drainTimeoutMs?: number; + backstopMs?: number; } = {}): void { const input = deps.input ?? process.stdin; const runImpl = deps.runImpl ?? run; - const exit = deps.exit ?? ((code: number) => process.exit(code)); - const destroyInput = (): void => { - try { - (input as { destroy?: () => void }).destroy?.(); - } catch { - // already closed - } - }; let payload = ""; let started = false; const start = (): void => { if (started) return; started = true; clearTimeout(timer); - void runImpl(payload).finally(() => { - destroyInput(); - exit(0); + // #2943: stdin destroy + process.exit in the same tick races libuv's + // Windows async-handle close and aborts before Claude sees exit 0. + // Shared helper tears stdin down and lets the loop drain naturally. + runSessionStartHook(() => runImpl(payload), { + input: input as { destroy?: () => void }, + exit: deps.exit, + backstopMs: deps.backstopMs, }); }; const timer = setTimeout(start, deps.drainTimeoutMs ?? HOOK_STDIN_DRAIN_TIMEOUT_MS); diff --git a/src/external-summary-compress.ts b/src/external-summary-compress.ts index 004032b072..48a353a30e 100644 --- a/src/external-summary-compress.ts +++ b/src/external-summary-compress.ts @@ -3,7 +3,7 @@ import { buildCompressSystemPrompt, parseCompressInput, type ParsedRange } from import { messageTimestampSections } from "./external-summary-surface.js"; import { configuredSummaryPlan } from "./external-summary-runtime.js"; import type { ResolvedKernelConfig } from "./compress-settings.js"; -import { applyRanges, noVisibleContextRefusal, normalizeRangeOrder, recoverEvictedView, supersededRefRejection, type RewriteCtx } from "./stream.js"; +import { applyRanges, externalSummaryBreakerRefusal, recordExternalSummaryFailure, noVisibleContextRefusal, normalizeRangeOrder, recoverEvictedView, supersededRefRejection, type RewriteCtx } from "./stream.js"; import { compressResult, type ProxyToolResult } from "./proxy-tool-result.js"; import { imagePlaceholders } from "./image-note.js"; import { describeExternalSummaryFailure, summaryLengthBudgetClause, type SummaryWork } from "./external-summary.js"; @@ -40,7 +40,12 @@ export async function applyConfiguredCompression(input: unknown, ctx: RewriteCtx // session's effective Config — same cascade that produced the wire). let plan; try { plan = configuredSummaryPlan((ctx.config as ResolvedKernelConfig).externalSummary, ctx.session.id); } - catch { return compressResult("[Compression FAILED: external summary configuration is invalid. Nothing compressed.]", "refused", 0); } + catch { + const advice = "Check the external summary configuration and resolve its credentials before retrying."; + const breaker = externalSummaryBreakerRefusal(ctx, "invalid-external-summary-config"); + const guard = breaker || recordExternalSummaryFailure(ctx, parseCompressInput(withOptionalSummaries(input), callId).ranges, advice); + return compressResult(`[Compression FAILED: external summary configuration is invalid. Nothing compressed. ${advice}]${guard}`, "refused", 0); + } if (!plan) return applyRanges(parseCompressInput(input, callId), ctx); const parsed = parseCompressInput(withOptionalSummaries(input), callId); if (parsed.ranges.length === 0) return applyRanges(parsed, ctx); @@ -56,6 +61,8 @@ export async function applyConfiguredCompression(input: unknown, ctx: RewriteCtx // One projection for judgment AND execution (#3236 discipline). const resolved = supersededRefRejection(parsed.ranges, ctx.session); if (resolved.rejection !== null) return applyRanges({ ...parsed, ranges: resolved.translatedRanges }, ctx); + const breaker = externalSummaryBreakerRefusal(ctx, plan.fingerprint, parsed.ranges); + if (breaker) return compressResult(`[Compression FAILED: external summary requests paused. Nothing compressed.]${breaker}`, "refused", 0); const state = ctx.session.state; const revision = ctx.session.revisionEpoch; const messages = (ctx.compressMessages ?? ctx.messages).map((message) => { @@ -125,7 +132,11 @@ export async function applyConfiguredCompression(input: unknown, ctx: RewriteCtx ctx.log(`[external-summary] completed ${successful.length}/${parsed.ranges.length} range(s); ${describeExternalSummaryFailure(batch)}`); // #4833: the bounded attempt diagnostics (source + budgets + outcomes) ride // the tool result too — enums and numbers only, never payload bytes. - if (successful.length === 0) return compressResult(`[Compression FAILED: all configured external summary candidates failed or exceeded their budget. Nothing compressed; do not retry unchanged configuration — summaries longer than the configured cap are rejected whole, so raise compress.maxSummaryLength or request less via per-range summaryMaxChars.] [${describeExternalSummaryFailure(batch)}]`, "refused", 0); + if (successful.length === 0) { + const advice = "Check the external summary service, credentials and configured budgets before retrying; changing ranges alone does not repair a failed summary service."; + const guard = plan.stats.attempts > 0 ? recordExternalSummaryFailure(ctx, parsed.ranges, advice) : ""; + return compressResult(`[Compression FAILED: all configured external summary candidates failed or exceeded their budget. Nothing compressed; do not retry unchanged configuration — summaries longer than the configured cap are rejected whole, so raise compress.maxSummaryLength or request less via per-range summaryMaxChars.] [${describeExternalSummaryFailure(batch)}]${guard}`, "refused", 0); + } const applied = applyRanges({ ...parsed, ranges: successful }, ctx); // Client call arguments carry only an optional hint, never the generated summary. // In-place refolds retain their old call id in the kernel; keep their anchor too. diff --git a/src/external-summary-runtime.ts b/src/external-summary-runtime.ts index 56b565b2d3..10b4c667fd 100644 --- a/src/external-summary-runtime.ts +++ b/src/external-summary-runtime.ts @@ -1,3 +1,4 @@ +import { createHash } from "node:crypto"; import { SummaryCredentialStore } from "./external-summary-credentials.js"; import { createSummaryHttpCandidate } from "./external-summary-http.js"; import { parseExternalSummarySettings, SUMMARY_CONCURRENCY_DEFAULT, type ExternalSummarySettings } from "./external-summary-settings.js"; @@ -24,6 +25,7 @@ export class ConfiguredSummaryPlan { * (shared-pool congestion, or the plan's budget already exhausted before * reaching the pool — congestion, not a chain failure). */ readonly stats = { calls: 0, attempts: 0 }; + readonly fingerprint: string; private readonly sessionId: string | undefined; // `raw` may be an already-parsed chain off the request rail or raw JSON @@ -36,9 +38,11 @@ export class ConfiguredSummaryPlan { this.settings = parseExternalSummarySettings(raw, { inlineKeys: true }); this.sessionId = sessionId; const proxyUrl = env.BILI_UPSTREAM_PROXY?.trim() || undefined; + const fingerprint = createHash("sha256").update(JSON.stringify({ settings: this.settings, proxyUrl })); this.candidates = this.settings.targets.map((target) => { try { const key = target.apiKey !== undefined ? target.apiKey : store.resolve(target.credentialRef ?? "", env); + fingerprint.update(JSON.stringify(key ?? null)); if (!key) throw new Error(`credential ${target.credentialRef || "(none)"} did not resolve to a value`); const headers: Record = target.protocol === "anthropic" ? { "x-api-key": key } : target.protocol === "google" ? { "x-goog-api-key": key } @@ -65,6 +69,7 @@ export class ConfiguredSummaryPlan { return { async summarize(): Promise { throw new Error("External summary candidate unavailable"); } }; } }); + this.fingerprint = fingerprint.digest("hex"); } async summarize(work: readonly SummaryWork[], signal?: AbortSignal): Promise { diff --git a/src/launcher.ts b/src/launcher.ts index 0fec707d61..24b5e8fd64 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -63,7 +63,6 @@ import { tcpConnectable } from "./liveness.js"; import { selfPackageRoot, isBiliPiEntry, ompPluginLoadedFrom, dshNativeInstalled, claudeNativeInstalled, piPackageStrings } from "./plugin-install.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"; import { VERSION } from "./version.js"; import { classifyStartupFailure } from "./startup-failure-page.js"; @@ -3627,7 +3626,7 @@ export function writeDshAcpPatch(dshHome: string, entryName?: string): string | writeOverlayFileAtomic( dir, ".bili-acp.patch.yml", - `- insert:\n - id: bili-native\n name: ${pluginUrl}\n - id: bili-compaction-gate\n name: ${gateUrl}\n- id: compaction-basic\n config:\n auto: false\n# #3354/#3662: keep in sync with dsh.bundle.patch.yml — web-profile presets run their own compaction-basic in nested rows no patch layer can address by id (#1772); these gates make them wait for the load-time overlay the always-on bili-compaction-gate entry registers (vendored from deshark42/dsh-compaction-basic-disable, MIT)\n${compactionDisablePatchRows()}# #2605: keep in sync with dsh.bundle.patch.yml — a patch replaces the row's whole config, so every default code is restated\n- id: llm-deepseek\n config:\n retryPolicy:\n mode: normal\n retryableCodes: [EMPTY_RESPONSE, RATE_LIMIT, SERVER, TIMEOUT, TRANSPORT, MALFORMED_RESPONSE]\n- id: llm-deepseek-account\n config:\n retryPolicy:\n mode: normal\n retryableCodes: [EMPTY_RESPONSE, RATE_LIMIT, SERVER, TIMEOUT, TRANSPORT, MALFORMED_RESPONSE]\n`, + `- insert:\n - id: bili-native\n name: ${pluginUrl}\n - id: bili-compaction-gate\n name: ${gateUrl}\n- id: compaction-basic\n config:\n auto: false\n# #7111: keep in sync with dsh.bundle.patch.yml — the four preset rows are no longer gated on the gate service (a fail-closed inject bricked session creation); the always-on bili-compaction-gate entry reconciles them after load instead\n# #2605: keep in sync with dsh.bundle.patch.yml — a patch replaces the row's whole config, so every default code is restated\n- id: llm-deepseek\n config:\n retryPolicy:\n mode: normal\n retryableCodes: [EMPTY_RESPONSE, RATE_LIMIT, SERVER, TIMEOUT, TRANSPORT, MALFORMED_RESPONSE]\n- id: llm-deepseek-account\n config:\n retryPolicy:\n mode: normal\n retryableCodes: [EMPTY_RESPONSE, RATE_LIMIT, SERVER, TIMEOUT, TRANSPORT, MALFORMED_RESPONSE]\n`, ); const file = path.join(dir, ".bili-acp.patch.yml"); try { diff --git a/src/loop/adapter-anthropic.ts b/src/loop/adapter-anthropic.ts index 468f08e675..8c8717674c 100644 --- a/src/loop/adapter-anthropic.ts +++ b/src/loop/adapter-anthropic.ts @@ -1,7 +1,7 @@ import type { CoreMessage } from "acp-kernel"; import { coreToAnthropic, healAnthropicSystemPositions, insertBeforeTrailingSystemRun, type AnthropicRequestBody } from "acp-kernel/wire"; import { dedupeAdjacentOutputConfig } from "../dedupe-adjacent-output-config.js"; -import { appendSystemText, declaredInclusiveFromUsage } from "../util.js"; +import { appendSystemText, declaredInclusiveFromUsage, lastIterationContext } from "../util.js"; import { stampAnthropicSystemCacheControl } from "./cache-control.js"; import { buildVisibilityMarker } from "./core.js"; import { composeStreamFilters, createBiliArtifactFilter, createMarkerLineFilter, createTagEchoFilter } from "./tag-echo-filter.js"; @@ -291,6 +291,7 @@ export function createAnthropicAdapter(requestBody: Record, ori let roundInput: number | undefined; let roundCached: number | undefined; let roundCreation: number | undefined; + let roundContext: number | undefined; // #4072: verbatim cache_creation breakdown for THIS round's frames — // like the numerics above, the final round's value is what the // synthetic terminal carries (stitched turns take the post-fold @@ -687,6 +688,7 @@ export function createAnthropicAdapter(requestBody: Record, ori // #4128: honour a billing_usage stamp carried on the delta frame. const declared = declaredInclusiveFromUsage(u); if (declared !== undefined) roundDeclaredInclusive = declared; + roundContext = lastIterationContext(u); if (input !== undefined && input > 0 && cached !== undefined) { // Complete authoritative usage object — e.g. the synthetic // terminal of a stitched multi-round stream (round1 @@ -733,6 +735,7 @@ export function createAnthropicAdapter(requestBody: Record, ori creationTokens: roundCreation, declaredInclusive: roundDeclaredInclusive, cacheCreation: roundCacheCreation, + contextTokens: roundContext, } as ParsedStreamEvent; } maybeWarnDegenerate(stopReason); @@ -758,6 +761,7 @@ export function createAnthropicAdapter(requestBody: Record, ori creationTokens: roundCreation, declaredInclusive: roundDeclaredInclusive, cacheCreation: roundCacheCreation, + contextTokens: roundContext, } as ParsedStreamEvent; } maybeWarnDegenerate(stopReason); diff --git a/src/loop/core.ts b/src/loop/core.ts index 269bb7a600..15df5c4208 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -364,7 +364,7 @@ export type ParsedStreamEvent = // in message_start, which the anthropic adapter forwards usage-neutral // (#1315), so only the synthetic terminal can deliver it to hosts that // derive their prompt-cache TTL from the breakdown. - | { kind: "usage"; inputTokens?: number; outputTokens?: number; cachedTokens?: number; creationTokens?: number; declaredInclusive?: boolean; cacheCreation?: Record } + | { kind: "usage"; inputTokens?: number; outputTokens?: number; cachedTokens?: number; creationTokens?: number; declaredInclusive?: boolean; cacheCreation?: Record; contextTokens?: number } // #1766: keys on the upstream terminal frame that bili does not model // (e.g. Anthropic's safeguard_results) — the loop replaces that frame with // a synthetic completion, so anything not carried here vanishes from the @@ -385,7 +385,7 @@ export type ParsedStreamEvent = export interface EmitCompletionOpts { finishReason?: string; - usage?: { inputTokens?: number; outputTokens?: number; cachedTokens?: number; creationTokens?: number; declaredInclusive?: boolean; cacheCreation?: Record }; + usage?: { inputTokens?: number; outputTokens?: number; cachedTokens?: number; creationTokens?: number; declaredInclusive?: boolean; cacheCreation?: Record; contextTokens?: number }; terminalExtra?: Record; } @@ -515,7 +515,7 @@ export function executeProxyTool( function recordUsage( ctx: LoopCtx, - usage: { inputTokens?: number; outputTokens?: number; cachedTokens?: number; creationTokens?: number; declaredInclusive?: boolean }, + usage: { inputTokens?: number; outputTokens?: number; cachedTokens?: number; creationTokens?: number; declaredInclusive?: boolean; contextTokens?: number }, round: number, ): void { const prompt = usage.inputTokens; @@ -539,10 +539,10 @@ function recordUsage( const foldNew = ctx.session.stats.pendingFoldUsage === true; if (foldNew) ctx.session.stats.pendingFoldUsage = false; ctx.log( - `[acp-usage] round ${round} input=${total} ${reportedCached !== null ? `cached=${reportedCached} (cache hit ${hitPct}%)` : "(no cache report)"}${foldNew ? " fold=new" : ""}${total <= 0 ? " (zero-total: lastInputTokens kept)" : ""}`, + `[acp-usage] round ${round} input=${total} ${reportedCached !== null ? `cached=${reportedCached} (cache hit ${hitPct}%)` : "(no cache report)"}${usage.contextTokens !== undefined ? ` context=${usage.contextTokens}` : ""}${foldNew ? " fold=new" : ""}${total <= 0 ? " (zero-total: lastInputTokens kept)" : ""}`, ); if (total > 0 || reportedCached !== null) { - settleUsageReport(ctx.session, { total, reportedCached, output: out, protocol: ctx.protocol, upstream: ctx.upstreamOrigin, creation: typeof usage.creationTokens === "number" ? usage.creationTokens : undefined }); + settleUsageReport(ctx.session, { total, reportedCached, output: out, protocol: ctx.protocol, upstream: ctx.upstreamOrigin, creation: typeof usage.creationTokens === "number" ? usage.creationTokens : undefined, context: usage.contextTokens }); } } @@ -884,7 +884,7 @@ export async function* runCompressLoop( const roundParts: RoundPart[] = []; let reasoningSealed = true; const calls: ToolCallEmit[] = []; - let usage: { inputTokens?: number; outputTokens?: number; cachedTokens?: number; creationTokens?: number; declaredInclusive?: boolean; cacheCreation?: Record } = {}; + let usage: { inputTokens?: number; outputTokens?: number; cachedTokens?: number; creationTokens?: number; declaredInclusive?: boolean; cacheCreation?: Record; contextTokens?: number } = {}; let finishReason: string | undefined; let streamError: string | undefined; let sawDone = false; @@ -1033,6 +1033,7 @@ export async function* runCompressLoop( creationTokens: ev.creationTokens, declaredInclusive: ev.declaredInclusive, cacheCreation: ev.cacheCreation, + contextTokens: ev.contextTokens, }; } else if (ev.kind === "done") { sawDone = true; diff --git a/src/persist.ts b/src/persist.ts index fc347920c3..f4cc43f8ce 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -36,7 +36,7 @@ import { markDirty } from "./session.js"; import { validCostUsd } from "./billing.js"; import { mergeState } from "./state-hydration.js"; import { restampRefsToLedger, restampContentStore } from "acp-kernel"; -import { LazyBlockContents, isDroppedContentStore, isDroppedMetadata, lazyContentStore, lazyMetadata, peekSession, sessionWeight } from "./session.js"; +import { LazyBlockContents, invalidateResidentWeight, isDroppedContentStore, isDroppedMetadata, lazyContentStore, lazyMetadata, peekSession, sessionWeight } from "./session.js"; import type { Session, BlockContent, BlockView } from "./session.js"; import { orphanContents, resolveStorageMode, UnifiedShadow, hadIntegrityQuickCheckFailure, type StorageMode } from "./storage/shadow.js"; import { noteColdLoad } from "./attach-heartbeat.js"; @@ -3656,6 +3656,7 @@ export class SessionStore { if (!raw || raw.length === 0) return; session.lastMessages = boundedFoldedSnapshot(session); session.lastMessagesFolded = true; + invalidateResidentWeight(session); } /** #5506: the EXACT metadata shape buildRecord persists ({...live, @@ -3725,6 +3726,7 @@ export class SessionStore { session.metadata = lazyMetadata(session.id, { protocol: meta?.protocol, upstreamOrigin: meta?.upstreamOrigin }, (id, m) => { return this.loadResidentFields(id, m, { metadata: true })?.metadata ?? null; }); + invalidateResidentWeight(session); } /** #3050 R3 ②b: after a successful save, an idle session's block @@ -3739,6 +3741,7 @@ export class SessionStore { const existing = session.blockContents; if (existing instanceof LazyBlockContents) { existing.drop(); + invalidateResidentWeight(session); return; } if (existing.size === 0) return; @@ -3748,6 +3751,7 @@ export class SessionStore { }); lazy.drop(); session.blockContents = lazy; + invalidateResidentWeight(session); } /** #4788 phase 2 (owner decision 2026-10-31, discussion #3916-E): the CCR @@ -3784,6 +3788,7 @@ export class SessionStore { session.contentStore = lazyContentStore(session.id, (id) => this.unifiedCcrFor(id) ?? this.contentStoreById(id, { protocol: meta?.protocol, upstreamOrigin: meta?.upstreamOrigin })); + invalidateResidentWeight(session); } /** #4788 phase 2, landing proof for dropResidentContentStore: mirrors the diff --git a/src/plugin.ts b/src/plugin.ts index 2c91145146..5eb1735c64 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -72,7 +72,7 @@ import { describeUpdateReady, getUpdateVisibility } from "./update-notes.js"; import { warnCacheCollapse } from "./cache-warn.js"; import { currentContextObservation, recordContextObservation, settleUsageReport } from "./cache-ledger.js"; import { billOutputUsage } from "./billing.js"; -import { promptInputTotal, declaredInclusiveFromUsage, type WireProtocol } from "./util.js"; +import { promptInputTotal, declaredInclusiveFromUsage, lastIterationContext, type WireProtocol } from "./util.js"; import { stateDir } from "./paths.js"; import { lookupToolUseIdSession, lookupToolWitness, recordToolUseId, recordToolWitness } from "./tool-ring.js"; import { awaitDrain } from "./server/stream-io.js"; @@ -2589,7 +2589,9 @@ export async function handlePluginTool( // part of the context size, but NOT a cache hit (#790). // declaredInclusive = #4128 upstream self-declaration (billing_usage.semantic) // that input_tokens already INCLUDES cache_read — set only on the anthropic lane. -export type UsageSample = { inputTokens?: number; cachedTokens?: number; outputTokens?: number; creationTokens?: number; declaredInclusive?: boolean }; +// contextTokens = the context an advisor reply's last executor call read (its +// top-level usage sums several calls; see lastIterationContext). +export type UsageSample = { inputTokens?: number; cachedTokens?: number; outputTokens?: number; creationTokens?: number; declaredInclusive?: boolean; contextTokens?: number }; function num(v: unknown): number | undefined { return typeof v === "number" && Number.isFinite(v) ? v : undefined; @@ -2652,6 +2654,8 @@ export function usageFromSseEvent(obj: Record): UsageSample | u if (creation !== undefined && creation > 0) sample.creationTokens = creation; const output = num(usage["output_tokens"]); if (output !== undefined) sample.outputTokens = output; + const context = lastIterationContext(usage); + if (context !== undefined) sample.contextTokens = context; // #4128: honour a billing_usage stamp carried on the delta frame too. const declared = declaredInclusiveFromUsage(usage); if (declared !== undefined) sample.declaredInclusive = declared; @@ -2709,8 +2713,8 @@ export function applyUsageSample(session: Session, sample: UsageSample, protocol const hit = reportedCached === null ? undefined : Math.round((100 * reportedCached) / total); const foldNew = session.stats.pendingFoldUsage === true; if (foldNew) session.stats.pendingFoldUsage = false; - loggerLog("info", `[${session.id}] [plugin] [acp-usage] input=${total} ${reportedCached === null ? "(no cache report)" : `cached=${reportedCached} (cache hit ${hit}%)`}${foldNew ? " fold=new" : ""}`); - settleUsageReport(session, { total, reportedCached, output: sample.outputTokens, protocol, upstream: upstreamOrigin, creation: typeof sample.creationTokens === "number" ? sample.creationTokens : undefined }); + loggerLog("info", `[${session.id}] [plugin] [acp-usage] input=${total} ${reportedCached === null ? "(no cache report)" : `cached=${reportedCached} (cache hit ${hit}%)`}${sample.contextTokens !== undefined ? ` context=${sample.contextTokens}` : ""}${foldNew ? " fold=new" : ""}`); + settleUsageReport(session, { total, reportedCached, output: sample.outputTokens, protocol, upstream: upstreamOrigin, creation: typeof sample.creationTokens === "number" ? sample.creationTokens : undefined, context: sample.contextTokens }); } if (sample.outputTokens !== undefined) { session.stats.outputTokens += sample.outputTokens; @@ -2741,6 +2745,7 @@ export function mergeUsageSample(acc: UsageSample, sample: UsageSample): void { if (sample.creationTokens !== undefined) acc.creationTokens = sample.creationTokens; if (sample.outputTokens !== undefined) acc.outputTokens = sample.outputTokens; if (sample.declaredInclusive !== undefined) acc.declaredInclusive = sample.declaredInclusive; + if (sample.contextTokens !== undefined) acc.contextTokens = sample.contextTokens; } /** Terminal reasons after which the model genuinely finished its turn. A diff --git a/src/preflight.ts b/src/preflight.ts index ebe8d88b87..8dba040360 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -27,7 +27,7 @@ import { epochConfirmPending } from "./fold-reconcile.js"; import { deadSkipKeys, filterDeadRanges, parseStructuralEmptyVerdict, recordDeadRange } from "./dead-ranges.js"; import { peekRegistryOutputLimit } from "./registry.js"; import { safePrefix } from "./text-safe.js"; -import { applyEstimateCalibration, currentCalibrationFactor } from "./util.js"; +import { applyEstimateCalibration, currentCalibrationFactor, type WireProtocol } from "./util.js"; import { configuredSummaryPlan, type ConfiguredSummaryPlan } from "./external-summary-runtime.js"; import { messageTimestampSections } from "./external-summary-surface.js"; import { isCodexClient } from "./codex-compact.js"; @@ -474,12 +474,31 @@ function isNonModelTopLevelKey(key: string): boolean { return NON_MODEL_TOP_LEVEL_KEYS.has(key) || key.startsWith("dsh_"); } -export function estimateRawBodyTokens(parsed: unknown): number { +// #6828: a dual-shape body carries the conversation under TWO top-level keys — +// the lane's native field (messages[] on openai/anthropic, input[] on +// responses, contents[] on google) plus another protocol's field (a +// Responses-native host such as dsh sends `input` alongside `messages`). The +// upstream bills only the native field, so counting the foreign copy double- +// counts the conversation and the egress fuse false-trips on the phantom total +// (1,815,699 measured vs ~380K billed, 4.77x). Skip the foreign conversation +// fields for the lane's protocol, TOP LEVEL ONLY — a nested key with the same +// name (a message's own `input` content) is real content and stays counted. +// Without a protocol the meter keeps its fail-closed count-everything behavior. +const FOREIGN_CONVERSATION_FIELDS: Record> = { + responses: new Set(["messages", "system"]), + openai: new Set(["input", "instructions"]), + anthropic: new Set(["input", "instructions"]), + google: new Set(["input", "instructions", "messages", "system"]), +}; + +export function estimateRawBodyTokens(parsed: unknown, protocol?: WireProtocol): number { let tokens = 0; + const foreign = protocol === undefined ? undefined : FOREIGN_CONVERSATION_FIELDS[protocol]; const pending: [unknown, string | undefined][] = []; if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { for (const [k, v] of Object.entries(parsed as Record)) { - if (isNonModelTopLevelKey(k)) continue; + if (foreign?.has(k)) continue; // #6828: the other protocol's conversation field + if (isNonModelTopLevelKey(k)) continue; // #6844: DSH-internal fields the model never sees pending.push([v, k]); } } else { @@ -508,39 +527,51 @@ export function estimateRawBodyTokens(parsed: unknown): number { * oversized content" is actively misleading for a 55.2 MiB body whose * messages[] is only 1.44 MiB. Same walk + same exclusions as * estimateRawBodyTokens, split on the TOP-LEVEL field, so `total` always - * equals estimateRawBodyTokens(parsed) exactly. */ + * equals estimateRawBodyTokens(parsed, protocol) exactly for the same protocol. */ export interface RawBodyBreakdown { total: number; messages: number; tools: number; system: number; other: number; + /** #6844: per-top-level-field token counts inside the `other` bucket, + * descending by tokens — the field dominating `other` is the actionable + * signal in a measured-doom 413 (which host-attached field is metered). + * Empty when `other` is empty. */ + otherTop: Array<{ field: string; tokens: number }>; } -export function estimateRawBodyBreakdown(parsed: unknown): RawBodyBreakdown { +export function estimateRawBodyBreakdown(parsed: unknown, protocol?: WireProtocol): RawBodyBreakdown { const buckets: [number, number, number, number] = [0, 0, 0, 0]; // messages, tools, system, other - const pending: [unknown, string | undefined, number][] = []; + const foreign = protocol === undefined ? undefined : FOREIGN_CONVERSATION_FIELDS[protocol]; + const otherByField = new Map(); + const pending: [unknown, string | undefined, number, string | undefined][] = []; if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { for (const [k, v] of Object.entries(parsed as Record)) { - if (isNonModelTopLevelKey(k)) continue; + if (foreign?.has(k)) continue; // #6828: the other protocol's conversation field + if (isNonModelTopLevelKey(k)) continue; // #6844: DSH-internal fields the model never sees const bucket = k === "messages" ? 0 : k === "tools" ? 1 : k === "system" ? 2 : 3; - pending.push([v, k, bucket]); + pending.push([v, k, bucket, bucket === 3 ? k : undefined]); } } else { - pending.push([parsed, undefined, 3]); + pending.push([parsed, undefined, 3, undefined]); } while (pending.length > 0) { - const [value, key, bucket] = pending.pop()!; + const [value, key, bucket, field] = pending.pop()!; if (typeof value === "string") { - if (!key || !NON_TEXT_BODY_KEYS.has(key)) buckets[bucket] += defaultCountTokens(value); + if (!key || !NON_TEXT_BODY_KEYS.has(key)) { + const tokens = defaultCountTokens(value); + buckets[bucket] += tokens; + if (bucket === 3 && field !== undefined) otherByField.set(field, (otherByField.get(field) ?? 0) + tokens); + } continue; } if (Array.isArray(value)) { - for (const item of value) pending.push([item, key, bucket]); + for (const item of value) pending.push([item, key, bucket, field]); continue; } if (value && typeof value === "object") { - for (const [k, v] of Object.entries(value as Record)) pending.push([v, k, bucket]); + for (const [k, v] of Object.entries(value as Record)) pending.push([v, k, bucket, field]); } } return { @@ -549,6 +580,7 @@ export function estimateRawBodyBreakdown(parsed: unknown): RawBodyBreakdown { tools: buckets[1], system: buckets[2], other: buckets[3], + otherTop: [...otherByField.entries()].map(([field, tokens]) => ({ field, tokens })).sort((a, b) => b.tokens - a.tokens), }; } diff --git a/src/server.ts b/src/server.ts index 82cf750c8a..aa7e2ef76b 100644 --- a/src/server.ts +++ b/src/server.ts @@ -3785,12 +3785,20 @@ interface MeasuredOutbound { // #5000 item 3: dimension clause for the measured-doom messages — which part // of the body carries the mass, so "trim the oversized content" tells the // operator WHERE (a 55.2 MiB #5000 body with 1.44 MiB of messages[] means the -// mass is tools[], not a giant paste). +// mass is tools[], not a giant paste). #6844: `other` is a catch-all bucket — +// when it is non-empty the clause also names the top-level field dominating +// it, so the operator sees WHICH host-attached field is being metered without +// manual arithmetic (the incident's 88%-`other` body took a body dump to +// attribute). export function measuredBreakdownClause(b: RawBodyBreakdown): string { - return `messages ~${b.messages.toLocaleString("en-US")} · tools[] ~${b.tools.toLocaleString("en-US")} · system ~${b.system.toLocaleString("en-US")} · other ~${b.other.toLocaleString("en-US")}`; + const top = b.otherTop[0]; + const other = b.other > 0 && top + ? `other ~${b.other.toLocaleString("en-US")} (top field: ${top.field} ~${top.tokens.toLocaleString("en-US")})` + : `other ~${b.other.toLocaleString("en-US")}`; + return `messages ~${b.messages.toLocaleString("en-US")} · tools[] ~${b.tools.toLocaleString("en-US")} · system ~${b.system.toLocaleString("en-US")} · ${other}`; } -export function measuredOutbound(prepared: { body: string | Buffer; parsedBody?: unknown }): MeasuredOutbound | undefined { +export function measuredOutbound(prepared: { body: string | Buffer; parsedBody?: unknown; protocol?: WireProtocol }): MeasuredOutbound | undefined { let parsed: unknown = prepared.parsedBody; if (parsed === undefined) { const raw = typeof prepared.body === "string" ? prepared.body : prepared.body.toString("utf8"); @@ -3800,7 +3808,10 @@ export function measuredOutbound(prepared: { body: string | Buffer; parsedBody?: return undefined; } } - const breakdown = estimateRawBodyBreakdown(parsed); + // #6828: pass the lane's protocol so a dual-shape body's foreign conversation + // field (e.g. Responses `input` riding alongside OpenAI `messages`) is not + // metered — the upstream never bills it, and counting it false-trips the fuse. + const breakdown = estimateRawBodyBreakdown(parsed, prepared.protocol); return { tokens: breakdown.total, breakdown }; } @@ -4532,7 +4543,9 @@ export async function preflightCompressIfNeeded( let outboundBreakdown: RawBodyBreakdown | undefined; try { const outBody: unknown = JSON.parse(typeof outbound.body === "string" ? outbound.body : outbound.body.toString("utf8")); - outboundBreakdown = estimateRawBodyBreakdown(outBody); + // #6828: same protocol-scoped meter as the relay fuse — a dual-shape + // body's foreign conversation field must not doom-gate the rebuild. + outboundBreakdown = estimateRawBodyBreakdown(outBody, prepared.protocol); outboundRawTokens = outboundBreakdown.total; const outField = outputBudgetField(outBody); const rawBudget = outField !== null ? readOutputBudget(outBody as Record, outField) : undefined; diff --git a/src/server/codex-catalog-cache.ts b/src/server/codex-catalog-cache.ts index 141b8ac27f..22468e6bc5 100644 --- a/src/server/codex-catalog-cache.ts +++ b/src/server/codex-catalog-cache.ts @@ -9,6 +9,8 @@ type Entry = { key: string; savedAt: number; body: string; etag?: string; lastMo type Log = (level: "info" | "warn", message: string) => void; const MAX_BYTES = 1024 * 1024; const MAX_ENTRIES = 16; +const WARM_DEADLINE_MS = 4000; +const COLD_DEADLINE_MS = 10000; const TRANSIENT_CODES = new Set([ "ECONNRESET", "ECONNREFUSED", @@ -103,6 +105,33 @@ export function isCodexCatalogRequest(url: string, init: FetchOptions): boolean } } +function awaitCatalogAbort(pending: Promise, signal: AbortSignal, onLate?: (value: T) => void): Promise { + return new Promise((resolve, reject) => { + let settled = false; + const finish = (complete: () => void) => { + if (settled) return; + settled = true; + signal.removeEventListener("abort", abort); + complete(); + }; + const abort = () => finish(() => reject(signal.reason ?? new DOMException("Aborted", "AbortError"))); + void pending.then( + (value) => { + if (settled) { + // Round already settled (deadline or caller abort): late-response cleanup + // is best-effort and must not surface past the settled outcome. + try { onLate?.(value); } catch { /* best-effort cleanup */ } + return; + } + finish(() => resolve(value)); + }, + (error: unknown) => finish(() => reject(error)), + ); + signal.addEventListener("abort", abort, { once: true }); + if (signal.aborted) abort(); + }); +} + export function createCodexCatalogFetch( options: { directory?: string | null; @@ -256,11 +285,14 @@ export function createCodexCatalogFetch( return replay(entry!); }; const deadline = new AbortController(); - const timeout = entry ? setTimeout(() => deadline.abort(), options.deadlineMs ?? 4000) : undefined; - const linked = entry ? AbortSignal.any([...(signal ? [signal] : []), deadline.signal]) : signal; + const timeout = setTimeout( + () => deadline.abort(Object.assign(new Error("model catalog deadline exceeded"), { name: "TimeoutError", code: "ETIMEDOUT" })), + options.deadlineMs ?? (entry ? WARM_DEADLINE_MS : COLD_DEADLINE_MS), + ); + const linked = AbortSignal.any([...(signal ? [signal] : []), deadline.signal]); let result: FetchResult | undefined; try { - result = await operation(linked); + result = await awaitCatalogAbort(operation(linked), linked, discard); signal?.throwIfAborted(); const status = result.response.status; if ([500, 502, 503, 504].includes(status)) return current() && fresh(entry) ? cached(`temporary HTTP ${status}`, result) : result; @@ -305,7 +337,7 @@ export function createCodexCatalogFetch( if (reader) { try { while (true) { - const part = await reader.read(); + const part = await awaitCatalogAbort(reader.read(), linked); if (part.done) { complete = true; break; diff --git a/src/server/relay.ts b/src/server/relay.ts index 29fa2581a4..3612571327 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -2930,7 +2930,7 @@ const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepare // `total` is always the true context size (see util.ts). const rawUsage = prepared.protocol === "google" ? json.usageMetadata ?? json.usage : json.usage; const u = (rawUsage ?? {}) as Record; - const { total, cached, creation } = usageTotals(prepared.protocol, u); + const { total, cached, creation, context } = usageTotals(prepared.protocol, u); const out = usageOutputTotal(prepared.protocol, u); // #1547: settle through the shared path — stats AND the cache // ledger. Before this, this branch updated stats only, so @@ -2941,7 +2941,7 @@ const reply = prepared && !prepared.sidePassthrough ? createHandoffReply(prepare const billed = typeof total === "number" ? total : 0; if (billed > 0 || reportedCached !== null) { // Use the send-time capture; settlement is not another send. - settleUsageReport(prepared.session, { total: billed, reportedCached, output: out, protocol: prepared.protocol, upstream: targetOrigin, creation }); + settleUsageReport(prepared.session, { total: billed, reportedCached, output: out, protocol: prepared.protocol, upstream: targetOrigin, creation, context }); if (reportedCached !== null) warnCacheCollapse(prepared.session, billed, reportedCached); const hitPct = reportedCached !== null && billed > 0 ? Math.round((100 * reportedCached) / billed) : undefined; loggerLog("info", `[${prepared.session.id}] [acp-usage] input=${billed} ${hitPct === undefined ? "(no cache report)" : `cached=${reportedCached} (cache hit ${hitPct}%)`}${billed <= 0 ? " (zero-total: lastInputTokens kept)" : ""}`); diff --git a/src/server/side-request.ts b/src/server/side-request.ts index 83b2c40993..6ee2e1506e 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -1044,6 +1044,10 @@ export function sideRequestGuard( const maxOut = (field ? readOutputBudget(parsed as Record, field) : undefined) ?? 0; if (reserveHeadroom && limit > 0) limit = reserveOutputHeadroom(limit, maxOut, headroomCap); const imageMass = imageReserve ?? imageTokensInParsedBody(protocol, parsed, imageBilling, configuredCap); - const estimate = estimateRawBodyTokens(parsed) + imageMass; + // #6828: the #554 meter must skip the lane's foreign conversation field — + // a dual-shape body (e.g. Responses `input` riding alongside OpenAI + // `messages`) double-counts the conversation and false-blocks the side + // request the upstream would bill only on the native field. + const estimate = estimateRawBodyTokens(parsed, protocol) + imageMass; return { blocked: limit > 0 && estimate >= limit * SIDE_REQUEST_GUARD_TOLERANCE, estimate, limit }; } diff --git a/src/session.ts b/src/session.ts index 9754b2811d..44d29f5429 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1285,6 +1285,7 @@ function deepSizeOf(value: unknown): number { if (Buffer.isBuffer(v)) return 64 + v.byteLength; if (v instanceof LazyBlockContents && v.isDropped) return 64; // #3050 R3 ②b: folded away — measuring must not re-materialize it if (droppedMetadataProxies.has(v)) return 64; // #3050 R3 ②c: same for folded metadata records + if (droppedContentStoreProxies.has(v)) return 64; let bytes = 64; if (v instanceof Map) { for (const [key, item] of v) bytes += size(key) + size(item) + 32; @@ -1312,6 +1313,10 @@ export function sessionWeight(session: Session): number { return bytes; } +export function invalidateResidentWeight(session: Session): void { + residentWeights.delete(session); +} + /** Per-field resident bytes for one session (#R3 diagnostics). The `total` leg * reuses the revision-cached sessionWeight; the per-field legs walk fresh on * every call (diagnostics only — never hot). Shared substructure between @@ -2189,6 +2194,7 @@ function evictOldest(opts?: { overrideGrace?: boolean }): boolean { if (!oldestId) return false; const s = sessions.get(oldestId)!; const graceOverride = isColdLoadGraced(s); + const liveWeight = sessionWeight(s); const ok = getStore().flushSync(s); if (!ok) { // A prior save does not contain the latest mutations. Keep newer @@ -2196,12 +2202,12 @@ function evictOldest(opts?: { overrideGrace?: boolean }): boolean { return false; } sessions.delete(oldestId); - stampEvictedWeight(oldestId, sessionWeight(s)); + stampEvictedWeight(oldestId, liveWeight); // #3126: eviction is the trigger for the empty-view compress window (the // session's message bytes live only here; state reloads from disk but the // view self-heals only on the next model request). Log it so operators can // correlate ctx=0 receipts with the exact moment a session left memory. - loggerLog("info", `[session] evicted ${oldestId} from resident pool (weight ${sessionWeight(s)} bytes, ${sessions.size} session(s) remain, budget ${MAX_RESIDENT_BYTES} bytes${graceOverride ? ", cold-load grace overridden for count-cap admission" : ""})`); + loggerLog("info", `[session] evicted ${oldestId} from resident pool (weight ${liveWeight} bytes, ${sessions.size} session(s) remain, budget ${MAX_RESIDENT_BYTES} bytes${graceOverride ? ", cold-load grace overridden for count-cap admission" : ""})`); return true; } diff --git a/src/stream.ts b/src/stream.ts index b074b1d456..51684b5f37 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -187,7 +187,7 @@ function normalizedSpecKey(ranges: ParsedRange[]): string { .sort() .join(","); } -function recordCompressFailure(session: Session, key: string, repeatAdvice?: string): string { +function recordCompressFailure(session: Session, key: string, repeatAdvice?: string, deterministic = true): string { if (!key) return ""; const prev = session.metadata[FAIL_STREAK_KEY]; const keys = Array.isArray(prev) ? prev.filter((k): k is string => typeof k === "string") : []; @@ -198,6 +198,7 @@ function recordCompressFailure(session: Session, key: string, repeatAdvice?: str markDirty(session); if (occurrences < 2) return ""; const advice = repeatAdvice ?? "Call acp_status and follow its current eligibility advice; continue the task if no eligible ranges remain."; + if (!deterministic) return ` [Repeat-failure guard: this spec has failed ${occurrences} time(s) in this session because external summarization failed. Changing ranges alone does not repair the summary service. ${advice}]`; return ` [Repeat-failure guard: you have now requested this exact spec ${occurrences} time(s) in this session and it keeps failing with the same error. Repeating it deterministically fails the same way until the visible context changes — do NOT re-issue it. ${advice}]`; } function clearCompressFailures(session: Session): void { @@ -308,6 +309,7 @@ function noteCompressLoopFailure(ctx: RewriteCtx, specLabel: string, errs?: stri const now = Date.now(); let streak = readCompressLoopStreak(ctx.session); if (streak && now - streak.lastAt > COMPRESS_LOOP_DECAY_MS) streak = undefined; + if (streak && (streak.cause === "external-summary") !== (forcedCause === "external-summary")) streak = undefined; const n = (streak?.n ?? 0) + 1; // #2432: persist the failure cause on the streak so surfaces OUTSIDE the // receipt (acp_status) can react to a substrate/stale-ref verdict at n=1 @@ -326,6 +328,7 @@ function noteCompressLoopFailure(ctx: RewriteCtx, specLabel: string, errs?: stri } else { ctx.log(`[warn: compress-loop] failure ${n} in armed streak (last spec: ${label})`); } + if (freshCause === "external-summary") return externalSummaryPauseParagraph(n); if (freshCause.startsWith("substrate-destruction")) { return substrateDestructionPauseParagraph(n); } @@ -348,6 +351,35 @@ export function compressBreakerDetail(session: Session): { n: number; threshold: return { n: s.n, threshold: COMPRESS_LOOP_THRESHOLD, decayMinutes: Math.round(COMPRESS_LOOP_DECAY_MS / 60000), ...(s.cause !== undefined ? { cause: s.cause } : {}) }; } +function externalSummaryPauseParagraph(n: number): string { + return ` [COMPRESS CIRCUIT BREAKER: ${n} consecutive compress failures in this session. External summary requests are paused; changing ranges does not repair a failed summary service. Continue the task without compressing. Requests may resume after the external summary configuration or resolved credentials change, or after 10 minutes of cooldown. One successful fold clears the failure streak.]`; +} + +export function externalSummaryBreakerRefusal(ctx: RewriteCtx, planFingerprint: string, ranges: readonly { summaryMaxChars?: number }[] = []): string { + const caps = [...new Set(ranges.map((range) => Math.min(range.summaryMaxChars ?? Infinity, ctx.config.compress.maxSummaryLength)))].sort((a, b) => a - b); + const fingerprint = createHash("sha256").update(JSON.stringify([ + planFingerprint, ctx.config.compress.minSummaryLength, ctx.config.compress.maxSummaryLength, caps, + ])).digest("hex"); + const key = "externalSummaryConfigHash"; + const previous = ctx.session.metadata[key]; + const streak = readCompressLoopStreak(ctx.session); + if (streak?.cause === "external-summary" && ((typeof previous === "string" && previous !== fingerprint) || Date.now() - streak.lastAt > COMPRESS_LOOP_DECAY_MS)) { + clearCompressFailures(ctx.session); + clearCompressLoopStreak(ctx.session); + } + if (previous !== fingerprint) { + ctx.session.metadata[key] = fingerprint; + markDirty(ctx.session); + } + const detail = compressBreakerDetail(ctx.session); + return detail?.cause === "external-summary" ? externalSummaryPauseParagraph(detail.n) : ""; +} + +export function recordExternalSummaryFailure(ctx: RewriteCtx, ranges: ParsedRange[], advice: string): string { + const key = normalizedSpecKey(ranges); + return recordCompressFailure(ctx.session, key, advice, false) + noteCompressLoopFailure(ctx, key, undefined, "external-summary"); +} + export function compressBreakerArmed(session: Session): boolean { return compressBreakerDetail(session) !== undefined; } diff --git a/src/util.ts b/src/util.ts index 9f05351d4c..9ed5a2b6bf 100644 --- a/src/util.ts +++ b/src/util.ts @@ -171,10 +171,26 @@ export type WireProtocol = "anthropic" | "openai" | "responses" | "google"; * portion, inflating the reported context size (→ premature compression) and * deflating the reported cache-hit rate. */ +// An advisor reply bundles several model calls and its top-level usage sums +// them; `iterations` lists each call, and the last executor call read the +// context the next request will carry. Undefined for a one-call reply. +export function lastIterationContext(usage: Record): number | undefined { + const its = usage["iterations"]; + if (!Array.isArray(its) || its.length < 2) return undefined; + for (let i = its.length - 1; i >= 0; i--) { + const it = its[i] as Record | null; + if (!it || it["type"] !== "message") continue; + const n = (k: string): number => (typeof it[k] === "number" && Number.isFinite(it[k]) ? (it[k] as number) : 0); + const sum = n("input_tokens") + n("cache_read_input_tokens") + n("cache_creation_input_tokens"); + return sum > 0 ? sum : undefined; + } + return undefined; +} + export function usageTotals( protocol: WireProtocol, usage: Record, -): { total: number | undefined; cached: number | undefined; creation?: number | undefined } { +): { total: number | undefined; cached: number | undefined; creation?: number | undefined; context?: number | undefined } { const num = (v: unknown): number | undefined => typeof v === "number" && Number.isFinite(v) ? v : undefined; if (protocol === "anthropic") { @@ -190,6 +206,7 @@ export function usageTotals( // #4899: anthropic is the only wire with a cache-WRITE premium — // surface creation tokens for the USD ledger (billing.ts). creation, + context: lastIterationContext(usage), }; } if (protocol === "openai") { diff --git a/tests/advisor-iterations-usage.test.ts b/tests/advisor-iterations-usage.test.ts new file mode 100644 index 0000000000..185e8e63fd --- /dev/null +++ b/tests/advisor-iterations-usage.test.ts @@ -0,0 +1,92 @@ +import { test, beforeEach } from "node:test"; +import assert from "node:assert/strict"; +import type { Config, CoreMessage } from "acp-kernel"; +import { createCore, createInitialState } from "acp-kernel"; +import { pipePluginChatWithStrip } from "../src/plugin.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { runCompressLoop, createAnthropicAdapter } from "../src/loop/index.ts"; +import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; +import { usageTotals } from "../src/util.ts"; +import type { Session } from "../src/session.ts"; + +// An advisor reply bundles several model calls: the executor, the advisor, +// the executor again. Its top-level usage sums the executor calls, about twice +// the real context, and bili took that sum as the context size: 488,488 read +// as 122% of a 400K window when the context was about 241K, which fired an +// emergency nudge and a server-side fold. The per-call `iterations` list is +// exact; the context the next request carries is what the last executor call +// read. Numbers below are a real advisor reply (2026-11-14T16:31:35Z). + +const LAST_CALL_CONTEXT = 2 + 182925 + 1156; +const BILLED = 4 + 362337 + 4669; +const ADVISOR_USAGE = { + input_tokens: 4, cache_read_input_tokens: 362337, cache_creation_input_tokens: 4669, output_tokens: 270, + iterations: [ + { type: "message", input_tokens: 2, cache_read_input_tokens: 179412, cache_creation_input_tokens: 3513, output_tokens: 86 }, + { type: "advisor_message", model: "claude-fable-5-2", input_tokens: 184943, cache_read_input_tokens: 0, cache_creation_input_tokens: 0, output_tokens: 9936 }, + { type: "message", input_tokens: 2, cache_read_input_tokens: 182925, cache_creation_input_tokens: 1156, output_tokens: 184 }, + ], +}; + +const ev = (type: string, data: unknown): string => `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; +const start = ev("message_start", { + type: "message_start", + message: { id: "msg_a", type: "message", role: "assistant", content: [], stop_reason: null, stop_sequence: null, + usage: { input_tokens: 2, cache_read_input_tokens: 179412, cache_creation_input_tokens: 3513 } }, +}); +const delta = (usage: unknown) => ev("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage }); +const stop = ev("message_stop", { type: "message_stop" }); + +function streamOf(chunks: string[]): ReadableStream { + const enc = new TextEncoder(); + let i = 0; + return new ReadableStream({ + pull(c) { if (i < chunks.length) c.enqueue(enc.encode(chunks[i++])); else c.close(); }, + }); +} + +function makeSession(id: string): Session { + return { + id, meta: {}, metadata: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, contextTokens: 0, lastInputTokens: 0, compressCreditTokens: 0, + retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 }, + state: createInitialState(), createdAt: Date.now(), lastSeen: Date.now(), blockContents: new Map(), inFlight: 0, persisted: false, pendingRetrievals: [], + } as unknown as Session; +} + +function sinkRes(): import("node:http").ServerResponse { + return { write: () => true, end() {}, once() {}, destroyed: false, writableEnded: false } as unknown as import("node:http").ServerResponse; +} + +beforeEach(() => _setStoreForTest(new SessionStore({ enabled: false }))); + +test("plugin lane: an advisor reply sets the context to the last executor call, and bills the sum", async () => { + const s = makeSession("advisor-plugin"); + await pipePluginChatWithStrip(streamOf([start, delta(ADVISOR_USAGE), stop]), sinkRes(), "anthropic", s); + assert.equal(s.stats.lastInputTokens, LAST_CALL_CONTEXT); + assert.equal(s.stats.contextTokens, LAST_CALL_CONTEXT); + assert.equal(s.stats.inputTokens, BILLED, "the cumulative token counter keeps what was billed"); +}); + +test("plugin lane: a one-call reply is unchanged", async () => { + const s = makeSession("plain-plugin"); + const one = { input_tokens: 2, cache_read_input_tokens: 179412, cache_creation_input_tokens: 3513, output_tokens: 86, + iterations: [{ type: "message", input_tokens: 2, cache_read_input_tokens: 179412, cache_creation_input_tokens: 3513, output_tokens: 86 }] }; + await pipePluginChatWithStrip(streamOf([start, delta(one), stop]), sinkRes(), "anthropic", s); + assert.equal(s.stats.lastInputTokens, 2 + 179412 + 3513); +}); + +test("proxy loop: an advisor reply sets the context to the last executor call", async () => { + const body = { model: "claude", messages: [], stream: true, max_tokens: 10 }; + const ctx = { core: createCore(), config: { modelContextLimit: 400000 } as Config, messages: [] as CoreMessage[], session: makeSession("advisor-loop"), log: () => {} }; + for await (const _ of runCompressLoop(streamOf([start, delta(ADVISOR_USAGE), stop]), ctx, body, { url: "http://mock", headers: {} }, createAnthropicAdapter(body), buildCompressSystemPrompt())) { /* drain */ } + assert.equal(ctx.session.stats.lastInputTokens, LAST_CALL_CONTEXT); + assert.equal(ctx.session.stats.inputTokens, BILLED); +}); + +test("non-streaming: usageTotals reports the last executor call as the context", () => { + const t = usageTotals("anthropic", ADVISOR_USAGE); + assert.equal(t.total, BILLED); + assert.equal(t.context, LAST_CALL_CONTEXT); + assert.equal(usageTotals("anthropic", { input_tokens: 5, cache_read_input_tokens: 10 }).context, undefined); +}); diff --git a/tests/claude-fork-lineage.test.ts b/tests/claude-fork-lineage.test.ts index 63152b8f63..f750e8a0ab 100644 --- a/tests/claude-fork-lineage.test.ts +++ b/tests/claude-fork-lineage.test.ts @@ -396,7 +396,7 @@ test("hook entrypoint: run() receives the FULL stdin payload only after drain", const payloadJson = JSON.stringify({ source: "fork", session_id: "11111111-2222-3333-4444-555555555555" }); const runs: string[] = []; const exits: number[] = []; - hookMainWithDeps({ input, runImpl: async (p) => { runs.push(p); }, exit: (c) => exits.push(c) }); + hookMainWithDeps({ input, runImpl: async (p) => { runs.push(p); }, exit: (c) => exits.push(c), backstopMs: 20 }); // The synchronous-call-site bug: at this point run() must NOT have fired // yet — and the payload below arrives asynchronously, exactly like a real // claude SessionStart pipe. @@ -410,6 +410,8 @@ test("hook entrypoint: run() receives the FULL stdin payload only after drain", while (runs.length === 0 && Date.now() < deadline) await new Promise((resolve) => setTimeout(resolve, 10)); assert.equal(runs.length, 1, "run() fires exactly once"); assert.equal(runs[0], payloadJson, "run() sees the complete payload (the old wiring passed \"\")"); + const exitDeadline = Date.now() + 2_000; + while (exits.length === 0 && Date.now() < exitDeadline) await new Promise((resolve) => setTimeout(resolve, 10)); assert.deepEqual(exits, [0]); }); @@ -418,7 +420,7 @@ test("hook entrypoint: a never-closed stdin still fires via the drain timeout", const payloadJson = JSON.stringify({ source: "fork", session_id: "11111111-2222-3333-4444-555555555555" }); const runs: string[] = []; const exits: number[] = []; - hookMainWithDeps({ input, runImpl: async (p) => { runs.push(p); }, exit: (c) => exits.push(c), drainTimeoutMs: 40 }); + hookMainWithDeps({ input, runImpl: async (p) => { runs.push(p); }, exit: (c) => exits.push(c), drainTimeoutMs: 40, backstopMs: 20 }); input.write(payloadJson); // No end(): only the timeout safety net may release the hook — claude // waits for this process to exit, so a hang here would stall the session. @@ -426,6 +428,8 @@ test("hook entrypoint: a never-closed stdin still fires via the drain timeout", while (runs.length === 0 && Date.now() < deadline) await new Promise((resolve) => setTimeout(resolve, 10)); assert.equal(runs.length, 1); assert.equal(runs[0], payloadJson, "timeout path still carries the buffered payload"); + const exitDeadline = Date.now() + 2_000; + while (exits.length === 0 && Date.now() < exitDeadline) await new Promise((resolve) => setTimeout(resolve, 10)); assert.deepEqual(exits, [0]); }); @@ -433,7 +437,7 @@ test("hook entrypoint: empty stdin (end with no data) exits cleanly", async () = const input = new PassThrough(); const runs: string[] = []; const exits: number[] = []; - hookMainWithDeps({ input, runImpl: async (p) => { runs.push(p); }, exit: (c) => exits.push(c) }); + hookMainWithDeps({ input, runImpl: async (p) => { runs.push(p); }, exit: (c) => exits.push(c), backstopMs: 20 }); input.end(); const deadline = Date.now() + 2_000; while (exits.length === 0 && Date.now() < deadline) await new Promise((resolve) => setTimeout(resolve, 10)); diff --git a/tests/codex-catalog-deadline.test.ts b/tests/codex-catalog-deadline.test.ts new file mode 100644 index 0000000000..ee65056bc3 --- /dev/null +++ b/tests/codex-catalog-deadline.test.ts @@ -0,0 +1,150 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { setImmediate } from "node:timers/promises"; +import { createCodexCatalogFetch } from "../src/server/codex-catalog-cache.js"; +import type { fetchWithTimeout } from "../src/fetch-util.js"; + +type FetchResult = Awaited>; +const url = "https://chatgpt.com/backend-api/codex/models"; +const init = { headers: { authorization: "Bearer SYNTHETIC" } }; +const catalog = JSON.stringify({ models: [{ slug: "synthetic", context_window: 10000, model_messages: {} }] }); +const result = (): FetchResult => ({ response: new Response(catalog), clearTimer() {}, stopIdleTimer() {} }); +const create = (deadlineMs = 10) => createCodexCatalogFetch({ directory: null, deadlineMs }); +const never = () => new Promise(() => {}); + +function stalled() { + let cancelled!: () => void; + const cancellation = new Promise((resolve) => { cancelled = resolve; }); + const cleanup = { timer: 0, idle: 0 }; + const response = new Response(new ReadableStream({ + start(controller) { controller.enqueue(new TextEncoder().encode('{"models":[')); }, + cancel() { cancelled(); }, + })); + return { + cancellation, + cleanup, + result: { + response, + clearTimer() { cleanup.timer++; }, + stopIdleTimer() { cleanup.idle++; }, + }, + }; +} + +async function warm(fetch: ReturnType) { + assert.equal(await (await fetch(async () => result(), url, init)).response.text(), catalog); +} + +test("cold catalog deadline defaults to ten seconds and warm deadline stays four seconds", async (t) => { + t.mock.timers.enable({ apis: ["setTimeout"] }); + try { + const cold = createCodexCatalogFetch({ directory: null }); + let coldSettled = false; + const coldFailure = cold(never, url, init).catch((error: unknown) => error).finally(() => { coldSettled = true; }); + t.mock.timers.tick(9999); + await setImmediate(); + assert.equal(coldSettled, false); + t.mock.timers.tick(1); + await setImmediate(); + assert.equal(coldSettled, true); + const coldError = await coldFailure; + assert.ok(coldError instanceof Error && "code" in coldError && coldError.code === "ETIMEDOUT"); + + const hot = createCodexCatalogFetch({ directory: null }); + await warm(hot); + let warmSettled = false; + const waiting = hot(never, url, init).finally(() => { warmSettled = true; }); + t.mock.timers.tick(3999); + await setImmediate(); + assert.equal(warmSettled, false); + t.mock.timers.tick(1); + const cached = await waiting; + assert.equal(cached.response.headers.get("x-bili-model-catalog"), "offline-cache"); + assert.equal(await cached.response.text(), catalog); + } finally { + t.mock.timers.reset(); + } +}); + +test("cold catalog operation that ignores cancellation still reaches its deadline", { timeout: 2000 }, async () => { + await assert.rejects(create()(never, url, init), { name: "TimeoutError", code: "ETIMEDOUT" }); +}); + +test("warm catalog operation that ignores cancellation returns the valid cache", { timeout: 2000 }, async () => { + const fetch = create(); + await warm(fetch); + const cached = await fetch(never, url, init); + assert.equal(cached.response.headers.get("x-bili-model-catalog"), "offline-cache"); + assert.equal(await cached.response.text(), catalog); +}); + +test("cold catalog body that never closes is cancelled and its transport timers are cleared", { timeout: 2000 }, async () => { + const pending = stalled(); + await assert.rejects(create()(async () => pending.result, url, init), { code: "ETIMEDOUT" }); + await pending.cancellation; + assert.deepEqual(pending.cleanup, { timer: 1, idle: 1 }); +}); + +test("warm catalog body that never closes is cancelled before replaying cached metadata", { timeout: 2000 }, async () => { + const fetch = create(); + await warm(fetch); + const pending = stalled(); + const cached = await fetch(async () => pending.result, url, init); + assert.equal(await cached.response.text(), catalog); + assert.equal(cached.response.headers.get("x-bili-model-catalog"), "offline-cache"); + await pending.cancellation; + assert.deepEqual(pending.cleanup, { timer: 1, idle: 1 }); +}); + +test("an operation returning after the deadline has its late response discarded", { timeout: 2000 }, async () => { + let complete!: (value: FetchResult) => void; + const operation = new Promise((resolve) => { complete = resolve; }); + await assert.rejects(create()(() => operation, url, init), { code: "ETIMEDOUT" }); + const late = stalled(); + complete(late.result); + await late.cancellation; + assert.deepEqual(late.cleanup, { timer: 1, idle: 1 }); +}); + +test("caller cancellation stays cancellation during cold and warm body reads", { timeout: 2000 }, async () => { + for (const cached of [false, true]) { + const fetch = create(1000); + if (cached) await warm(fetch); + const controller = new AbortController(); + const reason = new Error("synthetic caller cancellation"); + const pending = stalled(); + const waiting = fetch(async () => pending.result, url, init, controller.signal); + await setImmediate(); + controller.abort(reason); + await assert.rejects(waiting, (error: unknown) => error === reason); + await pending.cancellation; + assert.deepEqual(pending.cleanup, { timer: 1, idle: 1 }); + } +}); + +test("caller cancellation during an operation cleans up its late response", { timeout: 2000 }, async () => { + const fetch = create(1000); + await warm(fetch); + let complete!: (value: FetchResult) => void; + const operation = new Promise((resolve) => { complete = resolve; }); + const controller = new AbortController(); + const reason = new Error("synthetic caller cancellation"); + const waiting = fetch(() => operation, url, init, controller.signal); + controller.abort(reason); + await assert.rejects(waiting, (error: unknown) => error === reason); + const late = stalled(); + complete(late.result); + await late.cancellation; + assert.deepEqual(late.cleanup, { timer: 1, idle: 1 }); +}); + +test("inference requests keep their original signal and transport behavior", async () => { + const controller = new AbortController(); + const expected = result(); + const actual = await create(0)(async (signal) => { + assert.equal(signal, controller.signal); + await setImmediate(); + return expected; + }, url.replace("/models", "/responses"), { ...init, method: "POST" }, controller.signal); + assert.equal(actual, expected); +}); diff --git a/tests/dsh-dispatch-model-window.test.ts b/tests/dsh-dispatch-model-window.test.ts new file mode 100644 index 0000000000..c6ebe4a268 --- /dev/null +++ b/tests/dsh-dispatch-model-window.test.ts @@ -0,0 +1,134 @@ +/** + * dsh llm/stream dispatches are sized against the model THEY stream to, and a + * model's first dispatch carries its window: the window header used to come + * from the session header's model (wrong for auxiliary calls on another + * model) and from a fire-and-forget resolve (absent on a model's first fetch). + */ +import { strict as assert } from "node:assert"; +import { mkdtempSync, rmSync } from "node:fs"; +import { createServer, type Server } from "node:http"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import test from "node:test"; + +delete process.env.BILI_PROVIDER_REWRITES; +delete process.env.BILI_NATIVE_DSH; +delete process.env.BILLION_CONTEXT_PLUGIN; + +import { apply, _resetRegisterForTest, _settleNativeForTest, _stateHeadersForTest, type PluginContext } from "../src/agent/dsh-native.ts"; +import { __resetConversationRecallForTests } from "../src/agent/conversation-recall.ts"; + +const MODEL_URL = "https://api.anthropic.com/v1/messages"; +type Listener = (payload: unknown, next: () => unknown) => unknown; +type Resolve = (provider: string, model: string) => Promise<{ context?: { contextWindow?: number } } | undefined>; + +async function startMockProxy(): Promise<{ origin: string; server: Server }> { + const server = createServer((req, res) => { + res.writeHead(req.url === "/__bili/plugin/manifest" || req.url === "/__bili/plugin/session-name" ? 200 : 404, { "content-type": "application/json" }); + res.end(JSON.stringify(req.url === "/__bili/plugin/manifest" + ? { version: "0.1.119", tools: { anthropic: [{ name: "compress", description: "c", input_schema: { type: "object", properties: {} } }] } } + : {})); + }); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const address = server.address(); + if (address === null || typeof address !== "object") throw new Error("no port"); + return { origin: `http://127.0.0.1:${address.port}`, server }; +} + +async function withDsh(resolve: Resolve, sessionModel: string, fn: (dispatch: (options: Record) => Promise>) => Promise): Promise { + __resetConversationRecallForTests(); + const home = mkdtempSync(join(tmpdir(), "dsh-dispatch-model-")); + const saved = { BILLION_CONTEXT_PROXY: process.env.BILLION_CONTEXT_PROXY, DSH_HOME: process.env.DSH_HOME }; + const proxy = await startMockProxy(); + process.env.BILLION_CONTEXT_PROXY = proxy.origin; + process.env.DSH_HOME = home; + _resetRegisterForTest(proxy.origin); + const listeners: Listener[] = []; + const registered: unknown[] = []; + const services = { llm: { resolveModelInfo: resolve }, agentDefaultModel: { currentSelection: () => ({ provider: "deepseek", model: "main-model" }) } }; + const ctx = { + inject: (deps: readonly string[], cb: (sub: unknown) => void) => { if (deps.includes("llm")) cb(services); }, + on: (name: string, listener: Listener) => { if (name === "llm/stream") listeners.push(listener); }, + emit: () => undefined, + logger: { info: () => undefined, warn: () => undefined, error: () => undefined }, + tools: { register: (tool: unknown) => void registered.push(tool) }, + commands: { register: () => undefined }, + agents: { currentInitiator: () => ({ session: { id: "ambient", requestHeader: () => ({ config: { provider: "deepseek", model: sessionModel } }) } }) }, + } as unknown as PluginContext; + // One adapter fetch per dispatch: the headers stamped on its FIRST request. + const dispatch = async (options: Record): Promise> => { + let stamped: Record = {}; + const base = async function* (): AsyncIterable { + stamped = _stateHeadersForTest()?.(MODEL_URL) ?? {}; + yield 1; + }; + const composed = listeners.reduceRight((acc, l) => (p, n) => l(p, () => acc(p, n)), () => base()); + for await (const _ of composed(options, () => base()) as AsyncIterable) { /* drain */ } + return stamped; + }; + try { + await apply(ctx); + const deadline = Date.now() + 15_000; + while (registered.length === 0) { + if (Date.now() > deadline) throw new Error("timeout waiting for tool registration"); + await new Promise((r) => setTimeout(r, 20)); + } + await fn(dispatch); + } finally { + await _settleNativeForTest(); + proxy.server.close(); + proxy.server.closeIdleConnections?.(); + for (const [key, value] of Object.entries(saved)) { + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + rmSync(home, { recursive: true, force: true }); + _resetRegisterForTest(undefined); + } +} + +const WINDOWS: Record = { "main-model": 1_000_000, "aux-model": 65_536, "fresh-model": 131_072 }; +// The session's own model resolves at boot; every other model resolves late. +const slowResolve: Resolve = (_provider, model) => model === "main-model" + ? Promise.resolve({ context: { contextWindow: WINDOWS[model] } }) + : new Promise((r) => setTimeout(() => r({ context: { contextWindow: WINDOWS[model] } }), 100)); + +test("auxiliary dispatch on another model stamps THAT model's window on its first request", async () => { + await withDsh(slowResolve, "main-model", async (dispatch) => { + const deadline = Date.now() + 15_000; + while (_stateHeadersForTest()?.(MODEL_URL)?.["x-bili-plugin-context-window"] !== "1000000") { + if (Date.now() > deadline) throw new Error("timeout waiting for the session model's window"); + await new Promise((r) => setTimeout(r, 20)); + } + const headers = await dispatch({ sessionId: "sess-aux", purpose: "compaction", provider: "deepseek", model: "aux-model" }); + assert.equal(headers["x-bili-plugin-conversation"], "sess-aux"); + assert.equal(headers["x-bili-plugin-model"], "aux-model"); + assert.equal(headers["x-bili-plugin-context-window"], "65536"); + // A dispatch on the session's own model keeps that model's window. + const main = await dispatch({ sessionId: "sess-aux", provider: "deepseek", model: "main-model" }); + assert.equal(main["x-bili-plugin-model"], "main-model"); + assert.equal(main["x-bili-plugin-context-window"], "1000000"); + }); +}); + +test("a model's first dispatch waits for its window instead of going out without it", async () => { + await withDsh(slowResolve, "fresh-model", async (dispatch) => { + const headers = await dispatch({ sessionId: "sess-fresh" }); + assert.equal(headers["x-bili-plugin-model"], "fresh-model"); + assert.equal(headers["x-bili-plugin-context-window"], "131072"); + }); +}); + +test("a hung resolver delays only a model's first dispatch, and only by the bound", async () => { + const hung: Resolve = (_provider, model) => model === "aux-model" ? new Promise(() => {}) : Promise.resolve({ context: { contextWindow: WINDOWS[model] } }); + await withDsh(hung, "main-model", async (dispatch) => { + let t0 = Date.now(); + const first = await dispatch({ sessionId: "sess-hung", provider: "deepseek", model: "aux-model" }); + const firstMs = Date.now() - t0; + assert.equal(first["x-bili-plugin-context-window"], undefined); + assert.ok(firstMs >= 1_500 && firstMs < 5_000, `first dispatch waited ${firstMs}ms`); + t0 = Date.now(); + await dispatch({ sessionId: "sess-hung", provider: "deepseek", model: "aux-model" }); + assert.ok(Date.now() - t0 < 500, "a later dispatch does not wait on the hung resolve again"); + }); +}); diff --git a/tests/external-summary-runtime.test.ts b/tests/external-summary-runtime.test.ts index b97cdc4ac0..dac4350e70 100644 --- a/tests/external-summary-runtime.test.ts +++ b/tests/external-summary-runtime.test.ts @@ -127,6 +127,166 @@ test("all candidates failing preserves state and refuses without borrowing main }, (_req, res) => { calls++; res.writeHead(503); res.end("private-provider-error"); }); }); +test("three external total failures arm the breaker and further calls dispatch nothing", async () => { + let calls = 0; + await fixture(async (_base, externalSummary) => { + const ctx = context(externalSummary); + const before = JSON.stringify(ctx.session.state); + for (let index = 0; index < 3; index++) await applyConfiguredCompression(args(ctx), ctx); + assert.equal(calls, 6); + assert.equal((ctx.session.metadata.compressFailStreak as { n?: number } | undefined)?.n, 3); + const blocked = await applyConfiguredCompression(args(ctx), ctx); + assert.equal(blocked.outcome, "refused"); + assert.equal(blocked.text, "[Compression FAILED: external summary requests paused. Nothing compressed.] [COMPRESS CIRCUIT BREAKER: 3 consecutive compress failures in this session. External summary requests are paused; changing ranges does not repair a failed summary service. Continue the task without compressing. Requests may resume after the external summary configuration or resolved credentials change, or after 10 minutes of cooldown. One successful fold clears the failure streak.]"); + assert.equal(calls, 6, "fourth call must stop before external HTTP dispatch"); + assert.equal((ctx.session.metadata.compressFailStreak as { n: number }).n, 3, "blocked calls do not extend cooldown"); + assert.equal(JSON.stringify(ctx.session.state), before); + }, (_req, res) => { calls++; res.writeHead(403); res.end("denied"); }); +}); + +test("external failure repetition does not claim that changed visible content repairs the service", async () => { + await fixture(async (_base, externalSummary) => { + const ctx = context(externalSummary); + await applyConfiguredCompression(args(ctx), ctx); + const result = await applyConfiguredCompression(args(ctx), ctx); + assert.match(result.text, /Repeat-failure guard/); + assert.doesNotMatch(result.text, /until the visible context changes|Repeating it deterministically/); + }, (_req, res) => { res.writeHead(403); res.end("denied"); }); +}); + +test("external breaker resets on configuration change and a successful fold clears failures", async () => { + let calls = 0; + let healthy = false; + await fixture(async (_base, externalSummary) => { + const ctx = context(externalSummary); + for (let index = 0; index < 3; index++) await applyConfiguredCompression(args(ctx), ctx); + assert.match((await applyConfiguredCompression(args(ctx), ctx)).text, /CIRCUIT BREAKER/); + assert.equal(calls, 6); + healthy = true; + const targets = externalSummary.targets as Record[]; + targets[0].model = "repaired-model"; + const result = await applyConfiguredCompression(args(ctx), ctx); + assert.equal(result.outcome, "applied", result.text); + assert.equal(calls, 7); + assert.equal(ctx.session.metadata.compressFailStreak, undefined); + assert.equal(ctx.session.metadata.compressFailKeys, undefined); + }, (_req, res) => { calls++; if (healthy) success(res); else { res.writeHead(403); res.end("denied"); } }); +}); + +test("external breaker resets when a rejecting summary length cap is repaired", async () => { + let calls = 0; + await fixture(async (_base, externalSummary) => { + const ctx = context(externalSummary); + ctx.config.compress.maxSummaryLength = 100; + for (let index = 0; index < 3; index++) await applyConfiguredCompression(args(ctx), ctx); + assert.equal(calls, 6); + assert.match((await applyConfiguredCompression(args(ctx), ctx)).text, /CIRCUIT BREAKER/); + assert.equal(calls, 6); + ctx.config.compress.maxSummaryLength = 1000; + assert.equal((await applyConfiguredCompression(args(ctx), ctx)).outcome, "applied"); + assert.equal(calls, 7); + }, (_req, res) => { calls++; success(res); }); +}); + +for (const repairedCap of [1000, undefined]) test(`external breaker recovers when per-range summaryMaxChars becomes ${repairedCap ?? "unset"}`, async () => { + let calls = 0; + await fixture(async (_base, externalSummary) => { + const ctx = context(externalSummary); + const limited = { content: [{ startId: ctx.session.state.messageRefs.byRaw.history, endId: ctx.session.state.messageRefs.byRaw.history, summaryMaxChars: 100 }] }; + for (let index = 0; index < 3; index++) await applyConfiguredCompression(limited, ctx); + assert.equal(calls, 6); + assert.match((await applyConfiguredCompression(limited, ctx)).text, /CIRCUIT BREAKER/); + const repaired = repairedCap === undefined ? args(ctx) : { content: [{ ...limited.content[0], summaryMaxChars: repairedCap }] }; + const result = await applyConfiguredCompression(repaired, ctx); + assert.equal(result.outcome, "applied", result.text); + assert.equal(calls, 7); + assert.equal(ctx.session.metadata.compressFailStreak, undefined); + }, (_req, res) => { calls++; success(res); }); +}); + +test("ordinary parse failures do not count toward the external failure threshold", async () => { + let calls = 0; + let healthy = false; + await fixture(async (_base, externalSummary) => { + const ctx = context(externalSummary); + await applyConfiguredCompression({}, ctx); + await applyConfiguredCompression({}, ctx); + await applyConfiguredCompression(args(ctx), ctx); + assert.equal((ctx.session.metadata.compressFailStreak as { n?: number } | undefined)?.n, 1); + healthy = true; + const result = await applyConfiguredCompression(args(ctx), ctx); + assert.equal(result.outcome, "applied", result.text); + assert.equal(calls, 3); + }, (_req, res) => { calls++; if (healthy) success(res); else { res.writeHead(403); res.end("denied"); } }); +}); + +test("ordinary failures break an external consecutive-failure streak", async () => { + await fixture(async (_base, externalSummary) => { + const ctx = context(externalSummary); + await applyConfiguredCompression(args(ctx), ctx); + await applyConfiguredCompression(args(ctx), ctx); + await applyConfiguredCompression({}, ctx); + await applyConfiguredCompression(args(ctx), ctx); + assert.equal((ctx.session.metadata.compressFailStreak as { n?: number } | undefined)?.n, 1); + assert.equal((ctx.session.metadata.compressFailStreak as { cause?: string } | undefined)?.cause, "external-summary"); + }, (_req, res) => { res.writeHead(403); res.end("denied"); }); +}); + +test("external breaker resets on resolved credential rotation without persisting credential bytes", async () => { + let calls = 0; + await fixture(async (_base, externalSummary) => { + const ctx = context(externalSummary); + for (let index = 0; index < 3; index++) await applyConfiguredCompression(args(ctx), ctx); + new SummaryCredentialStore().set("primary", "repaired-key"); + assert.equal((await applyConfiguredCompression(args(ctx), ctx)).outcome, "applied"); + assert.equal(calls, 7); + assert.doesNotMatch(JSON.stringify(ctx.session.metadata), /test-primary-key|repaired-key/); + }, (req, res) => { + calls++; + if (req.headers.authorization === "Bearer repaired-key") success(res); + else { res.writeHead(403); res.end("denied"); } + }); +}); + +test("external breaker allows a retry after ten minutes and success clears the streak", async () => { + let healthy = false; + await fixture(async (_base, externalSummary) => { + const ctx = context(externalSummary); + for (let index = 0; index < 3; index++) await applyConfiguredCompression(args(ctx), ctx); + const streak = ctx.session.metadata.compressFailStreak as { lastAt: number } | undefined; + assert.ok(streak); + streak.lastAt = Date.now() - 10 * 60 * 1000 - 1; + healthy = true; + assert.equal((await applyConfiguredCompression(args(ctx), ctx)).outcome, "applied"); + assert.equal(ctx.session.metadata.compressFailStreak, undefined); + }, (_req, res) => { if (healthy) success(res); else { res.writeHead(403); res.end("denied"); } }); +}); + +test("an external success before the threshold clears the failure count", async () => { + let healthy = false; + await fixture(async (_base, externalSummary) => { + const ctx = context(externalSummary); + await applyConfiguredCompression(args(ctx), ctx); + assert.equal((ctx.session.metadata.compressFailStreak as { n?: number } | undefined)?.n, 1); + healthy = true; + assert.equal((await applyConfiguredCompression(args(ctx), ctx)).outcome, "applied"); + assert.equal(ctx.session.metadata.compressFailStreak, undefined); + assert.equal(ctx.session.metadata.compressFailKeys, undefined); + }, (_req, res) => { if (healthy) success(res); else { res.writeHead(403); res.end("denied"); } }); +}); + +test("invalid external configuration arms the breaker and recovers when repaired", async () => { + await fixture(async (_base, externalSummary) => { + const ctx = context({ enabled: true, targets: {} }); + for (let index = 0; index < 3; index++) await applyConfiguredCompression(args(ctx), ctx); + assert.equal((ctx.session.metadata.compressFailStreak as { n?: number } | undefined)?.n, 3); + assert.match((await applyConfiguredCompression(args(ctx), ctx)).text, /CIRCUIT BREAKER/); + (ctx.config as Config & { externalSummary?: unknown }).externalSummary = externalSummary; + assert.equal((await applyConfiguredCompression(args(ctx), ctx)).outcome, "applied"); + assert.equal(ctx.session.metadata.compressFailStreak, undefined); + }, (_req, res) => { success(res); }); +}); + test("SSE framing overhead does not consume the summary content budget", async () => { const text = "s".repeat(2000); await fixture(async (_base, externalSummary) => { @@ -543,6 +703,29 @@ test("saturated pool drops every batch before dispatch: preflight reports calls> assert.equal(endpointCalls, 0, "a pure queue-drop must not touch the summary endpoint"); }); +test("active compression queue drops do not arm the external service breaker", async () => { + let endpointCalls = 0; + await fixture(async (_base, externalSummary) => { + const ctx = context({ ...externalSummary, budget: { totalTimeoutMs: 100, targetTimeoutMs: 100 } }); + const holders = [0, 1, 2, 3].map(() => _executorForTest.executeBatch( + [{ content: "pool holder", instructions: "hold the permit" }], + [{ summarize: () => new Promise((resolve) => setTimeout(() => resolve("held"), 1500)) }], + { totalTimeoutMs: 30_000, targetTimeoutMs: 20_000, maxSummaryBytes: 65536 })); + try { + for (let index = 0; index < 3; index++) { + assert.equal((await applyConfiguredCompression(args(ctx), ctx)).outcome, "refused"); + } + assert.equal(endpointCalls, 0); + assert.equal(ctx.session.metadata.compressFailStreak, undefined); + assert.equal(ctx.session.metadata.compressFailKeys, undefined); + } finally { + await Promise.all(holders); + } + assert.equal((await applyConfiguredCompression(args(ctx), ctx)).outcome, "applied"); + assert.equal(endpointCalls, 1, "released queue must allow compression without a config change"); + }, (_req, res) => { endpointCalls++; success(res); }); +}); + test("a dispatched-but-failing attempt is NOT a queue drop: attempts>0 keeps the backoff armable (#2662)", async () => { _setSummaryRetryDelayForTest(0); await fixture(async (_base, externalSummary) => { diff --git a/tests/hook-graceful-exit.test.ts b/tests/hook-graceful-exit.test.ts index 8013b7c155..3eb330f3c8 100644 --- a/tests/hook-graceful-exit.test.ts +++ b/tests/hook-graceful-exit.test.ts @@ -3,7 +3,9 @@ import assert from "node:assert/strict"; import { spawn } from "node:child_process"; import { fileURLToPath } from "node:url"; import path from "node:path"; +import { PassThrough } from "node:stream"; import { runSessionStartHook } from "../src/agent/shared.js"; +import { hookMainWithDeps } from "../src/claude-native-bootstrap.ts"; const here = (p: string) => path.resolve(path.dirname(fileURLToPath(import.meta.url)), p); @@ -48,6 +50,73 @@ describe("runSessionStartHook (#2943)", () => { }); }); +describe("claude bootstrap-hook entry contract (#2943)", () => { + it("normal path: drains stdin and never force-exits", async () => { + const prevExitCode = process.exitCode; + const calls: number[] = []; + const input = new PassThrough(); + try { + hookMainWithDeps({ + input, + runImpl: async () => {}, + exit: (code) => { calls.push(code); }, + backstopMs: 60, + }); + input.write("{}"); + input.end(); + const deadline = Date.now() + 2_000; + while (!input.destroyed && Date.now() < deadline) await new Promise((r) => setTimeout(r, 10)); + assert.equal(input.destroyed, true, "stdin must be torn down before draining"); + assert.deepEqual(calls, [], "process.exit must NOT be called on the normal path (libuv race)"); + assert.equal(process.exitCode, 0, "exit code must stay 0 (fails open)"); + } finally { + process.exitCode = prevExitCode; + } + }); + + it("leak path: unref'd backstop force-exits 0 when something holds the loop", async () => { + const calls: number[] = []; + const input = new PassThrough(); + input.end(); + const leak = setInterval(() => {}, 1000); // ref'd handle keeps the loop alive + try { + hookMainWithDeps({ + input, + runImpl: async () => {}, + exit: (code) => { calls.push(code); }, + backstopMs: 40, + }); + await new Promise((r) => setTimeout(r, 130)); // > backstop + assert.deepEqual(calls, [0], "backstop must force a clean exit(0) when the loop would otherwise stay up"); + } finally { + clearInterval(leak); + } + }); +}); + +describe("claude real entry contract (#2943)", () => { + it("real entry exits 0 promptly and does not hang", async () => { + const hookPath = here("../src/claude-native-bootstrap.ts"); + const env = { ...process.env }; + // BILLION_CONTEXT_PROXY -> planClaudeNativeBootstrap action "exit": earliest + // safe return, no proxy/config side effects (the entry gate is argv-based, + // so NODE_TEST_CONTEXT needs no special handling here). + env.BILLION_CONTEXT_PROXY = "http://127.0.0.1:1"; + const child = spawn(process.execPath, ["--import", "tsx", hookPath], { env, stdio: ["pipe", "pipe", "pipe"] }); + const stderr: string[] = []; + child.stderr.on("data", (d) => stderr.push(String(d))); + child.stdin.write(JSON.stringify({ hook_event_name: "SessionStart", session_id: "t" })); + child.stdin.end(); + const kill = setTimeout(() => child.kill("SIGKILL"), 8000); // bounded: never hang the suite + const code = await new Promise((resolve) => { + child.on("close", (c) => resolve(c)); + child.on("error", () => resolve(null)); + }); + clearTimeout(kill); + assert.equal(code, 0, `claude hook must exit 0 (got ${code}); stderr=${stderr.join("")}`); + }); +}); + describe("zcode bootstrap-hook entry contract (#2943)", () => { it("real entry exits 0 promptly and does not hang", async () => { const hookPath = here("../src/zcode/bootstrap-hook.ts"); diff --git a/tests/issue3354-dsh-compaction-disable.test.ts b/tests/issue3354-dsh-compaction-disable.test.ts index 0474d9ef2a..45796e47cf 100644 --- a/tests/issue3354-dsh-compaction-disable.test.ts +++ b/tests/issue3354-dsh-compaction-disable.test.ts @@ -4,7 +4,7 @@ import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; -import { BILI_COMPACTION_DISABLE_SERVICE, compactionDisablePatchRows, declareDshCompactionActive, detectCompactionOverlayGap, disableBasicCompaction, registerCompactionDisable, _resetDshCompactionVerdictForTest } from "../src/agent/dsh-compaction-disable.ts"; +import { BILI_COMPACTION_DISABLE_SERVICE, declareDshCompactionActive, detectCompactionOverlayGap, disableBasicCompaction, registerCompactionDisable, _resetDshCompactionVerdictForTest } from "../src/agent/dsh-compaction-disable.ts"; import { writeDshAcpPatch } from "../src/launcher.ts"; // #3354: the vendored dsh basic-compaction disable overlay (deshark42/ @@ -13,7 +13,6 @@ import { writeDshAcpPatch } from "../src/launcher.ts"; // The apply-level wiring is covered in tests/dsh-native.test.ts. const TESTS_DIR = path.dirname(fileURLToPath(import.meta.url)); -const PRESET_ROWS = compactionDisablePatchRows(); // #3662: both carriers must mount the always-on gate entry alongside // bili-native — without it the preset inject rows pend forever whenever // bili-native does not run. @@ -100,7 +99,9 @@ test("#3806 detectCompactionOverlayGap: missing service provide -> no-service-pr }); test("#3354 registerCompactionDisable: transform honors the entry predicate and preserves siblings", () => { - type Mw = (raw: unknown, next: () => unknown) => unknown; + // #7112: cordis' real binding — `this` is null, the fiber is the FIRST + // argument; the mock calls below pass it there. + type Mw = (fiber: unknown, next: () => unknown) => unknown; let mw: Mw | undefined; let options: unknown; // #3662: the transform acts only while bili-native declared the lane owned. @@ -119,8 +120,8 @@ test("#3354 registerCompactionDisable: transform honors the entry predicate and { name: "@deepseek-ai/dsh-tool-result-pruner" }, ]; const cfg: Record = { plugins, isolate: { compaction: true } }; - const self = { entry: { options: { name: "@deepseek-ai/dsh-agent-preset" } } }; - const out = mw?.call(self, null, () => cfg) as { plugins?: Array>; isolate?: Record }; + const presetFiber = { entry: { options: { name: "@deepseek-ai/dsh-agent-preset" } } }; + const out = mw?.call(null, presetFiber, () => cfg) as { plugins?: Array>; isolate?: Record }; assert.deepEqual(out.isolate, { compaction: true }); assert.equal(out.plugins?.[0]?.disabled, true); assert.equal(out.plugins?.[1]?.disabled, true); @@ -129,26 +130,25 @@ test("#3354 registerCompactionDisable: transform honors the entry predicate and // compaction rows is rewritten too (custom presets), while an entry // whose plugins carry none keeps its resolved config object identity. const custom: Record = { plugins: [{ name: "@deepseek-ai/dsh-compaction-basic" }] }; - const customOut = mw?.call({ entry: { options: { name: "preset-general" } } }, null, () => custom) as { plugins?: Array> }; + const customOut = mw?.call(null, { entry: { options: { name: "preset-general" } } }, () => custom) as { plugins?: Array> }; assert.equal(customOut.plugins?.[0]?.disabled, true); const untouched: Record = { plugins: [{ name: "@deepseek-ai/dsh-other" }] }; - assert.equal(mw?.call({ entry: { options: { name: "other-entry" } } }, null, () => untouched), untouched); + assert.equal(mw?.call(null, { entry: { options: { name: "other-entry" } } }, () => untouched), untouched); assert.equal(mw?.call(undefined, null, () => untouched), untouched); // next() returning nothing → verbatim passthrough - assert.equal(mw?.call(self, null, () => undefined), undefined); + assert.equal(mw?.call(null, presetFiber, () => undefined), undefined); } finally { _resetDshCompactionVerdictForTest(); } }); -test("#3354 patch carriers agree: root bundle yml and launcher overlay carry the identical preset gate rows", () => { +test("#3354/#7111 patch carriers agree: both mount the gate entry and neither gates the presets", () => { // #2631: normalize CRLF checkouts before the byte-exact includes const bundle = fs.readFileSync(path.join(TESTS_DIR, "..", "dsh.bundle.patch.yml"), "utf8").replaceAll("\r\n", "\n"); for (const id of ["preset-standard", "preset-ptc", "preset-minimal", "preset-cordis"]) { const row = `- id: ${id}\n inject: [${BILI_COMPACTION_DISABLE_SERVICE}]\n`; - assert.ok(bundle.includes(row), `bundle yml must carry the ${id} gate row`); + assert.ok(!bundle.includes(row), `bundle yml must NOT gate ${id} on the gate service (#7111)`); } - assert.ok(bundle.includes(PRESET_ROWS), "bundle yml must carry the gate rows contiguously"); assert.ok(bundle.includes(GATE_INSERT), "bundle yml must mount the always-on gate entry (#3662)"); const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-cd-patch-")); @@ -156,7 +156,7 @@ test("#3354 patch carriers agree: root bundle yml and launcher overlay carry the const file = writeDshAcpPatch(home, "billion-context"); assert.ok(file, "overlay patch must be written"); const overlay = fs.readFileSync(file!, "utf8").replaceAll("\r\n", "\n"); - assert.ok(overlay.includes(PRESET_ROWS), "launcher overlay must carry the same gate rows"); + assert.ok(!overlay.includes(`inject: [${BILI_COMPACTION_DISABLE_SERVICE}]`), "launcher overlay must NOT gate the presets (#7111)"); assert.ok(overlay.includes(GATE_INSERT), "launcher overlay must mount the same gate entry (#3662)"); // the overlay still stands down the host plane and keeps the #2605 retries assert.ok(overlay.includes("- id: compaction-basic\n config:\n auto: false\n")); diff --git a/tests/issue3662-dsh-gate-fallback.test.ts b/tests/issue3662-dsh-gate-fallback.test.ts index dfd5869d12..8471629646 100644 --- a/tests/issue3662-dsh-gate-fallback.test.ts +++ b/tests/issue3662-dsh-gate-fallback.test.ts @@ -19,14 +19,14 @@ const TESTS_DIR = path.dirname(fileURLToPath(import.meta.url)); const GATE_INSERT = `- insert:\n - id: bili-native\n name: billion-context\n - id: bili-compaction-gate\n name: billion-context/dsh-compaction-gate\n`; type GateHost = { - on?: (event: string, listener: (raw: unknown, next: () => unknown) => unknown, options?: { global?: boolean }) => void; + on?: (event: string, listener: (fiber: unknown, next: () => unknown) => unknown, options?: { global?: boolean }) => void; reflect?: { provide?: (name: string, value: unknown) => void } | undefined; }; function mockGateHost() { const order: string[] = []; const provided: Array<{ name: string; value: unknown }> = []; let onOptions: { global?: boolean } | undefined; - let mw: ((raw: unknown, next: () => unknown) => unknown) | undefined; + let mw: ((fiber: unknown, next: () => unknown) => unknown) | undefined; const host: GateHost = { on: (event, listener, options) => { order.push(`on:${event}`); @@ -45,7 +45,9 @@ function mockGateHost() { globalFlag: () => onOptions?.global === true, }; } -const PRESET_SELF = { entry: { options: { name: "@deepseek-ai/dsh-agent-preset" } } }; +// #7112: the fiber is the FIRST listener argument (cordis binds no `this`); +// the mock calls below pass it there, not as `this`. +const PRESET_FIBER = { entry: { options: { name: "@deepseek-ai/dsh-agent-preset" } } }; function presetPlugins(): Array> { return [ { name: "@deepseek-ai/dsh-compaction-basic", config: { auto: true } }, @@ -91,10 +93,10 @@ test("#3662: bili-native absent (disabled / kill-switched) — service still pro // the four presets resolvable instead of pending forever const raw = m.middleware(); assert.equal(typeof raw, "function"); - const mw = raw as (raw: unknown, next: () => unknown) => unknown; + const mw = raw as (fiber: unknown, next: () => unknown) => unknown; const plugins = presetPlugins(); const cfg: Record = { plugins, x: 1 }; - const out = mw.call(PRESET_SELF, null, () => cfg) as Record; + const out = mw.call(null, PRESET_FIBER, () => cfg) as Record; assert.equal(out, cfg, "absent verdict must pass the resolved config through verbatim"); for (const row of plugins) assert.equal(row.disabled, undefined); } finally { @@ -111,10 +113,10 @@ test("#3662: normal boot (verdict active) — the gate entry disables the compac assert.deepEqual(m.provided.map((p) => p.name), [BILI_COMPACTION_DISABLE_SERVICE]); const raw = m.middleware(); assert.equal(typeof raw, "function"); - const mw = raw as (raw: unknown, next: () => unknown) => unknown; + const mw = raw as (fiber: unknown, next: () => unknown) => unknown; const plugins = presetPlugins(); const cfg: Record = { plugins, x: 1 }; - const out = mw.call(PRESET_SELF, null, () => cfg) as { plugins?: Array>; x?: number }; + const out = mw.call(null, PRESET_FIBER, () => cfg) as { plugins?: Array>; x?: number }; assert.equal(out.x, 1); assert.deepEqual(out.plugins?.[0]?.config, { auto: true }); assert.equal(out.plugins?.[0]?.disabled, true); @@ -124,9 +126,9 @@ test("#3662: normal boot (verdict active) — the gate entry disables the compac // they nest no compaction rows — a custom preset nesting them is // rewritten (structural match), unrelated config keeps identity. const otherCfg: Record = { plugins: [{ name: "@deepseek-ai/dsh-other" }] }; - assert.equal(mw.call({ entry: { options: { name: "something-else" } } }, null, () => otherCfg), otherCfg); + assert.equal(mw.call(null, { entry: { options: { name: "something-else" } } }, () => otherCfg), otherCfg); const customCfg: Record = { plugins: [{ name: "@deepseek-ai/dsh-compaction-basic" }] }; - const customOut = mw.call({ entry: { options: { name: "preset-general" } } }, null, () => customCfg) as { plugins?: Array> }; + const customOut = mw.call(null, { entry: { options: { name: "preset-general" } } }, () => customCfg) as { plugins?: Array> }; assert.equal(customOut.plugins?.[0]?.disabled, true); } finally { _resetDshCompactionVerdictForTest(); @@ -138,9 +140,8 @@ test("#3662: both patch carriers mount the gate entry alongside bili-native", () try { const bundle = fs.readFileSync(path.join(TESTS_DIR, "..", "dsh.bundle.patch.yml"), "utf8").replaceAll("\r\n", "\n"); assert.ok(bundle.includes(GATE_INSERT), "bundle patch must insert the always-on gate entry after bili-native"); - for (const id of ["preset-standard", "preset-ptc", "preset-minimal", "preset-cordis"]) { - assert.ok(bundle.includes(`- id: ${id}\n inject: [${BILI_COMPACTION_DISABLE_SERVICE}]\n`)); - } + assert.ok(!bundle.includes(`inject: [${BILI_COMPACTION_DISABLE_SERVICE}]`), + "the bundle patch must not gate the presets on the gate service (#7111)"); // launcher overlay, bare lane (shim resolves the subpath export) const file = writeDshAcpPatch(home, "billion-context"); assert.ok(file, "writeDshAcpPatch must produce the overlay file"); @@ -151,7 +152,8 @@ test("#3662: both patch carriers mount the gate entry alongside bili-native", () assert.ok(file2); const overlay2 = fs.readFileSync(file2, "utf8").replaceAll("\r\n", "\n"); assert.match(overlay2, /^ {4}- id: bili-compaction-gate\n {6}name: file:\/\/.+dsh-compaction-gate\.js$/m); - assert.ok(overlay2.includes(`- id: preset-standard\n inject: [${BILI_COMPACTION_DISABLE_SERVICE}]\n`)); + assert.ok(!overlay2.includes(`inject: [${BILI_COMPACTION_DISABLE_SERVICE}]`), + "the URL-lane overlay must not gate the presets either (#7111)"); } finally { fs.rmSync(home, { recursive: true, force: true }); fs.rmSync(`${home}-bili`, { recursive: true, force: true }); diff --git a/tests/issue4079-egress-window-fuse.test.ts b/tests/issue4079-egress-window-fuse.test.ts index 6a4d423bb0..fa55f181b3 100644 --- a/tests/issue4079-egress-window-fuse.test.ts +++ b/tests/issue4079-egress-window-fuse.test.ts @@ -269,3 +269,52 @@ test("e2e #4079 both-ways: bodies under the doom bars still forward — includin rmrf(root); } }); + +test("e2e #6844 both-ways: a model-hidden DSH top-level field no longer meters the fuse, while a genuinely oversized unknown field still does", async () => { + const root = mkdtempSync(path.join(tmpdir(), "bili-6844-")); + const { ctx, calls, close } = await setup(); + try { + // Incident #6844's shape: DSH 0.2.2-rc.3 attaches `dsh_session_log` + // (up to 8 MiB, documented as staying outside model input) as a + // top-level body field. No `model` in the body → preflight skipped → + // the relay fuse is the last line. The hidden field alone measures + // ~1.25M tokens — over the 1M floored bar — while the visible content + // is a few KB. Pre-#6844 this body 413'd with `other` at 88% of the + // measured body; the fuse is a meter, not a filter: the field still + // rides to the upstream byte-exact. + const r = await fetch(ctx.url, { + method: "POST", + headers: { ...ctx.headers }, + body: JSON.stringify({ + stream: true, + messages: [{ role: "user", content: "hello" }], + dsh_session_log: "L".repeat(5_000_000), + }), + }); + assert.equal(r.status, 200, `a model-hidden DSH field must not trip the fuse (got ${r.status})`); + await r.text(); + assert.equal(calls.length, 1, "the body forwarded to the upstream"); + + // Gate discipline (#3617): an UNKNOWN top-level field is still metered + // — the exemption is a documented-field allowlist, not a hole in the + // fuse — and the 413 names the field dominating the `other` bucket + // (#6844 item 3), so the next unknown field needs no manual arithmetic. + const doomed = await fetch(ctx.url, { + method: "POST", + headers: { ...ctx.headers }, + body: JSON.stringify({ + stream: true, + messages: [{ role: "user", content: "hello" }], + custom_extension: "E".repeat(5_000_000), + }), + }); + assert.equal(doomed.status, 413, `an oversized unknown field must still stop at the fuse (got ${doomed.status})`); + const parsed = JSON.parse(await doomed.text()) as { error?: { code?: string; message?: string } }; + assert.equal(parsed.error?.code, "egress_window_fuse", "the fuse names itself"); + assert.match(parsed.error?.message ?? "", /top field: custom_extension ~[\d,]+/, "the 413 names the field dominating the other bucket"); + assert.equal(calls.length, 1, "the unknown-field body never reached the upstream"); + } finally { + await close(); + rmrf(root); + } +}); diff --git a/tests/issue4147-dsh-overlay-structural.test.ts b/tests/issue4147-dsh-overlay-structural.test.ts index 6cadef1222..66b9d1cc15 100644 --- a/tests/issue4147-dsh-overlay-structural.test.ts +++ b/tests/issue4147-dsh-overlay-structural.test.ts @@ -16,7 +16,9 @@ import { // globalThis telemetry channel so the durable boot note can state actual // coverage (patch-row misses are otherwise loader-log-only). -type Mw = (raw: unknown, next: () => unknown) => unknown; +// #7112: cordis' real binding — `this` is null, the fiber (the name carrier) +// is the FIRST argument; the mock calls below pass it there. +type Mw = (fiber: unknown, next: () => unknown) => unknown; function middleware(): Mw { let mw: Mw | undefined; @@ -43,7 +45,7 @@ test("#4147: custom preset entries nesting compaction rows are rewritten (group { name: "@deepseek-ai/dsh-command-compact" }, ], }; - const out = mw.call({ entry: { options: { name: "preset-general" } } }, null, () => cfg) as { plugins: Array> }; + const out = mw.call(null, { entry: { options: { name: "preset-general" } } }, () => cfg) as { plugins: Array> }; assert.equal(out.plugins[0].disabled, undefined, "unrelated sibling row untouched"); const group = out.plugins[1] as { config: Array> }; assert.equal(group.config[0].disabled, true, "nested group row disabled"); @@ -66,9 +68,9 @@ test("#4147 negative: no compaction rows → resolved config object identity kep declareDshCompactionActive(); const mw = middleware(); const unrelated = { plugins: [{ name: "@deepseek-ai/dsh-tool-result-pruner" }] }; - assert.equal(mw.call({ entry: { options: { name: "preset-shipin" } } }, null, () => unrelated), unrelated); + assert.equal(mw.call(null, { entry: { options: { name: "preset-shipin" } } }, () => unrelated), unrelated); const noPlugins = { other: 1 }; - assert.equal(mw.call({ entry: { options: { name: "preset-shipin" } } }, null, () => noPlugins), noPlugins); + assert.equal(mw.call(null, { entry: { options: { name: "preset-shipin" } } }, () => noPlugins), noPlugins); assert.equal(dshCompactionGateTelemetry(), null, "no rewrite, no coverage record"); } finally { _resetDshCompactionVerdictForTest(); @@ -82,7 +84,7 @@ test("#4147/#3662 negative: absent verdict → stock passthrough records nothing _resetDshCompactionGateTelemetryForTest(); const mw = middleware(); const cfg = { plugins: [{ name: "@deepseek-ai/dsh-compaction-basic" }] }; - const out = mw.call({ entry: { options: { name: "preset-general" } } }, null, () => cfg) as { plugins: Array> }; + const out = mw.call(null, { entry: { options: { name: "preset-general" } } }, () => cfg) as { plugins: Array> }; assert.equal(out, cfg, "verbatim passthrough"); assert.equal((cfg.plugins as Array>)[0].disabled, undefined); assert.equal(dshCompactionGateTelemetry(), null, "stock passthrough leaves no coverage record"); @@ -99,11 +101,11 @@ test("#3962: telemetry dedups per entry, keeps the max row count, flags the fact declareDshCompactionActive(); const mw = middleware(); const one = { plugins: [{ name: "@deepseek-ai/dsh-compaction-basic" }] }; - mw.call({ entry: { options: { name: "preset-general" } } }, null, () => one); + mw.call(null, { entry: { options: { name: "preset-general" } } }, () => one); const two = { plugins: [{ name: "@deepseek-ai/dsh-compaction-basic" }, { name: "@deepseek-ai/dsh-command-compact" }] }; - mw.call({ entry: { options: { name: "preset-general" } } }, null, () => two); + mw.call(null, { entry: { options: { name: "preset-general" } } }, () => two); const factory = { plugins: [{ name: "@deepseek-ai/dsh-compaction-basic" }] }; - mw.call({ entry: { options: { name: "@deepseek-ai/dsh-agent-preset" } } }, null, () => factory); + mw.call(null, { entry: { options: { name: "@deepseek-ai/dsh-agent-preset" } } }, () => factory); const unnamed = { plugins: [{ name: "@deepseek-ai/dsh-compaction-basic" }] }; mw.call(undefined, null, () => unnamed); const t = dshCompactionGateTelemetry()!; @@ -126,7 +128,7 @@ test("#3962: long entry names (URLs) are truncated for the durable note", () => const mw = middleware(); const longName = "file:///".padEnd(200, "x"); const cfg = { plugins: [{ name: "@deepseek-ai/dsh-compaction-basic" }] }; - mw.call({ entry: { options: { name: longName } } }, null, () => cfg); + mw.call(null, { entry: { options: { name: longName } } }, () => cfg); const t = dshCompactionGateTelemetry()!; assert.equal(t.covered[0].entry.length, 120); } finally { diff --git a/tests/issue6770-compaction-coverage-text.test.ts b/tests/issue6770-compaction-coverage-text.test.ts index d6f9406c15..c693070f77 100644 --- a/tests/issue6770-compaction-coverage-text.test.ts +++ b/tests/issue6770-compaction-coverage-text.test.ts @@ -66,7 +66,9 @@ test("#6770: the argument-less default reads the live record the overlay writes" { name: "@deepseek-ai/dsh-command-compact" }, ], }; - mw.call({ entry: { options: { name: "preset-dsh-go-companion" } } }, null, () => cfg); + // #7115: cordis dispatches internal/config as (fiber, next) with this=null, + // so the entry name rides the FIRST argument — pass it there, not as this. + mw.call(null, { entry: { options: { name: "preset-dsh-go-companion" } } }, () => cfg); assert.equal(compactionCoverageText(), "1 preset entry rewritten (preset-dsh-go-companion\u00d72)"); } finally { _resetDshCompactionVerdictForTest(); diff --git a/tests/issue6828-guard-meter-dual-shape.test.ts b/tests/issue6828-guard-meter-dual-shape.test.ts new file mode 100644 index 0000000000..0efaeb0308 --- /dev/null +++ b/tests/issue6828-guard-meter-dual-shape.test.ts @@ -0,0 +1,104 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { defaultCountTokens } from "acp-kernel"; +import { estimateRawBodyBreakdown, estimateRawBodyTokens } from "../src/preflight.ts"; + +// #6828: a dual-shape body carries the conversation under TWO top-level keys — +// the lane's native field (messages[] on openai/anthropic, input[] on +// responses) plus the other protocol's field (a Responses-native host such as +// dsh sends `input` alongside `messages`). The upstream bills only the native +// field; the #554 meter walks every string field, so it counted the +// conversation twice and the egress fuse false-tripped on the phantom total +// (1,815,699 measured vs ~380K billed, 4.77x). The meter must skip the foreign +// conversation fields for the lane's protocol, top level only. +const t = (s: string): number => defaultCountTokens(s); +const WINDOW = 1_048_576; + +test("openai lane: a Responses-shaped `input` copy is not metered (#6828)", () => { + const body = { + model: "deepseek-flash", + messages: [{ role: "user", content: "hello" }], + tools: [{ type: "function", function: { name: "compress", description: "fold" } }], + system: "be helpful", + input: [{ type: "message", role: "user", content: "the whole raw history, again" }], + instructions: "do the thing", + }; + const bd = estimateRawBodyBreakdown(body, "openai"); + // the walk counts every string leaf, roles/types included + assert.equal(bd.messages, t("user") + t("hello")); + assert.equal(bd.system, t("be helpful")); + assert.equal(bd.tools, t("function") + t("compress") + t("fold"), "tools still counted"); + assert.equal(bd.other, t("deepseek-flash"), "other holds only the envelope strings, not input/instructions"); + assert.equal(bd.total, bd.messages + bd.tools + bd.system + bd.other); + assert.equal(bd.total, estimateRawBodyTokens(body, "openai"), "total invariant holds with a protocol"); +}); + +test("responses lane: stray `messages`/`system` copies are not metered (#6828)", () => { + const body = { + model: "deepseek-flash", + input: [{ type: "message", role: "user", content: "the conversation" }], + instructions: "be helpful", + tools: [{ type: "function", function: { name: "compress", description: "fold" } }], + messages: [{ role: "user", content: "the same conversation, openai-shaped" }], + system: "be helpful, again", + }; + const bd = estimateRawBodyBreakdown(body, "responses"); + assert.equal(bd.messages, 0, "foreign messages[] excluded on the responses lane"); + assert.equal(bd.system, 0, "foreign system excluded on the responses lane"); + assert.equal(bd.other, t("deepseek-flash") + t("message") + t("user") + t("the conversation") + t("be helpful")); + assert.equal(bd.total, estimateRawBodyTokens(body, "responses"), "total invariant holds with a protocol"); +}); + +test("no protocol: the meter keeps its fail-closed count-everything behavior (#6828)", () => { + const body = { + messages: [{ role: "user", content: "hello" }], + input: [{ type: "message", role: "user", content: "the whole raw history, again" }], + }; + const bd = estimateRawBodyBreakdown(body); + assert.equal(bd.messages, t("user") + t("hello")); + assert.equal(bd.other, t("message") + t("user") + t("the whole raw history, again"), "without a protocol nothing is excluded"); + assert.equal(bd.total, estimateRawBodyTokens(body)); +}); + +test("nested same-named keys stay counted: only the top level is skipped (#6828)", () => { + const body = { + messages: [{ role: "user", content: "hi", input: "nested input text" }], + input: "foreign top-level copy", + }; + const bd = estimateRawBodyBreakdown(body, "openai"); + assert.equal(bd.messages, t("user") + t("hi") + t("nested input text"), "a message's own `input` field is real content"); + assert.equal(bd.other, 0, "only the top-level `input` is skipped"); +}); + +test("reporter scale: the #6828 fuse reading measures ~213K, not ~1.8M (#6828)", () => { + // models the issue's fuse reading: messages ~126,254 · tools[] ~76,502 · + // system ~10,641 · other ~1,602,302 · total ~1,815,699 vs window 1,048,576, + // while the upstream billed ~380,665 (inputTokens 249 + cacheRead 380,416). + const cjk = (n: number): string => "测".repeat(n); + const ascii = (n: number): string => "a".repeat(n); + const perMsg = Math.floor(126_254 / 25); + const messages = Array.from({ length: 25 }, (_, i) => ({ + role: i % 2 === 0 ? "user" : "assistant", + content: cjk(perMsg), + })); + const tools = [ + { type: "function", function: { name: "compress", description: ascii(100_000), parameters: ascii(100_000) } }, + { type: "function", function: { name: "retrieve", description: ascii(106_008), parameters: "" } }, + ]; + const perItem = Math.floor(1_602_302 / 40); + const input = Array.from({ length: 40 }, (_, i) => ({ + type: "message", + role: i % 2 === 0 ? "user" : "assistant", + content: cjk(perItem), + })); + const body = { model: "deepseek-flash", messages, tools, system: cjk(10_641), input }; + const unscoped = estimateRawBodyBreakdown(body); + assert.ok(unscoped.total > WINDOW, `unscoped meter still sees the phantom total (${unscoped.total})`); + assert.ok(Math.abs(unscoped.total / 380_665 - 4.77) < 0.05, `unscoped ratio reproduces the reported 4.77x (${(unscoped.total / 380_665).toFixed(2)}x)`); + const bd = estimateRawBodyBreakdown(body, "openai"); + assert.ok(bd.total < WINDOW, `openai-lane meter fits the window (${bd.total} < ${WINDOW})`); + assert.ok(Math.abs(bd.messages - 126_254) < 200, `messages bucket matches the report (${bd.messages})`); + assert.ok(Math.abs(bd.tools - 76_502) < 200, `tools bucket matches the report (${bd.tools})`); + assert.equal(bd.system, 10_641, "system bucket matches the report"); + assert.ok(bd.other < 100, `other is envelope-only, not the 1.6M copy (${bd.other})`); +}); diff --git a/tests/issue6844-measured-body-model-hidden-fields.test.ts b/tests/issue6844-measured-body-model-hidden-fields.test.ts new file mode 100644 index 0000000000..3355b96433 --- /dev/null +++ b/tests/issue6844-measured-body-model-hidden-fields.test.ts @@ -0,0 +1,106 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { defaultCountTokens } from "acp-kernel"; +import { estimateRawBodyBreakdown, estimateRawBodyTokens } from "../src/preflight.ts"; +import { measuredBreakdownClause } from "../src/server.ts"; + +// #6844: DSH attaches model-hidden provider metadata as top-level body fields +// (`dsh_session_log`, `dsh_plugin_packages`) — documented as staying outside +// model input — but the measured-body walks priced every top-level field into +// `other`, so an ordinary request 413'd at the egress fuse with `other` at +// 88% of the measured body while the model-visible buckets used 28% of budget. +const t = (s: string): number => defaultCountTokens(s); + +function incidentBody(): { body: Record; visible: number } { + const messages = [{ role: "user", content: "hello message" }]; + const tools = [{ name: "search", description: "find things fast" }]; + const system = "system prompt text"; + const body = { + model: "deepseek-flash", + messages, + tools, + system, + dsh_session_log: "L".repeat(100_000), + dsh_plugin_packages: "P".repeat(10_000), + }; + const visible = t("deepseek-flash") + t("user") + t("hello message") + t("search") + t("find things fast") + t("system prompt text"); + return { body, visible }; +} + +test("model-hidden DSH fields are excluded from the measured body", () => { + const { body, visible } = incidentBody(); + const bd = estimateRawBodyBreakdown(body); + assert.equal(bd.other, t("deepseek-flash")); + assert.equal(bd.total, visible); + assert.equal(estimateRawBodyTokens(body), visible); +}); + +test("a genuinely oversized other field is still counted", () => { + const { body, visible } = incidentBody(); + const extra = "E".repeat(50_000); + const withOther = { ...body, custom_extension: extra }; + const bd = estimateRawBodyBreakdown(withOther); + assert.equal(bd.other, t("deepseek-flash") + t(extra)); + assert.equal(bd.total, visible + t(extra)); + assert.equal(estimateRawBodyTokens(withOther), visible + t(extra)); +}); + +test("the same key nested inside messages[] still counts", () => { + const nested = "N".repeat(1000); + const body = { + messages: [{ role: "user", content: [{ type: "text", text: "hi", dsh_session_log: nested }] }], + }; + const bd = estimateRawBodyBreakdown(body); + assert.equal(bd.messages, t("user") + t("text") + t("hi") + t(nested)); + assert.equal(bd.total, bd.messages); + assert.equal(estimateRawBodyTokens(body), bd.messages); +}); + +test("total invariant holds with hidden and other fields mixed", () => { + const { body, visible } = incidentBody(); + const mixed = { ...body, metadata: { note: "structural note" } }; + const bd = estimateRawBodyBreakdown(mixed); + assert.equal(bd.other, t("deepseek-flash") + t("structural note")); + assert.equal(bd.total, visible + t("structural note")); + assert.equal(bd.total, estimateRawBodyTokens(mixed)); +}); + +test("otherTop attributes the other bucket to its top-level fields, descending (#6844 item 3)", () => { + const { body, visible } = incidentBody(); + const extra = "E".repeat(50_000); + const withOther = { ...body, custom_extension: extra, metadata: { note: "note x" } }; + const bd = estimateRawBodyBreakdown(withOther); + assert.deepEqual(bd.otherTop, [ + { field: "custom_extension", tokens: t(extra) }, + { field: "model", tokens: t("deepseek-flash") }, + { field: "metadata", tokens: t("note x") }, + ], "per-field attribution, largest first"); + assert.equal(bd.otherTop.reduce((sum, e) => sum + e.tokens, 0), bd.other, "otherTop sums to the other bucket"); + assert.equal(bd.total, visible + t(extra) + t("note x")); +}); + +test("hidden fields leave no otherTop attribution; empty other stays empty", () => { + const { body } = incidentBody(); + const bd = estimateRawBodyBreakdown({ ...body, model: undefined }); + assert.equal(bd.other, 0, "only the two hidden fields were in other — both excluded"); + assert.deepEqual(bd.otherTop, []); + assert.deepEqual(estimateRawBodyBreakdown({ messages: [{ role: "user", content: "hi" }] }).otherTop, []); +}); + +test("measuredBreakdownClause names the field dominating other (#6844 item 3)", () => { + const { body } = incidentBody(); + const extra = "E".repeat(50_000); + const bd = estimateRawBodyBreakdown({ ...body, custom_extension: extra }); + const clause = measuredBreakdownClause(bd); + assert.match(clause, /other ~[\d,]+ \(top field: custom_extension ~[\d,]+\)/, `the 413 clause must name the dominating field, got: ${clause}`); + assert.match(clause, /^messages ~[\d,]+ · tools\[\] ~[\d,]+ · system ~[\d,]+ · other /, "the four-bucket shape is unchanged"); +}); + +test("measuredBreakdownClause stays bare when other is empty", () => { + const bd = estimateRawBodyBreakdown({ messages: [{ role: "user", content: "hi" }] }); + assert.equal( + measuredBreakdownClause(bd), + `messages ~${bd.messages.toLocaleString("en-US")} · tools[] ~0 · system ~0 · other ~0`, + "no top-field suffix when other has nothing to attribute", + ); +}); diff --git a/tests/issue7111-gate-fail-open.test.ts b/tests/issue7111-gate-fail-open.test.ts new file mode 100644 index 0000000000..df5241c3de --- /dev/null +++ b/tests/issue7111-gate-fail-open.test.ts @@ -0,0 +1,115 @@ +// #7111: the four Web presets used to be gated on the gate service through an +// entry-level inject. cordis has no optional dependency, no timeout and no +// fallback, so ANY missing gate service left all four presets pending forever +// (agent-presets/not-found on session create) while the host kept booting and +// serving its UI. The gate is now fail-open: the always-on entry reconciles the +// presets that were already mounted instead of letting them wait on it. +import test from "node:test"; +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +import * as gate from "../src/agent/dsh-compaction-gate.ts"; +import { + _resetDshCompactionGateTelemetryForTest, + _resetDshCompactionVerdictForTest, + declareDshCompactionActive, + reconcileLoadedPresets, + type CompactionDisableHost, +} from "../src/agent/dsh-compaction-disable.ts"; +import { writeDshAcpPatch } from "../src/launcher.ts"; + +const TESTS_DIR = path.dirname(fileURLToPath(import.meta.url)); +const GATE_SERVICE = "biliBasicCompactionPolicy"; + +function presetEntry() { + const updates: Array<{ plugins: Array<{ config?: Array<{ name?: string; disabled?: boolean }> }> }> = []; + return { + updates, + entry: { + options: { + id: "preset-standard", + name: "@deepseek-ai/dsh-agent-preset", + config: { + id: "standard", + plugins: [ + { + id: "compaction", + group: true, + config: [{ id: "compaction-basic", name: "@deepseek-ai/dsh-compaction-basic" }], + }, + ], + }, + }, + update: (options: { config: unknown }) => { updates.push(options.config as never) }, + }, + }; +} + +test("#7111: neither carrier gates the presets on the gate service any more", () => { + const bundle = fs.readFileSync(path.join(TESTS_DIR, "..", "dsh.bundle.patch.yml"), "utf8").replaceAll("\r\n", "\n"); + assert.ok(!bundle.includes(`inject: [${GATE_SERVICE}]`), "bundle yml must not gate the presets"); + assert.ok(bundle.includes("- id: bili-compaction-gate"), "the always-on gate entry must stay mounted"); + + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-7111-")); + try { + const file = writeDshAcpPatch(home, "billion-context"); + assert.ok(file, "overlay must be written"); + const overlay = fs.readFileSync(file!, "utf8").replaceAll("\r\n", "\n"); + assert.ok(!overlay.includes(`inject: [${GATE_SERVICE}]`), "launcher overlay must not gate the presets"); + assert.ok(overlay.includes("- id: bili-compaction-gate"), "the overlay must still mount the gate entry"); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + fs.rmSync(`${home}-bili`, { recursive: true, force: true }); + } +}); + +test("#7111: a preset that was already mounted is re-submitted with its compaction row disabled", () => { + _resetDshCompactionVerdictForTest(); + declareDshCompactionActive(); + try { + const { entry, updates } = presetEntry(); + assert.equal(reconcileLoadedPresets([entry]), 1); + assert.equal(updates.length, 1); + const row = updates[0]!.plugins[0]!.config![0]!; + assert.equal(row.disabled, true, "the nested compaction row must be stood down"); + const inputRow = entry.options.config.plugins[0]!.config![0] as { disabled?: boolean }; + assert.equal(inputRow.disabled, undefined, "input stays untouched"); + } finally { + _resetDshCompactionVerdictForTest(); + } +}); + +test("#7111: entries without a nested compaction row, without update, or non-objects are left alone", () => { + const plain = { options: { id: "llm-deepseek", config: { retryPolicy: {} } }, update: () => { throw new Error("must not run") } }; + const frozen = { options: { config: { plugins: [{ name: "@deepseek-ai/dsh-compaction-basic" }] } } }; + assert.equal(reconcileLoadedPresets([plain, frozen, null, undefined, 42]), 0); +}); + +test("#7111: the gate entry reconciles through the optional loader and still provides the service", () => { + _resetDshCompactionVerdictForTest(); + _resetDshCompactionGateTelemetryForTest(); + declareDshCompactionActive(); + try { + const { entry, updates } = presetEntry(); + const provided: string[] = []; + let scan: ((inner: { loader?: { entries?: () => Iterable } }) => void) | undefined; + gate.apply({ + on: () => {}, + reflect: { provide: (name: string) => { provided.push(name) } }, + inject: (_deps: string[], callback: (inner: { loader?: { entries?: () => Iterable } }) => void) => { + scan = callback; + }, + } as unknown as CompactionDisableHost); + + assert.deepEqual(provided, [GATE_SERVICE], "the service is published without waiting for the loader"); + scan?.({ loader: { entries: () => [entry] } }); + assert.equal(updates.length, 1, "an already-mounted preset must be reconciled"); + scan?.({ loader: undefined }); + assert.equal(updates.length, 1, "a missing loader must not throw or reconcile again"); + } finally { + _resetDshCompactionVerdictForTest(); + _resetDshCompactionGateTelemetryForTest(); + } +}); diff --git a/tests/issue7112-config-hook-fiber.test.ts b/tests/issue7112-config-hook-fiber.test.ts new file mode 100644 index 0000000000..80e38bb952 --- /dev/null +++ b/tests/issue7112-config-hook-fiber.test.ts @@ -0,0 +1,120 @@ +// #7112: the internal/config middleware read the entry name from `this`, but +// cordis dispatches that waterfall as `('internal/config', fiber, next)` — the +// first argument is the event name, so no `this` is bound (thisArg null) and +// the listener receives (fiber, next). Every name-keyed branch was therefore +// dead: coverage telemetry degraded to "unnamed-entry", sawFactoryPreset never +// latched, and PR #5994's #5248 restore could never fire. These tests dispatch +// through a REAL cordis Context — the mock hosts elsewhere call the captured +// listener directly, which is exactly why the defect stayed green. +import test from "node:test"; +import assert from "node:assert/strict"; +import { Context } from "@deepseek-ai/cordis"; +import { + BILI_COMPACTION_DISABLE_SERVICE, + _resetDshCompactionGateTelemetryForTest, + _resetDshCompactionVerdictForTest, + declareDshCompactionActive, + dshCompactionGateTelemetry, + entryNameOf, + registerCompactionDisable, +} from "../src/agent/dsh-compaction-disable.ts"; + +const FACTORY_PRESET = "@deepseek-ai/dsh-agent-preset"; + +function presetConfig() { + return { + id: "standard", + plugins: [ + { + id: "compaction", + group: true, + config: [{ id: "compaction-basic", name: "@deepseek-ai/dsh-compaction-basic" }], + }, + ], + }; +} + +function fiberNamed(name: string) { + return { entry: { options: { id: "preset-standard", name } } }; +} + +function dispatch(name: string): { config: unknown; telemetry: ReturnType } { + const ctx = new Context(); + registerCompactionDisable(ctx as unknown as Parameters[0]); + const dispatchConfig = ctx.waterfall as unknown as + (event: string, fiber: unknown, next: () => unknown) => unknown; + const config = dispatchConfig("internal/config", fiberNamed(name), () => presetConfig()); + return { config, telemetry: dshCompactionGateTelemetry() }; +} + +test("#7112: the hook binds no `this`, so the name comes from the fiber argument", () => { + _resetDshCompactionVerdictForTest(); + _resetDshCompactionGateTelemetryForTest(); + declareDshCompactionActive(); + try { + const ctx = new Context(); + let seenThis: unknown = "unset"; + let seenFiberName: string | undefined; + const on = ctx.on as unknown as + (event: string, listener: (fiber: unknown, next: () => unknown) => unknown, options?: { global?: boolean }) => void; + const listener: (this: unknown, fiber: unknown, next: () => unknown) => unknown = + function (this: unknown, fiber: unknown, next: () => unknown) { + seenThis = this; + seenFiberName = entryNameOf(fiber); + return next(); + }; + on("internal/config", listener, { global: true }); + const dispatchConfig = ctx.waterfall as unknown as + (event: string, fiber: unknown, next: () => unknown) => unknown; + dispatchConfig("internal/config", fiberNamed(FACTORY_PRESET), () => presetConfig()); + assert.equal(seenThis, null, "cordis must bind null, not the fiber"); + assert.equal(seenFiberName, FACTORY_PRESET, "the fiber argument carries the entry name"); + } finally { + _resetDshCompactionVerdictForTest(); + } +}); + +test("#7112: coverage telemetry records the real entry name, not unnamed-entry", () => { + _resetDshCompactionVerdictForTest(); + _resetDshCompactionGateTelemetryForTest(); + declareDshCompactionActive(); + try { + const { config, telemetry } = dispatch(FACTORY_PRESET); + assert.ok(telemetry, "the transform must record coverage"); + assert.deepEqual(telemetry.covered, [{ entry: FACTORY_PRESET, rows: 1 }]); + assert.equal(telemetry.sawFactoryPreset, true, "the factory preset must latch"); + const rows = (config as { plugins: Array<{ config: Array<{ disabled?: boolean }> }> }).plugins; + assert.equal(rows[0]?.config[0]?.disabled, true, "the rewrite still disables the row"); + } finally { + _resetDshCompactionVerdictForTest(); + } +}); + +test("#7112: a custom preset keeps its own name and does not latch sawFactoryPreset", () => { + _resetDshCompactionVerdictForTest(); + _resetDshCompactionGateTelemetryForTest(); + declareDshCompactionActive(); + try { + const { telemetry } = dispatch("my-custom-preset"); + assert.deepEqual(telemetry?.covered, [{ entry: "my-custom-preset", rows: 1 }]); + assert.equal(telemetry?.sawFactoryPreset, false); + } finally { + _resetDshCompactionVerdictForTest(); + } +}); + +test("#7112: absent or empty names fall back to undefined (caller renders unnamed-entry)", () => { + assert.equal(entryNameOf(undefined), undefined); + assert.equal(entryNameOf({}), undefined); + assert.equal(entryNameOf({ entry: { options: { name: "" } } }), undefined); + assert.equal(entryNameOf({ entry: { options: { name: FACTORY_PRESET } } }), FACTORY_PRESET); +}); + +test("#7112: the gate service is published even when the transform cannot bind a name", () => { + const provided: string[] = []; + registerCompactionDisable({ + on: () => {}, + reflect: { provide: (name: string) => { provided.push(name) } }, + } as unknown as Parameters[0]); + assert.deepEqual(provided, [BILI_COMPACTION_DISABLE_SERVICE]); +}); diff --git a/tests/issue7113-auto-false-boot-check.test.ts b/tests/issue7113-auto-false-boot-check.test.ts new file mode 100644 index 0000000000..11b0bdceb7 --- /dev/null +++ b/tests/issue7113-auto-false-boot-check.test.ts @@ -0,0 +1,95 @@ +// #7113: a dsh patch replaces a row's whole `config`, and `compaction-basic` +// defaults `auto` to true — so any patch applied AFTER bili (a hand-written +// profile row, a later bundle, a partial #2474 snapshot) that restates +// `- id: compaction-basic` silently re-arms native auto-compaction, and the +// user only learns about it when a checkpoint destroys the fold base. These +// tests pin the detector in both directions plus the gate wiring that turns it +// into a boot-time alarm. +import test from "node:test"; +import assert from "node:assert/strict"; +import * as gate from "../src/agent/dsh-compaction-gate.ts"; +import { + hostPlaneOverrideLostWarning, + hostPlaneRowsMissingAutoFalse, + type CompactionDisableHost, +} from "../src/agent/dsh-compaction-disable.ts"; + +const BASIC = "@deepseek-ai/dsh-compaction-basic"; + +function entry(over: Record = {}) { + return { options: { id: "compaction-basic", name: BASIC, ...over } }; +} + +test("#7113: an enabled row that kept auto:false is not reported", () => { + assert.deepEqual(hostPlaneRowsMissingAutoFalse([entry({ config: { auto: false } })]), []); +}); + +test("#7113: a later patch row that dropped auto is reported by entry id", () => { + assert.deepEqual( + hostPlaneRowsMissingAutoFalse([entry({ config: { thresholdRatio: 0.9 } })]), + ["compaction-basic"], + ); +}); + +test("#7113: a row whose config was replaced entirely is reported", () => { + assert.deepEqual(hostPlaneRowsMissingAutoFalse([{ options: { name: BASIC } }]), [BASIC]); +}); + +test("#7113: a disabled row is stock web-profile behaviour, not a lost override", () => { + assert.deepEqual(hostPlaneRowsMissingAutoFalse([entry({ disabled: true, config: { thresholdRatio: 0.9 } })]), []); +}); + +test("#7113: only compaction-basic rows with a usable config are judged", () => { + assert.deepEqual(hostPlaneRowsMissingAutoFalse([ + { options: { id: "llm-deepseek", name: "@deepseek-ai/dsh-llm-deepseek" } }, + entry({ config: [{ auto: false }] }), + { options: undefined }, + null, + ]), ["compaction-basic"]); +}); + +test("#7113: the alarm names the row and says what to fix", () => { + const message = hostPlaneOverrideLostWarning(["compaction-basic"]); + assert.match(message, /compaction-basic/); + assert.match(message, /auto: false/); + assert.match(message, /ARMED/); + assert.match(hostPlaneOverrideLostWarning(["a", "b"]), /"a", "b"/); +}); + +test("#7113: the gate entry scans the composed tree through an optional loader", () => { + let injectedDeps: string[] | undefined; + let scan: ((inner: { loader?: { entries?: () => Iterable } }) => void) | undefined; + const ctx = { + on: () => {}, + reflect: { provide: () => {} }, + inject: (deps: string[], callback: (inner: { loader?: { entries?: () => Iterable } }) => void) => { + injectedDeps = deps; + scan = callback; + }, + } as unknown as CompactionDisableHost; + + gate.apply(ctx); + assert.deepEqual(injectedDeps, ["loader"], "the loader is awaited in a child fiber, not injected hard"); + assert.equal(typeof scan, "function"); + + assert.equal(gate.hostPlaneWarningFor({ loader: undefined }), null, "no loader ⇒ silent"); + assert.equal(gate.hostPlaneWarningFor({ loader: { entries: () => [] } }), null, "no rows ⇒ silent"); + assert.match( + String(gate.hostPlaneWarningFor({ loader: { entries: () => [entry({ config: { thresholdRatio: 0.9 } })] } })), + /ARMED/, + ); + assert.equal( + gate.hostPlaneWarningFor({ loader: { entries: () => [entry({ config: { auto: false } })] } }), + null, + "healthy declaration ⇒ silent", + ); +}); + +test("#7113: the gate entry keeps publishing the service even without a loader", () => { + const provided: string[] = []; + gate.apply({ + on: () => {}, + reflect: { provide: (name: string) => { provided.push(name) } }, + } as unknown as CompactionDisableHost); + assert.equal(provided.length, 1); +}); diff --git a/tests/memory-contentstore-lazy.test.ts b/tests/memory-contentstore-lazy.test.ts index 66faf5a5da..3801c0f1b3 100644 --- a/tests/memory-contentstore-lazy.test.ts +++ b/tests/memory-contentstore-lazy.test.ts @@ -4,7 +4,7 @@ import { mkdtempSync, readFileSync, readdirSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { SessionStore } from "../src/persist.ts"; -import { isDroppedContentStore, markDirty, residentBreakdown, type Session } from "../src/session.ts"; +import { isDroppedContentStore, markDirty, residentBreakdown, sessionWeight, type Session } from "../src/session.ts"; import { createInitialState, createContentStore, storeOriginal, type CoreMessage, type MessageContentStore } from "acp-kernel"; import { rmrf } from "./tmp-rm.ts"; @@ -70,16 +70,20 @@ test("idle save folds the content store; first touch transparently re-materializ assert.ok(!isDroppedContentStore(s.contentStore), "starts plain"); const fullBytes = residentBreakdown(s).contentStore; assert.ok(fullBytes > 25_000, `seeded originals should be measurable, got ${fullBytes}`); + const beforeTotal = sessionWeight(s); await store.writeNow(s); assert.ok(isDroppedContentStore(s.contentStore), "idle save swaps in the lazy proxy"); - // NOTE: measuring deepSizeOf through the proxy IS a touch (one narrow - // read) — that is the design; RAM goes quiet only until first use. - // First touch: full set back, byte-identical to what was seeded. + assert.ok( + sessionWeight(s) < beforeTotal - Math.floor(fullBytes * 0.5), + `released store must leave the cached weight (${beforeTotal} -> ${sessionWeight(s)}; store was ~${fullBytes} B)`, + ); + assert.equal(residentBreakdown(s).contentStore, 64, "diagnostics count only the unloaded proxy"); + assert.ok(isDroppedContentStore(s.contentStore), "measuring must not load persisted originals"); + assert.equal(Object.keys(s.contentStore!.byRef).length, 30, "all refs visible through the proxy"); const restored = residentBreakdown(s).contentStore; assert.ok(restored >= Math.floor(fullBytes * 0.9), `re-materialized set must match the seed (~${fullBytes}), got ${restored}`); - assert.equal(Object.keys(s.contentStore!.byRef).length, 30, "all refs visible through the proxy"); assert.equal(Object.keys(s.contentStore!.byHash).length, 2, "deduped originals visible"); const entry = s.contentStore!.byRef["m00001"]; assert.equal(entry.rawId, "raw-1"); diff --git a/tests/native-intercept-selfheal.test.ts b/tests/native-intercept-selfheal.test.ts index 155ecc3652..f35732ba63 100644 --- a/tests/native-intercept-selfheal.test.ts +++ b/tests/native-intercept-selfheal.test.ts @@ -666,6 +666,37 @@ test("#2827: first web-access adoption writes nothing to the host terminal and r assert.deepEqual(nativeSink, ["http://127.0.0.1:40001/bili/https://api.example.invalid/v1/messages"]); }); +// #6829: pi-web-access 0.46+ marks its wrapper with the SHARED flag only +// (`__piWebAccessSharedProxyFetch`) — the old flag propagates only when the +// wrapped fetch already has it. The guard must recognize the shared marker +// as the same supported coexistence handshake (#2435), not an unknown +// third-party install, and the marker must be visible through the re-chained +// top so the wrapper's own install guard does not re-install. +test("#6829: a shared-marker pi-web-access install is quietly adopted as coexistence", async () => { + const nativeSink: string[] = []; + const { warns, logs, errors, out, err, lines } = await withDiagnosticsCapture(async () => { + await withScope(fakeFetch(nativeSink), async () => { + installNativeFetchIntercept({ origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }); + const base = globalThis.fetch; + const wrapper = (async (input: string | URL | Request, init?: RequestInit) => base(input, init)) as typeof fetch; + Object.defineProperty(wrapper, "__piWebAccessSharedProxyFetch", { value: true }); + globalThis.fetch = wrapper; + // Marker visible through the re-chained top (forwarded, not lost). + assert.equal(Reflect.get(globalThis.fetch, "__piWebAccessSharedProxyFetch"), true); + const res = await globalThis.fetch("https://api.example.invalid/v1/messages", { method: "POST" }); + assert.equal(res.status, 200); + }); + }); + assert.equal(warns.length, 0); + assert.equal(logs.length, 0); + assert.equal(errors.length, 0); + assertNoTerminalBili(out, err); + const diags = lines.filter(l => l.includes("[native-client]")); + assert.equal(diags.length, 1); + assert.match(diags[0], /\[info\] \[v=[^\]]+\] \[native-client\] \[bili-native\] adopted pi-web-access proxy fetch as downstream \(supported coexistence, #2435\)/); + assert.deepEqual(nativeSink, ["http://127.0.0.1:40001/bili/https://api.example.invalid/v1/messages"]); +}); + // #5907: the no-guard postures (env escape hatch / non-configurable host // property) are exactly where a third-party replacement silently un-routes // model traffic — pin that each one names the degraded posture in bili.log diff --git a/tests/opencode-v2.test.ts b/tests/opencode-v2.test.ts index f2ddb9ca11..cfd774f0fe 100644 --- a/tests/opencode-v2.test.ts +++ b/tests/opencode-v2.test.ts @@ -304,7 +304,7 @@ test("v2 setup: registers the bundled bili tools synchronously with exact schema assert.deepEqual(fake.addedTools.map((t) => t.name), EXPECTED_TOOLS); for (const t of fake.addedTools) { assert.equal(t.options?.codemode, false); - assert.equal(t.options?.permission, "allow"); + assert.equal(t.options?.permission, undefined); } const compressSrc = [...ACP_TOOLS_OPENAI, ABSORB_TOOL_OPENAI].find((t) => t.function.name === "compress")!; assert.deepEqual(fake.addedTools.find((t) => t.name === "compress")!.input, compressSrc.function.parameters); @@ -877,7 +877,7 @@ test("#2443: a manifest-advertised conditional tool (acp_retrieve) is registered const retrieve = fake.addedTools.find((t) => t.name === "acp_retrieve")!; assert.deepEqual(retrieve.input, rt.parameters); assert.equal(retrieve.options?.["codemode"], false); - assert.equal(retrieve.options?.["permission"], "allow"); + assert.equal(retrieve.options?.["permission"], undefined); // The registered executor forwards to the live proxy dispatcher (reachable, not a dead pointer). const out = await retrieve.execute({ ref: "m00001" }, { sessionID: "ses_ccr" }); assert.ok(typeof out.content === "string" && out.content.length > 0, "retrieve executes against the proxy"); diff --git a/tests/pi-inline.test.ts b/tests/pi-inline.test.ts index 00b175a4ff..0e25e15908 100644 --- a/tests/pi-inline.test.ts +++ b/tests/pi-inline.test.ts @@ -7,6 +7,7 @@ // mock upstream, makes exactly ONE upstream round trip per request (loop // safety), and tracks the session in-process. import { after, test } from "node:test"; +import { AsyncLocalStorage, AsyncResource } from "node:async_hooks"; import assert from "node:assert/strict"; import fs from "node:fs"; import os from "node:os"; @@ -112,6 +113,34 @@ async function freshChain() { return piInline; } +// #6841 rework: a minimal stand-in for the pi-web-access 0.46.0 shared proxy +// wrapper — the shape the real bundle installs when its proxy scope is on: +// with a scope active, any http(s) URL that is NOT on its bypass list is +// handled by the wrapper itself (it curls it) and never reaches the fetch +// below; with no scope active it passes through untouched. It marks itself +// with __piWebAccessSharedProxyFetch only (0.46+ behavior). `seenByWrapper` +// records what the wrapper actually observed, and `proxyCalls` records what +// its scope claimed — the assertions that prove inline traffic never reaches +// either. +function installScopeWrapper() { + const proxyScope = new AsyncLocalStorage(); + const proxyCalls: string[] = []; + const seenByWrapper: string[] = []; + const base = globalThis.fetch; + const wrapper = (async (input: string | URL | Request, init?: RequestInit): Promise => { + const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url; + seenByWrapper.push(url); + if (proxyScope.getStore() === true && !/^https?:\/\/(localhost|127\.0\.0\.1|\[::1\])([:/]|$)/.test(url)) { + proxyCalls.push(url); + return new Response("proxy-handled"); + } + return base(input, init); + }) as typeof fetch; + Object.defineProperty(wrapper, "__piWebAccessSharedProxyFetch", { value: true, configurable: true }); + globalThis.fetch = wrapper; + return { proxyScope, proxyCalls, seenByWrapper }; +} + test("#3278 transport: pseudo-origin URLs are served in-process, foreign URLs pass through", async () => { const ie = await import("../src/inline-engine.ts"); const calls: string[] = []; @@ -204,6 +233,266 @@ test("#4527 composition: native intercept + inline link stacked like the real ar } }); +test("#6829: pi-web-access 0.46+ wrapper (shared marker only) keeps model traffic on the inline engine", async () => { + // The production arm (pi-native.ts) installs the inline transport FIRST, + // then the native fetch intercept ON TOP. From pi-web-access 0.46.0 a web + // tool call wraps the intercept link with a wrapper that carries ONLY the + // new `__piWebAccessSharedProxyFetch` marker (the old + // `__piWebAccessProxyFetch` propagates only when the wrapped fetch already + // has it, which bili's chain never sets from native). Pre-fix the wrapper + // fell into the generic re-chain, then the #1662 escape handed the + // rewritten INLINE_ORIGIN request to the oldest live anchor — a real DNS + // lookup of billion-context.inline that dies ENOTFOUND, and only the first + // occurrence is logged, so every later model call failed silently. + const ie = await import("../src/inline-engine.ts"); + const ni = await import("../src/agent/native-intercept.ts"); + const fc = await import("../src/agent/fetch-chain/index.ts"); + const piInline = await import("../src/agent/pi-inline.ts"); + const calls: string[] = []; + ie._setEngineFactoryForTest(async () => ({ + origin: ie.INLINE_ORIGIN, + dispatch: async (input: string | URL | Request): Promise => { + calls.push(String(input)); + return new Response("inline-ok"); + }, + dispatchHttp: async () => {}, + wsOrigin: Promise.resolve(undefined), + webOrigin: Promise.resolve(undefined), + })); + try { + ni._resetForTest(); + fc.resetFetchChainForTest(); + piInline._resetInlineTransportForTest(); + piInline.installInlineTransport(); + const state: import("../src/agent/native-intercept.ts").NativeInterceptState = { origin: ie.INLINE_ORIGIN, ready: Promise.resolve(ie.INLINE_ORIGIN) }; + assert.equal(ni.installNativeFetchIntercept(state), true); + // 0.46+ installGlobalProxyFetch shape: wraps the current top and marks + // ONLY the shared flag; passes everything else through to its base. + const seenByWrapper: string[] = []; + const base = globalThis.fetch; + const wrapper = (async (input: string | URL | Request, init?: RequestInit) => { + seenByWrapper.push(typeof input === "string" ? input : input instanceof URL ? input.href : (input as Request).url); + return base(input, init); + }) as typeof fetch; + Object.defineProperty(wrapper, "__piWebAccessSharedProxyFetch", { value: true }); + globalThis.fetch = wrapper; + // The shared marker must be visible through bili's re-chained top, so + // pi-web-access's own install guard sees a proxy is already in place + // and does not re-install on every web tool call. + assert.equal(Reflect.get(globalThis.fetch, "__piWebAccessSharedProxyFetch"), true); + const before = hits.length; + // A PLAIN model URL — what the host SDK actually calls. The intercept + // rewrites it to the pseudo-origin; the inline link must serve it. + const r = await withTimeout(fetch(`${upstreamOrigin}/v1/messages`, { method: "POST", body: "{}" }), 10_000, "model via chain + 0.46 wrapper"); + assert.equal(r.status, 200); + assert.equal(await r.text(), "inline-ok"); + assert.deepEqual(calls, [`${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`]); + // The real upstream must stay untouched — a DNS-bound pseudo-origin + // leak would never reach it anyway (ENOTFOUND). + assert.equal(hits.length, before); + // The wrapper's hook still ran on the rewritten URL (coexistence, not bypass). + // #6841 rework: the wrapper NEVER sees pseudo-origin traffic now — + // the intercept serves it in-process before any downstream runs, so + // coexistence is the marker handshake, not the hook running on it. + assert.deepEqual(seenByWrapper, []); + // Management URLs take the intercept's control-passthrough branch and + // must land on the inline engine too. + const m = await withTimeout(fetch(`${ie.INLINE_ORIGIN}/__bili/health`), 10_000, "mgmt via chain + 0.46 wrapper"); + assert.equal(m.status, 200); + assert.deepEqual(calls, [`${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`, `${ie.INLINE_ORIGIN}/__bili/health`]); + } finally { + const piInline2 = await import("../src/agent/pi-inline.ts"); + const fc2 = await import("../src/agent/fetch-chain/index.ts"); + const ni2 = await import("../src/agent/native-intercept.ts"); + piInline2._resetInlineTransportForTest(); + fc2.resetFetchChainForTest(); + ni2._resetForTest(); + ie._setEngineFactoryForTest(undefined); + ie._resetInlineEngineForTest(); + } +}); + +test("#6841 rework: proxy-scope-ON pi-web-access wrapper cannot steal inline-origin traffic", async () => { + const ie = await import("../src/inline-engine.ts"); + const ni = await import("../src/agent/native-intercept.ts"); + const fc = await import("../src/agent/fetch-chain/index.ts"); + const piInline = await import("../src/agent/pi-inline.ts"); + ie._setEngineFactoryForTest(async () => ({ + origin: ie.INLINE_ORIGIN, + dispatch: async (): Promise => new Response("inline-ok"), + dispatchHttp: async () => {}, + wsOrigin: Promise.resolve(undefined), + webOrigin: Promise.resolve(undefined), + })); + try { + const model = `${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`; + const mgmt = `${ie.INLINE_ORIGIN}/__bili/health`; + const web = "http://example.test/web-search"; + // Intercept installed first (real arm order), wrapper above it. + ni._resetForTest(); + fc.resetFetchChainForTest(); + piInline._resetInlineTransportForTest(); + piInline.installInlineTransport(); + const state: import("../src/agent/native-intercept.ts").NativeInterceptState = { origin: ie.INLINE_ORIGIN, ready: Promise.resolve(ie.INLINE_ORIGIN) }; + assert.equal(ni.installNativeFetchIntercept(state), true); + const { proxyScope, proxyCalls, seenByWrapper } = installScopeWrapper(); + try { + // Scope ON: model + management traffic must reach the engine; + // the wrapper must see nothing at all. + await proxyScope.run(true, async () => { + assert.equal(await (await withTimeout(fetch(model, { method: "POST", body: "{}" }), 10_000, "model scope-on")).text(), "inline-ok"); + assert.equal(await (await withTimeout(fetch(mgmt), 10_000, "mgmt scope-on")).text(), "inline-ok"); + }); + assert.deepEqual(proxyCalls, []); + assert.deepEqual(seenByWrapper, []); + // Scope OFF: same outcome. + assert.equal(await (await withTimeout(fetch(model, { method: "POST", body: "{}" }), 10_000, "model scope-off")).text(), "inline-ok"); + assert.equal(await (await withTimeout(fetch(mgmt), 10_000, "mgmt scope-off")).text(), "inline-ok"); + assert.deepEqual(proxyCalls, []); + // Negative control: real web traffic with the scope ON still + // goes to the proxy stub, so the wrapper is genuinely active. + await proxyScope.run(true, async () => { + assert.equal(await (await withTimeout(fetch(web), 10_000, "web scope-on")).text(), "proxy-handled"); + }); + assert.deepEqual(proxyCalls, [web]); + } finally { + fc.resetFetchChainForTest(); + } + } finally { + const piInline2 = await import("../src/agent/pi-inline.ts"); + const fc2 = await import("../src/agent/fetch-chain/index.ts"); + const ni2 = await import("../src/agent/native-intercept.ts"); + piInline2._resetInlineTransportForTest(); + fc2.resetFetchChainForTest(); + ni2._resetForTest(); + ie._setEngineFactoryForTest(undefined); + ie._resetInlineEngineForTest(); + } +}); + +test("#6841 rework: inline-origin requests from a stale dispatchDepth context still reach the engine", async () => { + const ie = await import("../src/inline-engine.ts"); + const ni = await import("../src/agent/native-intercept.ts"); + const fc = await import("../src/agent/fetch-chain/index.ts"); + const piInline = await import("../src/agent/pi-inline.ts"); + // The dispatch mints an AsyncResource the #5614 way: created inside the + // fake engine dispatch, it keeps the dispatch's ALS (dispatchDepth 2) + // alive after the dispatch returns. + let stale: AsyncResource | undefined; + ie._setEngineFactoryForTest(async () => ({ + origin: ie.INLINE_ORIGIN, + dispatch: async (): Promise => { + stale = new AsyncResource("stale-scope"); + return new Response("inline-ok"); + }, + dispatchHttp: async () => {}, + wsOrigin: Promise.resolve(undefined), + webOrigin: Promise.resolve(undefined), + })); + try { + const model = `${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`; + ni._resetForTest(); + fc.resetFetchChainForTest(); + piInline._resetInlineTransportForTest(); + piInline.installInlineTransport(); + const state: import("../src/agent/native-intercept.ts").NativeInterceptState = { origin: ie.INLINE_ORIGIN, ready: Promise.resolve(ie.INLINE_ORIGIN) }; + assert.equal(ni.installNativeFetchIntercept(state), true); + const { proxyScope, proxyCalls, seenByWrapper } = installScopeWrapper(); + try { + await withTimeout(fetch(model, { method: "POST", body: "{}" }), 10_000, "seed"); + assert.notEqual(stale, undefined); + // Requests issued from that stale scope, with the proxy scope ON, + // must still reach the engine — not the wrapper, not the anchor. + await proxyScope.run(true, async () => { + await stale!.runInAsyncScope(async () => { + assert.equal(await (await withTimeout(fetch(model, { method: "POST", body: "{}" }), 10_000, "stale model")).text(), "inline-ok"); + assert.equal(await (await withTimeout(fetch(`${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`, { method: "POST", body: "{}" }), 10_000, "stale routed")).text(), "inline-ok"); + }); + }); + assert.deepEqual(proxyCalls, []); + assert.deepEqual(seenByWrapper, []); + } finally { + fc.resetFetchChainForTest(); + } + } finally { + const piInline2 = await import("../src/agent/pi-inline.ts"); + const fc2 = await import("../src/agent/fetch-chain/index.ts"); + const ni2 = await import("../src/agent/native-intercept.ts"); + piInline2._resetInlineTransportForTest(); + fc2.resetFetchChainForTest(); + ni2._resetForTest(); + ie._setEngineFactoryForTest(undefined); + ie._resetInlineEngineForTest(); + } +}); + +test("#6841 rework: inline-origin classification is the exact origin, not a prefix", async () => { + const ie = await import("../src/inline-engine.ts"); + const fc = await import("../src/agent/fetch-chain/index.ts"); + const piInline = await import("../src/agent/pi-inline.ts"); + const calls: string[] = []; + ie._setEngineFactoryForTest(async () => ({ + origin: ie.INLINE_ORIGIN, + dispatch: async (input: string | URL | Request): Promise => { + calls.push(String(input)); + return new Response("inline-ok"); + }, + dispatchHttp: async () => {}, + wsOrigin: Promise.resolve(undefined), + webOrigin: Promise.resolve(undefined), + })); + const originalFetch = globalThis.fetch; + try { + // Canned anchor: anything the link does NOT serve in-process lands + // here, so a misclassification is observable without real DNS. + const anchorCalls: string[] = []; + const canned = (async (input: string | URL | Request, _init?: RequestInit) => { + anchorCalls.push(typeof input === "string" ? input : input instanceof URL ? input.href : (input as Request).url); + return new Response("anchor-ok"); + }) as typeof fetch; + // Define the property DIRECTLY: a plain assignment would fire the + // stale guard accessor left by the previous test, re-chaining the + // canned sink as the downstream of the inherited top — every later + // test would then read "anchor-ok" instead of the real upstream. + // No { anchor } option: the canned sink is only the link's + // downstream (captured from globalThis.fetch at install); the + // module anchor stays the real fetch. + Object.defineProperty(globalThis, "fetch", { value: canned, writable: true, configurable: true, enumerable: true }); + fc.resetFetchChainForTest(); + piInline._resetInlineTransportForTest(); + piInline.installInlineTransport(); + const model = `${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`; + // Exact origin, case-insensitive host, :80 — served in-process. + assert.equal(await (await withTimeout(fetch(model), 10_000, "exact")).text(), "inline-ok"); + assert.equal(await (await withTimeout(fetch("HTTP://BILLION-CONTEXT.INLINE/bili/x"), 10_000, "case")).text(), "inline-ok"); + assert.equal(await (await withTimeout(fetch("http://billion-context.inline:80/bili/x"), 10_000, "port80")).text(), "inline-ok"); + // Lookalike host, https, :81, credentials — NOT the pseudo-origin: + // they pass through to the anchor instead of reaching the engine. + assert.equal(await (await withTimeout(fetch("http://billion-context.inline.evil.com/bili/x"), 10_000, "lookalike")).text(), "anchor-ok"); + assert.equal(await (await withTimeout(fetch("https://billion-context.inline/bili/x"), 10_000, "https")).text(), "anchor-ok"); + assert.equal(await (await withTimeout(fetch("http://billion-context.inline:81/bili/x"), 10_000, "port81")).text(), "anchor-ok"); + assert.equal(await (await withTimeout(fetch("http://user:pass@billion-context.inline/bili/x"), 10_000, "creds")).text(), "anchor-ok"); + assert.deepEqual(calls, [model, "HTTP://BILLION-CONTEXT.INLINE/bili/x", "http://billion-context.inline:80/bili/x"]); + assert.deepEqual(anchorCalls, [ + "http://billion-context.inline.evil.com/bili/x", + "https://billion-context.inline/bili/x", + "http://billion-context.inline:81/bili/x", + "http://user:pass@billion-context.inline/bili/x", + ]); + } finally { + const piInline2 = await import("../src/agent/pi-inline.ts"); + const fc2 = await import("../src/agent/fetch-chain/index.ts"); + piInline2._resetInlineTransportForTest(); + fc2.resetFetchChainForTest(); + // Reset restores the pre-install descriptor (the canned anchor); + // define the previous top back directly, so the guarded setter + // cannot swallow it as an own-chain write-back (see above). + Object.defineProperty(globalThis, "fetch", { value: originalFetch, writable: true, configurable: true, enumerable: true }); + ie._setEngineFactoryForTest(undefined); + ie._resetInlineEngineForTest(); + } +}); + test("#3278 transport: engine failure degrades model traffic direct, management fails loud", async () => { const ie = await import("../src/inline-engine.ts"); ie._setEngineFactoryForTest(async () => { diff --git a/tests/raw-body-walk-depth.test.ts b/tests/raw-body-walk-depth.test.ts index 3357d5277d..6e52446329 100644 --- a/tests/raw-body-walk-depth.test.ts +++ b/tests/raw-body-walk-depth.test.ts @@ -81,6 +81,6 @@ test("shallow plumbing: exclusion set, key inheritance and bucket split stay int test("non-object top-level bodies land whole in the other bucket", () => { const bd = estimateRawBodyBreakdown("plain string body"); - assert.deepEqual(bd, { total: t("plain string body"), messages: 0, tools: 0, system: 0, other: t("plain string body") }); + assert.deepEqual(bd, { total: t("plain string body"), messages: 0, tools: 0, system: 0, other: t("plain string body"), otherTop: [] }); assert.equal(estimateRawBodyTokens("plain string body"), t("plain string body")); }); diff --git a/tests/side-request-isolation.test.ts b/tests/side-request-isolation.test.ts index e14cdc10bd..04a5d508a2 100644 --- a/tests/side-request-isolation.test.ts +++ b/tests/side-request-isolation.test.ts @@ -678,6 +678,22 @@ test("sideRequestGuard: raw-body fit against declared ∩ armed window minus out assert.equal(sideRequestGuard(cjkBody, "anthropic", Math.floor(cjkEst / 1.20), undefined, undefined, 1).blocked, true, "genuinely oversized CJK still blocks"); }); +test("sideRequestGuard: a dual-shape body's foreign field does not false-block (#6828)", () => { + // The #554 guard meters the raw body before forwarding a side request. + // On a dual-shape body the protocol-blind walk counted the foreign + // conversation field too, so the compress call — the very request that + // would shrink the session — false-blocked at the same phantom total. + const cjk = "测".repeat(400_000); + const body = { + messages: [{ role: "user", content: cjk }], + input: [{ type: "message", role: "user", content: cjk }], + }; + const limit = 600_000; + const guard = sideRequestGuard(body, "openai", limit); + assert.ok(guard.estimate < limit * 1.15, `native field alone is under the guard bar (${guard.estimate} < ${Math.round(limit * 1.15)})`); + assert.equal(guard.blocked, false, "the dual-shape body's foreign field must not false-block the side request"); +}); + function okSse(inputTokens: number): string { return ( `event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "m1", role: "assistant", usage: { input_tokens: inputTokens } } })}\n\n` +