feat(admin): live admin commands in trace 5 (schema 63)
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / postgres-tools (push) Successful in 30s
Foundation / server-image (push) Successful in 4m44s
Foundation / image (push) Successful in 5m33s
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / postgres-tools (push) Successful in 30s
Foundation / server-image (push) Successful in 4m44s
Foundation / image (push) Successful in 5m33s
The admin portal can now spawn wildlife, add bubbles, add to or release the spent pool, remove a cell and grant a cell's reserve on a running rules-13 region (ADR-0182 section 10). No new owner format or region opening. - Trace 5: trace 4 plus 64-byte command records counted in the tick header's last word; a batch without commands stays trace 4 (older builds replay it). Bundle 15 admits trace 5. - CellularLives::admin_candidate applies a tick's commands after its births, each on a copy kept only when it applies and closes: imports through the boundary ledger, removals into the spent pool with a new `removed` corpse cause, spawned roots via the ADR-0180 seed (now a shared helper) funded by an exact import. Refused commands change nothing. - Schema 63: the immutable admin_commands queue (spawn identities from the life allocator), pending/history reads, and binding of every recorded command exactly once inside both cellular publishers; the SQL trace parsers read trace 5 through its trace-4 normal form. - The region polls pending commands every 2 s and appends up to 16 per tick; the admin API and portal gain region actions, per-cell Remove/Grant and the command history. - Fixes an existing test that did not advance its head after a tick commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -209,7 +209,9 @@ void version_checks(const CellularLives& base, const CellularBatch& batch) {
|
||||
"action matcher admitted new trace on old owner");
|
||||
empty.format_version = cellular_batch_format;
|
||||
require(replay_cellular_batch(*old, empty).has_value(), "old replay control failed");
|
||||
empty.format_version = 5;
|
||||
empty.format_version = 5; // ADR-0182 trace 5 needs a rules-13 owner.
|
||||
refuse(*old, empty, CellularBatchError::version);
|
||||
empty.format_version = 6;
|
||||
refuse(*old, empty, CellularBatchError::version);
|
||||
require(!encode_cellular_batch(empty), "unknown trace encoded");
|
||||
auto scope = batch;
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#endif
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
@@ -1492,9 +1493,100 @@ void run_starting_wildlife_publication_tests() {
|
||||
auto tick = publish_cellular_batch(*runtime, **objects, *lease, head, batch, *recovered->state,
|
||||
identity(8), build, 5s);
|
||||
CHECK(tick);
|
||||
head = tick->committed;
|
||||
recovered = recover_cellular_checkpoint(*runtime, **objects, key, *lease, builds, 5s);
|
||||
CHECK(recovered && recovered->state->snapshot() == tick_bytes &&
|
||||
recovered->state->lives().size() == 32);
|
||||
|
||||
// ADR-0182 (schema 63): queued admin commands publish in trace 5 and bind once.
|
||||
CHECK(owner->query("INSERT INTO evolution.life_allocators(world_id, next_identity) "
|
||||
"VALUES ($1, 5000000) ON CONFLICT DO NOTHING",
|
||||
std::array{std::to_string(key.world)}, 5s));
|
||||
const auto enqueue = [&](int kind, int count, std::uint64_t target, std::uint64_t energy) {
|
||||
const auto rows = owner->query(
|
||||
"SELECT command_id, target FROM evolution.admin_enqueue_command_v1($1, $2, $3, $4, "
|
||||
"$5, $6, 0, 0, 0, 0, 'ops', 'test')",
|
||||
std::array{std::to_string(key.world), std::to_string(key.region), std::to_string(kind),
|
||||
std::to_string(count), std::to_string(target), std::to_string(energy)},
|
||||
5s);
|
||||
CHECK(rows && rows->size() == 1);
|
||||
return std::pair{std::stoull(*(*rows)[0][0]), std::stoull(*(*rows)[0][1])};
|
||||
};
|
||||
CHECK(!owner->query("SELECT * FROM evolution.admin_enqueue_command_v1($1, $2, 4, 0, 0, 0, 0, "
|
||||
"0, 0, 0, 'ops', '')",
|
||||
std::array{std::to_string(key.world), std::to_string(key.region)}, 5s));
|
||||
owner = connect_as(env("PGUSER"));
|
||||
const auto victim = recovered->state->lives().front().identity.life;
|
||||
const auto [pool_id, pool_target] = enqueue(4, 0, 0, 1'000'000'000);
|
||||
const auto [spawn_id, first_wild] = enqueue(6, 2, 0, 0);
|
||||
const auto [remove_id, remove_target] = enqueue(1, 0, victim, 0);
|
||||
CHECK(pool_target == 0 && first_wild == 5'000'000 && remove_target == victim);
|
||||
using Kind = evo::server::CellularAdminKind;
|
||||
const std::vector<evo::server::CellularAdminCommand> commands{
|
||||
{static_cast<std::uint64_t>(pool_id), Kind::add_pool, 0, 0, {{1'000'000'000}, {}}},
|
||||
{static_cast<std::uint64_t>(spawn_id), Kind::spawn_wildlife, 2, first_wild, {}},
|
||||
{static_cast<std::uint64_t>(remove_id), Kind::remove_cell, 0, victim, {}}};
|
||||
const auto pending = [&] {
|
||||
const auto rows =
|
||||
runtime->query("SELECT count(*) FROM evolution.load_pending_admin_commands_v1($1, $2)",
|
||||
std::array{std::to_string(key.world), std::to_string(key.region)}, 5s);
|
||||
CHECK(rows && rows->size() == 1);
|
||||
return std::stoull(*(*rows)[0][0]);
|
||||
};
|
||||
CHECK(pending() == 3);
|
||||
const auto admin_batch = [&](std::vector<evo::server::CellularAdminCommand> recorded) {
|
||||
CellularBatch made{{key.world, key.region, 1, head.head.commit, head.head.digest, 2, 3},
|
||||
{{}}};
|
||||
made.format_version = evo::server::cellular_batch_admin_format;
|
||||
made.ticks.front().lives = checked_value(wildlife_intents(*recovered->state, 3));
|
||||
made.ticks.front().admin = std::move(recorded);
|
||||
return made;
|
||||
};
|
||||
// A command that differs from its queue row is refused and the head stays.
|
||||
auto tampered = commands;
|
||||
tampered.front().amount.energy.value = 2'000'000'000;
|
||||
auto wrong = admin_batch(tampered);
|
||||
auto wrong_base = evo::fixture::restore(*recovered->state);
|
||||
CHECK(wrong_base->advance_tick(wrong.ticks.front().lives));
|
||||
auto wrong_state = wrong_base->admin_candidate(tampered);
|
||||
CHECK(wrong_state);
|
||||
CHECK(!publish_cellular_batch(*runtime, **objects, *lease, head, wrong, **wrong_state,
|
||||
identity(9), build, 5s));
|
||||
runtime = connect_as(env("EVO_PG_RUNTIME_USER"));
|
||||
CHECK(pending() == 3);
|
||||
// The exact commands publish, bind and recover.
|
||||
auto admin = admin_batch(commands);
|
||||
CHECK(recovered->state->advance_tick(admin.ticks.front().lives));
|
||||
std::vector<evo::server::CellularAdminResult> results;
|
||||
auto applied = recovered->state->admin_candidate(commands, &results);
|
||||
CHECK(applied && results.size() == 3 &&
|
||||
std::ranges::all_of(results, [](auto r) { return r.applied; }));
|
||||
const auto applied_bytes = (*applied)->snapshot();
|
||||
auto published = publish_cellular_batch(*runtime, **objects, *lease, head, admin, **applied,
|
||||
identity(10), build, 5s);
|
||||
CHECK(published);
|
||||
head = published->committed;
|
||||
CHECK(pending() == 0);
|
||||
const auto bound = owner->query(
|
||||
"SELECT count(*), min(tick), max(tick) FROM evolution.admin_command_applications "
|
||||
"WHERE command_id IN ($1, $2, $3)",
|
||||
std::array{std::to_string(pool_id), std::to_string(spawn_id), std::to_string(remove_id)},
|
||||
5s);
|
||||
CHECK(bound && (*bound)[0][0] == "3" && (*bound)[0][1] == "3" && (*bound)[0][2] == "3");
|
||||
recovered = recover_cellular_checkpoint(*runtime, **objects, key, *lease, builds, 5s);
|
||||
CHECK(recovered && recovered->state->snapshot() == applied_bytes &&
|
||||
recovered->state->lives().size() == 33);
|
||||
// The same commands cannot bind to another commit.
|
||||
auto again = admin_batch(commands);
|
||||
again.header = {key.world, key.region, 1, head.head.commit, head.head.digest, 3, 4};
|
||||
again.ticks.front().lives = checked_value(wildlife_intents(*recovered->state, 4));
|
||||
auto again_base = evo::fixture::restore(*recovered->state);
|
||||
CHECK(again_base->advance_tick(again.ticks.front().lives));
|
||||
auto again_state = again_base->admin_candidate(commands);
|
||||
CHECK(again_state);
|
||||
CHECK(!publish_cellular_batch(*runtime, **objects, *lease, head, again, **again_state,
|
||||
identity(11), build, 5s));
|
||||
runtime = connect_as(env("EVO_PG_RUNTIME_USER"));
|
||||
CHECK(release_region_lease(*runtime, key, *lease, 5s));
|
||||
std::puts("starting wildlife publication and replacement-owner replay passed");
|
||||
}
|
||||
|
||||
@@ -874,6 +874,100 @@ void trace_replays() {
|
||||
"Emerge did not survive the trace");
|
||||
}
|
||||
|
||||
// ADR-0182: operator commands import through the boundary ledger, remove into the spent pool,
|
||||
// spawn funded wildlife, refuse what does not apply, and replay exactly from trace 5.
|
||||
void admin_commands_apply_and_replay() {
|
||||
using Kind = CellularAdminKind;
|
||||
auto live = revamp_region();
|
||||
const auto base = restore(*live);
|
||||
accounting::Inventory pool{{5'000'000'000}, {}};
|
||||
accounting::Inventory bubbles{{400'000'000}, {}};
|
||||
bubbles.matter[0].value = 4'000;
|
||||
const std::vector<CellularAdminCommand> commands{
|
||||
{1, Kind::add_pool, 0, 0, pool},
|
||||
{2, Kind::add_bubbles, 4, 0, bubbles},
|
||||
{3, Kind::grant_reserve, 0, 100, {{1'000'000}, {}}},
|
||||
{4, Kind::spawn_wildlife, 3, 900'000, {}},
|
||||
{5, Kind::remove_cell, 0, 101, {}},
|
||||
{6, Kind::release_pool, 2, 0, {{1'000'000'000}, {}}},
|
||||
{7, Kind::remove_cell, 0, 424'242, {}}, // No such life: refused, nothing changes.
|
||||
};
|
||||
for (const auto& bad : {CellularAdminCommand{8, Kind::add_pool, 0, 0, {}},
|
||||
CellularAdminCommand{8, Kind::spawn_wildlife, 65, 900'000, {}},
|
||||
CellularAdminCommand{8, Kind::add_bubbles, 0, 0, pool},
|
||||
CellularAdminCommand{0, Kind::add_pool, 0, 0, pool}})
|
||||
require(!cellular_admin_command_valid(bad), "an invalid command shape was accepted");
|
||||
require(!live->admin_candidate(std::vector{commands[1], commands[0]}),
|
||||
"commands out of order were accepted");
|
||||
|
||||
// The live run: one tick, then the commands after it.
|
||||
const auto intents = swim(*live, {});
|
||||
checked(live->advance_tick(intents), "tick before the commands");
|
||||
const auto spent_before = live->spent();
|
||||
const auto deposits_before = live->habitat().patches().size();
|
||||
const auto removed_body = life_of(*live, 101).body;
|
||||
const auto removed_reserve = life_of(*live, 101).reserve;
|
||||
std::vector<CellularAdminResult> results;
|
||||
auto after = checked(live->admin_candidate(commands, &results), "commands apply");
|
||||
require(results.size() == 7 &&
|
||||
std::ranges::all_of(results.begin(), results.begin() + 6,
|
||||
[](const auto& r) { return r.applied; }) &&
|
||||
!results.back().applied,
|
||||
"unexpected command results");
|
||||
closed(*after);
|
||||
require(after->lives().size() == live->lives().size() - 1 + 3, "wrong cell count");
|
||||
std::size_t wildlife = 0;
|
||||
for (const auto& life : after->lives())
|
||||
if (life.account == LifeAccount{} && life.identity.life >= 900'000 &&
|
||||
life.identity.life < 900'003)
|
||||
++wildlife;
|
||||
require(wildlife == 3, "spawned wildlife missing");
|
||||
require(life_of(*after, 100).reserve.energy.value ==
|
||||
life_of(*live, 100).reserve.energy.value + 1'000'000,
|
||||
"the grant did not reach the reserve");
|
||||
const auto corpse = std::ranges::find(after->corpses(), 101ULL, &CellularOwnedCorpse::life_id);
|
||||
require(corpse != after->corpses().end() &&
|
||||
corpse->cause == biology::CellularDeathCause::removed,
|
||||
"the removed cell left no removed corpse");
|
||||
auto expected_spent = spent_before;
|
||||
expected_spent.energy.value += pool.energy.value - 1'000'000'000;
|
||||
for (const auto& part : {removed_body, removed_reserve}) {
|
||||
expected_spent.energy.value += part.energy.value;
|
||||
for (std::size_t c = 0; c < 4; ++c)
|
||||
expected_spent.matter[c].value += part.matter[c].value;
|
||||
}
|
||||
require(after->spent() == expected_spent, "the spent pool is off");
|
||||
require(after->habitat().patches().size() == deposits_before + 4 + 2, "bubbles were not added");
|
||||
require(restore(*after)->snapshot() == after->snapshot(), "commands do not restore");
|
||||
auto again = checked(live->admin_candidate(commands), "commands apply again");
|
||||
require(again->snapshot() == after->snapshot(), "commands are not deterministic");
|
||||
|
||||
// The same tick and commands from trace 5 replay to the same state.
|
||||
CellularBatch batch;
|
||||
batch.format_version = cellular_batch_admin_format;
|
||||
batch.header = {base->world(), base->region(), base->frame(), {}, {}, base->tick(), 0};
|
||||
batch.header.predecessor.fill(1);
|
||||
batch.header.predecessor_digest.fill(2);
|
||||
batch.ticks.push_back({intents, {}, {}, commands});
|
||||
batch.header.end_tick = base->tick() + 1;
|
||||
const auto bytes = checked(encode_cellular_batch(batch), "trace 5 encodes");
|
||||
const auto decoded = checked(decode_cellular_batch(bytes), "trace 5 decodes");
|
||||
require(decoded == batch, "trace 5 did not round-trip");
|
||||
std::vector<CellularMutationOutcome> outcomes;
|
||||
std::vector<CellularAdminResult> replayed_results;
|
||||
const auto replayed = checked(replay_cellular_batch(*base, decoded, outcomes, replayed_results),
|
||||
"trace 5 replays");
|
||||
require(replayed->snapshot() == after->snapshot() && replayed_results == results,
|
||||
"trace 5 replay diverged");
|
||||
// Trace 4 cannot carry commands, and older owners cannot replay trace 5.
|
||||
auto old_format = batch;
|
||||
old_format.format_version = cellular_batch_inheritance_format;
|
||||
require(!encode_cellular_batch(old_format), "trace 4 carried commands");
|
||||
auto mangled = bytes;
|
||||
mangled[8] = 4; // The version byte: a trace-4 header with nonzero command counts.
|
||||
require(!decode_cellular_batch(mangled), "a trace 4 with commands decoded");
|
||||
}
|
||||
|
||||
// The safety revision preserves all five channels, follows both control choices,
|
||||
// separates eligible predator/prey siblings and replays their actual motion exactly.
|
||||
void safety_split(bool become) {
|
||||
@@ -1032,6 +1126,7 @@ void safe_bud_provision(bool become) {
|
||||
} // namespace
|
||||
|
||||
int main() try {
|
||||
admin_commands_apply_and_replay();
|
||||
safety_split(false);
|
||||
safety_split(true);
|
||||
safe_bud_provision(false);
|
||||
|
||||
@@ -550,6 +550,43 @@ void admin_api_tests(const Realm& realm) {
|
||||
CHECK(!owner->query(
|
||||
"SELECT * FROM evolution.admin_set_session_window_v1(9831, 1, 14401, 'x', '')", {}, 5s));
|
||||
|
||||
// Admin commands (schema 63): queued with the admin's identity, shape-checked, listed.
|
||||
for (const std::string_view bad :
|
||||
{"/api/regions/1/commands", "/api/regions/1/commands?kind=explode",
|
||||
"/api/regions/1/commands?kind=add_pool", "/api/regions/1/commands?kind=add_pool&energy=x",
|
||||
"/api/regions/1/commands?kind=remove_cell&target=7&energy=5",
|
||||
"/api/regions/1/commands?kind=spawn_wildlife&count=65"})
|
||||
CHECK(post(bad).status == 400);
|
||||
const auto queued =
|
||||
must(JsonValue::parse(post("/api/regions/1/commands?kind=add_pool&energy=1000&carbon=7"
|
||||
"&reason=top+up")
|
||||
.body));
|
||||
CHECK(queued.member("id") && queued.member("target")->string() == "0");
|
||||
CHECK(post("/api/regions/1/commands?kind=remove_cell&target=424242").status == 200);
|
||||
CHECK(post("/api/regions/2/commands?kind=add_pool&energy=1").status == 400);
|
||||
const auto listed_commands = must(JsonValue::parse(get("/api/regions/1/commands", admin).body));
|
||||
const auto& newest = listed_commands.member("commands")->array()->at(0);
|
||||
const auto& older = listed_commands.member("commands")->array()->at(1);
|
||||
CHECK(newest.member("kind")->string() == "remove_cell" &&
|
||||
newest.member("target")->string() == "424242" &&
|
||||
newest.member("applied_tick")->kind() == JsonValue::Kind::null &&
|
||||
older.member("kind")->string() == "add_pool" &&
|
||||
older.member("amount")->array()->at(0).string() == "1000" &&
|
||||
older.member("amount")->array()->at(1).string() == "7" &&
|
||||
older.member("reason")->string() == "top up" &&
|
||||
older.member("author")->string() == "ops@example.test (ops)");
|
||||
{
|
||||
auto reader = connect_as(env("EVO_PG_RUNTIME_USER"));
|
||||
const auto pending = reader->query(
|
||||
"SELECT count(*) FROM evolution.load_pending_admin_commands_v1(9831, 1)", {}, 5s);
|
||||
CHECK(pending && (*pending)[0][0] == "2");
|
||||
CHECK(!reader->query("SELECT * FROM evolution.admin_enqueue_command_v1(9831, 1, 4, 0, 0, "
|
||||
"1, 0, 0, 0, 0, 'x', '')",
|
||||
{}, 5s));
|
||||
reader = connect_as(env("PGUSER"));
|
||||
CHECK(!reader->query("DELETE FROM evolution.admin_commands", {}, 5s));
|
||||
}
|
||||
|
||||
// The read views are the only way in: the runtime role still cannot read the tables.
|
||||
runtime = connect_as(env("EVO_PG_RUNTIME_USER"));
|
||||
CHECK(!runtime->query("SELECT count(*) FROM evolution.life_reservations", {}, 5s));
|
||||
|
||||
@@ -228,6 +228,7 @@ void upgrade_across_migration_and_barrier() {
|
||||
" -v migration_sha_60=$(s 0060_cellular_revamp_bundle.sql)"
|
||||
" -v migration_sha_61=$(s 0061_admin_read_views.sql)"
|
||||
" -v migration_sha_62=$(s 0062_session_windows.sql)"
|
||||
" -v migration_sha_63=$(s 0063_admin_commands.sql)"
|
||||
" -v runtime_role=evo_runtime_test -f migrations/apply.sql"
|
||||
" > /dev/null 2>&1") != 0);
|
||||
CHECK(history(*owner_db) == "1,2,3,4,5,6,7");
|
||||
@@ -245,7 +246,7 @@ void upgrade_across_migration_and_barrier() {
|
||||
"tools/migrate-postgres.sh\" > /dev/null") == 0);
|
||||
CHECK(history(*owner_db) == "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,"
|
||||
"25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,"
|
||||
"47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62");
|
||||
"47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63");
|
||||
CHECK(scalar(*owner_db, "SELECT (c.condeferrable AND c.condeferred AND "
|
||||
"c.confrelid = 'evolution.region_commits'::regclass)::text "
|
||||
"FROM pg_catalog.pg_constraint c WHERE c.conrelid = "
|
||||
|
||||
Reference in New Issue
Block a user