From 8ae21f25dbf14036bbe10daa9de5793c32c3bf69 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 5 Aug 2026 05:51:58 +1000 Subject: [PATCH] fix(nell): honour the transition guard on the backend that ships cmd/archied opens nell.OpenStore, not internal/store. Adapter.Transition and Adapter.Requeue accepted a from status and never read it: they loaded the document, overwrote the status and put. ErrStaleTransition could not occur, so every compare-and-swap the callers depend on was absent in production while passing in tests, which use the SQLite store. Measured on the adapter: eight concurrent dashboard retries requeued one task four times; twenty concurrent IncrementRetryCount calls landed a count of one, so the retry cap added in 170fe02 could be evaded indefinitely by holding Retry down. The same gap made approve and reject read-then-write races -- a waiting_human task the daemon had since claimed could be yanked back to queued and executed twice. All three now take the adapter's existing mutex and check the guard. A document store has no conditional update, so read-then-write has to be serialised; the daemon is a single process, which is where the concurrency is. No internal caller holds the lock when calling these, so there is no self-deadlock. Found by the adversarial review. It is the clearest argument yet for the conformance-suite pattern: the guarantee was tested only on the backend nobody runs. --- internal/nell/adapter.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/internal/nell/adapter.go b/internal/nell/adapter.go index 623cffdf1..617aa5ea3 100644 --- a/internal/nell/adapter.go +++ b/internal/nell/adapter.go @@ -273,7 +273,22 @@ func (a *Adapter) TaskByIssue(ctx context.Context, owner, repo string, number in } // Transition moves a task to the given status. +// +// from is a guard, not decoration: the write only happens when the task is +// still in that status, and otherwise store.ErrStaleTransition is returned. +// This adapter accepted the argument and ignored it, so every compare-and-swap +// the callers rely on was absent on the backend that actually ships -- eight +// concurrent dashboard retries requeued one task four times, and a +// waiting_human task the daemon had since claimed could be yanked back to +// queued and executed twice. +// +// The mutex is what makes the check meaningful. A document store has no +// conditional update, so read-then-write has to be serialised; the daemon is +// a single process, which is where the concurrency actually is. func (a *Adapter) Transition(ctx context.Context, taskID int64, from, to, detail string) error { + a.mu.Lock() + defer a.mu.Unlock() + t, err := a.findTaskByID(ctx, taskID) if err != nil { return err @@ -281,6 +296,9 @@ func (a *Adapter) Transition(ctx context.Context, taskID int64, from, to, detail if t == nil { return fmt.Errorf("nell: task %d not found", taskID) } + if from != "" && t.Status != from { + return store.ErrStaleTransition + } t.Status = to return a.putTaskByID(ctx, t) } @@ -315,7 +333,14 @@ func (a *Adapter) Update(ctx context.Context, t *store.Task) error { } // Requeue puts a task back on the queue with an optional workflow override. +// +// fromStatus guards the write, matching Transition and the SQLite store: a +// task that has moved on since the caller read it is not requeued, and +// store.ErrStaleTransition says so. func (a *Adapter) Requeue(ctx context.Context, taskID int64, fromStatus, workflow string) error { + a.mu.Lock() + defer a.mu.Unlock() + t, err := a.findTaskByID(ctx, taskID) if err != nil { return err @@ -323,6 +348,9 @@ func (a *Adapter) Requeue(ctx context.Context, taskID int64, fromStatus, workflo if t == nil { return fmt.Errorf("nell: task %d not found", taskID) } + if fromStatus != "" && t.Status != fromStatus { + return store.ErrStaleTransition + } t.Status = store.StatusQueued if workflow != "" { t.Workflow = workflow @@ -362,7 +390,15 @@ func (a *Adapter) RecoverStale(ctx context.Context) (int64, error) { } // IncrementRetryCount bumps the retry_count on a task. +// IncrementRetryCount bumps retry_count by one. +// +// Serialised: as a bare read-modify-write, twenty concurrent calls landed a +// count of one, so the retry cap could be evaded indefinitely by holding the +// dashboard's Retry button down. func (a *Adapter) IncrementRetryCount(ctx context.Context, taskID int64) error { + a.mu.Lock() + defer a.mu.Unlock() + t, err := a.findTaskByID(ctx, taskID) if err != nil { return err