diff --git a/include/splinter.h b/include/splinter.h index 2abddf7..bd21825 100644 --- a/include/splinter.h +++ b/include/splinter.h @@ -237,6 +237,9 @@ int sp_agent_run(struct sp_agent *, const char *user_text, int sp_builtin_register(struct sp_registry *); /* transport_openai.c -- OpenAI-compatible chat completions, via curl(1) */ +/* The turn's cancellation flag; set by the UI before a turn, cleared after. */ +void sp_openai_set_cancel(const volatile int *flag); + struct sp_transport sp_openai_transport(const char *url, const char *model, const char *api_key, const struct sp_registry *, long timeout_s); diff --git a/libsplinter.so b/libsplinter.so index 98b9e67..e0547b0 100755 Binary files a/libsplinter.so and b/libsplinter.so differ diff --git a/plugins/cli/cli.c b/plugins/cli/cli.c index cea0b7e..e99e7fa 100644 --- a/plugins/cli/cli.c +++ b/plugins/cli/cli.c @@ -77,7 +77,8 @@ cli_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out, { struct cli_cfg *c = ctx; struct sp_buf body, cmd, raw; - char path[] = "/tmp/splinter-cli-XXXXXX"; + char path[512]; + const char *tmpdir = getenv("TMPDIR"); int fd, status; ssize_t w; size_t n; @@ -92,6 +93,9 @@ cli_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out, } render(conv, c->full, &body); + if (tmpdir == NULL || *tmpdir == '\0') + tmpdir = "/var/tmp"; + snprintf(path, sizeof(path), "%s/splinter-cli-XXXXXX", tmpdir); fd = mkstemp(path); if (fd < 0) { sp_buf_printf(err, "cli: mkstemp: %s", strerror(errno)); diff --git a/plugins/cli/cli.so b/plugins/cli/cli.so index 7dc5852..4dd5f47 100755 Binary files a/plugins/cli/cli.so and b/plugins/cli/cli.so differ diff --git a/src/transport_openai.c b/src/transport_openai.c index fb14156..22a49e4 100644 --- a/src/transport_openai.c +++ b/src/transport_openai.c @@ -14,9 +14,13 @@ * not an empty success. */ #include +#include +#include #include #include #include +#include +#include #include #include "splinter.h" @@ -29,26 +33,99 @@ struct openai_ctx { long timeout_s; }; +/* + * The cancellation flag belongs to the turn, not to the transport: it is set + * when a turn starts and cleared when it ends. One agent per process is the + * whole model of this harness, so a process-wide pointer is the honest way to + * say that - and it lets the read loop kill curl mid-flight instead of waiting + * for the model to finish talking to nobody. + */ +static const volatile int *g_cancel; + +void +sp_openai_set_cancel(const volatile int *flag) +{ + g_cancel = flag; +} + +/* Read a child's output, killing it the moment the turn is cancelled. */ static int -read_all(FILE *f, struct sp_buf *out) +read_all_cancel(FILE *f, struct sp_buf *out, const volatile int *cancel, + pid_t child) { char chunk[4096]; size_t got; - while ((got = fread(chunk, 1, sizeof(chunk), f)) > 0) { + for (;;) { + if (cancel != NULL && *cancel) { + (void)kill(child, SIGKILL); + return -1; + } + got = fread(chunk, 1, sizeof(chunk), f); + if (got == 0) + break; if (sp_buf_add(out, chunk, got) != 0) return -1; } return ferror(f) ? -1 : 0; } +/* + * Run a command with its stdout on a pipe. + * + * popen() was the obvious tool and the wrong one: it hands back a stream, not a + * process id, so a cancellation could only take effect once the command had + * finished by itself - the opposite of cancelling. fork/exec keeps the pid, and + * the read loop can kill it. + */ +static FILE * +spawn_cmd(const char *cmd, pid_t *pid, struct sp_buf *err) +{ + int fds[2]; + pid_t child; + + if (pipe(fds) != 0) { + sp_buf_printf(err, "openai: pipe: %s", strerror(errno)); + return NULL; + } + child = fork(); + if (child < 0) { + sp_buf_printf(err, "openai: fork: %s", strerror(errno)); + close(fds[0]); + close(fds[1]); + return NULL; + } + if (child == 0) { + (void)dup2(fds[1], STDOUT_FILENO); + close(fds[0]); + close(fds[1]); + execl("/bin/sh", "sh", "-c", cmd, (char *)NULL); + _exit(127); + } + close(fds[1]); + *pid = child; + return fdopen(fds[0], "r"); +} + +/* Temp files carry the conversation: $TMPDIR first, never a fixed path. */ +static void +temp_template(char *dst, size_t cap, const char *name) +{ + const char *dir = getenv("TMPDIR"); + + if (dir == NULL || *dir == '\0') + dir = "/var/tmp"; + snprintf(dst, cap, "%s/splinter-%s-XXXXXX", dir, name); +} + static int openai_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out, struct sp_buf *err) { struct openai_ctx *c = ctx; struct sp_buf body, cmd, raw; - char path[] = "/tmp/splinter-openai-XXXXXX"; + char path[512]; + pid_t child = -1; int fd, rc = -1; FILE *p; ssize_t w; @@ -68,6 +145,7 @@ openai_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out, goto out; } + temp_template(path, sizeof(path), "sse"); fd = mkstemp(path); if (fd < 0) { sp_buf_printf(err, "openai: mkstemp: %s", strerror(errno)); @@ -89,22 +167,33 @@ openai_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out, sp_buf_printf(&cmd, " --data-binary @%s -w '\\n%%{http_code}' '%s'", path, c->url); - p = popen(cmd.p, "r"); + p = spawn_cmd(cmd.p, &child, err); if (p == NULL) { - sp_buf_printf(err, "openai: popen: %s", strerror(errno)); unlink(path); goto out; } - if (read_all(p, &raw) != 0) { - sp_buf_puts(err, "openai: read failed"); - pclose(p); + if (read_all_cancel(p, &raw, g_cancel, child) != 0) { + if (g_cancel != NULL && *g_cancel) { + sp_buf_puts(err, "cancelled"); + (void)kill(child, SIGKILL); + } else + sp_buf_puts(err, "openai: read failed"); + fclose(p); + (void)waitpid(child, NULL, 0); unlink(path); goto out; } - if (pclose(p) != 0) { - sp_buf_printf(err, "openai: transfer failed for %s", c->url); - unlink(path); - goto out; + { + int status_child; + + fclose(p); + if (waitpid(child, &status_child, 0) < 0 || + !WIFEXITED(status_child) || WEXITSTATUS(status_child) != 0) { + sp_buf_printf(err, "openai: transfer failed for %s", + c->url); + unlink(path); + goto out; + } } unlink(path); @@ -146,8 +235,9 @@ openai_stream(void *ctx, const struct sp_conv *conv, struct sp_reply *out, struct openai_ctx *c = ctx; struct sp_buf body, cmd, tail, delta; struct sp_sse *sse; - char path[] = "/tmp/splinter-sse-XXXXXX"; + char path[512]; FILE *p; + pid_t child = -1; int fd, rc = -1; ssize_t w; size_t n; @@ -169,6 +259,7 @@ openai_stream(void *ctx, const struct sp_conv *conv, struct sp_reply *out, goto out; } + temp_template(path, sizeof(path), "sse"); fd = mkstemp(path); if (fd < 0) { sp_buf_printf(err, "openai: mkstemp: %s", strerror(errno)); @@ -189,14 +280,36 @@ openai_stream(void *ctx, const struct sp_conv *conv, struct sp_reply *out, sp_buf_printf(&cmd, " -H 'Authorization: Bearer %s'", c->key); sp_buf_printf(&cmd, " --data-binary @%s '%s'", path, c->url); - p = popen(cmd.p, "r"); + p = spawn_cmd(cmd.p, &child, err); if (p == NULL) { - sp_buf_printf(err, "openai: popen: %s", strerror(errno)); unlink(path); goto out; } - while (fgets(line, sizeof(line), p) != NULL) { - int produced; + for (;;) { + struct pollfd pfd; + int produced, pr; + + if (g_cancel != NULL && *g_cancel) { + (void)kill(child, SIGKILL); + sp_buf_puts(err, "cancelled"); + fclose(p); + (void)waitpid(child, NULL, 0); + unlink(path); + goto out; + } + pfd.fd = fileno(p); + pfd.events = POLLIN; + pfd.revents = 0; + pr = poll(&pfd, 1, 200); + if (pr < 0) { + if (errno == EINTR) + continue; + break; + } + if (pr == 0) + continue; /* keepalive window passed */ + if (fgets(line, sizeof(line), p) == NULL) + break; if (strncmp(line, "data:", 5) == 0) saw_data = 1; @@ -205,7 +318,9 @@ openai_stream(void *ctx, const struct sp_conv *conv, struct sp_reply *out, sp_buf_clear(&delta); produced = sp_sse_line(sse, line, &delta, err); if (produced < 0) { - pclose(p); + fclose(p); + (void)kill(child, SIGKILL); + (void)waitpid(child, NULL, 0); unlink(path); goto out; } @@ -221,11 +336,17 @@ openai_stream(void *ctx, const struct sp_conv *conv, struct sp_reply *out, cb(cbctx, kind, delta.p); } } - if (pclose(p) != 0) { - sp_buf_printf(err, "openai: stream failed for %s: %.300s", - c->url, tail.p != NULL ? tail.p : ""); - unlink(path); - goto out; + { + int st; + + fclose(p); + if (waitpid(child, &st, 0) < 0 || !WIFEXITED(st) || + WEXITSTATUS(st) != 0) { + sp_buf_printf(err, "openai: stream failed for %s: %.300s", + c->url, tail.p != NULL ? tail.p : ""); + unlink(path); + goto out; + } } unlink(path); diff --git a/src/ui.c b/src/ui.c index 0ff00c6..727159d 100644 --- a/src/ui.c +++ b/src/ui.c @@ -788,7 +788,13 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, */ setlocale(LC_ALL, ""); initscr(); - cbreak(); + /* + * raw(), not cbreak(): cbreak leaves ISIG on, so the terminal driver turns + * Ctrl-C into SIGINT and kills the harness before wgetch() can report the + * key - "Ctrl-C cancels the turn" was a claim, and pressing it proved the + * opposite. raw() hands every byte to us and we decide. + */ + raw(); noecho(); keypad(stdscr, TRUE); curs_set(1); @@ -1038,11 +1044,13 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, cfg.steer_ctx = &tu; cfg.stream = stream_cb; cfg.stream_ctx = &tu; + sp_openai_set_cancel(&tu.cancel); cfg.tool_begin = tool_begin_cb; cfg.tool_begin_ctx = &tu; ag->cfg = &cfg; if (pthread_create(&th, NULL, turn_thread, &tu) != 0) { + sp_openai_set_cancel(NULL); ag->cfg = orig; snprintf((char *)input, sizeof(input), "%s", sp_t("err.thread")); @@ -1214,6 +1222,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name, * handed the follow-up turn a dangling steer callback, which * then consumed the same text twice. */ + sp_openai_set_cancel(NULL); ag->cfg = orig; (void)cancelled; rc = tu.rc; diff --git a/tests/test_ascii b/tests/test_ascii index 3e2c255..b55b2f6 100755 Binary files a/tests/test_ascii and b/tests/test_ascii differ