Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/agent/tool-guardrails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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?.() ?? "");
});
}
5 changes: 4 additions & 1 deletion tests/test-suite-lock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
Loading