ADR 0015: M3 hardening and the self-host runtime
Status
Section titled “Status”Accepted. 2026-11-26.
Context
Section titled “Context”M3 adds rate limits, upload hardening, retention and deletion, signed webhooks with retries, JSON/CSV export, and a Node/Docker self-host path. The existing Hono app lived with the Cloudflare bindings and D1/R2 adapters, so a second runtime would either duplicate routes or push Cloudflare types into the portable core. PLAN §6 and ADR 0013 require the core to stay free of Cloudflare-specific imports and both runtimes to share contracts.
The metadata model already used tenant_id on every tenant-owned row. The capture payload stored
submitted map/lot, address, and permit values only inside JSON, which made portable search
dialect-specific. Rate limits and webhook retries need durable state rather than isolate memory.
Decision
Section titled “Decision”- Move the Hono router, middleware, and error mapping into
packages/api, a runtime-neutral package configured with dependency and runtime callbacks.apps/apiremains the Cloudflare entry point andapps/api-nodeis the self-host entry point. - Add
packages/contract-testswith one Vitest suite executed by both adapters. The suite covers health, API-key and session authentication, upload validation, capture intake, review, export, webhooks, deletion/retention, and rate limiting. - Add ports for
RateLimiter,MalwareScanner,WebhookRepository,WebhookTransport, andRetentionRepository. Use fixed-window rate-limit counters in the metadata store; use HMAC-SHA256 over${timestamp}.${body}for webhook signatures. - Store submitted map/lot, address, and permit number as capture columns so SQLite and Postgres use the same search SQL. Keep the JSON payload as the immutable capture record.
- Enqueue webhook deliveries inside the review transaction. Deliveries are at-least-once, retried with exponential backoff, and safe to process from more than one instance.
- Soft-delete captures by setting
deleted_at, removing attachment objects, and appendingcapture.deleted. Audit events are never deleted. Lists, detail, and exports exclude deleted captures. A retention pass applies per-tenant capture and unconsumed-upload ages. - Implement the Node adapter with raw parameterized SQL over
better-sqlite3orpg, an S3-compatible store using@aws-sdk/client-s3, and an optional filesystem store for local development. Docker Compose provides Postgres, MinIO, the API, and the built web app behind nginx.
Consequences
Section titled “Consequences”- The Worker bundle and core stay free of Node,
pg, and AWS SDK imports. - Two SQL implementations exist: Drizzle/SQLite for D1 and raw SQL for Node. The shared contract suite and the mirrored migration schema keep them aligned.
- Webhook secrets must be stored recoverably because they sign payloads. They are returned only once and never appear in delivery history. Hosted deployments should encrypt the database at rest.
- Deletion destroys evidence bytes but keeps tenant-scoped capture metadata and audit events; a future hard-purge policy would need a new ADR.
- Self-host uses the same migration semantics but its own migration runner. D1 and Node schemas must be changed together.
Verification
Section titled “Verification”pnpm typecheck,pnpm test,pnpm build, andpnpm spec:validatepass.- The same contract suite passes with Miniflare D1/R2 and Node/SQLite; Postgres runs the suite when
TEST_POSTGRES_URLis provided. packages/corehas no Cloudflare imports.- CI runs the hosted verify job and the self-host contract job.
- The repository remains private; this ADR does not authorize a public flip or Cloudflare deployment.