Skip to content

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

fix(desktop): 核心切换失败时回滚档案,避免旧核心配新档案

hairyfe24ab5fb1daf
diff --git a/src/ui/config/core.tsx b/src/ui/config/core.tsx
index 37a6dfe0..f54e63ce 100644
--- a/src/ui/config/core.tsx
+++ b/src/ui/config/core.tsx
@@ -144,22 +144,39 @@ export function ConfigCore() {
*
* 档案名按后端归一化后的 id 匹配(`0.17` → `017`),因此这里先取列表再决定是否新建,
* 不能直接 create——已存在时后端会以 `PROFILE_EXISTS` 拒绝。
+ *
+ * 返回 `previousId`(切换前使用中的档案)供核心切换失败时回滚:档案与核心是两次独立的
+ * 写盘,核心没切成不能把用户留在「旧核心 + 新档案」的组合上。
*/
- async function activateVersionProfile(name: string): Promise<string> {
+ async function activateVersionProfile(name: string): Promise<{ id: string, previousId: string }> {
const profiles = await queryClient.fetchQuery({
queryKey: queryKeys.profiles,
queryFn: () => invoke<Profile[]>('get_profiles'),
})
+ const previousId = profiles.find(p => p.active)?.id ?? ''
const id = normalizeProfileId(name)
const existing = profiles.find(p => p.id === id)
if (existing) {
if (!existing.active)
await invoke<Profile>('set_active_profile', { id })
- return id
+ return { id, previousId }
}
const created = await invoke<Profile>('create_profile', { name })
await invoke<Profile>('set_active_profile', { id: created.id })
- return created.id
+ return { id: created.id, previousId }
+ }
+
+ /** 回滚档案:核心切换失败时切回原档案;回滚失败只记日志,不盖住原始失败提示 */
+ async function restoreActiveProfile(id: string) {
+ try {
+ await invoke<Profile>('set_active_profile', { id })
+ }
+ catch (err) {
+ console.error('[ConfigCore] restore active profile failed:', err)
+ }
+ finally {
+ void queryClient.invalidateQueries({ queryKey: queryKeys.profiles })
+ }
}
async function onActivate(core: HarnessCore) {
@@ -224,9 +241,13 @@ export function ConfigCore() {
}
}
// 先落档案再切核心:两者都由同一次重启生效,顺序颠倒会让新核心先于配套档案运行。
+ let previousProfileId = ''
+ let switchedProfileId = ''
if (profileName) {
try {
- await activateVersionProfile(profileName)
+ const activated = await activateVersionProfile(profileName)
+ previousProfileId = activated.previousId
+ switchedProfileId = activated.id
void queryClient.invalidateQueries({ queryKey: queryKeys.profiles })
}
catch (err) {
@@ -257,6 +278,10 @@ export function ConfigCore() {
catch (err) {
console.error('[ConfigCore] switch failed:', err)
toast(t('core.switch_failed'), {})
+ // 核心没切成就把档案切回去:否则下次启动会跑在「旧核心 + 新档案」上,
+ // 用户会看到自己的插件与设置「凭空消失」。
+ if (previousProfileId && previousProfileId !== switchedProfileId)
+ await restoreActiveProfile(previousProfileId)
}
}
diff --git a/test/core-upgrade-profile.test.ts b/test/core-upgrade-profile.test.ts
index 0789a873..a04aa851 100644
--- a/test/core-upgrade-profile.test.ts
+++ b/test/core-upgrade-profile.test.ts
@@ -90,6 +90,17 @@ describe('确认后的编排顺序:档案 → 核心 → 重启', () => {
expect(failureBranch).toContain('core.breaking_profile_failed')
expect(failureBranch).toMatch(/return/)
})
+
+ it('核心切换失败时回滚到切换前的档案', () => {
+ const body = bodyOf(coreSource(), 'async function onActivate')
+ const failureIndex = body.indexOf('core.switch_failed')
+ expect(failureIndex).toBeGreaterThan(-1)
+
+ const failureBranch = body.slice(body.lastIndexOf('catch', failureIndex))
+ expect(failureBranch).toMatch(/await restoreActiveProfile\(previousProfileId\)/)
+ // 档案本来就没换(目标档案已使用中)时不做无意义的回滚
+ expect(failureBranch).toContain('previousProfileId !== switchedProfileId')
+ })
})
describe('activateVersionProfile:缺失才新建,已存在只切换', () => {
@@ -104,7 +115,7 @@ describe('activateVersionProfile:缺失才新建,已存在只切换', () =>
it('已存在时切到该档案并直接返回,不再新建', () => {
const body = bodyOf(coreSource(), 'async function activateVersionProfile')
const existsIndex = body.indexOf('if (existing)')
- const returnIndex = body.indexOf('return id', existsIndex)
+ const returnIndex = body.indexOf('return { id, previousId }', existsIndex)
const createIndex = body.indexOf('\'create_profile\'')
expect(existsIndex).toBeGreaterThan(-1)
@@ -119,6 +130,25 @@ describe('activateVersionProfile:缺失才新建,已存在只切换', () =>
expect(body).toContain('invoke<Profile>(\'create_profile\', { name })')
expect(body).toContain('invoke<Profile>(\'set_active_profile\', { id: created.id })')
})
+
+ it('一并返回切换前的在用档案,供核心失败时回滚', () => {
+ const body = bodyOf(coreSource(), 'async function activateVersionProfile')
+
+ expect(body).toContain('profiles.find(p => p.active)?.id')
+ expect(body).toMatch(/Promise<\{ id: string, previousId: string \}>/)
+ expect(body).toMatch(/return \{ id: created\.id, previousId \}/)
+ })
+})
+
+describe('restoreActiveProfile:回滚不掩盖原始失败', () => {
+ it('切回原档案,回滚失败只记日志,并始终失效档案查询', () => {
+ const body = bodyOf(coreSource(), 'async function restoreActiveProfile')
+
+ expect(body).toContain('invoke<Profile>(\'set_active_profile\', { id })')
+ expect(body).toMatch(/console\.error/)
+ expect(body).toMatch(/finally \{/)
+ expect(body).toContain('queryKeys.profiles')
+ })
})
describe('警告对话框控件', () => {