Skip to content

fix(bw*): keep OpenTUI capability queries off the main screen - #53

Merged
Max Rakitin (mrakitin) merged 4 commits into
NSLS2:mainfrom
padraic-shafer:fix/opentui-probe-altscreen
Dec 2, 2026
Merged

Max Rakitin (mrakitin) merged 4 commits into
NSLS2:mainfrom
padraic-shafer:fix/opentui-probe-altscreen

Conversation

@padraic-shafer

@padraic-shafer Padraic Shafer (padraic-shafer) commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Stop the capability queries that opencode's TUI sends at startup from being
printed on the shell's screen of beamline workstations that start it with
bla (or bwopencode/bwopencode2 directly).

Description

On the RHEL 8 workstations the screen showed, right after the launcher's
messages, a line of boxed ESC glyphs and text such as
?1016$p ?2027$p ... ?u 99;i=opentui-notifications:p=?; ... 7501;? ... Gi=31337,s=1,v=1,a=q,t=d,f=24;AAAA
followed by the next prompt on the same line (e.g. SXN bash-4.4$).

These are the terminal capability queries of OpenTUI, the library opencode2
draws with (queryTerminalSend() in packages/native/src/session.zig, which
runs before enterAltScreen()). They are sent to the main screen. A terminal
that does not know them prints them; newer terminals and tmux ignore them. The
launcher is not the cause: sudo passes the bytes through unchanged, and
bwopencode2 started directly does the same. It is not the stray mouse-report
problem of #23, which is input arriving after the tool exits.

Fix: launch_sandbox writes CSI ? 1049 h (switch to the alternate screen)
as its last step before the exec, so the queries land on the alternate
screen. OpenTUI clears that screen when it switches, and leaves it when it
exits, so the main screen and scrollback are not touched. The launcher's own
messages (network report, warnings) are printed before the switch and stay in
the scrollback.

  • bwopencode2 decides from guard_tool_args, which already finds the
    subcommand (and adds --standalone) and now also sets TOOL_STARTS_TUI: 1
    for no subcommand (or a directory argument) and no -h/--help,
    -v/--version, --completions[=SHELL] or --wizard anywhere in the arguments. So
    bla, bwopencode2 and bwopencode2 -s ses_abc switch; bwopencode2 run ...,
    --version and --log-level info run hello do not. bwopencode has no
    guard, so it switches only when given no arguments.
  • opentui_altscreen_prepare (lib) adds the checks the wrapper cannot make
    (stdin and stdout are a terminal, the opt-out is not set), and the wrappers
    call it just before launch_sandbox, never for --exec or --serve.
  • BWRAP_OPENTUI_ALTSCREEN=0 in the host environment turns it off.
  • No change to bla: it starts the wrapper with no arguments, on a terminal
    (sudo's pty).

Open points for reviewers:

  • If opencode exits before it draws anything, its error message is on the
    alternate screen and the shell stays there until tput rmcup (or reset):
    the launcher hands its process over to the sandbox, so nothing is left to
    switch back. Documented in docs/bw-wrappers.md. Switching back from inside
    the sandbox when the tool fails (CSI ? 1047 l) could be a follow-up.
  • bwopencode (opencode 1) is covered too, because it also uses OpenTUI, but I
    have only seen the printed queries with bwopencode2.
  • The proper fix is upstream: OpenTUI should send its queries after it enters
    the alternate screen. An issue for anomalyco/opentui is drafted separately;
    this change can be removed when that lands.

Testing

  • tests/test-opentui-altscreen.sh (new, 28 checks, run in a pseudo-terminal):
    which bwopencode2 arguments start the TUI (run through guard_tool_args, so
    after the --standalone rewrite), when the switch is made, and that launch_sandbox sends it after
    its own messages and before the tool starts, with a stub in place of bwrap.
    It is not run by CI (like the other shell tests); pixi run -e dev test
    runs the Python tests only.
  • Also run and passing: tests/test-bwopencode2-serve.sh,
    tests/test-bla-git-config.sh, tests/test-net-lustre-guard.sh,
    pixi run -e dev test, and prek on the changed files.
  • Tried on vdi-10-65-31-215 (RHEL 8.10, gnome-terminal 3.28.3, VTE 0.52.4,
    opencode v2.4.3; the same OS as the SXN workstation where the junk was
    reported, and it shows the same junk), with this branch at 447e08a:
    • the installed bwopencode2 prints the queries and the next prompt starts on
      the same line;
    • bwopencode2 and bwopencode2 -c leave a clean screen and a prompt on
      their own line after quitting opencode;
    • with BWRAP_OPENTUI_ALTSCREEN=0 the queries are printed again;
    • --version, --help, --log-level info --version and --exec echo hello
      print normally with no screen switch: script records no ESC[?1049h for
      --version;
    • script on a TUI start shows the switch to the alternate screen at byte 171,
      before OpenTUI's first query at byte 201, then OpenTUI's own switch at 391.
  • Seen on the same terminal: an exit before the TUI draws (--prompt with no
    value) leaves the shell on the alternate screen, with the error there and not
    in the scrollback, until tput rmcup (see the open points).
  • Not tested: through bla on an SXN workstation, which needs the change
    deployed.

Checklist

  • Edit the sections above with relevant information
  • Update in-repo documentation, if applicable
  • Run pixi run -e dev lint locally (or wait for CI) — see .github/workflows/lint.yml
  • Test the changes on a representative managed host (or runansible deployment)

Assisted-by: opencode:claude-sonnet-5-5

OpenTUI sends its terminal capability queries before it switches to the alternate screen, so a terminal that does not know them (the VTE terminal on RHEL 8 workstations) prints them on the shell's screen, and the next prompt starts on the same line.

Switch to the alternate screen in launch_sandbox, after its own messages, when a TUI will start: stdin and stdout are a terminal and there is no subcommand, --exec or --serve. BWRAP_OPENTUI_ALTSCREEN=0 turns it off.

Assisted-by: opencode:claude-sonnet-5-5
Copilot AI balanced review requested due to automatic review settings December 2, 2026 17:25
If opencode exits before it draws anything, nothing is left to switch the terminal back, so the shell stays on the alternate screen. Document the recovery (tput rmcup) next to the switch.

Assisted-by: opencode:claude-sonnet-5-5

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The wrapper incorrectly enters the alternate screen for text-only commands and lacks restoration on startup failures, leaving subsequent prompts there with normal scrollback hidden.

2 open issues
What changed in this PR

Moves OpenTUI startup capability queries onto the alternate screen to prevent terminal artifacts when launching OpenCode through the sandbox wrappers.

Changes:

  • Adds shared screen-switch preparation and integrates both OpenCode wrappers.
  • Adds terminal and argument checks plus a host-environment opt-out.
  • Documents the workaround and adds pseudo-terminal checks.
File Description
tests/​test-opentui-altscreen.sh Checks screen-switch decisions and output ordering.
lib/​bwrap_sandbox_lib.sh Adds preparation logic and the pre-launch screen switch.
docs/​bw-wrappers.md Documents behavior, limitations, and opt-out.
docs/​beamline-agent.md Explains applicability to beamline launches.
bin/​bwopencode2 Calls preparation outside exec and serve modes.
bin/​bwopencode Calls preparation outside exec mode.

Review effort: Balanced

Comment thread lib/bwrap_sandbox_lib.sh Resolved
Comment thread lib/bwrap_sandbox_lib.sh Resolved
The first tool argument is not enough: the argument guard puts --standalone in front, so --version and --log-level info run hello looked like a TUI start and left the shell on the alternate screen.

Let guard_tool_args, which already finds the subcommand, say whether the TUI starts (no subcommand, no --help, --version, --completions or --wizard), and test it with the rewritten arguments. bwopencode has no guard, so it switches only when given no arguments. opentui_altscreen_prepare keeps only the terminal and opt-out checks.

Assisted-by: opencode:claude-sonnet-5-5

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The --completions=bash form incorrectly enables TUI mode, switching completion output to the alternate screen and leaving the next shell prompt there.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings December 2, 2026 18:35

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The --completions=bash form is still misclassified as launching the TUI, leaving completion output and the subsequent shell prompt on the alternate screen without cleanup.

No open issues 🎉

Review effort: Balanced

--completions=bash, with =, was not recognised as a text-only option, so the completion script was printed on the alternate screen and the shell was left there.

Assisted-by: opencode:claude-sonnet-5-5
Copilot AI balanced review requested due to automatic review settings December 2, 2026 18:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human acceptance is needed for the retained limitation that failures after the alternate-screen switch can leave the shell there, affecting the deployed workstation experience.

No open issues 🎉

Review effort: Balanced

@mrakitin Max Rakitin (mrakitin) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not read the test in details, other changes look good.

@mrakitin
Max Rakitin (mrakitin) merged commit 11fefbe into NSLS2:main Dec 2, 2026
1 check passed
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.

3 participants