Skip to content

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

TypeScript

fix(control-ui): replay pending stream refresh after paging (r8-m3)

Josuéc1c4598286bf
diff --git a/control-ui/components/GfsBrowser.tsx b/control-ui/components/GfsBrowser.tsx
index 135fd3223..e4e5e9bc4 100644
--- a/control-ui/components/GfsBrowser.tsx
+++ b/control-ui/components/GfsBrowser.tsx
@@ -346,6 +346,9 @@ export function GfsBrowser(): React.JSX.Element {
const loadArbiterRef = useRef(new GfsLoadArbiter())
const entityChangeRefetchControllerRef = useRef<AbortController | null>(null)
const loadedPageCountRef = useRef(1)
+ const foregroundLoadsInFlightRef = useRef(0)
+ const pendingStreamRevalidationRef = useRef(false)
+ const requestStreamRevalidationRef = useRef<(cursor?: string) => void>(() => undefined)
const loadedLocationRef = useRef<string | null | undefined>(undefined)
// Operator selects a resource to delegate access on (grant panel).
const [selected, setSelected] = useState<GfsChild | null>(null)
@@ -479,10 +482,15 @@ export function GfsBrowser(): React.JSX.Element {
const appending = Boolean(cursor)
const background = Boolean(options?.background)
if (!background) {
+ foregroundLoadsInFlightRef.current += 1
// User navigation or a foreground mutation refresh owns the next
// visible result. Cancel stream-triggered reads so an older response
// for the same folder cannot overwrite it.
- entityChangeRefetchControllerRef.current?.abort()
+ const activeStreamRefetch = entityChangeRefetchControllerRef.current
+ if (activeStreamRefetch) {
+ activeStreamRefetch.abort()
+ pendingStreamRevalidationRef.current = true
+ }
entityChangeRefetchControllerRef.current = null
}
const loadArbiter = loadArbiterRef.current
@@ -566,6 +574,13 @@ export function GfsBrowser(): React.JSX.Element {
setError(err instanceof Error ? err.message : 'Failed to load EvenDrive')
}
} finally {
+ if (!background) {
+ foregroundLoadsInFlightRef.current = Math.max(0, foregroundLoadsInFlightRef.current - 1)
+ if (foregroundLoadsInFlightRef.current === 0 && pendingStreamRevalidationRef.current) {
+ pendingStreamRevalidationRef.current = false
+ requestStreamRevalidationRef.current()
+ }
+ }
if (isCurrent()) {
if (appending) setLoadingMore(false)
else if (!background) setLoading(false)
@@ -699,6 +714,7 @@ export function GfsBrowser(): React.JSX.Element {
100
)
const invalidateVisibleState = (cursor?: string) => revalidationScheduler.request(cursor)
+ requestStreamRevalidationRef.current = invalidateVisibleState
const scheduleRecovery = () => {
if (!active || recoveryTimer) return
const delay = recoveryDelay
@@ -810,6 +826,14 @@ export function GfsBrowser(): React.JSX.Element {
}
performVisibleStateRevalidation = async (cursor?: string) => {
+ // Foreground navigation and pagination own their results. If a stream
+ // invalidation arrives while either is in flight, run one authoritative
+ // refresh after the user operation settles rather than superseding it or
+ // losing the invalidation.
+ if (foregroundLoadsInFlightRef.current > 0) {
+ pendingStreamRevalidationRef.current = true
+ return
+ }
// Retire an older same-folder focus/cache refresh before publishing the
// newer stream-authoritative listing. Its late response must not win.
loadArbiterRef.current.beginStreamRevalidation()
@@ -1137,6 +1161,7 @@ export function GfsBrowser(): React.JSX.Element {
revalidationScheduler.dispose()
entityChangeRefetchControllerRef.current?.abort()
entityChangeRefetchControllerRef.current = null
+ requestStreamRevalidationRef.current = () => undefined
if (retryTimer) clearTimeout(retryTimer)
if (recoveryTimer) clearTimeout(recoveryTimer)
}
diff --git a/control-ui/components/__tests__/GfsBrowser.revalidation.integration.test.tsx b/control-ui/components/__tests__/GfsBrowser.revalidation.integration.test.tsx
index a860dae38..84d407ea2 100644
--- a/control-ui/components/__tests__/GfsBrowser.revalidation.integration.test.tsx
+++ b/control-ui/components/__tests__/GfsBrowser.revalidation.integration.test.tsx
@@ -538,6 +538,116 @@ describe('GfsBrowser authoritative revalidation integration', () => {
expect(screen.getByRole('button', { name: 'first.txt' })).toBeVisible()
})
+ it('replays an invalidation canceled by Load more after pagination settles', async () => {
+ const work = child('folder-1', 'rid-folder-1', 'work', 'directory')
+ const first = child('file-1', 'rid-file-1', 'first.txt', 'file')
+ const second = child('file-2', 'rid-file-2', 'second.txt', 'file')
+ const remote = child('file-3', 'rid-file-3', 'remote.txt', 'file')
+ const folderPath = '/control-api/api/v1/gfs/resources/folder-1/children'
+ let finishPage!: (response: Response) => void
+ const pendingPage = new Promise<Response>(resolve => {
+ finishPage = resolve
+ })
+ let streamController: ReadableStreamDefaultController<Uint8Array> | null = null
+ let initialReads = 0
+ let streamRefreshReads = 0
+ let cursorReads = 0
+ let pageStarted = false
+ let folderOpened = false
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
+ const url = new URL(String(input), 'http://control-ui.test')
+ if (url.pathname.endsWith('/api/v1/gfs/entity-changes/stream')) {
+ return new Response(
+ new ReadableStream<Uint8Array>({
+ start(controller) {
+ streamController = controller
+ init?.signal?.addEventListener('abort', () => controller.close(), { once: true })
+ },
+ }),
+ { status: 200, headers: { 'content-type': 'application/x-ndjson' } }
+ )
+ }
+ if (url.pathname.endsWith('/api/v1/gfs/tree')) {
+ return jsonResponse({ rootResourceId: 'root-1', items: [work], nextCursor: null })
+ }
+ if (url.pathname === '/control-api/api/v1/gfs/resources/root-1/children') {
+ return jsonResponse({ items: [work], nextCursor: null })
+ }
+ if (url.pathname === folderPath) {
+ const cursor = url.searchParams.get('cursor')
+ if (cursor) {
+ cursorReads += 1
+ if (cursorReads === 1) {
+ pageStarted = true
+ return pendingPage
+ }
+ return jsonResponse({ items: [second], nextCursor: null })
+ }
+ if (!folderOpened || initialReads++ === 0) {
+ return jsonResponse({ items: [first], nextCursor: 'page-2' })
+ }
+ streamRefreshReads += 1
+ if (streamRefreshReads === 1) {
+ return new Promise<Response>((_resolve, reject) => {
+ init?.signal?.addEventListener(
+ 'abort',
+ () => reject(Object.assign(new Error('aborted'), { name: 'AbortError' })),
+ { once: true }
+ )
+ })
+ }
+ return jsonResponse({ items: [first, remote], nextCursor: 'page-2' })
+ }
+ if (
+ url.pathname.endsWith('/api/v1/gfs/resolve') ||
+ url.pathname.endsWith('/api/v1/gfs/by-path')
+ ) {
+ return jsonResponse({
+ resourceId: 'folder-1',
+ rid: 'rid-folder-1',
+ gfsUri: 'gfs://main/rid-folder-1',
+ name: 'work',
+ kind: 'directory',
+ path: '/work',
+ version: 1,
+ })
+ }
+ return jsonResponse({ items: [], nextCursor: null })
+ })
+ )
+
+ render(
+ <ToastProvider>
+ <GfsBrowser />
+ </ToastProvider>
+ )
+ await waitFor(() => expect(streamController).not.toBeNull())
+ await screen.findByRole('button', { name: 'work' })
+ folderOpened = true
+ fireEvent.click(screen.getByRole('button', { name: 'work' }))
+ await screen.findByRole('button', { name: 'first.txt' })
+ await waitFor(() => expect(initialReads).toBeGreaterThan(0))
+
+ const producerFrame = await controlApiProducerFrame()
+ await act(async () => {
+ streamController!.enqueue(new TextEncoder().encode(`${producerFrame}\n`))
+ })
+ await waitFor(() => expect(streamRefreshReads).toBe(1))
+
+ fireEvent.click(await screen.findByRole('button', { name: 'Load more' }))
+ await waitFor(() => expect(pageStarted).toBe(true))
+ expect(screen.getByRole('button', { name: 'first.txt' })).toBeVisible()
+ await act(async () => finishPage(jsonResponse({ items: [second], nextCursor: null })))
+ await screen.findByRole('button', { name: 'second.txt' })
+
+ await screen.findByRole('button', { name: 'remote.txt' }, { timeout: 2_000 })
+ expect(streamRefreshReads).toBe(2)
+ expect(screen.getByRole('button', { name: 'first.txt' })).toBeVisible()
+ expect(screen.getByRole('button', { name: 'second.txt' })).toBeVisible()
+ })
+
it('recovers the rendered list after a real API-client stream refresh fails once', async () => {
const existingFile = child('file-1', 'rid-file-1', 'existing.txt', 'file')
const remoteFolder = child('folder-2', 'rid-folder-2', 'remote-folder', 'directory')
diff --git a/control-ui/package-lock.json b/control-ui/package-lock.json
index 361b54b6f..1cc1a37ed 100644
--- a/control-ui/package-lock.json
+++ b/control-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-ui",
- "version": "0.1.692",
+ "version": "0.1.693",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-ui",
- "version": "0.1.692",
+ "version": "0.1.693",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/frontend-components": "file:../packages/frontend-components",
diff --git a/control-ui/package.json b/control-ui/package.json
index d65a65f31..18d73d4fd 100644
--- a/control-ui/package.json
+++ b/control-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-ui",
- "version": "0.1.692",
+ "version": "0.1.693",
"private": true,
"scripts": {
"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",