Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/test-suite-registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,10 @@
"pkgs/id/hosted-worker/test/usage.test.ts",
"pkgs/id/hosted-worker/test/currency.test.ts",
"pkgs/id/hosted-worker/test/front-door.test.ts",
"pkgs/id/hosted-worker/test/id-uri-vectors.test.ts"
"pkgs/id/hosted-worker/test/id-uri-vectors.test.ts",
"pkgs/id/hosted-worker/test/tenancy-schema.test.ts"
],
"invocation": "cd pkgs/id/hosted-worker && npm test (node --experimental-transform-types --test test/routing.test.ts test/blobs.test.ts test/canonical-cbor.test.ts test/usage.test.ts test/currency.test.ts test/front-door.test.ts test/id-uri-vectors.test.ts)",
"invocation": "cd pkgs/id/hosted-worker && npm test (node --experimental-transform-types --test test/routing.test.ts test/blobs.test.ts test/canonical-cbor.test.ts test/usage.test.ts test/currency.test.ts test/front-door.test.ts test/id-uri-vectors.test.ts test/tenancy-schema.test.ts)",
"venue": {
"workflow": ".github/workflows/id-hosted-worker-tests.yml",
"job_step": "hosted-worker-tests / Run the hosted-worker routing suite",
Expand Down
10 changes: 10 additions & 0 deletions pkgs/id/hosted-worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ First local-only vertical slice of the hosted data plane (slice 3 of
creation and isolation; R2 blobs, metering and caps are later
#3811 steps and are out of scope here).

## Tenancy control plane (component E, #9083)

`tenancy/` holds the central tenancy DB schema — the control plane
above this data plane: account registry, binding pointers,
tenant/database registry, role grants, peer policy, publish records
and credential metadata, as numbered migrations. It holds no user
content, no keys and no token values (design §2.3, "never central");
`test/tenancy-schema.test.ts` fails if a content/key/token column
ever appears. See `tenancy/README.md`.

## Local reality check (verified 2026-11-03, wrangler dev / workerd)

Local workerd does **not** implement jurisdiction restrictions:
Expand Down
2 changes: 1 addition & 1 deletion pkgs/id/hosted-worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"dev": "wrangler dev",
"typecheck": "tsc --noEmit",
"test": "node --experimental-transform-types --test test/routing.test.ts test/provisioning.test.ts test/blobs.test.ts test/canonical-cbor.test.ts test/usage.test.ts test/currency.test.ts test/front-door.test.ts test/id-uri-vectors.test.ts test/bridge-blake3.test.ts test/bridge-event.test.ts test/bridge-range.test.ts test/bridge-store.test.ts test/bridge-session.test.ts test/bridge-do.test.ts",
"test": "node --experimental-transform-types --test test/routing.test.ts test/provisioning.test.ts test/blobs.test.ts test/canonical-cbor.test.ts test/usage.test.ts test/currency.test.ts test/front-door.test.ts test/id-uri-vectors.test.ts test/bridge-blake3.test.ts test/bridge-event.test.ts test/bridge-range.test.ts test/bridge-store.test.ts test/bridge-session.test.ts test/bridge-do.test.ts test/tenancy-schema.test.ts",
"smoke": "node smoke/smoke.mjs"
},
"devDependencies": {
Expand Down
49 changes: 49 additions & 0 deletions pkgs/id/hosted-worker/tenancy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Tenancy control plane (component E)

The central tenancy database: one Turso/SQLite database holding the
account registry, binding pointers, the tenant/database registry,
role grants, peer policy, publish records and credential metadata —
and nothing else. Design record:
`doc/2026-11-17T16-55-00Z_design_hosted-tenancy-architecture`
(component E of epic #9078, issue #9083).

This directory is the schema home for that database. The tenant
**data** plane (per-user databases and the `UserDO` that serves them)
is `../src/`; this is the control plane above it, queried by tenancy
Workers and the `TenancyOpsDO` of later E slices.

## The invariant (design §2.3, "never central")

The tenancy DB holds **no user content, no keys, and no token
values**. Tokens are metadata only: the `credential` table records
scope, expiry and a `jti` revocation handle, never the token. The
public root key appears as `iroh_id` and nothing more. A tenancy-DB
dump must not yield content or database access.

`../test/tenancy-schema.test.ts` enforces this: it applies the
migrations and fails if any content/key/token column appears, if the
schema drifts from the §2.1 baseline, or if a participant kind gets
special-cased.

## Peer principle

Hosted, Rust-binary and self-hosted nodes are the same participant
kinds. `tenant_db.participant_kind`
(`user_hosted | user_native | self_hosted_node | public`) is
configuration on the shared registry row; there are no hosted-only
tables and the sync/publish protocols never branch on "hosted".

## Migrations

Numbered, append-only SQL files in `migrations/`, applied in order.
`0001_tenancy_baseline.sql` is the §2.1 baseline (E1, #12796); later
E slices (E2 provisioning, E4 credentials, E5 publish) add migrations
here rather than editing landed ones. `meta.schema_version` records
the applied baseline, mirroring the `UserDO` bootstrap pattern in
`../src/user-do.ts`.

Two deliberate statements of the design's rules over its sketch DDL
are documented in the baseline's header: the active-binding rule is
a partial unique index (not the sketch's table-level UNIQUE, which
would break the rebind chain), and the vocabularies the record names
as closed carry CHECK constraints.
156 changes: 156 additions & 0 deletions pkgs/id/hosted-worker/tenancy/migrations/0001_tenancy_baseline.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
-- 0001_tenancy_baseline.sql
--
-- Central tenancy DB baseline (component E1 of #9083, issue #12796).
--
-- Implements section 2.1 of the hosted-tenancy design record
-- (doc/2026-11-17T16-55-00Z_design_hosted-tenancy-architecture) as the
-- first migration of the central tenancy database: one Turso/SQLite
-- database holding the account registry, the binding pointers, the
-- tenant/database registry, role grants, peer policy, publish records
-- and credential metadata.
--
-- THE INVARIANT (design section 2.3, "never central"): this schema
-- holds NO user content, NO keys and NO token values. Tokens are
-- metadata only (the `credential` table records a `jti` revocation
-- handle, never the token). A tenancy-DB dump must not yield content
-- or database access. test/tenancy-schema.test.ts fails if a
-- content/key/token column ever appears in this schema.
--
-- Peer principle (design sections 0-1, 9): hosted, Rust-binary and
-- self-hosted nodes are the same participant kinds. `tenant_db`
-- carries one `participant_kind` vocabulary for all of them; there
-- are no hosted-only tables and nothing in this schema special-cases
-- a hosted tenant.
--
-- Two places where this migration states the design's rule rather
-- than its sketch DDL verbatim (the record calls the DDL "a sketch
-- for the implementation issues, not a migration"):
--
-- 1. `binding`: the sketch's UNIQUE (account_id, status) would cap
-- an account at one superseded binding ever, which breaks the
-- rebind chain the same sketch defines via `superseded_by`. The
-- sketch's stated rule is "at most one active row per account",
-- so that rule is enforced with a partial unique index over
-- active rows only.
-- 2. Closed vocabularies the record names as closed get CHECK
-- constraints: `tenant_db.participant_kind`,
-- `tenant_db.jurisdiction` ("closed vocabulary" in the sketch),
-- `peer_policy.class` (the #9106/B31 classes) and
-- `credential.scope` ("(closed)" in the sketch).
--
-- `meta` mirrors the bootstrap pattern of the tenant data plane
-- (src/user-do.ts): a name/value table carrying `schema_version`.
-- (Its columns are name/value, not key/value, on purpose: the
-- never-central guard bans any column with "key" in its name, with
-- no exceptions to argue about.)
-- Later E slices add numbered migrations; this file never changes.

CREATE TABLE meta (
name TEXT PRIMARY KEY,
value TEXT NOT NULL
);

INSERT INTO meta (name, value) VALUES ('schema_version', '1');

-- One row per identity that can own things. Aligned with #2258 s.1.1.
-- sub is the identity provider's stable subject; email is contact-only.
CREATE TABLE account (
account_id TEXT PRIMARY KEY, -- opaque, immutable, service-minted
sub TEXT NOT NULL UNIQUE, -- #9077 provider subject; never an email
email_contact TEXT, -- notifications only; never a key
created_at TEXT NOT NULL,
state TEXT NOT NULL, -- active | suspended | closed
tier TEXT NOT NULL, -- #2046 parameter
meter_rollup TEXT, -- latest daily rollup (content-free)
CHECK (state IN ('active','suspended','closed'))
);

-- The stable pointer from account to identity and to the tenant. #2258 s.1.2
-- is the authority on the ceremony; this table stores the pointer + status,
-- not the ceremony transcript (that stays in the binding record store).
CREATE TABLE binding (
binding_id TEXT PRIMARY KEY, -- 32 lowercase hex; names the DO and the tenant
account_id TEXT NOT NULL REFERENCES account(account_id),
iroh_id TEXT NOT NULL, -- current root key; public key only
status TEXT NOT NULL, -- active | rebind_pending | superseded
bound_at TEXT NOT NULL,
superseded_by TEXT REFERENCES binding(binding_id),
CHECK (status IN ('active','rebind_pending','superseded'))
);

-- At most one active binding per account (design note 1 above).
CREATE UNIQUE INDEX binding_one_active_per_account
ON binding (account_id) WHERE status = 'active';

-- The registry. One row per data plane a tenant owns (private tenant DB,
-- later maybe a second DB); the public DB is a tenant too, see s.7.
-- participant_kind is the peer principle in one column: a hosted user,
-- a native (Rust-binary) user, a self-hosted node and the public plane
-- are the same kind of participant, differing only in backend.
CREATE TABLE tenant_db (
tenant_id TEXT PRIMARY KEY, -- = binding_id for user tenants
participant_kind TEXT NOT NULL, -- user_hosted | user_native | self_hosted_node | public
turso_db_id TEXT, -- provider handle; NULL until provisioned
turso_db_url TEXT, -- https://<db>.turso.io; public endpoint only
jurisdiction TEXT NOT NULL, -- us | eu | fedramp (closed vocabulary)
backend TEXT NOT NULL, -- turso | redb | k8s_tiered (capability, s.9)
state TEXT NOT NULL, -- see s.5.1 of the design (provisioning lifecycle)
schema_version INTEGER NOT NULL,
created_at TEXT NOT NULL,
deleted_at TEXT,
CHECK (participant_kind IN ('user_hosted','user_native','self_hosted_node','public')),
CHECK (jurisdiction IN ('us','eu','fedramp')),
CHECK (state IN ('requested','db_creating','db_ready','active',
'suspended','export_only','deleting','deleted','failed'))
);

-- Roles are #1253's vocabulary, not a new one. Scope is a tenant or '*'.
CREATE TABLE role_grant (
binding_id TEXT NOT NULL REFERENCES binding(binding_id),
role TEXT NOT NULL, -- #1253 role name
scope TEXT NOT NULL, -- tenant_id or '*'
granted_at TEXT NOT NULL,
granted_by TEXT NOT NULL,
revoked_at TEXT,
PRIMARY KEY (binding_id, role, scope)
);

-- Peer configuration in the #9106/B31 shape, not a parallel trust model (s.9).
CREATE TABLE peer_policy (
peer_id TEXT NOT NULL,
tenant_id TEXT NOT NULL, -- whose tenancy this governs
class TEXT NOT NULL, -- fully_trusted | send_only | accept_signed_from | unknown
operations TEXT NOT NULL, -- JSON array: accept_data | send_data | ...
relay_granted INTEGER NOT NULL DEFAULT 0,
scope TEXT NOT NULL,
policy_version INTEGER NOT NULL,
valid_from TEXT,
expires_at TEXT,
PRIMARY KEY (peer_id, tenant_id, policy_version),
CHECK (class IN ('fully_trusted','send_only','accept_signed_from','unknown'))
);

-- Publish records: what a tenant has offered to the public plane (s.7).
CREATE TABLE publish_record (
publish_id TEXT PRIMARY KEY,
tenant_id TEXT NOT NULL,
content_hash TEXT NOT NULL, -- content address; the row is the only grant
signed_artifact TEXT NOT NULL, -- the signed bytes live in the public DB; this is the digest + envelope id
Comment on lines +137 to +138

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve the publish-record kind in the baseline schema

The §2.1 design schema includes a required publish_record.kind column (document | note | tag | artifact), but this migration drops it without documenting a deviation. Consequently, the baseline cannot record the published object's kind as specified. The new EXPECTED_SCHEMA also omits this field, so the claimed column-for-column conformance test incorrectly passes. Add the discriminator to the migration and its expected schema.

Useful? React with 👍 / 👎.

published_at TEXT NOT NULL,
retracted_at TEXT,
policy_version INTEGER NOT NULL
);

-- Credential metadata only. Tokens are NEVER stored here (s.8).
CREATE TABLE credential (
credential_id TEXT PRIMARY KEY,
tenant_id TEXT NOT NULL,
audience TEXT NOT NULL, -- turso_db_id
scope TEXT NOT NULL, -- read | readwrite | ddl (closed)
issued_at TEXT NOT NULL,
expires_at TEXT NOT NULL,
revoked_at TEXT,
issuer TEXT NOT NULL, -- which Worker/DO issued it
jti TEXT NOT NULL UNIQUE, -- revocation handle; not the token
CHECK (scope IN ('read','readwrite','ddl'))
);
Loading