fix(tree): 改名只在标题本来就来自文件名时才改写(#915 后续)

调解此前**无条件**把页签标题写成新的基名,于是**作者自设的标题**会被一次改名
悄悄替换掉:宿主的 agent-opens 推送就带着模型自己给的名字
(`use-host-feeds` → `openFile(scope, path, title)`),消费插件也可以自己标名字。

- `retargetPathTabs` 只在 `tab.title === baseName(tab.path)`(也就是打开时由
  文件名派生的那种)时才带上 title 补丁;目录改名本来就不动后代(后代基名
  没变),两处口径因此一致。
- `PathTab` / `NativePathTab` 补 `title` 字段,原生枚举(`pathTabs()`)随之
  交出每个记录当前的芯片标题。

测试:`tests/editor-unsaved-guard.spec.tsx` +1(同一个文件的两个页签:派生标题
跟着走、自设标题原样保留;底部工作台那一路一并断言),`tests/native-surface.spec.ts`
的路径枚举断言带上 `title`。判别性实测:把判定换回无条件改写 → 该用例红。
This commit is contained in:
ymac committed 2026-10-12 08:49:28 +08:00
1 parent 2d850a5261
commit 953bebcda3
5 files changed
+63 -10

No files matched your search

+4
View File
@@ -358,6 +358,10 @@ export function createNativeTabRecords(): NativeTabRecords {
cwd: entry.scope.cwd,
id: entry.tab.id,
path,
// The chip's title travels with the row: the rename
// reconciliation retitles a page only when that title was
// derived from the file name (see tree-mutations).
title: entry.tab.title,
})
}
}
+5
View File
@@ -561,6 +561,11 @@ export interface NativePathTab {
/** The path the tab is bound to — a file path, or a browser tab's address
* (a url seed is stored on the same field). */
path: string
/** The chip's current title. The rename reconciliation reads it to tell a
* title the tree DERIVED from the file name (the basename it wrote on open)
* from one its author chose — a model-driven open names its own title, and
* that is not the tree's to rewrite. */
title: string
}
/**
+23 -4
View File
@@ -46,6 +46,9 @@ interface PathTab {
* session (the guard then reads conservatively rather than missing the
* record and closing a live draft without asking). */
scopeSessionId: string | undefined
/** The tab's current title — the rename reconciliation retitles a page only
* when this is the file name the tree itself wrote there. */
title: string
/** The session the tab is WRITTEN through: the native record's seat, or the
* bottom-workbench row's own session — every row names its session now, so a
* background conversation's pages are patched in place instead of resolving
@@ -140,6 +143,7 @@ function pathTabsOf(store: SidebarStore, native: readonly NativePathTab[]): Path
key: pathTabKey(sessionId, tab.id),
id: tab.id,
path: tab.path,
title: tab.title,
absolute: tab.path,
scopeSessionId: sessionId,
sessionId,
@@ -152,6 +156,7 @@ function pathTabsOf(store: SidebarStore, native: readonly NativePathTab[]): Path
key: pathTabKey(tab.scopeSessionId, tab.id),
id: tab.id,
path: tab.path,
title: tab.title,
// A chat link opens its tab with the ADDRESS's path, and `fileAddressFor`
// spells that workspace-relative whenever it knows the cwd. Comparing it
// raw against the tree's absolute paths skipped exactly those tabs, so the
@@ -182,13 +187,21 @@ function movedPath(path: string, oldPath: string, newPath: string): string | und
return tail === '.' ? newPath : `${newPath}/${tail}`
}
/** Retarget tabs after `oldPath` became `newPath` (title follows the new
* base name, matching how openFile titles editor tabs).
/** Retarget tabs after `oldPath` became `newPath`.
*
* Files and anything under a renamed DIRECTORY are retargeted — the same
* subtree rule the delete path uses — in EVERY session the store has cached.
* The move is announced through {@link markRetargetedPath} so the editor keeps
* its document (and an unsaved draft) instead of reloading the new name. */
* its document (and an unsaved draft) instead of reloading the new name.
*
* The TITLE follows only when it was DERIVED from the file name — an editor
* page opened the way the plugin opens one is titled with the basename, so a
* rename has to carry it along; a title its author chose is not the tree's to
* rewrite. The host's agent-opens push is the live case: the model asks for a
* file with a title of its own (`use-host-feeds` → `openFile(scope, path,
* title)`), and a consumer plugin may label a page itself. A directory rename
* never retitles a descendant anyway (their basenames do not change), so this
* is the same rule seen from the file side. */
export function retargetPathTabs(ctx: Context, store: SidebarStore, oldPath: string, newPath: string): void {
const service = ctx.get('betterSidebar')
if (service === undefined) return
@@ -198,7 +211,13 @@ export function retargetPathTabs(ctx: Context, store: SidebarStore, oldPath: str
// The announcement has to match what the editor will see: it compares the
// path it holds (the tab's own spelling) with the one written here.
markRetargetedPath(tab.key, tab.path, moved)
service.updateTab(tab.id, { path: moved, title: baseName(moved) }, tab.sessionId)
service.updateTab(tab.id, {
path: moved,
// Only a DERIVED title follows the new name: `baseName(tab.path)` is the
// spelling the opener would have written, so a title that differs is one
// somebody chose.
...(tab.title === baseName(tab.path) ? { title: baseName(moved) } : {}),
}, tab.sessionId)
}
}
+26 -3
View File
@@ -298,19 +298,20 @@ describe('tree rename/delete reconciliation', () => {
return { store, service, records, close, context }
}
/** One editor record bound to a file, as a native open would mint it. */
/** One editor record bound to a file, as a native open would mint it. The
* title defaults to the file name, i.e. what an open derives on its own. */
const openNativeFile = (
records: ReturnType<typeof createNativeTabRecords>,
id: string,
path: string,
seat: { sessionId?: string; cwd?: string } = {},
seat: { sessionId?: string; cwd?: string; title?: string } = {},
): void => {
const sessionId = seat.sessionId ?? 'native-scope'
records.ensure({
sessionId,
id,
kind: 'editor',
title: 'a.ts',
title: seat.title ?? path.slice(path.lastIndexOf('/') + 1),
params: { path },
scope: seat.cwd === undefined ? { sessionId } : { sessionId, cwd: seat.cwd },
})
@@ -347,6 +348,28 @@ describe('tree rename/delete reconciliation', () => {
expect(consumeRetargetedPath(key, '/ws/dir/a.ts', '/ws/elsewhere.ts')).toBe(false)
})
it('follows the new name in the title only when the title CAME from the name', () => {
// The host's agent-opens push carries a title of its own (the model asked
// for that file by a label it chose: `use-host-feeds` → `openFile(scope,
// path, title)`), and a consumer plugin may label a page itself. A rename
// carries along the title the opener DERIVED from the file name — the
// basename — and leaves a chosen one as it is. Both tab spaces obey it.
const { store, service, records, context } = mountNativeSession()
openNativeFile(records, 'tab1', '/ws/a.ts')
openNativeFile(records, 'tab2', '/ws/a.ts', { title: 'Plan draft' })
service.openTab({ type: 'editor', title: 'Plan draft', path: '/ws/a.ts', target: 'bottom' })
retargetPathTabs(context, store, '/ws/a.ts', '/ws/b.ts')
// Derived: the chip follows the file…
expect(records.get('native-scope', 'tab1')?.tab).toMatchObject({ path: '/ws/b.ts', title: 'b.ts' })
// …authored: the path moves, the label stays.
expect(records.get('native-scope', 'tab2')?.tab).toMatchObject({ path: '/ws/b.ts', title: 'Plan draft' })
const bottom = allLeaves(store.getSnapshot().state!.bottomSplits)
.flatMap(leaf => leaf.tabs).find(tab => tab.path !== undefined)
expect(bottom).toMatchObject({ path: '/ws/b.ts', title: 'Plan draft' })
})
it('retargets a tab opened from a CHAT LINK (its address stores a workspace-relative path)', () => {
// `fileAddressFor` spells a path inside the workspace relatively, so a tab
// opened from chat holds `src/a.ts` while the tree renames by absolute path.
+5 -3
View File
@@ -1312,10 +1312,12 @@ describe('native records: path enumeration and the navigation gate', () => {
records.ensure({ sessionId: 's3', id: 'tab9', kind: 'browser', title: 'site', params: { url: 'https://example.test/x' }, scope: { sessionId: 's3' } })
expect(records.pathTabs().sort((left, right) => left.id.localeCompare(right.id))).toEqual([
{ sessionId: 's1', scopeSessionId: 's1', cwd: undefined, id: 'tab1', path: '/w/a.ts' },
{ sessionId: 's2', scopeSessionId: 's2', cwd: '/w', id: 'tab1', path: 'b.ts' },
// The chip's title rides along: the rename reconciliation retitles a
// page only when that title is the file name it wrote itself.
{ sessionId: 's1', scopeSessionId: 's1', cwd: undefined, id: 'tab1', path: '/w/a.ts', title: 'a.ts' },
{ sessionId: 's2', scopeSessionId: 's2', cwd: '/w', id: 'tab1', path: 'b.ts', title: 'b.ts' },
// A browser seed lands on `path` too (the address bar reads it there).
{ sessionId: 's3', scopeSessionId: 's3', cwd: undefined, id: 'tab9', path: 'https://example.test/x' },
{ sessionId: 's3', scopeSessionId: 's3', cwd: undefined, id: 'tab9', path: 'https://example.test/x', title: 'site' },
])
})