Skip to content

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

TypeScript

fix(desktop): r7-m2 purge previews on session expiry

Josué3baa9211cc4f
diff --git a/desktop-app/ui/src/App.tsx b/desktop-app/ui/src/App.tsx
index 5f25349de..f5b8f22d7 100644
--- a/desktop-app/ui/src/App.tsx
+++ b/desktop-app/ui/src/App.tsx
@@ -39,7 +39,11 @@ import { useWindowFocusBridge } from '@hooks/useWindowFocusBridge'
import type { ChatLocalMatch } from '@lib/chatLocalSearch'
import { buildLoadedChatSemanticModels } from '@lib/chatMessageSemantics'
import { EntityChangeRegistry } from '@lib/entityChangeRegistry'
-import { authoritativeGfsStatus, shouldRevalidateGfsQuery } from '@lib/gfsEntityChangeState'
+import {
+ authoritativeGfsStatus,
+ expireGfsPreviewTabs,
+ shouldRevalidateGfsQuery,
+} from '@lib/gfsEntityChangeState'
import { resolveGfsPreview } from '@lib/gfsPreview'
import { desktopQueryClient } from '@lib/queryClient'
import {
@@ -1014,6 +1018,28 @@ export function App() {
} catch (error) {
if (previewRefreshGenerationRef.current.get(gfsUri) !== generation) return
const status = authoritativeGfsStatus(error)
+ if (status === 401) {
+ // A 401 is session-wide, unlike a resource-scoped 403/404. Purge
+ // every open GFS preview and its cache immediately; never keep
+ // displaying bytes fetched under a rejected session.
+ for (const [uri, timer] of previewRetryTimersRef.current) {
+ window.clearTimeout(timer)
+ previewRefreshGenerationRef.current.set(
+ uri,
+ (previewRefreshGenerationRef.current.get(uri) ?? 0) + 1
+ )
+ }
+ previewRetryTimersRef.current.clear()
+ previewRetryAttemptRef.current.clear()
+ setWorkspaceTabs(expireGfsPreviewTabs)
+ setPluginGfsPreview(current =>
+ current && !current.unavailable
+ ? { ...current, unavailable: true, reloadVersion: current.reloadVersion + 1 }
+ : current
+ )
+ void queryClient.removeQueries({ queryKey: desktopQueryKeys.gfsRoot })
+ return
+ }
if (status === 403 || status === 404) {
setWorkspaceTabs(state =>
refreshPreviewTab(state, gfsUri, {
diff --git a/desktop-app/ui/src/__tests__/App.chatDrawer.test.tsx b/desktop-app/ui/src/__tests__/App.chatDrawer.test.tsx
index 4b1d9ebd6..55b0a0443 100644
--- a/desktop-app/ui/src/__tests__/App.chatDrawer.test.tsx
+++ b/desktop-app/ui/src/__tests__/App.chatDrawer.test.tsx
@@ -1388,6 +1388,42 @@ describe('App live GFS preview revalidation', () => {
expect(initial()?.kind === 'preview' && initial()?.preview?.unavailable).toBe(true)
)
})
+
+ it('purges every open GFS preview when the authenticated session expires', async () => {
+ currentController.workspaceTabs = openPreviewTab(currentController.workspaceTabs, {
+ id: 'preview-image',
+ title: 'diagram.png',
+ gfsUri: 'gfs://main/diagram',
+ fileKind: 'image',
+ byteLength: 128,
+ mimeType: 'image/png',
+ resourceVersion: 7,
+ })
+ const resolve = vi.mocked(window.clerum.gfs.resolve)
+ resolve.mockImplementation(uri => {
+ if (uri === 'gfs://main/readme') {
+ return Promise.reject(new Error('session expired httpStatus=401')) as never
+ }
+ return new Promise(() => {}) as never
+ })
+ render(<App />)
+ await waitFor(() => expect(dispatchEntityChange).toBeTypeOf('function'))
+
+ act(() =>
+ dispatchEntityChange?.({
+ type: 'scope.invalidated',
+ schemaVersion: 1,
+ cursor: 'cursor-expired',
+ scopes: ['authorization'],
+ })
+ )
+
+ await waitFor(() => {
+ const previews = currentController.workspaceTabs.tabs.filter(tab => tab.kind === 'preview')
+ expect(previews).toHaveLength(2)
+ expect(previews.every(tab => tab.preview?.unavailable)).toBe(true)
+ })
+ })
})
// Mini-spec 05: below the minimum panel width the drawer is SUPPRESSED (hidden,
diff --git a/desktop-app/ui/src/lib/gfsEntityChangeState.ts b/desktop-app/ui/src/lib/gfsEntityChangeState.ts
index dea05e06a..1a63aec44 100644
--- a/desktop-app/ui/src/lib/gfsEntityChangeState.ts
+++ b/desktop-app/ui/src/lib/gfsEntityChangeState.ts
@@ -1,4 +1,6 @@
import { parseHttpStatus } from './gfsGrantErrors'
+import { refreshPreviewTab } from './workspaceTabs'
+import type { WorkspaceTabsState } from './workspaceTabs.types'
/**
* Return only a transport-vetted status attached by the main-process GFS client.
@@ -14,3 +16,18 @@ export function authoritativeGfsStatus(error: unknown): number | undefined {
export function shouldRevalidateGfsQuery(queryKey: readonly unknown[]): boolean {
return queryKey[0] === 'desktop-app' && queryKey[1] === 'gfs' && queryKey[3] !== 'affordances'
}
+
+/** Purge every open GFS preview after the authenticated session is rejected. */
+export function expireGfsPreviewTabs(state: WorkspaceTabsState): WorkspaceTabsState {
+ const gfsUris = new Set(
+ state.tabs.flatMap(tab => (tab.kind === 'preview' && tab.preview ? [tab.preview.gfsUri] : []))
+ )
+ return Array.from(gfsUris).reduce(
+ (next, gfsUri) =>
+ refreshPreviewTab(next, gfsUri, {
+ status: 'unavailable',
+ shellTitle: 'File unavailable',
+ }),
+ state
+ )
+}