Skip to content

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

Go

fix(daemon): interrupt local agent on server-side terminal task states (#3878)

NanamiKite2e34016f1f2f
diff --git a/server/internal/daemon/client.go b/server/internal/daemon/client.go
index 0253986dab..cdcea09d55 100644
--- a/server/internal/daemon/client.go
+++ b/server/internal/daemon/client.go
@@ -267,7 +267,8 @@ func (c *Client) RecoverOrphans(ctx context.Context, runtimeID string) error {
}
// GetTaskStatus returns the current status of a task. Used by the daemon to
-// detect if a task was cancelled while it was executing.
+// detect terminal/interruption signals (cancelled, failed, completed, or a
+// 404 task-not-found) while a task is executing.
func (c *Client) GetTaskStatus(ctx context.Context, taskID string) (string, error) {
var resp struct {
Status string `json:"status"`
diff --git a/server/internal/daemon/daemon.go b/server/internal/daemon/daemon.go
index 12629c8b3b..e3e48eaa84 100644
--- a/server/internal/daemon/daemon.go
+++ b/server/internal/daemon/daemon.go
@@ -2137,10 +2137,17 @@ func newTaskSlotSemaphore(maxConcurrentTasks int) chan int {
// trivially testable; the polling goroutine in watchTaskCancellation is just
// I/O around it.
//
-// Two cases trigger cancellation:
+// Two conditions trigger cancellation:
//
-// 1. status == "cancelled" — the server moved the task to cancelled
-// (issue reassigned, user cancel, ...).
+// 1. status is a terminal state — "completed", "failed", or "cancelled"
+// (isAgentTaskTerminal). The server has already finalized the task: user
+// cancel, issue reassignment, the runtime offline sweeper flipping
+// running → failed during a disconnect, or a duplicate execution that
+// already completed it. Letting the local agent run on is pure waste —
+// CompleteAgentTask only accepts status == "running", so its eventual
+// CompleteTask/FailTask callback is guaranteed to fail and just adds log
+// noise. Reusing isAgentTaskTerminal keeps this set in lockstep with the
+// GC's notion of a terminal task.
// 2. err is a 404 with "task not found" — the task row was deleted while
// the agent was running. Without this we'd let the local agent keep
// emitting tool calls against a dead task for its full timeout window.
@@ -2152,7 +2159,7 @@ func shouldInterruptAgent(status string, err error) bool {
if err != nil {
return isTaskNotFoundError(err)
}
- return status == "cancelled"
+ return isAgentTaskTerminal(status)
}
// watchTaskCancellation polls the server for the task's status on the given
@@ -2176,7 +2183,7 @@ func (d *Daemon) watchTaskCancellation(ctx context.Context, taskID string, pollI
if err != nil {
taskLog.Info("task gone server-side, interrupting agent", "error", err)
} else {
- taskLog.Info("task cancelled by server, interrupting agent")
+ taskLog.Info("task reached terminal state server-side, interrupting agent", "status", status)
}
close(cancelled)
return
@@ -2248,8 +2255,9 @@ func (d *Daemon) handleTask(ctx context.Context, task Task, slot int) {
_ = d.client.ReportProgress(ctx, task.ID, fmt.Sprintf("Launching %s", provider), 1, 2)
// Create a cancellable context so we can interrupt the running agent
- // when the server signals the task should stop — either status moves
- // to "cancelled" or the task row is deleted (404).
+ // when the server signals the task should stop — either the task reached
+ // a terminal state (completed/failed/cancelled) or the task row is
+ // deleted (404).
runCtx, runCancel := context.WithCancel(ctx)
defer runCancel()
@@ -2306,10 +2314,11 @@ func (d *Daemon) handleTask(ctx context.Context, task Task, slot int) {
_ = d.client.ReportProgress(ctx, task.ID, "Finishing task", 2, 2)
- // 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.
+ // Final pre-completion check: if the server already moved the task to a
+ // terminal state (completed/failed/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
@@ -2411,10 +2420,10 @@ func (d *Daemon) acquireLocalDirectoryLockIfNeeded(ctx context.Context, task Tas
taskLog.Warn("local_directory: mark waiting status failed", "error", waitErr)
}
// Start polling once we actually park. shouldInterruptAgent inside
- // watchTaskCancellation already handles both server-side cancel
- // (status=cancelled) and the row-deleted reassignment case (404),
- // which is the full set of "this task shouldn't run anymore"
- // signals we need to react to during the wait.
+ // watchTaskCancellation already handles both server-side terminal
+ // states (completed/failed/cancelled) and the row-deleted
+ // reassignment case (404), which is the full set of "this task
+ // shouldn't run anymore" signals we need to react to during the wait.
watcherOnce.Do(func() {
cancelledByPoll = d.watchTaskCancellation(waitCtx, task.ID, pollInterval, taskLog)
go func() {
@@ -2428,15 +2437,15 @@ func (d *Daemon) acquireLocalDirectoryLockIfNeeded(ctx context.Context, task Tas
}
release, err = d.localPathLocks.Acquire(waitCtx, assignment.RealPath, task.ID, onWait)
if err != nil {
- // If the wait was cut short because the server cancelled the task
- // (or deleted the row), the row is already in a terminal state —
- // return silently the same way the run-phase poller does at
- // lines ~2104. Issuing FailTask here would be a no-op at best
+ // If the wait was cut short because the server finalized the task
+ // (terminal state) or deleted the row, the row is already in a
+ // terminal state — return silently the same way the run-phase poller
+ // does at lines ~2104. Issuing FailTask here would be a no-op at best
// and a confusing redundant log line at worst.
if cancelledByPoll != nil {
select {
case <-cancelledByPoll:
- taskLog.Info("local_directory: wait aborted by server-side cancel")
+ taskLog.Info("local_directory: wait aborted by server-side terminal state")
return nil, true
default:
}
diff --git a/server/internal/daemon/daemon_test.go b/server/internal/daemon/daemon_test.go
index e3dcb7da98..de1d462dea 100644
--- a/server/internal/daemon/daemon_test.go
+++ b/server/internal/daemon/daemon_test.go
@@ -703,8 +703,12 @@ func TestShouldInterruptAgent(t *testing.T) {
want bool
}{
{name: "status cancelled", status: "cancelled", err: nil, want: true},
+ {name: "status failed (offline sweeper)", status: "failed", err: nil, want: true},
+ {name: "status completed (finished elsewhere)", status: "completed", err: nil, want: true},
{name: "task deleted (404)", status: "", err: notFound, want: true},
{name: "running normally", status: "running", err: nil, want: false},
+ {name: "waiting_local_directory keeps running", status: "waiting_local_directory", err: nil, want: false},
+ {name: "dispatched keeps running", status: "dispatched", err: nil, want: false},
{name: "transient 5xx is not a cancel signal", status: "", err: transient, want: false},
{name: "no information yet", status: "", err: nil, want: false},
}
diff --git a/server/internal/handler/daemon.go b/server/internal/handler/daemon.go
index 0651be1e39..fb1da948dd 100644
--- a/server/internal/handler/daemon.go
+++ b/server/internal/handler/daemon.go
@@ -1929,7 +1929,8 @@ func (h *Handler) ReportTaskUsage(w http.ResponseWriter, r *http.Request) {
}
// GetTaskStatus returns the current status of a task.
-// Used by the daemon to check whether a task was cancelled mid-execution.
+// Used by the daemon to detect terminal/interruption signals (cancelled,
+// failed, completed) while a task is executing mid-flight.
func (h *Handler) GetTaskStatus(w http.ResponseWriter, r *http.Request) {
taskID := chi.URLParam(r, "taskId")