13#include <nlohmann/json.hpp>
38 : server_manager_(server_manager),
39 loop_config_(loop_config),
40 callbacks_(callbacks),
51 permission_persist_ = persist;
64 const std::vector<ToolCall>& tool_calls) {
65 logger->info(
"Processing {} tool calls", tool_calls.size());
66 ctx.
state = AgentState::WAITING_TOOL;
67 fire_state_callback(ctx);
69 auto limited = sort_tool_calls(tool_calls);
73 truncate_to_limit(limited, eff_limit);
75 std::vector<Message> results;
76 for (
const auto& call : limited) {
77 auto msgs = process_single_call(ctx, call);
78 for (
auto& m : msgs) {
79 results.push_back(std::move(m));
81 if (should_stop_batch(ctx, results)) {
97std::vector<ToolCall> ToolExecutor::sort_tool_calls(
98 const std::vector<ToolCall>& calls) {
100 std::stable_sort(sorted.begin(), sorted.end(),
102 bool a_delegate = (a.name ==
"entropic.delegate");
103 bool b_delegate = (b.name ==
"entropic.delegate");
104 return !a_delegate && b_delegate;
117std::string ToolExecutor::check_duplicate(
118 const LoopContext& ctx,
119 const ToolCall& call)
const {
123 auto key = tool_call_key(call);
124 auto it = ctx.recent_tool_calls.find(key);
125 if (it != ctx.recent_tool_calls.end()) {
140Message ToolExecutor::handle_duplicate(
142 const ToolCall& call,
143 const std::string& previous_result) {
144 ctx.consecutive_duplicate_attempts++;
145 logger->warn(
"Duplicate tool call #{}: {}",
146 ctx.consecutive_duplicate_attempts, call.name);
148 if (ctx.consecutive_duplicate_attempts >= 3) {
149 return create_circuit_breaker_message();
151 return create_duplicate_message(call, previous_result);
161bool ToolExecutor::check_approval(
const ToolCall& call) {
162 auto args_json = serialize_args(call);
165 call.name, args_json);
167 bool approved = auto_ok;
169 auto call_json = serialize_tool_call(call);
176 if (hook_iface_.fire_info !=
nullptr) {
177 std::string perm = approved ?
"allowed" :
"denied";
178 std::string json =
"{\"tool_name\":\""
179 + call.name +
"\",\"permission\":\"" + perm +
"\"}";
180 hook_iface_.fire_info(hook_iface_.registry,
185 logger->warn(
"No approval callback — denying: {}", call.name);
198std::optional<Message> ToolExecutor::check_tier_allowed(
199 const LoopContext& ctx,
const ToolCall& call)
const {
202 if (ctx.locked_tier.empty()) {
218 const nlohmann::json& schema,
219 const nlohmann::json& args)
221 for (
const auto& req : schema.value(
"required",
222 nlohmann::json::array())) {
223 if (!args.contains(req.get<std::string>())) {
224 return "Missing required argument: "
225 + req.get<std::string>();
250 const std::string& key,
251 const nlohmann::json& allowed,
252 const nlohmann::json& val)
254 for (
const auto& e : allowed) {
255 if (e == val) {
return ""; }
257 return "Invalid value for '" + key +
"': "
258 + val.dump() +
". Must be one of: " + allowed.dump();
271 const std::string& key,
272 const std::string& type,
273 const nlohmann::json& val)
275 bool ok = (type ==
"string" && val.is_string())
276 || (type ==
"integer" && val.is_number_integer())
277 || (type ==
"number" && val.is_number())
278 || (type ==
"boolean" && val.is_boolean())
279 || (type ==
"array" && val.is_array())
280 || (type ==
"object" && val.is_object());
281 return ok ?
"" :
"Type mismatch for '" + key
282 +
"': expected " + type;
295 const std::string& key,
296 const nlohmann::json& prop,
297 const nlohmann::json& val)
299 if (prop.contains(
"enum")) {
300 auto err =
check_enum(key, prop[
"enum"], val);
301 if (!err.empty()) {
return err; }
303 if (!prop.contains(
"type")) {
return ""; }
304 return check_type(key, prop[
"type"].get<std::string>(), val);
320 const std::string& schema_json,
321 const nlohmann::json& args)
323 auto schema = nlohmann::json::parse(schema_json,
nullptr,
false);
324 if (!schema.is_object()) {
return ""; }
327 auto props = schema.value(
"properties", nlohmann::json::object());
328 for (
auto it = props.begin(); it != props.end() && err.empty(); ++it) {
329 if (args.contains(it.key())) {
331 it.key(), it.value(), args[it.key()]);
346 auto j = nlohmann::json::parse(result_json);
347 return j.value(
"result", result_json);
361std::pair<Message, std::string> ToolExecutor::execute_tool(
362 LoopContext& ctx,
const ToolCall& call) {
364 auto args_json = serialize_args(call);
367 auto call_json = serialize_tool_call(call);
372 auto start = std::chrono::steady_clock::now();
379 auto result_json = mcp::sanitize_utf8(
380 server_manager_.
execute(call.name, args_json));
381 auto end = std::chrono::steady_clock::now();
382 auto ms = std::chrono::duration_cast<
383 std::chrono::milliseconds>(end - start).count();
385 ctx.metrics.tool_calls++;
393 record_tool_history(call, args_json, result_text, ms,
394 ctx.metrics.iterations);
396 fire_tool_complete_callback(call, result_text, ms);
400 msg.content = result_text;
401 msg.metadata[
"tool_call_id"] = call.id;
402 msg.metadata[
"tool_name"] = call.name;
404 return {std::move(msg), result_json};
417void ToolExecutor::record_tool_history(
const ToolCall& call,
418 const std::string& args_json,
419 const std::string& result_text,
420 long long ms,
int iteration) {
422 rec.sequence = ++history_seq_;
423 rec.tool_name = call.name;
425 rec.status = (result_text.rfind(
"error", 0) == 0)
426 ?
"error" :
"success";
428 rec.elapsed_ms =
static_cast<double>(ms);
429 rec.iteration = iteration;
440std::string ToolExecutor::tool_call_key(
const ToolCall& call) {
443 for (
const auto& [k, v] : call.arguments) {
446 return call.name +
":" + args.dump();
457void ToolExecutor::record_tool_call(
459 const ToolCall& call,
460 const std::string& result) {
462 std::string text = result;
464 auto j = nlohmann::json::parse(result);
465 text = j.value(
"result", result);
469 if (text.find(
"Error:") == 0 || text.find(
"error:") == 0) {
472 auto key = tool_call_key(call);
473 ctx.recent_tool_calls[key] = text;
484Message ToolExecutor::create_denied_message(
485 const ToolCall& call,
486 const std::string& reason) {
490 "Tool `" + call.name +
"` was denied: " + reason +
"\n\n"
491 "This tool is not available to you. Do NOT retry it. "
492 "Use a different approach to accomplish your task.";
504Message ToolExecutor::create_error_message(
505 const ToolCall& call,
506 const std::string&
error) {
510 "Tool `" + call.name +
"` failed with error: " +
error +
512 "- Check arguments are correct\n"
513 "- Try a different approach\n"
514 "- Do NOT retry with the same arguments";
526void ToolExecutor::fire_state_callback(
const LoopContext& ctx) {
529 static_cast<int>(ctx.state), callbacks_.
user_data);
540void ToolExecutor::truncate_to_limit(
541 std::vector<ToolCall>& calls,
543 auto lim =
static_cast<size_t>(limit);
544 if (calls.size() > lim) {
557std::optional<Message> ToolExecutor::check_mcp_authorization(
558 const LoopContext& ctx,
559 const ToolCall& call)
const {
560 if (auth_mgr_ ==
nullptr) {
563 auto identity = ctx.locked_tier.empty()
564 ?
"lead" : ctx.locked_tier;
568 auth_mgr_->
check_access(identity, call.name, required)) {
572 logger->warn(
"MCP key denied: {} requires {} for {}",
573 call.name, level_str, identity);
577 "Tool `" + call.name +
"` was denied: identity `"
578 + identity +
"` lacks " + level_str
580 "Your MCP key set does not authorize this tool. "
581 "Use `entropic.delegate` to hand off to an identity "
582 "that has the required access.";
594std::optional<Message> ToolExecutor::check_dup_or_approval(
595 LoopContext& ctx,
const ToolCall& call) {
596 auto dup_result = check_duplicate(ctx, call);
597 if (!dup_result.empty()) {
598 return handle_duplicate(ctx, call, dup_result);
600 ctx.consecutive_duplicate_attempts = 0;
601 return check_approval(call)
603 : std::optional{create_denied_message(
604 call,
"Permission denied")};
622std::optional<Message> ToolExecutor::check_schema(
623 const ToolCall& call) {
625 if (schema.empty()) {
return std::nullopt; }
626 auto args = nlohmann::json::parse(
627 serialize_args(call),
nullptr,
false);
628 auto err = args.is_discarded()
630 if (err.empty()) {
return std::nullopt; }
631 logger->warn(
"Tool '{}' argument validation failed: {}",
633 return create_denied_message(call, err);
649PreconditionCheck ToolExecutor::check_call_preconditions(
650 LoopContext& ctx,
const ToolCall& call) {
655 PreconditionCheck pc = check_anti_spiral_hard_block(ctx, call);
656 if (pc.rejection.has_value()) {
659 if (
auto r = check_schema(call); r.has_value()) {
660 pc.rejection = std::move(r);
662 }
else if (
auto a = check_mcp_authorization(ctx, call);
664 pc.rejection = std::move(a);
666 }
else if (
auto t = check_tier_allowed(ctx, call); t.has_value()) {
667 pc.rejection = std::move(t);
669 }
else if (
auto dup = check_duplicate(ctx, call); !
dup.empty()) {
670 pc.rejection = handle_duplicate(ctx, call,
dup);
673 pc = check_approval_pc(ctx, call);
687PreconditionCheck ToolExecutor::check_approval_pc(
688 LoopContext& ctx,
const ToolCall& call) {
689 PreconditionCheck pc;
690 ctx.consecutive_duplicate_attempts = 0;
691 if (!check_approval(call)) {
692 pc.rejection = create_denied_message(
693 call,
"Permission denied");
711std::vector<Message> ToolExecutor::process_single_call(
712 LoopContext& ctx,
const ToolCall& call) {
715 if (fire_pre_tool_hook(ctx, call)) {
716 auto msg = create_denied_message(call,
"Cancelled by hook");
717 fire_post_tool_hook(ctx, call,
"", 0.0,
719 return {std::move(msg)};
722 auto pc = check_call_preconditions(ctx, call);
723 if (pc.rejection.has_value()) {
724 logger->info(
"Tool '{}' rejected by precondition (kind={})",
726 fire_post_tool_hook(ctx, call,
"", 0.0, pc.kind, *pc.rejection);
727 return {std::move(*pc.rejection)};
730 auto exec_start = std::chrono::steady_clock::now();
731 auto [msg, raw_result] = execute_tool(ctx, call);
732 auto exec_ms = std::chrono::duration<double, std::milli>(
733 std::chrono::steady_clock::now() - exec_start).count();
735 finalize_tool_call(ctx, call, msg, raw_result, exec_ms);
737 return {std::move(msg)};
748 if (mcp::looks_like_tool_error(content)) {
751 if (mcp::is_effectively_empty(content)) {
767void ToolExecutor::log_tool_call(LoopContext& ctx,
const ToolCall& call,
769 const std::string& raw_result,
771 auto args_log = serialize_args(call);
772 if (args_log.size() > 512) { args_log.resize(512); }
773 logger->info(
"[tool_call] iter={} tier={} tool={} args={} "
774 "elapsed_ms={:.0f} result_chars={} status={}",
775 ctx.metrics.iterations,
776 ctx.locked_tier.empty() ?
"lead" : ctx.locked_tier,
777 call.name, args_log, exec_ms,
791void ToolExecutor::finalize_tool_call(LoopContext& ctx,
const ToolCall& call,
793 const std::string& raw_result,
799 apply_result_size_cap(msg.content);
800 ctx.effective_tool_calls++;
801 msg.metadata[
"added_at_iteration"] =
802 std::to_string(ctx.metrics.iterations);
803 record_tool_call(ctx, call, raw_result);
807 fire_post_tool_hook(ctx, call, raw_result, exec_ms, kind, msg);
812 update_anti_spiral_tracking(ctx, call.name);
814 log_tool_call(ctx, call, exec_ms, raw_result, kind);
816 extract_and_process_directives(ctx, raw_result);
817 run_post_tool_hooks(ctx);
828bool ToolExecutor::fire_pre_tool_hook(
829 const LoopContext& ctx,
const ToolCall& call) {
830 if (hook_iface_.fire_pre ==
nullptr) {
return false; }
831 auto json = build_pre_tool_json(call, ctx.locked_tier,
832 ctx.metrics.iterations);
834 int rc = hook_iface_.fire_pre(hook_iface_.registry,
848void ToolExecutor::apply_result_size_cap(std::string& content)
const {
860void ToolExecutor::update_anti_spiral_tracking(
861 LoopContext& ctx,
const std::string& tool_name) {
862 if (tool_name == ctx.last_tool_name) {
863 ++ctx.consecutive_same_tool_calls;
865 ctx.last_tool_name = tool_name;
866 ctx.consecutive_same_tool_calls = 1;
868 if (ctx.consecutive_same_tool_calls
870 ctx.pending_anti_spiral_warning =
871 tool_name +
" has been called "
872 + std::to_string(ctx.consecutive_same_tool_calls)
873 +
" times consecutively; pivot to a different tool or "
874 "complete the task next turn.";
886int ToolExecutor::effective_hard_block_threshold()
const {
888 if (configured < 0) {
909PreconditionCheck ToolExecutor::check_anti_spiral_hard_block(
910 const LoopContext& ctx,
const ToolCall& call)
const {
911 PreconditionCheck pc;
912 int projected = (call.name == ctx.last_tool_name)
913 ? (ctx.consecutive_same_tool_calls + 1)
915 int threshold = effective_hard_block_threshold();
916 if (projected >= threshold) {
918 "[anti-spiral] tool '" + call.name +
"' blocked after "
919 + std::to_string(projected)
920 +
" consecutive calls (threshold "
921 + std::to_string(threshold)
922 +
"); pivot to a different tool or complete the task.";
923 pc.rejection = create_denied_message(call, text);
947void ToolExecutor::fire_post_tool_hook(
948 const LoopContext& ctx,
const ToolCall& call,
949 const std::string& raw_result,
double elapsed_ms,
951 if (hook_iface_.fire_post ==
nullptr) {
return; }
952 auto json = build_post_tool_json(
953 call, raw_result, elapsed_ms, ctx.locked_tier,
954 ctx.metrics.iterations, kind);
956 hook_iface_.fire_post(hook_iface_.registry,
958 if (out !=
nullptr) {
972bool ToolExecutor::should_stop_batch(
973 const LoopContext& ctx,
974 const std::vector<Message>& )
const {
975 return ctx.state == AgentState::COMPLETE
976 || ctx.pending_delegation.has_value()
977 || ctx.pending_pipeline.has_value()
978 || ctx.consecutive_duplicate_attempts >= 3;
987void ToolExecutor::run_post_tool_hooks(LoopContext& ctx) {
999Message ToolExecutor::create_circuit_breaker_message() {
1003 "STOP: You have called the same tool 3 times with "
1004 "identical arguments. This indicates you are stuck. "
1005 "Please try a completely different approach or respond "
1006 "to the user explaining what's blocking you.";
1007 logger->error(
"Circuit breaker triggered");
1019Message ToolExecutor::create_duplicate_message(
1020 const ToolCall& call,
1021 const std::string& previous_result) {
1023 previous_result.find(
"was denied") != std::string::npos
1024 || previous_result.find(
"not available") != std::string::npos;
1031 "Tool `" + call.name +
"` is not available to you "
1032 "and retrying will not help. You MUST use a different "
1033 "approach. Do NOT call `" + call.name +
"` again.";
1036 "Tool `" + call.name +
"` was already called with "
1037 "the same arguments.\n\nPrevious result:\n" +
1039 "\n\nDo NOT call this tool again. "
1040 "Use the previous result above.";
1057std::string ToolExecutor::serialize_args(
const ToolCall& call) {
1058 if (!call.arguments_json.empty()) {
1059 return call.arguments_json;
1061 nlohmann::json args;
1062 for (
const auto& [k, v] : call.arguments) {
1075std::string ToolExecutor::serialize_tool_call(
const ToolCall& call) {
1078 j[
"name"] = call.name;
1079 j[
"arguments"] = nlohmann::json::object();
1080 for (
const auto& [k, v] : call.arguments) {
1081 j[
"arguments"][k] = v;
1094void ToolExecutor::fire_tool_complete_callback(
1095 const ToolCall& call,
1096 const std::string& result,
1101 auto call_json = serialize_tool_call(call);
1103 call_json.c_str(), result.c_str(),
1104 static_cast<double>(ms), callbacks_.
user_data);
1119std::string ToolExecutor::build_post_tool_json(
1120 const ToolCall& call,
1121 const std::string& raw_result,
1123 const std::string& tier,
1127 ctx[
"tool_name"] = call.name;
1128 ctx[
"args"] = nlohmann::json::parse(serialize_args(call));
1130 ctx[
"tier"] = tier.empty() ? std::string{
"lead"} : tier;
1131 ctx[
"iteration"] = iteration;
1134 auto sr = nlohmann::json::parse(raw_result);
1135 ctx[
"result"] = sr.value(
"result", raw_result);
1136 ctx[
"directives"] = sr.value(
1137 "directives", nlohmann::json::array());
1139 ctx[
"result"] = raw_result;
1140 ctx[
"directives"] = nlohmann::json::array();
1154std::string ToolExecutor::build_pre_tool_json(
1155 const ToolCall& call,
1156 const std::string& tier,
1159 j[
"tool_name"] = call.name;
1160 j[
"args"] = nlohmann::json::parse(serialize_args(call));
1161 j[
"tier"] = tier.empty() ? std::string{
"lead"} : tier;
1162 j[
"iteration"] = iteration;
1186 const nlohmann::json& result_json) {
1187 std::vector<std::string> stages;
1188 if (!result_json.contains(
"stages")) {
return stages; }
1189 for (
const auto& s : result_json[
"stages"]) {
1190 stages.push_back(s.get<std::string>());
1216 const nlohmann::json& result_json) {
1217 auto cd = std::make_unique<CompleteDirective>(
1218 result_json.value(
"summary",
""));
1219 cd->coverage_gap = result_json.value(
"coverage_gap",
false);
1220 cd->gap_description = result_json.value(
"gap_description",
"");
1221 if (result_json.contains(
"suggested_files")
1222 && result_json[
"suggested_files"].is_array()) {
1223 cd->suggested_files =
1224 result_json[
"suggested_files"].get<std::vector<std::string>>();
1235 const nlohmann::json& d,
const nlohmann::json& result_json) {
1236 auto type_str = d.value(
"type",
"");
1237 std::unique_ptr<Directive> result;
1238 if (type_str ==
"stop_processing") {
1239 result = std::make_unique<StopProcessingDirective>();
1240 }
else if (type_str ==
"delegate") {
1245 result = std::make_unique<DelegateDirective>(
1246 result_json.value(
"target",
""),
1247 result_json.value(
"task",
""),
1248 result_json.value(
"max_turns", -1),
1249 result_json.value(
"delegation_id",
""));
1250 }
else if (type_str ==
"complete") {
1252 }
else if (type_str ==
"pipeline") {
1253 result = std::make_unique<PipelineDirective>(
1255 result_json.value(
"task",
""));
1268static std::optional<std::pair<nlohmann::json, nlohmann::json>>
1270 auto resp = nlohmann::json::parse(raw_result,
nullptr,
false);
1271 if (!resp.is_object() || !resp.contains(
"directives")) {
1272 return std::nullopt;
1274 auto dirs = resp[
"directives"];
1275 if (!dirs.is_array() || dirs.empty()) {
return std::nullopt; }
1276 return std::make_pair(std::move(resp), std::move(dirs));
1286void ToolExecutor::extract_and_process_directives(
1287 LoopContext& ctx,
const std::string& raw_result) {
1290 if (!extracted) {
return; }
1291 auto& [resp, dirs] = *extracted;
1293 auto result_json = nlohmann::json::parse(
1294 resp.value(
"result",
"{}"),
nullptr,
false);
1296 std::vector<std::unique_ptr<Directive>> owned;
1297 for (
const auto& d : dirs) {
1299 if (directive) { owned.push_back(std::move(directive)); }
1301 if (owned.empty()) {
return; }
1303 std::vector<const Directive*> ptrs;
1304 ptrs.reserve(owned.size());
1305 for (
const auto& d : owned) { ptrs.push_back(d.get()); }
1306 logger->info(
"Processing {} directives from tool result", ptrs.size());
bool is_enforced(const std::string &identity_name) const
Check if an identity has authorization enforcement enabled.
bool check_access(const std::string &identity_name, const std::string &tool_name, MCPAccessLevel required_level) const
Check if a tool call is authorized for an identity.
Manages MCP server instances and routes tool calls.
MCPAccessLevel get_required_access_level(const std::string &tool_name) const
Get the required access level for a tool.
bool is_explicitly_allowed(const std::string &tool_name, const std::string &args_json) const
Check if tool is explicitly allowed (skip prompting).
bool skip_duplicate_check(const std::string &tool_name) const
Check if tool should skip duplicate detection.
std::string get_tool_schema(const std::string &tool_name) const
Get the JSON Schema for a tool's input parameters.
std::string execute(const std::string &tool_name, const std::string &args_json)
Execute a tool call via the appropriate server.
void record(const ToolCallRecord &entry)
Record a completed tool call.
@ ENTROPIC_HOOK_ON_PERMISSION_CHECK
15: Permission check evaluated
@ ENTROPIC_HOOK_PRE_TOOL_CALL
3: Before tool execution
@ ENTROPIC_HOOK_POST_TOOL_CALL
4: After tool execution returns
spdlog initialization and logger access.
ENTROPIC_EXPORT std::shared_ptr< spdlog::logger > get(const std::string &name)
Get or create a named logger.
double elapsed_ms(std::chrono::steady_clock::time_point start, std::chrono::steady_clock::time_point end)
Compute elapsed milliseconds between two time points.
Activate model on GPU (WARM → ACTIVE).
static std::string check_type(const std::string &key, const std::string &type, const nlohmann::json &val)
Check a single value against a type constraint.
ToolResultKind
Categorical outcome of a single tool invocation.
@ ok
Tool dispatched, returned non-empty content.
@ rejected_schema
Precondition: argument schema violation.
@ rejected_anti_spiral
Anti-spiral hard threshold crossed; tool blocked. (#14, v2.1.4)
@ rejected_duplicate
Precondition: duplicate in recent history.
@ ok_empty
Tool dispatched cleanly but returned no content (v2.1.0, #44)
@ error
Tool server returned an error payload.
@ rejected_precondition
Any other precondition reject (auth, tier, hook-cancel)
static std::string parse_tool_result_text(const std::string &result_json)
Extract the "result" text from an MCP result JSON envelope.
static std::vector< std::string > extract_pipeline_stages(const nlohmann::json &result_json)
Extract directives from ServerResponse JSON and process them.
static std::string check_property_constraints(const std::string &key, const nlohmann::json &prop, const nlohmann::json &val)
Check one property's enum and type constraints.
static ToolResultKind classify_tool_result(const std::string &content)
Classify a tool result by its content (error/empty/ok).
const char * mcp_access_level_name(MCPAccessLevel level)
Convert MCPAccessLevel to string representation.
static std::unique_ptr< Directive > build_directive(const nlohmann::json &d, const nlohmann::json &result_json)
Build a Directive from a parsed directive + result JSON.
const char * result_kind_to_string(ToolResultKind kind)
Serialize a ToolResultKind to its wire-stable string form.
static std::unique_ptr< Directive > build_complete_directive(const nlohmann::json &result_json)
Build a typed Directive from a directive-descriptor JSON.
static std::optional< std::pair< nlohmann::json, nlohmann::json > > extract_directive_array(const std::string &raw_result)
Pull the "directives" array out of a tool ServerResponse JSON.
std::string truncate_result(const std::string &text, size_t max_len)
Truncate a string to max_len characters with "..." suffix.
static char * dup(const std::string &s)
Heap-allocate a C string copy.
std::string summarize_params(const std::string &args_json)
Extract top-level JSON keys as a comma-separated summary.
static std::string validate_tool_args(const std::string &schema_json, const nlohmann::json &args)
Validate tool arguments against the tool's JSON Schema.
static std::string check_enum(const std::string &key, const nlohmann::json &allowed, const nlohmann::json &val)
Check one property's enum and type constraints.
static std::string check_required_fields(const nlohmann::json &schema, const nlohmann::json &args)
Check required fields are present.
Callback function pointer types for engine events.
void(* on_tool_call)(const char *json, void *ud)
Tool call request.
void(* on_tool_complete)(const char *json, const char *result, double ms, void *ud)
Tool execution done.
void * user_data
Opaque pointer passed to all callbacks.
void(* on_tool_start)(const char *json, void *ud)
Tool execution start.
void(* on_state_change)(int state, void *ud)
AgentState as int.
Configuration for the agentic loop.
int max_consecutive_same_tool
Anti-spiral SOFT threshold: after N consecutive calls of the SAME tool (regardless of arg similarity,...
int max_consecutive_same_tool_hard_block
Anti-spiral HARD threshold: when consecutive same-tool calls exceed this, the engine blocks the call ...
bool auto_approve_tools
Skip tool approval (v1.8.5)
int max_tool_result_bytes
Maximum byte length for a single tool's result content before the engine truncates with a "[....
int max_tool_calls_per_turn
Tool calls per iteration (v1.8.5)
Mutable state carried through the agentic loop.
int consecutive_errors
Error streak counter.
int effective_max_tool_calls_per_turn
Per-identity override (-1 = LoopConfig, P3-18)
AgentState state
Current state.
Permission persistence interface.
UTF-8 validation + replacement at every system boundary where bytes change ownership.