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?.() ?? ""); }); } 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")),