Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): stream folder-zip entries into one pre-sized buffer (BUG-175 review M2)
Mc13b114c39ce
diff --git a/desktop-app/ui/src/lib/__tests__/zipWriter.test.ts b/desktop-app/ui/src/lib/__tests__/zipWriter.test.tsindex 81ff6044f..135c01ec7 100644--- a/desktop-app/ui/src/lib/__tests__/zipWriter.test.ts+++ b/desktop-app/ui/src/lib/__tests__/zipWriter.test.ts@@ -111,4 +111,36 @@ describe('createZipWriter', () => {expect(archive.length).toBe(22)expect(readStoredZip(archive)).toEqual([])})++ it('emits exactly the declared structure — no slack, no assembly copy (M2)', () => {+ const writer = createZipWriter()+ const a = textBytes('first-entry')+ const b = textBytes('b')+ writer.addFile('root/a.txt', a)+ writer.addFile('root/nested/b.bin', b)++ const archive = writer.build()+ // 30+name+size local records, 46+name central records, 22-byte EOCD.+ const expected =+ 30 ++ 'root/a.txt'.length ++ a.length ++ (30 + 'root/nested/b.bin'.length + b.length) ++ (46 + 'root/a.txt'.length) ++ (46 + 'root/nested/b.bin'.length) ++ 22+ expect(archive.length).toBe(expected)+ })++ it('accepts a caller-provided capacity and keeps the same bytes', () => {+ const payload = textBytes('pre-sized payload')+ const writer = createZipWriter({ initialCapacityBytes: 1 })+ writer.addFile('docs/plan.md', payload)++ const archive = writer.build()+ expect(archive.length).toBe(+ 30 + 'docs/plan.md'.length + payload.length + 46 + 'docs/plan.md'.length + 22+ )+ expect(readStoredZip(archive)).toEqual([{ name: 'docs/plan.md', bytes: payload }])+ })})diff --git a/desktop-app/ui/src/lib/gfsFolderZip.ts b/desktop-app/ui/src/lib/gfsFolderZip.tsindex 196818330..393ffbeb1 100644--- a/desktop-app/ui/src/lib/gfsFolderZip.ts+++ b/desktop-app/ui/src/lib/gfsFolderZip.ts@@ -192,7 +192,6 @@ export async function createGfsFolderZip(}}- const writer = createZipWriter()const skipped: GfsZipSkippedEntry[] = []const files: Array<{ uri: string; path: string }> = []const rootName = sanitizeZipSegment(folder.name)@@ -201,7 +200,6 @@ export async function createGfsFolderZip(]const visitedFolders = new Set<string>()let plannedBytes = 0-// ── Phase 1: recursive listing (breadth-first, cursor-paginated). ──report({ phase: 'listing', filesFound: 0, filesAdded: 0, currentPath: rootName })while (queue.length) {@@ -244,7 +242,9 @@ export async function createGfsFolderZip(// Older servers omit `bytes`; a missing value must read as 0, not// NaN — NaN would poison `plannedBytes` and silently disable the// size guard for the rest of the walk (M1).- plannedBytes += Number.isFinite(child.bytes) ? child.bytes : 0+ const childBytes =+ typeof child.bytes === 'number' && Number.isFinite(child.bytes) ? child.bytes : 0+ plannedBytes += childBytesif (plannedBytes > maxTotalBytes) {throw new GfsFolderZipLimitError(`"${folder.name}" exceeds the ${formatZipBytes(maxTotalBytes)} folder-zip limit. Download smaller subfolders individually.`@@ -267,6 +267,16 @@ export async function createGfsFolderZip(// can still explain why nothing was archivable.if (files.length === 0) throw new GfsFolderZipEmptyError(folder.name, skipped)+ // Pre-size the single archive buffer (M2): local header (30) + payload ++ // central record (46), each carrying the UTF-8 name, plus the 22-byte EOCD.+ // With server-provided sizes this allocates once; without them the writer+ // falls back to doubling.+ const nameEncoder = new TextEncoder()+ const estimatedArchiveBytes =+ plannedBytes ++ files.reduce((sum, file) => sum + 76 + 2 * nameEncoder.encode(file.path).length, 22)+ const writer = createZipWriter({ initialCapacityBytes: estimatedArchiveBytes })+// ── Phase 2: throttled downloads, skipping per-resource denials. ──let addedBytes = 0for (const [index, file] of files.entries()) {diff --git a/desktop-app/ui/src/lib/zipWriter.ts b/desktop-app/ui/src/lib/zipWriter.tsindex 6b4f480dd..2efff0569 100644--- a/desktop-app/ui/src/lib/zipWriter.ts+++ b/desktop-app/ui/src/lib/zipWriter.ts@@ -7,6 +7,15 @@* a fully valid ZIP for every standard extractor. No compression also means* the total size guard can reason about byte budgets exactly.*+ * Memory contract (review M2): entries stream into ONE growable buffer as they+ * arrive — `addFile` copies the caller's bytes in and hands ownership back, so+ * per-entry buffers are released immediately instead of being retained until+ * assembly. `build()` appends the central directory in place and returns a+ * VIEW over the buffer, never a second copy. Callers that know the planned+ * content size (the zip walk does) pass `initialCapacityBytes` so the buffer+ * is allocated once; without it the buffer grows by doubling, whose last+ * reallocation transiently holds old + new buffers.+ ** Format reference: APPNOTE.TXT (PKWARE) — local file header (4.3.7), central* directory (4.3.12), end-of-central-directory (4.3.19). Names are UTF-8 with* the general-purpose flag bit 11 set. DOS timestamps are zero (deterministic@@ -19,6 +28,10 @@ const END_OF_CENTRAL_DIRECTORY_SIGNATURE = 0x06054b50const VERSION_NEEDED = 20const UTF8_FLAG = 0x0800const STORED_METHOD = 0+const EOCD_SIZE = 22+const LOCAL_HEADER_FIXED_SIZE = 30+const CENTRAL_HEADER_FIXED_SIZE = 46+const DEFAULT_INITIAL_CAPACITY_BYTES = 64 * 1024const CRC32_POLYNOMIAL = 0xedb88320@@ -48,12 +61,16 @@ export function crc32(bytes: Uint8Array): number {return (crc ^ 0xffffffff) >>> 0}-interface StoredEntry {+interface DirectoryEntry {nameBytes: Uint8Arraycrc: numbersize: numberlocalHeaderOffset: number- bytes: Uint8Array+}++export interface CreateZipWriterOptions {+ /** Expected final archive size, when the caller can compute it up front. */+ initialCapacityBytes?: number}export interface ZipWriter {@@ -62,7 +79,7 @@ export interface ZipWriter {/** True once any entry with this exact path was added. */has(path: string): booleanentryCount(): number- /** Assembles the final archive bytes (local records + central directory). */+ /** Finalizes the archive and returns its bytes (a view — do not mutate). */build(): Uint8Array<ArrayBuffer>}@@ -70,22 +87,58 @@ function encodeName(path: string): Uint8Array {return new TextEncoder().encode(path)}-export function createZipWriter(): ZipWriter {- const entries: StoredEntry[] = []+export function createZipWriter(options: CreateZipWriterOptions = {}): ZipWriter {+ const entries: DirectoryEntry[] = []const usedNames = new Set<string>()+ let capacity = Math.max(1, options.initialCapacityBytes ?? DEFAULT_INITIAL_CAPACITY_BYTES)+ let buffer = new Uint8Array(capacity)+ let length = 0++ const ensureCapacity = (extra: number) => {+ if (length + extra <= capacity) return+ let next = capacity+ while (next < length + extra) next *= 2+ const grown = new Uint8Array(next)+ grown.set(buffer.subarray(0, length))+ buffer = grown+ capacity = next+ }++ const write = (source: Uint8Array) => {+ ensureCapacity(source.length)+ buffer.set(source, length)+ length += source.length+ }+return {addFile(path, bytes) {const writtenPath = ensureUniqueName(path, usedNames)usedNames.add(writtenPath)const nameBytes = encodeName(writtenPath)- entries.push({- nameBytes,- crc: crc32(bytes),- size: bytes.length,- localHeaderOffset: 0,- bytes,- })+ const crc = crc32(bytes)+ const localHeaderOffset = length++ const header = new Uint8Array(LOCAL_HEADER_FIXED_SIZE + nameBytes.length)+ const view = new DataView(header.buffer)+ view.setUint32(0, LOCAL_FILE_HEADER_SIGNATURE, true)+ view.setUint16(4, VERSION_NEEDED, true)+ view.setUint16(6, UTF8_FLAG, true)+ view.setUint16(8, STORED_METHOD, true)+ view.setUint16(10, 0, true) // mod time+ view.setUint16(12, 0, true) // mod date+ view.setUint32(14, crc, true)+ view.setUint32(18, bytes.length, true) // compressed size (stored)+ view.setUint32(22, bytes.length, true) // uncompressed size+ view.setUint16(26, nameBytes.length, true)+ view.setUint16(28, 0, true) // extra length+ header.set(nameBytes, LOCAL_HEADER_FIXED_SIZE)+ write(header)+ // Copy the payload into the archive buffer; after this returns the+ // caller's per-entry buffer is no longer referenced (M2).+ write(bytes)++ entries.push({ nameBytes, crc, size: bytes.length, localHeaderOffset })return writtenPath},@@ -98,33 +151,9 @@ export function createZipWriter(): ZipWriter {},build() {- const parts: Uint8Array[] = []- let offset = 0- const localHeaderSize = (entry: StoredEntry) => 30 + entry.nameBytes.length-- for (const entry of entries) {- entry.localHeaderOffset = offset- const header = new Uint8Array(localHeaderSize(entry))- const view = new DataView(header.buffer)- view.setUint32(0, LOCAL_FILE_HEADER_SIGNATURE, true)- view.setUint16(4, VERSION_NEEDED, true)- view.setUint16(6, UTF8_FLAG, true)- view.setUint16(8, STORED_METHOD, true)- view.setUint16(10, 0, true) // mod time- view.setUint16(12, 0, true) // mod date- view.setUint32(14, entry.crc, true)- view.setUint32(18, entry.size, true) // compressed size (stored)- view.setUint32(22, entry.size, true) // uncompressed size- view.setUint16(26, entry.nameBytes.length, true)- view.setUint16(28, 0, true) // extra length- header.set(entry.nameBytes, 30)- parts.push(header, entry.bytes)- offset += header.length + entry.bytes.length- }-- const centralDirectoryOffset = offset+ const centralDirectoryOffset = lengthfor (const entry of entries) {- const record = new Uint8Array(46 + entry.nameBytes.length)+ const record = new Uint8Array(CENTRAL_HEADER_FIXED_SIZE + entry.nameBytes.length)const view = new DataView(record.buffer)view.setUint32(0, CENTRAL_DIRECTORY_SIGNATURE, true)view.setUint16(4, VERSION_NEEDED, true) // made by@@ -143,13 +172,12 @@ export function createZipWriter(): ZipWriter {view.setUint16(36, 0, true) // internal attributesview.setUint32(38, 0, true) // external attributesview.setUint32(42, entry.localHeaderOffset, true)- record.set(entry.nameBytes, 46)- parts.push(record)- offset += record.length+ record.set(entry.nameBytes, CENTRAL_HEADER_FIXED_SIZE)+ write(record)}- const centralDirectorySize = offset - centralDirectoryOffset+ const centralDirectorySize = length - centralDirectoryOffset- const eocd = new Uint8Array(22)+ const eocd = new Uint8Array(EOCD_SIZE)const view = new DataView(eocd.buffer)view.setUint32(0, END_OF_CENTRAL_DIRECTORY_SIGNATURE, true)view.setUint16(4, 0, true) // this disk@@ -159,16 +187,8 @@ export function createZipWriter(): ZipWriter {view.setUint32(12, centralDirectorySize, true)view.setUint32(16, centralDirectoryOffset, true)view.setUint16(20, 0, true) // comment length- parts.push(eocd)-- const total = parts.reduce((sum, part) => sum + part.length, 0)- const out = new Uint8Array(total)- let cursor = 0- for (const part of parts) {- out.set(part, cursor)- cursor += part.length- }- return out+ write(eocd)+ return buffer.subarray(0, length)},}}