@glittering0717/ai-browser 2.5.10 → 2.6.1

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/README.md CHANGED
@@ -385,7 +385,7 @@ Three things used to make "run it in the background" impossible, and all three a
385
385
  now driven only by you (Dock icon) or by an explicit `ui.window` call.
386
386
  - A tab that was never attached to the window had **no compositor surface**: reading, clicking and
387
387
  typing worked, but `screenshot` returned 0 bytes and `paint_check` reported `no-frame` — a black box.
388
- All tabs are now attached and z-ordered (`setTopBrowserView`), so a background tab renders and can be
388
+ All tabs are now attached and z-ordered (re-adding a view moves it to the top), so a background tab renders and can be
389
389
  screenshotted.
390
390
 
391
391
  ```bash
@@ -504,7 +504,7 @@ and nothing is restored afterwards (the emulation is sticky; toggling it would p
504
504
  visible→hidden→visible on every action). If a tab still can't receive input, the action is **not
505
505
  dispatched** and the receipt says so: `success:false` + `input_blocked:true` + the page's visibility,
506
506
  with the escape hatches named (JS path via `ui.evaluate`, or show the window once). Measured 2026-11-03:
507
- raising the tab (`setTopBrowserView`), `webContents.focus()`, detach+reattach, `setBounds`,
507
+ raising the tab (re-adding it on top), `webContents.focus()`, detach+reattach, `setBounds`,
508
508
  `setBackgroundThrottling(false)` and `Page.setWebLifecycleState{active}` do **not** deliver input;
509
509
  `Emulation.setPageVisibilityOverride` does not exist in Chromium 130. "The active tab is always visible"
510
510
  turned out to be false too — under `AI_BROWSER_FOCUS=never` even it reports hidden and input is dropped,
@@ -729,7 +729,7 @@ node examples/read-page.mjs
729
729
 
730
730
  ## Troubleshooting
731
731
 
732
- **The page updated but the window still shows the old one.** A `BrowserView`'s surface is only
732
+ **The page updated but the window still shows the old one.** A tab view's surface is only
733
733
  re-rasterized when something forces it, so the app asks for a repaint on window `show` / `restore` /
734
734
  `focus` — that matters because this window is designed never to steal your focus, so it can sit behind
735
735
  your editor for minutes while the agent works. `ui.repaint` forces it on demand and reports
@@ -1009,7 +1009,7 @@ to launch with *"another AI Browser instance is already running"*.
1009
1009
 
1010
1010
  **`npm run view`** — the suite for the two things that quietly produce false confidence.
1011
1011
 
1012
- *Viewport.* A `BrowserView` can be fully loaded and still show stale pixels, and the only thing
1012
+ *Viewport.* A tab view can be fully loaded and still show stale pixels, and the only thing
1013
1013
  that forced a repaint was switching tabs — which is exactly why "click the tabs a few times" used to
1014
1014
  fix it. It now fires on every navigation signal and on window `show`/`restore`/`focus`. The suite
1015
1015
  proves it from the **cumulative repaint counter**, not from a screenshot: a screenshot needs eyes, a
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@glittering0717/ai-browser",
3
- "version": "2.5.10",
3
+ "version": "2.6.1",
4
4
  "description": "AI-native browser — renders pixels for humans, structured semantic-tree API for agents. No screenshots, no OCR, no coordinate guessing.",
5
5
  "main": "src/main/index.js",
6
6
  "bin": {
@@ -104,7 +104,7 @@
104
104
  },
105
105
  "dependencies": {
106
106
  "@modelcontextprotocol/sdk": "^1.29.0",
107
- "electron": "^33.4.11",
107
+ "electron": "^45.5.0",
108
108
  "ws": "^8.0.0"
109
109
  },
110
110
  "devDependencies": {
@@ -95,10 +95,15 @@ function rpcCall({ port, timeout, method, params }) {
95
95
  // 自报家门,但**不要**自动工作区:CLI 是人坐在终端前敲的,"不传 tab
96
96
  // 就打当前页"才是他期望的语义。auto_workspace:false 只关掉"自动开一页",
97
97
  // 分组照旧 —— 它自己 new_tab 出来的页仍然归到 "Terminal" 组里。
98
+ //
99
+ // key/scope 必须是**固定值**(2026-11-29 修):以前这里不带 key,服务端就
100
+ // 用连接 id 兜底,而 CLI 每条命令都是一个新连接 —— 于是每敲一次
101
+ // `ai-browser call ...` 就多一个 "Terminal" 组,只要开过页就永久留在标签栏上。
102
+ // 现在所有 CLI 调用共用一组;capped:false 让批量脚本不受 8 页上限约束。
98
103
  try {
99
104
  ws.send(JSON.stringify({
100
105
  jsonrpc: '2.0', method: 'ui.hello',
101
- params: { label: 'Terminal', auto_workspace: false },
106
+ params: { label: 'Terminal', auto_workspace: false, key: 'cli', scope: 'cli', capped: false },
102
107
  }));
103
108
  } catch (_) { /* 发不出去就退化成匿名连接 */ }
104
109
  ws.send(JSON.stringify({ jsonrpc: '2.0', id: 1, method, params }));
@@ -30,6 +30,9 @@ ai-browser call ui.act '{"action":"click","target":"e:a-3"}' # 点它
30
30
 
31
31
  第一次动手给你一页**空白工作页**,归你的标签组;省略 `tab` 就打它,用户的页不会被碰。
32
32
 
33
+ 用完的页随手关掉(`browse_tabs {"op":"close","tab":N}`)。组是你这一轮任务的空间,
34
+ 页攒着不关就会一直挂在用户的标签栏上(每组上限 8,满了会挤掉最久没用的那页)。
35
+
33
36
  ## 点东西只认 id
34
37
 
35
38
  `act` 的 `target` 只认 `get_tree` 给的 id;传文字会报 `target_not_found`。
@@ -2,6 +2,17 @@
2
2
 
3
3
  浏览器里同时有两个人在干活:**用户**,和**每一个 agent**。他们的标签页不混在一起。
4
4
 
5
+ ## 组是怎么认人的
6
+
7
+ 一个 agent 就是一组,**换会话、重启客户端也还是那一组**。浏览器按"客户端名 +
8
+ 工作目录"认人:同一个项目里再开一次会话,会接回上次那一组(页还在,接着干)。
9
+
10
+ 同一个框架里**同时**跑两个 agent 时不会合并 —— 先到的占着原组,后到的另起一组,
11
+ 名字后面带编号(`Trae` / `Trae #4`)。想让某类任务固定单独占一组,给启动它的
12
+ 进程设 `AI_BROWSER_AGENT_SCOPE=<任意短名>`。
13
+
14
+ 命令行(`ai-browser call ...`)不受 8 页上限约束,所有 CLI 调用共用 `Terminal` 组。
15
+
5
16
  ## 你的工作页从哪来
6
17
 
7
18
  第一次真正动手(读树、导航、点击…)而还没指定过目标时,浏览器会给你开一页
@@ -68,11 +68,18 @@ function actionsFor(role) {
68
68
  }
69
69
  }
70
70
 
71
+ // CDP 的布尔属性有两种类型名:`boolean`(disabled / required 这类)和
72
+ // `booleanOrUndefined`(focusable 这类"可能没定义"的)。只认前者会把 focusable
73
+ // 整个丢掉 —— 而它正是"这东西能不能聚焦/是不是控件"的唯一 AX 信号。
74
+ // 2026-11-29 实测 Chromium 156:tabindex=0 的 generic 容器 focusable 是
75
+ // booleanOrUndefined;按旧判据整层 states 为空,agg 也连着失效。
76
+ const BOOL_TYPES = { boolean: 1, booleanOrUndefined: 1 };
77
+
71
78
  function statesFor(node) {
72
79
  const states = [];
73
80
  for (const p of node.properties || []) {
74
81
  const v = p.value;
75
- if (!v || v.type !== 'boolean' || v.value !== true) continue;
82
+ if (!v || !BOOL_TYPES[v.type] || v.value !== true) continue;
76
83
  if (p.name === 'read-only') { states.push('readonly'); continue; }
77
84
  if (p.name === 'hidden') continue;
78
85
  // required 在下面单独处理(要给出与 DOM 路径同一份语义)。这里跳过,
@@ -311,6 +318,35 @@ const AGG_CONTAINER_ROLES = {
311
318
  table: 1, rowgroup: 1, toolbar: 1, tablist: 1, menu: 1, tabpanel: 1, document: 1,
312
319
  };
313
320
 
321
+ // ARIA 1.2 起,`generic` 角色的 "name from contents" 是 **prohibited**;Chromium
322
+ // 已经按规范执行(实测 156,见 2026-11-29 wrapper 套件)。于是同一个行容器:
323
+ // Chromium ≤155 : generic,label = 整段预览文字的拼接(所以能打 agg:1)
324
+ // Chromium 156 : generic,label = 空;文字只剩在子 StaticText 上
325
+ // DOM 读层(preload/extractor.cjs)不受影响——它按 ownText 为空 + 有子元素自己拼。
326
+ // 两层不一致的后果很具体:`ax:true` 读同一页时,按文字找链接会命中不可点的
327
+ // StaticText,容器自己没有 label 也就没有 agg——又回到"树里有、点不动"且没信号。
328
+ //
329
+ // 这里只把**控件形状**的容器补回来(focusable / 带 click 动作),不碰纯布局容器:
330
+ // 后者本来就不该有名字,补了会让每棵树都膨胀。合成出来的 label 显式带
331
+ // `label_synthesized:1`——读树的人必须知道这段不是内核给的,而是我们按子节点拼的。
332
+ const CONTROL_STATES = { focusable: 1, focused: 1, clickable: 1 };
333
+
334
+ function synthesizedLabel(n, descendantLabels) {
335
+ const isControl = (n.actions || []).indexOf('click') >= 0
336
+ || (n.states || []).some((s) => CONTROL_STATES[s]);
337
+ if (!isControl) return '';
338
+ const parts = [];
339
+ for (const raw of descendantLabels) {
340
+ const t = String(raw || '').trim();
341
+ if (!t) continue;
342
+ // 超长子块不参与:那多半是整段正文,不是"这个控件叫什么"。
343
+ if (t.length > 200) continue;
344
+ parts.push(t);
345
+ if (parts.join('\n').length > 220) break;
346
+ }
347
+ return parts.join('\n');
348
+ }
349
+
314
350
  export function markAggregatedLabels(tree) {
315
351
  const visit = (n) => {
316
352
  if (!n) return { labels: [] };
@@ -320,12 +356,22 @@ export function markAggregatedLabels(tree) {
320
356
  if (k.self) descendantLabels.push(k.self);
321
357
  for (const l of k.labels) descendantLabels.push(l);
322
358
  }
323
- const label = String(n.label || '');
359
+ let label = String(n.label || '');
360
+ if (!label && AGG_CONTAINER_ROLES[n.role] && descendantLabels.length) {
361
+ const syn = synthesizedLabel(n, descendantLabels);
362
+ if (syn) {
363
+ n.label = syn;
364
+ n.label_synthesized = 1;
365
+ label = syn;
366
+ }
367
+ }
324
368
  if (AGG_CONTAINER_ROLES[n.role] && label.length >= 8 && descendantLabels.length) {
325
369
  const aggregated = descendantLabels.some((l) => l.length >= 4 && l.length < label.length && label.indexOf(l) >= 0);
326
370
  if (aggregated) n.agg = 1;
327
371
  }
328
- return { self: label.length >= 4 ? label : '', labels: descendantLabels };
372
+ // self 收 1 字以上的 label:合成拼接串时要连短标签一起算("10-21" 这种就是
373
+ // 行容器名字的一部分),而 agg 判据自己仍然只认 ≥4 字的子串,不会因此变松。
374
+ return { self: label.length >= 1 ? label : '', labels: descendantLabels };
329
375
  };
330
376
  visit(tree);
331
377
  return tree;
package/src/main/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // main/index.js — Electron entry v5 (multi-tab, real UI)
2
2
  // One process, one WS server, multiple tabs with real tab bar.
3
- import { app, BrowserWindow, BrowserView, ipcMain, net, session, dialog, screen, powerMonitor, shell, nativeImage } from 'electron';
3
+ import { app, BrowserWindow, ipcMain, net, session, dialog, screen, powerMonitor, shell, nativeImage } from 'electron';
4
+ import { createView, attach, detach, raise } from './view_layer.js';
4
5
  import path from 'node:path';
5
6
  import fs from 'node:fs';
6
7
  import os from 'node:os';
@@ -108,7 +109,7 @@ try {
108
109
  // macOS 底层仍是 NSAlert 模态循环,**会阻塞主进程**。对照实验:不弹框时 WS 4ms
109
110
  // 返回,弹框期间 8 秒无响应。一次"能自愈的异常"就这样被放大成"所有 RPC 超时",
110
111
  // agent 那边看到的是满屏 timed_out,与真实原因(一个无关的 JS 异常)完全对不上。
111
- // 横幅是窗口里的一个 BrowserView:主进程照常跑,RPC 不受影响。
112
+ // 横幅是窗口里的一个独立视图:主进程照常跑,RPC 不受影响。
112
113
  //
113
114
  // 原生框仍作为调试开关保留(AI_BROWSER_ERROR_DIALOG=1),但只在需要"停下来盯着看"
114
115
  // 时用;默认路径绝不允许阻塞。
@@ -204,18 +205,18 @@ function showNativeFatalDialog(detail) {
204
205
  // .webContents.isDestroyed();
205
206
  // 2. tabBarView 每次被 pushNavState 摸到时顺手体检一次,死了就重建 —— 把原来的
206
207
  // "崩溃点"变成"自愈点";
207
- // 3. 重建有节流与上限:GPU 连续崩溃时不能无限 new BrowserView(那就是另一种死循环)。
208
+ // 3. 重建有节流与上限:GPU 连续崩溃时不能无限 new WebContentsView(那就是另一种死循环)。
208
209
  let tabBarRebuildAt = 0;
209
210
  let tabBarRebuildCount = 0;
210
211
  const TAB_BAR_REBUILD_MIN_GAP = 1500; // 两次重建至少隔 1.5s
211
212
  const TAB_BAR_REBUILD_MAX = 8; // 单进程生命周期内最多重建 8 次
212
213
 
213
- /** 造一个全新的标签栏 BrowserView 并挂进窗口。交互靠主进程全局 IPC,不用逐 view 重绑。 */
214
+ /** 造一个全新的标签栏视图并挂进窗口。交互靠主进程全局 IPC,不用逐 view 重绑。 */
214
215
  function createTabBarView() {
215
- const view = new BrowserView({
216
+ const view = createView({
216
217
  webPreferences: { contextIsolation: false, nodeIntegration: true },
217
218
  });
218
- mainWindow.addBrowserView(view);
219
+ attach(mainWindow, view);
219
220
  try {
220
221
  const cb = mainWindow.getContentBounds();
221
222
  view.setBounds({ x: 0, y: 0, width: cb.width, height: TAB_BAR_HEIGHT });
@@ -262,7 +263,7 @@ function ensureTabBarAlive(force = false) {
262
263
  }
263
264
  // 摘掉悬空的旧对象(try 包裹:它可能已经被 Electron 内部拆掉了)。
264
265
  if (tabBarView) {
265
- try { mainWindow.removeBrowserView(tabBarView); } catch {}
266
+ try { detach(mainWindow, tabBarView); } catch {}
266
267
  }
267
268
  try {
268
269
  tabBarView = createTabBarView();
@@ -309,20 +310,20 @@ function ensureErrorBanner() {
309
310
  if (isAlive(errorBannerView)) return errorBannerView;
310
311
  // 悬空的旧横幅:先从窗口摘掉,再重建。否则旧对象会一直占着 z 序位置。
311
312
  if (errorBannerView) {
312
- try { mainWindow.removeBrowserView(errorBannerView); } catch {}
313
+ try { detach(mainWindow, errorBannerView); } catch {}
313
314
  errorBannerView = null;
314
315
  }
315
316
  if (!mainWindow || mainWindow.isDestroyed()) return null;
316
317
  // 与标签栏同样的配置(nodeIntegration + 非隔离):横幅是我们自己的本地页面,
317
318
  // 需要 ipcRenderer 才能把「×」传回主进程。页面里没有任何远程内容。
318
- errorBannerView = new BrowserView({
319
+ errorBannerView = createView({
319
320
  webPreferences: { contextIsolation: false, nodeIntegration: true },
320
321
  });
321
322
  try { errorBannerView.setBackgroundColor('#7f1d1d'); } catch {}
322
- mainWindow.addBrowserView(errorBannerView);
323
+ attach(mainWindow, errorBannerView);
323
324
  // 新开 tab 会往窗口里再加一个视图;显式置顶一次,避免"横幅被后来的页面盖住"
324
325
  // 这种只在特定时序下出现的 bug。
325
- try { mainWindow.setTopBrowserView(errorBannerView); } catch {}
326
+ try { raise(mainWindow, errorBannerView); } catch {}
326
327
  errorBannerView.webContents.loadFile(path.join(__dirname, '../renderer/error_banner.html'));
327
328
  return errorBannerView;
328
329
  }
@@ -347,9 +348,9 @@ function hideErrorBanner() {
347
348
  const view = errorBannerView;
348
349
  errorBannerView = null;
349
350
  try {
350
- if (mainWindow && !mainWindow.isDestroyed()) mainWindow.removeBrowserView(view);
351
+ if (mainWindow && !mainWindow.isDestroyed()) detach(mainWindow, view);
351
352
  } catch {}
352
- try { view.webContents.destroy(); } catch {}
353
+ try { view.webContents.close(); } catch {}
353
354
  layoutWithBanner();
354
355
  }
355
356
 
@@ -791,7 +792,7 @@ function createWindow() {
791
792
  });
792
793
  ipcMain.handle('tab:list', () => pageManager.listTabs());
793
794
 
794
- // 错误横幅上的「×」:连关闭动作也走 IPC —— 横幅是独立的 BrowserView,
795
+ // 错误横幅上的「×」:连关闭动作也走 IPC —— 横幅是独立视图,
795
796
  // 它没有别的通道能告诉主进程"把我收起来"。
796
797
  // setImmediate:不要在渲染进程自己派发的事件里销毁它所属的 frame(先让这条
797
798
  // IPC 走完,再拆视图)。
@@ -956,7 +957,7 @@ function createWindow() {
956
957
  mainWindow.on('browser-window-focus', repaintActive);
957
958
 
958
959
  // === Renderer crash handling — log only, don't kill the app ===
959
- // The main BrowserWindow has no real content (it hosts BrowserViews), so a
960
+ // The main BrowserWindow has no real content (it hosts WebContentsViews), so a
960
961
  // renderer-gone event here is usually a transient GPU/sandbox hiccup.
961
962
  // Quitting on it makes the whole app flash-exit on startup. Just log.
962
963
  mainWindow.webContents.on('render-process-gone', (_e, details) => {
@@ -1421,7 +1422,7 @@ app.whenReady().then(() => {
1421
1422
  // AI_BROWSER_ERROR_DIALOG=1(它阻塞主进程,只用于人工盯着看)。
1422
1423
  //
1423
1424
  // `crash-close` 存在的原因:收起横幅那条路(销毁视图 + 把让出去的 34px 还回去)
1424
- // 没法从外面点 —— 它是个独立的 BrowserView,没有 WS 入口,而 macOS 上模拟点击
1425
+ // 没法从外面点 —— 它是个独立视图,没有 WS 入口,而 macOS 上模拟点击
1425
1426
  // 需要辅助功能权限(实测被拒,-10004)。不测就等于把"横幅关不掉 / 关了以后
1426
1427
  // 底部一直空一条"这类问题留给用户。
1427
1428
  const _selftest = String(process.env.AI_BROWSER_SELFTEST || '');
@@ -1461,7 +1462,7 @@ app.whenReady().then(() => {
1461
1462
  // 权限同理:不装 setPermissionRequestHandler 时每个请求被静默拒绝,站点要
1462
1463
  // pointerLock / clipboard 写入才能继续时,agent 完全看不出是被权限挡住。
1463
1464
  //
1464
- // ⚠️ 必须是 tab 的 BrowserView 实际所在的那个 session。它们用的是
1465
+ // ⚠️ 必须是 tab 视图实际所在的那个 session。它们用的是
1465
1466
  // `partition: config.viewPartition`,不是 defaultSession —— 第一版传了
1466
1467
  // defaultSession,钩子装到了另一个 session 上,表现是"页面确实发出了下载请求
1467
1468
  // (服务端收到了 GET),但 will-download 从不触发、文件不落盘"。
@@ -1584,7 +1585,7 @@ function _finishCleanup() {
1584
1585
  networkWatchdog = null;
1585
1586
  }
1586
1587
  // Close PageManager first — it rejects pending IPC requests and tears down
1587
- // BrowserViews cleanly. Otherwise pending requests hang the WS server close.
1588
+ // views cleanly. Otherwise pending requests hang the WS server close.
1588
1589
  if (pageManager) {
1589
1590
  try { pageManager.close(); } catch (e) { console.error('[index] pageManager.close error:', e.message); }
1590
1591
  pageManager = null;
@@ -10,6 +10,8 @@ import {
10
10
  ListResourceTemplatesRequestSchema,
11
11
  } from '@modelcontextprotocol/sdk/types.js';
12
12
  import path from 'node:path';
13
+ import os from 'node:os';
14
+ import { createHash } from 'node:crypto';
13
15
  import { fileURLToPath } from 'node:url';
14
16
  // 启动逻辑与 CLI 共用一份(scripts/ensure_running.cjs)。曾经这里有一份私有实现,
15
17
  // 结果 MCP 能自动拉起浏览器、CLI 不能 —— 同一件事两个行为。
@@ -43,6 +45,30 @@ const { ensureRunning, checkPort } = ensureMod;
43
45
  // (`ai-browser call` / `ai-browser ensure`) shares ONE implementation with this
44
46
  // server. Duplicating it here is what made "install and use" a two-step dance
45
47
  // for CLI callers: MCP auto-started the browser, the CLI did not.
48
+ // 这个 MCP 进程的身份 —— 一次 agent 会话一个,进程内所有重连共用。
49
+ //
50
+ // 为什么需要它(2026-11-29 用户反馈"agent 开了一堆标签组"):组的归属以前是
51
+ // **一次 WS 连接**,而 mcp_ws 会自动重连 —— 浏览器重启、网络抖动、断线重试,
52
+ // 每重连一次就是一个新连接、一个新组,旧组带着页留在标签栏上。
53
+ // 现在由进程自己报一个稳定 key:进程内的重连复用同一组;
54
+ // 同一框架里开多个 agent(各自一个进程)仍然是各自独立的组 —— 它们干的事不一样。
55
+ const AGENT_KEY = `mcp-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
56
+
57
+ // 进程会死,身份不会 —— 这是第二个键,比 AGENT_KEY 活得久。
58
+ //
59
+ // 为什么还需要它(2026-11-29 第二轮,用户还是看到"一个 agent 开多个组"):
60
+ // AGENT_KEY 是**进程**级的。客户端每开一次新会话就拉起一个新的 MCP 进程,
61
+ // 于是每次都是新 key、新组;上一组的页还留着,就又多一组。实测:连续起两个
62
+ // MCP 进程各导航一次 → 标签栏两个组(Agent 37 / Agent 41)。
63
+ //
64
+ // scope 由"客户端名 + 工作目录"算出来,跟进程无关:同一个客户端在同一个项目里
65
+ // 干活,不论换了多少次进程,浏览器那边都能认出是同一个人,把上一次的组接回来。
66
+ // 同一框架里**同时**开两个 agent 不会被合并:收养只发生在"组里没人用"的时候,
67
+ // 先到的占着组,后到的只能新建。
68
+ //
69
+ // AI_BROWSER_AGENT_SCOPE 可以手动指定(想让某类任务单独占一组时用)。
70
+ const AGENT_SCOPE = agentScope();
71
+
46
72
  const wsClient = createWsClient({
47
73
  url: WS_URL,
48
74
  timeout: WS_TIMEOUT,
@@ -50,9 +76,33 @@ const wsClient = createWsClient({
50
76
  // 自报家门:告诉浏览器"我是一个 agent",于是它会给我一块自己的标签组
51
77
  // (默认目标永远是组里我自己的页,绝不开在人眼前那一页上)。
52
78
  // 名字只用于标签栏上那个小圆点旁边的文字,猜不出来就留空让服务端编号。
53
- hello: { label: agentLabel(), auto_workspace: true },
79
+ // key 是**进程**的身份(重连不变),scope 是**agent** 的身份(换进程也不变)。
80
+ // AI_BROWSER_DEBUG_HELLO=1 时把这几个字段打到 stderr —— 排查"组怎么分的"用。
81
+ hello: (() => {
82
+ const payload = { label: agentLabel(), auto_workspace: true, key: AGENT_KEY, scope: AGENT_SCOPE };
83
+ if (process.env.AI_BROWSER_DEBUG_HELLO) console.error('[hello]', JSON.stringify(payload));
84
+ return payload;
85
+ })(),
54
86
  });
55
87
 
88
+ /**
89
+ * 算一个跨进程稳定的 agent 身份。
90
+ *
91
+ * 工作目录是"这个 agent 在给哪个项目干活"的代理:同一个项目 → 同一个身份,
92
+ * 不同项目 → 各占一组。cwd 不可靠时(`/` 或家目录 —— 有些客户端不从工作区
93
+ * 启动子进程)退回 ppid,至少同一台机器上的不同客户端不会撞在一起。
94
+ */
95
+ function agentScope() {
96
+ const label = agentLabel() || 'agent';
97
+ const explicit = String(process.env.AI_BROWSER_AGENT_SCOPE || '').trim();
98
+ if (explicit) return `${label}:${explicit.slice(0, 60)}`;
99
+ let cwd = '';
100
+ try { cwd = process.cwd(); } catch (_) { cwd = ''; }
101
+ const weak = !cwd || cwd === '/' || cwd === os.homedir();
102
+ const anchor = weak ? `ppid:${process.ppid}` : cwd;
103
+ return `${label}:${createHash('sha1').update(anchor).digest('hex').slice(0, 10)}`;
104
+ }
105
+
56
106
  /** 从环境推断是哪个客户端在驱动我。猜不出来就返回空串(服务端会编 "Agent N")。 */
57
107
  function agentLabel() {
58
108
  const env = process.env;
@@ -349,7 +349,7 @@ export const MCP_TOOLS = [
349
349
  {
350
350
  name: 'browse_repaint',
351
351
  title: 'Force repaint',
352
- description: "Force the compositor to redraw a tab's BrowserView when the window shows a stale page; returns repaint_count.",
352
+ description: "Force the compositor to redraw a tab's view when the window shows a stale page; returns repaint_count.",
353
353
  annotations: { readOnlyHint: false, destructiveHint: false, openWorldHint: false },
354
354
  inputSchema: {
355
355
  type: 'object',