Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/renderer/src/lib/browser-page-palette-activation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,62 @@ describe('activateBrowserPagePaletteResult group focus', () => {
expect(useAppStore.getState().activeBrowserTabId).toBeNull()
})
})

describe('activateBrowserPagePaletteResult host-colliding browser tabs', () => {
it('focuses the selected paired host tab when workspace ids collide', () => {
const localHost = makeWorktree({ hostId: 'local', displayName: 'Local' })
const remoteHost = makeWorktree({
hostId: 'ssh:private-target',
runtimeOwnerEnvironmentId: 'paired-host',
displayName: 'Remote'
})
const localTab = makeBrowserTab({ id: 'browser-local-tab', executionHostId: 'local' })
const remoteTab = makeBrowserTab({
id: 'browser-remote-tab',
executionHostId: 'runtime:paired-host',
groupId: 'group-remote'
})
seedStore({
worktreesByRepo: { 'repo-1': [localHost, remoteHost] },
browserTabsByWorktree: { 'wt-1': [makeWorkspace()] },
unifiedTabsByWorktree: { 'wt-1': [localTab, remoteTab] },
groupsByWorktree: {
'wt-1': [
makeGroup({ id: 'group-1', activeTabId: localTab.id }),
makeGroup({ id: 'group-remote', activeTabId: remoteTab.id })
]
},
activeWorktreeId: 'wt-1',
activeWorkspaceExecutionHostId: 'local'
})
mocks.activateAndRevealWorktree.mockImplementation((_id, options) => {
useAppStore.setState({ activeWorkspaceExecutionHostId: options?.executionHostId ?? null })
return true
})

expect(
activateBrowserPagePaletteResult({
pageId: 'page-1',
workspaceId: 'ws-1',
worktreeId: 'wt-1',
executionHostId: 'runtime:paired-host'
})
).toMatchObject({ status: 'activated' })
const state = useAppStore.getState()
expect(state.activeWorkspaceExecutionHostId).toBe('runtime:paired-host')
expect(state.activeGroupIdByWorktree['wt-1']).toBe('group-remote')
expect(state.groupsByWorktree['wt-1'][1].activeTabId).toBe('browser-remote-tab')

expect(
activateBrowserPagePaletteResult({
pageId: 'page-1',
workspaceId: 'ws-1',
worktreeId: 'wt-1',
executionHostId: 'local'
})
).toMatchObject({ status: 'activated' })
expect(useAppStore.getState().activeWorkspaceExecutionHostId).toBe('local')
expect(useAppStore.getState().activeGroupIdByWorktree['wt-1']).toBe('group-1')
expect(useAppStore.getState().groupsByWorktree['wt-1'][0].activeTabId).toBe('browser-local-tab')
})
})
27 changes: 22 additions & 5 deletions src/renderer/src/lib/browser-page-palette-activation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { useAppStore } from '@/store'
import { activateBrowserWorkspaceTab } from '@/lib/browser-workspace-tab-activation'
import {
activateBrowserWorkspaceTab,
resolveBrowserWorkspaceUnifiedTab
} from '@/lib/browser-workspace-tab-activation'
import type { ExecutionHostId } from '../../../shared/execution-host'
import { isBlankBrowserUrl } from './browser-palette-search'
import { activateAndRevealWorktree } from './worktree-activation'
Expand Down Expand Up @@ -29,11 +32,14 @@ export function activateBrowserPagePaletteResult({
worktreeId
}: BrowserPagePaletteActivationTarget): BrowserPagePaletteActivationResult {
const initialState = useAppStore.getState()
const page = (initialState.browserPagesByWorkspace[workspaceId] ?? []).find(
(candidate) => candidate.id === pageId
)
const workspace = (initialState.browserTabsByWorktree[worktreeId] ?? []).find(
(candidate) => candidate.id === workspaceId
(candidate) => candidate.id === workspaceId && candidate.worktreeId === worktreeId
)
const page = (initialState.browserPagesByWorkspace[workspaceId] ?? []).find(
(candidate) =>
candidate.id === pageId &&
candidate.workspaceId === workspaceId &&
candidate.worktreeId === worktreeId
)
const worktree = initialState.getKnownWorktreeById(worktreeId, executionHostId)
// Why worktree first: removing a worktree also purges its browser workspaces
Expand All @@ -52,6 +58,16 @@ export function activateBrowserPagePaletteResult({
: 'webview'

const targetHostId = executionHostId ?? worktree.hostId
const matchingUnifiedTab = resolveBrowserWorkspaceUnifiedTab({
executionHostId: targetHostId,
worktreeId: worktree.id,
workspaceId: workspace.id
})
// Validate ownership before activation so a stale browser tab cannot partially
// switch the active host and then fail to render a browser surface.
if (!matchingUnifiedTab) {
return { status: 'failed', reason: 'missing-tab' }
}
const activated = activateAndRevealWorktree(
worktree.id,
targetHostId ? { executionHostId: targetHostId } : {}
Expand All @@ -64,6 +80,7 @@ export function activateBrowserPagePaletteResult({
// active behind a tab that never shows the page.
if (
!activateBrowserWorkspaceTab({
executionHostId: targetHostId,
worktreeId: worktree.id,
workspaceId: workspace.id,
pageId
Expand Down
42 changes: 37 additions & 5 deletions src/renderer/src/lib/browser-workspace-tab-activation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
import { useAppStore } from '@/store'
import type { ExecutionHostId } from '../../../shared/execution-host'
import type { Tab } from '../../../shared/tab-types'
import { findAmbiguousWorktreeIds, isUnifiedTabOwnedByWorktree } from './unified-tab-host-ownership'

export function resolveBrowserWorkspaceUnifiedTab(params: {
executionHostId?: ExecutionHostId
worktreeId: string
workspaceId: string
}): Tab | null {
const state = useAppStore.getState()
const worktree = params.executionHostId
? state.getKnownWorktreeById(params.worktreeId, params.executionHostId)
: undefined
if (params.executionHostId && !worktree) {
return null
}
const ambiguousWorktreeIds = params.executionHostId
? findAmbiguousWorktreeIds(state.allWorktrees())
: new Set<string>()
return (
(state.unifiedTabsByWorktree[params.worktreeId] ?? []).find(
(candidate) =>
candidate.contentType === 'browser' &&
candidate.entityId === params.workspaceId &&
(!params.executionHostId ||
isUnifiedTabOwnedByWorktree(candidate, worktree!, ambiguousWorktreeIds))
) ?? null
)
}

/**
* Bring a browser workspace forward as the surface the reader is in.
Expand All @@ -9,22 +38,25 @@ import { useAppStore } from '@/store'
* nothing to bring forward.
*/
export function activateBrowserWorkspaceTab(params: {
executionHostId?: ExecutionHostId
worktreeId: string
workspaceId: string
pageId?: string
}): boolean {
const state = useAppStore.getState()
const unifiedTab = (state.unifiedTabsByWorktree[params.worktreeId] ?? []).find(
(candidate) => candidate.contentType === 'browser' && candidate.entityId === params.workspaceId
)
const unifiedTab = resolveBrowserWorkspaceUnifiedTab(params)
if (!unifiedTab) {
return false
}
state.focusGroup(params.worktreeId, unifiedTab.groupId)
state.activateTab(unifiedTab.id)
state.setActiveBrowserTab(params.workspaceId)
if (params.pageId) {
state.setActiveBrowserPage(params.workspaceId, params.pageId)
}
state.focusGroup(params.worktreeId, unifiedTab.groupId)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reorder is load-bearing: setActiveBrowserTab internally calls activateTab on the first browser tab matching the workspace id (browser.ts setActiveBrowserTab), unscoped to any host. In a host collision that would focus the wrong group; running the host-scoped activateTab last is what corrects it. A short Why: comment would stop a future cleanup from reordering it back.

Suggested change
state.focusGroup(params.worktreeId, unifiedTab.groupId)
// Why: setActiveBrowserTab re-activates the first workspace-matching tab unscoped
// (browser.ts setActiveBrowserTab), so this host-scoped activation must run last.
state.focusGroup(params.worktreeId, unifiedTab.groupId)

if (params.executionHostId) {
state.activateTab(unifiedTab.id, { worktreeId: params.worktreeId })
} else {
state.activateTab(unifiedTab.id)
}
return true
}
Loading