From e630c786c2e922e6fc6967390022665fb1cca9c8 Mon Sep 17 00:00:00 2001 From: awork Date: Fri, 23 Oct 2026 08:20:55 +0800 Subject: [PATCH 1/4] fix(fork-adopt): defer adoption when the capability probe cannot reach the proxy (#4266) Item 9 of the zero-inheritance-cloning family (#4266): createForkAdopter's capability gate treated ANY failed manifest probe as terminal (done.add on first failure, no retry budget), even when the failure was only that the proxy was not reachable yet (host starting up / restarting / port changing). A child forked in that window was permanently zero-inherited, then dead-ended on the first-turn context_length_exceeded downstream. probeForkCapability now distinguishes definitive=true (manifest read OK but fork not advertised - old proxy: terminal on first probe, unchanged, avoids 404 noise) from definitive=false (manifest unreadable right now - !res.ok / timeout / network error: transient, deferred and retried up to maxAttempts, adopting once the proxy is reachable). Only definitive results stay cached; a failed read is never cached, so the next request re-hits the wire and self-heals. manifestForkCapable stays a thin boolean wrapper so existing callers/tests are unaffected. All native lanes (dsh/opencode/pi) share createForkAdopter, so the fix covers all of them. Behavior change (old -> new): a single transient capability-probe failure used to retire a child's adoption for the process lifetime; it now retries within the existing maxAttempts budget (default 3). Definitively-incapable proxies are unchanged (still terminal on first probe). Tests: rewrote the exploding-network case (now asserts deferred-until-cap instead of closes-on-first) and added a regression repro (proxy down on request 1, up on request 2 -> child adopts). 45/45 green across the three fork-adopt suites. Addresses item 9 of #4266. --- src/agent/fork-adopt.ts | 53 ++++++++++++++++++++++++++------- tests/pi-omp-fork-adopt.test.ts | 39 ++++++++++++++++++++---- 2 files changed, 75 insertions(+), 17 deletions(-) diff --git a/src/agent/fork-adopt.ts b/src/agent/fork-adopt.ts index c4a0ba7428..727449613f 100644 --- a/src/agent/fork-adopt.ts +++ b/src/agent/fork-adopt.ts @@ -479,16 +479,28 @@ export function resetForkCapabilityCacheForTest(): void { forkCapabilityCache.clear(); } -/** Old proxies have no fork endpoints at all; probing manifest keeps adoption - * off their request path (and avoids harmless 404 noise). Cached per base — - * register.base changes when the extension re-registers with a new proxy. */ -export async function manifestForkCapable(base: string, fetchImpl?: typeof fetch): Promise { +/** Capability-probe result. `definitive` separates two DIFFERENT "not capable" + * outcomes that must drive different retry policy (#4266 item ⑨): + * - definitive=true — the manifest was READ but does not advertise fork (an + * old proxy with no fork endpoints). Terminal: retrying can never change the + * answer, so adoption closes on the first probe (keeps such proxies off the + * request path and avoids repeated 404 noise). + * - definitive=false — the manifest could not be read RIGHT NOW (proxy still + * starting up / restarting / port changing: !res.ok, timeout, network error). + * Transient: the window stays open so a later request retries within the + * attempt budget instead of permanently retiring the child. + * Only definitive results are cached per base (register.base changes when the + * extension re-registers with a new proxy); a failed read is never cached, so a + * later probe re-hits the wire and self-heals once the proxy is reachable. */ +export type ForkCapabilityProbe = { capable: boolean; definitive: boolean }; + +export async function probeForkCapability(base: string, fetchImpl?: typeof fetch): Promise { const cached = forkCapabilityCache.get(base); - if (cached !== undefined) return cached; + if (cached !== undefined) return { capable: cached, definitive: true }; const doFetch = fetchImpl ?? fetch; try { const res = await doFetch(`${base}/__bili/plugin/manifest`, { signal: AbortSignal.timeout(5000) }); - if (!res.ok) return false; + if (!res.ok) return { capable: false, definitive: false }; const manifest = await res.json() as { capabilities?: { fork?: { protocolVersion?: unknown } } }; // #2671 Phase 2b: fork protocol v2 proxies advertise protocolVersion 2; // v1 proxies advertise 1. The adoption payload stays v1 during the @@ -496,12 +508,18 @@ export async function manifestForkCapable(base: string, fetchImpl?: typeof fetch const version = manifest.capabilities?.fork?.protocolVersion; const capable = version === 1 || version === 2; forkCapabilityCache.set(base, capable); - return capable; + return { capable, definitive: true }; } catch { - return false; + return { capable: false, definitive: false }; } } +/** Boolean convenience kept for direct callers/tests: true only when the probe + * is capable (a failed read reports false, matching the pre-#4266 contract). */ +export async function manifestForkCapable(base: string, fetchImpl?: typeof fetch): Promise { + return (await probeForkCapability(base, fetchImpl)).capable; +} + export type ForkAdoptInput = { base: string; parent: string; child: string; body: unknown }; /** Per-host fork-adoption coordinator (#2399 stage 2): absorbs the bookkeeping @@ -548,9 +566,22 @@ export function createForkAdopter(log: (line: string) => void, opts?: { maxAttem done.add(input.child); return; } - if (!(await manifestForkCapable(input.base, opts?.fetchImpl))) { - log(`fork adoption for ${input.child} skipped — the proxy does not advertise the fork capability (#2399)`); - done.add(input.child); + const capability = await probeForkCapability(input.base, opts?.fetchImpl); + if (!capability.capable) { + if (capability.definitive) { + // Manifest read OK but fork not advertised (old proxy): terminal — + // retrying can never change the answer. + log(`fork adoption for ${input.child} skipped — the proxy does not advertise the fork capability (#2399)`); + done.add(input.child); + } else { + // Manifest unreadable right now (proxy starting/restarting/port + // changing) — transient. Leave the window open so a later request + // retries within the budget instead of permanently retiring the + // child (#4266 item ⑨). + const attempt = attempts.get(input.child) ?? 0; + log(`fork adoption for ${input.child} deferred — could not confirm fork capability yet (attempt ${attempt}/${maxAttempts}) (#2399/#4266)`); + if (attempt >= maxAttempts) done.add(input.child); + } return; } const result = await tryForkAdoption({ diff --git a/tests/pi-omp-fork-adopt.test.ts b/tests/pi-omp-fork-adopt.test.ts index 1e1819d548..6845b4a549 100644 --- a/tests/pi-omp-fork-adopt.test.ts +++ b/tests/pi-omp-fork-adopt.test.ts @@ -223,18 +223,45 @@ test("createForkAdopter lets responses children reach the protocol — no termin assert.ok(!lines.some((l) => l.includes("wire requests are not supported")), "no unsupported-wire skip for responses"); }); -test("createForkAdopter never throws — an exploding network soft-degrades and closes the window", async () => { +test("createForkAdopter never throws — an unreachable proxy defers adoption until the cap (#4266 item ⑨)", async () => { resetForkCapabilityCacheForTest(); const boom = (async () => { throw new Error("network exploded"); }) as unknown as typeof fetch; const lines: string[] = []; const adopter = createForkAdopter((l) => lines.push(l), { fetchImpl: boom }); - // manifestForkCapable swallows the explosion (false = not capable), so the - // child degrades on the capability line and maybeAdopt never rejects. + // probeForkCapability swallows the explosion (capable=false, definitive=false), + // so the child DEFERS instead of terminating: maybeAdopt never rejects, and the + // window stays open for retries within the budget rather than closing on the + // first failed probe (the pre-fix behavior permanently retired the child). await adopter.maybeAdopt(adoptInput()); - assert.ok(lines.some((l) => l.includes("does not advertise the fork capability")), "the failure is logged, not thrown"); - const callsBefore = lines.length; + assert.ok(lines.some((l) => l.includes("could not confirm fork capability yet")), "the unreachable probe is logged as deferred, not thrown"); await adopter.maybeAdopt(adoptInput()); - assert.equal(lines.length, callsBefore, "the child is done after the first terminal outcome"); + await adopter.maybeAdopt(adoptInput()); + assert.equal(lines.filter((l) => l.includes("could not confirm fork capability yet")).length, 3, "each attempt within the cap re-probes"); + const afterCap = lines.length; + await adopter.maybeAdopt(adoptInput()); + assert.equal(lines.length, afterCap, "past the cap the window is closed"); +}); + +test("createForkAdopter adopts once the proxy comes up — a failed probe does not retire the child (#4266 item ⑨)", async () => { + resetForkCapabilityCacheForTest(); + const core = openaiBodyToCore(adoptInput().body as Record)!; + const matchSnapshot = { match: snapshotRoute.match, respond: () => Response.json({ ok: true, protocolVersion: 1, status: "exact", parentRevision: hash("rev1"), orderHash: hash("full"), orderedMessages: [{ rawId: "a", ref: "m00001", identityHash: forkIdentityHashOf(core[0]!) }] }) }; + // The proxy is down for the first request (manifest probe gets a 5xx / connection + // refused) and comes up before the second — exactly the host-startup window the + // production incident hit. Pre-fix the first failed probe marked the child done + // forever; now the window stays open and the second request adopts. + let manifestHits = 0; + const flakyManifest = { match: manifestRoute.match, respond: () => { manifestHits += 1; return manifestHits === 1 ? new Response("{}", { status: 503 }) : Response.json({ capabilities: { fork: { protocolVersion: 1 } } }); } }; + const { fetchImpl, calls } = fakeFetchRouting([flakyManifest, matchSnapshot, forkRoute]); + const lines: string[] = []; + const adopter = createForkAdopter((l) => lines.push(l), { fetchImpl }); + await adopter.maybeAdopt(adoptInput()); + assert.ok(!lines.some((l) => l.includes("degraded")) && !lines.some((l) => l.includes("does not advertise")), "the transient probe failure neither degrades nor terminally skips"); + assert.ok(lines.some((l) => l.includes("could not confirm fork capability yet")), "the first request defers"); + assert.equal(calls.filter((c) => c.url.endsWith("/__bili/plugin/fork")).length, 0, "no fork POST while the proxy is down"); + await adopter.maybeAdopt(adoptInput()); + assert.equal(calls.filter((c) => c.url.endsWith("/__bili/plugin/fork")).length, 1, "the next request adopts once the proxy is reachable"); + assert.equal(lines.filter((l) => l.includes("could not confirm fork capability yet")).length, 1, "exactly one deferral — the retry succeeded"); }); test("createForkAdopter retries transient failures until the cap, then closes (#2403 review)", async () => { From dc108f9da1d41c0f3c7d3e30ec1a25adc8771568 Mon Sep 17 00:00:00 2001 From: awork Date: Fri, 23 Oct 2026 08:28:29 +0800 Subject: [PATCH 2/4] fix(fork-adopt): drop unused export flagged by unused-exports gate ForkCapabilityProbe is module-internal; only probeForkCapability and manifestForkCapable are consumed across modules. INTERNAL_ONLY fix per scripts/check-unused-exports.mjs. --- src/agent/fork-adopt.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/agent/fork-adopt.ts b/src/agent/fork-adopt.ts index 727449613f..e483450e0e 100644 --- a/src/agent/fork-adopt.ts +++ b/src/agent/fork-adopt.ts @@ -492,7 +492,7 @@ export function resetForkCapabilityCacheForTest(): void { * Only definitive results are cached per base (register.base changes when the * extension re-registers with a new proxy); a failed read is never cached, so a * later probe re-hits the wire and self-heals once the proxy is reachable. */ -export type ForkCapabilityProbe = { capable: boolean; definitive: boolean }; +type ForkCapabilityProbe = { capable: boolean; definitive: boolean }; export async function probeForkCapability(base: string, fetchImpl?: typeof fetch): Promise { const cached = forkCapabilityCache.get(base); From 65f9db3733f2172f5091b09d2daf52d98dc4e2dc Mon Sep 17 00:00:00 2001 From: awork Date: Fri, 23 Oct 2026 18:14:49 +0800 Subject: [PATCH 3/4] fix(ci): un-export file-local names in corrupt-index-child helper (unused-exports gate) In-passing fix, unrelated to this PR's fork-adopt change: #4378 landed ENGPROOF_CHECK_NAMES / BANNER_CHECK_NAMES / CorruptIndexCheck / CorruptIndexVerdict exported but referenced only inside the same file, which the unused-exports gate (scripts/check-unused-exports.mjs, ci.yml unused-exports lane) rejects on every head including this one. Dropped the four leading exports; verified no importer outside the file (parents use ENGPROOF_CHECKS/BANNER_CHECKS/spawnCorruptIndexChild only). Gate clean + both consumer suites 17/17. --- tests/corrupt-index-child.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/corrupt-index-child.ts b/tests/corrupt-index-child.ts index eb35035dd7..b58394a370 100644 --- a/tests/corrupt-index-child.ts +++ b/tests/corrupt-index-child.ts @@ -125,7 +125,7 @@ export function makeOpts(upstreamPort: number): ProxyOptions { // sides reference these constants, so the coverage list cannot drift. // --------------------------------------------------------------------------- -export const ENGPROOF_CHECK_NAMES = { +const ENGPROOF_CHECK_NAMES = { fallbackRead: "legacy fallback returned a session over the corrupt index", midFlowGuard: "no live handle after the corrupt-index read (#2781 rename guard)", stateDigest: "corrupt-index fallback state digest matches the clean full-mode read", @@ -134,7 +134,7 @@ export const ENGPROOF_CHECK_NAMES = { } as const; export const ENGPROOF_CHECKS: readonly string[] = Object.values(ENGPROOF_CHECK_NAMES); -export const BANNER_CHECK_NAMES = { +const BANNER_CHECK_NAMES = { overviewAnswers: "/__bili/overview answers 200 with JSON", requestedFull: "storage.requested is full", effectiveLegacy: "storage.effective is legacy", @@ -145,12 +145,12 @@ export const BANNER_CHECK_NAMES = { } as const; export const BANNER_CHECKS: readonly string[] = Object.values(BANNER_CHECK_NAMES); -export interface CorruptIndexCheck { +interface CorruptIndexCheck { name: string; ok: boolean; detail?: string; } -export interface CorruptIndexVerdict { +interface CorruptIndexVerdict { ok: boolean; failure: string | null; checks: CorruptIndexCheck[]; From dfadc21b339bfa45c6ec03b2f13e723ce6bf0507 Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 15:22:50 +0800 Subject: [PATCH 4/4] fix(ci): sync lockfile recorded versions with acp-kernel 0.0.165 (#4599) master window commits 293ae6542/de5e03659 bumped kernel/package.json to 0.0.165 without regenerating the lockfiles; lock-sync re-resolution then rewrites the stale recorded versions (root 0.0.163, kernel 0.0.164) and fails. Pure recorded-version sync via npm install --package-lock-only; no dependency or semantic change. --- kernel/package-lock.json | 4 ++-- package-lock.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/package-lock.json b/kernel/package-lock.json index da011d94e1..4c5e2d79e9 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.164", + "version": "0.0.165", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.164", + "version": "0.0.165", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/package-lock.json b/package-lock.json index 380212c96e..15a6e3f4e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.163", + "version": "0.0.165", "dev": true, "license": "MIT", "devDependencies": {