Fix pinned worktree host metadata

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
brennanb2025
2026-06-23 17:36:32 -07:00
co-authored by Orca
parent 92bede6af8
commit 64a1a6487c
4 changed files with 37 additions and 5 deletions
@@ -922,6 +922,8 @@ function getPreferredWorktreeRows(
rows: readonly WorktreeItemRow[],
pinnedDisplayPolicy: PinnedWorktreeDisplayPolicy
): WorktreeItemRow[] {
// Why: when pinned rows are duplicated into natural groups, navigation and
// selection should prefer the natural-group copy and only fall back to pinned.
if (pinnedDisplayPolicy === 'single-location') {
const seen = new Set<string>()
return rows.filter((row) => {
@@ -29,6 +29,8 @@ function getSidebarWorktreeOptions(worktreeId: string): HTMLElement[] {
}
function isPinnedSidebarWorktreeOption(element: HTMLElement): boolean {
// Why: duplicated pinned rows share a worktree id; the row-key prefix is the
// stable row-scoped signal that distinguishes the pinned copy from natural rows.
return element.dataset.worktreeRowKey?.startsWith('pinned:') === true
}
@@ -220,6 +220,35 @@ describe('buildRows with pinned worktrees', () => {
expect(rows[2]).toMatchObject({ type: 'header', icon: ALL_GROUP_META.icon })
})
it('uses worktree host ownership for pinned header host counts', () => {
const runtimePinned = {
...pinned,
hostId: 'runtime:03ef704c-b180-4b10-998d-e28fbd5de9a3' as const
}
const rows = buildRows(
'none',
[runtimePinned, unpinned1],
repoMap,
null,
new Set(),
undefined,
undefined,
undefined,
{},
new Map([
[runtimePinned.id, runtimePinned],
[unpinned1.id, unpinned1]
])
)
const pinnedHeader = rows[0]
expect(pinnedHeader).toMatchObject({ type: 'header', key: 'pinned' })
expect(pinnedHeader.type === 'header' ? pinnedHeader.hostWorktreeCounts : undefined).toEqual(
new Map([['runtime:03ef704c-b180-4b10-998d-e28fbd5de9a3', 1]])
)
})
it('groups all worktrees under All in groupBy none', () => {
const rows = buildRows('none', [unpinned1, unpinned2], repoMap, null, new Set())
@@ -412,11 +412,7 @@ function emitPinnedGroup(
const pinnedRepoOrder: string[] = []
const seenPinnedRepoIds = new Set<string>()
for (const worktree of pinned) {
const repo = repoMap.get(worktree.repoId)
const hostId =
repo && (repo.connectionId || repo.executionHostId)
? getRepoExecutionHostId(repo)
: defaultHostId
const hostId = getWorktreeHostId(worktree, repoMap, defaultHostId)
hostWorktreeCounts.set(hostId, (hostWorktreeCounts.get(hostId) ?? 0) + 1)
const hostIds = hostWorktreeIds.get(hostId) ?? []
hostIds.push(worktree.id)
@@ -666,6 +662,9 @@ function getWorktreeHostId(
repoMap: Map<string, Repo>,
defaultHostId: ExecutionHostId
): ExecutionHostId {
if (worktree.hostId) {
return worktree.hostId
}
const repo = repoMap.get(worktree.repoId)
return repo && (repo.connectionId || repo.executionHostId)
? getRepoExecutionHostId(repo)