NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
sqlite_checkpoint.h
Go to the documentation of this file.
1
45#pragma once
46
47#include <neograph/api.h>
49#include <chrono>
50#include <functional>
51#include <memory>
52#include <mutex>
53#include <string>
54
55// Forward-declare so this header doesn't drag the sqlite3 C header
56// into every TU that includes it.
57struct sqlite3;
58
59namespace neograph::graph {
60
69class NEOGRAPH_API SqliteCheckpointStore : public CheckpointStore {
70public:
71 // Called after BEGIN IMMEDIATE has acquired the SQLite writer and before
72 // any checkpoint mutation. Throw to roll the transaction back. The guard
73 // may inspect this connection but must not start another transaction.
74 using WriteGuard = std::function<void(sqlite3*, const std::string& thread_id)>;
77 explicit SqliteCheckpointStore(const std::string& db_path);
81 SqliteCheckpointStore(const std::string& db_path,
82 std::chrono::milliseconds busy_timeout);
83 SqliteCheckpointStore(const std::string& db_path,
84 std::chrono::milliseconds busy_timeout,
85 WriteGuard write_guard);
86
87 ~SqliteCheckpointStore() override;
88
89 // sqlite3* is a unique resource — no copying or moving.
91 SqliteCheckpointStore& operator=(const SqliteCheckpointStore&) = delete;
92
93 void save(const Checkpoint& cp) override;
94 std::optional<Checkpoint> load_latest(const std::string& thread_id) override;
95 std::optional<Checkpoint> load_by_id(const std::string& id) override;
96 std::vector<Checkpoint> list(const std::string& thread_id,
97 int limit = 100) override;
98 void delete_thread(const std::string& thread_id) override;
99
100 void put_writes(const std::string& thread_id,
101 const std::string& parent_checkpoint_id,
102 const PendingWrite& write) override;
103 std::vector<PendingWrite> get_writes(
104 const std::string& thread_id,
105 const std::string& parent_checkpoint_id) override;
106 void clear_writes(const std::string& thread_id,
107 const std::string& parent_checkpoint_id) override;
108
110 bool requires_managed_budget(const std::string& thread_id) override;
111 asio::awaitable<bool> requires_managed_budget_async(std::string thread_id) override;
112
113 std::shared_ptr<OwnedManagedBudgetLease> acquire_managed_budget_lease(
114 const ManagedBudgetLeaseScope& scope, const std::string& expected_checkpoint_id,
115 const std::string& expected_checkpoint_commitment) override;
116 asio::awaitable<std::shared_ptr<OwnedManagedBudgetLease>> acquire_managed_budget_lease_async(
117 ManagedBudgetLeaseScope scope, std::string expected_checkpoint_id,
118 std::string expected_checkpoint_commitment) override;
119 ManagedBudgetEffectReceipt begin_managed_budget_effect(
120 const std::shared_ptr<OwnedManagedBudgetLease>& lease, const std::string& effect_id,
121 std::uint64_t exact_claim_amount, const std::string& prepared_request_digest) override;
122 asio::awaitable<ManagedBudgetEffectReceipt> begin_managed_budget_effect_async(
123 std::shared_ptr<OwnedManagedBudgetLease> lease, std::string effect_id,
124 std::uint64_t exact_claim_amount, std::string prepared_request_digest) override;
125 void settle_managed_budget_effect(
126 const std::shared_ptr<OwnedManagedBudgetLease>& lease,
127 const ManagedBudgetEffectReceipt& effect, sp::runtime::Result genuine_outcome,
128 const UsageAccumulator::AuthoritySnapshot& authority) override;
129 asio::awaitable<void> settle_managed_budget_effect_async(
130 std::shared_ptr<OwnedManagedBudgetLease> lease, ManagedBudgetEffectReceipt effect,
131 sp::runtime::Result genuine_outcome, UsageAccumulator::AuthoritySnapshot authority) override;
132 void publish_managed_budget_checkpoint(
133 const std::shared_ptr<OwnedManagedBudgetLease>& lease, const Checkpoint& checkpoint) override;
134 asio::awaitable<void> publish_managed_budget_checkpoint_async(
135 std::shared_ptr<OwnedManagedBudgetLease> lease, Checkpoint checkpoint) override;
136 void release_managed_budget_lease(
137 const std::shared_ptr<OwnedManagedBudgetLease>& lease) override;
138 asio::awaitable<void> release_managed_budget_lease_async(
139 std::shared_ptr<OwnedManagedBudgetLease> lease) override;
140
144
149 size_t blob_count();
150
151private:
152 void ensure_schema();
153 void exec_ddl(const char* sql);
155 void save_locked(const Checkpoint& checkpoint);
156
159 sqlite3* db_ = nullptr;
160 std::mutex db_mutex_;
161 WriteGuard write_guard_;
162};
163
164} // namespace neograph::graph
NEOGRAPH_API export/import macro for shared-library builds.
Checkpoint system for graph execution state persistence and time-travel.
Abstract interface for checkpoint persistence backends.
Definition checkpoint.h:400
Actual write-ahead claim, bound to one original generation and owned actor.
Definition checkpoint.h:96
Persistent CheckpointStore backed by a SQLite database file.
void save(const Checkpoint &cp) override
Save a checkpoint.
SqliteCheckpointStore(const std::string &db_path, std::chrono::milliseconds busy_timeout)
Configure how long writes wait for a competing SQLite writer, and how long opening waits for another ...
void delete_thread(const std::string &thread_id) override
Delete all checkpoints for a thread.
std::vector< Checkpoint > list(const std::string &thread_id, int limit=100) override
List checkpoints for a thread, most recently saved first, one entry per ID; the first entry is load_l...
std::optional< Checkpoint > load_by_id(const std::string &id) override
Load a checkpoint by its unique ID.
SqliteCheckpointStore(const std::string &db_path)
void put_writes(const std::string &thread_id, const std::string &parent_checkpoint_id, const PendingWrite &write) override
Record a successful node execution within an in-progress super-step.
void drop_schema()
Drop and recreate checkpoint payload tables.
std::vector< PendingWrite > get_writes(const std::string &thread_id, const std::string &parent_checkpoint_id) override
Load all pending writes attached to a parent checkpoint.
bool requires_managed_budget(const std::string &thread_id) override
Monotonic denial evidence, independent of retained checkpoint JSON.
void clear_writes(const std::string &thread_id, const std::string &parent_checkpoint_id) override
Discard pending writes for a parent checkpoint once its successor super-step has been fully committed...
size_t blob_count()
Number of distinct channel-value blobs currently held.
std::optional< Checkpoint > load_latest(const std::string &thread_id) override
Load the most recently saved checkpoint for a thread.
Serialized snapshot of graph execution state at a single super-step.
Definition checkpoint.h:173
Successful node writes recorded within an in-progress super-step.
Definition checkpoint.h:242