Files
aevolution/include/evo/server/lifecycle/primitive_lives.hpp
T
dingo 48684d30d9
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m53s
Foundation / checks (client) (push) Failing after 9m57s
Foundation / checks (network) (push) Successful in 13m32s
Foundation / checks (tsan) (push) Successful in 7m56s
Foundation / checks (checks) (push) Successful in 33m49s
Foundation / checks (postgres) (push) Successful in 46m39s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
M11.2b: restore funded life state with canonical closure checks
2026-10-09 18:30:58 -04:00

102 lines
4.2 KiB
C++

#pragma once
#include "evo/biology/founder.hpp"
#include "evo/biology/lineage.hpp"
#include <array>
#include <cstddef>
#include <cstdint>
#include <expected>
#include <memory>
#include <span>
#include <vector>
// 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<std::uint8_t, 16>;
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_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<SpawnSite> 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<std::unique_ptr<PrimitiveLives>, 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<PrimitiveLife, LifeError> spawn(LifeRequest request,
LifeAllocation allocation);
// References/spans are invalidated by a successful non-retry spawn.
[[nodiscard]] const NurseryConfig& config() const noexcept;
[[nodiscard]] std::span<const PrimitiveLife> lives() const noexcept;
[[nodiscard]] std::span<const accounting::Inventory> 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<std::uint8_t> snapshot() const;
[[nodiscard]] static std::expected<std::unique_ptr<PrimitiveLives>, LifeCodecError>
restore(std::span<const std::uint8_t> bytes, std::uint64_t world, std::uint64_t region,
std::uint64_t frame);
private:
struct State;
explicit PrimitiveLives(std::unique_ptr<State> state);
std::unique_ptr<State> state_;
};
} // namespace evo::server