#pragma once #include "evo/biology/lineage.hpp" #include "evo/processes/environmental_community.hpp" #include #include #include #include #include // History-capable environmental region (ADR-0062, schemas/environmental-lineage-v1.md): // the environmental community, the lineage archive that retains its organisms' // genomes and deaths, the binding of its two accounts to archived organisms, and // identity high-waters. One owner; every advance keeps archive deaths in the same // tick as the community's terminal transitions, so a checkpoint cannot split them. namespace evo::processes { struct LineageBinding { std::uint64_t producer{}, consumer{}; // Archived organism identities. std::uint64_t next_organism{}, next_genome{}; // Never reissue below these. auto operator<=>(const LineageBinding&) const = default; }; enum class LineageRegionError : std::uint8_t { community, // The community refused the tick or its envelope; nothing changed. closure, // Binding, lifecycle, living set or high-waters disagree. archive, // Archive bytes refused (see ArchiveCodecError via restore). format, // Container framing, lengths or reserved fields. version, // Unknown container format. checksum, // Container FNV-1a64 mismatch. poisoned, // A prior advance failed after committing the community tick. }; inline constexpr std::uint32_t lineage_region_format = 1; inline constexpr std::size_t max_lineage_region_bytes = 72 + EnvironmentalCommunity::max_snapshot_bytes + biology::max_archive_bytes; class LineageRegion { public: // Validates closure: both bound organisms recorded; terminal flags equal the // archive's death records; the archive's living organisms are exactly the // bound, non-terminal ones; births/deaths not after the current tick; and both // high-waters above every recorded identity. [[nodiscard]] static std::expected, LineageRegionError> create(std::unique_ptr community, const biology::LineageArchive& archive, LineageBinding binding); LineageRegion(const LineageRegion&) = delete; LineageRegion& operator=(const LineageRegion&) = delete; LineageRegion(LineageRegion&&) = delete; LineageRegion& operator=(LineageRegion&&) = delete; ~LineageRegion() = default; // Advances the community one tick and records each terminal transition as an // archive death at that tick. [[nodiscard]] std::expected advance(std::uint64_t tick, const EnvironmentalInput& input); [[nodiscard]] std::expected snapshot() const; // Restores the envelope (full replay audit) and the archive, validates closure // and requires a byte-identical canonical re-encoding. The budget is charged to // the community; archive memory is bounded by the archive's fixed caps. [[nodiscard]] static std::expected, LineageRegionError> restore(std::span bytes, world::WorldIdentity identity, std::size_t budget); [[nodiscard]] const EnvironmentalCommunity& community() const noexcept { return *community_; } [[nodiscard]] const biology::LineageArchive& archive() const noexcept { return archive_; } [[nodiscard]] const LineageBinding& binding() const noexcept { return binding_; } // Clone support (ADR-0067): only the nested community records a world; the // archive and binding are identity-free. False for world 0 or a poisoned region. [[nodiscard]] bool rebind_world(std::uint64_t world) noexcept { return !poisoned_ && community_->rebind_world(world); } private: LineageRegion(std::unique_ptr community, const biology::LineageArchive& archive, LineageBinding binding); [[nodiscard]] bool closed() const; std::unique_ptr community_; biology::LineageArchive archive_; LineageBinding binding_; bool poisoned_{}; }; } // namespace evo::processes