# Tasks — `@suaveplan/api-saga` > Every checkbox MUST reflect work actually done in this change. ## 0. Pre-flight (non-negotiable) - [x] 0.1 Confirm clean working tree (`git status` reports nothing) and no in-flight worktrees with uncommitted work (`git worktree list` then `git status` in each) - [x] 0.2 If any worktrees exist under `.claude/worktrees/`, account for each one: still-active agent, work-pending-merge, or stale-and-prune. Stale worktrees MUST be removed (`git worktree remove`) before opening a new one — code that has been lost twice on main is the failure mode this guards against. - [x] 0.3 Confirm the package set in `MANIFEST.md` matches `find packages -maxdepth 3 -name package.json` count. Drift here means a prior change archived without regenerating the manifest. ## 1. Spec authoring - [x] 1.1 Read design doc §9.1 (sagas) and §9.3 (engine choice) - [x] 1.2 Read umbrella `openspec/changes/api-plugin-foundation/proposal.md` (Stage 6 acceptance) - [x] 1.3 Read `openspec/AGENTS.md` Sections 13 and 14 - [x] 1.4 Draft `proposal.md` from `openspec/templates/proposal.md` - [x] 1.5 Draft `specs/api-saga/spec.md` from `openspec/templates/spec-delta.md` - [x] 1.6 Every requirement carries a `**Verified by:**` line naming concrete `it(...)` titles ## 2. Implementation - [x] 2.1 Scaffold `packages/api/api-saga/` with `package.json` (`"version": "0.1.0"`, `"sideEffects": false`), `tsconfig.json`, `tsup.config.ts` - [x] 2.2 `src/orchestrator/orchestrator.ts` — `createSagaOrchestrator(deps)` factory + `SagaOrchestrator` class - [x] 2.3 `src/registry/participant-registry.ts` — owner + participant registration, validation of declared step → plugin → action wiring - [x] 2.4 `src/persistence/persistence.ts` — `SagaPersistence` interface bound against host metadata DB; CRUD over `suaveplan_saga_instances` and `suaveplan_saga_steps` - [x] 2.5 `src/recovery/recovery.ts` — boot-time replay: query incomplete instances, resume each through orchestrator - [x] 2.6 `src/idempotency/idempotency.ts` — per-step idempotency wrapper using `@suaveplan/idempotency` keyed by `{sagaId, stepName}`; honour per-action `idempotency: "manual"` opt-out - [x] 2.7 `src/events/events.ts` — Zod schemas for every saga event payload (`saga.step.requested`, `.completed`, `.failed`, `.compensated`, plus `.instance.*`); typed emitter helpers - [x] 2.8 `src/compensation/compensation.ts` — reverse-order compensation runner with retry up to `maxCompensationAttempts`; `compensation_failed` terminal state - [x] 2.9 `src/tenant/cross-tenant.ts` — capability check for `saga.cross-tenant`; refuse step dispatch otherwise - [x] 2.10 `src/state/state-machine.ts` — `SagaInstanceState` + `SagaStepState` transition table with invalid-transition guard - [x] 2.11 `src/instruments/instruments.ts` — OTel instrument registration (span/counter/histogram/gauge per design §9.1) - [x] 2.12 `src/error-codes.ts` — 10 `SAGA_*` codes; every code thrown from at least one production path - [x] 2.13 `./testing` subpath — `runSagaParticipantConformance` suite - [x] 2.14 `src/index.ts` — explicit named re-exports ## 3. Tests - [x] 3.1 Unit tests per module (every `it(...)` `{ timeout: 5000 }`) - [x] 3.2 `src/orchestrator/orchestrator.spec-conformance.test.ts` — runs the conformance suite against a reference participant - [x] 3.3 `src/persistence/persistence.test.ts` — Postgres integration via `@suaveplan/testing-integration-postgres` (`{ timeout: 30000 }`); pins recovery-after-restart - [x] 3.4 `tests/integration/cross-plugin-saga.test.ts` — full two-plugin saga end-to-end with forced compensation (`{ timeout: 60000 }`) - [x] 3.5 Every test imports test-runner primitives from `@suaveplan/testing/runner` ## 4. Documentation - [x] 4.1 Sibling `.md` for every implementation `.ts` (≥200 words, basic + advanced examples) - [x] 4.2 `README.md` at package root linking every co-located `.md` under `## Modules`; calls out owner-vs-participant model ## 5. Pre-archive verification (the 5-gate triple) - [x] 5.1 `bun run check` — Biome 0/0/0 - [x] 5.2 `bunx turbo lint typecheck test --filter=@suaveplan/api-saga` — green - [x] 5.3 `bunx turbo test:coverage --filter=@suaveplan/api-saga` — 100/100/100/100 - [x] 5.4 `bun scripts/openspec-conformance.ts @suaveplan/api-saga` — exit 0 - [x] 5.5 `bunx turbo build --filter=@suaveplan/api-saga` — green ## 6. Pre-archive 7-row checklist (AGENTS.md Section 13) - [x] 6.1 Generics threaded through `SagaOrchestrator`; `expectTypeOf` assertions on every literal-type claim - [x] 6.2 Zod schemas wired at every event-payload boundary and at the manifest-tree consumer - [x] 6.3 Every `SAGA_*` error code thrown by at least one production path - [x] 6.4 Strict-mode behaviour tested both on and off (idempotency manual-opt-out, cross-tenant gate) - [x] 6.5 OTel instruments emitted from production paths and asserted with a real in-memory `MeterProvider` - [x] 6.6 Lifecycle dedup tested: re-dispatching a completed step is a no-op; resume after restart does not double-fire - [x] 6.7 No retroactive tick — every box above reflects work done in this change ## 7. Archive - [x] 7.1 Fold spec delta into `openspec/specs/api-saga/spec.md` - [x] 7.2 Move `openspec/changes/api-saga/` → `openspec/archive/-api-saga/` - [x] 7.3 Tick the Stage-6 row in umbrella `openspec/changes/api-plugin-foundation/tasks.md` - [x] 7.4 Refresh `MANIFEST.md`