Skip to content
Open
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
46 changes: 45 additions & 1 deletion src/renderer/src/components/terminal-pane/pty-connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18420,10 +18420,12 @@ describe('connectPanePty', () => {
expect(window.api.ssh.needsPassphrasePrompt).not.toHaveBeenCalled()
})

it('fails closed when the same SSH worktree id is projected by two HUBs', async () => {
it('routes through the focused HUB when the same SSH worktree id is projected by two HUBs (#10491)', async () => {
const { connectPanePty } = await import('./pty-connection')
const { createRemoteRuntimePtyTransport } = await import('./remote-runtime-pty-transport')
const { createIpcPtyTransport } = await import('./pty-transport')
const transport = createMockTransport()
transportFactoryQueue.push(transport)
mockStoreState = {
...mockStoreState,
tabsByWorktree: { 'wt-1': [{ id: 'tab-1', ptyId: null }] },
Expand Down Expand Up @@ -18458,6 +18460,48 @@ describe('connectPanePty', () => {
)
await flushAsyncTicks()

expect(createRemoteRuntimePtyTransport).toHaveBeenCalledWith('hub-a', expect.any(Object))
expect(createIpcPtyTransport).not.toHaveBeenCalled()
})

it('fails closed when two HUB projections share an id and focus matches neither', async () => {
const { connectPanePty } = await import('./pty-connection')
const { createRemoteRuntimePtyTransport } = await import('./remote-runtime-pty-transport')
const { createIpcPtyTransport } = await import('./pty-transport')
mockStoreState = {
...mockStoreState,
tabsByWorktree: { 'wt-1': [{ id: 'tab-1', ptyId: null }] },
worktreesByRepo: {
repo1: [
{
id: 'wt-1',
repoId: 'repo1',
path: '/srv/same-worktree',
hostId: 'ssh:same-private-target',
runtimeOwnerEnvironmentId: 'hub-a'
},
{
id: 'wt-1',
repoId: 'repo1',
path: '/srv/same-worktree',
hostId: 'ssh:same-private-target',
runtimeOwnerEnvironmentId: 'hub-b'
}
]
},
settings: {
...mockStoreState.settings,
activeRuntimeEnvironmentId: 'hub-c'
}
} as StoreState

connectPanePty(
createPane(1) as never,
createManager(1) as never,
createDeps({ restoredPtyIdByLeafId: { [LEAF_1]: null } }) as never
)
await flushAsyncTicks()

expect(createRemoteRuntimePtyTransport).not.toHaveBeenCalled()
expect(createIpcPtyTransport).not.toHaveBeenCalled()
expect(window.api.ssh.connect).not.toHaveBeenCalled()
Expand Down
45 changes: 45 additions & 0 deletions src/renderer/src/lib/terminal-worktree-route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,51 @@ describe('resolveTerminalWorktreeRoute', () => {
it('does not treat a folder workspace as an unresolved worktree', () => {
expect(resolveTerminalWorktreeRoute(localState(), folderWorkspaceKey('abc-123'))).not.toBeNull()
})

it('routes a focused-host copy when the same worktree id is projected on two hosts (#10491)', () => {
const state = localState({
settings: { activeRuntimeEnvironmentId: 'hub-a' },
runtimeEnvironments: [{ id: 'hub-a' }, { id: 'hub-b' }],
worktreesByRepo: {
'repo-1': [
{ id: 'repo-1::/w', repoId: 'repo-1', hostId: 'local' },
{
id: 'repo-1::/w',
repoId: 'repo-1',
hostId: 'ssh:private-target',
runtimeOwnerEnvironmentId: 'hub-a'
}
]
}
} as unknown as Partial<AppState>)
expect(resolveTerminalWorktreeRoute(state, 'repo-1::/w')).toEqual({
runtimeEnvironmentId: 'hub-a'
})
})

it('fails closed when multi-host projections share an id and focus matches none (#10491)', () => {
const state = localState({
settings: { activeRuntimeEnvironmentId: 'hub-c' },
runtimeEnvironments: [{ id: 'hub-a' }, { id: 'hub-b' }, { id: 'hub-c' }],
worktreesByRepo: {
'repo-1': [
{
id: 'repo-1::/w',
repoId: 'repo-1',
hostId: 'ssh:private-a',
runtimeOwnerEnvironmentId: 'hub-a'
},
{
id: 'repo-1::/w',
repoId: 'repo-1',
hostId: 'ssh:private-b',
runtimeOwnerEnvironmentId: 'hub-b'
}
]
}
} as unknown as Partial<AppState>)
expect(resolveTerminalWorktreeRoute(state, 'repo-1::/w')).toBeNull()
})
})

// STA-2639: teardown must follow the PTY's real host while spawn stays free to prefer a focused
Expand Down
72 changes: 72 additions & 0 deletions src/renderer/src/lib/worktree-operation-route-focus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {
getSettingsFocusedExecutionHostId,
parseExecutionHostId,
type ExecutionHostId
} from '../../../shared/execution-host'

export type WorktreeOperationRoute = {
executionHostId: ExecutionHostId | null
runtimeEnvironmentId: string | null
}

export type WorktreeOperationRouteResolution =
| { kind: 'resolved'; route: WorktreeOperationRoute }
| { kind: 'ambiguous' }
| { kind: 'missing' }

function routeMatchesFocusedHost(
route: WorktreeOperationRoute,
focusedHostId: ExecutionHostId
): boolean {
if (route.executionHostId === focusedHostId) {
return true
}
// Why: HUB-owned SSH rows keep executionHostId=ssh:* while transport ownership is the runtime.
const focused = parseExecutionHostId(focusedHostId)
return (
focused?.kind === 'runtime' &&
route.runtimeEnvironmentId != null &&
route.runtimeEnvironmentId === focused.environmentId
)
}

// Why: same worktree/repo id can project on multiple hosts; only collapse when focus selects one (#10491).
export function preferFocusedRoute(
routes: Iterable<WorktreeOperationRoute>,
settings: { activeRuntimeEnvironmentId?: string | null } | null | undefined
): WorktreeOperationRoute | null {
const candidates = [...routes]
if (candidates.length === 0) {
return null
}
if (candidates.length === 1) {
return candidates[0]
}
const focusedHostId = getSettingsFocusedExecutionHostId(settings)
const focusedMatches = candidates.filter((route) => routeMatchesFocusedHost(route, focusedHostId))
return focusedMatches.length === 1 ? focusedMatches[0] : null
}

export function resolveFromCandidateRoutes(
routes: Map<string, WorktreeOperationRoute>,
settings: { activeRuntimeEnvironmentId?: string | null } | null | undefined
): WorktreeOperationRouteResolution {
if (routes.size === 0) {
return { kind: 'missing' }
}
const preferred = preferFocusedRoute(routes.values(), settings)
if (preferred) {
return { kind: 'resolved', route: preferred }
}
return routes.size > 1 ? { kind: 'ambiguous' } : { kind: 'missing' }
}

export function addOperationRoute(
routes: Map<string, WorktreeOperationRoute>,
route: WorktreeOperationRoute | null
): void {
if (!route) {
return
}
routes.set(JSON.stringify(route), route)
}
58 changes: 57 additions & 1 deletion src/renderer/src/lib/worktree-operation-route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe('resolveWorktreeOperationRouteResult', () => {
})
})

it('fails closed when the same SSH worktree is projected by two HUBs', () => {
it('prefers the focused HUB when the same SSH worktree is projected by two HUBs (#10491)', () => {
expect(
resolveWorktreeOperationRouteResult(
{
Expand All @@ -74,6 +74,29 @@ describe('resolveWorktreeOperationRouteResult', () => {
},
WORKTREE_ID
)
).toEqual({
kind: 'resolved',
route: {
executionHostId: 'ssh:same-private-target',
runtimeEnvironmentId: 'hub-a'
}
})
})

it('fails closed when two HUB projections share an id and focus matches neither', () => {
expect(
resolveWorktreeOperationRouteResult(
{
settings: { activeRuntimeEnvironmentId: 'hub-c' } as never,
worktreesByRepo: {
'repo-1': [
worktree('ssh:same-private-target', 'hub-a'),
worktree('ssh:same-private-target', 'hub-b')
]
}
},
WORKTREE_ID
)
).toEqual({ kind: 'ambiguous' })
})

Expand All @@ -97,6 +120,39 @@ describe('resolveWorktreeOperationRouteResult', () => {
})
})

it('prefers the local projection when local focus selects one of two host copies (#10491)', () => {
expect(
resolveWorktreeOperationRouteResult(
{
worktreesByRepo: {
'repo-1': [worktree('local'), worktree('ssh:ssh-1', 'hub-a')]
}
},
WORKTREE_ID
)
).toEqual({
kind: 'resolved',
route: { executionHostId: 'local', runtimeEnvironmentId: null }
})
})

it('prefers the runtime projection when runtime focus selects one of two host copies (#10491)', () => {
expect(
resolveWorktreeOperationRouteResult(
{
settings: { activeRuntimeEnvironmentId: 'hub-a' } as never,
worktreesByRepo: {
'repo-1': [worktree('local'), worktree('runtime:hub-a')]
}
},
WORKTREE_ID
)
).toEqual({
kind: 'resolved',
route: { executionHostId: 'runtime:hub-a', runtimeEnvironmentId: 'hub-a' }
})
})

it('deduplicates identical projections from the same HUB', () => {
expect(
resolveWorktreeOperationRouteResult(
Expand Down
93 changes: 56 additions & 37 deletions src/renderer/src/lib/worktree-operation-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
} from '../../../shared/execution-host'
import { parseWorkspaceKey } from '../../../shared/workspace-scope'
import { getRepoIdFromWorktreeId } from '@/store/slices/worktree-helpers'
import { addRoute, resolveExactWorktreeRoute, routeForOwner } from './worktree-owner-route'
import { resolveExactWorktreeRoute, routeForOwner } from './worktree-owner-route'
import { addOperationRoute, resolveFromCandidateRoutes } from './worktree-operation-route-focus'
import {
findIndexedDetectedWorktrees,
hasIndexedDetectedWorktree,
Expand Down Expand Up @@ -197,61 +198,79 @@ function resolveFolderWorkspaceOperationRoute(
}
}

export function resolveExplicitWorktreeOperationRouteResult(
export function collectOwnerRoutes(
state: WorktreeOperationRouteState,
worktreeId: string
): WorktreeOperationRouteResolution {
const exactRoutes = new Map<string, WorktreeOperationRoute>()
const exactRepoIds = new Set<string>()
const indexedWorktree = resolveIndexedWorktreeOwner(state.worktreesByRepo, worktreeId)
if (indexedWorktree.kind === 'ambiguous') {
return { kind: 'ambiguous' }
}
if (indexedWorktree.kind === 'resolved') {
exactRepoIds.add(indexedWorktree.owner.repoId)
const resolution = resolveExactWorktreeRoute(state, indexedWorktree.owner)
if (resolution.kind === 'ambiguous') {
return resolution
owners: Iterable<WorktreeOperationOwnerRecord>,
worktreeId: string,
exactRoutes: Map<string, WorktreeOperationRoute>,
exactRepoIds: Set<string>
): void {
for (const worktree of owners) {
if (worktree.id !== worktreeId) {
continue
}
exactRepoIds.add(worktree.repoId)
const resolution = resolveExactWorktreeRoute(state, worktree)
if (resolution.kind === 'resolved') {
addRoute(exactRoutes, resolution.route)
addOperationRoute(exactRoutes, resolution.route)
} else if (resolution.kind === 'ambiguous') {
// Why: bare SSH route when multi-host repo recovery stays ambiguous; focus may still pick.
addOperationRoute(exactRoutes, routeForOwner(worktree))
}
}
for (const worktree of findIndexedDetectedWorktrees(state.detectedWorktreesByRepo, worktreeId)) {
exactRepoIds.add(worktree.repoId)
const resolution = resolveExactWorktreeRoute(state, worktree)
if (resolution.kind === 'ambiguous') {
return resolution
}

function collectRepoOperationRoutes(
repos: WorktreeOperationRouteState['repos'],
repoId: string
): Map<string, WorktreeOperationRoute> {
const routes = new Map<string, WorktreeOperationRoute>()
if (!repos) {
return routes
}
for (const repo of repos) {
if (repo.id !== repoId) {
continue
}
if (resolution.kind === 'resolved') {
addRoute(exactRoutes, resolution.route)
if (!repo.executionHostId?.trim() && !repo.connectionId?.trim()) {
continue
}
addOperationRoute(routes, routeForOwner({ hostId: getRepoExecutionHostId(repo) }))
}
return routes
}

export function resolveExplicitWorktreeOperationRouteResult(
state: WorktreeOperationRouteState,
worktreeId: string
): WorktreeOperationRouteResolution {
const exactRoutes = new Map<string, WorktreeOperationRoute>()
const exactRepoIds = new Set<string>()
// Why: scan every projection so focus can select the unique active-host owner (#10491).
for (const worktrees of Object.values(state.worktreesByRepo ?? {})) {
collectOwnerRoutes(state, worktrees, worktreeId, exactRoutes, exactRepoIds)
}
for (const result of Object.values(state.detectedWorktreesByRepo ?? {})) {
collectOwnerRoutes(state, result.worktrees, worktreeId, exactRoutes, exactRepoIds)
}
if (exactRoutes.size > 0) {
const route = exactRoutes.values().next().value
return exactRoutes.size === 1 && route ? { kind: 'resolved', route } : { kind: 'ambiguous' }
return resolveFromCandidateRoutes(exactRoutes, state.settings)
}
if (exactRepoIds.size === 0) {
exactRepoIds.add(getRepoIdFromWorktreeId(worktreeId))
}
const repoRoutes = new Map<string, WorktreeOperationRoute>()
for (const repoId of exactRepoIds) {
const resolution = resolveIndexedRepoOperationRoute(state.repos, repoId)
if (resolution.kind === 'ambiguous') {
return resolution
}
if (resolution.kind === 'resolved') {
addRoute(repoRoutes, resolution.route)
addOperationRoute(repoRoutes, resolution.route)
} else if (resolution.kind === 'ambiguous') {
for (const route of collectRepoOperationRoutes(state.repos, repoId).values()) {
addOperationRoute(repoRoutes, route)
}
}
}
const route = repoRoutes.values().next().value
if (repoRoutes.size === 1 && route) {
return { kind: 'resolved', route }
}
if (repoRoutes.size > 1) {
return { kind: 'ambiguous' }
}
return { kind: 'missing' }
return resolveFromCandidateRoutes(repoRoutes, state.settings)
}

function resolveIndexedRepoOperationRoute(
Expand Down
Loading