Event Processing Architecture
The transactional outbox, the event relay, NATS JetStream delivery, evidence bundles in MinIO, and reliability metrics.
Every change that matters to security appends a row to security_events inside the transaction that made the change. The event pipeline delivers those committed events to the outside world without ever touching the request path.
Transactional outbox
The trigger means no writer had to change to participate. A rolled-back transaction leaves no outbox row, so subscribers never see an uncommitted change.
The relay
runtime/gateway/cmd/event-relay is a separate Go process with three jobs.
Publishing
- Claim up to
RELAY_BATCH(default 100) outbox rows, oldest first, withFOR UPDATE SKIP LOCKED. - Publish each event to stream
AGENTSHIELD_EVENTSon subjectagentshield.events.<organization id>.<event type>, withNats-Msg-Idset to the event id. - Delete only the rows the stream acknowledged, then commit.
A crash at any point republishes; the stream's ten-minute duplicate window drops the copy. A publish failure stops the batch, so events from one relay stay in order. The stream retains events for seven days.
Evidence bundles
A durable JetStream consumer named evidence filters content.flagged and tool.failed. For each event it writes a JSON bundle containing the event, invocation, decision, execution, and alerts, all copied from rows that were already redacted when written.
- Object key:
<organization>/<yyyy>/<mm>/<dd>/<event id>.jsonin the privateagentshield-evidencebucket. - The object is written before the
evidence_objectsrow that records its SHA-256, size, and key. - An event that already has a row is acknowledged without writing again.
- Explicit acks, a 30-second ack wait, and at most eight deliveries. A malformed message is terminated rather than retried forever.
The control plane's GET /api/v1/evidence/{id} recomputes the SHA-256 of the stored object and refuses it with 409 when it no longer matches. Every read appends evidence.viewed or evidence.integrity_failed, which the relay then publishes like any other event.
Reliability metrics
Every 30 seconds the relay calls agentshield_compute_reliability for minutes that ended more than two minutes ago, so late commits are included. It aggregates finished executions per organization, agent, tool, and minute into reliability_metrics:
| Metric | Labels |
|---|---|
tool_calls_completed | tool |
tool_calls_failed | tool, failure kind |
tool_latency_ms_avg, tool_latency_ms_max | tool. Measured from dispatched_at to ended_at |
Metrics are computed from executions, the source of truth, rather than counted from the stream, where redelivery would double-count. A unique index makes recomputing a minute a no-op.
Least privilege
The relay's cross-organization work runs through SECURITY DEFINER functions: claim, acknowledge, backlog, and compute. It therefore runs as agentshield_app, and that role has no direct access to event_outbox or reliability_cursor.
Health
| Endpoint | Behavior |
|---|---|
GET /health/live | Process liveness |
GET /health/ready | 503 when PostgreSQL or NATS is unreachable, or when the oldest queued event has waited more than one minute |
GET /metrics | Prometheus metrics on the same internal port (RELAY_HTTP_ADDR, default 127.0.0.1:8083) |
A stopped relay only grows the outbox. The gateway's hot path is unchanged, and events go out when the relay starts again.
Delivery semantics
| Property | Guarantee |
|---|---|
| Uncommitted events | Never published |
| Committed events | Published at least once; deduplicated by Nats-Msg-Id within ten minutes, so effectively once |
| Ordering | Preserved per relay. Two relays may interleave; run one replica |
| Events before migration 0006 | Not in the outbox and not published |
Consume events with your own durable consumer. See Event streaming.
Implementation references
Was this page helpful?
Intelligence Engine
The Python content analysis service. What it checks, its HTTP contract, how the gateway calls it, and why its findings are advisory.
Data Storage Architecture
The PostgreSQL schema, how tenant isolation and append-only guarantees are enforced in the database, and what lives in Redis, NATS, and MinIO.