From 6dfbcb11ba0f378c880270da7bd70a552eaf8ef1 Mon Sep 17 00:00:00 2001 From: awork Date: Wed, 14 Oct 2026 19:53:17 +0800 Subject: [PATCH 1/2] fix: guardrail handlers tolerate a missing event context (#3166 review) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #3088 wired wireToolGuardrails unconditionally into createBiliPlugin, and its input/session_shutdown/tool_call handlers read ctx.sessionManager without guarding ctx itself. The #2185 factory test fires session_shutdown with ctx=undefined (harness contract), so every pi plugin instance crashed the moment that event fired — deterministic red across all four OS/node legs of any merge-tree CI run. Null-safe reads change nothing when a context IS present (the real host always passes one); they only stop the crash. --- src/agent/tool-guardrails.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/agent/tool-guardrails.ts b/src/agent/tool-guardrails.ts index c00d3a7167..85afb22034 100644 --- a/src/agent/tool-guardrails.ts +++ b/src/agent/tool-guardrails.ts @@ -229,14 +229,17 @@ export function wireToolGuardrails(pi: ExtensionAPI, opts: GuardrailOptions = {} const bashTimeout = opts.bashTimeout ?? DEFAULT_TOOL_BASH_TIMEOUT; const maxBytes = opts.maxBytes ?? DEFAULT_TOOL_OUTPUT_MAX_BYTES; + // #3166 review: events may carry no context at all (harnesses fire + // session_shutdown with undefined; hosts omit ctx on shutdown paths) — + // every ctx read below stays null-safe instead of crashing the plugin. pi.on("tool_call", (event, rawCtx) => { - const ctx = rawCtx as unknown as GuardrailCtx; + const ctx = rawCtx as unknown as GuardrailCtx | undefined; const e = event as unknown as ToolCallEventLike; if (isBashToolCall(e)) { const t = resolveBashTimeout(e.input, bashTimeout); if (t !== undefined) e.input.timeout = t; } - if (!repCfg.enabled) return; + if (!ctx || !repCfg.enabled) return; const sid = ctx.sessionManager?.getSessionId?.() ?? ""; let tracker = trackers.get(sid); if (!tracker) { @@ -283,12 +286,12 @@ export function wireToolGuardrails(pi: ExtensionAPI, opts: GuardrailOptions = {} }); pi.on("input", (event, rawCtx) => { - const ctx = rawCtx as unknown as GuardrailCtx; + const ctx = rawCtx as unknown as GuardrailCtx | undefined; if ((event as unknown as { source?: string }).source === "extension") return; - trackers.get(ctx.sessionManager?.getSessionId?.() ?? "")?.reset(); + trackers.get(ctx?.sessionManager?.getSessionId?.() ?? "")?.reset(); }); pi.on("session_shutdown", (_e, rawCtx) => { - const ctx = rawCtx as unknown as GuardrailCtx; - trackers.delete(ctx.sessionManager?.getSessionId?.() ?? ""); + const ctx = rawCtx as unknown as GuardrailCtx | undefined; + trackers.delete(ctx?.sessionManager?.getSessionId?.() ?? ""); }); } From ac43a9d0bd1e80bfab57baa5e47e6949a2c7134c Mon Sep 17 00:00:00 2001 From: awork Date: Wed, 14 Oct 2026 19:53:17 +0800 Subject: [PATCH 2/2] test: resolveLockPath expectation mirrors paths.ts xdg() resolve on win32 (#3166 review) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #3112's resolveLockPath does path.resolve(XDG_CACHE_HOME) exactly like paths.ts xdg() does — the mirror is correct. But the test computed its expectation with join(), which only agrees on Linux: on win32 a POSIX-style value resolves against the current drive, so the assertion failed on every Windows leg. Expect via resolve() so the mirror is pinned cross-platform; the implementation (and thus the lock file location) is unchanged. --- tests/test-suite-lock.test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test-suite-lock.test.ts b/tests/test-suite-lock.test.ts index 6391c0a631..5a32bfbfce 100644 --- a/tests/test-suite-lock.test.ts +++ b/tests/test-suite-lock.test.ts @@ -64,9 +64,12 @@ test("forceEnabled parses BILI_TEST_FORCE truthy/opt-out table", () => { test("resolveLockPath mirrors paths.ts cacheDir() under XDG_CACHE_HOME", () => { const dir = "/tmp/x/cache-home"; + // Both sides resolve the XDG value (paths.ts xdg() and resolveLockPath do), + // so the expectation must use resolve too — on win32 a POSIX-style value + // resolves against the current drive, where join() would not (#3166 review). assert.equal( lck.resolveLockPath({ XDG_CACHE_HOME: dir } as NodeJS.ProcessEnv), - join(dir, "billion-context", "test-suite.lock"), + resolve(dir, "billion-context", "test-suite.lock"), ); assert.ok( lck.resolveLockPath({} as NodeJS.ProcessEnv).startsWith(join(homedir(), ".cache")),