NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
harness.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <neograph/api.h>
9#include <neograph/json.h>
10#include <neograph/mcp/harness_program_translator.h>
16#include <neograph/types.h>
17#include <neograph/tool_set.h>
18
19#include <atomic>
20#include <chrono>
21#include <cstddef>
22#include <functional>
23#include <cstdint>
24#include <limits>
25#include <memory>
26#include <map>
27#include <optional>
28#include <string>
29#include <utility>
30#include <vector>
31
32namespace sp { class NativeArchive; }
33
34namespace neograph {
35class Provider;
36namespace program {
37class ProgramRuntime;
38}
39}
40
41namespace neograph::mcp {
42
43class MCPServer;
44
45enum class HarnessWorkerResponseKind {
46 VALUE,
47 EMPTY,
48 PARSE_ERROR,
49 TOOL_ERROR,
50 TIMEOUT,
51 CANCELLED,
52 AWAITING_TOOL_RESULTS,
53 INPUT_REQUIRED,
54};
55
58 json task;
59 json worker;
60 json tool_catalog;
61 json policy;
62 std::size_t attempt = 1;
63 std::string repair_feedback;
64 std::optional<json> resume_value;
67 std::string run_id;
68 std::shared_ptr<UsageAccumulator> usage;
69 std::uint64_t model_token_budget = 0;
70 std::shared_ptr<std::atomic_bool> budget_exhausted;
71};
72
74struct NEOGRAPH_HARNESS_API HarnessWorkerResponse {
75 HarnessWorkerResponseKind kind = HarnessWorkerResponseKind::VALUE;
76 json value;
77 std::string message;
79 bool provider_effect_uncertain = false;
80
81 static HarnessWorkerResponse success(json value);
82 static HarnessWorkerResponse empty(std::string message = {});
83 static HarnessWorkerResponse parse_error(std::string message);
84 static HarnessWorkerResponse tool_error(std::string message);
85 static HarnessWorkerResponse timeout(std::string message = {});
86 static HarnessWorkerResponse cancelled(std::string message = {});
87 static HarnessWorkerResponse awaiting_tool_results(json pending);
88 static HarnessWorkerResponse input_required(json pending);
89};
90
91using HarnessWorkerExecutor = std::function<HarnessWorkerResponse(
92 const HarnessWorkerCall&, const std::shared_ptr<graph::CancelToken>&)>;
93
94using HarnessCapabilityExecutor = std::function<json(const json& tool_definition,
95 const json& arguments,
96 const std::shared_ptr<graph::CancelToken>&)>;
97
98struct HarnessProviderExecutorConfig {
99 std::shared_ptr<Provider> provider;
100 std::string model;
101 HarnessCapabilityExecutor capability_executor;
102 std::size_t max_tool_rounds = 8;
104 std::shared_ptr<sp::NativeArchive> native_archive;
105};
106
108NEOGRAPH_HARNESS_API HarnessWorkerExecutor
109make_provider_harness_executor(HarnessProviderExecutorConfig config);
110
112class NEOGRAPH_HARNESS_API HarnessJournal {
113public:
114 virtual ~HarnessJournal() = default;
115
116 virtual void append_event(const json& event) = 0;
117 virtual std::vector<json> list_events(const std::string& run_id,
118 std::size_t after_sequence = 0,
119 std::size_t limit = 1000) = 0;
120};
121
123class NEOGRAPH_HARNESS_API HarnessRecordStore {
124public:
125 virtual ~HarnessRecordStore() = default;
126
127 virtual void save_artifact(const std::string& artifact_id, const json& record) = 0;
128 virtual std::optional<json> load_artifact(const std::string& artifact_id) = 0;
129 virtual void save_run(const std::string& run_id, const json& record) = 0;
130 virtual std::optional<json> load_run(const std::string& run_id) = 0;
131};
132
135 std::size_t max_artifacts = std::numeric_limits<std::size_t>::max();
136 std::size_t max_runs = std::numeric_limits<std::size_t>::max();
137 std::vector<std::string> protected_artifact_ids;
138 std::vector<std::string> protected_run_ids;
141 std::string namespace_prefix;
142};
143
146 std::vector<std::string> artifact_ids;
147 std::vector<std::string> run_ids;
148};
149
151class NEOGRAPH_HARNESS_API HarnessRetentionStore {
152public:
153 virtual ~HarnessRetentionStore() = default;
156};
157
159class NEOGRAPH_HARNESS_API FileHarnessRecordStore final : public HarnessRecordStore {
160public:
161 explicit FileHarnessRecordStore(std::string root_directory);
162 ~FileHarnessRecordStore() override;
163
164 void save_artifact(const std::string& artifact_id, const json& record) override;
165 std::optional<json> load_artifact(const std::string& artifact_id) override;
166 void save_run(const std::string& run_id, const json& record) override;
167 std::optional<json> load_run(const std::string& run_id) override;
168
169private:
170 struct Impl;
171 std::unique_ptr<Impl> impl_;
172};
173
175
176struct HarnessProgramHostConfig {
177 HarnessWorkerExecutor worker_executor;
178 HarnessCapabilityExecutor capability_executor;
179 HarnessProgramSnapshotConfig snapshots;
180 std::shared_ptr<graph::CheckpointStore> checkpoints;
181 std::shared_ptr<graph::Store> state_store;
182 std::shared_ptr<program::EngineGenerationCache> engines;
183 std::string compiler_build_id;
184 std::string provider_binding_identity;
186 json provider_host_configuration = json::object();
187 std::map<std::string, std::string> tool_binding_identities;
188 std::size_t scheduler_threads = 1;
189};
190
191NEOGRAPH_HARNESS_API HarnessServiceResources
192make_harness_program_service_resources(HarnessProgramHostConfig config);
193
194using HarnessProgramCatalogFactory = std::function<std::shared_ptr<program::ProgramCatalog>(
195 std::shared_ptr<program::ProgramStore>,
196 const HarnessCapabilityBindingRequest&)>;
197
198using HarnessProgramRuntimeFactory = std::function<std::shared_ptr<program::ProgramRuntime>(
199 std::shared_ptr<program::ProgramCatalog>,
200 std::shared_ptr<program::ProgramTransitionStore>)>;
201using HarnessRecordedBindingFactory = std::function<program::RecordedBindingSet(
202 const program::ProgramVersion&,
203 const HarnessCapabilityBindingRequest&,
204 const std::vector<program::ProgramEvent>&)>;
205
213 HarnessProgramSnapshots snapshots;
214 std::shared_ptr<program::ProgramCompiler> compiler;
215 HarnessProgramCatalogFactory make_program_catalog;
216 HarnessProgramRuntimeFactory make_program_runtime;
217 HarnessRecordedBindingFactory make_recorded_binding;
218 std::string owner_scope;
221};
222
223struct HarnessServiceConfig {
224 std::shared_ptr<HarnessRecordStore> record_store;
225 HarnessTranslationDefaults translation_defaults;
226 std::size_t max_artifacts = 128;
227 std::size_t max_runs = 128;
228 std::chrono::milliseconds poll_interval{1000};
229 std::chrono::milliseconds run_ttl{std::chrono::hours(24)};
232 bool enable_experimental_tasks = false;
233};
234
242class NEOGRAPH_HARNESS_API HarnessService {
243public:
244 HarnessService(HarnessServiceConfig config,
245 std::shared_ptr<HarnessJournal> journal,
246 HarnessServiceResources resources);
248
249 HarnessService(const HarnessService&) = delete;
250 HarnessService& operator=(const HarnessService&) = delete;
251
254
256 json schema() const;
257
259 json compile(const json& request);
260
262 json start(const json& arguments);
263
265 json resume(const json& arguments);
266
268 json get(const std::string& run_id, const std::string& view = "status") const;
269
271 json get(const std::string& run_id,
272 const std::string& view,
273 std::size_t after_sequence,
274 std::size_t limit) const;
275
277 json read(const std::string& uri) const;
278
280 bool cancel(const std::string& run_id);
281
282private:
283 struct Impl;
284 std::shared_ptr<Impl> impl_;
285};
286
287} // namespace neograph::mcp
NEOGRAPH_API export/import macro for shared-library builds.
Cooperative cancellation primitive for graph runs.
Admission and process-local materialization of immutable Programs.
Atomic JSON-file implementation suitable for local process restarts.
Definition harness.h:159
Append-only causal event boundary, separate from mutable run snapshots.
Definition harness.h:112
Durable storage boundary for retained Harness artifacts and run records.
Definition harness.h:123
Optional sibling boundary; HarnessRecordStore's stable vtable remains unchanged.
Definition harness.h:151
virtual HarnessRetentionResult cleanup_retained(const HarnessRetentionPolicy &policy)=0
Namespace filtering, reference checks and deletion must share one transaction.
Strict MCP adapter over ProgramCompiler -> ProgramCatalog -> ProgramRuntime.
Definition harness.h:242
json resume(const json &arguments)
Resume the exact pending call with a schema-validated host result.
json get(const std::string &run_id, const std::string &view="status") const
Return a compact snapshot for a run.
json start(const json &arguments)
Start from {artifact_id} or compile-and-start from {request}.
bool cancel(const std::string &run_id)
Request cooperative cancellation. Returns false for unknown/terminal runs.
json schema() const
Build-specific schemas, node palette, presets, and capabilities.
json get(const std::string &run_id, const std::string &view, std::size_t after_sequence, std::size_t limit) const
Return a paginated debugger view without changing the compact default.
void register_tools(MCPServer &server)
Register neograph_schema/compile/start/get/resume/cancel on a server.
json compile(const json &request)
Compile and retain an immutable artifact when the request is valid.
json read(const std::string &uri) const
Dereference a neograph://runs/<run_id>/<view> result URI.
Local, stdio-first MCP server with typed tools and bounded execution.
Definition server.h:53
Move-only captured evidence proposal.
Definition replay.h:84
Cross-thread shared memory store (LangGraph Store equivalent).
NEOGRAPH_HARNESS_API HarnessWorkerExecutor make_provider_harness_executor(HarnessProviderExecutorConfig config)
Build a worker executor that calls a NeoGraph Provider directly.
Thin C++ RAII wrapper around yyjson with nlohmann-compatible API.
Pure versioned Program source compiler.
Atomic storage of admitted Program bundles and versions.
Execution service for admitted immutable Programs.
Count-bounded cleanup policy for stores that support durable retention.
Definition harness.h:134
std::string namespace_prefix
Restrict both counts and deletion candidates to this exact byte prefix.
Definition harness.h:141
Records removed by one atomic record-store cleanup pass.
Definition harness.h:145
Immutable host-owned Program control/data-plane resources.
Definition harness.h:212
std::string artifact_binding_identity
Host configuration identity that makes an artifact safe to reconnect.
Definition harness.h:220
Input handed to the embedding's provider/tool execution backend.
Definition harness.h:57
std::string run_id
Program-scoped accounting and cancellation budget supplied by the host node.
Definition harness.h:67
Typed worker response; failure classes never masquerade as valid output.
Definition harness.h:74