ui, model, cost and idle timeouts

- lcm: the sidebar widget and /status segment show the context, the number of
  compactions, messages and bytes of the last cut, and how many old tool
  outputs were trimmed. The counters are the compactor's own, not an estimate.
- cost: a provider-reported USD cost (usage.cost/cost_usd) is carried through
  sse/openai, the agent and the engine. /cost reports it with the cache share
  and splits judged from plain turns; the notes say that an upstream judge is
  not visible in the provider's usage. The status plugin and junction prefer
  the reported number over list-price arithmetic.
- curses: the running turn's seconds moved from the input line to the status
  bar. Notes and errors are transcript rows below the cause, never text in the
  input line or the status bar; a failed backend request is a status row and is
  retried by the agent. The steer prompt is only "steer> " and what was typed.
- model: /model probe works in the chat. The picker lists the configuration at
  once, then asks the endpoint the session is on what it serves and adds those
  models, so a router's whole menu is selectable. provider/model selectors now
  apply the right provider instead of becoming a model name. The choice is
  written to <session>.model and restored when that session is reopened; /new
  carries the current model into the new session.
- sidebar: plugin titles are bold and underlined (this curses has no italic
  attribute), with a rule between the plugin blocks.
- run/cli: a command timeout is an idle window. Every chunk on stdout or
  stderr resets it, and the process is asked with waitpid(WNOHANG) whether it
  is still there before the window is extended. A long build that keeps
  reporting progress is no longer killed for being long; a silent hang is.
- config: ';' separates settings on one line, so
  plugin lcm { keep = 6; tool_trim = 8192 } parses as two keys.
- tests: lcm widget/status counters, one-line config blocks, session model
  restore and /model probe, cli idle timeout with a talkative and a silent
  command, run output reset, and the UI status/retry rows.

make check: all tests passed.
This commit is contained in:
lotherk committed 2026-11-17 23:21:56 +00:00
1 parent dc9a1c62ec
commit af2001bfee
33 files changed
+1035 -184

No files matched your search

+67 -15
View File
@@ -254,9 +254,23 @@ ci_contains(const char *hay, const char *needle)
return 0;
}
/* the idle window in seconds; 0 or less means no limit */
static long
cli_timeout_s(const struct cli_cfg *c)
{
char *end = NULL;
long n;
if (c == NULL || c->timeout[0] == '\0')
return 0;
n = strtol(c->timeout, &end, 10);
return n > 0 ? n : 0;
}
/*
* Build the shell line: timeout, the command with placeholders expanded, the
* session arguments, stdin and stderr redirection.
* Build the shell line: the command with placeholders expanded, the session
* arguments, stdin and stderr redirection. The timeout is not a `timeout`
* wrapper here: the read loop owns it so output can move the deadline.
*/
static int
build_cmd(const struct cli_cfg *c, const char *path, const char *prompt,
@@ -273,8 +287,6 @@ build_cmd(const struct cli_cfg *c, const char *path, const char *prompt,
(sp_buf_puts(cmd, ". ") != 0 || sh_word(cmd, envfile) != 0 ||
sp_buf_puts(cmd, " && ") != 0))
return -1;
if (sp_buf_printf(cmd, "timeout %s ", c->timeout) != 0)
return -1;
while (*p != '\0') {
if (strncmp(p, "{file}", 6) == 0) {
if (sh_word(cmd, path) != 0)
@@ -315,22 +327,30 @@ build_cmd(const struct cli_cfg *c, const char *path, const char *prompt,
* turn could only end when the program decided to. `claude -p` under its
* 30-minute timeout therefore held Ctrl-C hostage: the UI set the flag, joined
* the worker, and the worker was still blocked reading a pipe. The child gets
* its own process group (the shell, `timeout`, and the program under it), and
* the read loop polls it, so a cancellation kills the whole tree.
* its own process group (the shell and the program under it), and the read
* loop polls it, so a cancellation kills the whole tree.
*
* The timeout is an idle window, not a wall clock: every byte on stdout or
* stderr moves it forward, and the process itself is asked whether it is still
* there. A long answer that keeps typing is work, not a hang.
*/
static int
run_cmd(const char *line, const char *errpath, struct sp_buf *raw,
char *errtext, size_t errcap, int *status, struct sp_buf *err)
run_cmd(const char *line, const char *errpath, long timeout_s,
struct sp_buf *raw, char *errtext, size_t errcap, int *status,
struct sp_buf *err)
{
const volatile int *cancel = sp_cancel_flag();
char chunk[4096];
FILE *ef;
int fds[2], st = 0, rc = 0;
int fds[2], st = 0, rc = 0, timed_out = 0, reaped = 0;
pid_t child;
size_t en = 0;
time_t last_out;
long errseen = 0;
sp_buf_clear(raw);
*status = 0;
last_out = time(NULL);
if (pipe(fds) != 0) {
sp_buf_printf(err, "cli: pipe: %s", strerror(errno));
return -1;
@@ -359,7 +379,9 @@ run_cmd(const char *line, const char *errpath, struct sp_buf *raw,
close(fds[1]);
for (;;) {
struct pollfd pfd;
struct stat es;
ssize_t got;
time_t now;
int pr;
if (cancel != NULL && *cancel) {
@@ -368,6 +390,18 @@ run_cmd(const char *line, const char *errpath, struct sp_buf *raw,
rc = -1;
break;
}
now = time(NULL);
/* stderr goes to a file: its growth is output too */
if (errpath[0] != '\0' && stat(errpath, &es) == 0 &&
(long)es.st_size > errseen) {
errseen = (long)es.st_size;
last_out = now;
}
if (timeout_s > 0 && now - last_out >= timeout_s) {
(void)kill(-child, SIGKILL);
timed_out = 1;
break;
}
pfd.fd = fds[0];
pfd.events = POLLIN;
pfd.revents = 0;
@@ -402,10 +436,28 @@ run_cmd(const char *line, const char *errpath, struct sp_buf *raw,
rc = -1;
break;
}
/*
* Output keeps the program alive only if the program is still
* there: a closed child can leave bytes in the pipe.
*/
if (!reaped) {
pid_t r;
do {
r = waitpid(child, &st, WNOHANG);
} while (r < 0 && errno == EINTR);
if (r == child)
reaped = 1;
}
if (!reaped)
last_out = time(NULL);
}
close(fds[0]);
(void)waitpid(child, &st, 0);
*status = st;
if (!reaped)
while (waitpid(child, &st, 0) < 0 && errno == EINTR)
;
/* the caller reports 124 as the cli's own timeout */
*status = timed_out ? (124 << 8) : st;
ef = fopen(errpath, "r");
if (ef != NULL) {
en = fread(errtext, 1, errcap - 1, ef);
@@ -532,8 +584,8 @@ cli_send_one(struct cli_cfg *c, const struct sp_conv *conv,
}
if (build_cmd(c, path, body.p != NULL ? body.p : "", key, uuid, extra,
errpath, envfile, &cmd) != 0 ||
run_cmd(cmd.p, errpath, &raw, errtext, sizeof(errtext), &status,
err) != 0) {
run_cmd(cmd.p, errpath, cli_timeout_s(c), &raw, errtext,
sizeof(errtext), &status, err) != 0) {
unlink(path);
goto fail;
}
@@ -550,8 +602,8 @@ cli_send_one(struct cli_cfg *c, const struct sp_conv *conv,
resume ? "--resume" : "--session-id", uuid);
if (build_cmd(c, path, body.p != NULL ? body.p : "", key, uuid,
extra, errpath, envfile, &cmd) != 0 ||
run_cmd(cmd.p, errpath, &raw, errtext, sizeof(errtext),
&status, err) != 0) {
run_cmd(cmd.p, errpath, cli_timeout_s(c), &raw,
errtext, sizeof(errtext), &status, err) != 0) {
unlink(path);
goto fail;
}
+3
View File
@@ -979,6 +979,9 @@ snapshot_text(struct sp_buf *out)
sp_buf_printf(out, " models: %u\n", j.models);
sp_buf_printf(out, " model: %s\n", j.model[0] != '\0' ? j.model :
"(unknown)");
if (strstr(j.model, "judged") != NULL)
sp_buf_puts(out, " judged: yes (judge calls are upstream; the "
"provider's usage carries only the final answer)\n");
sp_buf_printf(out, " serving: %s\n", j.served[0] != '\0' ? j.served :
"(no request yet)");
sp_buf_printf(out, " route: %s\n", j.route[0] != '\0' ? j.route :
+107 -1
View File
@@ -16,6 +16,7 @@
* writes the summary) is a different plugin, not a flag on this one.
*/
#include <ctype.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -31,6 +32,14 @@
static size_t keep_tail = KEEP_TAIL;
static size_t tool_trim = TOOL_TRIM;
/* what the widget and the status segment show; written by the compactor */
static pthread_mutex_t lcm_lock = PTHREAD_MUTEX_INITIALIZER;
static unsigned compactions;
static size_t dropped_total;
static unsigned trimmed_total;
static size_t last_msgs_from, last_msgs_to;
static size_t last_bytes_from, last_bytes_to;
static int
is_role(const struct sp_msg *m, const char *role)
{
@@ -111,6 +120,7 @@ lcm_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
unsigned turn)
{
size_t bytes, i, drop, first, keep_from;
size_t msgs_before, bytes_before;
char preview[PREVIEW + 1];
char summary[512];
size_t n_user = 0, n_asst = 0, n_tool = 0;
@@ -126,6 +136,8 @@ lcm_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
if (budget == 0)
return 0; /* no byte budget: nothing to do */
bytes = sp_conv_bytes(conv);
msgs_before = conv->n;
bytes_before = bytes;
if (bytes <= budget)
return 0; /* fits: nothing to do */
@@ -147,9 +159,20 @@ lcm_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
*/
trimmed = trim_tools(conv, first, keep_from, tool_trim);
if (trimmed > 0) {
pthread_mutex_lock(&lcm_lock);
trimmed_total += (unsigned)trimmed;
pthread_mutex_unlock(&lcm_lock);
bytes = sp_conv_bytes(conv);
if (bytes <= budget)
if (bytes <= budget) {
pthread_mutex_lock(&lcm_lock);
compactions++;
dropped_total += 0;
last_msgs_from = last_msgs_to = conv->n;
last_bytes_from = bytes_before;
last_bytes_to = bytes;
pthread_mutex_unlock(&lcm_lock);
return 1;
}
}
/*
* The newest user message is the task being answered. A long tool loop
@@ -222,6 +245,16 @@ lcm_compact(struct sp_host *h, struct sp_conv *conv, size_t budget,
if (sp_conv_insert(conv, first + 1, &m) != 0)
goto fail;
}
{
pthread_mutex_lock(&lcm_lock);
compactions++;
dropped_total += drop;
last_msgs_from = msgs_before;
last_msgs_to = conv->n;
last_bytes_from = bytes_before;
last_bytes_to = sp_conv_bytes(conv);
pthread_mutex_unlock(&lcm_lock);
}
free(pin_content);
free(pin_name);
return 1;
@@ -319,6 +352,70 @@ static const struct sp_tool recall_tool = {
static const struct sp_compactor lcm = { "lcm", lcm_compact };
/* "12.3k" for a token count, "842" below a thousand */
static void
human(size_t n, char *buf, size_t cap)
{
if (n >= 1000000)
snprintf(buf, cap, "%.1fM", (double)n / 1e6);
else if (n >= 1000)
snprintf(buf, cap, "%.1fk", (double)n / 1e3);
else
snprintf(buf, cap, "%zu", n);
}
/*
* What compaction did, in the sidebar: the context the last request carried,
* how often lcm cut, and how the last cut looked. The counters are the
* compactor's own, not a second estimate.
*/
static int
lcm_tick(struct sp_host *h, const char *name, void *ctx)
{
struct sp_meter m;
char used[24], max[24], text[512];
unsigned c;
size_t dropped, from, to;
unsigned trimmed;
size_t bf, bt;
(void)ctx;
sp_host_meter_get(h, &m);
pthread_mutex_lock(&lcm_lock);
c = compactions;
dropped = dropped_total;
trimmed = trimmed_total;
from = last_msgs_from;
to = last_msgs_to;
bf = last_bytes_from;
bt = last_bytes_to;
pthread_mutex_unlock(&lcm_lock);
human(m.ctx_used, used, sizeof(used));
if (m.ctx_max > 0)
human(m.ctx_max, max, sizeof(max));
else
snprintf(max, sizeof(max), "?");
snprintf(text, sizeof(text),
"ctx %s/%s\n"
"compacted %u cut %zu msgs\n"
"last %zu->%zu msgs %zu->%zu B\n"
"tools trimmed %u",
used, max, c, dropped, from, to, bf, bt, trimmed);
return sp_host_widget_text(h, name, text);
}
static void
lcm_status(struct sp_host *h, char *buf, size_t len)
{
unsigned c;
(void)h;
pthread_mutex_lock(&lcm_lock);
c = compactions;
pthread_mutex_unlock(&lcm_lock);
snprintf(buf, len, "compact %u", c);
}
static int
lcm_init(struct sp_host *h)
{
@@ -331,9 +428,18 @@ lcm_init(struct sp_host *h)
v = sp_host_plugin_config(h, "lcm", "tool_trim");
if (v != NULL && atoi(v) >= 0)
tool_trim = (size_t)atoi(v);
static const struct sp_widget w = {
"lcm", lcm_tick, NULL, NULL, 2000
};
static const struct sp_status seg = { "compact", lcm_status };
rt.ctx = h;
if (sp_host_add_compactor(h, &lcm) != 0)
return -1;
if (sp_host_add_widget(h, &w) != 0)
return -1;
if (sp_host_add_status(h, &seg) != 0)
return -1;
return sp_host_add_tool(h, &rt);
}
+11 -7
View File
@@ -9,8 +9,9 @@
* clock: 17:20:01 | ctx 12.3k/32k [|||||||...] | 41 tok/s | 1.2k tok | $0.0042
*
* The segments go to sp_host_add_status(): /status shows them in the
* transcript and the Telegram gateway sends them with its replies. The chat has
* no status bar - the prompt carries the running time and the tool.
* transcript and the Telegram gateway sends them with its replies. The chat's
* status bar draws its own meters (context, cache, speed, the running turn);
* the segments are not squeezed onto that line.
*
* The numbers come from the harness (struct sp_meter), never from the plugin
* counting for itself: two counters that disagree are worse than none.
@@ -125,15 +126,18 @@ render_cost(struct sp_host *h, char *buf, size_t len)
const struct sp_meter *m = &mm;
sp_host_meter_get(h, &mm);
/*
* A provider that reports a cost is believed over our own arithmetic:
* a router may bill differently than its list price.
*/
if (m->cost > 0) {
snprintf(buf, len, "$%.4f", m->cost);
return;
}
if (price_in == 0 && price_out == 0) {
buf[0] = '\0';
return;
}
/*
* The price belongs to the model, the tokens belong to the harness: the
* product is computed here so a price change needs no rebuild of anything
* else.
*/
snprintf(buf, len, "$%.4f",
(double)m->tok_in * price_in / 1e6 +
(double)m->tok_out * price_out / 1e6);