splinter: Ctrl-C actually cancels, and no fixed /tmp
Pressing Ctrl-C killed the harness. cbreak() leaves ISIG on, so the terminal driver turned the key into SIGINT and the process died before wgetch() could return 3 - the 'Ctrl-C cancels' line was a claim, and one keystroke disproved it. raw() hands every byte to the program. Cancelling a turn also only took effect between steps, so a request in flight kept running to completion. The transport used popen(), which returns a stream and not a process id, so there was nothing to kill. It now forks and execs, keeps the pid, and both the read loop and the streaming loop poll with a 200 ms timeout and SIGKILL the child when the turn is cancelled. Measured: Ctrl-C during a streaming request returns to the prompt in 510 ms and the UI survives. Temp files no longer use a fixed /tmp path: $TMPDIR, then /var/tmp. Three sites, including the cli plugin, and they carry the conversation, so the path is now a 512-byte buffer built at runtime instead of a literal.
This commit is contained in:
1 parent
c458f02385
commit
fd72b688c1
7 files changed
+162
-25
No files matched your search
@@ -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);
|
||||
|
||||
|
||||
Binary file not shown.
+5
-1
@@ -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));
|
||||
|
||||
Binary file not shown.
+144
-23
@@ -14,9 +14,13 @@
|
||||
* not an empty success.
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <poll.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Binary file not shown.
Reference in new issue
Block a user