Skip to content

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

Fix download reservation, stale folders and symlink swaps

Fei2411e33ec3e0b47d
diff --git a/app/src/bot-views.mjs b/app/src/bot-views.mjs
index 64936579..c00370a9 100644
--- a/app/src/bot-views.mjs
+++ b/app/src/bot-views.mjs
@@ -178,10 +178,14 @@ export class BotViews {
event.preventDefault()
return
}
+ const downloadPath = this.#allowedDownloadPath(entry.botId, entry.downloadPath)
+ if (downloadPath === undefined) {
+ event.preventDefault()
+ return
+ }
try {
- mkdirSync(entry.downloadPath, { recursive: true })
const guid = randomUUID()
- const savePath = join(entry.downloadPath, guid)
+ const savePath = join(downloadPath, guid)
item.setSavePath(savePath)
entry.downloads.set(guid, { item, savePath })
this.#broadcast(entry.botId, { method: 'Browser.downloadWillBegin', params: {
@@ -205,7 +209,8 @@ export class BotViews {
#allowedDownloadPath(botId, requested) {
if (typeof requested !== 'string' || !isAbsolute(requested)) return undefined
const target = resolve(requested)
- const roots = (this.#downloadRoots.get(botId) ?? []).filter(root => containsPath(root, target))
+ const roots = (this.#downloadRoots.get(botId) ?? []).filter(root =>
+ containsPath(root.path, target) || (root.realPath !== undefined && containsPath(root.realPath, target)))
const temporaryRoot = resolve(tmpdir())
let temporaryRealRoot
try {
@@ -218,12 +223,12 @@ export class BotViews {
mkdirSync(target, { recursive: true })
const realTarget = realpathSync(target)
const inRoot = roots.some(root => {
- try { return containsPath(realpathSync(root), realTarget) } catch { return false }
+ return root.realPath !== undefined && containsPath(root.realPath, realTarget)
})
const inArtifacts = isArtifact && temporaryRealRoot !== undefined
&& isDirectChild(temporaryRealRoot, realTarget)
&& basename(realTarget).startsWith(PLAYWRIGHT_ARTIFACT_PREFIX)
- return inRoot || inArtifacts ? target : undefined
+ return inRoot || inArtifacts ? realTarget : undefined
} catch {
return undefined
}
@@ -293,7 +298,13 @@ export class BotViews {
if (!Array.isArray(downloadRoots) || downloadRoots.length > 4 || !Array.from(downloadRoots).every(root => typeof root === 'string' && isAbsolute(root))) {
throw new Error('Invalid browser download roots.')
}
- this.#downloadRoots.set(botId, [...new Set(downloadRoots.map(root => resolve(root)))])
+ this.#downloadRoots.set(botId, [...new Set(downloadRoots.map(root => resolve(root)))].map(path => {
+ try {
+ return { path, realPath: realpathSync(path) }
+ } catch {
+ return { path, realPath: undefined }
+ }
+ }))
this.#downloadPaths.delete(botId)
for (const entry of this.#views.values()) if (entry.botId === botId) entry.downloadPath = undefined
}
diff --git a/app/test/bot-views.test.mjs b/app/test/bot-views.test.mjs
index 736a7f21..a2cf00f0 100644
--- a/app/test/bot-views.test.mjs
+++ b/app/test/bot-views.test.mjs
@@ -1,6 +1,6 @@
import assert from 'node:assert/strict'
import { EventEmitter } from 'node:events'
-import { existsSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { test } from 'node:test'
@@ -195,7 +195,7 @@ test('CDP download behavior saves to the Bot path and emits download events', as
item.emit('done', {}, 'completed')
await Promise.race([eventsReady, new Promise((_, reject) => setTimeout(() => reject(new Error('download events were not relayed')), 1000))])
assert.equal(saved.length, 1)
- assert.match(saved[0], new RegExp(`^${downloadPath.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}[/\\\\]`, 'u'))
+ assert.match(saved[0], new RegExp(`^${realpathSync(downloadPath).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}[/\\\\]`, 'u'))
assert.equal(events[0].params.suggestedFilename, 'report.pdf')
assert.equal(events.some(message => message.method === 'Browser.downloadProgress' && message.params.state === 'completed'), true)
assert.equal(sessionId.length > 0, true)
@@ -230,7 +230,7 @@ test('download roots are isolated per Bot', async t => {
getFilename() { return 'report.pdf' },
})
handlers.get('bot:a')({ preventDefault() { throw new Error('Bot A download was refused') } }, accepted, views[0].webContents)
- assert.match(savedByA, new RegExp(`^${rootA.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}[/\\\\]`, 'u'))
+ assert.match(savedByA, new RegExp(`^${realpathSync(rootA).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}[/\\\\]`, 'u'))
let preventedForB = false
const refused = Object.assign(new EventEmitter(), {
setSavePath() { throw new Error('Bot B must not use Bot A download root') },
@@ -340,7 +340,38 @@ test('CDP accepts a direct Playwright artifacts directory under os.tmpdir()', as
let prevented = false
handlers.get('bot:a')({ preventDefault() { prevented = true } }, item, views[0].webContents)
assert.equal(prevented, false)
- assert.match(savePath, new RegExp(`^${downloadPath.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}[/\\\\]`, 'u'))
+ assert.match(savePath, new RegExp(`^${realpathSync(downloadPath).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}[/\\\\]`, 'u'))
+})
+
+test('a relay download is cancelled if its accepted directory is swapped for an outside symlink', async t => {
+ const { manager, handlers, views } = fixture()
+ t.after(() => manager.dispose())
+ const rootPath = mkdtempSync(join(tmpdir(), 'bot-views-download-symlink-swap-'))
+ const downloadPath = join(rootPath, 'downloads')
+ const outsidePath = mkdtempSync(join(tmpdir(), 'bot-views-download-outside-'))
+ t.after(() => rmSync(outsidePath, { recursive: true, force: true }))
+ t.after(() => rmSync(rootPath, { recursive: true, force: true }))
+ const entry = manager.create('a')
+ const socket = await openSocket(await manager.endpoint('a', { downloadRoots: [rootPath] }))
+ t.after(() => socket.close())
+ await request(socket, 1, 'Target.attachToTarget', { targetId: entry.tabId, flatten: true })
+ const accepted = await request(socket, 2, 'Browser.setDownloadBehavior', {
+ behavior: 'allowAndName',
+ downloadPath,
+ })
+ assert.deepEqual(accepted.result, {})
+
+ rmSync(downloadPath, { recursive: true, force: true })
+ symlinkSync(outsidePath, downloadPath)
+ let prevented = false
+ const item = Object.assign(new EventEmitter(), {
+ setSavePath(path) { writeFileSync(path, 'outside') },
+ getURL() { return 'https://example.com/file' },
+ getFilename() { return 'report.pdf' },
+ })
+ handlers.get('bot:a')({ preventDefault() { prevented = true } }, item, views[0].webContents)
+ assert.equal(prevented, true)
+ assert.deepEqual(readdirSync(outsidePath), [])
})
test('user hold closes relay sockets, rejects endpoints, and allows a new endpoint after give-back', async t => {
diff --git a/browser-plugins/test/write-safety.test.js b/browser-plugins/test/write-safety.test.js
index cf42e7c8..58b2ecde 100644
--- a/browser-plugins/test/write-safety.test.js
+++ b/browser-plugins/test/write-safety.test.js
@@ -84,6 +84,22 @@ async function fixtureServer() {
'content-disposition': 'attachment; filename="report.txt"',
})
response.end('download contents')
+ } else if (path === '/download-concurrent') {
+ const value = new URL(request.url ?? '/', `http://${request.headers.host}`).searchParams.get('value') ?? ''
+ response.writeHead(200, {
+ 'content-type': 'text/plain',
+ 'content-disposition': 'attachment; filename="report.txt"',
+ 'content-length': Buffer.byteLength(value),
+ })
+ setTimeout(() => response.end(value), 25)
+ } else if (path === '/download-fail') {
+ response.writeHead(200, {
+ 'content-type': 'text/plain',
+ 'content-disposition': 'attachment; filename="unfinished.txt"',
+ 'content-length': '1000',
+ })
+ response.write('partial')
+ setTimeout(() => request.socket.destroy(), 25)
} else if (path === '/hang' && request.method === 'POST') {
counters.hang += 1
request.resume()
@@ -520,6 +536,21 @@ test('standalone uploads without configured roots are refused', async (t) => {
assert.doesNotMatch(await session.snapshotText(), /notes\.txt/)
})
+test('uploads skip a missing legacy folder when the working folder exists', async t => {
+ const { fixture, profileRoot, session } = await setup(t, 500, profileRoot => [
+ join(profileRoot, 'working'),
+ join(profileRoot, 'browser-downloads', 'bot'),
+ ])
+ const working = join(profileRoot, 'working')
+ const file = join(working, 'notes.txt')
+ await mkdir(working, { recursive: true })
+ await writeFile(file, 'from working')
+ const snapshot = await session.open(fixture.url)
+ const result = await gateScope.run(() => {}, () => session.upload(findRef(snapshot, 'Upload file'), [file]))
+ assert.match(result, /notes\.txt/u)
+ assert.match(await session.snapshotText(), /notes\.txt/u)
+})
+
test('mounted downloads and uploads use the Bot working directory', async t => {
const { result, working } = await mountedDownloadAndUpload(t, profileRoot => ({
roots: [join(profileRoot, 'working'), join(profileRoot, 'team')],
@@ -576,3 +607,54 @@ test('downloads are gated, saved without overwriting, or cancelled on refusal',
assert.equal(await readFile(join(downloadDir, 'report.txt'), 'utf8'), 'download contents')
assert.equal(fixture.counters.upload, 0)
})
+
+test('concurrent downloads reserve unique names and preserve both files', async t => {
+ const fixture = await fixtureServer()
+ const profileRoot = await mkdtemp(join(tmpdir(), 'browser-concurrent-downloads-'))
+ const downloadDir = join(profileRoot, 'shared')
+ await mkdir(downloadDir, { recursive: true })
+ const sessions = []
+ t.after(async () => {
+ await Promise.all(sessions.map(session => session.close().catch(() => {})))
+ await fixture.close()
+ await rm(profileRoot, { recursive: true, force: true })
+ })
+ for (const botId of ['download-one', 'download-two']) {
+ sessions.push(await openBotBrowser({
+ botId,
+ profileRoot,
+ downloadDir,
+ playwrightUrl,
+ }))
+ }
+ const urls = ['first', 'second'].map(value => {
+ const url = new URL('download-concurrent', fixture.url)
+ url.searchParams.set('value', value)
+ return url.href
+ })
+ const results = await Promise.all(sessions.map((session, index) => session.open(urls[index])))
+ assert.ok(results.every(result => /Downloaded "report\.txt" to /u.test(result)))
+ const filenames = (await readdir(downloadDir)).sort()
+ assert.deepEqual(filenames, ['report (2).txt', 'report.txt'])
+ const contents = await Promise.all(filenames.map(filename => readFile(join(downloadDir, filename), 'utf8')))
+ assert.deepEqual(contents.sort(), ['first', 'second'])
+})
+
+test('downloads resolve a function-valued directory for every save and remove failed placeholders', async t => {
+ let destination
+ const { fixture, profileRoot, session } = await setup(t, 500, { downloadDir: async () => destination })
+ const first = join(profileRoot, 'first-downloads')
+ const second = join(profileRoot, 'second-downloads')
+ destination = first
+ const firstResult = await session.open(new URL('download', fixture.url).href)
+ destination = second
+ const secondResult = await session.open(new URL('download', fixture.url).href)
+ assert.ok(firstResult.includes(join(first, 'report.txt')))
+ assert.ok(secondResult.includes(join(second, 'report.txt')))
+ assert.equal(await readFile(join(first, 'report.txt'), 'utf8'), 'download contents')
+ assert.equal(await readFile(join(second, 'report.txt'), 'utf8'), 'download contents')
+
+ const failed = await session.open(new URL('download-fail', fixture.url).href)
+ assert.match(failed, /Download "unfinished\.txt" failed/u)
+ assert.deepEqual(await readdir(second), ['report.txt'])
+})
diff --git a/ds-bot/src/host/browser/mount.js b/ds-bot/src/host/browser/mount.js
index 8d4428d3..791d8681 100644
--- a/ds-bot/src/host/browser/mount.js
+++ b/ds-bot/src/host/browser/mount.js
@@ -64,7 +64,9 @@ export function mountBrowserPlugin(ctx, { toolDefs, defineTool, keyOf, profileRo
session = await openSession({
botId,
profileRoot,
- downloadDir: roots === undefined ? join(downloadRoot ?? join(profileRoot, 'browser-downloads'), botId) : roots[0],
+ downloadDir: roots === undefined
+ ? join(downloadRoot ?? join(profileRoot, 'browser-downloads'), botId)
+ : async () => (await downloadRoots(botId))[0],
headless,
playwrightUrl,
executablePath,
diff --git a/ds-bot/src/host/browser/session.js b/ds-bot/src/host/browser/session.js
index ae6248d0..562143e7 100644
--- a/ds-bot/src/host/browser/session.js
+++ b/ds-bot/src/host/browser/session.js
@@ -2,7 +2,7 @@
// share this directory, so a new conversation part keeps the login. Another Bot never
// opens it: profiles are not copied across the team.
import { randomUUID } from 'node:crypto'
-import { mkdir, readFile, realpath, stat, writeFile } from 'node:fs/promises'
+import { mkdir, open, readFile, realpath, stat, unlink, writeFile } from 'node:fs/promises'
import { basename, extname, isAbsolute, join, relative as relativePath } from 'node:path'
import { gateScope } from './gate.js'
@@ -86,9 +86,10 @@ async function loadChromium(playwrightUrl) {
}
/**
- * @param {{ botId: string, profileRoot: string, headless?: boolean, playwrightUrl?: string, executablePath?: string, actionTimeout?: number, downloadDir?: string, uploadRoots?: () => Promise<string[]>, connect?: (botId: string) => Promise<string>, getHold?: () => any, onHold?: (hold: any) => void }} options
+ * @param {{ botId: string, profileRoot: string, headless?: boolean, playwrightUrl?: string, executablePath?: string, actionTimeout?: number, downloadDir?: string | (() => Promise<string | undefined>), uploadRoots?: () => Promise<string[]>, connect?: (botId: string) => Promise<string>, getHold?: () => any, onHold?: (hold: any) => void }} options
*/
export async function openBotBrowser({ botId, profileRoot, downloadDir, headless = true, playwrightUrl, executablePath, actionTimeout = ACTION_TIMEOUT, connect, getHold, onHold, uploadRoots }) {
+ const getDownloadDir = typeof downloadDir === 'function' ? downloadDir : async () => downloadDir
const userDataDir = join(profileRoot, botId)
const chromium = await loadChromium(playwrightUrl)
const browser = connect
@@ -113,7 +114,7 @@ export async function openBotBrowser({ botId, profileRoot, downloadDir, headless
if (Array.isArray(saved) && saved.length > 0) await context.addCookies(saved)
} catch { /* no cookies yet */ }
}
- return createSession({ context, browser, page, botId, userDataDir, cookieFile, downloadDir, actionTimeout, getHold, onHold, uploadRoots })
+ return createSession({ context, browser, page, botId, userDataDir, cookieFile, downloadDir: getDownloadDir, actionTimeout, getHold, onHold, uploadRoots })
}
function createSession({ context, browser, page, botId, userDataDir, cookieFile, downloadDir, actionTimeout, getHold, onHold, uploadRoots }) {
@@ -297,7 +298,7 @@ function createSession({ context, browser, page, botId, userDataDir, cookieFile,
return new Error(`This looks like a payment ("${action.label}" on ${action.host}). The user makes payments themselves: call browser_handoff with reason "payment" and stop. Nothing was done.`)
}
- async function reserveDownloadPath(filename) {
+ async function reserveDownloadPath(filename, directory) {
const name = Array.from(basename(filename), character => {
const code = character.charCodeAt(0)
return code < 32 || '<>:"/\\|?*'.includes(character) ? '_' : character
@@ -306,11 +307,18 @@ function createSession({ context, browser, page, botId, userDataDir, cookieFile,
const extension = extname(safeName)
const stem = extension ? safeName.slice(0, -extension.length) : safeName
for (let index = 1; ; index += 1) {
- const candidate = join(downloadDir, index === 1 ? safeName : `${stem} (${index})${extension}`)
+ const candidate = join(directory, index === 1 ? safeName : `${stem} (${index})${extension}`)
try {
- await stat(candidate)
+ const handle = await open(candidate, 'wx')
+ try {
+ await handle.close()
+ } catch (error) {
+ await unlink(candidate).catch(() => {})
+ throw error
+ }
+ return candidate
} catch (error) {
- if (error.code === 'ENOENT') return candidate
+ if (error.code === 'EEXIST') continue
throw error
}
}
@@ -330,13 +338,16 @@ function createSession({ context, browser, page, botId, userDataDir, cookieFile,
const message = String(error?.message ?? error).replace(/\s+/g, ' ').slice(0, 160)
return message || `Download "${filename}" was refused. Nothing was saved.`
}
+ let path
try {
- if (!downloadDir) throw new Error('No download directory is configured.')
- await mkdir(downloadDir, { recursive: true })
- const path = await reserveDownloadPath(filename)
+ const directory = await downloadDir()
+ if (!directory) throw new Error('No download directory is configured.')
+ await mkdir(directory, { recursive: true })
+ path = await reserveDownloadPath(filename, directory)
await download.saveAs(path)
return `Downloaded "${filename}" to ${path}.`
} catch (error) {
+ if (path) await unlink(path).catch(() => {})
await download.cancel().catch(() => {})
await download.delete().catch(() => {})
const message = String(error?.message ?? error).replace(/\s+/g, ' ').slice(0, 160)
@@ -635,8 +646,19 @@ function createSession({ context, browser, page, botId, userDataDir, cookieFile,
if (roots.length === 0) {
throw new Error(`Uploads must come from an allowed folder. ${paths[0]} is outside them. Nothing was uploaded.`)
}
- if (downloadDir) await mkdir(downloadDir, { recursive: true })
- const realRoots = await Promise.all(roots.map(root => realpath(root)))
+ const directory = await downloadDir()
+ if (directory) await mkdir(directory, { recursive: true })
+ const realRoots = (await Promise.all(roots.map(async root => {
+ try {
+ return await realpath(root)
+ } catch (error) {
+ if (error.code === 'ENOENT') return undefined
+ throw error
+ }
+ }))).filter(root => root !== undefined)
+ if (realRoots.length === 0) {
+ throw new Error(`Uploads must come from an allowed folder. ${paths[0]} is outside them. Nothing was uploaded.`)
+ }
const files = []
const labels = []
for (const path of paths) {
diff --git a/ds-bot/test/browser-plugins.test.js b/ds-bot/test/browser-plugins.test.js
index 2f9e8df4..ef407dd5 100644
--- a/ds-bot/test/browser-plugins.test.js
+++ b/ds-bot/test/browser-plugins.test.js
@@ -130,7 +130,7 @@ test('app browser endpoint carries download roots for the Bot resolved from the
connect: appBrowserEndpoint,
openSession: async options => {
assert.equal(options.botId, 'b2')
- assert.equal(options.downloadDir, roots[0])
+ assert.equal(await options.downloadDir(), roots[0])
assert.deepEqual(await options.uploadRoots(), roots)
await options.connect(options.botId)
return { close: async () => {} }