From 3b572b102b95d4c575dbcc3dcb4c27f2d12d5dae Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 9 Nov 2026 16:32:38 +0800 Subject: [PATCH 01/11] fix(#5959): install globalThis.fetch as a plain data property; heal evictions via a 1 s integrity watchdog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The guarded accessor (#1158) broke third-party observers that require a value-carrying descriptor — DSH's experimental-inspector aborted at load (#3528). The default guarded install now writes a plain writable data property and arms a watchdog that re-reads the descriptor every second, healing every eviction shape (third-party assignment #1158, delete/redefine #2791, hostile accessors) under the same adopt-as-downstream policy. Give-up counts consecutive unproductive repairs only (max 3), re-arms automatically when the slot becomes usable again, and logs durably through logHostEvent. BILI_RECLAIM_FETCH_PATCH=0 keeps the classic direct install with no watchdog. --- src/agent/fetch-chain/index.ts | 215 ++++++++++++++++------ tests/native-intercept-selfheal.test.ts | 234 +++++++++++++++++++++--- 2 files changed, 365 insertions(+), 84 deletions(-) diff --git a/src/agent/fetch-chain/index.ts b/src/agent/fetch-chain/index.ts index 722b559254..4c8c6b2b05 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,74 +242,137 @@ 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. + const current = typeof globalThis.fetch === "function" ? globalThis.fetch : 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. + try { + globalThis.fetch = top; + } catch { + Object.defineProperty(globalThis, "fetch", { value: top, writable: true, configurable: true, enumerable: true }); + } + failures = globalThis.fetch === top ? 0 : 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})`); } - rearmCount += 1; - noteFetch(v); - top = makeChain(v as typeof globalThis.fetch); - }, + 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 }); + } + 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). + // (no guard, no watchdog — the escape-hatch trade-off stands). globalThis.fetch = top; } } 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/native-intercept-selfheal.test.ts b/tests/native-intercept-selfheal.test.ts index 7fe68d785f..eeda538bf4 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[] = []; @@ -54,6 +62,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); }); @@ -69,6 +78,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. @@ -83,21 +93,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) @@ -154,6 +174,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"); @@ -171,8 +192,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"); @@ -183,22 +205,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 () => { @@ -254,8 +281,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. @@ -281,7 +310,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; @@ -292,13 +321,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"); @@ -355,6 +394,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 { @@ -407,6 +447,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++) { @@ -438,11 +479,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); @@ -538,6 +582,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" }); @@ -579,6 +624,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); @@ -604,7 +650,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); @@ -626,8 +674,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"); @@ -641,3 +690,136 @@ test("#2827: first web-access adoption writes nothing to the host terminal and r 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"]); }); + +// #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(); // given up — further checks are quiet no-ops + 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: 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, []); +}); From 790860ada7a901c7b2e3ccfe30ee65ddd3fa158f Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 9 Nov 2026 16:32:50 +0800 Subject: [PATCH 02/11] =?UTF-8?q?docs(#5959):=20rewrite=20BILI=5FRECLAIM?= =?UTF-8?q?=5FFETCH=5FPATCH=20entries=20=E2=80=94=20data-property=20instal?= =?UTF-8?q?l=20+=20integrity=20watchdog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The env-var entries (en+zh) and table rows still described the retired accessor form and the stale 'reclaim on shutdown' wording. Now state the plain writable data property default, the 1 s watchdog and its heal/give-up/re-arm policy, and that the #3528 inspector coexistence conflict is resolved. --- docs/configuration.md | 4 ++-- docs/configuration.zh-CN.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 1ff02035a2..396ef1bf3d 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -387,7 +387,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. | @@ -2022,7 +2022,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 da7397d93d..6a78c31ab1 100644 --- a/docs/configuration.zh-CN.md +++ b/docs/configuration.zh-CN.md @@ -387,7 +387,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) | — | 链检查点校验的近期窗口大小。 | @@ -1998,7 +1998,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 文件)。 | | `ACP_PORT` / `PORT` | 覆盖监听端口。 | From 7fd23aa8d4884f068f5260c03dd56fdef6e490b0 Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 9 Nov 2026 17:03:06 +0800 Subject: [PATCH 03/11] docs(#5959): move BILI_RECLAIM_FETCH_PATCH table wording into the gen-config-docs seed The param-ref table rows are generated artifacts (docs-sync lane runs tools/gen-config-docs.mjs generate + git diff --exit-code). The wording rewritten in the previous commit landed hand-edited inside the generated block, so CI regenerated the stale seed text and flagged drift. Edit the seed instead; regenerated output is now byte-identical to the committed docs. --- website/config-reference/env-only.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/config-reference/env-only.yaml b/website/config-reference/env-only.yaml index bcf15edb15..848dc3428c 100644 --- a/website/config-reference/env-only.yaml +++ b/website/config-reference/env-only.yaml @@ -112,8 +112,8 @@ entries: kind: env-only audience: user group: envonly - desc_en: "Toggle for reclaiming the global fetch patch on shutdown." - desc_zh: "进程退出时回收全局 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` 保持经典直装(无看门狗)。" - key: BILI_CONFLICT_SCAN type: boolean-ish default: "off" From 7ba8cafc9c880c15312969df1ddb265fd3a696ff Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 9 Nov 2026 17:28:10 +0800 Subject: [PATCH 04/11] fix(fetch-chain): make the integrity watchdog survive non-configurable slot shapes (#5959) Review finding: in the own-top branch, the defineProperty fallback ran OUTSIDE any try/catch. When the slot is a non-configurable accessor masking our own top, both the strict-mode assign and the redefine throw, and the TypeError escaped the setInterval callback -> uncaughtException -> host process death (reproduced: 'Cannot redefine property: fetch'). Wrap the repair like the sibling adopt branch does and count a thrown repair as an unproductive failure (give-up ladder applies). Adds a regression test in its own file because non-configurability is permanent for the process lifetime. Also corrects a factually wrong comment in the hostile-accessor give-up test (a tick whose slot still resolves to a function clears givenUp and retries; it is not a quiet no-op). --- src/agent/fetch-chain/index.ts | 15 +++-- ...ative-intercept-selfheal-nonconfig.test.ts | 60 +++++++++++++++++++ tests/native-intercept-selfheal.test.ts | 2 +- 3 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 tests/native-intercept-selfheal-nonconfig.test.ts diff --git a/src/agent/fetch-chain/index.ts b/src/agent/fetch-chain/index.ts index 4c8c6b2b05..aa0b5df310 100644 --- a/src/agent/fetch-chain/index.ts +++ b/src/agent/fetch-chain/index.ts @@ -289,13 +289,20 @@ export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch) 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. + // 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 { - globalThis.fetch = top; + 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 { - Object.defineProperty(globalThis, "fetch", { value: top, writable: true, configurable: true, enumerable: true }); + failures += 1; } - failures = globalThis.fetch === top ? 0 : failures + 1; giveUpIfExhausted(); return; } 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 eeda538bf4..db8d554ed9 100644 --- a/tests/native-intercept-selfheal.test.ts +++ b/tests/native-intercept-selfheal.test.ts @@ -759,7 +759,7 @@ test("#5959: a hostile accessor that swallows every write gives up loudly — an enumerable: true, }); for (let i = 0; i < 3; i++) tick(); // WATCHDOG_MAX_FAILURES consecutive unproductive repairs - tick(); // given up — further checks are quiet no-ops + tick(); // the slot still resolves to a function → givenUp clears and the repair retries (counter restarts) 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 }); From 8f6d359906a70be069c4e7f579400c4b910c7874 Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 9 Nov 2026 17:28:10 +0800 Subject: [PATCH 05/11] docs(config): move the BILI_RECLAIM_FETCH_PATCH description into the env-only seed (#5959) The PR's docs commit hand-edited the generated index row without updating website/config-reference/env-only.yaml, so the docs-sync drift guard (tools/gen-config-docs.mjs generate + git diff --exit-code) went red. The seed is the single source: with the new desc_en/desc_zh in place, regeneration reproduces the committed configuration.md/zh-CN rows byte-identically. --- website/config-reference/env-only.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/config-reference/env-only.yaml b/website/config-reference/env-only.yaml index bcf15edb15..848dc3428c 100644 --- a/website/config-reference/env-only.yaml +++ b/website/config-reference/env-only.yaml @@ -112,8 +112,8 @@ entries: kind: env-only audience: user group: envonly - desc_en: "Toggle for reclaiming the global fetch patch on shutdown." - desc_zh: "进程退出时回收全局 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` 保持经典直装(无看门狗)。" - key: BILI_CONFLICT_SCAN type: boolean-ish default: "off" From c8bb6abc55e3327ffeda454adb2163057a8128fa Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 9 Nov 2026 17:55:30 +0800 Subject: [PATCH 06/11] test(#5959): drive the integrity tick in the #4516 dead-proxy wording test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The data-property install (#5959) no longer launders a post-install globalThis.fetch write into a fresh wrapper synchronously — the old accessor's setter did that instantly, the new design re-adopts the evictor as downstream within one watchdog tick (disclosed behavior change: assignment-heal instant -> <=1 s). This test rearms a dying fetch and immediately calls through globalThis.fetch, so under the new timing the immediate read sees the raw third-party function and the TypeError escapes instead of being converted to the 422 refusal whose wording the test pins. Drive one deterministic tick after the re-arm so the call enters through the intercept; the pinned mechanism (dead-proxy refusal wording, empty sink) is unchanged. --- tests/issue4516-pi-direct-fallback.test.ts | 6 ++++++ 1 file changed, 6 insertions(+) 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" }, From 0b78bdeb680769b8e592ff5df825df977b220bd9 Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 10 Nov 2026 14:37:25 +0800 Subject: [PATCH 07/11] fix: keep a throwing getter out of the watchdog's eviction read (#5959 review) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The integrity check read globalThis.fetch bare (typeof check invokes the getter). A hostile third-party GETTER that throws escaped the setInterval callback as an uncaughtException and killed the host process — exactly the failure class the write side of the same check already guards against. Swallow the read (current = undefined → re-anchor onto the module anchor, or stay quiet while given up); a configurable throwing getter now self-heals within one tick via the existing assign→defineProperty fallback. Pinned by a new regression test that fails on the pre-fix code. --- src/agent/fetch-chain/index.ts | 11 +++++++- tests/native-intercept-selfheal.test.ts | 35 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/agent/fetch-chain/index.ts b/src/agent/fetch-chain/index.ts index 9dfd052579..176c6b1c24 100644 --- a/src/agent/fetch-chain/index.ts +++ b/src/agent/fetch-chain/index.ts @@ -281,7 +281,16 @@ export function installFetchChain(makeDispatch: (send: typeof globalThis.fetch) return; } // Eviction: foreign value, non-function value, deleted, or accessorized. - const current = typeof globalThis.fetch === "function" ? globalThis.fetch : undefined; + // #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 diff --git a/tests/native-intercept-selfheal.test.ts b/tests/native-intercept-selfheal.test.ts index 2bd6d5570b..054b007a3d 100644 --- a/tests/native-intercept-selfheal.test.ts +++ b/tests/native-intercept-selfheal.test.ts @@ -778,6 +778,41 @@ test("#5959: a hostile accessor that swallows every write gives up loudly — an } }); +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 From b512c3b27e77f41dff6ab5a131098676aba7d8b7 Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 10 Nov 2026 14:40:06 +0800 Subject: [PATCH 08/11] fix: correct the give-up retry comment in the hostile-accessor selfheal test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The old comment claimed the failure counter restarts after a given-up repair retry; it does not — failures keeps counting (3→4→...) and the give-up error re-fires every tick while the hostile getter persists. --- tests/native-intercept-selfheal.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/native-intercept-selfheal.test.ts b/tests/native-intercept-selfheal.test.ts index 054b007a3d..267aa76e79 100644 --- a/tests/native-intercept-selfheal.test.ts +++ b/tests/native-intercept-selfheal.test.ts @@ -759,7 +759,7 @@ test("#5959: a hostile accessor that swallows every write gives up loudly — an 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 and the repair retries (counter restarts) + 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 }); From 8ed56be997c292584844ab8b482c04f8fe77256f Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 16 Nov 2026 20:42:39 +0800 Subject: [PATCH 09/11] docs(architecture): add src/responses-system-sections.ts to the module map (#6922) --- reference/architecture.md | 1 + 1 file changed, 1 insertion(+) diff --git a/reference/architecture.md b/reference/architecture.md index 484cd70bf1..9226ec7713 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -303,6 +303,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) From 245ecb1a9e5161ba97fa0b09865078025c9a2c8f Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 16 Nov 2026 22:51:46 +0800 Subject: [PATCH 10/11] fix(test): recalibrate #2929 probe anchor to nudge positions {3,5,11,14} MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The probe anchor PROXY_TOOL_AT=6 was calibrated on 2026-10-31 for #4521 (schedule {4,6,8,9}). The nudge scheduling has shifted again — nudge now lands at {3,5,11,14}. Re-calibrate the anchor to the next nudge-carrying request pair (5 → tool round, 6 → fold round). The fold/proxy-tool-round mechanics under test are unchanged. Closes: master-red on test (windows-latest, 22, 3) and (ubuntu-latest, 22, 1). --- tests/issue2929-codex-fold-double-miss.test.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/issue2929-codex-fold-double-miss.test.ts b/tests/issue2929-codex-fold-double-miss.test.ts index 81b61ca727..83ee857801 100644 --- a/tests/issue2929-codex-fold-double-miss.test.ts +++ b/tests/issue2929-codex-fold-double-miss.test.ts @@ -38,11 +38,12 @@ const NUDGE_MARKER = "Compressible ranges ("; // 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) +// gate back, so the schedule shifts — the fold/proxy-tool-round mechanics +// under test are unchanged, only this probe's anchor re-calibrates to the +// next nudge-carrying request pair (5 → tool round, 6 → fold round). +// Re-calibrated 2026-11-16: nudge now lands at {3,5,11,14} (was {4,6,8,9}). +const PROXY_TOOL_AT = 5; // upstream request index whose response = acp_status call (round 1) +const FOLD_AT = 6; // 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. const TOTAL_TURNS = 13; From 7247410976508f4dedf42afb89450c2041141c0b Mon Sep 17 00:00:00 2001 From: ework-agent Date: Tue, 17 Nov 2026 01:36:34 +0800 Subject: [PATCH 11/11] test(#2929): make fold probe schedule-independent + hermetic wire shape MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The probe's hardcoded request-index anchors (PROXY_TOOL_AT/FOLD_AT) broke every time the nudge gate's schedule shifted. The last recalibration (245ecb1a9) was measured on a direct local run (timestamps ON by default since #4313), but CI runs the suite preload pin BILI_MESSAGE_TIMESTAMPS="0" (timestamps OFF) — a different wire shape, a different schedule (no nudge at index 5) — so it passed locally and failed on every CI shard. Root-cause fix instead of another anchor whack-a-mole: - Arm the fold on the first request carrying the ephemeral nudge with >=11 tagged refs; fold on the re-request after the acp_status round. The probe now materializes the #2929 scenario wherever the schedule places it. - Pin compress.messageTimestamps:false so the probe's wire shape is hermetic regardless of how the suite is launched (matches the CI preload pin). Supersedes the 245ecb1a9 recalibration. Verified: passes with and without the preload pin, same deterministic schedule (nudge at inputs[7], fold at inputs[8]). --- .../issue2929-codex-fold-double-miss.test.ts | 67 +++++++++++-------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/tests/issue2929-codex-fold-double-miss.test.ts b/tests/issue2929-codex-fold-double-miss.test.ts index 83ee857801..7419629dc1 100644 --- a/tests/issue2929-codex-fold-double-miss.test.ts +++ b/tests/issue2929-codex-fold-double-miss.test.ts @@ -34,17 +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 — the fold/proxy-tool-round mechanics -// under test are unchanged, only this probe's anchor re-calibrates to the -// next nudge-carrying request pair (5 → tool round, 6 → fold round). -// Re-calibrated 2026-11-16: nudge now lands at {3,5,11,14} (was {4,6,8,9}). -const PROXY_TOOL_AT = 5; // upstream request index whose response = acp_status call (round 1) -const FOLD_AT = 6; // 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 => { @@ -121,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)); @@ -128,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)}` }, @@ -145,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; } @@ -170,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, @@ -224,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"); @@ -242,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]}]`);