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
HTML agent-harnessagent-orchestrationagentsai-agentsautonomous-agentsclaude-codecodexcoding-agentsdesktop-appdeveloper-toolselectrongemini-cliharnessharness-engineeringlocal-firstmemorymulti-agentopencodeorchestrationtypescript
fix(terminal): answer the OSC colour queries so TUIs can match the window
Chaitanya Giri4e9fa2bb6fdc
diff --git a/src/renderer/src/components/termColor.ts b/src/renderer/src/components/termColor.tsnew file mode 100644index 00000000..9105c045--- /dev/null+++ b/src/renderer/src/components/termColor.ts@@ -0,0 +1,31 @@+/**+ * Colour helpers for the terminal's OSC 10/11 replies.+ *+ * Free of xterm and of every browser global on purpose, so the parsing rules are+ * unit-testable. `terminalPool.ts` itself cannot be loaded in a test: it imports+ * xterm, which touches `self`. Same reasoning as `hooks/queueDelivery.ts` and+ * `store/focusMode.ts`.+ */++/**+ * `#rgb` or `#rrggbb` to [r, g, b], or null for anything else.+ *+ * Null means "stay silent". Answering an OSC colour query with a guess is worse+ * than not answering at all, because the TUI then styles itself confidently+ * wrong, which is the exact failure this code exists to fix.+ */+export function parseHexColor(hex: string): [number, number, number] | null {+ const m = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(hex.trim());+ if (!m) return null;+ const h = m[1].length === 3 ? m[1].split('').map((c) => c + c).join('') : m[1];+ return [parseInt(h.slice(0, 2), 16), parseInt(h.slice(2, 4), 16), parseInt(h.slice(4, 6), 16)];+}++/**+ * The body of an OSC 10/11 reply for a colour, in xterm's 16-bit-per-channel+ * form. Doubling each byte is the conventional widening, so 0x1a becomes 0x1a1a.+ */+export function oscColorBody(rgb: [number, number, number]): string {+ const wide = (v: number) => v.toString(16).padStart(2, '0').repeat(2);+ return `rgb:${wide(rgb[0])}/${wide(rgb[1])}/${wide(rgb[2])}`;+}diff --git a/src/renderer/src/components/terminalPool.ts b/src/renderer/src/components/terminalPool.tsindex 8462b903..0c8e39a1 100644--- a/src/renderer/src/components/terminalPool.ts+++ b/src/renderer/src/components/terminalPool.ts@@ -75,8 +75,11 @@ export interface TerminalEntry {const pool = new Map<string, TerminalEntry>();+import { parseHexColor, oscColorBody } from './termColor';+type ThemeMap = Record<string, string>;+/** 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 {@@ -235,6 +238,32 @@ export function acquireTerminal(ptyId: string, theme?: ThemeMap, fontSize = 14):pasteClipboard();});+ // Answer the terminal-colour queries (OSC 10 foreground, OSC 11 background).+ //+ // A TUI that wants to match the terminal asks for its colours with+ // `ESC ] 11 ; ? BEL` and styles itself from the reply. We registered NO OSC+ // handler at all, so the query went unanswered and the app fell back to its own+ // default, which for OpenCode is LIGHT. That is why OpenCode painted near-white+ // panels inside a dark window even with its theme set to `system`, and why it+ // showed up across agents rather than on one engine: any TUI that asks gets the+ // same silence.+ //+ // COLORFGBG (set at spawn) is the older, coarser channel and only carries+ // "light or dark". This carries the ACTUAL colour, so a TUI can match the+ // window rather than guess a side.+ const oscColorReply = (index: 10 | 11) => (data: string): boolean => {+ if (data !== '?') return false; // only the QUERY form; a SET is not ours to handle+ if (entry.exited) return true; // swallow it rather than write to a dead pty+ const map = (term.options.theme ?? theme) as ThemeMap | undefined;+ const hex = index === 11 ? map?.background : map?.foreground;+ const rgb = hex && parseHexColor(hex);+ if (!rgb) return false; // unknown colour: stay silent rather than lie+ window.cth.writePty(ptyId, `\x1b]${index};${oscColorBody(rgb)}\x1b\\`);+ return true;+ };+ term.parser.registerOscHandler(10, oscColorReply(10));+ term.parser.registerOscHandler(11, oscColorReply(11));+// 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/osc-color-reply.test.cjs b/test/osc-color-reply.test.cjsnew file mode 100644index 00000000..ee0c5284--- /dev/null+++ b/test/osc-color-reply.test.cjs@@ -0,0 +1,40 @@+'use strict';++const test = require('node:test');+const assert = require('node:assert/strict');+const loadTs = require('./load-ts.cjs');++const { parseHexColor, oscColorBody } =+ loadTs('src/renderer/src/components/termColor.ts');++// A TUI asks the terminal for its colours with OSC 10/11 and styles itself from+// the reply. We answered nothing, so OpenCode fell back to its own default (light)+// and painted near-white panels inside a dark window.++test('#rrggbb parses to bytes', () => {+ assert.deepEqual(parseHexColor('#1a2b3c'), [0x1a, 0x2b, 0x3c]);+});++test('#rgb shorthand expands the way CSS does', () => {+ assert.deepEqual(parseHexColor('#abc'), [0xaa, 0xbb, 0xcc]);+});++test('case and surrounding space do not matter', () => {+ assert.deepEqual(parseHexColor(' #FFFFFF '), [255, 255, 255]);+});++test('anything we cannot read returns null so the query goes UNANSWERED', () => {+ // Silence is the correct failure mode. Replying with a guess is worse than not+ // replying: the TUI then styles itself confidently wrong, which is the exact+ // bug this fixes.+ for (const bad of ['', 'red', 'rgb(0,0,0)', '#12', '#12345', '#gggggg', '1a2b3c']) {+ assert.equal(parseHexColor(bad), null, `expected null for ${JSON.stringify(bad)}`);+ }+});++test('the reply widens each byte the way xterm does', () => {+ // xterm answers in 16-bit-per-channel form; doubling the byte is the+ // conventional widening, so 0x1a becomes 0x1a1a.+ assert.equal(oscColorBody(parseHexColor('#1a2b3c')), 'rgb:1a1a/2b2b/3c3c');+ assert.equal(oscColorBody(parseHexColor('#000')), 'rgb:0000/0000/0000');+});