Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { describe, expect, it } from 'vitest'
import { FLOATING_TERMINAL_WORKTREE_ID } from '../../../../shared/constants'
import { useAppStore } from '@/store'
import { makeFolderWorkspace, makeWorktree } from '@/store/slices/worktrees-slice-test-fixtures'
import {
mergeWorktreesForHost,
worktreeHostMatchOptions
} from '@/store/slices/worktrees/listing/worktree-host-ownership'
import type { Repo } from '../../../../shared/repo-types'
import type { Worktree } from '../../../../shared/worktree/types'
import { getNotificationWorkspaceLabels } from './terminal-notification-state'

function stateWithWorkspace() {
Expand Down Expand Up @@ -143,6 +149,86 @@ describe('notification workspace labels', () => {
})
})

describe('STA-4343 two-host worktree id collision', () => {
const COLLIDING_ID = 'repo1::/work/orca'
const HOSTS = {
// The laptop row is unqualified, which is how a local worktree listing publishes it.
local: {
row: { displayName: 'Laptop feature' },
repo: { displayName: 'Orca on laptop', connectionId: null }
},
'ssh:build-box': {
row: { hostId: 'ssh:build-box' as const, displayName: 'Build box feature' },
repo: { displayName: 'Orca on build box', connectionId: 'build-box' }
}
} as const
type CollidingHostId = keyof typeof HOSTS

/**
* Reachability: both rows land through the production per-host merge, which
* replaces only the refreshing host's rows and keeps every other host's.
*
* `naiveWinner` refreshes first and is listed last in `repos`, so it is the row
* a first-wins worktree map AND a last-wins repo map both answer with. Naming
* the other host therefore cannot pass by accident.
*/
function stateWithCollidingHosts(naiveWinner: CollidingHostId) {
const rival: CollidingHostId = naiveWinner === 'local' ? 'ssh:build-box' : 'local'
const repos: Repo[] = [rival, naiveWinner].map((hostId) => ({
id: 'repo1',
path: '/work/orca',
badgeColor: 'blue',
addedAt: 0,
...HOSTS[hostId].repo
}))
const merged = [naiveWinner, rival].reduce<Worktree[] | undefined>(
(current, hostId) =>
mergeWorktreesForHost(
current,
[
makeWorktree({
id: COLLIDING_ID,
repoId: 'repo1',
path: '/work/orca',
...HOSTS[hostId].row
})
],
hostId,
worktreeHostMatchOptions({ repos }, 'repo1', hostId)
),
undefined
)
expect(merged).toHaveLength(2)
return { ...stateWithWorkspace(), repos, worktreesByRepo: { repo1: merged ?? [] } }
}

it.each(['local', 'ssh:build-box'] as const)(
'names the %s row, never the other host',
(hostId) => {
const state = stateWithCollidingHosts(hostId === 'local' ? 'ssh:build-box' : 'local')
state.activeWorktreeId = COLLIDING_ID
state.activeWorkspaceExecutionHostId = hostId
expect(getNotificationWorkspaceLabels(state, COLLIDING_ID, 'Terminal')).toEqual({
repoLabel: HOSTS[hostId].repo.displayName,
worktreeLabel: HOSTS[hostId].row.displayName
})
}
)

it.each(['local', 'ssh:build-box'] as const)(
'omits both names for a background workspace whose %s row would win a bare-id lookup',
(naiveWinner) => {
const state = stateWithCollidingHosts(naiveWinner)
state.activeWorktreeId = 'repo1::/work/other'
state.activeWorkspaceExecutionHostId = 'local'
expect(getNotificationWorkspaceLabels(state, COLLIDING_ID, 'Terminal')).toEqual({
repoLabel: undefined,
worktreeLabel: 'Terminal'
})
}
)
})

it.each(['folder:missing', 'missing-worktree', FLOATING_TERMINAL_WORKTREE_ID])(
'uses readable fallbacks for %s',
(id) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import type { useAppStore } from '@/store'
import { getRepoMapFromState, getWorktreeMapFromState } from '@/store/selectors'
import { getRepoMapFromState } from '@/store/selectors'
import { getIndexedWorktreesById } from '@/store/worktree-repo-index'
import {
worktreeHostMatchOptions,
worktreeMatchesHost
} from '@/store/slices/worktrees/listing/worktree-host-ownership'
import { getResolvedExecutionHostIdForWorktree } from '@/lib/resolved-worktree-execution-host'
import {
findIndexedFolderWorkspaceOwner,
findIndexedProjectGroupOwner,
findIndexedRepoOwnerForHost,
getCatalogOwnerHostId
} from '@/lib/worktree-runtime-owner-index'
import type { ExecutionHostId } from '../../../../shared/execution-host'
import type { Repo } from '../../../../shared/repo-types'
import type { Worktree } from '../../../../shared/worktree/types'
import { parseWorkspaceKey } from '../../../../shared/workspace-scope'
import { parsePaneKey } from '../../../../shared/stable-pane-id'
import type { TerminalPaneLayoutNode } from '../../../../shared/terminal-tab-types'
Expand Down Expand Up @@ -148,6 +158,44 @@ export function isCurrentKnownPaneKey(
return ptyHints.length === 0 || ptyHints.some((ptyId) => !isSuppressedPtyHint(state, ptyId))
}

/**
* The row for this workspace on the host that owns it, plus that host when the
* id needed one to be named.
*
* STA-4343: a worktree id is `repoId::path` with no host component, so two hosts
* publish one id for two different workspaces. An id-keyed lookup answers with
* whichever row came first, which on a collision is a coin flip — so resolve the
* owning host instead, and name nothing when hydrated ownership cannot prove one.
*/
function findWorktreeRowOnItsOwnHost(
state: StoreSnapshot,
worktreeId: string
): { worktree: Worktree | undefined; hostId: ExecutionHostId | null } {
const rows = getIndexedWorktreesById(state.worktreesByRepo, worktreeId)
if (rows.length <= 1) {
return { worktree: rows[0], hostId: null }
Comment thread
pullfrog[bot] marked this conversation as resolved.
Resolved
}
const hostId = getResolvedExecutionHostIdForWorktree(state, worktreeId)
if (!hostId) {
return { worktree: undefined, hostId: null }
}
// Colliding rows share the id's repo prefix, so any row names the repo to scope against.
const matchOptions = worktreeHostMatchOptions(state, rows[0].repoId, hostId)
return { worktree: rows.find((row) => worktreeMatchesHost(row, hostId, matchOptions)), hostId }
}

function findNotificationRepo(
state: StoreSnapshot,
repoId: string,
hostId: ExecutionHostId | null
): Repo | undefined {
if (!hostId) {
return getRepoMapFromState(state).get(repoId)
}
// A repo id collides across hosts too, so the resolved host picks the project name.
return findIndexedRepoOwnerForHost(state.repos, repoId, hostId) ?? undefined
}

export function getNotificationWorkspaceLabels(
state: StoreSnapshot,
workspaceId: string,
Expand All @@ -167,10 +215,11 @@ export function getNotificationWorkspaceLabels(
)
return { repoLabel: group?.name, worktreeLabel: folder?.name || fallback }
}
const worktree = getWorktreeMapFromState(state).get(
const { worktree, hostId } = findWorktreeRowOnItsOwnHost(
state,
scope?.type === 'worktree' ? scope.worktreeId : workspaceId
)
const repo = worktree ? getRepoMapFromState(state).get(worktree.repoId) : undefined
const repo = worktree ? findNotificationRepo(state, worktree.repoId, hostId) : undefined
return {
repoLabel: repo?.displayName,
worktreeLabel: worktree?.displayName || worktree?.branch || fallback
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Expand Down
Loading