Files
aevolution/tests/lineage_region_tests.cpp
T
dingoandClaude Opus 5.5 703f67834f Complete M7.9a region clones and retained-history audit
Clone a verified committed head into an isolated world and database
(ADR-0067): world re-identification with a proven byte round trip, a
schema v7 clone genesis naming the source commit and digest with digested
inherited outcomes, a read-only source session, an owner-only atomic
target transaction and the evo-world-clone tool. audit_region_history
re-verifies every retained link. Real-process tests cover trajectory
equality after the cut, source isolation, killed clones and retention.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 06:47:45 -04:00

182 lines
8.6 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.
}
// ADR-0067: a clone records another world ID; nothing else about the region, its
// history or its trajectory changes, and rebinding back is byte-exact.
void rebinding_changes_only_the_world() {
auto source = lineage_region();
advance_lineage(*source, 25, true); // Consumer dead at tick 20.
const auto original = source->snapshot();
CHECK(original);
auto clone = LineageRegion::restore(*original, region_identity, 8000000);
CHECK(clone);
CHECK(!(*clone)->rebind_world(0));
CHECK((*clone)->rebind_world(8) && (*clone)->community().world() == 8);
const auto rebound = (*clone)->snapshot();
CHECK(rebound && *rebound != *original && rebound->size() == original->size());
// Only the new identity restores it; the source identity no longer does.
CHECK(!LineageRegion::restore(*rebound, region_identity, 8000000));
auto restored = LineageRegion::restore(*rebound, {8, region_identity.frame}, 8000000);
CHECK(restored && (*restored)->archive().organism(11)->died.value == 20);
CHECK((*restored)->binding() == source->binding());
// Identical continuation: every report matches, and so does the state once the
// clone is rebound to the source world.
for (std::uint64_t tick = 26; tick <= 90; ++tick) {
auto a = source->advance(tick, evo::test::region_input(tick));
auto b = (*restored)->advance(tick, evo::test::region_input(tick));
CHECK(a && b && *a == *b);
}
CHECK((*restored)->rebind_world(region_identity.world));
CHECK((*restored)->snapshot() == source->snapshot());
// The plain community rebinds the same way.
auto community = region_community();
const auto before = community->snapshot();
CHECK(community->rebind_world(9) && community->rebind_world(region_identity.world));
CHECK(community->snapshot() == before);
}
} // namespace
int main() {
try {
closure_is_required();
deaths_are_recorded_in_the_same_tick();
snapshots_restore_and_continue();
rebinding_changes_only_the_world();
std::puts("lineage region contracts passed");
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
return 1;
}
}