Skip to content

Commit 2ef76ff

Browse files
committed
fix(server): memoise git top-level lookup in repository identity resolver
resolve() ran `git rev-parse --show-toplevel` on every call, outside the existing identity cache, so it spawned a git subprocess each time even for the same workspace. During replayEvents enrichment that means one git process per project event on every reconnect, a big contributor to the "Some requests are slow" toast. Memoise the cwd -> top-level mapping in its own cache. A resolved top-level uses the positive TTL; an unresolved cwd (not a repo / git timed out) uses the negative TTL so a directory that later becomes a repo is still picked up. Adds a regression test counting process invocations. Part of #2037.
1 parent 20f37f3 commit 2ef76ff

2 files changed

Lines changed: 106 additions & 11 deletions

File tree

‎apps/server/src/project/Layers/RepositoryIdentityResolver.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as FileSystem from "effect/FileSystem";
66
import * as Layer from "effect/Layer";
77
import * as Path from "effect/Path";
88
import { TestClock } from "effect/testing";
9+
import { ChildProcessSpawner } from "effect/unstable/process";
910

1011
import * as ProcessRunner from "../../processRunner.ts";
1112
import { RepositoryIdentityResolver } from "../Services/RepositoryIdentityResolver.ts";
@@ -26,6 +27,33 @@ const git = (cwd: string, args: ReadonlyArray<string>) =>
2627
});
2728
}).pipe(Effect.provide(ProcessRunner.layer));
2829

30+
const makeCallCountingProcessRunner = (
31+
state: { revParseCalls: number; remoteCalls: number },
32+
fixture: { readonly topLevel: string; readonly remotesStdout: string },
33+
) =>
34+
ProcessRunner.ProcessRunner.of({
35+
run: (input) => {
36+
const succeed = (stdout: string) =>
37+
Effect.succeed({
38+
stdout,
39+
stderr: "",
40+
code: ChildProcessSpawner.ExitCode(0),
41+
timedOut: false,
42+
stdoutTruncated: false,
43+
stderrTruncated: false,
44+
} satisfies ProcessRunner.ProcessRunOutput);
45+
if (input.args.includes("rev-parse") && input.args.includes("--show-toplevel")) {
46+
state.revParseCalls += 1;
47+
return succeed(fixture.topLevel);
48+
}
49+
if (input.args.includes("remote") && input.args.includes("-v")) {
50+
state.remoteCalls += 1;
51+
return succeed(fixture.remotesStdout);
52+
}
53+
return succeed("");
54+
},
55+
});
56+
2957
const makeRepositoryIdentityResolverTestLayer = (options: {
3058
readonly positiveCacheTtl?: Duration.Input;
3159
readonly negativeCacheTtl?: Duration.Input;
@@ -236,4 +264,42 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
236264
),
237265
),
238266
);
267+
268+
it.effect(
269+
"resolves the git top-level only once across repeated resolves of the same workspace",
270+
() =>
271+
Effect.gen(function* () {
272+
const state = { revParseCalls: 0, remoteCalls: 0 };
273+
const cwd = "/workspace/project";
274+
const resolverLayer = Layer.effect(
275+
RepositoryIdentityResolver,
276+
makeRepositoryIdentityResolver({ cacheCapacity: 16 }),
277+
).pipe(
278+
Layer.provide(
279+
Layer.succeed(
280+
ProcessRunner.ProcessRunner,
281+
makeCallCountingProcessRunner(state, {
282+
topLevel: cwd,
283+
remotesStdout:
284+
"origin\[email protected]:T3Tools/t3code.git (fetch)\n" +
285+
"origin\[email protected]:T3Tools/t3code.git (push)\n",
286+
}),
287+
),
288+
),
289+
);
290+
291+
yield* Effect.gen(function* () {
292+
const resolver = yield* RepositoryIdentityResolver;
293+
const first = yield* resolver.resolve(cwd);
294+
const second = yield* resolver.resolve(cwd);
295+
296+
expect(first?.canonicalKey).toBe("github.com/t3tools/t3code");
297+
expect(second?.canonicalKey).toBe("github.com/t3tools/t3code");
298+
// The top-level lookup must be memoised: replaying many events for the
299+
// same workspace should not re-spawn `git rev-parse --show-toplevel`.
300+
expect(state.revParseCalls).toBe(1);
301+
expect(state.remoteCalls).toBe(1);
302+
}).pipe(Effect.provide(resolverLayer));
303+
}),
304+
);
239305
});

‎apps/server/src/project/Layers/RepositoryIdentityResolver.ts‎

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,17 @@ interface RepositoryIdentityResolverOptions {
8383
readonly negativeCacheTtl?: Duration.Input;
8484
}
8585

86+
interface RepositoryIdentityCacheKey {
87+
/** The git top-level path when found, otherwise the original cwd. */
88+
readonly cacheKey: string;
89+
/** Whether `git rev-parse --show-toplevel` actually resolved a top-level. */
90+
readonly resolved: boolean;
91+
}
92+
8693
const resolveRepositoryIdentityCacheKey = Effect.fn("resolveRepositoryIdentityCacheKey")(function* (
8794
cwd: string,
88-
) {
95+
): Effect.fn.Return<RepositoryIdentityCacheKey, never, ProcessRunner.ProcessRunner> {
8996
const processRunner = yield* ProcessRunner.ProcessRunner;
90-
let cacheKey = cwd;
9197

9298
// git is a real executable on every platform — no cmd.exe shell mode, which
9399
// would split paths containing spaces during cmd's re-tokenization.
@@ -99,15 +105,13 @@ const resolveRepositoryIdentityCacheKey = Effect.fn("resolveRepositoryIdentityCa
99105
})
100106
.pipe(Effect.option);
101107
if (topLevelResult._tag === "None" || topLevelResult.value.code !== 0) {
102-
return cacheKey;
108+
return { cacheKey: cwd, resolved: false };
103109
}
104110

105111
const candidate = topLevelResult.value.stdout.trim();
106-
if (candidate.length > 0) {
107-
cacheKey = candidate;
108-
}
109-
110-
return cacheKey;
112+
return candidate.length > 0
113+
? { cacheKey: candidate, resolved: true }
114+
: { cacheKey: cwd, resolved: false };
111115
});
112116

113117
const resolveRepositoryIdentityFromCacheKey = Effect.fn("resolveRepositoryIdentityFromCacheKey")(
@@ -135,6 +139,33 @@ export const makeRepositoryIdentityResolver = Effect.fn("makeRepositoryIdentityR
135139
function* (options: RepositoryIdentityResolverOptions = {}) {
136140
const processRunner = yield* ProcessRunner.ProcessRunner;
137141

142+
// `git rev-parse --show-toplevel` is otherwise re-run on every resolve. During
143+
// `replayEvents` enrichment that means one git subprocess per project event on
144+
// every reconnect, which is the dominant cost behind the "Some requests are
145+
// slow" toast (#2037). Memoise the cwd -> top-level mapping so a replay burst
146+
// resolves each workspace at most once per TTL. A resolved top-level uses the
147+
// positive TTL; an unresolved cwd (not a repo / git timed out) uses the
148+
// negative TTL so a directory that later becomes a repo is still picked up.
149+
const repositoryIdentityCacheKeyCache = yield* Cache.makeWith<
150+
string,
151+
RepositoryIdentityCacheKey
152+
>(
153+
(cwd) =>
154+
resolveRepositoryIdentityCacheKey(cwd).pipe(
155+
Effect.provideService(ProcessRunner.ProcessRunner, processRunner),
156+
),
157+
{
158+
capacity: options.cacheCapacity ?? DEFAULT_REPOSITORY_IDENTITY_CACHE_CAPACITY,
159+
timeToLive: Exit.match({
160+
onSuccess: (value) =>
161+
value.resolved
162+
? (options.positiveCacheTtl ?? DEFAULT_POSITIVE_CACHE_TTL)
163+
: (options.negativeCacheTtl ?? DEFAULT_NEGATIVE_CACHE_TTL),
164+
onFailure: () => Duration.zero,
165+
}),
166+
},
167+
);
168+
138169
const repositoryIdentityCache = yield* Cache.makeWith<string, RepositoryIdentity | null>(
139170
(cacheKey) =>
140171
resolveRepositoryIdentityFromCacheKey(cacheKey).pipe(
@@ -155,9 +186,7 @@ export const makeRepositoryIdentityResolver = Effect.fn("makeRepositoryIdentityR
155186
const resolve: RepositoryIdentityResolverShape["resolve"] = Effect.fn(
156187
"RepositoryIdentityResolver.resolve",
157188
)(function* (cwd) {
158-
const cacheKey = yield* resolveRepositoryIdentityCacheKey(cwd).pipe(
159-
Effect.provideService(ProcessRunner.ProcessRunner, processRunner),
160-
);
189+
const { cacheKey } = yield* Cache.get(repositoryIdentityCacheKeyCache, cwd);
161190
return yield* Cache.get(repositoryIdentityCache, cacheKey);
162191
});
163192

0 commit comments

Comments
 (0)