env=NAME=VALUE on a delegate line (repeatable) is set in the child right before exec and beats what the parent exported, so two delegates can run the same cli with two accounts. Remotely it goes in front as `env 'NAME=VALUE'`, quoted like the rest. cli_env = NAME=VALUE does the same for the main cli transport. A bad name is an error with the line. Also in this commit, the work in progress from the tree: swarm and provider state, the provider/scheduler/planner/tmux plugins, the ui test, whose tmux session name is now per run so parallel runs do not collide. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
224 lines
5.6 KiB
C
224 lines
5.6 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"
|
|
|
|
#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.
|
|
*/
|
|
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;
|
|
|
|
(void)err;
|
|
if (sp_buf_init(&raw) != 0 || sp_buf_init(&errb) != 0)
|
|
return -1;
|
|
if (sp_cmdscript_run(dir, name, args, &raw, &errb) != 0) {
|
|
sp_buf_printf(out, "error: %s", errb.p != NULL ? errb.p : "failed");
|
|
sp_buf_free(&raw);
|
|
sp_buf_free(&errb);
|
|
return 0;
|
|
}
|
|
if (raw.p == NULL || raw.p[0] == '\0')
|
|
sp_buf_printf(out, "Der Befehl /%s hat nichts ausgegeben.", name);
|
|
else
|
|
sp_buf_printf(out, "Ausgabe von /%s%s%s:\n\n%s", name,
|
|
args != NULL && *args != '\0' ? " " : "",
|
|
args != NULL ? args : "", raw.p);
|
|
sp_buf_free(&raw);
|
|
sp_buf_free(&errb);
|
|
return 0;
|
|
}
|