18#include <shared_mutex>
20#include <core/native_archive.h>
22namespace neograph::graph {
23namespace detail {
class ManagedBudgetJournalAccess; }
24class NativeGraphCheckpoint final {
27 std::vector<sp::Message> messages;
28 json projection = json::array();
30 struct BudgetBinding {
31 std::shared_ptr<UsageAccumulator> bank;
32 std::uint64_t ceiling = 0;
33 std::uint64_t original_ceiling = 0;
35 std::string owner_scope, thread_id, graph_identity;
36 std::string original_thread_id, fork_source_thread_id;
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_;
43 NativeGraphCheckpoint() =
default;
44 friend class GraphState;
45 friend class detail::ManagedBudgetJournalAccess;
68 const json& initial_value = json(),
69 ChannelLifecyclePolicy lifecycle = {});
76 json
get(
const std::string& channel)
const;
84 return get(channel.name()).template get<T>();
95 if (!has_channel(channel.name()))
return std::nullopt;
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;
131 void write(
const std::string& channel,
const json& value);
180 void restore_runtime(
const json& data);
189 std::shared_ptr<const NativeGraphCheckpoint> native = {});
190 std::pair<json, std::shared_ptr<const NativeGraphCheckpoint>> checkpoint_snapshot()
const;
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_;
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.
Thread-safe container for all graph state channels.
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.
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.
Graph engine type definitions: channels, edges, nodes, events, and control flow.
std::function< json(const json ¤t, const json &incoming)> ReducerFn
Custom reducer function signature.
ReducerType
Strategy for merging values when writing to a channel.
Per-run metadata exposed to graph nodes.