Merge remote-tracking branch 'origin/fix/48-transition-ignores-from-status-guard-allowing-lost-updates'

This commit is contained in:
sam committed 2026-07-29 23:15:48 +10:00
commit 38be20b93d
2 files changed
+158 -8

No files matched your search

+58 -8
View File
@@ -257,16 +257,43 @@ func (s *Store) ClaimByIssue(ctx context.Context, owner, repo string, number int
return t, err
}
// ErrStaleTransition is returned when a guarded transition fails because
// the task's current status does not match the expected from status.
var ErrStaleTransition = errors.New("store: stale transition: task status does not match expected from status")
// Transition moves a task to a new status, recording the change.
// The from status acts as a guard: the UPDATE only affects rows whose
// current status matches from. If no row matches, ErrStaleTransition is
// returned and no audit row is written. Both statements execute in a
// single transaction.
func (s *Store) Transition(ctx context.Context, taskID int64, from, to, detail string) error {
if _, err := s.db.ExecContext(ctx,
`UPDATE tasks SET status=?, updated_at=datetime('now') WHERE id=?`, to, taskID); err != nil {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
_, err := s.db.ExecContext(ctx,
defer tx.Rollback()
res, err := tx.ExecContext(ctx,
`UPDATE tasks SET status=?, updated_at=datetime('now') WHERE id=? AND status=?`, to, taskID, from)
if err != nil {
return err
}
n, err := res.RowsAffected()
if err != nil {
return err
}
if n == 0 {
return ErrStaleTransition
}
_, err = tx.ExecContext(ctx,
`INSERT INTO transitions (task_id, from_status, to_status, detail) VALUES (?, ?, ?, ?)`,
taskID, from, to, clip(detail, 4000))
return err
if err != nil {
return err
}
return tx.Commit()
}
// Update persists mutable task fields written by workflows.
@@ -341,18 +368,41 @@ func (s *Store) WaitingTasks(ctx context.Context) (tasks []Task, retErr error) {
// Requeue puts a task back on the queue. A non-empty workflow forces it
// (the waiting_human → approved → implement handoff); empty keeps the
// task's current workflow (retrying a parked task).
// The fromStatus acts as a guard: the UPDATE only affects rows whose
// current status matches fromStatus. If no row matches, ErrStaleTransition
// is returned and no audit row is written. Both statements execute in a
// single transaction.
func (s *Store) Requeue(ctx context.Context, taskID int64, fromStatus, workflow string) error {
if _, err := s.db.ExecContext(ctx, `
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
res, err := tx.ExecContext(ctx, `
UPDATE tasks SET status='queued',
workflow=CASE WHEN ?='' THEN workflow ELSE ? END,
stage='', park_reason='', updated_at=datetime('now')
WHERE id=?`, workflow, workflow, taskID); err != nil {
WHERE id=? AND status=?`, workflow, workflow, taskID, fromStatus)
if err != nil {
return err
}
_, err := s.db.ExecContext(ctx,
n, err := res.RowsAffected()
if err != nil {
return err
}
if n == 0 {
return ErrStaleTransition
}
_, err = tx.ExecContext(ctx,
`INSERT INTO transitions (task_id, from_status, to_status, detail) VALUES (?, ?, 'queued', ?)`,
taskID, fromStatus, "requeued "+workflow)
return err
if err != nil {
return err
}
return tx.Commit()
}
// RecoverStale re-queues tasks left running by a crashed daemon.
+100
View File
@@ -155,6 +155,106 @@ func TestEventLogRoundTrip(t *testing.T) {
}
}
// TestTransitionRejectsStaleFrom proves that Transition rejects a call
// whose `from` parameter does not match the task's current status.
// Today Transition ignores the `from` guard, so the call succeeds when
// it should fail — this test captures that bug.
func TestTransitionRejectsStaleFrom(t *testing.T) {
s := openTest(t)
ctx := context.Background()
if _, err := s.EnqueueIssue(ctx, "acme", "widget", 1, "t", "b", "", ""); err != nil {
t.Fatal(err)
}
task, err := s.ClaimNext(ctx)
if err != nil || task == nil {
t.Fatalf("claim = (%v, %v)", task, err)
}
// Task is now StatusRunning. Transition with from=StatusQueued
// must fail because the task is not queued.
err = s.Transition(ctx, task.ID, StatusQueued, StatusPROpen, "stale from")
if err == nil {
t.Fatal("Transition with stale 'from' (StatusQueued) on a running task must return an error, but got nil")
}
// Verify the task was NOT changed.
got, err := s.TaskByID(ctx, task.ID)
if err != nil || got == nil {
t.Fatalf("TaskByID = (%+v, %v)", got, err)
}
if got.Status != StatusRunning {
t.Fatalf("task status changed to %q despite stale from guard; want %q", got.Status, StatusRunning)
}
}
// TestTransitionPreventsDoubleTransition proves that two callers racing
// to transition the same task from the same `from` status cannot both
// succeed — the second must get an error because the first already
// changed the status.
func TestTransitionPreventsDoubleTransition(t *testing.T) {
s := openTest(t)
ctx := context.Background()
if _, err := s.EnqueueIssue(ctx, "acme", "widget", 1, "t", "b", "", ""); err != nil {
t.Fatal(err)
}
task, err := s.ClaimNext(ctx)
if err != nil || task == nil {
t.Fatalf("claim = (%v, %v)", task, err)
}
// First transition: running → pr_open should succeed.
if err := s.Transition(ctx, task.ID, StatusRunning, StatusPROpen, "first"); err != nil {
t.Fatalf("first transition = %v", err)
}
// Second transition: the task is now StatusPROpen, so
// from=StatusRunning must fail.
err = s.Transition(ctx, task.ID, StatusRunning, StatusMerged, "second")
if err == nil {
t.Fatal("second Transition with stale 'from' (StatusRunning) on a pr_open task must return an error, but got nil")
}
// Verify the task kept the first transition's status.
got, err := s.TaskByID(ctx, task.ID)
if err != nil || got == nil {
t.Fatalf("TaskByID = (%+v, %v)", got, err)
}
if got.Status != StatusPROpen {
t.Fatalf("task status = %q, want %q (second transition must not overwrite)", got.Status, StatusPROpen)
}
}
// TestRequeueRejectsStaleFrom proves that Requeue rejects a call whose
// fromStatus does not match the task's current status.
func TestRequeueRejectsStaleFrom(t *testing.T) {
s := openTest(t)
ctx := context.Background()
if _, err := s.EnqueueIssue(ctx, "acme", "widget", 1, "t", "b", "", ""); err != nil {
t.Fatal(err)
}
task, err := s.ClaimNext(ctx)
if err != nil || task == nil {
t.Fatalf("claim = (%v, %v)", task, err)
}
// Task is StatusRunning. Requeue with fromStatus=StatusParked must
// fail because the task is not parked.
err = s.Requeue(ctx, task.ID, StatusParked, "implement")
if err == nil {
t.Fatal("Requeue with stale fromStatus (StatusParked) on a running task must return an error, but got nil")
}
// Verify the task was NOT changed.
got, err := s.TaskByID(ctx, task.ID)
if err != nil || got == nil {
t.Fatalf("TaskByID = (%+v, %v)", got, err)
}
if got.Status != StatusRunning {
t.Fatalf("task status changed to %q despite stale fromStatus guard; want %q", got.Status, StatusRunning)
}
}
func TestIncrementRetryCount(t *testing.T) {
s := openTest(t)
ctx := context.Background()