From 6a8ab67ccd9944cb544c987f6803470691af015b Mon Sep 17 00:00:00 2001 From: sidreyas Date: Sun, 15 Nov 2026 16:40:33 +0530 Subject: [PATCH] fix(client): new threads from a grouped repository start in its root checkout A repository group (sub-folder projects plus the repository root) picked its first-added member as the checkout for new threads, so threads landed in the oldest sub-folder even when the root itself was a member. The group's default checkout now prefers the member whose workspace root is the git top-level, keeping the preferred-environment, current-project and last-used rules. This covers the group representative, the new-thread picker fallback, the default project for the New thread button and shortcut, and the mobile picker. Fixes #36296 Co-Authored-By: Claude Opus 5.5 --- .../new-task-project-selection.test.ts | 23 +++++++++ .../threads/new-task-project-selection.ts | 10 +++- apps/web/src/environmentGrouping.test.ts | 14 +++++- apps/web/src/hooks/useHandleNewThread.ts | 18 +++++-- apps/web/src/shortcuts/chatRouteCommands.ts | 1 + apps/web/src/sidebarProjectGrouping.ts | 7 ++- .../src/state/projectGrouping.test.ts | 49 +++++++++++++++++++ .../src/state/projectGrouping.ts | 38 +++++++++++++- 8 files changed, 149 insertions(+), 11 deletions(-) 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 82533a3c91e74..8501093c0f631 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 @@ -77,6 +77,29 @@ describe("getProjectScopeSelectionTarget", () => { expect(target([], "server")).toBe(server); expect(target([])).toBe(mac); }); + + it("starts in the repository root over an earlier sub-folder in the same environment", () => { + const identity = (project: EnvironmentProject) => ({ + ...project.repositoryIdentity!, + rootPath: "/work/workspace", + }); + const base = makeProject("old-tool", "mac", { + workspaceRoot: "/work/workspace/_archive/old-tool", + repositoryKey: "github.com/owner/workspace", + }); + const subfolder = { ...base, repositoryIdentity: identity(base) }; + const root = { + ...makeProject("workspace", "mac", { workspaceRoot: "/work/workspace" }), + repositoryIdentity: identity(base), + }; + expect( + getProjectScopeSelectionTarget( + { ...makeScope([subfolder, root]), representative: root }, + EnvironmentId.make("mac"), + new Set([EnvironmentId.make("mac")]), + ), + ).toBe(root); + }); }); describe("newTaskFilteredProject", () => { 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 2fa21eba1e234..bc4726fa0e5e4 100644 --- a/apps/mobile/src/features/threads/new-task-project-selection.ts +++ b/apps/mobile/src/features/threads/new-task-project-selection.ts @@ -1,5 +1,6 @@ import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection"; import { availableScratchWorkspaceRoot } from "@t3tools/client-runtime/operations/projects"; +import { isRepositoryRootProject } from "@t3tools/client-runtime/state/project-grouping"; import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; import { repositoryGroupingKeyOf, type EnvironmentId, type ServerConfig } from "@t3tools/contracts"; @@ -14,7 +15,8 @@ type DraftProjectSelectionResolution = /** * The copy of a project a new thread starts in: the preferred environment's, then the - * representative. A connected copy wins over a disconnected one. + * representative, with a repository root ahead of a sub-folder. A connected copy wins over a + * disconnected one. */ export function getProjectScopeSelectionTarget( scope: HomeProjectScope, @@ -23,7 +25,11 @@ export function getProjectScopeSelectionTarget( ): EnvironmentProject { const rank = (project: EnvironmentProject) => project.environmentId === preferredEnvironmentId ? 0 : project === scope.representative ? 1 : 2; - const ordered = [...scope.projects].sort((left, right) => rank(left) - rank(right)); + const ordered = [...scope.projects].sort( + (left, right) => + rank(left) - rank(right) || + Number(isRepositoryRootProject(right)) - Number(isRepositoryRootProject(left)), + ); return ( ordered.find((project) => connectedEnvironmentIds.has(project.environmentId)) ?? ordered[0] ?? diff --git a/apps/web/src/environmentGrouping.test.ts b/apps/web/src/environmentGrouping.test.ts index b2081b4b8fe9a..53028e268d78b 100644 --- a/apps/web/src/environmentGrouping.test.ts +++ b/apps/web/src/environmentGrouping.test.ts @@ -232,12 +232,23 @@ describe("environment grouping", () => { workspaceRoot: "/tmp/destination", repositoryIdentity: destinationRepositoryIdentity, }); + const destinationRemoteIdentity = { + ...destinationRepositoryIdentity, + rootPath: "/remote/destination", + }; + const destinationRemoteSubfolder = makeProject({ + id: ProjectId.make("destination-remote-subfolder"), + environmentId: remoteEnvironmentId, + title: "destination-app", + workspaceRoot: "/remote/destination/app", + repositoryIdentity: destinationRemoteIdentity, + }); const destinationRemote = makeProject({ id: ProjectId.make("destination-remote"), environmentId: remoteEnvironmentId, title: "destination", workspaceRoot: "/remote/destination", - repositoryIdentity: destinationRepositoryIdentity, + repositoryIdentity: destinationRemoteIdentity, }); const fallbackPrimary = makeProject({ id: ProjectId.make("fallback-primary"), @@ -249,6 +260,7 @@ describe("environment grouping", () => { currentPrimary, currentRemote, destinationPrimary, + destinationRemoteSubfolder, destinationRemote, fallbackPrimary, ], diff --git a/apps/web/src/hooks/useHandleNewThread.ts b/apps/web/src/hooks/useHandleNewThread.ts index 9fae0319539b4..ffdbb1c33f3a4 100644 --- a/apps/web/src/hooks/useHandleNewThread.ts +++ b/apps/web/src/hooks/useHandleNewThread.ts @@ -18,7 +18,9 @@ import { orderItemsByPreferredIds } from "../components/Sidebar.logic"; import { deriveLogicalProjectKeyFromSettings, getProjectOrderKey, + resolveGroupDefaultProject, selectProjectGroupingSettings, + type ProjectGroupingSettings, } from "@t3tools/client-runtime/state/project-grouping"; import { resolveNewThreadEnvMode, resolveProjectSettings } from "@t3tools/shared/projectSettings"; import { @@ -43,7 +45,7 @@ import { environmentSummaries } from "../state/presentation"; import { environmentServerConfigsAtom } from "../state/server"; import { resolveThreadRouteTarget } from "../threadRoutes"; import { legacyProjectCwdPreferenceKey, useUiStateStore } from "../uiStateStore"; -import { getClientSettings } from "./useSettings"; +import { getClientSettings, useClientSettings } from "./useSettings"; import { getLocalStorageItem, useLocalStorage } from "./useLocalStorage"; import * as Schema from "effect/Schema"; import { useAtomValue } from "@effect/atom-react"; @@ -450,6 +452,7 @@ export function resolveDefaultProjectRef( projectOrder: readonly string[], lastUsedProjectKey: string | null, fallbackReady = true, + groupingSettings?: ProjectGroupingSettings, ): ScopedProjectRef | null { const lastUsed = projects.find( (project) => @@ -458,8 +461,9 @@ export function resolveDefaultProjectRef( ); if (lastUsed) return scopeProjectRef(lastUsed.environmentId, lastUsed.id); if (!fallbackReady) return null; - const [defaultProject] = orderItemsByPreferredIds({ - items: projects.filter((project) => project.archivedAt == null), + const liveProjects = projects.filter((project) => project.archivedAt == null); + const [firstProject] = orderItemsByPreferredIds({ + items: liveProjects, preferredIds: projectOrder, getId: getProjectOrderKey, getPreferenceIds: (project) => [ @@ -467,7 +471,11 @@ export function resolveDefaultProjectRef( legacyProjectCwdPreferenceKey(project.workspaceRoot), ], }); - return defaultProject ? scopeProjectRef(defaultProject.environmentId, defaultProject.id) : null; + if (!firstProject) return null; + const defaultProject = groupingSettings + ? resolveGroupDefaultProject(firstProject, liveProjects, groupingSettings) + : firstProject; + return scopeProjectRef(defaultProject.environmentId, defaultProject.id); } export function useHandleNewThread() { @@ -494,11 +502,13 @@ export function useHandleNewThread() { ); const projects = useProjects(); const handleNewThread = useNewThreadHandler(); + const groupingSettings = useClientSettings(selectProjectGroupingSettings); const defaultProjectRef = resolveDefaultProjectRef( projects, projectOrder, lastUsedProjectKey, defaultProjectFallbackReady, + groupingSettings, ); return { diff --git a/apps/web/src/shortcuts/chatRouteCommands.ts b/apps/web/src/shortcuts/chatRouteCommands.ts index 81c5cc29a2cfb..fdcb5a5d4233e 100644 --- a/apps/web/src/shortcuts/chatRouteCommands.ts +++ b/apps/web/src/shortcuts/chatRouteCommands.ts @@ -88,6 +88,7 @@ export function installChatRouteCommands(router: AnyRouter): () => void { useUiStateStore.getState().projectOrder, lastUsedProjectKey, appAtomRegistry.get(defaultProjectFallbackReadyAtom(lastUsedProjectKey)), + selectProjectGroupingSettings(getClientSettings()), ), handleNewThread, }; diff --git a/apps/web/src/sidebarProjectGrouping.ts b/apps/web/src/sidebarProjectGrouping.ts index 8fb8c4a5a1c6c..af8cdf0affced 100644 --- a/apps/web/src/sidebarProjectGrouping.ts +++ b/apps/web/src/sidebarProjectGrouping.ts @@ -7,6 +7,7 @@ import { buildProjectGroups, derivePhysicalProjectKey, resolveScratchProjectScopeKey, + selectGroupDefaultProject, type ProjectGroupingSettings, } from "@t3tools/client-runtime/state/project-grouping"; import type { Project } from "./types"; @@ -225,8 +226,10 @@ export function buildSidebarProjectPickerEntries(input: { project.environmentId === preferredProjectRef.environmentId && project.id === preferredProjectRef.projectId, ) ?? - group.memberProjects.find( - (project) => project.environmentId === preferredProjectRef.environmentId, + selectGroupDefaultProject( + group.memberProjects.filter( + (project) => project.environmentId === preferredProjectRef.environmentId, + ), )) : null; const targetProject = diff --git a/packages/client-runtime/src/state/projectGrouping.test.ts b/packages/client-runtime/src/state/projectGrouping.test.ts index a991e7a45cb22..8f01ead8b9df1 100644 --- a/packages/client-runtime/src/state/projectGrouping.test.ts +++ b/packages/client-runtime/src/state/projectGrouping.test.ts @@ -8,6 +8,7 @@ import { deriveLogicalProjectKeyFromSettings, pickableProjectGroups, derivePhysicalProjectKey, + resolveGroupDefaultProject, resolveScratchProjectScopeKey, sortProjectsByGroupOrder, type ProjectGroupingSettings, @@ -265,6 +266,54 @@ describe("buildProjectGroups", () => { } }); + it("represents a repository group by its root checkout, even when it was added last", () => { + const remoteEnvironmentId = EnvironmentId.make("remote"); + const local = { ...repositoryIdentity, rootPath: "/work/workspace" }; + const remote = { ...repositoryIdentity, rootPath: "/srv/workspace" }; + const archivedTool = makeProject("old-tool", "/work/workspace/_archive/old-tool", { + repositoryIdentity: local, + }); + const app = makeProject("app-a", "/work/workspace/app-a", { repositoryIdentity: local }); + const remoteApp = makeProject("remote-app", "/srv/workspace/app-a", { + environmentId: remoteEnvironmentId, + repositoryIdentity: remote, + }); + const remoteRoot = makeProject("remote-root", "/srv/workspace", { + environmentId: remoteEnvironmentId, + repositoryIdentity: remote, + }); + const root = makeProject("workspace", "/work/workspace", { repositoryIdentity: local }); + const representativeOf = ( + projects: ReadonlyArray, + preferredEnvironmentId: EnvironmentId | null = null, + ) => + buildProjectGroups({ projects, settings: settings("repository"), preferredEnvironmentId })[0] + ?.representative.id; + + expect(representativeOf([archivedTool, app, root])).toBe("workspace"); + expect(representativeOf([archivedTool, app, remoteApp, remoteRoot, root], environmentId)).toBe( + "workspace", + ); + expect( + representativeOf([archivedTool, app, remoteApp, remoteRoot, root], remoteEnvironmentId), + ).toBe("remote-root"); + // A local root is not worth leaving the current environment for. + expect(representativeOf([archivedTool, root, remoteApp], remoteEnvironmentId)).toBe( + "remote-app", + ); + expect(representativeOf([archivedTool, app])).toBe("old-tool"); + + // A new thread with no current or last-used project starts from the first project's group. + const all = [archivedTool, app, remoteApp, remoteRoot, root]; + expect(resolveGroupDefaultProject(archivedTool, all, settings("repository")).id).toBe( + "workspace", + ); + expect(resolveGroupDefaultProject(remoteApp, all, settings("repository")).id).toBe( + "remote-root", + ); + expect(resolveGroupDefaultProject(archivedTool, all, settings("separate")).id).toBe("old-tool"); + }); + it("keeps a fork apart from its upstream checkout and labels it by its own remote", () => { const fork = makeProject("fork", "/work/t3code-fork", { repositoryIdentity: { diff --git a/packages/client-runtime/src/state/projectGrouping.ts b/packages/client-runtime/src/state/projectGrouping.ts index 607f28dc8ee10..7a234a00a50e5 100644 --- a/packages/client-runtime/src/state/projectGrouping.ts +++ b/packages/client-runtime/src/state/projectGrouping.ts @@ -68,6 +68,20 @@ function deriveRepositoryRelativeProjectPath( return normalizedProjectPath.slice(rootPrefix.length).replaceAll("\\", "/"); } +/** The project's workspace root is its repository's git top-level, not a sub-folder. */ +export function isRepositoryRootProject( + project: Pick, +): boolean { + return deriveRepositoryRelativeProjectPath(project) === ""; +} + +/** A group's default checkout for new threads: its repository root, else its first member. */ +export function selectGroupDefaultProject< + TProject extends Pick, +>(projects: ReadonlyArray): TProject | undefined { + return projects.find(isRepositoryRootProject) ?? projects[0]; +} + export function derivePhysicalProjectKeyFromPath(environmentId: string, cwd: string): string { return `${environmentId}:${normalizeProjectPathForComparison(cwd)}`; } @@ -163,6 +177,23 @@ export function deriveLogicalProjectKeyFromSettings( }); } +/** The checkout a new thread in `project`'s group starts in on its environment: the group's root. */ +export function resolveGroupDefaultProject( + project: TProject, + projects: ReadonlyArray, + settings: ProjectGroupingSettings, +): TProject { + const groupKey = deriveLogicalProjectKeyFromSettings(project, settings); + return ( + projects.find( + (candidate) => + candidate.environmentId === project.environmentId && + isRepositoryRootProject(candidate) && + deriveLogicalProjectKeyFromSettings(candidate, settings) === groupKey, + ) ?? project + ); +} + export function resolveScratchProjectScopeKey( key: string | null, projects: ReadonlyArray< @@ -372,10 +403,13 @@ export function buildProjectGroups(input: { const preferredEnvironmentId = input.preferredEnvironmentId ?? null; return Array.from(groupedMembers, ([key, members]) => { + const projects = members.map((member) => member.project); const representative = (preferredEnvironmentId - ? members.find((member) => member.project.environmentId === preferredEnvironmentId)?.project - : null) ?? members[0]!.project; + ? selectGroupDefaultProject( + projects.filter((project) => project.environmentId === preferredEnvironmentId), + ) + : undefined) ?? selectGroupDefaultProject(projects)!; return { key, label: