diff --git a/src/client/native/tab-adapter.tsx b/src/client/native/tab-adapter.tsx index 979d74d9..4c390f7c 100644 --- a/src/client/native/tab-adapter.tsx +++ b/src/client/native/tab-adapter.tsx @@ -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, }) } } diff --git a/src/client/service.ts b/src/client/service.ts index 94eaee56..390dad18 100644 --- a/src/client/service.ts +++ b/src/client/service.ts @@ -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 } /** diff --git a/src/client/tree-mutations.ts b/src/client/tree-mutations.ts index f1096fb5..59b0849e 100644 --- a/src/client/tree-mutations.ts +++ b/src/client/tree-mutations.ts @@ -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) } } diff --git a/tests/editor-unsaved-guard.spec.tsx b/tests/editor-unsaved-guard.spec.tsx index ad6f24df..88279909 100644 --- a/tests/editor-unsaved-guard.spec.tsx +++ b/tests/editor-unsaved-guard.spec.tsx @@ -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, 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. diff --git a/tests/native-surface.spec.ts b/tests/native-surface.spec.ts index 9cb080d2..7e6a2022 100644 --- a/tests/native-surface.spec.ts +++ b/tests/native-surface.spec.ts @@ -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' }, ]) })