diff --git a/deploy/kubernetes/region/dev-session-route/listener.yaml b/deploy/kubernetes/region/dev-session-route/listener.yaml index 319635d..4177132 100644 --- a/deploy/kubernetes/region/dev-session-route/listener.yaml +++ b/deploy/kubernetes/region/dev-session-route/listener.yaml @@ -12,7 +12,7 @@ spec: env: - {name: REGION_SESSION, value: "dev"} - {name: REGION_SESSION_LISTEN, value: "0.0.0.0:7400"} - - {name: REGION_SESSION_WINDOW, value: "dev-20261022T215205-42ad8184:1792720325"} + - {name: REGION_SESSION_WINDOW, value: "dev-20261022T220847-feb5f51f:1792721327"} - {name: REGION_M10_MAX_GRANT_LIFETIME, value: "14400"} ports: # QUIC (ADR-0090): UDP only; nothing listens on TCP 7400. diff --git a/include/evo/biology/development.hpp b/include/evo/biology/development.hpp index 72fb087..dbec669 100644 --- a/include/evo/biology/development.hpp +++ b/include/evo/biology/development.hpp @@ -32,10 +32,18 @@ inline constexpr std::uint64_t development_v2_rate_microseconds = 1'000'000; [[nodiscard]] constexpr std::uint64_t development_rate_microseconds(std::uint32_t development_version) noexcept { return development_version == 1 ? development_v1_tick_microseconds - : development_version >= 2 && development_version <= 6 - ? development_v2_rate_microseconds // Versions 3-6 keep version 2's rates. + : development_version >= 2 && development_version <= 7 + ? development_v2_rate_microseconds // Versions 3-7 keep version 2's rates. : 0; } +// Development version 7 (ADR-0179): region-declared body tuning. Upkeep is an essential +// share of the mass-based upkeep that no trait reduces, plus the rest divided by the +// genome's upkeep efficiency. Earlier versions ignore it. +struct BodyTuning { + std::uint32_t essential_upkeep_per_thousand{250}; // Trial (ADR-0179). + auto operator<=>(const BodyTuning&) const = default; +}; +inline constexpr BodyTuning default_body_tuning{}; // Chosen constants, reviewable as a unit and selected by the genome's // development version. Declared model parameters, not balance. struct DevelopmentRules { @@ -68,6 +76,7 @@ struct Phenotype { // built inside the cell. At efficiency p per thousand they add p/4 of the structural mass // as membrane mass (so they are upkept twice, like membrane) and slow movement by p/2 per // thousand. Light itself is captured by the region's environment owner, never here. +// Version 7 keeps the organelle mass but drops the speed penalty: mass slows the cell. inline constexpr std::uint32_t photosynthesis_organelle_quarter = 4; inline constexpr std::uint32_t photosynthesis_speed_halving = 2; // A proposal. It reserves nothing, pays for nothing and commits nothing; the @@ -85,12 +94,14 @@ rules_for(std::uint32_t development_version); // Pure: a phenotype is recomputed from retained inputs rather than cached, so a // cached copy can always be checked against a recomputation. [[nodiscard]] std::expected -derive_phenotype(const Genome& genome, GrowthState growth, EnvironmentState environment); +derive_phenotype(const Genome& genome, GrowthState growth, EnvironmentState environment, + const BodyTuning& tuning = default_body_tuning); // Advances realized growth toward the target by at most the rule step, reduced to // the largest step the available mass and energy admit. This bounds by total mass // only: it cannot see which elements a step consumes, so it is a proposal and not // an affordability authority. `metabolic_budget` decides that, per channel, and // can only narrow this further. Never repairs damage. [[nodiscard]] std::expected -plan_development(const Genome& genome, GrowthState growth, EnvironmentState environment); +plan_development(const Genome& genome, GrowthState growth, EnvironmentState environment, + const BodyTuning& tuning = default_body_tuning); } // namespace evo::biology diff --git a/include/evo/biology/founder.hpp b/include/evo/biology/founder.hpp index 3d1e62c..e13536a 100644 --- a/include/evo/biology/founder.hpp +++ b/include/evo/biology/founder.hpp @@ -40,6 +40,10 @@ inline constexpr FounderVersion primitive_founder_v4{genome_schema_v5, 5}; // Founder 5 serves rules 12 (Cellular_Region_Movement_Plan.md): founder 4 with the founder // movement arrangement (every index at the 100-point reference). inline constexpr FounderVersion primitive_founder_v5{genome_schema_v6, 6}; +// Founder 6 serves rules 13 (ADR-0179): founder 5 on genome schema 7 and development 7, with +// the membrane in nanometres (1,000 nm, the same 1 um) and the founder's cellular traits. +inline constexpr FounderVersion primitive_founder_v6{genome_schema_v7, 7}; +inline constexpr std::uint32_t founder_membrane_nanometres = 1'000; inline constexpr std::uint32_t primitive_founder_development_version = primitive_founder_v1.development_version; // The founder genome under a world-scoped identity and its origin event. Fails only on diff --git a/include/evo/biology/genome.hpp b/include/evo/biology/genome.hpp index ffdbd72..697f1bd 100644 --- a/include/evo/biology/genome.hpp +++ b/include/evo/biology/genome.hpp @@ -32,7 +32,8 @@ enum class Unit : std::uint8_t { femtograms_per_tick, // Schema 1: per 50 ms tick. millikelvin, micrometres_per_second, // Schema 2 (ADR-0099). - femtograms_per_second // Schema 2 (ADR-0099). + femtograms_per_second, // Schema 2 (ADR-0099). + nanometres // Schema 7 membrane (ADR-0179). }; struct CapabilitySpec { Unit unit; @@ -88,6 +89,21 @@ inline constexpr std::array capability_schema_ 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 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. @@ -113,6 +129,24 @@ 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 collection{}; // E, C, N, P, O. + std::array burn{}; // C, N, P, O. + std::uint16_t engulf_capacity{}, kill_absorption{}, litter{}; + std::array 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{}; @@ -126,6 +160,8 @@ struct GenomeDraft { std::uint32_t schema_version{1}; // Required from schema 6 and refused before it. std::optional movement_arrangement{}; + // Required from schema 7 and refused before it. + std::optional cellular_traits{}; }; enum class GenomeError : std::uint8_t { unsupported_version, @@ -153,15 +189,19 @@ 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_v6; -// The capability table of a supported schema (1 to 6); nullptr otherwise. +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* 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; @@ -205,6 +245,10 @@ class Genome { [[nodiscard]] const std::optional& movement_arrangement() const noexcept { return movement_arrangement_; } + // Present exactly from schema 7. + [[nodiscard]] const std::optional& cellular_traits() const noexcept { + return cellular_traits_; + } [[nodiscard]] bool carries(Capability kind) const noexcept; [[nodiscard]] std::expected parameter(Capability kind) const noexcept; @@ -230,6 +274,7 @@ class Genome { std::vector parents_; std::vector capabilities_; std::optional movement_arrangement_; + std::optional cellular_traits_; std::uint64_t digest_{}; }; } // namespace evo::biology diff --git a/include/evo/biology/metabolism.hpp b/include/evo/biology/metabolism.hpp index fbf5f64..07b8f92 100644 --- a/include/evo/biology/metabolism.hpp +++ b/include/evo/biology/metabolism.hpp @@ -8,6 +8,9 @@ namespace evo::biology { // Development version 4 (rules 7, ADR-0137): a bud is always this share of the parent's body. inline constexpr std::uint32_t bud_body_per_thousand_v4 = 500; +// Development version 7 (ADR-0179): the reproduction value is the bud's share of the body again, +// and the construction discount stays at the founder's level (500 per thousand: a quarter off). +inline constexpr std::uint32_t primitive_bud_skill_v7 = 500; // Shares in thousandths over the ledger's C/N/P/other channels, summing to 1000. // The last channel absorbs the rounding remainder, so a split conserves mass. using Composition = std::array; @@ -59,7 +62,8 @@ metabolic_rules_for(std::uint32_t development_version); // its versioned cadence. The default preserves earlier budget results exactly. [[nodiscard]] std::expected metabolic_budget(const Genome& genome, GrowthState growth, EnvironmentState environment, - std::uint32_t max_growth_per_thousand = 1000); + std::uint32_t max_growth_per_thousand = 1000, + const BodyTuning& tuning = default_body_tuning); // An analysis, not a rule (ADR-0098 decision 7): whole seconds until feeding funds the // budget's reproduction investment, channel by channel and in energy, when the organism // takes in its full intake limit for `feeding_per_thousand` of the time and pays its diff --git a/src/biology/development.cpp b/src/biology/development.cpp index b157ad7..2391628 100644 --- a/src/biology/development.cpp +++ b/src/biology/development.cpp @@ -72,14 +72,18 @@ std::expected rules_for(std::uint32_t develo // Version 5 (ADR-0175) keeps them too; it adds photosynthetic organelles below. // Version 6 (rules 12) keeps version 5's body; handling comes from the genome's movement // arrangement (derive_cellular_handling). - if (development_version >= 3 && development_version <= 6) { + // Version 7 (rules 13, ADR-0179) keeps the constants; the genome's cellular traits set + // density, recipe and upkeep efficiency, and the membrane is in nanometres. + if (development_version >= 3 && development_version <= 7) { return DevelopmentRules{1000, 1200, 10, 15, 13, 60'000, 293'150, 50}; } return std::unexpected{DevelopmentError::unsupported_rules}; } -std::expected -derive_phenotype(const Genome& genome, GrowthState growth, EnvironmentState environment) { +std::expected derive_phenotype(const Genome& genome, + GrowthState growth, + EnvironmentState environment, + const BodyTuning& tuning) { if (growth.realized_per_thousand > thousand || growth.damage_per_thousand > thousand) { return std::unexpected{DevelopmentError::invalid_growth}; } @@ -87,13 +91,23 @@ derive_phenotype(const Genome& genome, GrowthState growth, EnvironmentState envi if (!rules) { return std::unexpected{rules.error()}; } + // Version 7 reads the schema-7 traits; a version-7 genome without them is unsupported. + const bool v7 = genome.development_version() >= 7; + const auto& traits = genome.cellular_traits(); + if (v7 != traits.has_value() || tuning.essential_upkeep_per_thousand > thousand) { + return std::unexpected{DevelopmentError::unsupported_rules}; + } Phenotype phenotype; // target_size, membrane and energy_conversion are required by schema 1. const auto target = *genome.parameter(Capability::target_size); const auto built = scaled(target, growth.realized_per_thousand); phenotype.structural_mass = scaled(built, thousand - growth.damage_per_thousand); - phenotype.extent = static_cast( - integer_cube_root(phenotype.structural_mass / rules->structure_density)); + // Version 7: density scales how tightly that mass packs, so a denser body is smaller. + const auto density = + v7 ? std::max(1, rules->structure_density * traits->density / 100) + : rules->structure_density; + phenotype.extent = + static_cast(integer_cube_root(phenotype.structural_mass / density)); std::uint64_t face{}; if (!multiply(face, phenotype.extent, phenotype.extent) || !multiply(phenotype.surface, face, 6)) { @@ -104,6 +118,10 @@ derive_phenotype(const Genome& genome, GrowthState growth, EnvironmentState envi !multiply(phenotype.membrane_mass, shell, rules->membrane_density)) { return std::unexpected{DevelopmentError::overflow}; } + // Version 7 holds the membrane in nanometres: the same mass at 1,000 nm as at 1 um. + if (v7) { + phenotype.membrane_mass /= thousand; + } const auto photosynthesis = genome.development_version() >= 5 && genome.carries(Capability::photosynthesis) ? *genome.parameter(Capability::photosynthesis) @@ -122,6 +140,15 @@ derive_phenotype(const Genome& genome, GrowthState growth, EnvironmentState envi scaled(phenotype.membrane_mass, rules->membrane_upkeep_per_kilo))) { return std::unexpected{DevelopmentError::overflow}; } + // Version 7: an essential share no trait reduces, plus the rest over upkeep efficiency. + if (v7) { + const auto essential = scaled(phenotype.maintenance, tuning.essential_upkeep_per_thousand); + std::uint64_t adjustable{}; + if (!multiply(adjustable, phenotype.maintenance - essential, 100)) { + return std::unexpected{DevelopmentError::overflow}; + } + phenotype.maintenance = essential + adjustable / traits->upkeep_efficiency; + } phenotype.conversion_per_thousand = static_cast(*genome.parameter(Capability::energy_conversion)); // A capability the genome does not carry yields nothing, whatever the size. @@ -137,8 +164,9 @@ derive_phenotype(const Genome& genome, GrowthState growth, EnvironmentState envi if (!speed || *speed > std::numeric_limits::max()) { return std::unexpected{DevelopmentError::overflow}; } - // Organelles slow the cell: p/2 per thousand off its speed (version 5). - const auto kept = thousand - photosynthesis / photosynthesis_speed_halving; + // Organelles slow the cell: p/2 per thousand off its speed (versions 5 and 6). Version 7 + // drops this: organelle mass slows the cell through its handling instead. + const auto kept = v7 ? thousand : thousand - photosynthesis / photosynthesis_speed_halving; phenotype.movement_speed = static_cast(scaled(*speed, kept)); } if (genome.carries(Capability::ingestion)) { @@ -159,9 +187,11 @@ derive_phenotype(const Genome& genome, GrowthState growth, EnvironmentState envi return phenotype; } -std::expected -plan_development(const Genome& genome, GrowthState growth, EnvironmentState environment) { - const auto current = derive_phenotype(genome, growth, environment); +std::expected plan_development(const Genome& genome, + GrowthState growth, + EnvironmentState environment, + const BodyTuning& tuning) { + const auto current = derive_phenotype(genome, growth, environment, tuning); if (!current) { return std::unexpected{current.error()}; } @@ -179,7 +209,7 @@ plan_development(const Genome& genome, GrowthState growth, EnvironmentState envi for (std::uint32_t attempt = step; attempt > 0; --attempt) { auto proposed = growth; proposed.realized_per_thousand += attempt; - const auto grown = derive_phenotype(genome, proposed, environment); + const auto grown = derive_phenotype(genome, proposed, environment, tuning); if (!grown) { return std::unexpected{grown.error()}; } diff --git a/src/biology/founder.cpp b/src/biology/founder.cpp index 2005840..0f486bc 100644 --- a/src/biology/founder.cpp +++ b/src/biology/founder.cpp @@ -11,14 +11,18 @@ std::expected primitive_founder(std::uint64_t id, std::uint draft.id = id; draft.origin_event = origin_event; draft.development_version = version.development_version; + const auto membrane = + version.genome_schema >= genome_schema_v7 ? founder_membrane_nanometres : p.membrane; draft.capabilities = {{Capability::energy_conversion, p.energy_conversion, 0}, - {Capability::membrane, p.membrane, 0}, + {Capability::membrane, membrane, 0}, {Capability::movement, p.movement, 0}, {Capability::ingestion, p.ingestion, 0}, {Capability::reproduction, p.reproduction, 0}, {Capability::target_size, p.target_size, 0}}; if (version.genome_schema >= genome_schema_v6) draft.movement_arrangement = founder_movement_arrangement; + if (version.genome_schema >= genome_schema_v7) + draft.cellular_traits = founder_cellular_traits; return Genome::create(draft); } diff --git a/src/biology/genome.cpp b/src/biology/genome.cpp index df31e55..fb2c511 100644 --- a/src/biology/genome.cpp +++ b/src/biology/genome.cpp @@ -17,9 +17,56 @@ constexpr std::size_t checksum_size = 8; constexpr std::size_t arrangement_count = 9; constexpr std::size_t arrangement_size = arrangement_count * 2; +// Schema 7: twenty 16-bit cellular traits after the arrangement. +constexpr std::size_t trait_count = 20; +constexpr std::size_t traits_size = trait_count * 2; + bool has_arrangement(std::uint32_t schema) noexcept { return schema >= genome_schema_v6; } +bool has_traits(std::uint32_t schema) noexcept { + return schema >= genome_schema_v7; +} + +std::array trait_values(const CellularTraits& t) noexcept { + return {t.density, t.upkeep_efficiency, t.growth_speed, t.collection[0], + t.collection[1], t.collection[2], t.collection[3], t.collection[4], + t.burn[0], t.burn[1], t.burn[2], t.burn[3], + t.engulf_capacity, t.kill_absorption, t.litter, t.recipe[0], + t.recipe[1], t.recipe[2], t.recipe[3], t.adhesion}; +} +CellularTraits traits_from(const std::array& v) noexcept { + return {v[0], v[1], v[2], {v[3], v[4], v[5], v[6], v[7]}, {v[8], v[9], v[10], v[11]}, + v[12], v[13], v[14], {v[15], v[16], v[17], v[18]}, v[19]}; +} +// Required from schema 7 and refused before it. Every index is 1 to 1,000 points; litter +// is bounded; each recipe share is at least 1 and they sum to 1000; adhesion is a flag. +bool valid_traits(std::uint32_t schema, const std::optional& traits) noexcept { + if (!has_traits(schema)) + return !traits.has_value(); + if (!traits) + return false; + const auto& t = *traits; + const auto index = [](std::uint16_t value) { return value >= 1 && value <= max_trait_points; }; + if (!index(t.density) || !index(t.upkeep_efficiency) || !index(t.growth_speed) || + !index(t.engulf_capacity) || !index(t.kill_absorption)) + return false; + for (const auto value : t.collection) + if (!index(value)) + return false; + for (const auto value : t.burn) + if (!index(value)) + return false; + if (t.litter > max_litter || t.adhesion > 1) + return false; + std::uint32_t recipe{}; + for (const auto share : t.recipe) { + if (share == 0) + return false; + recipe += share; + } + return recipe == 1000; +} std::array indices(const MovementArrangement& a) noexcept { return {a.output, a.efficiency, a.burst, a.steering, a.streamlining, @@ -204,6 +251,9 @@ std::vector Genome::body() const { if (movement_arrangement_) for (const auto value : indices(*movement_arrangement_)) append(bytes, value, 2); + if (cellular_traits_) + for (const auto value : trait_values(*cellular_traits_)) + append(bytes, value, 2); return bytes; } @@ -226,7 +276,8 @@ std::expected Genome::create(const GenomeDraft& draft) { !valid) { return std::unexpected{valid.error()}; } - if (!valid_arrangement(draft.schema_version, draft.movement_arrangement)) { + if (!valid_arrangement(draft.schema_version, draft.movement_arrangement) || + !valid_traits(draft.schema_version, draft.cellular_traits)) { return std::unexpected{GenomeError::parameter_out_of_range}; } Genome genome; @@ -237,6 +288,7 @@ std::expected Genome::create(const GenomeDraft& draft) { genome.parents_ = draft.parents; genome.capabilities_ = std::move(ordered); genome.movement_arrangement_ = draft.movement_arrangement; + genome.cellular_traits_ = draft.cellular_traits; genome.digest_ = digest_of(genome.body()); return genome; } @@ -278,8 +330,9 @@ std::expected Genome::decode(std::span } // An exact length match rejects trailing bytes rather than ignoring them. const auto arrangement_bytes = has_arrangement(schema) ? arrangement_size : 0; + const auto trait_bytes = has_traits(schema) ? traits_size : 0; const auto expected = counted + static_cast(record_count) * record_size + - arrangement_bytes + digest_size + checksum_size; + arrangement_bytes + trait_bytes + digest_size + checksum_size; if (bytes.size() != expected) { return std::unexpected{GenomeError::invalid_encoding}; } @@ -306,11 +359,18 @@ std::expected Genome::decode(std::span arrangement = MovementArrangement{read[0], read[1], read[2], read[3], read[4], read[5], read[6], read[7], read[8]}; } + std::optional traits; + if (has_traits(schema)) { + std::array read{}; + for (auto& value : read) + value = static_cast(reader.read(2)); + traits = traits_from(read); + } if (const auto valid = validate(schema, id, origin, development, parents, capabilities); !valid) { return std::unexpected{valid.error()}; } - if (!valid_arrangement(schema, arrangement)) { + if (!valid_arrangement(schema, arrangement) || !valid_traits(schema, traits)) { return std::unexpected{GenomeError::parameter_out_of_range}; } Genome genome; @@ -321,6 +381,7 @@ std::expected Genome::decode(std::span genome.parents_ = std::move(parents); genome.capabilities_ = std::move(capabilities); genome.movement_arrangement_ = arrangement; + genome.cellular_traits_ = traits; // The body must reproduce exactly, so a rewritten record is rejected. const auto digest = digest_of(genome.body()); if (digest != reader.read(digest_size)) { diff --git a/src/biology/metabolism.cpp b/src/biology/metabolism.cpp index b957aa1..b4a62d3 100644 --- a/src/biology/metabolism.cpp +++ b/src/biology/metabolism.cpp @@ -109,8 +109,9 @@ metabolic_rules_for(std::uint32_t development_version) { return MetabolicRules{structure_shares, membrane_shares, 20, 50'000}; } // Version 2: per second; movement at 1000 um/s costs 50 pJ/s per 1000 fg (tunable). - // Versions 3 to 6: the same metabolism. - if (development_version >= 2 && development_version <= 6) { + // Versions 3 to 7: the same metabolism. Version 7 reads the body's structure recipe from + // the genome's cellular traits instead (ADR-0179); intake keeps these founder shares. + if (development_version >= 2 && development_version <= 7) { return MetabolicRules{structure_shares, membrane_shares, 50, 50'000}; } return std::unexpected{DevelopmentError::unsupported_rules}; @@ -118,8 +119,8 @@ metabolic_rules_for(std::uint32_t development_version) { std::expected metabolic_budget(const Genome& genome, GrowthState growth, EnvironmentState environment, - std::uint32_t max_growth_per_thousand) { - const auto phenotype = derive_phenotype(genome, growth, environment); + std::uint32_t max_growth_per_thousand, const BodyTuning& tuning) { + const auto phenotype = derive_phenotype(genome, growth, environment, tuning); if (!phenotype) { return std::unexpected{phenotype.error()}; } @@ -131,12 +132,17 @@ metabolic_budget(const Genome& genome, GrowthState growth, EnvironmentState envi if (!shape) { return std::unexpected{shape.error()}; } + // The heritable recipe (version 7) or the version's fixed structure shares. + const auto& traits = genome.cellular_traits(); + const Composition structure = traits ? Composition{traits->recipe[0], traits->recipe[1], + traits->recipe[2], traits->recipe[3]} + : rules->structure; MetabolicBudget budget; budget.phenotype = *phenotype; budget.rate_microseconds = development_rate_microseconds(genome.development_version()); budget.assimilation_per_thousand = static_cast(phenotype->conversion_per_thousand); - budget.body = compose(phenotype->structural_mass, rules->structure); + budget.body = compose(phenotype->structural_mass, structure); if (!combine(budget.body, compose(phenotype->membrane_mass, rules->membrane))) { return std::unexpected{DevelopmentError::overflow}; } @@ -155,7 +161,7 @@ metabolic_budget(const Genome& genome, GrowthState growth, EnvironmentState envi budget.intake_limit = compose(phenotype->ingestion_rate, rules->structure); budget.intake_limit.energy.value = scaled(phenotype->ingestion_rate, rules->intake_energy_per_kilogram); - const auto plan = plan_development(genome, growth, environment); + const auto plan = plan_development(genome, growth, environment, tuning); if (!plan) { return std::unexpected{plan.error()}; } @@ -169,7 +175,7 @@ metabolic_budget(const Genome& genome, GrowthState growth, EnvironmentState envi --attempt) { const GrowthState candidate{growth.realized_per_thousand + attempt, growth.damage_per_thousand}; - const auto grown = derive_phenotype(genome, candidate, environment); + const auto grown = derive_phenotype(genome, candidate, environment, tuning); if (!grown) { return std::unexpected{grown.error()}; } @@ -178,7 +184,7 @@ metabolic_budget(const Genome& genome, GrowthState growth, EnvironmentState envi // body already carries, and the budget could not then reproduce the // derived body channel by channel. Each compartment is still composed // separately: the two differ in what they cost. - auto reached = compose(grown->structural_mass, rules->structure); + auto reached = compose(grown->structural_mass, structure); if (!combine(reached, compose(grown->membrane_mass, rules->membrane))) { return std::unexpected{DevelopmentError::overflow}; } @@ -209,15 +215,20 @@ metabolic_budget(const Genome& genome, GrowthState growth, EnvironmentState envi // Version 4 (ADR-0137): a bud is always half the body, and reproduction is skill: // it takes up to half off the bud's construction energy (value / 2000). Matter is // never discounted (ADR-0118). Earlier versions invest `value` per thousand. - const bool skill = genome.development_version() >= 4; // Versions 4 to 6. - const auto fraction = skill ? bud_body_per_thousand_v4 : value; + // Version 7 (ADR-0179): `value` is again the bud's share of the body, and keeps the + // skill discount at the founder's 500. + const bool skill = genome.development_version() >= 4; + const bool share = genome.development_version() >= 7; + const auto fraction = share || !skill ? value : bud_body_per_thousand_v4; // A parent divides the body it has, so the investment keeps its composition. budget.reproduction_investment = fraction_of(budget.body, fraction); // Offspring tissue is priced at what the parent's own tissue costs. const auto construction = scaled(matter_total(budget.reproduction_investment), shape->construction_per_kilogram); // Discount = construction * skill / 2000, split to stay within 64 bits. - const std::uint64_t level = std::min(value, 1000); + // Version 7 fixes the discount at the founder's level, so bigger buds are not cheaper. + const std::uint64_t level = + share ? primitive_bud_skill_v7 : std::min(value, 1000); const auto discount = construction / 2000 * level + construction % 2000 * level / 2000; budget.reproduction_investment.energy.value = skill ? construction - discount : construction; diff --git a/src/biology/mutation.cpp b/src/biology/mutation.cpp index a72a9d7..2f99d2e 100644 --- a/src/biology/mutation.cpp +++ b/src/biology/mutation.cpp @@ -39,7 +39,7 @@ constexpr std::array catalog = [] { constexpr std::uint32_t adjustment_step(const std::array& table, Capability kind, std::uint32_t schema, std::uint32_t current) noexcept { - if (schema == genome_schema_v4 || schema == genome_schema_v5 || schema == genome_schema_v6) + if (schema >= genome_schema_v4) return current / 10 == 0 ? 1 : current / 10; const auto& limits = table[static_cast(kind)]; const auto span = limits.maximum - limits.minimum; @@ -104,6 +104,7 @@ std::optional apply(const Genome& genome, const MutationOperator& e draft.parents = {genome.id()}; draft.capabilities.assign(genome.capabilities().begin(), genome.capabilities().end()); draft.movement_arrangement = genome.movement_arrangement(); + draft.cellular_traits = genome.cellular_traits(); const auto& table = *capability_schema_for(genome.schema_version()); const auto& limits = table[static_cast(entry.target)]; const auto carried = diff --git a/tests/development_tests.cpp b/tests/development_tests.cpp index 8931903..898bf45 100644 --- a/tests/development_tests.cpp +++ b/tests/development_tests.cpp @@ -174,15 +174,20 @@ void rule_versions_and_invalid_growth() { v3->structure_density == v2->structure_density && v3->maintenance_per_kilogram == v2->maintenance_per_kilogram && development_rate_microseconds(3) == development_rate_microseconds(2)); - for (const auto version : {0U, 7U, 99U}) { + for (const auto version : {0U, 8U, 99U}) { const auto missing = rules_for(version); CHECK(!missing && missing.error() == DevelopmentError::unsupported_rules); } auto source = draft(); - source.development_version = 7; // Version 6 exists since rules 12. + source.development_version = 8; // Version 7 exists since rules 13. const auto future = build(source); const auto derived = derive_phenotype(future, {1000, 0}, temperate); CHECK(!derived && derived.error() == DevelopmentError::unsupported_rules); + // Version 7 reads the schema-7 traits; a genome without them fails closed. + auto untraited = draft(); + untraited.development_version = 7; + const auto bare = derive_phenotype(build(untraited), {1000, 0}, temperate); + CHECK(!bare && bare.error() == DevelopmentError::unsupported_rules); const auto planned = plan_development(future, {0, 0}, temperate); CHECK(!planned && planned.error() == DevelopmentError::unsupported_rules); const auto genome = build(draft()); @@ -312,6 +317,9 @@ int main() try { extreme_sizes_are_exact(); std::cout << "PASS development rules, phenotype derivation and bounded plans\n"; return 0; +} catch (const std::exception& e) { + std::cerr << e.what() << '\n'; + return 1; } catch (...) { return 1; } diff --git a/tests/genome_tests.cpp b/tests/genome_tests.cpp index e9965aa..fc94b3e 100644 --- a/tests/genome_tests.cpp +++ b/tests/genome_tests.cpp @@ -362,10 +362,10 @@ void schema_v2_rates() { v1.capabilities[4].parameter = 1'000'001; reject(v1, GenomeError::parameter_out_of_range); auto unknown = founder(); - unknown.schema_version = 7; // Schema 6 exists since rules 12. + unknown.schema_version = 8; // Schema 7 exists since rules 13. reject(unknown, GenomeError::unsupported_version); auto bytes = Genome::create(founder())->encode(); - bytes[8] = 7; // Schema field; decode refuses before the checksum matters. + bytes[8] = 8; // Schema field; decode refuses before the checksum matters. CHECK(!Genome::decode(bytes) && Genome::decode(bytes).error() == GenomeError::unsupported_version); } @@ -443,6 +443,66 @@ void schema_v6_movement_arrangement() { early.movement_arrangement = founder_movement_arrangement; reject(early, GenomeError::parameter_out_of_range); } + +void schema_v7_cellular_traits() { + auto draft = founder(); + draft.schema_version = genome_schema_v7; + draft.development_version = 7; + draft.movement_arrangement = founder_movement_arrangement; + reject(draft, GenomeError::parameter_out_of_range); // Missing traits. + draft.cellular_traits = founder_cellular_traits; + const auto genome = Genome::create(draft); + CHECK(genome && genome->cellular_traits() == founder_cellular_traits); + const auto bytes = genome->encode(); + const auto decoded = Genome::decode(bytes); + CHECK(decoded && *decoded == *genome); + CHECK(bytes.size() == 36 + 4 + 3 * 9 + 18 + 40 + 16); + // The largest record still fits the bound: one parent and all eight capabilities. + CHECK(36 + 8 + 4 + 8 * 9 + 18 + 40 + 16 <= max_genome_bytes); + + auto changed = draft; + changed.cellular_traits->litter = 25; + const auto other = Genome::create(changed); + CHECK(other && other->content_digest() != genome->content_digest()); + CHECK(Genome::decode(other->encode()) == other); + + auto tampered = bytes; + tampered[36 + 4 + 3 * 9 + 18] ^= 1U; // First trait byte; the checksum catches it. + CHECK(!Genome::decode(tampered)); + auto truncated = bytes; + truncated.erase(truncated.end() - 18, truncated.end() - 16); + CHECK(!Genome::decode(truncated)); + + const auto refuse = [&](auto edit) { + auto refused = draft; + edit(*refused.cellular_traits); + reject(refused, GenomeError::parameter_out_of_range); + }; + refuse([](CellularTraits& t) { t.density = 0; }); + refuse([](CellularTraits& t) { t.upkeep_efficiency = 1'001; }); + refuse([](CellularTraits& t) { t.collection[4] = 0; }); + refuse([](CellularTraits& t) { t.burn[0] = 1'001; }); + refuse([](CellularTraits& t) { t.litter = max_litter + 1; }); + refuse([](CellularTraits& t) { t.adhesion = 2; }); + refuse([](CellularTraits& t) { t.recipe = {500, 150, 50, 299}; }); // Sums to 999. + refuse([](CellularTraits& t) { t.recipe = {650, 300, 50, 0}; }); // A channel at zero. + auto shifted = draft; + shifted.cellular_traits->recipe = {480, 157, 57, 306}; // A thrifty-carbon body. + shifted.cellular_traits->adhesion = 1; + shifted.cellular_traits->litter = max_litter; + CHECK(Genome::create(shifted)); + + auto early = draft; + early.schema_version = genome_schema_v6; + early.development_version = 6; + reject(early, GenomeError::parameter_out_of_range); // Traits before schema 7. + // Schema 7 measures the membrane in nanometres: 10 mm is the bound, not 10 um. + auto thick = draft; + thick.capabilities[1].parameter = 10'000'000; + CHECK(Genome::create(thick)); + thick.capabilities[1].parameter = 10'000'001; + reject(thick, GenomeError::parameter_out_of_range); +} } // namespace int main() try { founders_and_ancestry(); @@ -455,6 +515,7 @@ int main() try { schema_v2_rates(); moves_preserve_validity(); schema_v6_movement_arrangement(); + schema_v7_cellular_traits(); std::cout << "PASS genome schema, ancestry, development graph and canonical encoding\n"; return 0; } catch (const std::exception& e) { diff --git a/tests/metabolism_tests.cpp b/tests/metabolism_tests.cpp index 6cd400e..c2c1964 100644 --- a/tests/metabolism_tests.cpp +++ b/tests/metabolism_tests.cpp @@ -385,12 +385,12 @@ void rules_fail_closed() { const auto m3 = metabolic_rules_for(3); CHECK(m2 && m3 && m3->movement_per_kilogram == m2->movement_per_kilogram && m3->intake_energy_per_kilogram == m2->intake_energy_per_kilogram); - for (const auto version : {0U, 7U, 99U}) { + for (const auto version : {0U, 8U, 99U}) { const auto missing = metabolic_rules_for(version); CHECK(!missing && missing.error() == DevelopmentError::unsupported_rules); } auto source = draft(); - source.development_version = 7; + source.development_version = 8; // Version 7 exists since rules 13. const auto future = build(source); const auto refused = metabolic_budget(future, {1000, 0}, temperate); CHECK(!refused && refused.error() == DevelopmentError::unsupported_rules); @@ -570,6 +570,108 @@ void founder_v3_buds_more_cheaply_with_skill() { CHECK(with_skill(1).energy.value > with_skill(1000).energy.value); } +// ADR-0179 (development 7, founder 6): the founder's traits build founder 5's cell; density, +// upkeep efficiency, the recipe and the bud share change the body as the plans say. +Genome founder6_with(auto edit, std::uint32_t photosynthesis = 0) { + const auto base = primitive_founder(1, 1, primitive_founder_v6); + CHECK(base.has_value()); + GenomeDraft draft; + draft.schema_version = genome_schema_v7; + draft.id = 2; + draft.origin_event = 1; + draft.development_version = 7; + for (const auto& record : base->capabilities()) + draft.capabilities.push_back(record); + if (photosynthesis != 0) + draft.capabilities.push_back({Capability::photosynthesis, photosynthesis, 0}); + draft.movement_arrangement = base->movement_arrangement(); + draft.cellular_traits = base->cellular_traits(); + edit(draft); + const auto genome = Genome::create(draft); + CHECK(genome.has_value()); + return genome.value_or(*base); +} +void founder_v6_builds_founder_v5s_cell() { + const auto v5 = primitive_founder(1, 1, primitive_founder_v5); + const auto v6 = primitive_founder(1, 1, primitive_founder_v6); + CHECK(v5 && v6 && v6->schema_version() == 7 && v6->development_version() == 7); + CHECK(*v6->parameter(Capability::membrane) == 1'000); // Nanometres: the same 1 um. + const auto b5 = metabolic_budget(*v5, {1000, 0}, temperate); + const auto b6 = metabolic_budget(*v6, {1000, 0}, temperate); + CHECK(b5 && b6 && b5->body == b6->body && b5->phenotype == b6->phenotype); + CHECK(b5->intake_limit == b6->intake_limit && b5->movement == b6->movement); + CHECK(b5->reproduction_investment == b6->reproduction_investment); + const auto e5 = primitive_endowment(primitive_founder_v5); + const auto e6 = primitive_endowment(primitive_founder_v6); + CHECK(e5 && e6 && *e5 == *e6); +} +void development_v7_traits_shape_the_body() { + const auto founder = founder6_with([](GenomeDraft&) {}); + const auto base = metabolic_budget(founder, {1000, 0}, temperate); + CHECK(base.has_value()); + const auto budget_of = [&](const Genome& genome, BodyTuning tuning = {}) { + const auto budget = metabolic_budget(genome, {1000, 0}, temperate, 1000, tuning); + CHECK(budget.has_value()); + return budget.value_or(*base); + }; + // Denser tissue packs the same structure smaller: less surface, membrane and upkeep. + const auto dense = + budget_of(founder6_with([](GenomeDraft& d) { d.cellular_traits->density = 125; })); + CHECK(dense.phenotype.structural_mass == base->phenotype.structural_mass); + CHECK(dense.phenotype.extent < base->phenotype.extent); + CHECK(dense.phenotype.membrane_mass < base->phenotype.membrane_mass); + CHECK(dense.phenotype.maintenance < base->phenotype.maintenance); + const auto light = + budget_of(founder6_with([](GenomeDraft& d) { d.cellular_traits->density = 80; })); + CHECK(light.phenotype.extent > base->phenotype.extent && + light.phenotype.ingestion_rate > base->phenotype.ingestion_rate); + // Upkeep efficiency divides only the adjustable part: never below the essential floor. + const auto upkeep = base->phenotype.maintenance; + const auto efficient = [&](std::uint16_t value) { + return budget_of(founder6_with([value](GenomeDraft& d) { + d.cellular_traits->upkeep_efficiency = value; + })) + .phenotype.maintenance; + }; + CHECK(efficient(200) < upkeep && efficient(1'000) < efficient(200)); + CHECK(efficient(1'000) >= upkeep / 4 && efficient(50) > upkeep); + const auto all_essential = budget_of( + founder6_with([](GenomeDraft& d) { d.cellular_traits->upkeep_efficiency = 1'000; }), + BodyTuning{1000}); + CHECK(all_essential.phenotype.maintenance == upkeep); + // The recipe changes what the body is made of, not how much there is or how it feeds. + const auto thrifty = budget_of( + founder6_with([](GenomeDraft& d) { d.cellular_traits->recipe = {480, 157, 57, 306}; })); + CHECK(thrifty.body.matter[0] < base->body.matter[0] && + thrifty.body.matter[1] > base->body.matter[1]); + std::uint64_t before{}, after{}; + for (std::size_t c = 0; c < 4; ++c) { + before += base->body.matter[c].value; + after += thrifty.body.matter[c].value; + } + CHECK(before == after && thrifty.intake_limit == base->intake_limit); + // Version 7 drops the producer speed penalty; the organelles still add mass. + const auto producer = budget_of(founder6_with([](GenomeDraft&) {}, 100)); + CHECK(producer.phenotype.movement_speed == base->phenotype.movement_speed); + CHECK(producer.phenotype.total_mass > base->phenotype.total_mass); + // The reproduction value is the bud's share of the body; the discount stays fixed. + const auto bigger = budget_of(founder6_with([](GenomeDraft& d) { + for (auto& record : d.capabilities) + if (record.kind == Capability::reproduction) + record.parameter = 600; + })); + std::uint64_t half{}, sixty{}; + for (std::size_t c = 0; c < 4; ++c) { + half += base->reproduction_investment.matter[c].value; + sixty += bigger.reproduction_investment.matter[c].value; + } + CHECK(sixty > half && sixty * 5 <= half * 6 + 8); + CHECK(bigger.reproduction_investment.energy.value * 5 <= + base->reproduction_investment.energy.value * 6 + 8); + // A version-7 genome needs its traits, and a bad tuning share fails closed. + CHECK(!metabolic_budget(founder, {1000, 0}, temperate, 1000, BodyTuning{1001})); +} + int main() try { hand_calculated_budget(); construction_composes_each_compartment(); @@ -588,8 +690,13 @@ int main() try { development_v2_and_the_first_bud(); founder_v2_builds_the_same_cell_for_less_energy(); founder_v3_buds_more_cheaply_with_skill(); + founder_v6_builds_founder_v5s_cell(); + development_v7_traits_shape_the_body(); std::cout << "PASS metabolic budgets, composition splits and genome-driven feeding\n"; return 0; +} catch (const std::exception& e) { + std::cerr << e.what() << '\n'; + return 1; } catch (...) { return 1; }