Ingest Contract

What a registry sends to POST /ingest/acdp, and exactly how the control plane (CP) authenticates, attributes, validates, and stores it. This document is the owner of the ingest facts (HMAC key choice, enrollment, tenant attribution, quota, Origin handling, authority derivation, dedup); API.md summarises the route and links here.

The sender's side — the webhook envelope, event variants, header set and signature scheme — is defined by the registry in acdp-registry-rs WEBHOOKS.md. This page does not restate it; it documents what the CP does with what arrives.

Request lifecycle

The checks run in this order. Everything above the HMAC check runs for an unauthenticated request.

sequenceDiagram
    autonumber
    participant R as Registry
    participant X as Express body parser
    participant G as Global guards
    participant S as IngestService
    participant DB as Postgres
    participant P as EventProcessorService
    R->>X: POST /ingest/acdp (raw JSON body)
    X-->>R: 413 if body > INGEST_MAX_BODY_BYTES, 400 if malformed JSON
    X->>G: rawBody captured, body parsed
    Note over G: AuthGuard skipped (@Public) - ThrottleByUserGuard keyed on client IP
    G-->>R: 429 RATE_LIMITED when throttled
    G->>S: handle(rawBody, headers)
    Note over S: depth pre-scan of the raw text, object check (400)
    S->>DB: find enrollment by claimed authority
    S-->>R: 403 REGISTRY_DISABLED or REGISTRY_NOT_ENROLLED
    Note over S: HMAC-SHA256 over raw bytes with the chosen secret
    S-->>R: 401 INVALID_WEBHOOK_SIGNATURE
    Note over S: field checks + domain-pack gate (400)
    Note over S: QuotaService.enforce(resolved tenant, publish)
    S-->>R: 429 QUOTA_EXCEEDED + Retry-After
    S->>P: process(payload, runId, tenantId, baseUrl, eventId)
    P->>DB: insert context_events (duplicate = stop, no side effects)
    P->>DB: runs, lineage_edges, context_lifecycle, agents, registries
    P-->>P: publish SSE (per-run + global), fire outbound webhooks (not awaited)
    S-->>R: 204 No Content
OrderCheckFailure
1Express JSON body parser (captures the raw bytes, then parses): size limit INGEST_MAX_BODY_BYTES (default 1 MiB); malformed JSON413 PAYLOAD_TOO_LARGE / 400 INVALID_PAYLOAD
2Drain gate (only while the process is closing)503 SERVICE_DRAINING
3ThrottleByUserGuard — the route is @Public(), so the bucket is the client IP (normalizeIp; IPv6 per /64; req.ip honours TRUST_PROXY only)429 RATE_LIMITED
4Nesting depth > INGEST_MAX_JSON_DEPTH (default 64); payload not an object400 INVALID_PAYLOAD
5Enrollment lookup for the claimed authority403 REGISTRY_DISABLED / 403 REGISTRY_NOT_ENROLLED
6HMAC-SHA256401 INVALID_WEBHOOK_SIGNATURE
7type present; agent_id present when type is context_published; domain-pack gate; an authority is derivable400 INVALID_PAYLOAD
8publish quota for the resolved tenant — see Quota429 QUOTA_EXCEEDED + Retry-After
9Pipeline (a duplicate stops at its first step)500 INTERNAL_ERROR on a database failure

Success is always 204 No Content, for a new event and for a duplicate alike. Error bodies use the CP error envelope (API.md).

Authentication — HMAC-SHA256

x-acdp-signature carries the hex HMAC-SHA256 of the raw request bytes (src/ingest/hmac.ts). The sha256= prefix is optional. The comparison is constant-time; a missing header, non-hex value or wrong length fails.

Which secret. If the claimed authority has an enrollment (POST /registries/enroll) with a webhookSecret, that secret is used. Otherwise the global WEBHOOK_SECRET is used. The registry side of this is the per-subscription webhook.secret in its signature scheme.

Empty secret = no verification. When the chosen secret is empty (no enrollment secret and WEBHOOK_SECRET unset), every signature is accepted. Startup refuses an empty WEBHOOK_SECRET unless NODE_ENV=development (the default when NODE_ENV is unset), so this is a development-only mode. An enrollment secret is still enforced in development.

What is signed, and what is not. Only the body. These headers are read but are not covered by the signature, so anyone replaying a captured delivery can change them:

HeaderUsed for
x-run-idRun correlation (wins over body run_id)
x-tenant-idTenant attribution for an unenrolled authority
x-acdp-event-idDedup key (wins over body event_id)
originRegistry base URL fallback

No replay window. The CP ignores X-ACDP-Timestamp and X-ACDP-Signature-Timestamped; it enforces no freshness bound. The only replay defence is dedup, which a replayer defeats by sending a fresh x-acdp-event-id. See the registry's note on why the timestamped signature is opt-in for receivers: WEBHOOKS.md — signature scheme.

Send Content-Type: application/json. The raw bytes the HMAC is computed over are captured by the body parser for JSON (and form) bodies only; a body under any other content type is never seen, so it fails the signature check.

Test signer (Node.js)

For sending test events by hand. Sign the exact bytes you send.

import { createHmac } from 'node:crypto';

const body = JSON.stringify(event);
const sig = createHmac('sha256', WEBHOOK_SECRET).update(body).digest('hex');

await fetch('http://localhost:3001/ingest/acdp', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'x-acdp-signature': `sha256=${sig}`,
    'x-run-id': runId, // optional
  },
  body,
});

Authority derivation

The authority selects the enrollment (and so the secret and tenant) before HMAC, and is stored on the event:

  1. body registry_authority, else
  2. the host part of ctx_id when it has the form acdp://<authority>/<id>.

The registry's webhook envelope carries no registry_authority, so in practice it comes from ctx_id. The claimed authority is untrusted until the HMAC passes; an attacker can claim any authority but cannot produce its signature. An event with neither is still looked up as "no enrollment"; after HMAC it is rejected 400 (Missing required field: registry_authority (and ctx_id has no authority)).

Registry enrollment

Enrollment (POST /registries/enroll, admin-only — API.md) binds an authority to one tenant (the authority is the table's primary key), with an optional webhookSecret and baseUrl, and an enabled flag. The tenant binding is immutable: re-enrolling under the same tenant updates the enrollment (PATCH-like — omitted fields, including webhookSecret and a disabled enabled: false, are kept; an explicit "webhookSecret": null clears the per-registry secret so ingest falls back to the global WEBHOOK_SECRET), while re-enrolling under a different tenant is rejected with 409 REGISTRY_ENROLLED_ELSEWHERE and leaves the binding (and so the tenant that ingest resolves for the authority) unchanged.

Enrollment stateINGEST_REQUIRE_ENROLLMENT=false (default)INGEST_REQUIRE_ENROLLMENT=true
enrolled, enabledaccepted; enrollment's tenant + secretsame
enrolled, enabled=false403 REGISTRY_DISABLED403 REGISTRY_DISABLED
not enrolledaccepted; global secret403 REGISTRY_NOT_ENROLLED

Current behaviour: both 403s are returned before the HMAC check, so an unauthenticated caller can learn whether an authority is enrolled or disabled by the status it gets back (403 versus 401). Each request also costs one enrollment lookup.

Tenant attribution

The route is @Public(), so the AuthGuard tenant rules (AUTH_REQUIRE_TENANT, TENANT_HEADER_TRUST, the reserved-default rejection) do not apply. The event's tenant is:

CaseTenant
Authority is enrolledThe enrollment's tenant. X-Tenant-Id is ignored.
Not enrolled, INGEST_STRICT_TENANT=false (default)X-Tenant-Id (trimmed), else default
Not enrolled, INGEST_STRICT_TENANT=true, header names a non-default tenantdefault, with a warn log (ingest: unenrolled authority asserted X-Tenant-Id; …)

INGEST_STRICT_TENANT is recommended for multi-tenant deployments: only a server-side enrollment can then place events in a non-default tenant. Every write in the pipeline is stamped with this tenant. See TENANCY.md.

Quota and rate limits

The publish quota is enforced inside IngestService (QuotaService.enforce(tenantId, 'publish', res)), immediately before the pipeline — not by a route guard. A guard would run before the signature is checked and before the tenant is known.

  • Only signed, accepted requests count. The check sits after the enrollment lookup, HMAC verification, field checks and the domain-pack gate, so a request that ends in 400, 401 or 403 consumes nothing. A forged-signature flood cannot drain a tenant's budget. (With WEBHOOK_SECRET empty — dev mode — "signed" just means "passed the other checks".)
  • Charged to the resolved tenant. The counter is acdp:quota:<tenant>:publish, where <tenant> is the one from Tenant attribution: the enrollment's tenant for an enrolled authority; otherwise X-Tenant-Id or default (default whenever INGEST_STRICT_TENANT rejects the header). So a tenant-x:publish=… rule in TENANT_QUOTAS limits registries enrolled under tenant-x.
  • Over the limit → 429 QUOTA_EXCEEDED with Retry-After, counted as acdp_ingest_rejected_total{reason="quota"}. With no matching rule, or the quota store unavailable, the request passes (fail-open, unchanged).
  • A registry retry of an event the CP already stored still counts: dedup happens inside the pipeline, after the quota check.

Upgrade note (tenant-enroll-quota-fix P4). Before this change the route carried @CheckQuota('publish'): every request, forged ones included, was counted against default, and only a default:publish rule ever limited ingest. Now:

  • A deployment with only default:publish stops throttling enrolled registries bound to other tenants. Unenrolled authorities, and strict-mode fallbacks, still land in default and stay limited. Add a <tenant>:publish=… rule for each tenant you want to cap.
  • With INGEST_STRICT_TENANT off, an unenrolled sender holding the global WEBHOOK_SECRET can name any rule-less X-Tenant-Id and so avoid the default cap. The same sender (or any enrollment without its own secret) can also name an enrolled tenant and spend that tenant's publish budget, making its registry receive 429s (and drop events). Set INGEST_STRICT_TENANT=true (or INGEST_REQUIRE_ENROLLMENT=true) and give each enrollment its own webhookSecret so only an enrollment chooses the tenant.

A quota 429 drops events. The registry's webhook worker treats 429 as retryable, but makes at most webhook.max_retries attempts (default 3) with exponential backoff from 250 ms — about 0.75 s in total with the defaults, and without waiting for Retry-After — then logs and drops the event. See the registry's WEBHOOKS.md — delivery and retries. Size publish rules for peak legitimate traffic: they shed load by losing events, not by delaying them. Watch acdp_ingest_rejected_total{reason="quota"}.

Unsigned traffic is not quota-limited. What bounds a flood of unsigned or forged requests is the coarse ThrottleByUserGuard (THROTTLE_LIMIT per THROTTLE_TTL_MS, default 200 per 60 s), keyed on the client IP, plus the body size cap (Request limits). Each such request still costs one JSON parse and one enrollment lookup before it is rejected. Behind a proxy or load balancer, set TRUST_PROXY so every registry does not share the proxy's throttle bucket. See POLICY.md for quota configuration.

Request limits

Env varDefaultEffect
INGEST_MAX_BODY_BYTES1048576 (1 MiB)Body-parser limit for the whole app (matches the registry's 1 MB payload ceiling). Larger → 413 PAYLOAD_TOO_LARGE, before any guard runs.
INGEST_MAX_JSON_DEPTH64Nesting-depth scan of the raw text in IngestService. Deeper → 400.

Current behaviour: the Express JSON parser has already parsed the body by the time IngestService runs its depth scan and its own JSON.parse, so the depth cap bounds what the CP processes further, not the first parse; that is bounded only by the byte limit. IngestService also re-checks the byte length (400), but the parser's 413 fires first for any JSON request.


Run correlation

The run id is the x-run-id header, else the body's top-level run_id. With neither, the event is stored and broadcast on the global feed but belongs to no run.

The first event for a (tenant, run_id) creates the runs row (status running). Its scenario_id is the event's scenario (below), or "unknown". Every later non-duplicate event with that run id — of any type — increments contexts_count and adds its authority to registries if new. The scenario is not updated by later events; POST /runs/started fills it in when it is still "unknown".

Event shape

The wire format is the registry's (WEBHOOKS.md); lifecycle events are normative in RFC-ACDP-0013 and the lifecycle-event-types registry. The CP stores the whole payload in context_events.raw_payload and reads only these fields:

FieldRequiredUsed for
typeyescontext_published, context_retrieved, context_retracted, context_republished, search_executed. Lineage edges only on context_published; retract/republish drive context_lifecycle.
agent_idcontext_published onlyProducer DID, stored opaque. Retrieve/search events are agent-less by design. Populates agents when present.
registry_authorityderived if absentSee Authority derivation. Populates registries.
ctx_idnoacdp://<authority>/<id>. Authority fallback, lineage edge target, part of the content fingerprint.
event_idnoRegistry-minted delivery id; dedup key when X-ACDP-Event-Id is absent.
registry_base_urlnoPreferred registry base URL.
lineage_idnoStored; used by the lifecycle projection.
context_typenoStored; subject to the domain-pack gate.
visibility, versionnoStored. version is part of the content fingerprint.
derived_fromnoArray of ctx_ids; each becomes a lineage edge on context_published.
metadata.scenario_id, scenario_idnoRun scenario. metadata.scenario_id wins over the top-level field.
run_idnoSee Run correlation.
created_at, atnoEvent timestamp: created_at, else at (the RFC-ACDP-0013 lifecycle timestamp), else arrival time.
actor, reasonnoLifted into context_lifecycle and the SSE event on retract/republish.
key_fingerprintnoLifted into the key_fingerprint column (string values only); SSE keyFingerprint.
registry_receiptnoKept in raw_payload; an object here sets receipt_present, and the receipt-audit sweep checks it later.

Unknown fields are kept in raw_payload and returned by GET /runs/:runId/events.

Minimal example

{
  "type":         "context_published",
  "event_id":     "6f1c2c1e-4b8a-4d3f-9d2e-0b7f3a1c9e55",
  "agent_id":     "did:web:scoring-agent.example",
  "ctx_id":       "acdp://registry-east.example/01F3…",
  "context_type": "analysis",
  "derived_from": ["acdp://registry-east.example/01F2…"],
  "metadata":     { "scenario_id": "credit-review-v1" },
  "created_at":   "2026-05-24T12:00:00Z"
}

Registry base URL

The federation proxy (GET /contexts/*ctxId) needs a base URL for each authority. The pipeline's registry upsert takes the first value that is not null/undefined (chained with ??, so an empty string stops the chain and leaves the stored URL unchanged):

  1. body registry_base_url,
  2. the request's Origin header,
  3. the enrollment's baseUrl.

A non-empty value overwrites the stored one; an empty one leaves it unchanged. Origin is unsigned, so a replayed delivery can repoint the stored URL; the federation proxy's SSRF gate still applies to whatever is stored.

Domain-pack context_type gate

Domain packs are compiled in (src/domain-packs/known-packs.ts; today only finance) and selected at boot by DOMAIN_PACKS. An unknown name fails startup. GET /domain-packs lists the active packs; there is no runtime reload.

  • No packs configured → no gate; every context_type is accepted.
  • Always accepted: the base types data_snapshot, analysis, prediction, alert (context-types registry), plus key-revocation and its interim spelling acdp:key-revocation (RFC-ACDP-0014 §4/§10), both from src/contracts/revocation.ts. An event with no context_type is also accepted.
  • Anything else must be declared by an active pack, or it is rejected 400, logged at warn, and counted on acdp_ingest_rejected_total{reason="pack_gate"}.

Operator note — silent divergence. The registry treats a 4xx as permanent and does not retry it (it logs webhook_4xx), so a pack-gated publish is stored by the registry but never by the CP. Watch the metric above, and either declare the type in a pack or leave DOMAIN_PACKS unset.


What happens after ingest

EventProcessorService.process runs the seven-step pipeline described in ARCHITECTURE.md: persist (with dedup), run upsert, lineage edges / lifecycle projection, agent upsert, registry upsert, SSE publish, outbound webhooks. The 204 is sent after SSE publish; outbound webhook delivery is not awaited.

Idempotency

Each event gets a dedup key, stored in context_events.fingerprint under a partial unique index on (tenant_id, fingerprint):

  1. X-ACDP-Event-Id header (trimmed), else body event_id → stored as evt:<id>;
  2. otherwise a content fingerprint: the first 32 hex characters of sha256("type:ctx_id:agent_id:created_at:run_id:version"), where run_id is the resolved run id (header or body). The evt: prefix keeps the two forms from colliding.

A duplicate insert is skipped and the request still returns 204, with no side effects: no run increment, lineage edge, SSE event or outbound webhook. Lineage edges are also unique on (tenant_id, from_ctx_id, to_ctx_id).

Limits of this dedup (current behaviour):

  • It is per tenant. The same event attributed to two tenants is stored twice.
  • X-ACDP-Event-Id is unsigned; a different value is a different event.
  • Once retention purges a context_events row, a replay of that event is new.
  • Lifecycle events with the header stripped. The body event_id is a per-delivery id; the actor-minted lifecycle_event_id is provenance and never a dedup key. A producer's byte-identical resubmit of a signed retract or republish therefore arrives as two deliveries with different ids and is stored twice. The lifecycle projection is unaffected (re-applying a transition is a no-op). See the registry's two-ids note.
  • GET /ingest/health — @Public(), returns { "ok": true }; for testing a registry's webhook configuration. Throttled like any other route.
  • POST /runs/started and POST /runs/:runId/complete use the same HMAC header, but always with the global WEBHOOK_SECRET (never an enrollment secret). They take X-Tenant-Id as given — INGEST_STRICT_TENANT does not apply — except that an explicit default is rejected 403 TENANT_RESERVED. See API.md.