coolmantou on Discord asked to start an agent from magpie's Agents list.
In the Mac app's window, an agent row's menu (openRowMenu, the one the
grip and a right-click open) now has "Open in terminal". This includes a
row under Not set up. Clicking it posts POST /api/agents/open with only
{agent: id}. The server finds that exact id in agent.Detected() (not
agent.Find's prefix match) and builds the line itself: the agent's
Launch() where it has one (agy, Claude Science, Cursor local), otherwise
its program's path, single-quoted (BinPath, new in internal/agent).
openTerminal then opens that line with the user's Session terminal
choice. The command never comes from the page. Other systems, the web
build, WSL agents and desktop apps with no program are refused, and
their rows have no item (agentJSON.terminal). The tray panel's rows don't
have the item either. The label is "Open in terminal", not "Terminal",
because the user's terminal can be iTerm or Ghostty. The string is in
en, zh, zh-TW, ja and de. Windows and Linux are #4474.
The core budget had 39 bytes left on main, so no budget was raised.
Instead:
- the item says nothing on success; the terminal opening is the answer,
and a failure goes to statusErr;
- the omp roles' comment above OMP_UNSET was cut to what OMP_UNSET's
values don't already say.
app.js is now 22 bytes under main's size.
Run:
- go test ./internal/gui ./internal/agent on mote (macOS) and in
magpie-desktop (Linux, -tags nogui), both ok.
- TestAgentOpenRunsOnlyTheAgent puts claude in a folder named
"it's a' $(touch made1) `touch made2`; touch made3 #". It posts the
ids "claude; touch made4", "claude'$(touch made5)'" and "cl"; none
of them opens anything. It then runs the line made for claude
through /bin/sh and checks that only the program ran.
- After the rebase onto origin/main 2604d68db, the core budget
first went red (1,536,904 > 1,536,586). It passed again after the
trims above, along with TestAgentOpen* and TestAgentCommand*.
- gofmt -l clean. go vet on darwin, GOOS=linux -tags nogui and
GOOS=windows for internal/gui and internal/agent: ok.
- GUI, both engines (wsl-run): agent-open-terminal.test.cjs is new,
14/14 at the pushed tree. It covers every language, a row under Not set up, no item for
the desktop app, the web build or the tray panel, and checks that the
post body is the id only. Its goto has 30s, since a goto while
another suite ran on the box took 5s once. i18n-keys and agent-disconnect pass.
- Real run on mote in a sandbox HOME: the endpoint was called through
the real openTerminal with an `open` on PATH that records instead of
opening. It returned 204, and the open args were -a
/System/Applications/Utilities/Terminal.app <file>. The file was
-rwx------ and read: #!/bin/sh / '<sandbox>/.local/bin/claude' /
exec "${SHELL:-/bin/zsh}" -l.
Broken by hand:
- agentCommand returning the path unquoted: the Go test went red with
"ran more than the agent: [made1 made2 made3]".
- The term item dropped from openRowMenu: the 10 row tests went red.
- The panel and web gates dropped: the 4 gate tests went red.
Not tried:
- No terminal app actually opened. The scratch run recorded what
`open` would get instead.
- iTerm, Ghostty and the other Session terminal choices were not tried.
- Launch() agents (agy, Claude Science, Cursor local) were not opened
for real; their lines were already quoted and used by
"Copy launch command".
- The full Go suite and the whole GUI suite were started before the
rebase and not waited on (ship at once).
122 lines
3.8 KiB
Go
122 lines
3.8 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/agent"
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// Open in terminal on an Agents row (coolmantou on Discord): the page
|
|
// sends only the agent's id, and the line Terminal runs is made here from
|
|
// the agent's own program. A path that spells shell commands runs that
|
|
// program and nothing else, and an id that isn't an agent's opens nothing.
|
|
func TestAgentOpenRunsOnlyTheAgent(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("the .command file is a POSIX shell's")
|
|
}
|
|
sandboxHome(t)
|
|
// the folder Claude Code's program is in, named to run commands if
|
|
// its path went into the line unquoted
|
|
dir := filepath.Join(t.TempDir(), `it's a' $(touch made1) `+"`touch made2`"+`; touch made3 #`)
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
prog := filepath.Join(dir, "claude")
|
|
if err := os.WriteFile(prog, []byte("#!/bin/sh\necho ran >> ran\n"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Setenv("PATH", dir+string(os.PathListSeparator)+"/usr/bin:/bin")
|
|
proc.Forget()
|
|
t.Cleanup(proc.Forget)
|
|
|
|
var opened []string
|
|
was := agentTerminal
|
|
agentTerminal = func(run, choice string) error { opened = append(opened, run); return nil }
|
|
t.Cleanup(func() { agentTerminal = was })
|
|
|
|
h := Handler(nil, nil)
|
|
post := func(id string) int {
|
|
t.Helper()
|
|
b, _ := json.Marshal(map[string]string{"agent": id})
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest("POST", "/api/agents/open", strings.NewReader(string(b))))
|
|
return rec.Code
|
|
}
|
|
// an id that isn't an agent's, whatever it spells, opens nothing
|
|
for _, id := range []string{"claude; touch made4", "claude'$(touch made5)'", "cl"} {
|
|
if code := post(id); code == http.StatusNoContent || len(opened) != 0 {
|
|
t.Fatalf("%q: %d, opened %q", id, code, opened)
|
|
}
|
|
}
|
|
|
|
var row *agentJSON
|
|
s := state()
|
|
for i := range s.Agents {
|
|
if s.Agents[i].ID == "claude" {
|
|
row = &s.Agents[i]
|
|
}
|
|
}
|
|
if row == nil {
|
|
t.Fatalf("claude not detected on PATH %q", os.Getenv("PATH"))
|
|
}
|
|
if row.Terminal != (runtime.GOOS == "darwin") {
|
|
t.Fatalf("the row's terminal is %v on %s", row.Terminal, runtime.GOOS)
|
|
}
|
|
|
|
a, err := agent.Find("claude")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run := agentCommand(a)
|
|
if runtime.GOOS == "darwin" {
|
|
if code := post("claude"); code != http.StatusNoContent || len(opened) != 1 {
|
|
t.Fatalf("claude: %d, opened %q", code, opened)
|
|
}
|
|
run = opened[0]
|
|
} else if code := post("claude"); code == http.StatusNoContent || len(opened) != 0 {
|
|
t.Fatalf("off a Mac: %d, opened %q", code, opened)
|
|
}
|
|
|
|
// the line run as the .command file runs it, in a folder of its own
|
|
work := t.TempDir()
|
|
cmd := proc.Command("/bin/sh", "-c", run)
|
|
cmd.Dir = work
|
|
out, err := cmd.CombinedOutput()
|
|
for _, d := range []string{work, dir} {
|
|
if made, _ := filepath.Glob(filepath.Join(d, "made*")); len(made) != 0 {
|
|
t.Fatalf("%q ran more than the agent: %q", run, made)
|
|
}
|
|
}
|
|
if b, _ := os.ReadFile(filepath.Join(work, "ran")); err != nil || string(b) != "ran\n" {
|
|
t.Fatalf("%q didn't run the agent's program: %q (%v %s)", run, b, err, out)
|
|
}
|
|
}
|
|
|
|
// A desktop app's row has no command to open, and neither has an agent in
|
|
// a WSL distro: no Open in terminal.
|
|
func TestAgentCommandNeedsAProgram(t *testing.T) {
|
|
sandboxHome(t)
|
|
proc.Forget()
|
|
t.Cleanup(proc.Forget)
|
|
for _, a := range []*agent.Agent{
|
|
{ID: "app", Name: "An app"},
|
|
{ID: "claude@wsl:Ubuntu", Bin: "sh", WSL: "Ubuntu", Launch: func() string { return "claude" }},
|
|
} {
|
|
if run := agentCommand(a); run != "" {
|
|
t.Errorf("%s: %q", a.ID, run)
|
|
}
|
|
}
|
|
// one that starts on magpie by its environment (agy) opens on that
|
|
if run := agentCommand(&agent.Agent{ID: "agy", Bin: "agy", Launch: func() string { return "X=1 agy" }}); run != "X=1 agy" {
|
|
t.Errorf("agy: %q", run)
|
|
}
|
|
}
|