Compare commits

...
Author SHA1 Message Date
Mohamed Boudra a012702b79 fix(server): keep Changes probes working across Git versions and worktrees
Use line-delimited batch input for older Git versions. Resolve shared refs in the common Git directory so a removed worktree cannot block the remaining comparisons.
2026-11-19 05:39:03 +01:00
Mohamed Boudra 093866a3f0 fix(server): report Changes metadata fallback in daemon diagnostics
Log the watcher failure once per root and count comparisons currently using the fallback. Exercise the transition from an active watcher to polling and the release of the last comparison.
2026-11-19 05:25:36 +01:00
Mohamed Boudra 5b99f85213 fix(server): probe only revisions used by degraded Changes streams
Reuse each comparison's initial Git resolution as the baseline. Batched ref lookups run after subscription setup, stay bounded when the repository has a large ref table, and refresh only comparisons with changed inputs.
2026-11-19 05:21:13 +01:00
Mohamed Boudra 5e964a3847 fix(server): keep Changes available when Git metadata watching fails
A valid checkout could lose its first comparison when metadata observation exceeded the directory limit. Keep working-tree observation active and share a backing-off metadata sample per Git root so quiet comparisons stay idle.
2026-11-19 05:05:37 +01:00
9 changed files with 841 additions and 91 deletions
@@ -50,7 +50,10 @@ export class CheckoutDiffManager {
workspaceGitService: Pick<WorkspaceGitService, "requestWorkingTreeWatch">;
workspaceRuntimeStore: WorkspaceRuntimeStore;
}) {
this.streams = new DiffSubscriptions(options.workspaceGitService);
this.streams = new DiffSubscriptions(
options.workspaceGitService,
options.logger.child({ module: "checkout-diff" }),
);
this.paseoHome = options.paseoHome;
}
readCommitFile(input: CommitFileRead) {
@@ -178,7 +181,7 @@ export class CheckoutDiffManager {
checkoutDiffTargetCount: metrics.targets,
checkoutDiffSubscriptionCount: metrics.subscriptions,
checkoutDiffWatcherCount: metrics.metadataObservers,
checkoutDiffFallbackRefreshTargetCount: 0,
checkoutDiffFallbackRefreshTargetCount: metrics.fallbackTargets,
};
}
async dispose(): Promise<void> {
@@ -1,5 +1,5 @@
import { test, expect, beforeEach, afterEach } from "vitest";
import { execSync } from "child_process";
import { execFileSync, execSync } from "child_process";
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "fs";
import { tmpdir } from "os";
import path from "path";
@@ -164,3 +164,165 @@ test("keeps the socket usable after rejecting an oversized structured diff", asy
rmSync(cwd, { recursive: true, force: true });
}
}, 120000);
test.skipIf(process.platform !== "linux")(
"Changes opens and follows Git updates when metadata exceeds the watcher limit",
async () => {
const cwd = tmpCwd();
const git = (...args: string[]) => execFileSync("git", args, { cwd, encoding: "utf8" }).trim();
try {
initGitRepo(cwd);
commitFile(cwd, "tracked.txt", "base\n");
const head = git("rev-parse", "HEAD");
git("switch", "-c", "topic");
commitFile(cwd, "next.txt", "next\n");
const next = git("rev-parse", "HEAD");
git("reset", "--hard", head);
execFileSync("git", ["update-ref", "--stdin"], {
cwd,
input: Array.from(
{ length: 5010 },
(_, i) => `create refs/heads/overflow-${i}/branch ${head}\n`,
).join(""),
});
writeFileSync(path.join(cwd, "tracked.txt"), "edit\n");
const changes = ctx.client.observeDiff({
cwd,
base: { kind: "rev", rev: "main" },
head: { kind: "worktree" },
mergeBase: true,
});
let current: import("@getpaseo/client").DiffState | undefined;
const unsubscribe = changes.subscribe({
snapshot: (state) => {
current = state;
},
update: () => {},
});
try {
await expect
.poll(() => current, { timeout: 12000 })
.toMatchObject({
phase: "ready",
files: [{ path: "tracked.txt" }],
});
const legacy = ctx.client.observeCheckoutDiff(cwd, { mode: "all", baseRef: "main" });
try {
expect(await legacy.ready).toMatchObject({
error: null,
files: [{ path: "tracked.txt" }],
});
} finally {
await legacy.release();
}
expect(
await ctx.client.getCheckoutDiff(cwd, { mode: "all", baseRef: "main" }),
).toMatchObject({ error: null, files: [{ path: "tracked.txt" }] });
writeFileSync(path.join(cwd, "working.txt"), "untracked\n");
await expect
.poll(() => current, { timeout: 3000 })
.toMatchObject({
phase: "ready",
files: [{ path: "tracked.txt" }, { path: "working.txt" }],
});
const staged = ctx.client.observeDiff({
cwd,
base: { kind: "rev", rev: head },
head: { kind: "index" },
});
const pinned = ctx.client.observeDiff({
cwd,
base: { kind: "empty" },
head: { kind: "rev", rev: head },
});
const selected = ctx.client.observeDiff({
cwd,
base: { kind: "rev", rev: head },
head: { kind: "rev", rev: "main" },
});
let stagedState: typeof current;
let pinnedState: typeof current;
let selectedState: typeof current;
const stopStaged = staged.subscribe({
snapshot: (state) => {
stagedState = state;
},
update: () => {},
});
const stopPinned = pinned.subscribe({
snapshot: (state) => {
pinnedState = state;
},
update: () => {},
});
const stopSelected = selected.subscribe({
snapshot: (state) => {
selectedState = state;
},
update: () => {},
});
try {
await expect.poll(() => stagedState).toMatchObject({ phase: "ready", files: [] });
await expect
.poll(() => pinnedState)
.toMatchObject({
phase: "ready",
files: [{ path: "tracked.txt", status: "ok" }],
});
await expect.poll(() => selectedState).toMatchObject({ phase: "ready", files: [] });
git("add", "tracked.txt");
writeFileSync(path.join(cwd, ".git", "info", "attributes"), "tracked.txt -diff\n");
git("update-ref", "refs/heads/main", next);
await expect
.poll(() => selectedState, { timeout: 17000 })
.toMatchObject({
phase: "ready",
files: [{ path: "next.txt" }],
resolved: { headSha: next },
});
await expect
.poll(() => stagedState)
.toMatchObject({
phase: "ready",
files: [{ path: "tracked.txt" }],
});
await expect
.poll(() => pinnedState)
.toMatchObject({
phase: "ready",
files: [{ path: "tracked.txt", status: "binary" }],
});
git("symbolic-ref", "HEAD", "refs/heads/main");
await expect
.poll(() => current, { timeout: 17000 })
.toMatchObject({
phase: "ready",
resolved: { mergeBaseSha: next },
files: [{ path: "next.txt" }, { path: "tracked.txt" }, { path: "working.txt" }],
});
expect(
await ctx.client.readDiff({
cwd,
base: { kind: "rev", rev: "missing-branch" },
head: { kind: "worktree" },
}),
).toMatchObject({ phase: "error", error: { code: "REF_NOT_FOUND" } });
} finally {
stopStaged();
stopPinned();
stopSelected();
await Promise.all([staged.release(), pinned.release(), selected.release()]);
}
} finally {
unsubscribe();
await changes.release();
}
} finally {
rmSync(cwd, { recursive: true, force: true });
}
},
45000,
);
+165 -59
View File
@@ -1,91 +1,197 @@
import { resolve, relative } from "node:path";
import type { Logger } from "pino";
import { runGitCommand } from "../../utils/run-git-command.js";
import { createFileObserver, type FileObserverSubscription } from "../file-observer/index.js";
import {
affectsGitDiffInterpretation,
classifyGitMetadataPath,
type GitMetadataScope,
} from "../git-metadata-event-rules.js";
import { resolve } from "node:path";
import { runGitCommand } from "../../utils/run-git-command.js";
import type { WorkspaceGitService } from "../workspace-git-service.js";
import { createFileObserver, type FileObserverSubscription } from "../file-observer/index.js";
import type { RevisionWatch } from "./revision-probe.js";
import { pollDiffMetadata } from "./metadata-poll.js";
export interface DiffDependencies {
cwd: string;
refs: string[];
export interface DiffDependencies extends Omit<RevisionWatch, "changed" | "gitDirectory"> {
movingRevision: boolean;
index: boolean;
worktree: boolean;
}
// Own metadata observations here; comparison managers declare inputs rather than watching paths.
type MetadataListener = (paths: string[] | null) => void;
export interface DiffDependencySubscription {
release(): Promise<void>;
isPolling(): boolean;
}
interface MetadataObservation {
listeners: Set<MetadataListener>;
revisions: Set<RevisionWatch>;
ready: Promise<void>;
watcher?: FileObserverSubscription;
poll?: ReturnType<typeof pollDiffMetadata>;
}
// Comparison managers declare inputs. This observer owns their shared Git metadata roots.
export class DiffDependencyObserver {
private observer = createFileObserver();
constructor(private workspaceGit: Pick<WorkspaceGitService, "requestWorkingTreeWatch">) {}
private roots = new Map<string, MetadataObservation>();
constructor(
private workspaceGit: Pick<WorkspaceGitService, "requestWorkingTreeWatch">,
private logger?: Pick<Logger, "warn">,
) {}
async declareDependencies(
input: DiffDependencies,
changed: (kind: "endpoints" | "interpretation") => void,
): Promise<() => Promise<void>> {
const hasMutableEndpoints = input.refs.length > 0 || input.index || input.worktree;
let polling: ReturnType<typeof setInterval> | null = null;
const subscriptions: FileObserverSubscription[] = [];
let workingTree: Awaited<ReturnType<WorkspaceGitService["requestWorkingTreeWatch"]>> | null =
null;
): Promise<DiffDependencySubscription> {
const hasMutableEndpoints =
input.movingRevision || input.checkoutBase !== undefined || input.index || input.worktree;
const rootPaths = await runGitCommand(["rev-parse", "--absolute-git-dir", "--git-common-dir"], {
cwd: input.cwd,
});
const [gitDir, commonDir] = rootPaths.stdout.split(/\r?\n/);
if (!gitDir || !commonDir) throw new Error("Git returned incomplete metadata roots");
const worktreeRoot = resolve(input.cwd, gitDir);
const commonRoot = resolve(input.cwd, commonDir);
const roots: [string, GitMetadataScope[]][] =
commonRoot === worktreeRoot
? [[worktreeRoot, ["worktree", "common"]]]
: [
[worktreeRoot, ["worktree"]],
[commonRoot, ["common"]],
];
const metadata: DiffDependencySubscription[] = [];
const releaseMetadata = async () => {
await Promise.all(metadata.map((subscription) => subscription.release()));
};
try {
const paths = await runGitCommand(["rev-parse", "--absolute-git-dir", "--git-common-dir"], {
cwd: input.cwd,
});
const [gitDir, commonDir] = paths.stdout.split(/\r?\n/);
if (!gitDir || !commonDir) throw new Error("Git returned incomplete metadata roots");
const roots = new Map<string, GitMetadataScope[]>();
for (const [directory, scope] of [
[gitDir, "worktree"],
[commonDir, "common"],
] as const) {
const root = resolve(input.cwd, directory);
const scopes = roots.get(root) ?? [];
scopes.push(scope);
roots.set(root, scopes);
}
for (const [root, scopes] of roots) {
const subscription = await this.observer.subscribe(
root,
(error, events) => {
if (error) {
changed("endpoints");
polling ??= setInterval(() => changed("endpoints"), 5000);
return;
}
let interpretation = false;
const endpoints = events.some((event) => {
const path = event.path.slice(root.length + 1).replaceAll("\\", "/");
if (scopes.some((scope) => affectsGitDiffInterpretation(scope, path)))
interpretation = true;
if (!hasMutableEndpoints) return false;
if (path === "index") return input.index;
return scopes.some((scope) => classifyGitMetadataPath(scope, path).kind !== "ignore");
});
if (endpoints) changed("endpoints");
else if (interpretation) changed("interpretation");
},
{ ignore: [resolve(root, "objects"), resolve(root, "logs"), resolve(root, "hooks")] },
const isWorktree = scopes.includes("worktree");
metadata.push(
await this.observeMetadata(
root,
root === commonRoot
? { ...input, gitDirectory: worktreeRoot, changed: () => changed("endpoints") }
: null,
(paths) => {
if (
hasMutableEndpoints &&
(paths === null ||
paths.some((path) => {
if (path === "index") return isWorktree && input.index;
if (path === "HEAD") return isWorktree;
return scopes.some(
(scope) => classifyGitMetadataPath(scope, path).kind !== "ignore",
);
}))
) {
changed("endpoints");
} else if (
paths === null ||
paths.some((path) =>
scopes.some((scope) => affectsGitDiffInterpretation(scope, path)),
)
) {
changed("interpretation");
}
},
),
);
subscriptions.push(subscription);
}
workingTree = await this.workspaceGit.requestWorkingTreeWatch(input.cwd, () =>
const workingTree = await this.workspaceGit.requestWorkingTreeWatch(input.cwd, () =>
changed(input.worktree ? "endpoints" : "interpretation"),
);
return async () => {
if (polling) clearInterval(polling);
await workingTree?.unsubscribe();
await Promise.all(subscriptions.map((subscription) => subscription.unsubscribe()));
return {
isPolling: () => metadata.some((subscription) => subscription.isPolling()),
async release() {
try {
workingTree.unsubscribe();
} finally {
await releaseMetadata();
}
},
};
} catch (error) {
if (polling) clearInterval(polling);
await workingTree?.unsubscribe();
await Promise.all(subscriptions.map((subscription) => subscription.unsubscribe()));
await releaseMetadata();
throw error;
}
}
private async observeMetadata(
root: string,
revisions: RevisionWatch | null,
listener: MetadataListener,
): Promise<DiffDependencySubscription> {
let observation = this.roots.get(root);
if (!observation) {
observation = { listeners: new Set(), revisions: new Set(), ready: Promise.resolve() };
this.roots.set(root, observation);
observation.ready = this.openMetadata(root, observation);
}
const current = observation;
current.listeners.add(listener);
if (revisions) current.revisions.add(revisions);
await current.ready;
return {
isPolling: () => current.poll !== undefined,
release: async () => {
if (!current.listeners.delete(listener)) return;
if (revisions) current.revisions.delete(revisions);
if (current.listeners.size) return;
this.roots.delete(root);
await Promise.all([current.watcher?.unsubscribe(), current.poll?.close()]);
},
};
}
private async openMetadata(root: string, observation: MetadataObservation) {
const notify: MetadataListener = (paths) => {
for (const listener of observation.listeners) listener(paths);
};
const startPoll = (error: unknown) => {
if (!observation.poll) {
this.logger?.warn(
{ err: error, root },
"Diff metadata watcher failed; using degraded polling",
);
let reported = false;
observation.poll = pollDiffMetadata(root, observation.revisions, notify, (err, cwd) => {
if (reported) return;
reported = true;
this.logger?.warn({ err, root, cwd }, "Failed to probe revisions for Changes");
});
}
return observation.poll;
};
const recover = async (error: Error) => {
// Resample the gap between the last watcher batch and the first poll.
await startPoll(error).ready;
notify(null);
};
try {
observation.watcher = await this.observer.subscribe(
root,
(error, events) => {
if (error) {
void recover(error);
return;
}
notify(events.map((event) => relative(root, event.path).replaceAll("\\", "/")));
},
{ ignore: [resolve(root, "objects"), resolve(root, "logs"), resolve(root, "hooks")] },
);
} catch (error) {
await startPoll(error).ready;
}
}
getObservationCount(): number {
return this.observer.getDiagnostics().activeObservationCount;
}
async close(): Promise<void> {
await this.observer.close();
}
+22 -5
View File
@@ -21,6 +21,15 @@ import { parseDiff, highlightDiffWithFileContent } from "../utils/diff-highlight
export type Compare = Omit<DiffSubscribeRequest, "type" | "requestId"> & {
checkoutBase?: { compare: CheckoutDiffCompare; paseoHome: string };
};
export interface ComparisonRevisions {
refs: Map<string, string | null>;
checkoutBase?: string | null;
}
export function isMovingRevision(rev: string): boolean {
return !/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/i.test(rev);
}
export interface ListedFile {
entry: DiffEntry;
identity: string;
@@ -90,6 +99,7 @@ async function resolveEndpoint(
cwd: string,
endpoint: DiffEndpoint,
indexFile: string | null,
revisions?: ComparisonRevisions,
): Promise<string> {
if (endpoint.kind === "empty") return EMPTY;
if (endpoint.kind === "index") {
@@ -108,7 +118,9 @@ async function resolveEndpoint(
acceptExitCodes: [0, 128],
},
);
if (result.exitCode === 0) return result.stdout.trim();
const sha = result.exitCode === 0 ? result.stdout.trim() : null;
if (isMovingRevision(ref)) revisions?.refs.set(ref, sha);
if (sha !== null) return sha;
if (ref === "HEAD") {
const unborn = await runGitCommand(["rev-parse", "--verify", "HEAD"], {
cwd,
@@ -142,11 +154,16 @@ function parseStats(
}
return stats;
}
export async function listComparison(compare: Compare): Promise<Comparison> {
export async function listComparison(
compare: Compare,
revisions?: ComparisonRevisions,
): Promise<Comparison> {
if (compare.checkoutBase) {
if (revisions) revisions.checkoutBase = null;
const selected = await resolveCheckoutDiffBase(compare.cwd, compare.checkoutBase.compare, {
paseoHome: compare.checkoutBase.paseoHome,
});
if (revisions) revisions.checkoutBase = selected;
if (!selected) throw new DiffReadError("REF_NOT_FOUND", "Comparison base is unavailable");
compare = { ...compare, base: { kind: "rev", rev: selected } };
}
@@ -157,14 +174,14 @@ export async function listComparison(compare: Compare): Promise<Comparison> {
if (compare.base.kind === "index" || compare.head.kind === "index")
privateIndex = await snapshotComparisonIndex(cwd);
const indexFile = privateIndex ? privateIndex.indexFile : null;
const baseSha = await resolveEndpoint(cwd, compare.base, indexFile);
const headSha = await resolveEndpoint(cwd, compare.head, indexFile);
const baseSha = await resolveEndpoint(cwd, compare.base, indexFile, revisions);
const headSha = await resolveEndpoint(cwd, compare.head, indexFile, revisions);
let mergeBaseSha = "";
let comparisonBaseSha = baseSha;
if (compare.mergeBase) {
const headCommit =
compare.head.kind === "index"
? await resolveEndpoint(cwd, { kind: "rev", rev: "HEAD" }, indexFile)
? await resolveEndpoint(cwd, { kind: "rev", rev: "HEAD" }, indexFile, revisions)
: headSha;
if (headCommit === EMPTY) comparisonBaseSha = EMPTY;
else if (baseSha !== EMPTY) {
+28 -19
View File
@@ -1,3 +1,4 @@
import type { Logger } from "pino";
import { TOTAL_DIFF_MAX_BYTES } from "../../utils/checkout-git.js";
import { DEFAULT_MAX_OUTPUT_BYTES, runGitCommand } from "../../utils/run-git-command.js";
import { expandTilde, createRealpathAwarePathMatcher } from "../../utils/path.js";
@@ -19,13 +20,15 @@ import type {
import type { WorkspaceGitService } from "../workspace-git-service.js";
import {
listComparison,
isMovingRevision,
validateCompare,
worktreeRoot,
DiffReadError,
type Compare,
type Comparison,
type ComparisonRevisions,
} from "./git.js";
import { DiffDependencyObserver } from "./dependencies.js";
import { DiffDependencyObserver, type DiffDependencySubscription } from "./dependencies.js";
interface RecordCache {
identity: string;
@@ -42,6 +45,7 @@ interface Target {
compare: Compare;
listeners: Set<Listener>;
revision: number;
revisions: ComparisonRevisions | null;
committed: number;
cache: Map<string, RecordCache>;
entries: DiffEntry[];
@@ -53,7 +57,7 @@ interface Target {
checkInterpretation: (() => Promise<boolean>) | null;
interpretationCheck: Promise<void> | null;
interpretationDirty: boolean;
release: (() => Promise<void>) | null;
dependencies: DiffDependencySubscription | null;
opening: Promise<void> | null;
closed: boolean;
timer: ReturnType<typeof setTimeout> | null;
@@ -73,16 +77,18 @@ export class DiffSubscriptions {
private dependencies: DiffDependencyObserver;
private computations = 0;
private highlightCalls = 0;
constructor(workspaceGit: Pick<WorkspaceGitService, "requestWorkingTreeWatch">) {
this.dependencies = new DiffDependencyObserver(workspaceGit);
constructor(
workspaceGit: Pick<WorkspaceGitService, "requestWorkingTreeWatch">,
logger?: Pick<Logger, "warn">,
) {
this.dependencies = new DiffDependencyObserver(workspaceGit, logger);
}
getMetrics() {
const targets = [...this.targets.values()];
return {
targets: this.targets.size,
subscriptions: [...this.targets.values()].reduce(
(sum, target) => sum + target.listeners.size,
0,
),
targets: targets.length,
subscriptions: targets.reduce((sum, target) => sum + target.listeners.size, 0),
fallbackTargets: targets.filter((target) => target.dependencies?.isPolling()).length,
computations: this.computations,
highlightCalls: this.highlightCalls,
metadataObservers: this.dependencies.getObservationCount(),
@@ -141,6 +147,7 @@ export class DiffSubscriptions {
compare,
listeners: new Set(),
revision: 0,
revisions: null,
committed: 0,
cache: new Map(),
entries: [],
@@ -152,7 +159,7 @@ export class DiffSubscriptions {
checkInterpretation: null,
interpretationCheck: null,
interpretationDirty: false,
release: null,
dependencies: null,
opening: null,
closed: false,
timer: null,
@@ -164,11 +171,11 @@ export class DiffSubscriptions {
.declareDependencies(
{
cwd: compare.cwd,
refs: [compare.base, compare.head].flatMap((endpoint) =>
endpoint.kind === "rev" && !/^[a-f0-9]{40,64}$/.test(endpoint.rev)
? [endpoint.rev]
: [],
movingRevision: [compare.base, compare.head].some(
(endpoint) => endpoint.kind === "rev" && isMovingRevision(endpoint.rev),
),
checkoutBase: compare.checkoutBase,
getRevisions: () => captured.revisions,
index: compare.base.kind === "index" || compare.head.kind !== "rev",
worktree: compare.head.kind === "worktree",
},
@@ -177,8 +184,8 @@ export class DiffSubscriptions {
else this.invalidate(captured);
},
)
.then((release) => {
captured.release = release;
.then((dependencies) => {
captured.dependencies = dependencies;
return undefined;
});
} catch (error) {
@@ -219,7 +226,7 @@ export class DiffSubscriptions {
current.closed = true;
if (current.timer) clearTimeout(current.timer);
this.targets.delete(key);
await current.release?.();
await current.dependencies?.release();
await current.refresh;
await current.interpretationCheck;
},
@@ -329,7 +336,9 @@ export class DiffSubscriptions {
let began = false;
try {
this.computations++;
comparison = await listComparison(target.compare);
const revisions: ComparisonRevisions = { refs: new Map() };
target.revisions = revisions;
comparison = await listComparison(target.compare, revisions);
target.checkInterpretation = comparison.hasInterpretationChanged;
const resolved = {
baseSha: comparison.baseSha,
@@ -450,7 +459,7 @@ export class DiffSubscriptions {
target.closed = true;
if (target.timer) clearTimeout(target.timer);
await target.opening;
await target.release?.();
await target.dependencies?.release();
await target.refresh;
await target.interpretationCheck;
}
@@ -0,0 +1,76 @@
import { stat } from "node:fs/promises";
import { join } from "node:path";
import { GIT_METADATA_EVENT_RULES } from "../git-metadata-event-rules.js";
import { checkDiffRevisions, type RevisionWatch } from "./revision-probe.js";
const FIRST_DELAY_MS = 5000;
const MAX_DELAY_MS = 60000;
const metadataFiles = [
...new Set(
GIT_METADATA_EVENT_RULES.filter(
(rule) => rule.match === "exact" && rule.id !== "packed-refs",
).map((rule) => rule.path),
),
];
async function fileIdentity(path: string): Promise<string> {
try {
const info = await stat(path, { bigint: true });
return `${info.dev}:${info.ino}:${info.size}:${info.mtimeNs}:${info.ctimeNs}`;
} catch (error) {
return (error as NodeJS.ErrnoException).code ?? "unavailable";
}
}
export function pollDiffMetadata(
root: string,
revisions: ReadonlySet<RevisionWatch>,
changed: (paths: string[]) => void,
probeFailed: (error: unknown, cwd: string) => void,
) {
let closed = false;
let delay = FIRST_DELAY_MS;
let timer: ReturnType<typeof setTimeout> | undefined;
let pending: Promise<void>;
let previous: string[];
function schedule() {
timer = setTimeout(() => {
pending = sample();
}, delay);
timer.unref();
}
function readFiles() {
return Promise.all(metadataFiles.map((path) => fileIdentity(join(root, path))));
}
async function sample() {
const current = await readFiles();
if (closed) return;
const paths = metadataFiles.filter((_, index) => current[index] !== previous[index]);
if (paths.length) changed(paths);
previous = current;
const refsChanged = await checkDiffRevisions(root, revisions, probeFailed);
if (closed) return;
delay = paths.length || refsChanged ? FIRST_DELAY_MS : Math.min(delay * 2, MAX_DELAY_MS);
schedule();
}
async function initialize() {
previous = await readFiles();
if (!closed) schedule();
}
// File identities establish the event boundary before the first diff. The
// comparison's own revision reads establish its ref baseline.
const ready = (pending = initialize());
return {
ready,
async close() {
closed = true;
clearTimeout(timer);
await pending;
},
};
}
@@ -0,0 +1,145 @@
import { resolveCheckoutDiffBase } from "../../utils/checkout-git.js";
import { createRunGitCommand } from "../../utils/run-git-command.js";
import type { Compare, ComparisonRevisions } from "./git.js";
const git = createRunGitCommand("diff-metadata-poll", { priority: "background" });
const TIMEOUT_MS = 5000;
const BATCH_SIZE = 128;
export interface RevisionWatch {
cwd: string;
gitDirectory: string;
checkoutBase?: Compare["checkoutBase"];
getRevisions(): ComparisonRevisions | null;
changed(): void;
}
function probeDirectory(root: string, watch: RevisionWatch, ref: string): string {
const sharedRef = /^refs\/(?:heads|remotes|tags)\/[\w./-]+$/.test(ref);
const mainHead = ref === "HEAD" && watch.gitDirectory === root;
return sharedRef || mainHead ? root : watch.cwd;
}
async function readRevisions(
cwd: string,
commonRoot: string,
refs: Set<string>,
failed: (error: unknown, cwd: string) => void,
): Promise<Map<string, string | null>> {
const result = new Map<string, string | null>();
const options = {
cwd,
timeout: TIMEOUT_MS,
envOverlay: cwd === commonRoot ? { GIT_DIR: "." } : undefined,
};
const names: string[] = [];
for (const ref of refs) {
if (!ref.includes("\n")) {
names.push(ref);
continue;
}
// Revision expressions can contain a newline even though ref names cannot.
try {
const output = await git(["rev-parse", "--verify", "--end-of-options", `${ref}^{commit}`], {
...options,
acceptExitCodes: [0, 128],
});
result.set(ref, output.exitCode === 0 ? output.stdout.trim() : null);
} catch (error) {
failed(error, cwd);
}
}
for (let index = 0; index < names.length; index += BATCH_SIZE) {
const batch = names.slice(index, index + BATCH_SIZE);
const queries = batch.map((ref) => `${ref}^{commit}`);
try {
const output = await git(["cat-file", "--batch-check=%(objectname)"], {
...options,
input: queries.join("\n") + "\n",
});
const records = output.stdout.split("\n");
if (output.truncated || records.length !== queries.length + 1) {
throw new Error("Git returned incomplete diff revision records");
}
for (const [position, query] of queries.entries()) {
const record = records[position];
if (/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/.test(record)) {
result.set(batch[position], record);
} else if (record === `${query} missing` || record === `${query} ambiguous`) {
result.set(batch[position], null);
}
}
} catch (error) {
failed(error, cwd);
}
}
return result;
}
export async function checkDiffRevisions(
root: string,
watches: ReadonlySet<RevisionWatch>,
failed: (error: unknown, cwd: string) => void,
): Promise<boolean> {
const active = [...watches].flatMap((watch) => {
const previous = watch.getRevisions();
return previous ? [{ watch, previous }] : [];
});
const requests = new Map<string, Set<string>>();
for (const { watch, previous } of active) {
for (const ref of previous.refs.keys()) {
const cwd = probeDirectory(root, watch, ref);
const refs = requests.get(cwd) ?? new Set<string>();
refs.add(ref);
requests.set(cwd, refs);
}
}
const reads = new Map(
[...requests].map(([cwd, refs]) => [cwd, readRevisions(cwd, root, refs, failed)]),
);
const checkoutBases = new Map<string, Promise<string | null | undefined>>();
async function readCheckoutBase(watch: RevisionWatch, observed: string | null) {
const base = watch.checkoutBase;
if (!base) return observed;
// An explicit qualified selection is already checked by the revision batch.
if (observed === base.compare.baseRef && /^refs\/(?:heads|remotes)\//.test(observed)) {
return observed;
}
const key = JSON.stringify([watch.cwd, base]);
let selected = checkoutBases.get(key);
if (!selected) {
selected = resolveCheckoutDiffBase(watch.cwd, base.compare, {
paseoHome: base.paseoHome,
runGitCommand: (args, options) => git(args, { ...options, timeout: TIMEOUT_MS }),
}).catch(() => undefined);
checkoutBases.set(key, selected);
}
return selected;
}
const changed = await Promise.all(
active.map(async ({ watch, previous }) => {
const base = previous.checkoutBase;
const [selected, refs] = await Promise.all([
base === undefined ? undefined : readCheckoutBase(watch, base),
Promise.all(
[...previous.refs].map(async ([ref, sha]) => {
const current = (await reads.get(probeDirectory(root, watch, ref)))?.get(ref);
return current !== undefined && current !== sha && previous.refs.get(ref) === sha;
}),
),
]);
if (
((selected !== undefined && selected !== base && previous.checkoutBase === base) ||
refs.some(Boolean)) &&
watches.has(watch) &&
watch.getRevisions() === previous
) {
watch.changed();
return true;
}
return false;
}),
);
return changed.some(Boolean);
}
@@ -1,5 +1,5 @@
import { execFileSync } from "node:child_process";
import { mkdtemp, writeFile, rm, mkdir } from "node:fs/promises";
import { mkdtemp, writeFile, rm, mkdir, rename } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { expect, test } from "vitest";
@@ -8,6 +8,9 @@ import { DiffSubscriptions } from "./index.js";
import { createFileObserver } from "../file-observer/index.js";
import {
configureGitProcessPolicy,
getGitCommandMetrics,
startGitCommandMetrics,
stopGitCommandMetrics,
runGitCommand,
runWithGitCommandPriority,
} from "../../utils/run-git-command.js";
@@ -170,6 +173,7 @@ test("shares computation, highlights only the changed file, recovers refs and re
computations: before,
highlightCalls: previousHighlightCalls,
metadataObservers: 1,
fallbackTargets: 0,
});
first.length = 0;
second.length = 0;
@@ -1259,3 +1263,231 @@ test.each(["pinned", "HEAD"] as const)(
},
30000,
);
test.skipIf(process.platform !== "linux")(
"overflow fallback opens before probing refs and refreshes only comparisons whose inputs move",
async () => {
const fixture = await createObservedWorktreeDiff("diff-overflow-");
const git = (...args: string[]) =>
execFileSync("git", args, { cwd: fixture.cwd, encoding: "utf8" }).trim();
const record = (frames: DiffFrame[]) => async (frame: DiffFrame) => {
frames.push(frame);
};
const selectedFrames: DiffFrame[] = [];
const pinnedFrames: DiffFrame[] = [];
try {
const head = git("rev-parse", "HEAD");
const selectedRef = "refs/heads/selected";
const empty = git("hash-object", "-t", "tree", "-w", "--stdin");
const moved = git(
"-c",
"commit.gpgsign=false",
"commit-tree",
empty,
"-p",
head,
"-m",
"next",
);
execFileSync("git", ["update-ref", "--stdin"], {
cwd: fixture.cwd,
input: [
`create ${selectedRef} ${head}`,
...Array.from({ length: 5010 }, (_, i) => `create refs/heads/overflow-${i}/ref ${head}`),
"",
].join("\n"),
});
// The valid packed refs alone are larger than a full Git command's output budget.
const packedPrefix = `refs/heads/${"p".repeat(180)}`;
const packed = Array.from(
{ length: 100000 },
(_, i) => `${head} ${packedPrefix}/${String(i).padStart(6, "0")}\n`,
);
await writeFile(
join(fixture.cwd, ".git", "packed-refs"),
"# pack-refs with: peeled fully-peeled sorted \n" + packed.join(""),
);
expect(git("rev-parse", "--verify", `${packedPrefix}/099999`)).toBe(head);
startGitCommandMetrics();
const selected = await fixture.subscriptions.subscribe(
{ ...fixture.compare, base: { kind: "rev", rev: selectedRef }, highlight: false },
record(selectedFrames),
);
const pinned = await fixture.subscriptions.subscribe(
{ ...fixture.compare, base: { kind: "rev", rev: head }, highlight: false },
record(pinnedFrames),
);
await Promise.all([selected.start(), pinned.start()]);
expect(selectedFrames.find((frame) => frame.opcode === DiffOpcode.End)).toMatchObject({
revision: 1,
error: null,
});
expect(pinnedFrames.find((frame) => frame.opcode === DiffOpcode.End)).toMatchObject({
revision: 1,
error: null,
});
expect(
getGitCommandMetrics().submissions.filter((command) => command.priority === "background"),
).toEqual([]);
// Move the selected ref before the first poll. The pinned comparison shares the
// same working tree and Git metadata, but the ref is unrelated to its inputs.
git("update-ref", selectedRef, moved);
await expect
.poll(() => selectedFrames.findLast((frame) => frame.opcode === DiffOpcode.Begin), {
timeout: 10000,
})
.toMatchObject({ revision: 2, baseSha: moved });
await expect
.poll(() => selectedFrames.findLast((frame) => frame.opcode === DiffOpcode.End))
.toMatchObject({ revision: 2, error: null });
expect(
getGitCommandMetrics().submissions.filter((command) => command.priority === "background"),
).toEqual([
{
args: ["cat-file", "--batch-check=%(objectname)"],
cwd: join(fixture.cwd, ".git"),
priority: "background",
},
]);
await selected.release();
await pinned.release();
expect(pinnedFrames.filter((frame) => frame.opcode === DiffOpcode.Begin)).toHaveLength(1);
expect(fixture.subscriptions.getMetrics().computations).toBe(3);
} finally {
await fixture.close();
stopGitCommandMetrics();
}
},
30000,
);
test.skipIf(process.platform !== "linux")(
"a running comparison keeps following its ref after metadata observation overflows",
async () => {
const fixture = await createObservedWorktreeDiff("diff-runtime-overflow-");
const overflow = await mkdtemp(join(tmpdir(), "diff-runtime-refs-"));
const git = (...args: string[]) =>
execFileSync("git", args, { cwd: fixture.cwd, encoding: "utf8" }).trim();
const frames: DiffFrame[] = [];
try {
git("branch", "selected");
const empty = git("hash-object", "-t", "tree", "-w", "--stdin");
const moved = git("-c", "commit.gpgsign=false", "commit-tree", empty, "-m", "next");
// Move the populated directory into the metadata watch in a single event.
for (let index = 0; index < 5010; index += 64) {
await Promise.all(
Array.from({ length: Math.min(64, 5010 - index) }, (_, offset) =>
mkdir(join(overflow, String(index + offset))),
),
);
}
const subscription = await fixture.subscriptions.subscribe(
{ ...fixture.compare, base: { kind: "rev", rev: "refs/heads/selected" }, highlight: false },
async (frame) => {
frames.push(frame);
},
);
await subscription.start();
expect(fixture.subscriptions.getMetrics()).toMatchObject({
metadataObservers: 1,
fallbackTargets: 0,
});
await rename(overflow, join(fixture.cwd, ".git", "refs", "heads", "overflow"));
await expect
.poll(() => fixture.subscriptions.getMetrics(), { timeout: 10000 })
.toMatchObject({ metadataObservers: 0, fallbackTargets: 1 });
await expect
.poll(() => frames.findLast((frame) => frame.opcode === DiffOpcode.End), { timeout: 5000 })
.toMatchObject({ revision: 2, error: null });
git("update-ref", "refs/heads/selected", moved);
await expect
.poll(() => frames.findLast((frame) => frame.opcode === DiffOpcode.Begin), {
timeout: 12000,
})
.toMatchObject({ revision: 3, baseSha: moved });
await subscription.release();
expect(fixture.subscriptions.getMetrics()).toMatchObject({
metadataObservers: 0,
fallbackTargets: 0,
});
} finally {
await fixture.close();
await rm(overflow, { recursive: true, force: true });
}
},
35000,
);
test.skipIf(process.platform !== "linux")(
"an unreachable worktree leaves shared revision probes available to other comparisons",
async () => {
const fixture = await createObservedWorktreeDiff("diff-shared-ref-");
const removed = await mkdtemp(join(tmpdir(), "diff-removed-worktree-"));
const git = (...args: string[]) =>
execFileSync("git", args, { cwd: fixture.cwd, encoding: "utf8" }).trim();
const frames: DiffFrame[] = [];
const ref = "refs/heads/selected";
try {
const head = git("rev-parse", "HEAD");
const empty = git("hash-object", "-t", "tree", "-w", "--stdin");
const moved = git(
"-c",
"commit.gpgsign=false",
"commit-tree",
empty,
"-p",
head,
"-m",
"next",
);
git("worktree", "add", "--detach", removed, head);
execFileSync("git", ["update-ref", "--stdin"], {
cwd: fixture.cwd,
input: [
`create ${ref} ${head}`,
...Array.from({ length: 5010 }, (_, i) => `create refs/heads/overflow-${i}/ref ${head}`),
"",
].join("\n"),
});
const compare = {
base: { kind: "empty" },
head: { kind: "rev", rev: ref },
highlight: false,
} as const;
const unavailable = await fixture.subscriptions.subscribe(
{ ...compare, cwd: removed },
async () => {},
);
const remaining = await fixture.subscriptions.subscribe(
{ ...compare, cwd: fixture.cwd },
async (frame) => {
frames.push(frame);
},
);
await Promise.all([unavailable.start(), remaining.start()]);
expect(frames.find((frame) => frame.opcode === DiffOpcode.Begin)).toMatchObject({
revision: 1,
headSha: head,
});
git("worktree", "remove", removed);
git("update-ref", ref, moved);
await expect
.poll(() => frames.findLast((frame) => frame.opcode === DiffOpcode.Begin), {
timeout: 12000,
})
.toMatchObject({ revision: 2, headSha: moved });
await unavailable.release();
await remaining.release();
} finally {
await fixture.close();
await rm(removed, { recursive: true, force: true });
}
},
35000,
);
+4 -4
View File
@@ -1687,10 +1687,10 @@ export async function resolveRepositoryDefaultBranch(
// A stale origin/HEAD must fall through to the available local branches.
}
const { stdout } = await getRunGitCommand(context)(["branch", "--format=%(refname:short)"], {
cwd: repoRoot,
envOverlay: READ_ONLY_GIT_ENV,
});
const { stdout } = await getRunGitCommand(context)(
["branch", "--list", "--format=%(refname:short)", "main", "master"],
{ cwd: repoRoot, envOverlay: READ_ONLY_GIT_ENV },
);
const branches = new Set(
stdout
.split("\n")