@@ -1,5 +1,5 @@
11 import type { ToolDef } from "../llm-api/types.ts";
2-import type { CreateOutput } from "../tools/create.ts";
2+import type { CreateToolOutput } from "../tools/create.ts";
33 import { createTool } from "../tools/create.ts";
44 import { webContentTool, webSearchTool } from "../tools/exa.ts";
55 import type { GlobOutput } from "../tools/glob.ts";
@@ -17,11 +17,11 @@ import {
1717 hookEnvForShell,
1818 runHook,
1919 } from "../tools/hooks.ts";
20-import type { InsertOutput } from "../tools/insert.ts";
20+import type { InsertToolOutput } from "../tools/insert.ts";
2121 import { insertTool } from "../tools/insert.ts";
2222 import type { ReadOutput } from "../tools/read.ts";
2323 import { readTool } from "../tools/read.ts";
24-import type { ReplaceOutput } from "../tools/replace.ts";
24+import type { ReplaceToolOutput } from "../tools/replace.ts";
2525 import { replaceTool } from "../tools/replace.ts";
2626 import type { ShellOutput } from "../tools/shell.ts";
2727 import { shellTool } from "../tools/shell.ts";
@@ -30,6 +30,10 @@ import {
3030 createSubagentTool,
3131 type SubagentOutput,
3232 } from "../tools/subagent.ts";
33+import {
34+ finalizeWriteResult,
35+ stripWriteResultMeta,
36+} from "../tools/write-result.ts";
3337
3438 // ─── Tool wrappers ────────────────────────────────────────────────────────────
3539
@@ -55,19 +59,28 @@ type EnvBuilder<TInput, TOutput> = (
5559 input: TInput,
5660 ) => Record<string, string>;
5761
62+type ResultFinalizer<TOutput, TFinalOutput> = (
63+ result: TOutput,
64+) => Promise<TFinalOutput>;
65+
5866 /**
5967 * Wrap a tool to fire its post-hook (if one exists) after each successful execute.
6068 * Hooks are looked up via the session-scoped cache — no filesystem access per call.
6169 * Errors are always swallowed. onHook is called with the result so the caller can
6270 * render a UI line.
6371 */
64-function withHooks<TInput extends { cwd?: string }, TOutput>(
72+function withHooks<
73+ TInput extends { cwd?: string },
74+ TOutput,
75+ TFinalOutput = TOutput,
76+>(
6577 tool: ToolDef<TInput, TOutput>,
6678 lookupHook: (toolName: string) => string | null,
6779 cwd: string,
6880 buildEnv: EnvBuilder<TInput, TOutput>,
6981 onHook: (toolName: string, scriptPath: string, success: boolean) => void,
70-): ToolDef<TInput, TOutput> {
82+ finalizeResult?: ResultFinalizer<TOutput, TFinalOutput>,
83+): ToolDef<TInput, TFinalOutput> {
7184 const originalExecute = tool.execute;
7285 return {
7386 ...tool,
@@ -78,8 +91,15 @@ function withHooks<TInput extends { cwd?: string }, TOutput>(
7891 const env = buildEnv(result, input);
7992 const success = await runHook(hookScript, env, cwd);
8093 onHook(tool.name, hookScript, success);
94+ if (finalizeResult) return finalizeResult(result);
95+ return result as TFinalOutput;
96+ }
97+ if (finalizeResult) {
98+ return stripWriteResultMeta(
99+ result as TOutput & { _filePath: string; _before: string },
100+ ) as TFinalOutput;
81101 }
82- return result;
102+ return result as TFinalOutput;
83103 },
84104 };
85105 }
@@ -149,32 +169,35 @@ export function buildToolSet(opts: {
149169 withHooks(
150170 withCwdDefault(createTool as ToolDef, cwd) as ToolDef<
151171 { cwd?: string },
152- CreateOutput
172+ CreateToolOutput
153173 >,
154174 lookupHook,
155175 cwd,
156176 (result) => hookEnvForCreate(result, cwd),
157177 onHook,
178+ finalizeWriteResult,
158179 ) as ToolDef,
159180 withHooks(
160181 withCwdDefault(replaceTool as ToolDef, cwd) as ToolDef<
161182 { cwd?: string },
162- ReplaceOutput
183+ ReplaceToolOutput
163184 >,
164185 lookupHook,
165186 cwd,
166187 (result) => hookEnvForReplace(result, cwd),
167188 onHook,
189+ finalizeWriteResult,
168190 ) as ToolDef,
169191 withHooks(
170192 withCwdDefault(insertTool as ToolDef, cwd) as ToolDef<
171193 { cwd?: string },
172- InsertOutput
194+ InsertToolOutput
173195 >,
174196 lookupHook,
175197 cwd,
176198 (result) => hookEnvForInsert(result, cwd),
177199 onHook,
200+ finalizeWriteResult,
178201 ) as ToolDef,
179202 // Shell and subagent
180203 withHooks(