44#include <unordered_map>
45#include <unordered_set>
48#include <nlohmann/json_fwd.hpp>
94 void* user_data =
nullptr;
103 std::vector<Message>
run(std::vector<Message> messages);
139 void set_hooks(
const HookInterface& hooks);
292 const std::vector<Message>& messages)
const;
347 std::unordered_map<std::string, LoopMetrics>
376 std::vector<Message>
run_turn(
const std::string& input);
398 std::vector<Message>
run_turn(std::vector<Message> new_messages);
557 void (*observer)(
const char*,
size_t,
void*),
580 void (*observer)(
int state,
void* user_data),
630 const std::unordered_map<std::string,
631 std::vector<std::string>>& rules);
644 const std::string& tier)
const;
658 const LoopContext& ctx,
const std::string& target)
const;
676 const LoopContext& ctx,
const std::string& target)
const;
770 const std::string& content,
771 const std::string& finish_reason);
781 bool handle_terminal_finish_reasons(
782 LoopContext& ctx,
const std::string& finish_reason);
793 bool record_explicit_completion_failure(
794 LoopContext& ctx,
const std::string& finish_reason);
808 void finalize_delegation_result(
818 void relay_partial_result(
LoopContext& ctx,
const std::string& summary);
828 const std::string& terminal_reason = {});
836 bool should_stop(
const LoopContext& ctx)
const;
844 void set_state(LoopContext& ctx,
AgentState state);
851 void reinject_context_anchors(LoopContext& ctx);
857 void register_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&);
878 std::pair<std::string, std::vector<ToolCall>> parse_tool_calls(
879 const std::string& raw_content);
887 void process_tool_results(LoopContext& ctx,
888 const std::vector<ToolCall>& tool_calls);
897 void execute_pending_delegation(LoopContext& ctx);
912 bool reject_delegation_if_guarded(LoopContext& ctx,
913 const PendingDelegation& pending);
920 void execute_pending_pipeline(LoopContext& ctx);
930 bool should_auto_chain(
const LoopContext& ctx,
931 const std::string& finish_reason,
932 const std::string& content);
942 bool try_auto_chain(LoopContext& ctx,
943 const std::string& finish_reason,
944 const std::string& content);
968 void fire_post_generate_hook(GenerateResult& result,
969 const std::string& tier,
970 const std::vector<Message>& messages);
985 void capture_validation_feedback(LoopContext& ctx);
1007 void dispatch_post_generate(LoopContext& ctx,
1008 GenerateResult& result);
1023 void process_generation_result(LoopContext& ctx,
1024 GenerateResult& result);
1038 bool fire_complete_hook(
const std::string& summary,
1039 const LoopContext& ctx);
1048 bool fire_delegate_pre_hook(
const PendingDelegation& pending,
1062 void fire_delegate_complete_hook(
const std::string& target,
1064 const std::string& summary =
"");
1083 std::filesystem::path get_repo_dir();
1105 SandboxManager* ensure_sandbox_manager();
1124 bool resolve_resume_delegation(
1126 PendingDelegation& pending,
1127 std::vector<Message>& out_history);
1143 bool fetch_resume_payload(
1145 const std::string&
id,
1146 nlohmann::json& parsed);
1164 DelegationResult run_pending_delegation(
1166 const PendingDelegation& pending,
1167 std::vector<Message> resume_history);
1176 void fire_delegation_start(
const LoopContext& ctx,
1177 const std::string& tier,
1178 const std::string& task);
1187 void fire_delegation_complete(
const LoopContext& ctx,
1188 const std::string& tier,
1189 const struct DelegationResult& result);
1192 LoopMetrics last_metrics_;
1193 std::unordered_map<std::string, LoopMetrics>
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;
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_;
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();
1258 void fire_queue_consumed(
const std::string& consumed,
size_t remaining);
1261 std::vector<Message> conversation_;
1262 std::string system_prompt_;
1263 SessionLogger* session_logger_ =
nullptr;
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_;
1276 void wire_internal_tier_resolution();
1289 void apply_identity_overrides(LoopContext& ctx);
1298 int resolve_max_iterations(
const LoopContext& ctx)
const;
1307 int resolve_max_tool_calls(
const LoopContext& ctx)
const;
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);
Core agent execution engine.
void cancel_pause()
Cancel pause and interrupt completely.
bool queue_user_message(const std::string &message)
Append a user message to the mid-gen queue.
void set_validation_provider(char *(*provider)(void *), void *user_data)
Register a JSON provider for the validation block in ON_COMPLETE hook context.
void run_loop(LoopContext &ctx)
Run the engine loop on a pre-built context.
bool is_delegation_cycle(const LoopContext &ctx, const std::string &target) const
Check whether pending delegation would close a cycle.
size_t user_message_queue_depth() const
Snapshot of the queue depth.
void clear_user_message_queue()
Drop all queued user messages.
const std::vector< Message > & get_messages() const
Get conversation messages (read-only).
bool prepare_next_turn(std::vector< Message > &pending)
Pull the next queued user message into pending (gh#40).
void set_callbacks(const EngineCallbacks &callbacks)
Set callback functions for loop events.
void seed_system_prompt(const std::vector< Message > &new_messages)
Prepend the configured system prompt if this turn needs it.
std::vector< Message > run_turn(const std::string &input)
Run a single conversation turn (stateful).
DelegationCallbacks delegation_callbacks_snapshot() const
Atomically snapshot the registered delegation callbacks.
void set_stream_observer(TokenCallback observer, void *user_data)
Set the global stream observer.
LoopMetrics last_loop_metrics() const
Get metrics from the most recent completed run.
const LoopConfig & loop_config() const
Get loop configuration.
void set_project_dir(const std::filesystem::path &project_dir)
Set the configured project directory (gh#31, v2.1.6).
bool is_running() const
Whether a top-level run_turn is currently in progress.
DirectiveProcessor & directive_processor()
Get directive processor for external hook wiring.
void interrupt()
Interrupt the running loop (thread-safe).
void pause()
Pause generation (thread-safe).
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?...
void set_hooks(const HookInterface &hooks)
Set the hook dispatch interface.
void set_tier_resolution(const TierResolutionInterface &tier_res)
Set the tier resolution interface for delegation.
void set_session_logger(SessionLogger *logger)
Set session logger for model transcript logging.
const TierResolutionInterface & tier_resolution() const
Get the tier resolution interface.
void set_storage(const StorageInterface &storage)
Set the storage interface for persistence.
std::unordered_map< std::string, LoopMetrics > per_tier_metrics() const
Per-tier aggregated metrics since engine start.
void clear_conversation()
Clear conversation history.
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.
std::pair< int, int > context_usage(const std::vector< Message > &messages) const
Get context usage for a message list.
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...
void set_system_prompt(const std::string &prompt)
Set the system prompt for conversation state.
void set_relay_single_delegate(const std::string &name)
Mark a tier as relay-on-single-delegate.
void set_state_observer(void(*observer)(int state, void *user_data), void *user_data)
Register a persistent state-transition observer.
size_t message_count() const
Get conversation message count.
static constexpr int MAX_DELEGATION_DEPTH
Max delegation nesting depth (0=root, 1=child, 2=max).
std::vector< Message > run(std::vector< Message > messages)
Run the engine on a set of messages.
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).
ToolExecutorHooks build_directive_hooks()
Build ToolExecutorHooks wired to this engine's DirectiveProcessor.
void set_handoff_rules(const std::unordered_map< std::string, std::vector< std::string > > &rules)
Store handoff rules for tier delegation.
void set_tier_info(const std::string &name, const ChildContextInfo &info)
Store pre-resolved tier context info.
int run_streaming(const std::string &input, TokenCallback on_token, void *user_data, int *cancel_flag)
Run a streaming conversation turn (stateful).
void set_message_queue_capacity(int cap)
Set the runtime capacity of the mid-gen queue.
EngineCallbacks & callbacks()
Get mutable reference to engine callbacks.
void reset_interrupt()
Reset interrupt flag for next run.
void set_external_interrupt(void(*cb)(void *user_data), void *user_data)
Register a callback invoked alongside interrupt().
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 ...
int64_t seconds_since_last_activity() const
gh#35: seconds since the engine last serviced a run().
void set_tool_executor(const ToolExecutionInterface &tool_exec)
Set the tool execution interface.
Processes tool directives via registry-based dispatch.
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.
entropic_hook_point_t
Hook points in the engine lifecycle.
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.
Result of a finalized delegation, delivered to the consumer.
Grouped consumer-registered delegation callbacks (gh#29).
Resolved tier information for building child delegation contexts.
Auto-compaction configuration.
Result returned from a child delegation loop.
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.
Storage interface for conversation persistence.
Tier resolution callbacks for delegation and auto-chain.