Files
aevolution/experiments/resolution/cellular_cohorts.cpp
T
dingo 2b074b9ae1
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
M12.2: select and qualify exact cellular local cohorts
2026-10-17 12:04:39 -04:00

163 lines
8.9 KiB
C++

// 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;
}