From f6c6ce7133db33b62df7ef11d7a17a34c06667a3 Mon Sep 17 00:00:00 2001 From: iamtoruk Date: Wed, 2 Sep 2026 05:12:46 -0700 Subject: [PATCH] fix(app): a default is for a machine that has made no choice The first run wrote enabled: true into windows-dock.json and wrote the tray app's Run value, whatever was already there. On a machine where somebody had installed the tray app by hand and deliberately turned the rail off, or turned launch at login off, installing the desktop app quietly undid both. A default is an answer for a question nobody has answered, not an override of one that has been. So the seed now looks first. An existing enabled value is left exactly as it is and mirrored into the Sidebar switch instead, so the corner opens saying what the rail is actually doing rather than what this app would have preferred; only an absent key is seeded. A file that exists but has never mentioned the rail still counts as no answer, which is the case for anyone who has only ever dragged the rail somewhere. The Run value follows the same rule and, like the rail, is only ever touched on the first run. After that it belongs to whoever changes it: the tray app's own launch-at-login toggle, or the Menu bar switch here, which still writes it on and deletes it off. --- app/electron/menubar.test.ts | 82 ++++++++++++++++++++++++++++++++++++ app/electron/menubar.ts | 66 ++++++++++++++++++++++++++--- 2 files changed, 143 insertions(+), 5 deletions(-) diff --git a/app/electron/menubar.test.ts b/app/electron/menubar.test.ts index f194b683..27620ff8 100644 --- a/app/electron/menubar.test.ts +++ b/app/electron/menubar.test.ts @@ -11,7 +11,9 @@ import { findPackagedTrayExe, findStagedMsi, parseInstallResult, + parseRunKeyValue, readCompanionSettings, + readDockEnabled, runKeyArgs, system32Path, writeCompanionSettings, @@ -89,6 +91,22 @@ describe('runKeyArgs', () => { }) }) +describe('parseRunKeyValue', () => { + it('reads the value out of a reg query, and null when there is none', () => { + const output = [ + '', + 'HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run', + ` CodeBurn REG_SZ "${TRAY_EXE}"`, + '', + ].join('\r\n') + + expect(parseRunKeyValue(output)).toBe(`"${TRAY_EXE}"`) + expect(parseRunKeyValue('ERROR: The system was unable to find the specified registry key')).toBeNull() + // A different value under the same key is not this one. + expect(parseRunKeyValue(' OneDrive REG_SZ "C:\\OneDrive.exe"')).toBeNull() + }) +}) + describe('system32Path', () => { it('never resolves a system tool by bare name', () => { expect(system32Path('reg.exe', { SystemRoot: 'D:\\Windows' })).toBe('D:\\Windows\\System32\\reg.exe') @@ -107,6 +125,20 @@ describe('the dock preference file', () => { expect(JSON.parse(readFileSync(dockPrefsPath(home), 'utf8'))).toEqual({ enabled: true }) }) + it('reports whether the tray app has already said something about the rail', () => { + expect(readDockEnabled(home)).toBeUndefined() + + mkdirSync(join(home, '.config', 'codeburn'), { recursive: true }) + writeFileSync(dockPrefsPath(home), JSON.stringify({ scale: 1.2 })) + expect(readDockEnabled(home)).toBeUndefined() + + writeFileSync(dockPrefsPath(home), JSON.stringify({ enabled: false, scale: 1.2 })) + expect(readDockEnabled(home)).toBe(false) + + writeFileSync(dockPrefsPath(home), '{ broken') + expect(readDockEnabled(home)).toBeUndefined() + }) + it('keeps every key the tray app owns', () => { mkdirSync(join(home, '.config', 'codeburn'), { recursive: true }) writeFileSync(dockPrefsPath(home), JSON.stringify({ enabled: true, scale: 1.2, preferred: 'claude' })) @@ -146,6 +178,7 @@ describe('MenubarCompanion', () => { let regCalls: string[][] let cliCalls: Array<{ args: string[]; env: NodeJS.ProcessEnv | undefined }> let installResult: MenubarInstallResult | null + let existingRunKey: string | null function deps(overrides: Record = {}) { return { @@ -157,6 +190,7 @@ describe('MenubarCompanion', () => { home, launch: (exe: string, args: string[]) => { launches.push({ exe, args }) }, runReg: async (args: string[]) => { regCalls.push(args) }, + readRunKey: async () => existingRunKey, runCli: async (args: string[], opts: { extraEnv?: NodeJS.ProcessEnv }) => { cliCalls.push({ args, env: opts.extraEnv }) return { @@ -192,6 +226,7 @@ describe('MenubarCompanion', () => { regCalls = [] cliCalls = [] installResult = result() + existingRunKey = null }) afterEach(() => { rmSync(sandbox, { recursive: true, force: true }) }) @@ -229,6 +264,53 @@ describe('MenubarCompanion', () => { expect(JSON.parse(readFileSync(dockPrefsPath(home), 'utf8'))).toEqual({ enabled: false }) }) + it('leaves a rail preference that was already there, and mirrors it into the switch', async () => { + stageMsi() + // A tray app installed by hand, with the rail deliberately off. + mkdirSync(join(home, '.config', 'codeburn'), { recursive: true }) + writeFileSync(dockPrefsPath(home), JSON.stringify({ enabled: false, scale: 1.2 })) + const companion = new MenubarCompanion(deps()) + + await companion.bootstrap() + + expect(JSON.parse(readFileSync(dockPrefsPath(home), 'utf8'))).toEqual({ enabled: false, scale: 1.2 }) + expect(companion.status().sidebar).toBe(false) + expect(readCompanionSettings(stateDir).sidebar).toBe(false) + }) + + it('still seeds when the file exists but has never mentioned the rail', async () => { + stageMsi() + mkdirSync(join(home, '.config', 'codeburn'), { recursive: true }) + writeFileSync(dockPrefsPath(home), JSON.stringify({ scale: 1.2 })) + + await new MenubarCompanion(deps()).bootstrap() + + expect(JSON.parse(readFileSync(dockPrefsPath(home), 'utf8'))).toEqual({ enabled: true, scale: 1.2 }) + }) + + it('leaves an existing launch-at-login value alone', async () => { + stageMsi() + existingRunKey = `"${TRAY_EXE}"` + + await new MenubarCompanion(deps()).bootstrap() + + expect(regCalls).toEqual([]) + // Everything else still happens: the install is what carries a version forward. + expect(cliCalls).toHaveLength(1) + expect(launches).toEqual([{ exe: TRAY_EXE, args: ['--reload-settings'] }]) + }) + + it('writes the launch-at-login value on the first run only', async () => { + stageMsi() + await new MenubarCompanion(deps()).bootstrap() + expect(regCalls).toEqual([runKeyArgs(true, TRAY_EXE)]) + + // A person turning launch at login off in the tray app's settings must not find it back on. + regCalls = [] + await new MenubarCompanion(deps()).bootstrap() + expect(regCalls).toEqual([]) + }) + it('does nothing at all with the Menu bar switch off', async () => { stageMsi() writeCompanionSettings(stateDir, { menuBar: false, sidebar: true, trayExePath: null, seeded: true }) diff --git a/app/electron/menubar.ts b/app/electron/menubar.ts index a0933ba0..8dceb197 100644 --- a/app/electron/menubar.ts +++ b/app/electron/menubar.ts @@ -87,6 +87,8 @@ export type MenubarDeps = { runCli?: (args: string[], opts: { timeoutMs?: number; extraEnv?: NodeJS.ProcessEnv }) => Promise launch?: (exePath: string, args: string[]) => void runReg?: (args: string[]) => Promise + /** The tray app's existing Run value, or null when there is none. */ + readRunKey?: () => Promise home?: string } @@ -151,6 +153,20 @@ export function writeCompanionSettings(stateDir: string, settings: CompanionSett } } +/** Whether the tray app already has an opinion about the rail, or undefined when nobody has + * said. The difference matters exactly once: a default is for a machine that has made no + * choice, never an override of one already made. */ +export function readDockEnabled(home = homedir()): boolean | undefined { + try { + const parsed: unknown = JSON.parse(readFileSync(dockPrefsPath(home), 'utf8')) + if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return undefined + const enabled = (parsed as Record).enabled + return typeof enabled === 'boolean' ? enabled : undefined + } catch { + return undefined + } +} + /** Read, change one key, write back. The tray app owns every other key in this file (the * rail's placement, size and provider set), so the whole object is preserved. */ export function writeDockEnabled(enabled: boolean, home = homedir()): void { @@ -185,6 +201,14 @@ export function runKeyArgs(enabled: boolean, exePath: string): string[] { : ['delete', RUN_KEY, '/v', RUN_VALUE, '/f'] } +/** `reg query /v CodeBurn` prints the value on its own indented line. Null means the + * value is not there, which is the only thing the caller acts on. */ +export function parseRunKeyValue(regOutput: string): string | null { + const match = new RegExp(`^\\s+${RUN_VALUE}\\s{4}REG_\\w+\\s{4}(.*)$`, 'm').exec(regOutput) + const value = match?.[1]?.trim() + return value ? value : null +} + /** Windows searches the current directory before PATH, so `reg` by bare name lets anything * dropped beside the app impersonate it. Same rule as src/menubar-installer.ts. */ export function system32Path(exe: string, env: NodeJS.ProcessEnv): string { @@ -213,6 +237,23 @@ function regRun(args: string[], env: NodeJS.ProcessEnv): Promise { }) } +function regQueryRunValue(env: NodeJS.ProcessEnv): Promise { + return new Promise(resolve => { + const child = spawn(system32Path('reg.exe', env), ['query', RUN_KEY, '/v', RUN_VALUE], { + stdio: ['ignore', 'pipe', 'ignore'], + windowsHide: true, + }) + let out = '' + child.stdout.setEncoding('utf8') + child.stdout.on('data', chunk => { out += chunk }) + child.on('error', () => resolve(null)) + // reg exits non-zero for a value that is not there, which is the answer rather than a + // failure; anything else that goes wrong reads the same way, and the only cost of + // guessing "absent" is one write of a value that already said the same thing. + child.on('close', code => resolve(code === 0 ? parseRunKeyValue(out) : null)) + }) +} + // The controller --------------------------------------------------------------------------------- export type CompanionStatus = { @@ -268,10 +309,16 @@ export class MenubarCompanion { async bootstrap(): Promise { if (!this.supported()) return - // The defaults are seeded once. A person who later turns the rail off in the tray app's - // own settings must not find it back on at the next launch of this one. + // The defaults are seeded once, and only onto a machine that has made no choice. A tray + // app someone installed by hand has preferences of its own, and a default that overrode + // them would be this app deciding something the person already decided. const firstRun = !this.settings.seeded - if (firstRun) this.save({ seeded: true }) + const existingDock = firstRun ? readDockEnabled(this.home) : undefined + if (firstRun) { + // An existing rail preference is mirrored into the switch rather than overwritten, so + // the corner opens saying what the rail is actually doing. + this.save({ seeded: true, ...(existingDock === undefined ? {} : { sidebar: existingDock }) }) + } if (!this.settings.menuBar) return @@ -281,11 +328,20 @@ export class MenubarCompanion { if (!exePath) return this.save({ trayExePath: exePath }) - if (firstRun && this.settings.sidebar) this.applyDockSetting(true) - await this.setRunKey(true) + if (firstRun && existingDock === undefined && this.settings.sidebar) this.applyDockSetting(true) + // Same rule for launch at login: an existing Run value was written by the tray app's own + // toggle or by an earlier launch of this one, and either way it is not this launch's to + // rewrite. After the first run only the Menu bar switch touches it. + if (firstRun && await this.existingRunKey() === null) await this.setRunKey(true) this.launch(exePath, ['--reload-settings']) } + private existingRunKey(): Promise { + if (this.deps.readRunKey) return this.deps.readRunKey() + // The Store route has no Run value at all, so there is nothing to ask about. + return this.deps.store ? Promise.resolve(null) : regQueryRunValue(this.deps.env) + } + /** The install itself belongs to the CLI: it owns the registry read, the checksum, the * msiexec call and the never-downgrade rule, and this side only stages the file. */ private async install(): Promise {