Files
aevolution/docs/adr/0182-admin-portal-and-world-tuning.md
T
dingoandClaude Opus 5.5 b1cc3b121b
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / postgres-tools (push) Successful in 30s
Foundation / server-image (push) Successful in 4m44s
Foundation / image (push) Successful in 5m33s
feat(admin): live admin commands in trace 5 (schema 63)
The admin portal can now spawn wildlife, add bubbles, add to or release the
spent pool, remove a cell and grant a cell's reserve on a running rules-13
region (ADR-0182 section 10). No new owner format or region opening.

- Trace 5: trace 4 plus 64-byte command records counted in the tick header's
  last word; a batch without commands stays trace 4 (older builds replay it).
  Bundle 15 admits trace 5.
- CellularLives::admin_candidate applies a tick's commands after its births,
  each on a copy kept only when it applies and closes: imports through the
  boundary ledger, removals into the spent pool with a new `removed` corpse
  cause, spawned roots via the ADR-0180 seed (now a shared helper) funded by
  an exact import. Refused commands change nothing.
- Schema 63: the immutable admin_commands queue (spawn identities from the
  life allocator), pending/history reads, and binding of every recorded
  command exactly once inside both cellular publishers; the SQL trace parsers
  read trace 5 through its trace-4 normal form.
- The region polls pending commands every 2 s and appends up to 16 per tick;
  the admin API and portal gain region actions, per-cell Remove/Grant and the
  command history.
- Fixes an existing test that did not advance its head after a tick commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 17:14:04 -04:00

246 lines
15 KiB
Markdown

# ADR-0182: Admin portal, world tuning in SQL and per-tick amendments
## Status and scope
Accepted direction, 2026-10-27, from the maintainer's decisions in chat; implementation
pending. DEV (world 100) only; stage and World0 need their own review. Applies to a new owner
format and new openings only:
regions 12-16 keep their recorded rules and Git-mounted tuning. Supersedes, for that new
format, ADR-0181's "Git tuning affects future openings only. No live retune."
## Context
Every balance change so far has meant a Git tuning edit, a new image or ConfigMap and a new
DEV region (regions 14, 15 and 16 in a week). The maintainer wants an admin portal to review
accounts, act on regions and tune the cellular game while it runs. This is a game, not a
pure simulation: designer-controlled resource sources are acceptable when they are named and
counted.
The constraint is determinism. A region's history must replay to the same state, so nothing
may change a running region except an event recorded at a tick.
## Decision
### 1. World tuning lives in SQL
- Schema 63 adds append-only `world_tuning_revisions(world, revision, parent_revision,
format, canonical_text, digest, author_subject, reason, created_at)`. A revision is the
complete canonical tuning text (`encode_cellular_revamp_tuning`), never a diff, so the
existing parser and validator are the only interpretation.
- All regions of a world share its tuning and mutation definitions (maintainer decision).
Per-region values are not offered.
- Revisions are immutable. Undo is a new revision whose text equals an earlier one.
- Git keeps the seed file for creating a world and the test fixtures. It is not consulted
by a running region, and Argo no longer carries tuning for regions on the new format.
### 2. Amendments are recorded per tick
- A region polls for a newer world revision on its existing database connection, bounded
and off the tick path (at most once a second).
- It validates the revision with its own parser. If it can parse it, it adopts it at its next
tick boundary and records `tuning_adopted(revision, digest)` at that tick in its own
history, published with that tick. If it cannot, for example because the revision uses
keys its image does not know, it keeps its current revision and reports `tuning_refused`
with the revision number. The portal shows each region's adopted revision.
- Replay and recovery read the recorded revision by number and digest, never "the latest".
- Amendments change future calculations only. Heritable values already in a genome are not
rewritten (for example, a new `mutation.min_points` does not change existing traits).
- Every tuning key is classified **live** or **opening-only**. Opening-only keys (geometry
and size, tick rate, world/frame, liquid profile structure) can be revised, but a running
region ignores them and the portal labels them "next opening".
### 3. Admin commands
- Schema 63 adds `admin_commands(world, region, command_id, kind, payload, author_subject,
reason, created_at, applied_tick, outcome)`. The admin API inserts and the region host
drains them at tick boundaries in id order, publishing each with that tick. This is the
same pattern as the durable mutation queue (schema 59).
- Kinds:
- `kill_cell(life)`: the cell dies with an "admin" outcome. Its body and reserve go to
the spent pool and drip back.
- `grant_reserve(life, inventory)`: a **game-source import** into that cell's reserve.
- `add_region_bubbles(inventory, count)`: a game-source import as bubbles at random
spots, at most the region's bubble limit per tick; the rest follows on later ticks.
- `add_pool(inventory)`: a game-source import into the spent pool.
- `release_pool(inventory)`: moves pool matter into the region through the drip settings
below, so a large release is spread over drips, never one tick.
- **Game source** is a named import in the region's accounting, like emergence is a named
export. Conservation still holds: every unit is either from the starting stock, imported
by a recorded source or exported by a recorded sink.
### 4. Drip and source settings
The spent-pool drip (today: interval, largest energy bubble, largest matter bubble per
channel) becomes one settings group used by both the pool drip and the game-source spawn:
| Setting | Meaning |
|---|---|
| `drip.interval_ticks` | how often a drip happens (0: never) |
| `drip.bubbles_per_drip` | the most bubbles one drip creates |
| `drip.bubble_max.<channel>` | the most of that channel one bubble may carry (energy pj; carbon, nitrogen, phosphorus, other fg) |
| `drip.scale_percent.<channel>` | per channel, how much faster or slower that channel is released |
| `source.per_drip.<channel>` | game-source import per drip and channel (0: none) |
For the pool, scale changes how quickly the pool drains; it never releases more than the
pool holds. For the source, scale multiplies the import.
### 5. Data-defined mutations
- Mutation definitions move from the compiled catalog into the world revision:
`mutation.<key>.*` gains the definition itself (name, description, effect, primary and
paired traits, steps, costs, triggers and requirements), not only overrides.
- A definition may use only effects, traits, triggers and requirement predicates the image
implements. Requirements come from a fixed predicate set (carbon burned, buds made, a
trait at or above a value, a mutation held). New behaviour needs code and a new image;
the region then refuses revisions using it until promoted.
- The per-cell affinity scores become a vector keyed by mutation id. New entries start at
zero. This is part of the new owner format.
- Mutations are never deleted from a running world. `enabled = 0` stops new offers;
applied mutations stay applied because their effect already changed the genome.
Replacement is disable plus a new definition.
- Session protocol: the client receives definitions (name, description, costs) instead of
using compiled names.
### 6. Accounts
- **Status**: logged in (an open session interval), banned, alive or dead, the region of
the active life, and current mutations (applied mutation results along the life's line).
- **Ban**: `account_bans(account, author_subject, reason, created_at, lifted_at)`. The grant
broker refuses a banned account (`403 account_banned`) and the ban runs the existing
logout (`revoke_account_grants`), so a live session closes within 5 s.
- **Log out**: the existing `revoke_account_grants`.
- **Grant resource**: `grant_reserve` against the account's active life.
- **Death count**: the account's lives with a death outcome.
- **Reset count**: the account's founder births after its first (a new life after death).
- **Living offspring**: living wildlife descended from any of the account's lives, using
the parent links kept since format 5.
- **Time played**: `session_intervals(account, region, started_at, ended_at)` written by the
region host on session open and close. Intervals still open at a region start are closed
at that start. History begins with this schema; earlier play is not recoverable.
### 7. Admin API and portal
- `evo-admin-api`: a C++ service built like the grant broker. It verifies Keycloak access
tokens against the realm keys pinned in Git, requires audience `aevolution-admin`,
authorized party `aevolution-admin-portal` and realm role `aevolution-admin`, and serves
one configured world. Its component exists only in the evolution-dev overlay; World0 and
stage have no admin portal (World0 has no fixed world id to refuse in code).
- Every write records the token subject and a reason.
- The region view: the region host's internal ops port gains a bounded snapshot route (cell
id, position, extent, wildlife/player and account, bubbles), reachable only from the admin
API by NetworkPolicy. The admin API serves it to the portal at up to 1 Hz.
- The portal is a static browser application at `https://srs.aeternalisgames.com`
(section **Cellular** now, **3D** later) using authorization code with PKCE. It is plain
HTML, CSS and JavaScript with no third-party code, so no dependency evaluation is needed.
- Keycloak (maintainer setup): realm role `aevolution-admin`; public client
`aevolution-admin-portal` with standard flow and PKCE S256, redirect URI
`https://srs.aeternalisgames.com/*`, web origin `https://srs.aeternalisgames.com` and
post-logout redirect `https://srs.aeternalisgames.com/`; an audience mapper adding
`aevolution-admin`; MFA required for the role.
### 8. Read-only portal (delivered first, schema 61)
- **Schema 61** adds three bounded, read-only SECURITY DEFINER functions, executable by the
runtime group and listed in the privilege audit: `admin_accounts_v1(world)` (every account
with its identity, session control, lives, deaths, active life and mutation-choice count),
`admin_account_lives_v1(world, account)` (its life requests and endings) and
`admin_regions_v1(world)` (leases and heads). No table or existing function changes. The
runtime group still has no table privilege. DEV's admin API logs in with its own login in
that group, as the grant broker does; a dedicated read role is a later least-privilege step.
- **Derived values.** A life is a published, unrefused reservation; a death is an ended one;
resets are lives after the first; logged in means the session control has not expired.
- **Region view.** `GET /admin/view` on the cellular region's ops port renders the committed
in-memory owner (`render_cellular_admin_view`): every cell (life and organism ids as
strings, account, footprint, extent, wildlife/connected/lingering/suspended flags,
members, reserve, body mass) and every pool and bubble, bounded to 4,096 cells and 16,384
deposits with totals. It reads without changing anything; the ops server's response
bound becomes configurable (4 MiB for this port), default 16 KiB elsewhere.
- **Portal files and headers.** The image ships the portal under
`/usr/local/share/evo-admin-portal`. Files go out with a strict Content-Security-Policy
(`'self'` scripts and styles, connections only to this origin and the realm), `nosniff`,
`no-referrer` and `DENY` framing; every answer is `no-store`. The access token stays in
page memory and the rotating refresh token in the tab's sessionStorage.
- **Not yet available** (they need the schema-63 records): time played, bans, current
mutations and living offspring.
### 9. Session windows and the admin role (schema 62)
The dev session window moves from Git to SQL and is opened, extended and closed from the
portal; see the [ADR-0087 amendment](0087-dev-session-route.md). Schema 62 also moves the
admin functions to a dedicated, operator-created NOLOGIN group role (`aevolution_admin` in
DEV, `EVO_PG_ADMIN_ROLE` for the migration): every migration run grants it exactly the admin
functions and audits it like the runtime group (no table privilege, no privileged
attribute, no membership outside it, not the runtime role). Schema 61's read views are
revoked from the runtime group. The admin API logs in with its own login in that group.
Writes go through POST without a body (`?minutes=M&reason=R`); the bearer token in the
`Authorization` header, never a cookie, is the only credential.
### 10. Admin commands (delivered, schema 63, trace 5)
The commands of section 3 are delivered without a new owner format or region opening:
- **Trace 5.** A rules-13 batch may record operator commands: trace 4 plus 64-byte records
(id, kind, count, target, energy and four matter amounts) counted in the tick header's last
word, which trace 4 keeps zero. A batch without commands stays trace 4, so builds before
this one still replay it; only commits that carry commands need this build or later.
Bundle 15 admits trace 5 (`cellular_checkpoint_trace_admits`); the manifest is unchanged.
- **Application.** `CellularLives::admin_candidate` applies a tick's commands after its
births, by ascending id, each on a copy kept only if it applies and still closes:
`remove_cell` (body and reserve to the spent pool, a corpse with the new `removed` cause;
a player's life then ends as any death does), `grant_reserve`, `add_bubbles` and
`add_pool` (imports in the boundary ledger), `release_pool` (moves at most what the pool
holds into bubbles) and `spawn_wildlife` (1-64 randomized founder roots, the ADR-0180 seed
with an exact import for their reserve, body and construction). A command whose target is
gone, or that does not fit (region cap, deposit limit, a taken identity), is refused and
changes nothing.
- **Queue and binding.** `admin_enqueue_command_v1` (admin role) stores the exact command;
spawning reserves its identities from the world's life allocator. The region reads
`load_pending_admin_commands_v1` every 2 s and appends up to 16 per tick. Both cellular
publishers bind every recorded command to its queue row exactly once, in the publishing
transaction; a differing or already-applied command refuses the publication. The SQL trace
parsers read a trace 5 through its trace-4 normal form, after binding the real trace to
the manifest.
- **Portal.** Region actions (spawn wildlife, add bubbles, add to or release the spent pool),
per-cell Remove and Grant reserve, and the command history with the tick each applied at.
Amounts are entered in µJ and ng.
Still pending from this ADR: drip/source settings (section 4), tuning amendments
(sections 1-2), data-defined mutations (section 5), bans and time played (section 6).
## Delivery order
1. Read-only portal: login, accounts (from existing data), regions, region view (schema 61,
section 8).
2. Schema 63 and the new owner format: world revisions, adoption records, admin commands,
game source, drip settings, the score vector, session intervals and bans. One new
opening on that format.
3. Admin commands and account actions in the portal.
4. Live tuning in the portal.
5. Data-defined mutations in the portal.
## Deferred
- **Cell migration between regions.** With world-shared tuning and mutations it no longer
meets per-region mismatches, but still needs the paired transfer extended to the new
format and to player lives, and a client handoff.
- **New matter types.** `accounting::Element` has four channels throughout accounting,
traits, bubbles, the protocol and the client. A later decision; a fixed number of matter
slots is the expected approach.
## Alternatives
- **Export portal changes to Git and reopen regions.** Rejected: more steps for the same
result, and not live.
- **Change values in a running region's memory.** Rejected: replay and recovery would diverge.
- **Per-region tuning.** Rejected: all regions of a world share rules.
- **Database push instead of polling.** Not needed at one adoption check per second.
## Consequences
- Balance changes become data: no image, ConfigMap or region per change.
- Balance history moves from Git review to the revision table (author, reason, full text).
It is covered by database backups and history domains (ADR-0155).
- Argo no longer shows tuning drift for new-format regions; the portal shows each region's
adopted revision instead.
- One more opening is required, for the new owner format.