Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(agent): surface codex turn errors instead of reporting empty output (#1156)
Bohan Jiangac8b08e540c4
diff --git a/server/pkg/agent/codex.go b/server/pkg/agent/codex.goindex 649bad68b4..b1fcd53182 100644--- a/server/pkg/agent/codex.go+++ b/server/pkg/agent/codex.go@@ -200,9 +200,15 @@ func (b *codexBackend) Execute(ctx context.Context, prompt string, opts ExecOpti// Wait for turn completion or context cancellationselect {case aborted := <-turnDone:- if aborted {+ switch {+ case aborted:finalStatus = "aborted"finalError = "turn was aborted"+ default:+ if errMsg := c.getTurnError(); errMsg != "" {+ finalStatus = "failed"+ finalError = errMsg+ }}case <-runCtx.Done():if runCtx.Err() == context.DeadlineExceeded {@@ -287,6 +293,26 @@ type codexClient struct {usageMu sync.Mutexusage TokenUsage // accumulated from turn events++ turnErrorMu sync.Mutex+ turnError string // captured from turn/completed status=failed or terminal error notifications+}++func (c *codexClient) setTurnError(msg string) {+ if msg == "" {+ return+ }+ c.turnErrorMu.Lock()+ defer c.turnErrorMu.Unlock()+ if c.turnError == "" {+ c.turnError = msg+ }+}++func (c *codexClient) getTurnError() string {+ c.turnErrorMu.Lock()+ defer c.turnErrorMu.Unlock()+ return c.turnError}type pendingRPC struct {@@ -567,6 +593,16 @@ func (c *codexClient) handleRawNotification(method string, params map[string]anyaborted := status == "cancelled" || status == "canceled" ||status == "aborted" || status == "interrupted"+ // Capture the error message from failed turns so callers can surface+ // a real reason instead of falling back to "empty output".+ if status == "failed" {+ errMsg := extractNestedString(params, "turn", "error", "message")+ if errMsg == "" {+ errMsg = "codex turn failed"+ }+ c.setTurnError(errMsg)+ }+if c.completedTurnIDs == nil {c.completedTurnIDs = map[string]bool{}}@@ -586,6 +622,22 @@ func (c *codexClient) handleRawNotification(method string, params map[string]anyc.onTurnDone(aborted)}+ case "error":+ // Top-level protocol error. Retrying notifications (willRetry=true) are+ // transient reconnect attempts; only capture terminal errors so we+ // don't stomp on a real failure later with a retry placeholder.+ willRetry, _ := params["willRetry"].(bool)+ errMsg := extractNestedString(params, "error", "message")+ if errMsg == "" {+ errMsg = extractNestedString(params, "message")+ }+ if errMsg != "" {+ c.cfg.Logger.Warn("codex error notification", "message", errMsg, "will_retry", willRetry)+ if !willRetry {+ c.setTurnError(errMsg)+ }+ }+case "thread/status/changed":statusType := extractNestedString(params, "status", "type")if statusType == "idle" && c.turnStarted {diff --git a/server/pkg/agent/codex_test.go b/server/pkg/agent/codex_test.goindex 2312b4b326..b416da38a0 100644--- a/server/pkg/agent/codex_test.go+++ b/server/pkg/agent/codex_test.go@@ -349,6 +349,80 @@ func TestCodexRawTurnCompletedAborted(t *testing.T) {}}+func TestCodexRawTurnCompletedFailedCapturesError(t *testing.T) {+ t.Parallel()++ c, _, _ := newTestCodexClient(t)+ c.notificationProtocol = "raw"++ var wasAborted bool+ c.onTurnDone = func(aborted bool) {+ wasAborted = aborted+ }++ c.handleLine(`{"jsonrpc":"2.0","method":"turn/completed","params":{"turn":{"id":"turn-f","status":"failed","error":{"message":"unexpected status 401 Unauthorized"}}}}`)++ if wasAborted {+ t.Fatal("failed is distinct from aborted")+ }+ if got := c.getTurnError(); got != "unexpected status 401 Unauthorized" {+ t.Fatalf("expected error captured from turn.error.message, got %q", got)+ }+}++func TestCodexRawTurnCompletedFailedWithoutMessageFallsBack(t *testing.T) {+ t.Parallel()++ c, _, _ := newTestCodexClient(t)+ c.notificationProtocol = "raw"+ c.onTurnDone = func(aborted bool) {}++ c.handleLine(`{"jsonrpc":"2.0","method":"turn/completed","params":{"turn":{"id":"turn-f","status":"failed"}}}`)++ if got := c.getTurnError(); got != "codex turn failed" {+ t.Fatalf("expected fallback message, got %q", got)+ }+}++func TestCodexRawErrorNotificationTerminal(t *testing.T) {+ t.Parallel()++ c, _, _ := newTestCodexClient(t)+ c.notificationProtocol = "raw"++ c.handleLine(`{"jsonrpc":"2.0","method":"error","params":{"error":{"message":"boom"},"willRetry":false}}`)++ if got := c.getTurnError(); got != "boom" {+ t.Fatalf("expected terminal error captured, got %q", got)+ }+}++func TestCodexRawErrorNotificationRetryingIgnored(t *testing.T) {+ t.Parallel()++ c, _, _ := newTestCodexClient(t)+ c.notificationProtocol = "raw"++ c.handleLine(`{"jsonrpc":"2.0","method":"error","params":{"error":{"message":"reconnecting"},"willRetry":true}}`)++ if got := c.getTurnError(); got != "" {+ t.Fatalf("retrying error should not be captured, got %q", got)+ }+}++func TestCodexSetTurnErrorFirstWins(t *testing.T) {+ t.Parallel()++ c, _, _ := newTestCodexClient(t)++ c.setTurnError("first")+ c.setTurnError("second")++ if got := c.getTurnError(); got != "first" {+ t.Fatalf("expected first-wins semantics, got %q", got)+ }+}+func TestCodexRawItemCommandExecution(t *testing.T) {t.Parallel()