The best way to control Claude Code, Codex and more from any computer or phone
perf(server): Pull no longer waits behind other repositories' fetches (6.3 s → 0.21 s median) (#30059)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Andrei committed
Nov 15, 2026 at 06:50 UTC
1483e970bd6f35bea45084d2f538e47eecbd5d18
2 files changed
+349
-9
apps/server/src/vcs/GitVcsDriverCore.test.ts
+281
@@ -1123,6 +1123,287 @@ describe("pullCurrentBranch", () => {
1123
};
1124
});
1125
1126
+ it.effect("resolves a status waiter when Pull claims its queued fetch", () =>
1127
+ Effect.scoped(
1128
+ Effect.gen(function* () {
1129
+ const { cwd, remoteHead } = yield* cloneBehindRemote;
1130
+ const blockers: string[] = [];
1131
+ for (let index = 0; index < 4; index++) {
1132
+ const blocker = yield* cloneBehindRemote;
1133
+ blockers.push(NodeFS.realpathSync.native(blocker.cwd));
1134
+ }
1135
+ const targetPath = NodeFS.realpathSync.native(cwd);
1136
+ const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
1137
+ const release = yield* Deferred.make<void>();
1138
+ const blockersStarted = yield* Deferred.make<void>();
1139
+ const targetEntered = yield* Deferred.make<void>();
1140
+ let running = 0;
1141
+ let targetFetches = 0;
1142
+ let waiterSucceeded = false;
1143
+ const spawner = ChildProcessSpawner.make((command) =>
1144
+ Effect.gen(function* () {
1145
+ const handle = yield* delegate.spawn(command);
1146
+ if (!ChildProcess.isStandardCommand(command) || !command.args.includes("fetch"))
1147
+ return handle;
1148
+ const commandCwd = String(command.options.cwd);
1149
+ if (commandCwd === targetPath) targetFetches++;
1150
+ if (blockers.includes(commandCwd)) {
1151
+ running++;
1152
+ if (running === 4) yield* Deferred.succeed(blockersStarted, undefined);
1153
+ yield* Deferred.await(release);
1154
+ }
1155
+ return handle;
1156
+ }),
1157
+ );
1158
+ const driver = yield* makeGitVcsDriverCore().pipe(
1159
+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
1160
+ );
1161
+ const tracer = Tracer.make({
1162
+ span(options) {
1163
+ const span = new Tracer.NativeSpan(options);
1164
+ if (options.name === "GitVcsDriver.fetchRemoteForStatus.wait") {
1165
+ const attribute = span.attribute.bind(span);
1166
+ span.attribute = (key, value) => {
1167
+ attribute(key, value);
1168
+ if (key === "statusFetch.joining") Deferred.doneUnsafe(targetEntered, Effect.void);
1169
+ };
1170
+ const end = span.end.bind(span);
1171
+ span.end = (endTime, exit) => {
1172
+ waiterSucceeded = Exit.isSuccess(exit);
1173
+ end(endTime, exit);
1174
+ };
1175
+ }
1176
+ return span;
1177
+ },
1178
+ });
1179
+ yield* Effect.gen(function* () {
1180
+ for (const blocker of blockers) {
1181
+ yield* driver.statusDetailsRemote(blocker).pipe(Effect.forkChild);
1182
+ }
1183
+ yield* Deferred.await(blockersStarted);
1184
+ const status = yield* driver
1185
+ .statusDetailsRemote(cwd)
1186
+ .pipe(Effect.withTracer(tracer), Effect.forkChild({ startImmediately: true }));
1187
+ yield* Deferred.await(targetEntered);
1188
+ const pull = yield* driver.pullCurrentBranch(cwd);
1189
+ const refreshed = yield* Fiber.join(status);
1190
+ assert.equal(pull.status, "pulled");
1191
+ assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), remoteHead);
1192
+ assert.isTrue(waiterSucceeded);
1193
+ assert.equal(refreshed.behindCount, 0);
1194
+ assert.equal(targetFetches, 0);
1195
+ }).pipe(Effect.ensuring(Deferred.succeed(release, undefined)));
1196
+ }),
1197
+ ).pipe(Effect.provide(layerTest)),
1198
+ );
1199
+
1200
+ const claimedFetchFallbacks = ["diverged", "interrupted", "upstream-only"] as const;
1201
+
1202
+ it.effect.each(claimedFetchFallbacks)(
1203
+ "falls back to the claimed status fetch when Pull is %s",
1204
+ (failure) =>
1205
+ Effect.scoped(
1206
+ Effect.gen(function* () {
1207
+ const { cwd, updater, remoteHead } = yield* cloneBehindRemote;
1208
+ if (failure === "upstream-only") {
1209
+ yield* git(updater, ["push", "origin", "HEAD:refs/heads/other"]);
1210
+ yield* writeTextFile(cwd, ".git/refs/remotes/origin/other.lock", "");
1211
+ }
1212
+ if (failure === "diverged") {
1213
+ yield* writeTextFile(cwd, "local.txt", "local\n");
1214
+ yield* git(cwd, ["add", "local.txt"]);
1215
+ yield* git(cwd, ["commit", "-m", "local commit"]);
1216
+ }
1217
+ const blockers: string[] = [];
1218
+ for (let index = 0; index < 4; index++) {
1219
+ const blocker = yield* cloneBehindRemote;
1220
+ blockers.push(NodeFS.realpathSync.native(blocker.cwd));
1221
+ }
1222
+ const targetPath = NodeFS.realpathSync.native(cwd);
1223
+ const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
1224
+ const release = yield* Deferred.make<void>();
1225
+ const blockersStarted = yield* Deferred.make<void>();
1226
+ const targetEntered = yield* Deferred.make<void>();
1227
+ const targetFetchStarted = yield* Deferred.make<void>();
1228
+ const pullBeforeFetch = yield* Deferred.make<void>();
1229
+ const releasePull = yield* Deferred.make<void>();
1230
+ let running = 0;
1231
+ let targetFetches = 0;
1232
+ let waiterSucceeded = false;
1233
+ let targetFetchExitCode: number | undefined;
1234
+ const spawner = ChildProcessSpawner.make((command) =>
1235
+ Effect.gen(function* () {
1236
+ if (ChildProcess.isStandardCommand(command)) {
1237
+ const commandCwd = NodeFS.realpathSync.native(String(command.options.cwd));
1238
+ if (
1239
+ commandCwd === targetPath &&
1240
+ failure === "interrupted" &&
1241
+ command.args[0] === "rev-parse" &&
1242
+ command.args[1] === "HEAD" &&
1243
+ command.args.length === 2
1244
+ ) {
1245
+ yield* Deferred.succeed(pullBeforeFetch, undefined);
1246
+ yield* Deferred.await(releasePull);
1247
+ }
1248
+ }
1249
+ const handle = yield* delegate.spawn(command);
1250
+ if (!ChildProcess.isStandardCommand(command) || !command.args.includes("fetch"))
1251
+ return handle;
1252
+ const commandCwd = String(command.options.cwd);
1253
+ if (commandCwd === targetPath) {
1254
+ targetFetches++;
1255
+ yield* Deferred.succeed(targetFetchStarted, undefined);
1256
+ return ChildProcessSpawner.makeHandle({
1257
+ ...handle,
1258
+ exitCode: handle.exitCode.pipe(
1259
+ Effect.tap((exitCode) =>
1260
+ Effect.sync(() => {
1261
+ targetFetchExitCode = exitCode;
1262
+ }),
1263
+ ),
1264
+ ),
1265
+ });
1266
+ }
1267
+ if (blockers.includes(commandCwd)) {
1268
+ running++;
1269
+ if (running === 4) yield* Deferred.succeed(blockersStarted, undefined);
1270
+ yield* Deferred.await(release);
1271
+ }
1272
+ return handle;
1273
+ }),
1274
+ );
1275
+ const driver = yield* makeGitVcsDriverCore().pipe(
1276
+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
1277
+ );
1278
+ const tracer = Tracer.make({
1279
+ span(options) {
1280
+ const span = new Tracer.NativeSpan(options);
1281
+ if (options.name === "GitVcsDriver.fetchRemoteForStatus.wait") {
1282
+ const attribute = span.attribute.bind(span);
1283
+ span.attribute = (key, value) => {
1284
+ attribute(key, value);
1285
+ if (key === "statusFetch.joining")
1286
+ Deferred.doneUnsafe(targetEntered, Effect.void);
1287
+ };
1288
+ const end = span.end.bind(span);
1289
+ span.end = (endTime, exit) => {
1290
+ waiterSucceeded = Exit.isSuccess(exit);
1291
+ end(endTime, exit);
1292
+ };
1293
+ }
1294
+ return span;
1295
+ },
1296
+ });
1297
+ yield* Effect.gen(function* () {
1298
+ for (const blocker of blockers) {
1299
+ yield* driver.statusDetailsRemote(blocker).pipe(Effect.forkChild);
1300
+ }
1301
+ yield* Deferred.await(blockersStarted);
1302
+ const status = yield* driver
1303
+ .statusDetailsRemote(cwd)
1304
+ .pipe(Effect.withTracer(tracer), Effect.forkChild({ startImmediately: true }));
1305
+ yield* Deferred.await(targetEntered);
1306
+ if (failure === "interrupted") {
1307
+ const pull = yield* driver.pullCurrentBranch(cwd).pipe(Effect.forkChild);
1308
+ yield* Deferred.await(pullBeforeFetch);
1309
+ yield* Fiber.interrupt(pull);
1310
+ const exit = yield* Fiber.await(pull);
1311
+ assert.isTrue(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause));
1312
+ } else if (failure === "upstream-only") {
1313
+ assert.equal((yield* driver.pullCurrentBranch(cwd)).status, "pulled");
1314
+ yield* (yield* FileSystem.FileSystem).remove(
1315
+ `${cwd}/.git/refs/remotes/origin/other.lock`,
1316
+ );
1317
+ } else {
1318
+ const error = yield* Effect.flip(driver.pullCurrentBranch(cwd));
1319
+ assert.equal(error.operation, "GitVcsDriver.pullCurrentBranch.pull");
1320
+ }
1321
+ assert.equal(targetFetches, 0);
1322
+ yield* Deferred.succeed(release, undefined);
1323
+ yield* Deferred.await(targetFetchStarted);
1324
+ assert.isTrue((yield* Fiber.join(status)).isRepo);
1325
+ assert.isTrue(waiterSucceeded);
1326
+ assert.equal(targetFetches, 1);
1327
+ assert.equal(targetFetchExitCode, 0);
1328
+ if (failure === "upstream-only") {
1329
+ assert.equal(yield* git(cwd, ["rev-parse", "refs/remotes/origin/other"]), remoteHead);
1330
+ }
1331
+ }).pipe(
1332
+ Effect.ensuring(
1333
+ Effect.all([
1334
+ Deferred.succeed(release, undefined),
1335
+ Deferred.succeed(releasePull, undefined),
1336
+ ]),
1337
+ ),
1338
+ );
1339
+ }),
1340
+ ).pipe(Effect.provide(layerTest)),
1341
+ );
1342
+
1343
+ it.effect("waits for a running status fetch before starting Pull", () =>
1344
+ Effect.scoped(
1345
+ Effect.gen(function* () {
1346
+ const { cwd, remoteHead } = yield* cloneBehindRemote;
1347
+ const targetPath = NodeFS.realpathSync.native(cwd);
1348
+ const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
1349
+ const fetchStarted = yield* Deferred.make<void>();
1350
+ const releaseFetch = yield* Deferred.make<void>();
1351
+ const pullResolvedCommonDir = yield* Deferred.make<void>();
1352
+ const pullStarted = yield* Deferred.make<void>();
1353
+ const events: string[] = [];
1354
+ const spawner = ChildProcessSpawner.make((command) =>
1355
+ Effect.gen(function* () {
1356
+ if (!ChildProcess.isStandardCommand(command)) return yield* delegate.spawn(command);
1357
+ const target = NodeFS.realpathSync.native(String(command.options.cwd)) === targetPath;
1358
+ if (target && command.args.includes("pull")) {
1359
+ events.push("pull-spawn");
1360
+ yield* Deferred.succeed(pullStarted, undefined);
1361
+ }
1362
+ const handle = yield* delegate.spawn(command);
1363
+ if (target && command.args.includes("fetch")) {
1364
+ yield* Deferred.succeed(fetchStarted, undefined);
1365
+ yield* Deferred.await(releaseFetch);
1366
+ yield* handle.exitCode;
1367
+ events.push("fetch-exit");
1368
+ }
1369
+ return handle;
1370
+ }),
1371
+ );
1372
+ const driver = yield* makeGitVcsDriverCore().pipe(
1373
+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
1374
+ );
1375
+ const tracer = Tracer.make({
1376
+ span(options) {
1377
+ const span = new Tracer.NativeSpan(options);
1378
+ if (options.name === "resolveGitCommonDir") {
1379
+ const end = span.end.bind(span);
1380
+ span.end = (endTime, exit) => {
1381
+ end(endTime, exit);
1382
+ Deferred.doneUnsafe(pullResolvedCommonDir, Effect.void);
1383
+ };
1384
+ }
1385
+ return span;
1386
+ },
1387
+ });
1388
+ yield* Effect.gen(function* () {
1389
+ yield* driver.statusDetailsRemote(cwd).pipe(Effect.forkChild);
1390
+ yield* Deferred.await(fetchStarted);
1391
+ const pull = yield* driver
1392
+ .pullCurrentBranch(cwd)
1393
+ .pipe(Effect.withTracer(tracer), Effect.forkChild({ startImmediately: true }));
1394
+ yield* Deferred.await(pullResolvedCommonDir);
1395
+ assert.isFalse(yield* Deferred.isDone(pullStarted));
1396
+ assert.isUndefined(pull.pollUnsafe());
1397
+ yield* Deferred.succeed(releaseFetch, undefined);
1398
+ const result = yield* Fiber.join(pull);
1399
+ assert.equal(result.status, "pulled");
1400
+ assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), remoteHead);
1401
+ assert.deepEqual(events, ["fetch-exit", "pull-spawn"]);
1402
+ }).pipe(Effect.ensuring(Deferred.succeed(releaseFetch, undefined)));
1403
+ }),
1404
+ ).pipe(Effect.provide(layerTest)),
1405
+ );
1406
+
1407
it.effect("still fetches other branches on the first pull", () =>
1408
Effect.gen(function* () {
1409
const driver = yield* GitVcsDriver.GitVcsDriver;
apps/server/src/vcs/GitVcsDriverCore.ts
+68
-9
@@ -2,6 +2,7 @@ import * as Cache from "effect/Cache";
2
import * as Cause from "effect/Cause";
3
import * as Clock from "effect/Clock";
4
import * as Data from "effect/Data";
5
+import * as Deferred from "effect/Deferred";
6
import * as Digest from "effect/crypto/Digest";
7
import * as SecureRandom from "effect/crypto/SecureRandom";
8
import * as DateTime from "effect/DateTime";
@@ -1023,7 +1024,18 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1024
const hostPlatform = yield* HostProcess.Platform;
1025
const serviceScope = yield* Effect.scope;
1026
const automaticFetchInterval = yield* VcsAutomaticFetchInterval;
1026
- const statusFetches = new Map<string, Fiber.Fiber<void, GitCommandError>>();
1027
+ type StatusFetchState = {
1028
+ phase: "queued" | "running" | "claimed";
1029
+ claimed: Deferred.Deferred<void>;
1030
+ finished: Deferred.Deferred<boolean>;
1031
+ };
1032
+ const statusFetches = new Map<
1033
+ string,
1034
+ {
1035
+ state: StatusFetchState;
1036
+ fiber: Fiber.Fiber<void, GitCommandError>;
1037
+ }
1038
+ >();
1039
const statusFetchProcesses = yield* Semaphore.make(4);
1040
1041
const executeRaw: GitVcsDriver.GitVcsDriver["Service"]["execute"] = Effect.fnUntraced(
@@ -1450,11 +1462,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1462
const fiber = yield* Effect.uninterruptible(
1463
Effect.gen(function* () {
1464
const pending = statusFetches.get(key);
1453
- if (pending) return pending;
1465
+ if (pending) return pending.fiber;
1466
+ const claimed = yield* Deferred.make<void>();
1467
+ const finished = yield* Deferred.make<boolean>();
1468
+ const state: StatusFetchState = { phase: "queued", claimed, finished };
1469
// Frequent poll deadlines and disconnected clients must not interrupt a
1470
// healthy writer. A separate safety limit prevents a hung transport from
1471
// occupying its worker slot indefinitely.
1457
- const started = yield* executeGit("GitVcsDriver.fetchRemoteForStatus", fetchCwd, args, {
1472
+ const fetch = executeGit("GitVcsDriver.fetchRemoteForStatus", fetchCwd, args, {
1473
env: STATUS_UPSTREAM_REFRESH_ENV,
1474
fallbackErrorDetail: "Background Git fetch exited with a non-zero status.",
1475
timeoutMs: Duration.toMillis(STATUS_UPSTREAM_FETCH_TIMEOUT),
@@ -1467,18 +1482,39 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
1482
),
1483
Effect.tapError(() => recordStatusRemoteRefreshFailure(key)),
1484
Effect.tapCause((cause) => Effect.logWarning("Background Git fetch failed", cause)),
1470
- statusFetchProcesses.withPermits(1),
1485
+ );
1486
+ const started = yield* Effect.raceFirst(
1487
+ Effect.uninterruptibleMask((restore) =>
1488
+ Effect.gen(function* () {
1489
+ if (state.phase !== "queued") return false;
1490
+ state.phase = "running";
1491
+ yield* restore(fetch);
1492
+ return true;
1493
+ }),
1494
+ ).pipe(statusFetchProcesses.withPermits(1)),
1495
+ Deferred.await(claimed).pipe(Effect.as(false)),
1496
+ ).pipe(
1497
+ Effect.flatMap((completed) =>
1498
+ completed
1499
+ ? Effect.void
1500
+ : Deferred.await(finished).pipe(
1501
+ Effect.flatMap((succeeded) =>
1502
+ succeeded ? Effect.void : fetch.pipe(statusFetchProcesses.withPermits(1)),
1503
+ ),
1504
+ ),
1505
+ ),
1506
Effect.onExit(() =>
1507
Effect.sync(() => {
1473
- statusFetches.delete(key);
1508
+ if (statusFetches.get(key)?.state === state) statusFetches.delete(key);
1509
}),
1510
),
1511
Effect.forkIn(serviceScope, { startImmediately: false, uninterruptible: false }),
1512
);
1478
- statusFetches.set(key, started);
1513
+ statusFetches.set(key, { state, fiber: started });
1514
return started;
1515
}),
1516
);
1517
+ yield* Effect.annotateCurrentSpan("statusFetch.joining", true);
1518
const result = yield* Fiber.join(fiber).pipe(
1519
Effect.timeoutOption(STATUS_UPSTREAM_REFRESH_TIMEOUT),
1520
);
@@ -3345,12 +3381,31 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3381
});
3382
}
3383
const remote = yield* readConfigValue(cwd, `branch.${refName}.remote`);
3384
+ // The upstream-only retry leaves other remote-tracking refs stale, so a claimed status
3385
+ // fetch must still run after it.
3386
+ let fetchedUpstreamOnly = false;
3387
if (remote) {
3388
const gitCommonDir = yield* resolveGitCommonDir(cwd);
3389
const pending = statusFetches.get(`${gitCommonDir}\0${remote}`);
3390
// The status wait can finish while its fetch still owns reference locks.
3391
// Let our existing writer finish before starting the pull's fetch.
3353
- if (pending) yield* Fiber.await(pending);
3392
+ if (pending) {
3393
+ const claimed = yield* Effect.uninterruptible(
3394
+ Effect.gen(function* () {
3395
+ if (pending.state.phase !== "queued") return false;
3396
+ pending.state.phase = "claimed";
3397
+ yield* Effect.addFinalizer((exit) =>
3398
+ Deferred.succeed(
3399
+ pending.state.finished,
3400
+ Exit.isSuccess(exit) && !fetchedUpstreamOnly,
3401
+ ),
3402
+ );
3403
+ yield* Deferred.succeed(pending.state.claimed, undefined);
3404
+ return true;
3405
+ }),
3406
+ );
3407
+ if (!claimed) yield* Fiber.await(pending.fiber);
3408
+ }
3409
}
3410
const beforeSha = yield* runGitStdout(
3411
"GitVcsDriver.pullCurrentBranch.beforeSha",
@@ -3404,6 +3459,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3459
currentSha === beforeSha
3460
) {
3461
args = ["pull", "--ff-only", "--", remote, mergeRef];
3462
+ fetchedUpstreamOnly = true;
3463
result = yield* executeGitWithStableDiagnostics(
3464
"GitVcsDriver.pullCurrentBranch.pull",
3465
cwd,
@@ -3432,11 +3488,14 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
3488
3489
const refreshed = yield* readStatusDetailsLocal(cwd);
3490
return {
3435
- status: beforeSha.length > 0 && beforeSha === afterSha ? "skipped_up_to_date" : "pulled",
3491
+ status:
3492
+ beforeSha.length > 0 && beforeSha === afterSha
3493
+ ? ("skipped_up_to_date" as const)
3494
+ : ("pulled" as const),
3495
refName,
3496
upstreamRef: refreshed.upstreamRef,
3497
};
3439
- });
3498
+ }, Effect.scoped);
3499
3500
const readRangeContext: GitVcsDriver.GitVcsDriver["Service"]["readRangeContext"] = Effect.fn(
3501
"readRangeContext",