A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.
TypeScript
fix(daemon): preserve sent attachments during upload cleanup
ismaelsuarezc869c9652b39
diff --git a/docs/design.md b/docs/design.mdindex d22b6cf..95e11f5 100644--- a/docs/design.md+++ b/docs/design.md@@ -199,7 +199,7 @@ The user can talk to the assistant: a mic button in the composer, and spoken repThe user can send files to the assistant (never the other way, L91).- Upload (`packages/daemon/src/uploads.ts`): `POST /upload`, one file per request, the raw bytes as the body, the name URI-encoded in `X-File-Name`, and `X-Upload-Id` to add a file to the folder of the message's first file. The access key goes only in `Authorization: Bearer`; a key in the address is ignored. A foreign `Origin` is refused as on the WebSocket, and allowed origins (the desktop app's `tauri://localhost`) get a CORS preflight answer. The answer is `{uploadId, name, size, mime, path}`, with `path` relative to the workspace.-- Storage and limits (S31.3): `<workspace>/uploads/<uploadId>/<name>`, folders 0700 and files 0600, written with `wx`. Names keep only the last path segment, without control or invisible characters, leading dots, or reserved names (`file` when nothing is left), and duplicates become `name (2).ext`. The daemon enforces 25 MB per file and 10 files / 50 MB per message (`ATTACHMENT_LIMITS`) while the bytes arrive: a declared `Content-Length` over the limit is refused before reading, and a refused or broken upload removes its partial file. Nothing is opened or run. The type comes from the first bytes (PNG, JPEG, GIF, WebP, PDF), never from the name. An upload never sent is removed after an hour.+- Storage and limits (S31.3): `<workspace>/uploads/<uploadId>/<name>`, folders 0700 and files 0600, written with `wx`. Names keep only the last path segment, without control or invisible characters, leading dots, or reserved names (`file` when nothing is left), and duplicates become `name (2).ext`. The daemon enforces 25 MB per file and 10 files / 50 MB per message (`ATTACHMENT_LIMITS`) while the bytes arrive: a declared `Content-Length` over the limit is refused before reading, and a refused or broken upload removes its partial file. Nothing is opened or run. The type comes from the first bytes (PNG, JPEG, GIF, WebP, PDF), never from the name. On a later upload, unsent files from batches at least an hour old are removed. Cleanup deletes only unsent files and removes their folder only if empty; files already sent from the same batch stay available.- Sending: `send` takes `attachments: {uploadId, name}[]` (1-10; the text may then be empty). Each upload is sent once; an unknown or used one answers `error attachment_not_found`, and nothing reaches the engine. The bridge appends an `<attachments>` block to the prompt, with one JSON line per file (name, size, type, absolute path). Images up to 3.75 MB (about 5 MB in base64) go to the engine as `images` only when the current model accepts them (`supervisor.modelImages`); otherwise the line says the model cannot see images.- Display: the block is stored with the message in the session file, so `user_message`, `history`, the queue, and titles show the user's own text, and `attachments: {name, size, mime}[]` become chips. A plain `send` is unchanged.- UI: a paper-clip button, drag and drop onto the chat, and pasted images add chips (name, size, ✕). The app checks the limits first and explains a refusal on the chip. Files upload on send with `XMLHttpRequest`, for progress, and an upload error stays on its chip with the message kept. In the desktop panel, HTML5 drops need the window's native drag-and-drop handler off (`disable_drag_drop_handler()` on the panel's `WebviewWindowBuilder`).diff --git a/packages/daemon/src/uploads.ts b/packages/daemon/src/uploads.tsindex b69b84d..f1696cb 100644--- a/packages/daemon/src/uploads.ts+++ b/packages/daemon/src/uploads.ts@@ -311,13 +311,14 @@ export class UploadStore {}}- /** Removes message folders that were never sent. */+ /** Removes expired unsent files, preserving sent files that share their folder. */private prune(): void {const cutoff = this.now() - this.ttlMs;for (const [uploadId, batch] of this.batches) {if (batch.createdAt > cutoff || batch.writing.size > 0) continue;- this.batches.delete(uploadId);- rmSync(batch.dir, { recursive: true, force: true });+ for (const file of batch.files.values()) rmSync(file.path, { force: true });+ batch.files.clear();+ this.dropIfEmpty(uploadId, batch);}}}diff --git a/packages/daemon/test/uploads.test.ts b/packages/daemon/test/uploads.test.tsindex 72aa824..f37d473 100644--- a/packages/daemon/test/uploads.test.ts+++ b/packages/daemon/test/uploads.test.ts@@ -138,6 +138,62 @@ describe("upload store", () => {expect(outcome).toMatchObject({ ok: false, status: 404, code: "upload_not_found" });});+ it("expires only unsent siblings, preserving sent files and refusing reuse", async () => {+ const workspace = tempDir();+ let now = 0;+ const uploads = new UploadStore({ workspace, ttlMs: 100, now: () => now });+ const sent = await uploads.receive(Readable.from(["SENT"]), { name: "sent.txt" });+ if (!sent.ok) throw new Error(sent.message);+ const pending = await uploads.receive(Readable.from(["UNSENT"]), {+ name: "pending.txt",+ uploadId: sent.file.uploadId,+ });+ if (!pending.ok) throw new Error(pending.message);+ uploads.consume([sent.file]);++ now = 99;+ const fresh = await uploads.receive(Readable.from(["FRESH"]), { name: "fresh.txt" });+ if (!fresh.ok) throw new Error(fresh.message);+ expect(uploads.resolve([pending.file]).ok).toBe(true);+ expect(readFileSync(join(workspace, pending.file.path), "utf8")).toBe("UNSENT");++ now = 100;+ const trigger = await uploads.receive(Readable.from(["TRIGGER"]), { name: "trigger.txt" });+ expect(trigger.ok).toBe(true);+ expect(existsSync(join(workspace, pending.file.path))).toBe(false);+ expect(readFileSync(join(workspace, sent.file.path), "utf8")).toBe("SENT");+ const unavailable = {+ ok: false,+ code: "attachment_not_found",+ message: "A file you attached is no longer available. Attach it again.",+ };+ expect(uploads.resolve([sent.file])).toEqual(unavailable);+ expect(uploads.resolve([pending.file])).toEqual(unavailable);+ expect(uploads.resolve([fresh.file]).ok).toBe(true);+ expect(readFileSync(join(workspace, fresh.file.path), "utf8")).toBe("FRESH");+ });++ it("still removes wholly unsent expired folders and preserves wholly sent folders", async () => {+ const workspace = tempDir();+ let now = 0;+ const uploads = new UploadStore({ workspace, ttlMs: 100, now: () => now });+ const pending = await uploads.receive(Readable.from(["UNSENT"]), { name: "pending.txt" });+ if (!pending.ok) throw new Error(pending.message);+ const sent = await uploads.receive(Readable.from(["SENT"]), { name: "sent.txt" });+ if (!sent.ok) throw new Error(sent.message);+ uploads.consume([sent.file]);++ now = 100;+ const trigger = await uploads.receive(Readable.from(["TRIGGER"]), { name: "trigger.txt" });+ if (!trigger.ok) throw new Error(trigger.message);+ expect(existsSync(join(workspace, "uploads", pending.file.uploadId))).toBe(false);+ expect(readFileSync(join(workspace, sent.file.path), "utf8")).toBe("SENT");+ expect(uploads.resolve([pending.file]).ok).toBe(false);+ expect(uploads.resolve([sent.file]).ok).toBe(false);+ expect(uploads.resolve([trigger.file]).ok).toBe(true);+ expect(readFileSync(join(workspace, trigger.file.path), "utf8")).toBe("TRIGGER");+ });+it("hands out stored files once: unknown names and used uploads are refused", async () => {const { workspace, uploads } = store();const sent = await uploads.receive(Readable.from(["hello"]), { name: "a.txt" });