airg-client 0.3.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.
- airg_client-0.3.0/.gitignore +13 -0
- airg_client-0.3.0/LICENSE +21 -0
- airg_client-0.3.0/PKG-INFO +119 -0
- airg_client-0.3.0/README.md +91 -0
- airg_client-0.3.0/governor_client.py +354 -0
- airg_client-0.3.0/py.typed +0 -0
- airg_client-0.3.0/pyproject.toml +53 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SOVEREIGN AI LAB
|
|
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.
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: airg-client
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Python client for the AI Runtime Governor – runtime AI-agent governance service
|
|
5
|
+
Project-URL: Homepage, https://github.com/othnielObasi/ai-runtime-governor-core
|
|
6
|
+
Project-URL: Repository, https://github.com/othnielObasi/ai-runtime-governor-core
|
|
7
|
+
Project-URL: Documentation, https://github.com/othnielObasi/ai-runtime-governor-core#readme
|
|
8
|
+
Project-URL: Issues, https://github.com/othnielObasi/ai-runtime-governor-core/issues
|
|
9
|
+
Author: Sovereign AI Lab
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: agent-governance,ai-runtime-governor,ai-safety,airg,runtime-policy
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
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: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
23
|
+
Classifier: Topic :: Security
|
|
24
|
+
Classifier: Typing :: Typed
|
|
25
|
+
Requires-Python: >=3.9
|
|
26
|
+
Requires-Dist: httpx>=0.24.0
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# airg-client
|
|
30
|
+
|
|
31
|
+
Python client for the **AI Runtime Governor** – a runtime governance layer for AI agents.
|
|
32
|
+
|
|
33
|
+
The Governor evaluates every tool invocation against configurable policies and
|
|
34
|
+
returns an **allow / block / review** decision before the tool executes.
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install airg-client
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Quick start
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from governor_client import evaluate_action, GovernorBlockedError
|
|
46
|
+
import governor_client
|
|
47
|
+
|
|
48
|
+
# Set your API key (or export GOVERNOR_API_KEY in your environment)
|
|
49
|
+
governor_client.GOVERNOR_API_KEY = "airg_your_key_here"
|
|
50
|
+
governor_client.GOVERNOR_URL = "https://ai-runtime-governor.fly.dev"
|
|
51
|
+
|
|
52
|
+
# Evaluate a tool call against the Governor
|
|
53
|
+
try:
|
|
54
|
+
decision = evaluate_action(
|
|
55
|
+
tool="shell_exec",
|
|
56
|
+
args={"command": "ls -la /tmp"},
|
|
57
|
+
context={"session_id": "abc-123"},
|
|
58
|
+
)
|
|
59
|
+
print(decision["decision"]) # "allow" | "review"
|
|
60
|
+
print(decision["risk_score"]) # 0.0 – 1.0
|
|
61
|
+
print(decision["explanation"])
|
|
62
|
+
except GovernorBlockedError as e:
|
|
63
|
+
print(f"Blocked: {e}")
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Configuration
|
|
67
|
+
|
|
68
|
+
| Environment variable | Default | Description |
|
|
69
|
+
|---|---|---|
|
|
70
|
+
| `GOVERNOR_URL` | `http://localhost:8000` | Base URL of the Governor service |
|
|
71
|
+
| `GOVERNOR_API_KEY` | *(empty)* | API key (`airg_…`) sent as `X-API-Key` header |
|
|
72
|
+
|
|
73
|
+
You can also set them programmatically:
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
import governor_client
|
|
77
|
+
|
|
78
|
+
governor_client.GOVERNOR_URL = "https://ai-runtime-governor.fly.dev"
|
|
79
|
+
governor_client.GOVERNOR_API_KEY = "airg_your_key_here"
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## API
|
|
83
|
+
|
|
84
|
+
### `evaluate_action(tool, args, context=None) → dict`
|
|
85
|
+
|
|
86
|
+
Send a tool call to the Governor for evaluation.
|
|
87
|
+
|
|
88
|
+
Returns the full decision dict:
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
{
|
|
92
|
+
"decision": "allow", # "allow" | "block" | "review"
|
|
93
|
+
"risk_score": 0.15,
|
|
94
|
+
"explanation": "Low-risk read operation",
|
|
95
|
+
"policy_ids": ["shell-guard"],
|
|
96
|
+
"modified_args": None,
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Raises `GovernorBlockedError` if the decision is `"block"`.
|
|
101
|
+
|
|
102
|
+
### `governed_call(tool, args, context=None) → dict`
|
|
103
|
+
|
|
104
|
+
Convenience wrapper around `evaluate_action`. Identical behaviour — callers
|
|
105
|
+
should inspect `decision` for `"review"` and handle accordingly.
|
|
106
|
+
|
|
107
|
+
### `GovernorBlockedError`
|
|
108
|
+
|
|
109
|
+
Exception raised when the Governor blocks a tool invocation. Subclass of
|
|
110
|
+
`RuntimeError`.
|
|
111
|
+
|
|
112
|
+
## Requirements
|
|
113
|
+
|
|
114
|
+
- Python ≥ 3.9
|
|
115
|
+
- [httpx](https://www.python-httpx.org/) ≥ 0.24.0
|
|
116
|
+
|
|
117
|
+
## License
|
|
118
|
+
|
|
119
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# airg-client
|
|
2
|
+
|
|
3
|
+
Python client for the **AI Runtime Governor** – a runtime governance layer for AI agents.
|
|
4
|
+
|
|
5
|
+
The Governor evaluates every tool invocation against configurable policies and
|
|
6
|
+
returns an **allow / block / review** decision before the tool executes.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
pip install airg-client
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Quick start
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
from governor_client import evaluate_action, GovernorBlockedError
|
|
18
|
+
import governor_client
|
|
19
|
+
|
|
20
|
+
# Set your API key (or export GOVERNOR_API_KEY in your environment)
|
|
21
|
+
governor_client.GOVERNOR_API_KEY = "airg_your_key_here"
|
|
22
|
+
governor_client.GOVERNOR_URL = "https://ai-runtime-governor.fly.dev"
|
|
23
|
+
|
|
24
|
+
# Evaluate a tool call against the Governor
|
|
25
|
+
try:
|
|
26
|
+
decision = evaluate_action(
|
|
27
|
+
tool="shell_exec",
|
|
28
|
+
args={"command": "ls -la /tmp"},
|
|
29
|
+
context={"session_id": "abc-123"},
|
|
30
|
+
)
|
|
31
|
+
print(decision["decision"]) # "allow" | "review"
|
|
32
|
+
print(decision["risk_score"]) # 0.0 – 1.0
|
|
33
|
+
print(decision["explanation"])
|
|
34
|
+
except GovernorBlockedError as e:
|
|
35
|
+
print(f"Blocked: {e}")
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Configuration
|
|
39
|
+
|
|
40
|
+
| Environment variable | Default | Description |
|
|
41
|
+
|---|---|---|
|
|
42
|
+
| `GOVERNOR_URL` | `http://localhost:8000` | Base URL of the Governor service |
|
|
43
|
+
| `GOVERNOR_API_KEY` | *(empty)* | API key (`airg_…`) sent as `X-API-Key` header |
|
|
44
|
+
|
|
45
|
+
You can also set them programmatically:
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
import governor_client
|
|
49
|
+
|
|
50
|
+
governor_client.GOVERNOR_URL = "https://ai-runtime-governor.fly.dev"
|
|
51
|
+
governor_client.GOVERNOR_API_KEY = "airg_your_key_here"
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## API
|
|
55
|
+
|
|
56
|
+
### `evaluate_action(tool, args, context=None) → dict`
|
|
57
|
+
|
|
58
|
+
Send a tool call to the Governor for evaluation.
|
|
59
|
+
|
|
60
|
+
Returns the full decision dict:
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
{
|
|
64
|
+
"decision": "allow", # "allow" | "block" | "review"
|
|
65
|
+
"risk_score": 0.15,
|
|
66
|
+
"explanation": "Low-risk read operation",
|
|
67
|
+
"policy_ids": ["shell-guard"],
|
|
68
|
+
"modified_args": None,
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Raises `GovernorBlockedError` if the decision is `"block"`.
|
|
73
|
+
|
|
74
|
+
### `governed_call(tool, args, context=None) → dict`
|
|
75
|
+
|
|
76
|
+
Convenience wrapper around `evaluate_action`. Identical behaviour — callers
|
|
77
|
+
should inspect `decision` for `"review"` and handle accordingly.
|
|
78
|
+
|
|
79
|
+
### `GovernorBlockedError`
|
|
80
|
+
|
|
81
|
+
Exception raised when the Governor blocks a tool invocation. Subclass of
|
|
82
|
+
`RuntimeError`.
|
|
83
|
+
|
|
84
|
+
## Requirements
|
|
85
|
+
|
|
86
|
+
- Python ≥ 3.9
|
|
87
|
+
- [httpx](https://www.python-httpx.org/) ≥ 0.24.0
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
"""
|
|
2
|
+
governed-tools – AI Runtime Governor skill
|
|
3
|
+
================================
|
|
4
|
+
Routes every tool invocation through the AI Runtime Governor service before
|
|
5
|
+
execution. The governor returns an allow/block/review decision; this skill
|
|
6
|
+
raises an error for blocked actions and logs review decisions.
|
|
7
|
+
|
|
8
|
+
Also provides trace observability: ingest agent trace spans, query traces,
|
|
9
|
+
and correlate governance decisions with the agent's execution timeline.
|
|
10
|
+
|
|
11
|
+
Environment variables
|
|
12
|
+
---------------------
|
|
13
|
+
GOVERNOR_URL – Base URL of the governor service (default: http://localhost:8000)
|
|
14
|
+
GOVERNOR_API_KEY – API key for authentication (airg_… format, sent as X-API-Key header)
|
|
15
|
+
"""
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import os
|
|
19
|
+
import time
|
|
20
|
+
from typing import Any, Dict, List, Optional
|
|
21
|
+
|
|
22
|
+
import httpx
|
|
23
|
+
|
|
24
|
+
GOVERNOR_URL = os.getenv("GOVERNOR_URL", "http://localhost:8000")
|
|
25
|
+
GOVERNOR_API_KEY = os.getenv("GOVERNOR_API_KEY", "")
|
|
26
|
+
_TIMEOUT = 10.0
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _headers() -> Dict[str, str]:
|
|
30
|
+
"""Build request headers, including X-API-Key when configured."""
|
|
31
|
+
h: Dict[str, str] = {"Content-Type": "application/json"}
|
|
32
|
+
key = GOVERNOR_API_KEY
|
|
33
|
+
if key:
|
|
34
|
+
h["X-API-Key"] = key
|
|
35
|
+
return h
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class GovernorBlockedError(RuntimeError):
|
|
39
|
+
"""Raised when the governor blocks an action."""
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class GovernorReviewRejectedError(RuntimeError):
|
|
43
|
+
"""Raised when a review decision is rejected by a human operator."""
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class GovernorReviewExpiredError(RuntimeError):
|
|
47
|
+
"""Raised when a review decision times out (hold mode only)."""
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# ---------------------------------------------------------------------------
|
|
51
|
+
# Action evaluation
|
|
52
|
+
# ---------------------------------------------------------------------------
|
|
53
|
+
|
|
54
|
+
def evaluate_action(
|
|
55
|
+
tool: str,
|
|
56
|
+
args: Dict[str, Any],
|
|
57
|
+
context: Optional[Dict[str, Any]] = None,
|
|
58
|
+
*,
|
|
59
|
+
review_mode: str = "proceed",
|
|
60
|
+
hold_timeout: int = 60,
|
|
61
|
+
hold_poll_interval: float = 1.0,
|
|
62
|
+
) -> Dict[str, Any]:
|
|
63
|
+
"""
|
|
64
|
+
Send a tool-call to the governor for evaluation.
|
|
65
|
+
|
|
66
|
+
Returns the full decision dict:
|
|
67
|
+
{ decision, risk_score, explanation, policy_ids, modified_args }
|
|
68
|
+
|
|
69
|
+
Raises GovernorBlockedError if decision == "block".
|
|
70
|
+
|
|
71
|
+
review_mode:
|
|
72
|
+
- "proceed" (default): Return the result immediately for 'review'
|
|
73
|
+
decisions. Callers should inspect decision and handle accordingly.
|
|
74
|
+
- "hold": If decision is 'review', long-poll the hold endpoint until
|
|
75
|
+
a human operator approves/rejects, or the timeout expires.
|
|
76
|
+
Raises GovernorReviewRejectedError if rejected.
|
|
77
|
+
Raises GovernorReviewExpiredError if timed out or expired.
|
|
78
|
+
|
|
79
|
+
hold_timeout: Max seconds to wait in hold mode (1-300, default: 60).
|
|
80
|
+
hold_poll_interval: Seconds between server-side polls (default: 1.0).
|
|
81
|
+
|
|
82
|
+
Tip: include ``trace_id`` and ``span_id`` in *context* to auto-create a
|
|
83
|
+
governance span in the agent's trace tree.
|
|
84
|
+
"""
|
|
85
|
+
if review_mode not in ("proceed", "hold"):
|
|
86
|
+
raise ValueError(f"review_mode must be 'proceed' or 'hold', got '{review_mode}'")
|
|
87
|
+
|
|
88
|
+
payload = {"tool": tool, "args": args, "context": context}
|
|
89
|
+
with httpx.Client(timeout=_TIMEOUT, headers=_headers()) as client:
|
|
90
|
+
resp = client.post(f"{GOVERNOR_URL}/actions/evaluate", json=payload)
|
|
91
|
+
resp.raise_for_status()
|
|
92
|
+
|
|
93
|
+
result = resp.json()
|
|
94
|
+
|
|
95
|
+
if result.get("decision") == "block":
|
|
96
|
+
raise GovernorBlockedError(
|
|
97
|
+
f"Governor blocked tool '{tool}': {result.get('explanation', 'no reason given')}"
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
# Handle review decisions
|
|
101
|
+
if result.get("decision") == "review" and review_mode == "hold":
|
|
102
|
+
escalation_id = result.get("escalation_id")
|
|
103
|
+
if escalation_id:
|
|
104
|
+
hold_result = _hold_for_review(
|
|
105
|
+
escalation_id,
|
|
106
|
+
timeout_seconds=hold_timeout,
|
|
107
|
+
poll_interval=hold_poll_interval,
|
|
108
|
+
)
|
|
109
|
+
result["review_status"] = hold_result.get("status", "unknown")
|
|
110
|
+
result["review_resolved_by"] = hold_result.get("resolved_by")
|
|
111
|
+
result["review_resolution_note"] = hold_result.get("resolution_note")
|
|
112
|
+
|
|
113
|
+
if hold_result.get("timed_out"):
|
|
114
|
+
raise GovernorReviewExpiredError(
|
|
115
|
+
f"Review for tool '{tool}' timed out after {hold_timeout}s "
|
|
116
|
+
f"(escalation_id={escalation_id})"
|
|
117
|
+
)
|
|
118
|
+
if hold_result.get("status") == "rejected":
|
|
119
|
+
raise GovernorReviewRejectedError(
|
|
120
|
+
f"Review for tool '{tool}' was rejected: "
|
|
121
|
+
f"{hold_result.get('resolution_note', 'no reason given')} "
|
|
122
|
+
f"(escalation_id={escalation_id})"
|
|
123
|
+
)
|
|
124
|
+
if hold_result.get("status") == "expired":
|
|
125
|
+
raise GovernorReviewExpiredError(
|
|
126
|
+
f"Review for tool '{tool}' expired before resolution "
|
|
127
|
+
f"(escalation_id={escalation_id})"
|
|
128
|
+
)
|
|
129
|
+
# approved or auto_resolved → continue
|
|
130
|
+
|
|
131
|
+
return result
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _hold_for_review(
|
|
135
|
+
escalation_id: int,
|
|
136
|
+
timeout_seconds: int = 60,
|
|
137
|
+
poll_interval: float = 1.0,
|
|
138
|
+
) -> Dict[str, Any]:
|
|
139
|
+
"""
|
|
140
|
+
Call the hold endpoint to long-poll for review resolution.
|
|
141
|
+
Returns the hold result dict from the server.
|
|
142
|
+
"""
|
|
143
|
+
params = {
|
|
144
|
+
"timeout_seconds": timeout_seconds,
|
|
145
|
+
"poll_interval": poll_interval,
|
|
146
|
+
}
|
|
147
|
+
# Use a longer client timeout than the hold timeout to avoid premature disconnects
|
|
148
|
+
client_timeout = timeout_seconds + 10
|
|
149
|
+
try:
|
|
150
|
+
with httpx.Client(timeout=client_timeout, headers=_headers()) as client:
|
|
151
|
+
resp = client.post(
|
|
152
|
+
f"{GOVERNOR_URL}/escalation/queue/{escalation_id}/hold",
|
|
153
|
+
params=params,
|
|
154
|
+
)
|
|
155
|
+
resp.raise_for_status()
|
|
156
|
+
return resp.json()
|
|
157
|
+
except httpx.TimeoutException:
|
|
158
|
+
return {"event_id": escalation_id, "status": "pending", "timed_out": True}
|
|
159
|
+
except Exception:
|
|
160
|
+
# If the hold endpoint is unavailable, fall through gracefully
|
|
161
|
+
return {"event_id": escalation_id, "status": "pending", "timed_out": True}
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def governed_call(
|
|
165
|
+
tool: str,
|
|
166
|
+
args: Dict[str, Any],
|
|
167
|
+
context: Optional[Dict[str, Any]] = None,
|
|
168
|
+
*,
|
|
169
|
+
review_mode: str = "proceed",
|
|
170
|
+
hold_timeout: int = 60,
|
|
171
|
+
) -> Dict[str, Any]:
|
|
172
|
+
"""
|
|
173
|
+
Convenience wrapper: evaluate then return the decision.
|
|
174
|
+
|
|
175
|
+
review_mode:
|
|
176
|
+
- "proceed": Return immediately (caller handles 'review' decisions).
|
|
177
|
+
- "hold": Wait for human resolution on 'review' decisions.
|
|
178
|
+
Raises GovernorReviewRejectedError / GovernorReviewExpiredError.
|
|
179
|
+
"""
|
|
180
|
+
return evaluate_action(
|
|
181
|
+
tool, args, context,
|
|
182
|
+
review_mode=review_mode,
|
|
183
|
+
hold_timeout=hold_timeout,
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
# ---------------------------------------------------------------------------
|
|
188
|
+
# Trace observability
|
|
189
|
+
# ---------------------------------------------------------------------------
|
|
190
|
+
|
|
191
|
+
def ingest_spans(spans: List[Dict[str, Any]]) -> Dict[str, Any]:
|
|
192
|
+
"""
|
|
193
|
+
Batch-ingest agent trace spans.
|
|
194
|
+
|
|
195
|
+
Each span dict should contain at minimum:
|
|
196
|
+
trace_id, span_id, kind, name, start_time
|
|
197
|
+
|
|
198
|
+
Optional fields: parent_span_id, status, end_time, duration_ms,
|
|
199
|
+
agent_id, session_id, attributes, input, output, events.
|
|
200
|
+
|
|
201
|
+
Valid span kinds: agent, llm, tool, governance, retrieval, chain, custom.
|
|
202
|
+
|
|
203
|
+
Returns ``{"inserted": N, "skipped": M}`` — duplicates are silently
|
|
204
|
+
skipped (idempotent).
|
|
205
|
+
"""
|
|
206
|
+
payload = {"spans": spans}
|
|
207
|
+
with httpx.Client(timeout=_TIMEOUT, headers=_headers()) as client:
|
|
208
|
+
resp = client.post(f"{GOVERNOR_URL}/traces/ingest", json=payload)
|
|
209
|
+
resp.raise_for_status()
|
|
210
|
+
return resp.json()
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def list_traces(
|
|
214
|
+
*,
|
|
215
|
+
agent_id: Optional[str] = None,
|
|
216
|
+
session_id: Optional[str] = None,
|
|
217
|
+
has_blocks: Optional[bool] = None,
|
|
218
|
+
limit: int = 50,
|
|
219
|
+
offset: int = 0,
|
|
220
|
+
) -> List[Dict[str, Any]]:
|
|
221
|
+
"""
|
|
222
|
+
List traces with optional filters.
|
|
223
|
+
|
|
224
|
+
Returns a list of trace summaries with span_count, governance_count,
|
|
225
|
+
root_span_name, has_errors, has_blocks, etc.
|
|
226
|
+
"""
|
|
227
|
+
params: Dict[str, Any] = {"limit": limit, "offset": offset}
|
|
228
|
+
if agent_id is not None:
|
|
229
|
+
params["agent_id"] = agent_id
|
|
230
|
+
if session_id is not None:
|
|
231
|
+
params["session_id"] = session_id
|
|
232
|
+
if has_blocks is not None:
|
|
233
|
+
params["has_blocks"] = str(has_blocks).lower()
|
|
234
|
+
with httpx.Client(timeout=_TIMEOUT, headers=_headers()) as client:
|
|
235
|
+
resp = client.get(f"{GOVERNOR_URL}/traces", params=params)
|
|
236
|
+
resp.raise_for_status()
|
|
237
|
+
return resp.json()
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def get_trace(trace_id: str) -> Dict[str, Any]:
|
|
241
|
+
"""
|
|
242
|
+
Fetch full trace detail — all spans plus correlated governance decisions.
|
|
243
|
+
|
|
244
|
+
Returns a dict with spans, governance_decisions, span_count,
|
|
245
|
+
governance_count, total_duration_ms, has_errors, has_blocks.
|
|
246
|
+
"""
|
|
247
|
+
with httpx.Client(timeout=_TIMEOUT, headers=_headers()) as client:
|
|
248
|
+
resp = client.get(f"{GOVERNOR_URL}/traces/{trace_id}")
|
|
249
|
+
resp.raise_for_status()
|
|
250
|
+
return resp.json()
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def delete_trace(trace_id: str) -> Dict[str, Any]:
|
|
254
|
+
"""
|
|
255
|
+
Delete all spans for a trace (action log entries are preserved).
|
|
256
|
+
|
|
257
|
+
Returns ``{"trace_id": "…", "spans_deleted": N}``.
|
|
258
|
+
"""
|
|
259
|
+
with httpx.Client(timeout=_TIMEOUT, headers=_headers()) as client:
|
|
260
|
+
resp = client.delete(f"{GOVERNOR_URL}/traces/{trace_id}")
|
|
261
|
+
resp.raise_for_status()
|
|
262
|
+
return resp.json()
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
# ---------------------------------------------------------------------------
|
|
266
|
+
# Post-execution verification
|
|
267
|
+
# ---------------------------------------------------------------------------
|
|
268
|
+
|
|
269
|
+
class GovernorVerificationError(RuntimeError):
|
|
270
|
+
"""Raised when post-execution verification finds a violation."""
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def verify_action(
|
|
274
|
+
action_id: int,
|
|
275
|
+
tool: str,
|
|
276
|
+
result: Dict[str, Any],
|
|
277
|
+
context: Optional[Dict[str, Any]] = None,
|
|
278
|
+
) -> Dict[str, Any]:
|
|
279
|
+
"""
|
|
280
|
+
Submit a tool execution result for post-execution verification.
|
|
281
|
+
|
|
282
|
+
Call this AFTER executing a tool whose evaluate_action response
|
|
283
|
+
had ``verification_required=True``.
|
|
284
|
+
|
|
285
|
+
Parameters:
|
|
286
|
+
action_id: The ``action_id`` from the evaluate response log entry.
|
|
287
|
+
tool: Name of the tool that was executed.
|
|
288
|
+
result: Execution result dict (keys: status, output, diff, error).
|
|
289
|
+
context: Same context as the original evaluate call.
|
|
290
|
+
|
|
291
|
+
Returns the verification verdict dict:
|
|
292
|
+
{ verification, risk_delta, findings, escalated, drift_score }
|
|
293
|
+
|
|
294
|
+
Raises GovernorVerificationError on 'violation' verdicts.
|
|
295
|
+
"""
|
|
296
|
+
payload = {
|
|
297
|
+
"action_id": action_id,
|
|
298
|
+
"tool": tool,
|
|
299
|
+
"result": result,
|
|
300
|
+
"context": context,
|
|
301
|
+
}
|
|
302
|
+
with httpx.Client(timeout=_TIMEOUT, headers=_headers()) as client:
|
|
303
|
+
resp = client.post(f"{GOVERNOR_URL}/actions/verify", json=payload)
|
|
304
|
+
resp.raise_for_status()
|
|
305
|
+
|
|
306
|
+
verdict = resp.json()
|
|
307
|
+
|
|
308
|
+
if verdict.get("verification") == "violation":
|
|
309
|
+
raise GovernorVerificationError(
|
|
310
|
+
f"Verification violation for tool '{tool}' (action_id={action_id}): "
|
|
311
|
+
f"{[f['detail'] for f in verdict.get('findings', []) if f.get('result') == 'fail']}"
|
|
312
|
+
)
|
|
313
|
+
|
|
314
|
+
return verdict
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
# ---------------------------------------------------------------------------
|
|
318
|
+
# Output scanning — scan agent responses before sending to users
|
|
319
|
+
# ---------------------------------------------------------------------------
|
|
320
|
+
|
|
321
|
+
def scan_output(
|
|
322
|
+
text: str,
|
|
323
|
+
agent_id: Optional[str] = None,
|
|
324
|
+
session_id: Optional[str] = None,
|
|
325
|
+
context: Optional[Dict[str, Any]] = None,
|
|
326
|
+
) -> Dict[str, Any]:
|
|
327
|
+
"""
|
|
328
|
+
Scan agent response text for exfiltration URLs, injection, and PII
|
|
329
|
+
before sending to users.
|
|
330
|
+
|
|
331
|
+
Returns a dict with:
|
|
332
|
+
safe: bool — True if no threats found
|
|
333
|
+
risk_score: int — aggregate risk 0–100
|
|
334
|
+
findings: list — individual check results
|
|
335
|
+
sanitized_text: str|None — text with dangerous URLs redacted (if unsafe)
|
|
336
|
+
|
|
337
|
+
Usage:
|
|
338
|
+
result = scan_output(agent_response_text)
|
|
339
|
+
if not result["safe"]:
|
|
340
|
+
# Use sanitized text or block the response
|
|
341
|
+
safe_text = result["sanitized_text"]
|
|
342
|
+
"""
|
|
343
|
+
payload: Dict[str, Any] = {"text": text}
|
|
344
|
+
if agent_id:
|
|
345
|
+
payload["agent_id"] = agent_id
|
|
346
|
+
if session_id:
|
|
347
|
+
payload["session_id"] = session_id
|
|
348
|
+
if context:
|
|
349
|
+
payload["context"] = context
|
|
350
|
+
|
|
351
|
+
with httpx.Client(timeout=_TIMEOUT, headers=_headers()) as client:
|
|
352
|
+
resp = client.post(f"{GOVERNOR_URL}/actions/scan-output", json=payload)
|
|
353
|
+
resp.raise_for_status()
|
|
354
|
+
return resp.json()
|
|
File without changes
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "airg-client"
|
|
7
|
+
version = "0.3.0"
|
|
8
|
+
description = "Python client for the AI Runtime Governor – runtime AI-agent governance service"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Sovereign AI Lab" },
|
|
14
|
+
]
|
|
15
|
+
keywords = ["airg", "ai-runtime-governor", "ai-safety", "agent-governance", "runtime-policy"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 3 - Alpha",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"License :: OSI Approved :: MIT License",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.9",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
27
|
+
"Topic :: Security",
|
|
28
|
+
"Typing :: Typed",
|
|
29
|
+
]
|
|
30
|
+
dependencies = [
|
|
31
|
+
"httpx>=0.24.0",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.urls]
|
|
35
|
+
Homepage = "https://github.com/othnielObasi/ai-runtime-governor-core"
|
|
36
|
+
Repository = "https://github.com/othnielObasi/ai-runtime-governor-core"
|
|
37
|
+
Documentation = "https://github.com/othnielObasi/ai-runtime-governor-core#readme"
|
|
38
|
+
Issues = "https://github.com/othnielObasi/ai-runtime-governor-core/issues"
|
|
39
|
+
|
|
40
|
+
[tool.hatch.build.targets.sdist]
|
|
41
|
+
include = [
|
|
42
|
+
"governor_client.py",
|
|
43
|
+
"README.md",
|
|
44
|
+
"LICENSE",
|
|
45
|
+
"py.typed",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
[tool.hatch.build.targets.wheel]
|
|
49
|
+
packages = ["."]
|
|
50
|
+
only-include = [
|
|
51
|
+
"governor_client.py",
|
|
52
|
+
"py.typed",
|
|
53
|
+
]
|