Skip to content

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

Merge remote-tracking branch 'origin/cursor/bot-browser-plugins-c44c' into feat/bot-browser-takeover

Fei24117c389eea96bf
diff --cc ds-bot/src/host/browser-plugins.js
index 5cda4663,746abbcb..0beeeebb
--- a/ds-bot/src/host/browser-plugins.js
+++ b/ds-bot/src/host/browser-plugins.js
@@@ -116,14 -94,7 +116,14 @@@ export function install(rt)
? rt.config.browserHeadless
: defaultBrowserHeadless(process.platform)
const downloadRoot = join(rt.home, 'browser-downloads')
+ async function downloadRoots(botId) {
+ await rt.load()
+ const team = rt.teamFolder()
+ const requested = rt.workDirOf({ botId })
+ const working = requested !== undefined && await rt.isFolder(requested) ? requested : team
+ return [...new Set([working, team].filter(root => typeof root === 'string' && isAbsolute(root)))]
+ }
- mountBrowserPlugin(rt.ctx, {
+ const mounted = mountBrowserPlugin(rt.ctx, {
toolDefs: plugin.tools ?? plugin.toolDefs,
defineTool,
profileRoot: join(rt.home, 'browser-profiles'),
diff --cc ds-bot/src/host/browser/mount.js
index 6de9ca65,8777badb..711f9bd1
--- a/ds-bot/src/host/browser/mount.js
+++ b/ds-bot/src/host/browser/mount.js
@@@ -30,24 -19,24 +31,32 @@@ export const browserOutput =
/**
* @param {any} ctx
- * @param {{ toolDefs: any[], defineTool: Function, keyOf: (exec: any) => Promise<string|undefined>, profileRoot: string, downloadRoot?: string, uploadRoots?: (botId: string) => Promise<string[]>, headless: boolean, playwrightUrl?: string, executablePath?: string, actionTimeout?: number, authorize?: (exec: any, botId: string, action: any) => Promise<void>, completeFor?: (exec: any) => Function }} options
+ * @param {{ toolDefs: any[], defineTool: Function, keyOf: (exec: any) => Promise<string|undefined>, profileRoot: string, downloadRoot?: string, downloadRoots?: (botId: string) => Promise<string[]>, uploadRoots?: (botId: string) => Promise<string[]>, headless: boolean, playwrightUrl?: string, executablePath?: string, actionTimeout?: number, authorize?: (exec: any, botId: string, action: any) => Promise<void>, completeFor?: (exec: any) => Function, connect?: (botId: string, downloadRoots?: string[]) => Promise<string>, getHold?: (botId: string) => any, onHold?: (botId: string, hold: any) => void, subscribeHolds?: (listener: (botId: string, hold: any) => void) => (() => void) | undefined, openSession?: typeof openBotBrowser }} options
*/
-export function mountBrowserPlugin(ctx, { toolDefs, defineTool, keyOf, profileRoot, downloadRoot, uploadRoots, headless, playwrightUrl, executablePath, actionTimeout, authorize, completeFor }) {
+export function mountBrowserPlugin(ctx, { toolDefs, defineTool, keyOf, profileRoot, downloadRoot, downloadRoots, uploadRoots, headless, playwrightUrl, executablePath, actionTimeout, authorize, completeFor, connect, getHold, onHold, subscribeHolds, openSession = openBotBrowser }) {
const sessions = new Map()
+ const unsubscribeHolds = subscribeHolds?.((botId, hold) => {
+ if (hold?.by !== 'user') return
+ const session = sessions.get(botId)
+ if (!session) return
+ sessions.delete(botId)
- void session.close()
++ void Promise.resolve(session).then(s => s.close(), () => {}).catch(() => {})
+ })
ctx.effect(() => async () => {
+ unsubscribeHolds?.()
const open = [...sessions.values()]
sessions.clear()
- await Promise.all(open.map(session => session.close()))
+ await Promise.all(open.map(session => Promise.resolve(session).then(opened => opened.close(), () => {})))
}, 'ds-bot: browser plugin')
+ // Closes a Bot's browser and removes its profile directory; used when the Bot is deleted.
+ async function drop(botId) {
+ const session = sessions.get(botId)
+ sessions.delete(botId)
+ if (session !== undefined) await Promise.resolve(session).then(opened => opened.close(), () => {})
+ await rm(join(profileRoot, botId), { recursive: true, force: true }).catch(() => {})
+ }
+
for (const spec of toolDefs) {
ctx.tools.register(defineTool({
name: spec.name,
@@@ -57,27 -46,26 +66,35 @@@
async execute(args, exec) {
const botId = await keyOf(exec)
if (!botId) return 'Only Bots on the team have a browser.'
+ if (getHold?.(botId)?.by === 'user') throw new Error('The user has taken over this browser. Wait for them to give it back.')
let session = sessions.get(botId)
if (spec.needsBrowser && session === undefined) {
- const roots = downloadRoots ? await downloadRoots(botId) : undefined
- session = await openSession({
- // Stored before the first await so concurrent calls share one browser launch.
- const opening = openBotBrowser({
-- botId,
-- profileRoot,
- downloadDir: roots === undefined
- ? join(downloadRoot ?? join(profileRoot, 'browser-downloads'), botId)
- : async () => (await downloadRoots(botId))[0],
- downloadDir: join(downloadRoot ?? join(profileRoot, 'browser-downloads'), botId),
-- headless,
-- playwrightUrl,
-- executablePath,
-- actionTimeout,
- connect: connect ? id => connect(id, roots ?? []) : undefined,
- getHold: () => getHold?.(botId),
- onHold: hold => onHold?.(botId, hold),
-- uploadRoots: uploadRoots ? () => uploadRoots(botId) : undefined,
++ // Store the opening promise before resolving roots so concurrent calls share the launch.
++ const opening = Promise.resolve().then(async () => {
++ const roots = downloadRoots ? await downloadRoots(botId) : undefined
++ return openSession({
++ botId,
++ profileRoot,
++ downloadDir: roots === undefined
++ ? join(downloadRoot ?? join(profileRoot, 'browser-downloads'), botId)
++ : async () => (await downloadRoots(botId))[0],
++ headless,
++ playwrightUrl,
++ executablePath,
++ actionTimeout,
++ connect: connect ? id => connect(id, roots ?? []) : undefined,
++ getHold: () => getHold?.(botId),
++ onHold: hold => onHold?.(botId, hold),
++ uploadRoots: uploadRoots ? () => uploadRoots(botId) : undefined,
++ })
+ })
+ sessions.set(botId, opening)
+ opening.catch(() => {
+ if (sessions.get(botId) === opening) sessions.delete(botId)
})
- sessions.set(botId, session)
+ session = opening
}
+ session = await session
const gate = authorize ? action => authorize(exec, botId, action) : undefined
return gateScope.run(gate, () => spec.execute(args ?? {}, {
botId,
diff --cc ds-bot/test/browser-plugin-output.test.js
index d585c38d,00000000..f5d0f2b0
mode 100644,000000..100644
--- a/ds-bot/test/browser-plugin-output.test.js
+++ b/ds-bot/test/browser-plugin-output.test.js
@@@ -1,192 -1,0 +1,200 @@@
+import assert from 'node:assert/strict'
+import { test } from 'node:test'
+import { Context } from '@deepseek-ai/cordis'
+import { ToolCallId } from '@deepseek-ai/dsh-llm'
+import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
+import ToolRuntime, { defineTool } from '@deepseek-ai/dsh-tools'
+import { mountBrowserPlugin } from '../src/host/browser/mount.js'
+import { toolDefs as refsTools } from '../src/host/browser/refs.js'
+
+test('browser handoff and resume return valid DSH outputs and clear the Bot hold', async t => {
+ const ctx = new Context()
+ await ctx.plugin(SystemPrompt)
+ await ctx.plugin(ToolRuntime)
+ t.after(async () => ctx.fiber.dispose())
+
+ let hold = null
- const plugin = inner => mountBrowserPlugin(inner, {
- toolDefs: refsTools.filter(tool => ['browser_handoff', 'browser_resume', 'browser_screenshot'].includes(tool.name)),
- defineTool,
- keyOf: async () => 'bot-1',
- profileRoot: '/tmp/browser-profiles',
- headless: true,
- openSession: async ({ getHold, onHold }) => ({
- handoff(reason, note) {
- const safeNote = String(note ?? '').slice(0, 300)
- onHold({ by: 'bot', reason, note: safeNote })
- return `Waiting for the user (${reason}). ${safeNote || 'Ask them to take over.'} Call browser_resume when they have finished.`
- },
- resume() {
- if (getHold()?.by === 'user') throw new Error('The user has taken over this browser. Only the user can give it back.')
- onHold(null)
- return 'The browser is yours again.'
- },
- async shot() {
- return { text: 'Screenshot of https://example.com/.', image: 'AAAA' }
- },
- async close() {},
- }),
- getHold: () => hold,
- onHold: (botId, value) => { hold = value },
- })
++ const plugin = inner => {
++ mountBrowserPlugin(inner, {
++ toolDefs: refsTools.filter(tool => ['browser_handoff', 'browser_resume', 'browser_screenshot'].includes(tool.name)),
++ defineTool,
++ keyOf: async () => 'bot-1',
++ profileRoot: '/tmp/browser-profiles',
++ headless: true,
++ openSession: async ({ getHold, onHold }) => ({
++ handoff(reason, note) {
++ const safeNote = String(note ?? '').slice(0, 300)
++ onHold({ by: 'bot', reason, note: safeNote })
++ return `Waiting for the user (${reason}). ${safeNote || 'Ask them to take over.'} Call browser_resume when they have finished.`
++ },
++ resume() {
++ if (getHold()?.by === 'user') throw new Error('The user has taken over this browser. Only the user can give it back.')
++ onHold(null)
++ return 'The browser is yours again.'
++ },
++ async shot() {
++ return { text: 'Screenshot of https://example.com/.', image: 'AAAA' }
++ },
++ async close() {},
++ }),
++ getHold: () => hold,
++ onHold: (botId, value) => { hold = value },
++ })
++ }
+ plugin.inject = ['tools']
+ await ctx.plugin(plugin)
+
+ const execute = (name, args) => ctx.tools.execute({
+ signal: new AbortController().signal,
+ callId: ToolCallId(name),
+ name,
+ arguments: args,
+ })
+
+ const handoff = await execute('browser_handoff', { reason: 'other', note: 'Please take over.' })
+ assert.equal(handoff.isError, false, handoff.error?.message)
+ assert.deepEqual(handoff.value, {
+ text: 'Waiting for the user (other). Please take over. Call browser_resume when they have finished.',
+ })
+ assert.deepEqual(handoff.content, [{
+ type: 'text',
+ text: 'Waiting for the user (other). Please take over. Call browser_resume when they have finished.',
+ }])
+ assert.deepEqual(hold, { by: 'bot', reason: 'other', note: 'Please take over.' })
+
+ const resume = await execute('browser_resume', {})
+ assert.equal(resume.isError, false, resume.error?.message)
+ assert.deepEqual(resume.value, { text: 'The browser is yours again.' })
+ assert.deepEqual(resume.content, [{ type: 'text', text: 'The browser is yours again.' }])
+ assert.equal(hold, null)
+
+ const screenshot = await execute('browser_screenshot', {})
+ assert.equal(screenshot.isError, false, screenshot.error?.message)
+ assert.deepEqual(screenshot.value, { text: 'Screenshot of https://example.com/.', image: 'AAAA' })
+ assert.deepEqual(screenshot.content, [
+ { type: 'text', text: 'Screenshot of https://example.com/.' },
+ { type: 'image', data: 'AAAA', mediaType: 'image/jpeg' },
+ ])
+})
+
+test('browser tools refuse every operation while a user hold is active', async t => {
+ const ctx = new Context()
+ await ctx.plugin(SystemPrompt)
+ await ctx.plugin(ToolRuntime)
+ t.after(async () => ctx.fiber.dispose())
+ const hold = { by: 'user', reason: 'stopped', note: 'The user is controlling this browser.' }
- const plugin = inner => mountBrowserPlugin(inner, {
- toolDefs: refsTools.filter(tool => ['browser_tabs', 'browser_screenshot', 'browser_handoff', 'browser_resume'].includes(tool.name)),
- defineTool,
- keyOf: async () => 'bot-1',
- profileRoot: '/tmp/browser-profiles',
- headless: true,
- openSession: async () => ({
- async tabs() { return [] },
- async shot() { return { text: 'shot' } },
- handoff() { throw new Error('handoff should not run') },
- resume() { throw new Error('resume should not run') },
- async close() {},
- }),
- getHold: () => hold,
- })
++ const plugin = inner => {
++ mountBrowserPlugin(inner, {
++ toolDefs: refsTools.filter(tool => ['browser_tabs', 'browser_screenshot', 'browser_handoff', 'browser_resume'].includes(tool.name)),
++ defineTool,
++ keyOf: async () => 'bot-1',
++ profileRoot: '/tmp/browser-profiles',
++ headless: true,
++ openSession: async () => ({
++ async tabs() { return [] },
++ async shot() { return { text: 'shot' } },
++ handoff() { throw new Error('handoff should not run') },
++ resume() { throw new Error('resume should not run') },
++ async close() {},
++ }),
++ getHold: () => hold,
++ })
++ }
+ plugin.inject = ['tools']
+ await ctx.plugin(plugin)
+ const execute = (name, args) => ctx.tools.execute({
+ signal: new AbortController().signal,
+ callId: ToolCallId(name),
+ name,
+ arguments: args,
+ })
+ for (const [name, args] of [
+ ['browser_tabs', { action: 'list' }],
+ ['browser_screenshot', {}],
+ ['browser_handoff', { reason: 'other' }],
+ ['browser_resume', {}],
+ ]) {
+ const result = await execute(name, args)
+ assert.equal(result.isError, true, `${name} should be refused`)
+ assert.match(result.error.message, /user has taken over/u)
+ }
+})
+
+test('browser endpoint connection uses the execution Bot identity, not tool arguments', async t => {
+ const ctx = new Context()
+ await ctx.plugin(SystemPrompt)
+ await ctx.plugin(ToolRuntime)
+ t.after(async () => ctx.fiber.dispose())
+ let connected
- const plugin = inner => mountBrowserPlugin(inner, {
- toolDefs: refsTools.filter(tool => tool.name === 'browser_open'),
- defineTool,
- keyOf: async () => 'a',
- profileRoot: '/tmp/browser-profiles',
- headless: true,
- connect: async botId => { connected = botId; return 'ws://127.0.0.1:1' },
- openSession: async options => {
- await options.connect?.(options.botId)
- return {
- async open() { return 'opened' },
- async close() {},
- }
- },
- })
++ const plugin = inner => {
++ mountBrowserPlugin(inner, {
++ toolDefs: refsTools.filter(tool => tool.name === 'browser_open'),
++ defineTool,
++ keyOf: async () => 'a',
++ profileRoot: '/tmp/browser-profiles',
++ headless: true,
++ connect: async botId => { connected = botId; return 'ws://127.0.0.1:1' },
++ openSession: async options => {
++ await options.connect?.(options.botId)
++ return {
++ async open() { return 'opened' },
++ async close() {},
++ }
++ },
++ })
++ }
+ plugin.inject = ['tools']
+ await ctx.plugin(plugin)
+ const result = await ctx.tools.execute({
+ signal: new AbortController().signal,
+ callId: ToolCallId('browser_open'),
+ name: 'browser_open',
+ arguments: { url: 'http://127.0.0.1:43118/', botId: 'b' },
+ })
+ assert.equal(result.isError, false, result.error?.message)
+ assert.equal(connected, 'a')
+})
+
+test('a channel user hold closes the cached Playwright session and give-back reconnects', async t => {
+ const ctx = new Context()
+ await ctx.plugin(SystemPrompt)
+ await ctx.plugin(ToolRuntime)
+ t.after(async () => ctx.fiber.dispose())
+ let hold = null
+ let notifyHold
+ let opens = 0
+ let closes = 0
- const plugin = inner => mountBrowserPlugin(inner, {
- toolDefs: refsTools.filter(tool => tool.name === 'browser_snapshot'),
- defineTool,
- keyOf: async () => 'bot-1',
- profileRoot: '/tmp/browser-profiles',
- headless: true,
- getHold: () => hold,
- subscribeHolds: listener => { notifyHold = listener; return () => { notifyHold = undefined } },
- openSession: async () => {
- opens += 1
- return { snapshotText: async () => 'page', async close() { closes += 1 } }
- },
- })
++ const plugin = inner => {
++ mountBrowserPlugin(inner, {
++ toolDefs: refsTools.filter(tool => tool.name === 'browser_snapshot'),
++ defineTool,
++ keyOf: async () => 'bot-1',
++ profileRoot: '/tmp/browser-profiles',
++ headless: true,
++ getHold: () => hold,
++ subscribeHolds: listener => { notifyHold = listener; return () => { notifyHold = undefined } },
++ openSession: async () => {
++ opens += 1
++ return { snapshotText: async () => 'page', async close() { closes += 1 } }
++ },
++ })
++ }
+ plugin.inject = ['tools']
+ await ctx.plugin(plugin)
+ const execute = () => ctx.tools.execute({
+ signal: new AbortController().signal,
+ callId: ToolCallId('browser_snapshot'),
+ name: 'browser_snapshot',
+ arguments: {},
+ })
+ assert.equal((await execute()).isError, false)
+ hold = { by: 'user', reason: 'takeover', note: '' }
+ notifyHold('bot-1', hold)
+ await new Promise(resolve => setImmediate(resolve))
+ assert.equal(closes, 1)
+ assert.equal((await execute()).isError, true)
+ hold = null
+ notifyHold('bot-1', null)
+ assert.equal((await execute()).isError, false)
+ assert.equal(opens, 2)
+})
diff --cc ds-bot/test/browser-plugins.test.js
index 478f05b2,8e3042a0..0361f4d1
--- a/ds-bot/test/browser-plugins.test.js
+++ b/ds-bot/test/browser-plugins.test.js
@@@ -93,64 -91,6 +93,186 @@@ function browserExec(botId = 'b2', sess
}
}
+test('app browser endpoint carries download roots for the Bot resolved from the exec', async () => {
+ const originalSend = process.send
+ const roots = ['/workspace/b2', '/workspace']
+ let request
+ const tools = new Map()
+ let cleanup
+ process.send = (message, callback) => {
+ request = message
+ callback?.()
+ queueMicrotask(() => process.emit('message', {
+ type: 'ds-bot:browser-endpoint-response',
+ id: message.id,
+ url: 'ws://127.0.0.1:1234/devtools/browser/token',
+ }))
+ return true
+ }
+ try {
+ mountBrowserPlugin({
+ effect(create) { cleanup = create() },
+ tools: { register(tool) { tools.set(tool.name, tool) } },
+ }, {
+ toolDefs: [{ name: 'browser_open', needsBrowser: true, execute: () => 'opened' }],
+ defineTool: value => value,
+ keyOf: async exec => exec.agent.id,
+ profileRoot: '/profiles',
+ downloadRoot: '/legacy-downloads',
+ downloadRoots: async botId => {
+ assert.equal(botId, 'b2')
+ return roots
+ },
+ uploadRoots: async botId => {
+ assert.equal(botId, 'b2')
+ return roots
+ },
+ headless: true,
+ connect: appBrowserEndpoint,
+ openSession: async options => {
+ assert.equal(options.botId, 'b2')
+ assert.equal(await options.downloadDir(), roots[0])
+ assert.deepEqual(await options.uploadRoots(), roots)
+ await options.connect(options.botId)
+ return { close: async () => {} }
+ },
+ })
+ const result = await tools.get('browser_open').execute({ botId: 'other' }, { agent: { id: 'b2' } })
+ assert.deepEqual(request, {
+ type: 'ds-bot:browser-endpoint',
+ id: request.id,
+ botId: 'b2',
+ downloadRoots: roots,
+ })
+ assert.equal(result, 'opened')
+ } finally {
+ await cleanup?.()
+ process.send = originalSend
+ }
+})
+
++test('concurrent first browser calls share opening and relay connection', async () => {
++ const tools = new Map()
++ let cleanup
++ let releaseOpen
++ const waitForOpen = new Promise(resolve => { releaseOpen = resolve })
++ let openCalls = 0
++ let connectCalls = 0
++ let rootsCalls = 0
++ mountBrowserPlugin({
++ effect(create) { cleanup = create() },
++ tools: { register(tool) { tools.set(tool.name, tool) } },
++ }, {
++ toolDefs: [{ name: 'browser_open', needsBrowser: true, execute: () => 'opened' }],
++ defineTool: value => value,
++ keyOf: async () => 'b2',
++ profileRoot: '/profiles',
++ downloadRoots: async () => {
++ rootsCalls += 1
++ return ['/workspace/b2', '/workspace']
++ },
++ headless: true,
++ connect: async () => {
++ connectCalls += 1
++ return 'ws://127.0.0.1:1234/devtools/browser/token'
++ },
++ openSession: async options => {
++ openCalls += 1
++ await options.connect(options.botId)
++ await waitForOpen
++ return { close: async () => {} }
++ },
++ })
++
++ const execute = () => tools.get('browser_open').execute({}, { agent: { id: 'b2' } })
++ const first = execute()
++ const second = execute()
++ await new Promise(resolve => setImmediate(resolve))
++
++ assert.equal(openCalls, 1)
++ assert.equal(connectCalls, 1)
++ assert.equal(rootsCalls, 1)
++
++ releaseOpen()
++ assert.deepEqual(await Promise.all([first, second]), ['opened', 'opened'])
++ await cleanup?.()
++})
++
++test('user takeover while opening closes the session once it resolves', async () => {
++ const tools = new Map()
++ let cleanup
++ let publishHold
++ let startedOpening
++ let releaseOpen
++ let resolveClosed
++ let closeCalls = 0
++ const openingStarted = new Promise(resolve => { startedOpening = resolve })
++ const waitForOpen = new Promise(resolve => { releaseOpen = resolve })
++ const closed = new Promise(resolve => { resolveClosed = resolve })
++ let hold = null
++ mountBrowserPlugin({
++ effect(create) { cleanup = create() },
++ tools: { register(tool) { tools.set(tool.name, tool) } },
++ }, {
++ toolDefs: [{ name: 'browser_open', needsBrowser: true, execute: () => 'opened' }],
++ defineTool: value => value,
++ keyOf: async () => 'b2',
++ profileRoot: '/profiles',
++ headless: true,
++ getHold: () => hold,
++ subscribeHolds: listener => {
++ publishHold = listener
++ return () => {}
++ },
++ openSession: () => {
++ startedOpening()
++ return waitForOpen
++ },
++ })
++
++ const opening = tools.get('browser_open').execute({}, { agent: { id: 'b2' } })
++ await openingStarted
++ hold = { by: 'user' }
++ publishHold('b2', hold)
++ releaseOpen({ close: async () => { closeCalls += 1; resolveClosed() } })
++ await opening
++ await closed
++ assert.equal(closeCalls, 1)
++ await cleanup?.()
++})
++
++test('user takeover refuses a browser call before opening or connecting', async () => {
++ const tools = new Map()
++ let openCalls = 0
++ let connectCalls = 0
++ mountBrowserPlugin({
++ effect() {},
++ tools: { register(tool) { tools.set(tool.name, tool) } },
++ }, {
++ toolDefs: [{ name: 'browser_open', needsBrowser: true, execute: () => 'opened' }],
++ defineTool: value => value,
++ keyOf: async () => 'b2',
++ profileRoot: '/profiles',
++ headless: true,
++ getHold: () => ({ by: 'user' }),
++ connect: async () => {
++ connectCalls += 1
++ return 'ws://127.0.0.1:1234/devtools/browser/token'
++ },
++ openSession: async () => {
++ openCalls += 1
++ return { close: async () => {} }
++ },
++ })
++
++ await assert.rejects(
++ tools.get('browser_open').execute({}, { agent: { id: 'b2' } }),
++ /The user has taken over this browser/,
++ )
++ assert.equal(openCalls, 0)
++ assert.equal(connectCalls, 0)
++})
++
test('no browser plugin is mounted unless one is selected for the team', (t) => {
const { ctx, stop } = start({})
t.after(stop)