From dc7c0e1fa10ddb8c8087a779e8f9f5ba1196ab66 Mon Sep 17 00:00:00 2001 From: dingo Date: Mon, 19 Oct 2026 15:06:47 -0400 Subject: [PATCH] feat(ecology): distinguish inherited plans in verified census --- CMakeLists.txt | 5 + README.md | 3 + .../region/dev-session-route/listener.yaml | 2 +- docs/STATUS.md | 15 ++ .../0169-retained-plan-census-observations.md | 55 +++++ docs/adr/README.md | 2 + docs/biology/REPRODUCTION_MODEL.md | 13 +- docs/engineering/M12_REMAINING_GATES.md | 5 +- docs/engineering/VALIDATION.md | 59 ++++++ .../persistence/cellular_census_snapshot.hpp | 2 +- .../evo/server/lifecycle/cellular_census.hpp | 6 + .../server/lifecycle/cellular_census_set.hpp | 3 +- schemas/cellular-inheritance-upgrade.md | 4 + .../ecology/regional-cellular-census-v2.md | 50 +++++ src/persistence/cellular_census_snapshot.cpp | 8 +- src/server/lifecycle/cellular_census.cpp | 35 +++- src/server/lifecycle/cellular_census_set.cpp | 9 +- tests/cellular_inheritance_tests.cpp | 2 +- tests/cellular_plan_census_tests.cpp | 188 ++++++++++++++++++ tests/population_publication_tests.cpp | 49 +++++ 20 files changed, 492 insertions(+), 23 deletions(-) create mode 100644 docs/adr/0169-retained-plan-census-observations.md create mode 100644 schemas/ecology/regional-cellular-census-v2.md create mode 100644 tests/cellular_plan_census_tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e4766f..b4a26f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -740,6 +740,11 @@ if(BUILD_TESTING) add_test(NAME cellular-population-transitions COMMAND evo-cellular-population-tests) set_tests_properties(cellular-population-transitions PROPERTIES TIMEOUT 180) add_executable(evo-cellular-census-tests tests/cellular_census_tests.cpp) + add_executable(evo-cellular-plan-census-tests tests/cellular_plan_census_tests.cpp) + target_link_libraries(evo-cellular-plan-census-tests PRIVATE evo::lifecycle) + evo_owned_target(evo-cellular-plan-census-tests) + add_test(NAME cellular-retained-plan-census COMMAND evo-cellular-plan-census-tests) + set_tests_properties(cellular-retained-plan-census PROPERTIES TIMEOUT ${evo_cohort_timeout}) target_link_libraries(evo-cellular-census-tests PRIVATE evo::lifecycle) evo_owned_target(evo-cellular-census-tests) add_test(NAME cellular-census-contract COMMAND evo-cellular-census-tests) diff --git a/README.md b/README.md index 518b2d9..3e7dea5 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,9 @@ process fixtures remain separate from organism biology. [Regional cellular census](schemas/ecology/regional-cellular-census-v1.md) provides 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 +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 journal. [Scoped multi-region queries](schemas/ecology/scoped-cellular-census-set-v1.md) diff --git a/deploy/kubernetes/region/dev-session-route/listener.yaml b/deploy/kubernetes/region/dev-session-route/listener.yaml index b14ec92..aec1ca9 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-20261019T190400-b82838f2:1792451040"} + - {name: REGION_SESSION_WINDOW, value: "dev-20261019T190609-a14a1852:1792451169"} - {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 d698c5e..d49f86f 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -1,5 +1,20 @@ # Project status and next steps +## Retained-plan census and verified history - 2026-10-19 + +[ADR-0169](adr/0169-retained-plan-census-observations.md) adds raw owner-8 census +projection 2. Expressed organisms/body/reserve retain their existing meaning; +separate living plan references preserve inherited diversity before any offspring +is born. Dead-plan records remain history. Exact/descendant/scoped queries and +committed history/registered snapshots retain the explicit projection boundary. +No extra inventory, redundant SQL journal or terminal extinction rule is added. + +MSVC/GCC Release pass **90/90**. Clang-tidy, formatting, focused ASan/UBSan and +actual database history/capture checks pass; [validation](engineering/VALIDATION.md) +records scope. Old projection-1 plan measures remain unobserved. Population and +migration integration, in-life trait timing, birth-time control, NPC behavior, +client UI and remaining target-host/ecology/product gates still prevent M12 closure. + ## Durable retained offspring plans - 2026-10-19 [ADR-0168](adr/0168-durable-retained-plan-admission.md)/schema 55 adds explicit diff --git a/docs/adr/0169-retained-plan-census-observations.md b/docs/adr/0169-retained-plan-census-observations.md new file mode 100644 index 0000000..838ec6a --- /dev/null +++ b/docs/adr/0169-retained-plan-census-observations.md @@ -0,0 +1,55 @@ +# ADR-0169: Retained-plan census observations + +## Status and scope + +Accepted read-only M12.6/12.7 integration for raw owner 8/rules 10, 2026-10-19. +Extends ADR-0150/0159 and ADR-0165/0168. No new biological rule, population or +migration admission, client permission, viable-store completeness or extinction +acceptance policy. + +## Decision + +Add census projection 2 for raw retained-plan owners. Keep projection 1 and its +old observed values for rules 8/9. Record the projection version in regional and +scoped boundaries; mixed projections/bodies/rules fail the complete set. Existing +recorded heads still require full canonical recovery and complete immutable +genome/resident identity checks before observation. + +Resident organism counts, body and reserve remain grouped by expressed genome +and their existing five presence/representation kinds. Add separate per-kind +counts of living organisms retaining each offspring plan. These references own +no inventory and are not extra organisms or expressed archive carriers. Include +suspended controlled holders: logout cannot erase the retained plan. Also count +local corpse plan records separately as history, never reproductive stock. + +Exact and descendant queries sum each measure independently with checked bounds. +An expressed parent can contribute resident stock to genome A and a plan +reference to genome B; do not add these counts together as population. A genome +with no expressed carrier can still have a living plan holder. A holder count is +not a guarantee of current reproductive capability, funded birth or survival. +Dead-plan and retained-nonresident history do not supply current viability. + +Projection-1 plan fields are unobserved (zero defaults), distinguished by the +boundary version; do not interpret them as measured absence. Projection 2 uses +the exact new owner references/history. All other unsupported owner/profile +combinations still refuse. No terminal extinction API is added. + +Verified regional history and scoped/registered snapshots propagate the explicit +projection boundary. Missing, stale, false or mixed evidence fails the entire +query; there is no skip/zero fallback. No additional SQL census journal or source +of authority is introduced. Cross-domain/other-store viability and durable +accepted reconciliation remain subsequent M12.7 work. + +## Alternatives, consequences and qualification + +Treating a plan as an expressed carrier invents stock. Ignoring it can falsely +erase heritable diversity. Changing projection 1 silently loses compatibility. +Separate versioned reference counts preserve both biological and accounting +meanings at bounded observation cost. + +Require unexpressed selected plans with zero inventory/carriers, funded children, +later plans leaving existing children unchanged, logout/resume, terminal death +and retained dead references. Verify exact/descendant/scoped sums, restore and +detachment, old projection controls and complete inventory closure. Qualify the +new observations through actual committed replay/history and registered snapshot +capture; preserve old projection values and all historical goldens. diff --git a/docs/adr/README.md b/docs/adr/README.md index 2ead323..75268bf 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -1,5 +1,7 @@ # Architecture decision records +- [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. + - [ADR-0168: Durable raw retained-plan admission](0168-durable-retained-plan-admission.md) - schema 55, explicit raw bundle-11 -> 12 fenced upgrades and trace-4 mutation/birth publication with exact recovery; no automatic opening or population admission. - [ADR-0167: Explicit retained-plan checkpoint upgrade](0167-retained-plan-checkpoint-upgrade.md) - portable raw-only bundle 12 and exact no-tick bundle-11 source transformation; SQL/durable admission and population support remain separate. diff --git a/docs/biology/REPRODUCTION_MODEL.md b/docs/biology/REPRODUCTION_MODEL.md index b5bb6ef..36cbd66 100644 --- a/docs/biology/REPRODUCTION_MODEL.md +++ b/docs/biology/REPRODUCTION_MODEL.md @@ -48,13 +48,16 @@ earlier children keep their original form. Save/reload retains plan selections and dead-parent history, and logout freezes the controlled parent's biology. This scoped mode edits inherited plans only. The future operator catalog still -needs to distinguish traits that can also change during an existing life. Existing -durable bundles, census, population and migration refuse the new owner until -their plan semantics are qualified. [Trace 4](../../schemas/cellular-region-batch-v4.md) +needs to distinguish traits that can also change during an existing life. +Population and migration still refuse the new owner. [Trace 4](../../schemas/cellular-region-batch-v4.md) supplies portable replay and inherited-offer action matching; old traces refuse this owner. [Bundle 12](../../schemas/cellular-inheritance-upgrade.md) supplies -its portable raw checkpoint and exact retained-source upgrade; SQL publication -is still separate. The birth-time control choice, +its raw checkpoint and exact retained-source upgrade. ADR-0168/schema 55 now +admits explicit fenced raw upgrades, durable plan selections/births and full +recovery. [Census projection 2](../../schemas/ecology/regional-cellular-census-v2.md) +separates living plan references from expressed bodies/reserves and retains dead +plan records as history. A plan reference owns no stock and promises no funded +offspring or survival. The birth-time control choice, persistent NPC behavior and UI remain unimplemented; default/DEV rules do not opt into this owner. diff --git a/docs/engineering/M12_REMAINING_GATES.md b/docs/engineering/M12_REMAINING_GATES.md index a25d220..33d0b7c 100644 --- a/docs/engineering/M12_REMAINING_GATES.md +++ b/docs/engineering/M12_REMAINING_GATES.md @@ -23,7 +23,10 @@ behavior remains. [ADR-0167](../adr/0167-retained-plan-checkpoint-upgrade.md) de the portable raw checkpoint tuple and exact retained-source upgrade. [ADR-0168](../adr/0168-durable-retained-plan-admission.md) adds schema 55, explicit fenced raw admission, plan selections and repeated funded births with -complete source/target recovery. Population/migration/census admission, the +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 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 36f80b9..137c0aa 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -1,5 +1,64 @@ # Validation report +## 2026-10-19 - Retained-plan census projection 2 and committed observations + +[ADR-0169](../adr/0169-retained-plan-census-observations.md) and the +[projection contract](../../schemas/ecology/regional-cellular-census-v2.md) +separate living plan holders and dead-plan history from expressed organisms and +inventory. Regional/scoped boundaries carry the projection version. Existing +history and registered capture fully reconstruct their cuts before observation; +no new SQL schema or census journal is added. + +Executed locally: + +- MSVC 19.44 Release full CTest **90/90, 23.07 s**, outside the filesystem + sandbox. The first resumed sandbox run passed 88/90 and failed its two + hard-link fixtures with access denied; the unrestricted full rerun passed. +- GCC 14.2 Release full CTest **90/90, 17.84 s**, with NDEBUG. +- Clang 19.1/clang-tidy 19 focused projection test **1/1, 6.92 s** and repository + format check passed. +- Clang 19.1 ASan/UBSan retained-plan owner, new census and legacy census + **3/3, 89.62 s**, without reports. +- Clang 19.1/clang-tidy 19 private database population suite **56 s total**; + Clang ASan/UBSan equivalent **114 s total**, without reports. Both include + the new committed-history/registered-capture integration and the existing + raw/cohort, interval, atomic migration and registered coverage/fault controls. +- GCC 14.2 persistence consumers rebuilt and the same private population suite + passed **38 s total**, including both committed plan-census observations. + +Tests earn a structural offspring plan through actual feeding/selection. Before +birth the plan has zero expressed carriers/stock and one living holder. Real +funded births express the plan; a subsequent lower-ID plan leaves earlier +children unchanged. Exact and descendant measures, per-kind sums, local corpse +history, actual death, logout/suspension, detached observations and restore +retain their separate meanings. The total resident count/body/reserve equals +the biological owner and total living holders equals living local organisms. +Two-region queries reject a mixed projection boundary. Legacy projection 1 +retains old values and unobserved zero-default plan fields; no golden changes. + +The database fixture selects a real plan, publishes result 4 and reconstructs +the exact selected head through history and registered capture. After repeated +births and replacement authority it repeats the comparison with direct +observation. Same-tick upgrade history retains projection 2 and its original +head/tick. An unavailable retained build refuses the complete registered query. +Generic historical replay, coverage and migration evidence checks remain active. + +These are read-only engineering observations. No plan-holder count promises +funded reproduction, survival or global viable-store completeness. Population, +migration, other stores/clone domains, accepted extinction/freshness, client +permissions and target-host query costs remain separate work. No player UI, +actual-days ecology, live deployment or M12 completion is claimed. + +Visual review used the existing 2026-10-17 `build/m12-ui-review` renders of the +compact HUD and evolution panel; these are retained review artifacts, not newly +captured screens. The current evolution panel describes changing the existing +cell. Retained-plan selection needs an explicit versioned presentation before +player exposure; this segment changes no UI or live/default biological opening. + +The census push refreshes the DEV window to **2026-10-19 23:06:09 UTC**. Its +exact staged deployment policy passes with 138 objects rendered across 17 +overlays and zero failures. This is no assertion of deployed reachability. + ## 2026-10-19 - Durable raw retained-plan admission [ADR-0168](../adr/0168-durable-retained-plan-admission.md)/schema 55 adds the diff --git a/include/evo/persistence/cellular_census_snapshot.hpp b/include/evo/persistence/cellular_census_snapshot.hpp index 9f8da41..5da7ef5 100644 --- a/include/evo/persistence/cellular_census_snapshot.hpp +++ b/include/evo/persistence/cellular_census_snapshot.hpp @@ -70,7 +70,7 @@ struct CellularCensusSnapshotHooks { // Idle connected session only. Requests: 1..64, nonzero strictly ascending regions // in one world; all stores nonnull. Capture current heads in a short READ ONLY // REPEATABLE READ transaction, finish it, then fully replay each immutable cut. -// All cuts must share frame/tick/owner/rules/quantum. No partial census on error. +// All cuts must share frame/tick/owner/rules/quantum/projection. No partial census. // Per-I/O timeout 1..30000 ms; no automatic retry or whole-query latency promise. // Metadata failures roll back; failed cleanup or an exception closes the session. // Caller-declared coverage, NOT world/branch completeness or live authority. diff --git a/include/evo/server/lifecycle/cellular_census.hpp b/include/evo/server/lifecycle/cellular_census.hpp index f888ed3..af5779e 100644 --- a/include/evo/server/lifecycle/cellular_census.hpp +++ b/include/evo/server/lifecycle/cellular_census.hpp @@ -11,6 +11,7 @@ namespace evo::server { inline constexpr std::uint32_t cellular_census_projection_version = 1; +inline constexpr std::uint32_t cellular_plan_census_projection_version = 2; enum class CellularCensusKind : std::uint8_t { individual_wildlife, cohort_wildlife, @@ -28,6 +29,9 @@ struct CellularCensusTotals { std::array resident{}; // Historical observations only: no owned inventory/current viability claim. std::uint64_t retained_nonresident_records{}; + // Projection 2: references only, never additional organisms or inventory. + std::array living_plan_holders{}; + std::uint64_t retained_dead_plan_records{}; bool operator==(const CellularCensusTotals&) const = default; }; struct CellularCensusRow { @@ -41,6 +45,7 @@ struct CellularCensusBoundary { std::uint64_t world{}, region{}, frame{}, tick{}; std::uint32_t owner_format{}, rules{}, population_format{}; // Zero: raw owner. std::uint64_t quantum_microseconds{biology::cellular_motion_tick_microseconds}; + std::uint32_t projection_version{cellular_census_projection_version}; bool operator==(const CellularCensusBoundary&) const = default; }; enum class CellularCensusScope : std::uint8_t { exact_genome, descendants, invalid = 255 }; @@ -55,6 +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. class CellularCensus { public: [[nodiscard]] static std::expected diff --git a/include/evo/server/lifecycle/cellular_census_set.hpp b/include/evo/server/lifecycle/cellular_census_set.hpp index b350ec7..c228c55 100644 --- a/include/evo/server/lifecycle/cellular_census_set.hpp +++ b/include/evo/server/lifecycle/cellular_census_set.hpp @@ -15,6 +15,7 @@ struct CellularCensusSetBoundary { std::uint64_t world{}, frame{}, tick{}; std::uint32_t owner_format{}, rules{}; std::uint64_t quantum_microseconds{biology::cellular_motion_tick_microseconds}; + std::uint32_t projection_version{cellular_census_projection_version}; bool operator==(const CellularCensusSetBoundary&) const = default; }; enum class CellularCensusSetError : std::uint8_t { @@ -30,7 +31,7 @@ class CellularCensusSet { public: // required_regions must be nonzero, strictly ascending and contain 1..64 IDs. // Exactly one observation per required region, at the exact common boundary. - // Raw/cohort classification may differ; owner format/rules/quantum may not. + // Raw/cohort classification may differ; owner/rules/quantum/projection may not. [[nodiscard]] static std::expected observe(CellularCensusSetBoundary, std::span required_regions, std::span observations); diff --git a/schemas/cellular-inheritance-upgrade.md b/schemas/cellular-inheritance-upgrade.md index 1f92afa..e104fc3 100644 --- a/schemas/cellular-inheritance-upgrade.md +++ b/schemas/cellular-inheritance-upgrade.md @@ -36,6 +36,10 @@ No population envelope maps to bundle 12. Profile transitions, population successors/restore, census and migration remain unsupported for this owner in the portable upgrade segment. Missing or unsupported projections cannot establish zero life or extinction. +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. 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/ecology/regional-cellular-census-v2.md b/schemas/ecology/regional-cellular-census-v2.md new file mode 100644 index 0000000..3fcc298 --- /dev/null +++ b/schemas/ecology/regional-cellular-census-v2.md @@ -0,0 +1,50 @@ +# Retained-plan cellular census projection 2 + +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 +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. + +Each genome row retains its five existing resident categories (individual and +cohort wildlife, active, lingering and suspended controlled organisms). Counts, +body and reserve use the organism's **expressed** genome. Projection 2 adds: + +| Field | Meaning | Owned inventory | +|---|---|---| +| `living_plan_holders[kind]` | Living local organisms whose retained offspring genome is this row | None | +| `retained_dead_plan_records` | Local corpses retaining this offspring genome | None | + +A parent with expressed genome A and selected plan B contributes its one body +and reserve to A and one reference to B. It does not contribute a second +organism, expressed archive carrier or future child. Each funded newborn is a +real additional resident under its own expressed genome and initially retains +that same plan. A later parent selection does not rewrite earlier children. +Suspension retains a living plan reference without offline progression; death +removes that living reference and retains a separate historical record. + +Exact-genome and descendant-closure queries independently sum stock and each +reference measure with overflow checks. Descendant closure includes the anchor, +visits shared descendants once and does not assume ancestry follows ID order. +Never sum reference counts into organism counts or inventory. A living plan +holder is not proof of reproductive capability, funded birth or survival. A dead +record is not current viable stock. Unknown genome or unavailable evidence +returns an error, never measured zero or terminal extinction. + +Projection 1 remains valid for its supported old owners. Its added C++ fields +default to zero and are **unobserved**, not measured absence. Inspect the +projection boundary before interpreting them. No old golden or retained +biological format changes. + +Committed history reconstructs each exact manifest/payload/trace cut before +observing it. Registered snapshots capture domain, registry and heads in their +existing read-only database snapshot, fully replay the selected cuts, propagate +the projection boundary and validate the whole declared set. Missing builds, +objects, stale authority or false evidence refuse the entire query. No extra +census write or publication window is introduced. Other stores, clone domains, +global viability reconciliation and client authorization remain separate work. diff --git a/src/persistence/cellular_census_snapshot.cpp b/src/persistence/cellular_census_snapshot.cpp index 0e05622..14bc8f7 100644 --- a/src/persistence/cellular_census_snapshot.cpp +++ b/src/persistence/cellular_census_snapshot.cpp @@ -225,10 +225,10 @@ load_snapshot(Postgres& db, std::uint64_t world, observations.push_back(std::move(page->entries.front().census)); } const auto& first = observations.front().boundary(); - auto census = server::CellularCensusSet::observe({first.world, first.frame, first.tick, - first.owner_format, first.rules, - first.quantum_microseconds}, - required, observations); + auto census = server::CellularCensusSet::observe( + {first.world, first.frame, first.tick, first.owner_format, first.rules, + first.quantum_microseconds, first.projection_version}, + required, observations); if (!census) return std::unexpected(Failure{Error::census, 0, {}, {}, census.error()}); return RegisteredCellularCensusSnapshot{std::move(cuts->catalog), diff --git a/src/server/lifecycle/cellular_census.cpp b/src/server/lifecycle/cellular_census.cpp index 983d020..a9901b1 100644 --- a/src/server/lifecycle/cellular_census.cpp +++ b/src/server/lifecycle/cellular_census.cpp @@ -21,12 +21,14 @@ bool add(accounting::Inventory& target, const accounting::Inventory& value) { return true; } bool add(CellularCensusTotals& target, const CellularCensusTotals& value) { - if (!add(target.retained_nonresident_records, value.retained_nonresident_records)) + 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(stock.count, source.count) || !add(stock.body, source.body) || + 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; } @@ -54,14 +56,19 @@ CellularCensus::observe(const CellularLives& state, std::span(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 nonresident_genomes; nonresident_genomes.reserve(nonresident.size()); diff --git a/src/server/lifecycle/cellular_census_set.cpp b/src/server/lifecycle/cellular_census_set.cpp index 5b99a9d..55d527d 100644 --- a/src/server/lifecycle/cellular_census_set.cpp +++ b/src/server/lifecycle/cellular_census_set.cpp @@ -20,12 +20,14 @@ bool add(accounting::Inventory& target, const accounting::Inventory& value) { return true; } bool add(CellularCensusTotals& target, const CellularCensusTotals& value) { - if (!add(target.retained_nonresident_records, value.retained_nonresident_records)) + 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(stock.count, source.count) || !add(stock.body, source.body) || + 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; } @@ -57,7 +59,8 @@ CellularCensusSet::observe(CellularCensusSetBoundary boundary, if (cut.region != required[i]) return std::unexpected(Error::coverage); if (boundary != CellularCensusSetBoundary{cut.world, cut.frame, cut.tick, cut.owner_format, - cut.rules, cut.quantum_microseconds}) + cut.rules, cut.quantum_microseconds, + cut.projection_version}) return std::unexpected(Error::boundary); for (const auto& genome : census.genomes_) genomes.push_back(&genome); diff --git a/tests/cellular_inheritance_tests.cpp b/tests/cellular_inheritance_tests.cpp index 5ba800e..55136d4 100644 --- a/tests/cellular_inheritance_tests.cpp +++ b/tests/cellular_inheritance_tests.cpp @@ -217,7 +217,7 @@ void codecs_and_siblings() { require(!CellularLives::restore(flipped, state->world(), state->region(), state->frame(), state->tick()), "checksum damage accepted"); - require(!CellularPopulation::create(restore(*state)) && !CellularCensus::observe(*state) && + require(!CellularPopulation::create(restore(*state)) && !CellularMigration::prepare(*state, *state, 100), "unsupported old adapter admitted plans"); CellularBatch old_trace; diff --git a/tests/cellular_plan_census_tests.cpp b/tests/cellular_plan_census_tests.cpp new file mode 100644 index 0000000..c2bd4f9 --- /dev/null +++ b/tests/cellular_plan_census_tests.cpp @@ -0,0 +1,188 @@ +#include "evo/server/lifecycle/cellular_census_set.hpp" +#include "fixtures/cellular_ecology.hpp" + +namespace { +using namespace evo; +using namespace evo::server; +using namespace evo::fixture; +using Kind = CellularCensusKind; +using Scope = CellularCensusScope; +std::size_t at(Kind kind) { + return static_cast(kind); +} +std::vector inputs(const CellularLives& state, bool bud = false, + bool frozen = false, bool move = false) { + std::vector result; + for (const auto& life : state.lives()) { + CellularLifeIntent input{life.identity.life, {}}; + if (life.account != LifeAccount{}) { + input.intent.bud = bud && !frozen; + input.intent.frozen = frozen; + if (move && !frozen) + input.intent.movement = {1000, 0}; + } + result.push_back(input); + } + return result; +} +Owner regional(std::uint64_t region) { + auto state = opening(7, true, 1, false, region); + checked(state->advance_tick({}), "regional census clock"); + LifeAccount account{}; + account.back() = static_cast(region); + const auto id = region * 100; + state = checked(state->birth_candidate({account, 1}, {id, id, id, id, {state->tick()}}), + "census founder"); + state = checked(state->migration_upgrade_candidate(), "census boundary"); + state = checked(state->maturation_upgrade_candidate(), "census maturation"); + return state; +} +void choose(Owner& state, std::uint64_t successor) { + for (unsigned tick = 0; tick < 60'000; ++tick) { + const auto cell = state->lives().front(); + if (cell.mutation.offer) { + const auto& offer = *cell.mutation.offer; + const auto structural = + std::ranges::find_if(offer.candidates, [](const auto& candidate) { + return candidate.target == biology::Capability::membrane || + candidate.target == biology::Capability::target_size; + }); + const CellularMutationSelection selection{ + cell.identity.life, cell.mutation.event_index, cell.mutation.offer_tick, successor, + structural == offer.candidates.end() ? std::uint16_t{0} : structural->operator_id}; + checked(state->advance_tick_with_mutations(inputs(*state), std::span{&selection, 1}), + "census plan selection"); + if (structural != offer.candidates.end()) + return; + } else + checked(state->advance_tick(inputs(*state)), "census earned offer"); + } + throw std::runtime_error("finite food did not earn census structural plan"); +} +CellularCensusTotals query(const CellularCensus& census, std::uint64_t genome) { + return checked(census.query(genome, Scope::exact_genome), "plan census query"); +} +void verify(const CellularLives& state) { + const auto before = state.snapshot(); + const auto census = checked(CellularCensus::observe(state), "plan census observation"); + require(census.boundary().projection_version == 2 && state.snapshot() == before, + "wrong projection or changed owner"); + Inventory body{}, reserve{}; + std::uint64_t residents = 0, holders = 0, dead = 0; + for (const auto& row : census.rows()) { + for (std::size_t kind = 0; kind < cellular_census_kind_count; ++kind) { + const auto& stock = row.totals.resident[kind]; + residents += stock.count; + holders += row.totals.living_plan_holders[kind]; + body.energy.value += stock.body.energy.value; + reserve.energy.value += stock.reserve.energy.value; + for (std::size_t channel = 0; channel < body.matter.size(); ++channel) { + body.matter[channel].value += stock.body.matter[channel].value; + reserve.matter[channel].value += stock.reserve.matter[channel].value; + } + } + dead += row.totals.retained_dead_plan_records; + } + Inventory actual_body{}, actual_reserve{}; + for (const auto& life : state.lives()) { + actual_body.energy.value += life.body.energy.value; + actual_reserve.energy.value += life.reserve.energy.value; + for (std::size_t channel = 0; channel < body.matter.size(); ++channel) { + actual_body.matter[channel].value += life.body.matter[channel].value; + actual_reserve.matter[channel].value += life.reserve.matter[channel].value; + } + } + require(residents == state.lives().size() && holders == state.lives().size() && + dead == state.corpses().size() && body == actual_body && reserve == actual_reserve, + "plan references invented organisms/inventory or lost holder history"); + require(std::ranges::equal( + census.rows(), + checked(CellularCensus::observe(*restore(state)), "restored census").rows()), + "restore changed plan observation"); +} +void family_and_history() { + auto state = regional(1); + const auto old = checked(CellularCensus::observe(*state), "old projection"); + require(old.boundary().projection_version == 1 && + query(old, 100).living_plan_holders == std::array{}, + "old projection silently gained plan measures"); + state = checked(state->inheritance_upgrade_candidate(), "inherited census upgrade"); + choose(state, 1'000'000); + const auto unexpressed = checked(CellularCensus::observe(*state), "unexpressed plan"); + const auto plan = query(unexpressed, 1'000'000); + require(plan.living_plan_holders[at(Kind::active_controlled)] == 1 && + plan.resident == std::array{} && + checked(state->archive().living_carriers(1'000'000), "unborn archive carriers") == + 0, + "unexpressed retained plan was erased or counted as tissue"); + const auto descendants = + checked(unexpressed.query(100, Scope::descendants), "plan descendants"); + require(descendants.resident[at(Kind::active_controlled)].count == 1 && + descendants.living_plan_holders[at(Kind::active_controlled)] == 1, + "ancestry query double-counted the parent's plan as another organism"); + bool born = false; + for (unsigned tick = 0; tick < 30'000 && !born; ++tick) { + checked(state->advance_tick(inputs(*state, true)), "census funded child"); + born = std::ranges::any_of(state->lives(), + [](const auto& life) { return life.parent_life == 100; }); + } + require(born, "census family had no funded child"); + verify(*state); + choose(state, 500'000); // Sparse descendant IDs need not follow ancestry order. + verify(*state); + for (unsigned tick = 0; tick <= cellular_linger_ticks; ++tick) + checked(state->advance_tick(inputs(*state, false, true)), "census suspended plan"); + const auto suspended = checked(CellularCensus::observe(*state), "suspended plan observation"); + require(query(suspended, 500'000).living_plan_holders[at(Kind::suspended_controlled)] == 1 && + query(unexpressed, 1'000'000).living_plan_holders[at(Kind::active_controlled)] == 1, + "logout erased a plan or rewrote detached history"); + verify(*state); + bool died = false; + for (unsigned tick = 0; tick < 100'000 && !died; ++tick) { + checked(state->advance_tick(inputs(*state, false, false, true)), "census ecological death"); + died = std::ranges::any_of(state->corpses(), + [](const auto& corpse) { return corpse.life_id == 100; }); + } + require(died, "census parent did not die after leaving finite food"); + verify(*state); + const auto dead = query(checked(CellularCensus::observe(*state), "dead plan census"), 500'000); + require(dead.retained_dead_plan_records == 1 && + dead.living_plan_holders == std::array{} && + dead.resident == std::array{}, + "dead plan history became current viability or stock"); +} +void scoped() { + auto a = regional(1), b = regional(2); + a = checked(a->inheritance_upgrade_candidate(), "scope A upgrade"); + b = checked(b->inheritance_upgrade_candidate(), "scope B upgrade"); + const auto ca = checked(CellularCensus::observe(*a), "scope A"); + const auto cb = checked(CellularCensus::observe(*b), "scope B"); + const auto& cut = ca.boundary(); + CellularCensusSetBoundary boundary{cut.world, + cut.frame, + cut.tick, + cut.owner_format, + cut.rules, + cut.quantum_microseconds, + cut.projection_version}; + const std::array required{1, 2}; + const std::array observations{cb, ca}; + const auto set = + checked(CellularCensusSet::observe(boundary, required, observations), "scoped plans"); + require(set.boundary().projection_version == 2 && + checked(set.query(100, Scope::exact_genome), "scope founder A") == query(ca, 100) && + checked(set.query(200, Scope::descendants), "scope founder B") == query(cb, 200), + "scoped plan observation lost independent measures"); + boundary.projection_version = 1; + require(!CellularCensusSet::observe(boundary, required, observations), + "mixed projection admitted"); +} +} // namespace +int main() try { + family_and_history(); + scoped(); + return 0; +} catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; +} diff --git a/tests/population_publication_tests.cpp b/tests/population_publication_tests.cpp index 1cdb562..026d7ea 100644 --- a/tests/population_publication_tests.cpp +++ b/tests/population_publication_tests.cpp @@ -3750,6 +3750,7 @@ void retained_plan_admission() { reload(); require(f.raw->snapshot() == expected && f.head.head.end_tick == before.head.end_tick, "durable plan upgrade changed biology or time"); + const auto upgraded_cut = f.head; require(!migrate_cellular_inheritance_profile(*f.db, *f.objects, f.lease, f.head, f.id(), f.build, 5s) && !migrate_cellular_to_population(*f.db, *f.objects, f.lease, f.head, f.id(), f.build, @@ -3879,6 +3880,53 @@ void retained_plan_admission() { const auto selected_bytes = f.raw->snapshot(); reload(); require(f.raw->snapshot() == selected_bytes, "selected plan did not recover exactly"); + const auto census_cut = [&](bool unexpressed) { + const auto direct = take(CellularCensus::observe(*f.raw), "direct retained-plan census"); + const auto history = + take(load_cellular_census_history(*f.db, *f.objects, f.key, f.lease, f.head.head, + f.head.head.sequence, 1, builds, 5s), + "committed retained-plan census"); + require(history.entries.size() == 1 && + history.entries.front().committed.head == f.head.head && + history.entries.front().census.boundary().projection_version == 2 && + std::ranges::equal(history.entries.front().census.rows(), direct.rows()), + "committed history changed retained-plan observations"); + const std::array requests{ + CellularCensusRegionRequest{f.key.region, f.lease, f.objects.get()}}; + const auto registered = + take(load_registered_cellular_census_snapshot(*f.db, f.key.world, requests, builds, 5s), + "registered retained-plan census"); + const auto& snapshot = registered.snapshot; + const auto totals = + take(direct.query(reservation.successor_identity, CellularCensusScope::exact_genome), + "retained-plan exact census"); + require(registered.catalog.size() == 1 && registered.migrations.empty() && + snapshot.cuts.size() == 1 && snapshot.cuts.front().head == f.head.head && + snapshot.census.boundary().projection_version == 2 && + take(snapshot.census.query(reservation.successor_identity, + CellularCensusScope::exact_genome), + "registered retained-plan query") == totals, + "registered capture lost plan measures or exact cut binding"); + if (unexpressed) + require(totals.resident == std::array{} && + totals.living_plan_holders[static_cast( + CellularCensusKind::active_controlled)] == 1, + "unexpressed committed plan was erased or counted as biomass"); + const auto upgrade_history = + take(load_cellular_census_history(*f.db, *f.objects, f.key, f.lease, f.head.head, + upgraded_cut.head.sequence, 1, builds, 5s), + "retained upgrade census history"); + require(upgrade_history.entries.size() == 1 && + upgrade_history.entries.front().committed.head == upgraded_cut.head && + upgrade_history.entries.front().census.boundary().projection_version == 2 && + upgrade_history.entries.front().census.boundary().tick == before.head.end_tick, + "same-tick upgrade history lost its projection boundary"); + const std::array unavailable{}; + require(!load_registered_cellular_census_snapshot(*f.db, f.key.world, requests, unavailable, + 5s), + "unavailable retained build became a partial or zero census"); + }; + census_cut(true); std::size_t born = 0; for (unsigned tries = 0; tries < 500 && born < 2; ++tries) { step(64, true); @@ -3905,6 +3953,7 @@ void retained_plan_admission() { "stale plan writer escaped fence"); reload(); require(f.raw->snapshot() == final_bytes, "replacement changed inherited family"); + census_cut(false); std::puts("retained-plan upgrade, choices, births, exact recovery and faults passed"); } } // namespace