From 7f46f87fe66f91c535d685b55116d3ddccd5c0e3 Mon Sep 17 00:00:00 2001 From: Milan Bhandari Date: Thu, 19 Nov 2026 11:30:05 -0800 Subject: [PATCH] Re-create failed worktree watchers and stop reinspecting on every read One watcher error, or a failed subscribe, marked a live worktree unhealthy for good. Every refresh tick and GET then re-ran a full inspection (git ls-files plus an lstat per working file) for every live board on it, until the app restarted. A worktree with a missing watcher now reuses its last inspection for ten seconds. The next read after that subscribes again and then inspects, so a watcher that comes back costs one inspection and one that cannot costs one every ten seconds. Watcher failures are logged. Closes #1548. Agent-Session: f5bf6b6e-2f61-4a5d-a234-befeae23de4d Agent-Session: 33d99bfe-0d11-4a51-86b9-0a2f51551005 --- .../review/src/review-api/local-data.test.ts | 57 ++++++++++---- packages/review/src/review-api/local-data.ts | 74 +++++++++++++++---- 2 files changed, 100 insertions(+), 31 deletions(-) diff --git a/packages/review/src/review-api/local-data.test.ts b/packages/review/src/review-api/local-data.test.ts index 05c0e3fc49..f4eb3be74b 100644 --- a/packages/review/src/review-api/local-data.test.ts +++ b/packages/review/src/review-api/local-data.test.ts @@ -35,7 +35,15 @@ import { AskThreads } from "@review/ask/threads.js"; import { createGlobalReviewServer } from "@review/server/desktop-server.js"; import { Hono } from "hono"; import sharp from "sharp"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { + afterEach, + beforeEach, + describe, + expect, + it, + onTestFinished, + vi, +} from "vitest"; import { type AuthoringTool, @@ -3591,7 +3599,7 @@ it("does not report clean tracked symlinks and submodules as modified", async () }); }); -it("continues capturing after watchers fail and stop emitting changes", async () => { +it("keeps capturing after watchers fail without reinspecting on every refresh", async () => { await local.data.close(); await local.store.close(); const callbacks: SubscribeCallback[] = []; @@ -3606,7 +3614,7 @@ it("continues capturing after watchers fail and stop emitting changes", async () const created = await local.store.execute( command({ type: "create", - title: "Watcher recovery", + title: "Watcher failure", target: { kind: "worktree", repositoryId: pins.repositoryId, @@ -3615,22 +3623,39 @@ it("continues capturing after watchers fail and stop emitting changes", async () }), ); + // An inspection made before the failure is reused for a while. + const now = performance.now.bind(performance); + let skipped = 0; + + const clock = vi + .spyOn(performance, "now") + .mockImplementation(() => now() + skipped); + + onTestFinished(() => clock.mockRestore()); + for (const callback of callbacks) callback(new Error("No space left on device"), []); - for (const value of ["first change\n", "second change\n"]) { - writeFileSync(path.join(repository, "example.ts"), value); - await local.store.refreshWorktrees(); - expect( - ( - await local.data.file( - local.store.read(created.reviewId).pins!, - "head", - "example.ts", - ) - ).text, - ).toBe(value); - } + writeFileSync(path.join(repository, "example.ts"), "changed\n"); + skipped += 60_000; + await local.store.refreshWorktrees(); + expect( + ( + await local.data.file( + local.store.read(created.reviewId).pins!, + "head", + "example.ts", + ) + ).text, + ).toBe("changed\n"); + + // The new watchers may still report the write. + await watcherLatency(); + await local.store.refreshWorktrees(); + recordSpawns(); + + for (let i = 0; i < 5; i++) await local.store.refreshWorktrees(); + expect(spawns.filter((args) => args.includes("ls-files"))).toHaveLength(0); }); it("keeps live language identity across edits but replaces it with a checkout at the same path", async () => { diff --git a/packages/review/src/review-api/local-data.ts b/packages/review/src/review-api/local-data.ts index be11989beb..f01362bb73 100644 --- a/packages/review/src/review-api/local-data.ts +++ b/packages/review/src/review-api/local-data.ts @@ -181,6 +181,9 @@ const PULL_REQUEST_STATE_TTL_MS = 5 * 60_000; /** How many PR states are read from GitHub at once. */ const PULL_REQUEST_STATE_READS = 2; +/** How long an inspection of a worktree with a failed watcher is reused. */ +const UNWATCHED_INSPECTION_MS = 10_000; + /** Local source/resource boundary, including Desktop-only local language context. */ export class LocalReviewData { @@ -685,8 +688,12 @@ export class LocalReviewData { epoch: number; inspectedEpoch: number; subscriptions: Map; - healthy: boolean; + /** Subscribes to each watched root. Unset until the first attempt ends. */ + watchers?: Map Promise>; + /** Shared by concurrent reads that find a watcher missing. */ + rewatching?: Promise; inspection?: Omit>, "files">; + inspectedAt: number; tracked?: Set; /** Rereads ignored directories after one may have appeared. */ ignoring?: NodeJS.Timeout; @@ -710,7 +717,7 @@ export class LocalReviewData { epoch: 0, inspectedEpoch: -1, subscriptions: new Map(), - healthy: true, + inspectedAt: 0, forks: new Map(), }; this.worktrees.set(repositoryId, entry); @@ -730,17 +737,26 @@ export class LocalReviewData { ignore: string[], matters: (name: string, event: WatcherEvent) => boolean, ) => { + let subscription: AsyncSubscription | undefined; + try { - const subscription = await ( + subscription = await ( this.options.subscribe ?? (await import("@parcel/watcher")).subscribe )( root, (error, events) => { if (error) { - state.healthy = false; const failed = state.subscriptions.get(root); + // A replaced subscription can still report late. + if (subscription && failed !== subscription) return; + + // The next read of a stale inspection subscribes again. + console.warn( + `Worktree watcher failed for ${root}: ${error.message}`, + ); + if (failed) void release(failed); state.subscriptions.delete(root); } else if ( @@ -762,8 +778,10 @@ export class LocalReviewData { state.subscriptions.set(root, subscription); if (previous) void release(previous); - } catch { - state.healthy = false; + } catch (error) { + // Later attempts follow a failure that was already logged. + if (!state.watchers) + console.warn(`Cannot watch worktree at ${root}: ${String(error)}`); } }; @@ -811,9 +829,12 @@ export class LocalReviewData { return prefix === null || rootEventMatters(name, state.tracked); }; - await Promise.all([ - watch(vcs.rootPath, ignored, rootMatters), - common && + const watchers = new Map([ + [vcs.rootPath, () => watch(vcs.rootPath, ignored, rootMatters)], + ]); + + if (common) + watchers.set(common, () => watch( common, INERT_GIT_PATHS.map((directory) => path.join(common, directory)), @@ -823,22 +844,45 @@ export class LocalReviewData { return prefix === null || gitEventMatters(name, prefix); }, ), - ]); + ); + + await Promise.all([...watchers.values()].map((subscribe) => subscribe())); + state.watchers = watchers; } + const worktree = entry; + + const unwatched = [...(worktree.watchers ?? [])].filter( + ([root]) => !worktree.subscriptions.has(root), + ); + + // A watched worktree is inspected again once an event changes the epoch. + // Without a watcher nothing reports changes, so an inspection is only + // good for a while: it stats every tracked file, so not on every read. if ( - entry.inspection && - entry.inspectedEpoch === entry.epoch && - entry.healthy && - entry.subscriptions.size + worktree.inspection && + (worktree.watchers && !unwatched.length + ? worktree.inspectedEpoch === worktree.epoch + : performance.now() - worktree.inspectedAt < UNWATCHED_INSPECTION_MS) ) - return { ...entry.inspection, forks: entry.forks }; + return { ...worktree.inspection, forks: worktree.forks }; + + // Subscribing before inspecting leaves no gap for a change to fall in. + if (unwatched.length) { + worktree.rewatching ??= Promise.all( + unwatched.map(([, subscribe]) => subscribe()), + ).finally(() => { + worktree.rewatching = undefined; + }); + await worktree.rewatching; + } const epoch = entry.epoch; const { files, ...inspected } = await inspectWorktree(repositoryId, vcs); entry.inspection = inspected; entry.tracked = trackedDirectories(files); entry.inspectedEpoch = epoch; + entry.inspectedAt = performance.now(); entry.forks = new Map(); return { ...inspected, forks: entry.forks };