M3.5 retains the genomes M3.4 produces and the organisms that carried them. The named risk is deleting extinct genomes, so the answer is structural: LineageArchive has no removal operation at all, not a guarded one and not a private one. Retention is a property of the interface rather than a rule someone has to remember, which is the only form of "keep extinct genomes indefinitely" that a later change cannot quietly undo. Four concepts assert that, with a positive control so the check is known to be live rather than vacuous. Admission requires the parent to be present, so ancestry never dangles and the parent graph is acyclic by construction: a cycle would need one of its members admitted before itself. A duplicate identity is refused rather than overwritten, because overwriting is deletion renamed. Organism identity and genome identity stay separate. A death is terminal for the organism and touches nothing about the genome it carried; ancestry, descendants and divergence all resolve afterwards. The quieter risk is calling something extinct that is merely unobserved. The archive reports living or absent and has no third answer: terminal extinction is a policy judgement over a reconciled watermark covering suspended members, eggs, spores and in-flight transfers, none of which this type can see, so the word does not appear in the API. Absence is also reversible, and a test shows a lineage regaining a carrier. Capacity is bounded and admission fails loudly at the limit, since refusing to record is not deleting but a silent drop would be. The roadmap lists an NPC-mutation decision as a dependency, and OPEN_QUESTIONS leaves it open until M12. It stays open: the archive records an origin event and never an actor, so whether an NPC may diverge changes nothing here. Two clang-tidy findings were fixed rather than suppressed. std::map members made the archive's move potentially throwing, because MSVC's map move constructor allocates; sorted vectors replaced them. A wrapper struct holding a Genome by value had the same problem, since ADR-0030 made Genome copy-only, so genomes are stored directly under their own id(). The redundant carrier tally went with it: carriers are counted from the organism records, so no second tally can drift. Native Release, clang-tidy analysis, ASan and format checks pass 21/21. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
231 lines
11 KiB
C++
231 lines
11 KiB
C++
#include "evo/biology/lineage.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <source_location>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <type_traits>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
using namespace evo::biology;
|
|
void check(bool condition, std::source_location location = std::source_location::current()) {
|
|
if (!condition) {
|
|
throw std::runtime_error("lineage line " + std::to_string(location.line()));
|
|
}
|
|
}
|
|
#define CHECK(expression) check(static_cast<bool>(expression))
|
|
Genome descend(std::uint64_t id, std::uint64_t parent, std::uint32_t conversion = 400) {
|
|
GenomeDraft source;
|
|
source.id = id;
|
|
source.origin_event = id;
|
|
source.development_version = 1;
|
|
if (parent != 0) {
|
|
source.parents = {parent};
|
|
}
|
|
source.capabilities = {{Capability::energy_conversion, conversion, 0},
|
|
{Capability::membrane, 120, 0},
|
|
{Capability::target_size, 8'000'000, 0}};
|
|
const auto genome = Genome::create(source);
|
|
check(genome.has_value());
|
|
return *genome;
|
|
}
|
|
// A founder with two branches, one of which branches again:
|
|
// 1 -> 2 -> 4, 5
|
|
// -> 3
|
|
LineageArchive branching() {
|
|
LineageArchive archive;
|
|
check(archive.admit(descend(1, 0, 400)).has_value());
|
|
check(archive.admit(descend(2, 1, 409)).has_value());
|
|
check(archive.admit(descend(3, 1, 391)).has_value());
|
|
check(archive.admit(descend(4, 2, 418)).has_value());
|
|
check(archive.admit(descend(5, 2, 400)).has_value());
|
|
return archive;
|
|
}
|
|
|
|
void a_genome_outlives_every_organism_that_carried_it() {
|
|
auto archive = branching();
|
|
CHECK(archive.record_birth(100, 4, evo::Tick{10}));
|
|
CHECK(archive.record_birth(101, 4, evo::Tick{12}));
|
|
CHECK(archive.living_carriers(4) && *archive.living_carriers(4) == 2);
|
|
CHECK(*archive.status(4) == LineageStatus::living);
|
|
CHECK(archive.record_death(100, evo::Tick{30}));
|
|
CHECK(*archive.living_carriers(4) == 1 && *archive.status(4) == LineageStatus::living);
|
|
CHECK(archive.record_death(101, evo::Tick{31}));
|
|
// No organism carries it now. Everything about the genome is still here.
|
|
CHECK(*archive.living_carriers(4) == 0);
|
|
CHECK(*archive.status(4) == LineageStatus::absent);
|
|
const auto kept = archive.find(4);
|
|
CHECK(kept && kept->id() == 4 && kept->parents()[0] == 2);
|
|
CHECK(*kept == descend(4, 2, 418));
|
|
const auto line = archive.ancestry(4);
|
|
CHECK(line && (*line == std::vector<std::uint64_t>{1, 2, 4}));
|
|
CHECK(archive.children(2) && archive.children(2)->size() == 2);
|
|
// The organisms themselves are still on record, with their terminal ticks.
|
|
const auto first = archive.organism(100);
|
|
CHECK(first && !first->alive && first->genome == 4);
|
|
CHECK(first->born.value == 10 && first->died.value == 30);
|
|
}
|
|
|
|
void ancestry_and_descendants_resolve_through_a_dead_middle() {
|
|
auto archive = branching();
|
|
// Every carrier of the middle genome dies; its children are untouched.
|
|
CHECK(archive.record_birth(200, 2, evo::Tick{5}));
|
|
CHECK(archive.record_birth(201, 4, evo::Tick{6}));
|
|
CHECK(archive.record_death(200, evo::Tick{20}));
|
|
CHECK(*archive.status(2) == LineageStatus::absent);
|
|
CHECK(*archive.status(4) == LineageStatus::living);
|
|
const auto through = archive.ancestry(4);
|
|
CHECK(through && (*through == std::vector<std::uint64_t>{1, 2, 4}));
|
|
const auto kids = archive.children(2);
|
|
CHECK(kids && (*kids == std::vector<std::uint64_t>{4, 5}));
|
|
// The founder has no living carrier either, and is equally intact.
|
|
CHECK(*archive.status(1) == LineageStatus::absent);
|
|
CHECK(archive.find(1) && archive.find(1)->parents().empty());
|
|
CHECK(archive.ancestry(1) && archive.ancestry(1)->size() == 1);
|
|
}
|
|
|
|
void branching_lineages_resolve_their_divergence() {
|
|
auto archive = branching();
|
|
CHECK(archive.common_ancestor(4, 5) && *archive.common_ancestor(4, 5) == 2);
|
|
CHECK(*archive.common_ancestor(4, 3) == 1);
|
|
CHECK(*archive.common_ancestor(2, 3) == 1);
|
|
CHECK(*archive.common_ancestor(4, 2) == 2); // An ancestor of itself.
|
|
CHECK(*archive.common_ancestor(4, 4) == 4);
|
|
CHECK(*archive.common_ancestor(1, 5) == 1);
|
|
// A separate founder shares nothing.
|
|
CHECK(archive.admit(descend(9, 0, 500)));
|
|
CHECK(archive.common_ancestor(4, 9) && *archive.common_ancestor(4, 9) == 0);
|
|
const auto unknown = archive.common_ancestor(4, 77);
|
|
CHECK(!unknown && unknown.error() == HistoryError::unknown_genome);
|
|
}
|
|
|
|
void admission_refuses_duplicates_and_dangling_parents() {
|
|
LineageArchive archive;
|
|
CHECK(archive.admit(descend(1, 0)));
|
|
// Re-admitting an ID would destroy the earlier record.
|
|
const auto repeated = archive.admit(descend(1, 0, 500));
|
|
CHECK(!repeated && repeated.error() == HistoryError::duplicate_genome);
|
|
CHECK(archive.find(1) && archive.find(1)->content_digest() == descend(1, 0).content_digest());
|
|
// A child before its parent would leave ancestry dangling, and would be the
|
|
// only way to build a cycle.
|
|
const auto orphan = archive.admit(descend(5, 4));
|
|
CHECK(!orphan && orphan.error() == HistoryError::unknown_parent);
|
|
CHECK(archive.archived() == 1);
|
|
const auto missing = archive.ancestry(4);
|
|
CHECK(!missing && missing.error() == HistoryError::unknown_genome);
|
|
// Admitting in order is fine, and the graph stays acyclic by that order.
|
|
CHECK(archive.admit(descend(4, 1)));
|
|
CHECK(archive.admit(descend(5, 4)));
|
|
CHECK(archive.ancestry(5) && (*archive.ancestry(5) == std::vector<std::uint64_t>{1, 4, 5}));
|
|
}
|
|
|
|
void death_is_terminal_and_births_are_unique() {
|
|
auto archive = branching();
|
|
const auto nobody = archive.record_death(500, evo::Tick{1});
|
|
CHECK(!nobody && nobody.error() == HistoryError::unknown_organism);
|
|
const auto nowhere = archive.record_birth(300, 77, evo::Tick{1});
|
|
CHECK(!nowhere && nowhere.error() == HistoryError::unknown_genome);
|
|
const auto anonymous = archive.record_birth(0, 1, evo::Tick{1});
|
|
CHECK(!anonymous && anonymous.error() == HistoryError::invalid_identity);
|
|
CHECK(archive.record_birth(300, 3, evo::Tick{8}));
|
|
// An identity is never reused, whether its holder lives or not.
|
|
const auto twice = archive.record_birth(300, 5, evo::Tick{9});
|
|
CHECK(!twice && twice.error() == HistoryError::duplicate_organism);
|
|
const auto backwards = archive.record_death(300, evo::Tick{7});
|
|
CHECK(!backwards && backwards.error() == HistoryError::invalid_tick);
|
|
CHECK(archive.record_death(300, evo::Tick{40}));
|
|
const auto again = archive.record_death(300, evo::Tick{41});
|
|
CHECK(!again && again.error() == HistoryError::already_dead);
|
|
// Nothing brings it back, and its record keeps the first terminal tick.
|
|
const auto reborn = archive.record_birth(300, 3, evo::Tick{50});
|
|
CHECK(!reborn && reborn.error() == HistoryError::duplicate_organism);
|
|
const auto record = archive.organism(300);
|
|
CHECK(record && !record->alive && record->died.value == 40);
|
|
CHECK(*archive.status(3) == LineageStatus::absent);
|
|
}
|
|
|
|
void absence_is_never_reported_as_extinction() {
|
|
auto archive = branching();
|
|
// Nothing was ever born: every lineage is absent, and none is extinct.
|
|
for (const std::uint64_t genome : {1, 2, 3, 4, 5}) {
|
|
CHECK(*archive.status(genome) == LineageStatus::absent);
|
|
CHECK(*archive.living_carriers(genome) == 0);
|
|
CHECK(archive.find(genome)); // Still retained regardless.
|
|
}
|
|
// The enumeration itself offers no way to say extinct.
|
|
CHECK(static_cast<int>(LineageStatus::living) != static_cast<int>(LineageStatus::absent));
|
|
// A lineage can return from absence: absence is about carriers, not the
|
|
// record, and the archive never claimed the lineage was finished.
|
|
CHECK(archive.record_birth(400, 5, evo::Tick{1}));
|
|
CHECK(*archive.status(5) == LineageStatus::living);
|
|
CHECK(archive.record_death(400, evo::Tick{2}));
|
|
CHECK(*archive.status(5) == LineageStatus::absent);
|
|
CHECK(archive.record_birth(401, 5, evo::Tick{3}));
|
|
CHECK(*archive.status(5) == LineageStatus::living);
|
|
}
|
|
|
|
// Retention is a property of the interface: the type offers no removal
|
|
// operation, so no caller can choose to delete a record. Written as concepts
|
|
// rather than bare requires-expressions so the check is a substitution failure
|
|
// instead of a hard error.
|
|
template <typename Archive>
|
|
concept erases = requires(Archive& archive, std::uint64_t id) { archive.erase(id); };
|
|
template <typename Archive>
|
|
concept removes = requires(Archive& archive, std::uint64_t id) { archive.remove(id); };
|
|
template <typename Archive>
|
|
concept clears = requires(Archive& archive) { archive.clear(); };
|
|
template <typename Archive>
|
|
concept prunes = requires(Archive& archive) { archive.prune(); };
|
|
|
|
void the_archive_offers_no_way_to_forget() {
|
|
static_assert(!erases<LineageArchive>);
|
|
static_assert(!removes<LineageArchive>);
|
|
static_assert(!clears<LineageArchive>);
|
|
static_assert(!prunes<LineageArchive>);
|
|
// The check itself is live: a container that does erase by key is detected.
|
|
static_assert(erases<std::map<std::uint64_t, std::uint64_t>>);
|
|
static_assert(clears<std::map<std::uint64_t, std::uint64_t>>);
|
|
auto archive = branching();
|
|
const auto before = archive.archived();
|
|
CHECK(archive.record_birth(700, 1, evo::Tick{1}));
|
|
CHECK(archive.record_death(700, evo::Tick{2}));
|
|
// Death changed nothing about how much is retained.
|
|
CHECK(archive.archived() == before && archive.recorded() == 1);
|
|
}
|
|
|
|
void capacity_refuses_loudly_rather_than_dropping() {
|
|
LineageArchive archive;
|
|
CHECK(archive.admit(descend(1, 0)));
|
|
for (std::uint64_t id = 2; id <= max_archived_genomes; ++id) {
|
|
CHECK(archive.admit(descend(id, id - 1)));
|
|
}
|
|
CHECK(archive.archived() == max_archived_genomes);
|
|
const auto overflowing = archive.admit(descend(max_archived_genomes + 1, max_archived_genomes));
|
|
CHECK(!overflowing && overflowing.error() == HistoryError::archive_full);
|
|
// Refusing to record is not deleting: everything admitted is still here.
|
|
CHECK(archive.archived() == max_archived_genomes);
|
|
CHECK(archive.find(1) && archive.find(max_archived_genomes));
|
|
const auto deep = archive.ancestry(max_archived_genomes);
|
|
CHECK(deep && deep->size() == max_archived_genomes);
|
|
CHECK(deep->front() == 1 && deep->back() == max_archived_genomes);
|
|
CHECK(*archive.common_ancestor(max_archived_genomes, 1) == 1);
|
|
}
|
|
} // namespace
|
|
int main() try {
|
|
a_genome_outlives_every_organism_that_carried_it();
|
|
ancestry_and_descendants_resolve_through_a_dead_middle();
|
|
branching_lineages_resolve_their_divergence();
|
|
admission_refuses_duplicates_and_dangling_parents();
|
|
death_is_terminal_and_births_are_unique();
|
|
absence_is_never_reported_as_extinction();
|
|
the_archive_offers_no_way_to_forget();
|
|
capacity_refuses_loudly_rather_than_dropping();
|
|
std::cout << "PASS lineage retention, branching ancestry and provisional absence\n";
|
|
return 0;
|
|
} catch (...) {
|
|
return 1;
|
|
}
|