Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop-app): r4-h1 ignore stale folder refreshes

Josué88121f9b4d89
diff --git a/desktop-app/ui/src/gfs/__fixtures__/gfsProducerFixtures.ts b/desktop-app/ui/src/gfs/__fixtures__/gfsProducerFixtures.ts
index 20a7fbc9a..3d56c7b96 100644
--- a/desktop-app/ui/src/gfs/__fixtures__/gfsProducerFixtures.ts
+++ b/desktop-app/ui/src/gfs/__fixtures__/gfsProducerFixtures.ts
@@ -248,3 +248,29 @@ export function resolvedFile(
...overrides,
}
}
+
+/** A `ResolvedGfsResource` for an openable directory. */
+export function resolvedDirectory(
+ resourceId: string,
+ name: string,
+ overrides: Partial<ResolvedGfsResource> = {}
+): ResolvedGfsResource {
+ return {
+ resourceId,
+ rid: resourceId,
+ gfsUri: `gfs://main/${resourceId}`,
+ drive: 'main',
+ parentResourceId: null,
+ name,
+ kind: 'directory',
+ path: `/${name}`,
+ version: 1,
+ ...overrides,
+ }
+}
+
+/** Resolve a resource through the real GfsClient and structured-clone IPC seam. */
+export async function resolveResource(resource: ResolvedGfsResource): Promise<ResolvedGfsResource> {
+ const client = new GfsClient(stubTransport({ ok: true, data: resource }))
+ return structuredClone(await client.resolveUri(resource.gfsUri, SESSION_TOKEN))
+}
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/useGfsBrowserController.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/useGfsBrowserController.test.tsx
index a115aa76e..94c9790c9 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/useGfsBrowserController.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/useGfsBrowserController.test.tsx
@@ -5,6 +5,13 @@ import { AuthContext, type AuthContextValue } from '@contexts/AuthContext'
import { QueryClient, QueryClientProvider, focusManager } from '@tanstack/react-query'
import { act, cleanup, render, screen, waitFor } from '@testing-library/react'
import { desktopQueryDefaults } from '@lib/queryClient'
+import {
+ childView,
+ listChildrenPage,
+ resolveDeniedMessage,
+ resolveResource,
+ resolvedDirectory,
+} from '@/gfs/__fixtures__/gfsProducerFixtures'
import { desktopQueryKeys } from '../queryKeys'
import { useGfsBrowserController } from '../useGfsBrowserController'
@@ -126,9 +133,17 @@ function Probe() {
crumb {crumb.name}
</button>
))}
+ {ctrl.items.map(item => (
+ <button key={item.resourceId} type="button" onClick={() => ctrl.openChild(item)}>
+ child {item.name}
+ </button>
+ ))}
<button type="button" onClick={() => swallow(ctrl.openUri('gfs://main/root'))}>
open
</button>
+ <button type="button" onClick={() => swallow(ctrl.refreshCurrentLocation())}>
+ refresh current location
+ </button>
<button type="button" onClick={() => swallow(ctrl.refreshAffordances())}>
refresh permissions
</button>
@@ -290,6 +305,68 @@ describe('useGfsBrowserController', () => {
expect(lastHarnessQueryClient?.getQueryData(grantsKey)).toBeUndefined()
})
+ it.each(['success', 'denial'] as const)(
+ 'keeps a newly opened child folder when an older refresh completes with %s',
+ async completion => {
+ const folderA = await resolveResource(
+ resolvedDirectory('folder-a', 'Folder A', { gfsUri: 'gfs://main/root' })
+ )
+ const folderAChildren = await listChildrenPage([
+ childView('folder-b', 'Folder B', 'directory', { parentResourceId: 'folder-a' }),
+ ])
+ let resolveRefresh!: (resource: typeof folderA) => void
+ let rejectRefresh!: (reason?: unknown) => void
+ const deferredRefresh = new Promise<typeof folderA>((resolve, reject) => {
+ resolveRefresh = resolve
+ rejectRefresh = reject
+ })
+ const resolve = vi
+ .fn()
+ .mockResolvedValueOnce(folderA)
+ .mockImplementationOnce(() => deferredRefresh)
+
+ Object.defineProperty(window, 'clerum', {
+ configurable: true,
+ value: {
+ gfs: {
+ resolve,
+ listChildren: vi.fn(async (resourceId: string) =>
+ resourceId === 'folder-a' ? folderAChildren : { items: [], nextCursor: null }
+ ),
+ affordances: vi.fn(async () => ({
+ held: [],
+ canDelegate: false,
+ grantableBits: [],
+ canCreateShare: false,
+ })),
+ },
+ },
+ })
+
+ render(<Probe />, { wrapper: Harness })
+ await act(async () => screen.getByRole('button', { name: 'open' }).click())
+ await waitFor(() => expect(screen.getByTestId('current').textContent).toBe('folder-a'))
+ await screen.findByRole('button', { name: 'child Folder B' })
+
+ screen.getByRole('button', { name: 'refresh current location' }).click()
+ await waitFor(() => expect(resolve).toHaveBeenCalledTimes(2))
+ screen.getByRole('button', { name: 'child Folder B' }).click()
+ await waitFor(() => expect(screen.getByTestId('current').textContent).toBe('folder-b'))
+
+ if (completion === 'success') {
+ await act(async () => resolveRefresh(folderA))
+ } else {
+ const denied = await resolveDeniedMessage('gfs://main/root')
+ await act(async () => rejectRefresh(new Error(denied)))
+ }
+
+ await waitFor(() => {
+ expect(screen.getByTestId('current').textContent).toBe('folder-b')
+ expect(screen.getByTestId('crumbs').textContent).toBe('Folder A / Folder B')
+ })
+ }
+ )
+
it('exposes the presented verdict for a failed affordances read, not the IPC wrapper', async () => {
// Every rejection that crosses ipcRenderer.invoke arrives wrapped in
// "Error invoking remote method '<channel>': ", which names our own
diff --git a/desktop-app/ui/src/hooks/domain/useGfsBrowserController.ts b/desktop-app/ui/src/hooks/domain/useGfsBrowserController.ts
index 3551d60d4..69fe860be 100644
--- a/desktop-app/ui/src/hooks/domain/useGfsBrowserController.ts
+++ b/desktop-app/ui/src/hooks/domain/useGfsBrowserController.ts
@@ -1,4 +1,5 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
+import type { Dispatch, SetStateAction } from 'react'
import { useAuthContext } from '@contexts/AuthContext'
import {
useInfiniteQuery,
@@ -299,10 +300,19 @@ export function useGfsBrowserController(options: GfsBrowserControllerOptions = {
const { grantsListEnabled = false } = options
const queryClient = useQueryClient()
const { isAuthenticated, me, runtimeConfigState } = useAuthContext()
- const [crumbs, setCrumbs] = useState<GfsCrumb[]>([])
+ const [crumbs, setCrumbsState] = useState<GfsCrumb[]>([])
const [openError, setOpenError] = useState<string | null>(null)
const [resolving, setResolving] = useState(false)
const openUriGenerationRef = useRef(0)
+ // Any browser-location update supersedes an in-flight URI resolution. This
+ // includes navigation through the tree/crumbs as well as metadata updates,
+ // so an older refresh (including a late 403/404) cannot overwrite a newer
+ // location or clear its state.
+ const setCrumbs = useCallback<Dispatch<SetStateAction<GfsCrumb[]>>>(next => {
+ openUriGenerationRef.current += 1
+ setResolving(false)
+ setCrumbsState(next)
+ }, [])
const previousSessionScopeRef = useRef<string | null>(null)
// Per controller-mount timestamp: discovery (`refetchOnMount: 'always'`) must
// land a response newer than this before cached GFS state may render again.
@@ -938,7 +948,8 @@ export function useGfsBrowserController(options: GfsBrowserControllerOptions = {
}
}
- setCrumbs([...ancestors.reverse(), crumb])
+ if (openUriGenerationRef.current !== generation) return false
+ setCrumbsState([...ancestors.reverse(), crumb])
return crumb
} catch (error) {
if (openUriGenerationRef.current !== generation) return false
@@ -952,7 +963,7 @@ export function useGfsBrowserController(options: GfsBrowserControllerOptions = {
if (!handleAuthorityFailure(message, 'operation')) {
const status = parseHttpStatus(message)
if (options?.clearIfUnavailable && (status === 403 || status === 404)) {
- setCrumbs([])
+ setCrumbsState([])
setOpenError(null)
void queryClient.removeQueries({ queryKey: desktopQueryKeys.gfsRoot })
} else {