Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/httpapi/assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 12 additions & 3 deletions internal/httpapi/assistant_failure.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"os/exec"
Expand All @@ -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"`
Expand All @@ -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) {
Expand Down
42 changes: 42 additions & 0 deletions internal/httpapi/assistant_failure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}