DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
fix(desktop): 帧确认绑定到当前 iframe 代,并在文档离开时作废 (#705)
hairyfc58e1be62a04
diff --git a/src-tauri/src/desktop/plugin_boot.js.inc b/src-tauri/src/desktop/plugin_boot.js.incindex f749cb43..ae413490 100644--- a/src-tauri/src/desktop/plugin_boot.js.inc+++ b/src-tauri/src/desktop/plugin_boot.js.inc@@ -5,7 +5,11 @@var stopped = falsevar sawSplash = false+ // 只有宿主直接内嵌的那一层 frame 参与帧身份握手:dsh 应用内部还会嵌套 iframe+ // (插件面板等),它们的自报/离开不代表宿主这一层换了文档。+ var shellFrame = window.parent === window.topvar reportedFrame = false+ var reportedLeaving = falsevar stalledForCurrentSplash = falsevar stalledTimer = nullvar observer = null@@ -96,12 +100,22 @@// 没有 #root。boot 页的 HTML 自带 #root,所以「子 frame 解析出 #root」是本 frame 正在// 承载 dsh 文档的确证;宿主据此区分「页面真的加载了」与「帧里只有一张浏览器错误页」。function reportFrame() {- if (reportedFrame) return+ if (reportedFrame || !shellFrame) returnif (!rootNode()) returnreportedFrame = truesend('dsh://plugin-boot:frame')}+ // 文档离开申报:帧内导航(dsh 页面把整帧跳到远端登录/错误页)不会换掉 iframe 元素,+ // 宿主只靠「换帧」清确认是抓不到的。自报过的文档一离开就作废旧确认,新文档要么自己+ // 重新自报,要么被宿主判成「帧里没有 dsh 页面」并给出可重试界面。只申报一次:迟到的+ // 重复 pagehide 不能把新文档刚立起来的确认再次作废。+ function reportLeaving() {+ if (!reportedFrame || reportedLeaving) return+ reportedLeaving = true+ send('dsh://plugin-boot:leaving')+ }+function stop() {if (stopped) returnstopped = true@@ -155,6 +169,9 @@observer = null}fallbackTimer = setInterval(evaluate, 1000)+ // 离开申报独立注册:stop() 会在观察结束后摘掉自己的 pagehide,而「文档后来导航走了」+ // 恰恰发生在观察结束后(应用 shell 已挂载),那一刻必须还能通知宿主。+ window.addEventListener('pagehide', reportLeaving)window.addEventListener('pagehide', stop)evaluate()})()diff --git a/src-tauri/src/desktop/plugin_boot.rs b/src-tauri/src/desktop/plugin_boot.rsindex 58959787..6f8ebc43 100644--- a/src-tauri/src/desktop/plugin_boot.rs+++ b/src-tauri/src/desktop/plugin_boot.rs@@ -27,7 +27,10 @@ mod tests {assert!(PLUGIN_BOOT_RELOAD_JS.contains("dsh://plugin-boot:ready"));assert!(PLUGIN_BOOT_RELOAD_JS.contains("dsh://plugin-boot:failed"));assert!(PLUGIN_BOOT_RELOAD_JS.contains("dsh://plugin-boot:frame"));+ assert!(PLUGIN_BOOT_RELOAD_JS.contains("dsh://plugin-boot:leaving"));assert!(PLUGIN_BOOT_RELOAD_JS.contains("reportFrame"));+ assert!(PLUGIN_BOOT_RELOAD_JS.contains("reportLeaving"));+ assert!(PLUGIN_BOOT_RELOAD_JS.contains("window.parent === window.top"));assert!(PLUGIN_BOOT_RELOAD_JS.contains("bootFailureText"));assert!(PLUGIN_BOOT_RELOAD_JS.contains("Failed to load plugins"));assert!(!PLUGIN_BOOT_RELOAD_JS.contains("body.innerText"));diff --git a/src/layout/components/iframe.tsx b/src/layout/components/iframe.tsxindex ac5a6431..8623ce42 100644--- a/src/layout/components/iframe.tsx+++ b/src/layout/components/iframe.tsx@@ -85,6 +85,11 @@ export function Iframe({ iframeRef }: IframeProps) {// iframe → 宿主:iframe 自身的桥共用一个监听器,按 `data.type` 分发useIframeMessage<IframeBridgeMessage>(iframeRef, (data) => {+ // 帧内文档离开(帧内导航):旧确认立刻作废,等新文档自己重新自报(issue #705)+ if (data.type === 'dsh://plugin-boot:leaving') {+ store.harness.markIframeLeaving()+ return+ }// 能走到这里说明帧内确实跑着 dsh 页面(来源与 origin 已由 hook 校验过),// 与具体桥无关——据此确认 iframe 不是一张浏览器内部错误页(issue #705)。store.harness.markIframeAlive()diff --git a/src/store/modules/harness/store.ts b/src/store/modules/harness/store.tsindex 04100efc..00079c9a 100644--- a/src/store/modules/harness/store.ts+++ b/src/store/modules/harness/store.ts@@ -100,13 +100,14 @@ export const harness = defineStore({iframeLoaded: false,iframeError: false,/**- * 帧内已确认承载 dsh 文档(收到任一帧内桥消息)。+ * 已确认承载 dsh 文档的 iframe 代(`iframeKey`)。** iframe 的 load 事件不足以证明页面加载成功:浏览器内部错误页(代理拦截、* DNS 失败等)同样触发 load,且永远不会发出帧内消息(issue #705)。因此页面- * 是否真的起来,只以「帧内自报」为准,load 只用来把判定窗口收紧。+ * 是否真的起来只以「帧内自报」为准,并把确认绑定到具体那一代 iframe——重挂+ * 之后旧文档的迟到消息不能替新文档背书。*/- iframeAlive: false,+ iframeAliveKey: null as number | null,iframeKey: 0,serviceHealthy: false,serviceRunning: false,@@ -123,6 +124,10 @@ export const harness = defineStore({showIframeError(): boolean {return this.serviceHealthy && this.iframeError},+ /** 当前这一代 iframe 是否已自报承载 dsh 文档(重挂即失效,见 `iframeAliveKey`) */+ iframeAlive(): boolean {+ return this.iframeAliveKey === this.iframeKey+ },/*** 内嵌页面加载失败的补充说明:服务已就绪却没有任何帧内消息,说明帧里根本没* 起来 dsh 页面(典型是一张浏览器内部错误页),而不是服务没跑起来。@@ -211,7 +216,7 @@ export const harness = defineStore({this.serviceHealthy = falsethis.iframeLoaded = falsethis.iframeError = false- this.iframeAlive = false+ this.iframeAliveKey = nullthis.fail(message)const error = await attachStartupDiagnostics(new Error(message), true)@@ -264,7 +269,7 @@ export const harness = defineStore({refreshIframe() {this.iframeLoaded = falsethis.iframeError = false- this.iframeAlive = false+ this.iframeAliveKey = nullif (iframeRefreshTimer !== undefined) {clearTimeout(iframeRefreshTimer)}@@ -287,7 +292,19 @@ export const harness = defineStore({/** 帧内任一桥消息到达:确认帧里确实跑着 dsh 页面(load 事件不算数,见 state 注释) */markIframeAlive() {- this.iframeAlive = true+ this.iframeAliveKey = this.iframeKey+ },++ /**+ * 帧内文档开始离开(pagehide):旧确认立即作废。+ *+ * 帧内导航(dsh 页面把整帧跳到远端登录/错误页)不换 iframe 元素,只靠代绑定+ * 抓不到;新文档要么自己重新自报,要么在宽限窗内被判成「帧里没有 dsh 页面」。+ * 同时撤回 load 结论:这一代已经没有任何已加载的文档了。+ */+ markIframeLeaving() {+ this.iframeAliveKey = null+ this.iframeLoaded = false},markIframeBootReady() {@@ -441,7 +458,7 @@ export const harness = defineStore({this.serviceHealthy = falsethis.iframeLoaded = falsethis.iframeError = false- this.iframeAlive = false+ this.iframeAliveKey = nullthis.startupPhase = 'process-boot'this.startupReason = i18next.t('status.loading_process')try {@@ -512,7 +529,7 @@ export const harness = defineStore({this.serviceHealthy = falsethis.iframeLoaded = falsethis.iframeError = false- this.iframeAlive = false+ this.iframeAliveKey = null// 重新启动/进入启动流程时先退出上一轮的错误与修复态(重启可能由插件修复、// 配置切换触发),避免旧的「启动失败 / Preview」等信息在启动期间闪现。// 注意:保留 attempts 计数,连续失败仍能命中「频繁失败」提示。@@ -845,8 +862,8 @@ export const harness = defineStore({// 当成加载成功——壳层既不给重试入口也不报错,用户只能重启电脑(issue #705)。let iframeWatch: { loaded: boolean, timer: ReturnType<typeof setTimeout> } | null = nullharness.$subscribe(() => {- const { status, serviceHealthy, iframeLoaded, iframeAlive, iframeError } = harness.$state- const watching = status === 'ready' && serviceHealthy && !iframeAlive && !iframeError+ const { status, serviceHealthy, iframeLoaded, iframeError } = harness.$state+ const watching = status === 'ready' && serviceHealthy && !harness.iframeAlive && !iframeErrorif (!watching) {if (iframeWatch) {clearTimeout(iframeWatch.timer)diff --git a/test/iframe-watchdog.test.ts b/test/iframe-watchdog.test.tsindex 6ef1063d..ab3bd051 100644--- a/test/iframe-watchdog.test.ts+++ b/test/iframe-watchdog.test.ts@@ -31,7 +31,7 @@ async function mountIframe(overrides: Record<string, unknown> = {}) {serviceRunning: true,iframeLoaded: false,iframeError: false,- iframeAlive: false,+ iframeAliveKey: null,busyAction: null,...overrides,})@@ -85,13 +85,16 @@ describe('iframe dsh document watchdog', () => {it('never shows the error screen while the frame reports a live dsh page', async () => {await mountIframe()harness.markIframeAlive()+ await vi.advanceTimersByTimeAsync(0)await vi.advanceTimersByTimeAsync(IFRAME_LOAD_TIMEOUT + IFRAME_FRAME_GRACE_TIMEOUT)expect(harness.iframeError).toBe(false)})it('requires a fresh frame report after the retry remounts the iframe', async () => {- await mountIframe({ iframeAlive: true, iframeLoaded: true })+ await mountIframe({ iframeLoaded: true })+ harness.markIframeAlive()+ await vi.advanceTimersByTimeAsync(0)harness.refreshIframe()await vi.advanceTimersByTimeAsync(0)@@ -103,6 +106,55 @@ describe('iframe dsh document watchdog', () => {expect(harness.iframeError).toBe(true)})+ // 上一代文档在重挂窗口(refreshIframe 的 800ms)里发出的迟到消息不能替新文档背书。+ it('ignores a late frame report that belongs to the previous iframe generation', async () => {+ await mountIframe({ iframeLoaded: true })+ harness.markIframeAlive()+ await vi.advanceTimersByTimeAsync(0)+ expect(harness.iframeAlive).toBe(true)++ harness.refreshIframe()+ harness.markIframeAlive()+ await vi.advanceTimersByTimeAsync(800)++ // 新文档提交(浏览器错误页同样会触发 load),但整代下来只有上一代的迟到自报+ harness.markIframeLoaded()+ await vi.advanceTimersByTimeAsync(IFRAME_FRAME_GRACE_TIMEOUT)++ expect(harness.iframeAlive).toBe(false)+ expect(harness.iframeError).toBe(true)+ })++ // 帧内导航(整帧跳到远端登录/错误页)不换 iframe 元素:自报过的文档离开后必须重新确认。+ it('requires a fresh frame report after the frame document navigates away', async () => {+ await mountIframe()+ harness.markIframeAlive()+ await vi.advanceTimersByTimeAsync(0)+ expect(harness.iframeAlive).toBe(true)++ harness.markIframeLeaving()+ await vi.advanceTimersByTimeAsync(0)+ expect(harness.iframeAlive).toBe(false)+ expect(harness.iframeLoaded).toBe(false)++ // 新文档再没自报(远端页面加载失败)→ 整体加载上限内给出可重试界面+ await vi.advanceTimersByTimeAsync(IFRAME_LOAD_TIMEOUT)+ expect(harness.iframeError).toBe(true)+ })++ it('keeps the frame alive again when the next document reports itself', async () => {+ await mountIframe({ iframeLoaded: true })+ harness.markIframeAlive()+ harness.markIframeLeaving()+ await vi.advanceTimersByTimeAsync(0)++ harness.markIframeAlive()+ await vi.advanceTimersByTimeAsync(0)++ await vi.advanceTimersByTimeAsync(IFRAME_LOAD_TIMEOUT + IFRAME_FRAME_GRACE_TIMEOUT)+ expect(harness.iframeError).toBe(false)+ })+it('explains the failure only when the frame never reported a dsh page', async () => {await mountIframe({ iframeError: true })expect(harness.iframeErrorHint).toBe(locale('zh-CN.json')['ui.iframe_unreachable_hint'])diff --git a/test/plugin-boot.test.ts b/test/plugin-boot.test.tsindex a53dfdc6..3509fe47 100644--- a/test/plugin-boot.test.ts+++ b/test/plugin-boot.test.ts@@ -7,11 +7,18 @@ type PageState = 'empty' | 'splash' | 'chat' | 'normal' | 'mounting'interface BootHarnessOptions {/** 模拟顶层文档(非 iframe):脚本必须整体不工作 */topFrame?: boolean+ /** 模拟 dsh 应用内部嵌套的 iframe(本脚本不作为宿主那一层参与握手) */+ nestedFrame?: boolean}interface BootHarness {+ /** 宿主(顶层文档)真实收到的消息 */messages: string[]+ /** 嵌套场景下发给中间那一层 dsh 文档的消息(宿主收不到) */+ nestedMessages: string[]setState: (state: PageState) => void+ /** 触发 pagehide(帧内导航离开当前文档) */+ leaveDocument: () => void}const script = readFileSync(@@ -22,10 +29,15 @@ const script = readFileSync(/** 帧身份申报(子 frame 解析出 `#root` 后必然先于其它消息到达一次)。 */const FRAME_REPORT = 'dsh://plugin-boot:frame'+/** 文档离开申报(帧内导航前上报,宿主据此作废旧确认)。 */+const FRAME_LEAVING = 'dsh://plugin-boot:leaving'+function createHarness(initialState: PageState, options: BootHarnessOptions = {}): BootHarness {let state = initialStatelet mutationCallback = () => {}const messages: string[] = []+ const nestedMessages: string[] = []+ const pageHideListeners: (() => void)[] = []function textNodes() {if (state === 'splash') {@@ -84,16 +96,37 @@ function createHarness(initialState: PageState, options: BootHarnessOptions = {}disconnect() {}}- const top = {}- const window = {+ // 宿主直接内嵌的那一层 frame:window.parent 就是顶层文档(window.top)。+ const top = {+ postMessage(message: { type: string }) {+ messages.push(message.type)+ },+ }+ // dsh 应用内部嵌套的 iframe:消息只会到中间那一层,宿主收不到。+ const nestedParent = {+ postMessage(message: { type: string }) {+ nestedMessages.push(message.type)+ },+ }+ const window: {+ top: unknown+ parent: { postMessage: (message: { type: string }) => void }+ addEventListener: (type: string, listener: () => void) => void+ removeEventListener: (type: string, listener: () => void) => void+ } = {top,- parent: {- postMessage(message: { type: string }) {- messages.push(message.type)- },+ parent: options.nestedFrame ? nestedParent : top,+ addEventListener(type: string, listener: () => void) {+ if (type === 'pagehide')+ pageHideListeners.push(listener)+ },+ removeEventListener(type: string, listener: () => void) {+ if (type !== 'pagehide')+ return+ const index = pageHideListeners.indexOf(listener)+ if (index >= 0)+ pageHideListeners.splice(index, 1)},- addEventListener() {},- removeEventListener() {},}if (options.topFrame)window.top = window@@ -112,10 +145,15 @@ function createHarness(initialState: PageState, options: BootHarnessOptions = {}return {messages,+ nestedMessages,setState(nextState) {state = nextStatemutationCallback()},+ leaveDocument() {+ for (const listener of [...pageHideListeners])+ listener()+ },}}@@ -198,6 +236,47 @@ describe('plugin boot bridge', () => {const harness = createHarness('normal', { topFrame: true })vi.advanceTimersByTime(60_000)+ harness.leaveDocument()+ expect(harness.messages).toEqual([])+ })++ // dsh 应用内部还会嵌套 iframe(插件面板等),它们既不是宿主那一层,也不代表宿主+ // 换过文档;参与握手会让宿主的确认被无关帧的导航搅乱。+ it('stays silent inside a frame nested in the dsh application', () => {+ vi.useFakeTimers()+ const harness = createHarness('splash', { nestedFrame: true })++ vi.advanceTimersByTime(20_000)+ harness.leaveDocument()+ // splash 卡死确实会通知——但发给的是中间那一层 dsh 文档,宿主永远收不到握手消息+ expect(harness.nestedMessages).toEqual(['dsh://plugin-boot:stalled'])+ expect(harness.messages).toEqual([])+ })++ // issue #705 的另一半:帧内导航(整帧跳到远端登录/错误页)不换 iframe 元素,+ // 自报过的文档必须在离开时主动作废宿主的确认,否则宿主会一直以为页面还在。+ it('reports the document leaving after a frame-internal navigation', () => {+ vi.useFakeTimers()+ const harness = createHarness('splash')++ vi.advanceTimersByTime(2_000)+ harness.setState('normal')+ expect(harness.messages).toEqual([FRAME_REPORT, 'dsh://plugin-boot:ready'])++ harness.leaveDocument()+ expect(harness.messages).toEqual([FRAME_REPORT, 'dsh://plugin-boot:ready', FRAME_LEAVING])++ // 离开只申报一次:再次触发不重复打扰宿主+ harness.leaveDocument()+ expect(harness.messages).toEqual([FRAME_REPORT, 'dsh://plugin-boot:ready', FRAME_LEAVING])+ })++ it('never reports a document leaving when the frame never carried a dsh page', () => {+ vi.useFakeTimers()+ const harness = createHarness('mounting')++ vi.advanceTimersByTime(10_000)+ harness.leaveDocument()expect(harness.messages).toEqual([])})})