diff --git a/include/splinter.h b/include/splinter.h index 34b44b0..50ca26d 100644 --- a/include/splinter.h +++ b/include/splinter.h @@ -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, diff --git a/include/splinter_plugin.h b/include/splinter_plugin.h index 2f53cfb..0278797 100644 --- a/include/splinter_plugin.h +++ b/include/splinter_plugin.h @@ -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); diff --git a/plugins/tasks/tasks.c b/plugins/tasks/tasks.c index bb31ff6..d2630c4 100644 --- a/plugins/tasks/tasks.c +++ b/plugins/tasks/tasks.c @@ -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; } diff --git a/src/delegate.c b/src/delegate.c index 66e5976..ecf7d01 100644 --- a/src/delegate.c +++ b/src/delegate.c @@ -30,6 +30,7 @@ #include #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"); diff --git a/src/main.c b/src/main.c index 328968a..a6ec6b3 100644 --- a/src/main.c +++ b/src/main.c @@ -536,7 +536,7 @@ main(int argc, char **argv) fprintf(stderr, "splinter: cannot register folder_history\n"); return 3; } - if (sp_delegate_register(®, &conf) != 0) { + if (sp_delegate_register(®, &conf, host) != 0) { fprintf(stderr, "splinter: cannot register delegate\n"); return 3; } diff --git a/src/plugin.c b/src/plugin.c index d297f8e..04863a8 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -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) {