diff --git a/docs/configuration.ja.md b/docs/configuration.ja.md index 215a6ffc3e..94ed5ccbfc 100644 --- a/docs/configuration.ja.md +++ b/docs/configuration.ja.md @@ -394,7 +394,7 @@ | `BILI_SESSIONS_DIR` | string | unset (XDG state path) | — | セッションレコードの保存先(パスの再配置)。 | | `BILI_ENCRYPTION_KEY` | string | unset | — | 暗号化された永続化ペイロード用の鍵材料(シークレット)。 | | `BILI_TUNNEL_ALLOWED_HOSTS` | string[] (csv) | unset | — | MITM トンネルレーン用のホスト許可リスト。 | -| `BILI_RECLAIM_FETCH_PATCH` | boolean-ish | on | — | シャットダウン時にグローバル fetch パッチを回収するためのトグル。 | +| `BILI_RECLAIM_FETCH_PATCH` | boolean-ish | on | — | ネイティブモードの fetch 自己修復ガード (#1158) のトグル: `0` で従来の直接インストール (ウォッチドッグなし) を維持する。 | | `BILI_CONFLICT_SCAN` | boolean-ish | off | — | インストールレーンの競合スキャナープローブを有効にする。 | | `BILI_CHAIN_MAX_FUTURE_SKEW_MS` | number | (built-in) | — | 未来にずれたチェーンチェックポイントタイムスタンプの許容範囲。 | | `BILI_CHAIN_RECENT_WINDOW_MS` | number | (built-in) | — | チェーンチェックポイント検証用の最近ウィンドウサイズ。 | @@ -2087,7 +2087,7 @@ ACP ネイティブエージェント(現在は `pi` 拡張機能)は、プ | `BILI_PLUGIN_BYPASS` | x-bili-plugin-bypass パススルー判定のオペレーターオーバーライド (#6220)、リクエストごとに読み取り (再起動不要)。3 状態: **未設定**/`auto` = 現状 (受信ヘッダーがリクエストごとに判定); **`1`/`on`/`true`** = 強制バイパス (すべてのリクエストをそのまま転送 — ツール注入なし、nudge なし、セッションバインドなし); **`0`/`off`/`false`** = 常にバイパスしない (受信 x-bili-plugin-bypass ヘッダーをすべて無視); 識別できない値は `auto` として扱われます。#6220 の owner 承認により環境変数のみです — 設定ファイルへのミラーはありません。 | | `BILI_PREFLIGHT_HOLD_MS` | 長時間のプリフライト圧縮が keep-alive バイトでクライアントを保持し始めるまでの猶予期間 (ms) (デフォルト `30000`; #568 を参照)。 | | `BILI_STREAM_KEEPALIVE_MS` | ストリーミングフェーズのクライアント保持 (#1647): SSE レスポンスがこのミリ秒数にわたってクライアントにゼロバイトしか書き込んでいない場合、bili は 1 行の SSE コメント行 (`: bili-keepalive`、仕様レベルの no-op) を発行し、クライアントの undici `bodyTimeout` (デフォルト 300秒; Node 組み込み fetch はリクエストごとにオーバーライドできません) が、上流がリライター/ストリップパイプを飲み込むピンポンで応答している長時間プリフィルを殺すのを防ぎます。デフォルトは `15000`; `0` で無効にします。`BILI_PREFLIGHT_HOLD_MS` の兄弟で、後者は圧縮プリフライトの沈黙をカバーし、これはストリーミング中の上流起因の沈黙をカバーします。 | -| `BILI_RECLAIM_FETCH_PATCH` | `0` に設定すると、ネイティブモードの fetch 自己修復リアームを無効にします (#1158)。デフォルトでは、ネイティブ fetch インターセプトは `globalThis.fetch` をガード付きアクセサとしてインストールするため、`globalThis.fetch` を再インストールするサードパーティのパッチ (例: dsh-http-proxy の設定リフレッシュが凍結された pre-bili `originalFetch` を書き込む場合) は下流として再チェーンされ、モデルトラフィックは bili を通ってルーティングされ続けます。`0` の場合、従来の直接インストールが残ります: サードパーティのリアームが勝つため、そのセッションで bili はモデルトラフィックを見なくなります。**送信メモ:** ガードが保持されている間、クレームされたモデルトラフィックは bili プロキシ自身によってディスパッチされます — サードパーティチェーンの送信 (例: dsh-http-proxy で設定された SOCKS5 プロキシ; bili 自身のアップストリームプロキシングは HTTP プロキシのみサポート) に乗らなくなります。サードパーティの送信を復元する必要がある場合は、`0` を設定し、bili レベルで送信を設定してください (`"proxy": "http://…"` )。**共存メモ (#3528):** デフォルトのアクセサ形式は、`globalThis.fetch` がプレーンな書き込み可能データプロパティであることを要求するサードパーティコンポーネントと互換性がありません — 彼らはプロパティディスクリプタを読み取り、`value` を見つけず、ロード時に fetch 観測者を中断します (報告済み: DSH Desktop の Developer Tools / NodeJS 診断プラグイン、`@deepseek-ai/dsh-experimental-inspector`)。そのような観測者が従来の直接インストール経由でロードされるように `0` を設定してください (その後ホストを完全に再起動); 上記のトレードオフを受け入れます。#3528 を参照してください。 | +| `BILI_RECLAIM_FETCH_PATCH` | `0` に設定すると、ネイティブモードの fetch 自己修復ガード (#1158、#5959) を無効にします。デフォルトでは、ネイティブ fetch インターセプトは `globalThis.fetch` を**プレーンな書き込み可能データプロパティ**としてインストールします — ディスクリプタに `value` を持つことを要求するサードパーティ製コンポーネントと互換性があります (以前はガード付きアクセサであり、そのようなコンポーネントはロード時に中止していました; 報告済み: DSH Desktop の NodeJS 診断プラグイン `@deepseek-ai/dsh-experimental-inspector`、#3528、本変更により解決) — さらにディスクリプタを毎秒再読み込みする**整合性ウォッチドッグ**を備えます。あらゆる追い出しの形態 — サードパーティの再代入 (例: dsh-http-proxy の設定リフレッシュが凍結された pre-bili `originalFetch` を書き込む)、`DELETE` や `REDEFINE` (#2791)、敵意あるアクセサ — は同じ「下流として採用」ポリシーで 1 tick 以内に修復されるため、モデルトラフィックは bili を通ってルーティングされ続けます。スロットがすべての修復を拒否した場合 (書き込み不可、または書き込みを飲み込むアクセサ)、ガードは 3 回の連続した無効な修復後に諦めてエラーを記録し、スロットが再び利用可能になると自動的に再武装します。`0` の場合、従来の直接インストールが残りウォッチドッグは動きません: サードパーティの再武装が勝ち、そのセッションで bili はモデルトラフィックを見なくなります。**送信メモ:** ガードが保持されている間、クレームされたモデルトラフィックは bili プロキシ自身によってディスパッチされます — サードパーティチェーンの送信 (例: dsh-http-proxy で設定された SOCKS5 プロキシ; bili 自身のアップストリームプロキシングは HTTP プロキシのみサポート) に乗らなくなります。サードパーティの送信を復元する必要がある場合は、`0` を設定し、bili レベルで送信を設定してください (`"proxy": "http://…"` )。#1158、#2791、#3528、#5959 を参照してください。 | | `BILI_RESIGN` | `0` に設定すると、#1884 の再署名アームをエンドツーエンドで展開解除します (修正前の動作: 署名されたボディは通常の書き換えパスに乗って上流で 401 で失敗します)。デフォルト: アーム済み — 再署名可能な (解決可能なクレデンシャルを持つ SDK-HMAC-SHA256) 署名されたモデルリクエストは、すべての送信ボディが再署名されてトンネルされます; このアームは dsh での設定を必要としません (クレデンシャルサービス経由のアカウントプール検出)。再署名できない (解決可能なクレデンシャルがない) BUILT-IN スキームのリクエストは、実行可能なメッセージとともにローカルで 403 で拒否されます — その修正 (クレデンシャルの提供) は実行可能です。検出されたその他のすべてのスキーム (SigV4、`x-ofm-signature` のようなゲートウェイ発明ヘッダー — 検出は形状ベース、#2090) は bili のどこにもクレデンシャルソースがないため、常に拒否されます (バイナリ契約: 再署名+圧縮、または拒否、署名なしでのパススルーは決してない): スキーム名を特定した 403、`resign-pending.json` に記憶され、bili がそのスキームの再署名機能をリリースするまで、すべての起動時に再警告されます (Web UI でも表示)。`BILI_RESIGN_PASSTHROUGH=1` は BUILT-IN スキームのみに対して、圧縮なしの逐語転送をオンにします — その他のすべてのスキームに対しては無効です。`enabled` / `passthrough` / `credentialRef` は [`resign`](#resign) ブロックの下に設定ファイルの双子を持ち、署名スキームをキーとします (`resign["sdk-hmac-sha256"]`; `providers..resign[""]` はレベル 2 のオーバーライド) — 環境変数はファイルより優先されます。スキームキーはパススルーのオプトインを固定します: そのキーが設定されたスキームのみがトンネルされます。関連: `BILI_RESIGN_BENEFIT` (署名付き `maas_type: benefit` ヘッダーが付与される CodeArts ベネフィットモデルのコンマ区切りリスト — 3 層ツリー全体より優先; ファイル側の双子はレベル 3 の `models..benefit` ブール値で、未設定の場合は組み込みの `glm-5.3-flash,deepseek-v4.1-flash` にフォールバックし、dsh codearts プラグインの `CODEARTS_BENEFIT_FALLBACK` を反映) および `BILI_CODEARTS_REF` (有効な `codearts` アカウントを `$DSH_HOME/jet-hub/state.json` から検出する代わりに、再署名に使用される dsh クレデンシャルサービス ref を強制)。 | | `BILI_CONFIG_FILE` | 設定ファイルのパスをオーバーライド (任意の JSON ファイルを指定)。 | | `ACP_PORT` / `PORT` | リスニングポートをオーバーライド。 | diff --git a/docs/configuration.md b/docs/configuration.md index d2bddf9df9..db6919add8 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -397,7 +397,7 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed | `BILI_SESSIONS_DIR` | string | unset (XDG state path) | — | Where session records are stored (path relocation). | | `BILI_ENCRYPTION_KEY` | string | unset | — | Key material for encrypted persisted payloads (secret). | | `BILI_TUNNEL_ALLOWED_HOSTS` | string[] (csv) | unset | — | Host allowlist for the MITM tunnel lane. | -| `BILI_RECLAIM_FETCH_PATCH` | boolean-ish | on | — | Toggle for reclaiming the global fetch patch on shutdown. | +| `BILI_RECLAIM_FETCH_PATCH` | boolean-ish | on | — | Toggle for the native-mode fetch self-heal guard (#1158): `0` keeps the classic direct install (no watchdog). | | `BILI_CONFLICT_SCAN` | boolean-ish | off | — | Enable the install-lane conflict scanner probe. | | `BILI_CHAIN_MAX_FUTURE_SKEW_MS` | number | (built-in) | — | Tolerance for future-skewed chain checkpoint timestamps. | | `BILI_CHAIN_RECENT_WINDOW_MS` | number | (built-in) | — | Recent-window size for chain checkpoint validation. | @@ -2100,7 +2100,7 @@ File keys resolve only when the matching env var is unset. Defaults in parenthes | `BILI_IMAGE_RESCALE_MAX_DIM` | Longest-edge trigger threshold (px) for the egress downscale pass (#3400): an image is a rescale candidate when its longest edge exceeds this value OR its base64 body exceeds 100 KB; fit-inside resizing never upscales. Default `1600`; non-numeric or values below `128` are ignored (default applies). Env-only by design — no config-file mirror. See [`imageRescale`](#imagerescale). | | `BILI_PREFLIGHT_HOLD_MS` | Grace period (ms) before a long preflight compression starts holding the client with keep-alive bytes (default `30000`; see #568). | | `BILI_STREAM_KEEPALIVE_MS` | Streaming-phase client hold (#1647): when an SSE response has written zero bytes to the client for this many milliseconds, bili emits one SSE comment line (`: bili-keepalive`, a spec-level no-op) so the client's undici `bodyTimeout` (default 300s; Node built-in fetch cannot override it per request) cannot kill long prefills whose upstream pings the rewriter/strip pipes swallow. Default `15000`; `0` disables. Sibling of `BILI_PREFLIGHT_HOLD_MS`, which covers compression-preflight silence — this covers upstream-caused silence during streaming. | -| `BILI_RECLAIM_FETCH_PATCH` | Set to `0` to disable the native-mode fetch self-heal re-arm (#1158). By default the native fetch intercept installs `globalThis.fetch` as a guarded accessor, so a third-party patch that re-installs `globalThis.fetch` (e.g. dsh-http-proxy's settings refresh writing its frozen pre-bili `originalFetch`) is re-chained as the downstream and model traffic keeps routing through bili. With `0` the classic direct install stays: a third-party re-arm then wins and bili stops seeing model traffic for the session. **Egress note:** while the guard holds, claimed model traffic is dispatched by the bili proxy itself — it no longer rides the third-party chain's egress (e.g. a SOCKS5 proxy configured in dsh-http-proxy; bili's own upstream proxying supports HTTP proxies only). If you need the third-party egress back, set `0` and configure the egress at bili's level (`"proxy": "http://…"`). **Coexistence note (#3528):** the default accessor form is incompatible with third-party components that require `globalThis.fetch` to be a plain writable data property — they read its property descriptor, find no `value`, and abort their fetch observer at load (reported: DSH Desktop's Developer Tools / NodeJS diagnostic plugin, `@deepseek-ai/dsh-experimental-inspector`). Set `0` (then fully restart the host) so such observers load via the classic direct install; you accept the trade-offs above. See #3528. | +| `BILI_RECLAIM_FETCH_PATCH` | Set to `0` to disable the native-mode fetch self-heal guard (#1158, #5959). By default the native fetch intercept installs `globalThis.fetch` as a **plain writable data property** — compatible with third-party components that require the descriptor to carry a `value` (previously it was a guarded accessor and such components aborted at load; reported: DSH Desktop's NodeJS diagnostic plugin `@deepseek-ai/dsh-experimental-inspector`, #3528, resolved by this change) — plus an **integrity watchdog** that re-reads the descriptor every second. Every eviction shape — a third-party re-assignment (e.g. dsh-http-proxy's settings refresh writing its frozen pre-bili `originalFetch`), a `DELETE` or `REDEFINE` (#2791), or a hostile accessor — is healed within one tick under the same adopt-as-downstream policy, so model traffic keeps routing through bili. If the slot rejects every repair (non-writable, or an accessor that swallows writes) the guard gives up after 3 consecutive unproductive repairs and logs an error, then re-arms itself once the slot becomes usable again. With `0` the classic direct install stays and no watchdog runs: a third-party re-arm then wins and bili stops seeing model traffic for the session. **Egress note:** while the guard holds, claimed model traffic is dispatched by the bili proxy itself — it no longer rides the third-party chain's egress (e.g. a SOCKS5 proxy configured in dsh-http-proxy; bili's own upstream proxying supports HTTP proxies only). If you need the third-party egress back, set `0` and configure the egress at bili's level (`"proxy": "http://…"`). See #1158, #2791, #3528, #5959. | | `BILI_RESIGN` | Set to `0` to un-deploy the #1884 re-sign arm end to end (pre-fix behavior: signed bodies ride the normal rewrite path and fail upstream with 401). Default: armed — a signed model request that can be re-signed (SDK-HMAC-SHA256 with a resolvable credential) tunnels with every egress body re-signed; the arm needs no configuration on dsh (account-pool discovery via the credentials service). A BUILT-IN-scheme request that CANNOT be re-signed (no resolvable credential) is refused locally with 403 and an actionable message — its fix (provide the credential) is actionable. Every OTHER detected scheme (SigV4, gateway-invented headers like `x-ofm-signature` — detection is shape-based, #2090) has no credential source anywhere in bili, so it is ALWAYS refused (binary contract: re-signed+compressed or refused, never passed through unsigned): a 403 naming the scheme, remembered in `resign-pending.json`, and re-warned at every startup (and shown in the web UI) until bili ships the scheme's re-signer. `BILI_RESIGN_PASSTHROUGH=1` turns on verbatim no-compression forwarding for the BUILT-IN scheme only — it is inert for every other scheme. `enabled` / `passthrough` / `credentialRef` have config-file twins under the [`resign`](#resign) block, keyed by signature scheme (`resign["sdk-hmac-sha256"]`; `providers..resign[""]` is the level-2 override) — env vars win over the file. Scheme keys pin the passthrough opt-in: only the scheme whose key sets it tunnels. Related: `BILI_RESIGN_BENEFIT` (comma-separated list of CodeArts benefit models whose requests get the signed `maas_type: benefit` header — wins over the whole three-level tree; file-side twin is the level-3 `models..benefit` boolean, unset falls back to the built-in `glm-5.3-flash,deepseek-v4.1-flash`, mirroring the dsh codearts plugin's `CODEARTS_BENEFIT_FALLBACK`) and `BILI_CODEARTS_REF` (force the dsh credentials-service ref used for re-signing instead of discovering enabled `codearts` accounts from `$DSH_HOME/jet-hub/state.json`). | | `BILI_CONFIG_FILE` | Override the config file path (point at any JSON file). | | `ACP_PORT` / `PORT` | Override the listen port. | diff --git a/docs/configuration.zh-CN.md b/docs/configuration.zh-CN.md index 3ad7b20267..5b8a3210aa 100644 --- a/docs/configuration.zh-CN.md +++ b/docs/configuration.zh-CN.md @@ -397,7 +397,7 @@ | `BILI_SESSIONS_DIR` | string | unset (XDG state path) | — | 会话记录的存放目录(路径重定位)。 | | `BILI_ENCRYPTION_KEY` | string | unset | — | 加密持久化载荷的密钥材料(机密)。 | | `BILI_TUNNEL_ALLOWED_HOSTS` | string[] (csv) | unset | — | MITM 隧道车道的主机白名单。 | -| `BILI_RECLAIM_FETCH_PATCH` | boolean-ish | on | — | 进程退出时回收全局 fetch 补丁的开关。 | +| `BILI_RECLAIM_FETCH_PATCH` | boolean-ish | on | — | native 模式 fetch 自愈守卫(#1158)开关:`0` 保持经典直装(无看门狗)。 | | `BILI_CONFLICT_SCAN` | boolean-ish | off | — | 启用安装车道冲突扫描探针。 | | `BILI_CHAIN_MAX_FUTURE_SKEW_MS` | number | (built-in) | — | 链检查点时间戳未来偏移的容忍度。 | | `BILI_CHAIN_RECENT_WINDOW_MS` | number | (built-in) | — | 链检查点校验的近期窗口大小。 | @@ -2076,7 +2076,7 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 | `BILI_IMAGE_RESCALE_MAX_DIM` | 出站降采样 pass 的最长边触发阈值(像素,#3400):图片最长边超过该值**或** base64 体积超过 100 KB 即成为降采样候选;fit-inside 缩放从不放大。默认 `1600`;非数字或低于 `128` 的取值被忽略(按默认处理)。设计上仅环境变量、无配置文件镜像。详见 [`imageRescale`](#imagerescale)。 | | `BILI_PREFLIGHT_HOLD_MS` | 预压缩超过该宽限期(毫秒)后,代理提前提交响应并用保活字节挂住客户端(默认 `30000`;见 #568)。 | | `BILI_STREAM_KEEPALIVE_MS` | 流式阶段客户端保活(#1647):SSE 响应连续该毫秒数没有向客户端写出任何字节时,bili 发一条 SSE 注释行(`: bili-keepalive`,协议层 no-op),防止客户端 undici `bodyTimeout`(默认 300s;Node 内置 fetch 无法按请求覆盖)在长 prefill 时断连——上游的 ping 注释会被重写器/剥离管道吞掉。默认 `15000`;`0` 关闭。与 `BILI_PREFLIGHT_HOLD_MS` 互补:后者覆盖压缩预检期的静默,本变量覆盖流式期上游导致的静默。 | -| `BILI_RECLAIM_FETCH_PATCH` | 设为 `0` 关闭 native 模式 fetch 自愈重武装(#1158)。默认情况下 native fetch 拦截会把 `globalThis.fetch` 装成受保护的访问器:第三方补丁重新赋值 `globalThis.fetch` 时(如 dsh-http-proxy 的 settings 刷新用冻结的 pre-bili `originalFetch` 盲覆盖),会被接链为下游,模型流量继续经过 bili。设 `0` 则回到经典直装:第三方重装生效,bili 将看不到本会话的模型流量。**出口提示:** 自愈生效期间,被认领的模型流量由 bili 代理自身派发——不再走第三方链的出口(例如 dsh-http-proxy 里配置的 SOCKS5;bili 自身的上游代理仅支持 HTTP 形式)。若需要回退第三方出口,设 `0` 并在 bili 层配置出口(`"proxy": "http://…"`)。**共存提示(#3528):** 默认的访问器形态与要求 `globalThis.fetch` 为普通可写数据属性的第三方组件不兼容——它们读取其属性描述符、发现没有 `value`,便在加载时中止 fetch 观察者(已报告:DSH 桌面「开发者工具 / NodeJS 诊断」插件,`@deepseek-ai/dsh-experimental-inspector`)。设 `0`(然后完全重启宿主)让此类观察者经经典直装正常加载;代价见上文。详见 #3528。 | +| `BILI_RECLAIM_FETCH_PATCH` | 设为 `0` 关闭 native 模式 fetch 自愈守卫(#1158、#5959)。默认情况下 native fetch 拦截把 `globalThis.fetch` 装成**普通可写数据属性**——与要求描述符带 `value` 的第三方组件兼容(此前是受保护访问器,这类组件会在加载时中止;已报告:DSH 桌面「NodeJS 诊断」插件 `@deepseek-ai/dsh-experimental-inspector`,#3528,本次变更已解决)——并附带一个**完整性看门狗**,每 1 秒重读一次该属性描述符。任何驱逐形态——第三方重新赋值(如 dsh-http-proxy 的 settings 刷新用冻结的 pre-bili `originalFetch` 盲覆盖)、`DELETE` 或 `REDEFINE`(#2791)、或敌意访问器——都会在一个 tick 内按同样的「接链为下游」策略修复,模型流量继续经过 bili。若该槽位拒绝一切修复(不可写、或吞掉写入的访问器),守卫在连续 3 次无效修复后放弃并记录 error 日志;槽位恢复可用后自动重新武装。设 `0` 则回到经典直装且不运行看门狗:第三方重装生效,bili 将看不到本会话的模型流量。**出口提示:** 自愈生效期间,被认领的模型流量由 bili 代理自身派发——不再走第三方链的出口(例如 dsh-http-proxy 里配置的 SOCKS5;bili 自身的上游代理仅支持 HTTP 形式)。若需要回退第三方出口,设 `0` 并在 bili 层配置出口(`"proxy": "http://…"`)。详见 #1158、#2791、#3528、#5959。 | | `BILI_RESIGN` | 设为 `0` 整体卸载 #1884 重签臂(回到修复前行为:带签名的请求照常改写、上游 401)。默认开启——可重签的签名请求(SDK-HMAC-SHA256 且能解析出凭据)走隧道,每个出站 body 都重签;重签臂在 dsh 上无需任何配置(经 credentials 服务做账号池发现)。**内置方案**无法重签的请求(解析不出凭据)本地拒收 403 并给出可操作提示——其修复(提供凭据)是可操作的。其他任何被检测到的方案(SigV4、`x-ofm-signature` 等网关自造头——检测是形状判定,#2090)在 bili 内部没有凭据来源也没有重签器,因此**一律本地拒收**(二元契约:重签+压缩或拒绝,绝不无签名放行):403 指明方案名,记入 `resign-pending.json`,每次启动(以及 web UI)反复提醒直到 bili 补上该方案的重签器。`BILI_RESIGN_PASSTHROUGH=1` 仅对内置方案开启原样转发(不压缩)——对其余方案无效。`enabled` / `passthrough` / `credentialRef` 有配置文件孪生项,见 [`resign`](#resign) 块——按签名方案分键(`resign["sdk-hmac-sha256"]`;`providers..resign["<方案>"]` 是二级覆盖)——环境变量优先于文件。方案键把 passthrough 钉死到具体签名:只有自己的键设了的方案才走隧道。相关:`BILI_RESIGN_BENEFIT`(逗号分隔的 CodeArts benefit 模型列表,这些请求附带参与签名的 `maas_type: benefit` 头——优先于整棵三级树;文件侧孪生项是三级的 `models..benefit` 布尔,未设置落到内置 `glm-5.3-flash,deepseek-v4.1-flash`,对齐 dsh codearts 插件的 `CODEARTS_BENEFIT_FALLBACK`)与 `BILI_CODEARTS_REF`(强制指定重签用的 dsh credentials 服务 ref,而不是从 `$DSH_HOME/jet-hub/state.json` 里发现启用的 `codearts` 账号)。 | | `BILI_CONFIG_FILE` | 覆盖配置文件路径(指向任意 JSON 文件;纯名字选择命名配置 `/NAME.json`)。 | | `ACP_PORT` / `PORT` | 覆盖监听端口。 | diff --git a/reference/architecture.md b/reference/architecture.md index f02e4afefa..477af01d9e 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -304,6 +304,7 @@ billion-context/ │ ├── remote-notices.ts # Remote announcement feed watcher: fail-open polling, last-known-good snapshot (#5548) │ ├── request-membership.ts # Settle-side membership probe: is this usage report ours (#4805) │ ├── resolver-wedge.ts # Persistent local DNS resolver wedge detector + alarm (#5221) +│ ├── responses-system-sections.ts # Single source of truth for the Responses-wire developer-section join: steady prepare + round-2 rebuild share one separator (#6922) │ ├── responses-tool-output.ts # Responses tool-output image parts: projection + patch helpers │ ├── rollout.ts # Feature rollout: per-install uuid, basis-point buckets, nested cohorts (#3062) │ ├── runaway-guard.ts # Intrinsic stream terminator for runaway tag/codepoint enumeration (#2346) diff --git a/src/agent/fetch-chain/index.ts b/src/agent/fetch-chain/index.ts index b516b10e1b..176c6b1c24 100644 --- a/src/agent/fetch-chain/index.ts +++ b/src/agent/fetch-chain/index.ts @@ -26,15 +26,15 @@ function shouldReclaimFetchPatch(): boolean { return !/^(0|false|off|no)$/i.test(raw.trim()); } -/** #1158 self-heal: the property descriptor captured before we installed the - * guarded accessor, so _resetForTest can restore a plain writable data +/** #1158 self-heal: the property descriptor captured before we installed our + * chain top, so resetFetchChainForTest can restore the host's original * property. Undefined before the first install in a process. */ let preInstallDesc: PropertyDescriptor | undefined; -/** The accessor descriptor we defined on globalThis.fetch — identity-compared - * by _resetForTest before restoring preInstallDesc, so a third party's legal - * delete/redefine in between (#1410) is never clobbered. */ -let installedDesc: PropertyDescriptor | undefined; +/** The chain-top function we installed on globalThis.fetch — identity-compared + * by resetFetchChainForTest before restoring preInstallDesc, so a third + * party's legal replace/delete/redefine in between (#1410) is never clobbered. */ +let installedTop: typeof globalThis.fetch | undefined; /** #1410 re-anchor ledger: every fetch this process has ever observed at the * top slot, ordered oldest→newest. The oldest entry is the module-load @@ -107,7 +107,40 @@ function nextLiveAnchor(dead: typeof globalThis.fetch): typeof globalThis.fetch return observedFetches.find((f) => !knownDeadFetches.has(f)); } -export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch) => typeof globalThis.fetch): void { +// #5959/#2804: integrity-watchdog state. A plain data property (the +// inspector-compatible form, #3528) has no setter to observe assignments, so +// every eviction shape — third-party ASSIGNMENT (#1158), DELETE or REDEFINE +// (#2791), or a hostile ACCESSOR — is detected by periodically re-reading the +// descriptor and healing with the same adopt-as-downstream policy. One check +// closure per install cycle; resetFetchChainForTest stops it so tests never +// leak timers or stale checks. +let watchdogTimer: ReturnType | undefined; +let watchdogCheck: (() => void) | undefined; +const WATCHDOG_INTERVAL_MS = 1_000; +const WATCHDOG_MAX_FAILURES = 3; + +function stopWatchdog(): void { + if (watchdogTimer !== undefined) { + clearInterval(watchdogTimer); + watchdogTimer = undefined; + } + watchdogCheck = undefined; +} + +/** Test seam: run one integrity check synchronously. Returns false when no + * watchdog is armed (classic install / reclaim disabled / already reset). */ +export function runFetchIntegrityCheckForTest(): boolean { + if (watchdogCheck === undefined) return false; + watchdogCheck(); + return true; +} + +interface FetchChainOptions { + /** Integrity-watchdog tick period in ms (default 1 s). Tests use small values. */ + watchdogIntervalMs?: number; +} + +export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch) => typeof globalThis.fetch, opts?: FetchChainOptions): void { const orig = globalThis.fetch; noteFetch(orig); const makeChain = (downstream: typeof globalThis.fetch) => { @@ -187,14 +220,17 @@ export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch) return chain; }; - // #1158 self-heal re-arm: dsh-http-proxy (0.1.3) re-applies by writing its + // #1158 self-heal: dsh-http-proxy (0.1.3) re-applies by writing its // module-load-time frozen originalFetch over globalThis.fetch, silently // un-routing every model request away from bili while the session keeps - // working (observed live on Windows). Guard the property instead of - // trusting the assignment to survive: any third-party install becomes - // our downstream and model traffic keeps routing through bili. + // working (observed live on Windows). Install the chain top as a plain + // writable DATA property (#5959/#3528: the old guarded accessor broke + // third-party fetch observers that require "value" in the descriptor — + // DSH's experimental-inspector refused to start) and watch the slot: any + // eviction — assignment (#1158), delete/redefine (#2791), hostile accessor + // — is healed within one tick by adopting whatever the evictor left behind + // as downstream, so model traffic keeps routing through bili. const desc = Object.getOwnPropertyDescriptor(globalThis, "fetch"); - let rearmCount = 0; // #2685: true once this install cycle adopted a recognized pi-web-access // wrapper. Its own guard (marker passthrough, #2435) makes the first adopt // the only one, so the FIRST marked install is a supported coexistence @@ -206,54 +242,135 @@ export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch) let top = makeChain(orig); if (guard && shouldReclaimFetchPatch()) { preInstallDesc = desc; - const accessor: PropertyDescriptor = { - configurable: true, - enumerable: desc?.enumerable ?? true, - get: () => top, - set: (v: unknown) => { - if (typeof v !== "function" || v === top) return; - // #1410: our own (possibly stale) chain link written back — - // recognize it by marker and ignore, never spend re-arm - // budget on ourselves. - if (isOwnChain(v)) return; - // #2685: a recognized pi-web-access wrapper is a SUPPORTED - // coexistence install, not an evict — its first adopt is a quiet - // handshake (neutral note, no alarm). Post-#1158-self-heal the - // guard ALWAYS re-adopts, so routing authority never actually - // 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; + globalThis.fetch = top; + installedTop = top; + // Give-up counts CONSECUTIVE UNPRODUCTIVE repairs only (readback + // mismatch / non-writable throw): a lifetime-install counter would + // permanently disable the intercept after sustained coexisting-plugin + // churn (#1662 shape), violating #1158's "routing authority never + // leaves" policy. Healthy ticks and productive heals both reset it. + let failures = 0; + let adoptions = 0; + let givenUp = false; + const giveUpIfExhausted = (): void => { + if (failures >= WATCHDOG_MAX_FAILURES) { + givenUp = true; + logHostEvent("error", "[native-client]", `[bili-native] cannot take back globalThis.fetch after ${failures} consecutive failed repairs (#2791) — the property rejects the re-install (non-writable, or a hostile accessor swallows writes) — intercept offline until the slot is healthy again`); + } + }; + const check = (): void => { + const cur = Object.getOwnPropertyDescriptor(globalThis, "fetch"); + const slotValue: unknown = cur !== undefined && "value" in cur ? cur.value : undefined; + if (slotValue === top || (typeof slotValue === "function" && isOwnChain(slotValue))) { + // Healthy — including #1410's own (possibly stale) chain link + // written back: the data property exposes the physical slot + // (the old getter masked it), so follow the written-back link + // instead of wrapping it; that keeps slot===top without + // spending budget or logging. A healthy slot also RE-ARMS + // after a give-up: the intercept must not stay offline until + // restart when the host later makes the slot usable again. + if (slotValue !== top) { + top = slotValue as typeof globalThis.fetch; + installedTop = top; + } + failures = 0; + if (givenUp) { + givenUp = false; + logHostEvent("info", "[native-client]", "[bili-native] globalThis.fetch is healthy again (#2791) — intercept re-armed after the give-up"); + } + return; + } + // Eviction: foreign value, non-function value, deleted, or accessorized. + // #5959 review: a hostile GETTER can throw on read — swallow it the + // same way the write side below does (an uncaughtException out of + // the timer callback kills the host process). + let current: typeof globalThis.fetch | undefined; + try { + const v: unknown = globalThis.fetch; + if (typeof v === "function") current = v as typeof globalThis.fetch; + } catch { + current = undefined; + } + if (givenUp) { + if (current === undefined) return; // still nothing usable — stay quiet + givenUp = false; // a live fetch reappeared — resume normal repair + } + if (current === top) { + // An accessor masks our own top (get: () => top): convert the + // slot back to a data property WITHOUT re-wrapping — wrapping + // our own top would nest dispatch twice. A NON-configurable + // accessor makes both steps throw; count that as a failed + // repair instead of letting it out of the timer callback — + // an uncaughtException there kills the host process. + try { + try { + globalThis.fetch = top; + } catch { + Object.defineProperty(globalThis, "fetch", { value: top, writable: true, configurable: true, enumerable: true }); + } + failures = globalThis.fetch === top ? 0 : failures + 1; + } catch { + failures += 1; + } + giveUpIfExhausted(); + return; + } + const downstream = current ?? moduleAnchor; + if (downstream === undefined) { + givenUp = true; + logHostEvent("error", "[native-client]", "[bili-native] globalThis.fetch lost the bili install (#2791) and no live fetch remains to re-anchor onto — intercept offline until a usable fetch reappears"); + return; + } + try { // 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 // continues: #1662 showed that surrendering the top slot past - // the limit (`top = v`) left every later foreign install stacking - // on the orphaned chain with ZERO visibility — unbounded growth, - // silent, until the host process died. With the re-entry - // termination in makeChain each further adopt costs O(1) per - // request regardless of history length, so the cap now bounds - // LOGGING only, and routing authority never leaves the guard. + // the limit left every later foreign install stacking on the + // orphaned chain with ZERO visibility — unbounded growth, silent, + // until the host process died. With the re-entry termination in + // makeChain each further adopt costs O(1) per request regardless + // of history length, so the cap bounds LOGGING only, and routing + // authority always returns within one tick. + const webAccess = Reflect.get(downstream, "__piWebAccessProxyFetch") === true; if (webAccess && !webAccessAdopted) { webAccessAdopted = true; logHostEvent("info", "[native-client]", "[bili-native] adopted pi-web-access proxy fetch as downstream (supported coexistence, #2435)"); - } else if (rearmCount < REARM_LIMIT) { - logHostEvent("warn", "[native-client]", `[bili-native] third-party globalThis.fetch install detected (#1158) — re-chaining as downstream (install ${rearmCount + 1})`); + } else if (adoptions < REARM_LIMIT) { + const evictedShape = !(typeof slotValue === "function"); + if (evictedShape) logHostEvent("warn", "[native-client]", `[bili-native] globalThis.fetch lost the bili install (#2791) — re-anchored over the remaining fetch as downstream (repair ${failures + 1})`); + else logHostEvent("warn", "[native-client]", `[bili-native] third-party globalThis.fetch install detected (#1158) — re-chaining as downstream (install ${adoptions + 1})`); + } + noteFetch(downstream); + top = makeChain(downstream); + try { + globalThis.fetch = top; + } catch { + // Getter-only accessor: strict-mode assignment throws; the + // property is still configurable, so redefine it as a data + // property. + Object.defineProperty(globalThis, "fetch", { value: top, writable: true, configurable: true, enumerable: true }); } - rearmCount += 1; - noteFetch(v); - top = makeChain(v as typeof globalThis.fetch); - }, + installedTop = top; + adoptions += 1; + failures = globalThis.fetch === top ? 0 : failures + 1; + } catch { + failures += 1; + } + giveUpIfExhausted(); }; - installedDesc = accessor; - Object.defineProperty(globalThis, "fetch", accessor); + stopWatchdog(); + watchdogCheck = check; + watchdogTimer = setInterval(check, opts?.watchdogIntervalMs ?? WATCHDOG_INTERVAL_MS); + watchdogTimer.unref?.(); } else { // Non-configurable host property or reclaim disabled // (BILI_RECLAIM_FETCH_PATCH=0): keep the classic direct install - // (no guard, the old behavior). #5907: without the guard a - // third-party replacement silently un-routes every model request - // (zero bili.log lines, raw payload to upstream) — name the degraded - // posture once instead of staying silent. + // (no guard, no watchdog — the escape-hatch trade-off stands). + // #5907: without the guard a third-party replacement silently + // un-routes every model request (zero bili.log lines, raw payload + // to upstream) — name the degraded posture once instead of staying + // silent. if (!shouldReclaimFetchPatch()) { logHostEvent("warn", "[native-client]", "[bili-native] BILI_RECLAIM_FETCH_PATCH=0 — the #1158 fetch self-heal is disabled by configuration; a third-party globalThis.fetch replacement will silently un-route model traffic instead of being re-chained (documented degradation, #1158/#5907)"); } else { @@ -264,24 +381,23 @@ export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch) } export function resetFetchChainForTest(opts: { anchor?: typeof globalThis.fetch } = {}): void { + stopWatchdog(); if (opts.anchor !== undefined) moduleAnchor = opts.anchor; observedFetches = moduleAnchor !== undefined ? [moduleAnchor] : []; knownDeadFetches.clear(); warnedReanchor = false; warnedReentry = false; - if (preInstallDesc !== undefined) { - const d = preInstallDesc; - preInstallDesc = undefined; - // #1410: restore ONLY while the property is still ours — it is - // configurable, so a third party may legally have deleted/redefined - // it meanwhile; restoring blindly would clobber their install. The - // comparison must be on the accessor FUNCTIONS, not the descriptor - // object: on the global object V8 rebuilds the descriptor wrapper - // around every set, so object identity is never stable. + const d = preInstallDesc; + const held = installedTop; + preInstallDesc = undefined; + installedTop = undefined; + if (d !== undefined) { + // #1410: restore ONLY while the slot still holds our own top — the + // property is configurable, so a third party may legally have + // replaced/deleted/redefined it meanwhile; restoring blindly would + // clobber their install. const cur = Object.getOwnPropertyDescriptor(globalThis, "fetch"); - if (cur !== undefined && installedDesc !== undefined && cur.get === installedDesc.get && cur.set === installedDesc.set) { - Object.defineProperty(globalThis, "fetch", { ...d, configurable: true }); - } - installedDesc = undefined; + const v: unknown = cur !== undefined && "value" in cur ? cur.value : undefined; + if (v === held) Object.defineProperty(globalThis, "fetch", { ...d, configurable: true }); } } diff --git a/tests/issue2929-codex-fold-double-miss.test.ts b/tests/issue2929-codex-fold-double-miss.test.ts index 81b61ca727..7419629dc1 100644 --- a/tests/issue2929-codex-fold-double-miss.test.ts +++ b/tests/issue2929-codex-fold-double-miss.test.ts @@ -34,16 +34,17 @@ type Item = Record; const SUMMARY_MARKER = "[Compressed conversation section]"; const NUDGE_MARKER = "Compressible ranges ("; -// Indices verified against the kernel's nudge gate with nudgeGrowthTokens=2000 -// and ~9KiB filler turns. Pre-#4521 (master) the injections landed on -// requests {7,8,9}; since #4521 the armed-but-silent status line rides an -// ephemeral carrier on the gated turns and its shown-marker feeds the nudge -// gate back, so the schedule shifts to {4,6,8,9} — the fold/proxy-tool-round -// mechanics under test are unchanged, only this probe's anchor re-calibrates -// to the next nudge-carrying request pair (6 → tool round, 7 → fold round). -const PROXY_TOOL_AT = 6; // upstream request index whose response = acp_status call (round 1) -const FOLD_AT = 7; // upstream request index whose response = compress call (round 2) -// FOLD_AT+1 = post-fold refreshed re-request (round 3); FOLD_AT+2 = next steady client turn. +// The fold turn is materialized SCHEDULE-INDEPENDENTLY: the fake upstream +// arms on the first request carrying the ephemeral nudge (with >=11 tagged +// refs to fold) and folds on the re-request that follows the acp_status +// round. A hardcoded request-index anchor broke every time the nudge gate's +// token schedule shifted — #4521's status-line feedback ({7,8,9} → {4,6,8,9}), +// #6928's status-line removal, and #4313's timestamp-rendering default flip +// (the suite preload pin BILI_MESSAGE_TIMESTAMPS="0" changes the wire shape +// and hence the schedule: OFF {4,13} vs ON {3,5,11,14} — direct local runs +// without the preload diverge from CI). The probe also pins +// compress.messageTimestamps:false so its wire shape is hermetic regardless +// of how the suite is launched. const TOTAL_TURNS = 13; const FILLER = (seed: number, kb: number): string => { @@ -120,6 +121,13 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl try { const captured: Array<{ url: string; body: string }> = []; + // Object (not bare lets) so the outer-scope reads aren't narrowed to + // the initial "idle" — the mutations happen inside the request callback. + const fold = { + state: "idle" as "idle" | "armed" | "folded", + armIdx: -1, // upstream request index that carried the nudge (acp_status round) + foldIdx: -1, // upstream request index that carried the compress call + }; upstream = http.createServer((req, res) => { const chunks: Buffer[] = []; req.on("data", (c: Buffer) => chunks.push(c)); @@ -127,15 +135,21 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl const idx = captured.length; const body = Buffer.concat(chunks).toString("utf8"); captured.push({ url: req.url ?? "", body }); - if (idx === PROXY_TOOL_AT) { - // Round 1 of the fold turn: model asks for proxy status — the - // loop executes it locally and re-requests (round 2). + const input = (JSON.parse(body) as { input?: Item[] }).input ?? []; + if (fold.state === "idle" && hasNudge(input) && refsOf(input).length >= 11) { + // Round 1 of the fold turn: the first nudge-carrying request + // with enough refs — model asks for proxy status, the loop + // executes it locally and re-requests (round 2). + fold.state = "armed"; + fold.armIdx = idx; functionCallSse(res, `resp_${idx}`, "acp_status", "{}", `call_st_${idx}`); return; } - if (idx === FOLD_AT) { - const prev = (JSON.parse(captured[idx - 1]!.body) as { input?: Item[] }).input ?? []; - const uniq = refsOf(prev); + if (fold.state === "armed") { + // Round 2: the re-request after the acp_status round — fold. + fold.state = "folded"; + fold.foldIdx = idx; + const uniq = refsOf(input); assert.ok(uniq.length >= 11, `fold round: expected >=11 tagged refs, saw ${uniq.length}`); const args = JSON.stringify({ content: [ { startId: uniq[3]!, endId: uniq[5]!, topic: "F1A", summary: `F1A summary: ${FILLER(1, 1)}` }, @@ -144,10 +158,9 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl functionCallSse(res, `resp_${idx}`, "compress", args, `call_cmp_${idx}`); return; } - const parsed = JSON.parse(body) as { input?: Item[] }; let label = "0"; - for (let i = (parsed.input ?? []).length - 1; i >= 0; i--) { - const it = parsed.input![i]!; + for (let i = input.length - 1; i >= 0; i--) { + const it = input[i]!; if (it.type === "message" && it.role === "user" && typeof it.content === "string") { const m = (it.content as string).match(/Turn (\d+):/); if (m) { label = m[1]!; break; } @@ -169,7 +182,7 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: 200_000 } } } }, modelContextLimit: 200_000, kernelConfig: defaultConfig(200_000), - compress: { injectTool: true, injectNudge: true, nudgeGrowthTokens: 2000 }, + compress: { injectTool: true, injectNudge: true, nudgeGrowthTokens: 2000, messageTimestamps: false }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: true, @@ -223,17 +236,18 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl } const inputs = captured.map((c) => (JSON.parse(c.body) as { input?: Item[] }).input ?? []); - assert.ok(inputs.length >= FOLD_AT + 3, `expected >= ${FOLD_AT + 3} upstream requests, got ${inputs.length}`); + assert.ok(fold.state === "folded", `scenario miss: the nudge never fired on a request with >=11 refs — probe cannot materialize the #2929 trigger; adjust nudgeGrowthTokens/filler`); + assert.ok(inputs.length >= fold.foldIdx + 3, `expected >= ${fold.foldIdx + 3} upstream requests, got ${inputs.length}`); // (0) Scenario materialization: the nudge must have been injected into - // the PRE-FOLD original request (production shape: nudge armed -> model + // the arming (pre-fold) request (production shape: nudge armed -> model // calls proxy tool -> compress). Without it this probe does not cover // the #2929 trigger. - assert.ok(hasNudge(inputs[PROXY_TOOL_AT]!), `scenario miss: no ephemeral nudge in the pre-fold request (inputs[${PROXY_TOOL_AT}]) — probe does not cover the production trigger; adjust nudgeGrowthTokens/filler`); - console.log(`probe: nudge confirmed in pre-fold request; total upstream requests=${inputs.length}`); + assert.ok(hasNudge(inputs[fold.armIdx]!), `scenario miss: no ephemeral nudge in the arming request (inputs[${fold.armIdx}]) — probe does not cover the production trigger`); + console.log(`probe: nudge at inputs[${fold.armIdx}], fold at inputs[${fold.foldIdx}]; total upstream requests=${inputs.length}`); // (1) The post-fold re-request carries the in-place summary(s). - const rPost = inputs[FOLD_AT + 1]!; + const rPost = inputs[fold.foldIdx + 1]!; const postSums = summaryIndices(rPost); assert.ok(postSums.length >= 1, "post-fold re-request must carry the in-place summary"); @@ -241,7 +255,7 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl // the post-fold re-request up to and including the summary anchor. In // production this is where the chain breaks (next turn hits only the // instructions baseline). - const next = inputs[FOLD_AT + 2]!; + const next = inputs[fold.foldIdx + 2]!; const nextSums = summaryIndices(next); assert.ok(nextSums.length >= 1, `next steady turn lost the summary entirely (summaries: ${nextSums.length}) — fold did not survive to the client turn`); assert.equal(nextSums[0], postSums[0], `summary anchor drifted: re-request item[${postSums[0]}] vs next turn item[${nextSums[0]}]`); diff --git a/tests/issue4516-pi-direct-fallback.test.ts b/tests/issue4516-pi-direct-fallback.test.ts index 26dba6ed49..96b9a6884d 100644 --- a/tests/issue4516-pi-direct-fallback.test.ts +++ b/tests/issue4516-pi-direct-fallback.test.ts @@ -3,6 +3,7 @@ import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { after, test } from "node:test"; +import { runFetchIntegrityCheckForTest } from "../src/agent/fetch-chain/index.js"; // #4516: the gateless lanes (pi/omp/opencode-V1) had NO size judgment on their // degraded direct-fallback path — a 1.06M-token raw history rode straight to @@ -266,6 +267,11 @@ test("#4516 dead-proxy wording: after holding an origin, the refusal says it DIE }) as typeof fetch; const { sink, result } = await withFallback(async ({ fetch, rearm }) => { rearm(dyingFetch); + // #5959 data-property install: the write is no longer laundered into a + // fresh wrapper synchronously — the integrity watchdog adopts the + // evictor as downstream within one tick. Drive that tick here so the + // call below deterministically enters through the intercept. + assert.equal(runFetchIntegrityCheckForTest(), true, "integrity watchdog must be armed"); const res = await fetch()(UPSTREAM, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin-context-window": "500" }, diff --git a/tests/native-intercept-selfheal-nonconfig.test.ts b/tests/native-intercept-selfheal-nonconfig.test.ts new file mode 100644 index 0000000000..491d635660 --- /dev/null +++ b/tests/native-intercept-selfheal-nonconfig.test.ts @@ -0,0 +1,60 @@ +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { after, test } from "node:test"; +import { installNativeFetchIntercept } from "../src/agent/native-intercept.js"; +import { runFetchIntegrityCheckForTest } from "../src/agent/fetch-chain/index.js"; + +// #5959 review finding: a NON-configurable accessor masking our own top made +// the watchdog's defineProperty fallback throw out of the timer callback → +// uncaughtException → host process death. Non-configurability is permanent +// for the process lifetime (no later test could restore the slot), so this +// regression lives in its own file — node --test runs each file in its own +// process. Diagnostics route to the shared bili.log (#2827); keep this suite +// hermetic like its sibling. +const STATE_ROOT = fs.mkdtempSync(path.join(os.tmpdir(), "bili-selfheal-nonconfig-")); +const PREV_XDG_STATE_HOME = process.env.XDG_STATE_HOME; +process.env.XDG_STATE_HOME = STATE_ROOT; +const CHAIN_LOG_FILE = path.join(STATE_ROOT, "billion-context", "bili.log"); +after(() => { + if (PREV_XDG_STATE_HOME === undefined) delete process.env.XDG_STATE_HOME; + else process.env.XDG_STATE_HOME = PREV_XDG_STATE_HOME; + fs.rmSync(STATE_ROOT, { recursive: true, force: true }); +}); + +function fakeFetch(sink: string[]) { + return (async (input: string | URL | Request, _init?: RequestInit) => { + sink.push(typeof input === "string" ? input : input instanceof URL ? input.href : (input as Request).url); + return new Response("{}", { status: 200 }); + }) as typeof fetch; +} + +test("#5959: a non-configurable accessor masking our own top cannot crash the watchdog", async () => { + const nativeSink: string[] = []; + const nativeFetch = fakeFetch(nativeSink); + const { _resetForTest } = await import("../src/agent/native-intercept.js"); + _resetForTest(); + globalThis.fetch = nativeFetch; + try { + fs.mkdirSync(path.dirname(CHAIN_LOG_FILE), { recursive: true }); + fs.writeFileSync(CHAIN_LOG_FILE, ""); + assert.equal(installNativeFetchIntercept({ origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }), true); + assert.equal(runFetchIntegrityCheckForTest(), true, "integrity watchdog must be armed"); + const top = globalThis.fetch; + // The evictor freezes the slot: an accessor that hands back our own top + // but can never be redefined again. + Object.defineProperty(globalThis, "fetch", { get: () => top, configurable: false }); + // Pre-fix each of these threw TypeError("Cannot redefine property: fetch") + // out of the timer callback (uncaughtException → host death). + assert.doesNotThrow(() => runFetchIntegrityCheckForTest()); + assert.doesNotThrow(() => runFetchIntegrityCheckForTest()); + // Third consecutive unproductive repair → loud give-up, not a crash. + assert.doesNotThrow(() => runFetchIntegrityCheckForTest()); + const lines = fs.readFileSync(CHAIN_LOG_FILE, "utf8").split("\n").filter((l) => l.length > 0); + assert.ok(lines.some((l) => l.includes("[error]") && l.includes("cannot take back globalThis.fetch")), + `expected a give-up error line, got: ${lines.join(" | ")}`); + } finally { + _resetForTest(); + } +}); diff --git a/tests/native-intercept-selfheal.test.ts b/tests/native-intercept-selfheal.test.ts index 155ecc3652..1984a29551 100644 --- a/tests/native-intercept-selfheal.test.ts +++ b/tests/native-intercept-selfheal.test.ts @@ -5,6 +5,7 @@ import os from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { installNativeFetchIntercept, type NativeInterceptState } from "../src/agent/native-intercept.js"; +import { installFetchChain, resetFetchChainForTest, runFetchIntegrityCheckForTest } from "../src/agent/fetch-chain/index.js"; // #2827: the fetch-chain diagnostics now land in the shared bili.log instead // of the terminal. Keep this whole suite hermetic: point XDG_STATE_HOME at a @@ -27,6 +28,13 @@ function fakeFetch(sink: string[]) { }) as typeof fetch; } +/** #5959: drive one integrity-watchdog check synchronously. The data-property + * guard heals evictions on the next tick instead of in a setter, so suites + * that simulate a third-party write steer the seam explicitly. */ +function tick(): void { + assert.equal(runFetchIntegrityCheckForTest(), true, "integrity watchdog must be armed"); +} + async function withHeal(fn: (ctx: { sink: string[]; rearm: (v: typeof fetch) => void; fetch: () => typeof fetch }) => Promise): Promise<{ sink: string[]; result: T }> { const saved = globalThis.fetch; const sink: string[] = []; @@ -78,6 +86,7 @@ test("#1158 self-heal: third-party reset to a frozen bare fetch re-chains and ke const bareSink: string[] = []; const { sink } = await withHeal(async ({ rearm, fetch }) => { rearm(fakeFetch(bareSink)); + tick(); // the data-property guard adopts the evictor on the next check const res = await fetch()("http://127.0.0.1:8199/v1/messages", { method: "POST" }); assert.equal(res.status, 200); }); @@ -93,6 +102,7 @@ test("#1158 self-heal: third-party wrapper becomes the downstream (dsh-http-prox return fakeFetch([])(input, init); }) as typeof globalThis.fetch; rearm(wrapper); + tick(); // adopt the wrapper as downstream before the next request const res = await fetch()("http://127.0.0.1:8199/v1/messages"); assert.equal(res.status, 200); // Non-model URL still passes through to the third-party wrapper untouched. @@ -107,21 +117,31 @@ test("#1158 self-heal: guarded property is transparent when nobody fights it", a const { sink } = await withHeal(async ({ fetch }) => { const res = await fetch()("http://127.0.0.1:8199/v1/messages"); assert.equal(res.status, 200); - // A re-read of the global yields a stable callable (accessor works). + // A healthy slot survives a check untouched, and a re-read of the + // global yields a stable callable (data property works). + tick(); + assert.equal(fetch(), globalThis.fetch); assert.equal(typeof fetch(), "function"); }); assert.deepEqual(sink, ["http://127.0.0.1:40001/bili/http://127.0.0.1:8199/v1/messages"]); }); -test("#1158 self-heal: non-function and self writes are ignored by the guard", async () => { - const { sink } = await withHeal(async ({ rearm, fetch }) => { - rearm(undefined as unknown as typeof globalThis.fetch); - const mine = fetch(); - rearm(mine); +test("#1158 self-heal: a non-function write heals onto the module anchor; an own-link write-back resyncs quietly", async () => { + const nativeSink: string[] = []; + const nativeFetch = fakeFetch(nativeSink); + await withScope(nativeFetch, async () => { + installNativeFetchIntercept({ origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }); + globalThis.fetch = undefined as unknown as typeof globalThis.fetch; + tick(); // heals: nothing usable in the slot → re-anchor on the module anchor + assert.equal(typeof globalThis.fetch, "function"); + const mine = globalThis.fetch; + globalThis.fetch = mine; // our own link written back — resync, no repair + tick(); + assert.equal(globalThis.fetch, mine); const res = await mine("http://127.0.0.1:8199/v1/messages"); assert.equal(res.status, 200); }); - assert.deepEqual(sink, ["http://127.0.0.1:40001/bili/http://127.0.0.1:8199/v1/messages"]); + assert.deepEqual(nativeSink, ["http://127.0.0.1:40001/bili/http://127.0.0.1:8199/v1/messages"]); }); // #1410: a coexisting network-scope plugin (dsh-codex-subscription shape) @@ -178,6 +198,7 @@ test("#1410: install inside a foreign network scope survives scope teardown (re- await globalThis.fetch("http://127.0.0.1:8199/v1/messages"); assert.deepEqual(nativeSink, ["http://127.0.0.1:40001/bili/http://127.0.0.1:8199/v1/messages"]); scope.close(); // its guard sees bili's chain on top → skips restore, nulls its locals + tick(); // healthy slot — the check stays quiet across the teardown const res = await globalThis.fetch("http://127.0.0.1:8199/v1/messages"); assert.equal(res.status, 200); await globalThis.fetch("https://registry.npmjs.org/billion-context"); @@ -195,8 +216,9 @@ test("#1410: foreign scope opening AFTER install — re-arm adopts its wrapper; await withScope(nativeFetch, async ({ scope }) => { const state: NativeInterceptState = { origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }; assert.equal(installNativeFetchIntercept(state), true); - scope.open(); // captures bili's top as its base; the write re-arms (#1158) onto its wrapper - scope.close(); // guard fails (bili's chain on top) → wrapper torn down behind our back + scope.open(); // captures bili's top as its base; the write evicts bili's top from the slot + scope.close(); // sees its own wrapper on top → restores bili's top; the wrapper is dropped + tick(); // healthy slot — the check stays quiet across the teardown const res = await globalThis.fetch("http://127.0.0.1:8199/v1/messages"); assert.equal(res.status, 200); await globalThis.fetch("https://registry.npmjs.org/billion-context"); @@ -207,22 +229,27 @@ test("#1410: foreign scope opening AFTER install — re-arm adopts its wrapper; ]); }); -test("#1410: writing back our OWN stale chain link is recognized as ours — no re-arm, top unchanged", async () => { +test("#1410: writing back our OWN stale chain link resyncs top to it (no wrap, no budget)", async () => { + const nativeSink: string[] = []; const foreignSink: string[] = []; - const { sink } = await withHeal(async ({ rearm, fetch }) => { - const link1 = fetch(); - const foreign = fakeFetch(foreignSink); - rearm(foreign); // genuine third-party evict → new top - const topAfter = fetch(); + const nativeFetch = fakeFetch(nativeSink); + await withScope(nativeFetch, async () => { + installNativeFetchIntercept({ origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }); + const link1 = globalThis.fetch; // T1 (ds = module anchor) + globalThis.fetch = fakeFetch(foreignSink); // genuine third-party evict + tick(); // adopts the evictor → new top over foreign + const topAfter = globalThis.fetch; assert.notEqual(topAfter, link1); - rearm(link1); // our own stale link written back — must be ignored - assert.equal(fetch(), topAfter); - const res = await fetch()("http://127.0.0.1:8199/v1/messages"); + globalThis.fetch = link1; // our own stale link written back to the slot + tick(); // resync: top follows the written-back link instead of wrapping it + assert.equal(globalThis.fetch, link1); + const res = await link1("http://127.0.0.1:8199/v1/messages"); assert.equal(res.status, 200); }); - // Routing still flows through the third-party downstream adopted at re-arm. - assert.deepEqual(foreignSink, ["http://127.0.0.1:40001/bili/http://127.0.0.1:8199/v1/messages"]); - assert.deepEqual(sink, []); + // Routing follows the written-back link's downstream (the module anchor), + // not the evictor that was adopted in between. + assert.deepEqual(nativeSink, ["http://127.0.0.1:40001/bili/http://127.0.0.1:8199/v1/messages"]); + assert.deepEqual(foreignSink, []); }); test("#1410: _resetForTest leaves a third-party redefined descriptor alone", async () => { @@ -278,8 +305,10 @@ test("#1158 escape hatch: BILI_RECLAIM_FETCH_PATCH=0 keeps the classic direct in const state: NativeInterceptState = { origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }; try { assert.equal(installNativeFetchIntercept(state), true); - // A plain writable data property again (no accessor guard). + // A plain writable data property again (no accessor guard), and no + // watchdog armed — the escape hatch is a full opt-out of self-heal. assert.equal(Object.getOwnPropertyDescriptor(globalThis, "fetch")?.writable, true); + assert.equal(runFetchIntegrityCheckForTest(), false, "escape hatch arms no watchdog"); const thirdParty = fakeFetch([]); globalThis.fetch = thirdParty; // The third party wins: direct send, no bili rewrite. @@ -305,7 +334,7 @@ test("#1158 escape hatch: BILI_RECLAIM_FETCH_PATCH=0 keeps the classic direct in // "RangeError: Maximum call stack size exceeded". The re-entry termination // must keep request depth constant no matter how many wraps happened. -test("#1662: thousands of foreign re-wraps of the current top cannot grow request depth", async () => { +test("#1662: thousands of foreign re-wraps, drained tick-by-tick, cannot grow request depth", async () => { const nativeSink: string[] = []; const nativeFetch = fakeFetch(nativeSink); const saved = globalThis.fetch; @@ -316,13 +345,23 @@ test("#1662: thousands of foreign re-wraps of the current top cannot grow reques const state: NativeInterceptState = { origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }; assert.equal(installNativeFetchIntercept(state), true); // 16000 sits well past the measured overflow threshold (~4-8K cycles - // depending on Node/platform stack limits); the fixed code's cost is - // O(1) per request regardless of history length. + // depending on Node/platform stack limits). The wraps land one per + // operation (the #1662 dsh-codex-subscription shape) and the watchdog + // drains each one as it lands — interleave a tick per wrap to model + // that cadence across time. A single synchronous burst of ≥~10K wraps + // inside one watchdog period (≤1 s) is an adversarial/host-crash + // shape the data-property slot no longer masks (the old accessor's + // setter laundered every write through a fresh top); see the #5959 + // PR notes. for (let i = 0; i < 16000; i++) { const base = globalThis.fetch; const wrapped = (async (input: string | URL | Request, init?: RequestInit) => base(input, init)) as typeof fetch; globalThis.fetch = wrapped; + tick(); // drain this wrap as it lands (per-operation cadence) } + const settled = globalThis.fetch; + tick(); // quiescent: idle checks must not accumulate further layers + assert.equal(globalThis.fetch, settled); const res = await globalThis.fetch("http://127.0.0.1:8199/v1/messages", { method: "POST" }); assert.equal(res.status, 200); await globalThis.fetch("https://registry.npmjs.org/billion-context"); @@ -379,6 +418,7 @@ test("#1662: a steady-state foreign wrapper wrapping bili's chain stays in the c return base(input, init); }) as typeof fetch; globalThis.fetch = wrapped; + tick(); // adopt the wrapper as downstream before the next request const res = await globalThis.fetch("http://127.0.0.1:8199/v1/messages", { method: "POST" }); assert.equal(res.status, 200); } finally { @@ -431,6 +471,7 @@ for (const first of ["bili", "web-access"]) { onDispatch: (_url, action) => { decisions.push(action); }, }); web.install(); + tick(); // adopt the marked wrapper (or confirm a healthy slot) before asserting on the top const top = globalThis.fetch; assert.equal(Reflect.get(top, "__piWebAccessProxyFetch"), true); for (let i = 0; i < 1000; i++) { @@ -462,11 +503,14 @@ test("pi-web-access: preserve an older proxy wrapper and still truncate unknown return base(input, init); }) as typeof fetch; } + tick(); // adopt the topmost unknown wrap so the proxy wraps a bili link, not the raw history const web = webAccessProxy(); web.install(); + tick(); // adopt the marked proxy wrapper await web.run(async () => { const base = globalThis.fetch; globalThis.fetch = (async (input: string | URL | Request, init?: RequestInit) => base(input, init)) as typeof fetch; + tick(); // adopt the transient write before it is read back const response = await globalThis.fetch("https://example.invalid/article"); assert.equal(await response.text(), "via-web-proxy"); assert.equal(web.installs, 1); @@ -562,6 +606,7 @@ test("#2685: bili-first — supported first web-access install is quietly adopte }); const web = webAccessProxy(); web.install(); + tick(); // the supported first adopt fires on the check, not in a setter assert.equal(web.installs, 1); assert.equal(await web.run(() => globalThis.fetch("https://example.invalid/article")).then(r => r.text()), "via-web-proxy"); await globalThis.fetch("https://api.example.invalid/v1/messages", { method: "POST" }); @@ -603,6 +648,7 @@ test("#2685: unknown third-party install still warns, with neutral 'install N' w await withScope(fakeFetch(nativeSink), async () => { installNativeFetchIntercept({ origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }); globalThis.fetch = fakeFetch([]); + tick(); // the warn fires on the check that adopts the write }); }); assert.equal(warns.length, 0); @@ -628,7 +674,9 @@ test("#2685: a SECOND marked install still warns — the compat marker never bla return w; }; globalThis.fetch = mark(globalThis.fetch); // first marked → quiet adopt note + tick(); globalThis.fetch = mark(globalThis.fetch); // second marked → must warn + tick(); }); }); assert.equal(warns.length, 0); @@ -650,8 +698,9 @@ test("#2827: first web-access adoption writes nothing to the host terminal and r await withScope(fakeFetch(nativeSink), async () => { installNativeFetchIntercept({ origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }); const web = webAccessProxy(); - // First proxy use installs the wrapper → the adopt event fires here. + // First proxy use installs the wrapper → the adopt event fires on the check. assert.equal(await web.run(() => globalThis.fetch("https://example.invalid/article")).then(r => r.text()), "via-web-proxy"); + tick(); // Repeat ordinary proxy-scope calls must not re-emit anything. for (let i = 0; i < 3; i++) { assert.equal(await web.run(() => globalThis.fetch("https://example.invalid/article")).then(r => r.text()), "via-web-proxy"); @@ -666,6 +715,128 @@ 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"]); }); +// #5959: the guarded install used to be a getter/setter accessor, which the +// DSH experimental-inspector's fetch observer refuses ("globalThis.fetch is an +// accessor and cannot be observed safely"). The install is now a plain +// writable data property; evictions (delete / redefine / third-party writes) +// are healed by the integrity watchdog on its next tick instead of in a setter. + +test("#5959: default install is a plain writable data property — the DSH inspector guard passes verbatim", async () => { + const nativeSink: string[] = []; + const nativeFetch = fakeFetch(nativeSink); + await withScope(nativeFetch, async () => { + installNativeFetchIntercept({ origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }); + // Verbatim from @deepseek-ai/dsh-experimental-inspector (network.js installFetchObserver): + const descriptor = Object.getOwnPropertyDescriptor(globalThis, "fetch"); + const original = globalThis.fetch; + if (typeof original !== "function") throw new Error("inspector: globalThis.fetch is unavailable"); + if (descriptor !== void 0 && !("value" in descriptor)) + throw new Error("inspector: globalThis.fetch is an accessor and cannot be observed safely"); + assert.ok(descriptor !== undefined); + if (!("value" in descriptor)) throw new Error("expected a data property descriptor"); + assert.equal(descriptor.writable, true); + assert.equal(descriptor.configurable, true); + assert.equal(descriptor.enumerable, true); + }); +}); + +test("#5959: delete and redefine evictions heal within one tick (#2791 shapes)", async () => { + const nativeSink: string[] = []; + const foreignSink: string[] = []; + const nativeFetch = fakeFetch(nativeSink); + await withScope(nativeFetch, async () => { + installNativeFetchIntercept({ origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }); + const installed = globalThis.fetch; + Reflect.deleteProperty(globalThis, "fetch"); // eviction shape 1: the slot vanishes + tick(); + assert.equal(typeof globalThis.fetch, "function"); + assert.notEqual(globalThis.fetch, installed); // re-anchored over the remaining fetch + const foreign = fakeFetch(foreignSink); + // Eviction shape 2: a getter-only accessor — a strict-mode plain assign + // throws, so the heal must fall back to a configurable redefine. + Object.defineProperty(globalThis, "fetch", { get: () => foreign, configurable: true, enumerable: true }); + tick(); + const res = await globalThis.fetch("http://127.0.0.1:8199/v1/messages", { method: "POST" }); + assert.equal(res.status, 200); + }); + // The evictor becomes the adopted downstream (#1158 policy), not bypassed. + assert.deepEqual(foreignSink, ["http://127.0.0.1:40001/bili/http://127.0.0.1:8199/v1/messages"]); + assert.deepEqual(nativeSink, []); +}); + +test("#5959: a hostile accessor that swallows every write gives up loudly — and re-arms when the slot heals", async () => { + const saved = globalThis.fetch; + const foreignSink: string[] = []; + const foreign = fakeFetch(foreignSink); + let ours: typeof globalThis.fetch | undefined; + const { _resetForTest } = await import("../src/agent/native-intercept.js"); + Object.defineProperty(globalThis, "fetch", { value: foreign, writable: true, configurable: true, enumerable: true }); + try { + const { lines } = await withDiagnosticsCapture(async () => { + installNativeFetchIntercept({ origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }); + ours = globalThis.fetch; // our chain link, before the hostile takeover + assert.ok(ours !== undefined); + Object.defineProperty(globalThis, "fetch", { + get: () => foreign, + set: () => { /* swallows every write — the readback never matches */ }, + configurable: true, + enumerable: true, + }); + for (let i = 0; i < 3; i++) tick(); // WATCHDOG_MAX_FAILURES consecutive unproductive repairs + tick(); // the slot still resolves to a function → givenUp clears; the retry fails again and re-triggers the give-up (failures keeps counting) + assert.equal(globalThis.fetch, foreign); // the slot still resolves through the hostile getter + // The hostile plugin stands down: the slot holds our link again → recovery. + Object.defineProperty(globalThis, "fetch", { value: ours as typeof globalThis.fetch, writable: true, configurable: true, enumerable: true }); + tick(); + assert.equal(globalThis.fetch, ours); + }); + assert.ok(lines.some(l => l.includes("[error]") && l.includes("[native-client]") && l.includes("cannot take back globalThis.fetch")), + `expected a give-up error line, got: ${lines.join(" | ")}`); + assert.ok(lines.some(l => l.includes("[info]") && l.includes("[native-client]") && l.includes("re-armed after the give-up")), + `expected a recovery info line, got: ${lines.join(" | ")}`); + } finally { + // A plain restore assignment would be swallowed by the hostile setter — + // force the slot back to a data property first (the leak that broke #D/#E). + Object.defineProperty(globalThis, "fetch", { value: saved, writable: true, configurable: true, enumerable: true }); + _resetForTest(); + } +}); + +test("#5959 review: a throwing getter eviction cannot escape the watchdog timer", async () => { + const saved = globalThis.fetch; + const preSink: string[] = []; + const preFake = fakeFetch(preSink); + const { _resetForTest } = await import("../src/agent/native-intercept.js"); + Object.defineProperty(globalThis, "fetch", { value: preFake, writable: true, configurable: true, enumerable: true }); + _resetForTest({ anchor: preFake }); // deterministic heal target below + try { + const { lines } = await withDiagnosticsCapture(async () => { + installNativeFetchIntercept({ origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }); + const installed = globalThis.fetch; + // Hostile shape beyond the swallowing setter: the GETTER itself throws. + Object.defineProperty(globalThis, "fetch", { + get: () => { throw new TypeError("fetch observer disabled"); }, + configurable: true, + enumerable: true, + }); + // Pre-fix the throw escaped the check into the setInterval callback + // (uncaughtException → host process death). + assert.doesNotThrow(() => tick()); + const d = Object.getOwnPropertyDescriptor(globalThis, "fetch"); + assert.ok(d !== undefined && "value" in d, "the heal converts the slot back to a data property"); + assert.notEqual(globalThis.fetch, installed); // re-anchored over the surviving fetch + const res = await globalThis.fetch("http://127.0.0.1:8199/v1/messages", { method: "POST" }); + assert.equal(res.status, 200); // the healed chain routes model traffic + }); + assert.deepEqual(preSink, ["http://127.0.0.1:40001/bili/http://127.0.0.1:8199/v1/messages"]); + assert.ok(lines.some(l => l.includes("[warn]") && l.includes("lost the bili install (#2791)")), + `expected the eviction warn line, got: ${lines.join(" | ")}`); + } finally { + Object.defineProperty(globalThis, "fetch", { value: saved, writable: true, configurable: true, enumerable: true }); + _resetForTest(); + } +}); + // #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 @@ -698,6 +869,52 @@ test("#5907: BILI_RECLAIM_FETCH_PATCH=0 keeps classic install and logs the disab } }); +test("#5959: idle ticks never accumulate layers — at most one per external wrap", async () => { + const nativeSink: string[] = []; + const nativeFetch = fakeFetch(nativeSink); + await withScope(nativeFetch, async () => { + installNativeFetchIntercept({ origin: "http://127.0.0.1:40001", ready: Promise.resolve("http://127.0.0.1:40001") }); + for (let i = 0; i < 10; i++) { + const base = globalThis.fetch; + const wrapped = (async (input: string | URL | Request, init?: RequestInit) => base(input, init)) as typeof fetch; + globalThis.fetch = wrapped; + tick(); // adopt this one wrap + const settled = globalThis.fetch; + tick(); // an idle check must not re-adopt or nest another layer + assert.equal(globalThis.fetch, settled); + } + const res = await globalThis.fetch("http://127.0.0.1:8199/v1/messages", { method: "POST" }); + assert.equal(res.status, 200); + }); + assert.deepEqual(nativeSink, ["http://127.0.0.1:40001/bili/http://127.0.0.1:8199/v1/messages"]); +}); + +test("#5959: the real watchdog timer drives the check (eviction heals without a manual tick)", async () => { + const saved = globalThis.fetch; + const nativeSink: string[] = []; + const foreignSink: string[] = []; + const nativeFetch = fakeFetch(nativeSink); + resetFetchChainForTest({ anchor: nativeFetch }); + globalThis.fetch = nativeFetch; + try { + installFetchChain((send) => async (input, init) => { + const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url; + return url.includes("/v1/messages") ? send(`http://127.0.0.1:40001/bili/${url}`, init) : send(input, init); + }, { watchdogIntervalMs: 5 }); + const foreign = fakeFetch(foreignSink); + globalThis.fetch = foreign; + await new Promise((resolve) => setTimeout(resolve, 100)); // ~20 watchdog periods + assert.notEqual(globalThis.fetch, foreign, "the timer must have healed the eviction"); + const res = await globalThis.fetch("http://127.0.0.1:8199/v1/messages", { method: "POST" }); + assert.equal(res.status, 200); + } finally { + globalThis.fetch = saved; + resetFetchChainForTest(); + } + assert.deepEqual(foreignSink, ["http://127.0.0.1:40001/bili/http://127.0.0.1:8199/v1/messages"]); + assert.deepEqual(nativeSink, []); +}); + test("#5907: non-configurable globalThis.fetch degrades to classic install with a loud one-time warning", async () => { // NOTE: this test permanently locks globalThis.fetch (configurable:false is // irreversible within a process). node --test runs each file in its own diff --git a/website/config-reference/env-only.yaml b/website/config-reference/env-only.yaml index b34c86ce13..0fbd23abda 100644 --- a/website/config-reference/env-only.yaml +++ b/website/config-reference/env-only.yaml @@ -125,9 +125,9 @@ entries: kind: env-only audience: user group: envonly - desc_en: "Toggle for reclaiming the global fetch patch on shutdown." - desc_zh: "进程退出时回收全局 fetch 补丁的开关。" - desc_ja: "シャットダウン時にグローバル fetch パッチを回収するためのトグル。" + desc_en: "Toggle for the native-mode fetch self-heal guard (#1158): `0` keeps the classic direct install (no watchdog)." + desc_zh: "native 模式 fetch 自愈守卫(#1158)开关:`0` 保持经典直装(无看门狗)。" + desc_ja: "ネイティブモードの fetch 自己修復ガード (#1158) のトグル: `0` で従来の直接インストール (ウォッチドッグなし) を維持する。" - key: BILI_CONFLICT_SCAN type: boolean-ish default: "off"