Skip to content

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

fix(worktree): 附件迁移失败时回滚源会话,避免草稿与附件丢失

hairyfa41f8ef176c5
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 288b1735..897406b0 100644
--- a/packages/dsh-tauri-worktree/src/client/components/mode-select.tsx
+++ b/packages/dsh-tauri-worktree/src/client/components/mode-select.tsx
@@ -1,4 +1,5 @@
import type { ReactElement } from 'react'
+import type { InputActions } from '../service/session-switch.types'
import type { ModeSelectProps } from './mode-select.types'
import { IconChevronDownOutline14 as ChevronDown, Menu } from '@deepseek-ai/dsh-client-ui-primitives'
import { CircleTree, Icon } from 'dsh-tauri-ui/client'
@@ -18,7 +19,7 @@ import { locale } from '../locales'
import { waitForInputActions, waitForSessionListed } from '../service/session-switch'
import { attach, create } from '../service/worktree'
import { store } from '../store'
-import { addDraftAttachments, draftAttachmentIds, removeDraftAttachment, resolveAccessModeGroup } from './mode-select.utils'
+import { addDraftAttachments, canAddDraftAttachments, draftAttachmentIds, removeDraftAttachment, resolveAccessModeGroup } from './mode-select.utils'
export function WorktreeModeSelect(props: ModeSelectProps): ReactElement {
const { sessionId } = props
@@ -100,6 +101,7 @@ function WorktreeModeControl({ sessionId, useInput, inputActions, sessionsRuntim
store.worktree.patch(sessionId, { mode: 'pending', phase: 'creating', loadingLabel: locale.text('progressCreating'), error: '' })
let switched = false
let detached = false
+ let nextActions: InputActions | undefined
try {
const created = await create({ sessionId: targetSessionId, sourceSessionId: sessionId, inherit: true })
if (!created.ok || !created.result)
@@ -115,6 +117,10 @@ function WorktreeModeControl({ sessionId, useInput, inputActions, sessionsRuntim
await sessionsRuntime.create({ cwd: worktreePath, sessionId: targetSessionId })
}
await attach({ sessionId: targetSessionId })
+ // 目标输入面只在切换后才物化,而源附件一旦摘除、源作用域随切换销毁就再也回不去:
+ // 先用源输入面探测该核心的附件面是否可写,不可写就整体中止(此时草稿与附件都还没动)。
+ if (imageIds.length > 0 && !canAddDraftAttachments(inputActions))
+ throw new Error('无法迁移消息附件到工作树会话')
// 0.1.6 起会话输入面只在被保留的会话上物化:先切换(切换即保留主视图)再取目标输入面。
// 源会话的草稿与附件必须先摘除——源作用域随切换销毁,会把仍挂在其草稿上的附件从注册表释放。
inputActions.setDraft('')
@@ -122,18 +128,19 @@ function WorktreeModeControl({ sessionId, useInput, inputActions, sessionsRuntim
detached = true
sessionsRuntime.open(targetSessionId)
switched = true
- const nextActions = await waitForInputActions({ sessions: sessionsRuntime, sessionId: targetSessionId, wait })
- nextActions.setDraft(draft)
- if (!addDraftAttachments(nextActions, imageIds))
+ const actions = await waitForInputActions({ sessions: sessionsRuntime, sessionId: targetSessionId, wait })
+ nextActions = actions
+ actions.setDraft(draft)
+ if (!addDraftAttachments(actions, imageIds))
throw new Error('无法迁移消息附件到工作树会话')
store.worktree.patch(sessionId, { mode: 'local', phase: 'idle', loadingLabel: '' })
queueMicrotask(() => {
try {
- nextActions.submit()
+ actions.submit()
}
finally {
- nextActions.setDraft('')
- forEach(imageIds, imageId => removeDraftAttachment(nextActions, imageId))
+ actions.setDraft('')
+ forEach(imageIds, imageId => removeDraftAttachment(actions, imageId))
}
})
if (result.inherited)
@@ -141,8 +148,23 @@ function WorktreeModeControl({ sessionId, useInput, inputActions, sessionsRuntim
}
catch (error) {
const message = get(error, 'message', String(error))
- // 切换失败的源会话仍在:已摘除的草稿与附件原样放回,用户不丢内容。
- if (detached && !switched) {
+ // 切换前的失败源会话仍在:把已摘除的草稿与附件放回即可。
+ // 切换后的失败无法就地恢复:切回源会话,用它重新物化的输入面放回内容,避免用户丢内容。
+ let restored = !switched
+ if (detached && switched) {
+ nextActions?.setDraft('')
+ try {
+ sessionsRuntime.open(sessionId)
+ const sourceActions = await waitForInputActions({ sessions: sessionsRuntime, sessionId, wait })
+ sourceActions.setDraft(draft)
+ addDraftAttachments(sourceActions, imageIds)
+ restored = true
+ }
+ catch {
+ restored = false
+ }
+ }
+ else if (detached) {
inputActions.setDraft(draft)
addDraftAttachments(inputActions, imageIds)
}
@@ -152,8 +174,8 @@ function WorktreeModeControl({ sessionId, useInput, inputActions, sessionsRuntim
loadingLabel: '',
error: message,
})
- // 切换成功后源会话的提示条不再渲染:错误同步到当前会话,避免静默失败。
- if (switched) {
+ // 没能切回源会话时源提示条不再渲染:错误同步到当前会话,避免静默失败。
+ if (!restored) {
store.worktree.patch(targetSessionId, {
mode: 'pending',
phase: 'error',
diff --git a/packages/dsh-tauri-worktree/src/client/components/mode-select.utils.test.ts b/packages/dsh-tauri-worktree/src/client/components/mode-select.utils.test.ts
index b91cd1c4..d6e6d3f7 100644
--- a/packages/dsh-tauri-worktree/src/client/components/mode-select.utils.test.ts
+++ b/packages/dsh-tauri-worktree/src/client/components/mode-select.utils.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, it, vi } from 'vitest'
import {
addDraftAttachments,
+ canAddDraftAttachments,
draftAttachmentIds,
NO_DRAFT_ATTACHMENTS,
removeDraftAttachment,
@@ -47,6 +48,15 @@ describe('addDraftAttachments', () => {
})
})
+describe('canAddDraftAttachments', () => {
+ it('识别两代可写的附件面,缺一面即不可写', () => {
+ expect(canAddDraftAttachments({ setDraft: vi.fn(), submit: vi.fn(), addAttachments: vi.fn() })).toBe(true)
+ expect(canAddDraftAttachments({ setDraft: vi.fn(), submit: vi.fn(), addImages: vi.fn() })).toBe(true)
+ expect(canAddDraftAttachments({ setDraft: vi.fn(), submit: vi.fn() })).toBe(false)
+ expect(canAddDraftAttachments(undefined)).toBe(false)
+ })
+})
+
describe('removeDraftAttachment', () => {
it('prefers removeAttachment over the legacy removeImage', () => {
const removeAttachment = vi.fn()
diff --git a/packages/dsh-tauri-worktree/src/client/components/mode-select.utils.ts b/packages/dsh-tauri-worktree/src/client/components/mode-select.utils.ts
index 8acbed08..e83ef4c7 100644
--- a/packages/dsh-tauri-worktree/src/client/components/mode-select.utils.ts
+++ b/packages/dsh-tauri-worktree/src/client/components/mode-select.utils.ts
@@ -6,6 +6,10 @@ export function draftAttachmentIds(state: InputState | undefined): readonly stri
return state?.attachmentIds ?? state?.imageIds ?? NO_DRAFT_ATTACHMENTS
}
+export function canAddDraftAttachments(actions: InputActions | undefined): boolean {
+ return typeof actions?.addAttachments === 'function' || typeof actions?.addImages === 'function'
+}
+
export function addDraftAttachments(actions: InputActions | undefined, ids: readonly string[]): boolean {
if (ids.length === 0)
return true