Files
aevolution/include/evo/server/lifecycle/cellular_environment.hpp
T
dingoandClaude Opus 5.5 4c70257883
Foundation / checks (ecs) (push) Successful in 2m28s
Foundation / checks (deploy) (push) Successful in 4m59s
Foundation / checks (network) (push) Successful in 20m56s
Foundation / checks (client) (push) Successful in 22m1s
Foundation / checks (tsan) (push) Successful in 45m41s
Foundation / checks (postgres) (push) Successful in 1h13m35s
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 1h20m34s
feat(ecology): own cells and their local environment together
ADR-0172 records the maintainer's M12.5 ownership decision: one owner per
patch of space for cells and their local environment, reserves belonging
to the biological individual, and an open planet boundary whose every
crossing is a recorded import or export.

The portable CellularEnvironment owner (envelope AEVOCEN1) holds an exact
population with tagged water and dissolved compartments, refuses declared
atmosphere, imports infall from finite per-source entitlements (ADR-0136
step B) and radiates retained heat. Each tick's exchange is a validated
swap on the nested owner, undone if the biological tick refuses. Closed
and source-off regions keep their biology byte for byte; raw and cohort
representations agree under infall. No existing format, trace, golden,
default opening or live version changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 16:05:17 -04:00

142 lines
6.5 KiB
C++

#pragma once
#include "evo/server/lifecycle/cellular_population.hpp"
#include "evo/world/coordinates.hpp"
#include "evo/world/environment_units.hpp"
#include <array>
#include <compare>
#include <cstddef>
#include <cstdint>
#include <expected>
#include <memory>
#include <span>
#include <vector>
namespace evo::server {
// ADR-0172: one owner of a region's cells and their local environment.
inline constexpr std::uint32_t cellular_environment_format = 1;
inline constexpr std::uint32_t cellular_environment_rules = 1;
inline constexpr std::size_t max_cellular_infall_sources = 4;
// Trial values (ADR-0136 step B): two simulated minutes between events on average,
// 12-24 bubbles per event, dropped within a 1 mm disc.
inline constexpr std::uint64_t cellular_infall_trial_interval_ticks = 2'400;
inline constexpr std::uint32_t cellular_infall_trial_bubbles_min = 12;
inline constexpr std::uint32_t cellular_infall_trial_bubbles_max = 24;
inline constexpr std::uint32_t cellular_infall_clump_radius_um = 500;
inline constexpr std::uint32_t max_cellular_infall_bubbles = 64;
inline constexpr std::size_t cellular_environment_header_bytes = 176;
inline constexpr std::size_t cellular_infall_source_bytes = 208;
inline constexpr std::size_t max_cellular_environment_bytes =
cellular_environment_header_bytes + cellular_infall_source_bytes * max_cellular_infall_sources +
max_cellular_population_bytes + 8;
// One axis-aligned habitat inside one environmental voxel. Its width and height
// equal the cellular area; depth only defines a sampling volume, never stock.
struct CellularEnvironmentGeometry {
world::VoxelAddress voxel{};
world::HabitatBox box{};
};
// Tagged, inedible compartments. No rules-1 process converts them.
struct CellularWaterColumn {
accounting::Femtograms water{}, dissolved_nitrogen{}, dissolved_phosphorus{};
auto operator<=>(const CellularWaterColumn&) const = default;
};
// An audited external import over a declared rectangle and half-open tick interval.
// The entitlement is finite; what remains is entitlement minus imported.
struct CellularInfallSource {
std::uint64_t id{};
std::uint32_t x_um{}, y_um{}, width_um{}, height_um{};
std::uint64_t start_tick{}, end_tick{};
std::uint64_t mean_interval_ticks{cellular_infall_trial_interval_ticks};
std::uint32_t bubbles_min{cellular_infall_trial_bubbles_min};
std::uint32_t bubbles_max{cellular_infall_trial_bubbles_max};
accounting::Inventory per_bubble{}; // Single-channel bubbles cycle stocked channels.
accounting::Inventory entitlement{}; // Cumulative import limit, per channel.
auto operator<=>(const CellularInfallSource&) const = default;
};
struct CellularInfallProgress {
accounting::Inventory imported{};
std::uint64_t events{}, credited_bubbles{}, skipped_bubbles{}, exhausted_events{};
auto operator<=>(const CellularInfallProgress&) const = default;
};
struct CellularEnvironmentConfig {
CellularEnvironmentGeometry geometry{};
CellularWaterColumn water{};
std::vector<CellularInfallSource> sources; // Sorted unique ids, disjoint rectangles.
accounting::Picojoules radiation_cap{}; // Heat exported per tick; zero is closed.
std::uint32_t atmosphere_species{}; // Any declared species is unsupported.
};
struct CellularEnvironmentTickReport {
std::uint64_t tick{};
accounting::Inventory imported{};
accounting::Picojoules exported{};
std::uint64_t events{}, credited_bubbles{}, skipped_bubbles{};
CellularTickExecution biology;
};
enum class CellularEnvironmentError : std::uint8_t {
unsupported,
invalid_geometry,
invalid_source,
identity,
habitat,
biology,
overflow,
conservation,
format,
checksum
};
class CellularEnvironment {
public:
// Consumes the population on success or failure.
[[nodiscard]] static std::expected<std::unique_ptr<CellularEnvironment>,
CellularEnvironmentError>
create(std::unique_ptr<CellularPopulation> population, CellularEnvironmentConfig config);
CellularEnvironment(const CellularEnvironment&) = delete;
CellularEnvironment& operator=(const CellularEnvironment&) = delete;
~CellularEnvironment();
// Read-only views; they confer no separate spending rights.
[[nodiscard]] const CellularPopulation& population() const noexcept;
[[nodiscard]] const CellularLives& state() const noexcept;
[[nodiscard]] const CellularEnvironmentGeometry& geometry() const noexcept;
[[nodiscard]] world::HabitatMeasure measure() const noexcept;
[[nodiscard]] const CellularWaterColumn& water() const noexcept;
[[nodiscard]] std::span<const CellularInfallSource> sources() const noexcept;
[[nodiscard]] std::span<const CellularInfallProgress> infall() const noexcept;
[[nodiscard]] accounting::Picojoules radiation_cap() const noexcept;
[[nodiscard]] accounting::Inventory imported() const noexcept; // All sources, cumulative.
[[nodiscard]] accounting::Picojoules exported() const noexcept;
[[nodiscard]] std::uint64_t tick() const noexcept;
// One joint 50 ms tick: the environmental exchange, then the unchanged population
// tick. Any refusal leaves the whole owner byte-for-byte unchanged.
[[nodiscard]] std::expected<CellularEnvironmentTickReport, CellularEnvironmentError>
advance_tick(std::span<const CellularLifeIntent> controlled,
std::span<const CellularMutationSelection> selections = {});
// Explicit representation changes of the nested population; no tick advances.
[[nodiscard]] std::expected<std::size_t, CellularEnvironmentError>
collapse(std::uint64_t genome);
[[nodiscard]] std::expected<std::size_t, CellularEnvironmentError>
materialize(std::uint64_t genome);
[[nodiscard]] std::vector<std::uint8_t> snapshot() const;
[[nodiscard]] static std::expected<std::unique_ptr<CellularEnvironment>,
CellularEnvironmentError>
restore(std::span<const std::uint8_t> bytes, std::uint64_t world, std::uint64_t region,
std::uint64_t frame, std::uint64_t tick);
private:
struct Exchange;
CellularEnvironment(std::unique_ptr<CellularPopulation> population,
CellularEnvironmentConfig config);
[[nodiscard]] std::expected<Exchange, CellularEnvironmentError>
plan_exchange(std::uint64_t tick) const;
std::unique_ptr<CellularPopulation> population_;
CellularEnvironmentConfig config_;
std::vector<CellularInfallProgress> progress_; // Source order.
accounting::Picojoules exported_{};
};
} // namespace evo::server