Compare commits

...
Author SHA1 Message Date
Cursor Agent 11e9d78da4 fix: use deriveLogicalProjectKeyFromSettings as fallback in syncProjects call sites
Both syncProjects call sites in service.ts were falling back to
derivePhysicalProjectKey(project) when the logical key map lookup missed,
while all other call sites across the codebase consistently fall back to
deriveLogicalProjectKeyFromSettings(project, settings). This latent
inconsistency could cause the UI store's logical key to diverge from
the logical key computed during thread creation and sidebar grouping,
potentially breaking project expansion state and thread routing.
2026-04-27 18:34:06 +00: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 305 additions and 30 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,
+30 -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,9 @@ 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. Matching
// projects can group across environments, but each rendered group keeps at
// most one member per environment so thread creation remains unambiguous.
const physicalToLogicalKey = useMemo(() => {
return buildPhysicalToLogicalProjectKeyMap({
projects: orderedProjects,
+120
View File
@@ -11,6 +11,7 @@ import {
type EnvironmentState,
} from "./store";
import {
buildLogicalProjectKeyMap,
deriveLogicalProjectKey,
deriveLogicalProjectKeyFromSettings,
derivePhysicalProjectKey,
@@ -449,6 +450,125 @@ describe("environment grouping", () => {
});
});
describe("buildLogicalProjectKeyMap", () => {
it("groups matching projects across environments without merging distinct projects in one environment", () => {
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))).not.toBe(
keys.get(derivePhysicalProjectKey(localWeb)),
);
});
it("keeps duplicate clones in the same environment separate 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))).not.toBe(
keys.get(derivePhysicalProjectKey(secondClone)),
);
});
});
describe("selectProjectsAcrossEnvironments", () => {
it("returns all projects from all environments", () => {
const state = makeFixtureState();
+9 -2
View File
@@ -62,6 +62,7 @@ import { useUiStateStore } from "~/uiStateStore";
import { WsTransport } from "../../rpc/wsTransport";
import { createWsRpcClient, type WsRpcClient } from "../../rpc/wsRpcClient";
import {
buildLogicalProjectKeyMap,
deriveLogicalProjectKeyFromSettings,
derivePhysicalProjectKey,
} from "../../logicalProject";
@@ -473,10 +474,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)) ??
deriveLogicalProjectKeyFromSettings(project, clientSettings),
cwd: project.cwd,
})),
);
@@ -548,10 +552,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)) ??
deriveLogicalProjectKeyFromSettings(project, clientSettings),
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],
);
}
+97
View File
@@ -51,6 +51,21 @@ function deriveRepositoryRelativeProjectPath(
return normalizedProjectPath.slice(rootPrefix.length).replaceAll("\\", "/");
}
function formatProjectGroupDiscriminator(value: string): string {
return value.length === 0 ? "." : value;
}
function hasDuplicateEnvironment(projects: ReadonlyArray<Pick<Project, "environmentId">>): boolean {
const seen = new Set<string>();
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)}`;
}
@@ -132,6 +147,88 @@ export function deriveLogicalProjectKeyFromSettings(
});
}
export function buildLogicalProjectKeyMap(
projects: ReadonlyArray<Pick<Project, "environmentId" | "id" | "cwd" | "repositoryIdentity">>,
settings: ProjectGroupingSettings,
): Map<string, string> {
const baseBuckets = new Map<
string,
Array<Pick<Project, "environmentId" | "id" | "cwd" | "repositoryIdentity">>
>();
const physicalKeyByProject = new Map<
Pick<Project, "environmentId" | "id" | "cwd" | "repositoryIdentity">,
string
>();
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<string, string>();
const assignBucket = (
baseKey: string,
bucket: ReadonlyArray<Pick<Project, "environmentId" | "id" | "cwd" | "repositoryIdentity">>,
discriminatorIndex: number,
) => {
if (!hasDuplicateEnvironment(bucket)) {
for (const project of bucket) {
result.set(physicalKeyByProject.get(project)!, baseKey);
}
return;
}
const discriminatorFns = [
(project: Pick<Project, "cwd" | "repositoryIdentity">) => {
const relativePath = deriveRepositoryRelativeProjectPath(project);
return relativePath === null ? null : formatProjectGroupDiscriminator(relativePath);
},
(project: Pick<Project, "cwd">) => normalizeProjectPathForComparison(project.cwd),
(project: Pick<Project, "environmentId" | "id" | "cwd" | "repositoryIdentity">) =>
physicalKeyByProject.get(project) ?? derivePhysicalProjectKey(project),
] as const;
const discriminator = discriminatorFns[discriminatorIndex];
if (!discriminator) {
for (const project of bucket) {
result.set(physicalKeyByProject.get(project)!, physicalKeyByProject.get(project)!);
}
return;
}
const buckets = new Map<
string,
Array<Pick<Project, "environmentId" | "id" | "cwd" | "repositoryIdentity">>
>();
for (const project of bucket) {
const value = discriminator(project) ?? normalizeProjectPathForComparison(project.cwd);
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) {
assignBucket(key, projectsForKey, discriminatorIndex + 1);
}
};
for (const [baseKey, bucket] of baseBuckets) {
assignBucket(baseKey, bucket, 0);
}
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;
}