Skip to content

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

Go

fix(cursor): resume stalled provider connections with bounded retries (MUL-7968) (#9180)

mrlonely2b6db0d2b8fa
diff --git a/server/internal/daemon/cursor_network_resume_test.go b/server/internal/daemon/cursor_network_resume_test.go
new file mode 100644
index 0000000000..2eda742585
--- /dev/null
+++ b/server/internal/daemon/cursor_network_resume_test.go
@@ -0,0 +1,22 @@
+package daemon
+
+import (
+ "testing"
+
+ "github.com/multica-ai/multica/server/pkg/agent"
+)
+
+func TestCursorConnectionStalledDoesNotStartFreshSession(t *testing.T) {
+ t.Parallel()
+ for _, raw := range []string{
+ "cursor-agent exited with error: exit status 1; cursor stderr: RetriableError: Connection stalled",
+ "cursor-agent prompt write failed: write |1: broken pipe; cursor stderr: RetriableError: Connection stalled repeatedly",
+ } {
+ for _, tools := range []int32{0, 41} {
+ result := agent.Result{Status: "failed", Error: raw}
+ if shouldRetryWithFreshSession(result, "existing-cursor-session", tools, "cursor") {
+ t.Errorf("transport failure must not discard the prior session (tools=%d, error=%q)", tools, raw)
+ }
+ }
+ }
+}
diff --git a/server/internal/service/cursor_network_retry_test.go b/server/internal/service/cursor_network_retry_test.go
new file mode 100644
index 0000000000..9ff60bba68
--- /dev/null
+++ b/server/internal/service/cursor_network_retry_test.go
@@ -0,0 +1,47 @@
+package service
+
+import (
+ "testing"
+
+ "github.com/jackc/pgx/v5/pgtype"
+ db "github.com/multica-ai/multica/server/pkg/db/generated"
+ "github.com/multica-ai/multica/server/pkg/taskfailure"
+)
+
+func TestCursorConnectionStalledRetriesWithSession(t *testing.T) {
+ t.Parallel()
+ const raw = "cursor-agent exited with error: exit status 1 (result_seen=false, exit_code=1, scanner_error=false, event_count=463, invalid_event_count=0, last_event_type=system); actions completed before finalization may already have taken effect; cursor stderr: RetriableError: Connection stalled"
+ for _, reported := range []string{"", "agent_error.process_failure", "agent_error.unknown", "agent_error"} {
+ t.Run(reported, func(t *testing.T) {
+ reason := reported
+ if reason == "" {
+ reason = taskfailure.Classify(raw).String()
+ }
+ reason = taskfailure.NormalizeDaemonReason(reason, raw).String()
+ if reason != taskfailure.ReasonAgentProviderNetwork.String() {
+ t.Fatalf("resolved reason = %q, want provider_network", reason)
+ }
+ if resumeUnsafeFailureReason(reason) {
+ t.Fatal("stalled transport must preserve the session for retry")
+ }
+ for _, tc := range []struct {
+ name string
+ attempt, max int32
+ want bool
+ }{
+ {"first failure", 1, 2, true},
+ {"second failure", 2, 2, true},
+ {"retry ceiling", providerNetworkMaxAttempts, 2, false},
+ {"explicitly disabled", 1, 1, false},
+ } {
+ task := db.AgentTaskQueue{
+ Attempt: tc.attempt, MaxAttempts: tc.max,
+ IssueID: pgtype.UUID{Bytes: [16]byte{1}, Valid: true},
+ }
+ if got := retryEligible(reason, task); got != tc.want {
+ t.Errorf("%s: retryEligible = %v, want %v", tc.name, got, tc.want)
+ }
+ }
+ })
+ }
+}
diff --git a/server/pkg/agent/cursor_execute_unix_test.go b/server/pkg/agent/cursor_execute_unix_test.go
index 2eec7f876e..8b5a45f5cd 100644
--- a/server/pkg/agent/cursor_execute_unix_test.go
+++ b/server/pkg/agent/cursor_execute_unix_test.go
@@ -31,6 +31,38 @@ import (
// structured-stream-error cases rank above it and are unaffected.
const drainStdin = "cat > /dev/null"
+func TestCursorExecuteConnectionStalledDiagnostics(t *testing.T) {
+ t.Parallel()
+ script := `#!/bin/sh
+` + drainStdin + `
+i=0
+while [ "$i" -lt 500 ]; do
+ printf '%s\n' '{"type":"system","subtype":"init","session_id":"sess-stalled"}'
+ i=$((i + 1))
+done
+printf '%s\n' 'RetriableError: Connection stalled' >&2
+exit 1
+`
+ result := executeFakeCursor(t, script)
+ if result.Status != "failed" || result.Output != "" {
+ t.Fatalf("stalled run must remain a failure without final output: %+v", result)
+ }
+ if result.SessionID != "sess-stalled" {
+ t.Fatalf("session id = %q, want sess-stalled", result.SessionID)
+ }
+ for _, want := range []string{"event_count=500", "cursor stderr: RetriableError: Connection stalled", cursorIncompleteFinalizationWarning} {
+ if !strings.Contains(result.Error, want) {
+ t.Errorf("error = %q, want substring %q", result.Error, want)
+ }
+ }
+ if got := taskfailure.Classify(result.Error); got != taskfailure.ReasonAgentProviderNetwork {
+ t.Fatalf("adapter error classified as %s, want provider_network", got)
+ }
+ if got := taskfailure.NormalizeDaemonReason(taskfailure.ReasonAgentProviderServerError.String(), result.Error); got != taskfailure.ReasonAgentProviderNetwork {
+ t.Fatalf("legacy daemon reason normalized to %s, want provider_network", got)
+ }
+}
+
func TestCursorExecuteStopsAfterTerminalResult(t *testing.T) {
t.Parallel()
diff --git a/server/pkg/taskfailure/classify.go b/server/pkg/taskfailure/classify.go
index 70a4221d5e..189485004e 100644
--- a/server/pkg/taskfailure/classify.go
+++ b/server/pkg/taskfailure/classify.go
@@ -85,6 +85,24 @@ func Classify(rawError string) Reason {
return ReasonAgentUnknown
}
lower := strings.ToLower(trimmed)
+ // Cursor's adapter adds event counts to known transport failures. Those
+ // numbers are not HTTP status codes (event_count=429 is not a rate limit).
+ // Apply the existing precedence to the provider stderr, keeping real auth
+ // or quota evidence there while excluding the adapter's diagnostics.
+ if isCursorProviderNetworkError(lower) {
+ if stderr, wrapped := cursorProviderStderr(lower); wrapped {
+ lower = stderr
+ }
+ // Node stack locations such as index.js:500:403 are not HTTP codes
+ // either. Keep non-frame error lines, including actual provider errors.
+ lines := strings.Split(lower, "\n")
+ for i, line := range lines {
+ if cursorStackFrame(line) {
+ lines[i] = ""
+ }
+ }
+ lower = strings.Join(lines, "\n")
+ }
switch {
// A concurrent-request rejection can contain both "access token" and HTTP
@@ -227,9 +245,9 @@ func Classify(rawError string) Reason {
// messages and the stable Pi/OMP exit composite, rather than treating
// the same broad substrings from local tools or MCP servers as retryable.
// Mirror these Pi message shapes into the MUL-1949 offline backfill SQL.
- // Cursor can exit before its first stream event with a Node connect
- // ETIMEDOUT error. Keep that failed resume network-safe instead of
- // letting the exit-status wrapper trigger a fresh-session retry.
+ // Cursor reports connect ETIMEDOUT and mid-stream RetriableError:
+ // Connection stalled failures. Keep those failed resumes network-safe
+ // instead of letting the exit-status wrapper trigger a fresh-session retry.
case isPiProviderNetworkError(lower), isCursorProviderNetworkError(lower),
containsAny(lower,
"stream disconnected",
@@ -448,18 +466,72 @@ var legacyEnvironmentPrepareWitnesses = []string{
"reuse execution environment:",
}
-// isCursorProviderNetworkError recognizes the captured Cursor provider error,
-// bare or in the adapter's process-failure wrapper. Do not match ETIMEDOUT
-// globally: a local tool or MCP connection timeout is not provider evidence.
+// isCursorProviderNetworkError recognizes captured Cursor provider errors,
+// bare or in the adapter's exit/prompt-write wrapper. Do not match transport
+// phrases globally: local tool or MCP failures are not provider evidence.
func isCursorProviderNetworkError(lower string) bool {
- if strings.HasPrefix(lower, "cursor-agent exited with error: ") {
- _, stderr, ok := strings.Cut(lower, "; cursor stderr: ")
- if !ok {
- return false
+ if stderr, wrapped := cursorProviderStderr(lower); wrapped {
+ lower = stderr
+ }
+ if strings.HasPrefix(lower, "error: [unavailable] connect etimedout ") {
+ return true
+ }
+ // Node may append a stack trace. Match the exact first-line message, not
+ // an arbitrary RetriableError or a substring quoted by another tool.
+ firstLine, _, _ := strings.Cut(lower, "\n")
+ switch strings.TrimSpace(firstLine) {
+ case "retriableerror: connection stalled", "retriableerror: connection stalled repeatedly":
+ return true
+ }
+ return false
+}
+
+func cursorProviderStderr(lower string) (string, bool) {
+ if !strings.HasPrefix(lower, "cursor-agent exited with error: ") &&
+ !strings.HasPrefix(lower, "cursor-agent prompt write failed: ") {
+ return "", false
+ }
+ _, stderr, ok := strings.Cut(lower, "; cursor stderr: ")
+ return strings.TrimSpace(stderr), ok
+}
+
+func cursorStackFrame(line string) bool {
+ return (strings.HasPrefix(line, " ") || strings.HasPrefix(line, "\t")) &&
+ strings.HasPrefix(strings.TrimSpace(line), "at ")
+}
+
+// Refined status buckets can also be stale when an old daemon matched a count
+// or stack location. Require the same classification from that diagnostic text;
+// a plain transport message or a genuinely refined reason stays intact.
+func cursorDiagnosticReason(reason, lower string) bool {
+ switch Reason(reason) {
+ case ReasonAgentProviderAuthOrAccess, ReasonAgentProviderQuotaLimit,
+ ReasonAgentProviderCapacityOrRateLimit, ReasonAgentProviderServerError:
+ default:
+ return false
+ }
+ var diagnostics []string
+ if _, wrapped := cursorProviderStderr(lower); wrapped {
+ header, _, _ := strings.Cut(lower, "; cursor stderr: ")
+ if strings.Contains(header, " (result_seen=false, ") {
+ diagnostics = append(diagnostics, header)
}
- lower = strings.TrimSpace(stderr)
}
- return strings.HasPrefix(lower, "error: [unavailable] connect etimedout ")
+ for _, line := range strings.Split(lower, "\n") {
+ if cursorStackFrame(line) {
+ diagnostics = append(diagnostics, line)
+ }
+ }
+ return len(diagnostics) > 0 && Classify(strings.Join(diagnostics, "\n")) == Reason(reason)
+}
+
+// Older daemons report these Cursor transport failures as process_failure
+// (wrapped exit), unknown (bare stderr), or the coarse agent_error bucket.
+// Preserve more specific reasons such as auth, quota, and hard timeouts.
+var legacyCursorProviderNetworkReasons = map[string]bool{
+ string(ReasonAgentProcessFailure): true,
+ string(ReasonAgentUnknown): true,
+ "agent_error": true,
}
func isPiProviderNetworkError(lower string) bool {
@@ -568,6 +640,13 @@ func NormalizeDaemonReason(reason, rawError string) Reason {
// flaky provider. Upgrading here makes the retry work the moment the server
// deploys, without waiting on the daemon fleet.
lowerError := strings.ToLower(strings.TrimSpace(rawError))
+ // Normalize before retry eligibility is evaluated so installed daemons
+ // receive the same bounded, resume-safe recovery as current ones. Retain
+ // Classify's precedence if a more specific provider failure is present.
+ if (legacyCursorProviderNetworkReasons[reason] || cursorDiagnosticReason(reason, lowerError)) &&
+ isCursorProviderNetworkError(lowerError) && Classify(rawError) == ReasonAgentProviderNetwork {
+ return ReasonAgentProviderNetwork
+ }
if legacyOpencodeStreamEndedReasons[reason] &&
(strings.HasPrefix(lowerError, opencodeStreamEndedPrefix) ||
strings.HasPrefix(lowerError, codeartsStreamEndedPrefix)) {
diff --git a/server/pkg/taskfailure/cursor_network_test.go b/server/pkg/taskfailure/cursor_network_test.go
index 5f06502701..08973c75d6 100644
--- a/server/pkg/taskfailure/cursor_network_test.go
+++ b/server/pkg/taskfailure/cursor_network_test.go
@@ -1,6 +1,7 @@
package taskfailure
import (
+ "fmt"
"strings"
"testing"
)
@@ -9,10 +10,29 @@ func TestClassifyCursorConnectTimeout(t *testing.T) {
t.Parallel()
const stderr = "Error: [unavailable] connect ETIMEDOUT 192.0.2.1:443"
const wrapped = "cursor-agent exited with error: exit status 1 (result_seen=false, exit_code=1, scanner_error=false, event_count=0, invalid_event_count=0, last_event_type=none); actions completed before finalization may already have taken effect; cursor stderr: " + stderr
- for _, input := range []string{stderr, wrapped, strings.ToUpper(wrapped)} {
+ const writeFailure = "cursor-agent prompt write failed: write |1: broken pipe; cursor stderr: " + stderr
+ for _, input := range []string{stderr, wrapped, strings.ToUpper(wrapped), writeFailure} {
if got := Classify(input); got != ReasonAgentProviderNetwork {
t.Errorf("Classify(%q) = %s, want %s", input, got, ReasonAgentProviderNetwork)
}
+ if got := NormalizeDaemonReason(ReasonAgentProcessFailure.String(), input); got != ReasonAgentProviderNetwork {
+ t.Errorf("NormalizeDaemonReason(process_failure, %q) = %s, want %s", input, got, ReasonAgentProviderNetwork)
+ }
+ }
+}
+
+func TestCursorTransportDiagnosticCountsAreNotHTTPStatus(t *testing.T) {
+ t.Parallel()
+ for _, count := range []int{401, 402, 403, 429, 500, 529, 599} {
+ raw := fmt.Sprintf("cursor-agent exited with error: exit status 1 (result_seen=false, exit_code=1, scanner_error=false, event_count=%d, invalid_event_count=0, last_event_type=system); actions completed before finalization may already have taken effect; cursor stderr: RetriableError: Connection stalled", count)
+ if got := Classify(raw); got != ReasonAgentProviderNetwork {
+ t.Errorf("event_count=%d classified as %s, want provider_network", count, got)
+ }
+ header, _, _ := strings.Cut(raw, "; cursor stderr: ")
+ legacy := Classify(header)
+ if got := NormalizeDaemonReason(legacy.String(), raw); got != ReasonAgentProviderNetwork {
+ t.Errorf("event_count=%d: NormalizeDaemonReason(%s) = %s, want provider_network", count, legacy, got)
+ }
}
}
@@ -24,9 +44,74 @@ func TestClassifyCursorConnectTimeoutScope(t *testing.T) {
"cursor-agent exited with error: exit status 1; cursor stderr: local tool failed: connect ETIMEDOUT 192.0.2.1:443",
"cursor-agent exited with error: exit status 1; cursor stderr: Error: [unavailable] connect ETIMEDOUT_OTHER 192.0.2.1:443",
"some tool exited with error: exit status 1; cursor stderr: Error: [unavailable] connect ETIMEDOUT 192.0.2.1:443",
+ "cursor-agent prompt write failed: write |1: broken pipe",
+ "cursor-agent prompt write failed: write |1: broken pipe; cursor stderr: local tool failed: connect ETIMEDOUT 192.0.2.1:443",
} {
if got := Classify(input); got == ReasonAgentProviderNetwork {
t.Errorf("Classify(%q) must not classify unrelated tool failures as provider network errors", input)
}
}
}
+
+func TestClassifyCursorConnectionStalled(t *testing.T) {
+ t.Parallel()
+ for _, stderr := range []string{
+ "RetriableError: Connection stalled",
+ "RetriableError: Connection stalled repeatedly",
+ "RetriableError: Connection stalled\n at transport (/cursor/index.js:1:1)",
+ "RetriableError: Connection stalled\n at transport (/cursor/index.js:500:403)",
+ } {
+ for _, input := range []string{
+ stderr,
+ "cursor-agent exited with error: exit status 1 (result_seen=false, exit_code=1, scanner_error=false, event_count=463, invalid_event_count=0, last_event_type=system); actions completed before finalization may already have taken effect; cursor stderr: " + stderr,
+ "cursor-agent prompt write failed: write |1: broken pipe; cursor stderr: " + stderr,
+ strings.ToUpper(stderr),
+ strings.ToUpper("cursor-agent exited with error: exit status 1; cursor stderr: " + stderr),
+ } {
+ if got := Classify(input); got != ReasonAgentProviderNetwork {
+ t.Errorf("Classify(%q) = %s, want %s", input, got, ReasonAgentProviderNetwork)
+ }
+ for _, legacy := range []Reason{ReasonAgentProcessFailure, ReasonAgentUnknown, "agent_error"} {
+ if got := NormalizeDaemonReason(legacy.String(), input); got != ReasonAgentProviderNetwork {
+ t.Errorf("NormalizeDaemonReason(%s, %q) = %s, want %s", legacy, input, got, ReasonAgentProviderNetwork)
+ }
+ }
+ }
+ }
+}
+
+func TestCursorConnectionStalledScope(t *testing.T) {
+ t.Parallel()
+ for _, input := range []string{
+ "Connection stalled",
+ "RetriableError: Something else",
+ "RetriableError: Connection stalled_OTHER",
+ "RetriableError: Connection stalled in a local tool",
+ "MCP server error: RetriableError: Connection stalled",
+ "some tool exited with error: exit status 1; cursor stderr: RetriableError: Connection stalled",
+ "cursor-agent exited with error: exit status 1; cursor stderr: local tool failed: RetriableError: Connection stalled",
+ "cursor-agent prompt write failed: broken pipe; cursor stderr: MCP server error: RetriableError: Connection stalled",
+ "cursor-agent exited with error: exit status 1; RetriableError: Connection stalled",
+ "RetriableError: Connection stalled\nAPI Error: 403 Forbidden",
+ } {
+ if got := Classify(input); got == ReasonAgentProviderNetwork {
+ t.Errorf("Classify(%q) must not classify unrelated failures as provider network errors", input)
+ }
+ if got := NormalizeDaemonReason(ReasonAgentProcessFailure.String(), input); got != ReasonAgentProcessFailure {
+ t.Errorf("NormalizeDaemonReason(process_failure, %q) = %s, want it preserved", input, got)
+ }
+ }
+ stack := "RetriableError: Connection stalled\n at transport (/cursor/index.js:500:403)"
+ if got := NormalizeDaemonReason(ReasonAgentProviderAuthOrAccess.String(), stack); got != ReasonAgentProviderNetwork {
+ t.Errorf("NormalizeDaemonReason(auth, stalled with diagnostic stack location) = %s, want provider_network", got)
+ }
+ for _, reason := range []Reason{ReasonAgentProviderAuthOrAccess, ReasonAgentProviderQuotaLimit, ReasonAgentContextOverflow, ReasonAgentTimeout} {
+ if got := NormalizeDaemonReason(reason.String(), "RetriableError: Connection stalled"); got != reason {
+ t.Errorf("NormalizeDaemonReason(%s, stalled) = %s, want it preserved", reason, got)
+ }
+ wrapped := "cursor-agent exited with error: exit status 1; cursor stderr: RetriableError: Connection stalled"
+ if got := NormalizeDaemonReason(reason.String(), wrapped); got != reason {
+ t.Errorf("NormalizeDaemonReason(%s, wrapped stalled without diagnostic count) = %s, want it preserved", reason, got)
+ }
+ }
+}