From 1e207b5ee41d961312e2db782e16db571b7550a4 Mon Sep 17 00:00:00 2001 From: dingo Date: Sat, 17 Oct 2026 18:23:33 -0400 Subject: [PATCH] M12.4: record and replay exact biological transfer pairs --- CMakeLists.txt | 6 + docs/STATUS.md | 11 + ...0144-exact-wildlife-transfer-accounting.md | 10 + docs/architecture/REGION_DURABILITY.md | 4 + .../M12_EXACT_BIOLOGICAL_TRANSFER.md | 16 ++ docs/engineering/M12_REMAINING_GATES.md | 3 + docs/engineering/VALIDATION.md | 39 ++++ .../server/lifecycle/cellular_migration.hpp | 4 +- .../lifecycle/cellular_migration_trace.hpp | 59 +++++ schemas/cellular-migration-trace-v1.md | 59 +++++ .../lifecycle/cellular_migration_trace.cpp | 212 ++++++++++++++++++ tests/cellular_migration_restart.cmake | 20 ++ tests/cellular_migration_tests.cpp | 196 +++++++++++++++- 13 files changed, 637 insertions(+), 2 deletions(-) create mode 100644 include/evo/server/lifecycle/cellular_migration_trace.hpp create mode 100644 schemas/cellular-migration-trace-v1.md create mode 100644 src/server/lifecycle/cellular_migration_trace.cpp create mode 100644 tests/cellular_migration_restart.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 2015819..bcf7072 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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=$" + "-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) diff --git a/docs/STATUS.md b/docs/STATUS.md index fc7ec69..92abbfc 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -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 diff --git a/docs/adr/0144-exact-wildlife-transfer-accounting.md b/docs/adr/0144-exact-wildlife-transfer-accounting.md index 5636383..9b6f6a0 100644 --- a/docs/adr/0144-exact-wildlife-transfer-accounting.md +++ b/docs/adr/0144-exact-wildlife-transfer-accounting.md @@ -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 diff --git a/docs/architecture/REGION_DURABILITY.md b/docs/architecture/REGION_DURABILITY.md index a8227ae..779f00a 100644 --- a/docs/architecture/REGION_DURABILITY.md +++ b/docs/architecture/REGION_DURABILITY.md @@ -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 diff --git a/docs/engineering/M12_EXACT_BIOLOGICAL_TRANSFER.md b/docs/engineering/M12_EXACT_BIOLOGICAL_TRANSFER.md index 3e26dd7..c1b2e1b 100644 --- a/docs/engineering/M12_EXACT_BIOLOGICAL_TRANSFER.md +++ b/docs/engineering/M12_EXACT_BIOLOGICAL_TRANSFER.md @@ -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. diff --git a/docs/engineering/M12_REMAINING_GATES.md b/docs/engineering/M12_REMAINING_GATES.md index ba00708..1944046 100644 --- a/docs/engineering/M12_REMAINING_GATES.md +++ b/docs/engineering/M12_REMAINING_GATES.md @@ -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 diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 50e41db..ec7eec4 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -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 diff --git a/include/evo/server/lifecycle/cellular_migration.hpp b/include/evo/server/lifecycle/cellular_migration.hpp index 90aa4c0..f0631a4 100644 --- a/include/evo/server/lifecycle/cellular_migration.hpp +++ b/include/evo/server/lifecycle/cellular_migration.hpp @@ -12,7 +12,9 @@ enum class CellularMigrationError : std::uint8_t { placement, archive, overflow, - conservation + conservation, + format, + checksum }; struct CellularMigrationExecution { std::uint64_t genome{}; diff --git a/include/evo/server/lifecycle/cellular_migration_trace.hpp b/include/evo/server/lifecycle/cellular_migration_trace.hpp new file mode 100644 index 0000000..433dad6 --- /dev/null +++ b/include/evo/server/lifecycle/cellular_migration_trace.hpp @@ -0,0 +1,59 @@ +#pragma once + +#include "evo/server/lifecycle/cellular_migration.hpp" + +#include + +namespace evo::server { +struct CellularMigrationEndpoint { + std::uint64_t region{}; + std::array predecessor{}, commit{}; + std::array predecessor_digest{}; + bool operator==(const CellularMigrationEndpoint&) const = default; +}; +struct CellularMigrationRequest { + std::array 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 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 +prepare_migration_trace(const CellularLives&, const CellularLives&, + const CellularMigrationRequest&); +[[nodiscard]] std::expected +prepare_migration_trace(const CellularPopulation&, const CellularPopulation&, + const CellularMigrationRequest&); +[[nodiscard]] std::expected +replay_migration_trace(const CellularLives&, const CellularLives&, + const CellularMigrationRequest& trusted, const CellularMigrationTrace&); +[[nodiscard]] std::expected +replay_migration_trace(const CellularPopulation&, const CellularPopulation&, + const CellularMigrationRequest& trusted, const CellularMigrationTrace&); +[[nodiscard]] std::expected, CellularMigrationError> +encode_migration_trace(const CellularMigrationTrace&); +[[nodiscard]] std::expected + decode_migration_trace(std::span); +} // namespace evo::server diff --git a/schemas/cellular-migration-trace-v1.md b/schemas/cellular-migration-trace-v1.md new file mode 100644 index 0000000..2b1f2fd --- /dev/null +++ b/schemas/cellular-migration-trace-v1.md @@ -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. diff --git a/src/server/lifecycle/cellular_migration_trace.cpp b/src/server/lifecycle/cellular_migration_trace.cpp new file mode 100644 index 0000000..b16837f --- /dev/null +++ b/src/server/lifecycle/cellular_migration_trace.cpp @@ -0,0 +1,212 @@ +#include "evo/server/lifecycle/cellular_migration_trace.hpp" + +#include +#include + +namespace evo::server { +namespace { +constexpr std::array magic{'A', 'E', 'V', 'M', 'I', 'G', '0', '1'}; +template 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 +std::expected +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 +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 bytes) { + std::uint64_t value = 14695981039346656037ULL; + for (const auto byte : bytes) + value = (value ^ byte) * 1099511628211ULL; + return value; +} +void put(std::vector& out, std::uint64_t value, unsigned width = 8) { + for (unsigned i = 0; i < width; ++i) + out.push_back(static_cast(value >> (8U * i))); +} +template void append(std::vector& out, const T& bytes) { + out.insert(out.end(), bytes.begin(), bytes.end()); +} +class Reader { + public: + explicit Reader(std::span 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::array array() { + std::array result{}; + for (auto& byte : result) + byte = static_cast(get(1)); + return result; + } + + private: + std::span bytes_; + std::size_t at_{}; +}; +} // namespace + +std::expected +prepare_migration_trace(const CellularLives& source, const CellularLives& destination, + const CellularMigrationRequest& request) { + return record(source, destination, request, + CellularMigrationRepresentation::individual); +} +std::expected +prepare_migration_trace(const CellularPopulation& source, const CellularPopulation& destination, + const CellularMigrationRequest& request) { + return record(source, destination, request, + CellularMigrationRepresentation::population); +} +std::expected +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 +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, CellularMigrationError> +encode_migration_trace(const CellularMigrationTrace& trace) { + if (!shape(trace)) + return std::unexpected(CellularMigrationError::format); + std::vector bytes; + bytes.reserve(migration_trace_header_bytes + 8 * trace.members.size() + 8); + append(bytes, magic); + put(bytes, 1, 4); + put(bytes, static_cast(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 +decode_migration_trace(std::span 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(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(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 diff --git a/tests/cellular_migration_restart.cmake b/tests/cellular_migration_restart.cmake new file mode 100644 index 0000000..b74d91e --- /dev/null +++ b/tests/cellular_migration_restart.cmake @@ -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() diff --git a/tests/cellular_migration_tests.cpp b/tests/cellular_migration_tests.cpp index e45cf11..bc95835 100644 --- a/tests/cellular_migration_tests.cpp +++ b/tests/cellular_migration_tests.cpp @@ -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 #include namespace { @@ -140,6 +142,186 @@ Owner decode(const CellularLives& trusted, const std::vector& byte trusted.tick()), "migration owner restore"); } +template std::array filled(std::uint8_t value) { + std::array 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 bytes) { + std::ofstream out(path, std::ios::binary | std::ios::trunc); + out.write(reinterpret_cast(bytes.data()), + static_cast(bytes.size())); + out.close(); + require(out.good(), "restart fixture write"); +} +std::vector 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(max_cellular_population_bytes), + "restart fixture bound"); + std::vector bytes(static_cast(length)); + in.seekg(0); + in.read(reinterpret_cast(bytes.data()), static_cast(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';