The widget tick calls load_tasks() every five seconds; a tool updating a task at the same moment had its array reset under it, which made test_planner fail intermittently (about one run in six). A recursive mutex now covers the three tools, the /tasks command and the tick; the reload and the save happen under the same lock. Found by running test_planner repeatedly after a full check failed once; 20 consecutive runs pass now.
422 lines
15 KiB
C
422 lines
15 KiB
C
/*
|
|
* splinter -- persistent session plan/tasks (/tasks), distinct from background jobs.
|
|
*
|
|
* 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.
|
|
*/
|
|
#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 "splinter_plugin.h"
|
|
|
|
#define PT_MAX 128
|
|
#define PT_DESC 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;
|
|
};
|
|
|
|
static pthread_mutex_t plan_lock;
|
|
static pthread_once_t plan_lock_once = PTHREAD_ONCE_INIT;
|
|
|
|
static void
|
|
plan_lock_init(void)
|
|
{
|
|
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);
|
|
}
|
|
|
|
#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 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)?",":"");
|
|
}
|
|
fprintf(f, "]\n");
|
|
fclose(f);
|
|
}
|
|
|
|
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;
|
|
ntasks++;
|
|
p=end+1;
|
|
}
|
|
}
|
|
|
|
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 struct ptask *next_todo(void) {
|
|
size_t i;
|
|
for(i=0;i<ntasks;i++) if(strcmp(tasks[i].status,"todo")==0) 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){
|
|
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;
|
|
}
|
|
|
|
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 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;
|
|
}
|
|
}
|
|
}
|
|
if(changed) save_tasks();
|
|
publish_widget(); return 0;
|
|
}
|
|
|
|
static int
|
|
tool_add(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
int rc;
|
|
|
|
PLAN_LOCK();
|
|
rc = tool_add_locked(args, out, ctx);
|
|
PLAN_UNLOCK();
|
|
return rc;
|
|
}
|
|
|
|
static int
|
|
tool_list(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
int rc;
|
|
|
|
PLAN_LOCK();
|
|
rc = tool_list_locked(args, out, ctx);
|
|
PLAN_UNLOCK();
|
|
return rc;
|
|
}
|
|
|
|
static int
|
|
tool_update(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
int rc;
|
|
|
|
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);
|
|
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; }
|