Initial Commit

This commit is contained in:
2026-05-27 23:29:53 +01:00
commit fbc70f96e6
10000 changed files with 1102152 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
# `@suaveplan/api-jobs-worker-proxy` — worker-side proxy to main-process job scheduler
**Package**: `@suaveplan/api-jobs-worker-proxy`
**Status**: PROPOSED
**Type**: new-package
**Stage**: 3 (Worker isolation infrastructure)
## Why
Per design doc §5.4, plugins register jobs (cron + on-demand + per-tenant fan-out) via their manifest, and the host's job scheduler — `@suaveplan/job-scheduler` — owns the actual scheduling, ticking, and per-tenant fan-out. Worker-tier plugins still need a `ctx.jobs` slot that lets them do the runtime work (enqueue an on-demand run, observe a registration handle), so this proxy surfaces those operations over `MessagePort`. Job TICKS themselves arrive at the worker via the same request-dispatch path used by routes — this proxy is for the registration-handle side of the contract, not the tick delivery.
## What changes
Net-new package.
**ADDED exports:**
- `./client` — `createJobsProxyClient(port): WorkerProxyOf<JobRegistrationHandle>`. Worker-side `ctx.jobs.enqueue(...)`, `ctx.jobs.pause(...)`, `ctx.jobs.resume(...)` calls marshal to the main-process `@suaveplan/job-scheduler`.
- `./error-codes` — `JOBS_PROXY_*` codes (`JOBS_PROXY_NOT_MANIFESTED`, `JOBS_PROXY_BACKPRESSURE`, `JOBS_PROXY_PORT_CLOSED`, `JOBS_PROXY_HANDLER_THROW`).
- `./testing` — paired `MessageChannel` + stub scheduler.
## Out of scope
- **Job tick delivery** — ticks arrive via `api-router-worker-bridge`-style envelopes (a "job invocation" envelope analogous to "request invocation"). This package handles the OUTBOUND registration-handle calls.
- **Cron expression parsing** — `@suaveplan/cron`'s job; main-side.
- **Per-tenant fan-out** — `@suaveplan/job-scheduler` + `tenant.provider` on the main side.
## Source mapping
Genesis-original. Design doc sections: §5.2a, §5.4.
| Requirement | Design doc section |
|---|---|
| Proxy over the host's `@suaveplan/job-scheduler` | §5.4 |
| Outbound handle calls marshalled via `MessagePort` | §5.2a |
| Tick delivery is request-style envelope (handled by sibling package) | §6 |
## Verification approach
- `src/client/client.test.ts` — `enqueue` / `pause` / `resume` round-trip; non-manifested job rejected.
- `tests/conformance/run-jobs-conformance.test.ts` — `runJobsConformance` from `@suaveplan/job-scheduler/testing` passes. **Required before Stage 4 can complete.**
## Migration impact
None — additive only.

View File

@@ -0,0 +1,47 @@
<!-- markdownlint-disable MD024 -->
# Capability Spec Delta: api-jobs-worker-proxy (api-jobs-worker-proxy)
**Package**: `@suaveplan/api-jobs-worker-proxy`
**Status**: PROPOSED
> Every requirement below carries a `**Verified by:**` line at authoring time.
## ADDED Requirements
### Requirement: Proxy preserves JobRegistrationHandle surface
The package SHALL expose `createJobsProxyClient(port)` returning a `WorkerProxyOf<JobRegistrationHandle>`. Method names + parameter shapes MUST match the direct surface; only return types are `Promise`-wrapped.
#### Scenario: surface matches WorkerProxyOf<JobRegistrationHandle>
- **WHEN** `expectTypeOf<ReturnType<typeof createJobsProxyClient>>().toEqualTypeOf<WorkerProxyOf<JobRegistrationHandle>>()` is checked
- **THEN** the assertion passes
**Verified by:** `src/client/client.spec-conformance.test.ts::"surface matches WorkerProxyOf<JobRegistrationHandle>"`
---
### Requirement: Non-manifested job name is rejected
The package SHALL surface main-side rejection of non-manifested job names as `JOBS_PROXY_NOT_MANIFESTED`. The host enforces — declared in the plugin's manifest `jobs[]` array — and rejects unknown names at the boundary.
#### Scenario: unknown job name rejects
- **WHEN** the worker calls `ctx.jobs.enqueue("unknown-job", {})`
- **THEN** the call rejects with `SuaveplanError` of code `JOBS_PROXY_NOT_MANIFESTED`
**Verified by:** `src/client/non-manifested.test.ts::"unknown job name rejects"`
---
### Requirement: Conformance suite passes against this proxy
The package SHALL be runnable as the surface argument to `runJobsConformance` from `@suaveplan/job-scheduler/testing`. Every scenario MUST pass identically to the direct surface. This is the Stage-4-precondition gate.
#### Scenario: runJobsConformance passes
- **WHEN** `runJobsConformance("worker-proxy", makeFactory)` runs
- **THEN** every scenario passes
**Verified by:** `tests/conformance/run-jobs-conformance.test.ts::"runJobsConformance passes against proxy"`

View File

@@ -0,0 +1,65 @@
# Tasks — `@suaveplan/api-jobs-worker-proxy`
> Every checkbox MUST reflect work actually done in this change.
## 0. Pre-flight (non-negotiable)
- [ ] 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)
- [ ] 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.
- [ ] 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 §5.2a, §5.4
- [x] 1.2 Read umbrella
- [x] 1.3 Read `openspec/AGENTS.md` Sections 13 and 14
- [x] 1.4 Draft `proposal.md`
- [x] 1.5 Draft `specs/api-jobs-worker-proxy/spec.md`
- [ ] 1.6 Every requirement carries `**Verified by:**`
## 2. Implementation
- [ ] 2.1 Scaffold `packages/api/api-jobs-worker-proxy/` (`0.1.0`, deps: `@suaveplan/api-worker-rpc`, `@suaveplan/api-types`, `@suaveplan/error`, `zod`; peer-dep on `@suaveplan/job-scheduler`)
- [ ] 2.2 `src/client/` — typed proxy client
- [ ] 2.3 `src/error-codes.ts` — `JOBS_PROXY_*` codes
- [ ] 2.4 `src/testing/` — paired `MessageChannel` + stub scheduler
- [ ] 2.5 `src/index.ts` — namespace re-exports
## 3. Tests
- [ ] 3.1 `src/client/client.test.ts` — `enqueue` / `pause` / `resume` round-trip
- [ ] 3.2 `src/client/non-manifested.test.ts` — non-manifested job name throws `JOBS_PROXY_NOT_MANIFESTED`
- [ ] 3.3 `tests/conformance/run-jobs-conformance.test.ts` — `runJobsConformance` passes
- [ ] 3.4 Every `it(...)` carries `{ timeout: 5000 }`
## 4. Documentation
- [ ] 4.1 Sibling `.md` per `.ts`
- [ ] 4.2 README links every co-located `.md`; explicit note that job ticks arrive via the request-bridge envelope path, not this package
- [ ] 4.3 Update CLAUDE.md
## 5. Pre-archive verification (the 5-gate triple)
- [ ] 5.1 `bun run check` — Biome 0/0/0
- [ ] 5.2 `bunx turbo lint typecheck test --filter=@suaveplan/api-jobs-worker-proxy` — green
- [ ] 5.3 `bunx turbo test:coverage --filter=@suaveplan/api-jobs-worker-proxy` — 100/100/100/100
- [ ] 5.4 `bun scripts/openspec-conformance.ts @suaveplan/api-jobs-worker-proxy` — exit 0
- [ ] 5.5 `bunx turbo build --filter=@suaveplan/api-jobs-worker-proxy` — green
- [ ] 5.6 **Stage-4 conformance gate**: `runJobsConformance` (from `@suaveplan/job-scheduler/testing`) MUST pass under both direct AND worker-proxy surface before Stage 4 can complete
## 6. Pre-archive 7-row checklist
- [ ] 6.1 Generics — `JobRegistrationHandle` shape preserved
- [ ] 6.2 Zod at wire boundary
- [ ] 6.3 Every `JOBS_PROXY_*` code thrown
- [ ] 6.4 Strict-mode behaviors — N/A
- [ ] 6.5 OTel — N/A here; main-side scheduler emits job-run metrics
- [ ] 6.6 Lifecycle dedup — N/A
- [ ] 6.7 No retroactive tick
## 7. Archive
- [ ] 7.1 Fold spec delta into `openspec/specs/api-jobs-worker-proxy/spec.md`
- [ ] 7.2 Move to `openspec/archive/<yyyy-mm-dd>-api-jobs-worker-proxy/`
- [ ] 7.3 Tick Stage-3 row in umbrella
- [ ] 7.4 Refresh `MANIFEST.md`