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>
This commit is contained in:
dingo
2026-09-29 06:47:45 -04:00
co-authored by Claude Opus 5.5
parent 7aec68db8e
commit 703f67834f
28 changed files with 2206 additions and 266 deletions
+2 -248
View File
@@ -1,138 +1,11 @@
#include "evo/persistence/checkpoint_store.hpp"
#include "evo/persistence/sha256.hpp"
#include <algorithm>
#include <array>
#include <string>
#include "checkpoint_detail.hpp"
namespace evo::persistence {
namespace {
using namespace detail;
using std::chrono::milliseconds;
struct Organisms {
std::uint64_t producer{}, consumer{};
};
// Lifecycle agreement shared by both profiles: exactly one durable death per
// terminal actor, none for an active one, never after the committed tick.
bool lifecycle_agrees(const processes::EnvironmentalProgress& progress, Organisms organisms,
std::span<const CommittedDecision> decisions, const RegionHead& head) {
for (const auto actor : {Actor::producer, Actor::consumer}) {
const auto terminal =
actor == Actor::producer ? progress.producer_terminal : progress.consumer_terminal;
const auto count = std::ranges::count_if(
decisions, [&](const CommittedDecision& d) { return d.decision.actor == actor; });
if (count > 1 || (terminal == 1) != (count == 1))
return false;
}
return std::ranges::all_of(decisions, [&](const CommittedDecision& d) {
const auto organism =
d.decision.actor == Actor::producer ? organisms.producer : organisms.consumer;
return d.decision.tick >= 1 && d.decision.tick <= head.end_tick &&
d.commit_sequence <= head.sequence && d.decision.organism == organism;
});
}
// Per-profile payload operations; 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 Organisms organisms(const State&) {
return {};
}
static auto restore(std::span<const std::uint8_t> payload, const CheckpointManifest& m,
std::size_t budget) {
return restore_environmental_payload(payload, m, budget);
}
static bool reconcile(const State& s, std::span<const CommittedDecision> d,
const RegionHead& head) {
return lifecycle_agrees(s.progress(), organisms(s), d, head);
}
};
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 Organisms organisms(const State& s) {
return {s.binding().producer, s.binding().consumer};
}
static auto restore(std::span<const std::uint8_t> payload, const CheckpointManifest& m,
std::size_t budget) {
return restore_lineage_payload(payload, m, budget);
}
// Plus: each durable death is the archive's death, at the same tick.
static bool reconcile(const State& s, std::span<const CommittedDecision> d,
const RegionHead& head) {
if (!lifecycle_agrees(s.community().progress(), organisms(s), d, head))
return false;
return std::ranges::all_of(d, [&](const CommittedDecision& x) {
const auto record = s.archive().organism(x.decision.organism);
return record && !record->alive && record->died.value == x.decision.tick;
});
}
};
PublicationFailure fail(PublicationError kind, ObjectError object = {}) {
return {kind, {}, object, {}};
}
std::string hex_parameter(std::span<const std::uint8_t> bytes) {
constexpr char digits[] = "0123456789abcdef";
std::string out = "\\x";
for (const auto b : bytes) {
out.push_back(digits[b >> 4U]);
out.push_back(digits[b & 0x0FU]);
}
return out;
}
std::expected<void, PublicationFailure> put_verified(const FilesystemObjectStore& objects,
const std::vector<std::uint8_t>& bytes,
const VerifiedObject& expected) {
auto stored = objects.put(bytes);
if (!stored)
return std::unexpected{fail(PublicationError::upload, stored.error())};
auto back = objects.get(*stored);
if (!back || *stored != expected || !std::ranges::equal(*back, bytes))
return std::unexpected{
fail(PublicationError::verify, back ? ObjectError::corrupt : back.error())};
return {};
}
// The receipt must agree with the local manifest the database digested.
std::expected<CommittedCheckpoint, PublicationFailure>
confirm(const std::expected<RegionHead, LeaseStoreFailure>& published,
std::vector<std::uint8_t> manifest_bytes, const CheckpointManifest& manifest,
PreparedCommit prepared) {
if (!published) {
const auto kind = published.error().kind == LeaseStoreError::unknown
? PublicationError::unknown
: PublicationError::rejected;
return std::unexpected{
PublicationFailure{kind, published.error(), {}, std::move(prepared)}};
}
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,
@@ -241,125 +114,6 @@ publish_batch(Postgres& db, const FilesystemObjectStore& objects, const LeaseGra
return PublishedBatch{std::move(*committed), decisions};
}
// Replays the head's recorded batch from its committed predecessor (ADR-0065).
template <typename Codec>
std::expected<void, RecoveryFailure>
verify_tail(Postgres& db, const FilesystemObjectStore& objects, RegionKey key,
const CheckpointManifest& m, std::span<const std::uint8_t> head_payload,
const typename Codec::State& head_state, std::span<const CommittedDecision> durable,
std::size_t budget, milliseconds timeout) {
const auto fail_with = [](RecoveryError kind) {
return std::unexpected{RecoveryFailure{kind, {}}};
};
if (m.sequence == 0)
return {}; // Genesis has no batch.
if (m.manifest_version != 2)
return fail_with(RecoveryError::unsupported);
auto trace_bytes = objects.get({m.batch.trace_sha256, m.batch.trace_bytes});
if (!trace_bytes)
return fail_with(trace_bytes.error() == ObjectError::io ? RecoveryError::store
: RecoveryError::tail);
auto trace = decode_trace(*trace_bytes);
const TraceHeader expected{static_cast<std::uint32_t>(Codec::profile),
key,
m.predecessor_commit,
m.predecessor_digest,
m.begin_tick,
m.end_tick};
if (!trace || trace->header != expected)
return fail_with(RecoveryError::tail);
auto predecessor = load_region_commit(db, key, m.sequence - 1, timeout);
if (!predecessor)
return predecessor.error().kind == LeaseStoreError::not_provisioned
? fail_with(RecoveryError::tail)
: std::unexpected{RecoveryFailure{RecoveryError::store, predecessor.error()}};
const auto digest = sha256(predecessor->manifest);
auto previous = decode_manifest(predecessor->manifest);
if (!digest || *digest != predecessor->head.digest ||
predecessor->head.digest != m.predecessor_digest ||
predecessor->head.commit != m.predecessor_commit || !previous ||
previous->end_tick != m.begin_tick)
return fail_with(RecoveryError::tail);
auto base = objects.get({previous->payload_sha256, previous->payload_bytes});
if (!base)
return fail_with(base.error() == ObjectError::io ? RecoveryError::store
: RecoveryError::tail);
auto replay = Codec::restore(*base, *previous, budget);
if (!replay)
return fail_with(RecoveryError::tail);
for (const auto& step : trace->steps) {
auto report = (*replay)->advance(step.report.tick, step.input);
if (!report || *report != step.report)
return fail_with(RecoveryError::tail);
}
const auto replayed = Codec::snapshot(**replay);
if (!replayed || !std::ranges::equal(*replayed, head_payload))
return fail_with(RecoveryError::tail);
// The head's durable outcomes are exactly those its trace derives.
const auto organisms = Codec::organisms(head_state);
const auto derived = derive_decisions(trace->steps, organisms.producer, organisms.consumer);
std::vector<BatchDecision> recorded;
for (const auto& d : durable)
if (d.commit_sequence == m.sequence)
recorded.push_back(d.decision);
if (derived != recorded || derived.size() != m.batch.decision_count)
return fail_with(RecoveryError::history);
return {};
}
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) {
const auto store_failure = [](const LeaseStoreFailure& f) {
return std::unexpected{RecoveryFailure{f.kind == LeaseStoreError::not_provisioned
? RecoveryError::not_provisioned
: RecoveryError::store,
f}};
};
auto committed = load_region_checkpoint(db, key, timeout);
if (!committed)
return store_failure(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);
// Durable permanent outcomes must agree with the restored state they finalized.
auto decisions = load_region_decisions(db, key, timeout);
if (!decisions)
return store_failure(decisions.error());
if (!Codec::reconcile(**state, *decisions, head))
return fail_with(RecoveryError::history);
if (auto tail = verify_tail<Codec>(db, objects, key, *manifest, *payload, **state, *decisions,
budget, timeout);
!tail)
return std::unexpected{tail.error()};
return typename Codec::Recovered{std::move(*committed), *manifest, std::move(*state),
std::move(*decisions)};
}
} // namespace
std::expected<CommittedCheckpoint, PublicationFailure> provision_environmental_region(