4b6817381b
CI (OpenClaw E2E) / openclaw test (push) Has been cancelled
CI / coverage-report (push) Has been cancelled
CI / test-kubernetes (push) Has been cancelled
CI / should-run-thorough (push) Has been cancelled
CI / test-thorough (cloudwatch-demo) (push) Has been cancelled
CI / test-thorough (flink-ecs) (push) Has been cancelled
CI / test-thorough (upstream-lambda) (push) Has been cancelled
CI / test-thorough (prefect-ecs-fargate) (push) Has been cancelled
Release / build-binaries (zip, opensre.exe, onefile, windows-latest, windows-x64) (push) Has been cancelled
Benchmark image — build + push to ECR (any adapter) / build + push (push) Has been cancelled
CI / quality (ubuntu-latest) (push) Has been cancelled
CI / test (tools-runtime) (push) Has been cancelled
CI / test (e2e-general) (push) Has been cancelled
CI / test (cli-runtime) (push) Has been cancelled
CI / test (e2e-provider-and-openclaw) (push) Has been cancelled
CI / test (integrations-and-misc) (push) Has been cancelled
Release / verify (push) Has been cancelled
Release / build-python-dist (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-15-intel, darwin-x64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, macos-latest, darwin-arm64) (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04, linux-x64) (push) Has been cancelled
Release / publish-release (push) Has been cancelled
Release / publish-main-release (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-checks (no-LLM) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Interactive Shell Live (PR + post-merge) / turn-live shard ${{ matrix.shard_index }} (push) Has been cancelled
Release / prepare (push) Has been cancelled
Release / build-binaries (tar.gz, opensre, onedir, ubuntu-22.04-arm, linux-arm64) (push) Has been cancelled
Synthetic Deterministic Tests / Synthetic offline (deterministic) (push) Has been cancelled
65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
"""JSONL audit logger for guardrail events."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
from datetime import UTC, datetime
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
from config.constants import OPENSRE_HOME_DIR
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_DEFAULT_AUDIT_PATH = OPENSRE_HOME_DIR / "guardrail_audit.jsonl"
|
|
|
|
|
|
class AuditLogger:
|
|
"""Append-only JSONL audit log for guardrail matches."""
|
|
|
|
def __init__(self, path: Path | None = None) -> None:
|
|
self._path = path or _DEFAULT_AUDIT_PATH
|
|
|
|
def log(
|
|
self,
|
|
*,
|
|
rule_name: str,
|
|
action: str,
|
|
matched_text_preview: str,
|
|
context: str = "",
|
|
) -> None:
|
|
"""Append one audit entry. Never raises on write failure."""
|
|
preview = (
|
|
matched_text_preview[:40] if len(matched_text_preview) > 40 else matched_text_preview
|
|
)
|
|
entry = {
|
|
"timestamp": datetime.now(UTC).isoformat(),
|
|
"rule_name": rule_name,
|
|
"action": action,
|
|
"matched_text_preview": preview,
|
|
"context": context,
|
|
}
|
|
try:
|
|
self._path.parent.mkdir(parents=True, exist_ok=True)
|
|
with self._path.open("a", encoding="utf-8") as fh:
|
|
fh.write(json.dumps(entry) + "\n")
|
|
except OSError:
|
|
logger.warning("Failed to write guardrail audit log to %s", self._path)
|
|
|
|
def read_entries(self, *, limit: int = 100) -> list[dict[str, Any]]:
|
|
"""Read the most recent audit entries."""
|
|
if not self._path.exists():
|
|
return []
|
|
try:
|
|
lines = self._path.read_text(encoding="utf-8").strip().splitlines()
|
|
except OSError:
|
|
return []
|
|
entries: list[dict[str, Any]] = []
|
|
for line in lines[-limit:]:
|
|
try:
|
|
entries.append(json.loads(line))
|
|
except json.JSONDecodeError:
|
|
continue
|
|
return entries
|