@@ -54,6 +54,26 @@ function withCwdDefault(
5454 };
5555 }
5656
57+/**
58+ * Inject an onOutput streaming callback into the shell tool's input.
59+ */
60+function withShellOutput(
61+ tool: ToolDef,
62+ onOutput: (chunk: string) => void,
63+): ToolDef {
64+ const originalExecute = tool.execute;
65+ return {
66+ ...tool,
67+ execute: async (input: unknown) => {
68+ const patched = {
69+ ...(typeof input === "object" && input !== null ? input : {}),
70+ onOutput,
71+ } as Record<string, unknown>;
72+ return originalExecute(patched);
73+ },
74+ };
75+}
76+
5777 type EnvBuilder<TInput, TOutput> = (
5878 result: TOutput,
5979 input: TInput,
@@ -67,7 +87,8 @@ type ResultFinalizer<TOutput, TFinalOutput> = (
6787 * Wrap a tool to fire its post-hook (if one exists) after each successful execute.
6888 * Hooks are looked up via the session-scoped cache — no filesystem access per call.
6989 * Errors are always swallowed. onHook is called with the result so the caller can
70- * render a UI line.
90+ * render a UI line. onBeforeHook is called just before the hook script runs so the
91+ * UI can update the spinner label.
7192 */
7293 function withHooks<
7394 TInput extends { cwd?: string },
@@ -80,6 +101,7 @@ function withHooks<
80101 buildEnv: EnvBuilder<TInput, TOutput>,
81102 onHook: (toolName: string, scriptPath: string, success: boolean) => void,
82103 finalizeResult?: ResultFinalizer<TOutput, TFinalOutput>,
104+ onBeforeHook?: (toolName: string, scriptPath: string) => void,
83105 ): ToolDef<TInput, TFinalOutput> {
84106 const originalExecute = tool.execute;
85107 return {
@@ -88,6 +110,7 @@ function withHooks<
88110 const result = await originalExecute(input);
89111 const hookScript = lookupHook(tool.name);
90112 if (hookScript) {
113+ onBeforeHook?.(tool.name, hookScript);
91114 const env = buildEnv(result, input);
92115 const success = await runHook(hookScript, env, cwd);
93116 onHook(tool.name, hookScript, success);
@@ -118,10 +141,13 @@ export function buildToolSet(opts: {
118141 ) => Promise<SubagentOutput>;
119142
120143 onHook: (toolName: string, scriptPath: string, success: boolean) => void;
144+ onBeforeHook?: (toolName: string, scriptPath: string) => void;
145+ onShellOutput?: (chunk: string) => void;
121146 availableAgents: ReadonlyMap<string, { description: string }>;
122147 snapshotCallback?: (filePath: string) => Promise<void>;
123148 }): ToolDef[] {
124- const { cwd, onHook } = opts;
149+ const { cwd, onHook, onBeforeHook, onShellOutput } = opts;
150+
125151 const lookupHook = createHookCache(HOOKABLE_TOOLS, cwd);
126152
127153 const tools: ToolDef[] = [
@@ -135,6 +161,8 @@ export function buildToolSet(opts: {
135161 cwd,
136162 (_, input) => hookEnvForRead(input, cwd),
137163 onHook,
164+ undefined,
165+ onBeforeHook,
138166 ) as ToolDef,
139167 withCwdDefault(listSkillsTool as ToolDef, cwd),
140168 withCwdDefault(readSkillTool as ToolDef, cwd),
@@ -150,6 +178,7 @@ export function buildToolSet(opts: {
150178 (result) => hookEnvForCreate(result, cwd),
151179 onHook,
152180 finalizeWriteResult,
181+ onBeforeHook,
153182 ) as ToolDef,
154183 withHooks(
155184 withCwdDefault(
@@ -162,6 +191,7 @@ export function buildToolSet(opts: {
162191 (result) => hookEnvForReplace(result, cwd),
163192 onHook,
164193 finalizeWriteResult,
194+ onBeforeHook,
165195 ) as ToolDef,
166196 withHooks(
167197 withCwdDefault(
@@ -174,10 +204,16 @@ export function buildToolSet(opts: {
174204 (result) => hookEnvForInsert(result, cwd),
175205 onHook,
176206 finalizeWriteResult,
207+ onBeforeHook,
177208 ) as ToolDef,
178209 // Shell and subagent
179210 withHooks(
180- withCwdDefault(shellTool as ToolDef, cwd) as ToolDef<
211+ (onShellOutput
212+ ? withShellOutput(
213+ withCwdDefault(shellTool as ToolDef, cwd),
214+ onShellOutput,
215+ )
216+ : withCwdDefault(shellTool as ToolDef, cwd)) as ToolDef<
181217 { cwd?: string; command: string },
182218 ShellOutput
183219 >,
@@ -185,7 +221,10 @@ export function buildToolSet(opts: {
185221 cwd,
186222 (result, input) => hookEnvForShell(result, input, cwd),
187223 onHook,
224+ undefined,
225+ onBeforeHook,
188226 ) as ToolDef,
227+
189228 createSubagentTool(async (prompt, agentName) => {
190229 const output = await opts.runSubagent(prompt, agentName, undefined);
191230 return output;