A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Persist and secure app browser views
Fei24117854d43096a3
diff --git a/app/src/bot-views.mjs b/app/src/bot-views.mjsindex 565eb81b..7eeb4516 100644--- a/app/src/bot-views.mjs+++ b/app/src/bot-views.mjs@@ -1,7 +1,8 @@import { randomBytes, randomUUID } from 'node:crypto'-import { mkdirSync } from 'node:fs'+import { mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from 'node:fs'+import { rm } from 'node:fs/promises'import { createServer } from 'node:http'-import { join } from 'node:path'+import { dirname, join } from 'node:path'import { WebSocketServer } from 'ws'import { allowedNavigation, requestAllowed } from './bot-browser.mjs'@@ -12,6 +13,8 @@ const rectOf = rect => ({width: Math.max(1, Math.round(rect?.width ?? 800)),height: Math.max(1, Math.round(rect?.height ?? 600)),})+const BOT_ID = /^session-[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu+export const isBotId = value => typeof value === 'string' && BOT_ID.test(value)export class BotViews {#window@@ -27,14 +30,25 @@ export class BotViews {#address#onChange#downloadPaths = new Map()+ #statePath+ #saved = {}+ #active = new Map()+ #held = new Set()- constructor({ window, WebContentsView, guests, origin, devTools = false, onChange = () => {} }) {+ constructor({ window, WebContentsView, guests, origin, devTools = false, onChange = () => {}, statePath }) {this.#window = windowthis.#WebContentsView = WebContentsViewthis.#guests = gueststhis.#origin = originthis.#devTools = devToolsthis.#onChange = onChange+ this.#statePath = statePath+ if (statePath !== undefined) {+ try {+ const stored = JSON.parse(readFileSync(statePath, 'utf8'))+ if (stored && typeof stored === 'object' && stored.bots && typeof stored.bots === 'object') this.#saved = stored.bots+ } catch {}+ }this.#server = createServer((_request, response) => { response.writeHead(404).end() })this.#wss = new WebSocketServer({ noServer: true })this.#server.on('upgrade', (request, socket, head) => {@@ -68,7 +82,7 @@ export class BotViews {webviewTag: false, plugins: false, navigateOnDragDrop: false, disableDialogs: true, devTools: this.#devTools,},})- const entry = { tabId, botId, view, createdByBot, openerId, sessions: new Map(), sessionMaps: new Map(), downloadPath: this.#downloadPaths.get(botId), mainFrameId: undefined }+ const entry = { tabId, botId, view, createdByBot, openerId, sessions: new Map(), sessionMaps: new Map(), downloads: new Map(), downloadPath: this.#downloadPaths.get(botId), mainFrameId: undefined }this.#views.set(tabId, entry)this.#window()?.contentView.addChildView(view)view.setBounds(rectOf(null))@@ -88,11 +102,20 @@ export class BotViews {})contents.on('did-start-loading', () => this.#publish(entry))contents.on('did-stop-loading', () => this.#publish(entry))+ contents.on('did-start-navigation', (_event, _url, isInPlace, isMainFrame) => {+ if (isMainFrame && !isInPlace) {+ entry.error = null+ this.#publish(entry)+ }+ })contents.on('will-navigate', (event, target) => { if (!allowedNavigation(target, this.#origin())) event.preventDefault() })contents.on('will-redirect', (event, target) => { if (!allowedNavigation(target, this.#origin())) event.preventDefault() })- contents.setWindowOpenHandler(({ url: target }) => {- if (entry.sessions.size > 0 && allowedNavigation(target, this.#origin())) {- const popup = this.create(botId, { url: target, createdByBot: true, openerId: tabId })+ contents.on('will-frame-navigate', event => {+ if (event.isMainFrame && !allowedNavigation(event.url, this.#origin())) event.preventDefault()+ })+ contents.setWindowOpenHandler(({ url: target, postBody }) => {+ if (postBody === undefined && allowedNavigation(target, this.#origin())) {+ const popup = this.create(botId, { url: target, createdByBot: entry.sessions.size > 0, openerId: tabId })this.#publish(popup)}return { action: 'deny' }@@ -105,8 +128,14 @@ export class BotViews {this.#download(target, event, item)})this.#installDebugger(entry)+ this.#active.set(botId, tabId)+ this.#remember(entry)+ this.#persist()this.#onChange({ type: 'created', ...this.#state(entry) })- void contents.loadURL(url).catch(() => {})+ const loading = contents.loadURL(url)+ this.#remember(entry)+ this.#persist()+ void loading.catch(() => {})return entry}@@ -118,16 +147,22 @@ export class BotViews {try {mkdirSync(entry.downloadPath, { recursive: true })const guid = randomUUID()- item.setSavePath(join(entry.downloadPath, guid))+ const savePath = join(entry.downloadPath, guid)+ item.setSavePath(savePath)+ entry.downloads.set(guid, { item, savePath })this.#broadcast(entry.botId, { method: 'Browser.downloadWillBegin', params: {frameId: entry.tabId, guid, url: item.getURL(), suggestedFilename: item.getFilename(),} })const progress = state => this.#broadcast(entry.botId, { method: 'Browser.downloadProgress', params: {guid, receivedBytes: item.getReceivedBytes(), totalBytes: Math.max(0, item.getTotalBytes()),- state: state === 'completed' ? 'completed' : state === 'interrupted' ? 'canceled' : 'inProgress',+ state: state === 'completed' ? 'completed' : state === 'interrupted' || state === 'cancelled' ? 'canceled' : 'inProgress',} })item.on('updated', (_event, state) => progress(state))- item.once('done', (_event, state) => progress(state))+ item.once('done', (_event, state) => {+ progress(state)+ entry.downloads.delete(guid)+ if (state !== 'completed') void rm(savePath, { force: true }).catch(() => {})+ })} catch {event.preventDefault()}@@ -138,6 +173,11 @@ export class BotViews {if (!entry) return falseif (rect !== null) entry.view.setBounds(rectOf(rect))entry.view.setVisible(visible === true)+ if (visible === true) {+ this.#active.set(entry.botId, tabId)+ this.#remember(entry)+ this.#persist()+ }return true}@@ -168,6 +208,7 @@ export class BotViews {}}this.#views.delete(tabId)+ this.#dropSaved(botId, tabId)entry.view.webContents.close()entry.sessions.clear()entry.sessionMaps.clear()@@ -179,11 +220,19 @@ export class BotViews {for (const entry of [...this.#views.values()]) if (entry.botId === botId) await this.close(botId, entry.tabId)for (const [token, id] of this.#tokens) if (id === botId) this.#tokens.delete(token)this.#downloadPaths.delete(botId)+ this.#held.delete(botId)+ delete this.#saved[botId]+ this.#persist()}async endpoint(botId) {+ if (this.#held.has(botId)) throw new Error('The user has taken over this browser.')if (!this.#address) await new Promise(resolve => this.#server.once('listening', resolve))- if (this.sync(botId).length === 0) this.create(botId, { url: 'about:blank', createdByBot: true })+ if (this.sync(botId).length === 0) {+ const saved = this.#saved[botId]+ const restored = saved?.tabs?.find(tab => tab.tabId === saved.active) ?? saved?.tabs?.[0]+ this.create(botId, { url: restored?.url ?? 'about:blank', ...(restored?.tabId ? { tabId: restored.tabId } : {}), createdByBot: true })+ }for (const [token, id] of this.#tokens) if (id === botId) this.#tokens.delete(token)const token = randomBytes(32).toString('hex')this.#tokens.set(token, botId)@@ -195,6 +244,7 @@ export class BotViews {}dispose() {+ this.#persist()for (const entry of this.#views.values()) entry.view.webContents.close()this.#views.clear()this.#tokens.clear()@@ -202,6 +252,25 @@ export class BotViews {this.#server.close()}+ async hold(botId, held) {+ if (!held) {+ this.#held.delete(botId)+ return true+ }+ this.#held.add(botId)+ for (const [token, id] of this.#tokens) if (id === botId) this.#tokens.delete(token)+ const clients = [...(this.#clients.get(botId) ?? [])]+ for (const client of clients) {+ for (const route of client.sessions.values()) {+ if (!route.nativeId) continue+ await route.entry.view.webContents.debugger.sendCommand('Target.detachFromTarget', { sessionId: route.nativeId }).catch(() => {})+ }+ client.socket.close()+ client.socket.terminate()+ }+ return true+ }+#owned(botId, tabId) {const entry = this.#views.get(tabId)if (!entry || entry.botId !== botId) throw new Error('Browser tab does not belong to this Bot')@@ -221,11 +290,41 @@ export class BotViews {#publish(entry) {if (!entry.view.webContents.isDestroyed()) {- if (!entry.view.webContents.isLoading()) entry.error = null+ this.#remember(entry)+ this.#persist()this.#onChange({ type: 'changed', ...this.#state(entry) })}}+ #remember(entry) {+ const previous = this.#saved[entry.botId] ?? { tabs: [], active: null }+ const tabs = previous.tabs.filter(tab => tab.tabId !== entry.tabId)+ tabs.push({ tabId: entry.tabId, url: entry.view.webContents.getURL(), title: entry.view.webContents.getTitle() })+ this.#saved[entry.botId] = { tabs, active: this.#active.get(entry.botId) ?? previous.active ?? entry.tabId }+ }++ #dropSaved(botId, tabId) {+ const previous = this.#saved[botId]+ if (!previous) return+ const tabs = previous.tabs.filter(tab => tab.tabId !== tabId)+ const active = this.#active.get(botId) === tabId ? tabs.at(-1)?.tabId ?? null : previous.active+ if (tabs.length === 0) delete this.#saved[botId]+ else this.#saved[botId] = { tabs, active }+ this.#persist()+ }++ #persist() {+ if (this.#statePath === undefined) return+ const temporary = `${this.#statePath}.${process.pid}.${randomUUID()}.tmp`+ try {+ mkdirSync(dirname(this.#statePath), { recursive: true })+ writeFileSync(temporary, JSON.stringify({ version: 1, bots: this.#saved }))+ renameSync(temporary, this.#statePath)+ } catch {+ try { unlinkSync(temporary) } catch {}+ }+ }+#installDebugger(entry) {const debuggerApi = entry.view.webContents.debuggerdebuggerApi.attach('1.3')@@ -276,7 +375,7 @@ export class BotViews {#connect(socket, botId) {const sessions = new Map()- const clientState = { socket, botId, autoAttach: false, discover: false, attach: undefined }+ const clientState = { socket, botId, autoAttach: false, discover: false, attach: undefined, sessions }const clients = this.#clients.get(botId) ?? new Set()clients.add(clientState)this.#clients.set(botId, clients)@@ -313,6 +412,11 @@ export class BotViews {for (const entry of targets()) entry.downloadPath = params.downloadPathreply({}); return}+ if (method === 'Browser.cancelDownload') {+ const download = targets().flatMap(entry => [...entry.downloads.entries()]).find(([guid]) => guid === params.guid)?.[1]+ if (download) await download.item.cancel()+ reply({}); return+ }if (method === 'Target.getBrowserContexts') { reply({ browserContextIds: [`bot:${botId}`] }); return }if (method === 'Target.getTargets') { reply({ targetInfos: targets().map(info) }); return }if (method === 'Target.setDiscoverTargets') {diff --git a/app/src/main.mjs b/app/src/main.mjsindex 714723af..fc5f4bda 100644--- a/app/src/main.mjs+++ b/app/src/main.mjs@@ -11,7 +11,7 @@ import { fileURLToPath } from 'node:url'import { getDshRuntimeVersion } from '@deepseek-ai/dsh-app-boot'import electronUpdater from 'electron-updater'import { BotBrowserGuests, partitionOf } from './bot-browser.mjs'-import { BotViews } from './bot-views.mjs'+import { BotViews, isBotId } from './bot-views.mjs'import { importFromBrowser, listSources } from './browser-sources.mjs'import { applyCookies, CookieImports } from './cookie-import.mjs'import { parseCookieFile } from './cookie-parse.mjs'@@ -34,7 +34,7 @@ const IPC = {browserViewSync: 'ds-bot:browser-view-sync', browserViewPlace: 'ds-bot:browser-view-place',browserViewNavigate: 'ds-bot:browser-view-navigate', browserViewCommand: 'ds-bot:browser-view-command',browserViewFocus: 'ds-bot:browser-view-focus', browserViewRead: 'ds-bot:browser-view-read',- browserViewClose: 'ds-bot:browser-view-close',+ browserViewClose: 'ds-bot:browser-view-close', browserViewHold: 'ds-bot:browser-view-hold',browserImportSources: 'ds-bot:browser-import-sources', browserImportPick: 'ds-bot:browser-import-pick',browserImportLive: 'ds-bot:browser-import-live', browserImportApply: 'ds-bot:browser-import-apply',browserImportCancel: 'ds-bot:browser-import-cancel',@@ -74,6 +74,7 @@ const copyAbort = new AbortController()const browserGuests = new BotBrowserGuests({ session, origin: () => origin, devTools: !app.isPackaged })const botViews = new BotViews({window: () => mainWindow, WebContentsView, guests: browserGuests, origin: () => origin, devTools: !app.isPackaged,+ statePath: join(app.getPath('userData'), 'bot-views.json'),onChange: state => mainWindow?.webContents.send(IPC.browserViewChanged, state),})const cookieImports = new CookieImports()@@ -157,7 +158,7 @@ async function start() {updates?.subscribe((state) => { mainWindow?.webContents.send(IPC.appUpdateChanged, state) })host = newHost()host.onMessage(message => {- if (message?.type !== 'ds-bot:browser-endpoint' || typeof message.id !== 'string' || typeof message.botId !== 'string') return+ if (message?.type !== 'ds-bot:browser-endpoint' || typeof message.id !== 'string' || !isBotId(message.botId)) returnvoid botViews.endpoint(message.botId).then(url => {host?.send({ type: 'ds-bot:browser-endpoint-response', id: message.id, url })}).catch(() => host?.send({ type: 'ds-bot:browser-endpoint-response', id: message.id, error: 'Could not create the browser endpoint.' }))@@ -203,6 +204,7 @@ async function start() {ipcMain.handle(IPC.browserViewFocus, (event, botId, tabId) => { assertWindow(event); return botViews.operate(String(botId), 'focus', { tabId: String(tabId) }) })ipcMain.handle(IPC.browserViewRead, (event, botId, tabId, script) => { assertWindow(event); return botViews.operate(String(botId), 'read', { tabId: String(tabId), script }) })ipcMain.handle(IPC.browserViewClose, (event, botId, tabId) => { assertWindow(event); return botViews.operate(String(botId), 'close', { tabId: String(tabId) }) })+ ipcMain.handle(IPC.browserViewHold, (event, botId, held) => { assertWindow(event); return botViews.hold(String(botId), held === true) })// Cookie imports: a file or an installed browser's profile is parsed or harvested into a// pending set first; only apply (the user's confirm) writes into the Bot's partition.ipcMain.handle(IPC.browserImportSources, (event) => {diff --git a/app/src/preload.cjs b/app/src/preload.cjsindex d3d77400..7868b061 100644--- a/app/src/preload.cjs+++ b/app/src/preload.cjs@@ -60,6 +60,7 @@ function browserBridge() {focus: (botId, tabId) => ipcRenderer.invoke('ds-bot:browser-view-focus', String(botId), String(tabId)),read: (botId, tabId, script) => ipcRenderer.invoke('ds-bot:browser-view-read', String(botId), String(tabId), String(script)),close: (botId, tabId) => ipcRenderer.invoke('ds-bot:browser-view-close', String(botId), String(tabId)),+ hold: (botId, held) => ipcRenderer.invoke('ds-bot:browser-view-hold', String(botId), held === true),subscribe(listener) { viewListeners.add(listener); return () => viewListeners.delete(listener) },},// Cookie imports, only in the app: sources and a two-step pick/harvest → preview → apply.diff --git a/app/test/bot-views.test.mjs b/app/test/bot-views.test.mjsindex 1dde4a1e..79a49e8d 100644--- a/app/test/bot-views.test.mjs+++ b/app/test/bot-views.test.mjs@@ -1,13 +1,13 @@import assert from 'node:assert/strict'import { EventEmitter } from 'node:events'-import { mkdtempSync, rmSync } from 'node:fs'+import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'import { tmpdir } from 'node:os'import { join } from 'node:path'import { test } from 'node:test'import { WebSocket } from 'ws'-import { BotViews } from '../src/bot-views.mjs'+import { BotViews, isBotId } from '../src/bot-views.mjs'-function fixture() {+function fixture({ statePath } = {}) {const changes = []const handlers = new Map()const views = []@@ -50,10 +50,16 @@ function fixture() {setDownloadHandler(key, handler) { handlers.set(key, handler) },}const window = { contentView: { addChildView() {} } }- const manager = new BotViews({ window: () => window, WebContentsView, guests, origin: () => 'http://127.0.0.1:43117' , onChange: change => changes.push(change) })+ const manager = new BotViews({ window: () => window, WebContentsView, guests, origin: () => 'http://127.0.0.1:43117', statePath, onChange: change => changes.push(change) })return { manager, handlers, views, changes }}+test('Bot endpoint identifiers follow DSH session.create IDs', () => {+ assert.equal(isBotId('session-7e90ddf9-38a7-4c6c-b37a-1c445f771234'), true)+ assert.equal(isBotId('7e90ddf9-38a7-4c6c-b37a-1c445f771234'), false)+ assert.equal(isBotId('session-1'), false)+})+function openSocket(url, options) {return new Promise((resolve, reject) => {const socket = new WebSocket(url, options)@@ -184,3 +190,93 @@ test('CDP download behavior saves to the Bot path and emits download events', asassert.equal(events.some(message => message.method === 'Browser.downloadProgress' && message.params.state === 'completed'), true)assert.equal(sessionId.length > 0, true)})++test('cancelling a relay download removes its save path', async t => {+ const { manager, handlers, views } = fixture()+ t.after(() => manager.dispose())+ const downloadPath = mkdtempSync(join(tmpdir(), 'bot-views-cancel-'))+ t.after(() => rmSync(downloadPath, { recursive: true, force: true }))+ const entry = manager.create('a')+ const socket = await openSocket(await manager.endpoint('a'))+ t.after(() => socket.close())+ await request(socket, 1, 'Target.attachToTarget', { targetId: entry.tabId, flatten: true })+ await request(socket, 2, 'Browser.setDownloadBehavior', { behavior: 'allow', downloadPath })+ const events = []+ socket.on('message', raw => {+ const message = JSON.parse(raw.toString())+ if (message.method?.startsWith('Browser.download')) events.push(message)+ })+ let savePath+ const item = Object.assign(new EventEmitter(), {+ setSavePath(path) { savePath = path; writeFileSync(path, 'partial') },+ getURL() { return 'https://example.com/file' },+ getFilename() { return 'report.pdf' },+ getReceivedBytes() { return 1 },+ getTotalBytes() { return 2 },+ async cancel() { this.emit('done', {}, 'cancelled') },+ })+ handlers.get('bot:a')({ preventDefault() {} }, item, views[0].webContents)+ await new Promise(resolve => setTimeout(resolve, 0))+ const guid = events.find(message => message.method === 'Browser.downloadWillBegin').params.guid+ await request(socket, 3, 'Browser.cancelDownload', { guid })+ await new Promise(resolve => setTimeout(resolve, 0))+ assert.equal(existsSync(savePath), false)+})++test('user hold closes relay sockets, rejects endpoints, and allows a new endpoint after give-back', async t => {+ const { manager } = fixture()+ t.after(() => manager.dispose())+ const endpoint = await manager.endpoint('a')+ const socket = await openSocket(endpoint)+ const closedPromise = closed(socket)+ await manager.hold('a', true)+ await closedPromise+ await assert.rejects(() => manager.endpoint('a'), /The user has taken over this browser/u)+ await manager.hold('a', false)+ const resumed = await manager.endpoint('a')+ const resumedSocket = await openSocket(resumed)+ t.after(() => resumedSocket.close())+ assert.equal((await request(resumedSocket, 3, 'Target.getTargets')).result.targetInfos.length, 1)+})++test('navigation guard rejects the app origin and popup tabs work before relay attachment', async t => {+ const { manager, views } = fixture()+ t.after(() => manager.dispose())+ const entry = manager.create('a')+ let prevented = false+ entry.view.webContents.emit('will-frame-navigate', { url: 'http://127.0.0.1:43117/settings', isMainFrame: true, preventDefault() { prevented = true } })+ assert.equal(prevented, true)+ const popup = entry.view.webContents.windowOpenHandler({ url: 'http://127.0.0.1:43118/popup', postBody: undefined })+ assert.deepEqual(popup, { action: 'deny' })+ assert.equal(views.length, 2)+ assert.equal(manager.sync('a')[1].url, 'http://127.0.0.1:43118/popup')+})++test('saved active tabs restore their tab id and URL', async t => {+ const statePath = join(mkdtempSync(join(tmpdir(), 'bot-views-state-')), 'bot-views.json')+ t.after(() => rmSync(statePath, { force: true }))+ const first = fixture({ statePath })+ const entry = first.manager.create('a', { tabId: 'saved-tab', url: 'http://127.0.0.1:43118/form' })+ assert.equal(first.manager.create('a', { tabId: 'saved-tab' }), entry)+ first.manager.dispose()+ const second = fixture({ statePath })+ t.after(() => second.manager.dispose())+ const endpoint = await second.manager.endpoint('a')+ assert.equal(second.manager.sync('a')[0].tabId, entry.tabId)+ assert.equal(second.manager.sync('a')[0].url, 'http://127.0.0.1:43118/form')+ const socket = await openSocket(endpoint)+ t.after(() => socket.close())+})++test('load errors remain published until a real main-frame navigation starts', async t => {+ const { manager, views, changes } = fixture()+ t.after(() => manager.dispose())+ const entry = manager.create('a')+ const contents = views[0].webContents+ contents.emit('did-fail-load', {}, -105, 'offline', contents.getURL(), true)+ contents.emit('did-stop-loading')+ assert.equal(changes.at(-1).error, 'offline')+ contents.emit('did-start-navigation', {}, 'http://127.0.0.1:43118/form', false, true)+ assert.equal(changes.at(-1).error, null)+ assert.ok(entry)+})