Open M3 with the record that development and mutation will read, in a new evo::biology library that nothing else links yet. A genome is validated once at construction and exposes no mutator, so an invalid one cannot exist at runtime and callers never revalidate. A draft carries unvalidated input; create and decode apply identical semantic checks, so a decoded record is never less validated than a constructed one. Schema 1 fixes the seven v0.1 capability kinds. Each has a unit, an inclusive range, whether every genome must carry it, and the capabilities it mandates, all held as schema facts rather than record data, so no record can contradict its own units. A capability adds one parameter and a bitmask of declared developmental edges; the union of declared and mandated edges must be acyclic, which seven nodes make exact and bounded. Identity is a nonzero world-scoped genome ID distinct from every runtime identity, a nonzero origin event, and zero or one parent. Zero parents identifies a documented founder. The format reserves room for a second so recombination needs a schema version rather than a layout change. The encoding follows ADR-0008: explicit little endian, bounded before any allocation, an exact length match so trailing bytes are rejected, records strictly ascending by kind so the bytes are canonical and duplicates fall out of the same comparison, a stored digest recomputed on decode, and a trailing checksum. The digest covers identity and ancestry, so identical capabilities from different origins never share one. Neither value is an authenticity claim. Unknown schema versions fail closed. Tests cover ancestry rules, every declared range boundary from both sides, self, mutual and three-node cycles, a cycle closed by a mandated edge, truncation at every length, trailing and oversized buffers, every single-byte corruption, and three resealed edits that reach the digest, version and ordering checks rather than stopping at the checksum. One branch is recorded as uncovered rather than given a test that cannot fail: in schema 1 every mandated capability is also required. No genome affects any simulation. Native Release, clang-tidy analysis, ASan and format checks pass 17/17; golden d61ad5bf0c928c76 and ecosystem fingerprint 2cd4a15f9fbe9949 are unchanged. Linux CI has not run yet. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
286 lines
13 KiB
C++
286 lines
13 KiB
C++
#include "evo/biology/genome.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <iostream>
|
|
#include <source_location>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#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("genome line " + std::to_string(location.line()));
|
|
}
|
|
}
|
|
#define CHECK(expression) check(static_cast<bool>(expression))
|
|
// The three schema-required capabilities, in canonical order.
|
|
std::vector<CapabilityRecord> minimal() {
|
|
return {{Capability::energy_conversion, 400, 0},
|
|
{Capability::membrane, 120, 0},
|
|
{Capability::target_size, 5'000, 0}};
|
|
}
|
|
GenomeDraft founder() {
|
|
return {7, 1, 1, {}, minimal()};
|
|
}
|
|
GenomeDraft descendant() {
|
|
auto draft = founder();
|
|
draft.id = 8;
|
|
draft.origin_event = 2;
|
|
draft.parents = {7};
|
|
return draft;
|
|
}
|
|
std::uint32_t bits(std::initializer_list<Capability> kinds) {
|
|
std::uint32_t mask{};
|
|
for (const auto kind : kinds) {
|
|
mask |= capability_bit(kind);
|
|
}
|
|
return mask;
|
|
}
|
|
void reject(const GenomeDraft& draft, GenomeError expected,
|
|
std::source_location location = std::source_location::current()) {
|
|
const auto result = Genome::create(draft);
|
|
check(!result && result.error() == expected, location);
|
|
}
|
|
|
|
void founders_and_ancestry() {
|
|
const auto first = Genome::create(founder());
|
|
CHECK(first);
|
|
CHECK(first->id() == 7 && first->origin_event() == 1);
|
|
CHECK(first->schema_version() == 1 && first->development_version() == 1);
|
|
CHECK(first->parents().empty()); // Zero parents identifies a documented founder.
|
|
CHECK(first->capabilities().size() == 3);
|
|
CHECK(first->carries(Capability::membrane) && !first->carries(Capability::movement));
|
|
CHECK(first->parameter(Capability::membrane) && *first->parameter(Capability::membrane) == 120);
|
|
const auto absent = first->parameter(Capability::movement);
|
|
CHECK(!absent && absent.error() == GenomeError::missing_capability);
|
|
const auto child = Genome::create(descendant());
|
|
CHECK(child);
|
|
CHECK(child->parents().size() == 1 && child->parents()[0] == 7);
|
|
// Identical capabilities from a different origin never share a digest.
|
|
CHECK(child->content_digest() != first->content_digest());
|
|
CHECK(*child != *first);
|
|
// A draft is unordered input; the stored record is canonical.
|
|
auto shuffled = founder();
|
|
std::ranges::reverse(shuffled.capabilities);
|
|
const auto same = Genome::create(shuffled);
|
|
CHECK(same && *same == *first && same->encode() == first->encode());
|
|
CHECK(std::ranges::is_sorted(same->capabilities(), {}, &CapabilityRecord::kind));
|
|
}
|
|
|
|
void ancestry_rules() {
|
|
auto draft = descendant();
|
|
draft.id = 0;
|
|
reject(draft, GenomeError::invalid_identity);
|
|
draft = descendant();
|
|
draft.origin_event = 0;
|
|
reject(draft, GenomeError::invalid_identity);
|
|
draft = descendant();
|
|
draft.development_version = 0;
|
|
reject(draft, GenomeError::unsupported_version);
|
|
draft = descendant();
|
|
draft.parents = {0};
|
|
reject(draft, GenomeError::invalid_ancestry);
|
|
draft = descendant();
|
|
draft.parents = {draft.id}; // A genome cannot descend from itself.
|
|
reject(draft, GenomeError::invalid_ancestry);
|
|
draft = descendant();
|
|
draft.parents = {7, 9}; // Reserved in the format, rejected by schema 1.
|
|
reject(draft, GenomeError::invalid_ancestry);
|
|
draft = descendant();
|
|
draft.parents = {7, 7, 7};
|
|
reject(draft, GenomeError::invalid_ancestry);
|
|
}
|
|
|
|
void capability_and_range_rules() {
|
|
auto draft = founder();
|
|
draft.capabilities.clear();
|
|
reject(draft, GenomeError::missing_capability);
|
|
for (const auto required :
|
|
{Capability::energy_conversion, Capability::membrane, Capability::target_size}) {
|
|
draft = founder();
|
|
std::erase_if(draft.capabilities,
|
|
[required](const auto& record) { return record.kind == required; });
|
|
reject(draft, GenomeError::missing_capability);
|
|
}
|
|
draft = founder();
|
|
draft.capabilities.push_back({Capability::membrane, 130, 0});
|
|
reject(draft, GenomeError::duplicate_capability);
|
|
draft = founder();
|
|
draft.capabilities.push_back({static_cast<Capability>(capability_count), 1, 0});
|
|
reject(draft, GenomeError::unknown_capability);
|
|
draft = founder();
|
|
draft.capabilities.push_back({Capability::thermal_tolerance, 1, 1U << capability_count});
|
|
reject(draft, GenomeError::unknown_capability);
|
|
// Every boundary of every declared range, from both sides.
|
|
for (std::size_t index = 0; index < capability_count; ++index) {
|
|
const auto kind = static_cast<Capability>(index);
|
|
const auto& limits = capability_schema[index];
|
|
for (const auto value : {limits.minimum, limits.maximum}) {
|
|
draft = founder();
|
|
std::erase_if(draft.capabilities,
|
|
[kind](const auto& record) { return record.kind == kind; });
|
|
draft.capabilities.push_back({kind, value, limits.mandates});
|
|
const auto accepted = Genome::create(draft);
|
|
CHECK(accepted && *accepted->parameter(kind) == value);
|
|
}
|
|
if (limits.minimum > 0) {
|
|
draft = founder();
|
|
std::erase_if(draft.capabilities,
|
|
[kind](const auto& record) { return record.kind == kind; });
|
|
draft.capabilities.push_back({kind, limits.minimum - 1, limits.mandates});
|
|
reject(draft, GenomeError::parameter_out_of_range);
|
|
}
|
|
draft = founder();
|
|
std::erase_if(draft.capabilities,
|
|
[kind](const auto& record) { return record.kind == kind; });
|
|
draft.capabilities.push_back({kind, limits.maximum + 1, limits.mandates});
|
|
reject(draft, GenomeError::parameter_out_of_range);
|
|
}
|
|
}
|
|
|
|
void development_graph_rules() {
|
|
// Movement and ingestion mandate a membrane, which a founder already carries.
|
|
auto draft = founder();
|
|
draft.capabilities.push_back({Capability::movement, 250, 0});
|
|
draft.capabilities.push_back({Capability::ingestion, 90, 0});
|
|
CHECK(Genome::create(draft));
|
|
// A declared prerequisite naming a capability the genome does not carry.
|
|
draft = founder();
|
|
draft.capabilities.push_back(
|
|
{Capability::thermal_tolerance, 500, bits({Capability::movement})});
|
|
reject(draft, GenomeError::missing_prerequisite);
|
|
// In schema 1 every mandated capability is also required, so a mandated
|
|
// capability can only be absent when a required one already is. Removing
|
|
// membrane reports that, not the mandate.
|
|
draft = founder();
|
|
std::erase_if(draft.capabilities,
|
|
[](const auto& record) { return record.kind == Capability::membrane; });
|
|
draft.capabilities.push_back({Capability::movement, 250, 0});
|
|
reject(draft, GenomeError::missing_capability);
|
|
draft = founder();
|
|
draft.capabilities.push_back({Capability::reproduction, 300, 0});
|
|
CHECK(Genome::create(draft)); // Reproduction mandates energy conversion, which is present.
|
|
// Self, mutual and longer cycles in the declared graph.
|
|
draft = founder();
|
|
draft.capabilities[1].prerequisites = bits({Capability::membrane});
|
|
reject(draft, GenomeError::cyclic_development);
|
|
draft = founder();
|
|
draft.capabilities[0].prerequisites = bits({Capability::membrane});
|
|
draft.capabilities[1].prerequisites = bits({Capability::energy_conversion});
|
|
reject(draft, GenomeError::cyclic_development);
|
|
draft = founder();
|
|
draft.capabilities[0].prerequisites = bits({Capability::membrane});
|
|
draft.capabilities[1].prerequisites = bits({Capability::target_size});
|
|
draft.capabilities[2].prerequisites = bits({Capability::energy_conversion});
|
|
reject(draft, GenomeError::cyclic_development);
|
|
// The same three edges without the closing one are a valid ordering.
|
|
draft = founder();
|
|
draft.capabilities[0].prerequisites = bits({Capability::membrane});
|
|
draft.capabilities[1].prerequisites = bits({Capability::target_size});
|
|
CHECK(Genome::create(draft));
|
|
// A mandated edge can close a cycle a declared edge opened.
|
|
draft = founder();
|
|
draft.capabilities.push_back({Capability::movement, 250, 0});
|
|
draft.capabilities[1].prerequisites = bits({Capability::movement});
|
|
reject(draft, GenomeError::cyclic_development);
|
|
}
|
|
|
|
void canonical_encoding() {
|
|
auto draft = descendant();
|
|
draft.capabilities.push_back({Capability::movement, 250, bits({Capability::membrane})});
|
|
draft.capabilities.push_back({Capability::reproduction, 300, 0});
|
|
const auto genome = Genome::create(draft);
|
|
CHECK(genome);
|
|
const auto bytes = genome->encode();
|
|
CHECK(bytes.size() <= max_genome_bytes);
|
|
const auto restored = Genome::decode(bytes);
|
|
CHECK(restored && *restored == *genome);
|
|
CHECK(restored->encode() == bytes); // Re-encoding is byte-exact.
|
|
CHECK(restored->content_digest() == genome->content_digest());
|
|
CHECK(restored->parents().size() == 1 && restored->parents()[0] == 7);
|
|
// Truncation at every length, and trailing bytes, are rejected.
|
|
for (std::size_t length = 0; length < bytes.size(); ++length) {
|
|
CHECK(!Genome::decode(std::span{bytes}.first(length)));
|
|
}
|
|
auto extended = bytes;
|
|
extended.push_back(0);
|
|
const auto trailing = Genome::decode(extended);
|
|
CHECK(!trailing && trailing.error() == GenomeError::invalid_encoding);
|
|
std::vector<std::uint8_t> oversized(max_genome_bytes + 1, 0);
|
|
CHECK(!Genome::decode(oversized));
|
|
// Every single-byte corruption is caught by the checksum or a later check.
|
|
for (std::size_t index = 0; index < bytes.size(); ++index) {
|
|
auto damaged = bytes;
|
|
damaged[index] = static_cast<std::uint8_t>(damaged[index] ^ 0xffU);
|
|
CHECK(!Genome::decode(damaged));
|
|
}
|
|
auto wrong_magic = bytes;
|
|
wrong_magic[0] = 'X';
|
|
const auto rejected = Genome::decode(wrong_magic);
|
|
CHECK(!rejected && rejected.error() == GenomeError::invalid_encoding);
|
|
}
|
|
|
|
void encoded_record_rules() {
|
|
const auto genome = Genome::create(descendant());
|
|
CHECK(genome);
|
|
auto bytes = genome->encode();
|
|
const auto reseal = [](std::vector<std::uint8_t>& raw) {
|
|
std::uint64_t hash = 14695981039346656037ULL;
|
|
for (std::size_t index = 0; index + 8 < raw.size(); ++index) {
|
|
hash = (hash ^ raw[index]) * 1099511628211ULL;
|
|
}
|
|
for (std::size_t index = 0; index < 8; ++index) {
|
|
raw[raw.size() - 8 + index] = static_cast<std::uint8_t>((hash >> (8U * index)) & 0xffU);
|
|
}
|
|
};
|
|
// A rewritten record with a valid checksum still fails the digest check.
|
|
auto rewritten = bytes;
|
|
rewritten[16] = static_cast<std::uint8_t>(rewritten[16] + 1); // A different genome ID.
|
|
reseal(rewritten);
|
|
const auto tampered = Genome::decode(rewritten);
|
|
CHECK(!tampered && tampered.error() == GenomeError::digest_mismatch);
|
|
// An unsupported schema version fails closed before anything else is read.
|
|
auto future = bytes;
|
|
future[8] = 2;
|
|
reseal(future);
|
|
const auto ahead = Genome::decode(future);
|
|
CHECK(!ahead && ahead.error() == GenomeError::unsupported_version);
|
|
// Non-canonical ordering is rejected even though both kinds are known.
|
|
auto unordered = Genome::create(founder())->encode();
|
|
const std::size_t first_record = 36 + 4;
|
|
const std::size_t second_record = first_record + 9;
|
|
std::swap_ranges(unordered.begin() + first_record, unordered.begin() + second_record,
|
|
unordered.begin() + second_record);
|
|
reseal(unordered);
|
|
const auto misordered = Genome::decode(unordered);
|
|
CHECK(!misordered && misordered.error() == GenomeError::duplicate_capability);
|
|
// A declared count larger than the schema knows about is refused. One parent
|
|
// sits between the fixed prefix and the capability count.
|
|
auto overcounted = bytes;
|
|
overcounted[36 + 8] = static_cast<std::uint8_t>(capability_count + 1);
|
|
reseal(overcounted);
|
|
const auto excessive = Genome::decode(overcounted);
|
|
CHECK(!excessive && excessive.error() == GenomeError::duplicate_capability);
|
|
// A parent count beyond the reserved room is refused before any parent is read.
|
|
auto crowded = bytes;
|
|
crowded[32] = static_cast<std::uint8_t>(max_parents + 1);
|
|
reseal(crowded);
|
|
const auto overfull = Genome::decode(crowded);
|
|
CHECK(!overfull && overfull.error() == GenomeError::invalid_ancestry);
|
|
}
|
|
} // namespace
|
|
int main() try {
|
|
founders_and_ancestry();
|
|
ancestry_rules();
|
|
capability_and_range_rules();
|
|
development_graph_rules();
|
|
canonical_encoding();
|
|
encoded_record_rules();
|
|
std::cout << "PASS genome schema, ancestry, development graph and canonical encoding\n";
|
|
return 0;
|
|
} catch (...) {
|
|
return 1;
|
|
}
|