diff --git a/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-frontier-owner.ts b/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-frontier-owner.ts index 64c7f527d50..ef3fa1b7ea7 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-frontier-owner.ts +++ b/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-frontier-owner.ts @@ -1,5 +1,9 @@ import { relativePathInsideRoot } from '../../../../../../shared/cross-platform-path' import { ExplorerRepositoryFrontierSupport } from './file-explorer-repository-frontier-support' +import { + ExplorerRepositoryProofEdges, + sameRepositoryListingOwner +} from './file-explorer-repository-proof-edges' import type { ExplorerRepositoryCandidate } from './file-explorer-repository-observer' import type { DirCache, FileExplorerOperationOwner } from './file-explorer-types' @@ -16,25 +20,11 @@ type FrontierConsumer = { ) => void invalidate: (path: string) => void resetProof: () => void + branches: (path: string) => readonly string[] + invalidateSubtree: (path: string) => void } -function sameOwner( - a: FileExplorerOperationOwner | undefined, - b: FileExplorerOperationOwner -): boolean { - return ( - a?.kind === b.kind && - (b.kind === 'ssh' - ? a.kind === 'ssh' && a.connectionId === b.connectionId - : b.kind === 'runtime' - ? a.kind === 'runtime' && - a.environmentId === b.environmentId && - a.executionHostId === b.executionHostId - : true) - ) -} - -/** Each reachable directory owns its direct contribution; hidden commits retain only path headers. */ +/** Each reachable directory owns its direct contribution; hidden structure retains only path headers. */ export class ExplorerRepositoryFrontier { private current: Readonly> = {} private consumed = this.current @@ -52,12 +42,20 @@ export class ExplorerRepositoryFrontier { private support = new ExplorerRepositoryFrontierSupport() private changes = new Map() private affected = new Set() + private proofEdges: ExplorerRepositoryProofEdges constructor( private root: string, private owner: FileExplorerOperationOwner, private consumer: FrontierConsumer - ) {} + ) { + this.proofEdges = new ExplorerRepositoryProofEdges( + root, + owner, + consumer.branches, + consumer.invalidateSubtree + ) + } setView( displayRoot: string | null, @@ -78,10 +76,12 @@ export class ExplorerRepositoryFrontier { this.current = cache if (reset) { this.reset = true + this.proofEdges.clear() this.dirty.clear() this.consumer.resetProof() } else { for (const path of paths) { + this.proofEdges.commit(path, cache[path]) const relative = relativePathInsideRoot(this.root, path) const before = this.valid(previous[path]) ? previous[path] : undefined const after = this.valid(cache[path]) ? cache[path] : undefined @@ -167,6 +167,7 @@ export class ExplorerRepositoryFrontier { this.consumed = this.current this.consumedExpanded = this.expanded this.dirty.clear() + this.proofEdges.clear() for (const path of this.support.changedRanges()) { this.affected.add(path) } @@ -178,7 +179,11 @@ export class ExplorerRepositoryFrontier { } private valid(cache: DirCache | undefined): cache is DirCache { - return !!cache && cache.error === undefined && sameOwner(cache.operationOwner, this.owner) + return ( + !!cache && + cache.error === undefined && + sameRepositoryListingOwner(cache.operationOwner, this.owner) + ) } private refreshCandidate(path: string): void { @@ -235,7 +240,7 @@ export class ExplorerRepositoryFrontier { if ( child.outsideProject || child.isSymlink || - !sameOwner(child.operationOwner, this.owner) + !sameRepositoryListingOwner(child.operationOwner, this.owner) ) { continue } diff --git a/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-frontier.ts b/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-frontier.ts index e7c0b941fa7..1210c8569e1 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-frontier.ts +++ b/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-frontier.ts @@ -16,7 +16,9 @@ export function buildExplorerRepositoryFrontier( const frontier = new ExplorerRepositoryFrontier(root, owner, { apply: () => {}, invalidate: () => {}, - resetProof: () => {} + resetProof: () => {}, + branches: () => [], + invalidateSubtree: () => {} }) frontier.setView(displayRoot, expanded, hasRootGitSource) frontier.commit(dirCache, []) diff --git a/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-observer.ts b/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-observer.ts index 04c237b91a0..7ef91eb686a 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-observer.ts +++ b/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-observer.ts @@ -172,6 +172,28 @@ export class FileExplorerRepositoryObserver { this.discovery.invalidate(path) } + proofBranches(path: string): string[] { + return [ + ...new Set([...this.discovery.index.branches(path), ...this.sourceIndex.branches(path)]) + ] + } + + invalidateProofSubtree(path: string): void { + for (const candidate of this.discovery.index.descendants([path])) { + this.discovery.invalidate(candidate) + } + for (const sourcePath of this.sourceIndex.descendants([path])) { + const source = this.sources.get(sourcePath)! + source.statusDirty = true + source.proofRevision++ + source.proofRetryAt = -Infinity + source.ignoredQueries.invalidate(this.visible) + if (this.visible) { + source.scheduler.signal() + } + } + } + setDemand(paths: readonly string[] = this.demand, partition?: ExplorerRepositoryDemand): void { this.demand = paths updateExplorerRepositoryDemand(this.sources, paths, this.visible, () => this.emit(), partition) diff --git a/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-path-index.ts b/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-path-index.ts index 08c4afa8375..b50a95fc499 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-path-index.ts +++ b/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-path-index.ts @@ -52,6 +52,12 @@ export class ExplorerRepositoryPathIndex { return this.find(path)?.member === true } + branches(path: string): string[] { + return [...(this.find(path)?.children.keys() ?? [])].map((part) => + path ? `${path}/${part}` : part + ) + } + set(paths: Iterable): void { this.root = { member: false, children: new Map() } for (const path of paths) { diff --git a/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-proof-edges.ts b/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-proof-edges.ts new file mode 100644 index 00000000000..1b6233308a2 --- /dev/null +++ b/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-proof-edges.ts @@ -0,0 +1,85 @@ +import { relativePathInsideRoot } from '../../../../../../shared/cross-platform-path' +import type { DirCache, FileExplorerOperationOwner } from './file-explorer-types' + +export function sameRepositoryListingOwner( + a: FileExplorerOperationOwner | undefined, + b: FileExplorerOperationOwner +): boolean { + return ( + a?.kind === b.kind && + (b.kind === 'ssh' + ? a.kind === 'ssh' && a.connectionId === b.connectionId + : b.kind === 'runtime' + ? a.kind === 'runtime' && + a.environmentId === b.environmentId && + a.executionHostId === b.executionHostId + : true) + ) +} + +/** Protected edges survive uncertain omissions, but never a proven identity break. */ +export class ExplorerRepositoryProofEdges { + private parents = new Map>() + + constructor( + private root: string, + private owner: FileExplorerOperationOwner, + private branches: (path: string) => readonly string[], + private invalidate: (path: string) => void + ) {} + + commit(path: string, listing: DirCache | undefined): void { + const relative = relativePathInsideRoot(this.root, path) + if (relative === null) { + return + } + let protectedEdges = this.parents.get(relative) + if (!protectedEdges) { + protectedEdges = new Set(this.branches(relative)) + if (!protectedEdges.size) { + return + } + this.parents.set(relative, protectedEdges) + } + if ( + !protectedEdges.size || + !listing || + listing.error !== undefined || + !sameRepositoryListingOwner(listing.operationOwner, this.owner) + ) { + return + } + const present = new Set() + if (!listing.outsideProject) { + for (const child of listing.children) { + const childPath = relativePathInsideRoot(this.root, child.path) + if (childPath === null || !protectedEdges.has(childPath)) { + continue + } + if ( + child.isDirectory && + !child.isSymlink && + !child.outsideProject && + sameRepositoryListingOwner(child.operationOwner, this.owner) + ) { + present.add(childPath) + } else { + this.invalidate(childPath) + protectedEdges.delete(childPath) + } + } + } + if (!listing.more || listing.outsideProject) { + for (const child of protectedEdges) { + if (!present.has(child)) { + this.invalidate(child) + protectedEdges.delete(child) + } + } + } + } + + clear(): void { + this.parents.clear() + } +} diff --git a/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-recovery.test.ts b/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-recovery.test.ts index 1360f0bfb5e..0bc9dcbaa01 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-recovery.test.ts +++ b/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-recovery.test.ts @@ -1,3 +1,4 @@ +import { ExplorerRepositoryFrontier } from './file-explorer-repository-frontier-owner' import { afterEach, expect, it, vi } from 'vitest' import type { GitRepositoryStatus, @@ -315,17 +316,36 @@ it('discards invalidated in-flight discovery proof while healthy siblings and la } ) const cache = discoveryCache(101) - applyDiscoveryCache(observer, cache) + const frontier = new ExplorerRepositoryFrontier( + '/w', + { kind: 'local' }, + { + apply: (changes, affected, retains) => observer.updateFrontier(changes, affected, retains), + invalidate: (path) => observer.invalidateCandidate(path), + resetProof: () => observer.invalidate(), + branches: (path) => observer.proofBranches(path), + invalidateSubtree: (path) => observer.invalidateProofSubtree(path) + } + ) + frontier.setView('/w', new Set(), true) + frontier.commit(cache, [], true) + frontier.setVisible(true) observer.setVisible(true) try { await vi.advanceTimersByTimeAsync(1000) expect(probes).toHaveLength(1) expect(probes[0]).toHaveLength(100) - observer.invalidateChanges([{ path: 'd1/.git', kind: 'update', directory: false }]) - applyDiscoveryCache(observer, { - ...cache, - '/w/unrelated': { operationOwner: { kind: 'local' }, children: [] } - }) + frontier.commit( + { + ...cache, + '/w': { + ...cache['/w'], + children: cache['/w'].children.filter((child) => child.relativePath !== 'd1') + } + }, + ['/w'] + ) + frontier.commit(cache, ['/w']) release(probes[0].map((path) => ({ path, kind: 'repository' }))) await vi.advanceTimersByTimeAsync(0) expect(probes).toHaveLength(2) diff --git a/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-scaling.test.tsx b/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-scaling.test.tsx index 0ddae15110d..bccdb7aedb8 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-scaling.test.tsx +++ b/src/renderer/src/components/right-sidebar/file-explorer/tree/file-explorer-repository-scaling.test.tsx @@ -495,3 +495,163 @@ it('keeps unknown discovery backoff across repeated unreadable cache updates', a expect(probes).toEqual([['folder'], ['folder']]) fixture.unmount() }) + +it.each([1000, 10000])( + 'retires only discontinued hidden parent edges among %i unrelated directories', + async (count) => { + vi.useFakeTimers() + supports.mockResolvedValue(true) + const repositories = new Set() + const probes: string[][] = [] + read.mockImplementation(async (_route, request: GitRepositoryStatusRequest) => { + if (request.repositoryIntent === 'probe') { + probes.push(request.repositoryPaths!) + } + return { + repositoryStatuses: request.repositoryPaths!.map((path) => ({ + path, + kind: repositories.has(path) ? 'repository' : 'absent', + ...(repositories.has(path) && request.repositoryIntent === 'status' + ? { status: { entries: [], conflictOperation: 'unknown' } } + : {}) + })) + } + }) + let parentRows = 0 + let unrelatedRows = 0 + let unrelatedMetadata = 0 + const child = (name: string): TreeNode => ({ + ...node(`p/${name}`, true), + get outsideProject() { + parentRows++ + return undefined + } + }) + const initialParent: DirCache = { + operationOwner: owner, + repositoryMarker: false, + children: [child('d'), child('stable')] + } + const cache: Record = { + '/w': { + operationOwner: owner, + children: [ + node('p', true), + ...Array.from({ length: count }, (_, index) => ({ + ...node(`peer${index}`, true), + get outsideProject() { + unrelatedRows++ + return undefined + } + })) + ] + }, + '/w/p': initialParent + } + for (let index = 0; index < count; index++) { + cache[`/w/peer${index}`] = { + operationOwner: owner, + children: [], + get repositoryMarker() { + unrelatedMetadata++ + return false + } + } + } + const store = new FileExplorerDirectoryCache() + store.update(() => ({ + dirCache: cache, + sourceWorkspaceId: 'folder:w', + reset: true, + changedPaths: [] + })) + const expanded = new Set(['/w/p']) + const fixture = renderHook( + ({ visible }) => + useFileExplorerRepositories( + 'folder:w', + '/w', + '/w', + { dirCacheStore: store }, + expanded, + visible, + true + ), + { initialProps: { visible: true } } + ) + await act(() => vi.advanceTimersByTimeAsync(1000)) + expect(probes.flat().sort()).toEqual(['p/d', 'p/stable']) + const commit = (listing: DirCache): void => { + act(() => + store.update((previous) => ({ + dirCache: { ...previous, '/w/p': listing }, + changedPaths: ['/w/p'] + })) + ) + } + const partial = { ...initialParent, children: [child('stable')], more: true } + const failed = { ...initialParent, children: [], error: 'unreadable' } + fixture.rerender({ visible: false }) + probes.length = 0 + parentRows = unrelatedRows = unrelatedMetadata = 0 + commit(failed) + commit(partial) + commit(initialParent) + expect(parentRows).toBe(3) + expect(probes).toEqual([]) + fixture.rerender({ visible: true }) + await act(() => vi.advanceTimersByTimeAsync(4000)) + expect(probes.flat()).toEqual(['p']) + + fixture.rerender({ visible: false }) + probes.length = 0 + parentRows = 0 + // Transient unadmitted edges do not create proof history or retry obligations. + for (let index = 0; index < 20; index++) { + commit({ + ...initialParent, + children: [...initialParent.children, child(`transient${index}`)] + }) + commit(initialParent) + } + expect(parentRows).toBe(80) + commit(failed) + commit(partial) + commit({ ...initialParent, children: [child('stable')] }) + const freshParent = { ...initialParent, children: [child('d'), child('stable')] } + commit(freshParent) + expect(parentRows).toBe(83) + repositories.add('p/d') + await act(() => vi.advanceTimersByTimeAsync(600_000)) + expect(probes).toEqual([]) + expect(parentRows).toBe(83) + expect(unrelatedRows).toBe(0) + expect(unrelatedMetadata).toBe(0) + fixture.rerender({ visible: true }) + await act(() => vi.advanceTimersByTimeAsync(4000)) + expect(probes.flat()).toEqual(['p/d']) + expect(fixture.result.current.sources.map((source) => source.path)).toEqual(['p/d']) + expect(parentRows).toBe(85) + + // Even a return to the identical parent object cannot restore stable's old proof. + fixture.rerender({ visible: false }) + probes.length = 0 + parentRows = 0 + commit({ ...freshParent, children: [child('d')] }) + commit(freshParent) + repositories.add('p/stable') + expect(parentRows).toBe(2) + expect(probes).toEqual([]) + fixture.rerender({ visible: true }) + await act(() => vi.advanceTimersByTimeAsync(4000)) + expect(probes.flat().sort()).toEqual(['p', 'p/stable']) + expect(fixture.result.current.sources.map((source) => source.path).sort()).toEqual([ + 'p/d', + 'p/stable' + ]) + expect(unrelatedRows).toBe(0) + expect(unrelatedMetadata).toBe(0) + fixture.unmount() + expect(vi.getTimerCount()).toBe(0) + } +) diff --git a/src/renderer/src/components/right-sidebar/file-explorer/tree/use-file-explorer-repositories.ts b/src/renderer/src/components/right-sidebar/file-explorer/tree/use-file-explorer-repositories.ts index cdcf0e59129..4aabc7c19f6 100644 --- a/src/renderer/src/components/right-sidebar/file-explorer/tree/use-file-explorer-repositories.ts +++ b/src/renderer/src/components/right-sidebar/file-explorer/tree/use-file-explorer-repositories.ts @@ -93,7 +93,9 @@ export function useFileExplorerRepositories( const model = new ExplorerRepositoryFrontier(root, JSON.parse(ownerKey), { apply: (changes, affected, retains) => instance.updateFrontier(changes, affected, retains), invalidate: (path) => instance.invalidateCandidate(path), - resetProof: () => instance.invalidate() + resetProof: () => instance.invalidate(), + branches: (path) => instance.proofBranches(path), + invalidateSubtree: (path) => instance.invalidateProofSubtree(path) }) frontier.current = model applyView(model)