A personal agent with a browser, terminal, files, and work that keeps going built with CopilotKit and AG-UI.
TypeScript
fix(worker): clean unpublished downloads after restart (#131)
Excelius4c8b5e0ba538
diff --git a/apps/worker/src/downloads.ts b/apps/worker/src/downloads.tsindex a0b43fe..134838d 100644--- a/apps/worker/src/downloads.ts+++ b/apps/worker/src/downloads.ts@@ -36,10 +36,19 @@ export async function readDownloadFailures(directory: string, recoverInterruptedconst path = join(folder, name);const outcome = JSON.parse(await readFile(path, "utf8")) as Outcome;if (outcome.status === "pending" && recoverInterrupted) {- if (await stat(join(directory, "downloads", `${outcome.id}.json`)).catch(() => null)) {+ const metadata = await stat(join(directory, "downloads", `${outcome.id}.json`)).catch(+ (error: unknown) => {+ if (error instanceof Error && "code" in error && error.code === "ENOENT") return null;+ throw error;+ },+ );+ if (metadata) {await rm(path, { force: true });continue;}+ // Clear unpublished files first so an incomplete cleanup can be retried.+ await rm(join(directory, "downloads", `${outcome.id}.pdf`), { force: true });+ await rm(join(directory, "downloads", `${outcome.id}.json.tmp`), { force: true });outcome.status = "failed";await persist(path, outcome);}diff --git a/tests/browser.test.ts b/tests/browser.test.tsindex cb386c7..815089d 100644--- a/tests/browser.test.ts+++ b/tests/browser.test.ts@@ -1,6 +1,6 @@import assert from "node:assert/strict";import { once } from "node:events";-import { mkdir, mkdtemp, rm, truncate, writeFile } from "node:fs/promises";+import { mkdir, mkdtemp, readFile, rm, symlink, truncate, writeFile } from "node:fs/promises";import { request } from "node:http";import { tmpdir } from "node:os";import { join } from "node:path";@@ -341,6 +341,150 @@ test("worker persists unsupported, oversized and interrupted download outcomes",}});+test("worker recovery cleans unpublished downloads and retains their interruption failures", async (t) => {+ const directory = await mkdtemp(join(tmpdir(), "openmuse-download-recovery-"));+ t.after(() => rm(directory, { recursive: true, force: true }));+ const folder = join(directory, "downloads");+ const outcomes = join(directory, "download-outcomes");+ await mkdir(folder);+ await mkdir(outcomes);+ const failure = {+ id: "00000000-0000-4000-8000-000000000006",+ name: "unfinished.pdf",+ code: "DOWNLOAD_INTERRUPTED",+ message: "The download was interrupted.",+ createdAt: "2026-09-15T00:00:00.000Z",+ };+ const pdf = join(folder, `${failure.id}.pdf`);+ const metadataTemp = join(folder, `${failure.id}.json.tmp`);+ const bytes = Buffer.from("%PDF-1.7\npartial download");+ await writeFile(pdf, bytes);+ await writeFile(metadataTemp, '{"id":');+ await writeFile(+ join(outcomes, `${failure.id}.json`),+ JSON.stringify({ ...failure, status: "pending" }),+ );++ assert.deepEqual(await readDownloadFailures(directory), []);+ assert.deepEqual(await readFile(pdf), bytes, "ordinary reads must not clean active downloads");+ assert.deepEqual(await readDownloadFailures(directory, true), [failure]);+ await assert.rejects(readFile(pdf), { code: "ENOENT" });+ await assert.rejects(readFile(metadataTemp), { code: "ENOENT" });+ assert.deepEqual(await readDownloadFailures(directory, true), [failure]);+});++test("worker recovery retries incomplete cleanup before recording failure", async (t) => {+ const directory = await mkdtemp(join(tmpdir(), "openmuse-download-recovery-"));+ t.after(() => rm(directory, { recursive: true, force: true }));+ const folder = join(directory, "downloads");+ const outcomes = join(directory, "download-outcomes");+ await mkdir(folder);+ await mkdir(outcomes);+ const failure = {+ id: "00000000-0000-4000-8000-000000000009",+ name: "unfinished.pdf",+ code: "DOWNLOAD_INTERRUPTED",+ message: "The download was interrupted.",+ createdAt: "2026-09-15T00:00:00.000Z",+ };+ const journal = join(outcomes, `${failure.id}.json`);+ await writeFile(journal, JSON.stringify({ ...failure, status: "pending" }));+ const pdf = join(folder, `${failure.id}.pdf`);+ await writeFile(pdf, "%PDF-1.7\npartial download");+ const metadataTemp = join(folder, `${failure.id}.json.tmp`);+ // A directory at the file path forces cleanup to fail without permission mocks.+ await mkdir(metadataTemp);++ await assert.rejects(readDownloadFailures(directory, true));+ assert.deepEqual(JSON.parse(await readFile(journal, "utf8")), { ...failure, status: "pending" });+ await assert.rejects(readFile(pdf), { code: "ENOENT" });+ await rm(metadataTemp, { recursive: true });+ assert.deepEqual(await readDownloadFailures(directory, true), [failure]);+ assert.deepEqual(await readDownloadFailures(directory, true), [failure]);+ assert.equal(JSON.parse(await readFile(journal, "utf8")).status, "failed");+});++test("worker recovery preserves downloads when metadata cannot be inspected", async (t) => {+ const directory = await mkdtemp(join(tmpdir(), "openmuse-download-recovery-"));+ t.after(() => rm(directory, { recursive: true, force: true }));+ const folder = join(directory, "downloads");+ const outcomes = join(directory, "download-outcomes");+ await mkdir(folder);+ await mkdir(outcomes);+ const id = "00000000-0000-4000-8000-000000000007";+ const outcome = {+ id,+ name: "unreadable.pdf",+ status: "pending",+ code: "DOWNLOAD_INTERRUPTED",+ message: "The download was interrupted.",+ createdAt: "2026-09-15T00:00:00.000Z",+ };+ const pdf = join(folder, `${id}.pdf`);+ const metadata = join(folder, `${id}.json`);+ const bytes = Buffer.from("%PDF-1.7\npreserve me");+ await writeFile(pdf, bytes);+ await writeFile(`${metadata}.tmp`, '{"id":');+ const journal = join(outcomes, `${id}.json`);+ await writeFile(journal, JSON.stringify(outcome));+ // A symlink loop raises a real filesystem error even when tests run as root.+ await symlink(`${id}.json`, metadata);++ await assert.rejects(readDownloadFailures(directory, true), { code: "ELOOP" });+ assert.deepEqual(await readFile(pdf), bytes);+ assert.equal(await readFile(`${metadata}.tmp`, "utf8"), '{"id":');+ assert.deepEqual(JSON.parse(await readFile(journal, "utf8")), outcome);+});++test("worker recovery reconciles published downloads and transfers without files", async (t) => {+ for (const published of [false, true]) {+ await t.test(+ published ? "published PDF is retained" : "missing files are tolerated",+ async (t) => {+ const directory = await mkdtemp(join(tmpdir(), "openmuse-download-recovery-"));+ t.after(() => rm(directory, { recursive: true, force: true }));+ const folder = join(directory, "downloads");+ const outcomes = join(directory, "download-outcomes");+ await mkdir(folder);+ await mkdir(outcomes);+ const failure = {+ id: "00000000-0000-4000-8000-000000000008",+ name: "report.pdf",+ code: "DOWNLOAD_INTERRUPTED",+ message: "The download was interrupted.",+ createdAt: "2026-09-15T00:00:00.000Z",+ };+ const journal = join(outcomes, `${failure.id}.json`);+ await writeFile(journal, JSON.stringify({ ...failure, status: "pending" }));+ const bytes = Buffer.from("%PDF-1.7\npublished download");+ const pdf = join(folder, `${failure.id}.pdf`);+ const metadata = join(folder, `${failure.id}.json`);+ const saved = {+ id: failure.id,+ name: failure.name,+ size: bytes.length,+ mimeType: "application/pdf",+ };+ if (published) {+ await writeFile(pdf, bytes);+ await writeFile(metadata, JSON.stringify(saved));+ }++ const expected = published ? [] : [failure];+ assert.deepEqual(await readDownloadFailures(directory, true), expected);+ assert.deepEqual(await readDownloadFailures(directory, true), expected);+ if (published) {+ assert.deepEqual(await readFile(pdf), bytes);+ assert.deepEqual(JSON.parse(await readFile(metadata, "utf8")), saved);+ await assert.rejects(readFile(journal), { code: "ENOENT" });+ } else {+ assert.equal(JSON.parse(await readFile(journal, "utf8")).status, "failed");+ }+ },+ );+ }+});+test("browser rejects private, special-use and encoded IP addresses", async () => {const blocked = ["127.0.0.1",@@ -418,6 +562,8 @@ test("worker protects all controls, validates before launch, and health revealsconst outcomeFolder = join(dataDir, savedId, "download-outcomes");await mkdir(downloadFolder, { recursive: true });await mkdir(outcomeFolder, { recursive: true });+ await writeFile(join(downloadFolder, `${pendingId}.pdf`), "%PDF-1.7\npartial download");+ await writeFile(join(downloadFolder, `${pendingId}.json.tmp`), '{"id":');await writeFile(join(outcomeFolder, `${pendingId}.json`),JSON.stringify({@@ -489,6 +635,10 @@ test("worker protects all controls, validates before launch, and health reveals).json();assert.equal(outcomes.downloads.length, 1);assert.equal(outcomes.failures[0]?.code, "DOWNLOAD_INTERRUPTED");+ await assert.rejects(readFile(join(downloadFolder, `${pendingId}.pdf`)), { code: "ENOENT" });+ await assert.rejects(readFile(join(downloadFolder, `${pendingId}.json.tmp`)), {+ code: "ENOENT",+ });const oversized = await fetch(`${base}/sessions/${savedId}/downloads/${downloadId}`, {headers,});