NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
executor.h
Go to the documentation of this file.
1
41#pragma once
42
43#include <neograph/api.h>
45#include <neograph/graph/node.h>
50
51#include <asio/thread_pool.hpp>
52
53#include <atomic>
54#include <chrono>
55#include <exception>
56#include <functional>
57#include <map>
58#include <memory>
59#include <optional>
60#include <string>
61#include <string_view>
62#include <unordered_map>
63#include <vector>
64
65namespace neograph::graph {
66
67class GraphState;
68class GraphRegistry;
69
75struct RunContext;
76
77namespace detail {
81struct InvocationSlot;
83struct FailureRouting;
86class AttemptQuiescence;
89struct SendStep;
92class AttemptEffectLog;
95struct AttemptsAccess;
96} // namespace detail
97
108class NEOGRAPH_API NodeExecutor {
109public:
110 using RetryPolicyLookup = std::function<RetryPolicy(const std::string&)>;
112 using AttemptTimeoutLookup = std::function<std::optional<int>(const std::string&)>;
113
129 const std::map<std::string, std::unique_ptr<GraphNode>>& nodes,
130 const std::vector<ChannelDef>& channel_defs,
131 RetryPolicyLookup retry_policy_for,
132 asio::thread_pool* fan_out_pool = nullptr,
133 NodeCache* node_cache = nullptr);
134
136 NodeExecutor(const std::map<std::string, std::unique_ptr<GraphNode>>& nodes,
137 const std::vector<ChannelDef>& channel_defs,
138 RetryPolicyLookup retry_policy_for,
139 std::shared_ptr<const GraphRegistry> registry,
140 asio::thread_pool* fan_out_pool = nullptr,
141 NodeCache* node_cache = nullptr);
142
158 asio::awaitable<NodeResult> run_one_async(
159 const std::string& node_name,
160 int step,
161 GraphState& state,
162 const std::unordered_map<std::string, NodeResult>& replay,
164 const std::string& parent_cp_id,
165 const BarrierState& barrier_state,
166 std::vector<std::string>& trace,
167 const GraphStreamCallback& cb,
168 StreamMode stream_mode,
169 const RunContext& ctx);
170
181 asio::awaitable<NodeResult> execute_node_with_retry_async(
182 const std::string& node_name,
183 GraphState& state,
184 const GraphStreamCallback& cb,
185 StreamMode stream_mode,
186 const RunContext& ctx);
187
206 asio::awaitable<std::vector<NodeResult>> run_parallel_async(
207 const std::vector<std::string>& ready,
208 int step,
209 GraphState& state,
210 const std::unordered_map<std::string, NodeResult>& replay,
212 const std::string& parent_cp_id,
213 const BarrierState& barrier_state,
214 std::vector<std::string>& trace,
215 const GraphStreamCallback& cb,
216 StreamMode stream_mode,
217 const RunContext& ctx);
218
238 asio::awaitable<std::vector<StepRouting>> run_sends_async(
239 const std::vector<Send>& sends,
240 int step,
241 GraphState& state,
242 const std::unordered_map<std::string, NodeResult>& replay,
244 const std::string& parent_cp_id,
245 std::vector<std::string>& trace,
246 const GraphStreamCallback& cb,
247 StreamMode stream_mode,
248 const RunContext& ctx);
249
250private:
251 friend class GraphEngine;
252 friend struct detail::AttemptsAccess;
253
259 asio::awaitable<std::vector<StepRouting>> run_sends_impl_async(
260 const std::vector<Send>& sends,
261 int step,
262 GraphState& state,
263 const std::unordered_map<std::string, NodeResult>& replay,
265 const std::string& parent_cp_id,
266 std::vector<std::string>& trace,
267 const GraphStreamCallback& cb,
268 StreamMode stream_mode,
269 const RunContext& ctx,
270 std::vector<json>* failure_records,
271 const detail::SendStep* send_step = nullptr);
272
276 asio::awaitable<NodeResult> execute_attempts_async(
277 const std::string& node_name,
278 GraphState& state,
279 const GraphStreamCallback& cb,
280 StreamMode stream_mode,
281 const RunContext& ctx,
282 detail::InvocationSlot* slot);
283
288 asio::awaitable<void> run_dispatched_attempt_async(
289 GraphNode* node,
290 GraphState& state,
291 const GraphStreamCallback& cb,
292 const RunContext& ctx,
293 detail::InvocationSlot* slot,
294 std::string_view effect_namespace,
295 const std::optional<int>& attempt_timeout,
296 const std::string& node_name,
297 int attempt,
298 std::optional<NodeResult>& ok_result,
299 std::exception_ptr& failure,
300 std::shared_ptr<detail::AttemptEffectLog>& effects,
301 bool& attempt_stopped,
302 bool& attempt_timed_out);
303
306 void init_state(GraphState& state) const;
307
310 void apply_input(GraphState& state, const json& input) const;
311
325 void maybe_warn_serial_fanout(std::size_t width) const;
326
327 const std::map<std::string, std::unique_ptr<GraphNode>>& nodes_;
328 const std::vector<ChannelDef>& channel_defs_;
329 RetryPolicyLookup retry_policy_for_;
330 std::shared_ptr<const GraphRegistry> registry_;
331 asio::thread_pool* fan_out_pool_ = nullptr;
332 NodeCache* node_cache_ = nullptr;
333 mutable std::atomic<bool> warned_serial_fanout_{false};
338 const detail::FailureRouting* failure_routing_ = nullptr;
340 AttemptTimeoutLookup attempt_timeout_for_;
343 std::shared_ptr<detail::AttemptQuiescence> attempt_quiescence_;
344};
345
358NEOGRAPH_API bool wait_for_node_attempts(
359 std::chrono::milliseconds timeout = std::chrono::milliseconds::max());
360
361} // namespace neograph::graph
NEOGRAPH_API export/import macro for shared-library builds.
Per-run coordinator for checkpoint lifecycle operations.
Super-step loop execution engine for graph-based agent workflows.
Definition engine.h:648
Abstract base class for all graph nodes.
Definition node.h:96
Thread-safe container for all graph state channels.
Definition state.h:55
Per-node result cache.
Definition node_cache.h:75
Stateless-per-call node invocation dispatcher.
Definition executor.h:108
std::function< std::optional< int >(const std::string &)> AttemptTimeoutLookup
#364: a node's resolved attempt timeout in milliseconds, or none.
Definition executor.h:112
NodeExecutor(const std::map< std::string, std::unique_ptr< GraphNode > > &nodes, const std::vector< ChannelDef > &channel_defs, RetryPolicyLookup retry_policy_for, asio::thread_pool *fan_out_pool=nullptr, NodeCache *node_cache=nullptr)
asio::awaitable< NodeResult > run_one_async(const std::string &node_name, int step, GraphState &state, const std::unordered_map< std::string, NodeResult > &replay, CheckpointCoordinator &coord, const std::string &parent_cp_id, const BarrierState &barrier_state, std::vector< std::string > &trace, const GraphStreamCallback &cb, StreamMode stream_mode, const RunContext &ctx)
Execute a single node in the current super-step.
asio::awaitable< std::vector< NodeResult > > run_parallel_async(const std::vector< std::string > &ready, int step, GraphState &state, const std::unordered_map< std::string, NodeResult > &replay, CheckpointCoordinator &coord, const std::string &parent_cp_id, const BarrierState &barrier_state, std::vector< std::string > &trace, const GraphStreamCallback &cb, StreamMode stream_mode, const RunContext &ctx)
Execute all ready nodes concurrently via asio::experimental::make_parallel_group + wait_for_all.
asio::awaitable< std::vector< StepRouting > > run_sends_async(const std::vector< Send > &sends, int step, GraphState &state, const std::unordered_map< std::string, NodeResult > &replay, CheckpointCoordinator &coord, const std::string &parent_cp_id, std::vector< std::string > &trace, const GraphStreamCallback &cb, StreamMode stream_mode, const RunContext &ctx)
Execute a list of Send requests accumulated this step.
asio::awaitable< NodeResult > execute_node_with_retry_async(const std::string &node_name, GraphState &state, const GraphStreamCallback &cb, StreamMode stream_mode, const RunContext &ctx)
Inner retry loop with exponential backoff + NodeInterrupt short-circuit.
NodeExecutor(const std::map< std::string, std::unique_ptr< GraphNode > > &nodes, const std::vector< ChannelDef > &channel_defs, RetryPolicyLookup retry_policy_for, std::shared_ptr< const GraphRegistry > registry, asio::thread_pool *fan_out_pool=nullptr, NodeCache *node_cache=nullptr)
Construct with a per-engine registry overlay.
CheckpointCoordinator — owns the super-step checkpoint lifecycle.
NEOGRAPH_API bool wait_for_node_attempts(std::chrono::milliseconds timeout=std::chrono::milliseconds::max())
Wait until no node attempt dispatched off its run's orchestrator is running anywhere in this process ...
JSON → declarative topology parsing and runtime node linking.
Graph engine type definitions: channels, edges, nodes, events, and control flow.
std::function< void(const GraphEvent &)> GraphStreamCallback
Callback for receiving graph execution events.
Definition types.h:881
StreamMode
Bitfield flags for selecting which events to stream during execution.
Definition types.h:690
Graph node implementations: base class and built-in node types.
Per-node result cache (opt-in).
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
Retry policy for node execution with exponential backoff.
Definition types.h:668
Per-run dispatch metadata threaded through the engine and executor.
Definition run_context.h:71