Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

refactor(tools): integrate policy filtering into resolveTools

Jiang Bohan12a467710715
diff --git a/src/agent/tools.ts b/src/agent/tools.ts
index 594f1cc11d..287bda45be 100644
--- a/src/agent/tools.ts
+++ b/src/agent/tools.ts
@@ -1,11 +1,12 @@
import type { AgentOptions } from "./types.js";
-import { getModel, type KnownProvider } from "@mariozechner/pi-ai";
+import { getModel } from "@mariozechner/pi-ai";
import { createCodingTools } from "@mariozechner/pi-coding-agent";
import type { AgentTool } from "@mariozechner/pi-agent-core";
import { createExecTool } from "./tools/exec.js";
import { createProcessTool } from "./tools/process.js";
import { createGlobTool } from "./tools/glob.js";
import { createWebFetchTool, createWebSearchTool } from "./tools/web/index.js";
+import { filterTools } from "./tools/policy.js";
export function resolveModel(options: AgentOptions) {
if (options.provider && options.model) {
@@ -18,14 +19,21 @@ export function resolveModel(options: AgentOptions) {
return getModel("kimi-coding", "kimi-k2-thinking");
}
-export function resolveTools(options: AgentOptions): AgentTool<any>[] {
- const cwd = options.cwd ?? process.cwd();
- const baseTools = createCodingTools(cwd).filter((tool) => tool.name !== "bash") as AgentTool<any>[];
+/**
+ * Create all available tools.
+ * This returns the full set before policy filtering.
+ */
+export function createAllTools(cwd: string): AgentTool<any>[] {
+ const baseTools = createCodingTools(cwd).filter(
+ (tool) => tool.name !== "bash",
+ ) as AgentTool<any>[];
+
const execTool = createExecTool(cwd);
const processTool = createProcessTool(cwd);
const globTool = createGlobTool(cwd);
const webFetchTool = createWebFetchTool();
const webSearchTool = createWebSearchTool();
+
return [
...baseTools,
execTool as AgentTool<any>,
@@ -35,3 +43,36 @@ export function resolveTools(options: AgentOptions): AgentTool<any>[] {
webSearchTool as AgentTool<any>,
];
}
+
+/**
+ * Resolve tools for an agent with policy filtering.
+ *
+ * Applies 4-layer filtering:
+ * 1. Profile (minimal/coding/web/full)
+ * 2. Global allow/deny
+ * 3. Provider-specific rules
+ * 4. Subagent restrictions
+ */
+export function resolveTools(options: AgentOptions): AgentTool<any>[] {
+ const cwd = options.cwd ?? process.cwd();
+
+ // Create all tools
+ const allTools = createAllTools(cwd);
+
+ // Apply policy filtering
+ const filtered = filterTools(allTools, {
+ config: options.tools,
+ provider: options.provider,
+ isSubagent: options.isSubagent,
+ });
+
+ return filtered;
+}
+
+/**
+ * Get all available tool names (for debugging/listing).
+ */
+export function getAllToolNames(cwd?: string): string[] {
+ const tools = createAllTools(cwd ?? process.cwd());
+ return tools.map((t) => t.name);
+}
diff --git a/src/agent/types.ts b/src/agent/types.ts
index 3f1ebb1e27..4a1cf9d783 100644
--- a/src/agent/types.ts
+++ b/src/agent/types.ts
@@ -1,5 +1,6 @@
import type { ThinkingLevel } from "@mariozechner/pi-agent-core";
import type { SkillsConfig } from "./skills/types.js";
+import type { ToolsConfig } from "./tools/policy.js";
export type AgentRunResult = {
text: string;
@@ -61,6 +62,12 @@ export type AgentOptions = {
extraSkillDirs?: string[] | undefined;
/** Full skills configuration */
skills?: SkillsConfig | undefined;
+
+ // === Tools Configuration ===
+ /** Tools policy configuration (profile, allow/deny, byProvider) */
+ tools?: ToolsConfig | undefined;
+ /** Whether this is a subagent (applies restricted tool set) */
+ isSubagent?: boolean | undefined;
};
export interface Message {