From 26a1eb20dbd81e4fb25b2c8f2f5fb5d513147686 Mon Sep 17 00:00:00 2001 From: ymac <707337118@qq.com> Date: Mon, 12 Oct 2026 08:38:32 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix(tree):=20=E6=94=B9=E5=90=8D/=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E7=9A=84=E8=B0=83=E8=A7=A3=E8=A6=86=E7=9B=96=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E4=BC=9A=E8=AF=9D=E7=9A=84=E5=BA=95=E9=83=A8=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=E5=8F=B0=E9=A1=B5=E7=AD=BE=EF=BC=88#915=20=E5=90=8E?= =?UTF-8?q?=E7=BB=AD=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #912 让调解同时走原生承载面的记录,但底部工作台那一路仍只读**当前会话**的 快照(`pathTabsOf` 吃 `store.getSnapshot()`),而底部工作台是**按会话**存的: 后台会话里开着的文件页(`target: 'bottom'` 的打开、或在原生承载面之前持久化 下来的布局)既不跟着改名、也不随删除关闭,切回去的下一次保存照样把改名掉 / 删掉的文件重新创建出来。 - 枚举改走 `store.getSessionStates()`:**每个已缓存会话**的底部工作台都在内, 每行带上**自己的会话**(原生那一路本来就有 seat 会话);`PathTab.sessionId` 随之必填。 - 写入带会话:`updateTab` / `closeTab` 遇到**不在屏的会话**改走定向通道 (`store.reduceFor`,与定向 `openTab(seed, scope)` 同一条路:写进那个会话 自己的布局,不发布、不通知);当前会话继续走发布通道,界面照常重渲染。 - 删除的草稿确认继续问在**内容会话**里(草稿注册表的键是 `sessionId::tabId`)。 测试:`tests/editor-unsaved-guard.spec.tsx` 的 tree rename/delete reconciliation 组 +2 例(后台会话的底部页面跟着改名、且当前会话既没被写也没被重新发布;删除时 按它自己的草稿先问、关在它自己的布局里)。判别性实测:枚举收回「只看当前会话」 → 2 条红;定向写入换回发布通道 → 2 条红;草稿确认的会话换成当前会话 → 删除 那条红。 --- src/client/service.ts | 47 +++++++++++++++-- src/client/tree-mutations.ts | 78 +++++++++++++++++------------ tests/editor-unsaved-guard.spec.tsx | 56 +++++++++++++++++++++ 3 files changed, 144 insertions(+), 37 deletions(-) diff --git a/src/client/service.ts b/src/client/service.ts index 9fb9d2d5..94eaee56 100644 --- a/src/client/service.ts +++ b/src/client/service.ts @@ -704,6 +704,11 @@ export interface BetterSidebarService { * strict no-op (no state churn, no callbacks). `scope` (v0.12.0+) rides * to the callback (its optional cwd included); absent, the callback gets * `{ sessionId }` of the active session. + * + * v0.26.0+: a scope naming a session that is **not the one on screen** closes + * the row in THAT session's layout through the targeted channel (no publish, + * no notify), which is how the file tree's delete reconciliation reaches a + * background conversation's pages. Without a scope nothing changes. */ closeTab(tabId: string, scope?: SessionScope): void /** Subscribe to registry changes (register/dispose). */ @@ -735,6 +740,13 @@ export interface BetterSidebarService { * carried (#826) is dropped with it, because that line belongs to the * navigation that delivered the file — an in-place switch or a rename that * kept it would send the next file to the previous file's line. + * + * v0.26.0+: a `sessionId` that is **not the session on screen** patches that + * session's own layout through the targeted channel (no publish, no notify — + * the same route a targeted `openTab(seed, scope)` takes), so the file tree's + * rename reconciliation reaches a background conversation's pages instead of + * resolving them against the active session. The active session keeps the + * publishing path, and an omitted `sessionId` behaves exactly as before. */ updateTab(tabId: string, patch: { title?: string; path?: string; meta?: unknown }, sessionId?: string): void /** @@ -1290,6 +1302,23 @@ export function createBetterSidebarService(store: SidebarStore): BetterSidebarSe else if (activated !== undefined) safeCall(() => descriptor.onActivate?.(activated!, callbackScope)) } + /** + * Apply a store mutation in the session it names. + * + * A session that is not on screen takes the TARGETED channel (`reduceFor`), + * the same one a targeted `openTab(seed, scope)` uses: it writes that + * session's own state and persists it without touching the published + * snapshot. The publishing channel cannot stand in for it — the row is not in + * the ACTIVE session's tree, so the reduce is a silent no-op at best, and + * patching a same-numbered row of the active session is the other possible + * outcome. The active session keeps the publishing path so the shell + * re-renders with the new title/path. + */ + const reduceInSession = (sessionId: string, reducer: (state: SidebarState) => SidebarState): void => { + if (sessionId === store.getSnapshot().sessionId) store.reduce(reducer) + else store.reduceFor(sessionId, reducer) + } + const closeTab = (tabId: string, scope?: SessionScope): void => { const sessionId = scope?.sessionId ?? store.getSnapshot().sessionId if (surface !== undefined && sessionId !== undefined) { @@ -1311,7 +1340,7 @@ export function createBetterSidebarService(store: SidebarStore): BetterSidebarSe } } let closed: SidebarTab | undefined - store.reduce((state) => { + const closeOne = (state: SidebarState): SidebarState => { // Unknown tab ids are a strict no-op: no state churn, no notify, no // pointless localStorage rewrite (mirrors updateTab's short-circuit). if (!tabOpenIn(state, tabId)) return state @@ -1319,7 +1348,11 @@ export function createBetterSidebarService(store: SidebarStore): BetterSidebarSe const leaf = leafWithTab(state.bottomSplits, tabId) closed = leaf?.tabs.find(tab => tab.id === tabId) return closeTabReducer(state, paneId, tabId) - }) + } + // The row lives in ITS session's tree: an explicit scope that is not on + // screen has to close it there (see `reduceInSession`). + if (sessionId === undefined) store.reduce(closeOne) + else reduceInSession(sessionId, closeOne) if (closed !== undefined) { const sessionId = scope?.sessionId ?? store.getSnapshot().sessionId if (sessionId !== undefined) { @@ -1343,11 +1376,17 @@ export function createBetterSidebarService(store: SidebarStore): BetterSidebarSe sessionId?: string, ): void => { if (surface?.update(tabId, patch, sessionId) === true) return - store.reduce((state) => patchTab(state, tabId, { + const patchOne = (state: SidebarState): SidebarState => patchTab(state, tabId, { ...(patch.title !== undefined ? { title: patch.title } : {}), ...(patch.path !== undefined ? { path: patch.path } : {}), ...(patch.meta !== undefined ? { meta: patch.meta } : {}), - })) + }) + // A named session that is not on screen is patched in ITS own state (a + // background conversation's file page under a rename — see + // `reduceInSession`); without one the current session keeps the publishing + // path it always had. + if (sessionId === undefined) store.reduce(patchOne) + else reduceInSession(sessionId, patchOne) } /** Activate an open tab (the tab-bar activation path; fires onActivate). */ diff --git a/src/client/tree-mutations.ts b/src/client/tree-mutations.ts index 7a7f215c..f1096fb5 100644 --- a/src/client/tree-mutations.ts +++ b/src/client/tree-mutations.ts @@ -15,12 +15,18 @@ * a delete must close every tab at or under the removed path. Both ride the * SERVICE paths (`updateTab`/`closeTab`) rather than direct state reducers so * the registered lifecycle callbacks fire like any other tab mutation. + * + * The walk covers EVERY session the store has cached, not just the one on + * screen: the bottom workbench is per session, and a background conversation's + * file page has the same next-save hazard. Each row therefore names its own + * session, and the writes go to that session (`updateTab`/`closeTab` take the + * targeted channel for a session that is not on screen). */ import type { Context } from '../context-types.ts' import type { NativePathTab } from './service.ts' import { baseName, isWithinWorkspace, relativeTo, resolveSidebarPath } from './paths.ts' import { confirmDiscardDraft } from './editor-dirty.ts' -import { allLeaves, type SidebarSnapshot, type SidebarStore } from './state.ts' +import { allLeaves, type SidebarStore } from './state.ts' /** One open tab that carries a file path, and the space it lives in. */ interface PathTab { @@ -36,13 +42,15 @@ interface PathTab { absolute: string /** The session this tab's EDITOR reads and writes in — the `scope.sessionId` * its host registers the unsaved draft under, so it is the one the close - * guard has to name. Absent only for a bottom-workbench tab whose store has - * no session yet (the guard then reads conservatively rather than missing - * the record and closing a live draft without asking). */ + * guard has to name. Absent only for a native tab whose address named no + * session (the guard then reads conservatively rather than missing the + * record and closing a live draft without asking). */ scopeSessionId: string | undefined - /** The native tab's seat session; absent for a bottom-workbench tab, whose - * updates resolve against the store's current session. */ - sessionId?: 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 + * against whatever session happens to be on screen. */ + sessionId: string } /** @@ -110,28 +118,32 @@ export function clearRetargetedPath(key: string): void { retargetedPaths.delete(key) } -/** Every open tab that carries a file path, from BOTH tab spaces. */ -function pathTabsOf(snapshot: SidebarSnapshot, native: readonly NativePathTab[]): PathTab[] { +/** Every open tab that carries a file path, from BOTH tab spaces — the bottom + * workbench of EVERY cached session, and the native records. */ +function pathTabsOf(store: SidebarStore, native: readonly NativePathTab[]): PathTab[] { const tabs: PathTab[] = [] - const state = snapshot.state - if (state !== undefined) { - // A bottom-workbench row resolves its scope against the store's session, so - // that same session is the one its editor registered its draft under — the - // key's `?? ''` is only the marker's spelling for "no session". - const session = snapshot.sessionId ?? '' + // Every session the store has cached, not just the one on screen: a + // conversation the reader is not looking at can hold file pages of its own + // (a `target: 'bottom'` open, a layout persisted back when files landed in + // the workbench), and a rename/delete has to reach them too — the first save + // after switching back would otherwise recreate the renamed-away file, which + // is the very data problem this module exists to prevent. Each row therefore + // carries its OWN session: it is both the identity half of the rename marker + // and the channel its write goes through. + for (const [sessionId, state] of store.getSessionStates()) { for (const leaf of allLeaves(state.bottomSplits)) { for (const tab of leaf.tabs) { // A bottom-workbench row carries an absolute path already, and it is the // same string the tree hands this module — one spelling, both fields. - if (tab.path !== undefined) { - tabs.push({ - key: pathTabKey(session, tab.id), - id: tab.id, - path: tab.path, - absolute: tab.path, - scopeSessionId: snapshot.sessionId, - }) - } + if (tab.path === undefined) continue + tabs.push({ + key: pathTabKey(sessionId, tab.id), + id: tab.id, + path: tab.path, + absolute: tab.path, + scopeSessionId: sessionId, + sessionId, + }) } } } @@ -174,15 +186,13 @@ function movedPath(path: string, oldPath: string, newPath: string): string | und * base name, matching how openFile titles editor tabs). * * Files and anything under a renamed DIRECTORY are retargeted — the same - * subtree rule the delete path uses. The move is announced through - * {@link markRetargetedPath} so the editor keeps its document (and an unsaved - * draft) instead of reloading the new name. */ + * 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. */ export function retargetPathTabs(ctx: Context, store: SidebarStore, oldPath: string, newPath: string): void { const service = ctx.get('betterSidebar') if (service === undefined) return - // The tabs live in the snapshot's session: native ids restart per session. - const snapshot = store.getSnapshot() - for (const tab of pathTabsOf(snapshot, service.nativePathTabs())) { + for (const tab of pathTabsOf(store, service.nativePathTabs())) { const moved = movedPath(tab.absolute, oldPath, newPath) if (moved === undefined) continue // The announcement has to match what the editor will see: it compares the @@ -200,17 +210,19 @@ export function retargetPathTabs(ctx: Context, store: SidebarStore, oldPath: str * * The question is asked in the tab's OWN session: the draft registry is keyed * by `sessionId::tabId`, so a bare id read would look up a same-numbered tab in - * some other session (or nothing at all) and close a live draft unprompted. */ + * some other session (or nothing at all) and close a live draft unprompted. + * Every session the store has cached is walked (a background conversation's + * page would otherwise come back with its next save). */ export function closePathTabs(ctx: Context, store: SidebarStore, target: string, unsavedMessage: string): void { const service = ctx.get('betterSidebar') if (service === undefined) return - for (const tab of pathTabsOf(store.getSnapshot(), service.nativePathTabs())) { + for (const tab of pathTabsOf(store, service.nativePathTabs())) { // `isWithinWorkspace` is true for the target itself too, so one test covers // both "this exact file" and "anything under this directory". if (!isWithinWorkspace(target, tab.absolute)) continue if (!confirmDiscardDraft(tab.id, unsavedMessage, tab.scopeSessionId)) continue // The tab is going away: a move announced for it can never be claimed. clearRetargetedPath(tab.key) - service.closeTab(tab.id, tab.sessionId === undefined ? undefined : { sessionId: tab.sessionId }) + service.closeTab(tab.id, { sessionId: tab.sessionId }) } } diff --git a/tests/editor-unsaved-guard.spec.tsx b/tests/editor-unsaved-guard.spec.tsx index bf9f8663..ad6f24df 100644 --- a/tests/editor-unsaved-guard.spec.tsx +++ b/tests/editor-unsaved-guard.spec.tsx @@ -464,6 +464,62 @@ describe('tree rename/delete reconciliation', () => { expect(records.has('native-scope', 'tab4')).toBe(false) expect(records.has('native-scope', 'tab5')).toBe(true) }) + + /** The file paths one session's bottom workbench has open (tree order). */ + const bottomPaths = (store: SidebarStore, sessionId: string): string[] => { + const state = store.getSessionStates().get(sessionId) + if (state === undefined) return [] + return allLeaves(state.bottomSplits) + .flatMap(leaf => leaf.tabs) + .flatMap(tab => (tab.path === undefined ? [] : [tab.path])) + } + + it('retargets a BACKGROUND session\'s bottom-workbench page (every cached session is walked)', () => { + // The bottom workbench is per session, and a file can live there on purpose + // (`target: 'bottom'`) or in a layout persisted back when files landed in + // the workbench. The reconciliation used to read the ACTIVE session's state + // alone, so a background conversation's page kept the old path and its next + // save recreated the renamed-away file — the same data problem, one + // conversation over. + const { store, service, context } = mountNativeSession() + service.openTab( + { type: 'editor', title: 'a.ts', path: '/ws/a.ts', target: 'bottom' }, + { sessionId: 'other-scope' }, + ) + expect(bottomPaths(store, 'other-scope')).toEqual(['/ws/a.ts']) + expect(store.getSnapshot().sessionId).toBe('native-scope') + + retargetPathTabs(context, store, '/ws/a.ts', '/ws/b.ts') + + // The page follows the file in ITS session… + expect(bottomPaths(store, 'other-scope')).toEqual(['/ws/b.ts']) + // …while the session on screen is neither written to nor re-published: a + // targeted write never touches the snapshot the shell renders from. + expect(store.getSnapshot().sessionId).toBe('native-scope') + expect(bottomPaths(store, 'native-scope')).toEqual([]) + }) + + it('closes a BACKGROUND session\'s page on delete, asking about ITS draft', () => { + const confirmSpy = vi.fn().mockReturnValue(true) + vi.stubGlobal('confirm', confirmSpy) + const { store, service, context } = mountNativeSession() + service.openTab( + { type: 'editor', title: 'a.ts', path: '/ws/a.ts', target: 'bottom' }, + { sessionId: 'other-scope' }, + ) + const row = allLeaves(store.getSessionStates().get('other-scope')!.bottomSplits) + .flatMap(leaf => leaf.tabs).find(tab => tab.path === '/ws/a.ts')! + // The editor behind that page registered its draft in ITS session, so the + // question has to be asked there — read against the active session the + // record would not be found and the tab would close unprompted. + setEditorDirty(row.id, true, 'other-scope', '/ws/a.ts') + + closePathTabs(context, store, '/ws/a.ts', 'discard?') + + expect(confirmSpy).toHaveBeenCalledWith('discard?') + expect(bottomPaths(store, 'other-scope')).toEqual([]) + expect(store.getSnapshot().sessionId).toBe('native-scope') + }) }) describe('EditorHost dirty registration', () => { From 2d850a5261b419ec5714c0919b5d011b0bb5a52f Mon Sep 17 00:00:00 2001 From: ymac <707337118@qq.com> Date: Mon, 12 Oct 2026 08:38:35 +0800 Subject: [PATCH 2/4] =?UTF-8?q?docs:=20=E8=AE=B0=E5=BD=95=20#915=20?= =?UTF-8?q?=E5=90=8E=E7=BB=AD=E2=80=94=E2=80=94=E5=90=8E=E5=8F=B0=E4=BC=9A?= =?UTF-8?q?=E8=AF=9D=E9=A1=B5=E7=AD=BE=E7=9A=84=E8=B0=83=E8=A7=A3=E4=B8=8E?= =?UTF-8?q?=E5=AE=9A=E5=90=91=E5=86=99=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - CHANGELOG:`### Unreleased` 置顶补这一条(含两条判别性实测)。 - 指南 §7:`updateTab` / `closeTab` 的 JSDoc 补「指到不在屏的会话时写进它 自己的布局(定向通道,不发布、不通知)」——AGENTS §5 要求 service.ts 的 行为变更同批同步指南。 --- CHANGELOG.md | 3 +++ docs/external-plugin-guide.md | 10 ++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dbff5f21..28efde42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ ### Unreleased +- 🧷 **改名 / 删除的页签调解覆盖后台会话的底部工作台**([#915](https://github.com/omdsh-dev/DSH-better-sidebar/issues/915) 后续):上一批(#912)让调解同时走**原生右侧栏**的记录,但底部工作台那一路仍只读**当前会话**的快照(`pathTabsOf` 吃 `store.getSnapshot()`),而底部工作台是**按会话**存的——后台会话里开着的文件页(`target: 'bottom'` 的打开、或在原生承载面之前持久化下来的布局)既不跟着改名、也不随删除关闭,切回去的下一次保存照样把改名掉的文件 / 删掉的文件重建出来。现在枚举走 `store.getSessionStates()`(**每个已缓存会话**都在内),每行都带上**自己的会话**;写入也不再落到「碰巧在屏」的那个会话上——`updateTab` / `closeTab` 遇到**不在屏的会话**改走定向通道(`store.reduceFor`,与定向 `openTab(seed, scope)` 同一条路:写进那个会话的自己的布局,不发布、不通知),当前会话继续走发布通道、界面照常重渲染。删除仍先问未保存的草稿,且问题问在**内容会话**里(草稿注册表的键是 `sessionId::tabId`)。 + - 🧪 测试:`tests/editor-unsaved-guard.spec.tsx` 新增 2 例(后台会话的底部页面在改名后跟着走、且当前会话既没被写也没被重新发布;删除时按**它自己的**草稿先问、关在它自己的布局里)。判别性实测:把枚举收回「只看当前会话」→ 2 条红;把定向写入换回发布通道 → 2 条红;把草稿确认的会话换成当前会话 → 删除那条红。 + - 🛡 **三处静默数据问题**([#898](https://github.com/omdsh-dev/DSH-better-sidebar/issues/898) / [#899](https://github.com/omdsh-dev/DSH-better-sidebar/issues/899) / [#900](https://github.com/omdsh-dev/DSH-better-sidebar/issues/900)):① 编辑器草稿守卫的记账键从**裸 tab id** 改成 **`会话::tab id`**——原生右栏每个会话的 id 都从 `tab1` 重新计数、底部工作台对同一路径在各会话复用 `editor:`,两套身份原先挤在同一个键上,**另一个会话打开编辑器就会把本会话的草稿记录一并删掉**(刷新时静默丢失);② 面包屑折叠行(`a/b/c` 压成一行)的重命名此前提交的是**链头**目录、与预填名不一致——不改字直接回车等于把父目录改名,现在落到链尾;③ 无 BOM 的 16 位小端数据(低幅采样表 / 未压缩音频数据段这类)不再被判成 UTF-16 文本打开、保存后整份改写(奇数长度还会丢尾字节):新的文本闸门要求 ≥90% 的单元是合法文本单元。同批还带了 dev 新增代码的一轮去重(内部导出 `runGitRaw` / `bottomTerminalKey` / `formatModelRoute` / `dirtyCountForSession` 收敛,仓内调用点同批改完)。(贡献者 PR [#901](https://github.com/omdsh-dev/DSH-better-sidebar/pull/901) @UNscientific-9) - 🧪 测试:`tests/editor-unsaved-guard.spec.tsx`(跨会话草稿:第二个会话挂同一个原生 id,既不能清掉、也不能确认掉第一个会话的草稿)、`tests/file-tree-compact-fold.spec.tsx`(折叠行重命名断言落到链尾:`/tmp/a/b/c` → `renamed`)、`tests/text-encoding.spec.ts`(uint16LE 采样表 / WAV `data` 段 / 奇数长度判二进制,真 UTF-16LE/BE 仍是文本)。 diff --git a/docs/external-plugin-guide.md b/docs/external-plugin-guide.md index 2c6409a1..beb288a4 100644 --- a/docs/external-plugin-guide.md +++ b/docs/external-plugin-guide.md @@ -773,7 +773,10 @@ interface BetterSidebarService { */ openTab(seed: OpenTabSeed, scope?: SessionScope): void /** 关闭一个 tab(未知 id 严格 no-op,无状态搅动);scope(v0.12.0+) - * 随回调传递(含可选 cwd),缺省为 { sessionId: 当前 } */ + * 随回调传递(含可选 cwd),缺省为 { sessionId: 当前 }。 + * scope 指的是**不在屏**的会话时(v0.26.0+),那一行在**它自己的**布局里关闭 + * ——走定向通道(不发布、不通知),与定向 `openTab(seed, scope)` 同一条路; + * 这正是文件树删除调解能覆盖后台会话页面的机制。不传 scope 行为不变。 */ closeTab(tabId: string, scope?: SessionScope): void /** 订阅注册表变化(register/dispose 时触发) */ subscribe(listener: () => void): () => void @@ -805,7 +808,10 @@ interface BetterSidebarService { * 下一次渲染当渲染垃圾抹掉,宿主**真正**发起的新导航(新修订)仍按地址 * 改写记录。文件树的重命名 / 删除调解会一并处理原生承载面上的页签(跟着 * 改址 / 按既有的未保存确认关闭;枚举由承载面内部交回服务,不是公开方法), - * 消费插件不需要改任何代码。 */ + * 消费插件不需要改任何代码。 + * **指到不在屏的会话时写进它自己的布局**(v0.26.0+):这类写入走定向通道 + * (不发布、不通知),所以文件树的重命名调解同样覆盖**后台会话**底部工作台里的 + * 页面,而当前会话继续走发布通道、界面照常重渲染。 */ updateTab(tabId: string, patch: { title?: string; path?: string; meta?: unknown }, sessionId?: string): void /** 激活一个已打开的 tab(tab 栏点击路径;触发 descriptor.onActivate; * 未知 id 严格 no-op);scope(v0.12.0+)随回调传递,同 closeTab */ From 953bebcda3d8a9123fe5fffccc2606fe6357adb1 Mon Sep 17 00:00:00 2001 From: ymac <707337118@qq.com> Date: Mon, 12 Oct 2026 08:38:50 +0800 Subject: [PATCH 3/4] =?UTF-8?q?fix(tree):=20=E6=94=B9=E5=90=8D=E5=8F=AA?= =?UTF-8?q?=E5=9C=A8=E6=A0=87=E9=A2=98=E6=9C=AC=E6=9D=A5=E5=B0=B1=E6=9D=A5?= =?UTF-8?q?=E8=87=AA=E6=96=87=E4=BB=B6=E5=90=8D=E6=97=B6=E6=89=8D=E6=94=B9?= =?UTF-8?q?=E5=86=99=EF=BC=88#915=20=E5=90=8E=E7=BB=AD=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 调解此前**无条件**把页签标题写成新的基名,于是**作者自设的标题**会被一次改名 悄悄替换掉:宿主的 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`。判别性实测:把判定换回无条件改写 → 该用例红。 --- src/client/native/tab-adapter.tsx | 4 ++++ src/client/service.ts | 5 +++++ src/client/tree-mutations.ts | 27 +++++++++++++++++++++++---- tests/editor-unsaved-guard.spec.tsx | 29 ++++++++++++++++++++++++++--- tests/native-surface.spec.ts | 8 +++++--- 5 files changed, 63 insertions(+), 10 deletions(-) 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' }, ]) }) From 3f7a344a49b7a3a22e22bf346065ffa2b45085aa Mon Sep 17 00:00:00 2001 From: ymac <707337118@qq.com> Date: Mon, 12 Oct 2026 08:38:50 +0800 Subject: [PATCH 4/4] =?UTF-8?q?docs:=20=E8=AE=B0=E5=BD=95=20#915=20?= =?UTF-8?q?=E5=90=8E=E7=BB=AD=E2=80=94=E2=80=94=E6=A0=87=E9=A2=98=E5=8F=AA?= =?UTF-8?q?=E5=9C=A8=E6=9D=A5=E8=87=AA=E6=96=87=E4=BB=B6=E5=90=8D=E6=97=B6?= =?UTF-8?q?=E6=89=8D=E8=B7=9F=E9=9A=8F=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - CHANGELOG:`### Unreleased` 置顶补这一条(含判别性实测)。 - 指南 §7:`updateTab` 的 JSDoc 补「派生标题跟着改名、作者自设的标题原样 保留」——这条 UI 行为由 service 的写面决定,按 AGENTS §5 同批同步。 --- CHANGELOG.md | 2 ++ docs/external-plugin-guide.md | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 28efde42..2efb3dce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Unreleased +- 🏷 **改名只在标题本来就来自文件名时才跟着改**([#915](https://github.com/omdsh-dev/DSH-better-sidebar/issues/915) 后续):页签调解此前**无条件**把标题写成新的基名,于是**作者自设的标题**会被一次改名悄悄替换掉——宿主的 agent-opens 推送就带着模型自己给的名字(`use-host-feeds` → `openFile(scope, path, title)`),消费插件也可以自己标名字。现在只有标题**等于该页签路径的基名**(也就是打开时由文件名派生的那种)才跟着新名字走;目录改名本来就不动后代(后代基名没变),两处口径因此一致。原生承载面的 `SidebarSurface.pathTabs()` 随之交出每个记录的 `title`(`NativePathTab` 新增字段)。 + - 🧪 测试:`tests/editor-unsaved-guard.spec.tsx` 新增 1 例(同一个文件的两个页签:派生标题跟着走、自设标题原样保留,底部工作台那一路一并断言);`tests/native-surface.spec.ts` 的枚举断言带上 `title`。判别性实测:把判定换回无条件改写 → 该用例红。 - 🧷 **改名 / 删除的页签调解覆盖后台会话的底部工作台**([#915](https://github.com/omdsh-dev/DSH-better-sidebar/issues/915) 后续):上一批(#912)让调解同时走**原生右侧栏**的记录,但底部工作台那一路仍只读**当前会话**的快照(`pathTabsOf` 吃 `store.getSnapshot()`),而底部工作台是**按会话**存的——后台会话里开着的文件页(`target: 'bottom'` 的打开、或在原生承载面之前持久化下来的布局)既不跟着改名、也不随删除关闭,切回去的下一次保存照样把改名掉的文件 / 删掉的文件重建出来。现在枚举走 `store.getSessionStates()`(**每个已缓存会话**都在内),每行都带上**自己的会话**;写入也不再落到「碰巧在屏」的那个会话上——`updateTab` / `closeTab` 遇到**不在屏的会话**改走定向通道(`store.reduceFor`,与定向 `openTab(seed, scope)` 同一条路:写进那个会话的自己的布局,不发布、不通知),当前会话继续走发布通道、界面照常重渲染。删除仍先问未保存的草稿,且问题问在**内容会话**里(草稿注册表的键是 `sessionId::tabId`)。 - 🧪 测试:`tests/editor-unsaved-guard.spec.tsx` 新增 2 例(后台会话的底部页面在改名后跟着走、且当前会话既没被写也没被重新发布;删除时按**它自己的**草稿先问、关在它自己的布局里)。判别性实测:把枚举收回「只看当前会话」→ 2 条红;把定向写入换回发布通道 → 2 条红;把草稿确认的会话换成当前会话 → 删除那条红。 diff --git a/docs/external-plugin-guide.md b/docs/external-plugin-guide.md index beb288a4..624f8263 100644 --- a/docs/external-plugin-guide.md +++ b/docs/external-plugin-guide.md @@ -811,7 +811,11 @@ interface BetterSidebarService { * 消费插件不需要改任何代码。 * **指到不在屏的会话时写进它自己的布局**(v0.26.0+):这类写入走定向通道 * (不发布、不通知),所以文件树的重命名调解同样覆盖**后台会话**底部工作台里的 - * 页面,而当前会话继续走发布通道、界面照常重渲染。 */ + * 页面,而当前会话继续走发布通道、界面照常重渲染。 + * **标题只在它本来就来自文件名时才跟着改名走**(v0.26.0+):页签调解把**派生 + * 自基名**的标题换成新基名,而 `openFile(scope, path, title)` 或 `openTab` 里 + * **作者自设的标题**(宿主的 agent-opens 推送就带一个)原样保留——名字是你标 + * 的,就不该被一次文件改名换掉。 */ updateTab(tabId: string, patch: { title?: string; path?: string; meta?: unknown }, sessionId?: string): void /** 激活一个已打开的 tab(tab 栏点击路径;触发 descriptor.onActivate; * 未知 id 严格 no-op);scope(v0.12.0+)随回调传递,同 closeTab */