fix(coding-agent): use tool-name allowlists and remove cwd-bound singletons

- treat tools as a global allowlist across built-in, extension, and SDK tools
- remove process-cwd singleton tool usage from SDK and CLI paths
- add regression coverage for extension tool filtering

closes #3452
closes #2835
This commit is contained in:
Mario Zechner
2026-04-20 21:53:07 +02:00
parent ed89480f20
commit 27c1544839
16 changed files with 295 additions and 199 deletions

View File

@@ -1,56 +1,44 @@
/**
* Tools Configuration
*
* Use built-in tool sets or individual tools.
* Use tool names to choose which built-in tools are enabled.
*
* IMPORTANT: When using a custom `cwd`, you must use the tool factory functions
* (createCodingTools, createReadOnlyTools, createReadTool, etc.) to ensure
* tools resolve paths relative to your cwd, not process.cwd().
* Tool names are matched against all available tools. If you use a custom `cwd`,
* createAgentSession() applies that cwd when it builds the actual built-in tools.
*
* For custom tools, see 06-extensions.ts - custom tools are now registered
* via the extensions system using pi.registerTool().
* For custom tools, see 06-extensions.ts - custom tools are registered via the
* extensions system using pi.registerTool().
*/
import {
bashTool,
createAgentSession,
createBashTool,
createCodingTools,
createGrepTool,
createReadTool,
grepTool,
readOnlyTools,
readTool,
SessionManager,
} from "@mariozechner/pi-coding-agent";
import { createAgentSession, SessionManager } from "@mariozechner/pi-coding-agent";
// Read-only mode (no edit/write) - uses process.cwd()
// Read-only mode (no edit/write)
await createAgentSession({
tools: readOnlyTools,
tools: ["read", "grep", "find", "ls"],
sessionManager: SessionManager.inMemory(),
});
console.log("Read-only session created");
// Custom tool selection - uses process.cwd()
// Custom tool selection
await createAgentSession({
tools: [readTool, bashTool, grepTool],
tools: ["read", "bash", "grep"],
sessionManager: SessionManager.inMemory(),
});
console.log("Custom tools session created");
// With custom cwd - MUST use factory functions!
// With custom cwd
const customCwd = "/path/to/project";
await createAgentSession({
cwd: customCwd,
tools: createCodingTools(customCwd), // Tools resolve paths relative to customCwd
sessionManager: SessionManager.inMemory(),
tools: ["read", "bash", "edit", "write"],
sessionManager: SessionManager.inMemory(customCwd),
});
console.log("Custom cwd session created");
// Or pick specific tools for custom cwd
await createAgentSession({
cwd: customCwd,
tools: [createReadTool(customCwd), createBashTool(customCwd), createGrepTool(customCwd)],
sessionManager: SessionManager.inMemory(),
tools: ["read", "bash", "grep"],
sessionManager: SessionManager.inMemory(customCwd),
});
console.log("Specific tools with custom cwd session created");

View File

@@ -2,19 +2,13 @@
* Full Control
*
* Replace everything - no discovery, explicit configuration.
*
* IMPORTANT: When providing `tools` with a custom `cwd`, use the tool factory
* functions (createReadTool, createBashTool, etc.) to ensure tools resolve
* paths relative to your cwd.
*/
import { getModel } from "@mariozechner/pi-ai";
import {
AuthStorage,
createAgentSession,
createBashTool,
createExtensionRuntime,
createReadTool,
ModelRegistry,
type ResourceLoader,
SessionManager,
@@ -41,7 +35,6 @@ const settingsManager = SettingsManager.inMemory({
retry: { enabled: true, maxRetries: 2 },
});
// When using a custom cwd with explicit tools, use the factory functions
const cwd = process.cwd();
const resourceLoader: ResourceLoader = {
@@ -65,9 +58,8 @@ const { session } = await createAgentSession({
authStorage,
modelRegistry,
resourceLoader,
// Use factory functions with the same cwd to ensure path resolution works correctly
tools: [createReadTool(cwd), createBashTool(cwd)],
sessionManager: SessionManager.inMemory(),
tools: ["read", "bash"],
sessionManager: SessionManager.inMemory(cwd),
settingsManager,
});