Files
aevolution/include/evo/biology/genome.hpp
T
dingoandClaude Opus 5.5 c183021fd5
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m27s
Foundation / postgres-tools (push) Successful in 33s
Foundation / server-image (push) Successful in 4m14s
Foundation / image (push) Successful in 4m41s
feat(biology): genome schema 7 traits, founder 6 and development 7
Phase 1 of the cellular revamp build plan (ADR-0179). Genome schema 7 carries
twenty 16-bit cellular traits after the movement arrangement: density, upkeep
efficiency, growth speed, collection per channel (E/C/N/P/O), burn efficiency
per matter channel, engulf capacity, kill absorption, litter, the body recipe
(C/N/P/O per thousand, summing to 1000) and adhesion. Its membrane is held in
nanometres so it can change in 5% steps of the founder's 1 um. The largest
record still fits the 256-byte bound.

Development 7 and founder 6 serve rules 13: density sets how large a body is
for its mass, the recipe sets the body's composition (intake keeps the founder
shares), upkeep is an essential share no trait reduces plus the rest over upkeep
efficiency (region-declared BodyTuning, trial 25%), the producer speed penalty
is dropped, and the reproduction value is the bud's share of the body with the
construction discount fixed at the founder's level. Founder 6 builds exactly
founder 5's cell. Earlier schemas and development versions are unchanged.

The development and metabolism test mains now print the failing line. Refreshes
the DEV session window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 18:09:06 -04:00

281 lines
14 KiB
C++

#pragma once
#include <array>
#include <compare>
#include <cstddef>
#include <cstdint>
#include <expected>
#include <optional>
#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,
photosynthesis // Schema 5 (ADR-0175); unrepresentable in earlier schemas.
};
inline constexpr std::size_t capability_count = 8;
// 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, // Schema 1: per 50 ms tick.
femtograms,
femtograms_per_tick, // Schema 1: per 50 ms tick.
millikelvin,
micrometres_per_second, // Schema 2 (ADR-0099).
femtograms_per_second, // Schema 2 (ADR-0099).
nanometres // Schema 7 membrane (ADR-0179).
};
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. Schema 1 states rates per 50 ms tick.
inline constexpr std::array<CapabilitySpec, capability_count> capability_schema_v1{
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},
// An empty range: no schema-1 record can carry photosynthesis.
CapabilitySpec{Unit::per_thousand, 1, 0, false, 0}};
// Schema 2 (ADR-0099): the same capabilities with rates per second. Each rate range is
// schema 1's times 20, so every schema 1 value has an exact schema 2 equivalent.
inline constexpr std::array<CapabilitySpec, capability_count> capability_schema_v2{
CapabilitySpec{Unit::per_thousand, 1, 1000, true, 0},
CapabilitySpec{Unit::micrometres, 1, 10'000, true, 0},
CapabilitySpec{Unit::micrometres_per_second, 0, 2'000'000, false,
capability_bit(Capability::membrane)},
CapabilitySpec{Unit::femtograms_per_second, 0, 20'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},
CapabilitySpec{Unit::per_thousand, 1, 0, false, 0}}; // Not in schemas 2-4.
// Schema 5 (ADR-0175): schema 2 plus photosynthesis, an efficiency per thousand that
// needs a membrane to hold its organelles.
inline constexpr std::array<CapabilitySpec, capability_count> capability_schema_v5{
CapabilitySpec{Unit::per_thousand, 1, 1000, true, 0},
CapabilitySpec{Unit::micrometres, 1, 10'000, true, 0},
CapabilitySpec{Unit::micrometres_per_second, 0, 2'000'000, false,
capability_bit(Capability::membrane)},
CapabilitySpec{Unit::femtograms_per_second, 0, 20'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},
CapabilitySpec{Unit::per_thousand, 1, 1000, false, capability_bit(Capability::membrane)}};
// Schema 7 (ADR-0179): schema 5 with the membrane in nanometres, so it can change in 5%
// steps of the founder's 1 um (1,000 nm). Other units are unchanged; energy conversion is
// read as the share of bubble energy kept, reproduction as the bud's share of the body.
inline constexpr std::array<CapabilitySpec, capability_count> capability_schema_v7{
CapabilitySpec{Unit::per_thousand, 1, 1000, true, 0},
CapabilitySpec{Unit::nanometres, 1, 10'000'000, true, 0},
CapabilitySpec{Unit::micrometres_per_second, 0, 2'000'000, false,
capability_bit(Capability::membrane)},
CapabilitySpec{Unit::femtograms_per_second, 0, 20'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},
CapabilitySpec{Unit::per_thousand, 1, 1000, false, capability_bit(Capability::membrane)}};
// The current schema's table.
inline constexpr const auto& capability_schema = capability_schema_v2;
// Schema 1 rates are per tick of this quantum.
inline constexpr std::uint64_t schema_v1_tick_microseconds = 50'000;
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;
};
// Schema 6 (Cellular_movement_draft.md 001-010): the body's movement arrangement as
// indices in reference points, where 100 is the founder's arrangement. Burst, anchor and
// colony drive are acquired modules: 0 means absent. Output 0 means propulsion was shed
// (009); independent propulsion also needs the movement capability.
struct MovementArrangement {
std::uint16_t output{}, efficiency{}, burst{}, steering{}, streamlining{}, braking{}, anchor{},
coasting{}, colony_drive{};
auto operator<=>(const MovementArrangement&) const = default;
};
inline constexpr std::uint16_t movement_reference_points = 100;
inline constexpr std::uint16_t max_movement_points = 1'000;
inline constexpr MovementArrangement founder_movement_arrangement{100, 100, 0, 100, 100,
100, 0, 100, 0};
// Schema 7 (ADR-0179): heritable cellular traits beyond the capability records. Indices are
// reference points where 100 is the founder; the recipe is per thousand of the body's
// structure in C, N, P and other matter (summing to 1000); litter is extra offspring per
// bud in hundredths (150 = one extra plus a 50% chance of a second); adhesion is 0 or 1.
struct CellularTraits {
std::uint16_t density{}, upkeep_efficiency{}, growth_speed{};
std::array<std::uint16_t, 5> collection{}; // E, C, N, P, O.
std::array<std::uint16_t, 4> burn{}; // C, N, P, O.
std::uint16_t engulf_capacity{}, kill_absorption{}, litter{};
std::array<std::uint16_t, 4> recipe{}; // C, N, P, O per thousand.
std::uint16_t adhesion{};
auto operator<=>(const CellularTraits&) const = default;
};
inline constexpr std::uint16_t max_trait_points = 1'000;
inline constexpr std::uint16_t max_litter = 9'900; // At most 100 offspring per bud.
inline constexpr CellularTraits founder_cellular_traits{
100, 100, 100, {100, 100, 100, 100, 100}, {100, 100, 100, 100},
100, 100, 0, {500, 150, 50, 300}, 0};
// 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;
// The schema the parameters are written in. Schema 1 is the default so an existing
// literal keeps its meaning (rates per 50 ms tick); new records name schema 2. Last,
// so positional initialisers are unaffected.
std::uint32_t schema_version{1};
// Required from schema 6 and refused before it.
std::optional<MovementArrangement> movement_arrangement{};
// Required from schema 7 and refused before it.
std::optional<CellularTraits> cellular_traits{};
};
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_v1 = 1;
inline constexpr std::uint32_t genome_schema_v2 = 2;
// Schema 3 (rules 6, 2026-10-15 balance): schema 2's capability table and units, with
// mutation steps a third as large (mutation.cpp). Founded only by rules-6 regions.
inline constexpr std::uint32_t genome_schema_v3 = 3;
// Schema 4 (rules 7, ADR-0137): schema 2's table and units; a mutation step is a tenth of
// the current value (at least 1), and an acquired capability starts at the founder's value.
inline constexpr std::uint32_t genome_schema_v4 = 4;
// Schema 5 (rules 11, ADR-0175): schema 4's steps with the photosynthesis capability.
inline constexpr std::uint32_t genome_schema_v5 = 5;
// Schema 6 (rules 12, Cellular_Region_Movement_Plan.md): schema 5's capabilities and steps
// plus the movement arrangement.
inline constexpr std::uint32_t genome_schema_v6 = 6;
// Schema 7 (rules 13, ADR-0179): schema 6 with the membrane in nanometres and the cellular
// traits. Its mutations are the named catalog, not the generic operators.
inline constexpr std::uint32_t genome_schema_v7 = 7;
// The newest schema. Older records stay valid and are never rewritten (ADR-0099).
inline constexpr std::uint32_t genome_schema_version = genome_schema_v7;
// The capability table of a supported schema (1 to 7); nullptr otherwise.
[[nodiscard]] constexpr const std::array<CapabilitySpec, capability_count>*
capability_schema_for(std::uint32_t schema) noexcept {
return schema == genome_schema_v1 ? &capability_schema_v1
: schema == genome_schema_v2 || schema == genome_schema_v3 || schema == genome_schema_v4
? &capability_schema_v2
: schema == genome_schema_v5 || schema == genome_schema_v6 ? &capability_schema_v5
: schema == genome_schema_v7 ? &capability_schema_v7
: nullptr;
}
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);
// Copy-only on purpose. A moved-from record would keep its identity, origin
// and digest while losing its capabilities, which is precisely the invalid
// genome this type exists to make unrepresentable. Declaring the copy
// operations suppresses the implicit moves, so a move decays to a copy; the
// record is at most seven capabilities and one parent, so that is cheap.
Genome(const Genome&) = default;
Genome& operator=(const Genome&) = default;
[[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 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_;
}
// Present exactly from schema 6.
[[nodiscard]] const std::optional<MovementArrangement>& movement_arrangement() const noexcept {
return movement_arrangement_;
}
// Present exactly from schema 7.
[[nodiscard]] const std::optional<CellularTraits>& cellular_traits() const noexcept {
return cellular_traits_;
}
[[nodiscard]] bool carries(Capability kind) const noexcept;
[[nodiscard]] std::expected<std::uint32_t, GenomeError>
parameter(Capability kind) const noexcept;
// A rate capability (movement, ingestion) per second, whatever the schema: a
// schema 1 per-tick value times 20, exactly. Other capabilities are returned as
// stored.
[[nodiscard]] std::expected<std::uint64_t, GenomeError>
per_second(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::uint32_t schema_version_{genome_schema_v1};
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::optional<MovementArrangement> movement_arrangement_;
std::optional<CellularTraits> cellular_traits_;
std::uint64_t digest_{};
};
} // namespace evo::biology