Files
aevolution/tests/lineage_region_tests.cpp
T
dingoandClaude Opus 5.5 a2c4543df2 Complete M7.4b lineage checkpoint profile; M7.4 done
ADR-0062 adds a canonical LineageArchive codec (parents before children,
organisms by id, FNV) that decodes by replaying admit/record_birth/
record_death and requires byte-identical re-encoding; it never overwrites
an existing archive. LineageRegion binds the environmental producer and
consumer accounts to archived organisms with identity high-waters,
enforces closure (lifecycle vs deaths, exact living set, tick bounds)
and records deaths in the tick that causes them.

Checkpoint profile 2 (environmental-lineage-v1, history profile 1)
publishes that container; schema v4 admits it with a 4,268,144-byte cap
and requires each commit to keep its predecessor's profile, RNG, history
and durability declarations. Linux adds provision/publish/recover for
the profile on the shared publication path.

Qualification found WSL2 stepping CLOCK_REALTIME back ~0.5 s every ~32 s,
which made two 1 s-lease tests flaky under ASan. Tests now wait on
database-time expiry; ADR-0060 records the slewed-clock requirement.

Validated: Windows CTest 57/57; Linux GCC Debug/Release, clang-tidy and
ASan/UBSan CTest 57/57 each; PostgreSQL suites 2x Release, 2x analysis,
5x ASan; SQL and C++ mutations each caught. No Docker/Gitea CI, remote
store, process kill or deployment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 16:43:10 -04:00

147 lines
6.8 KiB
C++

#include "evo/processes/lineage_region.hpp"
#include "fixtures/environmental_region.hpp"
#include <cstdio>
#include <source_location>
#include <stdexcept>
#include <string>
namespace {
using namespace evo::processes;
using evo::Tick;
using evo::biology::LineageArchive;
using evo::test::advance_lineage;
using evo::test::lineage_region;
using evo::test::region_archive;
using evo::test::region_binding;
using evo::test::region_community;
using evo::test::region_identity;
void check(bool ok, std::source_location at = std::source_location::current()) {
if (!ok)
throw std::runtime_error("lineage region line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
bool refused(const LineageArchive& archive, LineageBinding binding, LineageRegionError error) {
auto r = LineageRegion::create(region_community(), archive, binding);
return !r && r.error() == error;
}
void set(evo::world::WorldBytes& b, std::size_t at, std::uint64_t value) {
for (unsigned i = 0; i < 8; ++i)
b[at + i] = static_cast<std::uint8_t>((value >> (8U * i)) & 0xFFU);
}
void reseal(evo::world::WorldBytes& b) {
std::uint64_t h = 14695981039346656037ULL;
for (std::size_t i = 0; i + 8 < b.size(); ++i)
h = (h ^ b[i]) * 1099511628211ULL;
set(b, b.size() - 8, h);
}
void closure_is_required() {
CHECK(lineage_region());
CHECK(!LineageRegion::create(nullptr, region_archive(), region_binding));
const auto with = [](auto change) {
auto b = region_binding;
change(b);
return b;
};
const auto closure = LineageRegionError::closure;
CHECK(refused(region_archive(), with([](auto& b) { b.producer = 0; }), closure));
CHECK(refused(region_archive(), with([](auto& b) { b.consumer = b.producer; }), closure));
CHECK(refused(region_archive(), with([](auto& b) { b.consumer = 99; }), closure)); // Unknown.
CHECK(refused(region_archive(), with([](auto& b) { b.consumer = 5; }), closure)); // Dead.
CHECK(refused(region_archive(), with([](auto& b) { b.next_organism = 11; }), closure));
CHECK(refused(region_archive(), with([](auto& b) { b.next_genome = 2; }), closure));
// A living organism that no account owns is unowned state, not history.
auto stray = region_archive();
CHECK(stray.record_birth(12, 2, Tick{0}));
CHECK(refused(stray, region_binding, closure));
CHECK(refused(stray, with([](auto& b) { b.next_organism = 13; }), closure));
// Born in the future relative to the community tick.
LineageArchive future;
CHECK(future.admit(evo::test::region_genome(1, 0)) &&
future.admit(evo::test::region_genome(2, 1)));
CHECK(future.record_birth(10, 1, Tick{3}) && future.record_birth(11, 2, Tick{0}));
CHECK(refused(future, region_binding, closure));
// Archive says dead while the community's producer is active.
auto dead = region_archive();
CHECK(dead.record_death(10, Tick{0}));
CHECK(refused(dead, region_binding, closure));
}
void deaths_are_recorded_in_the_same_tick() {
auto region = lineage_region();
advance_lineage(*region, 19, true);
CHECK(region->archive().organism(11)->alive);
auto report = region->advance(20, evo::test::region_input(20, true));
CHECK(report && report->consumer_transition == 1);
const auto consumer = region->archive().organism(11);
CHECK(consumer && !consumer->alive && consumer->died.value == 20);
CHECK(region->community().progress().consumer_terminal == 1);
CHECK(region->archive().living_organisms() == std::vector<std::uint64_t>{10});
// The genome outlives its only carrier; ancestry still resolves.
CHECK(*region->archive().living_carriers(2) == 0 && region->archive().ancestry(2)->size() == 2);
// A refused tick changes neither the community nor the archive.
const auto before = region->snapshot();
CHECK(!region->advance(20, evo::test::region_input(20)));
CHECK(region->snapshot() == before);
}
void snapshots_restore_and_continue() {
auto original = lineage_region();
advance_lineage(*original, 24, true);
const auto bytes = original->snapshot();
CHECK(bytes && bytes->size() <= max_lineage_region_bytes);
auto restored = LineageRegion::restore(*bytes, region_identity, 8000000);
CHECK(restored && (*restored)->binding() == region_binding);
CHECK((*restored)->archive().encode() == original->archive().encode());
CHECK(!(*restored)->archive().organism(11)->alive);
for (std::uint64_t tick = 25; tick <= 60; ++tick) {
auto a = original->advance(tick, evo::test::region_input(tick));
auto b = (*restored)->advance(tick, evo::test::region_input(tick));
CHECK(a && b && *a == *b);
}
CHECK(original->snapshot() == (*restored)->snapshot());
const auto refused_with = [&](auto change, LineageRegionError error, bool seal = true) {
auto b = *bytes;
change(b);
if (seal)
reseal(b);
auto r = LineageRegion::restore(b, region_identity, 8000000);
return !r && r.error() == error;
};
CHECK(refused_with([](auto& b) { b[100] ^= 1U; }, LineageRegionError::checksum, false));
CHECK(refused_with([](auto& b) { b[0] ^= 1U; }, LineageRegionError::format));
CHECK(refused_with([](auto& b) { b[8] = 2; }, LineageRegionError::version));
CHECK(refused_with([](auto& b) { b[12] = 1; }, LineageRegionError::format));
CHECK(refused_with([](auto& b) { set(b, 48, 0); }, LineageRegionError::format));
CHECK(refused_with([&](auto& b) { set(b, 48, b.size()); }, LineageRegionError::format));
// Binding edits that break closure: a stale high-water or a swapped account.
CHECK(refused_with([](auto& b) { set(b, 32, 11); }, LineageRegionError::closure));
CHECK(refused_with([](auto& b) { set(b, 24, 10); }, LineageRegionError::closure));
// Nested damage: envelope byte and archive byte (each checked by its own codec).
CHECK(refused_with([](auto& b) { b[80] ^= 1U; }, LineageRegionError::community));
CHECK(refused_with([](auto& b) { b[b.size() - 20] ^= 1U; }, LineageRegionError::archive));
// An older checkpoint with the consumer still alive cannot pass as this one:
// the restored bytes are what the chain committed, nothing else is consulted.
auto earlier = lineage_region();
advance_lineage(*earlier, 10, true);
auto older = LineageRegion::restore(*earlier->snapshot(), region_identity, 8000000);
CHECK(older && (*older)->archive().organism(11)->alive);
CHECK(!LineageRegion::restore(*bytes, {7, 10}, 8000000)); // Foreign frame.
}
} // namespace
int main() {
try {
closure_is_required();
deaths_are_recorded_in_the_same_tick();
snapshots_restore_and_continue();
std::puts("lineage region contracts passed");
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
return 1;
}
}