* Handle Claude sign-in failures without timed retries * Judge a Claude login by the credential it has now A refusal is kept for the credential it was made on (savedLogin.Refused) and holds only while the account still has that credential. One check, claudeSignedOut, decides whether a saved Claude login can be used, after syncClaudeDir has read what Claude Code keeps in the account's directory: a credential it refreshed to there is taken, and one it emptied (as Claude Code 2.1.x does after Anthropic answers invalid_grant) is refused. Token is the only gate: a saved account's directory, or the credential Claude Code itself holds for its own account. Lapsed accounts stay candidates, so the trace shows each one passed over and the agent is told to sign in again when none is left. This fixes four paths of the first version: a moment the own sign-in could not be read left it lapsed after it came back unchanged; a credential refreshed in the account's directory after a refusal was ignored; a switch could restore a copy Claude Code had emptied; and with every account refused the agent got "none of ... is ready" (404). * Limit sign-in lapses to Claude accounts Address the review of #716: - A refused sign-in is failAuth only for a Claude account (failureOf); another vendor with the same words rests as before. - The account list's sign-in-required row is Claude's only, and the account Claude Code is signed in to offers no Remove. - Add the missing zh string for "its sign-in is gone; sign in again". - readOutput checks the error before reading Claude Code's sign-in, and the owner is parsed in one place (ownerAccount). TestSignInWordsFromAnotherVendorRest and the new browser case in claude-auth-status.test.cjs fail on the previous commit and pass.
62 lines
2.1 KiB
Go
62 lines
2.1 KiB
Go
package provider
|
|
|
|
// A Claude account is tested as it is used: by Claude Code itself, never
|
|
// by a request magpie makes to Anthropic's API with the account's sign-in.
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
// errClaudeViaCLI is what a request straight to Anthropic's API on a
|
|
// Claude account comes to: such requests run Claude Code instead.
|
|
var errClaudeViaCLI = errors.New("a Claude account is used through Claude Code, not by requests to Anthropic's API")
|
|
|
|
// claudeCLIProbe has Claude Code answer one "hi" at model, with configDir
|
|
// the config directory of a saved account (claude_dirs.go), "" for the one
|
|
// Claude Code is signed in to.
|
|
// The gateway, which runs Claude Code, sets it (ProbeClaudeVia).
|
|
var claudeCLIProbe func(ctx context.Context, configDir, model string) error
|
|
|
|
// ProbeClaudeVia sets how a Claude account's test runs Claude Code.
|
|
func ProbeClaudeVia(f func(ctx context.Context, configDir, model string) error) { claudeCLIProbe = f }
|
|
|
|
// claudeWait is how long a test waits on Claude Code, which takes a few
|
|
// seconds to start before it asks.
|
|
const claudeWait = time.Minute
|
|
|
|
// testClaude is a Claude account's test of model, run by Claude Code.
|
|
func (p Provider) testClaude(ctx context.Context, model string) Result {
|
|
r := Result{Protocol: Anthropic, Model: model, Account: p.Account.User}
|
|
if model == "" {
|
|
r.Error = "no model to try: expose one, or refresh the model list"
|
|
return r
|
|
}
|
|
if claudeCLIProbe == nil {
|
|
r.Error = "Claude Code isn't available to run the test"
|
|
return r
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, claudeWait)
|
|
defer cancel()
|
|
dir, _, err := p.Account.Token(ctx)
|
|
if err != nil {
|
|
r.Error = err.Error()
|
|
return r
|
|
}
|
|
start := time.Now()
|
|
version := ClaudeLoginVersion(p.Account.User)
|
|
err = claudeCLIProbe(ctx, dir, model)
|
|
r.Millis = time.Since(start).Milliseconds()
|
|
if err != nil {
|
|
NoteClaudeSignInFailure(p.Account.User, version, err.Error())
|
|
r.Error = err.Error()
|
|
return r
|
|
}
|
|
r.OK = true
|
|
return r
|
|
}
|
|
|
|
// isClaudeAccount is whether p is a Claude subscription account.
|
|
func (p Provider) isClaudeAccount() bool { return p.Account != nil && p.Account.Agent == "claude" }
|