planner: one lock around the task list

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.
This commit is contained in:
lotherk committed 2026-11-10 06:12:39 +00:00
1 parent 968b5eada4
commit ef3c4d0b37
4 files changed
+86 -10

No files matched your search

+83 -7
View File
@@ -6,6 +6,7 @@
* Integrates with /goal (focus plugin) to prevent drift.
*/
#include <ctype.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -29,6 +30,26 @@ struct ptask {
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;
@@ -172,7 +193,7 @@ static void focus_task(struct ptask *t) {
sp_buf_free(&out);
}
static int tool_add(const char *args, struct sp_buf *out, void *ctx) {
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';
@@ -192,7 +213,7 @@ static int tool_add(const char *args, struct sp_buf *out, void *ctx) {
return 0;
}
static int tool_list(const char *args, struct sp_buf *out, void *ctx) {
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");
@@ -206,7 +227,7 @@ static int tool_list(const char *args, struct sp_buf *out, void *ctx) {
return 0;
}
static int tool_update(const char *args, struct sp_buf *out, void *ctx) {
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';
@@ -240,13 +261,13 @@ static void mark_doing(struct ptask *t){
else current_id=-1;
}
static int cmd_tasks(struct sp_host *h, const char *args, struct sp_buf *out, void *ctx){
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(NULL,out,NULL);
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(a.p,out,NULL); sp_buf_free(&a); return rc; }
{ 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++;
@@ -311,7 +332,7 @@ static int cmd_tasks(struct sp_host *h, const char *args, struct sp_buf *out, vo
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(struct sp_host *h, const char *name, void *c){
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++){
@@ -329,6 +350,61 @@ static int tick(struct sp_host *h, const char *name, void *c){
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};
+1 -1
View File
@@ -53,7 +53,7 @@ hr { border: 0; border-top: 1px solid #1d2126; margin: 3em 0 1.5em 0; }
<h1>splinter<span>_</span></h1>
<p class="lead">An agent harness you own: chat in the terminal, tools with authorization classes, plugins, any model that speaks OpenAI — built by us so we can change it, instead of getting used to how somebody else's client behaves.</p>
<p class="meta"><b>23769</b> lines of C99/POSIX &middot;
<p class="meta"><b>23845</b> lines of C99/POSIX &middot;
<b>23</b> tools &middot; <b>31</b> test programs &middot;
<b>16</b> plugins &middot; <b>42</b> palettes &middot; BSD-3-Clause</p>
+1 -1
View File
@@ -53,7 +53,7 @@ hr { border: 0; border-top: 1px solid #1d2126; margin: 3em 0 1.5em 0; }
<h1>splinter<span>_</span></h1>
<p class="lead">Ein Agent-Harness, das dir gehoert: Chat im Terminal, Werkzeuge mit Autorisierungsklassen, Plugins, jedes Modell das OpenAI spricht — von uns gebaut, damit wir es umbauen koennen, statt uns an das Verhalten eines fremden Clients zu gewoehnen.</p>
<p class="meta"><b>23769</b> Zeilen C99/POSIX &middot;
<p class="meta"><b>23845</b> Zeilen C99/POSIX &middot;
<b>23</b> Werkzeuge &middot; <b>31</b> Test-Programme &middot;
<b>16</b> Plugins &middot; <b>42</b> Farbschemata &middot; BSD-3-Clause</p>
+1 -1
View File
@@ -53,7 +53,7 @@ hr { border: 0; border-top: 1px solid #1d2126; margin: 3em 0 1.5em 0; }
<h1>splinter<span>_</span></h1>
<p class="lead">Um harness de agente que e seu: chat no terminal, ferramentas com classes de autorizacao, plugins, qualquer modelo que fale OpenAI - escrito por nos para poder mudar-lo, em vez de nos acostumar com o comportamento de um cliente alheio.</p>
<p class="meta"><b>23769</b> linhas C99/POSIX &middot;
<p class="meta"><b>23845</b> linhas C99/POSIX &middot;
<b>23</b> ferramentas &middot; <b>31</b> programas de teste &middot;
<b>16</b> plugins &middot; <b>42</b> paletas &middot; BSD-3-Clause</p>