Skip to content

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

TypeScript

fix(desktop-app): r8-m10 serialize GFS revalidation

Josuéa8f4d414185e
diff --git a/desktop-app/ui/src/App.tsx b/desktop-app/ui/src/App.tsx
index cf88c27bb..b8c16c1fa 100644
--- a/desktop-app/ui/src/App.tsx
+++ b/desktop-app/ui/src/App.tsx
@@ -1103,24 +1103,51 @@ export function App() {
if (typeof entityChangeBridge?.subscribe !== 'function') return
let active = true
let stopSubscription: (() => Promise<void>) | null = null
- const unsubscribeGfs = entityChangeRegistry.subscribe(['gfs', 'authorization'], () => {
+ let gfsRevalidationInFlight = false
+ let gfsRevalidationPending = false
+ const gfsQueryFilter = {
+ queryKey: desktopQueryKeys.gfsRoot,
+ predicate: (query: { queryKey: readonly unknown[] }) =>
+ shouldRevalidateGfsQuery(query.queryKey),
+ }
+ const revalidateGfs = () => {
+ if (!active) return
+ if (gfsRevalidationInFlight) {
+ gfsRevalidationPending = true
+ return
+ }
+ gfsRevalidationInFlight = true
+ const foregroundReadWasActive = queryClient.isFetching(gfsQueryFilter) > 0
setRemoteGfsChangeEpoch(epoch => epoch + 1)
+ void queryClient
+ .invalidateQueries({ ...gfsQueryFilter, refetchType: 'active' }, { cancelRefetch: false })
+ .then(async () => {
+ const resources = new Set(
+ workspaceTabsRef.current.tabs
+ .filter(tab => tab.kind === 'preview' && tab.preview)
+ .map(tab => tab.preview!.gfsUri)
+ )
+ const pluginPreview = pluginGfsPreviewRef.current
+ if (pluginPreview) resources.add(pluginPreview.gfsUri)
+ await Promise.all(Array.from(resources, uri => refreshOpenPreview(uri)))
+ })
+ .finally(() => {
+ gfsRevalidationInFlight = false
+ if (!active) return
+ if (gfsRevalidationPending || foregroundReadWasActive) {
+ gfsRevalidationPending = false
+ revalidateGfs()
+ }
+ })
+ }
+ const unsubscribeGfs = entityChangeRegistry.subscribe(['gfs', 'authorization'], () => {
// Scope invalidations are soft convergence hints, not proof that cached
// data is no longer authorized. Keep visible/paginated rows while active
// queries refetch; only an authoritative 403/404 purges a preview.
- void queryClient.invalidateQueries({
- queryKey: desktopQueryKeys.gfsRoot,
- predicate: query => shouldRevalidateGfsQuery(query.queryKey),
- refetchType: 'active',
- })
- const resources = new Set(
- workspaceTabsRef.current.tabs
- .filter(tab => tab.kind === 'preview' && tab.preview)
- .map(tab => tab.preview!.gfsUri)
- )
- const pluginPreview = pluginGfsPreviewRef.current
- if (pluginPreview) resources.add(pluginPreview.gfsUri)
- for (const gfsUri of resources) void refreshOpenPreview(gfsUri)
+ // TanStack's default refetch cancels an active request. Serialize these
+ // soft passes and retain one trailing pass so a second frame cannot abort
+ // a foreground navigation/page chain or lose a mutation during that read.
+ revalidateGfs()
})
void entityChangeBridge
.subscribe(event => entityChangeRegistry.dispatch(event))
diff --git a/desktop-app/ui/src/__tests__/App.chatDrawer.test.tsx b/desktop-app/ui/src/__tests__/App.chatDrawer.test.tsx
index f24c2d997..5121b3285 100644
--- a/desktop-app/ui/src/__tests__/App.chatDrawer.test.tsx
+++ b/desktop-app/ui/src/__tests__/App.chatDrawer.test.tsx
@@ -1447,6 +1447,78 @@ describe('App live GFS preview revalidation', () => {
expect(measuredReads).toBeLessThanOrEqual(32)
})
+ it('queues a second invalidation instead of canceling a multi-page refresh', async () => {
+ let finishFirstPage!: (page: {
+ items: Array<{ name: string }>
+ nextCursor: string | null
+ }) => void
+ let readSequence = 0
+ const listChildren = vi.fn((_id: string, _drive?: string, cursor?: string) => {
+ readSequence += 1
+ if (readSequence === 1) {
+ return new Promise(resolve => {
+ finishFirstPage = resolve
+ })
+ }
+ return Promise.resolve({
+ items: [{ name: `${cursor ? 'second' : 'first'}-latest` }],
+ nextCursor: cursor ? null : 'page-2',
+ })
+ })
+ Object.assign(window.clerum.gfs, { listChildren })
+ const queryKey = desktopQueryKeys.gfsChildren('session', 'folder', 'main')
+ desktopQueryClient.setQueryData(queryKey, {
+ pages: [
+ { items: [{ name: 'first-old' }], nextCursor: 'page-2' },
+ { items: [{ name: 'second-old' }], nextCursor: null },
+ ],
+ pageParams: [undefined, 'page-2'],
+ })
+
+ function FolderRows() {
+ const query = useInfiniteQuery({
+ queryKey,
+ queryFn: ({ pageParam }) => window.clerum.gfs.listChildren('folder', 'main', pageParam),
+ initialPageParam: undefined as string | undefined,
+ getNextPageParam: page => page.nextCursor ?? undefined,
+ })
+ return (
+ <output data-testid="folder-rows">
+ {(query.data?.pages ?? [])
+ .flatMap(page => page.items)
+ .map(row => row.name)
+ .join(',')}
+ </output>
+ )
+ }
+
+ render(
+ <QueryClientProvider client={desktopQueryClient}>
+ <FolderRows />
+ <App />
+ </QueryClientProvider>
+ )
+ await waitFor(() => expect(dispatchEntityChange).toBeTypeOf('function'))
+
+ act(() => dispatchEntityChange?.({ ...USER_SCOPE_INVALIDATED }))
+ await waitFor(() => expect(listChildren).toHaveBeenCalledTimes(1))
+ act(() => dispatchEntityChange?.({ ...USER_SCOPE_INVALIDATED }))
+
+ // The event may mark this read stale, but it cannot cancel/restart the user's
+ // active page-chain read while the authoritative response is still pending.
+ expect(listChildren).toHaveBeenCalledTimes(1)
+ expect(screen.getByTestId('folder-rows').textContent).toBe('first-old,second-old')
+
+ await act(async () => {
+ finishFirstPage({ items: [{ name: 'first-latest' }], nextCursor: 'page-2' })
+ await Promise.resolve()
+ })
+ await waitFor(() =>
+ expect(screen.getByTestId('folder-rows').textContent).toBe('first-latest,second-latest')
+ )
+ expect(listChildren).toHaveBeenCalledTimes(4)
+ })
+
it('preserves an unchanged preview during soft scope revalidation and purges only on 403', async () => {
const resolve = vi.mocked(window.clerum.gfs.resolve)
const invalidateQueries = vi