Skip to content

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

Go

fix(daemon): tighten 404 task-not-found semantics — server + final guard (#2127)

Bohan Jiangae985ae2a389
diff --git a/server/internal/daemon/daemon.go b/server/internal/daemon/daemon.go
index 88aae699d2..22ebb98fe6 100644
--- a/server/internal/daemon/daemon.go
+++ b/server/internal/daemon/daemon.go
@@ -1463,11 +1463,12 @@ func (d *Daemon) handleTask(ctx context.Context, task Task, slot int) {
_ = d.client.ReportProgress(ctx, task.ID, "Finishing task", 2, 2)
- // Check if the task was cancelled while it was running (e.g. issue
- // was reassigned). If so, skip reporting results — the server already
- // moved the task to 'cancelled' so complete/fail would fail anyway.
- if status, err := d.client.GetTaskStatus(ctx, task.ID); err == nil && status == "cancelled" {
- taskLog.Info("task cancelled during execution, discarding result")
+ // Final pre-completion check: if the server already moved the task to
+ // "cancelled" or deleted the row outright, skip reporting — the
+ // complete/fail callbacks would fail anyway. Reuse shouldInterruptAgent
+ // so this guard honors the same signals as the in-flight watcher.
+ if status, err := d.client.GetTaskStatus(ctx, task.ID); shouldInterruptAgent(status, err) {
+ taskLog.Info("task cancelled during execution, discarding result", "status", status, "error", err)
return
}
diff --git a/server/internal/handler/daemon.go b/server/internal/handler/daemon.go
index 3c09841c00..948a242a02 100644
--- a/server/internal/handler/daemon.go
+++ b/server/internal/handler/daemon.go
@@ -78,7 +78,15 @@ func (h *Handler) requireDaemonTaskAccess(w http.ResponseWriter, r *http.Request
}
task, err := h.Queries.GetAgentTask(r.Context(), taskUUID)
if err != nil {
- writeError(w, http.StatusNotFound, "task not found")
+ // Only treat pgx.ErrNoRows as a real "task gone" signal — daemon
+ // uses this 404 to interrupt the running agent, so a transient DB
+ // error must not be reported as a deletion.
+ if isNotFound(err) {
+ writeError(w, http.StatusNotFound, "task not found")
+ return db.AgentTaskQueue{}, false
+ }
+ slog.Warn("get agent task failed", "task_id", taskID, "error", err)
+ writeError(w, http.StatusInternalServerError, "failed to load task")
return db.AgentTaskQueue{}, false
}
diff --git a/server/internal/handler/daemon_test.go b/server/internal/handler/daemon_test.go
index 82fc6a790c..2e20d1075c 100644
--- a/server/internal/handler/daemon_test.go
+++ b/server/internal/handler/daemon_test.go
@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
+ "errors"
"net/http"
"net/http/httptest"
"strings"
@@ -11,7 +12,9 @@ import (
"time"
"github.com/go-chi/chi/v5"
+ "github.com/jackc/pgx/v5"
"github.com/multica-ai/multica/server/internal/middleware"
+ db "github.com/multica-ai/multica/server/pkg/db/generated"
)
// slowProbeLocalSkillListStore wraps a LocalSkillListStore but blocks inside
@@ -323,6 +326,50 @@ func TestGetTaskStatus_WithDaemonToken_CrossWorkspace(t *testing.T) {
}
}
+// TestGetTaskStatus_TransientDBError_Returns500 verifies that a transient DB
+// error from GetAgentTask is reported as 500 rather than 404. The daemon
+// uses 404+"task not found" as a hard cancel signal; a transient lookup
+// failure must therefore not be smuggled into that body, otherwise a single
+// DB hiccup would kill an in-flight agent.
+func TestGetTaskStatus_TransientDBError_Returns500(t *testing.T) {
+ h := &Handler{}
+ h.Queries = db.New(&mockDB{getUserErr: errors.New("connection reset by peer")})
+
+ w := httptest.NewRecorder()
+ req := newDaemonTokenRequest("GET", "/api/daemon/tasks/00000000-0000-0000-0000-000000000001/status", nil,
+ "00000000-0000-0000-0000-000000000000", "test-daemon")
+ req = withURLParam(req, "taskId", "00000000-0000-0000-0000-000000000001")
+
+ h.GetTaskStatus(w, req)
+ if w.Code != http.StatusInternalServerError {
+ t.Fatalf("transient DB error: expected 500, got %d: %s", w.Code, w.Body.String())
+ }
+ if strings.Contains(w.Body.String(), "task not found") {
+ t.Fatalf("transient DB error must not surface as %q (daemon treats that as a deletion): %s", "task not found", w.Body.String())
+ }
+}
+
+// TestGetTaskStatus_ErrNoRows_Returns404 verifies that an actually-missing
+// task row still returns the 404+"task not found" body the daemon relies on
+// to interrupt the running agent.
+func TestGetTaskStatus_ErrNoRows_Returns404(t *testing.T) {
+ h := &Handler{}
+ h.Queries = db.New(&mockDB{getUserErr: pgx.ErrNoRows})
+
+ w := httptest.NewRecorder()
+ req := newDaemonTokenRequest("GET", "/api/daemon/tasks/00000000-0000-0000-0000-000000000001/status", nil,
+ "00000000-0000-0000-0000-000000000000", "test-daemon")
+ req = withURLParam(req, "taskId", "00000000-0000-0000-0000-000000000001")
+
+ h.GetTaskStatus(w, req)
+ if w.Code != http.StatusNotFound {
+ t.Fatalf("ErrNoRows: expected 404, got %d: %s", w.Code, w.Body.String())
+ }
+ if !strings.Contains(w.Body.String(), "task not found") {
+ t.Fatalf("ErrNoRows: expected body to contain %q, got %s", "task not found", w.Body.String())
+ }
+}
+
func TestGetIssueGCCheck_WithDaemonToken_CrossWorkspace(t *testing.T) {
if testHandler == nil {
t.Skip("database not available")