Resolve cellular engulfment and detritus in the region step
Foundation / checks (ecs) (push) Successful in 2m17s
Foundation / checks (deploy) (push) Successful in 3m57s
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 6m13s
Foundation / checks (checks) (push) Canceled after 6m19s
Foundation / checks (tsan) (push) Canceled after 3m58s
Foundation / checks (client) (push) Canceled after 6m17s
Foundation / checks (network) (push) Canceled after 6m18s

ADR-0124 implements ADR-0098's predation and corpse rules for the
cellular trial. Under rules 3 the pure region step adds an engulf phase
after every life's step: in ascending life id, a predator takes the
lowest-id contacted survivor whose total mass, times 1.5 per micrometre
of its membrane, is at most the predator's. The prey's whole inventory
moves once; the predator keeps what its remaining per-tick intake allows
and the rest, like every starvation corpse, becomes detritus in the
patch at the dead cell's 1 mm cell. Corpse candidates report their cause.
Rules 2 is unchanged and ignores engulf intents.

Owner, protocol v11, persistence and recovery wiring follow in the next
11.3f steps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-13 18:47:48 -04:00
co-authored by Claude Opus 5.5
parent 4751b23857
commit 0fb67c3257
9 changed files with 502 additions and 9 deletions
+190 -3
View File
@@ -1,14 +1,59 @@
#include "evo/biology/cellular_region_step.hpp"
#include "evo/accounting/rates.hpp"
#include <algorithm>
#include <limits>
#include <utility>
namespace evo::biology {
namespace {
constexpr auto u64_max = std::numeric_limits<std::uint64_t>::max();
bool add(std::uint64_t& to, std::uint64_t amount) noexcept {
if (amount > u64_max - to)
return false;
to += amount;
return true;
}
bool add(accounting::Inventory& to, const accounting::Inventory& from) noexcept {
if (!add(to.energy.value, from.energy.value))
return false;
for (std::size_t channel = 0; channel < to.matter.size(); ++channel)
if (!add(to.matter[channel].value, from.matter[channel].value))
return false;
return true;
}
// a * ka >= b * kb without overflow: compares the exact 128-bit products.
bool product_at_least(std::uint64_t a, std::uint64_t ka, std::uint64_t b, std::uint64_t kb) {
const auto wide = [](std::uint64_t x, std::uint64_t y) {
const std::uint64_t x0 = x & 0xFFFFFFFFU, x1 = x >> 32U;
const std::uint64_t y0 = y & 0xFFFFFFFFU, y1 = y >> 32U;
const std::uint64_t p00 = x0 * y0, p01 = x0 * y1, p10 = x1 * y0, p11 = x1 * y1;
const std::uint64_t middle = (p00 >> 32U) + (p01 & 0xFFFFFFFFU) + (p10 & 0xFFFFFFFFU);
const std::uint64_t high = p11 + (p01 >> 32U) + (p10 >> 32U) + (middle >> 32U);
const std::uint64_t low = (middle << 32U) | (p00 & 0xFFFFFFFFU);
return std::pair{high, low};
};
return wide(a, ka) >= wide(b, kb);
}
CellularPatchAddress cell_of(CellularPosition position) noexcept {
return {static_cast<std::uint32_t>(position.x_um / cellular_patch_pitch_um *
cellular_patch_pitch_um),
static_cast<std::uint32_t>(position.y_um / cellular_patch_pitch_um *
cellular_patch_pitch_um)};
}
struct Deposit {
std::uint64_t life_id{}, source{};
CellularPosition position;
accounting::Inventory inventory;
};
} // namespace
std::expected<CellularRegionStepCandidate, CellularRegionStepError>
propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
std::span<const CellularLifeStepInput> lives,
const CellularHabitat& habitat) {
const CellularHabitat& habitat, CellularRegionRules rules) {
if (begin_tick == std::numeric_limits<std::uint64_t>::max())
return std::unexpected{CellularRegionStepError::invalid_tick};
if (lives.size() > max_cellular_tick_lives)
@@ -19,6 +64,9 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
for (const auto& life : lives) {
if (life.life_id == 0)
return std::unexpected{CellularRegionStepError::invalid_life_id};
if (rules.engulf_and_detritus && (life.membrane_um == 0 || life.source == 0 ||
(life.intent.engulf && life.intent.intake_target)))
return std::unexpected{CellularRegionStepError::invalid_intent};
ordered.push_back(&life);
}
std::ranges::sort(ordered, {}, [](const auto* life) { return life->life_id; });
@@ -26,13 +74,17 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
if (ordered[i - 1]->life_id == ordered[i]->life_id)
return std::unexpected{CellularRegionStepError::duplicate_life_id};
CellularRegionStepCandidate candidate{begin_tick, begin_tick + 1, {}, {}, {}, {}};
CellularRegionStepCandidate candidate{begin_tick, begin_tick + 1, {}, {}, {}, {}, {}};
candidate.lives.reserve(ordered.size());
candidate.starved.reserve(ordered.size());
std::vector<accounting::Inventory> patch_stock;
patch_stock.reserve(habitat.patches().size());
for (const auto& patch : habitat.patches())
patch_stock.push_back(patch.stock);
// Inputs of the lives still in candidate.lives, in the same order.
std::vector<const CellularLifeStepInput*> survivors;
survivors.reserve(ordered.size());
std::vector<Deposit> deposits;
for (const auto* life : ordered) {
if (life->body.energy.value != 0)
@@ -43,6 +95,7 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
auto intent = life->intent;
const auto target = intent.intake_target;
intent.intake_target.reset();
intent.engulf = false;
const auto step =
propose_cellular_step(begin_tick, area, life->position, life->extent_um, intent,
life->reserve, life->heat, life->budget, habitat);
@@ -59,6 +112,10 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
std::numeric_limits<std::uint64_t>::max() - candidate.heat_released.value)
return std::unexpected{CellularRegionStepError::overflow};
candidate.heat_released.value += life->heat.energy.value;
if (rules.engulf_and_detritus) {
deposits.push_back({life->life_id, life->source, life->position, corpse.inventory});
corpse.inventory = {};
}
candidate.starved.push_back(corpse);
continue;
}
@@ -87,7 +144,137 @@ propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
}
}
}
candidate.lives.push_back({life->life_id, result});
CellularLifeStepResult life_result{life->life_id, result};
if (life->intent.engulf)
life_result.engulf = rules.engulf_and_detritus ? CellularEngulfStatus::no_eligible_prey
: CellularEngulfStatus::unsupported;
candidate.lives.push_back(life_result);
survivors.push_back(life);
}
if (rules.engulf_and_detritus) {
// Engulf phase (ADR-0124): ascending predator id; each takes at most the lowest-id
// eligible survivor in contact. A taken life neither acts nor is taken again.
std::vector<bool> taken(candidate.lives.size(), false);
for (std::size_t p = 0; p < candidate.lives.size(); ++p) {
if (taken[p] || !survivors[p]->intent.engulf)
continue;
auto& predator = candidate.lives[p];
const auto* hunter = survivors[p];
for (std::size_t q = 0; q < candidate.lives.size(); ++q) {
if (q == p || taken[q])
continue;
const auto* prey = survivors[q];
const auto& prey_step = candidate.lives[q].candidate;
if (!cellular_footprints_contact(predator.candidate.position, hunter->extent_um,
prey_step.position, prey->extent_um) ||
!product_at_least(hunter->budget.phenotype.total_mass, 1'000,
prey->budget.phenotype.total_mass,
cellular_engulf_ratio_milli_per_membrane_um *
prey->membrane_um))
continue;
// The prey's whole inventory moves once: body matter and its post-step reserve.
accounting::Inventory whole = prey_step.reserve_after;
if (!add(whole, prey->body))
return std::unexpected{CellularRegionStepError::overflow};
if (!add(candidate.heat_released.value, prey_step.heat_after.energy.value))
return std::unexpected{CellularRegionStepError::overflow};
// Capacity: what is left of the predator's per-tick intake limit this tick.
const auto remaining = [&](std::uint64_t per_second,
std::uint64_t already) -> std::optional<std::uint64_t> {
const auto limit = accounting::per_tick(
per_second, cellular_motion_tick_microseconds, accounting::Rounding::down);
if (!limit)
return std::nullopt;
return *limit > already ? *limit - already : 0;
};
const auto acquired = predator.candidate.intake
? predator.candidate.intake->transfer.acquired
: accounting::Inventory{};
accounting::Inventory taken_in{};
const auto energy_room =
remaining(hunter->budget.intake_limit.energy.value, acquired.energy.value);
if (!energy_room)
return std::unexpected{CellularRegionStepError::overflow};
taken_in.energy.value = std::min(whole.energy.value, *energy_room);
for (std::size_t channel = 0; channel < whole.matter.size(); ++channel) {
const auto room = remaining(hunter->budget.intake_limit.matter[channel].value,
acquired.matter[channel].value);
if (!room)
return std::unexpected{CellularRegionStepError::overflow};
taken_in.matter[channel].value = std::min(whole.matter[channel].value, *room);
}
if (!add(predator.candidate.reserve_after, taken_in))
return std::unexpected{CellularRegionStepError::overflow};
accounting::Inventory detritus = whole;
detritus.energy.value -= taken_in.energy.value;
for (std::size_t channel = 0; channel < detritus.matter.size(); ++channel)
detritus.matter[channel].value -= taken_in.matter[channel].value;
deposits.push_back({prey->life_id, prey->source, prey_step.position, detritus});
CellularCorpseCandidate corpse{prey->life_id, prey_step.position, {}};
corpse.cause = CellularDeathCause::engulfed;
corpse.predator_id = hunter->life_id;
candidate.starved.push_back(corpse);
predator.engulf = CellularEngulfStatus::engulfed;
predator.prey_id = prey->life_id;
taken[q] = true;
break;
}
}
std::vector<CellularLifeStepResult> remaining_lives;
remaining_lives.reserve(candidate.lives.size());
for (std::size_t i = 0; i < candidate.lives.size(); ++i)
if (!taken[i])
remaining_lives.push_back(candidate.lives[i]);
candidate.lives = std::move(remaining_lives);
std::ranges::sort(candidate.starved, {}, &CellularCorpseCandidate::life_id);
// Detritus: each dead life's inventory goes to the patch at its cell, a new patch at
// an empty cell, or the nearest patch once the habitat is full.
std::ranges::sort(deposits, {}, &Deposit::life_id);
for (const auto& deposit : deposits) {
const auto address = cell_of(deposit.position);
if (const auto index = habitat.find(address)) {
if (!add(patch_stock[*index], deposit.inventory))
return std::unexpected{CellularRegionStepError::overflow};
continue;
}
const auto made = std::ranges::lower_bound(candidate.new_patches, address, {},
&CellularPatch::address);
if (made != candidate.new_patches.end() && made->address == address) {
if (!add(made->stock, deposit.inventory))
return std::unexpected{CellularRegionStepError::overflow};
continue;
}
if (habitat.patches().size() + candidate.new_patches.size() < max_cellular_patches) {
candidate.new_patches.insert(made, {address, deposit.source, deposit.inventory});
continue;
}
// Full habitat: nearest existing patch, lowest address on a tie.
const auto distance = [&](CellularPatchAddress other) {
const auto dx = static_cast<std::int64_t>(other.x_um) - address.x_um;
const auto dy = static_cast<std::int64_t>(other.y_um) - address.y_um;
return static_cast<std::uint64_t>(dx * dx) + static_cast<std::uint64_t>(dy * dy);
};
std::optional<std::pair<std::uint64_t, CellularPatchAddress>> best;
for (const auto& patch : habitat.patches())
if (!best || std::pair{distance(patch.address), patch.address} < *best)
best = std::pair{distance(patch.address), patch.address};
for (const auto& patch : candidate.new_patches)
if (!best || std::pair{distance(patch.address), patch.address} < *best)
best = std::pair{distance(patch.address), patch.address};
if (!best)
return std::unexpected{CellularRegionStepError::habitat};
if (const auto index = habitat.find(best->second)) {
if (!add(patch_stock[*index], deposit.inventory))
return std::unexpected{CellularRegionStepError::overflow};
} else {
const auto nearest = std::ranges::lower_bound(candidate.new_patches, best->second,
{}, &CellularPatch::address);
if (!add(nearest->stock, deposit.inventory))
return std::unexpected{CellularRegionStepError::overflow};
}
}
}
for (std::size_t i = 0; i < patch_stock.size(); ++i)