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.
This commit is contained in:
paseo-bot[bot]
2026-11-22 03:25:34 +00:00
parent 9d64a5fc50
commit 2e4def4eb7
2 changed files with 51 additions and 10 deletions
@@ -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();
@@ -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<ReconciliationResult> {
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<string, PersistedWorkspaceRecord[]>();
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(