Files
Library/include/Void/Update/Install.hpp
T

229 lines
7.3 KiB
C++

#pragma once
#include <Void/Archive/Archive.hpp>
#include <Void/Crypto/Crypto.hpp>
#include <Void/Functional/Result.hpp>
#include <Void/Update/Feed.hpp>
#include <Void/Update/Types.hpp>
#include <Void/Update/VersionStore.hpp>
#include <filesystem>
#include <functional>
#include <map>
#include <memory>
#include <mutex>
#include <optional>
#include <set>
#include <span>
#include <string>
#include <string_view>
#include <vector>
namespace Void::Update
{
/// Durable high-water sequence floor per (feed_id, channel) persisted via
/// atomic file replacement + directory sync. Monotonic across restarts, key
/// rotations, and civil-clock rollbacks; corrupt floor files fail with
/// RecoveryRequired instead of resetting to zero.
class ReplayFloorStore
{
public:
explicit ReplayFloorStore(std::filesystem::path file_path);
[[nodiscard]] Void::Result::Result<void> load();
[[nodiscard]] std::optional<std::uint64_t>
floor(std::string_view feed_id,
std::string_view channel) const;
/// Validate `sequence >= stored_floor` (or strictly `>` when
/// `require_strictly_greater` is true) and durably persist the new
/// high-water mark before returning.
[[nodiscard]] Void::Result::Result<void>
advance(std::string_view feed_id, std::string_view channel,
std::uint64_t sequence,
bool require_strictly_greater = false);
private:
std::filesystem::path m_path{};
mutable std::mutex m_mu{};
std::map<std::string, std::uint64_t, std::less<>> m_floors{};
bool m_loaded{false};
};
/// Explicit trust-root store supporting application bootstrap,
/// authenticated old-trust key rotation, operator-approved rotation, and
/// key revocation without rewriting historical signed bytes.
class TrustStore
{
public:
[[nodiscard]] static std::string
rotation_statement(std::string_view new_key_id,
const Ed25519PublicKey& new_public_key);
[[nodiscard]] Void::Result::Result<void> bootstrap(Keyring keys);
/// Rotate in `new_key_id` using an Ed25519 signature produced by an
/// active trusted key (`authorizing_key_id`) over
/// `rotation_statement(new_key_id, new_public_key)`.
[[nodiscard]] Void::Result::Result<void>
rotate_key_authenticated(
std::string new_key_id,
const Ed25519PublicKey& new_public_key,
std::span<const std::byte> signature,
std::string_view authorizing_key_id);
[[nodiscard]] Void::Result::Result<void>
rotate_key_operator(std::string new_key_id,
const Ed25519PublicKey& new_public_key);
[[nodiscard]] Void::Result::Result<void>
revoke_key(std::string_view key_id);
[[nodiscard]] Keyring snapshot() const;
[[nodiscard]] bool is_revoked(std::string_view key_id) const;
private:
mutable std::mutex m_mu{};
Keyring m_active{};
std::set<std::string, std::less<>> m_revoked{};
};
struct DownloadRequest {
std::string url{};
std::string expected_sha256{};
std::uint64_t expected_size{0};
std::uint64_t max_bytes{0};
std::optional<std::string> resume_validator{};
std::uint64_t resume_offset{0};
std::stop_token stop{};
};
struct DownloadMetadata {
std::string validator{};
bool resumed{false};
};
/// Abstract bounded artifact download transport. Concrete Net/HTTP
/// transports implement this interface without coupling Update to Net.
class ArtifactTransport
{
public:
virtual ~ArtifactTransport() = default;
[[nodiscard]] virtual Void::Result::Result<DownloadMetadata>
download(
const DownloadRequest& request,
const std::function<
Void::Result::Result<void>(std::span<const std::byte>)>&
on_chunk) = 0;
};
/// Offline local-file transport for deterministic update workflow and
/// installed-consumer testing.
class LocalFileArtifactTransport final : public ArtifactTransport
{
public:
void map_url(std::string url, std::filesystem::path file_path,
std::string validator = "v1");
void set_fail_next(bool fail) noexcept;
void set_truncate_bytes(std::optional<std::size_t> bytes) noexcept;
[[nodiscard]] Void::Result::Result<DownloadMetadata> download(
const DownloadRequest& request,
const std::function<
Void::Result::Result<void>(std::span<const std::byte>)>&
on_chunk) override;
private:
struct Entry {
std::filesystem::path path{};
std::string validator{};
};
mutable std::mutex m_mu{};
std::map<std::string, Entry, std::less<>> m_entries{};
bool m_fail_next{false};
std::optional<std::size_t> m_truncate_bytes{};
};
struct LifecycleHooks {
/// Required when ActivationMode::ExternalLifecycle is selected.
std::function<Void::Result::Result<void>(
const Semver::Version&, const std::filesystem::path&)>
activate_external{};
/// Cooperative health probe executed outside coordinator locks.
/// Defaults to immediately healthy if unset.
std::function<Void::Result::Result<bool>(
const Semver::Version&, const std::filesystem::path&)>
health_probe{};
/// Invoked before rollback on readiness failure to stop/join the
/// candidate cleanly.
std::function<void(const Semver::Version&)> stop_candidate{};
/// Returns currently pinned live generations (e.g. loaded plugins)
/// that pruning must not delete.
std::function<std::vector<Semver::Version>()> live_pinned_versions{};
/// Owning progress event snapshot invoked outside coordinator locks.
std::function<void(const InstallReceipt&)> on_progress{};
/// Optional post-commit cleanup hook (also usable to inject cleanup
/// failures in tests to verify CleanupPending without rollback).
std::function<Void::Result::Result<void>()>
post_commit_cleanup_hook{};
};
/// Serialized per-store installation orchestrator combining verified
/// selections, monotonic replay floors, exact-digest streaming downloads,
/// Archive atomic staging, VersionStore single-commit activation,
/// checksummed operation journaling, and pin-aware bounded pruning.
class InstallCoordinator
{
public:
InstallCoordinator(VersionStore& store,
ReplayFloorStore& floor_store,
std::shared_ptr<ArtifactTransport> transport,
LifecycleHooks hooks = {});
InstallCoordinator(const InstallCoordinator&) = delete;
InstallCoordinator& operator=(const InstallCoordinator&) = delete;
[[nodiscard]] Void::Result::Result<InstallReceipt>
install(const VerifiedSelection& selection,
const InstallOptions& options = {});
/// Idempotent recovery pass inspecting both the checksummed
/// operation journal and VersionStore's durable activation/health
/// evidence. Corrupt or unknown journal records halt with
/// RecoveryRequired without discarding evidence.
[[nodiscard]] Void::Result::Result<InstallReceipt> reconcile();
/// Retry post-commit cleanup/pruning after a CleanupPending
/// outcome. Never rolls back a committed version.
[[nodiscard]] Void::Result::Result<InstallReceipt>
retry_cleanup(const InstallOptions& options = {});
void shutdown() noexcept;
[[nodiscard]] bool is_shutdown() const noexcept;
[[nodiscard]] std::filesystem::path
operation_journal_path() const;
private:
VersionStore& m_store;
ReplayFloorStore& m_floor_store;
std::shared_ptr<ArtifactTransport> m_transport;
LifecycleHooks m_hooks{};
mutable std::mutex m_state_mu{};
bool m_active{false};
bool m_shutdown{false};
};
} // namespace Void::Update