Complete M7.4b lineage checkpoint profile; M7.4 done

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>
This commit is contained in:
dingo
2026-09-28 16:43:10 -04:00
co-authored by Claude Opus 5.5
parent 5d4f59f901
commit a2c4543df2
33 changed files with 1622 additions and 69 deletions
+147
View File
@@ -161,6 +161,153 @@ LineageArchive::living_carriers(std::uint64_t genome) const {
return carrying;
}
std::vector<std::uint64_t> LineageArchive::living_organisms() const {
std::vector<std::uint64_t> living;
for (const auto& entry : organisms_)
if (entry.record.alive)
living.push_back(entry.id);
return living;
}
std::uint64_t LineageArchive::highest_genome() const noexcept {
return genomes_.empty() ? 0 : genomes_.back().id();
}
std::uint64_t LineageArchive::highest_organism() const noexcept {
return organisms_.empty() ? 0 : organisms_.back().id;
}
namespace {
constexpr std::uint64_t archive_magic = 0x3152414C4F564541ULL; // "AEVOLAR1"
constexpr std::size_t archive_header_bytes = 32;
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;
}
void put(std::vector<std::uint8_t>& out, std::uint64_t value, unsigned width) {
for (unsigned i = 0; i < width; ++i) {
out.push_back(static_cast<std::uint8_t>(value & 0xFFU));
value >>= 8U;
}
}
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
std::vector<std::uint8_t> LineageArchive::encode() const {
// Depth from the founder, memoized; the parent graph is acyclic by admission.
std::vector<std::uint32_t> depth(genomes_.size(), 0);
std::vector<bool> known(genomes_.size(), false);
const auto index = [&](std::uint64_t id) {
return static_cast<std::size_t>(locate(genomes_, id, by_genome) - genomes_.begin());
};
for (std::size_t i = 0; i < genomes_.size(); ++i) {
std::vector<std::size_t> chain;
auto at = i;
while (!known[at]) {
chain.push_back(at);
if (genomes_[at].parents().empty())
break;
at = index(genomes_[at].parents()[0]);
}
std::uint32_t d = known[at] ? depth[at] + 1 : 0;
for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
if (known[*it])
continue;
depth[*it] = genomes_[*it].parents().empty() ? 0 : d;
known[*it] = true;
d = depth[*it] + 1;
}
}
std::vector<std::size_t> order(genomes_.size());
for (std::size_t i = 0; i < order.size(); ++i)
order[i] = i;
// Genomes are id-sorted already, so a stable sort by depth yields (depth, id).
std::ranges::stable_sort(order, {}, [&](std::size_t i) { return depth[i]; });
std::vector<std::uint8_t> out;
out.reserve(archive_header_bytes + (genomes_.size() * (2 + max_genome_bytes)) +
(organisms_.size() * archived_organism_bytes) + 8);
put(out, archive_magic, 8);
put(out, lineage_archive_format, 4);
put(out, genome_schema_version, 4);
put(out, genomes_.size(), 4);
put(out, organisms_.size(), 4);
put(out, 0, 8); // Total length, patched below.
for (const auto i : order) {
const auto record = genomes_[i].encode();
put(out, record.size(), 2);
out.insert(out.end(), record.begin(), record.end());
}
for (const auto& entry : organisms_) {
put(out, entry.id, 8);
put(out, entry.record.genome, 8);
put(out, entry.record.born.value, 8);
put(out, entry.record.alive ? 0 : entry.record.died.value, 8);
put(out, entry.record.alive ? 1 : 0, 1);
}
const auto total = out.size() + 8;
for (unsigned b = 0; b < 8; ++b)
out[24 + b] = static_cast<std::uint8_t>((total >> (8U * b)) & 0xFFU);
put(out, fnv(out), 8);
return out;
}
std::expected<LineageArchive, ArchiveCodecError>
LineageArchive::decode(std::span<const std::uint8_t> bytes) {
if (bytes.size() < archive_header_bytes + 8 || get(bytes, 0, 8) != archive_magic)
return std::unexpected{ArchiveCodecError::format};
if (get(bytes, 8, 4) != lineage_archive_format || get(bytes, 12, 4) != genome_schema_version)
return std::unexpected{ArchiveCodecError::version};
const auto genomes = get(bytes, 16, 4);
const auto organisms = get(bytes, 20, 4);
if (bytes.size() > max_archive_bytes || genomes > max_archived_genomes ||
organisms > max_archived_organisms)
return std::unexpected{ArchiveCodecError::bounds};
if (get(bytes, 24, 8) != bytes.size())
return std::unexpected{ArchiveCodecError::format};
const auto body = bytes.first(bytes.size() - 8);
if (get(bytes, bytes.size() - 8, 8) != fnv(body))
return std::unexpected{ArchiveCodecError::checksum};
LineageArchive archive;
std::size_t at = archive_header_bytes;
for (std::uint64_t g = 0; g < genomes; ++g) {
if (at + 2 > body.size())
return std::unexpected{ArchiveCodecError::format};
const auto length = static_cast<std::size_t>(get(body, at, 2));
at += 2;
if (length == 0 || length > max_genome_bytes || at + length > body.size())
return std::unexpected{ArchiveCodecError::format};
const auto genome = Genome::decode(body.subspan(at, length));
if (!genome)
return std::unexpected{ArchiveCodecError::genome};
if (!archive.admit(*genome))
return std::unexpected{ArchiveCodecError::history};
at += length;
}
if (body.size() - at != organisms * archived_organism_bytes)
return std::unexpected{ArchiveCodecError::format};
for (std::uint64_t o = 0; o < organisms; ++o, at += archived_organism_bytes) {
const auto id = get(body, at, 8);
const Tick born{get(body, at + 16, 8)};
const Tick died{get(body, at + 24, 8)};
const auto alive = get(body, at + 32, 1);
if (alive > 1 || (alive == 1 && died.value != 0))
return std::unexpected{ArchiveCodecError::format};
if (!archive.record_birth(id, get(body, at + 8, 8), born) ||
(alive == 0 && !archive.record_death(id, died)))
return std::unexpected{ArchiveCodecError::history};
}
if (!std::ranges::equal(archive.encode(), bytes))
return std::unexpected{ArchiveCodecError::noncanonical};
return archive;
}
std::expected<LineageStatus, HistoryError> LineageArchive::status(std::uint64_t genome) const {
const auto carrying = living_carriers(genome);
if (!carrying) {
+118 -36
View File
@@ -7,16 +7,56 @@
namespace evo::persistence {
namespace {
using std::chrono::milliseconds;
// Per-profile payload operations; the publication and recovery paths are shared.
struct EnvironmentalCodec {
using State = processes::EnvironmentalCommunity;
using Recovered = RecoveredCheckpoint;
static constexpr auto profile = CheckpointProfile::environmental_v1;
static auto snapshot(const State& s) {
return s.snapshot();
}
static std::uint64_t tick(const State& s) {
return s.progress().tick;
}
static std::uint64_t quantum(const State& s) {
return s.tick_microseconds();
}
static auto restore(std::span<const std::uint8_t> payload, const CheckpointManifest& m,
std::size_t budget) {
return restore_environmental_payload(payload, m, budget);
}
};
struct LineageCodec {
using State = processes::LineageRegion;
using Recovered = RecoveredLineageCheckpoint;
static constexpr auto profile = CheckpointProfile::environmental_lineage_v1;
static auto snapshot(const State& s) {
return s.snapshot();
}
static std::uint64_t tick(const State& s) {
return s.community().progress().tick;
}
static std::uint64_t quantum(const State& s) {
return s.community().tick_microseconds();
}
static auto restore(std::span<const std::uint8_t> payload, const CheckpointManifest& m,
std::size_t budget) {
return restore_lineage_payload(payload, m, budget);
}
};
PublicationFailure fail(PublicationError kind) {
return {kind, {}, {}};
}
// Encodes the candidate, validates the exact payload bytes as recovery would,
// uploads and reads back. Returns the manifest bytes ready for the database.
// Validates the exact payload bytes as recovery would, uploads and reads back.
// Returns the manifest bytes ready for the database.
template <typename Codec>
std::expected<std::vector<std::uint8_t>, PublicationFailure>
stage(const FilesystemObjectStore& objects, const std::vector<std::uint8_t>& payload,
const CheckpointManifest& manifest, std::size_t budget) {
auto bytes = encode_manifest(manifest);
if (!bytes || !restore_environmental_payload(payload, manifest, budget))
if (!bytes || manifest.profile != Codec::profile || !Codec::restore(payload, manifest, budget))
return std::unexpected{fail(PublicationError::candidate)};
auto stored = objects.put(payload);
if (!stored)
@@ -45,23 +85,23 @@ confirm(const std::expected<RegionHead, LeaseStoreFailure>& published,
return std::unexpected{fail(PublicationError::receipt)};
return CommittedCheckpoint{*published, std::move(manifest_bytes)};
}
} // namespace
std::expected<CommittedCheckpoint, PublicationFailure> provision_environmental_region(
Postgres& owner, const FilesystemObjectStore& objects, RegionKey key, std::uint64_t frame,
const processes::EnvironmentalCommunity& opening, Uuid commit, const Sha256Digest& build,
std::size_t restore_budget, milliseconds timeout) {
auto payload = opening.snapshot();
if (!payload || opening.progress().tick != 0)
template <typename Codec>
std::expected<CommittedCheckpoint, PublicationFailure>
provision(Postgres& owner, const FilesystemObjectStore& objects, RegionKey key, std::uint64_t frame,
const typename Codec::State& opening, Uuid commit, const Sha256Digest& build,
std::size_t budget, milliseconds timeout) {
auto payload = Codec::snapshot(opening);
if (!payload || Codec::tick(opening) != 0)
return std::unexpected{fail(PublicationError::candidate)};
const auto digest = sha256(*payload);
if (!digest)
return std::unexpected{fail(PublicationError::digest)};
auto manifest = genesis_manifest(key, frame, commit, opening.tick_microseconds(), build,
payload->size(), *digest);
auto manifest = genesis_manifest(key, frame, commit, Codec::quantum(opening), build,
payload->size(), *digest, Codec::profile);
if (!manifest)
return std::unexpected{fail(PublicationError::candidate)};
auto bytes = stage(objects, *payload, *manifest, restore_budget);
auto bytes = stage<Codec>(objects, *payload, *manifest, budget);
if (!bytes)
return std::unexpected{bytes.error()};
constexpr char digits[] = "0123456789abcdef";
@@ -73,14 +113,11 @@ std::expected<CommittedCheckpoint, PublicationFailure> provision_environmental_r
const std::array<std::string, 1> p{parameter};
auto done = owner.query("SELECT evolution.provision_region_checkpoint($1::bytea)", p, timeout);
if (!done) {
const auto kind = done.error().kind == PgError::unknown ? PublicationError::unknown
: PublicationError::rejected;
return std::unexpected{
PublicationFailure{kind,
{done.error().kind == PgError::unknown ? LeaseStoreError::unknown
: LeaseStoreError::store,
done.error()},
{}}};
const bool unknown = done.error().kind == PgError::unknown;
return std::unexpected{PublicationFailure{
unknown ? PublicationError::unknown : PublicationError::rejected,
{unknown ? LeaseStoreError::unknown : LeaseStoreError::store, done.error()},
{}}};
}
auto committed = load_region_checkpoint(owner, key, timeout);
if (!committed)
@@ -89,32 +126,33 @@ std::expected<CommittedCheckpoint, PublicationFailure> provision_environmental_r
return confirm(committed->head, std::move(*bytes), *manifest);
}
std::expected<CommittedCheckpoint, PublicationFailure> publish_environmental_checkpoint(
Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
const CommittedCheckpoint& predecessor, const processes::EnvironmentalCommunity& state,
Uuid commit, const Sha256Digest& build, std::size_t restore_budget, milliseconds timeout) {
template <typename Codec>
std::expected<CommittedCheckpoint, PublicationFailure>
publish(Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
const CommittedCheckpoint& predecessor, const typename Codec::State& state, Uuid commit,
const Sha256Digest& build, std::size_t budget, milliseconds timeout) {
const auto previous = decode_manifest(predecessor.manifest);
auto payload = state.snapshot();
if (!previous || !payload)
auto payload = Codec::snapshot(state);
if (!previous || !payload || previous->profile != Codec::profile)
return std::unexpected{fail(PublicationError::candidate)};
const auto digest = sha256(*payload);
if (!digest)
return std::unexpected{fail(PublicationError::digest)};
auto manifest = successor_manifest(*previous, predecessor.head.digest, commit, lease,
state.progress().tick, build, payload->size(), *digest);
Codec::tick(state), build, payload->size(), *digest);
if (!manifest)
return std::unexpected{fail(PublicationError::candidate)};
auto bytes = stage(objects, *payload, *manifest, restore_budget);
auto bytes = stage<Codec>(objects, *payload, *manifest, budget);
if (!bytes)
return std::unexpected{bytes.error()};
auto published = publish_region_checkpoint(db, *bytes, timeout);
return confirm(published, std::move(*bytes), *manifest);
}
std::expected<RecoveredCheckpoint, RecoveryFailure>
recover_environmental_checkpoint(Postgres& db, const FilesystemObjectStore& objects, RegionKey key,
std::span<const Sha256Digest> retained_builds,
std::size_t restore_budget, milliseconds timeout) {
template <typename Codec>
std::expected<typename Codec::Recovered, RecoveryFailure>
recover(Postgres& db, const FilesystemObjectStore& objects, RegionKey key,
std::span<const Sha256Digest> retained_builds, std::size_t budget, milliseconds timeout) {
auto committed = load_region_checkpoint(db, key, timeout);
if (!committed)
return std::unexpected{
@@ -138,7 +176,7 @@ recover_environmental_checkpoint(Postgres& db, const FilesystemObjectStore& obje
manifest->sequence != head.sequence || manifest->commit != head.commit ||
manifest->end_tick != head.end_tick)
return fail_with(RecoveryError::manifest);
if (!supported(*manifest) ||
if (!supported(*manifest) || manifest->profile != Codec::profile ||
std::ranges::find(retained_builds, manifest->build) == retained_builds.end())
return fail_with(RecoveryError::unsupported);
auto payload = objects.get({manifest->payload_sha256, manifest->payload_bytes});
@@ -146,9 +184,53 @@ recover_environmental_checkpoint(Postgres& db, const FilesystemObjectStore& obje
return fail_with(payload.error() == ObjectError::missing ? RecoveryError::missing_payload
: payload.error() == ObjectError::corrupt ? RecoveryError::corrupt_payload
: RecoveryError::store);
auto state = restore_environmental_payload(*payload, *manifest, restore_budget);
auto state = Codec::restore(*payload, *manifest, budget);
if (!state)
return fail_with(RecoveryError::payload);
return RecoveredCheckpoint{std::move(*committed), *manifest, std::move(*state)};
return typename Codec::Recovered{std::move(*committed), *manifest, std::move(*state)};
}
} // namespace
std::expected<CommittedCheckpoint, PublicationFailure> provision_environmental_region(
Postgres& owner, const FilesystemObjectStore& objects, RegionKey key, std::uint64_t frame,
const processes::EnvironmentalCommunity& opening, Uuid commit, const Sha256Digest& build,
std::size_t restore_budget, milliseconds timeout) {
return provision<EnvironmentalCodec>(owner, objects, key, frame, opening, commit, build,
restore_budget, timeout);
}
std::expected<CommittedCheckpoint, PublicationFailure> publish_environmental_checkpoint(
Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
const CommittedCheckpoint& predecessor, const processes::EnvironmentalCommunity& state,
Uuid commit, const Sha256Digest& build, std::size_t restore_budget, milliseconds timeout) {
return publish<EnvironmentalCodec>(db, objects, lease, predecessor, state, commit, build,
restore_budget, timeout);
}
std::expected<RecoveredCheckpoint, RecoveryFailure>
recover_environmental_checkpoint(Postgres& db, const FilesystemObjectStore& objects, RegionKey key,
std::span<const Sha256Digest> retained_builds,
std::size_t restore_budget, milliseconds timeout) {
return recover<EnvironmentalCodec>(db, objects, key, retained_builds, restore_budget, timeout);
}
std::expected<CommittedCheckpoint, PublicationFailure>
provision_lineage_region(Postgres& owner, const FilesystemObjectStore& objects, RegionKey key,
std::uint64_t frame, const processes::LineageRegion& opening, Uuid commit,
const Sha256Digest& build, std::size_t restore_budget,
milliseconds timeout) {
return provision<LineageCodec>(owner, objects, key, frame, opening, commit, build,
restore_budget, timeout);
}
std::expected<CommittedCheckpoint, PublicationFailure> publish_lineage_checkpoint(
Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
const CommittedCheckpoint& predecessor, const processes::LineageRegion& state, Uuid commit,
const Sha256Digest& build, std::size_t restore_budget, milliseconds timeout) {
return publish<LineageCodec>(db, objects, lease, predecessor, state, commit, build,
restore_budget, timeout);
}
std::expected<RecoveredLineageCheckpoint, RecoveryFailure>
recover_lineage_checkpoint(Postgres& db, const FilesystemObjectStore& objects, RegionKey key,
std::span<const Sha256Digest> retained_builds,
std::size_t restore_budget, milliseconds timeout) {
return recover<LineageCodec>(db, objects, key, retained_builds, restore_budget, timeout);
}
} // namespace evo::persistence
+41 -6
View File
@@ -51,9 +51,12 @@ class Reader {
// Every field, independent of which versions this build supports.
bool structural(const CheckpointManifest& m) noexcept {
// An unknown profile is unsupported, not malformed: bound it by the largest cap.
const auto cap = max_payload_bytes(m.profile);
const auto limit = cap == 0 ? max_lineage_payload_bytes : cap;
if (m.key.world == 0 || m.key.region == 0 || m.frame == 0 || zero(m.commit) ||
m.tick_microseconds == 0 || zero(m.build) || m.payload_bytes == 0 ||
m.payload_bytes > max_environmental_payload_bytes || zero(m.payload_sha256))
m.payload_bytes > limit || zero(m.payload_sha256))
return false;
if (m.sequence == 0)
return zero(m.predecessor_commit) && zero(m.predecessor_digest) && m.begin_tick == 0 &&
@@ -65,9 +68,12 @@ bool structural(const CheckpointManifest& m) noexcept {
} // namespace
bool supported(const CheckpointManifest& m) noexcept {
return m.profile == CheckpointProfile::environmental_v1 &&
m.versions == environmental_v1_versions && m.rng_streams == 0 &&
m.history_profile == 0 && m.durability == DurabilityPolicy::single_volume_development;
const bool profile =
(m.profile == CheckpointProfile::environmental_v1 && m.history_profile == 0) ||
(m.profile == CheckpointProfile::environmental_lineage_v1 &&
m.history_profile == lineage_history_profile);
return profile && m.versions == environmental_v1_versions && m.rng_streams == 0 &&
m.durability == DurabilityPolicy::single_volume_development;
}
std::expected<std::vector<std::uint8_t>, CheckpointError>
@@ -150,8 +156,11 @@ decode_manifest(std::span<const std::uint8_t> bytes) {
std::expected<CheckpointManifest, CheckpointError>
genesis_manifest(RegionKey key, std::uint64_t frame, Uuid commit, std::uint64_t tick_microseconds,
const Sha256Digest& build, std::uint64_t payload_bytes,
const Sha256Digest& payload_sha256) {
const Sha256Digest& payload_sha256, CheckpointProfile profile) {
CheckpointManifest m;
m.profile = profile;
m.history_profile =
profile == CheckpointProfile::environmental_lineage_v1 ? lineage_history_profile : 0;
m.key = key;
m.frame = frame;
m.commit = commit;
@@ -196,7 +205,7 @@ restore_environmental_payload(std::span<const std::uint8_t> payload, const Check
std::size_t budget) {
if (!structural(m))
return std::unexpected{CheckpointError::format};
if (!supported(m))
if (!supported(m) || m.profile != CheckpointProfile::environmental_v1)
return std::unexpected{CheckpointError::unsupported};
if (payload.size() != m.payload_bytes || payload.size() < 16)
return std::unexpected{CheckpointError::payload};
@@ -217,4 +226,30 @@ restore_environmental_payload(std::span<const std::uint8_t> payload, const Check
return std::unexpected{CheckpointError::payload};
return std::move(*restored);
}
std::expected<std::unique_ptr<processes::LineageRegion>, CheckpointError>
restore_lineage_payload(std::span<const std::uint8_t> payload, const CheckpointManifest& m,
std::size_t budget) {
if (!structural(m))
return std::unexpected{CheckpointError::format};
if (!supported(m) || m.profile != CheckpointProfile::environmental_lineage_v1)
return std::unexpected{CheckpointError::unsupported};
// Container header (56 bytes) and the nested envelope's magic and format.
if (payload.size() != m.payload_bytes || payload.size() < 72)
return std::unexpected{CheckpointError::payload};
Reader container{payload.subspan(8)};
Reader nested{payload.subspan(64)};
if (container.u(4) != processes::lineage_region_format || nested.u(8) != m.versions.envelope)
return std::unexpected{CheckpointError::unsupported};
auto restored = processes::LineageRegion::restore(payload, {m.key.world, m.frame}, budget);
if (!restored)
return std::unexpected{restored.error() == processes::LineageRegionError::version
? CheckpointError::unsupported
: CheckpointError::payload};
const auto& community = (*restored)->community();
if (community.progress().tick != m.end_tick ||
community.tick_microseconds() != m.tick_microseconds)
return std::unexpected{CheckpointError::payload};
return std::move(*restored);
}
} // namespace evo::persistence
+179
View File
@@ -0,0 +1,179 @@
#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