Skip to content

Commit 7969a8c

Browse files
sahrizviclaude
andcommitted
fix(tui): ask the OS for appearance on the direct-run fallback too
Addresses the open review findings on this PR. `fallbackMode` fed `resolveInitialMode` only COLORFGBG and the OSC reply, so the direct-run renderer did not actually agree with the startup path it shares that function with. On a light Apple Terminal — no COLORFGBG, no OSC 11 answer — a failed palette query still resolved to "dark" and repainted a light terminal dark, which is the #809 symptom this branch exists to remove. It now consults `detectSystemAppearance()`, but only when both cheap signals came back empty, so the `defaults` spawn stays off the common path. `footer.ts` keeps the last known-good theme when a runtime palette refresh fails, and detected that by comparing against `RUN_THEME_FALLBACK`. Once the fallback became per-mode, a light terminal produced a different instance, the identity check missed, and the footer replaced a good theme with the fallback. `isRunThemeFallback` tests membership in the memo map instead. `detectSystemAppearance` had a `clean` clause resolving "light" for an error with no `code` that was not killed. execFile never produces that shape — it sets `code` to the errno string on spawn failure and to the exit status otherwise — and had it fired it would have reported light for an unknown failure, which the comment directly above it forbids. Unknown now stays null so the caller can keep looking. The palette-failure test pinned the dark instance by identity, which re-encoded the behaviour being fixed and fails on a light-mode runner. It now asserts that a failed lookup yields *a* fallback, with a separate case keeping the dark instance pinned for an explicit dark signal. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_018fJ3X7pcGT4R9yzjsJnqsV
1 parent e452112 commit 7969a8c

4 files changed

Lines changed: 69 additions & 15 deletions

File tree

‎packages/opencode/src/cli/cmd/run/footer.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { SUBAGENT_INSPECTOR_ROWS } from "./footer.subagent"
3535
import { PROMPT_MAX_ROWS, TEXTAREA_MIN_ROWS } from "./footer.prompt"
3636
import { RunFooterView } from "./footer.view"
3737
import { RunScrollbackStream } from "./scrollback.surface"
38-
import { RUN_THEME_FALLBACK, resolveRunTheme, type RunTheme } from "./theme"
38+
import { isRunThemeFallback, resolveRunTheme, type RunTheme } from "./theme"
3939
import { modelInfo } from "./variant.shared"
4040
import type {
4141
FooterApi,
@@ -1014,9 +1014,12 @@ export class RunFooter implements FooterApi {
10141014
}
10151015

10161016
// Keep the last known good theme when a runtime OSC probe times out.
1017-
if (theme === RUN_THEME_FALLBACK) {
1017+
// altimate_change start — upstream_fix: the fallback is per-mode now, so an
1018+
// identity check against the dark instance alone missed the light one.
1019+
if (isRunThemeFallback(theme)) {
10181020
return
10191021
}
1022+
// altimate_change end
10201023

10211024
this.themes.push(theme)
10221025
this.setTheme(theme)

‎packages/opencode/src/cli/cmd/run/theme.ts‎

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import type { TuiThemeCurrent } from "@opencode-ai/plugin/tui"
1010
import type { EntryKind } from "./types"
1111
// altimate_change start — share the TUI's mode-resolution chain with the direct-run renderer
1212
// Shared with the full-screen TUI so both renderers agree on how a mode is chosen.
13-
import { resolveInitialMode } from "@opencode-ai/tui/terminal-detection"
13+
import {
14+
detectModeFromCOLORFGBG,
15+
detectSystemAppearance,
16+
resolveInitialMode,
17+
} from "@opencode-ai/tui/terminal-detection"
1418
// altimate_change end
1519

1620
type Tone = {
@@ -688,6 +692,22 @@ export function runThemeFallback(mode: "dark" | "light"): RunTheme {
688692

689693
/** Dark instance, kept as the default for callers with no mode to hand. */
690694
export const RUN_THEME_FALLBACK: RunTheme = runThemeFallback("dark")
695+
696+
// altimate_change start — upstream_fix: recognise every per-mode fallback.
697+
/**
698+
* True for any fallback instance, not just the dark one.
699+
*
700+
* `footer.ts` keeps the last known-good theme when a runtime palette refresh
701+
* fails, and used to detect that by comparing against `RUN_THEME_FALLBACK`.
702+
* Now that the fallback is per-mode, a light terminal produced a *different*
703+
* instance, that check missed, and the footer replaced a good theme with the
704+
* fallback. Membership in the memo map is the identity test that survives.
705+
*/
706+
export function isRunThemeFallback(theme: RunTheme): boolean {
707+
for (const cached of fallbackByMode.values()) if (cached === theme) return true
708+
return false
709+
}
710+
// altimate_change end
691711
// altimate_change end
692712

693713
// altimate_change start — resolve a mode instead of always falling back to dark
@@ -699,11 +719,17 @@ export const RUN_THEME_FALLBACK: RunTheme = runThemeFallback("dark")
699719
* higher-traffic sibling of the startup detection in packages/tui — it drives
700720
* the direct-run and scrollback renderer.
701721
*/
702-
function fallbackMode(renderer: CliRenderer): "dark" | "light" {
703-
return resolveInitialMode({
704-
colorfgbg: process.env["COLORFGBG"],
705-
osc: renderer.themeMode ?? null,
706-
})
722+
async function fallbackMode(renderer: CliRenderer): Promise<"dark" | "light"> {
723+
const colorfgbg = process.env["COLORFGBG"]
724+
const osc = renderer.themeMode ?? null
725+
// Ask the OS only when neither cheap signal answered. Without this the
726+
// direct-run path did not actually agree with the startup path it shares
727+
// `resolveInitialMode` with: on a light Apple Terminal — no COLORFGBG, no
728+
// OSC 11 reply — it still resolved "dark" and repainted a light terminal
729+
// dark, which is the #809 symptom this change exists to remove. The probe
730+
// spawns `defaults`, so it stays behind the two free signals.
731+
const appearance = osc || detectModeFromCOLORFGBG(colorfgbg) ? null : await detectSystemAppearance()
732+
return resolveInitialMode({ colorfgbg, osc, appearance })
707733
}
708734
// altimate_change end
709735

@@ -715,7 +741,7 @@ export async function resolveRunTheme(renderer: CliRenderer): Promise<RunTheme>
715741
const bg = colors.defaultBackground ?? colors.palette[0]
716742
if (!bg) {
717743
// altimate_change start — light terminals must not get the dark fallback
718-
return runThemeFallback(fallbackMode(renderer))
744+
return runThemeFallback(await fallbackMode(renderer))
719745
// altimate_change end
720746
}
721747

@@ -742,7 +768,7 @@ export async function resolveRunTheme(renderer: CliRenderer): Promise<RunTheme>
742768
)
743769
} catch {
744770
// altimate_change start — light terminals must not get the dark fallback
745-
return runThemeFallback(fallbackMode(renderer))
771+
return runThemeFallback(await fallbackMode(renderer))
746772
// altimate_change end
747773
}
748774
}

‎packages/opencode/test/cli/run/theme.test.ts‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { expect, test } from "bun:test"
22
import { type ColorInput, RGBA, type CliRenderer, type TerminalColors } from "@opentui/core"
3-
import { RUN_THEME_FALLBACK,
4-
runThemeFallback, generateSystem, resolveRunTheme, resolveTheme } from "@/cli/cmd/run/theme"
3+
import {
4+
RUN_THEME_FALLBACK,
5+
isRunThemeFallback,
6+
runThemeFallback,
7+
generateSystem,
8+
resolveRunTheme,
9+
resolveTheme,
10+
} from "@/cli/cmd/run/theme"
511

612
const palette = ["#15161e", "#f7768e", "#9ece6a", "#e0af68", "#7aa2f7", "#bb9af7", "#7dcfff", "#c0caf5"] as const
713

@@ -60,7 +66,21 @@ function spread(color: RGBA) {
6066
}
6167

6268
test("falls back when palette lookup fails", async () => {
63-
expect(await resolveRunTheme(renderer({ fail: true }))).toBe(RUN_THEME_FALLBACK)
69+
// Deliberately not `toBe(RUN_THEME_FALLBACK)`: with no OSC reply and no
70+
// COLORFGBG the fallback now asks the OS for its appearance, so which
71+
// per-mode instance comes back depends on the machine running the test.
72+
// Pinning the dark one would re-encode the #809 behaviour this PR removes
73+
// and would fail on a light-mode runner. The invariant is that a failed
74+
// palette lookup yields *a* fallback rather than a resolved theme.
75+
const theme = await resolveRunTheme(renderer({ fail: true }))
76+
expect(isRunThemeFallback(theme)).toBe(true)
77+
})
78+
79+
test("a dark terminal still gets the dark fallback", async () => {
80+
// The mode-aware path must not have inverted anything: given an explicit
81+
// dark signal the fallback is still the dark instance callers compare by
82+
// identity.
83+
expect(await resolveRunTheme(renderer({ fail: true, themeMode: "dark" }))).toBe(RUN_THEME_FALLBACK)
6484
})
6585

6686
test("the fallback follows a light terminal instead of always going dark", async () => {

‎packages/tui/src/terminal-detection.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,13 @@ export function detectSystemAppearance(
109109
// silently reported as light.
110110
const err = error as NodeJS.ErrnoException & { killed?: boolean; status?: number | null; stderr?: string }
111111
const notFound = /does not exist/i.test(String(err.stderr ?? err.message ?? ""))
112-
const clean = err.code === undefined && err.killed !== true
113-
resolve(notFound || clean ? "light" : null)
112+
// Only the missing-key diagnostic means light. A previous `clean`
113+
// clause also accepted "no `code` and not killed", which execFile
114+
// never produces (it sets `code` to the errno string on spawn failure
115+
// and the exit status otherwise) — and had it ever fired it would have
116+
// reported light for an unknown failure, the exact thing the comment
117+
// above forbids. Unknown stays null so the caller can keep looking.
118+
resolve(notFound ? "light" : null)
114119
},
115120
)
116121
} catch {

0 commit comments

Comments
 (0)