Skip to content
Merged
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
2 changes: 1 addition & 1 deletion pkgs/id/book/concepts/hosted.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Any other path or method returns 404 with `{"error":"not_found"}`. Bridge routes

Read the table as a data plane, because that is what it is. The hosted object holds one bound user's KV, blob references, and a signed-object bridge store. As a sync peer it holds signed objects and fork evidence; it assigns no order, re-signs nothing, and serves no authoritative snapshot. Ordering and authority stay with the authors' chains, exactly as on a native node.

Also in the package, but not routed by `worker.ts`, are `src/provisioning.ts` and `provisioning-surface.ts` (a provisioning surface), `src/account-directory.ts`, `src/admission-gate.ts`, `src/export.ts`, and a currency gateway proof of concept under `src/currency/` (`gateway`, `ledger`, `relay`, `crypto`; #2298). The currency proof of concept is explicitly not the permanent currency service: it is an in-process ledger stub modelled on TigerBeetle, with gateway-signed balance snapshots and a UserDO relay reconciling by request-ID lookup. [Currency, tokens and metering](currency.md) covers it in place.
Also in the package, but not routed by `worker.ts`, are `src/provisioning.ts` and `provisioning-surface.ts` (a provisioning surface), `src/account-directory.ts`, `src/admission-gate.ts`, `src/export.ts`, `src/tenant-ops.ts` (the `TenantOpsDO` provisioning state machine of the tenancy design §4 — exported and bound as `TENANT_OPS`, but driven by the tenancy control plane, not the front door), and a currency gateway proof of concept under `src/currency/` (`gateway`, `ledger`, `relay`, `crypto`; #2298). The currency proof of concept is explicitly not the permanent currency service: it is an in-process ledger stub modelled on TigerBeetle, with gateway-signed balance snapshots and a UserDO relay reconciling by request-ID lookup. [Currency, tokens and metering](currency.md) covers it in place.

## What it does not yet do

Expand Down
22 changes: 21 additions & 1 deletion pkgs/id/book/reference/hosted-worker.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The hosted Worker API

Source: https://github.com/developing-today/code/blob/main/pkgs/id/hosted-worker/src/worker.ts, https://github.com/developing-today/code/blob/main/pkgs/id/hosted-worker/src/user-do.ts, https://github.com/developing-today/code/blob/main/pkgs/id/hosted-worker/wrangler.toml, https://github.com/developing-today/code/blob/main/pkgs/id/worker-poc/README.md
Source: https://github.com/developing-today/code/blob/main/pkgs/id/hosted-worker/src/worker.ts, https://github.com/developing-today/code/blob/main/pkgs/id/hosted-worker/src/user-do.ts, https://github.com/developing-today/code/blob/main/pkgs/id/hosted-worker/src/tenant-ops.ts, https://github.com/developing-today/code/blob/main/pkgs/id/hosted-worker/wrangler.toml, https://github.com/developing-today/code/blob/main/pkgs/id/worker-poc/README.md

This chapter documents the hosted Worker **as it stands**. Two packages exist, and neither is a live service:

Expand Down Expand Up @@ -55,6 +55,7 @@ From `wrangler.toml`: Worker name `id-hosted-worker`, `compatibility_date` `2025
| Binding / var | Type | Notes |
| --------------------------- | ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `USERS` | Durable Object namespace → `UserDO` | Migration tag `v1`, `new_sqlite_classes = ["UserDO"]`. |
| `TENANT_OPS` | Durable Object namespace → `TenantOpsDO` | Migration tag `v2`, `new_sqlite_classes = ["TenantOpsDO"]`. One object per tenant, named by `binding_id`. Exported by `worker.ts` but never routed by the front door; the tenancy Worker drives it. |
| `BLOBS` | R2 bucket `id-hosted-blobs` | Content-addressed blob bytes. Local emulation only. In the `UserDO` interface the binding is optional, and the code that needs it fails closed without it. |
| `USER_CEILING_BYTES` | String var, optional | Per-user storage ceiling. Absent or empty means no ceiling. A malformed value fails writes closed. Also read by the front door for the body limit: `MAX_BODY_BYTES` is a 100 MiB cap. |
| `BRIDGE_WAITING_PER_AUTHOR` | String var, optional | Bridge waiting-pool bound, per author. Absent means the native default in `bridge-store.ts`. A malformed value fails bridge writes closed. |
Expand Down Expand Up @@ -101,6 +102,24 @@ Failure shapes:
- A KV key outside `[A-Za-z0-9._-]{1,128}` is not a valid route key.
- A blob path whose segment is not 64 hex characters does not name a blob.

## TenantOpsDO routes

Implementation: `src/tenant-ops.ts` (E2 of the hosted-tenancy component, #12797), implementing the provisioning lifecycle of the tenancy design record §4 over the central tenancy DB's `tenant_db` row (schema baseline in `pkgs/id/hosted-worker/tenancy/`). One object per tenant, named by `binding_id`. The front door does not route to it; the caller is the tenancy Worker, which sets the actor on internal headers (`x-tenancy-actor-role`, `x-tenancy-actor-id`, `x-tenancy-trigger`) after authentication, and every step refuses an actor §4 does not name for it. All routes are POST except the state read.

| Method | Path | Who (§4) | Effect |
| ------ | ------------------------------- | ------------------------------------- | ---------------------------------------------------------------------- |
| POST | `/v1/tenant/request` | owner (the bound binding) | Record the row `requested` with its provisioning intent; a missing or inactive binding fails validation on the row (`failed`). Idempotent. |
| POST | `/v1/tenant/provision` | platform (the object itself) | `requested` → `db_creating` → `db_ready`: create the tenant database through the provider seam under the deterministic request id; retries adopt the database the intent already made instead of creating a second one. |
| POST | `/v1/tenant/bootstrap-observed` | platform | Record that the TenantDO's schema bootstrap has been observed; activation is refused until this exists. |
| POST | `/v1/tenant/activate` | platform | `db_ready` → `active`. |
| POST | `/v1/tenant/suspend` | operator, or the abuse/ceiling trigger | `active` → `suspended`. Reversible; data untouched. |
| POST | `/v1/tenant/resume` | operator | `suspended` → `active`. |
| POST | `/v1/tenant/export-only` | owner, operator, or the ceiling trigger | `active`/`suspended` → `export_only`: reads and export stay, writes refuse. |
| POST | `/v1/tenant/delete` | owner (initiate); owner or platform (complete) | Two-phase: initiation (from `export_only` only) revokes all credential metadata and opens the cooling-off window (`deleting`); completion after the window drops the database and writes the tombstone (`deleted`, row retained). |
| GET | `/v1/tenant/state` | owner, operator, platform | The registry row. Answers in every state — suspension must not look like loss. |

Failure shapes: no or unknown actor → `401`; an actor §4 does not name for the step (including an owner naming a different binding) → `403 actor_not_permitted`; a step attempted from a state §4 does not allow → `409 invalid_transition`; activation before observed bootstrap → `409 bootstrap_not_observed`; deletion inside the cooling-off window → `409 cooling_off`. With no database provider configured, provisioning and drops fail closed with `503 provider_unavailable` and the row does not advance. Credential issuance is a later slice (E4); this surface mints nothing and only revokes, as deletion's first phase.

### The README route list is a first-slice fossil (WEB-9)

The `hosted-worker` README describes the UserDO routes as `GET /v1/state` and `PUT|GET /v1/kv/<key>`, calls the KV store minimal, and states that R2 blobs, metering and caps are later steps and out of scope. The code implements blobs (PUT, GET, DELETE), usage and metering, export, KV DELETE, and the full bridge. The README's later sections cover the bridge and export but never revise that paragraph, and blobs, usage and KV-delete appear in no README route list. That is disagreement WEB-9; the table above is the implemented surface.
Expand All @@ -112,6 +131,7 @@ These modules exist in the package but are not routed by `worker.ts`:
| Module | What it is |
| ---------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `src/provisioning.ts`, `src/provisioning-surface.ts` | The provisioning surface. |
| `src/tenant-ops.ts` | `TenantOpsDO`, the tenancy provisioning state machine (see TenantOpsDO routes above). Exported and bound, but driven by the tenancy Worker, not the front door. |
| `src/account-directory.ts` | Account directory. |
| `src/admission-gate.ts` | Admission gate. |
| `src/export.ts` | Export support behind `/v1/bridge/export`. |
Expand Down
14 changes: 14 additions & 0 deletions pkgs/id/hosted-worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ 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`.

- `src/tenant-ops.ts` — `TenantOpsDO` (E2 of #9083, #12797): one
object per tenant, named by `binding_id`, implementing the design
§4 provisioning state machine over the `tenant_db` row —
`requested → db_creating → db_ready → active`, suspension,
export-only, and two-phase deletion (credential revocation,
cooling-off window, drop, tombstone). Creation is intent-first
under a deterministic request id, so concurrent sign-ups create
exactly one database; every step checks the actor §4 names for
it. Bound as `TENANT_OPS` in `wrangler.toml` but not routed by
the front door — the tenancy Worker drives it. The Turso
management API sits behind a provider seam; unconfigured, it
fails closed. `test/tenant-ops.test.ts` exercises the machine
against the real tenancy migrations.

## 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/admission-gate.test.ts test/provisioning.test.ts test/provisioning-surface.test.ts test/enablement.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 test/export.test.ts test/routing-boundary.test.ts test/migration-control.test.ts",
"test": "node --experimental-transform-types --test test/routing.test.ts test/admission-gate.test.ts test/provisioning.test.ts test/provisioning-surface.test.ts test/enablement.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 test/tenant-ops.test.ts test/export.test.ts test/routing-boundary.test.ts test/migration-control.test.ts",
"smoke": "node smoke/smoke.mjs"
},
"devDependencies": {
Expand Down
Loading