Files
aevolution/src/server/lifecycle/cellular_census.cpp
T
dingoandClaude Opus 5.5 108f29eda5
Foundation / checks (ecs) (push) Successful in 2m25s
Foundation / checks (deploy) (push) Successful in 5m15s
Foundation / checks (network) (push) Successful in 21m56s
Foundation / checks (client) (push) Successful in 22m54s
Foundation / checks (tsan) (push) Successful in 47m4s
Foundation / checks (postgres) (push) Successful in 1h15m49s
Foundation / checks (checks) (push) Failing after 1h30m4s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
feat(biology): evolve photosynthetic producers that capture light
ADR-0175 implements ADR-0136 step C with trial costs to be balanced after
playtesting. Genome schema 5 adds a photosynthesis efficiency (empty in
schemas 1-4, so earlier offers and draws are unchanged; mutation operators
29-32 are appended). Development version 5 builds the organelles as
internal membrane (p/4 of structure, upkept twice) and slows movement by
p/2 per thousand. Founder 4 and cellular rules 11 let lineages earn the
capability as an offspring plan that children express.

Owner format 9 declares the region's light. Inside each tick's detached
candidate, active unfrozen producers capture cross-section x irradiance x
efficiency (a quarter while moving), credited to the reserve and boundary
inflow together and replayed from recorded intentions. Well-lit wildlife
producers hold still. The joint owner keeps infall, water and radiation and
nests a rules-11 owner unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 18:06:02 -04:00

190 lines
9.0 KiB
C++

#include "evo/server/lifecycle/cellular_census.hpp"
#include <algorithm>
#include <limits>
#include <utility>
namespace evo::server {
namespace {
bool add(std::uint64_t& target, std::uint64_t value) {
if (value > std::numeric_limits<std::uint64_t>::max() - target)
return false;
target += value;
return true;
}
bool add(accounting::Inventory& target, const accounting::Inventory& value) {
if (!add(target.energy.value, value.energy.value))
return false;
for (std::size_t channel = 0; channel < target.matter.size(); ++channel)
if (!add(target.matter[channel].value, value.matter[channel].value))
return false;
return true;
}
bool add(CellularCensusTotals& target, const CellularCensusTotals& value) {
if (!add(target.retained_nonresident_records, value.retained_nonresident_records) ||
!add(target.retained_dead_plan_records, value.retained_dead_plan_records))
return false;
for (std::size_t kind = 0; kind < target.resident.size(); ++kind) {
auto& stock = target.resident[kind];
const auto& source = value.resident[kind];
if (!add(target.living_plan_holders[kind], value.living_plan_holders[kind]) ||
!add(stock.count, source.count) || !add(stock.body, source.body) ||
!add(stock.reserve, source.reserve))
return false;
}
return true;
}
} // namespace
std::size_t CellularCensus::locate(std::uint64_t genome) const {
const auto found = std::ranges::lower_bound(rows_, genome, {}, &CellularCensusRow::genome);
return found != rows_.end() && found->genome == genome
? static_cast<std::size_t>(found - rows_.begin())
: rows_.size();
}
std::expected<CellularCensus, CellularCensusError>
CellularCensus::observe(const CellularLives& state) {
return observe(state, {}, 0);
}
std::expected<CellularCensus, CellularCensusError>
CellularCensus::observe(const CellularPopulation& population) {
return observe(population.state(), population.members(), population.format_version());
}
std::expected<CellularCensus, CellularCensusError>
CellularCensus::observe(const CellularLives& state, std::span<const CellularCohortMember> members,
std::uint32_t population) {
const bool maturing = state.rules_version() == cellular_lives_maturation_rules &&
state.format_version() == cellular_lives_migration_format &&
(population == 0 || population == 2);
const bool inherited =
cellular_retained_plans(state.rules_version()) &&
state.format_version() >= cellular_lives_inheritance_format &&
(population == 0 || population == cellular_population_inheritance_format);
if (!inherited && ((!maturing && state.rules_version() != cellular_lives_foraging_rules) ||
(state.format_version() != cellular_lives_presence_format &&
state.format_version() != cellular_lives_migration_format) ||
(population != 0 && population != 1 && population != 2)))
return std::unexpected(CellularCensusError::unsupported);
CellularCensus result;
result.boundary_ = {state.world(), state.region(), state.frame(), state.tick(),
state.format_version(), state.rules_version(), population};
if (inherited)
result.boundary_.projection_version = cellular_plan_census_projection_version;
const auto& archive = state.archive();
result.genomes_.assign(archive.genomes().begin(), archive.genomes().end());
result.rows_.reserve(archive.archived());
for (const auto& genome : archive.genomes())
result.rows_.push_back({genome.id(),
genome.schema_version(),
genome.development_version(),
{genome.parents().begin(), genome.parents().end()},
{}});
result.children_.resize(result.rows_.size());
for (std::size_t child = 0; child < result.rows_.size(); ++child)
for (const auto parent : result.rows_[child].parents) {
const auto index = result.locate(parent);
if (index == result.rows_.size())
return std::unexpected(CellularCensusError::history);
result.children_[index].push_back(child);
}
for (const auto& life : state.lives()) {
result.residents_.push_back(life.identity.organism);
const auto row = result.locate(life.identity.genome);
if (row == result.rows_.size())
return std::unexpected(CellularCensusError::history);
CellularCensusKind kind{};
if (life.account == LifeAccount{}) {
const bool cohort = std::ranges::binary_search(members, life.identity.life, {},
&CellularCohortMember::life);
kind = cohort ? CellularCensusKind::cohort_wildlife
: CellularCensusKind::individual_wildlife;
} else {
switch (life.presence) {
case CellularPresence::active:
kind = CellularCensusKind::active_controlled;
break;
case CellularPresence::lingering:
kind = CellularCensusKind::lingering_controlled;
break;
case CellularPresence::suspended:
kind = CellularCensusKind::suspended_controlled;
break;
}
}
auto& stock = result.rows_[row].totals.resident[static_cast<std::size_t>(kind)];
if (!add(stock.count, 1) || !add(stock.body, life.body) ||
!add(stock.reserve, life.reserve))
return std::unexpected(CellularCensusError::overflow);
if (inherited) {
const auto plan = result.locate(life.offspring_genome);
if (plan == result.rows_.size())
return std::unexpected(CellularCensusError::history);
if (!add(result.rows_[plan].totals.living_plan_holders[static_cast<std::size_t>(kind)],
1))
return std::unexpected(CellularCensusError::overflow);
}
}
if (inherited)
for (const auto& corpse : state.corpses()) {
const auto plan = result.locate(corpse.offspring_genome);
if (plan == result.rows_.size())
return std::unexpected(CellularCensusError::history);
if (!add(result.rows_[plan].totals.retained_dead_plan_records, 1))
return std::unexpected(CellularCensusError::overflow);
}
const auto& nonresident = state.boundary_ledger().nonresident_organisms;
std::vector<std::uint64_t> nonresident_genomes;
nonresident_genomes.reserve(nonresident.size());
for (const auto organism : nonresident) {
const auto birth = archive.organism(organism);
if (!birth)
return std::unexpected(CellularCensusError::history);
nonresident_genomes.push_back(birth->genome);
}
// One bounded revision pass, rather than scanning every revision for every
// departed record. Archive admission guarantees canonical revision order.
for (const auto& revision : archive.revisions()) {
const auto found = std::ranges::lower_bound(nonresident, revision.organism);
if (found == nonresident.end() || *found != revision.organism)
continue;
auto& genome = nonresident_genomes[static_cast<std::size_t>(found - nonresident.begin())];
if (genome != revision.previous_genome)
return std::unexpected(CellularCensusError::history);
genome = revision.successor_genome;
}
for (const auto genome : nonresident_genomes) {
const auto row = result.locate(genome);
if (row == result.rows_.size())
return std::unexpected(CellularCensusError::history);
if (!add(result.rows_[row].totals.retained_nonresident_records, 1))
return std::unexpected(CellularCensusError::overflow);
}
return result;
}
std::expected<CellularCensusTotals, CellularCensusError>
CellularCensus::query(std::uint64_t anchor, CellularCensusScope scope) const {
if (scope != CellularCensusScope::exact_genome && scope != CellularCensusScope::descendants)
return std::unexpected(CellularCensusError::invalid_scope);
const auto root = locate(anchor);
if (root == rows_.size())
return std::unexpected(CellularCensusError::unknown_genome);
if (scope == CellularCensusScope::exact_genome)
return rows_[root].totals;
CellularCensusTotals result;
std::vector<bool> visited(rows_.size(), false);
std::vector<std::size_t> pending{root};
visited[root] = true;
for (std::size_t next = 0; next < pending.size(); ++next) {
const auto row = pending[next];
if (!add(result, rows_[row].totals))
return std::unexpected(CellularCensusError::overflow);
for (const auto child : children_[row])
if (!visited[child]) {
visited[child] = true;
pending.push_back(child);
}
}
return result;
}
} // namespace evo::server