Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

fix(worktree): 按 0.1.6 会话保留语义创建并切换工作树会话

hairyf519bc275fd75
diff --git a/packages/dsh-tauri-worktree/src/client/components/dialog.tsx b/packages/dsh-tauri-worktree/src/client/components/dialog.tsx
index 239d1dc8..3c81ee3e 100644
--- a/packages/dsh-tauri-worktree/src/client/components/dialog.tsx
+++ b/packages/dsh-tauri-worktree/src/client/components/dialog.tsx
@@ -1,10 +1,11 @@
import type { ReactElement } from 'react'
-import type { WorkspacesRuntime } from '../service/session-switch.types'
+import type { SessionsRuntime, WorkspacesRuntime } from '../service/session-switch.types'
import type { WorktreeDialogProps } from './dialog.types'
import { useMountStyle } from 'dsh-tauri-ui/client'
import { find, useEventListener } from 'dsh-tauri/client'
import { useRef } from 'react'
import { DIALOG_STYLE_ID } from '../constants'
+import { useCurrentSession } from '../hooks/use-current-session'
import { useDiscard } from '../hooks/use-discard'
import { useWaiter } from '../hooks/use-waiter'
import { useWorktreeSession } from '../hooks/use-worktree-session'
@@ -14,10 +15,10 @@ import { checkout } from '../service/worktree'
import { store } from '../store'
import dialogStyle from './dialog.cssr'
-export function WorktreeDialog({ useSessions, workspacesRuntime, sessionsRuntime }: WorktreeDialogProps): ReactElement | null {
+export function WorktreeDialog({ workspacesRuntime, sessionsRuntime }: WorktreeDialogProps): ReactElement | null {
locale.useLocale()
useMountStyle(dialogStyle, DIALOG_STYLE_ID)
- const sessionId = useSessions(state => state.current)
+ const sessionId = useCurrentSession(sessionsRuntime)
const state = useWorktreeSession(sessionId)
const discardWorktree = useDiscard(sessionId)
const checkoutOpen = state.checkoutOpen
@@ -72,11 +73,7 @@ function CheckoutDialog(props: {
branchName: string
error: string
workspacesRuntime: WorkspacesRuntime
- sessionsRuntime: {
- open: (sessionId: string) => void
- refresh: () => Promise<void>
- list: { getSnapshot: () => { current?: string, ids: string[] } }
- }
+ sessionsRuntime: SessionsRuntime
onCancel: () => void
}): ReactElement {
const { sessionId, worktreeKey, projectPath, workspacesRuntime, sessionsRuntime, onCancel } = props
diff --git a/packages/dsh-tauri-worktree/src/client/components/dialog.types.ts b/packages/dsh-tauri-worktree/src/client/components/dialog.types.ts
index 89a0927d..cd7ebef385 100644
--- a/packages/dsh-tauri-worktree/src/client/components/dialog.types.ts
+++ b/packages/dsh-tauri-worktree/src/client/components/dialog.types.ts
@@ -1,17 +1,6 @@
-import type { WorkspacesRuntime } from '../service/session-switch.types'
-
-export interface DialogListState {
- phase: string
- current?: string
- byId: Record<string, unknown>
-}
+import type { SessionsRuntime, WorkspacesRuntime } from '../service/session-switch.types'
export interface WorktreeDialogProps {
- useSessions: <S>(sel: (state: DialogListState) => S) => S
- sessionsRuntime: {
- open: (sessionId: string) => void
- refresh: () => Promise<void>
- list: { getSnapshot: () => { current?: string, ids: string[] } }
- }
+ sessionsRuntime: SessionsRuntime
workspacesRuntime: WorkspacesRuntime
}
diff --git a/packages/dsh-tauri-worktree/src/client/components/mode-select.tsx b/packages/dsh-tauri-worktree/src/client/components/mode-select.tsx
index 9639e36e..288b1735 100644
--- a/packages/dsh-tauri-worktree/src/client/components/mode-select.tsx
+++ b/packages/dsh-tauri-worktree/src/client/components/mode-select.tsx
@@ -98,6 +98,8 @@ function WorktreeModeControl({ sessionId, useInput, inputActions, sessionsRuntim
submittingRef.current = true
const targetSessionId = `session-${crypto.randomUUID()}`
store.worktree.patch(sessionId, { mode: 'pending', phase: 'creating', loadingLabel: locale.text('progressCreating'), error: '' })
+ let switched = false
+ let detached = false
try {
const created = await create({ sessionId: targetSessionId, sourceSessionId: sessionId, inherit: true })
if (!created.ok || !created.result)
@@ -113,14 +115,18 @@ function WorktreeModeControl({ sessionId, useInput, inputActions, sessionsRuntim
await sessionsRuntime.create({ cwd: worktreePath, sessionId: targetSessionId })
}
await attach({ sessionId: targetSessionId })
+ // 0.1.6 起会话输入面只在被保留的会话上物化:先切换(切换即保留主视图)再取目标输入面。
+ // 源会话的草稿与附件必须先摘除——源作用域随切换销毁,会把仍挂在其草稿上的附件从注册表释放。
+ inputActions.setDraft('')
+ forEach(imageIds, imageId => removeDraftAttachment(inputActions, imageId))
+ detached = true
+ sessionsRuntime.open(targetSessionId)
+ switched = true
const nextActions = await waitForInputActions({ sessions: sessionsRuntime, sessionId: targetSessionId, wait })
nextActions.setDraft(draft)
if (!addDraftAttachments(nextActions, imageIds))
throw new Error('无法迁移消息附件到工作树会话')
- inputActions.setDraft('')
- forEach(imageIds, imageId => removeDraftAttachment(inputActions, imageId))
store.worktree.patch(sessionId, { mode: 'local', phase: 'idle', loadingLabel: '' })
- sessionsRuntime.open(targetSessionId)
queueMicrotask(() => {
try {
nextActions.submit()
@@ -134,12 +140,27 @@ function WorktreeModeControl({ sessionId, useInput, inputActions, sessionsRuntim
await workspacesRuntime.archiveSession(sessionId).catch(() => {})
}
catch (error) {
+ const message = get(error, 'message', String(error))
+ // 切换失败的源会话仍在:已摘除的草稿与附件原样放回,用户不丢内容。
+ if (detached && !switched) {
+ inputActions.setDraft(draft)
+ addDraftAttachments(inputActions, imageIds)
+ }
store.worktree.patch(sessionId, {
mode: 'pending',
phase: 'error',
loadingLabel: '',
- error: get(error, 'message', String(error)),
+ error: message,
})
+ // 切换成功后源会话的提示条不再渲染:错误同步到当前会话,避免静默失败。
+ if (switched) {
+ store.worktree.patch(targetSessionId, {
+ mode: 'pending',
+ phase: 'error',
+ loadingLabel: '',
+ error: message,
+ })
+ }
}
finally {
submittingRef.current = false
diff --git a/packages/dsh-tauri-worktree/src/client/hooks/use-current-session.ts b/packages/dsh-tauri-worktree/src/client/hooks/use-current-session.ts
new file mode 100644
index 00000000..a959327a
--- /dev/null
+++ b/packages/dsh-tauri-worktree/src/client/hooks/use-current-session.ts
@@ -0,0 +1,9 @@
+import type { SessionsRuntime } from '../service/session-switch.types'
+import { useCallback, useSyncExternalStore } from 'react'
+
+/** 订阅适配层的会话列表投影,读出当前选中会话(0.1.6 起由适配层把 `current` 投影回列表快照)。 */
+export function useCurrentSession(sessions: Pick<SessionsRuntime, 'list'>): string | undefined {
+ const subscribe = useCallback((listener: () => void) => sessions.list.subscribe(listener), [sessions])
+ const getSnapshot = useCallback(() => sessions.list.getSnapshot().current, [sessions])
+ return useSyncExternalStore(subscribe, getSnapshot)
+}
diff --git a/packages/dsh-tauri-worktree/src/client/service/session-switch.test.ts b/packages/dsh-tauri-worktree/src/client/service/session-switch.test.ts
new file mode 100644
index 00000000..69d83bf2
--- /dev/null
+++ b/packages/dsh-tauri-worktree/src/client/service/session-switch.test.ts
@@ -0,0 +1,41 @@
+import type { InputActions, InputSessions } from './session-switch.types'
+import { describe, expect, it, vi } from 'vitest'
+import { waitForInputActions } from './session-switch'
+
+function makeSessions(provideInfo?: InputSessions['provideInfo']): InputSessions {
+ return {
+ refresh: async () => {},
+ list: { getSnapshot: () => ({ ids: [] }) },
+ ...(provideInfo === undefined ? {} : { provideInfo }),
+ }
+}
+
+async function wait(): Promise<void> {}
+
+describe('waitForInputActions', () => {
+ it('目标会话输入面就绪即返回', async () => {
+ const actions: InputActions = { setDraft: vi.fn(), submit: vi.fn() }
+ const provideInfo = vi.fn(() => ({ props: { inputActions: actions } }))
+
+ await expect(waitForInputActions({ sessions: makeSessions(provideInfo), sessionId: 's1', wait })).resolves.toBe(actions)
+ expect(provideInfo).toHaveBeenCalledWith('s1')
+ })
+
+ it('核心不提供 per-session 输入面时明确报「未就绪」,而不是 TypeError', async () => {
+ await expect(waitForInputActions({ sessions: makeSessions(), sessionId: 's1', wait, attempts: 2 }))
+ .rejects
+ .toThrow('新工作树会话的输入服务尚未就绪')
+ })
+
+ it('输入面尚未物化时按重试次数轮询', async () => {
+ const actions: InputActions = { setDraft: vi.fn(), submit: vi.fn() }
+ let ready = false
+ const provideInfo = vi.fn(() => (ready ? { props: { inputActions: actions } } : undefined))
+
+ const pending = waitForInputActions({ sessions: makeSessions(provideInfo), sessionId: 's1', wait, attempts: 3 })
+ ready = true
+
+ await expect(pending).resolves.toBe(actions)
+ expect(provideInfo).toHaveBeenCalledTimes(2)
+ })
+})
diff --git a/packages/dsh-tauri-worktree/src/client/service/session-switch.ts b/packages/dsh-tauri-worktree/src/client/service/session-switch.ts
index 5d7fb19b..cc80b02c 100644
--- a/packages/dsh-tauri-worktree/src/client/service/session-switch.ts
+++ b/packages/dsh-tauri-worktree/src/client/service/session-switch.ts
@@ -33,7 +33,7 @@ export async function waitForInputActions(input: InputWaitInput): Promise<InputA
const attempts = input.attempts ?? SESSION_SWITCH_MAX_ATTEMPTS
const delayMs = input.delayMs ?? SESSION_SWITCH_RETRY_DELAY_MS
for (let attempt = 0; attempt < attempts; attempt++) {
- const actions = input.sessions.provideInfo(input.sessionId)?.props?.inputActions
+ const actions = input.sessions.provideInfo?.(input.sessionId)?.props?.inputActions
if (actions)
return actions
await input.wait(delayMs)
diff --git a/packages/dsh-tauri-worktree/src/client/service/session-switch.types.ts b/packages/dsh-tauri-worktree/src/client/service/session-switch.types.ts
index 8ef609d0..676f6b5d 100644
--- a/packages/dsh-tauri-worktree/src/client/service/session-switch.types.ts
+++ b/packages/dsh-tauri-worktree/src/client/service/session-switch.types.ts
@@ -13,12 +13,20 @@ export interface InputActions {
submit: () => void
}
+export interface SessionListSnapshot {
+ ids: string[]
+ current?: string
+}
+
export interface SessionsRuntime {
create: (opts: { cwd: string, sessionId: string }) => Promise<string>
open: (sessionId: string) => void
- provideInfo: (sessionId: string) => { props?: { inputActions?: InputActions } } | undefined
+ provideInfo?: (sessionId: string) => { props?: { inputActions?: InputActions } } | undefined
refresh: () => Promise<void>
- list: { getSnapshot: () => { ids: string[], current?: string } }
+ list: {
+ getSnapshot: () => SessionListSnapshot
+ subscribe: (listener: () => void) => () => void
+ }
}
export interface WorkspacesRuntime {
@@ -39,15 +47,15 @@ export type SwitchOutcome = 'switched' | 'aborted' | 'retry'
export interface ListSessions {
refresh: () => Promise<void>
- list: { getSnapshot: () => { ids: string[] } }
+ list: { getSnapshot: () => SessionListSnapshot }
}
export interface InputSessions extends ListSessions {
- provideInfo: (sessionId: string) => { props?: { inputActions?: InputActions } } | undefined
+ provideInfo?: (sessionId: string) => { props?: { inputActions?: InputActions } } | undefined
}
export interface SwitchSessions {
open: (sessionId: string) => void
refresh: () => Promise<void>
- list: { getSnapshot: () => { ids: string[], current?: string } }
+ list: { getSnapshot: () => SessionListSnapshot }
}