Write your own rules
A policy is a Lua file, so it can do more than a list of rules: share a list, loop, name the shapes you repeat, and decide per action. Each step shows a whole example file. For one kind of rule at a time, see Recipes.
1. Start from the smallest policy
A policy is a Lua file that returns a table. An empty table loads and adds no rules; policy_version is a label you choose, and every decision record carries it.
-- 01-permissive.lua — the minimum loadable policy.
--
-- This file is the smallest possible policy: an empty table that the engine
-- treats as "no rules": no action is refused or allowed by a rule of yours,
-- and Isonapse's own checks still apply.
--
-- POLICY_VERSION exists on every policy as an operator-supplied free-form
-- label (SemVer, a git rev, a YYYY-MM-DD.N stamp — whatever you find
-- operationally meaningful). Every decision record carries it, so an
-- auditor can tell which policy version was in force at the time. The
-- Blake3 `policy_hash` (commits to the literal bytes of THIS file) is the
-- machine-verifiable companion — use both.
--
-- Try it: `isonapse hook policy check 01-permissive.lua` loads it without
-- changing anything.
return {
policy_version = "permissive-baseline-v1",
}
2. Know the fields
Each key of the table is a field: blocked actions, action rules, file and folder rules, site rules, rate limits and a few more. A field name Isonapse does not know is refused when the policy loads. Names inside a field, such as tool:Bash:rm, are not checked that way, so try each rule with the last step.
Policy fields describes each one, and the recipes show them at work.
4. Build an allowlist with a loop
A loop turns a short list into one rule per command. Allowing only what you list means a command nobody thought of keeps Isonapse's other checks.
-- 02-loop-allowlist.lua — table-build loop generates per-subcommand rules.
--
-- This example PERMITs git ONLY when the subcommand is one of
-- {status, log, diff, show}. Everything else keeps Isonapse's own checks:
-- in Enforce, `git reset --hard` asks you first, and work like what you
-- approved before may pass.
--
-- This is an ALLOWLIST: unlisted commands keep Isonapse's other checks. Expressing it
-- as `tool:Bash:git = PERMIT` plus a check_action that denies the bad
-- subcommands would be a BLOCKLIST, where a git subcommand nobody thought
-- of is permitted by default. That is the whole lesson.
--
-- `git status` produces the capability `tool:Bash:git:status`, so each
-- generated key names a capability the hook really produces. Commands whose
-- first operand is NOT a subcommand — `cat`, `rm` — never gain that segment,
-- because there the second word is your filename, not a verb.
--
-- In JSON you'd write each subcommand as a separate object. In Lua you
-- write the LIST and let the loop generate the rules — adding a new
-- read-only subcommand is a one-line change instead of a copy-paste.
local READ_ONLY_GIT = {
"status",
"log",
"diff",
"show",
}
-- Do not add branch or stash here: their capabilities also cover
-- `git branch -D`, `git stash clear` and `git stash drop`.
local actions = {}
for _, sub in ipairs(READ_ONLY_GIT) do
actions["tool:Bash:git:" .. sub] = {
decision = "PERMIT",
reason = "read-only git subcommand",
trust = "local", -- git runs on this host
}
end
return {
policy_version = "git-readonly-allowlist-v1",
actions = actions,
}
5. Name the shapes you repeat
Small functions such as permit(reason) and deny(reason) make a long policy read like a list of decisions.
-- 03-helper-functions.lua — declarative DSL built from a handful of
-- locals.
--
-- Once a policy grows past 30 lines, the field-name boilerplate
-- (`decision = "PERMIT", reason = "..."`) drowns out intent. Wrap
-- common shapes in helper functions and the rest of the policy reads
-- like a spec.
local function permit(reason)
return { decision = "PERMIT", reason = reason }
end
local function deny(reason)
return { decision = "DENY", reason = reason }
end
local function local_permit(reason, allowed_secrets)
return {
decision = "PERMIT",
reason = reason,
trust = "local",
allowed_secrets = allowed_secrets or {},
}
end
local function rate(per_min, per_hour)
local rl = { per_minute = per_min }
if per_hour then rl.per_hour = per_hour end
return rl
end
return {
policy_version = "helper-functions-v1",
actions = {
["tool:Bash:git"] = permit("operator-trusted git workflows"),
["tool:Bash:rm"] = deny("destructive — use trash-cli or a scoped script"),
["tool:Bash:sudo"] = deny("no privilege escalation"),
["tool:Bash:psql"] = local_permit(
"trusted dev DB client",
{ "DEV_DB_URL" }
),
},
rate_limits = {
["tool:Bash"] = rate(30, 200),
["tool:WebFetch"] = rate(10, 60),
["cap:llm-invoke"] = rate(20),
},
}
6. Drive several fields from one table
One table describes each kind of action once, and two loops derive the action rules and the coherence entries from it, so the two never drift apart.
It allows reads and writes to show the shape. Choose your own decisions before you copy it.
-- 04-data-driven.lua — one data spec, multiple policy fields derived.
--
-- A single table-of-tables describes EACH capability the policy cares
-- about: its decision, its reason, AND the coherence envelope that
-- backs it. Two helper functions walk the spec to derive `actions` and
-- `coherence` in lockstep, so adding a new capability is a single row
-- edit and the two fields can never drift out of sync.
--
-- This is the shape a real-world policy converges on once it has more
-- than ~10 capabilities. The data spec doubles as documentation — an
-- operator reading the file sees the WHOLE governance posture in one
-- table.
-- Each row: cap (string), decision ("PERMIT"|"DENY"), reason, and a
-- coherence baseline + tolerance on the per-tool call rate.
local CAPS = {
{ cap = "tool:Read", decision = "PERMIT", reason = "read-only", baseline = 60.0, tolerance = 3.0 },
{ cap = "tool:Edit", decision = "PERMIT", reason = "scoped writes", baseline = 20.0, tolerance = 3.0 },
{ cap = "tool:Write", decision = "PERMIT", reason = "scoped writes", baseline = 10.0, tolerance = 3.0 },
{ cap = "tool:Bash:git", decision = "PERMIT", reason = "git workflows", baseline = 20.0, tolerance = 4.0 },
{ cap = "tool:Bash:rm", decision = "DENY", reason = "destructive", baseline = 0.0, tolerance = 1.0 },
{ cap = "tool:Bash:sudo", decision = "DENY", reason = "escalation", baseline = 0.0, tolerance = 1.0 },
}
local function derive_actions(spec)
local actions = {}
for _, row in ipairs(spec) do
actions[row.cap] = {
decision = row.decision,
reason = row.reason,
}
end
return actions
end
local function derive_coherence(spec)
local coherence = {}
for _, row in ipairs(spec) do
-- Only emit envelopes for capabilities we PERMIT — there's no point
-- envelope-modeling a never-firing path. (DENYs short-circuit before
-- any envelope is checked.)
if row.decision == "PERMIT" then
coherence[#coherence + 1] = {
type = "scalar_baseline",
metric = "calls_per_min:" .. row.cap,
baseline = row.baseline,
tolerance = row.tolerance,
weight = 1.0,
direction = "above",
hard_cap = false, -- soft contribution to the aggregator
}
end
end
return coherence
end
return {
policy_version = "data-driven-v1",
actions = derive_actions(CAPS),
coherence = derive_coherence(CAPS),
}
7. Combine a shared base with your overrides
A base everyone shares and a team's overrides, merged at the end of the file: tables merge key by key, and a list in the overrides replaces the base's list.
-- 05-base-plus-overrides.lua — layered authoring via deep table-merge.
--
-- Organizations converge on a two-layer policy structure:
--
-- BASE — what's true for every team / project (org-wide baseline).
-- OVERRIDES — what's specific to THIS team / project.
--
-- JSON can't compose. You'd either fork the BASE for every team (drift
-- inevitable) or write a custom merge tool. Lua composes naturally:
-- author both layers as Lua tables, deep-merge at the bottom of the
-- file, return the result. The engine sees ONE policy; the author sees
-- the two-layer structure.
--
-- Production fleets usually pull the BASE from a shared repo and only
-- vary the OVERRIDES per team. This file shows both layers in one
-- place for illustration.
-- Org baseline — every team gets these.
local BASE = {
rate_limits = {
["tool:Bash"] = { per_minute = 30, per_hour = 200 },
["cap:llm-invoke"] = { per_minute = 20 },
},
blocked_capabilities = {
["*"] = { "tool:Bash:sudo", "tool:Bash:dd" },
},
llm = {
allowed_models = {
"claude-sonnet-4-6",
},
max_input_tokens = 16384,
budget_per_task_usd = 2.00,
},
}
-- Team-specific overrides — billing has stricter LLM models, tighter
-- budget, and adds a few sensitive paths to the blocked-write list.
local OVERRIDES = {
llm = {
allowed_models = {
"claude-sonnet-4-6",
-- billing-only model addition for invoice reconciliation
"gpt-4o-mini",
},
budget_per_task_usd = 1.00, -- tighter than the base $2.00
},
blocked_file_patterns = {
["cap:file-write"] = {
"**/invoices/*.lock",
"**/billing/*.snapshot",
},
},
}
-- Deep merge — for each top-level field:
-- * if BOTH sides have a map, merge keys (override wins on collision)
-- * if EITHER side has a value, use the non-nil one (override wins)
-- * arrays are REPLACED, not appended, because policy lists are
-- semantically "this is the whole set" — appending creates surprise
-- when overrides shrink the set.
local function deep_merge(base, override)
if type(base) ~= "table" or type(override) ~= "table" then
return override == nil and base or override
end
-- Detect array-shaped tables (1..N integer keys) → replace, don't merge.
if #override > 0 and override[1] ~= nil then
return override
end
local out = {}
for k, v in pairs(base) do out[k] = v end
for k, v in pairs(override) do
out[k] = deep_merge(base[k], v)
end
return out
end
local policy = deep_merge(BASE, OVERRIDES)
policy.policy_version = "billing-team-override-v1"
return policy
8. Branch on the environment
isonapse.config.get reads one of Isonapse's environment variables by its exact name, once you allow that name in Isonapse's configuration file; Isonapse reads it when it starts. A policy can also ask for the time with isonapse.config.now and the machine's name with isonapse.config.hostname. It cannot read files, run programs or load modules.
Its llm settings do not limit what a coding agent spends on its own model.
-- 06-config-environment.lua — branch on the deployment environment at
-- LOAD time via the mediated `isonapse.config` API.
--
-- A single policy file ships to the whole fleet, but the rules differ by
-- environment: production locks the model list and tightens the budget;
-- development stays permissive so engineers aren't slowed down. JSON
-- can't read its surroundings, and the Lua sandbox has no `os` or `io`,
-- so a policy CANNOT `os.getenv("ENVIRONMENT")`.
--
-- Instead, the operator allowlists keys in `config.toml`:
--
-- [lua.config_allowlist]
-- keys = ["environment", "region", "fleet_tier"]
--
-- and Isonapse reads ONLY those keys, each from its own environment
-- variable of exactly that name (case-sensitive: `environment` reads
-- `$environment`) when it starts, exposing them through
-- `isonapse.config.get`. A key that isn't allowlisted returns `nil` —
-- indistinguishable from an allowlisted-but-unset key, so the surface never
-- leaks which keys exist. Every key read at load time is recorded in
-- Isonapse's records.
local env = isonapse.config.get("environment") or "dev"
-- Production: a single audited model, a tight per-task budget.
-- Anything else (dev / staging / unset): the broader default set.
local allowed_models
local budget_usd
if env == "prod" then
allowed_models = { "claude-sonnet-4-6" }
budget_usd = 2.00
else
allowed_models = {
"claude-sonnet-4-6",
"claude-haiku-4-5-20251001",
"gpt-4o-mini",
}
budget_usd = 10.00
end
return {
policy_version = "fleet-by-environment:" .. env,
llm = {
allowed_models = allowed_models,
budget_per_task_usd = budget_usd,
},
}
9. Decide per action
A check_action(ctx) function runs for an action your other rules allow. It can refuse it using ctx.agent_id, ctx.capability, ctx.domain and ctx.path, or return nil to leave the decision as it is. It does not run for an action already refused, or for an action rule that asks or uses LEARNED.
ctx.domain and ctx.path are present only when the action has a site or a file, so check them before use.
-- 07-check-action-runtime.lua — per-action runtime decisions via
-- check_action(ctx).
--
-- The earlier rungs decide the policy ONCE, when the file loads
-- (table-build time). check_action runs PER ACTION, at evaluation time,
-- so it can branch on the runtime context of the *specific* action:
-- which agent, which capability, which file path, which domain.
--
-- Precedence: a static DENY always wins; otherwise check_action gets the
-- last word over a static PERMIT or the default-allow. It returns
-- { decision = "DENY", reason = "..." } -- override to deny
-- { decision = "PERMIT" } -- override to allow
-- nil -- fall through to the static decision
-- It runs in the same sandbox as load time — no io, no os; reaching for
-- them fails closed.
--
-- ctx fields for an agent's action: agent_id and capability, and, when the
-- action has them, domain and path (the file it reads or writes). Other
-- fields may be nil. Always nil-guard optional fields, as below.
--
-- NOTE: in Lua a chunk's `return` must be its LAST statement, so the
-- function is defined first and the policy table is returned at the end.
function check_action(ctx)
-- 1. Never edit/write under /etc, whatever the static rules say.
if (ctx.capability == "tool:Edit" or ctx.capability == "tool:Write")
and ctx.path and ctx.path:find("^/etc/") then
return { decision = "DENY", reason = "edits under /etc are blocked at runtime" }
end
-- 2. The SAME git push is trusted for the CI agent, denied for others
-- (a per-agent decision a static rule can't express).
if ctx.capability == "tool:Bash:git:push" and ctx.agent_id ~= "agent:ci" then
return { decision = "DENY", reason = "git push is restricted to the CI agent" }
end
-- 3. Everything else: fall through to the static decision / default.
return nil
end
return {
policy_version = "runtime-check-action-v1",
-- The static surface still applies first; a static DENY here could not
-- be undone by check_action above. This one just records an explicit
-- PERMIT for read-only git status.
actions = {
["tool:Bash:git:status"] = { decision = "PERMIT" },
},
}
10. Check it, try it and make it active
Save your policy as my-policy.lua, then:
isonapse hook policy check my-policy.luaLoads the file the way Isonapse does and names any file or folder pattern that can never match. It changes nothing.
isonapse hook policy test --candidate my-policy.lua --command "git push"Asks what each mode would do with one sample action. Nothing runs, and no record is written.
isonapse hook policy activate my-policy.luaShows the exact change; one approval with your login password makes it active. Use a plain terminal window that runs no agent, such as macOS Terminal or Ghostty.
isonapse hook policy revisionsLists the policies this machine keeps.
isonapse hook policy rollback <revision>Restores one of them, with one approval and your login password. Use a plain terminal window that runs no agent, such as macOS Terminal or Ghostty.
What a policy cannot do
- Read files, run programs or load modules. It sees only the environment variables you allow in Isonapse's configuration file, the time and the machine's name.
- Loosen your rules from inside a repository: a policy kept in a repository can only add restrictions, unless you trust that exact file. See Rules that travel with the repository.
Stuck? Help finds the message you see.