M11.2a: fund equal primitive lives from finite spawn pools
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m44s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (network) (push) Canceled after 13s
Foundation / checks (checks) (push) Canceled after 3m9s
Foundation / checks (client) (push) Canceled after 3m8s

This commit is contained in:
dingo
2026-10-09 18:27:55 -04:00
parent 7ee1c68a82
commit 69101a74c2
11 changed files with 533 additions and 20 deletions
+31
View File
@@ -1,5 +1,7 @@
#include "evo/biology/founder.hpp"
#include <limits>
namespace evo::biology {
std::expected<Genome, GenomeError> primitive_founder(std::uint64_t id, std::uint64_t origin_event) {
const auto& p = primitive_founder_parameters;
@@ -16,4 +18,33 @@ std::expected<Genome, GenomeError> primitive_founder(std::uint64_t id, std::uint
{Capability::target_size, p.target_size, 0}};
return Genome::create(draft);
}
std::expected<FounderEndowment, DevelopmentError> primitive_endowment() {
// Identity does not enter development; the published genome gets allocated
// identities at creation, not these analysis-only values.
const auto genome = primitive_founder(1, 1);
const auto rules = rules_for(primitive_founder_development_version);
if (!genome || !rules)
return std::unexpected{DevelopmentError::unsupported_rules};
const auto budget = metabolic_budget(*genome, {1000, 0}, {rules->reference_temperature, {}});
if (!budget)
return std::unexpected{budget.error()};
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
if (budget->demand.value > maximum / founder_reserve_seconds)
return std::unexpected{DevelopmentError::overflow};
// Same construction pricing as development: floor(mass * price / 1000),
// with checked intermediates. The founder pays its entire membrane too.
const auto mass = budget->phenotype.total_mass;
const auto price = rules->construction_per_kilogram;
if (price != 0 && (mass / 1000 > maximum / price || mass % 1000 > maximum / price))
return std::unexpected{DevelopmentError::overflow};
const auto whole = mass / 1000 * price;
const auto fraction = mass % 1000 * price / 1000;
if (fraction > maximum - whole)
return std::unexpected{DevelopmentError::overflow};
return FounderEndowment{{1000, 0},
budget->body,
{{budget->demand.value * founder_reserve_seconds}, {}},
{whole + fraction}};
}
} // namespace evo::biology
+117
View File
@@ -0,0 +1,117 @@
#include "evo/server/lifecycle/primitive_lives.hpp"
#include <algorithm>
#include <cstdlib>
#include <limits>
#include <utility>
namespace evo::server {
struct PrimitiveLives::State {
NurseryConfig config;
accounting::Ledger ledger;
biology::LineageArchive archive;
std::vector<PrimitiveLife> lives;
};
PrimitiveLives::PrimitiveLives(std::unique_ptr<State> state) : state_{std::move(state)} {}
PrimitiveLives::~PrimitiveLives() = default;
std::expected<std::unique_ptr<PrimitiveLives>, LifeError>
PrimitiveLives::create(NurseryConfig config) {
if (config.world == 0 || config.region == 0 || config.frame == 0 || config.source == 0 ||
config.sites.empty() || config.sites.size() > max_primitive_lives ||
!std::ranges::is_sorted(config.sites))
return std::unexpected{LifeError::invalid};
const auto founder = biology::primitive_founder(1, 1);
if (!founder)
return std::unexpected{LifeError::biology};
const auto phenotype = biology::derive_phenotype(*founder, {1000, 0}, {});
if (!phenotype)
return std::unexpected{LifeError::biology};
for (std::size_t i = 0; i < config.sites.size(); ++i) {
const auto site = config.sites[i];
if (site.x_um < -spawn_coordinate_limit_um || site.x_um > spawn_coordinate_limit_um ||
site.y_um < -spawn_coordinate_limit_um || site.y_um > spawn_coordinate_limit_um)
return std::unexpected{LifeError::invalid};
for (std::size_t j = 0; j < i; ++j) {
const auto dx = std::abs(std::int64_t{site.x_um} - config.sites[j].x_um);
const auto dy = std::abs(std::int64_t{site.y_um} - config.sites[j].y_um);
if (dx < phenotype->extent && dy < phenotype->extent)
return std::unexpected{LifeError::occupied};
}
}
std::vector<accounting::Inventory> opening(2 + 2 * config.sites.size());
opening[0] = config.funding; // Source 1, heat 2; body/reserve pairs follow.
auto ledger = accounting::Ledger::create(opening);
if (!ledger)
return std::unexpected{LifeError::overflow};
auto state = std::make_unique<State>(State{std::move(config), std::move(*ledger), {}, {}});
return std::unique_ptr<PrimitiveLives>{new PrimitiveLives{std::move(state)}};
}
std::expected<PrimitiveLife, LifeError> PrimitiveLives::spawn(LifeRequest request,
LifeAllocation allocation) {
if (request.action == 0 || std::ranges::all_of(request.account, [](auto b) { return b == 0; }))
return std::unexpected{LifeError::invalid};
for (const auto& life : state_->lives) {
if (life.request == request)
return life;
if (life.request.account == request.account)
return std::unexpected{LifeError::existing_life};
}
if (allocation.life == 0 || allocation.organism == 0 || allocation.genome == 0 ||
allocation.event == 0)
return std::unexpected{LifeError::invalid};
for (const auto& life : state_->lives) {
const auto& old = life.identity;
if (old.life == allocation.life || old.organism == allocation.organism ||
old.genome == allocation.genome || old.event == allocation.event)
return std::unexpected{LifeError::identity_conflict};
}
const auto slot = state_->lives.size();
if (slot == state_->config.sites.size())
return std::unexpected{LifeError::capacity};
const auto endowed = biology::primitive_endowment();
const auto genome = biology::primitive_founder(allocation.genome, allocation.event);
if (!endowed || !genome)
return std::unexpected{LifeError::biology};
const auto body = accounting::ReservoirId{static_cast<std::uint32_t>(3 + 2 * slot)};
const auto reserve = accounting::ReservoirId{body.value + 1};
const PrimitiveLife life{
request, allocation, state_->config.source, state_->config.sites[slot], endowed->growth,
body, reserve};
// Detached candidate: all fallible work, including allocations and archive
// admission, precedes the single noexcept pointer swap.
auto next = std::make_unique<State>(*state_);
const std::array legs{accounting::Transfer{{1}, body, endowed->body},
accounting::Transfer{{1}, reserve, endowed->reserve},
accounting::Transfer{{1}, {2}, {endowed->construction, {}}}};
const auto paid = next->ledger.transfer(slot + 1, legs);
if (!paid)
return std::unexpected{paid.error() == accounting::LedgerError::insufficient_inventory
? LifeError::insufficient_funding
: LifeError::overflow};
if (!next->archive.admit(*genome) ||
!next->archive.record_birth(allocation.organism, allocation.genome, allocation.born))
return std::unexpected{LifeError::archive};
next->lives.push_back(life);
state_.swap(next);
return life;
}
const NurseryConfig& PrimitiveLives::config() const noexcept {
return state_->config;
}
std::span<const PrimitiveLife> PrimitiveLives::lives() const noexcept {
return state_->lives;
}
std::span<const accounting::Inventory> PrimitiveLives::balances() const noexcept {
return state_->ledger.balances();
}
accounting::Inventory PrimitiveLives::totals() const noexcept {
return state_->ledger.conserved_totals();
}
const biology::LineageArchive& PrimitiveLives::archive() const noexcept {
return state_->archive;
}
} // namespace evo::server