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
+6
View File
@@ -88,6 +88,7 @@ evo_owned_target(evo_biology)
add_library(evo_lifecycle STATIC src/server/lifecycle/primitive_lives.cpp
src/server/lifecycle/primitive_batch.cpp src/server/lifecycle/cellular_lives.cpp
src/server/lifecycle/cellular_migration.cpp
src/server/lifecycle/cellular_migration_trace.cpp
src/server/lifecycle/cellular_batch.cpp src/server/lifecycle/wildlife_controller.cpp
src/server/lifecycle/cellular_play_summary.cpp src/server/lifecycle/cellular_population.cpp
src/server/lifecycle/cellular_population_codec.cpp src/server/lifecycle/cellular_population_policy.cpp
@@ -686,6 +687,11 @@ if(BUILD_TESTING)
evo_owned_target(evo-cellular-migration-tests)
add_test(NAME cellular-migration-contract COMMAND evo-cellular-migration-tests)
set_tests_properties(cellular-migration-contract PROPERTIES TIMEOUT 180)
add_test(NAME cellular-migration-process-restart COMMAND "${CMAKE_COMMAND}"
"-DMIGRATION_TEST=$<TARGET_FILE:evo-cellular-migration-tests>"
"-DTEST_DIR=${CMAKE_CURRENT_BINARY_DIR}/migration-restart"
-P "${CMAKE_CURRENT_SOURCE_DIR}/tests/cellular_migration_restart.cmake")
set_tests_properties(cellular-migration-process-restart PROPERTIES TIMEOUT 180)
add_executable(evo-population-checkpoint-tests tests/population_checkpoint_tests.cpp)
target_link_libraries(evo-population-checkpoint-tests PRIVATE evo::region_checkpoint)
evo_owned_target(evo-population-checkpoint-tests)
+11
View File
@@ -1,5 +1,16 @@
# Project status and next steps
## M12.4d paired biological transfer replay - 2026-10-17
Canonical bounded migration trace 1 binds both predecessor cuts, both resulting
commit IDs, the workflow request, representation and exact members/material.
Replay uses the separately trusted request and both source states. A fresh
process reproduces the complete source/destination snapshots for individual and
cohort owners. Native Release passes 78/78; Clang analysis/format, GCC 14 and
ASan/UBSan migration checks pass. [Contract and limits](../schemas/cellular-migration-trace-v1.md)
retain the I/O digest/fencing obligations: this trace grants no ownership and
does not close durable escrow, database retry/abort or destination activation.
## M12.4c portable biological transfer - 2026-10-17
ADR-0144 adds opt-in owner format 7 and population envelope 2 with exact paired
@@ -62,6 +62,16 @@ The caller's stable workflow request UUID belongs to that durable I/O journal;
it is not a biological identity or a kernel PRNG decision. Source/destination head
comparison prevents a second application under a different request UUID.
Portable paired trace 1 binds the stable request, both predecessor manifests,
both result commit IDs, representation, common cut, exact group and material.
Replay receives an independently trusted request and both complete source cuts;
it recomputes the paired candidates and verifies recorded membership and flows.
The trace is bounded/canonical and has no ownership authority by itself. The I/O
adapter must validate predecessor manifest/payload digests and journal bindings;
the trace's embedded request cannot supply that trust. Independent-process replay
qualifies the raw and population representations without implying durable escrow.
See [the trace schema](../../schemas/cellular-migration-trace-v1.md).
This primitive changes ownership at an explicitly compatible cut and preserves
coordinates. It does not choose a physical route or let current clamped movement
cross an arena edge. Automatic linked-habitat movement, boundary contacts and
+4
View File
@@ -55,6 +55,10 @@ no admitted durable manifest bundle yet. Existing profile-4/5 publishers retain
their tuples; population trace 1 refuses envelope 2. A future adapter must freeze
the source and atomically commit both fenced heads before destination activation.
See [scope and remaining proof](../engineering/M12_EXACT_BIOLOGICAL_TRANSFER.md).
Its bounded [paired migration trace](../../schemas/cellular-migration-trace-v1.md)
replays from both source cuts and an independently trusted request, including
complete cohort tags. Independent-process replay is qualified; no existing
durable publisher admits that trace or treats it as a transfer receipt.
World/region/frame are nonzero durable IDs. No ID is a Pod name. The host records
its unique holder ID and monotonically increasing nonzero lease epoch. Exhaustion
@@ -54,3 +54,19 @@ habitat field is inferred. Those need separately versioned contracts and fidelit
tests. M12.4 as a whole remains open, as do dependent M12.5–M12.9 gates.
Executed qualification is recorded in [VALIDATION](VALIDATION.md).
## M12.4d recorded paired replay
[Migration trace 1](../../schemas/cellular-migration-trace-v1.md) retains a bounded
canonical request/cut/member/flow record for both individual and cohort owners.
The portable replay API takes the separately trusted request and both predecessor
states, verifies all bindings and derives the exact pair again. A new process
restores both representations and reproduces all four source/destination output
snapshots byte-for-byte. Corruption, malformed bounds, semantic member/material
tampering, wrong predecessor/result IDs and representation confusion are refused.
This record provides reproducible evidence for the future paired publisher. It
has no freeze, fence, durable retry outcome or activation rights. Independently
establishing manifest/payload SHA-256 bindings and journal authority remains an
I/O obligation; neither a checksum nor a UUID grants ownership. The durable
escrow and linked-geometry limits above remain unchanged.
+3
View File
@@ -47,6 +47,9 @@ closed by adding a placeholder service or relabeling the non-biological probe.
ADR-0144's [portable transfer candidates](M12_EXACT_BIOLOGICAL_TRANSFER.md) resolve
the closed-owner/foreign-identity conflict with explicit flows and nonresident
history. They do not yet supply durable paired head publication or activation.
Paired transfer trace 1 now supplies bounded, independently replayable input and
member/material evidence, including raw/cohort separate-process reproduction.
It does not grant ownership or close database escrow/retry/partition qualification.
The transfer tests also expose the current rules-8 maturation gap: buds retain
their subadult realization while reproduction requires a fully realized parent.
Finite extra food alone does not close repeated wildlife generations. A versioned
+39
View File
@@ -1,5 +1,44 @@
# Validation report
## 2026-10-17 - M12.4d paired biological transfer trace and replay
ADR-0144's portable trace segment affects lifecycle replay/serialization, its
public API, CMake registration and contract/process-restart tests. It adds no
biological version, durable admission tuple, SQL, dependency, live flag or deploy.
Executed:
- MSVC 19.44 Release configure/build: **78/78 CTests passed**, 12.13 s.
- Clang 19 analysis build with clang-tidy: **2/2 migration CTests passed**,
13.71 s. Repository clang-format 19 gate passes on the final source.
- GCC 14 Release build: **2/2 migration CTests passed**, 1.89 s.
- Clang 19 ASan/UBSan: **2/2 migration CTests passed**, 46.68 s, no reports.
Tests record both predecessor/result-commit bindings and derive actual group/
stock evidence. They reject wrong trusted bindings, reversed direction, wrong
cut, changed member/material evidence and representation confusion. Codec checks
cover every truncation and single-byte corruption of the fixture, recomputed-
checksum malformed version/representation/count/reserved fields, maximum bounded
membership and oversized input. Same immutable inputs replay identically; applying
the record directly to its resulting pair is refused without changing inputs.
A separate executable invocation restores both individual and population inputs,
replays the retained records and writes four complete output snapshots. CMake
compares each to the uninterrupted source/destination result byte-for-byte,
including cohort tags, policy, sequence, nonresident history and boundary flows.
Tests use explicit runtime checks in NDEBUG. No golden was replaced.
Initial clang-tidy findings (overlarge enum storage and an internal returned
reference) were fixed before final analysis. Wire representation is still u32;
all values are checked before narrowing to the in-memory enum.
Limits: the I/O caller must independently establish both manifest/payload digest
bindings and trusted journal request. FNV/UUID fields grant no authority. These
tests do not qualify source freeze, two-lease atomic head publication, destination
activation, durable duplicate/abort/timeout outcomes or migration-specific process
faults. No SQL or publication adapter changed; M12.4c's affected private PostgreSQL
suite remains the latest database check. See the [trace contract](../../schemas/cellular-migration-trace-v1.md).
## 2026-10-17 - M12.4c portable exact biological transfer
ADR-0144 affects portable lifecycle ownership/cohort serialization, lineage
@@ -12,7 +12,9 @@ enum class CellularMigrationError : std::uint8_t {
placement,
archive,
overflow,
conservation
conservation,
format,
checksum
};
struct CellularMigrationExecution {
std::uint64_t genome{};
@@ -0,0 +1,59 @@
#pragma once
#include "evo/server/lifecycle/cellular_migration.hpp"
#include <array>
namespace evo::server {
struct CellularMigrationEndpoint {
std::uint64_t region{};
std::array<std::uint8_t, 16> predecessor{}, commit{};
std::array<std::uint8_t, 32> predecessor_digest{};
bool operator==(const CellularMigrationEndpoint&) const = default;
};
struct CellularMigrationRequest {
std::array<std::uint8_t, 16> request{};
CellularMigrationEndpoint source, destination;
std::uint64_t genome{};
bool operator==(const CellularMigrationRequest&) const = default;
};
enum class CellularMigrationRepresentation : std::uint8_t { individual = 1, population = 2 };
struct CellularMigrationTrace {
CellularMigrationRepresentation representation{CellularMigrationRepresentation::individual};
std::uint64_t world{}, frame{}, tick{};
CellularMigrationRequest request;
accounting::Inventory transferred;
std::vector<std::uint64_t> members;
bool operator==(const CellularMigrationTrace&) const = default;
};
inline constexpr std::size_t migration_trace_header_bytes = 256;
inline constexpr std::size_t max_migration_trace_bytes =
migration_trace_header_bytes + 8 * biology::max_cellular_tick_lives + 8;
struct RecordedCellularMigration {
CellularMigrationTrace trace;
CellularMigrationExecution execution;
};
struct RecordedPopulationMigration {
CellularMigrationTrace trace;
CellularPopulationMigrationExecution execution;
};
// Portable recorded pair, not a receipt or ownership grant. The I/O adapter must
// establish the trusted request against BOTH immutable predecessor manifests and
// digest-checked payloads, then fence/commit both results before activation.
[[nodiscard]] std::expected<RecordedCellularMigration, CellularMigrationError>
prepare_migration_trace(const CellularLives&, const CellularLives&,
const CellularMigrationRequest&);
[[nodiscard]] std::expected<RecordedPopulationMigration, CellularMigrationError>
prepare_migration_trace(const CellularPopulation&, const CellularPopulation&,
const CellularMigrationRequest&);
[[nodiscard]] std::expected<CellularMigrationExecution, CellularMigrationError>
replay_migration_trace(const CellularLives&, const CellularLives&,
const CellularMigrationRequest& trusted, const CellularMigrationTrace&);
[[nodiscard]] std::expected<CellularPopulationMigrationExecution, CellularMigrationError>
replay_migration_trace(const CellularPopulation&, const CellularPopulation&,
const CellularMigrationRequest& trusted, const CellularMigrationTrace&);
[[nodiscard]] std::expected<std::vector<std::uint8_t>, CellularMigrationError>
encode_migration_trace(const CellularMigrationTrace&);
[[nodiscard]] std::expected<CellularMigrationTrace, CellularMigrationError>
decode_migration_trace(std::span<const std::uint8_t>);
} // namespace evo::server
+59
View File
@@ -0,0 +1,59 @@
# Exact paired biological transfer trace 1
Authority: ADR-0144. Portable replay evidence only; no durable manifest admission,
ownership receipt or activation authority. This is separate from the M10 probe,
normal cellular tick traces and population batch 1. It is not a migration route.
All integers are little-endian without padding. Maximum size is 2,312 bytes.
| Offset | Bytes | Field |
|---:|---:|---|
| 0 | 8 | ASCII magic `AEVMIG01` |
| 8 | 4 | Version, exactly 1 |
| 12 | 4 | Representation: 1 individual owners, 2 population owners; reject all other u32 values before narrowing |
| 16 | 8 | World |
| 24 | 8 | Common frame |
| 32 | 8 | Common completed biological tick, positive; no tick advances |
| 40 | 8 | Whole wildlife genome group |
| 48 | 16 | Stable workflow request UUID, nonzero |
| 64 | 72 | Source endpoint |
| 136 | 72 | Destination endpoint |
| 208 | 40 | Exact transferred inventory: energy, C, N, P, other; five u64s |
| 248 | 4 | Member count, 1..256 |
| 252 | 4 | Reserved zero |
| 256 | 8 each | Sorted unique wildlife life IDs |
| final | 8 | FNV-1a64 over all preceding bytes |
Each endpoint is region u64, predecessor UUID (16), predecessor manifest SHA-256
(32), resulting commit UUID (16). Regions are distinct positive bud-capable IDs.
Both predecessor digests are nonzero; the four commit/predecessor UUIDs are
nonzero and pairwise distinct. Member IDs have valid positive birth-origin region
and sequence within the bud namespace. Transferred inventory is nonzero.
Require exact total length `256 + 8*count + 8`, all header/member bounds before
allocation and complete canonical re-encoding. The two representation variants
are distinct even when materialized biology is equal. Individual inputs must be
owner 7/rules 8; population inputs must contain those owners in envelope 2.
Preparation derives members and material from the actual paired migration
candidate. Replay takes an independently trusted request plus both complete
predecessor states. It checks request/endpoint/result-commit bindings, world,
frame, tick and representation, recomputes exact transfer candidates and requires
the recorded member set and every inventory channel to match. Replaying again
from the same immutable inputs gives the same results; applying it to the output
pair is refused. No controller, random sampler, biological tick, movement,
conversion or identity allocator runs as part of this trace.
The I/O adapter must establish that trusted request against both retained
predecessor manifests and digest-checked payloads before invoking replay. Taking
the trace's own request as a live ownership grant is invalid. FNV only detects
accidental corruption; a future durable publisher must bind this trace's SHA-256,
both result payloads and both fenced heads in one transaction, retaining the
source/destination histories and transfer journal. These UUIDs and SHA fields
alone do not establish current authority or transaction outcome.
The separate-process test records raw and cohort inputs/expected output pairs,
terminates, restores in a fresh process, replays and compares all four complete
output snapshots. This qualifies portable continuation, not database atomicity,
source freeze, ghost staging, abort, duplicate-delivery settlement or crash-safe
activation. No schema 45 publisher admits this trace or the new owner tuple.
@@ -0,0 +1,212 @@
#include "evo/server/lifecycle/cellular_migration_trace.hpp"
#include <algorithm>
#include <utility>
namespace evo::server {
namespace {
constexpr std::array<std::uint8_t, 8> magic{'A', 'E', 'V', 'M', 'I', 'G', '0', '1'};
template <typename T> bool zero(const T& bytes) {
return std::ranges::all_of(bytes, [](auto byte) { return byte == 0; });
}
bool valid_request(const CellularMigrationRequest& request) {
const auto& s = request.source;
const auto& d = request.destination;
if (zero(request.request) || request.genome == 0 || s.region == 0 || d.region == 0 ||
s.region > cellular_bud_max_region || d.region > cellular_bud_max_region ||
s.region == d.region || zero(s.predecessor_digest) || zero(d.predecessor_digest))
return false;
auto ids = std::array{s.predecessor, d.predecessor, s.commit, d.commit};
if (std::ranges::any_of(ids, [](const auto& id) { return zero(id); }))
return false;
std::ranges::sort(ids);
return std::ranges::adjacent_find(ids) == ids.end();
}
bool shape(const CellularMigrationTrace& trace) {
if ((trace.representation != CellularMigrationRepresentation::individual &&
trace.representation != CellularMigrationRepresentation::population) ||
trace.world == 0 || trace.frame == 0 || trace.tick == 0 || !valid_request(trace.request) ||
trace.transferred == accounting::Inventory{} || trace.members.empty() ||
trace.members.size() > biology::max_cellular_tick_lives ||
!std::ranges::is_sorted(trace.members) ||
std::ranges::adjacent_find(trace.members) != trace.members.end())
return false;
return std::ranges::all_of(trace.members, [](auto id) {
return id >= cellular_bud_namespace && ((id >> 32U) & cellular_bud_max_region) != 0 &&
(id & 0xFFFF'FFFFULL) != 0;
});
}
const CellularLives* state(const CellularLives& owner) {
return &owner;
}
const CellularLives* state(const CellularPopulation& owner) {
return &owner.state();
}
template <typename Recorded, typename Owner>
std::expected<Recorded, CellularMigrationError>
record(const Owner& source, const Owner& destination, const CellularMigrationRequest& request,
CellularMigrationRepresentation representation) {
if (!valid_request(request) || request.source.region != state(source)->region() ||
request.destination.region != state(destination)->region())
return std::unexpected(CellularMigrationError::identity);
auto execution = CellularMigration::prepare(source, destination, request.genome);
if (!execution)
return std::unexpected(execution.error());
CellularMigrationTrace trace{
representation, state(source)->world(), state(source)->frame(), state(source)->tick(),
request, execution->transferred, execution->members};
if (!shape(trace))
return std::unexpected(CellularMigrationError::format);
return Recorded{std::move(trace), std::move(*execution)};
}
template <typename Owner>
auto replay(const Owner& source, const Owner& destination, const CellularMigrationRequest& trusted,
const CellularMigrationTrace& trace, CellularMigrationRepresentation representation)
-> decltype(CellularMigration::prepare(source, destination, trusted.genome)) {
if (!shape(trace))
return std::unexpected(CellularMigrationError::format);
if (trace.request != trusted || trace.representation != representation ||
trace.world != state(source)->world() || trace.frame != state(source)->frame() ||
trace.tick != state(source)->tick() || trusted.source.region != state(source)->region() ||
trusted.destination.region != state(destination)->region())
return std::unexpected(CellularMigrationError::identity);
auto execution = CellularMigration::prepare(source, destination, trusted.genome);
if (!execution)
return std::unexpected(execution.error());
if (execution->members != trace.members)
return std::unexpected(CellularMigrationError::identity);
if (execution->transferred != trace.transferred)
return std::unexpected(CellularMigrationError::conservation);
return execution;
}
std::uint64_t checksum(std::span<const std::uint8_t> bytes) {
std::uint64_t value = 14695981039346656037ULL;
for (const auto byte : bytes)
value = (value ^ byte) * 1099511628211ULL;
return value;
}
void put(std::vector<std::uint8_t>& out, std::uint64_t value, unsigned width = 8) {
for (unsigned i = 0; i < width; ++i)
out.push_back(static_cast<std::uint8_t>(value >> (8U * i)));
}
template <typename T> void append(std::vector<std::uint8_t>& out, const T& bytes) {
out.insert(out.end(), bytes.begin(), bytes.end());
}
class Reader {
public:
explicit Reader(std::span<const std::uint8_t> bytes) : bytes_(bytes) {}
std::uint64_t get(unsigned width = 8) {
std::uint64_t value = 0;
for (unsigned i = 0; i < width; ++i)
value |= std::uint64_t{bytes_[at_++]} << (8U * i);
return value;
}
template <std::size_t N> std::array<std::uint8_t, N> array() {
std::array<std::uint8_t, N> result{};
for (auto& byte : result)
byte = static_cast<std::uint8_t>(get(1));
return result;
}
private:
std::span<const std::uint8_t> bytes_;
std::size_t at_{};
};
} // namespace
std::expected<RecordedCellularMigration, CellularMigrationError>
prepare_migration_trace(const CellularLives& source, const CellularLives& destination,
const CellularMigrationRequest& request) {
return record<RecordedCellularMigration>(source, destination, request,
CellularMigrationRepresentation::individual);
}
std::expected<RecordedPopulationMigration, CellularMigrationError>
prepare_migration_trace(const CellularPopulation& source, const CellularPopulation& destination,
const CellularMigrationRequest& request) {
return record<RecordedPopulationMigration>(source, destination, request,
CellularMigrationRepresentation::population);
}
std::expected<CellularMigrationExecution, CellularMigrationError>
replay_migration_trace(const CellularLives& source, const CellularLives& destination,
const CellularMigrationRequest& trusted,
const CellularMigrationTrace& trace) {
return replay(source, destination, trusted, trace, CellularMigrationRepresentation::individual);
}
std::expected<CellularPopulationMigrationExecution, CellularMigrationError>
replay_migration_trace(const CellularPopulation& source, const CellularPopulation& destination,
const CellularMigrationRequest& trusted,
const CellularMigrationTrace& trace) {
return replay(source, destination, trusted, trace, CellularMigrationRepresentation::population);
}
std::expected<std::vector<std::uint8_t>, CellularMigrationError>
encode_migration_trace(const CellularMigrationTrace& trace) {
if (!shape(trace))
return std::unexpected(CellularMigrationError::format);
std::vector<std::uint8_t> bytes;
bytes.reserve(migration_trace_header_bytes + 8 * trace.members.size() + 8);
append(bytes, magic);
put(bytes, 1, 4);
put(bytes, static_cast<std::uint32_t>(trace.representation), 4);
for (const auto value : {trace.world, trace.frame, trace.tick, trace.request.genome})
put(bytes, value);
append(bytes, trace.request.request);
for (const auto* endpoint : {&trace.request.source, &trace.request.destination}) {
put(bytes, endpoint->region);
append(bytes, endpoint->predecessor);
append(bytes, endpoint->predecessor_digest);
append(bytes, endpoint->commit);
}
put(bytes, trace.transferred.energy.value);
for (const auto mass : trace.transferred.matter)
put(bytes, mass.value);
put(bytes, trace.members.size(), 4);
put(bytes, 0, 4);
for (const auto id : trace.members)
put(bytes, id);
put(bytes, checksum(bytes));
return bytes;
}
std::expected<CellularMigrationTrace, CellularMigrationError>
decode_migration_trace(std::span<const std::uint8_t> bytes) {
if (bytes.size() < migration_trace_header_bytes + 8 || bytes.size() > max_migration_trace_bytes)
return std::unexpected(CellularMigrationError::format);
Reader end(bytes.last(8));
if (end.get() != checksum(bytes.first(bytes.size() - 8)))
return std::unexpected(CellularMigrationError::checksum);
Reader in(bytes);
if (in.array<8>() != magic)
return std::unexpected(CellularMigrationError::format);
if (in.get(4) != 1)
return std::unexpected(CellularMigrationError::version);
const auto representation = in.get(4);
if (representation != 1 && representation != 2)
return std::unexpected(CellularMigrationError::version);
CellularMigrationTrace trace;
trace.representation = static_cast<CellularMigrationRepresentation>(representation);
trace.world = in.get();
trace.frame = in.get();
trace.tick = in.get();
trace.request.genome = in.get();
trace.request.request = in.array<16>();
for (auto* endpoint : {&trace.request.source, &trace.request.destination}) {
endpoint->region = in.get();
endpoint->predecessor = in.array<16>();
endpoint->predecessor_digest = in.array<32>();
endpoint->commit = in.array<16>();
}
trace.transferred.energy.value = in.get();
for (auto& mass : trace.transferred.matter)
mass.value = in.get();
const auto count = in.get(4);
if (in.get(4) != 0 || count == 0 || count > biology::max_cellular_tick_lives ||
bytes.size() != migration_trace_header_bytes + 8 * count + 8)
return std::unexpected(CellularMigrationError::format);
trace.members.reserve(static_cast<std::size_t>(count));
for (std::uint64_t i = 0; i < count; ++i)
trace.members.push_back(in.get());
const auto encoded = encode_migration_trace(trace);
if (!encoded || !std::ranges::equal(*encoded, bytes))
return std::unexpected(CellularMigrationError::format);
return trace;
}
} // namespace evo::server
+20
View File
@@ -0,0 +1,20 @@
file(MAKE_DIRECTORY "${TEST_DIR}")
set(prefix "${TEST_DIR}/pair")
execute_process(COMMAND "${MIGRATION_TEST}" --record "${prefix}" RESULT_VARIABLE recorded)
if(NOT recorded EQUAL 0)
message(FATAL_ERROR "Migration trace preparation failed: ${recorded}")
endif()
execute_process(COMMAND "${MIGRATION_TEST}" --replay "${prefix}" RESULT_VARIABLE replayed)
if(NOT replayed EQUAL 0)
message(FATAL_ERROR "Fresh-process migration replay failed: ${replayed}")
endif()
foreach(representation raw pop)
foreach(side source destination)
execute_process(COMMAND "${CMAKE_COMMAND}" -E compare_files
"${prefix}.${representation}.${side}.expected"
"${prefix}.${representation}.${side}.resumed" RESULT_VARIABLE compared)
if(NOT compared EQUAL 0)
message(FATAL_ERROR "Migration ${representation}/${side} diverged across process replay")
endif()
endforeach()
endforeach()
+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';