diff --git a/.github/test-suite-registry.json b/.github/test-suite-registry.json index f98e15844..6286fa110 100644 --- a/.github/test-suite-registry.json +++ b/.github/test-suite-registry.json @@ -268,14 +268,22 @@ "id": "id.hosted-worker.routing", "paths": [ "pkgs/id/hosted-worker/test/routing.test.ts", + "pkgs/id/hosted-worker/test/provisioning.test.ts", "pkgs/id/hosted-worker/test/blobs.test.ts", "pkgs/id/hosted-worker/test/canonical-cbor.test.ts", "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/bridge-blake3.test.ts", + "pkgs/id/hosted-worker/test/bridge-event.test.ts", + "pkgs/id/hosted-worker/test/bridge-range.test.ts", + "pkgs/id/hosted-worker/test/bridge-store.test.ts", + "pkgs/id/hosted-worker/test/bridge-session.test.ts", + "pkgs/id/hosted-worker/test/bridge-do.test.ts", + "pkgs/id/hosted-worker/test/migration-control.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/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/migration-control.test.ts)", "venue": { "workflow": ".github/workflows/id-hosted-worker-tests.yml", "job_step": "hosted-worker-tests / Run the hosted-worker routing suite", diff --git a/pkgs/id/hosted-worker/package.json b/pkgs/id/hosted-worker/package.json index f4b6c4287..fa7220afe 100644 --- a/pkgs/id/hosted-worker/package.json +++ b/pkgs/id/hosted-worker/package.json @@ -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/migration-control.test.ts", "smoke": "node smoke/smoke.mjs" }, "devDependencies": { diff --git a/pkgs/id/hosted-worker/src/migration-control.ts b/pkgs/id/hosted-worker/src/migration-control.ts new file mode 100644 index 000000000..623a4b428 --- /dev/null +++ b/pkgs/id/hosted-worker/src/migration-control.ts @@ -0,0 +1,134 @@ +export type Jurisdiction = "us" | "eu"; + +export interface DirectoryRoute { + activeJurisdiction: Jurisdiction; + routingEpoch: number; +} + +export interface MigrationState { + migrationId: string; + sourceJurisdiction: Jurisdiction; + targetJurisdiction: Jurisdiction; + activeJurisdiction: Jurisdiction; + routingEpoch: number; + phase: "pending" | "frozen" | "verified" | "switched" | "active" | "aborted"; + sourceFrozen: boolean; + targetServing: boolean; +} + +export type MigrationEvent = + | { type: "freeze-acknowledged" } + | { type: "snapshot-verified" } + | { type: "switch" } + | { type: "activate-target" } + | { type: "abort" }; + +export class MigrationTransitionError extends Error { + constructor(message: string) { + super(message); + this.name = "MigrationTransitionError"; + } +} + +export function beginMigration( + directory: DirectoryRoute, + request: { migrationId: string; targetJurisdiction: Jurisdiction }, +): MigrationState { + if (!request.migrationId) throw new MigrationTransitionError("migration ID must not be empty"); + if (directory.activeJurisdiction === request.targetJurisdiction) { + throw new MigrationTransitionError("target jurisdiction must differ from source"); + } + if (!Number.isSafeInteger(directory.routingEpoch) || directory.routingEpoch < 0) { + throw new MigrationTransitionError("routing epoch must be a non-negative safe integer"); + } + if (directory.routingEpoch === Number.MAX_SAFE_INTEGER) { + throw new MigrationTransitionError("routing epoch cannot advance safely"); + } + + return { + migrationId: request.migrationId, + sourceJurisdiction: directory.activeJurisdiction, + targetJurisdiction: request.targetJurisdiction, + activeJurisdiction: directory.activeJurisdiction, + routingEpoch: directory.routingEpoch + 1, + phase: "pending", + sourceFrozen: false, + targetServing: false, + }; +} + +function assertState(state: MigrationState): void { + if (!state.migrationId || state.sourceJurisdiction === state.targetJurisdiction) { + throw new MigrationTransitionError("invalid migration identity"); + } + if (!Number.isSafeInteger(state.routingEpoch) || state.routingEpoch < 1) { + throw new MigrationTransitionError("routing epoch must be a positive safe integer"); + } + + const expected: Record = { + pending: [state.sourceJurisdiction, false, false], + frozen: [state.sourceJurisdiction, true, false], + verified: [state.sourceJurisdiction, true, false], + switched: [state.targetJurisdiction, true, false], + active: [state.targetJurisdiction, true, true], + aborted: [state.sourceJurisdiction, false, false], + }; + const [active, frozen, serving] = expected[state.phase]; + if ( + state.activeJurisdiction !== active || + state.sourceFrozen !== frozen || + state.targetServing !== serving + ) { + throw new MigrationTransitionError(`state invariants do not hold for phase ${state.phase}`); + } +} + +export function transitionMigration(state: MigrationState, event: MigrationEvent): MigrationState { + assertState(state); + + switch (event.type) { + case "freeze-acknowledged": + if (state.phase !== "pending") { + throw new MigrationTransitionError(`cannot acknowledge freeze during ${state.phase}`); + } + return { ...state, phase: "frozen", sourceFrozen: true }; + case "snapshot-verified": + if (state.phase !== "frozen") { + throw new MigrationTransitionError(`cannot verify snapshot during ${state.phase}`); + } + return { ...state, phase: "verified" }; + case "switch": + if (state.phase !== "verified") { + throw new MigrationTransitionError(`cannot switch during ${state.phase}`); + } + if (state.routingEpoch === Number.MAX_SAFE_INTEGER) { + throw new MigrationTransitionError("routing epoch cannot advance safely"); + } + return { + ...state, + activeJurisdiction: state.targetJurisdiction, + routingEpoch: state.routingEpoch + 1, + phase: "switched", + }; + case "activate-target": + if (state.phase !== "switched") { + throw new MigrationTransitionError(`cannot activate target during ${state.phase}`); + } + return { ...state, phase: "active", targetServing: true }; + case "abort": + if (state.phase !== "pending" && state.phase !== "frozen" && state.phase !== "verified") { + throw new MigrationTransitionError("cannot abort after switch"); + } + if (state.routingEpoch === Number.MAX_SAFE_INTEGER) { + throw new MigrationTransitionError("routing epoch cannot advance safely"); + } + return { + ...state, + activeJurisdiction: state.sourceJurisdiction, + routingEpoch: state.routingEpoch + 1, + phase: "aborted", + sourceFrozen: false, + targetServing: false, + }; + } +} diff --git a/pkgs/id/hosted-worker/test/migration-control.test.ts b/pkgs/id/hosted-worker/test/migration-control.test.ts new file mode 100644 index 000000000..7be710b98 --- /dev/null +++ b/pkgs/id/hosted-worker/test/migration-control.test.ts @@ -0,0 +1,171 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { + beginMigration, + MigrationTransitionError, + transitionMigration, + type MigrationEvent, +} from "../src/migration-control.ts"; + +// Oracle: the transition model in doc/2026-11-01_jurisdiction_aware_user_store_design.md. +// This first slice is deliberately pure: it cannot enable production migration or +// bypass the directory, restricted-routing, storage, or hosted-write prerequisites. + +test("a new migration keeps the source active and advances the routing epoch", () => { + const state = beginMigration( + { activeJurisdiction: "us", routingEpoch: 7 }, + { migrationId: "m-1", targetJurisdiction: "eu" }, + ); + + assert.deepEqual(state, { + migrationId: "m-1", + sourceJurisdiction: "us", + targetJurisdiction: "eu", + activeJurisdiction: "us", + routingEpoch: 8, + phase: "pending", + sourceFrozen: false, + targetServing: false, + }); +}); + +test("target stays non-serving until switch and activation follows switch", () => { + let state = beginMigration( + { activeJurisdiction: "us", routingEpoch: 0 }, + { migrationId: "m-2", targetJurisdiction: "eu" }, + ); + const events: MigrationEvent[] = [ + { type: "freeze-acknowledged" }, + { type: "snapshot-verified" }, + { type: "switch" }, + ]; + + for (const event of events) { + state = transitionMigration(state, event); + assert.equal(state.targetServing, false, `target served during ${state.phase}`); + } + assert.equal(state.activeJurisdiction, "eu"); + state = transitionMigration(state, { type: "activate-target" }); + assert.equal(state.phase, "active"); + assert.equal(state.targetServing, true); + assert.equal(state.sourceFrozen, true); +}); + +test("a switched migration cannot roll back; recovery can only continue forward", () => { + let state = beginMigration( + { activeJurisdiction: "us", routingEpoch: 4 }, + { migrationId: "m-3", targetJurisdiction: "eu" }, + ); + for (const event of [ + { type: "freeze-acknowledged" }, + { type: "snapshot-verified" }, + { type: "switch" }, + ] as const) { + state = transitionMigration(state, event); + } + + assert.throws( + () => transitionMigration(state, { type: "abort" }), + /cannot abort after switch/, + ); + assert.equal(state.activeJurisdiction, "eu"); + assert.equal(state.sourceFrozen, true); + assert.equal(state.targetServing, false); +}); + +test("pre-switch abort returns to source with a fresh epoch and never serves target", () => { + let state = beginMigration( + { activeJurisdiction: "us", routingEpoch: 12 }, + { migrationId: "m-4", targetJurisdiction: "eu" }, + ); + state = transitionMigration(state, { type: "freeze-acknowledged" }); + state = transitionMigration(state, { type: "abort" }); + + assert.equal(state.phase, "aborted"); + assert.equal(state.activeJurisdiction, "us"); + assert.equal(state.routingEpoch, 14); + assert.equal(state.sourceFrozen, false); + assert.equal(state.targetServing, false); +}); + +test("generated phase/event combinations preserve routing and serving invariants", () => { + const eventTypes = [ + "freeze-acknowledged", + "snapshot-verified", + "switch", + "activate-target", + "abort", + ] as const; + const phases = ["pending", "frozen", "verified", "switched", "active", "aborted"] as const; + + for (const phase of phases) { + for (const eventType of eventTypes) { + const before = { + migrationId: "generated", + sourceJurisdiction: "us" as const, + targetJurisdiction: "eu" as const, + activeJurisdiction: phase === "switched" || phase === "active" ? "eu" as const : "us" as const, + routingEpoch: 9, + phase, + sourceFrozen: phase === "frozen" || phase === "verified" || phase === "switched" || phase === "active", + targetServing: phase === "active", + }; + try { + const after = transitionMigration(before, { type: eventType }); + assert.ok(after.routingEpoch >= before.routingEpoch, `${phase} + ${eventType} decreased epoch`); + assert.ok(after.targetServing === false || after.phase === "active", `${phase} + ${eventType} served target early`); + assert.ok(after.activeJurisdiction === "us" || after.sourceFrozen, `${phase} + ${eventType} unfenced source`); + } catch (error) { + // Illegal transitions are valid outcomes; the oracle applies to accepted transitions. + assert.ok(error instanceof MigrationTransitionError); + } + } + } +}); + +test("seeded event sequences preserve migration invariants across generated histories", () => { + const eventTypes: MigrationEvent["type"][] = [ + "freeze-acknowledged", + "snapshot-verified", + "switch", + "activate-target", + "abort", + ]; + let seed = 0x3404; + const next = () => { + seed = (Math.imul(seed, 1664525) + 1013904223) >>> 0; + return seed; + }; + const assertInvariant = (state: ReturnType) => { + assert.ok(state.routingEpoch >= 1); + assert.equal(state.targetServing, state.phase === "active"); + assert.equal(state.sourceFrozen, state.phase === "frozen" || state.phase === "verified" || state.phase === "switched" || state.phase === "active"); + if (state.phase === "pending" || state.phase === "frozen" || state.phase === "verified" || state.phase === "aborted") { + assert.equal(state.activeJurisdiction, state.sourceJurisdiction); + } + }; + + // A fixed-seed generator makes the run reproducible; the oracle is the + // design's routing/fencing invariant, not a list of hand-picked outputs. + for (let run = 0; run < 1_000; run += 1) { + let state = beginMigration( + { activeJurisdiction: "us", routingEpoch: next() % 1_000 }, + { migrationId: `generated-${run}`, targetJurisdiction: "eu" }, + ); + for (let step = 0; step < 10; step += 1) { + const before = state; + const event: MigrationEvent = { type: eventTypes[next() % eventTypes.length]! }; + let after: ReturnType; + try { + after = transitionMigration(state, event); + } catch (error) { + assert.ok(error instanceof MigrationTransitionError); + assert.equal(state, before, "rejected transition mutated its input state"); + continue; + } + state = after; + assert.ok(state.routingEpoch >= before.routingEpoch); + assertInvariant(state); + } + } +});