@@ -1,40 +1,35 @@
11 import type { ToolDef } from "../llm-api/types.ts";
2+import type { CreateOutput } from "../tools/create.ts";
23 import { createTool } from "../tools/create.ts";
4+import type { GlobOutput } from "../tools/glob.ts";
35 import { globTool } from "../tools/glob.ts";
6+import type { GrepOutput } from "../tools/grep.ts";
47 import { grepTool } from "../tools/grep.ts";
8+import {
9+ createHookCache,
10+ hookEnvForCreate,
11+ hookEnvForGlob,
12+ hookEnvForGrep,
13+ hookEnvForInsert,
14+ hookEnvForRead,
15+ hookEnvForReplace,
16+ hookEnvForShell,
17+ runHook,
18+} from "../tools/hooks.ts";
19+import type { InsertOutput } from "../tools/insert.ts";
520 import { insertTool } from "../tools/insert.ts";
21+import type { ReadOutput } from "../tools/read.ts";
622 import { readTool } from "../tools/read.ts";
23+import type { ReplaceOutput } from "../tools/replace.ts";
724 import { replaceTool } from "../tools/replace.ts";
25+import type { ShellOutput } from "../tools/shell.ts";
826 import { shellTool } from "../tools/shell.ts";
927 import { createSubagentTool } from "../tools/subagent.ts";
1028
11-// ─── Hook types ────────────────────────────────────────────────────────────────
12-
13-export type ToolHook = (toolName: string, result: unknown) => Promise<void>;
14-
15-const _hooks: ToolHook[] = [];
16-
17-export function addToolHook(hook: ToolHook): void {
18- _hooks.push(hook);
19-}
20-
21-export async function runToolHooks(
22- toolName: string,
23- result: unknown,
24-): Promise<void> {
25- for (const hook of _hooks) {
26- try {
27- await hook(toolName, result);
28- } catch {
29- // Hooks must not crash the agent
30- }
31- }
32-}
33-
34-// ─── Tool registry ────────────────────────────────────────────────────────────
29+// ─── Tool wrappers ────────────────────────────────────────────────────────────
3530
3631 /**
37- * Wrap a tool's execute function to inject a default cwd if not provided.
32+ * Inject a default cwd if not provided by the LLM.
3833 */
3934 function withCwdDefault(tool: ToolDef, cwd: string): ToolDef {
4035 const originalExecute = tool.execute;
@@ -50,8 +45,52 @@ function withCwdDefault(tool: ToolDef, cwd: string): ToolDef {
5045 };
5146 }
5247
48+type EnvBuilder<TInput, TOutput> = (
49+ result: TOutput,
50+ input: TInput,
51+) => Record<string, string>;
52+
53+/**
54+ * Wrap a tool to fire its post-hook (if one exists) after each successful execute.
55+ * Hooks are looked up via the session-scoped cache — no filesystem access per call.
56+ * Hooks are fire-and-forget; errors are always swallowed.
57+ */
58+function withHooks<TInput extends { cwd?: string }, TOutput>(
59+ tool: ToolDef<TInput, TOutput>,
60+ lookupHook: (toolName: string) => string | null,
61+ cwd: string,
62+ buildEnv: EnvBuilder<TInput, TOutput>,
63+): ToolDef<TInput, TOutput> {
64+ const originalExecute = tool.execute;
65+ return {
66+ ...tool,
67+ execute: async (input: TInput) => {
68+ const result = await originalExecute(input);
69+ const hookScript = lookupHook(tool.name);
70+ if (hookScript) {
71+ const env = buildEnv(result, input);
72+ await runHook(hookScript, env, cwd);
73+ }
74+ return result;
75+ },
76+ };
77+}
78+
79+// ─── Tool registry ────────────────────────────────────────────────────────────
80+
5381 export const MAX_SUBAGENT_DEPTH = 3;
5482
83+// Names of all local tools that support hooks (MCP tools are excluded).
84+const HOOKABLE_TOOLS = [
85+ "glob",
86+ "grep",
87+ "read",
88+ "create",
89+ "replace",
90+ "insert",
91+ "shell",
92+];
93+
5594 export function buildToolSet(opts: {
5695 cwd: string;
5796 depth?: number;
@@ -64,18 +103,77 @@ export function buildToolSet(opts: {
64103 outputTokens: number;
65104 }>;
66105 }): ToolDef[] {
106+ const { cwd } = opts;
67107 const depth = opts.depth ?? 0;
108+ const lookupHook = createHookCache(HOOKABLE_TOOLS, cwd);
109+
68110 return [
69111 // Read-only: discover and inspect files
70- withCwdDefault(globTool as ToolDef, opts.cwd),
71- withCwdDefault(grepTool as ToolDef, opts.cwd),
72- withCwdDefault(readTool as ToolDef, opts.cwd),
112+ withHooks(
113+ withCwdDefault(globTool as ToolDef, cwd) as ToolDef<
114+ { cwd?: string; pattern: string },
115+ GlobOutput
116+ >,
117+ lookupHook,
118+ cwd,
119+ (_, input) => hookEnvForGlob(input, cwd),
120+ ) as ToolDef,
121+ withHooks(
122+ withCwdDefault(grepTool as ToolDef, cwd) as ToolDef<
123+ { cwd?: string; pattern: string },
124+ GrepOutput
125+ >,
126+ lookupHook,
127+ cwd,
128+ (_, input) => hookEnvForGrep(input, cwd),
129+ ) as ToolDef,
130+ withHooks(
131+ withCwdDefault(readTool as ToolDef, cwd) as ToolDef<
132+ { cwd?: string; path: string },
133+ ReadOutput
134+ >,
135+ lookupHook,
136+ cwd,
137+ (_, input) => hookEnvForRead(input, cwd),
138+ ) as ToolDef,
73139 // Write: create/overwrite, replace/delete, insert
74- withCwdDefault(createTool as ToolDef, opts.cwd),
75- withCwdDefault(replaceTool as ToolDef, opts.cwd),
76- withCwdDefault(insertTool as ToolDef, opts.cwd),
140+ withHooks(
141+ withCwdDefault(createTool as ToolDef, cwd) as ToolDef<
142+ { cwd?: string },
143+ CreateOutput
144+ >,
145+ lookupHook,
146+ cwd,
147+ (result) => hookEnvForCreate(result, cwd),
148+ ) as ToolDef,
149+ withHooks(
150+ withCwdDefault(replaceTool as ToolDef, cwd) as ToolDef<
151+ { cwd?: string },
152+ ReplaceOutput
153+ >,
154+ lookupHook,
155+ cwd,
156+ (result) => hookEnvForReplace(result, cwd),
157+ ) as ToolDef,
158+ withHooks(
159+ withCwdDefault(insertTool as ToolDef, cwd) as ToolDef<
160+ { cwd?: string },
161+ InsertOutput
162+ >,
163+ lookupHook,
164+ cwd,
165+ (result) => hookEnvForInsert(result, cwd),
166+ ) as ToolDef,
77167 // Shell and subagent
78- withCwdDefault(shellTool as ToolDef, opts.cwd),
168+ withHooks(
169+ withCwdDefault(shellTool as ToolDef, cwd) as ToolDef<
170+ { cwd?: string; command: string },
171+ ShellOutput
172+ >,
173+ lookupHook,
174+ cwd,
175+ (result, input) => hookEnvForShell(result, input, cwd),
176+ ) as ToolDef,
79177 createSubagentTool(async (prompt) => {
80178 if (depth >= MAX_SUBAGENT_DEPTH) {
81179 throw new Error(
@@ -88,10 +186,12 @@ export function buildToolSet(opts: {
88186 ];
89187 }
90188
189+// Plan mode: read-only tools only, no hooks (hooks are a write-tool concern).
91190 export function buildReadOnlyToolSet(opts: { cwd: string }): ToolDef[] {
191+ const { cwd } = opts;
92192 return [
93- withCwdDefault(globTool as ToolDef, opts.cwd),
94- withCwdDefault(grepTool as ToolDef, opts.cwd),
95- withCwdDefault(readTool as ToolDef, opts.cwd),
193+ withCwdDefault(globTool as ToolDef, cwd),
194+ withCwdDefault(grepTool as ToolDef, cwd),
195+ withCwdDefault(readTool as ToolDef, cwd),
96196 ];
97197 }