File size: 10,255 Bytes
3fbbaab 21ff762 3fbbaab 21ff762 3fbbaab 21ff762 3fbbaab 21ff762 3fbbaab | 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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | """ctx_audit_log.py -- Unified append-only audit log for every ctx action.
Every mutation that matters for post-hoc investigation is recorded here as a
single JSONL line in ``~/.claude/ctx-audit.jsonl``:
skill.added skill_add.add_skill()
skill.installed skill_add / skill_loader
skill.converted batch_convert / skill_category
skill.loaded context_monitor (from skill-events.jsonl "load")
skill.unloaded context_monitor (from skill-events.jsonl "unload")
skill.used usage_tracker (signal -> skill match)
skill.archived ctx_lifecycle archive transition
skill.deleted ctx_lifecycle purge transition
skill.restored ctx_lifecycle review-archived --restore
skill.score_updated skill_quality.persist_quality
skill.sidecar_rewritten skill_quality.SidecarSink.write
agent.* (same set, "agent" subject_type)
session.started first event in a session_id
session.ended Stop hook fires
backup.snapshot backup_mirror create/snapshot-if-changed
Schema (per line):
{
"ts": ISO-8601 UTC w/ seconds precision,
"event": dotted event name (see list above),
"subject_type": "skill" | "agent" | "session" | "backup" | "toolbox",
"subject": slug or id ("python-patterns", session uuid, etc.),
"actor": "hook" | "cli" | "lifecycle" | "user",
"session_id": optional session id if known,
"meta": optional dict with event-specific fields,
}
The log is append-only. Rotation is by day (``ctx-audit.jsonl`` is current;
``ctx-audit-YYYY-MM-DD.jsonl`` are historical). Callers never truncate.
Readers (``ctx-monitor`` dashboard, postmortem scripts) consume the log;
they never mutate it.
"""
from __future__ import annotations
import json
import os
import threading
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Literal
__all__ = [
"EVENT_TYPES",
"audit_log_path",
"log",
"log_skill_event",
"log_agent_event",
"log_session_event",
"log_backup_event",
"rotate_if_needed",
]
# Canonical event names. Keep this list in sync with the docstring above.
EVENT_TYPES: frozenset[str] = frozenset({
# lifecycle
"skill.added", "skill.installed", "skill.converted",
"skill.loaded", "skill.unloaded", "skill.used",
"skill.watched", "skill.demoted",
"skill.archived", "skill.deleted", "skill.restored",
"skill.score_updated", "skill.sidecar_rewritten",
# agent variants (same semantics)
"agent.added", "agent.installed",
"agent.loaded", "agent.unloaded", "agent.used",
"agent.watched", "agent.demoted",
"agent.archived", "agent.deleted", "agent.restored",
"agent.score_updated", "agent.sidecar_rewritten",
# meta
"session.started", "session.ended",
"backup.snapshot", "backup.prune",
"toolbox.triggered", "toolbox.verdict",
})
SubjectType = Literal["skill", "agent", "session", "backup", "toolbox"]
Actor = Literal["hook", "cli", "lifecycle", "user", "scheduler"]
# Single shared lock per process keeps concurrent write-within-process safe.
# Cross-process safety comes from O_APPEND semantics on POSIX + the atomic
# write-then-fsync pattern below on Windows.
_LOCK = threading.Lock()
_MAX_SAFE_DEPTH = 8
def audit_log_path() -> Path:
"""Return the configured audit log path.
Honors ``quality.paths.audit_log`` from ``ctx_config.cfg`` when set;
falls back to ``~/.claude/ctx-audit.jsonl``.
"""
try:
from ctx_config import cfg # local import — avoid cost on test import
raw = cfg.get("paths", {}) or {}
configured = raw.get("audit_log") if isinstance(raw, dict) else None
if isinstance(configured, str) and configured.strip():
return Path(os.path.expanduser(configured))
except Exception: # noqa: BLE001 — config unavailable in some test contexts
pass
return Path(os.path.expanduser("~/.claude/ctx-audit.jsonl"))
def _now_iso() -> str:
"""ISO-8601 UTC timestamp, seconds precision, trailing ``Z``."""
return datetime.now(timezone.utc).replace(microsecond=0).isoformat().replace(
"+00:00", "Z"
)
def _json_safe(value: Any, *, depth: int = 0, seen: set[int] | None = None) -> Any:
if seen is None:
seen = set()
if value is None or isinstance(value, (str, int, float, bool)):
return value
if depth >= _MAX_SAFE_DEPTH:
return repr(value)
ident = id(value)
if ident in seen:
return "<circular>"
if isinstance(value, Path):
return str(value)
if isinstance(value, dict):
seen.add(ident)
try:
return {
str(_json_safe(k, depth=depth + 1, seen=seen)):
_json_safe(v, depth=depth + 1, seen=seen)
for k, v in value.items()
}
finally:
seen.discard(ident)
if isinstance(value, (list, tuple, set, frozenset)):
seen.add(ident)
try:
return [
_json_safe(item, depth=depth + 1, seen=seen)
for item in value
]
finally:
seen.discard(ident)
isoformat = getattr(value, "isoformat", None)
if callable(isoformat):
try:
return isoformat()
except Exception: # noqa: BLE001
pass
return repr(value)
def log(
event: str,
*,
subject_type: SubjectType,
subject: str,
actor: Actor = "hook",
session_id: str | None = None,
meta: dict[str, Any] | None = None,
path: Path | None = None,
) -> None:
"""Append one audit line. Best-effort — never raises on I/O failure.
Unknown event names are logged under the literal string but emit a
stderr warning so callers notice typos early. We refuse to silently
remap them — audit logs are only useful if ``grep`` on an event name
matches exactly what the caller wrote.
"""
if event not in EVENT_TYPES:
import sys as _sys
print(
f"[ctx-audit] warning: unknown event {event!r}; consider adding "
f"to ctx_audit_log.EVENT_TYPES",
file=_sys.stderr,
)
record: dict[str, Any] = {
"ts": _now_iso(),
"event": event,
"subject_type": subject_type,
"subject": subject,
"actor": actor,
}
if session_id is not None:
record["session_id"] = session_id
if meta:
record["meta"] = meta
try:
target = path if path is not None else audit_log_path()
line = (
json.dumps(
_json_safe(record),
ensure_ascii=False,
separators=(",", ":"),
)
+ "\n"
)
with _LOCK:
target.parent.mkdir(parents=True, exist_ok=True)
# Append-only. O_APPEND is atomic per-write for writes up to
# PIPE_BUF bytes on POSIX; our records are well under that.
# On Windows we accept best-effort — concurrent writers may
# still interleave beyond PIPE_BUF, which is acceptable for
# an audit log (each record is a valid JSON document).
with open(target, "a", encoding="utf-8") as f:
f.write(line)
except Exception: # noqa: BLE001
# Never raise. Losing an audit line is strictly less bad than
# taking down a hook that's about to write valuable data
# elsewhere. Callers depend on this.
return
# ─── Convenience wrappers ───────────────────────────────────────────────────
def log_skill_event(
event: str,
slug: str,
*,
actor: Actor = "hook",
session_id: str | None = None,
meta: dict[str, Any] | None = None,
) -> None:
"""Log a ``skill.*`` event by slug."""
log(event, subject_type="skill", subject=slug, actor=actor,
session_id=session_id, meta=meta)
def log_agent_event(
event: str,
slug: str,
*,
actor: Actor = "hook",
session_id: str | None = None,
meta: dict[str, Any] | None = None,
) -> None:
"""Log an ``agent.*`` event by slug."""
log(event, subject_type="agent", subject=slug, actor=actor,
session_id=session_id, meta=meta)
def log_session_event(
event: str,
session_id: str,
*,
actor: Actor = "hook",
meta: dict[str, Any] | None = None,
) -> None:
"""Log a ``session.*`` event."""
log(event, subject_type="session", subject=session_id, actor=actor,
session_id=session_id, meta=meta)
def log_backup_event(
event: str,
snapshot_id: str,
*,
actor: Actor = "scheduler",
meta: dict[str, Any] | None = None,
) -> None:
"""Log a ``backup.*`` event (snapshot id or prune set id)."""
log(event, subject_type="backup", subject=snapshot_id, actor=actor,
meta=meta)
# ─── Log rotation (call at session-end / Stop hook) ─────────────────────────
def rotate_if_needed(max_bytes: int = 25 * 1024 * 1024) -> Path | None:
"""Rotate the current log if it exceeds ``max_bytes``.
Renames to ``ctx-audit-YYYYMMDDTHHMMSSZ.jsonl`` alongside the active
file. Returns the rotated path (or ``None`` if no rotation needed).
Called from ``quality_on_session_end.py`` so rotation happens at a
natural boundary — we never rotate mid-session.
"""
target = audit_log_path()
if not target.exists() or target.stat().st_size <= max_bytes:
return None
stamp = _now_iso().replace(":", "").replace("-", "").replace("Z", "Z")
rotated = target.with_name(f"{target.stem}-{stamp}{target.suffix}")
try:
target.rename(rotated)
except OSError:
# Another process may have rotated first; not an error.
return None
return rotated
|