From f74b60aa377f8638ce671bd0cbcc53ca604c2c24 Mon Sep 17 00:00:00 2001 From: xiaochaZ Date: Wed, 16 Sep 2026 23:15:53 +0800 Subject: [PATCH] =?UTF-8?q?fix(native):=20=E8=B7=A8=E4=BC=9A=E8=AF=9D?= =?UTF-8?q?=E6=89=93=E5=BC=80=E4=B8=8D=E5=86=8D=E4=BA=A4=E7=BB=99=20openTa?= =?UTF-8?q?bIn()=EF=BC=8C=E6=94=B9=E4=B8=BA=E6=8E=92=E9=98=9F=E5=88=B0?= =?UTF-8?q?=E4=B8=8A=E5=B1=8F=E9=87=8D=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 对"本页从未显示过"的会话调用 openTab/openResource 时,打开会被静默丢弃: runtime 只在该会话挂载过之后才 adopt 它的 rightbar store,而 openTabIn()/openResourceIn() 对这种会话是静默 no-op(不写、也不回报), place() 却无条件 return true,于是 entry 永远进不了 pending, flushPending() 也就永远不会重放它 —— 与模块头注释和指南 §0 承诺的 "目标会话未挂载时排队到上屏重放"相反。 - place() 对非在屏会话不再调用 openTabIn()/openResourceIn(),直接 return false 让 entry 入队,由既有 flushPending()(已订阅 session list)在该会话 上屏时重放;在屏会话的路径不变 - 同步模块头注释与 NativeController 的成员说明(那两个面保留为将来宿主 返回 boolean 时的接缝,closeIn 仍在用) - 新增 tests/native-surface-cross-session.spec.ts 锁定该契约 关联 #694 --- src/client/native/surface.ts | 32 ++--- tests/native-surface-cross-session.spec.ts | 129 +++++++++++++++++++++ 2 files changed, 148 insertions(+), 13 deletions(-) create mode 100644 tests/native-surface-cross-session.spec.ts diff --git a/src/client/native/surface.ts b/src/client/native/surface.ts index 6dc08617..4c1cd134 100644 --- a/src/client/native/surface.ts +++ b/src/client/native/surface.ts @@ -10,10 +10,11 @@ * * - the surface exists only while a session's panel is mounted, and the * service's public face (`ISidebarRight`) writes only into THAT session. - * The controller also carries `openTabIn` / `openResourceIn` / - * `closeIn`, which act on any session whose store the runtime has minted; - * both are probed at call time, and an open for a session that has no - * store yet is QUEUED and replayed when that session comes on screen; + * The controller also carries `openTabIn` / `openResourceIn` / `closeIn`, + * but the two open faces are silent no-ops for a session whose store the + * runtime never minted or adopted — they report nothing back — so an open + * aimed at a session that is not on screen is NOT handed to them: it stays + * QUEUED and is replayed once that session comes on screen; * - layout state is memory-only, so a queued open is not durable either. */ import type { Context } from '../../context-types.ts' @@ -31,7 +32,12 @@ interface NativeController { openTab(kind: string, options?: { params?: unknown; revealIfOpened?: boolean }): void openResource(address: string, options?: { params?: unknown; revealIfOpened?: boolean }): void close(tabId: string): void - /** Not part of `ISidebarRight`: the concrete controller's per-session writes. */ + /** + * Not part of `ISidebarRight`: the concrete controller's per-session writes. + * The two open faces are kept as the seam a boolean-reporting host API would + * plug into, but `place()` deliberately does not use them: they cannot tell + * "written" from "no store adopted yet" (see the module header). + */ openTabIn?(sessionId: string, kind: string, options?: { params?: unknown; revealIfOpened?: boolean }): void openResourceIn?(sessionId: string, address: string, options?: { params?: unknown; revealIfOpened?: boolean }): void closeIn?(sessionId: string, tabId: string): void @@ -76,10 +82,12 @@ export function createNativeSurface(ctx: Context, records: NativeTabRecords): Na api.openTab(entry.tabKind, options) return true } - if (api.openTabIn !== undefined) { - api.openTabIn(entry.sessionId, entry.tabKind, options) - return true - } + // Do not hand a non-current session's open to openTabIn(): for a session + // whose rightbar store the runtime never adopted (it was not opened since + // this page load) that call is a silent no-op that reports nothing back, so + // trusting it drops the open instead of queueing it as this module + // documents. Leave the entry queued — flushPending() replays it once that + // session comes on screen. return false } const options = { @@ -90,10 +98,8 @@ export function createNativeSurface(ctx: Context, records: NativeTabRecords): Na api.openResource(entry.address, options) return true } - if (api.openResourceIn !== undefined) { - api.openResourceIn(entry.sessionId, entry.address, options) - return true - } + // Same as the tab branch: a session with no adopted rightbar store makes + // openResourceIn() a silent no-op, so queue instead. return false } diff --git a/tests/native-surface-cross-session.spec.ts b/tests/native-surface-cross-session.spec.ts new file mode 100644 index 00000000..94f7fb55 --- /dev/null +++ b/tests/native-surface-cross-session.spec.ts @@ -0,0 +1,129 @@ +/** + * Cross-session opens at the native right-sidebar surface + * (`src/client/native/surface.ts`). + * + * The runtime only adopts a session's rightbar store once that session has been + * mounted; for a session whose store was never minted, `openTabIn()` / + * `openResourceIn()` are silent no-ops — they write nothing and report nothing + * back. Handing an off-screen session's open to them therefore LOSES it: + * `place()` returns true, `enqueue()` never queues the entry, and the replay + * subscribed to the session list never sees it. + * + * The queueing contract this file locks down is already the documented one: + * `docs/external-plugin-guide.md` (§0 「跨会话打开」: 目标会话的右侧栏 store + * 未挂载时,打开会排队到该会话上屏后重放) and AGENTS.md §3.10. + */ +import { describe, expect, it, vi } from 'vitest' +import { createNativeSurface } from '../src/client/native/surface.ts' +import { createNativeTabRecords } from '../src/client/native/tab-adapter.tsx' + +describe('createNativeSurface cross-session opens', () => { + /** Mount a surface over a recording controller + a switchable session list. */ + const mount = (initial = 's1') => { + const list = { current: initial } + let notify: (() => void) | undefined + const controller = { + openTab: vi.fn(), + openTabIn: vi.fn(), + openResource: vi.fn(), + openResourceIn: vi.fn(), + close: vi.fn(), + } + const ctx = { + get: () => controller, + sessions: { + list: { + getSnapshot: () => ({ current: list.current }), + subscribe: (fn: () => void) => { + notify = fn + return () => { notify = undefined } + }, + }, + }, + } + return { + surface: createNativeSurface(ctx as never, createNativeTabRecords()), + controller, + /** Switch the on-screen session, then let the surface replay its queue. */ + switchTo: (sessionId: string) => { + list.current = sessionId + notify?.() + }, + } + } + + it('queues a tab open for an off-screen session instead of handing it to openTabIn()', () => { + const { surface, controller, switchTo } = mount('s1') + + surface.openTab({ sessionId: 's2', kind: 'ego-browser:watch', params: {}, revealIfOpened: true }) + + expect(controller.openTabIn).not.toHaveBeenCalled() + expect(controller.openTab).not.toHaveBeenCalled() + + switchTo('s2') + expect(controller.openTab).toHaveBeenCalledTimes(1) + expect(controller.openTab).toHaveBeenCalledWith('ego-browser:watch', { params: {}, revealIfOpened: true }) + surface.dispose() + }) + + it('replays a queued tab open exactly once', () => { + const { surface, controller, switchTo } = mount('s1') + + surface.openTab({ sessionId: 's2', kind: 'files', params: {}, revealIfOpened: true }) + switchTo('s2') + switchTo('s2') + + expect(controller.openTab).toHaveBeenCalledTimes(1) + surface.dispose() + }) + + it('keeps a queued tab open across an unrelated session switch', () => { + const { surface, controller, switchTo } = mount('s1') + + surface.openTab({ sessionId: 's2', kind: 'files', params: {}, revealIfOpened: false }) + switchTo('s3') + expect(controller.openTab).not.toHaveBeenCalled() + + switchTo('s2') + expect(controller.openTab).toHaveBeenCalledTimes(1) + surface.dispose() + }) + + it('queues a resource open for an off-screen session instead of handing it to openResourceIn()', () => { + const { surface, controller, switchTo } = mount('s1') + + surface.openResource({ + sessionId: 's2', + address: 'dsh-resource://file/session/s2/a.ts', + line: 3, + revealIfOpened: true, + }) + + expect(controller.openResourceIn).not.toHaveBeenCalled() + expect(controller.openResource).not.toHaveBeenCalled() + + switchTo('s2') + expect(controller.openResource).toHaveBeenCalledTimes(1) + expect(controller.openResource).toHaveBeenCalledWith('dsh-resource://file/session/s2/a.ts', { + params: { line: 3 }, + revealIfOpened: true, + }) + surface.dispose() + }) + + it('still writes the on-screen session opens immediately', () => { + const { surface, controller } = mount('s1') + + surface.openTab({ sessionId: 's1', kind: 'browser', params: { url: 'https://example.com' }, revealIfOpened: true }) + surface.openResource({ sessionId: 's1', address: 'dsh-resource://file/session/s1/a.ts', revealIfOpened: true }) + + expect(controller.openTab).toHaveBeenCalledWith('browser', { + params: { url: 'https://example.com' }, + revealIfOpened: true, + }) + expect(controller.openResource).toHaveBeenCalledWith('dsh-resource://file/session/s1/a.ts', { + revealIfOpened: true, + }) + surface.dispose() + }) +})