Files
splinter/src/backend/cmdscript.c
T
lotherk 4cbd57d829 tree: lcm keeps the task, and the in-flight work it sits in
Everything that was uncommitted in the shared tree, in one commit because the
changes are interleaved across the same files (lcm, engine, frontend, locales).

lcm: a continuation prompt is not the task. After /resume the newest user
message is "Continue with the task ...", and pinning it kept the keystroke
while the question it continues was cut away -- measured: a resumed session
woke up with the prompt, one tool call and its error, and no task at all. The
pin now skips the harness's own continuation prompts, so the real question is
pinned again. test_compactor drives it: with the skip disabled the task is
lost (red), with it the task comes back after the summary (green).

engine: /self-check and the exact tool-argument parse are in a1794fb; this
commit carries the tests for them (a failed tool observation must appear in
the self-check prompt; mcp_parse_exact rejects two objects in one string) and
the i18n keys the new code uses: cmd.script.*, main.no_profile, tg.busy,
tg.interview.running, ui.agent_busy in all three catalogues.

The rest is the in-flight work from the shared session: engine/frontend,
plugins, telegram/a2a, and their tests.

make check: all tests passed; check_i18n ok, 204 keys in 3 catalogues.
2026-11-18 23:03:09 +00:00

236 lines
5.9 KiB
C

/*
* splinter -- slash commands that are just programs.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* Anything executable in ~/.splinter/commands/ becomes /name. It gets the rest
* of the line as its first argument, and whatever it prints goes to the model as
* context - not to the operator as a message. That is the point: a command is a
* way to hand the model something it cannot get itself (a git log, a database
* query, a screenshot's OCR), without the operator copying text around.
*
* Rule: the program decides what to print; the harness decides that the model
* sees it. A program that prints nothing is a bug the operator will notice,
* because the model will be told exactly that.
*/
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "splinter.h"
#include "cmdscript.h"
#include "i18n.h"
#define CMD_TIMEOUT_MS 300000
#define CMD_MAX_BYTES (4u << 20)
/*
* Where the programs live: $SPLINTER_COMMANDS_DIR, else ~/.splinter/commands.
*
* Not the session directory: commands are the operator's tools, not a session's
* private property - the first version keyed them off --session and found
* nothing whenever a session lived anywhere else.
*/
int
sp_cmdscript_dir(const char *session_dir, char *buf, size_t len)
{
const char *env = getenv("SPLINTER_COMMANDS_DIR");
const char *home = getenv("HOME");
(void)session_dir;
if (env != NULL && *env != '\0') {
snprintf(buf, len, "%s", env);
return 0;
}
if (home == NULL || *home == '\0')
return -1;
snprintf(buf, len, "%s/.splinter/commands", home);
return 0;
}
/* Is there an executable by that name? No paths, no dots: /foo is a name. */
int
sp_cmdscript_exists(const char *dir, const char *name)
{
char path[700];
size_t i;
if (dir == NULL || name == NULL || *name == '\0')
return 0;
for (i = 0; name[i] != '\0'; i++)
if (!((name[i] >= 'a' && name[i] <= 'z') ||
(name[i] >= 'A' && name[i] <= 'Z') ||
(name[i] >= '0' && name[i] <= '9') ||
name[i] == '-' || name[i] == '_' || name[i] == '.'))
return 0;
if (strstr(name, "..") != NULL)
return 0;
snprintf(path, sizeof(path), "%s/%s", dir, name);
return access(path, X_OK) == 0;
}
int
sp_cmdscript_list(const char *dir, struct sp_buf *out)
{
DIR *d;
struct dirent *e;
int n = 0;
d = opendir(dir);
if (d == NULL)
return 0;
while ((e = readdir(d)) != NULL) {
char path[700];
if (e->d_name[0] == '.')
continue;
snprintf(path, sizeof(path), "%s/%s", dir, e->d_name);
if (access(path, X_OK) != 0)
continue;
sp_buf_printf(out, "/%s ", e->d_name);
n++;
}
closedir(d);
return n;
}
/*
* Run it. The argument is the whole rest of the line, one string: a command
* that wants words can split it itself, and a command that takes a commit
* message keeps its spaces.
*/
int
sp_cmdscript_run(const char *dir, const char *name, const char *args,
struct sp_buf *out, struct sp_buf *err)
{
char path[700];
int fds[2];
pid_t child;
snprintf(path, sizeof(path), "%s/%s", dir, name);
if (pipe(fds) != 0) {
sp_buf_printf(err, "%s: pipe: %s", name, strerror(errno));
return -1;
}
child = fork();
if (child < 0) {
sp_buf_printf(err, "%s: fork: %s", name, strerror(errno));
close(fds[0]);
close(fds[1]);
return -1;
}
if (child == 0) {
char *argv[3];
int devnull = open("/dev/null", O_RDONLY);
(void)dup2(fds[1], STDOUT_FILENO);
(void)dup2(fds[1], STDERR_FILENO);
if (devnull >= 0) {
(void)dup2(devnull, STDIN_FILENO);
close(devnull);
}
close(fds[0]);
close(fds[1]);
argv[0] = path;
argv[1] = (char *)(args != NULL ? args : "");
argv[2] = NULL;
execv(path, argv);
fprintf(stderr, "%s: %s\n", path, strerror(errno));
_exit(126);
}
close(fds[1]);
{
struct pollfd pfd;
char chunk[4096];
int timed_out = 0;
for (;;) {
ssize_t got;
pfd.fd = fds[0];
pfd.events = POLLIN;
if (poll(&pfd, 1, CMD_TIMEOUT_MS) <= 0) {
(void)kill(child, SIGKILL);
timed_out = 1;
break;
}
got = read(fds[0], chunk, sizeof(chunk));
if (got <= 0)
break;
if (out->len + (size_t)got > CMD_MAX_BYTES) {
sp_buf_puts(out, "\n[command output truncated]\n");
(void)kill(child, SIGKILL);
break;
}
if (sp_buf_add(out, chunk, (size_t)got) != 0)
break;
}
close(fds[0]);
{
int st = 0;
(void)waitpid(child, &st, 0);
if (timed_out) {
sp_buf_printf(err, "/%s: timed out after %d s",
name, CMD_TIMEOUT_MS / 1000);
return -1;
}
if (WIFEXITED(st) && WEXITSTATUS(st) != 0)
sp_buf_printf(out, "[exit status %d]\n",
WEXITSTATUS(st));
}
}
return 0;
}
/*
* The entry point UI and gateway share: run /name and hand back the text the
* model should see, already framed so it cannot be mistaken for the user's own
* words. 0 with the text in 'out'; -1 when the program could not run (or ran
* out of time), with the reason in 'err' for the operator.
*/
int
sp_slash_script(const char *dir, const char *name, const char *args,
struct sp_buf *out, struct sp_buf *err)
{
struct sp_buf raw, errb;
if (sp_buf_init(&raw) != 0)
return -1;
if (sp_buf_init(&errb) != 0) {
sp_buf_free(&raw);
return -1;
}
if (sp_cmdscript_run(dir, name, args, &raw, &errb) != 0) {
if (err != NULL)
sp_buf_printf(err, sp_td("cmd.script.failed",
"/%s failed: %s"), name,
errb.p != NULL && errb.len > 0 ? errb.p : "?");
sp_buf_free(&raw);
sp_buf_free(&errb);
return -1;
}
if (raw.p == NULL || raw.p[0] == '\0')
sp_buf_printf(out, sp_td("cmd.script.empty",
"The command /%s printed nothing."), name);
else {
sp_buf_printf(out, sp_td("cmd.script.output",
"Output of /%s%s%s:"), name,
args != NULL && *args != '\0' ? " " : "",
args != NULL ? args : "");
sp_buf_printf(out, "\n\n%s", raw.p);
}
sp_buf_free(&raw);
sp_buf_free(&errb);
return 0;
}