← ~/pillars / deterministic-policy / policy-hierarchy

The policy hierarchy: hook > check > prompt

Choosing the right enforcement layer for a rule, and why the strongest available layer wins.

updated 2026-06-10 ~3 min read pattern 01/06 Deterministic Guardrails

on this page

Context. You have a rule the team has agreed on (formatting, a forbidden API, a migration discipline, a path no agent may touch) and an agent that sometimes forgets rules under context pressure.

Forces. Prompts are cheap to write but decay: compaction, long sessions, and competing instructions all erode them. Hooks and CI checks are deterministic but cost more to build and can block legitimate work when written too broadly. And not every rule can be mechanised: “never edit .env files” is a string comparison, but “use the saga pattern where it fits our tech debt” requires judgment no hook can encode.

Resolution. Place every rule at the strongest layer that can express it, working down this ladder:

  1. Permission deny rules for whole classes of action. The decisive property: rules merge across all five settings scopes (managed, CLI, local, project, user), and a deny at any scope wins over an allow at any other scope: a user-level deny blocks a project-level allow, and no CLI flag overrides a managed deny (permissions docs). Deny rules are therefore the one layer nobody can quietly loosen from a lower-precedence file.
  2. Hooks for rules that need logic a static pattern cannot express: inspecting the parsed tool input, checking repository state, consulting an external system. Hooks have a useful asymmetry (hooks docs): a PreToolUse hook that exits 2 blocks the tool call even when an allow rule matches, but a hook returning "permissionDecision": "allow" only skips the interactive prompt. It cannot override a deny rule. Hooks are reliable hard denials and unreliable hard approvals. Design with that grain: use hooks to subtract permissions, never to add them.
  3. CI checks for properties of the finished change that no in-session layer can see: the full test suite, dependency scans, anything that needs the merged diff rather than a single tool call. CI also catches what happened in sessions that ran with weaker settings. It is the layer that does not care how the code was authored.
  4. Prompt-space (CLAUDE.md, skills) for everything left: rules that require interpreting intent, weighing competing valid approaches, or context that spans turns. The boundary statement: a rule belongs in a hook or check if and only if it can be written as “if this observable artifact, command, or file has this property, then block”. If you cannot write that sentence, you cannot write the hook.

The corollary for enterprises: the endpoint of this ladder is managed settings with allowManagedPermissionRulesOnly and allowManagedHooksOnly, which stop user and project files from defining rules or hooks at all (settings docs). That converts the hierarchy from a convention into a guarantee.

Worked example. Take three rules a Java team actually has:

  • “All Java files are Spotless-formatted.” Mechanisable, per-edit → a PostToolUse hook. The format-on-edit artifact owns this rule; it never appears in CLAUDE.md at all, so prompt-space stays free for decisions that need a brain.
  • “No agent edits production configuration.” Mechanisable, must hold even if an allow rule covers Edit → a blocking PreToolUse hook, exit 2, reason on stderr. See block-prod-config-edits. A deny rule on the same paths is the belt to the hook’s braces: the hook gives a readable reason back to the model, the deny rule survives even if someone deletes the hook script.
  • “Prefer constructor injection over field injection.” Not mechanisable as a hard block without false positives: it has legitimate exceptions, so it stays in CLAUDE.md, and the team accepts it will occasionally be missed. ArchUnit can later promote the uncontroversial core of it into a CI check; rules migrate down the hierarchy as your confidence in their edge cases grows.

The pattern pages that follow develop each layer: hooks as hard gates, permission policy design, and CI as the outer gate.

// sources