M12.4: record and replay exact biological transfer pairs
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m30s
Foundation / checks (client) (push) Failing after 18m38s
Foundation / checks (network) (push) Successful in 22m21s
Foundation / checks (tsan) (push) Failing after 22m17s
Foundation / checks (postgres) (push) Successful in 51m32s
Foundation / checks (checks) (push) Failing after 59m20s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped

This commit is contained in:
dingo
2026-10-17 18:23:33 -04:00
parent decd4f8043
commit 1e207b5ee4
13 changed files with 637 additions and 2 deletions
+195 -1
View File
@@ -1,7 +1,9 @@
#include "evo/server/lifecycle/cellular_migration.hpp"
#include "evo/server/lifecycle/cellular_migration_trace.hpp"
#include "evo/server/lifecycle/cellular_population_batch.hpp"
#include "fixtures/cellular_ecology.hpp"
#include <fstream>
#include <limits>
namespace {
@@ -140,6 +142,186 @@ Owner decode(const CellularLives& trusted, const std::vector<std::uint8_t>& byte
trusted.tick()),
"migration owner restore");
}
template <std::size_t N> std::array<std::uint8_t, N> filled(std::uint8_t value) {
std::array<std::uint8_t, N> bytes{};
bytes.fill(value);
return bytes;
}
CellularMigrationRequest request_for(const CellularLives& source, const CellularLives& target) {
return {filled<16>(7),
{source.region(), filled<16>(1), filled<16>(3), filled<32>(5)},
{target.region(), filled<16>(2), filled<16>(4), filled<32>(6)},
wildlife_genome(source)};
}
void trace_contract() {
auto source = checked(funded()->migration_upgrade_candidate(), "trace source");
auto target = checked(destination(*source, 1)->migration_upgrade_candidate(), "trace target");
const auto before = source->snapshot(), peer_before = target->snapshot();
const auto request = request_for(*source, *target);
auto recorded = checked(prepare_migration_trace(*source, *target, request), "record pair");
const auto bytes = checked(encode_migration_trace(recorded.trace), "encode pair");
require(bytes.size() == migration_trace_header_bytes + 8 * recorded.trace.members.size() + 8,
"trace length changed");
const auto trace = checked(decode_migration_trace(bytes), "decode pair");
require(trace == recorded.trace, "trace canonical roundtrip");
auto replayed =
checked(replay_migration_trace(*source, *target, request, trace), "replay pair");
require(replayed.source->snapshot() == recorded.execution.source->snapshot() &&
replayed.destination->snapshot() == recorded.execution.destination->snapshot(),
"recorded pair replay diverged");
require(!replay_migration_trace(*replayed.source, *replayed.destination, request, trace),
"trace applied again to its result");
auto repeated =
checked(replay_migration_trace(*source, *target, request, trace), "repeat replay");
require(repeated.source->snapshot() == replayed.source->snapshot() &&
repeated.destination->snapshot() == replayed.destination->snapshot(),
"same immutable inputs replayed differently");
auto altered = trace;
++altered.transferred.energy.value;
require(!replay_migration_trace(*source, *target, request, altered), "forged transfer amount");
altered = trace;
altered.members.front() += 10'000;
require(!replay_migration_trace(*source, *target, request, altered), "forged member set");
auto trusted = request;
trusted.destination.predecessor_digest.front() ^= 1;
require(!replay_migration_trace(*source, *target, trusted, trace),
"wrong destination cut binding");
trusted = request;
trusted.source.commit.front() ^= 1;
require(!replay_migration_trace(*source, *target, trusted, trace),
"wrong result commit binding");
altered = trace;
++altered.tick;
require(!replay_migration_trace(*source, *target, request, altered), "wrong trace tick");
altered = trace;
std::swap(altered.request.source, altered.request.destination);
require(!replay_migration_trace(*source, *target, request, altered),
"reversed trace direction");
auto invalid = request;
invalid.request = {};
require(!prepare_migration_trace(*source, *target, invalid), "zero transfer request");
invalid = request;
invalid.destination.commit = invalid.source.commit;
require(!prepare_migration_trace(*source, *target, invalid), "one commit for both heads");
for (std::size_t length = 0; length < bytes.size(); ++length)
require(!decode_migration_trace(std::span{bytes}.first(length)), "truncated pair trace");
for (std::size_t offset = 0; offset < bytes.size(); ++offset) {
auto corrupt = bytes;
corrupt[offset] ^= 1;
require(!decode_migration_trace(corrupt), "undetected pair corruption");
}
for (const auto offset :
{std::size_t{8}, std::size_t{12}, std::size_t{248}, std::size_t{252}}) {
auto malformed = bytes;
put(malformed, offset, UINT32_MAX, 4);
checksum(malformed);
require(!decode_migration_trace(malformed), "unbounded/reserved pair header");
}
altered = trace;
altered.members.clear();
for (std::uint64_t i = 1; i <= biology::max_cellular_tick_lives; ++i)
altered.members.push_back(cellular_bud_identity(3, i));
auto maximum = checked(encode_migration_trace(altered), "maximum pair codec");
require(maximum.size() == max_migration_trace_bytes &&
decode_migration_trace(maximum).has_value(),
"bounded maximum trace refused");
require(!replay_migration_trace(*source, *target, request, altered),
"synthetic member evidence");
maximum.push_back(0);
require(!decode_migration_trace(maximum), "oversized pair trace");
auto population = checked(CellularPopulation::create(decode(*source, before)), "trace cohort");
checked(population->collapse(request.genome), "trace collapse");
auto peer =
checked(CellularPopulation::create(decode(*target, peer_before), {false, 1000, 2000}),
"trace cohort peer");
auto pop_recorded =
checked(prepare_migration_trace(*population, *peer, request), "record cohort pair");
auto pop_trace =
checked(decode_migration_trace(
checked(encode_migration_trace(pop_recorded.trace), "encode cohort trace")),
"decode cohort trace");
auto pop_replayed = checked(replay_migration_trace(*population, *peer, request, pop_trace),
"replay cohort pair");
require(pop_replayed.source->snapshot() == pop_recorded.execution.source->snapshot() &&
pop_replayed.destination->snapshot() ==
pop_recorded.execution.destination->snapshot() &&
std::ranges::equal(pop_replayed.destination->members(), population->members()),
"cohort pair replay lost representation");
require(!replay_migration_trace(*population, *peer, request, trace) &&
!replay_migration_trace(*source, *target, request, pop_trace),
"trace representation confused");
require(source->snapshot() == before && target->snapshot() == peer_before,
"trace validation changed its input");
}
void write_bytes(const std::string& path, std::span<const std::uint8_t> bytes) {
std::ofstream out(path, std::ios::binary | std::ios::trunc);
out.write(reinterpret_cast<const char*>(bytes.data()),
static_cast<std::streamsize>(bytes.size()));
out.close();
require(out.good(), "restart fixture write");
}
std::vector<std::uint8_t> read_bytes(const std::string& path) {
std::ifstream in(path, std::ios::binary | std::ios::ate);
const auto length = in.tellg();
require(length > 0 && length <= static_cast<std::streamoff>(max_cellular_population_bytes),
"restart fixture bound");
std::vector<std::uint8_t> bytes(static_cast<std::size_t>(length));
in.seekg(0);
in.read(reinterpret_cast<char*>(bytes.data()), static_cast<std::streamsize>(bytes.size()));
require(in.good(), "restart fixture read");
return bytes;
}
void record_restart(const std::string& prefix) {
auto source = checked(funded()->migration_upgrade_candidate(), "restart source");
auto target = checked(destination(*source, 1)->migration_upgrade_candidate(), "restart target");
const auto request = request_for(*source, *target);
auto recorded = checked(prepare_migration_trace(*source, *target, request), "restart raw pair");
write_bytes(prefix + ".raw.source", source->snapshot());
write_bytes(prefix + ".raw.destination", target->snapshot());
write_bytes(prefix + ".raw.trace",
checked(encode_migration_trace(recorded.trace), "restart raw trace"));
write_bytes(prefix + ".raw.source.expected", recorded.execution.source->snapshot());
write_bytes(prefix + ".raw.destination.expected", recorded.execution.destination->snapshot());
auto population = checked(CellularPopulation::create(std::move(source)), "restart population");
auto peer = checked(CellularPopulation::create(std::move(target)), "restart peer");
checked(population->collapse(request.genome), "restart cohort collapse");
auto pop =
checked(prepare_migration_trace(*population, *peer, request), "restart population pair");
write_bytes(prefix + ".pop.source", population->snapshot());
write_bytes(prefix + ".pop.destination", peer->snapshot());
write_bytes(prefix + ".pop.trace",
checked(encode_migration_trace(pop.trace), "restart pop trace"));
write_bytes(prefix + ".pop.source.expected", pop.execution.source->snapshot());
write_bytes(prefix + ".pop.destination.expected", pop.execution.destination->snapshot());
}
void replay_restart(const std::string& prefix) {
const auto raw =
checked(decode_migration_trace(read_bytes(prefix + ".raw.trace")), "fresh raw trace");
auto source =
checked(CellularLives::restore(read_bytes(prefix + ".raw.source"), 201, 1, 1, raw.tick),
"fresh source");
auto target = checked(
CellularLives::restore(read_bytes(prefix + ".raw.destination"), 201, 2, 1, raw.tick),
"fresh target");
const auto trusted =
request_for(*source, *target); // Fixed fixture IDs, not an inferred live grant.
auto replayed =
checked(replay_migration_trace(*source, *target, trusted, raw), "fresh raw replay");
write_bytes(prefix + ".raw.source.resumed", replayed.source->snapshot());
write_bytes(prefix + ".raw.destination.resumed", replayed.destination->snapshot());
const auto pop =
checked(decode_migration_trace(read_bytes(prefix + ".pop.trace")), "fresh pop trace");
auto population = checked(
CellularPopulation::restore(read_bytes(prefix + ".pop.source"), 201, 1, 1, pop.tick),
"fresh cohort");
auto peer = checked(
CellularPopulation::restore(read_bytes(prefix + ".pop.destination"), 201, 2, 1, pop.tick),
"fresh peer");
auto pop_replayed = checked(replay_migration_trace(*population, *peer, trusted, pop),
"fresh population replay");
write_bytes(prefix + ".pop.source.resumed", pop_replayed.source->snapshot());
write_bytes(prefix + ".pop.destination.resumed", pop_replayed.destination->snapshot());
}
void upgrade_and_move() {
auto original = funded();
auto other = destination(*original, 1);
@@ -446,13 +628,25 @@ void local_budding_among_imports() {
"local children displaced foreign namespace ordering");
}
} // namespace
int main() try {
int main(int argc, char** argv) try {
if (argc == 3) {
const std::string_view mode{argv[1]};
if (mode == "--record")
record_restart(argv[2]);
else if (mode == "--replay")
replay_restart(argv[2]);
else
throw std::runtime_error("unknown migration fixture mode");
return 0;
}
require(argc == 1, "migration test arguments");
upgrade_and_move();
refusal_and_codec();
capacity();
population_envelope();
overlap_refusal();
local_budding_among_imports();
trace_contract();
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';