Files
aevolution/src/server/lifecycle/cellular_admin_view.cpp
T
dingoandClaude Opus 5.5 3c753970c8
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / postgres-tools (push) Successful in 31s
Foundation / server-image (push) Successful in 4m29s
Foundation / image (push) Successful in 5m43s
feat(cellular): region ticks of up to 4,096 lives for testing (schema 68)
ADR-0182 section 17. The plan's 2,000-organism cap became the rules-12
key region.organism_cap, which was parsed but never enforced; the real
bound was max_cellular_tick_lives = 256 (ADR-0106), which rules 13's
organism cap could not exceed. The bound is now 4,096, for testing limits
only; the DEV tuning file keeps 256.

- max_cellular_tick_lives (4,096) bounds the raw region path: step, owner
  state, batch trace (cellular_batch_bytes_for) and current checkpoint
  traces. max_cellular_frozen_lives (256) keeps cohort populations,
  population batches, migration traces and pre-v3 cellular traces, whose
  database checks are fixed.
- Tuning: reproduction.organism_cap and wildlife_cap accept up to 4,096.
- Schema 68: the trace checker under the current publishers
  (cellular_births_v45) accepts 4,096 intents, births and mutations per
  tick; spawn_wildlife takes up to 256 cells a command.
- Region view and portal: the lineage archive's fill (births of 65,536,
  genomes of 4,096), at which a region stops ticking.
- PostgreSQL suite: 256 wildlife spawned in one command, then a 289-life
  tick published and recovered exactly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 17:30:22 -04:00

228 lines
8.0 KiB
C++

#include "evo/server/lifecycle/cellular_admin_view.hpp"
#include <algorithm>
#include <array>
#include <cstdint>
#include <string_view>
#include <vector>
namespace evo::server {
namespace {
void append(std::string& out, std::uint64_t value) {
out += std::to_string(value);
}
void append_inventory(std::string& out, const accounting::Inventory& inventory) {
append(out, inventory.energy.value);
for (const auto& channel : inventory.matter) {
out.push_back(',');
append(out, channel.value);
}
}
// Energy then the four matter channels, summed without the inventory's overflow checks: a
// region's stock is far below 2^64 in every channel.
using Totals = std::array<std::uint64_t, 5>;
void add(Totals& totals, const accounting::Inventory& inventory) {
totals[0] += inventory.energy.value;
for (std::size_t i = 0; i < inventory.matter.size(); ++i)
totals[i + 1] += inventory.matter[i].value;
}
void append_totals(std::string& out, std::string_view name, const Totals& totals) {
out += "\"";
out += name;
out += "\":[";
for (std::size_t i = 0; i < totals.size(); ++i) {
if (i != 0)
out.push_back(',');
append(out, totals[i]);
}
out.push_back(']');
}
} // namespace
std::string cellular_account_text(const LifeAccount& account) {
constexpr std::string_view digits = "0123456789abcdef";
std::string out;
out.reserve(36);
for (std::size_t i = 0; i < account.size(); ++i) {
if (i == 4 || i == 6 || i == 8 || i == 10)
out.push_back('-');
out.push_back(digits[account[i] >> 4U]);
out.push_back(digits[account[i] & 0x0FU]);
}
return out;
}
std::string render_cellular_admin_view(const CellularLives& state,
const CellularAdminOnline& online,
CellularAdminViewLimits limits,
std::string_view rules_sha256) {
const auto lives = state.lives();
const auto patches = state.habitat().patches();
std::string out;
out.reserve(256 + std::min(lives.size(), limits.cells) * 128 +
std::min(patches.size(), limits.deposits) * 64);
out += "{\"world\":";
append(out, state.world());
out += ",\"region\":";
append(out, state.region());
out += ",\"tick\":";
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(',');
append(out, state.area().height_um);
out += "],\"spent\":[";
append_inventory(out, state.spent());
out += "],\"heat\":";
append(out, state.heat().energy.value);
// The lineage archive never forgets: births and genomes recorded against its limits, at
// which the region stops ticking.
out += ",\"archive\":[";
append(out, state.archive().recorded());
out.push_back(',');
append(out, biology::max_archived_organisms);
out.push_back(',');
append(out, state.archive().archived());
out.push_back(',');
append(out, biology::max_archived_genomes);
out.push_back(']');
if (state.rules_version() >= cellular_lives_revamp_rules) {
const auto& rules = state.revamp_rules();
out += ",\"drip\":[";
append(out, rules.drip_interval_ticks);
out.push_back(',');
append(out, rules.drip_bubble_pj);
out.push_back(',');
append(out, rules.drip_bubble_fg);
out.push_back(',');
append(out, rules.heat_recycle_per_thousand);
out += rules.drip_absorb ? ",1]" : ",0]";
// The organism cap and the wildlife cap (0: the organism cap).
out += ",\"caps\":[";
append(out, rules.organism_cap);
out.push_back(',');
append(out, rules.wildlife_cap);
out.push_back(']');
}
out += ",\"cells_total\":";
append(out, lives.size());
out += ",\"deposits_total\":";
append(out, patches.size());
// Counts and stock over every life and deposit, beyond the listing limits.
std::array<std::uint64_t, 5> counts{}; // Players, wildlife, patches, pools, bubbles.
Totals reserve{}, body{};
std::array<Totals, 3> deposits_by_kind{};
for (const auto& life : lives) {
++counts[life.account == LifeAccount{} ? 1 : 0];
add(reserve, life.reserve);
add(body, life.body);
}
for (const auto& patch : patches) {
const auto kind = static_cast<std::size_t>(patch.kind);
if (kind < deposits_by_kind.size()) {
++counts[2 + kind];
add(deposits_by_kind[kind], patch.stock);
}
}
out += ",\"counts\":[";
for (std::size_t i = 0; i < counts.size(); ++i) {
if (i != 0)
out.push_back(',');
append(out, counts[i]);
}
out += "],\"totals\":{";
append_totals(out, "reserve", reserve);
out.push_back(',');
append_totals(out, "body", body);
out.push_back(',');
append_totals(out, "patches", deposits_by_kind[0]);
out.push_back(',');
append_totals(out, "pools", deposits_by_kind[1]);
out.push_back(',');
append_totals(out, "bubbles", deposits_by_kind[2]);
out.push_back('}');
// Lives are kept in ascending life id; a prefix is the lowest ids.
out += ",\"cells\":[";
const auto cells = std::min(lives.size(), limits.cells);
for (std::size_t i = 0; i < cells; ++i) {
const auto& life = lives[i];
const bool wildlife = life.account == LifeAccount{};
std::uint64_t flags = wildlife ? 1U : 0U;
if (!wildlife && online && online(life.account))
flags |= 2U;
if (life.presence == CellularPresence::lingering)
flags |= 4U;
if (life.presence == CellularPresence::suspended)
flags |= 8U;
std::uint64_t body_mass{};
for (const auto& channel : life.body.matter)
body_mass += channel.value;
if (i != 0)
out.push_back(',');
out += "[\"";
append(out, life.identity.life);
out += "\",\"";
append(out, life.identity.organism);
out += "\",\"";
if (!wildlife)
out += cellular_account_text(life.account);
out += "\",";
append(out, life.position.x_um);
out.push_back(',');
append(out, life.position.y_um);
out.push_back(',');
append(out, state.extent_of(life).value_or(0));
out.push_back(',');
append(out, flags);
out.push_back(',');
append(out, life.revamp.members);
out.push_back(',');
append_inventory(out, life.reserve);
out.push_back(',');
append(out, body_mass);
out += ",\"";
append(out, cellular_lineage_root(state, life.identity.life));
out += "\"]";
}
// Patches are kept by address; the view lists them by ascending id.
std::vector<const biology::CellularPatch*> listed;
listed.reserve(patches.size());
for (const auto& patch : patches)
listed.push_back(&patch);
const auto deposits = std::min(listed.size(), limits.deposits);
std::ranges::partial_sort(listed, listed.begin() + static_cast<std::ptrdiff_t>(deposits), {},
[](const biology::CellularPatch* patch) { return patch->id; });
out += "],\"deposits\":[";
for (std::size_t i = 0; i < deposits; ++i) {
const auto& patch = *listed[i];
if (i != 0)
out.push_back(',');
out.push_back('[');
append(out, patch.id);
out.push_back(',');
append(out, static_cast<std::uint64_t>(patch.kind));
out.push_back(',');
append(out, patch.address.x_um);
out.push_back(',');
append(out, patch.address.y_um);
out.push_back(',');
append(out, patch.radius_um);
out.push_back(',');
append_inventory(out, patch.stock);
out.push_back(']');
}
out += "]}\n";
return out;
}
} // namespace evo::server