Entropic 2.14.3
Local-first agentic inference engine
Loading...
Searching...
No Matches
tool_executor.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
13#pragma once
14
21
22#include <optional>
23#include <string>
24#include <unordered_map>
25#include <vector>
26
27namespace entropic {
28
29/* ToolExecutorHooks moved to engine_types.h — v2.0.2 */
30
45
56public:
65 ToolExecutor(ServerManager& server_manager,
66 const LoopConfig& loop_config,
67 EngineCallbacks& callbacks,
68 ToolExecutorHooks hooks = {});
69
76
91 ServerManager* (*fn)(const std::string& session_key, void* user_data),
92 void* user_data);
93
110 const std::unordered_map<std::string,
111 std::vector<std::string>>* map) {
112 tier_allowed_tools_ = map;
113 }
114
122 std::vector<Message> process_tool_calls(
124 const std::vector<ToolCall>& tool_calls);
125
126private:
133 static std::vector<ToolCall> sort_tool_calls(
134 const std::vector<ToolCall>& calls);
135
142 void extract_and_process_directives(
143 LoopContext& ctx, const std::string& raw_result);
144
152 std::string check_duplicate(const LoopContext& ctx,
153 const ToolCall& call) const;
154
163 Message handle_duplicate(LoopContext& ctx,
164 const ToolCall& call,
165 const std::string& previous_result);
166
173 bool check_approval(const ToolCall& call);
174
182 std::optional<Message> check_tier_allowed(
183 const LoopContext& ctx, const ToolCall& call) const;
184
192 std::pair<Message, std::string> execute_tool(
193 LoopContext& ctx, const ToolCall& call);
194
208 void record_tool_history(const ToolCall& call,
209 const std::string& args_json,
210 const std::string& result_text,
211 long long ms, int iteration);
212
229 void finalize_tool_call(LoopContext& ctx, const ToolCall& call,
230 Message& msg, const std::string& raw_result,
231 double exec_ms);
232
243 void log_tool_call(LoopContext& ctx, const ToolCall& call,
244 double exec_ms, const std::string& raw_result,
245 ToolResultKind kind);
246
253 static std::string tool_call_key(const ToolCall& call);
254
262 void record_tool_call(LoopContext& ctx,
263 const ToolCall& call,
264 const std::string& result);
265
273 static Message create_denied_message(const ToolCall& call,
274 const std::string& reason);
275
283 static Message create_error_message(const ToolCall& call,
284 const std::string& error);
285
292 void fire_state_callback(const LoopContext& ctx);
293
301 void truncate_to_limit(std::vector<ToolCall>& calls, int limit) const;
302
311 PreconditionCheck check_call_preconditions(
312 LoopContext& ctx, const ToolCall& call);
313
322 PreconditionCheck check_approval_pc(
323 LoopContext& ctx, const ToolCall& call);
324
331 std::optional<Message> check_schema(const ToolCall& call);
332
341 std::optional<Message> check_dup_or_approval(
342 LoopContext& ctx, const ToolCall& call);
343
357 std::vector<Message> process_single_call(
359
368 bool should_stop_batch(const LoopContext& ctx,
369 const std::vector<Message>& results) const;
370
377 void run_post_tool_hooks(LoopContext& ctx);
378
385 static Message create_circuit_breaker_message();
386
395 static Message create_duplicate_message(
396 const ToolCall& call,
397 const std::string& previous_result);
398
406 static std::string serialize_args(const ToolCall& call);
407
415 static std::string serialize_tool_call(const ToolCall& call);
416
430 static std::string build_post_tool_json(
431 const ToolCall& call,
432 const std::string& raw_result,
433 double elapsed_ms,
434 const std::string& tier,
435 int iteration,
436 ToolResultKind kind);
437
457 void update_anti_spiral_tracking(LoopContext& ctx,
458 const std::string& tool_name);
459
473 int effective_hard_block_threshold() const;
474
492 PreconditionCheck check_anti_spiral_hard_block(
493 const LoopContext& ctx, const ToolCall& call) const;
494
519 PreconditionCheck check_repeated_failure(
520 const LoopContext& ctx, const ToolCall& call) const;
521
535 PreconditionCheck check_spiral_blocks(
536 const LoopContext& ctx, const ToolCall& call) const;
537
555 void apply_result_size_cap(std::string& content) const;
556
566 static std::string build_pre_tool_json(
567 const ToolCall& call,
568 const std::string& tier,
569 int iteration);
570
598 void fire_post_tool_hook(const LoopContext& ctx,
599 const ToolCall& call,
600 const std::string& raw_result,
601 double elapsed_ms,
602 ToolResultKind kind,
603 Message& msg);
604
622 bool fire_pre_tool_hook(const LoopContext& ctx, ToolCall& call);
623
657 static bool apply_pre_tool_modification(ToolCall& call,
658 const char* modified);
659
668 void fire_tool_complete_callback(const ToolCall& call,
669 const std::string& result,
670 long long ms);
671
672 ServerManager& server_manager_;
674 ServerManager* (*server_resolver_)(const std::string&, void*) = nullptr;
675 void* server_resolver_data_ = nullptr;
676
683 ServerManager& servers_for(const std::string& session_key) const;
684
685 const LoopConfig& loop_config_;
686 EngineCallbacks& callbacks_;
687 ToolExecutorHooks hooks_;
688 PermissionPersistInterface permission_persist_;
692 const std::unordered_map<std::string, std::vector<std::string>>*
693 tier_allowed_tools_ = nullptr;
694 HookInterface hook_iface_;
695 ToolCallHistory history_{100};
696 std::atomic<size_t> history_seq_{0};
697
698public:
706 const ToolCallHistory& tool_history() const { return history_; }
707
714 void set_hooks(const HookInterface& hooks) { hook_iface_ = hooks; }
715
725
726private:
727 MCPAuthorizationManager* auth_mgr_ = nullptr;
728
737 std::optional<Message> check_mcp_authorization(
738 const LoopContext& ctx,
739 const ToolCall& call) const;
740};
741
742} // namespace entropic
Per-identity MCP authorization with runtime grant/revoke.
Manages MCP server instances and routes tool calls.
Fixed-size ring buffer of recent tool calls.
Processes tool calls from model output.
void set_authorization_manager(MCPAuthorizationManager *auth_mgr)
Set the MCP authorization manager.
std::vector< Message > process_tool_calls(LoopContext &ctx, const std::vector< ToolCall > &tool_calls)
Process a batch of tool calls.
void set_tier_allowed_tools(const std::unordered_map< std::string, std::vector< std::string > > *map)
Wire the per-tier allowed_tools map for dispatch-time enforcement.
void set_server_resolver(ServerManager *(*fn)(const std::string &session_key, void *user_data), void *user_data)
Resolve a session's MCP servers (gh#166, v2.13.0).
void set_permission_persist(const PermissionPersistInterface &persist)
Set permission persistence interface.
const ToolCallHistory & tool_history() const
Access the tool-call history ring buffer.
void set_hooks(const HookInterface &hooks)
Set the hook dispatch interface.
Directive processing for tool-to-engine communication.
Types for the agentic loop engine.
long limit
Results to keep.
Per-identity MCP authorization with runtime grant/revoke.
Activate model on GPU (WARM → ACTIVE).
ToolResultKind
Categorical outcome of a single tool invocation.
Definition tool_result.h:32
@ ok
Tool dispatched, returned non-empty content.
@ error
Tool server returned an error payload.
@ optional
optional: every escape asks the host's path approver
@ tier
The TIER's identity frontmatter grammar: stem (gh#154).
MCP server lifecycle management and tool routing.
Callback function pointer types for engine events.
Configuration for the agentic loop.
Mutable state carried through the agentic loop.
A message in a conversation.
Definition message.h:36
Permission persistence interface.
Outcome of precondition checks for a single tool call.
std::optional< Message > rejection
Rejection message (nullopt if cleared)
ToolResultKind kind
Category on rejection.
A tool call request parsed from model output.
Definition tool_call.h:31
Engine-level hooks called during tool processing.
In-memory ring buffer of recent tool calls for introspection.
Typed outcome for POST_TOOL_CALL hook consumers.