diff --git a/README.md b/README.md index 426ca29..135ec79 100644 --- a/README.md +++ b/README.md @@ -317,10 +317,12 @@ object across events, a truncated tail, and the keepalive lines that carry nothing. A request that fails before anything was streamed is tried again three times, -five seconds apart; a broken connection, a missing status and a 5xx qualify, a -401 or 404 does not. After the first streamed chunk the retry stops, because -printing the same answer twice is worse than reporting the provider's error. -Ctrl-C during a wait ends it at once. +five seconds apart; a broken connection, a missing status, a 5xx and a stream +that carried no answer qualify, a 401 or 404 does not. After the first streamed +chunk the retry stops, because printing the same answer twice is worse than +reporting the provider's error. The wait is visible: the prompt line says +`retrying in 5 s: ...` until the next attempt answers, so a pause does not look +like a hang. Ctrl-C during a wait ends it at once. ## Colour, measured diff --git a/include/splinter.h b/include/splinter.h index b98783e..1604070 100644 --- a/include/splinter.h +++ b/include/splinter.h @@ -326,6 +326,14 @@ struct sp_agent_config { const char *observation); void *tool_end_ctx; + /* + * A status line the operator should see while a request waits or is + * retried; "" clears it. The engine turns it into SP_EV_NOTE, so the + * screen can say "retrying in 5 s" instead of looking hung. + */ + void (*note)(void *ctx, const char *text); + void *note_ctx; + /* Called when the conversation was compacted to fit the budget. */ void (*on_compact)(void *ctx, size_t before, size_t after, size_t msgs_before, size_t msgs_after); diff --git a/plugins/junction/junction.c b/plugins/junction/junction.c index cc503f3..71c4934 100644 --- a/plugins/junction/junction.c +++ b/plugins/junction/junction.c @@ -320,27 +320,42 @@ junction_get(const char *url, struct jset *set, char **bodyp) return raw; } -/* junction's model patterns: '*' and '?' only, everything else literal */ +/* + * junction's model patterns: '*' and '?' only, everything else literal. + * Iterative Thompson-style matcher — avoids stack overflow on long patterns. + */ static int glob_match(const char *pat, const char *s) { - if (*pat == '\0') - return *s == '\0'; - if (*pat == '*') { - while (*pat == '*') + const char *star = NULL; + const char *s_backup = NULL; + + while (*s != '\0') { + if (*pat == '*' || *pat == '?') { + if (*pat == '*') { + star = pat++; + s_backup = s; + } else { + pat++; + } + continue; + } + if (*pat != *s) { + if (star != NULL) { + pat = star + 1; + s_backup++; + s = s_backup; + } else { + return 0; + } + } else { pat++; - if (*pat == '\0') - return 1; - for (; *s != '\0'; s++) - if (glob_match(pat, s)) - return 1; - return 0; + s++; + } } - if (*pat == '?') - return *s != '\0' && glob_match(pat + 1, s + 1); - if (*pat != *s) - return 0; - return glob_match(pat + 1, s + 1); + while (*pat == '*') + pat++; + return *pat == '\0'; } /* @@ -455,8 +470,9 @@ route_health(void) j.route_up++; continue; } - off += (size_t)snprintf(down + off, sizeof(down) - off, "%s%s", - off > 0 ? " " : "", p); + if (off < sizeof(down) - 1) + off += (size_t)snprintf(down + off, sizeof(down) - off, + "%s%s", off > 0 ? " " : "", p); } snprintf(j.route_down, sizeof(j.route_down), "%s", down); } @@ -666,14 +682,9 @@ compose(void) snprintf(copy, sizeof(copy), "%s", j.route); for (tok = strtok_r(copy, " ", &save); tok != NULL; tok = strtok_r(NULL, " ", &save)) { - char down[sizeof(j.route_down)], *dt, *ds = NULL; - int is_down = 0; + const struct jep *e = route_ep(tok); + int is_down = (e != NULL && e->probe_ok != 1); - snprintf(down, sizeof(down), "%s", j.route_down); - for (dt = strtok_r(down, " ", &ds); dt != NULL; - dt = strtok_r(NULL, " ", &ds)) - if (strcmp(dt, tok) == 0) - is_down = 1; off += (size_t)snprintf(marked + off, sizeof(marked) - off, "%s%s%s", off > 0 ? " " : "", tok, is_down ? "!" : ""); @@ -732,6 +743,9 @@ probe_status(const char *base, const char *path, struct jset *set) * The model this run is on: the active provider's model, else the main one. * /status' rules are matched against it, so the plugin shows the route the * conversation takes, not the route of its own /v1/models probe. + * + * j.host is set by junction_init() before the tick thread starts; it is + * never changed, so no lock is needed here. */ static void current_model(char *buf, size_t cap) diff --git a/share/locale/de.msg b/share/locale/de.msg index e5af5b5..56d3c8d 100644 --- a/share/locale/de.msg +++ b/share/locale/de.msg @@ -66,6 +66,7 @@ cli.timeout = cli: '%s' lief nach %s s in das Zeitlimit cli.failed = cli: '%s' scheiterte (exit %d): %.400s ui.prompt_mark = > ui.steer_mark = steuern> +ui.retry_note = neuer Versuch in %d s: %s ui.swarm_locked = [Schwarm gesperrt] ui.status_ctx_bar = ctx %zu/%zu %s %d%% ui.status_ctx = ctx %zu diff --git a/share/locale/en.msg b/share/locale/en.msg index 0e97043..64eef28 100644 --- a/share/locale/en.msg +++ b/share/locale/en.msg @@ -65,6 +65,7 @@ cli.timeout = cli: '%s' timed out after %s s cli.failed = cli: '%s' failed (exit %d): %.400s ui.prompt_mark = > ui.steer_mark = steer> +ui.retry_note = retrying in %d s: %s ui.swarm_locked = [swarm locked] ui.status_ctx_bar = ctx %zu/%zu %s %d%% ui.status_ctx = ctx %zu diff --git a/share/locale/pt_BR.msg b/share/locale/pt_BR.msg index 48cedd9..a8abc68 100644 --- a/share/locale/pt_BR.msg +++ b/share/locale/pt_BR.msg @@ -25,6 +25,7 @@ ui.model_empty = nenhum modelo configurado ui.model_none = nenhum resultado ui.prompt_mark = > ui.steer_mark = direcionar> +ui.retry_note = nova tentativa em %d s: %s ui.swarm_locked = [enxame bloqueado] ui.status_ctx_bar = ctx %zu/%zu %s %d%% ui.status_ctx = ctx %zu diff --git a/src/backend/agent.c b/src/backend/agent.c index 73237a0..53d5d41 100644 --- a/src/backend/agent.c +++ b/src/backend/agent.c @@ -497,8 +497,11 @@ agent_request(struct sp_agent *a, struct sp_conv *conv, struct sp_reply *reply, else sent_rc = a->tr.send(a->tr.ctx, conv, reply, err); if (sent_rc == 0 && reply->status >= 200 && - reply->status <= 299) + reply->status <= 299) { + if (a->cfg->note != NULL) + a->cfg->note(a->cfg->note_ctx, ""); return 0; + } if (sent_rc != 0) { sp_host_provider_account(a->host, a->tr.provider, a->tr.key_id, reply->status, 0, 0); @@ -533,6 +536,15 @@ agent_request(struct sp_agent *a, struct sp_conv *conv, struct sp_reply *reply, sp_logf("agent", "retry %d/%d in %dms: %s", attempt + 1, SP_NET_RETRIES, retry_wait_ms(), err->p != NULL ? err->p : "?"); + if (a->cfg->note != NULL) { + char line[300]; + + snprintf(line, sizeof(line), sp_td("ui.retry_note", + "retrying in %d s: %s"), + retry_wait_ms() / 1000, + err->p != NULL ? err->p : "no answer"); + a->cfg->note(a->cfg->note_ctx, line); + } sp_buf_clear(err); sp_reply_free(reply); if (!retry_sleep(a, retry_wait_ms())) { @@ -542,6 +554,8 @@ agent_request(struct sp_agent *a, struct sp_conv *conv, struct sp_reply *reply, } continue; } + if (a->cfg->note != NULL) + a->cfg->note(a->cfg->note_ctx, ""); return -1; } } diff --git a/src/backend/engine.c b/src/backend/engine.c index 957f7e0..acaebfa 100644 --- a/src/backend/engine.c +++ b/src/backend/engine.c @@ -630,6 +630,15 @@ engine_stream_cb(void *ctx, int kind, const char *text) ev_push(e, SP_EV_STREAM, kind, text, NULL); } +/* an agent note (a retry, a wait) goes to the front end's status line */ +static void +engine_note(void *ctx, const char *text) +{ + struct sp_engine *e = ctx; + + ev_push(e, SP_EV_NOTE, 0, text != NULL ? text : "", NULL); +} + static void engine_tool_begin(void *ctx, const char *name, const char *args) { @@ -1060,6 +1069,8 @@ sp_engine_submit(struct sp_engine *e, const char *text) e->turn_cfg.tool_begin_ctx = e; e->turn_cfg.tool_end = engine_tool_end; e->turn_cfg.tool_end_ctx = e; + e->turn_cfg.note = engine_note; + e->turn_cfg.note_ctx = e; e->turn_cfg.on_compact = engine_compact; e->turn_cfg.on_compact_ctx = e; if (e->base.limit_wait != NULL) { diff --git a/tests/test_agent.c b/tests/test_agent.c index 51be563..2b3458b 100644 --- a/tests/test_agent.c +++ b/tests/test_agent.c @@ -64,6 +64,19 @@ setup(struct sp_registry *r) (void)sp_registry_add(r, &t); } +static char note_text[256]; +static int note_seen; + +static void +test_note(void *ctx, const char *text) +{ + (void)ctx; + if (text != NULL && *text != '\0') { + snprintf(note_text, sizeof(note_text), "%s", text); + note_seen++; + } +} + static int run_turn(struct sp_mock *m, struct sp_registry *r, const char *prompt, int autonomous, size_t steps, size_t max_bytes, struct sp_buf *answer, @@ -77,6 +90,7 @@ run_turn(struct sp_mock *m, struct sp_registry *r, const char *prompt, cfg.max_steps = steps ? steps : 8; cfg.max_bytes = max_bytes; cfg.autonomous = autonomous; + cfg.note = test_note; a.reg = r; a.cfg = &cfg; a.tr = sp_mock_transport(m); @@ -197,6 +211,8 @@ main(void) sp_mock_free(&m); /* 9b. two transient failures, then the answer: the retry recovers */ + note_seen = 0; + note_text[0] = '\0'; sp_buf_clear(&err); CHECK(sp_mock_init(&m) == 0); CHECK(sp_mock_push(&m, 500, "boom") == 0); @@ -205,6 +221,9 @@ main(void) CHECK(run_turn(&m, &r, "hi", 1, 8, 0, &answer, &err, &st) == 0); CHECK(strstr(answer.p, "nach zwei fehlern") != NULL); CHECK(m.requests == 3); + /* the operator saw why it waited, and the note is cleared after it */ + CHECK(note_seen >= 1); + CHECK(strstr(note_text, "retrying in") != NULL); sp_mock_free(&m); (void)unsetenv("SPLINTER_RETRY_WAIT_MS");