From 994cd4fb68542dd870c46fff7935fb9cfdba453c Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 14:37:41 +0800 Subject: [PATCH] gui: Rex switches to the tab magpie opens, through the app's session.select (#4565, StringKe) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 do session.select session_id= window_id=`. 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 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= 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= 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). --- docs/reference.md | 10 ++- internal/gui/terminal_rex.go | 79 +++++++++++++++++- internal/gui/terminal_rex_test.go | 133 ++++++++++++++++++++++++++++++ 3 files changed, 219 insertions(+), 3 deletions(-) diff --git a/docs/reference.md b/docs/reference.md index fe3128c05..0551d31fc 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -3034,8 +3034,14 @@ shows. So magpie finds its session with `rex ls --json` and runs `rex run -s --label= -- /bin/sh -c '; 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= -- …`, -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 do session.select session_id= window_id=`. +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)). diff --git a/internal/gui/terminal_rex.go b/internal/gui/terminal_rex.go index 4a5b835c4..5d3261ca8 100644 --- a/internal/gui/terminal_rex.go +++ b/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