diff --git a/internal/agent/agent.go b/internal/agent/agent.go index 286465196..671149d88 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -305,6 +305,11 @@ func (a *Agent) Detected() bool { if a.Bin != "" && binAt(a.Bin) != "" { return true } + // one with no Bin, as its command's name is other tools' too, that + // Homebrew installed (Grok Build's grok-build cask) and hasn't run yet + if a.Bin == "" && a.WSL == "" && brewBin(cliSpecs[a.ID]) != "" { + return true + } return false } diff --git a/internal/agent/cliupdate.go b/internal/agent/cliupdate.go index 0d26dc858..de7c7d30c 100644 --- a/internal/agent/cliupdate.go +++ b/internal/agent/cliupdate.go @@ -16,6 +16,7 @@ import ( "io" "net/http" "os" + "path" "path/filepath" "regexp" "runtime" @@ -129,8 +130,10 @@ var cliSpecs = map[string]cliSpec{ // ../downloads/grok-, and grok update to a new // ../downloads/grok--; install.ps1 copies grok.exe // into ~\.grok\bin itself, as grok update does again. grok is also - // Homebrew's regular-expression tool, hence at. - "grok": {at: ".grok/bin/grok", latest: grokLatest, + // Homebrew's regular-expression tool, hence at; its own cask, grok-build, + // links $HOMEBREW_PREFIX/bin/grok to Caskroom/grok-build//grok-- + // (Will on Discord), which brew upgrade replaces (see cliBin). + "grok": {at: ".grok/bin/grok", latest: grokLatest, brew: []string{"grok-build"}, self: func(bin, real string) []string { if l := strings.ToLower(real); strings.Contains(l, "/.grok/bin/") || strings.Contains(l, "/.grok/downloads/") { return []string{bin, "update"} @@ -244,6 +247,31 @@ func fromBrew(real string, m []string) *updater { return u } +// brewBin is the CLI of an agent with no Bin (spec.at's name) where PATH +// has it, when Homebrew's formula or cask of the agent's put it there: a +// command of that name that isn't one of the agent's is another tool's +// (Homebrew's grok formula). "" otherwise. +func brewBin(spec cliSpec) string { + if spec.at == "" || len(spec.brew) == 0 { + return "" + } + bin := binAt(path.Base(spec.at)) + if bin == "" { + return "" + } + real, err := proc.RealPath(bin) + if err != nil { + return "" + } + if m := brewPath.FindStringSubmatch(filepath.ToSlash(real)); m == nil || !contains(spec.brew, m[2]) { + return "" + } + if abs, err := filepath.Abs(bin); err == nil { + bin = abs + } + return bin +} + // fromNodeModules is the updater of a CLI the package pkg installed in a // global node_modules: the binary leads there (npm's and bun's links), or // is a shim that names it (npm's .cmd on Windows, pnpm's scripts). @@ -656,6 +684,11 @@ func (a *Agent) cliBin() (string, cliSpec) { return "", spec } if a.Bin == "" { + // Homebrew's, where PATH has it: what a terminal runs, before a + // vendor install a brew one replaced + if bin := brewBin(spec); bin != "" { + return bin, spec + } home, err := os.UserHomeDir() if spec.at == "" || err != nil { return "", spec diff --git a/internal/agent/cliupdate_test.go b/internal/agent/cliupdate_test.go index 9a42e3e51..54d2a91d1 100644 --- a/internal/agent/cliupdate_test.go +++ b/internal/agent/cliupdate_test.go @@ -236,6 +236,12 @@ func TestHowInstalled(t *testing.T) { if u := howInstalled(cliSpecs["copilot"], link(t, "../lib/node_modules/@github/copilot/npm-loader.js", filepath.Join(nodeKeg, "bin/copilot"))); u == nil || u.via != "npm" { t.Errorf("npm under node@20's keg: %+v", u) } + // Grok Build's cask: bin/grok → Caskroom/grok-build/1.0.52/grok-1.0.52-macos-aarch64 + file(t, filepath.Join(brewDir, "Caskroom/grok-build/1.0.52/grok-1.0.52-macos-aarch64"), "") + grokBin := link(t, "../Caskroom/grok-build/1.0.52/grok-1.0.52-macos-aarch64", filepath.Join(brewDir, "bin/grok")) + if u := howInstalled(cliSpecs["grok"], grokBin); u == nil || !u.cask || !reflect.DeepEqual(u.cmd, []string{brew, "upgrade", "--cask", "grok-build"}) { + t.Errorf("Grok Build's cask: %+v", u) + } os.Remove(filepath.Join(brewDir, "bin/gemini")) file(t, filepath.Join(brewDir, "Cellar/something/1.0/bin/gemini"), "") if u := howInstalled(cliSpecs["gemini"], link(t, "../Cellar/something/1.0/bin/gemini", filepath.Join(brewDir, "bin/gemini"))); u != nil { @@ -501,3 +507,66 @@ func TestGrokInstalled(t *testing.T) { t.Errorf("alpha: %+v", c) } } + +// Grok Build from Homebrew's grok-build cask (Will on Discord): grok on PATH +// leads into Caskroom/grok-build, with no ~/.grok/bin/grok. It is found, +// detected before grok first runs, and updated with brew; Homebrew's grok +// formula, a regular-expression tool, never is. +func TestGrokBrewCask(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("Homebrew's links") + } + home, _ := filepath.EvalSymlinks(t.TempDir()) + t.Setenv("HOME", home) + t.Setenv("GROK_HOME", "") + brewDir := filepath.Join(home, "homebrew") + t.Setenv("PATH", filepath.Join(brewDir, "bin")) + file(t, filepath.Join(brewDir, "bin/brew"), "#!/bin/sh\n") + + oldVer, oldLatest := runVersion, brewLatest + says := "grok 1.0.46 (2765805b9442)" + runVersion = func(string) string { return says } + brewLatest = func(u *updater) (string, error) { return "1.0.52", nil } + versions, latests = memo{}, memo{} + t.Cleanup(func() { + runVersion, brewLatest = oldVer, oldLatest + versions, latests = memo{}, memo{} + }) + + // the regular-expression tool's grok, the formula of that name + file(t, filepath.Join(brewDir, "Cellar/grok/0.9.2/bin/grok"), "") + link(t, "../Cellar/grok/0.9.2/bin/grok", filepath.Join(brewDir, "bin/grok")) + a := grok(home) + if a.Detected() { + t.Error("Homebrew's grok formula detected as Grok Build") + } + if c, ok := a.CLI(); ok { + t.Errorf("Homebrew's grok formula taken for Grok Build: %+v", c) + } + + // the cask, as formulae.brew.sh/api/cask/grok-build.json has it: binary + // grok-1.0.46-macos-aarch64, target grok + os.Remove(filepath.Join(brewDir, "bin/grok")) + file(t, filepath.Join(brewDir, "Caskroom/grok-build/1.0.46/grok-1.0.46-macos-aarch64"), "") + bin := link(t, "../Caskroom/grok-build/1.0.46/grok-1.0.46-macos-aarch64", filepath.Join(brewDir, "bin/grok")) + if !a.Detected() { + t.Error("the grok-build cask, before grok has run, not detected") + } + c, ok := a.CLI() + if !ok || c.Path != bin || c.Version != "1.0.46" || c.Via != "brew" || c.Command != "brew upgrade --cask grok-build" || c.Latest != "1.0.52" || !c.Update { + t.Fatalf("the grok-build cask: %+v %v", c, ok) + } + + // with an old ~/.grok/bin/grok from x.ai's installer beside it, the + // cask PATH runs is the one shown and updated + file(t, filepath.Join(home, ".grok/downloads/grok-macos-aarch64"), "") + link(t, "../downloads/grok-macos-aarch64", filepath.Join(home, ".grok/bin/grok")) + if c, _ := a.CLI(); c.Path != bin || c.Via != "brew" { + t.Errorf("cask and installer: %+v", c) + } + // PATH running the installer's instead: that one, updated by grok update + os.Remove(bin) + if c, _ := a.CLI(); c.Path != filepath.Join(home, ".grok/bin/grok") || c.Via != "self" { + t.Errorf("installer only: %+v", c) + } +}