agentpriv 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.
- agentpriv-0.1.0/.github/workflows/publish.yml +20 -0
- agentpriv-0.1.0/.gitignore +6 -0
- agentpriv-0.1.0/LICENSE +21 -0
- agentpriv-0.1.0/PKG-INFO +115 -0
- agentpriv-0.1.0/README.md +102 -0
- agentpriv-0.1.0/agentpriv/__init__.py +4 -0
- agentpriv-0.1.0/agentpriv/core.py +93 -0
- agentpriv-0.1.0/agentpriv/exceptions.py +2 -0
- agentpriv-0.1.0/agentpriv/prompt.py +7 -0
- agentpriv-0.1.0/pyproject.toml +22 -0
- agentpriv-0.1.0/tests/test_core.py +180 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
environment: pypi
|
|
11
|
+
permissions:
|
|
12
|
+
id-token: write
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v4
|
|
15
|
+
- uses: actions/setup-python@v5
|
|
16
|
+
with:
|
|
17
|
+
python-version: "3.12"
|
|
18
|
+
- run: pip install build
|
|
19
|
+
- run: python -m build
|
|
20
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
agentpriv-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 agentpriv contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
agentpriv-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: agentpriv
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: sudo for AI agents - allow, deny, or ask before any tool runs
|
|
5
|
+
Project-URL: Homepage, https://github.com/nichkej/agentpriv
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Requires-Python: >=3.10
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
# agentpriv
|
|
15
|
+
|
|
16
|
+
sudo for AI agents - allow, deny, or ask before any tool runs.
|
|
17
|
+
|
|
18
|
+
AI agents run tools autonomously, but some calls are too risky to run unchecked. agentpriv gives you a permission layer to control what goes through.
|
|
19
|
+
|
|
20
|
+
## Why
|
|
21
|
+
|
|
22
|
+
- **One place** - guard a tool once, every agent using it gets the same rule
|
|
23
|
+
- **Gradual trust** - start on `"ask"`, promote to `"allow"` as you gain confidence
|
|
24
|
+
- **Visibility** - every blocked or prompted call is printed with full arguments, so you see exactly what the agent is trying to do
|
|
25
|
+
- **Framework agnostic** - plain wrapper around your functions, so it works with any agent framework or none at all
|
|
26
|
+
|
|
27
|
+
## Install
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
pip install agentpriv
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Quick start
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
from agentpriv import guard, guard_all, AgentPrivDenied
|
|
37
|
+
|
|
38
|
+
safe_send = guard(send_message, policy="ask")
|
|
39
|
+
|
|
40
|
+
tools = guard_all(
|
|
41
|
+
[read_messages, send_message, delete_channel],
|
|
42
|
+
policy={
|
|
43
|
+
"delete_*": "deny",
|
|
44
|
+
"send_*": "ask",
|
|
45
|
+
"*": "allow",
|
|
46
|
+
}
|
|
47
|
+
)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Three modes
|
|
51
|
+
|
|
52
|
+
| Mode | What happens |
|
|
53
|
+
| --------- | ----------------------------------------------------------------- |
|
|
54
|
+
| `"allow"` | Runs normally, no interruption |
|
|
55
|
+
| `"deny"` | Raises `AgentPrivDenied` immediately, the function never executes |
|
|
56
|
+
| `"ask"` | Pauses, shows the call in your terminal, waits for y/n |
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
agentpriv: send_message(channel='general', text='deploying now')
|
|
60
|
+
Allow this call? [y/n]: y # runs the function
|
|
61
|
+
Allow this call? [y/n]: n # raises AgentPrivDenied
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## `on_deny` - raise or return
|
|
65
|
+
|
|
66
|
+
By default, denied calls raise `AgentPrivDenied`. When using frameworks, set `on_deny="return"` so the LLM sees the denial as a tool result instead of crashing:
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
# Plain Python - raises exception
|
|
70
|
+
safe = guard(delete_channel, policy="deny")
|
|
71
|
+
|
|
72
|
+
# Frameworks - returns error string to the LLM
|
|
73
|
+
safe = guard(delete_channel, policy="deny", on_deny="return")
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Works with any framework
|
|
77
|
+
|
|
78
|
+
Guard first, then pass to your framework as usual:
|
|
79
|
+
|
|
80
|
+
**OpenAI Agents SDK**
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
safe_delete = function_tool(guard(delete_db, policy="ask", on_deny="return"))
|
|
84
|
+
agent = Agent(name="Demo", tools=[safe_delete])
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
**LangChain / LangGraph**
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
safe_delete = tool(guard(delete_db, policy="ask", on_deny="return"))
|
|
91
|
+
agent = create_agent(model=llm, tools=[safe_delete])
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
**PydanticAI**
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
agent = Agent("openai:gpt-4o", tools=[guard(delete_db, policy="ask", on_deny="return")])
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
**CrewAI**
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
safe_delete = tool("Delete DB")(guard(delete_db, policy="ask", on_deny="return"))
|
|
104
|
+
agent = Agent(role="DBA", tools=[safe_delete])
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Policy matching
|
|
108
|
+
|
|
109
|
+
- Patterns use glob syntax (`fnmatch`) against the function's `__name__`
|
|
110
|
+
- More specific patterns win over wildcards (`delete_channel` > `delete_*` > `*`)
|
|
111
|
+
- If a function doesn't match any pattern, it defaults to `"deny"` - so forgetting a rule blocks the call rather than silently allowing it. Use `"*": "allow"` as a catch-all to opt out
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
MIT
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# agentpriv
|
|
2
|
+
|
|
3
|
+
sudo for AI agents - allow, deny, or ask before any tool runs.
|
|
4
|
+
|
|
5
|
+
AI agents run tools autonomously, but some calls are too risky to run unchecked. agentpriv gives you a permission layer to control what goes through.
|
|
6
|
+
|
|
7
|
+
## Why
|
|
8
|
+
|
|
9
|
+
- **One place** - guard a tool once, every agent using it gets the same rule
|
|
10
|
+
- **Gradual trust** - start on `"ask"`, promote to `"allow"` as you gain confidence
|
|
11
|
+
- **Visibility** - every blocked or prompted call is printed with full arguments, so you see exactly what the agent is trying to do
|
|
12
|
+
- **Framework agnostic** - plain wrapper around your functions, so it works with any agent framework or none at all
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
pip install agentpriv
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick start
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from agentpriv import guard, guard_all, AgentPrivDenied
|
|
24
|
+
|
|
25
|
+
safe_send = guard(send_message, policy="ask")
|
|
26
|
+
|
|
27
|
+
tools = guard_all(
|
|
28
|
+
[read_messages, send_message, delete_channel],
|
|
29
|
+
policy={
|
|
30
|
+
"delete_*": "deny",
|
|
31
|
+
"send_*": "ask",
|
|
32
|
+
"*": "allow",
|
|
33
|
+
}
|
|
34
|
+
)
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Three modes
|
|
38
|
+
|
|
39
|
+
| Mode | What happens |
|
|
40
|
+
| --------- | ----------------------------------------------------------------- |
|
|
41
|
+
| `"allow"` | Runs normally, no interruption |
|
|
42
|
+
| `"deny"` | Raises `AgentPrivDenied` immediately, the function never executes |
|
|
43
|
+
| `"ask"` | Pauses, shows the call in your terminal, waits for y/n |
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
agentpriv: send_message(channel='general', text='deploying now')
|
|
47
|
+
Allow this call? [y/n]: y # runs the function
|
|
48
|
+
Allow this call? [y/n]: n # raises AgentPrivDenied
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## `on_deny` - raise or return
|
|
52
|
+
|
|
53
|
+
By default, denied calls raise `AgentPrivDenied`. When using frameworks, set `on_deny="return"` so the LLM sees the denial as a tool result instead of crashing:
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
# Plain Python - raises exception
|
|
57
|
+
safe = guard(delete_channel, policy="deny")
|
|
58
|
+
|
|
59
|
+
# Frameworks - returns error string to the LLM
|
|
60
|
+
safe = guard(delete_channel, policy="deny", on_deny="return")
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Works with any framework
|
|
64
|
+
|
|
65
|
+
Guard first, then pass to your framework as usual:
|
|
66
|
+
|
|
67
|
+
**OpenAI Agents SDK**
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
safe_delete = function_tool(guard(delete_db, policy="ask", on_deny="return"))
|
|
71
|
+
agent = Agent(name="Demo", tools=[safe_delete])
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
**LangChain / LangGraph**
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
safe_delete = tool(guard(delete_db, policy="ask", on_deny="return"))
|
|
78
|
+
agent = create_agent(model=llm, tools=[safe_delete])
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**PydanticAI**
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
agent = Agent("openai:gpt-4o", tools=[guard(delete_db, policy="ask", on_deny="return")])
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
**CrewAI**
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
safe_delete = tool("Delete DB")(guard(delete_db, policy="ask", on_deny="return"))
|
|
91
|
+
agent = Agent(role="DBA", tools=[safe_delete])
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Policy matching
|
|
95
|
+
|
|
96
|
+
- Patterns use glob syntax (`fnmatch`) against the function's `__name__`
|
|
97
|
+
- More specific patterns win over wildcards (`delete_channel` > `delete_*` > `*`)
|
|
98
|
+
- If a function doesn't match any pattern, it defaults to `"deny"` - so forgetting a rule blocks the call rather than silently allowing it. Use `"*": "allow"` as a catch-all to opt out
|
|
99
|
+
|
|
100
|
+
## License
|
|
101
|
+
|
|
102
|
+
MIT
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
import inspect
|
|
3
|
+
from fnmatch import fnmatch
|
|
4
|
+
|
|
5
|
+
from .exceptions import AgentPrivDenied
|
|
6
|
+
from .prompt import ask_human
|
|
7
|
+
|
|
8
|
+
VALID_POLICIES = ("allow", "deny", "ask")
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _resolve_policy(name, policy):
|
|
12
|
+
"""
|
|
13
|
+
Resolve which policy applies to a function name.
|
|
14
|
+
|
|
15
|
+
If policy is a string, return it directly.
|
|
16
|
+
If policy is a dict, find the best matching pattern.
|
|
17
|
+
More specific patterns (fewer wildcards / longer) win over broad ones.
|
|
18
|
+
Falls back to "deny" if nothing matches.
|
|
19
|
+
"""
|
|
20
|
+
if isinstance(policy, str):
|
|
21
|
+
if policy not in VALID_POLICIES:
|
|
22
|
+
raise ValueError(f"Invalid policy {policy!r}, must be one of {VALID_POLICIES}")
|
|
23
|
+
return policy
|
|
24
|
+
|
|
25
|
+
best_policy = "deny"
|
|
26
|
+
best_specificity = float("-inf")
|
|
27
|
+
|
|
28
|
+
for pattern, pol in policy.items():
|
|
29
|
+
if pol not in VALID_POLICIES:
|
|
30
|
+
raise ValueError(f"Invalid policy {pol!r} for pattern {pattern!r}")
|
|
31
|
+
if fnmatch(name, pattern):
|
|
32
|
+
# specificity = number of non-wildcard characters
|
|
33
|
+
specificity = len(pattern.replace("*", "").replace("?", ""))
|
|
34
|
+
if specificity > best_specificity:
|
|
35
|
+
best_specificity = specificity
|
|
36
|
+
best_policy = pol
|
|
37
|
+
|
|
38
|
+
return best_policy
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def guard(fn, policy="ask", on_deny="raise"):
|
|
42
|
+
"""
|
|
43
|
+
Wrap a callable with a permission policy.
|
|
44
|
+
|
|
45
|
+
Returns a new callable with the same signature that checks the policy
|
|
46
|
+
before executing. Supports both sync and async functions.
|
|
47
|
+
|
|
48
|
+
on_deny controls what happens when a call is denied:
|
|
49
|
+
- "raise": raise AgentPrivDenied (default, good for plain Python)
|
|
50
|
+
- "return": return an error string (good for frameworks like PydanticAI,
|
|
51
|
+
LangChain, etc. where the LLM sees the tool result)
|
|
52
|
+
"""
|
|
53
|
+
if on_deny not in ("raise", "return"):
|
|
54
|
+
raise ValueError(f"Invalid on_deny {on_deny!r}, must be 'raise' or 'return'")
|
|
55
|
+
|
|
56
|
+
resolved = _resolve_policy(fn.__name__, policy)
|
|
57
|
+
|
|
58
|
+
def _denied(reason):
|
|
59
|
+
msg = f"Call to {fn.__name__}() denied by {reason}"
|
|
60
|
+
if on_deny == "return":
|
|
61
|
+
return msg
|
|
62
|
+
raise AgentPrivDenied(msg)
|
|
63
|
+
|
|
64
|
+
if inspect.iscoroutinefunction(fn):
|
|
65
|
+
@functools.wraps(fn)
|
|
66
|
+
async def wrapper(*args, **kwargs):
|
|
67
|
+
if resolved == "deny":
|
|
68
|
+
return _denied("policy")
|
|
69
|
+
if resolved == "ask" and not ask_human(fn.__name__, args, kwargs):
|
|
70
|
+
return _denied("human")
|
|
71
|
+
return await fn(*args, **kwargs)
|
|
72
|
+
else:
|
|
73
|
+
@functools.wraps(fn)
|
|
74
|
+
def wrapper(*args, **kwargs):
|
|
75
|
+
if resolved == "deny":
|
|
76
|
+
return _denied("policy")
|
|
77
|
+
if resolved == "ask" and not ask_human(fn.__name__, args, kwargs):
|
|
78
|
+
return _denied("human")
|
|
79
|
+
return fn(*args, **kwargs)
|
|
80
|
+
|
|
81
|
+
return wrapper
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def guard_all(fns, policy=None):
|
|
85
|
+
"""
|
|
86
|
+
Wrap a list of callables with a policy dict.
|
|
87
|
+
|
|
88
|
+
Policy is a dict mapping glob patterns to policy strings.
|
|
89
|
+
Returns a list of wrapped callables in the same order.
|
|
90
|
+
"""
|
|
91
|
+
if policy is None:
|
|
92
|
+
policy = {"*": "ask"}
|
|
93
|
+
return [guard(fn, policy) for fn in fns]
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
def ask_human(name, args, kwargs):
|
|
2
|
+
"""Print call details and ask the human for y/n approval."""
|
|
3
|
+
parts = [repr(a) for a in args] + [f"{k}={v!r}" for k, v in kwargs.items()]
|
|
4
|
+
sig = ", ".join(parts)
|
|
5
|
+
print(f"\nagentpriv: {name}({sig})")
|
|
6
|
+
answer = input("Allow this call? [y/n]: ").strip().lower()
|
|
7
|
+
return answer == "y"
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "agentpriv"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "sudo for AI agents - allow, deny, or ask before any tool runs"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"License :: OSI Approved :: MIT License",
|
|
15
|
+
"Operating System :: OS Independent",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
[project.urls]
|
|
19
|
+
Homepage = "https://github.com/nichkej/agentpriv"
|
|
20
|
+
|
|
21
|
+
[tool.pytest.ini_options]
|
|
22
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import inspect
|
|
3
|
+
from unittest.mock import patch
|
|
4
|
+
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from agentpriv import AgentPrivDenied, guard, guard_all
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# --- helpers ---
|
|
11
|
+
|
|
12
|
+
def read_messages():
|
|
13
|
+
return ["msg1", "msg2"]
|
|
14
|
+
|
|
15
|
+
def send_message(channel, text):
|
|
16
|
+
return f"sent to {channel}: {text}"
|
|
17
|
+
|
|
18
|
+
def delete_channel(name):
|
|
19
|
+
return f"deleted {name}"
|
|
20
|
+
|
|
21
|
+
def delete_repo(name):
|
|
22
|
+
return f"deleted repo {name}"
|
|
23
|
+
|
|
24
|
+
async def async_read():
|
|
25
|
+
return "async result"
|
|
26
|
+
|
|
27
|
+
async def async_delete(name):
|
|
28
|
+
return f"async deleted {name}"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
# --- guard() ---
|
|
32
|
+
|
|
33
|
+
class TestGuardAllow:
|
|
34
|
+
def test_runs_normally(self):
|
|
35
|
+
safe = guard(read_messages, policy="allow")
|
|
36
|
+
assert safe() == read_messages()
|
|
37
|
+
|
|
38
|
+
def test_passes_args(self):
|
|
39
|
+
safe = guard(send_message, policy="allow")
|
|
40
|
+
assert safe("general", text="hello") == send_message("general", text="hello")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class TestGuardDeny:
|
|
44
|
+
def test_raises(self):
|
|
45
|
+
safe = guard(delete_channel, policy="deny")
|
|
46
|
+
with pytest.raises(AgentPrivDenied, match="denied by policy"):
|
|
47
|
+
safe("general")
|
|
48
|
+
|
|
49
|
+
def test_never_calls_function(self):
|
|
50
|
+
calls = []
|
|
51
|
+
def tracked():
|
|
52
|
+
calls.append(1)
|
|
53
|
+
safe = guard(tracked, policy="deny")
|
|
54
|
+
with pytest.raises(AgentPrivDenied):
|
|
55
|
+
safe()
|
|
56
|
+
assert calls == []
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class TestGuardAsk:
|
|
60
|
+
@patch("agentpriv.core.ask_human", return_value=True)
|
|
61
|
+
def test_approved(self, mock_ask):
|
|
62
|
+
safe = guard(send_message, policy="ask")
|
|
63
|
+
assert safe("general", text="hi") == send_message("general", text="hi")
|
|
64
|
+
mock_ask.assert_called_once_with("send_message", ("general",), {"text": "hi"})
|
|
65
|
+
|
|
66
|
+
@patch("agentpriv.core.ask_human", return_value=False)
|
|
67
|
+
def test_denied(self, _):
|
|
68
|
+
safe = guard(send_message, policy="ask")
|
|
69
|
+
with pytest.raises(AgentPrivDenied, match="denied by human"):
|
|
70
|
+
safe("general", text="hi")
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class TestGuardAsync:
|
|
74
|
+
def test_allow(self):
|
|
75
|
+
safe = guard(async_read, policy="allow")
|
|
76
|
+
assert inspect.iscoroutinefunction(safe)
|
|
77
|
+
assert asyncio.run(safe()) == asyncio.run(async_read())
|
|
78
|
+
|
|
79
|
+
def test_deny(self):
|
|
80
|
+
safe = guard(async_delete, policy="deny")
|
|
81
|
+
assert inspect.iscoroutinefunction(safe)
|
|
82
|
+
with pytest.raises(AgentPrivDenied):
|
|
83
|
+
asyncio.run(safe("general"))
|
|
84
|
+
|
|
85
|
+
@patch("agentpriv.core.ask_human", return_value=True)
|
|
86
|
+
def test_ask_approved(self, _):
|
|
87
|
+
safe = guard(async_read, policy="ask")
|
|
88
|
+
assert asyncio.run(safe()) == asyncio.run(async_read())
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
# --- guard_all() ---
|
|
92
|
+
|
|
93
|
+
class TestGuardAll:
|
|
94
|
+
def _make_tools(self, policy):
|
|
95
|
+
return guard_all(
|
|
96
|
+
[read_messages, send_message, delete_channel, delete_repo],
|
|
97
|
+
policy=policy,
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
def test_deny_pattern(self):
|
|
101
|
+
tools = self._make_tools({"delete_*": "deny", "*": "allow"})
|
|
102
|
+
assert tools[0]() == read_messages()
|
|
103
|
+
with pytest.raises(AgentPrivDenied):
|
|
104
|
+
tools[2]("general")
|
|
105
|
+
|
|
106
|
+
@patch("agentpriv.core.ask_human", return_value=True)
|
|
107
|
+
def test_ask_pattern(self, _):
|
|
108
|
+
tools = self._make_tools({"send_*": "ask", "*": "allow"})
|
|
109
|
+
assert tools[1]("general", text="hi") == send_message("general", text="hi")
|
|
110
|
+
|
|
111
|
+
def test_no_match_defaults_to_deny(self):
|
|
112
|
+
tools = self._make_tools({"send_*": "allow"})
|
|
113
|
+
with pytest.raises(AgentPrivDenied):
|
|
114
|
+
tools[0]()
|
|
115
|
+
|
|
116
|
+
def test_specific_pattern_wins(self):
|
|
117
|
+
tools = self._make_tools({
|
|
118
|
+
"delete_channel": "allow",
|
|
119
|
+
"delete_*": "deny",
|
|
120
|
+
"*": "allow",
|
|
121
|
+
})
|
|
122
|
+
assert tools[2]("general") == delete_channel("general")
|
|
123
|
+
with pytest.raises(AgentPrivDenied):
|
|
124
|
+
tools[3]("myrepo")
|
|
125
|
+
|
|
126
|
+
def test_default_policy_is_ask(self):
|
|
127
|
+
tools = guard_all([read_messages])
|
|
128
|
+
with patch("agentpriv.core.ask_human", return_value=False):
|
|
129
|
+
with pytest.raises(AgentPrivDenied):
|
|
130
|
+
tools[0]()
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
# --- on_deny="return" ---
|
|
134
|
+
|
|
135
|
+
class TestOnDenyReturn:
|
|
136
|
+
def test_deny_returns_string(self):
|
|
137
|
+
safe = guard(delete_channel, policy="deny", on_deny="return")
|
|
138
|
+
result = safe("general")
|
|
139
|
+
assert isinstance(result, str)
|
|
140
|
+
assert "denied by policy" in result
|
|
141
|
+
|
|
142
|
+
@patch("agentpriv.core.ask_human", return_value=False)
|
|
143
|
+
def test_ask_denied_returns_string(self, _):
|
|
144
|
+
safe = guard(send_message, policy="ask", on_deny="return")
|
|
145
|
+
result = safe("general", text="hi")
|
|
146
|
+
assert isinstance(result, str)
|
|
147
|
+
assert "denied by human" in result
|
|
148
|
+
|
|
149
|
+
def test_async_deny_returns_string(self):
|
|
150
|
+
safe = guard(async_delete, policy="deny", on_deny="return")
|
|
151
|
+
result = asyncio.run(safe("general"))
|
|
152
|
+
assert isinstance(result, str)
|
|
153
|
+
assert "denied by policy" in result
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
# --- edge cases ---
|
|
157
|
+
|
|
158
|
+
class TestEdgeCases:
|
|
159
|
+
def test_invalid_policy(self):
|
|
160
|
+
with pytest.raises(ValueError):
|
|
161
|
+
guard(read_messages, policy="yolo")
|
|
162
|
+
|
|
163
|
+
def test_invalid_policy_in_dict(self):
|
|
164
|
+
with pytest.raises(ValueError):
|
|
165
|
+
guard_all([read_messages], policy={"*": "yolo"})
|
|
166
|
+
|
|
167
|
+
def test_invalid_on_deny(self):
|
|
168
|
+
with pytest.raises(ValueError):
|
|
169
|
+
guard(read_messages, policy="deny", on_deny="explode")
|
|
170
|
+
|
|
171
|
+
def test_preserves_function_name(self):
|
|
172
|
+
safe = guard(send_message, policy="allow")
|
|
173
|
+
assert safe.__name__ == "send_message"
|
|
174
|
+
|
|
175
|
+
def test_preserves_docstring(self):
|
|
176
|
+
def documented():
|
|
177
|
+
"""My doc."""
|
|
178
|
+
pass
|
|
179
|
+
safe = guard(documented, policy="allow")
|
|
180
|
+
assert safe.__doc__ == "My doc."
|