Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
feat(desktop): honor runtime upload product limits
Josué4afab220bb29
diff --git a/desktop-app/src/gfs/upload.test.ts b/desktop-app/src/gfs/upload.test.tsindex 7c468052e..e1ffc2adb 100644--- a/desktop-app/src/gfs/upload.test.ts+++ b/desktop-app/src/gfs/upload.test.ts@@ -1,6 +1,6 @@import { describe, expect, it } from 'vitest'import { createHash } from 'node:crypto'-import { mkdtemp, rm, writeFile } from 'node:fs/promises'+import { mkdtemp, rm, truncate, writeFile } from 'node:fs/promises'import { tmpdir } from 'node:os'import { join } from 'node:path'import {@@ -43,6 +43,247 @@ describe('desktop GFS indexed uploader', () => {expect(isAmbiguousUploadStatus(507)).toBe(false)})+ it('reaches writer admission for a 250 MiB file under a 300 MiB capability', async () => {+ const root = await mkdtemp(join(tmpdir(), 'evenfire-gfs-upload-runtime-raise-'))+ try {+ const filePath = join(root, 'payload.bin')+ const fileSize = 250 * 1024 * 1024+ await writeFile(filePath, Buffer.alloc(0))+ await truncate(filePath, fileSize)+ let capabilityCalls = 0+ let createSize: number | undefined+ const transport = {+ async requestJson<T>(method: 'GET' | 'POST', url: string, options?: { body?: unknown }) {+ if (method === 'GET' && requestPath(url).endsWith('/capabilities')) {+ capabilityCalls += 1+ return enabledCapabilities({ maxFileBytes: 300 * 1024 * 1024 }) as T+ }+ if (method === 'POST' && requestPath(url).endsWith('/uploads')) {+ createSize = (options?.body as { sizeBytes?: number } | undefined)?.sizeBytes+ throw new Error('writer admission reached')+ }+ throw new Error(`unexpected ${method} ${url}`)+ },+ async requestPart() {+ throw new Error('part request was not expected')+ },+ }++ await expect(+ new DesktopGfsUploadJob({+ baseUrl: 'https://api.example',+ token: 'token',+ filePath,+ name: 'payload.bin',+ drive: 'main',+ operation: 'create',+ parentRid: 'parent-runtime-raise',+ transport,+ }).start()+ ).rejects.toThrow('writer admission reached')+ expect(capabilityCalls).toBe(1)+ expect(createSize).toBe(fileSize)+ } finally {+ await rm(root, { recursive: true, force: true })+ }+ })++ it('enforces a lowered 100 MiB writer product limit before session creation', async () => {+ const root = await mkdtemp(join(tmpdir(), 'evenfire-gfs-upload-runtime-lower-'))+ try {+ const filePath = join(root, 'payload.bin')+ await writeFile(filePath, Buffer.alloc(0))+ await truncate(filePath, 100 * 1024 * 1024 + 1)+ let createCalls = 0+ const transport = {+ async requestJson<T>(method: 'GET' | 'POST') {+ if (method === 'GET') return enabledCapabilities({ maxFileBytes: 100 * 1024 * 1024 }) as T+ createCalls += 1+ throw new Error('unexpected session creation')+ },+ async requestPart() {+ throw new Error('part request was not expected')+ },+ }++ await expect(+ new DesktopGfsUploadJob({+ baseUrl: 'https://api.example',+ token: 'token',+ filePath,+ name: 'payload.bin',+ drive: 'main',+ operation: 'create',+ parentRid: 'parent-runtime-lower',+ transport,+ }).start()+ ).rejects.toThrow(`GFS files are limited to ${100 * 1024 * 1024} bytes by the writer`)+ expect(createCalls).toBe(0)+ } finally {+ await rm(root, { recursive: true, force: true })+ }+ })++ it('uses the 200 MiB compatibility limit when an enabled writer omits maxFileBytes', async () => {+ const root = await mkdtemp(join(tmpdir(), 'evenfire-gfs-upload-missing-limit-'))+ try {+ const filePath = join(root, 'payload.bin')+ await writeFile(filePath, Buffer.alloc(0))+ await truncate(filePath, 200 * 1024 * 1024 + 1)+ let createCalls = 0+ const transport = {+ async requestJson<T>(method: 'GET' | 'POST') {+ if (method === 'GET') return enabledCapabilities() as T+ createCalls += 1+ throw new Error('unexpected session creation')+ },+ async requestPart() {+ throw new Error('part request was not expected')+ },+ }++ await expect(+ new DesktopGfsUploadJob({+ baseUrl: 'https://api.example',+ token: 'token',+ filePath,+ name: 'payload.bin',+ drive: 'main',+ operation: 'create',+ parentRid: 'parent-missing-limit',+ transport,+ }).start()+ ).rejects.toThrow(/200 MiB compatibility limit/)+ expect(createCalls).toBe(0)+ } finally {+ await rm(root, { recursive: true, force: true })+ }+ })++ it.each([0, -1, 1.5, 1024 * 1024 * 1024 + 1, Number.MAX_SAFE_INTEGER + 1, '314572800', null])(+ 'rejects malformed writer maxFileBytes %s as a capability failure',+ async maxFileBytes => {+ const root = await mkdtemp(join(tmpdir(), 'evenfire-gfs-upload-invalid-limit-'))+ try {+ const filePath = join(root, 'payload.bin')+ await writeFile(filePath, Buffer.from([1]))+ let createCalls = 0+ const transport = {+ async requestJson<T>(method: 'GET' | 'POST') {+ if (method === 'GET') return enabledCapabilities({ maxFileBytes }) as T+ createCalls += 1+ throw new Error('unexpected session creation')+ },+ async requestPart() {+ throw new Error('part request was not expected')+ },+ }+ await expect(+ new DesktopGfsUploadJob({+ baseUrl: 'https://api.example',+ token: 'token',+ filePath,+ name: 'payload.bin',+ drive: 'main',+ operation: 'create',+ parentRid: 'parent-invalid-limit',+ transport,+ }).start()+ ).rejects.toMatchObject({ name: 'DesktopUploadCapabilityError' })+ expect(createCalls).toBe(0)+ } finally {+ await rm(root, { recursive: true, force: true })+ }+ }+ )++ it('rejects above the 1 GiB protocol maximum before requesting capabilities', async () => {+ const root = await mkdtemp(join(tmpdir(), 'evenfire-gfs-upload-protocol-max-'))+ try {+ const filePath = join(root, 'payload.bin')+ await writeFile(filePath, Buffer.alloc(0))+ await truncate(filePath, 1024 * 1024 * 1024 + 1)+ let requestCalls = 0+ const transport = {+ async requestJson<T>() {+ requestCalls += 1+ return enabledCapabilities({ maxFileBytes: 1024 * 1024 * 1024 }) as T+ },+ async requestPart() {+ throw new Error('part request was not expected')+ },+ }+ await expect(+ new DesktopGfsUploadJob({+ baseUrl: 'https://api.example',+ token: 'token',+ filePath,+ name: 'payload.bin',+ drive: 'main',+ operation: 'create',+ parentRid: 'parent-protocol-max',+ transport,+ }).start()+ ).rejects.toThrow(/1 GiB Upload v2 protocol maximum/)+ expect(requestCalls).toBe(0)+ } finally {+ await rm(root, { recursive: true, force: true })+ }+ })++ it('resumes an admitted session after the writer product limit is lowered', async () => {+ const root = await mkdtemp(join(tmpdir(), 'evenfire-gfs-upload-resume-lowered-limit-'))+ try {+ const filePath = join(root, 'payload.bin')+ await writeFile(filePath, Buffer.from([1, 2]))+ const session = {+ uploadId: '91919191-9191-4191-8191-919191919191',+ drive: 'main',+ operation: 'create' as const,+ expectedBytes: 2,+ partBytes: 1,+ partCount: 2,+ state: 'paused',+ contiguousBytes: 0,+ committedBytes: 0,+ committedPartCount: 0,+ activePartCount: 0,+ expiresAt: new Date(Date.now() + 60_000).toISOString(),+ }+ let statusCalls = 0+ const transport = {+ async requestJson<T>(method: 'GET' | 'HEAD', url: string) {+ if (method === 'GET' && requestPath(url).endsWith('/capabilities'))+ return enabledCapabilities({ maxFileBytes: 1 }) as T+ if (method === 'HEAD') return {} as T+ if (method === 'GET' && requestPath(url).endsWith('/status')) {+ statusCalls += 1+ return { ok: true, data: { session, parts: [] } } as T+ }+ throw new Error(`unexpected ${method} ${url}`)+ },+ async requestPart() {+ throw new Error('part request was not expected')+ },+ }+ const result = await new DesktopGfsUploadJob({+ baseUrl: 'https://api.example',+ token: 'token',+ filePath,+ name: 'payload.bin',+ drive: 'main',+ operation: 'create',+ parentRid: 'parent-resume-lowered-limit',+ resumeUploadId: session.uploadId,+ transport,+ }).start()+ expect(result.state).toBe('paused')+ expect(statusCalls).toBe(1)+ } finally {+ await rm(root, { recursive: true, force: true })+ }+ })+it('streams independent binary parts with base64 checksums and completes the session', async () => {const root = await mkdtemp(join(tmpdir(), 'evenfire-gfs-upload-'))try {diff --git a/desktop-app/src/gfs/upload.ts b/desktop-app/src/gfs/upload.tsindex cfa5cb67f..ab152444b 100644--- a/desktop-app/src/gfs/upload.ts+++ b/desktop-app/src/gfs/upload.ts@@ -3,7 +3,8 @@ import { createReadStream } from 'node:fs'import { stat } from 'node:fs/promises'import { type Readable, Transform } from 'node:stream'-const GFS_UPLOAD_V2_PRODUCT_MAX_BYTES = 200 * 1024 * 1024+const GFS_UPLOAD_V2_DEFAULT_PRODUCT_MAX_BYTES = 200 * 1024 * 1024+const GFS_UPLOAD_V2_PROTOCOL_MAX_BYTES = 1024 * 1024 * 1024const GFS_UPLOAD_V2_PREFERRED_PART_BYTES = 8 * 1024 * 1024const GFS_UPLOAD_V2_MAX_PART_BYTES = 16 * 1024 * 1024const GFS_UPLOAD_V2_DEFAULT_CONCURRENCY = 4@@ -128,6 +129,20 @@ export class DesktopUploadCapabilityError extends Error {}}+export function normalizeUploadProductMaxBytes(value: unknown): number {+ if (value === undefined) return GFS_UPLOAD_V2_DEFAULT_PRODUCT_MAX_BYTES+ if (+ !Number.isSafeInteger(value) ||+ (value as number) < 1 ||+ (value as number) > GFS_UPLOAD_V2_PROTOCOL_MAX_BYTES+ ) {+ throw new DesktopUploadCapabilityError(+ 'GFS resumable capabilities advertised an invalid file ceiling'+ )+ }+ return value as number+}+export const GFS_UPLOAD_DEFAULT_INSTABILITY_FAILURE_THRESHOLD = 3const GFS_UPLOAD_MAX_INSTABILITY_FAILURE_THRESHOLD = 100@@ -1312,8 +1327,13 @@ export class DesktopGfsUploadJob {const identity = await sourceIdentity(this.input.filePath)const file = await stat(this.input.filePath)this.input.sourceIdentity = identity- if (!Number.isSafeInteger(file.size) || file.size > GFS_UPLOAD_V2_PRODUCT_MAX_BYTES)- throw new Error(`GFS files are limited to ${GFS_UPLOAD_V2_PRODUCT_MAX_BYTES} bytes`)+ if (+ !Number.isSafeInteger(file.size) ||+ file.size < 0 ||+ file.size > GFS_UPLOAD_V2_PROTOCOL_MAX_BYTES+ ) {+ throw new Error('GFS files cannot exceed the 1 GiB Upload v2 protocol maximum')+ }this.totalBytes = file.sizelet capabilities: UploadCapabilitiestry {@@ -1347,14 +1367,7 @@ export class DesktopGfsUploadJob {throw new DesktopUploadCapabilityError('Resumable GFS uploads are not enabled on this writer.')- if (resumable.maxFileBytes !== undefined) {- if (!Number.isSafeInteger(resumable.maxFileBytes) || resumable.maxFileBytes < 0)- throw new DesktopUploadCapabilityError(- 'GFS resumable capabilities advertised an invalid file ceiling'- )- if (file.size > resumable.maxFileBytes)- throw new Error(`GFS files are limited to ${resumable.maxFileBytes} bytes by the writer`)- }+ const productMaxFileBytes = normalizeUploadProductMaxBytes(resumable.maxFileBytes)this.input.advertisedConcurrency = resumable.maxConcurrentPartsPerSessionthis.input.instabilityFailureThreshold = normalizeInstabilityFailureThreshold(resumable.instabilityFailureThreshold@@ -1375,6 +1388,14 @@ export class DesktopGfsUploadJob {: await validateResumedParts(this.input.filePath, session, parts)return { session, committed, resumable }}+ if (file.size > productMaxFileBytes) {+ if (resumable.maxFileBytes === undefined) {+ throw new Error(+ 'GFS files are limited to the 200 MiB compatibility limit because the writer omitted maxFileBytes'+ )+ }+ throw new Error(`GFS files are limited to ${productMaxFileBytes} bytes by the writer`)+ }const target = this.input.operation === 'create' ? this.input.parentRid : this.input.resourceRidif (!target) throw new Error(`${this.input.operation} upload target is required`)this.assertAuthEpoch()diff --git a/desktop-app/ui/src/constants/gfsFileUpload.ts b/desktop-app/ui/src/constants/gfsFileUpload.tsindex 7e795d01e..856ac80d3 100644--- a/desktop-app/ui/src/constants/gfsFileUpload.ts+++ b/desktop-app/ui/src/constants/gfsFileUpload.ts@@ -1,7 +1,5 @@-// Product hard ceiling. Upload v2 sends indexed binary parts, so the 200 MiB-// file limit is independent of the 8 MiB preferred / 16 MiB hard part limit.-export const GFS_FILE_UPLOAD_MAX_BYTES = 200 * 1024 * 1024-export const GFS_FILE_UPLOAD_MAX_MEGABYTES = 200+// Absolute protocol guard. The writer advertises the runtime product policy.+export const GFS_FILE_UPLOAD_PROTOCOL_MAX_BYTES = 1024 * 1024 * 1024export const GFS_FILE_UPLOAD_PREFERRED_PART_BYTES = 8 * 1024 * 1024export const GFS_FILE_UPLOAD_MAX_PART_BYTES = 16 * 1024 * 1024export const GFS_FILE_UPLOAD_DEFAULT_CONCURRENCY = 4diff --git a/desktop-app/ui/src/lib/__tests__/gfsFileUpload.test.ts b/desktop-app/ui/src/lib/__tests__/gfsFileUpload.test.tsindex 8e47455e2..35dea25bf 100644--- a/desktop-app/ui/src/lib/__tests__/gfsFileUpload.test.ts+++ b/desktop-app/ui/src/lib/__tests__/gfsFileUpload.test.ts@@ -1,15 +1,15 @@import { describe, expect, it } from 'vitest'-import { GFS_FILE_UPLOAD_MAX_BYTES, GFS_FILE_UPLOAD_MAX_MEGABYTES } from '@constants/gfsFileUpload'import { assertGfsFileUploadSize } from '@lib/gfsFileUpload'describe('assertGfsFileUploadSize', () => {- it('accepts files at the upload limit', () => {- expect(() => assertGfsFileUploadSize(GFS_FILE_UPLOAD_MAX_BYTES)).not.toThrow()+ it('allows product-policy decisions through the 1 GiB protocol maximum', () => {+ expect(() => assertGfsFileUploadSize(250 * 1024 * 1024)).not.toThrow()+ expect(() => assertGfsFileUploadSize(1024 * 1024 * 1024)).not.toThrow()})- it('rejects files above the upload limit', () => {- expect(() => assertGfsFileUploadSize(GFS_FILE_UPLOAD_MAX_BYTES + 1)).toThrow(- `GFS uploads are limited to ${GFS_FILE_UPLOAD_MAX_MEGABYTES} MB per file.`+ it('rejects files above the absolute protocol maximum', () => {+ expect(() => assertGfsFileUploadSize(1024 * 1024 * 1024 + 1)).toThrow(+ 'GFS uploads cannot exceed the 1 GiB Upload v2 protocol maximum.')})})diff --git a/desktop-app/ui/src/lib/gfsFileUpload.ts b/desktop-app/ui/src/lib/gfsFileUpload.tsindex 5baef1b87..b01077543 100644--- a/desktop-app/ui/src/lib/gfsFileUpload.ts+++ b/desktop-app/ui/src/lib/gfsFileUpload.ts@@ -1,7 +1,11 @@-import { GFS_FILE_UPLOAD_MAX_BYTES, GFS_FILE_UPLOAD_MAX_MEGABYTES } from '@constants/gfsFileUpload'+import { GFS_FILE_UPLOAD_PROTOCOL_MAX_BYTES } from '@constants/gfsFileUpload'export function assertGfsFileUploadSize(byteLength: number): void {- if (byteLength > GFS_FILE_UPLOAD_MAX_BYTES) {- throw new Error(`GFS uploads are limited to ${GFS_FILE_UPLOAD_MAX_MEGABYTES} MB per file.`)+ if (+ !Number.isSafeInteger(byteLength) ||+ byteLength < 0 ||+ byteLength > GFS_FILE_UPLOAD_PROTOCOL_MAX_BYTES+ ) {+ throw new Error('GFS uploads cannot exceed the 1 GiB Upload v2 protocol maximum.')}}diff --git a/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx b/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsxindex b3acd26b4..c0b8e20ed 100644--- a/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx+++ b/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx@@ -3,7 +3,7 @@ import { useState } from 'react'import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import { QueryClient, QueryClientProvider } from '@tanstack/react-query'import { act, cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'-import { GFS_FILE_UPLOAD_MAX_BYTES } from '@constants/gfsFileUpload'+import { GFS_FILE_UPLOAD_PROTOCOL_MAX_BYTES } from '@constants/gfsFileUpload'import { GFS_IMAGE_PREVIEW_MAX_BYTES } from '@constants/gfsImagePreview'import { GFS_MARKDOWN_PREVIEW_MAX_BYTES } from '@constants/gfsMarkdownPreview'import { type GfsCrumb, isGfsSessionAuthorityFailure } from '@hooks/domain/useGfsBrowserController'@@ -743,7 +743,7 @@ describe('FilesPage', () => {createFile,})const oversized = new File(['small fixture'], 'oversized.md', { type: 'text/markdown' })- Object.defineProperty(oversized, 'size', { value: GFS_FILE_UPLOAD_MAX_BYTES + 1 })+ Object.defineProperty(oversized, 'size', { value: GFS_FILE_UPLOAD_PROTOCOL_MAX_BYTES + 1 })const arrayBuffer = vi.spyOn(oversized, 'arrayBuffer')renderFilesPage(pushToast)@@ -752,7 +752,10 @@ describe('FilesPage', () => {})await waitFor(() =>- expect(pushToast).toHaveBeenCalledWith('GFS uploads are limited to 200 MB per file.', 'error')+ expect(pushToast).toHaveBeenCalledWith(+ 'GFS uploads cannot exceed the 1 GiB Upload v2 protocol maximum.',+ 'error'+ ))expect(arrayBuffer).not.toHaveBeenCalled()expect(createFile).not.toHaveBeenCalled()