Compare commits

...
Author SHA1 Message Date
Julius Marminge e80d4a479c Simplify logical project grouping
- Use the configured grouping mode directly for sidebar and thread buckets
- Add coverage for repository-path grouping splitting root and nested projects
- Remove environment-based bucket splitting from logical key mapping
2026-04-27 11:16:24 -07:00
Julius Marminge e8f48ca8ff Group duplicate clones by repo-relative path
- Treat matching repo-relative paths as the same logical project
- Update grouping test to reflect the new duplicate-clone behavior
2026-04-27 11:12:07 -07:00
Julius Marminge c421b5d054 Stabilize project grouping across environments
- Build logical project key maps to avoid merging duplicate clones in one environment
- Reuse the mapping in chat, sidebar, thread creation, and runtime sync paths
- Add coverage for cross-environment grouping and duplicate clone separation
2026-04-27 10:33:36 -07:00
8 changed files with 263 additions and 34 deletions

No files matched your search

+20 -7
View File
@@ -117,7 +117,11 @@ import { getProviderModelCapabilities, resolveSelectableProvider } from "../prov
import { useSettings } from "../hooks/useSettings";
import { resolveAppModelSelection } from "../modelSelection";
import { isTerminalFocused } from "../lib/terminalFocus";
import { deriveLogicalProjectKeyFromSettings } from "../logicalProject";
import {
buildLogicalProjectKeyMap,
deriveLogicalProjectKeyFromSettings,
derivePhysicalProjectKey,
} from "../logicalProject";
import {
useSavedEnvironmentRegistryStore,
useSavedEnvironmentRuntimeStore,
@@ -856,11 +860,19 @@ export default function ChatView(props: ChatViewProps) {
sidebarProjectGroupingMode: settings.sidebarProjectGroupingMode,
sidebarProjectGroupingOverrides: settings.sidebarProjectGroupingOverrides,
}));
const logicalProjectKeyByPhysicalKey = useMemo(
() => buildLogicalProjectKeyMap(allProjects, projectGroupingSettings),
[allProjects, projectGroupingSettings],
);
const logicalProjectEnvironments = useMemo(() => {
if (!activeProject) return [];
const logicalKey = deriveLogicalProjectKeyFromSettings(activeProject, projectGroupingSettings);
const logicalKey =
logicalProjectKeyByPhysicalKey.get(derivePhysicalProjectKey(activeProject)) ??
deriveLogicalProjectKeyFromSettings(activeProject, projectGroupingSettings);
const memberProjects = allProjects.filter(
(p) => deriveLogicalProjectKeyFromSettings(p, projectGroupingSettings) === logicalKey,
(p) =>
(logicalProjectKeyByPhysicalKey.get(derivePhysicalProjectKey(p)) ??
deriveLogicalProjectKeyFromSettings(p, projectGroupingSettings)) === logicalKey,
);
const seen = new Set<string>();
const envs: Array<{
@@ -897,6 +909,7 @@ export default function ChatView(props: ChatViewProps) {
}, [
activeProject,
allProjects,
logicalProjectKeyByPhysicalKey,
projectGroupingSettings,
primaryEnvironmentId,
savedEnvironmentRegistry,
@@ -927,10 +940,9 @@ export default function ChatView(props: ChatViewProps) {
throw new Error("No active project is available for this pull request.");
}
const activeProjectRef = scopeProjectRef(activeProject.environmentId, activeProject.id);
const logicalProjectKey = deriveLogicalProjectKeyFromSettings(
activeProject,
projectGroupingSettings,
);
const logicalProjectKey =
logicalProjectKeyByPhysicalKey.get(derivePhysicalProjectKey(activeProject)) ??
deriveLogicalProjectKeyFromSettings(activeProject, projectGroupingSettings);
const storedDraftSession = getDraftSessionByLogicalProjectKey(logicalProjectKey);
if (storedDraftSession) {
setDraftThreadContext(storedDraftSession.draftId, input);
@@ -991,6 +1003,7 @@ export default function ChatView(props: ChatViewProps) {
getDraftSessionByLogicalProjectKey,
isServerThread,
navigate,
logicalProjectKeyByPhysicalKey,
projectGroupingSettings,
routeKind,
setDraftThreadContext,
+29 -6
View File
@@ -216,6 +216,17 @@ function formatProjectMemberActionLabel(
return member.environmentLabel ? `${member.environmentLabel} — ${member.cwd}` : member.cwd;
}
function hasDuplicateMemberEnvironment(members: readonly SidebarProjectGroupMember[]): boolean {
const seen = new Set<string>();
for (const member of members) {
if (seen.has(member.environmentId)) {
return true;
}
seen.add(member.environmentId);
}
return false;
}
function projectGroupingModeDescription(mode: SidebarProjectGroupingMode): string {
switch (mode) {
case "repository":
@@ -1674,8 +1685,15 @@ const SidebarProjectItem = memo(function SidebarProjectItem(props: SidebarProjec
event.preventDefault();
event.stopPropagation();
if (project.memberProjects.length === 1) {
createThreadForProjectMember(project.memberProjects[0]!);
if (
project.memberProjects.length === 1 ||
!hasDuplicateMemberEnvironment(project.memberProjects)
) {
const representative =
project.memberProjects.find(
(member) => member.environmentId === project.environmentId && member.id === project.id,
) ?? project.memberProjects[0]!;
createThreadForProjectMember(representative);
return;
}
@@ -1706,7 +1724,13 @@ const SidebarProjectItem = memo(function SidebarProjectItem(props: SidebarProjec
createThreadForProjectMember(targetMember);
})();
},
[createThreadForProjectMember, project.groupedProjectCount, project.memberProjects],
[
createThreadForProjectMember,
project.environmentId,
project.groupedProjectCount,
project.id,
project.memberProjects,
],
);
const attemptArchiveThread = useCallback(
@@ -2735,9 +2759,8 @@ export default function Sidebar() {
});
}, [projectOrder, projects]);
// Build a mapping from physical project key → logical project key for
// cross-environment grouping. Projects that share a repositoryIdentity
// canonicalKey are treated as one logical project in the sidebar.
// 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,
+162
View File
@@ -11,6 +11,7 @@ import {
type EnvironmentState,
} from "./store";
import {
buildLogicalProjectKeyMap,
deriveLogicalProjectKey,
deriveLogicalProjectKeyFromSettings,
derivePhysicalProjectKey,
@@ -449,6 +450,167 @@ describe("environment grouping", () => {
});
});
describe("buildLogicalProjectKeyMap", () => {
it("groups matching projects by repository under repository grouping", () => {
const localRoot = 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 localWeb = 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 remoteRoot = makeProject({
id: sharedProjectRemoteId,
environmentId: remoteEnvId,
name: "shared-repo",
cwd: "/srv/checkout",
repositoryIdentity: {
canonicalKey: SHARED_REPO_CANONICAL_KEY,
rootPath: "/srv/checkout",
locator: {
source: "git-remote",
remoteName: "origin",
remoteUrl: "https://github.com/example/shared-repo.git",
},
},
});
const remoteWeb = makeProject({
id: remoteOnlyProjectId,
environmentId: remoteEnvId,
name: "web",
cwd: "/srv/checkout/apps/web",
repositoryIdentity: {
canonicalKey: SHARED_REPO_CANONICAL_KEY,
rootPath: "/srv/checkout",
locator: {
source: "git-remote",
remoteName: "origin",
remoteUrl: "https://github.com/example/shared-repo.git",
},
},
});
const keys = buildLogicalProjectKeyMap(
[localRoot, localWeb, remoteRoot, remoteWeb],
DEFAULT_GROUPING_SETTINGS,
);
expect(keys.get(derivePhysicalProjectKey(localRoot))).toBe(
keys.get(derivePhysicalProjectKey(remoteRoot)),
);
expect(keys.get(derivePhysicalProjectKey(localWeb))).toBe(
keys.get(derivePhysicalProjectKey(remoteWeb)),
);
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,
environmentId: primaryEnvId,
name: "first",
cwd: "/workspace/first/repo",
repositoryIdentity: {
canonicalKey: SHARED_REPO_CANONICAL_KEY,
rootPath: "/workspace/first/repo",
locator: {
source: "git-remote",
remoteName: "origin",
remoteUrl: "https://github.com/example/shared-repo.git",
},
},
});
const secondClone = makeProject({
id: localOnlyProjectId,
environmentId: primaryEnvId,
name: "second",
cwd: "/workspace/second/repo",
repositoryIdentity: {
canonicalKey: SHARED_REPO_CANONICAL_KEY,
rootPath: "/workspace/second/repo",
locator: {
source: "git-remote",
remoteName: "origin",
remoteUrl: "https://github.com/example/shared-repo.git",
},
},
});
const keys = buildLogicalProjectKeyMap([firstClone, secondClone], DEFAULT_GROUPING_SETTINGS);
expect(keys.get(derivePhysicalProjectKey(firstClone))).toBe(
keys.get(derivePhysicalProjectKey(secondClone)),
);
});
});
describe("selectProjectsAcrossEnvironments", () => {
it("returns all projects from all environments", () => {
const state = makeFixtureState();
+9 -6
View File
@@ -61,10 +61,7 @@ import { useTerminalStateStore } from "~/terminalStateStore";
import { useUiStateStore } from "~/uiStateStore";
import { WsTransport } from "../../rpc/wsTransport";
import { createWsRpcClient, type WsRpcClient } from "../../rpc/wsRpcClient";
import {
deriveLogicalProjectKeyFromSettings,
derivePhysicalProjectKey,
} from "../../logicalProject";
import { buildLogicalProjectKeyMap, derivePhysicalProjectKey } from "../../logicalProject";
import { getClientSettings } from "~/hooks/useSettings";
type EnvironmentServiceState = {
@@ -473,10 +470,13 @@ function coalesceOrchestrationUiEvents(
function syncProjectUiFromStore() {
const projects = selectProjectsAcrossEnvironments(useStore.getState());
const clientSettings = getClientSettings();
const logicalKeyByPhysicalKey = buildLogicalProjectKeyMap(projects, clientSettings);
useUiStateStore.getState().syncProjects(
projects.map((project) => ({
key: derivePhysicalProjectKey(project),
logicalKey: deriveLogicalProjectKeyFromSettings(project, clientSettings),
logicalKey:
logicalKeyByPhysicalKey.get(derivePhysicalProjectKey(project)) ??
derivePhysicalProjectKey(project),
cwd: project.cwd,
})),
);
@@ -548,10 +548,13 @@ function applyRecoveredEventBatch(
if (needsProjectUiSync) {
const projects = selectProjectsAcrossEnvironments(useStore.getState());
const clientSettings = getClientSettings();
const logicalKeyByPhysicalKey = buildLogicalProjectKeyMap(projects, clientSettings);
useUiStateStore.getState().syncProjects(
projects.map((project) => ({
key: derivePhysicalProjectKey(project),
logicalKey: deriveLogicalProjectKeyFromSettings(project, clientSettings),
logicalKey:
logicalKeyByPhysicalKey.get(derivePhysicalProjectKey(project)) ??
derivePhysicalProjectKey(project),
cwd: project.cwd,
})),
);
+13 -3
View File
@@ -10,7 +10,12 @@ import {
} from "../composerDraftStore";
import { newDraftId, newThreadId } from "../lib/utils";
import { orderItemsByPreferredIds } from "../components/Sidebar.logic";
import { deriveLogicalProjectKeyFromSettings, getProjectOrderKey } from "../logicalProject";
import {
buildLogicalProjectKeyMap,
deriveLogicalProjectKeyFromSettings,
derivePhysicalProjectKey,
getProjectOrderKey,
} from "../logicalProject";
import { selectProjectsAcrossEnvironments, useStore } from "../store";
import { createThreadSelectorByRef } from "../storeSelectors";
import { resolveThreadRouteTarget } from "../threadRoutes";
@@ -23,6 +28,10 @@ function useNewThreadState() {
sidebarProjectGroupingMode: settings.sidebarProjectGroupingMode,
sidebarProjectGroupingOverrides: settings.sidebarProjectGroupingOverrides,
}));
const logicalKeyByPhysicalKey = useMemo(
() => buildLogicalProjectKeyMap(projects, projectGroupingSettings),
[projectGroupingSettings, projects],
);
const router = useRouter();
const getCurrentRouteTarget = useCallback(() => {
const currentRouteParams = router.state.matches[router.state.matches.length - 1]?.params ?? {};
@@ -53,7 +62,8 @@ function useNewThreadState() {
candidate.environmentId === projectRef.environmentId,
);
const logicalProjectKey = project
? deriveLogicalProjectKeyFromSettings(project, projectGroupingSettings)
? (logicalKeyByPhysicalKey.get(derivePhysicalProjectKey(project)) ??
deriveLogicalProjectKeyFromSettings(project, projectGroupingSettings))
: scopedProjectKey(projectRef);
const hasBranchOption = options?.branch !== undefined;
const hasWorktreePathOption = options?.worktreePath !== undefined;
@@ -134,7 +144,7 @@ function useNewThreadState() {
});
})();
},
[getCurrentRouteTarget, projectGroupingSettings, router, projects],
[getCurrentRouteTarget, logicalKeyByPhysicalKey, projectGroupingSettings, router, projects],
);
}
+14
View File
@@ -132,6 +132,20 @@ export function deriveLogicalProjectKeyFromSettings(
});
}
export function buildLogicalProjectKeyMap(
projects: ReadonlyArray<Pick<Project, "environmentId" | "id" | "cwd" | "repositoryIdentity">>,
settings: ProjectGroupingSettings,
): Map<string, string> {
const result = new Map<string, string>();
for (const project of projects) {
const physicalKey = derivePhysicalProjectKey(project);
result.set(physicalKey, deriveLogicalProjectKeyFromSettings(project, settings));
}
return result;
}
export function deriveLogicalProjectKeyFromRef(
projectRef: ScopedProjectRef,
project: Pick<Project, "environmentId" | "id" | "cwd" | "repositoryIdentity"> | null | undefined,
+7 -2
View File
@@ -29,7 +29,9 @@ import { resolveAndPersistPreferredEditor } from "../editorPreferences";
import { readLocalApi } from "../localApi";
import { useSettings } from "../hooks/useSettings";
import {
buildLogicalProjectKeyMap,
deriveLogicalProjectKeyFromSettings,
derivePhysicalProjectKey,
derivePhysicalProjectKeyFromPath,
} from "../logicalProject";
import {
@@ -40,7 +42,7 @@ import {
useServerConfigUpdatedSubscription,
useServerWelcomeSubscription,
} from "../rpc/serverState";
import { useStore } from "../store";
import { selectProjectsAcrossEnvironments, useStore } from "../store";
import { useUiStateStore } from "../uiStateStore";
import { syncBrowserChromeTheme } from "../hooks/useTheme";
import {
@@ -242,9 +244,12 @@ function EventRouter() {
useStore.getState().environmentStateById[payload.environment.environmentId];
const bootstrapProject =
bootstrapEnvironmentState?.projectById[payload.bootstrapProjectId] ?? null;
const projects = selectProjectsAcrossEnvironments(useStore.getState());
const logicalKeyByPhysicalKey = buildLogicalProjectKeyMap(projects, projectGroupingSettings);
const bootstrapProjectKey =
(bootstrapProject
? deriveLogicalProjectKeyFromSettings(bootstrapProject, projectGroupingSettings)
? (logicalKeyByPhysicalKey.get(derivePhysicalProjectKey(bootstrapProject)) ??
deriveLogicalProjectKeyFromSettings(bootstrapProject, projectGroupingSettings))
: null) ??
(serverConfig?.cwd
? derivePhysicalProjectKeyFromPath(payload.environment.environmentId, serverConfig.cwd)
+9 -10
View File
@@ -1,6 +1,7 @@
import { scopeProjectRef } from "@t3tools/client-runtime";
import type { EnvironmentId, ScopedProjectRef } from "@t3tools/contracts";
import {
buildLogicalProjectKeyMap,
deriveLogicalProjectKeyFromSettings,
derivePhysicalProjectKey,
deriveProjectGroupLabel,
@@ -29,14 +30,7 @@ export function buildPhysicalToLogicalProjectKeyMap(input: {
projects: ReadonlyArray<Project>;
settings: ProjectGroupingSettings;
}): Map<string, string> {
const mapping = new Map<string, string>();
for (const project of input.projects) {
mapping.set(
derivePhysicalProjectKey(project),
deriveLogicalProjectKeyFromSettings(project, input.settings),
);
}
return mapping;
return buildLogicalProjectKeyMap(input.projects, input.settings);
}
export function buildSidebarProjectSnapshots(input: {
@@ -45,9 +39,12 @@ export function buildSidebarProjectSnapshots(input: {
primaryEnvironmentId: EnvironmentId | null;
resolveEnvironmentLabel: (environmentId: EnvironmentId) => string | null;
}): SidebarProjectSnapshot[] {
const logicalKeyByPhysicalKey = buildLogicalProjectKeyMap(input.projects, input.settings);
const groupedMembers = new Map<string, SidebarProjectGroupMember[]>();
for (const project of input.projects) {
const logicalKey = deriveLogicalProjectKeyFromSettings(project, input.settings);
const logicalKey =
logicalKeyByPhysicalKey.get(derivePhysicalProjectKey(project)) ??
deriveLogicalProjectKeyFromSettings(project, input.settings);
const member: SidebarProjectGroupMember = {
...project,
physicalProjectKey: derivePhysicalProjectKey(project),
@@ -64,7 +61,9 @@ export function buildSidebarProjectSnapshots(input: {
const result: SidebarProjectSnapshot[] = [];
const seen = new Set<string>();
for (const project of input.projects) {
const logicalKey = deriveLogicalProjectKeyFromSettings(project, input.settings);
const logicalKey =
logicalKeyByPhysicalKey.get(derivePhysicalProjectKey(project)) ??
deriveLogicalProjectKeyFromSettings(project, input.settings);
if (seen.has(logicalKey)) {
continue;
}