diff --git a/internal/agent/agy.go b/internal/agent/agy.go index ccaba10f7..2af5d07cd 100644 --- a/internal/agent/agy.go +++ b/internal/agent/agy.go @@ -115,10 +115,17 @@ func agyFunction(kind, prog string) string { }` } q := "'" + strings.ReplaceAll(prog, "'", `'\''`) + "'" - // "function": a name the user aliased isn't expanded where it is defined - return "function agy {\n if [ -x " + q + " ]; then " + q + ` agy run "$@"; else command agy "$@"; fi` + "\n}" + return agyPosixHead + " if [ -x " + q + " ]; then " + q + ` agy run "$@"; else command agy "$@"; fi` + "\n}" } +// agyPosixHead opens the function agy in a POSIX shell's profile. It is +// "agy() {", not bash's "function agy {", which dash refuses: a ~/.profile +// that reads ~/.bashrc is read by sh -lc too, as Debian's dash (ljlwlcz on +// X: fnOS, where an SSH probe's sh -lc failed on it). An alias named agy +// would be expanded in "agy()", so it is taken out first, on a line of its +// own, since a shell expands aliases a line at a time. +const agyPosixHead = "unalias agy 2>/dev/null || true\nagy() {\n" + // agyDistroProfiles are the profiles of a WSL distro's shells the // function goes in, as magpie opens them: bash's ~/.bashrc (which the // ~/.profile of a login bash reads on Debian and Ubuntu), zsh's ~/.zshrc @@ -179,7 +186,7 @@ func agyDistroFunction(kind, key, gw string) string { " if test -n \"$m\"\n set argv --model $m $argv\n end\n" + " env GEMINI_API_KEY=" + fq(key) + " GOOGLE_GEMINI_BASE_URL=" + fq(gw) + " agy $argv\nend" } - return "function agy {\n" + + return agyPosixHead + " local m a\n" + " m=$(sed -n " + q(agyDistroModel) + " " + settings + " 2>/dev/null | head -n 1)\n" + ` if [ -z "$m" ]; then command agy "$@"; return; fi` + "\n" + diff --git a/internal/agent/agy_posix_test.go b/internal/agent/agy_posix_test.go new file mode 100644 index 000000000..f5d6a91a0 --- /dev/null +++ b/internal/agent/agy_posix_test.go @@ -0,0 +1,68 @@ +//go:build !windows + +package agent + +import ( + "os" + "os/exec" + "path/filepath" + "strings" + "testing" +) + +// The function agy magpie puts in a POSIX profile is read by every shell +// that reads the file: dash too, as sh -lc when ~/.profile reads ~/.bashrc +// (ljlwlcz on X: fnOS, where bash's "function agy {" failed an SSH probe's +// sh -lc). Each shell here reads the block after an alias named agy, as a +// profile does, and then agy starts the agy on PATH. +func TestAgyFunctionInEveryPOSIXShell(t *testing.T) { + var shells []string + for _, sh := range []string{"dash", "sh", "bash", "zsh", "mksh", "busybox"} { + if p, err := exec.LookPath(sh); err == nil { + shells = append(shells, p) + } + } + if len(shells) == 0 { + t.Skip("no POSIX shell here") + } + dir := t.TempDir() + bin := filepath.Join(dir, "bin") + home := filepath.Join(dir, "home") + settings := filepath.Join(home, ".gemini", "antigravity-cli", "settings.json") + if err := os.MkdirAll(bin, 0o755); err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(filepath.Dir(settings), 0o755); err != nil { + t.Fatal(err) + } + stub := "#!/bin/sh\necho \"agy key=$GEMINI_API_KEY gw=$GOOGLE_GEMINI_BASE_URL args=$*\"\n" + if err := os.WriteFile(filepath.Join(bin, "agy"), []byte(stub), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(settings, []byte("{\n \"model\": \"magpie/relay/glm-4.6\"\n}\n"), 0o644); err != nil { + t.Fatal(err) + } + gone := filepath.Join(dir, "no such magpie") + for _, c := range []struct{ name, fn, want string }{ + {"here", agyFunction("posix", gone), "agy key= gw= args=a b"}, + {"distro", agyDistroFunction("posix", "k'1", "http://172.20.0.1:3425"), "agy key=k'1 gw=http://172.20.0.1:3425 args=--model magpie/relay/glm-4.6 a b"}, + } { + profile := filepath.Join(dir, c.name+".profile") + body := "alias agy='echo ALIAS' 2>/dev/null\n# >>> magpie agy >>>\n" + c.fn + "\n# <<< magpie agy <<<\n" + if err := os.WriteFile(profile, []byte(body), 0o644); err != nil { + t.Fatal(err) + } + for _, sh := range shells { + args := []string{"-c", `. "$1" && agy a b`, "sh", profile} + if filepath.Base(sh) == "busybox" { + args = append([]string{"sh"}, args...) + } + cmd := exec.Command(sh, args...) + cmd.Env = []string{"HOME=" + home, "PATH=" + bin + ":/usr/bin:/bin"} + out, err := cmd.CombinedOutput() + if got := strings.TrimSpace(string(out)); err != nil || got != c.want { + t.Errorf("%s in %s: %v\n%s\nwant %s\nprofile:\n%s", c.name, sh, err, got, c.want, body) + } + } + } +} diff --git a/internal/agent/agy_shell_test.go b/internal/agent/agy_shell_test.go index 8d5520bee..9c06186d7 100644 --- a/internal/agent/agy_shell_test.go +++ b/internal/agent/agy_shell_test.go @@ -48,7 +48,7 @@ func TestAgyShell(t *testing.T) { } for _, p := range in { b, _ := os.ReadFile(p) - if s := string(b); !strings.HasPrefix(s, mine+"\n# >>> magpie agy >>>\nfunction agy {\n") || !strings.Contains(s, ` agy run "$@"; else command agy "$@"; fi`) || !strings.HasSuffix(s, "}\n# <<< magpie agy <<<\n") { + if s := string(b); !strings.HasPrefix(s, mine+"\n# >>> magpie agy >>>\n"+agyPosixHead) || !strings.Contains(s, ` agy run "$@"; else command agy "$@"; fi`) || !strings.HasSuffix(s, "}\n# <<< magpie agy <<<\n") { t.Fatalf("%s:\n%s", p, s) } } diff --git a/internal/agent/wsl_agy_test.go b/internal/agent/wsl_agy_test.go index 05d5e1cee..dc923f7a3 100644 --- a/internal/agent/wsl_agy_test.go +++ b/internal/agent/wsl_agy_test.go @@ -95,7 +95,7 @@ func TestWSLAgyPick(t *testing.T) { } for _, p := range []string{bashrc, zshrc} { body := readFile(p) - if !strings.Contains(body, "# >>> magpie agy >>>\nfunction agy {\n") || !strings.Contains(body, "GOOGLE_GEMINI_BASE_URL='"+gw+"'") || + if !strings.Contains(body, "# >>> magpie agy >>>\n"+agyPosixHead) || !strings.Contains(body, "GOOGLE_GEMINI_BASE_URL='"+gw+"'") || !strings.Contains(body, "GEMINI_API_KEY='"+key+"'") || strings.Contains(body, "127.0.0.1") { t.Fatalf("%s:\n%s", p, body) } @@ -247,7 +247,7 @@ func TestWSLAgyStopped(t *testing.T) { if strings.Join(*asked, ",") != "Stopped" { t.Fatalf("not started first: %v", *asked) } - if body := readFile(filepath.Join(root, "home", "me", ".bashrc")); !strings.Contains(body, "function agy {") { + if body := readFile(filepath.Join(root, "home", "me", ".bashrc")); !strings.Contains(body, agyPosixHead) { t.Fatalf(".bashrc:\n%s", body) } }