-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforge_audit.py
More file actions
224 lines (177 loc) · 7.41 KB
/
forge_audit.py
File metadata and controls
224 lines (177 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""Nova Forge Audit Trail — JSONL query and diff tracking.
Provides query interface for the audit log written by the track-autonomy
hook. Supports filtering by time, project, agent, tool, and risk level.
Usage::
from forge_audit import AuditQuery
aq = AuditQuery(project_root=Path("./myapp"))
entries = aq.query(since="1h", tool="Write")
stats = aq.stats()
"""
from __future__ import annotations
import json
import logging
from dataclasses import dataclass, field
from datetime import datetime, timezone, timedelta
from pathlib import Path
from typing import Any, Optional
from config import FORGE_DIR_NAME
logger = logging.getLogger(__name__)
# ── Data structures ──────────────────────────────────────────────────────────
@dataclass
class AuditEntry:
"""A single audit log entry."""
timestamp: str
project: str = ""
tool: str = ""
file: str = ""
risk: str = ""
outcome: str = ""
autonomy_level: int = 0
agent_id: str = ""
task_id: str = ""
diff_summary: str = ""
@classmethod
def from_dict(cls, data: dict) -> AuditEntry:
return cls(
timestamp=data.get("timestamp", ""),
project=data.get("project", ""),
tool=data.get("tool", ""),
file=data.get("file", ""),
risk=data.get("risk", ""),
outcome=data.get("outcome", ""),
autonomy_level=data.get("autonomy_level", 0),
agent_id=data.get("agent_id", ""),
task_id=data.get("task_id", ""),
diff_summary=data.get("diff_summary", ""),
)
@dataclass
class AuditStats:
"""Aggregated audit statistics."""
total_entries: int = 0
success_count: int = 0
error_count: int = 0
tools_used: dict[str, int] = field(default_factory=dict)
risk_distribution: dict[str, int] = field(default_factory=dict)
agents_active: set[str] = field(default_factory=set)
time_range: tuple[str, str] = ("", "")
# ── AuditQuery ───────────────────────────────────────────────────────────────
class AuditQuery:
"""Query and analyze the audit trail for a project."""
def __init__(self, project_root: Path | str) -> None:
self.project_root = Path(project_root).resolve()
self.audit_file = self.project_root / FORGE_DIR_NAME / "audit" / "audit.jsonl"
self.agent_usage_file = self.project_root / FORGE_DIR_NAME / "audit" / "agent-usage.jsonl"
self.session_log_file = self.project_root / FORGE_DIR_NAME / "audit" / "session-log.jsonl"
def query(
self,
since: str | None = None,
tool: str | None = None,
risk: str | None = None,
outcome: str | None = None,
limit: int = 100,
) -> list[AuditEntry]:
"""Query audit entries with optional filters.
Args:
since: Time filter (e.g. "1h", "30m", "2d")
tool: Filter by tool name
risk: Filter by risk level
outcome: Filter by outcome (success/error)
limit: Max results
"""
entries = self._load_entries(self.audit_file)
# Time filter
if since:
cutoff = self._parse_since(since)
entries = [e for e in entries if self._after(e.timestamp, cutoff)]
# Tool filter
if tool:
entries = [e for e in entries if e.tool.lower() == tool.lower()]
# Risk filter
if risk:
entries = [e for e in entries if e.risk.lower() == risk.lower()]
# Outcome filter
if outcome:
entries = [e for e in entries if e.outcome.lower() == outcome.lower()]
return entries[-limit:]
def stats(self) -> AuditStats:
"""Generate aggregated statistics from the audit trail."""
entries = self._load_entries(self.audit_file)
if not entries:
return AuditStats()
stats = AuditStats(total_entries=len(entries))
for e in entries:
if e.outcome == "success":
stats.success_count += 1
elif e.outcome == "error":
stats.error_count += 1
stats.tools_used[e.tool] = stats.tools_used.get(e.tool, 0) + 1
stats.risk_distribution[e.risk] = stats.risk_distribution.get(e.risk, 0) + 1
if e.agent_id:
stats.agents_active.add(e.agent_id)
if entries:
stats.time_range = (entries[0].timestamp, entries[-1].timestamp)
return stats
def agent_usage(self, limit: int = 50) -> list[dict]:
"""Query agent usage log."""
if not self.agent_usage_file.exists():
return []
entries = []
try:
for line in self.agent_usage_file.read_text().strip().split("\n"):
if line.strip():
entries.append(json.loads(line))
except (json.JSONDecodeError, OSError):
pass
return entries[-limit:]
def session_log(self, limit: int = 20) -> list[dict]:
"""Query session end log."""
if not self.session_log_file.exists():
return []
entries = []
try:
for line in self.session_log_file.read_text().strip().split("\n"):
if line.strip():
entries.append(json.loads(line))
except (json.JSONDecodeError, OSError):
pass
return entries[-limit:]
def recent(self, n: int = 10) -> list[AuditEntry]:
"""Get the N most recent audit entries."""
entries = self._load_entries(self.audit_file)
return entries[-n:]
# ── Internal ─────────────────────────────────────────────────────────────
def _load_entries(self, path: Path) -> list[AuditEntry]:
"""Load all entries from a JSONL file."""
if not path.exists():
return []
entries: list[AuditEntry] = []
try:
for line in path.read_text().strip().split("\n"):
if line.strip():
entries.append(AuditEntry.from_dict(json.loads(line)))
except (json.JSONDecodeError, OSError) as exc:
logger.warning("Failed to load audit entries from %s: %s", path, exc)
return entries
@staticmethod
def _parse_since(since: str) -> datetime:
"""Parse a relative time string to a datetime cutoff."""
now = datetime.now(timezone.utc)
value = int("".join(c for c in since if c.isdigit()) or "1")
unit = since[-1].lower() if since else "h"
if unit == "m":
return now - timedelta(minutes=value)
elif unit == "h":
return now - timedelta(hours=value)
elif unit == "d":
return now - timedelta(days=value)
return now - timedelta(hours=1)
@staticmethod
def _after(timestamp: str, cutoff: datetime) -> bool:
"""Check if a timestamp is after the cutoff."""
try:
dt = datetime.fromisoformat(timestamp)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
return dt >= cutoff
except (ValueError, TypeError):
return True # Include entries with unparseable timestamps