diff --git a/doc/2026-11-01_jurisdiction_aware_user_store_design.md b/doc/2026-11-01_jurisdiction_aware_user_store_design.md index 16042d4948..01005651bc 100644 --- a/doc/2026-11-01_jurisdiction_aware_user_store_design.md +++ b/doc/2026-11-01_jurisdiction_aware_user_store_design.md @@ -1,4 +1,4 @@ -# Design: jurisdiction-aware hosted user stores +# Design: jurisdiction-aware hosted tenant stores **Status:** proposed, not implemented. Source: [#2283](https://github.com/developing-today/code/issues/2283), [investigation #2061](2026-10-29T11-43-02Z_investigation_do_jurisdiction_pinning/2026-10-29T11-43-02Z_investigation_do_jurisdiction_pinning.md) and [hosted Workers investigation #2044](2026-10-28T23-34-14Z_investigation_cloudflare_workers_fit/2026-10-28T23-34-14Z_investigation_cloudflare_workers_fit.md). The hosted store is a proposed architecture, not an existing UserDO implementation: a repository search found the `google:` ID derivation only in those investigations. Names and schema below are implementation contracts, not existing APIs. @@ -10,19 +10,19 @@ This design does not expose region selection or decide cross-jurisdiction shared ## 1–4. Authoritative record, routing and object guard -The shared directory is the authoritative control plane. A user row has a unique pseudonymous `subject_key`, immutable subject-to-account binding, `jurisdiction` (active), `jurisdiction_pending` (nullable target), `migration_id`, `phase`, and monotonically increasing `routing_epoch`. A transaction creates this row with the policy value `us` **before** any DO ID is derived or request is sent. Duplicate signup reads the existing row; it never overwrites it or resets the policy. Thereafter only the migration state machine may change `jurisdiction`, with a compare-and-swap on migration ID, old jurisdiction, phase and epoch. Normal profile updates cannot change it, even before first DO creation. Invalid, absent, unsupported, or unprovisioned values fail closed. Legacy unrestricted objects must be inventoried and migrated by a separately reviewed procedure, not relabelled `us` or silently backfilled. +The shared directory is the authoritative control plane. A tenant row has a unique pseudonymous `subject_key`, immutable subject-to-account binding, `jurisdiction` (active), `jurisdiction_pending` (nullable target), `migration_id`, `phase`, and monotonically increasing `routing_epoch`. A transaction creates this row with the policy value `us` **before** any DO ID is derived or request is sent. Duplicate signup reads the existing row; it never overwrites it or resets the policy. Thereafter only the migration state machine may change `jurisdiction`, with a compare-and-swap on migration ID, old jurisdiction, phase and epoch. Normal profile updates cannot change it, even before first DO creation. Invalid, absent, unsupported, or unprovisioned values fail closed. Legacy unrestricted objects must be inventoried and migrated by a separately reviewed procedure, not relabelled `us` or silently backfilled. -One routing module owns the raw binding and exports a function conceptually `userObjectId(bindingId: BindingId, jurisdiction: Jurisdiction)` with **two required arguments and no default**, plus a `userStub(bindingId, jurisdiction)` that obtains both ID and stub from the _same_ `env.USERS.jurisdiction(jurisdiction)` restricted namespace. `idFromName(bindingId)` is called only on that namespace, where `bindingId` is the stable `binding_id` minted once by the binding ceremony (32 lowercase hex chars; `pkgs/id/src/hosted_binding.rs`, #3808) — never an email and never a provider subject: the earlier `userObjectId(sub)` / `idFromName("google:" + sub)` sketch in this design predates the binding contract in #2258/#3811 and is superseded by it (reconciled by #5199). The directory row's `subject_key` remains a control-plane lookup key only; it is never a DO name. The module validates both values at runtime as well as statically; no subject-only overload, raw namespace access, unrestricted `get`, or fallback exists in application paths. Expose the raw binding only to this module and enforce this boundary by lint/static tests and code review. Each authenticated request resolves the authoritative row first and routes using its active jurisdiction and epoch; caches cannot authorize writes without the fencing protocol below. A missing jurisdiction must not silently produce a second, empty user store: unrestricted `idFromName` and restricted ID derivation produce different objects for the same name. A restricted namespace also rejects an ID from a different jurisdiction, unlike unrestricted `get`. +One routing module owns the raw binding and exports a function conceptually `userObjectId(bindingId: BindingId, jurisdiction: Jurisdiction)` with **two required arguments and no default**, plus a `userStub(bindingId, jurisdiction)` that obtains both ID and stub from the _same_ `env.USERS.jurisdiction(jurisdiction)` restricted namespace. `idFromName(bindingId)` is called only on that namespace, where `bindingId` is the stable `binding_id` minted once by the binding ceremony (32 lowercase hex chars; `pkgs/id/src/hosted_binding.rs`, #3808) — never an email and never a provider subject: the earlier `userObjectId(sub)` / `idFromName("google:" + sub)` sketch in this design predates the binding contract in #2258/#3811 and is superseded by it (reconciled by #5199). The directory row's `subject_key` remains a control-plane lookup key only; it is never a DO name. The module validates both values at runtime as well as statically; no subject-only overload, raw namespace access, unrestricted `get`, or fallback exists in application paths. Expose the raw binding only to this module and enforce this boundary by lint/static tests and code review. Each authenticated request resolves the authoritative row first and routes using its active jurisdiction and epoch; caches cannot authorize writes without the fencing protocol below. A missing jurisdiction must not silently produce a second, empty tenant store: unrestricted `idFromName` and restricted ID derivation produce different objects for the same name. A restricted namespace also rejects an ID from a different jurisdiction, unlike unrestricted `get`. Every identity-touching entry point—fetch, RPC, WebSocket connect/message/close handlers, alarms, initialization, import/export and administrative operations—runs a common guard **before reads, writes, external effects or alarm rescheduling**. It checks `ctx.id.jurisdiction` is defined, supported, and equals the expected jurisdiction from the authenticated, trusted routing context and, once initialized, the object's persisted immutable identity/jurisdiction. The route context includes account ID and epoch and is internal/authenticated, never client-controlled. Initialization is an explicit authorized operation, not automatic empty-store creation on arbitrary reads. Any mismatch or missing trusted context fails closed with a safe error and metric, without revealing identity. -`undefined` covers both unrestricted objects and alarms scheduled before 2026-03-15. Alarm handlers lack a fresh request context: they compare against persisted identity/jurisdiction and a fresh authoritative migration fence, otherwise stop without side effects. Never infer `us` from undefined. A separately authorized, known restricted object's fetch/RPC may reschedule a legacy alarm so the platform backfills jurisdiction; if it cannot be proven restricted, quarantine and investigate rather than process it. Identity-free health checks must not touch user state. Tests must exercise all paths, including unexpected `undefined`. +`undefined` covers both unrestricted objects and alarms scheduled before 2026-03-15. Alarm handlers lack a fresh request context: they compare against persisted identity/jurisdiction and a fresh authoritative migration fence, otherwise stop without side effects. Never infer `us` from undefined. A separately authorized, known restricted object's fetch/RPC may reschedule a legacy alarm so the platform backfills jurisdiction; if it cannot be proven restricted, quarantine and investigate rather than process it. Identity-free health checks must not touch tenant state. Tests must exercise all paths, including unexpected `undefined`. ## 5–6. Blob and directory boundaries Provision one immutable R2 bucket per supported jurisdiction. Bind each separately and repeat the jurisdiction, e.g. `r2_buckets: [{ binding: "BLOBS_US", bucket_name: "user-blobs-us", jurisdiction: "us" }]`; validate the exact syntax and account support during the canary. Pick the binding strictly from the authoritative jurisdiction; unknown or missing binding is an error. Content hash keys are stable, but deduplication and garbage collection are **within each bucket**, never across jurisdictions. Only the active object may authorize writes; migration copying has narrowly scoped credentials and explicit source/target. Do not assume a hash key itself proves the stored bytes are correct. -The single D1 directory has one fixed, explicitly configured and documented jurisdiction for all users; provisioning must verify its actual setting rather than infer it from a hint or deployment name. It stores only hashed/pseudonymous subject keys, public keys, public server records, and minimal routing/migration metadata, not graph, journal, blob bytes, or private profile content. A hashed subject remains potentially personal data, not anonymous. Document this shared directory as the **single shared durable-directory residency exception** when a user's jurisdiction differs, including access, retention and deletion policy. This is not a claim that the entire system has only one exception: KV caches, logs, request processing, shared worlds and vendor backup uncertainty also need disclosure. Store migration manifests and exports in jurisdiction-appropriate storage, not D1; D1 stores only opaque operation IDs and progress metadata. +The single D1 directory has one fixed, explicitly configured and documented jurisdiction for all tenants; provisioning must verify its actual setting rather than infer it from a hint or deployment name. It stores only hashed/pseudonymous subject keys, public keys, public server records, and minimal routing/migration metadata, not graph, journal, blob bytes, or private profile content. A hashed subject remains potentially personal data, not anonymous. Document this shared directory as the **single shared durable-directory residency exception** when a tenant's jurisdiction differs, including access, retention and deletion policy. This is not a claim that the entire system has only one exception: KV caches, logs, request processing, shared worlds and vendor backup uncertainty also need disclosure. Store migration manifests and exports in jurisdiction-appropriate storage, not D1; D1 stores only opaque operation IDs and progress metadata. ## 7. Migration protocol and single-account runbook @@ -37,7 +37,7 @@ Migration is an authenticated, auditable product state machine, not a script tha 3. Enumerate manifest blobs, copy into target jurisdiction bucket using approved transfer path, and verify each destination's bytes/hash and size even if a key already existed. Abort on missing or corrupt source or destination data. Create target via the target restricted namespace and authorized migration initialization, then idempotently import the snapshot under M. Avoid partial import becoming visible; persist progress and reject a different manifest for the same M. 4. Read back the target independently: compare manifest digest, complete graph/journal counts and canonical contents, all referenced blob bytes/hashes and sizes, schema version, and signature verification against the same public keys. Confirm target object's identity and jurisdiction, and that no writes occurred after source watermark. Any discrepancy leaves source frozen and authoritative; fix/retry or explicitly abort. 5. Atomically CAS directory `(source, pending target, M, verified, E+1)` to `(target, M, switched, E+2)`. The source remains permanently fenced for M. Activate target with E+2 after confirming that authoritative row; if the process crashes after CAS, resume activation from durable state. Requests during the gap return retryable maintenance. Invalidate routing caches and reconnect sessions/bridge using account identity, not a permanently cached DO ID. Verify authenticated reads and writes on target; signature bytes remain unchanged. This CAS is the linearization point, not the first copy. -6. Retain the frozen source for a defined recovery period and audit successful target read-back. Then delete source DO state via a fenced, idempotent cleanup operation, confirm its completion, and retire the migration record only after required retention. **Do not delete source R2 blobs as part of migration:** other users may reference the same content. A separate jurisdiction-local GC may delete only after a concurrency-safe reachability proof covering all accounts, in-flight writes, migrations and retention roots; if such a proof is unavailable, retain the blobs. Clean target orphan copies through the same safe GC. Track backup/PITR retention separately: logical deletion is not verified physical erasure. +6. Retain the frozen source for a defined recovery period and audit successful target read-back. Then delete source DO state via a fenced, idempotent cleanup operation, confirm its completion, and retire the migration record only after required retention. **Do not delete source R2 blobs as part of migration:** other tenants may reference the same content. A separate jurisdiction-local GC may delete only after a concurrency-safe reachability proof covering all accounts, in-flight writes, migrations and retention roots; if such a proof is unavailable, retain the blobs. Clean target orphan copies through the same safe GC. Track backup/PITR retention separately: logical deletion is not verified physical erasure. Before switch, abort means discard/quarantine target, CAS back to source with a _new_ epoch, and explicitly unfreeze source after confirming the directory; orphan blobs remain for safe GC. After switch, **never simply switch back or unfreeze source**, because target may have accepted new writes: recover forward, or perform a new fenced reverse migration including all target writes. On ambiguous CAS/timeout, reread authoritative state and resume from the recorded phase; never guess which side is active. A failed deletion is retried and alerted, not treated as a failed switch. diff --git a/pkgs/id/hosted-worker/README.md b/pkgs/id/hosted-worker/README.md index 160db9770f..f37eabc997 100644 --- a/pkgs/id/hosted-worker/README.md +++ b/pkgs/id/hosted-worker/README.md @@ -7,7 +7,7 @@ First local-only vertical slice of the hosted data plane (slice 3 of resolved context (`X-Binding-Id`, `X-Jurisdiction`, set by the authenticated internal caller; the binding ceremony itself lives in `pkgs/id/src/hosted_binding.rs`), strips client-supplied internal - headers, and forwards to the per-user Durable Object. + headers, and forwards to the per-tenant Durable Object. - `src/routing.ts` — the only module that touches the raw `USERS` namespace. Objects are named by the stable `binding_id` minted at binding time (32 lowercase hex chars) — never email, never a provider @@ -20,7 +20,7 @@ First local-only vertical slice of the hosted data plane (slice 3 of (binding_id, jurisdiction); a common guard runs before every read or write and fails closed on unrouted requests, identity mismatch, or a runtime-reported jurisdiction mismatch. Routes: `GET /v1/state`, - `PUT|GET /v1/kv/` (a minimal per-user store proving lazy + `PUT|GET /v1/kv/` (a minimal per-tenant store proving lazy creation and isolation; R2 blobs, metering and caps are later #3811 steps and are out of scope here). @@ -42,7 +42,7 @@ blocked pending Drew authorization). ## The signed-object bridge (#3818) The hosted half of the #2046 bridge: production HTTPS/WebSocket -push/pull between native ID nodes and this per-user store. The +push/pull between native ID nodes and this per-tenant store. The store is a sync peer — it holds signed objects and proof-carrying chain/fork evidence, assigns no order, re-signs nothing, and serves no authoritative snapshot. diff --git a/planning/architecture/README.md b/planning/architecture/README.md index 49af5ea134..4a3df975d3 100644 --- a/planning/architecture/README.md +++ b/planning/architecture/README.md @@ -37,7 +37,7 @@ records. - [Design: a Muse↔OpenClaw comms path that is not laptop SSH](../../doc/2026-10-29T12-09-49Z_design_openclaw_comms_path/2026-10-29T12-09-49Z_design_openclaw_comms_path.md) - [Refundable grants via pending transfers — premium tokens that survive a refund](../../doc/2026-10-29T12-10-00Z_design_refundable_grants_pending_transfers/2026-10-29T12-10-00Z_design_refundable_grants_pending_transfers.md) - [Design: currency as the centralised exception — the hosted edition's boundary to the currency service](../../doc/2026-10-29T12-20-00Z_design_currency_centralised_exception/2026-10-29T12-20-00Z_design_currency_centralised_exception.md) -- [Hosted tenancy architecture — per-user Turso databases + a central tenancy DB (component E of #9078)](../../doc/2026-11-17T16-55-00Z_design_hosted-tenancy-architecture/2026-11-17T16-55-00Z_design_hosted-tenancy-architecture.md) +- [Hosted tenancy architecture — per-tenant Turso databases + a central tenancy DB (component E of #9078)](../../doc/2026-11-17T16-55-00Z_design_hosted-tenancy-architecture/2026-11-17T16-55-00Z_design_hosted-tenancy-architecture.md) - [Currency control plane — design: registry, account factory, gateway](../../doc/2026-10-29T12-42-21Z_design_currency_control_plane/2026-10-29T12-42-21Z_design_currency_control_plane.md) - [Design: ID app packaging — app manifest, folder layout, TUI/web parity](../../doc/2026-10-29T12-50-36Z_design_id_app_packaging/2026-10-29T12-50-36Z_design_id_app_packaging.md) - [One `/id-sync/1` handshake, and why serve's range flights moved to](../../doc/2026-10-29T16-21-00Z_design_id_sync_alpn_split/2026-10-29T16-21-00Z_design_id_sync_alpn_split.md)