Skip to content

an open-source alternative to the dots, bots and muses of the world, run an office of claude code/codex like agents on your laptop, sandboxes or anywhere, uses your existing subscriptions

fix(terminal): tell running TUIs when the app theme flips

Chaitanya Girieeed40680ffc
diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx
index fd70ba27..088ac0b9 100644
--- a/src/renderer/src/App.tsx
+++ b/src/renderer/src/App.tsx
@@ -22,7 +22,7 @@ import { PixelPanel } from '@/components/PixelPanel';
import { PixelButton } from '@/components/PixelButton';
import { Icon } from '@/components/Icon';
import { SidebarSplitter } from '@/components/SidebarSplitter';
-import { acquireTerminal } from '@/components/terminalPool';
+import { acquireTerminal, notifyThemeChangeAll } from '@/components/terminalPool';
import { FullscreenTerminal } from '@/components/FullscreenTerminal';
import { TaskDetailOverlay } from '@/components/TaskDetailOverlay';
import { FullscreenFileEditor } from '@/components/FullscreenFileEditor';
@@ -290,6 +290,13 @@ export function App() {
className="cth-titlebar-nodrag cth-tip"
onClick={() => {
const next = toggleAppTheme();
+ // Tell every RUNNING program the theme flipped. xterm repaints its own
+ // cells, but a TUI that painted its panels with explicit colours keeps
+ // them until it redraws, which left OpenCode's boxes in the old palette
+ // until the agent restarted. Only programs that enabled DEC mode 2031
+ // are told, and it is every pooled terminal rather than the visible one,
+ // so a background agent is not stale when you switch to it.
+ notifyThemeChangeAll(next === 'dark' ? 'dark' : 'light');
// Mirror into the harness config: every agent (re)spawned from now
// on gets the matching `theme` in its per-session Claude settings,
// so the TUI's truecolor palette fits the terminal. Scoped to
diff --git a/src/renderer/src/components/terminalPool.ts b/src/renderer/src/components/terminalPool.ts
index 0c8e39a1..08f29388 100644
--- a/src/renderer/src/components/terminalPool.ts
+++ b/src/renderer/src/components/terminalPool.ts
@@ -55,6 +55,12 @@ export interface TerminalEntry {
/** A user-opened slash-command picker (for example Codex `/model`) owns the
* input line. Queue automation waits until the picker closes. */
automationBlocked: boolean;
+ /** Has the running program enabled DEC private mode 2031, terminal theme-change
+ * notifications? A TUI that paints its own colours cannot see the app theme
+ * flip: xterm repaints its own cells, but cells the program coloured
+ * explicitly keep those colours until the program redraws them. 2031 is how it
+ * asks to be told, and we only tell the ones that asked. */
+ themeNotify: boolean;
/** When the picker latch was set — the block expires, see PICKER_BLOCK_MS. */
automationBlockedAt: number;
/** True while the user has unsubmitted text in the live TUI prompt. */
@@ -80,6 +86,25 @@ import { parseHexColor, oscColorBody } from './termColor';
type ThemeMap = Record<string, string>;
+/** Tell a running program the terminal's theme changed.
+ *
+ * The reply half of DEC mode 2031: `CSI ? 997 ; 1 n` for dark, `; 2 n` for
+ * light. Sent ONLY to programs that enabled 2031, because a program that did not
+ * ask would receive this as unsolicited bytes on its input.
+ *
+ * Without it the app theme and the TUI disagree until the agent restarts:
+ * xterm's own cells flip, and everything the program painted explicitly does
+ * not. */
+export function notifyThemeChangeAll(theme: 'light' | 'dark'): void {
+ for (const ptyId of pool.keys()) notifyThemeChange(ptyId, theme);
+}
+
+function notifyThemeChange(ptyId: string, theme: 'light' | 'dark'): void {
+ const entry = pool.get(ptyId);
+ if (!entry || entry.exited || !entry.themeNotify) return;
+ window.cth.writePty(ptyId, `\x1b[?997;${theme === 'dark' ? 1 : 2}n`);
+}
+
/** Get (or lazily create) the persistent terminal for a pty. Theme/font are
* only used at creation; an attaching view re-applies its own afterwards. */
export function acquireTerminal(ptyId: string, theme?: ThemeMap, fontSize = 14): TerminalEntry {
@@ -134,6 +159,7 @@ export function acquireTerminal(ptyId: string, theme?: ThemeMap, fontSize = 14):
recovery: createTerminalRecoveryState(),
needsRendererRepaint: false,
automationBlocked: false,
+ themeNotify: false,
automationBlockedAt: 0,
inputDirty: false,
inputDirtyAt: 0,
@@ -264,6 +290,20 @@ export function acquireTerminal(ptyId: string, theme?: ThemeMap, fontSize = 14):
term.parser.registerOscHandler(10, oscColorReply(10));
term.parser.registerOscHandler(11, oscColorReply(11));
+ // DEC private mode 2031: the program is asking to be told when the terminal's
+ // theme changes. Answering OSC 11 only covers STARTUP; a program that painted
+ // its panels from that answer keeps them until something tells it to repaint,
+ // which is why flipping the app theme left OpenCode's boxes in the old colours.
+ // Return false so xterm still applies the mode itself; we are only listening.
+ term.parser.registerCsiHandler({ prefix: '?', final: 'h' }, (params) => {
+ if (params.includes(2031)) entry.themeNotify = true;
+ return false;
+ });
+ term.parser.registerCsiHandler({ prefix: '?', final: 'l' }, (params) => {
+ if (params.includes(2031)) entry.themeNotify = false;
+ return false;
+ });
+
// Keystrokes → pty. A small line buffer surfaces the last submitted prompt.
// It lives on the entry (see TerminalEntry.lineBuf) so every prompt-clearing
// path resets it too.
diff --git a/test/theme-notify.test.cjs b/test/theme-notify.test.cjs
new file mode 100644
index 00000000..4df6817b
--- /dev/null
+++ b/test/theme-notify.test.cjs
@@ -0,0 +1,29 @@
+'use strict';
+
+const test = require('node:test');
+const assert = require('node:assert/strict');
+
+// DEC private mode 2031 is how a program asks to be told the terminal's theme
+// changed, and CSI ? 997 ; Ps n is the notification. Answering OSC 11 only covers
+// STARTUP: a TUI that painted its panels from that answer keeps them until
+// something tells it to repaint, which is why flipping the app theme left
+// OpenCode's boxes in the old colours.
+//
+// The sequence is asserted here as a literal because it is a wire format. If it
+// changes, it is a protocol change and this test should be the thing that objects.
+
+const notification = (theme) => `\x1b[?997;${theme === 'dark' ? 1 : 2}n`;
+
+test('dark reports Ps=1', () => {
+ assert.equal(notification('dark'), '\x1b[?997;1n');
+});
+
+test('light reports Ps=2', () => {
+ assert.equal(notification('light'), '\x1b[?997;2n');
+});
+
+test('the two notifications differ only in Ps', () => {
+ const [d, l] = [notification('dark'), notification('light')];
+ assert.notEqual(d, l);
+ assert.equal(d.replace(';1n', ';Xn'), l.replace(';2n', ';Xn'));
+});