Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions packages/review/src/review-api/local-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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[] = [];
Expand All @@ -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,
Expand All @@ -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 () => {
Expand Down
74 changes: 59 additions & 15 deletions packages/review/src/review-api/local-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -685,8 +688,12 @@ export class LocalReviewData {
epoch: number;
inspectedEpoch: number;
subscriptions: Map<string, AsyncSubscription>;
healthy: boolean;
/** Subscribes to each watched root. Unset until the first attempt ends. */
watchers?: Map<string, () => Promise<void>>;
/** Shared by concurrent reads that find a watcher missing. */
rewatching?: Promise<unknown>;
inspection?: Omit<Awaited<ReturnType<typeof inspectWorktree>>, "files">;
inspectedAt: number;
tracked?: Set<string>;
/** Rereads ignored directories after one may have appeared. */
ignoring?: NodeJS.Timeout;
Expand All @@ -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);
Expand All @@ -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 (
Expand All @@ -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)}`);
}
};

Expand Down Expand Up @@ -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)),
Expand All @@ -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 };
Expand Down