Skip to content

The agent that grows with you

fix(desktop): stop the project tree strobing while it re-probes an unreadable root

Brooklyn Nicholson2a36a7157807
diff --git a/apps/desktop/src/app/right-sidebar/files/use-project-tree.test.ts b/apps/desktop/src/app/right-sidebar/files/use-project-tree.test.ts
index 21c7677860c..a42e6636615 100644
--- a/apps/desktop/src/app/right-sidebar/files/use-project-tree.test.ts
+++ b/apps/desktop/src/app/right-sidebar/files/use-project-tree.test.ts
@@ -462,4 +462,97 @@ describe('useProjectTree', () => {
await waitFor(() => expect(result.current.rootError).toBe('no-bridge'))
expect(result.current.data).toEqual([])
})
+
+ // An unreadable root self-heals on a 3s timer, so this probe runs forever
+ // while the pane just sits there. Blanking the error first made every one of
+ // those a visible "unreadable" → blank → "unreadable" strobe.
+ it('keeps an unreadable root on screen while it re-probes', async () => {
+ readDir.mockResolvedValue({ entries: [], error: 'ENOENT' })
+
+ const { result } = renderHook(() => useProjectTree('/gone'))
+
+ await waitFor(() => expect(result.current.rootError).toBe('ENOENT'))
+
+ let releaseProbe: ((value: HermesReadDirResult) => void) | undefined
+
+ readDir.mockImplementationOnce(
+ () =>
+ new Promise<HermesReadDirResult>(resolve => {
+ releaseProbe = resolve
+ })
+ )
+
+ act(() => {
+ void result.current.refreshRoot()
+ })
+
+ expect(result.current.rootLoading).toBe(true)
+ expect(result.current.rootError).toBe('ENOENT')
+
+ await act(async () => {
+ releaseProbe?.({ entries: [], error: 'ENOENT' })
+ })
+
+ expect(result.current.rootError).toBe('ENOENT')
+ })
+
+ it('keeps loaded rows on screen while the root refreshes', async () => {
+ readDir.mockResolvedValueOnce(ok([{ name: 'src', path: '/p/src', isDirectory: true }]))
+
+ const { result } = renderHook(() => useProjectTree('/p'))
+
+ await waitFor(() => expect(result.current.data.length).toBe(1))
+
+ let releaseRefresh: ((value: HermesReadDirResult) => void) | undefined
+
+ readDir.mockImplementationOnce(
+ () =>
+ new Promise<HermesReadDirResult>(resolve => {
+ releaseRefresh = resolve
+ })
+ )
+
+ act(() => {
+ void result.current.refreshRoot()
+ })
+
+ expect(result.current.rootLoading).toBe(true)
+ expect(result.current.data.map(node => node.name)).toEqual(['src'])
+
+ await act(async () => {
+ releaseRefresh?.(ok([{ name: 'src', path: '/p/src', isDirectory: true }]))
+ })
+ })
+
+ it('clears the rows when the same path is re-read from another backend', async () => {
+ readDir.mockResolvedValueOnce(ok([{ name: 'from-a', path: '/shared/from-a', isDirectory: false }]))
+ $connection.set({ baseUrl: 'local-a', connectionId: 'connection-a', mode: 'local', profile: 'default' } as never)
+
+ const { result } = renderHook(() => useProjectTree('/shared'))
+
+ await waitFor(() => expect(result.current.data.map(node => node.name)).toEqual(['from-a']))
+
+ let releaseFromB: ((value: HermesReadDirResult) => void) | undefined
+
+ readDir.mockImplementationOnce(
+ () =>
+ new Promise<HermesReadDirResult>(resolve => {
+ releaseFromB = resolve
+ })
+ )
+
+ act(() => {
+ $connection.set({ baseUrl: 'local-b', connectionId: 'connection-b', mode: 'local', profile: 'default' } as never)
+ })
+
+ // The path is unchanged but the machine is not, so the old machine's
+ // listing must not sit there while the new one loads.
+ expect(result.current.data).toEqual([])
+
+ await act(async () => {
+ releaseFromB?.(ok([{ name: 'from-b', path: '/shared/from-b', isDirectory: false }]))
+ })
+
+ await waitFor(() => expect(result.current.data.map(node => node.name)).toEqual(['from-b']))
+ })
})
diff --git a/apps/desktop/src/app/right-sidebar/files/use-project-tree.ts b/apps/desktop/src/app/right-sidebar/files/use-project-tree.ts
index c36a6aa7800..13ee0f7c1aa 100644
--- a/apps/desktop/src/app/right-sidebar/files/use-project-tree.ts
+++ b/apps/desktop/src/app/right-sidebar/files/use-project-tree.ts
@@ -182,7 +182,11 @@ async function fallbackRootFor(cwd: string, sourceIsRemote: boolean): Promise<st
async function loadRoot(
cwd: string,
- { connectionKey = desktopFsCacheKey(), force = false }: { connectionKey?: string; force?: boolean } = {}
+ {
+ connectionKey = desktopFsCacheKey(),
+ force = false,
+ reset = false
+ }: { connectionKey?: string; force?: boolean; reset?: boolean } = {}
) {
if (!cwd) {
clearProjectTree()
@@ -204,15 +208,25 @@ async function loadRoot(
clearProjectDirCache(cwd)
}
+ // Re-reading the SAME root — the error retry below, the refresh button, a
+ // workspace revalidation — must probe underneath what is on screen rather
+ // than blanking it first. Clearing here meant an unreadable root strobed
+ // "unreadable" → blank → "unreadable" every retry, forever, while the app
+ // just sat there; the header's project name flickered with it as
+ // `resolvedCwd` dropped back to the raw cwd and returned. Only a different
+ // root (or a different backend, via `reset`) may clear, where the previous
+ // project's tree would otherwise linger under the new one.
+ const keepVisible = current.cwd === cwd && !reset
+
$projectTree.set({
collapseNonce: current.collapseNonce,
cwd,
- data: [],
+ data: keepVisible ? current.data : [],
loaded: false,
- openState: current.cwd === cwd ? current.openState : {},
+ openState: keepVisible ? current.openState : {},
requestId,
- resolvedCwd: '',
- rootError: null,
+ resolvedCwd: keepVisible ? current.resolvedCwd : '',
+ rootError: keepVisible ? current.rootError : null,
rootLoading: true
})
@@ -465,7 +479,9 @@ export function useProjectTree(cwd: string): UseProjectTreeResult {
if (connectionChanged) {
clearProjectDirCache()
- void loadRoot(cwd, { connectionKey, force: true })
+ // Same path, different machine: what is on screen was read from the old
+ // backend, so it has to go even though the cwd string is unchanged.
+ void loadRoot(cwd, { connectionKey, force: true, reset: true })
return
}