NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
coordinator.h
Go to the documentation of this file.
1
26#pragma once
27
28#include <neograph/api.h>
32#include <memory>
33#include <optional>
34#include <string>
35#include <unordered_map>
36#include <vector>
37
38namespace neograph { class HookRuntime; }
39namespace sp { class NativeArchive; }
40
41namespace neograph::graph {
42
43class GraphState;
44class CancelToken;
45namespace detail { struct SubgraphWriteJournal; }
46
49 std::string owner_scope;
50 std::string run_id;
51 std::string trace_id;
52 std::shared_ptr<CancelToken> cancellation;
53 std::optional<std::chrono::system_clock::time_point> parent_deadline;
54};
55
64 bool have_cp = false;
65
66 std::string checkpoint_id;
68 std::shared_ptr<const NativeGraphCheckpoint> native_history;
69 json metadata;
70 int start_step = 0;
71 CheckpointPhase phase = CheckpointPhase::Completed;
74 std::string current_node;
75 std::vector<std::string> next_nodes;
76
80 std::unordered_map<std::string, NodeResult> replay_results;
81
86 std::shared_ptr<const Checkpoint> managed_budget_source;
87};
88
98NEOGRAPH_API std::vector<PendingWrite> resume_pending_writes(CheckpointStore& store,
99 const Checkpoint& checkpoint);
100
111NEOGRAPH_API void copy_resume_pending_writes(CheckpointStore& store, const Checkpoint& source,
112 const Checkpoint& target);
113
121class NEOGRAPH_API CheckpointCoordinator {
122public:
125 CheckpointCoordinator(std::shared_ptr<CheckpointStore> store,
126 std::string thread_id,
127 std::shared_ptr<::neograph::HookRuntime> hook_runtime = {},
128 CheckpointHookContext hook_context = {},
129 std::shared_ptr<sp::NativeArchive> native_archive = {},
130 std::shared_ptr<detail::SubgraphWriteJournal> native_journal = {});
131
132 void set_managed_budget_lease(std::shared_ptr<OwnedManagedBudgetLease> lease);
133
135 bool enabled() const noexcept { return store_ != nullptr && !thread_id_.empty(); }
136
143 bool records_pending_writes(const std::string& parent_cp_id) const noexcept {
144 return enabled() && !parent_cp_id.empty();
145 }
146
147 const std::shared_ptr<CheckpointStore>& store() const noexcept { return store_; }
148 const std::string& thread_id() const noexcept { return thread_id_; }
149
154 std::string save_super_step(const GraphState& state,
155 const std::string& current_node,
156 const std::vector<std::string>& next_nodes,
157 CheckpointPhase phase,
158 int step,
159 const std::string& parent_id,
160 const BarrierState& barrier_state) const;
161
170
174 asio::awaitable<ResumeContext> load_for_resume_async() const;
182 asio::awaitable<ResumeContext> load_for_resume_by_id_async(
183 std::string checkpoint_id) const;
184
192 void record_pending_write(const std::string& parent_cp_id,
193 const std::string& task_id,
194 const std::string& task_path,
195 const std::string& node_name,
196 const NodeResult& nr,
197 int step) const;
198
207 void clear_pending_writes(const std::string& parent_cp_id) const;
208
209 // ── Async peers (Stage 3 / Sem 3.6 incremental) ─────────────────────
210 //
211 // Each routes to the matching CheckpointStore::*_async so the
212 // coroutine engine path doesn't block the io_context on
213 // checkpoint I/O. Behaviour identical to the sync versions.
214
215 asio::awaitable<std::string> save_super_step_async(
216 const GraphState& state,
217 const std::string& current_node,
218 const std::vector<std::string>& next_nodes,
219 CheckpointPhase phase,
220 int step,
221 const std::string& parent_id,
222 const BarrierState& barrier_state) const;
223
226 asio::awaitable<std::string> save_super_step_async(
227 const GraphState& state,
228 const std::string& current_node,
229 const std::vector<std::string>& next_nodes,
230 CheckpointPhase phase,
231 int step,
232 const std::string& parent_id,
233 const BarrierState& barrier_state,
234 const json& metadata) const;
235
240 asio::awaitable<Checkpoint> commit_super_step_async(
241 const GraphState& state,
242 const std::string& current_node,
243 const std::vector<std::string>& next_nodes,
244 int step,
245 const std::string& parent_id,
246 const BarrierState& barrier_state,
247 const json& metadata = json()) const;
248
249 asio::awaitable<void> record_pending_write_async(
250 const std::string& parent_cp_id,
251 const std::string& task_id,
252 const std::string& task_path,
253 const std::string& node_name,
254 const NodeResult& nr,
255 int step) const;
256
257 asio::awaitable<void> clear_pending_writes_async(
258 const std::string& parent_cp_id) const;
259
260private:
261 asio::awaitable<void> publish_checkpoint_async(const Checkpoint& checkpoint) const;
262 void capture_state(Checkpoint& checkpoint, const GraphState& state) const;
263
264 std::shared_ptr<CheckpointStore> store_;
265 std::string thread_id_;
266 std::shared_ptr<::neograph::HookRuntime> hook_runtime_;
267 CheckpointHookContext hook_context_;
268 std::shared_ptr<sp::NativeArchive> native_archive_;
269 std::shared_ptr<detail::SubgraphWriteJournal> native_journal_;
270 std::shared_ptr<OwnedManagedBudgetLease> managed_budget_lease_;
271 bool native_memory_ = false;
272};
273
274} // namespace neograph::graph
NEOGRAPH_API export/import macro for shared-library builds.
Checkpoint system for graph execution state persistence and time-travel.
CheckpointPhase
Phase at which a Checkpoint was produced.
Definition checkpoint.h:143
Per-run coordinator for checkpoint lifecycle operations.
asio::awaitable< std::string > save_super_step_async(const GraphState &state, const std::string &current_node, const std::vector< std::string > &next_nodes, CheckpointPhase phase, int step, const std::string &parent_id, const BarrierState &barrier_state, const json &metadata) const
Internal extension used by subgraph execution to persist its cumulative write journal without changin...
ResumeContext load_for_resume() const
Load the latest checkpoint + all pending writes attached to it.
void record_pending_write(const std::string &parent_cp_id, const std::string &task_id, const std::string &task_path, const std::string &node_name, const NodeResult &nr, int step) const
Durably record a completed node's writes under parent_cp_id.
asio::awaitable< Checkpoint > commit_super_step_async(const GraphState &state, const std::string &current_node, const std::vector< std::string > &next_nodes, int step, const std::string &parent_id, const BarrierState &barrier_state, const json &metadata=json()) const
Persist one completed super-step, settle the prior pending-write bucket, and return the exact deeply ...
bool records_pending_writes(const std::string &parent_cp_id) const noexcept
True when record_pending_write[_async] would store something: the coordinator is enabled and the step...
void clear_pending_writes(const std::string &parent_cp_id) const
Drop the pending-writes log for a parent cp after its super-step has been successfully committed.
CheckpointCoordinator(std::shared_ptr< CheckpointStore > store, std::string thread_id, std::shared_ptr<::neograph::HookRuntime > hook_runtime={}, CheckpointHookContext hook_context={}, std::shared_ptr< sp::NativeArchive > native_archive={}, std::shared_ptr< detail::SubgraphWriteJournal > native_journal={})
asio::awaitable< ResumeContext > load_for_resume_by_id_async(std::string checkpoint_id) const
Load one exact checkpoint and its pending writes by ID.
asio::awaitable< ResumeContext > load_for_resume_async() const
Async peer of load_for_resume (Sem 3.7.5).
std::string save_super_step(const GraphState &state, const std::string &current_node, const std::vector< std::string > &next_nodes, CheckpointPhase phase, int step, const std::string &parent_id, const BarrierState &barrier_state) const
Write a super-step snapshot.
Abstract interface for checkpoint persistence backends.
Definition checkpoint.h:400
Thread-safe container for all graph state channels.
Definition state.h:55
NEOGRAPH_API void copy_resume_pending_writes(CheckpointStore &store, const Checkpoint &source, const Checkpoint &target)
Record under target the pending writes that resuming source replays (#390).
NEOGRAPH_API std::vector< PendingWrite > resume_pending_writes(CheckpointStore &store, const Checkpoint &checkpoint)
The pending writes that resuming checkpoint replays (#390).
Graph engine type definitions: channels, edges, nodes, events, and control flow.
Pure routing / next-step-planning logic extracted from GraphEngine.
std::map< std::string, std::set< std::string > > BarrierState
Per-barrier signal bookkeeping.
Definition scheduler.h:77
Per-attempt host identity and control inherited by checkpoint hook delivery.
Definition coordinator.h:48
Serialized snapshot of graph execution state at a single super-step.
Definition checkpoint.h:173
Extended result returned by node execution.
Definition types.h:902
Context needed to resume execution from the last saved snapshot.
Definition coordinator.h:63
std::shared_ptr< const Checkpoint > managed_budget_source
Exact loaded source retained for the standalone bank head commitment.
Definition coordinator.h:86
json metadata
Checkpoint metadata including lifecycle guard.
Definition coordinator.h:69
BarrierState barrier_state
Barrier accumulators for in-flight AND-joins.
Definition coordinator.h:84
json channel_values
Serialized GraphState at cp time.
Definition coordinator.h:67
std::string current_node
The checkpoint's current_node: for a Before checkpoint, the node whose interrupt_before gate saved it...
Definition coordinator.h:74
std::unordered_map< std::string, NodeResult > replay_results
Partially-completed super-step writes, keyed by task_id.
Definition coordinator.h:80
int start_step
Phase-adjusted step to re-enter at.
Definition coordinator.h:70