Skip to content

Commit dcbaf05

Browse files
t3dotggclaude
andauthored
perf(server): idle status polls and PR sweeps start fewer git processes (pingdotgg#16272)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 1024941 commit dcbaf05

4 files changed

Lines changed: 248 additions & 87 deletions

File tree

‎apps/server/src/git/GitManager.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,49 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
10711071
}),
10721072
);
10731073

1074+
it.effect("a branch tracking origin reads origin's URL once per PR lookup", () =>
1075+
Effect.gen(function* () {
1076+
const repoDir = yield* makeTempDir("t3code-git-manager-");
1077+
yield* initRepo(repoDir);
1078+
const remoteDir = yield* createBareRemote();
1079+
yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);
1080+
yield* runGit(repoDir, ["checkout", "-b", "feature/origin-once"]);
1081+
yield* runGit(repoDir, ["push", "-u", "origin", "feature/origin-once"]);
1082+
1083+
const gitConfigReads: string[] = [];
1084+
const { manager } = yield* makeManager({
1085+
gitConfigReads,
1086+
ghScenario: {
1087+
prListSequence: [
1088+
// Fake gh returns raw JSON stdout, matching the CLI boundary under test.
1089+
// @effect-diagnostics-next-line preferSchemaOverJson:off
1090+
JSON.stringify([
1091+
{
1092+
number: 217,
1093+
title: "Origin once PR",
1094+
url: "https://github.com/pingdotgg/t3code/pull/217",
1095+
baseRefName: "main",
1096+
headRefName: "feature/origin-once",
1097+
state: "OPEN",
1098+
updatedAt: "2026-04-03T15:00:00Z",
1099+
},
1100+
]),
1101+
],
1102+
},
1103+
});
1104+
1105+
yield* manager.branchPullRequest({ cwd: repoDir, branch: "feature/origin-once" });
1106+
gitConfigReads.length = 0;
1107+
const pullRequest = yield* manager.branchPullRequest({
1108+
cwd: repoDir,
1109+
branch: "feature/origin-once",
1110+
});
1111+
1112+
expect(pullRequest?.number).toBe(217);
1113+
expect(gitConfigReads.filter((key) => key === "remote.origin.url")).toHaveLength(1);
1114+
}),
1115+
);
1116+
10741117
it.effect("turn-end refresh finds a new PR and keeps known PRs cached", () =>
10751118
Effect.gen(function* () {
10761119
const repoDir = yield* makeTempDir("t3code-git-manager-");

‎apps/server/src/git/GitManager.ts‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,17 +1404,25 @@ export const make = Effect.gen(function* () {
14041404
};
14051405
});
14061406

1407+
// Returns [head remote, origin]. Most branches track origin, so read it once.
1408+
const resolveHeadAndOriginContexts = (cwd: string, remoteName: string | null) =>
1409+
remoteName === "origin"
1410+
? resolveRemoteRepositoryContext(cwd, "origin").pipe(
1411+
Effect.map((origin) => [origin, origin] as const),
1412+
)
1413+
: Effect.all(
1414+
[
1415+
resolveRemoteRepositoryContext(cwd, remoteName),
1416+
resolveRemoteRepositoryContext(cwd, "origin"),
1417+
],
1418+
{ concurrency: "unbounded" },
1419+
);
1420+
14071421
const resolvePrLookupRepositoryIdentity = Effect.fn("resolvePrLookupRepositoryIdentity")(
14081422
function* (cwd: string, branch: string, remoteNameOverride?: string) {
14091423
const remoteName =
14101424
remoteNameOverride ?? (yield* readConfigValueNullable(cwd, `branch.${branch}.remote`));
1411-
const [headRemote, targetRemote] = yield* Effect.all(
1412-
[
1413-
resolveRemoteRepositoryContext(cwd, remoteName),
1414-
resolveRemoteRepositoryContext(cwd, "origin"),
1415-
],
1416-
{ concurrency: "unbounded" },
1417-
);
1425+
const [headRemote, targetRemote] = yield* resolveHeadAndOriginContexts(cwd, remoteName);
14181426
return {
14191427
remoteName,
14201428
headRemoteUrlKey:
@@ -1438,12 +1446,9 @@ export const make = Effect.gen(function* () {
14381446
const shouldProbeLocalBranchSelector =
14391447
headBranchFromUpstream.length === 0 || headBranch === details.branch;
14401448

1441-
const [remoteRepository, originRepository] = yield* Effect.all(
1442-
[
1443-
resolveRemoteRepositoryContext(cwd, remoteName),
1444-
resolveRemoteRepositoryContext(cwd, "origin"),
1445-
],
1446-
{ concurrency: "unbounded" },
1449+
const [remoteRepository, originRepository] = yield* resolveHeadAndOriginContexts(
1450+
cwd,
1451+
remoteName,
14471452
);
14481453

14491454
const isCrossRepository =
Lines changed: 126 additions & 70 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";
33
import * as Effect from "effect/Effect";
4+
import * as FileSystem from "effect/FileSystem";
45
import * as Layer from "effect/Layer";
6+
import * as Path from "effect/Path";
57
import { ChildProcessSpawner } from "effect/process";
68

79
import * as VcsProcess from "./VcsProcess.ts";
@@ -43,9 +45,10 @@ describe("VcsDriverRegistry", () => {
4345
}).pipe(Effect.provide(layer));
4446
});
4547

46-
it.effect("caches repository detection for repeated resolves in the same cwd and kind", () => {
47-
const calls: VcsProcess.VcsProcessInput[] = [];
48-
const layer = Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe(
48+
// Answers detection like git: the nearest folder at or above the cwd, up to
49+
// `rootDir`, that holds `.git` is the repository.
50+
const makeDiskBackedLayer = (rootDir: string, calls: string[]) =>
51+
Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe(
4952
Layer.provide(NodeServices.layer),
5053
Layer.provide(
5154
Layer.mock(VcsProjectConfig.VcsProjectConfig)({
@@ -55,86 +58,139 @@ describe("VcsDriverRegistry", () => {
5558
Layer.provide(
5659
Layer.mock(VcsProcess.VcsProcess)({
5760
run: (input) =>
58-
Effect.sync(() => {
59-
calls.push(input);
60-
const normalizedArgs =
61-
input.args[0] === "-C" && input.args.length >= 2 ? input.args.slice(2) : input.args;
62-
const command = normalizedArgs.join(" ");
63-
if (command === "rev-parse --is-inside-work-tree") {
64-
return processOutput("true\n");
61+
Effect.gen(function* () {
62+
const fs = yield* FileSystem.FileSystem;
63+
const path = yield* Path.Path;
64+
const command = normalizeGitArgs(input.args).join(" ");
65+
calls.push(command);
66+
let repoDir: string | null = input.cwd;
67+
while (repoDir !== null && !(yield* fs.exists(path.join(repoDir, ".git")))) {
68+
repoDir = repoDir === rootDir ? null : path.dirname(repoDir);
6569
}
66-
if (command === "rev-parse --show-toplevel") {
67-
return processOutput("/repo\n");
70+
if (repoDir === null) {
71+
return {
72+
...processOutput(""),
73+
exitCode: ChildProcessSpawner.ExitCode(128),
74+
stderr: "fatal: not a git repository",
75+
};
6876
}
77+
if (command === "rev-parse --is-inside-work-tree") return processOutput("true\n");
78+
if (command === "rev-parse --show-toplevel") return processOutput(`${repoDir}\n`);
6979
if (command === "rev-parse --git-common-dir") {
70-
return processOutput("/repo/.git\n");
80+
return processOutput(`${path.relative(input.cwd, path.join(repoDir, ".git"))}\n`);
7181
}
7282
return processOutput("");
73-
}),
83+
}).pipe(Effect.provide(NodeServices.layer), Effect.orDie),
7484
}),
7585
),
7686
);
7787

78-
return Effect.gen(function* () {
79-
const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
80-
const first = yield* registry.resolve({ cwd: "/repo", requestedKind: "git" });
81-
const second = yield* registry.resolve({ cwd: "/repo", requestedKind: "git" });
82-
83-
assert.equal(first.repository.rootPath, "/repo");
84-
assert.equal(second.repository.rootPath, "/repo");
85-
assert.deepStrictEqual(
86-
calls.map((call) => normalizeGitArgs(call.args).join(" ")),
87-
[
88+
it.effect("caches repository detection for repeated resolves in the same cwd and kind", () =>
89+
Effect.gen(function* () {
90+
const fs = yield* FileSystem.FileSystem;
91+
const path = yield* Path.Path;
92+
const repoDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" });
93+
yield* fs.makeDirectory(path.join(repoDir, ".git"));
94+
const calls: string[] = [];
95+
96+
yield* Effect.gen(function* () {
97+
const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
98+
const first = yield* registry.resolve({ cwd: repoDir, requestedKind: "git" });
99+
const second = yield* registry.resolve({ cwd: repoDir, requestedKind: "git" });
100+
101+
assert.equal(first.repository.rootPath, repoDir);
102+
assert.equal(second.repository.rootPath, repoDir);
103+
assert.deepStrictEqual(calls, [
88104
"rev-parse --is-inside-work-tree",
89105
"rev-parse --show-toplevel",
90106
"rev-parse --git-common-dir",
91-
],
92-
);
93-
}).pipe(Effect.provide(layer));
94-
});
107+
]);
108+
}).pipe(Effect.provide(makeDiskBackedLayer(repoDir, calls)));
109+
}).pipe(Effect.provide(NodeServices.layer)),
110+
);
95111

96-
it.effect("detects a repository created after a negative lookup", () => {
97-
let insideWorkTreeChecks = 0;
98-
const layer = Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe(
99-
Layer.provide(NodeServices.layer),
100-
Layer.provide(
101-
Layer.mock(VcsProjectConfig.VcsProjectConfig)({
102-
resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"),
103-
}),
104-
),
105-
Layer.provide(
106-
Layer.mock(VcsProcess.VcsProcess)({
107-
run: (input) =>
108-
Effect.sync(() => {
109-
const command = normalizeGitArgs(input.args).join(" ");
110-
if (command === "rev-parse --is-inside-work-tree") {
111-
insideWorkTreeChecks += 1;
112-
return insideWorkTreeChecks === 1
113-
? {
114-
...processOutput(""),
115-
exitCode: ChildProcessSpawner.ExitCode(128),
116-
stderr: "fatal: not a git repository",
117-
}
118-
: processOutput("true\n");
119-
}
120-
if (command === "rev-parse --show-toplevel") {
121-
return processOutput("/repo\n");
122-
}
123-
if (command === "rev-parse --git-common-dir") {
124-
return processOutput("/repo/.git\n");
125-
}
126-
return processOutput("");
127-
}),
128-
}),
129-
),
130-
);
112+
it.effect("detects again when a cached repository is removed from disk", () =>
113+
Effect.gen(function* () {
114+
const fs = yield* FileSystem.FileSystem;
115+
const path = yield* Path.Path;
116+
const repoDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" });
117+
yield* fs.makeDirectory(path.join(repoDir, ".git"));
118+
const calls: string[] = [];
131119

132-
return Effect.gen(function* () {
133-
const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
120+
yield* Effect.gen(function* () {
121+
const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
122+
assert.equal((yield* registry.detect({ cwd: repoDir }))?.repository.rootPath, repoDir);
134123

135-
assert.equal(yield* registry.detect({ cwd: "/repo" }), null);
136-
assert.equal((yield* registry.detect({ cwd: "/repo" }))?.repository.rootPath, "/repo");
137-
assert.equal(insideWorkTreeChecks, 2);
138-
}).pipe(Effect.provide(layer));
139-
});
124+
yield* fs.remove(path.join(repoDir, ".git"), { recursive: true });
125+
126+
assert.equal(yield* registry.detect({ cwd: repoDir }), null);
127+
assert.equal(calls.filter((call) => call === "rev-parse --is-inside-work-tree").length, 2);
128+
}).pipe(Effect.provide(makeDiskBackedLayer(repoDir, calls)));
129+
}).pipe(Effect.provide(NodeServices.layer)),
130+
);
131+
132+
it.effect("detects again when git init creates a repository inside a cached one", () =>
133+
Effect.gen(function* () {
134+
const fs = yield* FileSystem.FileSystem;
135+
const path = yield* Path.Path;
136+
const parentDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" });
137+
const projectDir = path.join(parentDir, "project");
138+
yield* fs.makeDirectory(path.join(parentDir, ".git"));
139+
yield* fs.makeDirectory(projectDir);
140+
const calls: string[] = [];
141+
142+
yield* Effect.gen(function* () {
143+
const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
144+
assert.equal((yield* registry.detect({ cwd: projectDir }))?.repository.rootPath, parentDir);
145+
146+
yield* fs.makeDirectory(path.join(projectDir, ".git"));
147+
148+
assert.equal(
149+
(yield* registry.detect({ cwd: projectDir }))?.repository.rootPath,
150+
projectDir,
151+
);
152+
}).pipe(Effect.provide(makeDiskBackedLayer(parentDir, calls)));
153+
}).pipe(Effect.provide(NodeServices.layer)),
154+
);
155+
156+
it.effect("detects again when git init creates a repository between the cwd and its root", () =>
157+
Effect.gen(function* () {
158+
const fs = yield* FileSystem.FileSystem;
159+
const path = yield* Path.Path;
160+
const parentDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" });
161+
const middleDir = path.join(parentDir, "a");
162+
const cwd = path.join(middleDir, "b");
163+
yield* fs.makeDirectory(path.join(parentDir, ".git"));
164+
yield* fs.makeDirectory(cwd, { recursive: true });
165+
const calls: string[] = [];
166+
167+
yield* Effect.gen(function* () {
168+
const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
169+
assert.equal((yield* registry.detect({ cwd }))?.repository.rootPath, parentDir);
170+
171+
yield* fs.makeDirectory(path.join(middleDir, ".git"));
172+
173+
assert.equal((yield* registry.detect({ cwd }))?.repository.rootPath, middleDir);
174+
}).pipe(Effect.provide(makeDiskBackedLayer(parentDir, calls)));
175+
}).pipe(Effect.provide(NodeServices.layer)),
176+
);
177+
178+
it.effect("detects a repository created after a negative lookup", () =>
179+
Effect.gen(function* () {
180+
const fs = yield* FileSystem.FileSystem;
181+
const path = yield* Path.Path;
182+
const repoDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-vcs-registry-" });
183+
const calls: string[] = [];
184+
185+
yield* Effect.gen(function* () {
186+
const registry = yield* VcsDriverRegistry.VcsDriverRegistry;
187+
assert.equal(yield* registry.detect({ cwd: repoDir }), null);
188+
189+
yield* fs.makeDirectory(path.join(repoDir, ".git"));
190+
191+
assert.equal((yield* registry.detect({ cwd: repoDir }))?.repository.rootPath, repoDir);
192+
assert.equal(calls.filter((call) => call === "rev-parse --is-inside-work-tree").length, 2);
193+
}).pipe(Effect.provide(makeDiskBackedLayer(repoDir, calls)));
194+
}).pipe(Effect.provide(NodeServices.layer)),
195+
);
140196
});

0 commit comments

Comments
 (0)