diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index 79b3a767b9..9f01fc26f9 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -2759,9 +2759,8 @@ export default function Sidebar() { }); }, [projectOrder, projects]); - // Build a mapping from physical project key -> logical project key. Matching - // projects can group across environments, but each rendered group keeps at - // most one member per environment so thread creation remains unambiguous. + // Build a mapping from physical project key -> logical project key so the + // sidebar, active route, and thread buckets all use the same grouping mode. const physicalToLogicalKey = useMemo(() => { return buildPhysicalToLogicalProjectKeyMap({ projects: orderedProjects, diff --git a/apps/web/src/environmentGrouping.test.ts b/apps/web/src/environmentGrouping.test.ts index a96735487a..eb1fef2676 100644 --- a/apps/web/src/environmentGrouping.test.ts +++ b/apps/web/src/environmentGrouping.test.ts @@ -451,7 +451,7 @@ describe("environment grouping", () => { }); describe("buildLogicalProjectKeyMap", () => { - it("groups matching projects across environments without merging distinct projects in one environment", () => { + it("groups matching projects by repository under repository grouping", () => { const localRoot = makeProject({ id: sharedProjectPrimaryId, environmentId: primaryEnvId, @@ -524,11 +524,53 @@ describe("environment grouping", () => { expect(keys.get(derivePhysicalProjectKey(localWeb))).toBe( keys.get(derivePhysicalProjectKey(remoteWeb)), ); - expect(keys.get(derivePhysicalProjectKey(localRoot))).not.toBe( + expect(keys.get(derivePhysicalProjectKey(localRoot))).toBe( keys.get(derivePhysicalProjectKey(localWeb)), ); }); + it("splits repo root and nested projects under repository path grouping", () => { + const rootProject = makeProject({ + id: sharedProjectPrimaryId, + environmentId: primaryEnvId, + name: "shared-repo", + cwd: "/workspace/repo", + repositoryIdentity: { + canonicalKey: SHARED_REPO_CANONICAL_KEY, + rootPath: "/workspace/repo", + locator: { + source: "git-remote", + remoteName: "origin", + remoteUrl: "https://github.com/example/shared-repo.git", + }, + }, + }); + const nestedProject = makeProject({ + id: localOnlyProjectId, + environmentId: primaryEnvId, + name: "web", + cwd: "/workspace/repo/apps/web", + repositoryIdentity: { + canonicalKey: SHARED_REPO_CANONICAL_KEY, + rootPath: "/workspace/repo", + locator: { + source: "git-remote", + remoteName: "origin", + remoteUrl: "https://github.com/example/shared-repo.git", + }, + }, + }); + + const keys = buildLogicalProjectKeyMap([rootProject, nestedProject], { + ...DEFAULT_GROUPING_SETTINGS, + sidebarProjectGroupingMode: "repository_path", + }); + + expect(keys.get(derivePhysicalProjectKey(rootProject))).not.toBe( + keys.get(derivePhysicalProjectKey(nestedProject)), + ); + }); + it("groups duplicate clones in the same environment when repo-relative paths match", () => { const firstClone = makeProject({ id: sharedProjectPrimaryId, diff --git a/apps/web/src/logicalProject.ts b/apps/web/src/logicalProject.ts index 9b161c515f..adde42e1ff 100644 --- a/apps/web/src/logicalProject.ts +++ b/apps/web/src/logicalProject.ts @@ -51,21 +51,6 @@ function deriveRepositoryRelativeProjectPath( return normalizedProjectPath.slice(rootPrefix.length).replaceAll("\\", "/"); } -function formatProjectGroupDiscriminator(value: string): string { - return value.length === 0 ? "." : value; -} - -function hasDuplicateEnvironment(projects: ReadonlyArray>): boolean { - const seen = new Set(); - for (const project of projects) { - if (seen.has(project.environmentId)) { - return true; - } - seen.add(project.environmentId); - } - return false; -} - export function derivePhysicalProjectKeyFromPath(environmentId: string, cwd: string): string { return `${environmentId}:${normalizeProjectPathForComparison(cwd)}`; } @@ -151,67 +136,11 @@ export function buildLogicalProjectKeyMap( projects: ReadonlyArray>, settings: ProjectGroupingSettings, ): Map { - const baseBuckets = new Map< - string, - Array> - >(); - const physicalKeyByProject = new Map< - Pick, - string - >(); + const result = new Map(); for (const project of projects) { const physicalKey = derivePhysicalProjectKey(project); - physicalKeyByProject.set(project, physicalKey); - const baseKey = deriveLogicalProjectKeyFromSettings(project, settings); - const bucket = baseBuckets.get(baseKey); - if (bucket) { - bucket.push(project); - } else { - baseBuckets.set(baseKey, [project]); - } - } - - const result = new Map(); - const assignBucket = ( - baseKey: string, - bucket: ReadonlyArray>, - ) => { - if (!hasDuplicateEnvironment(bucket)) { - for (const project of bucket) { - result.set(physicalKeyByProject.get(project)!, baseKey); - } - return; - } - - const buckets = new Map< - string, - Array> - >(); - for (const project of bucket) { - const relativePath = deriveRepositoryRelativeProjectPath(project); - const value = - relativePath === null - ? normalizeProjectPathForComparison(project.cwd) - : formatProjectGroupDiscriminator(relativePath); - const key = `${baseKey}::${value}`; - const existing = buckets.get(key); - if (existing) { - existing.push(project); - } else { - buckets.set(key, [project]); - } - } - - for (const [key, projectsForKey] of buckets) { - for (const project of projectsForKey) { - result.set(physicalKeyByProject.get(project)!, key); - } - } - }; - - for (const [baseKey, bucket] of baseBuckets) { - assignBucket(baseKey, bucket); + result.set(physicalKey, deriveLogicalProjectKeyFromSettings(project, settings)); } return result;