Skill v1.0.0
currentLLM-judged scan90/100version: "1.0.0" name: hook-dual-flow-command-dispatch description: "How to implement a single hook script that serves two distinct flows (e.g., check and mutate) by dispatching on a 'command' field in tool_input, so both guard and write operations share one registration." user-invocable: false
When to use
When a hook needs to handle two logically related but distinct operations — for example, a pre-tool-use guard that checks state and a separate write path that marks state as complete — and you want to register only one hook entry rather than two separate scripts.
The pattern uses tool_input.command as a discriminator field. Callers that want the mutate path set command explicitly; calls without command default to the check path.
Steps
- In the hook's
main(), read thecommandfield fromtool_input:
``bash command_field=$(printf '%s' "$tool_input" | jq -r '.tool_input.command // ""') ``
- Dispatch to the appropriate function based on the field value:
```bash if [ "$command_field" = "mark-phase-complete" ]; then agent_name=$(printf '%s' "$tool_input" | jq -r '.tool_input.agentName // ""') phase_number=$(printf '%s' "$tool_input" | jq -r '.tool_input.phaseNumber // 0') markPhaseComplete "$agent_name" "$phase_number" return fi
# Default: check path agent_name=$(printf '%s' "$tool_input" | jq -r '.tool_input.agentName // ""') current_phase=$(printf '%s' "$tool_input" | jq -r '.tool_input.currentPhase // 0') checkPhaseOrder "$agent_name" "$current_phase" ```
- Implement both functions in the same script. Each function outputs JSON to stdout and calls
exit 0(never a non-zero exit — hooks must not crash Claude Code). The mutate function callsmarkPhaseComplete; the guard function callscheckPhaseOrderor equivalent.
- Register the hook once in
hooks/hooks.json:
``json { "PreToolUse": [ { "matcher": "", "hooks": [{ "type": "command", "command": "bash \"${CLAUDE_PLUGIN_ROOT}/agents/dark-factory/scripts/my-hook.sh\"" }] } ] } ` Both flows use the same registration. The command field in tool_input` selects which code path runs at call time.
- In tests, exercise each path by setting or omitting the
commandfield in the test fixture:
```python # Check path (no command field) hook_input = {"tool_name": "Agent", "tool_input": {"agentName": "dark-factory-agent", "currentPhase": 2}}
# Mutate path hook_input = {"tool_name": "Bash", "tool_input": {"command": "mark-phase-complete", "agentName": "dark-factory-agent", "phaseNumber": 1}} ```
Notes
- Use a string enum for
commandvalues (e.g.,"mark-phase-complete") rather than a boolean flag so the dispatch is extensible to more than two paths in the future. - The
commandfield is read fromtool_input.command(one level inside the outertool_inputobject), not from the top-level hook JSON. The outertool_namefield identifies the Claude tool type (e.g.,Agent,Bash) and is separate. - Missing or empty
commandalways falls through to the default (check) path. This keeps ordinary Agent/tool invocations on the safe read-only path with no additional fields required. - Both functions must be idempotent or at least safe to call multiple times, because hooks fire for every matching tool invocation — including retries.
- Do not branch on
tool_nameto distinguish the two paths.tool_namereflects the Claude tool being used (e.g.,Bash,Agent), not the intent. Thecommandfield insidetool_inputis the correct discriminator for intent.