Files
aevolution/include/evo/persistence/object_store.hpp
T
dingoandClaude Opus 5.5 5d4f59f901 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>
2026-09-28 15:25:27 -04:00

58 lines
2.6 KiB
C++

#pragma once
#include "evo/persistence/region_lease.hpp"
#include <cstddef>
#include <cstdint>
#include <expected>
#include <filesystem>
#include <memory>
#include <span>
#include <vector>
// Immutable content-addressed payload store on a trusted local/mounted volume
// (ADR-0061). Linux only. One namespace directory per world/region; the object
// key is the lowercase hex SHA-256 of its bytes. There is no delete or overwrite:
// unreferenced uploads are orphans and remain until a reachability-proven GC exists.
namespace evo::persistence {
enum class ObjectError : std::uint8_t {
invalid, // Bad root, namespace or object reference; nothing done.
missing, // No object with that key.
corrupt, // Length or digest mismatch, or not a regular file: quarantine.
bounds, // Larger than the configured maximum; rejected before allocation.
io, // Write/sync/link failure; the object is absent or unverified.
};
struct VerifiedObject {
Sha256Digest sha256{};
std::uint64_t bytes{};
friend bool operator==(const VerifiedObject&, const VerifiedObject&) = default;
};
class FilesystemObjectStore {
public:
// trusted_root must be an existing real directory (not a symlink). Namespace
// directories w<world>/r<region> are created 0700 and their parents synced.
[[nodiscard]] static std::expected<std::unique_ptr<FilesystemObjectStore>, ObjectError>
open(const std::filesystem::path& trusted_root, RegionKey key, std::size_t max_object_bytes);
FilesystemObjectStore(const FilesystemObjectStore&) = delete;
FilesystemObjectStore& operator=(const FilesystemObjectStore&) = delete;
FilesystemObjectStore(FilesystemObjectStore&&) = delete;
FilesystemObjectStore& operator=(FilesystemObjectStore&&) = delete;
~FilesystemObjectStore();
// Writes a private temporary file, fsyncs it, links it to its key without
// replacing anything, then fsyncs the directory. Identical bytes already stored
// are verified and reused; a differing existing object is corrupt, never replaced.
[[nodiscard]] std::expected<VerifiedObject, ObjectError>
put(std::span<const std::uint8_t> bytes) const;
// Reads only an exact-length regular file and verifies its SHA-256.
[[nodiscard]] std::expected<std::vector<std::uint8_t>, ObjectError>
get(const VerifiedObject& object) const;
[[nodiscard]] const std::filesystem::path& directory() const noexcept {
return directory_;
}
private:
FilesystemObjectStore(std::filesystem::path directory, int descriptor, std::size_t maximum);
std::filesystem::path directory_;
int descriptor_;
std::size_t maximum_;
};
} // namespace evo::persistence