Skip to content

feat(terminal): broadcast theme changes to PTYs via DEC mode 2031 - #896

Merged
nwparker merged 2 commits into
stablyai:mainfrom
AmoabaKelvin:feat/terminal-color-scheme-reporting
Apr 22, 2026
Merged

nwparker merged 2 commits into
stablyai:mainfrom
AmoabaKelvin:feat/terminal-color-scheme-reporting

Conversation

@AmoabaKelvin

Copy link
Copy Markdown
Collaborator

Summary

Claude Code's /theme auto reads Orca's background via OSC 11 at startup (xterm.js answers that natively), so claude matches the terminal on launch. But when Orca's theme flips mid-session (dark to light or vice versa), claude had no signal: Orca never told running TUIs anything changed, so auto-theme stayed stuck wherever it started.

This wires the DEC private mode 2031 + CSI 997 protocol that Neovim, tmux, Kitty, and Ghostty use. A TUI sends CSI ?2031h to subscribe; when the theme flips, the terminal pushes CSI ?997;1n (dark) or CSI ?997;2n (light). The TUI then re-queries OSC 11 and repaints.

  • Per-pane subscription flag and last-emitted mode live on TerminalPane.
  • The CSI ?2031 h/l handlers are installed in onPaneCreated before the PTY attaches so the child's initial subscribe is captured, and an immediate push on enable seeds the current mode without a query.
  • applyTerminalAppearance only emits CSI 997 when the resolved mode actually flipped, so font/size/opacity tweaks that re-run the same effect don't spam subscribed TUIs.

Screenshots

Before
https://github.com/user-attachments/assets/bd350b91-3e8d-46b0-ace5-e40c7ead567e

After
See the theme change automatically without having to reset claude's theme from the /theme command

CleanShot.2026-04-21.at.17.49.17.mp4

Testing

  • pnpm lint
  • pnpm typecheck
  • pnpm test
  • pnpm build

Claude Code's /theme auto reads Orca's background via OSC 11 at startup
(xterm.js answers that natively), so claude matches the terminal on
launch. But when Orca's theme flips mid-session, claude had no signal:
Orca never told running TUIs anything changed, so auto-theme stayed
stuck wherever it started.

This wires the DEC private mode 2031 + CSI 997 protocol that Neovim,
tmux, Kitty, and Ghostty use. A TUI sends CSI ?2031h to subscribe;
when the theme flips, the terminal pushes CSI ?997;1n (dark) or
CSI ?997;2n (light). The TUI then re-queries OSC 11 and repaints.

- Per-pane subscription flag and last-emitted mode live on TerminalPane.
- The CSI ?2031 h/l handlers are installed in onPaneCreated before the
  PTY attaches so the child's initial subscribe is captured, and an
  immediate push on enable seeds the current mode without a query.
- applyTerminalAppearance only emits CSI 997 when the resolved mode
  actually flipped, so font/size/opacity tweaks that re-run the same
  effect don't spam subscribed TUIs.
@AmoabaKelvin
AmoabaKelvin force-pushed the feat/terminal-color-scheme-reporting branch from 5a39c4b to 8337292 Compare April 22, 2026 12:21
@nwparker
nwparker self-requested a review April 22, 2026 18:26
Pull the CSI 997 sequence builder and the subscribe/flip gate out of
applyTerminalAppearance so the appearance path and the subscribe-time
seed path in the lifecycle hook share one source of truth. Annotate the
?h / ?l parser handlers to make it explicit they observe (return false)
rather than suppress — compound DEC sequences like `?25;2031h` must
still flow to xterm's built-in handler. Adds unit coverage for the
no-op-on-no-flip gate, reconnect path, and per-pane tracking.
@nwparker
nwparker merged commit 9a8d716 into stablyai:main Apr 22, 2026
2 checks passed
@nwparker

Copy link
Copy Markdown
Contributor

Thanks alot for the change @AmoabaKelvin !

nwparker added a commit that referenced this pull request Apr 22, 2026
xterm.js ships with no OSC 52 handler, so tmux, Neovim, fzf, and ripgrep
running inside Orca — locally or over SSH — silently fail to copy to the
system clipboard. Add an opt-in handler gated behind a new setting
`terminalAllowOsc52Clipboard` (default off because untrusted output
piped into the terminal can silently overwrite the clipboard).

Implementation mirrors the DEC 2031 wiring from PR #896: extract parsing
into a pure helper (parseOsc52) with a full unit-test suite, register
the handler in onPaneCreated and dispose it symmetrically in
onPaneClosed, and read the gate from settingsRef at fire time so the
toggle takes effect without recreating panes.

Clipboard *queries* (Pd = "?") are intentionally dropped — answering
them would leak the user's clipboard to any process writing to the
PTY — along with malformed payloads, oversized payloads (>128KB),
and payloads using unknown selection letters.
nwparker added a commit that referenced this pull request Apr 22, 2026
…956)

Wire the two escape sequences that TUIs and modern shells use to tell
the terminal about state that xterm.js doesn't handle natively:

- OSC 7  — shell emits its current working directory on every `cd`.
- OSC 133 — FinalTerm / iTerm semantic prompt marks (A/B/C/D with
  optional exit code on D).

Parsing lives in a new pure `shell-integration.ts` helper with 21 unit
tests covering URI decoding, Windows UNC / drive-letter paths, all four
OSC 133 letters, the `key=value` hint suffix some emitters append, and
the ring-buffer cap on recorded marks.

Wiring follows the DEC 2031 / OSC 52 pattern from #896 and #952:

- Handlers registered per-pane in onPaneCreated, disposed symmetrically
  in onPaneClosed.
- Two new refs (`paneCwdRef`, `paneSemanticMarksRef`) live on
  TerminalPane and are passed through UseTerminalPaneLifecycleDeps.
- Semantic marks record the absolute buffer row (`baseY + cursorY`) so
  future block-navigation consumers can scroll to them.
- A 10 000-entry ring buffer caps per-pane memory.

Shipped consumer: the terminal context menu grows a platform-aware
"Reveal in Finder / Show in Explorer / Open Folder" item that opens
the shell's current directory in the native file manager. The item
is hidden when no OSC 7 has been received, consistent with how other
terminal emulators surface shell-integration-dependent features.

No consumer yet for OSC 133 — recording is cheap and the consumer UX
(block-nav keybinds, exit-code gutters) deserves its own design pass.
nwparker pushed a commit to AmoabaKelvin/orca that referenced this pull request Apr 24, 2026
The daemon's HeadlessEmulator had its onData wired back to
subprocess.write so xterm's auto-replies to terminal queries (OSC
10/11/12, DA1/DA2, DSR, DECRPM) flowed to the shell's stdin. But
handleSubprocessData writes data to the emulator synchronously,
in-process, before forwarding the same bytes to the renderer over IPC
— so the emulator's reply always won the race against the renderer's
xterm. The renderer is the authoritative responder (it has the real
theme, cursor position, modes); any daemon-side reply is a double-
reply with wrong values.

Most visible symptom: the emulator has no ITheme configured, so its
default background is xterm.js's #000000. Claude Code's /theme auto
sends OSC 11 at startup and after every CSI 997 mode-flip broadcast,
and always got back the emulator's black — forcing it into dark
regardless of Orca's configured terminal theme. With
experimentalTerminalDaemon: true this broke both the initial theme
match and the mid-session flip added in stablyai#896.

On the original intent: the pre-fix wiring was guarded with an
exited/disposed check to avoid a dead-NAPI-handle crash on
subprocess.write. That guard was defensive plumbing around a path
that never needed to exist — no caller depends on daemon-side query
replies. When the renderer is attached it answers correctly; when
detached, there is no visible terminal and any reply the daemon could
produce would carry wrong values anyway.

Fix: drop the onData wiring on the emulator entirely. The emulator
still parses for snapshot/state tracking (scrollback, cwd via OSC 7,
mode flags); xterm's auto-replies are now discarded instead of
forwarded.

Regression tests in session.test.ts cover OSC 11, DA1, and DSR — each
would fail on the pre-fix code by observing an auto-reply land on
subprocess.write.
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…ablyai#896)

* feat(terminal): broadcast theme changes to PTYs via DEC mode 2031

Claude Code's /theme auto reads Orca's background via OSC 11 at startup
(xterm.js answers that natively), so claude matches the terminal on
launch. But when Orca's theme flips mid-session, claude had no signal:
Orca never told running TUIs anything changed, so auto-theme stayed
stuck wherever it started.

This wires the DEC private mode 2031 + CSI 997 protocol that Neovim,
tmux, Kitty, and Ghostty use. A TUI sends CSI ?2031h to subscribe;
when the theme flips, the terminal pushes CSI ?997;1n (dark) or
CSI ?997;2n (light). The TUI then re-queries OSC 11 and repaints.

- Per-pane subscription flag and last-emitted mode live on TerminalPane.
- The CSI ?2031 h/l handlers are installed in onPaneCreated before the
  PTY attaches so the child's initial subscribe is captured, and an
  immediate push on enable seeds the current mode without a query.
- applyTerminalAppearance only emits CSI 997 when the resolved mode
  actually flipped, so font/size/opacity tweaks that re-run the same
  effect don't spam subscribed TUIs.

* refactor(terminal): extract mode 2031 helpers and add spam-gate tests

Pull the CSI 997 sequence builder and the subscribe/flip gate out of
applyTerminalAppearance so the appearance path and the subscribe-time
seed path in the lifecycle hook share one source of truth. Annotate the
?h / ?l parser handlers to make it explicit they observe (return false)
rather than suppress — compound DEC sequences like `?25;2031h` must
still flow to xterm's built-in handler. Adds unit coverage for the
no-op-on-no-flip gate, reconnect path, and per-pane tracking.

---------

Co-authored-by: Neil <[email protected]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
xterm.js ships with no OSC 52 handler, so tmux, Neovim, fzf, and ripgrep
running inside Orca — locally or over SSH — silently fail to copy to the
system clipboard. Add an opt-in handler gated behind a new setting
`terminalAllowOsc52Clipboard` (default off because untrusted output
piped into the terminal can silently overwrite the clipboard).

Implementation mirrors the DEC 2031 wiring from PR stablyai#896: extract parsing
into a pure helper (parseOsc52) with a full unit-test suite, register
the handler in onPaneCreated and dispose it symmetrically in
onPaneClosed, and read the gate from settingsRef at fire time so the
toggle takes effect without recreating panes.

Clipboard *queries* (Pd = "?") are intentionally dropped — answering
them would leak the user's clipboard to any process writing to the
PTY — along with malformed payloads, oversized payloads (>128KB),
and payloads using unknown selection letters.
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…tablyai#956)

Wire the two escape sequences that TUIs and modern shells use to tell
the terminal about state that xterm.js doesn't handle natively:

- OSC 7  — shell emits its current working directory on every `cd`.
- OSC 133 — FinalTerm / iTerm semantic prompt marks (A/B/C/D with
  optional exit code on D).

Parsing lives in a new pure `shell-integration.ts` helper with 21 unit
tests covering URI decoding, Windows UNC / drive-letter paths, all four
OSC 133 letters, the `key=value` hint suffix some emitters append, and
the ring-buffer cap on recorded marks.

Wiring follows the DEC 2031 / OSC 52 pattern from stablyai#896 and stablyai#952:

- Handlers registered per-pane in onPaneCreated, disposed symmetrically
  in onPaneClosed.
- Two new refs (`paneCwdRef`, `paneSemanticMarksRef`) live on
  TerminalPane and are passed through UseTerminalPaneLifecycleDeps.
- Semantic marks record the absolute buffer row (`baseY + cursorY`) so
  future block-navigation consumers can scroll to them.
- A 10 000-entry ring buffer caps per-pane memory.

Shipped consumer: the terminal context menu grows a platform-aware
"Reveal in Finder / Show in Explorer / Open Folder" item that opens
the shell's current directory in the native file manager. The item
is hidden when no OSC 7 has been received, consistent with how other
terminal emulators surface shell-integration-dependent features.

No consumer yet for OSC 133 — recording is cheap and the consumer UX
(block-nav keybinds, exit-code gutters) deserves its own design pass.
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