Deliver cellular tick diagnostics to owners
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 39s
Foundation / checks (ecs) (push) Successful in 1m55s
Foundation / checks (postgres) (push) Failing after 52s
Foundation / checks (tsan) (push) Failing after 33s
Foundation / checks (network) (push) Failing after 3m1s
Foundation / checks (client) (push) Failing after 4m30s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped

This commit is contained in:
dingo
2026-10-11 02:04:55 -04:00
parent fe03a0f8e4
commit ab9fbdec8f
16 changed files with 408 additions and 188 deletions
+140 -119
View File
@@ -101,8 +101,7 @@ bool add_inventory(accounting::Inventory& total, const accounting::Inventory& va
return true;
}
bool subtract_inventory(accounting::Inventory& total,
const accounting::Inventory& value) noexcept {
bool subtract_inventory(accounting::Inventory& total, const accounting::Inventory& value) noexcept {
if (value.energy.value > total.energy.value)
return false;
for (std::size_t channel = 0; channel < total.matter.size(); ++channel)
@@ -147,34 +146,31 @@ sum_state(accounting::Inventory nursery, accounting::Inventory heat,
std::expected<accounting::Inventory, CellularLivesError>
validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
const CellularLivesConfig& config, const NurseryConfig& nursery_config,
const biology::CellularHabitat& habitat,
std::span<const CellularOwnedLife> lives,
std::span<const CellularOwnedCorpse> corpses,
const biology::LineageArchive& archive, accounting::Inventory nursery,
accounting::Inventory heat, accounting::Inventory opening_total) {
const biology::CellularHabitat& habitat, std::span<const CellularOwnedLife> lives,
std::span<const CellularOwnedCorpse> corpses, const biology::LineageArchive& archive,
accounting::Inventory nursery, accounting::Inventory heat,
accounting::Inventory opening_total) {
const auto& map = habitat.config();
if (world == 0 || region == 0 || frame == 0 || nursery_config.world != world ||
nursery_config.region != region || nursery_config.frame != frame ||
nursery_config.source == 0 || nursery_config.funding != nursery ||
map.world != world || map.region != region || map.frame != frame ||
config.area.width_um == 0 || config.area.height_um == 0 ||
map.width_um != config.area.width_um || map.height_um != config.area.height_um ||
config.ambient_temperature_millikelvin == 0 ||
nursery_config.source == 0 || nursery_config.funding != nursery || map.world != world ||
map.region != region || map.frame != frame || config.area.width_um == 0 ||
config.area.height_um == 0 || map.width_um != config.area.width_um ||
map.height_um != config.area.height_um || config.ambient_temperature_millikelvin == 0 ||
nursery_config.sites.empty() || nursery_config.sites.size() > max_primitive_lives ||
!std::ranges::is_sorted(nursery_config.sites) ||
lives.size() > biology::max_cellular_tick_lives ||
corpses.size() > biology::max_archived_organisms ||
archive.recorded() != lives.size() + corpses.size())
return std::unexpected{CellularLivesError::invalid_config};
if (std::ranges::any_of(heat.matter,
[](const auto& value) { return value.value != 0; }))
if (std::ranges::any_of(heat.matter, [](const auto& value) { return value.value != 0; }))
return std::unexpected{CellularLivesError::conservation};
const auto founder = biology::primitive_founder(1, 1);
if (!founder)
return std::unexpected{CellularLivesError::biology};
const auto founder_budget =
biology::metabolic_budget(*founder, {1000, 0}, {config.ambient_temperature_millikelvin, {}});
const auto founder_budget = biology::metabolic_budget(
*founder, {1000, 0}, {config.ambient_temperature_millikelvin, {}});
if (!founder_budget)
return std::unexpected{CellularLivesError::biology};
const auto spawn_extent = founder_budget->phenotype.extent;
@@ -202,10 +198,9 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
for (std::size_t i = 0; i < lives.size(); ++i) {
const auto& life = lives[i];
if (life.account == LifeAccount{} || life.identity.life == 0 ||
life.identity.organism == 0 || life.identity.genome == 0 ||
life.identity.event == 0 || life.identity.born.value == 0 ||
life.identity.born.value > config.tick || life.source == 0 ||
(i != 0 && lives[i - 1].identity.life >= life.identity.life))
life.identity.organism == 0 || life.identity.genome == 0 || life.identity.event == 0 ||
life.identity.born.value == 0 || life.identity.born.value > config.tick ||
life.source == 0 || (i != 0 && lives[i - 1].identity.life >= life.identity.life))
return std::unexpected{CellularLivesError::invalid_life};
for (std::size_t j = 0; j < i; ++j)
if (lives[j].account == life.account ||
@@ -217,8 +212,7 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
return std::unexpected{CellularLivesError::archive};
const auto budget = budget_for(archive, life, config.ambient_temperature_millikelvin);
if (!budget || budget->body != life.body || life.body.energy.value != 0 ||
budget->phenotype.extent == 0 ||
budget->phenotype.extent > config.area.width_um ||
budget->phenotype.extent == 0 || budget->phenotype.extent > config.area.width_um ||
budget->phenotype.extent > config.area.height_um ||
life.position.x_um > config.area.width_um - budget->phenotype.extent ||
life.position.y_um > config.area.height_um - budget->phenotype.extent)
@@ -242,8 +236,8 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
const auto genome = archive.find(record->genome);
if (!genome)
return std::unexpected{CellularLivesError::archive};
const auto budget = biology::metabolic_budget(
*genome, corpse.growth, {config.ambient_temperature_millikelvin, {}});
const auto budget = biology::metabolic_budget(*genome, corpse.growth,
{config.ambient_temperature_millikelvin, {}});
if (!budget || budget->phenotype.extent != corpse.extent_um ||
corpse.extent_um > config.area.width_um || corpse.extent_um > config.area.height_um ||
corpse.position.x_um > config.area.width_um - corpse.extent_um ||
@@ -282,16 +276,14 @@ struct CellularLives::State {
State(std::uint64_t world_id, std::uint64_t region_id, std::uint64_t frame_id,
CellularLivesConfig cfg, NurseryConfig nursery_definition,
std::unique_ptr<biology::CellularHabitat> map,
std::vector<CellularOwnedLife> living, std::vector<CellularOwnedCorpse> dead,
const biology::LineageArchive& history, accounting::Inventory source,
accounting::Inventory accumulated_heat, accounting::Inventory initial,
accounting::Inventory current)
std::unique_ptr<biology::CellularHabitat> map, std::vector<CellularOwnedLife> living,
std::vector<CellularOwnedCorpse> dead, const biology::LineageArchive& history,
accounting::Inventory source, accounting::Inventory accumulated_heat,
accounting::Inventory initial, accounting::Inventory current)
: world{world_id}, region{region_id}, frame{frame_id}, config{cfg},
nursery_config{std::move(nursery_definition)},
habitat{std::move(map)}, lives{std::move(living)}, corpses{std::move(dead)},
archive{history}, nursery{source}, heat{accumulated_heat}, opening_total{initial},
current_total{current} {}
nursery_config{std::move(nursery_definition)}, habitat{std::move(map)},
lives{std::move(living)}, corpses{std::move(dead)}, archive{history}, nursery{source},
heat{accumulated_heat}, opening_total{initial}, current_total{current} {}
};
CellularLives::CellularLives(std::unique_ptr<State> state) : state_{std::move(state)} {}
@@ -319,8 +311,8 @@ CellularLives::create(const PrimitiveLives& opening,
for (const auto& primitive : opening.lives()) {
if (primitive.identity.born.value == 0 || primitive.identity.born.value > config.tick ||
primitive.identity.life == 0 || primitive.identity.organism == 0 ||
primitive.identity.genome == 0 || primitive.source == 0 ||
primitive.site.x_um < 0 || primitive.site.y_um < 0)
primitive.identity.genome == 0 || primitive.source == 0 || primitive.site.x_um < 0 ||
primitive.site.y_um < 0)
return std::unexpected{CellularLivesError::invalid_life};
if (primitive.body.value == 0 || primitive.reserve.value == 0)
return std::unexpected{CellularLivesError::invalid_life};
@@ -342,8 +334,8 @@ CellularLives::create(const PrimitiveLives& opening,
const auto& life = lives[i];
if (i != 0 && lives[i - 1].identity.life == life.identity.life)
return std::unexpected{CellularLivesError::duplicate_life};
const auto budget = budget_for(opening.archive(), life,
config.ambient_temperature_millikelvin);
const auto budget =
budget_for(opening.archive(), life, config.ambient_temperature_millikelvin);
if (!budget || budget->phenotype.extent == 0 ||
budget->phenotype.extent > config.area.width_um ||
budget->phenotype.extent > config.area.height_um || life.body.energy.value != 0 ||
@@ -367,36 +359,58 @@ CellularLives::create(const PrimitiveLives& opening,
const auto world = nursery_config.world;
const auto region = nursery_config.region;
const auto frame = nursery_config.frame;
auto state = std::make_unique<State>(
world, region, frame, config,
std::move(nursery_config),
std::move(habitat), std::move(lives), std::vector<CellularOwnedCorpse>{},
opening.archive(), nursery, heat, opening_total, *current);
auto state = std::make_unique<State>(world, region, frame, config, std::move(nursery_config),
std::move(habitat), std::move(lives),
std::vector<CellularOwnedCorpse>{}, opening.archive(),
nursery, heat, opening_total, *current);
return std::unique_ptr<CellularLives>{new CellularLives{std::move(state)}};
}
std::uint64_t CellularLives::world() const noexcept { return state_->world; }
std::uint64_t CellularLives::region() const noexcept { return state_->region; }
std::uint64_t CellularLives::frame() const noexcept { return state_->frame; }
std::uint64_t CellularLives::tick() const noexcept { return state_->config.tick; }
std::uint64_t CellularLives::world() const noexcept {
return state_->world;
}
std::uint64_t CellularLives::region() const noexcept {
return state_->region;
}
std::uint64_t CellularLives::frame() const noexcept {
return state_->frame;
}
std::uint64_t CellularLives::tick() const noexcept {
return state_->config.tick;
}
std::uint32_t CellularLives::ambient_temperature_millikelvin() const noexcept {
return state_->config.ambient_temperature_millikelvin;
}
biology::CellularArea CellularLives::area() const noexcept { return state_->config.area; }
std::span<const CellularOwnedLife> CellularLives::lives() const noexcept { return state_->lives; }
biology::CellularArea CellularLives::area() const noexcept {
return state_->config.area;
}
std::span<const CellularOwnedLife> CellularLives::lives() const noexcept {
return state_->lives;
}
std::span<const CellularOwnedCorpse> CellularLives::corpses() const noexcept {
return state_->corpses;
}
const biology::CellularHabitat& CellularLives::habitat() const noexcept { return *state_->habitat; }
const biology::LineageArchive& CellularLives::archive() const noexcept { return state_->archive; }
accounting::Inventory CellularLives::nursery() const noexcept { return state_->nursery; }
accounting::Inventory CellularLives::heat() const noexcept { return state_->heat; }
const biology::CellularHabitat& CellularLives::habitat() const noexcept {
return *state_->habitat;
}
const biology::LineageArchive& CellularLives::archive() const noexcept {
return state_->archive;
}
accounting::Inventory CellularLives::nursery() const noexcept {
return state_->nursery;
}
accounting::Inventory CellularLives::heat() const noexcept {
return state_->heat;
}
accounting::Inventory CellularLives::opening_total() const noexcept {
return state_->opening_total;
}
accounting::Inventory CellularLives::current_total() const noexcept {
return state_->current_total;
}
std::span<const CellularLifeTickFeedback> CellularLives::last_tick_feedback() const noexcept {
return last_tick_feedback_;
}
std::vector<std::uint8_t> CellularLives::snapshot() const {
const auto habitat = state_->habitat->snapshot();
@@ -408,7 +422,8 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
cellular_life_bytes * life_count +
cellular_corpse_bytes * corpse_count + habitat.size() +
archive.size() + 8;
if (total_bytes > max_cellular_lives_bytes || habitat.size() > biology::max_cellular_habitat_bytes ||
if (total_bytes > max_cellular_lives_bytes ||
habitat.size() > biology::max_cellular_habitat_bytes ||
archive.size() > biology::max_archive_bytes || site_count > max_primitive_lives ||
life_count > biology::max_cellular_tick_lives ||
corpse_count > biology::max_archived_organisms)
@@ -476,13 +491,12 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
std::expected<std::vector<biology::CellularPatchPerception>, CellularLivesError>
CellularLives::perceive(std::uint64_t life_id) const {
const auto life = std::ranges::find(state_->lives, life_id, [](const auto& owned) {
return owned.identity.life;
});
const auto life = std::ranges::find(state_->lives, life_id,
[](const auto& owned) { return owned.identity.life; });
if (life == state_->lives.end())
return std::unexpected{CellularLivesError::invalid_life};
const auto budget = budget_for(state_->archive, *life,
state_->config.ambient_temperature_millikelvin);
const auto budget =
budget_for(state_->archive, *life, state_->config.ambient_temperature_millikelvin);
if (!budget)
return std::unexpected{budget.error()};
auto view = state_->habitat->perceive(life->position, budget->phenotype.extent);
@@ -507,15 +521,19 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
return life.account == request.account;
}))
return std::unexpected{CellularLivesError::life_exists};
if (std::ranges::any_of(state_->lives, [&](const CellularOwnedLife& life) {
return life.identity.life == allocation.life ||
life.identity.organism == allocation.organism ||
life.identity.genome == allocation.genome ||
life.identity.event == allocation.event;
}) ||
std::ranges::any_of(state_->corpses, [&](const CellularOwnedCorpse& corpse) {
return corpse.life_id == allocation.life || corpse.organism == allocation.organism;
}) || state_->archive.find(allocation.genome) || state_->archive.organism(allocation.organism))
if (std::ranges::any_of(state_->lives,
[&](const CellularOwnedLife& life) {
return life.identity.life == allocation.life ||
life.identity.organism == allocation.organism ||
life.identity.genome == allocation.genome ||
life.identity.event == allocation.event;
}) ||
std::ranges::any_of(state_->corpses,
[&](const CellularOwnedCorpse& corpse) {
return corpse.life_id == allocation.life ||
corpse.organism == allocation.organism;
}) ||
state_->archive.find(allocation.genome) || state_->archive.organism(allocation.organism))
return std::unexpected{CellularLivesError::duplicate_life};
const auto endowed = biology::primitive_endowment();
@@ -523,8 +541,7 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
if (!endowed || !genome)
return std::unexpected{CellularLivesError::biology};
const auto budget = biology::metabolic_budget(
*genome, endowed->growth,
{state_->config.ambient_temperature_millikelvin, {}});
*genome, endowed->growth, {state_->config.ambient_temperature_millikelvin, {}});
if (!budget || budget->body != endowed->body)
return std::unexpected{CellularLivesError::biology};
@@ -533,11 +550,11 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
if (site.x_um < 0 || site.y_um < 0)
return std::unexpected{CellularLivesError::invalid_config};
const biology::CellularPosition candidate{static_cast<std::uint64_t>(site.x_um),
static_cast<std::uint64_t>(site.y_um)};
static_cast<std::uint64_t>(site.y_um)};
bool occupied = false;
for (const auto& living : state_->lives) {
const auto living_budget = budget_for(
state_->archive, living, state_->config.ambient_temperature_millikelvin);
const auto living_budget =
budget_for(state_->archive, living, state_->config.ambient_temperature_millikelvin);
if (!living_budget)
return std::unexpected{living_budget.error()};
const auto separated = [](std::uint64_t left, std::uint64_t right,
@@ -568,8 +585,7 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
return std::unexpected{CellularLivesError::insufficient_funding};
nursery.energy.value -= endowed->construction.value;
auto heat = state_->heat;
if (endowed->construction.value >
std::numeric_limits<std::uint64_t>::max() - heat.energy.value)
if (endowed->construction.value > std::numeric_limits<std::uint64_t>::max() - heat.energy.value)
return std::unexpected{CellularLivesError::overflow};
heat.energy.value += endowed->construction.value;
@@ -580,27 +596,25 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
auto lives = state_->lives;
lives.push_back({request.account, allocation, state_->nursery_config.source, endowed->growth,
*position, endowed->body, endowed->reserve});
std::ranges::sort(lives, {}, [](const CellularOwnedLife& life) {
return life.identity.life;
});
std::ranges::sort(lives, {}, [](const CellularOwnedLife& life) { return life.identity.life; });
auto nursery_config = state_->nursery_config;
nursery_config.funding = nursery;
auto habitat = biology::CellularHabitat::restore(state_->habitat->snapshot(), state_->world,
state_->region, state_->frame);
state_->region, state_->frame);
if (!habitat)
return std::unexpected{CellularLivesError::habitat};
const auto current = validate_state(state_->world, state_->region, state_->frame,
state_->config, nursery_config, **habitat, lives,
state_->corpses, archive, nursery, heat,
state_->opening_total);
const auto current = validate_state(
state_->world, state_->region, state_->frame, state_->config, nursery_config, **habitat,
lives, state_->corpses, archive, nursery, heat, state_->opening_total);
if (!current)
return std::unexpected{current.error()};
auto candidate = std::unique_ptr<CellularLives>{new CellularLives{std::make_unique<State>(
state_->world, state_->region, state_->frame, state_->config,
std::move(nursery_config), std::move(*habitat), std::move(lives), state_->corpses,
archive, nursery, heat, state_->opening_total, *current)}};
state_->world, state_->region, state_->frame, state_->config, std::move(nursery_config),
std::move(*habitat), std::move(lives), state_->corpses, archive, nursery, heat,
state_->opening_total, *current)}};
candidate->last_tick_feedback_ = last_tick_feedback_;
return candidate;
}
@@ -696,8 +710,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
const auto habitat_payload = in.raw(static_cast<std::size_t>(habitat_size));
const auto archive_payload = in.raw(static_cast<std::size_t>(archive_size));
auto habitat = biology::CellularHabitat::restore(habitat_payload, saved_world, saved_region,
saved_frame);
auto habitat =
biology::CellularHabitat::restore(habitat_payload, saved_world, saved_region, saved_frame);
if (!habitat) {
if (habitat.error() == biology::CellularHabitatError::version)
return std::unexpected{CellularLivesError::version};
@@ -715,9 +729,9 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
return std::unexpected{CellularLivesError::noncanonical};
return std::unexpected{CellularLivesError::archive};
}
const auto current = validate_state(saved_world, saved_region, saved_frame, config,
nursery_config, **habitat, lives, corpses, *archive,
nursery, heat, opening_total);
const auto current =
validate_state(saved_world, saved_region, saved_frame, config, nursery_config, **habitat,
lives, corpses, *archive, nursery, heat, opening_total);
if (!current)
return std::unexpected{current.error()};
@@ -747,8 +761,8 @@ CellularLives::advance_tick(std::span<const CellularLifeIntent> intents) {
inputs.reserve(state_->lives.size());
for (std::size_t i = 0; i < state_->lives.size(); ++i) {
const auto& life = state_->lives[i];
const auto budget = budget_for(state_->archive, life,
state_->config.ambient_temperature_millikelvin);
const auto budget =
budget_for(state_->archive, life, state_->config.ambient_temperature_millikelvin);
if (!budget)
return std::unexpected{budget.error()};
inputs.push_back({life.identity.life,
@@ -786,18 +800,13 @@ CellularLives::advance_tick(std::span<const CellularLifeIntent> intents) {
if (found == next_lives.end() || found->identity.life != dead.life_id ||
!next_archive.record_death(found->identity.organism, {proposal->end_tick}))
return std::unexpected{CellularLivesError::archive};
const auto corpse_budget = budget_for(
next_archive, *found, state_->config.ambient_temperature_millikelvin);
const auto corpse_budget =
budget_for(next_archive, *found, state_->config.ambient_temperature_millikelvin);
if (!corpse_budget)
return std::unexpected{corpse_budget.error()};
next_corpses.push_back({found->identity.life,
found->identity.organism,
found->source,
proposal->end_tick,
dead.position,
found->growth,
corpse_budget->phenotype.extent,
dead.inventory});
next_corpses.push_back({found->identity.life, found->identity.organism, found->source,
proposal->end_tick, dead.position, found->growth,
corpse_budget->phenotype.extent, dead.inventory});
next_lives.erase(found);
}
std::ranges::sort(next_corpses, {}, &CellularOwnedCorpse::organism);
@@ -806,10 +815,9 @@ CellularLives::advance_tick(std::span<const CellularLifeIntent> intents) {
return std::unexpected{CellularLivesError::overflow};
next_heat.energy.value += proposal->heat_released.value;
for (const auto& update : proposal->patches) {
const auto found = std::ranges::lower_bound(
habitat_config.patches, update.address, {}, [](const auto& patch) {
return patch.address;
});
const auto found =
std::ranges::lower_bound(habitat_config.patches, update.address, {},
[](const auto& patch) { return patch.address; });
if (found == habitat_config.patches.end() || found->address != update.address)
return std::unexpected{CellularLivesError::habitat};
found->stock = update.stock;
@@ -817,23 +825,36 @@ CellularLives::advance_tick(std::span<const CellularLifeIntent> intents) {
auto next_habitat = biology::CellularHabitat::create(std::move(habitat_config));
if (!next_habitat)
return std::unexpected{CellularLivesError::habitat};
const auto total = validate_state(
state_->world, state_->region, state_->frame,
CellularLivesConfig{state_->config.area, proposal->end_tick,
state_->config.ambient_temperature_millikelvin},
state_->nursery_config, **next_habitat, next_lives, next_corpses, next_archive,
state_->nursery, next_heat, state_->opening_total);
const auto total =
validate_state(state_->world, state_->region, state_->frame,
CellularLivesConfig{state_->config.area, proposal->end_tick,
state_->config.ambient_temperature_millikelvin},
state_->nursery_config, **next_habitat, next_lives, next_corpses,
next_archive, state_->nursery, next_heat, state_->opening_total);
if (!total)
return std::unexpected{total.error()};
auto next = std::make_unique<State>(
state_->world, state_->region, state_->frame,
CellularLivesConfig{state_->config.area, proposal->end_tick,
state_->config.ambient_temperature_millikelvin},
state_->nursery_config,
std::move(*next_habitat), std::move(next_lives), std::move(next_corpses), next_archive,
state_->nursery, next_heat, state_->opening_total, *total);
auto next =
std::make_unique<State>(state_->world, state_->region, state_->frame,
CellularLivesConfig{state_->config.area, proposal->end_tick,
state_->config.ambient_temperature_millikelvin},
state_->nursery_config, std::move(*next_habitat),
std::move(next_lives), std::move(next_corpses), next_archive,
state_->nursery, next_heat, state_->opening_total, *total);
std::vector<CellularLifeTickFeedback> feedback;
feedback.reserve(proposal->lives.size());
for (const auto& result : proposal->lives) {
const auto input =
std::ranges::lower_bound(ordered, result.life_id, {},
[](const CellularLifeIntent& value) { return value.life_id; });
feedback.push_back({result.life_id, proposal->end_tick, result.candidate.reserve_after,
result.candidate.maintenance_cost, result.candidate.movement_cost,
result.candidate.movement_status, result.candidate.intake_status});
if (input == ordered.end() || input->life_id != result.life_id)
return std::unexpected{CellularLivesError::invalid_intents};
}
state_.swap(next);
last_tick_feedback_ = std::move(feedback);
return std::move(*proposal);
}
} // namespace evo::server