feat(tree): one row per project — folders no longer own session lists

Extra folders were rendered as their own rows with their own sessions, so a
project that carried a second directory split into two session lists. On a
cloud workspace the base row also exposed the local mirror id
(`wmvl31c7ilkuc`) as a folder name, which read as a project nested inside
itself.

Folders are now extra DIRECTORIES of one project: `applyFolders` returns the
workspace's sessions untouched, so the owner row and every member workspace
share a single list. Folders stay manageable from the workspace menu
(⋯ → 管理文件夹) where they can still be added, rooted, opened or removed.

Drops the now-unused folder-label/label-matching helpers, updates the
regression test to assert one merged row, and rewrites the CHANGELOG entry.
typecheck 0 errors, 290 tests pass, all three verifiers pass.
This commit is contained in:
qyh15 2026-11-07 17:53:22 +08:00
parent 571638b836
commit bc6a427dfe
6 changed files with 69 additions and 275 deletions

View File

@ -35,11 +35,13 @@
`joinSubpath` 对绝对/盘符/UNC 输入的判定统一;`joinSubpath` 额外拒绝 `C:foo`。
### 侧栏文件夹行
- 工作区自己的目录不再渲染成一条文件夹行。以前只要工作区挂了额外文件夹,它就会多出
一条以目录末段命名的行;云端工作区的路径是镜像目录,于是侧栏出现 `wmvl31c7ilkuc`
这样的内部 id,并且工作区自身的会话被藏进这条行里,看起来像「一个项目套在它自己里面」。
- 现在只有真正额外挂进来的文件夹才有行,工作区自己的会话直接列在标题下方;没有额外
文件夹时节点形状与引入文件夹功能之前完全一致。
- 一个项目只占一行。此前工作区挂上额外文件夹后,每个文件夹都会变成一条带自己会话列表的行;
云端工作区的路径是本地镜像目录,于是侧栏出现 `wmvl31c7ilkuc` 这种内部 id,项目自身的
会话又被藏进那一行,看起来像「一个项目套在它自己里面」。
- 现在不再渲染文件夹行:工作区自己的目录和所有额外文件夹共用同一个会话列表,全部列在
工作区标题下面。文件夹仍然可以在工作区的「⋯ → 管理文件夹」里增删、设根、打开,
只是不再各自分裂出一套会话。
- 工作区没有额外文件夹时,节点形状与引入文件夹功能之前完全一致。
### 测试与工程
- 新增 `tests/folders-cas.test.ts`、`tests/folders-persist.test.ts`、`tests/remote-paths.test.ts`

View File

@ -1216,35 +1216,6 @@ window.__ModuleLoader__.load({
return effectiveFolders(workspacePath, entry).find((folder) => samePath(folder, root)) ?? workspacePath;
}
/**
* Owning folder of a session cwd: the most specific (longest) effective folder
* that equals the cwd or is a proper path ancestor of it.
*
* Both sides are {@link canonicalPath}-normalized, so `..` segments and mixed
* separator spellings no longer mis-group a session into the project base.
* @param cwd - recorded session cwd; undefined means "the workspace itself".
* @param workspacePath - canonical directory of the workspace.
* @param entry - saved folder entry, if any.
* @returns the owning folder, or the workspace path when nothing matches.
*/
function folderOfCwd(cwd, workspacePath, entry) {
if (cwd === void 0) return workspacePath;
const target = canonicalPath(cwd);
let best;
let bestLength = -1;
for (const folder of effectiveFolders(workspacePath, entry)) {
const root = canonicalPath(folder);
if (root === "") continue;
const separator = isWindowsStyle(root) ? BACKSLASH : SLASH;
const prefix = root.endsWith(separator) ? root : `${root}${separator}`;
if (target !== root && !target.startsWith(prefix)) continue;
if (root.length > bestLength) {
best = folder;
bestLength = root.length;
}
}
return best ?? workspacePath;
}
/**
* Append an extra folder. Idempotent under `samePath`, and the stored root (if
* any) survives untouched.
* @param entry - current entry, or undefined for a workspace with no extras.
@ -1497,13 +1468,14 @@ window.__ModuleLoader__.load({
return members.some((member) => member.sessionIds.includes(sessionId));
}
/**
* Display label of one folder: the last path segment. A drive root (`C:\`) or
* the POSIX root has no segment, so the whole path is the honest label.
* Display label of one folder: the last path segment. A drive root or the
* POSIX root has no segment, so the whole path is the honest label.
* @param path - absolute folder path.
*/ function folderLabel(path) {
const trimmed = path.replace(/[/\\]+$/u, "");
*/
function folderLabel(path) {
const trimmed = path.replace(/[/\u005C]+$/u, "");
if (trimmed === "" || /^[A-Za-z]:$/u.test(trimmed)) return path;
const base = trimmed.split(/[/\\]/u).pop();
const base = trimmed.split(/[/\u005C]/u).pop();
return base !== void 0 && base !== "" ? base : path;
}
/** Stable identity of one folder row (same path may live under two workspaces). */
@ -1511,72 +1483,15 @@ window.__ModuleLoader__.load({
return `${workspaceId}\u0000${path}`;
}
/**
* Display labels for member folders.
*
* A cloud member's directory is a locally generated mirror id (`wabc123`), which
* says nothing to the user: its REMOTE path or title is the honest label. Local
* members keep the last path segment, like every other folder.
* @param folders - per-workspace folders + members.
* A workspace's extra folders are extra DIRECTORIES of the same project, not
* extra session lists: every visible session of the workspace and of its member
* workspaces renders on the one workspace row. Folder rows were removed on
* request — a project reads as one row, and a cloud workspace's mirror id
* (`wmvl31c7ilkuc`) never surfaces as a folder name.
* @param sessions - every visible session of the workspace + its members.
*/
function memberFolderLabels(entry) {
const labels = /* @__PURE__ */ new Map();
for (const member of entry?.members ?? []) {
const label = member.kind === "remote" ? member.title ?? folderLabel(member.remotePath ?? member.path) : folderLabel(member.path);
if (label !== "") labels.set(member.path, label);
}
return labels;
}
/**
* Split a workspace's visible sessions between its base directory and its extra
* folders. Sessions keep the order they already had inside their owner, and a
* workspace with no extra folder returns its sessions untouched — the pre-
* folders node shape (no `folders` key at all).
* @param workspaceId - owning workspace.
* @param workspacePath - canonical directory (folder #0).
* @param sessions - every visible session of the workspace, in host order.
* @param list - sessions list snapshot (records each session's cwd).
* @param entry - saved extra folders + root for this workspace.
* @param collapsedFolders - collapsed folder-row keys. Undefined means "nothing
* is collapsed", which is both the default view and what search uses so every
* hit is visible without another click.
*/
function applyFolders(workspaceId, workspacePath, sessions, list, entry, collapsedFolders) {
const labels = memberFolderLabels(entry);
const extras = effectiveFolders(workspacePath, entry).slice(1);
if (extras.length === 0) return { sessions };
const root = rootOf(workspacePath, entry);
const base = [];
const byFolder = /* @__PURE__ */ new Map();
for (const node of sessions) {
const owner = folderOfCwd(list.byId[node.id]?.cwd, workspacePath, entry);
if (samePath(owner, workspacePath)) {
base.push(node);
continue;
}
const bucket = byFolder.get(owner);
if (bucket === void 0) byFolder.set(owner, [node]);
else bucket.push(node);
}
const folderNode = (path) => {
return {
path,
label: labels.get(path) ?? folderLabel(path),
isRoot: samePath(path, root),
expanded: collapsedFolders === void 0 || !collapsedFolders.has(folderKey(workspaceId, path)),
isBase: false,
sessions: byFolder.get(path) ?? []
};
};
const folders = extras.map((path) => folderNode(path));
const rootIndex = folders.findIndex((folder) => folder.isRoot);
if (rootIndex > 0) {
const [rootFolder] = folders.splice(rootIndex, 1);
if (rootFolder !== void 0) folders.unshift(rootFolder);
}
return {
sessions: base,
folders
};
function applyFolders(_workspaceId, _workspacePath, sessions, _list, _entry, _collapsedFolders) {
return { sessions };
}
/**
* Derive the three-level tree.
@ -8067,20 +7982,11 @@ window.__ModuleLoader__.load({
onArchive: onSessionArchive
}, session.id))
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(RowGroup, {
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(RowGroup, {
expanded: workspace.expanded,
className: "wgTreeChildren",
animate: animate && folders.length + workspace.sessions.length <= ANIMATION_ROW_CAP,
children: [workspace.sessions.map((session) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SessionRow, {
node: session,
currentId: current,
now,
t,
onOpen,
onRename: onSessionRename,
onFork,
onArchive: onSessionArchive
}, session.id)), folders.map((folder) => {
children: folders.map((folder) => {
const key = folderKey(workspace.workspaceId, folder.path);
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
role: "group",
@ -8120,7 +8026,7 @@ window.__ModuleLoader__.load({
}, session.id))
})]
}, key);
})]
})
});
}
/** One category section: header row + expanded workspace folders. */
@ -8315,18 +8221,9 @@ window.__ModuleLoader__.load({
onArchive: () => {}
}, session.id))
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
role: "group",
children: [workspace.sessions.map((session) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(SessionRow, {
node: session,
currentId: current,
now,
t,
onOpen: open,
onRename: () => {},
onFork: () => {},
onArchive: () => {}
}, session.id)), folders.map((folder) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
children: folders.map((folder) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
role: "group",
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(FolderRow, {
node: folder,
@ -8349,7 +8246,7 @@ window.__ModuleLoader__.load({
onArchive: () => {}
}, session.id))
})]
}, folderKey(workspace.workspaceId, folder.path)))]
}, folderKey(workspace.workspaceId, folder.path)))
});
}
/**

View File

@ -133,10 +133,11 @@ export interface MemberWorkspaces {
*/
export declare function memberWorkspaces(workspaces: readonly WorkspaceView[], folders?: FoldersDocument): MemberWorkspaces;
/**
* Display label of one folder: the last path segment. A drive root (`C:\`) or
* the POSIX root has no segment, so the whole path is the honest label.
* Display label of one folder: the last path segment. A drive root or the
* POSIX root has no segment, so the whole path is the honest label.
* @param path - absolute folder path.
*/ export declare function folderLabel(path: string): string;
*/
export declare function folderLabel(path: string): string;
/** Stable identity of one folder row (same path may live under two workspaces). */
export declare function folderKey(workspaceId: WorkspaceId, path: string): string;
/**

View File

@ -2366,19 +2366,6 @@ function WorkspaceSubtree({ workspace, current, now, t, animate, onOpen, onSessi
className="wgTreeChildren"
animate={animate && folders.length + workspace.sessions.length <= ANIMATION_ROW_CAP}
>
{workspace.sessions.map((session) => (
<SessionRow
key={session.id}
node={session}
currentId={current}
now={now}
t={t}
onOpen={onOpen}
onRename={onSessionRename}
onFork={onFork}
onArchive={onSessionArchive}
/>
))}
{folders.map((folder) => {
const key = folderKey(workspace.workspaceId, folder.path)
return (
@ -2686,19 +2673,6 @@ function SearchSubtree({ workspace, current, now, open, t }: {
}
return (
<div role="group">
{workspace.sessions.map((session) => (
<SessionRow
key={session.id}
node={session}
currentId={current}
now={now}
t={t}
onOpen={open}
onRename={() => {}}
onFork={() => {}}
onArchive={() => {}}
/>
))}
{folders.map((folder) => (
<div key={folderKey(workspace.workspaceId, folder.path)} role="group">
<FolderRow

View File

@ -21,11 +21,7 @@ import { categoryAppearance, effectiveCategories, orderedWorkspaceIds, resolveCa
import {
absorbedWorkspaceIds,
EMPTY_FOLDERS_DOCUMENT,
effectiveFolders,
entryFor,
folderOfCwd,
rootOf,
samePath,
type FoldersDocument,
type WorkspaceFolderEntry,
} from '../core/folders.ts'
@ -314,14 +310,16 @@ function projectHoldsSession(
return members.some(member => member.sessionIds.includes(sessionId))
}
/**
* Display label of one folder: the last path segment. A drive root (`C:\`) or
* the POSIX root has no segment, so the whole path is the honest label.
* Display label of one folder: the last path segment. A drive root or the
* POSIX root has no segment, so the whole path is the honest label.
* @param path - absolute folder path.
*/export function folderLabel(path: string): string {
const trimmed = path.replace(/[/\\]+$/u, '')
*/
export function folderLabel(path: string): string {
const trimmed = path.replace(/[/\u005C]+$/u, '')
if (trimmed === '' || /^[A-Za-z]:$/u.test(trimmed)) return path
const base = trimmed.split(/[/\\]/u).pop()
const base = trimmed.split(/[/\u005C]/u).pop()
return base !== undefined && base !== '' ? base : path
}
@ -331,86 +329,22 @@ export function folderKey(workspaceId: WorkspaceId, path: string): string {
}
/**
* Display labels for member folders.
*
* A cloud member's directory is a locally generated mirror id (`wabc123`), which
* says nothing to the user: its REMOTE path or title is the honest label. Local
* members keep the last path segment, like every other folder.
* @param folders - per-workspace folders + members.
*/
function memberFolderLabels(entry: WorkspaceFolderEntry | undefined): Map<string, string> {
const labels = new Map<string, string>()
for (const member of entry?.members ?? []) {
const label = member.kind === 'remote'
? (member.title ?? folderLabel(member.remotePath ?? member.path))
: folderLabel(member.path)
if (label !== '') labels.set(member.path, label)
}
return labels
}
/**
* Split a workspace's visible sessions between its base directory and its extra
* folders. Sessions keep the order they already had inside their owner, and a
* workspace with no extra folder returns its sessions untouched — the pre-
* folders node shape (no `folders` key at all).
* @param workspaceId - owning workspace.
* @param workspacePath - canonical directory (folder #0).
* @param sessions - every visible session of the workspace, in host order.
* @param list - sessions list snapshot (records each session's cwd).
* @param entry - saved extra folders + root for this workspace.
* @param collapsedFolders - collapsed folder-row keys. Undefined means "nothing
* is collapsed", which is both the default view and what search uses so every
* hit is visible without another click.
* A workspace's extra folders are extra DIRECTORIES of the same project, not
* extra session lists: every visible session of the workspace and of its member
* workspaces renders on the one workspace row. Folder rows were removed on
* request — a project reads as one row, and a cloud workspace's mirror id
* (`wmvl31c7ilkuc`) never surfaces as a folder name.
* @param sessions - every visible session of the workspace + its members.
*/
function applyFolders(
workspaceId: WorkspaceId,
workspacePath: string,
_workspaceId: WorkspaceId,
_workspacePath: string,
sessions: readonly SessionNode[],
list: SessionListState,
entry: WorkspaceFolderEntry | undefined,
collapsedFolders: ReadonlySet<string> | undefined,
_list: SessionListState,
_entry: WorkspaceFolderEntry | undefined,
_collapsedFolders: ReadonlySet<string> | undefined,
): { sessions: readonly SessionNode[]; folders?: readonly FolderNode[] } {
const labels = memberFolderLabels(entry)
const extras = effectiveFolders(workspacePath, entry).slice(1)
if (extras.length === 0) return { sessions }
const root = rootOf(workspacePath, entry)
const base: SessionNode[] = []
const byFolder = new Map<string, SessionNode[]>()
for (const node of sessions) {
const owner = folderOfCwd(list.byId[node.id]?.cwd, workspacePath, entry)
if (samePath(owner, workspacePath)) {
base.push(node)
continue
}
const bucket = byFolder.get(owner)
if (bucket === undefined) byFolder.set(owner, [node])
else bucket.push(node)
}
const folderNode = (path: string): FolderNode => {
return {
path,
label: labels.get(path) ?? folderLabel(path),
isRoot: samePath(path, root),
expanded: collapsedFolders === undefined || !collapsedFolders.has(folderKey(workspaceId, path)),
// The base directory is the workspace row itself, so every row here is an
// extra folder: a mirror id must never be dressed up as one, and none of
// them can be removed from the workspace.
isBase: false,
// Every owned session is kept on the node so the row's count stays honest
// while the folder is collapsed; the RowGroup does the hiding.
sessions: byFolder.get(path) ?? [],
}
}
const folders: FolderNode[] = extras.map(path => folderNode(path))
// Root first, then stored order: moving the root row to the front keeps the
// remaining rows in their stored order.
const rootIndex = folders.findIndex(folder => folder.isRoot)
if (rootIndex > 0) {
const [rootFolder] = folders.splice(rootIndex, 1)
if (rootFolder !== undefined) folders.unshift(rootFolder)
}
return { sessions: base, folders }
return { sessions }
}
/**

View File

@ -218,12 +218,11 @@ describe('deriveTopLevel', () => {
})
/**
* Regression: a workspace whose base directory is a remote mirror used to grow
* a folder row labelled with the mirror's leaf segment (`wmvl31c7ilkuc`) and
* then hide all of the workspace's own sessions behind it, so one cloud
* project looked like a project nested inside itself.
* A project renders as ONE row: its own directory and every extra folder share
* that workspace's session list, so a cloud project never reads as a project
* nested inside itself and its mirror id never surfaces as a folder name.
*/
describe('folder rows never include the base directory', () => {
describe('a project renders as one row', () => {
const MIRROR = 'C:/Users/zcol/.dsh/remote-workspaces/wmvl31c7ilkuc'
const LOCAL = 'C:/Users/zcol/云端记忆系统'
@ -231,16 +230,6 @@ describe('folder rows never include the base directory', () => {
return { ...session(id, `会话-${id}`), cwd } as unknown as SessionSummary
}
function state(byId: Record<string, SessionSummary>): SessionListState {
return {
ids: Object.keys(byId),
byId,
current: undefined,
phase: 'ready',
subagentsByParent: {},
} as unknown as SessionListState
}
const workspaces = [
workspace('ws-o', MIRROR, '插件项目', ['s-cloud']),
workspace('ws-m', LOCAL, '云端记忆系统', ['s-local']),
@ -248,34 +237,31 @@ describe('folder rows never include the base directory', () => {
const folders: FoldersDocument = {
version: 1,
workspaces: {
'ws-o': {
folders: [],
members: [{ path: LOCAL, workspaceId: 'ws-m', kind: 'local' }],
},
'ws-o': { folders: [], members: [{ path: LOCAL, workspaceId: 'ws-m', kind: 'local' }] },
},
}
const list = state({
's-cloud': sessionIn('s-cloud', `${MIRROR}/src`),
's-local': sessionIn('s-local', `${LOCAL}/docs`),
})
const list = {
ids: ['s-cloud', 's-local'],
byId: {
's-cloud': sessionIn('s-cloud', `${MIRROR}/src`),
's-local': sessionIn('s-local', `${LOCAL}/docs`),
},
current: undefined,
phase: 'ready',
subagentsByParent: {},
} as unknown as SessionListState
const noManual: ManualGroups = { categories: [], assignments: {} }
// Sessions only materialize on an expanded row, so the owner starts open.
const view = { expandedCategories: [], expandedWorkspaces: ['ws-o'] }
it('renders only the extra folder and keeps base sessions on the workspace row', () => {
it('merges owner and member sessions and emits no folder rows', () => {
const groups = deriveGroups(list, workspaces, [], CONFIG, view, noManual, new Map(), folders)
const node = groups.flatMap(group => group.workspaces).find(w => w.workspaceId === 'ws-o')
expect(node).toBeDefined()
expect(node!.folders?.map(f => f.path)).toEqual([LOCAL])
expect(node!.folders?.every(f => f.isBase === false)).toBe(true)
// The mirror id must never leak into a folder label.
expect(node!.folders?.map(f => f.label) ?? []).not.toContain('wmvl31c7ilkuc')
// Base-owned sessions stay on the workspace itself.
expect(node!.sessions.map(s => s.id)).toEqual(['s-cloud'])
expect(node!.folders?.[0]?.sessions.map(s => s.id)).toEqual(['s-local'])
expect(node!.folders).toBeUndefined()
expect(node!.sessions.map(s => s.id)).toEqual(['s-cloud', 's-local'])
})
it('keeps the pre-folders node shape when the workspace has no extras', () => {
it('keeps the pre-folders node shape without extras', () => {
const groups = deriveGroups(list, workspaces, [], CONFIG, view, noManual, new Map(), {
version: 1,
workspaces: {},