Duet backend
duet-api owns Duet’s Rust service. It has identity,
evidence, doctor workspace, health-context and privacy owners. A module’s presence
does not establish the complete real-service journey.
Ownership
| Responsibility | Source owner |
|---|---|
| Startup, configured stores and hosted profile | main.rs, hosting.rs |
| Duet accounts and authentication | identity.rs, session.rs; security boundaries |
| Evidence identity, ingestion authority, exact reads and paging | evidence.rs, evidence_entity.rs, evidence_routes.rs, evidence_paging.rs |
| Health context and producer grants | health_context.rs, producer_routes.rs, producer_database.rs |
| For my doctor preparation | workspace.rs, doctor_routes.rs |
| Privacy receipts, replay and deletion archive | Privacy recovery |
| Schema and migrations | database.rs, migrations/ |
| Practice invitations and connection adapter | Accounts and practice connection |
Canonical evidence ingestion
EvidenceStore is the single mutation owner for canonical health evidence and
clinical notes. It applies single and batch mutations under an account-locked
transaction. The HTTP routes POST /api/canonical-evidence and
POST /api/canonical-evidence/batch always write with device authority. Device
authority may not create, correct, revive or remove the originals of the
server-owned portal sources (epic, oracleHealth, healow, googleHealth).
Those originals can be written only in process, through
ingest_with_authority and a matching connector source kind and connection.
Such refusals return the stable conflict code source_authority_refused. A
record’s source kind, connection and note label are fixed when it is created.
- A batch holds 1–100 items, each a bounded source object up to 2 MB. Each item
runs in its own savepoint and gets an
acceptedor typedrejectedoutcome, so no item is skipped silently. An infrastructure failure aborts the whole batch. - Retrying an operation returns its first acknowledged address. A new operation identity must name the exact current revision.
measured_atorrecorded_atis stored only from an RFC 3339 instant the source declared. Otherwise the stored date is unknown (NULL); receipt time is never used, and the literal stays in the payload. Patient-authored notes (Added in Duet) must carry an explicit instant.operation: "delete"is an explicit removal by the owning source, fenced by exact revision and source generation. It keeps the base row and retained revisions, marks membershipdeletedandsource_removed, and leaves earlier revisions readable as non-current citations. Privacy erasure (privacy recovery) deletes base rows and their retained revisions, and remains authoritative.
The Duet spec (D4) owns For my doctor and what it sends to a connected practice.
Duet must not import the physician local-product; the
boundaries tests define this boundary.
API and clients
duet-client adds Duet client behavior over
duet-wire. Generate wire types with
cargo run -p workspace-tools -- generate-duet-wire; additive Rust operations
live in openapi/additions.json.
workspace-tools::wire orchestrates the
composer, OpenAPI Generator and
finalizer; their compatibility templates live
beside those scripts. The generated API index
and model/operation pages are regenerated with the Rust sources.
cargo run -p workspace-tools -- generate-duet-wire --check compares sources,
the manifest and documentation against fresh output. Use the pinned toolchain
in nix develop.
The generator consumes the pinned OpenAPI source. The composer excludes the
retired /api/sharing/ person-to-person routes and schemas used only by them.
Practice-connection and doctor-workspace contracts keep their own owners.
Regeneration clears generated source and documentation directories so removed
operations cannot survive as stale files.
Persistence and migrations
The API never owns schema migration at startup. Run
duet-migrate with
DUET_MIGRATION_DATABASE_URL and DUET_RUNTIME_ROLE. The migration role and
pre-existing runtime login must be distinct; the runtime role must not be a
superuser, bypass RLS, create roles/databases, or inherit the migration role.
The binary applies the Duet-owned chain first and optionally the front-door
chain when FRONT_DOOR_DATABASE_URL is supplied. Do not replay the coda-app
migration ledger to initialize this service.
Queries go through the typed database API, as AGENTS.md asks.
Schema/account transactions use SQLx. src/sql/has_migration.sql is a
schema-readiness query, not a CRUD pattern, and duet-migrate keeps the separate
reference schema_migrations ledger. Ordinary CRUD belongs to the owning
product’s typed entities; products do not share stores.
Proof
Release and GCP explain the build and hosted proof
boundaries. Default tests may skip database-gated checks. The
check-postgres.sh suites require a disposable
PostgreSQL instance with pgvector and all five script URLs
(MIGRATION_DATABASE_URL, PHYSICIAN_DATABASE_URL, FRONT_DOOR_DATABASE_URL,
DUET_IDENTITY_TEST_DATABASE_URL, PHYSICIAN_RUNTIME_TEST_DATABASE_URL).
They create/drop databases. The Duet owner suites, such as fresh_producers,
public_budget and evidence_cohort, run against DUET_TEST_DATABASE_URL.
These suites and image smoke tests establish their
exercised persistence/startup paths, not sign-in, installed-client or clinical
qualification.