feat(admin): resource schedules for bubbles, pool releases and wildlife (schema 65)
Foundation / checks (ecs) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / postgres-tools (push) Successful in 23s
Foundation / server-image (push) Successful in 4m38s
Foundation / image (push) Successful in 5m35s

ADR-0182 section 12: the game source as recurring admin commands. A schedule
repeats add_bubbles, release_pool or spawn_wildlife every 1 s to 24 h while
enabled; the region's pending-command read queues each due run as an ordinary
command (recorded in the trace, bound once), never two waiting per schedule,
so an idle region builds no backlog. Append-only schedule versions; admin API
GET/POST /api/regions/<n>/schedules and a portal card to add, stop and start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-27 18:30:37 -04:00
co-authored by Claude Opus 5.5
parent 2485a028bb
commit 018b4b1817
13 changed files with 425 additions and 14 deletions
+56
View File
@@ -483,6 +483,59 @@ function commandsCard(region) {
return { card, load, send };
}
// Resource schedules (schema 65): a command repeated every N seconds while enabled. The game
// source for bubbles, a steady release of the spent pool, or a wildlife population top-up.
function schedulesCard(region) {
const list = h('div');
const note = h('div', { class: 'muted small' });
const kind = h('select', {}, [['add_bubbles', 'Add bubbles'], ['release_pool', 'Release spent pool'], ['spawn_wildlife', 'Spawn wildlife']]
.map(([v, label]) => h('option', { value: v }, label)));
const count = h('input', { type: 'number', min: 1, max: 256, value: 10, class: 'count', title: 'bubbles or cells each time' });
const every = h('input', { type: 'number', min: 1, max: 86400, value: 30, class: 'count', title: 'seconds between runs' });
const amount = amountInputs();
const reason = h('input', { type: 'text', maxlength: 200, placeholder: 'Reason (optional)' });
const fromSchedule = (x) => ({
id: x.id, kind: x.kind, count: String(x.count), interval: String(x.interval),
energy: x.amount[0], carbon: x.amount[1], nitrogen: x.amount[2], phosphorus: x.amount[3], other: x.amount[4],
});
const save = async (params, label) => {
try {
const q = new URLSearchParams(params);
if (reason.value.trim()) q.set('reason', reason.value.trim());
const r = await api(`/api/regions/${region}/schedules?${q}`, true, 'POST');
note.textContent = `Saved schedule #${r.id} (${label}).`;
reason.value = '';
load();
return true;
} catch (e) { note.textContent = `Could not save: ${e.code || e.message}`; return false; }
};
const add = () => {
let values;
try { values = kind.value === 'spawn_wildlife' ? {} : amount.read(); } catch (e) { note.textContent = e.message; return; }
save({ kind: kind.value, count: count.value, interval: every.value, enabled: '1', ...values }, 'new')
.then((ok) => { if (ok) amount.clear(); });
};
async function load() {
try {
const data = await api(`/api/regions/${region}/schedules`);
list.replaceChildren(data.schedules.length ? h('table', { class: 'compact' }, h('tbody', {}, data.schedules.map((x) =>
h('tr', {},
h('td', { class: 'mono' }, `#${x.id}`),
h('td', {}, `${kindLabels[x.kind] || x.kind} ×${x.count} every ${fmtDuration(x.interval)}`,
x.kind !== 'spawn_wildlife' ? h('div', { class: 'muted' }, `per run: ${fmtEnergy(x.amount[0])}, ${x.amount.slice(1).map(fmtMass).join(' / ')}`) : null),
h('td', {}, x.enabled ? h('span', { class: 'badge alive' }, `on · ${x.runs} runs`) : h('span', { class: 'badge' }, 'off')),
h('td', {}, h('button', { onclick: () => save({ ...fromSchedule(x), enabled: x.enabled ? '0' : '1' }, x.enabled ? 'disabled' : 'enabled') }, x.enabled ? 'Stop' : 'Start')))))) : h('p', { class: 'muted' }, 'No schedules.'));
} catch (e) { list.textContent = `Unavailable: ${e.message}`; }
}
const card = h('div', { class: 'card' }, h('h2', {}, 'Resource schedules'), list,
h('div', { class: 'action' }, h('strong', {}, 'New schedule'),
h('div', { class: 'row' }, kind, count, h('span', { class: 'muted' }, 'every'), every, h('span', { class: 'muted' }, 's')),
amount.node, h('div', { class: 'row' }, h('button', { onclick: add }, 'Add schedule'))),
reason, note);
load();
return { card, load };
}
// ---------- Region view ----------
const channelNames = ['energy', 'carbon', 'nitrogen', 'phosphorus', 'other'];
const css = (name) => getComputedStyle(document.documentElement).getPropertyValue(`--${name}`).trim();
@@ -500,6 +553,7 @@ async function regionPage(region, params) {
const live = h('input', { type: 'checkbox', checked: true });
const sessions = sessionWindowCard(region);
const commands = commandsCard(region);
const schedules = schedulesCard(region);
let view = null, selected = null, timer = null, beats = 0;
let zoom = 1, panX = 0, panY = 0, drag = null;
@@ -513,6 +567,7 @@ async function regionPage(region, params) {
sessions.card,
detail,
commands.card,
schedules.card,
h('div', { class: 'card' }, h('h2', {}, 'Show'), h('div', { class: 'toggles' },
toggle('players', 'Player cells'), toggle('wildlife', 'Wildlife'), toggle('pools', 'Pools'), toggle('bubbles', 'Bubbles'))),
h('div', { class: 'card' }, h('h2', {}, 'Legend'), h('div', { class: 'legend' },
@@ -712,6 +767,7 @@ async function regionPage(region, params) {
await load();
if (++beats % 10 === 0) sessions.load();
if (beats % 5 === 0) commands.load();
if (beats % 15 === 0) schedules.load();
}
tick();
}, 1000);
+7
View File
@@ -1,5 +1,12 @@
# Project status and next steps
## Resource schedules - 2026-10-27
The admin portal can schedule recurring bubbles (the game source), spent-pool releases and
wildlife spawns per region, every 1 s to 24 h, and stop or restart them. Each run is an
ordinary recorded admin command (schema 65). See
[ADR-0182](adr/0182-admin-portal-and-world-tuning.md) section 12.
## Account administration: bans, logout, time played - 2026-10-27
The admin portal can ban (and unban) an account, which also logs it out and stops new
+17 -2
View File
@@ -218,8 +218,23 @@ The commands of section 3 are delivered without a new owner format or region ope
- **Living offspring.** The region view adds each cell's lineage root; the portal counts
living wildlife whose root is one of the account's lives.
Still pending from this ADR: drip/source settings (section 4), tuning amendments
(sections 1-2), data-defined mutations (section 5) and the account's current mutations.
### 12. Resource schedules (delivered, schema 65)
The game source of section 4 without a new owner format: a schedule repeats an
`add_bubbles`, `release_pool` or `spawn_wildlife` command (count, amounts per resource)
every 1 s to 24 h while enabled. When the region reads its pending commands, each due
schedule queues its next command as an operator would, so it is recorded in the trace and
bound once; a schedule never has two commands waiting, so an idle region (no living cells,
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),
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
per-region Deployments, volumes and routes).
## Delivery order
+15
View File
@@ -10559,3 +10559,18 @@ admin logout, refused shapes (bad flag, unknown action, POST on the account itse
Limits: no live ban or logout was exercised against a connected client; time played starts
at this schema.
## 2026-10-27 - Resource schedules (schema 65)
Scope: ADR-0182 section 12. Recurring add_bubbles, release_pool and spawn_wildlife commands
queued by the pending-command read when due (one outstanding per schedule); append-only
schedule versions; admin API and portal card. No C++ simulation, trace or wire change.
Executed (WSL Ubuntu, GCC 14.2 Release/NDEBUG): full build; CTest 119/119;
`tools/ci/postgres-check.sh --full` passed (639 s, every phase; the schema-50 rewind restores
the pending-command read, and the probe moves to 66). New PostgreSQL cases: refused shapes
(disallowed kind, zero and over-long intervals, missing amounts, unknown schedule), a due
schedule queuing exactly one command across repeated reads, its run count and author in the
listings, and stopping it by a new version.
Limits: no live schedule has run on a region yet.
+8
View File
@@ -38,6 +38,11 @@
// [&nitrogen=FG][&phosphorus=FG][&other=FG][&reason=R]
// queues a command (remove_cell, grant_reserve,
// add_bubbles, add_pool, release_pool, spawn_wildlife)
// GET /api/regions/<n>/schedules the region's resource schedules (schema 65)
// POST /api/regions/<n>/schedules?kind=K&count=N&interval=S[&id=ID][&enabled=0|1]
// [&energy=PJ][&carbon=FG]...[&reason=R]
// creates (no id) or replaces a schedule repeating
// add_bubbles, release_pool or spawn_wildlife
namespace evo::server::admin {
struct AdminAsset {
std::string path; // "/index.html", ...; "/" serves "/index.html".
@@ -75,6 +80,9 @@ class AdminApi {
[[nodiscard]] OpsResponse view(std::uint64_t region, std::chrono::steady_clock::time_point now);
[[nodiscard]] OpsResponse window(std::uint64_t region);
[[nodiscard]] OpsResponse commands(std::uint64_t region);
[[nodiscard]] OpsResponse schedules(std::uint64_t region);
[[nodiscard]] OpsResponse set_schedule(std::uint64_t region, std::string_view query,
const broker::AccessTokenClaims& admin);
[[nodiscard]] OpsResponse enqueue(std::uint64_t region, std::string_view query,
const broker::AccessTokenClaims& admin);
[[nodiscard]] OpsResponse set_window(std::uint64_t region, std::string_view query,
+143
View File
@@ -0,0 +1,143 @@
-- Resource schedules (ADR-0182 section 4, 2026-10-27): the game source as recurring admin
-- commands. A schedule repeats an add_bubbles, release_pool or spawn_wildlife command every
-- `interval_seconds` while it is enabled. When the region reads its pending commands, each
-- enabled schedule whose last command was applied (or that never ran) and whose interval has
-- passed queues its next command, exactly as an operator would; it is then recorded in the
-- trace and bound once like any command. At most one command per schedule waits, so an idle
-- region (no living cells, no ticks) builds no backlog. Schedule versions are append-only;
-- the newest row of a schedule decides.
ALTER TABLE evolution.admin_commands ADD COLUMN schedule_id bigint;
CREATE TABLE evolution.admin_schedules (
sequence bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
schedule_id bigint NOT NULL,
world_id evolution.u64 NOT NULL,
region_id evolution.u64 NOT NULL,
kind smallint NOT NULL CHECK (kind IN (3, 5, 6)),
cell_count integer NOT NULL CHECK (cell_count BETWEEN 1 AND 256),
energy evolution.u64 NOT NULL,
carbon evolution.u64 NOT NULL,
nitrogen evolution.u64 NOT NULL,
phosphorus evolution.u64 NOT NULL,
other evolution.u64 NOT NULL,
interval_seconds integer NOT NULL CHECK (interval_seconds BETWEEN 1 AND 86400),
enabled boolean NOT NULL,
author_subject text NOT NULL CHECK (octet_length(author_subject) BETWEEN 1 AND 255),
reason text NOT NULL DEFAULT '' CHECK (octet_length(reason) <= 500),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
FOREIGN KEY (world_id, region_id) REFERENCES evolution.region_leases,
CHECK ((kind = 6 AND cell_count <= 64 AND energy + carbon + nitrogen + phosphorus + other = 0)
OR (kind IN (3, 5) AND energy + carbon + nitrogen + phosphorus + other > 0))
);
CREATE INDEX admin_schedules_by_schedule ON evolution.admin_schedules(schedule_id, sequence);
CREATE SEQUENCE evolution.admin_schedule_ids;
CREATE TRIGGER admin_schedules_immutable BEFORE UPDATE OR DELETE ON evolution.admin_schedules
FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite();
-- Admin: creates (p_schedule 0) or replaces a schedule's settings; disabling stops it.
CREATE FUNCTION evolution.admin_set_schedule_v1(p_world evolution.u64, p_region evolution.u64,
p_schedule bigint, p_kind integer, p_count integer, p_energy evolution.u64,
p_carbon evolution.u64, p_nitrogen evolution.u64, p_phosphorus evolution.u64,
p_other evolution.u64, p_interval integer, p_enabled boolean, p_subject text, p_reason text)
RETURNS bigint LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
DECLARE id bigint := p_schedule;
BEGIN
IF id IS NULL OR id = 0 THEN
id := nextval('evolution.admin_schedule_ids');
ELSIF NOT EXISTS (SELECT 1 FROM evolution.admin_schedules s WHERE s.schedule_id = id
AND s.world_id = p_world AND s.region_id = p_region) THEN
RAISE EXCEPTION 'unknown schedule' USING ERRCODE = '22023';
END IF;
BEGIN
INSERT INTO evolution.admin_schedules(schedule_id, world_id, region_id, kind, cell_count,
energy, carbon, nitrogen, phosphorus, other, interval_seconds, enabled, author_subject,
reason)
VALUES (id, p_world, p_region, p_kind, p_count, p_energy, p_carbon, p_nitrogen,
p_phosphorus, p_other, p_interval, p_enabled, p_subject, coalesce(p_reason, ''));
EXCEPTION WHEN check_violation OR foreign_key_violation OR not_null_violation THEN
RAISE EXCEPTION 'invalid schedule' USING ERRCODE = '22023';
END;
RETURN id;
END $$;
REVOKE ALL ON FUNCTION 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) FROM PUBLIC;
-- Admin: a region's schedules as they now stand, with their last queued command.
CREATE FUNCTION evolution.admin_schedules_v1(p_world evolution.u64, p_region evolution.u64)
RETURNS TABLE (schedule_id bigint, kind integer, cell_count integer, energy evolution.u64,
carbon evolution.u64, nitrogen evolution.u64, phosphorus evolution.u64, other evolution.u64,
interval_seconds integer, enabled boolean, author_subject text, reason text,
changed_at timestamptz, last_queued timestamptz, runs bigint)
LANGUAGE sql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
SELECT s.schedule_id, s.kind, s.cell_count, s.energy, s.carbon, s.nitrogen, s.phosphorus,
s.other, s.interval_seconds, s.enabled, s.author_subject, s.reason, s.created_at,
(SELECT max(q.created_at) FROM evolution.admin_commands q
WHERE q.schedule_id = s.schedule_id),
(SELECT count(*) FROM evolution.admin_commands q WHERE q.schedule_id = s.schedule_id)
FROM evolution.admin_schedules s
WHERE s.world_id = p_world AND s.region_id = p_region
AND s.sequence = (SELECT max(x.sequence) FROM evolution.admin_schedules x
WHERE x.schedule_id = s.schedule_id)
ORDER BY s.schedule_id
$$;
REVOKE ALL ON FUNCTION evolution.admin_schedules_v1(evolution.u64, evolution.u64) FROM PUBLIC;
-- Queues every due schedule's next command (spawning takes identities from the allocator).
CREATE FUNCTION evolution.queue_due_schedules(p_world evolution.u64, p_region evolution.u64)
RETURNS void LANGUAGE plpgsql SET search_path = pg_catalog, pg_temp AS $$
DECLARE
s record;
first numeric;
BEGIN
FOR s IN SELECT * FROM evolution.admin_schedules x
WHERE x.world_id = p_world AND x.region_id = p_region
AND x.sequence = (SELECT max(y.sequence) FROM evolution.admin_schedules y
WHERE y.schedule_id = x.schedule_id)
AND x.enabled
ORDER BY x.schedule_id LOOP
CONTINUE WHEN EXISTS (SELECT 1 FROM evolution.admin_commands q
WHERE q.schedule_id = s.schedule_id
AND NOT EXISTS (SELECT 1 FROM evolution.admin_command_applications a
WHERE a.command_id = q.command_id));
CONTINUE WHEN coalesce((SELECT max(q.created_at) FROM evolution.admin_commands q
WHERE q.schedule_id = s.schedule_id), '-infinity'::timestamptz)
+ make_interval(secs => s.interval_seconds) > clock_timestamp();
first := 0;
IF s.kind = 6 THEN
SELECT a.next_identity INTO first FROM evolution.life_allocators a
WHERE a.world_id = p_world FOR UPDATE;
CONTINUE WHEN first IS NULL OR first + s.cell_count > 9223372036854775808;
UPDATE evolution.life_allocators SET next_identity = first + s.cell_count
WHERE world_id = p_world;
END IF;
INSERT INTO evolution.admin_commands(world_id, region_id, kind, cell_count, target, energy,
carbon, nitrogen, phosphorus, other, author_subject, reason, schedule_id)
VALUES (p_world, p_region, s.kind, s.cell_count, first, s.energy, s.carbon, s.nitrogen,
s.phosphorus, s.other, 'schedule ' || s.schedule_id, s.reason, s.schedule_id);
END LOOP;
END $$;
REVOKE ALL ON FUNCTION evolution.queue_due_schedules(evolution.u64, evolution.u64) FROM PUBLIC;
-- The region's pending commands, after queuing what its schedules owe (same signature).
ALTER FUNCTION evolution.load_pending_admin_commands_v1(evolution.u64, evolution.u64)
RENAME TO load_pending_admin_commands_v64;
REVOKE ALL ON FUNCTION evolution.load_pending_admin_commands_v64(evolution.u64, evolution.u64)
FROM PUBLIC, :"runtime_role";
CREATE FUNCTION evolution.load_pending_admin_commands_v1(p_world evolution.u64,
p_region evolution.u64)
RETURNS TABLE (command_id bigint, kind integer, cell_count integer, target evolution.u64,
energy evolution.u64, carbon evolution.u64, nitrogen evolution.u64, phosphorus evolution.u64,
other evolution.u64)
LANGUAGE plpgsql VOLATILE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
BEGIN
PERFORM evolution.queue_due_schedules(p_world, p_region);
RETURN QUERY SELECT * FROM evolution.load_pending_admin_commands_v64(p_world, p_region);
END $$;
REVOKE ALL ON FUNCTION evolution.load_pending_admin_commands_v1(evolution.u64, evolution.u64)
FROM PUBLIC;
GRANT EXECUTE ON FUNCTION evolution.load_pending_admin_commands_v1(evolution.u64, evolution.u64)
TO :"runtime_role";
INSERT INTO evolution.schema_migrations(version,sha256) VALUES(65,:'migration_sha_65');
+20 -4
View File
@@ -140,6 +140,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
SELECT :'history_v61' || ',62:' || :'migration_sha_62' AS history_v62 \gset
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' = :'history_v22' AS at_v22,
:'history' = :'history_v23' AS at_v23,
:'history' = :'history_v24' AS at_v24,
@@ -182,8 +183,10 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
:'history' = :'history_v61' AS at_v61,
:'history' = :'history_v62' AS at_v62,
:'history' = :'history_v63' AS at_v63,
:'history' = :'history_v64' AS at_v64 \gset
\if :at_v64
:'history' = :'history_v64' AS at_v64,
:'history' = :'history_v65' AS at_v65 \gset
\if :at_v65
\elif :at_v64
\elif :at_v63
\elif :at_v62
\elif :at_v61
@@ -605,7 +608,8 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
coalesce(max(version), 0) = 61 AS at_v61,
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) = 64 AS at_v64,
coalesce(max(version), 0) = 65 AS at_v65
FROM evolution.schema_migrations \gset
\if :at_v24
\ir 0025_cellular_v2_bundle.sql
@@ -838,6 +842,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
\elif :at_v62
\elif :at_v63
\elif :at_v64
\elif :at_v65
\else
\echo 'Unknown or changed migration history after v43'
DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$;
@@ -926,6 +931,10 @@ SELECT coalesce(max(version), 0) = 63 AS needs_v64 FROM evolution.schema_migrati
\if :needs_v64
\ir 0064_account_admin.sql
\endif
SELECT coalesce(max(version), 0) = 64 AS needs_v65 FROM evolution.schema_migrations \gset
\if :needs_v65
\ir 0065_admin_schedules.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
@@ -1079,6 +1088,11 @@ GRANT EXECUTE ON FUNCTION evolution.admin_set_account_ban_v1(evolution.u64, uuid
GRANT EXECUTE ON FUNCTION evolution.admin_logout_account_v1(evolution.u64, uuid) TO :"admin_role";
GRANT EXECUTE ON FUNCTION evolution.admin_account_status_v1(evolution.u64) TO :"admin_role";
GRANT EXECUTE ON FUNCTION evolution.admin_account_bans_v1(uuid) TO :"admin_role";
GRANT EXECUTE ON FUNCTION 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) TO :"admin_role";
GRANT EXECUTE ON FUNCTION evolution.admin_schedules_v1(evolution.u64, evolution.u64)
TO :"admin_role";
SELECT pg_catalog.set_config('evolution.admin_role', :'admin_role', true);
DO $$
DECLARE
@@ -1107,7 +1121,9 @@ BEGIN
('evolution.admin_set_account_ban_v1(evolution.u64,uuid,boolean,text,text)'::regprocedure::oid),
('evolution.admin_logout_account_v1(evolution.u64,uuid)'::regprocedure::oid),
('evolution.admin_account_status_v1(evolution.u64)'::regprocedure::oid),
('evolution.admin_account_bans_v1(uuid)'::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)
), violations(reason) AS (
SELECT 'admin role is the runtime role' WHERE admin = runtime
UNION ALL
+97 -1
View File
@@ -276,7 +276,8 @@ OpsResponse AdminApi::handle(const ApiRequest& request, std::uint64_t now_second
if (request.method != "GET" &&
!(request.method == "POST" &&
((path.starts_with("/api/regions/") &&
(path.ends_with("/session-window") || path.ends_with("/commands"))) ||
(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"};
@@ -358,6 +359,15 @@ OpsResponse AdminApi::api(std::string_view method, std::string_view path, std::s
return error(404, "not_found");
return method == "POST" ? enqueue(*region, query, admin) : commands(*region);
}
constexpr std::string_view schedules_suffix = "/schedules";
if (path.starts_with(region_prefix) && path.ends_with(schedules_suffix) &&
path.size() > region_prefix.size() + schedules_suffix.size()) {
const auto region = number(path.substr(
region_prefix.size(), path.size() - region_prefix.size() - schedules_suffix.size()));
if (!region)
return error(404, "not_found");
return method == "POST" ? set_schedule(*region, query, admin) : schedules(*region);
}
return error(404, "not_found");
}
@@ -622,6 +632,92 @@ OpsResponse AdminApi::enqueue(std::uint64_t region, std::string_view query_text,
",\"target\":" + text_value(at(rows->front(), 1)) + "}\n");
}
OpsResponse AdminApi::schedules(std::uint64_t region) {
const auto rows =
query("SELECT schedule_id, kind, cell_count, energy, carbon, nitrogen, phosphorus, other, "
"interval_seconds, enabled, author_subject, reason, "
"floor(extract(epoch FROM changed_at))::bigint, "
"floor(extract(epoch FROM last_queued))::bigint, runs "
"FROM evolution.admin_schedules_v1($1, $2)",
{std::to_string(config_.world), std::to_string(region)});
if (!rows)
return error(503, "unavailable");
std::string out = "{\"region\":\"" + std::to_string(region) + "\",\"schedules\":[";
for (std::size_t i = 0; i < rows->size(); ++i) {
const auto& row = (*rows)[i];
const auto kind = number(at(row, 1).value_or("0")).value_or(0);
out +=
std::string(i == 0 ? "" : ",") + "{\"id\":" + text_value(at(row, 0)) + ",\"kind\":\"" +
std::string(kind < command_kinds.size() ? command_kinds[kind] : "") +
"\",\"count\":" + count_value(at(row, 2)) + ",\"amount\":[" + text_value(at(row, 3)) +
"," + text_value(at(row, 4)) + "," + text_value(at(row, 5)) + "," +
text_value(at(row, 6)) + "," + text_value(at(row, 7)) +
"],\"interval\":" + count_value(at(row, 8)) + ",\"enabled\":" + bool_value(at(row, 9)) +
",\"author\":" + text_value(at(row, 10)) + ",\"reason\":" + text_value(at(row, 11)) +
",\"changed_at\":" + seconds_value(at(row, 12)) +
",\"last_queued\":" + seconds_value(at(row, 13)) +
",\"runs\":" + count_value(at(row, 14)) + "}";
}
return json(200, out + "]}\n");
}
OpsResponse AdminApi::set_schedule(std::uint64_t region, std::string_view query_text,
const broker::AccessTokenClaims& admin) {
const auto kind_text = query_value(query_text, "kind", 20);
const auto kind =
kind_text ? std::ranges::find(command_kinds, *kind_text) : command_kinds.end();
if (!kind_text || kind_text->empty() || kind == command_kinds.end())
return error(400, "bad_kind");
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);
if (!text)
return query_text.find(std::string(name) + "=") == std::string_view::npos
? std::optional<std::uint64_t>{absent}
: std::nullopt;
return number(*text);
};
const std::array<std::optional<std::uint64_t>, 9> v{
value("id", 0), value("count", 0), value("energy", 0),
value("carbon", 0), value("nitrogen", 0), value("phosphorus", 0),
value("other", 0), value("interval", 0), value("enabled", 1)};
if (std::ranges::any_of(v, [](const auto& x) { return !x; }) || v[7].value_or(0) > 86'400 ||
v[8].value_or(2) > 1 || v[1].value_or(0) > 256)
return error(400, "bad_number");
const auto reason = query_value(query_text, "reason");
if (!reason && query_text.find("reason=") != std::string_view::npos)
return error(400, "bad_reason");
auto author = admin.email.empty() ? admin.subject : admin.email + " (" + admin.subject + ")";
if (author.size() > 255)
author = admin.subject;
std::vector<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()));
for (std::size_t i = 1; i <= 7; ++i)
parameters.push_back(std::to_string(v[i].value_or(0)));
parameters.push_back(v[8].value_or(1) == 1 ? "true" : "false");
parameters.push_back(author);
parameters.push_back(reason.value_or(""));
if (!db_ && connect_)
db_ = connect_();
if (!db_)
return error(503, "unavailable");
const auto rows =
db_->query("SELECT evolution.admin_set_schedule_v1($1, $2, $3, $4, $5, $6, $7, $8, $9, "
"$10, $11, $12::boolean, $13, $14)",
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_schedule" : "unavailable");
}
if (rows->empty())
return error(503, "unavailable");
return json(200, "{\"id\":" + text_value(at(rows->front(), 0)) + "}\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");
+43
View File
@@ -587,6 +587,49 @@ void admin_api_tests(const Realm& realm) {
CHECK(!reader->query("DELETE FROM evolution.admin_commands", {}, 5s));
}
// Resource schedules (schema 65): a due schedule queues one command, never a second
// while that one waits; the list shows the run; stopping writes a new version.
for (const std::string_view bad :
{"/api/regions/1/schedules?kind=add_pool&count=1&interval=5&energy=1",
"/api/regions/1/schedules?kind=add_bubbles&count=4&interval=0&energy=1",
"/api/regions/1/schedules?kind=add_bubbles&count=4&interval=90000&energy=1",
"/api/regions/1/schedules?kind=add_bubbles&count=4&interval=5"})
CHECK(post(bad).status == 400);
const auto made = must(JsonValue::parse(
post("/api/regions/1/schedules?kind=add_bubbles&count=4&interval=60&energy=100&"
"reason=feed")
.body));
const auto schedule_id = std::string(must(made.member("id")->string()));
const auto pending_now = [&] {
auto reader = connect_as(env("EVO_PG_RUNTIME_USER"));
const auto rows = reader->query(
"SELECT count(*) FROM evolution.load_pending_admin_commands_v1(9831, 1)", {}, 5s);
CHECK(rows && rows->size() == 1);
return std::string(*(*rows)[0][0]);
};
CHECK(pending_now() == "3" && pending_now() == "3");
const auto listed_schedules =
must(JsonValue::parse(get("/api/regions/1/schedules", admin).body));
const auto& schedule = listed_schedules.member("schedules")->array()->at(0);
CHECK(schedule.member("id")->string() == schedule_id &&
schedule.member("kind")->string() == "add_bubbles" &&
schedule.member("runs")->integer() == 1 &&
schedule.member("enabled")->boolean() == true &&
schedule.member("interval")->integer() == 60);
const auto queued_by_schedule =
must(JsonValue::parse(get("/api/regions/1/commands", admin).body));
CHECK(queued_by_schedule.member("commands")->array()->at(0).member("author")->string() ==
"schedule " + schedule_id);
CHECK(post("/api/regions/1/schedules?kind=add_bubbles&count=4&interval=60&energy=100&"
"enabled=0&id=" +
schedule_id)
.status == 200);
const auto stopped = must(JsonValue::parse(get("/api/regions/1/schedules", admin).body));
CHECK(stopped.member("schedules")->array()->size() == 1 &&
stopped.member("schedules")->array()->at(0).member("enabled")->boolean() == false);
CHECK(post("/api/regions/1/schedules?kind=add_bubbles&count=4&interval=60&energy=1&id=999999")
.status == 400);
// 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 &&
+2 -1
View File
@@ -230,6 +230,7 @@ void upgrade_across_migration_and_barrier() {
" -v migration_sha_62=$(s 0062_session_windows.sql)"
" -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 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");
@@ -247,7 +248,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");
"47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65");
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 = "
+9
View File
@@ -75,6 +75,15 @@ 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.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";
DROP FUNCTION evolution.queue_due_schedules(evolution.u64,evolution.u64);
DROP FUNCTION evolution.admin_schedules_v1(evolution.u64,evolution.u64);
DROP FUNCTION 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);
DROP TABLE evolution.admin_schedules;
DROP SEQUENCE evolution.admin_schedule_ids;
ALTER TABLE evolution.admin_commands DROP COLUMN schedule_id;
DROP FUNCTION evolution.release_account_control(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64);
ALTER FUNCTION evolution.release_account_control_v63(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64) RENAME TO release_account_control;
GRANT EXECUTE ON FUNCTION evolution.release_account_control(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64) TO :"runtime_role";
+5 -5
View File
@@ -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
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
# 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")" = 64: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
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(*) 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
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 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=65 WHERE version=64'
psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=66 WHERE version=65'
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=64 WHERE version=65"
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 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"
+3 -1
View File
@@ -144,6 +144,8 @@ digest_63="$(sha256sum "$root/migrations/0063_admin_commands.sql")"
digest_63="${digest_63%% *}"
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%% *}"
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" \
@@ -194,6 +196,6 @@ 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" "${admin[@]}" \
--set="migration_sha_64=$digest_64" --set="migration_sha_65=$digest_65" "${admin[@]}" \
--set="runtime_role=$runtime_role" \
--file="$root/migrations/apply.sql"