Skip to content

Hook

agent_gate.hook

The PreToolUse hook: read a payload, judge it, answer Claude Code.

Protocol: Claude Code writes one JSON object to stdin. The hook writes at most one JSON object to stdout and always exits 0. A deny or ask goes out as hookSpecificOutput.permissionDecision; a pass writes nothing, which leaves the call to the normal permission flow. The gate never writes allow.

The gate fails closed: any error while judging becomes a deny.

evaluate(payload, *, record=True, client=None)

Judge one hook payload through every layer.

Parameters:

Name Type Description Default
payload dict[str, Any]

the PreToolUse payload.

required
record bool

append the verdict to the session ledger.

True
client Any

an Anthropic client for the model gate, for tests.

None
Source code in src/agent_gate/hook.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def evaluate(payload: dict[str, Any], *, record: bool = True, client: Any = None) -> Verdict:
    """Judge one hook payload through every layer.

    :param payload: the PreToolUse payload.
    :param record: append the verdict to the session ledger.
    :param client: an Anthropic client for the model gate, for tests.
    """
    action = Action.from_hook(payload)
    mandate = load_mandate(action.cwd)
    ledger = Ledger(action.session_id)
    entries = ledger.entries()
    verdict = strictest(
        rules.check(action, mandate),
        rules.check_history(action, mandate, entries, mandate.digest),
    )
    if verdict.decision != "deny" and action.consequential and mandate.model.enabled:
        verdict = strictest(verdict, model_gate.judge(action, mandate, entries, client=client))
    if record:
        ledger.append(action, verdict, mandate.digest)
    return verdict

main(stdin=sys.stdin, stdout=sys.stdout)

Run the hook once.

Parameters:

Name Type Description Default
stdin TextIO

where the payload comes from.

stdin
stdout TextIO

where the decision goes.

stdout

Returns:

Type Description
int

the exit code, always 0.

Source code in src/agent_gate/hook.py
67
68
69
70
71
72
73
74
75
76
77
78
79
def main(stdin: TextIO = sys.stdin, stdout: TextIO = sys.stdout) -> int:
    """Run the hook once.

    :param stdin: where the payload comes from.
    :param stdout: where the decision goes.
    :return: the exit code, always 0.
    """
    try:
        verdict = evaluate(json.load(stdin))
    except Exception as exc:  # noqa: BLE001 -- the gate fails closed: any error denies
        verdict = Verdict("deny", None, f"the gate failed ({type(exc).__name__}: {exc})", "gate")
    stdout.write(render(verdict))
    return 0

render(verdict)

The hook's stdout for a verdict: a JSON object, or empty for pass.

Parameters:

Name Type Description Default
verdict Verdict

the verdict.

required
Source code in src/agent_gate/hook.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def render(verdict: Verdict) -> str:
    """The hook's stdout for a verdict: a JSON object, or empty for pass.

    :param verdict: the verdict.
    """
    if verdict.decision == "pass":
        return ""
    law = f"Law {verdict.law}" if verdict.law else "no law"
    reason = f"agent-gate ({law}, {verdict.layer}): {verdict.reason}"
    return json.dumps(
        {
            "hookSpecificOutput": {
                "hookEventName": "PreToolUse",
                "permissionDecision": verdict.decision,
                "permissionDecisionReason": reason,
            }
        }
    )