Skip to Content
AgentShield
Architecture

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

  1. Claim up to RELAY_BATCH (default 100) outbox rows, oldest first, with FOR UPDATE SKIP LOCKED.
  2. Publish each event to stream AGENTSHIELD_EVENTS on subject agentshield.events.<organization id>.<event type>, with Nats-Msg-Id set to the event id.
  3. 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>.json in the private agentshield-evidence bucket.
  • The object is written before the evidence_objects row 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:

MetricLabels
tool_calls_completedtool
tool_calls_failedtool, failure kind
tool_latency_ms_avg, tool_latency_ms_maxtool. 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

EndpointBehavior
GET /health/liveProcess liveness
GET /health/ready503 when PostgreSQL or NATS is unreachable, or when the oldest queued event has waited more than one minute
GET /metricsPrometheus 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

PropertyGuarantee
Uncommitted eventsNever published
Committed eventsPublished at least once; deduplicated by Nats-Msg-Id within ten minutes, so effectively once
OrderingPreserved per relay. Two relays may interleave; run one replica
Events before migration 0006Not in the outbox and not published

Consume events with your own durable consumer. See Event streaming.

Was this page helpful?

On this page