Skip to content

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

Enforce Bot browser takeover holds

Fei2411705bfa0347ee
diff --git a/ds-bot/client.js b/ds-bot/client.js
index bde7cc36..2e8b02c1 100644
--- a/ds-bot/client.js
+++ b/ds-bot/client.js
@@ -17960,7 +17960,7 @@ function createBotBrowsers({ bridge, createSource: createSource2, storage = glob
return;
}
if (!owner.has(tabId)) owner.set(tabId, botId);
- if (change.type === "created" && change.createdByBot) {
+ if (change.type === "created") {
ensureTabs(botId);
tabs.set((snapshot) => {
const own = snapshot[botId] ?? { ids: [], active: null };
@@ -19165,9 +19165,21 @@ function apply(ctx) {
browserNavigate: (tabId, input) => browsers.navigate(tabId, input),
browserCommand: (tabId, name) => browsers.command(tabId, name),
browserFocus: (tabId) => browsers.focus(tabId),
- browserTakeover: bridge.views ? (botId) => call("browser-takeover", { botId }) : void 0,
- browserStop: bridge.views ? (botId) => call("browser-stop", { botId }) : void 0,
- browserGiveBack: bridge.views ? (botId) => call("browser-give-back", { botId }) : void 0,
+ browserTakeover: bridge.views ? async (botId) => {
+ const result = await call("browser-takeover", { botId });
+ await bridge.views.hold(botId, true);
+ return result;
+ } : void 0,
+ browserStop: bridge.views ? async (botId) => {
+ const result = await call("browser-stop", { botId });
+ await bridge.views.hold(botId, true);
+ return result;
+ } : void 0,
+ browserGiveBack: bridge.views ? async (botId) => {
+ const result = await call("browser-give-back", { botId });
+ await bridge.views.hold(botId, false);
+ return result;
+ } : void 0,
// Cookie imports, only where the bridge offers them (the desktop app): a file or an
// installed browser's profile is previewed, then applied into the Bot's storage key.
browserImport: typeof bridge.importSources === "function" ? {
diff --git a/ds-bot/src/client/bot-browsers.js b/ds-bot/src/client/bot-browsers.js
index 1528d883..97ce574e 100644
--- a/ds-bot/src/client/bot-browsers.js
+++ b/ds-bot/src/client/bot-browsers.js
@@ -143,7 +143,7 @@ export function createBotBrowsers({ bridge, createSource, storage = globalThis.l
return
}
if (!owner.has(tabId)) owner.set(tabId, botId)
- if (change.type === 'created' && change.createdByBot) {
+ if (change.type === 'created') {
ensureTabs(botId)
tabs.set(snapshot => {
const own = snapshot[botId] ?? { ids: [], active: null }
diff --git a/ds-bot/src/client/index.js b/ds-bot/src/client/index.js
index bc9c817a..6b8b5589 100644
--- a/ds-bot/src/client/index.js
+++ b/ds-bot/src/client/index.js
@@ -542,9 +542,21 @@ export function apply(ctx) {
browserNavigate: (tabId, input) => browsers.navigate(tabId, input),
browserCommand: (tabId, name) => browsers.command(tabId, name),
browserFocus: (tabId) => browsers.focus(tabId),
- browserTakeover: bridge.views ? botId => call('browser-takeover', { botId }) : undefined,
- browserStop: bridge.views ? botId => call('browser-stop', { botId }) : undefined,
- browserGiveBack: bridge.views ? botId => call('browser-give-back', { botId }) : undefined,
+ browserTakeover: bridge.views ? async botId => {
+ const result = await call('browser-takeover', { botId })
+ await bridge.views.hold(botId, true)
+ return result
+ } : undefined,
+ browserStop: bridge.views ? async botId => {
+ const result = await call('browser-stop', { botId })
+ await bridge.views.hold(botId, true)
+ return result
+ } : undefined,
+ browserGiveBack: bridge.views ? async botId => {
+ const result = await call('browser-give-back', { botId })
+ await bridge.views.hold(botId, false)
+ return result
+ } : undefined,
// Cookie imports, only where the bridge offers them (the desktop app): a file or an
// installed browser's profile is previewed, then applied into the Bot's storage key.
browserImport: typeof bridge.importSources === 'function' ? {
diff --git a/ds-bot/src/host/browser-plugins.js b/ds-bot/src/host/browser-plugins.js
index 87d91e44..9de923ae 100644
--- a/ds-bot/src/host/browser-plugins.js
+++ b/ds-bot/src/host/browser-plugins.js
@@ -129,6 +129,7 @@ export function install(rt) {
connect: process.env.DS_BOT_APP === '1' && typeof process.send === 'function' ? appBrowserEndpoint : undefined,
getHold: botId => rt.browserChannel?.holdOf(botId) ?? null,
onHold: (botId, hold) => rt.browserChannel?.setHold(botId, hold),
+ subscribeHolds: listener => rt.browserChannel?.subscribeHolds?.(listener),
authorize: (exec, botId, action) => authorizeBrowserAction(rt, exec, botId, action),
complete: rt.config.browserComplete ?? completeDeepseek,
async keyOf(exec) {
diff --git a/ds-bot/src/host/browser.js b/ds-bot/src/host/browser.js
index c2910093..5b9cc613 100644
--- a/ds-bot/src/host/browser.js
+++ b/ds-bot/src/host/browser.js
@@ -27,6 +27,7 @@ export function createBrowserChannel({ now = Date.now, waitMs = WAIT_MS, staleMs
/** @type {Map<string, {client:any, resolve:(value:any) => void, timer:any}>} */
const pending = new Map()
const holds = new Map()
+ const holdListeners = new Set()
let nextId = 0
let holdVersion = 0
@@ -134,6 +135,9 @@ export function createBrowserChannel({ now = Date.now, waitMs = WAIT_MS, staleMs
else holds.set(botId, { by: hold.by, reason: hold.reason, note: hold.note })
holdVersion += 1
for (const client of clients.values()) client.wake?.()
+ for (const listener of holdListeners) {
+ try { listener(botId, holds.get(botId) ?? null) } catch {}
+ }
return holds.get(botId) ?? null
}
@@ -147,7 +151,11 @@ export function createBrowserChannel({ now = Date.now, waitMs = WAIT_MS, staleMs
holds.clear()
}
- return { wait, result, read, openPages, setHold, holdOf, holdSnapshot, dispose }
+ const subscribeHolds = listener => {
+ holdListeners.add(listener)
+ return () => holdListeners.delete(listener)
+ }
+ return { wait, result, read, openPages, setHold, holdOf, holdSnapshot, subscribeHolds, dispose }
}
const clean = (value, max) => String(value ?? '').replace(/\s+/g, ' ').trim().slice(0, max)
diff --git a/ds-bot/src/host/browser/mount.js b/ds-bot/src/host/browser/mount.js
index 36e96452..926bb781 100644
--- a/ds-bot/src/host/browser/mount.js
+++ b/ds-bot/src/host/browser/mount.js
@@ -30,11 +30,19 @@ 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>, complete?: Function, connect?: (botId: string) => Promise<string>, getHold?: (botId: string) => any, onHold?: (botId: string, hold: any) => void, openSession?: typeof openBotBrowser }} options
+ * @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>, complete?: Function, connect?: (botId: 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, complete, connect, getHold, onHold, openSession = openBotBrowser }) {
+export function mountBrowserPlugin(ctx, { toolDefs, defineTool, keyOf, profileRoot, downloadRoot, uploadRoots, headless, playwrightUrl, executablePath, actionTimeout, authorize, complete, 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()
+ })
ctx.effect(() => async () => {
+ unsubscribeHolds?.()
const open = [...sessions.values()]
sessions.clear()
await Promise.all(open.map(session => session.close()))
@@ -49,6 +57,7 @@ export function mountBrowserPlugin(ctx, { toolDefs, defineTool, keyOf, profileRo
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) {
session = await openSession({
diff --git a/ds-bot/src/host/browser/session.js b/ds-bot/src/host/browser/session.js
index 3283ec1d..ae6248d0 100644
--- a/ds-bot/src/host/browser/session.js
+++ b/ds-bot/src/host/browser/session.js
@@ -199,6 +199,7 @@ function createSession({ context, browser, page, botId, userDataDir, cookieFile,
}
async function tabs() {
+ ready()
await pendingPageReady
const active = activePage()
return Promise.all(openPages().map(async (target, index) => ({
@@ -325,6 +326,7 @@ function createSession({ context, browser, page, botId, userDataDir, cookieFile,
await authorizeWrite(action, gate)
} catch (error) {
await download.cancel().catch(() => {})
+ await download.delete().catch(() => {})
const message = String(error?.message ?? error).replace(/\s+/g, ' ').slice(0, 160)
return message || `Download "${filename}" was refused. Nothing was saved.`
}
@@ -336,6 +338,7 @@ function createSession({ context, browser, page, botId, userDataDir, cookieFile,
return `Downloaded "${filename}" to ${path}.`
} catch (error) {
await download.cancel().catch(() => {})
+ await download.delete().catch(() => {})
const message = String(error?.message ?? error).replace(/\s+/g, ' ').slice(0, 160)
return `Download "${filename}" failed: ${message || 'unknown error'}.`
}
@@ -491,6 +494,7 @@ function createSession({ context, browser, page, botId, userDataDir, cookieFile,
}
async function shot() {
+ ready()
const target = activePage()
if (!target) throw new Error('The browser has no open tabs.')
const image = await target.screenshot({ type: 'jpeg', quality: 32 })
@@ -670,6 +674,7 @@ function createSession({ context, browser, page, botId, userDataDir, cookieFile,
},
shot,
handoff(reason, note) {
+ ready()
const kind = REASONS.has(reason) ? reason : 'other'
hold = { reason: kind, note: String(note ?? '').slice(0, 300) }
onHold?.({ by: 'bot', reason: kind, note: hold.note })
diff --git a/ds-bot/test/bot-browsers.test.js b/ds-bot/test/bot-browsers.test.js
index 8186f284..6c60de4b 100644
--- a/ds-bot/test/bot-browsers.test.js
+++ b/ds-bot/test/bot-browsers.test.js
@@ -209,6 +209,33 @@ test('a page that asks for a new window opens a new tab in the same Bot', async
assert.deepEqual(guest.loads, ['https://pop.example.com/'])
})
+test('an app popup is adopted and activated before a Bot has attached', async () => {
+ const calls = []
+ let receive
+ const doc = { body: { append() {} }, createElement: () => ({ style: {}, isConnected: true, append() {}, remove() {} }) }
+ const bridge = {
+ views: {
+ subscribe(listener) { receive = listener; return () => {} },
+ async sync() { return [] },
+ async create(botId, tabId) { calls.push({ type: 'create', botId, tabId }); return tabId },
+ async place(tabId, rect, visible) { calls.push({ type: 'place', tabId, rect, visible }) },
+ async navigate() {},
+ async command() {},
+ async focus() {},
+ async read() {},
+ async close() {},
+ },
+ }
+ const browsers = createBotBrowsers({ bridge, createSource, storage: fakeDesktop().storage, document: doc })
+ browsers.ensureTabs('a')
+ receive({ type: 'created', botId: 'a', tabId: 'user-popup', url: 'http://127.0.0.1:43118/popup', createdByBot: false, loading: false })
+ await tick()
+ assert.deepEqual(browsers.tabs.getSnapshot().a, { ids: ['user-popup'], active: 'user-popup' })
+ assert.ok(calls.some(call => call.type === 'create' && call.tabId === 'user-popup'))
+ await browsers.show('user-popup', BOX)
+ assert.ok(calls.some(call => call.type === 'place' && call.tabId === 'user-popup' && call.visible))
+})
+
test('past six live guests the least recently shown one is released; forget drops all of a Bot’s tabs', async () => {
const desktop = fakeDesktop()
const browsers = createBotBrowsers({ bridge: desktop.bridge, createSource, storage: desktop.storage, document: desktop.doc })
diff --git a/ds-bot/test/browser-plugin-output.test.js b/ds-bot/test/browser-plugin-output.test.js
index 9831bb17..d585c38d 100644
--- a/ds-bot/test/browser-plugin-output.test.js
+++ b/ds-bot/test/browser-plugin-output.test.js
@@ -74,3 +74,119 @@ test('browser handoff and resume return valid DSH outputs and clear the Bot hold
{ 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,
+ })
+ 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() {},
+ }
+ },
+ })
+ 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 } }
+ },
+ })
+ 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)
+})