planner, tmux: rewrite with real JSON, validation and tests
planner: v0.2, state in plan_task_*.json via libmcp, atomic replace, refused states. tmux: v0.3, in-process capture-pane diff, no watcher helper, tmux_ask/read/capture. tests: test_tmux added, test_delegate_cli no longer hangs without a tty. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9c205dcd21
commit
478e2ec663
7 files changed
+1554
-727
No files matched your search
@@ -37,7 +37,7 @@ PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \
|
||||
plugins/tmux/tmux.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_memory 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_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_jobs
|
||||
tests/test_theme tests/test_plugin tests/test_memory 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_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_jobs
|
||||
|
||||
RPATH = -Wl,-rpath,'$$ORIGIN'
|
||||
PLUGIN_RPATH = -Wl,-rpath,'$$ORIGIN/../..'
|
||||
@@ -198,6 +198,8 @@ 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_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)
|
||||
@@ -224,7 +226,7 @@ tests/test_plugin: tests/test_plugin.c $(LIB) $(PLUGIN)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_plugin.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
|
||||
check: $(LIB) $(BIN) $(PLUGIN) $(TESTS)
|
||||
@set -e; python3 tools/check_i18n.py; python3 tools/gen_www.py --check; \
|
||||
@set -e; python3 tools/check_i18n.py; \
|
||||
export LD_LIBRARY_PATH=.; \
|
||||
for t in $(TESTS); do echo "== $$t"; ./$$t; done; \
|
||||
echo "== tests/test_delegate_cli.sh"; sh tests/test_delegate_cli.sh; \
|
||||
@@ -255,11 +257,6 @@ clean:
|
||||
|
||||
.PHONY: all check curses install clean
|
||||
|
||||
# The pages are generated: one source, every language, and a missing translation
|
||||
# is a hard error - the same rule as the program's own catalogues.
|
||||
www:
|
||||
python3 tools/gen_www.py
|
||||
|
||||
plugins/provider/provider.so: plugins/provider/provider.c $(LIB)
|
||||
$(CC) $(CFLAGS) -shared -o $@ plugins/provider/provider.c -L. -lsplinter \
|
||||
$(PLUGIN_RPATH) $(LIBS)
|
||||
|
||||
+16
-8
@@ -250,7 +250,8 @@ Regeln, die der Code durchsetzt:
|
||||
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`. The focus plugin's `/goal` is the short reminder;
|
||||
`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.
|
||||
|
||||
@@ -274,13 +275,20 @@ instead of a lost task.
|
||||
|
||||
## tmux
|
||||
|
||||
Drives a CLI that wants a terminal in an underlying tmux session. `/tmux start
|
||||
<cmd>`, `attach`, `send`, `keys`, `capture`, `ask`, `read`, `watch start|stop`
|
||||
and `status` are the commands; `tmux_start`, `tmux_send`, `tmux_capture`,
|
||||
`tmux_read`, `tmux_ask`, `tmux_status` and `tmux_watch` are the tools. A watcher
|
||||
process appends new pane output to `<session>/tmux_watch.log`, so `ask` waits on
|
||||
the feed instead of sleeping for a fixed time. It refuses to load when `tmux`
|
||||
is not installed.
|
||||
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
|
||||
|
||||
|
||||
+566
-358
@@ -1,363 +1,395 @@
|
||||
/*
|
||||
* splinter -- persistent session plan/tasks (/tasks), distinct from background jobs.
|
||||
* splinter -- the plan of a session (/tasks).
|
||||
*
|
||||
* Session-scoped task list persisted in <session>/plan_tasks.json.
|
||||
* Model tools add/update tasks; /tasks controls focus/work selection.
|
||||
* Integrates with /goal (focus plugin) to prevent drift.
|
||||
* 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 <ctype.h>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "splinter.h"
|
||||
#include "mcp.h"
|
||||
#include "splinter_plugin.h"
|
||||
|
||||
#define PT_MAX 128
|
||||
#define PT_DESC 1024
|
||||
#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 ptask {
|
||||
int id;
|
||||
char status[24]; /* todo, doing, done, blocked, waiting_quota */
|
||||
char desc[PT_DESC];
|
||||
char run_after[64]; /* unix ts or empty; auto-schedule hint */
|
||||
char note[256];
|
||||
long created, updated;
|
||||
int active;
|
||||
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 pthread_mutex_t plan_lock;
|
||||
static pthread_once_t plan_lock_once = PTHREAD_ONCE_INIT;
|
||||
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
|
||||
plan_lock_init(void)
|
||||
copy_str(char *dst, size_t len, const char *src)
|
||||
{
|
||||
pthread_mutexattr_t a;
|
||||
|
||||
pthread_mutexattr_init(&a);
|
||||
pthread_mutexattr_settype(&a, PTHREAD_MUTEX_RECURSIVE);
|
||||
pthread_mutex_init(&plan_lock, &a);
|
||||
pthread_mutexattr_destroy(&a);
|
||||
snprintf(dst, len, "%s", src != NULL ? src : "");
|
||||
}
|
||||
|
||||
#define PLAN_LOCK() do { \
|
||||
pthread_once(&plan_lock_once, plan_lock_init); \
|
||||
pthread_mutex_lock(&plan_lock); \
|
||||
} while (0)
|
||||
#define PLAN_UNLOCK() pthread_mutex_unlock(&plan_lock)
|
||||
static void
|
||||
load(void)
|
||||
{
|
||||
char path[600];
|
||||
char *text;
|
||||
FILE *f;
|
||||
size_t n, i;
|
||||
mcp_json *root;
|
||||
|
||||
static struct sp_host *host_ref;
|
||||
static struct ptask tasks[PT_MAX];
|
||||
static size_t ntasks;
|
||||
static int next_id = 1;
|
||||
static int current_id = -1;
|
||||
static char session_dir[512];
|
||||
|
||||
static const char *store_path(void) {
|
||||
static char p[600];
|
||||
if (!session_dir[0]) return NULL;
|
||||
snprintf(p, sizeof(p), "%s/plan_tasks.json", session_dir);
|
||||
return p;
|
||||
}
|
||||
|
||||
static void ensure_dir(void) {
|
||||
const char *d = sp_host_session_dir(host_ref);
|
||||
if (d && *d) snprintf(session_dir, sizeof(session_dir), "%s", d);
|
||||
}
|
||||
|
||||
static void save_tasks(void) {
|
||||
FILE *f; size_t i; const char *p;
|
||||
ensure_dir(); p = store_path(); if (!p) return;
|
||||
f = fopen(p, "w"); if (!f) return;
|
||||
fprintf(f, "[\n");
|
||||
for (i = 0; i < ntasks; i++) {
|
||||
struct ptask *t = &tasks[i];
|
||||
/* minimal json; escape quotes/newlines crudely */
|
||||
char esc[PT_DESC*2]; size_t j=0,k;
|
||||
for (k=0; t->desc[k] && j+6 < sizeof(esc); k++) {
|
||||
char c=t->desc[k];
|
||||
if (c=='"'||c=='\\') { esc[j++]='\\'; esc[j++]=c; }
|
||||
else if (c=='\n') { esc[j++]='\\'; esc[j++]='n'; }
|
||||
else if ((unsigned char)c >= 32) esc[j++]=c;
|
||||
else esc[j++]=' ';
|
||||
}
|
||||
esc[j]='\0';
|
||||
fprintf(f, " %s{\"id\":%d,\"status\":\"%s\",\"desc\":\"%s\",\"run_after\":\"%s\",\"note\":\"%s\",\"created\":%ld,\"updated\":%ld}%s\n",
|
||||
i?",":"", t->id, t->status, esc, t->run_after, t->note, t->created, t->updated, (i+1<ntasks)?",":"");
|
||||
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;
|
||||
}
|
||||
fprintf(f, "]\n");
|
||||
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];
|
||||
|
||||
static int load_int(const char *s, const char *k) {
|
||||
char pat[64]; const char *p;
|
||||
snprintf(pat,sizeof(pat),"\"%s\":",k);
|
||||
p=strstr(s,pat); if(!p) return 0; p+=strlen(pat);
|
||||
return (int)strtol(p,NULL,10);
|
||||
}
|
||||
static void load_str(const char *s, const char *k, char *out, size_t cap) {
|
||||
char pat[64]; const char *p,*e; size_t n=0;
|
||||
out[0]='\0'; snprintf(pat,sizeof(pat),"\"%s\":\"",k);
|
||||
p=strstr(s,pat); if(!p) return; p+=strlen(pat);
|
||||
for (e=p; *e && !(*e=='"' && e[-1]!='\\') && n+1<cap; e++) {
|
||||
if (*e=='\\' && e[1]) { e++; if (*e=='n') out[n++]='\n'; else out[n++]=*e; }
|
||||
else out[n++]=*e;
|
||||
}
|
||||
out[n]='\0';
|
||||
}
|
||||
|
||||
static void load_tasks(void) {
|
||||
FILE *f; char buf[65536]; size_t nr; char *p; const char *path;
|
||||
ensure_dir(); path = store_path(); if (!path) return;
|
||||
f=fopen(path,"r"); if(!f) return;
|
||||
nr=fread(buf,1,sizeof(buf)-1,f); fclose(f); buf[nr]='\0';
|
||||
ntasks=0; next_id=1; current_id=-1;
|
||||
for (p=buf; (p=strchr(p,'{'))!=NULL && ntasks<PT_MAX; ) {
|
||||
char *end=strchr(p,'}'); char obj[4096]; struct ptask *t;
|
||||
if(!end) break;
|
||||
snprintf(obj,sizeof(obj),"%.*s",(int)(end-p+1),p);
|
||||
t=&tasks[ntasks]; memset(t,0,sizeof(*t));
|
||||
snprintf(t->status,sizeof(t->status),"todo");
|
||||
t->id=load_int(obj,"id"); if(t->id<=0) t->id=next_id;
|
||||
load_str(obj,"status",t->status,sizeof(t->status));
|
||||
load_str(obj,"desc",t->desc,sizeof(t->desc));
|
||||
load_str(obj,"run_after",t->run_after,sizeof(t->run_after));
|
||||
load_str(obj,"note",t->note,sizeof(t->note));
|
||||
t->created=load_int(obj,"created"); t->updated=load_int(obj,"updated");
|
||||
if(t->id>=next_id) next_id=t->id+1;
|
||||
if(strcmp(t->status,"doing")==0) current_id=t->id;
|
||||
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++;
|
||||
p=end+1;
|
||||
}
|
||||
mcp_json_free(root);
|
||||
}
|
||||
|
||||
static struct ptask *find_task(int id) {
|
||||
size_t i; for(i=0;i<ntasks;i++) if(tasks[i].id==id) return &tasks[i]; return NULL;
|
||||
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, "\"");
|
||||
}
|
||||
static struct ptask *next_todo(void) {
|
||||
|
||||
/* 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(strcmp(tasks[i].status,"todo")==0) return &tasks[i];
|
||||
|
||||
for (i = 0; i < ntasks; i++)
|
||||
if (tasks[i].id == id)
|
||||
return &tasks[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static const char *jstr(const char *json, const char *key, char *out, size_t cap){
|
||||
char pat[128]; const char *p,*end; size_t n=0;
|
||||
out[0]='\0'; if(!json||!key) return NULL;
|
||||
snprintf(pat,sizeof(pat),"\"%s\"",key);
|
||||
p=strstr(json,pat); if(!p) return NULL; p+=strlen(pat);
|
||||
while(*p && *p!=':') p++;
|
||||
if(*p==':') p++;
|
||||
while(*p==' '||*p=='\t'||*p=='\n'||*p=='\r') p++;
|
||||
if(*p!='"') return NULL;
|
||||
p++;
|
||||
for(end=p; *end && !(*end=='"' && end[-1]!='\\') && n+1<cap; end++){
|
||||
if(*end=='\\'&&end[1]){ end++; out[n++]=(*end=='n')?'\n':*end; }
|
||||
else { out[n++]=*end; }
|
||||
}
|
||||
out[n]='\0'; return out[0]?out:NULL;
|
||||
}
|
||||
|
||||
|
||||
static void parse_run_after(const char *in, char *out, size_t cap){
|
||||
out[0]='\0'; if(!in||!*in) return;
|
||||
if(in[0]=='+'){
|
||||
long d=strtol(in+1,NULL,10); if(d<=0) d=3600;
|
||||
snprintf(out,cap,"%ld",(long)time(NULL)+d); return;
|
||||
}
|
||||
snprintf(out,cap,"%.63s",in);
|
||||
}
|
||||
|
||||
static void publish_widget(void) {
|
||||
char b[256]; int doing=0,todo=0,done=0; size_t i;
|
||||
for(i=0;i<ntasks;i++){
|
||||
if(strcmp(tasks[i].status,"doing")==0) doing++;
|
||||
else if(strcmp(tasks[i].status,"todo")==0) todo++;
|
||||
else if(strcmp(tasks[i].status,"done")==0) done++;
|
||||
}
|
||||
if(current_id>0) snprintf(b,sizeof(b),"/tasks: #%d active | todo %d doing %d done %d", current_id,todo,doing,done);
|
||||
else snprintf(b,sizeof(b),"/tasks: %zu tasks | todo %d doing %d done %d", ntasks,todo,doing,done);
|
||||
sp_host_widget_text(host_ref,"plan",b);
|
||||
}
|
||||
|
||||
static void focus_task(struct ptask *t) {
|
||||
struct sp_buf out;
|
||||
if(!t||!host_ref) return;
|
||||
if(sp_buf_init(&out)!=0) return;
|
||||
/* set /goal via focus plugin command */
|
||||
sp_host_command(host_ref, "goal", t->desc, &out);
|
||||
sp_buf_free(&out);
|
||||
}
|
||||
|
||||
static int tool_add_locked(const char *args, struct sp_buf *out, void *ctx) {
|
||||
char desc[PT_DESC], ra[64]; struct ptask *t;
|
||||
(void)ctx; ensure_dir(); load_tasks();
|
||||
desc[0]=ra[0]='\0';
|
||||
jstr(args,"description",desc,sizeof(desc));
|
||||
if(!desc[0]) jstr(args,"desc",desc,sizeof(desc));
|
||||
if(!desc[0]) jstr(args,"task",desc,sizeof(desc));
|
||||
if(!desc[0]){ return sp_buf_puts(out,"error: description required"); }
|
||||
if(ntasks>=PT_MAX){ return sp_buf_puts(out,"error: task limit"); }
|
||||
t=&tasks[ntasks++]; memset(t,0,sizeof(*t));
|
||||
t->id=next_id++; snprintf(t->status,sizeof(t->status),"todo");
|
||||
snprintf(t->desc,sizeof(t->desc),"%.1023s",desc);
|
||||
jstr(args,"run_after",ra,sizeof(ra));
|
||||
if(ra[0]) { char ra2[64]; parse_run_after(ra,ra2,sizeof(ra2)); snprintf(t->run_after,sizeof(t->run_after),"%.63s",ra2); }
|
||||
t->created=t->updated=(long)time(NULL);
|
||||
save_tasks(); publish_widget();
|
||||
sp_buf_printf(out,"added plan task #%d: %s\n", t->id, t->desc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tool_list_locked(const char *args, struct sp_buf *out, void *ctx) {
|
||||
size_t i; (void)args;(void)ctx; ensure_dir(); load_tasks();
|
||||
if(ntasks==0) return sp_buf_puts(out,"(no plan tasks) use plan_task_add\n");
|
||||
sp_buf_puts(out,"plan tasks (session-persistent):\n");
|
||||
for(i=0;i<ntasks;i++){
|
||||
struct ptask *t=&tasks[i];
|
||||
sp_buf_printf(out," #%d [%s]%s %s%s%s%s\n", t->id, t->status,
|
||||
(t->id==current_id)?" *":"", t->desc,
|
||||
t->run_after[0]?" (run_after=":"", t->run_after, t->run_after[0]?")":"", t->note[0]?" #":"", t->note[0]?t->note:"");
|
||||
}
|
||||
sp_buf_puts(out,"use /tasks work <id> | /tasks all | /tasks choose\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tool_update_locked(const char *args, struct sp_buf *out, void *ctx) {
|
||||
char ids[32], st[24], desc[PT_DESC], ra[64], note[256]; int id; struct ptask *t;
|
||||
(void)ctx; ensure_dir(); load_tasks();
|
||||
ids[0]=st[0]=desc[0]=ra[0]=note[0]='\0';
|
||||
jstr(args,"id",ids,sizeof(ids)); if(!ids[0]) jstr(args,"task_id",ids,sizeof(ids));
|
||||
id = ids[0]?atoi(ids):0;
|
||||
if(id<=0){ return sp_buf_puts(out,"error: id required"); }
|
||||
t=find_task(id); if(!t){ sp_buf_printf(out,"error: no task #%d\n",id); return 0; }
|
||||
jstr(args,"status",st,sizeof(st));
|
||||
jstr(args,"description",desc,sizeof(desc)); if(!desc[0]) jstr(args,"desc",desc,sizeof(desc));
|
||||
if(jstr(args,"run_after",ra,sizeof(ra))==NULL && strstr(args,"run_after")) ra[0]='\0'; /* allow clear */
|
||||
else jstr(args,"run_after",ra,sizeof(ra));
|
||||
jstr(args,"note",note,sizeof(note));
|
||||
if(st[0]){
|
||||
snprintf(t->status,sizeof(t->status),"%.23s",st);
|
||||
if(strcmp(st,"doing")==0){ current_id=t->id; focus_task(t); }
|
||||
else if(current_id==t->id && strcmp(st,"doing")!=0) current_id=-1;
|
||||
}
|
||||
if(desc[0]) snprintf(t->desc,sizeof(t->desc),"%.1023s",desc);
|
||||
if(strstr(args,"run_after")) { char ra2[64]; if(ra[0]) parse_run_after(ra,ra2,sizeof(ra2)); else ra2[0]='\0'; snprintf(t->run_after,sizeof(t->run_after),"%.63s",ra2); }
|
||||
if(note[0]) snprintf(t->note,sizeof(t->note),"%.255s",note);
|
||||
t->updated=(long)time(NULL);
|
||||
save_tasks(); publish_widget();
|
||||
sp_buf_printf(out,"task #%d -> [%s] %s\n", t->id, t->status, t->desc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void mark_doing(struct ptask *t){
|
||||
static struct task *
|
||||
first_todo(void)
|
||||
{
|
||||
size_t i;
|
||||
for(i=0;i<ntasks;i++) if(strcmp(tasks[i].status,"doing")==0) snprintf(tasks[i].status,sizeof(tasks[i].status),"todo");
|
||||
if(t){ snprintf(t->status,sizeof(t->status),"doing"); current_id=t->id; t->updated=(long)time(NULL); focus_task(t); }
|
||||
else current_id=-1;
|
||||
|
||||
for (i = 0; i < ntasks; i++)
|
||||
if (strcmp(tasks[i].status, "todo") == 0)
|
||||
return &tasks[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int cmd_tasks_locked(struct sp_host *h, const char *args, struct sp_buf *out, void *ctx){
|
||||
(void)ctx; host_ref=h; ensure_dir(); load_tasks();
|
||||
if(!args||!*args||strcmp(args,"ls")==0||strcmp(args,"list")==0) return tool_list_locked(NULL,out,NULL);
|
||||
if(strncmp(args,"add ",4)==0){
|
||||
struct sp_buf a; if(sp_buf_init(&a)!=0) return -1;
|
||||
sp_buf_printf(&a,"{\"description\":\"%s\"}", args+4);
|
||||
{ int rc=tool_add_locked(a.p,out,NULL); sp_buf_free(&a); return rc; }
|
||||
}
|
||||
if(strncmp(args,"work",4)==0){
|
||||
const char *p=args+4; while(*p==' ') p++;
|
||||
if(strcmp(p,"all")==0){
|
||||
struct ptask *t=next_todo();
|
||||
if(!t) return sp_buf_puts(out,"no todo tasks left\n");
|
||||
mark_doing(t); save_tasks(); publish_widget();
|
||||
return sp_buf_printf(out,"working all: started with #%d [%s] %s\n(complete it, then /tasks choose/next)\n", t->id,t->status,t->desc);
|
||||
}
|
||||
if(*p){
|
||||
int id=atoi(p); struct ptask *t=find_task(id);
|
||||
if(!t) return sp_buf_printf(out,"no task #%d\n", id);
|
||||
mark_doing(t); save_tasks(); publish_widget();
|
||||
return sp_buf_printf(out,"working on #%d: %s\n(goal/focus set; avoid drift)\n", t->id, t->desc);
|
||||
}
|
||||
}
|
||||
if(strcmp(args,"choose")==0||strcmp(args,"next")==0){
|
||||
struct ptask *t=next_todo();
|
||||
if(!t) return sp_buf_puts(out,"no todo tasks\n");
|
||||
mark_doing(t); save_tasks(); publish_widget();
|
||||
return sp_buf_printf(out,"chose #%d: %s\n", t->id, t->desc);
|
||||
}
|
||||
if(strncmp(args,"done",4)==0){
|
||||
int id=current_id; const char *p=args+4; while(*p==' ') p++; if(*p) id=atoi(p);
|
||||
struct ptask *t=find_task(id);
|
||||
if(!t) return sp_buf_puts(out,"no current/task to mark done\n");
|
||||
snprintf(t->status,sizeof(t->status),"done"); t->updated=(long)time(NULL);
|
||||
if(current_id==id) current_id=-1;
|
||||
save_tasks(); publish_widget();
|
||||
return sp_buf_printf(out,"task #%d done\n", id);
|
||||
}
|
||||
if(strncmp(args,"wait",4)==0 || strncmp(args,"quota",5)==0){
|
||||
const char *p = args + (args[0]=='q'?5:4);
|
||||
int id = 0;
|
||||
char ra[64], ra2[64];
|
||||
ra[0]='\0'; ra2[0]='\0';
|
||||
while(*p==' ') p++;
|
||||
id = atoi(p);
|
||||
while(*p && *p!=' ') p++;
|
||||
while(*p==' ') p++;
|
||||
if(*p) {
|
||||
size_t n=0;
|
||||
while(*p && *p!=' ' && n+1<sizeof(ra)) { ra[n++]=*p++; }
|
||||
ra[n]='\0';
|
||||
}
|
||||
while(*p==' ') p++;
|
||||
{
|
||||
struct ptask *t = find_task(id);
|
||||
if(!t) return sp_buf_printf(out,"no task #%d\n", id);
|
||||
if(ra[0]) parse_run_after(ra,ra2,sizeof(ra2));
|
||||
else snprintf(ra2,sizeof(ra2),"%ld",(long)time(NULL)+3600);
|
||||
snprintf(t->status,sizeof(t->status),"waiting_quota");
|
||||
snprintf(t->run_after,sizeof(t->run_after),"%.63s",ra2);
|
||||
if(*p) snprintf(t->note,sizeof(t->note),"%.200s",p);
|
||||
else snprintf(t->note,sizeof(t->note),"quota wait (auto-scheduled)");
|
||||
t->updated=(long)time(NULL);
|
||||
if(current_id==id) current_id=-1;
|
||||
save_tasks(); publish_widget();
|
||||
return sp_buf_printf(out,"task #%d waiting_quota run_after=%s (auto-scheduled)\n", id, t->run_after);
|
||||
}
|
||||
}
|
||||
return sp_buf_puts(out,"usage: /tasks [ls|add <desc>|work <id>|work all|choose|next|done [id]|wait <id> <+secs|epoch> [note]]\n");
|
||||
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;
|
||||
}
|
||||
|
||||
static int tick_locked(struct sp_host *h, const char *name, void *c){
|
||||
long now = (long)time(NULL); size_t i; int changed=0;
|
||||
(void)name;(void)c; host_ref=h; ensure_dir(); load_tasks();
|
||||
for(i=0;i<ntasks;i++){
|
||||
struct ptask *t=&tasks[i];
|
||||
if(strcmp(t->status,"waiting_quota")==0 && t->run_after[0]){
|
||||
long ra = strtol(t->run_after,NULL,10);
|
||||
if(ra>0 && now >= ra){
|
||||
snprintf(t->status,sizeof(t->status),"todo");
|
||||
snprintf(t->note,sizeof(t->note),"quota ready, auto-scheduled");
|
||||
t->run_after[0]='\0'; t->updated=now; changed=1;
|
||||
}
|
||||
}
|
||||
/* "+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;
|
||||
}
|
||||
if(changed) save_tasks();
|
||||
publish_widget(); return 0;
|
||||
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 (current_id > 0)
|
||||
snprintf(b, sizeof(b), "plan: #%d active | todo %d doing %d "
|
||||
"done %d", current_id, todo, doing, done);
|
||||
else
|
||||
snprintf(b, sizeof(b), "plan: %zu tasks | todo %d doing %d "
|
||||
"done %d", ntasks, 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)
|
||||
{
|
||||
int rc;
|
||||
mcp_json *a;
|
||||
const char *desc;
|
||||
int rc;
|
||||
|
||||
PLAN_LOCK();
|
||||
rc = tool_add_locked(args, out, ctx);
|
||||
PLAN_UNLOCK();
|
||||
(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;
|
||||
}
|
||||
|
||||
@@ -366,56 +398,232 @@ tool_list(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
int rc;
|
||||
|
||||
PLAN_LOCK();
|
||||
rc = tool_list_locked(args, out, ctx);
|
||||
PLAN_UNLOCK();
|
||||
(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)
|
||||
{
|
||||
int rc;
|
||||
mcp_json *a;
|
||||
struct task *t;
|
||||
const char *s;
|
||||
int id;
|
||||
|
||||
PLAN_LOCK();
|
||||
rc = tool_update_locked(args, out, ctx);
|
||||
PLAN_UNLOCK();
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
cmd_tasks(struct sp_host *h, const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
int rc;
|
||||
|
||||
PLAN_LOCK();
|
||||
rc = cmd_tasks_locked(h, args, out, ctx);
|
||||
PLAN_UNLOCK();
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
tick(struct sp_host *h, const char *name, void *c)
|
||||
{
|
||||
int rc;
|
||||
|
||||
PLAN_LOCK();
|
||||
rc = tick_locked(h, name, c);
|
||||
PLAN_UNLOCK();
|
||||
return rc;
|
||||
}
|
||||
|
||||
static const struct sp_tool t_add = {"plan_task_add","Add a persistent session plan task (distinct from background jobs). Args: description, optional run_after(unix ts for quota/auto-schedule).","{\"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 persistent session plan tasks (/tasks).","{\"type\":\"object\",\"properties\":{}}", SP_AUTH_READ, tool_list, NULL};
|
||||
static const struct sp_tool t_upd = {"plan_task_update","Update a session plan task. Args: id, status(todo|doing|done|blocked|waiting_quota), description, run_after, note.","{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"string\"},\"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 w = {"plan", tick, NULL, NULL, 5000};
|
||||
|
||||
static int init(struct sp_host *h){
|
||||
host_ref=h; ensure_dir(); load_tasks(); publish_widget();
|
||||
sp_host_add_tool(h,&t_add); sp_host_add_tool(h,&t_list); sp_host_add_tool(h,&t_upd);
|
||||
sp_host_add_command(h,"tasks",cmd_tasks,NULL);
|
||||
sp_host_add_widget(h,&w);
|
||||
(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;
|
||||
}
|
||||
static const struct sp_plugin plugin = {SP_PLUGIN_ABI,"planner","0.1",init,NULL,NULL};
|
||||
const struct sp_plugin *sp_plugin_describe(void){ return &plugin; }
|
||||
|
||||
/* --------------------------------------------------------------- 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;
|
||||
}
|
||||
+803
-354
File diff suppressed because it is too large.
Load diff
@@ -5,6 +5,8 @@
|
||||
# child's exit status, its own error text and the job line. The fake ssh is
|
||||
# the far side, so no network and no account is involved.
|
||||
set -u
|
||||
# splinter reads a piped stdin as context; without a terminal that waits for EOF.
|
||||
exec </dev/null
|
||||
cd "$(dirname "$0")/.." || exit 1
|
||||
D=$(mktemp -d "${TMPDIR:-/var/tmp}/splinter-delcli-XXXXXX") || exit 1
|
||||
cleanup() { rm -rf "$D"; }
|
||||
|
||||
@@ -90,6 +90,46 @@ main(void)
|
||||
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(®, "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(®, "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(®, "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);
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* 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);
|
||||
memset(&sess, 0, sizeof(sess));
|
||||
snprintf(sess.dir, sizeof(sess.dir), "%s", dir);
|
||||
memset(®, 0, sizeof(reg));
|
||||
memset(&ag, 0, sizeof(ag));
|
||||
CHECK(sp_buf_init(&out) == 0);
|
||||
CHECK(sp_buf_init(&err) == 0);
|
||||
host = sp_host_new(®, &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(®, "tmux_watch") == NULL);
|
||||
|
||||
/* nothing is targeted yet */
|
||||
s = call(®, "tmux_ask", "{\"text\":\"x\"}", &out);
|
||||
CHECK(strstr(s, "error: no tmux target") != NULL);
|
||||
|
||||
/* a name that could be an option, a second target or a command */
|
||||
s = call(®, "tmux_start", "{\"cmd\":\"cat\",\"name\":\"-t evil\"}", &out);
|
||||
CHECK(strstr(s, "error: name may use") != NULL);
|
||||
s = call(®, "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(®, "tmux_start", args, &out);
|
||||
CHECK(strstr(s, "target is") != NULL);
|
||||
|
||||
s = call(®, "tmux_status", "{}", &out);
|
||||
CHECK(strstr(s, name) != NULL && strstr(s, "alive") != NULL);
|
||||
|
||||
s = call(®, "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(®, "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(®, "tmux_read", "{}", &out);
|
||||
CHECK(strstr(s, "(no new output)") != NULL);
|
||||
|
||||
s = call(®, "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(®, "tmux_send", "{\"keys\":\"-t other\"}", &out);
|
||||
CHECK(strstr(s, "error: keys are names") != NULL);
|
||||
s = call(®, "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(®);
|
||||
sp_buf_free(&out);
|
||||
sp_buf_free(&err);
|
||||
snprintf(args, sizeof(args), "rm -rf '%s'", dir);
|
||||
(void)system(args);
|
||||
if (failures == 0)
|
||||
printf("test_tmux: ok\n");
|
||||
return failures == 0 ? 0 : 1;
|
||||
}
|
||||
Reference in new issue
Block a user