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;
|
||||
|
||||
@@ -149,7 +149,8 @@ retry_cellular_profile_migration(Postgres& db, const CellularProfileMigrationPre
|
||||
const auto manifest = decode_manifest(prepared.manifest);
|
||||
if (!manifest || manifest->key != prepared.key || manifest->commit != prepared.commit ||
|
||||
manifest->profile != CheckpointProfile::cellular_lives_v1 ||
|
||||
manifest->cellular_versions != cellular_v2_versions ||
|
||||
(manifest->cellular_versions != cellular_v2_versions &&
|
||||
manifest->cellular_versions != cellular_v3_versions) ||
|
||||
manifest->begin_tick != manifest->end_tick || manifest->batch != BatchDigests{})
|
||||
return std::unexpected{migration_failure(PublicationError::candidate, {}, {}, prepared)};
|
||||
|
||||
@@ -232,6 +233,56 @@ migrate_cellular_profile_to_v2(Postgres& db, const FilesystemObjectStore& object
|
||||
db, CellularProfileMigrationPrepared{previous->key, commit, std::move(*encoded)}, timeout);
|
||||
}
|
||||
|
||||
std::expected<CommittedCheckpoint, CellularProfileMigrationFailure>
|
||||
migrate_cellular_profile_to_v3(Postgres& db, const FilesystemObjectStore& objects,
|
||||
const LeaseGrant& lease, const CommittedCheckpoint& predecessor,
|
||||
Uuid commit, const Sha256Digest& build, milliseconds timeout) {
|
||||
const auto previous = decode_manifest(predecessor.manifest);
|
||||
const auto previous_digest = sha256(predecessor.manifest);
|
||||
if (!previous || !previous_digest ||
|
||||
previous->profile != CheckpointProfile::cellular_lives_v1 ||
|
||||
previous->cellular_versions != cellular_v2_versions ||
|
||||
predecessor.head.digest != *previous_digest ||
|
||||
predecessor.head.sequence != previous->sequence ||
|
||||
predecessor.head.commit != previous->commit ||
|
||||
predecessor.head.end_tick != previous->end_tick || lease.frame != previous->frame ||
|
||||
previous->sequence == 0 || previous->end_tick == 0)
|
||||
return std::unexpected{migration_failure(PublicationError::candidate)};
|
||||
|
||||
auto source_payload = objects.get({previous->payload_sha256, previous->payload_bytes});
|
||||
if (!source_payload)
|
||||
return std::unexpected{
|
||||
migration_failure(PublicationError::predecessor, source_payload.error())};
|
||||
const auto source_digest = sha256(*source_payload);
|
||||
if (!source_digest || *source_digest != previous->payload_sha256)
|
||||
return std::unexpected{
|
||||
migration_failure(PublicationError::predecessor, ObjectError::corrupt)};
|
||||
auto source = restore_cellular_payload(*source_payload, *previous);
|
||||
if (!source || (*source)->format_version() != server::cellular_lives_format ||
|
||||
(*source)->rules_version() != server::cellular_lives_rules)
|
||||
return std::unexpected{migration_failure(PublicationError::predecessor)};
|
||||
auto migrated = (*source)->migrate_to_v3();
|
||||
if (!migrated || (*migrated)->tick() != (*source)->tick() ||
|
||||
(*migrated)->current_total() != (*source)->current_total())
|
||||
return std::unexpected{migration_failure(PublicationError::candidate)};
|
||||
|
||||
const auto payload = (*migrated)->snapshot();
|
||||
const auto payload_digest = sha256(payload);
|
||||
if (!payload_digest)
|
||||
return std::unexpected{migration_failure(PublicationError::digest)};
|
||||
auto manifest = cellular_profile_migration_manifest(*previous, *previous_digest, commit, lease,
|
||||
build, payload.size(), *payload_digest);
|
||||
auto encoded = manifest ? encode_manifest(*manifest)
|
||||
: std::expected<std::vector<std::uint8_t>, CheckpointError>{
|
||||
std::unexpected{CheckpointError::format}};
|
||||
if (!encoded || !restore_cellular_payload(payload, *manifest))
|
||||
return std::unexpected{migration_failure(PublicationError::candidate)};
|
||||
if (auto stored = put_verified(objects, payload, {*payload_digest, payload.size()}); !stored)
|
||||
return std::unexpected{migration_failure(stored.error().kind, stored.error().object)};
|
||||
return retry_cellular_profile_migration(
|
||||
db, CellularProfileMigrationPrepared{previous->key, commit, std::move(*encoded)}, timeout);
|
||||
}
|
||||
|
||||
std::expected<CommittedCheckpoint, CellularPublicationFailure>
|
||||
retry_cellular_prepared(Postgres& db, const CellularPreparedCommit& prepared,
|
||||
milliseconds timeout) {
|
||||
@@ -385,8 +436,11 @@ recover_cellular_checkpoint(Postgres& db, const FilesystemObjectStore& objects,
|
||||
predecessor->head.digest != *source_manifest_digest ||
|
||||
predecessor->head.commit != manifest->predecessor_commit ||
|
||||
predecessor->head.sequence != manifest->sequence - 1 ||
|
||||
source_manifest->cellular_versions != cellular_v1_versions ||
|
||||
source_manifest->profile != CheckpointProfile::cellular_lives_v1 ||
|
||||
(source_manifest->cellular_versions != cellular_v1_versions &&
|
||||
source_manifest->cellular_versions != cellular_v2_versions) ||
|
||||
((source_manifest->cellular_versions == cellular_v1_versions) !=
|
||||
(manifest->cellular_versions == cellular_v2_versions)) ||
|
||||
source_manifest->end_tick != manifest->end_tick ||
|
||||
(*migration)->source_sequence != source_manifest->sequence ||
|
||||
(*migration)->source_commit != source_manifest->commit ||
|
||||
@@ -409,9 +463,11 @@ recover_cellular_checkpoint(Postgres& db, const FilesystemObjectStore& objects,
|
||||
return fail_with(RecoveryError::tail);
|
||||
auto source_state = restore_cellular_payload(*source_bytes, *source_manifest);
|
||||
if (!source_state ||
|
||||
(*source_state)->format_version() != server::cellular_lives_legacy_format)
|
||||
(*source_state)->format_version() != source_manifest->cellular_versions.envelope)
|
||||
return fail_with(RecoveryError::tail);
|
||||
auto migrated = (*source_state)->migrate_to_v2();
|
||||
auto migrated = source_manifest->cellular_versions == cellular_v1_versions
|
||||
? (*source_state)->migrate_to_v2()
|
||||
: (*source_state)->migrate_to_v3();
|
||||
if (!migrated || (*migrated)->tick() != manifest->end_tick ||
|
||||
(*migrated)->snapshot() != *payload)
|
||||
return fail_with(RecoveryError::history);
|
||||
|
||||
@@ -61,12 +61,15 @@ bool structural(const CheckpointManifest& m) noexcept {
|
||||
const auto& b = m.batch;
|
||||
const bool primitive = m.profile == CheckpointProfile::primitive_lives_v1;
|
||||
const bool cellular = m.profile == CheckpointProfile::cellular_lives_v1;
|
||||
const bool cellular_migration =
|
||||
cellular && m.sequence > 0 && m.cellular_versions == cellular_v2_versions &&
|
||||
m.begin_tick == m.end_tick && m.end_tick > 0 && b == BatchDigests{};
|
||||
const bool cellular_migration = cellular && m.sequence > 0 &&
|
||||
(m.cellular_versions == cellular_v2_versions ||
|
||||
m.cellular_versions == cellular_v3_versions) &&
|
||||
m.begin_tick == m.end_tick && m.end_tick > 0 &&
|
||||
b == BatchDigests{};
|
||||
if ((!cellular && m.cellular_versions != cellular_v1_versions) ||
|
||||
(cellular && m.cellular_versions != cellular_v1_versions &&
|
||||
m.cellular_versions != cellular_v2_versions) ||
|
||||
m.cellular_versions != cellular_v2_versions &&
|
||||
m.cellular_versions != cellular_v3_versions) ||
|
||||
(cellular && m.versions != environmental_v1_versions))
|
||||
return false;
|
||||
if (primitive && (m.manifest_version != 2 || m.tick_microseconds != 50000 ||
|
||||
@@ -139,12 +142,18 @@ bool supported(const CheckpointManifest& m) noexcept {
|
||||
return m.manifest_version == 2 && m.history_profile == primitive_history_profile &&
|
||||
m.versions == primitive_v1_versions && m.tick_microseconds == 50000 &&
|
||||
m.rng_streams == 0 && m.durability == DurabilityPolicy::single_volume_development;
|
||||
if (m.profile == CheckpointProfile::cellular_lives_v1)
|
||||
return m.manifest_version == 2 && m.history_profile == cellular_history_profile &&
|
||||
if (m.profile == CheckpointProfile::cellular_lives_v1) {
|
||||
const bool history_matches = (m.cellular_versions != cellular_v3_versions &&
|
||||
m.history_profile == cellular_history_profile) ||
|
||||
(m.cellular_versions == cellular_v3_versions &&
|
||||
m.history_profile == cellular_revision_history_profile);
|
||||
return m.manifest_version == 2 && history_matches &&
|
||||
(m.cellular_versions == cellular_v1_versions ||
|
||||
m.cellular_versions == cellular_v2_versions) &&
|
||||
m.cellular_versions == cellular_v2_versions ||
|
||||
m.cellular_versions == cellular_v3_versions) &&
|
||||
m.versions == environmental_v1_versions && m.tick_microseconds == 50000 &&
|
||||
m.rng_streams == 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 &&
|
||||
@@ -354,10 +363,12 @@ std::expected<CheckpointManifest, CheckpointError> cellular_profile_migration_ma
|
||||
if (!supported(p))
|
||||
return std::unexpected{CheckpointError::unsupported};
|
||||
if (!structural(p) || p.profile != CheckpointProfile::cellular_lives_v1 ||
|
||||
p.cellular_versions != cellular_v1_versions || p.sequence == 0 || p.sequence == maximum ||
|
||||
p.end_tick == 0 || lease.frame != p.frame || lease.holder == Uuid{} || lease.epoch == 0 ||
|
||||
predecessor_digest == Sha256Digest{} || commit == Uuid{} || commit == p.commit ||
|
||||
build == Sha256Digest{} || payload_bytes == 0 || payload_sha256 == Sha256Digest{})
|
||||
(p.cellular_versions != cellular_v1_versions &&
|
||||
p.cellular_versions != cellular_v2_versions) ||
|
||||
p.sequence == 0 || p.sequence == maximum || p.end_tick == 0 || lease.frame != p.frame ||
|
||||
lease.holder == Uuid{} || lease.epoch == 0 || predecessor_digest == Sha256Digest{} ||
|
||||
commit == Uuid{} || commit == p.commit || build == Sha256Digest{} || payload_bytes == 0 ||
|
||||
payload_sha256 == Sha256Digest{})
|
||||
return std::unexpected{CheckpointError::chain};
|
||||
CheckpointManifest m = p;
|
||||
m.commit = commit;
|
||||
@@ -371,7 +382,13 @@ std::expected<CheckpointManifest, CheckpointError> cellular_profile_migration_ma
|
||||
m.build = build;
|
||||
m.payload_bytes = payload_bytes;
|
||||
m.payload_sha256 = payload_sha256;
|
||||
m.cellular_versions = cellular_v2_versions;
|
||||
if (p.cellular_versions == cellular_v1_versions) {
|
||||
m.cellular_versions = cellular_v2_versions;
|
||||
m.history_profile = cellular_history_profile;
|
||||
} else {
|
||||
m.cellular_versions = cellular_v3_versions;
|
||||
m.history_profile = cellular_revision_history_profile;
|
||||
}
|
||||
m.batch = {};
|
||||
if (!structural(m))
|
||||
return std::unexpected{CheckpointError::format};
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace evo::server {
|
||||
namespace {
|
||||
constexpr std::uint64_t cellular_lives_v1_magic = 0x3130564C43564541ULL; // AEVCLV01
|
||||
constexpr std::uint64_t cellular_lives_v2_magic = 0x3230564C43564541ULL; // AEVCLV02
|
||||
constexpr std::uint64_t cellular_lives_v3_magic = 0x3330564C43564541ULL; // AEVCLV03
|
||||
constexpr std::size_t cellular_lives_header_bytes = 212;
|
||||
constexpr std::size_t cellular_site_bytes = 8;
|
||||
constexpr std::size_t cellular_life_v1_bytes = 168;
|
||||
@@ -222,8 +223,9 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
lives[j].identity.organism == life.identity.organism)
|
||||
return std::unexpected{CellularLivesError::duplicate_life};
|
||||
const auto record = archive.organism(life.identity.organism);
|
||||
if (!record || !record->alive || record->genome != life.identity.genome ||
|
||||
record->born != life.identity.born)
|
||||
const auto current_genome = archive.current_genome(life.identity.organism);
|
||||
if (!record || !record->alive || !current_genome ||
|
||||
*current_genome != life.identity.genome || record->born != life.identity.born)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto budget = budget_for(archive, life, config.ambient_temperature_millikelvin);
|
||||
if (!budget || budget->body != life.body || life.body.energy.value != 0 ||
|
||||
@@ -248,7 +250,10 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
const auto record = archive.organism(corpse.organism);
|
||||
if (!record || record->alive || record->died.value != corpse.died_tick)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto genome = archive.find(record->genome);
|
||||
const auto current_genome = archive.current_genome(corpse.organism);
|
||||
if (!current_genome)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto genome = archive.find(*current_genome);
|
||||
if (!genome)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto budget = biology::metabolic_budget(*genome, corpse.growth,
|
||||
@@ -347,13 +352,13 @@ CellularLives::create(const PrimitiveLives& opening,
|
||||
opening.balances()[body_index],
|
||||
opening.balances()[reserve_index]});
|
||||
}
|
||||
auto archive = opening.archive();
|
||||
std::ranges::sort(lives, {}, [](const auto& life) { return life.identity.life; });
|
||||
for (std::size_t i = 0; i < lives.size(); ++i) {
|
||||
const auto& life = lives[i];
|
||||
if (i != 0 && lives[i - 1].identity.life == life.identity.life)
|
||||
return std::unexpected{CellularLivesError::duplicate_life};
|
||||
const auto budget =
|
||||
budget_for(opening.archive(), life, config.ambient_temperature_millikelvin);
|
||||
const auto budget = budget_for(archive, life, config.ambient_temperature_millikelvin);
|
||||
if (!budget || budget->phenotype.extent == 0 ||
|
||||
budget->phenotype.extent > config.area.width_um ||
|
||||
budget->phenotype.extent > config.area.height_um || life.body.energy.value != 0 ||
|
||||
@@ -368,9 +373,9 @@ CellularLives::create(const PrimitiveLives& opening,
|
||||
const auto nursery = opening.balances()[0];
|
||||
nursery_config.funding = nursery;
|
||||
const auto heat = opening.balances()[1];
|
||||
const auto current = validate_state(nursery_config.world, nursery_config.region,
|
||||
nursery_config.frame, config, nursery_config, *habitat,
|
||||
lives, {}, opening.archive(), nursery, heat, opening_total);
|
||||
const auto current =
|
||||
validate_state(nursery_config.world, nursery_config.region, nursery_config.frame, config,
|
||||
nursery_config, *habitat, lives, {}, archive, nursery, heat, opening_total);
|
||||
if (!current)
|
||||
return std::unexpected{current.error()};
|
||||
|
||||
@@ -379,8 +384,8 @@ CellularLives::create(const PrimitiveLives& opening,
|
||||
const auto frame = nursery_config.frame;
|
||||
auto state = std::make_unique<State>(world, region, frame, config, std::move(nursery_config),
|
||||
std::move(habitat), std::move(lives),
|
||||
std::vector<CellularOwnedCorpse>{}, opening.archive(),
|
||||
nursery, heat, opening_total, *current);
|
||||
std::vector<CellularOwnedCorpse>{}, archive, nursery, heat,
|
||||
opening_total, *current);
|
||||
return std::unique_ptr<CellularLives>{new CellularLives{std::move(state)}};
|
||||
}
|
||||
|
||||
@@ -458,8 +463,11 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
std::vector<std::uint8_t> bytes;
|
||||
bytes.reserve(total_bytes);
|
||||
Writer out{bytes};
|
||||
out.put(state_->format_version == cellular_lives_legacy_format ? cellular_lives_v1_magic
|
||||
: cellular_lives_v2_magic);
|
||||
const auto magic =
|
||||
state_->format_version == cellular_lives_legacy_format ? cellular_lives_v1_magic
|
||||
: state_->format_version == cellular_lives_revision_format ? cellular_lives_v3_magic
|
||||
: cellular_lives_v2_magic;
|
||||
out.put(magic);
|
||||
out.put(state_->format_version, 4);
|
||||
out.put(state_->rules_version, 4);
|
||||
out.put(state_->world);
|
||||
@@ -595,6 +603,32 @@ CellularLives::migrate_to_v2() const {
|
||||
return candidate;
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||
CellularLives::migrate_to_v3() const {
|
||||
if (state_->format_version != cellular_lives_format ||
|
||||
state_->rules_version != cellular_lives_rules ||
|
||||
state_->archive.format_version() != biology::lineage_archive_format)
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
auto archive = state_->archive;
|
||||
archive.upgrade_to_revision_format();
|
||||
auto habitat = biology::CellularHabitat::restore(state_->habitat->snapshot(), state_->world,
|
||||
state_->region, state_->frame);
|
||||
if (!habitat)
|
||||
return std::unexpected{CellularLivesError::habitat};
|
||||
const auto current =
|
||||
validate_state(state_->world, state_->region, state_->frame, state_->config,
|
||||
state_->nursery_config, **habitat, state_->lives, state_->corpses, archive,
|
||||
state_->nursery, state_->heat, state_->opening_total);
|
||||
if (!current || *current != state_->current_total)
|
||||
return std::unexpected{current ? CellularLivesError::conservation : current.error()};
|
||||
auto candidate = std::unique_ptr<CellularLives>{new CellularLives{std::make_unique<State>(
|
||||
state_->world, state_->region, state_->frame, state_->config, state_->nursery_config,
|
||||
std::move(*habitat), state_->lives, state_->corpses, archive, state_->nursery, state_->heat,
|
||||
state_->opening_total, state_->current_total, cellular_lives_revision_format,
|
||||
cellular_lives_rules)}};
|
||||
return candidate;
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||
CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) const {
|
||||
if (request.action == 0 ||
|
||||
@@ -723,10 +757,14 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
const auto rules_version = static_cast<std::uint32_t>(in.get(4));
|
||||
const bool legacy = magic == cellular_lives_v1_magic &&
|
||||
format_version == cellular_lives_legacy_format && rules_version == 1;
|
||||
const bool current_version = magic == cellular_lives_v2_magic &&
|
||||
format_version == cellular_lives_format &&
|
||||
rules_version == cellular_lives_rules;
|
||||
if (!legacy && !current_version)
|
||||
const bool offer_version = magic == cellular_lives_v2_magic &&
|
||||
format_version == cellular_lives_format &&
|
||||
rules_version == cellular_lives_rules;
|
||||
const bool revision_version = magic == cellular_lives_v3_magic &&
|
||||
format_version == cellular_lives_revision_format &&
|
||||
rules_version == cellular_lives_rules;
|
||||
const bool mutation_version = offer_version || revision_version;
|
||||
if (!legacy && !mutation_version)
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
const auto saved_world = in.get();
|
||||
const auto saved_region = in.get();
|
||||
@@ -785,7 +823,7 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
life.position = {in.get(), in.get()};
|
||||
life.body = in.inventory();
|
||||
life.reserve = in.inventory();
|
||||
if (current_version) {
|
||||
if (mutation_version) {
|
||||
auto& mutation = life.mutation;
|
||||
mutation.fed_energy_credit_pj = in.get();
|
||||
mutation.event_index = in.get();
|
||||
@@ -873,7 +911,10 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
}
|
||||
if (current_version) {
|
||||
if (archive->format_version() != (revision_version ? biology::lineage_archive_revision_format
|
||||
: biology::lineage_archive_format))
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
if (mutation_version) {
|
||||
for (const auto& life : lives) {
|
||||
if (!life.mutation.offer)
|
||||
continue;
|
||||
|
||||
@@ -256,7 +256,7 @@ PrimitiveLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world
|
||||
c.funding = in.inventory();
|
||||
const auto sites = in.get(4), count = in.get(4), archive_bytes = in.get(4);
|
||||
if (in.get(4) != 0 || sites == 0 || sites > max_primitive_lives || count > sites ||
|
||||
archive_bytes > biology::max_archive_bytes ||
|
||||
archive_bytes > biology::max_archive_legacy_bytes ||
|
||||
bytes.size() != life_header_bytes + 8 * sites + life_record_bytes * count +
|
||||
40 * (2 + 2 * sites) + archive_bytes + 8)
|
||||
return std::unexpected{LifeCodecError::format};
|
||||
|
||||
Reference in New Issue
Block a user