Files
dingoandClaude Opus 5.5 108f29eda5
Foundation / checks (ecs) (push) Successful in 2m25s
Foundation / checks (deploy) (push) Successful in 5m15s
Foundation / checks (network) (push) Successful in 21m56s
Foundation / checks (client) (push) Successful in 22m54s
Foundation / checks (tsan) (push) Successful in 47m4s
Foundation / checks (postgres) (push) Successful in 1h15m49s
Foundation / checks (checks) (push) Failing after 1h30m4s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
feat(biology): evolve photosynthetic producers that capture light
ADR-0175 implements ADR-0136 step C with trial costs to be balanced after
playtesting. Genome schema 5 adds a photosynthesis efficiency (empty in
schemas 1-4, so earlier offers and draws are unchanged; mutation operators
29-32 are appended). Development version 5 builds the organelles as
internal membrane (p/4 of structure, upkept twice) and slows movement by
p/2 per thousand. Founder 4 and cellular rules 11 let lineages earn the
capability as an offspring plan that children express.

Owner format 9 declares the region's light. Inside each tick's detached
candidate, active unfrozen producers capture cross-section x irradiance x
efficiency (a quarter while moving), credited to the reserve and boundary
inflow together and replayed from recorded intentions. Well-lit wildlife
producers hold still. The joint owner keeps infall, water and radiation and
nests a rules-11 owner unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 18:06:02 -04:00

197 lines
9.9 KiB
C++

#pragma once
#include "evo/server/lifecycle/cellular_population_batch.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{}; // Infall only; a rules-11 owner books its own light.
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
};
// ADR-0173: one tick's recorded environmental crossing. Replay re-derives it from
// configuration, tick and seed and must reproduce it exactly; it is evidence, not input.
struct CellularEnvironmentFlow {
accounting::Inventory imported{};
accounting::Picojoules exported{};
bool operator==(const CellularEnvironmentFlow&) const = default;
};
struct CellularEnvironmentBatch;
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; // Infall, 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:
friend std::expected<std::unique_ptr<CellularEnvironment>, CellularEnvironmentError>
execute_environment_batch(const CellularEnvironment&, CellularEnvironmentBatch&, bool record);
struct Exchange;
struct Step {
CellularEnvironmentTickReport report;
};
enum class StepMode : std::uint8_t { live, record, replay };
[[nodiscard]] std::expected<Step, CellularEnvironmentError>
joint_step(std::vector<CellularLifeIntent>& intents,
std::span<const CellularMutationSelection> selections, StepMode mode);
CellularEnvironment(std::unique_ptr<CellularPopulation> population,
CellularEnvironmentConfig config);
[[nodiscard]] std::expected<Exchange, CellularEnvironmentError>
plan_exchange(std::uint64_t tick) const;
// One unattended joint tick on a detached candidate: the exchange, then frozen
// controlled intentions with either the recorded wildlife intentions (replay) or
// the shared controller's choice on the exchanged state (recording, `intents` empty).
[[nodiscard]] std::expected<CellularEnvironmentFlow, CellularEnvironmentError>
advance_unattended(std::vector<CellularLifeIntent>& intents, bool record);
[[nodiscard]] CellularPopulationBoundary population_boundary() const;
std::unique_ptr<CellularPopulation> population_;
CellularEnvironmentConfig config_;
std::vector<CellularInfallProgress> progress_; // Source order.
accounting::Picojoules exported_{};
};
// Joint batch 1: the unchanged population batch (interval or whole-group conversion)
// plus one flow per biological tick. Conversions carry no flows.
struct CellularEnvironmentBatch {
CellularPopulationBatch population;
std::vector<CellularEnvironmentFlow> flows;
bool operator==(const CellularEnvironmentBatch&) const = default;
};
inline constexpr std::uint32_t cellular_environment_batch_format = 1;
inline constexpr std::size_t environment_batch_header_bytes = 32;
inline constexpr std::size_t environment_flow_bytes = 48;
inline constexpr std::size_t max_environment_batch_bytes =
environment_batch_header_bytes + max_population_batch_bytes +
environment_flow_bytes * max_cellular_batch_ticks;
struct CellularEnvironmentExecution {
CellularEnvironmentBatch batch;
std::unique_ptr<CellularEnvironment> state;
};
// An unattended interval: every controlled cell must be fully suspended. Each tick
// applies its exchange, then the unchanged wildlife controller chooses on that state;
// the recorded intentions replay without rerunning the controller.
[[nodiscard]] std::expected<CellularEnvironmentExecution, CellularEnvironmentError>
prepare_environment_interval(const CellularEnvironment& base, const CellularBatchHeader& header);
[[nodiscard]] std::expected<CellularEnvironmentExecution, CellularEnvironmentError>
prepare_environment_conversion(const CellularEnvironment& base, CellularBatchHeader header,
CellularPopulationConversion conversion);
[[nodiscard]] std::expected<std::unique_ptr<CellularEnvironment>, CellularEnvironmentError>
replay_environment_batch(const CellularEnvironment& base, const CellularEnvironmentBatch& batch);
[[nodiscard]] std::expected<std::vector<std::uint8_t>, CellularEnvironmentError>
encode_environment_batch(const CellularEnvironmentBatch& batch);
[[nodiscard]] std::expected<CellularEnvironmentBatch, CellularEnvironmentError>
decode_environment_batch(std::span<const std::uint8_t> bytes);
} // namespace evo::server