anthropic 400 on repeated user turns, exit in under a second, /quit

- anthropic: two messages of the same role in a row (a failed turn asked
  again) put the second one's blocks after the closed message object; the
  API answered 400 "messages.1.role: Field required". The message now stays
  open until the role changes.
- exit took 20+ s: the mcp plugin's HTTP round trip to a server that is down
  had curl -m 60 and nothing killed it; shutdown now kills the curl in
  flight, and connects time out after 5 s. Widget threads slept a whole
  interval before seeing the stop flag; they sleep in 100 ms steps.
- /quit (/exit, /q) leaves the chat.
- the prompt mark has its space again: the catalogue trims trailing blanks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lotherkandClaude Opus 5.5 committed 2026-11-13 20:31:51 +00:00
1 parent 2c58dcfd2a
commit bddba6423b
6 files changed
+108 -17

No files matched your search

+23 -1
View File
@@ -63,6 +63,7 @@ struct server {
char cmd[SP_CONFIG_MCP_CMD];
char session[160]; /* Mcp-Session-Id, echoed back */
pid_t pid;
volatile pid_t http_pid; /* the curl of a round trip in flight */
FILE *to; /* stdio: our writes become the server's stdin */
int from; /* stdio: its stdout, read without stdio */
struct sp_buf in; /* bytes read but not yet a whole line */
@@ -349,6 +350,8 @@ http_rpc(struct server *s, const char *method, const char *params,
snprintf(data, sizeof(data), "@%s", body);
args[n++] = "-sS";
args[n++] = "--connect-timeout";
args[n++] = "5";
args[n++] = "-m";
args[n++] = tmo;
args[n++] = "-X";
@@ -375,6 +378,14 @@ http_rpc(struct server *s, const char *method, const char *params,
}
hdrs[n] = NULL;
if (stopping) {
sp_buf_puts(err, "mcp: shutting down");
(void)unlink(body);
(void)unlink(hdr);
sp_buf_free(&req);
sp_buf_free(&out);
return NULL;
}
fd = sp_curl_start(args, s->url, hdrs, 1, &pid, err);
if (fd < 0) {
(void)unlink(body);
@@ -383,6 +394,8 @@ http_rpc(struct server *s, const char *method, const char *params,
sp_buf_free(&out);
return NULL;
}
/* shutdown kills it: a server that is down must not hold the exit */
s->http_pid = pid;
{
char chunk[8192];
ssize_t got;
@@ -394,6 +407,7 @@ http_rpc(struct server *s, const char *method, const char *params,
}
close(fd);
(void)waitpid(pid, &rc, 0);
s->http_pid = 0;
take_session(s, hdrp);
(void)unlink(body);
(void)unlink(hdr);
@@ -566,6 +580,8 @@ notify(struct server *s, const char *method)
close(fd);
snprintf(data, sizeof(data), "@%s", body);
args[n++] = "-sS";
args[n++] = "--connect-timeout";
args[n++] = "5";
args[n++] = "-m";
args[n++] = HTTP_TIMEOUT_S;
args[n++] = "-X";
@@ -579,10 +595,13 @@ notify(struct server *s, const char *method)
if (s->header[0] != '\0')
hdrs[n++] = s->header;
hdrs[n] = NULL;
fd = sp_curl_start(args, s->url, hdrs,
fd = stopping ? -1 :
sp_curl_start(args, s->url, hdrs,
1, &pid, &err);
if (fd >= 0) {
s->http_pid = pid;
(void)waitpid(pid, NULL, 0);
s->http_pid = 0;
close(fd);
take_session(s, hdr);
}
@@ -1072,6 +1091,9 @@ mcp_shutdown(struct sp_host *h)
(void)waitpid(sv[i].pid, NULL, 0);
sv[i].pid = 0;
}
/* an HTTP round trip in flight: its curl goes, the thread sees EOF */
if (sv[i].http && sv[i].http_pid > 0)
(void)kill(sv[i].http_pid, SIGKILL);
sv[i].dead = 1;
}
}
+14 -7
View File
@@ -373,23 +373,30 @@ sp_anthropic_request(const struct sp_conv *conv, const char *model,
}
if (!any)
continue; /* nothing to say: no empty message */
if (prev_assistant == assistant) {
/*
* Same role as the message before (observations of a parallel
* round, or a user turn that failed and was asked again): its
* blocks join the open message. The message stays open until
* the role changes -- closing it first put the blocks outside
* any message, and the API answered 400 "messages.N.role".
*/
if (nmsg > 0 && prev_assistant == assistant) {
if (sp_buf_puts(out, ",") != 0 ||
(blocks.len > 0 && sp_buf_add(out, blocks.p,
blocks.len) != 0))
sp_buf_add(out, blocks.p, blocks.len) != 0)
goto out;
continue;
}
if (nmsg++ > 0 && sp_buf_puts(out, ",") != 0)
if (nmsg > 0 && sp_buf_puts(out, "]},") != 0)
goto out;
nmsg++;
if (sp_buf_printf(out, "{\"role\":\"%s\",\"content\":[",
assistant ? "assistant" : "user") != 0 ||
(blocks.len > 0 && sp_buf_add(out, blocks.p,
blocks.len) != 0) ||
sp_buf_puts(out, "]}") != 0)
sp_buf_add(out, blocks.p, blocks.len) != 0)
goto out;
prev_assistant = assistant;
}
if (nmsg > 0 && sp_buf_puts(out, "]}") != 0)
goto out;
if (sp_buf_puts(out, "]") != 0)
goto out;
+21 -3
View File
@@ -108,9 +108,27 @@ widget_thread(void *arg)
if (w->interval_ms == 0)
break;
ts.tv_sec = w->interval_ms / 1000;
ts.tv_nsec = (long)(w->interval_ms % 1000) * 1000000L;
nanosleep(&ts, NULL);
/*
* Sleep in short steps and look at the stop flag between them:
* one long sleep made the exit wait for a whole interval, since
* sp_host_free() joins this thread.
*/
{
unsigned long left = w->interval_ms;
int stop = 0;
while (left > 0 && !stop) {
unsigned long step = left > 100 ? 100 : left;
ts.tv_sec = 0;
ts.tv_nsec = (long)step * 1000000L;
nanosleep(&ts, NULL);
left -= step;
pthread_mutex_lock(&w->lock);
stop = w->stop;
pthread_mutex_unlock(&w->lock);
}
}
}
return NULL;
}
+28 -3
View File
@@ -1814,11 +1814,27 @@ redraw(struct ui_state *s, struct sp_agent *ag, struct sp_host *host)
doupdate();
}
/*
* The prompt mark, always followed by one space: the catalogue reader trims
* trailing blanks, so "> " in the .msg file arrives as ">".
*/
static const char *
prompt_mark(void)
{
static char buf[32];
const char *m = sp_t("ui.prompt_mark");
size_t n = strlen(m);
snprintf(buf, sizeof(buf), "%s%s", m, n > 0 && m[n - 1] == ' ' ?
"" : " ");
return buf;
}
/* the prompt line with what has been typed so far */
static void
draw_prompt(const char *input)
{
const char *mark = sp_t("ui.prompt_mark");
const char *mark = prompt_mark();
char *ascii;
size_t n, avail, cap;
int w = getmaxx(ui_input);
@@ -2356,6 +2372,12 @@ ui_slash(struct ui_state *s, struct sp_agent *ag, struct sp_host *host,
add_entry(s, "splinter", outbuf);
return 1;
}
if (strcmp(name, "quit") == 0 || strcmp(name, "exit") == 0 ||
strcmp(name, "q") == 0) {
s->quit = 1;
sp_buf_free(&out);
return 1;
}
if (strcmp(name, "usage") == 0) {
sp_usage_report(s->conf, &out);
snprintf(outbuf, sizeof(outbuf), "%s", out.p ? out.p : "");
@@ -3289,7 +3311,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *model,
draw_sidebar(&s, SESS_ID(ag), SESS_DIR(ag), host);
draw_status(&s, NULL);
werase(ui_input);
mvwaddstr(ui_input, 0, 0, sp_t("ui.prompt_mark"));
mvwaddstr(ui_input, 0, 0, prompt_mark());
wnoutrefresh(ui_input);
doupdate();
@@ -3308,8 +3330,11 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *model,
"you were working on. Do not repeat work that is "
"already done.");
if (input[0] == '/' && input[1] != '\0' &&
ui_slash(&s, ag, host, input, sizeof(input)))
ui_slash(&s, ag, host, input, sizeof(input))) {
if (s.quit)
break;
continue;
}
if (s.cc.active) {
struct sp_buf o;
+19
View File
@@ -236,6 +236,25 @@ main(void)
sp_reply_free(&rep);
sp_as_sse_free(sse);
/* two user turns in a row (the first one failed): one message, both blocks */
{
struct sp_conv c2;
struct sp_buf r2;
sp_conv_init(&c2);
CHECK(sp_buf_init(&r2) == 0);
CHECK(sp_conv_push(&c2, "user", NULL, "first") == 0);
CHECK(sp_conv_push(&c2, "user", NULL, "again") == 0);
CHECK(sp_conv_push(&c2, "assistant", NULL, "ok") == 0);
CHECK(sp_anthropic_request(&c2, "claude-x", &reg, 0, 0, &r2) == 0);
CHECK(strstr(r2.p, "\"messages\":[{\"role\":\"user\",\"content\":["
"{\"type\":\"text\",\"text\":\"first\"},{\"type\":\"text\","
"\"text\":\"again\"}]},{\"role\":\"assistant\"") != NULL);
/* no block may stand outside a message */
CHECK(strstr(r2.p, "]},{\"type\"") == NULL);
sp_buf_free(&r2);
sp_conv_free(&c2);
}
sp_buf_free(&req);
sp_buf_free(&err);
sp_buf_free(&delta);
+3 -3
View File
@@ -108,11 +108,11 @@ check grep -q 'vom gateway' "$D/out.txt"
typed 'halb'
check shows 'halb' # let the UI consume the keys
key C-l; sleep 1
check shows '^>halb'
check shows '^> halb'
key BSpace BSpace
check shows '^>ha *$' # the backspaces arrived
check shows '^> ha *$' # the backspaces arrived
key C-l; sleep 1
check shows '^>ha *$'
check shows '^> ha *$'
typed 'zeig eine tabelle'; key Enter
check shows '| a | b |'
check shows '|---|----|'