Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(gfs): make status snapshots and resume fallback safe
Alfredo Lopezfdefc811b34f
diff --git a/control-api/test/gfsUploadSession.realPostgres.integration.test.ts b/control-api/test/gfsUploadSession.realPostgres.integration.test.tsindex 62e728bca..338d118bf 100644--- a/control-api/test/gfsUploadSession.realPostgres.integration.test.ts+++ b/control-api/test/gfsUploadSession.realPostgres.integration.test.ts@@ -260,7 +260,7 @@ describeRealPostgres('GFS Upload v2 session engine on real PostgreSQL', () => {idempotencyKey: randomUUID(),})await pool.query(- `UPDATE gfs_upload_sessions SET state = 'uploading', active_part_count = 2 WHERE upload_id = $1`,+ `UPDATE gfs_upload_sessions SET state = 'uploading', active_part_count = 99 WHERE upload_id = $1`,[created.session.uploadId])await pool.query(diff --git a/control-ui/components/GfsBrowser.tsx b/control-ui/components/GfsBrowser.tsxindex 86df75d0d..e984c7280 100644--- a/control-ui/components/GfsBrowser.tsx+++ b/control-ui/components/GfsBrowser.tsx@@ -103,6 +103,27 @@ function clearPendingGfsUpload(uploadId?: string): void {window.localStorage.removeItem(PENDING_GFS_UPLOAD_KEY)}+function matchingPendingResumeUploadId(+ file: File,+ target: GfsUploadJobInput['target'],+ name: string+): string | undefined {+ const pending = readPendingGfsUpload()+ if (!pending?.uploadId) return undefined+ if (+ pending.fileName !== file.name ||+ pending.fileSize !== file.size ||+ pending.lastModified !== file.lastModified ||+ pending.name !== name ||+ pending.target.operation !== target.operation ||+ pending.target.parentRid !== target.parentRid ||+ pending.target.resourceRid !== target.resourceRid ||+ pending.target.ifMatch !== target.ifMatch+ )+ return undefined+ return pending.uploadId+}+function formatBytes(bytes: number): string {if (!Number.isFinite(bytes) || bytes <= 0) return '0 B'if (bytes < 1024) return `${bytes} B`@@ -371,6 +392,12 @@ export function GfsBrowser(): React.JSX.Element {return 'completed'} catch (error) {if (!(error instanceof GfsUploadCapabilityError)) throw error+ if (input.resumeUploadId) {+ throw new GfsUploadCapabilityError(+ 'The persisted resumable session cannot be resumed while GFS Upload v2 is unavailable.',+ { cause: error }+ )+ }showToast('Resumable upload is unavailable; using the legacy 16 MiB path.', { tone: 'info' })await uploadGfsFileLegacy(input)clearPendingGfsUpload()@@ -417,12 +444,13 @@ export function GfsBrowser(): React.JSX.Element {const rid = current?.rid ?? (current?.id ? ridOfResourceId(current.id) : null)if (!rid || !file) returnsetUploading(true)+ let resumeUploadId: string | undefinedtry {assertGfsFileUploadSize(file.size)const name = await normalizeGfsResourceName(file.name)const target = { operation: 'create' as const, parentRid: rid }const pending = readPendingGfsUpload()- const resumeUploadId =+ resumeUploadId =pending &&pending.fileName === file.name &&pending.fileSize === file.size &&@@ -478,6 +506,13 @@ export function GfsBrowser(): React.JSX.Element {} catch (err) {if (uploadJobRef.current?.snapshot().state === 'aborted') returnif (err instanceof GfsUploadCapabilityError) {+ if (resumeUploadId) {+ showToast(+ 'The persisted resumable session cannot be resumed while GFS Upload v2 is unavailable.',+ { tone: 'error' }+ )+ return+ }showToast('Resumable upload is unavailable; using the legacy 16 MiB path.', {tone: 'info',})@@ -646,10 +681,16 @@ export function GfsBrowser(): React.JSX.Element {async function replaceFile(child: GfsChild, file: File | null | undefined): Promise<void> {if (!file) returntry {+ const target = {+ operation: 'replace' as const,+ resourceRid: child.rid,+ ifMatch: child.version,+ }const state = await uploadWithCompatibility({file,name: file.name,- target: { operation: 'replace', resourceRid: child.rid, ifMatch: child.version },+ target,+ resumeUploadId: matchingPendingResumeUploadId(file, target, file.name),})if (state === 'completed') {showToast('File replaced.', { tone: 'success' })@@ -669,10 +710,12 @@ export function GfsBrowser(): React.JSX.Element {if (!file || folder.kind !== 'directory') returntry {const name = await normalizeGfsResourceName(file.name)+ const target = { operation: 'create' as const, parentRid: folder.rid }const state = await uploadWithCompatibility({file,name,- target: { operation: 'create', parentRid: folder.rid },+ target,+ resumeUploadId: matchingPendingResumeUploadId(file, target, name),})if (state === 'completed') showToast('File uploaded.', { tone: 'success' })else showToast('Upload paused. Resume it from the upload panel.', { tone: 'info' })diff --git a/control-ui/components/__tests__/GfsBrowser.test.tsx b/control-ui/components/__tests__/GfsBrowser.test.tsxindex 103df2733..2a2d74e1c 100644--- a/control-ui/components/__tests__/GfsBrowser.test.tsx+++ b/control-ui/components/__tests__/GfsBrowser.test.tsx@@ -16,7 +16,7 @@ import {gfsFetchFileBlob,putGfsGrant,} from '@lib/api'-import { createGfsUploadJob, uploadGfsFile } from '@lib/gfsFileUpload'+import { GfsUploadCapabilityError, createGfsUploadJob, uploadGfsFile } from '@lib/gfsFileUpload'import { normalizeGfsResourceName } from '@lib/gfsResourceName'import { GfsBrowser } from '../GfsBrowser'import { ToastProvider } from '../Toast'@@ -399,6 +399,53 @@ describe('GfsBrowser', () => {)})+ it('keeps a persisted drag-and-drop session when resumable capabilities are unavailable', async () => {+ const rootId = '11111111-1111-1111-1111-111111111111'+ const rootRid = '11111111111111111111111111111111'+ const uploadId = '77777777-7777-4777-8777-777777777777'+ const lastModified = 1_725_000_000_000+ window.localStorage.setItem(+ 'evenfire:gfs-upload-v2:pending',+ JSON.stringify({+ uploadId,+ fileName: 'resume.md',+ fileSize: 11,+ lastModified,+ target: { operation: 'create', parentRid: rootRid },+ name: 'resume.md',+ })+ )+ mockApiGet.mockResolvedValue({ rootResourceId: rootId, items: [], nextCursor: null })+ mockCreateGfsUploadJob.mockImplementationOnce(() => ({+ start: vi.fn().mockRejectedValue(new GfsUploadCapabilityError('writer unavailable')),+ snapshot: vi.fn(() => ({ state: 'failed' })),+ pause: vi.fn(),+ resume: vi.fn(),+ cancel: vi.fn(),+ }))+ renderBrowser()+ await screen.findByText('No resources are visible in this folder.')++ const browser = screen.getByRole('region', { name: 'Global File System browser' })+ const file = new File(['resume data'], 'resume.md', {+ type: 'text/markdown',+ lastModified,+ })+ fireEvent.drop(browser.querySelector('.cu-gfs-card')!, {+ dataTransfer: { dropEffect: 'none', files: [file], types: ['Files'] },+ })++ await waitFor(() =>+ expect(screen.getByText(/persisted resumable session cannot be resumed/i)).toBeTruthy()+ )+ expect(mockApiSend).not.toHaveBeenCalled()+ expect(+ JSON.parse(window.localStorage.getItem('evenfire:gfs-upload-v2:pending')!)+ ).toMatchObject({+ uploadId,+ })+ })+it('shows byte progress and exposes pause/resume/cancel through the visible upload modal', async () => {const rootId = '11111111-1111-1111-1111-111111111111'const rootRid = '11111111111111111111111111111111'@@ -741,6 +788,53 @@ describe('GfsBrowser', () => {)})+ it('does not fall back to legacy when replacing a persisted resumable session', async () => {+ const lastModified = 1_725_000_000_000+ const uploadId = '66666666-6666-4666-8666-666666666666'+ window.localStorage.setItem(+ 'evenfire:gfs-upload-v2:pending',+ JSON.stringify({+ uploadId,+ fileName: 'report.md',+ fileSize: 11,+ lastModified,+ target: { operation: 'replace', resourceRid: 'r2', ifMatch: 0 },+ name: 'report.md',+ })+ )+ mockApiGet.mockResolvedValueOnce({+ items: [child('report.md', 'file', 2)],+ nextCursor: null,+ })+ const capabilityError = new GfsUploadCapabilityError('writer unavailable')+ mockCreateGfsUploadJob.mockImplementationOnce(() => ({+ start: vi.fn().mockRejectedValue(capabilityError),+ snapshot: vi.fn(() => ({ state: 'failed' })),+ pause: vi.fn(),+ resume: vi.fn(),+ cancel: vi.fn(),+ }))+ renderBrowser()++ await openResourceMenu('report.md')+ fireEvent.click(screen.getByRole('menuitem', { name: 'Replace file' }))+ fireEvent.change(screen.getByLabelText('Replace report.md'), {+ target: {+ files: [new File(['resume data'], 'report.md', { lastModified })],+ },+ })++ await waitFor(() =>+ expect(screen.getByText(/persisted resumable session cannot be resumed/i)).toBeTruthy()+ )+ expect(mockApiSend).not.toHaveBeenCalled()+ expect(+ JSON.parse(window.localStorage.getItem('evenfire:gfs-upload-v2:pending')!)+ ).toMatchObject({+ uploadId,+ })+ })+it('supports roving keyboard focus and Escape in the resource menu', async () => {mockApiGet.mockResolvedValueOnce({items: [child('report.md', 'file', 2)],diff --git a/gfs-controller/src/upload/uploadSession.test.ts b/gfs-controller/src/upload/uploadSession.test.tsindex c16872ece..263d8f253 100644--- a/gfs-controller/src/upload/uploadSession.test.ts+++ b/gfs-controller/src/upload/uploadSession.test.ts@@ -23,6 +23,7 @@ class MemoryDb {async query(text: string, values: unknown[] = []): Promise<{ rows: Row[] }> {const sql = text.replace(/\s+/g, ' ').trim()+ if (sql === 'SET TRANSACTION ISOLATION LEVEL REPEATABLE READ') return { rows: [] }if (sql.startsWith('SELECT pg_advisory_xact_lock')) return { rows: [] }if (sql.startsWith('SELECT * FROM gfs_upload_sessions WHERE owner_subject')) {const [owner, drive, key] = values.map(String)diff --git a/gfs-controller/src/upload/uploadSession.ts b/gfs-controller/src/upload/uploadSession.tsindex c2b3ac478..409c07c69 100644--- a/gfs-controller/src/upload/uploadSession.ts+++ b/gfs-controller/src/upload/uploadSession.ts@@ -701,7 +701,7 @@ export class GfsUploadSessionService {options?: { cursor?: string; limit?: number }): Promise<UploadStatusPage> {requireEnabled(this.deps.config)- const session = await this.loadOwned(uploadId, principal)+ requireUuid(uploadId, 'uploadId')const rawCursor = options?.cursor ?? ''if (rawCursor && !/^\d+$/.test(rawCursor))throw new GfsError('path_invalid', 'status cursor is invalid')@@ -709,34 +709,52 @@ export class GfsUploadSessionService {const requestedLimit = options?.limit ?? 256if (!Number.isSafeInteger(requestedLimit) || requestedLimit < 1 || requestedLimit > 256)throw new GfsError('path_invalid', 'status limit is invalid')- const result = await this.deps.db.query(- `SELECT upload_id, part_number, offset_bytes, length_bytes, sha256, state, staging_path, lease_epoch- FROM gfs_upload_parts WHERE upload_id = $1 AND state = 'committed' AND part_number > $2 ORDER BY part_number ASC LIMIT $3`,- [uploadId, cursor, requestedLimit + 1]- )- const active = await this.deps.db.query(- `SELECT part_number FROM gfs_upload_parts WHERE upload_id = $1 AND state = 'reserved' ORDER BY part_number ASC`,- [uploadId]- )- const hasMore = result.rows.length > requestedLimit- const rows = hasMore ? result.rows.slice(0, requestedLimit) : result.rows- const lastPart = rows.at(-1)- return {- session: {- ...toReceipt(session),- activePartNumbers: active.rows.map(row => Number(row.part_number)),- },- parts: rows.map(row => {- const part = rowToPart(row)- return {- partNumber: part.partNumber,- offsetBytes: part.offsetBytes,- lengthBytes: part.lengthBytes,- sha256: part.sha256,- }- }),- nextCursor: hasMore && lastPart ? String(Number(lastPart.part_number)) : null,- }+ return this.deps.tx.transaction(async client => {+ // A status response is consumed as one reconciliation snapshot. Keep+ // session state, committed parts, and durable active leases on the same+ // MVCC snapshot so a sibling reservation/commit cannot produce a stale+ // count paired with a newer part-number set.+ await client.query('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ')+ const sessionResult = await client.query(+ `SELECT * FROM gfs_upload_sessions WHERE upload_id = $1 AND drive = $2 AND owner_subject = $3`,+ [uploadId, principal.drive, principal.ownerSubject]+ )+ const sessionRow = sessionResult.rows[0]+ if (!sessionRow) throw new GfsError('not_found', 'upload session not found')+ const session = rowToSession(sessionRow)+ assertNotExpired(session, this.now())++ const result = await client.query(+ `SELECT upload_id, part_number, offset_bytes, length_bytes, sha256, state, staging_path, lease_epoch+ FROM gfs_upload_parts WHERE upload_id = $1 AND state = 'committed' AND part_number > $2 ORDER BY part_number ASC LIMIT $3`,+ [uploadId, cursor, requestedLimit + 1]+ )+ const active = await client.query(+ `SELECT part_number FROM gfs_upload_parts WHERE upload_id = $1 AND state = 'reserved' ORDER BY part_number ASC`,+ [uploadId]+ )+ const activePartNumbers = active.rows.map(row => Number(row.part_number))+ const hasMore = result.rows.length > requestedLimit+ const rows = hasMore ? result.rows.slice(0, requestedLimit) : result.rows+ const lastPart = rows.at(-1)+ return {+ session: {+ ...toReceipt(session),+ activePartCount: activePartNumbers.length,+ activePartNumbers,+ },+ parts: rows.map(row => {+ const part = rowToPart(row)+ return {+ partNumber: part.partNumber,+ offsetBytes: part.offsetBytes,+ lengthBytes: part.lengthBytes,+ sha256: part.sha256,+ }+ }),+ nextCursor: hasMore && lastPart ? String(Number(lastPart.part_number)) : null,+ }+ })}async putPart(