Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ trace, study, receipt, API and database schema versions.

### Added

- An audit log of access and deletion actions. Migration 6 adds an
`audit_events` table recording API keys created and revoked, traces deleted
and retention purges, with the project, target, counts and acting identity
(admin API, project key prefix, CLI, dashboard or retention schedule), written
in the same transaction as the action. Admins read it with
`GET /v1/audit-events`, `AgentLoopClient.list_audit_events`,
`agentloop audit-log` or `agentloop remote-audit-log`. Key values are never
recorded.
- Trace labels are visible and searchable. Paginated trace listings include
each trace's `metadata.labels`, and `GET /v1/traces?label=KEY=VALUE`,
`AgentLoopClient.list_traces(label=...)`, `agentloop list-stored-traces --label`
Expand Down
37 changes: 33 additions & 4 deletions agentloop/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ def init_store() -> None:
@app.command("create-api-key")
def create_api_key(project_id: str = "default", name: str = "default") -> None:
db = get_store()
key = db.create_api_key(project_id=project_id, name=name)
key = db.create_api_key(project_id=project_id, name=name, actor="cli")
console.print("Created API key. Save it now; it will not be shown again.")
console.print(key["api_key"])

Expand All @@ -1006,7 +1006,7 @@ def revoke_api_key(
) -> None:
"""Revoke a project's API key; it stops working immediately."""
try:
revoked = get_store().revoke_api_key(project_id, prefix)
revoked = get_store().revoke_api_key(project_id, prefix, actor="cli")
except ValueError as exc:
raise typer.BadParameter(str(exc)) from exc
if not revoked:
Expand All @@ -1023,7 +1023,7 @@ def delete_trace(
project_id: str = "default",
) -> None:
"""Delete a stored trace with its findings, usage and interventions."""
deleted = get_store().delete_trace(run_id, project_id)
deleted = get_store().delete_trace(run_id, project_id, actor="cli")
if deleted is None:
console.print(f"No trace {run_id} in project {project_id}.", markup=False)
raise typer.Exit(1)
Expand All @@ -1041,6 +1041,7 @@ def purge_traces(
project_id: str | None = typer.Option(None, help="Only purge this project."),
) -> None:
"""Apply a retention period: delete traces stored longer ago than allowed."""
actor = "cli" if older_than_days is not None else "retention:AGENTLOOP_RETENTION_DAYS"
if older_than_days is None:
configured = os.getenv("AGENTLOOP_RETENTION_DAYS", "").strip()
if not configured:
Expand All @@ -1057,7 +1058,7 @@ def purge_traces(
param_hint="AGENTLOOP_RETENTION_DAYS",
) from None
try:
deleted = get_store().purge_traces(older_than_days, project_id=project_id)
deleted = get_store().purge_traces(older_than_days, project_id=project_id, actor=actor)
except ValueError as exc:
raise typer.BadParameter(str(exc)) from exc
scope = f"project {project_id}" if project_id else "all projects"
Expand All @@ -1070,6 +1071,20 @@ def purge_traces(
)


@app.command("audit-log")
def audit_log(
project_id: str | None = typer.Option(None, help="Only this project's events."),
limit: int = typer.Option(50, min=1, max=1000, help="Newest events to show."),
) -> None:
"""Show recent key, deletion and retention events from the store's audit log."""
console.print_json(
data={
"project_id": project_id,
"events": get_store().list_audit_events(project_id, limit=limit),
}
)


@app.command("store-trace")
def store_trace(
path: Path = Path("runs/research_agent_baseline.json"),
Expand Down Expand Up @@ -1515,6 +1530,20 @@ def remote_list_api_keys(
console.print_json(data=client.list_api_keys(project_id))


@app.command("remote-audit-log")
def remote_audit_log(
project_id: str | None = typer.Option(None, help="Only this project's events."),
limit: int = typer.Option(50, min=1, max=1000, help="Newest events to show."),
api_url: str = typer.Option("http://127.0.0.1:8000", help="AgentLoop API base URL."),
admin_api_key: str | None = typer.Option(
None, help="Admin API key. Defaults to AGENTLOOP_ADMIN_API_KEY."
),
) -> None:
"""Show a remote server's audit log (admin key required)."""
client = _remote_admin_client(api_url, admin_api_key)
console.print_json(data=client.list_audit_events(project_id=project_id, limit=limit))


@app.command("remote-revoke-api-key")
def remote_revoke_api_key(
prefix: str = typer.Option(..., help="First 10 characters of the key, as listed."),
Expand Down
9 changes: 9 additions & 0 deletions agentloop/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ def create_api_key(self, project_id: str = "default", name: str = "default") ->
credential_scope="admin",
)

def list_audit_events(self, project_id: str | None = None, limit: int = 100) -> dict[str, Any]:
"""Newest audit events first, optionally for one project (admin key required)."""
params: dict[str, Any] = {"limit": limit}
if project_id:
params["project_id"] = project_id
return self._request(
"GET", "/audit-events?" + urllib.parse.urlencode(params), credential_scope="admin"
)

def list_api_keys(self, project_id: str) -> dict[str, Any]:
"""List a project's key prefixes, names and usage times (admin key required)."""
return self._request(
Expand Down
19 changes: 19 additions & 0 deletions agentloop/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,25 @@ class Migration:
"PRIMARY KEY(project_id, intervention_id))",
),
),
Migration(
version=6,
name="audit_log",
sqlite_statements=(
"CREATE TABLE IF NOT EXISTS audit_events ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"occurred_at TEXT DEFAULT CURRENT_TIMESTAMP, project_id TEXT, "
"action TEXT NOT NULL, actor TEXT NOT NULL, target TEXT, "
"details_json TEXT NOT NULL DEFAULT '{}')",
"CREATE INDEX IF NOT EXISTS audit_events_project_id ON audit_events(project_id, id)",
),
postgres_statements=(
"CREATE TABLE IF NOT EXISTS audit_events ("
"id BIGSERIAL PRIMARY KEY, occurred_at TIMESTAMPTZ DEFAULT now(), project_id TEXT, "
"action TEXT NOT NULL, actor TEXT NOT NULL, target TEXT, "
"details_json TEXT NOT NULL DEFAULT '{}')",
"CREATE INDEX IF NOT EXISTS audit_events_project_id ON audit_events(project_id, id)",
),
),
)


Expand Down
19 changes: 16 additions & 3 deletions agentloop/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,18 @@ def create_api_key(
_: None = Depends(resolve_admin),
db: TraceStore = Depends(store),
) -> dict[str, Any]:
return db.create_api_key(project_id=payload.project_id, name=payload.name)
return db.create_api_key(project_id=payload.project_id, name=payload.name, actor="admin-api")


@versioned.get("/audit-events")
def list_audit_events(
project_id: str | None = Query(default=None, min_length=1),
limit: int = Query(default=100, ge=1, le=MAX_PAGE_SIZE),
_: None = Depends(resolve_admin),
db: TraceStore = Depends(store),
) -> dict[str, Any]:
"""Newest audit events first: key creation and revocation, deletions and purges."""
return {"project_id": project_id, "events": db.list_audit_events(project_id, limit=limit)}


@versioned.get("/api-keys")
Expand All @@ -198,7 +209,7 @@ def revoke_api_key(
) -> dict[str, Any]:
"""Revoke a project's key by its prefix; it stops working immediately."""
try:
revoked = db.revoke_api_key(project_id, prefix)
revoked = db.revoke_api_key(project_id, prefix, actor="admin-api")
except ValueError as exc:
raise HTTPException(status_code=422, detail=str(exc)) from exc
if not revoked:
Expand Down Expand Up @@ -302,9 +313,11 @@ def delete_trace(
run_id: str,
project_id: str = Depends(resolve_project),
db: TraceStore = Depends(store),
x_agentloop_key: str | None = Header(default=None),
) -> dict[str, Any]:
"""Delete a trace of the caller's project with its findings, usage and interventions."""
deleted = db.delete_trace(run_id, project_id)
actor = f"api-key:{x_agentloop_key[:10]}" if x_agentloop_key else "api"
deleted = db.delete_trace(run_id, project_id, actor=actor)
if deleted is None:
raise HTTPException(status_code=404, detail="trace not found")
return {"run_id": run_id, "project_id": project_id, "deleted": deleted}
Expand Down
Loading
Loading