Keep every lineage, and never call absence extinction

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>
This commit is contained in:
dingo
2026-09-23 16:55:36 -04:00
co-authored by Claude Opus 5
parent badd28fe9b
commit 8020347f9b
11 changed files with 696 additions and 7 deletions
+173
View File
@@ -0,0 +1,173 @@
#include "evo/biology/lineage.hpp"
#include <algorithm>
namespace evo::biology {
namespace {
constexpr auto by_organism = [](const auto& entry) { return entry.id; };
constexpr auto by_genome = [](const Genome& record) { return record.id(); };
template <typename Records, typename Identify>
auto locate(Records& records, std::uint64_t identity, Identify identify) {
return std::ranges::lower_bound(records, identity, {}, identify);
}
template <typename Records, typename Identify>
bool holds(const Records& records, std::uint64_t identity, Identify identify) {
const auto found = locate(records, identity, identify);
return found != records.end() && identify(*found) == identity;
}
} // namespace
std::expected<void, HistoryError> LineageArchive::admit(const Genome& genome) {
if (genomes_.size() >= max_archived_genomes) {
// Refusing to record is not deleting; a silent drop would be.
return std::unexpected{HistoryError::archive_full};
}
const auto slot = locate(genomes_, genome.id(), by_genome);
// Overwriting an existing record is deletion wearing another name.
if (slot != genomes_.end() && slot->id() == genome.id()) {
return std::unexpected{HistoryError::duplicate_genome};
}
for (const auto parent : genome.parents()) {
if (!holds(genomes_, parent, by_genome)) {
return std::unexpected{HistoryError::unknown_parent};
}
}
genomes_.insert(slot, genome);
return {};
}
std::expected<void, HistoryError> LineageArchive::record_birth(std::uint64_t organism,
std::uint64_t genome, Tick when) {
if (organism == 0) {
return std::unexpected{HistoryError::invalid_identity};
}
if (organisms_.size() >= max_archived_organisms) {
return std::unexpected{HistoryError::archive_full};
}
if (!holds(genomes_, genome, by_genome)) {
return std::unexpected{HistoryError::unknown_genome};
}
const auto slot = locate(organisms_, organism, by_organism);
// An identity is never reused, alive or dead, so this is not a search for a
// free slot: a repeat is a mistake and is reported as one.
if (slot != organisms_.end() && slot->id == organism) {
return std::unexpected{HistoryError::duplicate_organism};
}
organisms_.insert(slot, ArchivedOrganism{organism, OrganismRecord{genome, when, {}, true}});
return {};
}
std::expected<void, HistoryError> LineageArchive::record_death(std::uint64_t organism, Tick when) {
const auto found = locate(organisms_, organism, by_organism);
if (found == organisms_.end() || found->id != organism) {
return std::unexpected{HistoryError::unknown_organism};
}
if (!found->record.alive) {
return std::unexpected{HistoryError::already_dead};
}
if (when < found->record.born) {
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<Genome, HistoryError> LineageArchive::find(std::uint64_t genome) const {
const auto found = locate(genomes_, genome, by_genome);
if (found == genomes_.end() || found->id() != genome) {
return std::unexpected{HistoryError::unknown_genome};
}
return *found;
}
std::expected<OrganismRecord, HistoryError> LineageArchive::organism(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};
}
return found->record;
}
std::expected<std::vector<std::uint64_t>, HistoryError>
LineageArchive::ancestry(std::uint64_t genome) const {
if (!holds(genomes_, genome, by_genome)) {
return std::unexpected{HistoryError::unknown_genome};
}
// Admission order makes the parent graph acyclic, so this terminates.
std::vector<std::uint64_t> line;
auto current = genome;
while (true) {
line.push_back(current);
const auto& record = *locate(genomes_, current, by_genome);
if (record.parents().empty()) {
break;
}
current = record.parents()[0];
}
std::ranges::reverse(line); // Founder first.
return line;
}
std::expected<std::vector<std::uint64_t>, HistoryError>
LineageArchive::children(std::uint64_t genome) const {
if (!holds(genomes_, genome, by_genome)) {
return std::unexpected{HistoryError::unknown_genome};
}
std::vector<std::uint64_t> descendants;
for (const auto& record : genomes_) {
if (!record.parents().empty() && record.parents()[0] == genome) {
descendants.push_back(record.id());
}
}
return descendants;
}
std::expected<std::uint64_t, HistoryError>
LineageArchive::common_ancestor(std::uint64_t left, std::uint64_t right) const {
const auto one = ancestry(left);
if (!one) {
return std::unexpected{one.error()};
}
const auto other = ancestry(right);
if (!other) {
return std::unexpected{other.error()};
}
// Both lines start at their founder, so the last shared prefix entry is the
// most recent common ancestor. Zero when the founders differ.
std::uint64_t shared = 0;
for (std::size_t index = 0; index < one->size() && index < other->size(); ++index) {
if ((*one)[index] != (*other)[index]) {
break;
}
shared = (*one)[index];
}
return shared;
}
std::expected<std::uint64_t, HistoryError>
LineageArchive::living_carriers(std::uint64_t genome) const {
if (!holds(genomes_, genome, by_genome)) {
return std::unexpected{HistoryError::unknown_genome};
}
std::uint64_t carrying = 0;
for (const auto& entry : organisms_) {
if (entry.record.alive && entry.record.genome == genome) {
++carrying;
}
}
return carrying;
}
std::expected<LineageStatus, HistoryError> LineageArchive::status(std::uint64_t genome) const {
const auto carrying = living_carriers(genome);
if (!carrying) {
return std::unexpected{carrying.error()};
}
// Absent, never extinct: this type cannot see the world watermark that a
// claim of terminal extinction would require.
return *carrying == 0 ? LineageStatus::absent : LineageStatus::living;
}
} // namespace evo::biology