drop tmux/planner/sysinfo, tasks becomes processes, no capitalised calls

- plugins/tmux, plugins/planner and plugins/sysinfo are gone (plugin list,
  Makefile rules, tests, docs). The config no longer loads them; the stale
  .so files were removed from the install tree.
- plugins/tasks is plugins/processes: plugin name "processes", widget
  "processes", tools process_start/process_list/process_result/process_kill.
  The sidebar now lists only running work: a finished task keeps its output
  in process_result but stops taking a row forever.
- the dead planner link in the delegate (link_plan_quota -> command "tasks")
  is removed with the planner.
- tool call rows are no longer capitalised: "run  ls -l", "read  src/x.c" as
  the model sent them, not "Run "/"Read ".
- tests updated (delegate panel expects no finished row, engine/UI expect the
  lowercase call row, secrets/delegate use the processes plugin and tools).
- the user's config gets a Delegate/Scheduler section: the three providers
  carry review/plan/docs tags and keep max_parallel=2; plugin scheduler stays
  loaded so waiting jobs are retried.

make check: all tests passed.
This commit is contained in:
lotherk committed 2026-11-17 21:06:55 +00:00
1 parent 1e15c91e4e
commit 8aee12e9d4
17 files changed
+73 -2074

No files matched your search

+4 -22
View File
@@ -38,18 +38,15 @@ CLIENTOBJ = src/frontend/client.o
PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \
plugins/cli/cli.so plugins/focus/focus.so plugins/mcp/mcp.so \
plugins/ring/ring.so plugins/reminder/reminder.so \
plugins/kids/kids.so plugins/status/status.so plugins/tasks/tasks.so \
plugins/kids/kids.so plugins/status/status.so plugins/processes/processes.so \
plugins/provider/provider.so \
plugins/scheduler/scheduler.so \
plugins/planner/planner.so \
plugins/a2a/a2a.so \
plugins/sysinfo/sysinfo.so \
plugins/tmux/tmux.so \
plugins/junction/junction.so \
plugins/self/self.so
TESTS = tests/test_buf tests/test_tool tests/test_session tests/test_conv \
tests/test_transport tests/test_agent tests/test_openai \
tests/test_theme tests/test_plugin tests/test_skills tests/test_telegram tests/test_delegate tests/test_builtin tests/test_ascii tests/test_ack tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_anthropic tests/test_cli tests/test_compactor tests/test_secrets tests/test_mcpclient tests/test_kids tests/test_lifecycle tests/test_md tests/test_tgmd tests/test_providers tests/test_b64 tests/test_pause tests/test_swarm tests/test_provstate tests/test_planner tests/test_tmux tests/test_usage tests/test_agents tests/test_confblock tests/test_jobs tests/test_engine tests/test_self
tests/test_theme tests/test_plugin tests/test_skills tests/test_telegram tests/test_delegate tests/test_builtin tests/test_ascii tests/test_ack tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_anthropic tests/test_cli tests/test_compactor tests/test_secrets tests/test_mcpclient tests/test_kids tests/test_lifecycle tests/test_md tests/test_tgmd tests/test_providers tests/test_b64 tests/test_pause tests/test_swarm tests/test_provstate tests/test_usage tests/test_agents tests/test_confblock tests/test_jobs tests/test_engine tests/test_self
RPATH = -Wl,-rpath,'$$ORIGIN'
PLUGIN_RPATH = -Wl,-rpath,'$$ORIGIN/../..'
@@ -115,8 +112,8 @@ plugins/ring/ring.so: plugins/ring/ring.c $(LIB)
plugins/kids/kids.so: plugins/kids/kids.c $(LIB)
$(CC) $(CFLAGS) -shared -o $@ plugins/kids/kids.c -L. -lsplinter \
$(PLUGIN_RPATH) $(LIBS)
plugins/tasks/tasks.so: plugins/tasks/tasks.c $(LIB)
$(CC) $(CFLAGS) -shared -o $@ plugins/tasks/tasks.c -L. -lsplinter \
plugins/processes/processes.so: plugins/processes/processes.c $(LIB)
$(CC) $(CFLAGS) -shared -o $@ plugins/processes/processes.c -L. -lsplinter \
$(PLUGIN_RPATH) $(LIBS)
plugins/status/status.so: plugins/status/status.c $(LIB)
$(CC) $(CFLAGS) -shared -o $@ plugins/status/status.c -L. -lsplinter \
@@ -246,8 +243,6 @@ tests/test_swarm: tests/test_swarm.c $(LIB)
$(CC) $(CFLAGS) -o $@ tests/test_swarm.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
tests/test_provstate: tests/test_provstate.c $(LIB)
$(CC) $(CFLAGS) -o $@ tests/test_provstate.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
tests/test_planner: tests/test_planner.c $(LIB) $(PLUGIN)
$(CC) $(CFLAGS) -o $@ tests/test_planner.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
tests/test_agents: tests/test_agents.c $(LIB)
$(CC) $(CFLAGS) -o $@ tests/test_agents.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
@@ -263,8 +258,6 @@ tests/test_self: tests/test_self.c $(LIB) plugins/self/self.so
$(CC) $(CFLAGS) -o $@ tests/test_self.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
tests/test_usage: tests/test_usage.c $(LIB)
$(CC) $(CFLAGS) -o $@ tests/test_usage.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
tests/test_tmux: tests/test_tmux.c $(LIB) $(PLUGIN)
$(CC) $(CFLAGS) -o $@ tests/test_tmux.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
tests/test_jobs: tests/test_jobs.c $(LIB) $(PLUGIN)
$(CC) $(CFLAGS) -o $@ tests/test_jobs.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
tests/test_config: tests/test_config.c $(LIB)
@@ -361,21 +354,10 @@ plugins/provider/provider.so: plugins/provider/provider.c $(LIB)
plugins/scheduler/scheduler.so: plugins/scheduler/scheduler.c $(LIB)
$(CC) $(CFLAGS) -shared -o $@ plugins/scheduler/scheduler.c -L. -lsplinter \
$(PLUGIN_RPATH) $(LIBS)
plugins/planner/planner.so: plugins/planner/planner.c $(LIB)
$(CC) $(CFLAGS) -shared -o $@ plugins/planner/planner.c -L. -lsplinter \
$(PLUGIN_RPATH) $(LIBS)
plugins/tmux/tmux.so: plugins/tmux/tmux.c $(LIB)
$(CC) $(CFLAGS) -shared -o $@ plugins/tmux/tmux.c -L. -lsplinter \
$(PLUGIN_RPATH) $(LIBS)
plugins/a2a/a2a.so: plugins/a2a/a2a.c $(LIB)
$(CC) $(CFLAGS) -shared -o $@ plugins/a2a/a2a.c -L. -lsplinter \
$(PLUGIN_RPATH) $(LIBS)
plugins/sysinfo/sysinfo.so: plugins/sysinfo/sysinfo.c $(LIB)
$(CC) $(CFLAGS) -shared -o $@ plugins/sysinfo/sysinfo.c -L. -lsplinter \
$(PLUGIN_RPATH) $(LIBS)
plugins/junction/junction.so: plugins/junction/junction.c $(LIB)
$(CC) $(CFLAGS) -shared -o $@ plugins/junction/junction.c -L. -lsplinter \
$(PLUGIN_RPATH) $(LIBS)
+2 -2
View File
@@ -513,7 +513,7 @@ API answers `chat not found`. That is Telegram, not the harness.
The bot registers the commands that actually exist with `setMyCommands`:
`/start`, `/help`, `/interview`, `/new`, `/model`, `/connect`, every command a
loaded plugin registers
(`/goal`, `/tasks`, `/quota`, `/tmux`, ...) and every program in
(`/goal`, `/quota`, ...) and every program in
`~/.splinter/commands/` whose name is a valid Telegram command (lower-case,
digits, underscore, at most 32). `/start` and `/help` answer with the short
text plus that live list. An approval arrives as a message with **Yes/No
@@ -617,7 +617,7 @@ own model, up to eight run at once, and the answers come back labelled:
- slow: 1 task(s), 12018 ms total
- fast: 1 task(s), 16 ms total
The planner gets who did what, how long it took and whether it worked - not a wall
The summary gets who did what, how long it took and whether it worked - not a wall
of answers. A delegate that is slow or failing is visible in the header before
anybody reads its prose.
-1
View File
@@ -336,7 +336,6 @@ apart from `path` and `enabled` is that plugin's own configuration:
plugin lcm {
keep = 40
}
plugin tmux { }
plugin junction { } # url/header from the mcp junction server
plugin mine {
path = ~/my/plugin.so # not shipped: say where it is
-42
View File
@@ -327,21 +327,6 @@ children, delegates, a CLI transport. CPU is the delta between two samples
cumulative for the session. `/self` and the `session_usage` tool print the
same numbers with the pid and the child count.
## sysinfo
RAM, CPU, swap and disk in one sidebar line:
```
RAM 46% [|| ] CPU 13% [| ] SWAP 12% [ ] DISK 71% [|||| ]
```
A widget thread samples `/proc/meminfo`, `/proc/stat` and `statvfs("/")` every
two seconds. CPU is the busy share between two samples; RAM is
`MemTotal - MemAvailable` (with Free+Buffers+Cached as the fallback); swap is
`SwapTotal - SwapFree`; disk is `f_blocks - f_bfree` on `/`. Everything is a
percentage with a five-cell `[|||| ]` gauge. A missing `/proc` entry leaves the
number at 0 and the plugin still loads. It has no commands and no config.
## Zustimmung (ack)
Ein Werkzeug, das etwas aendert, braucht ein Ja von einem Menschen. Der Mensch
@@ -399,16 +384,6 @@ Regeln, die der Code durchsetzt:
selben Konto vom Zustimmen abhalten will, braucht eine eigene uid fuer splinter
(oder einen Kanal, dessen Antwort der lokale Benutzer nicht schreiben kann).
## planner
A plan for the session, not a background job. The list lives in
`<session>/plan_tasks.json`; the model can add and update tasks, and `/tasks`
shows them, picks the active one and moves it between `todo`, `doing`, `done`,
`blocked` and `waiting_quota` (any other state is refused). The file is read and
written under one lock on every operation and replaced atomically. The focus plugin's `/goal` is the short reminder;
planner is the list behind it. It is a different store from the `tasks` plugin,
which owns shell commands with a process and a clock.
## provider
Per-`(provider, key)` quota and usage, shared by every splinter that uses the
@@ -427,23 +402,6 @@ delegation job that is due (`waiting_quota`, `waiting_poll` or `queued` with
delegate job tools, "quota is back in three days" becomes a scheduled retry
instead of a lost task.
## tmux
Drives a program that wants a terminal (a REPL, an installer, another agent's
TUI) in a detached tmux session. Tools: `tmux_start` (destructive: it runs a
shell command), `tmux_send` (text and/or key names such as `C-c`), `tmux_ask`
(send a line, wait until the pane has new output and has been quiet for
`quiet_ms`, return the new lines), `tmux_read`, `tmux_capture` and
`tmux_status`. `/tmux` has the same operations plus `attach` and `stop`.
"New" is a diff of two `capture-pane` snapshots kept in memory: it finds the old
capture inside the new one and returns what follows. There is no helper process,
no pid file and nothing left behind when splinter exits; the target name is the
only thing written, to `<session>/tmux_target`, so a resumed session finds its
terminal again. Session names and key names are checked so that neither can be
read by tmux as an option. The plugin refuses to load when `tmux` is not
installed. `tests/test_tmux.c` runs against a real tmux and skips without one.
## a2a
Another agent as the backend, over the Agent2Agent protocol. The transport is
+4 -2
View File
@@ -47,8 +47,10 @@ splinter # Chat auf "work"
3. **Arbeiten**: normal beschreiben, was getan werden soll. Umsetzung macht
der Pool `work`; Aufgaben mit Wörtern wie *analysiere, entscheide, bewerte,
review, Architektur, Ursache* gehen an `claudeapi`.
4. **Plan führen**: `/tasks` zeigt den Plan der Sitzung, `/tasks work next`
nimmt den nächsten Punkt.
4. **Hintergrundarbeit**: `process_start` startet lange Kommandos,
`process_list`/`process_result` holen Stand und Ausgabe; `delegate` gibt
eine Aufgabe an ein anderes Modell, der `scheduler` wiederholt wartende
Jobs nach Quota/`run_after`.
5. **Verbrauch sehen**: Statusleiste und `/usage` (Balken je Provider).
6. **Beenden/Fortsetzen**: Ctrl-D beendet, `splinter -c` setzt die letzte
Sitzung in diesem Verzeichnis fort.
-633
View File
@@ -1,633 +0,0 @@
/*
* splinter -- the plan of a session (/tasks).
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* A list of things the session means to do, kept in <session>/plan_tasks.json.
* It is not a process table: a shell command with a pid and a clock belongs to
* the tasks plugin (task_start). Here a task is a sentence and a state:
*
* todo -> doing -> done blocked waiting_quota
*
* The file is the contract. Every operation reads it, changes it and writes it
* back under one lock, so the tools, the /tasks command, the sidebar tick and a
* second splinter looking at the same session all see the same list. Making a
* task "doing" sets /goal to its text (the focus plugin), which is what keeps a
* long session from drifting.
*
* Other code reaches this through the command, not through this file:
* the delegate parks a task with "/tasks wait <id> <when> <note>" when its
* provider is out of quota, and the tick below puts it back to todo when the
* time has come.
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "splinter.h"
#include "mcp.h"
#include "splinter_plugin.h"
#include "i18n.h"
#define MAX_TASKS 128
#define DESC_LEN 1024
#define NOTE_LEN 256
#define WHEN_LEN 64
#define STATUS_LEN 24
#define FILE_MAX (256 * 1024)
struct task {
int id;
char status[STATUS_LEN];
char desc[DESC_LEN];
char run_after[WHEN_LEN]; /* unix time, or empty */
char note[NOTE_LEN];
long created;
long updated;
};
static const char *const states[] = {
"todo", "doing", "done", "blocked", "waiting_quota", NULL
};
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static struct sp_host *host_ref;
static struct task tasks[MAX_TASKS];
static size_t ntasks;
static int next_id = 1;
static int current_id = -1; /* the task in "doing" */
/* ------------------------------------------------------------- the file */
static int
store_path(char *path, size_t len)
{
const char *dir = sp_host_session_dir(host_ref);
if (dir == NULL || dir[0] == '\0')
return -1;
snprintf(path, len, "%s/plan_tasks.json", dir);
return 0;
}
static void
copy_str(char *dst, size_t len, const char *src)
{
snprintf(dst, len, "%s", src != NULL ? src : "");
}
static void
load(void)
{
char path[600];
char *text;
FILE *f;
size_t n, i;
mcp_json *root;
ntasks = 0;
next_id = 1;
current_id = -1;
if (store_path(path, sizeof(path)) != 0 || (f = fopen(path, "r")) == NULL)
return;
if ((text = malloc(FILE_MAX + 1)) == NULL) {
fclose(f);
return;
}
n = fread(text, 1, FILE_MAX, f);
fclose(f);
text[n] = '\0';
root = mcp_parse(text);
free(text);
if (root == NULL)
return;
for (i = 0; i < mcp_array_len(root) && ntasks < MAX_TASKS; i++) {
const mcp_json *o = mcp_array_at(root, i);
struct task *t = &tasks[ntasks];
memset(t, 0, sizeof(*t));
t->id = mcp_arg_int(o, "id", 0);
if (t->id <= 0)
t->id = next_id;
copy_str(t->status, sizeof(t->status),
mcp_arg_string(o, "status"));
if (t->status[0] == '\0')
copy_str(t->status, sizeof(t->status), "todo");
copy_str(t->desc, sizeof(t->desc), mcp_arg_string(o, "desc"));
copy_str(t->run_after, sizeof(t->run_after),
mcp_arg_string(o, "run_after"));
copy_str(t->note, sizeof(t->note), mcp_arg_string(o, "note"));
t->created = (long)mcp_arg_num(o, "created", 0);
t->updated = (long)mcp_arg_num(o, "updated", 0);
if (t->id >= next_id)
next_id = t->id + 1;
if (strcmp(t->status, "doing") == 0)
current_id = t->id;
ntasks++;
}
mcp_json_free(root);
}
static void
put_field(struct sp_buf *b, const char *key, const char *val)
{
sp_buf_printf(b, "\"%s\":\"", key);
sp_json_escape(b, val);
sp_buf_puts(b, "\"");
}
/* Write to a temporary name and rename: a reader never sees half a file. */
static void
save(void)
{
char path[600], tmp[620];
struct sp_buf b;
size_t i;
FILE *f;
if (store_path(path, sizeof(path)) != 0 || sp_buf_init(&b) != 0)
return;
sp_buf_puts(&b, "[\n");
for (i = 0; i < ntasks; i++) {
const struct task *t = &tasks[i];
sp_buf_printf(&b, " {\"id\":%d,", t->id);
put_field(&b, "status", t->status);
sp_buf_puts(&b, ",");
put_field(&b, "desc", t->desc);
sp_buf_puts(&b, ",");
put_field(&b, "run_after", t->run_after);
sp_buf_puts(&b, ",");
put_field(&b, "note", t->note);
sp_buf_printf(&b, ",\"created\":%ld,\"updated\":%ld}%s\n",
t->created, t->updated, i + 1 < ntasks ? "," : "");
}
sp_buf_puts(&b, "]\n");
snprintf(tmp, sizeof(tmp), "%s.tmp", path);
if ((f = fopen(tmp, "w")) != NULL) {
int ok = fwrite(b.p, 1, b.len, f) == b.len;
if (fclose(f) != 0)
ok = 0;
if (!ok || rename(tmp, path) != 0)
unlink(tmp);
}
sp_buf_free(&b);
}
/* ---------------------------------------------------------------- state */
static struct task *
find(int id)
{
size_t i;
for (i = 0; i < ntasks; i++)
if (tasks[i].id == id)
return &tasks[i];
return NULL;
}
static struct task *
first_todo(void)
{
size_t i;
for (i = 0; i < ntasks; i++)
if (strcmp(tasks[i].status, "todo") == 0)
return &tasks[i];
return NULL;
}
static int
valid_state(const char *s)
{
size_t i;
for (i = 0; states[i] != NULL; i++)
if (strcmp(states[i], s) == 0)
return 1;
return 0;
}
/* "+90" is ninety seconds from now; anything else is taken as written. */
static void
when(const char *in, char *out, size_t len)
{
out[0] = '\0';
if (in == NULL || in[0] == '\0')
return;
if (in[0] == '+') {
long d = strtol(in + 1, NULL, 10);
if (d <= 0)
d = 3600;
snprintf(out, len, "%ld", (long)time(NULL) + d);
return;
}
snprintf(out, len, "%s", in);
}
static void
widget(void)
{
char b[256];
size_t i;
int todo = 0, doing = 0, done = 0;
for (i = 0; i < ntasks; i++) {
if (strcmp(tasks[i].status, "todo") == 0)
todo++;
else if (strcmp(tasks[i].status, "doing") == 0)
doing++;
else if (strcmp(tasks[i].status, "done") == 0)
done++;
}
if (ntasks == 0)
b[0] = '\0'; /* no plan: nothing on the sidebar */
else if (current_id > 0)
snprintf(b, sizeof(b), sp_td("plan.widget_active",
"#%d active - todo %d, doing %d, done %d"), current_id,
todo, doing, done);
else
snprintf(b, sizeof(b), sp_td("plan.widget",
"todo %d, doing %d, done %d"), todo, doing, done);
sp_host_widget_text(host_ref, "plan", b);
}
/* One task is "doing" at a time; its text becomes the session goal. */
static void
start(struct task *t)
{
size_t i;
struct sp_buf ignored;
for (i = 0; i < ntasks; i++)
if (strcmp(tasks[i].status, "doing") == 0)
copy_str(tasks[i].status, STATUS_LEN, "todo");
copy_str(t->status, STATUS_LEN, "doing");
t->updated = (long)time(NULL);
current_id = t->id;
if (sp_buf_init(&ignored) == 0) {
(void)sp_host_command(host_ref, "goal", t->desc, &ignored);
sp_buf_free(&ignored);
}
}
static void
changed(void)
{
save();
widget();
}
/* ------------------------------------------------------------ operations */
static int
op_add(const char *desc, const char *run_after, struct sp_buf *out)
{
struct task *t;
if (desc == NULL || desc[0] == '\0')
return sp_buf_puts(out, "error: description required\n");
if (ntasks >= MAX_TASKS)
return sp_buf_printf(out, "error: the plan holds %d tasks\n",
MAX_TASKS);
t = &tasks[ntasks++];
memset(t, 0, sizeof(*t));
t->id = next_id++;
copy_str(t->status, sizeof(t->status), "todo");
copy_str(t->desc, sizeof(t->desc), desc);
when(run_after, t->run_after, sizeof(t->run_after));
t->created = t->updated = (long)time(NULL);
changed();
return sp_buf_printf(out, "added plan task #%d: %s\n", t->id, t->desc);
}
static int
op_list(struct sp_buf *out)
{
size_t i;
if (ntasks == 0)
return sp_buf_puts(out, "(no plan tasks) use plan_task_add\n");
sp_buf_puts(out, "plan tasks:\n");
for (i = 0; i < ntasks; i++) {
const struct task *t = &tasks[i];
sp_buf_printf(out, " #%d [%s]%s %s", t->id, t->status,
t->id == current_id ? " *" : "", t->desc);
if (t->run_after[0] != '\0')
sp_buf_printf(out, " (run_after=%s)", t->run_after);
if (t->note[0] != '\0')
sp_buf_printf(out, " # %s", t->note);
sp_buf_puts(out, "\n");
}
return sp_buf_puts(out, "use /tasks work <id> | work all | next\n");
}
static int
op_done(int id, struct sp_buf *out)
{
struct task *t = find(id);
if (t == NULL)
return sp_buf_puts(out, "no current task to mark done\n");
copy_str(t->status, sizeof(t->status), "done");
t->updated = (long)time(NULL);
if (current_id == id)
current_id = -1;
changed();
return sp_buf_printf(out, "task #%d done\n", id);
}
/* Park a task until a time: the delegate does this when a quota runs out. */
static int
op_wait(int id, const char *at, const char *note, struct sp_buf *out)
{
struct task *t = find(id);
if (t == NULL)
return sp_buf_printf(out, "no task #%d\n", id);
if (at != NULL && at[0] != '\0')
when(at, t->run_after, sizeof(t->run_after));
else
when("+3600", t->run_after, sizeof(t->run_after));
copy_str(t->status, sizeof(t->status), "waiting_quota");
copy_str(t->note, sizeof(t->note),
note != NULL && note[0] != '\0' ? note : "quota wait");
t->updated = (long)time(NULL);
if (current_id == id)
current_id = -1;
changed();
return sp_buf_printf(out, "task #%d waiting_quota run_after=%s\n", id,
t->run_after);
}
/* ----------------------------------------------------------------- tools */
static int
arg_id(const mcp_json *a, const char *key)
{
const char *s = mcp_arg_string(a, key);
return s != NULL ? atoi(s) : mcp_arg_int(a, key, 0);
}
static int
tool_add(const char *args, struct sp_buf *out, void *ctx)
{
mcp_json *a;
const char *desc;
int rc;
(void)ctx;
if ((a = mcp_parse(args != NULL ? args : "{}")) == NULL)
return sp_buf_puts(out, "error: arguments are not JSON\n");
desc = mcp_arg_string(a, "description");
pthread_mutex_lock(&lock);
load();
rc = op_add(desc, mcp_arg_string(a, "run_after"), out);
pthread_mutex_unlock(&lock);
mcp_json_free(a);
return rc;
}
static int
tool_list(const char *args, struct sp_buf *out, void *ctx)
{
int rc;
(void)args;
(void)ctx;
pthread_mutex_lock(&lock);
load();
rc = op_list(out);
pthread_mutex_unlock(&lock);
return rc;
}
static int
tool_update(const char *args, struct sp_buf *out, void *ctx)
{
mcp_json *a;
struct task *t;
const char *s;
int id;
(void)ctx;
if ((a = mcp_parse(args != NULL ? args : "{}")) == NULL)
return sp_buf_puts(out, "error: arguments are not JSON\n");
id = arg_id(a, "id");
pthread_mutex_lock(&lock);
load();
if (id <= 0 || (t = find(id)) == NULL) {
sp_buf_printf(out, id <= 0 ? "error: id required\n" :
"error: no task #%d\n", id);
goto out;
}
if ((s = mcp_arg_string(a, "status")) != NULL && s[0] != '\0') {
if (!valid_state(s)) {
sp_buf_printf(out, "error: status is one of todo, "
"doing, done, blocked, waiting_quota, not '%s'\n", s);
goto out;
}
if (strcmp(s, "doing") == 0)
start(t);
else {
copy_str(t->status, sizeof(t->status), s);
if (current_id == t->id)
current_id = -1;
}
}
if ((s = mcp_arg_string(a, "description")) != NULL && s[0] != '\0')
copy_str(t->desc, sizeof(t->desc), s);
if (mcp_arg(a, "run_after") != NULL) /* present but empty: clear it */
when(mcp_arg_string(a, "run_after"), t->run_after,
sizeof(t->run_after));
if ((s = mcp_arg_string(a, "note")) != NULL && s[0] != '\0')
copy_str(t->note, sizeof(t->note), s);
t->updated = (long)time(NULL);
changed();
sp_buf_printf(out, "task #%d -> [%s] %s\n", t->id, t->status, t->desc);
out:
pthread_mutex_unlock(&lock);
mcp_json_free(a);
return 0;
}
/* --------------------------------------------------------------- command */
static const char *
skip(const char *s)
{
while (*s == ' ')
s++;
return s;
}
static int
command_locked(const char *args, struct sp_buf *out)
{
const char *p;
struct task *t;
if (args == NULL || args[0] == '\0' || strcmp(args, "ls") == 0 ||
strcmp(args, "list") == 0)
return op_list(out);
if (strncmp(args, "add ", 4) == 0)
return op_add(skip(args + 4), NULL, out);
if (strncmp(args, "work", 4) == 0) {
p = skip(args + 4);
if (strcmp(p, "all") == 0) {
if ((t = first_todo()) == NULL)
return sp_buf_puts(out, "no todo tasks left\n");
} else if (*p != '\0') {
if ((t = find(atoi(p))) == NULL)
return sp_buf_printf(out, "no task #%d\n",
atoi(p));
} else
return sp_buf_puts(out, "usage: /tasks work <id>|all\n");
start(t);
changed();
return sp_buf_printf(out, "working on #%d: %s\n", t->id,
t->desc);
}
if (strcmp(args, "choose") == 0 || strcmp(args, "next") == 0) {
if ((t = first_todo()) == NULL)
return sp_buf_puts(out, "no todo tasks\n");
start(t);
changed();
return sp_buf_printf(out, "chose #%d: %s\n", t->id, t->desc);
}
if (strncmp(args, "done", 4) == 0) {
p = skip(args + 4);
return op_done(*p != '\0' ? atoi(p) : current_id, out);
}
if (strncmp(args, "wait", 4) == 0) {
char at[WHEN_LEN];
int id;
size_t n = 0;
p = skip(args + 4);
id = atoi(p);
while (*p != '\0' && *p != ' ')
p++;
p = skip(p);
while (*p != '\0' && *p != ' ' && n + 1 < sizeof(at))
at[n++] = *p++;
at[n] = '\0';
return op_wait(id, at, skip(p), out);
}
return sp_buf_puts(out, "usage: /tasks [ls|add <text>|work <id>|work all"
"|next|done [id]|wait <id> <+secs|epoch> [note]]\n");
}
static int
command(struct sp_host *h, const char *args, struct sp_buf *out, void *ctx)
{
int rc;
(void)ctx;
host_ref = h;
pthread_mutex_lock(&lock);
load();
rc = command_locked(args, out);
pthread_mutex_unlock(&lock);
return rc;
}
/* The clock: a parked task whose time has come is todo again. */
static int
tick(struct sp_host *h, const char *name, void *ctx)
{
long now = (long)time(NULL);
size_t i;
int moved = 0;
(void)name;
(void)ctx;
host_ref = h;
pthread_mutex_lock(&lock);
load();
for (i = 0; i < ntasks; i++) {
struct task *t = &tasks[i];
long at = strtol(t->run_after, NULL, 10);
if (strcmp(t->status, "waiting_quota") != 0 || at <= 0 ||
now < at)
continue;
copy_str(t->status, sizeof(t->status), "todo");
copy_str(t->note, sizeof(t->note), "quota is back");
t->run_after[0] = '\0';
t->updated = now;
moved = 1;
}
if (moved)
save();
widget();
pthread_mutex_unlock(&lock);
return 0;
}
static const struct sp_tool t_add = {
"plan_task_add",
"Add a task to the session's plan: a sentence to do, not a process "
"(use task_start for shell commands). Optional run_after is a unix "
"time, or +seconds from now.",
"{\"type\":\"object\",\"properties\":{\"description\":{\"type\":"
"\"string\"},\"run_after\":{\"type\":\"string\"}},\"required\":"
"[\"description\"]}",
SP_AUTH_MUTATE, tool_add, NULL
};
static const struct sp_tool t_list = {
"plan_task_list",
"List the session's plan with state; the one marked * is active.",
"{\"type\":\"object\",\"properties\":{}}",
SP_AUTH_READ, tool_list, NULL
};
static const struct sp_tool t_update = {
"plan_task_update",
"Change a plan task. status is todo, doing, done, blocked or "
"waiting_quota; doing makes it the session goal. An empty run_after "
"clears the wait.",
"{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"},"
"\"status\":{\"type\":\"string\"},\"description\":{\"type\":\"string\"},"
"\"run_after\":{\"type\":\"string\"},\"note\":{\"type\":\"string\"}},"
"\"required\":[\"id\"]}",
SP_AUTH_MUTATE, tool_update, NULL
};
static const struct sp_widget plan_widget = { "plan", tick, NULL, NULL, 5000 };
static int
init(struct sp_host *h)
{
host_ref = h;
pthread_mutex_lock(&lock);
load();
widget();
pthread_mutex_unlock(&lock);
sp_host_add_tool(h, &t_add);
sp_host_add_tool(h, &t_list);
sp_host_add_tool(h, &t_update);
sp_host_add_command(h, "tasks", command, NULL);
sp_host_add_widget(h, &plan_widget);
return 0;
}
static const struct sp_plugin plugin = {
SP_PLUGIN_ABI, "planner", "0.2", init, NULL, NULL
};
const struct sp_plugin *
sp_plugin_describe(void)
{
return &plugin;
}
@@ -14,10 +14,10 @@
*
* The tools:
*
* task_start command, label? -> id, starts at once
* task_list -> id, state, seconds, bytes, label
* task_result id -> output, exit status; fails while running
* task_kill id -> stop it
* process_start command, label? -> id, starts at once
* process_list -> id, state, seconds, bytes, label
* process_result id -> output, exit status; fails while running
* process_kill id -> stop it
*
* Tasks are shell commands, so starting one is destructive in the same sense that
* `run` is: the gate and the approval path see it like any other change.
@@ -73,7 +73,7 @@ running_count(void)
}
static void
set_task_fields(struct sp_meter *m, void *ctx)
set_process_fields(struct sp_meter *m, void *ctx)
{
const unsigned *run = ctx;
@@ -91,7 +91,7 @@ publish_meter(struct sp_host *h)
{
unsigned run = (unsigned)running_count();
sp_host_meter_update(h, set_task_fields, &run);
sp_host_meter_update(h, set_process_fields, &run);
}
static void
@@ -106,35 +106,37 @@ refresh_widget(struct sp_host *h)
struct task *t = &tasks[i];
int n;
if (t->running)
n = snprintf(text + used, sizeof(text) - used,
"#%d %s %lds\n", t->id, t->label,
(long)(time(NULL) - t->started));
else
n = snprintf(text + used, sizeof(text) - used,
"#%d %s exit %d %.2fs\n", t->id, t->label,
t->status, difftime(t->ended, t->started));
/*
* The sidebar is for what is happening now. A finished task
* stays in process_list/process_result (its output is still there),
* but it does not take a row of the sidebar forever.
*/
if (!t->running)
continue;
n = snprintf(text + used, sizeof(text) - used,
"#%d %s %lds\n", t->id, t->label,
(long)(time(NULL) - t->started));
if (n < 0 || (size_t)n >= sizeof(text) - used)
break;
used += (size_t)n;
}
pthread_mutex_unlock(&tasks_lock);
if (used == 0)
(void)sp_host_widget_text(h, "tasks", "");
(void)sp_host_widget_text(h, "processes", "");
else
(void)sp_host_widget_text(h, "tasks", text);
(void)sp_host_widget_text(h, "processes", text);
}
extern char **environ;
/*
* The end of a task, under tasks_lock: that is the lock task_list, the widget
* The end of a task, under tasks_lock: that is the lock process_list, the widget
* and running_count read 'running', 'status' and 'ended' under. They used to
* be written under the task's own lock only - two locks guarding one field
* guard nothing.
*/
static void
task_finish(struct task *t, int status)
process_finish(struct task *t, int status)
{
pthread_mutex_lock(&tasks_lock);
t->status = status;
@@ -144,14 +146,14 @@ task_finish(struct task *t, int status)
}
/*
* Run the task in its own process group, so task_kill can stop the shell and
* Run the task in its own process group, so process_kill can stop the shell and
* everything it started. popen() gave no pid: t->pid stayed 0, and
* kill(-0, SIGTERM) is "every process in my group" - task_kill killed
* kill(-0, SIGTERM) is "every process in my group" - process_kill killed
* splinter itself. stdin is /dev/null (a task must not read the terminal), and
* the environment loses splinter's keys, as for the 'run' tool.
*/
static void *
task_run(void *arg)
process_run(void *arg)
{
struct task *t = arg;
char chunk[2048];
@@ -169,7 +171,7 @@ task_run(void *arg)
pthread_mutex_lock(&t->lock);
sp_buf_printf(&t->out, "error: %s", strerror(errno));
pthread_mutex_unlock(&t->lock);
task_finish(t, 127);
process_finish(t, 127);
return NULL;
}
for (i = 0, j = 0; i < n; i++)
@@ -200,7 +202,7 @@ task_run(void *arg)
pthread_mutex_lock(&t->lock);
sp_buf_printf(&t->out, "error: fork: %s", strerror(errno));
pthread_mutex_unlock(&t->lock);
task_finish(t, 127);
process_finish(t, 127);
return NULL;
}
(void)setpgid(child, child);
@@ -224,7 +226,7 @@ task_run(void *arg)
pthread_mutex_lock(&tasks_lock);
t->pid = 0; /* reaped: the id may be reused */
pthread_mutex_unlock(&tasks_lock);
task_finish(t, st);
process_finish(t, st);
return NULL;
}
@@ -242,7 +244,7 @@ find(int id)
/*
* The value of an argument, with the parser the rest of splinter uses. The old
* strstr() only matched "key":"..." - the schema declares 'id' an integer, so
* {"id":3} came out empty and every task_kill said "no such task".
* {"id":3} came out empty and every process_kill said "no such task".
*/
static void
arg_str(const char *args, const char *key, char *buf, size_t len)
@@ -325,7 +327,7 @@ ops_end(struct sp_host *h, int id, int status, size_t tokens)
static const struct sp_task_ops task_ops = { ops_begin, ops_end };
static int
tool_task_start(const char *args, struct sp_buf *out, void *ctx)
tool_process_start(const char *args, struct sp_buf *out, void *ctx)
{
char cmd[600], label[80];
struct task *t;
@@ -362,7 +364,7 @@ tool_task_start(const char *args, struct sp_buf *out, void *ctx)
return sp_buf_puts(out, "error: no memory");
}
id = t->id;
if (pthread_create(&t->th, NULL, task_run, t) != 0) {
if (pthread_create(&t->th, NULL, process_run, t) != 0) {
t->running = 0;
ntasks--;
pthread_mutex_unlock(&tasks_lock);
@@ -380,7 +382,7 @@ tool_task_start(const char *args, struct sp_buf *out, void *ctx)
}
static int
tool_task_list(const char *args, struct sp_buf *out, void *ctx)
tool_process_list(const char *args, struct sp_buf *out, void *ctx)
{
int i, any = 0;
@@ -407,7 +409,7 @@ tool_task_list(const char *args, struct sp_buf *out, void *ctx)
}
static int
tool_task_result(const char *args, struct sp_buf *out, void *ctx)
tool_process_result(const char *args, struct sp_buf *out, void *ctx)
{
char num[32];
struct task *t;
@@ -440,7 +442,7 @@ tool_task_result(const char *args, struct sp_buf *out, void *ctx)
}
static int
tool_task_kill(const char *args, struct sp_buf *out, void *ctx)
tool_process_kill(const char *args, struct sp_buf *out, void *ctx)
{
char num[32];
struct task *t;
@@ -475,31 +477,31 @@ tool_task_kill(const char *args, struct sp_buf *out, void *ctx)
}
static const struct sp_tool t_start = {
"task_start",
"process_start",
"Start a shell command in the background and return its id. Use it for "
"builds, tests or long runs instead of blocking the conversation.",
"{\"type\":\"object\",\"properties\":{\"command\":{\"type\":\"string\"},"
"\"label\":{\"type\":\"string\",\"description\":\"short name\"}},"
"\"required\":[\"command\"]}",
SP_AUTH_DESTRUCTIVE, tool_task_start, NULL
SP_AUTH_DESTRUCTIVE, tool_process_start, NULL
};
static const struct sp_tool t_list = {
"task_list", "List background tasks: id, state, time, size, label.",
"{\"type\":\"object\",\"properties\":{}}", SP_AUTH_READ, tool_task_list,
"process_list", "List background tasks: id, state, time, size, label.",
"{\"type\":\"object\",\"properties\":{}}", SP_AUTH_READ, tool_process_list,
NULL
};
static const struct sp_tool t_result = {
"task_result", "Output and exit status of a finished task.",
"process_result", "Output and exit status of a finished task.",
"{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"}},"
"\"required\":[\"id\"]}", SP_AUTH_READ, tool_task_result, NULL
"\"required\":[\"id\"]}", SP_AUTH_READ, tool_process_result, NULL
};
static const struct sp_tool t_kill = {
"task_kill", "Stop a running task.",
"process_kill", "Stop a running task.",
"{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"}},"
"\"required\":[\"id\"]}", SP_AUTH_DESTRUCTIVE, tool_task_kill, NULL
"\"required\":[\"id\"]}", SP_AUTH_DESTRUCTIVE, tool_process_kill, NULL
};
static int
@@ -513,7 +515,7 @@ tasks_tick(struct sp_host *h, const char *name, void *ctx)
}
static const struct sp_widget widget = {
"tasks", /* name */
"processes", /* name */
tasks_tick, /* tick */
NULL, /* draw */
NULL, /* ctx */
@@ -537,7 +539,7 @@ tasks_init(struct sp_host *h)
(void)sp_host_add_tool(h, &t_result);
(void)sp_host_add_tool(h, &t_kill);
(void)sp_host_add_widget(h, &widget);
(void)sp_host_widget_text(h, "tasks", "");
(void)sp_host_widget_text(h, "processes", "");
sp_host_set_task_ops(h, &task_ops);
publish_meter(h);
return 0;
-191
View File
@@ -1,191 +0,0 @@
/*
* splinter -- sysinfo: RAM, CPU, swap and disk in the sidebar.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* Four numbers the operator checks before starting a long turn: is the box
* swapping, is the CPU already busy, is the disk full. Linux exposes all of
* them in /proc and statvfs(2); the widget thread samples them every two
* seconds and publishes one compact line:
*
* RAM 62% [||| ] CPU 13% [| ] SWAP 0% [ ] DISK 71% [|||| ]
*
* The sidebar wraps it at spaces. A missing /proc entry is not an error:
* the plugin still loads and says n/a for what it cannot read.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/statvfs.h>
#include <unistd.h>
#include "splinter.h"
#include "splinter_plugin.h"
#define BAR_WIDTH 5
static int have_cpu;
static unsigned long long prev_total, prev_idle;
/* one [||| ] gauge, width 5 */
static void
gauge(char *b, size_t cap, double pct)
{
int i, filled;
if (cap < BAR_WIDTH + 3)
return;
if (pct < 0)
pct = 0;
if (pct > 100)
pct = 100;
filled = (int)((pct * BAR_WIDTH + 50) / 100);
b[0] = '[';
for (i = 0; i < BAR_WIDTH; i++)
b[i + 1] = i < filled ? '|' : ' ';
b[BAR_WIDTH + 1] = ']';
b[BAR_WIDTH + 2] = '\0';
}
/* /proc/stat's first line: total and idle ticks */
static int
cpu_read(unsigned long long *total, unsigned long long *idle)
{
FILE *f = fopen("/proc/stat", "r");
char line[512];
unsigned long long v[10];
int n;
if (f == NULL)
return -1;
if (fgets(line, sizeof(line), f) == NULL) {
fclose(f);
return -1;
}
fclose(f);
memset(v, 0, sizeof(v));
n = sscanf(line, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
&v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7], &v[8]);
if (n < 4)
return -1;
*total = v[0] + v[1] + v[2] + v[3] + v[4] + v[5] + v[6] + v[7] +
v[8];
*idle = v[3] + v[4]; /* idle + iowait */
return *total > 0 ? 0 : -1;
}
/* /proc/meminfo: MemTotal, MemAvailable or Free+Buffers+Cached, Swap */
static int
mem_read(unsigned long long *mt, unsigned long long *ma,
unsigned long long *st, unsigned long long *sf)
{
FILE *f = fopen("/proc/meminfo", "r");
char line[256];
unsigned long long v;
unsigned long long free_kb = 0, buffers = 0, cached = 0;
*mt = *ma = *st = *sf = 0;
if (f == NULL)
return -1;
while (fgets(line, sizeof(line), f) != NULL) {
if (sscanf(line, "MemTotal: %llu", &v) == 1)
*mt = v;
else if (sscanf(line, "MemAvailable: %llu", &v) == 1)
*ma = v;
else if (sscanf(line, "MemFree: %llu", &v) == 1)
free_kb = v;
else if (sscanf(line, "Buffers: %llu", &v) == 1)
buffers = v;
else if (sscanf(line, "Cached: %llu", &v) == 1)
cached = v;
else if (sscanf(line, "SwapTotal: %llu", &v) == 1)
*st = v;
else if (sscanf(line, "SwapFree: %llu", &v) == 1)
*sf = v;
}
fclose(f);
if (*ma == 0)
*ma = free_kb + buffers + cached;
return *mt > 0 ? 0 : -1;
}
static double
pct(unsigned long long used, unsigned long long total)
{
if (total == 0)
return 0;
if (used > total)
used = total;
return (double)used * 100.0 / (double)total;
}
static int
tick(struct sp_host *h, const char *name, void *ctx)
{
unsigned long long mt, ma, st, sf, total, idle;
double ram = 0, swap = 0, disk = 0, cpu = 0;
char rb[16], cb[16], sb[16], db[16], line[512];
(void)ctx;
if (mem_read(&mt, &ma, &st, &sf) == 0) {
ram = pct(mt - ma, mt);
if (st > 0)
swap = pct(st - sf, st);
}
if (cpu_read(&total, &idle) == 0) {
if (have_cpu && total > prev_total &&
idle >= prev_idle && total - prev_total > 0) {
unsigned long long dt = total - prev_total;
unsigned long long di = idle - prev_idle;
cpu = pct(dt > di ? dt - di : 0, dt);
}
prev_total = total;
prev_idle = idle;
have_cpu = 1;
}
{
struct statvfs vfs;
if (statvfs("/", &vfs) == 0 && vfs.f_blocks > 0) {
unsigned long long used = (unsigned long long)
(vfs.f_blocks - vfs.f_bfree);
disk = pct(used, (unsigned long long)vfs.f_blocks);
}
}
gauge(rb, sizeof(rb), ram);
gauge(cb, sizeof(cb), cpu);
gauge(sb, sizeof(sb), swap);
gauge(db, sizeof(db), disk);
snprintf(line, sizeof(line),
"RAM %.0f%% %s\nCPU %.0f%% %s\nSWAP %.0f%% %s\nDISK %.0f%% %s",
ram, rb, cpu, cb, swap, sb, disk, db);
return sp_host_widget_text(h, name, line);
}
static int
init(struct sp_host *h)
{
static const struct sp_widget w = { "sysinfo", tick, NULL, NULL, 2000 };
unsigned long long total, idle;
(void)cpu_read(&total, &idle);
if (total > 0) {
prev_total = total;
prev_idle = idle;
have_cpu = 1;
}
return sp_host_add_widget(h, &w);
}
static const struct sp_plugin plugin = {
SP_PLUGIN_ABI, "sysinfo", "0.1.0", init, NULL, NULL
};
const struct sp_plugin *
sp_plugin_describe(void)
{
return &plugin;
}
-826
View File
@@ -1,826 +0,0 @@
/*
* splinter -- drive a program that wants a terminal, through tmux.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* Some programs cannot be run with `run`: a REPL, an installer that asks
* questions, another agent's TUI. They need a terminal. tmux gives them one,
* detached, and gives splinter two things back: what is on the pane
* (capture-pane) and a way to type into it (send-keys).
*
* The tools:
*
* tmux_start cmd, name? start cmd in a detached session; it is the target
* tmux_send text? keys? type text and Enter, and/or named keys (C-c, Up)
* tmux_ask text, wait_ms? send, then wait until the pane has new output and
* quiet_ms? has been quiet for quiet_ms; return only the new lines
* tmux_read wait_ms? the lines that are new since the last read
* tmux_capture lines? the pane as it is, newest lines
* tmux_status the target, and whether it is alive
*
* "New" is a diff of two captures, kept in memory: there is no helper process,
* no pid file and nothing left behind when splinter exits. A pane is a screen,
* not a stream, so the diff finds where the old capture ends inside the new one
* (output scrolls up) and returns what follows; a program that repaints the
* whole screen gives the lines that changed at the bottom, or, when the old
* capture cannot be found at all, the last SHOWN_MAX lines.
*
* Starting a session runs a shell command, so tmux_start is destructive, and
* typing into a terminal is whatever that terminal does: tmux_send and
* tmux_ask are mutating.
*/
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "splinter.h"
#include "mcp.h"
#include "splinter_plugin.h"
#define NAME_MAX_LEN 64
#define HISTORY 500 /* lines of scrollback looked at */
#define SHOWN_MAX 200 /* lines handed to the model at once */
#define POLL_MS 200
#define WAIT_DEFAULT 15000
#define WAIT_CAP 120000
#define QUIET_DEFAULT 1200
static struct sp_host *host_ref;
static char target[NAME_MAX_LEN * 2];
static char *last; /* the previous capture */
/* ---------------------------------------------------------------- tmux */
/*
* Run argv, collect stdout and stderr. The child does nothing between fork and
* exec but dup2 and close, which is all a forked child of a threaded process may
* safely do.
*/
static int
run(char *const argv[], struct sp_buf *out)
{
int fd[2], st = 0;
char buf[4096];
ssize_t n;
pid_t pid;
if (pipe(fd) != 0)
return -1;
if ((pid = fork()) < 0) {
close(fd[0]);
close(fd[1]);
return -1;
}
if (pid == 0) {
dup2(fd[1], 1);
dup2(fd[1], 2);
close(fd[0]);
close(fd[1]);
execvp(argv[0], argv);
_exit(127);
}
close(fd[1]);
while ((n = read(fd[0], buf, sizeof(buf))) > 0 ||
(n < 0 && errno == EINTR))
if (n > 0 && out != NULL)
sp_buf_add(out, buf, (size_t)n);
close(fd[0]);
while (waitpid(pid, &st, 0) < 0 && errno == EINTR)
;
return WIFEXITED(st) ? WEXITSTATUS(st) : -1;
}
static int
have_tmux(void)
{
char *const argv[] = { "tmux", "-V", NULL };
return run(argv, NULL) == 0;
}
/* A name that cannot be mistaken for an option or a second target. */
static int
safe_name(const char *s, size_t max)
{
size_t i;
if (s == NULL || s[0] == '\0' || s[0] == '-' || strlen(s) >= max)
return 0;
for (i = 0; s[i] != '\0'; i++)
if (!isalnum((unsigned char)s[i]) && strchr("_.-:%@", s[i]) == NULL)
return 0;
return 1;
}
static int
alive(void)
{
char *const argv[] = { "tmux", "has-session", "-t", target, NULL };
return target[0] != '\0' && run(argv, NULL) == 0;
}
static void
target_file(char *path, size_t len)
{
const char *dir = sp_host_session_dir(host_ref);
if (dir != NULL && dir[0] != '\0')
snprintf(path, len, "%s/tmux_target", dir);
else
path[0] = '\0';
}
static void
set_target(const char *name)
{
char path[600];
FILE *f;
snprintf(target, sizeof(target), "%s", name);
free(last);
last = NULL;
target_file(path, sizeof(path));
if (path[0] != '\0' && (f = fopen(path, "w")) != NULL) {
fprintf(f, "%s\n", target);
fclose(f);
}
}
/* A resumed session finds its terminal again. */
static void
restore_target(void)
{
char path[600];
FILE *f;
target_file(path, sizeof(path));
if (path[0] == '\0' || (f = fopen(path, "r")) == NULL)
return;
if (fgets(target, sizeof(target), f) != NULL)
target[strcspn(target, "\n")] = '\0';
fclose(f);
if (!safe_name(target, sizeof(target)))
target[0] = '\0';
}
static long
now_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
static void
pause_ms(long ms)
{
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000L;
nanosleep(&ts, NULL);
}
/* ----------------------------------------------------- capture and diff */
/*
* The pane with its history, joined lines, without the empty rows tmux pads
* a screen with. NULL when there is no pane.
*/
static char *
capture(int lines)
{
struct sp_buf b;
char from[32];
char *const argv[] = { "tmux", "capture-pane", "-p", "-J", "-t",
target, "-S", from, NULL };
char *s;
size_t n;
snprintf(from, sizeof(from), "-%d", lines);
if (sp_buf_init(&b) != 0)
return NULL;
if (run(argv, &b) != 0) {
sp_buf_free(&b);
return NULL;
}
n = b.len;
while (n > 0 && (b.p[n - 1] == '\n' || b.p[n - 1] == ' '))
n--;
s = malloc(n + 1);
if (s != NULL) {
memcpy(s, b.p, n);
s[n] = '\0';
}
sp_buf_free(&b);
return s;
}
/* Split in place; v gets the start of every line. */
static size_t
split(char *text, char ***v)
{
size_t n = 0, cap = 0;
char *p = text;
*v = NULL;
for (;;) {
char *nl;
if (n == cap) {
char **nv = realloc(*v, (cap ? cap * 2 : 64) * sizeof(**v));
if (nv == NULL) {
free(*v);
*v = NULL;
return 0;
}
*v = nv;
cap = cap ? cap * 2 : 64;
}
(*v)[n++] = p;
if ((nl = strchr(p, '\n')) == NULL)
break;
*nl = '\0';
p = nl + 1;
}
return n;
}
/* Does b[0..k) equal a[from..from+k)? */
static int
same_run(char **a, size_t from, char **b, size_t k)
{
size_t i;
for (i = 0; i < k; i++)
if (strcmp(a[from + i], b[i]) != 0)
return 0;
return 1;
}
/*
* Where in `neu` do the lines that are new begin? The old capture is found as a
* run at the start of the new one (nothing scrolled), or as a tail of the old one
* at the start of the new one (output pushed the top off). The old last line is
* tried last and dropped, because it is the prompt or the line being edited and is
* the one most likely to differ. Returns the index of the first new line, or 0
* when nothing of the old capture can be found.
*/
static size_t
first_new(char **old, size_t no, char **neu, size_t nn)
{
size_t trim, s;
for (trim = 0; trim <= 1 && trim < no; trim++) {
size_t have = no - trim;
for (s = 0; s < have; s++) {
size_t k = have - s;
if (k <= nn && same_run(old, s, neu, k))
return k;
}
}
return 0;
}
/* The lines of `now` that `before` did not have. */
static void
delta(const char *before, const char *now, struct sp_buf *out)
{
char *a = NULL, *b = NULL, **av = NULL, **bv = NULL;
size_t na = 0, nb, from, i;
if ((b = strdup(now)) == NULL)
return;
nb = split(b, &bv);
if (before != NULL && before[0] != '\0') {
a = strdup(before);
if (a != NULL)
na = split(a, &av);
}
from = (na > 0 && bv != NULL) ? first_new(av, na, bv, nb) : 0;
if (bv != NULL && nb - from > SHOWN_MAX) {
sp_buf_printf(out, "[%zu earlier lines omitted]\n",
nb - from - SHOWN_MAX);
from = nb - SHOWN_MAX;
}
for (i = from; bv != NULL && i < nb; i++)
sp_buf_printf(out, "%s\n", bv[i]);
free(av);
free(bv);
free(a);
free(b);
}
/* Capture, report what is new since the last capture, remember this one. */
static int
report_new(struct sp_buf *out)
{
char *now = capture(HISTORY);
struct sp_buf d;
if (now == NULL)
return sp_buf_puts(out, "error: the tmux pane is gone\n");
if (sp_buf_init(&d) != 0) {
free(now);
return -1;
}
delta(last, now, &d);
free(last);
last = now;
if (d.len == 0)
sp_buf_puts(out, "(no new output)\n");
else
sp_buf_add(out, d.p, d.len);
sp_buf_free(&d);
return 0;
}
/*
* Wait until the pane differs from `base`, then until it has stayed the same for
* quiet_ms, or until wait_ms is over. Returns 1 when something changed.
*/
static int
settle(const char *base, long wait_ms, long quiet_ms)
{
long start = now_ms(), changed_at = 0;
char *prev = base != NULL ? strdup(base) : strdup("");
int changed = 0;
while (prev != NULL && now_ms() - start < wait_ms) {
char *cur;
pause_ms(POLL_MS);
if ((cur = capture(HISTORY)) == NULL)
break;
if (strcmp(cur, prev) != 0) {
changed = 1;
changed_at = now_ms();
free(prev);
prev = cur;
continue;
}
free(cur);
if (changed && now_ms() - changed_at >= quiet_ms)
break;
}
free(prev);
return changed;
}
/* ------------------------------------------------------------ operations */
static int
need_target(struct sp_buf *out)
{
if (target[0] == '\0')
return !sp_buf_puts(out, "error: no tmux target; use tmux_start "
"or /tmux attach\n");
if (!alive())
return !sp_buf_printf(out, "error: tmux session '%s' is gone\n",
target);
return 1;
}
static int
op_start(const char *cmd, const char *name, struct sp_buf *out)
{
char auto_name[NAME_MAX_LEN];
char *const argv[] = { "tmux", "new-session", "-d", "-x", "200", "-y",
"50", "-s", auto_name, "sh", "-c", (char *)cmd, NULL };
struct sp_buf o;
int rc;
if (cmd == NULL || cmd[0] == '\0')
return sp_buf_puts(out, "error: cmd required\n");
if (name == NULL || name[0] == '\0')
snprintf(auto_name, sizeof(auto_name), "splinter-%d",
(int)getpid());
else if (!safe_name(name, sizeof(auto_name)) || strchr(name, ':') != NULL)
return sp_buf_puts(out, "error: name may use letters, digits "
"and _ . - only\n");
else
snprintf(auto_name, sizeof(auto_name), "%s", name);
if (sp_buf_init(&o) != 0)
return -1;
snprintf(target, sizeof(target), "%s", auto_name);
if (alive()) {
set_target(auto_name);
sp_buf_free(&o);
return sp_buf_printf(out, "session %s exists, attached\n",
auto_name);
}
rc = run(argv, &o);
if (rc != 0) {
target[0] = '\0';
sp_buf_printf(out, "error: tmux new-session failed (%d): %s",
rc, o.p != NULL ? o.p : "\n");
sp_buf_free(&o);
return 0;
}
sp_buf_free(&o);
set_target(auto_name);
free(last);
last = capture(HISTORY);
return sp_buf_printf(out, "target is %s (cmd: %s)\n", target, cmd);
}
/* Named keys only: letters, digits and - + _, so none can be an option. */
static int
safe_keys(const char *keys)
{
size_t i;
for (i = 0; keys[i] != '\0'; i++)
if (!isalnum((unsigned char)keys[i]) && keys[i] != ' ' &&
keys[i] != '-' && keys[i] != '_' && keys[i] != '+')
return 0;
return keys[0] != '-';
}
static int
send_text(const char *text, int enter, struct sp_buf *out)
{
char *const lit[] = { "tmux", "send-keys", "-t", target, "-l", "--",
(char *)text, NULL };
char *const ret[] = { "tmux", "send-keys", "-t", target, "Enter", NULL };
if (text[0] != '\0' && run(lit, out) != 0)
return -1;
return enter ? run(ret, out) : 0;
}
static int
send_keys(const char *keys, struct sp_buf *out)
{
char *copy = strdup(keys), *argv[40], *save = NULL, *tok;
int n = 0, rc;
if (copy == NULL)
return -1;
argv[n++] = "tmux";
argv[n++] = "send-keys";
argv[n++] = "-t";
argv[n++] = target;
for (tok = strtok_r(copy, " ", &save); tok != NULL && n < 38;
tok = strtok_r(NULL, " ", &save))
argv[n++] = tok;
argv[n] = NULL;
rc = n > 4 ? run(argv, out) : 0;
free(copy);
return rc;
}
static int
op_send(const char *text, const char *keys, int enter, struct sp_buf *out)
{
if (!need_target(out))
return 0;
if ((text == NULL || text[0] == '\0') && (keys == NULL || keys[0] == '\0'))
return sp_buf_puts(out, "error: text or keys required\n");
if (keys != NULL && !safe_keys(keys))
return sp_buf_puts(out, "error: keys are names such as C-c, "
"Up, Escape\n");
if (text != NULL && text[0] != '\0' &&
send_text(text, keys == NULL || keys[0] == '\0' ? enter : 0, out) != 0)
return sp_buf_puts(out, "error: tmux send-keys failed\n");
if (keys != NULL && keys[0] != '\0' && send_keys(keys, out) != 0)
return sp_buf_puts(out, "error: tmux send-keys failed\n");
return sp_buf_puts(out, "sent\n");
}
static int
op_ask(const char *text, long wait_ms, long quiet_ms, struct sp_buf *out)
{
char *before;
if (!need_target(out))
return 0;
if (text == NULL || text[0] == '\0')
return sp_buf_puts(out, "error: text required\n");
if ((before = capture(HISTORY)) == NULL)
return sp_buf_puts(out, "error: the tmux pane is gone\n");
free(last);
last = before;
if (send_text(text, 1, out) != 0)
return sp_buf_puts(out, "error: tmux send-keys failed\n");
if (!settle(before, wait_ms, quiet_ms))
sp_buf_puts(out, "(timeout: the pane did not change; it may be "
"busy or waiting; try tmux_read or tmux_capture)\n");
return report_new(out);
}
static int
op_read(long wait_ms, long quiet_ms, struct sp_buf *out)
{
if (!need_target(out))
return 0;
if (wait_ms > 0 && last != NULL)
(void)settle(last, wait_ms, quiet_ms);
return report_new(out);
}
static int
op_capture(int lines, struct sp_buf *out)
{
char *s;
if (!need_target(out))
return 0;
if ((s = capture(lines)) == NULL)
return sp_buf_puts(out, "error: the tmux pane is gone\n");
sp_buf_printf(out, "[pane %s, last %d lines]\n%s\n", target, lines, s);
free(s);
return 0;
}
static int
op_status(struct sp_buf *out)
{
return sp_buf_printf(out, "tmux target: %s%s\n",
target[0] != '\0' ? target : "(none)",
target[0] == '\0' ? "" : alive() ? " (alive)" : " (gone)");
}
/* ----------------------------------------------------------------- tools */
static long
clamp(long v, long lo, long hi, long dflt)
{
if (v == -1)
return dflt;
return v < lo ? lo : v > hi ? hi : v;
}
static mcp_json *
parse(const char *args)
{
return mcp_parse(args != NULL ? args : "{}");
}
#define WITH_ARGS(a, out) \
if (((a) = parse(args)) == NULL) \
return sp_buf_puts((out), "error: arguments are not JSON\n")
static int
tool_start(const char *args, struct sp_buf *out, void *ctx)
{
mcp_json *a;
int rc;
(void)ctx;
WITH_ARGS(a, out);
rc = op_start(mcp_arg_string(a, "cmd"), mcp_arg_string(a, "name"), out);
mcp_json_free(a);
return rc;
}
static int
tool_send(const char *args, struct sp_buf *out, void *ctx)
{
mcp_json *a;
int rc;
(void)ctx;
WITH_ARGS(a, out);
rc = op_send(mcp_arg_string(a, "text"), mcp_arg_string(a, "keys"),
mcp_arg_bool(a, "enter", 1), out);
mcp_json_free(a);
return rc;
}
static int
tool_ask(const char *args, struct sp_buf *out, void *ctx)
{
mcp_json *a;
int rc;
(void)ctx;
WITH_ARGS(a, out);
rc = op_ask(mcp_arg_string(a, "text"),
clamp(mcp_arg_int(a, "wait_ms", -1), 1000, WAIT_CAP, WAIT_DEFAULT),
clamp(mcp_arg_int(a, "quiet_ms", -1), 200, 30000, QUIET_DEFAULT),
out);
mcp_json_free(a);
return rc;
}
static int
tool_read(const char *args, struct sp_buf *out, void *ctx)
{
mcp_json *a;
int rc;
(void)ctx;
WITH_ARGS(a, out);
rc = op_read(clamp(mcp_arg_int(a, "wait_ms", -1), 0, WAIT_CAP, 0),
QUIET_DEFAULT, out);
mcp_json_free(a);
return rc;
}
static int
tool_capture(const char *args, struct sp_buf *out, void *ctx)
{
mcp_json *a;
int rc;
(void)ctx;
WITH_ARGS(a, out);
rc = op_capture((int)clamp(mcp_arg_int(a, "lines", -1), 1, 5000, 200),
out);
mcp_json_free(a);
return rc;
}
static int
tool_status(const char *args, struct sp_buf *out, void *ctx)
{
(void)args;
(void)ctx;
return op_status(out);
}
/* --------------------------------------------------------------- command */
static const char *
skip(const char *s)
{
while (*s == ' ')
s++;
return s;
}
static int
word(const char *args, const char *name, const char **rest)
{
size_t n = strlen(name);
if (strncmp(args, name, n) != 0 || (args[n] != '\0' && args[n] != ' '))
return 0;
*rest = skip(args + n);
return 1;
}
static int
command(struct sp_host *h, const char *args, struct sp_buf *out, void *ctx)
{
const char *r;
(void)ctx;
host_ref = h;
if (args == NULL || args[0] == '\0' || word(args, "status", &r))
return op_status(out);
if (word(args, "start", &r))
return op_start(r, NULL, out);
if (word(args, "attach", &r)) {
if (!safe_name(r, sizeof(target)))
return sp_buf_puts(out, "error: not a tmux target name\n");
set_target(r);
return op_status(out);
}
if (word(args, "send", &r))
return op_send(r, NULL, 1, out);
if (word(args, "keys", &r))
return op_send(NULL, r, 0, out);
if (word(args, "ask", &r))
return op_ask(r, WAIT_DEFAULT, QUIET_DEFAULT, out);
if (word(args, "read", &r))
return op_read(clamp(*r != '\0' ? atol(r) : -1, 0, WAIT_CAP, 0),
QUIET_DEFAULT, out);
if (word(args, "capture", &r))
return op_capture((int)clamp(*r != '\0' ? atol(r) : -1, 1, 5000,
200), out);
if (word(args, "stop", &r)) {
char *const argv[] = { "tmux", "kill-session", "-t", target, NULL };
if (target[0] == '\0')
return sp_buf_puts(out, "no target\n");
(void)run(argv, NULL);
sp_buf_printf(out, "killed %s\n", target);
target[0] = '\0';
free(last);
last = NULL;
return 0;
}
return sp_buf_puts(out, "usage: /tmux start <cmd> | attach <target> | "
"send <text> | keys <names> | ask <text> | read [wait_ms] | "
"capture [lines] | status | stop\n");
}
static int
tick(struct sp_host *h, const char *name, void *ctx)
{
char b[160];
(void)name;
(void)ctx;
host_ref = h;
snprintf(b, sizeof(b), "%.100s", target);
sp_host_widget_text(h, "tmux", b);
return 0;
}
static const struct sp_tool t_start = {
"tmux_start",
"Start a shell command in a detached tmux terminal and make it the "
"target. For programs that need a terminal: REPLs, installers, TUIs. "
"Use run for anything that just prints and exits.",
"{\"type\":\"object\",\"properties\":{\"cmd\":{\"type\":\"string\"},"
"\"name\":{\"type\":\"string\",\"description\":\"session name, "
"letters digits _ . -\"}},\"required\":[\"cmd\"]}",
SP_AUTH_DESTRUCTIVE, tool_start, NULL
};
static const struct sp_tool t_send = {
"tmux_send",
"Type into the target terminal. text is typed literally and followed by "
"Enter (enter=false to leave it off); keys are tmux key names such as "
"C-c, Escape, Up, separated by spaces.",
"{\"type\":\"object\",\"properties\":{\"text\":{\"type\":\"string\"},"
"\"keys\":{\"type\":\"string\"},\"enter\":{\"type\":\"boolean\"}}}",
SP_AUTH_MUTATE, tool_send, NULL
};
static const struct sp_tool t_ask = {
"tmux_ask",
"Type a line into the target terminal, wait until it has new output and "
"has been quiet for quiet_ms, and return the new lines. Gives up after "
"wait_ms (default 15000) and says so.",
"{\"type\":\"object\",\"properties\":{\"text\":{\"type\":\"string\"},"
"\"wait_ms\":{\"type\":\"integer\"},\"quiet_ms\":{\"type\":\"integer\"}},"
"\"required\":[\"text\"]}",
SP_AUTH_MUTATE, tool_ask, NULL
};
static const struct sp_tool t_read = {
"tmux_read",
"The lines on the target terminal that are new since the last "
"tmux_ask or tmux_read. wait_ms waits for something to appear first.",
"{\"type\":\"object\",\"properties\":{\"wait_ms\":{\"type\":\"integer\"}}}",
SP_AUTH_READ, tool_read, NULL
};
static const struct sp_tool t_capture = {
"tmux_capture",
"The target terminal as it is now: the last `lines` lines (default 200).",
"{\"type\":\"object\",\"properties\":{\"lines\":{\"type\":\"integer\"}}}",
SP_AUTH_READ, tool_capture, NULL
};
static const struct sp_tool t_status = {
"tmux_status",
"The current tmux target and whether its session is alive.",
"{\"type\":\"object\",\"properties\":{}}",
SP_AUTH_READ, tool_status, NULL
};
static const struct sp_widget tmux_widget = { "tmux", tick, NULL, NULL, 5000 };
static int
init(struct sp_host *h)
{
if (!have_tmux())
return -1; /* no tmux: the plugin does not load */
host_ref = h;
restore_target();
sp_host_add_tool(h, &t_start);
sp_host_add_tool(h, &t_send);
sp_host_add_tool(h, &t_ask);
sp_host_add_tool(h, &t_read);
sp_host_add_tool(h, &t_capture);
sp_host_add_tool(h, &t_status);
sp_host_add_command(h, "tmux", command, NULL);
sp_host_add_widget(h, &tmux_widget);
return 0;
}
static void
shutdown_plugin(struct sp_host *h)
{
(void)h;
free(last);
last = NULL;
}
static const struct sp_plugin plugin = {
SP_PLUGIN_ABI, "tmux", "0.3", init, NULL, shutdown_plugin
};
const struct sp_plugin *
sp_plugin_describe(void)
{
return &plugin;
}
-14
View File
@@ -1310,7 +1310,6 @@ run_child_fb(const struct del_ctx *c, const struct sp_delegate **dp,
static const struct sp_delegate *find_delegate(const struct sp_config *cfg, const char *id);
static int looks_transient(const char *e);
static int looks_quota_wait(const char *e, long *ready_at);
static void link_plan_quota(struct del_ctx *c, const char *plan_task_id, const char *run_after, const char *job_id);
static int
tool_delegate(const char *args, struct sp_buf *out, void *ctx)
@@ -1445,7 +1444,6 @@ tool_delegate(const char *args, struct sp_buf *out, void *ctx)
if (ra>0) { snprintf(jr.run_after,sizeof(jr.run_after),"%ld",ra); snprintf(jr.next_poll_at,sizeof(jr.next_poll_at),"%ld",ra); }
else snprintf(jr.next_poll_at,sizeof(jr.next_poll_at),"%ld",(long)time(NULL)+3600);
jobrec_write(c, &jr);
link_plan_quota(c, jr.plan_task_id, jr.run_after[0]?jr.run_after:jr.next_poll_at, jr.id);
sp_buf_printf(out, "quota: %s\n[job %s: waiting_quota run_after=%s, auto-scheduled]\n", err.p?err.p:"quota", jr.id, jr.run_after);
} else if (looks_transient(err.p)) {
snprintf(jr.state, sizeof(jr.state), "waiting_poll");
@@ -2195,17 +2193,6 @@ sp_delegate_tick(struct sp_buf *out)
}
static void
link_plan_quota(struct del_ctx *c, const char *plan_task_id, const char *run_after, const char *job_id)
{
struct sp_buf out, args;
if (!c || !c->host || !plan_task_id || !*plan_task_id) return;
if (sp_buf_init(&out)!=0 || sp_buf_init(&args)!=0) return;
sp_buf_printf(&args, "wait %s %s job:%s", plan_task_id,
(run_after && *run_after) ? run_after : "+3600", job_id ? job_id : "");
(void)sp_host_command(c->host, "tasks", args.p ? args.p : "", &out);
sp_buf_free(&out); sp_buf_free(&args);
}
int
sp_delegate_retry_job(const char *id, int force, struct sp_buf *out)
@@ -2287,7 +2274,6 @@ sp_delegate_retry_job(const char *id, int force, struct sp_buf *out)
snprintf(jr.state,sizeof(jr.state),"waiting_quota");
if(ra>0){snprintf(jr.run_after,sizeof(jr.run_after),"%ld",ra); snprintf(jr.next_poll_at,sizeof(jr.next_poll_at),"%ld",ra);}
else snprintf(jr.next_poll_at,sizeof(jr.next_poll_at),"%ld",(long)time(NULL)+3600);
link_plan_quota(c, jr.plan_task_id, jr.run_after[0]?jr.run_after:jr.next_poll_at, jr.id);
} else if (looks_transient(err.p)) {
snprintf(jr.state,sizeof(jr.state),"waiting_poll");
snprintf(jr.next_poll_at,sizeof(jr.next_poll_at),"%ld",(long)time(NULL)+60);
-2
View File
@@ -520,8 +520,6 @@ sp_call_summary(const char *name, const char *args, char *out, size_t cap)
if (detail[0] == '\0')
pretty_args(args != NULL ? args : "", detail, sizeof(detail));
snprintf(out, cap, "%s", name);
if (out[0] >= 'a' && out[0] <= 'z')
out[0] = (char)(out[0] - 'a' + 'A');
n = strlen(out);
if (*detail != '\0' && n + 2 < cap)
snprintf(out + n, cap - n, " %s", detail);
+9 -6
View File
@@ -372,7 +372,6 @@ main(void)
const char *oldpath = getenv("PATH");
const struct sp_tool *t;
FILE *f;
size_t n;
CHECK(mkdtemp(tdir) != NULL);
snprintf(path, sizeof(path), "%s/ssh", tdir);
@@ -397,7 +396,7 @@ main(void)
CHECK(sp_buf_init(&err) == 0);
host = sp_host_new(&r, &ag, NULL);
CHECK(host != NULL);
CHECK(sp_plugin_load(host, "plugins/tasks/tasks.so", &err) == 0);
CHECK(sp_plugin_load(host, "plugins/processes/processes.so", &err) == 0);
CHECK(sp_delegate_register(&r, &c, host) == 0);
t = sp_registry_find(&r, "delegate");
CHECK(t != NULL);
@@ -406,12 +405,16 @@ main(void)
&out, t->ctx);
CHECK(out.p != NULL && strstr(out.p, "antwort vom panel") != NULL);
n = sp_host_widget_snapshot(host, snap, sizeof(snap));
(void)sp_host_widget_snapshot(host, snap, sizeof(snap));
if (getenv("TEST_VERBOSE") != NULL)
fprintf(stderr, "panel: %s\n", snap);
CHECK(n > 0);
CHECK(strstr(snap, "delegate:panel") != NULL);
CHECK(strstr(snap, "exit 0") != NULL);
/*
* The work is finished: the sidebar only shows running work, so
* the delegate row is gone (its output is still in the tool
* result and in task_result).
*/
CHECK(strstr(snap, "delegate:panel") == NULL);
CHECK(strstr(snap, "exit 0") == NULL);
CHECK(sp_host_meter(host)->tasks_running == 0);
sp_host_free(host);
+2 -2
View File
@@ -308,10 +308,10 @@ main(void)
/* the shaping both front ends print */
sp_call_summary("run", "{\"command\":\"ls -l\"}", call, sizeof(call));
CHECK(strcmp(call, "Run ls -l") == 0);
CHECK(strcmp(call, "run ls -l") == 0);
sp_call_summary("read", "{\"path\":\"src/x.c\",\"limit\":10}", call,
sizeof(call));
CHECK(strcmp(call, "Read src/x.c") == 0);
CHECK(strcmp(call, "read src/x.c") == 0);
sp_buf_clear(&out);
CHECK(sp_obs_short("[exit status 0]", &out) == SP_OBS_JOIN);
CHECK(strcmp(out.p, "[exit status 0]") == 0);
-147
View File
@@ -1,147 +0,0 @@
/*
* splinter -- planner plugin smoke test.
*
* The plan belongs to the session: the tools add, list and update tasks, the
* list is on disk, and a second reader sees the same file. This is not the
* background-task store; that one owns processes.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "splinter.h"
#include "splinter_plugin.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
int
main(void)
{
struct sp_session_info sess;
struct sp_registry reg;
struct sp_agent ag;
struct sp_host *host;
struct sp_buf out, err;
const struct sp_tool *t;
char dir[] = "/tmp/splinter-plan-XXXXXX";
char path[600], snap[512], text[2048];
FILE *f;
size_t n;
CHECK(mkdtemp(dir) != NULL);
memset(&sess, 0, sizeof(sess));
snprintf(sess.dir, sizeof(sess.dir), "%s", dir);
memset(&reg, 0, sizeof(reg));
memset(&ag, 0, sizeof(ag));
CHECK(sp_buf_init(&out) == 0);
CHECK(sp_buf_init(&err) == 0);
host = sp_host_new(&reg, &ag, &sess);
CHECK(host != NULL);
CHECK(sp_plugin_load(host, "plugins/planner/planner.so", &err) == 0);
t = sp_registry_find(&reg, "plan_task_add");
CHECK(t != NULL);
if (t != NULL)
(void)t->run("{\"description\":\"write the planner test\"}",
&out, t->ctx);
CHECK(out.p != NULL && strstr(out.p, "added plan task #1") != NULL);
sp_buf_clear(&out);
t = sp_registry_find(&reg, "plan_task_list");
CHECK(t != NULL);
if (t != NULL)
(void)t->run("{}", &out, t->ctx);
CHECK(out.p != NULL &&
strstr(out.p, "write the planner test") != NULL);
CHECK(out.p != NULL && strstr(out.p, "[todo]") != NULL);
sp_buf_clear(&out);
t = sp_registry_find(&reg, "plan_task_update");
CHECK(t != NULL);
if (t != NULL)
(void)t->run("{\"id\":\"1\",\"status\":\"doing\"}", &out,
t->ctx);
CHECK(out.p != NULL && strstr(out.p, "[doing]") != NULL);
/* the command path is the same store */
sp_buf_clear(&out);
CHECK(sp_host_command(host, "tasks", "ls", &out) == 0);
CHECK(out.p != NULL &&
strstr(out.p, "write the planner test") != NULL);
/* the file is the contract: a second reader sees it */
snprintf(path, sizeof(path), "%s/plan_tasks.json", dir);
f = fopen(path, "r");
CHECK(f != NULL);
if (f != NULL) {
n = fread(text, 1, sizeof(text) - 1, f);
text[n] = '\0';
fclose(f);
CHECK(strstr(text, "write the planner test") != NULL);
CHECK(strstr(text, "\"doing\"") != NULL);
}
/* the schema says id is an integer; a model sends it as one */
sp_buf_clear(&out);
t = sp_registry_find(&reg, "plan_task_update");
(void)t->run("{\"id\":1,\"note\":\"half done\"}", &out, t->ctx);
CHECK(strstr(out.p, "task #1") != NULL);
/* a state that does not exist is refused, not stored */
sp_buf_clear(&out);
(void)t->run("{\"id\":1,\"status\":\"finished\"}", &out, t->ctx);
CHECK(strstr(out.p, "error: status is one of") != NULL);
sp_buf_clear(&out);
(void)t->run("{\"id\":99,\"status\":\"done\"}", &out, t->ctx);
CHECK(strstr(out.p, "no task #99") != NULL);
/* quotes, backslashes and newlines survive the file */
sp_buf_clear(&out);
t = sp_registry_find(&reg, "plan_task_add");
(void)t->run("{\"description\":\"say \\\"hi\\\" in C:\\\\tmp\\nthen stop\"}",
&out, t->ctx);
CHECK(strstr(out.p, "added plan task #2") != NULL);
sp_buf_clear(&out);
t = sp_registry_find(&reg, "plan_task_list");
(void)t->run("{}", &out, t->ctx);
CHECK(strstr(out.p, "say \"hi\" in C:\\tmp\nthen stop") != NULL);
/* a parked task waits, and the other command forms work */
sp_buf_clear(&out);
CHECK(sp_host_command(host, "tasks", "wait 2 +3600 job:abc", &out) == 0);
CHECK(strstr(out.p, "task #2 waiting_quota") != NULL);
sp_buf_clear(&out);
CHECK(sp_host_command(host, "tasks", "ls", &out) == 0);
CHECK(strstr(out.p, "[waiting_quota]") != NULL);
CHECK(strstr(out.p, "# job:abc") != NULL);
sp_buf_clear(&out);
CHECK(sp_host_command(host, "tasks", "done 1", &out) == 0);
CHECK(strstr(out.p, "task #1 done") != NULL);
sp_buf_clear(&out);
CHECK(sp_host_command(host, "tasks", "next", &out) == 0);
CHECK(strstr(out.p, "no todo tasks") != NULL);
n = sp_host_widget_snapshot(host, snap, sizeof(snap));
CHECK(n > 0);
CHECK(strstr(snap, "plan:") != NULL);
sp_host_free(host);
sp_registry_free(&reg);
sp_buf_free(&out);
sp_buf_free(&err);
snprintf(path, sizeof(path), "rm -rf '%s'", dir);
(void)system(path);
if (failures == 0)
printf("test_planner: ok\n");
return failures == 0 ? 0 : 1;
}
+7 -7
View File
@@ -1,6 +1,6 @@
/*
* splinter -- secrets stay out of argv and out of the model's shell, the MCP
* server does not talk to strangers, and task_kill does not kill splinter.
* server does not talk to strangers, and process_kill does not kill splinter.
*
* Each block is the failure it guards against, as it was found.
*/
@@ -254,7 +254,7 @@ test_mcp(void)
sp_registry_free(&r);
}
/* ---- tasks: task_kill stops the task, and only the task ---- */
/* ---- processes: process_kill stops the task, and only the task ---- */
static void
test_tasks(void)
{
@@ -272,9 +272,9 @@ test_tasks(void)
CHECK(host != NULL);
if (host == NULL)
return;
CHECK(sp_plugin_load(host, "plugins/tasks/tasks.so", &err) == 0);
CHECK(sp_plugin_load(host, "plugins/processes/processes.so", &err) == 0);
run_tool(&r, "task_start", "{\"command\":\"sleep 30\",\"label\":\"s\"}",
run_tool(&r, "process_start", "{\"command\":\"sleep 30\",\"label\":\"s\"}",
&out);
CHECK(strstr(out.p, "1") != NULL);
for (i = 0; i < 20; i++) { /* let the thread fork */
@@ -287,17 +287,17 @@ test_tasks(void)
* this test would have died on the next line. An integer id, as the
* schema declares it.
*/
run_tool(&r, "task_kill", "{\"id\":1}", &out);
run_tool(&r, "process_kill", "{\"id\":1}", &out);
CHECK(strstr(out.p, "kill signal sent") != NULL);
for (i = 0; i < 40; i++) {
struct timespec ts = { 0, 50 * 1000 * 1000 };
run_tool(&r, "task_list", "{}", &out);
run_tool(&r, "process_list", "{}", &out);
if (strstr(out.p, "exit") != NULL || strstr(out.p, "fertig") != NULL)
break;
(void)nanosleep(&ts, NULL);
}
run_tool(&r, "task_kill", "{\"id\":99}", &out);
run_tool(&r, "process_kill", "{\"id\":99}", &out);
CHECK(strstr(out.p, "no such task") != NULL);
sp_host_free(host);
-134
View File
@@ -1,134 +0,0 @@
/*
* splinter -- tmux plugin test.
*
* Drives a real tmux with `cat`: whatever is typed comes back twice (the tty
* echoes it, cat prints it), which is enough to prove the three things that
* matter - a tool reaches the terminal, "new output" excludes what was already
* seen, and a name or key that could be an option is refused. Without tmux the
* test says so and passes; the plugin does not load there either.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "splinter.h"
#include "splinter_plugin.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static const struct sp_tool *reg_tool;
static const char *
call(const struct sp_registry *reg, const char *tool, const char *args,
struct sp_buf *out)
{
const struct sp_tool *t = sp_registry_find(reg, tool);
sp_buf_clear(out);
reg_tool = t;
CHECK(t != NULL);
if (t != NULL)
(void)t->run(args, out, t->ctx);
return out->p != NULL ? out->p : "";
}
int
main(void)
{
struct sp_session_info sess;
struct sp_registry reg;
struct sp_agent ag;
struct sp_host *host;
struct sp_buf out, err;
char dir[] = "/tmp/splinter-tmux-XXXXXX";
char name[64], args[256];
const char *s;
CHECK(mkdtemp(dir) != NULL);
/*
* A tmux server of our own. $TMUX (set when this runs inside the
* user's tmux) wins over TMUX_TMPDIR, and without either the test
* would create its session on the user's server - a stray window, and
* keys meant for `cat` could land in a real pane.
*/
unsetenv("TMUX");
unsetenv("TMUX_PANE");
setenv("TMUX_TMPDIR", dir, 1);
memset(&sess, 0, sizeof(sess));
snprintf(sess.dir, sizeof(sess.dir), "%s", dir);
memset(&reg, 0, sizeof(reg));
memset(&ag, 0, sizeof(ag));
CHECK(sp_buf_init(&out) == 0);
CHECK(sp_buf_init(&err) == 0);
host = sp_host_new(&reg, &ag, &sess);
CHECK(host != NULL);
if (sp_plugin_load(host, "plugins/tmux/tmux.so", &err) != 0) {
printf("test_tmux: skipped (no tmux)\n");
goto done;
}
CHECK(sp_registry_find(&reg, "tmux_watch") == NULL);
/* nothing is targeted yet: the text must not reach any terminal */
s = call(&reg, "tmux_ask", "{\"text\":\"probe\"}", &out);
CHECK(strstr(s, "error: no tmux target") != NULL);
/* a name that could be an option, a second target or a command */
s = call(&reg, "tmux_start", "{\"cmd\":\"cat\",\"name\":\"-t evil\"}", &out);
CHECK(strstr(s, "error: name may use") != NULL);
s = call(&reg, "tmux_start", "{\"cmd\":\"cat\",\"name\":\"a;b\"}", &out);
CHECK(strstr(s, "error: name may use") != NULL);
snprintf(name, sizeof(name), "splinter-test-%d", (int)getpid());
snprintf(args, sizeof(args), "{\"cmd\":\"cat\",\"name\":\"%s\"}", name);
s = call(&reg, "tmux_start", args, &out);
CHECK(strstr(s, "target is") != NULL);
s = call(&reg, "tmux_status", "{}", &out);
CHECK(strstr(s, name) != NULL && strstr(s, "alive") != NULL);
s = call(&reg, "tmux_ask",
"{\"text\":\"ping\",\"wait_ms\":5000,\"quiet_ms\":400}", &out);
CHECK(strstr(s, "ping") != NULL);
/* the second answer is only what is new: no "ping" in it */
s = call(&reg, "tmux_ask",
"{\"text\":\"pong\",\"wait_ms\":5000,\"quiet_ms\":400}", &out);
CHECK(strstr(s, "pong") != NULL);
CHECK(strstr(s, "ping") == NULL);
/* and with nothing typed there is nothing new */
s = call(&reg, "tmux_read", "{}", &out);
CHECK(strstr(s, "(no new output)") != NULL);
s = call(&reg, "tmux_capture", "{\"lines\":50}", &out);
CHECK(strstr(s, "ping") != NULL && strstr(s, "pong") != NULL);
/* keys are names; anything that looks like an option is refused */
s = call(&reg, "tmux_send", "{\"keys\":\"-t other\"}", &out);
CHECK(strstr(s, "error: keys are names") != NULL);
s = call(&reg, "tmux_send", "{\"keys\":\"C-d\"}", &out);
CHECK(strstr(s, "sent") != NULL);
CHECK(sp_host_command(host, "tmux", "stop", &out) == 0);
done:
sp_host_free(host);
sp_registry_free(&reg);
sp_buf_free(&out);
sp_buf_free(&err);
/* only the private server: TMUX_TMPDIR points into dir */
(void)system("tmux kill-server 2>/dev/null");
snprintf(args, sizeof(args), "rm -rf '%s'", dir);
(void)system(args);
if (failures == 0)
printf("test_tmux: ok\n");
return failures == 0 ? 0 : 1;
}
+3 -3
View File
@@ -119,12 +119,12 @@ check shows 'No configuration directory to write to.'
typed 'schreib die datei'; key Enter
check shows 'changes files'
# the question names the call - verb, path, class - and never the arguments
# as the model sent them ("Write /tmp/x/out.txt", not write {"path": ...})
check shows 'Write '
# as the model sent them ("write /tmp/x/out.txt", not write {"path": ...})
check shows 'write '
check not_shows '{"path"'
key y
check shows 'fertig'
check shows '>> Write '
check shows '>> write '
# the status bar above the prompt: context gauge, cache share, speed
check shows 'ctx 1290/131072'
check shows 'cache 62%'