Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions docs/ROLE_ADOPTION_PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions docs/SUPPORT_COMPOSITION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand All @@ -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.
Expand Down Expand Up @@ -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-<principalId>`) 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
Expand Down
67 changes: 44 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Comment thread
FlyOverCoderKY marked this conversation as resolved.
Resolved
"@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",
Expand Down
2 changes: 1 addition & 1 deletion src/data/recipe-backlog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 1 addition & 2 deletions worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ function createSupportHandler(env: AppEnv, logger: ReturnType<typeof createAppLo
logger,
// The stored Support ROLE is the only staff gate (fail closed).
roleStore: supportRuntime.roleStore,
bootstrapMarkers: supportRuntime.bootstrapMarkers,
bootstrapPrincipals: parseBootstrapPrincipals(env),
});
return { supportRuntime, api };
Expand Down Expand Up @@ -174,7 +173,7 @@ export default {
storage: 'cloudflare-d1',
sessions: '@pegma/sessions@0.2.0',
runtime: '@pegma/identity@0.1.2',
authorizationAdapter: '@pegma/authorization-identity@0.2.0',
authorizationAdapter: '@pegma/authorization-identity@0.3.0',
emailDelivery:
String(env.IDENTITY_EMAIL_ENABLED) === 'true' &&
Boolean(env.RESEND_API_KEY),
Expand Down
117 changes: 27 additions & 90 deletions worker/src/role-bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import type { PrincipalId } from '@pegma/spine';
import {
defineCollection,
type CollectionStore,
type EntityKey,
type StoredRecord,
} from '@pegma/storage-core';
import { APPLICATION_SCOPE, SUPPORT_ROLE } from './support-access';
import type { RoleStore } from './support-desk';

Expand All @@ -16,75 +10,24 @@ import type { RoleStore } from './support-desk';
* support touch. The env var seeds state; it is never itself an
* authorization path — every later check reads the role store.
*
* "Already seeded" is recorded in a host marker collection, not inferred
* from role state. The role store refuses a second ACTIVE assignment per
* (principal, role, scope) and cannot enumerate revoked assignments
* (authorization-core#24), so no assignment-shaped signal survives every
* case — in particular a listed principal holding `Support` through some
* other assignment would carry no trace, and revoking that assignment
* while the env var lingers would silently re-grant on the next touch.
* The marker is written whenever the seed converges (granted, or nothing
* to grant), making a later revocation durable in all cases. The grant
* itself is human-managed despite its system actor: `system:bootstrap`
* writes once and never touches the assignment again, so it is revocable
* like any human grant.
* "Already seeded" is ANY `Support` assignment record for the principal —
* active or revoked, whatever its provenance — read first-class via
* `listRoleAssignments` (authorization-storage 0.3.0, shipped from
* upstream issue #24; this check previously needed a host marker
* collection because enumeration was active-only). A deliberately revoked
* operator therefore stays revoked even while the env var is still
* configured: history is the durable evidence, and re-listing never
* re-seeds. The grant is human-managed despite its system actor:
* `system:bootstrap` writes once and never touches the assignment again,
* so it is revocable like any human grant.
*
* Delete the env var once the first operator holds the role.
*/

const BOOTSTRAP_MARKER_PARTITION = 'support-bootstrap';

/** Durable "the one-time seed already happened" record for one principal. */
export interface BootstrapMarker {
readonly principalId: string;
readonly seededAtEpochMs: number;
/** Whether the seed granted, or found `Support` already held elsewhere. */
readonly outcome: 'granted' | 'held_elsewhere';
}

function bootstrapMarkerKey(principalId: string): EntityKey {
return { partition: BOOTSTRAP_MARKER_PARTITION, id: principalId };
}

function encode(value: BootstrapMarker): StoredRecord {
return {
principalId: value.principalId,
seededAtEpochMs: value.seededAtEpochMs,
outcome: value.outcome,
};
}

function decode(record: StoredRecord): BootstrapMarker {
return {
principalId:
typeof record['principalId'] === 'string' ? record['principalId'] : '',
seededAtEpochMs:
typeof record['seededAtEpochMs'] === 'number'
? record['seededAtEpochMs']
: 0,
outcome: record['outcome'] === 'held_elsewhere' ? 'held_elsewhere' : 'granted',
};
}

/** The host collection holding bootstrap markers (`support-bootstrap.markers.v1`). */
export function bootstrapMarkerCollection() {
return defineCollection<BootstrapMarker>({
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<BootstrapMarker>,
'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). */
Expand All @@ -111,25 +54,26 @@ 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<string>,
nowEpochMs: () => number = () => Date.now(),
): Promise<'granted' | 'already' | 'not_listed'> {
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);
Expand All @@ -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';
}
Loading