Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Keep finished downloads when the save fails

Fei241135391b32ab58
diff --git a/app/src/downloads.mjs b/app/src/downloads.mjs
index 2b1e6cfe..c762e333 100644
--- a/app/src/downloads.mjs
+++ b/app/src/downloads.mjs
@@ -6,9 +6,9 @@
// download starts into a staging folder of its own, and moves to the chosen file once both the
// answer and the download are in. A cancelled picker cancels the download.
import { randomUUID } from 'node:crypto'
-import { copyFileSync, existsSync, mkdirSync, renameSync, rmSync } from 'node:fs'
+import { constants, copyFileSync, existsSync, mkdirSync, renameSync, rmSync } from 'node:fs'
import { homedir as osHomedir } from 'node:os'
-import { basename, extname, join } from 'node:path'
+import { basename, dirname, extname, join } from 'node:path'
/**
* @param {{
@@ -49,15 +49,36 @@ export function downloadSaver({ window, ask, staging, getPath, homedir = osHomed
target = null
}
if (target === null) item.cancel()
- else if (target === undefined) target = folder === undefined ? undefined : freePath(folder, name)
const state = await finished
+ if (state !== 'completed' || target === null) {
+ rmSync(holding, { recursive: true, force: true })
+ return
+ }
+ // A picker that cannot run means the start folder under a free name. The name is
+ // picked only now, in the same breath as the move, so two downloads of one name
+ // cannot take the same path.
+ if (target === undefined && folder !== undefined) target = freePath(folder, name)
+ if (typeof target !== 'string') {
+ log.warn(`DS Bot: a finished download has nowhere to go; it stays at ${staged}`)
+ return
+ }
try {
- if (state === 'completed' && typeof target === 'string') move(staged, target)
+ move(staged, target)
} catch (error) {
- log.warn(`DS Bot: a download could not be moved to ${target}: ${message(error)}`)
- } finally {
- rmSync(holding, { recursive: true, force: true })
+ if (folder === undefined) {
+ log.warn(`DS Bot: a download could not be moved to ${target}; it stays at ${staged}: ${message(error)}`)
+ return
+ }
+ try {
+ const fallback = freePath(folder, name)
+ move(staged, fallback)
+ log.warn(`DS Bot: a download could not be moved to ${target}; it went to ${fallback} instead: ${message(error)}`)
+ } catch (secondError) {
+ log.warn(`DS Bot: a download could not be moved to ${target}; it stays at ${staged}: ${message(secondError)}`)
+ return
+ }
}
+ rmSync(holding, { recursive: true, force: true })
})()
}
}
@@ -70,13 +91,22 @@ function safeName(name) {
return plain === '' || plain === '.' || plain === '..' ? 'download' : plain
}
-// The picker asked before it replaced a file; a rename does not cross drives.
-function move(from, to) {
+// The picker asked before it replaced a file; a rename does not cross drives. The copy
+// lands next to the target first and then takes its name, so a failed copy leaves the
+// old file whole.
+export function move(from, to, fs = { renameSync, copyFileSync, rmSync }) {
try {
- renameSync(from, to)
+ fs.renameSync(from, to)
} catch (error) {
if (error?.code !== 'EXDEV' && error?.code !== 'EPERM' && error?.code !== 'EEXIST') throw error
- copyFileSync(from, to)
+ const part = join(dirname(to), `.${basename(to)}.${randomUUID()}.part`)
+ try {
+ fs.copyFileSync(from, part, constants.COPYFILE_EXCL)
+ fs.renameSync(part, to)
+ } catch (copyError) {
+ fs.rmSync(part, { force: true })
+ throw copyError
+ }
}
}
diff --git a/app/test/downloads.test.mjs b/app/test/downloads.test.mjs
index b8b2ee19..95bff0fd 100644
--- a/app/test/downloads.test.mjs
+++ b/app/test/downloads.test.mjs
@@ -3,11 +3,11 @@
// cancelled picker cancels it; other contents' downloads pass through. Temp folders only.
import assert from 'node:assert/strict'
import { EventEmitter } from 'node:events'
-import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
+import { copyFileSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { dirname, join } from 'node:path'
import { after, test } from 'node:test'
-import { downloadSaver, freePath } from '../src/downloads.mjs'
+import { downloadSaver, freePath, move } from '../src/downloads.mjs'
const top = mkdtempSync(join(tmpdir(), 'ds-bot-downloads-'))
after(() => { rmSync(top, { recursive: true, force: true }) })
@@ -175,3 +175,86 @@ test('the window is asked for at the moment of the download, because the app may
save(event(), item(), contents)
assert.equal(asked, 1)
})
+
+test('a download whose chosen path cannot be written lands in the downloads folder instead', async () => {
+ const at = place()
+ const target = join(at.home, 'missing', 'picked.png')
+ const warnings = []
+ const { contents, save } = saver(at, async () => target, { log: { warn: line => warnings.push(line) } })
+ const download = item()
+ save(event(), download, contents)
+ download.finish('png')
+ const fallback = join(at.downloads, 'chart.png')
+ await until(() => existsSync(fallback))
+ assert.equal(readFileSync(fallback, 'utf8'), 'png')
+ assert.ok(warnings.some(line => line.includes(target) && line.includes(fallback)), 'the warning names both paths')
+ await until(() => readdirSync(at.staging).length === 0)
+ assert.deepEqual(readdirSync(at.staging), [], 'the staging folder is cleared once the file is placed')
+})
+
+test('a download that can go nowhere keeps its staged file', async () => {
+ const at = place()
+ rmSync(at.downloads, { recursive: true })
+ writeFileSync(at.downloads, 'not a folder')
+ const warnings = []
+ const { contents, save } = saver(at, async () => join(at.home, 'missing', 'picked.png'), { log: { warn: line => warnings.push(line) } })
+ const download = item()
+ save(event(), download, contents)
+ download.finish('png')
+ await until(() => warnings.length > 0)
+ assert.ok(warnings.some(line => line.includes(download.path)), 'the warning names the staged file')
+ assert.equal(readFileSync(download.path, 'utf8'), 'png', 'the staged file stays whole')
+})
+
+test('a cross-drive move copies next to the target, then takes its name', () => {
+ const at = place()
+ const staged = join(at.staging, 'held', 'chart.png')
+ mkdirSync(dirname(staged), { recursive: true })
+ writeFileSync(staged, 'new')
+ const target = join(at.downloads, 'chart.png')
+ writeFileSync(target, 'old')
+ const fs = {
+ renameSync(from, to) {
+ if (from === staged) throw Object.assign(new Error('cross-device link not permitted'), { code: 'EXDEV' })
+ renameSync(from, to)
+ },
+ copyFileSync,
+ rmSync,
+ }
+ move(staged, target, fs)
+ assert.equal(readFileSync(target, 'utf8'), 'new')
+ assert.deepEqual(readdirSync(at.downloads), ['chart.png'], 'no temp file is left next to the target')
+})
+
+test('a cross-drive move whose copy fails leaves the old file and the staged file alone', () => {
+ const at = place()
+ const staged = join(at.staging, 'held', 'chart.png')
+ mkdirSync(dirname(staged), { recursive: true })
+ writeFileSync(staged, 'new')
+ const target = join(at.downloads, 'chart.png')
+ writeFileSync(target, 'old')
+ const fs = {
+ renameSync() { throw Object.assign(new Error('cross-device link not permitted'), { code: 'EXDEV' }) },
+ copyFileSync(_from, to) { writeFileSync(to, 'partial'); throw new Error('no space left on device') },
+ rmSync,
+ }
+ assert.throws(() => move(staged, target, fs), /no space left/)
+ assert.equal(readFileSync(target, 'utf8'), 'old', 'the old file is not truncated')
+ assert.deepEqual(readdirSync(at.downloads), ['chart.png'], 'the failed temp file is removed')
+ assert.equal(readFileSync(staged, 'utf8'), 'new', 'the staged copy stays')
+})
+
+test('two downloads of one name without a picker each keep their own file', async () => {
+ const at = place()
+ const { contents, save } = saver(at, async () => undefined)
+ const first = item('chart.png')
+ const second = item('chart.png')
+ save(event(), first, contents)
+ save(event(), second, contents)
+ first.finish('one')
+ second.finish('two')
+ const names = [join(at.downloads, 'chart.png'), join(at.downloads, 'chart (2).png')]
+ await until(() => names.every(existsSync))
+ const texts = names.map(name => readFileSync(name, 'utf8')).sort()
+ assert.deepEqual(texts, ['one', 'two'], 'each download kept its own bytes')
+})