fix stale timeout, empty tool calls and trailing-space commands
Three defects from a review of the agent loop. A session's tool schema is built by create(), and load() then replaced the whole object with the one on disk, so a session saved under SOLO_TIMEOUT=42 went on telling the model the 42 s limit while run() enforced whatever the environment said now. The schema moves into mkschema() and is rebuilt after the load, so the description and the deadline always agree. A tool call whose arguments held no command, which a flaky router or a truncated stream can produce, was passed to the shell as an empty string. The shell ran it as [exit 0], the agent asked again, and a peer that always did this pinned the loop at full speed: a mock measuring 685 requests in five seconds of the same call. Such a call is now answered with an error instead of a shell run, command is declared required in the schema, and three rounds in a row that run nothing end the turn. The same mock now stops after three requests. dispatch() read a line without trimming it, so "/help " was taken as the generic parameter editor and wrote a help key into the session, "/quit " did not quit, and "/temperature 0.5 " stored trailing spaces. A line that starts with / or ! is right-trimmed before it is routed; a task keeps its exact text, leading space included, so the model still sees what was typed and a leading space still keeps the line out of the history. Eleven offline regression tests cover the three, including a mock endpoint that sends nothing but empty tool calls. make test: 42 passed, 0 failed.
This commit is contained in:
@@ -175,15 +175,13 @@ static void load(void) {
|
||||
json_object_put(SSN); SSN = o;
|
||||
}
|
||||
|
||||
static void create(void) {
|
||||
json_object *a, *o, *f, *p, *pr, *cp, *ms, *sy; char *sb, tb[512]; size_t nb;
|
||||
if(SSN) json_object_put(SSN);
|
||||
SSN = json_object_new_object();
|
||||
A(SSN, "model", S(MOD));
|
||||
A(SSN, "reasoning_effort", S(RSN));
|
||||
A(SSN, "temperature", D(TEMP));
|
||||
A(SSN, "stream", B(0));
|
||||
A(SSN, "tool_choice", S("auto"));
|
||||
/* Builds the single tool the agent exposes and stores it in the session. Kept
|
||||
apart from create() because the description quotes the timeout, so it has to
|
||||
be rebuilt after load(): a session saved under one SOLO_TIMEOUT would
|
||||
otherwise keep telling the model the old limit while run() enforced the new
|
||||
one. */
|
||||
static void mkschema(void) {
|
||||
json_object *a, *o, *f, *p, *pr, *cp, *rq; char tb[512];
|
||||
a = json_object_new_array(); o = json_object_new_object(); f = json_object_new_object();
|
||||
p = json_object_new_object(); pr = json_object_new_object(); cp = json_object_new_object();
|
||||
A(cp, "type", S("string"));
|
||||
@@ -191,10 +189,23 @@ static void create(void) {
|
||||
"A command still running after %ds is killed and reports exit code 124: start long work in the background instead and poll it, e.g. \"nohup make >build.log 2>&1 &\".", TO);
|
||||
A(cp, "description", S(tb));
|
||||
A(pr, "command", cp);
|
||||
A(p, "type", S("object")); A(p, "properties", pr); A(p, "required", json_object_new_array());
|
||||
rq = json_object_new_array(); json_object_array_add(rq, S("command")); /* command is required, so an endpoint cannot quietly send an empty call */
|
||||
A(p, "type", S("object")); A(p, "properties", pr); A(p, "required", rq);
|
||||
A(f, "name", S("bash")); A(f, "description", S("Run a shell command")); A(f, "parameters", p);
|
||||
A(o, "type", S("function")); A(o, "function", f);
|
||||
json_object_array_add(a, o); A(SSN, "tools", a);
|
||||
}
|
||||
|
||||
static void create(void) {
|
||||
json_object *ms, *sy; char *sb; size_t nb;
|
||||
if(SSN) json_object_put(SSN);
|
||||
SSN = json_object_new_object();
|
||||
A(SSN, "model", S(MOD));
|
||||
A(SSN, "reasoning_effort", S(RSN));
|
||||
A(SSN, "temperature", D(TEMP));
|
||||
A(SSN, "stream", B(0));
|
||||
A(SSN, "tool_choice", S("auto"));
|
||||
mkschema();
|
||||
ms = json_object_new_array(); sy = json_object_new_object();
|
||||
A(sy, "role", S("system"));
|
||||
if(SYS && *SYS) A(sy, "content", S(SYS));
|
||||
@@ -224,16 +235,28 @@ static void create(void) {
|
||||
json_object_array_add(ms, sy); A(SSN, "messages", ms);
|
||||
}
|
||||
|
||||
static int tools(json_object *tc) {
|
||||
/* ran is set to the number of commands actually executed, so the caller can see
|
||||
a round that ran nothing at all and stop a peer that keeps sending empty
|
||||
tool calls instead of looping on it forever. */
|
||||
static int tools(json_object *tc, int *ran) {
|
||||
json_object *fn, *ar, *ms, *tm; const char *id, *cs, *s; char *cmd, *out, *cb, mk[80]; int i, rc; size_t l;
|
||||
*ran = 0;
|
||||
if(!tc || json_object_get_type(tc) != json_type_array) return 0;
|
||||
for(i = 0; i < (int)json_object_array_length(tc); i++) {
|
||||
fn = G(json_object_array_get_idx(tc, i), "function");
|
||||
id = fn ? GS(json_object_array_get_idx(tc, i), "id") : 0;
|
||||
s = fn ? GS(fn, "arguments") : 0; cmd = strdup("");
|
||||
if(s) { ar = json_tokener_parse(s); cs = ar ? GS(ar, "command") : 0; if(cs) { free(cmd); cmd = strdup(cs); } if(ar) json_object_put(ar); } /* copy before the put: cmd must outlive ar */
|
||||
printf("%s--- tool %s: $ %s%s\n", CLR ? YEL : "", id ? id : "", cmd, CLR ? OF : ""); fflush(stdout);
|
||||
run(cmd, &out, &rc);
|
||||
s = fn ? GS(fn, "arguments") : 0; cmd = 0;
|
||||
if(s) { ar = json_tokener_parse(s); cs = ar ? GS(ar, "command") : 0; if(cs) cmd = strdup(cs); if(ar) json_object_put(ar); } /* copy before the put: cmd must outlive ar */
|
||||
if(cmd) { char *q = cmd; while(*q && isspace((unsigned char)*q)) q++; if(!*q) { free(cmd); cmd = 0; } } /* a blank command is no command */
|
||||
if(!cmd) {
|
||||
printf("%s--- tool %s: no command in the arguments%s\n", CLR ? YEL : "", id ? id : "", CLR ? OF : "");
|
||||
out = strdup("error: this tool call carried no usable \"command\" argument, so nothing ran. Call bash again with a command, e.g. {\"command\": \"ls -l\"}.");
|
||||
rc = 1;
|
||||
} else {
|
||||
printf("%s--- tool %s: $ %s%s\n", CLR ? YEL : "", id ? id : "", cmd, CLR ? OF : ""); fflush(stdout);
|
||||
run(cmd, &out, &rc);
|
||||
(*ran)++;
|
||||
}
|
||||
printf("%s%s%s\n", CLR ? GRY DIM : "", out, CLR ? OF : ""); fflush(stdout);
|
||||
ms = G(SSN, "messages");
|
||||
if(ms) {
|
||||
@@ -252,6 +275,7 @@ static int tools(json_object *tc) {
|
||||
|
||||
static int ask(const char *in) {
|
||||
json_object *ms, *d, *p, *ch, *m, *as, *tc, *u; const char *raw, *co, *rs, *rc2, *mn;
|
||||
int empty = 0;
|
||||
if(!(ms = G(SSN, "messages"))) return 1;
|
||||
as = json_object_new_object(); A(as, "role", S("user")); A(as, "content", S(in));
|
||||
json_object_array_add(ms, as); sv();
|
||||
@@ -282,10 +306,16 @@ static int ask(const char *in) {
|
||||
if(tc && json_object_get_type(tc) == json_type_array) A(as, "tool_calls", json_object_get(tc)); /* json_object_object_add MOVES the ref and does not refcount: take our own ref or json_object_put(p) below frees a node still living in SSN */
|
||||
json_object_array_add(ms, as); sv();
|
||||
if(tc && json_object_get_type(tc) == json_type_array && json_object_array_length(tc)) {
|
||||
int r = tools(tc); /* the usage line waits for the turn that ends the loop, so a tool run is not chopped up by stats */
|
||||
int r, ran; /* the usage line waits for the turn that ends the loop, so a tool run is not chopped up by stats */
|
||||
r = tools(tc, &ran);
|
||||
if(p) json_object_put(p);
|
||||
if(d) json_object_put(d);
|
||||
if(r) return r;
|
||||
if(ran) empty = 0;
|
||||
else if(++empty >= 3) { /* three rounds in a row with nothing to run: feed the errors back was useless, so give up rather than spin */
|
||||
puts("no command in three consecutive tool rounds, stopping the turn");
|
||||
return 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
u = p ? G(p, "usage") : 0; mn = p ? GS(p, "model") : 0;
|
||||
@@ -312,7 +342,12 @@ static const char *HELP =
|
||||
|
||||
static void setstr(char **slot, const char *v) { free(*slot); *slot = strdup(v); }
|
||||
|
||||
static int dispatch(const char *l) { /* runs one line and returns the exit status for it */
|
||||
static void rtrim(char *s) { size_t n = strlen(s); while(n && isspace((unsigned char)s[n - 1])) s[--n] = 0; }
|
||||
|
||||
/* Runs the command lines (a leading / or !) of one already right-trimmed line.
|
||||
The trim happens in dispatch(), because "/help " and "/quit " must stay
|
||||
commands rather than being read as a parameter named help or quit. */
|
||||
static int command(char *l) {
|
||||
char *k, *v, *out, mk[80]; const char *s; json_object *ms, *nm; int rc;
|
||||
if(!strcmp(l, "/help")) { puts(HELP); return 0; }
|
||||
if(!strcmp(l, "/quit")) { QUIT = 1; return 0; }
|
||||
@@ -334,19 +369,19 @@ static int dispatch(const char *l) { /* runs one line and returns the exit statu
|
||||
return 0;
|
||||
}
|
||||
if(!strncmp(l, "/api_key", 8)) {
|
||||
for(v = (char *)l + 8; *v == ' '; v++);
|
||||
for(v = l + 8; *v == ' '; v++);
|
||||
if(*v && (!strcmp(v, "null") || !strcmp(v, "none") || !strcmp(v, "off") || !strcmp(v, "-"))) { free(KEY); KEY = 0; }
|
||||
else if(*v) setstr(&KEY, v);
|
||||
if(KEY) printf("api_key = set, %d chars\n", (int)strlen(KEY)); else puts("api_key = unset");
|
||||
return 0;
|
||||
}
|
||||
if(!strncmp(l, "/endpoint", 9)) {
|
||||
for(v = (char *)l + 9; *v == ' '; v++);
|
||||
for(v = l + 9; *v == ' '; v++);
|
||||
if(*v) { setstr(&EP, v); for(char *e = EP + strlen(EP); e > EP && e[-1] == '/'; e--) *e = 0; }
|
||||
printf("endpoint = %s\n", EP); return 0;
|
||||
}
|
||||
if(*l == '!') {
|
||||
for(v = (char *)l + 1; *v == ' '; v++);
|
||||
for(v = l + 1; *v == ' '; v++);
|
||||
printf("%s--- $ %s%s\n", CLR ? YEL : "", v, CLR ? OF : "");
|
||||
run(v, &out, &rc);
|
||||
if(*out) printf("%s%s%s\n", CLR ? GRY DIM : "", out, CLR ? OF : "");
|
||||
@@ -382,7 +417,16 @@ static int dispatch(const char *l) { /* runs one line and returns the exit statu
|
||||
}
|
||||
free(k); return 0;
|
||||
}
|
||||
return ask(l);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Runs one line and returns the exit status for it. A task keeps its exact
|
||||
text, including a leading space that keeps it out of the history. */
|
||||
static int dispatch(const char *l) {
|
||||
char *cl; int rc;
|
||||
if(*l != '/' && *l != '!') return ask(l);
|
||||
cl = strdup(l); rtrim(cl); rc = command(cl); free(cl);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
@@ -413,7 +457,7 @@ int main(int argc, char **argv) {
|
||||
snprintf(SH, sizeof SH, "%s", e);
|
||||
signal(SIGINT, onint); signal(SIGPIPE, SIG_IGN);
|
||||
rl_signal_event_hook = ci_hook;
|
||||
create(); load();
|
||||
create(); load(); mkschema(); /* a restored session carries the schema of the run that saved it: rebuild it, so the timeout in the description matches the one run() enforces */
|
||||
if((e = getenv("SOLO_MODEL")) && *e) { setstr(&MOD, e); A(SSN, "model", S(e)); }
|
||||
if((e = getenv("SOLO_TEMP")) && *e) { TEMP = atof(e); A(SSN, "temperature", D(TEMP)); }
|
||||
if((e = getenv("SOLO_REASON")) && *e) { setstr(&RSN, e); A(SSN, "reasoning_effort", S(e)); }
|
||||
|
||||
Reference in New Issue
Block a user