Files
dingoandClaude Opus 5.5 703f67834f Complete M7.9a region clones and retained-history audit
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>
2026-09-29 06:47:45 -04:00

120 lines
4.5 KiB
C++

#pragma once
#include "evo/world/environment.hpp"
#include <array>
#include <compare>
#include <cstddef>
#include <cstdint>
#include <expected>
#include <memory>
#include <span>
namespace evo::processes {
class EnvironmentalCommunity;
}
namespace evo::world {
struct OrganismResourceId {
std::uint64_t value{};
auto operator<=>(const OrganismResourceId&) const = default;
};
// Tagged compartments in Field order; fg for mass, pJ for energy. No lifecycle state.
struct OrganismResources {
OrganismResourceId id;
std::array<std::uint64_t, field_count> balances{}, capacities{};
auto operator<=>(const OrganismResources&) const = default;
};
enum class ResourceDirection : std::uint8_t { to_organism, to_environment };
struct EnvironmentalLeg {
VoxelAddress voxel;
OrganismResourceId organism;
Field field;
ResourceDirection direction;
std::uint64_t amount{};
auto operator<=>(const EnvironmentalLeg&) const = default;
};
struct EnvironmentalBatch {
std::uint64_t command{}, tick{};
std::span<const EnvironmentalLeg> legs;
};
struct EnvironmentalReceipt {
std::uint64_t command{}, tick{};
std::size_t legs{};
auto operator<=>(const EnvironmentalReceipt&) const = default;
};
enum class TransferError : std::uint8_t {
config,
identity,
terrain_binding,
capacity,
overflow,
budget,
invalid_request,
wrong_owner,
stale_view,
uncovered,
missing,
unknown_organism,
insufficient,
conflict,
stale_command,
format,
version,
checksum
};
class EnvironmentalTransfers {
public:
static constexpr std::size_t max_sites = 256, max_organisms = 64, max_legs = 32;
static constexpr std::size_t retained_commands = 64, scratch_bytes = 65536;
// Consumes both stores on success or failure. Retaining mutable aliases is forbidden.
[[nodiscard]] static std::expected<std::unique_ptr<EnvironmentalTransfers>, TransferError>
create(WorldIdentity identity, std::unique_ptr<FieldStore> fields,
std::unique_ptr<ChunkStore> terrain, std::span<const OrganismResources> organisms,
std::size_t byte_budget);
EnvironmentalTransfers(const EnvironmentalTransfers&) = delete;
EnvironmentalTransfers& operator=(const EnvironmentalTransfers&) = delete;
EnvironmentalTransfers(EnvironmentalTransfers&&) = delete;
EnvironmentalTransfers& operator=(EnvironmentalTransfers&&) = delete;
[[nodiscard]] const FieldStore& fields() const noexcept {
return *fields_;
}
[[nodiscard]] const ChunkStore& terrain() const noexcept {
return *terrain_;
}
[[nodiscard]] std::span<const OrganismResources> organisms() const noexcept {
return std::span{organisms_}.first(count_);
}
static constexpr std::size_t max_snapshot_bytes = std::size_t{72} * 1024 * 1024;
[[nodiscard]] std::expected<WorldBytes, TransferError> snapshot() const;
[[nodiscard]] static std::expected<std::unique_ptr<EnvironmentalTransfers>, TransferError>
restore(std::span<const std::uint8_t> bytes, WorldIdentity identity, std::size_t byte_budget,
std::size_t field_budget, std::size_t terrain_budget);
[[nodiscard]] accounting::Inventory totals() const noexcept;
[[nodiscard]] std::size_t accounted_bytes() const noexcept;
[[nodiscard]] std::expected<EnvironmentalReceipt, TransferError>
commit(const EnvironmentalBatch& batch, const EnvironmentView& view,
std::uint64_t current_tick);
private:
friend class evo::processes::EnvironmentalCommunity;
// Clone support (ADR-0067): the field store and, through it, terrain snapshots
// carry this identity; no other resource state depends on it.
void rebind(WorldIdentity identity) noexcept {
fields_->identity_ = identity;
}
[[nodiscard]] bool process_checkpoint(std::uint64_t tick,
const std::array<std::uint64_t, 10>& environment,
const std::array<std::uint64_t, 10>& producer,
const std::array<std::uint64_t, 10>& consumer) noexcept;
struct History {
EnvironmentalReceipt receipt{};
std::array<EnvironmentalLeg, max_legs> legs{};
};
EnvironmentalTransfers() = default;
std::unique_ptr<FieldStore> fields_;
std::unique_ptr<ChunkStore> terrain_;
std::array<OrganismResources, max_organisms> organisms_{};
std::array<History, retained_commands> history_{};
std::size_t count_{}, budget_{}, next_history_{};
std::uint64_t adoption_command_{};
};
} // namespace evo::world