No slash command · from session-orchestrator
Guides creating, modifying, and debugging Claude Code hooks including PreToolUse, PostToolUse, Stop, and lifecycle events. Covers plugin hooks.json wrapper format vs. user settings.json format, matchers, security patterns, $CLAUDE_PLUGIN_ROOT portability, and debugging.
Ships in session-orchestrator. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Use the official Claude Code hooks reference as the source of truth for current events and schemas. This skill keeps only the conventions needed to author this plugin's hooks.
Claude Code also documents http, mcp_tool, and experimental agent handlers. Use those only after checking their current fields and event support in the official reference.
{
"type": "prompt",
"prompt": "Evaluate whether this event should proceed: $ARGUMENTS",
"timeout": 30
}
Prompt hooks are supported only on events documented for that handler type. $ARGUMENTS contains the hook input JSON.
Use for: context-aware decisions, flexible evaluation, natural-language reasoning.
{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks/validate.mjs",
"timeout": 60
}
Use for: fast deterministic validations, file-system ops, external tools, performance-critical paths.
Our convention: hook logic lives in .mjs files — see hooks/pre-bash-destructive-guard.mjs and hooks/enforce-scope.mjs. The manifest invokes them through the repository's runtime wrapper.
Keep the file location and its outer document shape explicit when copying an example.
hooks/hooks.json — wrapper format{
"description": "Plugin hook description (optional)",
"hooks": {
"PreToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{ "type": "command", "command": "${CLAUDE_PLUGIN_ROOT}/hooks/validate.mjs" }
]
}
]
}
}
hooks wrapper is requireddescription is optional.claude/settings.json — settings format{
"hooks": {
"PreToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{ "type": "command", "command": "~/my-hook.sh" }
]
}
]
}
}
hooks key is required in settings.hooks/hooks.json may additionally carry a top-level description.The distinction is registration and scope: settings hooks belong to a user, project, or managed policy; plugin hooks run while the plugin is enabled. The nested event → matcher group → handler shape is the same.
| Event | When | Use for |
|---|---|---|
PreToolUse | Before tool runs | Validate, modify, block |
PostToolUse | After tool completes | React to result, log |
UserPromptSubmit | User submits prompt | Add context, validate |
Stop | Main agent stopping | Completeness check |
SubagentStop | Subagent stopping | Task validation |
SessionStart | Session begins or resumes | Context load |
SessionEnd | Session ends | Cleanup, logging |
PreCompact | Before compaction | Preserve critical state |
Notification | User notified | Logging, reactions |
{
"hookSpecificOutput": {
"hookEventName": "PreToolUse",
"permissionDecision": "deny",
"permissionDecisionReason": "Why this decision was made",
"updatedInput": { "field": "modified_value" }
},
"systemMessage": "Explanation shown to Claude"
}
{
"decision": "block",
"reason": "Why Claude should continue",
"systemMessage": "Additional context"
}
Omit decision to allow stopping. approve is not a valid Stop decision. For non-error feedback that keeps the conversation running, use hookSpecificOutput.additionalContext with hookEventName set to Stop or SubagentStop.
echo "export PROJECT_TYPE=nodejs" >> "$CLAUDE_ENV_FILE"
$CLAUDE_ENV_FILE is unique to SessionStart hooks.
All hooks receive JSON on stdin:
{
"session_id": "abc123",
"transcript_path": "/path/to/transcript.jsonl",
"cwd": "/current/working/dir",
"permission_mode": "default",
"hook_event_name": "PreToolUse"
}
Event-specific extras:
PreToolUse: tool_name, tool_input, tool_use_idPostToolUse: tool_name, tool_input, tool_response, tool_use_idUserPromptSubmit: promptStop: stop_hook_active, last_assistant_message; SubagentStop also carries agent identity and transcript fieldsEvent fields vary and evolve. Parse only fields needed by the hook and consult the official event section before depending on one. The UserPromptSubmit field name above was last checked against the official reference on 2026-09-15 (branch codex/ecc-systematic-review); this plugin registers no UserPromptSubmit handler, so no code here exercises either spelling — verify before depending on it. Prompt and agent hooks receive the complete input through $ARGUMENTS.
| Var | Scope | Purpose |
|---|---|---|
$CLAUDE_PROJECT_DIR | All | Project root |
$CLAUDE_PLUGIN_ROOT | Plugin hooks | Plugin directory — use this, never hardcode paths |
$CLAUDE_ENV_FILE | SessionStart only | Persist env vars |
$CLAUDE_CODE_REMOTE | All (conditional) | Set if running remote |
// ✅ Portable — works everywhere the plugin installs
{ "command": "${CLAUDE_PLUGIN_ROOT}/hooks/guard.mjs" }
// ❌ Broken — only works on the operator's machine
{ "command": "~/Projects/.../guard.mjs" }
"matcher": "Write" // Exact tool
"matcher": "Read|Write|Edit" // Multiple
"matcher": "*" // All tools
"matcher": "mcp__.*__delete.*" // Regex — all MCP delete tools
"matcher": "mcp__gitlab_.*" // Specific MCP server
Matchers are case-sensitive.
#!/bin/bash
set -euo pipefail
input=$(cat)
tool_name=$(echo "$input" | jq -r '.tool_name')
if [[ ! "$tool_name" =~ ^[a-zA-Z0-9_]+$ ]]; then
echo '{"decision": "deny", "reason": "Invalid tool name"}' >&2
exit 2
fi
In Node/.mjs hooks (our convention), same principle — parse stdin JSON, validate structure before trusting.
file_path=$(echo "$input" | jq -r '.tool_input.file_path')
# Deny path traversal
[[ "$file_path" == *".."* ]] && { echo '{"decision":"deny","reason":"Path traversal"}' >&2; exit 2; }
# Deny sensitive files
[[ "$file_path" == *".env"* ]] && { echo '{"decision":"deny","reason":"Sensitive file"}' >&2; exit 2; }
Our enforce-scope.mjs implements this for wave-scope boundaries.
echo "$file_path" # ✅
cd "$CLAUDE_PROJECT_DIR" # ✅
echo $file_path # ❌ unquoted injection risk
Current defaults are 600 seconds for command/HTTP/MCP-tool hooks, 30 seconds for prompt hooks, and 60 seconds for agent hooks, with shorter defaults for some events. SessionEnd also has a shared time budget. Set a short explicit timeout appropriate to the hook; a timed-out PreToolUse command hook does not block the tool call.
{ "type": "command", "command": "...", "timeout": 10 }
All matching hooks run in parallel — they don't see each other's output, ordering is non-deterministic. Design for independence.
Installed capability and active registration are different. A plugin can ship hook files without those hooks running when the plugin is disabled. Settings hooks merge with plugin and managed hooks; /hooks shows the active sources.
Direct edits to hooks in settings files are normally picked up by Claude Code's file watcher. Plugin registration changes may require disabling/re-enabling the plugin or starting a fresh session. A command hook's script is launched when the event fires, so editing the script itself can affect the next invocation without re-registering the manifest.
To test a registration change, inspect /hooks, trigger the matching event, and use claude --debug when the source, matcher, output, or timeout remains unclear.
claude --debug
Surfaces hook registration, execution logs, stdin/stdout JSON, timing.
echo '{"tool_name":"Write","tool_input":{"file_path":"/test"}}' | \
${CLAUDE_PLUGIN_ROOT}/hooks/guard.mjs
echo "Exit code: $?"
output=$(./your-hook.mjs < test-input.json)
echo "$output" | jq .
Invalid structured output is normally reported as a non-blocking hook error and the action proceeds, so always verify the output and the resulting decision.
Pattern: check for a flag file or config before running:
#!/bin/bash
FLAG_FILE="$CLAUDE_PROJECT_DIR/.enable-strict-validation"
[[ ! -f "$FLAG_FILE" ]] && exit 0 # Flag not present, skip
# ... validation logic
Or config-based (matches our Session-Config pattern):
CONFIG_FILE="$CLAUDE_PROJECT_DIR/.claude/config.json"
enabled=$(jq -r '.strictMode // false' "$CONFIG_FILE" 2>/dev/null)
[[ "$enabled" != "true" ]] && exit 0
examples/)hooks/pre-bash-destructive-guard.mjs — policy-driven command blocker backed by .orchestrator/policy/blocked-commands.jsonhooks/enforce-scope.mjs — scope enforcement using wave-scope.json in the platform's state directoryhooks/on-session-start.mjs — banner + session inithooks/post-edit-validate.mjs — validates edits after the facthooks/on-stop.mjs — session-event capture + metricsDo:
${CLAUDE_PLUGIN_ROOT} for pathsDon't:
tool_input without validationAll hook handlers support runtime opt-out via two environment variables without any settings-file changes. This is implemented in hooks/_lib/profile-gate.mjs.
| Variable | Values | Behaviour |
|---|---|---|
SO_HOOK_PROFILE | strict | standard | relaxed | minimal | off (full = strict) | Preset bundle (default standard). |
SO_DISABLED_HOOKS | Comma-separated names | Disable individual hooks; overrides profile. |
The same two settings can live per host in owner.yaml (hooks.profile, hooks.disabled); env wins, and owner.yaml cannot disable a safety hook (#1881).
strict (full is the old name): all hooks run, every destructive-guard rule at its policy severity.standard (default): strict minus STANDARD_OFF_HOOKS in hooks/_lib/profile-gate.mjs; the cases in RELAXED_WARN_RULES warn instead of block.relaxed (#2090): the hooks of standard; additionally two measured destructive-guard cases warn and run instead of being denied: a truncating redirect onto an unmodified tracked file, and plain git reset --hard on a clean tree (redirectCaseClass / resetHardCaseClass in hooks/pre-bash-destructive-guard.mjs are the authority). Every other blocking case of the shipped policy is denied (policy warn rules still only warn, and a rule a repo adds in its own overlay with class: recoverable warns as a whole), including a commit or push from a worktree without git hooks; a rule class of irreversible is the default when a rule carries none. owner.yaml may select it; the key allow-destructive-ops in the instruction file no longer relaxes anything (#2089).minimal: only on-session-start + pre-bash-destructive-guard (rules as in standard).off: no hooks run.A new guard that interrupts often but protects no data belongs in STANDARD_OFF_HOOKS; one that stops data loss or a scope escape belongs in SAFETY_HOOKS.
Every new hook handler must add the gate call as the very first executable statement after imports. The pattern is two lines at the top of the file, immediately after the import block:
import { shouldRunHook } from './_lib/profile-gate.mjs';
if (!shouldRunHook('your-hook-name')) process.exit(0);
Use the kebab-case file stem without the .mjs extension as the hook name (e.g. my-hook for hooks/my-hook.mjs). When the hook exits 0 here it is silent — no stdout, no stderr — so Claude Code sees a clean allow.
SO_HOOK_PROFILE value → ignored with a single stderr warning; owner.yaml or the default standard decides.SO_DISABLED_HOOKS with extra whitespace or mixed case is normalised automatically.defaultEnabled param of shouldRunHook is for future opt-in hooks; pass false for any handler that should be off by default in every profile.tests/hooks/profile-gate.test.mjs (10 tests) covers: full/minimal/off profiles, disabled-list override, unknown-profile fallback + warning, defaultEnabled=false, whitespace normalisation, empty disabled-list.
Hook handlers and scripts that need the plugin directory must NOT read
process.env.CLAUDE_PLUGIN_ROOT directly. Use resolvePluginRoot() from
scripts/lib/plugin-root.mjs instead, which implements a 4-level fallback
so manual installs (where the env var is absent) still work.
| Level | Source | Condition |
|---|---|---|
| 1 | CLAUDE_PLUGIN_ROOT env var | Returned immediately when set and is a directory |
| 2 | CODEX_PLUGIN_ROOT env var | Returned immediately when set and is a directory |
| 3 | Walk up from import.meta.url | Looks for package.json with name: "session-orchestrator" |
| 4 | Walk up from process.cwd() | Same marker; catches manual install paths outside the repo tree |
Levels 1 and 2 are fast paths — no filesystem walk is performed when either env var is set. This preserves backward compat with all existing deployments.
When all four levels fail a PluginRootResolutionError is thrown with a
triedPaths array listing what was attempted.
import { resolvePluginRoot, PluginRootResolutionError } from '../scripts/lib/plugin-root.mjs';
// Throws on failure — handle or let it bubble (hooks have top-level catch)
const pluginRoot = resolvePluginRoot();
scripts/lib/platform.mjs's resolvePluginRoot() delegates to this helper
internally, so any caller already using the platform module gets the 4-level
fallback transparently.
tests/lib/plugin-root.test.mjs (10 tests) covers: env-claude, env-codex,
walk-from-import-meta, walk-from-cwd, all-fail-throws-named-error,
env-precedence, PluginRootResolutionError class shape.
hooks/hooks.json uses wrapper format, NOT settings direct format${CLAUDE_PLUGIN_ROOT} for all pathsecho '...' | hook.mjsclaude --debugWhat SKILL.md sets; muted values are Claude Code's defaults
sonnetnpx claudepluginhub kanevry/session-orchestrator --plugin session-orchestrator55 skills in this plugin
/architectureUse when the user asks to improve architecture, find refactoring opportunities, surface deepening opportunities, consolidate tightly-coupled modules, or make a codebase more testable and AI-navigable. Surfaces shallow modules and hypothetical seams using a precise vocabulary (Module / Interface / Implementation / Depth / Seam / Adapter / Leverage / Locality from LANGUAGE.md)./autopilotUse this skill when running an autonomous session-orchestration loop. Chains session-start → session-plan → wave-executor → session-end for N iterations with all 10 kill-switches (SPIRAL, FAILED wave, carryover > 50%, max-hours, max-sessions, resource-overload, token-budget, stall-timeout, sub-threshold confidence, user-abort). Reads Mode-Selector output (Phase B) to decide auto-execute vs. fallback. Writes one autopilot.jsonl record per loop run. Phase C scaffold (issue #277); implementation lives in scripts/lib/autopilot.mjs (Phase C-1 follow-up).
/backlogOther plugins' skills for the same job