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>
356 lines
17 KiB
C++
356 lines
17 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;
|
|
}
|
|
// Synthetic batch evidence of the right size for `ticks` (content is not checked here).
|
|
BatchDigests batch_for(std::uint64_t ticks, std::uint32_t decisions = 0) {
|
|
return {trace_bytes_for(ticks), filled<32>(0x7C), decisions,
|
|
decisions == 0 ? Sha256Digest{} : filled<32>(0x7D)};
|
|
}
|
|
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), batch_for(end_tick, 1));
|
|
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) == 2 && 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);
|
|
CHECK(at(*bytes, 8, 4) == 2 && at(*bytes, 280, 8) == trace_bytes_for(16));
|
|
CHECK((*bytes)[288] == 0x7C && at(*bytes, 320, 4) == 1 && at(*bytes, 324, 4) == 0);
|
|
CHECK((*bytes)[328] == 0x7D);
|
|
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(359)).error() == CheckpointError::format);
|
|
// Version 1 (M7.4 commits) still decodes with no batch evidence; v2 is never
|
|
// written as v1 and v1 bytes cannot carry batch fields.
|
|
std::vector<std::uint8_t> v1(bytes.begin(), bytes.begin() + 280);
|
|
v1[8] = 1;
|
|
auto old = decode_manifest(v1);
|
|
CHECK(old && old->manifest_version == 1 && old->batch == BatchDigests{});
|
|
CHECK(!encode_manifest(*old));
|
|
auto stretched = bytes;
|
|
stretched[8] = 1;
|
|
CHECK(decode_manifest(stretched).error() == CheckpointError::format);
|
|
auto reserved = bytes;
|
|
reserved[324] = 1;
|
|
CHECK(decode_manifest(reserved).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] = 3;
|
|
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, batch_for(1))
|
|
.error() == CheckpointError::chain);
|
|
CHECK(successor_manifest(g, d, filled<16>(2), lease(), 65, build, 1, payload, batch_for(1))
|
|
.error() == CheckpointError::chain);
|
|
auto foreign = lease();
|
|
foreign.frame = 8;
|
|
CHECK(successor_manifest(g, d, filled<16>(2), foreign, 1, build, 1, payload, batch_for(1))
|
|
.error() == CheckpointError::chain);
|
|
CHECK(successor_manifest(g, d, g.commit, lease(), 1, build, 1, payload, batch_for(1)).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, batch_for(1))
|
|
.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, batch_for(1))
|
|
.error() == CheckpointError::chain);
|
|
// Batch evidence must match the tick range: one trace step per tick.
|
|
CHECK(successor_manifest(g, d, filled<16>(2), lease(), 2, build, 1, payload, batch_for(1))
|
|
.error() == CheckpointError::format);
|
|
auto untraced = batch_for(1);
|
|
untraced.trace_sha256 = {};
|
|
CHECK(
|
|
successor_manifest(g, d, filled<16>(2), lease(), 1, build, 1, payload, untraced).error() ==
|
|
CheckpointError::format);
|
|
auto unsigned_decisions = batch_for(1, 1);
|
|
unsigned_decisions.decisions_sha256 = {};
|
|
CHECK(successor_manifest(g, d, filled<16>(2), lease(), 1, build, 1, payload, unsigned_decisions)
|
|
.error() == CheckpointError::format);
|
|
CHECK(successor_manifest(g, d, filled<16>(2), lease(), 1, build, 1, payload, batch_for(1, 9))
|
|
.error() == CheckpointError::format);
|
|
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), batch_for(16));
|
|
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;
|
|
x.batch = batch_for(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), batch_for(24));
|
|
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;
|
|
wrong_tick.batch = batch_for(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);
|
|
}
|
|
// ADR-0067 clone genesis: sequence 0 at the source tick, naming its origin.
|
|
void clone_genesis() {
|
|
const auto source = next(16);
|
|
const auto digest = filled<32>(0xD1);
|
|
const RegionKey target{8, 3};
|
|
const BatchDigests inherited{0, {}, 1, filled<32>(0x7E)};
|
|
auto c =
|
|
clone_manifest(source, digest, target, filled<16>(0x05), 300, filled<32>(0xA5), inherited);
|
|
CHECK(c && c->sequence == 0 && c->key == target && c->frame == source.frame);
|
|
CHECK(c->begin_tick == 16 && c->end_tick == 16 && c->predecessor_commit == source.commit);
|
|
CHECK(c->predecessor_digest == digest && c->epoch == 0 && c->holder == Uuid{});
|
|
CHECK(c->versions == source.versions && c->profile == source.profile &&
|
|
c->tick_microseconds == source.tick_microseconds && c->build == source.build);
|
|
auto bytes = encode_manifest(*c);
|
|
CHECK(bytes && decode_manifest(*bytes) == *c && supported(*c));
|
|
// A successor chains from the clone exactly as from any genesis.
|
|
auto after = successor_manifest(*c, filled<32>(0xD2), filled<16>(0x06), lease(), 20, build, 1,
|
|
filled<32>(0xA6), batch_for(4));
|
|
CHECK(after && after->sequence == 1 && after->begin_tick == 16 && after->key == target);
|
|
// Isolation and shape: another world, a fresh commit, no trace, bounded outcomes.
|
|
CHECK(
|
|
clone_manifest(source, digest, {7, 4}, filled<16>(5), 300, filled<32>(0xA5), {}).error() ==
|
|
CheckpointError::chain);
|
|
CHECK(
|
|
clone_manifest(source, digest, target, source.commit, 300, filled<32>(0xA5), {}).error() ==
|
|
CheckpointError::chain);
|
|
CHECK(clone_manifest(source, digest, target, filled<16>(5), 300, filled<32>(0xA5),
|
|
{trace_bytes_for(1), filled<32>(1), 0, {}})
|
|
.error() == CheckpointError::chain);
|
|
CHECK(
|
|
clone_manifest(source, digest, target, filled<16>(5), 300, filled<32>(0xA5), {0, {}, 1, {}})
|
|
.error() == CheckpointError::format);
|
|
CHECK(clone_manifest(source, digest, target, filled<16>(5), 300, filled<32>(0xA5),
|
|
{0, {}, 9, filled<32>(1)})
|
|
.error() == CheckpointError::format);
|
|
CHECK(clone_manifest(source, {}, target, filled<16>(5), 300, filled<32>(0xA5), {}).error() ==
|
|
CheckpointError::format);
|
|
auto old = source;
|
|
old.versions.rules = 1;
|
|
CHECK(clone_manifest(old, digest, target, filled<16>(5), 300, filled<32>(0xA5), {}).error() ==
|
|
CheckpointError::unsupported);
|
|
// A provisioned genesis still starts at tick 0 with no origin or outcomes.
|
|
auto g = genesis();
|
|
g.batch = inherited;
|
|
CHECK(encode_manifest(g).error() == CheckpointError::format);
|
|
// Manifest v1 never carried an origin.
|
|
auto v1 = *c;
|
|
v1.manifest_version = 1;
|
|
v1.batch = {};
|
|
CHECK(encode_manifest(v1).error() == CheckpointError::format);
|
|
// Half an origin is malformed.
|
|
auto half = *c;
|
|
half.predecessor_commit = {};
|
|
CHECK(encode_manifest(half).error() == CheckpointError::format);
|
|
}
|
|
} // namespace
|
|
|
|
int main() {
|
|
try {
|
|
layout_and_round_trip();
|
|
malformed_and_unsupported();
|
|
successor_chain();
|
|
payload_validation_and_restored_trajectory();
|
|
lineage_profile();
|
|
clone_genesis();
|
|
std::puts("region checkpoint contracts passed");
|
|
return 0;
|
|
} catch (const std::exception& e) {
|
|
std::fprintf(stderr, "%s\n", e.what());
|
|
return 1;
|
|
}
|
|
}
|