diff --git a/internal/httpapi/assistant.go b/internal/httpapi/assistant.go index 1030e9bc7..9d7c257bf 100644 --- a/internal/httpapi/assistant.go +++ b/internal/httpapi/assistant.go @@ -1759,7 +1759,7 @@ func runAssistantCLI(ctx context.Context, req assistantCLIRequest, user domain.U if req.Resume && missingSession && !modelStarted && final == "" && ctx.Err() == nil { return "", sessionID, errAssistantSessionMissing } - if engine != assistantEngineCodeBuddy && ctx.Err() == nil && strings.TrimSpace(final) == "" && + if engine != assistantEngineCodeBuddy && ctx.Err() == nil && (strings.TrimSpace(final) == "" || resultError) && (credentialRejected || (streamError != nil && assistantAuthFailureText(streamError.Error()))) { if req.RunnerLogin { return "", sessionID, assistantRunnerLoginError(engine) diff --git a/internal/httpapi/assistant_failure.go b/internal/httpapi/assistant_failure.go index dba9de521..26585d0d7 100644 --- a/internal/httpapi/assistant_failure.go +++ b/internal/httpapi/assistant_failure.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "net/http" "os" "os/exec" @@ -27,9 +28,12 @@ func assistantCLIMaxTurns(text string) bool { // Keep their terminal cause even when the process itself exits successfully. func assistantCLIStreamFailure(line string) error { var event struct { - Type string `json:"type"` - Error json.RawMessage `json:"error"` - Message struct { + Type string `json:"type"` + Error json.RawMessage `json:"error"` + APIErrorStatus int `json:"api_error_status"` + IsAPIErrorMessage bool `json:"is_api_error_message"` + IsError bool `json:"is_error"` + Message struct { Content []struct { Text string `json:"text"` } `json:"content"` @@ -38,6 +42,11 @@ func assistantCLIStreamFailure(line string) error { if json.Unmarshal([]byte(line), &event) != nil { return nil } + // Claude Code writes provider failures to stdout even when it exits nonzero. + apiFailure := event.Type == "assistant" && event.IsAPIErrorMessage || event.Type == "result" && event.IsError + if apiFailure && (event.APIErrorStatus == http.StatusUnauthorized || event.APIErrorStatus == http.StatusForbidden) { + return fmt.Errorf("HTTP %d: provider rejected the configured credentials", event.APIErrorStatus) + } if event.Type == "assistant" { for _, block := range event.Message.Content { if assistantCLIMaxTurns(block.Text) { diff --git a/internal/httpapi/assistant_failure_test.go b/internal/httpapi/assistant_failure_test.go index 43450f752..148c7c314 100644 --- a/internal/httpapi/assistant_failure_test.go +++ b/internal/httpapi/assistant_failure_test.go @@ -309,3 +309,45 @@ func TestFailedSharedGenerationSnapshotKeepsCauseAndRetryHistory(t *testing.T) { }) } } + +func TestAssistantClaudeReportsStructuredProviderAuthFailure(t *testing.T) { + for _, tc := range []struct { + name string + lines []string + }{ + { + name: "rejected key with nonempty failure result", + lines: []string{ + `{"type":"assistant","error":"authentication_failed","is_api_error_message":true,"api_error_status":401,"message":{"content":[{"type":"text","text":"Failed to authenticate. API Error: 401 Credentials missing. (MISSING_CREDENTIALS)"}]}}`, + `{"type":"result","is_error":true,"api_error_status":401,"result":"Failed to authenticate. API Error: 401 Credentials missing. (MISSING_CREDENTIALS)"}`, + }, + }, + { + name: "rejected credential reported only in failure result", + lines: []string{`{"type":"result","is_error":true,"api_error_status":403,"result":"Access denied"}`}, + }, + } { + t.Run(tc.name, func(t *testing.T) { + req := assistantCLIRequest{ + Executor: "claude-code", + Model: "gateway-test-model", + Prompt: "test", + SessionRoot: t.TempDir(), + Native: true, + RemoteBinary: true, + Execute: func(_ context.Context, _ string, _ runtime.CommandSpec, _ runtime.RunCredentials, sink runtime.EventSink) (string, error) { + sink(domain.TelemetryEvent{Name: "process.stderr", Message: "[claude-code:unrecognized_model]"}) + for _, line := range tc.lines { + sink(domain.TelemetryEvent{Name: "process.stdout", Message: line}) + } + return "", errors.New("exit status 1") + }, + } + reply, _, err := runAssistantCLI(context.Background(), req, domain.User{}, nil) + failure := classifyAssistantFailure(context.Background(), err) + if reply != "" || !errors.Is(err, errAssistantCredentialRejected) || failure.Code != "AUTHENTICATION_FAILED" { + t.Fatalf("reply=%q err=%v failure=%+v, want authentication failure", reply, err, failure) + } + }) + } +}