Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: disambiguate same-name working directory groups (#20298)

Nekoc268b5755ba1
diff --git a/packages/utils/src/client/topic.test.ts b/packages/utils/src/client/topic.test.ts
index dabe14927e4..391d9fe7f92 100644
--- a/packages/utils/src/client/topic.test.ts
+++ b/packages/utils/src/client/topic.test.ts
@@ -309,6 +309,142 @@ describe('working directory topic helpers', () => {
});
});
+ /** @example Identical basenames expose only the distinguishing suffix, retaining membership. */
+ it('disambiguates duplicate project names without changing group identity or topic order', () => {
+ // ROOT CAUSE:
+ //
+ // groupTopicsByProject used only the final path segment for each title.
+ // Separate IDs therefore rendered as the same "lobehub" in GroupedAccordion.
+ // Expanding colliding names to a unique path suffix fixes the visible labels
+ // while preserving the source-path IDs and topic sorting.
+ const gitPath = '/Users/me/Git/lobehub/lobehub';
+ const workPath = '/Users/me/work/lobehub/lobehub';
+ const topics = [
+ createTopic('git-old', { workingDirectory: gitPath }, 1),
+ createTopic('work', { workingDirectory: workPath }, 3),
+ createTopic('git-new', { workingDirectory: gitPath }, 4),
+ createTopic('unique', { workingDirectory: '/Users/me/private/other' }, 2),
+ createTopic('none', undefined, 5),
+ ];
+
+ const result = groupTopicsByProject(topics, 'updatedAt');
+
+ /** @example The user's two checkouts keep their IDs, sorted children, and private ancestors hidden. */
+ expect(result).toEqual([
+ { children: [topics[2], topics[0]], id: `project:${gitPath}`, title: 'Git/lobehub/lobehub' },
+ { children: [topics[1]], id: `project:${workPath}`, title: 'work/lobehub/lobehub' },
+ { children: [topics[3]], id: 'project:/Users/me/private/other', title: 'other' },
+ { children: [topics[4]], id: 'no-project', title: undefined },
+ ]);
+ /** @example Reversing input does not change the labels or time-sorted groups. */
+ expect(groupTopicsByProject([...topics].reverse(), 'updatedAt')).toEqual(result);
+ });
+
+ /** @example A manager with 1,000 same-name projects retains unique labels and exact topic ownership. */
+ it('disambiguates a large project list without changing membership', () => {
+ // ROOT CAUSE:
+ //
+ // Each label previously split every other project's path, making the growing
+ // manager list quadratic. Shared suffix counts remove that repeated work;
+ // this case protects the resulting labels and membership at the reported scale.
+ const topics = Array.from({ length: 1000 }, (_, index) =>
+ createTopic(String(index), { workingDirectory: `/private/parent-${index}/repo` }, index),
+ );
+ const result = groupTopicsByProject(topics, 'updatedAt');
+
+ /** @example All 1,000 independently owned projects remain visible and distinguishable. */
+ expect(new Set(result.map(({ title }) => title)).size).toBe(1000);
+ /** @example Each label reveals only its distinguishing parent and keeps the original ID and child. */
+ expect(result).toEqual(
+ [...topics].reverse().map((topic) => ({
+ children: [topic],
+ id: `project:/private/parent-${topic.id}/repo`,
+ title: `parent-${topic.id}/repo`,
+ })),
+ );
+ });
+
+ /** @example A lone checkout keeps its basename even after a collision disappears. */
+ it('keeps a unique project name compact', () => {
+ const topics = [createTopic('single', { workingDirectory: '/Users/me/Git/lobehub/lobehub' })];
+
+ /** @example No parent path is exposed without another same-name project. */
+ expect(groupTopicsByProject(topics, 'createdAt')).toEqual([
+ { children: topics, id: 'project:/Users/me/Git/lobehub/lobehub', title: 'lobehub' },
+ ]);
+ });
+
+ /** @example Windows spelling remains part of identity; trailing separators retain existing normalization. */
+ it('retains case-sensitive path identities and normalizes trailing separators', () => {
+ const topics = [
+ createTopic('upper', { workingDirectory: 'C:/Work/repo/' }),
+ createTopic('lower', { workingDirectory: 'c:/work/repo' }),
+ createTopic('same', { workingDirectory: ' C:/Work/repo// ' }),
+ ];
+
+ /** @example Display suffixes distinguish parents while existing matching paths stay together. */
+ expect(groupTopicsByProject(topics, 'createdAt')).toEqual([
+ { children: [topics[0], topics[2]], id: 'project:C:/Work/repo', title: 'Work/repo' },
+ { children: [topics[1]], id: 'project:c:/work/repo', title: 'work/repo' },
+ ]);
+ });
+
+ /** @example Drive letters and UNC server names remain available when nearer ancestors collide. */
+ it('disambiguates Windows drives and remote shares', () => {
+ const paths = [
+ String.raw`C:\work\repo`,
+ 'D:/work/repo',
+ String.raw`\\server-a\share\repo`,
+ String.raw`\\server-b\share\repo`,
+ ];
+ const topics = paths.map((path, index) =>
+ createTopic(String(index), { workingDirectory: path }),
+ );
+
+ /** @example Separators are consistent for display without altering path identity. */
+ expect(
+ groupTopicsByProject(topics, 'createdAt').map(({ id, title }) => ({ id, title })),
+ ).toEqual([
+ { id: `project:${paths[0]}`, title: 'C:/work/repo' },
+ { id: `project:${paths[1]}`, title: 'D:/work/repo' },
+ { id: `project:${paths[2]}`, title: 'server-a/share/repo' },
+ { id: `project:${paths[3]}`, title: 'server-b/share/repo' },
+ ]);
+ });
+
+ /** @example A path that is itself another path's suffix still has a distinct visible label. */
+ it('preserves raw path distinctions when no unique segment suffix exists', () => {
+ const paths = ['/repo', '/home/repo', 'home/repo', String.raw`home\repo`];
+ const topics = paths.map((path, index) =>
+ createTopic(String(index), { workingDirectory: path }),
+ );
+
+ /** @example The absolute root and separator spelling distinguish otherwise identical suffixes. */
+ expect(groupTopicsByProject(topics, 'createdAt').map((group) => group.title)).toEqual(paths);
+ });
+
+ /** @example Worktree locations must not determine the source project's display suffix. */
+ it('uses source repository paths when disambiguating worktree projects', () => {
+ const topics = [
+ createTopic('worktree', {
+ workingDirectory: '/tmp/checkout',
+ workingDirectoryConfig: {
+ git: { activeWorktree: '/tmp/checkout', branch: 'fix', isWorktree: true },
+ path: '/Git/repo',
+ repoType: 'git',
+ },
+ }),
+ createTopic('source', { workingDirectory: '/Git/repo' }),
+ createTopic('other', { workingDirectory: '/work/repo' }),
+ ];
+
+ /** @example The worktree stays with its source; only the two project labels expand. */
+ expect(groupTopicsByProject(topics, 'createdAt')).toEqual([
+ { children: [topics[0], topics[1]], id: 'project:/Git/repo', title: 'Git/repo' },
+ { children: [topics[2]], id: 'project:/work/repo', title: 'work/repo' },
+ ]);
+ });
+
it('groups worktree topics under the source project', () => {
const topics = [
createTopic(
diff --git a/packages/utils/src/client/topic.ts b/packages/utils/src/client/topic.ts
index 22c69230fa3..84464ef8d8e 100644
--- a/packages/utils/src/client/topic.ts
+++ b/packages/utils/src/client/topic.ts
@@ -118,10 +118,30 @@ export const groupTopicsByUpdatedTime = (topics: ChatTopic[]) =>
const NO_PROJECT_GROUP_ID = 'no-project';
const PROJECT_GROUP_PREFIX = 'project:';
-// Extract the final path segment as display name; supports POSIX and Windows separators
-const getProjectName = (dir: string): string => {
- const segments = dir.split(/[/\\]+/).filter(Boolean);
- return segments.at(-1) || dir;
+/**
+ * Normalizes project display names to their shortest distinguishing path suffix.
+ *
+ * Before:
+ * - "/Users/me/Git/lobehub/lobehub", "/Users/me/work/lobehub/lobehub"
+ *
+ * After:
+ * - "Git/lobehub/lobehub", "work/lobehub/lobehub"
+ */
+// Extract the final path segment as display name; supports POSIX and Windows separators.
+// Expand only colliding names so unrelated private ancestors stay out of the sidebar.
+const getProjectName = (
+ dir: string,
+ segments: string[],
+ suffixCounts: Map<string, number>,
+): string => {
+ // The first unique suffix reveals only as much of the path as needed.
+ for (let depth = 1; depth <= segments.length; depth++) {
+ const name = segments.slice(-depth).join('/');
+ if (suffixCounts.get(name) === 1) return name;
+ }
+
+ // Preserve absolute/relative and separator distinctions when all segments match.
+ return dir;
};
const normalizeWorkingDirectory = (dir: string): string => dir.trim().replace(/[/\\]+$/, '');
@@ -158,13 +178,27 @@ export const getTopicWorkingDirectorySourcePath = (topic: ChatTopic): string | u
export const getTopicWorkingDirectoryEffectivePath = (topic: ChatTopic): string | undefined =>
getTopicMetadataWorkingDirectoryEffectivePath(topic.metadata);
+/**
+ * Groups topics by source directory with distinguishable project titles.
+ *
+ * Use when:
+ * - Rendering project groups in a topic sidebar or management view.
+ *
+ * Expects:
+ * - Topics with optional source or worktree directory metadata.
+ * - A timestamp field for descending activity order.
+ *
+ * Returns:
+ * - Stable path-based group IDs and sorted topics, with no-project topics last.
+ * - Basenames for unique projects and distinguishing path suffixes for collisions.
+ */
export const groupTopicsByProject = (
topics: ChatTopic[],
field: 'createdAt' | 'updatedAt',
): GroupedTopic[] => {
if (!topics.length) return [];
- const groupsMap = new Map<string, { children: ChatTopic[]; path: string }>();
+ const groupsMap = new Map<string, { children: ChatTopic[]; path: string; segments: string[] }>();
for (const topic of topics) {
const normalized = getTopicWorkingDirectorySourcePath(topic) ?? '';
@@ -173,7 +207,12 @@ export const groupTopicsByProject = (
if (existing) {
existing.children.push(topic);
} else {
- groupsMap.set(id, { children: [topic], path: normalized });
+ // Parse each source path once; display labels never change its grouping identity.
+ groupsMap.set(id, {
+ children: [topic],
+ path: normalized,
+ segments: normalized.split(/[/\\]+/).filter(Boolean),
+ });
}
}
@@ -182,11 +221,19 @@ export const groupTopicsByProject = (
group.children.sort((a, b) => getTopicSortTime(b, field) - getTopicSortTime(a, field));
}
+ // Count suffixes once so an expanding project list does not compare every pair of paths.
+ const suffixCounts = new Map<string, number>();
+ for (const { segments } of groupsMap.values()) {
+ for (let depth = 1; depth <= segments.length; depth++) {
+ const suffix = segments.slice(-depth).join('/');
+ suffixCounts.set(suffix, (suffixCounts.get(suffix) ?? 0) + 1);
+ }
+ }
const groups: GroupedTopic[] = Array.from(groupsMap.entries()).map(
- ([id, { children, path }]) => ({
+ ([id, { children, path, segments }]) => ({
children,
id,
- title: id === NO_PROJECT_GROUP_ID ? undefined : getProjectName(path),
+ title: id === NO_PROJECT_GROUP_ID ? undefined : getProjectName(path, segments, suffixCounts),
}),
);