A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Tighten computer-use guards, consent and the client around them
Fei2411bc8a56d00048
diff --git a/app/src/computer-indicator.mjs b/app/src/computer-indicator.mjsindex c9945834..25cfadc8 100644--- a/app/src/computer-indicator.mjs+++ b/app/src/computer-indicator.mjs@@ -16,6 +16,7 @@export function createComputerIndicator({ BrowserWindow, screen, html, onAction = () => {}, log = console }) {let win = nulllet entries = []+ let labels = {}const open = () => {if (win !== null) returnconst area = screen.getPrimaryDisplay()@@ -49,7 +50,7 @@ export function createComputerIndicator({ BrowserWindow, screen, html, onActionconst [, action, botId] = message.split(':')onAction(action, botId === '' || botId === undefined ? null : botId)})- const page = html(entries)+ const page = html(entries, labels)win.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(page)}`).catch((error) => {log.warn(`DS Bot: the indicator did not load: ${error instanceof Error ? error.message : String(error)}`)})@@ -63,10 +64,11 @@ export function createComputerIndicator({ BrowserWindow, screen, html, onActionreturn {/** Re-render the bar; empty entries hide the window, entries show it. */update(next) {- entries = Array.isArray(next) ? next : []+ entries = Array.isArray(next?.rows) ? next.rows : []+ labels = next?.labels ?? {}if (entries.length === 0) { close(); return }if (win === null) { open(); return }- try { win.webContents?.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(html(entries))}`) } catch (error) { log.warn(`DS Bot: the indicator did not update: ${error instanceof Error ? error.message : String(error)}`) }+ try { win.webContents?.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(html(entries, labels))}`) } catch (error) { log.warn(`DS Bot: the indicator did not update: ${error instanceof Error ? error.message : String(error)}`) }},dispose() {entries = []@@ -77,20 +79,20 @@ export function createComputerIndicator({ BrowserWindow, screen, html, onAction// The bar's own page: one row per entry, buttons that post a console message the// main process listens for. Plain inline styles keep it self-contained.-export function indicatorHtml(entries) {+export function indicatorHtml(entries, labels = {}) {const row = (entry) => `<span style="display:flex;align-items:center;gap:4px;min-width:0"><span style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap">${escapeHtml(entry.label)}</span>- <button data-act="stop:${escapeHtml(entry.botId ?? '')}" style="border:0;border-radius:50%;width:18px;height:18px;cursor:pointer;font-size:12px;line-height:1">×</button>+ <button data-act="stop:${escapeHtml(entry.botId ?? '')}" style="border:0;background:none;color:#2563eb;cursor:pointer;font:inherit;white-space:nowrap">${escapeHtml(labels.stop ?? 'Stop')}</button></span>`const waiting = (entry) => `<span style="display:flex;align-items:center;gap:6px"><span style="white-space:nowrap">${escapeHtml(entry.label)}</span>- <button data-act="letgo:${escapeHtml(entry.botId ?? '')}" style="border:0;background:none;color:#2563eb;cursor:pointer;font:inherit;white-space:nowrap">Let it continue</button>+ <button data-act="letgo:${escapeHtml(entry.botId ?? '')}" style="border:0;background:none;color:#2563eb;cursor:pointer;font:inherit;white-space:nowrap">${escapeHtml(labels.letGo ?? 'Let it continue')}</button></span>`const rows = entries.map(entry => entry.waiting ? waiting(entry) : row(entry)).join('')const stopAll = entries.filter(entry => !entry.waiting).length > 1- ? '<button data-act="stopall:" style="border:0;background:none;color:#2563eb;cursor:pointer;font:inherit;white-space:nowrap">Stop all</button>' : ''+ ? `<button data-act="stopall:" style="border:0;background:none;color:#2563eb;cursor:pointer;font:inherit;white-space:nowrap">${escapeHtml(labels.stopAll ?? 'Stop all')}</button>` : ''return `<!doctype html><html><body style="margin:0;display:flex;align-items:center;gap:8px;height:40px;padding:0 12px;border-radius:20px;background:rgba(28,28,30,.92);color:#fff;font:12.5px -apple-system,sans-serif;-webkit-app-region:no-drag;user-select:none" onclick="const a=event.target.dataset&&event.target.dataset.act;if(a)console.log('dsbot-computer:'+a)">${rows}${stopAll}</body></html>`}diff --git a/app/src/main.mjs b/app/src/main.mjsindex bfe005ee..80aa1f7a 100644--- a/app/src/main.mjs+++ b/app/src/main.mjs@@ -732,8 +732,9 @@ function handleIpc() {})ipcMain.on(IPC.computer, (event, entries) => {if (!fromWindow(event)) return- const list = Array.isArray(entries) ? entries.filter(entry => entry && typeof entry.label === 'string').slice(0, 12) : []- computerIndicator.update(list)+ const list = Array.isArray(entries?.rows) ? entries.rows.filter(entry => entry && typeof entry.label === 'string').slice(0, 12) : []+ const labels = entries?.labels ?? {}+ computerIndicator.update({ rows: list, labels })shell.setComputer(list)})ipcMain.handle(IPC.idleSeconds, (event) => (fromWindow(event) ? powerMonitor.getSystemIdleTime() : 0))diff --git a/app/src/preload.cjs b/app/src/preload.cjsindex c9ac713d..8e4dec29 100644--- a/app/src/preload.cjs+++ b/app/src/preload.cjs@@ -71,15 +71,23 @@ if (startPage !== undefined) {return place}const computerEntriesOf = (value) => {- if (!Array.isArray(value)) return []- return value.map((entry) => {- if (entry === null || typeof entry !== 'object' || typeof entry.label !== 'string') return undefined- return {- label: String(entry.label).slice(0, 80),- botId: typeof entry.botId === 'string' ? entry.botId : null,- waiting: entry.waiting === true,- }- }).filter(entry => entry !== undefined)+ const rows = Array.isArray(value?.rows) ? value.rows : []+ const labels = value?.labels ?? {}+ return {+ rows: rows.map((entry) => {+ if (entry === null || typeof entry !== 'object' || typeof entry.label !== 'string') return undefined+ return {+ label: String(entry.label).slice(0, 80),+ botId: typeof entry.botId === 'string' ? entry.botId : null,+ waiting: entry.waiting === true,+ }+ }).filter(entry => entry !== undefined),+ labels: {+ stop: typeof labels.stop === 'string' ? String(labels.stop).slice(0, 30) : undefined,+ stopAll: typeof labels.stopAll === 'string' ? String(labels.stopAll).slice(0, 30) : undefined,+ letGo: typeof labels.letGo === 'string' ? String(labels.letGo).slice(0, 30) : undefined,+ },+ }}const handoffOf = (value) => {if (value === null || typeof value !== 'object') return undefineddiff --git a/app/src/shell.mjs b/app/src/shell.mjsindex a2527618..0e948705 100644--- a/app/src/shell.mjs+++ b/app/src/shell.mjs@@ -14,6 +14,7 @@ export const SHELL_TEXT = {noticeConfirm: 'OK',fallback: 'The system tray is not available, so DS Bot cannot stay in the background. Do you want to end it?',cancel: 'Cancel',+ stopAll: 'Stop all',},zh: {tooltip: 'DS Bot',@@ -23,6 +24,7 @@ export const SHELL_TEXT = {noticeConfirm: '好',fallback: '系统托盘不可用,DS Bot 不能留在后台。要结束它吗?',cancel: '取消',+ stopAll: '全部停止',},}@@ -133,7 +135,7 @@ export function createShell(options) {image, tooltip: text().tooltip,open: { label: text().open, run: () => { open() } },quit: { label: text().quit, run: () => { quit() } },- computer: { onAction: (kind, botId) => { options.computer?.(kind, botId) } },+ computer: { onAction: (kind, botId) => { options.computer?.(kind, botId) }, stopAll: () => text().stopAll },log,})} catch (error) {diff --git a/app/src/tray.mjs b/app/src/tray.mjsindex 4872de9f..d16565fb 100644--- a/app/src/tray.mjs+++ b/app/src/tray.mjs@@ -25,7 +25,7 @@ export function createTray({ Tray, Menu, platform, image, tooltip, open, quit, cclick: () => { computer?.onAction('stop', entry.botId ?? null) },}))if (entries.length > 1) rows.push({- label: 'Stop all', click: () => { computer?.onAction('stopall', null) },+ label: computer?.stopAll?.() ?? 'Stop all', click: () => { computer?.onAction('stopall', null) },})tray.setContextMenu(Menu.buildFromTemplate([{ label: open.label, click: () => { open.run() } },diff --git a/app/test/computer-indicator.test.mjs b/app/test/computer-indicator.test.mjsindex 8721af96..6cf46018 100644--- a/app/test/computer-indicator.test.mjs+++ b/app/test/computer-indicator.test.mjs@@ -30,7 +30,7 @@ function fakeElectron() {test('the indicator window is frameless, unfocusable, protected and centered', () => {const { BrowserWindow, screen, windows } = fakeElectron()const indicator = createComputerIndicator({ BrowserWindow, screen, html: indicatorHtml, log: { warn: () => {} } })- indicator.update([{ label: 'River · Notes', botId: 'w1' }])+ indicator.update({ rows: [{ label: 'River · Notes', botId: 'w1' }], labels: {} })assert.equal(windows.length, 1)const [win] = windowsassert.equal(win.options.frame, false)@@ -50,25 +50,31 @@ test('empty entries hide the window; button clicks reach onAction', async () =>const { BrowserWindow, screen, windows } = fakeElectron()const actions = []const indicator = createComputerIndicator({ BrowserWindow, screen, html: indicatorHtml, onAction: (kind, botId) => actions.push([kind, botId]), log: { warn: () => {} } })- indicator.update([{ label: 'River · Notes', botId: 'w1' }])+ indicator.update({ rows: [{ label: 'River · Notes', botId: 'w1' }], labels: {} })const [win] = windowswin.web['console-message'](null, 3, 'dsbot-computer:stop:w1')win.web['console-message'](null, 3, 'dsbot-computer:stopall:')win.web['console-message'](null, 3, 'dsbot-computer:letgo:m1')win.web['console-message'](null, 3, 'console noise')assert.deepEqual(actions, [['stop', 'w1'], ['stopall', null], ['letgo', 'm1']])- indicator.update([])+ indicator.update({ rows: [], labels: {} })assert.equal(win.closed, true)})-test('the page lists every entry with its own stop and escapes markup', () => {- const page = indicatorHtml([{ label: 'River · <img>', botId: 'w1' }, { label: 'Chief · Mail', botId: 'm1' }, { label: 'Writer waits', botId: 'w2', waiting: true }])+test('the page lists every entry, escapes markup and uses the passed labels', () => {+ const labels = { stop: 'Stop', stopAll: 'Stop all', letGo: 'Let it continue' }+ const page = indicatorHtml([{ label: 'River · <img>', botId: 'w1' }, { label: 'Chief · Mail', botId: 'm1' }, { label: 'Writer waits', botId: 'w2', waiting: true }], labels)assert.match(page, /River · <img>/)assert.match(page, /data-act="stop:w1"/)assert.match(page, /data-act="stop:m1"/)assert.match(page, /data-act="letgo:w2"/)assert.match(page, /data-act="stopall:"/)+ assert.match(page, />Stop</)+ assert.match(page, />Stop all</)+ assert.match(page, />Let it continue</)assert.doesNotMatch(page, /<img>/)- const one = indicatorHtml([{ label: 'River · Notes', botId: 'w1' }])+ const one = indicatorHtml([{ label: 'River · Notes', botId: 'w1' }], labels)assert.doesNotMatch(one, /stopall/)+ const zh = indicatorHtml([{ label: 'River · Notes', botId: 'w1' }], { stop: '停止' })+ assert.match(zh, />停止</)})diff --git a/ds-bot/client.js b/ds-bot/client.jsindex da698538..f710846a 100644--- a/ds-bot/client.js+++ b/ds-bot/client.js@@ -1386,6 +1386,7 @@ var HOST_ZH = {"Always lets it use {app} without asking again.": "选总是允许后它用这个应用就不再问了。","This time": "这一次","Always": "总是",+ "the computer": "电脑","Needs your confirmation": "需要你确认","Help needed on the computer": "电脑上需要你帮忙","You control the computer": "你在操控电脑",@@ -5358,12 +5359,51 @@ function PendingAppAccess({ ask, actions, inline }) {"div",{ className: "bt-q-foot" },(0, import_react10.createElement)("button", { type: "button", className: "bt-q-btn", onClick: () => answer("deny") }, t("Deny")),- (0, import_react10.createElement)("button", { type: "button", className: "bt-q-btn", onClick: () => answer("always") }, t("Always")),- (0, import_react10.createElement)("button", { type: "button", className: "bt-q-submit", onClick: () => answer("once") }, t("This time"))+ (0, import_react10.createElement)("button", { type: "button", className: "bt-q-btn", onClick: () => answer("once") }, t("This time")),+ (0, import_react10.createElement)("button", { type: "button", className: "bt-q-submit", onClick: () => answer("always") }, t("Always")))));}+var ACTION_NAMES = { click: "Click", doubleClick: "Double click", rightClick: "Right click", drag: "Drag", typeText: "Type", pressKey: "Key", hotkey: "Hotkey", scroll: "Scroll", screenshot: "Screenshot", getScreenSize: "Screen size", getCapabilities: "Capabilities", listApps: "List apps", listWindows: "List windows", getState: "State", getAccessibilityTree: "Accessibility tree", launchApp: "Launch", killApp: "Kill", bringToFront: "Bring to front", invokeMenu: "Menu", setValue: "Set value", getWindowState: "Window state", verifyState: "Verify state" };+var actionName = (method) => {+ const known = ACTION_NAMES[method];+ if (known !== void 0) return known;+ const words = String(method ?? "").replace(/^cua_driver_native__/, "").replace(/([a-z])([A-Z])/g, "$1 $2").replace(/_/g, " ").trim();+ return words === "" ? "Computer" : words[0].toUpperCase() + words.slice(1);+};+function ComputerCallRow({ sessionId, call: call2, roster, actions, hooks }) {+ const method = call2?.arguments?.method ?? String(call2?.name ?? "").replace(/^cua_driver_native__/, "");+ const app = hooks?.useComputer?.((map) => (map.bots?.[roster.byId[sessionId]?.id]?.steps ?? []).find((step) => step.callId === call2?.callId)?.app);+ const shot = (call2?.content ?? []).find((block) => (block?.type === "image" || block?.type === "file") && block.attachment !== void 0)?.attachment;+ const [url, setUrl] = (0, import_react10.useState)(null);+ const [open, setOpen] = (0, import_react10.useState)(false);+ (0, import_react10.useEffect)(() => {+ const id = shot?.attachmentId;+ if (id === void 0 || !actions.computerShot) {+ setUrl(null);+ return void 0;+ }+ let current = true;+ void Promise.resolve(actions.computerShot(id)).then((value) => {+ if (current) setUrl(value?.dataUrl ?? null);+ }, () => {+ });+ return () => {+ current = false;+ };+ }, [shot?.attachmentId]);+ return (0, import_react10.createElement)(+ "div",+ { className: "bt-cstep" },+ (0, import_react10.createElement)("span", { className: "bt-cstep-label" }, `${app ?? t("the computer")} · ${actionName(method)}`),+ url !== null ? (0, import_react10.createElement)(+ "button",+ { type: "button", className: open ? "bt-cstep-shot bt-cstep-shot-open" : "bt-cstep-shot", onClick: () => setOpen((value) => !value) },+ (0, import_react10.createElement)("img", { src: url, alt: actionName(method) })+ ) : null+ );+}function AppAccessCall({ sessionId, callId, roster, actions, heldCards, hooks }) {const bot = roster.byId[sessionId];const ask = hooks.useComputer?.((map) => Object.values(map.asks ?? {}).find((item) => item.callId === callId));@@ -5496,7 +5536,7 @@ function ComputerIndicator({ useComputer, useRoster, actions }) {"span",{ key: index, className: "bt-cbar-entry" },(0, import_react10.createElement)("span", { className: "bt-cbar-name" }, entry.kind === "foreground" ? t("{name} · foreground", { name: nameOf(entry.botId) }) : `${nameOf(entry.botId)} · ${entry.appName}`),- (0, import_react10.createElement)("button", { type: "button", className: "bt-cbar-stop", "aria-label": t("Stop {name}", { name: nameOf(entry.botId) }), onClick: () => void actions.computerStop(entry.botId) }, "×")+ (0, import_react10.createElement)("button", { type: "button", className: "bt-cbar-stop", onClick: () => void actions.computerStop(entry.botId) }, t("Stop")))),waiting.map(([botId]) => (0, import_react10.createElement)("span",@@ -5528,6 +5568,11 @@ var COMPUTER_CSS = `.bt-cbar-name{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.bt-cbar-stop{flex:none;width:18px;height:18px;border:0;border-radius:50%;background:var(--bt-hover);color:var(--bt-ink-2);cursor:pointer;line-height:1;font-size:12px}.bt-cbar-stop:hover{background:var(--bt-danger);color:var(--bt-mark-ink)}+.bt-cstep{display:flex;align-items:center;gap:10px;padding:3px 0;color:var(--bt-ink-2);font-size:12.5px}+.bt-cstep-shot{flex:none;border:1px solid var(--bt-line);border-radius:8px;padding:0;background:none;cursor:zoom-in;overflow:hidden;max-height:40px}+.bt-cstep-shot img{display:block;max-height:40px;max-width:72px;object-fit:cover}+.bt-cstep-shot-open{max-height:none;cursor:zoom-out}+.bt-cstep-shot-open img{max-height:280px;max-width:480px}.bt-cbar-go,.bt-cbar-all{border:0;background:none;color:var(--bt-accent);font:inherit;cursor:pointer;white-space:nowrap}`;@@ -10214,8 +10259,9 @@ function GroupReplies({ sessionId, callId, done, content, roster, actions, useUi}));}+var isComputerCall = (name) => name === "cua" || name?.startsWith("cua_driver_native__") === true;var DRAWN = /* @__PURE__ */ new Set(["message_bot", "stop_bot", "create_bot", "update_bot", "group_relay", "create_group", "update_group", "delete_group", "post_to_group", "ask_user", "request_secret", "ask_user_question", "show_view", "browser_handoff", "read_group_chat", "remember", "forget"]);-var CARDS = /* @__PURE__ */ new Set(["ask_user", "request_secret", "show_view", "browser_handoff", "computer_handoff", "cua", "cua_driver"]);+var CARDS = /* @__PURE__ */ new Set(["ask_user", "request_secret", "show_view", "browser_handoff", "computer_handoff"]);function callTree(root) {const calls = [];const seen = /* @__PURE__ */ new Set();@@ -10319,8 +10365,13 @@ function renderCall(call2, { sessionId, roster, actions, useUi, useWork, findBotcase "forget":return done ? (0, import_react21.createElement)(MemoryEvent, { sessionId, scope: arg("scope"), tool, result: contentText(call2.content), failed: call2.isError === true, code: call2.error?.code, roster, actions, findBot }) : (0, import_react21.createElement)(CellEmpty);default:- if (tool === "cua" || tool.startsWith("cua_driver_native__")) {- return (0, import_react21.createElement)(AppAccessCall, { sessionId, callId: call2.callId, roster, actions, heldCards, hooks });+ if (isComputerCall(tool)) {+ return (0, import_react21.createElement)(+ import_react21.Fragment,+ null,+ (0, import_react21.createElement)(ComputerCallRow, { sessionId, call: call2, roster, actions, hooks }),+ (0, import_react21.createElement)(AppAccessCall, { sessionId, callId: call2.callId, roster, actions, heldCards, hooks })+ );}return (0, import_react21.createElement)(CellEmpty);}@@ -10333,7 +10384,7 @@ function ToolCell({ node, sessionId, useRoster, useUi, useWork, useHeldCards, usconst hidden = isHiddenTurn(roster, sessionId, turn);const findBot = (nameOrId) => roster.byId[nameOrId] ?? roster.bots.find((bot) => bot.name.toLowerCase() === String(nameOrId ?? "").toLowerCase());const calls = hidden ? [] : callTree(root);- const drawn = calls.filter((call2) => DRAWN.has(toolName(call2)));+ const drawn = calls.filter((call2) => DRAWN.has(toolName(call2)) || isComputerCall(toolName(call2)));const running = root.kind === "tool-result" ? void 0 : calls.findLast((call2) => call2.kind !== "tool-result");const note = running ? toolActivity(toolName(running), toolArgs(running).text, findBot, roster.byId[sessionId]) : null;useLiveNote(actions, sessionId, `tool:${root.callId ?? node.key}`, note);@@ -25249,7 +25300,10 @@ function apply(ctx) {for (const [botId, entry] of Object.entries(live2.bots ?? {})) {if (entry.waitingFor === "user-idle") rows2.push({ botId, waiting: true, label: t("{name} waits for the computer", { name: name(botId) }) });}- appComputer.computer(rows2);+ appComputer.computer({+ rows: rows2,+ labels: { stop: t("Stop"), stopAll: t("Stop all"), letGo: t("Let it continue") }+ });};sync();return computer.subscribe(sync);diff --git a/ds-bot/src/client/cells.js b/ds-bot/src/client/cells.jsindex 76478580..27f4664f 100644--- a/ds-bot/src/client/cells.js+++ b/ds-bot/src/client/cells.js@@ -12,7 +12,7 @@ import { SettledQuestion, PendingQuestion } from './questions.js'import { AnsweredReply, WidgetCell } from './widget-cell.js'import { SecretToolCell } from './secret-card.js'import { HandoffCall } from './handoff-card.js'-import { AppAccessCall, ComputerHandoffCall } from './computer.js'+import { AppAccessCall, ComputerCallRow, ComputerHandoffCall } from './computer.js'import { t } from './i18n.js'import { call } from './api.js'import { failureLabel } from './diagnostics.js'@@ -1020,9 +1020,10 @@ function GroupReplies({ sessionId, callId, done, content, roster, actions, useUi// The calls ToolCell draws. A PTC program (`run_code`) dispatches the real tools as// its sub-calls, so a call draws by its own name wherever it sits in the tree.+const isComputerCall = name => name === 'cua' || name?.startsWith('cua_driver_native__') === trueconst DRAWN = new Set(['message_bot', 'stop_bot', 'create_bot', 'update_bot', 'group_relay', 'create_group', 'update_group', 'delete_group', 'post_to_group', 'ask_user', 'request_secret', 'ask_user_question', 'show_view', 'browser_handoff', 'read_group_chat', 'remember', 'forget'])// Calls whose card a dock above the composer carries when the call is not drawn.-const CARDS = new Set(['ask_user', 'request_secret', 'show_view', 'browser_handoff', 'computer_handoff', 'cua', 'cua_driver'])+const CARDS = new Set(['ask_user', 'request_secret', 'show_view', 'browser_handoff', 'computer_handoff'])// The root and its sub-calls in dispatch order, each call once.export function callTree(root) {@@ -1130,8 +1131,10 @@ function renderCall(call, { sessionId, roster, actions, useUi, useWork, findBot,case 'forget':return done ? h(MemoryEvent, { sessionId, scope: arg('scope'), tool, result: contentText(call.content), failed: call.isError === true, code: call.error?.code, roster, actions, findBot }) : h(CellEmpty)default:- if (tool === 'cua' || tool.startsWith('cua_driver_native__')) {- return h(AppAccessCall, { sessionId, callId: call.callId, roster, actions, heldCards, hooks })+ if (isComputerCall(tool)) {+ return h(Fragment, null,+ h(ComputerCallRow, { sessionId, call, roster, actions, hooks }),+ h(AppAccessCall, { sessionId, callId: call.callId, roster, actions, heldCards, hooks }))}return h(CellEmpty)}@@ -1145,7 +1148,7 @@ export function ToolCell({ node, sessionId, useRoster, useUi, useWork, useHeldCaconst hidden = isHiddenTurn(roster, sessionId, turn)const findBot = nameOrId => roster.byId[nameOrId] ?? roster.bots.find(bot => bot.name.toLowerCase() === String(nameOrId ?? '').toLowerCase())const calls = hidden ? [] : callTree(root)- const drawn = calls.filter(call => DRAWN.has(toolName(call)))+ const drawn = calls.filter(call => DRAWN.has(toolName(call)) || isComputerCall(toolName(call)))// The newest call still running names the work, so a program reads as the tool it is in.const running = root.kind === 'tool-result' ? undefined : calls.findLast(call => call.kind !== 'tool-result')const note = running ? toolActivity(toolName(running), toolArgs(running).text, findBot, roster.byId[sessionId]) : nulldiff --git a/ds-bot/src/client/computer.js b/ds-bot/src/client/computer.jsindex 75766515..da2aeec0 100644--- a/ds-bot/src/client/computer.js+++ b/ds-bot/src/client/computer.js@@ -145,8 +145,36 @@ export function PendingAppAccess({ ask, actions, inline }) {h('div', { className: 'bt-q-detail' }, t('Always lets it use {app} without asking again.', { app: ask.appName })))),h('div', { className: 'bt-q-foot' },h('button', { type: 'button', className: 'bt-q-btn', onClick: () => answer('deny') }, t('Deny')),- h('button', { type: 'button', className: 'bt-q-btn', onClick: () => answer('always') }, t('Always')),- h('button', { type: 'button', className: 'bt-q-submit', onClick: () => answer('once') }, t('This time')))))+ h('button', { type: 'button', className: 'bt-q-btn', onClick: () => answer('once') }, t('This time')),+ h('button', { type: 'button', className: 'bt-q-submit', onClick: () => answer('always') }, t('Always')))))+}++// One line per computer call: the app and the action. A screenshot result adds a+// thumbnail that opens larger on click.+const ACTION_NAMES = { click: 'Click', doubleClick: 'Double click', rightClick: 'Right click', drag: 'Drag', typeText: 'Type', pressKey: 'Key', hotkey: 'Hotkey', scroll: 'Scroll', screenshot: 'Screenshot', getScreenSize: 'Screen size', getCapabilities: 'Capabilities', listApps: 'List apps', listWindows: 'List windows', getState: 'State', getAccessibilityTree: 'Accessibility tree', launchApp: 'Launch', killApp: 'Kill', bringToFront: 'Bring to front', invokeMenu: 'Menu', setValue: 'Set value', getWindowState: 'Window state', verifyState: 'Verify state' }+const actionName = (method) => {+ const known = ACTION_NAMES[method]+ if (known !== undefined) return known+ const words = String(method ?? '').replace(/^cua_driver_native__/, '').replace(/([a-z])([A-Z])/g, '$1 $2').replace(/_/g, ' ').trim()+ return words === '' ? 'Computer' : words[0].toUpperCase() + words.slice(1)+}+export function ComputerCallRow({ sessionId, call, roster, actions, hooks }) {+ const method = call?.arguments?.method ?? String(call?.name ?? '').replace(/^cua_driver_native__/, '')+ const app = hooks?.useComputer?.(map => (map.bots?.[roster.byId[sessionId]?.id]?.steps ?? []).find(step => step.callId === call?.callId)?.app)+ const shot = (call?.content ?? []).find(block => (block?.type === 'image' || block?.type === 'file') && block.attachment !== undefined)?.attachment+ const [url, setUrl] = useState(null)+ const [open, setOpen] = useState(false)+ useEffect(() => {+ const id = shot?.attachmentId+ if (id === undefined || !actions.computerShot) { setUrl(null); return undefined }+ let current = true+ void Promise.resolve(actions.computerShot(id)).then(value => { if (current) setUrl(value?.dataUrl ?? null) }, () => {})+ return () => { current = false }+ }, [shot?.attachmentId])+ return h('div', { className: 'bt-cstep' },+ h('span', { className: 'bt-cstep-label' }, `${app ?? t('the computer')} · ${actionName(method)}`),+ url !== null ? h('button', { type: 'button', className: open ? 'bt-cstep-shot bt-cstep-shot-open' : 'bt-cstep-shot', onClick: () => setOpen(value => !value) },+ h('img', { src: url, alt: actionName(method) })) : null)}// The call the ask waits on: drawn at it in the chat, like the handoff card.@@ -263,7 +291,7 @@ export function ComputerIndicator({ useComputer, useRoster, actions }) {h(MonitorIcon),leases.map((entry, index) => h('span', { key: index, className: 'bt-cbar-entry' },h('span', { className: 'bt-cbar-name' }, entry.kind === 'foreground' ? t('{name} · foreground', { name: nameOf(entry.botId) }) : `${nameOf(entry.botId)} · ${entry.appName}`),- h('button', { type: 'button', className: 'bt-cbar-stop', 'aria-label': t('Stop {name}', { name: nameOf(entry.botId) }), onClick: () => void actions.computerStop(entry.botId) }, '×'))),+ h('button', { type: 'button', className: 'bt-cbar-stop', onClick: () => void actions.computerStop(entry.botId) }, t('Stop')))),waiting.map(([botId]) => h('span', { key: botId, className: 'bt-cbar-entry bt-cbar-wait' },h('span', { className: 'bt-cbar-name' }, t('{name} waits for the computer', { name: nameOf(botId) })),h('button', { type: 'button', className: 'bt-cbar-go', onClick: () => void actions.computerAllowForeground(botId) }, t('Let it continue')))),@@ -291,5 +319,10 @@ export const COMPUTER_CSS = `.bt-cbar-name{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.bt-cbar-stop{flex:none;width:18px;height:18px;border:0;border-radius:50%;background:var(--bt-hover);color:var(--bt-ink-2);cursor:pointer;line-height:1;font-size:12px}.bt-cbar-stop:hover{background:var(--bt-danger);color:var(--bt-mark-ink)}+.bt-cstep{display:flex;align-items:center;gap:10px;padding:3px 0;color:var(--bt-ink-2);font-size:12.5px}+.bt-cstep-shot{flex:none;border:1px solid var(--bt-line);border-radius:8px;padding:0;background:none;cursor:zoom-in;overflow:hidden;max-height:40px}+.bt-cstep-shot img{display:block;max-height:40px;max-width:72px;object-fit:cover}+.bt-cstep-shot-open{max-height:none;cursor:zoom-out}+.bt-cstep-shot-open img{max-height:280px;max-width:480px}.bt-cbar-go,.bt-cbar-all{border:0;background:none;color:var(--bt-accent);font:inherit;cursor:pointer;white-space:nowrap}`diff --git a/ds-bot/src/client/i18n.js b/ds-bot/src/client/i18n.jsindex 07368aa2..6d61668c 100644--- a/ds-bot/src/client/i18n.js+++ b/ds-bot/src/client/i18n.js@@ -1342,6 +1342,7 @@ const HOST_ZH = {'Always lets it use {app} without asking again.': '选总是允许后它用这个应用就不再问了。','This time': '这一次','Always': '总是',+ 'the computer': '电脑','Needs your confirmation': '需要你确认','Help needed on the computer': '电脑上需要你帮忙','You control the computer': '你在操控电脑',diff --git a/ds-bot/src/client/index.js b/ds-bot/src/client/index.jsindex 0d1f65b0..7c553a71 100644--- a/ds-bot/src/client/index.js+++ b/ds-bot/src/client/index.js@@ -755,7 +755,10 @@ export function apply(ctx) {for (const [botId, entry] of Object.entries(live.bots ?? {})) {if (entry.waitingFor === 'user-idle') rows.push({ botId, waiting: true, label: t('{name} waits for the computer', { name: name(botId) }) })}- appComputer.computer(rows)+ appComputer.computer({+ rows,+ labels: { stop: t('Stop'), stopAll: t('Stop all'), letGo: t('Let it continue') },+ })}sync()return computer.subscribe(sync)diff --git a/ds-bot/src/host/computer/index.js b/ds-bot/src/host/computer/index.jsindex 5a38cd9f..e6dc4451 100644--- a/ds-bot/src/host/computer/index.js+++ b/ds-bot/src/host/computer/index.js@@ -73,6 +73,7 @@ export function install(rt) {const sessionApps = new Map()/** @type {Map<string, Map<string, 'once'|'deny'>>} `${botId}:${sessionId}` -> appKey -> this-session decision */const sessionDecisions = new Map()+ const shotMimes = new Map()/** @type {Map<string, {by:string, reason:string, note:string, callId?:string, surface:'computer'}>} */const holds = new Map()/** @type {Map<string, {botId:string, sessionId:string, appKey?:string, abort:AbortController}>} callId -> in-flight call */@@ -292,11 +293,11 @@ export function install(rt) {const pid = [args.pid, args.appPid, args.windowPid, args.target_pid].find(value => Number.isInteger(value))if (pid === undefined) return undefinedconst known = appsOfSession(sessionId).get(`pid:${pid}`)- return known ?? { key: `pid:${pid}`, name: `pid ${pid}`, unknown: true }+ return known ?? { key: `pid:${pid}`, name: `pid ${pid}`, pid, unknown: true }}// What a result tells DS Bot about apps: getApp returns the handle plus the app's// identity; driver lists name pids.- const recordAppResult = (exec, call, result) => {+ const recordAppResult = async (exec, call, result) => {const sessionId = exec.agent?.idif (sessionId === undefined || result === null || typeof result !== 'object') returnif (call.provider === 'cua' && call.method === 'getApp') {@@ -305,6 +306,12 @@ export function install(rt) {if (typeof handle === 'string' && (typeof identity.name === 'string' || typeof identity.id === 'string')) {const app = { key: appKeyOf(identity), name: String(identity.name ?? 'app'), id: identity.id, path: identity.executablePath, pid: result.pid ?? undefined }appsOfSession(sessionId).set(`handle:${handle}`, app)+ // An Always grant recorded name-only learns the id, so a later revoke can+ // drop the provider's persistent decision too.+ const owner = rt.botOf(rt.selfOf(sessionId))+ const grant = owner === undefined || identity.id === undefined ? undefined+ : Object.values(grantsOf(owner)).find(entry => entry.id === undefined && normName(entry.name) === normName(app.name))+ if (grant !== undefined) { grant.id = identity.id; await rt.save() }}return}@@ -405,6 +412,7 @@ export function install(rt) {await recordGrant(bot, { ...app, name: app.name })return allow()}+ decisionsOf(bot.id, sessionId).set(app.key, answer === 'once' ? 'once' : 'deny')return answer === 'once' ? allow() : deny()})// Windows/Linux: no consent inside the provider, so the first call that targets an@@ -447,36 +455,52 @@ export function install(rt) {return { kind: 'deny', reason: `The user is operating the computer for ${bot.name}. Wait until they hand it back.` }}const app = targetAppOf(exec, call)- if (app !== undefined && isProtectedApp({ name: app.name, id: app.id, path: app.path })) {+ // A getApp selector is checked as name, id and path, so a protected bundle+ // id is refused the same as a protected name.+ if (app !== undefined && isProtectedApp({ name: app.name, id: app.id ?? (app.tentative === true ? app.name : undefined), path: app.path ?? (app.tentative === true ? app.name : undefined) })) {return { kind: 'deny', reason: `${bot.name} cannot use ${app.name}: that application is protected.` }}- if (call.kind !== 'observe' && app !== undefined) {- if (call.provider === 'cua-driver') {- if (app.unknown === true) {- return { kind: 'deny', reason: `${bot.name} does not know which application pid ${app.pid ?? ''} is. List the windows or apps first.` }- }- const verdict = await driverConsent(exec, bot, sessionId, app)- if (verdict !== 'allow') return { kind: 'deny', reason: verdict }+ // Every refusal is decided before a lease is taken.+ if (call.provider === 'cua' && app !== undefined && app.tentative !== true) {+ // macOS: DSH's per-Session decision is not enough; DS Bot requires its own+ // Always grant or a "This time" for this Session, so a revoke bites at once.+ const decision = decisionsOf(bot.id, sessionId).get(app.key) ?? decisionsOf(bot.id, sessionId).get(normName(app.name))+ if (decision === 'deny') {+ return { kind: 'deny', reason: `The user did not allow ${bot.name} to use ${app.name}.` }+ }+ if (decision !== 'once' && grantFor(bot, app) === undefined) {+ return { kind: 'deny', reason: `Access to ${app.name} was revoked. Select it again with getApp.` }}- const held = leases.takeApp(app.key, bot.id, sessionId)- if (held !== undefined) {+ }+ if (call.provider === 'cua-driver' && app !== undefined) {+ if (app.unknown === true) {+ return { kind: 'deny', reason: `${bot.name} does not know which application pid ${app.pid ?? ''} is. List the windows or apps first.` }+ }+ const verdict = await driverConsent(exec, bot, sessionId, app)+ if (verdict !== 'allow') return { kind: 'deny', reason: verdict }+ }+ if (call.kind !== 'observe' && app !== undefined) {+ const held = leases.holderOfApp(app.key)+ if (held !== undefined && held.botId !== bot.id) {return { kind: 'deny', reason: `${rt.botOf(held.botId)?.name ?? 'Another Bot'} is using ${app.name}. Try a different app, or wait until it is done.` }}- ctl.app = app.name}if (call.kind === 'foreground') {- const held = leases.takeForeground(bot.id, sessionId)- if (held !== undefined) {+ const held = leases.holderOfForeground()+ if (held !== undefined && held.botId !== bot.id) {return { kind: 'deny', reason: `${rt.botOf(held.botId)?.name ?? 'Another Bot'} is using the foreground. Wait until it is done.` }}- if (Date.now() < ctl.allowForegroundUntil) {- // "Let it continue" bought this Bot a minute of foreground calls.- } else if (userBusy()) {+ if (Date.now() >= ctl.allowForegroundUntil && userBusy()) {ctl.state = 'waiting'ctl.waitingFor = 'user-idle'return { kind: 'deny', reason: 'The user is using the computer right now, so foreground actions wait. Try again later, or wait until they let you continue.' }}}+ if (call.kind !== 'observe' && app !== undefined) {+ leases.takeApp(app.key, bot.id, sessionId)+ ctl.app = app.name+ }+ if (call.kind === 'foreground') leases.takeForeground(bot.id, sessionId)if (ctl.state === 'idle' || ctl.state === 'waiting') {ctl.state = 'bot'ctl.waitingFor = undefined@@ -507,13 +531,16 @@ export function install(rt) {const started = Date.now()try {const result = await next()- recordAppResult(exec, call, result)+ await recordAppResult(exec, call, result)const shot = result && typeof result === 'object' ? result.attachment : undefined- if (shot !== undefined) controlOf(botId).lastShot = shot- noteStep(botId, { time: started, app: app?.name, action: call.method ?? exec.name, kind: call.kind, ok: true })+ if (shot !== undefined) {+ controlOf(botId).lastShot = shot+ if (typeof shot.mimeType === 'string') shotMimes.set(shot.attachmentId ?? shot.id, shot.mimeType)+ }+ noteStep(botId, { time: started, callId: exec.callId, app: app?.name, action: call.method ?? exec.name, kind: call.kind, ok: true })return result} catch (error) {- noteStep(botId, { time: started, app: app?.name, action: call.method ?? exec.name, kind: call.kind, ok: false, error: String(error?.message ?? error).slice(0, 200) })+ noteStep(botId, { time: started, callId: exec.callId, app: app?.name, action: call.method ?? exec.name, kind: call.kind, ok: false, error: String(error?.message ?? error).slice(0, 200) })throw error} finally {if (typeof exec.callId === 'string') inFlight.delete(exec.callId)@@ -628,7 +655,7 @@ export function install(rt) {text: (context) => {const id = context?.agent?.id ?? context?.agent?.session?.idconst bot = rt.botOf(rt.selfOf(id))- if (bot === undefined || !rt.isBotSession(id) || !enabled() || levelOf(bot) === 'off') return ''+ if (bot === undefined || !rt.isBotSession(id) || !enabled() || providerStatus.state !== 'ready' || levelOf(bot) === 'off') return ''const level = levelOf(bot)return ['## Using the computer',@@ -698,11 +725,15 @@ export function install(rt) {// The provider's own session decisions are per Session, so a revoked Bot's next// getApp asks again through the card. The provider's persistent store is only// touched when no other Bot holds an Always grant for that app id.+ // DSH's "Allow for this session" outlives the grant, so a revoke must deny+ // the provider's decision too or the Bot keeps using the app. Other Bots'+ // grants are unaffected: their next getApp is answered by DS Bot again.const access = ctx.get?.('cuaAccess')- if (access !== undefined && grant?.id !== undefined && typeof access.revoke === 'function') {- const others = Object.values(state.bots).some(other => other.id !== botId- && Object.values(grantsOf(other)).some(entry => entry.id === grant.id))- if (!others) await access.revoke(grant.id).catch(error => rt.warn('ds-bot: revoking %s in the provider failed: %s', grant.id, error))+ const wanted = normName(grant?.name ?? appKey)+ const knownId = grant?.id ?? [...sessionApps.values()].flatMap(map => [...map.values()])+ .find(app => app.id !== undefined && (normName(app.name) === wanted || app.key === appKey))?.id+ if (access !== undefined && knownId !== undefined && typeof access.revoke === 'function') {+ await access.revoke(knownId).catch(error => rt.warn('ds-bot: revoking %s in the provider failed: %s', knownId, error))}return true}@@ -752,7 +783,9 @@ export function install(rt) {case 'stop': {await rt.load()if (body.all === true) {- const ids = new Set([...leases.entries().map(entry => entry.botId), ...control.keys()])+ const ids = new Set([...leases.entries().map(entry => entry.botId),+ ...[...inFlight.values()].map(flight => flight.botId),+ ...[...control.entries()].filter(([, ctl]) => ctl.state === 'bot' || ctl.state === 'waiting').map(([id]) => id)])for (const id of ids) await stopBot(id)return true}@@ -784,7 +817,8 @@ export function install(rt) {if (attachments === undefined || id === '') return nullconst chunks = []for await (const chunk of attachments.readFileStream({ attachmentId: id }, AbortSignal.timeout(5000))) chunks.push(chunk)- return { dataUrl: `data:image/png;base64,${Buffer.concat(chunks).toString('base64')}` }+ const mime = shotMimes.get(id) ?? 'image/png'+ return { dataUrl: `data:${mime};base64,${Buffer.concat(chunks).toString('base64')}` }}case 'app-answer':return answerAppAccess(body.id, body.answer)@@ -829,6 +863,11 @@ export function install(rt) {rt.computerLevelOf = levelOfrt.computerEnabled = enabledrt.isProtectedApp = isProtectedApp+ // A member whose turn waits on the app-access card shows 'waiting for approval'+ // in its group, the same as a DSH approval does.+ const baseWaits = rt.waitsForApproval+ rt.waitsForApproval = (sessionId) => baseWaits?.(sessionId) === true+ || [...pendingAsks.values()].some(ask => ask.sessionId === sessionId)ctx.effect(() => () => {stopped = truediff --git a/ds-bot/test/computer-ui.test.js b/ds-bot/test/computer-ui.test.jsnew file mode 100644index 00000000..a6c6a712--- /dev/null+++ b/ds-bot/test/computer-ui.test.js@@ -0,0 +1,120 @@+import { test } from 'node:test'+import assert from 'node:assert/strict'+import { fileURLToPath } from 'node:url'+import { runInNewContext } from 'node:vm'+import { build } from 'esbuild'++const { outputFiles } = await build({+ stdin: {+ contents: [+ "export { ComputerPage, BotComputerCard, ComputerView, PendingAppAccess, ComputerIndicator } from './src/client/computer.js'",+ ].join('\n'),+ resolveDir: fileURLToPath(new URL('..', import.meta.url)),+ },+ bundle: true, format: 'cjs', platform: 'browser', write: false, logLevel: 'silent',+ external: ['react', '@deepseek-ai/dsh-client-ui-primitives'],+})+let current+const slot = (make) => {+ const index = current.index+++ return current.slots[index] ??= make()+}+const effect = (make, deps) => {+ const cell = slot(() => ({}))+ if (!cell.deps || deps.some((value, index) => value !== cell.deps[index])) {+ current.effects.push(() => { cell.stop?.(); cell.stop = make(); cell.deps = deps })+ }+}+const react = {+ Fragment: 'fragment',+ createContext: (initial) => { const ctx = { value: initial }; ctx.Provider = ({ children }) => children; return ctx },+ createElement: (type, props, ...children) => ({ type, props: { ...props, children: children.length === 1 ? children[0] : children } }),+ useCallback: fn => fn,+ useContext: ctx => ctx.value,+ useEffect: (make, deps) => effect(make, deps ?? []),+ useId: () => 'id',+ useLayoutEffect: (make, deps) => effect(make, deps ?? []),+ useMemo: (make) => make(),+ useRef: value => slot(() => ({ current: value })),+ useState: initial => { const cell = slot(() => ({ value: typeof initial === 'function' ? initial() : initial })); return [cell.value, next => { cell.value = next }] },+}+const run = (code, sandbox) => {+ const module = { exports: {} }+ runInNewContext(code, { module, exports: module.exports, require: (name) => name === 'react' ? react : {}, ...sandbox }, { filename: 'bundle.cjs' })+ return module.exports+}+const mod = run(outputFiles[0].text)+const render = (component, props) => {+ current = { index: 0, slots: [], effects: [] }+ const tree = mod[component](props)+ return tree+}+const texts = (node, found = []) => {+ if (typeof node === 'string' || typeof node === 'number') found.push(String(node))+ if (Array.isArray(node)) node.forEach(item => texts(item, found))+ if (node && typeof node === 'object') {+ for (const key of ['children', 'label', 'title']) if (node.props?.[key]) texts(node.props[key], found)+ }+ return found+}+const buttons = (node, found = []) => {+ if (Array.isArray(node)) node.forEach(item => buttons(item, found))+ if (node && typeof node === 'object') {+ if (node.type === 'button') found.push(texts(node).join(''))+ for (const key of ['children', 'label', 'title']) if (node.props?.[key]) buttons(node.props[key], found)+ }+ return found+}++test('the app-access card orders its buttons Deny, This time, Always', () => {+ const actions = { computerAppAnswer: async () => true }+ const ask = { id: 'a1', sessionId: 'w1', botName: 'Writer', appName: 'Notes' }+ const tree = render('PendingAppAccess', { ask, actions, inline: true })+ assert.deepEqual(buttons(tree), ['Deny', 'This time', 'Always'])+})++test('the settings page draws the switch, the provider line and one level picker per Bot', () => {+ const roster = { bots: [{ id: 'w1', name: 'Writer' }], computer: { enabled: false } }+ const hooks = { useComputer: pick => pick({ provider: { state: 'missing' } }) }+ const Switch = ({ label }) => ({ type: 'switch', props: { label } })+ const tree = render('ComputerPage', {+ roster, actions: { computerSetEnabled: async () => {} }, run: p => p, hooks,+ Section: ({ children }) => h2(children), Row: ({ children }) => h2(children), Switch,+ })+ function h2(children) { return { type: 'row', props: { children } } }+ assert.ok(texts(tree).some(text => text.includes('Computer')))+})++test('the control card shows the level picker and the Always apps with Revoke', () => {+ const bot = { id: 'w1', name: 'Writer', computer: { level: 'background', apps: { notes: { name: 'Notes', grantedAt: 0 } } } }+ const tree = render('BotComputerCard', {+ bot, roster: { computer: { enabled: true } },+ actions: { computerSetLevel: async () => {}, computerRevokeApp: async () => {} }, run: p => p,+ Card: ({ children }) => h3(children), Row: ({ children }) => h3(children),+ })+ function h3(children) { return { type: 'row', props: { children } } }+ assert.ok(texts(tree).includes('Revoke'))+ assert.ok(texts(tree).includes('Notes'))+})++test('the computer view shows the state, steps and the action buttons', () => {+ const bot = { id: 'w1', name: 'Writer' }+ const tree = render('ComputerView', {+ bot,+ actions: { computerShot: async () => ({ dataUrl: 'data:image/png;base64,x' }), computerStop: async () => {}, computerTakeover: async () => {}, computerHandback: async () => {} },+ hooks: { useComputer: pick => pick({ bots: { w1: { state: 'bot', app: 'Notes', steps: [{ time: 1, action: 'click', ok: true }] } } }) },+ })+ assert.ok(texts(tree).includes('Take over') || texts(tree).includes('Hand back'))+ assert.ok(texts(tree).includes('Stop'))+})++test('the indicator lists entries with a localized Stop and Stop all', () => {+ const live = { leases: [{ kind: 'app', appKey: 'notes', appName: 'Notes', botId: 'w1' }, { kind: 'app', appKey: 'mail', appName: 'Mail', botId: 'm1' }], bots: {} }+ const roster = { getSnapshot: () => ({ byId: { w1: { name: 'Writer' }, m1: { name: 'Chief' } } }) }+ const useComputer = pick => pick(live)+ const useRoster = pick => pick(roster.getSnapshot())+ const tree = render('ComputerIndicator', { useComputer, useRoster, actions: { computerStop: async () => {} } })+ const found = buttons(tree)+ assert.deepEqual(found.filter(name => name === 'Stop').length, 2)+ assert.ok(found.includes('Stop all'))+})diff --git a/ds-bot/test/computer.test.js b/ds-bot/test/computer.test.jsindex 58ac7937..f9d52d26 100644--- a/ds-bot/test/computer.test.js+++ b/ds-bot/test/computer.test.js@@ -155,8 +155,21 @@ test('the guard refuses in order: switch, Bot, level, control, protected, consenconst [track] = ctx.listeners.get('tools/execute').filter(fn => fn.name === 'computerTrack')await track({ name: 'cua', arguments: { method: 'getApp', params: { selector: 'Notes' } }, agent: { id: 'w1' }, callId: 'get-1' },async () => ({ appHandle: 'h1', pid: 42, authorization: { id: 'com.apple.Notes', name: 'Notes' } }))- // m1 never saw h1; it takes a lease on the raw handle, w1's is on the app id+ // w1 holds an Always grant for Notes, so its calls on the handle are allowed.+ const w1 = rt.botOf('w1')+ w1.computer = { ...w1.computer, apps: { 'com.apple.Notes': { name: 'Notes', id: 'com.apple.Notes', grantedAt: 0 } } }assert.equal(await call('w1', 'cua', { method: 'click', params: { appHandle: 'h1' } }), 'allowed')+ // m1 sees the same app through its own getApp; its call is refused and names w1.+ await track({ name: 'cua', arguments: { method: 'getApp', params: { selector: 'Notes' } }, agent: { id: 'm1' }, callId: 'get-2' },+ async () => ({ appHandle: 'h1', pid: 42, authorization: { id: 'com.apple.Notes', name: 'Notes' } }))+ const m1 = rt.botOf('m1')+ m1.computer = { ...m1.computer, apps: { 'com.apple.Notes': { name: 'Notes', id: 'com.apple.Notes', grantedAt: 0 } } }+ const blocked = await call('m1', 'cua', { method: 'click', params: { appHandle: 'h1' } })+ assert.equal(blocked.kind, 'deny')+ assert.match(blocked.reason, /Writer is using Notes/)+ // A call another Bot's lease refuses takes no lease of its own.+ const other = await call('w1', 'cua', { method: 'click', params: { appHandle: 'h1' } })+ assert.equal(other, 'allowed')})test('cua-driver consent: first call asks, This time lasts the session, Deny refuses it', async (t) => {@@ -228,3 +241,335 @@ test('macOS consent answerer: grants allow, the card answers, Always records, DSassert.deepEqual(await ask('w1', 'Mail'), { answers: [{ id: 'cua-app-access', selected: ['Allow for this session'] }] })assert.equal(rt.botOf('w1').computer.apps['mail']?.name, 'Mail')})++// A fake Cordis plugin mount + the registry the provider registers on.+function withProvider(ctx, { providerName } = {}) {+ const fibers = []+ ctx.plugin = (module, config) => {+ const entry = { module, config }+ fibers.push(entry)+ // Only the provider mount registers a provider name, not the registry's.+ if (ctx.computerUse !== undefined && String(module?.name ?? '').includes('experimental')) ctx.computerUse.providerName = providerName ?? 'fake-provider'+ return {+ await: async () => {},+ dispose: async () => { const index = fibers.indexOf(entry); if (index >= 0) fibers.splice(index, 1) },+ }+ }+ ctx.computerUse = { providerName: undefined }+ return fibers+}++test('the provider mounts when the switch comes on and reports its status', async (t) => {+ const { ctx, home, rt } = setup()+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })+ const services = {}+ ctx.get = name => services[name]+ services.loader = { import: async name => ({ name }) }+ services.agents = { list: () => [] }+ const fibers = withProvider(ctx)+ await rt.load()+ assert.deepEqual(rt.computerStatus().state, 'off')+ await rt.computerEndpoint('set-enabled', { enabled: true })+ // provider-retry joins the mount that set-enabled started+ await rt.computerEndpoint('provider-retry', {})+ assert.deepEqual(rt.computerStatus(), { state: 'ready', name: 'fake-provider' })+ // registry + provider fibers mounted, provider with the platform config+ assert.equal(fibers.length, 2)+ assert.deepEqual(fibers[1].config, process.platform === 'darwin' ? { agentPreset: false } : {})+ // switching off disposes the provider+ await rt.computerEndpoint('set-enabled', { enabled: false })+ assert.deepEqual(rt.computerStatus(), { state: 'off' })+ assert.equal(fibers.length, 0)+})++test('provider status: occupied, missing, unsupported, error and retry', async (t) => {+ const { ctx, home, rt } = setup()+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })+ const services = {}+ ctx.get = name => services[name]+ services.loader = { import: async name => { if (name.includes('missing')) throw new Error('Cannot find package') ; return { name } } }+ services.agents = { list: () => [] }+ withProvider(ctx)+ // occupied: another provider is already registered+ ctx.computerUse.providerName = 'other-provider'+ await rt.load()+ await rt.computerEndpoint('set-enabled', { enabled: true })+ await rt.computerEndpoint('provider-retry', {})+ assert.deepEqual(rt.computerStatus(), { state: 'occupied', name: 'other-provider' })+ ctx.computerUse.providerName = undefined+ await rt.computerEndpoint('provider-retry', {})+ assert.equal(rt.computerStatus().state, 'ready')+ // missing: the package cannot be imported+ services.loader = { import: async () => { throw Object.assign(new Error('Cannot find package'), { code: 'ERR_MODULE_NOT_FOUND' }) } }+ await rt.computerEndpoint('provider-retry', {})+ assert.equal(rt.computerStatus().state, 'missing')+ // error: any other import failure+ services.loader = { import: async () => { throw new Error('boom') } }+ await rt.computerEndpoint('provider-retry', {})+ assert.equal(rt.computerStatus().state, 'error')+})++test('masking: non-Bot agents and off Bots lose the tools and the sections', async (t) => {+ const { ctx, home, rt } = setup()+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })+ const denied = []+ const scoped = []+ const makeAgent = id => ({+ id, session: { id },+ ctx: {+ tools: { register: tool => { ctx.registeredTools.set(tool.name, tool); return () => {} }, restrict: filter => { denied.push([id, filter.deny]); return () => {} } },+ on: (event, listener) => { scoped.push({ id, event, listener }); return () => {} },+ effect: () => {},+ },+ })+ const services = { loader: { import: async name => ({ name }) }, agents: { list: () => [] } }+ ctx.get = name => services[name]+ withProvider(ctx)+ await rt.load()+ await rt.computerEndpoint('set-enabled', { enabled: true })+ await rt.computerEndpoint('provider-retry', {})+ const w1 = makeAgent('w1')+ const plain = makeAgent('plain')+ ctx.tools.schemas = () => [{ name: 'cua' }, { name: 'bash' }]+ services.agents = { list: () => [w1, plain] }+ await rt.computerEndpoint('set-level', { botId: 'w1', level: 'off' })+ // both masked: w1 is off, plain is not a Bot+ assert.deepEqual(denied.map(([id, deny]) => [id, deny]), [['w1', ['cua']], ['plain', ['cua']]])+ // w1 at view loses its mask but keeps the handoff tool off until background+ const registeredBefore = ctx.registeredTools.size+ await rt.computerEndpoint('set-level', { botId: 'w1', level: 'view' })+ assert.equal(ctx.registeredTools.has('computer_handoff'), true)+ assert.ok(ctx.registeredTools.size > registeredBefore)+})++test('the ds-bot section is empty for a non-Bot, off, or provider-not-ready agent', async (t) => {+ const { ctx, home, rt } = setup()+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })+ const services = { loader: { import: async name => ({ name }) }, agents: { list: () => [] } }+ ctx.get = name => services[name]+ withProvider(ctx)+ await rt.load()+ const section = ctx.sections.get('computer-use:ds-bot')+ const text = agent => section.text({ agent: { id: agent } })+ assert.equal(text('w1'), '')+ await rt.computerEndpoint('set-enabled', { enabled: true })+ await rt.computerEndpoint('provider-retry', {})+ assert.match(text('w1'), /## Using the computer/)+ assert.equal(text('plain'), '')+ await rt.computerEndpoint('set-level', { botId: 'w1', level: 'off' })+ assert.equal(text('w1'), '')+})++test('a revoke removes the grant, aborts the call, revokes in the provider and the guard refuses the handle', async (t) => {+ const { ctx, home, rt } = setup()+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })+ const revoked = []+ const services = { loader: { import: async name => ({ name }) }, agents: { list: () => [] }, cuaAccess: { revoke: async id => revoked.push(id) } }+ ctx.get = name => services[name]+ withProvider(ctx)+ await rt.load()+ await rt.computerEndpoint('set-enabled', { enabled: true })+ await rt.computerEndpoint('set-level', { botId: 'w1', level: 'background' })+ const track = ctx.listeners.get('tools/execute').find(fn => fn.name === 'computerTrack')+ const gate = ctx.listeners.get('tools/pre-execute').find(fn => fn.name === 'computerGate')+ // the Bot got a name-only Always grant, then getApp returned the id+ const bot = rt.botOf('w1')+ bot.computer = { ...bot.computer, apps: { notes: { name: 'Notes', grantedAt: 0 } } }+ await track({ name: 'cua', arguments: { method: 'getApp', params: { selector: 'Notes' } }, agent: { id: 'w1' }, callId: 'g1' },+ async () => ({ appHandle: 'h1', pid: 9, authorization: { id: 'com.apple.Notes', name: 'Notes' } }))+ // the grant learned the id+ assert.equal(rt.botOf('w1').computer.apps.notes.id, 'com.apple.Notes')+ const allow = async () => 'allowed'+ const call = args => gate({ name: 'cua', arguments: args, agent: { id: 'w1' }, callId: 'c1' }, allow)+ assert.equal(await call({ method: 'click', params: { appHandle: 'h1' } }), 'allowed')+ // revoke: grant gone, provider revoked, guard refuses the old handle+ assert.equal(await rt.computerEndpoint('revoke-app', { botId: 'w1', appKey: 'notes' }), true)+ assert.equal(rt.botOf('w1').computer.apps, undefined)+ assert.deepEqual(revoked, ['com.apple.Notes'])+ const after = await call({ method: 'click', params: { appHandle: 'h1' } })+ assert.equal(after.kind, 'deny')+ assert.match(after.reason, /revoked|Select it again/)+ // and a fresh getApp asks through the card again+ const answerer = ctx.listeners.get('user-questions/request').find(fn => fn.name === 'computerConsent')+ void answerer({ agent: { id: 'w1' }, questions: [{ id: 'cua-app-access', intent: { subject: { name: 'Notes' } }, options: [] }] }, async () => 'next')+ await new Promise(resolve => setImmediate(resolve))+ assert.equal(Object.keys(rt.computerLive().asks).length, 1)+})++test('grants hold in a later part and in a group Session of the same Bot', async (t) => {+ const { ctx, home, rt } = setup()+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })+ const services = { loader: { import: async name => ({ name }) }, agents: { list: () => [] } }+ ctx.get = name => services[name]+ withProvider(ctx)+ await rt.load()+ await rt.computerEndpoint('set-enabled', { enabled: true })+ await rt.computerEndpoint('set-level', { botId: 'w1', level: 'background' })+ const bot = rt.botOf('w1')+ bot.computer = { ...bot.computer, apps: { 'com.apple.Notes': { name: 'Notes', id: 'com.apple.Notes', grantedAt: 0 } } }+ const track = ctx.listeners.get('tools/execute').find(fn => fn.name === 'computerTrack')+ const gate = ctx.listeners.get('tools/pre-execute').find(fn => fn.name === 'computerGate')+ const allow = async () => 'allowed'+ // another Session of the same Bot (a later part, or its group Session) sees the app+ const other = 'w1-group'+ rt.isBotSession = id => id === 'w1' || id === other+ const selfOf = rt.selfOf+ rt.selfOf = id => (id === other ? 'w1' : selfOf(id))+ await track({ name: 'cua', arguments: { method: 'getApp', params: { selector: 'Notes' } }, agent: { id: other }, callId: 'g' },+ async () => ({ appHandle: 'h2', authorization: { id: 'com.apple.Notes', name: 'Notes' } }))+ assert.equal(await gate({ name: 'cua', arguments: { method: 'click', params: { appHandle: 'h2' } }, agent: { id: other }, callId: 'c' }, allow), 'allowed')+})++test('getApp refuses a protected selector by bundle id', async (t) => {+ const { ctx, home, rt } = setup()+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })+ await rt.load()+ await rt.computerEndpoint('set-enabled', { enabled: true })+ await rt.computerEndpoint('set-level', { botId: 'w1', level: 'foreground' })+ const gate = ctx.listeners.get('tools/pre-execute').find(fn => fn.name === 'computerGate')+ const denied = await gate({ name: 'cua', arguments: { method: 'getApp', params: { selector: 'com.apple.systempreferences' } }, agent: { id: 'w1' }, callId: 'c' }, async () => 'allowed')+ assert.equal(denied.kind, 'deny')+ assert.match(denied.reason, /protected/)+})++test('leases release at turn end, stop and takeover; user-busy gates foreground', async (t) => {+ const { ctx, home, rt } = setup()+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })+ await rt.load()+ await rt.computerEndpoint('set-enabled', { enabled: true })+ await rt.computerEndpoint('set-level', { botId: 'w1', level: 'foreground' })+ await rt.computerEndpoint('set-level', { botId: 'm1', level: 'foreground' })+ const gate = ctx.listeners.get('tools/pre-execute').find(fn => fn.name === 'computerGate')+ const allow = async () => 'allowed'+ const call = (sessionId, args) => gate({ name: 'cua', arguments: args, agent: { id: sessionId }, callId: `c-${sessionId}` }, allow)+ const grant = (botId) => { const bot = rt.botOf(botId); bot.computer = { ...bot.computer, apps: { notes: { name: 'Notes', grantedAt: 0 } } } }+ const track = ctx.listeners.get('tools/execute').find(fn => fn.name === 'computerTrack')+ const seed = async sessionId => track({ name: 'cua', arguments: { method: 'getApp', params: { selector: 'Notes' } }, agent: { id: sessionId }, callId: `g-${sessionId}` },+ async () => ({ appHandle: `h-${sessionId}`, authorization: { name: 'Notes' } }))+ grant('w1'); grant('m1')+ await seed('w1')+ // m1 sees a different app so only the foreground lease collides with w1's.+ await track({ name: 'cua', arguments: { method: 'getApp', params: { selector: 'Mail' } }, agent: { id: 'm1' }, callId: 'g-m1' },+ async () => ({ appHandle: 'h-m1', authorization: { name: 'Mail' } }))+ rt.botOf('m1').computer.apps.mail = { name: 'Mail', grantedAt: 0 }+ // user busy gates a foreground call+ await rt.computerEndpoint('user-active', { idleSeconds: 0 })+ const busy = await call('w1', { method: 'activate', params: { appHandle: 'h-w1' } })+ assert.equal(busy.kind, 'deny')+ assert.match(busy.reason, /user is using/)+ // the refused call took no lease: a background call still works+ await rt.computerEndpoint('user-active', { idleSeconds: 10 })+ assert.equal(await call('w1', { method: 'click', params: { appHandle: 'h-w1' } }), 'allowed')+ // let it continue buys foreground+ await rt.computerEndpoint('user-active', { idleSeconds: 0 })+ await rt.computerEndpoint('allow-foreground', { botId: 'w1' })+ assert.equal(await call('w1', { method: 'activate', params: { appHandle: 'h-w1' } }), 'allowed')+ // m1's foreground call is refused and names w1+ const other = await call('m1', { method: 'activate', params: { appHandle: 'h-m1' } })+ assert.equal(other.kind, 'deny')+ assert.match(other.reason, /Writer is using the foreground/)+ // turn end releases w1's leases+ for (const fn of ctx.listeners.get('session/event') ?? []) if (fn.length >= 2) fn({ id: 'w1' }, { type: 'turn/end' })+ await rt.computerEndpoint('user-active', { idleSeconds: 10 })+ assert.equal(await call('m1', { method: 'activate', params: { appHandle: 'h-m1' } }), 'allowed')+ // stop releases m1's+ await rt.computerEndpoint('stop', { botId: 'm1' })+ assert.equal(await call('w1', { method: 'activate', params: { appHandle: 'h-w1' } }), 'allowed')+})++test('takeover refuses the Bot and handback delivers a message with nothing the user did', async (t) => {+ const { ctx, home, rt } = setup()+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })+ await rt.load()+ await rt.computerEndpoint('set-enabled', { enabled: true })+ await rt.computerEndpoint('set-level', { botId: 'w1', level: 'foreground' })+ const gate = ctx.listeners.get('tools/pre-execute').find(fn => fn.name === 'computerGate')+ await rt.computerEndpoint('takeover', { botId: 'w1' })+ const denied = await gate({ name: 'cua', arguments: { method: 'click', params: {} }, agent: { id: 'w1' }, callId: 'c' }, async () => 'allowed')+ assert.equal(denied.kind, 'deny')+ assert.match(denied.reason, /user is operating/)+ // handback: control goes idle and the Bot gets the standard message+ const followups = []+ ctx.sessions = { flush: async () => {} }+ rt.resolveAgent = async () => ({ followup: message => followups.push(message), session: { id: 'w1' } })+ rt.chatOf = () => 'w1'+ await rt.computerEndpoint('handback', { botId: 'w1' })+ assert.equal(rt.computerLive().bots.w1.state, 'idle')+ const last = followups.at(-1)+ const text = last.content?.[0]?.text ?? JSON.stringify(last)+ assert.match(text, /handed|back/i)+ assert.doesNotMatch(text, /typed|clicked|password/i)+})++test('Stop all stops only Bots that hold a lease, are in-flight, or are bot/waiting', async (t) => {+ const { ctx, home, rt } = setup()+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })+ await rt.load()+ await rt.computerEndpoint('set-enabled', { enabled: true })+ await rt.computerEndpoint('set-level', { botId: 'w1', level: 'background' })+ const gate = ctx.listeners.get('tools/pre-execute').find(fn => fn.name === 'computerGate')+ const stopped = []+ rt.stopChat = async botId => stopped.push(botId)+ const track = ctx.listeners.get('tools/execute').find(fn => fn.name === 'computerTrack')+ const bot = rt.botOf('w1')+ bot.computer = { ...bot.computer, apps: { notes: { name: 'Notes', grantedAt: 0 } } }+ await track({ name: 'cua', arguments: { method: 'getApp', params: { selector: 'Notes' } }, agent: { id: 'w1' }, callId: 'g' },+ async () => ({ appHandle: 'h1', authorization: { name: 'Notes' } }))+ await gate({ name: 'cua', arguments: { method: 'click', params: { appHandle: 'h1' } }, agent: { id: 'w1' }, callId: 'c' }, async () => 'allowed')+ // w1 holds a lease; m1 has a control entry but nothing held (simulated by a user state)+ await rt.computerEndpoint('takeover', { botId: 'm1' })+ await rt.computerEndpoint('stop', { all: true })+ assert.deepEqual([...stopped].sort(), ['w1'])+})++test('the Windows path also asks consent for observe calls on a known app', async (t) => {+ const { ctx, home, rt } = setup()+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })+ await rt.load()+ await rt.computerEndpoint('set-enabled', { enabled: true })+ await rt.computerEndpoint('set-level', { botId: 'w1', level: 'view' })+ const gate = ctx.listeners.get('tools/pre-execute').find(fn => fn.name === 'computerGate')+ const allow = async () => 'allowed'+ const track = ctx.listeners.get('tools/execute').find(fn => fn.name === 'computerTrack')+ await track({ name: 'cua_driver_native__list_windows', arguments: {}, agent: { id: 'w1' }, callId: 'l' },+ async () => ({ windows: [{ pid: 7, name: 'Notes' }] }))+ // a screenshot of the known pid asks; list calls do not+ assert.equal(await gate({ name: 'cua_driver_native__list_windows', arguments: {}, agent: { id: 'w1' }, callId: 'c1' }, allow), 'allowed')+ const pending = gate({ name: 'cua_driver_native__get_desktop_state', arguments: { pid: 7 }, agent: { id: 'w1' }, callId: 'c2' }, allow)+ await new Promise(resolve => setImmediate(resolve))+ assert.equal(Object.keys(rt.computerLive().asks).length, 1)+ const [id] = Object.entries(rt.computerLive().asks)[0]+ await rt.computerEndpoint('app-answer', { id, answer: 'once' })+ assert.equal(await pending, 'allowed')+})++test('computer_handoff: the hold answers done, skip and takeover', async (t) => {+ const { ctx, home, rt } = setup()+ t.after(async () => { await ctx.dispose(); rmSync(home, { recursive: true, force: true, maxRetries: 5 }) })+ const services = { loader: { import: async name => ({ name }) }, agents: { list: () => [] } }+ ctx.get = name => services[name]+ withProvider(ctx)+ await rt.load()+ await rt.computerEndpoint('set-enabled', { enabled: true })+ await rt.computerEndpoint('provider-retry', {})+ await rt.computerEndpoint('set-level', { botId: 'w1', level: 'foreground' })+ // the per-agent tool is registered for an allowed Bot+ const agent = ctx.scopeAgent('w1')+ await ctx.emitAsync('agent/created', { agent })+ const handoff = ctx.registeredTools.get('computer_handoff')+ assert.notEqual(handoff, undefined)+ const followups = []+ ctx.sessions = { flush: async () => {} }+ rt.resolveAgent = async () => ({ followup: message => followups.push(message), session: { id: 'w1' } })+ rt.chatOf = () => 'w1'+ await handoff.execute({ reason: 'login', note: 'needs a sign-in' }, { agent: { id: 'w1' }, callId: 'call-9' })+ // the call shows the hold in the Bot's own chat+ const hold = Object.entries(rt.computerLive().holds ?? {})[0]+ assert.notEqual(hold, undefined)+ // done: the Bot gets the handback message+ await rt.computerEndpoint('hold-answer', { botId: 'w1', outcome: 'done' })+ const reply = JSON.parse(followups.at(-1).content[0].text)+ assert.equal(reply.kind, 'answer_to_pending_handoff')+ assert.equal(reply.outcome, 'done')+ assert.match(reply.message, /gave the computer back|handed/)+})