Skip to content

Commit cdf0ac1

Browse files
t3dotggclaude
andcommitted
perf(server): stop re-running git for every project each minute
Three background sweeps (settlement, PR discovery, PR sync) resolve every project's repository identity each minute. The identity cache also lived one minute, so each git project cost two git spawns per minute. Folders that are not repositories were never cached and spawned git on every call. - Keep a found identity for 15 minutes. Cache a "not a repository" root for the existing 1-minute negative TTL instead of not at all. - Refresh the identity after publishing a repository, like clone already does, so PR discovery sees the new remote right away. - Stop tracing resolve itself. Almost every call is a cache hit, and the lookups that spawn git keep their own spans. Refs #11220 Refs #8949 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 2598ce7 commit cdf0ac1

3 files changed

Lines changed: 97 additions & 43 deletions

File tree

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

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,41 @@ const makeRepositoryIdentityResolverTestLayer = (options: {
3838
}),
3939
).pipe(Layer.provide(ProcessRunner.layer));
4040

41+
// Resolver with default TTLs on a fake git that records its calls. `rootPath`
42+
// answers `rev-parse --show-toplevel`; null answers like a folder outside any
43+
// repository.
44+
const makeFakeGitResolverLayer = (
45+
calls: Array<ReadonlyArray<string>>,
46+
rootPath: () => string | null,
47+
) =>
48+
Layer.effect(
49+
RepositoryIdentityResolver.RepositoryIdentityResolver,
50+
RepositoryIdentityResolver.make(),
51+
).pipe(
52+
Layer.provide(
53+
Layer.succeed(ProcessRunner.ProcessRunner, {
54+
run: (input) =>
55+
Effect.sync(() => {
56+
calls.push(input.args);
57+
const root = input.args.includes("rev-parse") ? rootPath() : undefined;
58+
return {
59+
stdout:
60+
root === undefined
61+
? "origin\[email protected]:T3Tools/t3code.git (fetch)\n"
62+
: `${root ?? ""}\n`,
63+
stderr: root === null ? "fatal: not a git repository" : "",
64+
code: ChildProcessSpawner.ExitCode(root === null ? 128 : 0),
65+
timedOut: false,
66+
stdoutTruncated: false,
67+
stderrTruncated: false,
68+
stdoutInvalidUtf8: false,
69+
stderrInvalidUtf8: false,
70+
};
71+
}),
72+
}),
73+
),
74+
);
75+
4176
it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
4277
it.effect("refreshes the Git root only when requested", () => {
4378
const calls: Array<ReadonlyArray<string>> = [];
@@ -126,48 +161,55 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => {
126161
}).pipe(Effect.provide(resolverLayer));
127162
});
128163

129-
it.effect("retries Git root discovery after a failed lookup", () => {
164+
it.effect("reuses a found identity for longer than the one-minute sweep cadence", () => {
130165
const calls: Array<ReadonlyArray<string>> = [];
131-
let rootAttempts = 0;
132-
const processRunner = Layer.succeed(ProcessRunner.ProcessRunner, {
133-
run: (input) =>
134-
Effect.sync(() => {
135-
calls.push(input.args);
136-
const rootLookup = input.args.includes("rev-parse");
137-
const failed = rootLookup && rootAttempts++ === 0;
138-
return {
139-
stdout: rootLookup
140-
? failed
141-
? ""
142-
: "/repo\n"
143-
: "origin\[email protected]:T3Tools/t3code.git (fetch)\n",
144-
stderr: failed ? "temporary Git failure" : "",
145-
code: ChildProcessSpawner.ExitCode(failed ? 1 : 0),
146-
timedOut: false,
147-
stdoutTruncated: false,
148-
stderrTruncated: false,
149-
stdoutInvalidUtf8: false,
150-
stderrInvalidUtf8: false,
151-
};
152-
}),
153-
});
154-
const resolverLayer = Layer.effect(
155-
RepositoryIdentityResolver.RepositoryIdentityResolver,
156-
RepositoryIdentityResolver.make(),
157-
).pipe(Layer.provide(processRunner));
166+
return Effect.gen(function* () {
167+
const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
168+
const first = yield* resolver.resolve("/repo/packages/web");
169+
expect(first?.canonicalKey).toBe("github.com/t3tools/t3code");
170+
171+
yield* TestClock.adjust(Duration.minutes(10));
172+
expect(yield* resolver.resolve("/repo/packages/web")).toEqual(first);
173+
expect(calls).toEqual([
174+
["-C", "/repo/packages/web", "rev-parse", "--show-toplevel"],
175+
["-C", "/repo", "remote", "-v"],
176+
]);
177+
}).pipe(
178+
Effect.provide(
179+
Layer.merge(
180+
TestClock.layer(),
181+
makeFakeGitResolverLayer(calls, () => "/repo"),
182+
),
183+
),
184+
);
185+
});
158186

187+
it.effect("rechecks a folder outside any repository only after the negative TTL", () => {
188+
const calls: Array<ReadonlyArray<string>> = [];
189+
let rootPath: string | null = null;
159190
return Effect.gen(function* () {
160191
const resolver = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
161192
expect(yield* resolver.resolve("/repo/packages/web")).toBeNull();
193+
rootPath = "/repo";
194+
expect(yield* resolver.resolve("/repo/packages/web")).toBeNull();
195+
expect(calls).toHaveLength(1);
162196

197+
yield* TestClock.adjust(Duration.minutes(1));
163198
const recovered = yield* resolver.resolve("/repo/packages/web");
164199
expect(recovered?.rootPath).toBe("/repo");
165200
expect(calls).toEqual([
166201
["-C", "/repo/packages/web", "rev-parse", "--show-toplevel"],
167202
["-C", "/repo/packages/web", "rev-parse", "--show-toplevel"],
168203
["-C", "/repo", "remote", "-v"],
169204
]);
170-
}).pipe(Effect.provide(resolverLayer));
205+
}).pipe(
206+
Effect.provide(
207+
Layer.merge(
208+
TestClock.layer(),
209+
makeFakeGitResolverLayer(calls, () => rootPath),
210+
),
211+
),
212+
);
171213
});
172214

173215
it.effect("normalizes equivalent GitHub remotes into a stable repository identity", () =>

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import * as Layer from "effect/Layer";
1313
import * as ProcessRunner from "../processRunner.ts";
1414

1515
const DEFAULT_REPOSITORY_IDENTITY_CACHE_CAPACITY = 512;
16-
const DEFAULT_POSITIVE_CACHE_TTL = Duration.minutes(1);
16+
// Background sweeps resolve every project each minute, so a found identity must
17+
// outlive that cadence. Clone and publish resolve with `refresh: true`.
18+
const DEFAULT_POSITIVE_CACHE_TTL = Duration.minutes(15);
19+
// Short, so a folder that gains a repository or a remote shows up quickly.
1720
const DEFAULT_NEGATIVE_CACHE_TTL = Duration.minutes(1);
1821

1922
export interface RepositoryIdentityResolverOptions {
@@ -152,7 +155,9 @@ export const make = Effect.fn("RepositoryIdentityResolver.make")(function* (
152155
capacity: cacheCapacity,
153156
timeToLive: Exit.match({
154157
onSuccess: (value) =>
155-
value === null ? Duration.zero : (options.positiveCacheTtl ?? DEFAULT_POSITIVE_CACHE_TTL),
158+
value === null
159+
? (options.negativeCacheTtl ?? DEFAULT_NEGATIVE_CACHE_TTL)
160+
: (options.positiveCacheTtl ?? DEFAULT_POSITIVE_CACHE_TTL),
156161
onFailure: () => Duration.zero,
157162
}),
158163
},
@@ -179,15 +184,17 @@ export const make = Effect.fn("RepositoryIdentityResolver.make")(function* (
179184
},
180185
);
181186

182-
const resolve: RepositoryIdentityResolver["Service"]["resolve"] = Effect.fn(
183-
"RepositoryIdentityResolver.resolve",
184-
)(function* (cwd, options) {
185-
if (options?.refresh) yield* Cache.invalidate(repositoryRootCache, cwd);
186-
const cacheKey = yield* Cache.get(repositoryRootCache, cwd);
187-
if (cacheKey === null) return null;
188-
if (options?.refresh) yield* Cache.invalidate(repositoryIdentityCache, cacheKey);
189-
return yield* Cache.get(repositoryIdentityCache, cacheKey);
190-
});
187+
// Untraced because almost every call is a cache hit. The lookups that spawn
188+
// git keep their own spans.
189+
const resolve: RepositoryIdentityResolver["Service"]["resolve"] = Effect.fnUntraced(
190+
function* (cwd, options) {
191+
if (options?.refresh) yield* Cache.invalidate(repositoryRootCache, cwd);
192+
const cacheKey = yield* Cache.get(repositoryRootCache, cwd);
193+
if (cacheKey === null) return null;
194+
if (options?.refresh) yield* Cache.invalidate(repositoryIdentityCache, cacheKey);
195+
return yield* Cache.get(repositoryIdentityCache, cacheKey);
196+
},
197+
);
191198

192199
return RepositoryIdentityResolver.of({ resolve });
193200
});

‎apps/server/src/ws.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3038,9 +3038,14 @@ const makeWsRpcLayer = (
30383038
[WS_METHODS.sourceControlPublishRepository]: (input) =>
30393039
observeRpcEffect(
30403040
WS_METHODS.sourceControlPublishRepository,
3041-
sourceControlRepositories
3042-
.publishRepository(input)
3043-
.pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
3041+
sourceControlRepositories.publishRepository(input).pipe(
3042+
// The new remote can change the repository identity, which stays
3043+
// cached for minutes. This refreshes the entry for `cwd` only, so a
3044+
// publish from a linked worktree leaves the project root entry to
3045+
// expire on its own.
3046+
Effect.tap(() => repositoryIdentityResolver.resolve(input.cwd, { refresh: true })),
3047+
Effect.tap(() => refreshGitStatus(input.cwd)),
3048+
),
30443049
{
30453050
"rpc.aggregate": "source-control",
30463051
},

0 commit comments

Comments
 (0)