Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-7879: fix(wakeup): clear a rule's last_error once a pass commits (#9069)

Ray Walker9a8c3dec4e95
diff --git a/server/internal/handler/issue_child_done_test.go b/server/internal/handler/issue_child_done_test.go
index eb8d68cafd..f06030215e 100644
--- a/server/internal/handler/issue_child_done_test.go
+++ b/server/internal/handler/issue_child_done_test.go
@@ -302,6 +302,44 @@ func TestChildDoneHeldWhileParentInBacklog(t *testing.T) {
}
}
+// A pass on a parked parent evaluates nothing but still commits, so it clears
+// an earlier pass's error instead of holding it until the parent leaves backlog.
+func TestChildDoneParkedPassClearsEarlierError(t *testing.T) {
+ ctx := context.Background()
+ fx := newChildDoneFixture(t, "backlog")
+ setIssueAssigneeDirect(t, fx.parent.ID, "agent", handlerTestAgentID(t))
+ updateChildStatus(t, fx.child.ID, "done") // creates the parent's rule
+ runWakeupTick(t)
+ tag, err := testPool.Exec(ctx, `UPDATE issue_wakeup SET last_error = 'earlier failure'
+ WHERE issue_id = $1 AND system_rule IS NOT NULL`, fx.parent.ID)
+ if err != nil || tag.RowsAffected() != 1 {
+ t.Fatalf("seed the parent's rule error: %v (%d rows)", err, tag.RowsAffected())
+ }
+
+ receipts := func() (n int) {
+ t.Helper()
+ if err := testPool.QueryRow(ctx, `SELECT count(*) FROM issue_wakeup_receipt r JOIN issue_wakeup w ON w.id = r.wakeup_id
+ WHERE w.issue_id = $1 AND w.system_rule IS NOT NULL`, fx.parent.ID).Scan(&n); err != nil {
+ t.Fatal(err)
+ }
+ return n
+ }
+ before := receipts()
+ updateChildStatus(t, fx.child.ID, "in_progress") // its hint dispatches the parked pass inline
+ runWakeupTick(t)
+ if receipts() == before {
+ t.Fatal("reopening the sub-issue sent the parked parent's rule no hint")
+ }
+ var lastError *string
+ if err := testPool.QueryRow(ctx, `SELECT last_error FROM issue_wakeup
+ WHERE issue_id = $1 AND system_rule IS NOT NULL`, fx.parent.ID).Scan(&lastError); err != nil {
+ t.Fatal(err)
+ }
+ if lastError != nil {
+ t.Fatalf("the parked pass kept the earlier error %q", *lastError)
+ }
+}
+
// An issue without a parent creates no rule anywhere.
func TestChildDoneSkippedWhenNoParent(t *testing.T) {
orphan := dbfx.Issue(t, "orphan child-done", testutil.Cols{"status": "in_progress"})
diff --git a/server/internal/service/issue_wakeup_condition.go b/server/internal/service/issue_wakeup_condition.go
index 94523348af..6bd07efe22 100644
--- a/server/internal/service/issue_wakeup_condition.go
+++ b/server/internal/service/issue_wakeup_condition.go
@@ -502,23 +502,15 @@ func (s *IssueWakeupService) baselineCondition(ctx context.Context, tx pgx.Tx, q
return q.SetWakeupConditionState(ctx, db.SetWakeupConditionStateParams{ID: w.ID, ConditionState: fingerprint, NextFireAt: pgtype.Timestamptz{Time: now.Add(conditionPollInterval), Valid: true}})
}
-// saveConditionDiagnostic owns only last_error values carrying its prefix.
-// Dispatch failures share the column and must survive a condition poll. Avoid
-// rewriting an unchanged diagnostic on every polling interval as well.
+// A completed evaluation replaces the previous pass's error with its current
+// diagnostic. Later dispatch failures roll this write back and are recorded
+// outside the transaction. Avoid rewriting unchanged diagnostics on every poll.
func saveConditionDiagnostic(ctx context.Context, q *db.Queries, w db.IssueWakeup, diagnostic error) error {
- owned := w.LastError.Valid && strings.HasPrefix(w.LastError.String, conditionDiagnosticPrefix)
var lastError pgtype.Text
if diagnostic != nil {
lastError = pgtype.Text{String: conditionDiagnosticPrefix + diagnostic.Error(), Valid: true}
- if w.LastError.Valid {
- if w.LastError.String == lastError.String {
- return nil
- }
- if !owned {
- return nil
- }
- }
- } else if !owned {
+ }
+ if w.LastError == lastError {
return nil
}
return q.NoteWakeupFailure(ctx, db.NoteWakeupFailureParams{ID: w.ID, LastError: lastError})
diff --git a/server/internal/service/issue_wakeup_condition_pr_test.go b/server/internal/service/issue_wakeup_condition_pr_test.go
index e84c4fbc0e..7e1b4e1815 100644
--- a/server/internal/service/issue_wakeup_condition_pr_test.go
+++ b/server/internal/service/issue_wakeup_condition_pr_test.go
@@ -138,35 +138,39 @@ func TestWakeupConditionPRDiagnosticPolling(t *testing.T) {
}
}
-func TestSaveConditionDiagnosticPreservesDispatchErrors(t *testing.T) {
+func TestSaveConditionDiagnosticReplacesEarlierFailures(t *testing.T) {
ctx := context.Background()
tx := &prConditionTx{}
q := db.New(tx)
realFailure := pgtype.Text{String: "dispatch failed", Valid: true}
+ tx.lastError = realFailure
w := db.IssueWakeup{ID: dbid.NewV7(), LastError: realFailure}
if err := saveConditionDiagnostic(ctx, q, w, errPRNotLinked); err != nil {
t.Fatal(err)
}
+ if tx.writes != 1 || tx.lastError.String != conditionDiagnosticPrefix+errPRNotLinked.Error() {
+ t.Fatalf("recovered evaluation kept the earlier dispatch failure: writes=%d error=%+v", tx.writes, tx.lastError)
+ }
if err := saveConditionDiagnostic(ctx, q, w, nil); err != nil {
t.Fatal(err)
}
- if tx.writes != 0 || tx.lastError.Valid {
- t.Fatalf("condition evaluation changed an unrelated error: writes=%d error=%+v", tx.writes, tx.lastError)
+ if tx.writes != 2 || tx.lastError.Valid {
+ t.Fatalf("recovered evaluation did not clear the earlier dispatch failure: writes=%d error=%+v", tx.writes, tx.lastError)
}
w.LastError = pgtype.Text{String: conditionDiagnosticPrefix + errPRNotLinked.Error(), Valid: true}
if err := saveConditionDiagnostic(ctx, q, w, errPRSnapshotPending); err != nil {
t.Fatal(err)
}
- if tx.writes != 1 || tx.lastError.String != conditionDiagnosticPrefix+errPRSnapshotPending.Error() {
+ if tx.writes != 3 || tx.lastError.String != conditionDiagnosticPrefix+errPRSnapshotPending.Error() {
t.Fatalf("owned diagnostic was not updated: writes=%d error=%+v", tx.writes, tx.lastError)
}
w.LastError = tx.lastError
if err := saveConditionDiagnostic(ctx, q, w, nil); err != nil {
t.Fatal(err)
}
- if tx.writes != 2 || tx.lastError.Valid {
+ if tx.writes != 4 || tx.lastError.Valid {
t.Fatalf("owned diagnostic was not cleared: writes=%d error=%+v", tx.writes, tx.lastError)
}
}
diff --git a/server/internal/service/issue_wakeup_condition_test.go b/server/internal/service/issue_wakeup_condition_test.go
index 925d5ffddc..bb404b1b0d 100644
--- a/server/internal/service/issue_wakeup_condition_test.go
+++ b/server/internal/service/issue_wakeup_condition_test.go
@@ -7,6 +7,7 @@ import (
"strings"
"testing"
+ "github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/multica-ai/multica/server/internal/testutil"
"github.com/multica-ai/multica/server/internal/util"
@@ -280,6 +281,107 @@ func TestWakeupConditionValidation(t *testing.T) {
}
}
+// A check that loses its lock wait records an error; the next unmet check that commits clears it.
+func TestWakeupConditionCheckClearsAnEarlierFailure(t *testing.T) {
+ f, s, issue, agent := conditionFixture(t)
+ ctx := context.Background()
+ workspace := parseTestUUID(t, f.WorkspaceID)
+ w := wakeCreate(t, f, s, issue, WakeupInput{AgentID: agent, Kind: "event", Instruction: "Review when ready",
+ Condition: condition(t, map[string]any{"type": "issue_field", "field": "status", "value": "in_review"})})
+ due := func() {
+ f.Exec(t, "UPDATE issue_wakeup SET next_fire_at=now()-interval '1 second' WHERE id=$1", w.ID)
+ }
+
+ due()
+ tx, err := f.Pool.Begin(ctx)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err = tx.Exec(ctx, "SELECT 1 FROM issue WHERE id=$1 FOR UPDATE", issue); err != nil {
+ t.Fatal(err)
+ }
+ tickErr := s.TickWorkspaces(ctx, workspace)
+ if err = tx.Rollback(ctx); err != nil {
+ t.Fatal(err)
+ }
+ if tickErr == nil {
+ t.Fatal("a check blocked on its issue row reported no error")
+ }
+ failed, err := f.q.LocklessWakeup(ctx, w.ID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !failed.LastError.Valid {
+ t.Fatal("the failed check recorded no error")
+ }
+
+ due()
+ if err = s.TickWorkspaces(ctx, workspace); err != nil {
+ t.Fatal(err)
+ }
+ got, err := f.q.LocklessWakeup(ctx, w.ID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got.LastError.Valid {
+ t.Fatalf("the check that committed kept the earlier error %q", got.LastError.String)
+ }
+}
+
+type wakeupCommitFailureStarter struct {
+ TxStarter
+ err error
+}
+
+func (s wakeupCommitFailureStarter) Begin(ctx context.Context) (pgx.Tx, error) {
+ tx, err := s.TxStarter.Begin(ctx)
+ if err != nil {
+ return nil, err
+ }
+ return wakeupCommitFailureTx{Tx: tx, err: s.err}, nil
+}
+
+type wakeupCommitFailureTx struct {
+ pgx.Tx
+ err error
+}
+
+func (tx wakeupCommitFailureTx) Commit(context.Context) error { return tx.err }
+
+func TestWakeupConditionFailedCommitPreservesFailure(t *testing.T) {
+ f, s, issue, agent := conditionFixture(t)
+ ctx := context.Background()
+ w := wakeCreate(t, f, s, issue, WakeupInput{AgentID: agent, Kind: "event", Instruction: "Review when ready",
+ Condition: condition(t, map[string]any{"type": "issue_field", "field": "status", "value": "in_review"})})
+ f.Exec(t, "UPDATE issue_wakeup SET next_fire_at=now()-interval '1 second',last_error='earlier dispatch failure' WHERE id=$1", w.ID)
+ commitErr := errors.New("dispatch commit failed")
+ s.Tasks.TxStarter = wakeupCommitFailureStarter{TxStarter: f.Pool, err: commitErr}
+ if err := s.dispatch(ctx, w); !errors.Is(err, commitErr) {
+ t.Fatalf("dispatch error=%v, want %v", err, commitErr)
+ }
+ got, err := f.q.LocklessWakeup(ctx, w.ID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !got.LastError.Valid || got.LastError.String != "earlier dispatch failure" {
+ t.Fatalf("failed commit cleared the earlier failure: %+v", got.LastError)
+ }
+ if err := s.TickWorkspaces(ctx, parseTestUUID(t, f.WorkspaceID)); !errors.Is(err, commitErr) {
+ t.Fatalf("tick error=%v, want %v", err, commitErr)
+ }
+ got, err = f.q.LocklessWakeup(ctx, w.ID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !got.LastError.Valid || got.LastError.String != commitErr.Error() {
+ t.Fatalf("failed pass did not record the current failure: %+v", got.LastError)
+ }
+ s.Tasks.TxStarter = f.Pool
+ if got := wakeTick(t, f, s, w.ID); got.LastError.Valid {
+ t.Fatalf("successful retry kept the commit failure: %+v", got.LastError)
+ }
+}
+
func TestWakeupConditionPRDiagnosticsRecover(t *testing.T) {
for _, event := range []string{"checks_finished", "merged"} {
t.Run(event, func(t *testing.T) {
@@ -306,17 +408,25 @@ func TestWakeupConditionPRDiagnosticsRecover(t *testing.T) {
t.Fatal("diagnostic stopped condition polling")
}
}
+ assertRecoveredFailure := func(diagnostic error) {
+ t.Helper()
+ f.Exec(t, "UPDATE issue_wakeup SET last_error='earlier dispatch failure' WHERE id=$1", w.ID)
+ assertWaiting(wakeTick(t, f, s, w.ID), diagnostic)
+ }
if event == "checks_finished" {
// A disabled snapshot client is visible even in the create response.
assertWaiting(w, errPRSnapshotsUnavailable)
assertWaiting(wakeTick(t, f, s, w.ID), errPRSnapshotsUnavailable)
+ assertRecoveredFailure(errPRSnapshotsUnavailable)
s.Tasks.PRSnapshotsEnabled = true
}
// Linking to the parent must never satisfy a child issue's rule.
assertWaiting(wakeTick(t, f, s, w.ID), errPRNotLinked)
+ assertRecoveredFailure(errPRNotLinked)
f.Exec(t, "INSERT INTO issue_pull_request(issue_id,pull_request_id) VALUES($1,$2)", issue, pr)
if event == "checks_finished" {
assertWaiting(wakeTick(t, f, s, w.ID), errPRSnapshotPending)
+ assertRecoveredFailure(errPRSnapshotPending)
// Re-enabling also accepts a transiently missing snapshot.
disabled, err := s.Disable(ctx, issue, w.ID, parseTestUUID(t, f.UserID))
if err != nil {
diff --git a/server/internal/service/issue_wakeup_system.go b/server/internal/service/issue_wakeup_system.go
index b6dcc8585d..ccab4a4a4a 100644
--- a/server/internal/service/issue_wakeup_system.go
+++ b/server/internal/service/issue_wakeup_system.go
@@ -434,6 +434,11 @@ func (s *IssueWakeupService) dispatchSystem(ctx context.Context, prev db.IssueWa
if w.Revision != prev.Revision {
return tx.Commit(ctx)
}
+ // Every commit below, including those that evaluate nothing, means this
+ // pass got the locks an earlier failed pass did not; an error rolls it back.
+ if _, err = tx.Exec(ctx, "UPDATE issue_wakeup SET last_error=NULL WHERE id=$1 AND last_error IS NOT NULL", w.ID); err != nil {
+ return err
+ }
current, err := resolveWakeTarget(ctx, q, issue)
if err != nil {
return err