Files
aevolution/include/evo/processes/lineage_region.hpp
T
dingoandClaude Opus 5.5 a2c4543df2 Complete M7.4b lineage checkpoint profile; M7.4 done
ADR-0062 adds a canonical LineageArchive codec (parents before children,
organisms by id, FNV) that decodes by replaying admit/record_birth/
record_death and requires byte-identical re-encoding; it never overwrites
an existing archive. LineageRegion binds the environmental producer and
consumer accounts to archived organisms with identity high-waters,
enforces closure (lifecycle vs deaths, exact living set, tick bounds)
and records deaths in the tick that causes them.

Checkpoint profile 2 (environmental-lineage-v1, history profile 1)
publishes that container; schema v4 admits it with a 4,268,144-byte cap
and requires each commit to keep its predecessor's profile, RNG, history
and durability declarations. Linux adds provision/publish/recover for
the profile on the shared publication path.

Qualification found WSL2 stepping CLOCK_REALTIME back ~0.5 s every ~32 s,
which made two 1 s-lease tests flaky under ASan. Tests now wait on
database-time expiry; ADR-0060 records the slewed-clock requirement.

Validated: Windows CTest 57/57; Linux GCC Debug/Release, clang-tidy and
ASan/UBSan CTest 57/57 each; PostgreSQL suites 2x Release, 2x analysis,
5x ASan; SQL and C++ mutations each caught. No Docker/Gitea CI, remote
store, process kill or deployment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 16:43:10 -04:00

80 lines
3.8 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_;
}
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