Skip to content
Merged
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
310 changes: 310 additions & 0 deletions doc/2026-11-01_design_id835.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,310 @@
# Design: builder and moderator authority through world-session frames

- Date: 2026-11-01
- Status: **design** — implementation has not started.
- Issue: [#835](https://github.com/developing-today/code/issues/835), F14.6 / F12.3 remainder.
- Scope: session-frame builder installs and moderator revocations only; no implementation changes are included in this record.

## Decision to make

How should a joined builder install a world module and a joined moderator
revoke a participant, such that the live session capability—not a claimed
participant ID—authorizes the action, the actor identity is derived by the
world actor, and successful actions leave the existing journal audit records?
The implementation must also preserve the journal rollback contract, surface
the documented denial code, and avoid granting a builder host or native-process
authority.

## Context and current state

Issue #835 says F12.3 already added `BUILDER` / `MODERATOR`, role-based invites,
and the journal entries `BuilderInstalled` and `Revoked { by }`, but the
session paths still call the `_by` APIs with no actor. It requires real-session
success and guest-denial tests, actor-attributed audit entries, and no new
network, file, or process authority for builders. It also names #227 as a
prerequisite because an emitted `BuilderInstalled` must be covered by the
journal version contract.

The role bits are intentionally narrow: `WorldScopes::BUILDER` and
`MODERATOR` are separate from `ALL`, and the source comments say only the
install and revoke paths read them (`pkgs/id/src/world/mod.rs:119-134`). The
role presets add each bit to the guest scopes (`world/mod.rs:148-165`). The
existing unit/integration test checks that neither role changes
`allows_effect` for chat, time, or records (`pkgs/id/tests/world_roles.rs:62-94`).

The two session operations are not currently joined-session operations:

- `InstallBegin` requires an `admin_token` (`pkgs/id/src/world_session/types.rs:253-265`);
the pre-join handler checks the admin path and streams bounded chunks to
`install_module_session` (`pkgs/id/src/world_session/mod.rs:1124-1154`).
That helper rechecks the admin token, enforces size/hash and upload-slot
bounds, and finally calls the admin-only service installer
(`world_session/mod.rs:1730-1758, 1805-1819`). A joined session rejects
install frames as `already joined` (`world_session/mod.rs:2444-2459`).
- There is no participant-revoke `ClientFrame`. The actor has
`WorldCommand::Revoke { participant_id, by }`; its handler mutates the
revocation tree and then attempts to persist `Revoked { by }`
(`pkgs/id/src/world/actor.rs:1088-1103`). The session's joined loop already
retains the authenticated `JoinCapability` (`world_session/mod.rs:1856-1864,
1934-1939`).

The lower-level pieces exist but do not enforce frame authority. The handle
exposes `revoke_by` and `install_program_by` with optional actor IDs
(`pkgs/id/src/world/handle.rs:570-590, 735-760`); `WorldCore::authorize`
checks the live digest, revocation, effective scopes, and expiry
(`pkgs/id/src/world/core.rs:1113-1139`), but `WorldHandle::participant_id`
currently asks only for `JOIN` (`world/handle.rs:436-446`; actor dispatch at
`pkgs/id/src/world/actor.rs:1105-1107`). A session-side check followed by a
later actor call would therefore leave a revocation/expiry race unless the
actor rechecks the same capability at the mutation boundary.

Journal compatibility is a real gate, not implied by #227 being closed. The
current code requires the version-2 header for `ExecutionProfile`, while all
other variants—including `BuilderInstalled`—default to version 1
(`pkgs/id/src/world/mod.rs:646-683`). `FileJournal::append` bumps the header
before a newly-versioned entry, then appends and syncs each entry separately
(`pkgs/id/src/world_store.rs:177-198`). The current Phase 14 plan says F14.1
assigns `BuilderInstalled` and `Forked` to the first new version and gates
F14.6 on F14.1 (`.opencode/plans/phase-14-close-out.md:76, 115-125, 157-162`).
The latest #835 triage likewise says #227's closure alone does not establish
rollback compatibility for `BuilderInstalled`, and calls for journal fixtures
before enabling the frames. This design therefore treats the BuilderInstalled
version/fixture gap as the first implementation gate; it does not pull in the
separate F12.2 `Forked` work.

There is also an audit atomicity gap to close. A builder install currently
persists `Committed { ProgramInstalled }` and then `BuilderInstalled` as two
separate appends before committing the in-memory event
(`pkgs/id/src/world/actor.rs:1295-1322`; `world_store.rs:193-196`). If the
second append fails, the durable journal can contain the install without its
audit record. Revocation currently applies in memory even on storage failure,
but discards the persistence error (`world/actor.rs:1093-1103`); that
fail-closed security behavior should remain, while a session must not report a
successful, audited revoke when the journal append failed.

The stable F11.13 code for an actor lacking a required scope is
`actor_denied` (`pkgs/id/src/world_caps.rs:26-40, 353-372, 422-447`). The
session `ServerFrame::Error` currently has only `message`
(`pkgs/id/src/world_session/types.rs:383-386`), so the implementation needs a
backwards-compatible way to carry that code for these denials rather than
inventing another code.

## Viable options

### 1. Authority source

| Option | Trade-off |
| --- | --- |
| Keep the admin-token-only frame | Safest short-term and leaves today's wire untouched, but does not meet #835 or make builder/moderator roles usable over sessions. |
| Accept a client-supplied `participant_id` as the actor | Small wire/API change, but the caller can claim another participant and bypass the capability as proof of authority. Reject. |
| Derive actor from the already-joined capability and authorize in the world actor | Reuses the session's authenticated bearer capability, applies expiry/revocation/scope checks at the serialized mutation boundary, and records the actual actor. Recommend. |

### 2. Where the operations live

| Option | Trade-off |
| --- | --- |
| Add builder/moderator credentials to the pre-join admin-only paths | Preserves one-shot upload structure but turns participant authority into another client-supplied credential and does not naturally cover a live session's revocation race. Reject. |
| Make install and revoke joined-session frames | The joined loop already owns the live token. Add a joined `revoke` frame and allow `install_begin` without an admin token only in that joined context; keep existing pre-join admin install behavior. Recommend. |
| Add a separate builder upload protocol and keep the existing admin protocol | Explicitly distinguishes callers but duplicates upload framing, bounds, and tests. Do not duplicate: use the existing `install_begin`/chunk/end sequence, with context deciding which authority is required. |

For compatibility, make `InstallBegin.admin_token` optional on the wire: existing
admin clients that send a string continue to use the pre-join admin path; a
joined builder omits it and uses the session capability. A joined frame that
also supplies an admin token is rejected rather than mixing authority sources.
The new `Revoke` frame carries only the target participant ID; it never carries
an actor ID or a replacement capability. Revoke and install remain unavailable
to a plain guest.

### 3. Authorization timing

| Option | Trade-off |
| --- | --- |
| Check `BUILDER` / `MODERATOR` once in the session layer, then call `*_by` | Easy to wire, but a capability can expire or be revoked between the check and actor mutation; the `_by` methods accept caller-provided attribution and do not revalidate authority. Reject. |
| Check only when the actor begins the operation | Closes the revoke TOCTOU for moderator actions, but not for builder uploads that spend time receiving, compiling, validating, or pinning bytes before installation. Reject as incomplete. |
| Check before accepting expensive work and recheck the same capability inside the serialized actor command that commits the effect | Early rejection avoids unauthenticated uploads; final check prevents expired/revoked builders from activating a module and makes revoke authorization plus target mutation one serialized decision. Recommend. |

The session may derive the participant ID for display/metrics, but that value is
not authorization. The actor must call `authorize(token, BUILDER)` or
`authorize(token, MODERATOR)` itself and use the returned ID for `by`. The
builder check runs before accepting upload bytes and again after upload,
validation/compilation, and pinning, at activation. The moderator check and
target revoke run in the same actor command. Revocation during an upload must
make the final install check fail.

### 4. Audit and journal format

| Option | Trade-off |
| --- | --- |
| Keep two separate appends for `Committed` and `BuilderInstalled` | Minimal code, but an I/O failure can persist only one half of the successful-install record. Reject. |
| Record only `BuilderInstalled` and infer/rebuild the install event from it | One record could be atomic, but changes the existing replay meaning and conflates the audit record with the world event. Not recommended for this remainder. |
| Add an atomic journal transaction for the install event plus `BuilderInstalled`, preserving both entry meanings | Uses the existing entries and guarantees all-or-neither durability for the pair; the transaction can be implemented as an atomic journal replacement using the file journal's temp/sync/rename pattern. Recommend despite the rare install's O(journal-size) write. |

Before any reachable builder path can write `BuilderInstalled`, the journal
must require the existing version-2 header for that variant, bumping older
journals atomically before the write. Also detect and upgrade on open any
legacy version-1 journal that already contains `BuilderInstalled`: the current
open path scans entries for their required version and bumps legacy files
(`pkgs/id/src/world_store.rs:143-154`), so extending the mapping covers both
future writes and existing files. Keep ordinary version-1 journals readable
and leave journals without this entry at their existing version. Add a
cross-version fixture proving the prior reader refuses the version-2 journal
at the header, and an injected-failure test proving the install event and audit
record are never split. This is the F14.1 prerequisite exposed by the current
source, not a new journal policy for every unrelated entry.

For revocation, preserve the existing security rule that an active target is
revoked in memory and disconnected even if storage has failed. However, surface
the persistence failure and do not acknowledge the session request as a
successful audited operation. On the normal path, the `Revoked { by }` append
must succeed before the frame reports success. No-op revokes of inactive or
unknown targets do not create a false “revoked” record; failed attempts are
outside the journal's state-transition audit and belong to the later structured
audit work (F14.21), if required.

### 5. Denial code and native execution

Reuse `actor_denied` for invalid, expired, revoked, or insufficient joined
capabilities; do not add a new F11.13 code. Add an optional `code` to error
frames and omit it for existing errors so their serialized shape remains
unchanged; these two scope denials return `code: "actor_denied"`.

Builder authority must remain install-only. Do not route a builder through the
native installer even when the host enables native execution: the existing
installer can launch a native program when `self.native` is enabled
(`pkgs/id/src/world_session/mod.rs:538-588`). Builder installs are Wasm-only;
admin behavior is unchanged. The role bit does not add world capability grants
or ceiling entries, and the existing effect-scope assertions remain required.

## Recommendation

Implement the joined-session option with actor-derived identity, an early and
final scope check, a new moderator `revoke` frame, and the existing upload
sequence for builder installs. Preserve the admin pre-join path. Make the
BuilderInstalled journal version and atomic install/audit transaction a hard
gate before the session path is enabled. Return the established
`actor_denied` code for role denials, reject native modules from builder installs,
and preserve immediate in-memory revocation if durability fails while reporting
that failure instead of claiming an audited success.

This is the smallest design that meets #835 without trusting caller-supplied
actor identity, creating a time-of-check/time-of-use gap, or permitting a
half-written install audit. It reuses the existing role model, upload bounds,
admin flow, and journal entry meanings.

## Invariants

1. **Identity comes from the live capability.** No frame-supplied actor ID is
used for authorization or audit attribution.
2. **Authority is checked at the effect boundary.** The serialized world actor
checks the capability's current scope, revocation state, and expiry when it
installs or revokes; install also has an early check before receiving bytes.
3. **A successful builder install is Wasm-only.** Builder authority adds no
network, file, process, provider, grant, or ceiling authority; native
installation remains admin-only.
4. **Success implies durable attribution.** A successful install has an
all-or-neither durable `Committed` + `BuilderInstalled { by }` transaction;
a successful effective revoke has a durable `Revoked { by }` entry.
5. **Rollback is explicit.** `BuilderInstalled` is protected by the journal
version contract before the new session path can emit it; older binaries
reject the bumped journal at its header.
6. **Denials are stable and non-oracular.** Role/capability denials use
`actor_denied` without exposing whether a token was expired, revoked, or
lacked the bit.
7. **Existing admin and guest behavior stays bounded.** Admin-only compile,
native execution, source-only policy, module size/hash validation, upload
concurrency, and current F11.13 meanings are not widened by this change.

## Acceptance criteria for implementation

- A builder joining with a live `BUILDER` capability can install a valid Wasm
module through the real session upload sequence; the journal records the
install event and `BuilderInstalled { by: <derived participant> }` atomically.
- A moderator joining with a live `MODERATOR` capability can revoke an active
participant through a joined `revoke` frame; the journal contains
`Revoked { participant_id, by: Some(<derived participant>) }` before success
is reported, and the revoked session is disconnected.
- A plain guest is refused for both actions with wire code `actor_denied`; an
unknown, expired, or revoked capability is also refused with that code.
- A caller cannot set its own audit actor by adding an actor field or choosing
another participant ID.
- Revoking a builder during upload/validation/pinning prevents final activation
and produces no install audit or active-program change.
- Builder install rejects native artifacts even if native execution is enabled;
tests continue to prove the builder bit does not change `allows_effect` or
world grants/ceilings.
- A version-1 journal is upgraded atomically before its first
`BuilderInstalled`, and any legacy version-1 journal already containing
`BuilderInstalled` is upgraded on open; version-1 data remains readable, an
old reader refuses the upgraded header before entry replay, and a failed
transaction cannot leave only one half of the install/audit pair.
- If a revoke's journal write fails, the target is still denied in memory and
disconnected, the world remains read-only as the existing journal contract
requires, and the session receives a failure rather than an audited-success
response.
- Existing admin pre-join install behavior, guest behavior, upload size/hash
bounds, upload concurrency limit, source-only refusal, and F11.13 code
meanings remain unchanged.

## Phased implementation plan

Each step can be reviewed and merged independently; the session path is not
enabled until the journal gate is complete.

1. **Journal gate and transaction primitive.** Require version 2 for
`BuilderInstalled` using the existing atomic header bump; add version mapping
documentation and v1/v2 compatibility fixtures. Add an atomic multi-entry
journal operation for the `Committed` + `BuilderInstalled` pair and failure
injection coverage. Do not expose new frames in this step. **Done when:**
successful and failed transactions replay all-or-neither; a legacy v1
journal already containing `BuilderInstalled` is bumped on open; and a v1
reader refuses the upgraded journal at the header.
2. **Actor-authorized world operations.** Add actor commands/APIs that accept
the live `JoinCapability`, check `BUILDER` / `MODERATOR` in the serialized
actor, derive the audit actor ID, and perform the final install/revoke check
with the state change. Preserve fail-closed in-memory revocation and surface
journal failure. Add actor-level tests for role, expiry, revocation, audit
attribution, and write failures. **Done when:** no session path can create
a successful `_by` action from an unverified ID, and the journal records
match the authorized actor.
3. **Joined-session frames and end-to-end proof.** Make `InstallBegin` accept
an omitted admin token only in joined builder sessions; add joined
`revoke { participant_id }`; retain pre-join admin install and reject mixed
admin/session credentials. Carry optional `actor_denied` on error frames,
enforce builder Wasm-only behavior, and add real-session tests for builder,
moderator, guest, expiry/revocation races, journal attribution, and unchanged
admin/upload safeguards. **Done when:** every #835 acceptance criterion
passes through `SessionIo` rather than only the handle API.

Dependencies: 1 → 2 → 3. The Phase 14 plan's separate `Forked` versioning work
and future F14.21 structured audit log are not prerequisites for enabling this
issue's two role actions; F14.21 remains the home for failed-attempt audit.

## Questions for Drew

None. The wire shape, denial behavior, rollback gate, failure behavior, and
native-install boundary follow the issue's stated acceptance criteria, the
latest #835 security triage, and the current Phase 14 sequencing. The technical
defaults above are explicit so implementation does not need to wait for a
product decision.

## References

- Issue [#835](https://github.com/developing-today/code/issues/835), including
the acceptance criteria and latest security triage comment.
- Issue [#227](https://github.com/developing-today/code/issues/227) closeout and
`pkgs/id/src/world/mod.rs:646-683` for the current journal version contract.
- `.opencode/plans/phase-14-close-out.md:76, 102-109, 115-125, 157-162` for
F14.1/F14.6 ordering, narrow role authority, and the future audit work.
- `pkgs/id/src/world/mod.rs:119-165, 509-529` for role scope boundaries and
existing audit entry shapes.
- `pkgs/id/src/world/core.rs:1113-1139`,
`pkgs/id/src/world/handle.rs:436-446, 570-590, 735-760`, and
`pkgs/id/src/world/actor.rs:1088-1103, 1295-1322` for authorization, actor
attribution, and current persistence order.
- `pkgs/id/src/world_session/types.rs:253-265, 383-386`,
`pkgs/id/src/world_session/mod.rs:1124-1154, 1730-1844, 1856-1864,
1934-1948, 2444-2459`, and `pkgs/id/src/world_caps.rs:26-40, 353-447` for
session framing, upload safeguards, and the stable `actor_denied` code.
- `pkgs/id/src/world_store.rs:177-253` for append, version bump, and atomic
replacement behavior; `pkgs/id/tests/world_roles.rs:62-153` for current role
and actor-attribution tests.