From 82568da9cd24f75a63d529251b28a5f1cd42bc71 Mon Sep 17 00:00:00 2001 From: dingo Date: Sun, 18 Oct 2026 15:11:51 -0400 Subject: [PATCH] M12.6: add scoped multi-region cellular census queries --- CMakeLists.txt | 1 + README.md | 6 +- docs/STATUS.md | 16 ++ docs/adr/0152-scoped-cellular-census-sets.md | 60 +++++++ docs/adr/README.md | 2 + docs/architecture/PERSISTENCE_ARCHITECTURE.md | 7 + docs/biology/ECOLOGICAL_IDENTITY_AND_TIME.md | 7 + docs/engineering/M12_DELIVERY_PLAN.md | 2 +- docs/engineering/M12_REMAINING_GATES.md | 9 +- docs/engineering/TEST_STRATEGY.md | 11 ++ docs/engineering/VALIDATION.md | 56 ++++++ .../evo/server/lifecycle/cellular_census.hpp | 5 + .../server/lifecycle/cellular_census_set.hpp | 55 ++++++ .../ecology/regional-cellular-census-v1.md | 5 + .../ecology/scoped-cellular-census-set-v1.md | 41 +++++ src/server/lifecycle/cellular_census.cpp | 2 + src/server/lifecycle/cellular_census_set.cpp | 102 +++++++++++ tests/cellular_census_tests.cpp | 164 ++++++++++++++++++ tests/cellular_migration_tests.cpp | 41 +++++ tests/population_publication_tests.cpp | 33 ++++ 20 files changed, 621 insertions(+), 4 deletions(-) create mode 100644 docs/adr/0152-scoped-cellular-census-sets.md create mode 100644 include/evo/server/lifecycle/cellular_census_set.hpp create mode 100644 schemas/ecology/scoped-cellular-census-set-v1.md create mode 100644 src/server/lifecycle/cellular_census_set.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 166594d..5fa63df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -90,6 +90,7 @@ add_library(evo_lifecycle STATIC src/server/lifecycle/primitive_lives.cpp src/server/lifecycle/cellular_migration.cpp src/server/lifecycle/cellular_migration_trace.cpp src/server/lifecycle/cellular_census.cpp + src/server/lifecycle/cellular_census_set.cpp src/server/lifecycle/cellular_batch.cpp src/server/lifecycle/wildlife_controller.cpp src/server/lifecycle/cellular_play_summary.cpp src/server/lifecycle/cellular_population.cpp src/server/lifecycle/cellular_population_codec.cpp src/server/lifecycle/cellular_population_policy.cpp diff --git a/README.md b/README.md index c84653d..bb52483 100644 --- a/README.md +++ b/README.md @@ -134,8 +134,10 @@ read-only local resident/presence/cohort and exact/descendant observations from current cellular owners. Retained departed history is excluded from owned stock. [Committed regional history](schemas/ecology/committed-cellular-history-v1.md) now derives bounded, fully replayed census pages from the existing atomic biological -journal. Complete world/branch coverage, client queries and terminal extinction -remain later gates. +journal. [Scoped multi-region queries](schemas/ecology/scoped-cellular-census-set-v1.md) +require exact declared coverage/boundaries, consistent immutable genomes and +disjoint resident identities before summing. Complete world/branch reconciliation, +client queries and terminal extinction remain later gates. `apps/evo-sim` owns CLI/file I/O and reporting. `tests` holds contract, fuzz and measurement programs. `src/server/lifecycle` owns funded primitive diff --git a/docs/STATUS.md b/docs/STATUS.md index 087835e..84cc0cb 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -1,5 +1,21 @@ # Project status and next steps +## M12.6c scoped multi-region census queries - 2026-10-18 + +The portable census now queries 1..64 explicitly declared regions at an exact +simulation boundary. Missing/stale coverage, conflicting immutable genome records +and duplicate resident organisms fail before totals are exposed. Real settled +migrations count stock once, retain suspended living stock and support mixed +raw/cohort classifications; departed history remains separate record occurrences. + +Native Release passes **79/79**; focused GCC and ASan/UBSan contracts, private +PostgreSQL committed raw/cohort history integration, Clang analysis and formatting +pass. [Evidence and limits](engineering/VALIDATION.md) retain durable world/branch +coverage, reconciled migration cuts, client access and target-host query costs. +A caller-declared region set is not a complete world watermark and cannot accept +terminal extinction. Linked ecology, maturation and later integration/soak gates +remain open; no deployment or World0 action. + ## M12.6b replayed census history from committed cuts - 2026-10-18 Regional census history now reads exact retained biological commits as canonical diff --git a/docs/adr/0152-scoped-cellular-census-sets.md b/docs/adr/0152-scoped-cellular-census-sets.md new file mode 100644 index 0000000..18a9c3b --- /dev/null +++ b/docs/adr/0152-scoped-cellular-census-sets.md @@ -0,0 +1,60 @@ +# ADR-0152: Scoped multi-region cellular census queries + +## Status + +Accepted for M12.6c, 2026-10-18. Portable detached queries under ADR-0133; +no world-completeness, branch, authority or terminal-extinction acceptance. + +## Context and decision + +Regional history pages now bind census observations to exact biological commits. +Adding local totals without checking coverage, identity or resident duplication +could still hide a stale region or count a migration source and ghost twice. + +Add `CellularCensusSet` to the portable lifecycle library. The caller declares +1..64 distinct, ascending, nonzero required region IDs and an exact world, frame, +tick, owner format, rule and quantum boundary. Require exactly one matching +observation per region. Raw/population classifications may differ because both +describe the same exact individuals. All other boundary fields must agree. +The 64-region bound limits query work; it selects no production world size. + +Keep full immutable genome records and permanent resident organism IDs privately +in detached regional projections. Across regions require byte-equivalent genome +values for every shared genome identity, including origin, capabilities and +parents; a short content hash or matching ancestry alone is insufficient. +Reject duplicate resident organism IDs even when life or genome IDs differ. +Retained nonresident records do not participate in resident duplication checks. + +Retain sorted detached regional observations, then answer exact or descendant +queries with checked channel/count sums. Each local archive retains complete +ancestry, so a locally unknown anchor contributes no carriers only after exact +declared coverage and shared-genome consistency have been checked. Unknown in +every archive remains an error. Nonresident sums are historical record occurrences, +not a count of unique organisms or evidence of their current remote viability. +Any invalid evidence refuses construction; it cannot become a partial/zero total. + +## Alternatives, benefits, costs and risks + +Blind summation conceals missing regions and repeated residents. Using only an +archive's known-alive records counts departed migrants again. Trusting a short +digest would permit conflicting immutable identities. Full records add bounded +memory/copy cost to every detached census; the set retains at most 64 observations +with up to 4,096 genomes and 256 residents each. Identity checks sort bounded +genome references/resident IDs; queries traverse each local ancestry graph once. +This is not a measured deployment latency or capacity guarantee. + +The required list is caller-declared, not an authoritative durable world catalog. +Observations themselves confer no commit, branch, epoch or ownership authority. +Callers must keep verified commit references and exact selected cuts; same-tick +arbitrary cuts can still miss a transfer and are not a reconciled watermark. +No new persistence/wire format, cache/index, permissions endpoint or client UI. + +## Consequences and qualification + +Require direct stock oracles, disjoint/shared/unknown ancestry, exact coverage, +all boundary fields, invalid/over-bound requests, immutable genome conflicts, +duplicate permanent organism identities, detachment and real funded migration +before/after. Test raw/cohort committed migration observations together. +Durable branch identity, required owner coverage and atomic migration-cut +reconciliation remain M12.6/12.7 work. No query here accepts global absence or +terminal extinction; linked resources, maturation and product gates remain open. diff --git a/docs/adr/README.md b/docs/adr/README.md index 990f2d9..0f2b2ab 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -1,5 +1,7 @@ # Architecture decision records +- [ADR-0152: Scoped multi-region cellular census queries](0152-scoped-cellular-census-sets.md) - bounded declared coverage, exact boundaries, complete shared-genome equality and disjoint resident identities; no world-completeness or extinction authority. + - [ADR-0151: Committed cellular census history](0151-committed-cellular-census-history.md) - derives bounded, exact-cut census pages from the existing atomic immutable biological journal, with full historical replay; no duplicate census writes or world-extinction authority. - [ADR-0150: Regional cellular census projection](0150-regional-cellular-census-projection.md) - bounded local resident/representation/presence census and exact/descendant queries; retained nonresident history never counts as owned stock or world extinction. diff --git a/docs/architecture/PERSISTENCE_ARCHITECTURE.md b/docs/architecture/PERSISTENCE_ARCHITECTURE.md index 450fe89..03cff4c 100644 --- a/docs/architecture/PERSISTENCE_ARCHITECTURE.md +++ b/docs/architecture/PERSISTENCE_ARCHITECTURE.md @@ -1,5 +1,12 @@ # Persistence and region authority +M12.6c's portable [scoped census set](../../schemas/ecology/scoped-cellular-census-set-v1.md) +combines a bounded declared set of regional observations, including replayed +history entries. It verifies exact common boundaries, complete shared genome +records and disjoint resident organism IDs. Callers retain the verified commit +references separately. Declared coverage is not a durable world catalog, and +same-tick cuts are not by themselves a reconciled migration watermark. + M12.6b derives regional census history from the existing immutable biological commit journal. A commit and its exact payload are the canonical event, already atomic with the head; no duplicate census-write window or new SQL table is added. diff --git a/docs/biology/ECOLOGICAL_IDENTITY_AND_TIME.md b/docs/biology/ECOLOGICAL_IDENTITY_AND_TIME.md index b3b471d..b30ef69 100644 --- a/docs/biology/ECOLOGICAL_IDENTITY_AND_TIME.md +++ b/docs/biology/ECOLOGICAL_IDENTITY_AND_TIME.md @@ -15,6 +15,13 @@ binds those projections to exact retained biological commits with full replay. The commit journal supplies atomic durable events without a second census write. Its fixed regional cut still supplies no complete world or external branch proof. +M12.6c's [scoped census set](../../schemas/ecology/scoped-cellular-census-set-v1.md) +checks exact declared regional coverage/boundaries, shared immutable genomes and +disjoint resident organism identities before summing exact/descendant queries. +It refuses missing/stale/conflicting evidence. The list remains caller-declared; +neither a durable world catalog nor reconciled migration cuts or branch proof +is supplied, so this query cannot accept world absence or terminal extinction. + ## Identity and classification An organism has a permanent identity and terminal death. An immutable genome has diff --git a/docs/engineering/M12_DELIVERY_PLAN.md b/docs/engineering/M12_DELIVERY_PLAN.md index 54eec74..c309aa5 100644 --- a/docs/engineering/M12_DELIVERY_PLAN.md +++ b/docs/engineering/M12_DELIVERY_PLAN.md @@ -13,7 +13,7 @@ Commit and push each validated scoped segment separately. | 12.3 | Exact population profile/trace/store and opt-in host integration implemented (12.3b) | ADR-0142, schema 44; exact publication/recovery, mutation race guards, ambiguous replies and process kills. Executable/network population fault drills and target-host qualification remain open. | | 12.4 | Owned intervals and durable paired migration/fault qualification implemented in scoped segments through 12.4l | ADR-0143–0149/schema 49; bounded admitted work, source freeze, staged ghost, atomic pair and exact recovery, local TLS and storage faults. Multi-host/orchestrator, linked ecology and integrated stock audits remain open. | | 12.5 | Blocked on 12.2-12.4 | Finite M6 process fixtures do not qualify coupled persistent cellular/cohort atmosphere/water budgets. | -| 12.6 | Regional census/lineage queries and replayed commit-bound history pages implemented (12.6a/b) | ADR-0150/0151; existing atomic biological commits are the durable events. Fixed regional watermarks and full replay bind observations; world/branch context, cross-region query/index costs and client path remain required. | +| 12.6 | Regional census/lineage queries, replayed commit-bound history pages and scoped multi-region queries implemented (12.6a-c) | ADR-0150–0152; atomic biological commits are the durable events. Fixed regional cuts and full replay bind observations; declared sets reject missing/stale/conflicting identity or duplicate residents. Durable world/branch coverage, reconciled migration cuts, target-host query/index costs and client path remain required. | | 12.7 | Evidence policy defined; reconciler open | Complete durable world watermark and every viable/in-flight store remain required. | | 12.8 | Not qualified | Actual multi-day integrated ecology/restart/clone evidence remains required. | | 12.9 | Not accepted | Returning-player product evidence and expansion review remain required; World0 stays separately blocked. | diff --git a/docs/engineering/M12_REMAINING_GATES.md b/docs/engineering/M12_REMAINING_GATES.md index 1075ad2..7a44c12 100644 --- a/docs/engineering/M12_REMAINING_GATES.md +++ b/docs/engineering/M12_REMAINING_GATES.md @@ -27,7 +27,7 @@ or authorize production enablement. | 12.3 | Dedicated population profile/trace/store/owning host; exact recovery and private PostgreSQL fault tests | Population-specific executable/network fault drills and target-host publication/capacity qualification. | | 12.4 | Owned intervals; exact transfer/accounting; durable atomic pair/recovery; independent loopback TLS path loss and owner expiry/replacement/heal; local object sync/read-failure matrix | Multi-host/kernel-network, storage-host and orchestrator drills, linked-boundary contracts and integrated ownership/stock audits at scale. | | 12.5 | Finite cellular contact feeding/decay and separate M6 producer/consumer resource owner | One jointly owned cellular/environment budget, explicit effective depth and source entitlements, versioned water/atmosphere species mapping and producer coefficients, plus closed/open and nutrient-exhaustion comparisons. | -| 12.6 | Retained archive, bounded regional census/lineage queries and fully replayed census history pages from atomic biological commits (12.6a/b) | World/branch history context and cross-region query/index qualification, client permission/presentation path and deployment-host query costs; no account power or invented precise statistical ancestry. | +| 12.6 | Retained archive, regional census/lineage queries, replayed commit-bound history and bounded scoped multi-region queries (12.6a-c) | Durable world/branch history context and required-region coverage, reconciled migration cuts, target-host query/index qualification and client permission/presentation; no account power or invented precise statistical ancestry. | | 12.7 | ADR-0133's viability/unknown semantics | Durable region-set/watermark reconciliation covering every owned representation, suspended/viable stock and migration escrow. Missing/stale regions must prevent terminal extinction. | | 12.8 | Deterministic seed/regime drivers and separate-process exact continuation | Reproducible integrated worlds running for actual days, real restarts/clone comparisons and retained accounting/diversity/cost audits. Accelerated tests cannot supply elapsed days. | | 12.9 | M11 feedback and current UI review retained | Returning-player samples on the integrated ecology and explicit product/3D readiness decision after soak evidence. No M12 product acceptance or World0 promotion is inferred. | @@ -107,6 +107,13 @@ is not an external branch identity or complete world watermark. World-level history context/queries, client presentation and M12.7 reconciliation remain open; neither primitive accepts terminal extinction. +M12.6c adds [scoped multi-region queries](../../schemas/ecology/scoped-cellular-census-set-v1.md) +over 1..64 explicitly declared regions. Exact coverage and simulation boundaries, +complete shared-genome identity and disjoint resident organism IDs are checked +before querying. Nonresident historical records add no living stock. A required +list supplied by a caller is still not durable world coverage; equal ticks alone +cannot prove a consistent migration cut or branch. This does not close M12.7. + Current expansion readiness: **no-go for M13 integration or World0 promotion** while these M12 integration/evidence gates remain open. This records the present engineering evidence, not a substitute for the later maintainer product review. diff --git a/docs/engineering/TEST_STRATEGY.md b/docs/engineering/TEST_STRATEGY.md index 058e351..d50481d 100644 --- a/docs/engineering/TEST_STRATEGY.md +++ b/docs/engineering/TEST_STRATEGY.md @@ -1,5 +1,16 @@ # Validation strategy +M12.6c's scoped census-set contract compares declared-region queries with direct +resident count/body/reserve oracles. Require missing/extra/duplicate regions, +every boundary field, unknown/invalid scope, conflicting immutable successor IDs, +repeated permanent resident IDs, detached observations and the 64-region work +bound. Real funded transfers must preserve stock across the combined set while +source-plus-incoming-copy observations fail. Committed raw/cohort histories +must also compose after full recovery. A refused set yields no partial/zero +total. Native Release, Linux Release, relevant ASan/UBSan, private population +history integration and Clang analysis/format qualify this read-only change. +No global coverage or terminal-extinction claim follows from these checks. + M12.6b's private PostgreSQL history tests require exact fixed-watermark pagination after new no-tick publications, preserved genesis/profile/migration cuts, unchanged heads/stock and repeated event identity. Invalid limits, false cuts/regions/frames, diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 51bbb93..d5f12c6 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -1,5 +1,61 @@ # Validation report +## 2026-10-18 - M12.6c scoped multi-region census queries + +[ADR-0152](../adr/0152-scoped-cellular-census-sets.md) adds a portable detached +query over exactly 1..64 declared regions at one exact simulation boundary. +Missing/extra/duplicate regions, mismatched boundaries, conflicting complete +immutable genome records and duplicate permanent resident organism IDs refuse +construction. Queries preserve resident categories and checked count/body/reserve +sums. Unknown anchors remain errors; retained zero-carrier ancestry can report +scoped zero. Nonresident history is counted only as record occurrences. + +Executed locally: + +- MSVC **19.44.35217.0** Release full CTest **79/79 passed**, **14.56 s**; + final census rebuild/contract passed after the analysis-only test correction. +- GCC 14 Release census/migration contracts **2/2 passed**, **2.48 s**. +- Clang 19 Linux **ASan/UBSan**, same contracts **2/2 passed**, **66.57 s**, + without sanitizer reports. +- Optional GCC persistence/process targets rebuilt; private PostgreSQL population + qualification **exit 0**, **19 s** (15 s population phase). Committed raw/cohort + paired migration histories compose after full recovery and preserve resident + lineage counts across direction changes. The runner used a fresh isolated + temporary cluster and stopped it on exit. +- Explicit clang-tidy 19 covered both census sources and census, migration and + population integration tests. It found a missing defensive default in an integer + test selector; that was fixed and affected tests rechecked without owned warnings. + Repository clang-format 19 and `git diff --check` passed. + +Real funded mutation successors exercise conflicting world genome identities; +funded migration checks every resident category's count/body/reserve, excludes +departed history, retains suspended stock and permits mixed raw/cohort observations. +A source plus incoming destination copy fails duplicate ownership checks. +Declared coverage, all boundary fields, unknown/invalid scopes, known absence, +detachment and the 64-region work bound are exercised under NDEBUG. + +The first test fixture incorrectly supplied unequal founder allocation IDs; +public birth validation rejected it. Tests now use valid funded births/mutations, +without bypassing biology. The initial sandboxed native run could not create +existing hard-link fixtures; the complete rerun with filesystem access passed. +No golden, biological rule/format, SQL schema, dependency or ownership change. + +Limits: the required list is caller-declared, not a durable complete world catalog. +No external branch identity or reconciled cross-region migration watermark is +supplied. Equal-tick source-after/destination-before cuts can omit a migrant; +duplicate detection alone cannot prove completeness. This API accepts neither +world absence nor terminal extinction. Client permissions/presentation and +deployment-host query/index costs remain open. No new TSan, full SQL/backup/DR +matrix, remote CI, multi-host drill or multi-day soak was run for this portable +read-only change; M12.6b retains the preceding full persistence regression. +No deployment, live database or World0 action. + +Logs: `build/m126c-final-native.log`, `build/m126c-final-native-census.log`, +`build/m126c-final-gcc.log`, `build/m126c-final-asan.log`, +`build/m126c-postgres-build.log`, `build/m126c-postgres.log`, +`build/m126c-tidy.log`, `build/m126c-final-tidy.log`, +`build/m126c-final-format.log`. + ## 2026-10-18 - M12.6b committed regional census history ADR-0151 uses the retained immutable biological commit journal as canonical diff --git a/include/evo/server/lifecycle/cellular_census.hpp b/include/evo/server/lifecycle/cellular_census.hpp index 018f032..63a8fd9 100644 --- a/include/evo/server/lifecycle/cellular_census.hpp +++ b/include/evo/server/lifecycle/cellular_census.hpp @@ -71,6 +71,7 @@ class CellularCensus { query(std::uint64_t anchor, CellularCensusScope scope) const; private: + friend class CellularCensusSet; CellularCensus() = default; [[nodiscard]] static std::expected observe(const CellularLives&, std::span, std::uint32_t population); @@ -78,5 +79,9 @@ class CellularCensus { CellularCensusBoundary boundary_; std::vector rows_; std::vector> children_; + // Complete immutable identity evidence for cross-region consistency checks. + // A short content digest alone cannot establish equality. + std::vector genomes_; + std::vector residents_; }; } // namespace evo::server diff --git a/include/evo/server/lifecycle/cellular_census_set.hpp b/include/evo/server/lifecycle/cellular_census_set.hpp new file mode 100644 index 0000000..b350ec7 --- /dev/null +++ b/include/evo/server/lifecycle/cellular_census_set.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include "evo/server/lifecycle/cellular_census.hpp" + +#include +#include +#include +#include +#include + +namespace evo::server { +// A query/work bound, not a selected world size or proof of world completeness. +inline constexpr std::size_t max_cellular_census_regions = 64; +struct CellularCensusSetBoundary { + std::uint64_t world{}, frame{}, tick{}; + std::uint32_t owner_format{}, rules{}; + std::uint64_t quantum_microseconds{biology::cellular_motion_tick_microseconds}; + bool operator==(const CellularCensusSetBoundary&) const = default; +}; +enum class CellularCensusSetError : std::uint8_t { + invalid_request, + coverage, + boundary, + genome_conflict, + resident_conflict +}; +// Detached observations of an explicitly declared region set. No branch, +// commit, permission, ownership or globally complete reconciliation proof. +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. + [[nodiscard]] static std::expected + observe(CellularCensusSetBoundary, std::span required_regions, + std::span observations); + [[nodiscard]] const CellularCensusSetBoundary& boundary() const noexcept { + return boundary_; + } + // Sorted by region, owning detached copies. Lifetime: this object. + [[nodiscard]] std::span regions() const noexcept { + return regions_; + } + // Unknown everywhere is an error, never absence. An absent local anchor can + // contribute zero only after the complete declared set has been validated. + // Nonresident totals count historical record occurrences, not unique lives. + [[nodiscard]] std::expected + query(std::uint64_t anchor, CellularCensusScope scope) const; + + private: + CellularCensusSet() = default; + CellularCensusSetBoundary boundary_; + std::vector regions_; +}; +} // namespace evo::server diff --git a/schemas/ecology/regional-cellular-census-v1.md b/schemas/ecology/regional-cellular-census-v1.md index a1263d8..da4aba7 100644 --- a/schemas/ecology/regional-cellular-census-v1.md +++ b/schemas/ecology/regional-cellular-census-v1.md @@ -1,5 +1,10 @@ # Regional cellular census projection v1 +Follow-on [scoped census sets](scoped-cellular-census-set-v1.md) compare complete +immutable genome records and permanent resident IDs retained privately by these +observations. They require exact declared coverage and boundaries before querying +multiple regions; no public row, biological codec or rule version changes. + Authority: [ADR-0150](../../docs/adr/0150-regional-cellular-census-projection.md), under [ADR-0133](../../docs/adr/0133-ecological-identity-and-time-evidence.md). Portable API: `evo::server::CellularCensus`. This is an in-memory observation diff --git a/schemas/ecology/scoped-cellular-census-set-v1.md b/schemas/ecology/scoped-cellular-census-set-v1.md new file mode 100644 index 0000000..a7439ff --- /dev/null +++ b/schemas/ecology/scoped-cellular-census-set-v1.md @@ -0,0 +1,41 @@ +# Scoped cellular census set, version 1 + +Semantic in-memory query contract under +[ADR-0152](../../docs/adr/0152-scoped-cellular-census-sets.md), not a persistent or +wire codec. Portable implementation: `server::CellularCensusSet`. + +Input names a world, frame, completed tick, owner format, rules and tick quantum, +plus 1..64 nonzero strictly ascending required region IDs. Exactly one valid +detached census must match each ID and every boundary field. Observations can +arrive in any order; raw/exact-cohort classifications can differ. Construction +refuses incomplete, extra, duplicate, foreign, stale or incompatible evidence. +It returns no partial total on failure. + +Shared genome IDs must carry the same complete immutable `Genome` value, including +origin event, schema, development, parentage and capability parameters. All +resident permanent organism IDs must be disjoint across observations. Full +identity equality is checked, not just a short digest or genome parent list. +This detects a source plus incoming ghost that both contain the same resident. +Departed nonresident archive records are historical occurrences without stock. + +Queries explicitly choose an exact genome or its descendant closure. An anchor +unknown everywhere fails. A locally unknown anchor contributes zero only within +the validated declared set, because every retained local genome includes all +ancestors. Each region's resident categories and per-channel body/reserve are +summed with overflow checks. Nonresident totals count record occurrences and +can include repeated historical observations of one organism. They never imply +remote viability, a unique population count or resident ownership. + +Returned regional observations own detached copies sorted by region. No input +biology changes, no ownership moves, and no tick advances. Source records count +as owned until the paired commit; only that commit authorizes destination stock. +Observing a candidate conveys no authority. Rejecting duplicate residents alone +does not prove a consistent migration cut: source-after plus destination-before +can omit a migrant without duplicating any identity. + +The caller-declared list is not a durable complete world region catalog. There +is no external branch identity, commit selection, permission decision, escrow +reconciliation or terminal-extinction outcome. A zero is only a fact about these +supplied observations. Persistence callers retain exact verified commit/page +references separately. Same tick/world/frame alone does not reconcile branches +or prove that all required owners, stores and viable reserves are covered. diff --git a/src/server/lifecycle/cellular_census.cpp b/src/server/lifecycle/cellular_census.cpp index 59035d1..598c96a 100644 --- a/src/server/lifecycle/cellular_census.cpp +++ b/src/server/lifecycle/cellular_census.cpp @@ -60,6 +60,7 @@ CellularCensus::observe(const CellularLives& state, std::span +#include + +namespace evo::server { +namespace { +bool add(std::uint64_t& target, std::uint64_t value) { + if (value > std::numeric_limits::max() - target) + return false; + target += value; + return true; +} +bool add(accounting::Inventory& target, const accounting::Inventory& value) { + if (!add(target.energy.value, value.energy.value)) + return false; + for (std::size_t channel = 0; channel < target.matter.size(); ++channel) + if (!add(target.matter[channel].value, value.matter[channel].value)) + return false; + return true; +} +bool add(CellularCensusTotals& target, const CellularCensusTotals& value) { + if (!add(target.retained_nonresident_records, value.retained_nonresident_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) || + !add(stock.reserve, source.reserve)) + return false; + } + return true; +} +} // namespace + +std::expected +CellularCensusSet::observe(CellularCensusSetBoundary boundary, + std::span required, + std::span observations) { + using Error = CellularCensusSetError; + if (boundary.world == 0 || boundary.frame == 0 || required.empty() || + required.size() > max_cellular_census_regions || required.front() == 0 || + !std::ranges::is_sorted(required) || std::ranges::adjacent_find(required) != required.end()) + return std::unexpected(Error::invalid_request); + if (observations.size() != required.size()) + return std::unexpected(Error::coverage); + std::vector ordered; + ordered.reserve(observations.size()); + for (const auto& observation : observations) + ordered.push_back(&observation); + std::ranges::sort(ordered, {}, [](const auto* census) { return census->boundary().region; }); + std::vector genomes; + std::vector residents; + for (std::size_t i = 0; i < ordered.size(); ++i) { + const auto& census = *ordered[i]; + const auto& cut = census.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}) + return std::unexpected(Error::boundary); + for (const auto& genome : census.genomes_) + genomes.push_back(&genome); + residents.insert(residents.end(), census.residents_.begin(), census.residents_.end()); + } + std::ranges::sort(residents); + if (std::ranges::adjacent_find(residents) != residents.end()) + return std::unexpected(Error::resident_conflict); + std::ranges::sort(genomes, {}, [](const auto* genome) { return genome->id(); }); + for (std::size_t i = 1; i < genomes.size(); ++i) + if (genomes[i - 1]->id() == genomes[i]->id() && *genomes[i - 1] != *genomes[i]) + return std::unexpected(Error::genome_conflict); + CellularCensusSet result; + result.boundary_ = boundary; + result.regions_.reserve(ordered.size()); + for (const auto* census : ordered) + result.regions_.push_back(*census); + return result; +} + +std::expected +CellularCensusSet::query(std::uint64_t anchor, CellularCensusScope scope) const { + if (scope != CellularCensusScope::exact_genome && scope != CellularCensusScope::descendants) + return std::unexpected(CellularCensusError::invalid_scope); + CellularCensusTotals result; + bool known = false; + for (const auto& census : regions_) { + const auto local = census.query(anchor, scope); + if (!local) { + if (local.error() == CellularCensusError::unknown_genome) + continue; + return std::unexpected(local.error()); + } + known = true; + if (!add(result, *local)) + return std::unexpected(CellularCensusError::overflow); + } + if (!known) + return std::unexpected(CellularCensusError::unknown_genome); + return result; +} +} // namespace evo::server diff --git a/tests/cellular_census_tests.cpp b/tests/cellular_census_tests.cpp index fa7efb1..fad597b 100644 --- a/tests/cellular_census_tests.cpp +++ b/tests/cellular_census_tests.cpp @@ -1,4 +1,5 @@ #include "evo/server/lifecycle/cellular_census.hpp" +#include "evo/server/lifecycle/cellular_census_set.hpp" #include "fixtures/cellular_ecology.hpp" #include @@ -202,6 +203,16 @@ void cohorts_and_death() { verify(dead, *scarce); require(dead.rows().size() == 1 && dead.rows().front().totals == CellularCensusTotals{}, "corpse/detritus counted as a living or reproductive reserve"); + const auto& dead_boundary = dead.boundary(); + const auto dead_set = checked( + CellularCensusSet::observe({dead_boundary.world, dead_boundary.frame, dead_boundary.tick, + dead_boundary.owner_format, dead_boundary.rules, + dead_boundary.quantum_microseconds}, + std::array{dead_boundary.region}, std::array{dead}), + "known absent scoped census"); + require(checked(dead_set.query(dead.rows().front().genome, Scope::descendants), + "known absent scoped lineage") == CellularCensusTotals{}, + "retained zero-carrier lineage became unknown or living stock"); const auto empty = opening(1, true, 1, false); require(checked(CellularCensus::observe(*empty), "empty census").rows().empty(), "empty owner invented history"); @@ -218,11 +229,164 @@ void cohorts_and_death() { require(!unsupported && unsupported.error() == CellularCensusError::unsupported, "legacy rules silently reinterpreted by current census"); } +Owner regional_founder(std::uint64_t region, std::uint64_t life, std::uint64_t genome, + std::uint64_t event, std::uint64_t organism = 0) { + auto owner = opening(1, true, 1, false, region); + step(*owner); + LifeRequest request{}; + request.account.back() = 1; + request.action = 1; + return checked( + owner->birth_candidate( + request, {life, organism == 0 ? life : organism, genome, event, {owner->tick()}}), + "regional census funded founder"); +} +CellularCensusSetBoundary set_boundary(const CellularCensus& census) { + const auto& b = census.boundary(); + return {b.world, b.frame, b.tick, b.owner_format, b.rules, b.quantum_microseconds}; +} +void regional_sets() { + auto a = regional_founder(1, 100, 100, 100); + auto b = regional_founder(2, 200, 200, 200); + const auto ca = checked(CellularCensus::observe(*a), "set region A"); + const auto cb = checked(CellularCensus::observe(*b), "set region B"); + const std::array required{1, 2}; + const auto boundary = set_boundary(ca); + const auto observe = [&](const auto& observations) { + return CellularCensusSet::observe(boundary, required, observations); + }; + const std::array reversed{cb, ca}; + const auto set = checked(observe(reversed), "complete regional set"); + require(set.regions()[0].boundary().region == 1 && set.regions()[1].boundary().region == 2 && + set.boundary() == boundary, + "regional set lost its boundary/canonical region order"); + for (const auto scope : {Scope::exact_genome, Scope::descendants}) { + require( + checked(set.query(100, scope), "cross-region founder A") == direct(*a, 100, false) && + checked(set.query(200, scope), "cross-region founder B") == direct(*b, 200, false), + "missing local anchor hid a known lineage or manufactured stock"); + const auto unknown = set.query(300, scope); + require(!unknown && unknown.error() == CellularCensusError::unknown_genome, + "unknown anchor became scoped absence"); + } + require(!set.query(100, Scope::invalid) && + set.query(100, Scope::invalid).error() == CellularCensusError::invalid_scope, + "set accepted invalid scope"); + const auto refuse = [&](const auto& observations, CellularCensusSetError expected) { + const auto result = observe(observations); + require(!result && result.error() == expected, + "set accepted incomplete/conflicting evidence"); + }; + refuse(std::array{ca}, CellularCensusSetError::coverage); + refuse(std::array{ca, ca}, CellularCensusSetError::coverage); + refuse(std::array{ca, cb, cb}, CellularCensusSetError::coverage); + for (unsigned field = 0; field < 6; ++field) { + auto wrong = boundary; + switch (field) { + case 0: + ++wrong.world; + break; + case 1: + ++wrong.frame; + break; + case 2: + ++wrong.tick; + break; + case 3: + ++wrong.owner_format; + break; + case 4: + ++wrong.rules; + break; + case 5: + ++wrong.quantum_microseconds; + break; + default: + require(false, "invalid boundary-field test selector"); + break; + } + const auto result = CellularCensusSet::observe(wrong, required, reversed); + require(!result && result.error() == CellularCensusSetError::boundary, + "regional set accepted foreign/stale/version-mismatched evidence"); + } + for (const auto bad : {std::array{0, 2}, {2, 1}, {1, 1}}) { + const auto result = CellularCensusSet::observe(boundary, bad, reversed); + require(!result && result.error() == CellularCensusSetError::invalid_request, + "invalid required region set accepted"); + } + require(!CellularCensusSet::observe(boundary, {}, {}), "empty scope accepted"); + auto duplicate = regional_founder(2, 100, 100, 100); + refuse(std::array{ca, checked(CellularCensus::observe(*duplicate), "duplicate organism")}, + CellularCensusSetError::resident_conflict); + step(*b); + refuse(std::array{ca, checked(CellularCensus::observe(*b), "stale boundary")}, + CellularCensusSetError::boundary); + require(checked(set.query(200, Scope::exact_genome), "detached region query") == + direct(*regional_founder(2, 200, 200, 200), 200, false), + "later biology mutated the detached set"); + std::vector empty_regions; + std::vector ids; + for (std::uint64_t region = 1; region <= max_cellular_census_regions + 1; ++region) { + auto empty = opening(1, true, 1, false, region); + step(*empty); + empty_regions.push_back(checked(CellularCensus::observe(*empty), "empty region")); + ids.push_back(region); + } + require(CellularCensusSet::observe(boundary, std::span{ids}.first(64), + std::span{empty_regions}.first(64)) + .has_value() && + !CellularCensusSet::observe(boundary, ids, empty_regions), + "regional query work bound not enforced"); +} +void conflicting_regional_genomes() { + auto a = regional_founder(1, 100, 100, 100); + auto b = regional_founder(2, 200, 200, 200); + // Independently valid owners can allocate the same successor ID. Shared + // world identity must not merge their different immutable parent records. + for (auto* owner : {a.get(), b.get()}) { + bool applied = false; + for (unsigned tick = 0; tick < 8192 && !applied; ++tick) { + const auto& life = owner->lives().front(); + std::vector choices; + if (life.mutation.offer) { + const auto& offer = *life.mutation.offer; + for (const auto& candidate : offer.candidates) { + const auto genome = checked(owner->archive().find(life.identity.genome), + "set conflict mutation genome"); + if (biology::fund_mutation(genome, offer.context, offer, candidate.operator_id, + 900, 900, life.reserve)) { + choices.push_back({life.identity.life, life.mutation.event_index, + life.mutation.offer_tick, 900, candidate.operator_id}); + break; + } + } + } + const std::array intent{{{life.identity.life, {}}}}; + const auto result = checked(owner->advance_tick_with_mutations(intent, choices), + "set conflict funded mutation"); + applied = !result.mutations.empty() && + result.mutations.front().result == CellularMutationResult::applied; + } + require(applied, "set conflict fixture did not fund a mutation"); + } + while (a->tick() < b->tick()) + step(*a); + while (b->tick() < a->tick()) + step(*b); + const auto ca = checked(CellularCensus::observe(*a), "conflict census A"); + const auto cb = checked(CellularCensus::observe(*b), "conflict census B"); + const auto result = CellularCensusSet::observe( + set_boundary(ca), std::array{1, 2}, std::array{ca, cb}); + require(!result && result.error() == CellularCensusSetError::genome_conflict, + "different immutable records merged under one world genome ID"); +} } // namespace int main() try { presence_and_detachment(); mutation_and_retention(); cohorts_and_death(); + regional_sets(); + conflicting_regional_genomes(); std::cout << "PASS local census presence, exact lineage queries, conversion and retention\n"; return 0; } catch (const std::exception& error) { diff --git a/tests/cellular_migration_tests.cpp b/tests/cellular_migration_tests.cpp index 7ea09f4..dc26021 100644 --- a/tests/cellular_migration_tests.cpp +++ b/tests/cellular_migration_tests.cpp @@ -1,4 +1,5 @@ #include "evo/server/lifecycle/cellular_census.hpp" +#include "evo/server/lifecycle/cellular_census_set.hpp" #include "evo/server/lifecycle/cellular_migration.hpp" #include "evo/server/lifecycle/cellular_migration_trace.hpp" #include "evo/server/lifecycle/cellular_population_batch.hpp" @@ -440,6 +441,46 @@ void upgrade_and_move() { require(arrived.resident[0].body == migrant_bodies && arrived.resident[0].reserve == migrant_reserves, "census migrant inventory differs from exact individual stock"); + const auto& cut = departure.boundary(); + const CellularCensusSetBoundary boundary{cut.world, cut.frame, cut.tick, + cut.owner_format, cut.rules, cut.quantum_microseconds}; + const std::array regions{1, 2}; + const auto combined = + checked(CellularCensusSet::observe(boundary, regions, std::array{arrival, departure}), + "settled pair census set"); + const auto combined_stock = checked(combined.query(genome, CellularCensusScope::descendants), + "settled cross-region lineage"); + require(combined_stock.resident[0] == arrived.resident[0] && + combined_stock.retained_nonresident_records == moved.members.size(), + "cross-region census counted departed history twice"); + for (std::size_t kind = 0; kind < combined_stock.resident.size(); ++kind) { + const auto& actual = combined_stock.resident[kind]; + const auto& left = departed.resident[kind]; + const auto& right = arrived.resident[kind]; + require(actual.count == left.count + right.count && + actual.body == plus(left.body, right.body) && + actual.reserve == plus(left.reserve, right.reserve), + "combined lineage count/body/reserve differs from settled exact stock"); + } + auto arrival_population = + checked(CellularPopulation::create(restore(*moved.destination)), "mixed census population"); + checked(arrival_population->collapse(genome), "mixed census collapse"); + const auto cohort_arrival = + checked(CellularCensus::observe(*arrival_population), "mixed census cohort arrival"); + const auto mixed = checked( + CellularCensusSet::observe(boundary, regions, std::array{departure, cohort_arrival}), + "mixed raw/cohort census set"); + const auto mixed_stock = checked(mixed.query(genome, CellularCensusScope::descendants), + "mixed representation lineage"); + require(mixed_stock.resident[0] == CellularCensusStock{} && + mixed_stock.resident[1] == arrived.resident[0] && + mixed_stock.resident[4] == departed.resident[4], + "mixed classification lost/fabricated stock or suspended parent"); + const auto old_source = checked(CellularCensus::observe(*source), "unsettled source census"); + const auto double_owned = + CellularCensusSet::observe(boundary, regions, std::array{old_source, arrival}); + require(!double_owned && double_owned.error() == CellularCensusSetError::resident_conflict, + "source plus uncommitted destination copy counted twice"); require(moved.destination->next_bud_sequence() == target->next_bud_sequence(), "import consumed a local bud identity"); auto source_after = decode(*moved.source, moved.source->snapshot()); diff --git a/tests/population_publication_tests.cpp b/tests/population_publication_tests.cpp index 12bf756..0409396 100644 --- a/tests/population_publication_tests.cpp +++ b/tests/population_publication_tests.cpp @@ -4,6 +4,7 @@ #include "evo/persistence/population_interval_store.hpp" #include "evo/persistence/sha256.hpp" #include "evo/server/cellular_region_host.hpp" +#include "evo/server/lifecycle/cellular_census_set.hpp" #include "fixtures/cellular_ecology.hpp" #include "fixtures/postgres_relay.hpp" @@ -1600,6 +1601,38 @@ void migration_publication(Fixture& f, const RegionKey destination_key, std::ranges::equal(destination_history.entries[0].census.rows(), census_oracle(expected_destination, dh).rows()), "historical migration census differs from exact paired biology"); + const auto& source_census = source_history.entries[0].census; + const auto& destination_census = destination_history.entries[0].census; + const auto& boundary = source_census.boundary(); + std::array required_regions{sk.region, dk.region}; + std::ranges::sort(required_regions); + const auto census_set = + take(CellularCensusSet::observe( + {boundary.world, boundary.frame, boundary.tick, boundary.owner_format, + boundary.rules, boundary.quantum_microseconds}, + required_regions, std::array{source_census, destination_census}), + "committed paired history census set"); + // Identity and complete per-region rows survive real raw/cohort + // publication, replay and repeated direction changes. + require(census_set.regions().size() == 2 && + census_set.regions()[0].boundary().region == required_regions[0] && + census_set.regions()[1].boundary().region == required_regions[1], + "paired history query lost declared region coverage"); + for (const auto& row : destination_census.rows()) { + const auto combined = + take(census_set.query(row.genome, CellularCensusScope::descendants), + "committed cross-region lineage query"); + const auto local = + take(destination_census.query(row.genome, CellularCensusScope::descendants), + "committed destination lineage"); + const auto other = + source_census.query(row.genome, CellularCensusScope::descendants); + for (std::size_t kind = 0; kind < local.resident.size(); ++kind) + require(combined.resident[kind].count == + local.resident[kind].count + + (other ? other->resident[kind].count : 0), + "committed set duplicated/dropped resident lineage stock"); + } if (direction == 0) { outgoing_history = std::move(source_history.entries.front()); incoming_history = std::move(destination_history.entries.front());