feat: add audit log tables, indexes, and CLI/content recording hooks

This commit is contained in:
2026-06-11 20:28:17 +00:00
parent 3862958fae
commit 78d70d9b08
58 changed files with 4456 additions and 25 deletions
@@ -252,9 +252,38 @@ class GatewayUsageLedger:
"user_agent": (raw.get("user_agent") or "")[:300],
}
get_table(GATEWAY_LEDGER).insert(row)
self._audit(raw, norm, cost_usd)
except Exception as exc:
logger.warning("gateway usage record failed: %s", exc)
def _audit(self, raw: dict, norm: dict, cost_usd: float) -> None:
from devplacepy.services.audit import record as audit
owner_kind = raw.get("owner_kind") or "unknown"
owner_id = raw.get("owner_id") or "unknown"
actor_kind = "guest" if owner_kind == "guest" else ("user" if owner_kind in ("user", "admin") else "system")
audit.record_system(
"ai.gateway.call",
actor_kind=actor_kind,
actor_uid=owner_id if actor_kind == "user" else None,
actor_role="admin" if owner_kind == "admin" else (actor_kind if actor_kind != "user" else "member"),
origin="api",
result="success" if raw.get("success") else "failure",
summary=f"LLM call by {owner_kind}/{owner_id} (model {raw.get('model') or ''})",
metadata={
"model": raw.get("model"),
"backend": raw.get("backend"),
"prompt_tokens": norm["prompt"],
"completion_tokens": norm["completion"],
"total_tokens": norm["total"],
"cost_usd": round(cost_usd, 8),
"status_code": raw.get("status_code"),
"error_category": raw.get("error_category"),
"owner_kind": owner_kind,
"owner_id": owner_id,
},
)
def sample_concurrency(self, in_flight: int) -> None:
try:
get_table(GATEWAY_CONCURRENCY).insert(