NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
run_context.h
Go to the documentation of this file.
1
5#pragma once
6
12#include <neograph/types.h>
13
14#include <asio/any_io_executor.hpp>
15
16#include <atomic>
17#include <chrono>
18#include <cstdint>
19#include <memory>
20#include <optional>
21#include <string>
22namespace sp { class NativeArchive; }
23
24namespace neograph::graph {
25
26struct ProviderOutcomes {
27 mutable std::mutex mutex;
28 std::vector<sp::runtime::Result> values;
29 void add(sp::runtime::Result value) {
30 std::lock_guard lock(mutex);
31 values.push_back(std::move(value));
32 }
33 std::vector<sp::runtime::Result> snapshot() const {
34 std::lock_guard lock(mutex);
35 return values;
36 }
37};
38
42 struct Entry {
43 std::vector<sp::Message> messages;
44 std::uint64_t turns = 0;
45 bool client_calls_ready = false;
46 };
47 mutable std::mutex mutex;
48 std::map<std::string, Entry> entries;
49 Entry get(const std::string& task) const {
50 std::lock_guard lock(mutex);
51 const auto found = entries.find(task);
52 return found == entries.end() ? Entry{} : found->second;
53 }
54 void set(const std::string& task, Entry entry) {
55 std::lock_guard lock(mutex);
56 entries.insert_or_assign(task, std::move(entry));
57 }
58 std::map<std::string, Entry> snapshot() const {
59 std::lock_guard lock(mutex);
60 return entries;
61 }
62};
63
71struct RunContext {
73 std::shared_ptr<CancelToken> cancel_token;
74
76 std::shared_ptr<UsageAccumulator> usage;
77 std::shared_ptr<ProviderOutcomes> provider_outcomes;
78 std::shared_ptr<ProviderLoopHistory> provider_loop_history;
79 std::function<void(const sp::Event&)> on_provider_event;
80 std::shared_ptr<sp::NativeArchive> native_history_archive;
81
83 std::uint64_t model_token_budget = 0;
84 std::shared_ptr<std::atomic_bool> budget_exhausted;
86 std::string run_id;
88 std::shared_ptr<CancelToken> budget_cancel_token;
90 std::shared_ptr<OwnedManagedBudgetLease> managed_budget_lease;
91 std::shared_ptr<CheckpointStore> managed_budget_store;
92
94 std::optional<std::chrono::steady_clock::time_point> deadline;
95
98 bool shared_scheduler = false;
99
102 asio::any_io_executor dispatch_executor;
103
105 std::string trace_id;
106
108 std::string thread_id;
109
112 std::uint64_t cache_execution_id = 0;
113
115 int step = 0;
116
118 StreamMode stream_mode = StreamMode::ALL;
119
126 std::optional<json> resume_value;
127
138
140 std::shared_ptr<Store> store;
141
146
148 std::shared_ptr<ToolExecutionController> tool_execution_controller;
149
152
153};
154
165NEOGRAPH_API std::shared_ptr<ProviderCallBroker> provider_call_broker(const RunContext& ctx);
166inline ProviderCallIdentity make_provider_call_identity(const RunContext& ctx,
167 const std::string& node, std::uint64_t ordinal = 0) {
168 ProviderCallIdentity identity;
169 identity.owner_scope = ctx.tool_execution_identity.owner_scope;
170 identity.run_id = ctx.run_id;
171 identity.thread_id = ctx.thread_id;
172 identity.task_id = make_tool_execution_context(ctx).effect_task_id;
173 identity.node_name = node;
174 identity.call_ordinal = ordinal;
175 identity.usage = ctx.usage;
176 identity.model_token_budget = ctx.model_token_budget;
177 identity.budget_exhausted = ctx.budget_exhausted;
178 identity.budget_cancel_token = ctx.budget_cancel_token;
179 identity.managed_budget_lease = ctx.managed_budget_lease;
180 identity.managed_budget_store = ctx.managed_budget_store;
181 return identity;
182}
183
185inline void record_usage(const RunContext& ctx, const sp::runtime::Result& result) {
186 if (!result) throw std::runtime_error("Provider returned no outcome");
187 if (ctx.usage && ctx.model_token_budget == 0 && !provider_call_broker(ctx))
188 ctx.usage->add(outcome_usage(*result));
189 if (ctx.provider_outcomes) ctx.provider_outcomes->add(result);
190}
191
193inline asio::awaitable<sp::runtime::Result> observe_provider_result(
194 const RunContext& ctx, asio::awaitable<sp::runtime::Result> operation) {
195 try {
196 co_return co_await std::move(operation);
197 } catch (const ProviderOutcomeError& error) {
198 // An uncertain dispatch carries no outcome, so there is nothing to
199 // record; it must still leave as the typed uncertain failure.
200 if (error.outcome()) record_usage(ctx, error.outcome());
201 throw;
202 } catch (const ProviderFailure& error) {
203 record_usage(ctx, error.outcome());
204 throw;
205 }
206}
207
208} // namespace neograph::graph
Cooperative cancellation primitive for graph runs.
How provider stop reasons reach callers.
Definition types.h:391
Post-dispatch host failure retaining the exact drained provider outcome.
Definition provider.h:204
Cross-thread shared memory store (LangGraph Store equivalent).
Graph engine type definitions: channels, edges, nodes, events, and control flow.
StreamMode
Bitfield flags for selecting which events to stream during execution.
Definition types.h:690
Host-owned, invocation-scoped boundary for built-in Core provider calls.
void record_usage(const RunContext &ctx, const sp::runtime::Result &result)
Fold exactly the actual nullable report, never a reservation or charge.
NEOGRAPH_API ToolExecutionContext make_tool_execution_context(const RunContext &ctx)
Populate the mediated Tool execution context for a node invocation: the run's (or attempt's) cancel t...
asio::awaitable< sp::runtime::Result > observe_provider_result(const RunContext &ctx, asio::awaitable< sp::runtime::Result > operation)
Owned failures must not erase the real outcome or its nullable usage.
Per-invocation context for a cooperative asynchronous tool.
Definition tool.h:39
std::string effect_task_id
Stable Core node invocation slot, populated by GraphEngine.
Definition tool.h:46
Stable request identity used for resource accounting and audit correlation.
std::string owner_scope
Stable caller or tenant scope. Empty is normalized to "anonymous".
std::uint64_t call_ordinal
Stable per-task ordinal; inner loops assign each provider turn explicitly.
Engine-owned inner-loop continuations, keyed by the compiled task slot.
Definition run_context.h:41
Per-run dispatch metadata threaded through the engine and executor.
Definition run_context.h:71
std::shared_ptr< ToolExecutionController > tool_execution_controller
Shared host admission boundary for every tool call in this run.
std::shared_ptr< CancelToken > budget_cancel_token
Shared sibling-cancellation scope for budget-aware nodes.
Definition run_context.h:88
std::uint64_t cache_execution_id
Engine-assigned identity for this run or resume call.
std::shared_ptr< OwnedManagedBudgetLease > managed_budget_lease
Host-only standalone currency; never recovered from caller JSON.
Definition run_context.h:90
std::shared_ptr< CancelToken > cancel_token
Operation-scoped cooperative cancellation handle.
Definition run_context.h:73
std::string run_id
Durable Program run identity used by host-brokered journal effects.
Definition run_context.h:86
int step
Current super-step index.
bool shared_scheduler
RunMetadata::shared_scheduler: blocking node bodies should not hold the calling executor,...
Definition run_context.h:98
asio::any_io_executor dispatch_executor
RunMetadata::dispatch_executor: where a node attempt that needs its own worker runs.
std::string thread_id
Mirrors RunConfig::thread_id.
std::uint64_t model_token_budget
Program-supplied token ceiling and terminal signal. Zero/null leave generic Core runs unchanged.
Definition run_context.h:83
ToolExecutionIdentity tool_execution_identity
Stable host/Program identity copied to each dispatched tool call.
std::optional< std::chrono::steady_clock::time_point > deadline
Absolute monotonic-clock deadline supplied through RunMetadata, when set.
Definition run_context.h:94
std::shared_ptr< Store > store
Optional cross-thread shared memory.
std::optional< json > resume_value
Value supplied to GraphEngine::resume(), for the one invocation the resume answers,...
StreamMode stream_mode
Mirrors RunConfig::stream_mode.
ToolGate tool_gate
Effective tool policy for this invocation.
std::string resume_interrupt_id
#406: the interrupt resume_value answers, when it answers this invocation's own NodeInterrupt: its Pe...
std::string trace_id
Per-run trace correlator supplied through RunMetadata.
std::shared_ptr< UsageAccumulator > usage
Shared token accounting sink for this run and its subgraphs.
Definition run_context.h:76
The single implementation of "execute an assistant message's tool_calls".
std::function< asio::awaitable< ToolDecision >(ToolCall, ToolGateContext)> ToolGate
Called once per tool call, before any tool runs.