haltstate-sdk 0.8.0.dev0__tar.gz → 0.8.0.dev1__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.
- {haltstate_sdk-0.8.0.dev0/haltstate_sdk.egg-info → haltstate_sdk-0.8.0.dev1}/PKG-INFO +1 -1
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/haltstate/__init__.py +13 -2
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1/haltstate_sdk.egg-info}/PKG-INFO +1 -1
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/haltstate_sdk.egg-info/SOURCES.txt +2 -0
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/janus/__init__.py +13 -2
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/janus/async_client.py +259 -11
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/janus/client.py +317 -10
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/janus/models.py +39 -0
- haltstate_sdk-0.8.0.dev1/janus/recording.py +181 -0
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/pyproject.toml +1 -1
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/tests/test_client.py +515 -0
- haltstate_sdk-0.8.0.dev1/tests/test_governed_message_capture.py +160 -0
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/LICENSE +0 -0
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/README.md +0 -0
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/haltstate/decorators.py +0 -0
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/haltstate_sdk.egg-info/dependency_links.txt +0 -0
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/haltstate_sdk.egg-info/requires.txt +0 -0
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/haltstate_sdk.egg-info/top_level.txt +0 -0
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/janus/decorators.py +0 -0
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/janus/exceptions.py +0 -0
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/janus/semiotic_probe.py +0 -0
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/setup.cfg +0 -0
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/tests/test_import.py +0 -0
- {haltstate_sdk-0.8.0.dev0 → haltstate_sdk-0.8.0.dev1}/tests/test_semiotic_probe.py +0 -0
|
@@ -5,7 +5,15 @@
|
|
|
5
5
|
from janus.client import JanusClient as HaltStateClient, Permit
|
|
6
6
|
from janus.semiotic_probe import SemioticProbeConnection, SemioticProbeError
|
|
7
7
|
from janus.async_client import AsyncJanusClient as AsyncHaltStateClient
|
|
8
|
-
from janus.models import
|
|
8
|
+
from janus.models import (
|
|
9
|
+
AttachmentReceipt,
|
|
10
|
+
CheckResult,
|
|
11
|
+
Decision,
|
|
12
|
+
ApprovalDecision,
|
|
13
|
+
ApprovalStatus,
|
|
14
|
+
EvidenceReference,
|
|
15
|
+
ObservationReceipt,
|
|
16
|
+
)
|
|
9
17
|
from janus.decorators import (
|
|
10
18
|
janus_guard,
|
|
11
19
|
janus_guard as haltstate_guard,
|
|
@@ -63,6 +71,9 @@ __all__ = [
|
|
|
63
71
|
"JanusApprovalTimeoutError",
|
|
64
72
|
# Shared models
|
|
65
73
|
"CheckResult",
|
|
74
|
+
"AttachmentReceipt",
|
|
75
|
+
"EvidenceReference",
|
|
76
|
+
"ObservationReceipt",
|
|
66
77
|
"Decision",
|
|
67
78
|
"ApprovalDecision",
|
|
68
79
|
"ApprovalStatus",
|
|
@@ -77,4 +88,4 @@ __all__ = [
|
|
|
77
88
|
"KillSwitchTriggered",
|
|
78
89
|
]
|
|
79
90
|
|
|
80
|
-
__version__ = "0.8.0.
|
|
91
|
+
__version__ = "0.8.0.dev1"
|
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
from .client import JanusClient, Permit
|
|
2
2
|
from .async_client import AsyncJanusClient
|
|
3
3
|
from .semiotic_probe import SemioticProbeConnection, SemioticProbeError
|
|
4
|
-
from .models import
|
|
4
|
+
from .models import (
|
|
5
|
+
AttachmentReceipt,
|
|
6
|
+
CheckResult,
|
|
7
|
+
Decision,
|
|
8
|
+
ApprovalDecision,
|
|
9
|
+
ApprovalStatus,
|
|
10
|
+
EvidenceReference,
|
|
11
|
+
ObservationReceipt,
|
|
12
|
+
)
|
|
5
13
|
from .decorators import janus_guard, ajanus_guard, openai_guard
|
|
6
14
|
from .exceptions import (
|
|
7
15
|
JanusError,
|
|
@@ -25,6 +33,9 @@ __all__ = [
|
|
|
25
33
|
"SemioticProbeError",
|
|
26
34
|
# Models
|
|
27
35
|
"CheckResult",
|
|
36
|
+
"AttachmentReceipt",
|
|
37
|
+
"EvidenceReference",
|
|
38
|
+
"ObservationReceipt",
|
|
28
39
|
"Decision",
|
|
29
40
|
"ApprovalDecision",
|
|
30
41
|
"ApprovalStatus",
|
|
@@ -48,4 +59,4 @@ __all__ = [
|
|
|
48
59
|
"KillSwitchTriggered",
|
|
49
60
|
]
|
|
50
61
|
|
|
51
|
-
__version__ = "0.8.0.
|
|
62
|
+
__version__ = "0.8.0.dev1" # Recording repair release candidate; not yet published
|
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
import uuid
|
|
2
|
+
import base64
|
|
2
3
|
import json
|
|
3
4
|
from datetime import datetime
|
|
4
5
|
from typing import Dict, Any, Optional, List, AsyncIterator, Callable
|
|
5
6
|
|
|
6
7
|
import httpx
|
|
7
8
|
|
|
8
|
-
from .models import
|
|
9
|
+
from .models import (
|
|
10
|
+
AttachmentReceipt,
|
|
11
|
+
CheckResult,
|
|
12
|
+
Decision,
|
|
13
|
+
ApprovalDecision,
|
|
14
|
+
ApprovalStatus,
|
|
15
|
+
EvidenceReference,
|
|
16
|
+
ObservationReceipt,
|
|
17
|
+
)
|
|
18
|
+
from .recording import (
|
|
19
|
+
MAX_CONTENT_BYTES,
|
|
20
|
+
observation_error,
|
|
21
|
+
observation_payload,
|
|
22
|
+
parse_observation_receipt,
|
|
23
|
+
selected_business_content,
|
|
24
|
+
stable_attachment_id,
|
|
25
|
+
validate_observation_metadata,
|
|
26
|
+
)
|
|
9
27
|
from .exceptions import JanusError, JanusConnectionError, JanusAuthError, JanusRateLimitError, JanusApprovalTimeoutError
|
|
10
28
|
|
|
11
29
|
|
|
@@ -33,6 +51,7 @@ class AsyncJanusClient:
|
|
|
33
51
|
self.fail_open = fail_open
|
|
34
52
|
self.retry_count = retry_count
|
|
35
53
|
self._resolved_route_paths: Dict[str, str] = {}
|
|
54
|
+
self._capture_attempts: Dict[tuple[str, str], AttachmentReceipt] = {}
|
|
36
55
|
|
|
37
56
|
self._client = httpx.AsyncClient(
|
|
38
57
|
timeout=self.timeout,
|
|
@@ -90,15 +109,16 @@ class AsyncJanusClient:
|
|
|
90
109
|
return last_response
|
|
91
110
|
raise JanusError(f"No route candidates available for {path}")
|
|
92
111
|
|
|
93
|
-
async def check(self, action: str, params: Dict[str, Any], agent_id: str = "default") -> CheckResult:
|
|
112
|
+
async def check(self, action: str, params: Dict[str, Any], agent_id: str = "default", evidence_context: Optional[Dict[str, str]] = None) -> CheckResult:
|
|
94
113
|
request_id = str(uuid.uuid4())
|
|
114
|
+
context = {"correlation_id": request_id, **(evidence_context or {})}
|
|
95
115
|
|
|
96
116
|
for attempt in range(self.retry_count + 1):
|
|
97
117
|
try:
|
|
98
118
|
response = await self._request_with_route_fallback(
|
|
99
119
|
"POST",
|
|
100
120
|
"/api/sentinel/action/check",
|
|
101
|
-
json={"agent_id": agent_id, "action": action, "params": params},
|
|
121
|
+
json={"agent_id": agent_id, "action": action, "params": params, "evidence_context": context},
|
|
102
122
|
)
|
|
103
123
|
|
|
104
124
|
if response.status_code == 401:
|
|
@@ -114,14 +134,26 @@ class AsyncJanusClient:
|
|
|
114
134
|
decision = Decision(data["decision"])
|
|
115
135
|
approval_id = data.get("approval_id") or data.get("request_id")
|
|
116
136
|
|
|
117
|
-
|
|
137
|
+
result = CheckResult(
|
|
118
138
|
decision=decision,
|
|
119
139
|
reason=data.get("reason", ""),
|
|
120
140
|
policy_id=data.get("policy_id"),
|
|
121
141
|
latency_ms=data.get("latency_ms", 0),
|
|
122
142
|
request_id=request_id,
|
|
123
143
|
approval_id=approval_id if decision == Decision.APPROVAL_REQUIRED else None,
|
|
144
|
+
evidence_context=context, action=action, agent_id=agent_id,
|
|
145
|
+
evidence_id=data.get("evidence_id"),
|
|
146
|
+
activity_id=data.get("activity_id"),
|
|
124
147
|
)
|
|
148
|
+
capture = await self._capture_supported_fields(
|
|
149
|
+
result,
|
|
150
|
+
params,
|
|
151
|
+
"input",
|
|
152
|
+
)
|
|
153
|
+
if capture is not None:
|
|
154
|
+
result.input_capture = capture
|
|
155
|
+
result.capture_state = capture.state
|
|
156
|
+
return result
|
|
125
157
|
|
|
126
158
|
except httpx.RequestError as exc:
|
|
127
159
|
if attempt < self.retry_count:
|
|
@@ -133,6 +165,9 @@ class AsyncJanusClient:
|
|
|
133
165
|
policy_id=None,
|
|
134
166
|
latency_ms=0,
|
|
135
167
|
request_id=request_id,
|
|
168
|
+
evidence_context=context,
|
|
169
|
+
action=action,
|
|
170
|
+
agent_id=agent_id,
|
|
136
171
|
)
|
|
137
172
|
raise JanusConnectionError(f"Connection failed: {exc}")
|
|
138
173
|
|
|
@@ -144,21 +179,234 @@ class AsyncJanusClient:
|
|
|
144
179
|
status: str,
|
|
145
180
|
result: Optional[Dict[str, Any]] = None,
|
|
146
181
|
agent_id: str = "default",
|
|
147
|
-
action: Optional[str] = None,
|
|
148
|
-
) ->
|
|
182
|
+
action: Optional[str] = None, evidence_context: Optional[Dict[str, str]] = None,
|
|
183
|
+
) -> Optional[AttachmentReceipt]:
|
|
149
184
|
payload = {
|
|
150
185
|
"request_id": check_result.request_id,
|
|
151
|
-
"agent_id": agent_id,
|
|
152
|
-
"action": action or "",
|
|
186
|
+
"agent_id": agent_id if agent_id != "default" else (check_result.agent_id or agent_id),
|
|
187
|
+
"action": action or check_result.action or "",
|
|
153
188
|
"status": status,
|
|
154
189
|
"decision": check_result.decision.value,
|
|
155
190
|
"policy_id": check_result.policy_id,
|
|
156
|
-
"result": result,
|
|
191
|
+
"result": result, "evidence_context": evidence_context or check_result.evidence_context,
|
|
157
192
|
}
|
|
158
193
|
try:
|
|
159
|
-
await self._request_with_route_fallback(
|
|
194
|
+
response = await self._request_with_route_fallback(
|
|
195
|
+
"POST",
|
|
196
|
+
"/api/sentinel/action/report",
|
|
197
|
+
json=payload,
|
|
198
|
+
)
|
|
199
|
+
if response.status_code >= 400:
|
|
200
|
+
return None
|
|
201
|
+
except Exception:
|
|
202
|
+
return None
|
|
203
|
+
capture_reference: Any = check_result
|
|
204
|
+
try:
|
|
205
|
+
response_data = response.json()
|
|
160
206
|
except Exception:
|
|
161
|
-
|
|
207
|
+
response_data = {}
|
|
208
|
+
if isinstance(response_data, dict) and isinstance(
|
|
209
|
+
response_data.get("evidence_id"),
|
|
210
|
+
str,
|
|
211
|
+
):
|
|
212
|
+
capture_reference = EvidenceReference(
|
|
213
|
+
evidence_id=response_data["evidence_id"],
|
|
214
|
+
agent_id=check_result.agent_id or payload["agent_id"],
|
|
215
|
+
action=check_result.action or payload["action"],
|
|
216
|
+
activity_id=(
|
|
217
|
+
response_data.get("activity_id") or check_result.activity_id
|
|
218
|
+
),
|
|
219
|
+
)
|
|
220
|
+
capture = await self._capture_supported_fields(
|
|
221
|
+
capture_reference,
|
|
222
|
+
result,
|
|
223
|
+
"output",
|
|
224
|
+
)
|
|
225
|
+
if capture is not None:
|
|
226
|
+
check_result.output_capture = capture
|
|
227
|
+
check_result.capture_state = capture.state
|
|
228
|
+
return capture
|
|
229
|
+
|
|
230
|
+
async def record_observation(
|
|
231
|
+
self,
|
|
232
|
+
*,
|
|
233
|
+
agent_id: str,
|
|
234
|
+
event_id: str,
|
|
235
|
+
action: str,
|
|
236
|
+
direction: str,
|
|
237
|
+
occurred_at: Optional[str] = None,
|
|
238
|
+
evidence_context: Optional[Dict[str, str]] = None,
|
|
239
|
+
external_references: Optional[List[Dict[str, str]]] = None,
|
|
240
|
+
content: Optional[bytes] = None,
|
|
241
|
+
) -> ObservationReceipt:
|
|
242
|
+
"""Record a non-governance business event with stable retry identity."""
|
|
243
|
+
if not all(
|
|
244
|
+
isinstance(value, str) and bool(value.strip())
|
|
245
|
+
for value in (agent_id, event_id, action)
|
|
246
|
+
):
|
|
247
|
+
return observation_error("agent_id, event_id and action are required")
|
|
248
|
+
if direction not in {"input", "output", "event"}:
|
|
249
|
+
return observation_error("direction must be input, output or event")
|
|
250
|
+
if content is not None and not isinstance(content, bytes):
|
|
251
|
+
return observation_error("content must be bytes")
|
|
252
|
+
if content is not None and len(content) > MAX_CONTENT_BYTES:
|
|
253
|
+
return observation_error("content exceeds the 256 KiB limit")
|
|
254
|
+
metadata_error = validate_observation_metadata(
|
|
255
|
+
evidence_context,
|
|
256
|
+
external_references,
|
|
257
|
+
)
|
|
258
|
+
if metadata_error:
|
|
259
|
+
return observation_error(metadata_error)
|
|
260
|
+
payload = observation_payload(
|
|
261
|
+
agent_id=agent_id,
|
|
262
|
+
event_id=event_id,
|
|
263
|
+
action=action,
|
|
264
|
+
direction=direction,
|
|
265
|
+
occurred_at=occurred_at,
|
|
266
|
+
evidence_context=evidence_context,
|
|
267
|
+
external_references=external_references,
|
|
268
|
+
content=content,
|
|
269
|
+
)
|
|
270
|
+
last_error = "observation capture failed"
|
|
271
|
+
for attempt in range(self.retry_count + 1):
|
|
272
|
+
try:
|
|
273
|
+
response = await self._client.request(
|
|
274
|
+
"POST",
|
|
275
|
+
f"{self.base_url}/api/haltstate/evidence/observations",
|
|
276
|
+
json=dict(payload),
|
|
277
|
+
)
|
|
278
|
+
except httpx.RequestError as exc:
|
|
279
|
+
last_error = f"observation transport failed ({type(exc).__name__})"
|
|
280
|
+
if attempt < self.retry_count:
|
|
281
|
+
continue
|
|
282
|
+
break
|
|
283
|
+
except Exception as exc:
|
|
284
|
+
last_error = f"observation request failed ({type(exc).__name__})"
|
|
285
|
+
break
|
|
286
|
+
if response.status_code == 429 or response.status_code >= 500:
|
|
287
|
+
last_error = f"observation capture failed with HTTP {response.status_code}"
|
|
288
|
+
if attempt < self.retry_count:
|
|
289
|
+
continue
|
|
290
|
+
break
|
|
291
|
+
if response.status_code >= 400:
|
|
292
|
+
return observation_error(
|
|
293
|
+
f"observation capture was rejected with HTTP {response.status_code}"
|
|
294
|
+
)
|
|
295
|
+
try:
|
|
296
|
+
return parse_observation_receipt(response.json())
|
|
297
|
+
except Exception:
|
|
298
|
+
return observation_error("observation response was invalid")
|
|
299
|
+
return observation_error(last_error)
|
|
300
|
+
|
|
301
|
+
async def attach_evidence_reference(
|
|
302
|
+
self,
|
|
303
|
+
reference: EvidenceReference,
|
|
304
|
+
attachment_id: str,
|
|
305
|
+
content: bytes,
|
|
306
|
+
content_kind: str = "input",
|
|
307
|
+
) -> AttachmentReceipt:
|
|
308
|
+
if not reference.evidence_id or not reference.agent_id:
|
|
309
|
+
return AttachmentReceipt(
|
|
310
|
+
state="error",
|
|
311
|
+
error="evidence reference lacks persisted context",
|
|
312
|
+
)
|
|
313
|
+
if content_kind not in {"input", "output"}:
|
|
314
|
+
return AttachmentReceipt(state="error", error="invalid content kind")
|
|
315
|
+
if not isinstance(content, bytes):
|
|
316
|
+
return AttachmentReceipt(state="error", error="content must be bytes")
|
|
317
|
+
if len(content) > MAX_CONTENT_BYTES:
|
|
318
|
+
return AttachmentReceipt(
|
|
319
|
+
state="error",
|
|
320
|
+
error="content exceeds the 256 KiB limit",
|
|
321
|
+
)
|
|
322
|
+
try:
|
|
323
|
+
response = await self._client.post(
|
|
324
|
+
f"{self.base_url}/api/haltstate/evidence/attachments",
|
|
325
|
+
json={
|
|
326
|
+
"attachment_id": attachment_id,
|
|
327
|
+
"evidence_id": reference.evidence_id,
|
|
328
|
+
"agent_id": reference.agent_id,
|
|
329
|
+
"content_kind": content_kind,
|
|
330
|
+
"content_base64": base64.b64encode(content).decode("ascii"),
|
|
331
|
+
},
|
|
332
|
+
)
|
|
333
|
+
data = (
|
|
334
|
+
response.json()
|
|
335
|
+
if response.headers.get("content-type", "").startswith(
|
|
336
|
+
"application/json"
|
|
337
|
+
)
|
|
338
|
+
else {}
|
|
339
|
+
)
|
|
340
|
+
if not response.is_success:
|
|
341
|
+
return AttachmentReceipt(
|
|
342
|
+
state="error",
|
|
343
|
+
error=str(data.get("detail") or "attachment capture failed"),
|
|
344
|
+
)
|
|
345
|
+
receipt = AttachmentReceipt(
|
|
346
|
+
**{
|
|
347
|
+
key: data.get(key)
|
|
348
|
+
for key in AttachmentReceipt.__dataclass_fields__
|
|
349
|
+
}
|
|
350
|
+
)
|
|
351
|
+
if not receipt.state:
|
|
352
|
+
return AttachmentReceipt(
|
|
353
|
+
state="error",
|
|
354
|
+
error="attachment response lacked capture state",
|
|
355
|
+
)
|
|
356
|
+
return receipt
|
|
357
|
+
except Exception as exc:
|
|
358
|
+
return AttachmentReceipt(state="error", error=str(exc))
|
|
359
|
+
|
|
360
|
+
async def _capture_supported_fields(
|
|
361
|
+
self,
|
|
362
|
+
reference: Any,
|
|
363
|
+
values: Optional[Dict[str, Any]],
|
|
364
|
+
direction: str,
|
|
365
|
+
) -> Optional[AttachmentReceipt]:
|
|
366
|
+
content = selected_business_content(values, direction)
|
|
367
|
+
evidence_id = getattr(reference, "evidence_id", None)
|
|
368
|
+
agent_id = getattr(reference, "agent_id", None)
|
|
369
|
+
action = getattr(reference, "action", None)
|
|
370
|
+
if content is None or not evidence_id or not agent_id or not action:
|
|
371
|
+
return None
|
|
372
|
+
receipt = await self.attach_evidence_reference(
|
|
373
|
+
EvidenceReference(
|
|
374
|
+
evidence_id=evidence_id,
|
|
375
|
+
agent_id=agent_id,
|
|
376
|
+
action=action,
|
|
377
|
+
activity_id=getattr(reference, "activity_id", None),
|
|
378
|
+
),
|
|
379
|
+
stable_attachment_id(agent_id, evidence_id, direction),
|
|
380
|
+
content,
|
|
381
|
+
direction,
|
|
382
|
+
)
|
|
383
|
+
key = (evidence_id, direction)
|
|
384
|
+
if len(self._capture_attempts) >= 1024 and key not in self._capture_attempts:
|
|
385
|
+
self._capture_attempts.pop(next(iter(self._capture_attempts)))
|
|
386
|
+
self._capture_attempts[key] = receipt
|
|
387
|
+
return receipt
|
|
388
|
+
|
|
389
|
+
def capture_receipt_for_reference(
|
|
390
|
+
self,
|
|
391
|
+
evidence_id: str,
|
|
392
|
+
direction: str,
|
|
393
|
+
) -> Optional[AttachmentReceipt]:
|
|
394
|
+
return self._capture_attempts.get((evidence_id, direction))
|
|
395
|
+
|
|
396
|
+
async def attach_evidence(self, check_result: CheckResult, attachment_id: str, content: bytes, content_kind: str = "input") -> AttachmentReceipt:
|
|
397
|
+
if not check_result.action or not check_result.agent_id or not check_result.evidence_id:
|
|
398
|
+
return AttachmentReceipt(state="error", error="check result lacks evidence action context")
|
|
399
|
+
return await self.attach_evidence_reference(
|
|
400
|
+
EvidenceReference(
|
|
401
|
+
evidence_id=check_result.evidence_id,
|
|
402
|
+
agent_id=check_result.agent_id,
|
|
403
|
+
action=check_result.action,
|
|
404
|
+
activity_id=check_result.activity_id,
|
|
405
|
+
),
|
|
406
|
+
attachment_id,
|
|
407
|
+
content,
|
|
408
|
+
content_kind,
|
|
409
|
+
)
|
|
162
410
|
|
|
163
411
|
async def close(self) -> None:
|
|
164
412
|
await self._client.aclose()
|