Entropic 2.3.8
Local-first agentic inference engine
Loading...
Searching...
No Matches
engine.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
24#pragma once
25
26#include <entropic/entropic.h> // for ent_decision_t and ent_delegation_* (gh#29, v2.1.5)
32#include <entropic/core/sandbox.h> // gh#33 (v2.1.6): engine-owned session sandbox
37
38#include <atomic>
39#include <deque>
40#include <filesystem>
41#include <mutex>
42#include <optional>
43#include <string>
44#include <unordered_map>
45#include <unordered_set>
46#include <vector>
47
48#include <nlohmann/json_fwd.hpp> // gh#32 (v2.1.6) resume payload type
49
50namespace entropic {
51
63public:
66 static constexpr int MAX_DELEGATION_DEPTH = 2;
67
75 AgentEngine(const InferenceInterface& inference,
77 const CompactionConfig& compaction_config);
78
90 ent_decision_t (*start)(const ent_delegation_request_t*, void*)
91 = nullptr;
92 ent_decision_t (*complete)(const ent_delegation_result_t*, void*)
93 = nullptr;
94 void* user_data = nullptr;
95 };
96
103 std::vector<Message> run(std::vector<Message> messages);
104
111
117 void set_tool_executor(const ToolExecutionInterface& tool_exec);
118
124 void set_tier_resolution(const TierResolutionInterface& tier_res);
125
132 void set_storage(const StorageInterface& storage);
133
139 void set_hooks(const HookInterface& hooks);
140
160 void set_project_dir(const std::filesystem::path& project_dir);
161
173 void set_stream_observer(TokenCallback observer, void* user_data);
174
190 void set_validation_provider(char* (*provider)(void*),
191 void* user_data);
192
209 ent_decision_t (*on_start)(const ent_delegation_request_t*, void*),
210 ent_decision_t (*on_complete)(const ent_delegation_result_t*, void*),
211 void* user_data);
212
227
240 void set_external_interrupt(void (*cb)(void* user_data),
241 void* user_data);
242
252 void run_loop(LoopContext& ctx);
253
260
265 void interrupt();
266
271 void reset_interrupt();
272
277 void pause();
278
283 void cancel_pause();
284
291 std::pair<int, int> context_usage(
292 const std::vector<Message>& messages) const;
293
304 EngineCallbacks& callbacks() { return callbacks_; }
305
312 const LoopConfig& loop_config() const { return loop_config_; }
313
320 DirectiveProcessor& directive_processor() { return directive_processor_; }
321
333 LoopMetrics last_loop_metrics() const { return last_metrics_; }
334
347 std::unordered_map<std::string, LoopMetrics>
348 per_tier_metrics() const { return per_tier_metrics_; }
349
350 // ── Conversation state (v2.0.2) ─────────────────────────
351
357 void set_system_prompt(const std::string& prompt);
358
364 void set_session_logger(SessionLogger* logger);
365
376 std::vector<Message> run_turn(const std::string& input);
377
398 std::vector<Message> run_turn(std::vector<Message> new_messages);
399
411 void seed_system_prompt(const std::vector<Message>& new_messages);
412
424 bool prepare_next_turn(std::vector<Message>& pending);
425
439 int run_streaming(const std::string& input,
440 TokenCallback on_token,
441 void* user_data,
442 int* cancel_flag);
443
459 int run_streaming(std::vector<Message> new_messages,
460 TokenCallback on_token,
461 void* user_data,
462 int* cancel_flag);
463
468 void clear_conversation();
469
475 size_t message_count() const;
476
482 const std::vector<Message>& get_messages() const;
483
484 // ── Mid-generation user-message queue (gh#40, v2.1.10) ──
485
498 bool queue_user_message(const std::string& message);
499
505 size_t user_message_queue_depth() const;
506
513
526 void set_message_queue_capacity(int cap);
527
539 bool is_running() const { return running_flag_.load(); }
540
557 void (*observer)(const char*, size_t, void*),
558 void* user_data);
559
580 void (*observer)(int state, void* user_data),
581 void* user_data);
582
583 // ── Directive hooks (v2.0.2) ────────────────────────────
584
595
596 // ── Tier info (v2.0.2) ──────────────────────────────────
597
608 void set_tier_info(const std::string& name,
609 const ChildContextInfo& info);
610
622 void set_relay_single_delegate(const std::string& name);
623
630 const std::unordered_map<std::string,
631 std::vector<std::string>>& rules);
632
644 const std::string& tier) const;
645
658 const LoopContext& ctx, const std::string& target) const;
659
676 const LoopContext& ctx, const std::string& target) const;
677
691 int64_t seconds_since_last_activity() const;
692
720 LoopContext& ctx, const Message& tool_result_msg) const;
721
722private:
734 void init_session_conversation(LoopContext& ctx);
735
746 void accumulate_run_metrics(LoopContext& ctx);
747
753 void loop(LoopContext& ctx);
754
760 void execute_iteration(LoopContext& ctx);
761
769 void evaluate_no_tool_decision(LoopContext& ctx,
770 const std::string& content,
771 const std::string& finish_reason);
772
781 bool handle_terminal_finish_reasons(
782 LoopContext& ctx, const std::string& finish_reason);
783
793 bool record_explicit_completion_failure(
794 LoopContext& ctx, const std::string& finish_reason);
795
808 void finalize_delegation_result(
809 LoopContext& ctx, const struct DelegationResult& result);
810
818 void relay_partial_result(LoopContext& ctx, const std::string& summary);
819
827 void log_relay_status(LoopContext& ctx,
828 const std::string& terminal_reason = {});
829
836 bool should_stop(const LoopContext& ctx) const;
837
844 void set_state(LoopContext& ctx, AgentState state);
845
851 void reinject_context_anchors(LoopContext& ctx);
852
857 void register_directive_handlers();
858
859 // ── Directive handlers ───────────────────────────────
860 void dir_stop(LoopContext&, const Directive&, DirectiveResult&);
861 void dir_tier_change(LoopContext&, const Directive&, DirectiveResult&);
862 void dir_delegate(LoopContext&, const Directive&, DirectiveResult&);
863 void dir_pipeline(LoopContext&, const Directive&, DirectiveResult&);
864 void dir_complete(LoopContext&, const Directive&, DirectiveResult&);
865 void dir_clear_todos(LoopContext&, const Directive&, DirectiveResult&);
866 void dir_inject(LoopContext&, const Directive&, DirectiveResult&);
867 void dir_prune(LoopContext&, const Directive&, DirectiveResult&);
868 void dir_anchor(LoopContext&, const Directive&, DirectiveResult&);
869 void dir_phase(LoopContext&, const Directive&, DirectiveResult&);
870 void dir_notify(LoopContext&, const Directive&, DirectiveResult&);
871
878 std::pair<std::string, std::vector<ToolCall>> parse_tool_calls(
879 const std::string& raw_content);
880
887 void process_tool_results(LoopContext& ctx,
888 const std::vector<ToolCall>& tool_calls);
889
890 // ── Delegation (v1.8.6) ──────────────────────────────
891
897 void execute_pending_delegation(LoopContext& ctx);
898
912 bool reject_delegation_if_guarded(LoopContext& ctx,
913 const PendingDelegation& pending);
914
920 void execute_pending_pipeline(LoopContext& ctx);
921
930 bool should_auto_chain(const LoopContext& ctx,
931 const std::string& finish_reason,
932 const std::string& content);
933
942 bool try_auto_chain(LoopContext& ctx,
943 const std::string& finish_reason,
944 const std::string& content);
945
953 bool fire_pre_hook(entropic_hook_point_t point, int iteration);
954
968 void fire_post_generate_hook(GenerateResult& result,
969 const std::string& tier,
970 const std::vector<Message>& messages);
971
985 void capture_validation_feedback(LoopContext& ctx);
986
1007 void dispatch_post_generate(LoopContext& ctx,
1008 GenerateResult& result);
1009
1023 void process_generation_result(LoopContext& ctx,
1024 GenerateResult& result);
1025
1038 bool fire_complete_hook(const std::string& summary,
1039 const LoopContext& ctx);
1040
1048 bool fire_delegate_pre_hook(const PendingDelegation& pending,
1049 int depth);
1050
1062 void fire_delegate_complete_hook(const std::string& target,
1063 bool success,
1064 const std::string& summary = "");
1065
1083 std::filesystem::path get_repo_dir();
1084
1105 SandboxManager* ensure_sandbox_manager();
1106
1124 bool resolve_resume_delegation(
1125 LoopContext& ctx,
1126 PendingDelegation& pending,
1127 std::vector<Message>& out_history);
1128
1143 bool fetch_resume_payload(
1144 LoopContext& ctx,
1145 const std::string& id,
1146 nlohmann::json& parsed);
1147
1164 DelegationResult run_pending_delegation(
1165 LoopContext& ctx,
1166 const PendingDelegation& pending,
1167 std::vector<Message> resume_history);
1168
1176 void fire_delegation_start(const LoopContext& ctx,
1177 const std::string& tier,
1178 const std::string& task);
1179
1187 void fire_delegation_complete(const LoopContext& ctx,
1188 const std::string& tier,
1189 const struct DelegationResult& result);
1190
1191 // ── Members ──────────────────────────────────────────
1192 LoopMetrics last_metrics_;
1193 std::unordered_map<std::string, LoopMetrics>
1194 per_tier_metrics_;
1195 InferenceInterface inference_;
1196 LoopConfig loop_config_;
1197 EngineCallbacks callbacks_;
1198 std::atomic<bool> interrupt_flag_{false};
1199 std::atomic<bool> pause_flag_{false};
1200 void (*external_interrupt_cb_)(void*) = nullptr;
1201 void* external_interrupt_data_ = nullptr;
1202 // ── Delegation callbacks (gh#29, v2.1.5) ────────────────
1207 DelegationCallbacks delegation_cb_;
1208 mutable std::mutex delegation_cb_mutex_;
1209 char* (*validation_provider_)(void*) = nullptr;
1210 void* validation_provider_data_ = nullptr;
1211 std::unordered_map<std::string, std::string> context_anchors_;
1212 ToolExecutionInterface tool_exec_;
1213 TierResolutionInterface tier_res_;
1214 StorageInterface storage_;
1215 DirectiveProcessor directive_processor_;
1216 TokenCounter token_counter_;
1217 CompactionManager compaction_manager_;
1218 ContextManager context_manager_;
1219 ResponseGenerator response_generator_;
1220 HookInterface hooks_;
1221 std::optional<std::filesystem::path> cached_repo_dir_;
1222 bool repo_dir_checked_ = false;
1223 std::filesystem::path project_dir_override_;
1224 std::optional<SandboxManager> sandbox_mgr_;
1225
1226 // ── Mid-generation user-message queue (gh#40, v2.1.10) ──
1227 mutable std::mutex queue_mutex_;
1228 std::deque<std::string> user_message_queue_;
1229 std::atomic<bool> running_flag_{false};
1232 std::atomic<int64_t> last_activity_epoch_s_{0};
1233 void (*queue_observer_)(const char*, size_t, void*) = nullptr;
1234 void* queue_observer_data_ = nullptr;
1237 void (*state_observer_)(int, void*) = nullptr;
1238 void* state_observer_data_ = nullptr;
1249 std::optional<std::string> pop_queued_user_message();
1250
1258 void fire_queue_consumed(const std::string& consumed, size_t remaining);
1259
1260 // ── Conversation state (v2.0.2) ─────────────────────────
1261 std::vector<Message> conversation_;
1262 std::string system_prompt_;
1263 SessionLogger* session_logger_ = nullptr;
1264
1265 // ── Pre-resolved tier data (v2.0.2) ─────────────────────
1266 std::unordered_map<std::string, ChildContextInfo> tier_info_;
1267 std::unordered_map<std::string, std::vector<std::string>> handoff_rules_;
1269 std::unordered_set<std::string> relay_single_delegate_tiers_;
1270
1276 void wire_internal_tier_resolution();
1277
1289 void apply_identity_overrides(LoopContext& ctx);
1290
1298 int resolve_max_iterations(const LoopContext& ctx) const;
1299
1307 int resolve_max_tool_calls(const LoopContext& ctx) const;
1308
1309 /* ── TierResolutionInterface trampolines (static — accept void* ud) ── */
1318 static ChildContextInfo tri_resolve_tier(
1319 const std::string& name, void* ud);
1328 static bool tri_tier_exists(
1329 const std::string& name, void* ud);
1338 static std::vector<std::string> tri_get_handoff_targets(
1339 const std::string& name, void* ud);
1349 static std::string tri_get_tier_param(
1350 const std::string& name, const std::string& param, void* ud);
1351};
1352
1353} // namespace entropic
Core agent execution engine.
Definition engine.h:62
void cancel_pause()
Cancel pause and interrupt completely.
Definition engine.cpp:862
bool queue_user_message(const std::string &message)
Append a user message to the mid-gen queue.
Definition engine.cpp:2818
void set_validation_provider(char *(*provider)(void *), void *user_data)
Register a JSON provider for the validation block in ON_COMPLETE hook context.
Definition engine.cpp:261
void run_loop(LoopContext &ctx)
Run the engine loop on a pre-built context.
Definition engine.cpp:342
bool is_delegation_cycle(const LoopContext &ctx, const std::string &target) const
Check whether pending delegation would close a cycle.
Definition engine.cpp:684
size_t user_message_queue_depth() const
Snapshot of the queue depth.
Definition engine.cpp:2837
void clear_user_message_queue()
Drop all queued user messages.
Definition engine.cpp:2847
const std::vector< Message > & get_messages() const
Get conversation messages (read-only).
Definition engine.cpp:2807
bool prepare_next_turn(std::vector< Message > &pending)
Pull the next queued user message into pending (gh#40).
Definition engine.cpp:2603
void set_callbacks(const EngineCallbacks &callbacks)
Set callback functions for loop events.
Definition engine.cpp:158
void seed_system_prompt(const std::vector< Message > &new_messages)
Prepend the configured system prompt if this turn needs it.
Definition engine.cpp:2581
std::vector< Message > run_turn(const std::string &input)
Run a single conversation turn (stateful).
Definition engine.cpp:2523
DelegationCallbacks delegation_callbacks_snapshot() const
Atomically snapshot the registered delegation callbacks.
Definition engine.cpp:249
void set_stream_observer(TokenCallback observer, void *user_data)
Set the global stream observer.
Definition engine.cpp:219
LoopMetrics last_loop_metrics() const
Get metrics from the most recent completed run.
Definition engine.h:333
const LoopConfig & loop_config() const
Get loop configuration.
Definition engine.h:312
void set_project_dir(const std::filesystem::path &project_dir)
Set the configured project directory (gh#31, v2.1.6).
Definition engine.cpp:2329
bool is_running() const
Whether a top-level run_turn is currently in progress.
Definition engine.h:539
DirectiveProcessor & directive_processor()
Get directive processor for external hook wiring.
Definition engine.h:320
void interrupt()
Interrupt the running loop (thread-safe).
Definition engine.cpp:808
void pause()
Pause generation (thread-safe).
Definition engine.cpp:852
bool is_delegation_repeat_blocked(const LoopContext &ctx, const std::string &target) const
Predicate: should this delegation be blocked because the same target has just failed too many times?...
Definition engine.cpp:698
void set_hooks(const HookInterface &hooks)
Set the hook dispatch interface.
Definition engine.cpp:201
void set_tier_resolution(const TierResolutionInterface &tier_res)
Set the tier resolution interface for delegation.
Definition engine.cpp:179
void set_session_logger(SessionLogger *logger)
Set session logger for model transcript logging.
Definition engine.cpp:2507
const TierResolutionInterface & tier_resolution() const
Get the tier resolution interface.
Definition engine.cpp:273
void set_storage(const StorageInterface &storage)
Set the storage interface for persistence.
Definition engine.cpp:190
std::unordered_map< std::string, LoopMetrics > per_tier_metrics() const
Per-tier aggregated metrics since engine start.
Definition engine.h:348
void clear_conversation()
Clear conversation history.
Definition engine.cpp:2786
void set_queue_observer(void(*observer)(const char *, size_t, void *), void *user_data)
Register an observer that fires when a queued user message is consumed and seeded as the next turn.
Definition engine.cpp:2900
std::pair< int, int > context_usage(const std::vector< Message > &messages) const
Get context usage for a message list.
Definition engine.cpp:875
bool fold_complete_into_assistant(LoopContext &ctx, const Message &tool_result_msg) const
gh#68 (v2.3.4): fold entropic.complete summary into the prior assistant message, suppressing the JSON...
Definition engine.cpp:710
void set_system_prompt(const std::string &prompt)
Set the system prompt for conversation state.
Definition engine.cpp:2497
void set_relay_single_delegate(const std::string &name)
Mark a tier as relay-on-single-delegate.
Definition engine.cpp:2969
void set_state_observer(void(*observer)(int state, void *user_data), void *user_data)
Register a persistent state-transition observer.
Definition engine.cpp:2918
size_t message_count() const
Get conversation message count.
Definition engine.cpp:2797
static constexpr int MAX_DELEGATION_DEPTH
Max delegation nesting depth (0=root, 1=child, 2=max).
Definition engine.h:66
std::vector< Message > run(std::vector< Message > messages)
Run the engine on a set of messages.
Definition engine.cpp:370
void set_delegation_callbacks(ent_decision_t(*on_start)(const ent_delegation_request_t *, void *), ent_decision_t(*on_complete)(const ent_delegation_result_t *, void *), void *user_data)
Register delegation start/complete callbacks (gh#29, v2.1.5).
Definition engine.cpp:232
ToolExecutorHooks build_directive_hooks()
Build ToolExecutorHooks wired to this engine's DirectiveProcessor.
Definition engine.cpp:2934
void set_handoff_rules(const std::unordered_map< std::string, std::vector< std::string > > &rules)
Store handoff rules for tier delegation.
Definition engine.cpp:2979
void set_tier_info(const std::string &name, const ChildContextInfo &info)
Store pre-resolved tier context info.
Definition engine.cpp:2956
int run_streaming(const std::string &input, TokenCallback on_token, void *user_data, int *cancel_flag)
Run a streaming conversation turn (stateful).
Definition engine.cpp:2656
void set_message_queue_capacity(int cap)
Set the runtime capacity of the mid-gen queue.
Definition engine.cpp:2861
EngineCallbacks & callbacks()
Get mutable reference to engine callbacks.
Definition engine.h:304
void reset_interrupt()
Reset interrupt flag for next run.
Definition engine.cpp:843
void set_external_interrupt(void(*cb)(void *user_data), void *user_data)
Register a callback invoked alongside interrupt().
Definition engine.cpp:832
bool tier_requires_explicit_completion(const std::string &tier) const
Check if a tier requires an explicit entropic.complete / entropic.delegate tool call to conclude its ...
Definition engine.cpp:663
int64_t seconds_since_last_activity() const
gh#35: seconds since the engine last serviced a run().
Definition engine.cpp:730
void set_tool_executor(const ToolExecutionInterface &tool_exec)
Set the tool execution interface.
Definition engine.cpp:168
Processes tool directives via registry-based dispatch.
Definition directives.h:312
Manages session_model.log for raw streaming content.
Auto-compaction for context management.
Context management subsystem for the agentic loop.
Directive processing for tool-to-engine communication.
Types for the agentic loop engine.
Public C API for the Entropic inference engine.
ent_decision_t
Consumer decision returned from delegation callbacks.
Definition entropic.h:870
entropic_hook_point_t
Hook points in the engine lifecycle.
Definition hooks.h:34
Hook dispatch interface injected into engine subsystems.
Function pointer types for core-to-inference communication.
Activate model on GPU (WARM → ACTIVE).
void(*)(const char *, size_t, void *) TokenCallback
Token callback type matching the C API signature.
AgentState
C++ enum class for agent execution states.
Response generation subsystem for the agentic loop.
Filesystem-based sandbox isolation for delegations.
Session model log — raw streaming transcript.
Streaming filter that strips <think>...</think> blocks.
Request describing a delegation that is about to run.
Definition entropic.h:884
Result of a finalized delegation, delivered to the consumer.
Definition entropic.h:907
Grouped consumer-registered delegation callbacks (gh#29).
Definition engine.h:89
Resolved tier information for building child delegation contexts.
Auto-compaction configuration.
Definition config.h:508
Result returned from a child delegation loop.
Definition delegation.h:33
Callback function pointer types for engine events.
Configuration for the agentic loop.
Mutable state carried through the agentic loop.
Metrics collected during loop execution.
A message in a conversation.
Definition message.h:35
Storage interface for conversation persistence.
Tier resolution callbacks for delegation and auto-chain.
Tool execution interface for the engine.
Engine-level hooks called during tool processing.