From 25f0a38aba9054340aac0b04b6ff43cdaf1f28c5 Mon Sep 17 00:00:00 2001 From: axisrow Date: Mon, 28 Sep 2026 09:58:47 +0800 Subject: [PATCH] fix(daemon): re-probe terminal width on every serve request getTerminalWidth memoizes its probe process-globally, so in a long-lived --serve process a terminal resize after the first request kept serving the stale width forever, even with terminalWidthCacheTtlSeconds: 0 (80->120 resize reproduced: serve kept 80, a fresh process saw 120). Reset the memo before each request via the existing resetTerminalWidthCache seam, and add a regression test asserting the new width is picked up. Co-Authored-By: Claude Code --- src/__tests__/serve.test.ts | 20 ++++++++++++++++++++ src/serve.ts | 13 +++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/__tests__/serve.test.ts b/src/__tests__/serve.test.ts index 0c977cd39..d8cf02d20 100644 --- a/src/__tests__/serve.test.ts +++ b/src/__tests__/serve.test.ts @@ -68,4 +68,24 @@ describe('handleServeLine', () => { expect(invocation.terminalWidth).toBeNull(); expect(typeof deps.loadSettings).toBe('function'); }); + + it('re-probes terminal width on every request instead of serving the memo', () => { + const deps = createProcessServeDependencies(); + const originalWidth = process.env.CCSTATUSLINE_WIDTH; + try { + process.env.CCSTATUSLINE_WIDTH = '80'; + expect(deps.resolveTerminalWidth(undefined, 0)).toBe(80); + + // A resize between requests must be picked up: the process-global + // memo is reset before each probe. + process.env.CCSTATUSLINE_WIDTH = '120'; + expect(deps.resolveTerminalWidth(undefined, 0)).toBe(120); + } finally { + if (originalWidth === undefined) { + delete process.env.CCSTATUSLINE_WIDTH; + } else { + process.env.CCSTATUSLINE_WIDTH = originalWidth; + } + } + }); }); diff --git a/src/serve.ts b/src/serve.ts index f86e09568..062616cc4 100644 --- a/src/serve.ts +++ b/src/serve.ts @@ -9,7 +9,10 @@ import { getConfigPath, loadSettingsFrom } from './utils/config'; -import { getTerminalWidth } from './utils/terminal'; +import { + getTerminalWidth, + resetTerminalWidthCache +} from './utils/terminal'; // Dependencies of the serve loop, injectable so tests can run hermetically // (fixed settings, no terminal probe) while production wires process state. @@ -22,7 +25,13 @@ export interface ServeDependencies { export function createProcessServeDependencies(): ServeDependencies { return { loadSettings: () => loadSettingsFrom(getConfigPath()), - resolveTerminalWidth: (sessionId, ttlSeconds) => getTerminalWidth({ sessionId, ttlSeconds }), + resolveTerminalWidth: (sessionId, ttlSeconds) => { + // A long-lived serve process must re-probe per request: the width + // memo is process-global, so a terminal resize after the first + // request would serve the stale width forever, ttl or not. + resetTerminalWidthCache(); + return getTerminalWidth({ sessionId, ttlSeconds }); + }, buildInvocation: terminalWidth => ({ configPath: getConfigPath(), cwd: process.cwd(),