#pragma once #include "evo/biology/metabolism.hpp" namespace evo::biology { enum class MutationEffect : std::uint8_t { adjust_up, adjust_down, acquire, shed }; // One entry of the static catalog. The ID is stable: it orders candidates and // names them across calls, and it carries no ranking. struct MutationOperator { std::uint16_t id; Capability target; MutationEffect effect; auto operator<=>(const MutationOperator&) const = default; }; // What the recorded context is. The draw reads this and the parent genome and // nothing else, so events elsewhere in a world cannot move a candidate set. struct MutationContext { std::uint64_t seed; std::uint32_t stream; // explicit stream identity, never a shared global std::uint64_t event_index; // a new event earns a new draw; a retry does not std::uint16_t candidate_count; GrowthState growth; EnvironmentState environment; auto operator<=>(const MutationContext&) const = default; }; // A proposal. It reserves nothing. Maintenance and mass are what ADR-0032 would // charge for the successor, not a price invented here and not a score. struct MutationCandidate { std::uint16_t operator_id; Capability target; MutationEffect effect; std::uint32_t parameter; // the successor's value; zero when shed std::uint64_t maintenance; std::uint64_t total_mass; auto operator<=>(const MutationCandidate&) const = default; }; // What was offered, and what it was drawn from. The record carries its own // context and parent because a candidate list cannot stand in for them: a small // catalog yields few distinct lists, so different contexts draw the same one // often, and comparing lists would accept a stale offer. struct MutationOffer { std::uint64_t parent; std::uint64_t parent_digest; MutationContext context; std::vector candidates; auto operator<=>(const MutationOffer&) const = default; }; // A fully funded genome change, detached from the organism until its owner // commits it. Body matter is recomputed from the successor; reserve includes // channel-specific material releases and pays all deficits plus construction // energy. A refusal never spends anything. struct MutationTransition { Genome successor; accounting::Inventory body; accounting::Inventory reserve; accounting::Inventory intake; accounting::Inventory release; auto operator<=>(const MutationTransition&) const = default; }; enum class MutationError : std::uint8_t { invalid_context, no_candidates, unknown_candidate, context_changed, altered_offer, invalid_successor, insufficient_funding, development }; inline constexpr std::size_t max_candidates = 8; // ADR-0175 trial value: an acquired photosynthesis starts at 10 % efficiency. inline constexpr std::uint32_t photosynthesis_acquired_start = 100; [[nodiscard]] std::span mutation_catalog() noexcept; // Identical inputs always yield identical candidates, sorted by operator ID. // The order is canonical and means nothing: nothing here is ranked. [[nodiscard]] std::expected mutation_candidates(const Genome& genome, const MutationContext& context); // Compares the offer's recorded context and parent against the current ones // before anything else, so a changed context is rejected rather than answered // with a different mutation. [[nodiscard]] std::expected select_mutation(const Genome& genome, const MutationContext& context, const MutationOffer& offered, std::uint16_t chosen, std::uint64_t successor_id, std::uint64_t origin_event); // Revalidates the offer, derives both bodies at the offer's recorded growth and // environment, and funds the exact channel transition from retained reserve. // Added matter in each channel incurs construction energy at that genome's // versioned construction rate. No state is changed by this proposal. [[nodiscard]] std::expected fund_mutation(const Genome& genome, const MutationContext& context, const MutationOffer& offered, std::uint16_t chosen, std::uint64_t successor_id, std::uint64_t origin_event, const accounting::Inventory& reserve); // ADR-0157: preserve and validate the original draw, but fund its selected genome // at the organism's current growth. No redraw or reset to the offer's old body. [[nodiscard]] std::expected fund_mutation_at_growth(const Genome& genome, const MutationContext& context, const MutationOffer& offered, std::uint16_t chosen, std::uint64_t successor_id, std::uint64_t origin_event, const accounting::Inventory& reserve, GrowthState current_growth); } // namespace evo::biology