Implement M7.4a checkpoint publication and verified recovery

ADR-0061 publishes environmental-v1 full checkpoints. Manifest v1 is a
fixed 280-byte record (identity, chain, lease, versions, build, zero
RNG/history declarations, durability policy, payload length/SHA-256)
that PostgreSQL parses and digests itself. Schema v3 adds region_commits
with a digest CHECK and a composite head FK, owner-managed retained
builds, fenced publish/load functions and owner-only genesis; it drops
the bare v2 head kernel and fails closed while v2 heads exist.

Linux evo::region_store adds OpenSSL SHA-256, an immutable
content-addressed filesystem object store (temp+fsync+link, no
overwrite/delete, O_NOFOLLOW exact-length verified reads), and
publication (validate -> upload -> read back -> fenced transaction ->
receipt check) and recovery (re-digest, allowlist, verify, full restore).
Portable evo::region_checkpoint holds the codec and payload validation.

Validated: Windows CTest 56/56; WSL Release, clang-tidy and ASan/UBSan
private-PostgreSQL suites; four SQL and one host mutation each caught.
M7.4 stays open for 7.4b (lineage archive codec and history binding).
No Docker/Gitea CI, remote store, process kill or deployment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-09-28 15:25:27 -04:00
co-authored by Claude Opus 5.5
parent 07116446c6
commit 5d4f59f901
32 changed files with 2253 additions and 145 deletions
+154
View File
@@ -0,0 +1,154 @@
#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;
PublicationFailure fail(PublicationError kind) {
return {kind, {}, {}};
}
// Encodes the candidate, validates the exact payload bytes as recovery would,
// uploads and reads back. Returns the manifest bytes ready for the database.
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 || !restore_environmental_payload(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)};
}
} // 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) {
auto payload = opening.snapshot();
if (!payload || opening.progress().tick != 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, opening.tick_microseconds(), build,
payload->size(), *digest);
if (!manifest)
return std::unexpected{fail(PublicationError::candidate)};
auto bytes = stage(objects, *payload, *manifest, restore_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 auto kind = done.error().kind == PgError::unknown ? PublicationError::unknown
: PublicationError::rejected;
return std::unexpected{
PublicationFailure{kind,
{done.error().kind == PgError::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);
}
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) {
const auto previous = decode_manifest(predecessor.manifest);
auto payload = state.snapshot();
if (!previous || !payload)
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,
state.progress().tick, build, payload->size(), *digest);
if (!manifest)
return std::unexpected{fail(PublicationError::candidate)};
auto bytes = stage(objects, *payload, *manifest, restore_budget);
if (!bytes)
return std::unexpected{bytes.error()};
auto published = publish_region_checkpoint(db, *bytes, timeout);
return confirm(published, std::move(*bytes), *manifest);
}
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) {
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) ||
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 = restore_environmental_payload(*payload, *manifest, restore_budget);
if (!state)
return fail_with(RecoveryError::payload);
return RecoveredCheckpoint{std::move(*committed), *manifest, std::move(*state)};
}
} // namespace evo::persistence