diff --git a/packages/gui-plugins/src/browser/pane-machine.test.ts b/packages/gui-plugins/src/browser/pane-machine.test.ts index 8ed7f08aded..0fe9abdc1c5 100644 --- a/packages/gui-plugins/src/browser/pane-machine.test.ts +++ b/packages/gui-plugins/src/browser/pane-machine.test.ts @@ -73,6 +73,16 @@ function attached(restore?: Browser.State) { return play(createPaneState(), registered(restore), on({ type: "server.attached", fileRoots: [] })).state } +test("reconnect tears down the previous native pages before restoring the same window and session", () => { + const opened = play(attached(), on({type:"tab.opened", tabID: tabA, attempt:1, tab:tab(tabA,"workspace://session/index.html")})) + const replacement = registered(undefined, "b2") + + if (replacement.type !== "registered") throw new Error("expected registration") + const next = transitionPane(opened.state, {...replacement, storageKey:"server\nb1"}) + expect(selectBinding(next.state,"b1")).toBeUndefined() + expect(next.effects.map(effect => effect.type)).toEqual(["emit", "release-pages", "teardown", "start-timer"]) +}) + test("sharing a preview releases its old page without closing the tab or exposing its old title", () => { const start = attached() const attempt = entry(start).nextAttempt @@ -95,14 +105,17 @@ test("tab inventories and command replies cross the same JSON boundary, includin for (const url of ["about:blank", "file:///project/index.html", "workspace://session/index.html", "artifact://art_report/"]) { const failed = { ...tab(tabA, url), loadError: "ERR_FAILED" } const restored = entry(attached({tabs:[failed], focusedTabID:tabA})).tabs.get(tabA)!.tab + for (const t of [failed, restored]) { const state = { tabs: [t], focusedTabID: tabA } expect(wireState(state)).toEqual(state) expect(wireOutcome({type:"success",result:{value:state,files:[]}}).type).toBe("success") } } - // SAFETY: simulate malformed runtime data from a browser operation, not a type-safe caller. - expect(wireOutcome({type:"success",result:{value:{missing:undefined},files:[]}} as unknown as Browser.Outcome)).toEqual(invalidReply) + + const invalid = {} + Object.defineProperty(invalid, "missing", { value: undefined, enumerable: true }) + expect(wireOutcome({ type: "success", result: { value: invalid, files: [] } })).toEqual(invalidReply) }) test("an offline registration returns the saved inventory for retry without forgetting it", () => { diff --git a/packages/gui-plugins/src/browser/pane-machine.ts b/packages/gui-plugins/src/browser/pane-machine.ts index 5279d878228..5dcf58f5c87 100644 --- a/packages/gui-plugins/src/browser/pane-machine.ts +++ b/packages/gui-plugins/src/browser/pane-machine.ts @@ -203,10 +203,20 @@ export function transitionPane(state: PaneState, event: PaneMachineEvent): PaneT if (event.type === "registered") { if (state.bindings.has(event.binding)) return { state, effects: [] } + // A reconnect can arrive before the old attach stream observes the disconnect. The window owns one + // native pane for this session, even when the renderer lost the IPC that would normally close it. + const bindings = new Map(state.bindings) + const effects: PaneEffect[] = [] + + for (const [id, old] of bindings) { + if (old.window !== event.window || old.storageKey !== event.storageKey) continue + bindings.delete(id) + effects.push(...close(old, "browser.pane.replaced", false).effects) + } return { - state: { status: "active", bindings: new Map(state.bindings).set(event.binding, createConnecting(event)) }, - effects: [{ type: "start-timer", binding: event.binding, ms: REGISTER_TIMEOUT_MS }], + state: { status: "active", bindings: bindings.set(event.binding, createConnecting(event)) }, + effects: [...effects, { type: "start-timer", binding: event.binding, ms: REGISTER_TIMEOUT_MS }], } } diff --git a/packages/gui-plugins/src/browser/workspace-pages.ts b/packages/gui-plugins/src/browser/workspace-pages.ts index 1f35bf5c7f1..cc71e234a8a 100644 --- a/packages/gui-plugins/src/browser/workspace-pages.ts +++ b/packages/gui-plugins/src/browser/workspace-pages.ts @@ -46,6 +46,13 @@ export function workspacePages( const identity = prior ?? previousRoot ?? (reader.type === "remote" ? await reader.pin(document, lifetime) : await previewRoot(root).then(pin => pin.id, () => undefined)) if (lifetime.aborted || disposed) return + + // Reconnect/reload replaces this document; its origin and storage are not a second live grant. + if (old) { + grants.get(old)?.release() + grants.delete(old) + } + previousRoot = undefined lastRoot = identity let first = true