From b1cc3b121bc61b7bf0043322a6e3c2d024ab8e9d Mon Sep 17 00:00:00 2001 From: dingo Date: Tue, 27 Oct 2026 17:14:04 -0400 Subject: [PATCH] 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 --- apps/evo-admin-api/portal/app.js | 98 ++++- apps/evo-admin-api/portal/style.css | 8 +- apps/evo-server/main.cpp | 24 +- docs/STATUS.md | 9 + .../adr/0182-admin-portal-and-world-tuning.md | 32 ++ docs/engineering/VALIDATION.md | 33 ++ include/evo/biology/cellular_region_step.hpp | 3 +- include/evo/persistence/region_checkpoint.hpp | 8 + include/evo/server/admin/admin_api.hpp | 8 + include/evo/server/cellular_region_host.hpp | 6 + include/evo/server/cellular_session_batch.hpp | 13 +- .../evo/server/lifecycle/cellular_batch.hpp | 15 +- .../evo/server/lifecycle/cellular_lives.hpp | 37 ++ .../evo/server/lifecycle/cellular_revamp.hpp | 7 + migrations/0063_admin_commands.sql | 341 +++++++++++++++++ migrations/apply.sql | 25 +- src/persistence/cellular_checkpoint_store.cpp | 8 +- src/server/admin/admin_api.cpp | 100 ++++- src/server/cellular_region_host.cpp | 68 ++++ src/server/lifecycle/cellular_batch.cpp | 86 ++++- src/server/lifecycle/cellular_lives.cpp | 354 +++++++++++++----- .../lifecycle/cellular_play_summary.cpp | 1 + src/server/lifecycle/cellular_revamp.cpp | 43 ++- src/server/session/cellular_session_batch.cpp | 34 +- tests/cellular_inheritance_replay_tests.cpp | 4 +- tests/cellular_publication_tests.cpp | 92 +++++ tests/cellular_revamp_owner_tests.cpp | 95 +++++ tests/grant_broker_tests.cpp | 37 ++ tests/region_migration_tests.cpp | 3 +- tools/ci/history-domain-check.sh | 19 + tools/ci/postgres-check.sh | 10 +- tools/migrate-postgres.sh | 4 +- 32 files changed, 1487 insertions(+), 138 deletions(-) create mode 100644 migrations/0063_admin_commands.sql diff --git a/apps/evo-admin-api/portal/app.js b/apps/evo-admin-api/portal/app.js index 8555b07..170b72a 100644 --- a/apps/evo-admin-api/portal/app.js +++ b/apps/evo-admin-api/portal/app.js @@ -349,6 +349,86 @@ function sessionWindowCard(region) { return { card, load }; } +// Amounts typed in friendly units: energy in µJ (10^6 pJ), matter in ng (10^6 fg). +function amountInputs() { + const make = (label) => h('input', { type: 'number', min: 0, step: 'any', placeholder: label, class: 'amount' }); + const fields = { energy: make('energy µJ'), carbon: make('C ng'), nitrogen: make('N ng'), phosphorus: make('P ng'), other: make('other ng') }; + const read = () => { + const out = {}; + for (const [name, input] of Object.entries(fields)) { + const v = Number(input.value || 0); + if (!Number.isFinite(v) || v < 0) throw new Error(`${name} must be a positive number`); + out[name] = String(Math.round(v * 1e6)); + } + return out; + }; + const clear = () => Object.values(fields).forEach((input) => { input.value = ''; }); + return { node: h('div', { class: 'amounts' }, Object.values(fields)), read, clear }; +} + +const kindLabels = { + remove_cell: 'remove cell', grant_reserve: 'grant reserve', add_bubbles: 'add bubbles', + add_pool: 'add to pool', release_pool: 'release pool', spawn_wildlife: 'spawn wildlife', +}; + +// Admin commands (ADR-0182): queued now, applied by the region at its next ticks. +function commandsCard(region) { + const note = h('div', { class: 'muted small' }); + const history = h('div'); + const reason = h('input', { type: 'text', maxlength: 200, placeholder: 'Reason (optional)' }); + async function send(params, label) { + try { + const q = new URLSearchParams(params); + if (reason.value.trim()) q.set('reason', reason.value.trim()); + const r = await api(`/api/regions/${region}/commands?${q}`, true, 'POST'); + note.textContent = `Queued #${r.id} (${label}). The region applies it within a few seconds.`; + reason.value = ''; + load(); + return true; + } catch (e) { + note.textContent = `Could not queue ${label}: ${e.code || e.message}`; + return false; + } + } + const spawnCount = h('input', { type: 'number', min: 1, max: 64, value: 10, class: 'count' }); + const bubbleCount = h('input', { type: 'number', min: 1, max: 256, value: 20, class: 'count' }); + const bubbleAmount = amountInputs(); + const poolAmount = amountInputs(); + const releaseCount = h('input', { type: 'number', min: 1, max: 256, value: 20, class: 'count' }); + const releaseAmount = amountInputs(); + const withAmount = (amount, extra, label) => { + let values; + try { values = amount.read(); } catch (e) { note.textContent = e.message; return; } + send({ ...extra, ...values }, label).then((ok) => { if (ok) amount.clear(); }); + }; + async function load() { + try { + const data = await api(`/api/regions/${region}/commands`); + history.replaceChildren(data.commands.length ? h('table', { class: 'compact' }, h('tbody', {}, data.commands.slice(0, 10).map((c) => + h('tr', {}, h('td', { class: 'mono' }, `#${c.id}`), h('td', {}, kindLabels[c.kind] || c.kind, + c.count ? ` ×${c.count}` : '', c.kind === 'remove_cell' || c.kind === 'grant_reserve' ? ` ${c.target}` : ''), + h('td', {}, c.applied_tick ? h('span', { class: 'badge alive' }, `tick ${c.applied_tick}`) : h('span', { class: 'badge warn' }, 'pending')), + h('td', { class: 'muted', title: c.author || '' }, (c.author || '').split(' (')[0]))))) : h('p', { class: 'muted' }, 'No commands yet.')); + } catch (e) { + history.textContent = `Unavailable: ${e.message}`; + } + } + const card = h('div', { class: 'card' }, + h('h2', {}, 'Region actions'), + h('div', { class: 'action' }, h('strong', {}, 'Spawn wildlife'), h('div', { class: 'row' }, spawnCount, + h('button', { onclick: () => send({ kind: 'spawn_wildlife', count: spawnCount.value }, 'spawn wildlife') }, 'Spawn'))), + h('div', { class: 'action' }, h('strong', {}, 'Add bubbles'), bubbleAmount.node, h('div', { class: 'row' }, bubbleCount, h('span', { class: 'muted' }, 'bubbles'), + h('button', { onclick: () => withAmount(bubbleAmount, { kind: 'add_bubbles', count: bubbleCount.value }, 'add bubbles') }, 'Add'))), + h('div', { class: 'action' }, h('strong', {}, 'Add to spent pool'), poolAmount.node, + h('div', { class: 'row' }, h('button', { onclick: () => withAmount(poolAmount, { kind: 'add_pool' }, 'add to pool') }, 'Add to pool'))), + h('div', { class: 'action' }, h('strong', {}, 'Release spent pool as bubbles'), releaseAmount.node, + h('div', { class: 'row' }, releaseCount, h('span', { class: 'muted' }, 'bubbles'), + h('button', { onclick: () => withAmount(releaseAmount, { kind: 'release_pool', count: releaseCount.value }, 'release pool') }, 'Release'))), + reason, note, h('h2', {}, 'Recent commands'), history); + load(); + return { card, load, send }; +} + // ---------- Region view ---------- const channelNames = ['energy', 'carbon', 'nitrogen', 'phosphorus', 'other']; const css = (name) => getComputedStyle(document.documentElement).getPropertyValue(`--${name}`).trim(); @@ -365,6 +445,7 @@ async function regionPage(region, params) { const toggle = (k, label) => h('label', {}, h('input', { type: 'checkbox', checked: true, onchange: (e) => { show_[k] = e.target.checked; paint(); } }), label); const live = h('input', { type: 'checkbox', checked: true }); const sessions = sessionWindowCard(region); + const commands = commandsCard(region); let view = null, selected = null, timer = null, beats = 0; let zoom = 1, panX = 0, panY = 0, drag = null; @@ -377,6 +458,7 @@ async function regionPage(region, params) { h('div', { class: 'side' }, sessions.card, detail, + commands.card, h('div', { class: 'card' }, h('h2', {}, 'Show'), h('div', { class: 'toggles' }, toggle('players', 'Player cells'), toggle('wildlife', 'Wildlife'), toggle('pools', 'Pools'), toggle('bubbles', 'Bubbles'))), h('div', { class: 'card' }, h('h2', {}, 'Legend'), h('div', { class: 'legend' }, @@ -480,9 +562,22 @@ async function regionPage(region, params) { } function showDetail(c) { selected = c; + const grant = amountInputs(); + const remove = () => { + if (!confirm(`Remove ${c.flags & 1 ? 'wildlife' : 'player'} cell ${c.life}? Its body and reserve go to the spent pool.`)) return; + commands.send({ kind: 'remove_cell', target: c.life }, `remove cell ${c.life}`); + }; + const give = () => { + let values; + try { values = grant.read(); } catch (e) { return alert(e.message); } + commands.send({ kind: 'grant_reserve', target: c.life, ...values }, `grant reserve to ${c.life}`) + .then((ok) => { if (ok) grant.clear(); }); + }; detail.replaceChildren(h('h2', {}, c.flags & 1 ? 'Wildlife cell' : 'Player cell'), h('dl', { class: 'facts' }, cellLines(c).flatMap(([k, v]) => [h('dt', {}, k), h('dd', { class: k === 'Life' || k === 'Account' ? 'mono' : null }, v)])), - c.account ? h('p', {}, h('a', { href: `#/accounts/${c.account}` }, 'Open account →')) : null); + c.account ? h('p', {}, h('a', { href: `#/accounts/${c.account}` }, 'Open account →')) : null, + h('div', { class: 'action' }, h('strong', {}, 'Grant reserve'), grant.node, + h('div', { class: 'row' }, h('button', { onclick: give }, 'Grant'), h('button', { class: 'danger', onclick: remove }, 'Remove cell')))); paint(); } @@ -562,6 +657,7 @@ async function regionPage(region, params) { if (live.checked && !document.hidden) { await load(); if (++beats % 10 === 0) sessions.load(); + if (beats % 5 === 0) commands.load(); } tick(); }, 1000); diff --git a/apps/evo-admin-api/portal/style.css b/apps/evo-admin-api/portal/style.css index 96fc17e..7cdfa75 100644 --- a/apps/evo-admin-api/portal/style.css +++ b/apps/evo-admin-api/portal/style.css @@ -58,7 +58,7 @@ button { button:hover { border-color: var(--accent); } button.primary { background: var(--accent); border-color: var(--accent); color: #fff; } button.ghost { background: transparent; } -input[type="search"], input[type="text"], select { +input[type="search"], input[type="text"], input[type="number"], select { font: inherit; color: var(--text); background: var(--panel); @@ -188,3 +188,9 @@ dl.facts dd { margin: 0; } table.compact { margin-top: 10px; font-size: 12.5px; background: transparent; } table.compact td { padding: 4px 6px; white-space: normal; } button:disabled { opacity: 0.5; cursor: default; } +.action { border-top: 1px solid var(--line); padding-top: 10px; margin-top: 10px; } +.action .row { display: flex; gap: 8px; align-items: center; margin-top: 6px; flex-wrap: wrap; } +.amounts { display: grid; grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); gap: 6px; margin-top: 6px; } +input.amount, input.count { min-width: 0; width: 100%; } +input.count { width: 80px; } +button.danger { color: var(--bad); border-color: var(--bad); } diff --git a/apps/evo-server/main.cpp b/apps/evo-server/main.cpp index f9a5123..4996de5 100644 --- a/apps/evo-server/main.cpp +++ b/apps/evo-server/main.cpp @@ -1338,8 +1338,19 @@ int serve_cellular(Options& o) { }); const std::span mutations = unbound; + // ADR-0182: queued admin commands not already in this batch. + auto admin = host.unbound_admin_commands(); + std::erase_if(admin, [&](const auto& queued) { + return std::ranges::any_of( + batch_builder->batch().ticks, [&](const auto& previous) { + return std::ranges::find(previous.admin, queued.id, + &server::CellularAdminCommand::id) != + previous.admin.end(); + }); + }); + const auto admin_before = batch_builder->admin_results().size(); auto appended = batch_builder->append_with_birth_refusals( - tick, baseline, actions, births, mutations); + tick, baseline, actions, births, mutations, admin); if (!appended && !actions.empty()) { // ADR-0127: one account's unusable action (its life died, or it // names an unperceived patch) refuses only that action, never the @@ -1354,7 +1365,7 @@ int serve_cellular(Options& o) { bridge->dropped(refused, true); actions = std::move(usable); appended = batch_builder->append_with_birth_refusals( - tick, baseline, actions, births, mutations); + tick, baseline, actions, births, mutations, admin); } if (!appended) { const auto lives = batch_builder->last_lives_error(); @@ -1370,7 +1381,14 @@ int serve_cellular(Options& o) { actions.end()); batch_actions.insert(batch_actions.end(), actions.begin(), actions.end()); - const bool prompt = !births.empty() || !mutations.empty(); + const auto results = batch_builder->admin_results(); + for (std::size_t i = admin_before; i < results.size(); ++i) + std::printf("cellular admin command=%llu tick=%llu %s\n", + static_cast(results[i].id), + static_cast(tick), + results[i].applied ? "applied" : "refused"); + const bool prompt = + !births.empty() || !mutations.empty() || !admin.empty(); if (prompt || batch_builder->batch().ticks.size() >= flush_ticks) flush(); } diff --git a/docs/STATUS.md b/docs/STATUS.md index f28fd86..3c3499b 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -1,5 +1,14 @@ # Project status and next steps +## Admin commands: spawn wildlife, resources, remove and grant - 2026-10-27 + +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, live. Commands are queued in SQL (schema 63) and +the region applies each once, recording it in its history (trace 5) so replay and recovery +reproduce it. Imports are counted in the region's boundary ledger. No new region is needed. +See [ADR-0182](adr/0182-admin-portal-and-world-tuning.md) section 10 and +[validation](engineering/VALIDATION.md). + ## Session windows from the admin portal - 2026-10-27 The dev session window now lives in SQL (schema 62) instead of Git: the admin portal opens, diff --git a/docs/adr/0182-admin-portal-and-world-tuning.md b/docs/adr/0182-admin-portal-and-world-tuning.md index 9cdf04b..45b6c42 100644 --- a/docs/adr/0182-admin-portal-and-world-tuning.md +++ b/docs/adr/0182-admin-portal-and-world-tuning.md @@ -175,6 +175,38 @@ revoked from the runtime group. The admin API logs in with its own login in that 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, diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 5e6b5aa..f35183c 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -10508,3 +10508,36 @@ Limits: no live region has read a SQL window yet, and no process test drives the window through a real session; the elapsed-time guard of the 2026-10-08 clock amendment is still pending. Applying it needs the DEV `aevolution_admin` role and the admin portal's own login first. + +## 2026-10-27 - Admin commands in trace 5 (schema 63) + +Scope: ADR-0182 section 10. Operators queue commands in the admin portal (remove a cell, +grant a reserve, add bubbles, add to or release the spent pool, spawn wildlife); the +cellular region applies each once after a tick's births and records it in trace 5, which +bundle 15 admits beside trace 4. Publication binds each recorded command to its queue row +in the same transaction. No owner format, rules, manifest bundle, genome, wire version or +golden changed; a corpse may carry the new `removed` cause (refused by older readers). +The ADR-0180 seeding loop moved into a shared helper with identical draws. + +Executed (WSL Ubuntu, GCC 14.2 Release/NDEBUG, build/m97-postgres): + +- `cellular-revamp-owner-contracts`: new case applying all six kinds plus a refused one + (missing target): results, closure through the boundary ledger, the exact spent pool, + bubble counts, the removed corpse, spawned wildlife, restore, determinism, trace-5 + round trip and replay equality, trace 4 refusing commands; the existing starting-wildlife + and trace cases still pass after the seeding refactor. +- `tools/ci/postgres-check.sh --full` passed (637 s, every phase). New PostgreSQL cases: on + the starting-wildlife region, commands queued through `admin_enqueue_command_v1` (spawn + identities from the life allocator), a tampered trace-5 batch refused by the binding, the + exact batch published with three bindings at its tick and no pending commands left, + recovery equal to the applied state, and a second publication of the same commands + refused; admin API command endpoints (shape refusals, unknown region, author/reason, + history order, runtime role limited to the pending read, rows immutable). +- An existing test bug surfaced: the starting-wildlife publication test did not advance its + head after its tick commit; it now does before the command batch. +- clang-tidy 19 (warnings as errors) on the changed sources after narrowing the command kind + to a byte (the trace keeps a u16 and refuses values over 255); clang-format 19.1.5; + `node --check` on the portal script. + +Limits: no live region has applied a command yet; the portal's command forms were not +driven in a browser. diff --git a/include/evo/biology/cellular_region_step.hpp b/include/evo/biology/cellular_region_step.hpp index fa12872..8ec6fe1 100644 --- a/include/evo/biology/cellular_region_step.hpp +++ b/include/evo/biology/cellular_region_step.hpp @@ -120,7 +120,8 @@ enum class CellularDeathCause : std::uint8_t { starved = 1, engulfed = 2, merged = 3, - emerged = 4 + emerged = 4, + removed = 5 // ADR-0182: an operator removed the cell (rules 13). }; struct CellularCorpseCandidate { std::uint64_t life_id{}; diff --git a/include/evo/persistence/region_checkpoint.hpp b/include/evo/persistence/region_checkpoint.hpp index 54fa73f..7270efd 100644 --- a/include/evo/persistence/region_checkpoint.hpp +++ b/include/evo/persistence/region_checkpoint.hpp @@ -118,6 +118,14 @@ cellular_checkpoint_trace_format(const CellularVersionBundle& b) noexcept { : server::cellular_batch_v2_format; } +// ADR-0182: a bundle-15 commit may carry admin commands in trace 5; every other format is +// exactly the bundle's. +[[nodiscard]] constexpr bool cellular_checkpoint_trace_admits(const CellularVersionBundle& b, + std::uint32_t format) noexcept { + return format == cellular_checkpoint_trace_format(b) || + (format == server::cellular_batch_admin_format && b == cellular_v15_versions); +} + // Batch evidence bound by manifest v2 (ADR-0063); all zero at genesis. struct BatchDigests { std::uint64_t trace_bytes{}; diff --git a/include/evo/server/admin/admin_api.hpp b/include/evo/server/admin/admin_api.hpp index 0aca1d1..7e89765 100644 --- a/include/evo/server/admin/admin_api.hpp +++ b/include/evo/server/admin/admin_api.hpp @@ -31,6 +31,11 @@ // opens or extends the window for M (1..240) minutes // from now, or closes it (M = 0), as the signed-in admin // (ADR-0087 amendment). No request body. +// GET /api/regions//commands the region's last admin commands and where they applied +// POST /api/regions//commands?kind=K[&count=N][&target=ID][&energy=PJ][&carbon=FG] +// [&nitrogen=FG][&phosphorus=FG][&other=FG][&reason=R] +// queues a command (remove_cell, grant_reserve, +// add_bubbles, add_pool, release_pool, spawn_wildlife) namespace evo::server::admin { struct AdminAsset { std::string path; // "/index.html", ...; "/" serves "/index.html". @@ -64,6 +69,9 @@ class AdminApi { [[nodiscard]] OpsResponse regions(); [[nodiscard]] OpsResponse view(std::uint64_t region, std::chrono::steady_clock::time_point now); [[nodiscard]] OpsResponse window(std::uint64_t region); + [[nodiscard]] OpsResponse commands(std::uint64_t region); + [[nodiscard]] OpsResponse enqueue(std::uint64_t region, std::string_view query, + const broker::AccessTokenClaims& admin); [[nodiscard]] OpsResponse set_window(std::uint64_t region, std::string_view query, const broker::AccessTokenClaims& admin); [[nodiscard]] std::optional query(const std::string& sql, diff --git a/include/evo/server/cellular_region_host.hpp b/include/evo/server/cellular_region_host.hpp index 85eb364..770bdb9 100644 --- a/include/evo/server/cellular_region_host.hpp +++ b/include/evo/server/cellular_region_host.hpp @@ -152,6 +152,9 @@ class CellularRegionHost { } // Queued choices not carried by the commit in flight: the open batch binds these. [[nodiscard]] std::vector unbound_mutations() const; + // ADR-0182: queued admin commands (rules 13) not carried by the commit in flight, oldest + // first, at most one tick's worth. Publication binds each to its queue row once. + [[nodiscard]] std::vector unbound_admin_commands() const; [[nodiscard]] std::vector take_finalized_mutations(); // v5 requests allocate durably, then wait for the scheduler to include the exact // reservation in a profile-4 batch. Repeating the request is idempotent. @@ -207,6 +210,7 @@ class CellularRegionHost { void finalize_births(std::span births); void queue_birth(const CellularTickIntents::Birth& birth); void reload_mutations(); + void reload_admin_commands(); // ADR-0127: owned lives that died record their ending so the account may start again. void note_endings(const CellularLives& before, const CellularLives& after, const persistence::Uuid& commit); @@ -285,6 +289,8 @@ class CellularRegionHost { std::vector queued_births_; std::vector pending_birth_refusals_; std::vector queued_mutations_; + std::vector queued_admin_; + std::chrono::steady_clock::time_point next_admin_reload_{}; // A reservation may exist that the queue does not show; reload before publishing. bool mutations_uncertain_{}; std::vector finalized_mutations_; diff --git a/include/evo/server/cellular_session_batch.hpp b/include/evo/server/cellular_session_batch.hpp index 7728a69..f53f4a3 100644 --- a/include/evo/server/cellular_session_batch.hpp +++ b/include/evo/server/cellular_session_batch.hpp @@ -58,7 +58,8 @@ class CellularSessionBatchBuilder { append(std::uint64_t tick, std::span baseline, std::span actions, std::span births = {}, - std::span mutations = {}); + std::span mutations = {}, + std::span admin = {}); // Scheduler path: expected placement/funding/existing-life refusals are reported // separately, so they cannot prevent other births or the ordinary tick. Unexpected // failures retain append's all-or-nothing behavior. Persist reports only after publish. @@ -66,13 +67,18 @@ class CellularSessionBatchBuilder { std::uint64_t tick, std::span baseline, std::span actions, std::span births = {}, - std::span mutations = {}); + std::span mutations = {}, + std::span admin = {}); [[nodiscard]] std::span refused_births() const noexcept { return refused_births_; } [[nodiscard]] const CellularBatch& batch() const noexcept { return batch_; } + // ADR-0182: each admin command appended so far and whether it applied, in trace order. + [[nodiscard]] std::span admin_results() const noexcept { + return admin_results_; + } [[nodiscard]] std::span actions() const noexcept { return actions_; } @@ -106,7 +112,7 @@ class CellularSessionBatchBuilder { std::span actions, std::span births, std::span mutations, - bool allow_refusals); + std::span admin, bool allow_refusals); explicit CellularSessionBatchBuilder(std::unique_ptr working) : working_{std::move(working)} {} std::unique_ptr working_; @@ -114,6 +120,7 @@ class CellularSessionBatchBuilder { std::vector actions_; std::vector mutation_actions_; std::vector refused_births_; + std::vector admin_results_; std::optional last_lives_error_; }; } // namespace evo::server diff --git a/include/evo/server/lifecycle/cellular_batch.hpp b/include/evo/server/lifecycle/cellular_batch.hpp index 5a7ea72..40245a9 100644 --- a/include/evo/server/lifecycle/cellular_batch.hpp +++ b/include/evo/server/lifecycle/cellular_batch.hpp @@ -22,6 +22,8 @@ struct CellularBatchHeader { inline constexpr std::uint32_t cellular_batch_format = 3; // ADR-0166: explicit opt-in for owner format 8/rules 10; the default remains 3. inline constexpr std::uint32_t cellular_batch_inheritance_format = 4; +// ADR-0182: trace 4 plus admin commands, for rules 13 only. A batch without commands stays 4. +inline constexpr std::uint32_t cellular_batch_admin_format = 5; inline constexpr std::uint32_t cellular_batch_v2_format = 2; inline constexpr std::uint32_t cellular_batch_legacy_format = 1; struct CellularTickIntents { @@ -38,6 +40,8 @@ struct CellularTickIntents { // Choices accepted against the preceding checkpoint and scheduled for this tick. // The successor identity is durably reserved before the batch is replayed. std::vector mutations; + // Format 5 (ADR-0182): operator commands applied after the births, by ascending id. + std::vector admin{}; friend bool operator==(const CellularTickIntents&, const CellularTickIntents&) = default; }; // Transient scheduler report, not part of the replay trace. A refused birth changes @@ -81,13 +85,17 @@ inline constexpr std::size_t cellular_batch_tick_header_bytes = 16; inline constexpr std::size_t cellular_batch_intent_bytes = 24; inline constexpr std::size_t cellular_batch_birth_bytes = 40; inline constexpr std::size_t cellular_batch_mutation_bytes = 40; +// Format 5: id u64, kind u16, count u16, zero u32, target u64, energy and four matter u64. +inline constexpr std::size_t cellular_batch_admin_bytes = 64; +inline constexpr std::size_t max_cellular_tick_admin_commands = 16; inline constexpr std::size_t max_cellular_batch_ticks = 64; inline constexpr std::size_t max_cellular_batch_bytes = cellular_batch_header_bytes + max_cellular_batch_ticks * (cellular_batch_tick_header_bytes + biology::max_cellular_tick_lives * (cellular_batch_intent_bytes + cellular_batch_birth_bytes + - cellular_batch_mutation_bytes)); + cellular_batch_mutation_bytes) + + max_cellular_tick_admin_commands * cellular_batch_admin_bytes); enum class CellularBatchError : std::uint8_t { format, version, scope, state }; inline constexpr std::size_t cellular_action_record_bytes = 48; inline constexpr std::size_t max_cellular_action_records = 1'024; @@ -118,6 +126,11 @@ struct CellularMutationOutcome { [[nodiscard]] std::expected, CellularBatchError> replay_cellular_batch(const CellularLives& base, const CellularBatch& batch, std::vector& mutation_outcomes); +// ADR-0182: also reports every admin command's result in trace order. +[[nodiscard]] std::expected, CellularBatchError> +replay_cellular_batch(const CellularLives& base, const CellularBatch& batch, + std::vector& mutation_outcomes, + std::vector& admin_results); // The canonical 34-byte reserved choice (schema v29): tick, life, event and offer tick as // u64 and the operator as u16, little-endian. inline constexpr std::size_t cellular_mutation_input_bytes = 34; diff --git a/include/evo/server/lifecycle/cellular_lives.hpp b/include/evo/server/lifecycle/cellular_lives.hpp index eeb0e03..59abae2 100644 --- a/include/evo/server/lifecycle/cellular_lives.hpp +++ b/include/evo/server/lifecycle/cellular_lives.hpp @@ -179,6 +179,33 @@ struct CellularLifeIntent { biology::CellularStepIntent intent{}; auto operator<=>(const CellularLifeIntent&) const = default; }; +// ADR-0182 admin commands: operator inputs recorded in the trace (batch format 5) and applied +// after a tick's births, in ascending id. Imports enter the boundary ledger as a named game +// source; removed matter goes to the spent pool. A command whose target is gone or that does +// not fit is refused and changes nothing; the others still apply. +enum class CellularAdminKind : std::uint8_t { // A u16 in the trace. + remove_cell = 1, // `target` life: body and reserve go to the spent pool. + grant_reserve = 2, // `amount` imported into the `target` life's reserve. + add_bubbles = 3, // `amount` imported as `count` (1..256) bubbles sharing it. + add_pool = 4, // `amount` imported into the spent pool. + release_pool = 5, // Up to `amount` moved from the spent pool into `count` bubbles. + spawn_wildlife = 6, // `count` (1..64) random-genome roots, identities `target`.., imported. +}; +struct CellularAdminCommand { + std::uint64_t id{}; // The durable queue id. + CellularAdminKind kind{}; + std::uint16_t count{}; + std::uint64_t target{}; + accounting::Inventory amount{}; + friend bool operator==(const CellularAdminCommand&, const CellularAdminCommand&) = default; +}; +// Shape only (kind, counts, amounts, identity range); whether it applies is decided at its tick. +[[nodiscard]] bool cellular_admin_command_valid(const CellularAdminCommand& command) noexcept; +struct CellularAdminResult { + std::uint64_t id{}; + bool applied{}; + friend bool operator==(const CellularAdminResult&, const CellularAdminResult&) = default; +}; struct CellularMutationSelection { std::uint64_t life_id{}, event_index{}, offer_tick{}, successor_id{}; std::uint16_t operator_id{}; @@ -412,6 +439,11 @@ class CellularLives { [[nodiscard]] std::expected advance_tick_with_mutations(std::span intents, std::span selections); + // ADR-0182: a detached candidate after `commands` (valid, strictly ascending ids) at the + // current tick, after its births. Rules 13 only. `results` reports each command, in order. + [[nodiscard]] std::expected, CellularLivesError> + admin_candidate(std::span commands, + std::vector* results = nullptr) const; private: friend class CellularMigration; @@ -432,6 +464,11 @@ class CellularLives { migration_replacement(std::vector, const biology::LineageArchive&, CellularBoundaryLedger) const; struct State; + // ADR-0180/0182: one randomized founder root `id` placed at a seeded free spot, funded + // from the nursery, or (with `imported`) by a game-source import added to it. + [[nodiscard]] static std::optional + seed_wildlife_root(State& next, std::uint64_t world, std::uint64_t region, std::uint64_t tick, + std::uint64_t id, accounting::Inventory* imported); explicit CellularLives(std::unique_ptr state); std::unique_ptr state_; std::vector last_tick_feedback_; diff --git a/include/evo/server/lifecycle/cellular_revamp.hpp b/include/evo/server/lifecycle/cellular_revamp.hpp index d5e135a..c46bafe 100644 --- a/include/evo/server/lifecycle/cellular_revamp.hpp +++ b/include/evo/server/lifecycle/cellular_revamp.hpp @@ -121,6 +121,13 @@ cellular_mutation_cost(const biology::CellularMutationTuning& tuning) noexcept; const CellularRevampRules& rules, std::uint64_t world, std::uint64_t region, std::uint64_t tick, std::uint64_t source); +// ADR-0182 admin bubbles: `amount` as `count` bubbles, each channel split evenly (the remainder +// in the last), at seeded random spots nudged east past taken addresses, with the next deposit +// ids and `source`. False, with `config` unchanged, when the habitat cannot take them all. +[[nodiscard]] bool cellular_scatter_bubbles(biology::CellularHabitatConfig& config, + const accounting::Inventory& amount, + std::uint16_t count, std::uint64_t seed, + std::uint64_t source); // A body's handling carrying reserve matter: thrust, reverse thrust and turning scale by // body / (body + load), full-output costs by (body + load) / body, where load is the declared // share of `carried` matter. Drag is unchanged. No load leaves the handling as it is. diff --git a/migrations/0063_admin_commands.sql b/migrations/0063_admin_commands.sql new file mode 100644 index 0000000..ae25612 --- /dev/null +++ b/migrations/0063_admin_commands.sql @@ -0,0 +1,341 @@ +-- Admin commands (ADR-0182, 2026-10-27). The admin portal queues operator commands for a +-- region: remove a cell, grant a reserve, add bubbles, add to or release the spent pool, and +-- spawn wildlife. The region applies each once, after a tick's births, and records it in the +-- trace (format 5: trace 4 plus 64-byte command records counted in the tick header's last +-- word), so replay and recovery reproduce it. Publication binds every recorded command to +-- its queue row exactly once. A bundle-15 trace without commands stays trace 4; the existing +-- parsers read a trace 5 through its trace-4 normal form, so nothing else changes. + +-- u64 values as little-endian bytes. +CREATE FUNCTION evolution.le_bytes(v numeric, n integer) +RETURNS bytea LANGUAGE plpgsql IMMUTABLE SET search_path = pg_catalog, pg_temp AS $$ +DECLARE out bytea := ''::bytea; x numeric := v; +BEGIN + FOR i IN 1 .. n LOOP + out := out || set_byte('\x00'::bytea, 0, (x % 256)::integer); + x := floor(x / 256); + END LOOP; + RETURN out; +END $$; +REVOKE ALL ON FUNCTION evolution.le_bytes(numeric, integer) FROM PUBLIC; + +-- The trace-4 normal form of a trace 5: version 4, command counts zero, records dropped. +CREATE FUNCTION evolution.cellular_trace_without_admin(t bytea) +RETURNS bytea LANGUAGE plpgsql IMMUTABLE SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + ticks integer; + off integer := 112; + out bytea; + records integer; + admin integer; +BEGIN + IF t IS NULL OR octet_length(t) < 112 OR evolution.manifest_uint(t, 8, 4) <> 5 THEN + RETURN t; + END IF; + ticks := evolution.manifest_uint(t, 104, 4)::integer; + out := overlay(substring(t FROM 1 FOR 112) PLACING '\x04000000'::bytea FROM 9 FOR 4); + FOR i IN 1 .. ticks LOOP + IF octet_length(t) < off + 16 THEN + RAISE EXCEPTION 'invalid admin trace' USING ERRCODE = '22023'; + END IF; + records := evolution.manifest_uint(t, off, 4)::integer * 24 + + evolution.manifest_uint(t, off + 4, 4)::integer * 40 + + evolution.manifest_uint(t, off + 8, 4)::integer * 40; + admin := evolution.manifest_uint(t, off + 12, 4)::integer; + IF admin > 16 OR octet_length(t) < off + 16 + records + admin * 64 THEN + RAISE EXCEPTION 'invalid admin trace' USING ERRCODE = '22023'; + END IF; + out := out || substring(t FROM off + 1 FOR 12) || '\x00000000'::bytea + || substring(t FROM off + 17 FOR records); + off := off + 16 + records + admin * 64; + END LOOP; + IF off <> octet_length(t) THEN + RAISE EXCEPTION 'invalid admin trace' USING ERRCODE = '22023'; + END IF; + RETURN out; +END $$; +REVOKE ALL ON FUNCTION evolution.cellular_trace_without_admin(bytea) FROM PUBLIC; + +-- A trace 5's command records with the tick each applies at, in trace order. +CREATE FUNCTION evolution.cellular_trace_admin(t bytea, m bytea) +RETURNS TABLE (tick evolution.u64, command_id bigint, kind integer, cell_count integer, + target evolution.u64, energy evolution.u64, carbon evolution.u64, nitrogen evolution.u64, + phosphorus evolution.u64, other evolution.u64) +LANGUAGE plpgsql IMMUTABLE SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(m); + ticks integer; + off integer := 112; + records integer; + admin integer; + previous numeric; +BEGIN + IF t IS NULL OR octet_length(t) < 112 OR evolution.manifest_uint(t, 8, 4) <> 5 THEN + RETURN; + END IF; + ticks := evolution.manifest_uint(t, 104, 4)::integer; + FOR tick_index IN 0 .. ticks - 1 LOOP + records := evolution.manifest_uint(t, off, 4)::integer * 24 + + evolution.manifest_uint(t, off + 4, 4)::integer * 40 + + evolution.manifest_uint(t, off + 8, 4)::integer * 40; + admin := evolution.manifest_uint(t, off + 12, 4)::integer; + off := off + 16 + records; + previous := 0; + FOR i IN 1 .. admin LOOP + tick := c.begin_tick + tick_index + 1; + command_id := evolution.manifest_uint(t, off, 8)::bigint; + kind := evolution.manifest_uint(t, off + 8, 2)::integer; + cell_count := evolution.manifest_uint(t, off + 10, 2)::integer; + target := evolution.manifest_uint(t, off + 16, 8); + energy := evolution.manifest_uint(t, off + 24, 8); + carbon := evolution.manifest_uint(t, off + 32, 8); + nitrogen := evolution.manifest_uint(t, off + 40, 8); + phosphorus := evolution.manifest_uint(t, off + 48, 8); + other := evolution.manifest_uint(t, off + 56, 8); + IF command_id <= previous OR substring(t FROM off + 13 FOR 4) <> '\x00000000'::bytea THEN + RAISE EXCEPTION 'invalid admin trace' USING ERRCODE = '22023'; + END IF; + previous := command_id; + off := off + 64; + RETURN NEXT; + END LOOP; + END LOOP; +END $$; +REVOKE ALL ON FUNCTION evolution.cellular_trace_admin(bytea, bytea) FROM PUBLIC; + +-- The trace parsers read a bundle-15 trace 5 through its normal form, after binding the real +-- trace to the manifest (as schema 55 did for retained plans). +CREATE FUNCTION evolution.cellular_trace_normal(INOUT t bytea, INOUT m bytea) +LANGUAGE plpgsql IMMUTABLE SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(m); + b15 constant bytea := '\x0d0000000b00000002000000010000000100000007000000060000000200000001000000'; +BEGIN + IF t IS NULL OR octet_length(t) < 12 OR evolution.manifest_uint(t, 8, 4) <> 5 THEN + RETURN; + END IF; + IF c.versions <> b15 OR octet_length(t) <> c.trace_bytes OR sha256(t) <> c.trace_sha256 THEN + RAISE EXCEPTION 'invalid admin trace binding' USING ERRCODE = '22023'; + END IF; + t := evolution.cellular_trace_without_admin(t); + m := overlay(m PLACING evolution.le_bytes(octet_length(t), 8) FROM 281 FOR 8); + m := overlay(m PLACING sha256(t) FROM 289 FOR 32); +END $$; +REVOKE ALL ON FUNCTION evolution.cellular_trace_normal(bytea, bytea) FROM PUBLIC; + +ALTER FUNCTION evolution.cellular_births(bytea,bytea) RENAME TO cellular_births_v62; +REVOKE ALL ON FUNCTION evolution.cellular_births_v62(bytea,bytea) FROM PUBLIC, :"runtime_role"; +CREATE FUNCTION evolution.cellular_births(t bytea, m bytea) +RETURNS TABLE(account_id uuid, request_id evolution.u64, generation evolution.u64, + identity evolution.u64, born evolution.u64) +LANGUAGE plpgsql IMMUTABLE SET search_path = pg_catalog, pg_temp AS $$ +DECLARE n record := evolution.cellular_trace_normal(t, m); +BEGIN + RETURN QUERY SELECT * FROM evolution.cellular_births_v62(n.t, n.m); +END $$; +REVOKE ALL ON FUNCTION evolution.cellular_births(bytea,bytea) FROM PUBLIC; + +ALTER FUNCTION evolution.cellular_mutation_selections(bytea,bytea) + RENAME TO cellular_mutation_selections_v62; +REVOKE ALL ON FUNCTION evolution.cellular_mutation_selections_v62(bytea,bytea) + FROM PUBLIC, :"runtime_role"; +CREATE FUNCTION evolution.cellular_mutation_selections(p_trace bytea, p_manifest bytea) +RETURNS TABLE (tick evolution.u64, life_id evolution.u64, event_index evolution.u64, + offer_tick evolution.u64, successor_identity evolution.u64, operator_id integer) +LANGUAGE plpgsql IMMUTABLE SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$ +DECLARE n record := evolution.cellular_trace_normal(p_trace, p_manifest); +BEGIN + RETURN QUERY SELECT * FROM evolution.cellular_mutation_selections_v62(n.t, n.m); +END $$; +REVOKE ALL ON FUNCTION evolution.cellular_mutation_selections(bytea,bytea) FROM PUBLIC; + +-- The queue. A row is the operator's exact command; its application is recorded separately. +CREATE TABLE evolution.admin_commands ( + command_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + world_id evolution.u64 NOT NULL, + region_id evolution.u64 NOT NULL, + kind smallint NOT NULL CHECK (kind BETWEEN 1 AND 6), + cell_count integer NOT NULL CHECK (cell_count BETWEEN 0 AND 256), + target evolution.u64 NOT NULL, + energy evolution.u64 NOT NULL, + carbon evolution.u64 NOT NULL, + nitrogen evolution.u64 NOT NULL, + phosphorus evolution.u64 NOT NULL, + other evolution.u64 NOT NULL, + author_subject text NOT NULL CHECK (octet_length(author_subject) BETWEEN 1 AND 255), + reason text NOT NULL DEFAULT '' CHECK (octet_length(reason) <= 500), + created_at timestamptz NOT NULL DEFAULT clock_timestamp(), + FOREIGN KEY (world_id, region_id) REFERENCES evolution.region_leases +); +CREATE TRIGGER admin_commands_immutable BEFORE UPDATE OR DELETE ON evolution.admin_commands + FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite(); +CREATE TRIGGER admin_commands_no_truncate BEFORE TRUNCATE ON evolution.admin_commands + FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite(); +CREATE TABLE evolution.admin_command_applications ( + command_id bigint PRIMARY KEY REFERENCES evolution.admin_commands, + commit_id uuid NOT NULL, + commit_sequence evolution.u64 NOT NULL, + tick evolution.u64 NOT NULL +); +CREATE TRIGGER admin_command_applications_immutable BEFORE UPDATE OR DELETE + ON evolution.admin_command_applications + FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite(); + +-- Queues a command (the same shapes the owner accepts). Spawning takes its identities from the +-- world's life allocator, so they never meet a player's. +CREATE FUNCTION evolution.admin_enqueue_command_v1(p_world evolution.u64, p_region evolution.u64, + p_kind integer, p_count integer, p_target evolution.u64, p_energy evolution.u64, + p_carbon evolution.u64, p_nitrogen evolution.u64, p_phosphorus evolution.u64, + p_other evolution.u64, p_subject text, p_reason text) +RETURNS TABLE (command_id bigint, target evolution.u64) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +#variable_conflict use_column +DECLARE + has_amount boolean := p_energy + p_carbon + p_nitrogen + p_phosphorus + p_other > 0; + first numeric := p_target; +BEGIN + IF NOT ((p_kind IN (1, 2) AND p_target > 0 AND p_count = 0 AND has_amount = (p_kind = 2)) + OR (p_kind IN (3, 5) AND p_target = 0 AND p_count BETWEEN 1 AND 256 AND has_amount) + OR (p_kind = 4 AND p_target = 0 AND p_count = 0 AND has_amount) + OR (p_kind = 6 AND p_count BETWEEN 1 AND 64 AND NOT has_amount)) THEN + RAISE EXCEPTION 'invalid admin command' USING ERRCODE = '22023'; + END IF; + IF NOT EXISTS (SELECT 1 FROM evolution.region_leases l + WHERE l.world_id = p_world AND l.region_id = p_region) THEN + RAISE EXCEPTION 'unknown region' USING ERRCODE = '22023'; + END IF; + IF p_kind = 6 THEN + SELECT a.next_identity INTO first FROM evolution.life_allocators a + WHERE a.world_id = p_world FOR UPDATE; + IF first IS NULL OR first + p_count > 9223372036854775808 THEN + RAISE EXCEPTION 'no identities for wildlife' USING ERRCODE = '22023'; + END IF; + UPDATE evolution.life_allocators SET next_identity = first + p_count + WHERE world_id = p_world; + END IF; + RETURN QUERY + WITH queued AS ( + INSERT INTO evolution.admin_commands AS q(world_id, region_id, kind, cell_count, target, + energy, carbon, nitrogen, phosphorus, other, author_subject, reason) + VALUES (p_world, p_region, p_kind, p_count, first, p_energy, p_carbon, p_nitrogen, + p_phosphorus, p_other, p_subject, coalesce(p_reason, '')) + RETURNING q.command_id, q.target) + SELECT queued.command_id, queued.target FROM queued; +END $$; +REVOKE ALL ON FUNCTION evolution.admin_enqueue_command_v1(evolution.u64, evolution.u64, integer, + integer, evolution.u64, evolution.u64, evolution.u64, evolution.u64, evolution.u64, + evolution.u64, text, text) FROM PUBLIC; + +-- The region's commands not yet published, oldest first. +CREATE FUNCTION evolution.load_pending_admin_commands_v1(p_world evolution.u64, + p_region evolution.u64) +RETURNS TABLE (command_id bigint, kind integer, cell_count integer, target evolution.u64, + energy evolution.u64, carbon evolution.u64, nitrogen evolution.u64, phosphorus evolution.u64, + other evolution.u64) +LANGUAGE sql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ + SELECT q.command_id, q.kind, q.cell_count, q.target, q.energy, q.carbon, q.nitrogen, + q.phosphorus, q.other + FROM evolution.admin_commands q + WHERE q.world_id = p_world AND q.region_id = p_region + AND NOT EXISTS (SELECT 1 FROM evolution.admin_command_applications a + WHERE a.command_id = q.command_id) + ORDER BY q.command_id + LIMIT 64 +$$; +REVOKE ALL ON FUNCTION evolution.load_pending_admin_commands_v1(evolution.u64, evolution.u64) + FROM PUBLIC; +GRANT EXECUTE ON FUNCTION evolution.load_pending_admin_commands_v1(evolution.u64, evolution.u64) + TO :"runtime_role"; + +-- The region's last 100 commands, newest first, with the tick each applied at. +CREATE FUNCTION evolution.admin_commands_v1(p_world evolution.u64, p_region evolution.u64) +RETURNS TABLE (command_id bigint, kind integer, cell_count integer, target evolution.u64, + energy evolution.u64, carbon evolution.u64, nitrogen evolution.u64, phosphorus evolution.u64, + other evolution.u64, author_subject text, reason text, created_at timestamptz, + applied_tick evolution.u64) +LANGUAGE sql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ + SELECT q.command_id, q.kind, q.cell_count, q.target, q.energy, q.carbon, q.nitrogen, + q.phosphorus, q.other, q.author_subject, q.reason, q.created_at, a.tick + FROM evolution.admin_commands q + LEFT JOIN evolution.admin_command_applications a ON a.command_id = q.command_id + WHERE q.world_id = p_world AND q.region_id = p_region + ORDER BY q.command_id DESC + LIMIT 100 +$$; +REVOKE ALL ON FUNCTION evolution.admin_commands_v1(evolution.u64, evolution.u64) FROM PUBLIC; + +-- Every command recorded in a published trace is exactly its queue row and applies once. +CREATE FUNCTION evolution.bind_cellular_admin_commands(p_manifest bytea, p_trace bytea) +RETURNS void LANGUAGE plpgsql SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest); + r record; +BEGIN + FOR r IN SELECT * FROM evolution.cellular_trace_admin(p_trace, p_manifest) LOOP + IF NOT EXISTS (SELECT 1 FROM evolution.admin_commands q + WHERE q.command_id = r.command_id AND q.world_id = c.world_id + AND q.region_id = c.region_id AND q.kind = r.kind AND q.cell_count = r.cell_count + AND q.target = r.target AND q.energy = r.energy AND q.carbon = r.carbon + AND q.nitrogen = r.nitrogen AND q.phosphorus = r.phosphorus AND q.other = r.other) THEN + RAISE EXCEPTION 'admin command differs from its queue' USING ERRCODE = '22023'; + END IF; + IF EXISTS (SELECT 1 FROM evolution.admin_command_applications a + WHERE a.command_id = r.command_id AND (a.commit_id <> c.commit_id OR a.tick <> r.tick)) THEN + RAISE EXCEPTION 'admin command already applied' USING ERRCODE = 'EVL07'; + END IF; + INSERT INTO evolution.admin_command_applications(command_id, commit_id, commit_sequence, tick) + VALUES (r.command_id, c.commit_id, c.commit_sequence, r.tick) + ON CONFLICT (command_id) DO NOTHING; + END LOOP; +END $$; +REVOKE ALL ON FUNCTION evolution.bind_cellular_admin_commands(bytea, bytea) FROM PUBLIC; + +-- Schema 59's publishers, binding admin commands in the same transaction. +ALTER FUNCTION evolution.publish_cellular_checkpoint(bytea,bytea,bytea,bytea) + RENAME TO publish_cellular_checkpoint_v62; +REVOKE ALL ON FUNCTION evolution.publish_cellular_checkpoint_v62(bytea,bytea,bytea,bytea) + FROM PUBLIC, :"runtime_role"; +CREATE FUNCTION evolution.publish_cellular_checkpoint(p_manifest bytea, p_decisions bytea, + p_actions bytea, p_trace bytea) +RETURNS TABLE(region_frame evolution.u64, head_sequence evolution.u64, head_commit uuid, + head_digest text, head_tick evolution.u64) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +DECLARE h record; +BEGIN + SELECT * INTO h FROM evolution.publish_cellular_checkpoint_v62(p_manifest, p_decisions, + p_actions, p_trace); + IF NOT FOUND THEN + RAISE EXCEPTION 'publication returned no head' USING ERRCODE = 'XX000'; + END IF; + PERFORM evolution.bind_cellular_admin_commands(p_manifest, p_trace); + RETURN QUERY SELECT h.region_frame, h.head_sequence, h.head_commit, h.head_digest, h.head_tick; +END $$; +REVOKE ALL ON FUNCTION evolution.publish_cellular_checkpoint(bytea,bytea,bytea,bytea) FROM PUBLIC; +GRANT EXECUTE ON FUNCTION evolution.publish_cellular_checkpoint(bytea,bytea,bytea,bytea) + TO :"runtime_role"; + +ALTER FUNCTION evolution.publish_cellular_mutation_checkpoint(bytea,bytea,bytea,bytea,bytea,bytea) + RENAME TO publish_cellular_mutation_checkpoint_v62; +REVOKE ALL ON FUNCTION evolution.publish_cellular_mutation_checkpoint_v62(bytea,bytea,bytea,bytea, + bytea,bytea) FROM PUBLIC, :"runtime_role"; +CREATE FUNCTION evolution.publish_cellular_mutation_checkpoint(p_manifest bytea, + p_decisions bytea, p_actions bytea, p_trace bytea, p_mutations bytea, p_results bytea) +RETURNS TABLE(region_frame evolution.u64, head_sequence evolution.u64, head_commit uuid, + head_digest text, head_tick evolution.u64) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +DECLARE h record; +BEGIN + SELECT * INTO h FROM evolution.publish_cellular_mutation_checkpoint_v62(p_manifest, + p_decisions, p_actions, p_trace, p_mutations, p_results); + IF NOT FOUND THEN + RAISE EXCEPTION 'publication returned no head' USING ERRCODE = 'XX000'; + END IF; + PERFORM evolution.bind_cellular_admin_commands(p_manifest, p_trace); + RETURN QUERY SELECT h.region_frame, h.head_sequence, h.head_commit, h.head_digest, h.head_tick; +END $$; +REVOKE ALL ON FUNCTION evolution.publish_cellular_mutation_checkpoint(bytea,bytea,bytea,bytea, + bytea,bytea) FROM PUBLIC; +GRANT EXECUTE ON FUNCTION evolution.publish_cellular_mutation_checkpoint(bytea,bytea,bytea,bytea, + bytea,bytea) TO :"runtime_role"; + +INSERT INTO evolution.schema_migrations(version,sha256) VALUES(63,:'migration_sha_63'); diff --git a/migrations/apply.sql b/migrations/apply.sql index e4a27e2..f73c309 100644 --- a/migrations/apply.sql +++ b/migrations/apply.sql @@ -138,6 +138,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') SELECT :'history_v59' || ',60:' || :'migration_sha_60' AS history_v60 \gset SELECT :'history_v60' || ',61:' || :'migration_sha_61' AS history_v61 \gset SELECT :'history_v61' || ',62:' || :'migration_sha_62' AS history_v62 \gset + SELECT :'history_v62' || ',63:' || :'migration_sha_63' AS history_v63 \gset SELECT :'history' = :'history_v22' AS at_v22, :'history' = :'history_v23' AS at_v23, :'history' = :'history_v24' AS at_v24, @@ -178,8 +179,10 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') :'history' = :'history_v59' AS at_v59, :'history' = :'history_v60' AS at_v60, :'history' = :'history_v61' AS at_v61, - :'history' = :'history_v62' AS at_v62 \gset - \if :at_v62 + :'history' = :'history_v62' AS at_v62, + :'history' = :'history_v63' AS at_v63 \gset + \if :at_v63 + \elif :at_v62 \elif :at_v61 \elif :at_v60 \elif :at_v59 @@ -597,7 +600,8 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, coalesce(max(version), 0) = 59 AS at_v59, coalesce(max(version), 0) = 60 AS at_v60, coalesce(max(version), 0) = 61 AS at_v61, - coalesce(max(version), 0) = 62 AS at_v62 + coalesce(max(version), 0) = 62 AS at_v62, + coalesce(max(version), 0) = 63 AS at_v63 FROM evolution.schema_migrations \gset \if :at_v24 \ir 0025_cellular_v2_bundle.sql @@ -828,6 +832,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, \elif :at_v60 \elif :at_v61 \elif :at_v62 +\elif :at_v63 \else \echo 'Unknown or changed migration history after v43' DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$; @@ -908,6 +913,10 @@ SELECT coalesce(max(version), 0) = 61 AS needs_v62 FROM evolution.schema_migrati \if :needs_v62 \ir 0062_session_windows.sql \endif +SELECT coalesce(max(version), 0) = 62 AS needs_v63 FROM evolution.schema_migrations \gset +\if :needs_v63 + \ir 0063_admin_commands.sql +\endif -- Effective-privilege audit on every run, including no-ops (ADR-0060). Attributes -- alone are insufficient: predefined roles such as pg_write_all_data, PUBLIC -- grants or SET-able memberships would bypass the fenced functions. Checks the @@ -982,6 +991,7 @@ BEGIN ('evolution.end_cellular_lives(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,bytea)'::regprocedure::oid), ('evolution.issue_account_grant(evolution.u64,text,text)'::regprocedure::oid), ('evolution.load_session_window_v1(evolution.u64,evolution.u64)'::regprocedure::oid), + ('evolution.load_pending_admin_commands_v1(evolution.u64,evolution.u64)'::regprocedure::oid), ('evolution.revoke_account_grants(evolution.u64,text,text)'::regprocedure::oid), ('evolution.load_cellular_publication_actions(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid), ('evolution.load_cellular_publication_births(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid), @@ -1050,6 +1060,11 @@ GRANT EXECUTE ON FUNCTION evolution.admin_session_windows_v1(evolution.u64, evol TO :"admin_role"; GRANT EXECUTE ON FUNCTION evolution.admin_set_session_window_v1(evolution.u64, evolution.u64, integer, text, text) TO :"admin_role"; +GRANT EXECUTE ON FUNCTION evolution.admin_enqueue_command_v1(evolution.u64, evolution.u64, integer, + integer, evolution.u64, evolution.u64, evolution.u64, evolution.u64, evolution.u64, + evolution.u64, text, text) TO :"admin_role"; +GRANT EXECUTE ON FUNCTION evolution.admin_commands_v1(evolution.u64, evolution.u64) + TO :"admin_role"; SELECT pg_catalog.set_config('evolution.admin_role', :'admin_role', true); DO $$ DECLARE @@ -1072,7 +1087,9 @@ BEGIN ('evolution.admin_regions_v1(evolution.u64)'::regprocedure::oid), ('evolution.load_session_window_v1(evolution.u64,evolution.u64)'::regprocedure::oid), ('evolution.admin_session_windows_v1(evolution.u64,evolution.u64)'::regprocedure::oid), - ('evolution.admin_set_session_window_v1(evolution.u64,evolution.u64,integer,text,text)'::regprocedure::oid) + ('evolution.admin_set_session_window_v1(evolution.u64,evolution.u64,integer,text,text)'::regprocedure::oid), + ('evolution.admin_enqueue_command_v1(evolution.u64,evolution.u64,integer,integer,evolution.u64,evolution.u64,evolution.u64,evolution.u64,evolution.u64,evolution.u64,text,text)'::regprocedure::oid), + ('evolution.admin_commands_v1(evolution.u64,evolution.u64)'::regprocedure::oid) ), violations(reason) AS ( SELECT 'admin role is the runtime role' WHERE admin = runtime UNION ALL diff --git a/src/persistence/cellular_checkpoint_store.cpp b/src/persistence/cellular_checkpoint_store.cpp index 2a25548..59dee65 100644 --- a/src/persistence/cellular_checkpoint_store.cpp +++ b/src/persistence/cellular_checkpoint_store.cpp @@ -149,7 +149,7 @@ bool prepared_is_canonical(const CellularPreparedCommit& prepared, const auto encoded_trace = server::encode_cellular_batch(trace); const auto digest = sha256(prepared.trace); const bool trace_version_matches = - trace.format_version == cellular_checkpoint_trace_format(manifest.cellular_versions); + cellular_checkpoint_trace_admits(manifest.cellular_versions, trace.format_version); return manifest.profile == CheckpointProfile::cellular_lives_v1 && trace_version_matches && manifest.key == prepared.key && manifest.commit == prepared.commit && manifest.sequence > 0 && manifest.batch.trace_bytes == prepared.trace.size() && digest && @@ -561,7 +561,9 @@ publish_cellular_batch(Postgres& db, const FilesystemObjectStore& objects, const const auto previous = decode_manifest(predecessor.manifest); const auto predecessor_digest = sha256(predecessor.manifest); auto committed_batch = batch; - if (previous) + if (previous && !(committed_batch.format_version == server::cellular_batch_admin_format && + cellular_checkpoint_trace_admits(previous->cellular_versions, + committed_batch.format_version))) committed_batch.format_version = cellular_checkpoint_trace_format(previous->cellular_versions); const auto trace = server::encode_cellular_batch(committed_batch); @@ -834,7 +836,7 @@ recover_cellular_loaded(Postgres& db, const FilesystemObjectStore& objects, Regi auto trace = server::decode_cellular_batch(*trace_bytes); if (!trace) return fail_with(RecoveryError::tail); - if (trace->format_version != cellular_checkpoint_trace_format(manifest->cellular_versions)) + if (!cellular_checkpoint_trace_admits(manifest->cellular_versions, trace->format_version)) return fail_with(RecoveryError::tail); if (trace->header.world != key.world || trace->header.region != key.region || trace->header.frame != manifest->frame || diff --git a/src/server/admin/admin_api.cpp b/src/server/admin/admin_api.cpp index 801a573..f5afe41 100644 --- a/src/server/admin/admin_api.cpp +++ b/src/server/admin/admin_api.cpp @@ -1,6 +1,7 @@ #include "evo/server/admin/admin_api.hpp" #include +#include #include #include #include @@ -263,10 +264,10 @@ OpsResponse AdminApi::handle(const ApiRequest& request, std::uint64_t now_second auto path = request.path.substr(0, mark); const auto query = mark == std::string_view::npos ? std::string_view{} : request.path.substr(mark + 1); - // Only the session window accepts POST; everything else is read-only. + // Only session windows and commands accept POST; everything else is read-only. if (request.method != "GET" && !(request.method == "POST" && path.starts_with("/api/regions/") && - path.ends_with("/session-window"))) + (path.ends_with("/session-window") || path.ends_with("/commands")))) return {405, "method not allowed\n"}; if (path == "/healthz") return {200, "ok\n"}; @@ -330,6 +331,15 @@ OpsResponse AdminApi::api(std::string_view method, std::string_view path, std::s return error(404, "not_found"); return method == "POST" ? set_window(*region, query, admin) : window(*region); } + constexpr std::string_view commands_suffix = "/commands"; + if (path.starts_with(region_prefix) && path.ends_with(commands_suffix) && + path.size() > region_prefix.size() + commands_suffix.size()) { + const auto region = number(path.substr( + region_prefix.size(), path.size() - region_prefix.size() - commands_suffix.size())); + if (!region) + return error(404, "not_found"); + return method == "POST" ? enqueue(*region, query, admin) : commands(*region); + } return error(404, "not_found"); } @@ -464,6 +474,92 @@ OpsResponse AdminApi::set_window(std::uint64_t region, std::string_view query_te ",\"expires_at\":" + seconds_value(at(rows->front(), 1)) + "}\n"); } +namespace { +constexpr std::array command_kinds{ + "", "remove_cell", "grant_reserve", "add_bubbles", + "add_pool", "release_pool", "spawn_wildlife"}; +} // namespace + +OpsResponse AdminApi::commands(std::uint64_t region) { + const auto rows = + query("SELECT command_id, kind, cell_count, target, energy, carbon, nitrogen, phosphorus, " + "other, author_subject, reason, floor(extract(epoch FROM created_at))::bigint, " + "applied_tick FROM evolution.admin_commands_v1($1, $2)", + {std::to_string(config_.world), std::to_string(region)}); + if (!rows) + return error(503, "unavailable"); + std::string out = "{\"region\":\"" + std::to_string(region) + "\",\"commands\":["; + for (std::size_t i = 0; i < rows->size(); ++i) { + const auto& row = (*rows)[i]; + const auto kind = number(at(row, 1).value_or("0")).value_or(0); + out += + std::string(i == 0 ? "" : ",") + "{\"id\":" + text_value(at(row, 0)) + ",\"kind\":\"" + + std::string(kind < command_kinds.size() ? command_kinds[kind] : "") + + "\",\"count\":" + count_value(at(row, 2)) + ",\"target\":" + text_value(at(row, 3)) + + ",\"amount\":[" + text_value(at(row, 4)) + "," + text_value(at(row, 5)) + "," + + text_value(at(row, 6)) + "," + text_value(at(row, 7)) + "," + text_value(at(row, 8)) + + "],\"author\":" + text_value(at(row, 9)) + ",\"reason\":" + text_value(at(row, 10)) + + ",\"created_at\":" + seconds_value(at(row, 11)) + + ",\"applied_tick\":" + text_value(at(row, 12)) + "}"; + } + return json(200, out + "]}\n"); +} + +OpsResponse AdminApi::enqueue(std::uint64_t region, std::string_view query_text, + const broker::AccessTokenClaims& admin) { + const auto kind_text = query_value(query_text, "kind", 20); + const auto kind = + kind_text ? std::ranges::find(command_kinds, *kind_text) : command_kinds.end(); + if (!kind_text || kind_text->empty() || kind == command_kinds.end()) + return error(400, "bad_kind"); + // Absent numbers are zero; present ones must be plain u64 decimals. + const auto value = [&](std::string_view name) -> std::optional { + const auto text = query_value(query_text, name, 20); + if (!text) + return query_text.find(std::string(name) + "=") == std::string_view::npos + ? std::optional{0} + : std::nullopt; + return number(*text); + }; + std::array, 7> v{ + value("count"), value("target"), value("energy"), value("carbon"), + value("nitrogen"), value("phosphorus"), value("other")}; + if (std::ranges::any_of(v, [](const auto& x) { return !x; })) + return error(400, "bad_number"); + const auto reason = query_value(query_text, "reason"); + if (!reason && query_text.find("reason=") != std::string_view::npos) + return error(400, "bad_reason"); + auto author = admin.email.empty() ? admin.subject : admin.email + " (" + admin.subject + ")"; + if (author.size() > 255) + author = admin.subject; + std::vector parameters{std::to_string(config_.world), std::to_string(region), + std::to_string(kind - command_kinds.begin())}; + for (const auto& x : v) + parameters.push_back(std::to_string(x.value_or(0))); // All present: checked above. + parameters.push_back(author); + parameters.push_back(reason.value_or("")); + if (!db_ && connect_) + db_ = connect_(); + if (!db_) + return error(503, "unavailable"); + const auto rows = + db_->query("SELECT command_id, target FROM evolution.admin_enqueue_command_v1(" + "$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)", + parameters, config_.db_timeout); + if (!rows) { + // An invalid command is refused by the database (SQLSTATE 22023); keep the connection. + const bool invalid = rows.error().kind == persistence::PgError::sql && + std::string_view{rows.error().sqlstate.data(), 5} == "22023"; + if (!invalid) + db_.reset(); + return error(invalid ? 400 : 503, invalid ? "invalid_command" : "unavailable"); + } + if (rows->empty()) + return error(503, "unavailable"); + return json(200, "{\"id\":" + text_value(at(rows->front(), 0)) + + ",\"target\":" + text_value(at(rows->front(), 1)) + "}\n"); +} + OpsResponse AdminApi::view(std::uint64_t region, std::chrono::steady_clock::time_point now) { if (!std::ranges::contains(config_.view_regions, region)) return error(404, "not_viewable"); diff --git a/src/server/cellular_region_host.cpp b/src/server/cellular_region_host.cpp index ca882f4..ffc0025 100644 --- a/src/server/cellular_region_host.cpp +++ b/src/server/cellular_region_host.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -448,6 +449,9 @@ void CellularRegionHost::resolve() { finalized_actions_.push_back({action, committed_->head.sequence}); finalize_births(pending_->births); finalize_mutations(pending_->mutation_actions, pending_->prepared.results); + // The store knows which admin commands this commit bound: read the queue again. + queued_admin_.clear(); + next_admin_reload_ = {}; pending_.reset(); ++status_.commits; status_.committed_sequence = committed_->head.sequence; @@ -738,6 +742,8 @@ const HostStatus& CellularRegionHost::step() { reconcile_interval(); if (!pending_ && state_ && mutations_uncertain_ && status_.phase == HostPhase::running) reload_mutations(); + if (!pending_ && state_ && status_.phase == HostPhase::running) + reload_admin_commands(); if (!pending_ && state_ && status_.phase == HostPhase::running) flush_endings(); refresh(); @@ -928,6 +934,64 @@ CellularRegionHost::select_mutation(const persistence::Uuid& account, std::uint6 return {admitted, tick, {}}; } +std::vector CellularRegionHost::unbound_admin_commands() const { + std::vector unbound; + for (const auto& queued : queued_admin_) { + if (unbound.size() >= max_cellular_tick_admin_commands) + break; + const bool flying = + in_flight_ && std::ranges::any_of(in_flight_->batch.ticks, [&](const auto& tick) { + return std::ranges::find(tick.admin, queued.id, &CellularAdminCommand::id) != + tick.admin.end(); + }); + if (!flying) + unbound.push_back(queued); + } + return unbound; +} + +// ADR-0182: the region's unpublished admin commands, every 2 s (rules 13 only). +void CellularRegionHost::reload_admin_commands() { + const auto now = std::chrono::steady_clock::now(); + if (now < next_admin_reload_ || !committed_ || + state_->rules_version() < cellular_lives_revamp_rules || !database()) + return; + next_admin_reload_ = now + std::chrono::seconds{2}; + const std::array parameters{std::to_string(config_.key.world), + std::to_string(config_.key.region)}; + const auto rows = db_->query( + "SELECT command_id, kind, cell_count, target, energy, carbon, nitrogen, phosphorus, other " + "FROM evolution.load_pending_admin_commands_v1($1::evolution.u64, $2::evolution.u64)", + parameters, config_.lease.commit_timeout); + if (!rows) { + db_.reset(); + ++status_.store_failures; + return; + } + std::vector loaded; + for (const auto& row : *rows) { + std::array v{}; + bool ok = row.size() == v.size(); + for (std::size_t i = 0; ok && i < v.size(); ++i) { + const auto& text = row[i]; + ok = text.has_value() && + std::from_chars(text->data(), text->data() + text->size(), v[i]).ec == std::errc{}; + } + if (!ok || v[1] > 0xFF || v[2] > 0xFFFF) + continue; + CellularAdminCommand command{v[0], + static_cast(v[1]), + static_cast(v[2]), + v[3], + {{v[4]}, {}}}; + for (std::size_t c = 0; c < command.amount.matter.size(); ++c) + command.amount.matter[c].value = v[5 + c]; + if (cellular_admin_command_valid(command)) + loaded.push_back(command); + } + queued_admin_ = std::move(loaded); +} + std::vector CellularRegionHost::unbound_mutations() const { std::vector unbound; @@ -1091,6 +1155,10 @@ CellularRegionHost::finish_publication( for (const auto result : published->mutation_results) results.push_back(static_cast(result)); finalize_mutations(mutation_actions, results); + for (const auto& tick : batch.ticks) + for (const auto& command : tick.admin) + std::erase_if(queued_admin_, + [&](const auto& queued) { return queued.id == command.id; }); ++status_.commits; status_.committed_sequence = committed_->head.sequence; status_.committed_tick = committed_->head.end_tick; diff --git a/src/server/lifecycle/cellular_batch.cpp b/src/server/lifecycle/cellular_batch.cpp index 2dc4c4b..7e47836 100644 --- a/src/server/lifecycle/cellular_batch.cpp +++ b/src/server/lifecycle/cellular_batch.cpp @@ -34,20 +34,29 @@ bool valid_header(const CellularBatchHeader& h, std::size_t ticks) { } bool known_format(std::uint32_t format) { return format == cellular_batch_legacy_format || format == cellular_batch_v2_format || - format == cellular_batch_format || format == cellular_batch_inheritance_format; + format == cellular_batch_format || format == cellular_batch_inheritance_format || + format == cellular_batch_admin_format; } bool owner_version_matches(const CellularLives& owner, const CellularBatch& batch) { if (!known_format(batch.format_version)) return false; + if (batch.format_version == cellular_batch_admin_format) + return owner.format_version() == cellular_lives_revamp_format && + owner.rules_version() >= cellular_lives_revamp_rules; if (batch.format_version == cellular_batch_inheritance_format) return owner.format_version() >= cellular_lives_inheritance_format && cellular_retained_plans(owner.rules_version()); return owner.format_version() < cellular_lives_inheritance_format; } bool payload_version_matches(const CellularBatch& batch) { + // ADR-0182: trace 5 only with a command; a batch without one is trace 4. + if (batch.format_version == cellular_batch_admin_format && + std::ranges::none_of(batch.ticks, [](const auto& tick) { return !tick.admin.empty(); })) + return false; return std::ranges::all_of(batch.ticks, [&](const auto& tick) { return (batch.format_version >= cellular_batch_v2_format || tick.births.empty()) && - (batch.format_version >= cellular_batch_format || tick.mutations.empty()); + (batch.format_version >= cellular_batch_format || tick.mutations.empty()) && + (batch.format_version == cellular_batch_admin_format || tick.admin.empty()); }); } bool valid_intent(const CellularLifeIntent& life) { @@ -84,6 +93,12 @@ bool valid_ticks(std::span ticks) { if (!identities.insert(birth.identity).second || !accounts.insert(birth.account).second) return false; } + if (tick.admin.size() > max_cellular_tick_admin_commands) + return false; + for (std::size_t i = 0; i < tick.admin.size(); ++i) + if (!cellular_admin_command_valid(tick.admin[i]) || + (i > 0 && tick.admin[i - 1].id >= tick.admin[i].id)) + return false; for (std::size_t i = 0; i < tick.mutations.size(); ++i) { const auto& mutation = tick.mutations[i]; if (mutation.life_id == 0 || mutation.event_index == 0 || mutation.offer_tick == 0 || @@ -198,7 +213,8 @@ encode_cellular_batch(const CellularBatch& batch) { : 0) + (batch.format_version >= cellular_batch_format ? tick.mutations.size() * cellular_batch_mutation_bytes - : 0); + : 0) + + tick.admin.size() * cellular_batch_admin_bytes; if (records > max_cellular_batch_bytes - size - tick_header_bytes) return std::unexpected{CellularBatchError::format}; size += tick_header_bytes + records; @@ -224,7 +240,7 @@ encode_cellular_batch(const CellularBatch& batch) { put(out, tick.births.size(), 4); if (batch.format_version >= cellular_batch_format) { put(out, tick.mutations.size(), 4); - put(out, 0, 4); + put(out, tick.admin.size(), 4); // Zero before format 5. } for (const auto& life : tick.lives) put_intent(out, life); @@ -247,6 +263,16 @@ encode_cellular_batch(const CellularBatch& batch) { put(out, mutation.become ? 1 : 0, 1); // Byte 34 (rules 13). put(out, 0, 5); } + for (const auto& command : tick.admin) { + put(out, command.id, 8); + put(out, static_cast(command.kind), 2); + put(out, command.count, 2); + put(out, 0, 4); + put(out, command.target, 8); + put(out, command.amount.energy.value, 8); + for (const auto& channel : command.amount.matter) + put(out, channel.value, 8); + } } if (out.size() != size) return std::unexpected{CellularBatchError::format}; @@ -259,8 +285,8 @@ decode_cellular_batch(std::span bytes) { !std::ranges::equal(bytes.first(magic.size()), magic)) return std::unexpected{CellularBatchError::format}; const auto format = get(bytes, 8, 4); - if (format > cellular_batch_inheritance_format || - !known_format(static_cast(format)) || get(bytes, 12, 4) != 4) + if (format > cellular_batch_admin_format || !known_format(static_cast(format)) || + get(bytes, 12, 4) != 4) return std::unexpected{CellularBatchError::version}; if (get(bytes, 108, 4) != 0) return std::unexpected{CellularBatchError::format}; @@ -298,13 +324,16 @@ decode_cellular_batch(std::span bytes) { birth_count = get(bytes, offset, 4); offset += 4; } - std::uint64_t mutation_count{}; + std::uint64_t mutation_count{}, admin_count{}; if (format >= cellular_batch_format) { mutation_count = get(bytes, offset, 4); - if (get(bytes, offset + 4, 4) != 0) + admin_count = get(bytes, offset + 4, 4); + if (format != cellular_batch_admin_format && admin_count != 0) return std::unexpected{CellularBatchError::format}; offset += 8; } + if (admin_count > max_cellular_tick_admin_commands) + return std::unexpected{CellularBatchError::format}; if (count > biology::max_cellular_tick_lives || count > (bytes.size() - offset) / cellular_batch_intent_bytes || birth_count > biology::max_cellular_tick_lives || @@ -381,9 +410,29 @@ decode_cellular_batch(std::span bytes) { step.mutations.push_back(mutation); offset += cellular_batch_mutation_bytes; } + if (admin_count > (bytes.size() - offset) / cellular_batch_admin_bytes) + return std::unexpected{CellularBatchError::format}; + for (std::uint64_t i = 0; i < admin_count; ++i) { + const auto record = bytes.subspan(offset, cellular_batch_admin_bytes); + CellularAdminCommand command; + command.id = get(record, 0, 8); + const auto kind = get(record, 8, 2); + if (kind > 0xFF) + return std::unexpected{CellularBatchError::format}; + command.kind = static_cast(kind); + command.count = static_cast(get(record, 10, 2)); + command.target = get(record, 16, 8); + command.amount.energy.value = get(record, 24, 8); + for (std::size_t c = 0; c < command.amount.matter.size(); ++c) + command.amount.matter[c].value = get(record, 32 + 8 * c, 8); + if (!all_zero(record.subspan(12, 4))) + return std::unexpected{CellularBatchError::format}; + step.admin.push_back(command); + offset += cellular_batch_admin_bytes; + } out.ticks.push_back(std::move(step)); } - if (offset != bytes.size() || !valid_ticks(out.ticks)) + if (offset != bytes.size() || !valid_ticks(out.ticks) || !payload_version_matches(out)) return std::unexpected{CellularBatchError::format}; return out; } @@ -433,7 +482,16 @@ decode_cellular_mutation_input(const std::array& account, std: std::expected, CellularBatchError> replay_cellular_batch(const CellularLives& base, const CellularBatch& batch, std::vector& mutation_outcomes) { + std::vector ignored; + return replay_cellular_batch(base, batch, mutation_outcomes, ignored); +} + +std::expected, CellularBatchError> +replay_cellular_batch(const CellularLives& base, const CellularBatch& batch, + std::vector& mutation_outcomes, + std::vector& admin_results) { mutation_outcomes.clear(); + admin_results.clear(); if (!owner_version_matches(base, batch)) return std::unexpected{CellularBatchError::version}; const auto& h = batch.header; @@ -448,6 +506,7 @@ replay_cellular_batch(const CellularLives& base, const CellularBatch& batch, if (!replay) return std::unexpected{CellularBatchError::state}; std::vector complete_outcomes; + std::vector complete_admin; for (const auto& tick : batch.ticks) { const auto executed = (*replay)->advance_tick_with_mutations(tick.lives, tick.mutations); if (!executed) @@ -467,8 +526,17 @@ replay_cellular_batch(const CellularLives& base, const CellularBatch& batch, return std::unexpected{CellularBatchError::state}; *replay = std::move(*born); } + if (!tick.admin.empty()) { + std::vector results; + auto applied = (*replay)->admin_candidate(tick.admin, &results); + if (!applied) + return std::unexpected{CellularBatchError::state}; + *replay = std::move(*applied); + complete_admin.insert(complete_admin.end(), results.begin(), results.end()); + } } mutation_outcomes = std::move(complete_outcomes); + admin_results = std::move(complete_admin); return std::move(*replay); } diff --git a/src/server/lifecycle/cellular_lives.cpp b/src/server/lifecycle/cellular_lives.cpp index 7bf0f88..650084a 100644 --- a/src/server/lifecycle/cellular_lives.cpp +++ b/src/server/lifecycle/cellular_lives.cpp @@ -1344,103 +1344,268 @@ CellularLives::revamp_upgrade_candidate(const CellularRevampRules& rules, std::u next.archive.archived() != 0 || count >= rules.organism_cap || count > 255 || first == 0 || first > cellular_bud_namespace - count) return std::unexpected{CellularLivesError::invalid_config}; - for (std::uint32_t index = 0; index < count; ++index) { - const auto id = first + index; - const auto founder = biology::primitive_founder(id, id, biology::primitive_founder_v6); - if (!founder) - return std::unexpected{CellularLivesError::biology}; - biology::GenomeDraft draft{ - .id = id, - .origin_event = id, - .development_version = 7, - .parents = {}, - .capabilities = {founder->capabilities().begin(), founder->capabilities().end()}, - .schema_version = biology::genome_schema_v7, - .movement_arrangement = biology::founder_movement_arrangement, - .cellular_traits = biology::founder_cellular_traits}; - Random random{mutation_seed(world(), region(), id, 0x57494C44U)}; - const auto points = [&] { return static_cast(75 + random.next() % 51); }; - auto& traits = *draft.cellular_traits; - traits.density = points(); - traits.upkeep_efficiency = points(); - traits.growth_speed = points(); - for (auto& channel : traits.collection) - channel = points(); - for (auto& channel : traits.burn) - channel = points(); - traits.engulf_capacity = points(); - traits.kill_absorption = points(); - auto& movement = *draft.movement_arrangement; - movement.output = points(); - movement.efficiency = points(); - movement.steering = points(); - movement.streamlining = points(); - movement.braking = points(); - movement.coasting = points(); - const auto genome = biology::Genome::create(draft); - const auto budget = - genome ? biology::metabolic_budget(*genome, {1000, 0}, - {next.config.ambient_temperature_millikelvin, {}}, - 1000, rules.body) - : std::unexpected{biology::DevelopmentError::overflow}; - if (!budget) - return std::unexpected{CellularLivesError::biology}; - // A normal founder's starting provision, evaluated for its randomized genome. - const accounting::Inventory reserve{ - {budget->demand.value * biology::founder_reserve_seconds}, {}}; - auto empty_body = accounting::Inventory{}; - auto nursery = next.nursery; - auto heat = next.heat; - const auto development = biology::rules_for(genome->development_version()); - if (!development || !subtract_inventory(nursery, reserve) || - !rebody(empty_body, nursery, heat, budget->body, - development->construction_per_kilogram)) - return std::unexpected{CellularLivesError::insufficient_funding}; - const auto extent = budget->phenotype.extent; - if (next.config.area.width_um <= extent + 2000 || - next.config.area.height_um <= extent + 2000) - return std::unexpected{CellularLivesError::no_placement}; - std::optional position; - for (int attempt = 0; attempt < 1024 && !position; ++attempt) { - const biology::CellularPosition spot{ - 1000 + random.next() % (next.config.area.width_um - extent - 2000), - 1000 + random.next() % (next.config.area.height_um - extent - 2000)}; - bool occupied = false; - for (const auto& other : next.lives) { - const auto body = budget_for( - next.archive, other, next.config.ambient_temperature_millikelvin, rules.body); - if (!body) - return std::unexpected{CellularLivesError::biology}; - const auto side = body->phenotype.extent; - occupied = occupied || (spot.x_um < other.position.x_um + side && - other.position.x_um < spot.x_um + extent && - spot.y_um < other.position.y_um + side && - other.position.y_um < spot.y_um + extent); - } - if (!occupied) - position = spot; - } - if (!position) - return std::unexpected{CellularLivesError::no_placement}; - if (!next.archive.admit(*genome) || !next.archive.record_birth(id, id, {tick()})) - return std::unexpected{CellularLivesError::archive}; - CellularOwnedLife life{}; - life.identity = {id, id, id, id, {tick()}}; - life.source = next.nursery_config.source; - life.growth = {1000, 0}; - life.position = *position; - life.body = budget->body; - life.reserve = reserve; - life.offspring_genome = id; - next.lives.push_back(life); - next.nursery = nursery; - next.nursery_config.funding = nursery; - next.heat = heat; - } + for (std::uint32_t index = 0; index < count; ++index) + if (const auto error = + seed_wildlife_root(next, world(), region(), tick(), first + index, nullptr)) + return std::unexpected{*error}; // Restore validates bodies, identities, history and channel-by-channel closure. return restore((*candidate)->snapshot(), world(), region(), frame(), tick()); } +std::optional +CellularLives::seed_wildlife_root(State& next, std::uint64_t world, std::uint64_t region, + std::uint64_t tick, std::uint64_t id, + accounting::Inventory* imported) { + const auto& rules = next.revamp; + const auto founder = biology::primitive_founder(id, id, biology::primitive_founder_v6); + if (!founder) + return CellularLivesError::biology; + biology::GenomeDraft draft{ + .id = id, + .origin_event = id, + .development_version = 7, + .parents = {}, + .capabilities = {founder->capabilities().begin(), founder->capabilities().end()}, + .schema_version = biology::genome_schema_v7, + .movement_arrangement = biology::founder_movement_arrangement, + .cellular_traits = biology::founder_cellular_traits}; + Random random{mutation_seed(world, region, id, 0x57494C44U)}; + const auto points = [&] { return static_cast(75 + random.next() % 51); }; + auto& traits = *draft.cellular_traits; + traits.density = points(); + traits.upkeep_efficiency = points(); + traits.growth_speed = points(); + for (auto& channel : traits.collection) + channel = points(); + for (auto& channel : traits.burn) + channel = points(); + traits.engulf_capacity = points(); + traits.kill_absorption = points(); + auto& movement = *draft.movement_arrangement; + movement.output = points(); + movement.efficiency = points(); + movement.steering = points(); + movement.streamlining = points(); + movement.braking = points(); + movement.coasting = points(); + const auto genome = biology::Genome::create(draft); + const auto budget = + genome ? biology::metabolic_budget(*genome, {1000, 0}, + {next.config.ambient_temperature_millikelvin, {}}, 1000, + rules.body) + : std::unexpected{biology::DevelopmentError::overflow}; + if (!budget) + return CellularLivesError::biology; + // A normal founder's starting provision, evaluated for its randomized genome. + const accounting::Inventory reserve{{budget->demand.value * biology::founder_reserve_seconds}, + {}}; + auto empty_body = accounting::Inventory{}; + const auto development = biology::rules_for(genome->development_version()); + if (!development) + return CellularLivesError::insufficient_funding; + // ADR-0182: an import pays exactly the reserve, the body's matter and its construction. + auto funding = next.nursery; + if (imported) { + std::uint64_t added{}; + funding = reserve; + for (std::size_t c = 0; c < funding.matter.size(); ++c) { + funding.matter[c] = budget->body.matter[c]; + added += budget->body.matter[c].value; + } + const auto price = development->construction_per_kilogram; + funding.energy.value += added / 1'000 * price + added % 1'000 * price / 1'000; + } + const auto paid = funding; + auto heat = next.heat; + if (!subtract_inventory(funding, reserve) || + !rebody(empty_body, funding, heat, budget->body, development->construction_per_kilogram)) + return CellularLivesError::insufficient_funding; + const auto extent = budget->phenotype.extent; + if (next.config.area.width_um <= extent + 2000 || next.config.area.height_um <= extent + 2000) + return CellularLivesError::no_placement; + std::optional position; + for (int attempt = 0; attempt < 1024 && !position; ++attempt) { + const biology::CellularPosition spot{ + 1000 + random.next() % (next.config.area.width_um - extent - 2000), + 1000 + random.next() % (next.config.area.height_um - extent - 2000)}; + bool occupied = false; + for (const auto& other : next.lives) { + const auto body = budget_for(next.archive, other, + next.config.ambient_temperature_millikelvin, rules.body); + if (!body) + return CellularLivesError::biology; + const auto side = body->phenotype.extent; + occupied = occupied || (spot.x_um < other.position.x_um + side && + other.position.x_um < spot.x_um + extent && + spot.y_um < other.position.y_um + side && + other.position.y_um < spot.y_um + extent); + } + if (!occupied) + position = spot; + } + if (!position) + return CellularLivesError::no_placement; + if (!next.archive.admit(*genome) || !next.archive.record_birth(id, id, {tick})) + return CellularLivesError::archive; + CellularOwnedLife life{}; + life.identity = {id, id, id, id, {tick}}; + life.source = next.nursery_config.source; + life.growth = {1000, 0}; + life.position = *position; + life.body = budget->body; + life.reserve = reserve; + life.offspring_genome = id; + const auto at = std::ranges::lower_bound(next.lives, id, {}, + [](const auto& value) { return value.identity.life; }); + next.lives.insert(at, life); + if (imported) { + if (funding != accounting::Inventory{} || !add_inventory(*imported, paid)) + return CellularLivesError::conservation; + } else { + next.nursery = funding; + next.nursery_config.funding = funding; + } + next.heat = heat; + return std::nullopt; +} + +bool cellular_admin_command_valid(const CellularAdminCommand& c) noexcept { + const bool some = c.amount != accounting::Inventory{}; + switch (c.kind) { + case CellularAdminKind::remove_cell: + return c.id != 0 && c.target != 0 && c.count == 0 && !some; + case CellularAdminKind::grant_reserve: + return c.id != 0 && c.target != 0 && c.count == 0 && some; + case CellularAdminKind::add_bubbles: + case CellularAdminKind::release_pool: + return c.id != 0 && c.target == 0 && c.count >= 1 && c.count <= 256 && some; + case CellularAdminKind::add_pool: + return c.id != 0 && c.target == 0 && c.count == 0 && some; + case CellularAdminKind::spawn_wildlife: + return c.id != 0 && c.count >= 1 && c.count <= 64 && !some && c.target != 0 && + c.target <= cellular_bud_namespace - c.count; + } + return false; +} + +std::expected, CellularLivesError> +CellularLives::admin_candidate(std::span commands, + std::vector* results) const { + if (state_->format_version != cellular_lives_revamp_format || + state_->rules_version < cellular_lives_revamp_rules) + return std::unexpected{CellularLivesError::version}; + for (std::size_t i = 0; i < commands.size(); ++i) + if (!cellular_admin_command_valid(commands[i]) || + (i != 0 && commands[i - 1].id >= commands[i].id)) + return std::unexpected{CellularLivesError::invalid_config}; + auto candidate = restore(snapshot(), world(), region(), frame(), tick()); + if (!candidate) + return std::unexpected(candidate.error()); + std::vector report; + for (const auto& command : commands) { + // Each command works on a copy, kept only when it applies and still closes. + auto attempt = restore((*candidate)->snapshot(), world(), region(), frame(), tick()); + if (!attempt) + return std::unexpected(attempt.error()); + auto& next = *(*attempt)->state_; + bool applied = false; + const auto life = std::ranges::lower_bound(next.lives, command.target, {}, + [](const auto& v) { return v.identity.life; }); + const bool found = life != next.lives.end() && life->identity.life == command.target; + switch (command.kind) { + case CellularAdminKind::remove_cell: + if (found && next.archive.record_death(life->identity.organism, {tick()})) { + auto single = *life; + single.revamp.members = 1; // A corpse records the single-cell extent. + const auto extent = + budget_for(next.archive, single, next.config.ambient_temperature_millikelvin, + next.revamp.body); + if (extent && add_inventory(next.spent, life->body) && + add_inventory(next.spent, life->reserve)) { + CellularOwnedCorpse corpse{life->identity.life, + life->identity.organism, + life->source, + tick(), + life->position, + life->growth, + extent->phenotype.extent, + {}, + biology::CellularDeathCause::removed, + 0, + life->parent_life, + life->offspring_genome}; + const auto at = std::ranges::lower_bound(next.corpses, corpse.organism, {}, + &CellularOwnedCorpse::organism); + next.corpses.insert(at, corpse); + next.lives.erase(life); + applied = true; + } + } + break; + case CellularAdminKind::grant_reserve: + applied = found && add_inventory(life->reserve, command.amount) && + add_inventory(next.boundary.incoming, command.amount); + break; + case CellularAdminKind::add_pool: + applied = add_inventory(next.spent, command.amount) && + add_inventory(next.boundary.incoming, command.amount); + break; + case CellularAdminKind::add_bubbles: + case CellularAdminKind::release_pool: { + const bool release = command.kind == CellularAdminKind::release_pool; + auto amount = command.amount; + if (release) // Never more than the pool holds. + for (std::size_t c = 0; c < 5; ++c) { + auto& want = c == 0 ? amount.energy.value : amount.matter[c - 1].value; + const auto have = + c == 0 ? next.spent.energy.value : next.spent.matter[c - 1].value; + want = std::min(want, have); + } + auto config = next.habitat->config(); + if (amount != accounting::Inventory{} && + (release ? subtract_inventory(next.spent, amount) + : add_inventory(next.boundary.incoming, amount)) && + cellular_scatter_bubbles(config, amount, command.count, + mutation_seed(world(), region(), command.id, tick()), + next.nursery_config.source)) { + if (auto habitat = biology::CellularHabitat::create(std::move(config))) { + next.habitat = std::move(*habitat); + applied = true; + } + } + break; + } + case CellularAdminKind::spawn_wildlife: { + if (next.lives.size() + command.count >= next.revamp.organism_cap) + break; + accounting::Inventory imported{}; + bool ok = true; + for (std::uint16_t i = 0; ok && i < command.count; ++i) { + const auto id = command.target + i; + ok = !std::ranges::binary_search(next.lives, id, {}, + [](const auto& v) { return v.identity.life; }) && + !next.archive.find(id) && + !seed_wildlife_root(next, world(), region(), tick(), id, &imported); + } + applied = ok && add_inventory(next.boundary.incoming, imported); + break; + } + } + if (applied) { + // Restore checks bodies, identities, history and channel-by-channel closure. + auto checked = restore((*attempt)->snapshot(), world(), region(), frame(), tick()); + if (checked) + candidate = std::move(checked); + else + applied = false; + } + report.push_back({command.id, applied}); + } + if (results) + *results = std::move(report); + return std::move(*candidate); +} + std::optional CellularLives::budding_cost(std::uint64_t id) const { const auto life = std::ranges::find(state_->lives, id, [](const auto& value) { return value.identity.life; }); @@ -2354,7 +2519,8 @@ CellularLives::restore(std::span bytes, std::uint64_t world, const bool engulfed = corpse.cause == biology::CellularDeathCause::engulfed; const bool revamp_cause = revamp_version && (corpse.cause == biology::CellularDeathCause::merged || - corpse.cause == biology::CellularDeathCause::emerged); + corpse.cause == biology::CellularDeathCause::emerged || + corpse.cause == biology::CellularDeathCause::removed); if (padding != 0 || (corpse.cause != biology::CellularDeathCause::starved && !engulfed && !revamp_cause) || diff --git a/src/server/lifecycle/cellular_play_summary.cpp b/src/server/lifecycle/cellular_play_summary.cpp index 1d188da..bcfa97f 100644 --- a/src/server/lifecycle/cellular_play_summary.cpp +++ b/src/server/lifecycle/cellular_play_summary.cpp @@ -48,6 +48,7 @@ summarize_cellular_play(const CellularLives& state) { ++summary.engulfed; break; case biology::CellularDeathCause::unrecorded: + case biology::CellularDeathCause::removed: // ADR-0182: an operator's, not biology's. ++summary.unrecorded; break; case biology::CellularDeathCause::merged: diff --git a/src/server/lifecycle/cellular_revamp.cpp b/src/server/lifecycle/cellular_revamp.cpp index be62dd2..8b74e3f 100644 --- a/src/server/lifecycle/cellular_revamp.cpp +++ b/src/server/lifecycle/cellular_revamp.cpp @@ -59,8 +59,9 @@ bool cellular_revamp_valid(const CellularRevampRules& rules) noexcept { rules.split_share_per_thousand < 1'000 && rules.split_min_growth != 0 && rules.split_min_growth <= 1'000 && rules.bud_fee_scale_percent <= 100 && rules.bud_fee.energy.value <= most && - std::ranges::all_of(rules.bud_fee.matter, - [](const accounting::Femtograms& f) { return f.value <= most; }) && + std::ranges::all_of( + rules.bud_fee.matter, + [](const accounting::Femtograms& f) { return f.value <= most; }) && rules.organism_cap != 0 && rules.organism_cap <= biology::max_cellular_tick_lives && rules.organism_load_per_thousand <= 10'000 && rules.emerge_members >= 2 && rules.emerge_members <= 10'000 && rules.emerge_mass_fg <= most && @@ -133,6 +134,44 @@ bool cellular_drip_spent(biology::CellularHabitatConfig& config, accounting::Inv return true; } +bool cellular_scatter_bubbles(biology::CellularHabitatConfig& config, + const accounting::Inventory& amount, std::uint16_t count, + std::uint64_t seed, std::uint64_t source) { + if (count == 0 || config.patches.size() + count > biology::max_cellular_liquid_deposits || + config.next_deposit > std::numeric_limits::max() - count || + config.width_um == 0 || config.height_um == 0) + return false; + Random random{seed}; + const auto by_address = [](const biology::CellularPatch& patch) { return patch.address; }; + for (std::uint16_t i = 0; i < count; ++i) { + biology::CellularPatchAddress address{ + static_cast(random.next() % config.width_um), + static_cast(random.next() % config.height_um)}; + auto at = std::ranges::lower_bound(config.patches, address, {}, by_address); + while (at != config.patches.end() && at->address == address) { + address.x_um = address.x_um + 1 < config.width_um ? address.x_um + 1 : 0; + at = std::ranges::lower_bound(config.patches, address, {}, by_address); + } + const bool last = i + 1 == count; + const auto share = [&](std::uint64_t total) { + return total / count + (last ? total % count : 0); + }; + biology::CellularPatch bubble{address, + source, + {}, + config.next_deposit++, + biology::cellular_mote_radius_um, + biology::CellularDepositKind::mote}; + bubble.merged = 1; + bubble.stock.energy.value = share(amount.energy.value); + for (std::size_t c = 0; c < amount.matter.size(); ++c) + bubble.stock.matter[c].value = share(amount.matter[c].value); + if (bubble.stock != accounting::Inventory{}) + config.patches.insert(at, bubble); + } + return true; +} + std::optional cellular_draw_offer(const biology::Genome& plan, const CellularRevampLife& life, const CellularRevampRules& rules, diff --git a/src/server/session/cellular_session_batch.cpp b/src/server/session/cellular_session_batch.cpp index 8e8d42c..e33ea97 100644 --- a/src/server/session/cellular_session_batch.cpp +++ b/src/server/session/cellular_session_batch.cpp @@ -201,24 +201,33 @@ std::expected CellularSessionBatchBuilder::appe std::uint64_t tick, std::span baseline, std::span actions, std::span births, - std::span mutations) { - return append_impl(tick, baseline, actions, births, mutations, false); + std::span mutations, + std::span admin) { + return append_impl(tick, baseline, actions, births, mutations, admin, false); } std::expected CellularSessionBatchBuilder::append_with_birth_refusals( std::uint64_t tick, std::span baseline, std::span actions, std::span births, - std::span mutations) { - return append_impl(tick, baseline, actions, births, mutations, true); + std::span mutations, + std::span admin) { + return append_impl(tick, baseline, actions, births, mutations, admin, true); } std::expected CellularSessionBatchBuilder::append_impl( std::uint64_t tick, std::span baseline, std::span actions, std::span births, - std::span mutations, bool allow_refusals) { + std::span mutations, + std::span admin, bool allow_refusals) { if (batch_.ticks.size() >= max_cellular_batch_ticks) return std::unexpected{CellularSessionBatchError::batch_full}; + // ADR-0182: a command appears once in a publication. + for (const auto& command : admin) + for (const auto& previous_tick : batch_.ticks) + if (std::ranges::find(previous_tick.admin, command.id, &CellularAdminCommand::id) != + previous_tick.admin.end()) + return std::unexpected{CellularSessionBatchError::duplicate_action}; if (actions_.size() + actions.size() > max_cellular_action_records) return std::unexpected{CellularSessionBatchError::action_limit}; if (mutation_actions_.size() + mutations.size() > max_cellular_action_records) @@ -267,7 +276,7 @@ std::expected CellularSessionBatchBuilder::appe // it byte-for-byte unchanged: check the encodings first, then advance in place. A full // copy per tick costs a restore of the whole region (rules-12 regions hold thousands of // deposits). - if (prepared->intents.births.empty()) { + if (prepared->intents.births.empty() && admin.empty()) { auto next_batch = batch_; next_batch.ticks.push_back(prepared->intents); next_batch.header.end_tick = tick; @@ -325,8 +334,20 @@ std::expected CellularSessionBatchBuilder::appe accepted_births.push_back(birth); } prepared->intents.births = std::move(accepted_births); + std::vector admin_results; + if (!admin.empty()) { + auto applied = (*next)->admin_candidate(admin, &admin_results); + if (!applied) { + last_lives_error_ = applied.error(); + return std::unexpected{CellularSessionBatchError::biology}; + } + *next = std::move(*applied); + prepared->intents.admin.assign(admin.begin(), admin.end()); + } auto next_batch = batch_; + if (!admin.empty()) + next_batch.format_version = cellular_batch_admin_format; next_batch.ticks.push_back(std::move(prepared->intents)); next_batch.header.end_tick = tick; if (!encode_cellular_batch(next_batch)) @@ -345,6 +366,7 @@ std::expected CellularSessionBatchBuilder::appe actions_.insert(actions_.end(), prepared->actions.begin(), prepared->actions.end()); mutation_actions_ = next_mutation_actions; refused_births_.insert(refused_births_.end(), refused_births.begin(), refused_births.end()); + admin_results_.insert(admin_results_.end(), admin_results.begin(), admin_results.end()); return {}; } } // namespace evo::server diff --git a/tests/cellular_inheritance_replay_tests.cpp b/tests/cellular_inheritance_replay_tests.cpp index 6965967..92d4a7d 100644 --- a/tests/cellular_inheritance_replay_tests.cpp +++ b/tests/cellular_inheritance_replay_tests.cpp @@ -209,7 +209,9 @@ void version_checks(const CellularLives& base, const CellularBatch& batch) { "action matcher admitted new trace on old owner"); empty.format_version = cellular_batch_format; require(replay_cellular_batch(*old, empty).has_value(), "old replay control failed"); - empty.format_version = 5; + empty.format_version = 5; // ADR-0182 trace 5 needs a rules-13 owner. + refuse(*old, empty, CellularBatchError::version); + empty.format_version = 6; refuse(*old, empty, CellularBatchError::version); require(!encode_cellular_batch(empty), "unknown trace encoded"); auto scope = batch; diff --git a/tests/cellular_publication_tests.cpp b/tests/cellular_publication_tests.cpp index df5b98e..51d8783 100644 --- a/tests/cellular_publication_tests.cpp +++ b/tests/cellular_publication_tests.cpp @@ -10,6 +10,7 @@ #endif #include +#include #include #include #include @@ -1492,9 +1493,100 @@ void run_starting_wildlife_publication_tests() { auto tick = publish_cellular_batch(*runtime, **objects, *lease, head, batch, *recovered->state, identity(8), build, 5s); CHECK(tick); + head = tick->committed; recovered = recover_cellular_checkpoint(*runtime, **objects, key, *lease, builds, 5s); CHECK(recovered && recovered->state->snapshot() == tick_bytes && recovered->state->lives().size() == 32); + + // ADR-0182 (schema 63): queued admin commands publish in trace 5 and bind once. + CHECK(owner->query("INSERT INTO evolution.life_allocators(world_id, next_identity) " + "VALUES ($1, 5000000) ON CONFLICT DO NOTHING", + std::array{std::to_string(key.world)}, 5s)); + const auto enqueue = [&](int kind, int count, std::uint64_t target, std::uint64_t energy) { + const auto rows = owner->query( + "SELECT command_id, target FROM evolution.admin_enqueue_command_v1($1, $2, $3, $4, " + "$5, $6, 0, 0, 0, 0, 'ops', 'test')", + std::array{std::to_string(key.world), std::to_string(key.region), std::to_string(kind), + std::to_string(count), std::to_string(target), std::to_string(energy)}, + 5s); + CHECK(rows && rows->size() == 1); + return std::pair{std::stoull(*(*rows)[0][0]), std::stoull(*(*rows)[0][1])}; + }; + CHECK(!owner->query("SELECT * FROM evolution.admin_enqueue_command_v1($1, $2, 4, 0, 0, 0, 0, " + "0, 0, 0, 'ops', '')", + std::array{std::to_string(key.world), std::to_string(key.region)}, 5s)); + owner = connect_as(env("PGUSER")); + const auto victim = recovered->state->lives().front().identity.life; + const auto [pool_id, pool_target] = enqueue(4, 0, 0, 1'000'000'000); + const auto [spawn_id, first_wild] = enqueue(6, 2, 0, 0); + const auto [remove_id, remove_target] = enqueue(1, 0, victim, 0); + CHECK(pool_target == 0 && first_wild == 5'000'000 && remove_target == victim); + using Kind = evo::server::CellularAdminKind; + const std::vector commands{ + {static_cast(pool_id), Kind::add_pool, 0, 0, {{1'000'000'000}, {}}}, + {static_cast(spawn_id), Kind::spawn_wildlife, 2, first_wild, {}}, + {static_cast(remove_id), Kind::remove_cell, 0, victim, {}}}; + const auto pending = [&] { + const auto rows = + runtime->query("SELECT count(*) FROM evolution.load_pending_admin_commands_v1($1, $2)", + std::array{std::to_string(key.world), std::to_string(key.region)}, 5s); + CHECK(rows && rows->size() == 1); + return std::stoull(*(*rows)[0][0]); + }; + CHECK(pending() == 3); + const auto admin_batch = [&](std::vector recorded) { + CellularBatch made{{key.world, key.region, 1, head.head.commit, head.head.digest, 2, 3}, + {{}}}; + made.format_version = evo::server::cellular_batch_admin_format; + made.ticks.front().lives = checked_value(wildlife_intents(*recovered->state, 3)); + made.ticks.front().admin = std::move(recorded); + return made; + }; + // A command that differs from its queue row is refused and the head stays. + auto tampered = commands; + tampered.front().amount.energy.value = 2'000'000'000; + auto wrong = admin_batch(tampered); + auto wrong_base = evo::fixture::restore(*recovered->state); + CHECK(wrong_base->advance_tick(wrong.ticks.front().lives)); + auto wrong_state = wrong_base->admin_candidate(tampered); + CHECK(wrong_state); + CHECK(!publish_cellular_batch(*runtime, **objects, *lease, head, wrong, **wrong_state, + identity(9), build, 5s)); + runtime = connect_as(env("EVO_PG_RUNTIME_USER")); + CHECK(pending() == 3); + // The exact commands publish, bind and recover. + auto admin = admin_batch(commands); + CHECK(recovered->state->advance_tick(admin.ticks.front().lives)); + std::vector results; + auto applied = recovered->state->admin_candidate(commands, &results); + CHECK(applied && results.size() == 3 && + std::ranges::all_of(results, [](auto r) { return r.applied; })); + const auto applied_bytes = (*applied)->snapshot(); + auto published = publish_cellular_batch(*runtime, **objects, *lease, head, admin, **applied, + identity(10), build, 5s); + CHECK(published); + head = published->committed; + CHECK(pending() == 0); + const auto bound = owner->query( + "SELECT count(*), min(tick), max(tick) FROM evolution.admin_command_applications " + "WHERE command_id IN ($1, $2, $3)", + std::array{std::to_string(pool_id), std::to_string(spawn_id), std::to_string(remove_id)}, + 5s); + CHECK(bound && (*bound)[0][0] == "3" && (*bound)[0][1] == "3" && (*bound)[0][2] == "3"); + recovered = recover_cellular_checkpoint(*runtime, **objects, key, *lease, builds, 5s); + CHECK(recovered && recovered->state->snapshot() == applied_bytes && + recovered->state->lives().size() == 33); + // The same commands cannot bind to another commit. + auto again = admin_batch(commands); + again.header = {key.world, key.region, 1, head.head.commit, head.head.digest, 3, 4}; + again.ticks.front().lives = checked_value(wildlife_intents(*recovered->state, 4)); + auto again_base = evo::fixture::restore(*recovered->state); + CHECK(again_base->advance_tick(again.ticks.front().lives)); + auto again_state = again_base->admin_candidate(commands); + CHECK(again_state); + CHECK(!publish_cellular_batch(*runtime, **objects, *lease, head, again, **again_state, + identity(11), build, 5s)); + runtime = connect_as(env("EVO_PG_RUNTIME_USER")); CHECK(release_region_lease(*runtime, key, *lease, 5s)); std::puts("starting wildlife publication and replacement-owner replay passed"); } diff --git a/tests/cellular_revamp_owner_tests.cpp b/tests/cellular_revamp_owner_tests.cpp index 190b2f1..befa3a4 100644 --- a/tests/cellular_revamp_owner_tests.cpp +++ b/tests/cellular_revamp_owner_tests.cpp @@ -874,6 +874,100 @@ void trace_replays() { "Emerge did not survive the trace"); } +// ADR-0182: operator commands import through the boundary ledger, remove into the spent pool, +// spawn funded wildlife, refuse what does not apply, and replay exactly from trace 5. +void admin_commands_apply_and_replay() { + using Kind = CellularAdminKind; + auto live = revamp_region(); + const auto base = restore(*live); + accounting::Inventory pool{{5'000'000'000}, {}}; + accounting::Inventory bubbles{{400'000'000}, {}}; + bubbles.matter[0].value = 4'000; + const std::vector commands{ + {1, Kind::add_pool, 0, 0, pool}, + {2, Kind::add_bubbles, 4, 0, bubbles}, + {3, Kind::grant_reserve, 0, 100, {{1'000'000}, {}}}, + {4, Kind::spawn_wildlife, 3, 900'000, {}}, + {5, Kind::remove_cell, 0, 101, {}}, + {6, Kind::release_pool, 2, 0, {{1'000'000'000}, {}}}, + {7, Kind::remove_cell, 0, 424'242, {}}, // No such life: refused, nothing changes. + }; + for (const auto& bad : {CellularAdminCommand{8, Kind::add_pool, 0, 0, {}}, + CellularAdminCommand{8, Kind::spawn_wildlife, 65, 900'000, {}}, + CellularAdminCommand{8, Kind::add_bubbles, 0, 0, pool}, + CellularAdminCommand{0, Kind::add_pool, 0, 0, pool}}) + require(!cellular_admin_command_valid(bad), "an invalid command shape was accepted"); + require(!live->admin_candidate(std::vector{commands[1], commands[0]}), + "commands out of order were accepted"); + + // The live run: one tick, then the commands after it. + const auto intents = swim(*live, {}); + checked(live->advance_tick(intents), "tick before the commands"); + const auto spent_before = live->spent(); + const auto deposits_before = live->habitat().patches().size(); + const auto removed_body = life_of(*live, 101).body; + const auto removed_reserve = life_of(*live, 101).reserve; + std::vector results; + auto after = checked(live->admin_candidate(commands, &results), "commands apply"); + require(results.size() == 7 && + std::ranges::all_of(results.begin(), results.begin() + 6, + [](const auto& r) { return r.applied; }) && + !results.back().applied, + "unexpected command results"); + closed(*after); + require(after->lives().size() == live->lives().size() - 1 + 3, "wrong cell count"); + std::size_t wildlife = 0; + for (const auto& life : after->lives()) + if (life.account == LifeAccount{} && life.identity.life >= 900'000 && + life.identity.life < 900'003) + ++wildlife; + require(wildlife == 3, "spawned wildlife missing"); + require(life_of(*after, 100).reserve.energy.value == + life_of(*live, 100).reserve.energy.value + 1'000'000, + "the grant did not reach the reserve"); + const auto corpse = std::ranges::find(after->corpses(), 101ULL, &CellularOwnedCorpse::life_id); + require(corpse != after->corpses().end() && + corpse->cause == biology::CellularDeathCause::removed, + "the removed cell left no removed corpse"); + auto expected_spent = spent_before; + expected_spent.energy.value += pool.energy.value - 1'000'000'000; + for (const auto& part : {removed_body, removed_reserve}) { + expected_spent.energy.value += part.energy.value; + for (std::size_t c = 0; c < 4; ++c) + expected_spent.matter[c].value += part.matter[c].value; + } + require(after->spent() == expected_spent, "the spent pool is off"); + require(after->habitat().patches().size() == deposits_before + 4 + 2, "bubbles were not added"); + require(restore(*after)->snapshot() == after->snapshot(), "commands do not restore"); + auto again = checked(live->admin_candidate(commands), "commands apply again"); + require(again->snapshot() == after->snapshot(), "commands are not deterministic"); + + // The same tick and commands from trace 5 replay to the same state. + CellularBatch batch; + batch.format_version = cellular_batch_admin_format; + batch.header = {base->world(), base->region(), base->frame(), {}, {}, base->tick(), 0}; + batch.header.predecessor.fill(1); + batch.header.predecessor_digest.fill(2); + batch.ticks.push_back({intents, {}, {}, commands}); + batch.header.end_tick = base->tick() + 1; + const auto bytes = checked(encode_cellular_batch(batch), "trace 5 encodes"); + const auto decoded = checked(decode_cellular_batch(bytes), "trace 5 decodes"); + require(decoded == batch, "trace 5 did not round-trip"); + std::vector outcomes; + std::vector replayed_results; + const auto replayed = checked(replay_cellular_batch(*base, decoded, outcomes, replayed_results), + "trace 5 replays"); + require(replayed->snapshot() == after->snapshot() && replayed_results == results, + "trace 5 replay diverged"); + // Trace 4 cannot carry commands, and older owners cannot replay trace 5. + auto old_format = batch; + old_format.format_version = cellular_batch_inheritance_format; + require(!encode_cellular_batch(old_format), "trace 4 carried commands"); + auto mangled = bytes; + mangled[8] = 4; // The version byte: a trace-4 header with nonzero command counts. + require(!decode_cellular_batch(mangled), "a trace 4 with commands decoded"); +} + // The safety revision preserves all five channels, follows both control choices, // separates eligible predator/prey siblings and replays their actual motion exactly. void safety_split(bool become) { @@ -1032,6 +1126,7 @@ void safe_bud_provision(bool become) { } // namespace int main() try { + admin_commands_apply_and_replay(); safety_split(false); safety_split(true); safe_bud_provision(false); diff --git a/tests/grant_broker_tests.cpp b/tests/grant_broker_tests.cpp index 23d21ec..a887101 100644 --- a/tests/grant_broker_tests.cpp +++ b/tests/grant_broker_tests.cpp @@ -550,6 +550,43 @@ void admin_api_tests(const Realm& realm) { CHECK(!owner->query( "SELECT * FROM evolution.admin_set_session_window_v1(9831, 1, 14401, 'x', '')", {}, 5s)); + // Admin commands (schema 63): queued with the admin's identity, shape-checked, listed. + for (const std::string_view bad : + {"/api/regions/1/commands", "/api/regions/1/commands?kind=explode", + "/api/regions/1/commands?kind=add_pool", "/api/regions/1/commands?kind=add_pool&energy=x", + "/api/regions/1/commands?kind=remove_cell&target=7&energy=5", + "/api/regions/1/commands?kind=spawn_wildlife&count=65"}) + CHECK(post(bad).status == 400); + const auto queued = + must(JsonValue::parse(post("/api/regions/1/commands?kind=add_pool&energy=1000&carbon=7" + "&reason=top+up") + .body)); + CHECK(queued.member("id") && queued.member("target")->string() == "0"); + CHECK(post("/api/regions/1/commands?kind=remove_cell&target=424242").status == 200); + CHECK(post("/api/regions/2/commands?kind=add_pool&energy=1").status == 400); + const auto listed_commands = must(JsonValue::parse(get("/api/regions/1/commands", admin).body)); + const auto& newest = listed_commands.member("commands")->array()->at(0); + const auto& older = listed_commands.member("commands")->array()->at(1); + CHECK(newest.member("kind")->string() == "remove_cell" && + newest.member("target")->string() == "424242" && + newest.member("applied_tick")->kind() == JsonValue::Kind::null && + older.member("kind")->string() == "add_pool" && + older.member("amount")->array()->at(0).string() == "1000" && + older.member("amount")->array()->at(1).string() == "7" && + older.member("reason")->string() == "top up" && + older.member("author")->string() == "ops@example.test (ops)"); + { + auto reader = connect_as(env("EVO_PG_RUNTIME_USER")); + const auto pending = reader->query( + "SELECT count(*) FROM evolution.load_pending_admin_commands_v1(9831, 1)", {}, 5s); + CHECK(pending && (*pending)[0][0] == "2"); + CHECK(!reader->query("SELECT * FROM evolution.admin_enqueue_command_v1(9831, 1, 4, 0, 0, " + "1, 0, 0, 0, 0, 'x', '')", + {}, 5s)); + reader = connect_as(env("PGUSER")); + CHECK(!reader->query("DELETE FROM evolution.admin_commands", {}, 5s)); + } + // The read views are the only way in: the runtime role still cannot read the tables. runtime = connect_as(env("EVO_PG_RUNTIME_USER")); CHECK(!runtime->query("SELECT count(*) FROM evolution.life_reservations", {}, 5s)); diff --git a/tests/region_migration_tests.cpp b/tests/region_migration_tests.cpp index 1711efe..6e8a54e 100644 --- a/tests/region_migration_tests.cpp +++ b/tests/region_migration_tests.cpp @@ -228,6 +228,7 @@ void upgrade_across_migration_and_barrier() { " -v migration_sha_60=$(s 0060_cellular_revamp_bundle.sql)" " -v migration_sha_61=$(s 0061_admin_read_views.sql)" " -v migration_sha_62=$(s 0062_session_windows.sql)" + " -v migration_sha_63=$(s 0063_admin_commands.sql)" " -v runtime_role=evo_runtime_test -f migrations/apply.sql" " > /dev/null 2>&1") != 0); CHECK(history(*owner_db) == "1,2,3,4,5,6,7"); @@ -245,7 +246,7 @@ void upgrade_across_migration_and_barrier() { "tools/migrate-postgres.sh\" > /dev/null") == 0); CHECK(history(*owner_db) == "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24," "25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46," - "47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62"); + "47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63"); CHECK(scalar(*owner_db, "SELECT (c.condeferrable AND c.condeferred AND " "c.confrelid = 'evolution.region_commits'::regclass)::text " "FROM pg_catalog.pg_constraint c WHERE c.conrelid = " diff --git a/tools/ci/history-domain-check.sh b/tools/ci/history-domain-check.sh index 3fbcc1d..9a697c8 100644 --- a/tools/ci/history-domain-check.sh +++ b/tools/ci/history-domain-check.sh @@ -75,6 +75,25 @@ awk '/^CREATE FUNCTION evolution\.(publish_cellular_checkpoint|publish_cellular_ awk '/^CREATE OR REPLACE FUNCTION evolution\.valid_cellular_action_input\(/ {capture=1} capture {print} capture && /^END \$\$;/ {capture=0}' \ "$root/migrations/0036_cellular_v5_budding_bundle.sql" | sql > /dev/null sql -v runtime_role="$EVO_PG_RUNTIME_ROLE" > /dev/null <<'SQL' +DROP FUNCTION evolution.publish_cellular_mutation_checkpoint(bytea,bytea,bytea,bytea,bytea,bytea); +ALTER FUNCTION evolution.publish_cellular_mutation_checkpoint_v62(bytea,bytea,bytea,bytea,bytea,bytea) RENAME TO publish_cellular_mutation_checkpoint; +GRANT EXECUTE ON FUNCTION evolution.publish_cellular_mutation_checkpoint(bytea,bytea,bytea,bytea,bytea,bytea) TO :"runtime_role"; +DROP FUNCTION evolution.publish_cellular_checkpoint(bytea,bytea,bytea,bytea); +ALTER FUNCTION evolution.publish_cellular_checkpoint_v62(bytea,bytea,bytea,bytea) RENAME TO publish_cellular_checkpoint; +GRANT EXECUTE ON FUNCTION evolution.publish_cellular_checkpoint(bytea,bytea,bytea,bytea) TO :"runtime_role"; +DROP FUNCTION evolution.bind_cellular_admin_commands(bytea,bytea); +DROP FUNCTION evolution.admin_commands_v1(evolution.u64,evolution.u64); +DROP FUNCTION evolution.load_pending_admin_commands_v1(evolution.u64,evolution.u64); +DROP FUNCTION evolution.admin_enqueue_command_v1(evolution.u64,evolution.u64,integer,integer,evolution.u64,evolution.u64,evolution.u64,evolution.u64,evolution.u64,evolution.u64,text,text); +DROP TABLE evolution.admin_command_applications, evolution.admin_commands; +DROP FUNCTION evolution.cellular_mutation_selections(bytea,bytea); +ALTER FUNCTION evolution.cellular_mutation_selections_v62(bytea,bytea) RENAME TO cellular_mutation_selections; +DROP FUNCTION evolution.cellular_births(bytea,bytea); +ALTER FUNCTION evolution.cellular_births_v62(bytea,bytea) RENAME TO cellular_births; +DROP FUNCTION evolution.cellular_trace_normal(bytea,bytea); +DROP FUNCTION evolution.cellular_trace_admin(bytea,bytea); +DROP FUNCTION evolution.cellular_trace_without_admin(bytea); +DROP FUNCTION evolution.le_bytes(numeric,integer); DROP FUNCTION evolution.admin_session_windows_v1(evolution.u64,evolution.u64); DROP FUNCTION evolution.admin_set_session_window_v1(evolution.u64,evolution.u64,integer,text,text); DROP FUNCTION evolution.load_session_window_v1(evolution.u64,evolution.u64); diff --git a/tools/ci/postgres-check.sh b/tools/ci/postgres-check.sh index 6e826be..fba6032 100644 --- a/tools/ci/postgres-check.sh +++ b/tools/ci/postgres-check.sh @@ -172,7 +172,7 @@ PGDATABASE=upgrade_path psql -X -v ON_ERROR_STOP=1 -1 -v "migration_sha=$v1_sha" -f "$root/migrations/0001_region_bootstrap.sql" > /dev/null test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',') FROM evolution.schema_migrations")" = 1 PGDATABASE=upgrade_path bash "$root/tools/migrate-postgres.sh" -test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62 +test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63 # v2 heads have no payload binding: v2->v3 fails closed and leaves history at v2. v2_sha="$(sha256sum "$root/migrations/0002_region_fencing.sql")" v2_sha="${v2_sha%% *}" @@ -193,7 +193,7 @@ PGDATABASE=v3_commits psql -X -v ON_ERROR_STOP=1 \ -c "INSERT INTO evolution.retained_builds VALUES (decode(repeat('b1', 32), 'hex'))" \ -c "SELECT evolution.provision_region_checkpoint(decode('4145564f52434d31' || '01000000' || '01000000' || '0100000000000000' || '0100000000000000' || '0900000000000000' || repeat('11', 16) || repeat('00', 8) || repeat('00', 16) || repeat('00', 32) || repeat('00', 16) || repeat('00', 16) || repeat('00', 8) || '50c3000000000000' || '020000000200000001000000010000000100000001000000010000000100000001000000' || repeat('b1', 32) || '00000000' || '00000000' || '01000000' || '6400000000000000' || repeat('a0', 32), 'hex'))" > /dev/null PGDATABASE=v3_commits bash "$root/tools/migrate-postgres.sh" -test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 62:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62 +test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 63:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63 test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) FROM evolution.region_commits")" = 1 # Review P1: a v4 database holding a published (possibly terminal) checkpoint has # no durable decisions for it. The v5 upgrade must refuse before changing anything. @@ -247,7 +247,7 @@ PGDATABASE=v6_populated psql -X -v ON_ERROR_STOP=1 \ -c "INSERT INTO evolution.retained_builds VALUES (decode(repeat('b1', 32), 'hex'))" \ -c "SELECT evolution.provision_region_checkpoint(decode('4145564f52434d31' || '02000000' || '01000000' || '0100000000000000' || '0100000000000000' || '0900000000000000' || repeat('11', 16) || repeat('00', 8) || repeat('00', 16) || repeat('00', 32) || repeat('00', 16) || repeat('00', 16) || repeat('00', 8) || '50c3000000000000' || '020000000200000001000000010000000100000001000000010000000100000001000000' || repeat('b1', 32) || '00000000' || '00000000' || '01000000' || '6400000000000000' || repeat('a0', 32) || repeat('00', 80), 'hex'))" > /dev/null PGDATABASE=v6_populated bash "$root/tools/migrate-postgres.sh" -test "$(PGDATABASE=v6_populated psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62 +test "$(PGDATABASE=v6_populated psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63 test "$(PGDATABASE=v6_populated psql -XAtc "SELECT count(*) FROM evolution.region_commits c, LATERAL evolution.parse_checkpoint_manifest(c.manifest) m WHERE m.commit_sequence = 0 AND m.predecessor_commit IS NULL")" = 1 test "$(PGDATABASE=v6_populated psql -XAtc "SELECT string_agg(conname, ',') FROM pg_constraint WHERE conrelid = 'evolution.region_heads'::regclass AND contype = 'c' AND pg_get_constraintdef(oid) LIKE '%end_tick%'")" = region_heads_published_tick phase permission-audit @@ -305,9 +305,9 @@ expect_audit_failure 'REVOKE EXECUTE ON FUNCTION evolution.load_world_cellular_m expect_audit_failure 'GRANT EXECUTE ON FUNCTION evolution.fork_restored_history_v1(uuid) TO evo_runtime_test' 'REVOKE EXECUTE ON FUNCTION evolution.fork_restored_history_v1(uuid) FROM evo_runtime_test' phase checksum-drift # Unknown future versions and checksum drift must not be repaired silently. -psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=63 WHERE version=62' +psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=64 WHERE version=63' if bash "$root/tools/migrate-postgres.sh"; then exit 1; fi -psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=62 WHERE version=63" +psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=63 WHERE version=64" psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET sha256=repeat('0',64) WHERE version=14" if bash "$root/tools/migrate-postgres.sh"; then exit 1; fi psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET sha256=repeat('0',64) WHERE version=13" diff --git a/tools/migrate-postgres.sh b/tools/migrate-postgres.sh index f53d365..498590a 100644 --- a/tools/migrate-postgres.sh +++ b/tools/migrate-postgres.sh @@ -140,6 +140,8 @@ digest_61="$(sha256sum "$root/migrations/0061_admin_read_views.sql")" digest_61="${digest_61%% *}" digest_62="$(sha256sum "$root/migrations/0062_session_windows.sql")" digest_62="${digest_62%% *}" +digest_63="$(sha256sum "$root/migrations/0063_admin_commands.sql")" +digest_63="${digest_63%% *}" psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_2=$digest_2" --set="migration_sha_3=$digest_3" \ --set="migration_sha_4=$digest_4" --set="migration_sha_5=$digest_5" \ @@ -189,6 +191,6 @@ psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_55=$digest_55" --set="migration_sha_56=$digest_56" \ --set="migration_sha_57=$digest_57" --set="migration_sha_58=$digest_58" --set="migration_sha_59=$digest_59" \ --set="migration_sha_60=$digest_60" --set="migration_sha_61=$digest_61" \ - --set="migration_sha_62=$digest_62" "${admin[@]}" \ + --set="migration_sha_62=$digest_62" --set="migration_sha_63=$digest_63" "${admin[@]}" \ --set="runtime_role=$runtime_role" \ --file="$root/migrations/apply.sql"