Repository navigation
Expand file tree
/
Copy pathtool_executor.cc
More file actions
338 lines (310 loc) · 15.1 KB
/
Copy pathtool_executor.cc
File metadata and controls
338 lines (310 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "tool_executor.h"
#include "model.h"
#include "session_utils.h"
#include "output.h"
#include "signals.h"
#include "parsers.h"
#include "tokens.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;
using namespace Tokens;
// Forward declarations for functions defined in main.cc
extern void diag(const string& msg, const char* color);
extern bool is_debug;
extern ofstream chat_log;
extern ofstream token_log;
ToolExecutor::Result ToolExecutor::execute(
SessionState& state,
string& generated_text,
const string& full_generated,
size_t tool_start,
size_t tool_end,
bool was_mid_tool_call,
function<vector<llama_token>(string)> tokenize,
function<bool(const vector<llama_token>&)> feed_tokens,
llama_context* ctx,
int& n_past,
const llama_context_params& cparams,
int& g_auto_continue_depth,
int max_auto_continue
) {
Result result;
// When resuming from a mid-tool-call interrupt, prepend the saved partial text
// so the extracted tool_call contains the complete XML including FUNC_START.
string full_gen = full_generated;
if (!state.partial_tool_text.empty()) {
full_gen = state.partial_tool_text + full_gen;
state.partial_tool_text.clear();
// tool_end was found within generated_text, but full_gen now has
// state.partial_tool_text prepended. Adjust tool_end to be relative to full_gen.
// Re-search for FUNC_END in full_gen starting from tool_start to get the correct offset.
// On resume (tool_start==0), search from the actual FUNC_START position inside
// state.partial_tool_text to avoid double-counting it (depth would go 1->2 and never return).
size_t resume_search_from = was_mid_tool_call
? full_gen.find(FUNC_START)
: tool_start;
tool_end = find_tool_end_robust(full_gen, resume_search_from);
if (tool_end != string::npos) {
size_t exact_pos = full_gen.find(FUNC_END, resume_search_from);
if (exact_pos == string::npos) {
repair_malformed_tool_end(full_gen, tool_end);
tool_end = full_gen.find(FUNC_END, resume_search_from);
}
}
}
string tool_call = full_gen.substr(tool_start, tool_end - tool_start + string(FUNC_END).length());
string preamble = "";
if (tool_start > 0) preamble = generated_text.substr(0, tool_start);
vector<string> strip_tags_vec;
// Strip full turn markers (user_start, assistant_start, turn_end).
if (!g_model_tokens.user_turn_start.text.empty()) strip_tags_vec.push_back(g_model_tokens.user_turn_start.text);
if (!g_model_tokens.assistant_turn_start.text.empty()) strip_tags_vec.push_back(g_model_tokens.assistant_turn_start.text);
if (!g_model_tokens.turn_end.text.empty()) strip_tags_vec.push_back(g_model_tokens.turn_end.text);
// Also strip individual base tokens (<|im_end|>, <|eot_id|>, etc.) that can
// appear as spurious EOGs embedded inside XML tags and attributes.
collect_base_turn_tokens(strip_tags_vec);
strip_tags(tool_call, strip_tags_vec);
// Strip thinking blocks (think_start...think_end) that can leak into tool calls.
// The LLM sometimes emits thinking tags mid-tool-call, corrupting params.
if (!g_model_tokens.think_start.empty() && !g_model_tokens.think_end.empty()) {
size_t ts;
while ((ts = tool_call.find(g_model_tokens.think_start)) != string::npos) {
size_t te = tool_call.find(g_model_tokens.think_end, ts);
if (te != string::npos) {
tool_call.erase(ts, te + g_model_tokens.think_end.length() - ts);
} else {
// Unclosed think tag -- strip from marker to end of tool call.
tool_call.erase(ts);
break;
}
}
}
ToolResult tool_out;
bool abort_auto = false;
// Execute the tool.
tool_out = execute_tool_call(tool_call, state);
// Handle validation errors reported by the struct.
// Policy: STRUCTURALLY broken calls (unknown tool, or broken XML such as
// a missing PARAM_END causing param bleed) get a correction attempt --
// the main loop rolls back via the slot checkpoint (or, when the slot is
// stale after a non-lockstep MTP FUNC_START, re-decodes the suffix from
// the newest prompt checkpoint -- never a full-context re-decode), feeds
// the system prompt, lets the LLM generate a fix, then injects the good
// tool call cleanly. Feeding the abort message and ejecting to the
// prompt happens only when this call's correction attempt is already
// spent, or when the correction itself fails (step 8b in the main loop).
// A MINOR validation error -- the call is recognized and structurally
// well-formed but missing a required parameter -- first gets one quick
// self-fix chance: execute_tool_call set a targeted error (tool name +
// missing params + the tool's full required set) and it is fed back as
// an ordinary tool result so the model re-issues the call complete.
// There is no broken XML to roll back, and a precise message avoids the
// theory-spewing a vague "invalid tool call" invites. Only when the
// model's next tool call in this chain is AGAIN a minor validation
// error does the call revert to the correction cycle above (or eject to
// the prompt when that attempt is already spent).
bool structural_failure = !tool_out.recognized || tool_out.malformed_xml;
bool minor_failure = !structural_failure && !tool_out.params_valid;
if (structural_failure || minor_failure) {
// A structural failure moves past any pending minor-error self-fix
// chance (the model's reply was structurally broken, not a
// completed re-issue): clear the latch so a later minor error in
// this chain gets a fresh chance.
if (structural_failure) state.minor_tool_error_self_fix_used = false;
if (is_debug) {
// Show the raw tool call for diagnosis.
diag(" Raw tool_call: " + tool_call, "\033[2;90m");
diag(" Parsed tool name: \"" + tool_out.parsed_tool_name + "\"", "\033[2;90m");
if (!tool_out.recognized) {
diag(" Reason: Unknown tool name. Known tools: read_files, search_file, write_file, edit_file, exec_shell, web_search.", "\033[2;90m");
}
}
if (minor_failure && !state.minor_tool_error_self_fix_used) {
// One quick self-fix chance: do NOT correct -- fall through and
// feed the targeted error below as an ordinary tool result so
// the model re-issues the call complete.
state.minor_tool_error_self_fix_used = true;
diag("System: Missing required parameter(s); giving the model one chance to re-issue the call.", "\033[1;33m");
} else if (!state.correction_attempted_this_turn) {
// Attempt tool-call correction: the main loop rolls back via the
// slot checkpoint (or the prompt-anchor suffix re-decode when the
// slot is stale after a non-lockstep MTP FUNC_START), feeds the
// full system prompt, lets the LLM generate a fix, then injects
// the good tool call cleanly.
diag("System: Invalid tool call. Attempting correction.", "\033[1;33m");
state.correction_attempted_this_turn = true;
result.needs_correction = true;
} else {
// A correction was already attempted for this call: feed the
// abort message and eject to the prompt.
diag("System: Invalid tool call. Ejecting to prompt.", "\033[1;31m");
abort_auto = true;
}
} else {
// No validation error: the model moved on, so any pending
// minor-error self-fix chance is resolved and a later minor error
// in this chain starts with a fresh chance.
state.minor_tool_error_self_fix_used = false;
if (stop_generation) {
diag("Tool Interrupted by User", "\033[31m");
stop_generation = 0;
state.reincarnate_mode = false;
}
}
// Feed the malformed-call abort message as a tool result (shared by the
// mid-chain abort and the final no-continue abort paths).
auto feed_abort = [&]() {
string abort_msg = "System Error: You are generating malformed tool calls. Your XML schema is incorrect. Stop and carefully review the required format. Do NOT wrap tool calls in markdown code blocks or other formatting.";
vector<llama_token> abort_tokens = build_tool_result_turn(ctx, abort_msg);
// build_tool_result_turn wraps: user_start + "[Tool Result]\n" + msg + turn_end + "\n" + assistant_start
if (n_past + (int)abort_tokens.size() < (int)cparams.n_ctx) {
feed_tokens(abort_tokens);
// Log abort tool result tokens to token_log when debug is enabled
log_tokens("FEED TOOL_RESULT", abort_tokens, ctx);
}
};
if (!abort_auto) {
// If correction is needed, return immediately without feeding any tool
// result tokens. The main loop will handle the correction cycle: feed
// the system prompt reminder, generate once, parse for a valid tool call,
// then roll back and inject cleanly.
if (result.needs_correction) {
state.auto_continue = false;
return result;
}
// Edge-newline corrections (high_attention) are fed as a plain user
// turn -- the operator channel, which converges in ~1 retry vs 2-3 for
// the [Tool Result] channel (toolCallAnalysis.md decision 4).
bool operator_turn = tool_out.high_attention;
if (is_debug) {
console(operator_turn ? "\n\033[91m[Operator]\033[0m\n" : "\n\033[92m[Tool Result]\033[0m\n");
string result_to_print = operator_turn ? tool_out.content : tool_out.display;
// Strip terminal control characters from tool output (issue 9) so
// injected content cannot emit ANSI/OSC sequences to the terminal.
result_to_print = terminal_safe(result_to_print);
static constexpr size_t STDOUT_TRUNCATE_LIMIT = 500;
if (should_output_to_stdout() && result_to_print.length() > STDOUT_TRUNCATE_LIMIT) {
size_t original_len = result_to_print.length();
result_to_print = result_to_print.substr(0, STDOUT_TRUNCATE_LIMIT) + "\n ... (truncated, " + std::to_string(original_len) + " chars total -- see browser for full output)\n";
}
size_t p = 0;
while ((p = result_to_print.find('\n')) != string::npos) {
string line = result_to_print.substr(0, p);
result_to_print.erase(0, p + 1);
console(" ", line.c_str(), "\n");
}
if (!result_to_print.empty()) console(" ", result_to_print.c_str(), "\n");
}
string display_for_browser = tool_out.display;
if (!display_for_browser.empty()) {
string safe_result = html_escape(display_for_browser);
string result_html = "\n\n<div class='tool-result'><pre><code>" + safe_result + "</code></pre></div>\n\n";
stream_html(result_html);
}
consoleFlush();
// Log to chat_log with structured label.
// Operator corrections are always logged -- including exec_shell, whose
// normal results stream to chat_log in filesystem.cc and are skipped
// here to avoid duplication (a rejected command streamed nothing).
if (operator_turn) {
if (!tool_out.content.empty()) {
chat_log << "=== OPERATOR ===\n" << tool_out.content << "\n\n";
chat_log.flush();
}
} else if (tool_out.parsed_tool_name != "exec_shell") {
string logged = tool_out.content;
// For web_search, log query + snippets but skip full page content
if (logged.find("Search Results for:") == 0) {
string filtered;
size_t i = 0;
while (i < logged.size()) {
size_t page_content = logged.find("Page Content: ", i);
if (page_content != string::npos && page_content + 14 < logged.size()) {
filtered += logged.substr(i, page_content - i);
size_t j = page_content + 14;
bool past_blank = false;
while (j < logged.size()) {
if (logged[j] == '\n') {
if (past_blank) break;
past_blank = true;
} else {
past_blank = false;
}
j++;
}
filtered += "[Page Content omitted from log]\n";
i = j;
} else {
filtered += logged.substr(i);
break;
}
}
logged = filtered;
}
if (!logged.empty()) {
chat_log << "=== TOOL_RESULT ===\n" << logged << "\n\n";
chat_log.flush();
}
}
generated_text = "";
// Build the feedback turn as a string, then tokenize in one pass.
// Operator corrections skip the [Tool Result] prefix: a plain user turn
// commands scolding-level attention (decision 4). build_user_assistant
// _turn_text and build_tool_result_turn_text differ only by that prefix,
// so the non-operator path is byte-identical to before.
vector<llama_token> t_tokens;
{
// Escape reserved tokens per the contract (SYSTEM -> LLM direction),
// then tokenize with parse_special=false on the content so any other
// special tokens in tool output are plain text, not control tokens.
string safe_content = tool_out.content;
escape_parameter_tags(safe_content);
escape_turn_tags(safe_content);
t_tokens = build_user_turn_safe(ctx, safe_content, !operator_turn);
}
// If the result doesn't fit in the remaining context, replace it with a
// compact error so the LLM can adapt (narrow the request, paginate, or
// proceed without it) instead of ejecting to prompt. Nothing has been fed
// yet, so the KV cache is clean and no rollback is needed. The full output
// was already shown to the user in the browser/chat log above.
if (n_past + (int)t_tokens.size() >= (int)cparams.n_ctx) {
double pct = (double)n_past / cparams.n_ctx * 100.0;
char buf[32];
snprintf(buf, sizeof(buf), "%.1f%%", pct);
diag("Tool result too large to fit in context (" + std::to_string(t_tokens.size()) + " tokens needed, " + std::to_string(cparams.n_ctx - n_past) + " available). Context usage: " + string(buf) + ". Reporting error to LLM.", "\033[1;33m");
string too_large_content = "System Error: Tool output too large to fit in remaining context (" + std::to_string(t_tokens.size()) + " tokens needed, " + std::to_string(cparams.n_ctx - n_past) + " available). It was discarded. Request a smaller output (e.g., a line range, head/tail, or a narrower query) or proceed without it.";
t_tokens = build_user_turn_safe(ctx, too_large_content, true);
}
if (!feed_tokens(t_tokens)) {
abort_auto = true;
} else {
// Log tool result tokens to token_log when debug is enabled
log_tokens(operator_turn ? "FEED OPERATOR" : "FEED TOOL_RESULT", t_tokens, ctx);
g_auto_continue_depth++;
if (g_auto_continue_depth > max_auto_continue) {
diag("System: Max auto-continue depth reached (" + std::to_string(g_auto_continue_depth) + "/" + std::to_string(max_auto_continue) + "). LLM may be stuck in a loop. Ejecting to prompt.", "\033[1;31m");
state.auto_continue = false;
} else {
diag_speed(n_past, cparams.n_ctx, state.last_t_count, state.last_elapsed, state.last_decode_time);
state.auto_continue = true;
result.should_auto_continue = true;
return result;
}
}
if (abort_auto) {
state.auto_continue = false;
generated_text = "";
feed_abort();
}
} else {
state.auto_continue = false;
generated_text = "";
feed_abort();
}
return result;
}