Bud unowned wildlife from the cellular owner under rules 4

Owner format 5 / rules 4 (ADR-0125) adds the bud intent: after the tick's
mutations a parent pays fund_bud's investment and provision, and the bud is
placed touching it with the next region-namespace identity, the parent's
current genome and its parent life. Buds are wildlife with no account and
never earn mutation offers; restore enforces the identity namespace and
sequence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-13 23:20:35 -04:00
co-authored by Claude Opus 5.5
parent 972c19843e
commit 66632f53b3
7 changed files with 427 additions and 45 deletions
+11
View File
@@ -48,3 +48,14 @@ descendant, and no special wildlife biology.
largest-fit growth, the provision, determinism, a refusal for every short
channel (abundant energy never pays for matter), partial growth, damage and a
genome without the capability.
- 11.5b: owner format 5 / rules 4 (`CellularLivesProfile::budding`). A
`CellularStepIntent::bud` (rules 4 only, exclusive with feeding and
engulfing) funds a bud after the tick's mutations through `fund_bud`, places
it touching the parent (eight fixed directions), gives it the next
region-namespace identity and the parent's current genome, and records its
archive birth and parent life. Results are reported per parent
(`CellularBudExecution`, tick feedback). Wildlife (zero account) never earns
offers. Validation and restore enforce the identity namespace and sequence;
`cellular-lives-owner` buds a fed founder, checks the refusals spend nothing,
conservation, the shared genome's two carriers, wildlife feeding without
offers, exact restore and a rewound sequence; rules 3 refuses a bud intent.
+12
View File
@@ -7351,3 +7351,15 @@ not-grown, damaged and no-capability refusals.
Executed locally: MSVC Debug `budding-contracts`; g++-14 `-Werror`,
clang-tidy-19 and clang-format 19.1.5 on the new sources.
## 2026-10-13 — M11.5b owner budding and wildlife (CI pending)
The cellular owner gains format 5 / rules 4 (ADR-0125, schema doc updated):
bud intents, `fund_bud` funding, touching placement, region-namespace
identities with a durable sequence, archive birth of the parent's genome,
parent life on lives and corpses, and unowned wildlife without mutation
offers. `cellular-lives-owner` adds `rules_four_owner_buds_wildlife` and a rules-3
refusal of a bud intent.
Executed locally: MSVC Debug `ctest` 69/69; g++-14 `-Werror`, clang-tidy-19 and
clang-format 19.1.5 on the owner, region step and owner tests.
+3
View File
@@ -12,6 +12,9 @@ struct CellularStepIntent {
// ADR-0124: take one smaller contacted cell after every life's step (rules 3 only).
// Exclusive with an intake target; the server chooses the prey.
bool engulf{};
// ADR-0125: fund and place one wildlife bud after the tick's mutations (rules 4 only).
// Exclusive with an intake target and with engulfment; the owner decides everything.
bool bud{};
auto operator<=>(const CellularStepIntent&) const = default;
};
@@ -22,6 +22,21 @@ inline constexpr std::uint32_t cellular_lives_rules = 2;
// corpse detritus. They are used together, with the revision-format archive.
inline constexpr std::uint32_t cellular_lives_engulf_format = 4;
inline constexpr std::uint32_t cellular_lives_engulf_rules = 3;
// ADR-0125: format 5 adds the region's bud sequence and each life's and corpse's parent
// life; rules 4 add funded budding of unowned wildlife on top of rules 3.
inline constexpr std::uint32_t cellular_lives_bud_format = 5;
inline constexpr std::uint32_t cellular_lives_bud_rules = 4;
// A bud's life, organism and event identity: 2^63 + region * 2^32 + sequence. The world
// allocator stays at or below 2^63 (schema v36), so the namespaces never meet.
inline constexpr std::uint64_t cellular_bud_namespace = std::uint64_t{1} << 63U;
inline constexpr std::uint64_t cellular_bud_max_region = (std::uint64_t{1} << 31U) - 1;
[[nodiscard]] constexpr std::uint64_t cellular_bud_identity(std::uint64_t region,
std::uint64_t sequence) noexcept {
return region == 0 || region > cellular_bud_max_region || sequence == 0 ||
sequence > 0xFFFF'FFFFULL
? 0
: cellular_bud_namespace + (region << 32U) + sequence;
}
inline constexpr std::size_t max_cellular_lives_bytes = std::size_t{16} * 1024 * 1024;
inline constexpr std::uint64_t cellular_mutation_surplus_seconds = 60;
inline constexpr std::uint16_t cellular_mutation_candidate_count = 3;
@@ -44,6 +59,7 @@ struct CellularOwnedLife {
accounting::Inventory body{};
accounting::Inventory reserve{};
CellularMutationProgress mutation{};
std::uint64_t parent_life{}; // Format 5: the budding parent; zero for a founder.
auto operator<=>(const CellularOwnedLife&) const = default;
};
struct CellularOwnedCorpse {
@@ -57,6 +73,7 @@ struct CellularOwnedCorpse {
accounting::Inventory inventory{}; // Empty under rules 3: it became detritus.
biology::CellularDeathCause cause{biology::CellularDeathCause::unrecorded};
std::uint64_t predator_life{}; // Engulfed only.
std::uint64_t parent_life{}; // Format 5: kept after death.
auto operator<=>(const CellularOwnedCorpse&) const = default;
};
struct CellularLifeIntent {
@@ -75,9 +92,22 @@ struct CellularMutationExecution {
CellularMutationResult result{};
auto operator<=>(const CellularMutationExecution&) const = default;
};
enum class CellularBudResult : std::uint8_t {
budded,
life_died, // The parent died earlier in the tick.
not_able, // No reproduction capability, not fully grown, or damaged.
insufficient_funding, // Some channel of investment plus provision is missing.
no_placement // No free spot touching the parent, or no identity left.
};
struct CellularBudExecution {
std::uint64_t parent_life{}, bud_life{}; // bud_life is zero unless budded.
CellularBudResult result{};
auto operator<=>(const CellularBudExecution&) const = default;
};
struct CellularTickExecution {
biology::CellularRegionStepCandidate step;
std::vector<CellularMutationExecution> mutations;
std::vector<CellularBudExecution> buds; // Rules 4, in parent life order.
};
struct CellularLifeTickFeedback {
std::uint64_t life_id{}, tick{};
@@ -87,11 +117,14 @@ struct CellularLifeTickFeedback {
biology::CellularIntakeStatus intake{biology::CellularIntakeStatus::not_requested};
biology::CellularEngulfStatus engulf{biology::CellularEngulfStatus::not_requested};
std::uint64_t prey_life{};
std::optional<CellularBudResult> bud; // Rules 4, when this life asked to bud.
std::uint64_t bud_life{};
auto operator<=>(const CellularLifeTickFeedback&) const = default;
};
// Which owner version a new region starts on. Offers: format 2, rules 2 (the M11.3/11.4
// trial, upgraded by the no-tick migrations). Engulf: format 4, rules 3 (ADR-0124).
enum class CellularLivesProfile : std::uint8_t { offers, engulf };
// Budding: format 5, rules 4 (ADR-0125).
enum class CellularLivesProfile : std::uint8_t { offers, engulf, budding };
struct CellularLivesConfig {
biology::CellularArea area{};
std::uint64_t tick{};
@@ -139,6 +172,8 @@ class CellularLives {
[[nodiscard]] std::uint64_t tick() const noexcept;
[[nodiscard]] std::uint32_t format_version() const noexcept;
[[nodiscard]] std::uint32_t rules_version() const noexcept;
// Format 5: the sequence the next bud's identity will use (ADR-0125).
[[nodiscard]] std::uint64_t next_bud_sequence() const noexcept;
[[nodiscard]] std::uint32_t ambient_temperature_millikelvin() const noexcept;
[[nodiscard]] biology::CellularArea area() const noexcept;
[[nodiscard]] std::span<const CellularOwnedLife> lives() const noexcept;
+14
View File
@@ -72,6 +72,20 @@ cause. The checkpoint manifest pins these as cellular version bundle
founder 1, archive 2, mutation_state 1}` with the revision history profile
(schema v34).
### Format 5 (rules 4, ADR-0125)
Magic `AEVCLV05`, format 5, rules 4: format 4 plus budding. The envelope header
is 220 bytes: the 212 above, then the u64 next bud sequence (at least 1). Each
life record is 488 bytes, the format 2/3 record followed by the u64 parent life;
each corpse record is 124 bytes, the format 4 record followed by the u64 parent
life. A founder has parent 0, an account and an allocated identity below 2^63. A
bud has a non-zero parent, the zero account, an empty mutation progress and
equal life, organism and event identities of the form
`2^63 + region * 2^32 + sequence` with `1 <= sequence < next bud sequence`;
regions above 2^31 - 1 have no bud identities. Restore refuses a bud identity
outside that range, an owned bud, an unowned founder, and wildlife with
mutation progress.
## Restore validation
The decoder checks envelope bounds and checksum, identity/tick, versions,
+247 -43
View File
@@ -1,7 +1,9 @@
#include "evo/server/lifecycle/cellular_lives.hpp"
#include "evo/biology/budding.hpp"
#include "evo/biology/founder.hpp"
#include <algorithm>
#include <array>
#include <bit>
#include <limits>
#include <ranges>
@@ -14,6 +16,7 @@ constexpr std::uint64_t cellular_lives_v1_magic = 0x3130564C43564541ULL; // AEVC
constexpr std::uint64_t cellular_lives_v2_magic = 0x3230564C43564541ULL; // AEVCLV02
constexpr std::uint64_t cellular_lives_v3_magic = 0x3330564C43564541ULL; // AEVCLV03
constexpr std::uint64_t cellular_lives_v4_magic = 0x3430564C43564541ULL; // AEVCLV04
constexpr std::uint64_t cellular_lives_v5_magic = 0x3530564C43564541ULL; // AEVCLV05
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;
@@ -21,6 +24,11 @@ constexpr std::size_t cellular_life_v2_bytes = 480;
constexpr std::size_t cellular_corpse_bytes = 100;
// Format 4: cause (1), 7 zero bytes and the predator's life id (8).
constexpr std::size_t cellular_corpse_v4_bytes = cellular_corpse_bytes + 16;
// Format 5 (ADR-0125): the header adds the next bud sequence, each life and each corpse
// its parent life (8 bytes each).
constexpr std::size_t cellular_lives_v5_header_bytes = cellular_lives_header_bytes + 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;
std::uint64_t checksum(std::span<const std::uint8_t> bytes) noexcept {
std::uint64_t value = 14695981039346656037ULL;
@@ -144,6 +152,58 @@ budget_for(const biology::LineageArchive& archive, const CellularOwnedLife& life
return *budget;
}
bool separated(std::uint64_t left, std::uint64_t right, std::uint32_t left_extent,
std::uint32_t right_extent) noexcept {
return left < right ? right - left >= left_extent : left - right >= right_extent;
}
// ADR-0125 placement: touching the parent, centres (parent + bud extent) / 2 + 1 apart on
// each moved axis, trying east and then counter-clockwise in 45-degree steps. The first
// spot inside the area and clear of every living cell wins; none is a refusal.
std::expected<std::optional<biology::CellularPosition>, CellularLivesError>
bud_position(const CellularOwnedLife& parent, std::uint32_t parent_extent, std::uint32_t bud_extent,
std::span<const CellularOwnedLife> lives, const biology::LineageArchive& archive,
const CellularLivesConfig& config) {
constexpr std::array<std::array<std::int64_t, 2>, 8> directions{
{{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}};
if (bud_extent == 0 || bud_extent > config.area.width_um || bud_extent > config.area.height_um)
return std::optional<biology::CellularPosition>{};
std::vector<std::uint32_t> extents;
extents.reserve(lives.size());
for (const auto& living : lives) {
const auto budget = budget_for(archive, living, config.ambient_temperature_millikelvin);
if (!budget)
return std::unexpected{budget.error()};
extents.push_back(budget->phenotype.extent);
}
// Doubled centre coordinates keep half extents exact.
const auto step = std::int64_t{parent_extent} + bud_extent + 2;
// Twice the bud's corner coordinate on one axis; negative is outside the area.
const auto doubled_corner = [&](std::uint64_t parent_corner, std::int64_t direction) {
return 2 * static_cast<std::int64_t>(parent_corner) + parent_extent + direction * step -
std::int64_t{bud_extent};
};
for (const auto& [dx, dy] : directions) {
const auto twice_x = doubled_corner(parent.position.x_um, dx);
const auto twice_y = doubled_corner(parent.position.y_um, dy);
const auto x = twice_x / 2;
const auto y = twice_y / 2;
if (twice_x < 0 || twice_y < 0 ||
static_cast<std::uint64_t>(x) > config.area.width_um - bud_extent ||
static_cast<std::uint64_t>(y) > config.area.height_um - bud_extent)
continue;
const biology::CellularPosition candidate{static_cast<std::uint64_t>(x),
static_cast<std::uint64_t>(y)};
bool clear = true;
for (std::size_t i = 0; i < lives.size() && clear; ++i)
clear = separated(candidate.x_um, lives[i].position.x_um, bud_extent, extents[i]) ||
separated(candidate.y_um, lives[i].position.y_um, bud_extent, extents[i]);
if (clear)
return std::optional{candidate};
}
return std::optional<biology::CellularPosition>{};
}
std::expected<accounting::Inventory, CellularLivesError>
sum_state(accounting::Inventory nursery, accounting::Inventory heat,
const biology::CellularHabitat& habitat, std::span<const CellularOwnedLife> lives,
@@ -168,7 +228,20 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
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, bool detritus_rules = false) {
accounting::Inventory opening_total, bool detritus_rules = false,
bool bud_rules = false, std::uint64_t next_bud_sequence = 1) {
// 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 {
const auto first = cellular_bud_identity(region, 1);
return bud_rules && first != 0 && next_bud_sequence > 1 && identity >= first &&
identity - first < next_bud_sequence - 1;
};
const auto lineage_valid = [&](std::uint64_t life_id, std::uint64_t parent) noexcept {
if (parent == 0)
return life_id < cellular_bud_namespace;
return bud_identity(life_id) && parent != life_id;
};
const auto& map = habitat.config();
if (world == 0 || region == 0 || frame == 0 || nursery_config.world != world ||
nursery_config.region != region || nursery_config.frame != frame ||
@@ -216,13 +289,19 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
life_ids.reserve(lives.size() + corpses.size());
for (std::size_t i = 0; i < lives.size(); ++i) {
const auto& life = lives[i];
if (life.account == LifeAccount{} || life.identity.life == 0 ||
// Founders are owned; buds are unowned wildlife (rules 4) and never earn offers.
const bool wildlife = life.parent_life != 0;
if ((life.account == LifeAccount{}) != wildlife || 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.source == 0 || (i != 0 && lives[i - 1].identity.life >= life.identity.life) ||
!lineage_valid(life.identity.life, life.parent_life) ||
(wildlife && (life.identity.organism != life.identity.life ||
life.identity.event != life.identity.life ||
life.mutation != CellularMutationProgress{})))
return std::unexpected{CellularLivesError::invalid_life};
for (std::size_t j = 0; j < i; ++j)
if (lives[j].account == life.account ||
if ((!wildlife && lives[j].account == life.account) ||
lives[j].identity.organism == life.identity.organism)
return std::unexpected{CellularLivesError::duplicate_life};
const auto record = archive.organism(life.identity.organism);
@@ -248,7 +327,8 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
const auto& corpse = corpses[i];
if (corpse.life_id == 0 || corpse.organism == 0 || corpse.source == 0 ||
corpse.died_tick == 0 || corpse.died_tick > config.tick || corpse.extent_um == 0 ||
(i != 0 && corpses[i - 1].organism >= corpse.organism))
(i != 0 && corpses[i - 1].organism >= corpse.organism) ||
!lineage_valid(corpse.life_id, corpse.parent_life))
return std::unexpected{CellularLivesError::invalid_life};
const auto record = archive.organism(corpse.organism);
if (!record || record->alive || record->died.value != corpse.died_tick)
@@ -303,6 +383,7 @@ struct CellularLives::State {
accounting::Inventory heat{};
accounting::Inventory opening_total{};
accounting::Inventory current_total{};
std::uint64_t next_bud_sequence{1}; // Format 5: the next bud identity's sequence.
State(std::uint64_t world_id, std::uint64_t region_id, std::uint64_t frame_id,
CellularLivesConfig cfg, NurseryConfig nursery_definition,
@@ -391,14 +472,20 @@ CellularLives::create(const PrimitiveLives& opening,
const auto world = nursery_config.world;
const auto region = nursery_config.region;
const auto frame = nursery_config.frame;
const bool engulf = profile == CellularLivesProfile::engulf;
const bool budding = profile == CellularLivesProfile::budding;
const bool engulf = profile == CellularLivesProfile::engulf || budding;
if (engulf)
archive.upgrade_to_revision_format(); // Rules 3 regions carry genome revisions.
auto state = std::make_unique<State>(
world, region, frame, config, std::move(nursery_config), std::move(habitat),
std::move(lives), std::vector<CellularOwnedCorpse>{}, archive, nursery, heat, opening_total,
*current, engulf ? cellular_lives_engulf_format : cellular_lives_format,
engulf ? cellular_lives_engulf_rules : cellular_lives_rules);
archive.upgrade_to_revision_format(); // Rules 3+ regions carry genome revisions.
auto state = std::make_unique<State>(world, region, frame, config, std::move(nursery_config),
std::move(habitat), std::move(lives),
std::vector<CellularOwnedCorpse>{}, archive, nursery, heat,
opening_total, *current,
budding ? cellular_lives_bud_format
: engulf ? cellular_lives_engulf_format
: cellular_lives_format,
budding ? cellular_lives_bud_rules
: engulf ? cellular_lives_engulf_rules
: cellular_lives_rules);
return std::unique_ptr<CellularLives>{new CellularLives{std::move(state)}};
}
@@ -420,6 +507,9 @@ std::uint32_t CellularLives::format_version() const noexcept {
std::uint32_t CellularLives::rules_version() const noexcept {
return state_->rules_version;
}
std::uint64_t CellularLives::next_bud_sequence() const noexcept {
return state_->next_bud_sequence;
}
std::uint32_t CellularLives::ambient_temperature_millikelvin() const noexcept {
return state_->config.ambient_temperature_millikelvin;
}
@@ -460,12 +550,18 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
const auto site_count = state_->nursery_config.sites.size();
const auto life_count = state_->lives.size();
const auto corpse_count = state_->corpses.size();
const bool parents = state_->format_version == cellular_lives_bud_format;
const auto life_bytes = state_->format_version == cellular_lives_legacy_format
? cellular_life_v1_bytes
: cellular_life_v2_bytes;
const bool causes = state_->format_version == cellular_lives_engulf_format;
const auto corpse_bytes = causes ? cellular_corpse_v4_bytes : cellular_corpse_bytes;
const auto total_bytes = cellular_lives_header_bytes + cellular_site_bytes * site_count +
: parents ? cellular_life_v5_bytes
: cellular_life_v2_bytes;
const bool causes = state_->format_version >= cellular_lives_engulf_format;
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 auto total_bytes = header_bytes + cellular_site_bytes * site_count +
life_bytes * life_count + corpse_bytes * corpse_count +
habitat.size() + archive.size() + 8;
if (total_bytes > max_cellular_lives_bytes ||
@@ -482,6 +578,7 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
state_->format_version == cellular_lives_legacy_format ? cellular_lives_v1_magic
: state_->format_version == cellular_lives_revision_format ? cellular_lives_v3_magic
: state_->format_version == cellular_lives_engulf_format ? cellular_lives_v4_magic
: state_->format_version == cellular_lives_bud_format ? cellular_lives_v5_magic
: cellular_lives_v2_magic;
out.put(magic);
out.put(state_->format_version, 4);
@@ -503,6 +600,8 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
out.inventory(state_->opening_total);
out.inventory(state_->nursery);
out.inventory(state_->heat);
if (parents)
out.put(state_->next_bud_sequence);
for (const auto site : state_->nursery_config.sites)
out.site(site);
for (const auto& life : state_->lives) {
@@ -554,6 +653,8 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
}
}
}
if (parents)
out.put(life.parent_life);
}
for (const auto& corpse : state_->corpses) {
out.put(corpse.life_id);
@@ -571,6 +672,8 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
out.zeros(7);
out.put(corpse.predator_life);
}
if (parents)
out.put(corpse.parent_life);
}
out.raw(habitat);
out.raw(archive);
@@ -749,10 +852,11 @@ 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);
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);
if (!current)
return std::unexpected{current.error()};
@@ -761,6 +865,7 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
std::move(*habitat), std::move(lives), state_->corpses, archive, nursery, heat,
state_->opening_total, *current, state_->format_version, state_->rules_version)}};
candidate->last_tick_feedback_ = last_tick_feedback_;
candidate->state_->next_bud_sequence = state_->next_bud_sequence;
return candidate;
}
@@ -782,9 +887,13 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
const bool offer_version = magic == cellular_lives_v2_magic &&
format_version == cellular_lives_format &&
rules_version == cellular_lives_rules;
const bool engulf_version = magic == cellular_lives_v4_magic &&
format_version == cellular_lives_engulf_format &&
rules_version == cellular_lives_engulf_rules;
const bool bud_version = magic == cellular_lives_v5_magic &&
format_version == cellular_lives_bud_format &&
rules_version == cellular_lives_bud_rules;
// Formats 4 and 5 record death causes and leave detritus (rules 3 and 4).
const bool engulf_version = bud_version || (magic == cellular_lives_v4_magic &&
format_version == cellular_lives_engulf_format &&
rules_version == cellular_lives_engulf_rules);
const bool revision_version =
engulf_version ||
(magic == cellular_lives_v3_magic && format_version == cellular_lives_revision_format &&
@@ -821,12 +930,18 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
const auto opening_total = in.inventory();
const auto nursery = in.inventory();
const auto heat = in.inventory();
const auto life_bytes = legacy ? cellular_life_v1_bytes : cellular_life_v2_bytes;
const auto corpse_bytes = engulf_version ? cellular_corpse_v4_bytes : cellular_corpse_bytes;
const auto expected_size = cellular_lives_header_bytes + cellular_site_bytes * site_count +
life_bytes * life_count + corpse_bytes * corpse_count +
habitat_size + archive_size + 8;
if (source == 0 || expected_size != bytes.size())
const auto next_bud_sequence = bud_version ? in.get() : std::uint64_t{1};
const auto life_bytes = legacy ? cellular_life_v1_bytes
: bud_version ? cellular_life_v5_bytes
: cellular_life_v2_bytes;
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;
if (source == 0 || next_bud_sequence == 0 || expected_size != bytes.size())
return std::unexpected{CellularLivesError::format};
NurseryConfig nursery_config{saved_world, saved_region, saved_frame, source, nursery, {}};
@@ -898,6 +1013,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
mutation.offer = std::move(offer);
}
}
if (bud_version)
life.parent_life = in.get();
lives.push_back(life);
}
@@ -928,6 +1045,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
corpse.inventory != accounting::Inventory{})
return std::unexpected{CellularLivesError::noncanonical};
}
if (bud_version)
corpse.parent_life = in.get();
corpses.push_back(corpse);
}
const auto habitat_payload = in.raw(static_cast<std::size_t>(habitat_size));
@@ -977,9 +1096,9 @@ 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);
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);
if (!current)
return std::unexpected{current.error()};
@@ -987,6 +1106,7 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
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)}};
restored->state_->next_bud_sequence = next_bud_sequence;
if (!std::ranges::equal(bytes, restored->snapshot()))
return std::unexpected{CellularLivesError::noncanonical};
return restored;
@@ -1017,7 +1137,14 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
if (!selections.empty() && state_->format_version < cellular_lives_revision_format)
return std::unexpected{CellularLivesError::version};
const bool engulf_rules = state_->rules_version == cellular_lives_engulf_rules;
// Rules 4 keep rules 3 (engulfment, detritus) and add budding (ADR-0125).
const bool engulf_rules = state_->rules_version >= cellular_lives_engulf_rules;
const bool bud_rules = state_->rules_version == cellular_lives_bud_rules;
for (const auto& ordered_intent : ordered) {
const auto& intent = ordered_intent.intent;
if (intent.bud && (!bud_rules || intent.intake_target || intent.engulf))
return std::unexpected{CellularLivesError::invalid_intents};
}
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 ||
@@ -1091,7 +1218,9 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
found->reserve = result.candidate.reserve_after;
if (!add_inventory(next_heat, result.candidate.heat_after))
return std::unexpected{CellularLivesError::overflow};
if (state_->format_version >= cellular_lives_format && result.candidate.intake) {
// Only owned lives earn mutation offers; wildlife never mutates in M11 (ADR-0098).
if (state_->format_version >= cellular_lives_format && result.candidate.intake &&
found->account != LifeAccount{}) {
const auto acquired = result.candidate.intake->transfer.acquired.energy.value;
if (acquired >
std::numeric_limits<std::uint64_t>::max() - found->mutation.fed_energy_credit_pj)
@@ -1150,7 +1279,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
proposal->end_tick, dead.position, found->growth,
corpse_budget->phenotype.extent, dead.inventory,
engulf_rules ? dead.cause : biology::CellularDeathCause::unrecorded,
engulf_rules ? dead.predator_id : 0});
engulf_rules ? dead.predator_id : 0, found->parent_life});
next_lives.erase(found);
}
std::vector<CellularMutationExecution> mutation_results;
@@ -1194,6 +1323,72 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
mutation_results.push_back(
{selection.life_id, selection.successor_id, CellularMutationResult::applied});
}
// Rules 4 budding (ADR-0125), after mutations and in parent life order: a parent that
// died this tick cannot bud, and a bud born this tick asked for nothing.
std::vector<CellularBudExecution> bud_results;
auto next_bud_sequence = state_->next_bud_sequence;
for (const auto& request : ordered) {
if (!request.intent.bud)
continue;
const auto parent = std::ranges::lower_bound(
next_lives, request.life_id, {},
[](const CellularOwnedLife& value) { return value.identity.life; });
if (parent == next_lives.end() || parent->identity.life != request.life_id) {
bud_results.push_back({request.life_id, 0, CellularBudResult::life_died});
continue;
}
const auto temperature = state_->config.ambient_temperature_millikelvin;
const auto genome = next_archive.find(parent->identity.genome);
const auto parent_budget = budget_for(next_archive, *parent, temperature);
if (!genome || !parent_budget)
return std::unexpected{CellularLivesError::archive};
const auto transition =
biology::fund_bud(*genome, parent->growth, parent->reserve, temperature);
if (!transition) {
if (transition.error() == biology::BuddingError::development)
return std::unexpected{CellularLivesError::biology};
bud_results.push_back({request.life_id, 0,
transition.error() == biology::BuddingError::insufficient_funding
? CellularBudResult::insufficient_funding
: CellularBudResult::not_able});
continue;
}
const auto bud_budget =
biology::metabolic_budget(*genome, transition->growth, {temperature, {}});
if (!bud_budget || bud_budget->body.matter != transition->body.matter)
return std::unexpected{CellularLivesError::biology};
const auto identity = cellular_bud_identity(state_->region, next_bud_sequence);
const auto position =
bud_position(*parent, parent_budget->phenotype.extent, bud_budget->phenotype.extent,
next_lives, next_archive, state_->config);
if (!position)
return std::unexpected{position.error()};
if (identity == 0 || !*position || next_lives.size() >= biology::max_cellular_tick_lives ||
next_archive.organism(identity)) {
bud_results.push_back({request.life_id, 0, CellularBudResult::no_placement});
continue;
}
if (!add_inventory(next_heat, accounting::Inventory{transition->heat, {}}) ||
!next_archive.record_birth(identity, parent->identity.genome, {proposal->end_tick}))
return std::unexpected{CellularLivesError::archive};
parent->reserve = transition->parent_reserve;
CellularOwnedLife bud{};
bud.identity = {
identity, identity, parent->identity.genome, identity, {proposal->end_tick}};
bud.source = parent->source;
bud.growth = transition->growth;
bud.position = **position;
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 and `parent` stays valid.
next_lives.push_back(bud);
++next_bud_sequence;
bud_results.push_back({request.life_id, identity, CellularBudResult::budded});
}
std::ranges::sort(next_lives, {},
[](const CellularOwnedLife& life) { return life.identity.life; });
std::ranges::sort(next_corpses, {}, &CellularOwnedCorpse::organism);
if (proposal->heat_released.value >
std::numeric_limits<std::uint64_t>::max() - next_heat.energy.value)
@@ -1218,12 +1413,13 @@ 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);
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);
if (!total)
return std::unexpected{total.error()};
@@ -1234,6 +1430,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
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);
next->next_bud_sequence = next_bud_sequence;
std::vector<CellularLifeTickFeedback> feedback;
feedback.reserve(proposal->lives.size());
for (const auto& result : proposal->lives) {
@@ -1243,12 +1440,19 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
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,
result.engulf, result.prey_id});
result.engulf, result.prey_id, std::nullopt, 0});
if (const auto bud =
std::ranges::find(bud_results, result.life_id, &CellularBudExecution::parent_life);
bud != bud_results.end()) {
feedback.back().bud = bud->result;
feedback.back().bud_life = bud->bud_life;
}
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 CellularTickExecution{std::move(*proposal), std::move(mutation_results)};
return CellularTickExecution{std::move(*proposal), std::move(mutation_results),
std::move(bud_results)};
}
} // namespace evo::server
+104 -1
View File
@@ -587,6 +587,11 @@ void rules_three_owner_resolves_engulf_and_detritus() {
both.intake_target = evo::biology::CellularPatchAddress{0, 0};
const std::array conflicting{CellularLifeIntent{1, both}, CellularLifeIntent{2, {}}};
CHECK(!owner.advance_tick(conflicting) && owner.tick() == 2);
// Budding needs rules 4.
evo::biology::CellularStepIntent bud{};
bud.bud = true;
CHECK(owner.advance_tick(std::array{CellularLifeIntent{1, bud}, CellularLifeIntent{2, {}}})
.error() == CellularLivesError::invalid_intents);
const auto patch_before = owner.habitat().patches()[0].stock;
std::size_t steps{};
@@ -629,6 +634,102 @@ void rules_three_owner_resolves_engulf_and_detritus() {
CHECK(CellularLives::restore(bad_predator, 92, 1, 7, owner.tick()).error() ==
CellularLivesError::noncanonical);
}
// ADR-0125: a fed founder buds an unowned wildlife cell carrying its genome. The bud's
// identity is in the region namespace, funding is conserved, and restore is exact.
void rules_four_owner_buds_wildlife() {
auto primitive = PrimitiveLives::create({93, 1, 7, 17, founder_price(1), {{0, 0}}});
CHECK(primitive);
LifeRequest request{};
request.account.back() = 3;
request.action = 1;
CHECK((*primitive)->spawn(request, {1, 101, 201, 301, {1}}));
constexpr std::uint64_t rich = 10'000'000'000;
evo::biology::CellularHabitatConfig habitat_config{
93, 1, 7, 3'000, 2'000, {{{0, 0}, 29, {{rich}, {{{rich}, {rich}, {rich}, {rich}}}}}}};
auto habitat = evo::biology::CellularHabitat::create(std::move(habitat_config));
CHECK(habitat);
auto created =
CellularLives::create(**primitive, std::move(*habitat), {{3'000, 2'000}, 1, 293'150},
CellularLivesProfile::budding);
CHECK(created);
auto& owner = **created;
CHECK(owner.format_version() == cellular_lives_bud_format &&
owner.rules_version() == cellular_lives_bud_rules && owner.next_bud_sequence() == 1);
evo::biology::CellularStepIntent feed{};
feed.intake_target = evo::biology::CellularPatchAddress{0, 0};
evo::biology::CellularStepIntent bud{};
bud.bud = true;
// A bud is exclusive with feeding, and needs rules 4 (checked in the rules 3 test).
auto feeding_bud = feed;
feeding_bud.bud = true;
CHECK(owner.advance_tick(std::array{CellularLifeIntent{1, feeding_bud}}).error() ==
CellularLivesError::invalid_intents);
const auto parent_genome = owner.lives().front().identity.genome;
std::optional<CellularTickExecution> budded;
std::size_t refused{};
for (std::size_t round = 0; round < 400 && !budded; ++round) {
for (int tick = 0; tick < 19; ++tick)
CHECK(owner.advance_tick(std::array{CellularLifeIntent{1, feed}}));
const auto before = owner.lives().front().reserve;
auto attempt = owner.advance_tick_with_mutations(
std::array{CellularLifeIntent{1, bud}}, std::span<const CellularMutationSelection>{});
CHECK(attempt && attempt->buds.size() == 1 && attempt->buds.front().parent_life == 1);
CHECK(owner.current_total() == owner.opening_total());
if (attempt->buds.front().result == CellularBudResult::budded) {
budded = std::move(*attempt);
} else {
// A refusal spends nothing beyond the ordinary tick.
CHECK(attempt->buds.front().result == CellularBudResult::insufficient_funding);
CHECK(owner.lives().size() == 1 &&
owner.lives().front().reserve.matter == before.matter);
++refused;
}
}
CHECK(budded.has_value() && refused > 0);
const auto budding = budded.value_or(CellularTickExecution{});
CHECK(budding.buds.size() == 1);
const auto identity = cellular_bud_identity(1, 1);
CHECK(budding.buds.front().bud_life == identity && owner.next_bud_sequence() == 2);
CHECK(owner.lives().size() == 2);
const auto& parent = owner.lives()[0];
const auto& child = owner.lives()[1];
CHECK(parent.identity.life == 1 && child.identity.life == identity);
CHECK(child.account == LifeAccount{} && child.parent_life == 1 &&
child.identity.genome == parent_genome && child.identity.organism == identity &&
child.identity.born.value == owner.tick() &&
child.mutation == CellularMutationProgress{});
CHECK(child.growth.realized_per_thousand < 1000 && child.reserve.energy.value > 0);
CHECK(owner.archive().living_carriers(parent_genome).value_or(0) == 2);
CHECK(owner.last_tick_feedback().front().bud == CellularBudResult::budded &&
owner.last_tick_feedback().front().bud_life == identity);
CHECK(owner.current_total() == owner.opening_total());
// Wildlife feeds like any cell but never earns a mutation offer.
const auto child_x = child.position.x_um / 1000;
const auto child_y = child.position.y_um / 1000;
evo::biology::CellularStepIntent child_feed{};
child_feed.intake_target = evo::biology::CellularPatchAddress{
static_cast<std::uint32_t>(child_x), static_cast<std::uint32_t>(child_y)};
for (int tick = 0; tick < 40; ++tick)
CHECK(owner.advance_tick(
std::array{CellularLifeIntent{1, feed}, CellularLifeIntent{identity, child_feed}}));
CHECK(owner.lives().size() == 2 && owner.lives()[1].mutation == CellularMutationProgress{});
const auto saved = owner.snapshot();
auto recovered = CellularLives::restore(saved, 93, 1, 7, owner.tick());
CHECK(recovered && (*recovered)->snapshot() == saved &&
(*recovered)->next_bud_sequence() == 2 &&
std::ranges::equal((*recovered)->lives(), owner.lives()));
// A bud identity at or beyond the recorded sequence is refused on restore.
auto rewound = saved;
const auto sequence_at = std::size_t{212};
rewound[sequence_at] = 1;
refresh_checksum(rewound);
CHECK(!CellularLives::restore(rewound, 93, 1, 7, owner.tick()));
}
} // namespace
int main() {
@@ -638,7 +739,9 @@ int main() {
retained_feeding_earns_a_persistent_mutation_offer();
owner_advances_and_closes_death_atomically();
rules_three_owner_resolves_engulf_and_detritus();
std::puts("PASS cellular lives: feeding, tick ownership, terminal closure, rules 3");
rules_four_owner_buds_wildlife();
std::puts(
"PASS cellular lives: feeding, tick ownership, terminal closure, rules 3, budding");
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;