agent-policy-engine 1.0.1__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.
Files changed (55) hide show
  1. agent_policy_engine-1.0.1/LICENSE +190 -0
  2. agent_policy_engine-1.0.1/PKG-INFO +456 -0
  3. agent_policy_engine-1.0.1/README.md +423 -0
  4. agent_policy_engine-1.0.1/pyproject.toml +87 -0
  5. agent_policy_engine-1.0.1/setup.cfg +4 -0
  6. agent_policy_engine-1.0.1/src/agent_policy_engine.egg-info/PKG-INFO +456 -0
  7. agent_policy_engine-1.0.1/src/agent_policy_engine.egg-info/SOURCES.txt +53 -0
  8. agent_policy_engine-1.0.1/src/agent_policy_engine.egg-info/dependency_links.txt +1 -0
  9. agent_policy_engine-1.0.1/src/agent_policy_engine.egg-info/entry_points.txt +2 -0
  10. agent_policy_engine-1.0.1/src/agent_policy_engine.egg-info/requires.txt +8 -0
  11. agent_policy_engine-1.0.1/src/agent_policy_engine.egg-info/top_level.txt +1 -0
  12. agent_policy_engine-1.0.1/src/ape/__init__.py +393 -0
  13. agent_policy_engine-1.0.1/src/ape/action/__init__.py +17 -0
  14. agent_policy_engine-1.0.1/src/ape/action/action.py +194 -0
  15. agent_policy_engine-1.0.1/src/ape/action/schema.json +52 -0
  16. agent_policy_engine-1.0.1/src/ape/action_repository/__init__.py +68 -0
  17. agent_policy_engine-1.0.1/src/ape/action_repository/repository.py +508 -0
  18. agent_policy_engine-1.0.1/src/ape/action_repository/standard.py +549 -0
  19. agent_policy_engine-1.0.1/src/ape/audit/__init__.py +17 -0
  20. agent_policy_engine-1.0.1/src/ape/audit/logger.py +306 -0
  21. agent_policy_engine-1.0.1/src/ape/authority/__init__.py +16 -0
  22. agent_policy_engine-1.0.1/src/ape/authority/manager.py +413 -0
  23. agent_policy_engine-1.0.1/src/ape/cli/__init__.py +35 -0
  24. agent_policy_engine-1.0.1/src/ape/cli/main-orig.py +354 -0
  25. agent_policy_engine-1.0.1/src/ape/cli/main.py +998 -0
  26. agent_policy_engine-1.0.1/src/ape/config.py +147 -0
  27. agent_policy_engine-1.0.1/src/ape/enforcement/__init__.py +16 -0
  28. agent_policy_engine-1.0.1/src/ape/enforcement/gate.py +242 -0
  29. agent_policy_engine-1.0.1/src/ape/errors.py +498 -0
  30. agent_policy_engine-1.0.1/src/ape/escalation/__init__.py +27 -0
  31. agent_policy_engine-1.0.1/src/ape/escalation/handler.py +198 -0
  32. agent_policy_engine-1.0.1/src/ape/intent/__init__.py +16 -0
  33. agent_policy_engine-1.0.1/src/ape/intent/manager.py +275 -0
  34. agent_policy_engine-1.0.1/src/ape/intent/schema.json +55 -0
  35. agent_policy_engine-1.0.1/src/ape/intent_compiler/__init__.py +44 -0
  36. agent_policy_engine-1.0.1/src/ape/intent_compiler/compiler.py +809 -0
  37. agent_policy_engine-1.0.1/src/ape/mcp/__init__.py +16 -0
  38. agent_policy_engine-1.0.1/src/ape/mcp/scanner.py +310 -0
  39. agent_policy_engine-1.0.1/src/ape/orchestrator/__init__.py +50 -0
  40. agent_policy_engine-1.0.1/src/ape/orchestrator/orchestrator.py +784 -0
  41. agent_policy_engine-1.0.1/src/ape/plan/__init__.py +17 -0
  42. agent_policy_engine-1.0.1/src/ape/plan/manager.py +350 -0
  43. agent_policy_engine-1.0.1/src/ape/plan/schema.json +49 -0
  44. agent_policy_engine-1.0.1/src/ape/plan_generator/__init__.py +46 -0
  45. agent_policy_engine-1.0.1/src/ape/plan_generator/generator.py +737 -0
  46. agent_policy_engine-1.0.1/src/ape/policy/__init__.py +25 -0
  47. agent_policy_engine-1.0.1/src/ape/policy/engine.py +497 -0
  48. agent_policy_engine-1.0.1/src/ape/policy/schema.json +68 -0
  49. agent_policy_engine-1.0.1/src/ape/provenance/__init__.py +23 -0
  50. agent_policy_engine-1.0.1/src/ape/provenance/manager.py +212 -0
  51. agent_policy_engine-1.0.1/src/ape/reference_agent/__init__.py +17 -0
  52. agent_policy_engine-1.0.1/src/ape/reference_agent/agent.py +372 -0
  53. agent_policy_engine-1.0.1/src/ape/runtime/__init__.py +31 -0
  54. agent_policy_engine-1.0.1/src/ape/runtime/orchestrator.py +186 -0
  55. agent_policy_engine-1.0.1/src/ape/runtime/state.py +116 -0
@@ -0,0 +1,190 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to the Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2024 APE Contributors
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
@@ -0,0 +1,456 @@
1
+ Metadata-Version: 2.4
2
+ Name: agent-policy-engine
3
+ Version: 1.0.1
4
+ Summary: Deterministic, capability-based policy enforcement runtime for AI agents
5
+ Author: Shawn Kahalewai Reilly
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/kahalewai/agent-policy-engine
8
+ Project-URL: Documentation, https://github.com/kahalewai/agent-policy-engine/IMPLEMENTATION_GUIDE.md
9
+ Project-URL: Repository, https://github.com/kahalewai/agent-policy-engine
10
+ Project-URL: Changelog, https://github.com/kahalewai/agent-policy-engine/CHANGELOG.md
11
+ Keywords: ai,agents,security,policy,enforcement,llm,mcp,intent,orchestration
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: Apache Software License
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Topic :: Security
20
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
21
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
22
+ Requires-Python: >=3.10
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Requires-Dist: pyyaml>=6.0
26
+ Requires-Dist: jsonschema>=4.17.0
27
+ Provides-Extra: dev
28
+ Requires-Dist: pytest>=7.0; extra == "dev"
29
+ Requires-Dist: pytest-cov>=4.0; extra == "dev"
30
+ Requires-Dist: mypy>=1.0; extra == "dev"
31
+ Requires-Dist: ruff>=0.1.0; extra == "dev"
32
+ Dynamic: license-file
33
+
34
+ # APE - Agent Policy Engine
35
+
36
+ **Deterministic, capability-based policy enforcement runtime for AI agents.**
37
+
38
+ [![Version](https://img.shields.io/badge/version-1.0.1-blue.svg)](https://github.com/agent-policy-engine/ape)
39
+ [![License](https://img.shields.io/badge/license-Apache%202.0-green.svg)](LICENSE)
40
+ [![Python](https://img.shields.io/badge/python-3.10+-blue.svg)](https://python.org)
41
+
42
+ APE provides a complete security framework for AI agent systems, enforcing explicit intents, validated plans, and auditable execution. It solves critical security challenges in AI agent deployments including prompt injection, confused deputy attacks, and unauthorized privilege escalation.
43
+
44
+ ## Core Principle
45
+
46
+ > **"Prompts guide intent, but never are intent."**
47
+
48
+ User prompts are compiled through APE's Intent Compiler into structured, policy-constrained intents. The prompt itself never becomes executable - only the validated, narrowed intent is enforced.
49
+
50
+ ## Features
51
+
52
+ ### Security Guarantees
53
+ - **Deterministic Enforcement**: Non-probabilistic, predictable policy decisions
54
+ - **Cryptographic Binding**: Authority tokens bound to specific intent and plan hashes
55
+ - **Default-Deny**: Any action not explicitly allowed is denied
56
+ - **Complete Audit Trail**: Every decision logged for compliance and debugging
57
+ - **Provenance Tracking**: Data origin tracking prevents untrusted data in authority decisions
58
+
59
+ ### v1.0.1 Components
60
+ - **Action Repository**: Canonical registry of known actions with schemas and risk levels
61
+ - **Intent Compiler**: Natural language → structured intent transformation
62
+ - **Plan Generator**: Intent → validated execution plan with LLM support
63
+ - **APE Orchestrator**: Unified one-call API from prompt to execution
64
+
65
+ ### Core Components (v1.0)
66
+ - **Runtime State Machine**: Enforces valid execution flow
67
+ - **Policy Engine**: YAML-based policy loading and evaluation
68
+ - **Authority Manager**: Single-use, time-limited execution tokens
69
+ - **Enforcement Gate**: Mandatory checkpoint for all tool execution
70
+ - **Escalation Handler**: Routes high-risk actions for approval
71
+ - **MCP Scanner**: Auto-generates policies from MCP configurations
72
+
73
+ ## Installation
74
+
75
+ ```bash
76
+ pip install agent-policy-engine
77
+ ```
78
+
79
+ Or from source:
80
+
81
+ ```bash
82
+ git clone https://github.com/agent-policy-engine/ape.git
83
+ cd ape
84
+ pip install -e .
85
+ ```
86
+
87
+ ## Quick Start
88
+
89
+ APE offers two integration paths: **Orchestrator** (simple) and **Manual** (full control).
90
+
91
+ ### Orchestrator Path (Recommended for Most Use Cases)
92
+
93
+ ```python
94
+ from ape import APEOrchestrator
95
+
96
+ # Create orchestrator from policy
97
+ orch = APEOrchestrator.from_policy("policies/read_only.yaml")
98
+
99
+ # Register your tool implementations
100
+ orch.register_tool("read_file", lambda path: open(path).read())
101
+ orch.register_tool("list_directory", lambda path: os.listdir(path))
102
+
103
+ # Execute with one-call API
104
+ result = orch.execute("Read the config.json file")
105
+
106
+ if result.success:
107
+ print(result.results[0]) # File contents
108
+ else:
109
+ print(f"Error: {result.error}")
110
+ ```
111
+
112
+ ### Manual Path (Maximum Control)
113
+
114
+ ```python
115
+ from ape import (
116
+ PolicyEngine, IntentManager, PlanManager,
117
+ RuntimeOrchestrator, AuthorityManager, EnforcementGate,
118
+ ActionRepository, IntentCompiler, PlanGenerator,
119
+ Action, Provenance, RuntimeState,
120
+ create_standard_repository,
121
+ )
122
+
123
+ # 1. Setup components
124
+ repository = create_standard_repository()
125
+ policy = PolicyEngine("policies/read_only.yaml")
126
+ compiler = IntentCompiler(repository)
127
+ generator = PlanGenerator(repository)
128
+
129
+ # 2. Compile intent from prompt
130
+ intent = compiler.compile(
131
+ prompt="Read the config.json file",
132
+ policy_allowed=policy.get_all_allowed_actions(),
133
+ )
134
+
135
+ # 3. Generate plan
136
+ plan = generator.generate(intent)
137
+
138
+ # 4. Setup APE runtime
139
+ runtime = RuntimeOrchestrator()
140
+ intent_manager = IntentManager()
141
+ plan_manager = PlanManager(intent_manager)
142
+ authority = AuthorityManager(runtime)
143
+ enforcement = EnforcementGate(authority)
144
+
145
+ # 5. Execute through APE
146
+ intent_version = intent_manager.set(intent.to_ape_intent(), Provenance.USER_TRUSTED)
147
+ runtime.transition(RuntimeState.INTENT_SET)
148
+
149
+ plan_hash = plan_manager.submit(plan.to_ape_plan(), Provenance.USER_TRUSTED)
150
+ plan_manager.approve()
151
+ runtime.transition(RuntimeState.PLAN_APPROVED)
152
+
153
+ runtime.transition(RuntimeState.EXECUTING)
154
+
155
+ for idx, step in enumerate(plan.steps):
156
+ action = Action(
157
+ action_id=step.action_id,
158
+ tool_id=step.tool_id,
159
+ parameters=step.parameters,
160
+ intent_version=intent_version,
161
+ plan_hash=plan_hash,
162
+ plan_step_index=idx,
163
+ )
164
+
165
+ policy.evaluate_or_raise(action.action_id)
166
+ token = authority.issue(intent_version, plan_hash, action)
167
+ result = enforcement.execute(token, my_tool, action, **step.parameters)
168
+
169
+ runtime.transition(RuntimeState.TERMINATED)
170
+ ```
171
+
172
+ ## Architecture
173
+
174
+ ```
175
+ ┌─────────────────────────────────────────────────────────────────────────┐
176
+ │ APE Orchestrator │
177
+ │ (Unified API - orchestrates all components) │
178
+ ├─────────────────────────────────────────────────────────────────────────┤
179
+ │ │
180
+ │ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │
181
+ │ │ Action │ │ Intent │ │ Plan │ │
182
+ │ │ Repository │───►│ Compiler │───►│ Generator │ │
183
+ │ │ │ │ │ │ │ │
184
+ │ │ • Known actions │ │ • Prompt→Intent │ │ • Intent→Plan │ │
185
+ │ │ • Schemas │ │ • Policy narrow │ │ • LLM parsing │ │
186
+ │ │ • Risk levels │ │ • Risk filter │ │ • Validation │ │
187
+ │ └─────────────────┘ └─────────────────┘ └─────────────────┘ │
188
+ │ │ │
189
+ ├──────────────────────────────────────────────────────────┼──────────────┤
190
+ │ APE Core (v1.0) │ │
191
+ │ ▼ │
192
+ │ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │
193
+ │ │ Intent │ │ Plan │ │ Policy │ │
194
+ │ │ Manager │───►│ Manager │───►│ Engine │ │
195
+ │ └─────────────────┘ └─────────────────┘ └─────────────────┘ │
196
+ │ │ │ │ │
197
+ │ ▼ ▼ ▼ │
198
+ │ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │
199
+ │ │ Runtime │ │ Authority │ │ Enforcement │ │
200
+ │ │ Orchestrator │───►│ Manager │───►│ Gate │ │
201
+ │ │ │ │ │ │ │ │
202
+ │ │ State machine │ │ Token lifecycle │ │ Execute tools │ │
203
+ │ └─────────────────┘ └─────────────────┘ └─────────────────┘ │
204
+ │ │ │
205
+ │ ▼ │
206
+ │ ┌─────────────────┐ │
207
+ │ │ Audit Logger │ │
208
+ │ └─────────────────┘ │
209
+ └─────────────────────────────────────────────────────────────────────────┘
210
+ ```
211
+
212
+ ## Components
213
+
214
+ ### Action Repository
215
+
216
+ The canonical registry of all actions APE can authorize.
217
+
218
+ ```python
219
+ from ape import ActionRepository, ActionDefinition, ActionCategory, ActionRiskLevel
220
+
221
+ # Use standard repository with 18 common actions
222
+ repository = create_standard_repository()
223
+
224
+ # Or create custom
225
+ repository = ActionRepository()
226
+ repository.register(ActionDefinition(
227
+ action_id="my_action",
228
+ description="My custom action",
229
+ category=ActionCategory.CUSTOM,
230
+ risk_level=ActionRiskLevel.LOW,
231
+ parameter_schema={
232
+ "type": "object",
233
+ "required": ["param1"],
234
+ "properties": {
235
+ "param1": {"type": "string"},
236
+ },
237
+ },
238
+ ))
239
+ ```
240
+
241
+ ### Intent Compiler
242
+
243
+ Transforms natural language prompts into structured intents.
244
+
245
+ ```python
246
+ from ape import IntentCompiler, create_standard_repository
247
+
248
+ repository = create_standard_repository()
249
+ compiler = IntentCompiler(repository)
250
+
251
+ intent = compiler.compile(
252
+ prompt="Read config.json and list the data directory",
253
+ policy_allowed=["read_file", "list_directory"],
254
+ max_risk_level=ActionRiskLevel.MODERATE,
255
+ )
256
+
257
+ print(intent.allowed_actions) # ['read_file', 'list_directory']
258
+ print(intent.scope) # 'directory'
259
+ print(intent.confidence) # 0.92
260
+ ```
261
+
262
+ ### Plan Generator
263
+
264
+ Creates validated execution plans from intents or LLM output.
265
+
266
+ ```python
267
+ from ape import PlanGenerator
268
+
269
+ generator = PlanGenerator(repository)
270
+
271
+ # Generate from intent
272
+ plan = generator.generate(intent)
273
+
274
+ # Or parse LLM output
275
+ plan = generator.parse_and_validate(
276
+ llm_output='[{"action_id": "read_file", "parameters": {"path": "config.json"}}]',
277
+ intent=intent,
278
+ policy_check=policy.evaluate_or_raise,
279
+ )
280
+
281
+ # Convert to APE format
282
+ ape_plan = plan.to_ape_plan()
283
+ ```
284
+
285
+ ### APE Orchestrator
286
+
287
+ Unified API combining all components.
288
+
289
+ ```python
290
+ from ape import APEOrchestrator, ActionRiskLevel
291
+
292
+ orch = APEOrchestrator.from_policy("policy.yaml")
293
+
294
+ # Register tools
295
+ orch.register_tools({
296
+ "read_file": read_file_func,
297
+ "write_file": write_file_func,
298
+ "list_directory": list_dir_func,
299
+ })
300
+
301
+ # One-call execution
302
+ result = orch.execute(
303
+ "Read config.json",
304
+ max_risk_level=ActionRiskLevel.MODERATE,
305
+ parameters={"read_file": {"path": "config.json"}},
306
+ )
307
+
308
+ # Step-by-step execution
309
+ intent = orch.compile_intent("Read config.json")
310
+ plan = orch.create_plan(intent, parameters={"read_file": {"path": "config.json"}})
311
+ result = orch.execute_plan(plan)
312
+
313
+ # Analysis (no execution)
314
+ analysis = orch.analyze_prompt("Read config.json and delete temp files")
315
+ print(analysis["policy_blocked"]) # Shows delete_file blocked by policy
316
+ ```
317
+
318
+ ## Policy Configuration
319
+
320
+ Policies are YAML files that define allowed actions.
321
+
322
+ ```yaml
323
+ # policies/read_only.yaml
324
+ name: read_only
325
+ version: "1.0"
326
+ description: "Read-only file access"
327
+
328
+ default_decision: deny
329
+
330
+ rules:
331
+ - action: read_file
332
+ decision: allow
333
+
334
+ - action: list_directory
335
+ decision: allow
336
+
337
+ - action: write_file
338
+ decision: deny
339
+
340
+ - action: delete_file
341
+ decision: deny
342
+ ```
343
+
344
+ ### Example Policies
345
+
346
+ | Policy | Description |
347
+ |--------|-------------|
348
+ | `minimal_safe.yaml` | Minimal read-only, maximum safety |
349
+ | `read_only.yaml` | File reading only |
350
+ | `development.yaml` | Broader permissions for development |
351
+ | `filesystem_scoped.yaml` | Path-constrained file access |
352
+ | `human_in_loop.yaml` | All actions require escalation |
353
+
354
+ ## Risk Levels
355
+
356
+ Actions are classified by risk:
357
+
358
+ | Level | Description | Default Behavior |
359
+ |-------|-------------|------------------|
360
+ | MINIMAL | Read-only, no side effects | Allowed |
361
+ | LOW | Reversible side effects | Allowed |
362
+ | MODERATE | Irreversible but recoverable | Allowed with caution |
363
+ | HIGH | Potentially destructive | Requires escalation |
364
+ | CRITICAL | Requires explicit approval | Always escalates |
365
+
366
+ ```python
367
+ intent = compiler.compile(
368
+ prompt="Delete the temp files",
369
+ max_risk_level=ActionRiskLevel.MODERATE, # HIGH/CRITICAL → escalation
370
+ )
371
+
372
+ print(intent.escalation_required) # ['delete_file']
373
+ ```
374
+
375
+ ## CLI Tools
376
+
377
+ ```bash
378
+ # Validate a policy file
379
+ ape validate policies/my_policy.yaml
380
+
381
+ # Scan MCP configuration
382
+ ape scan ~/.config/claude/claude_desktop_config.json
383
+
384
+ # Generate policy from MCP
385
+ ape generate ~/.config/claude/claude_desktop_config.json -o policies/mcp_generated.yaml
386
+ ```
387
+
388
+ ## Error Handling
389
+
390
+ APE provides typed, deterministic errors:
391
+
392
+ ```python
393
+ from ape import (
394
+ PolicyDenyError,
395
+ EscalationRequiredError,
396
+ IntentAmbiguityError,
397
+ PlanValidationError,
398
+ )
399
+
400
+ try:
401
+ result = orch.execute("Delete all files")
402
+ except PolicyDenyError as e:
403
+ print(f"Policy denied: {e.action_id}")
404
+ except EscalationRequiredError as e:
405
+ print(f"Needs approval: {e.action_id}")
406
+ ```
407
+
408
+ ## Security Considerations
409
+
410
+ ### Threat Model
411
+
412
+ APE mitigates:
413
+
414
+ | Threat | Mitigation |
415
+ |--------|------------|
416
+ | Prompt Injection | Prompts compiled, never executed directly |
417
+ | Confused Deputy | Actions bound to explicit intent |
418
+ | Privilege Escalation | Policy narrowing, risk-based filtering |
419
+ | Token Replay | Single-use, time-limited tokens |
420
+ | Plan Tampering | Cryptographic plan hashing |
421
+
422
+ ### Best Practices
423
+
424
+ 1. **Start with minimal policies** - Add permissions as needed
425
+ 2. **Use escalation for high-risk actions** - Human approval for destructive operations
426
+ 3. **Enable audit logging** - Complete trail for compliance
427
+ 4. **Validate all policies** - Use `ape validate` before deployment
428
+ 5. **Test with dry-run** - Use `orch.analyze_prompt()` before execution
429
+
430
+ ## Development
431
+
432
+ ```bash
433
+ # Install with dev dependencies
434
+ pip install -e ".[dev]"
435
+
436
+ # Run tests
437
+ pytest
438
+
439
+ # Type checking
440
+ mypy ape
441
+
442
+ # Linting
443
+ ruff check ape
444
+ ```
445
+
446
+ ## License
447
+
448
+ Apache License 2.0 - See [LICENSE](LICENSE) for details.
449
+
450
+ ## Contributing
451
+
452
+ Contributions welcome! Please read our contributing guidelines and submit pull requests.
453
+
454
+ ## Changelog
455
+
456
+ See [CHANGELOG.md](CHANGELOG.md) for version history.