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>
237 lines
12 KiB
C++
237 lines
12 KiB
C++
#include "evo/persistence/checkpoint_store.hpp"
|
|
#include "evo/persistence/sha256.hpp"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <string>
|
|
|
|
namespace evo::persistence {
|
|
namespace {
|
|
using std::chrono::milliseconds;
|
|
|
|
// Per-profile payload operations; the publication and recovery paths are shared.
|
|
struct EnvironmentalCodec {
|
|
using State = processes::EnvironmentalCommunity;
|
|
using Recovered = RecoveredCheckpoint;
|
|
static constexpr auto profile = CheckpointProfile::environmental_v1;
|
|
static auto snapshot(const State& s) {
|
|
return s.snapshot();
|
|
}
|
|
static std::uint64_t tick(const State& s) {
|
|
return s.progress().tick;
|
|
}
|
|
static std::uint64_t quantum(const State& s) {
|
|
return s.tick_microseconds();
|
|
}
|
|
static auto restore(std::span<const std::uint8_t> payload, const CheckpointManifest& m,
|
|
std::size_t budget) {
|
|
return restore_environmental_payload(payload, m, budget);
|
|
}
|
|
};
|
|
struct LineageCodec {
|
|
using State = processes::LineageRegion;
|
|
using Recovered = RecoveredLineageCheckpoint;
|
|
static constexpr auto profile = CheckpointProfile::environmental_lineage_v1;
|
|
static auto snapshot(const State& s) {
|
|
return s.snapshot();
|
|
}
|
|
static std::uint64_t tick(const State& s) {
|
|
return s.community().progress().tick;
|
|
}
|
|
static std::uint64_t quantum(const State& s) {
|
|
return s.community().tick_microseconds();
|
|
}
|
|
static auto restore(std::span<const std::uint8_t> payload, const CheckpointManifest& m,
|
|
std::size_t budget) {
|
|
return restore_lineage_payload(payload, m, budget);
|
|
}
|
|
};
|
|
|
|
PublicationFailure fail(PublicationError kind) {
|
|
return {kind, {}, {}};
|
|
}
|
|
// Validates the exact payload bytes as recovery would, uploads and reads back.
|
|
// Returns the manifest bytes ready for the database.
|
|
template <typename Codec>
|
|
std::expected<std::vector<std::uint8_t>, PublicationFailure>
|
|
stage(const FilesystemObjectStore& objects, const std::vector<std::uint8_t>& payload,
|
|
const CheckpointManifest& manifest, std::size_t budget) {
|
|
auto bytes = encode_manifest(manifest);
|
|
if (!bytes || manifest.profile != Codec::profile || !Codec::restore(payload, manifest, budget))
|
|
return std::unexpected{fail(PublicationError::candidate)};
|
|
auto stored = objects.put(payload);
|
|
if (!stored)
|
|
return std::unexpected{PublicationFailure{PublicationError::upload, {}, stored.error()}};
|
|
auto back = objects.get(*stored);
|
|
if (!back || *stored != VerifiedObject{manifest.payload_sha256, manifest.payload_bytes} ||
|
|
!std::ranges::equal(*back, payload))
|
|
return std::unexpected{PublicationFailure{
|
|
PublicationError::verify, {}, back ? ObjectError::corrupt : back.error()}};
|
|
return std::move(*bytes);
|
|
}
|
|
std::expected<CommittedCheckpoint, PublicationFailure>
|
|
confirm(const std::expected<RegionHead, LeaseStoreFailure>& published,
|
|
std::vector<std::uint8_t> manifest_bytes, const CheckpointManifest& manifest) {
|
|
if (!published) {
|
|
const auto kind = published.error().kind == LeaseStoreError::unknown
|
|
? PublicationError::unknown
|
|
: PublicationError::rejected;
|
|
return std::unexpected{PublicationFailure{kind, published.error(), {}}};
|
|
}
|
|
// The database digested the bytes independently; the receipt must agree.
|
|
const auto digest = sha256(manifest_bytes);
|
|
if (!digest || published->digest != *digest || published->sequence != manifest.sequence ||
|
|
published->commit != manifest.commit || published->end_tick != manifest.end_tick ||
|
|
published->frame != manifest.frame)
|
|
return std::unexpected{fail(PublicationError::receipt)};
|
|
return CommittedCheckpoint{*published, std::move(manifest_bytes)};
|
|
}
|
|
|
|
template <typename Codec>
|
|
std::expected<CommittedCheckpoint, PublicationFailure>
|
|
provision(Postgres& owner, const FilesystemObjectStore& objects, RegionKey key, std::uint64_t frame,
|
|
const typename Codec::State& opening, Uuid commit, const Sha256Digest& build,
|
|
std::size_t budget, milliseconds timeout) {
|
|
auto payload = Codec::snapshot(opening);
|
|
if (!payload || Codec::tick(opening) != 0)
|
|
return std::unexpected{fail(PublicationError::candidate)};
|
|
const auto digest = sha256(*payload);
|
|
if (!digest)
|
|
return std::unexpected{fail(PublicationError::digest)};
|
|
auto manifest = genesis_manifest(key, frame, commit, Codec::quantum(opening), build,
|
|
payload->size(), *digest, Codec::profile);
|
|
if (!manifest)
|
|
return std::unexpected{fail(PublicationError::candidate)};
|
|
auto bytes = stage<Codec>(objects, *payload, *manifest, budget);
|
|
if (!bytes)
|
|
return std::unexpected{bytes.error()};
|
|
constexpr char digits[] = "0123456789abcdef";
|
|
std::string parameter = "\\x";
|
|
for (const auto b : *bytes) {
|
|
parameter.push_back(digits[b >> 4U]);
|
|
parameter.push_back(digits[b & 0x0FU]);
|
|
}
|
|
const std::array<std::string, 1> p{parameter};
|
|
auto done = owner.query("SELECT evolution.provision_region_checkpoint($1::bytea)", p, timeout);
|
|
if (!done) {
|
|
const bool unknown = done.error().kind == PgError::unknown;
|
|
return std::unexpected{PublicationFailure{
|
|
unknown ? PublicationError::unknown : PublicationError::rejected,
|
|
{unknown ? LeaseStoreError::unknown : LeaseStoreError::store, done.error()},
|
|
{}}};
|
|
}
|
|
auto committed = load_region_checkpoint(owner, key, timeout);
|
|
if (!committed)
|
|
return std::unexpected{
|
|
PublicationFailure{PublicationError::unknown, committed.error(), {}}};
|
|
return confirm(committed->head, std::move(*bytes), *manifest);
|
|
}
|
|
|
|
template <typename Codec>
|
|
std::expected<CommittedCheckpoint, PublicationFailure>
|
|
publish(Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
|
|
const CommittedCheckpoint& predecessor, const typename Codec::State& state, Uuid commit,
|
|
const Sha256Digest& build, std::size_t budget, milliseconds timeout) {
|
|
const auto previous = decode_manifest(predecessor.manifest);
|
|
auto payload = Codec::snapshot(state);
|
|
if (!previous || !payload || previous->profile != Codec::profile)
|
|
return std::unexpected{fail(PublicationError::candidate)};
|
|
const auto digest = sha256(*payload);
|
|
if (!digest)
|
|
return std::unexpected{fail(PublicationError::digest)};
|
|
auto manifest = successor_manifest(*previous, predecessor.head.digest, commit, lease,
|
|
Codec::tick(state), build, payload->size(), *digest);
|
|
if (!manifest)
|
|
return std::unexpected{fail(PublicationError::candidate)};
|
|
auto bytes = stage<Codec>(objects, *payload, *manifest, budget);
|
|
if (!bytes)
|
|
return std::unexpected{bytes.error()};
|
|
auto published = publish_region_checkpoint(db, *bytes, timeout);
|
|
return confirm(published, std::move(*bytes), *manifest);
|
|
}
|
|
|
|
template <typename Codec>
|
|
std::expected<typename Codec::Recovered, RecoveryFailure>
|
|
recover(Postgres& db, const FilesystemObjectStore& objects, RegionKey key,
|
|
std::span<const Sha256Digest> retained_builds, std::size_t budget, milliseconds timeout) {
|
|
auto committed = load_region_checkpoint(db, key, timeout);
|
|
if (!committed)
|
|
return std::unexpected{
|
|
RecoveryFailure{committed.error().kind == LeaseStoreError::not_provisioned
|
|
? RecoveryError::not_provisioned
|
|
: RecoveryError::store,
|
|
committed.error()}};
|
|
const auto fail_with = [](RecoveryError kind) {
|
|
return std::unexpected{RecoveryFailure{kind, {}}};
|
|
};
|
|
const auto digest = sha256(committed->manifest);
|
|
auto manifest = decode_manifest(committed->manifest);
|
|
const auto& head = committed->head;
|
|
if (!digest || *digest != head.digest)
|
|
return fail_with(RecoveryError::manifest);
|
|
if (!manifest)
|
|
return fail_with(manifest.error() == CheckpointError::unsupported
|
|
? RecoveryError::unsupported
|
|
: RecoveryError::manifest);
|
|
if (manifest->key != key || manifest->frame != head.frame ||
|
|
manifest->sequence != head.sequence || manifest->commit != head.commit ||
|
|
manifest->end_tick != head.end_tick)
|
|
return fail_with(RecoveryError::manifest);
|
|
if (!supported(*manifest) || manifest->profile != Codec::profile ||
|
|
std::ranges::find(retained_builds, manifest->build) == retained_builds.end())
|
|
return fail_with(RecoveryError::unsupported);
|
|
auto payload = objects.get({manifest->payload_sha256, manifest->payload_bytes});
|
|
if (!payload)
|
|
return fail_with(payload.error() == ObjectError::missing ? RecoveryError::missing_payload
|
|
: payload.error() == ObjectError::corrupt ? RecoveryError::corrupt_payload
|
|
: RecoveryError::store);
|
|
auto state = Codec::restore(*payload, *manifest, budget);
|
|
if (!state)
|
|
return fail_with(RecoveryError::payload);
|
|
return typename Codec::Recovered{std::move(*committed), *manifest, std::move(*state)};
|
|
}
|
|
} // namespace
|
|
|
|
std::expected<CommittedCheckpoint, PublicationFailure> provision_environmental_region(
|
|
Postgres& owner, const FilesystemObjectStore& objects, RegionKey key, std::uint64_t frame,
|
|
const processes::EnvironmentalCommunity& opening, Uuid commit, const Sha256Digest& build,
|
|
std::size_t restore_budget, milliseconds timeout) {
|
|
return provision<EnvironmentalCodec>(owner, objects, key, frame, opening, commit, build,
|
|
restore_budget, timeout);
|
|
}
|
|
std::expected<CommittedCheckpoint, PublicationFailure> publish_environmental_checkpoint(
|
|
Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
|
|
const CommittedCheckpoint& predecessor, const processes::EnvironmentalCommunity& state,
|
|
Uuid commit, const Sha256Digest& build, std::size_t restore_budget, milliseconds timeout) {
|
|
return publish<EnvironmentalCodec>(db, objects, lease, predecessor, state, commit, build,
|
|
restore_budget, timeout);
|
|
}
|
|
std::expected<RecoveredCheckpoint, RecoveryFailure>
|
|
recover_environmental_checkpoint(Postgres& db, const FilesystemObjectStore& objects, RegionKey key,
|
|
std::span<const Sha256Digest> retained_builds,
|
|
std::size_t restore_budget, milliseconds timeout) {
|
|
return recover<EnvironmentalCodec>(db, objects, key, retained_builds, restore_budget, timeout);
|
|
}
|
|
|
|
std::expected<CommittedCheckpoint, PublicationFailure>
|
|
provision_lineage_region(Postgres& owner, const FilesystemObjectStore& objects, RegionKey key,
|
|
std::uint64_t frame, const processes::LineageRegion& opening, Uuid commit,
|
|
const Sha256Digest& build, std::size_t restore_budget,
|
|
milliseconds timeout) {
|
|
return provision<LineageCodec>(owner, objects, key, frame, opening, commit, build,
|
|
restore_budget, timeout);
|
|
}
|
|
std::expected<CommittedCheckpoint, PublicationFailure> publish_lineage_checkpoint(
|
|
Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease,
|
|
const CommittedCheckpoint& predecessor, const processes::LineageRegion& state, Uuid commit,
|
|
const Sha256Digest& build, std::size_t restore_budget, milliseconds timeout) {
|
|
return publish<LineageCodec>(db, objects, lease, predecessor, state, commit, build,
|
|
restore_budget, timeout);
|
|
}
|
|
std::expected<RecoveredLineageCheckpoint, RecoveryFailure>
|
|
recover_lineage_checkpoint(Postgres& db, const FilesystemObjectStore& objects, RegionKey key,
|
|
std::span<const Sha256Digest> retained_builds,
|
|
std::size_t restore_budget, milliseconds timeout) {
|
|
return recover<LineageCodec>(db, objects, key, retained_builds, restore_budget, timeout);
|
|
}
|
|
} // namespace evo::persistence
|