Arachne 1.0
Arachne - the perpetual stitcher of Wikidata entities.
Loading...
Searching...
No Matches
store.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Yaroslav Riabtsev
3 * SPDX-License-Identifier: MIT
4 */
5
6#ifndef ARACHNE_PENELOPE_STORE_HPP
7#define ARACHNE_PENELOPE_STORE_HPP
8
9#include <cstddef>
10#include <filesystem>
11#include <optional>
12#include <stdexcept>
13#include <string>
14#include <vector>
15
16namespace arachne::penelope {
17
18/** A complete research_candidate_graph_plan_v1 artifact. */
19struct candidate_plan_descriptor final {
20 /** Validated research_candidate_graph_plan_v1 control contract. */
21 std::filesystem::path control_contract_path;
22 /** Bytes resolved from control_contract.plan_artifact.storage_ref. */
23 std::filesystem::path resolved_plan_payload_path;
24};
25
26/** Inputs for one destructive-in-staging candidate-graph rebuild. */
27struct candidate_snapshot_request final {
28 std::string run_id;
29 candidate_plan_descriptor plan;
30};
31
32enum class graph_domain { candidate };
33
34struct integrity_report final {
35 bool ok { false };
37};
38
39/** Paths and content identities of an immutable snapshot. */
40struct snapshot_result final {
42 std::string snapshot_id;
43 std::filesystem::path database_path;
44 std::filesystem::path export_path;
45 std::filesystem::path metadata_path;
46 std::string database_sha256;
47 std::string export_sha256;
48 std::size_t applied_inputs { 0 };
49 std::size_t skipped_inputs { 0 };
50 bool activated { false };
51 bool changed { false };
52};
53
54class store_error : public std::runtime_error {
55public:
56 using std::runtime_error::runtime_error;
57};
58
59/**
60 * Penelope's isolated SQLite persistence boundary.
61 *
62 * Candidate builds always start from an empty database and therefore cannot
63 * carry grey/group/greedy state from an older build. Product data is maintained
64 * directly by the dedicated inbox importer; it is deliberately outside this
65 * immutable candidate-snapshot store.
66 *
67 * Canonical SQLite files contain research data only. Run IDs, payload paths,
68 * and artifact hashes are written exclusively to metadata.json beside the
69 * database.
70 */
71class store final {
72public:
73 explicit store(std::filesystem::path root);
74
75 [[nodiscard]] snapshot_result
76 replace_candidate_snapshot(const candidate_snapshot_request& request);
77
78 /** Return the currently active immutable snapshot, if one exists. */
79 [[nodiscard]] std::optional<snapshot_result>
80 active_snapshot(graph_domain domain) const;
81
82 /** Run SQLite integrity, FK, and domain-specific structural checks. */
83 [[nodiscard]] integrity_report integrity_check(
84 graph_domain domain, const std::filesystem::path& database_path
85 ) const;
86
87 /**
88 * Checkpoint a staging database and require a clean integrity report.
89 * This must not be used to modify a database referenced by ACTIVE.
90 */
92 graph_domain domain, const std::filesystem::path& database_path
93 ) const;
94
95 /** Write the stable, table-ordered JSONL representation of a snapshot. */
96 [[nodiscard]] std::string export_jsonl(
97 graph_domain domain, const std::filesystem::path& database_path,
98 const std::filesystem::path& destination
99 ) const;
100
101 [[nodiscard]] const std::filesystem::path& root() const noexcept {
102 return root_;
103 }
104
105private:
106 std::filesystem::path root_;
107};
108
109} // namespace arachne::penelope
110
111#endif // ARACHNE_PENELOPE_STORE_HPP
cli_error(std::string message, const int exit_code=2)
Definition main.cpp:57
bool flag(const std::string_view name) const
Definition main.cpp:243
const std::string & require(const std::string_view name) const
Definition main.cpp:226
std::map< std::string, std::string, std::less<> > values_
Definition main.cpp:248
std::optional< std::string > optional(const std::string_view name) const
Definition main.cpp:235
std::set< std::string, std::less<> > present_flags_
Definition main.cpp:249
options(const std::vector< std::string > &arguments, const std::size_t first, const std::initializer_list< std::string_view > allowed, const std::initializer_list< std::string_view > flags={})
Definition main.cpp:187
envelope_record intake(const intake_request &request)
void capture_inbox_baseline(const std::filesystem::path &inbox_root)
snapshot_result replace_candidate_snapshot(const candidate_snapshot_request &request)
Definition store.cpp:1523
const std::filesystem::path & root() const noexcept
Definition store.hpp:101
std::filesystem::path root_
Definition store.hpp:106
store(std::filesystem::path root)
Definition store.cpp:1283
std::optional< snapshot_result > active_snapshot(graph_domain domain) const
Definition store.cpp:1307
void checkpoint_staging(graph_domain domain, const std::filesystem::path &database_path) const
Definition store.cpp:1323
integrity_report integrity_check(graph_domain domain, const std::filesystem::path &database_path) const
Definition store.cpp:1315
std::string export_jsonl(graph_domain domain, const std::filesystem::path &database_path, const std::filesystem::path &destination) const
Definition store.cpp:1337
acquired_artifact_v1 execute(const fetch_request_v1 &request) const
int main(const int argc, char **argv)
Definition main.cpp:2205
std::string join_strings(const std::span< const std::string > values, const std::string_view separator)
Definition main.cpp:1575
int command_inbox_baseline(const options &arguments)
Definition main.cpp:1218
const json & required_object(const json &parent, const std::string_view key)
Definition main.cpp:313
std::string validation_details(const arachnespace::contracts::validation_result &result, const std::string_view description)
Definition main.cpp:171
int command_fetch(const options &arguments)
Definition main.cpp:1506
std::size_t size_value(const json &object, const std::string_view key, const std::size_t fallback)
Definition main.cpp:281
written_run_manifest write_graph_run_manifest(const configuration &config, const std::string_view domain_directory, const std::string_view graph_domain, const std::string_view run_id, ordered_json configuration_hashes, ordered_json inputs, const arachne::penelope::snapshot_result &snapshot)
Definition main.cpp:601
void emit(const json &document)
Definition main.cpp:167
void atomic_write(const fs::path &destination, const std::string_view bytes, const bool replace)
Definition main.cpp:487
int command_contract_validate(const options &arguments)
Definition main.cpp:1117
ordered_json envelope_json(const arachne::coordination::envelope_record &envelope)
Definition main.cpp:560
json candidate_tables_for_viewer(const json &tables)
Definition main.cpp:916
fs::path resolved_path(const fs::path &path, const fs::path &relative_root)
Definition main.cpp:76
ordered_json wikidata_point_fetch_request(const configuration &config, const json &plan, const json &planned, const std::span< const std::string > entities, const std::string &request_id)
Definition main.cpp:1588
std::string read_bytes(const fs::path &path, const std::uintmax_t maximum_bytes, const std::string_view description)
Definition main.cpp:101
constexpr std::uintmax_t maximum_config_bytes
Definition main.cpp:49
resolved_snapshot_export resolve_snapshot_export(const configuration &config, const fs::path &control_path, const arachnespace::contracts::contract_name expected_contract, const std::string_view expected_kind)
Definition main.cpp:762
std::string form_encode(const std::string_view value)
Definition main.cpp:1548
void write_immutable_exact(const fs::path &destination, const std::string_view bytes, const std::string_view description)
Definition main.cpp:539
ordered_json snapshot_json(const arachne::penelope::snapshot_result &snapshot)
Definition main.cpp:579
int command_candidate_rebuild(const options &arguments)
Definition main.cpp:1354
int command_fetch_plan(const options &arguments)
Definition main.cpp:1782
void verify_external_source_snapshot(const configuration &config, const json &external_graph)
Definition main.cpp:836
arachne::ariadne::candidate_configuration candidate_configuration_from(const configuration &config)
Definition main.cpp:1081
constexpr std::uintmax_t maximum_export_bytes
Definition main.cpp:51
std::optional< fs::path > conventional_legacy_inbox()
Definition main.cpp:91
int command_intake(const options &arguments)
Definition main.cpp:1154
int command_cocoon_transition(const options &arguments)
Definition main.cpp:1177
json materialize_jsonl_export(const fs::path &path, const bool candidate)
Definition main.cpp:993
ordered_json actor_inbox_issues(const arachne::coordination::operational_ledger &ledger, const fs::path &inbox)
Definition main.cpp:1205
int dispatch(const std::vector< std::string > &arguments)
Definition main.cpp:2116
std::string policy_configuration_hash(const configuration &config, const std::string_view section)
Definition main.cpp:455
bool path_is_in_protected_legacy(const fs::path &path, const configuration &config)
Definition main.cpp:479
ordered_json wikidata_bulk_fetch_request(const json &plan, const json &planned, std::string request_id, std::string locator)
Definition main.cpp:1698
fs::path resolve_plan_artifact(const configuration &config, const fs::path &control_path, const std::string_view storage_ref)
Definition main.cpp:715
bool wikidata_entity_id(const std::string_view value)
Definition main.cpp:1566
configuration load_configuration(const fs::path &input_path)
Definition main.cpp:323
std::atomic< std::uint64_t > temporary_sequence
Definition main.cpp:53
ordered_json capabilities()
Definition main.cpp:2103
bool valid_logical_date(const std::string_view value)
Definition main.cpp:1056
int command_candidate_plan(const options &arguments)
Definition main.cpp:1951
int command_viewer_build(const options &arguments)
Definition main.cpp:2025
json parse_embedded_json(const json &row, const std::string_view key, json fallback)
Definition main.cpp:896
ordered_json issue_json(std::string path, std::string message)
Definition main.cpp:693
bool path_has_symlink(const fs::path &root, const fs::path &candidate)
Definition main.cpp:698
void verify_product_coverage(const json &external_graph, const json &product_tables)
Definition main.cpp:859
int command_inbox_verify(const options &arguments)
Definition main.cpp:1265
std::uint64_t unsigned_value(const json &object, const std::string_view key, const std::uint64_t default_value, const bool positive)
Definition main.cpp:252
int command_product_inbox(const bool apply)
Definition main.cpp:1297
fs::path command_path(const std::string &value)
Definition main.cpp:474
json read_json(const fs::path &path, const std::uintmax_t maximum_bytes, const std::string_view description)
Definition main.cpp:142
constexpr std::uintmax_t maximum_control_bytes
Definition main.cpp:50
std::string sha256(std::span< const std::byte > bytes)
Definition crypto.cpp:209
std::string sha256_file(const std::filesystem::path &path)
Definition crypto.cpp:221
inbox_result apply_product_inbox(const std::filesystem::path &repository_root)
const char * to_string(inbox_batch_status status) noexcept
Definition inbox.cpp:6133
inbox_result check_product_inbox(const std::filesystem::path &repository_root)
std::size_t rebuild_product_merge_hints(const std::filesystem::path &repository_root)
std::optional< fs::path > legacy_inbox
Definition main.cpp:299
std::vector< fs::path > protected_legacy_inboxes
Definition main.cpp:300
std::chrono::seconds candidate_lock_stale
Definition main.cpp:309
std::filesystem::path resolved_plan_payload_path
Definition store.hpp:23
std::filesystem::path control_contract_path
Definition store.hpp:21
std::vector< inbox_issue > issues
Definition inbox.hpp:36
std::vector< inbox_batch_report > batches
Definition inbox.hpp:46
std::size_t already_applied_count
Definition inbox.hpp:44
std::vector< std::string > problems
Definition store.hpp:36
std::filesystem::path metadata_path
Definition store.hpp:45
std::filesystem::path export_path
Definition store.hpp:44
std::filesystem::path database_path
Definition store.hpp:43