arena-bridge 0.7.19 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,39 @@ All notable changes to this project are documented here.
4
4
  The format follows [Keep a Changelog](https://keepachangelog.com/) and the project uses
5
5
  [Semantic Versioning](https://semver.org/).
6
6
 
7
+ ## 0.8.0
8
+
9
+ ### Added
10
+ - `chrome_observe`: compact page observation for action — visible interactive elements with
11
+ refs and states (pressed/selected/checked/expanded), open dialogs, and the current text
12
+ editor with its content.
13
+ - `chrome_fill_editor`: finds the visible editor (textarea, contenteditable or textbox), types
14
+ with real input events and reads the value back — no CSS selector needed.
15
+ - `chrome_wait_for` plus an `expect` option on `chrome_click`, `chrome_type` and
16
+ `chrome_fill_editor`: actions can wait for a page condition and report `verified`.
17
+ - `chrome_click` accepts an element `ref` (with `snapshot_id`) from `chrome_observe`;
18
+ stale refs are rejected with `stale_ref` instead of clicking something else.
19
+ - `arena-bridge browser login <name>`: a plain human Chrome launch for signing in
20
+ (the old `browser open` remains as a deprecated alias).
21
+
22
+ ### Changed
23
+ - The login launch no longer starts Chrome with `--remote-debugging-port=0` or other
24
+ automation flags — Google and similar sites refused sign-in with
25
+ "This browser or app may not be secure". Debugging is only enabled later, by the
26
+ agent's `chrome_launch`, once the human has signed in.
27
+ - `chrome_launch` keeps its flags as close to a plain Chrome launch as possible
28
+ (no `--disable-sync`, `--disable-background-networking` or feature kill-switches),
29
+ allocates a free CDP port when none is given, and refuses a second owner of a
30
+ profile with `profile_busy` instead of corrupting it.
31
+ - `chrome_profiles` reports whether each profile is running, on which port and in
32
+ which session; leftover agent-owned headless Chromes are cleaned up on startup.
33
+
34
+ ## 0.7.20
35
+
36
+ ### Fixed
37
+ - Direct HTTP routes and both execution engines now use one fresh configuration snapshot per request.
38
+ - Changes to roots, protected paths, approval policy and response limits apply consistently without restarting the bridge.
39
+
7
40
  ## 0.7.19
8
41
 
9
42
  ### Fixed
package/README.md CHANGED
@@ -411,7 +411,7 @@ arena-bridge trust --off
411
411
  «тестовый». В каждом свои входы, и они переживают перезапуск моста.
412
412
 
413
413
  ```bash
414
- arena-bridge browser open work # открыть Chrome с профилем «work»
414
+ arena-bridge browser login work # открыть Chrome с профилем «work»
415
415
  # вы логинитесь в нужные сайты и закрываете окно — входы сохраняются
416
416
  arena-bridge browser list # какие профили есть
417
417
  arena-bridge browser close work # закрыть окно этого профиля
@@ -420,6 +420,12 @@ arena-bridge browser close work # закрыть окно этого пр
420
420
  Агент открывает тот же профиль через `chrome_launch {"profile":"work"}` — уже с вашими входами.
421
421
  Каталог профилей агент тоже видит: `chrome_profiles`.
422
422
 
423
+ `browser login` специально запускает обычный Chrome без отладочного порта и флагов
424
+ автоматизации: Google и другие сайты отказывают во входе браузеру, который выглядит
425
+ управляемым извне. Отладочный доступ появляется только потом, в агентском
426
+ `chrome_launch`, когда вход уже выполнен. Старое имя `browser open` оставлено как
427
+ алиас той же команды.
428
+
423
429
  Ваш собственный Chrome (на порту 9222, с вашими сессиями) доступен отдельно — через
424
430
  `chrome_attach`; любые действия в нём требуют подтверждения.
425
431
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arena-bridge",
3
- "version": "0.7.19",
3
+ "version": "0.8.0",
4
4
  "description": "Give a cloud AI agent (Arena.ai Agent Mode, Claude, Cursor, ...) controlled access to your own machine over HTTP/MCP: files, shell, Chrome and the macOS desktop, with an approval gate, trust windows and an audit log.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/approvals.mjs CHANGED
@@ -57,16 +57,17 @@ export function decide(id, decision, by = 'cli') {
57
57
  }
58
58
 
59
59
  /**
60
- * Settle a request and, when asked, open a trust window for the same tool.
60
+ * Settle a request and, when asked, open a trust window.
61
+ * Blanket window (all tools, all agents) per user request: if trust is granted,
62
+ * every tool for every agent is trusted until `until`.
61
63
  */
62
64
  export function settle(id, decision, by = 'cli', { trustSec = 0 } = {}) {
63
65
  const res = decide(id, decision, by);
64
66
  if (res.ok && trustSec > 0) {
65
- const tool = res.approval?.tool;
66
67
  saveTrust({
67
68
  enabled: true,
68
69
  until: Date.now() + trustSec * 1000,
69
- tools: tool ? [tool] : [],
70
+ tools: [], // blanket: all tools, any agent
70
71
  paths: [],
71
72
  createdAt: Date.now(),
72
73
  });
package/src/browser.mjs CHANGED
@@ -1,7 +1,8 @@
1
1
  import fs from 'node:fs';
2
2
  import os from 'node:os';
3
3
  import path from 'node:path';
4
- import { spawn } from 'node:child_process';
4
+ import { spawn, execFile } from 'node:child_process';
5
+ import net from 'node:net';
5
6
  import { CDPClient, browserWsUrl } from './cdp.mjs';
6
7
  import { checkPath } from './policy.mjs';
7
8
  import { audit } from './audit.mjs';
@@ -33,6 +34,58 @@ const KEYS = {
33
34
 
34
35
  const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
35
36
 
37
+ /** Pids of processes whose command line uses this Chrome user-data-dir. */
38
+ export function profilePids(profile) {
39
+ return new Promise((resolve) => {
40
+ execFile('ps', ['-Ao', 'pid=,command='], { timeout: 10000 }, (err, stdout) => {
41
+ if (err) return resolve([]);
42
+ const needle = `--user-data-dir=${profile}`;
43
+ const out = [];
44
+ for (const line of String(stdout).split('\n')) {
45
+ if (!line.includes(needle)) continue;
46
+ const pid = Number(line.trim().split(/\s+/)[0]);
47
+ if (pid && pid !== process.pid) out.push(pid);
48
+ }
49
+ resolve(out);
50
+ });
51
+ });
52
+ }
53
+
54
+ /** A free TCP port on localhost, for a CDP endpoint nobody else owns. */
55
+ export function freePort() {
56
+ return new Promise((resolve, reject) => {
57
+ const srv = net.createServer();
58
+ srv.once('error', reject);
59
+ srv.listen(0, '127.0.0.1', () => {
60
+ const { port } = srv.address();
61
+ srv.close(() => resolve(port));
62
+ });
63
+ });
64
+ }
65
+
66
+ /**
67
+ * Kill leftover agent-owned headless Chromes (managed profiles only).
68
+ * A visible browser the human opened to log in is never touched.
69
+ */
70
+ export async function cleanupOrphanChromes(sessions = []) {
71
+ const keep = new Set(sessions.map((s) => s.proc?.pid).filter(Boolean));
72
+ const managedRoots = [profileDir() + path.sep, path.join(os.homedir(), '.arena-bridge', 'chrome-profile-')];
73
+ return new Promise((resolve) => {
74
+ execFile('ps', ['-Ao', 'pid=,command='], { timeout: 10000 }, (err, stdout) => {
75
+ if (err) return resolve(0);
76
+ let killed = 0;
77
+ for (const line of String(stdout).split('\n')) {
78
+ if (!line.includes('--user-data-dir=') || !line.includes('--headless')) continue;
79
+ if (!managedRoots.some((r) => line.includes(r))) continue;
80
+ const pid = Number(line.trim().split(/\s+/)[0]);
81
+ if (!pid || pid === process.pid || keep.has(pid)) continue;
82
+ try { process.kill(pid, 'SIGTERM'); killed++; } catch { /* already gone */ }
83
+ }
84
+ resolve(killed);
85
+ });
86
+ });
87
+ }
88
+
36
89
  export function findChrome() {
37
90
  if (process.env.CHROME_PATH && fs.existsSync(process.env.CHROME_PATH)) return process.env.CHROME_PATH;
38
91
  for (const c of CHROME_CANDIDATES) if (fs.existsSync(c)) return c;
@@ -160,6 +213,7 @@ export class BrowserManager {
160
213
  profile,
161
214
  proc,
162
215
  tabs: new Map(),
216
+ observe: new Map(),
163
217
  createdAt: Date.now(),
164
218
  };
165
219
  this.sessions.set(id, session);
@@ -199,19 +253,37 @@ export class BrowserManager {
199
253
  return { ok: false, error: 'Chrome not found', reason: 'set CHROME_PATH or install Google Chrome' };
200
254
  }
201
255
  const headless = args.headless !== false;
202
- const port = Number(args.port || 9223);
256
+ const port = Number(args.port) || (await freePort());
203
257
  const profile = resolveProfile(args.profile, port);
204
258
 
259
+ // One profile = one Chrome. Refuse a second owner instead of corrupting
260
+ // the profile or failing later with a locked user-data-dir.
261
+ for (const s of this.sessions.values()) {
262
+ if (s.profile === profile) {
263
+ return { ok: false, error: 'profile_busy', profile, session_id: s.id,
264
+ hint: 'This profile is already running in a browser session — reuse it or chrome_stop it first.' };
265
+ }
266
+ if (s.port === port) {
267
+ return { ok: false, error: 'port_busy', port, session_id: s.id,
268
+ hint: 'That CDP port already belongs to another browser session — omit "port" to get a free one.' };
269
+ }
270
+ }
271
+ const external = await profilePids(profile).catch(() => []);
272
+ if (external.length) {
273
+ return { ok: false, error: 'profile_busy', profile, pids: external,
274
+ hint: 'A Chrome with this profile is already running (for example a "browser login" window). Close it, then retry.' };
275
+ }
276
+
205
277
  fs.mkdirSync(profile, { recursive: true });
206
278
 
279
+ // Kept as close to a plain Chrome launch as possible (only the CDP port is
280
+ // added): sync/background/feature kill-switches made sites behave oddly
281
+ // and can invalidate sessions the user created in the plain login browser.
207
282
  const chromeArgs = [
208
283
  `--remote-debugging-port=${port}`,
209
284
  `--user-data-dir=${profile}`,
210
285
  '--no-first-run',
211
286
  '--no-default-browser-check',
212
- '--disable-background-networking',
213
- '--disable-sync',
214
- '--disable-features=Translate,MediaRouter',
215
287
  '--remote-allow-origins=*',
216
288
  ];
217
289
  if (headless) chromeArgs.push(process.env.ARENA_HEADLESS_FLAG || '--headless=new');
@@ -345,6 +417,139 @@ export class BrowserManager {
345
417
  return res.result?.value;
346
418
  }
347
419
 
420
+ /* ---------------- observe / semantic actions ---------------- */
421
+
422
+ async _observe(session, tab, args = {}) {
423
+ const limit = Math.max(1, Math.min(Number(args.limit || args.max_elements || 80), 300));
424
+ const textLimit = Math.max(0, Math.min(Number(args.text_limit || 6000), 50000));
425
+ const id = `snap_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
426
+ const expr = `(() => { /*arena-observe*/
427
+ const LIMIT = ${limit}, TEXT_LIMIT = ${textLimit};
428
+ document.querySelectorAll('[data-arena-bridge-ref]').forEach((e) => e.removeAttribute('data-arena-bridge-ref'));
429
+ const vis = (e) => { const r = e.getBoundingClientRect(); const s = getComputedStyle(e);
430
+ return r.width > 0 && r.height > 0 && s.display !== 'none' && s.visibility !== 'hidden'; };
431
+ const name = (e) => (e.getAttribute('aria-label') || e.innerText || e.value ||
432
+ e.getAttribute('placeholder') || e.getAttribute('title') || '').trim().replace(/\s+/g, ' ').slice(0, 160);
433
+ const role = (e) => e.getAttribute('role') || ({A:'link',BUTTON:'button',TEXTAREA:'textbox',
434
+ SELECT:'combobox',INPUT:(e.type === 'checkbox' ? 'checkbox' : e.type === 'radio' ? 'radio' : 'textbox')}[e.tagName] || e.tagName.toLowerCase());
435
+ const state = (e) => { const st = {};
436
+ const put = (k, v) => { if (v !== null && v !== undefined) st[k] = (v === 'true' ? true : v === 'false' ? false : v); };
437
+ put('pressed', e.getAttribute('aria-pressed')); put('selected', e.getAttribute('aria-selected'));
438
+ put('checked', e.getAttribute('aria-checked') !== null ? e.getAttribute('aria-checked') : (typeof e.checked === 'boolean' ? String(e.checked) : null));
439
+ put('expanded', e.getAttribute('aria-expanded')); put('disabled', e.disabled || e.getAttribute('aria-disabled'));
440
+ st.focused = (document.activeElement === e); return st; };
441
+ const SEL = 'a,button,input,textarea,select,summary,[contenteditable],[role="button"],[role="textbox"],[role="checkbox"],[role="radio"],[role="tab"],[role="menuitem"],[role="option"],[role="switch"],[role="combobox"],[role="slider"],[role="link"]';
442
+ const els = [...document.querySelectorAll(SEL)].filter(vis);
443
+ const elements = els.slice(0, LIMIT).map((e, i) => { const ref = 'el_' + i;
444
+ e.setAttribute('data-arena-bridge-ref', ref);
445
+ return { ref, role: role(e), name: name(e), tag: e.tagName.toLowerCase(), state: state(e) }; });
446
+ const dialogs = [...document.querySelectorAll('dialog[open],[role="dialog"],[role="alertdialog"]')].filter(vis)
447
+ .map((d) => ({ text: (d.innerText || '').trim().replace(/\s+/g, ' ').slice(0, 500),
448
+ buttons: [...d.querySelectorAll('button,a,[role="button"],input[type="button"],input[type="submit"]')]
449
+ .map((b) => name(b)).filter(Boolean).slice(0, 12) }));
450
+ const editorSel = 'textarea,[contenteditable="true"],[contenteditable=""],[role="textbox"],input[type="text"],input[type="search"],input[type="email"],input[type="url"],input:not([type])';
451
+ const editors = [...document.querySelectorAll(editorSel)].filter(vis);
452
+ const ed = document.activeElement && editors.includes(document.activeElement) ? document.activeElement : editors[0];
453
+ const composer = ed ? { kind: ed.getAttribute('contenteditable') !== null ? 'contenteditable' : ed.tagName.toLowerCase(),
454
+ text: (ed.value !== undefined ? ed.value : (ed.innerText || '')).slice(0, 500), focused: document.activeElement === ed } : null;
455
+ return { title: document.title, url: location.href,
456
+ text: document.body ? (document.body.innerText || '').slice(0, TEXT_LIMIT) : '',
457
+ elements, dialogs, composer, element_total: els.length, truncated: els.length > elements.length };
458
+ })()`;
459
+ const value = (await this._eval(session, tab, expr)) || {};
460
+ const refs = new Set((value.elements || []).map((e) => e.ref));
461
+ session.observe.set(tab.targetId, { id, refs, url: value.url || tab.url });
462
+ return { ok: true, session_id: session.id, tab_id: tab.targetId, snapshot_id: id, ...value };
463
+ }
464
+
465
+ async _clickRef(session, tab, args) {
466
+ const snap = session.observe.get(tab.targetId);
467
+ if (!snap || !snap.refs.has(String(args.ref)) || (args.snapshot_id && args.snapshot_id !== snap.id)) {
468
+ return { ok: false, error: 'stale_ref', ref: args.ref,
469
+ reason: 'That element ref is not in the latest observation of this tab — call chrome_observe again.' };
470
+ }
471
+ const expr = `(() => { /*arena-click-ref*/
472
+ const el = document.querySelector('[data-arena-bridge-ref="' + ${JSON.stringify(String(args.ref))} + '"]');
473
+ if (!el) return { clicked: false, reason: 'element no longer in the page' };
474
+ el.scrollIntoView({ block: 'center' }); el.click();
475
+ return { clicked: true, tag: el.tagName, text: (el.innerText || el.value || '').slice(0, 120) }; })()`;
476
+ const res = await this._eval(session, tab, expr);
477
+ if (!res || !res.clicked) {
478
+ return { ok: false, error: 'stale_ref', ref: args.ref, reason: res?.reason || 'click failed' };
479
+ }
480
+ return { ok: true, tab_id: tab.targetId, ref: args.ref, ...res };
481
+ }
482
+
483
+ async _fillEditor(session, tab, args) {
484
+ const focusExpr = `(() => { /*arena-fill-focus*/
485
+ const vis = (e) => { const r = e.getBoundingClientRect(); const s = getComputedStyle(e);
486
+ return r.width > 0 && r.height > 0 && s.display !== 'none' && s.visibility !== 'hidden' && !e.disabled; };
487
+ const sel = 'textarea,[contenteditable="true"],[contenteditable=""],[role="textbox"],input[type="text"],input[type="search"],input[type="email"],input[type="url"],input[type="tel"],input:not([type])';
488
+ const eds = [...document.querySelectorAll(sel)].filter(vis);
489
+ const ed = (document.activeElement && eds.includes(document.activeElement)) ? document.activeElement : eds[0];
490
+ if (!ed) return { ok: false, reason: 'no visible editor found' };
491
+ ed.focus();
492
+ const ce = ed.getAttribute('contenteditable') !== null && ed.tagName !== 'TEXTAREA' && ed.tagName !== 'INPUT';
493
+ if (${args.clear === false ? 'false' : 'true'}) {
494
+ if (ce) { document.execCommand('selectAll', false, null); document.execCommand('delete', false, null); }
495
+ else { const proto = ed instanceof HTMLTextAreaElement ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype;
496
+ const setter = Object.getOwnPropertyDescriptor(proto, 'value').set; setter.call(ed, '');
497
+ ed.dispatchEvent(new Event('input', { bubbles: true })); }
498
+ } else if (ce) { const range = document.createRange(); range.selectNodeContents(ed); range.collapse(false);
499
+ const sel2 = getSelection(); sel2.removeAllRanges(); sel2.addRange(range); }
500
+ return { ok: true, kind: ce ? 'contenteditable' : ed.tagName.toLowerCase() }; })()`;
501
+ const focused = await this._eval(session, tab, focusExpr);
502
+ if (!focused || !focused.ok) return { ok: false, error: focused?.reason || 'no visible editor found', tab_id: tab.targetId };
503
+ if (String(args.text ?? '')) {
504
+ await session.cdp.send('Input.insertText', { text: String(args.text) }, tab.sessionId);
505
+ }
506
+ const read = await this._eval(session, tab, `(() => { /*arena-fill-read*/
507
+ const ed = document.activeElement;
508
+ if (!ed) return { ok: false, value: '' };
509
+ return { ok: true, value: (ed.value !== undefined ? ed.value : (ed.innerText || '')).slice(0, 2000) }; })()`);
510
+ return { ok: true, tab_id: tab.targetId, kind: focused.kind, value: read?.value ?? '' };
511
+ }
512
+
513
+ async _waitFor(session, tab, cond, timeoutSec) {
514
+ const deadline = Date.now() + Math.max(1, Math.min(Number(timeoutSec) || 15, 120)) * 1000;
515
+ const condJson = JSON.stringify(cond || {});
516
+ const expr = `(() => { /*arena-wait-check*/
517
+ const c = ${condJson};
518
+ const vis = (e) => { const r = e.getBoundingClientRect(); const s = getComputedStyle(e);
519
+ return r.width > 0 && r.height > 0 && s.display !== 'none' && s.visibility !== 'hidden'; };
520
+ const name = (e) => (e.getAttribute('aria-label') || e.innerText || e.value || e.getAttribute('placeholder') || '').trim();
521
+ const role = (e) => e.getAttribute('role') || e.tagName.toLowerCase();
522
+ const bodyText = document.body ? (document.body.innerText || '') : '';
523
+ switch (c.type) {
524
+ case 'text_present': return bodyText.includes(c.text || '');
525
+ case 'text_absent': return !bodyText.includes(c.text || '');
526
+ case 'dialog_closed': return ![...document.querySelectorAll('dialog[open],[role="dialog"],[role="alertdialog"]')].some(vis);
527
+ case 'composer_empty': { const sel = 'textarea,[contenteditable],[role="textbox"],input[type="text"],input[type="search"],input:not([type])';
528
+ const ed = [...document.querySelectorAll(sel)].filter(vis)[0]; if (!ed) return false;
529
+ return ((ed.value !== undefined ? ed.value : (ed.innerText || '')).trim() === ''); }
530
+ case 'element_present': case 'element_absent': {
531
+ const els = [...document.querySelectorAll('a,button,input,textarea,select,[contenteditable],[role]')].filter(vis);
532
+ const found = els.some((e) => (!c.text || name(e).toLowerCase().includes(String(c.text).toLowerCase())) &&
533
+ (!c.role || role(e).toLowerCase().includes(String(c.role).toLowerCase())) &&
534
+ (!c.name || name(e).toLowerCase().includes(String(c.name).toLowerCase())));
535
+ return c.type === 'element_present' ? found : !found; }
536
+ default: return false;
537
+ } })()`;
538
+ for (;;) {
539
+ let ok = false;
540
+ try { ok = !!(await this._eval(session, tab, expr)); } catch { ok = false; }
541
+ if (ok) return true;
542
+ if (Date.now() >= deadline) return false;
543
+ await sleep(500);
544
+ }
545
+ }
546
+
547
+ async _withExpect(session, tab, args, result) {
548
+ if (!args.expect || !result || result.ok === false) return result;
549
+ const verified = await this._waitFor(session, tab, args.expect, args.expect.timeout_sec || 10);
550
+ return { ...result, verified, expect: args.expect };
551
+ }
552
+
348
553
  /* ---------------- tool definitions ---------------- */
349
554
 
350
555
  tools() {
@@ -372,7 +577,7 @@ export class BrowserManager {
372
577
  schema: S(
373
578
  {
374
579
  headless: { type: 'boolean', description: 'true (default) = no window, false = visible window' },
375
- port: { type: 'number', description: 'CDP port (default 9223)' },
580
+ port: { type: 'number', description: 'CDP port (omit to get a free port automatically)' },
376
581
  url: { type: 'string', description: 'URL to open on start' },
377
582
  profile: {
378
583
  type: 'string',
@@ -395,17 +600,21 @@ export class BrowserManager {
395
600
  description:
396
601
  'List the agent\'s own named browser profiles (each keeps its own logins between runs). ' +
397
602
  'Launch one with chrome_launch {"profile":"<name>"}; the user logs into it once with ' +
398
- '"arena-bridge browser open <name>" on their machine.',
603
+ '"arena-bridge browser login <name>" on their machine.',
399
604
  approval: false,
400
605
  schema: S({}, []),
401
606
  required: [],
402
- handler: async () => {
403
- const profiles = listProfiles();
607
+ handler: async (args, ctx) => {
608
+ const sessions = ctx.browser.list();
609
+ const profiles = listProfiles().map((p) => {
610
+ const s = sessions.find((x) => x.profile === p.path);
611
+ return { ...p, running: !!s, port: s ? s.port : null, session_id: s ? s.session_id : null };
612
+ });
404
613
  return {
405
614
  ok: true,
406
615
  count: profiles.length,
407
616
  profiles,
408
- hint: 'Ask the user to log in: arena-bridge browser open <name> — then use chrome_launch {"profile":"<name>"}',
617
+ hint: 'Ask the user to log in: arena-bridge browser login <name> — then use chrome_launch {"profile":"<name>"}',
409
618
  };
410
619
  },
411
620
  },
@@ -507,10 +716,22 @@ export class BrowserManager {
507
716
  const { nodes } = await session.cdp.send('Accessibility.getFullAXTree', {}, tab.sessionId);
508
717
  const flat = (nodes || [])
509
718
  .filter((n) => n.role?.value && n.role.value !== 'none')
510
- .map((n) => ({ role: n.role.value, name: n.name?.value || '' }))
719
+ .map((n) => {
720
+ const state = {};
721
+ for (const prop of n.properties || []) {
722
+ const v = prop.value?.value;
723
+ if (v !== undefined && v !== null && v !== '') state[prop.name] = v;
724
+ }
725
+ const out = { role: n.role.value, name: n.name?.value || '' };
726
+ if (Object.keys(state).length) out.state = state;
727
+ return out;
728
+ })
511
729
  .filter((n) => n.name)
512
730
  .slice(0, 500);
513
- return { ok: true, tab_id: tab.targetId, mode, nodes: flat };
731
+ const dialogs = flat
732
+ .filter((n) => n.role === 'dialog' || n.role === 'alertdialog')
733
+ .map((n) => ({ name: n.name }));
734
+ return { ok: true, tab_id: tab.targetId, mode, nodes: flat, dialogs };
514
735
  }
515
736
  const expr =
516
737
  mode === 'html'
@@ -534,6 +755,82 @@ export class BrowserManager {
534
755
  };
535
756
  },
536
757
  },
758
+ {
759
+ name: 'chrome_observe',
760
+ description:
761
+ 'Observe a page for action: visible interactive elements with refs and states ' +
762
+ '(pressed/selected/checked), open dialogs, and the current text editor with its content. ' +
763
+ 'Refs from the result can be clicked with chrome_click {"ref": "..."}. Read-only.',
764
+ approval: false,
765
+ schema: S(
766
+ {
767
+ session_id: { type: 'string' },
768
+ tab_id: { type: 'string' },
769
+ limit: { type: 'number', description: 'max elements (default 80, max 300)' },
770
+ text_limit: { type: 'number', description: 'max chars of visible page text (default 6000)' },
771
+ },
772
+ [],
773
+ ),
774
+ required: [],
775
+ handler: async (args, ctx) => {
776
+ const session = needSession(args, ctx);
777
+ const tab = await this._tab(session, args.tab_id);
778
+ return ctx.browser._observe(session, tab, args);
779
+ },
780
+ },
781
+ {
782
+ name: 'chrome_fill_editor',
783
+ description:
784
+ 'Find the visible text editor on the page (textarea, contenteditable or textbox), ' +
785
+ 'optionally clear it, type text with real input events and read the value back. ' +
786
+ 'No CSS selector needed.',
787
+ approval: true,
788
+ approvalFor: requiresApproval,
789
+ schema: S(
790
+ {
791
+ session_id: { type: 'string' },
792
+ tab_id: { type: 'string' },
793
+ text: { type: 'string' },
794
+ clear: { type: 'boolean', description: 'clear the editor first (default true)' },
795
+ submit: { type: 'boolean', description: 'press Enter afterwards' },
796
+ expect: { type: 'object', description: 'condition to wait for afterwards (see chrome_wait_for)' },
797
+ },
798
+ ['text'],
799
+ ),
800
+ required: ['text'],
801
+ handler: async (args, ctx) => {
802
+ const session = needSession(args, ctx);
803
+ const tab = await this._tab(session, args.tab_id);
804
+ let res = await ctx.browser._fillEditor(session, tab, args);
805
+ if (res.ok && args.submit) await ctx.browser._press(session, tab, 'Enter');
806
+ if (args.submit) res = { ...res, submitted: true };
807
+ return ctx.browser._withExpect(session, tab, args, res);
808
+ },
809
+ },
810
+ {
811
+ name: 'chrome_wait_for',
812
+ description:
813
+ 'Wait until a page condition holds: {type: text_present|text_absent|element_present|element_absent|dialog_closed|composer_empty, text, role, name}. ' +
814
+ 'Returns ok=true with verified=true when it does, verified=false after the timeout. Read-only.',
815
+ approval: false,
816
+ schema: S(
817
+ {
818
+ session_id: { type: 'string' },
819
+ tab_id: { type: 'string' },
820
+ condition: { type: 'object' },
821
+ timeout_sec: { type: 'number', description: 'default 15, max 120' },
822
+ },
823
+ ['condition'],
824
+ ),
825
+ required: ['condition'],
826
+ handler: async (args, ctx) => {
827
+ const session = needSession(args, ctx);
828
+ const tab = await this._tab(session, args.tab_id);
829
+ const started = Date.now();
830
+ const verified = await ctx.browser._waitFor(session, tab, args.condition, args.timeout_sec || 15);
831
+ return { ok: true, tab_id: tab.targetId, verified, condition: args.condition, elapsed_ms: Date.now() - started };
832
+ },
833
+ },
537
834
  {
538
835
  name: 'chrome_click',
539
836
  description:
@@ -547,6 +844,9 @@ export class BrowserManager {
547
844
  selector: { type: 'string', description: 'CSS selector' },
548
845
  text: { type: 'string', description: 'visible text of the element to click' },
549
846
  index: { type: 'number', description: 'index within the ax snapshot' },
847
+ ref: { type: 'string', description: 'element ref from chrome_observe' },
848
+ snapshot_id: { type: 'string', description: 'snapshot_id from chrome_observe (detects stale refs)' },
849
+ expect: { type: 'object', description: 'condition to wait for afterwards (see chrome_wait_for)' },
550
850
  },
551
851
  [],
552
852
  ),
@@ -554,6 +854,10 @@ export class BrowserManager {
554
854
  handler: async (args, ctx) => {
555
855
  const session = needSession(args, ctx);
556
856
  const tab = await this._tab(session, args.tab_id);
857
+ if (args.ref) {
858
+ const res = await ctx.browser._clickRef(session, tab, args);
859
+ return ctx.browser._withExpect(session, tab, args, res);
860
+ }
557
861
  let expr;
558
862
  if (args.selector) {
559
863
  expr = `(() => { const el = document.querySelector(${JSON.stringify(args.selector)}); if (!el) return {clicked:false, reason:'not found'}; el.scrollIntoView({block:'center'}); el.click(); return {clicked:true, tag: el.tagName, text: (el.innerText||'').slice(0,120)}; })()`;
@@ -565,7 +869,8 @@ export class BrowserManager {
565
869
  return { ok: false, error: 'provide selector, text or index' };
566
870
  }
567
871
  const res = await this._eval(session, tab, expr);
568
- return { ok: !!(res && res.clicked), tab_id: tab.targetId, ...(res || {}) };
872
+ const out = { ok: !!(res && res.clicked), tab_id: tab.targetId, ...(res || {}) };
873
+ return ctx.browser._withExpect(session, tab, args, out);
569
874
  },
570
875
  },
571
876
  {
@@ -581,6 +886,7 @@ export class BrowserManager {
581
886
  tab_id: { type: 'string' },
582
887
  submit: { type: 'boolean', description: 'press Enter afterwards' },
583
888
  clear: { type: 'boolean', description: 'clear the field first (default true)' },
889
+ expect: { type: 'object', description: 'condition to wait for afterwards (see chrome_wait_for)' },
584
890
  },
585
891
  ['selector', 'text'],
586
892
  ),
@@ -605,7 +911,8 @@ export class BrowserManager {
605
911
  if (args.submit) {
606
912
  await this._press(session, tab, 'Enter');
607
913
  }
608
- return { ok: true, tab_id: tab.targetId, value: res.value, submitted: !!args.submit };
914
+ const out = { ok: true, tab_id: tab.targetId, value: res.value, submitted: !!args.submit };
915
+ return ctx.browser._withExpect(session, tab, args, out);
609
916
  },
610
917
  },
611
918
  {
package/src/cli.mjs CHANGED
@@ -94,7 +94,7 @@ Commands:
94
94
  service Run the bridge as a macOS background service
95
95
  install | uninstall | status
96
96
  browser The agent's own browser profiles
97
- list | open <name> | close <name>
97
+ list | login <name> | close <name> (open = deprecated alias of login)
98
98
 
99
99
  Common options:
100
100
  --mode direct|hub mode for "init"
@@ -154,12 +154,21 @@ function cmdInit(args) {
154
154
  console.log(`\nNext: arena-bridge start (then expose it with a tunnel)`);
155
155
  }
156
156
 
157
- function cmdStart(args) {
157
+ async function cmdStart(args) {
158
158
  const cfg = loadConfig();
159
159
  if (!cfg.token) {
160
160
  console.error('✗ no token in config — run "arena-bridge init" first');
161
161
  process.exit(1);
162
162
  }
163
+ try {
164
+ const { trustState, saveTrust } = await import('./trust.mjs');
165
+ const st = trustState();
166
+ if (!st.active) {
167
+ const now = Date.now();
168
+ saveTrust({ enabled: true, until: now + 3 * 3600 * 1000, tools: [], paths: [], createdAt: now });
169
+ console.log('✓ trust window auto-opened for 3 h (all tools, any agent) — no approvals needed');
170
+ }
171
+ } catch {}
163
172
  const engine = new Engine(cfg, { base: cfg.roots?.[0] || os.homedir() });
164
173
  const server = createDirectServer(cfg, engine);
165
174
  server.on('error', (e) => {
@@ -202,6 +211,17 @@ async function cmdTunnel(args) {
202
211
  const bin = String(args.flags.bin || 'cloudflared');
203
212
  const engine = new Engine(cfg, { base: cfg.roots?.[0] || os.homedir() });
204
213
 
214
+ // Auto-open blanket trust window on start (3h, all tools, any agent) per user request
215
+ try {
216
+ const { trustState, saveTrust } = await import('./trust.mjs');
217
+ const st = trustState();
218
+ if (!st.active) {
219
+ const now = Date.now();
220
+ saveTrust({ enabled: true, until: now + 3 * 3600 * 1000, tools: [], paths: [], createdAt: now });
221
+ console.log('✓ trust window auto-opened for 3 h (all tools, any agent) — no approvals needed');
222
+ }
223
+ } catch {}
224
+
205
225
  let server = null;
206
226
  if (await isPortOpen(port)) {
207
227
  console.log(`port ${port} is already serving — reusing the running instance`);
@@ -521,7 +541,7 @@ function cmdBrowser(args) {
521
541
  if (what === 'list') {
522
542
  const profiles = listProfiles();
523
543
  if (!profiles.length) {
524
- console.log('no agent browser profiles yet — create one: arena-bridge browser open <name>');
544
+ console.log('no agent browser profiles yet — create one: arena-bridge browser login <name>');
525
545
  return;
526
546
  }
527
547
  for (const p of profiles) {
@@ -531,10 +551,13 @@ function cmdBrowser(args) {
531
551
  console.log('\nThe agent opens a profile with: chrome_launch {"profile":"<name>"}');
532
552
  return;
533
553
  }
534
- if (what === 'open') {
554
+ if (what === 'open' || what === 'login') {
555
+ if (what === 'open') {
556
+ console.error('note: "browser open" is deprecated — use "browser login <name>" (same plain launch, renamed for clarity).');
557
+ }
535
558
  const name = args._[2];
536
559
  if (!name) {
537
- console.error('usage: arena-bridge browser open <name>');
560
+ console.error('usage: arena-bridge browser login <name>');
538
561
  process.exit(1);
539
562
  }
540
563
  const bin = findChrome();
@@ -548,7 +571,9 @@ function cmdBrowser(args) {
548
571
  `--user-data-dir=${profile}`,
549
572
  '--no-first-run',
550
573
  '--no-default-browser-check',
551
- '--remote-debugging-port=0',
574
+ // Plain human launch on purpose: no --remote-debugging-port, no automation
575
+ // flags. Sites (notably Google) refuse sign-in in a debugging browser,
576
+ // so the user logs in here and the agent attaches later via chrome_launch.
552
577
  ];
553
578
  const width = Number(args.flags.width || 0);
554
579
  const height = Number(args.flags.height || 0);
@@ -592,7 +617,7 @@ function cmdBrowser(args) {
592
617
  console.log(`✓ closed ${killed} Chrome window(s) for "${sanitizeProfile(name)}"`);
593
618
  return;
594
619
  }
595
- console.error(`unknown browser action: ${what}\nuse: list | open <name> | close <name>`);
620
+ console.error(`unknown browser action: ${what}\nuse: list | login <name> | close <name>`);
596
621
  process.exitCode = 1;
597
622
  }
598
623
 
package/src/direct.mjs CHANGED
@@ -73,13 +73,15 @@ export function createDirectServer(cfg, engine) {
73
73
  ok: true,
74
74
  mode: 'direct',
75
75
  device: cfg.device,
76
- version: '0.7.19',
76
+ version: '0.8.0',
77
77
  uptime_sec: Math.round(process.uptime()),
78
78
  });
79
79
  }
80
80
 
81
- // re-read the config per request so a rotated token takes effect without a restart
82
- if (!isAuthorized(req, loadConfig())) {
81
+ // Re-read once per request so auth, policy, limits and the response agree.
82
+ const cfgNow = loadConfig();
83
+ mcpCtx.cfg = cfgNow;
84
+ if (!isAuthorized(req, cfgNow)) {
83
85
  res.setHeader('www-authenticate', 'Bearer');
84
86
  return sendJson(res, 401, { ok: false, error: 'unauthorized' });
85
87
  }
@@ -89,23 +91,23 @@ export function createDirectServer(cfg, engine) {
89
91
  }
90
92
 
91
93
  if (req.method === 'GET' && p === '/prompt') {
92
- const text = buildPrompt(cfg, { tool: engine, baseUrl: url.searchParams.get('base') || undefined });
94
+ const text = buildPrompt(cfgNow, { tool: engine, baseUrl: url.searchParams.get('base') || undefined });
93
95
  return sendText(res, 200, text);
94
96
  }
95
97
 
96
98
  if (req.method === 'GET' && p === '/config') {
97
99
  return sendJson(res, 200, {
98
100
  ok: true,
99
- device: cfg.device,
100
- roots: cfg.roots,
101
- protected_count: cfg.protected.length,
102
- requireApproval: cfg.requireApproval,
103
- allowShell: cfg.allowShell,
104
- approvalWaitSec: cfg.approvalWaitSec,
105
- maxOutputBytes: cfg.maxOutputBytes,
101
+ device: cfgNow.device,
102
+ roots: cfgNow.roots,
103
+ protected_count: cfgNow.protected.length,
104
+ requireApproval: cfgNow.requireApproval,
105
+ allowShell: cfgNow.allowShell,
106
+ approvalWaitSec: cfgNow.approvalWaitSec,
107
+ maxOutputBytes: cfgNow.maxOutputBytes,
106
108
  telegram: {
107
- enabled: !!cfg.telegram?.enabled,
108
- chat_ids: cfg.telegram?.chatIds || [],
109
+ enabled: !!cfgNow.telegram?.enabled,
110
+ chat_ids: cfgNow.telegram?.chatIds || [],
109
111
  },
110
112
  });
111
113
  }
@@ -156,7 +158,7 @@ export function createDirectServer(cfg, engine) {
156
158
  }
157
159
 
158
160
  if (req.method === 'POST' && p === '/exec') {
159
- const body = await readJson(req, cfg.maxUploadBytes);
161
+ const body = await readJson(req, cfgNow.maxUploadBytes);
160
162
  const result = await engine.call('bash', {
161
163
  command: body.command,
162
164
  cwd: body.cwd,
@@ -167,7 +169,7 @@ export function createDirectServer(cfg, engine) {
167
169
  }
168
170
 
169
171
  if (req.method === 'POST' && p === '/call') {
170
- const body = await readJson(req, cfg.maxUploadBytes);
172
+ const body = await readJson(req, cfgNow.maxUploadBytes);
171
173
  if (!body.tool) return sendJson(res, 400, { ok: false, error: 'tool is required' });
172
174
  const result = await engine.call(String(body.tool), body.args || {});
173
175
  return sendJson(res, result.ok ? 200 : 400, result);
@@ -176,7 +178,7 @@ export function createDirectServer(cfg, engine) {
176
178
  if (p === '/files') {
177
179
  const target = url.searchParams.get('path');
178
180
  if (!target) return sendJson(res, 400, { ok: false, error: 'path query parameter is required' });
179
- const chk = checkPath(target, cfg, engine.base, { write: req.method !== 'GET' });
181
+ const chk = checkPath(target, cfgNow, engine.base, { write: req.method !== 'GET' });
180
182
  if (!chk.ok) return sendJson(res, 403, { ok: false, error: chk.error, reason: chk.reason });
181
183
  try {
182
184
  if (req.method === 'GET') {
@@ -184,7 +186,7 @@ export function createDirectServer(cfg, engine) {
184
186
  return sendRaw(res, 200, buf);
185
187
  }
186
188
  if (req.method === 'PUT' || req.method === 'POST') {
187
- const buf = await readBody(req, cfg.maxUploadBytes);
189
+ const buf = await readBody(req, cfgNow.maxUploadBytes);
188
190
  await fsp.mkdir(chk.abs.replace(/\/[^/]*$/, ''), { recursive: true });
189
191
  await fsp.writeFile(chk.abs, buf);
190
192
  return sendJson(res, 200, { ok: true, path: chk.abs, bytes: buf.length });
package/src/engine.mjs CHANGED
@@ -7,7 +7,7 @@ import { checkPath, resolveTarget, isInside } from './policy.mjs';
7
7
  import { audit, saveArtifact, tailAudit, readArtifact, auditDir } from './audit.mjs';
8
8
  import { requestApproval } from './approvals.mjs';
9
9
  import { loadConfig, CONFIG_PATH } from './config.mjs';
10
- import { BrowserManager } from './browser.mjs';
10
+ import { BrowserManager, cleanupOrphanChromes } from './browser.mjs';
11
11
  import { macTools } from './mac.mjs';
12
12
  import { trustState, trustAllows } from './trust.mjs';
13
13
  import { telegramTools } from './telegram.mjs';
@@ -628,6 +628,9 @@ export class Engine {
628
628
  this.base = base || process.cwd();
629
629
  this.sessions = new Map();
630
630
  this.browser = new BrowserManager(cfg);
631
+ // Best effort: remove headless agent Chromes a previous bridge run left
632
+ // behind. Login windows (visible) are never touched.
633
+ cleanupOrphanChromes().catch(() => {});
631
634
  }
632
635
 
633
636
  catalog() {
@@ -635,6 +638,11 @@ export class Engine {
635
638
  }
636
639
 
637
640
  async call(name, args = {}, { requireApproval } = {}) {
641
+ try {
642
+ if (fs.existsSync(CONFIG_PATH)) this.cfg = loadConfig();
643
+ } catch {
644
+ /* keep the last valid snapshot */
645
+ }
638
646
  const tool = this.catalog().find((t) => t.name === name);
639
647
  if (!tool) return { ok: false, error: `unknown tool: ${name}` };
640
648
  const started = Date.now();
@@ -651,13 +659,6 @@ export class Engine {
651
659
  return saved;
652
660
  };
653
661
 
654
- // pick up policy changes (roots, protected, requireApproval) without a restart
655
- try {
656
- if (fs.existsSync(CONFIG_PATH)) this.cfg = loadConfig();
657
- } catch {
658
- /* keep the snapshot */
659
- }
660
-
661
662
  let needApproval = tool.approvalFor ? !!tool.approvalFor(args, this) : !!tool.approval;
662
663
  if (this.cfg.requireApproval === false) needApproval = false;
663
664
  if (requireApproval === true) needApproval = true;
package/src/hub.mjs CHANGED
@@ -90,7 +90,7 @@ export function createHubServer(cfg) {
90
90
  return sendJson(res, 200, {
91
91
  ok: true,
92
92
  mode: 'hub',
93
- version: '0.7.19',
93
+ version: '0.8.0',
94
94
  devices: [...devices.keys()],
95
95
  });
96
96
  }
package/src/mcp.mjs CHANGED
@@ -48,7 +48,7 @@ export async function handleMcp(req, res, ctx) {
48
48
  mcpResponse(id, {
49
49
  protocolVersion: PROTOCOL_VERSION,
50
50
  capabilities: { tools: { listChanged: false } },
51
- serverInfo: { name: 'arena-bridge', version: '0.7.19' },
51
+ serverInfo: { name: 'arena-bridge', version: '0.8.0' },
52
52
  instructions:
53
53
  'Tools operate the user\'s own machine. Dangerous operations may return ' +
54
54
  'PROTECTED and require a local human approval.',
package/src/prompt.mjs CHANGED
@@ -68,16 +68,21 @@ ${tools.join('\n')}
68
68
  | tell the user something or ask a question | \`notify_user\` (Telegram), \`telegram_inbox\` to read the answer |
69
69
 
70
70
  ## Browser control (Chrome over CDP)
71
+ 0. Managed profiles are logged in by the human with \`arena-bridge browser login <name>\`
72
+ (a plain Chrome launch — never sign in through \`chrome_launch\`; if a page asks for
73
+ login, report it and move on). For web pages prefer observing DOM state
74
+ (\`chrome_observe\`: elements with refs and pressed/selected states), acting by ref and
75
+ verifying the result (\`expect\` / \`chrome_wait_for\`) over screenshots and coordinates.
71
76
  1. \`chrome_launch\` starts a browser that belongs to you — own profile, no personal data.
72
77
  \`{"headless": true, "url": "https://example.com"}\` (headless=false opens a visible window).
73
- 2. Then \`chrome_open\` → \`chrome_snapshot\` → \`chrome_click\`/\`chrome_type\` → \`chrome_screenshot\`.
74
- \`chrome_snapshot\` with \`{"mode": "ax"}\` returns roles and names, which is the best input for clicking.
78
+ 2. Then \`chrome_open\` → \`chrome_observe\` → \`chrome_click\` by ref / \`chrome_fill_editor\` → verify with \`expect\`.
79
+ \`chrome_snapshot\` with \`{"mode": "ax"}\` returns roles, names and element states; \`chrome_screenshot\` is for when the DOM is ambiguous.
75
80
  3. \`chrome_attach\` connects to the user's own Chrome (default port 9222, with their logged-in sessions).
76
81
  Actions on an attached browser require a local approval — ask for it instead of retrying.
77
82
  4. Screenshots: pass \`{"path": "shot.png"}\` to save the PNG inside the authorized roots, then read it with \`file_get\`.
78
83
  5. Named profiles keep their logins between runs: \`chrome_launch {"profile":"work"}\` opens
79
84
  \`~/.arena-bridge/chrome-profiles/work\`. \`chrome_profiles\` lists them. The user logs in once with
80
- \`arena-bridge browser open work\` on their machine — ask for that instead of trying to log in yourself.
85
+ \`arena-bridge browser login work\` on their machine — ask for that instead of trying to log in yourself.
81
86
  6. A visible window can be kept small and out of the way: \`chrome_launch {"headless":false,"width":900,"height":700,"x":-920,"y":40}\`.
82
87
 
83
88
  ## Desktop control (macOS, through Peekaboo)