File size: 2,716 Bytes
b4c665d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Hooks Examples (Unit 5)

This directory demonstrates hook patterns across Claude Code, Codex, OpenCode, and Pi.

## Hook Lifecycle

```
User prompt
    β”‚
    β”œβ”€[UserPromptSubmit hook]─► log / inject context
    β–Ό
Model reasoning
    β”‚
    β”œβ”€[PreToolUse hook]─► allow, deny, rewrite args
    β–Ό
Tool executes
    β”‚
    β”œβ”€[PostToolUse hook]─► log, analyze, post-process
    β–Ό
Model continues
    β”‚
    └─[Stop / SessionEnd hooks]─► persist, notify, tear down
```

## Claude Code β€” `.claude/settings.json`

```json
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "python .claude/hooks/guard_bash.py"
          }
        ]
      }
    ],
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "python .claude/hooks/run_linter.py"
          }
        ]
      }
    ],
    "Stop": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "python .claude/hooks/notify_done.py"
          }
        ]
      }
    ]
  }
}
```

## OpenCode β€” `.opencode/plugins/activity-logger.ts`

```typescript
export default {
  "tool.execute.before": async (input, output) => {
    console.log(`[HOOK] Before tool: ${input.tool}`);
    // Block dangerous commands
    if (input.tool === "bash" && input.args?.command?.includes("rm -rf")) {
      throw new Error("Blocked: dangerous command");
    }
  },
  "tool.execute.after": async (input, output) => {
    console.log(`[HOOK] After tool: ${input.tool} -> ${output.result?.slice(0, 100)}`);
  },
  "event": async ({ event }) => {
    if (event.type === "session.idle") {
      console.log("[HOOK] Session ended");
    }
  }
};
```

## Guardrail Example β€” `.claude/hooks/guard_bash.py`

```python
import json, sys

payload = json.load(sys.stdin)
command = payload.get("tool_input", {}).get("command", "")

BLOCKED = ["rm -rf /", "rm -rf ~", "~/.ssh/", "chmod 777"]
for pattern in BLOCKED:
    if pattern in command:
        print(json.dumps({"decision": "deny", "reason": f"Blocked: {pattern}"}))
        sys.exit(0)

print(json.dumps({"decision": "allow"}))
```

## Three Patterns

1. **Observability** β€” Log every tool call, prompt, and stop event to a dashboard.
2. **Guardrails** β€” Inspect commands before execution; deny unsafe actions.
3. **Automation** β€” Run linters, formatters, tests automatically after file edits.

## Agent Activity Dashboard

A Gradio app that receives hook events over HTTP and visualizes tool calls,
prompts, and sessions in real time. See `hooks/dashboard.py`.