mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-19 23:05:21 +00:00
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.
This commit is contained in:
@@ -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,
|
||||
);
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { resolve, relative } from "node:path";
|
||||
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 { pollDiffMetadata } from "./metadata-poll.js";
|
||||
|
||||
export interface DiffDependencies {
|
||||
cwd: string;
|
||||
@@ -14,78 +15,140 @@ export interface DiffDependencies {
|
||||
index: boolean;
|
||||
worktree: boolean;
|
||||
}
|
||||
// Own metadata observations here; comparison managers declare inputs rather than watching paths.
|
||||
|
||||
type MetadataListener = (paths: string[] | null) => void;
|
||||
type Release = () => Promise<void>;
|
||||
|
||||
interface MetadataObservation {
|
||||
listeners: Set<MetadataListener>;
|
||||
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();
|
||||
private roots = new Map<string, MetadataObservation>();
|
||||
|
||||
constructor(private workspaceGit: Pick<WorkspaceGitService, "requestWorkingTreeWatch">) {}
|
||||
|
||||
async declareDependencies(
|
||||
input: DiffDependencies,
|
||||
changed: (kind: "endpoints" | "interpretation") => void,
|
||||
): Promise<() => Promise<void>> {
|
||||
): Promise<Release> {
|
||||
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;
|
||||
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 release: Release[] = [];
|
||||
const close = async () => {
|
||||
await Promise.all(release.map((unsubscribe) => unsubscribe()));
|
||||
};
|
||||
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) {
|
||||
release.push(
|
||||
await this.observeMetadata(root, root === commonRoot, (paths) => {
|
||||
if (
|
||||
hasMutableEndpoints &&
|
||||
(paths === null ||
|
||||
paths.some((path) =>
|
||||
path === "index"
|
||||
? input.index
|
||||
: scopes.some(
|
||||
(scope) => classifyGitMetadataPath(scope, path).kind !== "ignore",
|
||||
),
|
||||
))
|
||||
) {
|
||||
changed("endpoints");
|
||||
polling ??= setInterval(() => changed("endpoints"), 5000);
|
||||
return;
|
||||
} else if (
|
||||
paths === null ||
|
||||
paths.some((path) =>
|
||||
scopes.some((scope) => affectsGitDiffInterpretation(scope, path)),
|
||||
)
|
||||
) {
|
||||
changed("interpretation");
|
||||
}
|
||||
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")] },
|
||||
}),
|
||||
);
|
||||
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()));
|
||||
};
|
||||
release.push(async () => workingTree.unsubscribe());
|
||||
return close;
|
||||
} catch (error) {
|
||||
if (polling) clearInterval(polling);
|
||||
await workingTree?.unsubscribe();
|
||||
await Promise.all(subscriptions.map((subscription) => subscription.unsubscribe()));
|
||||
await close();
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private async observeMetadata(
|
||||
root: string,
|
||||
common: boolean,
|
||||
listener: MetadataListener,
|
||||
): Promise<Release> {
|
||||
let observation = this.roots.get(root);
|
||||
if (!observation) {
|
||||
observation = { listeners: new Set(), ready: Promise.resolve() };
|
||||
this.roots.set(root, observation);
|
||||
observation.ready = this.openMetadata(root, common, observation);
|
||||
}
|
||||
const current = observation;
|
||||
current.listeners.add(listener);
|
||||
await current.ready;
|
||||
return async () => {
|
||||
current.listeners.delete(listener);
|
||||
if (current.listeners.size) return;
|
||||
this.roots.delete(root);
|
||||
await Promise.all([current.watcher?.unsubscribe(), current.poll?.close()]);
|
||||
};
|
||||
}
|
||||
|
||||
private async openMetadata(root: string, common: boolean, observation: MetadataObservation) {
|
||||
const notify: MetadataListener = (paths) => {
|
||||
for (const listener of observation.listeners) listener(paths);
|
||||
};
|
||||
const startPoll = () => (observation.poll ??= pollDiffMetadata(root, common, notify));
|
||||
const recover = async () => {
|
||||
// Resample the gap between the last watcher batch and the first poll.
|
||||
await startPoll().ready;
|
||||
notify(null);
|
||||
};
|
||||
|
||||
try {
|
||||
observation.watcher = await this.observer.subscribe(
|
||||
root,
|
||||
(error, events) => {
|
||||
if (error) {
|
||||
void recover();
|
||||
return;
|
||||
}
|
||||
notify(events.map((event) => relative(root, event.path).replaceAll("\\", "/")));
|
||||
},
|
||||
{ ignore: [resolve(root, "objects"), resolve(root, "logs"), resolve(root, "hooks")] },
|
||||
);
|
||||
} catch {
|
||||
await startPoll().ready;
|
||||
}
|
||||
}
|
||||
|
||||
getObservationCount(): number {
|
||||
return this.observer.getDiagnostics().activeObservationCount;
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
await this.observer.close();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { stat } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { createRunGitCommand } from "../../utils/run-git-command.js";
|
||||
import { GIT_METADATA_EVENT_RULES } from "../git-metadata-event-rules.js";
|
||||
|
||||
const FIRST_DELAY_MS = 5000;
|
||||
const MAX_DELAY_MS = 60000;
|
||||
const readRefs = createRunGitCommand("diff-metadata-poll", { priority: "background" });
|
||||
const metadataFiles = [
|
||||
...new Set(
|
||||
GIT_METADATA_EVENT_RULES.filter((rule) => rule.match === "exact").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";
|
||||
}
|
||||
}
|
||||
|
||||
async function refsIdentity(root: string): Promise<string> {
|
||||
try {
|
||||
const refs = await readRefs(
|
||||
["--git-dir=.", "for-each-ref", "--format=%(refname) %(objectname) %(symref)"],
|
||||
{ cwd: root, timeout: 5000 },
|
||||
);
|
||||
if (refs.truncated) return "truncated";
|
||||
return createHash("sha256").update(refs.stdout).digest("hex");
|
||||
} catch {
|
||||
return "unavailable";
|
||||
}
|
||||
}
|
||||
|
||||
// Git resolves loose, packed and reftable refs in one read independent of the
|
||||
// number of comparisons. Report the ref snapshot as a change in the ref namespace.
|
||||
export function pollDiffMetadata(
|
||||
root: string,
|
||||
common: boolean,
|
||||
changed: (paths: string[]) => void,
|
||||
) {
|
||||
let closed = false;
|
||||
let delay = FIRST_DELAY_MS;
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
let pending: Promise<void>;
|
||||
const inputs: { path: string; read: () => Promise<string> }[] = metadataFiles.map((path) => ({
|
||||
path,
|
||||
read: () => fileIdentity(join(root, path)),
|
||||
}));
|
||||
if (common) inputs.push({ path: "refs/heads", read: () => refsIdentity(root) });
|
||||
let previous: string[] | undefined;
|
||||
|
||||
async function sample() {
|
||||
const current = await Promise.all(inputs.map((input) => input.read()));
|
||||
if (closed) return;
|
||||
if (previous) {
|
||||
const baseline = previous;
|
||||
const paths = inputs
|
||||
.filter((_, index) => current[index] !== baseline[index])
|
||||
.map((input) => input.path);
|
||||
delay = paths.length ? FIRST_DELAY_MS : Math.min(delay * 2, MAX_DELAY_MS);
|
||||
if (paths.length) changed(paths);
|
||||
}
|
||||
previous = current;
|
||||
timer = setTimeout(() => {
|
||||
pending = sample();
|
||||
}, delay);
|
||||
timer.unref();
|
||||
}
|
||||
|
||||
const ready = (pending = sample());
|
||||
return {
|
||||
ready,
|
||||
async close() {
|
||||
closed = true;
|
||||
clearTimeout(timer);
|
||||
await pending;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user