Skip to content

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

fix(shell): 远端新窗口启动寻址挂起化——实例未就绪不再落空回本地

Menghuan191842f66c1aaf32
diff --git a/src/store/modules/remote/store.test.ts b/src/store/modules/remote/store.test.ts
index 3aac2257..6e75547a 100644
--- a/src/store/modules/remote/store.test.ts
+++ b/src/store/modules/remote/store.test.ts
@@ -82,13 +82,30 @@ describe('remote store 轮询与降级', () => {
})
describe('remote store 远端弹窗启动寻址', () => {
- it('openInitialMachine:列表为空先拉一轮,已连接机器直接切换', async () => {
+ it('openInitialMachine:登记挂起并拉一轮,已连接机器直接切换', async () => {
const engine = bindEngine({ list: [[machineOf({ id: 'm1', state: 'connected', tunnelBaseUrl: 'http://127.0.0.1:4001' })]] })
expect(remote.machines).toHaveLength(0)
- await remote.openInitialMachine('m1')
+ remote.openInitialMachine('m1')
+ expect(remote.pendingBootMachineId).toBe('m1')
+ await vi.waitFor(() => expect(remote.activeTunnelUrl).toBe('http://127.0.0.1:4001'))
expect(engine.listMachines).toHaveBeenCalled()
expect(remote.activeId).toBe('m1')
- expect(remote.activeTunnelUrl).toBe('http://127.0.0.1:4001')
+ expect(remote.pendingBootMachineId).toBeNull()
+ })
+
+ it('openInitialMachine:实例未就绪时不落空——后续成功轮询补切换', async () => {
+ // 首轮不可达(新窗口启动时实例健康检查常未就绪),恢复后轮询推进
+ bindEngine({ list: [] })
+ const engine = bindEngine({ list: [] })
+ engine.listMachines.mockRejectedValueOnce(new Error('SSH_API_HTTP_503'))
+ engine.listMachines.mockImplementation(async () =>
+ [machineOf({ id: 'm1', state: 'connected', tunnelBaseUrl: 'http://127.0.0.1:4001' })])
+ remote.openInitialMachine('m1')
+ await vi.waitFor(() => expect(remote.available).toBe(false))
+ expect(remote.activeId).toBeNull()
+ await remote.refresh()
+ await vi.waitFor(() => expect(remote.activeTunnelUrl).toBe('http://127.0.0.1:4001'))
+ expect(remote.activeId).toBe('m1')
})
it('openInitialMachine:未连接机器走标准连接流程(进度弹窗照常)', async () => {
@@ -105,17 +122,22 @@ describe('remote store 远端弹窗启动寻址', () => {
[machineOf(connected
? { id: 'm1', state: 'connected', tunnelBaseUrl: 'http://127.0.0.1:4001' }
: { id: 'm1', state: 'disconnected' })])
- await remote.openInitialMachine('m1')
- expect(engine.connect).toHaveBeenCalledWith('m1')
+ remote.openInitialMachine('m1')
await vi.waitFor(() => expect(remote.activeTunnelUrl).toBe('http://127.0.0.1:4001'))
+ expect(engine.connect).toHaveBeenCalledWith('m1')
expect(remote.activeId).toBe('m1')
})
it('openInitialMachine:未知机器静默不切换(label 与列表不符的兜底)', async () => {
bindEngine({ list: [[]] })
- await remote.openInitialMachine('ghost')
+ remote.openInitialMachine('ghost')
+ await remote.refresh()
expect(remote.activeId).toBeNull()
expect(remote.activeTunnelUrl).toBe('')
+ // 挂起目标仍在(机器可能在后续轮询中出现),手动回本地即撤销
+ expect(remote.pendingBootMachineId).toBe('ghost')
+ remote.backToLocal()
+ expect(remote.pendingBootMachineId).toBeNull()
})
})
diff --git a/src/store/modules/remote/store.ts b/src/store/modules/remote/store.ts
index 159fb741..e6c070b3 100644
--- a/src/store/modules/remote/store.ts
+++ b/src/store/modules/remote/store.ts
@@ -64,6 +64,8 @@ export const remote = defineStore({
activeId: null as string | null,
/** 点击未连接机器后待切换的目标(连接就绪后升为 activeId)。 */
pendingId: null as string | null,
+ /** 远端弹窗启动寻址的挂起目标(refresh 成功且机器出现即切换;手动操作撤销) */
+ pendingBootMachineId: null as string | null,
/** 活动机器最近已知的隧道 URL(重连窗口粘性保留,避免指向空端口)。 */
activeTunnelUrl: '',
/** `/api-ssh` 是否可达;不可达时切换器降级(禁用远端项 + 提示)。 */
@@ -112,6 +114,18 @@ export const remote = defineStore({
this.pendingId = next.pendingId
this.activeTunnelUrl = next.activeTunnelUrl
this.available = true
+ // 启动寻址(远端弹窗 remote-<id>):实例就绪前 refresh 会连续失败,
+ // 一次性「拉一轮再切」抢跑必然落空——挂起目标随每次成功轮询推进,
+ // 机器一出现即切(已连接直切/未连接发起连接),用户手动操作则撤销
+ if (this.pendingBootMachineId !== null
+ && this.machines.some(machine => machine.id === this.pendingBootMachineId)) {
+ const target = this.pendingBootMachineId
+ this.pendingBootMachineId = null
+ // 必须等本轮 refresh 收尾再切:switchTo 的连接路径
+ // (connectAndSwitch)内部会再 refresh,嵌套调用会被
+ // refreshing 重入守卫吞掉导致永不切换
+ setTimeout(() => this.switchTo(target), 0)
+ }
await this.trackConnectProgress()
}
catch (err) {
@@ -177,6 +191,7 @@ export const remote = defineStore({
* 连接(阻塞到隧道就绪,见 S1 契约——不会指向空端口)。
*/
switchTo(machineId: string) {
+ this.pendingBootMachineId = null
if (!this.available)
return
const machine = this.machines.find(item => item.id === machineId)
@@ -245,17 +260,19 @@ export const remote = defineStore({
/**
* 远端弹窗启动寻址:窗口 label 为 remote-<machineId> 时由壳层调用——
- * 列表未载先拉一轮,随后走标准 switchTo(已连接直切;未连接发起连接,
- * 进度弹窗照常)。幂等:重复调用以最后一次为准。
+ * 登记挂起目标并立即触发一轮拉取;机器在任一成功轮询中出现即切
+ * (已连接直切;未连接发起连接,进度弹窗照常)。挂在 refresh 成功分支
+ * 而非一次性 await:新窗口启动时实例健康检查往往尚未就绪,抢跑的
+ * refresh 只会失败落空。幂等:重复调用以最后一次为准。
*/
- async openInitialMachine(machineId: string) {
- if (this.machines.length === 0)
- await this.refresh().catch(() => undefined)
- this.switchTo(machineId)
+ openInitialMachine(machineId: string) {
+ this.pendingBootMachineId = machineId
+ void this.refresh()
},
/** 退回本地实例视图(不断开远端连接;同时撤销挂起中的切换)。 */
backToLocal() {
+ this.pendingBootMachineId = null
this.activeId = null
this.activeTunnelUrl = ''
this.pendingId = null
@@ -290,6 +307,7 @@ export function disposeRemoteForTests(): void {
remote.machines = []
remote.activeId = null
remote.pendingId = null
+ remote.pendingBootMachineId = null
remote.activeTunnelUrl = ''
remote.available = true
remote.refreshing = false