Skip to content

Commit bdb8d5d

Browse files
perf(server): background PR discovery no longer delays git status after a restart (#32162)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 6fe0fdf commit bdb8d5d

5 files changed

Lines changed: 66 additions & 1 deletion

File tree

‎apps/server/src/orchestration-v2/ThreadPullRequestService.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as Stream from "effect/Stream";
2020

2121
import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";
2222
import * as GitManager from "../git/GitManager.ts";
23+
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
2324
import * as ProjectStore from "./ProjectStore.ts";
2425
import * as PullRequestService from "../pullRequest/PullRequestService.ts";
2526
import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
@@ -347,6 +348,7 @@ export const make = Effect.gen(function* () {
347348

348349
const worker = yield* makeDrainableWorker((request: RefreshRequest) =>
349350
synchronize(request).pipe(
351+
Effect.provideService(GitVcsDriver.BackgroundGitWork, true),
350352
Effect.catchCauseIf(
351353
(cause) => !Cause.hasInterruptsOnly(cause),
352354
(cause) =>

‎apps/server/src/orchestration-v2/ThreadSettlementService.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import * as PullRequestService from "../pullRequest/PullRequestService.ts";
3131
import * as ServerSettings from "../serverSettings.ts";
3232
import { forkParked } from "../serverActivation.ts";
3333
import * as TerminalManager from "../terminal/Manager.ts";
34+
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
3435
import * as ProjectStore from "./ProjectStore.ts";
3536
import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
3637
import * as Orchestrator from "./Orchestrator.ts";
@@ -556,6 +557,7 @@ export const make = Effect.gen(function* () {
556557
threadId?: ThreadId,
557558
) =>
558559
sweep(mergedPullRequest, threadId).pipe(
560+
Effect.provideService(GitVcsDriver.BackgroundGitWork, true),
559561
Effect.catchCauseIf(
560562
(cause) => !Cause.hasInterruptsOnly(cause),
561563
(cause) =>

‎apps/server/src/vcs/GitVcsDriver.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,11 @@ export interface GitRemoteStatusOptions {
301301
readonly refreshUpstream?: boolean;
302302
}
303303

304+
/** Set by background discovery so its Git commands run in their own small lane, not the user's. */
305+
export const BackgroundGitWork = Context.Reference<boolean>("t3/vcs/BackgroundGitWork", {
306+
defaultValue: () => false,
307+
});
308+
304309
export class GitVcsDriver extends Context.Service<
305310
GitVcsDriver,
306311
{

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import * as FileSystem from "effect/FileSystem";
1414
import * as Fiber from "effect/Fiber";
1515
import * as Layer from "effect/Layer";
1616
import * as Path from "effect/Path";
17+
import * as Queue from "effect/Queue";
1718
import * as PlatformError from "effect/PlatformError";
1819
import * as Ref from "effect/Ref";
1920
import * as Result from "effect/Result";
@@ -129,6 +130,55 @@ const makeDriverReleasingLock = (
129130
return { driver, failures };
130131
});
131132

133+
it.effect("runs background Git in its own lane so user commands never queue behind it", () =>
134+
Effect.gen(function* () {
135+
const gate = yield* Deferred.make<void>();
136+
const starts = yield* Queue.unbounded<string>();
137+
const spawner = ChildProcessSpawner.make((command) =>
138+
Effect.gen(function* () {
139+
yield* Queue.offer(
140+
starts,
141+
ChildProcess.isStandardCommand(command) ? String(command.args[0]) : "",
142+
);
143+
return ChildProcessSpawner.makeHandle({
144+
...makeSuccessfulHandle("ok"),
145+
exitCode: Deferred.await(gate).pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),
146+
});
147+
}),
148+
);
149+
const driver = yield* makeGitVcsDriverCore().pipe(
150+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
151+
Effect.provide(layerServerConfig),
152+
);
153+
const run = (args: ReadonlyArray<string>) =>
154+
driver.execute({ operation: "test.gitLane", cwd: "/repo", args: [...args] });
155+
const background = yield* Effect.forEach(
156+
Array.from({ length: 12 }),
157+
() => run(["background"]),
158+
{
159+
concurrency: "unbounded",
160+
},
161+
).pipe(Effect.provideService(GitVcsDriver.BackgroundGitWork, true), Effect.forkChild);
162+
const foreground = yield* Effect.forEach(Array.from({ length: 4 }), () => run(["foreground"]), {
163+
concurrency: "unbounded",
164+
}).pipe(Effect.forkChild);
165+
166+
yield* TestClock.adjust("2 seconds");
167+
const started = yield* Queue.takeAll(starts);
168+
assert.deepStrictEqual([...started].toSorted(), [
169+
"background",
170+
"background",
171+
"foreground",
172+
"foreground",
173+
"foreground",
174+
"foreground",
175+
]);
176+
yield* Deferred.succeed(gate, undefined);
177+
assert.equal((yield* Fiber.join(background)).length, 12);
178+
assert.equal((yield* Fiber.join(foreground)).length, 4);
179+
}).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
180+
);
181+
132182
describe("Git output collection", () => {
133183
// Git writes in arbitrary chunk sizes; one byte per chunk splits every
134184
// multibyte character across reads.

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const branchBaseConfigKey = (branch: string) => `branch.${branch}.t3-merge-base`
6363
*/
6464
const legacyBranchBaseConfigKey = (branch: string) => `branch.${branch}.gh-merge-base`;
6565
const gitProcesses = Semaphore.makeUnsafe(8);
66+
const backgroundGitProcesses = Semaphore.makeUnsafe(2);
6667
const WORKTREE_REMOVE_TIMEOUT_MS = Duration.toMillis(Duration.minutes(5));
6768
const DEFAULT_MAX_OUTPUT_BYTES = 1_000_000;
6869
const OUTPUT_TRUNCATED_MARKER = "\n\n[truncated]";
@@ -1123,7 +1124,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
11231124
(execution) =>
11241125
input.timeoutMs === null || (input.timeoutMs ?? DEFAULT_TIMEOUT_MS) > DEFAULT_TIMEOUT_MS
11251126
? execution
1126-
: gitProcesses.withPermits(1)(execution),
1127+
: Effect.gen(function* () {
1128+
const lane = (yield* GitVcsDriver.BackgroundGitWork)
1129+
? backgroundGitProcesses
1130+
: gitProcesses;
1131+
return yield* lane.withPermits(1)(execution);
1132+
}),
11271133
Effect.withSpan(input.operation, {
11281134
kind: "client",
11291135
attributes: {

0 commit comments

Comments
 (0)