Add funded cellular mutation proposal
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (checks) (push) Failing after 43s
Foundation / checks (ecs) (push) Successful in 1m57s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 53s
Foundation / checks (client) (push) Canceled after 2m59s
Foundation / checks (network) (push) Canceled after 2m19s

This commit is contained in:
dingo
2026-10-11 02:42:53 -04:00
parent b7f3d73980
commit 84efa4bf14
6 changed files with 264 additions and 3 deletions
+68
View File
@@ -217,4 +217,72 @@ select_mutation(const Genome& genome, const MutationContext& context, const Muta
}
return *successor;
}
std::expected<MutationTransition, MutationError>
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) {
const auto successor =
select_mutation(genome, context, offered, chosen, successor_id, origin_event);
if (!successor) {
return std::unexpected{successor.error()};
}
const auto before = metabolic_budget(genome, context.growth, context.environment);
const auto after = metabolic_budget(*successor, context.growth, context.environment);
const auto rules = rules_for(genome.development_version());
if (!before || !after || !rules) {
return std::unexpected{MutationError::development};
}
MutationTransition transition{*successor, after->body, reserve, {}, {}};
std::uint64_t added_matter = 0;
for (std::size_t channel = 0; channel < transition.body.matter.size(); ++channel) {
const auto old_mass = before->body.matter[channel].value;
const auto new_mass = after->body.matter[channel].value;
if (new_mass > old_mass) {
const auto amount = new_mass - old_mass;
if (amount > std::numeric_limits<std::uint64_t>::max() - added_matter) {
return std::unexpected{MutationError::development};
}
added_matter += amount;
transition.intake.matter[channel].value = amount;
} else {
transition.release.matter[channel].value = old_mass - new_mass;
}
}
// Construction energy is charged for each channel's added material. The
// release from another channel cannot pay for construction or hide a missing
// nutrient; this matches channel-specific affordability in M3.3.
const auto per_thousand = rules->construction_per_kilogram;
const auto whole = added_matter / 1000;
const auto remainder = added_matter % 1000;
if ((whole != 0 && per_thousand > std::numeric_limits<std::uint64_t>::max() / whole) ||
(remainder != 0 && per_thousand > std::numeric_limits<std::uint64_t>::max() / remainder)) {
return std::unexpected{MutationError::development};
}
const auto whole_energy = whole * per_thousand;
const auto remainder_energy = remainder * per_thousand / 1000;
if (remainder_energy > std::numeric_limits<std::uint64_t>::max() - whole_energy) {
return std::unexpected{MutationError::development};
}
transition.intake.energy.value = whole_energy + remainder_energy;
if (reserve.energy.value < transition.intake.energy.value) {
return std::unexpected{MutationError::insufficient_funding};
}
transition.reserve.energy.value -= transition.intake.energy.value;
for (std::size_t channel = 0; channel < transition.body.matter.size(); ++channel) {
const auto available = reserve.matter[channel].value;
const auto required = transition.intake.matter[channel].value;
const auto returned = transition.release.matter[channel].value;
if (available < required) {
return std::unexpected{MutationError::insufficient_funding};
}
if (returned > std::numeric_limits<std::uint64_t>::max() - (available - required)) {
return std::unexpected{MutationError::development};
}
transition.reserve.matter[channel].value = available - required + returned;
}
return transition;
}
} // namespace evo::biology