From 72e1256444272ce9489b138ecb311349d348c130 Mon Sep 17 00:00:00 2001 From: sidreyas Date: Sun, 22 Nov 2026 10:08:13 +0530 Subject: [PATCH 1/2] fix(server): checkpoint repository scopes walk the project folder once per turn CheckpointService.repositoryScopes asked WorkspaceFolders for the thread's folders and, in parallel, for its submodules. submodules read the same folders again through forThread, so every turn start and every checkpoint capture ran the full project folder scan twice. submodules now takes the folders the caller already has. Refs #40659 Co-Authored-By: Claude Opus 5.5 --- .../CheckpointService.test.ts | 94 +++++++++++++++++++ .../src/orchestration-v2/CheckpointService.ts | 9 +- apps/server/src/workspace/WorkspaceFolders.ts | 4 +- 3 files changed, 102 insertions(+), 5 deletions(-) diff --git a/apps/server/src/orchestration-v2/CheckpointService.test.ts b/apps/server/src/orchestration-v2/CheckpointService.test.ts index 0433208167a73..851173e498a60 100644 --- a/apps/server/src/orchestration-v2/CheckpointService.test.ts +++ b/apps/server/src/orchestration-v2/CheckpointService.test.ts @@ -18,15 +18,20 @@ import * as Cause from "effect/Cause"; import * as DateTime from "effect/DateTime"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; import * as Fiber from "effect/Fiber"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; +import * as Path from "effect/Path"; import * as Queue from "effect/Queue"; import * as Ref from "effect/Ref"; import * as TestClock from "effect/testing/TestClock"; import * as CheckpointStore from "../checkpointing/CheckpointStore.ts"; import * as CheckpointService from "./CheckpointService.ts"; +import * as ProjectStore from "./ProjectStore.ts"; +import * as SqlitePersistence from "../persistence/Sqlite.ts"; +import * as TaskFolderStore from "../workspace/TaskFolderStore.ts"; import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import * as WorkspaceFolders from "../workspace/WorkspaceFolders.ts"; @@ -771,3 +776,92 @@ it.effect("keeps the pre-rewind snapshot of a folder a failed rewind could not p assert.deepEqual(captured, ["/repo/first", "/repo/second", "/repo/second"]); }).pipe(Effect.provide(layerTest)); }); + +it.effect("walks the project folder once to find a thread's repositories and submodules", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const workspaceRoot = yield* fileSystem.realPath( + yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-checkpoint-scopes-" }), + ); + for (const folder of ["api/.git", "api/vendor/lib/.git", "notes"]) { + yield* fileSystem.makeDirectory(path.join(workspaceRoot, folder), { recursive: true }); + } + yield* fileSystem.writeFileString( + path.join(workspaceRoot, "api", ".gitmodules"), + '[submodule "lib"]\n\tpath = vendor/lib\n', + ); + const projectId = ProjectId.make("project:checkpoint-scopes"); + let rootReads = 0; + const counting = FileSystem.FileSystem.of({ + ...fileSystem, + readDirectory: (target, options) => { + if (target === workspaceRoot) rootReads += 1; + return fileSystem.readDirectory(target, options); + }, + }); + const layerTest = CheckpointService.layer.pipe( + Layer.provide( + Layer.mergeAll( + IdAllocator.layer, + Layer.mock(CheckpointStore.CheckpointStore)({}), + WorkspaceFolders.layer.pipe( + Layer.provide( + Layer.mock(ProjectStore.ProjectStoreV2)({ + get: () => + Effect.succeedSome({ + projectId, + title: "Project", + hasCustomTitle: false, + defaultModelSelection: null, + defaultThreadEnvMode: null, + autoPull: false, + faviconPath: null, + projectIcon: null, + scripts: [], + pinnedAt: null, + archivedAt: null, + createdAt: "2026-06-21T00:00:00.000Z", + updatedAt: "2026-06-21T00:00:00.000Z", + deletedAt: null, + workspaceRoot, + trackedFolders: [], + }), + }), + ), + Layer.provide(TaskFolderStore.layer.pipe(Layer.provide(SqlitePersistence.layerMemory))), + ), + ), + ), + Layer.provide(Layer.succeed(FileSystem.FileSystem, counting)), + Layer.provideMerge(NodeCrypto.layer), + ); + + const scopes = yield* Effect.gen(function* () { + const checkpoints = yield* CheckpointService.CheckpointServiceV2; + return yield* checkpoints.repositoryScopes({ + rootScope: { + id: CheckpointScopeId.make("checkpoint-scope:checkpoint-scopes"), + threadId: ThreadId.make("thread:checkpoint-scopes"), + runId: RunId.make("run:checkpoint-scopes:1"), + nodeId: NodeId.make("node:checkpoint-scopes:1"), + parentScopeId: null, + providerThreadId: ProviderThreadId.make("provider-thread:checkpoint-scopes"), + kind: "root_run", + ordinalWithinParent: 0, + advancesAppRunCount: true, + cwd: workspaceRoot, + createdAt: DateTime.makeUnsafe("2026-07-28T00:00:00.000Z"), + }, + projectId, + worktreePath: null, + }); + }).pipe(Effect.provide(layerTest)); + + assert.deepEqual( + scopes.map((scope) => scope.cwd), + [path.join(workspaceRoot, "api"), path.join(workspaceRoot, "api", "vendor", "lib")], + ); + assert.strictEqual(rootReads, 1); + }).pipe(Effect.provide(NodeServices.layer), Effect.scoped), +); diff --git a/apps/server/src/orchestration-v2/CheckpointService.ts b/apps/server/src/orchestration-v2/CheckpointService.ts index 079ac88211870..ec3e872d61c2b 100644 --- a/apps/server/src/orchestration-v2/CheckpointService.ts +++ b/apps/server/src/orchestration-v2/CheckpointService.ts @@ -924,10 +924,11 @@ export const layer: Layer.Layer< const repositoryScopesIfReadable: CheckpointServiceV2Shape["repositoryScopesIfReadable"] = Effect.fn("CheckpointService.repositoryScopes")( function* (input) { - const [workspace, submodules] = yield* Effect.all([ - workspaceFolders.forThread(input), - workspaceFolders.submodules(input), - ]); + const workspace = yield* workspaceFolders.forThread(input); + const submodules = yield* workspaceFolders.submodules({ + ...input, + folders: workspace.folders, + }); return yield* Effect.forEach( [...workspace.folders.filter((folder) => folder.isGitRepository), ...submodules], (folder) => diff --git a/apps/server/src/workspace/WorkspaceFolders.ts b/apps/server/src/workspace/WorkspaceFolders.ts index 776f90ea6e5b0..53fe776609a03 100644 --- a/apps/server/src/workspace/WorkspaceFolders.ts +++ b/apps/server/src/workspace/WorkspaceFolders.ts @@ -64,6 +64,8 @@ export class WorkspaceFolders extends Context.Service< readonly submodules: (input: { readonly projectId: ProjectId; readonly worktreePath: string | null; + /** The thread's `forThread` folders, when the caller has them; each read walks the project. */ + readonly folders?: ReadonlyArray; }) => Effect.Effect, WorkspaceFoldersError>; readonly list: ( input: WorkspaceListFoldersInput, @@ -506,7 +508,7 @@ const make = Effect.gen(function* () { const submodules: WorkspaceFolders["Service"]["submodules"] = Effect.fn( "WorkspaceFolders.submodules", )(function* (input) { - const { folders } = yield* forThread(input); + const folders = input.folders ?? (yield* forThread(input)).folders; // A worktree can outlive its project record; its submodules are then named by where they are. const projectRoot = yield* readProject(input.projectId).pipe( Effect.map((project) => path.resolve(project.workspaceRoot)), From ff4660d8d52581750cca97e862821d422d7968b9 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 04:59:58 +0000 Subject: [PATCH 2/2] refactor(server): checkpoint scopes always hand their folders to submodule discovery Co-Authored-By: Claude Opus 5.5 --- .../CheckpointService.test.ts | 94 ------------------- .../src/workspace/WorkspaceFolders.test.ts | 22 +++-- apps/server/src/workspace/WorkspaceFolders.ts | 7 +- 3 files changed, 16 insertions(+), 107 deletions(-) diff --git a/apps/server/src/orchestration-v2/CheckpointService.test.ts b/apps/server/src/orchestration-v2/CheckpointService.test.ts index 851173e498a60..0433208167a73 100644 --- a/apps/server/src/orchestration-v2/CheckpointService.test.ts +++ b/apps/server/src/orchestration-v2/CheckpointService.test.ts @@ -18,20 +18,15 @@ import * as Cause from "effect/Cause"; import * as DateTime from "effect/DateTime"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; -import * as FileSystem from "effect/FileSystem"; import * as Fiber from "effect/Fiber"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; -import * as Path from "effect/Path"; import * as Queue from "effect/Queue"; import * as Ref from "effect/Ref"; import * as TestClock from "effect/testing/TestClock"; import * as CheckpointStore from "../checkpointing/CheckpointStore.ts"; import * as CheckpointService from "./CheckpointService.ts"; -import * as ProjectStore from "./ProjectStore.ts"; -import * as SqlitePersistence from "../persistence/Sqlite.ts"; -import * as TaskFolderStore from "../workspace/TaskFolderStore.ts"; import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; import * as WorkspaceFolders from "../workspace/WorkspaceFolders.ts"; @@ -776,92 +771,3 @@ it.effect("keeps the pre-rewind snapshot of a folder a failed rewind could not p assert.deepEqual(captured, ["/repo/first", "/repo/second", "/repo/second"]); }).pipe(Effect.provide(layerTest)); }); - -it.effect("walks the project folder once to find a thread's repositories and submodules", () => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const workspaceRoot = yield* fileSystem.realPath( - yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3code-checkpoint-scopes-" }), - ); - for (const folder of ["api/.git", "api/vendor/lib/.git", "notes"]) { - yield* fileSystem.makeDirectory(path.join(workspaceRoot, folder), { recursive: true }); - } - yield* fileSystem.writeFileString( - path.join(workspaceRoot, "api", ".gitmodules"), - '[submodule "lib"]\n\tpath = vendor/lib\n', - ); - const projectId = ProjectId.make("project:checkpoint-scopes"); - let rootReads = 0; - const counting = FileSystem.FileSystem.of({ - ...fileSystem, - readDirectory: (target, options) => { - if (target === workspaceRoot) rootReads += 1; - return fileSystem.readDirectory(target, options); - }, - }); - const layerTest = CheckpointService.layer.pipe( - Layer.provide( - Layer.mergeAll( - IdAllocator.layer, - Layer.mock(CheckpointStore.CheckpointStore)({}), - WorkspaceFolders.layer.pipe( - Layer.provide( - Layer.mock(ProjectStore.ProjectStoreV2)({ - get: () => - Effect.succeedSome({ - projectId, - title: "Project", - hasCustomTitle: false, - defaultModelSelection: null, - defaultThreadEnvMode: null, - autoPull: false, - faviconPath: null, - projectIcon: null, - scripts: [], - pinnedAt: null, - archivedAt: null, - createdAt: "2026-06-21T00:00:00.000Z", - updatedAt: "2026-06-21T00:00:00.000Z", - deletedAt: null, - workspaceRoot, - trackedFolders: [], - }), - }), - ), - Layer.provide(TaskFolderStore.layer.pipe(Layer.provide(SqlitePersistence.layerMemory))), - ), - ), - ), - Layer.provide(Layer.succeed(FileSystem.FileSystem, counting)), - Layer.provideMerge(NodeCrypto.layer), - ); - - const scopes = yield* Effect.gen(function* () { - const checkpoints = yield* CheckpointService.CheckpointServiceV2; - return yield* checkpoints.repositoryScopes({ - rootScope: { - id: CheckpointScopeId.make("checkpoint-scope:checkpoint-scopes"), - threadId: ThreadId.make("thread:checkpoint-scopes"), - runId: RunId.make("run:checkpoint-scopes:1"), - nodeId: NodeId.make("node:checkpoint-scopes:1"), - parentScopeId: null, - providerThreadId: ProviderThreadId.make("provider-thread:checkpoint-scopes"), - kind: "root_run", - ordinalWithinParent: 0, - advancesAppRunCount: true, - cwd: workspaceRoot, - createdAt: DateTime.makeUnsafe("2026-07-28T00:00:00.000Z"), - }, - projectId, - worktreePath: null, - }); - }).pipe(Effect.provide(layerTest)); - - assert.deepEqual( - scopes.map((scope) => scope.cwd), - [path.join(workspaceRoot, "api"), path.join(workspaceRoot, "api", "vendor", "lib")], - ); - assert.strictEqual(rootReads, 1); - }).pipe(Effect.provide(NodeServices.layer), Effect.scoped), -); diff --git a/apps/server/src/workspace/WorkspaceFolders.test.ts b/apps/server/src/workspace/WorkspaceFolders.test.ts index 4c5b33bbf7776..3a02d202be891 100644 --- a/apps/server/src/workspace/WorkspaceFolders.test.ts +++ b/apps/server/src/workspace/WorkspaceFolders.test.ts @@ -330,17 +330,21 @@ it.layer(NodeServices.layer)("WorkspaceFolders", (it) => { folders: [], extraRoots: [], }); - assert.deepEqual(yield* workspaceFolders.submodules({ projectId, worktreePath: null }), [ - { name: "lib", path: path.join(workspaceRoot, "vendor", "lib"), isGitRepository: true }, - { - name: "deep", - path: path.join(workspaceRoot, "vendor", "lib", "deep"), - isGitRepository: true, - }, - ]); + const { folders } = yield* workspaceFolders.forThread({ projectId, worktreePath: null }); + assert.deepEqual( + yield* workspaceFolders.submodules({ projectId, worktreePath: null, folders }), + [ + { name: "lib", path: path.join(workspaceRoot, "vendor", "lib"), isGitRepository: true }, + { + name: "deep", + path: path.join(workspaceRoot, "vendor", "lib", "deep"), + isGitRepository: true, + }, + ], + ); // A worktree reaches its own submodules, named by where the project folder has them. assert.deepEqual( - yield* workspaceFolders.submodules({ projectId, worktreePath: worktree }), + yield* workspaceFolders.submodules({ projectId, worktreePath: worktree, folders: [] }), [ { name: "lib", diff --git a/apps/server/src/workspace/WorkspaceFolders.ts b/apps/server/src/workspace/WorkspaceFolders.ts index 8e5f903a69187..0824c647ce360 100644 --- a/apps/server/src/workspace/WorkspaceFolders.ts +++ b/apps/server/src/workspace/WorkspaceFolders.ts @@ -64,8 +64,8 @@ export class WorkspaceFolders extends Context.Service< readonly submodules: (input: { readonly projectId: ProjectId; readonly worktreePath: string | null; - /** The thread's `forThread` folders, when the caller has them; each read walks the project. */ - readonly folders?: ReadonlyArray; + /** The thread's `forThread` folders; passed in so the project is walked once. */ + readonly folders: ReadonlyArray; }) => Effect.Effect, WorkspaceFoldersError>; readonly list: ( input: WorkspaceListFoldersInput, @@ -510,7 +510,6 @@ const make = Effect.gen(function* () { const submodules: WorkspaceFolders["Service"]["submodules"] = Effect.fn( "WorkspaceFolders.submodules", )(function* (input) { - const folders = input.folders ?? (yield* forThread(input)).folders; // A worktree can outlive its project record; its submodules are then named by where they are. const projectRoot = yield* readProject(input.projectId).pipe( Effect.map((project) => path.resolve(project.workspaceRoot)), @@ -522,7 +521,7 @@ const make = Effect.gen(function* () { const identity = Option.getOrElse(projectRoot, () => root); return yield* submodulesOf([ { path: root, identity }, - ...folders.flatMap((folder) => + ...input.folders.flatMap((folder) => folder.isGitRepository ? [{ path: folder.path, identity: folder.repositoryPath ?? folder.path }] : [],