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

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:
dingo
2026-10-27 17:14:04 -04:00
co-authored by Claude Opus 5.5
parent b075198040
commit b1cc3b121b
32 changed files with 1487 additions and 138 deletions
+260 -94
View File
@@ -1344,103 +1344,268 @@ CellularLives::revamp_upgrade_candidate(const CellularRevampRules& rules, std::u
next.archive.archived() != 0 || count >= rules.organism_cap || count > 255 || first == 0 ||
first > cellular_bud_namespace - count)
return std::unexpected{CellularLivesError::invalid_config};
for (std::uint32_t index = 0; index < count; ++index) {
const auto id = first + index;
const auto founder = biology::primitive_founder(id, id, biology::primitive_founder_v6);
if (!founder)
return std::unexpected{CellularLivesError::biology};
biology::GenomeDraft draft{
.id = id,
.origin_event = id,
.development_version = 7,
.parents = {},
.capabilities = {founder->capabilities().begin(), founder->capabilities().end()},
.schema_version = biology::genome_schema_v7,
.movement_arrangement = biology::founder_movement_arrangement,
.cellular_traits = biology::founder_cellular_traits};
Random random{mutation_seed(world(), region(), id, 0x57494C44U)};
const auto points = [&] { return static_cast<std::uint16_t>(75 + random.next() % 51); };
auto& traits = *draft.cellular_traits;
traits.density = points();
traits.upkeep_efficiency = points();
traits.growth_speed = points();
for (auto& channel : traits.collection)
channel = points();
for (auto& channel : traits.burn)
channel = points();
traits.engulf_capacity = points();
traits.kill_absorption = points();
auto& movement = *draft.movement_arrangement;
movement.output = points();
movement.efficiency = points();
movement.steering = points();
movement.streamlining = points();
movement.braking = points();
movement.coasting = points();
const auto genome = biology::Genome::create(draft);
const auto budget =
genome ? biology::metabolic_budget(*genome, {1000, 0},
{next.config.ambient_temperature_millikelvin, {}},
1000, rules.body)
: std::unexpected{biology::DevelopmentError::overflow};
if (!budget)
return std::unexpected{CellularLivesError::biology};
// A normal founder's starting provision, evaluated for its randomized genome.
const accounting::Inventory reserve{
{budget->demand.value * biology::founder_reserve_seconds}, {}};
auto empty_body = accounting::Inventory{};
auto nursery = next.nursery;
auto heat = next.heat;
const auto development = biology::rules_for(genome->development_version());
if (!development || !subtract_inventory(nursery, reserve) ||
!rebody(empty_body, nursery, heat, budget->body,
development->construction_per_kilogram))
return std::unexpected{CellularLivesError::insufficient_funding};
const auto extent = budget->phenotype.extent;
if (next.config.area.width_um <= extent + 2000 ||
next.config.area.height_um <= extent + 2000)
return std::unexpected{CellularLivesError::no_placement};
std::optional<biology::CellularPosition> position;
for (int attempt = 0; attempt < 1024 && !position; ++attempt) {
const biology::CellularPosition spot{
1000 + random.next() % (next.config.area.width_um - extent - 2000),
1000 + random.next() % (next.config.area.height_um - extent - 2000)};
bool occupied = false;
for (const auto& other : next.lives) {
const auto body = budget_for(
next.archive, other, next.config.ambient_temperature_millikelvin, rules.body);
if (!body)
return std::unexpected{CellularLivesError::biology};
const auto side = body->phenotype.extent;
occupied = occupied || (spot.x_um < other.position.x_um + side &&
other.position.x_um < spot.x_um + extent &&
spot.y_um < other.position.y_um + side &&
other.position.y_um < spot.y_um + extent);
}
if (!occupied)
position = spot;
}
if (!position)
return std::unexpected{CellularLivesError::no_placement};
if (!next.archive.admit(*genome) || !next.archive.record_birth(id, id, {tick()}))
return std::unexpected{CellularLivesError::archive};
CellularOwnedLife life{};
life.identity = {id, id, id, id, {tick()}};
life.source = next.nursery_config.source;
life.growth = {1000, 0};
life.position = *position;
life.body = budget->body;
life.reserve = reserve;
life.offspring_genome = id;
next.lives.push_back(life);
next.nursery = nursery;
next.nursery_config.funding = nursery;
next.heat = heat;
}
for (std::uint32_t index = 0; index < count; ++index)
if (const auto error =
seed_wildlife_root(next, world(), region(), tick(), first + index, nullptr))
return std::unexpected{*error};
// Restore validates bodies, identities, history and channel-by-channel closure.
return restore((*candidate)->snapshot(), world(), region(), frame(), tick());
}
std::optional<CellularLivesError>
CellularLives::seed_wildlife_root(State& next, std::uint64_t world, std::uint64_t region,
std::uint64_t tick, std::uint64_t id,
accounting::Inventory* imported) {
const auto& rules = next.revamp;
const auto founder = biology::primitive_founder(id, id, biology::primitive_founder_v6);
if (!founder)
return CellularLivesError::biology;
biology::GenomeDraft draft{
.id = id,
.origin_event = id,
.development_version = 7,
.parents = {},
.capabilities = {founder->capabilities().begin(), founder->capabilities().end()},
.schema_version = biology::genome_schema_v7,
.movement_arrangement = biology::founder_movement_arrangement,
.cellular_traits = biology::founder_cellular_traits};
Random random{mutation_seed(world, region, id, 0x57494C44U)};
const auto points = [&] { return static_cast<std::uint16_t>(75 + random.next() % 51); };
auto& traits = *draft.cellular_traits;
traits.density = points();
traits.upkeep_efficiency = points();
traits.growth_speed = points();
for (auto& channel : traits.collection)
channel = points();
for (auto& channel : traits.burn)
channel = points();
traits.engulf_capacity = points();
traits.kill_absorption = points();
auto& movement = *draft.movement_arrangement;
movement.output = points();
movement.efficiency = points();
movement.steering = points();
movement.streamlining = points();
movement.braking = points();
movement.coasting = points();
const auto genome = biology::Genome::create(draft);
const auto budget =
genome ? biology::metabolic_budget(*genome, {1000, 0},
{next.config.ambient_temperature_millikelvin, {}}, 1000,
rules.body)
: std::unexpected{biology::DevelopmentError::overflow};
if (!budget)
return CellularLivesError::biology;
// A normal founder's starting provision, evaluated for its randomized genome.
const accounting::Inventory reserve{{budget->demand.value * biology::founder_reserve_seconds},
{}};
auto empty_body = accounting::Inventory{};
const auto development = biology::rules_for(genome->development_version());
if (!development)
return CellularLivesError::insufficient_funding;
// ADR-0182: an import pays exactly the reserve, the body's matter and its construction.
auto funding = next.nursery;
if (imported) {
std::uint64_t added{};
funding = reserve;
for (std::size_t c = 0; c < funding.matter.size(); ++c) {
funding.matter[c] = budget->body.matter[c];
added += budget->body.matter[c].value;
}
const auto price = development->construction_per_kilogram;
funding.energy.value += added / 1'000 * price + added % 1'000 * price / 1'000;
}
const auto paid = funding;
auto heat = next.heat;
if (!subtract_inventory(funding, reserve) ||
!rebody(empty_body, funding, heat, budget->body, development->construction_per_kilogram))
return CellularLivesError::insufficient_funding;
const auto extent = budget->phenotype.extent;
if (next.config.area.width_um <= extent + 2000 || next.config.area.height_um <= extent + 2000)
return CellularLivesError::no_placement;
std::optional<biology::CellularPosition> position;
for (int attempt = 0; attempt < 1024 && !position; ++attempt) {
const biology::CellularPosition spot{
1000 + random.next() % (next.config.area.width_um - extent - 2000),
1000 + random.next() % (next.config.area.height_um - extent - 2000)};
bool occupied = false;
for (const auto& other : next.lives) {
const auto body = budget_for(next.archive, other,
next.config.ambient_temperature_millikelvin, rules.body);
if (!body)
return CellularLivesError::biology;
const auto side = body->phenotype.extent;
occupied = occupied || (spot.x_um < other.position.x_um + side &&
other.position.x_um < spot.x_um + extent &&
spot.y_um < other.position.y_um + side &&
other.position.y_um < spot.y_um + extent);
}
if (!occupied)
position = spot;
}
if (!position)
return CellularLivesError::no_placement;
if (!next.archive.admit(*genome) || !next.archive.record_birth(id, id, {tick}))
return CellularLivesError::archive;
CellularOwnedLife life{};
life.identity = {id, id, id, id, {tick}};
life.source = next.nursery_config.source;
life.growth = {1000, 0};
life.position = *position;
life.body = budget->body;
life.reserve = reserve;
life.offspring_genome = id;
const auto at = std::ranges::lower_bound(next.lives, id, {},
[](const auto& value) { return value.identity.life; });
next.lives.insert(at, life);
if (imported) {
if (funding != accounting::Inventory{} || !add_inventory(*imported, paid))
return CellularLivesError::conservation;
} else {
next.nursery = funding;
next.nursery_config.funding = funding;
}
next.heat = heat;
return std::nullopt;
}
bool cellular_admin_command_valid(const CellularAdminCommand& c) noexcept {
const bool some = c.amount != accounting::Inventory{};
switch (c.kind) {
case CellularAdminKind::remove_cell:
return c.id != 0 && c.target != 0 && c.count == 0 && !some;
case CellularAdminKind::grant_reserve:
return c.id != 0 && c.target != 0 && c.count == 0 && some;
case CellularAdminKind::add_bubbles:
case CellularAdminKind::release_pool:
return c.id != 0 && c.target == 0 && c.count >= 1 && c.count <= 256 && some;
case CellularAdminKind::add_pool:
return c.id != 0 && c.target == 0 && c.count == 0 && some;
case CellularAdminKind::spawn_wildlife:
return c.id != 0 && c.count >= 1 && c.count <= 64 && !some && c.target != 0 &&
c.target <= cellular_bud_namespace - c.count;
}
return false;
}
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
CellularLives::admin_candidate(std::span<const CellularAdminCommand> commands,
std::vector<CellularAdminResult>* results) const {
if (state_->format_version != cellular_lives_revamp_format ||
state_->rules_version < cellular_lives_revamp_rules)
return std::unexpected{CellularLivesError::version};
for (std::size_t i = 0; i < commands.size(); ++i)
if (!cellular_admin_command_valid(commands[i]) ||
(i != 0 && commands[i - 1].id >= commands[i].id))
return std::unexpected{CellularLivesError::invalid_config};
auto candidate = restore(snapshot(), world(), region(), frame(), tick());
if (!candidate)
return std::unexpected(candidate.error());
std::vector<CellularAdminResult> report;
for (const auto& command : commands) {
// Each command works on a copy, kept only when it applies and still closes.
auto attempt = restore((*candidate)->snapshot(), world(), region(), frame(), tick());
if (!attempt)
return std::unexpected(attempt.error());
auto& next = *(*attempt)->state_;
bool applied = false;
const auto life = std::ranges::lower_bound(next.lives, command.target, {},
[](const auto& v) { return v.identity.life; });
const bool found = life != next.lives.end() && life->identity.life == command.target;
switch (command.kind) {
case CellularAdminKind::remove_cell:
if (found && next.archive.record_death(life->identity.organism, {tick()})) {
auto single = *life;
single.revamp.members = 1; // A corpse records the single-cell extent.
const auto extent =
budget_for(next.archive, single, next.config.ambient_temperature_millikelvin,
next.revamp.body);
if (extent && add_inventory(next.spent, life->body) &&
add_inventory(next.spent, life->reserve)) {
CellularOwnedCorpse corpse{life->identity.life,
life->identity.organism,
life->source,
tick(),
life->position,
life->growth,
extent->phenotype.extent,
{},
biology::CellularDeathCause::removed,
0,
life->parent_life,
life->offspring_genome};
const auto at = std::ranges::lower_bound(next.corpses, corpse.organism, {},
&CellularOwnedCorpse::organism);
next.corpses.insert(at, corpse);
next.lives.erase(life);
applied = true;
}
}
break;
case CellularAdminKind::grant_reserve:
applied = found && add_inventory(life->reserve, command.amount) &&
add_inventory(next.boundary.incoming, command.amount);
break;
case CellularAdminKind::add_pool:
applied = add_inventory(next.spent, command.amount) &&
add_inventory(next.boundary.incoming, command.amount);
break;
case CellularAdminKind::add_bubbles:
case CellularAdminKind::release_pool: {
const bool release = command.kind == CellularAdminKind::release_pool;
auto amount = command.amount;
if (release) // Never more than the pool holds.
for (std::size_t c = 0; c < 5; ++c) {
auto& want = c == 0 ? amount.energy.value : amount.matter[c - 1].value;
const auto have =
c == 0 ? next.spent.energy.value : next.spent.matter[c - 1].value;
want = std::min(want, have);
}
auto config = next.habitat->config();
if (amount != accounting::Inventory{} &&
(release ? subtract_inventory(next.spent, amount)
: add_inventory(next.boundary.incoming, amount)) &&
cellular_scatter_bubbles(config, amount, command.count,
mutation_seed(world(), region(), command.id, tick()),
next.nursery_config.source)) {
if (auto habitat = biology::CellularHabitat::create(std::move(config))) {
next.habitat = std::move(*habitat);
applied = true;
}
}
break;
}
case CellularAdminKind::spawn_wildlife: {
if (next.lives.size() + command.count >= next.revamp.organism_cap)
break;
accounting::Inventory imported{};
bool ok = true;
for (std::uint16_t i = 0; ok && i < command.count; ++i) {
const auto id = command.target + i;
ok = !std::ranges::binary_search(next.lives, id, {},
[](const auto& v) { return v.identity.life; }) &&
!next.archive.find(id) &&
!seed_wildlife_root(next, world(), region(), tick(), id, &imported);
}
applied = ok && add_inventory(next.boundary.incoming, imported);
break;
}
}
if (applied) {
// Restore checks bodies, identities, history and channel-by-channel closure.
auto checked = restore((*attempt)->snapshot(), world(), region(), frame(), tick());
if (checked)
candidate = std::move(checked);
else
applied = false;
}
report.push_back({command.id, applied});
}
if (results)
*results = std::move(report);
return std::move(*candidate);
}
std::optional<accounting::Inventory> CellularLives::budding_cost(std::uint64_t id) const {
const auto life =
std::ranges::find(state_->lives, id, [](const auto& value) { return value.identity.life; });
@@ -2354,7 +2519,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
const bool engulfed = corpse.cause == biology::CellularDeathCause::engulfed;
const bool revamp_cause =
revamp_version && (corpse.cause == biology::CellularDeathCause::merged ||
corpse.cause == biology::CellularDeathCause::emerged);
corpse.cause == biology::CellularDeathCause::emerged ||
corpse.cause == biology::CellularDeathCause::removed);
if (padding != 0 ||
(corpse.cause != biology::CellularDeathCause::starved && !engulfed &&
!revamp_cause) ||