diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index f109a9a0c..576684926 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -112,6 +112,8 @@ const LEFTOVER_WORKTREE_REMOVE_DELAY = Duration.millis(250); const WORKTREE_REMOVE_TIMEOUT_MS = 5 * 60_000; /** Checkouts inspected at once while listing worktree statuses. */ const WORKTREE_STATUS_CONCURRENCY = 4; +/** Folder checks for `git worktree list` entries; each is one cheap stat. */ +const WORKTREE_EXISTS_CONCURRENCY = 64; /** * Shipping verdicts kept per (base commit, branch commit) pair. Commit ids pin * both histories, so a verdict never goes stale; the cap only bounds memory. @@ -859,7 +861,8 @@ interface WorktreeRegistration { */ function parseWorktreeRegistrations(stdout: string): ReadonlyArray { const registrations: WorktreeRegistration[] = []; - for (const block of stdout.split("\n\n")) { + // Entries are separated by a blank line; CRLF output would hide that. + for (const block of stdout.replaceAll("\r\n", "\n").split("\n\n")) { const lines = block.split("\n"); const header = lines.find((line) => line.startsWith("worktree ")); if (header === undefined) { @@ -4172,44 +4175,39 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* * hand, so callers that resolve a path (branch pickers, cwd inference) would * otherwise point at nothing. */ + /** + * The checkouts `git worktree list --porcelain` reports whose folder still + * exists. The folders are checked side by side: a repository with ~90 + * worktrees took seconds checking them one after another on a busy server, + * and every branch list waits on this. + */ const parseWorktreeList = Effect.fn("parseWorktreeList")(function* ( result: GitVcsDriver.ExecuteGitResult, ) { if (result.exitCode !== 0) { return [] as ReadonlyArray; } - const entries: Array = []; - let currentPath: string | null = null; - for (const line of result.stdout.split("\n")) { - if (line.startsWith("worktree ")) { - const candidatePath = line.slice("worktree ".length); - const exists = yield* fileSystem.stat(candidatePath).pipe( - Effect.map(() => true), + const registrations = parseWorktreeRegistrations(result.stdout); + const present = yield* Effect.forEach( + registrations, + (registration) => + fileSystem.stat(registration.path).pipe( + Effect.as(true), Effect.catch(() => Effect.succeed(false)), - ); - currentPath = exists ? candidatePath : null; - if (currentPath) { - entries.push({ path: currentPath, branch: null }); - } - } else if (line.startsWith("branch refs/heads/") && currentPath) { - const branch = line.slice("branch refs/heads/".length); - const pending = entries.at(-1); - if (pending && pending.path === currentPath) { - entries[entries.length - 1] = { ...pending, branch }; - } - } else if ((line === "locked" || line.startsWith("locked ")) && currentPath) { - const pending = entries.at(-1); - if (pending && pending.path === currentPath) { - entries[entries.length - 1] = { - ...pending, - lockReason: line.slice("locked".length).trim(), - }; - } - } else if (line === "") { - currentPath = null; - } - } - return entries as ReadonlyArray; + ), + { concurrency: WORKTREE_EXISTS_CONCURRENCY }, + ); + return registrations.flatMap((registration, index): Array => + present[index] + ? [ + { + path: registration.path, + branch: registration.branch, + ...(registration.lockReason === null ? {} : { lockReason: registration.lockReason }), + }, + ] + : [], + ); }); const listWorktrees: GitVcsDriver.GitVcsDriverShape["listWorktrees"] = Effect.fn("listWorktrees")( diff --git a/apps/server/src/vcs/VcsProjectConfig.test.ts b/apps/server/src/vcs/VcsProjectConfig.test.ts index abcb74190..6239af065 100644 --- a/apps/server/src/vcs/VcsProjectConfig.test.ts +++ b/apps/server/src/vcs/VcsProjectConfig.test.ts @@ -53,6 +53,65 @@ describe("VcsProjectConfig", () => { ); }); + it.layer(TestLayer)("prefers the nearest vcs.json", (it) => { + it.effect("uses a nested workspace's config over the repository root's", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const root = yield* fileSystem.makeTempDirectoryScoped({ + prefix: "threadlines-vcs-config-test-", + }); + const nested = path.join(root, "packages", "app"); + const writeConfig = (directory: string, kind: string) => + fileSystem + .makeDirectory(directory, { recursive: true }) + .pipe( + Effect.andThen( + fileSystem.writeFileString( + path.join(directory, "vcs.json"), + JSON.stringify({ vcs: { kind } }), + ), + ), + ); + yield* writeConfig(path.join(root, ".threadlines"), "jj"); + yield* writeConfig(path.join(nested, ".t3code"), "git"); + + const config = yield* VcsProjectConfig.VcsProjectConfig; + const kind = yield* config.resolveKind({ cwd: nested }); + + assert.equal(kind, "git"); + }), + ); + }); + + // A check further up that never answers (an unreachable network mount) + // must not hold back the nearer config that exists. + it.layer( + VcsProjectConfig.layer.pipe( + Layer.provide( + FileSystem.layerNoop({ + exists: (candidate) => + candidate === "/repo/packages/app/.t3code/vcs.json" + ? Effect.succeed(true) + : candidate === "/repo/.threadlines/vcs.json" + ? Effect.never + : Effect.succeed(false), + readFileString: () => Effect.succeed('{"vcs":{"kind":"git"}}'), + }), + ), + Layer.provide(NodeServices.layer), + ), + )("answers once the nearest config is known", (it) => { + it.effect("doesn't wait on a stalled check further up", () => + Effect.gen(function* () { + const config = yield* VcsProjectConfig.VcsProjectConfig; + const kind = yield* config.resolveKind({ cwd: "/repo/packages/app" }); + + assert.equal(kind, "git"); + }), + ); + }); + it.layer(TestLayer)("falls back to legacy .t3code/vcs.json", (it) => { it.effect("returns the configured kind", () => Effect.gen(function* () { diff --git a/apps/server/src/vcs/VcsProjectConfig.ts b/apps/server/src/vcs/VcsProjectConfig.ts index 65d1cd599..567ddc632 100644 --- a/apps/server/src/vcs/VcsProjectConfig.ts +++ b/apps/server/src/vcs/VcsProjectConfig.ts @@ -1,5 +1,6 @@ import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Path from "effect/Path"; @@ -60,22 +61,35 @@ export const make = Effect.fn("makeVcsProjectConfig")(function* () { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; + /** + * The nearest `vcs.json` at or above `cwd`. Every candidate up to the root + * is checked side by side (each check waits its turn on a busy server). The + * answer comes as soon as the nearest existing one is known, so a stalled + * check further up (an unreachable network mount) doesn't hold it. + */ const findConfigPath = Effect.fn("VcsProjectConfig.findConfigPath")(function* (cwd: string) { + const candidates: Array = []; let current = cwd; while (true) { for (const directoryName of PROJECT_VCS_CONFIG_DIR_NAMES) { - const candidate = path.join(current, directoryName, "vcs.json"); - if (yield* fileSystem.exists(candidate).pipe(Effect.orElseSucceed(() => false))) { - return candidate; - } + candidates.push(path.join(current, directoryName, "vcs.json")); } - const parent = path.dirname(current); if (parent === current) { - return null; + break; } current = parent; } + const checks = yield* Effect.forEach(candidates, (candidate) => + Effect.forkChild(fileSystem.exists(candidate).pipe(Effect.orElseSucceed(() => false))), + ); + for (const [index, check] of checks.entries()) { + if (yield* Fiber.join(check)) { + yield* Fiber.interruptAll(checks.slice(index + 1)); + return candidates[index] ?? null; + } + } + return null; }); const readConfiguredKind = Effect.fn("VcsProjectConfig.readConfiguredKind")(function* (