Clone a verified committed head into an isolated world and database (ADR-0067): world re-identification with a proven byte round trip, a schema v7 clone genesis naming the source commit and digest with digested inherited outcomes, a read-only source session, an owner-only atomic target transaction and the evo-world-clone tool. audit_region_history re-verifies every retained link. Real-process tests cover trajectory equality after the cut, source isolation, killed clones and retention. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
85 lines
4.1 KiB
C++
85 lines
4.1 KiB
C++
#pragma once
|
|
#include "evo/biology/lineage.hpp"
|
|
#include "evo/processes/environmental_community.hpp"
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <expected>
|
|
#include <memory>
|
|
#include <span>
|
|
|
|
// 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<std::unique_ptr<LineageRegion>, LineageRegionError>
|
|
create(std::unique_ptr<EnvironmentalCommunity> 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<EnvironmentalReport, LineageRegionError>
|
|
advance(std::uint64_t tick, const EnvironmentalInput& input);
|
|
[[nodiscard]] std::expected<world::WorldBytes, LineageRegionError> 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<std::unique_ptr<LineageRegion>, LineageRegionError>
|
|
restore(std::span<const std::uint8_t> 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<EnvironmentalCommunity> community,
|
|
const biology::LineageArchive& archive, LineageBinding binding);
|
|
[[nodiscard]] bool closed() const;
|
|
std::unique_ptr<EnvironmentalCommunity> community_;
|
|
biology::LineageArchive archive_;
|
|
LineageBinding binding_;
|
|
bool poisoned_{};
|
|
};
|
|
} // namespace evo::processes
|