Skip to content

TUI: theme picked in /themes is sometimes replaced by an earlier preview (saves race for cli.json's lock) #65523

Closed

Description

@cevr

Description

Picking a theme in /themes sometimes doesn't stick: the TUI ends up on a theme that matched the first letters I typed (cursor from "c"), and cli.json keeps it after a restart.

The list calls themes.set on every keystroke and move, and each one fires void config.update(...) without waiting for the previous save. Every save takes the cli.json lock, and Flock.acquire polls with backoff, so the lock isn't granted in call order. An early preview's save can land after the pick's save, and the config watcher then applies it to the running TUI too.

On dev (1645a8a), real TUI in a PTY, clean config, 30 picks each:

  • keys back to back: 8–11 of 30 end on the wrong theme
  • 60 ms per key, Enter right after: 2 of 30
  • 60 ms per key with a pause before Enter: 0–1 of 30

One pick writes cli.json 3–19 times. It's rare at normal typing speed on a fast disk, but it happens.

Related: #56147 / #62498 (change the theme only on confirm). This one is about saves landing out of order; any other quick config.update calls race the same way.

Plugins

none

OpenCode version

dev @ 1645a8a (packages/cli 2.3.0)

Steps to reproduce

  1. Clean config, bun dev, open /themes.
  2. Type cur quickly, delete it, type nord, press Enter right away.
  3. A second later ~/.config/opencode/cli.json sometimes says "cursor", and the TUI has switched to it.
  4. Restart: the TUI starts with cursor.

Operating System

macOS 27.0.1 (arm64), Bun 1.4.4

Terminal

PTY harness (xterm-256color)

Activity

  1. cevr commented on Nov 22, 2026

    @cevr
    Author

    I have a fix on a branch (serial config updates, and the theme list previews in memory and saves once on Enter): https://github.com/cevr/opencode/tree/theme-save-order. I'm at the open PR limit here, so I'll open it as a PR when a slot frees up.

  2. added
    area/tui
    type/bug
    version/v2
    on Nov 22, 2026
  3. opencodejr commented on Nov 22, 2026

    @opencodejr
    Contributor

    Thanks for the detailed report, @cevr. I could reproduce this.

    The script below runs the TUI headless with a fresh config. On each try it opens /themes, types cur one key at a time, deletes it, types the target theme (nord or opencode), presses Enter right away and reads cli.json 1.5 s later.

    • 2.3.0: 10 of 20 picks left theme.name set to cursor, the theme the c filter previewed, instead of the one picked.
    • Latest dev (5310e42), run from source: still happens, 9 of 16 picks ended on cursor. themes.set() there still saves the config without waiting for the previous save.

    I checked this through cli.json only. I didn't do the restart step. The recording below shows the run.

    Related: #56147 and PR #62498 (change the theme only on confirm). That change would avoid this in the theme list, but the out-of-order saves would remain for any other quick config updates.

    recording.mp4
    Reproduction script (drive.ts)
    // Repro for #65523: rapid /themes filtering makes an earlier preview's cli.json
    // save land after the confirmed pick's save.
    import { Effect } from "effect"
    import { readFileSync } from "node:fs"
    import { join } from "node:path"
    import { OpenCodeDriver } from "opencode-drive"
    
    const ATTEMPTS = Number(process.env.ATTEMPTS ?? 10)
    
    export default OpenCodeDriver.use(
      {
        ...(process.env.OC_DEV ? { opencode: { dev: process.env.OC_DEV } } : {}),
        tui: { recording: true, keypressOverlay: true },
        tuiConfig: { theme: { name: "opencode" } },
      },
      (oc) =>
        Effect.gen(function* () {
          const ui = oc.ui
          const cliJson = join(oc.artifacts, "files", ".opencode", "cli.json")
          const saved = () => {
            try {
              return JSON.parse(readFileSync(cliJson, "utf8"))?.theme?.name
            } catch (e) {
              return `unreadable: ${e}`
            }
          }
          yield* Effect.sleep(1500)
          let wrong = 0
          for (let i = 1; i <= ATTEMPTS; i++) {
            const target = i % 2 ? "nord" : "opencode"
            yield* ui.type("/themes")
            yield* ui.enter()
            yield* ui.waitFor("esc", { timeout: 20000 })
            // type "cur" one key at a time, delete it, type the target, Enter right away
            for (const c of "cur") yield* ui.type(c)
            for (let k = 0; k < 3; k++) yield* ui.press("backspace")
            for (const c of target) yield* ui.type(c)
            yield* ui.enter()
            yield* Effect.sleep(1500)
            const name = saved()
            const ok = name === target
            if (!ok) wrong++
            console.log(`attempt ${i}: picked=${target} cli.json theme.name=${name} ${ok ? "OK" : "WRONG"}`)
            if (!ok) yield* ui.screenshot(`wrong-${i}`)
          }
          console.log(`RESULT: ${wrong}/${ATTEMPTS} picks ended on the wrong theme in cli.json`)
        }),
    )

    Run it with opencode-drive: opencode-drive run ./drive.ts. It drives opencode from your PATH; set OC_DEV to the path of an OpenCode checkout to run that from source instead.

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

    area/tuiplatform/macosreproducedtype/bugversion/v2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions