Add in-memory cellular life owner
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 41s
Foundation / checks (ecs) (push) Successful in 1m50s
Foundation / checks (postgres) (push) Failing after 50s
Foundation / checks (tsan) (push) Failing after 33s
Foundation / checks (network) (push) Failing after 3m6s
Foundation / checks (client) (push) Failing after 4m24s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped

This commit is contained in:
dingo
2026-10-10 22:20:29 -04:00
parent e7fec914e7
commit 562781ce77
7 changed files with 500 additions and 5 deletions
+266
View File
@@ -0,0 +1,266 @@
#include "evo/server/lifecycle/cellular_lives.hpp"
#include <algorithm>
#include <limits>
#include <utility>
namespace evo::server {
namespace {
bool add_inventory(accounting::Inventory& total, const accounting::Inventory& value) noexcept {
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
if (value.energy.value > maximum - total.energy.value)
return false;
for (std::size_t channel = 0; channel < total.matter.size(); ++channel)
if (value.matter[channel].value > maximum - total.matter[channel].value)
return false;
total.energy.value += value.energy.value;
for (std::size_t channel = 0; channel < total.matter.size(); ++channel)
total.matter[channel].value += value.matter[channel].value;
return true;
}
std::expected<biology::MetabolicBudget, CellularLivesError>
budget_for(const biology::LineageArchive& archive, const CellularOwnedLife& life,
std::uint32_t temperature) {
const auto genome = archive.find(life.identity.genome);
if (!genome)
return std::unexpected{CellularLivesError::archive};
const auto budget = biology::metabolic_budget(*genome, life.growth, {temperature, {}});
if (!budget)
return std::unexpected{CellularLivesError::biology};
return *budget;
}
std::expected<accounting::Inventory, CellularLivesError>
sum_state(accounting::Inventory nursery, accounting::Inventory heat,
const biology::CellularHabitat& habitat, std::span<const CellularOwnedLife> lives,
std::span<const CellularOwnedCorpse> corpses) noexcept {
auto total = nursery;
if (!add_inventory(total, heat))
return std::unexpected{CellularLivesError::overflow};
if (!add_inventory(total, habitat.totals()))
return std::unexpected{CellularLivesError::overflow};
for (const auto& life : lives)
if (!add_inventory(total, life.body) || !add_inventory(total, life.reserve))
return std::unexpected{CellularLivesError::overflow};
for (const auto& corpse : corpses)
if (!add_inventory(total, corpse.inventory))
return std::unexpected{CellularLivesError::overflow};
return total;
}
} // namespace
struct CellularLives::State {
std::uint64_t world{}, region{}, frame{};
CellularLivesConfig config{};
std::unique_ptr<biology::CellularHabitat> habitat;
std::vector<CellularOwnedLife> lives;
std::vector<CellularOwnedCorpse> corpses;
biology::LineageArchive archive;
accounting::Inventory nursery{};
accounting::Inventory heat{};
accounting::Inventory opening_total{};
accounting::Inventory current_total{};
State(std::uint64_t world_id, std::uint64_t region_id, std::uint64_t frame_id,
CellularLivesConfig cfg, std::unique_ptr<biology::CellularHabitat> map,
std::vector<CellularOwnedLife> living, std::vector<CellularOwnedCorpse> dead,
const biology::LineageArchive& history, accounting::Inventory source,
accounting::Inventory accumulated_heat, accounting::Inventory initial,
accounting::Inventory current)
: world{world_id}, region{region_id}, frame{frame_id}, config{cfg},
habitat{std::move(map)}, lives{std::move(living)}, corpses{std::move(dead)},
archive{history}, nursery{source}, heat{accumulated_heat}, opening_total{initial},
current_total{current} {}
};
CellularLives::CellularLives(std::unique_ptr<State> state) : state_{std::move(state)} {}
CellularLives::~CellularLives() = default;
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
CellularLives::create(const PrimitiveLives& opening,
std::unique_ptr<biology::CellularHabitat> habitat,
CellularLivesConfig config) {
if (!habitat)
return std::unexpected{CellularLivesError::invalid_config};
const auto& nursery_config = opening.config();
const auto& map = habitat->config();
if (nursery_config.world == 0 || nursery_config.region == 0 || nursery_config.frame == 0 ||
map.world != nursery_config.world || map.region != nursery_config.region ||
map.frame != nursery_config.frame || config.area.width_um == 0 ||
config.area.height_um == 0 || config.area.width_um != map.width_um ||
config.area.height_um != map.height_um || config.ambient_temperature_millikelvin == 0 ||
opening.lives().size() > biology::max_cellular_tick_lives ||
opening.balances().size() != 2 + 2 * nursery_config.sites.size())
return std::unexpected{CellularLivesError::invalid_config};
std::vector<CellularOwnedLife> lives;
lives.reserve(opening.lives().size());
for (const auto& primitive : opening.lives()) {
if (primitive.identity.born.value == 0 || primitive.identity.born.value > config.tick ||
primitive.identity.life == 0 || primitive.identity.organism == 0 ||
primitive.identity.genome == 0 || primitive.source == 0 ||
primitive.site.x_um < 0 || primitive.site.y_um < 0)
return std::unexpected{CellularLivesError::invalid_life};
if (primitive.body.value == 0 || primitive.reserve.value == 0)
return std::unexpected{CellularLivesError::invalid_life};
const auto body_index = static_cast<std::size_t>(primitive.body.value - 1);
const auto reserve_index = static_cast<std::size_t>(primitive.reserve.value - 1);
if (body_index >= opening.balances().size() || reserve_index >= opening.balances().size())
return std::unexpected{CellularLivesError::invalid_life};
lives.push_back({primitive.request.account,
primitive.identity,
primitive.source,
primitive.growth,
{static_cast<std::uint64_t>(primitive.site.x_um),
static_cast<std::uint64_t>(primitive.site.y_um)},
opening.balances()[body_index],
opening.balances()[reserve_index]});
}
std::ranges::sort(lives, {}, [](const auto& life) { return life.identity.life; });
for (std::size_t i = 0; i < lives.size(); ++i) {
const auto& life = lives[i];
if (i != 0 && lives[i - 1].identity.life == life.identity.life)
return std::unexpected{CellularLivesError::duplicate_life};
const auto budget = budget_for(opening.archive(), life,
config.ambient_temperature_millikelvin);
if (!budget || budget->phenotype.extent == 0 ||
budget->phenotype.extent > config.area.width_um ||
budget->phenotype.extent > config.area.height_um || life.body.energy.value != 0 ||
budget->body != life.body ||
life.position.x_um > config.area.width_um - budget->phenotype.extent ||
life.position.y_um > config.area.height_um - budget->phenotype.extent)
return std::unexpected{CellularLivesError::invalid_life};
}
auto initial_total = opening.totals();
if (!add_inventory(initial_total, habitat->totals()))
return std::unexpected{CellularLivesError::overflow};
const auto nursery = opening.balances()[0];
const auto heat = opening.balances()[1];
const auto current = sum_state(nursery, heat, *habitat, lives, {});
if (!current)
return std::unexpected{current.error()};
if (*current != initial_total)
return std::unexpected{CellularLivesError::conservation};
auto state = std::make_unique<State>(
nursery_config.world, nursery_config.region, nursery_config.frame, config,
std::move(habitat), std::move(lives), std::vector<CellularOwnedCorpse>{},
opening.archive(), nursery, heat, initial_total, *current);
return std::unique_ptr<CellularLives>{new CellularLives{std::move(state)}};
}
std::uint64_t CellularLives::world() const noexcept { return state_->world; }
std::uint64_t CellularLives::region() const noexcept { return state_->region; }
std::uint64_t CellularLives::frame() const noexcept { return state_->frame; }
std::uint64_t CellularLives::tick() const noexcept { return state_->config.tick; }
biology::CellularArea CellularLives::area() const noexcept { return state_->config.area; }
std::span<const CellularOwnedLife> CellularLives::lives() const noexcept { return state_->lives; }
std::span<const CellularOwnedCorpse> CellularLives::corpses() const noexcept {
return state_->corpses;
}
const biology::CellularHabitat& CellularLives::habitat() const noexcept { return *state_->habitat; }
const biology::LineageArchive& CellularLives::archive() const noexcept { return state_->archive; }
accounting::Inventory CellularLives::nursery() const noexcept { return state_->nursery; }
accounting::Inventory CellularLives::heat() const noexcept { return state_->heat; }
accounting::Inventory CellularLives::opening_total() const noexcept {
return state_->opening_total;
}
accounting::Inventory CellularLives::current_total() const noexcept {
return state_->current_total;
}
std::expected<biology::CellularRegionStepCandidate, CellularLivesError>
CellularLives::advance_tick(std::span<const CellularLifeIntent> intents) {
if (state_->config.tick == std::numeric_limits<std::uint64_t>::max())
return std::unexpected{CellularLivesError::invalid_tick};
if (intents.size() != state_->lives.size())
return std::unexpected{CellularLivesError::invalid_intents};
std::vector<CellularLifeIntent> ordered{intents.begin(), intents.end()};
std::ranges::sort(ordered, {}, &CellularLifeIntent::life_id);
for (std::size_t i = 0; i < ordered.size(); ++i)
if ((i != 0 && ordered[i - 1].life_id == ordered[i].life_id) ||
ordered[i].life_id != state_->lives[i].identity.life)
return std::unexpected{CellularLivesError::invalid_intents};
std::vector<biology::CellularLifeStepInput> inputs;
inputs.reserve(state_->lives.size());
for (std::size_t i = 0; i < state_->lives.size(); ++i) {
const auto& life = state_->lives[i];
const auto budget = budget_for(state_->archive, life,
state_->config.ambient_temperature_millikelvin);
if (!budget)
return std::unexpected{budget.error()};
inputs.push_back({life.identity.life,
life.position,
budget->phenotype.extent,
ordered[i].intent,
life.body,
life.reserve,
{},
*budget});
}
auto proposal = biology::propose_cellular_region_step(state_->config.tick, state_->config.area,
inputs, *state_->habitat);
if (!proposal)
return std::unexpected{CellularLivesError::region_step};
auto next_lives = state_->lives;
auto next_corpses = state_->corpses;
auto next_archive = state_->archive;
auto next_heat = state_->heat;
auto habitat_config = state_->habitat->config();
for (const auto& result : proposal->lives) {
const auto found = std::ranges::lower_bound(
next_lives, result.life_id, {}, [](const auto& life) { return life.identity.life; });
if (found == next_lives.end() || found->identity.life != result.life_id)
return std::unexpected{CellularLivesError::invalid_life};
found->position = result.candidate.position;
found->reserve = result.candidate.reserve_after;
if (!add_inventory(next_heat, result.candidate.heat_after))
return std::unexpected{CellularLivesError::overflow};
}
for (const auto& dead : proposal->starved) {
const auto found = std::ranges::lower_bound(
next_lives, dead.life_id, {}, [](const auto& life) { return life.identity.life; });
if (found == next_lives.end() || found->identity.life != dead.life_id ||
!next_archive.record_death(found->identity.organism, {proposal->end_tick}))
return std::unexpected{CellularLivesError::archive};
next_corpses.push_back(
{found->identity.organism, proposal->end_tick, dead.position, dead.inventory});
next_lives.erase(found);
}
std::ranges::sort(next_corpses, {}, &CellularOwnedCorpse::organism);
if (proposal->heat_released.value >
std::numeric_limits<std::uint64_t>::max() - next_heat.energy.value)
return std::unexpected{CellularLivesError::overflow};
next_heat.energy.value += proposal->heat_released.value;
for (const auto& update : proposal->patches) {
const auto found = std::ranges::lower_bound(
habitat_config.patches, update.address, {}, [](const auto& patch) {
return patch.address;
});
if (found == habitat_config.patches.end() || found->address != update.address)
return std::unexpected{CellularLivesError::habitat};
found->stock = update.stock;
}
auto next_habitat = biology::CellularHabitat::create(std::move(habitat_config));
if (!next_habitat)
return std::unexpected{CellularLivesError::habitat};
const auto total = sum_state(state_->nursery, next_heat, **next_habitat, next_lives,
next_corpses);
if (!total)
return std::unexpected{total.error()};
if (*total != state_->opening_total)
return std::unexpected{CellularLivesError::conservation};
auto next = std::make_unique<State>(
state_->world, state_->region, state_->frame,
CellularLivesConfig{state_->config.area, proposal->end_tick,
state_->config.ambient_temperature_millikelvin},
std::move(*next_habitat), std::move(next_lives), std::move(next_corpses), next_archive,
state_->nursery, next_heat, state_->opening_total, *total);
state_.swap(next);
return std::move(*proposal);
}
} // namespace evo::server