From 2e4def4eb775a50899e3e793189334b7bb81bf17 Mon Sep 17 00:00:00 2001 From: "paseo-bot[bot]" <266920839+paseo-bot[bot]@users.noreply.github.com> Date: Sun, 22 Nov 2026 03:25:34 +0000 Subject: [PATCH] fix(server): reconcile projects nested under a changed Git root Git initialization or removal changes the checkout for directories beneath the watched root. Include nested projects and workspaces in the event-driven pass, including workspaces attached to another project. --- ...rkspace-reconciliation-observation.test.ts | 30 ++++++++++++++++++ .../workspace-reconciliation-service.ts | 31 +++++++++++++------ 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/packages/server/src/server/workspace-reconciliation-observation.test.ts b/packages/server/src/server/workspace-reconciliation-observation.test.ts index 98817c61e8..b3d6eeea6b 100644 --- a/packages/server/src/server/workspace-reconciliation-observation.test.ts +++ b/packages/server/src/server/workspace-reconciliation-observation.test.ts @@ -497,6 +497,36 @@ describe("observed workspace placement", () => { observed.dispose(); }); + test("updates nested projects and workspaces when a parent directory becomes a Git repository", async () => { + const observed = new ObservedPlacements([ + { id: "parent", root: "repo", workspaces: [{ id: "parent-ws", cwd: "repo" }] }, + { id: "nested", root: "repo/sub", workspaces: [{ id: "nested-ws", cwd: "repo/sub" }] }, + { id: "outside", root: "outside", workspaces: [{ id: "attached-ws", cwd: "repo/sub" }] }, + { id: "unrelated", root: "repo-other" }, + ]); + await observed.start(); + try { + observed.makeProjectGit("parent"); + observed.makeProjectGit("nested"); + + observed.change("repo", ".git"); + await observed.advanceBy(DEBOUNCE_MS); + + expect(observed.projectUpdates).toContainEqual({ + kind: "upsert", + project: expect.objectContaining({ projectId: "nested", kind: "git" }), + }); + expect(await observed.placement("nested-ws")).toMatchObject({ kind: "local_checkout" }); + expect(await observed.placement("attached-ws")).toMatchObject({ kind: "local_checkout" }); + expect(observed.workspaceBatches.map((batch) => batch.sort())).toEqual([ + ["attached-ws", "nested-ws", "parent-ws"], + ]); + expect(observed.takeCheckoutReads()).toEqual(["outside", "repo", "repo/sub"]); + } finally { + observed.dispose(); + } + }); + test("turns Git changes under several roots into one read per changed root", async () => { const observed = new ObservedPlacements(threeProjects(1)); await observed.start(); diff --git a/packages/server/src/server/workspace-reconciliation-service.ts b/packages/server/src/server/workspace-reconciliation-service.ts index 43e28d6083..b6fd310f0b 100644 --- a/packages/server/src/server/workspace-reconciliation-service.ts +++ b/packages/server/src/server/workspace-reconciliation-service.ts @@ -11,7 +11,7 @@ import type { PersistedWorkspaceRecord, } from "./workspace-registry.js"; import type { WorkspaceGitService } from "./workspace-git-service.js"; -import { areEquivalentPaths } from "../utils/path.js"; +import { areEquivalentPaths, isPathInsideRoot } from "../utils/path.js"; import { deriveProjectKind, reconcileWorkspacePlacement, @@ -216,7 +216,7 @@ export class WorkspaceReconciliationService { /** * Reconciles mutable Git facts only; never archives missing records. With - * `rootPaths`, only the projects rooted there and their workspaces are read. + * `rootPaths`, read projects and workspaces whose checkouts can change under those paths. */ async reconcileGitMetadata(options?: { rootPaths?: string[] }): Promise { const start = Date.now(); @@ -226,15 +226,22 @@ export class WorkspaceReconciliationService { this.workspaceRegistry.listActive(), ]); const rootPaths = options?.rootPaths; - const projects = rootPaths - ? allProjects.filter((project) => - rootPaths.some((rootPath) => areEquivalentPaths(rootPath, project.rootPath)), - ) - : allProjects; - const projectIds = new Set(projects.map((project) => project.projectId)); + const isAffectedDirectory = (cwd: string) => + rootPaths?.some((rootPath) => isPathInsideRoot(rootPath, cwd)) ?? true; + // A nested project can gain or lose its Git checkout when a parent .git changes. + const affectedProjectIds = new Set( + allProjects + .filter((project) => !project.archivedAt && isAffectedDirectory(project.rootPath)) + .map((project) => project.projectId), + ); + // An explicitly chosen project can own a workspace outside its root. const workspacesByProject = new Map(); const workspaceStates = await mapBounded( - workspaces.filter((w) => !w.archivedAt && (!rootPaths || projectIds.has(w.projectId))), + workspaces.filter( + (workspace) => + !workspace.archivedAt && + (affectedProjectIds.has(workspace.projectId) || isAffectedDirectory(workspace.cwd)), + ), async (workspace) => ({ workspace, state: await this.inspectDirectory(workspace.cwd) }), ); for (const { workspace, state } of workspaceStates) { @@ -245,7 +252,11 @@ export class WorkspaceReconciliationService { workspacesByProject.set(workspace.projectId, siblings); } const projectStates = await mapBounded( - projects.filter((p) => !p.archivedAt), + allProjects.filter( + (project) => + !project.archivedAt && + (affectedProjectIds.has(project.projectId) || workspacesByProject.has(project.projectId)), + ), async (project) => ({ project, state: await this.inspectDirectory(project.rootPath) }), ); await this.reconcileGitMetadataForProjects(