Skip to content

TUI: set DECSET 2031 so an in-place terminal theme change updates the composer colours #38575

Open

Description

@everlof

What variant of Codex are you using?

CLI (codex-cli 0.147.0, macOS arm64)

What feature would you like to see?

Codex reads the terminal's foreground and background once at startup with OSC 10/11 and holds that answer for the process lifetime. The discussion in #18942 is about restoring a re-query on FocusGained. That covers a user switching macOS appearance while Codex sits in a background window, but it cannot cover a terminal that changes its own colours while its window keeps focus, because there is no focus transition to hang the re-query on.

DECSET 2031 is the mechanism for that case:

  • The application subscribes with CSI ? 2031 h, and unsubscribes with CSI ? 2031 l on shutdown.
  • The terminal then sends CSI ? 997 ; 1 n when it becomes dark and CSI ? 997 ; 2 n when it becomes light, whenever its colour scheme changes for any reason.
  • The report is a prompt to re-ask rather than the news itself, so the handler is just "re-issue the OSC 10/11 probe" and reuses whatever probe path the FocusGained fix lands on. The 1/2 payload only says which direction it moved.

This matters for every terminal whose theme is its own rather than the OS's: Ghostty and WezTerm configs, iTerm2 profiles, and any host app with its own palette. Concretely, in Threading, a macOS terminal I maintain that hosts agent CLIs and owns its own themes, a user switching the app theme from a dark one to a light one leaves a running Codex painting a #393939 composer plate with #CFCFCF ink onto a #FFFFFF background, and there is no focus change anywhere in that interaction. Quitting and resuming the session is the only recovery.

Claude Code subscribes to 2031 and re-themes live under the same theme switch. strings on the Codex 0.147.0 binary contains no 2031, so a terminal that emits the report has nothing Codex can hear.

Additional information

Measured against 0.147.0 in a bare PTY (no tmux, no SSH, no emulator), harness and results in #18942: exactly one OSC 10/11 query per process, at startup, and none afterwards under focus events, keyboard input, window resize or SIGWINCH.

2031 originates in Contour's terminal spec and is implemented by Ghostty, WezTerm, foot and others, so the report arrives from real terminals today.

Activity

  1. added
    enhancement
    CLI
    TUI
    on Aug 14, 2026
  2. jdcodes1 commented on Aug 18, 2026

    @jdcodes1

    Implementation notes from the current code, for whoever picks this up:

    The palette is a process-lifetime cache with an attempted flag, filled once by the bounded startup probe (https://github.com/openai/codex/blob/1f41cc5d92/codex-rs/tui/src/terminal_palette.rs#L140-L175). set_default_colors_from_startup_probe already overwrites it, so the "re-probe on 2031 report" handler mostly exists — reset the cache and re-run the OSC 10/11 probe, then trigger a full redraw.

    The real blocker is plumbing: after startup the TUI reads input through crossterm's event stream, and crossterm's parser silently discards CSI sequences it doesn't recognize — CSI ? 997 ; Ps n is one of them. So the report never reaches application code without either a crossterm patch/fork hook or moving the subscription parsing into the raw-input layer the startup probe already uses. That plumbing choice is shared with the FocusGained re-query in #18942, so it's worth solving once for both: any mechanism that can re-run the probe post-startup serves 2031, focus, and a manual /theme refresh fallback alike.

  3. me-on-gh commented on Aug 24, 2026

    @me-on-gh

    Environment

    • Codex CLI 0.149.1
    • macOS Terminal.app
    • tmux 3.7b
    • TERM=tmux-256color

    Problem

    When Terminal.app switches between light and dark profiles while Codex remains
    running, Codex keeps the foreground/background palette cached at startup.

    The terminal and tmux repaint correctly, but the Codex composer retains colors
    from the previous theme and can become effectively unreadable.

    Previously I worked around this by sending CSI I (FocusGained) to marked
    Codex panes. That stopped working after #35649 / commit 976129f, because
    FocusGained now requests a redraw without re-querying OSC 10/11 colors.

    tmux refresh-client, SIGWINCH, resize events, and synthetic focus events
    therefore only redraw the stale cached palette. Restarting Codex corrects it.

    Reproduction

    1. Start Codex using a light Terminal.app profile.
    2. Change Terminal.app to a dark profile without restarting Codex.
    3. Focus away and back, or send CSI I through tmux.
    4. Observe that the composer keeps its light-theme colors.
    5. Restart Codex and observe that the colors become correct.

    Requirement

    A long-running Codex TUI should be able to refresh its cached terminal
    foreground/background colors without restarting and without losing or delaying
    keyboard input.

    Suggested acceptance criteria

    • Palette refresh is bounded and non-blocking.
    • Queued keyboard input is preserved.
    • Palette-derived composer, transcript and syntax colors are recalculated.
    • It works through tmux.
    • When automatic theme-change detection is unavailable, provide an explicit
      trigger such as /refresh-theme, a signal, or a documented control sequence.

    DECSET 2031 may cover supporting terminals, but an explicit refresh mechanism is
    still needed for terminals or multiplexers that do not support or forward it.

  4. cblp0k commented on Aug 25, 2026

    @cblp0k

    Confirming this issue on Linux with Foot, including the current Codex release.

    Environment

    • Codex CLI: 0.149.0, standalone Linux x86_64 build
    • Terminal: Foot 1.27.0
    • TERM=foot
    • COLORTERM=truecolor
    • Session: Hyprland / Wayland
    • OS: Manjaro Linux, kernel 7.2.0--1-MANJARO
    • Direct Foot PTY, without tmux or SSH

    Reproduction

    1. Start Foot with its light color theme active.
    2. Start codex.
    3. Keep the Foot window and Codex TUI focused.
    4. Switch the existing Foot instance to its dark theme at runtime using Foot native two-theme switching (SIGUSR1/SIGUSR2).

    Actual behavior

    Foot changes its background and palette immediately, but the running Codex TUI keeps the old light RGB background in the composer/input area.

    The stale white composer remains after keyboard input, normal redraws, and an explicit SIGWINCH. Restarting only Codex — without restarting Foot — fixes the colors immediately.

    Expected behavior

    Codex should subscribe to Foot private mode 2031 notifications. After receiving a theme-change report, it should:

    1. re-query the terminal defaults through OSC 10/11;
    2. update the cached terminal foreground/background;
    3. invalidate cached TUI styles;
    4. perform a full redraw.

    Foot 1.27 implements private mode 2031, so this appears to be the same missing subscription described in this issue. This reproduction also confirms that the problem is still present in Codex CLI 0.149.0 and affects the light-to-dark direction.

  5. piotrostr commented on Aug 26, 2026

    @piotrostr

    Reference implementation is ready:

    It subscribes to DEC mode 2031, handles color-scheme reports automatically, re-queries OSC 10/11 through an input-preserving probe, replaces the palette cache, re-resolves the adaptive syntax theme, fixes stale composer foreground cells, and redraws. No command or config surface.

    Validated on Ghostty 1.2.3 through tmux 3.6a on macOS. just test -p codex-tui: 3976 passed, 6 skipped, including a PTY regression test for automatic refresh and interleaved typeahead preservation.

    The repository blocks external PR creation, so the Codex change is hosted as a reviewable fork PR for maintainers to cherry-pick or port.

    ~ codex & piotr

  6. fbaltor commented on Aug 27, 2026

    @fbaltor

    Confirming on Linux with a direct Ghostty PTY, without tmux or SSH.

    Environment

    • Codex CLI 0.149.1
    • Ghostty 1.3.1 (GTK, TERM=xterm-ghostty, COLORTERM=truecolor)
    • NixOS 26.05, GNOME/Wayland

    Reproduction

    1. Start Codex while Ghostty uses a light theme.
    2. From another Ghostty tab/session, switch the theme globally. My theme helper writes the selected theme's OSC 10/11/12 and OSC 4 palette sequences directly to every live Ghostty PTY, then reloads Ghostty.
    3. The affected terminal surface changes to the dark palette immediately, while remaining focused.
    4. The running Codex composer/input field remains a bright white block against the dark terminal.
    5. Exit and resume Codex; the composer is immediately correct.

    This is the same stale startup palette described here, but confirms the in-place/no-focus-transition path on Ghostty 1.3.1 under Linux. Normal redraws do not repair it because the terminal defaults have changed while Codex retains palette-derived RGB values.

  7. dkozlov commented on Aug 30, 2026

    @dkozlov

    I can confirm this on Android over SSH with Mobile SSH (https://mobile-ssh.github.io) and codex-cli 0.149.1.

    While debugging it I first found a terminal-side issue: Mobile SSH was swallowing Codex's CSI ? 996 n semantic colour-scheme query. After fixing that, Codex starts with the correct palette in both light and dark themes. An in-place theme change still reproduces this issue:

    1. Start Codex while Mobile SSH is using its dark theme.
    2. Keep the Codex process and terminal pane running and focused.
    3. Change Mobile SSH to its light theme.
    4. The terminal surface becomes light immediately, but Codex keeps its dark-theme composer/status colours, producing low contrast. Restarting Codex repairs it. The reverse transition has the analogous problem.

    Mobile SSH 2.16 now implements the terminal side of this protocol:

    • CSI ? 996 n replies with CSI ? 997 ; 1 n (dark) or CSI ? 997 ; 2 n (light).
    • DECSET 2031 is tracked, including DECRQM state.
    • A live theme change emits the corresponding 997 report, but only after the application has enabled mode 2031.

    With Codex 0.149.1, the terminal never observes CSI ? 2031 h, so it correctly sends no live notification and Codex retains the stale palette.

    This confirms the requested subscription is useful beyond macOS-native terminals: it also affects a terminal whose palette changes locally while Codex is reached through SSH.

  8. fabge commented on Aug 30, 2026

    @fabge

    Confirming this is still reproducible with:

    • Codex CLI: 0.151.0
    • Platform: macOS 27.0
    • Direction: light theme → dark theme

    I switched the terminal theme while a Codex session was running. The terminal background updated immediately, but Codex retained colors from the previous theme.

    This affects more than the composer: the input area remains light, while parts of the transcript, diff preview, and status UI retain stale colors or have very low contrast.

    Expected behavior: all palette-derived TUI colors should be recalculated and the entire interface redrawn after an in-place terminal theme change.

  9. stefanwayon commented on Sep 11, 2026

    @stefanwayon

    Confirming this remains a usability problem in codex-cli 0.154.0. Adding a reproduction, an additional affected component, and a confirmed workaround.

    Reproduction and impact

    1. Start Codex while the terminal follows the system's dark theme, with:
      [tui]
      theme = "ansi"
    2. Leave the Codex session running and switch the system/terminal to its light theme.
    3. The terminal's default/ANSI foreground colours update, but the composer/input box retains its old dark background. The new dark foreground against the stale dark background becomes difficult to read.
    4. The animated/shimmering status text also retains colours from the previous theme.

    This is more than a cosmetic mismatch: it reduces input-text contrast during ordinary automatic or manual system theme switching. Selecting the ANSI syntax theme does not prevent it.

    Confirmed workaround

    Restarting with:

    FORCE_COLOR=1 codex resume --last

    while retaining tui.theme = "ansi" makes the interface substantially more usable. This was tried and confirmed by the affected user.

    Source inspection for the 0.154.0 release explains both symptoms and the workaround:

    • terminal_palette.rs caches the default foreground/background from the startup probe; in ANSI-16 mode, computed colours fall back to Color::Reset.
    • shimmer.rs blends cached foreground/background RGB values in truecolour mode. Its non-truecolour path uses dim/normal/bold styling with the default foreground instead.

    Requested behaviour

    Please subscribe to terminal-issued colour/palette-change notifications and refresh all palette-derived colours, including the composer and animated/shimmer text, without restarting the session. Re-query OSC 10/11 for the actual foreground/background values, update the cache, invalidate affected rendered styles, and redraw while preserving keyboard input.

    iTerm2 is a relevant supported-terminal target: its VT100Terminal implementation handles DEC mode 2031 via sendUnsolicitedDarkModeDSR and emits the 997 dark/light report. This is source verification, not a capture of events from the affected session.

    The protocol specification defines subscribing with CSI ? 2031 h, receiving CSI ? 997 ; 1 n / CSI ? 997 ; 2 n, and unsubscribing with CSI ? 2031 l. Notifications should prompt a palette refresh even when focus has not changed.

    Please include a regression check that switches themes during the shimmer animation and checks composer foreground/background contrast. A documented whole-TUI ANSI/default-colour option would also be useful as a fallback; the current syntax-theme setting alone does not provide this.

    Related bug: #18942.

  10. NorthIsUp commented on Oct 17, 2026

    @NorthIsUp

    Still reproducible in codex-cli 0.163.0 (macOS arm64), in tty7, a terminal that supports DECSET 2031 and sends CSI ? 997 ; 1|2 n on every light/dark change.

    The 0.163.0 binary contains the OSC 10/11 probe strings and ?1004h, but nothing for ?2031 or ?997, so Codex never subscribes. After a dark → light flip, plain default-colour text follows the terminal, but the composer and user-message backgrounds stay dark: they were derived from the startup OSC 11 answer. Restarting (codex resume --last) is the only workaround.

    The current focus-in handler (codex-rs/tui/src/tui/event_stream.rs) deliberately keeps the startup palette, so neither path re-probes today. Subscribing to 2031 and re-running the OSC 10/11 probe when a 997 report arrives would cover terminals like this one, as described above.

  11. NorthIsUp commented on Oct 17, 2026

    @NorthIsUp

    Additional current reproduction on codex-cli 0.163.0 with tty7 26.10.0, macOS 26.7.1 (25G241), arm64. CLI/OS versions and TERM_PROGRAM=tty7 / TERM_PROGRAM_VERSION=26.10.0 were checked locally. The reproduction below is the user's observed session, not an automated replay.

    1. Run Codex in tty7 while using the dark appearance.
    2. Change the macOS system appearance to light while that Codex session remains running.
    3. The terminal/TUI surroundings change to a light cream background, but the composer keeps its dark background. Composer text is also dark, leaving the input difficult to read.

    Expected: the running TUI should refresh all palette-dependent backgrounds and foregrounds together after the terminal follows the system theme, without requiring a restart or losing the draft/input.

    The user supplied a screenshot of this occurrence. It is intentionally not uploaded because it includes private project paths and session information; the relevant visual symptom is described above. No private logs or project files are attached.

    This appears to be the same runtime palette-refresh problem tracked here and in #45163. This follow-up confirms the system-theme-change/readability symptom on the versions above; it does not independently establish which terminal notification or cached-style path is responsible.

  12. nfarina commented on Nov 3, 2026

    @nfarina

    Confirming this on codex-cli 0.166.0 (macOS arm64). Codex sends OSC 10/OSC 11 once at startup and never again. I drove it in a pty and sent a focus-out/focus-in pair (it enables ?1004h), an unsolicited CSI ? 997 ; 2 n, and a resize. None of them led to another color query, so the composer keeps its startup background.

    For a terminal that already supports this: Miks (https://github.com/nfarina/miks), a macOS terminal for running coding agents, sends CSI ? 997 ; 1|2 n on every light/dark change once a program sets mode 2031. It also answers CSI ? 996 n. Claude Code switches its theme live through it, so Codex subscribing with CSI ? 2031 h and querying OSC 10/11 again on each report would be all it takes.

    Written by Claude Opus 5.5 at my request. Feel free to delete it if it isn't useful.

  13. gielcobben commented on Nov 19, 2026

    @gielcobben

    One thing from the terminal side that I don't see in this thread yet, because it affects how the handler has to be written.

    A terminal can send more than one report per change, and the first can name the old theme.

    I make Wortel, a macOS terminal built on libghostty. Logging the bytes each way with Claude Code running, one switch from light to dark produced this, all within 20 ms (T is the terminal, P the program):

    T> CSI ? 997 ; 2 n                    light: the theme being left
    P> OSC 11 ; ?
    T> OSC 11 ; rgb:ffff/ffff/ffff        still the old background
    T> CSI ? 997 ; 1 n
    T> CSI ? 997 ; 1 n
    P> OSC 11 ; ?
    T> OSC 11 ; rgb:0d0d/1111/1717
    

    Stock Ghostty on macOS does the same: ghostty-org/ghostty#15116. I have since removed the stale first report in Wortel, but two reports per change remain there, and Ghostty still sends three.

    So, for whoever implements this:

    • Coalesce. Reports arrive in a burst, and one query after the last of them is enough. neovim does this with a timer (bg_query_timer in src/nvim/tui/input.c).
    • Take the result from the last OSC 10/11 answer, so a stale early answer cannot win.
    • Never act on the report's own value.

    A handler that re-themes on the first report and ignores the rest would land on the wrong theme about as often as Codex does today.

    What it looks like now. Measured with the Codex bundled in the desktop app (0.142.3): OSC 10 and OSC 11 once at startup, nothing after. main on 2026-11-19 still keeps the startup cache (tui/event_stream.rs, "Keep the startup-cached palette"). After a switch to dark, the composer is a near-white band with near-white text in it, a contrast of about 1.07, so what you type cannot be read. Claude Code follows the same switch live in the same terminal.

    To keep Codex usable I now force a minimum contrast in the terminal so the text is at least legible, and I plan to restart idle Codex processes on an appearance change (codex resume <id>). Both are workarounds for one subscription and one re-query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    CLITUIenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions