From a898a35e1bd0c379fc595199fa6eadbc002855ea Mon Sep 17 00:00:00 2001 From: dingo Date: Mon, 19 Oct 2026 15:19:43 -0400 Subject: [PATCH] feat(lifecycle): preserve inherited plans in exact populations --- CMakeLists.txt | 10 + README.md | 3 +- .../region/dev-session-route/listener.yaml | 2 +- docs/STATUS.md | 15 + .../0171-retained-plan-exact-populations.md | 57 ++++ docs/adr/README.md | 2 + docs/biology/CELLULAR_POPULATION.md | 9 + docs/engineering/M12_REMAINING_GATES.md | 5 +- docs/engineering/VALIDATION.md | 52 +++ .../evo/server/lifecycle/cellular_census.hpp | 2 +- .../server/lifecycle/cellular_population.hpp | 4 + .../lifecycle/cellular_population_batch.hpp | 3 +- schemas/cellular-inheritance-upgrade.md | 3 + schemas/cellular-population-batch.md | 15 + schemas/cellular-population.md | 29 ++ .../ecology/regional-cellular-census-v2.md | 8 +- src/server/lifecycle/cellular_census.cpp | 7 +- src/server/lifecycle/cellular_population.cpp | 30 +- .../lifecycle/cellular_population_batch.cpp | 32 +- .../lifecycle/cellular_population_codec.cpp | 17 +- tests/cellular_inheritance_tests.cpp | 5 +- tests/cellular_plan_population_tests.cpp | 308 ++++++++++++++++++ 22 files changed, 582 insertions(+), 36 deletions(-) create mode 100644 docs/adr/0171-retained-plan-exact-populations.md create mode 100644 tests/cellular_plan_population_tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b4a26f4..ed9a217 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -695,6 +695,16 @@ endif() if(BUILD_TESTING) add_executable(evo-cellular-population-tests tests/cellular_population_tests.cpp) + add_executable(evo-cellular-plan-population-tests tests/cellular_plan_population_tests.cpp) + target_link_libraries(evo-cellular-plan-population-tests PRIVATE evo::lifecycle) + evo_owned_target(evo-cellular-plan-population-tests) + add_test(NAME cellular-retained-plan-population COMMAND evo-cellular-plan-population-tests) + add_test(NAME cellular-retained-plan-population-restart COMMAND "${CMAKE_COMMAND}" + "-DPOPULATION_TEST=$" + "-DTEST_DIR=${CMAKE_CURRENT_BINARY_DIR}/plan-population-restart" + -P "${CMAKE_CURRENT_SOURCE_DIR}/tests/cellular_population_restart.cmake") + set_tests_properties(cellular-retained-plan-population cellular-retained-plan-population-restart + PROPERTIES TIMEOUT ${evo_cohort_timeout}) add_executable(evo-cellular-inheritance-tests tests/cellular_inheritance_tests.cpp) target_link_libraries(evo-cellular-inheritance-tests PRIVATE evo::lifecycle) evo_owned_target(evo-cellular-inheritance-tests) diff --git a/README.md b/README.md index 3e7dea5..eb02771 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,8 @@ process fixtures remain separate from organism biology. read-only local resident/presence/cohort and exact/descendant observations from current cellular owners. Retained departed history is excluded from owned stock. [Projection 2](schemas/ecology/regional-cellular-census-v2.md) additionally -observes raw retained offspring plans separately from expressed organisms and +observes raw and portable exact-population offspring plans separately from +expressed organisms and inventory; old projection-1 plan measures remain unobserved. [Committed regional history](schemas/ecology/committed-cellular-history-v1.md) now derives bounded, fully replayed census pages from the existing atomic biological diff --git a/deploy/kubernetes/region/dev-session-route/listener.yaml b/deploy/kubernetes/region/dev-session-route/listener.yaml index b00c67f..9fe4ee7 100644 --- a/deploy/kubernetes/region/dev-session-route/listener.yaml +++ b/deploy/kubernetes/region/dev-session-route/listener.yaml @@ -12,7 +12,7 @@ spec: env: - {name: REGION_SESSION, value: "dev"} - {name: REGION_SESSION_LISTEN, value: "0.0.0.0:7400"} - - {name: REGION_SESSION_WINDOW, value: "dev-20261019T190730-815046b1:1792451250"} + - {name: REGION_SESSION_WINDOW, value: "dev-20261019T191902-e356d1d1:1792451942"} - {name: REGION_M10_MAX_GRANT_LIFETIME, value: "14400"} ports: # QUIC (ADR-0090): UDP only; nothing listens on TCP 7400. diff --git a/docs/STATUS.md b/docs/STATUS.md index b39699f..ea27ef6 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -1,5 +1,20 @@ # Project status and next steps +## Portable retained-plan exact populations - 2026-10-19 + +[ADR-0171](adr/0171-retained-plan-exact-populations.md) adds envelope/batch 3 for +owner 8/rules 10. An explicit no-tick inheritance candidate preserves the exact +membership, policy and sequence. Raw/cohort execution agrees through earned +structural plans, paid children, later selections, suspension, conversion and +recorded unattended intervals. Projection 2 keeps cohort plan references separate +from expressed stock. Old envelopes/traces and all goldens retain their meaning. + +Both full Release suites pass **92/92**. Final focused tests, analysis, formatting +and ASan/UBSan pass; fresh-process MSVC/GCC output bytes agree. +[Validation](engineering/VALIDATION.md) records exact evidence. Durable population +admission, migration, linked resource ownership, control/NPC/UI and operational/ +actual-days/product gates remain open. No default/live version changes. + ## Explicit DEV worker placement - 2026-10-19 [ADR-0170](adr/0170-dev-worker-placement.md) implements the maintainer's worker diff --git a/docs/adr/0171-retained-plan-exact-populations.md b/docs/adr/0171-retained-plan-exact-populations.md new file mode 100644 index 0000000..95432a4 --- /dev/null +++ b/docs/adr/0171-retained-plan-exact-populations.md @@ -0,0 +1,57 @@ +# ADR-0171: Retained-plan exact populations + +## Status and scope + +Accepted portable M12 population integration, 2026-10-19. Extends +ADR-0139/0140/0165/0169. No approximate aggregation, durable admission, automatic +upgrade, cross-region migration, new controller or live/default rule change. + +## Decision + +Population envelope 3 (`AEVOPOP3`) contains exactly owner 8/rules 10. Retain the +existing header/member layout, complete nested biological snapshot, policy and +conversion sequence. Membership still tags whole eligible wildlife groups by +their **expressed** genome. It never groups away or replaces individual offspring +plans, revisions, body/reserve, age, growth or identity. References own no stock. + +An explicit detached no-tick envelope-2/rules-9 inheritance upgrade preserves all +tags, policy and sequence while invoking the exact nested owner upgrade. Old +envelopes retain their precise meaning and reject the new nested owner. Repeated +and skipped upgrades refuse. A raw inherited owner can be wrapped portably; that +does not authorize a database profile transition or create durable ownership. + +Population batch 3 (`AEVOPBT3`) pairs only with envelope 3 and nested raw trace 4. +Keep the existing bounded recorded intentions and exact membership boundaries. +Unattended intervals still require fully suspended controlled cells, frozen-only +controlled intentions and no birth requests or mutation selections. Wildlife +uses its unchanged shared controller during recording and retained intentions +during replay. Real funded wildlife births inherit their stored plans through +the same kernel; tags own no additional organisms or inventory. No controller +rerun, resampling, or restoration-time policy evaluation is introduced. + +Projection 2 additionally supports envelope 3. Tag classification determines the +existing resident and plan-holder kind independently of the expressed/plan genome +row. Raw and cohort observations may share an exact owner/rules/projection cut. +Living plans remain references, dead plans history, and unsupported evidence +never supplies terminal extinction. Projection 1 retains its old domain. + +## Alternatives, consequences and qualification + +Reusing envelope 2 would reinterpret its nested-format contract. Grouping by an +average plan discards inherited diversity; treating plan references as members +invents organisms. Exact retained members avoid both errors, without a speedup +or compression claim. The existing capacity ceiling and disabled policy default +remain; target-host cost is still unqualified. + +Require real earned structural plans and funded children, unchanged expressed +parents, subsequent plan changes, active and suspended raw/cohort equality, +whole-group conversion, exact per-kind census, snapshot/trace restoration, +mid-interval refusal and fresh-process continuation. Mutated outer versions, +wrong raw trace pairings, altered membership boundaries and controlled activity +must refuse without changing the source. Preserve all old goldens. Qualify +Release/NDEBUG, clang-tidy, formatting and relevant ASan/UBSan before committing. + +SQL and checkpoint population mappings remain unchanged. New envelope/trace +readers alone cannot publish or migrate this owner. A later durable segment must +bind its exact tuple, upgrade, trace and recovered result through existing +fences/journals, with faults and retries, before population publication is admitted. diff --git a/docs/adr/README.md b/docs/adr/README.md index 4e633f4..cbed044 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -1,5 +1,7 @@ # Architecture decision records +- [ADR-0171: Retained-plan exact populations](0171-retained-plan-exact-populations.md) - portable envelope/batch 3 preserve every member's expressed/heritable state, exact conversion/policy replay and projection-2 plan counts; durable admission remains separate. + - [ADR-0170: Explicit DEV worker placement](0170-dev-worker-placement.md) - required control-plane/master exclusion for DEV workloads in full and standalone overlays, with rendered-policy regression checks; no live-placement claim. - [ADR-0169: Retained-plan census observations](0169-retained-plan-census-observations.md) - raw owner-8 projection 2 separates expressed resident stock from living offspring-plan references and dead-plan history; verified history and registered capture retain exact boundaries. diff --git a/docs/biology/CELLULAR_POPULATION.md b/docs/biology/CELLULAR_POPULATION.md index e95f743..ef3b45d 100644 --- a/docs/biology/CELLULAR_POPULATION.md +++ b/docs/biology/CELLULAR_POPULATION.md @@ -23,6 +23,15 @@ The model retains exact members and makes no aggregation speedup claim. There is no regional population approximation or biological cross-region migration. The existing 256-life ceiling remains; target-host service capacity is separate. +[ADR-0171](../adr/0171-retained-plan-exact-populations.md) extends the portable +owner to retained-plan rules 10/owner 8 using envelope 3 and population batch 3 +(nested raw trace 4). An explicit envelope-2/rules-9 inheritance candidate retains +tags, policy and sequence. Each member keeps its complete expressed body and +offspring plan; grouping by expressed genome never erases plan differences. +Projection 2 observes cohort plan holders separately from owned stock. Durable +population mappings/admission and biological migration remain separate work; +there is no automatic live upgrade, approximation or compression claim. + M12.3 adds [population envelope 1](../../schemas/cellular-population.md), detached conversion candidates and an opt-in automatic policy (ADR-0140). The default remains disabled. The policy materializes whole genome groups within 1200 um of diff --git a/docs/engineering/M12_REMAINING_GATES.md b/docs/engineering/M12_REMAINING_GATES.md index a3f7409..e70118c 100644 --- a/docs/engineering/M12_REMAINING_GATES.md +++ b/docs/engineering/M12_REMAINING_GATES.md @@ -26,7 +26,10 @@ explicit fenced raw admission, plan selections and repeated funded births with complete source/target recovery. [ADR-0169](../adr/0169-retained-plan-census-observations.md) qualifies raw census projection 2 through committed history and registered snapshots, with separate living plan holders and dead-plan history rather than -invented expressed carriers or stock. Population/migration admission, the +invented expressed carriers or stock. +[ADR-0171](../adr/0171-retained-plan-exact-populations.md) adds portable exact +population envelope/batch 3, retained membership/plan state, raw/cohort equality +and projection-2 cohort counts. Durable population/migration admission, the in-life expression catalog, authority handoff and NPC/UI integration are still required. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 9bc5173..08a672c 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -1,5 +1,57 @@ # Validation report +## 2026-10-19 - Portable retained-plan exact populations + +[ADR-0171](../adr/0171-retained-plan-exact-populations.md) adds explicit envelope +3 and population batch 3/nested raw trace 4. Every member retains its complete +expressed/heritable state; the no-tick inheritance candidate preserves policy, +sequence and tags. Projection 2 separately counts cohort plan references and +expressed stock. Existing checkpoint/SQL population allowlists are unchanged. + +Executed locally: + +- MSVC 19.44 Release full CTest **92/92, 29.63 s** and GCC 14.2 Release full + CTest **92/92, 19.99 s**, with NDEBUG. The final additional conversion and + automatic-policy proofs passed **2/2, 2.82 s** on MSVC and **2/2, 1.80 s** + on GCC. Only these tests changed after the full runs; production sources did not. +- Clang 19.1/clang-tidy 19 new population/restart and census **3/3, 21.17 s**. + Final expanded population/restart tests **2/2, 15.22 s**; format check passed. +- Clang 19.1 ASan/UBSan population/restart, inherited owner and census + **4/4, 108.71 s**, without reports. Final expanded population/restart proofs + **2/2, 48.62 s**, without reports. +- GCC 14.2 persistence consumers rebuilt and the private population/fault suite + passed **37 s total**. Existing raw/cohort, interval, migration and registered + history behavior passes; schema 55 still refuses retained-plan population + publication/profile conversion. No new SQL admission was inferred from readers. + +The fixture creates a real funded legacy child and whole-group cohort before +upgrading. It compares the complete nested state and outcomes with an independent +raw continuation every tick through two earned structural plans, funded children, +offline suspension and 64-tick intervals. Manual conversions encode/replay exact +membership without changing biology; automatic policy with a fully suspended +observer collapses every actual wildlife member and replays the same boundaries. +Census equality after normalizing only the representation category preserves all +per-genome stock and independent plan/dead-history measures. A suspended selected +plan still has one holder and zero expressed stock before birth. + +Tests refuse old/unknown outer versions resealed with a valid checksum, truncated +envelopes, old raw trace pairings, changed membership boundaries, repeated upgrade +and active-controlled unattended admission. Source bytes remain unchanged on +detached preparation/refusal. Fresh-process full-population continuation is exact; +MSVC/GCC expected output shares SHA-256 +`818824b44bb4475177c04492ff07df4b657d3370c90d96cf7ab6ac84f9b1d297`. +An initial test incorrectly used an already populated opening and failed its +empty-input genesis tick. The fixture now explicitly requests an empty opening; +no biological rule or golden was changed to make it pass. + +This retains exact individuals and makes no compression, throughput or target-host +cost claim. Database admission, cross-region migration, producer/resource coupling, +control succession/NPC behavior/UI and actual-days/product qualification remain +separate. No new service, default/DEV opening, live upgrade or World0 operation. +The refreshed population-segment DEV window expires **2026-10-19 23:19:02 UTC**. +Rendered deployment policy passes with 138 objects across 17 overlays and zero +failures. A successful push does not establish deployed reachability. + ## 2026-10-19 - Explicit worker-only DEV scheduling [ADR-0170](../adr/0170-dev-worker-placement.md) applies required negative diff --git a/include/evo/server/lifecycle/cellular_census.hpp b/include/evo/server/lifecycle/cellular_census.hpp index af5779e..27212bd 100644 --- a/include/evo/server/lifecycle/cellular_census.hpp +++ b/include/evo/server/lifecycle/cellular_census.hpp @@ -60,7 +60,7 @@ enum class CellularCensusError : std::uint8_t { // It never owns inventory, changes biology or reports terminal extinction. // ADR-0159: rules 8/owner 6 or 7; rules 9/owner 7 (raw or population envelope 2). // Growth observes retained body/reserve, never an unfunded adult forecast. -// ADR-0169: raw rules 10/owner 8 uses projection 2 with separate plan references. +// ADR-0169/0171: rules 10/owner 8, raw or envelope 3, uses projection 2. class CellularCensus { public: [[nodiscard]] static std::expected diff --git a/include/evo/server/lifecycle/cellular_population.hpp b/include/evo/server/lifecycle/cellular_population.hpp index ea6a4bf..f6f05c6 100644 --- a/include/evo/server/lifecycle/cellular_population.hpp +++ b/include/evo/server/lifecycle/cellular_population.hpp @@ -32,6 +32,7 @@ struct CellularTierPolicy { auto operator<=>(const CellularTierPolicy&) const = default; }; inline constexpr std::size_t cellular_population_header_bytes = 80; +inline constexpr std::uint32_t cellular_population_inheritance_format = 3; inline constexpr std::size_t max_cellular_population_bytes = cellular_population_header_bytes + 16 * biology::max_cellular_tick_lives + max_cellular_lives_bytes + 8; @@ -56,6 +57,9 @@ class CellularPopulation { // ADR-0157: preserve exact membership while upgrading nested biology to rules 9. [[nodiscard]] std::expected, CellularPopulationError> maturation_upgrade_candidate() const; + // ADR-0171: envelope 3 retains all tags and nested expressed/heritable state. + [[nodiscard]] std::expected, CellularPopulationError> + inheritance_upgrade_candidate() const; [[nodiscard]] std::vector snapshot() const; [[nodiscard]] static std::expected, CellularPopulationError> restore(std::span bytes, std::uint64_t world, std::uint64_t region, diff --git a/include/evo/server/lifecycle/cellular_population_batch.hpp b/include/evo/server/lifecycle/cellular_population_batch.hpp index 8a55332..3ee92f2 100644 --- a/include/evo/server/lifecycle/cellular_population_batch.hpp +++ b/include/evo/server/lifecycle/cellular_population_batch.hpp @@ -20,7 +20,8 @@ struct CellularPopulationBatch { CellularBatch biology; std::optional conversion; std::vector boundaries; - std::uint32_t format_version{1}; // 1: envelope 1/owner 6; 2: envelope 2/owner 7. + // 1: envelope 1/owner 6; 2: envelope 2/owner 7; 3: envelope 3/owner 8. + std::uint32_t format_version{1}; bool operator==(const CellularPopulationBatch&) const = default; }; inline constexpr std::size_t population_batch_header_bytes = 136; diff --git a/schemas/cellular-inheritance-upgrade.md b/schemas/cellular-inheritance-upgrade.md index e104fc3..053cc07 100644 --- a/schemas/cellular-inheritance-upgrade.md +++ b/schemas/cellular-inheritance-upgrade.md @@ -40,6 +40,9 @@ ADR-0169 subsequently adds explicit raw [census projection 2](ecology/regional-cellular-census-v2.md), including verified history and registered capture. It separates plan references from expressed resident stock; population and migration support remain separate. +[ADR-0171](../docs/adr/0171-retained-plan-exact-populations.md) subsequently +qualifies portable population envelope/batch 3 and projection-2 cohort counts. +It adds no checkpoint tuple mapping or durable population admission. The portable candidate functions perform no durable publication. [ADR-0168](../docs/adr/0168-durable-retained-plan-admission.md) adds schema 55 diff --git a/schemas/cellular-population-batch.md b/schemas/cellular-population-batch.md index e66fa78..ee2969f 100644 --- a/schemas/cellular-population-batch.md +++ b/schemas/cellular-population-batch.md @@ -81,3 +81,18 @@ magic/version bindings are checked before SQL's parser-local normalization; stored bytes and retry identities are unchanged. Interval admission/settlement and player-operation materialization keep their existing meaning. Paired transfer trace 1 remains unadmitted and this adds no destination activation rights. + +## Portable batch 3 + +[ADR-0171](../docs/adr/0171-retained-plan-exact-populations.md) adds magic +`AEVOPBT3`, version 3, paired only with population envelope 3/owner 8/rules 10. +Keep this header, work bounds and exact membership-boundary layout. Biological +intervals contain raw trace 4, never trace 3. Manual no-tick conversions contain +no raw trace. Fully suspended controlled cells retain frozen-only inputs; birth +requests and mutation selections remain forbidden in unattended batches. + +Recording uses the unchanged wildlife controller and replay uses its retained +intentions. The full nested owner retains plans, real paid offspring, stocks and +history through every exact boundary. Wrong outer/nested trace pairs or altered +boundaries fail without modifying the source. This portable codec adds no SQL +allowlist, durable population tuple, destination activation or automatic upgrade. diff --git a/schemas/cellular-population.md b/schemas/cellular-population.md index 289b7ca..4e7c8a4 100644 --- a/schemas/cellular-population.md +++ b/schemas/cellular-population.md @@ -69,3 +69,32 @@ ADR-0158/schema 53 subsequently admits nested rules 9 as bundle 11. Entry requir the retained bundle-10 same-profile upgrade; ordinary intervals use batch 2 and profile transitions preserve the exact tuple. See [durable maturation](cellular-maturation-upgrade.md). Default entry is unchanged. + +## Portable retained-plan envelope 3 + +[ADR-0171](../docs/adr/0171-retained-plan-exact-populations.md) adds `AEVOPOP3`, +version 3, with the same bounded header/member layout and a complete nested +[owner 8/rules 10](cellular-lives-v8.md). Exact nested plans and their revisions +remain per organism. Whole eligible groups are still tagged by expressed genome; +membership does not replace heterogeneous retained state or own inventory. + +An explicit detached envelope-2/rules-9 inheritance candidate preserves policy, +conversion sequence and every membership tag. It invokes the exact nested +inheritance upgrade without advancing time. Direct portable wrapping of an +inherited raw owner is also possible. Old outer versions cannot label owner 8; +magic, version and nested owner/rules must agree and re-encode exactly. Earlier +envelopes retain their original bytes and meanings. + +Population batch 3 uses `AEVOPBT3`, version 3 and nested raw trace 4; the existing +header, recorded tick intentions and exact representation-boundary layout are +unchanged. No-tick conversions carry no biological trace. Biological intervals +remain bounded by 64 ticks, require fully suspended controlled cells and retain +frozen-only controlled inputs, with no birth requests or mutation selections. +Wildlife executes the unchanged controller while recording and recorded inputs +while replaying. Paid births, development and inherited plans use the same kernel. +Old batch versions and raw trace 3 never replay an envelope-3 biological interval. + +[Census projection 2](ecology/regional-cellular-census-v2.md) distinguishes +cohort/individual stock and plan references without changing biological totals. +No population checkpoint tuple mapping, SQL admission, profile transition, +cross-region transfer or production selector is added by this portable segment. diff --git a/schemas/ecology/regional-cellular-census-v2.md b/schemas/ecology/regional-cellular-census-v2.md index 3fcc298..9dace5e 100644 --- a/schemas/ecology/regional-cellular-census-v2.md +++ b/schemas/ecology/regional-cellular-census-v2.md @@ -4,12 +4,14 @@ Authority: [ADR-0169](../../docs/adr/0169-retained-plan-census-observations.md). This is a detached read-only C++ observation, not a new snapshot/wire codec, SQL journal, permission grant or accepted extinction record. -Projection 2 supports raw owner format 8/rules 10 only. Regional and scoped +Projection 2 supports owner format 8/rules 10, raw or portable population envelope +3 (ADR-0171). Regional and scoped boundaries explicitly include `projection_version=2`; all other identity, frame, tick, quantum, complete genome and disjoint-resident checks from [projection 1](regional-cellular-census-v1.md) still apply. Mixed projection -boundaries refuse the complete set. Population conversion and migration of this -owner remain unsupported. +boundaries refuse the complete set. Portable whole-group representation changes +preserve the complete owner; durable population admission and migration remain +unsupported in this segment. Each genome row retains its five existing resident categories (individual and cohort wildlife, active, lingering and suspended controlled organisms). Counts, diff --git a/src/server/lifecycle/cellular_census.cpp b/src/server/lifecycle/cellular_census.cpp index a9901b1..a145923 100644 --- a/src/server/lifecycle/cellular_census.cpp +++ b/src/server/lifecycle/cellular_census.cpp @@ -56,9 +56,10 @@ CellularCensus::observe(const CellularLives& state, std::span state, std::expected, CellularPopulationError> CellularPopulation::create(std::unique_ptr state, CellularTierPolicy policy) { - if (!state || - (state->rules_version() != cellular_lives_foraging_rules && - !(state->rules_version() == cellular_lives_maturation_rules && - state->format_version() == cellular_lives_migration_format)) || - (state->format_version() != cellular_lives_presence_format && - state->format_version() != cellular_lives_migration_format) || - policy.materialize_radius_um == 0 || + const bool supported = + state && ((state->rules_version() == cellular_lives_foraging_rules && + (state->format_version() == cellular_lives_presence_format || + state->format_version() == cellular_lives_migration_format)) || + (state->rules_version() == cellular_lives_maturation_rules && + state->format_version() == cellular_lives_migration_format) || + (state->rules_version() == cellular_lives_inheritance_rules && + state->format_version() == cellular_lives_inheritance_format)); + if (!supported || policy.materialize_radius_um == 0 || policy.collapse_radius_um <= policy.materialize_radius_um || policy.collapse_radius_um > biology::cellular_motion_axis_limit_um) return std::unexpected(CellularPopulationError::unsupported); @@ -42,6 +44,8 @@ std::uint64_t CellularPopulation::conversion_sequence() const noexcept { return conversion_sequence_; } std::uint32_t CellularPopulation::format_version() const noexcept { + if (state_->format_version() == cellular_lives_inheritance_format) + return cellular_population_inheritance_format; return state_->format_version() == cellular_lives_migration_format ? 2U : 1U; } std::expected, CellularPopulationError> @@ -68,6 +72,18 @@ CellularPopulation::maturation_upgrade_candidate() const { (*candidate)->conversion_sequence_ = conversion_sequence_; return candidate; } +std::expected, CellularPopulationError> +CellularPopulation::inheritance_upgrade_candidate() const { + auto biological = state_->inheritance_upgrade_candidate(); + if (!biological) + return std::unexpected(CellularPopulationError::unsupported); + auto candidate = create(std::move(*biological), policy_); + if (!candidate) + return std::unexpected(candidate.error()); + (*candidate)->members_ = members_; + (*candidate)->conversion_sequence_ = conversion_sequence_; + return candidate; +} std::expected CellularPopulation::collapse(std::uint64_t genome) { auto candidate = members_; diff --git a/src/server/lifecycle/cellular_population_batch.cpp b/src/server/lifecycle/cellular_population_batch.cpp index f525b2d..066e594 100644 --- a/src/server/lifecycle/cellular_population_batch.cpp +++ b/src/server/lifecycle/cellular_population_batch.cpp @@ -6,16 +6,18 @@ namespace evo::server { namespace { -constexpr std::uint64_t magic = 0x315442504F564541ULL; // "AEVOPBT1" -constexpr std::uint64_t migration_magic = 0x325442504F564541ULL; // "AEVOPBT2" +constexpr std::uint64_t magic = 0x315442504F564541ULL; // "AEVOPBT1" +constexpr std::uint64_t migration_magic = 0x325442504F564541ULL; // "AEVOPBT2" +constexpr std::uint64_t inheritance_magic = 0x335442504F564541ULL; // "AEVOPBT3" bool shape(const CellularPopulationBatch& batch, bool recording) { const auto& h = batch.header; const auto zero = [](const auto& bytes) { return std::ranges::all_of(bytes, [](auto byte) { return byte == 0; }); }; - if ((batch.format_version != 1 && batch.format_version != 2) || h.world == 0 || h.region == 0 || - h.frame == 0 || zero(h.predecessor) || zero(h.predecessor_digest) || - h.end_tick < h.begin_tick) + if ((batch.format_version != 1 && batch.format_version != 2 && + batch.format_version != cellular_population_inheritance_format) || + h.world == 0 || h.region == 0 || h.frame == 0 || zero(h.predecessor) || + zero(h.predecessor_digest) || h.end_tick < h.begin_tick) return false; const auto count = batch.conversion ? std::size_t{1} : batch.biology.ticks.size(); if (count == 0 || count > max_cellular_batch_ticks || @@ -26,7 +28,10 @@ bool shape(const CellularPopulationBatch& batch, bool recording) { !batch.biology.ticks.empty() || batch.biology.header != CellularBatchHeader{}) return false; } else { - if (batch.biology.header != h || batch.biology.format_version != cellular_batch_format || + const auto raw_format = batch.format_version == cellular_population_inheritance_format + ? cellular_batch_inheritance_format + : cellular_batch_format; + if (batch.biology.header != h || batch.biology.format_version != raw_format || h.end_tick - h.begin_tick != count || !encode_cellular_batch(batch.biology)) return false; for (const auto& tick : batch.biology.ticks) @@ -78,7 +83,9 @@ class Reader { bool population_interval_safe(const CellularLives& state) noexcept { return (state.rules_version() == cellular_lives_foraging_rules || - state.rules_version() == cellular_lives_maturation_rules) && + state.rules_version() == cellular_lives_maturation_rules || + (state.rules_version() == cellular_lives_inheritance_rules && + state.format_version() == cellular_lives_inheritance_format)) && std::ranges::all_of(state.lives(), [](const auto& life) { return life.account == LifeAccount{} || life.presence == CellularPresence::suspended; }); @@ -96,6 +103,8 @@ unattended_population_inputs(const CellularLives& base, const CellularBatchHeade if (!working) return std::unexpected(CellularPopulationError::format); CellularBatch batch{header, {}}; + if (base.rules_version() == cellular_lives_inheritance_rules) + batch.format_version = cellular_batch_inheritance_format; while ((*working)->tick() < header.end_tick) { auto intents = wildlife_intents(**working, (*working)->tick() + 1); if (!intents) @@ -164,7 +173,7 @@ execute_population_batch(const CellularPopulation& base, CellularPopulationBatch if (intent == input.lives.end() || intent->intent != frozen) return std::unexpected(CellularPopulationError::ineligible); } - CellularBatch one{batch.biology.header, {input}, cellular_batch_format}; + CellularBatch one{batch.biology.header, {input}, batch.biology.format_version}; one.header.begin_tick = (*candidate)->state().tick(); one.header.end_tick = one.header.begin_tick + 1; auto biology = replay_cellular_batch((*candidate)->state(), one); @@ -229,7 +238,9 @@ encode_population_batch(const CellularPopulationBatch& batch) { biological = std::move(*encoded); } std::vector out; - put(out, batch.format_version == 2 ? migration_magic : magic); + put(out, batch.format_version == cellular_population_inheritance_format ? inheritance_magic + : batch.format_version == 2 ? migration_magic + : magic); put(out, batch.format_version, 4); put(out, batch.conversion ? 1 : 0, 4); const auto& h = batch.header; @@ -264,7 +275,8 @@ decode_population_batch(std::span bytes) { const auto saved_magic = in.get(); const auto version = in.get(4); if (!((saved_magic == magic && version == 1) || - (saved_magic == migration_magic && version == 2))) + (saved_magic == migration_magic && version == 2) || + (saved_magic == inheritance_magic && version == cellular_population_inheritance_format))) return std::unexpected(CellularPopulationError::unsupported); const auto kind = in.get(4); CellularPopulationBatch batch; diff --git a/src/server/lifecycle/cellular_population_codec.cpp b/src/server/lifecycle/cellular_population_codec.cpp index 4684ed9..739588e 100644 --- a/src/server/lifecycle/cellular_population_codec.cpp +++ b/src/server/lifecycle/cellular_population_codec.cpp @@ -5,8 +5,9 @@ namespace evo::server { namespace { -constexpr std::uint64_t magic = 0x31504F504F564541ULL; // "AEVOPOP1" -constexpr std::uint64_t migration_magic = 0x32504F504F564541ULL; // "AEVOPOP2" +constexpr std::uint64_t magic = 0x31504F504F564541ULL; // "AEVOPOP1" +constexpr std::uint64_t migration_magic = 0x32504F504F564541ULL; // "AEVOPOP2" +constexpr std::uint64_t inheritance_magic = 0x33504F504F564541ULL; // "AEVOPOP3" std::uint64_t checksum(std::span bytes) { std::uint64_t hash = 14695981039346656037ULL; for (const auto byte : bytes) @@ -39,7 +40,11 @@ std::vector CellularPopulation::snapshot() const { const auto biology = state_->snapshot(); std::vector bytes; bytes.reserve(cellular_population_header_bytes + members_.size() * 16 + biology.size() + 8); - put(bytes, format_version() == 2 ? migration_magic : magic, 8); + put(bytes, + format_version() == cellular_population_inheritance_format ? inheritance_magic + : format_version() == 2 ? migration_magic + : magic, + 8); put(bytes, format_version(), 4); put(bytes, policy_.automatic ? 1 : 0, 4); put(bytes, policy_.materialize_radius_um, 4); @@ -68,10 +73,12 @@ CellularPopulation::restore(std::span bytes, std::uint64_t w return std::unexpected(CellularPopulationError::checksum); Reader reader(bytes); const auto saved_magic = reader.get(8); - if (saved_magic != magic && saved_magic != migration_magic) + if (saved_magic != magic && saved_magic != migration_magic && saved_magic != inheritance_magic) return std::unexpected(CellularPopulationError::format); const auto version = reader.get(4); - if ((saved_magic == magic && version != 1) || (saved_magic == migration_magic && version != 2)) + if ((saved_magic == magic && version != 1) || + (saved_magic == migration_magic && version != 2) || + (saved_magic == inheritance_magic && version != cellular_population_inheritance_format)) return std::unexpected(CellularPopulationError::unsupported); const auto flags = reader.get(4); if (flags > 1) diff --git a/tests/cellular_inheritance_tests.cpp b/tests/cellular_inheritance_tests.cpp index 55136d4..1da2da9 100644 --- a/tests/cellular_inheritance_tests.cpp +++ b/tests/cellular_inheritance_tests.cpp @@ -217,9 +217,8 @@ void codecs_and_siblings() { require(!CellularLives::restore(flipped, state->world(), state->region(), state->frame(), state->tick()), "checksum damage accepted"); - require(!CellularPopulation::create(restore(*state)) && - !CellularMigration::prepare(*state, *state, 100), - "unsupported old adapter admitted plans"); + require(!CellularMigration::prepare(*state, *state, 100), + "unsupported migration adapter admitted plans"); CellularBatch old_trace; old_trace.header = {state->world(), state->region(), state->frame(), {}, {}, state->tick(), state->tick() + 1}; diff --git a/tests/cellular_plan_population_tests.cpp b/tests/cellular_plan_population_tests.cpp new file mode 100644 index 0000000..bb30fac --- /dev/null +++ b/tests/cellular_plan_population_tests.cpp @@ -0,0 +1,308 @@ +#include "evo/server/lifecycle/cellular_census_set.hpp" +#include "evo/server/lifecycle/cellular_population_batch.hpp" +#include "fixtures/cellular_ecology.hpp" + +#include +#include + +namespace { +using namespace evo; +using namespace evo::server; +using namespace evo::fixture; +using Population = std::unique_ptr; +std::vector controlled(const CellularLives& state, bool frozen = false, + bool bud = false) { + std::vector result; + for (const auto& life : state.lives()) + if (life.account != LifeAccount{}) { + CellularLifeIntent input{life.identity.life, {}}; + input.intent.frozen = frozen; + input.intent.bud = bud && !frozen; + result.push_back(input); + } + return result; +} +void paired(CellularPopulation& population, CellularLives& raw, bool frozen = false, + bool bud = false, std::span selections = {}) { + auto inputs = controlled(raw, frozen, bud); + auto all = checked(wildlife_intents(raw, raw.tick() + 1), "paired wildlife"); + all.insert(all.end(), inputs.begin(), inputs.end()); + std::ranges::sort(all, {}, &CellularLifeIntent::life_id); + const auto expected = + checked(raw.advance_tick_with_mutations(all, selections), "raw plan tick"); + const auto actual = checked(population.advance_tick(inputs, selections), "cohort plan tick"); + require(actual.mutations == expected.mutations && actual.buds == expected.buds && + actual.growth == expected.growth && + population.state().snapshot() == raw.snapshot() && + raw.current_total() == raw.opening_total(), + "cohort changed inherited biology or conserved inventory"); +} +Population legacy() { + auto state = opening(7, true, 1, false); + checked(state->advance_tick({}), "population source clock"); + LifeAccount account{}; + account.back() = 1; + state = checked(state->birth_candidate({account, 1}, {100, 100, 100, 100, {state->tick()}}), + "population source founder"); + state = checked(state->migration_upgrade_candidate(), "source boundary upgrade"); + state = checked(state->maturation_upgrade_candidate(), "source maturation upgrade"); + for (unsigned tick = 0; tick < 30'000 && state->lives().size() == 1; ++tick) + checked(state->advance_tick(controlled(*state, false, true)), "fund legacy bud"); + require(state->lives().size() > 1, "no real legacy wildlife for retained tags"); + auto result = checked(CellularPopulation::create(std::move(state), {false, 900, 1600}), + "legacy exact population"); + checked(result->collapse(100), "legacy whole-group collapse"); + return result; +} +void choose(CellularPopulation& population, CellularLives& raw, std::uint64_t successor) { + for (unsigned tick = 0; tick < 60'000; ++tick) { + const auto life = raw.lives().front(); + require(life.identity.life == 100, "plan parent died before selection"); + if (!life.mutation.offer) { + paired(population, raw); + continue; + } + const auto& offer = *life.mutation.offer; + const auto found = std::ranges::find_if(offer.candidates, [](const auto& candidate) { + return candidate.target == biology::Capability::membrane || + candidate.target == biology::Capability::target_size; + }); + const bool structural = found != offer.candidates.end(); + const CellularMutationSelection selection{ + 100, life.mutation.event_index, life.mutation.offer_tick, successor, + structural ? found->operator_id : std::uint16_t{0}}; + paired(population, raw, false, false, std::span{&selection, 1}); + if (structural) { + require(raw.lives().front().identity.genome == 100 && + raw.lives().front().offspring_genome == successor, + "cohort selection rewrote the expressed parent"); + return; + } + } + throw std::runtime_error("finite food did not earn structural cohort plan"); +} +void census(const CellularPopulation& population, const CellularLives& raw) { + const auto individual = checked(CellularCensus::observe(raw), "raw plan census"); + const auto cohort = checked(CellularCensus::observe(population), "cohort plan census"); + require(cohort.boundary().projection_version == 2 && cohort.boundary().population_format == 3 && + cohort.rows().size() == individual.rows().size(), + "cohort lost its inherited census boundary"); + for (std::size_t row = 0; row < cohort.rows().size(); ++row) { + auto expected = individual.rows()[row]; + auto actual = cohort.rows()[row]; + auto& stock = actual.totals.resident[0]; + const auto& tagged = actual.totals.resident[1]; + stock.count += tagged.count; + stock.body.energy.value += tagged.body.energy.value; + stock.reserve.energy.value += tagged.reserve.energy.value; + for (std::size_t channel = 0; channel < stock.body.matter.size(); ++channel) { + stock.body.matter[channel].value += tagged.body.matter[channel].value; + stock.reserve.matter[channel].value += tagged.reserve.matter[channel].value; + } + actual.totals.resident[1] = {}; + actual.totals.living_plan_holders[0] += actual.totals.living_plan_holders[1]; + actual.totals.living_plan_holders[1] = 0; + require(actual == expected, "cohort census changed stock or inherited-plan references"); + } +} +Population family() { + auto source = legacy(); + const auto bytes = source->snapshot(); + const auto tags = + std::vector{source->members().begin(), source->members().end()}; + const auto sequence = source->conversion_sequence(); + auto population = + checked(source->inheritance_upgrade_candidate(), "tagged inheritance upgrade"); + require(source->snapshot() == bytes && population->format_version() == 3 && + population->policy() == source->policy() && + population->conversion_sequence() == sequence && + std::ranges::equal(population->members(), tags) && + !population->inheritance_upgrade_candidate() && + population->state().current_total() == source->state().current_total(), + "population upgrade reset representation or biology"); + auto raw = checked(source->state().inheritance_upgrade_candidate(), "independent raw upgrade"); + require(raw->snapshot() == population->state().snapshot(), + "nested upgrade changed raw biology"); + choose(*population, *raw, 1'000'000); + census(*population, *raw); + require(!population_interval_safe(*raw), + "active controlled parent entered unattended interval"); + bool born = false; + for (unsigned tick = 0; tick < 30'000 && !born; ++tick) { + paired(*population, *raw, false, true); + born = std::ranges::any_of(raw->lives(), [](const auto& life) { + return life.parent_life == 100 && life.identity.genome == 1'000'000; + }); + } + require(born, "cohort parent did not fund inherited child"); + const auto biology = population->state().snapshot(); + auto tagged = + checked(population->conversion_candidate(1'000'000, true), "tag inherited children"); + require(population->state().snapshot() == biology && tagged->state().snapshot() == biology && + tagged->conversion_sequence() == population->conversion_sequence() + 1, + "whole-group conversion changed inherited biology"); + population = std::move(tagged); + census(*population, *raw); + const auto conversion_base = population->snapshot(); + CellularBatchHeader cut{raw->world(), raw->region(), raw->frame(), {}, + {}, raw->tick(), raw->tick()}; + cut.predecessor[0] = 1; + cut.predecessor_digest[0] = 2; + auto conversion = checked(prepare_population_conversion(*population, cut, {1'000'000, false}), + "recorded inherited conversion"); + auto recorded = checked(decode_population_batch(checked( + encode_population_batch(conversion.batch), "conversion encode")), + "conversion decode"); + require(recorded.format_version == 3 && population->snapshot() == conversion_base && + checked(replay_population_batch(*population, recorded), "conversion replay") + ->snapshot() == conversion.state->snapshot() && + conversion.state->state().snapshot() == raw->snapshot(), + "recorded inherited conversion changed biology or representation result"); + recorded.format_version = 2; + require(!replay_population_batch(*population, recorded), + "legacy conversion trace admitted envelope 3"); + population = std::move(conversion.state); + checked(population->collapse(1'000'000), "retag inherited children after conversion"); + choose(*population, *raw, 500'000); + census(*population, *raw); + for (unsigned tick = 0; tick <= cellular_linger_ticks; ++tick) + paired(*population, *raw, true); + require(population_interval_safe(*raw), "suspended inherited owner refused interval"); + census(*population, *raw); + const auto observed = checked(CellularCensus::observe(*population), "suspended cohort census"); + const auto plan = + checked(observed.query(500'000, CellularCensusScope::exact_genome), "unborn plan query"); + require(plan.resident == std::array{} && + plan.living_plan_holders[static_cast( + CellularCensusKind::suspended_controlled)] == 1, + "suspension erased inherited plan or invented expressed stock"); + return population; +} +CellularBatchHeader header(const CellularLives& state, unsigned ticks) { + CellularBatchHeader result{state.world(), state.region(), state.frame(), {}, {}, + state.tick(), state.tick() + ticks}; + result.predecessor[0] = 1; + result.predecessor_digest[0] = 2; + return result; +} +void intervals(CellularPopulation& population) { + const auto before = population.snapshot(); + const auto h = header(population.state(), 64); + const auto inputs = + checked(unattended_population_inputs(population.state(), h), "plan interval inputs"); + require(inputs.format_version == cellular_batch_inheritance_format, + "wrong inherited raw trace"); + auto executed = checked(prepare_population_batch(population, inputs), "inherited interval"); + auto trace = checked(decode_population_batch( + checked(encode_population_batch(executed.batch), "plan trace encode")), + "plan trace decode"); + require(trace.format_version == 3 && population.snapshot() == before && + checked(replay_population_batch(population, trace), "plan interval replay") + ->snapshot() == executed.state->snapshot(), + "inherited interval replay or source mutation"); + auto raw = restore(population.state()); + for (unsigned tick = 0; tick < 64; ++tick) + paired(population, *raw, true); + require(population.snapshot() == executed.state->snapshot(), + "recorded inherited interval differs from raw/control execution"); + census(population, *raw); + auto automatic = + checked(CellularPopulation::create(restore(population.state()), {true, 900, 1600}), + "automatic inherited population"); + const auto auto_inputs = + checked(unattended_population_inputs(automatic->state(), header(automatic->state(), 64)), + "automatic inherited inputs"); + const auto auto_run = + checked(prepare_population_batch(*automatic, auto_inputs), "automatic inherited interval"); + require( + checked(replay_population_batch(*automatic, auto_run.batch), "automatic inherited replay") + ->snapshot() == auto_run.state->snapshot(), + "automatic inherited membership replay differs"); + auto auto_raw = restore(automatic->state()); + for (unsigned tick = 0; tick < 64; ++tick) + paired(*automatic, *auto_raw, true); + const auto wildlife = std::ranges::count_if( + auto_raw->lives(), [](const auto& life) { return life.account == LifeAccount{}; }); + require(wildlife > 0 && automatic->members().size() == static_cast(wildlife) && + automatic->snapshot() == auto_run.state->snapshot(), + "automatic policy did not retain all real inherited wildlife"); + const auto untouched = population.snapshot(); + trace.biology.format_version = cellular_batch_format; + require(!encode_population_batch(trace) && !replay_population_batch(population, trace) && + population.snapshot() == untouched, + "old trace admitted inherited biology"); + trace = executed.batch; + ++trace.boundaries.front().sequence; + require(!replay_population_batch(*checked(CellularPopulation::restore(before, h.world, h.region, + h.frame, h.begin_tick), + "unaltered interval base"), + trace), + "altered inherited membership boundary accepted"); + const auto restored = + checked(CellularPopulation::restore(population.snapshot(), h.world, h.region, h.frame, + population.state().tick()), + "inherited population restore"); + require(restored->snapshot() == population.snapshot(), + "retained envelope did not re-encode exactly"); +} +void codec_boundaries(const CellularPopulation& population) { + const auto put = [](std::vector& bytes, std::size_t offset, std::uint64_t value, + unsigned width) { + for (unsigned i = 0; i < width; ++i) + bytes.at(offset + i) = static_cast(value >> (8U * i)); + }; + auto bytes = population.snapshot(); + const auto& state = population.state(); + for (const auto version : {1U, 2U, 4U}) { + auto false_version = bytes; + false_version.at(7) = static_cast('0' + version); + put(false_version, 8, version, 4); + std::uint64_t hash = 14695981039346656037ULL; + for (const auto byte : std::span{false_version}.first(false_version.size() - 8)) + hash = (hash ^ byte) * 1099511628211ULL; + put(false_version, false_version.size() - 8, hash, 8); + require(!CellularPopulation::restore(false_version, state.world(), state.region(), + state.frame(), state.tick()), + "inherited owner relabeled with old/unknown envelope"); + } + bytes.pop_back(); + require(!CellularPopulation::restore(bytes, state.world(), state.region(), state.frame(), + state.tick()), + "truncated retained envelope accepted"); +} +void save(const char* path, std::span bytes) { + std::ofstream output(path, std::ios::binary | std::ios::trunc); + output.write(reinterpret_cast(bytes.data()), + static_cast(bytes.size())); + require(output.good(), "retained population write"); +} +} // namespace +int main(int argc, char** argv) try { + if (argc == 4 && std::string_view{argv[1]} == "--resume") { + std::ifstream input(argv[2], std::ios::binary); + require(input.good(), "retained population read"); + const std::vector bytes{std::istreambuf_iterator{input}, {}}; + require(bytes.size() >= cellular_population_header_bytes + 8, "retained restart header"); + std::uint64_t tick{}; + for (unsigned i = 0; i < 8; ++i) + tick |= std::uint64_t{bytes[56 + i]} << (8U * i); + auto population = checked(CellularPopulation::restore(bytes, 207, 1, 1, tick), + "fresh inherited population"); + intervals(*population); + save(argv[3], population->snapshot()); + return 0; + } + require(argc == 1 || (argc == 4 && std::string_view{argv[1]} == "--checkpoint"), + "unknown population plan arguments"); + auto population = family(); + codec_boundaries(*population); + if (argc == 4) + save(argv[2], population->snapshot()); + intervals(*population); + if (argc == 4) + save(argv[3], population->snapshot()); + return 0; +} catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; +}