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>
138 lines
4.8 KiB
C++
138 lines
4.8 KiB
C++
#pragma once
|
|
|
|
#include <array>
|
|
#include <compare>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <expected>
|
|
#include <span>
|
|
#include <vector>
|
|
|
|
namespace evo::biology {
|
|
// Evolvable developmental capabilities, not genes, organs, species or DNA.
|
|
enum class Capability : std::uint8_t {
|
|
energy_conversion,
|
|
membrane,
|
|
movement,
|
|
ingestion,
|
|
reproduction,
|
|
target_size,
|
|
thermal_tolerance
|
|
};
|
|
inline constexpr std::size_t capability_count = 7;
|
|
// The unit is a schema fact per kind and is never encoded, so no record can
|
|
// contradict its own units.
|
|
enum class Unit : std::uint8_t {
|
|
per_thousand,
|
|
micrometres,
|
|
micrometres_per_tick,
|
|
femtograms,
|
|
femtograms_per_tick,
|
|
millikelvin
|
|
};
|
|
struct CapabilitySpec {
|
|
Unit unit;
|
|
std::uint32_t minimum;
|
|
std::uint32_t maximum;
|
|
bool required;
|
|
// Capabilities the schema mandates be present; developmental edges as well.
|
|
std::uint32_t mandates;
|
|
};
|
|
[[nodiscard]] constexpr std::uint32_t capability_bit(Capability kind) noexcept {
|
|
return 1U << static_cast<std::uint8_t>(kind);
|
|
}
|
|
// Chosen schema constants, reviewable as a unit. These are ranges of expressible
|
|
// capability, not balance, stats or a power score.
|
|
inline constexpr std::array<CapabilitySpec, capability_count> capability_schema{
|
|
CapabilitySpec{Unit::per_thousand, 1, 1000, true, 0},
|
|
CapabilitySpec{Unit::micrometres, 1, 10'000, true, 0},
|
|
CapabilitySpec{Unit::micrometres_per_tick, 0, 100'000, false,
|
|
capability_bit(Capability::membrane)},
|
|
CapabilitySpec{Unit::femtograms_per_tick, 0, 1'000'000, false,
|
|
capability_bit(Capability::membrane)},
|
|
CapabilitySpec{Unit::per_thousand, 1, 1000, false,
|
|
capability_bit(Capability::energy_conversion)},
|
|
CapabilitySpec{Unit::femtograms, 1, 1'000'000'000, true, 0},
|
|
CapabilitySpec{Unit::millikelvin, 1, 100'000, false, 0}};
|
|
|
|
struct CapabilityRecord {
|
|
Capability kind{};
|
|
std::uint32_t parameter{};
|
|
// Declared developmental ordering, over and above what the schema mandates.
|
|
std::uint32_t prerequisites{};
|
|
auto operator<=>(const CapabilityRecord&) const = default;
|
|
};
|
|
// Unvalidated input. Only Genome::create turns one into a record.
|
|
struct GenomeDraft {
|
|
std::uint64_t id{};
|
|
std::uint64_t origin_event{};
|
|
std::uint32_t development_version{};
|
|
std::vector<std::uint64_t> parents;
|
|
std::vector<CapabilityRecord> capabilities;
|
|
};
|
|
enum class GenomeError : std::uint8_t {
|
|
unsupported_version,
|
|
unknown_capability,
|
|
missing_capability,
|
|
duplicate_capability,
|
|
parameter_out_of_range,
|
|
missing_prerequisite,
|
|
cyclic_development,
|
|
invalid_identity,
|
|
invalid_ancestry,
|
|
digest_mismatch,
|
|
invalid_encoding
|
|
};
|
|
inline constexpr std::uint32_t genome_schema_version = 1;
|
|
inline constexpr std::size_t max_parents = 2;
|
|
inline constexpr std::size_t max_genome_bytes = 256;
|
|
|
|
// Immutable once constructed: validation happens exactly once, at the boundary.
|
|
class Genome {
|
|
public:
|
|
[[nodiscard]] static std::expected<Genome, GenomeError> create(const GenomeDraft& draft);
|
|
[[nodiscard]] static std::expected<Genome, GenomeError>
|
|
decode(std::span<const std::uint8_t> bytes);
|
|
[[nodiscard]] std::vector<std::uint8_t> encode() const;
|
|
[[nodiscard]] std::uint64_t id() const noexcept {
|
|
return id_;
|
|
}
|
|
[[nodiscard]] std::uint64_t origin_event() const noexcept {
|
|
return origin_event_;
|
|
}
|
|
[[nodiscard]] std::uint32_t schema_version() const noexcept {
|
|
return genome_schema_version;
|
|
}
|
|
[[nodiscard]] std::uint32_t development_version() const noexcept {
|
|
return development_version_;
|
|
}
|
|
// Zero parents identifies a documented founder; v1 permits at most one.
|
|
[[nodiscard]] std::span<const std::uint64_t> parents() const noexcept {
|
|
return parents_;
|
|
}
|
|
// Sorted strictly ascending by kind, so the record is canonical.
|
|
[[nodiscard]] std::span<const CapabilityRecord> capabilities() const noexcept {
|
|
return capabilities_;
|
|
}
|
|
[[nodiscard]] bool carries(Capability kind) const noexcept;
|
|
[[nodiscard]] std::expected<std::uint32_t, GenomeError>
|
|
parameter(Capability kind) const noexcept;
|
|
// Covers identity and ancestry, so distinct origins never share a digest.
|
|
// An integrity and deduplication aid, never a content key and never authenticity.
|
|
[[nodiscard]] std::uint64_t content_digest() const noexcept {
|
|
return digest_;
|
|
}
|
|
auto operator<=>(const Genome&) const = default;
|
|
|
|
private:
|
|
Genome() = default;
|
|
[[nodiscard]] std::vector<std::uint8_t> body() const;
|
|
std::uint64_t id_{};
|
|
std::uint64_t origin_event_{};
|
|
std::uint32_t development_version_{};
|
|
std::vector<std::uint64_t> parents_;
|
|
std::vector<CapabilityRecord> capabilities_;
|
|
std::uint64_t digest_{};
|
|
};
|
|
} // namespace evo::biology
|