From 2b074b9ae19ae5fbc119295aad5b176bccbc39b3 Mon Sep 17 00:00:00 2001 From: dingo Date: Sat, 17 Oct 2026 12:04:39 -0400 Subject: [PATCH] M12.2: select and qualify exact cellular local cohorts --- CMakeLists.txt | 10 +- README.md | 1 + docs/STATUS.md | 12 ++ docs/adr/0139-exact-cellular-local-cohorts.md | 63 ++++++ docs/adr/README.md | 2 + docs/biology/CELLULAR_POPULATION.md | 32 ++++ docs/biology/ECOLOGY_MODEL.md | 6 + docs/engineering/M12_DELIVERY_PLAN.md | 14 +- .../engineering/M12_EXACT_CELLULAR_COHORTS.md | 57 ++++++ docs/engineering/VALIDATION.md | 23 +++ .../results/m12-2d-cohorts-gcc14.jsonl | 29 +++ .../results/m12-2d-cohorts-msvc1944.jsonl | 29 +++ experiments/resolution/cellular_cohorts.cpp | 162 ++++++++++++++++ experiments/resolution/cellular_reference.cpp | 147 +------------- .../server/lifecycle/cellular_population.hpp | 52 +++++ src/server/lifecycle/cellular_population.cpp | 92 +++++++++ tests/fixtures/cellular_ecology.hpp | 180 ++++++++++++++++++ 17 files changed, 762 insertions(+), 149 deletions(-) create mode 100644 docs/adr/0139-exact-cellular-local-cohorts.md create mode 100644 docs/biology/CELLULAR_POPULATION.md create mode 100644 docs/engineering/M12_EXACT_CELLULAR_COHORTS.md create mode 100644 docs/engineering/results/m12-2d-cohorts-gcc14.jsonl create mode 100644 docs/engineering/results/m12-2d-cohorts-msvc1944.jsonl create mode 100644 experiments/resolution/cellular_cohorts.cpp create mode 100644 include/evo/server/lifecycle/cellular_population.hpp create mode 100644 src/server/lifecycle/cellular_population.cpp create mode 100644 tests/fixtures/cellular_ecology.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d38411d..8d83573 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,7 +88,7 @@ evo_owned_target(evo_biology) add_library(evo_lifecycle STATIC src/server/lifecycle/primitive_lives.cpp src/server/lifecycle/primitive_batch.cpp src/server/lifecycle/cellular_lives.cpp src/server/lifecycle/cellular_batch.cpp src/server/lifecycle/wildlife_controller.cpp - src/server/lifecycle/cellular_play_summary.cpp) + src/server/lifecycle/cellular_play_summary.cpp src/server/lifecycle/cellular_population.cpp) add_library(evo::lifecycle ALIAS evo_lifecycle) target_link_libraries(evo_lifecycle PUBLIC evo::biology) evo_owned_target(evo_lifecycle) @@ -652,6 +652,14 @@ if(BUILD_TESTING) set_tests_properties(cellular-population-reference PROPERTIES TIMEOUT 180) endif() +add_executable(evo-cellular-cohorts experiments/resolution/cellular_cohorts.cpp) +target_link_libraries(evo-cellular-cohorts PRIVATE evo::lifecycle) +evo_owned_target(evo-cellular-cohorts) +if(BUILD_TESTING) + add_test(NAME cellular-cohort-qualification COMMAND evo-cellular-cohorts --verify) + set_tests_properties(cellular-cohort-qualification PROPERTIES TIMEOUT 180) +endif() + add_executable(evo-chunk-layout experiments/world/chunk_layout.cpp) target_link_libraries(evo-chunk-layout PRIVATE evo::world) evo_owned_target(evo-chunk-layout) diff --git a/README.md b/README.md index c164a25..cf46bd6 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,7 @@ ctest --preset release ./build/release/evo-spatial-benchmark ./build/release/evo-resolution-benchmark ./build/release/evo-cellular-reference --verify +./build/release/evo-cellular-cohorts --verify ./build/release/evo-chunk-layout ``` diff --git a/docs/STATUS.md b/docs/STATUS.md index 94ed6c5..dac6b47 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -1,5 +1,17 @@ # Project status and next steps +## M12.2d exact cellular cohorts - 2026-10-17 + +Under the maintainer's delegated M12.2 authority, ADR-0139 selects exact local +cohorts retaining every member. Regional approximation stays disabled. The +[qualification](engineering/M12_EXACT_CELLULAR_COHORTS.md) matches complete +canonical biology over 196,608 paired ticks per compiler, including thousands +of conversion oscillations. Local active fallback samples reach the existing +256-life ceiling with admission backpressure. Release CTest passes 72/72; +clang-format/tidy 19 and ASan/UBSan checks pass. No compression or production +capacity is claimed. Target-host costs and fuller interaction coverage remain +open. Exact-owner durable transitions may proceed without live enablement. + ## M12.2c current cellular reference - 2026-10-17 The [rules-8 headless reference](engineering/M12_CELLULAR_REFERENCE.md) passes diff --git a/docs/adr/0139-exact-cellular-local-cohorts.md b/docs/adr/0139-exact-cellular-local-cohorts.md new file mode 100644 index 0000000..08c4fe1 --- /dev/null +++ b/docs/adr/0139-exact-cellular-local-cohorts.md @@ -0,0 +1,63 @@ +# ADR-0139: Exact retained-member cellular local cohorts + +## Status + +Accepted for the bounded M12 cellular population implementation, 2026-10-17. +The maintainer delegates M12.2 decisions to the implementing agent, subject to +later revision. This decision does not enable a production selector, increase +capacity, qualify approximate aggregation or authorize deployment. + +## Decision + +Replace the rejected M4 averaged model with an exact local model for rules 8. +A cohort groups wildlife of one genome in one region, retaining every member's +identity, position, birth tick, growth, damage, reserves, reproduction state and +ancestry. There are no bins, fractional members, random materialization samples, +mean reserves or statistical parentage. The residual is identically zero. + +`CellularPopulation` exclusively owns one `CellularLives` store. Sorted membership +tags partition that store into active individuals and cohort members; they are +not a second inventory. The read-only biological view conveys no spending rights. +All actions execute through the population owner. Controlled cells, including +offline/suspended cells, never collapse. Collapse/materialize operates on the +entire eligible wildlife genome group, never a caller-picked healthy subset. +New offspring initially have individual representation. Dead membership is +removed after the successful atomic biological step. + +The same bounded wildlife controller and complete shared interaction kernel run +at the existing 50 ms quantum, in the existing life-ID order. Cohort membership +cannot affect intake contention, movement, hunting, budding, mutation, death, +inventory or ancestry. The host submits only controlled intentions; it cannot +supply a different controller for cohorts. No autonomous mutation is introduced. +Biological rules, existing codec versions and old goldens are unchanged. + +Keep regional approximation disabled. Do not silently substitute the old M4 +model under overload. Retain the existing hard limit of 256 living cells per +region; new admissions backpressure rather than deleting incumbents. This is a +structural ceiling, not a measured service capacity. The operational target is +completion within the 50 ms tick, with measured tail latency and headroom on the +actual deployment host required before enabling a production selector. An +overrun must leave admitted ticks pending, not skip ecology. + +Qualification is declared before results: reference seeds 0/1, held-out 2/3, +rich/scarce finite food, conversion oscillation every 1/16/64 ticks, paired full +canonical biology equality every tick. Zero demographic, biomass, diversity, +ancestry or inventory discrepancy is allowed, including false local extinctions. +Report conversion coverage and full owner costs, including active fallbacks at +16/64/128/256 lives. Bin sensitivity is inapplicable: no state is binned. Do not +infer unobserved predation, twins or long-term stability from passing cases. + +## Consequences and limits + +This resolves the unsafe averaging conflict for an exact local implementation +and permits work on its durable representation. It deliberately trades memory +and computation for fidelity. Cohorts currently provide ownership organization, +not population compression or a demonstrated speedup. The M12.2 target-host cost +gate and scalable approximate model remain open; do not mark the whole gate +passed solely on this decision. Production enabling and higher capacity require +new measured evidence. Future approximation requires a separate version/ADR and +paired evidence; it must not reinterpret these exact members. + +The module lives in portable `evo::lifecycle`, independent of renderer, network, +database and clocks. The foundation proof and legacy ecology experiments retain +their existing boundaries. No arbitrary third-party dependency is added. diff --git a/docs/adr/README.md b/docs/adr/README.md index bf9f6b1..22f8ff6 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -155,3 +155,5 @@ design. No unadopted candidate is a link dependency of the proof. - [ADR-0087: Dev session route](0087-dev-session-route.md) - evolution-dev only: void.aeternalisgames.com:7400 via a dedicated internal MetalLB VIP to the region pod, TLS end to end, CIDR-limited, operator-opened windows enforced by evo-server; committed disabled with fail-closed policy tests. - [ADR-0086: Windows client package](0086-windows-client-package.md) - static-runtime Windows x64 ZIP with explicit allowlist, licence texts, build identity, SHA256 manifest, credential guards and deterministic layout; local packaging until a Windows runner exists. - [ADR-0085: Rebindable engineering controls](0085-rebindable-engineering-controls.md) - SDL-free action/HID-key bindings, conflict validation, versioned all-or-nothing controls file, binding capture before toolkit/world. + +- [ADR-0139: Exact cellular local cohorts](0139-exact-cellular-local-cohorts.md) diff --git a/docs/biology/CELLULAR_POPULATION.md b/docs/biology/CELLULAR_POPULATION.md new file mode 100644 index 0000000..3950848 --- /dev/null +++ b/docs/biology/CELLULAR_POPULATION.md @@ -0,0 +1,32 @@ +# Exact cellular local population + +[ADR-0139](../adr/0139-exact-cellular-local-cohorts.md) defines the first rules-8 +population owner. It owns one biological store and an exclusive representation +tag per cohort member; cohort inventory must never be added to `current_total` +again. Untagged living cells are individuals. Views are read-only. + +`create` accepts a non-null rules-8 owner. `collapse(genome)` tags every eligible +zero-account member of that genome, excluding controlled cells. An absent +wildlife group or an ineligible wildlife presence rejects the conversion. +Repeating collapse returns zero without duplicating anything. `materialize` +removes all tags for that genome; an absent cohort is refused. Neither operation +changes the biological snapshot, identities, locations, ages or balances. + +`advance_tick` takes exactly the sorted controlled set and optional controlled +mutation selections. It obtains every wildlife intention from the existing +controller, merges in life-ID order and uses the same atomic cellular kernel. +Failed input validation changes neither biology nor membership. Successful death +removes stale ownership tags; funded births begin as individuals. Suspension +continues to freeze controlled biology regardless of representation elsewhere. + +The model retains exact members and makes no aggregation speedup claim. There is +no regional population approximation, process migration, production selector or +new persisted format in this segment. The existing 256-life ceiling remains; +target-host service capacity is a separate qualification. + +Run `evo-cellular-cohorts --verify` for the CTest matrix or omit the option for +8192 ticks per case. The executable compares complete canonical state every tick, +not just conserved totals. It reports observed conversion/member-tick coverage, +events, costs and 16/64/128/256-life active fallback observations. Shared fixture +setup is identical to the unchanged rules-8 reference; no alternate test biology +or altered golden is used. diff --git a/docs/biology/ECOLOGY_MODEL.md b/docs/biology/ECOLOGY_MODEL.md index 6790f86..7d4c6fb 100644 --- a/docs/biology/ECOLOGY_MODEL.md +++ b/docs/biology/ECOLOGY_MODEL.md @@ -1,5 +1,11 @@ # Ecology and simulation fidelity +M12.2d selects [exact retained-member local cohorts](CELLULAR_POPULATION.md) +under [ADR-0139](../adr/0139-exact-cellular-local-cohorts.md). Paired rules-8 +fidelity passes the declared matrix; regional approximation and production +selector enabling remain disabled pending operational qualification. Historical +statements below about no accepted model refer to the earlier M4/M12 baseline. + M12.2c's [current cellular reference](../engineering/M12_CELLULAR_REFERENCE.md) uses implemented rules 8 and validates exact encoded replay/offline closure. It is the active control for future cohort comparisons, not a qualified approximate diff --git a/docs/engineering/M12_DELIVERY_PLAN.md b/docs/engineering/M12_DELIVERY_PLAN.md index 32a3b4e..553cd86 100644 --- a/docs/engineering/M12_DELIVERY_PLAN.md +++ b/docs/engineering/M12_DELIVERY_PLAN.md @@ -9,8 +9,8 @@ Commit and push each validated scoped segment separately. | Subset | State | Evidence or condition for completion | |---|---|---| | 12.1 | Engineering evidence policy and explicit deferrals documented | ADR-0133, identity/time contract and worked histories; no new game rules or runtime versions. | -| 12.2 | Baseline reproduction (12.2a), exact-strata instrument (12.2b) and rules-8 cellular reference (12.2c) complete; cohort qualification open | The old widths remain rejected. Synthetic exact-state and current cellular controls pass; an actual paired cellular/cohort model and target-host fallback budgets remain required. | -| 12.3 | Blocked on 12.2 | No accepted population model, deterministic durable conversion sample or profile integrating cellular/cohort state. | +| 12.2 | Exact local model selected and paired qualification implemented (12.2d); production cost gate open | ADR-0139, exact canonical equality across 196,608 paired ticks per platform. Regional approximation disabled; no compression claim. Deployment-host capacity and fuller interaction coverage remain open. | +| 12.3 | Exact-owner durable implementation authorized; production enabling depends on qualification | ADR-0139 resolves the averaging conflict for retained members. Durable conversion closure/publication remains to implement. | | 12.4 | Blocked on 12.2-12.3 | Existing cellular worker pauses without sessions/lingering players; bounded unattended recovery and biological migration escrow remain unimplemented. | | 12.5 | Blocked on 12.2-12.4 | Finite M6 process fixtures do not qualify coupled persistent cellular/cohort atmosphere/water budgets. | @@ -42,7 +42,15 @@ non-timing outputs agree; native Release CTest passes 71/71. This is an exact reference control; the paired cellular/cohort model, fuller predation/capacity matrix and target-host budgets remain open. No automatic tier selector is enabled. -## Architectural conflict and resolution +## Architectural conflict and resolution (updated 2026-10-17) + +The maintainer delegates M12.2 decisions, subject to later revision. ADR-0139 +selects exact retained-member local cohorts instead of the failed average model. +[Paired results](M12_EXACT_CELLULAR_COHORTS.md) validate biological equality with +conversion oscillation and local fallback samples up to the existing 256-cell +ceiling. This permits implementing durable exact ownership without enabling a +production selector. Target-host/scaling gates remain explicit; the historical +conflict below still prohibits deploying approximate aggregation. Automatic transitions or regional aggregation now would conflict with ADR-0041 and ECOLOGY_MODEL: the measured model can erase viable populations while exactly diff --git a/docs/engineering/M12_EXACT_CELLULAR_COHORTS.md b/docs/engineering/M12_EXACT_CELLULAR_COHORTS.md new file mode 100644 index 0000000..4c9fd71 --- /dev/null +++ b/docs/engineering/M12_EXACT_CELLULAR_COHORTS.md @@ -0,0 +1,57 @@ +# M12.2d exact cellular cohort qualification + +Decision: [ADR-0139](../adr/0139-exact-cellular-local-cohorts.md), accepted under +the maintainer's delegated M12.2 decision authority. The rules-8 model retains +every member exactly. Regional approximation remains disabled. No compression, +speedup, deployment or production capacity is claimed. + +## Executed evidence, 2026-10-17 + +Full Release runs on MSVC 19.44 and GCC 14.2 use the current reference's four +seeds (0/1 development, 2/3 held out), rich/scarce finite deposits and conversion +oscillation widths 1/16/64. Each of 24 cases compares complete canonical +biological snapshots at every one of 8192 ticks: **196,608 paired ticks** per +platform, with zero state error. All non-timing paired JSON fields agree across +compilers. The original reference's setup, rules and goldens are unchanged. + +Across these repetitions: **2354 collapsed members, 2354 materialized members, +6380 cohort member-ticks, 12 offspring, 48 applied mutations and 57 deaths**. +Counts include repeated reference scenarios at three conversion widths, not +independent biological replicates. Some rich seeds produce no offspring; the +test requires actual conversion coverage in seed 1 and across the full matrix. +The short CTest also runs that long seed-1 case, preventing a vacuous pass with +no cohorts. Controlled founder collapse, missing intentions, duplicate collapse +and repeated materialization refusals preserve state. Per-tick checks validate +tags against live unowned active cells and preserve total inventory. + +Raw output: [MSVC](results/m12-2d-cohorts-msvc1944.jsonl), +[GCC](results/m12-2d-cohorts-gcc14.jsonl). + +| Living cells | MSVC p95 step (ms) | GCC/WSL p95 step (ms) | Snapshot bytes | +|---|---:|---:|---:| +| 16 | 0.045 | 0.058 | 21,108 | +| 64 | 0.274 | 0.200 | 71,076 | +| 128 | 0.773 | 0.777 | 137,700 | +| 256 | 2.359 | 2.326 | 270,948 | + +These are 32 local warm stepping observations per size, fully funded controlled +founders and finite contact food. They include the complete biological owner +step but exclude network, storage/publication, rendering, initialization and +the qualification's byte comparisons. They are not a mature wildlife stress +workload, long-term tail distribution or the Linux deployment host. Refusing a +257th admission leaves the existing 256 alive and the snapshot unchanged. +The hard ceiling is retained; production headroom remains unqualified. + +## Limits and next work + +Local exact-state fidelity passes the declared matrix; useful aggregation and +target-host fallback capacity remain open parts of M12.2. The largest observed +cohort in these ecological histories is small. Full predation, twin-bud, +multi-member cohort, capacity wildlife and blocked-resume matrices need stronger +coverage before production enablement. No arbitrary approximation may fill that +gap. Bins do not exist; widths change conversion timing, not biological quantum. + +M12.3 can implement the exact owner's durable conversion contract without +enabling a live selector. Database publication, process loss and deployment +qualification must be reported separately from codec/replay success. No World0 +state is accessed. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 7addc2c..23b24de 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -1,5 +1,28 @@ # Validation report +## 2026-10-17 - M12.2d exact cellular cohorts + +ADR-0139 selects exact retained-member local cohorts under delegated M12.2 +decision authority. Changes affect portable lifecycle orchestration, a shared +rules-8 qualification fixture and the paired executable, not the foundation +proof, old ecology model, persisted versions, goldens or live world. + +Executed: MSVC 19.44 Release configure/build and **72/72 CTests** outside the +filesystem sandbox; GCC 14.2 Release build/reference and cohort CTests; full +24-case cohort matrices on MSVC/GCC with all non-timing paired fields equal; +clang-format 19 repository check, clang-tidy 19 analysis build and both affected +CTests (23.81 s); ASan/UBSan cohort CTest with required nonempty conversion case +(61.58 s), no reports. Tests use explicit checks under NDEBUG. No threading added. + +Full evidence: [M12_EXACT_CELLULAR_COHORTS](M12_EXACT_CELLULAR_COHORTS.md). +196,608 canonical paired comparisons per platform, zero state error; 2354 +collapses and 2354 materializations. Local 32-sample capacity observations at +16/64/128/256 cells include stepping, not publication or network. The R730/worker +deployment host was not measured; no service-capacity acceptance follows. The +initial short matrix had no conversions; it was strengthened with the full +seed-1 case, and the final matrix requires nonzero cohort coverage. No live DB, +process-loss, migration, regional resource coupling or unattended soak claimed. + ## 2026-10-17 - M12.2c current cellular reference ADR-0138 scopes an isolated headless research executable over the current rules-8 diff --git a/docs/engineering/results/m12-2d-cohorts-gcc14.jsonl b/docs/engineering/results/m12-2d-cohorts-gcc14.jsonl new file mode 100644 index 0000000..a4950f9 --- /dev/null +++ b/docs/engineering/results/m12-2d-cohorts-gcc14.jsonl @@ -0,0 +1,29 @@ +{"kind":"metadata","model":"exact-cellular-cohorts-1","rules":8,"quantum_us":50000,"regional_approximation":false,"compression_claimed":false,"runtime_enabled":false} +{"kind":"paired","seed":0,"holdout":false,"rich":true,"ticks":8192,"conversion_width":1,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":0,"buds":0,"mutations":4,"reference_ns":72869300,"population_ns":67712038,"conversion_ns":249665,"max_state_error":0} +{"kind":"paired","seed":0,"holdout":false,"rich":true,"ticks":8192,"conversion_width":16,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":0,"buds":0,"mutations":4,"reference_ns":70965756,"population_ns":70058568,"conversion_ns":37867,"max_state_error":0} +{"kind":"paired","seed":0,"holdout":false,"rich":true,"ticks":8192,"conversion_width":64,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":0,"buds":0,"mutations":4,"reference_ns":59653588,"population_ns":58224478,"conversion_ns":10112,"max_state_error":0} +{"kind":"paired","seed":0,"holdout":false,"rich":false,"ticks":8192,"conversion_width":1,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":31851651,"population_ns":30474676,"conversion_ns":197263,"max_state_error":0} +{"kind":"paired","seed":0,"holdout":false,"rich":false,"ticks":8192,"conversion_width":16,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":30804521,"population_ns":30142879,"conversion_ns":17924,"max_state_error":0} +{"kind":"paired","seed":0,"holdout":false,"rich":false,"ticks":8192,"conversion_width":64,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":29899945,"population_ns":29510995,"conversion_ns":6913,"max_state_error":0} +{"kind":"paired","seed":1,"holdout":false,"rich":true,"ticks":8192,"conversion_width":1,"collapsed":1048,"materialized":1048,"cohort_member_ticks":1048,"peak_members":1,"deaths":0,"buds":1,"mutations":4,"reference_ns":78692413,"population_ns":92228994,"conversion_ns":416627,"max_state_error":0} +{"kind":"paired","seed":1,"holdout":false,"rich":true,"ticks":8192,"conversion_width":16,"collapsed":65,"materialized":65,"cohort_member_ticks":1040,"peak_members":1,"deaths":0,"buds":1,"mutations":4,"reference_ns":77510113,"population_ns":92380369,"conversion_ns":73074,"max_state_error":0} +{"kind":"paired","seed":1,"holdout":false,"rich":true,"ticks":8192,"conversion_width":64,"collapsed":16,"materialized":16,"cohort_member_ticks":1024,"peak_members":1,"deaths":0,"buds":1,"mutations":4,"reference_ns":77010079,"population_ns":92608570,"conversion_ns":22708,"max_state_error":0} +{"kind":"paired","seed":1,"holdout":false,"rich":false,"ticks":8192,"conversion_width":1,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":30871717,"population_ns":29629546,"conversion_ns":178037,"max_state_error":0} +{"kind":"paired","seed":1,"holdout":false,"rich":false,"ticks":8192,"conversion_width":16,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":29482526,"population_ns":28755916,"conversion_ns":18267,"max_state_error":0} +{"kind":"paired","seed":1,"holdout":false,"rich":false,"ticks":8192,"conversion_width":64,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":29243431,"population_ns":28548913,"conversion_ns":6733,"max_state_error":0} +{"kind":"paired","seed":2,"holdout":true,"rich":true,"ticks":8192,"conversion_width":1,"collapsed":92,"materialized":92,"cohort_member_ticks":92,"peak_members":2,"deaths":1,"buds":2,"mutations":4,"reference_ns":65520558,"population_ns":64282957,"conversion_ns":232683,"max_state_error":0} +{"kind":"paired","seed":2,"holdout":true,"rich":true,"ticks":8192,"conversion_width":16,"collapsed":4,"materialized":4,"cohort_member_ticks":64,"peak_members":2,"deaths":1,"buds":2,"mutations":4,"reference_ns":62061716,"population_ns":62918965,"conversion_ns":30676,"max_state_error":0} +{"kind":"paired","seed":2,"holdout":true,"rich":true,"ticks":8192,"conversion_width":64,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":1,"buds":2,"mutations":4,"reference_ns":63541908,"population_ns":64404835,"conversion_ns":10887,"max_state_error":0} +{"kind":"paired","seed":2,"holdout":true,"rich":false,"ticks":8192,"conversion_width":1,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":31716929,"population_ns":30172844,"conversion_ns":181926,"max_state_error":0} +{"kind":"paired","seed":2,"holdout":true,"rich":false,"ticks":8192,"conversion_width":16,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":30328184,"population_ns":29810575,"conversion_ns":19862,"max_state_error":0} +{"kind":"paired","seed":2,"holdout":true,"rich":false,"ticks":8192,"conversion_width":64,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":31373917,"population_ns":30973437,"conversion_ns":6963,"max_state_error":0} +{"kind":"paired","seed":3,"holdout":true,"rich":true,"ticks":8192,"conversion_width":1,"collapsed":1048,"materialized":1048,"cohort_member_ticks":1048,"peak_members":1,"deaths":2,"buds":1,"mutations":4,"reference_ns":67664442,"population_ns":80044229,"conversion_ns":388135,"max_state_error":0} +{"kind":"paired","seed":3,"holdout":true,"rich":true,"ticks":8192,"conversion_width":16,"collapsed":65,"materialized":65,"cohort_member_ticks":1040,"peak_members":1,"deaths":2,"buds":1,"mutations":4,"reference_ns":66749928,"population_ns":79618241,"conversion_ns":66623,"max_state_error":0} +{"kind":"paired","seed":3,"holdout":true,"rich":true,"ticks":8192,"conversion_width":64,"collapsed":16,"materialized":16,"cohort_member_ticks":1024,"peak_members":1,"deaths":2,"buds":1,"mutations":4,"reference_ns":69393875,"population_ns":84295460,"conversion_ns":20125,"max_state_error":0} +{"kind":"paired","seed":3,"holdout":true,"rich":false,"ticks":8192,"conversion_width":1,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":31509866,"population_ns":30254577,"conversion_ns":178899,"max_state_error":0} +{"kind":"paired","seed":3,"holdout":true,"rich":false,"ticks":8192,"conversion_width":16,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":30723658,"population_ns":29980614,"conversion_ns":19559,"max_state_error":0} +{"kind":"paired","seed":3,"holdout":true,"rich":false,"ticks":8192,"conversion_width":64,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":30027737,"population_ns":29407690,"conversion_ns":6849,"max_state_error":0} +{"kind":"capacity","lives":16,"samples":32,"p50_step_ns":27464,"p95_step_ns":58384,"max_step_ns":69180,"snapshot_bytes":21108} +{"kind":"capacity","lives":64,"samples":32,"p50_step_ns":156652,"p95_step_ns":200302,"max_step_ns":206407,"snapshot_bytes":71076} +{"kind":"capacity","lives":128,"samples":32,"p50_step_ns":463772,"p95_step_ns":777120,"max_step_ns":860498,"snapshot_bytes":137700} +{"kind":"capacity","lives":256,"samples":32,"p50_step_ns":1642881,"p95_step_ns":2326166,"max_step_ns":2346854,"snapshot_bytes":270948} diff --git a/docs/engineering/results/m12-2d-cohorts-msvc1944.jsonl b/docs/engineering/results/m12-2d-cohorts-msvc1944.jsonl new file mode 100644 index 0000000..d45dc50 --- /dev/null +++ b/docs/engineering/results/m12-2d-cohorts-msvc1944.jsonl @@ -0,0 +1,29 @@ +{"kind":"metadata","model":"exact-cellular-cohorts-1","rules":8,"quantum_us":50000,"regional_approximation":false,"compression_claimed":false,"runtime_enabled":false} +{"kind":"paired","seed":0,"holdout":false,"rich":true,"ticks":8192,"conversion_width":1,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":0,"buds":0,"mutations":4,"reference_ns":79518200,"population_ns":78399100,"conversion_ns":294100,"max_state_error":0} +{"kind":"paired","seed":0,"holdout":false,"rich":true,"ticks":8192,"conversion_width":16,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":0,"buds":0,"mutations":4,"reference_ns":78496800,"population_ns":77822200,"conversion_ns":23600,"max_state_error":0} +{"kind":"paired","seed":0,"holdout":false,"rich":true,"ticks":8192,"conversion_width":64,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":0,"buds":0,"mutations":4,"reference_ns":80076700,"population_ns":79481900,"conversion_ns":8100,"max_state_error":0} +{"kind":"paired","seed":0,"holdout":false,"rich":false,"ticks":8192,"conversion_width":1,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":36539800,"population_ns":35949100,"conversion_ns":268800,"max_state_error":0} +{"kind":"paired","seed":0,"holdout":false,"rich":false,"ticks":8192,"conversion_width":16,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":35103900,"population_ns":34638700,"conversion_ns":16400,"max_state_error":0} +{"kind":"paired","seed":0,"holdout":false,"rich":false,"ticks":8192,"conversion_width":64,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":34786100,"population_ns":34289600,"conversion_ns":5500,"max_state_error":0} +{"kind":"paired","seed":1,"holdout":false,"rich":true,"ticks":8192,"conversion_width":1,"collapsed":1048,"materialized":1048,"cohort_member_ticks":1048,"peak_members":1,"deaths":0,"buds":1,"mutations":4,"reference_ns":114624300,"population_ns":142200400,"conversion_ns":526600,"max_state_error":0} +{"kind":"paired","seed":1,"holdout":false,"rich":true,"ticks":8192,"conversion_width":16,"collapsed":65,"materialized":65,"cohort_member_ticks":1040,"peak_members":1,"deaths":0,"buds":1,"mutations":4,"reference_ns":111942600,"population_ns":140512100,"conversion_ns":63700,"max_state_error":0} +{"kind":"paired","seed":1,"holdout":false,"rich":true,"ticks":8192,"conversion_width":64,"collapsed":16,"materialized":16,"cohort_member_ticks":1024,"peak_members":1,"deaths":0,"buds":1,"mutations":4,"reference_ns":110333100,"population_ns":139143200,"conversion_ns":20900,"max_state_error":0} +{"kind":"paired","seed":1,"holdout":false,"rich":false,"ticks":8192,"conversion_width":1,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":36818400,"population_ns":35576600,"conversion_ns":290700,"max_state_error":0} +{"kind":"paired","seed":1,"holdout":false,"rich":false,"ticks":8192,"conversion_width":16,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":37690000,"population_ns":37046500,"conversion_ns":21900,"max_state_error":0} +{"kind":"paired","seed":1,"holdout":false,"rich":false,"ticks":8192,"conversion_width":64,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":35733400,"population_ns":35013500,"conversion_ns":6000,"max_state_error":0} +{"kind":"paired","seed":2,"holdout":true,"rich":true,"ticks":8192,"conversion_width":1,"collapsed":92,"materialized":92,"cohort_member_ticks":92,"peak_members":2,"deaths":1,"buds":2,"mutations":4,"reference_ns":89974500,"population_ns":91528800,"conversion_ns":331400,"max_state_error":0} +{"kind":"paired","seed":2,"holdout":true,"rich":true,"ticks":8192,"conversion_width":16,"collapsed":4,"materialized":4,"cohort_member_ticks":64,"peak_members":2,"deaths":1,"buds":2,"mutations":4,"reference_ns":90020800,"population_ns":92138900,"conversion_ns":32800,"max_state_error":0} +{"kind":"paired","seed":2,"holdout":true,"rich":true,"ticks":8192,"conversion_width":64,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":1,"buds":2,"mutations":4,"reference_ns":91760300,"population_ns":93397100,"conversion_ns":10500,"max_state_error":0} +{"kind":"paired","seed":2,"holdout":true,"rich":false,"ticks":8192,"conversion_width":1,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":36148400,"population_ns":35062700,"conversion_ns":284800,"max_state_error":0} +{"kind":"paired","seed":2,"holdout":true,"rich":false,"ticks":8192,"conversion_width":16,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":36434900,"population_ns":35393500,"conversion_ns":25000,"max_state_error":0} +{"kind":"paired","seed":2,"holdout":true,"rich":false,"ticks":8192,"conversion_width":64,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":36644500,"population_ns":36073900,"conversion_ns":7100,"max_state_error":0} +{"kind":"paired","seed":3,"holdout":true,"rich":true,"ticks":8192,"conversion_width":1,"collapsed":1048,"materialized":1048,"cohort_member_ticks":1048,"peak_members":1,"deaths":2,"buds":1,"mutations":4,"reference_ns":96796400,"population_ns":124074500,"conversion_ns":466300,"max_state_error":0} +{"kind":"paired","seed":3,"holdout":true,"rich":true,"ticks":8192,"conversion_width":16,"collapsed":65,"materialized":65,"cohort_member_ticks":1040,"peak_members":1,"deaths":2,"buds":1,"mutations":4,"reference_ns":95656900,"population_ns":123448500,"conversion_ns":44100,"max_state_error":0} +{"kind":"paired","seed":3,"holdout":true,"rich":true,"ticks":8192,"conversion_width":64,"collapsed":16,"materialized":16,"cohort_member_ticks":1024,"peak_members":1,"deaths":2,"buds":1,"mutations":4,"reference_ns":96276900,"population_ns":123619300,"conversion_ns":15100,"max_state_error":0} +{"kind":"paired","seed":3,"holdout":true,"rich":false,"ticks":8192,"conversion_width":1,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":35880600,"population_ns":35016700,"conversion_ns":279700,"max_state_error":0} +{"kind":"paired","seed":3,"holdout":true,"rich":false,"ticks":8192,"conversion_width":16,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":35098200,"population_ns":34524500,"conversion_ns":18600,"max_state_error":0} +{"kind":"paired","seed":3,"holdout":true,"rich":false,"ticks":8192,"conversion_width":64,"collapsed":0,"materialized":0,"cohort_member_ticks":0,"peak_members":0,"deaths":4,"buds":0,"mutations":0,"reference_ns":36080000,"population_ns":35380700,"conversion_ns":6200,"max_state_error":0} +{"kind":"capacity","lives":16,"samples":32,"p50_step_ns":38600,"p95_step_ns":45000,"max_step_ns":50800,"snapshot_bytes":21108} +{"kind":"capacity","lives":64,"samples":32,"p50_step_ns":255500,"p95_step_ns":274400,"max_step_ns":297800,"snapshot_bytes":71076} +{"kind":"capacity","lives":128,"samples":32,"p50_step_ns":694500,"p95_step_ns":773300,"max_step_ns":799500,"snapshot_bytes":137700} +{"kind":"capacity","lives":256,"samples":32,"p50_step_ns":2246300,"p95_step_ns":2358700,"max_step_ns":2375100,"snapshot_bytes":270948} diff --git a/experiments/resolution/cellular_cohorts.cpp b/experiments/resolution/cellular_cohorts.cpp new file mode 100644 index 0000000..3e1823d --- /dev/null +++ b/experiments/resolution/cellular_cohorts.cpp @@ -0,0 +1,162 @@ +// ADR-0139: paired qualification of exact retained-member cohorts, not compression. +#include "../../tests/fixtures/cellular_ecology.hpp" +#include "evo/server/lifecycle/cellular_population.hpp" + +#include + +namespace { +using namespace evo; +using namespace evo::server; +using namespace evo::fixture; +using Clock = std::chrono::steady_clock; + +std::uint64_t paired(std::uint64_t seed, bool rich, std::uint64_t ticks, std::uint64_t width) { + auto reference = opening(seed, rich); + auto population = checked(CellularPopulation::create(restore(*reference)), "cohort owner"); + std::set mutated; + std::uint64_t collapsed = 0, materialized = 0, cohort_ticks = 0, max_members = 0; + std::uint64_t reference_ns = 0, population_ns = 0, conversions_ns = 0; + const auto initial = reference->snapshot(); + require(!population->collapse(100), "controlled founder collapsed"); + require(!population->materialize(100), "missing cohort materialized"); + require(population->state().snapshot() == initial, "refused conversion mutated state"); + require(!population->advance_tick({}), "missing controlled intentions admitted"); + require(population->state().snapshot() == initial, "refused tick mutated state"); + for (std::uint64_t step = 0; step < ticks; ++step) { + if (step % width == 0) { + const auto start = Clock::now(); + std::set genomes; + for (const auto& life : population->state().lives()) + if (life.account == LifeAccount{}) + genomes.insert(life.identity.genome); + for (const auto genome : genomes) { + // Whole-genome oscillation is deliberate: an adversarial conversion may + // not select a better member, heal one, refresh its age or duplicate stock. + if ((step / width) % 2 == 0) { + collapsed += checked(population->collapse(genome), "collapse"); + require(checked(population->collapse(genome), "repeat collapse") == 0, + "collapse duplicated membership"); + } else if (std::ranges::any_of(population->members(), [genome](const auto& m) { + return m.genome == genome; + })) { + materialized += checked(population->materialize(genome), "materialize"); + require(!population->materialize(genome), "materialized twice"); + } + } + conversions_ns += static_cast( + std::chrono::duration_cast(Clock::now() - start).count()); + require(population->state().snapshot() == reference->snapshot(), + "conversion changed retained biology"); + } + const auto input = inputs(*reference, seed, mutated); + std::vector controlled; + for (const auto& life : reference->lives()) + if (life.account != LifeAccount{}) { + const auto found = std::ranges::find(input.lives, life.identity.life, + &CellularLifeIntent::life_id); + require(found != input.lives.end(), "missing fixture player"); + controlled.push_back(*found); + require(!population->in_cohort(life.identity.life), "controlled cohort member"); + } + auto start = Clock::now(); + const auto executed = checked( + reference->advance_tick_with_mutations(input.lives, input.mutations), "reference tick"); + reference_ns += static_cast( + std::chrono::duration_cast(Clock::now() - start).count()); + start = Clock::now(); + const auto candidate = + checked(population->advance_tick(controlled, input.mutations), "population tick"); + population_ns += static_cast( + std::chrono::duration_cast(Clock::now() - start).count()); + require(candidate.buds == executed.buds && candidate.mutations == executed.mutations, + "population events diverged"); + for (const auto& mutation : executed.mutations) + if (mutation.result == CellularMutationResult::applied) + mutated.insert(mutation.life_id); + require(population->state().snapshot() == reference->snapshot(), + "population demographic/biomass/diversity/history divergence"); + require(population->state().current_total() == population->state().opening_total(), + "population inventory divergence"); + for (const auto& member : population->members()) { + const auto life = + std::ranges::find(population->state().lives(), member.life, + [](const auto& cell) { return cell.identity.life; }); + require(life != population->state().lives().end() && life->account == LifeAccount{} && + life->presence == CellularPresence::active && + life->identity.genome == member.genome, + "invalid ownership tag"); + } + cohort_ticks += population->members().size(); + max_members = + std::max(max_members, static_cast(population->members().size())); + } + const auto summary = checked(summarize_cellular_play(*reference), "population census"); + std::cout << "{\"kind\":\"paired\",\"seed\":" << seed + << ",\"holdout\":" << (seed >= 2 ? "true" : "false") + << ",\"rich\":" << (rich ? "true" : "false") << ",\"ticks\":" << ticks + << ",\"conversion_width\":" << width << ",\"collapsed\":" << collapsed + << ",\"materialized\":" << materialized << ",\"cohort_member_ticks\":" << cohort_ticks + << ",\"peak_members\":" << max_members << ",\"deaths\":" << summary.deaths + << ",\"buds\":" << summary.buds << ",\"mutations\":" << summary.mutations + << ",\"reference_ns\":" << reference_ns << ",\"population_ns\":" << population_ns + << ",\"conversion_ns\":" << conversions_ns << ",\"max_state_error\":0}\n"; + if (rich && seed == 1 && ticks >= 8192) + require(collapsed > 0 && materialized > 0 && cohort_ticks > 0, + "long qualification missed cohort conversion"); + return cohort_ticks; +} + +void capacity(std::uint32_t count) { + auto population = + checked(CellularPopulation::create(opening(7, true, count)), "capacity owner"); + std::vector samples; + for (unsigned tick = 0; tick < 32; ++tick) { + std::vector players; + for (const auto& life : population->state().lives()) + players.push_back({life.identity.life, {}}); + const auto start = Clock::now(); + checked(population->advance_tick(players), "capacity tick"); + samples.push_back(static_cast( + std::chrono::duration_cast(Clock::now() - start).count())); + require(population->state().lives().size() == count, "capacity silently culled a life"); + } + if (count == biology::max_cellular_tick_lives) { + const auto before = population->state().snapshot(); + LifeRequest request{}; + request.account.front() = 1; + request.action = 1; + const auto refused = population->state().birth_candidate( + request, {1000, 1000, 1000, 1000, {population->state().tick()}}); + require(!refused && refused.error() == CellularLivesError::no_placement, + "capacity failed to backpressure"); + require(before == population->state().snapshot(), "capacity refusal changed state"); + } + std::ranges::sort(samples); + std::cout << "{\"kind\":\"capacity\",\"lives\":" << count + << ",\"samples\":32,\"p50_step_ns\":" << samples[16] + << ",\"p95_step_ns\":" << samples[30] << ",\"max_step_ns\":" << samples.back() + << ",\"snapshot_bytes\":" << population->state().snapshot().size() << "}\n"; +} +} // namespace + +int main(int argc, char** argv) try { + if (argc > 2 || (argc == 2 && std::string_view{argv[1]} != "--verify")) + return 2; + std::cout << "{\"kind\":\"metadata\",\"model\":\"exact-cellular-cohorts-1\"," + "\"rules\":8,\"quantum_us\":50000,\"regional_approximation\":false," + "\"compression_claimed\":false,\"runtime_enabled\":false}\n"; + std::uint64_t member_ticks = 0; + for (std::uint64_t seed = 0; seed < 4; ++seed) + for (const bool rich : {true, false}) + for (const auto width : {1ULL, 16ULL, 64ULL}) + member_ticks += paired(seed, rich, argc == 1 ? 8192 : 512, width); + if (argc == 2) + member_ticks += paired(1, true, 8192, 16); + require(member_ticks > 0, "qualification missed all cohort members"); + for (const auto count : {16U, 64U, 128U, 256U}) + capacity(count); + return 0; +} catch (const std::exception& error) { + std::fprintf(stderr, "%s\n", error.what()); + return 1; +} diff --git a/experiments/resolution/cellular_reference.cpp b/experiments/resolution/cellular_reference.cpp index e35065f..fc7db23 100644 --- a/experiments/resolution/cellular_reference.cpp +++ b/experiments/resolution/cellular_reference.cpp @@ -1,4 +1,5 @@ // ADR-0138: current cellular exact control; no aggregate model or runtime policy. +#include "../../tests/fixtures/cellular_ecology.hpp" #include "evo/server/lifecycle/cellular_batch.hpp" #include "evo/server/lifecycle/cellular_play_summary.hpp" #include "evo/server/lifecycle/wildlife_controller.hpp" @@ -23,26 +24,9 @@ namespace { using namespace evo; using namespace evo::server; using accounting::Inventory; -using Owner = std::unique_ptr; +using namespace evo::fixture; using Clock = std::chrono::steady_clock; -void require(bool condition, const char* message) { - if (!condition) - throw std::runtime_error(message); -} -template T checked(std::expected result, const char* message) { - if (!result) - throw std::runtime_error(std::string{message} + ": " + - std::to_string(static_cast(result.error()))); - return std::move(*result); -} - -Owner restore(const CellularLives& state) { - return checked(CellularLives::restore(state.snapshot(), state.world(), state.region(), - state.frame(), state.tick()), - "reference restore"); -} - std::uint64_t fingerprint(const CellularLives& state) { std::uint64_t value = 14695981039346656037ULL; for (const auto byte : state.snapshot()) @@ -50,133 +34,6 @@ std::uint64_t fingerprint(const CellularLives& state) { return value; // Reproducibility fingerprint only, never an authenticity proof. } -Owner opening(std::uint64_t seed, bool rich) { - constexpr std::uint32_t side = 6'000; - const auto world = 200 + seed; - const auto endowment = checked(biology::primitive_endowment(biology::primitive_founder_v3), - "reference founder endowment"); - auto funding = endowment.body; - funding.energy.value = 4 * (endowment.reserve.energy.value + endowment.construction.value); - for (auto& channel : funding.matter) - channel.value *= 4; - const std::vector sites{ - {1'000, 1'000}, {1'000, 4'000}, {4'000, 1'000}, {4'000, 4'000}}; - auto primitive = - checked(PrimitiveLives::create({world, 1, 1, 17, funding, sites}), "reference nursery"); - std::vector pools; - for (const auto& site : sites) { - biology::CellularPatch pool; - pool.address = {static_cast(site.x_um), - static_cast(site.y_um)}; - pool.radius_um = 500; - pool.source = 18; - pool.stock.energy.value = rich ? 1'000'000'000'000ULL : 0; - for (auto& channel : pool.stock.matter) - channel.value = rich ? 1'000'000'000'000ULL : 1'000; - pools.push_back(pool); - } - Inventory amount{{rich ? 1'000'000ULL : 10'000ULL}, {}}; - for (auto& channel : amount.matter) - channel.value = rich ? 100'000 : 10; - auto config = checked( - biology::scatter_deposits({world, 1, 1, side, side, {}}, pools, {64, 19, amount, seed}), - "reference deposits"); - if (rich) { - // Explicit finite contact food on each founding site. It is not a reserve grant - // or replenishment; the owner must acquire and spend it through real biology. - for (const auto& site : sites) - for (std::uint32_t channel = 0; channel < 5; ++channel) { - biology::CellularPatch mote; - mote.address = {static_cast(site.x_um) + 10 + channel * 2, - static_cast(site.y_um) + 10}; - mote.source = 20; - mote.id = config.next_deposit++; - mote.radius_um = biology::cellular_mote_radius_um; - mote.kind = biology::CellularDepositKind::mote; - if (channel == 0) - mote.stock.energy.value = 1'000'000'000'000ULL; - else - mote.stock.matter[channel - 1].value = 1'000'000'000'000ULL; - config.patches.push_back(mote); - } - std::ranges::sort(config.patches, {}, &biology::CellularPatch::address); - } - auto habitat = - checked(biology::CellularHabitat::create(std::move(config)), "reference habitat"); - auto state = - checked(CellularLives::create(*primitive, std::move(habitat), {{side, side}, 0, 293'150}, - CellularLivesProfile::foraging), - "reference cellular owner"); - require(state->advance_tick(std::span{}).has_value(), - "reference genesis tick"); - for (std::uint64_t index = 0; index < 4; ++index) { - LifeRequest request{}; - request.account.back() = static_cast(index + 1); - request.action = 1; - const auto identity = 100 + index; - state = checked(state->birth_candidate( - request, {identity, identity, identity, identity, {state->tick()}}), - "reference funded birth"); - } - require(state->rules_version() == cellular_lives_foraging_rules && - state->habitat().config().format == biology::cellular_habitat_deposit_format, - "reference version bundle"); - return state; -} - -CellularTickIntents inputs(const CellularLives& state, std::uint64_t seed, - const std::set& mutated) { - CellularTickIntents tick; - const auto wildlife = checked(wildlife_intents(state, state.tick() + 1), "reference wildlife"); - for (const auto& life : state.lives()) { - CellularLifeIntent intent{life.identity.life, {}}; - if (life.account == LifeAccount{}) { - const auto found = - std::ranges::find(wildlife, life.identity.life, &CellularLifeIntent::life_id); - require(found != wildlife.end(), "reference missing wildlife intention"); - intent = *found; - } else { - const auto phase = (state.tick() + 1) % 1024; - intent.intent.frozen = life.identity.life == 100 && phase >= 128 && phase < 384; - if (!intent.intent.frozen) { - // Fixture players request real actions; the owner alone admits/funds them. - if ((state.tick() + 1 + life.identity.life) % 100 == 0) - intent.intent.bud = true; - else if ((state.tick() + 1) % 197 == 0) - intent.intent.engulf = true; - else if (state.tick() >= 4096) { - const auto direction = (state.tick() / 64 + life.identity.life + seed) % 4; - constexpr std::array directions{ - {{1'000, 0}, {0, 1'000}, {-1'000, 0}, {0, -1'000}}}; - intent.intent.movement = directions[direction]; - } - } - if (!intent.intent.frozen && life.presence == CellularPresence::active && - !mutated.contains(life.identity.life) && life.mutation.offer) { - const auto genome = - checked(state.archive().find(life.identity.genome), "reference offered genome"); - const auto& offer = *life.mutation.offer; - for (std::size_t offset = 0; offset < offer.candidates.size(); ++offset) { - const auto& choice = - offer.candidates[(offset + seed) % offer.candidates.size()]; - const auto successor = 1'000'000 + state.tick() * 4 + life.identity.life - 100; - const auto funded = - biology::fund_mutation(genome, offer.context, offer, choice.operator_id, - successor, successor, life.reserve); - if (funded) { - tick.mutations.push_back({life.identity.life, life.mutation.event_index, - life.mutation.offer_tick, successor, - choice.operator_id}); - break; - } - } - } - } - tick.lives.push_back(intent); - } - return tick; -} - struct Replay { std::uint64_t width{}; Owner state; diff --git a/include/evo/server/lifecycle/cellular_population.hpp b/include/evo/server/lifecycle/cellular_population.hpp new file mode 100644 index 0000000..14f3e45 --- /dev/null +++ b/include/evo/server/lifecycle/cellular_population.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include "evo/server/lifecycle/cellular_lives.hpp" + +#include +#include +#include +#include +#include +#include + +namespace evo::server { +// ADR-0139: exact local cohorts retain individuals, never mean reserves or samples. +// The single owned CellularLives store backs BOTH representations, exactly once. +struct CellularCohortMember { + std::uint64_t life{}, genome{}; + auto operator<=>(const CellularCohortMember&) const = default; +}; +enum class CellularPopulationError : std::uint8_t { unsupported, absent, ineligible }; + +class CellularPopulation { + public: + [[nodiscard]] static std::expected, CellularPopulationError> + create(std::unique_ptr state); + CellularPopulation(const CellularPopulation&) = delete; + CellularPopulation& operator=(const CellularPopulation&) = delete; + + // Read-only biology view: it confers no separate spending rights. + [[nodiscard]] const CellularLives& state() const noexcept; + [[nodiscard]] std::span members() const noexcept; + [[nodiscard]] bool in_cohort(std::uint64_t life) const noexcept; + + // Convert the WHOLE eligible wildlife genome group. No selectable healthy subset, + // resampling, rounding, new identity, healing, age reset or inventory transfer. + // Controlled lives are never members. Refusal leaves biology and ownership unchanged. + [[nodiscard]] std::expected + collapse(std::uint64_t genome); + [[nodiscard]] std::expected + materialize(std::uint64_t genome); + + // Only controlled-life intentions/selections are supplied by the host, sorted by ID. + // All wildlife, including cohorts, uses the unchanged shared controller and kernel. + [[nodiscard]] std::expected + advance_tick(std::span controlled, + std::span selections = {}); + + private: + explicit CellularPopulation(std::unique_ptr state); + std::unique_ptr state_; + std::vector members_; // Sorted life IDs, unique ownership tags. +}; +} // namespace evo::server diff --git a/src/server/lifecycle/cellular_population.cpp b/src/server/lifecycle/cellular_population.cpp new file mode 100644 index 0000000..8cd8449 --- /dev/null +++ b/src/server/lifecycle/cellular_population.cpp @@ -0,0 +1,92 @@ +#include "evo/server/lifecycle/cellular_population.hpp" + +#include "evo/server/lifecycle/wildlife_controller.hpp" + +#include +#include + +namespace evo::server { +CellularPopulation::CellularPopulation(std::unique_ptr state) + : state_(std::move(state)) {} + +std::expected, CellularPopulationError> +CellularPopulation::create(std::unique_ptr state) { + if (!state || state->rules_version() != cellular_lives_foraging_rules) + return std::unexpected(CellularPopulationError::unsupported); + return std::unique_ptr(new CellularPopulation(std::move(state))); +} +const CellularLives& CellularPopulation::state() const noexcept { + return *state_; +} +std::span CellularPopulation::members() const noexcept { + return members_; +} +bool CellularPopulation::in_cohort(std::uint64_t life) const noexcept { + return std::ranges::binary_search(members_, life, {}, &CellularCohortMember::life); +} +std::expected +CellularPopulation::collapse(std::uint64_t genome) { + auto candidate = members_; + std::size_t converted = 0; + bool found = false; + for (const auto& life : state_->lives()) { + if (life.identity.genome != genome || life.account != LifeAccount{}) + continue; + found = true; + if (life.presence != CellularPresence::active) + return std::unexpected(CellularPopulationError::ineligible); + if (!in_cohort(life.identity.life)) { + candidate.push_back({life.identity.life, genome}); + ++converted; + } + } + if (!found) + return std::unexpected(CellularPopulationError::absent); + std::ranges::sort(candidate); + members_.swap(candidate); + return converted; +} +std::expected +CellularPopulation::materialize(std::uint64_t genome) { + const auto removed = + std::erase_if(members_, [genome](const auto& m) { return m.genome == genome; }); + if (removed == 0) + return std::unexpected(CellularPopulationError::absent); + return removed; +} +std::expected +CellularPopulation::advance_tick(std::span controlled, + std::span selections) { + auto wildlife = wildlife_intents(*state_, state_->tick() + 1); + if (!wildlife) + return std::unexpected(wildlife.error()); + std::vector intents; + intents.reserve(state_->lives().size()); + std::size_t player = 0, autonomous = 0; + for (const auto& life : state_->lives()) { + if (life.account == LifeAccount{}) { + intents.push_back((*wildlife)[autonomous++]); + } else { + if (player == controlled.size() || controlled[player].life_id != life.identity.life) + return std::unexpected(CellularLivesError::invalid_intents); + intents.push_back(controlled[player++]); + } + } + if (player != controlled.size()) + return std::unexpected(CellularLivesError::invalid_intents); + for (const auto& selection : selections) { + if (std::ranges::find(controlled, selection.life_id, &CellularLifeIntent::life_id) == + controlled.end()) + return std::unexpected(CellularLivesError::invalid_intents); + } + auto executed = state_->advance_tick_with_mutations(intents, selections); + if (!executed) + return std::unexpected(executed.error()); + std::erase_if(members_, [this](const auto& member) { + return std::ranges::find(state_->lives(), member.life, [](const auto& life) { + return life.identity.life; + }) == state_->lives().end(); + }); + return executed; +} +} // namespace evo::server diff --git a/tests/fixtures/cellular_ecology.hpp b/tests/fixtures/cellular_ecology.hpp new file mode 100644 index 0000000..cbd03c2 --- /dev/null +++ b/tests/fixtures/cellular_ecology.hpp @@ -0,0 +1,180 @@ +#pragma once + +// Current rules-8 funded reference fixture, shared by population qualification. +#include "evo/server/lifecycle/cellular_batch.hpp" +#include "evo/server/lifecycle/cellular_play_summary.hpp" +#include "evo/server/lifecycle/wildlife_controller.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace evo::fixture { +using namespace evo; +using namespace evo::server; +using accounting::Inventory; +using Owner = std::unique_ptr; + +inline void require(bool condition, const char* message) { + if (!condition) + throw std::runtime_error(message); +} +template T checked(std::expected result, const char* message) { + if (!result) + throw std::runtime_error(std::string{message} + ": " + + std::to_string(static_cast(result.error()))); + return std::move(*result); +} + +inline Owner restore(const CellularLives& state) { + return checked(CellularLives::restore(state.snapshot(), state.world(), state.region(), + state.frame(), state.tick()), + "reference restore"); +} + +inline Owner opening(std::uint64_t seed, bool rich, std::uint32_t count = 4) { + require(count > 0 && count <= biology::max_cellular_tick_lives, "fixture population bound"); + const std::uint32_t side = count == 4 ? 6'000 : 48'000; + const auto world = 200 + seed; + const auto endowment = checked(biology::primitive_endowment(biology::primitive_founder_v3), + "reference founder endowment"); + auto funding = endowment.body; + funding.energy.value = count * (endowment.reserve.energy.value + endowment.construction.value); + for (auto& channel : funding.matter) + channel.value *= count; + std::vector sites{{1'000, 1'000}, {1'000, 4'000}, {4'000, 1'000}, {4'000, 4'000}}; + if (count != 4) { + sites.clear(); + for (std::int32_t index = 0; index < static_cast(count); ++index) + sites.push_back({1'000 + (index / 16) * 3'000, 1'000 + (index % 16) * 3'000}); + } + auto primitive = + checked(PrimitiveLives::create({world, 1, 1, 17, funding, sites}), "reference nursery"); + std::vector pools; + for (const auto& site : sites) { + biology::CellularPatch pool; + pool.address = {static_cast(site.x_um), + static_cast(site.y_um)}; + pool.radius_um = 500; + pool.source = 18; + pool.stock.energy.value = rich ? 1'000'000'000'000ULL : 0; + for (auto& channel : pool.stock.matter) + channel.value = rich ? 1'000'000'000'000ULL : 1'000; + pools.push_back(pool); + } + Inventory amount{{rich ? 1'000'000ULL : 10'000ULL}, {}}; + for (auto& channel : amount.matter) + channel.value = rich ? 100'000 : 10; + auto config = checked( + biology::scatter_deposits({world, 1, 1, side, side, {}}, pools, {64, 19, amount, seed}), + "reference deposits"); + if (rich) { + // Explicit finite contact food on each founding site. It is not a reserve grant + // or replenishment; the owner must acquire and spend it through real biology. + for (const auto& site : sites) + for (std::uint32_t channel = 0; channel < 5; ++channel) { + biology::CellularPatch mote; + mote.address = {static_cast(site.x_um) + 10 + channel * 2, + static_cast(site.y_um) + 10}; + mote.source = 20; + mote.id = config.next_deposit++; + mote.radius_um = biology::cellular_mote_radius_um; + mote.kind = biology::CellularDepositKind::mote; + if (channel == 0) + mote.stock.energy.value = 1'000'000'000'000ULL; + else + mote.stock.matter[channel - 1].value = 1'000'000'000'000ULL; + config.patches.push_back(mote); + } + std::ranges::sort(config.patches, {}, &biology::CellularPatch::address); + } + auto habitat = + checked(biology::CellularHabitat::create(std::move(config)), "reference habitat"); + auto state = + checked(CellularLives::create(*primitive, std::move(habitat), {{side, side}, 0, 293'150}, + CellularLivesProfile::foraging), + "reference cellular owner"); + require(state->advance_tick(std::span{}).has_value(), + "reference genesis tick"); + for (std::uint64_t index = 0; index < count; ++index) { + LifeRequest request{}; + request.account.back() = static_cast(index + 1); + request.account[request.account.size() - 2] = static_cast((index + 1) >> 8U); + request.action = 1; + const auto identity = 100 + index; + state = checked(state->birth_candidate( + request, {identity, identity, identity, identity, {state->tick()}}), + "reference funded birth"); + } + require(state->rules_version() == cellular_lives_foraging_rules && + state->habitat().config().format == biology::cellular_habitat_deposit_format, + "reference version bundle"); + return state; +} + +inline CellularTickIntents inputs(const CellularLives& state, std::uint64_t seed, + const std::set& mutated) { + CellularTickIntents tick; + const auto wildlife = checked(wildlife_intents(state, state.tick() + 1), "reference wildlife"); + for (const auto& life : state.lives()) { + CellularLifeIntent intent{life.identity.life, {}}; + if (life.account == LifeAccount{}) { + const auto found = + std::ranges::find(wildlife, life.identity.life, &CellularLifeIntent::life_id); + require(found != wildlife.end(), "reference missing wildlife intention"); + intent = *found; + } else { + const auto phase = (state.tick() + 1) % 1024; + intent.intent.frozen = life.identity.life == 100 && phase >= 128 && phase < 384; + if (!intent.intent.frozen) { + // Fixture players request real actions; the owner alone admits/funds them. + if ((state.tick() + 1 + life.identity.life) % 100 == 0) + intent.intent.bud = true; + else if ((state.tick() + 1) % 197 == 0) + intent.intent.engulf = true; + else if (state.tick() >= 4096) { + const auto direction = (state.tick() / 64 + life.identity.life + seed) % 4; + constexpr std::array directions{ + {{1'000, 0}, {0, 1'000}, {-1'000, 0}, {0, -1'000}}}; + intent.intent.movement = directions[direction]; + } + } + if (!intent.intent.frozen && life.presence == CellularPresence::active && + !mutated.contains(life.identity.life) && life.mutation.offer) { + const auto genome = + checked(state.archive().find(life.identity.genome), "reference offered genome"); + const auto& offer = *life.mutation.offer; + for (std::size_t offset = 0; offset < offer.candidates.size(); ++offset) { + const auto& choice = + offer.candidates[(offset + seed) % offer.candidates.size()]; + const auto successor = 1'000'000 + state.tick() * 4 + life.identity.life - 100; + const auto funded = + biology::fund_mutation(genome, offer.context, offer, choice.operator_id, + successor, successor, life.reserve); + if (funded) { + tick.mutations.push_back({life.identity.life, life.mutation.event_index, + life.mutation.offer_tick, successor, + choice.operator_id}); + break; + } + } + } + } + tick.lives.push_back(intent); + } + return tick; +} + +} // namespace evo::fixture