feat(admin): live tuning revisions adopted at recorded ticks (schema 66)
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (checks) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m47s
Foundation / postgres-tools (push) Successful in 29s
Foundation / server-image (push) Successful in 4m42s
Foundation / image (push) Successful in 5m55s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (checks) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m47s
Foundation / postgres-tools (push) Successful in 29s
Foundation / server-image (push) Successful in 4m42s
Foundation / image (push) Successful in 5m55s
ADR-0182 section 13. The admin portal's Tuning page edits the world's rules-13 tuning (the Git revamp file format); the admin API checks it, stores an append-only revision and queues an adopt_tuning admin command (kind 7: revision and rules digest) for every region of the world. - Lifecycle: adopt_tuning replaces the region's rules after births when a resolver names the rules for that revision and digest; the safety revision cannot change live. At most one adoption per batch. - Host: fetches the revision text, rebuilds and digests the rules before appending the command; publication and recovery name the rules from the successor state, checked against the recorded digest. - Schema 66: tuning_revisions, admin_set_tuning_v1, load_tuning_revision_v1 (runtime), admin_tuning_revisions_v1, admin_tuning_text_v1. - OpsServer: optional bounded request bodies (Content-Length, 413). - Region view: rules_sha256, so the portal shows each region's revision. - Portal: Tuning page; baseline from the mounted cellular-revamp.tuning. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,10 +2,12 @@
|
||||
//
|
||||
// evo-admin-api --listen ADDRESS:PORT --world N --jwks JSON --oidc-issuer URL
|
||||
// --audience NAME --client NAME --role NAME --assets DIR
|
||||
// --pg-passfile F --pg-service F [--region REGION=HOST:PORT ...]
|
||||
// --pg-passfile F --pg-service F [--region REGION=HOST:PORT ...] [--baseline-tuning F]
|
||||
//
|
||||
// --assets holds the portal's index.html, app.js and style.css. Each --region names a
|
||||
// region's operations address, whose /admin/view the portal may show. No token is printed.
|
||||
// region's operations address, whose /admin/view the portal may show; those regions adopt
|
||||
// tuning revisions. --baseline-tuning is the revamp tuning file they opened with, shown
|
||||
// until the first revision. No token is printed.
|
||||
#include "evo/persistence/postgres.hpp"
|
||||
#include "evo/server/admin/admin_api.hpp"
|
||||
|
||||
@@ -33,7 +35,7 @@ void on_stop(int) {
|
||||
}
|
||||
|
||||
struct Options {
|
||||
std::string listen, jwks, oidc_issuer, audience, client, role, assets;
|
||||
std::string listen, jwks, oidc_issuer, audience, client, role, assets, baseline_tuning;
|
||||
std::uint64_t world{};
|
||||
std::map<std::uint64_t, std::string> regions; // Region to its operations address.
|
||||
persistence::PgOptions pg;
|
||||
@@ -70,6 +72,8 @@ std::optional<Options> parse(int argc, char** argv) {
|
||||
o.role = value;
|
||||
else if (flag == "--assets")
|
||||
o.assets = value;
|
||||
else if (flag == "--baseline-tuning")
|
||||
o.baseline_tuning = value;
|
||||
else if (flag == "--pg-service")
|
||||
pg_service = value;
|
||||
else if (flag == "--pg-passfile")
|
||||
@@ -157,6 +161,14 @@ int main(int argc, char** argv) {
|
||||
config.world = options->world;
|
||||
for (const auto& [region, address] : options->regions)
|
||||
config.view_regions.push_back(region);
|
||||
if (!options->baseline_tuning.empty()) {
|
||||
auto baseline = read_file(options->baseline_tuning);
|
||||
if (!baseline) {
|
||||
std::puts("error baseline-tuning");
|
||||
return 2;
|
||||
}
|
||||
config.baseline_tuning = std::move(*baseline);
|
||||
}
|
||||
config.portal_config_json = "{\"issuer\":\"" + options->oidc_issuer + "\",\"client_id\":\"" +
|
||||
options->client + "\",\"world\":" + std::to_string(options->world) +
|
||||
"}\n";
|
||||
@@ -191,9 +203,12 @@ int main(int argc, char** argv) {
|
||||
static_cast<unsigned long long>(options->world));
|
||||
std::fflush(stdout);
|
||||
while (stop_requested == 0) {
|
||||
(*listening)->serve_api([&](const server::ApiRequest& request) {
|
||||
return api.handle(request, wall_seconds(), std::chrono::steady_clock::now());
|
||||
});
|
||||
(*listening)
|
||||
->serve_api(
|
||||
[&](const server::ApiRequest& request) {
|
||||
return api.handle(request, wall_seconds(), std::chrono::steady_clock::now());
|
||||
},
|
||||
server::OpsServer::max_api_body_bytes); // Tuning texts (ADR-0182 section 13).
|
||||
std::this_thread::sleep_for(5ms);
|
||||
}
|
||||
std::puts("admin api stopped");
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// ADR-0182 read-only admin portal. Keycloak sign-in (authorization code with PKCE), then the
|
||||
// ADR-0182 admin portal. Keycloak sign-in (authorization code with PKCE), then the
|
||||
// admin API on this origin. Tokens stay in this tab: the access token in memory, the refresh
|
||||
// and ID tokens in sessionStorage so a reload keeps the session. No third-party code.
|
||||
|
||||
@@ -151,17 +151,20 @@ function signOut() {
|
||||
}
|
||||
|
||||
class ApiError extends Error {
|
||||
constructor(status, code) { super(code || `HTTP ${status}`); this.status = status; this.code = code; }
|
||||
constructor(status, code, detail = {}) { super(code || `HTTP ${status}`); this.status = status; this.code = code; this.detail = detail; }
|
||||
}
|
||||
async function api(path, retry = true, method = 'GET') {
|
||||
// `body` is sent as plain text (only tuning revisions take one).
|
||||
async function api(path, retry = true, method = 'GET', body = undefined) {
|
||||
const t = await token();
|
||||
if (!t) throw new ApiError(401, 'signed_out');
|
||||
const res = await fetch(path, { method, headers: { Authorization: `Bearer ${t}` }, cache: 'no-store' });
|
||||
if (res.status === 401 && retry) { access = null; return api(path, false, method); }
|
||||
const headers = { Authorization: `Bearer ${t}` };
|
||||
if (body !== undefined) headers['Content-Type'] = 'text/plain; charset=utf-8';
|
||||
const res = await fetch(path, { method, headers, body, cache: 'no-store' });
|
||||
if (res.status === 401 && retry) { access = null; return api(path, false, method, body); }
|
||||
if (!res.ok) {
|
||||
let code = null;
|
||||
try { code = (await res.json()).error; } catch { /* plain text */ }
|
||||
throw new ApiError(res.status, code);
|
||||
let detail = {};
|
||||
try { detail = await res.json(); } catch { /* plain text */ }
|
||||
throw new ApiError(res.status, detail.error || (res.status === 413 ? 'too_large' : null), detail);
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
@@ -423,6 +426,7 @@ function amountInputs() {
|
||||
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',
|
||||
adopt_tuning: 'adopt tuning',
|
||||
};
|
||||
|
||||
// Admin commands (ADR-0182): queued now, applied by the region at its next ticks.
|
||||
@@ -460,7 +464,8 @@ function commandsCard(region) {
|
||||
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}` : ''),
|
||||
c.count ? ` ×${c.count}` : '', c.kind === 'remove_cell' || c.kind === 'grant_reserve' ? ` ${c.target}` : '',
|
||||
c.kind === 'adopt_tuning' ? ` revision ${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) {
|
||||
@@ -777,6 +782,135 @@ async function regionPage(region, params) {
|
||||
stopPage = () => { clearTimeout(timer); window.removeEventListener('resize', onResize); };
|
||||
}
|
||||
|
||||
// ---------- Tuning (schema 66) ----------
|
||||
const tuningErrors = {
|
||||
too_large: 'the text is too large', syntax: 'not a `key = value` line', unknown_key: 'unknown key',
|
||||
duplicate_key: 'key given twice', invalid_value: 'not an integer', out_of_range: 'value out of range',
|
||||
missing_key: 'required key missing', inconsistent: 'values contradict each other',
|
||||
unsupported_version: 'unsupported tuning.version',
|
||||
};
|
||||
function tuningProblem(e) {
|
||||
const d = e.detail || {};
|
||||
if (e.code === 'invalid_tuning')
|
||||
return `${d.line ? `Line ${d.line}: ` : ''}${tuningErrors[d.kind] || d.kind}${d.key ? ` (${d.key})` : ''}`;
|
||||
if (e.code === 'invalid_rules') return 'The values parse but do not make valid rules (a bound or gate is out of range).';
|
||||
if (e.code === 'safety_version_changed') return `reproduction.safety_version must stay ${d.expected}: it is fixed when a region opens.`;
|
||||
if (e.code === 'invalid_text') return 'The text must be plain ASCII.';
|
||||
if (e.code === 'too_large') return 'The text is over 64 KiB.';
|
||||
return `Refused: ${e.code || e.message}`;
|
||||
}
|
||||
// Selects line `n` (1-based) of a textarea so the problem is in view.
|
||||
function selectLine(area, n) {
|
||||
const lines = area.value.split('\n');
|
||||
if (!n || n > lines.length) return;
|
||||
const start = lines.slice(0, n - 1).reduce((s, l) => s + l.length + 1, 0);
|
||||
area.focus();
|
||||
area.setSelectionRange(start, start + lines[n - 1].length);
|
||||
const lineHeight = parseFloat(getComputedStyle(area).lineHeight) || 18;
|
||||
area.scrollTop = Math.max(0, (n - 5) * lineHeight);
|
||||
}
|
||||
|
||||
async function tuningPage() {
|
||||
const data = await api('/api/tuning');
|
||||
const area = h('textarea', { class: 'tuning', spellcheck: 'false', 'aria-label': 'Tuning text' });
|
||||
const source = h('span', { class: 'muted' });
|
||||
const reason = h('input', { type: 'text', class: 'wide', maxlength: 200, placeholder: 'Reason (recorded with the revision)' });
|
||||
const note = h('div', { class: 'small' });
|
||||
const regionsBox = h('div', { class: 'muted' }, 'Loading…');
|
||||
const byDigest = new Map(data.revisions.map((r) => [r.rules_sha256, `revision ${r.revision}`]));
|
||||
if (data.baseline && data.baseline.rules_sha256 && !byDigest.has(data.baseline.rules_sha256))
|
||||
byDigest.set(data.baseline.rules_sha256, 'the baseline file');
|
||||
let loadedText = '';
|
||||
const load = (text, label) => { area.value = text; loadedText = text; source.textContent = label; note.textContent = ''; };
|
||||
if (data.newest) load(data.newest.text, `Editing a copy of revision ${data.newest.revision}`);
|
||||
else if (data.baseline) load(data.baseline.text, 'Editing a copy of the baseline file (no revisions yet)');
|
||||
else load('', 'No baseline file and no revisions');
|
||||
|
||||
const say = (text, cls = 'muted') => { note.className = `small ${cls}`; note.textContent = text; };
|
||||
const check = async () => {
|
||||
try {
|
||||
const r = await api('/api/tuning?check=1', true, 'POST', area.value);
|
||||
const known = byDigest.get(r.rules_sha256);
|
||||
say(known ? `Valid. These are the same rules as ${known}.` : `Valid. New rules ${r.rules_sha256.slice(0, 12)}…`, 'good');
|
||||
return r;
|
||||
} catch (e) {
|
||||
say(tuningProblem(e), 'error');
|
||||
if (e.detail && e.detail.line) selectLine(area, e.detail.line);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
const adopt = async () => {
|
||||
const ok = await check();
|
||||
if (!ok) return;
|
||||
const regions = data.regions.length === 1 ? `region ${data.regions[0]}` : `all ${data.regions.length} regions`;
|
||||
if (!window.confirm(`Adopt this tuning in ${regions} of world ${data.world}?\n\nEach region switches at its next tick; the change is recorded in its history and cannot be undone except by adopting another revision.`)) return;
|
||||
try {
|
||||
const q = new URLSearchParams();
|
||||
if (reason.value.trim()) q.set('reason', reason.value.trim());
|
||||
const r = await api(`/api/tuning?${q}`, true, 'POST', area.value);
|
||||
say(`Stored revision ${r.revision}; queued for ${r.regions} region(s).`, 'good');
|
||||
reason.value = '';
|
||||
setTimeout(() => route(), 1500);
|
||||
} catch (e) {
|
||||
say(tuningProblem(e), 'error');
|
||||
}
|
||||
};
|
||||
const revert = () => load(loadedText, source.textContent);
|
||||
|
||||
// Which rules each region runs now, from its live view.
|
||||
(async () => {
|
||||
const rows = await Promise.all(data.regions.map(async (region) => {
|
||||
try {
|
||||
const v = await api(`/api/regions/${region}/view`);
|
||||
const running = v.rules_sha256 ? byDigest.get(v.rules_sha256) : null;
|
||||
return h('tr', {}, h('td', {}, h('a', { href: `#/regions/${region}` }, `Region ${region}`)),
|
||||
h('td', {}, v.rules_sha256
|
||||
? (running ? h('span', { class: 'badge alive' }, running) : h('span', { class: 'badge warn', title: v.rules_sha256 }, `other rules ${v.rules_sha256.slice(0, 8)}…`))
|
||||
: h('span', { class: 'muted' }, `rules ${v.rules}`)),
|
||||
h('td', { class: 'muted' }, `tick ${fmtInt(v.tick)}`));
|
||||
} catch (e) {
|
||||
return h('tr', {}, h('td', {}, `Region ${region}`), h('td', { class: 'muted', colspan: 2 }, `unavailable: ${e.code || e.message}`));
|
||||
}
|
||||
}));
|
||||
regionsBox.replaceChildren(rows.length ? h('table', { class: 'compact' }, h('tbody', {}, rows)) : 'No regions configured.');
|
||||
})();
|
||||
|
||||
const revisions = data.revisions.length ? h('table', { class: 'compact' }, h('tbody', {}, data.revisions.map((r) =>
|
||||
h('tr', {},
|
||||
h('td', { class: 'mono' }, `#${r.revision}`),
|
||||
h('td', {}, h('div', {}, r.reason || h('span', { class: 'muted' }, 'no reason')),
|
||||
h('div', { class: 'muted', title: r.author || '' }, `${(r.author || '').split(' (')[0]} · ${fmtAgo(r.created_at)}`)),
|
||||
h('td', {}, r.adopted >= r.regions && r.regions > 0
|
||||
? h('span', { class: 'badge alive', title: r.last_tick ? `last at tick ${r.last_tick}` : '' }, `adopted ${r.adopted}/${r.regions}`)
|
||||
: h('span', { class: 'badge warn' }, `pending ${r.adopted}/${r.regions}`)),
|
||||
h('td', {}, h('button', {
|
||||
onclick: async () => {
|
||||
try { const t = await api(`/api/tuning/${r.revision}`); load(t.text, `Editing a copy of revision ${t.revision}`); } catch (e) { say(`Could not load: ${e.code || e.message}`, 'error'); }
|
||||
},
|
||||
}, 'Load')))))) : h('p', { class: 'muted' }, 'No revisions yet: regions run the rules they opened with.');
|
||||
|
||||
show(
|
||||
h('div', { class: 'head' },
|
||||
h('div', {}, h('h1', {}, 'Cellular tuning'), h('span', { class: 'muted' }, `World ${data.world} · rules 13 · every region adopts the same revision`)),
|
||||
h('div', { class: 'tools' }, h('button', { onclick: () => route() }, 'Refresh'))),
|
||||
h('div', { class: 'tuning-layout' },
|
||||
h('div', { class: 'card' },
|
||||
h('div', { class: 'head' }, source,
|
||||
h('div', { class: 'tools' },
|
||||
data.baseline ? h('button', { class: 'ghost', onclick: () => load(data.baseline.text, 'Editing a copy of the baseline file') }, 'Load baseline') : null,
|
||||
h('button', { class: 'ghost', onclick: revert }, 'Revert'))),
|
||||
area,
|
||||
h('div', { class: 'action' }, reason,
|
||||
h('div', { class: 'row' }, h('button', { onclick: check }, 'Check'), h('button', { class: 'primary', onclick: adopt }, 'Adopt')),
|
||||
note)),
|
||||
h('div', { class: 'side' },
|
||||
h('div', { class: 'card' }, h('h2', {}, 'Regions'), regionsBox),
|
||||
h('div', { class: 'card' }, h('h2', {}, 'Revisions'), revisions),
|
||||
h('div', { class: 'card muted small' },
|
||||
'The file format is the Git revamp tuning file (key = value, integers). A region adopts a revision at a tick recorded in its trace, so replay and recovery reproduce it. ',
|
||||
'reproduction.safety_version cannot change live, and playtest.* keys only matter when a region opens.'))));
|
||||
}
|
||||
|
||||
// ---------- Routing ----------
|
||||
async function route() {
|
||||
stopPage();
|
||||
@@ -789,6 +923,7 @@ async function route() {
|
||||
if (parts[0] === 'accounts' && parts[1]) await accountPage(parts[1]);
|
||||
else if (parts[0] === 'regions' && parts[1]) await regionPage(parts[1], params);
|
||||
else if (parts[0] === 'regions') await regionsPage();
|
||||
else if (parts[0] === 'tuning') await tuningPage();
|
||||
else await accountsPage();
|
||||
main().focus({ preventScroll: true });
|
||||
} catch (e) {
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
<span class="section-name">Cellular</span>
|
||||
<a href="#/accounts" data-route="accounts">Accounts</a>
|
||||
<a href="#/regions" data-route="regions">Regions</a>
|
||||
<a href="#/tuning" data-route="tuning">Tuning</a>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="who">
|
||||
|
||||
@@ -181,6 +181,7 @@ dl.facts dd { margin: 0; }
|
||||
.toggles { display: flex; flex-direction: column; gap: 6px; }
|
||||
.toggles label { display: flex; gap: 8px; align-items: center; cursor: pointer; }
|
||||
.error { color: var(--bad); }
|
||||
.good { color: var(--good); }
|
||||
.center { display: grid; place-items: center; min-height: 50vh; text-align: center; }
|
||||
.window-controls { display: flex; gap: 8px; margin: 10px 0 8px; flex-wrap: wrap; }
|
||||
.side input[type="text"] { width: 100%; min-width: 0; }
|
||||
@@ -194,3 +195,23 @@ button:disabled { opacity: 0.5; cursor: default; }
|
||||
input.amount, input.count { min-width: 0; width: 100%; }
|
||||
input.count { width: 80px; }
|
||||
button.danger { color: var(--bad); border-color: var(--bad); }
|
||||
.tuning-layout { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 14px; }
|
||||
@media (max-width: 1000px) { .tuning-layout { grid-template-columns: 1fr; } }
|
||||
textarea.tuning {
|
||||
display: block;
|
||||
width: 100%;
|
||||
min-height: 60vh;
|
||||
box-sizing: border-box;
|
||||
font-family: var(--mono);
|
||||
font-size: 12.5px;
|
||||
line-height: 1.5;
|
||||
color: var(--text);
|
||||
background: var(--panel-2);
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 6px;
|
||||
padding: 10px 12px;
|
||||
resize: vertical;
|
||||
tab-size: 4;
|
||||
}
|
||||
textarea.tuning:focus { outline: none; border-color: var(--accent); }
|
||||
.card input[type="text"].wide { width: 100%; min-width: 0; box-sizing: border-box; }
|
||||
|
||||
@@ -53,6 +53,7 @@
|
||||
// --primitive-* flags enable the v5 new-life listener; biology ticks and genesis
|
||||
// provisioning remain disabled.
|
||||
#include "evo/biology/founder.hpp"
|
||||
#include "evo/persistence/sha256.hpp"
|
||||
#include "evo/processes/development_opening.hpp"
|
||||
#include "evo/server/cellular_session_batch.hpp"
|
||||
#include "evo/server/lifecycle/cellular_admin_view.hpp"
|
||||
@@ -1108,7 +1109,18 @@ int serve_cellular(Options& o) {
|
||||
id.bytes = account;
|
||||
return bridge->sessions().holder(id).has_value();
|
||||
};
|
||||
return {200, server::render_cellular_admin_view(*state, online),
|
||||
// The digest tuning revisions are adopted by (ADR-0182 section 13).
|
||||
std::string rules_sha256;
|
||||
if (state->rules_version() >= server::cellular_lives_revamp_rules)
|
||||
if (const auto digest = persistence::sha256(
|
||||
server::encode_cellular_revamp_rules(state->revamp_rules())))
|
||||
for (const auto byte : *digest) {
|
||||
constexpr std::string_view digits = "0123456789abcdef";
|
||||
rules_sha256.push_back(digits[byte >> 4U]);
|
||||
rules_sha256.push_back(digits[byte & 0x0FU]);
|
||||
}
|
||||
return {200,
|
||||
server::render_cellular_admin_view(*state, online, {}, rules_sha256),
|
||||
"application/json"};
|
||||
}
|
||||
return {404, "not found\n"};
|
||||
@@ -1203,8 +1215,10 @@ int serve_cellular(Options& o) {
|
||||
provisional.digest.fill(0xFF);
|
||||
if (auto created =
|
||||
server::CellularSessionBatchBuilder::create(candidate, provisional);
|
||||
created)
|
||||
created) {
|
||||
next.emplace(std::move(*created));
|
||||
next->set_tuning_resolver(host.tuning_resolver());
|
||||
}
|
||||
}
|
||||
flight_ticks = batch_builder->batch().ticks.size();
|
||||
const auto refusals = batch_builder->refused_births();
|
||||
@@ -1312,9 +1326,10 @@ int serve_cellular(Options& o) {
|
||||
if (!batch_builder) {
|
||||
auto created = server::CellularSessionBatchBuilder::create(
|
||||
*state, host.committed()->head);
|
||||
if (created)
|
||||
if (created) {
|
||||
batch_builder.emplace(std::move(*created));
|
||||
else
|
||||
batch_builder->set_tuning_resolver(host.tuning_resolver());
|
||||
} else
|
||||
report_stall("create", created.error(), tick);
|
||||
}
|
||||
if (!batch_builder) {
|
||||
@@ -1338,7 +1353,8 @@ int serve_cellular(Options& o) {
|
||||
});
|
||||
const std::span<const persistence::CellularMutationReservation> mutations =
|
||||
unbound;
|
||||
// ADR-0182: queued admin commands not already in this batch.
|
||||
// ADR-0182: queued admin commands not already in this batch, and at
|
||||
// most one tuning adoption per batch (its successor names its rules).
|
||||
auto admin = host.unbound_admin_commands();
|
||||
std::erase_if(admin, [&](const auto& queued) {
|
||||
return std::ranges::any_of(
|
||||
@@ -1348,6 +1364,20 @@ int serve_cellular(Options& o) {
|
||||
previous.admin.end();
|
||||
});
|
||||
});
|
||||
bool adopting = std::ranges::any_of(
|
||||
batch_builder->batch().ticks, [](const auto& previous) {
|
||||
return std::ranges::any_of(previous.admin, [](const auto& c) {
|
||||
return c.kind == server::CellularAdminKind::adopt_tuning;
|
||||
});
|
||||
});
|
||||
std::erase_if(admin, [&](const auto& queued) {
|
||||
if (queued.kind != server::CellularAdminKind::adopt_tuning)
|
||||
return false;
|
||||
if (adopting)
|
||||
return true;
|
||||
adopting = true;
|
||||
return false;
|
||||
});
|
||||
const auto admin_before = batch_builder->admin_results().size();
|
||||
auto appended = batch_builder->append_with_birth_refusals(
|
||||
tick, baseline, actions, births, mutations, admin);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# ADR-0182 read-only admin API and portal. One replica; it holds no state but a short view
|
||||
# ADR-0182 admin API and portal. One replica; it holds no state but a short view
|
||||
# cache. The realm's public signing key is pinned below (ADMIN_JWKS, the same key as the grant
|
||||
# broker's); a Keycloak key rotation needs a Git update in both.
|
||||
apiVersion: apps/v1
|
||||
@@ -47,7 +47,7 @@ spec:
|
||||
--audience aevolution-admin --client aevolution-admin-portal \
|
||||
--role aevolution-admin --assets /usr/local/share/evo-admin-portal \
|
||||
--pg-service /run/postgres/pg_service.conf --pg-passfile /run/postgres/pgpass \
|
||||
"${regions[@]}"
|
||||
--baseline-tuning /etc/evo/tuning/cellular-revamp.tuning "${regions[@]}"
|
||||
env:
|
||||
- {name: ADMIN_WORLD, value: "100"}
|
||||
# Space-separated REGION=HOST:PORT: each region's operations address for /admin/view.
|
||||
@@ -77,7 +77,16 @@ spec:
|
||||
capabilities: {drop: [ALL]}
|
||||
volumeMounts:
|
||||
- {name: connection, mountPath: /run/postgres, readOnly: true}
|
||||
# The revamp tuning file the regions opened with: the portal's baseline until the
|
||||
# first tuning revision (ADR-0182 section 13).
|
||||
- {name: tuning, mountPath: /etc/evo/tuning, readOnly: true}
|
||||
volumes:
|
||||
- name: tuning
|
||||
configMap:
|
||||
name: cellular-tuning
|
||||
defaultMode: 0444
|
||||
items:
|
||||
- {key: cellular-revamp.tuning, path: cellular-revamp.tuning}
|
||||
- name: connection
|
||||
secret:
|
||||
secretName: aevolution-admin-pg
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
# Project status and next steps
|
||||
|
||||
## Live tuning - 2026-10-27
|
||||
|
||||
The admin portal's Tuning page edits the world's rules-13 tuning (the Git revamp file
|
||||
format) and adopts it in every region at a recorded tick, without a new image or region:
|
||||
upkeep, burn, feeding, predation, the spent-pool drip, mutation offers and costs,
|
||||
reproduction and organisms. Revisions are append-only with author and reason (schema 66);
|
||||
the reproduction safety revision stays fixed. See
|
||||
[ADR-0182](adr/0182-admin-portal-and-world-tuning.md) section 13.
|
||||
|
||||
## Resource schedules - 2026-10-27
|
||||
|
||||
The admin portal can schedule recurring bubbles (the game source), spent-pool releases and
|
||||
|
||||
@@ -2,11 +2,11 @@
|
||||
|
||||
## Status and scope
|
||||
|
||||
Accepted direction, 2026-10-27, from the maintainer's decisions in chat; implementation
|
||||
pending. DEV (world 100) only; stage and World0 need their own review. Applies to a new owner
|
||||
format and new openings only:
|
||||
regions 12-16 keep their recorded rules and Git-mounted tuning. Supersedes, for that new
|
||||
format, ADR-0181's "Git tuning affects future openings only. No live retune."
|
||||
Accepted direction, 2026-10-27, from the maintainer's decisions in chat; delivered in parts
|
||||
(sections 8-13). DEV (world 100) only; stage and World0 need their own review. Supersedes, for
|
||||
rules-13 regions, ADR-0181's "Git tuning affects future openings only. No live retune": since
|
||||
schema 66 (section 13) a running rules-13 region adopts tuning revisions at recorded ticks.
|
||||
Regions on older rules keep their recorded rules.
|
||||
|
||||
## Context
|
||||
|
||||
@@ -230,7 +230,46 @@ no ticks) accumulates nothing. Versions are append-only; the newest decides.
|
||||
The spent pool's own drip (interval, bubble caps, bubbles per drip, per-channel scale) is a
|
||||
rules setting and stays with the tuning amendments.
|
||||
|
||||
Still pending from this ADR: tuning amendments (sections 1-2, including the drip rules),
|
||||
### 13. Tuning amendments (delivered, schema 66)
|
||||
|
||||
Sections 1-2 are delivered without a new owner format or opening, so running rules-13 regions
|
||||
(region 16 included) can be retuned:
|
||||
|
||||
- **Revisions.** Append-only `tuning_revisions(revision, world, text, rules_sha256, author,
|
||||
reason, created_at)`. The text is the Git revamp tuning file format, kept as the admin wrote
|
||||
it (comments included); `rules_sha256` is the SHA-256 of `encode_cellular_revamp_rules` for
|
||||
the rules it produces, the same canonical bytes the owner snapshot stores. The admin API
|
||||
parses and validates the text (`parse_cellular_revamp_tuning`, `cellular_revamp_rules`,
|
||||
`cellular_revamp_valid`) before storing it, and `admin_set_tuning_v1` queues the adoption
|
||||
in every region of the world in the same transaction.
|
||||
- **Adoption is an admin command.** Kind 7 `adopt_tuning` carries the revision as its target
|
||||
and the rules digest in its four amount words (little-endian; `other` zero). It is queued
|
||||
only through a revision, never by kind. The region fetches the revision's text
|
||||
(`load_tuning_revision_v1`), rebuilds the rules with its own parser and appends the command
|
||||
only when the digest matches; a text it cannot parse or a mismatch is logged and the command
|
||||
stays pending (section 2's `tuning_refused`). Applied at a tick after births, the command
|
||||
replaces the region's rules; it is recorded in trace 5 and bound once like any command.
|
||||
- **Replay and recovery.** Applying or replaying an adoption needs whoever does it to name
|
||||
the rules for that revision and digest (`CellularTuningResolver`): the host from its
|
||||
verified revisions, the publisher and recovery from the successor state, checked against
|
||||
the recorded digest. A batch carries at most one adoption, so its successor names exactly
|
||||
one set of rules. Nothing reads "the latest" revision.
|
||||
- **What cannot change live.** `reproduction.safety_version` is a law of the opening
|
||||
(ADR-0181): the admin API refuses a text that changes it, and a region would refuse the
|
||||
command. `playtest.*` only matters when a region opens. Heritable values already in genomes
|
||||
are not rewritten.
|
||||
- **Portal.** A Tuning page: the editor starts from the newest revision or the baseline file
|
||||
the regions opened with (`--baseline-tuning`, the mounted Git revamp file), Check reports
|
||||
the line, key and kind of a problem, Adopt stores the revision with a reason, the revision
|
||||
list shows how many regions adopted each, and each region shows which revision its live
|
||||
rules match (`rules_sha256` in the region view).
|
||||
|
||||
Live today: everything the revamp file holds, including the drip (`spent.drip_interval_ticks`,
|
||||
`spent.drip_bubble_pj`, `spent.drip_bubble_fg`). Section 4's new drip settings (bubbles per
|
||||
drip, per-channel scale) are new rules fields, so they need a rules format change; the
|
||||
resource schedules of section 12 cover the game source meanwhile.
|
||||
|
||||
Still pending from this ADR: section 4's new drip settings,
|
||||
data-defined mutations (section 5) and the account's current mutations; opening new regions
|
||||
or worlds from the portal (a later gate: Git-generated manifests through the Gitea API and an
|
||||
Argo sync for one more region in the world; several concurrent regions or worlds need
|
||||
|
||||
@@ -43,6 +43,13 @@
|
||||
// [&energy=PJ][&carbon=FG]...[&reason=R]
|
||||
// creates (no id) or replaces a schedule repeating
|
||||
// add_bubbles, release_pool or spawn_wildlife
|
||||
// GET /api/tuning the world's rules-13 tuning revisions, the newest text
|
||||
// and the baseline file regions opened with (schema 66)
|
||||
// GET /api/tuning/<revision> one revision's text
|
||||
// POST /api/tuning[?check=1][&reason=R] the request body is a tuning text (the Git
|
||||
// revamp file format): checked, then stored as a new
|
||||
// revision that every region of the world adopts at its
|
||||
// next tick (check=1: checked only, nothing stored)
|
||||
namespace evo::server::admin {
|
||||
struct AdminAsset {
|
||||
std::string path; // "/index.html", ...; "/" serves "/index.html".
|
||||
@@ -50,9 +57,12 @@ struct AdminAsset {
|
||||
};
|
||||
struct AdminConfig {
|
||||
std::uint64_t world{};
|
||||
std::vector<std::uint64_t> view_regions; // Regions whose live view can be fetched.
|
||||
std::string portal_config_json; // Served at /config.json.
|
||||
std::string content_security_policy; // Sent with every portal file.
|
||||
// The world's regions: their live view can be fetched and they adopt tuning revisions.
|
||||
std::vector<std::uint64_t> view_regions;
|
||||
// The revamp tuning file the world's regions opened with (empty: unknown).
|
||||
std::string baseline_tuning;
|
||||
std::string portal_config_json; // Served at /config.json.
|
||||
std::string content_security_policy; // Sent with every portal file.
|
||||
std::chrono::milliseconds db_timeout{3000};
|
||||
std::chrono::milliseconds view_cache{750}; // A fetched view answers repeats this long.
|
||||
};
|
||||
@@ -70,7 +80,8 @@ class AdminApi {
|
||||
|
||||
private:
|
||||
[[nodiscard]] OpsResponse api(std::string_view method, std::string_view path,
|
||||
std::string_view query, const broker::AccessTokenClaims& admin,
|
||||
std::string_view query, std::string_view body,
|
||||
const broker::AccessTokenClaims& admin,
|
||||
std::chrono::steady_clock::time_point now);
|
||||
[[nodiscard]] OpsResponse accounts(std::optional<std::string_view> account);
|
||||
[[nodiscard]] OpsResponse account_action(std::string_view account, std::string_view action,
|
||||
@@ -87,6 +98,10 @@ class AdminApi {
|
||||
const broker::AccessTokenClaims& admin);
|
||||
[[nodiscard]] OpsResponse set_window(std::uint64_t region, std::string_view query,
|
||||
const broker::AccessTokenClaims& admin);
|
||||
[[nodiscard]] OpsResponse tuning();
|
||||
[[nodiscard]] OpsResponse tuning_text(std::uint64_t revision);
|
||||
[[nodiscard]] OpsResponse set_tuning(std::string_view query, std::string_view text,
|
||||
const broker::AccessTokenClaims& admin);
|
||||
[[nodiscard]] std::optional<persistence::PgRows> query(const std::string& sql,
|
||||
std::vector<std::string> parameters);
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
@@ -155,6 +156,8 @@ class CellularRegionHost {
|
||||
// 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<CellularAdminCommand> unbound_admin_commands() const;
|
||||
// ADR-0182: the tuning revisions this host fetched and verified, for adopt_tuning commands.
|
||||
[[nodiscard]] CellularTuningResolver tuning_resolver() const;
|
||||
[[nodiscard]] std::vector<FinalCellularMutation> 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.
|
||||
@@ -290,6 +293,7 @@ class CellularRegionHost {
|
||||
std::vector<CellularBirthRefusal> pending_birth_refusals_;
|
||||
std::vector<persistence::CellularMutationReservation> queued_mutations_;
|
||||
std::vector<CellularAdminCommand> queued_admin_;
|
||||
std::map<std::uint64_t, CellularRevampRules> verified_tuning_; // By revision.
|
||||
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_{};
|
||||
|
||||
@@ -75,6 +75,10 @@ class CellularSessionBatchBuilder {
|
||||
[[nodiscard]] const CellularBatch& batch() const noexcept {
|
||||
return batch_;
|
||||
}
|
||||
// ADR-0182: names the rules of adopt_tuning commands (the host's verified revisions).
|
||||
void set_tuning_resolver(CellularTuningResolver tuning) {
|
||||
tuning_ = std::move(tuning);
|
||||
}
|
||||
// ADR-0182: each admin command appended so far and whether it applied, in trace order.
|
||||
[[nodiscard]] std::span<const CellularAdminResult> admin_results() const noexcept {
|
||||
return admin_results_;
|
||||
@@ -121,6 +125,7 @@ class CellularSessionBatchBuilder {
|
||||
std::vector<CellularMutationActionRecord> mutation_actions_;
|
||||
std::vector<CellularBirthRefusal> refused_births_;
|
||||
std::vector<CellularAdminResult> admin_results_;
|
||||
CellularTuningResolver tuning_;
|
||||
std::optional<CellularLivesError> last_lives_error_;
|
||||
};
|
||||
} // namespace evo::server
|
||||
|
||||
@@ -5,12 +5,13 @@
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
// ADR-0182 read-only region view for the admin portal: every cell, pool and bubble of a
|
||||
// cellular owner as compact JSON. It reads the committed in-memory state and changes nothing;
|
||||
// it is not a simulation output, a checkpoint or a client protocol.
|
||||
//
|
||||
// {"world":W,"region":R,"tick":T,"rules":N,"area":[w,h],
|
||||
// {"world":W,"region":R,"tick":T,"rules":N,["rules_sha256":"hex",]"area":[w,h],
|
||||
// "spent":[e,c,n,p,o],"cells_total":N,"deposits_total":N,
|
||||
// "cells":[["life","organism","account",x,y,extent,flags,members,
|
||||
// reserve_e,reserve_c,reserve_n,reserve_p,reserve_o,body_mass,"root"],...],
|
||||
@@ -22,7 +23,8 @@
|
||||
// request a lineage descends from, cellular_lineage_root). Flags: 1 wildlife, 2 the account's
|
||||
// session is connected, 4 lingering, 8 suspended. Deposit kind: 0 patch, 1 pool, 2 bubble. Energy
|
||||
// in pJ, matter in fg. Cells and deposits beyond the limits are left out (by ascending life id and
|
||||
// deposit id); the totals still count them.
|
||||
// deposit id); the totals still count them. `rules_sha256` is given by the caller (the SHA-256
|
||||
// of encode_cellular_revamp_rules, which tuning revisions are adopted by) and left out when empty.
|
||||
namespace evo::server {
|
||||
struct CellularAdminViewLimits {
|
||||
std::size_t cells{4096}, deposits{16384};
|
||||
@@ -31,7 +33,8 @@ struct CellularAdminViewLimits {
|
||||
using CellularAdminOnline = std::function<bool(const LifeAccount&)>;
|
||||
[[nodiscard]] std::string render_cellular_admin_view(const CellularLives& state,
|
||||
const CellularAdminOnline& online,
|
||||
CellularAdminViewLimits limits = {});
|
||||
CellularAdminViewLimits limits = {},
|
||||
std::string_view rules_sha256 = {});
|
||||
// Lowercase 8-4-4-4-12 text of a 16-byte account.
|
||||
[[nodiscard]] std::string cellular_account_text(const LifeAccount& account);
|
||||
} // namespace evo::server
|
||||
|
||||
@@ -126,11 +126,19 @@ struct CellularMutationOutcome {
|
||||
[[nodiscard]] std::expected<std::unique_ptr<CellularLives>, CellularBatchError>
|
||||
replay_cellular_batch(const CellularLives& base, const CellularBatch& batch,
|
||||
std::vector<CellularMutationOutcome>& mutation_outcomes);
|
||||
// ADR-0182: also reports every admin command's result in trace order.
|
||||
// ADR-0182: also reports every admin command's result in trace order; `tuning` names the
|
||||
// rules an adopt_tuning command adopts (a batch holds at most one).
|
||||
[[nodiscard]] std::expected<std::unique_ptr<CellularLives>, CellularBatchError>
|
||||
replay_cellular_batch(const CellularLives& base, const CellularBatch& batch,
|
||||
std::vector<CellularMutationOutcome>& mutation_outcomes,
|
||||
std::vector<CellularAdminResult>& admin_results);
|
||||
std::vector<CellularAdminResult>& admin_results,
|
||||
const CellularTuningResolver& tuning = {});
|
||||
// ADR-0182: the rules a batch's single adopt_tuning names are its successor's (nothing else
|
||||
// changes them), so publication and recovery resolve from the successor once they have
|
||||
// checked the digest: this returns the successor's rules for `check(encoding, digest)`.
|
||||
[[nodiscard]] CellularTuningResolver cellular_successor_tuning(
|
||||
const CellularLives& successor,
|
||||
std::function<bool(std::span<const std::uint8_t>, const std::array<std::uint8_t, 32>&)> check);
|
||||
// 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;
|
||||
|
||||
@@ -7,10 +7,12 @@
|
||||
#include "evo/server/lifecycle/cellular_revamp.hpp"
|
||||
#include "evo/server/lifecycle/primitive_lives.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <compare>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
@@ -190,6 +192,10 @@ enum class CellularAdminKind : std::uint8_t { // A u16 in the trace.
|
||||
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.
|
||||
// ADR-0182 tuning amendment: the region's rules-13 rules become tuning revision `target`,
|
||||
// whose canonical rules encoding (encode_cellular_revamp_rules) has the SHA-256 held in the
|
||||
// energy, carbon, nitrogen and phosphorus amounts (little-endian words; `other` zero).
|
||||
adopt_tuning = 7,
|
||||
};
|
||||
struct CellularAdminCommand {
|
||||
std::uint64_t id{}; // The durable queue id.
|
||||
@@ -201,6 +207,18 @@ struct CellularAdminCommand {
|
||||
};
|
||||
// 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;
|
||||
// The 32-byte digest an adopt_tuning command carries, and a command carrying one.
|
||||
[[nodiscard]] std::array<std::uint8_t, 32>
|
||||
cellular_admin_digest(const CellularAdminCommand& command) noexcept;
|
||||
[[nodiscard]] CellularAdminCommand
|
||||
cellular_adopt_tuning_command(std::uint64_t id, std::uint64_t revision,
|
||||
const std::array<std::uint8_t, 32>& digest) noexcept;
|
||||
// The rules an adopt_tuning command names, from whoever can check its digest (the region host
|
||||
// from the revision text, publication and recovery from the successor state). Nullopt: unknown.
|
||||
using CellularTuningResolver = std::function<std::optional<CellularRevampRules>(
|
||||
std::uint64_t revision, const std::array<std::uint8_t, 32>& digest)>;
|
||||
// Canonical bytes of the rules-13 rules (as the owner snapshot stores them, without the pool).
|
||||
[[nodiscard]] std::vector<std::uint8_t> encode_cellular_revamp_rules(const CellularRevampRules& r);
|
||||
struct CellularAdminResult {
|
||||
std::uint64_t id{};
|
||||
bool applied{};
|
||||
@@ -443,7 +461,8 @@ class CellularLives {
|
||||
// current tick, after its births. Rules 13 only. `results` reports each command, in order.
|
||||
[[nodiscard]] std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||
admin_candidate(std::span<const CellularAdminCommand> commands,
|
||||
std::vector<CellularAdminResult>* results = nullptr) const;
|
||||
std::vector<CellularAdminResult>* results = nullptr,
|
||||
const CellularTuningResolver& tuning = {}) const;
|
||||
|
||||
private:
|
||||
friend class CellularMigration;
|
||||
|
||||
@@ -23,10 +23,13 @@ struct OpsResponse {
|
||||
};
|
||||
// Receives the request path (no query handling: probes use exact paths).
|
||||
using OpsHandler = std::function<OpsResponse(std::string_view path)>;
|
||||
// ADR-0131 API mode (the grant broker): GET or POST without a body, the path and the
|
||||
// Authorization header value (empty when absent). Views are valid during the call only.
|
||||
// ADR-0131 API mode (the grant broker): GET or POST, the path and the Authorization header
|
||||
// value (empty when absent). A body (Content-Length, no chunked encoding) is accepted only
|
||||
// when the server is asked for one (ADR-0182 admin tuning); otherwise it is a 400. Views are
|
||||
// valid during the call only.
|
||||
struct ApiRequest {
|
||||
std::string_view method, path, authorization;
|
||||
std::string_view body{};
|
||||
};
|
||||
using ApiHandler = std::function<OpsResponse(const ApiRequest&)>;
|
||||
enum class OpsError : std::uint8_t { address, socket };
|
||||
@@ -39,7 +42,8 @@ class OpsServer {
|
||||
static constexpr std::size_t max_accepts_per_serve = 4;
|
||||
static constexpr std::size_t max_request_bytes = 4096;
|
||||
static constexpr std::size_t max_api_request_bytes = 8192; // Room for an access token.
|
||||
static constexpr std::size_t max_response_bytes = 16384; // The default bound.
|
||||
static constexpr std::size_t max_api_body_bytes = std::size_t{64} << 10U; // Beyond those.
|
||||
static constexpr std::size_t max_response_bytes = 16384; // The default bound.
|
||||
// ADR-0182: the admin view and portal answer up to this many bytes when asked to.
|
||||
static constexpr std::size_t max_configurable_response_bytes = std::size_t{4} << 20U;
|
||||
static constexpr std::chrono::milliseconds connection_deadline{1000};
|
||||
@@ -61,8 +65,9 @@ class OpsServer {
|
||||
// expires connections without waiting. Connections beyond the bound are closed
|
||||
// at once; slow or oversized ones are answered or dropped without delaying others.
|
||||
void serve(const OpsHandler& handler);
|
||||
// The same bounded loop for API requests (max_api_request_bytes).
|
||||
void serve_api(const ApiHandler& handler);
|
||||
// The same bounded loop for API requests (max_api_request_bytes), with request bodies of
|
||||
// at most `body_limit` (at most max_api_body_bytes) bytes; larger ones answer 413.
|
||||
void serve_api(const ApiHandler& handler, std::size_t body_limit = 0);
|
||||
[[nodiscard]] std::uint64_t refused() const noexcept {
|
||||
return refused_;
|
||||
}
|
||||
@@ -80,7 +85,7 @@ class OpsServer {
|
||||
OpsServer(int fd, std::uint16_t port, std::size_t response_limit)
|
||||
: fd_(fd), port_(port), response_limit_(response_limit) {}
|
||||
void respond(Connection& c, const OpsHandler& handler);
|
||||
void respond_api(Connection& c, const ApiHandler& handler);
|
||||
void respond_api(Connection& c, const ApiHandler& handler, std::size_t body_limit);
|
||||
template <typename Answer> void serve_with(std::size_t request_limit, const Answer& answer);
|
||||
int fd_;
|
||||
std::uint16_t port_;
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
-- Tuning revisions (ADR-0182 sections 1-2, 2026-10-27). The rules-13 tuning of a world is a
|
||||
-- sequence of append-only revisions: the canonical tuning text and the SHA-256 of the rules it
|
||||
-- produces (encode_cellular_revamp_rules). Storing a revision queues an adopt_tuning admin
|
||||
-- command (kind 7) for each named region; the region fetches the text, rebuilds the rules,
|
||||
-- checks the digest and adopts them at a tick recorded in its trace, so replay and recovery
|
||||
-- reproduce the change exactly. Commands of kind 7 come only from this function.
|
||||
|
||||
ALTER TABLE evolution.admin_commands DROP CONSTRAINT admin_commands_kind_check;
|
||||
ALTER TABLE evolution.admin_commands ADD CONSTRAINT admin_commands_kind_check
|
||||
CHECK (kind BETWEEN 1 AND 7);
|
||||
|
||||
CREATE TABLE evolution.tuning_revisions (
|
||||
revision bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
world_id evolution.u64 NOT NULL,
|
||||
text text NOT NULL CHECK (octet_length(text) BETWEEN 1 AND 262144),
|
||||
rules_sha256 bytea NOT NULL CHECK (octet_length(rules_sha256) = 32),
|
||||
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()
|
||||
);
|
||||
CREATE TRIGGER tuning_revisions_immutable BEFORE UPDATE OR DELETE ON evolution.tuning_revisions
|
||||
FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite();
|
||||
|
||||
-- Admin: stores a revision (validated by the admin API, which also computed the rules digest;
|
||||
-- the text is kept as written, comments included) and queues its adoption in each of
|
||||
-- `p_regions`.
|
||||
CREATE FUNCTION evolution.admin_set_tuning_v1(p_world evolution.u64, p_text text,
|
||||
p_rules_sha256 bytea, p_regions bigint[], p_subject text, p_reason text)
|
||||
RETURNS bigint LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
||||
DECLARE
|
||||
made bigint;
|
||||
region bigint;
|
||||
BEGIN
|
||||
IF p_world IS NULL OR p_world = 0 OR p_regions IS NULL OR cardinality(p_regions) = 0
|
||||
OR cardinality(p_regions) > 64 OR p_rules_sha256 IS NULL
|
||||
OR octet_length(p_rules_sha256) <> 32 THEN
|
||||
RAISE EXCEPTION 'invalid tuning revision' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
BEGIN
|
||||
INSERT INTO evolution.tuning_revisions(world_id, text, rules_sha256, author_subject, reason)
|
||||
VALUES (p_world, p_text, p_rules_sha256, p_subject, coalesce(p_reason, ''))
|
||||
RETURNING revision INTO made;
|
||||
EXCEPTION WHEN check_violation OR not_null_violation THEN
|
||||
RAISE EXCEPTION 'invalid tuning revision' USING ERRCODE = '22023';
|
||||
END;
|
||||
FOREACH region IN ARRAY p_regions LOOP
|
||||
IF NOT EXISTS (SELECT 1 FROM evolution.region_leases l
|
||||
WHERE l.world_id = p_world AND l.region_id = region) THEN
|
||||
RAISE EXCEPTION 'unknown region' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
INSERT INTO evolution.admin_commands(world_id, region_id, kind, cell_count, target, energy,
|
||||
carbon, nitrogen, phosphorus, other, author_subject, reason)
|
||||
VALUES (p_world, region, 7, 0, made, evolution.manifest_uint(p_rules_sha256, 0, 8),
|
||||
evolution.manifest_uint(p_rules_sha256, 8, 8),
|
||||
evolution.manifest_uint(p_rules_sha256, 16, 8),
|
||||
evolution.manifest_uint(p_rules_sha256, 24, 8), 0, p_subject, coalesce(p_reason, ''));
|
||||
END LOOP;
|
||||
RETURN made;
|
||||
END $$;
|
||||
REVOKE ALL ON FUNCTION evolution.admin_set_tuning_v1(evolution.u64, text, bytea, bigint[], text,
|
||||
text) FROM PUBLIC;
|
||||
|
||||
-- The region: a revision's text and rules digest.
|
||||
CREATE FUNCTION evolution.load_tuning_revision_v1(p_world evolution.u64, p_revision bigint)
|
||||
RETURNS TABLE (text text, rules_sha256 bytea)
|
||||
LANGUAGE sql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
||||
SELECT t.text, t.rules_sha256 FROM evolution.tuning_revisions t
|
||||
WHERE t.world_id = p_world AND t.revision = p_revision
|
||||
$$;
|
||||
REVOKE ALL ON FUNCTION evolution.load_tuning_revision_v1(evolution.u64, bigint) FROM PUBLIC;
|
||||
GRANT EXECUTE ON FUNCTION evolution.load_tuning_revision_v1(evolution.u64, bigint)
|
||||
TO :"runtime_role";
|
||||
|
||||
-- Admin: the world's revisions, newest first, with how many regions have adopted each.
|
||||
CREATE FUNCTION evolution.admin_tuning_revisions_v1(p_world evolution.u64)
|
||||
RETURNS TABLE (revision bigint, rules_sha256 text, author_subject text, reason text,
|
||||
created_at timestamptz, regions bigint, adopted bigint, last_tick evolution.u64)
|
||||
LANGUAGE sql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
||||
SELECT t.revision, encode(t.rules_sha256, 'hex'), t.author_subject, t.reason, t.created_at,
|
||||
(SELECT count(*) FROM evolution.admin_commands q
|
||||
WHERE q.kind = 7 AND q.world_id = p_world AND q.target = t.revision),
|
||||
(SELECT count(*) FROM evolution.admin_commands q
|
||||
JOIN evolution.admin_command_applications a ON a.command_id = q.command_id
|
||||
WHERE q.kind = 7 AND q.world_id = p_world AND q.target = t.revision),
|
||||
(SELECT max(a.tick) FROM evolution.admin_commands q
|
||||
JOIN evolution.admin_command_applications a ON a.command_id = q.command_id
|
||||
WHERE q.kind = 7 AND q.world_id = p_world AND q.target = t.revision)
|
||||
FROM evolution.tuning_revisions t
|
||||
WHERE t.world_id = p_world
|
||||
ORDER BY t.revision DESC
|
||||
LIMIT 100
|
||||
$$;
|
||||
REVOKE ALL ON FUNCTION evolution.admin_tuning_revisions_v1(evolution.u64) FROM PUBLIC;
|
||||
|
||||
-- Admin: a revision's text (0: the newest).
|
||||
CREATE FUNCTION evolution.admin_tuning_text_v1(p_world evolution.u64, p_revision bigint)
|
||||
RETURNS TABLE (revision bigint, text text)
|
||||
LANGUAGE sql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
||||
SELECT t.revision, t.text FROM evolution.tuning_revisions t
|
||||
WHERE t.world_id = p_world AND (p_revision = 0 OR t.revision = p_revision)
|
||||
ORDER BY t.revision DESC
|
||||
LIMIT 1
|
||||
$$;
|
||||
REVOKE ALL ON FUNCTION evolution.admin_tuning_text_v1(evolution.u64, bigint) FROM PUBLIC;
|
||||
|
||||
INSERT INTO evolution.schema_migrations(version,sha256) VALUES(66,:'migration_sha_66');
|
||||
+21
-4
@@ -141,6 +141,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
|
||||
SELECT :'history_v62' || ',63:' || :'migration_sha_63' AS history_v63 \gset
|
||||
SELECT :'history_v63' || ',64:' || :'migration_sha_64' AS history_v64 \gset
|
||||
SELECT :'history_v64' || ',65:' || :'migration_sha_65' AS history_v65 \gset
|
||||
SELECT :'history_v65' || ',66:' || :'migration_sha_66' AS history_v66 \gset
|
||||
SELECT :'history' = :'history_v22' AS at_v22,
|
||||
:'history' = :'history_v23' AS at_v23,
|
||||
:'history' = :'history_v24' AS at_v24,
|
||||
@@ -184,8 +185,10 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
|
||||
:'history' = :'history_v62' AS at_v62,
|
||||
:'history' = :'history_v63' AS at_v63,
|
||||
:'history' = :'history_v64' AS at_v64,
|
||||
:'history' = :'history_v65' AS at_v65 \gset
|
||||
\if :at_v65
|
||||
:'history' = :'history_v65' AS at_v65,
|
||||
:'history' = :'history_v66' AS at_v66 \gset
|
||||
\if :at_v66
|
||||
\elif :at_v65
|
||||
\elif :at_v64
|
||||
\elif :at_v63
|
||||
\elif :at_v62
|
||||
@@ -609,7 +612,8 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
|
||||
coalesce(max(version), 0) = 62 AS at_v62,
|
||||
coalesce(max(version), 0) = 63 AS at_v63,
|
||||
coalesce(max(version), 0) = 64 AS at_v64,
|
||||
coalesce(max(version), 0) = 65 AS at_v65
|
||||
coalesce(max(version), 0) = 65 AS at_v65,
|
||||
coalesce(max(version), 0) = 66 AS at_v66
|
||||
FROM evolution.schema_migrations \gset
|
||||
\if :at_v24
|
||||
\ir 0025_cellular_v2_bundle.sql
|
||||
@@ -843,6 +847,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
|
||||
\elif :at_v63
|
||||
\elif :at_v64
|
||||
\elif :at_v65
|
||||
\elif :at_v66
|
||||
\else
|
||||
\echo 'Unknown or changed migration history after v43'
|
||||
DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$;
|
||||
@@ -935,6 +940,10 @@ SELECT coalesce(max(version), 0) = 64 AS needs_v65 FROM evolution.schema_migrati
|
||||
\if :needs_v65
|
||||
\ir 0065_admin_schedules.sql
|
||||
\endif
|
||||
SELECT coalesce(max(version), 0) = 65 AS needs_v66 FROM evolution.schema_migrations \gset
|
||||
\if :needs_v66
|
||||
\ir 0066_tuning_revisions.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
|
||||
@@ -1010,6 +1019,7 @@ BEGIN
|
||||
('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.load_tuning_revision_v1(evolution.u64,bigint)'::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),
|
||||
@@ -1093,6 +1103,10 @@ GRANT EXECUTE ON FUNCTION evolution.admin_set_schedule_v1(evolution.u64, evoluti
|
||||
integer, boolean, text, text) TO :"admin_role";
|
||||
GRANT EXECUTE ON FUNCTION evolution.admin_schedules_v1(evolution.u64, evolution.u64)
|
||||
TO :"admin_role";
|
||||
GRANT EXECUTE ON FUNCTION evolution.admin_set_tuning_v1(evolution.u64, text, bytea, bigint[], text,
|
||||
text) TO :"admin_role";
|
||||
GRANT EXECUTE ON FUNCTION evolution.admin_tuning_revisions_v1(evolution.u64) TO :"admin_role";
|
||||
GRANT EXECUTE ON FUNCTION evolution.admin_tuning_text_v1(evolution.u64, bigint) TO :"admin_role";
|
||||
SELECT pg_catalog.set_config('evolution.admin_role', :'admin_role', true);
|
||||
DO $$
|
||||
DECLARE
|
||||
@@ -1123,7 +1137,10 @@ BEGIN
|
||||
('evolution.admin_account_status_v1(evolution.u64)'::regprocedure::oid),
|
||||
('evolution.admin_account_bans_v1(uuid)'::regprocedure::oid),
|
||||
('evolution.admin_set_schedule_v1(evolution.u64,evolution.u64,bigint,integer,integer,evolution.u64,evolution.u64,evolution.u64,evolution.u64,evolution.u64,integer,boolean,text,text)'::regprocedure::oid),
|
||||
('evolution.admin_schedules_v1(evolution.u64,evolution.u64)'::regprocedure::oid)
|
||||
('evolution.admin_schedules_v1(evolution.u64,evolution.u64)'::regprocedure::oid),
|
||||
('evolution.admin_set_tuning_v1(evolution.u64,text,bytea,bigint[],text,text)'::regprocedure::oid),
|
||||
('evolution.admin_tuning_revisions_v1(evolution.u64)'::regprocedure::oid),
|
||||
('evolution.admin_tuning_text_v1(evolution.u64,bigint)'::regprocedure::oid)
|
||||
), violations(reason) AS (
|
||||
SELECT 'admin role is the runtime role' WHERE admin = runtime
|
||||
UNION ALL
|
||||
|
||||
@@ -15,6 +15,16 @@
|
||||
|
||||
namespace evo::persistence {
|
||||
namespace {
|
||||
// ADR-0182: a batch's tuning adoption resolves to its successor's rules after their SHA-256
|
||||
// matches the command's digest.
|
||||
server::CellularTuningResolver successor_tuning(const server::CellularLives& successor) {
|
||||
return server::cellular_successor_tuning(
|
||||
successor,
|
||||
[](std::span<const std::uint8_t> bytes, const std::array<std::uint8_t, 32>& digest) {
|
||||
const auto computed = sha256(bytes);
|
||||
return computed && std::ranges::equal(*computed, digest);
|
||||
});
|
||||
}
|
||||
using std::chrono::milliseconds;
|
||||
|
||||
std::uint64_t input_tick(const server::CellularActionRecord& action) {
|
||||
@@ -601,7 +611,9 @@ publish_cellular_batch(Postgres& db, const FilesystemObjectStore& objects, const
|
||||
if (!base)
|
||||
return std::unexpected{failure(PublicationError::predecessor)};
|
||||
std::vector<server::CellularMutationOutcome> outcomes;
|
||||
auto replay = server::replay_cellular_batch(**base, committed_batch, outcomes);
|
||||
std::vector<server::CellularAdminResult> admin_results;
|
||||
auto replay = server::replay_cellular_batch(**base, committed_batch, outcomes, admin_results,
|
||||
successor_tuning(candidate));
|
||||
const auto payload = candidate.snapshot();
|
||||
if (!replay || (*replay)->snapshot() != payload ||
|
||||
!server::match_cellular_actions_to_trace(**base, committed_batch, actions) ||
|
||||
@@ -886,7 +898,19 @@ recover_cellular_loaded(Postgres& db, const FilesystemObjectStore& objects, Regi
|
||||
!server::match_cellular_actions_to_trace(**base, *trace, *batch_actions))
|
||||
return fail_with(RecoveryError::history);
|
||||
std::vector<server::CellularMutationOutcome> outcomes;
|
||||
auto replay = server::replay_cellular_batch(**base, *trace, outcomes);
|
||||
std::vector<server::CellularAdminResult> admin_results;
|
||||
// ADR-0182: an adoption resolves from the stored successor (trace 5 only).
|
||||
server::CellularTuningResolver tuning;
|
||||
std::unique_ptr<server::CellularLives> successor;
|
||||
if (trace->format_version == server::cellular_batch_admin_format) {
|
||||
auto restored = restore_cellular_payload(*payload, *manifest);
|
||||
if (!restored)
|
||||
return fail_with(RecoveryError::tail);
|
||||
successor = std::move(*restored);
|
||||
tuning = successor_tuning(*successor);
|
||||
}
|
||||
auto replay =
|
||||
server::replay_cellular_batch(**base, *trace, outcomes, admin_results, tuning);
|
||||
if (!replay || (*replay)->snapshot() != *payload)
|
||||
return fail_with(RecoveryError::tail);
|
||||
|
||||
|
||||
+204
-19
@@ -1,11 +1,18 @@
|
||||
#include "evo/server/admin/admin_api.hpp"
|
||||
|
||||
#include "evo/biology/cellular_revamp_tuning.hpp"
|
||||
#include "evo/persistence/sha256.hpp"
|
||||
#include "evo/server/lifecycle/cellular_lives.hpp"
|
||||
#include "evo/server/lifecycle/cellular_revamp.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cerrno>
|
||||
#include <charconv>
|
||||
#include <expected>
|
||||
#include <netdb.h>
|
||||
#include <poll.h>
|
||||
#include <span>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -19,6 +26,11 @@ OpsResponse json(int status, std::string body) {
|
||||
OpsResponse error(int status, std::string_view code) {
|
||||
return json(status, "{\"error\":\"" + std::string(code) + "\"}\n");
|
||||
}
|
||||
// The admin as recorded with what they did: email and subject, or the subject alone.
|
||||
std::string author_of(const broker::AccessTokenClaims& admin) {
|
||||
auto author = admin.email.empty() ? admin.subject : admin.email + " (" + admin.subject + ")";
|
||||
return author.size() > 255 ? admin.subject : author;
|
||||
}
|
||||
const char* token_error(TokenError e) {
|
||||
switch (e) {
|
||||
case TokenError::expired:
|
||||
@@ -272,15 +284,19 @@ 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 session windows, commands and account actions accept POST.
|
||||
// Only session windows, commands, schedules, tuning and account actions accept POST, and
|
||||
// only tuning takes a request body.
|
||||
if (request.method != "GET" &&
|
||||
!(request.method == "POST" &&
|
||||
((path.starts_with("/api/regions/") &&
|
||||
(path == "/api/tuning" ||
|
||||
(path.starts_with("/api/regions/") &&
|
||||
(path.ends_with("/session-window") || path.ends_with("/commands") ||
|
||||
path.ends_with("/schedules"))) ||
|
||||
(path.starts_with("/api/accounts/") &&
|
||||
(path.ends_with("/ban") || path.ends_with("/logout"))))))
|
||||
return {405, "method not allowed\n"};
|
||||
if (!request.body.empty() && path != "/api/tuning")
|
||||
return {400, "bad request\n"};
|
||||
if (path == "/healthz")
|
||||
return {200, "ok\n"};
|
||||
if (path == "/config.json")
|
||||
@@ -308,11 +324,11 @@ OpsResponse AdminApi::handle(const ApiRequest& request, std::uint64_t now_second
|
||||
if (!claims)
|
||||
return error(claims.error() == TokenError::missing_role ? 403 : 401,
|
||||
token_error(claims.error()));
|
||||
return api(request.method, path, query, *claims, now);
|
||||
return api(request.method, path, query, request.body, *claims, now);
|
||||
}
|
||||
|
||||
OpsResponse AdminApi::api(std::string_view method, std::string_view path, std::string_view query,
|
||||
const broker::AccessTokenClaims& admin,
|
||||
std::string_view body, const broker::AccessTokenClaims& admin,
|
||||
std::chrono::steady_clock::time_point now) {
|
||||
if (path == "/api/me")
|
||||
return json(200, "{\"subject\":" + json_string(admin.subject) +
|
||||
@@ -334,6 +350,15 @@ OpsResponse AdminApi::api(std::string_view method, std::string_view path, std::s
|
||||
}
|
||||
if (path == "/api/regions")
|
||||
return regions();
|
||||
if (path == "/api/tuning")
|
||||
return method == "POST" ? set_tuning(query, body, admin) : tuning();
|
||||
constexpr std::string_view tuning_prefix = "/api/tuning/";
|
||||
if (path.starts_with(tuning_prefix)) {
|
||||
const auto revision = number(path.substr(tuning_prefix.size()));
|
||||
if (!revision || *revision == 0)
|
||||
return error(404, "not_found");
|
||||
return method == "GET" ? tuning_text(*revision) : error(405, "method_not_allowed");
|
||||
}
|
||||
constexpr std::string_view region_prefix = "/api/regions/";
|
||||
constexpr std::string_view view_suffix = "/view";
|
||||
if (path.starts_with(region_prefix) && path.ends_with(view_suffix)) {
|
||||
@@ -453,9 +478,7 @@ OpsResponse AdminApi::account_action(std::string_view account, std::string_view
|
||||
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;
|
||||
const auto author = author_of(admin);
|
||||
const auto rows = query("SELECT evolution.admin_set_account_ban_v1($1, $2::uuid, $3::boolean, "
|
||||
"$4, $5)",
|
||||
{world, std::string(account), *banned == "1" ? "true" : "false", author,
|
||||
@@ -533,9 +556,7 @@ OpsResponse AdminApi::set_window(std::uint64_t region, std::string_view query_te
|
||||
if (!reason && query_text.find("reason=") != std::string_view::npos)
|
||||
return error(400, "bad_reason");
|
||||
// Who: the email for people reading the history, and the subject that identifies them.
|
||||
auto author = admin.email.empty() ? admin.subject : admin.email + " (" + admin.subject + ")";
|
||||
if (author.size() > 255)
|
||||
author = admin.subject;
|
||||
const auto author = author_of(admin);
|
||||
const auto rows = query("SELECT activation_id, floor(extract(epoch FROM expires_at))::bigint "
|
||||
"FROM evolution.admin_set_session_window_v1($1, $2, $3, $4, $5)",
|
||||
{std::to_string(config_.world), std::to_string(region),
|
||||
@@ -547,9 +568,56 @@ OpsResponse AdminApi::set_window(std::uint64_t region, std::string_view query_te
|
||||
}
|
||||
|
||||
namespace {
|
||||
constexpr std::array<std::string_view, 7> command_kinds{
|
||||
"", "remove_cell", "grant_reserve", "add_bubbles",
|
||||
"add_pool", "release_pool", "spawn_wildlife"};
|
||||
// Kinds 1-6 are queued by name; adopt_tuning (7) only through a tuning revision.
|
||||
constexpr std::array<std::string_view, 8> command_kinds{
|
||||
"", "remove_cell", "grant_reserve", "add_bubbles",
|
||||
"add_pool", "release_pool", "spawn_wildlife", "adopt_tuning"};
|
||||
constexpr std::size_t queued_kinds = 7;
|
||||
|
||||
std::string hex(std::span<const std::uint8_t> bytes) {
|
||||
constexpr std::string_view digits = "0123456789abcdef";
|
||||
std::string out;
|
||||
for (const auto byte : bytes) {
|
||||
out.push_back(digits[byte >> 4U]);
|
||||
out.push_back(digits[byte & 0x0FU]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
constexpr std::array<std::string_view, 9> tuning_error_kinds{
|
||||
"too_large", "syntax", "unknown_key", "duplicate_key", "invalid_value",
|
||||
"out_of_range", "missing_key", "inconsistent", "unsupported_version"};
|
||||
// A tuning text's rules and their digest (SHA-256 of encode_cellular_revamp_rules, what the
|
||||
// adopt_tuning command carries), or why it cannot be adopted.
|
||||
struct CheckedTuning {
|
||||
CellularRevampRules rules;
|
||||
persistence::Sha256Digest digest{};
|
||||
};
|
||||
std::expected<CheckedTuning, OpsResponse> check_tuning(std::string_view text) {
|
||||
// Tuning files are ASCII text: no control bytes but tabs and line ends.
|
||||
if (text.empty() || text.size() > OpsServer::max_api_body_bytes ||
|
||||
!std::ranges::all_of(text, [](char ch) {
|
||||
return ch == '\t' || ch == '\n' || ch == '\r' || (ch >= ' ' && ch <= '~');
|
||||
}))
|
||||
return std::unexpected{error(400, "invalid_text")};
|
||||
const auto parsed = biology::parse_cellular_revamp_tuning(text);
|
||||
if (!parsed) {
|
||||
const auto& failure = parsed.error();
|
||||
const auto kind = static_cast<std::size_t>(failure.kind);
|
||||
return std::unexpected{
|
||||
json(400, "{\"error\":\"invalid_tuning\",\"kind\":\"" +
|
||||
std::string(kind < tuning_error_kinds.size() ? tuning_error_kinds.at(kind)
|
||||
: "invalid") +
|
||||
"\",\"line\":" + std::to_string(failure.line) +
|
||||
",\"key\":" + json_string(failure.key) + "}\n")};
|
||||
}
|
||||
const auto rules = cellular_revamp_rules(*parsed);
|
||||
if (!cellular_revamp_valid(rules))
|
||||
return std::unexpected{error(400, "invalid_rules")};
|
||||
const auto digest = persistence::sha256(encode_cellular_revamp_rules(rules));
|
||||
if (!digest)
|
||||
return std::unexpected{error(503, "unavailable")};
|
||||
return CheckedTuning{rules, *digest};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
OpsResponse AdminApi::commands(std::uint64_t region) {
|
||||
@@ -584,6 +652,8 @@ OpsResponse AdminApi::enqueue(std::uint64_t region, std::string_view query_text,
|
||||
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");
|
||||
if (static_cast<std::size_t>(kind - command_kinds.begin()) >= queued_kinds)
|
||||
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<std::uint64_t> {
|
||||
const auto text = query_value(query_text, name, 20);
|
||||
@@ -601,9 +671,7 @@ OpsResponse AdminApi::enqueue(std::uint64_t region, std::string_view query_text,
|
||||
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;
|
||||
const auto author = author_of(admin);
|
||||
std::vector<std::string> parameters{std::to_string(config_.world), std::to_string(region),
|
||||
std::to_string(kind - command_kinds.begin())};
|
||||
for (const auto& x : v)
|
||||
@@ -668,6 +736,8 @@ OpsResponse AdminApi::set_schedule(std::uint64_t region, std::string_view query_
|
||||
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");
|
||||
if (static_cast<std::size_t>(kind - command_kinds.begin()) >= queued_kinds)
|
||||
return error(400, "bad_kind");
|
||||
const auto value = [&](std::string_view name,
|
||||
std::uint64_t absent) -> std::optional<std::uint64_t> {
|
||||
const auto text = query_value(query_text, name, 20);
|
||||
@@ -687,9 +757,7 @@ OpsResponse AdminApi::set_schedule(std::uint64_t region, std::string_view query_
|
||||
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;
|
||||
const auto author = author_of(admin);
|
||||
std::vector<std::string> parameters{std::to_string(config_.world), std::to_string(region)};
|
||||
parameters.push_back(std::to_string(v[0].value_or(0)));
|
||||
parameters.push_back(std::to_string(kind - command_kinds.begin()));
|
||||
@@ -718,6 +786,123 @@ OpsResponse AdminApi::set_schedule(std::uint64_t region, std::string_view query_
|
||||
return json(200, "{\"id\":" + text_value(at(rows->front(), 0)) + "}\n");
|
||||
}
|
||||
|
||||
OpsResponse AdminApi::tuning() {
|
||||
const auto world = std::to_string(config_.world);
|
||||
const auto revisions =
|
||||
query("SELECT revision, rules_sha256, author_subject, reason, "
|
||||
"floor(extract(epoch FROM created_at))::bigint, regions, adopted, last_tick "
|
||||
"FROM evolution.admin_tuning_revisions_v1($1)",
|
||||
{world});
|
||||
if (!revisions)
|
||||
return error(503, "unavailable");
|
||||
const auto newest =
|
||||
query("SELECT revision, text FROM evolution.admin_tuning_text_v1($1, 0)", {world});
|
||||
if (!newest)
|
||||
return error(503, "unavailable");
|
||||
std::string out = "{\"world\":" + world + ",\"regions\":[";
|
||||
for (std::size_t i = 0; i < config_.view_regions.size(); ++i)
|
||||
out += (i == 0 ? "\"" : ",\"") + std::to_string(config_.view_regions[i]) + "\"";
|
||||
out += "],\"baseline\":";
|
||||
if (config_.baseline_tuning.empty()) {
|
||||
out += "null";
|
||||
} else {
|
||||
const auto checked = check_tuning(config_.baseline_tuning);
|
||||
out += "{\"text\":" + json_string(config_.baseline_tuning) + ",\"rules_sha256\":" +
|
||||
(checked ? "\"" + hex(checked->digest) + "\"" : std::string{"null"}) + "}";
|
||||
}
|
||||
out += ",\"newest\":";
|
||||
if (newest->empty())
|
||||
out += "null";
|
||||
else
|
||||
out += "{\"revision\":" + text_value(at(newest->front(), 0)) +
|
||||
",\"text\":" + text_value(at(newest->front(), 1)) + "}";
|
||||
out += ",\"revisions\":[";
|
||||
for (std::size_t i = 0; i < revisions->size(); ++i) {
|
||||
const auto& row = (*revisions)[i];
|
||||
out += std::string(i == 0 ? "" : ",") + "{\"revision\":" + text_value(at(row, 0)) +
|
||||
",\"rules_sha256\":" + text_value(at(row, 1)) +
|
||||
",\"author\":" + text_value(at(row, 2)) + ",\"reason\":" + text_value(at(row, 3)) +
|
||||
",\"created_at\":" + seconds_value(at(row, 4)) +
|
||||
",\"regions\":" + count_value(at(row, 5)) +
|
||||
",\"adopted\":" + count_value(at(row, 6)) +
|
||||
",\"last_tick\":" + text_value(at(row, 7)) + "}";
|
||||
}
|
||||
return json(200, out + "]}\n");
|
||||
}
|
||||
|
||||
OpsResponse AdminApi::tuning_text(std::uint64_t revision) {
|
||||
const auto rows = query("SELECT revision, text FROM evolution.admin_tuning_text_v1($1, $2)",
|
||||
{std::to_string(config_.world), std::to_string(revision)});
|
||||
if (!rows)
|
||||
return error(503, "unavailable");
|
||||
if (rows->empty())
|
||||
return error(404, "not_found");
|
||||
return json(200, "{\"revision\":" + text_value(at(rows->front(), 0)) +
|
||||
",\"text\":" + text_value(at(rows->front(), 1)) + "}\n");
|
||||
}
|
||||
|
||||
OpsResponse AdminApi::set_tuning(std::string_view query_text, std::string_view text,
|
||||
const broker::AccessTokenClaims& admin) {
|
||||
const auto checked = check_tuning(text);
|
||||
if (!checked)
|
||||
return checked.error();
|
||||
// The safety revision is a law of the opening (ADR-0181): a region keeps its rules rather
|
||||
// than change it, so a text that changes it is refused here. The reference is the newest
|
||||
// revision, or the baseline file before the first.
|
||||
const auto newest = query("SELECT text FROM evolution.admin_tuning_text_v1($1, 0)",
|
||||
{std::to_string(config_.world)});
|
||||
if (!newest)
|
||||
return error(503, "unavailable");
|
||||
const auto newest_text =
|
||||
newest->empty() ? std::optional<std::string>{} : at(newest->front(), 0);
|
||||
const auto reference = newest_text ? check_tuning(*newest_text)
|
||||
: !config_.baseline_tuning.empty()
|
||||
? check_tuning(config_.baseline_tuning)
|
||||
: check_tuning({});
|
||||
if (reference && reference->rules.safety_version != checked->rules.safety_version)
|
||||
return json(400, "{\"error\":\"safety_version_changed\",\"expected\":" +
|
||||
std::to_string(reference->rules.safety_version) + "}\n");
|
||||
const auto digest = hex(checked->digest);
|
||||
const auto only_check = query_value(query_text, "check", 1);
|
||||
if (only_check == "1")
|
||||
return json(200, "{\"valid\":true,\"rules_sha256\":\"" + digest + "\",\"safety_version\":" +
|
||||
std::to_string(checked->rules.safety_version) + "}\n");
|
||||
const auto reason = query_value(query_text, "reason");
|
||||
if (!reason && query_text.find("reason=") != std::string_view::npos)
|
||||
return error(400, "bad_reason");
|
||||
if (config_.view_regions.empty())
|
||||
return error(400, "no_regions");
|
||||
std::string regions = "{";
|
||||
for (std::size_t i = 0; i < config_.view_regions.size(); ++i)
|
||||
regions += (i == 0 ? "" : ",") + std::to_string(config_.view_regions[i]);
|
||||
regions += "}";
|
||||
if (!db_ && connect_)
|
||||
db_ = connect_();
|
||||
if (!db_)
|
||||
return error(503, "unavailable");
|
||||
const std::vector<std::string> parameters{std::to_string(config_.world),
|
||||
std::string(text),
|
||||
digest,
|
||||
regions,
|
||||
author_of(admin),
|
||||
reason.value_or("")};
|
||||
const auto rows = db_->query(
|
||||
"SELECT evolution.admin_set_tuning_v1($1, $2, decode($3, 'hex'), $4::bigint[], $5, $6)",
|
||||
parameters, config_.db_timeout);
|
||||
if (!rows) {
|
||||
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_revision" : "unavailable");
|
||||
}
|
||||
if (rows->empty())
|
||||
return error(503, "unavailable");
|
||||
return json(200, "{\"revision\":" + text_value(at(rows->front(), 0)) + ",\"rules_sha256\":\"" +
|
||||
digest + "\",\"regions\":" + std::to_string(config_.view_regions.size()) +
|
||||
"}\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");
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "evo/server/cellular_region_host.hpp"
|
||||
|
||||
#include "evo/biology/cellular_revamp_tuning.hpp"
|
||||
#include "evo/biology/founder.hpp"
|
||||
#include "evo/persistence/sha256.hpp"
|
||||
|
||||
@@ -986,12 +987,58 @@ void CellularRegionHost::reload_admin_commands() {
|
||||
{{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);
|
||||
if (!cellular_admin_command_valid(command))
|
||||
continue;
|
||||
// A tuning adoption waits until its revision's text rebuilds exactly these rules.
|
||||
if (command.kind == CellularAdminKind::adopt_tuning &&
|
||||
!verified_tuning_.contains(command.target)) {
|
||||
const std::array<std::string, 2> lookup{std::to_string(config_.key.world),
|
||||
std::to_string(command.target)};
|
||||
const auto text =
|
||||
db_->query("SELECT text FROM evolution.load_tuning_revision_v1($1::evolution.u64, "
|
||||
"$2::bigint)",
|
||||
lookup, config_.lease.commit_timeout);
|
||||
if (!text) {
|
||||
db_.reset();
|
||||
++status_.store_failures;
|
||||
return;
|
||||
}
|
||||
const auto cell = text->size() == 1 && (*text)[0].size() == 1
|
||||
? (*text)[0][0]
|
||||
: std::optional<std::string>{};
|
||||
const auto parsed = cell ? biology::parse_cellular_revamp_tuning(*cell)
|
||||
: std::unexpected{biology::CellularTuningError{
|
||||
biology::CellularTuningErrorKind::syntax, 0, {}}};
|
||||
if (!parsed) {
|
||||
std::printf("cellular tuning revision=%llu unusable\n",
|
||||
static_cast<unsigned long long>(command.target));
|
||||
continue;
|
||||
}
|
||||
const auto rules = cellular_revamp_rules(*parsed);
|
||||
const auto digest = persistence::sha256(encode_cellular_revamp_rules(rules));
|
||||
if (!digest || !std::ranges::equal(*digest, cellular_admin_digest(command))) {
|
||||
std::printf("cellular tuning revision=%llu digest mismatch\n",
|
||||
static_cast<unsigned long long>(command.target));
|
||||
continue;
|
||||
}
|
||||
verified_tuning_.emplace(command.target, rules);
|
||||
}
|
||||
loaded.push_back(command);
|
||||
}
|
||||
queued_admin_ = std::move(loaded);
|
||||
}
|
||||
|
||||
CellularTuningResolver CellularRegionHost::tuning_resolver() const {
|
||||
return [this](std::uint64_t revision,
|
||||
const std::array<std::uint8_t, 32>&) -> std::optional<CellularRevampRules> {
|
||||
// Verified against the command's digest when the command was loaded.
|
||||
const auto found = verified_tuning_.find(revision);
|
||||
if (found == verified_tuning_.end())
|
||||
return std::nullopt;
|
||||
return found->second;
|
||||
};
|
||||
}
|
||||
|
||||
std::vector<persistence::CellularMutationReservation>
|
||||
CellularRegionHost::unbound_mutations() const {
|
||||
std::vector<persistence::CellularMutationReservation> unbound;
|
||||
@@ -1018,7 +1065,10 @@ CellularRegionHost::publish(const CellularBatch& batch,
|
||||
CellularPublishFailure{CellularHostError::invalid_batch, {}, {}, {}});
|
||||
return publish_population(batch);
|
||||
}
|
||||
auto candidate = replay_cellular_batch(*state_, batch);
|
||||
std::vector<CellularMutationOutcome> outcomes;
|
||||
std::vector<CellularAdminResult> admin_results;
|
||||
auto candidate =
|
||||
replay_cellular_batch(*state_, batch, outcomes, admin_results, tuning_resolver());
|
||||
if (!candidate)
|
||||
return std::unexpected{
|
||||
CellularPublishFailure{CellularHostError::invalid_batch, {}, {}, {}}};
|
||||
|
||||
@@ -34,7 +34,8 @@ std::string cellular_account_text(const LifeAccount& account) {
|
||||
|
||||
std::string render_cellular_admin_view(const CellularLives& state,
|
||||
const CellularAdminOnline& online,
|
||||
CellularAdminViewLimits limits) {
|
||||
CellularAdminViewLimits limits,
|
||||
std::string_view rules_sha256) {
|
||||
const auto lives = state.lives();
|
||||
const auto patches = state.habitat().patches();
|
||||
std::string out;
|
||||
@@ -48,6 +49,11 @@ std::string render_cellular_admin_view(const CellularLives& state,
|
||||
append(out, state.tick());
|
||||
out += ",\"rules\":";
|
||||
append(out, state.rules_version());
|
||||
if (!rules_sha256.empty()) {
|
||||
out += ",\"rules_sha256\":\"";
|
||||
out += rules_sha256;
|
||||
out.push_back('"');
|
||||
}
|
||||
out += ",\"area\":[";
|
||||
append(out, state.area().width_um);
|
||||
out.push_back(',');
|
||||
|
||||
@@ -71,6 +71,7 @@ bool valid_intent(const CellularLifeIntent& life) {
|
||||
!(step.emerge && (step.frozen || step.bud || step.engulf || step.intake_target));
|
||||
}
|
||||
bool valid_ticks(std::span<const CellularTickIntents> ticks) {
|
||||
std::size_t adoptions = 0;
|
||||
std::set<std::uint64_t> identities;
|
||||
std::set<LifeAccount> accounts;
|
||||
for (const auto& tick : ticks) {
|
||||
@@ -95,10 +96,14 @@ bool valid_ticks(std::span<const CellularTickIntents> ticks) {
|
||||
}
|
||||
if (tick.admin.size() > max_cellular_tick_admin_commands)
|
||||
return false;
|
||||
for (std::size_t i = 0; i < tick.admin.size(); ++i)
|
||||
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;
|
||||
// ADR-0182: one tuning adoption per batch, so its successor names its rules.
|
||||
if (tick.admin[i].kind == CellularAdminKind::adopt_tuning && ++adoptions > 1)
|
||||
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 ||
|
||||
@@ -486,10 +491,23 @@ replay_cellular_batch(const CellularLives& base, const CellularBatch& batch,
|
||||
return replay_cellular_batch(base, batch, mutation_outcomes, ignored);
|
||||
}
|
||||
|
||||
CellularTuningResolver cellular_successor_tuning(
|
||||
const CellularLives& successor,
|
||||
std::function<bool(std::span<const std::uint8_t>, const std::array<std::uint8_t, 32>&)> check) {
|
||||
return [rules = successor.revamp_rules(), check = std::move(check)](
|
||||
std::uint64_t,
|
||||
const std::array<std::uint8_t, 32>& digest) -> std::optional<CellularRevampRules> {
|
||||
if (!check || !check(encode_cellular_revamp_rules(rules), digest))
|
||||
return std::nullopt;
|
||||
return rules;
|
||||
};
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<CellularLives>, CellularBatchError>
|
||||
replay_cellular_batch(const CellularLives& base, const CellularBatch& batch,
|
||||
std::vector<CellularMutationOutcome>& mutation_outcomes,
|
||||
std::vector<CellularAdminResult>& admin_results) {
|
||||
std::vector<CellularAdminResult>& admin_results,
|
||||
const CellularTuningResolver& tuning) {
|
||||
mutation_outcomes.clear();
|
||||
admin_results.clear();
|
||||
if (!owner_version_matches(base, batch))
|
||||
@@ -528,7 +546,7 @@ replay_cellular_batch(const CellularLives& base, const CellularBatch& batch,
|
||||
}
|
||||
if (!tick.admin.empty()) {
|
||||
std::vector<CellularAdminResult> results;
|
||||
auto applied = (*replay)->admin_candidate(tick.admin, &results);
|
||||
auto applied = (*replay)->admin_candidate(tick.admin, &results, tuning);
|
||||
if (!applied)
|
||||
return std::unexpected{CellularBatchError::state};
|
||||
*replay = std::move(*applied);
|
||||
|
||||
@@ -1469,6 +1469,51 @@ CellularLives::seed_wildlife_root(State& next, std::uint64_t world, std::uint64_
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::array<std::uint8_t, 32> cellular_admin_digest(const CellularAdminCommand& c) noexcept {
|
||||
std::array<std::uint8_t, 32> digest{};
|
||||
const std::array<std::uint64_t, 4> words{c.amount.energy.value, c.amount.matter[0].value,
|
||||
c.amount.matter[1].value, c.amount.matter[2].value};
|
||||
for (std::size_t w = 0; w < words.size(); ++w)
|
||||
for (std::size_t b = 0; b < 8; ++b)
|
||||
digest.at(w * 8 + b) = static_cast<std::uint8_t>(words.at(w) >> (8 * b));
|
||||
return digest;
|
||||
}
|
||||
|
||||
CellularAdminCommand
|
||||
cellular_adopt_tuning_command(std::uint64_t id, std::uint64_t revision,
|
||||
const std::array<std::uint8_t, 32>& digest) noexcept {
|
||||
CellularAdminCommand command{id, CellularAdminKind::adopt_tuning, 0, revision, {}};
|
||||
std::array<std::uint64_t, 4> words{};
|
||||
for (std::size_t w = 0; w < words.size(); ++w)
|
||||
for (std::size_t b = 0; b < 8; ++b)
|
||||
words.at(w) |= std::uint64_t{digest.at(w * 8 + b)} << (8 * b);
|
||||
command.amount.energy.value = words[0];
|
||||
for (std::size_t c = 0; c < 3; ++c)
|
||||
command.amount.matter.at(c).value = words.at(c + 1);
|
||||
return command;
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> encode_cellular_revamp_rules(const CellularRevampRules& r) {
|
||||
std::vector<std::uint8_t> bytes;
|
||||
Writer out{bytes};
|
||||
out.put(r.body.essential_upkeep_per_thousand, 4);
|
||||
out.put((r.step.collection_percent == 100 ? 0U : r.step.collection_percent) |
|
||||
(r.step.split_evenly ? 0x10000U : 0U),
|
||||
4);
|
||||
for (const auto rate : r.burn_pj_per_kfg)
|
||||
out.put(rate);
|
||||
out.put(r.step.membrane_collection_per_mille_per_point, 4);
|
||||
out.put(r.step.engulf_ratio_milli, 4);
|
||||
out.put(r.step.engulf_reserve_percent, 4);
|
||||
out.put(r.step.kill_bubble_per_thousand, 4);
|
||||
out.put(r.drip_interval_ticks, 4);
|
||||
out.put(r.reserve_load_per_thousand, 4);
|
||||
out.put(r.drip_bubble_pj);
|
||||
out.put(r.drip_bubble_fg);
|
||||
write_revamp_offers(out, r);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
bool cellular_admin_command_valid(const CellularAdminCommand& c) noexcept {
|
||||
const bool some = c.amount != accounting::Inventory{};
|
||||
switch (c.kind) {
|
||||
@@ -1484,13 +1529,16 @@ bool cellular_admin_command_valid(const CellularAdminCommand& c) noexcept {
|
||||
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;
|
||||
case CellularAdminKind::adopt_tuning:
|
||||
return c.id != 0 && c.count == 0 && c.target != 0 && some && c.amount.matter[3].value == 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||
CellularLives::admin_candidate(std::span<const CellularAdminCommand> commands,
|
||||
std::vector<CellularAdminResult>* results) const {
|
||||
std::vector<CellularAdminResult>* results,
|
||||
const CellularTuningResolver& tuning) const {
|
||||
if (state_->format_version != cellular_lives_revamp_format ||
|
||||
state_->rules_version < cellular_lives_revamp_rules)
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
@@ -1575,6 +1623,20 @@ CellularLives::admin_candidate(std::span<const CellularAdminCommand> commands,
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CellularAdminKind::adopt_tuning: {
|
||||
// A revision nobody can name is not a refusal: replay would diverge.
|
||||
const auto rules =
|
||||
tuning ? tuning(command.target, cellular_admin_digest(command)) : std::nullopt;
|
||||
if (!rules)
|
||||
return std::unexpected{CellularLivesError::invalid_config};
|
||||
// The safety revision is a recorded law of the opening; it does not change live.
|
||||
if (cellular_revamp_valid(*rules) &&
|
||||
rules->safety_version == next.revamp.safety_version) {
|
||||
next.revamp = *rules;
|
||||
applied = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CellularAdminKind::spawn_wildlife: {
|
||||
if (next.lives.size() + command.count >= next.revamp.organism_cap)
|
||||
break;
|
||||
|
||||
+41
-12
@@ -1,8 +1,10 @@
|
||||
#include "evo/server/ops_http.hpp"
|
||||
#include <algorithm>
|
||||
#include <arpa/inet.h>
|
||||
#include <cerrno>
|
||||
#include <charconv>
|
||||
#include <netinet/in.h>
|
||||
#include <optional>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -25,6 +27,8 @@ const char* reason(int status) {
|
||||
return "Too Many Requests";
|
||||
case 405:
|
||||
return "Method Not Allowed";
|
||||
case 413:
|
||||
return "Content Too Large";
|
||||
case 431:
|
||||
return "Request Header Fields Too Large";
|
||||
case 503:
|
||||
@@ -134,7 +138,7 @@ void OpsServer::respond(Connection& c, const OpsHandler& handler) {
|
||||
c.out = encode(response);
|
||||
}
|
||||
|
||||
void OpsServer::respond_api(Connection& c, const ApiHandler& handler) {
|
||||
void OpsServer::respond_api(Connection& c, const ApiHandler& handler, std::size_t body_limit) {
|
||||
OpsResponse response;
|
||||
const std::string_view request = c.in;
|
||||
const auto line_end = request.find("\r\n");
|
||||
@@ -153,7 +157,8 @@ void OpsServer::respond_api(Connection& c, const ApiHandler& handler) {
|
||||
const auto query = path.find('?');
|
||||
const auto route = path.substr(0, query);
|
||||
std::string_view authorization;
|
||||
bool has_body = false;
|
||||
bool bad_body = false;
|
||||
std::optional<std::size_t> length;
|
||||
// Header lines up to the blank line; names are case-insensitive.
|
||||
auto at = line_end + 2;
|
||||
while (at < request.size()) {
|
||||
@@ -169,20 +174,39 @@ void OpsServer::respond_api(Connection& c, const ApiHandler& handler) {
|
||||
auto value = header.substr(colon + 1);
|
||||
while (!value.empty() && value.front() == ' ')
|
||||
value.remove_prefix(1);
|
||||
if (name == "authorization")
|
||||
if (name == "authorization") {
|
||||
authorization = value;
|
||||
else if ((name == "content-length" && value != "0") || name == "transfer-encoding")
|
||||
has_body = true;
|
||||
} else if (name == "content-length") {
|
||||
std::size_t bytes{};
|
||||
const auto [stop, ec] =
|
||||
std::from_chars(value.data(), value.data() + value.size(), bytes);
|
||||
if (length || value.empty() || ec != std::errc{} ||
|
||||
stop != value.data() + value.size())
|
||||
bad_body = true;
|
||||
else
|
||||
length = bytes;
|
||||
} else if (name == "transfer-encoding") {
|
||||
bad_body = true; // Chunked bodies are not supported.
|
||||
}
|
||||
}
|
||||
at = end + 2;
|
||||
}
|
||||
const auto body_at = request.find("\r\n\r\n") + 4;
|
||||
const auto body_size = length.value_or(0);
|
||||
if (!bad_body && body_size > 0 && body_size <= body_limit &&
|
||||
request.size() < body_at + body_size)
|
||||
return; // The rest of the body is still arriving.
|
||||
const bool has_body = bad_body || body_size > 0;
|
||||
if (method != "GET" && method != "POST")
|
||||
response = {405, "method not allowed\n"};
|
||||
else if (!bad_body && body_limit > 0 && body_size > body_limit)
|
||||
response = {413, "request body too large\n"};
|
||||
else if (route.empty() || route.front() != '/' || route.size() > 256 || !printable(path) ||
|
||||
has_body)
|
||||
(has_body && (body_limit == 0 || bad_body || method != "POST" ||
|
||||
request.size() != body_at + body_size)))
|
||||
response = {400, "bad request\n"};
|
||||
else
|
||||
response = handler({method, path, authorization});
|
||||
response = handler({method, path, authorization, request.substr(body_at, body_size)});
|
||||
if (response.body.size() > response_limit_)
|
||||
response = {500, "response too large\n"};
|
||||
c.out = encode(response);
|
||||
@@ -192,8 +216,10 @@ void OpsServer::serve(const OpsHandler& handler) {
|
||||
serve_with(max_request_bytes, [&](Connection& c) { respond(c, handler); });
|
||||
}
|
||||
|
||||
void OpsServer::serve_api(const ApiHandler& handler) {
|
||||
serve_with(max_api_request_bytes, [&](Connection& c) { respond_api(c, handler); });
|
||||
void OpsServer::serve_api(const ApiHandler& handler, std::size_t body_limit) {
|
||||
body_limit = std::min(body_limit, max_api_body_bytes);
|
||||
serve_with(max_api_request_bytes + body_limit,
|
||||
[&](Connection& c) { respond_api(c, handler, body_limit); });
|
||||
}
|
||||
|
||||
template <typename Answer>
|
||||
@@ -216,8 +242,9 @@ void OpsServer::serve_with(std::size_t request_limit, const Answer& answer) {
|
||||
bool done = false;
|
||||
// Read before expiring: a complete request that waited behind a bounded host
|
||||
// step is still answered; only incomplete or unsent work expires.
|
||||
if (c.out.empty()) {
|
||||
char chunk[1024];
|
||||
// A few reads per call, so a request body arrives well inside the deadline.
|
||||
for (int read = 0; read < 16 && !done && c.out.empty(); ++read) {
|
||||
char chunk[4096];
|
||||
const auto n = ::recv(c.fd, chunk, sizeof chunk, 0);
|
||||
if (n == 0 || (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)) {
|
||||
done = true;
|
||||
@@ -226,8 +253,10 @@ void OpsServer::serve_with(std::size_t request_limit, const Answer& answer) {
|
||||
if (c.in.size() > request_limit)
|
||||
c.out = encode({431, "request too large\n"});
|
||||
else if (c.in.find("\r\n\r\n") != std::string::npos)
|
||||
answer(c);
|
||||
answer(c); // Leaves c.out empty while a body is still arriving.
|
||||
}
|
||||
if (n < static_cast<long>(sizeof chunk))
|
||||
break;
|
||||
}
|
||||
if (!done && !c.out.empty()) {
|
||||
const auto n = ::send(c.fd, c.out.data() + c.sent, c.out.size() - c.sent, MSG_NOSIGNAL);
|
||||
|
||||
@@ -336,7 +336,7 @@ std::expected<void, CellularSessionBatchError> CellularSessionBatchBuilder::appe
|
||||
prepared->intents.births = std::move(accepted_births);
|
||||
std::vector<CellularAdminResult> admin_results;
|
||||
if (!admin.empty()) {
|
||||
auto applied = (*next)->admin_candidate(admin, &admin_results);
|
||||
auto applied = (*next)->admin_candidate(admin, &admin_results, tuning_);
|
||||
if (!applied) {
|
||||
last_lives_error_ = applied.error();
|
||||
return std::unexpected{CellularSessionBatchError::biology};
|
||||
|
||||
@@ -1587,6 +1587,56 @@ void run_starting_wildlife_publication_tests() {
|
||||
CHECK(!publish_cellular_batch(*runtime, **objects, *lease, head, again, **again_state,
|
||||
identity(11), build, 5s));
|
||||
runtime = connect_as(env("EVO_PG_RUNTIME_USER"));
|
||||
|
||||
// ADR-0182 (schema 66): a tuning revision's adoption publishes, binds and recovers; the
|
||||
// publisher and recovery name its rules from the successor state, checked by digest.
|
||||
auto tuned_rules = recovered->state->revamp_rules();
|
||||
auto& upkeep = tuned_rules.body.essential_upkeep_per_thousand;
|
||||
upkeep = upkeep == 100 ? 101 : 100;
|
||||
CHECK(evo::server::cellular_revamp_valid(tuned_rules) &&
|
||||
tuned_rules != recovered->state->revamp_rules());
|
||||
const auto tuned_digest = checked_value(
|
||||
evo::persistence::sha256(evo::server::encode_cellular_revamp_rules(tuned_rules)));
|
||||
std::string tuned_hex;
|
||||
for (const auto byte : tuned_digest) {
|
||||
constexpr std::string_view digits = "0123456789abcdef";
|
||||
tuned_hex.push_back(digits[byte >> 4U]);
|
||||
tuned_hex.push_back(digits[byte & 0x0FU]);
|
||||
}
|
||||
const auto revision_rows = owner->query(
|
||||
"SELECT evolution.admin_set_tuning_v1($1, 'not parsed by publication', "
|
||||
"decode($2, 'hex'), ARRAY[$3::bigint], 'ops', 'test')",
|
||||
std::array{std::to_string(key.world), tuned_hex, std::to_string(key.region)}, 5s);
|
||||
CHECK(revision_rows && revision_rows->size() == 1);
|
||||
const auto tuning_revision = std::stoull(revision_rows.value().at(0).at(0).value_or(""));
|
||||
const auto queued_adoption = runtime->query(
|
||||
"SELECT command_id FROM evolution.load_pending_admin_commands_v1($1, $2) WHERE kind = 7",
|
||||
std::array{std::to_string(key.world), std::to_string(key.region)}, 5s);
|
||||
CHECK(queued_adoption && queued_adoption->size() == 1);
|
||||
const auto adoption = evo::server::cellular_adopt_tuning_command(
|
||||
std::stoull(queued_adoption.value().at(0).at(0).value_or("")), tuning_revision,
|
||||
tuned_digest);
|
||||
auto adopt_batch = admin_batch({adoption});
|
||||
adopt_batch.header = {key.world, key.region, 1, head.head.commit, head.head.digest, 3, 4};
|
||||
adopt_batch.ticks.front().lives = checked_value(wildlife_intents(*recovered->state, 4));
|
||||
auto adopted = evo::fixture::restore(*recovered->state);
|
||||
CHECK(adopted->advance_tick(adopt_batch.ticks.front().lives));
|
||||
std::vector<evo::server::CellularAdminResult> adopt_results;
|
||||
auto adopted_state =
|
||||
adopted->admin_candidate(std::vector{adoption}, &adopt_results,
|
||||
[&](std::uint64_t, const std::array<std::uint8_t, 32>&) {
|
||||
return std::optional{tuned_rules};
|
||||
});
|
||||
CHECK(adopted_state && adopt_results.size() == 1 && adopt_results.front().applied);
|
||||
const auto adopted_bytes = (*adopted_state)->snapshot();
|
||||
auto adopted_publish = publish_cellular_batch(*runtime, **objects, *lease, head, adopt_batch,
|
||||
**adopted_state, identity(12), build, 5s);
|
||||
CHECK(adopted_publish);
|
||||
head = adopted_publish->committed;
|
||||
CHECK(pending() == 0);
|
||||
recovered = recover_cellular_checkpoint(*runtime, **objects, key, *lease, builds, 5s);
|
||||
CHECK(recovered && recovered->state->snapshot() == adopted_bytes &&
|
||||
recovered->state->revamp_rules() == tuned_rules);
|
||||
CHECK(release_region_lease(*runtime, key, *lease, 5s));
|
||||
std::puts("starting wildlife publication and replacement-owner replay passed");
|
||||
}
|
||||
|
||||
@@ -968,6 +968,97 @@ void admin_commands_apply_and_replay() {
|
||||
require(!decode_cellular_batch(mangled), "a trace 4 with commands decoded");
|
||||
}
|
||||
|
||||
// ADR-0182 tuning amendments: adopt_tuning swaps the rules at a recorded tick; whoever applies
|
||||
// or replays it must name the rules for its revision and digest, and the safety revision stays.
|
||||
void tuning_is_adopted_and_replayed() {
|
||||
auto live = revamp_region();
|
||||
const auto base = restore(*live);
|
||||
const auto& before = live->revamp_rules();
|
||||
auto changed = before;
|
||||
changed.drip_interval_ticks += 7;
|
||||
changed.drip_bubble_pj *= 2;
|
||||
changed.body.essential_upkeep_per_thousand = 200;
|
||||
require(cellular_revamp_valid(changed) &&
|
||||
encode_cellular_revamp_rules(changed) != encode_cellular_revamp_rules(before),
|
||||
"the changed rules are not distinct valid rules");
|
||||
std::array<std::uint8_t, 32> digest{};
|
||||
for (std::size_t i = 0; i < digest.size(); ++i)
|
||||
digest.at(i) = static_cast<std::uint8_t>(i * 7 + 1);
|
||||
const auto adopt = cellular_adopt_tuning_command(1, 5, digest);
|
||||
require(cellular_admin_command_valid(adopt) && cellular_admin_digest(adopt) == digest &&
|
||||
adopt.target == 5,
|
||||
"the adoption command does not carry its revision and digest");
|
||||
auto zero = cellular_adopt_tuning_command(1, 5, {});
|
||||
auto counted = adopt;
|
||||
counted.count = 1;
|
||||
auto other = adopt;
|
||||
other.amount.matter[3].value = 1;
|
||||
for (const auto& bad : {zero, counted, other})
|
||||
require(!cellular_admin_command_valid(bad), "an invalid adoption was accepted");
|
||||
const CellularTuningResolver resolver =
|
||||
[&](std::uint64_t revision,
|
||||
const std::array<std::uint8_t, 32>& named) -> std::optional<CellularRevampRules> {
|
||||
if (revision != 5 || named != digest)
|
||||
return std::nullopt;
|
||||
return changed;
|
||||
};
|
||||
|
||||
const auto intents = swim(*live, {});
|
||||
checked(live->advance_tick(intents), "tick before the adoption");
|
||||
const std::vector commands{adopt};
|
||||
// Nobody can name the rules: an error, not a refusal (replay would diverge).
|
||||
require(!live->admin_candidate(commands), "an unnamed revision was applied or refused");
|
||||
std::vector<CellularAdminResult> results;
|
||||
auto after = checked(live->admin_candidate(commands, &results, resolver), "adoption applies");
|
||||
require(results.size() == 1 && results.front().applied && after->revamp_rules() == changed,
|
||||
"the rules were not adopted");
|
||||
require(restore(*after)->snapshot() == after->snapshot(), "adopted rules do not restore");
|
||||
// The safety revision is a law of the opening: such a revision is refused, nothing changes.
|
||||
auto unsafe = changed;
|
||||
unsafe.safety_version = before.safety_version + 1;
|
||||
std::vector<CellularAdminResult> refused;
|
||||
const auto kept =
|
||||
checked(live->admin_candidate(commands, &refused,
|
||||
[&](std::uint64_t, const std::array<std::uint8_t, 32>&) {
|
||||
return std::optional{unsafe};
|
||||
}),
|
||||
"an unsafe adoption is a refusal");
|
||||
require(refused.size() == 1 && !refused.front().applied && kept->revamp_rules() == before,
|
||||
"a safety revision change was adopted");
|
||||
|
||||
// Trace 5 replays the adoption with the same resolver, and the successor state names it.
|
||||
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 decoded =
|
||||
checked(decode_cellular_batch(checked(encode_cellular_batch(batch), "encodes")), "decodes");
|
||||
std::vector<CellularMutationOutcome> outcomes;
|
||||
std::vector<CellularAdminResult> replayed_results;
|
||||
require(!replay_cellular_batch(*base, decoded, outcomes, replayed_results),
|
||||
"an adoption replayed without its rules");
|
||||
const auto replayed =
|
||||
checked(replay_cellular_batch(*base, decoded, outcomes, replayed_results, resolver),
|
||||
"the adoption replays");
|
||||
require(replayed->snapshot() == after->snapshot(), "the adoption replay diverged");
|
||||
const auto encoded = encode_cellular_revamp_rules(changed);
|
||||
const auto successor = cellular_successor_tuning(
|
||||
*replayed, [&](std::span<const std::uint8_t> bytes, const std::array<std::uint8_t, 32>& d) {
|
||||
return d == digest && std::ranges::equal(bytes, encoded);
|
||||
});
|
||||
const auto from_successor =
|
||||
checked(replay_cellular_batch(*base, decoded, outcomes, replayed_results, successor),
|
||||
"the successor names the adopted rules");
|
||||
require(from_successor->snapshot() == after->snapshot(), "the successor's rules diverged");
|
||||
// One adoption per batch: its successor can name only one set of rules.
|
||||
auto twice = batch;
|
||||
twice.ticks.front().admin.push_back(cellular_adopt_tuning_command(2, 6, digest));
|
||||
require(!encode_cellular_batch(twice), "two adoptions shared a batch");
|
||||
}
|
||||
|
||||
// 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) {
|
||||
@@ -1127,6 +1218,7 @@ void safe_bud_provision(bool become) {
|
||||
|
||||
int main() try {
|
||||
admin_commands_apply_and_replay();
|
||||
tuning_is_adopted_and_replayed();
|
||||
safety_split(false);
|
||||
safety_split(true);
|
||||
safe_bud_provision(false);
|
||||
|
||||
@@ -12,7 +12,10 @@
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/evp.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <source_location>
|
||||
@@ -630,6 +633,121 @@ void admin_api_tests(const Realm& realm) {
|
||||
CHECK(post("/api/regions/1/schedules?kind=add_bubbles&count=4&interval=60&energy=1&id=999999")
|
||||
.status == 400);
|
||||
|
||||
// Tuning revisions (schema 66): a request body in the Git revamp file format, checked,
|
||||
// stored with the admin's identity and queued as an adopt_tuning command per region.
|
||||
{
|
||||
const auto file = [](std::string_view name) {
|
||||
std::ifstream in{env("EVO_SOURCE_ROOT") + "/deploy/kubernetes/region/dev/tuning/" +
|
||||
std::string(name),
|
||||
std::ios::binary};
|
||||
return std::string{std::istreambuf_iterator<char>{in}, {}};
|
||||
};
|
||||
const auto baseline = file("cellular-revamp.tuning");
|
||||
CHECK(!baseline.empty());
|
||||
server::admin::AdminConfig config;
|
||||
config.world = 9831;
|
||||
config.view_regions = {1};
|
||||
config.baseline_tuning = baseline;
|
||||
AdminApi tuned{config,
|
||||
*TokenVerifier::from_jwks(realm.jwks("sig-1"), admin_rules),
|
||||
[] { return connect_as(env("EVO_PG_ADMIN_USER")); },
|
||||
[](std::uint64_t) { return std::optional<std::string>{}; },
|
||||
{}};
|
||||
const auto send = [&](std::string_view target, std::string_view body) {
|
||||
return tuned.handle({"POST", target, admin, body}, now, t0);
|
||||
};
|
||||
const auto read = [&](std::string_view target) {
|
||||
return tuned.handle({"GET", target, admin}, now, t0);
|
||||
};
|
||||
const auto none = must(JsonValue::parse(read("/api/tuning").body));
|
||||
const auto digest =
|
||||
std::string(must(none.member("baseline")->member("rules_sha256")->string()));
|
||||
CHECK(none.member("newest")->kind() == JsonValue::Kind::null &&
|
||||
none.member("revisions")->array()->empty() &&
|
||||
none.member("baseline")->member("text")->string() == baseline &&
|
||||
digest.size() == 64 && none.member("regions")->array()->at(0).string() == "1");
|
||||
|
||||
// Bodies go to /api/tuning only; check=1 stores nothing.
|
||||
CHECK(tuned
|
||||
.handle({"POST", "/api/regions/1/commands?kind=add_pool&energy=1", admin, "x"},
|
||||
now, t0)
|
||||
.status == 400);
|
||||
const auto checked = must(JsonValue::parse(send("/api/tuning?check=1", baseline).body));
|
||||
CHECK(checked.member("valid")->boolean() == true &&
|
||||
checked.member("rules_sha256")->string() == digest &&
|
||||
checked.member("safety_version")->integer() == 0);
|
||||
// Problems name the kind, line and key; the safety revision cannot change live.
|
||||
const auto lines = std::ranges::count(baseline, '\n');
|
||||
const auto unknown = must(
|
||||
JsonValue::parse(send("/api/tuning?check=1", baseline + "no.such_key = 1\n").body));
|
||||
CHECK(unknown.member("error")->string() == "invalid_tuning" &&
|
||||
unknown.member("kind")->string() == "unknown_key" &&
|
||||
unknown.member("key")->string() == "no.such_key" &&
|
||||
unknown.member("line")->integer() == lines + 1);
|
||||
CHECK(send("/api/tuning", "").status == 400 &&
|
||||
send("/api/tuning", "a = \x01").status == 400);
|
||||
const auto unsafe = must(JsonValue::parse(
|
||||
send("/api/tuning?check=1", file("cellular-reproduction-safety.tuning")).body));
|
||||
CHECK(unsafe.member("error")->string() == "safety_version_changed" &&
|
||||
unsafe.member("expected")->integer() == 0);
|
||||
CHECK(
|
||||
must(JsonValue::parse(read("/api/tuning").body)).member("revisions")->array()->empty());
|
||||
|
||||
// A slower drip: stored, listed, and queued for region 1 with its digest.
|
||||
auto changed = baseline;
|
||||
const auto at = changed.find("spent.drip_interval_ticks = 20");
|
||||
CHECK(at != std::string::npos);
|
||||
changed.replace(at, 30, "spent.drip_interval_ticks = 40");
|
||||
const auto stored =
|
||||
must(JsonValue::parse(send("/api/tuning?reason=slower+drip", changed).body));
|
||||
const auto revision = std::string(must(stored.member("revision")->string()));
|
||||
const auto changed_digest = std::string(must(stored.member("rules_sha256")->string()));
|
||||
CHECK(changed_digest != digest && stored.member("regions")->integer() == 1);
|
||||
const auto listed_tuning = must(JsonValue::parse(read("/api/tuning").body));
|
||||
const auto& entry = listed_tuning.member("revisions")->array()->at(0);
|
||||
CHECK(listed_tuning.member("newest")->member("revision")->string() == revision &&
|
||||
listed_tuning.member("newest")->member("text")->string() == changed &&
|
||||
entry.member("rules_sha256")->string() == changed_digest &&
|
||||
entry.member("reason")->string() == "slower drip" &&
|
||||
entry.member("author")->string() == "ops@example.test (ops)" &&
|
||||
entry.member("regions")->integer() == 1 && entry.member("adopted")->integer() == 0);
|
||||
CHECK(
|
||||
must(JsonValue::parse(read("/api/tuning/" + revision).body)).member("text")->string() ==
|
||||
changed);
|
||||
CHECK(read("/api/tuning/999999").status == 404 && read("/api/tuning/0").status == 404 &&
|
||||
read("/api/tuning/x").status == 404);
|
||||
std::uint64_t first_word = 0;
|
||||
for (std::size_t b = 0; b < 8; ++b)
|
||||
first_word |= std::stoull(changed_digest.substr(b * 2, 2), nullptr, 16) << (8 * b);
|
||||
auto reader = connect_as(env("EVO_PG_RUNTIME_USER"));
|
||||
const auto adoption =
|
||||
reader->query("SELECT cell_count, target, energy, other FROM "
|
||||
"evolution.load_pending_admin_commands_v1(9831, 1) WHERE kind = 7",
|
||||
{}, 5s);
|
||||
CHECK(adoption && adoption->size() == 1 && (*adoption)[0][0] == "0" &&
|
||||
(*adoption)[0][1] == revision && (*adoption)[0][2] == std::to_string(first_word) &&
|
||||
(*adoption)[0][3] == "0");
|
||||
const auto text = reader->query(
|
||||
"SELECT text, encode(rules_sha256, 'hex') FROM evolution.load_tuning_revision_v1(9831, "
|
||||
"$1::bigint)",
|
||||
std::array{revision}, 5s);
|
||||
CHECK(text && text->size() == 1 && (*text)[0][0] == changed &&
|
||||
(*text)[0][1] == changed_digest);
|
||||
// Only the admin stores revisions, rows never change, and kind 7 is never queued by hand.
|
||||
CHECK(!reader->query("SELECT evolution.admin_set_tuning_v1(9831, 'x', "
|
||||
"decode(repeat('ab', 32), 'hex'), '{1}', 'x', '')",
|
||||
{}, 5s));
|
||||
reader = connect_as(env("PGUSER"));
|
||||
CHECK(!reader->query("UPDATE evolution.tuning_revisions SET reason = 'x'", {}, 5s));
|
||||
CHECK(post("/api/regions/1/commands?kind=adopt_tuning&target=1").status == 400);
|
||||
CHECK(post("/api/regions/1/schedules?kind=adopt_tuning&count=1&interval=60").status == 400);
|
||||
// An unknown region (3 has no lease) refuses the whole revision.
|
||||
CHECK(api.handle({"POST", "/api/tuning", admin, changed}, now, t0).status == 400);
|
||||
CHECK(
|
||||
must(JsonValue::parse(read("/api/tuning").body)).member("revisions")->array()->size() ==
|
||||
1);
|
||||
}
|
||||
|
||||
// Accounts (schema 64): control claims recorded alice's sessions; a ban refuses grants
|
||||
// and is listed with its reason; lifting it restores them; an admin logout succeeds.
|
||||
CHECK(alice->member("sessions")->integer().value_or(0) >= 1 &&
|
||||
|
||||
@@ -231,6 +231,7 @@ void upgrade_across_migration_and_barrier() {
|
||||
" -v migration_sha_63=$(s 0063_admin_commands.sql)"
|
||||
" -v migration_sha_64=$(s 0064_account_admin.sql)"
|
||||
" -v migration_sha_65=$(s 0065_admin_schedules.sql)"
|
||||
" -v migration_sha_66=$(s 0066_tuning_revisions.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");
|
||||
@@ -248,7 +249,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,63,64,65");
|
||||
"47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66");
|
||||
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 = "
|
||||
|
||||
@@ -229,6 +229,63 @@ void accept_work_is_bounded_per_serve() {
|
||||
::close(fd);
|
||||
}
|
||||
|
||||
// ADR-0182: API request bodies only when asked for, complete (Content-Length, possibly over
|
||||
// several reads), bounded (413) and nothing else: no chunking, no body on GET, no extra bytes.
|
||||
void api_bodies_are_bounded_and_complete() {
|
||||
using evo::server::OpsServer;
|
||||
for (const std::size_t limit : {std::size_t{0}, std::size_t{64}}) {
|
||||
auto made = OpsServer::listen("127.0.0.1:0");
|
||||
CHECK(made);
|
||||
auto& ops = **made;
|
||||
std::atomic<bool> stop{false};
|
||||
std::thread serving([&] {
|
||||
while (!stop.load()) {
|
||||
ops.serve_api(
|
||||
[](const evo::server::ApiRequest& r) {
|
||||
return evo::server::OpsResponse{200, std::string(r.method) + ":" +
|
||||
std::string(r.body)};
|
||||
},
|
||||
limit);
|
||||
std::this_thread::sleep_for(1ms);
|
||||
}
|
||||
});
|
||||
const auto port = ops.port();
|
||||
const auto post = [&](const std::string& headers, const std::string& body) {
|
||||
std::string request = "POST /x HTTP/1.1\r\n";
|
||||
request.append(headers).append("\r\n").append(body);
|
||||
return http_exchange(port, request);
|
||||
};
|
||||
CHECK(post("", "").body == "POST:");
|
||||
CHECK(post("Content-Length: 0\r\n", "").body == "POST:");
|
||||
const auto hello = post("Content-Length: 5\r\n", "hello");
|
||||
CHECK(limit == 0 ? hello.status == 400 : hello.status == 200 && hello.body == "POST:hello");
|
||||
if (limit > 0) {
|
||||
// A body arriving after the headers, in a later read.
|
||||
const int fd = connect_local(port);
|
||||
const std::string first = "POST /x HTTP/1.1\r\nContent-Length: 5\r\n\r\nhe";
|
||||
CHECK(::send(fd, first.data(), first.size(), MSG_NOSIGNAL) ==
|
||||
static_cast<ssize_t>(first.size()));
|
||||
std::this_thread::sleep_for(50ms);
|
||||
CHECK(::send(fd, "llo", 3, MSG_NOSIGNAL) == 3);
|
||||
std::string raw;
|
||||
char chunk[512];
|
||||
for (ssize_t n = 0; (n = ::recv(fd, chunk, sizeof chunk, 0)) > 0;)
|
||||
raw.append(chunk, static_cast<std::size_t>(n));
|
||||
::close(fd);
|
||||
CHECK(raw.starts_with("HTTP/1.1 200") && raw.ends_with("\r\n\r\nPOST:hello"));
|
||||
}
|
||||
CHECK(post("Content-Length: 65\r\n", std::string(65, 'a')).status ==
|
||||
(limit == 0 ? 400 : 413));
|
||||
CHECK(post("Transfer-Encoding: chunked\r\n", "5\r\nhello\r\n0\r\n\r\n").status == 400);
|
||||
CHECK(post("Content-Length: 2\r\n", "hixx").status == 400);
|
||||
CHECK(post("Content-Length: 2\r\nContent-Length: 2\r\n", "hi").status == 400);
|
||||
CHECK(post("Content-Length: two\r\n", "hi").status == 400);
|
||||
CHECK(http_exchange(port, "GET /x HTTP/1.1\r\nContent-Length: 2\r\n\r\nhi").status == 400);
|
||||
stop = true;
|
||||
serving.join();
|
||||
}
|
||||
}
|
||||
|
||||
// A real server under sustained connection churn keeps its lease and readiness.
|
||||
void connection_pressure_keeps_authority() {
|
||||
const auto r = provision(54, 0x0D);
|
||||
@@ -266,6 +323,7 @@ void connection_pressure_keeps_authority() {
|
||||
|
||||
void run_region_ops_tests() {
|
||||
accept_work_is_bounded_per_serve();
|
||||
api_bodies_are_bounded_and_complete();
|
||||
connection_pressure_keeps_authority();
|
||||
probes_follow_authority_and_stay_bounded();
|
||||
unavailable_database_is_live_not_ready();
|
||||
|
||||
@@ -75,6 +75,13 @@ 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.admin_tuning_text_v1(evolution.u64,bigint);
|
||||
DROP FUNCTION evolution.admin_tuning_revisions_v1(evolution.u64);
|
||||
DROP FUNCTION evolution.load_tuning_revision_v1(evolution.u64,bigint);
|
||||
DROP FUNCTION evolution.admin_set_tuning_v1(evolution.u64,text,bytea,bigint[],text,text);
|
||||
DROP TABLE evolution.tuning_revisions;
|
||||
ALTER TABLE evolution.admin_commands DROP CONSTRAINT admin_commands_kind_check;
|
||||
ALTER TABLE evolution.admin_commands ADD CONSTRAINT admin_commands_kind_check CHECK (kind BETWEEN 1 AND 6);
|
||||
DROP FUNCTION evolution.load_pending_admin_commands_v1(evolution.u64,evolution.u64);
|
||||
ALTER FUNCTION evolution.load_pending_admin_commands_v64(evolution.u64,evolution.u64) RENAME TO load_pending_admin_commands_v1;
|
||||
GRANT EXECUTE ON FUNCTION evolution.load_pending_admin_commands_v1(evolution.u64,evolution.u64) TO :"runtime_role";
|
||||
|
||||
@@ -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,63,64,65
|
||||
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,64,65,66
|
||||
# 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")" = 65: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,64,65
|
||||
test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 66: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,64,65,66
|
||||
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,63,64,65
|
||||
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,64,65,66
|
||||
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=66 WHERE version=65'
|
||||
psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=67 WHERE version=66'
|
||||
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=65 WHERE version=66"
|
||||
psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=66 WHERE version=67"
|
||||
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"
|
||||
|
||||
@@ -146,6 +146,8 @@ digest_64="$(sha256sum "$root/migrations/0064_account_admin.sql")"
|
||||
digest_64="${digest_64%% *}"
|
||||
digest_65="$(sha256sum "$root/migrations/0065_admin_schedules.sql")"
|
||||
digest_65="${digest_65%% *}"
|
||||
digest_66="$(sha256sum "$root/migrations/0066_tuning_revisions.sql")"
|
||||
digest_66="${digest_66%% *}"
|
||||
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" \
|
||||
@@ -196,6 +198,7 @@ psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \
|
||||
--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" --set="migration_sha_63=$digest_63" \
|
||||
--set="migration_sha_64=$digest_64" --set="migration_sha_65=$digest_65" "${admin[@]}" \
|
||||
--set="migration_sha_64=$digest_64" --set="migration_sha_65=$digest_65" \
|
||||
--set="migration_sha_66=$digest_66" "${admin[@]}" \
|
||||
--set="runtime_role=$runtime_role" \
|
||||
--file="$root/migrations/apply.sql"
|
||||
|
||||
Reference in New Issue
Block a user