carlos / platform Public

carlos auth login --wait: the code's own expiry ends the attempt

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 6f6b817b9e7aa455d68c6a815d21af0957ebd7be parent 745a7de
2 files changed, +74 −5
  • auth.go +26 −4
  • authpending_test.go +48 −1
diff --git a/auth.go b/auth.go
index b428c5b07..ba6f7d6cb 100644
--- a/auth.go
+++ b/auth.go
@@ -717,6 +717,16 @@ func agentLabel(env func(string) string, fallback string) string {
return "Claude Code on " + host
}
+// loginWaitCap bounds one --wait whatever the pending file says. A variable
+// only so a test can cut a wait short without reaching the code's expiry.
+var loginWaitCap = maxLoginWait
+
+// errLoginGaveUp is pollLogin running out of time, not the console's verdict.
+var errLoginGaveUp = errors.New("gave up waiting for approval — run this again when you're at a browser")
+
+// loginExpiredMsg is the draft copy for a code that ran out its own life.
+const loginExpiredMsg = "that login code expired before it was approved; start again with carlos auth login --no-wait"
+
// terminalLoginError marks a poll outcome after which the login can never be
// approved (denied, expired, unknown), as opposed to a failure worth retrying.
type terminalLoginError struct{ error }
@@ -784,7 +794,7 @@ func pollLogin(ctx context.Context, c *consoleClient, base, deviceCode, label st
return fmt.Errorf("the console answered with an unknown status %q", poll.Status)
}
}
- return errors.New("gave up waiting for approval — run this again when you're at a browser")
+ return errLoginGaveUp
}
func authLogin(out io.Writer, args []string) error {
@@ -812,11 +822,23 @@ func authLogin(out io.Writer, args []string) error {
}
// The console comes from the file, not -console or the defaults: the
// code in it only means something there.
- deadline := loginDeadline(time.Now(), int(time.Until(p.ExpiresAt).Seconds()))
- if p.ExpiresAt.IsZero() {
- deadline = loginDeadline(time.Now(), 0)
+ now := time.Now()
+ deadline := now.Add(loginWaitCap)
+ if !p.ExpiresAt.IsZero() && p.ExpiresAt.Before(deadline) {
+ deadline = p.ExpiresAt
+ }
+ if !p.ExpiresAt.IsZero() && !now.Before(p.ExpiresAt) {
+ // Already past its life: nothing to poll for.
+ if cerr := clearPendingLoginIf(p.DeviceCode); cerr != nil {
+ warnf(out, "Could not remove %s: %v", pendingLoginPath(), cerr)
+ }
+ return errors.New(loginExpiredMsg)
}
err = pollLogin(ctx, newConsoleClient(p.Console, ""), p.Console, p.DeviceCode, p.Label, p.Interval, deadline, out)
+ if errors.Is(err, errLoginGaveUp) && !p.ExpiresAt.IsZero() && !time.Now().Before(p.ExpiresAt) {
+ // The wait ended at the code's own expiry, which ends the attempt.
+ err = terminalLoginError{errors.New(loginExpiredMsg)}
+ }
var term terminalLoginError
if err == nil || errors.As(err, &term) {
if cerr := clearPendingLoginIf(p.DeviceCode); cerr != nil {
diff --git a/authpending_test.go b/authpending_test.go
index 52ba69c77..d93f761ce 100644
--- a/authpending_test.go
+++ b/authpending_test.go
@@ -7,6 +7,7 @@ import (
"net/http/httptest"
"os"
"strings"
+ "sync/atomic"
"testing"
"time"
)
@@ -144,11 +145,15 @@ func TestAuthLoginWaitTerminalStatesClear(t *testing.T) {
}
t.Run("transient keeps", func(t *testing.T) {
useTempCredentials(t)
+ old := loginWaitCap
+ loginWaitCap = 1500 * time.Millisecond
+ defer func() { loginWaitCap = old }()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadGateway)
}))
defer ts.Close()
- if err := savePendingLogin(pendingLogin{Console: ts.URL, DeviceCode: "d1", ExpiresAt: time.Now().Add(1500 * time.Millisecond)}); err != nil {
+ // Expiry is far away: the wait is cut short by the cap, not by the code's life.
+ if err := savePendingLogin(pendingLogin{Console: ts.URL, DeviceCode: "d1", ExpiresAt: time.Now().Add(time.Minute)}); err != nil {
t.Fatal(err)
}
var out bytes.Buffer
@@ -159,6 +164,48 @@ func TestAuthLoginWaitTerminalStatesClear(t *testing.T) {
t.Fatal("a transient failure removed the pending file")
}
})
+ t.Run("own expiry clears", func(t *testing.T) {
+ useTempCredentials(t)
+ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusBadGateway)
+ }))
+ defer ts.Close()
+ if err := savePendingLogin(pendingLogin{Console: ts.URL, DeviceCode: "d1", ExpiresAt: time.Now().Add(1500 * time.Millisecond)}); err != nil {
+ t.Fatal(err)
+ }
+ var out bytes.Buffer
+ err := authLogin(&out, []string{"--wait"})
+ if err == nil || !strings.Contains(err.Error(), "expired") {
+ t.Fatalf("err = %v, want expired", err)
+ }
+ if p, _ := loadPendingLogin(); p != nil {
+ t.Fatalf("expiry left the pending file: %+v", p)
+ }
+ })
+ t.Run("already expired does not poll", func(t *testing.T) {
+ useTempCredentials(t)
+ var polls atomic.Int32
+ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ polls.Add(1)
+ json.NewEncoder(w).Encode(map[string]any{"status": "pending"})
+ }))
+ defer ts.Close()
+ if err := savePendingLogin(pendingLogin{Console: ts.URL, DeviceCode: "d1", ExpiresAt: time.Now().Add(-time.Second)}); err != nil {
+ t.Fatal(err)
+ }
+ start := time.Now()
+ var out bytes.Buffer
+ err := authLogin(&out, []string{"--wait"})
+ if err == nil || !strings.Contains(err.Error(), "expired") {
+ t.Fatalf("err = %v, want expired", err)
+ }
+ if polls.Load() != 0 || time.Since(start) > 500*time.Millisecond {
+ t.Fatalf("polls = %d after %v, want none, prompt", polls.Load(), time.Since(start))
+ }
+ if p, _ := loadPendingLogin(); p != nil {
+ t.Fatal("pending file survived")
+ }
+ })
}
func TestAgentLabel(t *testing.T) {