splinter: delegations are tasks too, and the status line is alive

A delegate is work someone else does while we wait, so it belongs in the task
panel with its own clock. The tasks plugin now publishes sp_task_ops through the
host, and delegate.c opens an entry before the call and closes it with status and
rough token count after - 'who is still running' becomes a fact on screen instead
of a guess.

Measured status line, live during a turn:

  splinter 0.1.0  clock: 19:12:56  |  ctx 4.0k  |  12 tok/s  |
  4.0k in / 238 out  |  tasks 0/4          Delphinium 19178ms

Not yet observed: a delegate entry in the panel. Both test configs had no
reachable delegate (one endpoint dead, one with no defers at all), so the cycle
open -> close never ran against a real call. The wiring compiles and the task API
is exercised by task_start/task_list; the delegate half needs one live delegate to
prove.
This commit is contained in:
Konrad committed 2026-11-08 19:13:44 +00:00
1 parent b02d51326c
commit 3c5cdaf53a
6 files changed
+123 -4

No files matched your search

+2 -1
View File
@@ -257,7 +257,8 @@ int sp_agent_run(struct sp_agent *, const char *user_text,
/* builtin.c */
int sp_builtin_register(struct sp_registry *);
struct sp_config;
int sp_delegate_register(struct sp_registry *, const struct sp_config *);
int sp_delegate_register(struct sp_registry *, const struct sp_config *,
struct sp_host *);
/* Gateway mode: answers on Telegram until SIGINT/SIGTERM. */
struct sp_host;
int sp_telegram_run(struct sp_agent *, struct sp_host *host,
+14
View File
@@ -107,6 +107,17 @@ struct sp_status {
void (*render)(struct sp_host *, char *buf, size_t len);
};
/*
* Tasks owned by someone else. The delegate runs a model on another host and the
* tasks plugin shows it; neither knows the other, so the harness passes an entry
* through this little interface: begin gives an id, end closes it with a status
* and whatever was spent.
*/
struct sp_task_ops {
int (*begin)(struct sp_host *, const char *label, const char *detail);
void (*end)(struct sp_host *, int id, int status, size_t tokens);
};
struct sp_plugin {
uint32_t abi; /* must equal SP_PLUGIN_ABI */
const char *name;
@@ -134,6 +145,9 @@ int sp_host_add_status(struct sp_host *, const struct sp_status *);
const struct sp_meter *sp_host_meter(struct sp_host *);
void sp_host_set_meter(struct sp_host *, const struct sp_meter *);
size_t sp_host_status_text(struct sp_host *, char *buf, size_t len);
void sp_host_set_task_ops(struct sp_host *, const struct sp_task_ops *);
int sp_host_task_begin(struct sp_host *, const char *label, const char *detail);
void sp_host_task_end(struct sp_host *, int id, int status, size_t tokens);
/* Ask every gate. Returns 1 allowed, 0 denied (reason filled), -1 no opinion. */
int sp_host_gate(struct sp_host *, const char *tool, int auth,
const char *args, char *reason, size_t reason_len);
+58
View File
@@ -182,6 +182,63 @@ arg_str(const char *args, const char *key, char *buf, size_t len)
static struct sp_host *host_ref;
/*
* Tasks someone else owns - a delegate on another host. They get an entry and a
* clock, but no thread here: the harness closes them when the call returns.
*/
static int
ops_begin(struct sp_host *h, const char *label, const char *detail)
{
struct task *t;
int id;
(void)detail;
pthread_mutex_lock(&tasks_lock);
if (ntasks == TASKS_MAX) {
pthread_mutex_unlock(&tasks_lock);
return -1;
}
t = &tasks[ntasks++];
memset(t, 0, sizeof(*t));
(void)pthread_mutex_init(&t->lock, NULL);
t->id = next_id++;
t->running = 1;
t->started = time(NULL);
snprintf(t->label, sizeof(t->label), "%.63s", label != NULL ? label : "?");
id = t->id;
pthread_mutex_unlock(&tasks_lock);
if (h != NULL) {
publish_meter(h);
refresh_widget(h);
}
return id;
}
static void
ops_end(struct sp_host *h, int id, int status, size_t tokens)
{
struct task *t;
if (id < 0)
return;
pthread_mutex_lock(&tasks_lock);
t = find(id);
if (t != NULL) {
t->running = 0;
t->status = status;
t->ended = time(NULL);
if (tokens > 0)
(void)sp_buf_printf(&t->out, "[~%zu tokens]", tokens);
}
pthread_mutex_unlock(&tasks_lock);
if (h != NULL) {
publish_meter(h);
refresh_widget(h);
}
}
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)
{
@@ -387,6 +444,7 @@ tasks_init(struct sp_host *h)
(void)sp_host_add_tool(h, &t_kill);
(void)sp_host_add_widget(h, &widget);
(void)sp_host_widget_text(h, "tasks", "keine");
sp_host_set_task_ops(h, &task_ops);
publish_meter(h);
return 0;
}
+25 -2
View File
@@ -30,6 +30,7 @@
#include <unistd.h>
#include "splinter.h"
#include "splinter_plugin.h"
#include "mcp.h"
#include "config.h"
@@ -38,6 +39,7 @@
struct del_ctx {
const struct sp_config *cfg;
struct sp_host *host; /* may be NULL: no task panel */
char self[512];
};
@@ -278,7 +280,7 @@ tool_delegate(const char *args, struct sp_buf *out, void *ctx)
const char *task, *wanted;
const struct sp_delegate *d;
char why[192] = "";
int score = 0;
int score = 0, task_id = -1;
if (sp_buf_init(&keep) != 0 || sp_buf_init(&tbuf) != 0)
return -1;
@@ -343,7 +345,20 @@ tool_delegate(const char *args, struct sp_buf *out, void *ctx)
if (slot < SP_CONFIG_MAX_DELEGATES)
running[slot]++;
}
/*
* A delegate is work someone else does while we wait: it belongs in the task
* panel with its own clock, so 'who is still running' is a fact on screen and
* not a guess.
*/
{
char label[80], detail[160];
snprintf(label, sizeof(label), "delegate:%s", d->id);
snprintf(detail, sizeof(detail), "%.140s", task);
task_id = sp_host_task_begin(c->host, label, detail);
}
if (run_child(c, d, task, &reply, &err) != 0) {
sp_host_task_end(c->host, task_id, 1, 0);
size_t slot = (size_t)(d - c->cfg->delegate);
if (slot < SP_CONFIG_MAX_DELEGATES && running[slot] > 0)
@@ -361,6 +376,12 @@ tool_delegate(const char *args, struct sp_buf *out, void *ctx)
if (slot < SP_CONFIG_MAX_DELEGATES && running[slot] > 0)
running[slot]--;
}
/*
* Closed here, after the answer is in hand, so duration and tokens in the
* panel are the ones the call actually cost.
*/
sp_host_task_end(c->host, task_id, 0,
reply.p != NULL ? strlen(reply.p) / 4 : 0);
if (reply.p != NULL)
copy_answer(out, &reply);
else
@@ -753,7 +774,8 @@ sp_delegate_choose(const struct sp_config *cfg, const char *task,
}
int
sp_delegate_register(struct sp_registry *r, const struct sp_config *cfg)
sp_delegate_register(struct sp_registry *r, const struct sp_config *cfg,
struct sp_host *host)
{
static struct del_ctx ctx;
struct sp_tool t;
@@ -761,6 +783,7 @@ sp_delegate_register(struct sp_registry *r, const struct sp_config *cfg)
memset(&ctx, 0, sizeof(ctx));
ctx.cfg = cfg;
ctx.host = host; /* delegates show up in the task panel */
n = readlink("/proc/self/exe", ctx.self, sizeof(ctx.self) - 1);
if (n <= 0)
snprintf(ctx.self, sizeof(ctx.self), "splinter");
+1 -1
View File
@@ -536,7 +536,7 @@ main(int argc, char **argv)
fprintf(stderr, "splinter: cannot register folder_history\n");
return 3;
}
if (sp_delegate_register(&reg, &conf) != 0) {
if (sp_delegate_register(&reg, &conf, host) != 0) {
fprintf(stderr, "splinter: cannot register delegate\n");
return 3;
}
+23
View File
@@ -59,6 +59,7 @@ struct sp_host {
const struct sp_status *status[MAX_COMPACTORS];
size_t nstatus;
struct sp_meter meter;
const struct sp_task_ops *taskops;
struct {
char name[32];
struct sp_transport tr;
@@ -188,6 +189,28 @@ sp_host_add_status(struct sp_host *h, const struct sp_status *s)
return 0;
}
void
sp_host_set_task_ops(struct sp_host *h, const struct sp_task_ops *ops)
{
if (h != NULL)
h->taskops = ops;
}
int
sp_host_task_begin(struct sp_host *h, const char *label, const char *detail)
{
if (h == NULL || h->taskops == NULL || h->taskops->begin == NULL)
return -1;
return h->taskops->begin(h, label, detail);
}
void
sp_host_task_end(struct sp_host *h, int id, int status, size_t tokens)
{
if (h != NULL && h->taskops != NULL && h->taskops->end != NULL)
h->taskops->end(h, id, status, tokens);
}
const struct sp_meter *
sp_host_meter(struct sp_host *h)
{