From 14bae2e172aef67e652d3f923bbb2ae382f57318 Mon Sep 17 00:00:00 2001 From: Neil Date: Tue, 6 Oct 2026 16:11:59 -0700 Subject: [PATCH] Route mirrored editor closes through their captured runtime owner --- .../runtime/close-mirrored-editor-tab.test.ts | 33 +++++++++++++++++++ .../src/runtime/close-mirrored-editor-tab.ts | 6 +++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/renderer/src/runtime/close-mirrored-editor-tab.test.ts b/src/renderer/src/runtime/close-mirrored-editor-tab.test.ts index 4876487c9df..b6bd926e1f5 100644 --- a/src/renderer/src/runtime/close-mirrored-editor-tab.test.ts +++ b/src/renderer/src/runtime/close-mirrored-editor-tab.test.ts @@ -99,6 +99,39 @@ describe('notifyHostOfMirroredEditorClose', () => { ]) }) + it('routes to the captured file owner when the workspace catalog disagrees', async () => { + const state = buildState() + state.openFiles = state.openFiles.map((file) => ({ ...file, runtimeEnvironmentId: 'env-2' })) + + expect(notifyHostOfMirroredEditorClose(state, 'wt-1', 'file-1')).toBe(true) + expect( + isWebSessionCloseIntentPending( + { environmentId: 'env-2' }, + 'wt-1', + toHostSessionTabId('host-tab-1'), + Date.now() + ) + ).toBe(true) + await vi.waitFor(() => + expect(closeWebRuntimeSessionTabMock).toHaveBeenCalledWith({ + worktreeId: 'wt-1', + tabId: 'host-tab-1', + environmentId: 'env-2', + reason: 'user' + }) + ) + expect(getRuntimeEnvironmentIdForWorktreeMock).not.toHaveBeenCalled() + }) + + it('does not infer a remote owner for an explicitly local file', () => { + const state = buildState() + state.openFiles = state.openFiles.map((file) => ({ ...file, runtimeEnvironmentId: null })) + + expect(notifyHostOfMirroredEditorClose(state, 'wt-1', 'file-1')).toBe(false) + expect(getRuntimeEnvironmentIdForWorktreeMock).not.toHaveBeenCalled() + expect(closeWebRuntimeSessionTabMock).not.toHaveBeenCalled() + }) + it('does not route locally-opened (non-mirrored) files to the host', () => { const state = buildState({ openFiles: [ diff --git a/src/renderer/src/runtime/close-mirrored-editor-tab.ts b/src/renderer/src/runtime/close-mirrored-editor-tab.ts index 847d6c445a0..f8b0e8c54d6 100644 --- a/src/renderer/src/runtime/close-mirrored-editor-tab.ts +++ b/src/renderer/src/runtime/close-mirrored-editor-tab.ts @@ -27,7 +27,11 @@ export function notifyHostOfMirroredEditorClose( if (!file?.mirroredFromRuntimeSession) { return false } - const runtimeEnvironmentId = getRuntimeEnvironmentIdForWorktree(state, worktreeId) + // Only legacy records need catalog inference; snapshots capture their authoritative runtime owner. + const runtimeEnvironmentId = + file.runtimeEnvironmentId === undefined + ? getRuntimeEnvironmentIdForWorktree(state, worktreeId) + : file.runtimeEnvironmentId if (!runtimeEnvironmentId?.trim()) { return false }