/* * 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 /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 " when its * provider is out of quota, and the tick below puts it back to todo when the * time has come. */ #include #include #include #include #include #include #include "splinter.h" #include "mcp.h" #include "splinter_plugin.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 (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 | 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 |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 |work |work all" "|next|done [id]|wait <+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; }