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
35 changes: 35 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
# Unreleased

`@pegma/authorization-storage` gains a library-maintained by-role index, the
remaining storage ask from issue #24:

- `RoleHolderReader.listActiveRoleAssignmentsByRole(role, scope)` lists every
active assignment of one role in one exact scope across principals, ordered
by grant time and then assignment ID. Each result is verified against the
authoritative lifecycle record. It is a new, separate port, so hosts that
implement `RoleAssignmentReader` themselves are unaffected.
- `createRoleStore` and the in-memory adapter maintain the index on every
audited grant using the superset recipe from `docs/STORAGE.md` (write
before grant, never delete, verify on read).
- `RoleHolderIndexMaintenance.rebuildRoleHolderIndex()` backfills rows for
assignments granted before this release. Durable deployments should run it
once after upgrading; until then those assignments are missing from by-role
results, which makes a last-administrator guard refuse rather than allow.

`@pegma/authorization-admin` no longer requires a host `holderIndex` when its
store implements `RoleHolderReader`. Hosts that pass a `holderIndex` keep
the same guard decisions; the store's index is used only when none is given.
The guard re-verifies every candidate by exact ID whichever index supplied it.

Two costs reach every host using `createRoleStore`, including hosts that keep
their own `holderIndex`: `createRoleStore` declares a fourth collection,
`authorization_role_holders`, so a `Store` implementation that provisions
storage per collection name must provision it before upgrading; and every
audited grant performs one additional `insertIfAbsent`. By-role read cost
grows with a role's whole grant history; `docs/STORAGE.md` describes the
shape.

A first-class concurrency-safe last-administrator guard primitive remains
declined: the admin service's serialize, re-verify, and compensate treatment
is still the documented answer.

# Authorization Core 0.4.0

Authorization Core 0.4.0 first advertises `@pegma/authorization-admin`, the
Expand Down
30 changes: 20 additions & 10 deletions docs/ADMINISTRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ configuration, a lesson from the reference host.
**Audited assign.** A grant of a host-policy role — authorized by the HOST
before the call, see the ports section — with a caller-supplied assignment
id, an audit event, and duplicate refusal delegated to the store's
active-tuple guarantee. The holder-index row is written BEFORE the grant
active-tuple guarantee. When the store maintains the by-role index itself
(`RoleHolderReader`, provided by `createRoleStore`), the store writes the
index row inside its own grant path and the service needs no host index.
Otherwise the holder-index row is written BEFORE the grant
through the host port (superset invariant, `docs/STORAGE.md` recipe),
after a pre-check that the tuple is not already active. Dangling rows
still arise — from a crash between the two writes, or from a grant the
Expand Down Expand Up @@ -90,6 +93,11 @@ the common case, and the recovery is documented rather than implied.
an ordered event timeline. No host index involvement; pre-index grants
appear because the store is the authority.

**Guard verification.** Whichever index supplies the candidates — the
store's `RoleHolderReader` or a host index — the service re-reads each one
by exact ID before it counts, so a store whose by-role read is stale can
still only make the guard refuse.

**One-time seed helper.** Both hosts wrote the same bootstrap-adjacent
function; the third copy would be in this package's README, so it belongs
in the package: `ensureSeededAssignment` grants a role once per principal,
Expand Down Expand Up @@ -124,12 +132,12 @@ that document forbids stays forbidden.

## Ports (all constructor-injected, host-owned)

| Port | Provided by | Notes |
| ----------------- | ----------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Role store | `createRoleStore` (authorization-storage) | reads, lifecycle enumeration, audited mutations |
| Holder index | host, per `docs/STORAGE.md` recipe | `record(row)`, `listByRole(role)`; write-before-grant, never delete, verify-on-read. Stays a port until the library-maintained index (#24 follow-up) ships, then becomes optional. |
| Management policy | host configuration | `{ administratorRole, oneTimeSystemActors }`; the guard's compensation actor and the seed helper's actor are included by default |
| Clock | host (`@pegma/spine`) | deterministic tests |
| Port | Provided by | Notes |
| ----------------- | -------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Role store | `createRoleStore` (authorization-storage) | reads, lifecycle enumeration, audited mutations |
| Holder index | optional; host, per `docs/STORAGE.md` recipe | `record(row)`, `listByRole(role)`; write-before-grant, never delete, verify-on-read. Omit it when the store implements `RoleHolderReader` (`createRoleStore` does); a supplied index is always used in preference to the store's. |
| Management policy | host configuration | `{ administratorRole, oneTimeSystemActors }`; the guard's compensation actor and the seed helper's actor are included by default |
| Clock | host (`@pegma/spine`) | deterministic tests |

The service performs no permission checks on its own callers: the HOST
gates its routes with `hasPermission` against its own policy before
Expand All @@ -143,9 +151,11 @@ Tenth package. Same rules as the eighth and ninth: implementation lands
with conformance-style tests against the memory store; the one-time `0.0.0`
name reservation publishes under the non-default `bootstrap` dist-tag;
trusted publishing is configured; the next synchronized `0.x` release is
its first advertised publication. No new storage: every durable record it
touches is either the existing authorization collections or the host's own
index collection reached through the port.
its first advertised publication. The package declares no storage of its
own: every durable record it touches is either the authorization collections
`createRoleStore` declares (including the library-maintained
`authorization_role_holders` index) or the host's own index collection
reached through the port.

## Host adoption order (recorded, executed in the host repos)

Expand Down
8 changes: 8 additions & 0 deletions docs/PROJECT_PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,14 @@ candidate), and a first-class concurrency-safe guard primitive, declined for
now because records for different principals live in different partitions and
the single-partition transaction boundary cannot honestly promise it.

**Library-maintained by-role index (2026-11-02):** the first #24 follow-up
landed. `RoleHolderReader.listActiveRoleAssignmentsByRole` is a separate,
optional port; `createRoleStore` maintains the `authorization_role_holders`
index with the recipe's write-before-grant, never-delete, verify-on-read
shape, and `rebuildRoleHolderIndex` backfills grants made before it existed.
The admin service's host `holderIndex` becomes optional. The first-class
guard primitive remains declined for the reason above.

**Administration-surface extraction (2026-07-30, design under review):** with
both reference hosts having independently built the same audited
role-management surface, the extract-on-second-consumer trigger is met.
Expand Down
74 changes: 62 additions & 12 deletions docs/STORAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,52 @@ authoritative records.

### By-role selection across principals

Neither listing crosses principals. Records are partitioned by principal and
scope so both listings are one-partition reads, which means "every active
holder of role R" has no partition to read and no supported query. A host that
needs it — a role-administration panel, or a last-administrator guard refusing
to revoke the final active `Admin` — must maintain a secondary index, and the
index must fail in the safe direction:
Neither per-principal listing crosses principals. Records are partitioned by
principal and scope so both listings are one-partition reads, which leaves
"every active holder of role R" without a partition of its own.

`RoleHolderReader.listActiveRoleAssignmentsByRole(role, scope)` answers it.
It returns every active assignment of one role in one exact scope, across
principals, ordered by `grantedAtEpochMs` ascending and then by assignment ID.
It is a separate port from `RoleAssignmentReader`, so hosts that implement the
storage ports themselves are not required to provide it; `createRoleStore`
and the in-memory adapter both do.

The provided adapters maintain the index themselves, following the recipe
below: every audited grant writes an immutable holder row (partitioned by
application, role, and exact scope) before the grant transaction, rows are
never deleted, and every read verifies each row against the authoritative
lifecycle record. A revoked assignment, or a grant refused after the pointer
check (an occupied tuple or a different event ID), leaves a row that reads as
nothing; a grant refused at the pointer check writes no row; a stored
assignment is never missed.

Each audited grant therefore costs one more `insertIfAbsent` than before,
outside the grant transaction. Each by-role read is one partition listing plus
one authoritative point read per row, issued in batches of 16. Because rows
are never deleted, the partition grows with every grant attempt for that role
and scope, including revoked and refused ones, so read cost tracks a role's
whole grant history rather than its current holders. That is the right trade
for administrator-style roles granted a handful of times; a role granted
routinely to large populations is a poor fit for by-role reads.

Assignments granted before the index existed (before this release) have no
row. Until `RoleHolderIndexMaintenance.rebuildRoleHolderIndex()` runs once
over the backing store, they are missing from by-role results, which a
last-administrator guard sees as "no other holder" and refuses — fail closed,
but still a refusal an operator would notice. The rebuild reads the
application's assignment pointers (a single-partition scan), writes any
missing rows, reports how many it wrote, and is idempotent and safe to run
alongside live grants and revocations.

The by-role read is a non-snapshot read like every other listing, so the
concurrency caveat below still applies to at-least-one invariants.

#### Recipe for a host-maintained index

A host that implements the storage ports itself, or that predates the
library-maintained index, can keep its own secondary index. It must fail in
the safe direction:

1. Write the index entry **before** the audited grant, so the index can only
over-report, never under-report. A grant refused after its index entry was
Expand All @@ -122,8 +162,10 @@ closed. The check itself is still a non-snapshot read: a guard that verifies
"another active holder exists" and then revokes can race a concurrent
revocation of that other holder. A host enforcing an at-least-one invariant
must re-verify after its own revocation commits and compensate — regrant under
a fresh assignment ID — when the invariant was lost. Whether this index becomes
a library-maintained structure is tracked as Phase 6 integration feedback.
a fresh assignment ID — when the invariant was lost. The library-maintained
index above follows exactly this recipe; a first-class concurrency-safe guard
primitive remains declined, because the single-partition transaction boundary
cannot make a cross-principal invariant atomic.

## Atomic create

Expand Down Expand Up @@ -215,7 +257,7 @@ complete audit history.

## Record layout

`createRoleStore(store, applicationId)` binds three declared collections to one
`createRoleStore(store, applicationId)` binds four declared collections to one
`Store` and one host application.

`authorization_records` holds everything about one principal in one exact
Expand Down Expand Up @@ -248,6 +290,14 @@ written with `insertIfAbsent` before the grant transaction rather than inside
it. A pointer that resolves to nothing reads as absence, which the port already
treats as definitive.

`authorization_role_holders` is the by-role index. Its partition is
`applicationId|role|scopeTag` and each record ID is the encoded assignment ID;
a row carries the assignment's principal, scope, and role. Like a pointer, a
row is immutable and written with `insertIfAbsent` before the grant
transaction, after the pointer check, and it is never deleted. A host whose
`Store` provisions storage per collection name must provision this one before
upgrading.

`authorization_identity_links` maps one exact issuer-and-subject tuple to a
principal. Issuer and subject are stored raw as well as encoded into the record
ID, so a reader confirms the exact case-sensitive tuple rather than trusting
Expand Down Expand Up @@ -297,7 +347,7 @@ partition and would therefore leave the transaction boundary.
## Non-snapshot reads

`listActiveRoleAssignments` and `listRoleAssignments` are each one partition
read of the authoritative assignment records. There is no derived selection
read of the authoritative assignment records. They use no derived selection
index and no fence to compare, so there is nothing that can drift from the
records or that would need reconciling; but a partition listing is not a
snapshot, and a read that begins before a revocation may still observe the
Expand All @@ -315,8 +365,8 @@ deadline, exact binding, invalidation, and stale-fill rules in that guide.

`createInMemoryStorageAdapter` is `createRoleStore` over the storage-core
in-process memory store, bound to one application namespace. It implements
exact principal lookup, identity link writes, role reads, audit reads, and
combined audited grants and revocations. Its returned object deliberately has
exact principal lookup, identity link writes, role reads, by-role reads and
index rebuild, audit reads, and combined audited grants and revocations. Its returned object deliberately has
no
`createRoleAssignment`, raw `revokeRoleAssignment` signature, or
`appendRoleAssignmentAuditEvent` capability outside the combined command
Expand Down
64 changes: 53 additions & 11 deletions docs/api/authorization-admin.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ Options for {@link ensureSeededAssignment}.
```ts
export interface EnsureSeededAssignmentOptions {
readonly store: RoleAdministrationStore;
/** When provided, the superset row is recorded before the grant. */
/**
* The host's own by-role index, when it keeps one: the superset row is
* recorded before the grant. Omit it when the store maintains the
* library's index (`createRoleStore` does) — the store records its own row
* on every audited grant, so a host index would only be a second copy.
*/
readonly holderIndex?: RoleHolderIndex;
readonly principalId: PrincipalId;
readonly role: RoleName;
Expand Down Expand Up @@ -230,6 +235,22 @@ export interface RoleAdministration {
}
```

## RoleAdministrationCommonOptions

**Kind:** interface

Options shared by both {@link RoleAdministrationOptions} shapes.

```ts
export interface RoleAdministrationCommonOptions {
readonly policy: RoleManagementPolicy;
/** Epoch-milliseconds clock; injectable for deterministic tests. */
readonly now?: () => number;
/** Fresh opaque id source for grants and audit events. */
readonly generateId?: () => string;
}
```

## RoleAdministrationEvent

**Kind:** interface
Expand All @@ -249,20 +270,28 @@ export interface RoleAdministrationEvent {

## RoleAdministrationOptions

**Kind:** interface
**Kind:** type

Constructor options for {@link createRoleAdministration}.

Either the store maintains the by-role index itself (preferred), or the
host supplies a {@link RoleHolderIndex}. When a `holderIndex` is given it
is used even if the store could answer by-role queries, so upgrading the
storage package never silently changes which index an existing host's
guard trusts.

```ts
export interface RoleAdministrationOptions {
readonly store: RoleAdministrationStore;
readonly holderIndex: RoleHolderIndex;
readonly policy: RoleManagementPolicy;
/** Epoch-milliseconds clock; injectable for deterministic tests. */
readonly now?: () => number;
/** Fresh opaque id source for grants and audit events. */
readonly generateId?: () => string;
}
export type RoleAdministrationOptions = RoleAdministrationCommonOptions &
Comment thread
FlyOverCoderKY marked this conversation as resolved.
Resolved
(
| {
readonly store: RoleHolderAwareAdministrationStore;
readonly holderIndex?: RoleHolderIndex | undefined;
}
| {
readonly store: RoleAdministrationStore;
readonly holderIndex: RoleHolderIndex;
}
);
```

## RoleAdministrationStore
Expand All @@ -276,6 +305,19 @@ export interface RoleAdministrationStore
extends RoleAssignmentReader, AuditedRoleAssignmentMutationStore {}
```

## RoleHolderAwareAdministrationStore

**Kind:** interface

A store that also maintains the library's by-role index
(`createRoleStore` and `createInMemoryStorageAdapter` both do). With one,
the service needs no host-provided {@link RoleHolderIndex}.

```ts
export interface RoleHolderAwareAdministrationStore
extends RoleAdministrationStore, RoleHolderReader {}
```

## RoleHolderIndex

**Kind:** interface
Expand Down
Loading
Loading