|
| 1 | +"""Strict adapters for Agent Mesh audit entries shipped by AGT core.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import uuid |
| 6 | +from datetime import datetime, timezone |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from .base import EventFactory |
| 10 | + |
| 11 | + |
| 12 | +_AUDIT_NAMESPACE = uuid.UUID("398428fd-2730-498f-8ed8-6b4290668171") |
| 13 | +_POLICY_EVENTS = {"policy_evaluation", "policy_violation"} |
| 14 | +_ACTION_EVENTS = {"tool_invocation", "tool_blocked", "action"} |
| 15 | + |
| 16 | + |
| 17 | +def agt_audit_policy_decision( |
| 18 | + factory: EventFactory, |
| 19 | + source: Any, |
| 20 | + *, |
| 21 | + run_id: str, |
| 22 | + policy_engine_version: str, |
| 23 | + bundle_digest: dict[str, str], |
| 24 | + resource_type: str, |
| 25 | + evaluation_duration_ns: int = 0, |
| 26 | + enforcement_mode: str = "enforce", |
| 27 | +) -> dict[str, Any]: |
| 28 | + """Map a policy-oriented Agent Mesh AuditEntry without copying its data.""" |
| 29 | + event_type = _required_string(_field(source, "event_type"), "event_type") |
| 30 | + if event_type not in _POLICY_EVENTS: |
| 31 | + raise ValueError(f"AGT audit event is not a policy event: {event_type!r}") |
| 32 | + decision = _policy_decision(_field(source, "policy_decision")) |
| 33 | + entry_id = _required_string(_field(source, "entry_id"), "entry_id") |
| 34 | + policy: dict[str, Any] = { |
| 35 | + "engine": "agt", |
| 36 | + "engine_version": _required_string(policy_engine_version, "policy_engine_version"), |
| 37 | + "bundle_digest": bundle_digest, |
| 38 | + } |
| 39 | + matched_rule = _field(source, "matched_rule") |
| 40 | + if matched_rule is not None: |
| 41 | + policy["policy_id"] = _required_string(matched_rule, "matched_rule") |
| 42 | + return factory.build( |
| 43 | + "policy.decision", |
| 44 | + run_id=run_id, |
| 45 | + agent_id=_required_string(_field(source, "agent_did"), "agent_did"), |
| 46 | + event_id=_source_event_id("policy", entry_id), |
| 47 | + time_unix_nano=_datetime_ns(_field(source, "timestamp"), "timestamp"), |
| 48 | + trace_id=_optional_string(_field(source, "trace_id"), "trace_id"), |
| 49 | + decision=decision, |
| 50 | + policy=policy, |
| 51 | + action_type=_required_string(_field(source, "action"), "action"), |
| 52 | + resource_type=_required_string(resource_type, "resource_type"), |
| 53 | + enforcement_mode=enforcement_mode, |
| 54 | + evaluation_duration_ns=evaluation_duration_ns, |
| 55 | + reason_codes=[f"agt.audit:{event_type}"], |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def agt_audit_action( |
| 60 | + factory: EventFactory, |
| 61 | + source: Any, |
| 62 | + *, |
| 63 | + run_id: str, |
| 64 | + action_digest: dict[str, str], |
| 65 | + action_kind: str, |
| 66 | + operation: str, |
| 67 | + duration_ns: int | None = None, |
| 68 | +) -> dict[str, Any]: |
| 69 | + """Map an action audit row using a caller-computed full action digest.""" |
| 70 | + event_type = _required_string(_field(source, "event_type"), "event_type") |
| 71 | + if event_type not in _ACTION_EVENTS: |
| 72 | + raise ValueError(f"AGT audit event is not an action event: {event_type!r}") |
| 73 | + entry_id = _required_string(_field(source, "entry_id"), "entry_id") |
| 74 | + outcome = "denied" if event_type == "tool_blocked" else _action_outcome( |
| 75 | + _field(source, "outcome") |
| 76 | + ) |
| 77 | + resolved_duration = duration_ns |
| 78 | + if resolved_duration is None: |
| 79 | + resolved_duration = _audit_duration(source) |
| 80 | + target: dict[str, str] | None = None |
| 81 | + target_did = _field(source, "target_did") |
| 82 | + if target_did is not None: |
| 83 | + target = {"kind": "agent", "id": _required_string(target_did, "target_did")} |
| 84 | + return factory.build( |
| 85 | + "action.executed", |
| 86 | + run_id=run_id, |
| 87 | + agent_id=_required_string(_field(source, "agent_did"), "agent_did"), |
| 88 | + event_id=_source_event_id("action", entry_id), |
| 89 | + time_unix_nano=_datetime_ns(_field(source, "timestamp"), "timestamp"), |
| 90 | + trace_id=_optional_string(_field(source, "trace_id"), "trace_id"), |
| 91 | + action_id=entry_id, |
| 92 | + action_kind=action_kind, |
| 93 | + action_name=_required_string(_field(source, "action"), "action"), |
| 94 | + operation=_required_string(operation, "operation"), |
| 95 | + outcome=outcome, |
| 96 | + duration_ns=resolved_duration, |
| 97 | + action_digest=action_digest, |
| 98 | + **({"target": target} if target else {}), |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +def _policy_decision(value: Any) -> str: |
| 103 | + mapping = { |
| 104 | + "allow": "allow", |
| 105 | + "allowed": "allow", |
| 106 | + "deny": "deny", |
| 107 | + "denied": "deny", |
| 108 | + "require_approval": "challenge", |
| 109 | + "requires_approval": "challenge", |
| 110 | + "review": "challenge", |
| 111 | + "not_applicable": "not_applicable", |
| 112 | + "error": "error", |
| 113 | + } |
| 114 | + if value not in mapping: |
| 115 | + raise ValueError(f"unsupported AGT audit policy decision: {value!r}") |
| 116 | + return mapping[value] |
| 117 | + |
| 118 | + |
| 119 | +def _action_outcome(value: Any) -> str: |
| 120 | + mapping = { |
| 121 | + "success": "success", |
| 122 | + "failure": "error", |
| 123 | + "error": "error", |
| 124 | + "denied": "denied", |
| 125 | + "cancelled": "cancelled", |
| 126 | + "timeout": "timeout", |
| 127 | + } |
| 128 | + if value not in mapping: |
| 129 | + raise ValueError(f"unsupported AGT audit action outcome: {value!r}") |
| 130 | + return mapping[value] |
| 131 | + |
| 132 | + |
| 133 | +def _audit_duration(source: Any) -> int: |
| 134 | + issued = _field(source, "issued_at") |
| 135 | + completed = _field(source, "completed_at") |
| 136 | + if issued is None or completed is None: |
| 137 | + raise ValueError( |
| 138 | + "AGT action audit requires duration_ns or both issued_at and completed_at" |
| 139 | + ) |
| 140 | + issued_ns = _datetime_ns(issued, "issued_at") |
| 141 | + completed_ns = _datetime_ns(completed, "completed_at") |
| 142 | + if completed_ns < issued_ns: |
| 143 | + raise ValueError("AGT completed_at cannot predate issued_at") |
| 144 | + return completed_ns - issued_ns |
| 145 | + |
| 146 | + |
| 147 | +def _source_event_id(kind: str, source_id: str) -> str: |
| 148 | + return str(uuid.uuid5(_AUDIT_NAMESPACE, f"{kind}:{source_id}")) |
| 149 | + |
| 150 | + |
| 151 | +def _field(source: Any, name: str) -> Any: |
| 152 | + if isinstance(source, dict): |
| 153 | + return source.get(name) |
| 154 | + return getattr(source, name, None) |
| 155 | + |
| 156 | + |
| 157 | +def _required_string(value: Any, field: str) -> str: |
| 158 | + if not isinstance(value, str) or not value: |
| 159 | + raise ValueError(f"AGT audit {field} must be a non-empty string") |
| 160 | + return value |
| 161 | + |
| 162 | + |
| 163 | +def _optional_string(value: Any, field: str) -> str | None: |
| 164 | + return None if value is None else _required_string(value, field) |
| 165 | + |
| 166 | + |
| 167 | +def _datetime_ns(value: Any, field: str) -> int: |
| 168 | + if not isinstance(value, datetime) or value.tzinfo is None: |
| 169 | + raise ValueError(f"AGT audit {field} must be a timezone-aware datetime") |
| 170 | + utc = value.astimezone(timezone.utc) |
| 171 | + epoch = datetime(1970, 1, 1, tzinfo=timezone.utc) |
| 172 | + delta = utc - epoch |
| 173 | + return ( |
| 174 | + delta.days * 86_400_000_000_000 |
| 175 | + + delta.seconds * 1_000_000_000 |
| 176 | + + delta.microseconds * 1_000 |
| 177 | + ) |
0 commit comments