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-l7 preserve trail on ancestor lookup failure

Josué887dc3bce38e
diff --git a/control-ui/components/GfsBrowser.tsx b/control-ui/components/GfsBrowser.tsx
index ca5ed2a68..31b947ffb 100644
--- a/control-ui/components/GfsBrowser.tsx
+++ b/control-ui/components/GfsBrowser.tsx
@@ -53,6 +53,7 @@ import {
createGfsUploadJob,
uploadGfsFileLegacy,
} from '@lib/gfsFileUpload'
+import { resolveGfsHierarchy } from '@lib/gfsHierarchyRevalidation'
import { gfsImagePreviewMimeType } from '@lib/gfsImagePreview'
import { GfsLoadArbiter } from '@lib/gfsLoadArbitration'
import { isGfsMarkdownPreviewFile } from '@lib/gfsMarkdownPreview'
@@ -387,6 +388,7 @@ export function GfsBrowser(): React.JSX.Element {
const [trailRecovery, setTrailRecovery] = useState<{
resourceId: string
gfsUri: string
+ reason: 'move' | 'hierarchy'
} | null>(null)
// Monotonic epoch for breadcrumb-reconstruction attempts (R7-M1 race). A
// reconstruction (a move's rebuild or a Retry) captures the epoch at start
@@ -966,44 +968,21 @@ export function GfsBrowser(): React.JSX.Element {
const trailEpoch = trailReconstructionEpochRef.current
const hierarchyRefresh = (async () => {
const rootCrumb = { ...(crumbsRef.current[0] ?? { id: null, rid: null, name: '/' }) }
- let refreshed: Crumb[] = [rootCrumb]
- let retryHierarchy = false
- try {
- const resolved = (await apiGet(
- '/api/v1/gfs/resolve',
- { uri: `gfs://${DRIVE}/${visibleCrumb.rid ?? ridOfResourceId(visibleResourceId)}` },
- { signal }
- )) as GfsResolvedLocation
- if (resolved.kind !== 'directory' || !resolved.path?.startsWith('/')) {
- throw new Error('Current GFS folder is no longer available')
- }
- let path = ''
- for (const segment of resolved.path.split('/').filter(Boolean)) {
- path += `/${segment}`
- const ancestor = (await apiGet(
+ const hierarchy = await resolveGfsHierarchy({
+ readCurrent: () =>
+ apiGet(
+ '/api/v1/gfs/resolve',
+ { uri: `gfs://${DRIVE}/${visibleCrumb.rid ?? ridOfResourceId(visibleResourceId)}` },
+ { signal }
+ ) as Promise<GfsResolvedLocation>,
+ readAncestor: path =>
+ apiGet(
'/api/v1/gfs/by-path',
{ drive: DRIVE, path },
{ signal }
- )) as GfsResolvedLocation
- if (ancestor.kind !== 'directory') {
- throw new Error('GFS folder hierarchy changed during refresh')
- }
- refreshed.push(folderResourceToCrumb(ancestor))
- }
- if (refreshed[refreshed.length - 1]?.id !== resolved.resourceId) {
- throw new Error('GFS folder hierarchy changed during refresh')
- }
- } catch (error) {
- if (signal.aborted) return
- // Do not keep presenting the stale hierarchy if the current folder
- // was deleted, moved during resolution, or is no longer authorized.
- if (isTransientEntityChangeRefetchError(error)) {
- retryHierarchy = true
- scheduleRecovery()
- } else {
- refreshed = [rootCrumb]
- }
- }
+ ) as Promise<GfsResolvedLocation>,
+ isTransient: isTransientEntityChangeRefetchError,
+ })
if (
generation !== hierarchyRefreshGenerationRef.current ||
signal.aborted ||
@@ -1012,10 +991,31 @@ export function GfsBrowser(): React.JSX.Element {
) {
return
}
- if (!retryHierarchy) {
- if (!sameCrumbTrail(crumbsRef.current, refreshed)) setCrumbs(refreshed)
+ if (hierarchy.kind === 'missing') {
+ // Only denial of the stable-ID current-folder resolve is authority
+ // to leave this location. A failed ancestor lookup is not revocation.
+ setCrumbs([rootCrumb])
setTrailRecovery(null)
+ return
+ }
+ if (hierarchy.kind === 'retry' || hierarchy.kind === 'preserve') {
+ setTrailRecovery(previous =>
+ previous?.resourceId === visibleResourceId && previous.reason === 'hierarchy'
+ ? previous
+ : {
+ resourceId: visibleResourceId,
+ gfsUri:
+ visibleCrumb.gfsUri ??
+ `gfs://${DRIVE}/${visibleCrumb.rid ?? ridOfResourceId(visibleResourceId)}`,
+ reason: 'hierarchy',
+ }
+ )
+ if (hierarchy.kind === 'retry') scheduleRecovery()
+ return
}
+ const refreshed = [rootCrumb, ...hierarchy.ancestors.map(folderResourceToCrumb)]
+ if (!sameCrumbTrail(crumbsRef.current, refreshed)) setCrumbs(refreshed)
+ setTrailRecovery(null)
})()
revalidationTasks.push(hierarchyRefresh)
await Promise.all(revalidationTasks)
@@ -1350,7 +1350,7 @@ export function GfsBrowser(): React.JSX.Element {
// reconstruction succeeds. ('applied' clears any older notice for
// this resource inside the rebuild; 'superseded' leaves state to
// the newer attempt that displaced this one.)
- setTrailRecovery({ resourceId: source.resourceId, gfsUri: source.gfsUri })
+ setTrailRecovery({ resourceId: source.resourceId, gfsUri: source.gfsUri, reason: 'move' })
}
return
}
@@ -2054,14 +2054,25 @@ export function GfsBrowser(): React.JSX.Element {
{trailRecovery ? (
<div className="cu-banner cu-banner--warning cu-banner--dismissible" role="alert">
- <span>
- &ldquo;
- {crumbs.find(crumb => crumb.id === trailRecovery.resourceId)?.name ??
- trailRecovery.resourceId}
- &rdquo; moved, but its folder path could not be refreshed — the breadcrumb may not
- show the real location.
- </span>
- <Button size="sm" onClick={() => void retryTrailRecovery()}>
+ {trailRecovery.reason === 'move' ? (
+ <span>
+ &ldquo;
+ {crumbs.find(crumb => crumb.id === trailRecovery.resourceId)?.name ??
+ trailRecovery.resourceId}
+ &rdquo; moved, but its folder path could not be refreshed — the breadcrumb may not
+ show the real location.
+ </span>
+ ) : (
+ <span>
+ The current folder path could not be refreshed. Your current location is still
+ shown.
+ </span>
+ )}
+ <Button
+ size="sm"
+ onClick={() => void retryTrailRecovery()}
+ aria-label={trailRecovery.reason === 'hierarchy' ? 'Retry folder path' : undefined}
+ >
Retry
</Button>
</div>
diff --git a/control-ui/components/__tests__/GfsBrowser.revalidation.integration.test.tsx b/control-ui/components/__tests__/GfsBrowser.revalidation.integration.test.tsx
index 3c193e5d5..7dc3d92d6 100644
--- a/control-ui/components/__tests__/GfsBrowser.revalidation.integration.test.tsx
+++ b/control-ui/components/__tests__/GfsBrowser.revalidation.integration.test.tsx
@@ -1,6 +1,6 @@
// @vitest-environment jsdom
import { afterEach, describe, expect, it, vi } from 'vitest'
-import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
+import { act, cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'
import { EventEmitter } from 'node:events'
import {
closeActiveEntityChangeStreams,
@@ -392,4 +392,95 @@ describe('GfsBrowser authoritative revalidation integration', () => {
expect(rootChildReads).toBe(3)
expect(screen.getByRole('button', { name: 'existing.txt' })).toBeVisible()
})
+
+ it('preserves the complete folder trail when an ancestor lookup fails', async () => {
+ const work = child('folder-1', 'rid-folder-1', 'work', 'directory')
+ const existingFile = child('file-1', 'rid-file-1', 'existing.txt', 'file')
+ let byPathReads = 0
+ let streamController: ReadableStreamDefaultController<Uint8Array> | null = null
+ 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.endsWith('/api/v1/gfs/resources/root-1/children')) {
+ return jsonResponse({ items: [work], nextCursor: null })
+ }
+ if (url.pathname.endsWith('/api/v1/gfs/resources/folder-1/children')) {
+ return jsonResponse({ items: [existingFile], nextCursor: null })
+ }
+ if (url.pathname.endsWith('/api/v1/gfs/resolve')) {
+ return jsonResponse({
+ resourceId: work.resourceId,
+ rid: work.rid,
+ gfsUri: work.gfsUri,
+ name: work.name,
+ kind: 'directory',
+ path: '/work',
+ version: work.version,
+ })
+ }
+ if (url.pathname.endsWith('/api/v1/gfs/by-path')) {
+ byPathReads += 1
+ if (byPathReads === 1)
+ return new Response('ancestor temporarily unavailable', { status: 409 })
+ return jsonResponse({
+ resourceId: work.resourceId,
+ rid: work.rid,
+ gfsUri: work.gfsUri,
+ name: work.name,
+ kind: 'directory',
+ path: '/work',
+ version: work.version,
+ })
+ }
+ return jsonResponse({ items: [], nextCursor: null })
+ })
+ )
+
+ render(
+ <ToastProvider>
+ <GfsBrowser />
+ </ToastProvider>
+ )
+ await waitFor(() => expect(streamController).not.toBeNull())
+ await screen.findByRole('button', { name: 'work' })
+ fireEvent.click(screen.getByRole('button', { name: 'work' }))
+ await screen.findByRole('button', { name: 'existing.txt' })
+
+ await act(async () => {
+ streamController!.enqueue(new TextEncoder().encode(`${await controlApiProducerFrame()}\n`))
+ })
+
+ await waitFor(() => expect(byPathReads).toBe(1))
+ const breadcrumb = screen.getByRole('navigation', { name: 'Breadcrumb' })
+ await waitFor(() =>
+ expect(within(breadcrumb).getByRole('button', { name: 'work' })).toHaveAttribute(
+ 'aria-current',
+ 'page'
+ )
+ )
+ expect(screen.getByRole('button', { name: 'existing.txt' })).toBeVisible()
+ expect(screen.getByRole('alert')).toHaveTextContent('could not be refreshed')
+ fireEvent.click(screen.getByRole('button', { name: 'Retry folder path' }))
+ await waitFor(() => expect(screen.queryByRole('alert')).toBeNull())
+ expect(within(breadcrumb).getByRole('button', { name: 'work' })).toHaveAttribute(
+ 'aria-current',
+ 'page'
+ )
+ expect(screen.getByRole('button', { name: 'existing.txt' })).toBeVisible()
+ })
})
diff --git a/control-ui/lib/gfsHierarchyRevalidation.ts b/control-ui/lib/gfsHierarchyRevalidation.ts
new file mode 100644
index 000000000..16d712a98
--- /dev/null
+++ b/control-ui/lib/gfsHierarchyRevalidation.ts
@@ -0,0 +1,60 @@
+export interface GfsHierarchyResource {
+ resourceId: string
+ rid: string
+ gfsUri: string
+ name: string
+ kind: string
+ path: string | null
+ version: number
+}
+
+export type GfsHierarchyResult =
+ | { kind: 'resolved'; ancestors: GfsHierarchyResource[] }
+ | { kind: 'missing' }
+ | { kind: 'retry' }
+ | { kind: 'preserve' }
+
+function isAuthoritativeDenial(error: unknown): boolean {
+ if (!error || typeof error !== 'object') return false
+ const status = (error as { status?: unknown }).status
+ return status === 403 || status === 404
+}
+
+/** Resolve a complete folder trail without publishing a partial ancestry. */
+export async function resolveGfsHierarchy(input: {
+ readCurrent: () => Promise<GfsHierarchyResource>
+ readAncestor: (path: string) => Promise<GfsHierarchyResource>
+ isTransient: (error: unknown) => boolean
+}): Promise<GfsHierarchyResult> {
+ let current: GfsHierarchyResource
+ try {
+ current = await input.readCurrent()
+ } catch (error) {
+ if (isAuthoritativeDenial(error)) return { kind: 'missing' }
+ return { kind: input.isTransient(error) ? 'retry' : 'preserve' }
+ }
+
+ if (current.kind !== 'directory') return { kind: 'missing' }
+ if (!current.path || !current.path.startsWith('/')) return { kind: 'preserve' }
+
+ const ancestors: GfsHierarchyResource[] = []
+ let path = ''
+ for (const segment of current.path.split('/').filter(Boolean)) {
+ path += `/${segment}`
+ let ancestor: GfsHierarchyResource
+ try {
+ ancestor = await input.readAncestor(path)
+ } catch (error) {
+ // A missing ancestor does not prove the stable-id folder was revoked.
+ // Keep the full last-known trail until the whole ancestry resolves.
+ return { kind: input.isTransient(error) ? 'retry' : 'preserve' }
+ }
+ if (ancestor.kind !== 'directory') return { kind: 'preserve' }
+ ancestors.push(ancestor)
+ }
+
+ if (ancestors[ancestors.length - 1]?.resourceId !== current.resourceId) {
+ return { kind: 'preserve' }
+ }
+ return { kind: 'resolved', ancestors }
+}
diff --git a/control-ui/package-lock.json b/control-ui/package-lock.json
index 52e213638..3f31465c3 100644
--- a/control-ui/package-lock.json
+++ b/control-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-ui",
- "version": "0.1.678",
+ "version": "0.1.679",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-ui",
- "version": "0.1.678",
+ "version": "0.1.679",
"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 581398a39..b21a9f8a7 100644
--- a/control-ui/package.json
+++ b/control-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-ui",
- "version": "0.1.678",
+ "version": "0.1.679",
"private": true,
"scripts": {
"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",