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
@@ -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