diff --git a/internal/provider/account_test.go b/internal/provider/account_test.go index 0f093778b..fb215fd17 100644 --- a/internal/provider/account_test.go +++ b/internal/provider/account_test.go @@ -76,6 +76,10 @@ func isolate(t testing.TB) { oldDevin := DevinExecutable claudeKeychain = false claudeExecutable = func() string { return "" } + // a Claude sign-in with no Claude Code downloads one: never in a test + oldDownload := downloadClaudeCode + downloadClaudeCode = func(context.Context) error { return errors.New("no Claude Code download in tests") } + t.Cleanup(func() { downloadClaudeCode = oldDownload }) // the machine's own devin, if it has one, is no test's DevinExecutable = func() string { return "" } forgetClaudeCredential() diff --git a/internal/provider/install.go b/internal/provider/install.go index ed0a351f5..4a0860654 100644 --- a/internal/provider/install.go +++ b/internal/provider/install.go @@ -1,7 +1,7 @@ package provider -// Some subscriptions are used through the vendor's own CLI — Cursor's, -// Grok's, Devin's — and signed in with it too. One that isn't installed is +// Some subscriptions are used through the vendor's own CLI — Claude +// Code, Cursor's, Grok's, Devin's — and signed in with it too. One that isn't installed is // installed with the vendor's own installer when its sign-in starts, as the // vendor's page tells a person to, rather than leaving the sign-in to fail // or, as Devin's did, to finish with nothing to show for it. @@ -15,6 +15,7 @@ import ( "strings" "time" + "github.com/yetone/magpie/internal/claudecode" "github.com/yetone/magpie/internal/netproxy" "github.com/yetone/magpie/internal/proc" ) @@ -24,18 +25,34 @@ import ( // when there is one, does what the shell one does without a shell, for a // magpie with none (its Docker image had no bash, curl or wget until #2834). setup: the // installer ends by running the CLI's own setup, which asks how to sign in; -// magpie signs in itself once the CLI is there. +// magpie signs in itself once the CLI is there. manual, when set, is how +// a person installs it, told when the install fails, where a CLI with no +// one-liner (Claude Code's, which magpie downloads itself) has native only. type agentCLI struct { Name string find func() string sh string ps string native func(ctx context.Context) error + manual string setup bool } func cliFor(agent string) (agentCLI, bool) { switch agent { + case "claude": + // Kabubu01 (X): the GUI's Claude sign-in failed with "install + // Claude Code first … or run `magpie claude-code install`". The + // sign-in now downloads it as that command does: Anthropic's own + // build, checked against its manifest, kept in magpie's cache and + // not put on PATH + return agentCLI{Name: "Claude Code", find: func() string { + c, ok := findClaudeCLI() + if ok && c.wsl != nil { + return "wsl:" + c.wsl.Distro + } + return c.path + }, native: downloadClaudeCode, manual: "magpie claude-code install"}, true case "devin": return agentCLI{Name: "Devin CLI", find: func() string { return DevinExecutable() }, sh: "curl -fsSL https://cli.devin.ai/install.sh | bash", @@ -68,7 +85,7 @@ var installTimeout = 10 * time.Minute // runInstaller runs a vendor's installer; a var so tests can fake it. var runInstaller = func(ctx context.Context, c agentCLI) ([]byte, error) { - if runtime.GOOS != "windows" && c.native != nil && !shellInstallerRuns() { + if c.native != nil && (c.sh == "" && c.ps == "" || runtime.GOOS != "windows" && !shellInstallerRuns()) { return nil, c.native(ctx) } var cmd *exec.Cmd @@ -144,6 +161,18 @@ func runUntilInstalled(ctx context.Context, cmd *exec.Cmd, c agentCLI) ([]byte, return out.Bytes(), err } +// downloadClaudeCode downloads the newest Claude Code into magpie's cache +// (claudecode.Install), where claudecode.Find, and so the sign-in, finds +// it; a var so tests never reach the network. +var downloadClaudeCode = func(ctx context.Context) error { + r, err := claudecode.Latest(ctx) + if err != nil { + return err + } + _, err = claudecode.Install(ctx, r, nil) + return err +} + // shellInstallerRuns is whether a vendor's `curl … | bash` can run here: // bash, and curl, which every one-liner (and the scripts behind them) // downloads with. magpie's Docker image had none of them, and the install @@ -174,6 +203,9 @@ func installCLI(ctx context.Context, c agentCLI) error { if runtime.GOOS == "windows" { manual = c.ps } + if c.manual != "" { + manual = c.manual + } if ctx.Err() == context.Canceled { return ctx.Err() } diff --git a/internal/provider/signin_claude_download_test.go b/internal/provider/signin_claude_download_test.go new file mode 100644 index 000000000..e30ee3a70 --- /dev/null +++ b/internal/provider/signin_claude_download_test.go @@ -0,0 +1,63 @@ +package provider + +import ( + "context" + "errors" + "strings" + "sync/atomic" + "testing" +) + +// Kabubu01 (X): with no Claude Code on the computer, the GUI's Claude +// sign-in failed with "install Claude Code first … or run `magpie +// claude-code install`". The sign-in downloads it now, as Devin's, +// Cursor's and Grok's install their CLIs, and goes on to the sign-in page. +func TestClaudeSignInDownloadsClaudeCode(t *testing.T) { + claudeHome(t) + noAnthropic(t) + exe := fakeClaudeLogin(t, fakeClaudeAccount{email: "dl@example.com", plan: "max", refresh: "sk-ant-ort01-dl"}, "", false) + var downloaded atomic.Bool + claudeExecutable = func() string { + if downloaded.Load() { + return exe + } + return "" + } + downloadClaudeCode = func(context.Context) error { + downloaded.Store(true) + return nil + } + + st, err := StartSignIn("claude") + if err != nil { + t.Fatal(err) + } + if st.State != "installing" || st.Installing != "Claude Code" { + t.Fatalf("with no Claude Code: %+v", st) + } + st = waitPast(t, st.ID, "installing") + if !downloaded.Load() || !st.PasteCode { + t.Fatalf("after the download: %+v", st) + } + if err := SubmitSignInCallback(st.ID, "the-code#st"); err != nil { + t.Fatal(err) + } + if st = waitDone(t, st.ID); st.State != "done" || st.User != "dl@example.com" { + t.Fatalf("state %+v", st) + } +} + +// a download that fails says so, and the command that does it by hand +func TestClaudeSignInDownloadFails(t *testing.T) { + claudeHome(t) + downloadClaudeCode = func(context.Context) error { return errors.New("downloads.claude.ai: 403 Forbidden") } + + st, err := StartSignIn("claude") + if err != nil { + t.Fatal(err) + } + st = waitPast(t, st.ID, "installing") + if st.State != "failed" || !strings.Contains(st.Error, "403 Forbidden") || !strings.Contains(st.Error, "magpie claude-code install") { + t.Fatalf("state %+v", st) + } +}