chore(vendor): import pi 1.3.0
Imported as a linear vendor commit so forge rebases do not replay the upstream root tree. git-subtree-dir: third_party/pi git-subtree-split: 71023aec1546d6d677a65669c5665c0927ce0dd0
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { existsSync, mkdirSync, rmSync } from "node:fs";
|
||||
import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { getModel } from "@earendil-works/pi-ai/compat";
|
||||
@@ -79,6 +79,24 @@ describe("defaultTools setting", () => {
|
||||
session.dispose();
|
||||
});
|
||||
|
||||
it("activates an inactive extension tool with +name", async () => {
|
||||
const session = await createSession(["+inactive_tool", "-write"], {}, [
|
||||
(pi) => {
|
||||
pi.registerTool({
|
||||
name: "inactive_tool",
|
||||
label: "Inactive Tool",
|
||||
description: "Extension tool registered inactive",
|
||||
parameters: Type.Object({}),
|
||||
execute: async () => ({ content: [{ type: "text", text: "ok" }], details: {} }),
|
||||
defaultActive: false,
|
||||
});
|
||||
},
|
||||
]);
|
||||
|
||||
expect(session.getActiveToolNames().sort()).toEqual(["bash", "edit", "inactive_tool", "read"]);
|
||||
session.dispose();
|
||||
});
|
||||
|
||||
it("keeps extension and SDK custom tools enabled", async () => {
|
||||
const session = await createSession(
|
||||
["grep"],
|
||||
@@ -138,6 +156,237 @@ describe("defaultTools setting", () => {
|
||||
toolLessSession.dispose();
|
||||
});
|
||||
|
||||
it("applies +name and -name tool options to the default selection", async () => {
|
||||
const inactiveTool: InlineExtension = (pi) => {
|
||||
pi.registerTool({
|
||||
name: "inactive_tool",
|
||||
label: "Inactive Tool",
|
||||
description: "Extension tool registered inactive",
|
||||
parameters: Type.Object({}),
|
||||
execute: async () => ({ content: [{ type: "text", text: "ok" }], details: {} }),
|
||||
defaultActive: false,
|
||||
});
|
||||
pi.registerTool({
|
||||
name: "active_tool",
|
||||
label: "Active Tool",
|
||||
description: "Extension tool registered active",
|
||||
parameters: Type.Object({}),
|
||||
execute: async () => ({ content: [{ type: "text", text: "ok" }], details: {} }),
|
||||
});
|
||||
};
|
||||
|
||||
const session = await createSession(["+grep"], { tools: ["+inactive_tool", "-write"] }, [inactiveTool]);
|
||||
expect(session.getActiveToolNames().sort()).toEqual([
|
||||
"active_tool",
|
||||
"bash",
|
||||
"edit",
|
||||
"grep",
|
||||
"inactive_tool",
|
||||
"read",
|
||||
]);
|
||||
session.dispose();
|
||||
|
||||
const toolLess = await createSession(["read"], { noTools: "all", tools: ["+inactive_tool"] }, [inactiveTool]);
|
||||
expect(toolLess.getActiveToolNames()).toEqual(["inactive_tool"]);
|
||||
toolLess.dispose();
|
||||
});
|
||||
|
||||
it("rejects invalid tool modifier options", async () => {
|
||||
await expect(createSession([], { tools: ["read", "+grep"] })).rejects.toThrow(
|
||||
"Invalid tools option: tool names cannot be mixed with +name or -name entries",
|
||||
);
|
||||
await expect(createSession([], { tools: ["-gr*"] })).rejects.toThrow(
|
||||
"Invalid tools option: +name and -name entries take exact tool names, not patterns: -gr*",
|
||||
);
|
||||
});
|
||||
|
||||
describe("reload", () => {
|
||||
const inactiveTool: InlineExtension = (pi) => {
|
||||
pi.registerTool({
|
||||
name: "inactive_tool",
|
||||
label: "Inactive Tool",
|
||||
description: "Extension tool registered inactive",
|
||||
parameters: Type.Object({}),
|
||||
execute: async () => ({ content: [{ type: "text", text: "ok" }], details: {} }),
|
||||
defaultActive: false,
|
||||
});
|
||||
};
|
||||
|
||||
const writeSettings = (settings: object) =>
|
||||
writeFileSync(join(agentDir, "settings.json"), JSON.stringify(settings));
|
||||
|
||||
async function createFileSession(options: ToolOptions = {}) {
|
||||
const settingsManager = SettingsManager.create(tempDir, agentDir);
|
||||
const resourceLoader = new DefaultResourceLoader({
|
||||
cwd: tempDir,
|
||||
agentDir,
|
||||
settingsManager,
|
||||
extensionFactories: [inactiveTool],
|
||||
});
|
||||
await resourceLoader.reload();
|
||||
return (
|
||||
await createAgentSession({
|
||||
cwd: tempDir,
|
||||
agentDir,
|
||||
model: getModel("anthropic", "claude-sonnet-4-5")!,
|
||||
settingsManager,
|
||||
sessionManager: SessionManager.inMemory(tempDir),
|
||||
resourceLoader,
|
||||
...options,
|
||||
})
|
||||
).session;
|
||||
}
|
||||
|
||||
// #10245
|
||||
it("activates only tools newly added to defaultTools", async () => {
|
||||
const session = await createFileSession();
|
||||
expect(session.getActiveToolNames()).toEqual(["read", "bash", "edit", "write"]);
|
||||
session.setActiveToolsByName(["read", "edit", "write"]);
|
||||
|
||||
writeSettings({ defaultTools: ["+inactive_tool", "+grep"] });
|
||||
await session.reload();
|
||||
// bash was disabled during the session and is not newly added, so it stays off.
|
||||
expect(session.getActiveToolNames().sort()).toEqual(["edit", "grep", "inactive_tool", "read", "write"]);
|
||||
|
||||
// Removing tools from the setting does not disable them.
|
||||
writeSettings({ defaultTools: ["-read"] });
|
||||
await session.reload();
|
||||
expect(session.getActiveToolNames().sort()).toEqual(["edit", "grep", "inactive_tool", "read", "write"]);
|
||||
session.dispose();
|
||||
});
|
||||
|
||||
it("keeps tools removed by -name tool options removed on reload", async () => {
|
||||
writeSettings({ defaultTools: ["read"] });
|
||||
const session = await createFileSession({ tools: ["-bash", "+grep"] });
|
||||
expect(session.getActiveToolNames()).toEqual(["read", "grep"]);
|
||||
|
||||
writeSettings({ defaultTools: ["read", "bash", "inactive_tool"] });
|
||||
await session.reload();
|
||||
expect(session.getActiveToolNames().sort()).toEqual(["grep", "inactive_tool", "read"]);
|
||||
session.dispose();
|
||||
});
|
||||
|
||||
it("keeps explicit tool options on reload", async () => {
|
||||
const allowlisted = await createFileSession({ tools: ["read"] });
|
||||
writeSettings({ defaultTools: ["+grep"] });
|
||||
await allowlisted.reload();
|
||||
expect(allowlisted.getActiveToolNames()).toEqual(["read"]);
|
||||
allowlisted.dispose();
|
||||
|
||||
writeSettings({});
|
||||
const builtinless = await createFileSession({ noTools: "builtin" });
|
||||
writeSettings({ defaultTools: ["+grep"] });
|
||||
await builtinless.reload();
|
||||
expect(builtinless.getActiveToolNames()).toEqual([]);
|
||||
builtinless.dispose();
|
||||
|
||||
writeSettings({});
|
||||
const excluded = await createFileSession({ excludeTools: ["grep"] });
|
||||
writeSettings({ defaultTools: ["+grep", "+inactive_tool"] });
|
||||
await excluded.reload();
|
||||
expect(excluded.getActiveToolNames().sort()).toEqual(["bash", "edit", "inactive_tool", "read", "write"]);
|
||||
excluded.dispose();
|
||||
});
|
||||
});
|
||||
|
||||
// #10785
|
||||
describe("resume", () => {
|
||||
const extensionTools: InlineExtension = (pi) => {
|
||||
pi.registerTool({
|
||||
name: "inactive_tool",
|
||||
label: "Inactive Tool",
|
||||
description: "Extension tool registered inactive",
|
||||
parameters: Type.Object({}),
|
||||
execute: async () => ({ content: [{ type: "text", text: "ok" }], details: {} }),
|
||||
defaultActive: false,
|
||||
});
|
||||
pi.registerTool({
|
||||
name: "active_tool",
|
||||
label: "Active Tool",
|
||||
description: "Extension tool registered active",
|
||||
parameters: Type.Object({}),
|
||||
execute: async () => ({ content: [{ type: "text", text: "ok" }], details: {} }),
|
||||
});
|
||||
};
|
||||
|
||||
/** A session whose transcript records the loadout `toolNames`, as a run records it. */
|
||||
function createRecordedSession(toolNames: string[]): SessionManager {
|
||||
const sessionManager = SessionManager.inMemory(tempDir);
|
||||
sessionManager.appendMessage({
|
||||
role: "system",
|
||||
content: "",
|
||||
toolsAdded: toolNames.map((name) => ({ name, description: name, parameters: Type.Object({}) })),
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
return sessionManager;
|
||||
}
|
||||
|
||||
async function resume(sessionManager: SessionManager, options: ToolOptions = {}) {
|
||||
const settingsManager = SettingsManager.inMemory();
|
||||
const resourceLoader = new DefaultResourceLoader({
|
||||
cwd: tempDir,
|
||||
agentDir,
|
||||
settingsManager,
|
||||
extensionFactories: [extensionTools],
|
||||
});
|
||||
await resourceLoader.reload();
|
||||
return (
|
||||
await createAgentSession({
|
||||
cwd: tempDir,
|
||||
agentDir,
|
||||
model: getModel("anthropic", "claude-sonnet-4-5")!,
|
||||
settingsManager,
|
||||
sessionManager,
|
||||
resourceLoader,
|
||||
...options,
|
||||
})
|
||||
).session;
|
||||
}
|
||||
|
||||
it("restores the loadout the transcript records", async () => {
|
||||
// active_tool was deactivated during the session, inactive_tool activated.
|
||||
const session = await resume(createRecordedSession(["read", "grep", "inactive_tool"]));
|
||||
expect(session.getActiveToolNames().sort()).toEqual(["grep", "inactive_tool", "read"]);
|
||||
session.dispose();
|
||||
});
|
||||
|
||||
it("does not restore excluded tools", async () => {
|
||||
const session = await resume(createRecordedSession(["read", "grep", "inactive_tool"]), {
|
||||
excludeTools: ["inactive_tool"],
|
||||
});
|
||||
expect(session.getActiveToolNames().sort()).toEqual(["grep", "read"]);
|
||||
session.dispose();
|
||||
});
|
||||
|
||||
it("uses explicit tool options instead of the recorded loadout", async () => {
|
||||
const recorded = ["read", "grep", "inactive_tool"];
|
||||
|
||||
const allowlisted = await resume(createRecordedSession(recorded), { tools: ["read", "write"] });
|
||||
expect(allowlisted.getActiveToolNames()).toEqual(["read", "write"]);
|
||||
allowlisted.dispose();
|
||||
|
||||
const builtinless = await resume(createRecordedSession(recorded), { noTools: "builtin" });
|
||||
expect(builtinless.getActiveToolNames()).toEqual(["active_tool"]);
|
||||
builtinless.dispose();
|
||||
});
|
||||
|
||||
it("applies +name and -name tool options to the recorded loadout", async () => {
|
||||
const session = await resume(createRecordedSession(["read", "grep", "inactive_tool"]), {
|
||||
tools: ["-grep", "+write"],
|
||||
});
|
||||
expect(session.getActiveToolNames().sort()).toEqual(["inactive_tool", "read", "write"]);
|
||||
session.dispose();
|
||||
});
|
||||
|
||||
it("uses the default tools when the transcript records no loadout", async () => {
|
||||
const sessionManager = SessionManager.inMemory(tempDir);
|
||||
sessionManager.appendMessage({ role: "user", content: "hi", timestamp: Date.now() });
|
||||
const session = await resume(sessionManager);
|
||||
expect(session.getActiveToolNames().sort()).toEqual(["active_tool", "bash", "edit", "read", "write"]);
|
||||
session.dispose();
|
||||
});
|
||||
});
|
||||
|
||||
it("applies through service-based session creation", async () => {
|
||||
const settingsManager = SettingsManager.inMemory({ defaultTools: ["ls"] });
|
||||
const services = await createAgentSessionServices({ cwd: tempDir, agentDir, settingsManager });
|
||||
|
||||
Reference in New Issue
Block a user