From bdb1dfdd1b2c0a064d0e1d489d1f75ea51356d1a Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 29 Sep 2026 17:41:26 +0800 Subject: [PATCH] =?UTF-8?q?providers:=20a=20Command=20Code=20sign-in=20goe?= =?UTF-8?q?s=20through=20when=20whoami=20errs=20on=20the=20new=20key=20(ti?= =?UTF-8?q?gger=5Fultra=20on=20X:=20Sign-in=20didn't=20finish=20=C2=B7=20C?= =?UTF-8?q?ommand=20Code=20didn't=20take=20the=20new=20key:=20Internal=20S?= =?UTF-8?q?erver=20Error)=20=E2=80=94=20magpie=20asked=20/alpha/whoami=20t?= =?UTF-8?q?o=20name=20the=20account=20of=20the=20key=20Studio=20had=20just?= =?UTF-8?q?=20posted,=20and=20failed=20the=20sign-in=20on=20any=20answer?= =?UTF-8?q?=20but=20a=20200;=20whoami=20answered=20that=20key=20with=20a?= =?UTF-8?q?=20500.=20The=20CLI's=20own=20browser=20sign-in=20never=20asks?= =?UTF-8?q?=20whoami=20(it=20keeps=20the=20key=20and=20the=20name=20Studio?= =?UTF-8?q?=20posts;=20whoami=20is=20its=20check=20on=20a=20key=20typed=20?= =?UTF-8?q?in,=20and=20only=20a=20401=20there=20means=20a=20bad=20key).=20?= =?UTF-8?q?Now=20only=20whoami=20turning=20the=20key=20down=20(401,=20403)?= =?UTF-8?q?=20fails=20it;=20any=20other=20error=20is=20asked=20again=20twi?= =?UTF-8?q?ce,=20a=20second=20apart,=20and=20then=20passed=20over=20for=20?= =?UTF-8?q?the=20name=20Studio=20sent,=20the=20plan=20still=20read=20from?= =?UTF-8?q?=20billing/subscriptions.=20With=20no=20name=20from=20Studio=20?= =?UTF-8?q?either,=20it=20fails=20saying=20so.=20Tested:=20TestCommandCode?= =?UTF-8?q?SignInWhoamiDown,=20a=20fake=20Command=20Code=20whose=20whoami?= =?UTF-8?q?=20answers=20500:=20signed=20in=20as=20the=20posted=20name=20wi?= =?UTF-8?q?th=20its=20plan=20after=20three=20asks,=20a=20401=20key=20refus?= =?UTF-8?q?ed,=20no=20name=20at=20all=20refused;=20go=20test=20-tags=20nog?= =?UTF-8?q?ui=20./...=20(TestClaudeSignedOut=20failed=20once=20under=20loa?= =?UTF-8?q?d,=20passes=20on=20its=20own=20with=20and=20without=20the=20cha?= =?UTF-8?q?nge=20and=20in=20a=20rerun),=20GOOS=3Dlinux=20build?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/provider/commandcode_plan.go | 33 +++++++++++-- internal/provider/commandcode_plan_test.go | 54 ++++++++++++++++++++++ 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/internal/provider/commandcode_plan.go b/internal/provider/commandcode_plan.go index 15f6607a5..f0ee8da3d 100644 --- a/internal/provider/commandcode_plan.go +++ b/internal/provider/commandcode_plan.go @@ -19,6 +19,7 @@ package provider import ( "context" "encoding/json" + "errors" "fmt" "io" "net" @@ -416,6 +417,9 @@ func startCommandCodeSignIn(s *signInFlow) error { return nil } +// cmdWhoamiWait is how long a sign-in waits to ask whoami again. +var cmdWhoamiWait = time.Second + // cmdOrigins are the Studio pages that may post to the callback. var cmdOrigins = []string{"https://commandcode.ai", "https://staging.commandcode.ai"} @@ -540,8 +544,12 @@ func (s *signInFlow) commandCodeCallback(w http.ResponseWriter, r *http.Request) answer(true, "") } -// cmdSignedIn checks a new key and names its account, as the CLI does -// with whoami, and reads its plan. +// cmdSignedIn names a new key's account with whoami, and reads its plan. +// Only whoami turning the key down (401, 403) fails the sign-in: the CLI's +// own browser sign-in keeps the key Studio posts without asking whoami at +// all, and whoami has answered a key just made with a 500, so a whoami +// that errs is asked again a few times and then passed over for the name +// Studio sent with the key. func cmdSignedIn(ctx context.Context, a cmdAuth) (who, plan string, err error) { var me struct { User struct { @@ -551,8 +559,25 @@ func cmdSignedIn(ctx context.Context, a cmdAuth) (who, plan string, err error) { Email string `json:"email"` } `json:"user"` } - if err := accountJSON(ctx, cmdAPI+"/alpha/whoami", a.APIKey, nil, &me); err != nil { - return "", "", fmt.Errorf("Command Code didn't take the new key: %w", err) + for try := 0; ; try++ { + err := accountJSON(ctx, cmdAPI+"/alpha/whoami", a.APIKey, nil, &me) + var st *accountStatusError + if err == nil { + break + } + if errors.As(err, &st) && (st.status == http.StatusUnauthorized || st.status == http.StatusForbidden) { + return "", "", fmt.Errorf("Command Code didn't take the new key: %w", err) + } + if try == 2 || ctx.Err() != nil { + if firstNonEmpty(a.UserName, a.UserID) == "" { + return "", "", fmt.Errorf("Command Code couldn't say which account signed in: %w", err) + } + break + } + select { + case <-ctx.Done(): + case <-time.After(cmdWhoamiWait): + } } who = firstNonEmpty(me.User.UserName, a.UserName, me.User.Email, me.User.ID, a.UserID) if who == "" { diff --git a/internal/provider/commandcode_plan_test.go b/internal/provider/commandcode_plan_test.go index 52ac1c905..89cc5acac 100644 --- a/internal/provider/commandcode_plan_test.go +++ b/internal/provider/commandcode_plan_test.go @@ -9,6 +9,7 @@ import ( "os" "path/filepath" "strings" + "sync/atomic" "testing" "time" ) @@ -197,3 +198,56 @@ func TestCommandCodePlanName(t *testing.T) { t.Fatalf("ms: %v", tm) } } + +// A whoami that errs on a key just made doesn't fail the sign-in (tigger_ultra: +// "Command Code didn't take the new key: Internal Server Error"): it is asked +// again, then passed over for the name Studio sent; only a key it turns down +// fails. +func TestCommandCodeSignInWhoamiDown(t *testing.T) { + signIn(t) + var asked atomic.Int32 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + key := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ") + switch { + case key == "bad-key": + w.WriteHeader(401) + case r.URL.Path == "/alpha/whoami": + asked.Add(1) + w.WriteHeader(500) + case r.URL.Path == "/alpha/billing/subscriptions": + _ = json.NewEncoder(w).Encode(map[string]any{"success": true, "data": map[string]any{"planId": "individual-pro-monthly", "status": "active"}}) + default: + w.WriteHeader(404) + } + })) + defer srv.Close() + oldAPI, oldWait := cmdAPI, cmdWhoamiWait + cmdAPI, cmdWhoamiWait = srv.URL, 10*time.Millisecond + defer func() { cmdAPI, cmdWhoamiWait = oldAPI, oldWait }() + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + noFollow := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} + + for _, c := range []struct{ key, user, state, errText string }{ + {"new-key", "tigger", "done", ""}, + {"bad-key", "evil", "failed", "Command Code didn't take the new key: Unauthorized"}, + {"new-key", "", "failed", "Command Code couldn't say which account signed in: Internal Server Error"}, + } { + asked.Store(0) + st, err := StartSignIn(CommandCodePlanID) + if err != nil { + t.Fatal(err) + } + u, _ := url.Parse(st.URL) + if _, err := noFollow.PostForm(u.Query().Get("callback"), url.Values{"apiKey": {c.key}, "state": {u.Query().Get("state")}, "userName": {c.user}}); err != nil { + t.Fatal(err) + } + st, _ = WaitSignIn(ctx, st.ID) + if st.State != c.state || st.Error != c.errText { + t.Fatalf("%s %q: %+v", c.key, c.user, st) + } + if c.state == "done" && (st.User != c.user || st.Plan != "Pro" || asked.Load() != 3) { + t.Fatalf("signed in: %+v, whoami asked %d times", st, asked.Load()) + } + } +}