diff --git a/src/renderer/src/hooks/ipc-events/worktree-metadata-set-events.ts b/src/renderer/src/hooks/ipc-events/worktree-metadata-set-events.ts index 8a3b29fd205..9ce76a4dc55 100644 --- a/src/renderer/src/hooks/ipc-events/worktree-metadata-set-events.ts +++ b/src/renderer/src/hooks/ipc-events/worktree-metadata-set-events.ts @@ -31,20 +31,26 @@ function apply(record: unknown, source: MetadataSetSource): void { const listingHost: ExecutionHostId = source.kind === 'environment' ? toRuntimeExecutionHostId(source.environmentId) : set.hostId const producerKey = workspaceMetadataProducerKey(listingHost) - const state = useAppStore.getState() - // Every repo projection this producer lists here: each repo row on a host this producer serves. - const projections = state.repos.flatMap((repo) => { - const hostId = getRepoExecutionHostId(repo) - return workspaceMetadataProducerKey(hostId) === producerKey ? [{ repoId: repo.id, hostId }] : [] - }) - const listed = new Set(projections.map(({ repoId, hostId }) => `${hostId}\u0000${repoId}`)) + // Every repo projection this producer lists here, built only for a gap or a first-seen projection. + let projections: { repoId: string; hostId: ExecutionHostId }[] | undefined + let listed: Set | undefined + const producerProjections = (): { repoId: string; hostId: ExecutionHostId }[] => + (projections ??= useAppStore.getState().repos.flatMap((repo) => { + const hostId = getRepoExecutionHostId(repo) + return workspaceMetadataProducerKey(hostId) === producerKey + ? [{ repoId: repo.id, hostId }] + : [] + })) const { replayed, gap } = noteWorkspaceMetadataRecord( producerKey, set.epoch, set.seq, set.repoId, listingHost, - (repoId, hostId) => listed.has(`${hostId}\u0000${repoId}`) + (repoId, hostId) => + (listed ??= new Set(producerProjections().map((p) => `${p.hostId}\u0000${p.repoId}`))).has( + `${hostId}\u0000${repoId}` + ) ) if (replayed) { return @@ -57,8 +63,8 @@ function apply(record: unknown, source: MetadataSetSource): void { }) if (gap) { // A missed record names no projection: list every one this producer owns here, each once. - requireWorkspaceMetadataCoverage(producerKey, projections) - for (const { repoId, hostId } of projections) { + requireWorkspaceMetadataCoverage(producerKey, producerProjections()) + for (const { repoId, hostId } of producerProjections()) { requestWorkspaceMetadataRecovery(repoId, hostId, fetchWorktrees) } } else if (!applied) { diff --git a/src/renderer/src/store/slices/worktrees/listing/workspace-metadata-coverage.test.ts b/src/renderer/src/store/slices/worktrees/listing/workspace-metadata-coverage.test.ts index a0d826a7e43..fd6edc9c5c8 100644 --- a/src/renderer/src/store/slices/worktrees/listing/workspace-metadata-coverage.test.ts +++ b/src/renderer/src/store/slices/worktrees/listing/workspace-metadata-coverage.test.ts @@ -56,7 +56,8 @@ describe('workspace metadata recovery listing', () => { expect(fetchWorktrees).toHaveBeenCalledTimes(1) expect(fetchWorktrees).toHaveBeenCalledWith('repo-a', { forceLocalOwner: true, - forceFresh: true + forceFresh: true, + suppressRemoteLineageRefresh: true }) finish() diff --git a/src/renderer/src/store/slices/worktrees/listing/workspace-metadata-coverage.ts b/src/renderer/src/store/slices/worktrees/listing/workspace-metadata-coverage.ts index 3934e5e1839..23fbcabf1b6 100644 --- a/src/renderer/src/store/slices/worktrees/listing/workspace-metadata-coverage.ts +++ b/src/renderer/src/store/slices/worktrees/listing/workspace-metadata-coverage.ts @@ -3,7 +3,10 @@ import type { Worktree } from '../../../../../../shared/worktree/types' import type { WorkspaceMetadataCursor } from '../../../../../../shared/worktree/workspace-metadata-set' import { getWorkspaceAttachments } from '../../../../../../shared/workspace-attachments/workspace-attachments' import { getWorkspaceReferenceIdentity } from '../../../../../../shared/workspace-attachments/workspace-reference-identity' -import { getRuntimeEnvironmentRevision } from '../../../../runtime/rpc/environment-revisions/runtime-environment-revision' +import { + getRuntimeEnvironmentRevision, + onRuntimeEnvironmentsRetired +} from '../../../../runtime/rpc/environment-revisions/runtime-environment-revision' import { getRuntimeEnvironmentConnectionGeneration } from '../../runtime-status/runtime-status-connection-generation' import type { AppState } from '../../../types' import { selectWorktreesByRepo } from '../../../workspaces/catalog/workspace-record-fields' @@ -13,12 +16,29 @@ import type { WorktreeFetchOptions } from './worktree-fetch-options' import { requestWorkspaceMetadataRecovery } from './workspace-metadata-recovery' // One producer's workspaceMetadata stream as this client has seen it: the last record seq, and per listed -// projection (repo on a host) the newest record that touched it and whether a covering listing landed since. -type Projection = { repoId: string; hostId: ExecutionHostId; latest: number; pending: boolean } +// projection (repo on a host) the newest record that touched it. +type Projection = { repoId: string; hostId: ExecutionHostId; latest: number } type ProducerCoverage = { epoch: string; lastSeq: number; projections: Map } const producers = new Map() +const environmentKeyPrefix = (environmentId: string): string => `env:${environmentId}:` + +function dropEnvironmentProducers(environmentId: string, except?: string): void { + for (const key of producers.keys()) { + if (key !== except && key.startsWith(environmentKeyPrefix(environmentId))) { + producers.delete(key) + } + } +} + +// A removed or re-pointed environment's stream ends with it. +onRuntimeEnvironmentsRetired((environmentIds) => { + for (const environmentId of environmentIds) { + dropEnvironmentProducers(environmentId) + } +}) + const projectionKey = (repoId: string, hostId: ExecutionHostId): string => `${hostId}\u0000${repoId}` @@ -32,24 +52,20 @@ export function workspaceMetadataProducerKey(hostId: ExecutionHostId): string { return 'local' } const environmentId = parsed.environmentId - return `env:${environmentId}:${getRuntimeEnvironmentRevision(environmentId) ?? ''}:${getRuntimeEnvironmentConnectionGeneration(environmentId)}` + return `${environmentKeyPrefix(environmentId)}${getRuntimeEnvironmentRevision(environmentId) ?? ''}:${getRuntimeEnvironmentConnectionGeneration(environmentId)}` } -function producerFor(key: string, epoch: string): ProducerCoverage { +function producerFor(key: string, epoch: string, hostId: ExecutionHostId): ProducerCoverage { const current = producers.get(key) if (current?.epoch === epoch) { return current } // A new epoch is a restarted producer: what it lists already includes its earlier writes. const fresh = { epoch, lastSeq: 0, projections: new Map() } - if (key.startsWith('env:')) { - // A re-paired or reconnected environment is a new producer; its earlier bookkeeping dies here. - const environmentPrefix = key.slice(0, key.indexOf(':', 4) + 1) - for (const other of producers.keys()) { - if (other !== key && other.startsWith(environmentPrefix)) { - producers.delete(other) - } - } + const parsed = parseExecutionHostId(hostId) + if (parsed?.kind === 'runtime') { + // A reconnected environment is a new producer; its earlier bookkeeping dies here. + dropEnvironmentProducers(parsed.environmentId, key) } producers.set(key, fresh) return fresh @@ -58,7 +74,7 @@ function producerFor(key: string, epoch: string): ProducerCoverage { /** * Notes a record before it is applied. `replayed` records are dropped; a `gap` means some earlier * record was missed and its projection is unknown, so every projection of this producer needs a - * covering listing. Projections no longer listed here are forgotten. + * covering listing. A projection seen for the first time forgets those no longer listed here. */ export function noteWorkspaceMetadataRecord( key: string, @@ -68,23 +84,21 @@ export function noteWorkspaceMetadataRecord( hostId: ExecutionHostId, isListed: (repoId: string, hostId: ExecutionHostId) => boolean ): { replayed: boolean; gap: boolean } { - const producer = producerFor(key, epoch) + const producer = producerFor(key, epoch, hostId) if (seq <= producer.lastSeq) { return { replayed: true, gap: false } } const gap = producer.lastSeq > 0 && seq > producer.lastSeq + 1 producer.lastSeq = seq - for (const [projection, entry] of producer.projections) { - if (!isListed(entry.repoId, entry.hostId)) { - producer.projections.delete(projection) + const recorded = projectionKey(repoId, hostId) + if (!producer.projections.has(recorded)) { + for (const [projection, entry] of producer.projections) { + if (!isListed(entry.repoId, entry.hostId)) { + producer.projections.delete(projection) + } } } - producer.projections.set(projectionKey(repoId, hostId), { - repoId, - hostId, - latest: seq, - pending: true - }) + producer.projections.set(recorded, { repoId, hostId, latest: seq }) return { replayed: false, gap } } @@ -101,8 +115,7 @@ export function requireWorkspaceMetadataCoverage( producer.projections.set(projectionKey(repoId, hostId), { repoId, hostId, - latest: producer.lastSeq, - pending: true + latest: producer.lastSeq }) } } @@ -129,26 +142,6 @@ export function workspaceMetadataListingCoverage( return stamp.epoch !== producer.epoch || stamp.seq >= projection.latest ? 'covered' : 'stale' } -/** Whether the projection still waits for a listing that includes its newest record. */ -function isPending(key: string, repoId: string, hostId: ExecutionHostId): boolean { - return producers.get(key)?.projections.get(projectionKey(repoId, hostId))?.pending === true -} - -/** - * A covering listing ends the wait. The projection keeps its newest seq, so a listing that read before - * it and lands later is still recognised as stale. - */ -export function settleWorkspaceMetadataCoverage( - key: string, - repoId: string, - hostId: ExecutionHostId -): void { - const projection = producers.get(key)?.projections.get(projectionKey(repoId, hostId)) - if (projection) { - projection.pending = false - } -} - /** * A listing that read metadata before a published write must not erase what that write already showed: * keep the current rows' issue titles on the same, still-untitled links. Never adds or restores a link. @@ -189,9 +182,8 @@ function keepPublishedLinkTitles( type FetchWorktrees = (repoId: string, options: WorktreeFetchOptions) => Promise /** - * A listing's place against the records its repo is waiting on, checked right before it is merged: a - * stale listing keeps already-shown titles and, once applied, asks for a fresh listing; a covering one - * ends the repo's obligation. + * A listing's place against the newest record for its repo and host, checked right before it is + * merged: a stale listing keeps already-shown titles and, once applied, asks for a fresh listing. */ export function listingMetadataCoverage( state: AppState, @@ -224,9 +216,8 @@ export function listingMetadataCoverage( } : refresh, settle: (fetchWorktrees) => { - if (coverage === 'covered') { - settleWorkspaceMetadataCoverage(key, repoId, hostId) - } else if (coverage === 'stale' && isPending(key, repoId, hostId)) { + // Whatever the stale listing dropped (a newer link, say), a listing begun now includes. + if (coverage === 'stale') { requestWorkspaceMetadataRecovery(repoId, hostId, fetchWorktrees) } } diff --git a/src/renderer/src/store/slices/worktrees/listing/workspace-metadata-recovery.ts b/src/renderer/src/store/slices/worktrees/listing/workspace-metadata-recovery.ts index e1520bdea08..938afa9f88c 100644 --- a/src/renderer/src/store/slices/worktrees/listing/workspace-metadata-recovery.ts +++ b/src/renderer/src/store/slices/worktrees/listing/workspace-metadata-recovery.ts @@ -27,8 +27,8 @@ export function requestWorkspaceMetadataRecovery( inFlight.add(key) const options: WorktreeFetchOptions = hostId === LOCAL_EXECUTION_HOST_ID - ? { forceLocalOwner: true, forceFresh: true } - : { executionHostId: hostId, forceFresh: true } + ? { forceLocalOwner: true, forceFresh: true, suppressRemoteLineageRefresh: true } + : { executionHostId: hostId, forceFresh: true, suppressRemoteLineageRefresh: true } void fetchWorktrees(repoId, options) .catch(() => undefined) .finally(() => { diff --git a/src/renderer/src/store/slices/worktrees/listing/worktree-metadata-set-patch.test.ts b/src/renderer/src/store/slices/worktrees/listing/worktree-metadata-set-patch.test.ts index 5862923b35f..4af41008656 100644 --- a/src/renderer/src/store/slices/worktrees/listing/worktree-metadata-set-patch.test.ts +++ b/src/renderer/src/store/slices/worktrees/listing/worktree-metadata-set-patch.test.ts @@ -75,7 +75,7 @@ describe('addressed workspace metadata records', () => { expect(patchWorktreeMetadataSet(state(), twoLinks, local).applied).toBe(false) }) - it('keeps a shown title through a listing that predates it, then lists the repo afresh', () => { + it('keeps a shown title through a listing that predates it, then lists the repo afresh', async () => { noteWorkspaceMetadataRecord('local', 'e2', 7, 'repo-1', 'local', () => true) const shown = { ...state(), @@ -106,17 +106,19 @@ describe('addressed workspace metadata records', () => { stale.settle(fetchWorktrees) expect(fetchWorktrees).toHaveBeenCalledWith('repo-1', { forceLocalOwner: true, - forceFresh: true + forceFresh: true, + suppressRemoteLineageRefresh: true }) const covering = listingMetadataCoverage(shown, 'repo-1', 'local', listing(7)) expect(covering.refresh.result.worktrees[0].linkedItems).toEqual(issue()) covering.settle(fetchWorktrees) - // A listing that read before record 7 and lands after the covering one still keeps the title, - // and asks for nothing more. + expect(fetchWorktrees).toHaveBeenCalledTimes(1) + // A listing that read before record 7 and lands after the covering one still keeps the title, and + // lists again for anything else it dropped. const late = listingMetadataCoverage(shown, 'repo-1', 'local', listing(6)) expect(late.refresh.result.worktrees[0].linkedItems).toEqual(issue('Fix login')) late.settle(fetchWorktrees) - expect(fetchWorktrees).toHaveBeenCalledTimes(1) + await vi.waitFor(() => expect(fetchWorktrees).toHaveBeenCalledTimes(2)) }) })