Skip to content

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

TypeScript

fix(control-ui): r7-m8 keep stream reads newer than background loads

Codex31a94b61b597
diff --git a/control-ui/components/GfsBrowser.tsx b/control-ui/components/GfsBrowser.tsx
index 8d777bda6..413ed3111 100644
--- a/control-ui/components/GfsBrowser.tsx
+++ b/control-ui/components/GfsBrowser.tsx
@@ -808,6 +808,9 @@ export function GfsBrowser(): React.JSX.Element {
}
performVisibleStateRevalidation = async (cursor?: string) => {
+ // Retire an older same-folder focus/cache refresh before publishing the
+ // newer stream-authoritative listing. Its late response must not win.
+ backgroundLoadSeqRef.current += 1
// A committed remote change supersedes any local move/retry ancestry
// reconstruction still in flight. Its older response must not replace
// the hierarchy we are about to refetch from the authoritative API.
diff --git a/control-ui/components/__tests__/GfsBrowser.revalidation.integration.test.tsx b/control-ui/components/__tests__/GfsBrowser.revalidation.integration.test.tsx
new file mode 100644
index 000000000..35221f442
--- /dev/null
+++ b/control-ui/components/__tests__/GfsBrowser.revalidation.integration.test.tsx
@@ -0,0 +1,146 @@
+// @vitest-environment jsdom
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
+import { GfsBrowser } from '../GfsBrowser'
+import { ToastProvider } from '../Toast'
+
+function jsonResponse(value: unknown): Response {
+ return new Response(JSON.stringify(value), {
+ status: 200,
+ headers: { 'content-type': 'application/json' },
+ })
+}
+
+function child(resourceId: string, rid: string, name: string, kind: string) {
+ return {
+ resourceId,
+ rid,
+ gfsUri: `gfs://main/${rid}`,
+ name,
+ kind,
+ path: `/${name}`,
+ bytes: 0,
+ version: 1,
+ }
+}
+
+describe('GfsBrowser authoritative revalidation integration', () => {
+ afterEach(() => {
+ cleanup()
+ vi.unstubAllGlobals()
+ vi.restoreAllMocks()
+ })
+
+ it('does not publish a stale background page after the newer stream read', async () => {
+ const work = child('folder-1', 'rid-folder-1', 'work', 'directory')
+ const staleRow = child('stale-file', 'rid-stale', 'stale.txt', 'file')
+ const currentRow = child('current-file', 'rid-current', 'current.txt', 'file')
+ const folderPath = '/control-api/api/v1/gfs/resources/folder-1/children'
+ const stalePage = { items: [staleRow], nextCursor: null }
+ const currentPage = { items: [currentRow], nextCursor: null }
+ let finishStaleRead!: (response: Response) => void
+ const staleRead = new Promise<Response>(resolve => {
+ finishStaleRead = resolve
+ })
+ let childReads = 0
+ let folderOpened = false
+ let staleBackgroundStarted = false
+ const streamControllers: ReadableStreamDefaultController<Uint8Array>[] = []
+ 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')) {
+ const body = new ReadableStream<Uint8Array>({
+ start(controller) {
+ streamControllers.push(controller)
+ init?.signal?.addEventListener('abort', () => controller.close(), { once: true })
+ },
+ })
+ return new Response(body, {
+ 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) {
+ childReads += 1
+ if (!folderOpened) return jsonResponse(stalePage)
+ if (!staleBackgroundStarted) {
+ staleBackgroundStarted = true
+ return staleRead
+ }
+ return jsonResponse(currentPage)
+ }
+ if (url.pathname.endsWith('/api/v1/gfs/resolve')) {
+ return jsonResponse({
+ resourceId: 'folder-1',
+ rid: 'rid-folder-1',
+ gfsUri: 'gfs://main/rid-folder-1',
+ name: 'work',
+ kind: 'directory',
+ path: '/work',
+ version: 1,
+ })
+ }
+ if (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(streamControllers).toHaveLength(1))
+ await screen.findByRole('button', { name: 'work' })
+ fireEvent.click(screen.getByRole('button', { name: 'work' }))
+ await screen.findByRole('button', { name: 'stale.txt' })
+ const breadcrumb = screen.getByRole('navigation', { name: 'Breadcrumb' })
+ fireEvent.click(breadcrumb.querySelector('button')!)
+ await screen.findByRole('button', { name: 'work' })
+ folderOpened = true
+ fireEvent.click(screen.getByRole('button', { name: 'work' }))
+ await screen.findByRole('button', { name: 'stale.txt' })
+ await waitFor(() => expect(staleBackgroundStarted).toBe(true))
+
+ await act(async () => {
+ streamControllers[0]!.enqueue(
+ new TextEncoder().encode(
+ `${JSON.stringify({
+ schemaVersion: 1,
+ type: 'scope.invalidated',
+ cursor: 'd119f895-1ef8-4e73-8f08-f9754919682a',
+ scopes: ['gfs'],
+ })}\n`
+ )
+ )
+ })
+ await screen.findByRole('button', { name: 'current.txt' })
+ await waitFor(() => expect(childReads).toBeGreaterThan(2))
+
+ await act(async () => finishStaleRead(jsonResponse(stalePage)))
+ await waitFor(() => expect(screen.getByRole('button', { name: 'current.txt' })).toBeVisible())
+ expect(screen.queryByRole('button', { name: 'stale.txt' })).toBeNull()
+ })
+})
diff --git a/control-ui/package-lock.json b/control-ui/package-lock.json
index 28650c087..a4cf48932 100644
--- a/control-ui/package-lock.json
+++ b/control-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-ui",
- "version": "0.1.671",
+ "version": "0.1.672",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-ui",
- "version": "0.1.671",
+ "version": "0.1.672",
"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 1feab1ab3..5f95c6e06 100644
--- a/control-ui/package.json
+++ b/control-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-ui",
- "version": "0.1.671",
+ "version": "0.1.672",
"private": true,
"scripts": {
"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",