The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 12:00 UTC
perf(server): git status runs in parallel and repo detection spawns 3x fewer git processes (#27103)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 29, 2026 at 13:39 UTC
7c29f2d20be6d26ed3914cc4e075681ad1aa5db2
7 files changed
+194
-80
apps/server/src/vcs/GitVcsDriver.test.ts
+18
@@ -1006,6 +1006,24 @@ it.effect("GitVcsDriver treats a missing git executable as outside any repositor
1006
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
1007
);
1008
1009
+it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
1010
+ "GitVcsDriver detects a repository whose path contains a newline",
1011
+ () =>
1012
+ Effect.gen(function* () {
1013
+ const fileSystem = yield* FileSystem.FileSystem;
1014
+ const path = yield* Path.Path;
1015
+ const parent = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-newline-repo-" });
1016
+ const cwd = path.join(parent, "multi\nline");
1017
+ yield* fileSystem.makeDirectory(cwd);
1018
+ yield* runGit(cwd, ["init"]);
1019
+ const driver = yield* GitVcsDriver.makeVcsDriverShape();
1020
+
1021
+ const repository = yield* driver.detectRepository(cwd);
1022
+ assert.strictEqual(repository?.rootPath, yield* fileSystem.realPath(cwd));
1023
+ assert.strictEqual(repository?.metadataPath, ".git");
1024
+ }).pipe(Effect.scoped, Effect.provide(layerGitContract)),
1025
+);
1026
+
1027
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
1028
"GitVcsDriver still fails detection when git exists but cannot be spawned",
1029
() =>
apps/server/src/vcs/GitVcsDriver.ts
+32
-14
@@ -587,25 +587,43 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* (
587
const detectRepository: VcsDriver.VcsDriver["Service"]["detectRepository"] = Effect.fn(
588
"detectRepository",
589
)(function* (cwd) {
590
- if (!(yield* isInsideWorkTree(cwd))) {
591
- return null;
592
- }
593
-
594
- const root = yield* gitCommand(vcsProcess, "GitVcsDriver.detectRepository.root", cwd, [
595
- "rev-parse",
596
- "--show-toplevel",
597
- ]);
598
- const gitCommonDir = yield* gitCommand(
590
+ // One spawn answers all three; outside a work tree it exits non-zero or prints "false".
591
+ const result = yield* gitCommand(
592
vcsProcess,
600
- "GitVcsDriver.detectRepository.commonDir",
593
+ "GitVcsDriver.detectRepository",
594
cwd,
602
- ["rev-parse", "--git-common-dir"],
603
- ).pipe(Effect.orElseSucceed(() => null));
595
+ ["rev-parse", "--is-inside-work-tree", "--show-toplevel", "--git-common-dir"],
596
+ {
597
+ allowNonZeroExit: true,
598
+ timeoutMs: 5_000,
599
+ maxOutputBytes: 16_384,
600
+ },
601
+ ).pipe(Effect.catchIf(isMissingExecutable, () => Effect.succeed(null)));
602
+ if (result === null || result.exitCode !== 0) return null;
603
+ const lines = result.stdout.replace(/\r?\n$/u, "").split(/\r?\n/u);
604
+ if (lines[0]?.trim() !== "true") return null;
605
+ // A newline inside a path breaks the line framing; ask for each path on its own.
606
+ if (lines.length !== 3) {
607
+ const root = yield* gitCommand(vcsProcess, "GitVcsDriver.detectRepository", cwd, [
608
+ "rev-parse",
609
+ "--show-toplevel",
610
+ ]);
611
+ const gitCommonDir = yield* gitCommand(vcsProcess, "GitVcsDriver.detectRepository", cwd, [
612
+ "rev-parse",
613
+ "--git-common-dir",
614
+ ]).pipe(Effect.orElseSucceed(() => null));
615
+ return {
616
+ kind: "git" as const,
617
+ rootPath: root.stdout.trim(),
618
+ metadataPath: gitCommonDir?.stdout.trim() || null,
619
+ freshness: yield* nowFreshness(),
620
+ };
621
+ }
622
623
return {
624
kind: "git" as const,
607
- rootPath: root.stdout.trim(),
608
- metadataPath: gitCommonDir?.stdout.trim() || null,
625
+ rootPath: lines[1]!.trim(),
626
+ metadataPath: lines[2]!.trim() || null,
627
freshness: yield* nowFreshness(),
628
};
629
});
apps/server/src/vcs/GitVcsDriverCore.ts
+67
-53
@@ -2091,7 +2091,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2091
return yield* lockError;
2092
}
2093
}
2094
- const statusResult = yield* executeGitWithStableDiagnostics(
2094
+ const repositoryPaths = yield* resolveRepositoryPaths(cwd).pipe(
2095
+ Effect.catchTags({ GitCommandError: () => Effect.succeed(null) }),
2096
+ );
2097
+ const statusCacheKey = repositoryPaths?.gitCommonDir;
2098
+ const readStatus = executeGitWithStableDiagnostics(
2099
"GitVcsDriver.statusDetails.status",
2100
cwd,
2101
statusArgs,
@@ -2105,34 +2109,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2109
isMissingGitCwdError(error) ? Effect.succeed(null) : Effect.fail(error),
2110
}),
2111
);
2108
-
2109
- if (statusResult === null) {
2110
- return NON_REPOSITORY_STATUS_DETAILS;
2111
- }
2112
-
2113
- if (statusResult.exitCode !== 0) {
2114
- if (isNonRepositoryGitStderr(statusResult.stderr)) {
2115
- return NON_REPOSITORY_STATUS_DETAILS;
2116
- }
2117
- return yield* new GitCommandError({
2118
- ...gitCommandContext({
2119
- operation: "GitVcsDriver.statusDetails.status",
2120
- cwd,
2121
- args: statusArgs,
2122
- }),
2123
- detail: "Git status failed.",
2124
- exitCode: statusResult.exitCode,
2125
- stdoutLength: statusResult.stdout.length,
2126
- stderrLength: statusResult.stderr.length,
2127
- });
2128
- }
2129
-
2130
- const repositoryPaths = yield* resolveRepositoryPaths(cwd).pipe(
2131
- Effect.catchTags({ GitCommandError: () => Effect.succeed(null) }),
2132
- );
2133
- const statusCacheKey = repositoryPaths?.gitCommonDir;
2134
- const [numstatEntries, defaultBranch, hasPrimaryRemote] = yield* Effect.all(
2112
+ // diff-index does not read status output, so both spawn together; a non-repository discards it.
2113
+ const [statusResult, numstatResult, defaultBranch, hasPrimaryRemote] = yield* Effect.all(
2114
[
2115
+ readStatus,
2116
// Porcelain diff rewrites indexes despite GIT_OPTIONAL_LOCKS=0; plumbing with -M preserves rename detection.
2117
executeGitWithStableDiagnostics(
2118
"GitVcsDriver.statusDetails.numstat",
@@ -2178,6 +2158,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2158
}),
2159
);
2160
}),
2161
+ Effect.result,
2162
),
2163
statusCacheKey
2164
? Cache.get(defaultBranchCache, statusCacheKey).pipe(Effect.orElseSucceed(() => null))
@@ -2188,6 +2169,28 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2169
],
2170
{ concurrency: "unbounded" },
2171
);
2172
+ if (statusResult === null) {
2173
+ return NON_REPOSITORY_STATUS_DETAILS;
2174
+ }
2175
+
2176
+ if (statusResult.exitCode !== 0) {
2177
+ if (isNonRepositoryGitStderr(statusResult.stderr)) {
2178
+ return NON_REPOSITORY_STATUS_DETAILS;
2179
+ }
2180
+ return yield* new GitCommandError({
2181
+ ...gitCommandContext({
2182
+ operation: "GitVcsDriver.statusDetails.status",
2183
+ cwd,
2184
+ args: statusArgs,
2185
+ }),
2186
+ detail: "Git status failed.",
2187
+ exitCode: statusResult.exitCode,
2188
+ stdoutLength: statusResult.stdout.length,
2189
+ stderrLength: statusResult.stderr.length,
2190
+ });
2191
+ }
2192
+
2193
+ const numstatEntries = yield* Effect.fromResult(numstatResult);
2194
const statusStdout = statusResult.stdout;
2195
2196
let refName: string | null = null;
@@ -2231,25 +2234,34 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2234
}
2235
}
2236
2234
- const fallbackAheadCount =
2235
- includeDivergence && !upstreamRef && refName
2236
- ? yield* computeAheadCountAgainstBase(cwd, refName).pipe(Effect.orElseSucceed(() => 0))
2237
- : null;
2238
-
2239
- if (fallbackAheadCount !== null) {
2240
- aheadCount = fallbackAheadCount;
2241
- behindCount = 0;
2242
- }
2243
-
2237
const isDefaultBranch =
2238
refName !== null &&
2239
(refName === defaultBranch ||
2240
(defaultBranch === null && (refName === "main" || refName === "master")));
2248
- if (includeDivergence && refName && !isDefaultBranch) {
2249
- aheadOfDefaultCount =
2250
- fallbackAheadCount !== null
2251
- ? fallbackAheadCount
2252
- : yield* computeAheadCountAgainstBase(cwd, refName).pipe(Effect.orElseSucceed(() => 0));
2241
+ const needsFallbackAhead = includeDivergence && !upstreamRef && refName !== null;
2242
+ const needsAheadOfDefault = includeDivergence && refName !== null && !isDefaultBranch;
2243
+ const [baseAheadCount, branchChanges] = yield* Effect.all(
2244
+ [
2245
+ refName !== null && (needsFallbackAhead || needsAheadOfDefault)
2246
+ ? computeAheadCountAgainstBase(cwd, refName, {
2247
+ knownOriginDefaultBranch: defaultBranch,
2248
+ }).pipe(Effect.orElseSucceed(() => 0))
2249
+ : Effect.succeed(0),
2250
+ options?.includeBranchChanges
2251
+ ? readBranchChangeTotals(repositoryPaths?.worktreeRoot ?? cwd, refName, {
2252
+ knownOriginDefaultBranch: defaultBranch,
2253
+ }).pipe(Effect.orElseSucceed(() => undefined))
2254
+ : Effect.undefined,
2255
+ ],
2256
+ { concurrency: "unbounded" },
2257
+ );
2258
+
2259
+ if (needsFallbackAhead) {
2260
+ aheadCount = baseAheadCount;
2261
+ behindCount = 0;
2262
+ }
2263
+ if (needsAheadOfDefault) {
2264
+ aheadOfDefaultCount = baseAheadCount;
2265
}
2266
2267
const fileStatMap = new Map<string, { insertions: number; deletions: number }>();
@@ -2276,12 +2288,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
2288
}
2289
files.sort((a, b) => a.path.localeCompare(b.path));
2290
2279
- const branchChanges = options?.includeBranchChanges
2280
- ? yield* readBranchChangeTotals(repositoryPaths?.worktreeRoot ?? cwd, refName).pipe(
2281
- Effect.orElseSucceed(() => undefined),
2282
- )
2283
- : undefined;
2284
-
2291
return {
2292
isRepo: true,
2293
hasOriginRemote: hasPrimaryRemote,
@@ -3014,6 +3020,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3020
cwd: string,
3021
branch: string | null,
3022
explicitBaseRef?: string,
3023
+ knownOriginDefaultBranch: string | null = null,
3024
) {
3025
const base =
3026
explicitBaseRef !== undefined
@@ -3025,9 +3032,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3032
: explicitBaseRef,
3033
})
3034
: branch
3028
- ? yield* resolveBaseBranchForNoUpstream(cwd, branch, { allowRemoteOfCurrent: true }).pipe(
3029
- Effect.orElseSucceed(() => null),
3030
- )
3035
+ ? yield* resolveBaseBranchForNoUpstream(cwd, branch, {
3036
+ allowRemoteOfCurrent: true,
3037
+ knownOriginDefaultBranch,
3038
+ }).pipe(Effect.orElseSucceed(() => null))
3039
: null;
3040
if (base === null) return { baseRef: null, mergeBase: "HEAD" };
3041
const { baseRef, comparisonRef } = base;
@@ -3058,8 +3066,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3066
const readBranchChangeTotals = Effect.fn("readBranchChangeTotals")(function* (
3067
cwd: string,
3068
branch: string | null,
3069
+ options?: { readonly knownOriginDefaultBranch: string | null },
3070
) {
3062
- const { baseRef, mergeBase } = yield* resolveReviewMergeBase(cwd, branch);
3071
+ const { baseRef, mergeBase } = yield* resolveReviewMergeBase(
3072
+ cwd,
3073
+ branch,
3074
+ undefined,
3075
+ options?.knownOriginDefaultBranch ?? null,
3076
+ );
3077
const untracked = yield* prepareUntrackedReviewIndex(cwd, []);
3078
if (untracked === null) {
3079
return yield* new GitCommandError({
apps/server/src/vcs/VcsDriverRegistry.test.ts
+8
-6
@@ -21,6 +21,8 @@ const processOutput = (stdout: string): VcsProcess.VcsProcessOutput => ({
21
const normalizeGitArgs = (args: ReadonlyArray<string>): ReadonlyArray<string> =>
22
args[0] === "-C" && args.length >= 2 ? args.slice(2) : args;
23
24
+const DETECT_COMMAND = "rev-parse --is-inside-work-tree --show-toplevel --git-common-dir";
25
+
26
describe("VcsDriverRegistry", () => {
27
const makeDiskBackedLayer = (rootDir: string, calls: string[]) =>
28
Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe(
@@ -49,10 +51,10 @@ describe("VcsDriverRegistry", () => {
51
stderr: "fatal: not a git repository",
52
};
53
}
52
- if (command === "rev-parse --is-inside-work-tree") return processOutput("true\n");
53
- if (command === "rev-parse --show-toplevel") return processOutput(`${repoDir}\n`);
54
- if (command === "rev-parse --git-common-dir") {
55
- return processOutput(`${path.relative(input.cwd, path.join(repoDir, ".git"))}\n`);
54
+ if (command === DETECT_COMMAND) {
55
+ return processOutput(
56
+ `true\n${repoDir}\n${path.relative(input.cwd, path.join(repoDir, ".git"))}\n`,
57
+ );
58
}
59
return processOutput("");
60
}).pipe(Effect.provide(NodeServices.layer), Effect.orDie),
@@ -75,7 +77,7 @@ describe("VcsDriverRegistry", () => {
77
yield* fs.remove(path.join(repoDir, ".git"), { recursive: true });
78
79
assert.equal(yield* registry.detect({ cwd: repoDir }), null);
78
- assert.equal(calls.filter((call) => call === "rev-parse --is-inside-work-tree").length, 2);
80
+ assert.equal(calls.filter((call) => call === DETECT_COMMAND).length, 2);
81
}).pipe(Effect.provide(makeDiskBackedLayer(repoDir, calls)));
82
}).pipe(Effect.provide(NodeServices.layer)),
83
);
@@ -118,7 +120,7 @@ describe("VcsDriverRegistry", () => {
120
yield* fs.makeDirectory(path.join(repoDir, ".git"));
121
122
assert.equal((yield* registry.detect({ cwd: repoDir }))?.repository.rootPath, repoDir);
121
- assert.equal(calls.filter((call) => call === "rev-parse --is-inside-work-tree").length, 2);
123
+ assert.equal(calls.filter((call) => call === DETECT_COMMAND).length, 2);
124
}).pipe(Effect.provide(makeDiskBackedLayer(repoDir, calls)));
125
}).pipe(Effect.provide(NodeServices.layer)),
126
);
apps/server/src/vcs/VcsProcess.ts
+1
-2
@@ -83,8 +83,7 @@ const gitMetricOperations = new Set([
83
"GitVcsDriver.checkpoints.resolveGitCommonDir",
84
"GitVcsDriver.checkpoints.resolveHeadCommit",
85
"GitVcsDriver.checkpoints.restoreCheckpoint",
86
- "GitVcsDriver.detectRepository.commonDir",
87
- "GitVcsDriver.detectRepository.root",
86
+ "GitVcsDriver.detectRepository",
87
"GitVcsDriver.filterIgnoredPaths",
88
"GitVcsDriver.initRepository",
89
"GitVcsDriver.isInsideWorkTree",
apps/server/src/vcs/VcsStatusBroadcaster.test.ts
+46
-2
@@ -76,6 +76,7 @@ function layerTestFor(state: {
76
backgroundWorkEnabled?: boolean;
77
automaticFetchInterval?: Effect.Effect<Duration.Duration>;
78
beforeRemoteStatus?: Effect.Effect<void>;
79
+ beforeLocalStatus?: Effect.Effect<void>;
80
fileSystem?: FileSystem.FileSystem;
81
pullCurrentBranch?: GitWorkflowService.GitWorkflowService["Service"]["pullCurrentBranch"];
82
}) {
@@ -95,9 +96,11 @@ function layerTestFor(state: {
96
Layer.provide(
97
Layer.mock(GitWorkflowService.GitWorkflowService)({
98
localStatus: () =>
98
- Effect.sync(() => {
99
+ Effect.suspend(() => {
100
state.localStatusCalls += 1;
100
- return state.currentLocalStatus;
101
+ return (state.beforeLocalStatus ?? Effect.void).pipe(
102
+ Effect.as(state.currentLocalStatus),
103
+ );
104
}),
105
currentBranch: () => Effect.sync(() => state.currentLocalStatus.refName),
106
remoteStatus: (_input, options) =>
@@ -163,6 +166,47 @@ function layerBackgroundPolicy(shouldRunScopeWork: (scope: BackgroundScope) => b
166
}
167
168
describe("VcsStatusBroadcaster", () => {
169
+ it.effect("refreshes queued behind a running one share the next git status read", () => {
170
+ const state: Parameters<typeof layerTestFor>[0] = {
171
+ currentLocalStatus: baseLocalStatus,
172
+ currentRemoteStatus: baseRemoteStatus,
173
+ localStatusCalls: 0,
174
+ remoteStatusCalls: 0,
175
+ localInvalidationCalls: 0,
176
+ remoteInvalidationCalls: 0,
177
+ fileSystem: FileSystem.makeNoop({ realPath: (cwd) => Effect.succeed(cwd) }),
178
+ };
179
+
180
+ return Effect.gen(function* () {
181
+ const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
182
+ const started = yield* Deferred.make<void>();
183
+ const release = yield* Deferred.make<void>();
184
+ state.beforeLocalStatus = Deferred.succeed(started, undefined).pipe(
185
+ Effect.andThen(Deferred.await(release)),
186
+ );
187
+ const running = yield* broadcaster
188
+ .refreshLocalStatus("/repo")
189
+ .pipe(Effect.forkScoped({ startImmediately: true }));
190
+ yield* Deferred.await(started);
191
+ // Requested while the first read runs, so that read may predate their change.
192
+ state.currentLocalStatus = { ...baseLocalStatus, hasWorkingTreeChanges: true };
193
+ const queued = yield* Effect.all(
194
+ [broadcaster.refreshLocalStatus("/repo"), broadcaster.refreshLocalStatus("/repo")],
195
+ { concurrency: "unbounded" },
196
+ ).pipe(Effect.forkScoped({ startImmediately: true }));
197
+ yield* Deferred.succeed(release, undefined);
198
+
199
+ assert.isFalse((yield* Fiber.join(running)).hasWorkingTreeChanges);
200
+ const [second, third] = yield* Fiber.join(queued);
201
+ assert.isTrue(second.hasWorkingTreeChanges);
202
+ assert.isTrue(third.hasWorkingTreeChanges);
203
+ assert.strictEqual(state.localStatusCalls, 2);
204
+
205
+ yield* broadcaster.refreshLocalStatus("/repo");
206
+ assert.strictEqual(state.localStatusCalls, 3);
207
+ }).pipe(Effect.provide(layerTestFor(state)), Effect.scoped);
208
+ });
209
+
210
it.effect("publishes a terminal branch switch without reading remote status", () => {
211
const state = {
212
currentLocalStatus: baseLocalStatus,
apps/server/src/vcs/VcsStatusBroadcaster.ts
+22
-3
@@ -359,11 +359,30 @@ export const make = Effect.gen(function* () {
359
);
360
});
361
362
+ // A refresh that started after a request saw every change that request could mean, so queued
363
+ // callers share it instead of each running git status again.
364
+ const localRefreshLocks = yield* KeyedLock.make<string>();
365
+ const localRefreshRequests = new Map<string, number>();
366
+ const localRefreshResults = new Map<
367
+ string,
368
+ { readonly startedAt: number; readonly local: VcsStatusLocalResult }
369
+ >();
370
const refreshLocalStatusCore = Effect.fn("VcsStatusBroadcaster.refreshLocalStatusCore")(
371
function* (cwd: string) {
364
- yield* workflow.invalidateLocalStatus(cwd);
365
- const local = yield* workflow.localStatus({ cwd });
366
- return yield* updateCachedLocalStatus(cwd, local, { publish: true });
372
+ const requestedAt = (localRefreshRequests.get(cwd) ?? 0) + 1;
373
+ localRefreshRequests.set(cwd, requestedAt);
374
+ return yield* localRefreshLocks.withLock(
375
+ cwd,
376
+ Effect.gen(function* () {
377
+ const shared = localRefreshResults.get(cwd);
378
+ if (shared !== undefined && shared.startedAt >= requestedAt) return shared.local;
379
+ const startedAt = localRefreshRequests.get(cwd) ?? requestedAt;
380
+ yield* workflow.invalidateLocalStatus(cwd);
381
+ const local = yield* workflow.localStatus({ cwd });
382
+ localRefreshResults.set(cwd, { startedAt, local });
383
+ return yield* updateCachedLocalStatus(cwd, local, { publish: true });
384
+ }),
385
+ );
386
},
387
);
388