diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index daecb744c2..4c37e12889 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -2272,35 +2272,11 @@ export async function apply(ctx: PluginContext): Promise { } return false; }; - // #2924 direction 2: `threw` is an INTERNAL error in the host's - // attribution lookup (a disposed/closing agent scope), never evidence of - // a legitimate agentless lane — and the scope often settles within a tick - // or two. Give it a bounded settle-retry before deciding: claiming the - // request after recovery is the only path that RESTORES an over-window - // session (the host's own compaction and the #1117 direct fallback must - // both ship the full history upstream, which 400s once it exceeds the - // window — all recovery paths vanish the instant the gate lets go). - const settleThrewAttribution = async (message: string, url: string, body: { bytes: number } | undefined, init: RequestInit | undefined): Promise => { - // #3581: after giveUpStreak consecutive requests whose settle-retry - // exhausted STILL threw, stop re-settling — a disposed ctx stays - // disposed for the host's whole lifetime (#3578), so retrying the same - // dead scope per request is pure latency. The gate's single cheap - // attributionOf probe per request keeps self-heal alive: the first ok - // claim (new agent scope) clears the streak and resumes normal retries. - if (threwStreak < threwRetry.giveUpStreak) { - for (let attempt = 1; attempt < threwRetry.attempts; attempt++) { - await new Promise((resolve) => { - setTimeout(resolve, threwRetry.delayMs); - }); - const again = attributionOf(ctx); - if (again.state === "ok") { - persistClientEvent(`bili-native-dsh: takeover gate attribution recovered after ${attempt} settle-retr${attempt === 1 ? "y" : "ies"} — ${gateKeyOf(url)} claimed (#2924)`); - noteAttrRecovery(); - return true; - } - if (again.state === "none") return refuseUnattributed(url, again, body, init); - } - } + // #4841: the threw-state verdict once settle-retry is done OR deliberately + // skipped — shared by settleThrewAttribution's exhausted tail and the gate's + // no-init sync fast path below, so both shapes judge oversize identically + // (one doom call, one event wording). + const threwExhaustedVerdict = (message: string, url: string, body: { bytes: number } | undefined, init: RequestInit | undefined): GateVerdict => { // #2924 direction 1: still `threw` AND the body cannot fit — fail // this one request locally instead of handing upstream a request // that can only 400 (and, on a >window history, kills the session). @@ -2332,6 +2308,37 @@ export async function apply(ctx: PluginContext): Promise { } return refuse(url, attributionOf(ctx), body); }; + // #2924 direction 2: `threw` is an INTERNAL error in the host's + // attribution lookup (a disposed/closing agent scope), never evidence of + // a legitimate agentless lane — and the scope often settles within a tick + // or two. Give it a bounded settle-retry before deciding: claiming the + // request after recovery is the only path that RESTORES an over-window + // session (the host's own compaction and the #1117 direct fallback must + // both ship the full history upstream, which 400s once it exceeds the + // window — all recovery paths vanish the instant the gate lets go). + const settleThrewAttribution = async (message: string, url: string, body: { bytes: number } | undefined, init: RequestInit | undefined): Promise => { + // #3581: after giveUpStreak consecutive requests whose settle-retry + // exhausted STILL threw, stop re-settling — a disposed ctx stays + // disposed for the host's whole lifetime (#3578), so retrying the same + // dead scope per request is pure latency. The gate's single cheap + // attributionOf probe per request keeps self-heal alive: the first ok + // claim (new agent scope) clears the streak and resumes normal retries. + if (threwStreak < threwRetry.giveUpStreak) { + for (let attempt = 1; attempt < threwRetry.attempts; attempt++) { + await new Promise((resolve) => { + setTimeout(resolve, threwRetry.delayMs); + }); + const again = attributionOf(ctx); + if (again.state === "ok") { + persistClientEvent(`bili-native-dsh: takeover gate attribution recovered after ${attempt} settle-retr${attempt === 1 ? "y" : "ies"} — ${gateKeyOf(url)} claimed (#2924)`); + noteAttrRecovery(); + return true; + } + if (again.state === "none") return refuseUnattributed(url, again, body, init); + } + } + return threwExhaustedVerdict(message, url, body, init); + }; // #4068/#4079: `none` keeps #1117 direct semantics by design — an // agentless lane is not ours to manage — but a body whose CALIBRATED @@ -2408,6 +2415,19 @@ export async function apply(ctx: PluginContext): Promise { }); } if (attr.state === "threw") { + // #4841: the WS-lane consumer (native-ws-intercept.ts) compares its + // gate result with === false WITHOUT awaiting — a Promise verdict + // silently loses the refusal and the connection gets rewritten onto + // the proxy. No-init (WS-shaped) calls therefore get the sync + // verdict class: exactly the exhausted-settle tail (without init + // there is no body to decode or doom-check, and the settle-retry's + // tick-scale recovery is unreachable synchronously — the top-of-gate + // probe re-runs on every call, so self-heal is not lost). Streak + // untouched: this path attempts no settle, so it consumes no + // give-up budget. + if (init === undefined) { + return threwExhaustedVerdict(attr.message, url, body, init); + } return settleThrewAttribution(attr.message, url, body, init).then((verdict) => { if (verdict === true) { threwStreak = 0; diff --git a/src/agent/native-intercept.ts b/src/agent/native-intercept.ts index eb900339c1..155ac33789 100644 --- a/src/agent/native-intercept.ts +++ b/src/agent/native-intercept.ts @@ -160,7 +160,14 @@ export interface NativeInterceptState { * recoverable refusal for a body that provably cannot fit the model's * window; the fetch lane answers it with a protocol-shaped 422 instead * of an upstream 400. The optional body size lets the gate make that - * call without re-reading the request. */ + * call without re-reading the request. + * Every lane AWAITs the verdict (the fetch lane here, the WS lane inside + * its deferred window, #5838), so a Promise is honored in either case. + * dsh still answers no-init (WS-shaped) calls with a NON-Promise verdict + * (lower latency: skips the settle-retry round-trip; self-heal survives + * because the top-of-gate probe re-runs on every call) — the none arm is + * sync by construction, the threw arm via its no-init fast path. Pinned + * by tests/dsh-native.test.ts. */ takeoverGate?: (url: string, body?: { bytes: number }, init?: RequestInit) => GateVerdict | Promise; /** How long a pre-ready model request waits for the bootstrap before * falling back to a direct (uncompressed) send. */ diff --git a/tests/dsh-native.test.ts b/tests/dsh-native.test.ts index 1c9a6148dc..d660c5ba53 100644 --- a/tests/dsh-native.test.ts +++ b/tests/dsh-native.test.ts @@ -2097,7 +2097,9 @@ test("#2924 gate `threw` attribution: bounded settle-retry recovers and CLAIMS t if (throws <= 2) throw new Error("agent initiator scope is disposed"); return { session: { id: "s2924a" } }; }; - assert.equal(await gate("https://api.gate-2924a.test/v1/chat/completions"), true, "recovered attribution claims the request"); + // #4841: init-carrying call — the fetch-lane (HTTP) shape; the + // no-init shape is the WS shape and now settles synchronously. + assert.equal(await gate("https://api.gate-2924a.test/v1/chat/completions", { bytes: 16 }, {}), true, "recovered attribution claims the request"); assert.equal(throws, 3, "initial throw + exactly two settle-retries"); const content = fs.existsSync(logFile) ? fs.readFileSync(logFile, "utf8") : ""; // #3651: non-public upstream hosts are masked in the durable line (paste-safe sink). @@ -2112,6 +2114,58 @@ test("#2924 gate `threw` attribution: bounded settle-retry recovers and CLAIMS t } }); +test("#4841 gate `threw` no-init (WS-shaped) call: sync verdict class; init keeps the async settle-retry", async () => { + const proxy = await startMockProxy([]); + const stateHome = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-4841-state-")); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-4841-home-")); + try { + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: proxy.origin, XDG_STATE_HOME: stateHome }, async () => { + _resetRegisterForTest(proxy.origin); + const ctx = mockCtx(); + apply(ctx); + await waitFor(() => ctx.registeredTools.length === 1, "initial attach tool registration"); + const gate = _stateTakeoverGateForTest(); + assert.ok(gate !== undefined, "takeoverGate installed"); + _setThrewRetryForTest({ attempts: 2, delayMs: 1 }); + const logFile = path.join(stateHome, "billion-context", "bili.log"); + const refusedLines = (): string[] => (fs.existsSync(logFile) ? fs.readFileSync(logFile, "utf8") : "").split("\n").filter((l) => l.length > 0 && l.includes("takeover gate refused")); + const origCI = ctx.agents.currentInitiator; + ctx.agents.currentInitiator = () => { + throw new Error("agent initiator scope is disposed"); + }; + const url = "https://api.gate-4841.test/v1/chat/completions"; + // THE pin (#4841): the WS-lane consumer compares its gate result + // with === false WITHOUT awaiting — the no-init (WS-shaped) threw + // verdict must be synchronously false, not a Promise resolving to + // false later (a Promise never matches and the refusal was lost). + const raw = gate(url); + assert.equal(raw, false, "no-init threw verdict is synchronously false (pre-await)"); + assert.equal(refusedLines().length, 1, "sync refusal logs the standard once-per-endpoint line"); + assert.match(refusedLines()[0]!, /currentInitiator\(\) threw \(agent initiator scope is disposed\)/); + assert.equal(_attributionHealthForTest().threwStreak, 0, "fast path attempts no settle → consumes no give-up budget"); + // negative: WITH init the verdict stays a Promise — the settle-retry + // path is untouched for HTTP-shaped calls. + const withInit = gate(url, { bytes: 16 }, {}); + assert.ok(withInit instanceof Promise, "init-present call keeps the async settle-retry path"); + assert.equal(await withInit, false, "settle-retry still refuses a persistently thrown scope"); + assert.equal(_attributionHealthForTest().threwStreak, 1, "only the settled call consumed settle budget"); + assert.equal(refusedLines().length, 1, "same endpoint, same state → count continues silently"); + // recovery: the top-of-gate probe claims again on ok attribution — + // self-heal is not lost by skipping settle on the sync path. + ctx.agents.currentInitiator = origCI; + ctx.setInitiator({ session: { id: "s4841" } }); + assert.equal(gate(url), true, "recovered attribution claims again"); + assert.equal(gate(url), true, "…and the no-init shape too (ok arm is sync in every case)"); + }); + } finally { + _setThrewRetryForTest(); + proxy.close(); + rmrf(stateHome); + rmrf(home); + _resetRegisterForTest(undefined); + } +}); + test("#2924 gate `threw` persists: provably-oversized body refuses LOCALLY, everything else keeps #1117 semantics", async () => { const proxy = await startMockProxy([]); const stateHome = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-2924b-state-")); @@ -2488,16 +2542,17 @@ test("#3581 gate degradation reports event-driven; give-up latch stops pointless // initial attributionOf, the one settle-retry, the oversize arm's // window resolution (sessionModelTargetOf), and the final // refuse(attributionOf) re-probe → 4. The latch removes exactly the - // settle-retry probe → 3. - assert.equal(await gate(url), false); + // settle-retry probe → 3. #4841: calls are init-carrying (the + // fetch-lane shape) — the no-init shape now settles synchronously. + assert.equal(await gate(url, { bytes: 16 }, {}), false); assert.equal(probes, 4, "request 1: four probes while settling"); - assert.equal(await gate(url), false); + assert.equal(await gate(url, { bytes: 16 }, {}), false); assert.equal(probes, 8, "request 2: still settling"); - assert.equal(await gate(url), false); + assert.equal(await gate(url, { bytes: 16 }, {}), false); assert.equal(probes, 12, "request 3: streak reaches giveUpStreak → latch arms"); - assert.equal(await gate(url), false); + assert.equal(await gate(url, { bytes: 16 }, {}), false); assert.equal(probes, 15, "request 4: latch skips the settle loop — one fewer probe"); - assert.equal(await gate(url), false); + assert.equal(await gate(url, { bytes: 16 }, {}), false); assert.equal(probes, 18, "request 5: reduced cost holds"); const health = _attributionHealthForTest(); assert.equal(health.state, "threw"); @@ -2546,11 +2601,13 @@ test("#3581 recovery clears the episode, re-arms settle-retry, and reports ok", throw new Error("agent initiator scope is disposed"); }; const url = "https://api.gate-3581b.test/v1/chat/completions"; - for (let i = 0; i < 3; i++) assert.equal(await gate(url), false); + // #4841: init-carrying (fetch-lane shape) throughout — the no-init + // shape settles synchronously and would not arm the latch. + for (let i = 0; i < 3; i++) assert.equal(await gate(url, { bytes: 16 }, {}), false); assert.equal(_attributionHealthForTest().gaveUp, true, "latch armed by three exhausted throws"); // a new agent scope lands (host re-created it): the first ok claim heals. ctx.agents.currentInitiator = () => ({ session: { id: "s3581b" } }); - assert.equal(await gate(url), true, "recovered attribution claims immediately"); + assert.equal(await gate(url, { bytes: 16 }, {}), true, "recovered attribution claims immediately"); const healed = _attributionHealthForTest(); assert.deepEqual({ state: healed.state, refusals: healed.refusals, since: healed.since }, { state: "ok", refusals: 0, since: 0 }); assert.equal(healed.threwStreak, 0); @@ -2564,7 +2621,7 @@ test("#3581 recovery clears the episode, re-arms settle-retry, and reports ok", probes2 += 1; throw new Error("agent initiator scope is disposed again"); }; - assert.equal(await gate(url), false); + assert.equal(await gate(url, { bytes: 16 }, {}), false); assert.equal(probes2, 4, "settle-retry active again after recovery (full 4-probe cost)"); const reBroken = _attributionHealthForTest(); assert.equal(reBroken.state, "threw");