Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(control-ui): number conflicting file moves
Ma918b098e9e8
diff --git a/control-ui/components/GfsBrowser.tsx b/control-ui/components/GfsBrowser.tsxindex ed4dbaa6c..83de3bf29 100644--- a/control-ui/components/GfsBrowser.tsx+++ b/control-ui/components/GfsBrowser.tsx@@ -6,6 +6,8 @@ import {GFS_UPLOAD_NAME_RETRY_LIMIT,createGfsUploadNameReservationBook,gfsUploadNameRetryDecision,+ isGfsNameConflict,+ nextAvailableGfsResourceName,normalizeGfsResourceName,} from '@clerum/gfs-interaction-policy'import { FileUploadModal } from '@components/FileUploadModal'@@ -80,6 +82,8 @@ interface Crumb {const DRIVE = 'main'const PENDING_GFS_UPLOAD_KEY = 'evenfire:gfs-upload-v2:pending'const GFS_RESOURCE_DRAG_TYPE = 'application/x-evenfire-gfs-resource'+const GFS_MOVE_NAME_EXHAUSTED_MESSAGE =+ 'Could not move this resource because all numbered names are already in use.'interface PendingGfsUpload {uploadId?: string@@ -596,23 +600,44 @@ export function GfsBrowser(): React.JSX.Element {movingResourceRef.current = source.resourceIdsetMovingResourceId(source.resourceId)try {- await apiSend(- 'PATCH',- `/api/v1/gfs/resources/${encodeURIComponent(source.resourceId)}`,- {- drive: DRIVE,- newParentId: destination.resourceId,- ifMatch: source.version,- },- { drive: DRIVE }- )+ const occupiedNames = new Set<string>()+ let moveName = source.name+ let moved = false+ for (let attempt = 0; attempt < GFS_UPLOAD_NAME_RETRY_LIMIT; attempt += 1) {+ try {+ await apiSend(+ 'PATCH',+ `/api/v1/gfs/resources/${encodeURIComponent(source.resourceId)}`,+ {+ drive: DRIVE,+ newParentId: destination.resourceId,+ ifMatch: source.version,+ ...(moveName === source.name ? {} : { newName: moveName }),+ },+ { drive: DRIVE }+ )+ moved = true+ break+ } catch (error) {+ if (!isGfsNameConflict(error)) throw error+ occupiedNames.add(moveName)+ if (attempt + 1 >= GFS_UPLOAD_NAME_RETRY_LIMIT) {+ throw new Error(GFS_MOVE_NAME_EXHAUSTED_MESSAGE)+ }+ moveName = nextAvailableGfsResourceName(source.name, occupiedNames)+ }+ }+ if (!moved) throw new Error(GFS_MOVE_NAME_EXHAUSTED_MESSAGE)// The destination may have been prefetched while it was visible. Its// cached listing is stale after a move and must be revalidated before it// is opened.childCacheRef.current.delete(destination.resourceId)- showToast(`Moved "${source.name}" to "${destination.name}".`, {- tone: 'success',- })+ showToast(+ moveName === source.name+ ? `Moved "${source.name}" to "${destination.name}".`+ : `Moved "${source.name}" to "${destination.name}" as "${moveName}".`,+ { tone: 'success' }+ )await refreshCurrent()} catch (err) {if (bubbleError) throw errdiff --git a/control-ui/components/__tests__/GfsBrowser.test.tsx b/control-ui/components/__tests__/GfsBrowser.test.tsxindex 0831f569c..36deb76fb 100644--- a/control-ui/components/__tests__/GfsBrowser.test.tsx+++ b/control-ui/components/__tests__/GfsBrowser.test.tsx@@ -774,6 +774,75 @@ describe('GfsBrowser', () => {await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())})+ it('retries a conflicting move with numbered names until the destination accepts it', async () => {+ const actualApi = await vi.importActual<typeof import('@lib/api')>('@lib/api')+ const folder = child('archive', 'directory', 1)+ const file = child('report.md', 'file', 2)+ mockApiGet.mockImplementation(async (path: string) => {+ if (+ path === '/api/v1/gfs/tree' ||+ path === '/api/v1/gfs/resources/11111111-1111-1111-1111-111111111111/children'+ ) {+ return {+ rootResourceId: '11111111-1111-1111-1111-111111111111',+ items: [folder, file],+ nextCursor: null,+ }+ }+ return { items: [], nextCursor: null }+ })+ const conflict = () => {+ const text = JSON.stringify({+ error: { code: 'already_exists', message: 'a resource with this name already exists' },+ })+ return actualApi.formatApiError(+ new Response(text, { status: 409, statusText: 'Conflict' }),+ text+ )+ }+ mockApiSend+ .mockRejectedValueOnce(conflict())+ .mockRejectedValueOnce(conflict())+ .mockResolvedValueOnce({+ version: 1,+ })+ renderBrowser()++ await waitFor(() => expect(screen.queryByRole('status', { name: 'Loading files' })).toBeNull())+ await openResourceMenu('report.md')+ fireEvent.click(screen.getByRole('menuitem', { name: 'Move to…' }))+ const dialog = await screen.findByRole('dialog', { name: 'Move file report.md' })+ fireEvent.click(await within(dialog).findByRole('button', { name: 'archive' }))+ fireEvent.click(within(dialog).getByRole('button', { name: 'Move here (archive)' }))++ await waitFor(() => expect(mockApiSend).toHaveBeenCalledTimes(3))+ expect(mockApiSend).toHaveBeenNthCalledWith(+ 1,+ 'PATCH',+ '/api/v1/gfs/resources/id-2',+ { drive: 'main', newParentId: 'id-1', ifMatch: 0 },+ { drive: 'main' }+ )+ expect(mockApiSend).toHaveBeenNthCalledWith(+ 2,+ 'PATCH',+ '/api/v1/gfs/resources/id-2',+ { drive: 'main', newParentId: 'id-1', ifMatch: 0, newName: 'report (1).md' },+ { drive: 'main' }+ )+ expect(mockApiSend).toHaveBeenNthCalledWith(+ 3,+ 'PATCH',+ '/api/v1/gfs/resources/id-2',+ { drive: 'main', newParentId: 'id-1', ifMatch: 0, newName: 'report (2).md' },+ { drive: 'main' }+ )+ expect(+ await screen.findByText('Moved "report.md" to "archive" as "report (2).md".')+ ).toBeTruthy()+ await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())+ })+it('keeps a persisted drag-and-drop session when resumable capabilities are unavailable', async () => {const rootId = '11111111-1111-1111-1111-111111111111'const rootRid = '11111111111111111111111111111111'diff --git a/control-ui/lib/__tests__/apiErrors.test.ts b/control-ui/lib/__tests__/apiErrors.test.tsnew file mode 100644index 000000000..8ac3d0ac8--- /dev/null+++ b/control-ui/lib/__tests__/apiErrors.test.ts@@ -0,0 +1,28 @@+import { describe, expect, it } from 'vitest'+import { formatApiError } from '../api'++describe('formatApiError', () => {+ it('preserves a nested structured error without rendering object Object', () => {+ const text = JSON.stringify({+ error: {+ code: 'already_exists',+ message: 'a resource with this name already exists',+ },+ })+ const error = formatApiError(+ new Response(text, { status: 409, statusText: 'Conflict' }),+ text+ ) as Error & { status?: number; code?: string; body?: Record<string, unknown> }++ expect(error.message).toBe('409 Conflict - a resource with this name already exists')+ expect(error.message).not.toContain('[object Object]')+ expect(error.status).toBe(409)+ expect(error.code).toBe('already_exists')+ expect(error.body).toEqual({+ error: {+ code: 'already_exists',+ message: 'a resource with this name already exists',+ },+ })+ })+})diff --git a/control-ui/lib/api.ts b/control-ui/lib/api.tsindex 42235a71f..66ed6a69f 100644--- a/control-ui/lib/api.ts+++ b/control-ui/lib/api.ts@@ -96,7 +96,15 @@ export function formatApiError(res: Response, text: string): Error {try {const parsed = JSON.parse(text) as { error?: unknown; message?: unknown }if (parsed && typeof parsed === 'object') parsedBody = parsed as Record<string, unknown>- detail = String(parsed.message || parsed.error || text)+ const nestedError =+ parsed.error && typeof parsed.error === 'object'+ ? (parsed.error as Record<string, unknown>)+ : null+ detail =+ (typeof parsed.message === 'string' && parsed.message) ||+ (typeof nestedError?.message === 'string' && nestedError.message) ||+ (typeof parsed.error === 'string' && parsed.error) ||+ text} catch {detail = text}@@ -133,8 +141,16 @@ export function formatApiError(res: Response, text: string): Error {// render structured, actionable errors (e.g. unpriced_models, price_in_use_by_budget)// instead of the generic message string.if (parsedBody) {+ const nestedError =+ parsedBody.error && typeof parsedBody.error === 'object'+ ? (parsedBody.error as Record<string, unknown>)+ : null;(error as Error & { code?: string }).code =- typeof parsedBody.error === 'string' ? (parsedBody.error as string) : undefined+ typeof parsedBody.error === 'string'+ ? parsedBody.error+ : typeof nestedError?.code === 'string'+ ? nestedError.code+ : undefined;(error as Error & { body?: Record<string, unknown> }).body = parsedBody}return errordiff --git a/control-ui/package-lock.json b/control-ui/package-lock.jsonindex 5ff71ec30..aa42f899a 100644--- a/control-ui/package-lock.json+++ b/control-ui/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-ui",- "version": "0.1.567",+ "version": "0.1.568","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-ui",- "version": "0.1.567",+ "version": "0.1.568","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.jsonindex 0ce8faaa3..2a6f933b3 100644--- a/control-ui/package.json+++ b/control-ui/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-ui",- "version": "0.1.567",+ "version": "0.1.568","private": true,"scripts": {"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",