Skip to main content

Module hook_guard

Module hook_guard 

Source
Expand description

kranz hook-guard — the command Claude Code lifecycle hooks invoke inside worker sessions (ticket .kranz/tickets/claude-code-hook-gate-projection.md, KRZ-302; the engine side is kranz_engine::hook_gates, which also records the targeted hooks schema version).

This is an INTERNAL plumbing command, never an operator surface: the engine installs it into a worker session’s --settings JSON as a PreToolUse hook on the file-writing tools. The Claude Code CLI pipes the hook payload JSON to stdin; the guard judges the tool call’s target path against the per-session spec file the engine wrote, appends a structured record to the session’s record file, and exits:

  • 0 — the write is in contract; the normal permission flow proceeds.
  • 2 — BLOCK: stderr is fed back to the model as the refusal reason; a blocked record was appended (the engine folds it into a hook.gate.fired event after the session).
  • 1 — the GUARD itself failed (unreadable spec, unparseable payload): a non-blocking error in Claude Code, so the action proceeds and a hook-error notice lands in the transcript. Fail-OPEN by design — a broken guard must never freeze a session, and the miss is still judged by the authoritative engine-side out-of-contract sweep.

The guard runs with the session’s already-cleared environment and reads nothing but the spec file and stdin — no new credential or env channel.

Functions§

run_hook_guard
Run the guard: read the hook payload from stdin, judge it against the spec at config, record the outcome, and return the process exit code. Split from the clap dispatch so tests drive it in-process.