Skip to content

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

Go

fix(runtime): pause autopilots inside the runtime-delete teardown transaction (#3880)

Bohan Jiang0da879ec8947
diff --git a/server/internal/handler/runtime.go b/server/internal/handler/runtime.go
index bfb19f00d7..f3e07dab68 100644
--- a/server/internal/handler/runtime.go
+++ b/server/internal/handler/runtime.go
@@ -587,33 +587,35 @@ func (h *Handler) DeleteAgentRuntime(w http.ResponseWriter, r *http.Request) {
return
}
+ tx, err := h.TxStarter.Begin(r.Context())
+ if err != nil {
+ writeError(w, http.StatusInternalServerError, "failed to delete runtime")
+ return
+ }
+ defer tx.Rollback(r.Context())
+ qtx := h.Queries.WithTx(tx)
+
// Pause autopilots pointing at the archived agents BEFORE we delete
// them. Migration 096 dropped the autopilot.assignee_id agent FK, so a
// hard-delete here would otherwise leave dangling rows that subsequent
// scheduler ticks would skip with "assignee agent no longer exists" —
// quiet, but burning a run record every tick until an operator notices.
// Pausing makes the breakage visible in the autopilot list so the owner
- // can re-point or delete the row instead.
- archivedAgentIDs, err := h.Queries.ListArchivedAgentIDsByRuntime(r.Context(), rt.ID)
+ // can re-point or delete the row instead. This runs inside the teardown
+ // transaction so a pause that lands but is followed by a failed delete
+ // rolls back with everything else, matching ArchiveAgentsAndDeleteRuntime.
+ archivedAgentIDs, err := qtx.ListArchivedAgentIDsByRuntime(r.Context(), rt.ID)
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to enumerate archived agents")
return
}
if len(archivedAgentIDs) > 0 {
- if err := h.Queries.PauseAutopilotsByAgentAssignees(r.Context(), archivedAgentIDs); err != nil {
- slog.Warn("pause autopilots for archived agents failed",
- "runtime_id", uuidToString(rt.ID), "error", err)
+ if err := qtx.PauseAutopilotsByAgentAssignees(r.Context(), archivedAgentIDs); err != nil {
+ writeError(w, http.StatusInternalServerError, "failed to pause autopilots")
+ return
}
}
- tx, err := h.TxStarter.Begin(r.Context())
- if err != nil {
- writeError(w, http.StatusInternalServerError, "failed to delete runtime")
- return
- }
- defer tx.Rollback(r.Context())
- qtx := h.Queries.WithTx(tx)
-
// Remove archived squads whose leader is an archived agent on this runtime
// so the RESTRICT FK on squad.leader_id won't block the subsequent agent
// deletion. Active squads are handled by the 409 guard above instead.