diff --git a/src/renderer/src/components/sidebar/worktree-list-groups.test.ts b/src/renderer/src/components/sidebar/worktree-list-groups.test.ts index afc712abe..2633d036e 100644 --- a/src/renderer/src/components/sidebar/worktree-list-groups.test.ts +++ b/src/renderer/src/components/sidebar/worktree-list-groups.test.ts @@ -343,6 +343,70 @@ describe('buildRows with pinned worktrees', () => { ]) }) + it('splits same-host checkouts of one project into separate per-setup groups', () => { + // Why: multiple local clones/worktrees of one repo share the GitHub slug, so + // collapsing to the project would merge them into one arbitrarily-named group. + // They are distinct ProjectHostSetups on the same host and must stay separate. + const repoB: Repo = { ...repo, id: 'repo-2', path: '/tmp/orca-2', displayName: 'orca-2' } + const worktreeB: Worktree = { + ...worktree, + id: 'wt-2', + repoId: repoB.id, + path: '/tmp/orca-2-feature', + displayName: 'feature-b' + } + const localSetupB: ProjectHostSetup = { + ...projectHostSetups[0]!, + id: repoB.id, + repoId: repoB.id, + path: repoB.path, + displayName: repoB.displayName + } + const rows = buildRows( + 'repo', + [worktree, worktreeB], + new Map([ + [repo.id, repo], + [repoB.id, repoB] + ]), + null, + new Set(), + undefined, + undefined, + undefined, + {}, + new Map([ + [worktree.id, worktree], + [worktreeB.id, worktreeB] + ]), + false, + undefined, + [], + new Set(), + new Map(), + [], + { + projects: [{ ...project, sourceRepoIds: [repo.id, repoB.id] }], + projectHostSetups: [projectHostSetups[0]!, localSetupB] + } + ) + + const headers = rows.filter((row) => row.type === 'header') + expect(headers).toHaveLength(2) + expect(headers).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + key: 'project:github:stablyai/orca::setup:repo-1', + label: 'orca' + }), + expect.objectContaining({ + key: 'project:github:stablyai/orca::setup:repo-2', + label: 'orca-2' + }) + ]) + ) + }) + it('uses saved host labels for mixed-host sidebar card badges', () => { const runtimeRepo: Repo = { ...remoteRepo, diff --git a/src/renderer/src/components/sidebar/worktree-list-groups.ts b/src/renderer/src/components/sidebar/worktree-list-groups.ts index 97e073fd4..1fe4d7122 100644 --- a/src/renderer/src/components/sidebar/worktree-list-groups.ts +++ b/src/renderer/src/components/sidebar/worktree-list-groups.ts @@ -134,6 +134,14 @@ type WorktreeGroupEntry = { type ProjectGroupingIndex = { projectById: Map setupByRepoId: Map + // Why: `${projectId}::${hostId}` pairs that back more than one setup — i.e. the + // same project checked out multiple times on one host (independent clones or + // worktrees). Those setups must not collapse into a single project group. + multiSetupProjectHostKeys: Set +} + +function projectHostKey(projectId: string, hostId: string): string { + return `${projectId}::${hostId}` } function buildProjectGroupingIndex(model?: ProjectGroupingModel): ProjectGroupingIndex | null { @@ -142,9 +150,21 @@ function buildProjectGroupingIndex(model?: ProjectGroupingModel): ProjectGroupin if (projects.length === 0 || projectHostSetups.length === 0) { return null } + const setupCountByProjectHost = new Map() + for (const setup of projectHostSetups) { + const key = projectHostKey(setup.projectId, setup.hostId) + setupCountByProjectHost.set(key, (setupCountByProjectHost.get(key) ?? 0) + 1) + } + const multiSetupProjectHostKeys = new Set() + for (const [key, count] of setupCountByProjectHost) { + if (count > 1) { + multiSetupProjectHostKeys.add(key) + } + } return { projectById: new Map(projects.map((project) => [project.id, project])), - setupByRepoId: new Map(projectHostSetups.map((setup) => [setup.repoId, setup])) + setupByRepoId: new Map(projectHostSetups.map((setup) => [setup.repoId, setup])), + multiSetupProjectHostKeys } } @@ -163,6 +183,17 @@ function getProjectGroupingForRepo( repo } } + if (projectIndex?.multiSetupProjectHostKeys.has(projectHostKey(setup.projectId, setup.hostId))) { + // Why: this project is set up more than once on this host, so each checkout + // keeps its own group (labelled by its folder) instead of collapsing into a + // single project header named after whichever folder was added first. + return { + key: `project:${project.id}::setup:${repoId}`, + label: repo?.displayName ?? setup.displayName, + repo, + projectId: project.id + } + } return { key: `project:${project.id}`, label: project.displayName,