DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
fix(dsh-tauri): 适配层惰性解析 uiSession,修复 0.1.6 上会话续跑失效
hairyf3a9068cd2bba
diff --git a/packages/dsh-tauri/src/client/register/index.adapter.test.ts b/packages/dsh-tauri/src/client/register/index.adapter.test.tsindex aea08c6c..b12a52b1 100644--- a/packages/dsh-tauri/src/client/register/index.adapter.test.ts+++ b/packages/dsh-tauri/src/client/register/index.adapter.test.ts@@ -126,11 +126,13 @@ describe('defineAdapter — 世代探测与内置迁移', () => {expect(adapter.migrations).toContain('sessions:provide-info-bridge')expect(adapter.migrations).toContain('legacy:workspaces-navigation')- // 嵌套投影补齐成与新版同形的 getSnapshot / subscribe+ // 嵌套投影补齐成与新版同形的 getSnapshot / subscribe(订阅转发:核心列表发布即通知消费方)expect(adapter.sessions.getSnapshot?.()).toEqual(snapshot)- const listener = () => {}+ const listener = vi.fn()adapter.sessions.subscribe?.(listener)- expect(list.subscribe).toHaveBeenCalledWith(listener)+ expect(list.subscribe).toHaveBeenCalledTimes(1)+ list.subscribe.mock.calls[0]?.[0]?.()+ expect(listener).toHaveBeenCalledTimes(1)// per-session 信息:legacy 走 binding + uiSession.adapter.resolve 投影expect(adapter.has('sessions.provideInfo')).toBe(true)@@ -512,6 +514,56 @@ describe('defineAdapter — 0.1.6-alpha.2 会话面投影', () => {expect(adapter.sessions.getSnapshot?.()).toEqual({ ids: ['s1', 's2'], byId: {}, current: 's3' })})+ it('current 投影:创建期 uiSession 尚未激活,服务到达后仍补齐 current', () => {+ const list = makeLiveList<Record<string, unknown>>({ ids: ['s1'], byId: {} })+ const current = makeLiveList<{ key?: string }>({ key: undefined })+ // `dsh-tauri-ui` 只声明 sessions,会早于 ui-session 激活:创建期读不到 uiSession 是常态+ const services: Record<string, unknown> = { sessions: { list } }+ const adapter = defineAdapter(makeContext(services))++ expect(adapter.migrations).toContain('sessions:current-projection')+ const projected = adapter.sessions.list+ const currentOf = (): unknown => (projected?.getSnapshot() as { current?: unknown } | undefined)?.current+ expect(currentOf()).toBeUndefined()++ // ui-session 稍后激活:投影在调用期重读服务,无需重建适配层+ services.uiSession = { adapter: { bindingSource: vi.fn(), current } }+ current.publish({ key: 's1' })+ expect(currentOf()).toBe('s1')++ // 订阅侧同样自愈:源到达前订阅,到达后 current 变化仍要通知消费方+ const listener = vi.fn()+ const off = projected?.subscribe(listener)+ list.publish({ ids: ['s1', 's2'], byId: {} })+ expect(listener).toHaveBeenCalledTimes(1)+ current.publish({ key: 's2' })+ expect(listener).toHaveBeenCalledTimes(2)+ expect(currentOf()).toBe('s2')+ off?.()+ current.publish({ key: 's3' })+ expect(listener).toHaveBeenCalledTimes(2)+ })++ it('provideInfo:创建期 uiSession 尚未激活,服务到达后桥照常解析', () => {+ const inputActions = { submit: vi.fn() }+ const binding = { sessionId: 's1' }+ const source = makeLiveList({ key: 's1', props: { inputActions } })+ const services: Record<string, unknown> = {+ sessions: { list: makeList(), binding: vi.fn(() => binding) },+ }+ const adapter = defineAdapter(makeContext(services))++ expect(adapter.migrations).toContain('sessions:provide-info-bridge')+ expect(adapter.has('sessions.provideInfo')).toBe(true)+ // 投影源未到达:诚实回报不可用,而不是抛错+ expect(adapter.sessions.provideInfo?.('s1')).toBeUndefined()++ const bindingSource = vi.fn(() => source)+ services.uiSession = { adapter: { bindingSource, current: makeLiveList({ key: 's1' }) } }+ expect(adapter.sessions.provideInfo?.('s1')).toEqual({ props: { inputActions } })+ expect(bindingSource).toHaveBeenCalledWith({ sessionId: 's1', binding })+ })+it('current 投影:0.1.5 布局(核心自带 current)不装投影,原样保留', () => {const adapter = defineAdapter(makeContext({sessions: { list: makeList({ ids: ['s1'], current: 's1' }) },diff --git a/packages/dsh-tauri/src/client/register/index.adapter.ts b/packages/dsh-tauri/src/client/register/index.adapter.tsindex dd6c6f33..3295bf72 100644--- a/packages/dsh-tauri/src/client/register/index.adapter.ts+++ b/packages/dsh-tauri/src/client/register/index.adapter.ts@@ -143,6 +143,17 @@ function readInputActions(projected: unknown): unknown {return inputActions === undefined ? undefined : { props: { inputActions } }}+/**+ * 惰性读取 `uiSession.adapter`。+ *+ * 客户端服务按激活顺序到达:`dsh-tauri-ui` 只声明 `sessions`,会在 `ui-session`+ * (额外等 `remote`,由 `dsh-client-connection` 稍后提供)之前激活,因此**适配层创建期+ * 读到的 `uiSession` 可能缺席**。依赖它的投影一律在调用期重读,不缓存创建期快照。+ */+function uiSessionAdapter(probe: AdapterProbe): AdapterRuntimeObject | undefined {+ return probe.service<AdapterRuntimeObject>('uiSession')?.adapter as AdapterRuntimeObject | undefined+}+/*** 核心不暴露 per-session 的 `provideInfo`:官方能力优先,缺失时借已保留会话的 binding 补齐。*@@ -155,7 +166,7 @@ function readInputActions(projected: unknown): unknown {function resolveProvideInfo(sessions: AdapterRuntimeObject,sessionId: string,- uiSession: AdapterRuntimeObject | undefined,+ probe: AdapterProbe,): unknown {// 部分桌面运行时即使在 legacy 布局下也提供了完整投影:原生路径优先。const native = sessions.provideInfo@@ -175,7 +186,7 @@ function resolveProvideInfo(if (binding === undefined)return undefined- const adapter = uiSession?.adapter as AdapterRuntimeObject | undefined+ const adapter = uiSessionAdapter(probe)// legacy:`uiSession.adapter.resolve(id)` 直接按 id 物化标准面。const resolveFn = adapter?.resolve@@ -277,13 +288,21 @@ const SESSIONS_LIST_PROJECTION_MIGRATION: DshMigration = {* 缺席时用 `uiSession.adapter.current` 的 binding key 补齐。** 快照按(核心快照引用,current)缓存:uSES 的 `getSnapshot` 必须引用稳定,否则会无限重渲染。+ * `current` 源按调用期解析(见 {@link uiSessionAdapter}):`ui-session` 可能晚于本适配层创建,+ * 订阅侧因此在每次列表发布时重挂,源一出现就接上。*/-function projectListCurrent(list: AdapterListProjection, current: AdapterListProjection): AdapterListProjection {+function projectListCurrent(+ list: AdapterListProjection,+ resolveCurrent: () => AdapterListProjection | undefined,+): AdapterListProjection {let lastSnapshot: unknownlet lastCurrent: unknownlet lastProjected: unknownconst currentId = (): unknown => {- const value = current.getSnapshot()+ const source = resolveCurrent()+ if (source === undefined)+ return undefined+ const value = source.getSnapshot()return value !== null && typeof value === 'object' ? (value as { key?: unknown }).key : undefined}return {@@ -299,32 +318,48 @@ function projectListCurrent(list: AdapterListProjection, current: AdapterListProreturn lastProjected},subscribe: (listener) => {- const offList = list.subscribe(listener)- const offCurrent = typeof current.subscribe === 'function' ? current.subscribe(listener) : undefined+ let offCurrent: (() => void) | undefined+ let boundCurrent: AdapterListProjection | undefined+ const followCurrent = (): void => {+ const source = resolveCurrent()+ if (source === boundCurrent)+ return+ offCurrent?.()+ boundCurrent = source+ offCurrent = typeof source?.subscribe === 'function' ? source.subscribe(listener) : undefined+ }+ const offList = list.subscribe(() => {+ followCurrent()+ listener()+ })+ followCurrent()return () => {- offList?.()+ offList()offCurrent?.()}},}}+/** 核心列表快照是否自带 `current`(`0.1.5` 及更早自带,`0.1.6-alpha.2` 起交给 `uiSession`)。 */+function ownsCurrent(list: AdapterListProjection): boolean {+ const snapshot = list.getSnapshot()+ return snapshot !== null && typeof snapshot === 'object' && 'current' in snapshot+}+const SESSIONS_CURRENT_PROJECTION_MIGRATION: DshMigration = {id: 'sessions:current-projection',description: 'uiSession.adapter.current → sessions.list 快照的 current 投影',detect: (_probe, surface) => {- if (surface.sessions?.list === undefined)- return false- const adapter = surface.uiSession?.adapter as AdapterRuntimeObject | undefined- return typeof adapter?.bindingSource === 'function'+ const list = surface.sessions?.list+ return list !== undefined && !ownsCurrent(list)},- apply(surface) {+ apply(surface, probe) {const sessions = surface.sessionsconst list = sessions?.list as AdapterListProjection | undefined- const current = (surface.uiSession?.adapter as AdapterRuntimeObject | undefined)?.current as AdapterListProjection | undefined- if (sessions === undefined || list === undefined || current === undefined || typeof current.getSnapshot !== 'function')+ if (sessions === undefined || list === undefined)return- const projected = projectListCurrent(list, current)+ const projected = projectListCurrent(list, () => currentSourceOf(probe))surface.sessions = new Proxy(sessions as object, {get(target, prop) {if (prop === 'list')@@ -340,33 +375,33 @@ const SESSIONS_CURRENT_PROJECTION_MIGRATION: DshMigration = {},}+/** `uiSession.adapter.current` 投影源;缺席(服务未到达或核心不自带该源)时返回 undefined。 */+function currentSourceOf(probe: AdapterProbe): AdapterListProjection | undefined {+ const current = uiSessionAdapter(probe)?.current as AdapterListProjection | undefined+ return current !== undefined && typeof current.getSnapshot === 'function' ? current : undefined+}+/*** 核心不暴露 per-session 的 `provideInfo`:用 `sessions.binding(id)` +* `uiSession.adapter`(legacy 的 `resolve`,0.1.6 起的 `bindingSource`)补齐* (消费方如 worktree 依赖它取 inputActions)。*- * 探测很严:`binding` 与两种投影之一缺一就不安装桥——宁可在- * `has('sessions.provideInfo')` 上诚实报 false,也不放一个永远返回 undefined 的函数。- * 原生 `provideInfo` 在场也照样装桥:它可能只覆盖部分会话,`resolveProvideInfo` 会原生优先、- * 缺失时回退投影。+ * 探测只认 `binding`:`uiSession` 由投影在调用期解析,因此晚到达的 `ui-session` 不再让桥缺席;+ * 会话未被保留(`binding` 缺席)时仍诚实回报 undefined。原生 `provideInfo` 在场也照样装桥:+ * 它可能只覆盖部分会话,`resolveProvideInfo` 会原生优先、缺失时回退投影。*/const SESSIONS_PROVIDE_INFO_MIGRATION: DshMigration = {id: 'sessions:provide-info-bridge',description: 'sessions.binding + uiSession.adapter.resolve|bindingSource → sessions.provideInfo 兼容桥',- detect: (_probe, surface) => {- if (typeof surface.sessions?.binding !== 'function')- return false- const adapter = surface.uiSession?.adapter as AdapterRuntimeObject | undefined- return typeof adapter?.resolve === 'function' || typeof adapter?.bindingSource === 'function'- },- apply(surface) {+ detect: (_probe, surface) => typeof surface.sessions?.binding === 'function',+ apply(surface, probe) {const sessions = surface.sessionsif (sessions === undefined)returnsurface.sessions = new Proxy(sessions as object, {get(target, prop) {if (prop === 'provideInfo')- return (sessionId: string) => resolveProvideInfo(target as AdapterRuntimeObject, sessionId, surface.uiSession)+ return (sessionId: string) => resolveProvideInfo(target as AdapterRuntimeObject, sessionId, probe)const member = Reflect.get(target, prop)return typeof member === 'function' ? member.bind(target) : member},@@ -662,7 +697,10 @@ export function defineAdapter(ctx: unknown, options: DefineAdapterOptions = {}):sessions: service<AdapterSessions>('sessions') ?? {},workspaces: service<AdapterWorkspaces>('workspaces') ?? {},uiWorkspace: service<AdapterRuntimeObject>('uiWorkspace'),- uiSession: service<AdapterRuntimeObject>('uiSession'),+ // 取值为 live:`ui-session` 晚于本适配层激活,缓存创建期快照会让依赖它的迁移永久缺席。+ get uiSession() {+ return service<AdapterRuntimeObject>('uiSession')+ },}const migrations: string[] = []