xonecas.io
64875
Mc

mini-coder / commit e232079

feat: surface tool hook execution in the UI

- runHook now returns Promise<boolean> (true = exit 0, false = failure/error)
- withHooks accepts an onHook callback, called with (toolName, scriptPath, success)
- buildToolSet requires an onHook option, threaded to all hookable tools
- renderHook added to output.ts: shows a success line or a red error with path
- agent.ts passes renderHook as onHook for both the main and subagent tool sets
- HOME cached as a module-level constant in output.ts to avoid per-render syscall
- Tests updated to assert boolean return values from runHook

xonecas2026-02-20 e23207989f6a501be5af136f34d8ffe1a613f0d5 parent 0677d76

5 files changed

src/agent/agent.ts+16−2
@@ -8,12 +8,14 @@ import {
88 PREFIX,
99 Spinner,
1010 renderError,
11+ renderHook,
1112 renderInfo,
1213 renderStatusBar,
1314 renderTurn,
1415 restoreTerminal,
1516 writeln,
1617 } from "../cli/output.ts";
18+
1719 import { getContextWindow, resolveModel } from "../llm-api/providers.ts";
1820 import { type CoreMessage, runTurn } from "../llm-api/turn.ts";
1921 import type { Message, ToolDef } from "../llm-api/types.ts";
@@ -212,7 +214,13 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
212214 depth = 0,
213215 ): Promise<{ result: string; inputTokens: number; outputTokens: number }> => {
214216 const subMessages: Message[] = [{ role: "user", content: prompt }];
215- const subTools = buildToolSet({ cwd, depth, runSubagent });
217+ const subTools = buildToolSet({
218+ cwd,
219+ depth,
220+ runSubagent,
221+ onHook: renderHook,
222+ });
223+
216224 const subLlm = resolveModel(currentModel);
217225
218226 const systemPrompt = buildSystemPrompt(cwd);
@@ -240,7 +248,13 @@ export async function runAgent(opts: AgentOptions): Promise<void> {
240248 };
241249
242250 // ── MCP: load persisted servers and connect them ───────────────────────────
243- const tools: ToolDef[] = buildToolSet({ cwd, depth: 0, runSubagent });
251+ const tools: ToolDef[] = buildToolSet({
252+ cwd,
253+ depth: 0,
254+ runSubagent,
255+ onHook: renderHook,
256+ });
257+
244258 const mcpTools: ToolDef[] = [];
245259
246260 async function connectAndAddMcp(name: string): Promise<void> {
src/agent/tools.ts+14−3
@@ -53,13 +53,15 @@ type EnvBuilder<TInput, TOutput> = (
5353 /**
5454 * Wrap a tool to fire its post-hook (if one exists) after each successful execute.
5555 * Hooks are looked up via the session-scoped cache — no filesystem access per call.
56- * Hooks are fire-and-forget; errors are always swallowed.
56+ * Errors are always swallowed. onHook is called with the result so the caller can
57+ * render a UI line.
5758 */
5859 function withHooks<TInput extends { cwd?: string }, TOutput>(
5960 tool: ToolDef<TInput, TOutput>,
6061 lookupHook: (toolName: string) => string | null,
6162 cwd: string,
6263 buildEnv: EnvBuilder<TInput, TOutput>,
64+ onHook: (toolName: string, scriptPath: string, success: boolean) => void,
6365 ): ToolDef<TInput, TOutput> {
6466 const originalExecute = tool.execute;
6567 return {
@@ -69,7 +71,8 @@ function withHooks<TInput extends { cwd?: string }, TOutput>(
6971 const hookScript = lookupHook(tool.name);
7072 if (hookScript) {
7173 const env = buildEnv(result, input);
72- await runHook(hookScript, env, cwd);
74+ const success = await runHook(hookScript, env, cwd);
75+ onHook(tool.name, hookScript, success);
7376 }
7477 return result;
7578 },
@@ -102,8 +105,9 @@ export function buildToolSet(opts: {
102105 inputTokens: number;
103106 outputTokens: number;
104107 }>;
108+ onHook: (toolName: string, scriptPath: string, success: boolean) => void;
105109 }): ToolDef[] {
106- const { cwd } = opts;
110+ const { cwd, onHook } = opts;
107111 const depth = opts.depth ?? 0;
108112 const lookupHook = createHookCache(HOOKABLE_TOOLS, cwd);
109113
@@ -117,6 +121,7 @@ export function buildToolSet(opts: {
117121 lookupHook,
118122 cwd,
119123 (_, input) => hookEnvForGlob(input, cwd),
124+ onHook,
120125 ) as ToolDef,
121126 withHooks(
122127 withCwdDefault(grepTool as ToolDef, cwd) as ToolDef<
@@ -126,6 +131,7 @@ export function buildToolSet(opts: {
126131 lookupHook,
127132 cwd,
128133 (_, input) => hookEnvForGrep(input, cwd),
134+ onHook,
129135 ) as ToolDef,
130136 withHooks(
131137 withCwdDefault(readTool as ToolDef, cwd) as ToolDef<
@@ -135,6 +141,7 @@ export function buildToolSet(opts: {
135141 lookupHook,
136142 cwd,
137143 (_, input) => hookEnvForRead(input, cwd),
144+ onHook,
138145 ) as ToolDef,
139146 // Write: create/overwrite, replace/delete, insert
140147 withHooks(
@@ -145,6 +152,7 @@ export function buildToolSet(opts: {
145152 lookupHook,
146153 cwd,
147154 (result) => hookEnvForCreate(result, cwd),
155+ onHook,
148156 ) as ToolDef,
149157 withHooks(
150158 withCwdDefault(replaceTool as ToolDef, cwd) as ToolDef<
@@ -154,6 +162,7 @@ export function buildToolSet(opts: {
154162 lookupHook,
155163 cwd,
156164 (result) => hookEnvForReplace(result, cwd),
165+ onHook,
157166 ) as ToolDef,
158167 withHooks(
159168 withCwdDefault(insertTool as ToolDef, cwd) as ToolDef<
@@ -163,6 +172,7 @@ export function buildToolSet(opts: {
163172 lookupHook,
164173 cwd,
165174 (result) => hookEnvForInsert(result, cwd),
175+ onHook,
166176 ) as ToolDef,
167177 // Shell and subagent
168178 withHooks(
@@ -173,6 +183,7 @@ export function buildToolSet(opts: {
173183 lookupHook,
174184 cwd,
175185 (result, input) => hookEnvForShell(result, input, cwd),
186+ onHook,
176187 ) as ToolDef,
177188 createSubagentTool(async (prompt) => {
178189 if (depth >= MAX_SUBAGENT_DEPTH) {
src/cli/output.ts+19−0
@@ -1,7 +1,10 @@
1+import { homedir } from "node:os";
12 import * as c from "yoctocolors";
23 import type { TurnEvent } from "../llm-api/types.ts";
34 import { renderMarkdown } from "./markdown.ts";
45
6+const HOME = homedir();
7+
58 // ─── Terminal restore ─────────────────────────────────────────────────────────
69
710 export function restoreTerminal(): void {
@@ -191,6 +194,22 @@ export function renderToolCall(toolName: string, args: unknown): void {
191194 writeln(` ${toolCallLine(toolName, args)}`);
192195 }
193196
197+export function renderHook(
198+ toolName: string,
199+ scriptPath: string,
200+ success: boolean,
201+): void {
202+ const short = scriptPath.replace(HOME, "~");
203+
204+ if (success) {
205+ writeln(` ${G.ok} ${c.dim(`hook post-${toolName}`)}`);
206+ } else {
207+ writeln(
208+ ` ${G.err} ${c.red(`hook post-${toolName} failed`)} ${c.dim(short)}`,
209+ );
210+ }
211+}
212+
194213 // ─── Tool result — compact data ───────────────────────────────────────────────
195214
196215 export function renderToolResult(
src/tools/hooks.test.ts+10−10
@@ -65,23 +65,23 @@ describe("createHookCache", () => {
6565 // ─── runHook ──────────────────────────────────────────────────────────────────
6666
6767 describe("runHook", () => {
68- it("completes without throwing when the script exits zero", async () => {
68+ it("returns true when the script exits zero", async () => {
6969 const script = makeHook(tmpDir, "post-create", "#!/bin/bash\nexit 0");
70- await expect(
71- runHook(script, { TOOL: "create" }, tmpDir),
72- ).resolves.toBeUndefined();
70+ await expect(runHook(script, { TOOL: "create" }, tmpDir)).resolves.toBe(
71+ true,
72+ );
7373 });
7474
75- it("completes without throwing when the script exits non-zero", async () => {
75+ it("returns false when the script exits non-zero", async () => {
7676 const script = makeHook(tmpDir, "post-replace", "#!/bin/bash\nexit 1");
77- await expect(
78- runHook(script, { TOOL: "replace" }, tmpDir),
79- ).resolves.toBeUndefined();
77+ await expect(runHook(script, { TOOL: "replace" }, tmpDir)).resolves.toBe(
78+ false,
79+ );
8080 });
8181
82- it("completes without throwing when the script path does not exist", async () => {
82+ it("returns false when the script path does not exist", async () => {
8383 const missing = join(tmpDir, ".agents", "hooks", "post-missing");
84- await expect(runHook(missing, {}, tmpDir)).resolves.toBeUndefined();
84+ await expect(runHook(missing, {}, tmpDir)).resolves.toBe(false);
8585 });
8686
8787 it("passes env vars to the hook script", async () => {
src/tools/hooks.ts+4−2
@@ -51,7 +51,7 @@ export async function runHook(
5151 scriptPath: string,
5252 env: Record<string, string>,
5353 cwd: string,
54-): Promise<void> {
54+): Promise<boolean> {
5555 try {
5656 const proc = Bun.spawn(["bash", scriptPath], {
5757 cwd,
@@ -59,9 +59,11 @@ export async function runHook(
5959 stdout: "ignore",
6060 stderr: "ignore",
6161 });
62- await proc.exited;
62+ const code = await proc.exited;
63+ return code === 0;
6364 } catch {
6465 // Hooks must not crash the agent
66+ return false;
6567 }
6668 }