♻️ Quality: internal/hooks without smells (3 → 0)
- checkAction with an actionEvents table (which decision is valid for which event) - PreToolCall's per-hook step as preToolHook (deny, failure, allow, input) - tools.go: the tool-call events Checked: go test (deny, input rewrite, allow then a later deny, allow in an asked agent, invalid actions, failing hooks, postToolCall context). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
f0939b6 parent: 5a47e54 modified
internal/hooks/exec.go +13 -12 | @@ -8,6 +8,7 @@ import ( | ||
| 8 | 8 | "fmt" |
| 9 | 9 | "os" |
| 10 | 10 | "os/exec" |
| 11 | + "slices" | |
| 11 | 12 | "strings" |
| 12 | 13 | "time" |
| 13 | 14 | |
| @@ -110,22 +111,22 @@ func checkAction(reply Reply, event string) error { | ||
| 110 | 111 | return errors.New("the hook reported an error") |
| 111 | 112 | } |
| 112 | 113 | return errors.New(reply.Error) |
| 113 | - case ActionStop: | |
| 114 | - if event == config.HookPreGeneration { | |
| 115 | - return nil | |
| 116 | - } | |
| 117 | - case ActionDeny: | |
| 118 | - if event == config.HookPreToolCall || event == config.HookPreAsk { | |
| 119 | - return nil | |
| 120 | - } | |
| 121 | - case ActionAllow: | |
| 122 | - if event == config.HookPreToolCall { | |
| 123 | - return nil | |
| 124 | - } | |
| 114 | + } | |
| 115 | + if slices.Contains(actionEvents[reply.Action], event) { | |
| 116 | + return nil | |
| 125 | 117 | } |
| 126 | 118 | return fmt.Errorf("action %q is not valid for %s", reply.Action, event) |
| 127 | 119 | } |
| 128 | 120 | |
| 121 | +// actionEvents are the events each decision is valid for: stop ends a | |
| 122 | +// question before generation, deny refuses a tool call or a hand-off, allow | |
| 123 | +// approves a tool call. | |
| 124 | +var actionEvents = map[string][]string{ | |
| 125 | + ActionStop: {config.HookPreGeneration}, | |
| 126 | + ActionDeny: {config.HookPreToolCall, config.HookPreAsk}, | |
| 127 | + ActionAllow: {config.HookPreToolCall}, | |
| 128 | +} | |
| 129 | + | |
| 129 | 130 | // runHook runs a hook the way its entry says: a module in the wasm sandbox, |
| 130 | 131 | // or a command through the shell. |
| 131 | 132 | func runHook(ctx context.Context, h config.Hook, env []string, dir string, stdin []byte) ([]byte, error) { |
| @@ -8,6 +8,7 @@ import ( | |||
| 8 | "fmt" | 8 | "fmt" |
| 9 | "os" | 9 | "os" |
| 10 | "os/exec" | 10 | "os/exec" |
| 11 | + "slices" | ||
| 11 | "strings" | 12 | "strings" |
| 12 | "time" | 13 | "time" |
| 13 | 14 | ||
| @@ -110,22 +111,22 @@ func checkAction(reply Reply, event string) error { | |||
| 110 | return errors.New("the hook reported an error") | 111 | return errors.New("the hook reported an error") |
| 111 | } | 112 | } |
| 112 | return errors.New(reply.Error) | 113 | return errors.New(reply.Error) |
| 113 | - case ActionStop: | 114 | + } |
| 114 | - if event == config.HookPreGeneration { | 115 | + if slices.Contains(actionEvents[reply.Action], event) { |
| 115 | - return nil | 116 | + return nil |
| 116 | - } | ||
| 117 | - case ActionDeny: | ||
| 118 | - if event == config.HookPreToolCall || event == config.HookPreAsk { | ||
| 119 | - return nil | ||
| 120 | - } | ||
| 121 | - case ActionAllow: | ||
| 122 | - if event == config.HookPreToolCall { | ||
| 123 | - return nil | ||
| 124 | - } | ||
| 125 | } | 117 | } |
| 126 | return fmt.Errorf("action %q is not valid for %s", reply.Action, event) | 118 | return fmt.Errorf("action %q is not valid for %s", reply.Action, event) |
| 127 | } | 119 | } |
| 128 | 120 | ||
| 121 | +// actionEvents are the events each decision is valid for: stop ends a | ||
| 122 | +// question before generation, deny refuses a tool call or a hand-off, allow | ||
| 123 | +// approves a tool call. | ||
| 124 | +var actionEvents = map[string][]string{ | ||
| 125 | + ActionStop: {config.HookPreGeneration}, | ||
| 126 | + ActionDeny: {config.HookPreToolCall, config.HookPreAsk}, | ||
| 127 | + ActionAllow: {config.HookPreToolCall}, | ||
| 128 | +} | ||
| 129 | + | ||
| 129 | // runHook runs a hook the way its entry says: a module in the wasm sandbox, | 130 | // runHook runs a hook the way its entry says: a module in the wasm sandbox, |
| 130 | // or a command through the shell. | 131 | // or a command through the shell. |
| 131 | func runHook(ctx context.Context, h config.Hook, env []string, dir string, stdin []byte) ([]byte, error) { | 132 | func runHook(ctx context.Context, h config.Hook, env []string, dir string, stdin []byte) ([]byte, error) { |
modified
internal/hooks/hooks.go +0 -77 | @@ -207,83 +207,6 @@ func stopped(d PromptDecision, reason string, response ...string) PromptDecision | ||
| 207 | 207 | return d |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | -// ToolDecision is what the preToolCall chain decided. | |
| 211 | -type ToolDecision struct { | |
| 212 | - // Input is the call's arguments: the model's, or the last replacement. | |
| 213 | - Input any | |
| 214 | - // Denied means the tool must not run; Reason is handed to the model. | |
| 215 | - Denied bool | |
| 216 | - Reason string | |
| 217 | - // Allowed means a hook approved the call: it runs without asking the | |
| 218 | - // user (consent.WithApproval). Never set with Denied. | |
| 219 | - Allowed bool | |
| 220 | -} | |
| 221 | - | |
| 222 | -// PreToolCall runs the preToolCall hooks whose matcher accepts the tool. Each | |
| 223 | -// one sees the input as the previous one left it; the first deny ends the | |
| 224 | -// chain. An allow does not: the hooks after it still run, and may deny. | |
| 225 | -func (r *Runner) PreToolCall(ctx context.Context, in Input) ToolDecision { | |
| 226 | - in.Event = config.HookPreToolCall | |
| 227 | - d := ToolDecision{Input: in.Tool.Input} | |
| 228 | - if r == nil { | |
| 229 | - return d | |
| 230 | - } | |
| 231 | - // A copy: the chain rewrites the input it shows each hook, and the | |
| 232 | - // caller's ToolCall must stay what the model asked for. | |
| 233 | - tool := *in.Tool | |
| 234 | - in.Tool = &tool | |
| 235 | - for _, h := range r.cfg.For(config.HookPreToolCall) { | |
| 236 | - if !h.Matches(in.Tool.Name) { | |
| 237 | - continue | |
| 238 | - } | |
| 239 | - in.Tool.Input = d.Input | |
| 240 | - reply, err := r.call(ctx, h, in) | |
| 241 | - if err != nil { | |
| 242 | - if r.failed(h, in.Event, err) { | |
| 243 | - return ToolDecision{Input: d.Input, Denied: true, Reason: "hook failed: " + err.Error()} | |
| 244 | - } | |
| 245 | - continue | |
| 246 | - } | |
| 247 | - if reply.Action == ActionDeny { | |
| 248 | - return ToolDecision{Input: d.Input, Denied: true, Reason: reply.Reason} | |
| 249 | - } | |
| 250 | - if reply.Action == ActionAllow && config.AskDepth() == 0 { | |
| 251 | - d.Allowed = true | |
| 252 | - } | |
| 253 | - if len(reply.Input) > 0 { | |
| 254 | - var replaced any | |
| 255 | - if err := json.Unmarshal(reply.Input, &replaced); err == nil { | |
| 256 | - d.Input = replaced | |
| 257 | - } | |
| 258 | - } | |
| 259 | - } | |
| 260 | - return d | |
| 261 | -} | |
| 262 | - | |
| 263 | -// PostToolCall runs the postToolCall hooks whose matcher accepts the tool, | |
| 264 | -// and returns the context they added for the model, in hook order. | |
| 265 | -func (r *Runner) PostToolCall(ctx context.Context, in Input) []string { | |
| 266 | - in.Event = config.HookPostToolCall | |
| 267 | - if r == nil { | |
| 268 | - return nil | |
| 269 | - } | |
| 270 | - var added []string | |
| 271 | - for _, h := range r.cfg.For(config.HookPostToolCall) { | |
| 272 | - if !h.Matches(in.Tool.Name) { | |
| 273 | - continue | |
| 274 | - } | |
| 275 | - reply, err := r.call(ctx, h, in) | |
| 276 | - if err != nil { | |
| 277 | - r.failed(h, in.Event, err) | |
| 278 | - continue | |
| 279 | - } | |
| 280 | - if c := strings.TrimSpace(reply.Context); c != "" { | |
| 281 | - added = append(added, c) | |
| 282 | - } | |
| 283 | - } | |
| 284 | - return added | |
| 285 | -} | |
| 286 | - | |
| 287 | 210 | // PostGeneration runs the postGeneration hooks. They only observe: what they |
| 288 | 211 | // print is ignored, a failure is reported according to onError. |
| 289 | 212 | func (r *Runner) PostGeneration(ctx context.Context, in Input) { |
| @@ -207,83 +207,6 @@ func stopped(d PromptDecision, reason string, response ...string) PromptDecision | |||
| 207 | return d | 207 | return d |
| 208 | } | 208 | } |
| 209 | 209 | ||
| 210 | -// ToolDecision is what the preToolCall chain decided. | ||
| 211 | -type ToolDecision struct { | ||
| 212 | - // Input is the call's arguments: the model's, or the last replacement. | ||
| 213 | - Input any | ||
| 214 | - // Denied means the tool must not run; Reason is handed to the model. | ||
| 215 | - Denied bool | ||
| 216 | - Reason string | ||
| 217 | - // Allowed means a hook approved the call: it runs without asking the | ||
| 218 | - // user (consent.WithApproval). Never set with Denied. | ||
| 219 | - Allowed bool | ||
| 220 | -} | ||
| 221 | - | ||
| 222 | -// PreToolCall runs the preToolCall hooks whose matcher accepts the tool. Each | ||
| 223 | -// one sees the input as the previous one left it; the first deny ends the | ||
| 224 | -// chain. An allow does not: the hooks after it still run, and may deny. | ||
| 225 | -func (r *Runner) PreToolCall(ctx context.Context, in Input) ToolDecision { | ||
| 226 | - in.Event = config.HookPreToolCall | ||
| 227 | - d := ToolDecision{Input: in.Tool.Input} | ||
| 228 | - if r == nil { | ||
| 229 | - return d | ||
| 230 | - } | ||
| 231 | - // A copy: the chain rewrites the input it shows each hook, and the | ||
| 232 | - // caller's ToolCall must stay what the model asked for. | ||
| 233 | - tool := *in.Tool | ||
| 234 | - in.Tool = &tool | ||
| 235 | - for _, h := range r.cfg.For(config.HookPreToolCall) { | ||
| 236 | - if !h.Matches(in.Tool.Name) { | ||
| 237 | - continue | ||
| 238 | - } | ||
| 239 | - in.Tool.Input = d.Input | ||
| 240 | - reply, err := r.call(ctx, h, in) | ||
| 241 | - if err != nil { | ||
| 242 | - if r.failed(h, in.Event, err) { | ||
| 243 | - return ToolDecision{Input: d.Input, Denied: true, Reason: "hook failed: " + err.Error()} | ||
| 244 | - } | ||
| 245 | - continue | ||
| 246 | - } | ||
| 247 | - if reply.Action == ActionDeny { | ||
| 248 | - return ToolDecision{Input: d.Input, Denied: true, Reason: reply.Reason} | ||
| 249 | - } | ||
| 250 | - if reply.Action == ActionAllow && config.AskDepth() == 0 { | ||
| 251 | - d.Allowed = true | ||
| 252 | - } | ||
| 253 | - if len(reply.Input) > 0 { | ||
| 254 | - var replaced any | ||
| 255 | - if err := json.Unmarshal(reply.Input, &replaced); err == nil { | ||
| 256 | - d.Input = replaced | ||
| 257 | - } | ||
| 258 | - } | ||
| 259 | - } | ||
| 260 | - return d | ||
| 261 | -} | ||
| 262 | - | ||
| 263 | -// PostToolCall runs the postToolCall hooks whose matcher accepts the tool, | ||
| 264 | -// and returns the context they added for the model, in hook order. | ||
| 265 | -func (r *Runner) PostToolCall(ctx context.Context, in Input) []string { | ||
| 266 | - in.Event = config.HookPostToolCall | ||
| 267 | - if r == nil { | ||
| 268 | - return nil | ||
| 269 | - } | ||
| 270 | - var added []string | ||
| 271 | - for _, h := range r.cfg.For(config.HookPostToolCall) { | ||
| 272 | - if !h.Matches(in.Tool.Name) { | ||
| 273 | - continue | ||
| 274 | - } | ||
| 275 | - reply, err := r.call(ctx, h, in) | ||
| 276 | - if err != nil { | ||
| 277 | - r.failed(h, in.Event, err) | ||
| 278 | - continue | ||
| 279 | - } | ||
| 280 | - if c := strings.TrimSpace(reply.Context); c != "" { | ||
| 281 | - added = append(added, c) | ||
| 282 | - } | ||
| 283 | - } | ||
| 284 | - return added | ||
| 285 | -} | ||
| 286 | - | ||
| 287 | // PostGeneration runs the postGeneration hooks. They only observe: what they | 210 | // PostGeneration runs the postGeneration hooks. They only observe: what they |
| 288 | // print is ignored, a failure is reported according to onError. | 211 | // print is ignored, a failure is reported according to onError. |
| 289 | func (r *Runner) PostGeneration(ctx context.Context, in Input) { | 212 | func (r *Runner) PostGeneration(ctx context.Context, in Input) { |
added
internal/hooks/tools.go +102 -0 | new file mode 100644 | ||
| @@ -0,0 +1,102 @@ | ||
| 1 | +package hooks | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "context" | |
| 5 | + "encoding/json" | |
| 6 | + "strings" | |
| 7 | + | |
| 8 | + "mm/internal/config" | |
| 9 | +) | |
| 10 | + | |
| 11 | +// The tool-call events: preToolCall decides a call (deny, allow, a rewritten | |
| 12 | +// input), postToolCall adds context after it. | |
| 13 | + | |
| 14 | +// ToolDecision is what the preToolCall chain decided. | |
| 15 | +type ToolDecision struct { | |
| 16 | + // Input is the call's arguments: the model's, or the last replacement. | |
| 17 | + Input any | |
| 18 | + // Denied means the tool must not run; Reason is handed to the model. | |
| 19 | + Denied bool | |
| 20 | + Reason string | |
| 21 | + // Allowed means a hook approved the call: it runs without asking the | |
| 22 | + // user (consent.WithApproval). Never set with Denied. | |
| 23 | + Allowed bool | |
| 24 | +} | |
| 25 | + | |
| 26 | +// PreToolCall runs the preToolCall hooks whose matcher accepts the tool. Each | |
| 27 | +// one sees the input as the previous one left it; the first deny ends the | |
| 28 | +// chain. An allow does not: the hooks after it still run, and may deny. | |
| 29 | +func (r *Runner) PreToolCall(ctx context.Context, in Input) ToolDecision { | |
| 30 | + in.Event = config.HookPreToolCall | |
| 31 | + d := ToolDecision{Input: in.Tool.Input} | |
| 32 | + if r == nil { | |
| 33 | + return d | |
| 34 | + } | |
| 35 | + // A copy: the chain rewrites the input it shows each hook, and the | |
| 36 | + // caller's ToolCall must stay what the model asked for. | |
| 37 | + tool := *in.Tool | |
| 38 | + in.Tool = &tool | |
| 39 | + for _, h := range r.cfg.For(config.HookPreToolCall) { | |
| 40 | + if !h.Matches(in.Tool.Name) { | |
| 41 | + continue | |
| 42 | + } | |
| 43 | + in.Tool.Input = d.Input | |
| 44 | + if r.preToolHook(ctx, h, in, &d) { | |
| 45 | + return d | |
| 46 | + } | |
| 47 | + } | |
| 48 | + return d | |
| 49 | +} | |
| 50 | + | |
| 51 | +// preToolHook runs one preToolCall hook and folds its reply into d: a deny | |
| 52 | +// (or a failure onError turns into one) ends the chain — it reports true —; | |
| 53 | +// an allow approves the call at the top level only; a replaced input is | |
| 54 | +// what the next hook, and the tool, see. | |
| 55 | +func (r *Runner) preToolHook(ctx context.Context, h config.Hook, in Input, d *ToolDecision) bool { | |
| 56 | + reply, err := r.call(ctx, h, in) | |
| 57 | + if err != nil { | |
| 58 | + if r.failed(h, in.Event, err) { | |
| 59 | + *d = ToolDecision{Input: d.Input, Denied: true, Reason: "hook failed: " + err.Error()} | |
| 60 | + return true | |
| 61 | + } | |
| 62 | + return false | |
| 63 | + } | |
| 64 | + if reply.Action == ActionDeny { | |
| 65 | + *d = ToolDecision{Input: d.Input, Denied: true, Reason: reply.Reason} | |
| 66 | + return true | |
| 67 | + } | |
| 68 | + if reply.Action == ActionAllow && config.AskDepth() == 0 { | |
| 69 | + d.Allowed = true | |
| 70 | + } | |
| 71 | + if len(reply.Input) > 0 { | |
| 72 | + var replaced any | |
| 73 | + if err := json.Unmarshal(reply.Input, &replaced); err == nil { | |
| 74 | + d.Input = replaced | |
| 75 | + } | |
| 76 | + } | |
| 77 | + return false | |
| 78 | +} | |
| 79 | + | |
| 80 | +// PostToolCall runs the postToolCall hooks whose matcher accepts the tool, | |
| 81 | +// and returns the context they added for the model, in hook order. | |
| 82 | +func (r *Runner) PostToolCall(ctx context.Context, in Input) []string { | |
| 83 | + in.Event = config.HookPostToolCall | |
| 84 | + if r == nil { | |
| 85 | + return nil | |
| 86 | + } | |
| 87 | + var added []string | |
| 88 | + for _, h := range r.cfg.For(config.HookPostToolCall) { | |
| 89 | + if !h.Matches(in.Tool.Name) { | |
| 90 | + continue | |
| 91 | + } | |
| 92 | + reply, err := r.call(ctx, h, in) | |
| 93 | + if err != nil { | |
| 94 | + r.failed(h, in.Event, err) | |
| 95 | + continue | |
| 96 | + } | |
| 97 | + if c := strings.TrimSpace(reply.Context); c != "" { | |
| 98 | + added = append(added, c) | |
| 99 | + } | |
| 100 | + } | |
| 101 | + return added | |
| 102 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,102 @@ | |||
| 1 | +package hooks | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "context" | ||
| 5 | + "encoding/json" | ||
| 6 | + "strings" | ||
| 7 | + | ||
| 8 | + "mm/internal/config" | ||
| 9 | +) | ||
| 10 | + | ||
| 11 | +// The tool-call events: preToolCall decides a call (deny, allow, a rewritten | ||
| 12 | +// input), postToolCall adds context after it. | ||
| 13 | + | ||
| 14 | +// ToolDecision is what the preToolCall chain decided. | ||
| 15 | +type ToolDecision struct { | ||
| 16 | + // Input is the call's arguments: the model's, or the last replacement. | ||
| 17 | + Input any | ||
| 18 | + // Denied means the tool must not run; Reason is handed to the model. | ||
| 19 | + Denied bool | ||
| 20 | + Reason string | ||
| 21 | + // Allowed means a hook approved the call: it runs without asking the | ||
| 22 | + // user (consent.WithApproval). Never set with Denied. | ||
| 23 | + Allowed bool | ||
| 24 | +} | ||
| 25 | + | ||
| 26 | +// PreToolCall runs the preToolCall hooks whose matcher accepts the tool. Each | ||
| 27 | +// one sees the input as the previous one left it; the first deny ends the | ||
| 28 | +// chain. An allow does not: the hooks after it still run, and may deny. | ||
| 29 | +func (r *Runner) PreToolCall(ctx context.Context, in Input) ToolDecision { | ||
| 30 | + in.Event = config.HookPreToolCall | ||
| 31 | + d := ToolDecision{Input: in.Tool.Input} | ||
| 32 | + if r == nil { | ||
| 33 | + return d | ||
| 34 | + } | ||
| 35 | + // A copy: the chain rewrites the input it shows each hook, and the | ||
| 36 | + // caller's ToolCall must stay what the model asked for. | ||
| 37 | + tool := *in.Tool | ||
| 38 | + in.Tool = &tool | ||
| 39 | + for _, h := range r.cfg.For(config.HookPreToolCall) { | ||
| 40 | + if !h.Matches(in.Tool.Name) { | ||
| 41 | + continue | ||
| 42 | + } | ||
| 43 | + in.Tool.Input = d.Input | ||
| 44 | + if r.preToolHook(ctx, h, in, &d) { | ||
| 45 | + return d | ||
| 46 | + } | ||
| 47 | + } | ||
| 48 | + return d | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +// preToolHook runs one preToolCall hook and folds its reply into d: a deny | ||
| 52 | +// (or a failure onError turns into one) ends the chain — it reports true —; | ||
| 53 | +// an allow approves the call at the top level only; a replaced input is | ||
| 54 | +// what the next hook, and the tool, see. | ||
| 55 | +func (r *Runner) preToolHook(ctx context.Context, h config.Hook, in Input, d *ToolDecision) bool { | ||
| 56 | + reply, err := r.call(ctx, h, in) | ||
| 57 | + if err != nil { | ||
| 58 | + if r.failed(h, in.Event, err) { | ||
| 59 | + *d = ToolDecision{Input: d.Input, Denied: true, Reason: "hook failed: " + err.Error()} | ||
| 60 | + return true | ||
| 61 | + } | ||
| 62 | + return false | ||
| 63 | + } | ||
| 64 | + if reply.Action == ActionDeny { | ||
| 65 | + *d = ToolDecision{Input: d.Input, Denied: true, Reason: reply.Reason} | ||
| 66 | + return true | ||
| 67 | + } | ||
| 68 | + if reply.Action == ActionAllow && config.AskDepth() == 0 { | ||
| 69 | + d.Allowed = true | ||
| 70 | + } | ||
| 71 | + if len(reply.Input) > 0 { | ||
| 72 | + var replaced any | ||
| 73 | + if err := json.Unmarshal(reply.Input, &replaced); err == nil { | ||
| 74 | + d.Input = replaced | ||
| 75 | + } | ||
| 76 | + } | ||
| 77 | + return false | ||
| 78 | +} | ||
| 79 | + | ||
| 80 | +// PostToolCall runs the postToolCall hooks whose matcher accepts the tool, | ||
| 81 | +// and returns the context they added for the model, in hook order. | ||
| 82 | +func (r *Runner) PostToolCall(ctx context.Context, in Input) []string { | ||
| 83 | + in.Event = config.HookPostToolCall | ||
| 84 | + if r == nil { | ||
| 85 | + return nil | ||
| 86 | + } | ||
| 87 | + var added []string | ||
| 88 | + for _, h := range r.cfg.For(config.HookPostToolCall) { | ||
| 89 | + if !h.Matches(in.Tool.Name) { | ||
| 90 | + continue | ||
| 91 | + } | ||
| 92 | + reply, err := r.call(ctx, h, in) | ||
| 93 | + if err != nil { | ||
| 94 | + r.failed(h, in.Event, err) | ||
| 95 | + continue | ||
| 96 | + } | ||
| 97 | + if c := strings.TrimSpace(reply.Context); c != "" { | ||
| 98 | + added = append(added, c) | ||
| 99 | + } | ||
| 100 | + } | ||
| 101 | + return added | ||
| 102 | +} | ||