Keep retained presentation tied to the content owner

This commit is contained in:
Brennan Benson
2026-11-11 01:13:45 -08:00
parent 8f526c7136
commit b730ebe545
3 changed files with 156 additions and 12 deletions
@@ -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<AppState['unifiedTabsByWorktree'][string]>(
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<AppState['unifiedTabsByWorktree'][string]>(
current.unifiedTabsByWorktree,
[
[
listed.id,
current.unifiedTabsByWorktree[listed.id].map((tab) => ({
...tab,
executionHostId: 'ssh:a'
}))
]
]
)
}))
const restored = useAppStore.getState()
expect(selectWorkspacePresentationWorktrees(restored)).toBe(restored.worktreesByRepo)
})
@@ -92,8 +92,10 @@ it.each([100, 1000])(
true
)
const inspected: string[] = []
const inspectedContent: string[] = []
const selectPresentation = createWorkspacePresentationSelector<Worktree>({
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'])
}
@@ -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<string, readonly never[]> = {}
const EMPTY_DETECTED: Record<string, { worktrees: readonly never[] }> = {}
type Membership = { ids: Set<string>; changed: ReadonlySet<string> }
type Membership = {
ids: Set<string>
hostsById: Map<string, ReadonlySet<ExecutionHostId>>
changed: ReadonlySet<string>
}
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<string>()
for (const id of keys) {
onInspectBucket?.(id)
const hasContent = Boolean(source[id]?.length)
if (next.has(id) === hasContent) {
const hosts = new Set<ExecutionHostId>()
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<W extends PresentationRow>(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<string, readonly W[]> = EMPTY_WORKTREES
let previousDetected: NonNullable<WorkspacePresentationState<W>['detectedWorktreesByRepo']> =
EMPTY_DETECTED
@@ -156,10 +207,15 @@ export function createWorkspacePresentationSelector<W extends PresentationRow>(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)