#pragma once #include "evo/biology/founder.hpp" #include "evo/biology/lineage.hpp" #include #include #include #include #include #include #include // Portable, single-owner server orchestration, not the foundation simulation. // M11.2a is an in-memory candidate; success is NOT a durable acknowledgement. // The host must authorize requests and publish the entire state plus bindings // atomically (ADR-0100). No network, database, clocks or implicit biology here. namespace evo::server { using LifeAccount = std::array; struct SpawnSite { std::int32_t x_um{}, y_um{}; // Region/frame-local, micrometres. auto operator<=>(const SpawnSite&) const = default; }; inline constexpr std::size_t max_primitive_lives = 256; inline constexpr std::int32_t spawn_coordinate_limit_um = 1'000'000'000; inline constexpr std::uint32_t primitive_lives_format = 1; inline constexpr std::size_t max_primitive_lives_bytes = 200 + 184 * max_primitive_lives + biology::max_archive_legacy_bytes; enum class LifeCodecError : std::uint8_t { format, version, checksum, closure }; struct NurseryConfig { std::uint64_t world{}, region{}, frame{}, source{}; accounting::Inventory funding{}; // Explicit finite opening, never replenished here. std::vector sites; // Server-approved habitat; sorted lexicographically. }; struct LifeRequest { LifeAccount account{}; std::uint64_t action{}; auto operator<=>(const LifeRequest&) const = default; }; // Obtained from the world's durable allocator by the host, never from the client. // All identities are distinct namespaces. Gaps are allowed, reuse is not. struct LifeAllocation { std::uint64_t life{}, organism{}, genome{}, event{}; Tick born{}; auto operator<=>(const LifeAllocation&) const = default; }; struct PrimitiveLife { LifeRequest request{}; LifeAllocation identity{}; std::uint64_t source{}; // Funding provenance, within config.world. SpawnSite site{}; biology::GrowthState growth{}; accounting::ReservoirId body{}, reserve{}; auto operator<=>(const PrimitiveLife&) const = default; }; enum class LifeError : std::uint8_t { invalid, capacity, occupied, existing_life, identity_conflict, insufficient_funding, overflow, biology, archive }; class PrimitiveLives { public: [[nodiscard]] static std::expected, LifeError> create(NurseryConfig config); ~PrimitiveLives(); PrimitiveLives(const PrimitiveLives&) = delete; PrimitiveLives& operator=(const PrimitiveLives&) = delete; // Selects the first free provisioned site. Exact successful retry resolves // before allocation/site/funding checks, returning the original life. // Different action for an account already living is refused. On any refusal // (or allocation exception), every inventory and history entry is unchanged. [[nodiscard]] std::expected spawn(LifeRequest request, LifeAllocation allocation); // References/spans are invalidated by a successful non-retry spawn. [[nodiscard]] const NurseryConfig& config() const noexcept; [[nodiscard]] std::span lives() const noexcept; [[nodiscard]] std::span balances() const noexcept; [[nodiscard]] accounting::Inventory totals() const noexcept; [[nodiscard]] const biology::LineageArchive& archive() const noexcept; // Complete creation-state envelope (schemas/primitive-lives-v1.md). It has // no progressing biology yet. Bytes include balances and archive as well as // their founding requests; restore reconstructs and checks exact closure. [[nodiscard]] std::vector snapshot() const; [[nodiscard]] static std::expected, LifeCodecError> restore(std::span bytes, std::uint64_t world, std::uint64_t region, std::uint64_t frame); private: struct State; explicit PrimitiveLives(std::unique_ptr state); std::unique_ptr state_; }; } // namespace evo::server