Files
aevolution/tests/region_checkpoint_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

259 lines
12 KiB
C++

#include "evo/persistence/region_checkpoint.hpp"
#include "fixtures/environmental_region.hpp"
#include <cstdio>
#include <source_location>
#include <stdexcept>
#include <string>
#include <string_view>
namespace {
using namespace evo::persistence;
using evo::test::advance_region;
using evo::test::region_community;
using evo::test::region_input;
void check(bool ok, std::source_location at = std::source_location::current()) {
if (!ok)
throw std::runtime_error("region checkpoint line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
template <std::size_t N> std::array<std::uint8_t, N> filled(std::uint8_t value) {
std::array<std::uint8_t, N> out{};
out.fill(value);
return out;
}
constexpr RegionKey key{7, 3};
const auto build = filled<32>(0xB1);
LeaseGrant lease() {
return {filled<16>(0x11), 4, 9, std::chrono::milliseconds{20000}};
}
CheckpointManifest genesis(std::uint64_t payload_bytes = 100) {
auto g =
genesis_manifest(key, 9, filled<16>(0x01), 50000, build, payload_bytes, filled<32>(0xA0));
CHECK(g);
return *g;
}
CheckpointManifest next(std::uint64_t end_tick = 16) {
auto n = successor_manifest(genesis(), filled<32>(0xD0), filled<16>(0x02), lease(), end_tick,
build, 200, filled<32>(0xA1));
CHECK(n);
return *n;
}
std::uint64_t at(const std::vector<std::uint8_t>& b, std::size_t offset, unsigned width) {
std::uint64_t value{};
for (unsigned i = 0; i < width; ++i)
value |= static_cast<std::uint64_t>(b[offset + i]) << (8U * i);
return value;
}
void layout_and_round_trip() {
const auto m = next();
auto bytes = encode_manifest(m);
CHECK(bytes && bytes->size() == checkpoint_manifest_bytes);
// Golden offsets from schemas/region-commit.md.
CHECK(std::string_view(reinterpret_cast<const char*>(bytes->data()), 8) == "AEVORCM1");
CHECK(at(*bytes, 8, 4) == 1 && at(*bytes, 12, 4) == 1);
CHECK(at(*bytes, 16, 8) == 7 && at(*bytes, 24, 8) == 3 && at(*bytes, 32, 8) == 9);
CHECK((*bytes)[40] == 0x02 && at(*bytes, 56, 8) == 1 && (*bytes)[64] == 0x01);
CHECK((*bytes)[80] == 0xD0 && at(*bytes, 112, 8) == 0 && at(*bytes, 120, 8) == 16);
CHECK((*bytes)[128] == 0x11 && at(*bytes, 144, 8) == 4 && at(*bytes, 152, 8) == 50000);
CHECK(at(*bytes, 160, 4) == 2 && at(*bytes, 164, 4) == 2 && at(*bytes, 192, 4) == 1);
CHECK((*bytes)[196] == 0xB1 && at(*bytes, 228, 4) == 0 && at(*bytes, 232, 4) == 0);
CHECK(at(*bytes, 236, 4) == 1 && at(*bytes, 240, 8) == 200 && (*bytes)[248] == 0xA1);
auto decoded = decode_manifest(*bytes);
CHECK(decoded && *decoded == m && supported(*decoded));
auto g = encode_manifest(genesis());
CHECK(g && decode_manifest(*g) == genesis());
}
void malformed_and_unsupported() {
auto bytes = *encode_manifest(next());
CHECK(decode_manifest(std::span{bytes}.first(279)).error() == CheckpointError::format);
auto grown = bytes;
grown.push_back(0);
CHECK(decode_manifest(grown).error() == CheckpointError::format);
auto magic = bytes;
magic[0] ^= 1U;
CHECK(decode_manifest(magic).error() == CheckpointError::format);
auto version = bytes;
version[8] = 2;
CHECK(decode_manifest(version).error() == CheckpointError::unsupported);
// Structurally invalid: zero world, 65-tick batch, oversized payload, zero digest.
for (const auto& [offset, value] :
{std::pair{16, 0}, std::pair{120, 65}, std::pair{242, 0x11}, std::pair{144, 0}}) {
auto b = bytes;
b[static_cast<std::size_t>(offset)] = static_cast<std::uint8_t>(value);
if (offset == 144)
std::fill_n(b.begin() + 144, 8, std::uint8_t{0}); // Epoch zero off genesis.
CHECK(decode_manifest(b).error() == CheckpointError::format);
}
auto sha = bytes;
std::fill_n(sha.begin() + 248, 32, std::uint8_t{0});
CHECK(decode_manifest(sha).error() == CheckpointError::format);
// Parsable but not admitted by this build: decode succeeds, supported() refuses.
// 13/237 make profile/durability 257: never truncated onto a supported value.
for (const std::size_t offset : {12U, 13U, 160U, 164U, 192U, 228U, 232U, 236U, 237U}) {
auto b = bytes;
b[offset] = static_cast<std::uint8_t>(b[offset] + 1);
auto d = decode_manifest(b);
CHECK(d && !supported(*d));
}
auto g = genesis();
g.holder = filled<16>(1);
CHECK(encode_manifest(g).error() == CheckpointError::format);
g = genesis();
g.end_tick = 1;
CHECK(encode_manifest(g).error() == CheckpointError::format);
CHECK(
!genesis_manifest(key, 9, filled<16>(1), 50000, build, 1024 * 1024 + 1, filled<32>(0xA0)));
CHECK(!genesis_manifest(key, 0, filled<16>(1), 50000, build, 1, filled<32>(0xA0)));
}
void successor_chain() {
const auto g = genesis();
const auto d = filled<32>(0xD0);
const auto payload = filled<32>(0xA1);
CHECK(successor_manifest(g, d, filled<16>(2), lease(), 0, build, 1, payload).error() ==
CheckpointError::chain);
CHECK(successor_manifest(g, d, filled<16>(2), lease(), 65, build, 1, payload).error() ==
CheckpointError::chain);
auto foreign = lease();
foreign.frame = 8;
CHECK(successor_manifest(g, d, filled<16>(2), foreign, 1, build, 1, payload).error() ==
CheckpointError::chain);
CHECK(successor_manifest(g, d, g.commit, lease(), 1, build, 1, payload).error() ==
CheckpointError::format); // Reused commit identity.
auto old = g;
old.versions.rules = 1;
CHECK(successor_manifest(old, d, filled<16>(2), lease(), 1, build, 1, payload).error() ==
CheckpointError::unsupported);
auto last = next();
last.sequence = ~std::uint64_t{0};
CHECK(successor_manifest(last, d, filled<16>(3), lease(), 17, build, 1, payload).error() ==
CheckpointError::chain);
const auto n = next(64);
CHECK(n.sequence == 1 && n.predecessor_commit == g.commit && n.predecessor_digest == d);
CHECK(n.begin_tick == 0 && n.holder == lease().holder && n.epoch == 4 && n.frame == 9);
}
void payload_validation_and_restored_trajectory() {
const RegionKey region{evo::test::region_identity.world, 3};
auto original = region_community();
auto opening = original->snapshot();
CHECK(opening);
auto g = genesis_manifest(region, evo::test::region_identity.frame, filled<16>(1), 50000, build,
opening->size(), filled<32>(0xA0));
CHECK(g && restore_environmental_payload(*opening, *g, 8000000));
advance_region(*original, 16);
auto payload = original->snapshot();
CHECK(payload);
auto lease_at_frame = lease();
lease_at_frame.frame = evo::test::region_identity.frame;
auto m = successor_manifest(*g, filled<32>(0xD0), filled<16>(2), lease_at_frame, 16, build,
payload->size(), filled<32>(0xA1));
CHECK(m);
auto restored = restore_environmental_payload(*payload, *m, 8000000);
CHECK(restored);
// The restored checkpoint continues exactly like the uninterrupted original.
for (std::uint64_t tick = 17; tick <= 40; ++tick) {
auto a = original->advance(tick, region_input(tick, true));
auto b = (*restored)->advance(tick, region_input(tick, true));
CHECK(a && b && *a == *b);
}
CHECK(original->snapshot() == (*restored)->snapshot());
const auto refused = [&](auto change, CheckpointError error) {
auto bytes = *payload;
auto manifest = *m;
change(bytes, manifest);
auto r = restore_environmental_payload(bytes, manifest, 8000000);
return !r && r.error() == error;
};
CHECK(refused([](auto&, auto& x) { x.end_tick = 15; }, CheckpointError::payload));
CHECK(refused([](auto&, auto& x) { x.key.world = 8; }, CheckpointError::payload));
CHECK(refused([](auto&, auto& x) { x.frame = 10; }, CheckpointError::payload));
CHECK(refused([](auto&, auto& x) { x.tick_microseconds = 25000; }, CheckpointError::payload));
CHECK(refused([](auto&, auto& x) { --x.payload_bytes; }, CheckpointError::payload));
CHECK(refused([](auto& b, auto&) { b[b.size() / 2] ^= 1U; }, CheckpointError::payload));
CHECK(refused([](auto&, auto& x) { x.versions.rules = 1; }, CheckpointError::unsupported));
CHECK(refused([](auto&, auto& x) { x.rng_streams = 1; }, CheckpointError::unsupported));
CHECK(refused([](auto&, auto& x) { x.history_profile = 1; }, CheckpointError::unsupported));
CHECK(refused([](auto& b, auto&) { b[8] = 1; }, CheckpointError::unsupported));
CHECK(!restore_environmental_payload(*payload, *m, 1000)); // Budget still charged.
}
void lineage_profile() {
// The SQL parser hard-codes this cap (migrations/0004_lineage_profile.sql).
static_assert(max_lineage_payload_bytes == 4268144);
static_assert(max_payload_bytes(CheckpointProfile::unknown) == 0);
const RegionKey region{evo::test::region_identity.world, 5};
auto state = evo::test::lineage_region();
auto opening = state->snapshot();
CHECK(opening);
auto g = genesis_manifest(region, evo::test::region_identity.frame, filled<16>(1), 50000, build,
opening->size(), filled<32>(0xA0),
CheckpointProfile::environmental_lineage_v1);
CHECK(g && g->history_profile == 1 && supported(*g));
auto bytes = encode_manifest(*g);
CHECK(bytes && at(*bytes, 12, 4) == 2 && at(*bytes, 232, 4) == 1);
CHECK(decode_manifest(*bytes) == *g);
// Profile and history profile travel together.
auto mixed = *g;
mixed.history_profile = 0;
CHECK(!supported(mixed));
mixed = genesis();
mixed.history_profile = 1;
CHECK(!supported(mixed));
// Per-profile payload caps are structural.
CHECK(!genesis_manifest(region, 9, filled<16>(1), 50000, build, (1024 * 1024) + 1,
filled<32>(0xA0)));
CHECK(genesis_manifest(region, 9, filled<16>(1), 50000, build, (1024 * 1024) + 1,
filled<32>(0xA0), CheckpointProfile::environmental_lineage_v1));
CHECK(!genesis_manifest(region, 9, filled<16>(1), 50000, build, 4268145, filled<32>(0xA0),
CheckpointProfile::environmental_lineage_v1));
auto restored = restore_lineage_payload(*opening, *g, 8000000);
CHECK(restored && (*restored)->archive().recorded() == 3);
CHECK(restore_environmental_payload(*opening, *g, 8000000).error() ==
CheckpointError::unsupported);
auto plain = evo::test::region_community()->snapshot();
auto env =
genesis_manifest(region, 9, filled<16>(1), 50000, build, plain->size(), filled<32>(0xA0));
CHECK(restore_lineage_payload(*plain, *env, 8000000).error() == CheckpointError::unsupported);
// Successors inherit the profile; a lineage batch with a death restores exactly.
evo::test::advance_lineage(*state, 24, true);
auto payload = state->snapshot();
auto lease_at_frame = lease();
lease_at_frame.frame = evo::test::region_identity.frame;
auto m = successor_manifest(*g, filled<32>(0xD0), filled<16>(2), lease_at_frame, 24, build,
payload->size(), filled<32>(0xA1));
CHECK(m && m->profile == CheckpointProfile::environmental_lineage_v1 &&
m->history_profile == 1);
auto later = restore_lineage_payload(*payload, *m, 8000000);
CHECK(later && !(*later)->archive().organism(11)->alive);
auto wrong_tick = *m;
wrong_tick.end_tick = 23;
CHECK(restore_lineage_payload(*payload, wrong_tick, 8000000).error() ==
CheckpointError::payload);
auto nested = *payload;
nested[64] = 1; // Nested envelope format 1 is not this profile's envelope.
CHECK(restore_lineage_payload(nested, *m, 8000000).error() == CheckpointError::unsupported);
}
} // namespace
int main() {
try {
layout_and_round_trip();
malformed_and_unsupported();
successor_chain();
payload_validation_and_restored_trajectory();
lineage_profile();
std::puts("region checkpoint contracts passed");
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
return 1;
}
}