M12.2: select and qualify exact cellular local cohorts
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m19s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 6m24s
Foundation / checks (network) (push) Canceled after 6m33s
Foundation / checks (postgres) (push) Canceled after 6m34s
Foundation / checks (checks) (push) Canceled after 6m44s
Foundation / checks (client) (push) Canceled after 6m44s

This commit is contained in:
dingo
2026-10-17 12:04:39 -04:00
parent bee57e5829
commit 2b074b9ae1
17 changed files with 762 additions and 149 deletions
+162
View File
@@ -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 <chrono>
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<std::uint64_t> 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<std::uint64_t> 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::uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(Clock::now() - start).count());
require(population->state().snapshot() == reference->snapshot(),
"conversion changed retained biology");
}
const auto input = inputs(*reference, seed, mutated);
std::vector<CellularLifeIntent> 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::uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(Clock::now() - start).count());
start = Clock::now();
const auto candidate =
checked(population->advance_tick(controlled, input.mutations), "population tick");
population_ns += static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(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<std::uint64_t>(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<std::uint64_t> samples;
for (unsigned tick = 0; tick < 32; ++tick) {
std::vector<CellularLifeIntent> 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::uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(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;
}
+2 -145
View File
@@ -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<CellularLives>;
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 <typename T, typename E> T checked(std::expected<T, E> result, const char* message) {
if (!result)
throw std::runtime_error(std::string{message} + ": " +
std::to_string(static_cast<unsigned>(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<SpawnSite> 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<biology::CellularPatch> pools;
for (const auto& site : sites) {
biology::CellularPatch pool;
pool.address = {static_cast<std::uint32_t>(site.x_um),
static_cast<std::uint32_t>(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<std::uint32_t>(site.x_um) + 10 + channel * 2,
static_cast<std::uint32_t>(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<const CellularLifeIntent>{}).has_value(),
"reference genesis tick");
for (std::uint64_t index = 0; index < 4; ++index) {
LifeRequest request{};
request.account.back() = static_cast<std::uint8_t>(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<std::uint64_t>& 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<biology::CellularIntent, 4> 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;