agent: the function agy in a POSIX profile is agy(), which dash reads
magpie put bash's `function agy {` in ~/.bashrc and ~/.zshrc (and a WSL
distro's). On fnOS ~/.profile reads ~/.bashrc, so sh -lc, which is dash
there, read it too and stopped at a syntax error; Codync's SSH probe
failed (ljlwlcz on X, v0.1.3336). Both POSIX bodies, agyFunction's and
agyDistroFunction's, now open with `agy() {`. The keyword was there so an
alias named agy isn't expanded where the function is defined; that guard
is now an `unalias agy 2>/dev/null || true` on the line before, since a
shell expands aliases a line at a time. Blocks already in a profile are
rewritten between their markers the next time magpie writes them.
Run (mote, macOS; wsl-run, Ubuntu 24.04 where /bin/sh is dash):
- TestAgyFunctionInEveryPOSIXShell (new): each POSIX body read after an
alias agy by dash, sh, bash, zsh (mksh/busybox when present), then agy
run: here falls through to the agy on PATH, the distro one passes the
key, gateway and --model. Green on mote and in WSL.
- Broken by hand: with the old `function agy {` head it is red in dash
(exit 2, both bodies); with `agy() {` and no unalias it is red in dash,
sh (bash posix) and zsh.
- go test -timeout 45m ./internal/agent ./internal/update ./internal/cli:
ok on mote. gofmt -l clean; go vet for darwin, linux, windows ok.
Not run: the full suite, a real fnOS box, Codync itself.
This commit is contained in:
+10
-3
@@ -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" +
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user