From 9489f4106834ae8be3613296e7b6b0c48d116f8e Mon Sep 17 00:00:00 2001 From: gyixuan Date: Sat, 12 Sep 2026 13:29:05 +0800 Subject: [PATCH 1/2] fix(native): focus through the controller on surface.activate; carry meta on close MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit surface.activate() only returned records.has(tabId) without focusing, so external plugins with multi-instance native tabs (dsh-sidenote's side chats) could never bring an existing tab to the front programmatically. Focus via ISidebarRight.focus (dsh >= 0.1.5), which focuses the tab and its pane. surface.close() now also returns the record's meta so service.closeTab can hand it to descriptor.onClose — native closes previously dropped it, leaving meta-driven lifecycle consumers (e.g. recently-closed records) starved. --- src/client/native/surface.ts | 18 +++++++++++++----- src/client/service.ts | 11 ++++++++--- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/client/native/surface.ts b/src/client/native/surface.ts index 4886f205..86d5751d 100644 --- a/src/client/native/surface.ts +++ b/src/client/native/surface.ts @@ -47,6 +47,8 @@ interface NativeController { close(tabId: string): void /** The mounted-seat observation (0.1.7 `ISidebarRight.mounted`). */ mounted?: MountedSessions + /** `ISidebarRight.focus` — focuses the tab and the pane holding it (dsh >= 0.1.5). */ + focus?(tabId: string): void /** Not part of `ISidebarRight`: the concrete controller's per-session writes. */ openTabIn?(sessionId: string, kind: string, options?: { params?: unknown; revealIfOpened?: boolean }): void openResourceIn?(sessionId: string, address: string, options?: { params?: unknown; revealIfOpened?: boolean }): void @@ -203,7 +205,11 @@ export function createNativeSurface(ctx: Context, records: NativeTabRecords): Na if (sessionId === mountedSessionId(ctx)) api.close(tabId) else if (api.closeIn !== undefined) api.closeIn(sessionId, tabId) } - return { type: record.tab.type, title: record.tab.title } + return { + type: record.tab.type, + title: record.tab.title, + ...(record.tab.meta === undefined ? {} : { meta: record.tab.meta }), + } }, update(tabId, patch) { if (!records.has(tabId)) return false @@ -211,10 +217,12 @@ export function createNativeSurface(ctx: Context, records: NativeTabRecords): Na return true }, activate(tabId) { - // The native surface has no cross-pane activation face the plugin needs: - // a tab is focused by opening its (kind, address) again, which the - // native open already de-duplicates. - return records.has(tabId) + if (!records.has(tabId)) return false + // External plugin tabs (multi-instance kinds like side chats) carry no + // (kind, address) identity to re-open against, so "open again to focus" + // cannot work for them — focus through the controller's own face. + controller()?.focus?.(tabId) + return true }, has: tabId => records.has(tabId), flushPending, diff --git a/src/client/service.ts b/src/client/service.ts index a26fd8d5..80ec0584 100644 --- a/src/client/service.ts +++ b/src/client/service.ts @@ -454,8 +454,8 @@ export interface SidebarSurface { openResource(input: { sessionId: string; address: string; line?: number; revealIfOpened: boolean }): void /** The file address of one path (the native surface owns the grammar). */ fileAddress(sessionId: string, cwd: string | undefined, path: string): string - /** Close one native tab; the closed record's type/title, or undefined when the id is not native. */ - close(sessionId: string, tabId: string): { type: string; title: string } | undefined + /** Close one native tab; the closed record's type/title/meta, or undefined when the id is not native. */ + close(sessionId: string, tabId: string): { type: string; title: string; meta?: unknown } | undefined /** Patch a native tab's plugin-side record; false when it is not native. */ update(tabId: string, patch: { title?: string; path?: string; meta?: unknown }): boolean /** Focus a native tab; false when it is not native. */ @@ -1057,7 +1057,12 @@ export function createBetterSidebarService(store: SidebarStore): BetterSidebarSe const descriptor = tabs.get(closedNative.type) if (descriptor !== undefined) { safeCall(() => descriptor.onClose?.( - { id: tabId, type: closedNative.type as TabType, title: closedNative.title }, + { + id: tabId, + type: closedNative.type as TabType, + title: closedNative.title, + ...(closedNative.meta === undefined ? {} : { meta: closedNative.meta }), + }, scope ?? { sessionId }, )) } From bc1c6be74712bf407e997845de933e59c08e0997 Mon Sep 17 00:00:00 2001 From: gyixuan Date: Mon, 28 Sep 2026 14:38:12 +0800 Subject: [PATCH 2/2] test(native): pin activate-focus and close-meta; document the native lifecycle - five cases over the real createNativeSurface: activate rides ISidebarRight.focus for a known record (and stays a no-op for unknown ids), close hands the record meta back and rides the on-screen face for the mounted session (per-session face otherwise) - one service-level case: closeTab hands the native close meta through to descriptor.onClose - guide: activateTab's native path (focus via ISidebarRight.focus, dsh >= 0.1.5) and the close-meta carry, including the standing gap that a user-initiated x-close never reaches onClose --- docs/external-plugin-guide.md | 8 +-- tests/native-surface.spec.ts | 95 +++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 4 deletions(-) diff --git a/docs/external-plugin-guide.md b/docs/external-plugin-guide.md index d1dc6ed8..c0ef1d7e 100644 --- a/docs/external-plugin-guide.md +++ b/docs/external-plugin-guide.md @@ -14,13 +14,13 @@ - `registerTab` / `registerFileViewer` 签名不变; - `openTab` / `openFile` 默认落到原生右侧栏;新增可选 `OpenTabSeed.target`(`'right'` 默认 / `'bottom'` 落插件的底部工作台); -- `updateTab` / `closeTab` / `activateTab` 认识原生 tab id(插件为每个原生 tab 维护一条合成 `SidebarTab` 记录,`tab.meta` / `tab.path` 的写入照旧生效)。 +- `updateTab` / `closeTab` / `activateTab` 认识原生 tab id(插件为每个原生 tab 维护一条合成 `SidebarTab` 记录,`tab.meta` / `tab.path` 的写入照旧生效);`activateTab` 对原生 tab 经宿主 `ISidebarRight.focus`(dsh ≥ 0.1.5)聚焦 tab 及其所在 pane——多实例外部插件 tab(无 `(kind, 地址)` 身份可重开)因此也能被程序化唤起到前台。 行为差异(写在这里以免踩坑): | 事项 | 说明 | |---|---| -| 生命周期回调 | 原生面只有「一次打开」,不区分新建/聚焦,因此只触发 `onOpen`(`onActivate` 仅在插件自己的底部工作台里触发) | +| 生命周期回调 | 原生面的「打开」不区分新建/聚焦,因此 `onOpen` 每次打开都触发(`onActivate` 仅在插件自己的底部工作台里触发)。程序化 `closeTab` 与 `activateTab` 在原生 tab 上正常工作:关闭走宿主 per-session 面,回调拿到完整 `tab`(**含 `meta`**);激活经 `ISidebarRight.focus` 聚焦 tab 与其 pane。**例外**:用户直接点原生 tab 上的 × 关闭不会触发 `onClose`(宿主不广播该事件,插件在 tab 体卸载时丢弃记录) | | 去重 | 原生按 `(kind, 地址)` 去重:有 `createTab` 的类型每次新开一个 tab(sidechat / diff),其余聚焦已有 tab;`dedupeKey` 的自定义语义不参与原生面 | | 布局持久化 | 原生栏的布局**只在内存**(刷新后回到折叠默认),插件自己的底部工作台仍然持久化 | | 跨会话打开 | 目标会话的右侧栏 store 未挂载时,打开会排队到该会话上屏后重放 | @@ -248,9 +248,9 @@ interface TabDescriptor { badge?: (ctx: Context, scope: SessionScope, state: SidebarState) => string | number | null | undefined /** * 生命周期回调(v0.12.0+),只由 SERVICE 路径触发: - * - onOpen:openTab 真正**新建** tab 后(dedupe/id 安全网聚焦不算打开); + * - onOpen:openTab 真正**新建** tab 后(dedupe/id 安全网聚焦不算打开;原生面每次打开都触发); * - onActivate:tab 被聚焦时(dedupe 聚焦、id 安全网聚焦、tab 栏点击激活); - * - onClose:closeTab 关闭 tab 后。 + * - onClose:closeTab 关闭 tab 后(原生 tab 同样携带完整 tab,含 meta;用户点宿主 × 的关闭除外)。 * 内置专属流程(diff 拆分放置、sidechat 线程重开)直接改 state,不触发 * 回调——但它们只作用于内置类型(diff / sidechat),外部插件的 tab 永远走 * service 路径。回调抛错只 console.error,绝不打断打开/关闭流程。 diff --git a/tests/native-surface.spec.ts b/tests/native-surface.spec.ts index 88ca574c..786ce300 100644 --- a/tests/native-surface.spec.ts +++ b/tests/native-surface.spec.ts @@ -9,6 +9,7 @@ import { createElement } from 'react' import { createRoot, type Root } from 'react-dom/client' import { act } from 'react-dom/test-utils' import { createNativeTabRecords, NativeTabBody, NativeTabTitle } from '../src/client/native/tab-adapter.tsx' +import { createNativeSurface } from '../src/client/native/surface.ts' import { registerNativeSurface } from '../src/client/native/index.ts' import { createBetterSidebarService, type SidebarSurface } from '../src/client/service.ts' import { createSidebarStore, type SidebarTab } from '../src/client/state.ts' @@ -193,6 +194,30 @@ describe('service routing into the native surface', () => { expect(() => service.closeTab('other', scope)).not.toThrow() }) + it('hands the native close meta to descriptor.onClose', () => { + // Regression #644: the native close path projected the record down to + // type/title, starving meta-driven lifecycle consumers (recently-closed + // records, reopen flows). + const surface: SidebarSurface = { + openTab: () => {}, + openResource: () => {}, + fileAddress: () => 'addr', + close: () => ({ type: 'my-plugin:term', title: 'T', meta: { threadId: 't-9' } }), + update: () => false, + activate: () => false, + has: () => true, + } + const store = createSidebarStore() + store.setSession('s1') + const service = createBetterSidebarService(store) + service.setSurface(surface) + const seen: SidebarTab[] = [] + service.registerTab({ id: 'my-plugin:term', title: 'Terminal', component: () => null, onClose: tab => { seen.push(tab) } }) + service.closeTab('native-1', scope) + expect(seen).toHaveLength(1) + expect(seen[0]).toMatchObject({ id: 'native-1', type: 'my-plugin:term', title: 'T', meta: { threadId: 't-9' } }) + }) + it('refuses a disabled type before touching the surface', () => { const { service, calls } = mount() service.setSurface(undefined) @@ -212,6 +237,76 @@ describe('service routing into the native surface', () => { }) }) +describe('createNativeSurface activate/close (the real adapter)', () => { + const mountSurface = (): { + surface: ReturnType + records: ReturnType + focus: ReturnType + close: ReturnType + } => { + const focus = vi.fn() + const close = vi.fn() + const controller = { + openTab: () => {}, + openResource: () => {}, + close, + // `ISidebarRight.focus` (dsh >= 0.1.5): the face activate() rides. + focus, + mounted: { getSnapshot: () => 's1', subscribe: () => () => {} }, + openTabIn: () => {}, + openResourceIn: () => {}, + closeIn: () => {}, + } + const records = createNativeTabRecords() + const ctx = { + get: (name: string) => (name === 'sidebarRight' ? controller : undefined), + sessions: { list: { subscribe: () => () => {}, getSnapshot: () => ({}) } }, + } + const surface = createNativeSurface(ctx as never, records) + return { surface, records, focus, close } + } + + it('activate focuses an existing record through the controller focus face', () => { + // Regression #644: activate() used to return records.has(tabId) without + // focusing anything — external plugins with multi-instance native tabs + // (dsh-sidenote's side chats) could never bring a tab to the front. + const { surface, records, focus } = mountSurface() + records.ensure({ id: 'tab-9', kind: 'sidechat', title: 'Side Chat', params: { meta: { threadId: 't-1' } }, scope }) + expect(surface.activate('tab-9')).toBe(true) + expect(focus).toHaveBeenCalledTimes(1) + expect(focus).toHaveBeenCalledWith('tab-9') + }) + + it('activate leaves unknown ids alone and reports them', () => { + const { surface, focus } = mountSurface() + expect(surface.activate('missing')).toBe(false) + expect(focus).not.toHaveBeenCalled() + }) + + it('close returns the record meta and rides the on-screen face for the mounted session', () => { + const { surface, records, close } = mountSurface() + records.ensure({ id: 'tab-10', kind: 'sidechat', title: 'hello thread', params: { meta: { threadId: 't-7' } }, scope }) + const closed = surface.close('s1', 'tab-10') + expect(closed).toEqual({ type: 'sidechat', title: 'hello thread', meta: { threadId: 't-7' } }) + // The mounted seat is s1's, so the host close rode the on-screen face. + expect(close).toHaveBeenCalledWith('tab-10') + expect(records.has('tab-10')).toBe(false) + }) + + it('close of a non-mounted session rides the per-session face', () => { + const { surface, records } = mountSurface() + records.ensure({ id: 'tab-11', kind: 'sidechat', title: 'bg', params: undefined, scope }) + const closed = surface.close('s2', 'tab-11') + expect(closed).toEqual({ type: 'sidechat', title: 'bg' }) + }) + + it('close of an unknown id is undefined (a strict no-op)', () => { + const { surface, close } = mountSurface() + expect(surface.close('s1', 'missing')).toBeUndefined() + expect(close).not.toHaveBeenCalled() + }) +}) + describe('registerNativeSurface lifecycle (service-driven registration)', () => { it('registers the native tab types when the tab-type registry ARRIVES after the slot declaration', () => { // Regression: the native seat declares `sidebar.right.pane.tab` before it