Skip to content

fix(terminal): send one mode-2031 color-scheme report per theme flip - #23420

Closed
kargnas wants to merge 2 commits into
stablyai:mainfrom
kargnas:fix/terminal-mode-2031-duplicate-report
Closed

kargnas wants to merge 2 commits into
stablyai:mainfrom
kargnas:fix/terminal-mode-2031-duplicate-report

Conversation

@kargnas

@kargnas kargnas commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

Some terminal programs, such as Claude Code and Neovim, ask the terminal to tell them when it switches between light and dark mode, so they can switch their own colors. When you flipped Orca between light and dark, a terminal you were looking at told those programs twice. Sometimes the two messages even disagreed: one said "dark" and the other said "light". After this change, Orca sends one message per flip.

What Changed

Before: a program turns on these notifications by sending DEC private mode 2031 (CSI ?2031h). In a visible terminal pane, one Orca light/dark flip then sent the program two CSI ?997;Xn reports (1 = dark, 2 = light):

  1. xterm.js sent one by itself. Writing terminal.options.theme makes xterm's theme service fire onChangeColors, and while xterm's own colorSchemeUpdates flag is on, xterm replies with _reportColorScheme(). That report is based on how bright the new palette is, and xterm sends it on every palette write.
  2. Orca then sent its own report from maybePushMode2031Flip. That one follows the app's light/dark mode and goes out only when the mode actually flips. Orca already sends this report for hidden panes, where xterm never sees the program's ?2031h.

After: one report per flip, from Orca.

Mechanism:

  • applyTerminalAppearance now calls maybePushMode2031Flip before it writes the theme instead of after. The move changes nothing the program receives, because each theme write now produces at most one report (Orca's or xterm's), so there is no order between two reports left to change.
  • Orca's report now has its own gate: a connected transport and no phone fit override (mobile-fit). Before, it shared the PTY resize gate, which also skips a desktop whose pane is parked at another desktop's grid (remote-desktop-fit). That shared gate dates from feat(mobile): Expo companion app [beta] #1245, which put the report inside the resize block; the comment there covers only the resize. The resize gate itself is unchanged.
  • If that call just sent a report, the theme is written through the new setThemeWithoutXtermColorSchemeReport (in terminal-mode-2031-replies.ts). It turns xterm's colorSchemeUpdates flag off only while options.theme is written, then restores it in a finally. In every other case the theme is written exactly as before, and xterm reports as before.
  • xterm has no public switch for this flag, so the helper reaches xterm's private _core.coreService.decPrivateModes with in checks instead of a type cast. If a future xterm moves that field, the helper does nothing and the old duplicate comes back; nothing else breaks.
  • applyTerminalAppearance now accepts a Pick<PaneManager, ...> of the four methods it calls, so the new test can pass a typed fake instead of a cast. Existing callers are unchanged.

Behavior changes a reviewer should know about:

  • If the app mode and the terminal palette disagree (for example, light app mode with a dark terminal theme), a flip now sends only the app-mode report. Before, xterm's palette report arrived first and Orca's app-mode report arrived second, so the program ends up in the same state as before.
  • On a desktop parked at another desktop's grid (remote-desktop-fit), a flip is now reported by Orca instead of by xterm. Before, Orca's record of the last reported mode stayed stale there, so after this desktop took the grid back, the next appearance change (a font size change, for example) sent the same flip a second time. CodeRabbit found this on an earlier revision of this PR.
  • Nothing else changes. xterm still reports palette writes that do not flip the app mode, including a palette that changes between light and dark while the app mode stays the same (for example, choosing a light terminal theme for dark mode). Under a phone's fit override, Orca's report stays gated off and xterm's report is handled as before.

Why

xterm's report and Orca's report exist for different panes: xterm only sees programs in visible panes, and Orca covers hidden panes too. On a visible pane they overlap exactly when Orca reports a flip and also writes a new theme, so that is the only write this change mutes.

Alternatives considered:

  • vtExtensions.colorSchemeQuery: false. This stops xterm's report, but it also stops xterm from answering CSI ?996n and from tracking ?2031h at all. For hidden terminals, src/main/daemon/terminal-view-attribute-responder.ts answers ?996n the same way a visible xterm does. Turning xterm's answer off would make visible and hidden terminals answer differently.
  • Keep xterm's report and drop Orca's for visible panes. xterm follows palette brightness instead of the app mode, and hidden panes would still need Orca's report, so the result would depend on whether a pane is visible.
  • Mute xterm on every theme write Orca makes. This also removes the duplicate, but it silences xterm when the palette changes between light and dark while the app mode stays the same. In that case Orca sends nothing, so the program would never hear about the change.
  • Mute xterm only for a write whose flip Orca just reported (chosen). This removes exactly the duplicate and leaves every other xterm report alone.

Linked Issue

Fixes #25498

Two issues are related, but this PR does not fix either one: #10913, where hidden main-window tabs get no report after a system appearance change, and #9156, where the host and a paired desktop both answer the same query.

Visual Proof

N/A. This change is not visible. It removes one duplicate control sequence sent to the program, and the terminal's colors change exactly as before. No attachments are included.

Testing

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

The new terminal-appearance-color-scheme.test.ts runs a real xterm.js Terminal (under happy-dom) through applyTerminalAppearance. xterm's own replies pass through the same mobile-lock check that pty-input-forward.ts uses. It has 6 cases:

  • reports each light/dark flip exactly once
  • reports the app mode once when the terminal palette has the opposite mode (xterm still answers ?996n from the palette)
  • still reports a palette flip that keeps the app mode (xterm's report)
  • leaves the report to a mobile driver (mobile lock and mobile fit override)
  • reports to a desktop-driven PTY parked at another grid without resizing it, and does not repeat the flip once that desktop owns the grid again
  • keeps unrelated appearance changes silent, and after a muted theme write a program's own OSC 11 change still gets xterm's report

Results:

  • Unchanged main (27b823f): 3 of 6 fail. The flip case receives ?997;2n twice. The mismatch case receives ?997;1n followed by ?997;2n. The parked-grid case receives ?997;2n twice: xterm's report during the flip, then Orca's after the desktop takes the grid back.
  • Muting every theme write: 1 of 6 fails. The palette-flip case receives nothing.
  • Leaving xterm's flag off after the write (the finally restore removed): 1 of 6 fails. The OSC 11 case receives nothing.
  • Gating Orca's report on the resize gate (the previous revision of this PR): 1 of 6 fails. The parked-grid case receives ?997;2n twice.
  • This PR: all 6 pass.
  • pnpm test src/renderer/src/components/terminal-pane: 486 files, 4673 passed, 1 skipped. pnpm tc:web, oxlint, oxfmt --check, and pnpm run check:code-quality:changed pass.

What I did not verify: I did not run the Electron app or watch a real program such as Claude Code or Neovim receive the reports. The tests use xterm.js in happy-dom with a fake PTY transport. They ran on macOS only. I did not test on Linux, Windows, or over SSH.

AI Disclosure

Developed with Claude Code (Claude Opus 5.5).

Review

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • Cross-platform: renderer-only. The change touches no file paths, keyboard shortcuts, or processes, so it behaves the same on macOS, Linux, and Windows.
  • SSH / remote: the report still goes through the pane's own transport, so for SSH panes and paired remote-runtime panes the remote program now gets one report instead of two. The bytes are unchanged (CSI ?997;1n / CSI ?997;2n), so no RPC or stream format changes, and a new client with an older host (or the reverse) works the same way.
  • Mobile: unchanged. A phone's fit override still gates off Orca's report, and the mobile input lock on xterm's replies works as before.
  • Not handled (same as main): if a phone's fit override is ever held while the phone is not driving the PTY, xterm's report reaches the program and Orca's record of the last mode stays stale, so a later appearance change can repeat that flip. Closing it means gating Orca's report on the mobile input lock instead, which also changes when a flip made during the lock is reported. That belongs in its own PR.
  • Private xterm field: the fix depends on _core.coreService.decPrivateModes.colorSchemeUpdates (present in @xterm/xterm 6.1.0-beta.303). If an xterm upgrade removes it, the duplicate returns; nothing else breaks.
  • Performance: one extra property read and two writes per pane, only when Orca reports a flip and the theme changes.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred). I ran pnpm tc:web, the terminal-pane tests, and the changed-file lint and format gates; I left full lint, typecheck, test, and build to CI.

A program that enabled DEC mode 2031 received two CSI ?997 reports for
each Orca light/dark flip on a visible pane: xterm reported the palette
write itself, then Orca's maybePushMode2031Flip reported the app mode.

Orca now sends its report before writing the theme and mutes xterm's
copy only for a write whose flip Orca just reported. Every other xterm
report is unchanged, including a palette light/dark change that keeps
the app mode and the case where Orca's report is gated off.
@coderabbitai

coderabbitai Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

📝 Walkthrough

Walkthrough

applyTerminalAppearance now coordinates mode-2031 reports with theme changes for connected PTYs that do not use a mobile fit override. A new helper temporarily disables xterm color-scheme reports during theme assignment and restores the prior setting. Added tests cover app-mode and palette reports, mobile and desktop fit behavior, unrelated appearance changes, and program-driven palette changes.

Priority: ⬇️ Low

Merge Risk: 🔵 Low · up to c2fb2

The change appears mergeable, but the mobile-owned PTY test should cover the real input path so a future duplicate report cannot pass unnoticed.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 71.43% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely describes the main change: sending one mode-2031 color-scheme report per theme flip.
Description check ✅ Passed The description covers the required sections with clear change details, rationale, linked issue, visual-proof rationale, testing results, AI disclosure, and compatibility notes. It also states which c…
  • Fix all pre-merge checks with AI

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 9904d20b-31c0-41c6-8f42-87ccf6e691ee

📥 Commits

Reviewing files that changed from the base of the PR and between 27b823f and aafed10.

📒 Files selected for processing (3)
  • src/renderer/src/components/terminal-pane/terminal-appearance-color-scheme.test.ts
  • src/renderer/src/components/terminal-pane/terminal-appearance.ts
  • src/renderer/src/components/terminal-pane/terminal-mode-2031-replies.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review.

@pullfrog pullfrog Bot 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.

✅ No new issues found.

Reviewed changes

  • One report per flip: applyTerminalAppearance now calls maybePushMode2031Flip before the options.theme write and, when it reported a flip, routes the write through setThemeWithoutXtermColorSchemeReport, which mutes xterm's private colorSchemeUpdates flag only for that write and restores it in finally.
  • Reused gate: the report/safeFitAndThen predicate was hoisted into a single desktopSizesPty const, so the two paths can no longer drift apart.
  • Narrowed manager type: applyTerminalAppearance now takes a Pick<PaneManager, ...> of the four methods it actually uses, letting the new test pass a typed fake instead of a cast.
  • New xterm-backed test: terminal-appearance-color-scheme.test.ts drives a real xterm under happy-dom and pins exact CSI ?997 reply sequences across six scenarios.

I read the complete diff, the full applyTerminalAppearance/maybePushMode2031Flip sources, the mobile-fit-overrides and hidden-subscription state machines, and the installed xterm dist. I verified the new test is real coverage rather than theatre: with the mute removed it fails 2/6 exactly as the PR describes. Related terminal-pane suites (79 tests), pnpm run tc:web, and pnpm run check:code-quality:changed all pass. The only residual risk is the dependency on xterm's private _core.coreService.decPrivateModes.colorSchemeUpdates, which the author documents and which the new test would flag on an xterm bump that moves the field.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

…te grid

Orca's mode-2031 report shared the PTY resize gate, so it was skipped
under a remote-desktop-fit override too. There xterm reported the flip
instead and Orca's last-reported mode stayed stale, so the next
appearance change after the desktop took the grid back repeated it.
The report now has its own gate that skips only a phone's mobile-fit.

Constraint: a phone driving the PTY answers color-scheme queries for itself
Rejected: gate on the mobile input lock | changes when a flip made during the lock is reported; separate PR
Tested: terminal-appearance-color-scheme.test.ts fails on the old gate with two ?997;2n and passes here
Tested: pnpm test src/renderer/src/components/terminal-pane, pnpm tc:web, check:code-quality:changed
Not-tested: a real remote-desktop-fit session in the Electron app

@pullfrog pullfrog Bot 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.

✅ No new issues found.

Reviewed changes

  • Report gate split from the resize gate: applyTerminalAppearance now derives reportsColorScheme (connected && fitOverride?.mode !== 'mobile-fit') separately from desktopSizesPty (connected && !fitOverride), so a desktop parked at another desktop's grid reports the flip itself instead of staying silent.
  • Parked-grid repeat fixed: with remote-desktop-fit now reporting, paneLastThemeMode advances, so the next appearance pass after this desktop regains the grid no longer re-sends the same flip.
  • Test extended: the parked-grid case re-applies after desktop-fit and asserts the reply list stays [LIGHT], an assertion that fails against the previous gate.

I read the full diff, the new commit's delta, plus mobile-fit-overrides.ts, pty-input-forward.ts, and the installed-xterm reply path. Verified the report now fires on remote-desktop-fit while the resize stays suppressed, and that pnpm tc:web, both appearance suites (36 tests), and pnpm run check:code-quality:changed pass. The cross-instance duplicate (an owner desktop and a parked viewer both reporting the same PTY) is unchanged in count versus main and remains the separate, already-tracked concern #9156.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (1)
src/renderer/src/components/terminal-pane/terminal-appearance-color-scheme.test.ts (1)

163-169: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Exercise the production input-forwarding boundary.

This handler duplicates the isPtyLocked check instead of using forwardPtyInput. The test can therefore pass if production forwarding stops dropping xterm's mode-2031 reply for a mobile-owned PTY. Route xterm's onData event through forwardPtyInput and assert that the production transport receives no reply.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 856c6583-1d7c-4e94-a7cb-fe148f19fb81

📥 Commits

Reviewing files that changed from the base of the PR and between aafed10 and c2fb2b2.

📒 Files selected for processing (2)
  • src/renderer/src/components/terminal-pane/terminal-appearance-color-scheme.test.ts
  • src/renderer/src/components/terminal-pane/terminal-appearance.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/renderer/src/components/terminal-pane/terminal-appearance-color-scheme.test.ts
  • src/renderer/src/components/terminal-pane/terminal-appearance.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

@kargnas

kargnas commented Sep 27, 2026

Copy link
Copy Markdown
Contributor Author

About the CodeRabbit nitpick on the mirrored lock check in terminal-appearance-color-scheme.test.ts: the production drop of xterm replies under the mobile lock is already pinned through the real pane session by pty-connection-terminal-input-gating.test.ts ("drops xterm protocol replies from live TUI output while mobile presence lock is active"). This suite keeps the one-line mirror so it can drive applyTerminalAppearance with a real xterm without building a whole pane session, and I left it as is.

@kargnas

kargnas commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

Hi @nwparker, since you've worked on the mode 2031 handling (#13904): on a visible pane, xterm's own palette-based ?997 report and maybePushMode2031Flip both fire for one light/dark switch, so the program gets two reports, and they can disagree. This mutes xterm's report only for the theme write whose flip Orca just reported, and leaves every other report alone. It's 3 files and fixes #25498. Could you take a look when you have time?

@nwparker

nwparker commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Thank you @kargnas for tracking this down so clearly. Your write-up here and in #25498 pinned the duplicate ?997 report exactly. We landed a broader fix in #37163: Orca is now the single sender of mode-2031 reports (xterm's built-in reporting is off), and terminals also catch up after a remote reconnect. That's why this one was closed. It's credited in #37163. Really appreciate the careful work!

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.

[Bug]: One light/dark switch sends two mode 2031 color-scheme reports to the terminal program

2 participants