Add cellular genome revision profile migration
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m59s
Foundation / checks (checks) (push) Failing after 2m10s
Foundation / checks (postgres) (push) Failing after 10m34s
Foundation / checks (network) (push) Failing after 12m19s
Foundation / checks (tsan) (push) Failing after 2m14s
Foundation / checks (client) (push) Successful in 15m7s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m59s
Foundation / checks (checks) (push) Failing after 2m10s
Foundation / checks (postgres) (push) Failing after 10m34s
Foundation / checks (network) (push) Failing after 12m19s
Foundation / checks (tsan) (push) Failing after 2m14s
Foundation / checks (client) (push) Successful in 15m7s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
This commit is contained in:
+156
-11
@@ -1,6 +1,7 @@
|
||||
#include "evo/biology/lineage.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <tuple>
|
||||
|
||||
namespace evo::biology {
|
||||
namespace {
|
||||
@@ -69,12 +70,65 @@ std::expected<void, HistoryError> LineageArchive::record_death(std::uint64_t org
|
||||
if (when < found->record.born) {
|
||||
return std::unexpected{HistoryError::invalid_tick};
|
||||
}
|
||||
if (std::ranges::any_of(revisions_, [&](const auto& revision) {
|
||||
return revision.organism == organism && revision.at >= when;
|
||||
}))
|
||||
return std::unexpected{HistoryError::invalid_tick};
|
||||
found->record.alive = false;
|
||||
found->record.died = when;
|
||||
// The organism is gone. Its genome, ancestry and origin are untouched.
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, HistoryError>
|
||||
LineageArchive::record_mutation(std::uint64_t organism, const Genome& successor, Tick when) {
|
||||
const auto organism_record = locate(organisms_, organism, by_organism);
|
||||
if (organism_record == organisms_.end() || organism_record->id != organism ||
|
||||
!organism_record->record.alive)
|
||||
return std::unexpected{HistoryError::invalid_revision};
|
||||
if (when <= organism_record->record.born)
|
||||
return std::unexpected{HistoryError::invalid_tick};
|
||||
if (genomes_.size() >= max_archived_genomes)
|
||||
return std::unexpected{HistoryError::archive_full};
|
||||
if (successor.id() == 0 || successor.origin_event() != successor.id() ||
|
||||
successor.parents().size() != 1 || holds(genomes_, successor.id(), by_genome) ||
|
||||
std::ranges::any_of(genomes_, [&](const auto& genome) {
|
||||
return genome.origin_event() == successor.origin_event();
|
||||
}))
|
||||
return std::unexpected{HistoryError::invalid_revision};
|
||||
const auto current = current_genome(organism);
|
||||
if (!current || successor.parents()[0] != *current)
|
||||
return std::unexpected{HistoryError::invalid_revision};
|
||||
if (std::ranges::any_of(revisions_, [&](const auto& revision) {
|
||||
return revision.organism == organism && revision.at >= when;
|
||||
}))
|
||||
return std::unexpected{HistoryError::invalid_tick};
|
||||
if (std::ranges::any_of(revisions_, [&](const auto& revision) {
|
||||
return revision.origin_event == successor.origin_event();
|
||||
}))
|
||||
return std::unexpected{HistoryError::invalid_revision};
|
||||
|
||||
revisions_.reserve(revisions_.size() + 1);
|
||||
genomes_.reserve(genomes_.size() + 1);
|
||||
const auto genome_slot = locate(genomes_, successor.id(), by_genome);
|
||||
genomes_.insert(genome_slot, successor);
|
||||
const OrganismGenomeRevision revision{organism, *current, successor.id(),
|
||||
successor.origin_event(), when};
|
||||
const auto revision_slot = std::ranges::lower_bound(
|
||||
revisions_, revision,
|
||||
[](const OrganismGenomeRevision& left, const OrganismGenomeRevision& right) {
|
||||
return std::tie(left.at.value, left.organism) <
|
||||
std::tie(right.at.value, right.organism);
|
||||
});
|
||||
revisions_.insert(revision_slot, revision);
|
||||
format_version_ = lineage_archive_revision_format;
|
||||
return {};
|
||||
}
|
||||
|
||||
void LineageArchive::upgrade_to_revision_format() noexcept {
|
||||
format_version_ = lineage_archive_revision_format;
|
||||
}
|
||||
|
||||
std::expected<Genome, HistoryError> LineageArchive::find(std::uint64_t genome) const {
|
||||
const auto found = locate(genomes_, genome, by_genome);
|
||||
if (found == genomes_.end() || found->id() != genome) {
|
||||
@@ -91,6 +145,58 @@ std::expected<OrganismRecord, HistoryError> LineageArchive::organism(std::uint64
|
||||
return found->record;
|
||||
}
|
||||
|
||||
std::expected<std::uint64_t, HistoryError>
|
||||
LineageArchive::current_genome(std::uint64_t identity) const {
|
||||
const auto found = locate(organisms_, identity, by_organism);
|
||||
if (found == organisms_.end() || found->id != identity)
|
||||
return std::unexpected{HistoryError::unknown_organism};
|
||||
auto current = found->record.genome;
|
||||
for (const auto& revision : revisions_) {
|
||||
if (revision.organism != identity)
|
||||
continue;
|
||||
if (revision.previous_genome != current)
|
||||
return std::unexpected{HistoryError::invalid_revision};
|
||||
current = revision.successor_genome;
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
std::expected<void, HistoryError>
|
||||
LineageArchive::append_revision(std::uint64_t identity, std::uint64_t previous,
|
||||
std::uint64_t successor, std::uint64_t origin_event, Tick when) {
|
||||
const auto record = locate(organisms_, identity, by_organism);
|
||||
const auto child = find(successor);
|
||||
const auto current = current_genome(identity);
|
||||
if (record == organisms_.end() || record->id != identity || !child || !current ||
|
||||
*current != previous || child->parents().size() != 1 || child->parents()[0] != previous ||
|
||||
child->origin_event() != origin_event || origin_event != successor ||
|
||||
when <= record->record.born || (!record->record.alive && when >= record->record.died))
|
||||
return std::unexpected{HistoryError::invalid_revision};
|
||||
if (std::ranges::any_of(revisions_, [&](const auto& revision) {
|
||||
return revision.organism == identity && revision.at >= when;
|
||||
}))
|
||||
return std::unexpected{HistoryError::invalid_tick};
|
||||
if (std::ranges::any_of(revisions_,
|
||||
[&](const auto& revision) {
|
||||
return revision.successor_genome == successor ||
|
||||
revision.origin_event == origin_event;
|
||||
}) ||
|
||||
std::ranges::any_of(genomes_, [&](const auto& genome) {
|
||||
return genome.id() != successor && genome.origin_event() == origin_event;
|
||||
}))
|
||||
return std::unexpected{HistoryError::invalid_revision};
|
||||
const OrganismGenomeRevision revision{identity, previous, successor, origin_event, when};
|
||||
const auto slot = std::ranges::lower_bound(
|
||||
revisions_, revision,
|
||||
[](const OrganismGenomeRevision& left, const OrganismGenomeRevision& right) {
|
||||
return std::tie(left.at.value, left.organism) <
|
||||
std::tie(right.at.value, right.organism);
|
||||
});
|
||||
revisions_.insert(slot, revision);
|
||||
format_version_ = lineage_archive_revision_format;
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<std::vector<std::uint64_t>, HistoryError>
|
||||
LineageArchive::ancestry(std::uint64_t genome) const {
|
||||
if (!holds(genomes_, genome, by_genome)) {
|
||||
@@ -178,6 +284,7 @@ std::uint64_t LineageArchive::highest_organism() const noexcept {
|
||||
namespace {
|
||||
constexpr std::uint64_t archive_magic = 0x3152414C4F564541ULL; // "AEVOLAR1"
|
||||
constexpr std::size_t archive_header_bytes = 32;
|
||||
constexpr std::size_t archive_revision_header_bytes = 40;
|
||||
std::uint64_t fnv(std::span<const std::uint8_t> bytes) noexcept {
|
||||
std::uint64_t value = 14695981039346656037ULL;
|
||||
for (const auto byte : bytes)
|
||||
@@ -229,11 +336,15 @@ std::vector<std::uint8_t> LineageArchive::encode() const {
|
||||
// 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]; });
|
||||
|
||||
const auto version = format_version_;
|
||||
const auto header_bytes =
|
||||
version == lineage_archive_format ? archive_header_bytes : archive_revision_header_bytes;
|
||||
std::vector<std::uint8_t> out;
|
||||
out.reserve(archive_header_bytes + (genomes_.size() * (2 + max_genome_bytes)) +
|
||||
(organisms_.size() * archived_organism_bytes) + 8);
|
||||
out.reserve(header_bytes + (genomes_.size() * (2 + max_genome_bytes)) +
|
||||
(organisms_.size() * archived_organism_bytes) +
|
||||
(revisions_.size() * archived_genome_revision_bytes) + 8);
|
||||
put(out, archive_magic, 8);
|
||||
put(out, lineage_archive_format, 4);
|
||||
put(out, version, 4);
|
||||
// The highest genome schema held: an archive of schema 1 records keeps its bytes.
|
||||
std::uint32_t schema = genome_schema_v1;
|
||||
for (const auto& g : genomes_)
|
||||
@@ -241,6 +352,10 @@ std::vector<std::uint8_t> LineageArchive::encode() const {
|
||||
put(out, schema, 4);
|
||||
put(out, genomes_.size(), 4);
|
||||
put(out, organisms_.size(), 4);
|
||||
if (version == lineage_archive_revision_format) {
|
||||
put(out, revisions_.size(), 4);
|
||||
put(out, 0, 4);
|
||||
}
|
||||
put(out, 0, 8); // Total length, patched below.
|
||||
for (const auto i : order) {
|
||||
const auto record = genomes_[i].encode();
|
||||
@@ -254,9 +369,17 @@ std::vector<std::uint8_t> LineageArchive::encode() const {
|
||||
put(out, entry.record.alive ? 0 : entry.record.died.value, 8);
|
||||
put(out, entry.record.alive ? 1 : 0, 1);
|
||||
}
|
||||
for (const auto& revision : revisions_) {
|
||||
put(out, revision.organism, 8);
|
||||
put(out, revision.previous_genome, 8);
|
||||
put(out, revision.successor_genome, 8);
|
||||
put(out, revision.origin_event, 8);
|
||||
put(out, revision.at.value, 8);
|
||||
}
|
||||
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);
|
||||
out[(version == lineage_archive_format ? 24 : 32) + b] =
|
||||
static_cast<std::uint8_t>((total >> (8U * b)) & 0xFFU);
|
||||
put(out, fnv(out), 8);
|
||||
return out;
|
||||
}
|
||||
@@ -265,24 +388,37 @@ 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};
|
||||
const auto format = get(bytes, 8, 4);
|
||||
const auto header_bytes =
|
||||
format == lineage_archive_format ? archive_header_bytes : archive_revision_header_bytes;
|
||||
if (format != lineage_archive_format && format != lineage_archive_revision_format)
|
||||
return std::unexpected{ArchiveCodecError::version};
|
||||
if (bytes.size() < header_bytes + 8)
|
||||
return std::unexpected{ArchiveCodecError::format};
|
||||
const auto schema = get(bytes, 12, 4);
|
||||
if (get(bytes, 8, 4) != lineage_archive_format || schema < genome_schema_v1 ||
|
||||
schema > genome_schema_version)
|
||||
if (schema < genome_schema_v1 || schema > 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)
|
||||
const auto revisions = format == lineage_archive_revision_format ? get(bytes, 24, 4) : 0;
|
||||
if ((format == lineage_archive_format && bytes.size() > max_archive_legacy_bytes) ||
|
||||
(format == lineage_archive_revision_format && bytes.size() > max_archive_bytes) ||
|
||||
genomes > max_archived_genomes || organisms > max_archived_organisms ||
|
||||
revisions > max_archived_genomes)
|
||||
return std::unexpected{ArchiveCodecError::bounds};
|
||||
if (get(bytes, 24, 8) != bytes.size())
|
||||
if ((format == lineage_archive_revision_format && get(bytes, 28, 4) != 0) ||
|
||||
genomes > max_archived_genomes || organisms > max_archived_organisms)
|
||||
return std::unexpected{ArchiveCodecError::format};
|
||||
if (get(bytes, format == lineage_archive_format ? 24 : 32, 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;
|
||||
archive.format_version_ = static_cast<std::uint32_t>(format);
|
||||
std::uint32_t highest = genome_schema_v1;
|
||||
std::size_t at = archive_header_bytes;
|
||||
std::size_t at = header_bytes;
|
||||
for (std::uint64_t g = 0; g < genomes; ++g) {
|
||||
if (at + 2 > body.size())
|
||||
return std::unexpected{ArchiveCodecError::format};
|
||||
@@ -301,7 +437,9 @@ LineageArchive::decode(std::span<const std::uint8_t> bytes) {
|
||||
// Canonical: the header names exactly the highest schema held (ADR-0099).
|
||||
if (highest != schema)
|
||||
return std::unexpected{ArchiveCodecError::version};
|
||||
if (body.size() - at != organisms * archived_organism_bytes)
|
||||
const auto expected_tail =
|
||||
organisms * archived_organism_bytes + revisions * archived_genome_revision_bytes;
|
||||
if (body.size() - at != expected_tail)
|
||||
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);
|
||||
@@ -314,6 +452,13 @@ LineageArchive::decode(std::span<const std::uint8_t> bytes) {
|
||||
(alive == 0 && !archive.record_death(id, died)))
|
||||
return std::unexpected{ArchiveCodecError::history};
|
||||
}
|
||||
for (std::uint64_t r = 0; r < revisions; ++r, at += archived_genome_revision_bytes) {
|
||||
const auto recorded =
|
||||
archive.append_revision(get(body, at, 8), get(body, at + 8, 8), get(body, at + 16, 8),
|
||||
get(body, at + 24, 8), Tick{get(body, at + 32, 8)});
|
||||
if (!recorded)
|
||||
return std::unexpected{ArchiveCodecError::history};
|
||||
}
|
||||
if (!std::ranges::equal(archive.encode(), bytes))
|
||||
return std::unexpected{ArchiveCodecError::noncanonical};
|
||||
return archive;
|
||||
|
||||
Reference in New Issue
Block a user