A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Merge browser safety safeguards from #417
Fei241164613a586a13
diff --cc browser-plugins/test/write-safety.test.jsindex e0d0907e,e4c63c43..fc746b77--- a/browser-plugins/test/write-safety.test.js+++ b/browser-plugins/test/write-safety.test.js@@@ -123,10 -136,10 +136,11 @@@ async function waitFor(check)}}- async function setup(t, actionTimeout = 500, extra = {}) {-async function setup(t, actionTimeout = 500, rootsForProfile) {++async function setup(t, actionTimeout = 500, options = {}) {const fixture = await fixtureServer()let profileRootlet session++ const extra = typeof options === 'function' ? {} : optionst.after(async () => {await session?.close().catch(() => {})try {@@@ -142,7 -155,7 +156,8 @@@downloadDir: join(profileRoot, 'downloads'),playwrightUrl,actionTimeout,- uploadRoots: rootsForProfile ? async () => rootsForProfile(profileRoot) : undefined,+ ...extra,++ uploadRoots: typeof options === 'function' ? async () => options(profileRoot) : extra.uploadRoots,})return { fixture, profileRoot, session }}@@@ -214,27 -256,6 +258,27 @@@ test('payment buttons are refused and cassert.doesNotMatch(after, /Paid/)})- test('Bot handoff can resume, but user takeover blocks browser actions until give-back', async t => {++test('Bot handoff can resume, while user takeover blocks browser actions', async t => {+ let hold = null+ const { fixture, session } = await setup(t, 500, {+ getHold: () => hold,+ onHold: value => { hold = value },+ })+ const snapshot = await session.open(fixture.url)+ assert.match(session.handoff('login', 'Sign in'), /Waiting for the user/)+ assert.equal(hold.by, 'bot')+ await assert.rejects(session.click(findRef(snapshot, 'Read link')), /browser is waiting for the user/)+ assert.equal(session.resume(), 'The browser is yours again.')+ assert.equal(hold, null)++ hold = { by: 'user', reason: 'takeover', note: '' }+ assert.throws(() => session.resume(), /Only the user can give it back/)+ await assert.rejects(session.snapshotText(), /The user has taken over this browser/)+ await assert.rejects(session.open(fixture.url), /The user has taken over this browser/)+ hold = null+ assert.match(await session.open(fixture.url), /Write safety/)+})+test('POST submit clicks are gated before the server receives them', async (t) => {const { fixture, session } = await setup(t)let allowed = truediff --cc ds-bot/client.jsindex 00eb3278,5e7ea3f6..bde7cc36--- a/ds-bot/client.js+++ b/ds-bot/client.js@@@ -14417,9 -14174,10 +14417,10 @@@ function ConnectorsPage({ roster, actio}// src/client/browser-settings.js-var import_react33 = require("react");+var import_react34 = require("react");var ACTIONS = [["post", "Post"], ["upload", "Upload"], ["download", "Download"], ["delete", "Delete"]];var DURATIONS = [["hour", "1 hour"], ["today", "Today"], ["week", "7 days"], ["permanent", "Permanent"]];+ var expired = (grant) => Number.isFinite(grant.until) && grant.until <= Date.now();function siteValue(value) {return value.trim().toLowerCase().replace(/^[a-z][a-z\d+.-]*:\/\//, "").split(/[/?#]/, 1)[0].replace(/:\d+$/, "") || "*";}@@@ -14474,37 -14232,37 +14475,37 @@@ function BrowserPage({ roster, actionsconst toggleKind = (kind, checked) => {setKinds((current) => checked ? current.includes(kind) ? current : [...current, kind] : current.filter((value) => value !== kind));};- return (0, import_react33.createElement)(+ return (0, import_react34.createElement)("div",{ className: "bt-browser-settings" },- (0, import_react33.createElement)(+ (0, import_react34.createElement)(Section,{ title: t("Browser access") },- (0, import_react33.createElement)("p", { className: "bt-browser-note" }, t("Bots browse, click and fill in forms freely. Posting, uploading, downloading and deleting ask you first unless you allow them here. Payments are always yours: the Bot hands the page to you."))+ (0, import_react34.createElement)("p", { className: "bt-browser-note" }, t("Bots browse, click and fill in forms freely. Posting, uploading, downloading and deleting ask you first unless you allow them here. Payments are always yours: the Bot hands the page to you."))),- (0, import_react33.createElement)(+ (0, import_react34.createElement)(Section,{ title: t("Allowed actions") },- grants.length === 0 ? (0, import_react34.createElement)(Row3, { label: t("Nothing allowed yet.") }) : grants.map(({ bot, grant, index, expired }) => (0, import_react34.createElement)(- grants.length === 0 ? (0, import_react33.createElement)(Row3, { label: t("Nothing allowed yet.") }) : grants.map(({ bot, grant, index, expired: expired2 }) => (0, import_react33.createElement)(++ grants.length === 0 ? (0, import_react34.createElement)(Row3, { label: t("Nothing allowed yet.") }) : grants.map(({ bot, grant, index, expired: expired2 }) => (0, import_react34.createElement)("div",{key: `${bot.id}:${index}`,- className: `bt-browser-grant${expired ? " bt-browser-grant-expired" : ""}`+ className: `bt-browser-grant${expired2 ? " bt-browser-grant-expired" : ""}`},- (0, import_react33.createElement)(+ (0, import_react34.createElement)("div",{ className: "bt-browser-grant-who" },- (0, import_react33.createElement)(BotAvatar, { bot, size: 28 }),- (0, import_react33.createElement)("span", null, bot.name)+ (0, import_react34.createElement)(BotAvatar, { bot, size: 28 }),+ (0, import_react34.createElement)("span", null, bot.name)),- (0, import_react33.createElement)(+ (0, import_react34.createElement)("div",{ className: "bt-browser-grant-meta" },- (0, import_react33.createElement)("span", { className: "bt-browser-grant-site" }, grant.site === "*" ? t("All sites") : grant.site),- (0, import_react33.createElement)("div", { className: "bt-browser-grant-kinds" }, grantKinds(grant).map((label) => (0, import_react33.createElement)("span", { key: label, className: "bt-chip" }, label))),- (0, import_react33.createElement)("span", { className: "bt-set-hint" }, grantTime(grant))+ (0, import_react34.createElement)("span", { className: "bt-browser-grant-site" }, grant.site === "*" ? t("All sites") : grant.site),+ (0, import_react34.createElement)("div", { className: "bt-browser-grant-kinds" }, grantKinds(grant).map((label) => (0, import_react34.createElement)("span", { key: label, className: "bt-chip" }, label))),+ (0, import_react34.createElement)("span", { className: "bt-set-hint" }, grantTime(grant))),- (0, import_react33.createElement)("button", {+ (0, import_react34.createElement)("button", {type: "button",className: "bt-soft bt-soft-sm",onClick: () => remove(bot, index),@@@ -14512,25 -14270,25 +14513,25 @@@}, t("Remove grant"))))),- (0, import_react33.createElement)(+ (0, import_react34.createElement)(Section,{ title: t("Allow an action") },- (0, import_react33.createElement)(+ (0, import_react34.createElement)("form",{ className: "bt-browser-form", onSubmit: add },- (0, import_react33.createElement)(+ (0, import_react34.createElement)(Row3,- { className: "bt-browser-form-row", label: t("Bot") },+ { label: t("Bot") },- (0, import_react33.createElement)(+ (0, import_react34.createElement)("select",{ className: "bt-select", value: botId, onChange: (event) => setBotId(event.target.value), disabled: bots.length === 0 },- bots.map((bot) => (0, import_react33.createElement)("option", { key: bot.id, value: bot.id }, bot.name))+ bots.map((bot) => (0, import_react34.createElement)("option", { key: bot.id, value: bot.id }, bot.name)))),- (0, import_react33.createElement)(+ (0, import_react34.createElement)(Row3,- { className: "bt-browser-form-row", label: t("Site") },+ { label: t("Site") },- (0, import_react33.createElement)("input", {+ (0, import_react34.createElement)("input", {className: "bt-select bt-browser-site",type: "text",value: site,@@@ -14538,16 -14296,16 +14539,16 @@@onChange: (event) => setSite(event.target.value)})),- (0, import_react33.createElement)(+ (0, import_react34.createElement)(Row3,- { className: "bt-browser-form-row", label: t("Actions") },+ { label: t("Actions") },- (0, import_react33.createElement)(+ (0, import_react34.createElement)("div",{ className: "bt-browser-action-list" },- ACTIONS.map(([kind, label]) => (0, import_react33.createElement)(+ ACTIONS.map(([kind, label]) => (0, import_react34.createElement)("label",{ key: kind, className: "bt-browser-action-choice" },- (0, import_react33.createElement)("input", {+ (0, import_react34.createElement)("input", {type: "checkbox",checked: kinds.includes(kind),onChange: (event) => toggleKind(kind, event.target.checked)@@@ -14556,13 -14314,13 +14557,13 @@@)))),- (0, import_react33.createElement)(+ (0, import_react34.createElement)(Row3,- { className: "bt-browser-form-row", label: t("Duration") },+ { label: t("Duration") },- (0, import_react33.createElement)(+ (0, import_react34.createElement)("div",{ className: "bt-browser-duration", role: "group", "aria-label": t("Duration") },- DURATIONS.map(([key, label]) => (0, import_react33.createElement)("button", {+ DURATIONS.map(([key, label]) => (0, import_react34.createElement)("button", {key,type: "button",className: "bt-browser-duration-choice",diff --cc ds-bot/src/host/browser/mount.jsindex a9c76939,fb3888dd..36e96452--- a/ds-bot/src/host/browser/mount.js+++ b/ds-bot/src/host/browser/mount.js@@@ -30,9 -18,9 +30,9 @@@ export const browserOutput =/*** @param {any} ctx- * @param {{ toolDefs: any[], defineTool: Function, keyOf: (exec: any) => Promise<string|undefined>, profileRoot: string, downloadRoot?: string, headless: boolean, playwrightUrl?: string, executablePath?: string, actionTimeout?: number, authorize?: (exec: any, botId: string, action: any) => Promise<void>, complete?: Function, 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 }} 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, openSession?: typeof openBotBrowser }} options*/- export function mountBrowserPlugin(ctx, { toolDefs, defineTool, keyOf, profileRoot, downloadRoot, 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 }) {++export function mountBrowserPlugin(ctx, { toolDefs, defineTool, keyOf, profileRoot, downloadRoot, uploadRoots, headless, playwrightUrl, executablePath, actionTimeout, authorize, complete, connect, getHold, onHold, openSession = openBotBrowser }) {const sessions = new Map()ctx.effect(() => async () => {const open = [...sessions.values()]@@@ -59,9 -47,7 +59,10 @@@playwrightUrl,executablePath,actionTimeout,+ connect,+ getHold: () => getHold?.(botId),+ onHold: hold => onHold?.(botId, hold),+ uploadRoots: uploadRoots ? () => uploadRoots(botId) : undefined,})sessions.set(botId, session)}diff --cc ds-bot/src/host/browser/session.jsindex 01658f87,21f4db9b..3283ec1d--- a/ds-bot/src/host/browser/session.js+++ b/ds-bot/src/host/browser/session.js@@@ -85,37 -86,29 +86,37 @@@ async function loadChromium(playwrightU}/**- * @param {{ botId: string, profileRoot: string, headless?: boolean, playwrightUrl?: string, executablePath?: string, actionTimeout?: number }} options- * @param {{ botId: string, profileRoot: string, headless?: boolean, playwrightUrl?: string, executablePath?: string, actionTimeout?: number, uploadRoots?: () => Promise<string[]> }} options++ * @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*/- export async function openBotBrowser({ botId, profileRoot, downloadDir, headless = true, playwrightUrl, executablePath, actionTimeout = ACTION_TIMEOUT, connect, getHold, onHold }) {-export async function openBotBrowser({ botId, profileRoot, downloadDir, headless = true, playwrightUrl, executablePath, actionTimeout = ACTION_TIMEOUT, uploadRoots }) {++export async function openBotBrowser({ botId, profileRoot, downloadDir, headless = true, playwrightUrl, executablePath, actionTimeout = ACTION_TIMEOUT, connect, getHold, onHold, uploadRoots }) {const userDataDir = join(profileRoot, botId)- await mkdir(userDataDir, { recursive: true })const chromium = await loadChromium(playwrightUrl)- const context = await chromium.launchPersistentContext(userDataDir, {- headless,- viewport: { width: 1280, height: 900 },- acceptDownloads: true,- ...(executablePath ? { executablePath } : {}),- args: ['--disable-dev-shm-usage'],- })+ const browser = connect+ ? await chromium.connectOverCDP(await connect(botId))+ : undefined+ if (!connect) await mkdir(userDataDir, { recursive: true })+ const context = browser+ ? browser.contexts()[0]+ : await chromium.launchPersistentContext(userDataDir, {+ headless,+ viewport: { width: 1280, height: 900 },+ acceptDownloads: true,+ ...(executablePath ? { executablePath } : {}),+ args: ['--disable-dev-shm-usage'],+ })+ if (!context) throw new Error('The app browser relay has no browser context.')const page = context.pages()[0] ?? await context.newPage()const cookieFile = join(userDataDir, 'cookies.json')- try {- const saved = JSON.parse(await readFile(cookieFile, 'utf8'))- if (Array.isArray(saved) && saved.length > 0) await context.addCookies(saved)- } catch { /* no cookies yet */ }- return createSession({ context, page, botId, userDataDir, cookieFile, downloadDir, actionTimeout, uploadRoots })+ if (!browser) {+ try {+ const saved = JSON.parse(await readFile(cookieFile, 'utf8'))+ 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 })++ return createSession({ context, browser, page, botId, userDataDir, cookieFile, downloadDir, actionTimeout, getHold, onHold, uploadRoots })}- function createSession({ context, browser, page, botId, userDataDir, cookieFile, downloadDir, actionTimeout, getHold, onHold }) {-function createSession({ context, page, botId, userDataDir, cookieFile, downloadDir, actionTimeout, uploadRoots }) {++function createSession({ context, browser, page, botId, userDataDir, cookieFile, downloadDir, actionTimeout, getHold, onHold, uploadRoots }) {const pages = new Set()const unknownByOrigin = new Map()const unknownPageOrigins = new WeakMap()@@@ -126,11 -119,9 +127,12 @@@let lastImage = ''let activeDownloads = nulllet pendingDelete = null+ let pendingDeleteLocator = nullconst ready = () => {+ const externalHold = getHold?.()+ if (externalHold?.by === 'user') throw new Error('The user has taken over this browser. Wait for them to give it back.')+ if (hold && getHold && externalHold == null) hold = nullif (hold) throw new Error(`The browser is waiting for the user (${hold.reason}). Ask them to ${hold.note || 'take over'}, then call browser_resume.`)}