Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
perf(server): slim daemon task status lookup (#8484)
Multica Eve2b42c8e3f0c0
diff --git a/server/internal/handler/daemon.go b/server/internal/handler/daemon.goindex 9a15276d0b..173c46b9db 100644--- a/server/internal/handler/daemon.go+++ b/server/internal/handler/daemon.go@@ -4726,13 +4726,31 @@ func (h *Handler) ReportTaskUsage(w http.ResponseWriter, r *http.Request) {// failed, completed) while a task is executing mid-flight.func (h *Handler) GetTaskStatus(w http.ResponseWriter, r *http.Request) {taskID := chi.URLParam(r, "taskId")-- // Verify the caller owns this task's workspace.- task, ok := h.requireDaemonTaskAccess(w, r, taskID)+ taskUUID, ok := parseUUIDOrBadRequest(w, taskID, "task_id")if !ok {return}+ // This endpoint is polled by every in-flight daemon task. Load only the+ // status and the owning agent's workspace instead of the full task plus its+ // optional issue / chat / autopilot source chain.+ task, err := h.Queries.GetAgentTaskStatus(r.Context(), taskUUID)+ if err != nil {+ // A missing row is the daemon's hard interruption signal. Keep transient+ // database failures distinct so a brief outage cannot kill healthy work.+ if isNotFound(err) {+ writeError(w, http.StatusNotFound, "task not found")+ return+ }+ slog.Warn("get agent task status failed", "task_id", taskID, "error", err)+ writeError(w, http.StatusInternalServerError, "failed to load task")+ return+ }++ if !h.requireDaemonWorkspaceAccess(w, r, uuidToString(task.WorkspaceID)) {+ return+ }+writeJSON(w, http.StatusOK, map[string]string{"status": task.Status})}diff --git a/server/internal/handler/daemon_task_lookup_test.go b/server/internal/handler/daemon_task_lookup_test.goindex 73ad1f373d..27a0248f28 100644--- a/server/internal/handler/daemon_task_lookup_test.go+++ b/server/internal/handler/daemon_task_lookup_test.go@@ -15,10 +15,8 @@ import (// The daemon interrupts a running agent the moment a task-status poll answers// `404 task not found` (shouldInterruptAgent → isTaskNotFoundError). So that-// body is a kill signal, and only a lookup that completed and found nothing may-// produce it. #2127 established this for GetAgentTask; these tests hold the same-// line for the workspace resolution three lines below it, which #2127 missed and-// GH #8272 hit in production.+// body is a kill signal, and only a lookup that completed and found no task row+// may produce it. Transient failures and authorization misses must stay distinct.// lookupFaultPool fails one named query and passes everything else through, so// a single link in ResolveTaskWorkspaceIDChecked can be made to time out while@@ -37,11 +35,10 @@ func (f *lookupFaultPool) QueryRow(ctx context.Context, query string, args ...anreturn f.DBTX.QueryRow(ctx, query, args...)}-// TestGetTaskStatus_WorkspaceLookupFailure_Returns500 covers every link kind the-// resolver walks. A timeout on any of them must be a 5xx the daemon retries, not-// a deletion it acts on.-func TestGetTaskStatus_WorkspaceLookupFailure_Returns500(t *testing.T) {- ctx := context.Background()+// TestGetTaskStatus_DoesNotResolveSourceWorkspace pins the hot-path contract:+// status polling authorizes through the owning agent's workspace and never+// follows optional issue / chat / autopilot links.+func TestGetTaskStatus_DoesNotResolveSourceWorkspace(t *testing.T) {runtimeID := dbfx.Runtime(t, "MUL-7259 lookup runtime")agentID := dbfx.Agent(t, "MUL-7259 lookup agent", runtimeID)issueID := dbfx.Issue(t, "MUL-7259 lookup issue")@@ -63,38 +60,25 @@ func TestGetTaskStatus_WorkspaceLookupFailure_Returns500(t *testing.T) {"runtime_id": runtimeID, "status": "running", "started_at": testutil.Raw("now()"), tc.column: tc.id,})fault := &lookupFaultPool{DBTX: testPool, query: tc.query}- h := &Handler{Queries: db.New(testPool), TaskService: &service.TaskService{Queries: db.New(fault)}}+ h := &Handler{Queries: db.New(fault), TaskService: &service.TaskService{Queries: db.New(fault)}}req := newDaemonTokenRequest(http.MethodGet, "/api/daemon/tasks/"+taskID+"/status", nil, testWorkspaceID, "test-daemon")req = withURLParam(req, "taskId", taskID)- w := testutil.Call(t, h.GetTaskStatus, req).Want(http.StatusInternalServerError)- if !fault.called {- t.Fatal("fault was never exercised — the resolver did not reach the injected query")- }- // isTaskNotFoundError requires BOTH a 404 and this body, so the- // 500 above is already enough to keep the daemon from- // interrupting. The body is asserted too so the response never- // carries the cancel-triggering string at all, should either half- // of that check ever be relaxed.- if strings.Contains(w.Body.String(), "task not found") {- t.Fatalf("5xx body must not carry the daemon's cancel-triggering string: %s", w.Body.String())+ var response map[string]string+ testutil.Call(t, h.GetTaskStatus, req).Want(http.StatusOK).JSON(&response)+ if fault.called {+ t.Fatalf("status poll unexpectedly executed %s", tc.query)}-- task, err := testHandler.Queries.GetAgentTask(ctx, parseUUID(taskID))- if err != nil || task.Status != "running" {- t.Fatalf("task must be untouched and still running: status=%s err=%v", task.Status, err)+ if response["status"] != "running" {+ t.Fatalf("status = %q, want running", response["status"])}- // Once the dependency recovers the identical request succeeds, so- // the 5xx really was "ask again", not a masked permanent failure.- testutil.Call(t, testHandler.GetTaskStatus, req).Want(http.StatusOK)})}}-// TestGetTaskStatus_GenuinelyUnreachable_Returns404 is the other half of the-// contract: the 404 must survive for real absence, otherwise a deleted task-// would leave its agent running to its full timeout.-func TestGetTaskStatus_GenuinelyUnreachable_Returns404(t *testing.T) {+// TestGetTaskStatus_TaskRowPresenceContract distinguishes a genuinely missing+// task from a surviving task whose optional source row has gone away.+func TestGetTaskStatus_TaskRowPresenceContract(t *testing.T) {runtimeID := dbfx.Runtime(t, "MUL-7259 absent runtime")agentID := dbfx.Agent(t, "MUL-7259 absent agent", runtimeID)@@ -108,13 +92,12 @@ func TestGetTaskStatus_GenuinelyUnreachable_Returns404(t *testing.T) {}})- t.Run("link target missing", func(t *testing.T) {+ t.Run("optional source missing", func(t *testing.T) {// agent_task_queue.issue_id is ON DELETE CASCADE, so an issue task can- // never outlive its issue — that absence surfaces as ErrNoRows on the- // task itself (covered above). chat_session_id is ON DELETE SET NULL,- // so this is the shape where the resolver genuinely has nothing left to- // resolve, and it must stay a 404 rather than becoming a 5xx the daemon- // retries forever.+ // never outlive its issue. chat_session_id is ON DELETE SET NULL, so a+ // chat task can survive its source. The status endpoint now authorizes+ // that row through its owning agent instead of treating the optional+ // source as task identity.chatID := dbfx.ChatSession(t, agentID)taskID := dbfx.Task(t, agentID, testutil.Cols{"runtime_id": runtimeID, "status": "running",@@ -124,9 +107,10 @@ func TestGetTaskStatus_GenuinelyUnreachable_Returns404(t *testing.T) {req := newDaemonTokenRequest(http.MethodGet, "/api/daemon/tasks/"+taskID+"/status", nil, testWorkspaceID, "test-daemon")req = withURLParam(req, "taskId", taskID)- w := testutil.Call(t, testHandler.GetTaskStatus, req).Want(http.StatusNotFound)- if !strings.Contains(w.Body.String(), "task not found") {- t.Fatalf("a task whose only link is gone is unreachable: %s", w.Body.String())+ var response map[string]string+ testutil.Call(t, testHandler.GetTaskStatus, req).Want(http.StatusOK).JSON(&response)+ if response["status"] != "running" {+ t.Fatalf("status = %q, want running", response["status"])}})}@@ -146,7 +130,10 @@ func TestGetTaskStatus_ForeignWorkspace_Returns404(t *testing.T) {otherWorkspace := uuid.NewString()req := newDaemonTokenRequest(http.MethodGet, "/api/daemon/tasks/"+taskID+"/status", nil, otherWorkspace, "other-daemon")req = withURLParam(req, "taskId", taskID)- testutil.Call(t, testHandler.GetTaskStatus, req).Want(http.StatusNotFound)+ w := testutil.Call(t, testHandler.GetTaskStatus, req).Want(http.StatusNotFound)+ if strings.Contains(w.Body.String(), "task not found") {+ t.Fatalf("authorization miss must not carry the daemon's deletion signal: %s", w.Body.String())+ }}// TestResolveTaskWorkspaceIDChecked_SeparatesAbsenceFromFailure asserts thediff --git a/server/internal/handler/daemon_test.go b/server/internal/handler/daemon_test.goindex b6913876b2..f91eb3d083 100644--- a/server/internal/handler/daemon_test.go+++ b/server/internal/handler/daemon_test.go@@ -1121,7 +1121,7 @@ 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+// error from GetAgentTaskStatus 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.diff --git a/server/pkg/db/generated/agent.sql.go b/server/pkg/db/generated/agent.sql.goindex 65537eb434..b0cccb32ee 100644--- a/server/pkg/db/generated/agent.sql.go+++ b/server/pkg/db/generated/agent.sql.go@@ -4333,6 +4333,28 @@ func (q *Queries) GetAgentTaskInWorkspace(ctx context.Context, arg GetAgentTaskIreturn i, err}+const getAgentTaskStatus = `-- name: GetAgentTaskStatus :one+SELECT atq.status, a.workspace_id+FROM agent_task_queue atq+JOIN agent a ON a.id = atq.agent_id+WHERE atq.id = $1+`++type GetAgentTaskStatusRow struct {+ Status string `json:"status"`+ WorkspaceID pgtype.UUID `json:"workspace_id"`+}++// Hot-path status polling needs only the task status and the owning agent's+// workspace for authorization. Keep this independent of optional source links+// (issue, chat session, autopilot run) so it needs no source-entity lookup.+func (q *Queries) GetAgentTaskStatus(ctx context.Context, id pgtype.UUID) (GetAgentTaskStatusRow, error) {+ row := q.db.QueryRow(ctx, getAgentTaskStatus, id)+ var i GetAgentTaskStatusRow+ err := row.Scan(&i.Status, &i.WorkspaceID)+ return i, err+}+const getCommentThreadRootID = `-- name: GetCommentThreadRootID :oneSELECT comment_thread_root_id($1::uuid)::uuid AS id`diff --git a/server/pkg/db/queries/agent.sql b/server/pkg/db/queries/agent.sqlindex da708ee24f..d6224e5170 100644--- a/server/pkg/db/queries/agent.sql+++ b/server/pkg/db/queries/agent.sql@@ -690,6 +690,15 @@ RETURNING *;SELECT * FROM agent_task_queueWHERE id = $1;+-- name: GetAgentTaskStatus :one+-- Hot-path status polling needs only the task status and the owning agent's+-- workspace for authorization. Keep this independent of optional source links+-- (issue, chat session, autopilot run) so it needs no source-entity lookup.+SELECT atq.status, a.workspace_id+FROM agent_task_queue atq+JOIN agent a ON a.id = atq.agent_id+WHERE atq.id = $1;+-- name: GetAgentTaskForDelegatedFailureUpdate :one-- Serializes the idempotent delegated-failure recovery signal for one failed-- task. FailTask and the stale-task sweepers can converge on the same row; the