From 8202e07286a4f8a00a6d07bb84d5a147ad51b8d3 Mon Sep 17 00:00:00 2001 From: Codeman maintainer Date: Fri, 20 Nov 2026 04:02:07 +0100 Subject: [PATCH] fix(watchers): the two review items of #818 The fs.watch failure tests skip on macOS, where chokidar 3 never calls fs.watch for these watchers (fsevents, or polling without it), so the stub never fired and the waits timed out. TeamWatcher.stop() clears its outage set, so a stop() and start() on the same instance warns again about a watcher that still fails; a new test pins it. Co-Authored-By: Claude Opus 5.5 --- src/team-watcher.ts | 1 + test/watcher-fs-watch-failure.test.ts | 34 ++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/team-watcher.ts b/src/team-watcher.ts index 54a04042..30809b08 100644 --- a/src/team-watcher.ts +++ b/src/team-watcher.ts @@ -137,6 +137,7 @@ export class TeamWatcher extends EventEmitter { this.taskMtimes.clear(); this.inboxMtimes.clear(); this.sessionToTeam.clear(); + this.watchersDown.clear(); } /** Get all discovered teams */ diff --git a/test/watcher-fs-watch-failure.test.ts b/test/watcher-fs-watch-failure.test.ts index 14f10721..a6463f4e 100644 --- a/test/watcher-fs-watch-failure.test.ts +++ b/test/watcher-fs-watch-failure.test.ts @@ -19,6 +19,11 @@ const FAILURES = [ ] as const; type WatchFailure = { code: string; errno: number; text: string }; +// On macOS chokidar 3 never calls fs.watch for these watchers: it uses fsevents, +// or polling when fsevents is missing, so the retry loop these tests pin cannot +// happen there and the stubbed fs.watch never fires. +const describeLinuxWatch = describe.skipIf(process.platform === 'darwin'); + const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); async function waitUntil(predicate: () => boolean, message: string): Promise { @@ -97,7 +102,7 @@ async function writeTeam(teamsDir: string, config: TeamConfig): Promise { await writeFile(join(dir, 'config.json'), JSON.stringify(config)); } -describe('TeamWatcher fs.watch failure', () => { +describeLinuxWatch('TeamWatcher fs.watch failure', () => { let teamsDir: string; let tasksDir: string; let teamWatcher: TeamWatcher; @@ -181,6 +186,29 @@ describe('TeamWatcher fs.watch failure', () => { } }); + it('warns again about a watcher that still fails after stop() and start() on the same instance', async () => { + const failure = FAILURES[0]; + const watch = failEveryWatch(failure); + const expectedWarnings = [ + `[TeamWatcher] teams watcher stopped, relying on polling: ${watchError(failure, teamsDir).message}`, + `[TeamWatcher] tasks watcher stopped, relying on polling: ${watchError(failure, tasksDir).message}`, + ]; + try { + teamWatcher.start(); + await waitUntil(() => warn.mock.calls.length >= 2, 'timed out waiting for both watcher warnings'); + await delay(200); + expectWarnings(expectedWarnings); + teamWatcher.stop(); + teamWatcher.start(); + await waitUntil(() => warn.mock.calls.length >= 4, 'a restarted watcher stayed silent about the same outage'); + await delay(200); + expectWarnings([...expectedWarnings, ...expectedWarnings]); + } finally { + teamWatcher.stop(); + watch.mockRestore(); + } + }); + it('keeps a healthy watcher active and discovers a new team before the poll interval', async () => { const watch = vi.spyOn(fs, 'watch'); try { @@ -426,7 +454,7 @@ describe('TeamWatcher fs.watch failure', () => { ); }); -describe('TeamWatcher in-flight fs.watch failure', () => { +describeLinuxWatch('TeamWatcher in-flight fs.watch failure', () => { it('stays stopped when file replacement and deletion race with ENOSPC', async () => { const teamsDir = join(tempDir, 'teams'); const tasksDir = join(tempDir, 'tasks'); @@ -534,7 +562,7 @@ const EXPECTED_RUN: WorkflowRunInfo = { lastActivityAt: RUN_START, }; -describe('WorkflowRunWatcher fs.watch failure', () => { +describeLinuxWatch('WorkflowRunWatcher fs.watch failure', () => { let runWatcher: WorkflowRunWatcher; let workflowsDir: string;