NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
state.h
Go to the documentation of this file.
1
10#pragma once
11
12#include <neograph/api.h>
16#include <mutex>
17#include <optional>
18#include <shared_mutex>
19#include <vector>
20#include <core/native_archive.h>
21
22namespace neograph::graph {
23namespace detail { class ManagedBudgetJournalAccess; }
24class NativeGraphCheckpoint final {
25private:
26 struct History {
27 std::vector<sp::Message> messages;
28 json projection = json::array();
29 };
30 struct BudgetBinding {
31 std::shared_ptr<UsageAccumulator> bank;
32 std::uint64_t ceiling = 0;
33 std::uint64_t original_ceiling = 0;
34 bool managed = false;
35 std::string owner_scope, thread_id, graph_identity;
36 std::string original_thread_id, fork_source_thread_id;
37 };
38 std::map<std::string, History> histories_;
39 std::map<std::string, ProviderLoopHistory::Entry> loops_;
40 std::vector<sp::runtime::Result> outcomes_;
41 std::optional<BudgetBinding> budget_;
42 json projection_;
43 NativeGraphCheckpoint() = default;
44 friend class GraphState;
45 friend class detail::ManagedBudgetJournalAccess;
46};
47
55class NEOGRAPH_API GraphState {
56public:
65 void init_channel(const std::string& name,
66 ReducerType type,
67 ReducerFn reducer,
68 const json& initial_value = json(),
69 ChannelLifecyclePolicy lifecycle = {});
70
76 json get(const std::string& channel) const;
77
82 template <typename T>
83 T get(const ChannelKey<T>& channel) const {
84 return get(channel.name()).template get<T>();
85 }
86
93 template <typename T>
94 std::optional<T> try_get(const ChannelKey<T>& channel) const {
95 if (!has_channel(channel.name())) return std::nullopt;
96 return get(channel);
97 }
98
103 std::vector<ChatMessage> get_messages() const;
105 std::vector<sp::Message> get_provider_messages(const std::string& channel = "messages") const;
107 std::optional<std::vector<sp::Message>> captured_provider_messages(
108 const std::string& channel = "messages") const;
109 void set_native_history_archive(std::shared_ptr<sp::NativeArchive> archive);
110 void copy_provider_history_from(const GraphState& source);
111 void set_provider_run_history(std::shared_ptr<ProviderLoopHistory> loops,
112 std::shared_ptr<ProviderOutcomes> outcomes);
113 std::vector<sp::runtime::Result> provider_outcomes() const;
114 void configure_budget_bank(std::shared_ptr<UsageAccumulator> bank, std::uint64_t ceiling,
115 bool managed, std::string owner_scope, std::string thread_id, std::string graph_identity);
116 std::shared_ptr<UsageAccumulator> budget_bank() const;
117 std::uint64_t budget_ceiling() const;
118 std::uint64_t budget_original_ceiling() const;
119 std::string budget_original_thread_id() const;
120 bool budget_managed() const;
121 void defer_budget_authority_restore();
122 void activate_budget_authority(bool observation_only = false);
123 void rebind_fork_budget_thread(std::string thread_id, bool original_in_memory_custody);
124 void validate_budget_context(const std::string& graph_identity, const std::string& thread_id) const;
125
131 void write(const std::string& channel, const json& value);
132
141 void apply_writes(const std::vector<ChannelWrite>& writes);
142
152 uint64_t channel_version(const std::string& channel) const;
153
162 uint64_t global_version() const;
163
168 json serialize() const;
169
174 void restore(const json& data);
177 json serialize_runtime() const;
179 json serialize_cache() const;
180 void restore_runtime(const json& data);
181
185
188 void restore_checkpoint(const json& data, const json& guard,
189 std::shared_ptr<const NativeGraphCheckpoint> native = {});
190 std::pair<json, std::shared_ptr<const NativeGraphCheckpoint>> checkpoint_snapshot() const;
191
192
197 std::vector<std::string> channel_names() const;
198
207 bool has_channel(const std::string& channel) const;
208
209 // v1.0 (9d): the v0.3 `run_cancel_token_` smuggling channel is gone.
210 // Cancel propagation now flows exclusively through
211 // `RunContext::cancel_token` on `NodeInput::ctx` (engine threads it
212 // through every dispatch; multi-Send fan-out copies the context by
213 // value, so the cancel token survives the isolated-state boundary
214 // without a serialize/restore hop).
215
216private:
217 std::map<std::string, Channel> channels_;
218 uint64_t global_version_ = 0;
219 using ProviderHistory = NativeGraphCheckpoint::History;
220 std::map<std::string, ProviderHistory> provider_histories_;
221 std::shared_ptr<sp::NativeArchive> native_history_archive_;
222 std::shared_ptr<ProviderLoopHistory> provider_loops_;
223 std::shared_ptr<ProviderOutcomes> provider_outcomes_;
224 std::optional<NativeGraphCheckpoint::BudgetBinding> provider_budget_;
225 bool defer_budget_authority_ = false;
226 std::optional<UsageAccumulator::AuthoritySnapshot> deferred_budget_authority_;
227 std::shared_ptr<UsageAccumulator> deferred_budget_bank_;
228 void admit_budget_binding(NativeGraphCheckpoint::BudgetBinding saved,
229 std::optional<UsageAccumulator::AuthoritySnapshot> authority);
230 json managed_budget_projection_locked() const;
231 void update_provider_history_locked(const std::string& channel, const json& before, const json& incoming,
232 const json& after, const std::shared_ptr<const std::vector<sp::Message>>& native);
233 void save_provider_history_locked(json& snapshot, bool durable = true, bool include_budget = true) const;
234 json serialize_runtime_locked(bool include_budget) const;
235 void restore_provider_history_locked(const json& snapshot, bool trusted_runtime_copy = false);
236 mutable std::shared_mutex mutex_;
237};
238
239} // namespace neograph::graph
NEOGRAPH_API export/import macro for shared-library builds.
Typed names for JSON-backed graph state channels.
Bind a channel name to its expected C++ value type.
Definition channel_key.h:19
Thread-safe container for all graph state channels.
Definition state.h:55
uint64_t channel_version(const std::string &channel) const
Get the version of a specific channel.
bool has_channel(const std::string &channel) const
Whether the graph declared this channel.
std::optional< T > try_get(const ChannelKey< T > &channel) const
Read a typed channel when it is declared.
Definition state.h:94
std::vector< ChatMessage > get_messages() const
Convenience method to read the "messages" channel as a vector of ChatMessage.
json serialize_cache() const
Cacheable channel/native inputs, excluding operational budget authority.
std::vector< sp::Message > get_provider_messages(const std::string &channel="messages") const
Full ordered provider history; JSON is only its portable projection.
void init_channel(const std::string &name, ReducerType type, ReducerFn reducer, const json &initial_value=json(), ChannelLifecyclePolicy lifecycle={})
Initialize a new channel with reducer and lifecycle policy.
json ephemeral_checkpoint_guard() const
Record which non-checkpointed channels existed at this checkpoint.
std::optional< std::vector< sp::Message > > captured_provider_messages(const std::string &channel="messages") const
Captured C++ history only; never interprets an arbitrary JSON channel.
json serialize_runtime() const
Isolated in-process Send workers need every live channel, including ephemeral values.
void write(const std::string &channel, const json &value)
Write a value to a single channel through its reducer (exclusive lock).
std::vector< std::string > channel_names() const
List all channel names in this state.
void restore(const json &data)
Restore durable channels from a JSON snapshot.
void apply_writes(const std::vector< ChannelWrite > &writes)
Apply a batch of channel writes atomically (exclusive lock).
uint64_t global_version() const
Get the state's write counter.
json serialize() const
Serialize checkpointed channel values and versions to JSON.
json get(const std::string &channel) const
Read a channel's current value (thread-safe, shared lock).
void restore_checkpoint(const json &data, const json &guard, std::shared_ptr< const NativeGraphCheckpoint > native={})
Reject missing/incompatible guards and lost ephemeral values before restoring a durable checkpoint.
T get(const ChannelKey< T > &channel) const
Read and convert a channel using a reusable typed key.
Definition state.h:83
Graph engine type definitions: channels, edges, nodes, events, and control flow.
std::function< json(const json &current, const json &incoming)> ReducerFn
Custom reducer function signature.
Definition types.h:103
ReducerType
Strategy for merging values when writing to a channel.
Definition types.h:61
Per-run metadata exposed to graph nodes.