ADR 0002: M1 capture API decisions
- Status: Accepted
- Date: 2026-11-24
- Deciders: AP Dev Solutions, LLC
- Related: the project overview §8–§10, §14 (M1),
docs/adr/0001-stack.md,openspec/changes/m1-capture-api/design.md
Context
Section titled “Context”M1 adds the first persisted capture intake path. The plan fixes the stack and most of the data model, but four decisions were not explicit: where pending uploads live, how captures are read before reviewer sessions exist, which initial status a stored capture receives, and whether uploads use presigned URLs or a Worker proxy.
Decisions
Section titled “Decisions”Pending uploads use a dedicated table
Section titled “Pending uploads use a dedicated table”POST /v1/uploads creates a row in uploads with a tenant-scoped storage key, SHA-256, MIME type,
byte count, and consumption timestamp. Capture submission resolves attachment references by SHA-256,
verifies the upload is pending and belongs to the tenant, and marks it consumed in the same D1 batch
as the capture.
A separate capture_attachments row then records the immutable evidence link. The plan’s table list
did not include uploads; adding it keeps pending evidence explicit and prevents reusing an upload
across captures.
Alternative considered: put pending metadata directly in capture_attachments with a nullable
capture id. Rejected because it makes the attachment table do two jobs and weakens the single-use
rule.
M1 reads use API keys; M2 adds reviewer sessions
Section titled “M1 reads use API keys; M2 adds reviewer sessions”The plan describes capture list/detail as session-authenticated reviewer endpoints. Reviewer sessions
arrive in M2, but M1’s acceptance criterion requires a submitted capture to be listed and read back.
M1 therefore exposes GET /v1/captures and GET /v1/captures/:id to the authenticated API key,
scoped to its tenant.
M2 adds magic-link sessions and reviewer authorization. The tenant-scoped repository methods stay the same; only the authentication middleware and route policy grow.
Alternative considered: defer public read endpoints and prove readback only through tests. Rejected because the milestone asks for an end-to-end stored/listed/read path.
Matched captures enter review; unmatched captures need a match
Section titled “Matched captures enter review; unmatched captures need a match”A capture that matches a parcel by id, exact map/lot, or exact address is stored as in_review.
A capture with no confident parcel match is stored as needs_match with a null parcel. The submission
audit event records the resolved status from null.
Alternative considered: persist every capture as received and resolve matching later. Rejected
because M1 has no background job, and the office needs to distinguish ready-to-review from
needs-match immediately.
Uploads are proxied through the Worker
Section titled “Uploads are proxied through the Worker”POST /v1/uploads accepts the binary body with a supported content type. R2 put runs in the Worker
after MIME, size, and SHA-256 validation. Originals are immutable for M1.
This keeps R2 credentials out of capture clients and works with the 25 MB limit. A presigned-upload path remains possible in M3 if Worker memory or client retry behavior justifies it.
Alternative considered: presigned R2 URLs. Rejected for the first slice because the Worker proxy is simpler to validate, hash, and test end to end.
Atomic submission writes use a D1 batch
Section titled “Atomic submission writes use a D1 batch”Drizzle owns the schema and generated migration. Normal reads use Drizzle. The submission write uses one D1 batch of prepared statements so the capture, attachment rows, audit event, and upload consumption either all commit or none do.
Alternative considered: separate inserts with compensating deletes. Rejected because D1 supports atomic batches and partial evidence would be harder to repair.
Consequences
Section titled “Consequences”- M1 has no admin API for creating towns, parcels, or API keys. Tests and local development seed those rows directly; M2 adds admin pages and reviewer sessions.
- API keys are the only M1 auth mechanism. A leaked key can read its tenant’s captures until revoked.
- Pending uploads and capture attachments both store storage keys; retention cleanup is a later M3 concern.
- The
uploadstable is an intentional addition to the plan’s data model and must be carried into any future Postgres adapter. - D1 batch SQL is confined to
apps/api/src/adapters/d1/repositories.ts; the core domain remains storage-agnostic.