agent: Grok Build from Homebrew's grok-build cask is detected, shows its version and updates with brew (#1923, Will)

Will (waitingsnow on Discord) installed Grok Build with
`brew install --cask grok-build`. The cask (formulae.brew.sh/api/cask/
grok-build.json, 1.0.52) links $HOMEBREW_PREFIX/bin/grok to
Caskroom/grok-build/<v>/grok-<v>-macos-<arch> and leaves ~/.grok/bin
empty. magpie only looked at ~/.grok/bin/grok (x.ai's installer), since
`grok` on PATH may be Homebrew's regular-expression formula, so the CLI row
had no version or update, and before grok's first run Grok Build wasn't
detected at all.

- cliSpecs: grok gets brew grok-build, so the cask is updated with
  `brew upgrade --cask grok-build`.
- brewBin: for an agent with no Bin (its command name is other tools'),
  the command on PATH counts only when it leads into one of the agent's
  own formulae or casks. cliBin prefers it (it is what a terminal runs)
  over ~/.grok/bin/grok; Agent.Detected uses it too.
- Grok is the only cliSpec with `at`; the brew siblings (gemini-cli, pi,
  cline formulae) are the previous commit.

Run: go test -tags nogui ./internal/agent -run 'TestHowInstalled|TestGrok'
(ok, new TestGrokBrewCask); full ./internal/agent passed with -timeout 40m
(650s on a loaded Mac). gofmt -l clean; go vet -tags nogui for darwin,
linux and windows. Broke each piece by hand and watched red: no grok-build
in the spec (cask not found/updated), cliBin skipping brewBin, Detected
skipping it, and brewBin taking any brew grok (the regex formula then shows
as Grok Build). Not tried with a real cask install: nothing installed on
the live machine; the fixture follows the cask JSON's binary/target.
This commit is contained in:
yetone
2026-10-13 13:06:14 +08:00
parent 2d06210f93
commit 7a3029db9c
3 changed files with 109 additions and 2 deletions
+5
View File
@@ -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
}
+35 -2
View File
@@ -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-<platform>, and grok update to a new
// ../downloads/grok-<version>-<platform>; 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/<v>/grok-<v>-<platform>
// (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
+69
View File
@@ -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)
}
}