From d7ccbd7fed2dc6169fc247776b469fcbeff97c8a Mon Sep 17 00:00:00 2001 From: Sharad-Patel1 <96814788+Sharad-Patel1@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:00:00 +0000 Subject: [PATCH] fix(server): checkpoint probes read the cached detection's work tree The detection rev-parse already reports whether cwd has a work tree. Record it on the repository identity and let CheckpointStore.isGitRepository read it from the cached detection, through a detectOrFail that keeps unreadable repositories failing. Repositories go back to main's spawn count; folders without a repository keep one probe per check. Co-Authored-By: Claude Opus 5.5 --- .../src/checkpointing/CheckpointStore.test.ts | 35 +++++++++++ .../src/checkpointing/CheckpointStore.ts | 8 +-- .../SourceControlProviderRegistry.test.ts | 1 + apps/server/src/vcs/GitVcsDriver.test.ts | 5 +- apps/server/src/vcs/GitVcsDriver.ts | 3 + apps/server/src/vcs/VcsDriverRegistry.ts | 58 +++++++++++-------- packages/contracts/src/vcs.ts | 2 + 7 files changed, 83 insertions(+), 29 deletions(-) diff --git a/apps/server/src/checkpointing/CheckpointStore.test.ts b/apps/server/src/checkpointing/CheckpointStore.test.ts index 45bb744bb6..7c1efea05b 100644 --- a/apps/server/src/checkpointing/CheckpointStore.test.ts +++ b/apps/server/src/checkpointing/CheckpointStore.test.ts @@ -711,3 +711,38 @@ it.layer(layerTest)("CheckpointStore.layer", (it) => { ); }); }); + +it.effect("answers a repository's later checks from the cached detection", () => { + const operations: Array = []; + const layerCountingProcess = Layer.effect( + VcsProcess.VcsProcess, + Effect.map(VcsProcess.VcsProcess, (vcsProcess) => + VcsProcess.VcsProcess.of({ + run: (input) => { + operations.push(input.operation); + return vcsProcess.run(input); + }, + }), + ), + ).pipe(Layer.provide(layerVcsProcessTest)); + return Effect.gen(function* () { + const tmp = yield* makeTmpDir(); + yield* initRepoWithCommit(tmp); + const checkpointStore = yield* CheckpointStore.CheckpointStore; + const start = operations.length; + + for (let check = 0; check < 3; check++) { + expect(yield* checkpointStore.isGitRepository(tmp)).toBe(true); + } + expect(operations.slice(start)).toEqual(["GitVcsDriver.detectRepository"]); + }).pipe( + Effect.scoped, + Effect.provide( + CheckpointStore.layer.pipe( + Layer.provideMerge(VcsDriverRegistry.layer), + Layer.provideMerge(layerCountingProcess), + Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), + ), + ), + ); +}); diff --git a/apps/server/src/checkpointing/CheckpointStore.ts b/apps/server/src/checkpointing/CheckpointStore.ts index 2e044548cd..de7a632793 100644 --- a/apps/server/src/checkpointing/CheckpointStore.ts +++ b/apps/server/src/checkpointing/CheckpointStore.ts @@ -87,13 +87,13 @@ export const make = Effect.gen(function* () { return handle.driver.checkpoints satisfies VcsCheckpointOps; }); - // Asks Git directly: the registry reads an unreadable repository as none, and accepts a bare - // repository's root, which has no work tree to checkpoint. + // Unlike detect, detectOrFail fails when Git cannot read the repository. A bare repository's + // root is detected but has no work tree to checkpoint. const isGitRepository: CheckpointStore["Service"]["isGitRepository"] = Effect.fn( "CheckpointStore.isGitRepository", )(function* (cwd) { - const git = yield* vcsRegistry.get("git"); - return yield* git.isInsideWorkTree(cwd); + const detected = yield* vcsRegistry.detectOrFail({ cwd, requestedKind: "git" }); + return detected?.repository.hasWorkTree === true; }); const captureCheckpoint: CheckpointStore["Service"]["captureCheckpoint"] = Effect.fn( diff --git a/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts b/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts index 9d472fe6e7..7717e0836c 100644 --- a/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts +++ b/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts @@ -137,6 +137,7 @@ function makeRegistry(input: { kind: "git", rootPath: "/repo", metadataPath: null, + hasWorkTree: true, freshness: { source: "live-local" as const, observedAt: TEST_EPOCH, diff --git a/apps/server/src/vcs/GitVcsDriver.test.ts b/apps/server/src/vcs/GitVcsDriver.test.ts index f359ca5ebd..45aefaaf23 100644 --- a/apps/server/src/vcs/GitVcsDriver.test.ts +++ b/apps/server/src/vcs/GitVcsDriver.test.ts @@ -67,6 +67,7 @@ const makeCaptureStore = Effect.fn("test.makeCaptureStore")(function* ( Effect.provideService(VcsDriverRegistry.VcsDriverRegistry, { get: () => Effect.succeed(driver), detect: () => Effect.succeed(handle), + detectOrFail: () => Effect.succeed(handle), resolve: () => Effect.succeed(handle), }), ); @@ -1985,7 +1986,9 @@ it.effect("GitVcsDriver finds no work tree in a deleted cwd or at a bare reposit assert.equal(yield* driver.detectRepository(deleted), null); assert.equal(yield* driver.isInsideWorkTree(bareRoot), false); // Worktree and branch commands work at a bare root, so it still counts as a repository. - assert.equal((yield* driver.detectRepository(bareRoot))?.rootPath, bareRoot); + const bare = yield* driver.detectRepository(bareRoot); + assert.equal(bare?.rootPath, bareRoot); + assert.equal(bare?.hasWorkTree, false); }).pipe(Effect.scoped, Effect.provide(layerGitContract)), ); diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.ts index cc752e9097..f755fcdb28 100644 --- a/apps/server/src/vcs/GitVcsDriver.ts +++ b/apps/server/src/vcs/GitVcsDriver.ts @@ -699,6 +699,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( kind: "git" as const, rootPath: path.resolve(cwd), metadataPath: lines.slice(2).join("\n").trim() || null, + hasWorkTree: false, freshness: yield* nowFreshness(), }; } @@ -716,6 +717,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( kind: "git" as const, rootPath: root.stdout.trim(), metadataPath: gitCommonDir?.stdout.trim() || null, + hasWorkTree: true, freshness: yield* nowFreshness(), }; } @@ -724,6 +726,7 @@ export const makeVcsDriverShape = Effect.fn("makeGitVcsDriverShape")(function* ( kind: "git" as const, rootPath: lines[3]!.trim(), metadataPath: lines[2]!.trim() || null, + hasWorkTree: true, freshness: yield* nowFreshness(), }; }); diff --git a/apps/server/src/vcs/VcsDriverRegistry.ts b/apps/server/src/vcs/VcsDriverRegistry.ts index 5bf7263ebe..0c0fe09233 100644 --- a/apps/server/src/vcs/VcsDriverRegistry.ts +++ b/apps/server/src/vcs/VcsDriverRegistry.ts @@ -35,6 +35,10 @@ export class VcsDriverRegistry extends Context.Service< readonly detect: ( input: VcsDriverResolveInput, ) => Effect.Effect; + /** Like detect, but fails when Git cannot read a repository instead of reporting none. */ + readonly detectOrFail: ( + input: VcsDriverResolveInput, + ) => Effect.Effect; readonly resolve: (input: VcsDriverResolveInput) => Effect.Effect; } >()("t3/vcs/VcsDriverRegistry") {} @@ -85,16 +89,7 @@ export const make = Effect.gen(function* () { driver: VcsDriver.VcsDriver["Service"], cwd: string, ) { - // Status, review and source control show a repository Git cannot read as none at all. - // Checkpoints need the failure, so CheckpointStore asks the driver directly. - const repository = yield* driver.detectRepository(cwd).pipe( - Effect.catchTags({ - VcsProcessExitError: (error) => - error.operation === GitVcsDriver.WORK_TREE_PROBE_OPERATION - ? Effect.succeed(null) - : Effect.fail(error), - }), - ); + const repository = yield* driver.detectRepository(cwd); if (!repository) { return null; } @@ -163,22 +158,36 @@ export const make = Effect.gen(function* () { }, ); + const detectOrFail: VcsDriverRegistry["Service"]["detectOrFail"] = Effect.fn( + "VcsDriverRegistry.detectOrFail", + )(function* (input) { + const requestedKind = yield* projectConfig.resolveKind(input); + const key = detectionCacheKey({ cwd: input.cwd, requestedKind }); + const cached = yield* Cache.get(detectionCache, key); + if (cached === null) return null; + const disk = yield* readDiskState(input.cwd, cached.handle.repository); + if ( + disk?.repositoryOnDisk && + disk.gitEntries.length === cached.gitEntries.length && + disk.gitEntries.every((exists, index) => exists === cached.gitEntries[index]) + ) { + return cached.handle; + } + yield* Cache.invalidate(detectionCache, key); + return (yield* Cache.get(detectionCache, key))?.handle ?? null; + }); + + // Status, review and source control show a repository Git cannot read as none at all. const detect: VcsDriverRegistry["Service"]["detect"] = Effect.fn("VcsDriverRegistry.detect")( function* (input) { - const requestedKind = yield* projectConfig.resolveKind(input); - const key = detectionCacheKey({ cwd: input.cwd, requestedKind }); - const cached = yield* Cache.get(detectionCache, key); - if (cached === null) return null; - const disk = yield* readDiskState(input.cwd, cached.handle.repository); - if ( - disk?.repositoryOnDisk && - disk.gitEntries.length === cached.gitEntries.length && - disk.gitEntries.every((exists, index) => exists === cached.gitEntries[index]) - ) { - return cached.handle; - } - yield* Cache.invalidate(detectionCache, key); - return (yield* Cache.get(detectionCache, key))?.handle ?? null; + return yield* detectOrFail(input).pipe( + Effect.catchTags({ + VcsProcessExitError: (error) => + error.operation === GitVcsDriver.WORK_TREE_PROBE_OPERATION + ? Effect.succeed(null) + : Effect.fail(error), + }), + ); }, ); @@ -204,6 +213,7 @@ export const make = Effect.gen(function* () { return VcsDriverRegistry.of({ get, detect, + detectOrFail, resolve, }); }); diff --git a/packages/contracts/src/vcs.ts b/packages/contracts/src/vcs.ts index 4d388ad8be..eaeae8985d 100644 --- a/packages/contracts/src/vcs.ts +++ b/packages/contracts/src/vcs.ts @@ -33,6 +33,8 @@ export const VcsRepositoryIdentity = Schema.Struct({ kind: VcsDriverKind, rootPath: TrimmedNonEmptyString, metadataPath: Schema.NullOr(TrimmedNonEmptyString), + /** False at a bare repository's root, where commands that need a work tree fail. */ + hasWorkTree: Schema.Boolean, freshness: VcsFreshness, }); export type VcsRepositoryIdentity = typeof VcsRepositoryIdentity.Type;