Skip to content

fix(rex): select the focused Pi session - #145

Merged
backnotprop merged 4 commits into
plannotator:mainfrom
kaushikgopal:kaush/no-issue/pi-rex-session
Nov 22, 2026
Merged

backnotprop merged 4 commits into
plannotator:mainfrom
kaushikgopal:kaush/no-issue/pi-rex-session

Conversation

@kaushikgopal

Copy link
Copy Markdown
Contributor

Rex can open a reply from another Pi session when multiple sessions share a working directory. The launcher passes the foreground PID, but Pi transcript discovery ignores it and picks the most recently modified session in that directory.

This adds a Pi extension that reports the live session path, ID, and active branch over a private Unix socket. Rex reads that snapshot for the focused process and refuses to guess if it is unavailable. It also checks the session again before sending feedback. Pi users need to install the extension and restart Pi or run /reload; the README includes the command.

The session check and terminal paste are separate operations. A session switch between them can still direct feedback to the replacement session.

Validation
  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets -- -D warnings
  • cargo test --workspace
  • npm test --prefix pi
  • python3 herdr/test-manifest.py
  • Started two isolated Pi TUIs in the same directory and used a fake Rex CLI to verify that each foreground PID selected its own reply. Also checked live Pi /reload and /new endpoint lifecycle.

The live test used a fake Rex CLI, not the Rex UI. Linux, musl, and Windows builds are left to CI.

@backnotprop

Copy link
Copy Markdown
Contributor

Thanks for tracking this down. The diagnosis is right: last ignored the pid Rex passes. Since you opened this, main gained a Pi extension whose registry already records each session's pid and transcript, so I put up a much smaller fix that uses it: #156 (you're credited as co-author). Because it doesn't need a second extension, Pi users without it keep today's behaviour.

Could you check whether it covers your setup? One case it leaves out: moving within the Pi tree (/tree) before sending a message, since that position isn't saved anywhere yet. If you need that case, we could add a field to the registry entry. I'll leave this PR open until you've had a look.

sebastianst pushed a commit to sebastianst/plannotator-tui that referenced this pull request Nov 15, 2026
…west in the folder (plannotator#156)

With two Pi sessions in one folder, `last --pid` ignored the pid and opened the most
recently written transcript. A Pi running our extension lists its pid and transcript in
the Pi link registry; `last` now uses that entry when the pid matches, and keeps the
newest-in-folder choice otherwise.

Reported and diagnosed in plannotator#145.

Co-authored-by: KG <[email protected]>
backnotprop and others added 3 commits November 21, 2026 22:19
Conflict in rex/launch.rs: main split the launcher into run and open and
added tab placement. The Pi session lookup moves into open; the new
picker and argv call sites pass no Pi session.
…ocket

Main already has a Pi extension that lists each session in the Pi link
registry, and plannotator#156 uses its pid and transcript for `last --pid`. This
drops the parts of this branch that the registry already covers, and
keeps what it did not:

- Dropped: the second extension in pi/ (its Unix socket server, tests,
  package files), the root package.json, the CI Node jobs, the socket
  reader in rex/pi.rs, and the `--pi-leaf-id` flag that carried its
  answer to the pane. Pi users without our extension keep the folder's
  newest transcript, as on main, instead of an error.
- /tree position: `/tree` moves Pi's leaf in memory and writes nothing
  to the transcript, so `last` showed the reply at the end of the newest
  branch, not the one Pi was on. The extension now writes `leaf` (Pi's
  current entry) and `tip` (the newest entry) into its registry entry,
  and rewrites it on `session_tree`. While the transcript's newest entry
  is still `tip`, `last` reads the branch ending at `leaf`; once Pi has
  appended, the newest entry is the leaf again. This uses this branch's
  branch walk in the hosts crate, with its tests adapted. It applies to
  any Pi transcript a registry entry names, so it also covers `--session`
  from the Herdr and herdr-core launchers.
- Session check before Send: a Rex `last` review of a Pi records the
  session id the registry lists for its pid, and Send refuses when Pi
  has since moved to another session (`/new`, `/resume`) or the entry is
  gone, as this branch's guard did with the socket.
- No guessing for a registered Pi with no saved reply: when the entry
  for the pid has no transcript file yet (a new session before its first
  reply, or `--no-session`), `last` says so instead of opening the
  folder's newest transcript, which belongs to another session.

Tests: hosts tests on the two-branch fixture (moved to the other branch,
moved back along the same branch, appended since, empty, missing,
dangling and cyclic leaves); `tests/last.rs` cases through the binary
with a fake registry entry, by pid and by --session; Rex unit tests for
the session check; and the extension's e2e test (real pi in RPC mode,
fake model) now navigates the tree and checks the entry.

Co-authored-by: KG <[email protected]>
Review follow-ups on the registry-based Pi session and tree position:

- The tree position comes from the entry of the pid `last` was given,
  not from whichever entry names the transcript last: two Pis can
  resume one session file, each at its own place. Without a pid (a
  plain --session or the folder), the entry written last.
- An entry counts only when its Pi answers a ping, as the agent picker
  already requires (`live_pi_sessions`, which deletes an entry whose
  port refuses). A crashed Pi's place no longer steers a read, and a
  reused pid no longer gets a stale entry's refusal.
- Clearer messages: a Pi moved with /tree to before its first reply is
  told so instead of "no assistant messages yet", and `pi --no-session`
  is told the session is not being saved.
- Rex Send refusals for a Pi that changed session, or whose link does
  not answer, are `Failed`, not `Unavailable`: nothing is copied and the
  notes stay pending for E (decision 13 as amended). The messages say
  what to do. /fork and /clone stay refused; the spec says why.
- The launcher hands the pane the registry root it found Pi in, and a
  pane that cannot read a registry at all sends without the check.
- The extension reads the session's entries inside its try, and its
  comment says how stale entries are found.
- README: run `plannotator-tui pi install` again after upgrading; the
  binary has no check for an older extension.

Tests: two Pis on one file, a Pi that is gone, --no-session, a move to
before the first reply, `deliver()` against a fake rex and a real
registry for each refusal, and the launcher's pin.
@backnotprop

Copy link
Copy Markdown
Contributor

Thanks again for this. Rather than leave it conflicting, we pushed three commits to your branch: a merge of current main, a commit that keeps the parts of your work main was still missing and drops the parts the Pi link registry (from #156) already covers, and a round of review fixes.

Kept, built on the registry instead of the socket:

  • The /tree position. Our extension now records Pi's current point (leaf) and the newest entry (tip) in its registry entry, and rewrites it on session_tree. last reads the branch at leaf until Pi writes something new. When it is given a pid, it uses only that Pi's own entry, and an entry counts only if its Pi answers a ping. Your branch walk in the hosts crate and its tests are kept, adapted to this.
  • Your session check before Send. A Rex last review of a Pi records the session id and refuses to send if Pi has moved to another session or doesn't answer. It copies nothing and keeps the notes pending so you can retry with E.
  • Not guessing when Pi is registered but has nothing saved. last says the session has no reply yet, or is not being saved (--no-session), instead of opening another session's transcript.

Dropped: the second extension in pi/ with its socket server, the root package.json, the CI Node jobs, rex/pi.rs and the --pi-leaf-id flag. Pi users without our extension keep today's behaviour instead of getting an error. You're co-author on the main commit.

We're merging it now so it ships in the next release. Could you check that it covers your setup? Run plannotator-tui pi install, then /reload in Pi. If something is off, open an issue or reply here and we'll fix it.

@backnotprop
backnotprop added this pull request to the merge queue Nov 22, 2026
Merged via the queue into plannotator:main with commit 1683fe4 Nov 22, 2026
3 checks passed
@backnotprop backnotprop mentioned this pull request Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants