Add cellular mutation replay trace v3
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 2m9s
Foundation / checks (ecs) (push) Successful in 2m2s
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 (client) (push) Canceled after 3m11s
Foundation / checks (postgres) (push) Canceled after 1m3s
Foundation / checks (network) (push) Canceled after 1m9s

This commit is contained in:
dingo
2026-10-11 06:29:17 -04:00
parent a479ce2d95
commit d9bf60138e
14 changed files with 596 additions and 49 deletions
+78 -1
View File
@@ -953,6 +953,16 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
std::expected<biology::CellularRegionStepCandidate, CellularLivesError>
CellularLives::advance_tick(std::span<const CellularLifeIntent> intents) {
auto execution =
advance_tick_with_mutations(intents, std::span<const CellularMutationSelection>{});
if (!execution)
return std::unexpected{execution.error()};
return std::move(execution->step);
}
std::expected<CellularTickExecution, CellularLivesError>
CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> intents,
std::span<const CellularMutationSelection> selections) {
if (state_->config.tick == std::numeric_limits<std::uint64_t>::max())
return std::unexpected{CellularLivesError::invalid_tick};
if (intents.size() != state_->lives.size())
@@ -964,6 +974,33 @@ CellularLives::advance_tick(std::span<const CellularLifeIntent> intents) {
ordered[i].life_id != state_->lives[i].identity.life)
return std::unexpected{CellularLivesError::invalid_intents};
if (!selections.empty() && state_->format_version != cellular_lives_revision_format)
return std::unexpected{CellularLivesError::version};
for (std::size_t i = 0; i < selections.size(); ++i) {
const auto& selection = selections[i];
if (selection.life_id == 0 || selection.event_index == 0 || selection.offer_tick == 0 ||
selection.operator_id == 0 || selection.successor_id == 0 ||
(i != 0 && selections[i - 1].life_id >= selection.life_id))
return std::unexpected{CellularLivesError::invalid_intents};
const auto life = std::ranges::lower_bound(
state_->lives, selection.life_id, {},
[](const CellularOwnedLife& value) { return value.identity.life; });
if (life == state_->lives.end() || life->identity.life != selection.life_id ||
!life->mutation.offer || life->mutation.event_index != selection.event_index ||
life->mutation.offer_tick != selection.offer_tick ||
life->mutation.offer->parent != life->identity.genome ||
life->mutation.offer->context.event_index != selection.event_index ||
!std::ranges::any_of(life->mutation.offer->candidates,
[&](const auto& candidate) {
return candidate.operator_id == selection.operator_id;
}) ||
state_->archive.find(selection.successor_id))
return std::unexpected{CellularLivesError::mutation};
for (std::size_t j = 0; j < i; ++j)
if (selections[j].successor_id == selection.successor_id)
return std::unexpected{CellularLivesError::identity};
}
std::vector<biology::CellularLifeStepInput> inputs;
inputs.reserve(state_->lives.size());
for (std::size_t i = 0; i < state_->lives.size(); ++i) {
@@ -1060,6 +1097,46 @@ CellularLives::advance_tick(std::span<const CellularLifeIntent> intents) {
corpse_budget->phenotype.extent, dead.inventory});
next_lives.erase(found);
}
std::vector<CellularMutationExecution> mutation_results;
mutation_results.reserve(selections.size());
for (const auto& selection : selections) {
const auto found = std::ranges::lower_bound(
next_lives, selection.life_id, {},
[](const CellularOwnedLife& value) { return value.identity.life; });
if (found == next_lives.end() || found->identity.life != selection.life_id) {
mutation_results.push_back(
{selection.life_id, selection.successor_id, CellularMutationResult::life_died});
continue;
}
const auto parent = next_archive.find(found->identity.genome);
if (!parent || !found->mutation.offer)
return std::unexpected{CellularLivesError::mutation};
auto transition = biology::fund_mutation(
*parent, found->mutation.offer->context, *found->mutation.offer, selection.operator_id,
selection.successor_id, selection.successor_id, found->reserve);
if (!transition) {
if (transition.error() == biology::MutationError::insufficient_funding) {
mutation_results.push_back({selection.life_id, selection.successor_id,
CellularMutationResult::insufficient_funding});
continue;
}
return std::unexpected{CellularLivesError::mutation};
}
if (!next_archive.record_mutation(found->identity.organism, transition->successor,
{proposal->end_tick}))
return std::unexpected{CellularLivesError::archive};
if (transition->intake.energy.value >
std::numeric_limits<std::uint64_t>::max() - next_heat.energy.value)
return std::unexpected{CellularLivesError::overflow};
next_heat.energy.value += transition->intake.energy.value;
found->identity.genome = selection.successor_id;
found->body = transition->body;
found->reserve = transition->reserve;
found->mutation.offer.reset();
found->mutation.offer_tick = 0;
mutation_results.push_back(
{selection.life_id, selection.successor_id, CellularMutationResult::applied});
}
std::ranges::sort(next_corpses, {}, &CellularOwnedCorpse::organism);
if (proposal->heat_released.value >
std::numeric_limits<std::uint64_t>::max() - next_heat.energy.value)
@@ -1106,6 +1183,6 @@ CellularLives::advance_tick(std::span<const CellularLifeIntent> intents) {
}
state_.swap(next);
last_tick_feedback_ = std::move(feedback);
return std::move(*proposal);
return CellularTickExecution{std::move(*proposal), std::move(mutation_results)};
}
} // namespace evo::server