From bd88b89988cca1629c2ed85ec232bd515ed04ed3 Mon Sep 17 00:00:00 2001 From: Resham Joshi <65915470+iamtoruk@users.noreply.github.com> Date: Mon, 5 Oct 2026 23:51:04 +0200 Subject: [PATCH] fix: stop the desktop status-timeout loop (#1644) The desktop serve client counted every death, watchdog kills included, against a 3-death budget that never reset, so three sporadic kills over an app run disabled serve for good. The budget now resets on any healthy answer: three generations in a row that never answer still disable it. One-shot CLI runs only heartbeated inside a parse, so a run waiting on another process's cold hydration lock (up to 10 min) went silent and was killed at 45s on every poll. Start the keepalive at command entry. --- app/electron/cli.test.ts | 27 +++++++++++++ app/electron/cli.ts | 8 ++-- src/main.ts | 6 +++ tests/cli-progress-keepalive-entry.test.ts | 45 ++++++++++++++++++++++ 4 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 tests/cli-progress-keepalive-entry.test.ts diff --git a/app/electron/cli.test.ts b/app/electron/cli.test.ts index 99047c4a..58db2996 100644 --- a/app/electron/cli.test.ts +++ b/app/electron/cli.test.ts @@ -1239,6 +1239,33 @@ describe('resident serve single-flight', { timeout: 30_000 }, () => { expect(readMaybe(oneShotsFile)).toBe('oooo') }) + it('keeps restarting a resident that answers between watchdog kills', async () => { + const startsFile = join(dir, 'serve-starts') + fakeBin( + 'answers-then-hangs-resident.js', + `const fs = require('node:fs'); const readline = require('node:readline'); + if (process.argv[2] === 'serve') { + fs.appendFileSync(${JSON.stringify(startsFile)}, 's'); + const rl = readline.createInterface({ input: process.stdin }); + rl.once('line', line => { + const request = JSON.parse(line); + process.stdout.write(JSON.stringify({ id: request.id, ok: true, output: JSON.stringify({ via: 'serve' }) }) + '\\n'); + }); + setInterval(() => {}, 1000); + } else { + process.stdout.write(JSON.stringify({ via: 'spawn' })); + }`, + ) + startServe() + + // Each generation answers once, then goes silent and is killed by the + // watchdog. Those kills must not add up across healthy answers. + for (let attempt = 0; attempt < 10 && readMaybe(startsFile).length < 4; attempt += 1) { + await spawnCli(['status', '--attempt', String(attempt)], { timeoutMs: 300 }) + } + expect(readMaybe(startsFile).length).toBeGreaterThanOrEqual(4) + }) + it('does not spawn a one-shot fallback after killAll destroys serve', async () => { const requestSeenFile = join(dir, 'request-seen') const oneShotsFile = join(dir, 'one-shot-reads') diff --git a/app/electron/cli.ts b/app/electron/cli.ts index c92c134e..28e23a9c 100644 --- a/app/electron/cli.ts +++ b/app/electron/cli.ts @@ -716,7 +716,8 @@ async function runScheduledCli( // - a child left idle for CODEBURN_SERVE_IDLE_MS retires; the next request // restarts one through the same lazy path a crash uses; // - any serve failure falls back to a normal spawn for that call; -// - three child deaths permanently disable serve for this app run. +// - three child deaths (watchdog kills included) with no answer between them +// disable serve for this app run; any answer resets that count. const SERVE_ROUTED = new Set(['status', 'models', 'sessions', 'compare', 'yield', 'spend', 'optimize', 'audit', 'report']) const SERVE_MAX_RESTARTS = 3 @@ -853,6 +854,7 @@ class ServeClient { waiter.clear() if (msg.ok && typeof msg.output === 'string') { if (waiter.warmsServe) this.warmed = true + this.deaths = 0 try { waiter.resolve(JSON.parse(msg.output)) } catch { waiter.reject(new CliError('bad-json', 'codeburn produced output that was not valid JSON')) } } else { @@ -1092,7 +1094,7 @@ function restartServeAfterMutation(): void { // CLI-only consumers never started serve, so do not create a surprise daemon // for them. In Electron, replace the resident child immediately so its parser // and output memos cannot survive a successful config mutation. Reusing the - // client preserves its app-lifetime budget of unexpected child deaths. + // client preserves its budget of unexpected child deaths. if (!serveClient) return serveClient.restartAfterMutation() } @@ -1142,7 +1144,7 @@ export function spawnCli( if (SERVE_ROUTED.has(args[0] ?? '') && isServeCompatibleEnv(opts.extraEnv)) { const serve = serveClient // Recover lazily from an unexpected child death. start() is synchronous and - // idempotent, and the client's lifetime death budget prevents an endlessly + // idempotent, and the client's consecutive-death budget prevents an endlessly // crashing binary from being respawned on every poll. if (serve && !serve.isRunning() && !serve.disabled()) serve.start() if (serve?.isRunning()) { diff --git a/src/main.ts b/src/main.ts index 1e0a716e..60e36bea 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3239,6 +3239,12 @@ if (process.argv[2] === 'serve') { // this child running as an orphan for as long as the machine is up. hardExit(0) } else { + // Beat for the app watchdogs from the first moment, not just inside a parse: + // a one-shot is also silent while it waits on another process's cold + // hydration lock or aggregates after the parse. Never stopped; the timer is + // unref'd and the process exits when the command does. Serve beats per + // request instead, so it must not take this. + startProgressKeepalive() const program = buildProgram() await registerLoadedPluginCommands(program) program.parse() diff --git a/tests/cli-progress-keepalive-entry.test.ts b/tests/cli-progress-keepalive-entry.test.ts new file mode 100644 index 00000000..6cdc43a6 --- /dev/null +++ b/tests/cli-progress-keepalive-entry.test.ts @@ -0,0 +1,45 @@ +import { spawn } from 'node:child_process' +import { mkdtemp, rm, writeFile } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' + +import { describe, expect, it } from 'vitest' + +// A one-shot read that waits on another process's cold hydration lock does no +// parsing yet, and the app watchdogs kill on silence. It must beat anyway. +describe('one-shot CLI keepalive', () => { + it('beats while waiting on a foreign hydration lock', async () => { + const home = await mkdtemp(join(tmpdir(), 'cb-entry-keepalive-home-')) + const cacheDir = await mkdtemp(join(tmpdir(), 'cb-entry-keepalive-cache-')) + // This test process is alive and is not the child, so the child waits. + await writeFile(join(cacheDir, 'hydrating.lock'), JSON.stringify({ pid: process.pid, at: Date.now() })) + const child = spawn(process.execPath, ['--import', 'tsx', 'src/cli.ts', 'status', '--format', 'menubar-json', '--provider', 'claude'], { + cwd: process.cwd(), + env: { + ...process.env, + HOME: home, + USERPROFILE: home, + CLAUDE_CONFIG_DIR: join(home, '.claude'), + CODEBURN_CACHE_DIR: cacheDir, + CODEBURN_PROGRESS: '1', + }, + stdio: ['ignore', 'ignore', 'pipe'], + }) + try { + const sawKeepalive = await new Promise(resolve => { + let stderr = '' + const timer = setTimeout(() => resolve(false), 20_000) + child.stderr.on('data', chunk => { + stderr += chunk + if (stderr.includes('"kind":"keepalive"')) { clearTimeout(timer); resolve(true) } + }) + child.on('exit', () => { clearTimeout(timer); resolve(false) }) + }) + expect(sawKeepalive).toBe(true) + } finally { + child.kill('SIGKILL') + await rm(home, { recursive: true, force: true }) + await rm(cacheDir, { recursive: true, force: true }) + } + }, 30_000) +})