Skip to content

Commit 4ad3059

Browse files
willsheldonclaude
andcommitted
fix(server): keep git repository detection cached for minutes
Repository detection spawns three git processes per cwd, and the result was only cached for two seconds. Every VCS call past that window re-ran them, which dominates latency when many projects refresh at once. Whether a directory is a git repository is stable, so the positive result now holds for five minutes. Negative results are still never cached, so a fresh `git init` is picked up on the next call. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 357b8d5 commit 4ad3059

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

‎apps/server/src/vcs/VcsDriverRegistry.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { assert, it, describe } from "@effect/vitest";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
3+
import * as Duration from "effect/Duration";
34
import * as Effect from "effect/Effect";
45
import * as Layer from "effect/Layer";
6+
import { TestClock } from "effect/testing";
57
import { ChildProcessSpawner } from "effect/unstable/process";
68

79
import * as VcsProcess from "./VcsProcess.ts";
@@ -93,6 +95,54 @@ describe("VcsDriverRegistry", () => {
9395
}).pipe(Effect.provide(layer));
9496
});
9597

98+
it.effect("keeps repository detection cached for minutes, not seconds", () => {
99+
const calls: VcsProcess.VcsProcessInput[] = [];
100+
const layer = Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe(
101+
Layer.provide(NodeServices.layer),
102+
Layer.provide(
103+
Layer.mock(VcsProjectConfig.VcsProjectConfig)({
104+
resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"),
105+
}),
106+
),
107+
Layer.provide(
108+
Layer.mock(VcsProcess.VcsProcess)({
109+
run: (input) =>
110+
Effect.sync(() => {
111+
calls.push(input);
112+
const command = normalizeGitArgs(input.args).join(" ");
113+
if (command === "rev-parse --is-inside-work-tree") {
114+
return processOutput("true\n");
115+
}
116+
if (command === "rev-parse --show-toplevel") {
117+
return processOutput("/repo\n");
118+
}
119+
if (command === "rev-parse --git-common-dir") {
120+
return processOutput("/repo/.git\n");
121+
}
122+
return processOutput("");
123+
}),
124+
}),
125+
),
126+
);
127+
128+
return Effect.gen(function* () {
129+
const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
130+
yield* registry.resolve({ cwd: "/repo", requestedKind: "git" });
131+
const callsAfterFirstResolve = calls.length;
132+
assert.isAbove(callsAfterFirstResolve, 0);
133+
134+
// Detection used to expire after two seconds, so every later VCS call
135+
// spawned git again for the same checkout.
136+
yield* TestClock.adjust(Duration.minutes(1));
137+
yield* registry.resolve({ cwd: "/repo", requestedKind: "git" });
138+
assert.strictEqual(calls.length, callsAfterFirstResolve);
139+
140+
yield* TestClock.adjust(Duration.minutes(6));
141+
yield* registry.resolve({ cwd: "/repo", requestedKind: "git" });
142+
assert.isAbove(calls.length, callsAfterFirstResolve);
143+
}).pipe(Effect.provide(Layer.merge(TestClock.layer(), layer)));
144+
});
145+
96146
it.effect("detects a repository created after a negative lookup", () => {
97147
let insideWorkTreeChecks = 0;
98148
const layer = Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe(

‎apps/server/src/vcs/VcsDriverRegistry.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import * as VcsProjectConfig from "./VcsProjectConfig.ts";
1212
import * as VcsDriver from "./VcsDriver.ts";
1313

1414
const DETECTION_CACHE_CAPACITY = 2_048;
15-
const DETECTION_CACHE_TTL = Duration.seconds(2);
15+
// Detection spawns three git processes per cwd. A two second window meant
16+
// every VCS call past that re-ran them, which dominates latency when many
17+
// projects refresh at once. Whether a directory is a git repository is stable,
18+
// and a negative result is still never cached, so `git init` is picked up at
19+
// once.
20+
const DETECTION_CACHE_TTL = Duration.minutes(5);
1621

1722
export interface VcsDriverResolveInput {
1823
readonly cwd: string;

0 commit comments

Comments
 (0)