Pure-function policy matrix evaluator for AI coding agents (repo x capability x context -> deny/require_approval/auto_allow).
Project description
agent-policy
Pure-function policy matrix for AI coding agents. Maps
(repo, capability, context)to one of three modes:deny/require_approval/auto_allow.
Status: 0.1.0 alpha. The public API is frozen for v0.1; examples and
hook/wrapper recipes will grow in v0.2.
Why
AI coding agents (Claude Code, Codex, Aider, and friends) need a single place to answer one question, the same way, every time:
"The agent wants to do X in repo Y — should I let it?"
agent-policy is that single place. It is deliberately tiny:
- One pure function —
evaluate(policy, repo, capability, context). - No I/O, no logging, no global state. The evaluator does not touch disk, network, or clocks. It is safe to call from a hook, a test, or a long-running daemon.
- Fail-closed defaults. A missing
default_modeisrequire_approval, unknown fields in policy files are rejected, and hard guardrails cannot be overridden by repo policy.
It does not parse shell commands, manage state, or send messages.
Those belong in the wrapper layer that calls evaluate.
Install
pip install yui-agent-policy # once published to PyPI
From a source checkout (until the PyPI release is live), install the
package in editable mode so both the library and examples/check.py can
resolve import agent_policy:
pip install -e .
Requires Python 3.11+ (uses stdlib tomllib). The only runtime dependency
is pydantic >= 2.
Quick start
from agent_policy import evaluate, PolicyMatrix, RepoPolicy
policy = PolicyMatrix(
default_mode="require_approval",
repo_policy=[
RepoPolicy(
repo="acme/app",
ownership_class="internal",
capabilities={
"read": "auto_allow",
"commit": "auto_allow",
"push": "auto_allow",
"shell": "require_approval",
},
),
],
)
decision = evaluate(
policy,
repo="acme/app",
capability="commit",
context={"ownership_class": "internal"},
)
print(decision.mode) # "auto_allow"
print(decision.reason) # "repo_policy"
print(decision.matched_repo) # "acme/app"
Load the same policy from a TOML file:
from agent_policy import evaluate, load_policy_file
policy = load_policy_file("policy.toml")
decision = evaluate(policy, repo="acme/app", capability="commit")
evaluate also accepts a plain dict in the same shape as PolicyMatrix,
which is convenient for tests and one-off scripts.
Decision model
Every call returns a frozen PolicyDecision with three fields:
| Field | Type | Meaning |
|---|---|---|
mode |
"deny" | "require_approval" | "auto_allow" |
What the caller should do. |
reason |
"hard_guardrail" | "repo_policy" | "default_mode" | ... |
Which rule produced the decision. |
matched_repo |
str | None |
The repo string that matched, or None. |
Decisions are evaluated in this order:
- Hard guardrails — cannot be overridden by repo policy.
push.force→ alwaysdeny.merge.pr→ alwaysrequire_approval.- External
first_write_to_repoon a mutating capability →require_approval. Read is not blocked.
- Repo policy match — every
[[repo_policy]]entry for the requested repo is scanned (optionally gated byownership_class). The first entry that declares the capability wins. Splitting a repo's policy across multiple entries is supported. default_modefallback — used when no repo policy declares the capability. Defaults torequire_approvalif unset.
HARD_GUARDRAILS is exported as a constant so tooling can assert against
it without importing private symbols.
Policy file format
# policy.toml
default_mode = "require_approval"
[[repo_policy]]
repo = "acme/app"
ownership_class = "internal"
[repo_policy.capabilities]
read = "auto_allow"
commit = "auto_allow"
push = "auto_allow"
[[repo_policy]]
repo = "acme/app" # same repo, extra constraint
[repo_policy.capabilities]
shell = "require_approval"
Unknown top-level fields or typos inside [[repo_policy]] fail loudly
with a pydantic.ValidationError — there is no silent degradation.
Wrapper pattern
agent-policy deliberately does not know how to parse git push --force
or a shell command line. The intended shape is:
┌────────────────────────┐
agent ───▶ │ wrapper (hook / CLI) │ ──▶ agent-policy.evaluate()
│ - normalize capability│ │
│ - build context │ ▼
│ - act on decision │ PolicyDecision
└────────────────────────┘
The wrapper owns: parsing the agent's intent, mapping it to one of the
MVP capabilities (read, write, commit, push, push.force,
merge.pr, shell), and executing whatever side effect the decision
implies (block, prompt for approval, log and allow).
A runnable minimal wrapper lives in examples/check.py.
Examples
See examples/. Runnable after installing the package
(pip install yui-agent-policy, or pip install -e . from a source checkout):
policy.toml— a minimal fail-closed policy with two repos.check.py— a tiny CLI wrapper that mapsPolicyDecisionto JSON on stdout and a process exit code, suitable for PreToolUse hooks.claude_code_hook.sh— a Claude CodePreToolUsehook that reads the hook payload from stdin, maps the tool to a capability, and shells out tocheck.py. SetAGENT_POLICY_FILEandAGENT_POLICY_REPOin the hook's environment, then point~/.claude/settings.jsonat it.codex_hook.sh— a Codex CLIPreToolUsehook (shell guardrail pilot). Codex hooks currently intercept Bash commands only — read, write, and edit operations are not covered. Mapsgit push --forcetopush.force,gh pr mergetomerge.pr, and everything else toshell. Requiresfeatures.codex_hooks = truein your Codex config and ahooks.jsonin~/.codex/or<repo>/.codex/.capability_map.py— stdlib-only helper that turns a raw Bash command into one ofpush.force/merge.pr/shell. Both hook wrappers shell out to it instead of doing substring matching, so quoted literals likeprintf '%s\n' 'git push --force'no longer produce a falsepush.forceclassification. See the file header for the exact algorithm (heredoc stripping →shlextokenization → scan-anywhere → recursivebash -c/eval).
Codex CLI hook — known MVP limitations
The Codex CLI hook feature is marked "Under development" in upstream
docs. Two gaps affect how agent-policy presents decisions through
it, and they are worth knowing before you enable it:
require_approvaldegrades to block. Codex hook events accept onlyallowordeny— there is nopermissionDecision: "ask"yet.examples/codex_hook.shtherefore exits2for bothdenyandrequire_approval, and the only UX signal distinguishing the two is the stderr line (DENY ...vsrequire_approval ...). Users must retry after manual approval rather than being prompted inline.- Bash-only scope. Codex hooks intercept shell commands and nothing else. Read, write, and edit tool calls are invisible — if you need capability gating on those, use the Claude Code hook.
- Heuristic command parsing.
capability_map.pyisshlex-based, not a full shell. It handles quoted literals, heredocs, compound statements, and the commonbash -c '...'/evalwrappers, but exotic forms such asgit --git-dir=/path push --force, process substitution, or function definitions are not modeled. The fail-closed default isshell, which policy can still flag asrequire_approvalordeny.
Releases
Tag-driven. Pushing a vX.Y.Z annotated tag triggers .github/workflows/release.yml, which builds the sdist + wheel and publishes to PyPI via Trusted Publishing (OIDC). No maintainer-side credentials are required.
License
MIT.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file yui_agent_policy-0.1.3.tar.gz.
File metadata
- Download URL: yui_agent_policy-0.1.3.tar.gz
- Upload date:
- Size: 27.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3caa47432f0bd5c047f4956f6610cdcbbd917af69aaec56d8e5233ac7601cb7
|
|
| MD5 |
be2a74318aa91d3b3b09b609deb74de2
|
|
| BLAKE2b-256 |
bf4758d9c27d3a659845e5b8b6a3f17ee331b523b519402e538c653c61d5522f
|
Provenance
The following attestation bundles were made for yui_agent_policy-0.1.3.tar.gz:
Publisher:
release.yml on yui-stingray/agent-policy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
yui_agent_policy-0.1.3.tar.gz -
Subject digest:
e3caa47432f0bd5c047f4956f6610cdcbbd917af69aaec56d8e5233ac7601cb7 - Sigstore transparency entry: 1262264670
- Sigstore integration time:
-
Permalink:
yui-stingray/agent-policy@0ebff4049fd9efadbe25ebf65c18523948569f64 -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/yui-stingray
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0ebff4049fd9efadbe25ebf65c18523948569f64 -
Trigger Event:
push
-
Statement type:
File details
Details for the file yui_agent_policy-0.1.3-py3-none-any.whl.
File metadata
- Download URL: yui_agent_policy-0.1.3-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
550f7db24ec83bfc2a7f928727bf63c30127d1cf321d60441907c425269b2050
|
|
| MD5 |
68a2ad74db9300aeabefddf12385c3cb
|
|
| BLAKE2b-256 |
228de8d5c73db3cb96f34d1a0bff4283977bc6a9591ea19c458f7ea8fb400bc0
|
Provenance
The following attestation bundles were made for yui_agent_policy-0.1.3-py3-none-any.whl:
Publisher:
release.yml on yui-stingray/agent-policy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
yui_agent_policy-0.1.3-py3-none-any.whl -
Subject digest:
550f7db24ec83bfc2a7f928727bf63c30127d1cf321d60441907c425269b2050 - Sigstore transparency entry: 1262264688
- Sigstore integration time:
-
Permalink:
yui-stingray/agent-policy@0ebff4049fd9efadbe25ebf65c18523948569f64 -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/yui-stingray
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@0ebff4049fd9efadbe25ebf65c18523948569f64 -
Trigger Event:
push
-
Statement type: