From fac5cffc6f496bd67069c8f0c2873060ee6d194d Mon Sep 17 00:00:00 2001 From: lotherk Date: Wed, 11 Nov 2026 19:04:23 +0000 Subject: [PATCH] ui: keep the text the model streams before a tool call; test: gateway turn waits for the model switch The final answer replaced the whole streamed entry, so 'I will read the file' vanished. The fake Telegram sent the follow-up turn together with the model button, so the turn could be read first (flaky test). Co-Authored-By: Claude Sonnet 5.5 --- src/ui.c | 22 +++++++++++++++++++--- tests/fake_telegram.py | 4 +++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/ui.c b/src/ui.c index a4b9273..23a5780 100644 --- a/src/ui.c +++ b/src/ui.c @@ -222,6 +222,9 @@ tool_begin_cb(void *ctx, const char *name, const char *args) snprintf(t->tools_done[t->ntools_done++], sizeof(t->tools_done[0]), "%s %s", name, a); /* Flow inline in the thinking stream instead of a separate zone below. */ + /* the text before a tool call and the text after it are two paragraphs */ + if (t->live.len > 0 && t->live.p[t->live.len - 1] != '\n') + (void)sp_buf_puts(&t->live, "\n\n"); if (t->flow.len > 0 && t->flow.p[t->flow.len - 1] != '\n') (void)sp_buf_puts(&t->flow, "\n"); snprintf(mark, sizeof(mark), "\n› %s %.480s\n\n", name, a); @@ -2612,9 +2615,22 @@ ui_turn(struct ui_state *s, struct sp_agent *ag, struct sp_host *host, if (rc == 0) { const char *ap = answer.p != NULL ? answer.p : ""; - if (v.ans_id != 0) - entry_set_text(s, v.ans_id, ap); - else + /* + * The answer the agent returns is the last step's text. What the + * model said before its tool calls ("I'll read the file") streamed + * into the entry too; replacing the entry with the final text + * would take it away. Keep the streamed text when it ends in the + * final answer. + */ + if (v.ans_id != 0) { + size_t lv = tu.live.len, la = strlen(ap); + + if (la > 0 && lv >= la && tu.live.p != NULL && + memcmp(tu.live.p + lv - la, ap, la) == 0) + entry_set_text(s, v.ans_id, tu.live.p); + else + entry_set_text(s, v.ans_id, ap); + } else add_entry(s, "agent", ap); s->total_out_chars += strlen(ap); s->out_chars = strlen(ap); diff --git a/tests/fake_telegram.py b/tests/fake_telegram.py index 39bbfae..b5898d3 100644 --- a/tests/fake_telegram.py +++ b/tests/fake_telegram.py @@ -112,7 +112,9 @@ class H(http.server.BaseHTTPRequestHandler): # run with SPLINTER_LOCALE=de) if "Waehle ein Modell" in text: add_callback("model:far") - # and a turn afterwards: it has to go to the new endpoint + if text.startswith("Modell: other (Provider far"): + # only once the switch is confirmed: sent together with the + # button, the turn could be read before the button was add_update("hallo") # /connect: one answer per question if "Neuer Provider" in text: