feat(cli): auto-detect terminal theme ('auto' or unset) (#3460)

* feat(cli): add terminal theme auto-detection when ui.theme is 'auto'

Detect terminal dark/light preference at startup using macOS system
appearance (AppleInterfaceStyle) and COLORFGBG env variable fallback,
then resolve to Qwen Dark or Qwen Light accordingly. Adds 'Auto' option
to the /theme dialog.

Closes #2998

* fix: address audit issues in terminal theme detection

- Fix ThemeDialog preview: use getActiveTheme() when 'auto' is
  highlighted so the preview shows the actual detected theme instead
  of always falling back to Qwen Dark.
- Swap detection order: check COLORFGBG (terminal-specific) before
  macOS system appearance (system-wide) since the terminal may use a
  different theme than the OS.
- Fix core/theme.test.ts mock to export AUTO_THEME_NAME and add test
  case verifying 'auto' bypasses validation.

* feat(cli): add OSC 11 background color query for theme detection

Send ESC]11;?BEL to the terminal at startup to read the actual
background RGB value, then decide dark/light via ITU-R BT.709
luminance. This is the most universal detection method and covers
Linux terminals (GNOME Terminal, Windows Terminal, etc.) that do
not set COLORFGBG.

Async detection (OSC 11 → COLORFGBG → macOS → dark) is used at
startup; the sync path (COLORFGBG → macOS → dark) remains for the
/theme dialog live-preview to avoid ~200ms latency per highlight.

* fix: optimize async detection order and improve comments

- Check COLORFGBG first in the async path to avoid a 200ms OSC 11
  timeout on terminals that already set COLORFGBG but lack OSC 11.
- Fix misleading comment about stdin flowing mode vs raw mode.

* fix(cli): defer auto theme detection past sandbox entry

- Move resolveAutoThemeAsync() to after the sandbox-check gate so the
  ~200ms OSC 11 probe does not block a process that is about to exec
  into the sandbox child (which reruns the same detection).
- Register missing i18n keys 'Auto (detect terminal theme)' and 'Auto'
  across all 7 locales; previously non-English users fell back to the
  English keys.
- Simplify resolveAutoThemeAsync to return Promise<void> (the caller
  never checked the previous always-true boolean).

* feat(cli): auto-detect theme when ui.theme is unset

An unset ui.theme now behaves the same as 'auto' — the async OSC 11 /
COLORFGBG / macOS probe runs at startup and resolves to Qwen Dark or
Qwen Light. Fresh installs no longer hard-code Qwen Dark.

The /theme dialog also highlights the "Auto" row when ui.theme is
undefined, so the selection reflects the effective resolution.

* fix(cli): do not run OSC 11 probe when ui.theme is unset

Fresh startups were showing kitty-protocol response bytes
(e.g. [?0u[?62c) inside the input box. The OSC 11 probe added for the
unset-theme path flips stdin raw mode and pauses the stream, and that
state dance interleaves with kitty protocol detection on some
terminals so the kitty responses leak past the early-input-capture
filter and land in the TUI input.

Fall back to the synchronous detector (COLORFGBG + macOS) when the
user has no theme configured. Explicit 'auto' still runs the OSC 11
probe since the user has opted in.

* fix(cli): run OSC 11 probe inside the early-capture window

Previous fix restricted the OSC 11 probe to explicit 'auto', leaving
fresh installs without terminal detection — not acceptable. The real
problem was that the probe managed its own stdin raw mode and pause
cycle before early input capture was attached, so kitty protocol
response bytes arriving during the gap slipped past the filter and
landed in the TUI input.

- Make detectOsc11Theme stdin-state-agnostic: it no longer flips raw
  mode or pauses the stream; it just attaches a listener, sends the
  query, and removes the listener on response or timeout.
- Defer the async probe in gemini.tsx until after startEarlyInputCapture
  (and kitty detection kickoff) inside the interactive block. The
  existing filter in startEarlyInputCapture absorbs the OSC 11 response
  bytes alongside our handler, so nothing can leak into the TUI input.
- Both unset theme and explicit 'auto' now run the async probe.

* fix(cli): sync theme baseline for non-interactive and pre-render UI

The previous refactor only resolved 'auto'/unset themes inside the
interactive startup block. That dropped detection for non-interactive
runs and left any pre-render UI (the --resume session picker) drawing
with the default Qwen Dark palette even on light terminals.

Set a synchronous baseline (COLORFGBG + macOS) right after loading
custom themes so the theme is already correct when those paths run;
the interactive block still refines with an OSC 11 probe when possible.

* fix(cli): cache async auto-detect so /theme Auto stays consistent

/theme's live preview calls setActiveTheme('auto'), which runs the
synchronous detector (COLORFGBG + macOS only). On terminals whose
light/dark state is only visible to OSC 11 (e.g. GNOME Terminal), the
sync path disagrees with the async probe done at startup — so picking
Auto once showed the correct preview, but switching away and picking
Auto again flipped the preview to the wrong theme.

Cache the result from resolveAutoThemeAsync and prefer it in the sync
path; fall back to live sync detection only when no async result is
known yet. Added a unit test that locks the regression down.

* fix(theme): don't pin macOS detection to Light on generic exec failure

detectMacOSTheme previously treated every `defaults read -g
AppleInterfaceStyle` failure as Light Mode. Only the "key does not
exist" error actually indicates Light — timeouts, missing `defaults`,
ENOENT, SIGTERM, etc. are inconclusive and should fall through so the
caller can continue its fallback chain instead of locking to Light.

Match the "does not exist" marker in the error's stderr or message;
return undefined otherwise. Adds tests for the timeout, ENOENT and
stderr-only paths.

* perf(cli): overlap OSC 11 theme probe with startup work

resolveAutoThemeAsync was awaited on the critical path, so an unset or
'auto' ui.theme paid the full OSC 11 timeout (~200 ms) plus the
synchronous macOS defaults read before the first paint. The synchronous
baseline picked earlier already keeps the theme valid for the
non-interactive paths and the pre-render UI, so this await was the only
thing forcing render to wait on the probe.

Kick the probe off without awaiting alongside detectAndEnableKittyProtocol
and drain the resulting promise just before startInteractiveUI. The OSC
11 timeout now overlaps with initializeApp and the warnings collection,
the early-capture filter is still active when the response arrives (so
no terminal bytes leak into the TUI), and the refined theme is in place
by the time the first frame renders.

* test(cli): cover OSC 11 probe listener lifecycle

Adds regression tests for the listener-leak path that motivated three
mid-PR fixes (OSC 11 bytes bleeding into the input box):

- happy-path resolves 'dark' from a simulated terminal response and
  asserts the data listener is removed
- timeout path resolves undefined and likewise restores the listener
  count to baseline
- multi-chunk path reassembles a response split across two data events

Also resets the module-level `cachedAutoDetection` singleton in the
theme-manager beforeEach so the async detection cache cannot leak
across tests and make ordering load-bearing.
This commit is contained in:
Edenman 2026-04-22 16:58:45 +08:00 committed by GitHub
parent 685296e978
commit 58cdf101ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 875 additions and 32 deletions

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { themeManager } from '../ui/themes/theme-manager.js';
import { themeManager, AUTO_THEME_NAME } from '../ui/themes/theme-manager.js';
import { type LoadedSettings } from '../config/settings.js';
import { t } from '../i18n/index.js';
@ -15,7 +15,11 @@ import { t } from '../i18n/index.js';
*/
export function validateTheme(settings: LoadedSettings): string | null {
const effectiveTheme = settings.merged.ui?.theme;
if (effectiveTheme && !themeManager.findThemeByName(effectiveTheme)) {
if (
effectiveTheme &&
effectiveTheme !== AUTO_THEME_NAME &&
!themeManager.findThemeByName(effectiveTheme)
) {
return t('Theme "{{themeName}}" not found.', {
themeName: effectiveTheme,
});