173 lines
6.6 KiB
C++
173 lines
6.6 KiB
C++
#pragma once
|
|
|
|
#include <Void/Build/Snapshot/Snapshot.hpp>
|
|
#include <Void/Build/State/RemoteCache.hpp>
|
|
#if defined(VBS_HAS_VOID_SQLITE)
|
|
#include <Void/Sqlite/Sqlite.hpp>
|
|
#endif
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <set>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace Void::Build::State {
|
|
|
|
inline constexpr int kStateSchemaVersion = 1;
|
|
|
|
struct BlobRef {
|
|
std::string sha256;
|
|
std::uint64_t size{0};
|
|
std::uint32_t mode{0644};
|
|
};
|
|
|
|
struct ActionSuccessRecord {
|
|
std::string action_key;
|
|
std::string profile_identity;
|
|
std::string target_name;
|
|
std::map<std::string, BlobRef, std::less<>> outputs; // relative/logical output -> CAS blob
|
|
std::map<std::string, std::string, std::less<>> discovered_inputs; // path -> sha256
|
|
std::vector<Snapshot::LookupGuard> lookup_guards;
|
|
std::int64_t recorded_epoch{0};
|
|
};
|
|
|
|
struct GraphGenerationRecord {
|
|
std::string generation_id;
|
|
std::string profile_name;
|
|
std::string profile_identity;
|
|
std::string snapshot_id;
|
|
std::string status; // "valid", "invalid", "stale"
|
|
std::string diagnostic;
|
|
std::uint64_t sequence{0};
|
|
};
|
|
|
|
struct GcStats {
|
|
std::size_t removed_blobs{0};
|
|
std::uint64_t freed_bytes{0};
|
|
std::size_t retained_blobs{0};
|
|
std::size_t cleaned_staging_files{0};
|
|
};
|
|
|
|
struct LeaseHandle {
|
|
std::string lease_id;
|
|
std::filesystem::path lease_file;
|
|
~LeaseHandle() noexcept;
|
|
LeaseHandle() = default;
|
|
LeaseHandle(const LeaseHandle &) = delete;
|
|
LeaseHandle &operator=(const LeaseHandle &) = delete;
|
|
LeaseHandle(LeaseHandle &&other) noexcept;
|
|
LeaseHandle &operator=(LeaseHandle &&other) noexcept;
|
|
void release() noexcept;
|
|
};
|
|
|
|
// Transactional persistent state & SHA-256 content-addressed local artifact cache (Plan 07).
|
|
// Uses Void::Sqlite when compiled with SQLite3 support and a durable atomic-replace
|
|
// transactional store via Void::FileSystem::replace_file in minimal environments (VL-011).
|
|
class StateStore {
|
|
public:
|
|
[[nodiscard]] static Void::Result::Result<StateStore, Void::Result::Error>
|
|
open(const std::filesystem::path &vbs_dir);
|
|
|
|
[[nodiscard]] const std::filesystem::path &root() const noexcept { return root_; }
|
|
[[nodiscard]] std::filesystem::path cas_blob_path(std::string_view sha256) const;
|
|
|
|
// CAS operations: publish immutable blobs BEFORE recording references in state.
|
|
[[nodiscard]] Void::Result::Result<BlobRef, Void::Result::Error>
|
|
store_blob_from_file(const std::filesystem::path &file_path);
|
|
|
|
[[nodiscard]] Void::Result::Result<bool, Void::Result::Error>
|
|
verify_blob(std::string_view sha256) const;
|
|
|
|
[[nodiscard]] Void::Result::Result<void, Void::Result::Error>
|
|
materialize_blob(const BlobRef &ref, const std::filesystem::path &destination) const;
|
|
|
|
// Transactional action cache queries & publication.
|
|
[[nodiscard]] Void::Result::Result<std::optional<ActionSuccessRecord>, Void::Result::Error>
|
|
lookup_action(std::string_view action_key) const;
|
|
|
|
[[nodiscard]] Void::Result::Result<void, Void::Result::Error>
|
|
record_action_success(const ActionSuccessRecord &record);
|
|
|
|
// Graph generation tracking.
|
|
[[nodiscard]] Void::Result::Result<void, Void::Result::Error>
|
|
record_generation(const GraphGenerationRecord &generation);
|
|
|
|
[[nodiscard]] Void::Result::Result<std::vector<GraphGenerationRecord>, Void::Result::Error>
|
|
list_generations(std::optional<std::string_view> profile_name = std::nullopt) const;
|
|
|
|
// Shared HTTP content-addressed action cache (Track D1). Configuration is runtime-only;
|
|
// the bearer token is never persisted. Remote reads always re-verify SHA-256 digests.
|
|
void configure_remote_cache(std::optional<RemoteCacheConfig> config);
|
|
[[nodiscard]] bool has_remote_cache() const noexcept { return remote_.has_value(); }
|
|
|
|
[[nodiscard]] Void::Result::Result<std::optional<std::string>, Void::Result::Error>
|
|
fetch_remote_action(std::string_view action_key) const;
|
|
[[nodiscard]] Void::Result::Result<void, Void::Result::Error>
|
|
publish_remote_action(std::string_view action_key, std::string_view json) const;
|
|
// Ensures a remote blob is present in the local CAS (downloading + verifying if needed).
|
|
[[nodiscard]] Void::Result::Result<bool, Void::Result::Error>
|
|
ensure_remote_blob(std::string_view sha256);
|
|
[[nodiscard]] Void::Result::Result<void, Void::Result::Error>
|
|
publish_remote_blob(std::string_view sha256) const;
|
|
|
|
// Leases protect active runs from GC deletion.
|
|
[[nodiscard]] Void::Result::Result<LeaseHandle, Void::Result::Error>
|
|
acquire_lease(std::string_view profile_name, const std::set<std::string> &pinned_blobs = {});
|
|
|
|
// Conservative crash recovery: removes uncommitted staging files and dead leases;
|
|
// never invents a successful action record.
|
|
[[nodiscard]] Void::Result::Result<std::size_t, Void::Result::Error> recover();
|
|
|
|
// Garbage collects unreachable CAS blobs outside active leases and pinned generations.
|
|
[[nodiscard]] Void::Result::Result<GcStats, Void::Result::Error>
|
|
gc(std::uint64_t max_cache_bytes = 0);
|
|
|
|
[[nodiscard]] std::size_t recorded_action_count() const;
|
|
|
|
private:
|
|
struct DatabaseSnapshot {
|
|
int schema_version{kStateSchemaVersion};
|
|
std::uint64_t next_sequence{1};
|
|
std::map<std::string, ActionSuccessRecord, std::less<>> actions;
|
|
std::vector<GraphGenerationRecord> generations;
|
|
};
|
|
|
|
struct CacheState;
|
|
|
|
explicit StateStore(std::filesystem::path root);
|
|
|
|
[[nodiscard]] Void::Result::Result<DatabaseSnapshot, Void::Result::Error>
|
|
load_db_locked() const;
|
|
[[nodiscard]] Void::Result::Result<void, Void::Result::Error>
|
|
save_db_locked(const DatabaseSnapshot &db) const;
|
|
[[nodiscard]] static Void::Result::Result<DatabaseSnapshot, Void::Result::Error>
|
|
parse_database_snapshot(std::string_view content, std::string_view origin);
|
|
[[nodiscard]] Void::Result::Result<DatabaseSnapshot, Void::Result::Error>
|
|
load_json_db_locked() const;
|
|
[[nodiscard]] Void::Result::Result<void, Void::Result::Error>
|
|
save_sqlite_db_locked(const DatabaseSnapshot &db) const;
|
|
|
|
std::filesystem::path root_;
|
|
std::filesystem::path cas_dir_;
|
|
std::filesystem::path staging_dir_;
|
|
std::filesystem::path leases_dir_;
|
|
std::filesystem::path db_file_;
|
|
std::filesystem::path sqlite_file_;
|
|
std::filesystem::path lock_file_;
|
|
#if defined(VBS_HAS_VOID_SQLITE)
|
|
mutable std::optional<Void::Sqlite::Database> sqlite_;
|
|
#endif
|
|
std::shared_ptr<CacheState> cache_;
|
|
std::optional<RemoteCasClient> remote_;
|
|
};
|
|
|
|
} // namespace Void::Build::State
|