M12.4: preserve exact biology across ownership candidates
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m18s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 5m33s
Foundation / checks (postgres) (push) Canceled after 5m29s
Foundation / checks (tsan) (push) Canceled after 5m28s
Foundation / checks (client) (push) Canceled after 5m37s
Foundation / checks (network) (push) Canceled after 5m39s

This commit is contained in:
dingo
2026-10-17 18:17:59 -04:00
parent 0a8962b57e
commit decd4f8043
27 changed files with 1408 additions and 61 deletions
+175 -46
View File
@@ -18,6 +18,7 @@ constexpr std::uint64_t cellular_lives_v3_magic = 0x3330564C43564541ULL; // AEVC
constexpr std::uint64_t cellular_lives_v4_magic = 0x3430564C43564541ULL; // AEVCLV04
constexpr std::uint64_t cellular_lives_v5_magic = 0x3530564C43564541ULL; // AEVCLV05
constexpr std::uint64_t cellular_lives_v6_magic = 0x3630564C43564541ULL; // AEVCLV06
constexpr std::uint64_t cellular_lives_v7_magic = 0x3730564C43564541ULL; // AEVCLV07
constexpr std::size_t cellular_lives_header_bytes = 212;
constexpr std::size_t cellular_site_bytes = 8;
constexpr std::size_t cellular_life_v1_bytes = 168;
@@ -32,6 +33,8 @@ constexpr std::size_t cellular_life_v5_bytes = cellular_life_v2_bytes + 8;
constexpr std::size_t cellular_corpse_v5_bytes = cellular_corpse_v4_bytes + 8;
// Format 6 (ADR-0128): each life adds presence (1), 7 zero bytes and its freeze tick (8).
constexpr std::size_t cellular_life_v6_bytes = cellular_life_v5_bytes + 16;
// Format 7: two boundary-flow inventories and nonresident count/reserved.
constexpr std::size_t cellular_lives_v7_header_bytes = cellular_lives_v5_header_bytes + 88;
std::uint64_t checksum(std::span<const std::uint8_t> bytes) noexcept {
std::uint64_t value = 14695981039346656037ULL;
@@ -281,7 +284,7 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
accounting::Inventory nursery, accounting::Inventory heat,
accounting::Inventory opening_total, bool detritus_rules = false,
bool bud_rules = false, std::uint64_t next_bud_sequence = 1,
bool presence_rules = false) {
bool presence_rules = false, const CellularBoundaryLedger* boundary = nullptr) {
// A bud's identity lies in this region's namespace below the next sequence; anything
// else is an allocated identity, below the namespace (ADR-0125).
const auto bud_identity = [&](std::uint64_t identity) noexcept {
@@ -292,7 +295,11 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
const auto lineage_valid = [&](std::uint64_t life_id, std::uint64_t parent) noexcept {
if (parent == 0) // Rules 4 keep allocated identities below the bud namespace.
return !bud_rules || life_id < cellular_bud_namespace;
return bud_identity(life_id) && parent != life_id;
const auto origin = (life_id >> 32U) & cellular_bud_max_region;
const bool imported = boundary && life_id >= cellular_bud_namespace && origin != region &&
origin > 0 && origin <= cellular_bud_max_region &&
(life_id & 0xFFFF'FFFFULL) != 0;
return (bud_identity(life_id) || imported) && parent != life_id;
};
const auto& map = habitat.config();
if (world == 0 || region == 0 || frame == 0 || nursery_config.world != world ||
@@ -305,7 +312,8 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
!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())
archive.recorded() !=
lives.size() + corpses.size() + (boundary ? boundary->nonresident_organisms.size() : 0))
return std::unexpected{CellularLivesError::invalid_config};
if (std::ranges::any_of(heat.matter, [](const auto& value) { return value.value != 0; }))
return std::unexpected{CellularLivesError::conservation};
@@ -338,7 +346,8 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
std::vector<std::uint64_t> live_organisms;
std::vector<std::uint64_t> life_ids;
live_organisms.reserve(lives.size());
life_ids.reserve(lives.size() + corpses.size());
life_ids.reserve(lives.size() + corpses.size() +
(boundary ? boundary->nonresident_organisms.size() : 0));
for (std::size_t i = 0; i < lives.size(); ++i) {
const auto& life = lives[i];
// Founders are owned; buds are unowned wildlife (rules 4) and never earn offers.
@@ -382,6 +391,24 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
live_organisms.push_back(life.identity.organism);
life_ids.push_back(life.identity.life);
}
if (boundary) {
if (boundary->nonresident_organisms.size() > biology::max_archived_organisms ||
!std::ranges::is_sorted(boundary->nonresident_organisms) ||
std::ranges::adjacent_find(boundary->nonresident_organisms) !=
boundary->nonresident_organisms.end())
return std::unexpected(CellularLivesError::invalid_life);
for (const auto id : boundary->nonresident_organisms) {
if (id < cellular_bud_namespace || !lineage_valid(id, 1))
return std::unexpected(CellularLivesError::invalid_life);
const auto record = archive.organism(id);
if (!record || record->born.value > config.tick ||
(!record->alive && record->died.value > config.tick))
return std::unexpected(CellularLivesError::archive);
if (record->alive)
live_organisms.push_back(id);
life_ids.push_back(id);
}
}
std::ranges::sort(live_organisms);
if (archive.living_organisms() != live_organisms)
return std::unexpected{CellularLivesError::archive};
@@ -427,7 +454,26 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
const auto current = sum_state(nursery, heat, habitat, lives, corpses);
if (!current)
return std::unexpected{current.error()};
if (*current != opening_total)
auto expected = opening_total;
if (boundary) {
accounting::Inventory credit{}, debit{};
const auto difference = [](std::uint64_t in, std::uint64_t out, std::uint64_t& plus,
std::uint64_t& minus) {
if (in >= out)
plus = in - out;
else
minus = out - in;
};
difference(boundary->incoming.energy.value, boundary->outgoing.energy.value,
credit.energy.value, debit.energy.value);
for (std::size_t channel = 0; channel < credit.matter.size(); ++channel)
difference(boundary->incoming.matter[channel].value,
boundary->outgoing.matter[channel].value, credit.matter[channel].value,
debit.matter[channel].value);
if (!add_inventory(expected, credit) || !subtract_inventory(expected, debit))
return std::unexpected(CellularLivesError::conservation);
}
if (*current != expected)
return std::unexpected{CellularLivesError::conservation};
return *current;
}
@@ -447,6 +493,7 @@ struct CellularLives::State {
accounting::Inventory opening_total{};
accounting::Inventory current_total{};
std::uint64_t next_bud_sequence{1}; // Format 5: the next bud identity's sequence.
CellularBoundaryLedger boundary{};
State(std::uint64_t world_id, std::uint64_t region_id, std::uint64_t frame_id,
CellularLivesConfig cfg, NurseryConfig nursery_definition,
@@ -454,12 +501,13 @@ struct CellularLives::State {
std::vector<CellularOwnedCorpse> dead, const biology::LineageArchive& history,
accounting::Inventory source, accounting::Inventory accumulated_heat,
accounting::Inventory initial, accounting::Inventory current,
std::uint32_t format = cellular_lives_format, std::uint32_t rules = cellular_lives_rules)
std::uint32_t format = cellular_lives_format, std::uint32_t rules = cellular_lives_rules,
CellularBoundaryLedger boundary_ledger = {})
: world{world_id}, region{region_id}, frame{frame_id}, format_version{format},
rules_version{rules}, 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} {}
current_total{current}, boundary{std::move(boundary_ledger)} {}
};
CellularLives::CellularLives(std::unique_ptr<State> state) : state_{std::move(state)} {}
@@ -623,6 +671,46 @@ accounting::Inventory CellularLives::opening_total() const noexcept {
accounting::Inventory CellularLives::current_total() const noexcept {
return state_->current_total;
}
const CellularBoundaryLedger& CellularLives::boundary_ledger() const noexcept {
return state_->boundary;
}
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
CellularLives::migration_replacement(std::vector<CellularOwnedLife> living,
const biology::LineageArchive& archive,
CellularBoundaryLedger boundary) const {
if (state_->rules_version != cellular_lives_foraging_rules ||
(state_->format_version != cellular_lives_presence_format &&
state_->format_version != cellular_lives_migration_format))
return std::unexpected(CellularLivesError::version);
auto habitat = biology::CellularHabitat::restore(state_->habitat->snapshot(), state_->world,
state_->region, state_->frame);
if (!habitat)
return std::unexpected(CellularLivesError::habitat);
const auto total = validate_state(state_->world, state_->region, state_->frame, state_->config,
state_->nursery_config, **habitat, living, state_->corpses,
archive, state_->nursery, state_->heat, state_->opening_total,
true, true, state_->next_bud_sequence, true, &boundary);
if (!total)
return std::unexpected(total.error());
auto replacement = std::unique_ptr<CellularLives>{new CellularLives{std::make_unique<State>(
state_->world, state_->region, state_->frame, state_->config, state_->nursery_config,
std::move(*habitat), std::move(living), state_->corpses, archive, state_->nursery,
state_->heat, state_->opening_total, *total, cellular_lives_migration_format,
state_->rules_version, std::move(boundary))}};
replacement->state_->next_bud_sequence = state_->next_bud_sequence;
try {
[[maybe_unused]] const auto bytes = replacement->snapshot();
} catch (const std::length_error&) {
return std::unexpected(CellularLivesError::overflow);
}
return replacement;
}
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
CellularLives::migration_upgrade_candidate() const {
return migration_replacement(state_->lives, state_->archive, state_->boundary);
}
std::span<const CellularLifeTickFeedback> CellularLives::last_tick_feedback() const noexcept {
return last_tick_feedback_;
}
@@ -644,16 +732,20 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
const auto corpse_bytes = parents ? cellular_corpse_v5_bytes
: causes ? cellular_corpse_v4_bytes
: cellular_corpse_bytes;
const auto header_bytes =
parents ? cellular_lives_v5_header_bytes : cellular_lives_header_bytes;
const bool migration = state_->format_version == cellular_lives_migration_format;
const auto header_bytes = migration ? cellular_lives_v7_header_bytes
: parents ? cellular_lives_v5_header_bytes
: cellular_lives_header_bytes;
const auto external_count = migration ? state_->boundary.nonresident_organisms.size() : 0;
const auto total_bytes = header_bytes + cellular_site_bytes * site_count +
life_bytes * life_count + corpse_bytes * corpse_count +
habitat.size() + archive.size() + 8;
habitat.size() + archive.size() + external_count * 8 + 8;
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)
corpse_count > biology::max_archived_organisms ||
external_count > biology::max_archived_organisms)
throw std::length_error{"cellular lives checkpoint exceeds its format bounds"};
std::vector<std::uint8_t> bytes;
@@ -665,6 +757,7 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
: state_->format_version == cellular_lives_engulf_format ? cellular_lives_v4_magic
: state_->format_version == cellular_lives_bud_format ? cellular_lives_v5_magic
: state_->format_version == cellular_lives_presence_format ? cellular_lives_v6_magic
: migration ? cellular_lives_v7_magic
: cellular_lives_v2_magic;
out.put(magic);
out.put(state_->format_version, 4);
@@ -688,6 +781,12 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
out.inventory(state_->heat);
if (parents)
out.put(state_->next_bud_sequence);
if (migration) {
out.inventory(state_->boundary.incoming);
out.inventory(state_->boundary.outgoing);
out.put(external_count, 4);
out.put(0, 4);
}
for (const auto site : state_->nursery_config.sites)
out.site(site);
for (const auto& life : state_->lives) {
@@ -768,6 +867,9 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
}
out.raw(habitat);
out.raw(archive);
if (migration)
for (const auto id : state_->boundary.nonresident_organisms)
out.put(id);
if (bytes.size() + 8 != total_bytes)
throw std::logic_error{"cellular lives checkpoint size calculation mismatch"};
out.put(checksum(bytes));
@@ -948,19 +1050,21 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
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, state_->rules_version >= cellular_lives_engulf_rules,
state_->rules_version >= cellular_lives_bud_rules, state_->next_bud_sequence,
state_->rules_version >= cellular_lives_presence_rules);
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,
state_->rules_version >= cellular_lives_engulf_rules,
state_->rules_version >= cellular_lives_bud_rules, state_->next_bud_sequence,
state_->rules_version >= cellular_lives_presence_rules,
state_->format_version == cellular_lives_migration_format ? &state_->boundary : nullptr);
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_->format_version, state_->rules_version)}};
state_->opening_total, *current, state_->format_version, state_->rules_version,
state_->boundary)}};
candidate->last_tick_feedback_ = last_tick_feedback_;
candidate->state_->next_bud_sequence = state_->next_bud_sequence;
return candidate;
@@ -985,12 +1089,16 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
format_version == cellular_lives_format &&
rules_version == cellular_lives_rules;
// Format 6 carries rules 5, 6 (founder 2) or 7 (founder 3, ADR-0137); no new state.
const bool presence_version = magic == cellular_lives_v6_magic &&
format_version == cellular_lives_presence_format &&
(rules_version == cellular_lives_presence_rules ||
rules_version == cellular_lives_balance_rules ||
rules_version == cellular_lives_reproduction_rules ||
rules_version == cellular_lives_foraging_rules);
const bool migration_version = magic == cellular_lives_v7_magic &&
format_version == cellular_lives_migration_format &&
rules_version == cellular_lives_foraging_rules;
const bool presence_version =
migration_version ||
(magic == cellular_lives_v6_magic && format_version == cellular_lives_presence_format &&
(rules_version == cellular_lives_presence_rules ||
rules_version == cellular_lives_balance_rules ||
rules_version == cellular_lives_reproduction_rules ||
rules_version == cellular_lives_foraging_rules));
// Formats 5 and 6 carry the bud sequence and parent lives (rules 4 and 5).
const bool bud_version = presence_version || (magic == cellular_lives_v5_magic &&
format_version == cellular_lives_bud_format &&
@@ -1006,6 +1114,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
const bool mutation_version = offer_version || revision_version;
if (!legacy && !mutation_version)
return std::unexpected{CellularLivesError::version};
if (migration_version && bytes.size() < cellular_lives_v7_header_bytes + 8)
return std::unexpected(CellularLivesError::format);
const auto saved_world = in.get();
const auto saved_region = in.get();
const auto saved_frame = in.get();
@@ -1036,6 +1146,15 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
const auto nursery = in.inventory();
const auto heat = in.inventory();
const auto next_bud_sequence = bud_version ? in.get() : std::uint64_t{1};
CellularBoundaryLedger boundary;
std::uint64_t nonresident_count = 0;
if (migration_version) {
boundary.incoming = in.inventory();
boundary.outgoing = in.inventory();
nonresident_count = in.get(4);
if (in.get(4) != 0 || nonresident_count > biology::max_archived_organisms)
return std::unexpected(CellularLivesError::format);
}
const auto life_bytes = legacy ? cellular_life_v1_bytes
: presence_version ? cellular_life_v6_bytes
: bud_version ? cellular_life_v5_bytes
@@ -1043,10 +1162,12 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
const auto corpse_bytes = bud_version ? cellular_corpse_v5_bytes
: engulf_version ? cellular_corpse_v4_bytes
: cellular_corpse_bytes;
const auto expected_size =
(bud_version ? cellular_lives_v5_header_bytes : cellular_lives_header_bytes) +
cellular_site_bytes * site_count + life_bytes * life_count + corpse_bytes * corpse_count +
habitat_size + archive_size + 8;
const auto expected_size = (migration_version ? cellular_lives_v7_header_bytes
: bud_version ? cellular_lives_v5_header_bytes
: cellular_lives_header_bytes) +
cellular_site_bytes * site_count + life_bytes * life_count +
corpse_bytes * corpse_count + habitat_size + archive_size +
nonresident_count * 8 + 8;
if (source == 0 || next_bud_sequence == 0 || expected_size != bytes.size())
return std::unexpected{CellularLivesError::format};
@@ -1189,6 +1310,11 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
return std::unexpected{CellularLivesError::noncanonical};
return std::unexpected{CellularLivesError::archive};
}
if (migration_version) {
boundary.nonresident_organisms.reserve(static_cast<std::size_t>(nonresident_count));
for (std::uint64_t index = 0; index < nonresident_count; ++index)
boundary.nonresident_organisms.push_back(in.get());
}
if (archive->format_version() != (revision_version ? biology::lineage_archive_revision_format
: biology::lineage_archive_format))
return std::unexpected{CellularLivesError::version};
@@ -1214,17 +1340,17 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
return std::unexpected{CellularLivesError::noncanonical};
}
}
const auto current =
validate_state(saved_world, saved_region, saved_frame, config, nursery_config, **habitat,
lives, corpses, *archive, nursery, heat, opening_total, engulf_version,
bud_version, next_bud_sequence, presence_version);
const auto current = validate_state(
saved_world, saved_region, saved_frame, config, nursery_config, **habitat, lives, corpses,
*archive, nursery, heat, opening_total, engulf_version, bud_version, next_bud_sequence,
presence_version, migration_version ? &boundary : nullptr);
if (!current)
return std::unexpected{current.error()};
auto restored = std::unique_ptr<CellularLives>{new CellularLives{std::make_unique<State>(
saved_world, saved_region, saved_frame, config, std::move(nursery_config),
std::move(*habitat), std::move(lives), std::move(corpses), *archive, nursery, heat,
opening_total, *current, format_version, rules_version)}};
opening_total, *current, format_version, rules_version, std::move(boundary))}};
restored->state_->next_bud_sequence = next_bud_sequence;
if (!std::ranges::equal(bytes, restored->snapshot()))
return std::unexpected{CellularLivesError::noncanonical};
@@ -1502,8 +1628,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
}();
for (int copy = 0; copy < (twins ? 2 : 1); ++copy) {
if (copy == 1)
// Appending the first bud may have moved the parent; find it again (buds
// sort after every existing life, so the order still holds).
// Inserting the first bud may have moved the parent; find it again.
parent = std::ranges::lower_bound(
next_lives, request.life_id, {},
[](const CellularOwnedLife& value) { return value.identity.life; });
@@ -1556,9 +1681,12 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
bud.body = transition->body;
bud.reserve = transition->reserve;
bud.parent_life = parent->identity.life;
// Bud identities increase within the region and exceed every allocated identity,
// so the new life sorts after all existing ones.
next_lives.push_back(bud);
// Foreign residents can have a higher origin-region namespace. Keep
// the sequence sorted even before the next parent's lookup/twin.
const auto slot = std::ranges::lower_bound(
next_lives, identity, {},
[](const CellularOwnedLife& life) { return life.identity.life; });
next_lives.insert(slot, bud);
++next_bud_sequence;
if (!twin)
bud_results.push_back({request.life_id, identity, CellularBudResult::budded});
@@ -1640,13 +1768,14 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
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,
engulf_rules, bud_rules, next_bud_sequence, presence_rules);
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, engulf_rules, bud_rules,
next_bud_sequence, presence_rules,
state_->format_version == cellular_lives_migration_format ? &state_->boundary : nullptr);
if (!total)
return std::unexpected{total.error()};
@@ -1656,7 +1785,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
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, state_->format_version, state_->rules_version);
*total, state_->format_version, state_->rules_version, state_->boundary);
next->next_bud_sequence = next_bud_sequence;
std::vector<CellularLifeTickFeedback> feedback;
feedback.reserve(proposal->lives.size());
+164
View File
@@ -0,0 +1,164 @@
#include "evo/server/lifecycle/cellular_migration.hpp"
#include <algorithm>
#include <limits>
namespace evo::server {
namespace {
bool add(accounting::Inventory& total, const accounting::Inventory& value) {
const auto maximum = std::numeric_limits<std::uint64_t>::max();
if (value.energy.value > maximum - total.energy.value)
return false;
for (std::size_t c = 0; c < total.matter.size(); ++c)
if (value.matter[c].value > maximum - total.matter[c].value)
return false;
total.energy.value += value.energy.value;
for (std::size_t c = 0; c < total.matter.size(); ++c)
total.matter[c].value += value.matter[c].value;
return true;
}
bool suspended(const CellularLives& owner) {
return std::ranges::all_of(owner.lives(), [](const auto& life) {
return life.account == LifeAccount{} || life.presence == CellularPresence::suspended;
});
}
std::expected<std::uint32_t, CellularMigrationError> extent(const CellularLives& owner,
const CellularOwnedLife& life) {
const auto genome = owner.archive().find(life.identity.genome);
if (!genome)
return std::unexpected(CellularMigrationError::archive);
const auto budget = biology::metabolic_budget(*genome, life.growth,
{owner.ambient_temperature_millikelvin(), {}});
if (!budget)
return std::unexpected(CellularMigrationError::archive);
return budget->phenotype.extent;
}
bool overlaps(biology::CellularPosition a, std::uint32_t size_a, biology::CellularPosition b,
std::uint32_t size_b) {
return a.x_um < b.x_um + size_b && b.x_um < a.x_um + size_a && a.y_um < b.y_um + size_b &&
b.y_um < a.y_um + size_a;
}
} // namespace
std::expected<CellularMigrationExecution, CellularMigrationError>
CellularMigration::prepare(const CellularLives& source, const CellularLives& destination,
std::uint64_t genome) {
if (source.format_version() != cellular_lives_migration_format ||
destination.format_version() != cellular_lives_migration_format ||
source.rules_version() != cellular_lives_foraging_rules ||
destination.rules_version() != cellular_lives_foraging_rules)
return std::unexpected(CellularMigrationError::version);
if (genome == 0 || source.world() != destination.world() ||
source.region() > cellular_bud_max_region ||
destination.region() > cellular_bud_max_region || source.region() == destination.region() ||
source.frame() != destination.frame() || source.area() != destination.area() ||
source.ambient_temperature_millikelvin() != destination.ambient_temperature_millikelvin())
return std::unexpected(CellularMigrationError::identity);
if (source.tick() == 0 || source.tick() != destination.tick())
return std::unexpected(CellularMigrationError::tick);
if (!suspended(source) || !suspended(destination))
return std::unexpected(CellularMigrationError::ineligible);
for (const auto& life : source.lives())
if (std::ranges::find(destination.lives(), life.identity.life, [](const auto& other) {
return other.identity.life;
}) != destination.lives().end())
return std::unexpected(CellularMigrationError::identity);
std::vector<CellularOwnedLife> moving, remaining;
moving.reserve(source.lives().size());
remaining.reserve(source.lives().size());
std::vector<std::uint64_t> members;
members.reserve(source.lives().size());
accounting::Inventory transferred;
for (const auto& life : source.lives()) {
if (life.account == LifeAccount{} && life.identity.genome == genome) {
moving.push_back(life);
members.push_back(life.identity.life);
if (!add(transferred, life.body) || !add(transferred, life.reserve))
return std::unexpected(CellularMigrationError::overflow);
} else
remaining.push_back(life);
}
if (moving.empty())
return std::unexpected(CellularMigrationError::ineligible);
if (destination.lives().size() + moving.size() > biology::max_cellular_tick_lives)
return std::unexpected(CellularMigrationError::capacity);
const auto area = destination.area();
for (const auto& life : moving) {
const auto size = extent(source, life);
if (!size)
return std::unexpected(size.error());
if (*size == 0 || *size > area.width_um || *size > area.height_um ||
life.position.x_um > area.width_um - *size ||
life.position.y_um > area.height_um - *size)
return std::unexpected(CellularMigrationError::placement);
for (const auto& other : destination.lives()) {
if (other.presence == CellularPresence::suspended)
continue;
const auto other_size = extent(destination, other);
if (!other_size)
return std::unexpected(other_size.error());
if (overlaps(life.position, *size, other.position, *other_size))
return std::unexpected(CellularMigrationError::placement);
}
}
auto merged = destination.archive().merge_living_from(source.archive(), members);
if (!merged)
return std::unexpected(merged.error() == biology::HistoryError::archive_full
? CellularMigrationError::capacity
: CellularMigrationError::archive);
auto source_ledger = source.boundary_ledger(),
destination_ledger = destination.boundary_ledger();
if (!add(source_ledger.outgoing, transferred) || !add(destination_ledger.incoming, transferred))
return std::unexpected(CellularMigrationError::overflow);
source_ledger.nonresident_organisms.insert(source_ledger.nonresident_organisms.end(),
members.begin(), members.end());
std::ranges::sort(source_ledger.nonresident_organisms);
std::erase_if(destination_ledger.nonresident_organisms,
[&](auto id) { return std::ranges::binary_search(members, id); });
std::vector<CellularOwnedLife> received{destination.lives().begin(), destination.lives().end()};
received.reserve(destination.lives().size() + moving.size());
received.insert(received.end(), moving.begin(), moving.end());
std::ranges::sort(received, {}, [](const auto& life) { return life.identity.life; });
auto after_source = source.migration_replacement(std::move(remaining), source.archive(),
std::move(source_ledger));
if (!after_source)
return std::unexpected(after_source.error() == CellularLivesError::overflow
? CellularMigrationError::overflow
: CellularMigrationError::conservation);
auto after_destination = destination.migration_replacement(std::move(received), *merged,
std::move(destination_ledger));
if (!after_destination)
return std::unexpected(after_destination.error() == CellularLivesError::overflow
? CellularMigrationError::overflow
: CellularMigrationError::conservation);
return CellularMigrationExecution{genome, std::move(members), transferred,
std::move(*after_source), std::move(*after_destination)};
}
std::expected<CellularPopulationMigrationExecution, CellularMigrationError>
CellularMigration::prepare(const CellularPopulation& source, const CellularPopulation& destination,
std::uint64_t genome) {
auto biological = prepare(source.state(), destination.state(), genome);
if (!biological)
return std::unexpected(biological.error());
auto after_source = CellularPopulation::create(std::move(biological->source), source.policy());
auto after_destination =
CellularPopulation::create(std::move(biological->destination), destination.policy());
if (!after_source || !after_destination)
return std::unexpected(CellularMigrationError::version);
(*after_source)->members_.reserve(source.members().size());
(*after_destination)->members_ = destination.members_;
(*after_destination)->members_.reserve(destination.members().size() + source.members().size());
for (const auto member : source.members()) {
if (std::ranges::binary_search(biological->members, member.life))
(*after_destination)->members_.push_back(member);
else
(*after_source)->members_.push_back(member);
}
std::ranges::sort((*after_destination)->members_);
(*after_source)->conversion_sequence_ = source.conversion_sequence();
(*after_destination)->conversion_sequence_ = destination.conversion_sequence();
return CellularPopulationMigrationExecution{genome, std::move(biological->members),
biological->transferred, std::move(*after_source),
std::move(*after_destination)};
}
} // namespace evo::server
@@ -15,6 +15,8 @@ CellularPopulation::CellularPopulation(std::unique_ptr<CellularLives> state,
std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
CellularPopulation::create(std::unique_ptr<CellularLives> state, CellularTierPolicy policy) {
if (!state || state->rules_version() != cellular_lives_foraging_rules ||
(state->format_version() != cellular_lives_presence_format &&
state->format_version() != cellular_lives_migration_format) ||
policy.materialize_radius_um == 0 ||
policy.collapse_radius_um <= policy.materialize_radius_um ||
policy.collapse_radius_um > biology::cellular_motion_axis_limit_um)
@@ -36,6 +38,21 @@ CellularTierPolicy CellularPopulation::policy() const noexcept {
std::uint64_t CellularPopulation::conversion_sequence() const noexcept {
return conversion_sequence_;
}
std::uint32_t CellularPopulation::format_version() const noexcept {
return state_->format_version() == cellular_lives_migration_format ? 2U : 1U;
}
std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
CellularPopulation::migration_upgrade_candidate() const {
auto biological = state_->migration_upgrade_candidate();
if (!biological)
return std::unexpected(CellularPopulationError::unsupported);
auto candidate = create(std::move(*biological), policy_);
if (!candidate)
return std::unexpected(candidate.error());
(*candidate)->members_ = members_;
(*candidate)->conversion_sequence_ = conversion_sequence_;
return candidate;
}
std::expected<std::size_t, CellularPopulationError>
CellularPopulation::collapse(std::uint64_t genome) {
auto candidate = members_;
@@ -117,6 +117,10 @@ unattended_population_inputs(const CellularLives& base, const CellularBatchHeade
std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
execute_population_batch(const CellularPopulation& base, CellularPopulationBatch& batch,
bool record) {
// Trace 1 is retained for envelope 1. A future durable migration adapter must
// bind the new owner/envelope tuple explicitly before recording its history.
if (base.format_version() != 1)
return std::unexpected(CellularPopulationError::unsupported);
if (!shape(batch, record) || batch.header.world != base.state().world() ||
batch.header.region != base.state().region() ||
batch.header.frame != base.state().frame() ||
@@ -5,7 +5,8 @@
namespace evo::server {
namespace {
constexpr std::uint64_t magic = 0x31504F504F564541ULL; // "AEVOPOP1"
constexpr std::uint64_t magic = 0x31504F504F564541ULL; // "AEVOPOP1"
constexpr std::uint64_t migration_magic = 0x32504F504F564541ULL; // "AEVOPOP2"
std::uint64_t checksum(std::span<const std::uint8_t> bytes) {
std::uint64_t hash = 14695981039346656037ULL;
for (const auto byte : bytes)
@@ -38,8 +39,8 @@ std::vector<std::uint8_t> CellularPopulation::snapshot() const {
const auto biology = state_->snapshot();
std::vector<std::uint8_t> bytes;
bytes.reserve(cellular_population_header_bytes + members_.size() * 16 + biology.size() + 8);
put(bytes, magic, 8);
put(bytes, 1, 4);
put(bytes, format_version() == 2 ? migration_magic : magic, 8);
put(bytes, format_version(), 4);
put(bytes, policy_.automatic ? 1 : 0, 4);
put(bytes, policy_.materialize_radius_um, 4);
put(bytes, policy_.collapse_radius_um, 4);
@@ -66,9 +67,11 @@ CellularPopulation::restore(std::span<const std::uint8_t> bytes, std::uint64_t w
if (tail.get(8) != checksum(bytes.first(bytes.size() - 8)))
return std::unexpected(CellularPopulationError::checksum);
Reader reader(bytes);
if (reader.get(8) != magic)
const auto saved_magic = reader.get(8);
if (saved_magic != magic && saved_magic != migration_magic)
return std::unexpected(CellularPopulationError::format);
if (reader.get(4) != 1)
const auto version = reader.get(4);
if ((saved_magic == magic && version != 1) || (saved_magic == migration_magic && version != 2))
return std::unexpected(CellularPopulationError::unsupported);
const auto flags = reader.get(4);
if (flags > 1)
@@ -104,6 +107,8 @@ CellularPopulation::restore(std::span<const std::uint8_t> bytes, std::uint64_t w
auto candidate = create(std::move(*biological), policy);
if (!candidate)
return std::unexpected(candidate.error());
if ((*candidate)->format_version() != version)
return std::unexpected(CellularPopulationError::unsupported);
for (const auto& member : members) {
const auto life = std::ranges::find((*candidate)->state().lives(), member.life,
[](const auto& cell) { return cell.identity.life; });