Files
aevolution/include/evo/biology/cellular_region_step.hpp
dingoandClaude Opus 5.5 3c753970c8
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / postgres-tools (push) Successful in 31s
Foundation / server-image (push) Successful in 4m29s
Foundation / image (push) Successful in 5m43s
feat(cellular): region ticks of up to 4,096 lives for testing (schema 68)
ADR-0182 section 17. The plan's 2,000-organism cap became the rules-12
key region.organism_cap, which was parsed but never enforced; the real
bound was max_cellular_tick_lives = 256 (ADR-0106), which rules 13's
organism cap could not exceed. The bound is now 4,096, for testing limits
only; the DEV tuning file keeps 256.

- max_cellular_tick_lives (4,096) bounds the raw region path: step, owner
  state, batch trace (cellular_batch_bytes_for) and current checkpoint
  traces. max_cellular_frozen_lives (256) keeps cohort populations,
  population batches, migration traces and pre-v3 cellular traces, whose
  database checks are fixed.
- Tuning: reproduction.organism_cap and wildlife_cap accept up to 4,096.
- Schema 68: the trace checker under the current publishers
  (cellular_births_v45) accepts 4,096 intents, births and mutations per
  tick; spawn_wildlife takes up to 256 cells a command.
- Region view and portal: the lineage archive's fill (births of 65,536,
  genomes of 4,096), at which a region stops ticking.
- PostgreSQL suite: 256 wildlife spawned in one command, then a 289-life
  tick published and recovered exactly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 17:30:22 -04:00

180 lines
9.1 KiB
C++

#pragma once
#include "evo/biology/cellular_step.hpp"
#include <array>
#include <compare>
#include <cstddef>
#include <cstdint>
#include <expected>
#include <optional>
#include <span>
#include <vector>
namespace evo::biology {
// Lives one region tick may hold (ADR-0182 section 17: 4,096 for testing; regions run far
// fewer). Formats frozen at the original 256 (cohort populations, migration traces and the
// pre-v3 cellular traces) keep max_cellular_frozen_lives.
inline constexpr std::size_t max_cellular_tick_lives = 4'096;
inline constexpr std::size_t max_cellular_frozen_lives = 256;
// ADR-0124 (rules 3): a predator needs total mass >= ratio x prey total mass, with the
// ratio in thousandths per micrometre of prey membrane (tunable; 1.5 for 1 um).
inline constexpr std::uint64_t cellular_engulf_ratio_milli_per_membrane_um = 1'500;
// Rules 13 (ADR-0179; feeding and energy, predation and defense plans): region-declared
// feeding, energy and predation values. All trial values from the revamp tuning file.
struct CellularRevampStepRules {
// Collection changes by this many per mille per membrane point away from the founder's 100
// (thicker collects slower), bounded to 10%-200%.
std::uint32_t membrane_collection_per_mille_per_point{};
// A predator needs engulf mass x capacity / 100 >= prey engulf mass x ratio / 1000 x prey
// membrane points / 100.
std::uint32_t engulf_ratio_milli{1'500};
// Share of reserve matter that counts toward engulf mass, per cent.
std::uint32_t engulf_reserve_percent{100};
// Share of a kill's remainder left as bubbles at the prey; the rest goes to the spent pool.
std::uint32_t kill_bubble_per_thousand{500};
// Every collection limit, per cent of the body's own (100: unchanged).
std::uint32_t collection_percent{100};
// Matter from one food source is split evenly between the nutrients it holds: all of the
// shared matter limit to a carbon-only bubble, half each to carbon and phosphorus, and so
// on (each share then scaled by the cell's collection trait). Off: per-nutrient limits in
// the body's proportions.
bool split_evenly{};
auto operator<=>(const CellularRevampStepRules&) const = default;
};
// Rules 13: engulf mass is the body plus the declared share of reserve matter.
[[nodiscard]] std::uint64_t cellular_engulf_mass(std::uint64_t body_mass,
const accounting::Inventory& reserve,
const CellularRevampStepRules& rules) noexcept;
// Rules 13: whether a hunter of this engulf mass and capacity may take prey of this engulf mass
// and membrane (nanometres), exactly as the region step decides.
[[nodiscard]] bool cellular_engulfable(std::uint64_t hunter_mass, std::uint16_t engulf_capacity,
std::uint64_t prey_mass, std::uint32_t prey_membrane_nm,
const CellularRevampStepRules& rules) noexcept;
// Rules 13: one life's heritable feeding, energy and predation values, from its genome.
struct CellularRevampLifeInput {
CellularBurn burn{};
std::array<std::uint16_t, 5> collection{100, 100, 100, 100, 100}; // E, C, N, P, O.
std::uint32_t energy_kept_per_thousand{1'000}; // Of each energy bubble; the rest is heat.
std::uint32_t membrane_nm{1'000};
std::uint16_t engulf_capacity{100}, kill_absorption{100};
auto operator<=>(const CellularRevampLifeInput&) const = default;
};
struct CellularRegionRules {
// Rules 3: engulfment, and every corpse's inventory deposited as patch detritus.
// Rules 2 (false) is the original trial: engulf intents are ignored and starvation
// corpses keep their inventory.
bool engulf_and_detritus{};
// ADR-0136 step A (format 2 habitats): every cell drains the deposits it touches, up to
// its intake rate, with no intake target; pools yield at most a quarter of that rate.
// A dead life's detritus becomes one bubble per non-empty channel.
bool contact_feeding{};
// Rules 13: present exactly when every life carries its revamp input.
std::optional<CellularRevampStepRules> revamp{};
auto operator<=>(const CellularRegionRules&) const = default;
};
struct CellularLifeStepInput {
std::uint64_t life_id{}; // Stable world identity; arbitrates same-tick patch contention.
CellularPosition position;
std::uint32_t extent_um{};
CellularStepIntent intent;
accounting::Inventory body; // Structural matter; energy must be zero.
accounting::Inventory reserve;
accounting::Inventory heat; // Energy-only owner; matter channels must be zero.
MetabolicBudget budget;
std::uint32_t membrane_um{}; // Genome membrane thickness; contests engulfment (rules 3).
std::uint64_t source{}; // Provenance for a detritus patch this life creates (rules 3).
// Rules 12: liquid motion, required exactly when the habitat is format 3. The movement
// intent becomes the propulsion command.
std::optional<CellularLiquidInput> liquid{};
// Rules 13 (ADR-0179), required exactly when the region rules declare revamp.
std::optional<CellularRevampLifeInput> revamp{};
auto operator<=>(const CellularLifeStepInput&) const = default;
};
enum class CellularEngulfStatus : std::uint8_t {
not_requested,
engulfed, // Took the reported prey.
no_eligible_prey, // Nothing in contact was small enough (no longer reported).
unsupported, // Rules 2 region: the intent had no effect.
// Why nothing was taken (reporting only; who is engulfed is unchanged):
no_contact, // No other cell touched it.
prey_too_large, // Touching cells were too large even with the thinnest membrane.
membrane_too_thick // A touching cell was small enough, but its membrane defended it.
};
struct CellularLifeStepResult {
std::uint64_t life_id{};
CellularStepCandidate candidate;
CellularEngulfStatus engulf{CellularEngulfStatus::not_requested};
std::uint64_t prey_id{};
auto operator<=>(const CellularLifeStepResult&) const = default;
};
struct CellularPatchStockUpdate {
CellularPatchAddress address;
accounting::Inventory stock;
auto operator<=>(const CellularPatchStockUpdate&) const = default;
};
// Unrecorded: a corpse from a checkpoint format that kept no cause (before format 4).
// Rules 13 (ADR-0179): merged, a single cell that became a multicellular organism (its body and
// reserve moved into the organism); emerged, an organism that left the region for the world.
enum class CellularDeathCause : std::uint8_t {
unrecorded = 0,
starved = 1,
engulfed = 2,
merged = 3,
emerged = 4,
removed = 5 // ADR-0182: an operator removed the cell (rules 13).
};
struct CellularCorpseCandidate {
std::uint64_t life_id{};
CellularPosition position;
// Rules 2: body and reserve. Rules 3: empty, because the inventory moved to the
// predator and detritus. Accumulated heat is transferred out in both.
accounting::Inventory inventory;
CellularDeathCause cause{CellularDeathCause::starved};
std::uint64_t predator_id{}; // Engulfed only.
auto operator<=>(const CellularCorpseCandidate&) const = default;
};
struct CellularRegionStepCandidate {
std::uint64_t begin_tick{}, end_tick{};
// Sorted by stable life_id, independent of connection or input order.
std::vector<CellularLifeStepResult> lives;
std::vector<CellularPatchStockUpdate> patches;
// Dead this tick (starved or engulfed), sorted by life_id.
std::vector<CellularCorpseCandidate> starved;
// Rules 3 detritus patches created at empty cells, sorted by address.
std::vector<CellularPatch> new_patches;
accounting::Picojoules heat_released{};
// Rules 13: everything for the region's spent pool this tick: matter every life burned in
// place of energy (also in its own step candidate) and the spent share of kill remainders.
accounting::Inventory spent{};
auto operator<=>(const CellularRegionStepCandidate&) const = default;
};
enum class CellularRegionStepError : std::uint8_t {
invalid_tick,
too_many_lives,
invalid_life_id,
duplicate_life_id,
invalid_body,
invalid_heat,
overflow,
life_step,
habitat,
invalid_intent
};
// Detached shared-habitat candidate. Same-tick claims resolve by ascending stable life id;
// the first valid claimant spends from each patch's remaining per-channel stock. Any refused
// life step rejects the complete candidate; the caller publishes nothing. If maintenance
// cannot be paid, the life dies in this tick: body and reserve move to one corpse candidate,
// and its accumulated heat is reported separately for transfer to the world heat owner.
// Under rules 3 (ADR-0124) an engulf phase follows, and every dead life's inventory is
// deposited into the patch at its cell (a new patch when the cell is empty).
[[nodiscard]] std::expected<CellularRegionStepCandidate, CellularRegionStepError>
propose_cellular_region_step(std::uint64_t begin_tick, CellularArea area,
std::span<const CellularLifeStepInput> lives,
const CellularHabitat& habitat, CellularRegionRules rules = {});
} // namespace evo::biology