From 81866a68970fb09e06965773f149b9d3b8145291 Mon Sep 17 00:00:00 2001 From: dingo Date: Sun, 11 Oct 2026 00:19:47 -0400 Subject: [PATCH] Add funded cellular birth candidate --- docs/engineering/M11_3_DELIVERY_PLAN.md | 10 ++ docs/engineering/VALIDATION.md | 24 ++++ .../evo/server/lifecycle/cellular_lives.hpp | 10 ++ src/server/lifecycle/cellular_batch.cpp | 12 +- src/server/lifecycle/cellular_lives.cpp | 127 ++++++++++++++++++ tests/cellular_lives_tests.cpp | 77 +++++++++++ 6 files changed, 254 insertions(+), 6 deletions(-) diff --git a/docs/engineering/M11_3_DELIVERY_PLAN.md b/docs/engineering/M11_3_DELIVERY_PLAN.md index f09c28d..74e91ab 100644 --- a/docs/engineering/M11_3_DELIVERY_PLAN.md +++ b/docs/engineering/M11_3_DELIVERY_PLAN.md @@ -300,6 +300,16 @@ scoped change before starting the next segment. retry and refuses nonempty founder/archive state or conflicting reuse. A PostgreSQL integration check covers exact restore and retry behavior. This does not provide a player spawn or make 11.3e playable. +- 11.3d-7g: `CellularLives` now builds a detached profile-4 birth candidate + from an allocator-reserved request after the current tick. The owner derives + primitive founder biology, selects the first unoccupied provisioned site, + funds body/reserve/construction from nursery inventory, records the birth in + a copied archive, and preserves energy/matter closure. Refusals leave the + committed owner unchanged. Focused tests cover funding, placement, duplicate + account, identity/tick validation, canonical restore and first action on the + following tick. This remains in-memory: canonical birth trace, durable + publication/recovery and session scheduling remain open, so 11.3e is not + playable yet. ## Exit boundary diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index b639d13..5800924 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -6374,3 +6374,27 @@ environment because `sys/random.h` is unavailable. The local CMake debug build of `evo-cellular-lives-tests` remains running without output; it is not counted as a pass. No PostgreSQL integration execution, CTest, clang-format or CI result is claimed. The Linux/PostgreSQL qualification remains pending CI. + +## 2026-10-11 — M11.3d-7g detached cellular birth candidate + +Added `CellularLives::birth_candidate`, which derives primitive-founder +biology from a reserved identity, chooses the first unoccupied provisioned site, +debits body/reserve/construction funding from the nursery, records the founder +in a copied archive, and returns a validated detached candidate. The committed +owner is unchanged on both success and refusal. The focused lifecycle test +covers a funded birth, conservation, restore, duplicate account, invalid +identity/tick, insufficient funding, occupied-site refusal, and first action +on the following tick. `cellular_batch.cpp` also avoids a GCC 13 +`maybe-uninitialized` false positive for an optional intake target under +optimized `NDEBUG` builds. + +The focused test executable was manually compiled and run with GCC 13.2 in +debug (`-O0 -g`) and release (`-O2 -DNDEBUG`) modes; both print PASS. The +pre-existing missing-field initializer warning in +`cellular_region_step.cpp:29` is demoted for these builds; other warnings remain +errors. `git diff --check` passes. CMake/CTest, sanitizers, clang-format, +PostgreSQL integration and CI have not run for this segment; the local CMake +jobs remain stalled without output and do not count as validation. The current +Windows environment also lacks `clang-format`. Durable birth trace and server +integration remain unimplemented, so this does not make the cellular stage +playable. diff --git a/include/evo/server/lifecycle/cellular_lives.hpp b/include/evo/server/lifecycle/cellular_lives.hpp index 8ad5682..b5bebdf 100644 --- a/include/evo/server/lifecycle/cellular_lives.hpp +++ b/include/evo/server/lifecycle/cellular_lives.hpp @@ -50,6 +50,9 @@ enum class CellularLivesError : std::uint8_t { identity, invalid_config, invalid_life, + life_exists, + no_placement, + insufficient_funding, duplicate_life, invalid_tick, invalid_intents, @@ -98,6 +101,13 @@ class CellularLives { CellularLivesError> perceive(std::uint64_t life_id) const; + // Returns a detached post-birth candidate for an allocator-reserved request. + // The caller supplies the durable identity; the owner derives founder biology, + // funding, source and the first unoccupied provisioned site. Birth must occur + // at the current checkpoint tick, after that tick's existing-life step. + [[nodiscard]] std::expected, CellularLivesError> + birth_candidate(LifeRequest request, LifeAllocation allocation) const; + [[nodiscard]] static std::expected, CellularLivesError> restore(std::span bytes, std::uint64_t world, std::uint64_t region, std::uint64_t frame, std::uint64_t tick); diff --git a/src/server/lifecycle/cellular_batch.cpp b/src/server/lifecycle/cellular_batch.cpp index 82051fa..8424b1e 100644 --- a/src/server/lifecycle/cellular_batch.cpp +++ b/src/server/lifecycle/cellular_batch.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -280,12 +281,11 @@ match_cellular_actions_to_trace(const CellularLives& predecessor, const Cellular if (intent == intents.end() || intent->life_id != life->identity.life) return std::unexpected{CellularActionBlockError::mismatch}; const auto input = std::span{action.input}; - const auto target = input[12] == 0 - ? std::optional{} - : std::optional{ - biology::CellularPatchAddress{ - static_cast(get(input, 16, 4)), - static_cast(get(input, 20, 4))}}; + std::optional target; + if (input[12] != 0) + target = biology::CellularPatchAddress{ + static_cast(get(input, 16, 4)), + static_cast(get(input, 20, 4))}; if (intent->intent.movement.x != std::bit_cast( static_cast(get(input, 8, 2))) || intent->intent.movement.y != std::bit_cast( diff --git a/src/server/lifecycle/cellular_lives.cpp b/src/server/lifecycle/cellular_lives.cpp index 6b98f4f..1d19462 100644 --- a/src/server/lifecycle/cellular_lives.cpp +++ b/src/server/lifecycle/cellular_lives.cpp @@ -1,4 +1,5 @@ #include "evo/server/lifecycle/cellular_lives.hpp" +#include "evo/biology/founder.hpp" #include #include @@ -100,6 +101,19 @@ bool add_inventory(accounting::Inventory& total, const accounting::Inventory& va return true; } +bool subtract_inventory(accounting::Inventory& total, + const accounting::Inventory& value) noexcept { + if (value.energy.value > total.energy.value) + return false; + for (std::size_t channel = 0; channel < total.matter.size(); ++channel) + if (value.matter[channel].value > 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 budget_for(const biology::LineageArchive& archive, const CellularOwnedLife& life, std::uint32_t temperature) { @@ -474,6 +488,119 @@ CellularLives::perceive(std::uint64_t life_id) const { return std::move(*view); } +std::expected, CellularLivesError> +CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) const { + if (request.action == 0 || + std::ranges::all_of(request.account, [](std::uint8_t value) { return value == 0; })) + return std::unexpected{CellularLivesError::invalid_life}; + if (allocation.life == 0 || allocation.life != allocation.organism || + allocation.life != allocation.genome || allocation.life != allocation.event) + return std::unexpected{CellularLivesError::identity}; + if (allocation.born.value == 0 || allocation.born.value != state_->config.tick) + return std::unexpected{CellularLivesError::invalid_tick}; + if (state_->lives.size() >= biology::max_cellular_tick_lives) + return std::unexpected{CellularLivesError::no_placement}; + if (std::ranges::any_of(state_->lives, [&](const CellularOwnedLife& life) { + return life.account == request.account; + })) + return std::unexpected{CellularLivesError::life_exists}; + if (std::ranges::any_of(state_->lives, [&](const CellularOwnedLife& life) { + return life.identity.life == allocation.life || + life.identity.organism == allocation.organism || + life.identity.genome == allocation.genome || + life.identity.event == allocation.event; + }) || + std::ranges::any_of(state_->corpses, [&](const CellularOwnedCorpse& corpse) { + return corpse.life_id == allocation.life || corpse.organism == allocation.organism; + }) || state_->archive.find(allocation.genome) || state_->archive.organism(allocation.organism)) + return std::unexpected{CellularLivesError::duplicate_life}; + + const auto endowed = biology::primitive_endowment(); + const auto genome = biology::primitive_founder(allocation.genome, allocation.event); + if (!endowed || !genome) + return std::unexpected{CellularLivesError::biology}; + const auto budget = biology::metabolic_budget( + *genome, endowed->growth, + {state_->config.ambient_temperature_millikelvin, {}}); + if (!budget || budget->body != endowed->body) + return std::unexpected{CellularLivesError::biology}; + + std::optional position; + for (const auto site : state_->nursery_config.sites) { + if (site.x_um < 0 || site.y_um < 0) + return std::unexpected{CellularLivesError::invalid_config}; + const biology::CellularPosition candidate{static_cast(site.x_um), + static_cast(site.y_um)}; + bool occupied = false; + for (const auto& living : state_->lives) { + const auto living_budget = budget_for( + state_->archive, living, state_->config.ambient_temperature_millikelvin); + if (!living_budget) + return std::unexpected{living_budget.error()}; + const auto separated = [](std::uint64_t left, std::uint64_t right, + std::uint32_t left_extent, + std::uint32_t right_extent) noexcept { + return left < right ? right - left >= left_extent : left - right >= right_extent; + }; + if (!separated(candidate.x_um, living.position.x_um, budget->phenotype.extent, + living_budget->phenotype.extent) && + !separated(candidate.y_um, living.position.y_um, budget->phenotype.extent, + living_budget->phenotype.extent)) { + occupied = true; + break; + } + } + if (!occupied) { + position = candidate; + break; + } + } + if (!position) + return std::unexpected{CellularLivesError::no_placement}; + + auto nursery = state_->nursery; + if (!subtract_inventory(nursery, endowed->body) || + !subtract_inventory(nursery, endowed->reserve) || + endowed->construction.value > nursery.energy.value) + return std::unexpected{CellularLivesError::insufficient_funding}; + nursery.energy.value -= endowed->construction.value; + auto heat = state_->heat; + if (endowed->construction.value > + std::numeric_limits::max() - heat.energy.value) + return std::unexpected{CellularLivesError::overflow}; + heat.energy.value += endowed->construction.value; + + auto archive = state_->archive; + if (!archive.admit(*genome) || + !archive.record_birth(allocation.organism, allocation.genome, allocation.born)) + return std::unexpected{CellularLivesError::archive}; + auto lives = state_->lives; + lives.push_back({request.account, allocation, state_->nursery_config.source, endowed->growth, + *position, endowed->body, endowed->reserve}); + std::ranges::sort(lives, {}, [](const CellularOwnedLife& life) { + return life.identity.life; + }); + + auto nursery_config = state_->nursery_config; + nursery_config.funding = nursery; + auto habitat = biology::CellularHabitat::restore(state_->habitat->snapshot(), state_->world, + state_->region, state_->frame); + if (!habitat) + return std::unexpected{CellularLivesError::habitat}; + const auto current = validate_state(state_->world, state_->region, state_->frame, + state_->config, nursery_config, **habitat, lives, + state_->corpses, archive, nursery, heat, + state_->opening_total); + if (!current) + return std::unexpected{current.error()}; + + auto candidate = std::unique_ptr{new CellularLives{std::make_unique( + state_->world, state_->region, state_->frame, state_->config, + std::move(nursery_config), std::move(*habitat), std::move(lives), state_->corpses, + archive, nursery, heat, state_->opening_total, *current)}}; + return candidate; +} + std::expected, CellularLivesError> CellularLives::restore(std::span bytes, std::uint64_t world, std::uint64_t region, std::uint64_t frame, std::uint64_t tick) { diff --git a/tests/cellular_lives_tests.cpp b/tests/cellular_lives_tests.cpp index 3eb7fe9..6bffc43 100644 --- a/tests/cellular_lives_tests.cpp +++ b/tests/cellular_lives_tests.cpp @@ -45,6 +45,82 @@ void refresh_checksum(std::vector& bytes) { bytes[bytes.size() - 8 + i] = static_cast(value >> (i * 8U)); } +std::unique_ptr empty_cellular_owner(Inventory funding, + std::vector sites) { + auto primitive = PrimitiveLives::create({91, 1, 1, 17, funding, std::move(sites)}); + CHECK(primitive); + auto habitat = evo::biology::CellularHabitat::create({91, 1, 1, 1'000, 1'000, {}}); + CHECK(habitat); + auto owner = CellularLives::create(**primitive, std::move(*habitat), {{1'000, 1'000}, 0, 293'150}); + CHECK(owner); + return std::move(*owner); +} + +void reserved_birth_is_funded_and_detached() { + auto opening = empty_cellular_owner(founder_price(1), {{0, 0}, {20, 0}}); + const std::span no_lives{}; + CHECK(opening->advance_tick(no_lives)); + const auto predecessor = opening->snapshot(); + LifeRequest request{}; + request.account.back() = 9; + request.action = 17; + const LifeAllocation allocation{501, 501, 501, 501, {1}}; + auto born = opening->birth_candidate(request, allocation); + CHECK(born && opening->tick() == 1 && opening->lives().empty()); + CHECK((*born)->tick() == opening->tick() && (*born)->lives().size() == 1 && + (*born)->lives()[0].account == request.account && + (*born)->lives()[0].identity == allocation && (*born)->lives()[0].source == 17 && + (*born)->lives()[0].position == (evo::biology::CellularPosition{0, 0})); + const auto endowment = evo::biology::primitive_endowment(); + CHECK(endowment); + CHECK((*born)->nursery().energy.value == opening->nursery().energy.value - + endowment->reserve.energy.value - + endowment->construction.value); + CHECK((*born)->heat().energy.value == endowment->construction.value && + (*born)->current_total() == (*born)->opening_total() && + (*born)->archive().recorded() == 1); + CHECK(opening->snapshot() == predecessor); + const auto recovered = CellularLives::restore((*born)->snapshot(), 91, 1, 1, 1); + CHECK(recovered && (*recovered)->snapshot() == (*born)->snapshot()); + auto retry_account = request; + retry_account.action++; + CHECK((*born)->birth_candidate(retry_account, {502, 502, 502, 502, {1}}).error() == + CellularLivesError::life_exists); + CHECK(opening->birth_candidate(request, {502, 503, 502, 502, {1}}).error() == + CellularLivesError::identity); + CHECK(opening->birth_candidate(request, {502, 502, 502, 502, {2}}).error() == + CellularLivesError::invalid_tick); + const std::array next_tick{CellularLifeIntent{allocation.life, {}}}; + CHECK((*born)->advance_tick(next_tick) && (*born)->tick() == 2 && + (*born)->lives().size() == 1); + + auto unfunded = empty_cellular_owner({}, {{0, 0}}); + CHECK(unfunded->advance_tick(no_lives)); + const auto unfunded_snapshot = unfunded->snapshot(); + CHECK(unfunded->birth_candidate(request, {601, 601, 601, 601, {1}}).error() == + CellularLivesError::insufficient_funding); + CHECK(unfunded->snapshot() == unfunded_snapshot && unfunded->lives().empty()); + + auto occupied_primitive = + PrimitiveLives::create({92, 1, 1, 17, founder_price(2), {{0, 0}}}); + CHECK(occupied_primitive); + LifeRequest existing{}; + existing.account.back() = 11; + existing.action = 1; + CHECK((*occupied_primitive)->spawn(existing, {701, 701, 701, 701, {1}})); + auto occupied_habitat = + evo::biology::CellularHabitat::create({92, 1, 1, 1'000, 1'000, {}}); + CHECK(occupied_habitat); + auto occupied = CellularLives::create(**occupied_primitive, std::move(*occupied_habitat), + {{1'000, 1'000}, 1, 293'150}); + CHECK(occupied); + LifeRequest arriving{}; + arriving.account.back() = 12; + arriving.action = 2; + CHECK((*occupied)->birth_candidate(arriving, {702, 702, 702, 702, {1}}).error() == + CellularLivesError::no_placement); +} + void owner_advances_and_closes_death_atomically() { auto primitive = PrimitiveLives::create({90, 1, 7, 17, founder_price(2), {{0, 0}, {20, 0}}}); CHECK(primitive); @@ -197,6 +273,7 @@ void owner_advances_and_closes_death_atomically() { int main() { try { + reserved_birth_is_funded_and_detached(); owner_advances_and_closes_death_atomically(); std::puts("PASS cellular lives: feeding, tick ownership and terminal closure"); } catch (const std::exception& error) {