gui: Rex switches to the tab magpie opens, through the app's session.select (#4565, StringKe)
StringKe tried v0.1.2446 on the real Rex app (Rex Beta 0.1.0 (1696)):
the tab opens, but the app keeps showing the tab the user was on. The
new tab shows only in the sidebar, and `open -b` only brings the window
forward. The app's own session.select action, sent to the app client,
does switch to it.
After `rex run` (reused magpie session) or `rex new` (now with --json,
for session_id and initial_windows[0].window_id), openInRex brings Rex
forward and then calls rexSelect. rexSelect reads `rex client list
--json`, takes the client of info.kind "app" whose session_ids hold the
session, else the last app connected, and skips one whose
connection_state is "lost". Then it runs `rex -C <client_id> do
session.select session_id=<id> window_id=<window>`. App actions need
Remote Control on in Rex's settings. With no app, a failed list, a
refused do or no answer within 3s (rexSelectWait), the open still
succeeds, the same way a failed rename is ignored. docs/reference.md's
Rex paragraph says this.
Command shapes come from the real rex CLI, snapshot 0.0.1793420730-
g6d0d9a, on mote with a sandbox HOME and XDG_RUNTIME_DIR: `rex --help`
(-C is a global flag, label or ID), `rex do --help`, `rex client ls
--help`, `rex api describe client.list` (client_id, info.kind
app|cli|agent|other, connection_state connected|lost, session_ids),
`rex new --json` output. With no app connected, `do` without -C exits 3
("no app client is connected"), and -C <unknown> exits 3. A client that
doesn't answer keeps `do` waiting 10s ("did not answer within 10s").
That is why the select runs after open -b and has its own 3s bound.
Tests: TestOpenInRexSelectsTheTab runs a fake rex program (a sh script,
so arguments go through a real exec), with a new session and with
magpie's session reused. It checks that the app attached to the session
is picked over another, that a lost app is never asked, and that no
app, a failing client list, a refused do (exit 1) or a do that never
answers (exec sleep 30, killed at the bound) leave the open a success
within its time. The in-process fakeRex answers client list (no app)
and new --json. TestOpenInRexReal runs these paths against the real rex
server, with no app client.
Ran, through mote-run, on this tree before the rebase onto 1e6a27d4f:
- gofmt -l; go vet ./internal/gui; GOOS=linux -tags nogui and
GOOS=windows go vet ./internal/gui; GOOS=linux -tags nogui and
GOOS=windows go build .: ok.
- MAGPIE_TEST_REX=<real rex> go test -count=1 -timeout 45m
./internal/gui: everything passed, TestOpenInRexReal included, except
TestSettingsSaveKeepsWhatItDoesNotSend (#4739, #4514) and
TestMainWindowCorners/macOS_26_SDK (#3949). Both fail the same way
alone on a detached origin/main 1e6a27d4f.
- At the rebased head on 1e6a27d4f: gofmt, go vet ./internal/gui and
MAGPIE_TEST_REX=<real rex> go test -run
"Rex|DiscoverTerminals|TerminalOpenArgs|AgentOpen" ./internal/gui: ok.
- No sleep, rex server or /tmp/rx* left on mote afterwards.
Broken by hand, each red, then restored: the old openInRex (4 of 7
cases red: "asked [], want [-C client:… do session.select …]"); the
attached-app preference dropped (the other app asked); the lost-state
skip dropped (the lost client asked); the 3s bound made an hour (the
never-answering case took 20.0s).
Not tried: the Rex app itself (its download needs a Discord sign-in),
so a real app client's session.select and what a refusal with Remote
Control off looks like are known only from StringKe's report and the
docs; the handling doesn't depend on which. Not the full go test ./...
(only internal/gui changed). No GUI Playwright run (no JS changed). Not
run on Linux or Windows (the tests skip on Windows).
This commit is contained in:
+8
-2
@@ -3034,8 +3034,14 @@ shows. So magpie finds its session with `rex ls --json` and runs
|
||||
`rex run -s <its ID> --label=<session or agent> -- /bin/sh -c '<command>; exec $SHELL -l'`,
|
||||
then names the new tab with `rex window rename`. With no such session (or one
|
||||
that ended since the list) it runs `rex new magpie --window=<session or agent> -- …`,
|
||||
whose first tab is so named. When Rex's server can't be reached (exit 4) it
|
||||
says to open Rex. The same applies to an agent's *Open in terminal* and a
|
||||
whose first tab is so named. A tab added from outside shows only in the
|
||||
app's sidebar, so magpie then brings Rex forward and asks the app to go to
|
||||
it: it takes the client of kind `app` from `rex client list --json` (the one
|
||||
attached to its session, else the last app connected) and runs
|
||||
`rex -C <client> do session.select session_id=<session> window_id=<tab>`.
|
||||
That needs *Remote Control* on in Rex's settings; with it off, with no app
|
||||
connected, or with no answer in 3 seconds, the tab stays open where it is.
|
||||
When Rex's server can't be reached (exit 4) it says to open Rex. The same applies to an agent's *Open in terminal* and a
|
||||
carried-over session (StringKe, #4565;
|
||||
[`terminal_rex.go`](../internal/gui/terminal_rex.go)).
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -178,6 +179,11 @@ const rexSessionLabel = "magpie"
|
||||
// block, not the tab). With none, `rex new magpie --window <label>` makes it,
|
||||
// its first tab so named; so does a 3 from run, the session having ended
|
||||
// since the list.
|
||||
//
|
||||
// A tab added from outside shows only in the app's sidebar: the app keeps
|
||||
// the tab it was on, and `open -b` only brings its window forward
|
||||
// (StringKe, #4565). So the app client is then asked to go to the new tab
|
||||
// (rexSelect).
|
||||
func openInRex(bin, command, label string) error {
|
||||
label = rexLabel(label)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
@@ -197,19 +203,90 @@ func openInRex(bin, command, label string) error {
|
||||
rexRun(ctx, bin, "window", "rename", "-s", id, "--", made.WindowID, label)
|
||||
}
|
||||
rexShow()
|
||||
rexSelect(ctx, bin, id, made.WindowID)
|
||||
return nil
|
||||
}
|
||||
if rexExit(err) != 3 {
|
||||
return rexError(err)
|
||||
}
|
||||
}
|
||||
if _, err := rexRun(ctx, bin, append([]string{"new", rexSessionLabel, "--window=" + label}, rexScript(command)...)...); err != nil {
|
||||
out, err := rexRun(ctx, bin, append([]string{"new", rexSessionLabel, "--window=" + label, "--json"}, rexScript(command)...)...)
|
||||
if err != nil {
|
||||
return rexError(err)
|
||||
}
|
||||
rexShow()
|
||||
var made struct {
|
||||
SessionID string `json:"session_id"`
|
||||
Windows []struct {
|
||||
WindowID string `json:"window_id"`
|
||||
} `json:"initial_windows"`
|
||||
}
|
||||
if json.Unmarshal(out, &made) == nil && made.SessionID != "" {
|
||||
w := ""
|
||||
if len(made.Windows) > 0 {
|
||||
w = made.Windows[0].WindowID
|
||||
}
|
||||
rexSelect(ctx, bin, made.SessionID, w)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// rexSelectWait bounds rexSelect. A client that doesn't answer an action
|
||||
// keeps `rex do` waiting 10s ("did not answer within 10s", tried on rex
|
||||
// 0.0.1793420730 with a CLI client), and the GUI waits for the open; the
|
||||
// window is already forward by then. A var so a test waits less.
|
||||
var rexSelectWait = 3 * time.Second
|
||||
|
||||
// rexSelect asks the Rex app to show window (a tab) of session, with
|
||||
// `rex -C <app client> do session.select session_id=… window_id=…`, the
|
||||
// app's own "Go to Session" action. The app client is the one of kind app
|
||||
// in `rex client list --json` attached to session, else the last app to
|
||||
// connect; one whose connection is lost is passed over. An app action is
|
||||
// refused unless Remote Control is on in Rex's settings, and there may be
|
||||
// no app at all (the CLI starts a server of its own): the tab is open
|
||||
// either way, so a missing client, a refusal or a slow answer is let go.
|
||||
// It runs after rexShow, so the window comes forward without waiting.
|
||||
func rexSelect(ctx context.Context, bin, session, window string) {
|
||||
ctx, cancel := context.WithTimeout(ctx, rexSelectWait)
|
||||
defer cancel()
|
||||
out, err := rexRun(ctx, bin, "client", "list", "--json")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var list struct {
|
||||
Clients []struct {
|
||||
ID string `json:"client_id"`
|
||||
State string `json:"connection_state"`
|
||||
Info struct {
|
||||
Kind string `json:"kind"`
|
||||
} `json:"info"`
|
||||
Sessions []string `json:"session_ids"`
|
||||
} `json:"clients"`
|
||||
}
|
||||
if json.Unmarshal(out, &list) != nil {
|
||||
return
|
||||
}
|
||||
app := ""
|
||||
for _, c := range list.Clients {
|
||||
if c.ID == "" || c.Info.Kind != "app" || c.State == "lost" {
|
||||
continue
|
||||
}
|
||||
if slices.Contains(c.Sessions, session) {
|
||||
app = c.ID
|
||||
break
|
||||
}
|
||||
app = c.ID // in the order they connected: the last one so far
|
||||
}
|
||||
if app == "" {
|
||||
return
|
||||
}
|
||||
args := []string{"-C", app, "do", "session.select", "session_id=" + session}
|
||||
if window != "" {
|
||||
args = append(args, "window_id="+window)
|
||||
}
|
||||
rexRun(ctx, bin, args...)
|
||||
}
|
||||
|
||||
// rexOwnSession is the full ID of magpie's own Rex session, "" with none:
|
||||
// one labelled exactly magpie, else one so labelled in another case.
|
||||
func rexOwnSession(ctx context.Context, bin string) (string, error) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"maps"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/proc"
|
||||
)
|
||||
@@ -170,7 +172,13 @@ func (f *fakeRex) run(_ context.Context, bin string, args ...string) ([]byte, er
|
||||
}
|
||||
x.windows, x.ids = []string{w}, []string{fmt.Sprintf("window:%04d", f.seq)}
|
||||
f.sessions = append(f.sessions, x)
|
||||
if _, ok := flags["json"]; ok {
|
||||
return []byte(fmt.Sprintf(`{"session_id":%q,"initial_windows":[{"window_id":%q,"block_ids":["block:%04d"],"revision":1}]}`, x.id, x.ids[0], f.seq)), nil
|
||||
}
|
||||
return []byte(x.id + "\n"), nil
|
||||
case "client":
|
||||
// no app connected: the CLI's own client only
|
||||
return []byte(`{"clients":[{"client_id":"client:cli","info":{"kind":"cli","name":"rex"},"principal":{"kind":"peer","transport":"unix","display":"uid 501"},"connected_at":"2026-10-31T23:24:11Z","connection_state":"connected"}]}`), nil
|
||||
case "run":
|
||||
i := -1
|
||||
if s, ok := flags["s"]; ok {
|
||||
@@ -431,3 +439,128 @@ func TestOpenInRexReal(t *testing.T) {
|
||||
t.Fatalf("magpie's and two: tabs %q, sessions %+v", got, list())
|
||||
}
|
||||
}
|
||||
|
||||
// fakeRexBin is a rex program for a test: a shell script that writes each
|
||||
// call's arguments to dir/log and answers from the files in dir: ls.json
|
||||
// for `rex ls`, clients.json for `rex client list` (clients.fail makes it
|
||||
// exit 1), do.exit to refuse an app action with that status and do.hang
|
||||
// to never answer one.
|
||||
func fakeRexBin(t *testing.T, dir string) string {
|
||||
t.Helper()
|
||||
bin := filepath.Join(dir, "rex")
|
||||
script := `#!/bin/sh
|
||||
d=` + shellQuote(dir) + `
|
||||
{ for a in "$@"; do printf '%s\n' "$a"; done; echo ---; } >> "$d/log"
|
||||
case "$1" in
|
||||
ls) cat "$d/ls.json" ;;
|
||||
run) echo '{"window_id":"window:run1","block_ids":["block:1"],"revision":2}' ;;
|
||||
window) ;;
|
||||
new) echo '{"session_id":"session:new1","initial_windows":[{"window_id":"window:new1","block_ids":["block:2"],"revision":1}]}' ;;
|
||||
client)
|
||||
if [ -f "$d/clients.fail" ]; then echo "Error: boom" >&2; exit 1; fi
|
||||
cat "$d/clients.json" ;;
|
||||
-C)
|
||||
[ "$3" = do ] || exit 2
|
||||
if [ -f "$d/do.hang" ]; then exec sleep 30; fi
|
||||
if [ -f "$d/do.exit" ]; then echo "Error: client refused the action: remote control is off" >&2; exit "$(cat "$d/do.exit")"; fi
|
||||
echo '{"performed_by":"'"$2"'"}' ;;
|
||||
*) echo "Error: unknown command $1" >&2; exit 2 ;;
|
||||
esac
|
||||
`
|
||||
if err := os.WriteFile(bin, []byte(script), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return bin
|
||||
}
|
||||
|
||||
// After opening the tab, magpie asks the Rex app to go to it: a tab added
|
||||
// from outside only shows in the app's sidebar, and `open -b` only brings
|
||||
// the window forward (StringKe, #4565). The app client is the one of kind
|
||||
// app attached to magpie's session, else the last app connected, never a
|
||||
// lost one; it is asked with `rex -C <client> do session.select`, with the
|
||||
// session and the new tab, for a new session and an added tab alike. Remote
|
||||
// Control off (a refused do), no app, a failed client list or an app that
|
||||
// never answers leave the tab open and the open a success. Runs a fake rex
|
||||
// program, a script, so the arguments go through a real exec.
|
||||
func TestOpenInRexSelectsTheTab(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("Rex runs on macOS, Linux and FreeBSD")
|
||||
}
|
||||
wasShow, wasWait := rexShow, rexSelectWait
|
||||
t.Cleanup(func() { rexShow, rexSelectWait = wasShow, wasWait })
|
||||
rexShow = func() {}
|
||||
rexSelectWait = 300 * time.Millisecond
|
||||
|
||||
const (
|
||||
none = `{"sessions":[{"session_id":"session:w","label":"work"}]}`
|
||||
mine = `{"sessions":[{"session_id":"session:w","label":"work"},{"session_id":"session:m","label":"magpie"}]}`
|
||||
cli = `{"client_id":"client:cli","info":{"kind":"cli","name":"rex"},"principal":{"kind":"peer","transport":"unix","display":"uid 501"},"connected_at":"2026-10-31T23:24:11Z","connection_state":"connected"}`
|
||||
appW = `{"client_id":"client:appW","info":{"kind":"app","name":"Rex.app","platform":"macos"},"principal":{"kind":"peer","transport":"unix","display":"uid 501"},"connected_at":"2026-10-31T20:00:00Z","connection_state":"connected","session_ids":["session:w"]}`
|
||||
appM = `{"client_id":"client:appM","info":{"kind":"app","name":"Rex.app","platform":"ios"},"principal":{"kind":"tailscale","transport":"tailscale","display":"me"},"connected_at":"2026-10-31T19:00:00Z","connection_state":"connected","session_ids":["session:m","session:w"]}`
|
||||
appLost = `{"client_id":"client:lost","info":{"kind":"app","name":"Rex.app"},"principal":{"kind":"peer","transport":"unix","display":"uid 501"},"connected_at":"2026-10-31T21:00:00Z","connection_state":"lost","connection_lost_at":"2026-10-31T22:00:00Z"}`
|
||||
)
|
||||
clients := func(c ...string) string { return `{"clients":[` + strings.Join(c, ",") + `]}` }
|
||||
for _, tc := range []struct {
|
||||
name, ls, clients string
|
||||
files map[string]string
|
||||
want []string // the do call, nil for none
|
||||
}{
|
||||
{"new session, an app on another", none, clients(appM, cli, appW, appLost), nil,
|
||||
[]string{"-C", "client:appW", "do", "session.select", "session_id=session:new1", "window_id=window:new1"}},
|
||||
{"magpie's session, the app attached to it", mine, clients(appM, cli, appW, appLost), nil,
|
||||
[]string{"-C", "client:appM", "do", "session.select", "session_id=session:m", "window_id=window:run1"}},
|
||||
{"only a lost app", mine, clients(cli, appLost), nil, nil},
|
||||
{"no app", none, clients(cli), nil, nil},
|
||||
{"client list fails", mine, clients(appW), map[string]string{"clients.fail": ""}, nil},
|
||||
{"remote control off", mine, clients(appW), map[string]string{"do.exit": "1"},
|
||||
[]string{"-C", "client:appW", "do", "session.select", "session_id=session:m", "window_id=window:run1"}},
|
||||
{"the app never answers", none, clients(appW), map[string]string{"do.hang": ""},
|
||||
[]string{"-C", "client:appW", "do", "session.select", "session_id=session:new1", "window_id=window:new1"}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
files := map[string]string{"ls.json": tc.ls, "clients.json": tc.clients}
|
||||
maps.Copy(files, tc.files)
|
||||
for k, v := range files {
|
||||
if err := os.WriteFile(filepath.Join(dir, k), []byte(v), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
bin := fakeRexBin(t, dir)
|
||||
start := time.Now()
|
||||
if err := openInRex(bin, "claude", "Claude Code"); err != nil {
|
||||
t.Fatalf("openInRex: %v, want the open to stand", err)
|
||||
}
|
||||
if d := time.Since(start); d > 10*time.Second {
|
||||
t.Fatalf("openInRex took %v", d)
|
||||
}
|
||||
b, _ := os.ReadFile(filepath.Join(dir, "log"))
|
||||
var calls [][]string
|
||||
for _, c := range strings.Split(strings.TrimSuffix(string(b), "---\n"), "---\n") {
|
||||
calls = append(calls, strings.Split(strings.TrimSuffix(c, "\n"), "\n"))
|
||||
}
|
||||
opened := false
|
||||
var dos [][]string
|
||||
for _, c := range calls {
|
||||
switch c[0] {
|
||||
case "run", "new":
|
||||
opened = true
|
||||
case "-C":
|
||||
dos = append(dos, c)
|
||||
}
|
||||
}
|
||||
if !opened {
|
||||
t.Fatalf("no tab opened: %q", calls)
|
||||
}
|
||||
if tc.want == nil {
|
||||
if len(dos) != 0 {
|
||||
t.Fatalf("asked %q, want no app asked", dos)
|
||||
}
|
||||
return
|
||||
}
|
||||
if len(dos) != 1 || !slices.Equal(dos[0], tc.want) {
|
||||
t.Fatalf("asked %q, want %q", dos, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user