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,51 @@
# `@suaveplan/api-tenant-resolver-jwt` — Principal-derived tenant resolver
**Package**: `@suaveplan/api-tenant-resolver-jwt`
**Status**: PROPOSED
**Type**: new-package
**Stage**: 4 (Base adapters) — see umbrella `openspec/changes/api-plugin-foundation/`
## Why
Per design doc §6 step 4, the tenant resolver chain runs after the session resolver. When the session is a JWT carrying `tenant_id` (or `tid`), the tenant resolver's job is trivial: read `ctx.principal.tenantId`. Collapsing this into a dedicated adapter — rather than encoding it inside `api-auth-jwt` — keeps the two contracts orthogonal and lets operators chain it with header- or subdomain-based resolvers (Stage 8) for paths the JWT doesn't cover.
When paired with `api-auth-jwt`, this resolver effectively degenerates to an identity read; its presence makes the data-flow explicit and lets the conformance suite test the empty/missing-tenant edge cases uniformly.
Stage 4 invariant: both direct and worker-proxy surfaces ship; both pass `runTenantResolverConformance` (§5.2a, §13.4).
## What changes
Net-new package depending on `@suaveplan/api-tenant-resolver` (contract), `@suaveplan/api-types`, `@suaveplan/api-worker-rpc`, `@suaveplan/error`, `zod`.
- ADDED: `createJwtTenantResolver(opts)` — direct surface returning a `TenantResolver`. Reads `ctx.principal.tenantId`; returns it if present, otherwise returns the chain-sentinel `undefined` (so the next resolver in the chain runs).
- ADDED: `createJwtTenantResolverWorkerProxy(port, opts)` — proxy surface.
- ADDED: `JwtTenantResolverOptions` — Zod schema with `requirePrincipal: boolean` (default false; if true, throws `TENANT_JWT_PRINCIPAL_REQUIRED` when principal is null) and `mode: "identity" | "claim-fallback"` (default `"identity"`).
- ADDED: `TENANT_JWT_*` error codes (`TENANT_JWT_PRINCIPAL_REQUIRED`, `TENANT_JWT_INVALID_OPTIONS`).
## Out of scope
- Reading tenant from arbitrary JWT claims independent of `Principal` projection — that responsibility belongs to `api-auth-jwt`'s `claimToPrincipal` mapping.
- Header- or subdomain-based resolution — `api-tenant-resolver-header` and `api-tenant-resolver-subdomain` (Stage 8).
- Tenant enumeration (for `perTenant: true` jobs) — `api-tenant-provider-memory` (this stage).
## Source mapping
Genesis-original. Source of design intent: `docs/superpowers/specs/2026-05-13-api-plugin-foundation-design.md`.
| Requirement | Design doc section |
|---|---|
| Tenant resolver chain step 4 | §6 |
| First match wins; sentinel for chain advance | §6 |
| Worker-proxy variant required | §5.2a |
| Conformance both surfaces | §13.4 |
## Verification approach
- `src/factory/create-jwt-tenant-resolver.test.ts` — principal with tenantId → returns it; principal without tenantId → returns `undefined` (advance chain); null principal → `undefined` unless `requirePrincipal: true` (throws).
- `src/factory/create-jwt-tenant-resolver.spec-conformance.test.ts` — `runTenantResolverConformance("jwt-direct", ...)`.
- `src/proxy/create-jwt-tenant-resolver-worker-proxy.test.ts` — proxy unit tests.
- `src/proxy/create-jwt-tenant-resolver-worker-proxy.spec-conformance.test.ts` — `runTenantResolverConformance("jwt-worker-proxy", ...)`.
## Migration impact
None — additive only.

View File

@@ -0,0 +1,63 @@
<!-- markdownlint-disable MD024 -->
# Capability Spec Delta: api-tenant-resolver-jwt (api-tenant-resolver-jwt)
**Package**: `@suaveplan/api-tenant-resolver-jwt`
**Status**: PROPOSED
## ADDED Requirements
### Requirement: Resolve tenant from Principal
The package SHALL implement `TenantResolver` such that `resolve(request, principal)` returns `principal.tenantId` when present, returns `undefined` (chain-advance sentinel) when principal is non-null but has no `tenantId`, and returns `undefined` when principal is null (unless `requirePrincipal: true`).
#### Scenario: principal with tenantId resolves directly
- **WHEN** `principal = { id: "u1", tenantId: "acme", scopes: [], claims: {} }`
- **THEN** `resolve()` returns `"acme"`
#### Scenario: principal without tenantId advances the chain
- **WHEN** `principal = { id: "u1", scopes: [], claims: {} }` (no tenantId)
- **THEN** `resolve()` returns `undefined` so the next resolver in the chain runs
#### Scenario: null principal advances the chain by default
- **WHEN** `principal === null` AND `requirePrincipal: false`
- **THEN** `resolve()` returns `undefined`
#### Scenario: requirePrincipal: true throws when principal is null
- **WHEN** `principal === null` AND `requirePrincipal: true`
- **THEN** `resolve()` throws `TENANT_JWT_PRINCIPAL_REQUIRED`
**Verified by:** `src/factory/create-jwt-tenant-resolver.test.ts::"returns principal.tenantId when present"` AND `src/factory/create-jwt-tenant-resolver.test.ts::"returns undefined when tenantId missing"` AND `src/factory/create-jwt-tenant-resolver.test.ts::"throws when requirePrincipal and principal is null"`
---
### Requirement: Direct and worker-proxy surfaces both pass conformance
#### Scenario: direct conforms
- **WHEN** `runTenantResolverConformance("jwt-direct", () => createJwtTenantResolver(...))` runs
- **THEN** every invariant passes
#### Scenario: worker-proxy conforms
- **WHEN** `runTenantResolverConformance("jwt-worker-proxy", ...)` runs over a real `MessageChannel`
- **THEN** every invariant passes
**Verified by:** `src/factory/create-jwt-tenant-resolver.spec-conformance.test.ts::"jwt-direct conforms to TenantResolver contract"` AND `src/proxy/create-jwt-tenant-resolver-worker-proxy.spec-conformance.test.ts::"jwt-worker-proxy conforms to TenantResolver contract"`
---
### Requirement: Options Zod-validated
#### Scenario: invalid options throw at construction
- **WHEN** `createJwtTenantResolver({ mode: "bogus" as never })`
- **THEN** a `SuaveplanError` with code `TENANT_JWT_INVALID_OPTIONS` is thrown synchronously
**Verified by:** `src/options/jwt-tenant-resolver-options.test.ts::"throws on invalid mode value"`
<!-- markdownlint-enable MD024 -->

View File

@@ -0,0 +1,65 @@
# Tasks — `@suaveplan/api-tenant-resolver-jwt`
> 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 (§6, §5.2a, §13.4)
- [x] 1.2 Read `openspec/changes/api-tenant-resolver/` (or contract location) for upstream interface (api-auth contains tenant-resolver chain types)
- [x] 1.3 Read `openspec/AGENTS.md` Sections 13 and 14
- [x] 1.4 Draft `proposal.md`
- [x] 1.5 Draft `specs/api-tenant-resolver-jwt/spec.md`
- [x] 1.6 Every requirement carries `**Verified by:**`
## 2. Implementation
- [ ] 2.1 Scaffold `packages/api/api-tenant-resolver-jwt/` (deps: api-tenant-resolver contract, api-types, api-worker-rpc, error, zod)
- [ ] 2.2 `src/options/jwt-tenant-resolver-options.ts` — Zod schema
- [ ] 2.3 `src/factory/create-jwt-tenant-resolver.ts` — direct surface
- [ ] 2.4 `src/proxy/create-jwt-tenant-resolver-worker-proxy.ts` — proxy surface
- [ ] 2.5 `src/error-codes.ts` — `TENANT_JWT_*` codes via `defineCodes`
- [ ] 2.6 `src/index.ts` — explicit named re-exports
## 3. Tests
- [ ] 3.1 `src/factory/create-jwt-tenant-resolver.test.ts`
- [ ] 3.2 `src/factory/create-jwt-tenant-resolver.spec-conformance.test.ts` — direct conformance
- [ ] 3.3 `src/proxy/create-jwt-tenant-resolver-worker-proxy.test.ts`
- [ ] 3.4 `src/proxy/create-jwt-tenant-resolver-worker-proxy.spec-conformance.test.ts` — proxy conformance
- [ ] 3.5 Every `it(...)` carries `{ timeout: 5000 }`
## 4. Documentation
- [ ] 4.1 Sibling `.md` per implementation `.ts`
- [ ] 4.2 README documents the chain interaction with header/subdomain resolvers (Stage 8 forward-reference)
## 5. Pre-archive verification (5-gate triple)
- [ ] 5.1 `bun run check` — clean
- [ ] 5.2 `bunx turbo lint typecheck test --filter=@suaveplan/api-tenant-resolver-jwt` — green
- [ ] 5.3 `bunx turbo test:coverage --filter=@suaveplan/api-tenant-resolver-jwt` — 100/100/100/100
- [ ] 5.4 `bun scripts/openspec-conformance.ts @suaveplan/api-tenant-resolver-jwt` — exit 0
- [ ] 5.5 `bunx turbo build --filter=@suaveplan/api-tenant-resolver-jwt` — green
## 6. Pre-archive 7-row checklist
- [ ] 6.1 `expectTypeOf` asserts the resolver returns `TenantId | undefined`
- [ ] 6.2 Zod schema at options boundary
- [ ] 6.3 Every `TENANT_JWT_*` code thrown by at least one production path
- [ ] 6.4 Strict-mode (`requirePrincipal`) tested both on and off
- [ ] 6.5 OTel instruments (`api.tenant.resolved_total{source=jwt,outcome}`) asserted with in-memory `MeterProvider`
- [ ] 6.6 Lifecycle dedup — N/A
- [ ] 6.7 No retroactive tick
## 7. Archive
- [ ] 7.1 Fold into `openspec/specs/api-tenant-resolver-jwt/spec.md`
- [ ] 7.2 Move to `openspec/archive/<yyyy-mm-dd>-api-tenant-resolver-jwt/`
- [ ] 7.3 Tick Stage 4 box in umbrella tasks
- [ ] 7.4 Refresh `MANIFEST.md`