TypeScript
perf(server): background PR discovery no longer delays git status after a restart (#32162)
Julius Marmingebdb8d5dfc067
diff --git a/apps/server/src/orchestration-v2/ThreadPullRequestService.ts b/apps/server/src/orchestration-v2/ThreadPullRequestService.tsindex 1cb01acd4ed..d84c532d06e 100644--- a/apps/server/src/orchestration-v2/ThreadPullRequestService.ts+++ b/apps/server/src/orchestration-v2/ThreadPullRequestService.ts@@ -20,6 +20,7 @@ import * as Stream from "effect/Stream";import * as BackgroundPolicy from "../background/BackgroundPolicy.ts";import * as GitManager from "../git/GitManager.ts";+import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";import * as ProjectStore from "./ProjectStore.ts";import * as PullRequestService from "../pullRequest/PullRequestService.ts";import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";@@ -347,6 +348,7 @@ export const make = Effect.gen(function* () {const worker = yield* makeDrainableWorker((request: RefreshRequest) =>synchronize(request).pipe(+ Effect.provideService(GitVcsDriver.BackgroundGitWork, true),Effect.catchCauseIf((cause) => !Cause.hasInterruptsOnly(cause),(cause) =>diff --git a/apps/server/src/orchestration-v2/ThreadSettlementService.ts b/apps/server/src/orchestration-v2/ThreadSettlementService.tsindex aedbb73fc55..8c852426da3 100644--- a/apps/server/src/orchestration-v2/ThreadSettlementService.ts+++ b/apps/server/src/orchestration-v2/ThreadSettlementService.ts@@ -31,6 +31,7 @@ import * as PullRequestService from "../pullRequest/PullRequestService.ts";import * as ServerSettings from "../serverSettings.ts";import { forkParked } from "../serverActivation.ts";import * as TerminalManager from "../terminal/Manager.ts";+import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";import * as ProjectStore from "./ProjectStore.ts";import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";import * as Orchestrator from "./Orchestrator.ts";@@ -556,6 +557,7 @@ export const make = Effect.gen(function* () {threadId?: ThreadId,) =>sweep(mergedPullRequest, threadId).pipe(+ Effect.provideService(GitVcsDriver.BackgroundGitWork, true),Effect.catchCauseIf((cause) => !Cause.hasInterruptsOnly(cause),(cause) =>diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.tsindex d120605d696..0d97d43c3e9 100644--- a/apps/server/src/vcs/GitVcsDriver.ts+++ b/apps/server/src/vcs/GitVcsDriver.ts@@ -301,6 +301,11 @@ export interface GitRemoteStatusOptions {readonly refreshUpstream?: boolean;}+/** Set by background discovery so its Git commands run in their own small lane, not the user's. */+export const BackgroundGitWork = Context.Reference<boolean>("t3/vcs/BackgroundGitWork", {+ defaultValue: () => false,+});+export class GitVcsDriver extends Context.Service<GitVcsDriver,{diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.tsindex 39427404093..728c20c034e 100644--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts@@ -14,6 +14,7 @@ import * as FileSystem from "effect/FileSystem";import * as Fiber from "effect/Fiber";import * as Layer from "effect/Layer";import * as Path from "effect/Path";+import * as Queue from "effect/Queue";import * as PlatformError from "effect/PlatformError";import * as Ref from "effect/Ref";import * as Result from "effect/Result";@@ -129,6 +130,55 @@ const makeDriverReleasingLock = (return { driver, failures };});+it.effect("runs background Git in its own lane so user commands never queue behind it", () =>+ Effect.gen(function* () {+ const gate = yield* Deferred.make<void>();+ const starts = yield* Queue.unbounded<string>();+ const spawner = ChildProcessSpawner.make((command) =>+ Effect.gen(function* () {+ yield* Queue.offer(+ starts,+ ChildProcess.isStandardCommand(command) ? String(command.args[0]) : "",+ );+ return ChildProcessSpawner.makeHandle({+ ...makeSuccessfulHandle("ok"),+ exitCode: Deferred.await(gate).pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),+ });+ }),+ );+ const driver = yield* makeGitVcsDriverCore().pipe(+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),+ Effect.provide(layerServerConfig),+ );+ const run = (args: ReadonlyArray<string>) =>+ driver.execute({ operation: "test.gitLane", cwd: "/repo", args: [...args] });+ const background = yield* Effect.forEach(+ Array.from({ length: 12 }),+ () => run(["background"]),+ {+ concurrency: "unbounded",+ },+ ).pipe(Effect.provideService(GitVcsDriver.BackgroundGitWork, true), Effect.forkChild);+ const foreground = yield* Effect.forEach(Array.from({ length: 4 }), () => run(["foreground"]), {+ concurrency: "unbounded",+ }).pipe(Effect.forkChild);++ yield* TestClock.adjust("2 seconds");+ const started = yield* Queue.takeAll(starts);+ assert.deepStrictEqual([...started].toSorted(), [+ "background",+ "background",+ "foreground",+ "foreground",+ "foreground",+ "foreground",+ ]);+ yield* Deferred.succeed(gate, undefined);+ assert.equal((yield* Fiber.join(background)).length, 12);+ assert.equal((yield* Fiber.join(foreground)).length, 4);+ }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),+);+describe("Git output collection", () => {// Git writes in arbitrary chunk sizes; one byte per chunk splits every// multibyte character across reads.diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.tsindex d1199f39c65..85a7e524b1a 100644--- a/apps/server/src/vcs/GitVcsDriverCore.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.ts@@ -63,6 +63,7 @@ const branchBaseConfigKey = (branch: string) => `branch.${branch}.t3-merge-base`*/const legacyBranchBaseConfigKey = (branch: string) => `branch.${branch}.gh-merge-base`;const gitProcesses = Semaphore.makeUnsafe(8);+const backgroundGitProcesses = Semaphore.makeUnsafe(2);const WORKTREE_REMOVE_TIMEOUT_MS = Duration.toMillis(Duration.minutes(5));const DEFAULT_MAX_OUTPUT_BYTES = 1_000_000;const OUTPUT_TRUNCATED_MARKER = "\n\n[truncated]";@@ -1123,7 +1124,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*(execution) =>input.timeoutMs === null || (input.timeoutMs ?? DEFAULT_TIMEOUT_MS) > DEFAULT_TIMEOUT_MS? execution- : gitProcesses.withPermits(1)(execution),+ : Effect.gen(function* () {+ const lane = (yield* GitVcsDriver.BackgroundGitWork)+ ? backgroundGitProcesses+ : gitProcesses;+ return yield* lane.withPermits(1)(execution);+ }),Effect.withSpan(input.operation, {kind: "client",attributes: {