From f909b12a067ce307b468b5e3f726a361d40d7514 Mon Sep 17 00:00:00 2001 From: Donald Silveira Date: Tue, 29 Sep 2026 20:20:45 -0300 Subject: [PATCH 1/3] fix: group remote projects by shared repository identity - Ignore machine-specific `gh repo set-default` when grouping projects across environments - Use the shared repository key for mobile project matching and automation selection --- .../threads/new-task-flow-provider.tsx | 5 +++-- .../threads/new-task-project-selection.ts | 9 ++++---- .../RepositoryIdentityResolver.test.ts | 2 ++ .../src/project/RepositoryIdentityResolver.ts | 17 +++++++++++++-- .../automations/AutomationsPage.tsx | 5 +++-- .../src/state/projectGrouping.test.ts | 21 +++++++++++++++++++ .../src/state/projectGrouping.ts | 20 ++++++++++++------ packages/contracts/src/environment.ts | 6 ++++++ 8 files changed, 68 insertions(+), 17 deletions(-) diff --git a/apps/mobile/src/features/threads/new-task-flow-provider.tsx b/apps/mobile/src/features/threads/new-task-flow-provider.tsx index 067a516622451..b60aba0722574 100644 --- a/apps/mobile/src/features/threads/new-task-flow-provider.tsx +++ b/apps/mobile/src/features/threads/new-task-flow-provider.tsx @@ -79,6 +79,7 @@ import { setPendingConnectionError, useSavedRemoteConnections, } from "../../state/use-remote-environment-registry"; +import { deriveRepositoryGroupKey } from "@t3tools/client-runtime/state/project-grouping"; import { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; import { type VcsRef } from "@t3tools/client-runtime/state/vcs"; import { @@ -341,7 +342,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) { // whatever unrelated project happens to be first on the other machine. Repository // identity is the primary signal; projects that haven't reported one yet (still // indexing) fall back to workspace basename / title so a valid host isn't hidden. - const selectedRepositoryKey = selectedProject?.repositoryIdentity?.canonicalKey ?? null; + const selectedRepositoryKey = deriveRepositoryGroupKey(selectedProject); // `|| null` (not `??`): a pending-task placeholder project can have an empty // workspaceRoot, and an "" basename would reject every real host below. const selectedWorkspaceBasename = selectedProject?.workspaceRoot.split("/").at(-1) || null; @@ -356,7 +357,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) { if (selectedRepositoryKey === null && selectedWorkspaceBasename === null) { return true; } - const projectKey = project.repositoryIdentity?.canonicalKey ?? null; + const projectKey = deriveRepositoryGroupKey(project); if (selectedRepositoryKey !== null && projectKey !== null) { return projectKey === selectedRepositoryKey; } diff --git a/apps/mobile/src/features/threads/new-task-project-selection.ts b/apps/mobile/src/features/threads/new-task-project-selection.ts index 27d1303c3a458..6da8eb63eed3d 100644 --- a/apps/mobile/src/features/threads/new-task-project-selection.ts +++ b/apps/mobile/src/features/threads/new-task-project-selection.ts @@ -1,3 +1,4 @@ +import { deriveRepositoryGroupKey } from "@t3tools/client-runtime/state/project-grouping"; import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; import type { EnvironmentId } from "@t3tools/contracts"; @@ -56,7 +57,7 @@ export function resolveEnvironmentProjectMatch( projectsOnTarget: ReadonlyArray, selectedProject: EnvironmentProject | null, ): EnvironmentProject | null { - const repositoryKey = selectedProject?.repositoryIdentity?.canonicalKey ?? null; + const repositoryKey = deriveRepositoryGroupKey(selectedProject); // `|| null` (not `??`): a pending-task placeholder project can have an empty // workspaceRoot, and an "" basename would match nothing meaningful. const workspaceBasename = selectedProject?.workspaceRoot.split("/").at(-1) || null; @@ -64,14 +65,12 @@ export function resolveEnvironmentProjectMatch( // side; two known, different repositories never match on a shared basename // or title (mirrors the environment list filter in the new-task flow). const isKnownMismatch = (project: EnvironmentProject) => { - const projectKey = project.repositoryIdentity?.canonicalKey ?? null; + const projectKey = deriveRepositoryGroupKey(project); return repositoryKey !== null && projectKey !== null && projectKey !== repositoryKey; }; return ( (repositoryKey !== null - ? projectsOnTarget.find( - (project) => (project.repositoryIdentity?.canonicalKey ?? null) === repositoryKey, - ) + ? projectsOnTarget.find((project) => deriveRepositoryGroupKey(project) === repositoryKey) : undefined) ?? (workspaceBasename !== null ? projectsOnTarget.find( diff --git a/apps/server/src/project/RepositoryIdentityResolver.test.ts b/apps/server/src/project/RepositoryIdentityResolver.test.ts index 3db6fa0f961b9..de63aa872663b 100644 --- a/apps/server/src/project/RepositoryIdentityResolver.test.ts +++ b/apps/server/src/project/RepositoryIdentityResolver.test.ts @@ -310,6 +310,8 @@ it.layer(NodeServices.layer)("RepositoryIdentityResolverLive", (it) => { expect(identity?.locator.remoteName).toBe("origin"); expect(identity?.canonicalKey).toBe("github.com/julius/t3code"); + // Grouping ignores the per-machine gh default. + expect(identity?.groupKey).toBe("github.com/t3tools/t3code"); }).pipe(Effect.provide(RepositoryIdentityResolver.layer)), ); diff --git a/apps/server/src/project/RepositoryIdentityResolver.ts b/apps/server/src/project/RepositoryIdentityResolver.ts index 7cbf502345522..0478f1fe7a9dc 100644 --- a/apps/server/src/project/RepositoryIdentityResolver.ts +++ b/apps/server/src/project/RepositoryIdentityResolver.ts @@ -105,9 +105,11 @@ function pickPrimaryRemote( function buildRepositoryIdentity(input: { readonly remoteName: string; readonly remoteUrl: string; + readonly groupRemoteUrl: string; readonly rootPath: string; }): RepositoryIdentity { const canonicalKey = normalizeGitRemoteUrl(input.remoteUrl); + const groupKey = normalizeGitRemoteUrl(input.groupRemoteUrl); const sourceControlProvider = detectSourceControlProviderFromGitRemoteUrl(input.remoteUrl); const repositoryPath = canonicalKey.split("/").slice(1).join("/"); const repositoryPathSegments = repositoryPath.split("/").filter((segment) => segment.length > 0); @@ -116,6 +118,7 @@ function buildRepositoryIdentity(input: { return { canonicalKey, + groupKey, locator: { source: "git-remote", remoteName: input.remoteName, @@ -173,8 +176,18 @@ const resolveRepositoryIdentityFromCacheKey = Effect.fn( ghDefaultResult._tag === "Some" && ghDefaultResult.value.code === 0 ? parseGhDefaultRemote(ghDefaultResult.value.stdout) : null; - const remote = pickPrimaryRemote(parseRemoteFetchUrls(remoteResult.value.stdout), ghDefault); - return remote ? buildRepositoryIdentity({ ...remote, rootPath: cacheKey }) : null; + const remotes = parseRemoteFetchUrls(remoteResult.value.stdout); + const remote = pickPrimaryRemote(remotes, ghDefault); + // The gh default is per-machine config, so grouping across environments + // skips it; otherwise one checkout splits in two when machines disagree. + const groupRemote = pickPrimaryRemote(remotes, null); + return remote && groupRemote + ? buildRepositoryIdentity({ + ...remote, + groupRemoteUrl: groupRemote.remoteUrl, + rootPath: cacheKey, + }) + : null; }); export const make = Effect.fn("RepositoryIdentityResolver.make")(function* ( diff --git a/apps/web/src/components/automations/AutomationsPage.tsx b/apps/web/src/components/automations/AutomationsPage.tsx index 5d76191749766..b9228d09e1833 100644 --- a/apps/web/src/components/automations/AutomationsPage.tsx +++ b/apps/web/src/components/automations/AutomationsPage.tsx @@ -21,6 +21,7 @@ import { resolveProviderProfileFallbackModelSelection, } from "@t3tools/shared/projectSettings"; import { scopeThreadRef } from "@t3tools/client-runtime/environment"; +import { deriveRepositoryGroupKey } from "@t3tools/client-runtime/state/project-grouping"; import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; import { AUTOMATION_GITHUB_EVENT_LABELS, @@ -1170,10 +1171,10 @@ function matchProjectOn( ): ProjectId | null { if (!from) return null; const candidates = projects.filter((project) => project.environmentId === environmentId); - const repository = from.repositoryIdentity?.canonicalKey; + const repository = deriveRepositoryGroupKey(from); const match = (repository - ? candidates.find((project) => project.repositoryIdentity?.canonicalKey === repository) + ? candidates.find((project) => deriveRepositoryGroupKey(project) === repository) : undefined) ?? candidates.find((project) => project.workspaceRoot === from.workspaceRoot) ?? candidates.find((project) => project.title === from.title); diff --git a/packages/client-runtime/src/state/projectGrouping.test.ts b/packages/client-runtime/src/state/projectGrouping.test.ts index 4884c3b99bbcf..76da9e3713811 100644 --- a/packages/client-runtime/src/state/projectGrouping.test.ts +++ b/packages/client-runtime/src/state/projectGrouping.test.ts @@ -136,6 +136,27 @@ describe("buildProjectGroups", () => { } }); + it("groups one checkout across environments whose gh default repositories differ", () => { + const fork = { + ...repositoryIdentity, + canonicalKey: "github.com/julius/t3code", + groupKey: "github.com/t3tools/t3code", + }; + const projects = [ + makeProject("linux", "/home/julius/t3code"), + makeProject("mac", "/Users/julius/t3code", { + environmentId: EnvironmentId.make("environment-mac"), + repositoryIdentity: fork, + }), + ]; + + const groups = buildProjectGroups({ projects, settings: settings("repository") }); + + expect(groups.map((group) => group.members.map((member) => member.project.id))).toEqual([ + ["linux", "mac"], + ]); + }); + it("uses a shared custom title as the repository group's label", () => { const projects = [ makeProject("first", "/work/t3code", { title: "Custom project" }), diff --git a/packages/client-runtime/src/state/projectGrouping.ts b/packages/client-runtime/src/state/projectGrouping.ts index ce5c984214fdc..8fe2f17a30af7 100644 --- a/packages/client-runtime/src/state/projectGrouping.ts +++ b/packages/client-runtime/src/state/projectGrouping.ts @@ -96,27 +96,35 @@ export function resolveProjectGroupingMode( ); } +/** Key that identifies the same repository across environments, or null when unknown. */ +export function deriveRepositoryGroupKey( + project: Pick | null | undefined, +): string | null { + const identity = project?.repositoryIdentity; + return identity?.groupKey ?? identity?.canonicalKey ?? null; +} + function deriveRepositoryScopedKey( project: Pick, groupingMode: SidebarProjectGroupingMode, ): string | null { - const canonicalKey = project.repositoryIdentity?.canonicalKey; - if (!canonicalKey) { + const repositoryKey = deriveRepositoryGroupKey(project); + if (!repositoryKey) { return null; } if (groupingMode === "repository") { - return canonicalKey; + return repositoryKey; } const relativeProjectPath = deriveRepositoryRelativeProjectPath(project); if (relativeProjectPath === null) { - return canonicalKey; + return repositoryKey; } return relativeProjectPath.length === 0 - ? canonicalKey - : `${canonicalKey}::${relativeProjectPath}`; + ? repositoryKey + : `${repositoryKey}::${relativeProjectPath}`; } export function deriveLogicalProjectKey( diff --git a/packages/contracts/src/environment.ts b/packages/contracts/src/environment.ts index 089133d878a4d..89accc7b8d7b8 100644 --- a/packages/contracts/src/environment.ts +++ b/packages/contracts/src/environment.ts @@ -208,6 +208,12 @@ export type RepositoryIdentityLocator = typeof RepositoryIdentityLocator.Type; export const RepositoryIdentity = Schema.Struct({ canonicalKey: TrimmedNonEmptyString, + /** + * Key for matching the same checkout across environments. Unlike `canonicalKey` + * it ignores `gh repo set-default`, which is per-machine config. Older servers + * omit it; fall back to `canonicalKey`. + */ + groupKey: Schema.optionalKey(TrimmedNonEmptyString), locator: RepositoryIdentityLocator, /** Repository browser URL resolved from the server's configured hosting account. */ webUrl: Schema.optionalKey(TrimmedNonEmptyString), From 6184c1ca3fa519071140333926570ab7821c75e8 Mon Sep 17 00:00:00 2001 From: Donald Silveira Date: Tue, 29 Sep 2026 21:06:01 -0300 Subject: [PATCH 2/3] fix: group projects when only one server reports groupKey - Match repositories using both groupKey and canonicalKey - Keep grouped project references consistent across clients --- .../threads/new-task-flow-provider.tsx | 13 +- .../new-task-project-selection.test.ts | 14 +++ .../threads/new-task-project-selection.ts | 16 +-- apps/web/src/components/ChatView.tsx | 16 ++- .../automations/AutomationsPage.tsx | 7 +- .../src/state/projectGrouping.test.ts | 24 ++++ .../src/state/projectGrouping.ts | 116 +++++++++++++----- 7 files changed, 149 insertions(+), 57 deletions(-) diff --git a/apps/mobile/src/features/threads/new-task-flow-provider.tsx b/apps/mobile/src/features/threads/new-task-flow-provider.tsx index b60aba0722574..552399a3dd3a8 100644 --- a/apps/mobile/src/features/threads/new-task-flow-provider.tsx +++ b/apps/mobile/src/features/threads/new-task-flow-provider.tsx @@ -79,7 +79,7 @@ import { setPendingConnectionError, useSavedRemoteConnections, } from "../../state/use-remote-environment-registry"; -import { deriveRepositoryGroupKey } from "@t3tools/client-runtime/state/project-grouping"; +import { sharesRepository } from "@t3tools/client-runtime/state/project-grouping"; import { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; import { type VcsRef } from "@t3tools/client-runtime/state/vcs"; import { @@ -342,7 +342,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) { // whatever unrelated project happens to be first on the other machine. Repository // identity is the primary signal; projects that haven't reported one yet (still // indexing) fall back to workspace basename / title so a valid host isn't hidden. - const selectedRepositoryKey = deriveRepositoryGroupKey(selectedProject); + const selectedRepositoryIdentity = selectedProject?.repositoryIdentity ?? null; // `|| null` (not `??`): a pending-task placeholder project can have an empty // workspaceRoot, and an "" basename would reject every real host below. const selectedWorkspaceBasename = selectedProject?.workspaceRoot.split("/").at(-1) || null; @@ -354,12 +354,11 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) { readonly environmentLabel: string; }> = []; const hostsSelectedRepository = (project: EnvironmentProject) => { - if (selectedRepositoryKey === null && selectedWorkspaceBasename === null) { + if (selectedRepositoryIdentity === null && selectedWorkspaceBasename === null) { return true; } - const projectKey = deriveRepositoryGroupKey(project); - if (selectedRepositoryKey !== null && projectKey !== null) { - return projectKey === selectedRepositoryKey; + if (selectedRepositoryIdentity !== null && project.repositoryIdentity !== null) { + return sharesRepository({ repositoryIdentity: selectedRepositoryIdentity }, project); } return ( project.workspaceRoot.split("/").at(-1) === selectedWorkspaceBasename || @@ -387,7 +386,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) { }, [ projects, savedConnectionsById, - selectedRepositoryKey, + selectedRepositoryIdentity, selectedWorkspaceBasename, selectedProjectTitle, ]); diff --git a/apps/mobile/src/features/threads/new-task-project-selection.test.ts b/apps/mobile/src/features/threads/new-task-project-selection.test.ts index 12f669530aae6..9cc09df6db725 100644 --- a/apps/mobile/src/features/threads/new-task-project-selection.test.ts +++ b/apps/mobile/src/features/threads/new-task-project-selection.test.ts @@ -17,6 +17,7 @@ function makeProject( readonly title?: string; readonly workspaceRoot?: string; readonly repositoryKey?: string; + readonly groupKey?: string; } = {}, ): EnvironmentProject { return { @@ -27,6 +28,7 @@ function makeProject( repositoryIdentity: options.repositoryKey ? { canonicalKey: options.repositoryKey, + ...(options.groupKey ? { groupKey: options.groupKey } : {}), locator: { source: "git-remote", remoteName: "origin", @@ -71,6 +73,18 @@ describe("getProjectScopeSelectionTarget", () => { }); describe("resolveEnvironmentProjectMatch", () => { + it("follows the repository onto a machine whose server predates groupKey", () => { + const selected = makeProject("t3code", "mac", { + repositoryKey: "github.com/julius/t3code", + groupKey: "github.com/t3tools/t3code", + }); + const target = [ + makeProject("t3code-fork", "server", { repositoryKey: "github.com/julius/other" }), + makeProject("t3code-clone", "server", { repositoryKey: "github.com/julius/t3code" }), + ]; + expect(resolveEnvironmentProjectMatch(target, selected)).toBe(target[1]); + }); + it("follows the same repository onto the target machine", () => { const selected = makeProject("t3code", "mac", { repositoryKey: "github.com/t3tools/t3code" }); const target = [ diff --git a/apps/mobile/src/features/threads/new-task-project-selection.ts b/apps/mobile/src/features/threads/new-task-project-selection.ts index 6da8eb63eed3d..010564170fe4d 100644 --- a/apps/mobile/src/features/threads/new-task-project-selection.ts +++ b/apps/mobile/src/features/threads/new-task-project-selection.ts @@ -1,4 +1,4 @@ -import { deriveRepositoryGroupKey } from "@t3tools/client-runtime/state/project-grouping"; +import { sharesRepository } from "@t3tools/client-runtime/state/project-grouping"; import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; import type { EnvironmentId } from "@t3tools/contracts"; @@ -57,20 +57,20 @@ export function resolveEnvironmentProjectMatch( projectsOnTarget: ReadonlyArray, selectedProject: EnvironmentProject | null, ): EnvironmentProject | null { - const repositoryKey = deriveRepositoryGroupKey(selectedProject); + const hasRepository = selectedProject?.repositoryIdentity != null; // `|| null` (not `??`): a pending-task placeholder project can have an empty // workspaceRoot, and an "" basename would match nothing meaningful. const workspaceBasename = selectedProject?.workspaceRoot.split("/").at(-1) || null; // The weaker signals only apply where identity is unknown on at least one // side; two known, different repositories never match on a shared basename // or title (mirrors the environment list filter in the new-task flow). - const isKnownMismatch = (project: EnvironmentProject) => { - const projectKey = deriveRepositoryGroupKey(project); - return repositoryKey !== null && projectKey !== null && projectKey !== repositoryKey; - }; + const isKnownMismatch = (project: EnvironmentProject) => + hasRepository && + project.repositoryIdentity !== null && + !sharesRepository(selectedProject, project); return ( - (repositoryKey !== null - ? projectsOnTarget.find((project) => deriveRepositoryGroupKey(project) === repositoryKey) + (hasRepository + ? projectsOnTarget.find((project) => sharesRepository(selectedProject, project)) : undefined) ?? (workspaceBasename !== null ? projectsOnTarget.find( diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 376657f324a82..db0e6551820bf 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -2458,10 +2458,18 @@ export default function ChatView(props: ChatViewProps) { ); const logicalProjectEnvironments = useMemo(() => { if (!activeProject) return []; - const logicalKey = deriveLogicalProjectKeyFromSettings(activeProject, projectGroupingSettings); - const memberProjects = allProjects.filter( - (p) => deriveLogicalProjectKeyFromSettings(p, projectGroupingSettings) === logicalKey, - ); + // Follow the sidebar's groups, which also merge projects whose servers + // report different repository keys. + const logicalKeyByPhysicalKey = buildPhysicalToLogicalProjectKeyMap({ + projects: allProjects, + settings: projectGroupingSettings, + primaryEnvironmentId, + }); + const logicalKeyOf = (project: typeof activeProject) => + logicalKeyByPhysicalKey.get(derivePhysicalProjectKey(project)) ?? + deriveLogicalProjectKeyFromSettings(project, projectGroupingSettings); + const logicalKey = logicalKeyOf(activeProject); + const memberProjects = allProjects.filter((p) => logicalKeyOf(p) === logicalKey); const seen = new Set(); const envs: EnvironmentOption[] = []; for (const p of memberProjects) { diff --git a/apps/web/src/components/automations/AutomationsPage.tsx b/apps/web/src/components/automations/AutomationsPage.tsx index b9228d09e1833..e3b3349203454 100644 --- a/apps/web/src/components/automations/AutomationsPage.tsx +++ b/apps/web/src/components/automations/AutomationsPage.tsx @@ -21,7 +21,7 @@ import { resolveProviderProfileFallbackModelSelection, } from "@t3tools/shared/projectSettings"; import { scopeThreadRef } from "@t3tools/client-runtime/environment"; -import { deriveRepositoryGroupKey } from "@t3tools/client-runtime/state/project-grouping"; +import { sharesRepository } from "@t3tools/client-runtime/state/project-grouping"; import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; import { AUTOMATION_GITHUB_EVENT_LABELS, @@ -1171,11 +1171,8 @@ function matchProjectOn( ): ProjectId | null { if (!from) return null; const candidates = projects.filter((project) => project.environmentId === environmentId); - const repository = deriveRepositoryGroupKey(from); const match = - (repository - ? candidates.find((project) => deriveRepositoryGroupKey(project) === repository) - : undefined) ?? + candidates.find((project) => sharesRepository(from, project)) ?? candidates.find((project) => project.workspaceRoot === from.workspaceRoot) ?? candidates.find((project) => project.title === from.title); return match?.id ?? null; diff --git a/packages/client-runtime/src/state/projectGrouping.test.ts b/packages/client-runtime/src/state/projectGrouping.test.ts index 76da9e3713811..613c3ebcbaf5c 100644 --- a/packages/client-runtime/src/state/projectGrouping.test.ts +++ b/packages/client-runtime/src/state/projectGrouping.test.ts @@ -157,6 +157,30 @@ describe("buildProjectGroups", () => { ]); }); + it("groups one checkout while only one environment's server reports groupKey", () => { + const fork = { ...repositoryIdentity, canonicalKey: "github.com/julius/t3code" }; + const mac = EnvironmentId.make("environment-mac"); + const projects = [ + makeProject("linux", "/home/julius/t3code", { + repositoryIdentity: { ...fork, groupKey: "github.com/t3tools/t3code" }, + }), + makeProject("mac", "/Users/julius/t3code", { environmentId: mac, repositoryIdentity: fork }), + ]; + + for (const mode of ["repository", "repository_path"] as const) { + const groups = buildProjectGroups({ + projects, + settings: settings(mode), + preferredEnvironmentId: mac, + }); + + expect(groups).toHaveLength(1); + expect(groups[0]?.key).toBe("github.com/julius/t3code"); + expect(groups[0]?.members.map((member) => member.project.id)).toEqual(["linux", "mac"]); + expect(groups[0]?.memberProjectRefs).toHaveLength(2); + } + }); + it("uses a shared custom title as the repository group's label", () => { const projects = [ makeProject("first", "/work/t3code", { title: "Custom project" }), diff --git a/packages/client-runtime/src/state/projectGrouping.ts b/packages/client-runtime/src/state/projectGrouping.ts index 8fe2f17a30af7..6dc8603ba5a9a 100644 --- a/packages/client-runtime/src/state/projectGrouping.ts +++ b/packages/client-runtime/src/state/projectGrouping.ts @@ -96,35 +96,40 @@ export function resolveProjectGroupingMode( ); } -/** Key that identifies the same repository across environments, or null when unknown. */ -export function deriveRepositoryGroupKey( +/** + * Keys that identify a project's repository across environments, preferred key + * first. Servers before `groupKey` report only `canonicalKey`, so two projects + * are the same repository when they share any key. + */ +export function deriveRepositoryKeys( project: Pick | null | undefined, -): string | null { +): ReadonlyArray { const identity = project?.repositoryIdentity; - return identity?.groupKey ?? identity?.canonicalKey ?? null; + if (!identity) return []; + return identity.groupKey && identity.groupKey !== identity.canonicalKey + ? [identity.groupKey, identity.canonicalKey] + : [identity.canonicalKey]; } -function deriveRepositoryScopedKey( +export function sharesRepository( + left: Pick | null | undefined, + right: Pick | null | undefined, +): boolean { + const rightKeys = deriveRepositoryKeys(right); + return deriveRepositoryKeys(left).some((key) => rightKeys.includes(key)); +} + +function deriveRepositoryScopedKeys( project: Pick, groupingMode: SidebarProjectGroupingMode, -): string | null { - const repositoryKey = deriveRepositoryGroupKey(project); - if (!repositoryKey) { - return null; - } - - if (groupingMode === "repository") { - return repositoryKey; - } +): ReadonlyArray { + const repositoryKeys = deriveRepositoryKeys(project); + if (groupingMode === "repository") return repositoryKeys; const relativeProjectPath = deriveRepositoryRelativeProjectPath(project); - if (relativeProjectPath === null) { - return repositoryKey; - } - - return relativeProjectPath.length === 0 - ? repositoryKey - : `${repositoryKey}::${relativeProjectPath}`; + return relativeProjectPath + ? repositoryKeys.map((key) => `${key}::${relativeProjectPath}`) + : repositoryKeys; } export function deriveLogicalProjectKey( @@ -142,7 +147,7 @@ export function deriveLogicalProjectKey( } return ( - deriveRepositoryScopedKey(project, groupingMode) ?? + deriveRepositoryScopedKeys(project, groupingMode)[0] ?? derivePhysicalProjectKey(project) ?? scopedProjectKey(scopeProjectRef(project.environmentId, project.id)) ); @@ -268,24 +273,70 @@ export function buildProjectGroups(input: { } } - const logicalKeyByPhysicalKey = new Map(); - const groupedMembers = new Map[]>(); + // Projects whose repository keys overlap share a group even when their + // preferred keys differ, as when only one environment's server sends `groupKey`. + const parentByLogicalKey = new Map(); + const findRoot = (logicalKey: string): string => { + const parent = parentByLogicalKey.get(logicalKey); + return parent === undefined ? logicalKey : findRoot(parent); + }; + const logicalKeyByRepositoryKey = new Map(); + const entries: Array<{ + readonly logicalKey: string; + readonly member: ProjectGroupMember; + }> = []; for (const [physicalProjectKey, physicalProjects] of projectsByPhysicalKey) { const winner = physicalProjects.reduce((current, candidate) => shouldReplacePhysicalProjectWinner(current, candidate) ? candidate : current, ); const identitySource = selectProjectIdentitySource(physicalProjects, winner); - const logicalKey = deriveLogicalProjectKey(identitySource, { - groupingMode: resolveProjectGroupingMode(winner, input.settings), - }); - logicalKeyByPhysicalKey.set(physicalProjectKey, logicalKey); - const member = { physicalProjectKey, project: winner }; - const existing = groupedMembers.get(logicalKey); + const groupingMode = resolveProjectGroupingMode(winner, input.settings); + const logicalKey = deriveLogicalProjectKey(identitySource, { groupingMode }); + if (groupingMode !== "separate") { + for (const repositoryKey of deriveRepositoryScopedKeys(identitySource, groupingMode)) { + const owner = logicalKeyByRepositoryKey.get(repositoryKey); + if (owner === undefined) { + logicalKeyByRepositoryKey.set(repositoryKey, logicalKey); + continue; + } + const ownerRoot = findRoot(owner); + const root = findRoot(logicalKey); + if (ownerRoot !== root) parentByLogicalKey.set(root, ownerRoot); + } + } + entries.push({ logicalKey, member: { physicalProjectKey, project: winner } }); + } + + const entriesByRoot = new Map(); + for (const entry of entries) { + const root = findRoot(entry.logicalKey); + const existing = entriesByRoot.get(root); if (existing) { - existing.push(member); + existing.push(entry); } else { - groupedMembers.set(logicalKey, [member]); + entriesByRoot.set(root, [entry]); + } + } + + // A merged group keeps the preferred environment's key, which is the key its + // local project derives on its own. + const preferredEnvironmentId = input.preferredEnvironmentId ?? null; + const logicalKeyByPhysicalKey = new Map(); + const groupedMembers = new Map[]>(); + for (const groupEntries of entriesByRoot.values()) { + const representativeEntry = + (preferredEnvironmentId + ? groupEntries.find( + (entry) => entry.member.project.environmentId === preferredEnvironmentId, + ) + : undefined) ?? groupEntries[0]!; + for (const entry of groupEntries) { + logicalKeyByPhysicalKey.set(entry.member.physicalProjectKey, representativeEntry.logicalKey); } + groupedMembers.set( + representativeEntry.logicalKey, + groupEntries.map((entry) => entry.member), + ); } const projectRefsByLogicalKey = new Map(); @@ -307,7 +358,6 @@ export function buildProjectGroups(input: { } } - const preferredEnvironmentId = input.preferredEnvironmentId ?? null; return Array.from(groupedMembers, ([key, members]) => { const representative = (preferredEnvironmentId From e1bb4f6b5682ce2b4c6b1ac64b12652c849580c2 Mon Sep 17 00:00:00 2001 From: Donald Silveira Date: Tue, 29 Sep 2026 21:33:33 -0300 Subject: [PATCH 3/3] fix(client-runtime): keep repository key helper module-private Knip flagged deriveRepositoryKeys as an unused export; only sharesRepository and grouping use it inside the module. Co-Authored-By: Claude Opus 5.5 (1M context) --- packages/client-runtime/src/state/projectGrouping.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/client-runtime/src/state/projectGrouping.ts b/packages/client-runtime/src/state/projectGrouping.ts index 6dc8603ba5a9a..78ce72bd29543 100644 --- a/packages/client-runtime/src/state/projectGrouping.ts +++ b/packages/client-runtime/src/state/projectGrouping.ts @@ -101,7 +101,7 @@ export function resolveProjectGroupingMode( * first. Servers before `groupKey` report only `canonicalKey`, so two projects * are the same repository when they share any key. */ -export function deriveRepositoryKeys( +function deriveRepositoryKeys( project: Pick | null | undefined, ): ReadonlyArray { const identity = project?.repositoryIdentity;