Skip to content
Merged
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
184 changes: 184 additions & 0 deletions test/advisor-repo-read-only-tools.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import fs from "node:fs";
import os from "node:os";
import path from "node:path";

import type { ToolDefinition } from "@earendil-works/pi-coding-agent";
import { afterEach, beforeEach, describe, expect, it } from "vitest";

import { createRepoConfinedReadOnlyTools } from "../tools/advisors/repo-read-only-tools.mts";

const tempDirs: string[] = [];
let workspace: string;
let outside: string;
let tools: Map<string, ToolDefinition>;

const toolInputs: Record<string, (target: string) => Record<string, unknown>> = {
read: (target) => ({ path: target }),
grep: (target) => ({ pattern: "needle", path: target, literal: true }),
find: (target) => ({ pattern: "*", path: target }),
ls: (target) => ({ path: target }),
};
const piUnicodeSpaces = [
["U+00A0", "\u00A0"],
["U+2000", "\u2000"],
["U+2001", "\u2001"],
["U+2002", "\u2002"],
["U+2003", "\u2003"],
["U+2004", "\u2004"],
["U+2005", "\u2005"],
["U+2006", "\u2006"],
["U+2007", "\u2007"],
["U+2008", "\u2008"],
["U+2009", "\u2009"],
["U+200A", "\u200A"],
["U+202F", "\u202F"],
["U+205F", "\u205F"],
["U+3000", "\u3000"],
] as const;

async function execute(name: string, input: Record<string, unknown>) {
return tools
.get(name)!
.execute("test-call", input as never, undefined, undefined, undefined as never);
}

beforeEach(() => {
workspace = fs.mkdtempSync(path.join(os.tmpdir(), "advisor-workspace-"));
outside = fs.mkdtempSync(path.join(os.tmpdir(), "advisor-outside-"));
tempDirs.push(workspace, outside);
fs.writeFileSync(path.join(workspace, "safe.txt"), "safe needle\n", "utf8");
fs.writeFileSync(path.join(outside, "secret.txt"), "secret needle\n", "utf8");
fs.symlinkSync(path.join(outside, "secret.txt"), path.join(workspace, "escaped-file"));
fs.symlinkSync(outside, path.join(workspace, "escaped-directory"), "dir");
tools = new Map(createRepoConfinedReadOnlyTools(workspace).map((tool) => [tool.name, tool]));
});

afterEach(() => {
for (const dir of tempDirs.splice(0)) fs.rmSync(dir, { recursive: true, force: true });
});

describe("repo-confined advisor read-only tools", () => {
it.each([
"read",
"grep",
"find",
"ls",
])("rejects an absolute outside path through %s (#6446)", async (name) => {
await expect(execute(name, toolInputs[name]!(outside))).rejects.toThrow(
"outside the workspace",
);
});

it.each([
["read", "escaped-file"],
["grep", "escaped-directory"],
["find", "escaped-directory"],
["ls", "escaped-directory"],
])("rejects a symlink escape through %s (#6446)", async (name, target) => {
await expect(execute(name, toolInputs[name]!(target))).rejects.toThrow(
"resolves outside the workspace",
);
});

it("rejects the proc environment path before the SDK can read it (#6446)", async () => {
await expect(execute("read", { path: "/proc/self/environ" })).rejects.toThrow(
"outside the workspace",
);
});

it.each([
["read", "@/proc/self/environ"],
["ls", "~/advisor-private-file"],
])("rejects the SDK %s path alias %s before delegation (#6446)", async (name, target) => {
await expect(execute(name, toolInputs[name]!(target))).rejects.toThrow("outside the workspace");
});

it("rejects a relative parent traversal before delegation (#6446)", async () => {
const traversal = path.relative(workspace, path.join(outside, "secret.txt"));
await expect(execute("read", { path: traversal })).rejects.toThrow("outside the workspace");
});

it.each(
piUnicodeSpaces,
)("normalizes the Pi SDK %s space before guarding read (#6446)", async (_codePoint, unicodeSpace) => {
const unicodePath = `safe${unicodeSpace}target`;
fs.writeFileSync(path.join(workspace, unicodePath), "safe\n", "utf8");
fs.symlinkSync(path.join(outside, "secret.txt"), path.join(workspace, "safe target"));

await expect(execute("read", { path: unicodePath })).rejects.toThrow(
"resolves outside the workspace",
);
});

it.each([
"grep",
"find",
"ls",
])("normalizes Unicode spaces before guarding a %s directory root (#6446)", async (name) => {
fs.mkdirSync(path.join(workspace, "safe\u00A0directory"));
fs.symlinkSync(outside, path.join(workspace, "safe directory"), "dir");

await expect(execute(name, toolInputs[name]!("safe\u00A0directory"))).rejects.toThrow(
"resolves outside the workspace",
);
});

it("rejects a canonical file target changed by Pi SDK normalization (#6446)", async () => {
fs.writeFileSync(path.join(workspace, "safe\u00A0target"), "safe\n", "utf8");
fs.symlinkSync(path.join(workspace, "safe\u00A0target"), path.join(workspace, "safe-link"));
fs.symlinkSync(path.join(outside, "secret.txt"), path.join(workspace, "safe target"));

await expect(execute("read", { path: "safe-link" })).rejects.toThrow(
"not stable under Pi SDK normalization",
);
});

it.each([
"grep",
"find",
"ls",
])("rejects a canonical %s directory target changed by Pi SDK normalization (#6446)", async (name) => {
fs.mkdirSync(path.join(workspace, "safe\u00A0directory"));
fs.symlinkSync(
path.join(workspace, "safe\u00A0directory"),
path.join(workspace, "safe-link"),
"dir",
);
fs.symlinkSync(outside, path.join(workspace, "safe directory"), "dir");

await expect(execute(name, toolInputs[name]!("safe-link"))).rejects.toThrow(
"not stable under Pi SDK normalization",
);
});

it("keeps ordinary read, grep, find, and ls behavior inside the workspace (#6446)", async () => {
await expect(execute("read", { path: "safe.txt" })).resolves.toMatchObject({
content: [{ type: "text", text: "safe needle\n" }],
});
await expect(
execute("grep", { pattern: "needle", path: ".", literal: true }),
).resolves.toMatchObject({
content: [{ type: "text", text: expect.stringContaining("safe.txt") }],
});
await expect(execute("find", { pattern: "*.txt", path: "." })).resolves.toMatchObject({
content: [{ type: "text", text: expect.stringContaining("safe.txt") }],
});
const listing = await execute("ls", { path: "." });
expect(listing).toMatchObject({
content: [{ type: "text", text: expect.stringContaining("safe.txt") }],
});
expect((listing.content[0] as { text: string }).text).not.toContain("escaped-");
});

it("does not traverse outside symlinks while searching the workspace (#6446)", async () => {
await expect(
execute("grep", { pattern: "secret", path: ".", literal: true }),
).resolves.toMatchObject({ content: [{ type: "text", text: "No matches found" }] });
await expect(execute("find", { pattern: "secret.txt", path: "." })).resolves.toMatchObject({
content: [{ type: "text", text: "No files found matching pattern" }],
});
});
});
159 changes: 159 additions & 0 deletions test/advisor-session-context-tools.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { describe, expect, it } from "vitest";
import {
type AdvisorPromptTurn,
type AdvisorTurnFlowEvent,
advisorTurnFlowErrors,
createAdvisorContextToolRuntime,
missingRequiredAdvisorToolNames,
promptWithRequiredContextTools,
resolveAdvisorTurnTools,
} from "../tools/advisors/session.mts";

function contextTurn(name: string, content: string): AdvisorPromptTurn {
return {
name,
prompt: `Turn ${name}`,
contextToolResults: [
{ toolName: "pr_review_context", content, contentType: "json", label: `${name} context` },
],
};
}

const ledgerToolName = "pr_review_update_ledger";
const finalMutationTools = {
activeToolNames: [ledgerToolName],
requiredToolNames: [ledgerToolName],
requireToolsBeforeText: [],
requireTextBeforeToolNames: [ledgerToolName],
};
const analysisEvent: AdvisorTurnFlowEvent = { type: "text", text: "analysis" };
const ledgerStart: AdvisorTurnFlowEvent = { type: "tool_start", toolName: ledgerToolName };
const ledgerSuccess: AdvisorTurnFlowEvent = {
type: "tool_end",
toolName: ledgerToolName,
isError: false,
};
const ledgerFailure: AdvisorTurnFlowEvent = { ...ledgerSuccess, isError: true };
const invalidFinalMutationFlows: Array<[string, AdvisorTurnFlowEvent[], string]> = [
["an omitted call", [analysisEvent], "observed 0 starts"],
[
"duplicate starts",
[analysisEvent, ledgerStart, ledgerStart, ledgerSuccess],
"observed 2 starts",
],
["an omitted completion", [analysisEvent, ledgerStart], "0 successful of 0 total"],
[
"duplicate successful completions",
[analysisEvent, ledgerStart, ledgerSuccess, ledgerSuccess],
"2 successful of 2 total",
],
["a failed completion", [analysisEvent, ledgerStart, ledgerFailure], "0 successful of 1 total"],
[
"a failed duplicate completion",
[analysisEvent, ledgerStart, ledgerSuccess, ledgerFailure],
"1 successful of 2 total",
],
];

describe("advisor session context tool flow", () => {
it("keeps turn context inert until its real scoped tool is invoked (#6446)", async () => {
const first = contextTurn("first", '{"turn":1}');
const second = contextTurn("second", '{"turn":2}');
const runtime = createAdvisorContextToolRuntime([first, second]);
const tool = runtime.customTools[0];

expect(runtime.allToolNames).toEqual(["pr_review_context"]);
expect(tool?.parameters).toEqual({
type: "object",
properties: {},
additionalProperties: false,
});
await expect(
tool?.execute("inactive", {}, undefined, undefined, undefined as never),
).rejects.toThrow("not active");

runtime.activateTurn(first);
await expect(
tool?.execute("first", {}, undefined, undefined, undefined as never),
).resolves.toMatchObject({ content: [{ type: "text", text: '{"turn":1}' }] });
runtime.activateTurn(second);
await expect(
tool?.execute("second", {}, undefined, undefined, undefined as never),
).resolves.toMatchObject({ content: [{ type: "text", text: '{"turn":2}' }] });
});

it("requires successful context and declared custom tool calls (#6446)", () => {
const turn: AdvisorPromptTurn = {
...contextTurn("review", "{}"),
activeToolNames: ["pr_review_update_ledger"],
requiredToolNames: ["pr_review_update_ledger"],
requireTextBeforeToolNames: ["pr_review_update_ledger"],
};
const tools = resolveAdvisorTurnTools(
turn,
["pr_review_context"],
new Set(["pr_review_context", "pr_review_update_ledger"]),
);

expect(tools.activeToolNames).toEqual(["pr_review_context", "pr_review_update_ledger"]);
expect(tools.requiredToolNames).toEqual(["pr_review_context", "pr_review_update_ledger"]);
expect(promptWithRequiredContextTools("Review", ["pr_review_context"])).toContain(
"results are not preloaded; call each before answering",
);
expect(
advisorTurnFlowErrors(
"review",
[
{ type: "tool_start", toolName: "pr_review_context" },
{ type: "tool_end", toolName: "pr_review_context", isError: false },
{ type: "text", text: "Finding F-001 is actionable." },
{ type: "tool_start", toolName: "pr_review_update_ledger" },
{ type: "tool_end", toolName: "pr_review_update_ledger", isError: false },
],
tools,
),
).toEqual([]);
expect(
advisorTurnFlowErrors(
"review",
[
{ type: "text", text: "premature" },
{ type: "tool_start", toolName: "pr_review_update_ledger" },
],
tools,
).join("; "),
).toContain("text before pr_review_context completed");
expect(
advisorTurnFlowErrors(
"review",
[
{ type: "tool_end", toolName: "pr_review_context", isError: false },
{ type: "text", text: "analysis" },
{ type: "tool_start", toolName: "pr_review_update_ledger" },
{ type: "tool_start", toolName: "read" },
],
tools,
).join("; "),
).toContain("called read after pr_review_update_ledger");
expect(
missingRequiredAdvisorToolNames(tools.requiredToolNames, new Set(["pr_review_context"])),
).toEqual(["pr_review_update_ledger"]);
expect(
missingRequiredAdvisorToolNames(
tools.requiredToolNames,
new Set(["pr_review_context", "pr_review_update_ledger"]),
),
).toEqual([]);
});

it.each(
invalidFinalMutationFlows,
)("rejects %s for a final mutation tool (#6446)", (_case, events, expectedError) => {
expect(advisorTurnFlowErrors("review", events, finalMutationTools).join("; ")).toContain(
expectedError,
);
});
});
Loading