From b730ebe545918f4c4191a355cc9ed85d3b6dff0e Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Wed, 11 Nov 2026 01:13:45 -0800 Subject: [PATCH] Keep retained presentation tied to the content owner --- .../retained-bare-workspace.test.tsx | 81 +++++++++++++++++++ ...ained-workspace-presentation.perf.test.tsx | 9 ++- .../retained-workspace-presentation.ts | 78 +++++++++++++++--- 3 files changed, 156 insertions(+), 12 deletions(-) diff --git a/src/renderer/src/components/retained-bare-workspace.test.tsx b/src/renderer/src/components/retained-bare-workspace.test.tsx index 87b77a86b0d..e332a4181e7 100644 --- a/src/renderer/src/components/retained-bare-workspace.test.tsx +++ b/src/renderer/src/components/retained-bare-workspace.test.tsx @@ -421,3 +421,84 @@ it('keeps host twins ambiguous and drops truly removed identities despite retain .map((row) => row.id) ).toEqual([checkout.id]) }) + +it('does not lend a listed owner’s content to a hidden host twin', () => { + const listed = { ...checkout, hostId: 'ssh:a' as const } + const hidden = { ...listed, hostId: 'runtime:other' as const, runtimeOwnerEnvironmentId: 'other' } + useAppStore.setState( + { + ...initialState, + repos: [{ ...TEST_REPO, executionHostId: 'ssh:a', connectionId: 'a' }], + activeWorktreeId: listed.id, + activeWorkspaceExecutionHostId: null, + worktreesByRepo: { repo1: [listed] }, + detectedWorktreesByRepo: { + repo1: { + repoId: 'repo1', + authoritative: true, + source: 'git', + worktrees: [{ ...hidden, ownership: 'external', selectedCheckout: false, visible: false }] + } + }, + unifiedTabsByWorktree: { + [listed.id]: [ + { + id: 'saved-twin', + entityId: 'saved-twin', + worktreeId: listed.id, + groupId: 'twin-group', + contentType: 'agent-session', + label: 'Saved twin', + customLabel: null, + color: null, + sortOrder: 0, + createdAt: 1 + } + ] + } + }, + true + ) + const state = useAppStore.getState() + expect(selectWorkspacePresentationWorktrees(state)).toBe(state.worktreesByRepo) + expect(getResolvedExecutionHostIdForWorktree(state, listed.id)).toBe('ssh:a') + expect(searchTabs('Saved twin').map((entry) => entry.executionHostId)).toEqual(['ssh:a']) + const created = state.createUnifiedTab(listed.id, 'terminal', { + id: 'new-twin-tab', + entityId: 'new-twin' + }) + expect(created.executionHostId).toBe('ssh:a') + useAppStore.setState((current) => ({ + unifiedTabsByWorktree: replaceRecordEntries( + current.unifiedTabsByWorktree, + [ + [ + listed.id, + current.unifiedTabsByWorktree[listed.id].map((tab) => + tab.id === 'saved-twin' ? { ...tab, executionHostId: 'runtime:other' } : tab + ) + ] + ] + ) + })) + expect( + selectWorkspacePresentationWorktrees(useAppStore.getState()).repo1.map((row) => row.hostId) + ).toEqual(['ssh:a', 'runtime:other']) + expect(searchTabs('Saved twin').map((entry) => entry.executionHostId)).toEqual(['runtime:other']) + useAppStore.setState((current) => ({ + unifiedTabsByWorktree: replaceRecordEntries( + current.unifiedTabsByWorktree, + [ + [ + listed.id, + current.unifiedTabsByWorktree[listed.id].map((tab) => ({ + ...tab, + executionHostId: 'ssh:a' + })) + ] + ] + ) + })) + const restored = useAppStore.getState() + expect(selectWorkspacePresentationWorktrees(restored)).toBe(restored.worktreesByRepo) +}) diff --git a/src/renderer/src/store/workspaces/catalog/retained-workspace-presentation.perf.test.tsx b/src/renderer/src/store/workspaces/catalog/retained-workspace-presentation.perf.test.tsx index 6818ba83d1a..9c071a4c94b 100644 --- a/src/renderer/src/store/workspaces/catalog/retained-workspace-presentation.perf.test.tsx +++ b/src/renderer/src/store/workspaces/catalog/retained-workspace-presentation.perf.test.tsx @@ -92,8 +92,10 @@ it.each([100, 1000])( true ) const inspected: string[] = [] + const inspectedContent: string[] = [] const selectPresentation = createWorkspacePresentationSelector({ - onInspectBucket: (id) => inspected.push(id) + onInspectBucket: (id) => inspected.push(id), + onInspectContent: (id) => inspectedContent.push(id) }) let targetRenders = 0 let otherRenders = 0 @@ -113,6 +115,7 @@ it.each([100, 1000])( const before = selectPresentation(useAppStore.getState()) targetRenders = otherRenders = emptyRenders = 0 inspected.length = 0 + inspectedContent.length = 0 act(() => { useAppStore.setState((state) => ({ tabsByWorktree: replaceRecordEntries(state.tabsByWorktree, [ @@ -125,17 +128,20 @@ it.each([100, 1000])( }) // A title changes one bucket, never membership: work is independent of fleet size. expect(inspected).toEqual([target.id]) + expect(inspectedContent).toEqual([target.id]) expect(selectPresentation(useAppStore.getState())).toBe(before) expect([targetRenders, otherRenders, emptyRenders]).toEqual([0, 0, 0]) expect(targetPane.result.current).toBe(before[target.repoId]) expect(otherPane.result.current).toBe(before[otherHost.repoId]) inspected.length = 0 + inspectedContent.length = 0 act(() => { useAppStore.setState({ agentStatusByPaneKey: { changed: makeAgentEntry('target-tab', 'working', 1) } }) }) expect(inspected).toEqual([]) + expect(inspectedContent).toEqual([]) expect([targetRenders, otherRenders, emptyRenders]).toEqual([0, 0, 0]) act(() => { useAppStore.setState((state) => ({ @@ -145,6 +151,7 @@ it.each([100, 1000])( })) }) expect(inspected).toEqual([empty.id]) + expect(inspectedContent).toEqual([empty.id]) expect([targetRenders, otherRenders, emptyRenders]).toEqual([0, 0, 1]) expect(newPane.result.current?.map((row) => row.id)).toEqual(['repo::/2/.bare']) } diff --git a/src/renderer/src/store/workspaces/catalog/retained-workspace-presentation.ts b/src/renderer/src/store/workspaces/catalog/retained-workspace-presentation.ts index b0e4ff1ff52..2f85dc9622e 100644 --- a/src/renderer/src/store/workspaces/catalog/retained-workspace-presentation.ts +++ b/src/renderer/src/store/workspaces/catalog/retained-workspace-presentation.ts @@ -1,5 +1,9 @@ import type { Worktree } from '../../../../../shared/worktree/types' -import { worktreeOwnerIdentity } from '@/lib/workspace/ownership/workspace-catalog-owner-index' +import { + worktreeOwnerHostIds, + worktreeOwnerIdentity +} from '@/lib/workspace/ownership/workspace-catalog-owner-index' +import { parseExecutionHostId, type ExecutionHostId } from '../../../../../shared/execution-host' import { readRecordChanges } from '../../keyed-record-change-reads' import { selectDetectedWorktreesByRepo, selectWorktreesByRepo } from './workspace-record-fields' @@ -19,11 +23,18 @@ const EMPTY_BUCKETS: ContentBuckets = {} const EMPTY_WORKTREES: Record = {} const EMPTY_DETECTED: Record = {} -type Membership = { ids: Set; changed: ReadonlySet } +type Membership = { + ids: Set + hostsById: Map> + changed: ReadonlySet +} -function createContentMembership(onInspectBucket?: (id: string) => void) { +function createContentMembership( + onInspectBucket?: (id: string) => void, + onInspectContent?: (id: string) => void +) { let previous: ContentBuckets = EMPTY_BUCKETS - let membership: Membership = { ids: new Set(), changed: new Set() } + let membership: Membership = { ids: new Set(), hostsById: new Map(), changed: new Set() } return (source: ContentBuckets): Membership => { if (source === previous) { return membership @@ -32,14 +43,44 @@ function createContentMembership(onInspectBucket?: (id: string) => void) { const addressed = changes?.kind === 'addressed' const keys = addressed ? changes.keys : new Set([...Object.keys(source), ...membership.ids]) let next = membership.ids + let hostsById = membership.hostsById const changed = new Set() for (const id of keys) { onInspectBucket?.(id) const hasContent = Boolean(source[id]?.length) - if (next.has(id) === hasContent) { + const hosts = new Set() + for (const entry of source[id] ?? []) { + onInspectContent?.(id) + if ( + entry && + typeof entry === 'object' && + 'executionHostId' in entry && + typeof entry.executionHostId === 'string' + ) { + const host = parseExecutionHostId(entry.executionHostId)?.id + if (host) { + hosts.add(host) + } + } + } + const previousHosts = hostsById.get(id) + const sameHosts = + hosts.size === (previousHosts?.size ?? 0) && + [...hosts].every((host) => previousHosts?.has(host)) + if (next.has(id) === hasContent && sameHosts) { continue } - if (next === membership.ids) { + if (!sameHosts) { + if (hostsById === membership.hostsById) { + hostsById = new Map(hostsById) + } + if (hosts.size) { + hostsById.set(id, hosts) + } else { + hostsById.delete(id) + } + } + if (next.has(id) !== hasContent && next === membership.ids) { next = new Set(membership.ids) } if (hasContent) { @@ -51,7 +92,7 @@ function createContentMembership(onInspectBucket?: (id: string) => void) { } previous = source if (changed.size) { - membership = { ids: next, changed } + membership = { ids: next, hostsById, changed } } return membership } @@ -78,10 +119,20 @@ function rowIdentity(row: PresentationRow): string { export function createWorkspacePresentationSelector(options?: { onInspectBucket?: (id: string) => void onInspectDetectedRow?: (id: string) => void + onInspectContent?: (id: string) => void }) { - const terminalMembership = createContentMembership(options?.onInspectBucket) - const unifiedMembership = createContentMembership(options?.onInspectBucket) - const browserMembership = createContentMembership(options?.onInspectBucket) + const terminalMembership = createContentMembership( + options?.onInspectBucket, + options?.onInspectContent + ) + const unifiedMembership = createContentMembership( + options?.onInspectBucket, + options?.onInspectContent + ) + const browserMembership = createContentMembership( + options?.onInspectBucket, + options?.onInspectContent + ) let previousVisible: Record = EMPTY_WORKTREES let previousDetected: NonNullable['detectedWorktreesByRepo']> = EMPTY_DETECTED @@ -156,10 +207,15 @@ export function createWorkspacePresentationSelector(o for (const repoId of affectedRepos) { const listed = visible[repoId] const listedIdentities = new Set((listed ?? []).map(rowIdentity)) + const listedIds = new Set((listed ?? []).map((row) => row.id)) const retained = (candidatesByRepo.get(repoId) ?? []).filter( (row) => memberships.some((membership) => membership.ids.has(row.id)) && - !listedIdentities.has(rowIdentity(row)) + !listedIdentities.has(rowIdentity(row)) && + (!listedIds.has(row.id) || + memberships.some((membership) => + worktreeOwnerHostIds(row).some((host) => membership.hostsById.get(row.id)?.has(host)) + )) ) if (retained.length) { retainedRepoIds.add(repoId)