Skip to content

Commit 13bc324

Browse files
terisukeclaude
andcommitted
feat(hook): add fact-checking PreToolUse hook for Write/Edit tools (#53)
Advisory hook that warns when Write/Edit tools are invoked without evidence of prior file reading. The hook checks tool input for research indicators (grep results, line references, etc.) and emits a stderr warning that gets injected into LLM context. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 128bc20 commit 13bc324

3 files changed

Lines changed: 128 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/sh
2+
# enforce-factcheck-before-edit.sh
3+
# PreToolUse hook: warns when Write/Edit tools are invoked without evidence of prior research
4+
# Exit 0 = pass (advisory only), stderr message becomes hook context
5+
6+
TOOL="$OPENCODE_TOOL_NAME"
7+
INPUT="$OPENCODE_TOOL_INPUT"
8+
9+
# Only apply to write/edit tools
10+
case "$TOOL" in
11+
write|Write|edit|Edit) ;;
12+
*) exit 0 ;;
13+
esac
14+
15+
# Check if tool input contains evidence of prior file reading
16+
if echo "$INPUT" | grep -qiE '(based on reading|as shown in|from the file|grep result|read tool output|line [0-9]+)'; then
17+
echo "FACT-CHECK: Evidence of prior research detected" >&2
18+
exit 0
19+
fi
20+
21+
echo "FACT-CHECK WARNING: File edit attempted without evidence of reading the target file first. Best practice: read the file before editing." >&2
22+
exit 0

‎.opencode/opencode.jsonc‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,16 @@
122122
"github-triage": false,
123123
"github-pr-search": false,
124124
},
125+
"hooks": {
126+
"PreToolUse": [
127+
{
128+
"command": ".opencode/hooks/enforce-factcheck-before-edit.sh",
129+
"matcher": "write",
130+
},
131+
{
132+
"command": ".opencode/hooks/enforce-factcheck-before-edit.sh",
133+
"matcher": "edit",
134+
},
135+
],
136+
},
125137
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import path from "node:path"
2+
import { describe, expect, test } from "bun:test"
3+
import { runHook, type HookEnv } from "../../src/hook"
4+
5+
const SCRIPT_PATH = path.resolve(
6+
import.meta.dir,
7+
"../../../../.opencode/hooks/enforce-factcheck-before-edit.sh",
8+
)
9+
10+
function makeEnv(toolName: string, toolInput: string): HookEnv {
11+
return {
12+
OPENCODE_HOOK_EVENT: "PreToolUse",
13+
OPENCODE_TOOL_NAME: toolName,
14+
OPENCODE_TOOL_INPUT: toolInput,
15+
OPENCODE_PROJECT_DIR: "/tmp/test",
16+
OPENCODE_SESSION_ID: "test-session",
17+
}
18+
}
19+
20+
describe("enforce-factcheck-before-edit", () => {
21+
test("passes non-edit tools without message", async () => {
22+
const result = await runHook({ command: SCRIPT_PATH }, makeEnv("bash", "{}"))
23+
expect(result.action).toBe("pass")
24+
expect(result.message).toBeUndefined()
25+
})
26+
27+
test("passes non-edit tool 'read'", async () => {
28+
const result = await runHook({ command: SCRIPT_PATH }, makeEnv("read", '{"file":"/tmp/x"}'))
29+
expect(result.action).toBe("pass")
30+
expect(result.message).toBeUndefined()
31+
})
32+
33+
test("warns on Write without evidence", async () => {
34+
const result = await runHook(
35+
{ command: SCRIPT_PATH },
36+
makeEnv("Write", '{"file_path":"/tmp/test.ts","content":"hello"}'),
37+
)
38+
expect(result.action).toBe("pass")
39+
expect(result.message).toContain("FACT-CHECK WARNING")
40+
})
41+
42+
test("warns on edit without evidence", async () => {
43+
const result = await runHook(
44+
{ command: SCRIPT_PATH },
45+
makeEnv("edit", '{"old_string":"foo","new_string":"bar"}'),
46+
)
47+
expect(result.action).toBe("pass")
48+
expect(result.message).toContain("FACT-CHECK WARNING")
49+
})
50+
51+
test("passes Edit with evidence phrase 'based on reading'", async () => {
52+
const result = await runHook(
53+
{ command: SCRIPT_PATH },
54+
makeEnv("Edit", '{"old_string":"based on reading the config"}'),
55+
)
56+
expect(result.action).toBe("pass")
57+
expect(result.message).toContain("Evidence of prior research detected")
58+
})
59+
60+
test("passes Write with evidence phrase 'line 42'", async () => {
61+
const result = await runHook(
62+
{ command: SCRIPT_PATH },
63+
makeEnv("Write", '{"content":"fix at line 42"}'),
64+
)
65+
expect(result.action).toBe("pass")
66+
expect(result.message).toContain("Evidence of prior research detected")
67+
})
68+
69+
test("passes write with evidence phrase 'grep result'", async () => {
70+
const result = await runHook(
71+
{ command: SCRIPT_PATH },
72+
makeEnv("write", '{"content":"grep result shows usage"}'),
73+
)
74+
expect(result.action).toBe("pass")
75+
expect(result.message).toContain("Evidence of prior research detected")
76+
})
77+
78+
test("passes Edit with evidence phrase 'read tool output'", async () => {
79+
const result = await runHook(
80+
{ command: SCRIPT_PATH },
81+
makeEnv("Edit", '{"old_string":"per read tool output"}'),
82+
)
83+
expect(result.action).toBe("pass")
84+
expect(result.message).toContain("Evidence of prior research detected")
85+
})
86+
87+
test("never blocks (advisory only)", async () => {
88+
const result = await runHook(
89+
{ command: SCRIPT_PATH },
90+
makeEnv("Write", '{"file_path":"/tmp/x","content":"no evidence here"}'),
91+
)
92+
expect(result.action).toBe("pass")
93+
})
94+
})

0 commit comments

Comments
 (0)