Skip to content

fix(rex): find the session from REX_BLOCK - #43

Closed
jonleopard wants to merge 1 commit into
lgalabru:mainfrom
jonleopard:fix/rex-block-session
Closed

jonleopard wants to merge 1 commit into
lgalabru:mainfrom
jonleopard:fix/rex-block-session

Conversation

@jonleopard

@jonleopard jonleopard commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Closes #42

Summary

Rex Server 0.1.0-preview.23 and later stopped exporting REX_SESSION in new terminals because tabs can move between sessions; terminals now export REX_SERVER, REX_BLOCK, and REX_BIN_DIR. axel_rex::detect() required both REX_SESSION and REX_BLOCK, so Axel treated fresh Rex panes as outside Rex.

axel_rex::detect()  (cached once per process)
  no REX_BLOCK  ──► None (no subprocess)
  REX_BLOCK set ──► rex --autostart=false --timeout 2s whoami --json
                      ├─ valid session_id ──► Some(RexEnv { session, block, server })
                      └─ error / timeout / null ──► fall back to REX_SESSION if set, else None

axel_rex::program()
  $AXEL_REX_BIN ─► $REX_BIN_DIR/rex (if file exists) ─► PATH ─► app install paths ─► "rex"
  • crates/rex/src/lib.rs:
    • Treat REX_BLOCK as the inside-Rex signal and resolve the real session:<uuid> through Runner (rex --autostart=false --timeout 2s whoami --json), cached once per process. When Rex answers, its session wins even over a stale REX_SESSION; older Rex builds (or any failure/timeout/invalid JSON) fall back to REX_SESSION.
    • Bound both child exit (2 s timeout) and post-exit pipe collection (PIPE_DRAIN_GRACE, 200 ms over channels) in wait_or_stop so a descendant holding stdout/stderr open cannot hang detection.
    • Prefer $REX_BIN_DIR/rex (when present) after $AXEL_REX_BIN and before PATH and app install paths so inside a Rex terminal Axel invokes the binary matching the running server.
  • crates/tui/src/stream.rs: route stream tool links through axel_rex::detect() instead of reading REX_SESSION directly.
  • crates/axel/src/cmd/doctor.rs & crates/i18n/translations.yaml: update the "not detected" message (English and zh-Hans) to reference REX_BLOCK / unavailable session.
  • docs/CONTRACTS.md, docs/DESIGN.md, docs/RESEARCH.md: align docs with REX_BLOCK-based detection and caching.

Evidence

  • Before (Rex 0.1.0-preview.25, fresh pane with REX_BLOCK set and REX_SESSION unset):
    axel doctor reports rex: not detected (REX_SESSION unset); launcher runs inline; axel runs inline; tool links are absent.
  • After (same Rex build, isolated temp AXEL_HOME, REX_SESSION unset):
    • Default PATH: axel doctor resolves session:<uuid> and block:<uuid>.
    • Reduced PATH=/usr/bin:/bin (rex absent from PATH, REX_BIN_DIR preserved): axel doctor still resolves session:<uuid> and block:<uuid>.
    • axel thread split opens the thread/compose/tokens panes instead of running inline; stream emits a valid axel-links://tools?... OSC 8 link; a failing stub rex falls back to REX_SESSION.
  • Local gates on commit 5839423:
    • cargo clippy --workspace --all-targets --locked --offline -- -D warnings: pass (0 warnings).
    • cargo test --workspace --locked --offline: 759 passed, 0 failed, 2 ignored.
    • cargo test --locked --offline -p axel-rex: 51 passed, 0 failed (+ 1 doctest) identically across default env, all REX_* unset, and synthetic REX_BLOCK set (pure detect_with/Fake unit tests + /bin/sh timeout/open-pipe process tests; no test spawns rex).
  • Not proven: interactive keyboard Tab switching across panes and clicking a tool link in Rex.app.

Merge Danger

Door: two-way

Blast Radius: rex-detection

  • Process-lifetime cache: detect() caches its result once per process, so a long-lived process retains its initial session after a tab moves between sessions and retains a failed detection after a transient error.
  • Subprocess timeout bound: with the 200 ms post-exit pipe-drain grace, a lookup can take up to ~2.2 s in the pathological case where rex exits with a descendant holding stdout/stderr open. For Rex::ask, normal exits and the 200 ms poll interval are unchanged; in that abnormal lingering-pipe case it now returns Withdrawn instead of hanging.
  • Binary resolution: preferring $REX_BIN_DIR/rex affects every rex CLI call inside Rex terminals that export REX_BIN_DIR (overriding an older rex earlier on PATH while keeping $AXEL_REX_BIN highest precedence).
  • Eager lookup in Ctx::open(): every interactive axel command run inside a Rex terminal now runs rex --autostart=false --timeout 2s whoami --json once (~30 ms) even when it doesn't touch Rex (agent children strip REX_* and skip it). Kept RexEnv / detect() unchanged to stay small and avoid reshaping CONTRACTS.md; if you'd rather make Ctx's Rex handle lazy or switch detect() to block-only / rex.control.v1 and resolve the session on demand, say the word.

Rex Server 0.1.0-preview.23 and later stopped exporting REX_SESSION in
new terminals because tabs can move between sessions; terminals now
export REX_SERVER, REX_BLOCK and REX_BIN_DIR.

Treat REX_BLOCK as the inside-Rex signal and resolve the block's current
session through `rex --autostart=false --timeout 2s whoami --json`
behind `axel_rex::Runner`, bounded by a 2s child-process timeout and
cached once per process. Pipe collection in `wait_or_stop` is also
bounded after child exit (200 ms grace over channels) so a descendant
holding stdout or stderr open cannot hang detection; for `Rex::ask`,
normal exits are unchanged and an abnormal lingering-pipe exit now
returns `Withdrawn` instead of hanging. When Rex answers, its session
wins even if a stale REX_SESSION is present. For older Rex builds (or
any case where Rex cannot answer, times out, or returns invalid/null
JSON), fall back to REX_SESSION if set; without REX_BLOCK or a resolved
session, return None.

Prefer `$REX_BIN_DIR/rex` (when that file exists) after `$AXEL_REX_BIN`
and before `PATH` and app install paths in `axel_rex::program()`. Inside
a Rex terminal that sets `REX_BIN_DIR`, this changes every `rex` CLI
call to run the binary matching the running server rather than an older
`rex` on `PATH` or failing when `rex` is omitted from `PATH`.

Update `axel-tui` stream tool links to use `axel_rex::detect()` instead
of reading REX_SESSION directly, update `axel doctor` and its zh-Hans
translation, and align docs/CONTRACTS.md, docs/DESIGN.md and
docs/RESEARCH.md.

Verified on Rex 0.1.0-preview.25 in isolated temp AXEL_HOME panes
(AXEL_KEYSTORE=file) with REX_BLOCK set and REX_SESSION unset: `axel
doctor` resolves the real session and block both with default PATH and
with PATH reduced to `/usr/bin:/bin` (no `rex` on PATH, `REX_BIN_DIR`
kept); `axel thread split` opens the thread/compose/tokens panes instead
of running inline, the stream emits a valid `axel-links://tools?...`
OSC 8 hyperlink, and a failing stub `rex` exercises the REX_SESSION
fallback.

Needs manual verification at the keyboard: interactive Tab switching
across panes and clicking a tool link in Rex.app.
@vercel

vercel Bot commented Nov 26, 2026

Copy link
Copy Markdown

@jonleopard is attempting to deploy a commit to the ludo-galabru Team on Vercel.

A member of the Team first needs to authorize it.

@jonleopard

Copy link
Copy Markdown
Contributor Author

Superseded by 36668ee / v0.3.164.

@jonleopard jonleopard closed this Nov 26, 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.

Broken on the latest rex update?

1 participant