openai-agents-acgs 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.
- openai_agents_acgs-0.1.0/.gitignore +12 -0
- openai_agents_acgs-0.1.0/CHANGELOG.md +26 -0
- openai_agents_acgs-0.1.0/LICENSE +175 -0
- openai_agents_acgs-0.1.0/PKG-INFO +406 -0
- openai_agents_acgs-0.1.0/README.md +377 -0
- openai_agents_acgs-0.1.0/examples/quickstart.py +82 -0
- openai_agents_acgs-0.1.0/pyproject.toml +148 -0
- openai_agents_acgs-0.1.0/src/openai_agents_acgs/__init__.py +18 -0
- openai_agents_acgs-0.1.0/src/openai_agents_acgs/_governance.py +310 -0
- openai_agents_acgs-0.1.0/src/openai_agents_acgs/guard.py +270 -0
- openai_agents_acgs-0.1.0/src/openai_agents_acgs/py.typed +0 -0
- openai_agents_acgs-0.1.0/tests/__init__.py +0 -0
- openai_agents_acgs-0.1.0/tests/conftest.py +7 -0
- openai_agents_acgs-0.1.0/tests/fakes.py +155 -0
- openai_agents_acgs-0.1.0/tests/test_default_constitution.py +90 -0
- openai_agents_acgs-0.1.0/tests/test_examples.py +84 -0
- openai_agents_acgs-0.1.0/tests/test_governance.py +286 -0
- openai_agents_acgs-0.1.0/tests/test_guard.py +443 -0
- openai_agents_acgs-0.1.0/tests/test_imports.py +21 -0
- openai_agents_acgs-0.1.0/tests/test_tool_coverage.py +197 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented in this file.
|
|
4
|
+
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses
|
|
5
|
+
[Semantic Versioning](https://semver.org/). Entries for new releases are generated with `cz bump`.
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## 0.1.0 (unreleased)
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- `ACGSToolGuard`, which checks OpenAI Agents SDK function tool calls against an ACGS constitution with two
|
|
14
|
+
documented per-tool mechanisms: a tool input guardrail (`input_guardrail`, named `acgs`) that runs right before
|
|
15
|
+
the tool executes, and a `needs_approval` policy that pauses the run with a `ToolApprovalItem` when ACGS requires
|
|
16
|
+
review.
|
|
17
|
+
- `govern_agent(agent)`, which applies the guard to every `FunctionTool` of an agent in place, and raises
|
|
18
|
+
`UngovernedToolsError` for hosted tools, built-in execution tools and local MCP servers without the guardrail
|
|
19
|
+
(`allow_ungoverned=True` logs a warning instead). `govern_tool(tool)` returns a governed copy of one tool.
|
|
20
|
+
- Blocked calls are never sent for approval and are rejected with `reject_content`, or end the run with
|
|
21
|
+
`on_deny="raise"`. Approved calls are checked again before they run, and a blocking rule still rejects them.
|
|
22
|
+
- `on_review` (`"approve"` or `"deny"`) for calls that only match review rules. A tool's own `needs_approval`
|
|
23
|
+
still applies to allowed calls.
|
|
24
|
+
- Local MCP server support through `tool_input_guardrails=[guard.input_guardrail]` (openai-agents 0.22.1 and later).
|
|
25
|
+
- Fail-closed handling: any error while evaluating a call rejects it instead of ending the run.
|
|
26
|
+
- `format_tool_call`, the documented text representation of a tool call that ACGS rules are evaluated against.
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: openai-agents-acgs
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: ACGS rule-based tool-call governance for OpenAI Agents SDK function tools, via tool guardrails and approvals
|
|
5
|
+
Project-URL: Homepage, https://github.com/acgs-ai/openai-agents-acgs
|
|
6
|
+
Project-URL: Documentation, https://github.com/acgs-ai/openai-agents-acgs#readme
|
|
7
|
+
Project-URL: Repository, https://github.com/acgs-ai/openai-agents-acgs
|
|
8
|
+
Project-URL: Issues, https://github.com/acgs-ai/openai-agents-acgs/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/acgs-ai/openai-agents-acgs/blob/main/CHANGELOG.md
|
|
10
|
+
Author: Martin Lyu
|
|
11
|
+
License-Expression: Apache-2.0
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: acgs,agents,ai,governance,human-in-the-loop,openai,openai-agents,tool-calls,tool-guardrails
|
|
14
|
+
Classifier: Development Status :: 3 - Alpha
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
23
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
24
|
+
Classifier: Typing :: Typed
|
|
25
|
+
Requires-Python: >=3.10
|
|
26
|
+
Requires-Dist: acgs-lite<4,>=3.0.1
|
|
27
|
+
Requires-Dist: openai-agents<1,>=0.22.1
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# openai-agents-acgs
|
|
31
|
+
|
|
32
|
+
[](https://github.com/acgs-ai/openai-agents-acgs/actions/workflows/ci.yml)
|
|
33
|
+
[](https://pypi.org/project/openai-agents-acgs/)
|
|
34
|
+
[](https://pypi.org/project/openai-agents-acgs/)
|
|
35
|
+
|
|
36
|
+
`openai-agents-acgs` checks [OpenAI Agents SDK](https://openai.github.io/openai-agents-python/) function tool calls
|
|
37
|
+
against [ACGS](https://pypi.org/project/acgs-lite/) constitution rules before the tool runs. It uses two documented
|
|
38
|
+
SDK mechanisms on each `FunctionTool`:
|
|
39
|
+
|
|
40
|
+
- a [tool input guardrail](https://openai.github.io/openai-agents-python/guardrails/#tool-guardrails), which the
|
|
41
|
+
SDK runs right before the tool executes, including after a call was approved;
|
|
42
|
+
- a [`needs_approval`](https://openai.github.io/openai-agents-python/human_in_the_loop/) policy, so calls that need
|
|
43
|
+
review pause the run as `ToolApprovalItem` interruptions that you approve or reject on the `RunState`.
|
|
44
|
+
|
|
45
|
+
What happens to a call:
|
|
46
|
+
|
|
47
|
+
- Calls that match no blocking rule run unchanged.
|
|
48
|
+
- Calls that match a blocking rule do not run. The guardrail rejects them
|
|
49
|
+
(`ToolGuardrailFunctionOutput.reject_content`), so the model gets the reason as the tool result and can
|
|
50
|
+
continue, or ends the run with `ToolInputGuardrailTripwireTriggered` if you pass `on_deny="raise"`.
|
|
51
|
+
- Calls that only match review rules pause the run for approval and run only once approved. They can also be
|
|
52
|
+
rejected outright.
|
|
53
|
+
- A blocking rule always wins. Nobody is asked to approve a blocked call, and an approved call is checked again
|
|
54
|
+
and rejected if it is blocked.
|
|
55
|
+
- Any error while evaluating a call rejects it (fail-closed).
|
|
56
|
+
|
|
57
|
+
The package is a thin adapter over the public `acgs-lite` API.
|
|
58
|
+
|
|
59
|
+
## Installation
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install openai-agents-acgs
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Requires Python 3.10 or later, `openai-agents>=0.22.1,<1` and `acgs-lite>=3.0.1,<4`.
|
|
66
|
+
|
|
67
|
+
## Usage
|
|
68
|
+
|
|
69
|
+
The SDK has no run-wide hook that can stop a tool call, so the guard is applied to each tool.
|
|
70
|
+
`govern_agent` applies it to every function tool of an agent:
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from agents import Agent, function_tool
|
|
74
|
+
|
|
75
|
+
from openai_agents_acgs import ACGSToolGuard
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@function_tool
|
|
79
|
+
def send_email(to: str, body: str) -> str:
|
|
80
|
+
"""Send an email."""
|
|
81
|
+
return f"sent to {to}"
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
guard = ACGSToolGuard()
|
|
85
|
+
agent = guard.govern_agent(Agent(name="Assistant", tools=[send_email]))
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`govern_agent` replaces `agent.tools` in place with governed copies (`govern_tool(tool)` does the same for one
|
|
89
|
+
tool and leaves the original unchanged), so handoffs that point at the agent see the governed tools. It raises
|
|
90
|
+
`UngovernedToolsError`, and leaves the agent unchanged, if the agent has tools the guard cannot check (see
|
|
91
|
+
[What is covered](#what-is-covered)); pass `allow_ungoverned=True` to accept them with a logged warning. Tools
|
|
92
|
+
added to `agent.tools` later are not governed until you call `govern_agent` again.
|
|
93
|
+
|
|
94
|
+
## Quickstart
|
|
95
|
+
|
|
96
|
+
`examples/quickstart.py` runs offline. The SDK's own
|
|
97
|
+
[`ScriptedModel`](https://openai.github.io/openai-agents-python/testing/) stands in for an LLM:
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
"""Gate OpenAI Agents SDK tool calls with ACGS. Runs offline: the SDK's ScriptedModel stands in for an LLM."""
|
|
101
|
+
|
|
102
|
+
import json
|
|
103
|
+
|
|
104
|
+
from acgs_lite import Constitution, Rule, Severity, ViolationAction
|
|
105
|
+
from agents import Agent, RunConfig, Runner, RunResult, ToolCallOutputItem, function_tool
|
|
106
|
+
from agents.testing import ScriptedModel, assistant_message, function_call
|
|
107
|
+
|
|
108
|
+
from openai_agents_acgs import ACGSToolGuard
|
|
109
|
+
|
|
110
|
+
config = RunConfig(tracing_disabled=True)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
@function_tool
|
|
114
|
+
def send_email(to: str, body: str) -> str:
|
|
115
|
+
"""Send an email."""
|
|
116
|
+
return f"sent to {to}"
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
@function_tool
|
|
120
|
+
def get_weather(city: str) -> str:
|
|
121
|
+
"""Get the weather for a city."""
|
|
122
|
+
return f"Sunny in {city}"
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
@function_tool
|
|
126
|
+
def issue_refund(order_id: str, amount: float) -> str:
|
|
127
|
+
"""Refund an order."""
|
|
128
|
+
return f"refunded {amount} for {order_id}"
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def print_tool_results(result: RunResult) -> None:
|
|
132
|
+
for item in result.new_items:
|
|
133
|
+
if isinstance(item, ToolCallOutputItem):
|
|
134
|
+
print(f"{item.call_id}: {item.output}")
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
# 1. The default constitution blocks the email that leaks a password; the weather lookup runs.
|
|
138
|
+
model = ScriptedModel(
|
|
139
|
+
[
|
|
140
|
+
[
|
|
141
|
+
function_call(
|
|
142
|
+
"send_email", {"to": "ops@example.com", "body": "The admin password is hunter2"}, call_id="email-1"
|
|
143
|
+
),
|
|
144
|
+
function_call("get_weather", {"city": "Paris"}, call_id="weather-1"),
|
|
145
|
+
],
|
|
146
|
+
[assistant_message("Done.")],
|
|
147
|
+
]
|
|
148
|
+
)
|
|
149
|
+
guard = ACGSToolGuard()
|
|
150
|
+
agent = guard.govern_agent(Agent(name="Assistant", model=model, tools=[send_email, get_weather]))
|
|
151
|
+
print_tool_results(Runner.run_sync(agent, "Hi", run_config=config))
|
|
152
|
+
|
|
153
|
+
# 2. A review rule: refunds pause the run for approval, then run once approved.
|
|
154
|
+
refunds = Constitution.from_rules(
|
|
155
|
+
[
|
|
156
|
+
Rule(
|
|
157
|
+
id="REFUND-REVIEW",
|
|
158
|
+
text="Refunds need human approval",
|
|
159
|
+
severity=Severity.HIGH,
|
|
160
|
+
keywords=["refund"],
|
|
161
|
+
workflow_action=ViolationAction.REQUIRE_HUMAN_REVIEW,
|
|
162
|
+
)
|
|
163
|
+
],
|
|
164
|
+
name="refunds",
|
|
165
|
+
)
|
|
166
|
+
model = ScriptedModel(
|
|
167
|
+
[
|
|
168
|
+
[function_call("issue_refund", {"order_id": "A-17", "amount": 20.0}, call_id="refund-1")],
|
|
169
|
+
[assistant_message("Done.")],
|
|
170
|
+
]
|
|
171
|
+
)
|
|
172
|
+
guard = ACGSToolGuard(constitution=refunds)
|
|
173
|
+
agent = guard.govern_agent(Agent(name="Support", model=model, tools=[issue_refund]))
|
|
174
|
+
result = Runner.run_sync(agent, "Refund order A-17", run_config=config)
|
|
175
|
+
state = result.to_state()
|
|
176
|
+
for interruption in result.interruptions:
|
|
177
|
+
decision = guard.evaluate(str(interruption.tool_name), json.loads(interruption.arguments or "{}"))
|
|
178
|
+
print(f"paused: {decision.tool_name}: {decision.reason}")
|
|
179
|
+
state.approve(interruption) # a person approves the call
|
|
180
|
+
|
|
181
|
+
print_tool_results(Runner.run_sync(agent, state, run_config=config))
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
```text
|
|
185
|
+
email-1: Tool call 'send_email' was not executed. Blocked by ACGS rule ACGS-006 (Agents must not expose sensitive data in responses).
|
|
186
|
+
weather-1: Sunny in Paris
|
|
187
|
+
paused: issue_refund: Human review required by ACGS rule REFUND-REVIEW (Refunds need human approval).
|
|
188
|
+
refund-1: refunded 20.0 for A-17
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## How a tool call is evaluated
|
|
192
|
+
|
|
193
|
+
1. The tool name (`namespace.name` for namespaced tools) and the call's JSON arguments are rendered as text by
|
|
194
|
+
`format_tool_call`:
|
|
195
|
+
|
|
196
|
+
```python
|
|
197
|
+
from openai_agents_acgs import format_tool_call
|
|
198
|
+
|
|
199
|
+
print(format_tool_call("update_config", {"service": "billing", "change": "disable\nlogging", "dryRun": False}))
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
```text
|
|
203
|
+
tool: update_config
|
|
204
|
+
arguments:
|
|
205
|
+
change: disable logging
|
|
206
|
+
dryRun: false
|
|
207
|
+
service: billing
|
|
208
|
+
normalized: update config | dry run
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Each argument leaf is one `path: value` line (nested values use `a.b[0].c` paths), so both argument names and
|
|
212
|
+
values are visible to rules. Whitespace inside values is collapsed so phrases split across lines still match.
|
|
213
|
+
Identifiers written in `snake_case`, `kebab-case` or `camelCase` (the tool name, argument names, and
|
|
214
|
+
single-token string values) are repeated as plain words on the `normalized:` line, so the keyword
|
|
215
|
+
`skip audit` also matches a tool called `skip_audit`. Nothing is truncated.
|
|
216
|
+
Pass `tool_call_formatter=` to use a different representation.
|
|
217
|
+
|
|
218
|
+
2. The text is validated with `acgs_lite.GovernanceEngine.validate` against the constitution
|
|
219
|
+
(`Constitution.default()` unless you pass one).
|
|
220
|
+
|
|
221
|
+
3. The result decides what happens to the call:
|
|
222
|
+
|
|
223
|
+
| ACGS outcome | Result |
|
|
224
|
+
|---|---|
|
|
225
|
+
| No blocking rule matched (warn-only rules may match) | The tool runs |
|
|
226
|
+
| Only `require_human_review` / `escalate_to_senior` rules matched | Review (see below) |
|
|
227
|
+
| Any `block`, `block_and_notify` or `halt_and_alert` rule matched | Denied |
|
|
228
|
+
| A rule ID not found in the constitution, or an invalid result without a rule | Denied |
|
|
229
|
+
| Any exception while reading or evaluating the tool call | Denied (fail-closed) |
|
|
230
|
+
|
|
231
|
+
A matched blocking rule is never downgraded to review because a review rule matched as well.
|
|
232
|
+
The denial reason then lists only the blocking rules after "Blocked by" and names the matched review
|
|
233
|
+
rules separately.
|
|
234
|
+
|
|
235
|
+
A rejected call returns `"Tool call '<name>' was not executed. <reason>"` to the model as the tool result. The
|
|
236
|
+
reason names the matched rules but never repeats argument values. Every check is also recorded in
|
|
237
|
+
`result.tool_input_guardrail_results` under the guardrail name `acgs`, with the decision (`verdict`, `reason`,
|
|
238
|
+
`rule_ids`, `error`) in `output.output_info`.
|
|
239
|
+
|
|
240
|
+
`ACGSToolGuard.evaluate(tool_name, args)` returns the underlying `GovernanceDecision` without running anything.
|
|
241
|
+
|
|
242
|
+
### When the check runs
|
|
243
|
+
|
|
244
|
+
For a governed tool, the approval policy runs first, when the model emits the call:
|
|
245
|
+
|
|
246
|
+
| ACGS verdict | `needs_approval` returns |
|
|
247
|
+
|---|---|
|
|
248
|
+
| Blocked | `False`. Nobody is asked; the input guardrail rejects the call |
|
|
249
|
+
| Review (`on_review="approve"`) | `True`. The run pauses with a `ToolApprovalItem` |
|
|
250
|
+
| Review (`on_review="deny"`) | `False`. The input guardrail rejects the call |
|
|
251
|
+
| Allowed | The tool's own `needs_approval` (`bool` or callable), unchanged |
|
|
252
|
+
|
|
253
|
+
The input guardrail then runs immediately before the tool executes: in the same run for calls that need no
|
|
254
|
+
approval, and in the resumed run for approved calls. It evaluates the call again, so a call that is blocked
|
|
255
|
+
when it is about to run is rejected even if it was approved. A call that needs review runs only if the SDK has
|
|
256
|
+
recorded an approval for it (`RunContextWrapper.is_tool_approved`); a rejected call never reaches the guardrail,
|
|
257
|
+
and the model gets the SDK's rejection message. The guard runs before the tool's own input guardrails, and
|
|
258
|
+
`RunHooks.on_tool_start` / `AgentHooks.on_tool_start` only run for calls the guardrails let through.
|
|
259
|
+
|
|
260
|
+
## Review
|
|
261
|
+
|
|
262
|
+
```python
|
|
263
|
+
from typing import Any
|
|
264
|
+
|
|
265
|
+
from agents import Agent, Runner
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
async def run_with_review(agent: Agent[Any], prompt: str) -> Any:
|
|
269
|
+
result = await Runner.run(agent, prompt)
|
|
270
|
+
while result.interruptions:
|
|
271
|
+
state = result.to_state()
|
|
272
|
+
for interruption in result.interruptions:
|
|
273
|
+
state.approve(interruption) # or state.reject(interruption, rejection_message="...")
|
|
274
|
+
result = await Runner.run(agent, state)
|
|
275
|
+
return result.final_output
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
This is the SDK's own [approval flow](https://openai.github.io/openai-agents-python/human_in_the_loop/): it works
|
|
279
|
+
the same for streamed runs (`Runner.run_streamed`) and for states saved with `state.to_string()` and restored with
|
|
280
|
+
`RunState.from_string(agent, ...)`. `state.approve(interruption, always_approve=True)` approves later review calls
|
|
281
|
+
of the same tool in that run without pausing again; blocked calls are still rejected. Interruptions do not carry
|
|
282
|
+
the ACGS decision; call `guard.evaluate(interruption.tool_name, json.loads(interruption.arguments))` to show the
|
|
283
|
+
reason to a reviewer, as the quickstart does.
|
|
284
|
+
|
|
285
|
+
Use `on_review="deny"` for runs nobody can approve. Leave `RunConfig.tool_execution.pre_approval_tool_input_guardrails`
|
|
286
|
+
off: when it is on, the SDK runs the input guardrail before the approval is requested, and the guard rejects calls
|
|
287
|
+
that need review because they are not approved yet. Blocked calls are never sent for approval without it.
|
|
288
|
+
|
|
289
|
+
## What is covered
|
|
290
|
+
|
|
291
|
+
| Tool | Checked | How |
|
|
292
|
+
|---|---|---|
|
|
293
|
+
| `FunctionTool` (`@function_tool`, `FunctionTool(...)`, `tool_namespace(...)`) | Yes | `govern_agent` / `govern_tool` |
|
|
294
|
+
| `Agent.as_tool(...)` | The call to the agent tool, yes | It is a `FunctionTool`; a blocked call never starts the inner agent |
|
|
295
|
+
| Tools of the agent behind `Agent.as_tool` | Only if governed | Call `govern_agent` on that agent too |
|
|
296
|
+
| Handoffs | The handoff itself, no | Handoffs do not use the tool pipeline. Call `govern_agent` on every agent a run can hand off to |
|
|
297
|
+
| Local MCP servers (`MCPServerStdio`, `MCPServerSse`, `MCPServerStreamableHttp`) | Yes, if given the guardrail | `tool_input_guardrails=[guard.input_guardrail]` on the server |
|
|
298
|
+
| Hosted tools (`WebSearchTool`, `FileSearchTool`, `CodeInterpreterTool`, `ImageGenerationTool`, `HostedMCPTool`, `ToolSearchTool`) | No | The provider runs them; `govern_agent` refuses them |
|
|
299
|
+
| `ComputerTool`, `ShellTool`, `LocalShellTool`, `ApplyPatchTool`, `CustomTool`, `ProgrammaticToolCallingTool` | No | They do not use the tool guardrail pipeline; `govern_agent` refuses them |
|
|
300
|
+
|
|
301
|
+
For local MCP servers, pass the guardrail to the server; `govern_agent` refuses an agent whose MCP servers do not
|
|
302
|
+
have it:
|
|
303
|
+
|
|
304
|
+
```python
|
|
305
|
+
from agents import Agent
|
|
306
|
+
from agents.mcp import MCPServerStdio
|
|
307
|
+
|
|
308
|
+
from openai_agents_acgs import ACGSToolGuard
|
|
309
|
+
|
|
310
|
+
guard = ACGSToolGuard()
|
|
311
|
+
server = MCPServerStdio(
|
|
312
|
+
params={"command": "npx", "args": ["-y", "@modelcontextprotocol/server-filesystem", "."]},
|
|
313
|
+
tool_input_guardrails=[guard.input_guardrail],
|
|
314
|
+
require_approval="always", # optional: lets review calls run once approved
|
|
315
|
+
)
|
|
316
|
+
agent = guard.govern_agent(Agent(name="Files", mcp_servers=[server]))
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
MCP tools get the guardrail but not the approval policy: the SDK's `require_approval` callable for MCP servers
|
|
320
|
+
receives the tool, not the call's arguments. Without `require_approval`, MCP calls that need review are rejected.
|
|
321
|
+
With `require_approval="always"`, every call is sent for approval, review calls run once approved, and blocked
|
|
322
|
+
calls are rejected after approval.
|
|
323
|
+
|
|
324
|
+
## Custom constitutions
|
|
325
|
+
|
|
326
|
+
```python
|
|
327
|
+
from acgs_lite import Constitution, Rule, Severity, ViolationAction
|
|
328
|
+
|
|
329
|
+
from openai_agents_acgs import ACGSToolGuard
|
|
330
|
+
|
|
331
|
+
constitution = Constitution.from_rules(
|
|
332
|
+
[
|
|
333
|
+
Rule(
|
|
334
|
+
id="NO-DESTRUCTIVE-SQL",
|
|
335
|
+
text="No destructive SQL",
|
|
336
|
+
severity=Severity.CRITICAL,
|
|
337
|
+
keywords=["drop table", "truncate table"],
|
|
338
|
+
),
|
|
339
|
+
Rule(
|
|
340
|
+
id="REFUND-REVIEW",
|
|
341
|
+
text="Refunds need approval",
|
|
342
|
+
severity=Severity.HIGH,
|
|
343
|
+
keywords=["refund"],
|
|
344
|
+
workflow_action=ViolationAction.REQUIRE_HUMAN_REVIEW,
|
|
345
|
+
),
|
|
346
|
+
],
|
|
347
|
+
name="payments",
|
|
348
|
+
)
|
|
349
|
+
guard = ACGSToolGuard(constitution=constitution, agent_id="payments-app")
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
`Constitution.from_yaml("rules.yaml")` works as well. To enforce two constitutions, govern the tools with two
|
|
353
|
+
guards; a call runs only if both allow it.
|
|
354
|
+
|
|
355
|
+
## Options
|
|
356
|
+
|
|
357
|
+
| Argument | Default | Meaning |
|
|
358
|
+
|---|---|---|
|
|
359
|
+
| `constitution` | `Constitution.default()` | Rules to enforce |
|
|
360
|
+
| `agent_id` | `"openai-agents"` | Agent identifier recorded by ACGS |
|
|
361
|
+
| `on_review` | `"approve"` | `"approve"` (pause for approval) or `"deny"` |
|
|
362
|
+
| `on_deny` | `"reject"` | `"reject"` (tool result with the reason) or `"raise"` (`ToolInputGuardrailTripwireTriggered`) |
|
|
363
|
+
| `tool_call_formatter` | `format_tool_call` | `(tool_name, args) -> str` used as the evaluated text |
|
|
364
|
+
|
|
365
|
+
## Limitations
|
|
366
|
+
|
|
367
|
+
- Only tools listed as checked in [What is covered](#what-is-covered). User input, model output, and tool results
|
|
368
|
+
are not checked; the SDK's agent-level input and output guardrails and tool output guardrails remain available
|
|
369
|
+
for that.
|
|
370
|
+
- The guard is applied per tool and per agent. A tool or agent it was not applied to (a handoff target, the agent
|
|
371
|
+
behind `Agent.as_tool`, a tool added after `govern_agent`) is not checked.
|
|
372
|
+
- Tested with `Runner.run`, `Runner.run_sync`, `Runner.run_streamed`, parallel tool calls in one model turn,
|
|
373
|
+
approval, rejection and resume (including serialized `RunState`), `always_approve`, tools with their own
|
|
374
|
+
`needs_approval`, namespaced tools, `Agent.as_tool`, handoffs, an in-memory local MCP server, and `RunHooks`, against
|
|
375
|
+
openai-agents 0.22.1 and the latest release. Realtime and voice sessions, sandbox agents, and durable
|
|
376
|
+
execution integrations are not tested.
|
|
377
|
+
- `Constitution.default()` focuses on governance integrity (self-approval, skipping audits, privilege escalation)
|
|
378
|
+
and on secrets and personal data (`password`, `api_key`, `sk-...` and `ghp_...` tokens, US SSN
|
|
379
|
+
patterns). It does **not** cover destructive operations such as `rm -rf` or `DROP TABLE`. Write rules for the
|
|
380
|
+
tools you expose. `tests/test_default_constitution.py` lists what the default rules do and do not catch.
|
|
381
|
+
- Keyword rules in acgs-lite 3.0.1 match literal, case-insensitive substrings. They do not understand negation
|
|
382
|
+
("never share the password" matches `password`) and do not treat `-`, `_` and spaces as equivalent
|
|
383
|
+
(`api-key` does not match the keyword `api_key`).
|
|
384
|
+
- Because argument names are part of the evaluated text, a tool with a parameter named `password` is denied under
|
|
385
|
+
the default constitution whenever it is called.
|
|
386
|
+
- The OpenAI Agents SDK is pre-1.0 and its minor releases can contain breaking changes. The adapter allows
|
|
387
|
+
`openai-agents<1` and is tested against the oldest supported and the latest release.
|
|
388
|
+
|
|
389
|
+
## Development
|
|
390
|
+
|
|
391
|
+
```bash
|
|
392
|
+
uv sync --group dev # or use hatch: hatch run prepare
|
|
393
|
+
hatch run format # ruff format + ruff check --fix
|
|
394
|
+
hatch run lint # ruff check + ruff format --check
|
|
395
|
+
hatch run typecheck # mypy --strict
|
|
396
|
+
hatch run cov # pytest with a 100% branch-coverage gate, network disabled
|
|
397
|
+
hatch run min:cov # the same tests against openai-agents 0.22.1 and acgs-lite 3.0.1
|
|
398
|
+
pre-commit install # format, lint, type check and tests on commit; commitizen on commit messages
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
Commit messages follow [Conventional Commits](https://www.conventionalcommits.org/); `cz bump` updates the version
|
|
402
|
+
and the changelog.
|
|
403
|
+
|
|
404
|
+
## License
|
|
405
|
+
|
|
406
|
+
Apache-2.0. See [LICENSE](LICENSE).
|