Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): stop transfers at exact budget exhaustion with clean finish (R2-L2)
Ma108761bcd02
diff --git a/desktop-app/ui/src/lib/__tests__/gfsFolderZip.test.ts b/desktop-app/ui/src/lib/__tests__/gfsFolderZip.test.tsindex a7b39a0c8..3137cd698 100644--- a/desktop-app/ui/src/lib/__tests__/gfsFolderZip.test.ts+++ b/desktop-app/ui/src/lib/__tests__/gfsFolderZip.test.ts@@ -416,12 +416,76 @@ describe('createGfsFolderZip', () => {})- await expect(- createGfsFolderZip(- { resourceId: 'root', drive: 'main', name: 'Ghost' },- { deps, limits: { maxTotalBytes: 1024 } }- )- ).rejects.toThrow(/exceeded the 1 KiB folder-zip limit while downloading/)+ const result = await createGfsFolderZip(+ { resourceId: 'root', drive: 'main', name: 'Ghost' },+ { deps, limits: { maxTotalBytes: 1024 } }+ ).catch(error => error)+ // R2-L2: a producer-refused body no longer kills the job — the file is+ // skipped with a visible reason and the walk terminates cleanly.+ expect(result).toBeInstanceOf(GfsFolderZipEmptyError)+ expect((result as GfsFolderZipEmptyError).skipped).toEqual([+ { path: 'Ghost/ghost.bin', reason: 'Zip byte limit reached' },+ ])+ })++ it('stops before the next transfer at exact budget exhaustion, never maxBytes 0 (R2-L2)', async () => {+ // Missing declared sizes: the first file honestly consumes the whole+ // budget; the two behind it must never be requested (a zero bound is an+ // invalid IPC), and the archive finishes with a visible notice per file.+ const seenBounds: Array<number | undefined> = []+ const deps = depsFor(+ {+ root: [+ {+ items: [+ folder({+ resourceId: 'fill',+ name: 'fill.bin',+ bytes: undefined as unknown as number,+ }),+ folder({+ resourceId: 'after-a',+ name: 'after-a.bin',+ bytes: undefined as unknown as number,+ }),+ folder({+ resourceId: 'after-b',+ name: 'after-b.bin',+ bytes: undefined as unknown as number,+ }),+ ],+ nextCursor: null,+ },+ ],+ },+ {+ 'gfs://main/fill': async (_uri: string, options?: { maxBytes?: number }) => {+ seenBounds.push(options?.maxBytes)+ return { bytes: new ArrayBuffer(1024) }+ },+ 'gfs://main/after-a': async (_uri: string, options?: { maxBytes?: number }) => {+ seenBounds.push(options?.maxBytes)+ return { bytes: new ArrayBuffer(1) }+ },+ 'gfs://main/after-b': async (_uri: string, options?: { maxBytes?: number }) => {+ seenBounds.push(options?.maxBytes)+ return { bytes: new ArrayBuffer(1) }+ },+ }+ )++ const result = await createGfsFolderZip(+ { resourceId: 'root', drive: 'main', name: 'Exact' },+ { deps, limits: { maxTotalBytes: 1024 } }+ )++ expect(seenBounds).toEqual([1024])+ expect(result.fileCount).toBe(1)+ expect(result.skipped).toEqual([+ { path: 'Exact/after-a.bin', reason: 'Zip byte limit reached' },+ { path: 'Exact/after-b.bin', reason: 'Zip byte limit reached' },+ ])+ expect(zipEntryNames(result.bytes)).toEqual(['Exact/fill.bin'])})it('carries a near-limit folder through download, zip and save-sized output (R1-H2)', async () => {diff --git a/desktop-app/ui/src/lib/gfsFolderZip.ts b/desktop-app/ui/src/lib/gfsFolderZip.tsindex e0995180d..e08ff6039 100644--- a/desktop-app/ui/src/lib/gfsFolderZip.ts+++ b/desktop-app/ui/src/lib/gfsFolderZip.ts@@ -423,6 +423,17 @@ export async function createGfsFolderZip(let addedBytes = 0for (const [index, file] of files.entries()) {throwIfAborted()+ // R2-L2 — exact budget exhaustion: stop BEFORE the next transfer so a+ // zero-or-negative bound is never requested; the un-fetched files are+ // reported as visible skips and the archive finishes cleanly with what it+ // holds (spec: round-2 extensions).+ const remainingBytes = maxTotalBytes - addedBytes+ if (remainingBytes <= 0) {+ for (const remainingFile of files.slice(index)) {+ skipped.push({ path: remainingFile.path, reason: 'Zip byte limit reached' })+ }+ break+ }// 1-based: the copy says "downloading N of M" while file N is fetched.report({phase: 'downloading',@@ -437,16 +448,16 @@ export async function createGfsFolderZip(// over-limit body (dishonest or missing declared size) is rejected// before it materializes in the renderer; the receipt check below stays// as the backstop.- const remainingBytes = maxTotalBytes - addedBytesbytes = (await withRateLimitRetry(() => download(file.uri, { maxBytes: remainingBytes }))).bytes} catch (error) {if (isFolderZipAbortError(error)) throw errorconst message = toMessage(error)if (parseHttpStatus(message) === 413) {- throw new GfsFolderZipLimitError(- `"${folder.name}" exceeded the ${formatZipBytes(maxTotalBytes)} folder-zip limit while downloading. Download smaller subfolders individually.`- )+ // The remaining budget cannot hold this body: skip it visibly and let+ // the walk terminate cleanly (later files hit the remaining-0 rule).+ skipped.push({ path: file.path, reason: 'Zip byte limit reached' })+ continue}if (isAccessDenied(message)) {skipped.push({ path: file.path, reason: 'Permission denied' })@@ -457,6 +468,8 @@ export async function createGfsFolderZip(const data = new Uint8Array(bytes)addedBytes += data.lengthif (addedBytes > maxTotalBytes) {+ // Defensive backstop: the producer-side bound makes this unreachable+ // unless the bound itself was ignored — fail loudly, not silently.throw new GfsFolderZipLimitError(`"${folder.name}" exceeded the ${formatZipBytes(maxTotalBytes)} folder-zip limit while downloading. Download smaller subfolders individually.`)@@ -464,6 +477,10 @@ export async function createGfsFolderZip(writer.addFile(file.path, data)}+ // Downloads can skip every file (permissions, exhausted budget): the empty+ // short-circuit applies to the archived result, not only the planned list.+ if (writer.entryCount() === 0) throw new GfsFolderZipEmptyError(folder.name, skipped)+// ── Phase 3: assemble the archive. ──report({phase: 'assembling',