ADR-0062 adds a canonical LineageArchive codec (parents before children, organisms by id, FNV) that decodes by replaying admit/record_birth/ record_death and requires byte-identical re-encoding; it never overwrites an existing archive. LineageRegion binds the environmental producer and consumer accounts to archived organisms with identity high-waters, enforces closure (lifecycle vs deaths, exact living set, tick bounds) and records deaths in the tick that causes them. Checkpoint profile 2 (environmental-lineage-v1, history profile 1) publishes that container; schema v4 admits it with a 4,268,144-byte cap and requires each commit to keep its predecessor's profile, RNG, history and durability declarations. Linux adds provision/publish/recover for the profile on the shared publication path. Qualification found WSL2 stepping CLOCK_REALTIME back ~0.5 s every ~32 s, which made two 1 s-lease tests flaky under ASan. Tests now wait on database-time expiry; ADR-0060 records the slewed-clock requirement. Validated: Windows CTest 57/57; Linux GCC Debug/Release, clang-tidy and ASan/UBSan CTest 57/57 each; PostgreSQL suites 2x Release, 2x analysis, 5x ASan; SQL and C++ mutations each caught. No Docker/Gitea CI, remote store, process kill or deployment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
180 lines
7.7 KiB
C++
180 lines
7.7 KiB
C++
#include "evo/processes/lineage_region.hpp"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <vector>
|
|
|
|
namespace evo::processes {
|
|
namespace {
|
|
constexpr std::uint64_t region_magic = 0x31524C454F564541ULL; // "AEVOELR1"
|
|
constexpr std::size_t region_header_bytes = 56;
|
|
std::uint64_t fnv(std::span<const std::uint8_t> bytes) noexcept {
|
|
std::uint64_t value = 14695981039346656037ULL;
|
|
for (const auto byte : bytes)
|
|
value = (value ^ byte) * 1099511628211ULL;
|
|
return value;
|
|
}
|
|
// Fills an exactly sized buffer; the caller checks the final cursor (M6.5c pattern,
|
|
// which also avoids GCC 14 reserve/append false positives).
|
|
class Cursor {
|
|
public:
|
|
explicit Cursor(world::WorldBytes& out) : out_(out) {}
|
|
void put(std::uint64_t value, unsigned width) {
|
|
for (unsigned i = 0; i < width; ++i, value >>= 8U)
|
|
out_[at_++] = static_cast<std::uint8_t>(value & 0xFFU);
|
|
}
|
|
void bytes(std::span<const std::uint8_t> value) {
|
|
std::ranges::copy(value, out_.begin() + static_cast<std::ptrdiff_t>(at_));
|
|
at_ += value.size();
|
|
}
|
|
[[nodiscard]] std::size_t at() const noexcept {
|
|
return at_;
|
|
}
|
|
|
|
private:
|
|
world::WorldBytes& out_;
|
|
std::size_t at_{};
|
|
};
|
|
std::uint64_t get(std::span<const std::uint8_t> in, std::size_t at, unsigned width) {
|
|
std::uint64_t value{};
|
|
for (unsigned i = 0; i < width; ++i)
|
|
value |= static_cast<std::uint64_t>(in[at + i]) << (8U * i);
|
|
return value;
|
|
}
|
|
} // namespace
|
|
|
|
LineageRegion::LineageRegion(std::unique_ptr<EnvironmentalCommunity> community,
|
|
const biology::LineageArchive& archive, LineageBinding binding)
|
|
: community_(std::move(community)), archive_(archive), binding_(binding) {}
|
|
|
|
bool LineageRegion::closed() const {
|
|
const auto& b = binding_;
|
|
if (!community_ || b.producer == 0 || b.consumer == 0 || b.producer == b.consumer)
|
|
return false;
|
|
const auto producer = archive_.organism(b.producer);
|
|
const auto consumer = archive_.organism(b.consumer);
|
|
if (!producer || !consumer)
|
|
return false;
|
|
const auto progress = community_->progress();
|
|
const Tick now{progress.tick};
|
|
// Terminal lifecycle and archive death agree, and neither is in the future.
|
|
for (const auto& [record, terminal] : {std::pair{*producer, progress.producer_terminal},
|
|
std::pair{*consumer, progress.consumer_terminal}}) {
|
|
if (terminal > 1 || record.alive != (terminal == 0) || record.born > now ||
|
|
(!record.alive && record.died > now))
|
|
return false;
|
|
}
|
|
// No living organism exists outside this region's accounts.
|
|
std::array<std::uint64_t, 2> expected{};
|
|
std::size_t count = 0;
|
|
if (producer->alive)
|
|
expected[count++] = b.producer;
|
|
if (consumer->alive)
|
|
expected[count++] = b.consumer;
|
|
if (count == 2 && expected[0] > expected[1])
|
|
std::swap(expected[0], expected[1]);
|
|
const auto living = archive_.living_organisms();
|
|
return std::ranges::equal(living, std::span{expected}.first(count)) &&
|
|
b.next_organism > archive_.highest_organism() &&
|
|
b.next_genome > archive_.highest_genome();
|
|
}
|
|
|
|
std::expected<std::unique_ptr<LineageRegion>, LineageRegionError>
|
|
LineageRegion::create(std::unique_ptr<EnvironmentalCommunity> community,
|
|
const biology::LineageArchive& archive, LineageBinding binding) {
|
|
if (!community)
|
|
return std::unexpected{LineageRegionError::community};
|
|
std::unique_ptr<LineageRegion> region(
|
|
new LineageRegion(std::move(community), archive, binding));
|
|
if (!region->closed())
|
|
return std::unexpected{LineageRegionError::closure};
|
|
return region;
|
|
}
|
|
|
|
std::expected<EnvironmentalReport, LineageRegionError>
|
|
LineageRegion::advance(std::uint64_t tick, const EnvironmentalInput& input) {
|
|
if (poisoned_)
|
|
return std::unexpected{LineageRegionError::poisoned};
|
|
auto report = community_->advance(tick, input);
|
|
if (!report)
|
|
return std::unexpected{LineageRegionError::community};
|
|
// Closure guarantees both organisms are alive until their transition and born
|
|
// no later than the previous tick, so these deaths cannot be refused.
|
|
const std::pair<std::uint64_t, std::uint64_t> deaths[] = {
|
|
{report->terminal_transition, binding_.producer},
|
|
{report->consumer_transition, binding_.consumer}};
|
|
for (const auto& [transition, organism] : deaths)
|
|
if (transition == 1 && !archive_.record_death(organism, Tick{tick})) {
|
|
poisoned_ = true;
|
|
return std::unexpected{LineageRegionError::poisoned};
|
|
}
|
|
return *report;
|
|
}
|
|
|
|
std::expected<world::WorldBytes, LineageRegionError> LineageRegion::snapshot() const {
|
|
if (poisoned_)
|
|
return std::unexpected{LineageRegionError::poisoned};
|
|
auto envelope = community_->snapshot();
|
|
if (!envelope)
|
|
return std::unexpected{LineageRegionError::community};
|
|
const auto archive = archive_.encode();
|
|
world::WorldBytes out(region_header_bytes + envelope->size() + 8 + archive.size() + 8);
|
|
Cursor w{out};
|
|
w.put(region_magic, 8);
|
|
w.put(lineage_region_format, 4);
|
|
w.put(0, 4);
|
|
w.put(binding_.producer, 8);
|
|
w.put(binding_.consumer, 8);
|
|
w.put(binding_.next_organism, 8);
|
|
w.put(binding_.next_genome, 8);
|
|
w.put(envelope->size(), 8);
|
|
w.bytes(*envelope);
|
|
w.put(archive.size(), 8);
|
|
w.bytes(archive);
|
|
w.put(fnv(std::span{out}.first(w.at())), 8);
|
|
if (w.at() != out.size())
|
|
return std::unexpected{LineageRegionError::format};
|
|
return out;
|
|
}
|
|
|
|
std::expected<std::unique_ptr<LineageRegion>, LineageRegionError>
|
|
LineageRegion::restore(std::span<const std::uint8_t> bytes, world::WorldIdentity identity,
|
|
std::size_t budget) {
|
|
if (bytes.size() < region_header_bytes + 24 || bytes.size() > max_lineage_region_bytes ||
|
|
get(bytes, 0, 8) != region_magic)
|
|
return std::unexpected{LineageRegionError::format};
|
|
if (get(bytes, 8, 4) != lineage_region_format)
|
|
return std::unexpected{LineageRegionError::version};
|
|
if (get(bytes, 12, 4) != 0)
|
|
return std::unexpected{LineageRegionError::format};
|
|
const auto body = bytes.first(bytes.size() - 8);
|
|
if (get(bytes, bytes.size() - 8, 8) != fnv(body))
|
|
return std::unexpected{LineageRegionError::checksum};
|
|
const LineageBinding binding{get(bytes, 16, 8), get(bytes, 24, 8), get(bytes, 32, 8),
|
|
get(bytes, 40, 8)};
|
|
// Lengths are checked against the remaining bytes before any sub-span is taken.
|
|
const auto envelope_bytes = get(bytes, 48, 8);
|
|
if (envelope_bytes == 0 || envelope_bytes > EnvironmentalCommunity::max_snapshot_bytes ||
|
|
envelope_bytes > body.size() - region_header_bytes - 8)
|
|
return std::unexpected{LineageRegionError::format};
|
|
const auto envelope =
|
|
body.subspan(region_header_bytes, static_cast<std::size_t>(envelope_bytes));
|
|
const auto after = region_header_bytes + envelope.size();
|
|
const auto archive_bytes = get(body, after, 8);
|
|
if (archive_bytes != body.size() - after - 8)
|
|
return std::unexpected{LineageRegionError::format};
|
|
auto community = EnvironmentalCommunity::restore(envelope, identity, budget);
|
|
if (!community)
|
|
return std::unexpected{LineageRegionError::community};
|
|
auto archive = biology::LineageArchive::decode(body.subspan(after + 8));
|
|
if (!archive)
|
|
return std::unexpected{LineageRegionError::archive};
|
|
auto region = create(std::move(*community), *archive, binding);
|
|
if (!region)
|
|
return std::unexpected{region.error()};
|
|
const auto again = (*region)->snapshot();
|
|
if (!again || !std::ranges::equal(*again, bytes))
|
|
return std::unexpected{LineageRegionError::format};
|
|
return std::move(*region);
|
|
}
|
|
} // namespace evo::processes
|