From 03d3986c2e095a119dd126aed1d75c7faef11410 Mon Sep 17 00:00:00 2001 From: Nathan Rover Date: Fri, 31 Jul 2026 15:59:28 -0400 Subject: [PATCH 1/2] Add Phase 4 independent consumer fixtures for Ops Hub and Support Desk. Fixtures pin workspace scheduler packages and prove real scheduled work for health/GitHub, mail/queue loops, and an Azure Functions timer composition without inventing per-bucket Support Desk tasks or requiring unpublished npm releases. --- README.md | 14 +- docs/ARCHITECTURE.md | 5 +- docs/CLOUDFLARE.md | 6 +- docs/CONSUMER_REQUIREMENTS.md | 6 +- docs/PROJECT_PLAN.md | 14 +- docs/RELEASING.md | 4 +- docs/TESTING.md | 16 +- fixtures/README.md | 20 ++ fixtures/azure-functions/composition.test.ts | 41 ++++ fixtures/azure-functions/composition.ts | 105 +++++++++ fixtures/ops-hub/composition.test.ts | 103 +++++++++ fixtures/ops-hub/composition.ts | 179 +++++++++++++++ fixtures/support-desk/composition.test.ts | 98 ++++++++ fixtures/support-desk/composition.ts | 222 +++++++++++++++++++ tsconfig.test.json | 1 + vitest.config.ts | 2 +- 16 files changed, 816 insertions(+), 20 deletions(-) create mode 100644 fixtures/README.md create mode 100644 fixtures/azure-functions/composition.test.ts create mode 100644 fixtures/azure-functions/composition.ts create mode 100644 fixtures/ops-hub/composition.test.ts create mode 100644 fixtures/ops-hub/composition.ts create mode 100644 fixtures/support-desk/composition.test.ts create mode 100644 fixtures/support-desk/composition.ts diff --git a/README.md b/README.md index e9125d8..685d4a9 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,11 @@ writes. ## Status -Phase 3 is implemented in source. The repository contains +Phase 4 is implemented in source. The repository contains `@pegma/scheduler` (contracts, durable runner), `@pegma/scheduler-cloudflare` -(explicit Cron Trigger dispatch), Memory Store / Azurite / local D1 tests, and -design documentation. No package has been published. +(explicit Cron Trigger dispatch), independent consumer fixtures under +`fixtures/`, Memory Store / Azurite / local D1 tests, and design documentation. +No package has been published. The first consumers are: @@ -25,8 +26,8 @@ The first consumers are: | Package | Status | Responsibility | | ----------------------------- | -------------- | ------------------------------------------- | -| `@pegma/scheduler` | Phase 3 source | Contracts, durable task state, and runner | -| `@pegma/scheduler-cloudflare` | Phase 3 source | Thin explicit Cron Trigger dispatch adapter | +| `@pegma/scheduler` | Phase 4 source | Contracts, durable task state, and runner | +| `@pegma/scheduler-cloudflare` | Phase 4 source | Thin explicit Cron Trigger dispatch adapter | ## Development @@ -40,4 +41,7 @@ npm run test:node npm run test:d1 ``` +Consumer fixtures (Ops Hub, Support Desk, Azure Functions shapes) live under +[fixtures/](fixtures/) and run as part of `npm test`. + See [docs/PROJECT_PLAN.md](docs/PROJECT_PLAN.md) for the delivery sequence. diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index dddd96b..d720804 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -26,8 +26,9 @@ into the core invocation contract and routes `controller.cron` through an explicit host-supplied map. It does not own D1, Wrangler configuration, or task discovery. -Azure begins as a tested composition example. A package is justified only -after repeated adapter behavior appears in a second Azure consumer. +Azure begins as a tested composition example under +`fixtures/azure-functions`. A package is justified only after repeated adapter +behavior appears in a second Azure consumer. ## Static task registry diff --git a/docs/CLOUDFLARE.md b/docs/CLOUDFLARE.md index 4d49992..3c4947b 100644 --- a/docs/CLOUDFLARE.md +++ b/docs/CLOUDFLARE.md @@ -64,8 +64,10 @@ survive restarts. Do not treat Memory Store as durability evidence. 2. Adapter unit tests construct scheduled controller inputs without a network. 3. Cloudflare Vitest pool tests run against real local D1 (`packages/scheduler-cloudflare/src/fixture.d1.test.ts`). -4. A green Ops Hub fixture (Phase 4) proves Docker volume and repeated trigger - behavior. +4. The Ops Hub-shaped fixture under `fixtures/ops-hub` proves Cron route + composition and scheduled work against Memory Store. Docker volume + durability and repeated `/__scheduled` triggers remain host/Worker concerns + once Ops Hub is more than bootstrap. Run the D1 suite alone: diff --git a/docs/CONSUMER_REQUIREMENTS.md b/docs/CONSUMER_REQUIREMENTS.md index 0472815..fcb3393 100644 --- a/docs/CONSUMER_REQUIREMENTS.md +++ b/docs/CONSUMER_REQUIREMENTS.md @@ -69,5 +69,7 @@ Both consumers need: - manual/local invocation; - safe operational telemetry. -This closes requirements Phase 0. It does not claim the integration phase is -complete: both consumer fixtures must still compile and run before release. +This closes requirements Phase 0. Phase 4 consumer fixtures under `fixtures/` +compile against exact workspace package pins and prove real scheduled work for +the Ops Hub and Support Desk task shapes. Full host applications still pin the +first advertised release after Phase 5 publishes. diff --git a/docs/PROJECT_PLAN.md b/docs/PROJECT_PLAN.md index 5cef65f..9834f65 100644 --- a/docs/PROJECT_PLAN.md +++ b/docs/PROJECT_PLAN.md @@ -2,8 +2,8 @@ ## Status -**Stage:** Phases 0–3 are complete in source (contracts, durable runner, and -Cloudflare Cron Trigger adapter with local D1 tests). No package is published +**Stage:** Phases 0–4 are complete in source (contracts, durable runner, +Cloudflare adapter, and independent consumer fixtures). No package is published or production-usable. **Trigger:** Ops Hub and Support Desk independently need the same explicit, @@ -91,7 +91,7 @@ tests pass against Memory Store and Azurite. **Exit criterion:** a synthetic Worker fixture survives restart, repeated events, overlap, and a complete checkpoint cycle against persistent local D1. -### Phase 4 — consumer integration +### Phase 4 — consumer integration (complete) - Compose Ops Hub health probing and GitHub synchronization. - Compose Support Desk mail send/reconciliation/retention and queue repair. @@ -102,6 +102,14 @@ events, overlap, and a complete checkpoint cycle against persistent local D1. **Exit criterion:** both independent consumers compile against exact package pins and their integration tests prove real scheduled work. +**Phase 4 evidence:** `fixtures/ops-hub`, `fixtures/support-desk`, and +`fixtures/azure-functions` pin workspace `@pegma/scheduler@0.0.0` (and the +Cloudflare adapter for Ops Hub) and prove scheduled work with independent +task state. Ops Hub remains bootstrap-only as a host application; the fixture +is the integration bar until that host lands Worker phases. Support Desk +domain packages stay outside this repository; the fixture mirrors +`HOST_COMPOSITION.md` loop names without inventing per-bucket tasks. + ### Phase 5 — first advertised release - Complete focused security review and package inventory verification. diff --git a/docs/RELEASING.md b/docs/RELEASING.md index 05e8706..6cedbf3 100644 --- a/docs/RELEASING.md +++ b/docs/RELEASING.md @@ -7,7 +7,9 @@ Before adding release automation: 1. Complete the Phase 2 runner and adversarial tests. 2. Complete real Azurite and D1 verification. -3. Complete Ops Hub and Support Desk consumer fixtures. +3. Complete Ops Hub and Support Desk consumer fixtures + (`fixtures/ops-hub`, `fixtures/support-desk`, plus Azure reference under + `fixtures/azure-functions`). 4. Complete a focused security review. 5. Refresh exact Pegma dependency pins from `https://pegma.dev/catalog.json`. diff --git a/docs/TESTING.md b/docs/TESTING.md index c9bf38b..a188bef 100644 --- a/docs/TESTING.md +++ b/docs/TESTING.md @@ -68,12 +68,20 @@ Real local D1 tests (`npm run test:d1`) through - concurrent claim fencing on the same task; - `waitUntil` wiring on the execution context. +## Phase 4 consumer fixture coverage + +Independent fixtures under `fixtures/` pin workspace package versions and prove: + +- Ops Hub: health + GitHub routes, independent checkpoints, live-lease overlap; +- Support Desk: five direct loops, independent checkpoints, no dynamic + receipt-bucket tasks, failure retains checkpoints without secrets; +- Azure Functions: timer entry point, checkpoint cycle, stale redelivery. + ## Adapter and consumer matrix -Before publication, also run: +Before publication, also run full host applications when they exist: -- an Ops Hub health and GitHub-sync fixture (Phase 4); -- Support Desk mail-send, reconciliation, terminal sweep, queue repair, and - inactive-sweep fixtures (Phase 4). +- Ops Hub Worker + Docker volume story (host still bootstrap-only); +- Support Desk production host wiring after `@pegma/scheduler@0.1.0` publishes. Memory tests are never a production durability claim. diff --git a/fixtures/README.md b/fixtures/README.md new file mode 100644 index 0000000..c796622 --- /dev/null +++ b/fixtures/README.md @@ -0,0 +1,20 @@ +# Consumer fixtures + +Independent host compositions that pin the local workspace packages +(`@pegma/scheduler@0.0.0` and `@pegma/scheduler-cloudflare@0.0.0`) and prove +real scheduled work before any npm publish. + +These fixtures are the Phase 4 integration bar described in +`docs/RELEASING.md`. They deliberately do **not** import Ops Hub or Support +Desk application packages: Ops Hub is still bootstrap-only, and Support Desk +domain APIs stay outside this repository. Each fixture mirrors the host task +names and bounded-page shapes documented in those consumers. + +| Fixture | Mirrors | +| ----------------- | ------------------------------------------------------------- | +| `ops-hub` | Health probes + GitHub org sync on Cloudflare Cron routes | +| `support-desk` | Mail send/reconcile/sweep + queue repair/inactive-sweep loops | +| `azure-functions` | Timer-trigger composition for a RetireGolden-style Azure host | + +Run with the repository gate (`npm test`). External consumers should pin the +first advertised release (`0.1.0`) after Phase 5 publishes. diff --git a/fixtures/azure-functions/composition.test.ts b/fixtures/azure-functions/composition.test.ts new file mode 100644 index 0000000..73e5991 --- /dev/null +++ b/fixtures/azure-functions/composition.test.ts @@ -0,0 +1,41 @@ +import { describe, expect, it } from "vitest"; + +import { + createAzureFunctionsComposition, + createFakeAzureDomain, +} from "./composition.js"; + +const T0 = "2026-07-31T12:00:00.000Z"; +const T1 = "2026-07-31T13:00:00.000Z"; + +describe("azure-functions consumer fixture", () => { + it("runs a timer occurrence and advances the projection checkpoint", async () => { + const domain = createFakeAzureDomain(); + const composition = createAzureFunctionsComposition({ domain }); + + const first = await composition.onTimer(T0); + expect(first.outcome).toBe("succeeded"); + if (first.outcome === "succeeded") { + expect(first.state.checkpoint).toBe("proj:25"); + } + + const second = await composition.onTimer(T1); + expect(second.outcome).toBe("succeeded"); + if (second.outcome === "succeeded") { + expect(second.state.checkpoint).toBeUndefined(); + expect(second.state.lastScheduledFor).toBe(T1); + } + + expect(domain.refreshes).toBe(2); + }); + + it("suppresses a stale timer redelivery", async () => { + const composition = createAzureFunctionsComposition(); + await composition.onTimer(T1); + const stale = await composition.onTimer(T0); + expect(stale).toMatchObject({ + outcome: "skipped", + reason: "stale_occurrence", + }); + }); +}); diff --git a/fixtures/azure-functions/composition.ts b/fixtures/azure-functions/composition.ts new file mode 100644 index 0000000..9f8000c --- /dev/null +++ b/fixtures/azure-functions/composition.ts @@ -0,0 +1,105 @@ +/** + * RetireGolden-shaped Azure Functions timer composition fixture. + * + * Azure owns the timer trigger wakeup. This fixture shows how a host would + * call createScheduler.runScheduled from a timer function without a dedicated + * Azure adapter package (none is justified until a second Azure consumer + * appears — see docs/ARCHITECTURE.md). + */ +import { + createScheduler, + defineScheduledTasks, + type Scheduler, + type SchedulerRunResult, +} from "@pegma/scheduler"; +import { + fixedClock, + noopLogger, + type Clock, + type IsoTimestamp, +} from "@pegma/spine"; +import { createMemoryStore, type Store } from "@pegma/storage-core"; + +export interface AzureTimerDomain { + refreshProjection(input: { + readonly cursor?: string; + }): Promise<{ readonly nextCursor: string | null; readonly rows: number }>; +} + +export interface AzureTimerComposition { + readonly store: Store; + readonly scheduler: Scheduler<{ + "retiregolden.projection.refresh": ( + context: import("@pegma/scheduler").ScheduledTaskContext, + ) => Promise; + }>; + /** + * Host timer entry point. `scheduledFor` is the trusted occurrence time the + * Azure Functions host supplies (not provider time). + */ + onTimer(scheduledFor: IsoTimestamp): Promise; +} + +export function createFakeAzureDomain(): AzureTimerDomain & { + readonly refreshes: number; +} { + let refreshes = 0; + return { + get refreshes() { + return refreshes; + }, + async refreshProjection({ cursor }) { + refreshes += 1; + if (cursor === undefined) { + return { rows: 25, nextCursor: "proj:25" }; + } + return { rows: 10, nextCursor: null }; + }, + }; +} + +export function createAzureFunctionsComposition(options?: { + readonly store?: Store; + readonly clock?: Clock; + readonly workerId?: string; + readonly domain?: AzureTimerDomain; +}): AzureTimerComposition { + const store = options?.store ?? createMemoryStore(); + const clock = options?.clock ?? fixedClock("2026-07-31T12:00:00.000Z"); + const domain = options?.domain ?? createFakeAzureDomain(); + + const tasks = defineScheduledTasks({ + "retiregolden.projection.refresh": async ({ checkpoint }) => { + const page = await domain.refreshProjection({ + ...(checkpoint === undefined ? {} : { cursor: checkpoint }), + }); + return { + nextCheckpoint: page.nextCursor, + summary: { rows: page.rows }, + more: page.nextCursor !== null, + }; + }, + }); + + const scheduler = createScheduler({ + store, + clock, + logger: noopLogger, + instanceId: "retiregolden-azure", + workerId: options?.workerId ?? "azure-func-instance", + tasks, + leaseMilliseconds: 60_000, + handlerTimeoutMilliseconds: 55_000, + }); + + return { + store, + scheduler, + onTimer(scheduledFor) { + return scheduler.runScheduled("retiregolden.projection.refresh", { + scheduledFor, + invocationId: `timer:${scheduledFor}`, + }); + }, + }; +} diff --git a/fixtures/ops-hub/composition.test.ts b/fixtures/ops-hub/composition.test.ts new file mode 100644 index 0000000..e00376c --- /dev/null +++ b/fixtures/ops-hub/composition.test.ts @@ -0,0 +1,103 @@ +import { describe, expect, it } from "vitest"; + +import { + createFakeOpsHubDomain, + createOpsHubComposition, + runOpsHubGitHubOccurrence, + runOpsHubHealthOccurrence, +} from "./composition.js"; + +const T0 = "2026-07-31T12:00:00.000Z"; +const T1 = "2026-07-31T12:01:00.000Z"; +const T2 = "2026-07-31T12:10:00.000Z"; + +describe("ops-hub consumer fixture", () => { + it("compiles a Cloudflare route map and advances health checkpoints", async () => { + const domain = createFakeOpsHubDomain(); + const composition = createOpsHubComposition({ domain }); + + const first = await runOpsHubHealthOccurrence(composition, T0); + expect(first.outcome).toBe("succeeded"); + if (first.outcome === "succeeded") { + expect(first.state.checkpoint).toBe("targets:2"); + expect(first.result.summary).toEqual({ probed: 2 }); + } + + const second = await runOpsHubHealthOccurrence(composition, T1); + expect(second.outcome).toBe("succeeded"); + if (second.outcome === "succeeded") { + expect(second.state.checkpoint).toBeUndefined(); + } + + expect(domain.probeCalls).toBe(2); + await expect( + composition.scheduler.getState("health.probe-targets"), + ).resolves.toMatchObject({ status: "succeeded" }); + }); + + it("keeps GitHub sync state independent of health probes", async () => { + const domain = createFakeOpsHubDomain(); + const composition = createOpsHubComposition({ domain }); + + await runOpsHubHealthOccurrence(composition, T0); + const sync = await runOpsHubGitHubOccurrence(composition, T2); + expect(sync.outcome).toBe("succeeded"); + if (sync.outcome === "succeeded") { + expect(sync.state.checkpoint).toBe("orgs:1"); + expect(sync.state.taskId).toBe("github.sync-orgs"); + } + + await expect( + composition.scheduler.getState("health.probe-targets"), + ).resolves.toMatchObject({ checkpoint: "targets:2" }); + expect(domain.syncCalls).toBe(1); + }); + + it("suppresses overlap for a live health lease", async () => { + let release: (() => void) | undefined; + const gate = new Promise((resolve) => { + release = resolve; + }); + let entered: (() => void) | undefined; + const enteredGate = new Promise((resolve) => { + entered = resolve; + }); + + const domain = { + probeCalls: 0, + syncCalls: 0, + async probeTargets() { + this.probeCalls += 1; + if (this.probeCalls === 1) { + entered?.(); + await gate; + } + return { probed: 1, nextCursor: null }; + }, + async syncOrgs() { + this.syncCalls += 1; + return { orgsSynced: 0, nextCursor: null }; + }, + }; + + const store = createOpsHubComposition().store; + const a = createOpsHubComposition({ + store, + workerId: "worker-a", + domain, + }); + const b = createOpsHubComposition({ + store, + workerId: "worker-b", + domain, + }); + + const first = runOpsHubHealthOccurrence(a, T0); + await enteredGate; + const second = await runOpsHubHealthOccurrence(b, T0); + expect(second).toMatchObject({ outcome: "skipped", reason: "live_lease" }); + release?.(); + await expect(first).resolves.toMatchObject({ outcome: "succeeded" }); + expect(domain.probeCalls).toBe(1); + }); +}); diff --git a/fixtures/ops-hub/composition.ts b/fixtures/ops-hub/composition.ts new file mode 100644 index 0000000..750ad45 --- /dev/null +++ b/fixtures/ops-hub/composition.ts @@ -0,0 +1,179 @@ +/** + * Ops Hub–shaped consumer fixture. + * + * Mirrors the planned composition-root tasks from ops-hub docs: + * - health.probe-targets (~30s local cadence via Docker → /__scheduled) + * - github.sync-orgs (5–15 min cron) + * + * Domain probe/sync logic stays in Ops Hub. This fixture only proves the + * durable coordination boundary with exact workspace package pins. + */ +import { + createScheduler, + defineScheduledTasks, + type Scheduler, + type SchedulerRunResult, +} from "@pegma/scheduler"; +import { createCloudflareSchedulerDispatch } from "@pegma/scheduler-cloudflare"; +import { + fixedClock, + noopLogger, + type Clock, + type IsoTimestamp, +} from "@pegma/spine"; +import { createMemoryStore, type Store } from "@pegma/storage-core"; + +export interface HealthProbePage { + readonly probed: number; + readonly nextCursor: string | null; +} + +export interface GitHubSyncPage { + readonly orgsSynced: number; + readonly nextCursor: string | null; +} + +export interface OpsHubDomain { + probeTargets(input: { + readonly limit: number; + readonly cursor?: string; + }): Promise; + syncOrgs(input: { + readonly limit: number; + readonly cursor?: string; + }): Promise; +} + +export type OpsHubTasks = { + "health.probe-targets": ( + context: import("@pegma/scheduler").ScheduledTaskContext, + ) => Promise; + "github.sync-orgs": ( + context: import("@pegma/scheduler").ScheduledTaskContext, + ) => Promise; +}; + +export interface OpsHubComposition { + readonly store: Store; + readonly scheduler: Scheduler; + readonly dispatch: ReturnType< + typeof createCloudflareSchedulerDispatch + >; + readonly domain: OpsHubDomain; +} + +export function createFakeOpsHubDomain(): OpsHubDomain & { + readonly probeCalls: number; + readonly syncCalls: number; +} { + let probeCalls = 0; + let syncCalls = 0; + return { + get probeCalls() { + return probeCalls; + }, + get syncCalls() { + return syncCalls; + }, + async probeTargets({ limit, cursor }) { + probeCalls += 1; + if (cursor === undefined) { + return { probed: Math.min(limit, 2), nextCursor: "targets:2" }; + } + return { probed: 1, nextCursor: null }; + }, + async syncOrgs({ limit, cursor }) { + syncCalls += 1; + if (cursor === undefined) { + return { orgsSynced: Math.min(limit, 1), nextCursor: "orgs:1" }; + } + return { orgsSynced: 1, nextCursor: null }; + }, + }; +} + +export function createOpsHubComposition(options?: { + readonly store?: Store; + readonly clock?: Clock; + readonly workerId?: string; + readonly domain?: OpsHubDomain; +}): OpsHubComposition { + const store = options?.store ?? createMemoryStore(); + const clock = options?.clock ?? fixedClock("2026-07-31T12:00:00.000Z"); + const domain = options?.domain ?? createFakeOpsHubDomain(); + + const tasks = defineScheduledTasks({ + "health.probe-targets": async ({ checkpoint }) => { + const page = await domain.probeTargets({ + limit: 50, + ...(checkpoint === undefined ? {} : { cursor: checkpoint }), + }); + return { + nextCheckpoint: page.nextCursor, + summary: { probed: page.probed }, + more: page.nextCursor !== null, + }; + }, + "github.sync-orgs": async ({ checkpoint }) => { + const page = await domain.syncOrgs({ + limit: 10, + ...(checkpoint === undefined ? {} : { cursor: checkpoint }), + }); + return { + nextCheckpoint: page.nextCursor, + summary: { orgs: page.orgsSynced }, + more: page.nextCursor !== null, + }; + }, + }); + + const scheduler = createScheduler({ + store, + clock, + logger: noopLogger, + instanceId: "ops-hub", + workerId: options?.workerId ?? "ops-hub-worker", + tasks, + leaseMilliseconds: 30_000, + handlerTimeoutMilliseconds: 25_000, + }); + + const dispatch = createCloudflareSchedulerDispatch({ + scheduler, + routes: { + // Local Docker 30s driver hits /__scheduled with this expression. + "* * * * *": ["health.probe-targets"], + "*/10 * * * *": ["github.sync-orgs"], + }, + }); + + return { store, scheduler, dispatch, domain }; +} + +export async function runOpsHubHealthOccurrence( + composition: OpsHubComposition, + scheduledFor: IsoTimestamp, +): Promise { + const [result] = await composition.dispatch.scheduled({ + cron: "* * * * *", + scheduledTime: Date.parse(scheduledFor), + }); + if (result === undefined) { + throw new Error("health route produced no result"); + } + return result; +} + +export async function runOpsHubGitHubOccurrence( + composition: OpsHubComposition, + scheduledFor: IsoTimestamp, +): Promise { + const [result] = await composition.dispatch.scheduled({ + cron: "*/10 * * * *", + scheduledTime: Date.parse(scheduledFor), + }); + if (result === undefined) { + throw new Error("github route produced no result"); + } + return result; +} diff --git a/fixtures/support-desk/composition.test.ts b/fixtures/support-desk/composition.test.ts new file mode 100644 index 0000000..3c3e94a --- /dev/null +++ b/fixtures/support-desk/composition.test.ts @@ -0,0 +1,98 @@ +import { describe, expect, it } from "vitest"; + +import { + createFakeSupportDeskDomain, + createSupportDeskComposition, +} from "./composition.js"; + +const T0 = "2026-07-31T12:00:00.000Z"; +const T1 = "2026-07-31T12:05:00.000Z"; +const T2 = "2026-07-31T12:10:00.000Z"; + +describe("support-desk consumer fixture", () => { + it("registers the five direct host loops and advances mail.send checkpoints", async () => { + const domain = createFakeSupportDeskDomain(); + const composition = createSupportDeskComposition({ domain }); + + const first = await composition.runScheduled("mail.send", T0); + expect(first.outcome).toBe("succeeded"); + if (first.outcome === "succeeded") { + expect(first.state.checkpoint).toBe("send:1"); + } + + const second = await composition.runScheduled("mail.send", T1); + expect(second.outcome).toBe("succeeded"); + if (second.outcome === "succeeded") { + expect(second.state.checkpoint).toBeUndefined(); + } + + expect(domain.calls["mail.send"]).toBe(2); + }); + + it("keeps independent checkpoints across mail and queue loops", async () => { + const domain = createFakeSupportDeskDomain(); + const composition = createSupportDeskComposition({ domain }); + + await composition.runScheduled("mail.send", T0); + await composition.runScheduled("queue.repair", T0); + await composition.runScheduled("mail.reconcile", T0); + await composition.runScheduled("mail.terminal-sweep", T0); + await composition.runScheduled("queue.inactive-sweep", T0); + + await expect( + composition.scheduler.getState("mail.send"), + ).resolves.toMatchObject({ checkpoint: "send:1" }); + await expect( + composition.scheduler.getState("queue.repair"), + ).resolves.toMatchObject({ checkpoint: "repair:1" }); + await expect( + composition.scheduler.getState("mail.reconcile"), + ).resolves.toMatchObject({ checkpoint: "reconcile:1" }); + await expect( + composition.scheduler.getState("mail.terminal-sweep"), + ).resolves.toMatchObject({ checkpoint: "terminal:1" }); + await expect( + composition.scheduler.getState("queue.inactive-sweep"), + ).resolves.toMatchObject({ checkpoint: "inactive:1" }); + + expect(Object.keys(domain.calls).sort()).toEqual([ + "mail.reconcile", + "mail.send", + "mail.terminal-sweep", + "queue.inactive-sweep", + "queue.repair", + ]); + }); + + it("does not invent dynamic receipt-bucket tasks", async () => { + const composition = createSupportDeskComposition(); + await expect( + // @ts-expect-error receipt sweeps stay host-selected drivers + composition.runScheduled("receipt.inbound.bucket-a", T2), + ).rejects.toThrow(/not registered/); + }); + + it("proves failure retains the mail.send checkpoint", async () => { + const domain = createFakeSupportDeskDomain(); + const composition = createSupportDeskComposition({ domain }); + await composition.runScheduled("mail.send", T0); + + const failing = createSupportDeskComposition({ + store: composition.store, + domain: { + ...domain, + async runSendPage() { + throw new Error("provider unavailable token=secret"); + }, + }, + workerId: "failing-worker", + }); + + const failed = await failing.runScheduled("mail.send", T1); + expect(failed.outcome).toBe("failed"); + if (failed.outcome === "failed") { + expect(failed.state.checkpoint).toBe("send:1"); + expect(JSON.stringify(failed.state)).not.toMatch(/secret/); + } + }); +}); diff --git a/fixtures/support-desk/composition.ts b/fixtures/support-desk/composition.ts new file mode 100644 index 0000000..64e3bae --- /dev/null +++ b/fixtures/support-desk/composition.ts @@ -0,0 +1,222 @@ +/** + * Support Desk–shaped consumer fixture. + * + * Mirrors host loops from support-desk docs/HOST_COMPOSITION.md: + * mail.send, mail.reconcile, mail.terminal-sweep, queue.repair, + * queue.inactive-sweep. Receipt/principal sweeps stay host-selected drivers + * outside Scheduler (no dynamic per-bucket tasks). + * + * Domain page functions are fakes with the same cursor/checkpoint contract + * real Support Desk entry points expose. + */ +import { + createScheduler, + defineScheduledTasks, + type Scheduler, + type SchedulerRunResult, +} from "@pegma/scheduler"; +import { + fixedClock, + noopLogger, + type Clock, + type IsoTimestamp, +} from "@pegma/spine"; +import { createMemoryStore, type Store } from "@pegma/storage-core"; + +export interface BoundedPage { + readonly examined: number; + readonly nextCursor: string | null; +} + +export interface SupportDeskDomain { + runSendPage(input: { + readonly limit: number; + readonly cursor?: string; + }): Promise; + runReconciliationPage(input: { + readonly limit: number; + readonly cursor?: string; + }): Promise; + runTerminalSweep(input: { + readonly limit: number; + readonly cursor?: string; + }): Promise<{ readonly deleted: number; readonly nextCursor: string | null }>; + repairQueueProjectionPage(input: { + readonly limit: number; + readonly cursor?: string; + }): Promise; + sweepInactiveQueueProjections(input: { + readonly limit: number; + readonly cursor?: string; + }): Promise<{ readonly deleted: number; readonly nextCursor: string | null }>; +} + +type SupportTasks = { + "mail.send": ( + context: import("@pegma/scheduler").ScheduledTaskContext, + ) => Promise; + "mail.reconcile": ( + context: import("@pegma/scheduler").ScheduledTaskContext, + ) => Promise; + "mail.terminal-sweep": ( + context: import("@pegma/scheduler").ScheduledTaskContext, + ) => Promise; + "queue.repair": ( + context: import("@pegma/scheduler").ScheduledTaskContext, + ) => Promise; + "queue.inactive-sweep": ( + context: import("@pegma/scheduler").ScheduledTaskContext, + ) => Promise; +}; + +export interface SupportDeskComposition { + readonly store: Store; + readonly scheduler: Scheduler; + readonly domain: SupportDeskDomain; + runScheduled( + taskId: keyof SupportTasks & string, + scheduledFor: IsoTimestamp, + ): Promise; +} + +function pageFromCursor( + cursor: string | undefined, + firstToken: string, +): BoundedPage { + if (cursor === undefined) { + return { examined: 2, nextCursor: firstToken }; + } + return { examined: 1, nextCursor: null }; +} + +export function createFakeSupportDeskDomain(): SupportDeskDomain & { + readonly calls: Readonly>; +} { + const calls: Record = Object.create(null) as Record< + string, + number + >; + const bump = (name: string) => { + calls[name] = (calls[name] ?? 0) + 1; + }; + return { + get calls() { + return calls; + }, + async runSendPage({ cursor }) { + bump("mail.send"); + const page = pageFromCursor(cursor, "send:1"); + return { ...page, accepted: page.examined }; + }, + async runReconciliationPage({ cursor }) { + bump("mail.reconcile"); + return pageFromCursor(cursor, "reconcile:1"); + }, + async runTerminalSweep({ cursor }) { + bump("mail.terminal-sweep"); + const page = pageFromCursor(cursor, "terminal:1"); + return { deleted: page.examined, nextCursor: page.nextCursor }; + }, + async repairQueueProjectionPage({ cursor }) { + bump("queue.repair"); + return pageFromCursor(cursor, "repair:1"); + }, + async sweepInactiveQueueProjections({ cursor }) { + bump("queue.inactive-sweep"); + const page = pageFromCursor(cursor, "inactive:1"); + return { deleted: page.examined, nextCursor: page.nextCursor }; + }, + }; +} + +export function createSupportDeskComposition(options?: { + readonly store?: Store; + readonly clock?: Clock; + readonly workerId?: string; + readonly domain?: SupportDeskDomain; +}): SupportDeskComposition { + const store = options?.store ?? createMemoryStore(); + const clock = options?.clock ?? fixedClock("2026-07-31T12:00:00.000Z"); + const domain = options?.domain ?? createFakeSupportDeskDomain(); + + const tasks = defineScheduledTasks({ + "mail.send": async ({ checkpoint }) => { + const page = await domain.runSendPage({ + limit: 50, + ...(checkpoint === undefined ? {} : { cursor: checkpoint }), + }); + return { + nextCheckpoint: page.nextCursor, + summary: { accepted: page.accepted, examined: page.examined }, + more: page.nextCursor !== null, + }; + }, + "mail.reconcile": async ({ checkpoint }) => { + const page = await domain.runReconciliationPage({ + limit: 50, + ...(checkpoint === undefined ? {} : { cursor: checkpoint }), + }); + return { + nextCheckpoint: page.nextCursor, + summary: { examined: page.examined }, + more: page.nextCursor !== null, + }; + }, + "mail.terminal-sweep": async ({ checkpoint }) => { + const page = await domain.runTerminalSweep({ + limit: 50, + ...(checkpoint === undefined ? {} : { cursor: checkpoint }), + }); + return { + nextCheckpoint: page.nextCursor, + summary: { deleted: page.deleted }, + more: page.nextCursor !== null, + }; + }, + "queue.repair": async ({ checkpoint }) => { + const page = await domain.repairQueueProjectionPage({ + limit: 50, + ...(checkpoint === undefined ? {} : { cursor: checkpoint }), + }); + return { + nextCheckpoint: page.nextCursor, + summary: { examined: page.examined }, + more: page.nextCursor !== null, + }; + }, + "queue.inactive-sweep": async ({ checkpoint }) => { + const page = await domain.sweepInactiveQueueProjections({ + limit: 50, + ...(checkpoint === undefined ? {} : { cursor: checkpoint }), + }); + return { + nextCheckpoint: page.nextCursor, + summary: { deleted: page.deleted }, + more: page.nextCursor !== null, + }; + }, + }); + + const scheduler = createScheduler({ + store, + clock, + logger: noopLogger, + instanceId: "retiregolden-support", + workerId: options?.workerId ?? "support-worker", + tasks, + leaseMilliseconds: 30_000, + handlerTimeoutMilliseconds: 25_000, + }); + + return { + store, + scheduler, + domain, + runScheduled(taskId, scheduledFor) { + return scheduler.runScheduled(taskId, { + scheduledFor, + invocationId: `${taskId}:${scheduledFor}`, + }); + }, + }; +} diff --git a/tsconfig.test.json b/tsconfig.test.json index 8da8ef3..4208d24 100644 --- a/tsconfig.test.json +++ b/tsconfig.test.json @@ -10,6 +10,7 @@ }, "include": [ "packages/*/src/**/*.test.ts", + "fixtures/**/*.ts", "test/**/*.ts", "vitest.config.ts", "vitest.d1.config.ts" diff --git a/vitest.config.ts b/vitest.config.ts index 991f2f0..2076d0b 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -2,7 +2,7 @@ import { defineConfig } from "vitest/config"; export default defineConfig({ test: { - include: ["packages/**/*.test.ts"], + include: ["packages/**/*.test.ts", "fixtures/**/*.test.ts"], exclude: ["**/dist/**", "**/node_modules/**", "**/*.d1.test.ts"], globalSetup: ["./test/azurite.ts"], testTimeout: 30_000, From 22e3a00e6ffbd3bc10da032a2d4a03ace8ab2169 Mon Sep 17 00:00:00 2001 From: Nathan Rover Date: Fri, 31 Jul 2026 16:03:46 -0400 Subject: [PATCH 2/2] Declare exact dependency pins on each consumer fixture package. Add private fixture workspaces with package.json pins for scheduler 0.0.0 so Phase 4 evidence is mechanized rather than only documented. --- docs/PROJECT_PLAN.md | 12 ++++---- fixtures/README.md | 5 ++-- fixtures/azure-functions/package.json | 12 ++++++++ fixtures/ops-hub/package.json | 13 ++++++++ fixtures/support-desk/package.json | 12 ++++++++ package-lock.json | 43 ++++++++++++++++++++++++++- package.json | 3 +- 7 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 fixtures/azure-functions/package.json create mode 100644 fixtures/ops-hub/package.json create mode 100644 fixtures/support-desk/package.json diff --git a/docs/PROJECT_PLAN.md b/docs/PROJECT_PLAN.md index 9834f65..18498ba 100644 --- a/docs/PROJECT_PLAN.md +++ b/docs/PROJECT_PLAN.md @@ -102,12 +102,12 @@ events, overlap, and a complete checkpoint cycle against persistent local D1. **Exit criterion:** both independent consumers compile against exact package pins and their integration tests prove real scheduled work. -**Phase 4 evidence:** `fixtures/ops-hub`, `fixtures/support-desk`, and -`fixtures/azure-functions` pin workspace `@pegma/scheduler@0.0.0` (and the -Cloudflare adapter for Ops Hub) and prove scheduled work with independent -task state. Ops Hub remains bootstrap-only as a host application; the fixture -is the integration bar until that host lands Worker phases. Support Desk -domain packages stay outside this repository; the fixture mirrors +**Phase 4 evidence:** workspace packages under `fixtures/*` each declare exact +dependency pins in their own `package.json` (`@pegma/scheduler@0.0.0`; Ops Hub +also pins `@pegma/scheduler-cloudflare@0.0.0`) and prove scheduled work with +independent task state. Ops Hub remains bootstrap-only as a host application; +the fixture is the integration bar until that host lands Worker phases. +Support Desk domain packages stay outside this repository; the fixture mirrors `HOST_COMPOSITION.md` loop names without inventing per-bucket tasks. ### Phase 5 — first advertised release diff --git a/fixtures/README.md b/fixtures/README.md index c796622..64bcf58 100644 --- a/fixtures/README.md +++ b/fixtures/README.md @@ -1,7 +1,8 @@ # Consumer fixtures -Independent host compositions that pin the local workspace packages -(`@pegma/scheduler@0.0.0` and `@pegma/scheduler-cloudflare@0.0.0`) and prove +Independent host compositions that declare exact dependency pins in each +fixture `package.json` (`@pegma/scheduler@0.0.0`, and for Ops Hub also +`@pegma/scheduler-cloudflare@0.0.0`) against the monorepo workspaces and prove real scheduled work before any npm publish. These fixtures are the Phase 4 integration bar described in diff --git a/fixtures/azure-functions/package.json b/fixtures/azure-functions/package.json new file mode 100644 index 0000000..988ee18 --- /dev/null +++ b/fixtures/azure-functions/package.json @@ -0,0 +1,12 @@ +{ + "name": "@pegma/fixture-azure-functions", + "version": "0.0.0", + "private": true, + "description": "Azure Functions timer composition fixture for @pegma/scheduler.", + "type": "module", + "dependencies": { + "@pegma/scheduler": "0.0.0", + "@pegma/spine": "0.1.2", + "@pegma/storage-core": "0.4.0" + } +} diff --git a/fixtures/ops-hub/package.json b/fixtures/ops-hub/package.json new file mode 100644 index 0000000..28e26ad --- /dev/null +++ b/fixtures/ops-hub/package.json @@ -0,0 +1,13 @@ +{ + "name": "@pegma/fixture-ops-hub", + "version": "0.0.0", + "private": true, + "description": "Ops Hub-shaped consumer fixture for @pegma/scheduler.", + "type": "module", + "dependencies": { + "@pegma/scheduler": "0.0.0", + "@pegma/scheduler-cloudflare": "0.0.0", + "@pegma/spine": "0.1.2", + "@pegma/storage-core": "0.4.0" + } +} diff --git a/fixtures/support-desk/package.json b/fixtures/support-desk/package.json new file mode 100644 index 0000000..b68528b --- /dev/null +++ b/fixtures/support-desk/package.json @@ -0,0 +1,12 @@ +{ + "name": "@pegma/fixture-support-desk", + "version": "0.0.0", + "private": true, + "description": "Support Desk-shaped consumer fixture for @pegma/scheduler.", + "type": "module", + "dependencies": { + "@pegma/scheduler": "0.0.0", + "@pegma/spine": "0.1.2", + "@pegma/storage-core": "0.4.0" + } +} diff --git a/package-lock.json b/package-lock.json index d714eab..2ec59b4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,8 @@ "version": "0.0.0", "license": "MIT", "workspaces": [ - "packages/*" + "packages/*", + "fixtures/*" ], "devDependencies": { "@azure/data-tables": "^13.3.2", @@ -27,6 +28,34 @@ "node": ">=22" } }, + "fixtures/azure-functions": { + "name": "@pegma/fixture-azure-functions", + "version": "0.0.0", + "dependencies": { + "@pegma/scheduler": "0.0.0", + "@pegma/spine": "0.1.2", + "@pegma/storage-core": "0.4.0" + } + }, + "fixtures/ops-hub": { + "name": "@pegma/fixture-ops-hub", + "version": "0.0.0", + "dependencies": { + "@pegma/scheduler": "0.0.0", + "@pegma/scheduler-cloudflare": "0.0.0", + "@pegma/spine": "0.1.2", + "@pegma/storage-core": "0.4.0" + } + }, + "fixtures/support-desk": { + "name": "@pegma/fixture-support-desk", + "version": "0.0.0", + "dependencies": { + "@pegma/scheduler": "0.0.0", + "@pegma/spine": "0.1.2", + "@pegma/storage-core": "0.4.0" + } + }, "node_modules/@azure-rest/core-client": { "version": "2.8.0", "resolved": "https://registry.npmjs.org/@azure-rest/core-client/-/core-client-2.8.0.tgz", @@ -2017,6 +2046,18 @@ "url": "https://github.com/sponsors/Boshen" } }, + "node_modules/@pegma/fixture-azure-functions": { + "resolved": "fixtures/azure-functions", + "link": true + }, + "node_modules/@pegma/fixture-ops-hub": { + "resolved": "fixtures/ops-hub", + "link": true + }, + "node_modules/@pegma/fixture-support-desk": { + "resolved": "fixtures/support-desk", + "link": true + }, "node_modules/@pegma/scheduler": { "resolved": "packages/scheduler", "link": true diff --git a/package.json b/package.json index 9854cb9..39f5fae 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "node": ">=22" }, "workspaces": [ - "packages/*" + "packages/*", + "fixtures/*" ], "scripts": { "build": "tsc -b",