diff --git a/README.md b/README.md index 844b89e..1e93076 100644 --- a/README.md +++ b/README.md @@ -245,18 +245,27 @@ over with a claim that it works. ## What you see, and what is on disk The chat draws a transcript: `[time] `, `[time] ~ denkt:` for the model's -reasoning, `[time] run command: ...` for a tool call, `[time] -` for what the tool returned, `[time] ` for the answer. A turn -that calls tools therefore reads in order: reasoning, text before the call, -the call, the output, text after it. +reasoning, `[time] >> Run du -sh ../xsrc` for a tool call, `[time] | output` +for what the tool returned, `[time] ` for the answer. A turn that calls +tools therefore reads in order: reasoning, text before the call, the call, the +output, text after it. + +A call is one row and it says what happened in the words a person would use: +the tool as a capitalized verb, then its one interesting argument - the command +of a `run`, the path of a `read`, the pattern of a `search`. No JSON, no +`command:` in front of the value, and no second copy of the arguments in the +prompt: the model's encoding is not the reader's business. Call rows are painted +in the accent colour, their output dim underneath, so a run of shell calls is a +list of calls and not a grey column of the same word. The same records go to `/logs//session.log`, in the order they happened: the user's words, the model's answer, its reasoning, every call with its arguments, every observation, failures and token counts. `wire.jsonl` next to it keeps the request bodies and replies, raw. The log is -the complete record - the screen keeps the head of a long observation (4 KiB, -with the number of lines left out) so a 300 KB JSON dump does not bury the -conversation; the log has all of it. +the complete record - the screen keeps sixteen rows and 4 KiB of a long +observation, whichever comes first, and says how many lines are left out, so an +`ls -la` or a 300 KB JSON dump does not bury the conversation; the log has all +of it. ## Input history @@ -266,16 +275,15 @@ in `/history`, so the next session starts with it. ## Steering while a turn runs The turn runs on its own thread, so the keyboard stays live. Every tool call is -written into the transcript with its name and arguments - `werkzeug: run -{"command":"echo a"}` - because "it did something" is not information. The -reasoning is inserted before those lines: it came first, and the log should read -in the order things happened. The live area has -a fixed height (reasoning 8 rows, answer 5, tool line 1): a region that grows -with the text moves everything else around it, and text that jumps around the -screen is unreadable. While it runs, the -input line shows `running Ns | Enter steers, Ctrl-C cancels`. A steer is picked -up at the next step boundary; in a single-step turn it becomes the next message -instead of being dropped. Curses is only ever touched by the UI thread. +one row in the transcript, in the words of the call itself - `>> Run echo a` - +because "it did something" is not information. The reasoning is inserted before +those rows: it came first, and the log should read in the order things +happened. While it runs, the input line leads with the elapsed seconds and the +call in progress (`12s Run du -sh ../xsrc`) and then the steer prompt, which +keeps its room when the row is narrow - a call that pushes `steer>` off the +right edge hides the one hint the row exists for. A steer is picked up at the +next step boundary; in a single-step turn it becomes the next message instead of +being dropped. Curses is only ever touched by the UI thread. ## Compaction @@ -589,8 +597,9 @@ the prompt. - `echo` (read), `clock` (read). Mutate and destructive tools ask first, unless `--autonomous` is set. In the -chat the question sits where the prompt is - `run (destructive) touch x? y/N` - -and the turn waits on it; `n` or Esc denies, and the model is told +chat the question sits where the prompt is, worded like the transcript row - +`Run touch x (destructive)? y/N` - and the turn waits on it; `n` or Esc +denies, and the model is told `error: denied by operator`. This used to be dead code in the TUI: the prompt read stdin behind curses' back, so with `--autonomous` off every such tool was silently denied. diff --git a/share/locale/de.msg b/share/locale/de.msg index 5cf231e..6f67c37 100644 --- a/share/locale/de.msg +++ b/share/locale/de.msg @@ -47,7 +47,7 @@ cli.failed = cli: '%s' scheiterte (exit %d): %.400s ui.prompt_mark = > ui.steer_mark = steuern> ui.swarm_locked = [Schwarm gesperrt] -ui.approve = %s ausfuehren (%s) %.60s? y/N +ui.approve = %s (%s)? y/N auth.mutate = aendert Dateien auth.destructive = zerstoerend role.diff = aenderung diff --git a/share/locale/en.msg b/share/locale/en.msg index fa58512..43e956e 100644 --- a/share/locale/en.msg +++ b/share/locale/en.msg @@ -46,7 +46,7 @@ cli.failed = cli: '%s' failed (exit %d): %.400s ui.prompt_mark = > ui.steer_mark = steer> ui.swarm_locked = [swarm locked] -ui.approve = run %s (%s) %.60s? y/N +ui.approve = %s (%s)? y/N auth.mutate = changes files auth.destructive = destructive role.diff = diff diff --git a/share/locale/pt_BR.msg b/share/locale/pt_BR.msg index 1d31ce2..586a219 100644 --- a/share/locale/pt_BR.msg +++ b/share/locale/pt_BR.msg @@ -11,7 +11,7 @@ ui.steering_queued = direcionamento na fila ui.prompt_mark = > ui.steer_mark = direcionar> ui.swarm_locked = [enxame bloqueado] -ui.approve = executar %s (%s) %.60s? y/N +ui.approve = %s (%s)? y/N sidebar.session = sessao sidebar.cwd = diretorio diff --git a/src/ui.c b/src/ui.c index 11e0c48..bc526f8 100644 --- a/src/ui.c +++ b/src/ui.c @@ -29,7 +29,6 @@ * * Keys: Enter sends, Ctrl-L redraws, Ctrl-C drops the input line, Ctrl-D quits. */ -#include #include #include #include @@ -43,6 +42,7 @@ #include #include "splinter.h" +#include "mcp.h" #include "i18n.h" #include "log.h" #include "cmdscript.h" @@ -250,18 +250,21 @@ approve_cb(void *ctx, const char *name, int auth, const char *args) /* * What the tool returned, into the transcript. A full `run` can carry a * 300 KB JSON dump, and printing that would bury the conversation, so the - * entry keeps the head of it and says how much is left. The whole text is in - * the session log either way - the log gets what the harness saw, not what - * fits on screen. + * entry keeps the head of it and says how much is left. Two limits, both + * about the screen: sixteen rows and four kilobytes - a row limit alone lets + * one enormous line through, a byte limit alone lets fifty short lines cover + * the whole screen. The whole text is in the session log either way: the log + * gets what the harness saw, not what fits on screen. */ -#define OBS_SHOW 4096 /* bytes of an observation the transcript keeps */ +#define OBS_ROWS 16 /* rows of an observation the transcript keeps */ +#define OBS_BYTES 4096 /* and its bytes */ static void tool_end_cb(void *ctx, const char *name, const char *obs) { struct turn *t = ctx; char *shown; - size_t len, keep, i, rest = 0; + size_t len, keep, rows, i, rest = 0; (void)name; if (obs == NULL || *obs == '\0') @@ -273,19 +276,53 @@ tool_end_cb(void *ctx, const char *name, const char *obs) return; while (len > 0 && (obs[len - 1] == '\n' || obs[len - 1] == '\r')) len--; - if (len <= OBS_SHOW) { + /* + * A call that said nothing but how it ended belongs on the call row - + * + * >> Run mkdir -p build [exit status 0] + * + * instead of two rows for every quiet command. Output that has + * something to say keeps its own block underneath. + */ + if (len >= 14 && len <= 32 && strncmp(obs, "[exit status ", 13) == 0 && + obs[len - 1] == ']' && memchr(obs, '\n', len) == NULL) { + char line[64]; + + snprintf(line, sizeof(line), "%.*s", (int)len, obs); + pthread_mutex_lock(&t->lock); + (void)ev_push(t, 's', line); + pthread_mutex_unlock(&t->lock); + return; + } + /* cut at the row that ends the visible block, not in the middle of one */ + keep = len; + for (rows = 0, i = 0; i < len; i++) { + if (i >= OBS_BYTES || rows >= OBS_ROWS) { + keep = i; + break; + } + if (obs[i] == '\n') + rows++; + } + if (keep >= len) { shown = strdup(obs); if (shown == NULL) return; } else { - keep = OBS_SHOW; - while (keep > 0 && obs[keep - 1] != '\n') - keep--; - if (keep == 0) - keep = OBS_SHOW; + /* a cut in the middle of a line is the byte limit talking */ + if (obs[keep] != '\n') { + size_t cut = keep; + + while (cut > 0 && obs[cut - 1] != '\n') + cut--; + if (cut > 0) + keep = cut; + } for (i = keep; i < len; i++) if (obs[i] == '\n') rest++; + if (obs[len - 1] != '\n') + rest++; shown = malloc(keep + 128); if (shown == NULL) return; @@ -405,6 +442,101 @@ pretty_args(const char *json, char *out, size_t cap) out[n < cap ? n : cap - 1] = '\0'; } +/* + * The argument that says what a call is about, per tool: a shell call is its + * command, a read is its path. Once the verb has said "Run", the key in front + * of the value ("command: du -sh") only repeats the harness to itself. + */ +static const char * +primary_key(const char *name) +{ + static const struct { + const char *tool, *key; + } keys[] = { + { "run", "command" }, + { "read", "path" }, + { "write", "path" }, + { "edit", "path" }, + { "list", "path" }, + { "search", "pattern" }, + { "websearch", "query" }, + { "webfetch", "url" }, + { "echo", "text" }, + { "delegate", "task" }, + }; + size_t i; + + for (i = 0; i < sizeof(keys) / sizeof(keys[0]); i++) + if (strcmp(name, keys[i].tool) == 0) + return keys[i].key; + return NULL; +} + +/* + * A tool call as a person reads it: + * + * Run du -sh ../xsrc + * Read src/ui.c + * Search sp_agent_run + * + * The verb is the tool, capitalized; behind it the one argument that matters. + * No JSON and no key names - the model's encoding is not the reader's + * business. The same line goes to the transcript, the prompt and the question + * that asks permission, so all three name the call in the same words. + */ +static void +call_summary(const char *name, const char *args, char *out, size_t cap) +{ + char detail[512] = ""; + const char *key; + size_t n; + + /* + * The value of the primary key, read out of the object instead of off + * the front of the pretty text: the model may order its arguments as it + * likes, and a "write" carries the whole file content next to the path. + * Everything else in the call is noise on one row. + */ + key = primary_key(name); + if (key != NULL && args != NULL && args[0] == '{') { + mcp_json *v = mcp_parse(args); + + if (v != NULL) { + const char *val = mcp_arg_string(v, key); + + if (val != NULL) + snprintf(detail, sizeof(detail), "%s", val); + mcp_json_free(v); + } + } + if (detail[0] == '\0') + pretty_args(args != NULL ? args : "", detail, sizeof(detail)); + snprintf(out, cap, "%s", name); + if (out[0] >= 'a' && out[0] <= 'z') + out[0] = (char)(out[0] - 'a' + 'A'); + n = strlen(out); + if (*detail != '\0' && n + 2 < cap) + snprintf(out + n, cap - n, " %s", detail); +} + +/* + * Cut a line for the screen and mark the cut. On a character boundary: the + * text is UTF-8, and half a sequence draws as garbage. + */ +static void +cut_line(char *s, size_t want) +{ + size_t n = strlen(s), cut = want; + + if (n <= want) + return; + while (cut > 0 && ((unsigned char)s[cut] & 0xc0) == 0x80) + cut--; + s[cut] = '\0'; + if (cut >= 3) + memcpy(s + cut - 3, "...", 3); +} + /* queue one event for the UI; the caller holds t->lock */ static int ev_push(struct turn *t, int kind, const char *text) @@ -440,14 +572,18 @@ static void tool_begin_cb(void *ctx, const char *name, const char *args) { struct turn *t = ctx; - const char *a = args != NULL ? args : ""; - char shown[512]; + char call[sizeof(t->tool_line)]; if (name == NULL) return; - pretty_args(a, shown, sizeof(shown)); + call_summary(name, args, call, sizeof(call)); pthread_mutex_lock(&t->lock); - snprintf(t->tool_line, sizeof(t->tool_line), "%s %s", name, a); + /* + * The prompt leads with the same words. It used to be "name {json}" - + * the model's own encoding, cut at forty bytes in the middle of an + * escape, which is what made the line unreadable. + */ + snprintf(t->tool_line, sizeof(t->tool_line), "%s", call); /* * The step is over as far as the screen is concerned: its reasoning and * its text go into the queue in that order, then the call itself, and @@ -457,27 +593,9 @@ tool_begin_cb(void *ctx, const char *name, const char *args) (void)ev_push(t, 'a', t->live.p); sp_buf_clear(&t->think); sp_buf_clear(&t->live); - { - /* - * ">> Run du -sh ../xsrc" reads like the thing that happened. The - * argument of a shell call is the command; saying "command:" before - * it, or pasting the JSON, is the harness talking about itself. - */ - char call[640], verb[64]; - const char *detail = shown; - size_t n; - - snprintf(verb, sizeof(verb), "%s", name); - verb[0] = (char)toupper((unsigned char)verb[0]); - if (strcmp(name, "run") == 0 && strncmp(detail, "command: ", 9) == 0) - detail += 9; - n = strlen(detail); - if (n > 200) - snprintf(call, sizeof(call), "%s %.200s...", verb, detail); - else - snprintf(call, sizeof(call), "%s %s", verb, detail); - (void)ev_push(t, 'x', call); - } + /* a call row is one row: two hundred columns is already more than fits */ + cut_line(call, 200); + (void)ev_push(t, 'x', call); pthread_mutex_unlock(&t->lock); } @@ -1294,6 +1412,13 @@ draw_history(struct ui_state *s) wattrset(ui_hist, COLOR_PAIR(PAIR_MAIN)); else if (strcmp(e->role, "error") == 0) wattrset(ui_hist, COLOR_PAIR(PAIR_ERROR) | A_BOLD); + /* + * A call is the interesting line of a tool step: what the tool + * returned is reference material under it. Painted with it, the + * transcript read as one grey wall of "Run". + */ + else if (strcmp(e->role, "tool") == 0) + wattrset(ui_hist, COLOR_PAIR(PAIR_ACCENT)); else wattrset(ui_hist, COLOR_PAIR(PAIR_DIM)); if (blank[idx]) { @@ -1499,14 +1624,14 @@ draw_sidebar(struct ui_state *s, const char *id, const char *dir, } } - /* the last turn, in one line */ + /* the last turn, in one line - two where the column is narrow */ if (s->last_ms > 0 && y < getmaxy(ui_side) - 2) { char line[160]; sidebar_heading(&y, cols, sp_t("sidebar.last_turn")); snprintf(line, sizeof(line), sp_t("sidebar.turn_line"), (double)s->last_ms / 1000.0, s->last_steps, s->last_calls); - side_text(&y, cols, line, 1); + side_text(&y, cols, line, 2); if (s->tokens_out > 0) { snprintf(line, sizeof(line), sp_t("sidebar.tokens"), s->tokens_out, s->tokens_in); @@ -2866,11 +2991,34 @@ turn_drain(struct ui_state *s, struct turn *tu, struct turn_view *v) tu->c_msgs_before = tu->c_msgs_after = 0; pthread_mutex_unlock(&tu->lock); for (k = 0; k < nev; k++) { - const char *role = ev[k].kind == 'x' ? "tool" : + const char *role; + + /* + * A one-line result ("[exit status 0]") joins the call row it + * belongs to; the call row is the entry just before it, because + * the queue keeps the order the turn produced. + */ + if (ev[k].kind == 's') { + if (s->n > 0) { + struct entry *e = &s->entries[s->n - 1]; + size_t need = strlen(e->text) + + strlen(ev[k].text) + 3; + char *joined = malloc(need); + + if (joined != NULL) { + snprintf(joined, need, "%s %s", e->text, + ev[k].text); + entry_set_text(s, e->id, joined); + free(joined); + } + } + free(ev[k].text); + continue; + } + role = ev[k].kind == 'x' ? "tool" : ev[k].kind == 'o' ? "observation" : ev[k].kind == 'n' ? "splinter" : ev[k].kind == 't' ? "thinking" : "agent"; - /* the log gets what the screen gets, in the same order */ if (ev[k].kind == 't') sp_logf("thinking", "%s", ev[k].text); @@ -2927,11 +3075,35 @@ turn_approval(struct turn *tu, int c) */ if (!pending || c == 3 || c == 4) return 0; - werase(ui_input); - mvwprintw(ui_input, 0, 0, sp_t("ui.approve"), nm, - auth == SP_AUTH_MUTATE ? sp_t("auth.mutate") : - sp_t("auth.destructive"), ar); - wrefresh(ui_input); + /* + * The question names the call the way the transcript does - "Run touch + * /tmp/x" - and then how far it reaches. It used to read "run run + * (destructive) {"command": "..."}", with the verb twice and the raw + * arguments in it, which is not a question anybody can answer at a + * glance. + */ + { + char call[640], label[64], line[768]; + size_t over, n; + int w = getmaxx(ui_input) - 1; + + call_summary(nm, ar, call, sizeof(call)); + snprintf(label, sizeof(label), "%s", + auth == SP_AUTH_MUTATE ? sp_t("auth.mutate") : + sp_t("auth.destructive")); + snprintf(line, sizeof(line), sp_t("ui.approve"), call, label); + if ((int)strlen(line) > w) { + /* the call gives way; the class and "y/N" never do */ + over = strlen(line) - (size_t)w; + n = strlen(call); + cut_line(call, n > over + 4 ? n - over - 4 : 0); + snprintf(line, sizeof(line), sp_t("ui.approve"), call, + label); + } + werase(ui_input); + mvwaddnstr(ui_input, 0, 0, line, w); + wrefresh(ui_input); + } if (c == 'y' || c == 'Y' || c == 'n' || c == 'N' || c == 27) { pthread_mutex_lock(&tu->lock); tu->approve_result = (c == 'y' || c == 'Y'); @@ -2992,21 +3164,25 @@ turn_paint(struct ui_state *s, struct sp_agent *ag, struct sp_host *host, werase(ui_input); w = getmaxx(ui_input) - 1; { - /* no status bar: the running time and the tool lead the input */ - char mark[160], tool[64] = ""; + /* + * No status bar: the running time and the call lead the input. + * The steer prompt keeps its room and the call gives way - a tool + * line that pushes "steer>" off the right edge hides the one hint + * the row exists for. + */ + char lead[600], mark[700], steer[32]; + size_t room; - pthread_mutex_lock(&tu->lock); - if (tu->tool_line[0] != '\0') - snprintf(tool, sizeof(tool), " %.40s", tu->tool_line); - pthread_mutex_unlock(&tu->lock); - snprintf(mark, sizeof(mark), "%lds%s %s", ms / 1000, tool, + snprintf(steer, sizeof(steer), "%s", sp_td("ui.steer_mark", "steer> ")); - { - size_t ml = strlen(mark); - - if (ml > 0 && mark[ml - 1] != ' ' && ml + 1 < sizeof(mark)) - snprintf(mark + ml, sizeof(mark) - ml, " "); - } + pthread_mutex_lock(&tu->lock); + snprintf(lead, sizeof(lead), "%lds%s%s", ms / 1000, + tu->tool_line[0] != '\0' ? " " : "", tu->tool_line); + pthread_mutex_unlock(&tu->lock); + room = w > (int)strlen(steer) + 2 ? + (size_t)w - strlen(steer) - 2 : 0; + cut_line(lead, room); + snprintf(mark, sizeof(mark), "%s %s", lead, steer); mvwaddnstr(ui_input, 0, 0, mark, w); used = (int)strlen(mark); } diff --git a/tests/test_ui.sh b/tests/test_ui.sh index 079a882..37153be 100755 --- a/tests/test_ui.sh +++ b/tests/test_ui.sh @@ -47,6 +47,13 @@ shows() { # wait until the screen contains $1 done return 1 } +not_shows() { # the screen must never show $1 - the model's JSON is not the reader's business + if tmux capture-pane -t $S -p | grep -q -- "$1"; then + echo "screen shows: $(tmux capture-pane -t $S -p | grep -- "$1" | head -1)" + return 1 + fi + return 0 +} done_turn() { # wait until the turn label is gone for i in $(seq 1 60); do tmux capture-pane -t $S -p | grep -q 'working' || return 0 @@ -102,8 +109,13 @@ typed '/connect'; key Enter check shows 'No configuration directory to write to.' typed 'schreib die datei'; key Enter check shows 'changes files' +# the question names the call - verb, path, class - and never the arguments +# as the model sent them ("Write /tmp/x/out.txt", not write {"path": ...}) +check shows 'Write ' +check not_shows '{"path"' key y check shows 'fertig' +check shows '>> Write ' check grep -q 'vom gateway' "$D/out.txt" typed 'halb' check shows 'halb' # let the UI consume the keys