feat: M3 mosh 挂起→恢复<3s(唤醒脉冲 + 会话状态机三相)
- SessionMachine 加 mosh 三相(moshActive/moshParked/moshResuming)+看门狗兜底, mosh 接管即拆 SSH、后台冻结不重建、前台唤醒脉冲等 SSP 续(+5 单测) - bridge.go: Node.WakeUp()(tsnet InjectEvent+MagicSock.Rebind/ReSTUN) + MoshRelay.Rebind()(同端口重开 loopback、泵按 socket 生成容错) - iosclient.cc: g_mosh_last_heard 探针(导出 mosh_last_heard_ms) + SIGCONT 全屏重绘 - MoshSession: nudge()(SIGCONT) + lastHeardMs();SSHTerminalModel 前台脉冲三连 + healthy 轮询 + beginBackgroundTask;URL scheme 供无头前台唤醒 - 验证(vohive-vm over tsnet):15s 冻结→前台 wake pulse→1.3s recovered,<3s Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -125,4 +125,78 @@ struct SessionMachineTests {
|
||||
#expect(m2.reduce(.retryRequested) == [.startConnect])
|
||||
#expect(m2.phase == .connecting)
|
||||
}
|
||||
|
||||
// MARK: - M3 mosh 生命周期
|
||||
|
||||
private func connectedThenMosh() -> SessionMachine {
|
||||
var m = SessionMachine()
|
||||
_ = m.reduce(.connectRequested)
|
||||
_ = m.reduce(.established)
|
||||
return m
|
||||
}
|
||||
|
||||
@Test("mosh 接管拆 SSH,之后忽略 transportClosed")
|
||||
func moshTakeover() {
|
||||
var m = connectedThenMosh()
|
||||
let e = m.reduce(.moshEstablished)
|
||||
#expect(m.phase == .moshActive)
|
||||
#expect(e.contains(.teardownTransport))
|
||||
// 底层 SSH 残连断开,不影响 mosh
|
||||
#expect(m.reduce(.transportClosed(reason: "ssh eof")) == [])
|
||||
#expect(m.phase == .moshActive)
|
||||
}
|
||||
|
||||
@Test("mosh 后台→前台:唤醒脉冲 + 看门狗,收到 healthy 即恢复")
|
||||
func moshSuspendResumeHealthy() {
|
||||
var m = connectedThenMosh()
|
||||
_ = m.reduce(.moshEstablished)
|
||||
#expect(m.reduce(.enteredBackground) == [])
|
||||
#expect(m.phase == .moshParked)
|
||||
let e = m.reduce(.enteredForeground)
|
||||
#expect(m.phase == .moshResuming(attempt: 1))
|
||||
#expect(e.contains(.nudgeResume))
|
||||
#expect(e.contains(.scheduleResumeWatchdog(ms: 3000)))
|
||||
// SSP 续上
|
||||
let e2 = m.reduce(.moshHealthy)
|
||||
#expect(m.phase == .moshActive)
|
||||
#expect(e2.contains(.cancelResumeWatchdog))
|
||||
}
|
||||
|
||||
@Test("恢复看门狗:一次超时再脉冲,两次仍失败则兜底全量重建")
|
||||
func moshResumeWatchdogFallback() {
|
||||
var m = SessionMachine(resumeWatchdogMS: 3000, maxResumePulses: 2)
|
||||
_ = m.reduce(.connectRequested)
|
||||
_ = m.reduce(.established)
|
||||
_ = m.reduce(.moshEstablished)
|
||||
_ = m.reduce(.enteredBackground)
|
||||
_ = m.reduce(.enteredForeground) // moshResuming(1)
|
||||
// 第一次看门狗到点 → 再脉冲,进 moshResuming(2)
|
||||
let e1 = m.reduce(.resumeWatchdogFired)
|
||||
#expect(m.phase == .moshResuming(attempt: 2))
|
||||
#expect(e1.contains(.nudgeResume))
|
||||
// 第二次看门狗到点 → 兜底全量重建
|
||||
let e2 = m.reduce(.resumeWatchdogFired)
|
||||
#expect(m.phase == .reconnecting(attempt: 1))
|
||||
#expect(e2.contains(.teardownMosh))
|
||||
#expect(e2.contains(.startConnect))
|
||||
}
|
||||
|
||||
@Test("mosh 主循环退出转 failed 并拆除")
|
||||
func moshExited() {
|
||||
var m = connectedThenMosh()
|
||||
_ = m.reduce(.moshEstablished)
|
||||
let e = m.reduce(.moshExited(rc: 1))
|
||||
if case .failed = m.phase {} else { Issue.record("期望 failed,实际 \(m.phase)") }
|
||||
#expect(e.contains(.teardownMosh))
|
||||
}
|
||||
|
||||
@Test("mosh 恢复中进后台取消看门狗")
|
||||
func moshResumingToBackground() {
|
||||
var m = connectedThenMosh()
|
||||
_ = m.reduce(.moshEstablished)
|
||||
_ = m.reduce(.enteredBackground)
|
||||
_ = m.reduce(.enteredForeground) // moshResuming(1)
|
||||
#expect(m.reduce(.enteredBackground) == [.cancelResumeWatchdog])
|
||||
#expect(m.phase == .moshParked)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user