diff --git a/docs/ROLE_ADOPTION_PLAN.md b/docs/ROLE_ADOPTION_PLAN.md index 96d8fd5..26dee77 100644 --- a/docs/ROLE_ADOPTION_PLAN.md +++ b/docs/ROLE_ADOPTION_PLAN.md @@ -90,14 +90,15 @@ The lockout-safe order proven on retiregolden.org, transplanted: Both hosts now want the same role-management surface (lookup → grants view → audited assign/revoke → history). Per the ecosystem's extract-on-second-consumer rule this is the extraction trigger — but it is -**gated on upstream first**: +**gated on upstream first** — a gate now largely cleared: [authorization-core#23](https://github.com/pegma-dev/authorization-core/issues/23) -(identity-link writes) and +(identity-link writes) shipped and [authorization-core#24](https://github.com/pegma-dev/authorization-core/issues/24) -(per-principal/by-role assignment enumeration) decide how much of -retiregolden.org's host-side index the shared component would otherwise have -to carry. Extracting before those decisions would bake a workaround into a -public package right as the library makes it unnecessary. +delivered per-principal lifecycle enumeration (`listRoleAssignments`) in the +synchronized 0.3.0 line, both adopted by the hosts 2026-07-30; by-role +selection deliberately remains the documented host-side +superset-with-verification recipe, which the shared component will carry as +its one host-provided port. When the gate clears, Phase 5 activates in this order — the `Admin` mapping and its bootstrap are PART OF this phase, because a surface gated on a role diff --git a/docs/SUPPORT_COMPOSITION.md b/docs/SUPPORT_COMPOSITION.md index 62a5859..fff7bd1 100644 --- a/docs/SUPPORT_COMPOSITION.md +++ b/docs/SUPPORT_COMPOSITION.md @@ -13,9 +13,9 @@ Exact published versions: - `@pegma/support-desk-contracts@0.1.0` - `@pegma/support-desk-core@0.1.0` - `@pegma/support-desk-templates@0.1.0` -- `@pegma/authorization-core@0.2.0` (AccessContext resolution) -- `@pegma/authorization-policy@0.2.0` (PolicyDocumentV1 schema validation) -- `@pegma/authorization-storage@0.2.0` (D1-backed role store) +- `@pegma/authorization-core@0.3.0` (AccessContext resolution) +- `@pegma/authorization-policy@0.3.0` (PolicyDocumentV1 schema validation) +- `@pegma/authorization-storage@0.3.0` (D1-backed role store) Peers already present on the host: `@pegma/storage-core@0.4.0`, `@pegma/storage-cloudflare-d1@0.4.0`, `@pegma/sessions@0.1.0`, @@ -33,7 +33,6 @@ collection name, not by sharing Identity mail cursors or session rows: | Ticket numbers | `support-desk.ticket-numbers.v1` | | Queue projection | `support-desk.queue-index.v1` | | Host maintenance cursors | `pegma-dev-support-maintenance` | -| Bootstrap-seed markers | `support-bootstrap.markers.v1` | Identity keeps its own collections and `pegma-dev-maintenance` cursors. The two never share a cursor key. @@ -74,10 +73,12 @@ The `Support` role maps to: (comma-separated Identity principal ids) seeds a real audited `Support` assignment (actor `system:bootstrap`, deterministic assignment id `bootstrap-support-`) on a listed principal's next authenticated -support request. Each principal is seeded at most once, ever: the handled -seed is recorded in the `support-bootstrap.markers.v1` collection — even when -nothing was granted because `Support` was already held through another -assignment — so a later revocation stays revoked while the env var lingers. +support request. Each principal is seeded at most once, ever: ANY `Support` +assignment record — active or revoked, whatever its provenance — counts as +already seeded, read first-class via `listRoleAssignments` +(authorization-storage 0.3.0; this check previously needed the host +`support-bootstrap.markers.v1` marker collection, deleted with the upgrade). +A deliberate revocation therefore stays revoked while the env var lingers. Delete the var once the first operator holds the role. **Fail-closed posture.** The role store is the only gate: a host wired diff --git a/package-lock.json b/package-lock.json index 1d1e6e8..04e907f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,10 +10,10 @@ "dependencies": { "@modelcontextprotocol/server": "^2.0.0", "@pegma/audit": "0.2.0", - "@pegma/authorization-core": "0.2.0", - "@pegma/authorization-identity": "0.2.0", - "@pegma/authorization-policy": "0.2.0", - "@pegma/authorization-storage": "0.2.0", + "@pegma/authorization-core": "0.3.0", + "@pegma/authorization-identity": "0.3.0", + "@pegma/authorization-policy": "0.3.0", + "@pegma/authorization-storage": "0.3.0", "@pegma/health": "^0.2.0", "@pegma/identity": "0.1.2", "@pegma/logger-cloudflare": "^0.1.1", @@ -3057,57 +3057,57 @@ } }, "node_modules/@pegma/authorization-contracts": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/@pegma/authorization-contracts/-/authorization-contracts-0.2.0.tgz", - "integrity": "sha512-KXoWzSLfJc05vYOjGoA7pAuSNzbqi0jS8e2zpNDF3mr7sYCC9jVAFPHcDDOqYFZlN4v7f2n49jTF6sVqLPOhAQ==", + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@pegma/authorization-contracts/-/authorization-contracts-0.3.0.tgz", + "integrity": "sha512-H98/qXSt2iXZJi9U8peXZrRJpGHqNUcfhRAOZpBBCEZu9UFAj5SLhgo6ruiy9Zj0MM+VKXEZouWSV3tGa4HgIA==", "license": "MIT", "engines": { "node": ">=22" } }, "node_modules/@pegma/authorization-core": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/@pegma/authorization-core/-/authorization-core-0.2.0.tgz", - "integrity": "sha512-IXZvK0RCysFJ9ifBEyGuMehlyePGpu5jcVe6/xFl/HkpOz+Q0ZYi4lWxIKqCY+OcueWFfN7ruxRVf9g54Etrmg==", + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@pegma/authorization-core/-/authorization-core-0.3.0.tgz", + "integrity": "sha512-xWqg0bYKlmF8npAPytFKDeb59kELmb4CfJndG1EH8/NoLFnKKh+YEAWNkn/VXZbrZjCLAY9qebWbmjuRTVXVZg==", "license": "MIT", "dependencies": { - "@pegma/authorization-contracts": "0.2.0" + "@pegma/authorization-contracts": "0.3.0" }, "engines": { "node": ">=22" } }, "node_modules/@pegma/authorization-identity": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/@pegma/authorization-identity/-/authorization-identity-0.2.0.tgz", - "integrity": "sha512-IpYEpsQ/ZrUgK4Gq0981ieyC/dm8JeyxuXomgPEh+pmo2hXVc/Jkv5r5YwUvP1DAGRmDk2rA2KDnB/TQtsWr3g==", + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@pegma/authorization-identity/-/authorization-identity-0.3.0.tgz", + "integrity": "sha512-GCBXYG75i5c0PxaSo3D6mVah2GhfmXG6KU/jZ0OxKYqY/FzpEHSvuKi03q7JHSJ4VDRNQM31vEgvopIfzdG/bw==", "license": "MIT", "dependencies": { - "@pegma/authorization-contracts": "0.2.0" + "@pegma/authorization-contracts": "0.3.0" }, "engines": { "node": ">=22" } }, "node_modules/@pegma/authorization-policy": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/@pegma/authorization-policy/-/authorization-policy-0.2.0.tgz", - "integrity": "sha512-DOrqHfC6mt3SvcyWDyQ/ydMYQWQ7DPOTWXbKFSQoG+noAfBsAT+jxjDlglOXzz2REYCK0x78z5XHRcEztTyPtg==", + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@pegma/authorization-policy/-/authorization-policy-0.3.0.tgz", + "integrity": "sha512-+mb3Zuzm2FTnYFPkhs49DTCRsyFmGLlbH6EGLEVdNQPk30KU7SCP0jnUjQD/2DMZy4ACxJRTpHymu5L/M7dmiw==", "license": "MIT", "dependencies": { - "@pegma/authorization-contracts": "0.2.0" + "@pegma/authorization-contracts": "0.3.0" }, "engines": { "node": ">=22" } }, "node_modules/@pegma/authorization-storage": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/@pegma/authorization-storage/-/authorization-storage-0.2.0.tgz", - "integrity": "sha512-RceYZ3ctTpOxWy59i/xNinQtWazhBFSuEFlhOObEj4UJcJ2flzCnoZBL8Lls4iHZtsG8UYf/hp4f+ny8PJ90yg==", + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@pegma/authorization-storage/-/authorization-storage-0.3.0.tgz", + "integrity": "sha512-EEdAwpSP+zSpUTOGcr0r0uyeK/XXhumoUPKgmrtusA1pX7QZgwGN3iuu62v6PpRdvsTgHXQaB0CWHmk5MpIGKw==", "license": "MIT", "dependencies": { - "@pegma/authorization-contracts": "0.2.0", + "@pegma/authorization-contracts": "0.3.0", "@pegma/storage-core": "0.4.0" }, "engines": { @@ -3270,6 +3270,27 @@ "node": ">=22" } }, + "node_modules/@pegma/support-desk-application/node_modules/@pegma/authorization-contracts": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/@pegma/authorization-contracts/-/authorization-contracts-0.2.0.tgz", + "integrity": "sha512-KXoWzSLfJc05vYOjGoA7pAuSNzbqi0jS8e2zpNDF3mr7sYCC9jVAFPHcDDOqYFZlN4v7f2n49jTF6sVqLPOhAQ==", + "license": "MIT", + "engines": { + "node": ">=22" + } + }, + "node_modules/@pegma/support-desk-application/node_modules/@pegma/authorization-core": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/@pegma/authorization-core/-/authorization-core-0.2.0.tgz", + "integrity": "sha512-IXZvK0RCysFJ9ifBEyGuMehlyePGpu5jcVe6/xFl/HkpOz+Q0ZYi4lWxIKqCY+OcueWFfN7ruxRVf9g54Etrmg==", + "license": "MIT", + "dependencies": { + "@pegma/authorization-contracts": "0.2.0" + }, + "engines": { + "node": ">=22" + } + }, "node_modules/@pegma/support-desk-contracts": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/@pegma/support-desk-contracts/-/support-desk-contracts-0.1.1.tgz", diff --git a/package.json b/package.json index ae72af2..35e4197 100644 --- a/package.json +++ b/package.json @@ -20,10 +20,10 @@ "dependencies": { "@modelcontextprotocol/server": "^2.0.0", "@pegma/audit": "0.2.0", - "@pegma/authorization-core": "0.2.0", - "@pegma/authorization-identity": "0.2.0", - "@pegma/authorization-policy": "0.2.0", - "@pegma/authorization-storage": "0.2.0", + "@pegma/authorization-core": "0.3.0", + "@pegma/authorization-identity": "0.3.0", + "@pegma/authorization-policy": "0.3.0", + "@pegma/authorization-storage": "0.3.0", "@pegma/health": "^0.2.0", "@pegma/identity": "0.1.2", "@pegma/logger-cloudflare": "^0.1.1", diff --git a/src/data/recipe-backlog.ts b/src/data/recipe-backlog.ts index b3d5a24..53d42d1 100644 --- a/src/data/recipe-backlog.ts +++ b/src/data/recipe-backlog.ts @@ -19,7 +19,7 @@ export const RECIPE_BACKLOG: readonly CatalogRecipe[] = [ '@pegma/sessions@0.2.0', '@pegma/mail@0.1.1', '@pegma/rate-limit@0.2.0', - '@pegma/authorization-identity@0.2.0', + '@pegma/authorization-identity@0.3.0', '@pegma/storage-core@0.4.0', '@pegma/storage-cloudflare-d1@0.4.1', '@pegma/spine@0.1.1', diff --git a/worker/src/index.ts b/worker/src/index.ts index da75924..0a900d9 100644 --- a/worker/src/index.ts +++ b/worker/src/index.ts @@ -60,7 +60,6 @@ function createSupportHandler(env: AppEnv, logger: ReturnType({ - name: 'support-bootstrap.markers.v1', - key: (value) => bootstrapMarkerKey(value.principalId), - codec: { encode, decode }, - }); -} - -/** The marker store surface the bootstrap needs. */ -export type BootstrapMarkerStore = Pick< - CollectionStore, - 'get' | 'insertIfAbsent' ->; - /** The role-store surface the bootstrap needs. */ export type BootstrapRoleStore = Pick< RoleStore, - 'grantRoleAssignmentWithAudit' + 'listRoleAssignments' | 'grantRoleAssignmentWithAudit' >; /** Parse the bootstrap principal allowlist (trimmed, empties dropped). */ @@ -111,17 +54,12 @@ export function bootstrapSupportAssignmentId(principalId: PrincipalId): string { /** * Seed the `Support` role for a listed principal, ONCE PER PRINCIPAL, EVER. * - * Order matters: the grant lands BEFORE the marker, so a crash between the - * two heals on the next touch (the re-grant converges on the deterministic - * assignment id, then the marker is written), while a marker written ahead - * of a failed grant would strand the principal unseeded forever. - * - * Idempotent and race-tolerant: an existing marker, a replayed grant, or a - * concurrent seed losing on the assignment-id guard all report `'already'`. + * Idempotent and race-tolerant: existing `Support` history, a replayed + * grant, or a concurrent seed losing on the deterministic assignment-id + * guard all report `'already'`. */ export async function ensureBootstrapSupport( roleStore: BootstrapRoleStore, - markers: BootstrapMarkerStore, principalId: PrincipalId, bootstrapPrincipals: ReadonlySet, nowEpochMs: () => number = () => Date.now(), @@ -129,7 +67,13 @@ export async function ensureBootstrapSupport( if (!bootstrapPrincipals.has(principalId)) { return 'not_listed'; } - if ((await markers.get(bootstrapMarkerKey(principalId))) !== null) { + // Full lifecycle history: any Support record — active OR revoked — means + // there is nothing for the one-time seed to do. + const history = await roleStore.listRoleAssignments( + principalId, + APPLICATION_SCOPE, + ); + if (history.some((assignment) => assignment.role === SUPPORT_ROLE)) { return 'already'; } const assignmentId = bootstrapSupportAssignmentId(principalId); @@ -146,17 +90,10 @@ export async function ensureBootstrapSupport( // Deterministic like the assignment id: exact replays are 'unchanged'. auditEventId: `evt-${assignmentId}`, }); - // 'granted'/'unchanged' seeded the assignment. 'conflict' converged too: - // on the assignment id (a pre-marker seed already exists, active or - // revoked) or on the active tuple (`Support` held through another - // assignment — nothing to grant, but the seed is HANDLED and must leave - // a trace so revoking that other assignment can't trigger a reseed). - const heldElsewhere = - result.status === 'conflict' && result.reason === 'active_tuple'; - await markers.insertIfAbsent({ - principalId, - seededAtEpochMs: nowEpochMs(), - outcome: heldElsewhere ? 'held_elsewhere' : 'granted', - }); - return result.status === 'granted' ? 'granted' : 'already'; + if (result.status === 'granted') { + return 'granted'; + } + // 'unchanged' = exact replay; 'conflict' = another request seeded first — + // converged either way. + return 'already'; } diff --git a/worker/src/support-api.test.ts b/worker/src/support-api.test.ts index 05aff33..bf0b5ad 100644 --- a/worker/src/support-api.test.ts +++ b/worker/src/support-api.test.ts @@ -184,7 +184,6 @@ function fixture(options: { roleStore: options.omitRoleStore ? undefined : (options.roleStoreOverride ?? runtime.roleStore), - bootstrapMarkers: runtime.bootstrapMarkers, bootstrapPrincipals: new Set(options.bootstrapPrincipals ?? []), }); return { @@ -192,7 +191,6 @@ function fixture(options: { sessions, runtime, roleStore: runtime.roleStore, - bootstrapMarkers: runtime.bootstrapMarkers, api, application: runtime.application, }; @@ -977,7 +975,7 @@ describe('Support role store adoption (docs/ROLE_ADOPTION_PLAN.md phases 1–3)' }); it('bootstrap seeds a listed principal on an authenticated touch, once, revocation-durable', async () => { - const { api, sessions, roleStore, bootstrapMarkers } = fixture({ + const { api, sessions, roleStore } = fixture({ bootstrapPrincipals: [principalStaff], }); const staff = await sessionCookie(sessions, principalStaff, 'b'.repeat(43)); @@ -1016,7 +1014,6 @@ describe('Support role store adoption (docs/ROLE_ADOPTION_PLAN.md phases 1–3)' expect( await ensureBootstrapSupport( roleStore, - bootstrapMarkers, principalStaff, new Set([principalStaff]), ), @@ -1026,8 +1023,8 @@ describe('Support role store adoption (docs/ROLE_ADOPTION_PLAN.md phases 1–3)' ).toBe(403); }); - it('marks a listed principal already holding Support elsewhere, so revoking that grant cannot be undone by a reseed', async () => { - const { api, sessions, roleStore, bootstrapMarkers } = fixture({ + it('treats existing Support history as seeded, so revoking that grant cannot be undone by a reseed', async () => { + const { api, sessions, roleStore } = fixture({ bootstrapPrincipals: [principalStaff], }); // Support held via a NON-bootstrap assignment before the first touch. @@ -1035,9 +1032,8 @@ describe('Support role store adoption (docs/ROLE_ADOPTION_PLAN.md phases 1–3)' const other = await roleStore.getRoleAssignment(`assign-${principalStaff}`); const staff = await sessionCookie(sessions, principalStaff, 'm'.repeat(43)); - // The touch grants nothing (the store refuses a second active - // assignment per tuple) but still records the handled seed — the host - // marker is the ONLY "already seeded" signal; role state is not one. + // The touch grants nothing: listRoleAssignments already shows Support + // history, which IS the "already seeded" signal (0.3.0 first-class). expect( (await api(get('/api/support/categories', staff.cookie))).status, ).toBe(200); @@ -1046,14 +1042,9 @@ describe('Support role store adoption (docs/ROLE_ADOPTION_PLAN.md phases 1–3)' bootstrapSupportAssignmentId(principalStaff), ), ).toBeNull(); - expect( - await bootstrapMarkers.get({ - partition: 'support-bootstrap', - id: principalStaff, - }), - ).toMatchObject({ principalId: principalStaff, outcome: 'held_elsewhere' }); - // Revoke the held assignment; the next touch must not re-grant. + // Revoke the held assignment; the next touch must not re-grant — + // the REVOKED record still counts as history. const revoked = await roleStore.revokeRoleAssignmentWithAudit({ assignmentId: other!.assignment.id, expectedConcurrencyToken: other!.concurrencyToken, diff --git a/worker/src/support-api.ts b/worker/src/support-api.ts index 56cd184..e45c05e 100644 --- a/worker/src/support-api.ts +++ b/worker/src/support-api.ts @@ -28,7 +28,6 @@ import type { } from './identity-contracts'; import { ensureBootstrapSupport, - type BootstrapMarkerStore, type BootstrapRoleStore, } from './role-bootstrap'; import { @@ -106,8 +105,6 @@ interface SupportApiOptions { readonly roleStore?: SupportRoleReader & BootstrapRoleStore; /** One-time Support bootstrap principals (Phase 3). */ readonly bootstrapPrincipals?: ReadonlySet; - /** Durable bootstrap-seed markers; required for the seed to run. */ - readonly bootstrapMarkers?: BootstrapMarkerStore; } interface SessionData { @@ -442,14 +439,12 @@ async function requireAuthentication( // everyone not listed. if ( options.roleStore !== undefined && - options.bootstrapMarkers !== undefined && options.bootstrapPrincipals !== undefined && options.bootstrapPrincipals.size > 0 ) { try { const seeded = await ensureBootstrapSupport( options.roleStore, - options.bootstrapMarkers, authenticated.link.subject, options.bootstrapPrincipals, ); diff --git a/worker/src/support-desk.ts b/worker/src/support-desk.ts index b1235e7..13dde4c 100644 --- a/worker/src/support-desk.ts +++ b/worker/src/support-desk.ts @@ -11,7 +11,7 @@ import { } from '@pegma/spine'; import { createRoleStore } from '@pegma/authorization-storage'; import { createCloudflareD1Store } from '@pegma/storage-cloudflare-d1'; -import type { CollectionStore, Store } from '@pegma/storage-core'; +import type { Store } from '@pegma/storage-core'; import { createSupportDeskApplication, defaultQueueTerminalRetentionMilliseconds, @@ -26,10 +26,6 @@ import { import { defineTemplate } from '@pegma/support-desk-templates'; import type { IdentityLinkProjector, IdentityPort } from './identity-contracts'; import { AUTHORIZATION_APPLICATION_ID } from './support-access'; -import { - bootstrapMarkerCollection, - type BootstrapMarker, -} from './role-bootstrap'; import type { SessionStore } from '@pegma/sessions'; /** The audited role store surface createRoleStore returns. */ @@ -185,8 +181,6 @@ export interface SupportRuntime { /** Audited role store bound to this host's application partition * (docs/ROLE_ADOPTION_PLAN.md Phase 1). */ readonly roleStore: RoleStore; - /** Durable one-time bootstrap-seed markers (role-bootstrap.ts). */ - readonly bootstrapMarkers: CollectionStore; readonly application: SupportDeskApplication; readonly clock: Clock; readonly createLimiter: DurableRateLimiter; @@ -254,12 +248,10 @@ export function createSupportRuntime( // Audited role store on the SAME Store, partitioned by this host's // application id (docs/ROLE_ADOPTION_PLAN.md Phase 1). const roleStore = createRoleStore(store, AUTHORIZATION_APPLICATION_ID); - const bootstrapMarkers = store.collection(bootstrapMarkerCollection()); return Object.freeze({ store, roleStore, - bootstrapMarkers, application, clock, createLimiter,