Skip to content

Commit b01ee2b

Browse files
terisukeclaude
andcommitted
fix(hook): inject stderr context from pass hooks into tool output
When hooks exit 0 with stderr messages, the context was being discarded at all 4 hook call sites in prompt.ts. This adds <hook-context> wrapping to inject PreToolUse messages as prefix and PostToolUse messages as suffix into tool output for both built-in and MCP tools, fulfilling Issue #47 acceptance criteria for "exit 0: stderr context injection". Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 128bc20 commit b01ee2b

2 files changed

Lines changed: 204 additions & 1 deletion

File tree

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,11 @@ NOTE: At any point in time through this workflow you should feel free to ask the
503503
output,
504504
)
505505

506+
// Inject PreToolUse hook context into output
507+
if (hookResult.message) {
508+
output.output = `<hook-context>\n${hookResult.message}\n</hook-context>\n\n${output.output}`
509+
}
510+
506511
// PostToolUse hooks
507512
const postHookResult = yield* Effect.promise(() =>
508513
runHooks(hookCfg.hooks?.PostToolUse, item.id, {
@@ -519,6 +524,11 @@ NOTE: At any point in time through this workflow you should feel free to ask the
519524
}
520525
}
521526

527+
// Inject PostToolUse hook context into output
528+
if (postHookResult.message) {
529+
output.output = `${output.output}\n\n<hook-context>\n${postHookResult.message}\n</hook-context>`
530+
}
531+
522532
return output
523533
}),
524534
)
@@ -621,10 +631,19 @@ NOTE: At any point in time through this workflow you should feel free to ask the
621631
...(truncated.truncated && { outputPath: truncated.outputPath }),
622632
}
623633

634+
// Inject hook context from PreToolUse and PostToolUse
635+
let mcpOutput = truncated.content
636+
if (mcpHookResult.message) {
637+
mcpOutput = `<hook-context>\n${mcpHookResult.message}\n</hook-context>\n\n${mcpOutput}`
638+
}
639+
if (mcpPostResult.message) {
640+
mcpOutput = `${mcpOutput}\n\n<hook-context>\n${mcpPostResult.message}\n</hook-context>`
641+
}
642+
624643
return {
625644
title: "",
626645
metadata,
627-
output: truncated.content,
646+
output: mcpOutput,
628647
attachments: attachments.map((attachment) => ({
629648
...attachment,
630649
id: PartID.ascending(),
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { runHooks, type HookEnv } from "../../src/hook"
3+
import type { HookEntry } from "../../src/hook"
4+
5+
function makeEnv(overrides?: Partial<HookEnv>): HookEnv {
6+
return {
7+
OPENCODE_HOOK_EVENT: "PreToolUse",
8+
OPENCODE_TOOL_NAME: "bash",
9+
OPENCODE_TOOL_INPUT: "{}",
10+
OPENCODE_PROJECT_DIR: "/tmp/test",
11+
OPENCODE_SESSION_ID: "test-session-id",
12+
...overrides,
13+
}
14+
}
15+
16+
/**
17+
* Applies PreToolUse hook context injection to built-in tool output.
18+
* Mirrors the logic in prompt.ts Site 1.
19+
*/
20+
function injectPreToolUseContext(
21+
hookMessage: string | undefined,
22+
originalOutput: string,
23+
): string {
24+
if (hookMessage) {
25+
return `<hook-context>\n${hookMessage}\n</hook-context>\n\n${originalOutput}`
26+
}
27+
return originalOutput
28+
}
29+
30+
/**
31+
* Applies PostToolUse hook context injection to built-in tool output.
32+
* Mirrors the logic in prompt.ts Site 2.
33+
*/
34+
function injectPostToolUseContext(
35+
hookMessage: string | undefined,
36+
currentOutput: string,
37+
): string {
38+
if (hookMessage) {
39+
return `${currentOutput}\n\n<hook-context>\n${hookMessage}\n</hook-context>`
40+
}
41+
return currentOutput
42+
}
43+
44+
describe("hook context injection", () => {
45+
describe("PreToolUse pass-with-message injects <hook-context>", () => {
46+
test("stderr message from exit 0 produces hook-context prefix", async () => {
47+
const entries: HookEntry[] = [
48+
{ command: 'echo "security warning: path is sensitive" >&2; exit 0' },
49+
]
50+
const result = await runHooks(entries, "bash", makeEnv())
51+
52+
expect(result.action).toBe("pass")
53+
expect(result.message).toBe("security warning: path is sensitive")
54+
55+
const toolOutput = "file contents here"
56+
const injected = injectPreToolUseContext(result.message, toolOutput)
57+
expect(injected).toBe(
58+
`<hook-context>\nsecurity warning: path is sensitive\n</hook-context>\n\n${toolOutput}`,
59+
)
60+
})
61+
62+
test("multiple hook messages are joined and wrapped", async () => {
63+
const entries: HookEntry[] = [
64+
{ command: 'echo "check-1: ok" >&2; exit 0' },
65+
{ command: 'echo "check-2: warning" >&2; exit 0' },
66+
]
67+
const result = await runHooks(entries, "bash", makeEnv())
68+
69+
expect(result.action).toBe("pass")
70+
expect(result.message).toBe("check-1: ok\ncheck-2: warning")
71+
72+
const toolOutput = "tool result"
73+
const injected = injectPreToolUseContext(result.message, toolOutput)
74+
expect(injected).toContain("<hook-context>")
75+
expect(injected).toContain("check-1: ok\ncheck-2: warning")
76+
expect(injected).toEndWith(toolOutput)
77+
})
78+
})
79+
80+
describe("pass-without-message leaves output unchanged", () => {
81+
test("exit 0 without stderr produces no injection", async () => {
82+
const entries: HookEntry[] = [{ command: "exit 0" }]
83+
const result = await runHooks(entries, "bash", makeEnv())
84+
85+
expect(result.action).toBe("pass")
86+
expect(result.message).toBeUndefined()
87+
88+
const toolOutput = "original output"
89+
const injected = injectPreToolUseContext(result.message, toolOutput)
90+
expect(injected).toBe(toolOutput)
91+
})
92+
93+
test("empty entries produce no injection", async () => {
94+
const result = await runHooks([], "bash", makeEnv())
95+
96+
expect(result.action).toBe("pass")
97+
expect(result.message).toBeUndefined()
98+
99+
const toolOutput = "original output"
100+
const injected = injectPreToolUseContext(result.message, toolOutput)
101+
expect(injected).toBe(toolOutput)
102+
})
103+
})
104+
105+
describe("block still works as before", () => {
106+
test("exit 2 returns block and does not reach injection", async () => {
107+
const entries: HookEntry[] = [
108+
{ command: 'echo "forbidden" >&2; exit 2' },
109+
]
110+
const result = await runHooks(entries, "bash", makeEnv())
111+
112+
expect(result.action).toBe("block")
113+
expect(result.message).toBe("forbidden")
114+
115+
// In prompt.ts, block returns early before injection code runs.
116+
// Verify the block message is usable as-is for the error output.
117+
expect(result.message).not.toContain("<hook-context>")
118+
})
119+
})
120+
121+
describe("PostToolUse context injection", () => {
122+
test("post-hook message is appended as suffix", async () => {
123+
const entries: HookEntry[] = [
124+
{ command: 'echo "audit: file was read" >&2; exit 0' },
125+
]
126+
const result = await runHooks(
127+
entries,
128+
"bash",
129+
makeEnv({ OPENCODE_HOOK_EVENT: "PostToolUse" }),
130+
)
131+
132+
expect(result.action).toBe("pass")
133+
expect(result.message).toBe("audit: file was read")
134+
135+
const toolOutput = "file contents here"
136+
const injected = injectPostToolUseContext(result.message, toolOutput)
137+
expect(injected).toBe(
138+
`${toolOutput}\n\n<hook-context>\naudit: file was read\n</hook-context>`,
139+
)
140+
})
141+
142+
test("post-hook without message leaves output unchanged", async () => {
143+
const entries: HookEntry[] = [{ command: "exit 0" }]
144+
const result = await runHooks(
145+
entries,
146+
"bash",
147+
makeEnv({ OPENCODE_HOOK_EVENT: "PostToolUse" }),
148+
)
149+
150+
expect(result.action).toBe("pass")
151+
expect(result.message).toBeUndefined()
152+
153+
const toolOutput = "original output"
154+
const injected = injectPostToolUseContext(result.message, toolOutput)
155+
expect(injected).toBe(toolOutput)
156+
})
157+
})
158+
159+
describe("combined Pre + Post injection", () => {
160+
test("both hooks inject context around tool output", async () => {
161+
const preEntries: HookEntry[] = [
162+
{ command: 'echo "pre-context: checking" >&2; exit 0' },
163+
]
164+
const postEntries: HookEntry[] = [
165+
{ command: 'echo "post-context: logged" >&2; exit 0' },
166+
]
167+
168+
const preResult = await runHooks(preEntries, "bash", makeEnv())
169+
const postResult = await runHooks(
170+
postEntries,
171+
"bash",
172+
makeEnv({ OPENCODE_HOOK_EVENT: "PostToolUse" }),
173+
)
174+
175+
let output = "tool execution result"
176+
output = injectPreToolUseContext(preResult.message, output)
177+
output = injectPostToolUseContext(postResult.message, output)
178+
179+
expect(output).toStartWith("<hook-context>\npre-context: checking\n</hook-context>")
180+
expect(output).toContain("tool execution result")
181+
expect(output).toEndWith("<hook-context>\npost-context: logged\n</hook-context>")
182+
})
183+
})
184+
})

0 commit comments

Comments
 (0)