NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
compiler.h
Go to the documentation of this file.
1
15#pragma once
16
17#include <neograph/api.h>
21
22#include <map>
23#include <memory>
24#include <optional>
25#include <set>
26#include <string>
27#include <vector>
28
29namespace neograph::graph {
30
32inline constexpr int TOPOLOGY_SCHEMA_VERSION = 1;
33
34class GraphRegistry;
41struct NEOGRAPH_API CompilerDiagnostic {
42 std::string code;
43 std::string json_pointer;
44 std::string message;
45 json witness;
46};
47
48class GraphNode;
49
56struct ChannelDef {
57 std::string name;
58 ReducerType type = ReducerType::OVERWRITE;
59 std::string reducer_name = "overwrite";
60 json initial_value;
65 bool has_initial = false;
67 ChannelRetentionPolicy retention = ChannelRetentionPolicy::Unbounded;
68 std::uint64_t retention_limit = 0;
70 ChannelPersistencePolicy persistence = ChannelPersistencePolicy::Checkpoint;
71};
72
74inline constexpr int kMinAttemptTimeoutMs = 100;
75inline constexpr int kMaxAttemptTimeoutMs = 3'600'000;
76
84 std::optional<int> max_retries;
85 std::optional<int> initial_delay_ms;
86 std::optional<double> backoff_multiplier;
87 std::optional<int> max_delay_ms;
88 std::vector<std::string> non_retryable;
89 std::optional<int> max_visits;
90 std::optional<bool> error_text;
94 std::optional<int> attempt_timeout_ms;
95
96 bool empty() const noexcept {
98 non_retryable.empty() && !max_visits && !error_text && !attempt_timeout_ms;
99 }
100 bool operator==(const NodeRetryPolicySpec&) const = default;
101};
102
105 std::string from;
106 std::string to;
108 std::optional<std::string> error_channel;
109
110 bool operator==(const FailureEdge&) const = default;
111};
112
116 std::string code;
117
118 bool operator==(const FailureExit&) const = default;
119};
120
128struct NEOGRAPH_API TopologySpec {
129 std::string name;
130 std::vector<ChannelDef> channel_defs;
131 std::vector<Edge> edges;
132 std::vector<ConditionalEdge> conditional_edges;
133 BarrierSpecs barrier_specs;
136 std::set<std::string> interrupt_before;
137 std::set<std::string> interrupt_after;
138 std::optional<RetryPolicy> retry_policy;
139 int schema_version = 0;
140 std::map<std::string, json> node_defs;
145 std::map<std::string, NodeRetryPolicySpec> node_retry_policies;
147 std::vector<FailureEdge> failure_edges;
148 std::map<std::string, FailureExit> failure_exits;
149
150 json to_json() const;
151};
157struct NEOGRAPH_API ParseReport {
158 std::optional<TopologySpec> topology;
159 std::vector<CompilerDiagnostic> diagnostics;
160
161 bool has_errors() const noexcept;
162 std::string summary() const;
163};
164
166struct NEOGRAPH_API RoundTripReport {
167 std::vector<CompilerDiagnostic> diagnostics;
168
169 bool has_errors() const noexcept;
170 std::string summary() const;
171};
172
186 std::string name;
187 std::vector<ChannelDef> channel_defs;
190 std::map<std::string, std::unique_ptr<GraphNode>> nodes;
191 std::vector<Edge> edges;
192 std::vector<ConditionalEdge> conditional_edges;
193 BarrierSpecs barrier_specs;
194 JoinSpecs join_specs;
195 std::set<std::string> interrupt_before;
196 std::set<std::string> interrupt_after;
199 std::optional<RetryPolicy> retry_policy;
200
207
210
212 std::map<std::string, NodeRetryPolicySpec> node_retry_policies;
213 std::vector<FailureEdge> failure_edges;
214 std::map<std::string, FailureExit> failure_exits;
215
220 std::map<std::string, json> node_defs;
221
238 NEOGRAPH_API json to_json() const;
239
241 NEOGRAPH_API TopologySpec topology() const;
242};
243
250class NEOGRAPH_API GraphCompiler {
251public:
255 static TopologySpec parse(const json& definition);
256
258 static TopologySpec parse(const json& definition, const GraphRegistry& registry);
259
261 static TopologySpec parse_local(const json& definition, const GraphRegistry& registry);
263 static ParseReport parse_report(const json& definition, const GraphRegistry& registry);
264
266 static ParseReport parse_local_report(const json& definition, const GraphRegistry& registry);
267
271 static CompiledGraph link(TopologySpec topology, const NodeContext& default_context);
272
275 const NodeContext& default_context,
276 const GraphRegistry& registry);
277
280 const NodeContext& default_context,
281 const GraphRegistry& registry);
282
324 static CompiledGraph compile(const json& definition, const NodeContext& default_context);
325
334 static CompiledGraph compile(const json& definition,
335 const NodeContext& default_context,
336 const GraphRegistry& registry);
337
339 static CompiledGraph compile_local(const json& definition,
340 const NodeContext& default_context,
341 const GraphRegistry& registry);
342
370 static json canon(const json& definition);
371
393 static json upgrade_to_latest(const json& definition);
394
404 static void verify_roundtrip(const json& definition, const CompiledGraph& cg);
405
407 static void verify_roundtrip(const json& definition, const TopologySpec& topology);
408
410 static RoundTripReport verify_roundtrip_report(const json& definition,
411 const TopologySpec& topology);
412};
413
414} // namespace neograph::graph
NEOGRAPH_API export/import macro for shared-library builds.
Copyable owner of a fixed collection of tools.
Definition tool_set.h:21
Stateless JSON parser and node-instantiation linker.
Definition compiler.h:250
static RoundTripReport verify_roundtrip_report(const json &definition, const TopologySpec &topology)
Total translation-validation before runtime node instantiation.
static void verify_roundtrip(const json &definition, const TopologySpec &topology)
Translation validation before runtime node instantiation.
static TopologySpec parse(const json &definition, const GraphRegistry &registry)
Pure parse using a local-first registry for schema metadata.
static CompiledGraph compile(const json &definition, const NodeContext &default_context)
Parse a JSON graph definition into a CompiledGraph.
static json canon(const json &definition)
Canonicalize a topology JSON document for equivalence comparison.
static CompiledGraph link(TopologySpec topology, const NodeContext &default_context, const GraphRegistry &registry)
Instantiate nodes using a local-first registry overlay.
static CompiledGraph link(TopologySpec topology, const NodeContext &default_context)
Instantiate runtime nodes from an already parsed topology.
static CompiledGraph compile(const json &definition, const NodeContext &default_context, const GraphRegistry &registry)
Compile with a local-first registry overlay.
static json upgrade_to_latest(const json &definition)
Upgrade a legacy (schema_version 0 / absent) topology document to the current schema version.
static ParseReport parse_local_report(const json &definition, const GraphRegistry &registry)
Total pure parse requiring every executable in the local registry.
static TopologySpec parse_local(const json &definition, const GraphRegistry &registry)
Pure parse that rejects every executable absent from the registry instance.
static CompiledGraph link_local(TopologySpec topology, const NodeContext &default_context, const GraphRegistry &registry)
Instantiate nodes exclusively through the registry instance.
static TopologySpec parse(const json &definition)
Parse JSON into a declarative topology without creating nodes.
static ParseReport parse_report(const json &definition, const GraphRegistry &registry)
Total pure parse using local-first registry metadata.
static void verify_roundtrip(const json &definition, const CompiledGraph &cg)
Translation validation: assert compile() lost nothing.
static CompiledGraph compile_local(const json &definition, const NodeContext &default_context, const GraphRegistry &registry)
Parse and link without process-global registry fallback.
Abstract base class for all graph nodes.
Definition node.h:96
Engine-owned registry of custom graph callables.
Definition registry.h:21
constexpr int kMinAttemptTimeoutMs
#364 bounds of NodeRetryPolicySpec::attempt_timeout_ms (milliseconds).
Definition compiler.h:74
constexpr int TOPOLOGY_SCHEMA_VERSION
Latest topology schema accepted by GraphCompiler.
Definition compiler.h:32
Graph engine type definitions: channels, edges, nodes, events, and control flow.
ChannelRetentionPolicy
Retention policy applied after a channel reducer produces a value.
Definition types.h:74
ReducerType
Strategy for merging values when writing to a channel.
Definition types.h:61
ChannelPersistencePolicy
Whether a channel value is part of serialized checkpoints.
Definition types.h:83
Legacy process-global registries for graph executables.
Pure routing / next-step-planning logic extracted from GraphEngine.
std::map< std::string, JoinSpec > JoinSpecs
Joins with at least one multi-alternative slot, by join node name.
Definition scheduler.h:102
std::map< std::string, std::set< std::string > > BarrierSpecs
Static barrier declarations extracted at graph-compile time.
Definition scheduler.h:82
Declarative configuration for a single state channel.
Definition compiler.h:56
ChannelPersistencePolicy persistence
Ephemeral values are omitted; checkpoint metadata guards resume.
Definition compiler.h:70
ChannelRetentionPolicy retention
Retention is applied after reducer combination.
Definition compiler.h:67
bool has_initial
True iff the JSON channel definition carried an explicit "initial" key.
Definition compiler.h:65
Parsed graph definition, ready for the engine to run.
Definition compiler.h:185
int schema_version
Declared "schema_version" from the topology JSON, or 0 when the document did not carry one.
Definition compiler.h:206
NEOGRAPH_API TopologySpec topology() const
Copy the declarative portion without live GraphNode instances.
NEOGRAPH_API json to_json() const
Re-emit this compiled graph as topology JSON.
std::map< std::string, NodeRetryPolicySpec > node_retry_policies
#364 failure routing keys, carried so to_json() round-trips.
Definition compiler.h:212
std::optional< RetryPolicy > retry_policy
Present iff the JSON defined a top-level "retry_policy" object.
Definition compiler.h:199
std::map< std::string, json > node_defs
Verbatim copy of each node's JSON definition (config + "type"), minus the "barrier" key (barriers are...
Definition compiler.h:220
ToolSet tools
Retains tools referenced by the compiled node instances until link().
Definition compiler.h:189
json metadata
Graph metadata (#362), carried so to_json() round-trips; null when absent.
Definition compiler.h:209
One stable, machine-readable compiler finding.
Definition compiler.h:41
#364: where a node's routable failure goes instead of failing the run.
Definition compiler.h:104
std::optional< std::string > error_channel
A channel with the built-in append reducer that receives the failure record.
Definition compiler.h:108
#364: a node whose completion ends the run as failed.
Definition compiler.h:114
std::string code
An author code; empty means the default, NG.Aborted.
Definition compiler.h:116
Dependency injection context passed to nodes during construction.
Definition types.h:753
#364: one node's retry fields exactly as the author wrote them.
Definition compiler.h:83
std::vector< std::string > non_retryable
Sorted, unique, at most 16 codes.
Definition compiler.h:88
std::optional< int > attempt_timeout_ms
Per-attempt time limit (milliseconds), kMinAttemptTimeoutMs to kMaxAttemptTimeoutMs.
Definition compiler.h:94
std::optional< int > max_retries
0-10
Definition compiler.h:84
std::optional< double > backoff_multiplier
1.0-10.0
Definition compiler.h:86
std::optional< int > max_visits
1-100
Definition compiler.h:89
std::optional< int > max_delay_ms
initial_delay_ms-300000
Definition compiler.h:87
std::optional< int > initial_delay_ms
10-60000
Definition compiler.h:85
Total result of parsing a topology document.
Definition compiler.h:157
Total result of translation-validation.
Definition compiler.h:166
Declarative graph topology with no live node instances or runtime state.
Definition compiler.h:128
std::vector< FailureEdge > failure_edges
Sorted by the UTF-8 bytes of from; at most one per node.
Definition compiler.h:147
json metadata
Host-readable author claims (#362): a nonempty object, or null when the definition has none.
Definition compiler.h:143
std::map< std::string, NodeRetryPolicySpec > node_retry_policies
#364 failure routing, read under schema_version >= 1. Absent when unused.
Definition compiler.h:145
JoinSpecs join_specs
Joins with any_of slots (#363); a legacy-equivalent spec stays in barrier_specs.
Definition compiler.h:135