yui-agent-policy 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- yui_agent_policy-0.1.0/.gitignore +13 -0
- yui_agent_policy-0.1.0/.serena/.gitignore +2 -0
- yui_agent_policy-0.1.0/.serena/project.local.yml +5 -0
- yui_agent_policy-0.1.0/.serena/project.yml +152 -0
- yui_agent_policy-0.1.0/PKG-INFO +202 -0
- yui_agent_policy-0.1.0/README.md +175 -0
- yui_agent_policy-0.1.0/examples/check.py +175 -0
- yui_agent_policy-0.1.0/examples/policy.toml +46 -0
- yui_agent_policy-0.1.0/pyproject.toml +50 -0
- yui_agent_policy-0.1.0/src/agent_policy/__init__.py +26 -0
- yui_agent_policy-0.1.0/src/agent_policy/decision.py +40 -0
- yui_agent_policy-0.1.0/src/agent_policy/evaluator.py +84 -0
- yui_agent_policy-0.1.0/src/agent_policy/guardrails.py +78 -0
- yui_agent_policy-0.1.0/src/agent_policy/loader.py +29 -0
- yui_agent_policy-0.1.0/src/agent_policy/matrix.py +37 -0
- yui_agent_policy-0.1.0/src/agent_policy/py.typed +0 -0
- yui_agent_policy-0.1.0/tests/test_check_example.py +219 -0
- yui_agent_policy-0.1.0/tests/test_evaluator.py +326 -0
- yui_agent_policy-0.1.0/tests/test_packaging.py +46 -0
- yui_agent_policy-0.1.0/tests/test_public_api.py +91 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Where: .gitignore
|
|
2
|
+
# What: keep build/test artefacts and the local venv out of version control.
|
|
3
|
+
# Why: agent-policy is a tiny pure-python package; nothing generated belongs in git.
|
|
4
|
+
|
|
5
|
+
.venv/
|
|
6
|
+
__pycache__/
|
|
7
|
+
*.py[cod]
|
|
8
|
+
*.egg-info/
|
|
9
|
+
build/
|
|
10
|
+
dist/
|
|
11
|
+
.pytest_cache/
|
|
12
|
+
.coverage
|
|
13
|
+
htmlcov/
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# This file allows you to locally override settings in project.yml for development purposes.
|
|
2
|
+
#
|
|
3
|
+
# Use the same keys as in project.yml here. Any setting you specify will override the corresponding
|
|
4
|
+
# setting in project.yml, allowing you to customise the configuration for your local development environment
|
|
5
|
+
# without affecting the project configuration in project.yml (which is intended to be versioned).
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# the name by which the project can be referenced within Serena
|
|
2
|
+
project_name: "agent-policy"
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
# list of languages for which language servers are started; choose from:
|
|
6
|
+
# al bash clojure cpp csharp
|
|
7
|
+
# csharp_omnisharp dart elixir elm erlang
|
|
8
|
+
# fortran fsharp go groovy haskell
|
|
9
|
+
# java julia kotlin lua markdown
|
|
10
|
+
# matlab nix pascal perl php
|
|
11
|
+
# php_phpactor powershell python python_jedi r
|
|
12
|
+
# rego ruby ruby_solargraph rust scala
|
|
13
|
+
# swift terraform toml typescript typescript_vts
|
|
14
|
+
# vue yaml zig
|
|
15
|
+
# (This list may be outdated. For the current list, see values of Language enum here:
|
|
16
|
+
# https://github.com/oraios/serena/blob/main/src/solidlsp/ls_config.py
|
|
17
|
+
# For some languages, there are alternative language servers, e.g. csharp_omnisharp, ruby_solargraph.)
|
|
18
|
+
# Note:
|
|
19
|
+
# - For C, use cpp
|
|
20
|
+
# - For JavaScript, use typescript
|
|
21
|
+
# - For Free Pascal/Lazarus, use pascal
|
|
22
|
+
# Special requirements:
|
|
23
|
+
# Some languages require additional setup/installations.
|
|
24
|
+
# See here for details: https://oraios.github.io/serena/01-about/020_programming-languages.html#language-servers
|
|
25
|
+
# When using multiple languages, the first language server that supports a given file will be used for that file.
|
|
26
|
+
# The first language is the default language and the respective language server will be used as a fallback.
|
|
27
|
+
# Note that when using the JetBrains backend, language servers are not used and this list is correspondingly ignored.
|
|
28
|
+
languages:
|
|
29
|
+
- python
|
|
30
|
+
|
|
31
|
+
# the encoding used by text files in the project
|
|
32
|
+
# For a list of possible encodings, see https://docs.python.org/3.11/library/codecs.html#standard-encodings
|
|
33
|
+
encoding: "utf-8"
|
|
34
|
+
|
|
35
|
+
# line ending convention to use when writing source files.
|
|
36
|
+
# Possible values: unset (use global setting), "lf", "crlf", or "native" (platform default)
|
|
37
|
+
# This does not affect Serena's own files (e.g. memories and configuration files), which always use native line endings.
|
|
38
|
+
line_ending:
|
|
39
|
+
|
|
40
|
+
# The language backend to use for this project.
|
|
41
|
+
# If not set, the global setting from serena_config.yml is used.
|
|
42
|
+
# Valid values: LSP, JetBrains
|
|
43
|
+
# Note: the backend is fixed at startup. If a project with a different backend
|
|
44
|
+
# is activated post-init, an error will be returned.
|
|
45
|
+
language_backend:
|
|
46
|
+
|
|
47
|
+
# whether to use project's .gitignore files to ignore files
|
|
48
|
+
ignore_all_files_in_gitignore: true
|
|
49
|
+
|
|
50
|
+
# advanced configuration option allowing to configure language server-specific options.
|
|
51
|
+
# Maps the language key to the options.
|
|
52
|
+
# Have a look at the docstring of the constructors of the LS implementations within solidlsp (e.g., for C# or PHP) to see which options are available.
|
|
53
|
+
# No documentation on options means no options are available.
|
|
54
|
+
ls_specific_settings: {}
|
|
55
|
+
|
|
56
|
+
# list of additional paths to ignore in this project.
|
|
57
|
+
# Same syntax as gitignore, so you can use * and **.
|
|
58
|
+
# Note: global ignored_paths from serena_config.yml are also applied additively.
|
|
59
|
+
ignored_paths: []
|
|
60
|
+
|
|
61
|
+
# whether the project is in read-only mode
|
|
62
|
+
# If set to true, all editing tools will be disabled and attempts to use them will result in an error
|
|
63
|
+
# Added on 2025-04-18
|
|
64
|
+
read_only: false
|
|
65
|
+
|
|
66
|
+
# list of tool names to exclude.
|
|
67
|
+
# This extends the existing exclusions (e.g. from the global configuration)
|
|
68
|
+
#
|
|
69
|
+
# Below is the complete list of tools for convenience.
|
|
70
|
+
# To make sure you have the latest list of tools, and to view their descriptions,
|
|
71
|
+
# execute `uv run scripts/print_tool_overview.py`.
|
|
72
|
+
#
|
|
73
|
+
# * `activate_project`: Activates a project by name.
|
|
74
|
+
# * `check_onboarding_performed`: Checks whether project onboarding was already performed.
|
|
75
|
+
# * `create_text_file`: Creates/overwrites a file in the project directory.
|
|
76
|
+
# * `delete_lines`: Deletes a range of lines within a file.
|
|
77
|
+
# * `delete_memory`: Deletes a memory from Serena's project-specific memory store.
|
|
78
|
+
# * `execute_shell_command`: Executes a shell command.
|
|
79
|
+
# * `find_referencing_code_snippets`: Finds code snippets in which the symbol at the given location is referenced.
|
|
80
|
+
# * `find_referencing_symbols`: Finds symbols that reference the symbol at the given location (optionally filtered by type).
|
|
81
|
+
# * `find_symbol`: Performs a global (or local) search for symbols with/containing a given name/substring (optionally filtered by type).
|
|
82
|
+
# * `get_current_config`: Prints the current configuration of the agent, including the active and available projects, tools, contexts, and modes.
|
|
83
|
+
# * `get_symbols_overview`: Gets an overview of the top-level symbols defined in a given file.
|
|
84
|
+
# * `initial_instructions`: Gets the initial instructions for the current project.
|
|
85
|
+
# Should only be used in settings where the system prompt cannot be set,
|
|
86
|
+
# e.g. in clients you have no control over, like Claude Desktop.
|
|
87
|
+
# * `insert_after_symbol`: Inserts content after the end of the definition of a given symbol.
|
|
88
|
+
# * `insert_at_line`: Inserts content at a given line in a file.
|
|
89
|
+
# * `insert_before_symbol`: Inserts content before the beginning of the definition of a given symbol.
|
|
90
|
+
# * `list_dir`: Lists files and directories in the given directory (optionally with recursion).
|
|
91
|
+
# * `list_memories`: Lists memories in Serena's project-specific memory store.
|
|
92
|
+
# * `onboarding`: Performs onboarding (identifying the project structure and essential tasks, e.g. for testing or building).
|
|
93
|
+
# * `prepare_for_new_conversation`: Provides instructions for preparing for a new conversation (in order to continue with the necessary context).
|
|
94
|
+
# * `read_file`: Reads a file within the project directory.
|
|
95
|
+
# * `read_memory`: Reads the memory with the given name from Serena's project-specific memory store.
|
|
96
|
+
# * `remove_project`: Removes a project from the Serena configuration.
|
|
97
|
+
# * `replace_lines`: Replaces a range of lines within a file with new content.
|
|
98
|
+
# * `replace_symbol_body`: Replaces the full definition of a symbol.
|
|
99
|
+
# * `restart_language_server`: Restarts the language server, may be necessary when edits not through Serena happen.
|
|
100
|
+
# * `search_for_pattern`: Performs a search for a pattern in the project.
|
|
101
|
+
# * `summarize_changes`: Provides instructions for summarizing the changes made to the codebase.
|
|
102
|
+
# * `switch_modes`: Activates modes by providing a list of their names
|
|
103
|
+
# * `think_about_collected_information`: Thinking tool for pondering the completeness of collected information.
|
|
104
|
+
# * `think_about_task_adherence`: Thinking tool for determining whether the agent is still on track with the current task.
|
|
105
|
+
# * `think_about_whether_you_are_done`: Thinking tool for determining whether the task is truly completed.
|
|
106
|
+
# * `write_memory`: Writes a named memory (for future reference) to Serena's project-specific memory store.
|
|
107
|
+
excluded_tools: []
|
|
108
|
+
|
|
109
|
+
# list of tools to include that would otherwise be disabled (particularly optional tools that are disabled by default).
|
|
110
|
+
# This extends the existing inclusions (e.g. from the global configuration).
|
|
111
|
+
included_optional_tools: []
|
|
112
|
+
|
|
113
|
+
# fixed set of tools to use as the base tool set (if non-empty), replacing Serena's default set of tools.
|
|
114
|
+
# This cannot be combined with non-empty excluded_tools or included_optional_tools.
|
|
115
|
+
fixed_tools: []
|
|
116
|
+
|
|
117
|
+
# list of mode names to that are always to be included in the set of active modes
|
|
118
|
+
# The full set of modes to be activated is base_modes + default_modes.
|
|
119
|
+
# If the setting is undefined, the base_modes from the global configuration (serena_config.yml) apply.
|
|
120
|
+
# Otherwise, this setting overrides the global configuration.
|
|
121
|
+
# Set this to [] to disable base modes for this project.
|
|
122
|
+
# Set this to a list of mode names to always include the respective modes for this project.
|
|
123
|
+
base_modes:
|
|
124
|
+
|
|
125
|
+
# list of mode names that are to be activated by default.
|
|
126
|
+
# The full set of modes to be activated is base_modes + default_modes.
|
|
127
|
+
# If the setting is undefined, the default_modes from the global configuration (serena_config.yml) apply.
|
|
128
|
+
# Otherwise, this overrides the setting from the global configuration (serena_config.yml).
|
|
129
|
+
# This setting can, in turn, be overridden by CLI parameters (--mode).
|
|
130
|
+
default_modes:
|
|
131
|
+
|
|
132
|
+
# initial prompt for the project. It will always be given to the LLM upon activating the project
|
|
133
|
+
# (contrary to the memories, which are loaded on demand).
|
|
134
|
+
initial_prompt: ""
|
|
135
|
+
|
|
136
|
+
# time budget (seconds) per tool call for the retrieval of additional symbol information
|
|
137
|
+
# such as docstrings or parameter information.
|
|
138
|
+
# This overrides the corresponding setting in the global configuration; see the documentation there.
|
|
139
|
+
# If null or missing, use the setting from the global configuration.
|
|
140
|
+
symbol_info_budget:
|
|
141
|
+
|
|
142
|
+
# list of regex patterns which, when matched, mark a memory entry as read‑only.
|
|
143
|
+
# Extends the list from the global configuration, merging the two lists.
|
|
144
|
+
read_only_memory_patterns: []
|
|
145
|
+
|
|
146
|
+
# list of regex patterns for memories to completely ignore.
|
|
147
|
+
# Matching memories will not appear in list_memories or activate_project output
|
|
148
|
+
# and cannot be accessed via read_memory or write_memory.
|
|
149
|
+
# To access ignored memory files, use the read_file tool on the raw file path.
|
|
150
|
+
# Extends the list from the global configuration, merging the two lists.
|
|
151
|
+
# Example: ["_archive/.*", "_episodes/.*"]
|
|
152
|
+
ignored_memory_patterns: []
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: yui-agent-policy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Pure-function policy matrix evaluator for AI coding agents (repo x capability x context -> deny/require_approval/auto_allow).
|
|
5
|
+
Project-URL: Repository, https://github.com/yui-stingray/agent-policy
|
|
6
|
+
Project-URL: Issues, https://github.com/yui-stingray/agent-policy/issues
|
|
7
|
+
Author: yui-stingray
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
Keywords: agent,ai-agents,governance,guardrails,policy
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Security
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.11
|
|
22
|
+
Requires-Dist: pydantic>=2.0
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: pytest-cov>=4; extra == 'dev'
|
|
25
|
+
Requires-Dist: pytest<9,>=7; extra == 'dev'
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# agent-policy
|
|
29
|
+
|
|
30
|
+
> Pure-function policy matrix for AI coding agents.
|
|
31
|
+
> Maps `(repo, capability, context)` to one of three modes:
|
|
32
|
+
> `deny` / `require_approval` / `auto_allow`.
|
|
33
|
+
|
|
34
|
+
**Status**: `0.1.0` alpha. The public API is frozen for v0.1; examples and
|
|
35
|
+
hook/wrapper recipes will grow in v0.2.
|
|
36
|
+
|
|
37
|
+
## Why
|
|
38
|
+
|
|
39
|
+
AI coding agents (Claude Code, Codex, Aider, and friends) need a single
|
|
40
|
+
place to answer one question, the same way, every time:
|
|
41
|
+
|
|
42
|
+
> "The agent wants to do X in repo Y — should I let it?"
|
|
43
|
+
|
|
44
|
+
`agent-policy` is that single place. It is deliberately tiny:
|
|
45
|
+
|
|
46
|
+
- **One pure function** — `evaluate(policy, repo, capability, context)`.
|
|
47
|
+
- **No I/O, no logging, no global state.** The evaluator does not touch
|
|
48
|
+
disk, network, or clocks. It is safe to call from a hook, a test, or a
|
|
49
|
+
long-running daemon.
|
|
50
|
+
- **Fail-closed defaults.** A missing `default_mode` is `require_approval`,
|
|
51
|
+
unknown fields in policy files are rejected, and hard guardrails cannot
|
|
52
|
+
be overridden by repo policy.
|
|
53
|
+
|
|
54
|
+
It does **not** parse shell commands, manage state, or send messages.
|
|
55
|
+
Those belong in the wrapper layer that calls `evaluate`.
|
|
56
|
+
|
|
57
|
+
## Install
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pip install yui-agent-policy # once published to PyPI
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
From a source checkout (until the PyPI release is live), install the
|
|
64
|
+
package in editable mode so both the library and `examples/check.py` can
|
|
65
|
+
resolve `import agent_policy`:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
pip install -e .
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Requires Python 3.11+ (uses stdlib `tomllib`). The only runtime dependency
|
|
72
|
+
is `pydantic >= 2`.
|
|
73
|
+
|
|
74
|
+
## Quick start
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
from agent_policy import evaluate, PolicyMatrix, RepoPolicy
|
|
78
|
+
|
|
79
|
+
policy = PolicyMatrix(
|
|
80
|
+
default_mode="require_approval",
|
|
81
|
+
repo_policy=[
|
|
82
|
+
RepoPolicy(
|
|
83
|
+
repo="acme/app",
|
|
84
|
+
ownership_class="internal",
|
|
85
|
+
capabilities={
|
|
86
|
+
"read": "auto_allow",
|
|
87
|
+
"commit": "auto_allow",
|
|
88
|
+
"push": "auto_allow",
|
|
89
|
+
"shell": "require_approval",
|
|
90
|
+
},
|
|
91
|
+
),
|
|
92
|
+
],
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
decision = evaluate(
|
|
96
|
+
policy,
|
|
97
|
+
repo="acme/app",
|
|
98
|
+
capability="commit",
|
|
99
|
+
context={"ownership_class": "internal"},
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
print(decision.mode) # "auto_allow"
|
|
103
|
+
print(decision.reason) # "repo_policy"
|
|
104
|
+
print(decision.matched_repo) # "acme/app"
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Load the same policy from a TOML file:
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
from agent_policy import evaluate, load_policy_file
|
|
111
|
+
|
|
112
|
+
policy = load_policy_file("policy.toml")
|
|
113
|
+
decision = evaluate(policy, repo="acme/app", capability="commit")
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
`evaluate` also accepts a plain `dict` in the same shape as `PolicyMatrix`,
|
|
117
|
+
which is convenient for tests and one-off scripts.
|
|
118
|
+
|
|
119
|
+
## Decision model
|
|
120
|
+
|
|
121
|
+
Every call returns a frozen `PolicyDecision` with three fields:
|
|
122
|
+
|
|
123
|
+
| Field | Type | Meaning |
|
|
124
|
+
|----------------|--------------------------------------------|----------------------------------------------|
|
|
125
|
+
| `mode` | `"deny" \| "require_approval" \| "auto_allow"` | What the caller should do. |
|
|
126
|
+
| `reason` | `"hard_guardrail" \| "repo_policy" \| "default_mode" \| ...` | Which rule produced the decision. |
|
|
127
|
+
| `matched_repo` | `str \| None` | The repo string that matched, or `None`. |
|
|
128
|
+
|
|
129
|
+
Decisions are evaluated in this order:
|
|
130
|
+
|
|
131
|
+
1. **Hard guardrails** — cannot be overridden by repo policy.
|
|
132
|
+
- `push.force` → always `deny`.
|
|
133
|
+
- `merge.pr` → always `require_approval`.
|
|
134
|
+
- External `first_write_to_repo` on a **mutating** capability →
|
|
135
|
+
`require_approval`. Read is not blocked.
|
|
136
|
+
2. **Repo policy match** — every `[[repo_policy]]` entry for the requested
|
|
137
|
+
repo is scanned (optionally gated by `ownership_class`). The first
|
|
138
|
+
entry that declares the capability wins. Splitting a repo's policy
|
|
139
|
+
across multiple entries is supported.
|
|
140
|
+
3. **`default_mode` fallback** — used when no repo policy declares the
|
|
141
|
+
capability. Defaults to `require_approval` if unset.
|
|
142
|
+
|
|
143
|
+
`HARD_GUARDRAILS` is exported as a constant so tooling can assert against
|
|
144
|
+
it without importing private symbols.
|
|
145
|
+
|
|
146
|
+
## Policy file format
|
|
147
|
+
|
|
148
|
+
```toml
|
|
149
|
+
# policy.toml
|
|
150
|
+
default_mode = "require_approval"
|
|
151
|
+
|
|
152
|
+
[[repo_policy]]
|
|
153
|
+
repo = "acme/app"
|
|
154
|
+
ownership_class = "internal"
|
|
155
|
+
|
|
156
|
+
[repo_policy.capabilities]
|
|
157
|
+
read = "auto_allow"
|
|
158
|
+
commit = "auto_allow"
|
|
159
|
+
push = "auto_allow"
|
|
160
|
+
|
|
161
|
+
[[repo_policy]]
|
|
162
|
+
repo = "acme/app" # same repo, extra constraint
|
|
163
|
+
[repo_policy.capabilities]
|
|
164
|
+
shell = "require_approval"
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Unknown top-level fields or typos inside `[[repo_policy]]` fail loudly
|
|
168
|
+
with a `pydantic.ValidationError` — there is no silent degradation.
|
|
169
|
+
|
|
170
|
+
## Wrapper pattern
|
|
171
|
+
|
|
172
|
+
`agent-policy` deliberately does not know how to parse `git push --force`
|
|
173
|
+
or a shell command line. The intended shape is:
|
|
174
|
+
|
|
175
|
+
```
|
|
176
|
+
┌────────────────────────┐
|
|
177
|
+
agent ───▶ │ wrapper (hook / CLI) │ ──▶ agent-policy.evaluate()
|
|
178
|
+
│ - normalize capability│ │
|
|
179
|
+
│ - build context │ ▼
|
|
180
|
+
│ - act on decision │ PolicyDecision
|
|
181
|
+
└────────────────────────┘
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
The wrapper owns: parsing the agent's intent, mapping it to one of the
|
|
185
|
+
MVP capabilities (`read`, `write`, `commit`, `push`, `push.force`,
|
|
186
|
+
`merge.pr`, `shell`), and executing whatever side effect the decision
|
|
187
|
+
implies (block, prompt for approval, log and allow).
|
|
188
|
+
|
|
189
|
+
A runnable minimal wrapper lives in [`examples/check.py`](examples/check.py).
|
|
190
|
+
|
|
191
|
+
## Examples
|
|
192
|
+
|
|
193
|
+
See [`examples/`](examples/). Runnable after installing the package
|
|
194
|
+
(`pip install yui-agent-policy`, or `pip install -e .` from a source checkout):
|
|
195
|
+
|
|
196
|
+
- `policy.toml` — a minimal fail-closed policy with two repos.
|
|
197
|
+
- `check.py` — a tiny CLI wrapper that maps `PolicyDecision` to JSON on
|
|
198
|
+
stdout and a process exit code, suitable for PreToolUse hooks.
|
|
199
|
+
|
|
200
|
+
## License
|
|
201
|
+
|
|
202
|
+
MIT.
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# agent-policy
|
|
2
|
+
|
|
3
|
+
> Pure-function policy matrix for AI coding agents.
|
|
4
|
+
> Maps `(repo, capability, context)` to one of three modes:
|
|
5
|
+
> `deny` / `require_approval` / `auto_allow`.
|
|
6
|
+
|
|
7
|
+
**Status**: `0.1.0` alpha. The public API is frozen for v0.1; examples and
|
|
8
|
+
hook/wrapper recipes will grow in v0.2.
|
|
9
|
+
|
|
10
|
+
## Why
|
|
11
|
+
|
|
12
|
+
AI coding agents (Claude Code, Codex, Aider, and friends) need a single
|
|
13
|
+
place to answer one question, the same way, every time:
|
|
14
|
+
|
|
15
|
+
> "The agent wants to do X in repo Y — should I let it?"
|
|
16
|
+
|
|
17
|
+
`agent-policy` is that single place. It is deliberately tiny:
|
|
18
|
+
|
|
19
|
+
- **One pure function** — `evaluate(policy, repo, capability, context)`.
|
|
20
|
+
- **No I/O, no logging, no global state.** The evaluator does not touch
|
|
21
|
+
disk, network, or clocks. It is safe to call from a hook, a test, or a
|
|
22
|
+
long-running daemon.
|
|
23
|
+
- **Fail-closed defaults.** A missing `default_mode` is `require_approval`,
|
|
24
|
+
unknown fields in policy files are rejected, and hard guardrails cannot
|
|
25
|
+
be overridden by repo policy.
|
|
26
|
+
|
|
27
|
+
It does **not** parse shell commands, manage state, or send messages.
|
|
28
|
+
Those belong in the wrapper layer that calls `evaluate`.
|
|
29
|
+
|
|
30
|
+
## Install
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install yui-agent-policy # once published to PyPI
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
From a source checkout (until the PyPI release is live), install the
|
|
37
|
+
package in editable mode so both the library and `examples/check.py` can
|
|
38
|
+
resolve `import agent_policy`:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install -e .
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Requires Python 3.11+ (uses stdlib `tomllib`). The only runtime dependency
|
|
45
|
+
is `pydantic >= 2`.
|
|
46
|
+
|
|
47
|
+
## Quick start
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from agent_policy import evaluate, PolicyMatrix, RepoPolicy
|
|
51
|
+
|
|
52
|
+
policy = PolicyMatrix(
|
|
53
|
+
default_mode="require_approval",
|
|
54
|
+
repo_policy=[
|
|
55
|
+
RepoPolicy(
|
|
56
|
+
repo="acme/app",
|
|
57
|
+
ownership_class="internal",
|
|
58
|
+
capabilities={
|
|
59
|
+
"read": "auto_allow",
|
|
60
|
+
"commit": "auto_allow",
|
|
61
|
+
"push": "auto_allow",
|
|
62
|
+
"shell": "require_approval",
|
|
63
|
+
},
|
|
64
|
+
),
|
|
65
|
+
],
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
decision = evaluate(
|
|
69
|
+
policy,
|
|
70
|
+
repo="acme/app",
|
|
71
|
+
capability="commit",
|
|
72
|
+
context={"ownership_class": "internal"},
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
print(decision.mode) # "auto_allow"
|
|
76
|
+
print(decision.reason) # "repo_policy"
|
|
77
|
+
print(decision.matched_repo) # "acme/app"
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Load the same policy from a TOML file:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from agent_policy import evaluate, load_policy_file
|
|
84
|
+
|
|
85
|
+
policy = load_policy_file("policy.toml")
|
|
86
|
+
decision = evaluate(policy, repo="acme/app", capability="commit")
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`evaluate` also accepts a plain `dict` in the same shape as `PolicyMatrix`,
|
|
90
|
+
which is convenient for tests and one-off scripts.
|
|
91
|
+
|
|
92
|
+
## Decision model
|
|
93
|
+
|
|
94
|
+
Every call returns a frozen `PolicyDecision` with three fields:
|
|
95
|
+
|
|
96
|
+
| Field | Type | Meaning |
|
|
97
|
+
|----------------|--------------------------------------------|----------------------------------------------|
|
|
98
|
+
| `mode` | `"deny" \| "require_approval" \| "auto_allow"` | What the caller should do. |
|
|
99
|
+
| `reason` | `"hard_guardrail" \| "repo_policy" \| "default_mode" \| ...` | Which rule produced the decision. |
|
|
100
|
+
| `matched_repo` | `str \| None` | The repo string that matched, or `None`. |
|
|
101
|
+
|
|
102
|
+
Decisions are evaluated in this order:
|
|
103
|
+
|
|
104
|
+
1. **Hard guardrails** — cannot be overridden by repo policy.
|
|
105
|
+
- `push.force` → always `deny`.
|
|
106
|
+
- `merge.pr` → always `require_approval`.
|
|
107
|
+
- External `first_write_to_repo` on a **mutating** capability →
|
|
108
|
+
`require_approval`. Read is not blocked.
|
|
109
|
+
2. **Repo policy match** — every `[[repo_policy]]` entry for the requested
|
|
110
|
+
repo is scanned (optionally gated by `ownership_class`). The first
|
|
111
|
+
entry that declares the capability wins. Splitting a repo's policy
|
|
112
|
+
across multiple entries is supported.
|
|
113
|
+
3. **`default_mode` fallback** — used when no repo policy declares the
|
|
114
|
+
capability. Defaults to `require_approval` if unset.
|
|
115
|
+
|
|
116
|
+
`HARD_GUARDRAILS` is exported as a constant so tooling can assert against
|
|
117
|
+
it without importing private symbols.
|
|
118
|
+
|
|
119
|
+
## Policy file format
|
|
120
|
+
|
|
121
|
+
```toml
|
|
122
|
+
# policy.toml
|
|
123
|
+
default_mode = "require_approval"
|
|
124
|
+
|
|
125
|
+
[[repo_policy]]
|
|
126
|
+
repo = "acme/app"
|
|
127
|
+
ownership_class = "internal"
|
|
128
|
+
|
|
129
|
+
[repo_policy.capabilities]
|
|
130
|
+
read = "auto_allow"
|
|
131
|
+
commit = "auto_allow"
|
|
132
|
+
push = "auto_allow"
|
|
133
|
+
|
|
134
|
+
[[repo_policy]]
|
|
135
|
+
repo = "acme/app" # same repo, extra constraint
|
|
136
|
+
[repo_policy.capabilities]
|
|
137
|
+
shell = "require_approval"
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Unknown top-level fields or typos inside `[[repo_policy]]` fail loudly
|
|
141
|
+
with a `pydantic.ValidationError` — there is no silent degradation.
|
|
142
|
+
|
|
143
|
+
## Wrapper pattern
|
|
144
|
+
|
|
145
|
+
`agent-policy` deliberately does not know how to parse `git push --force`
|
|
146
|
+
or a shell command line. The intended shape is:
|
|
147
|
+
|
|
148
|
+
```
|
|
149
|
+
┌────────────────────────┐
|
|
150
|
+
agent ───▶ │ wrapper (hook / CLI) │ ──▶ agent-policy.evaluate()
|
|
151
|
+
│ - normalize capability│ │
|
|
152
|
+
│ - build context │ ▼
|
|
153
|
+
│ - act on decision │ PolicyDecision
|
|
154
|
+
└────────────────────────┘
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
The wrapper owns: parsing the agent's intent, mapping it to one of the
|
|
158
|
+
MVP capabilities (`read`, `write`, `commit`, `push`, `push.force`,
|
|
159
|
+
`merge.pr`, `shell`), and executing whatever side effect the decision
|
|
160
|
+
implies (block, prompt for approval, log and allow).
|
|
161
|
+
|
|
162
|
+
A runnable minimal wrapper lives in [`examples/check.py`](examples/check.py).
|
|
163
|
+
|
|
164
|
+
## Examples
|
|
165
|
+
|
|
166
|
+
See [`examples/`](examples/). Runnable after installing the package
|
|
167
|
+
(`pip install yui-agent-policy`, or `pip install -e .` from a source checkout):
|
|
168
|
+
|
|
169
|
+
- `policy.toml` — a minimal fail-closed policy with two repos.
|
|
170
|
+
- `check.py` — a tiny CLI wrapper that maps `PolicyDecision` to JSON on
|
|
171
|
+
stdout and a process exit code, suitable for PreToolUse hooks.
|
|
172
|
+
|
|
173
|
+
## License
|
|
174
|
+
|
|
175
|
+
MIT.
|