@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.
@@ -55,13 +55,14 @@ const serverStartedAt = Math.floor(Date.now() / 1000);
55
55
  const TABLESS_METHODS = new Set([
56
56
  'ui.new_tab', 'ui.list_tabs', 'ui.close_tab', 'ui.server_info', 'ui.errors',
57
57
  'ui.window', 'ui.window_bounds', 'ui.window_shown', 'ui.quit',
58
- 'ui.subscribe', 'ui.unsubscribe',
58
+ 'ui.subscribe', 'ui.unsubscribe', 'ui.groups_debug',
59
59
  ]);
60
60
  // 这些方法本来就在"管理标签页",不能先替调用方开一个工作页再执行。
61
61
  const TAB_OWN_METHODS = new Set([
62
62
  'ui.new_tab', 'ui.list_tabs', 'ui.close_tab', 'ui.set_active_tab',
63
63
  'ui.server_info', 'ui.errors', 'ui.window', 'ui.window_bounds',
64
64
  'ui.window_shown', 'ui.quit', 'ui.subscribe', 'ui.unsubscribe', 'ui.hello',
65
+ 'ui.groups_debug',
65
66
  ]);
66
67
 
67
68
  export function startWSServer(pageManager, port = config.wsPort, onQuit = null, opts = {}) {
@@ -155,6 +156,12 @@ export function startWSServer(pageManager, port = config.wsPort, onQuit = null,
155
156
  // 连接的身份与工作区。isAgentConn 只有 ui.hello 才会置真 —— 裸 WS(e2e、
156
157
  // 脚本、调试)保持老行为:不传 tab 就打当前页。
157
158
  let clientName = null;
159
+ // 组的归属键(agent 进程自报,重连不变)。没发 ui.hello 的连接为 null,
160
+ // 各处退回 sessionId —— 等价于老行为。
161
+ let groupKey = null;
162
+ // 组的**身份**(agent 会话级:客户端名 + 工作目录)。进程换了但身份没换,
163
+ // 就靠它把上一次的组(连同页)接回来。见 page_manager.ensureGroup。
164
+ let groupScope = null;
158
165
  let isAgentConn = false;
159
166
  // 声明自己是 agent 的连接默认获得自动工作区;CLI 这类"人就坐在终端前"的
160
167
  // 入口显式关掉(auto_workspace:false),它想打当前页就是打当前页。
@@ -169,9 +176,25 @@ export function startWSServer(pageManager, port = config.wsPort, onQuit = null,
169
176
  isAgentConn = true;
170
177
  if (params.auto_workspace === false) autoWorkspace = false;
171
178
  if (process.env.AI_BROWSER_AUTO_WORKSPACE === '0') autoWorkspace = false;
172
- // capped:true —— 声明自己是 agent 才受"每组上限"约束(见 canOpenInGroup)。
179
+ // 组的归属键:优先用客户端自报的稳定 key(MCP 进程级,重连不变),
180
+ // 没有就退回连接 id —— 裸 WS / CLI 等价于老行为(一次连接一组)。
181
+ //
182
+ // 为什么不能用连接 id 当归属(2026-11-29):mcp_ws 会自动重连,浏览器
183
+ // 重启一次、网络抖一下就是一个新连接、一个新组,旧组的页留在标签栏上,
184
+ // 于是"一个 agent 一组"变成了"一次重连一组"。
185
+ groupKey = String(params.key || '').trim().slice(0, 48) || sessionId;
186
+ // 身份:进程死了它也还在(客户端名 + 工作目录的哈希,MCP 侧算好带过来)。
187
+ // 没有它就是老行为 —— 一个 key 一组,不会串组。
188
+ groupScope = String(params.scope || '').trim().slice(0, 80) || null;
189
+ // capped:声明自己是 agent 才受"每组上限"约束(见 canOpenInGroup)。
190
+ // CLI 显式发 capped:false —— 它所有命令共用一组("Terminal"),
191
+ // 要是也受 8 页上限,一个脚本批量开页就会被上限拦住。
173
192
  if (typeof pageManager.ensureGroup === 'function') {
174
- pageManager.ensureGroup(sessionId, clientName, { capped: true });
193
+ pageManager.ensureGroup(groupKey, clientName, {
194
+ capped: params.capped !== false,
195
+ sessionId,
196
+ scope: groupScope,
197
+ });
175
198
  }
176
199
  return { ok: true, agent: clientName || true, auto_workspace: autoWorkspace };
177
200
  };
@@ -295,7 +318,7 @@ export function startWSServer(pageManager, port = config.wsPort, onQuit = null,
295
318
  pageManager.sessionTabOf(sessionId) === null) {
296
319
  try {
297
320
  const g = typeof pageManager.ensureGroup === 'function'
298
- ? pageManager.ensureGroup(sessionId, clientName) : null;
321
+ ? pageManager.ensureGroup(groupKey || sessionId, clientName, { sessionId, scope: groupScope }) : null;
299
322
  const wid = pageManager.newTab(null, { show: false, group: g ? g.id : undefined });
300
323
  pageManager.bindSessionTab(sessionId, wid);
301
324
  tabId = wid;
@@ -440,7 +463,7 @@ export function startWSServer(pageManager, port = config.wsPort, onQuit = null,
440
463
  break;
441
464
  }
442
465
  case 'ui.repaint': {
443
- // Force the compositor to redraw a tab's BrowserView. Needed because
466
+ // Force the compositor to redraw a tab's view. Needed because
444
467
  // "the window shows a stale page" has NO observable state to detect:
445
468
  // the DOM tree / evaluate already report the NEW page while the
446
469
  // screen shows the OLD one. So the agent's only handle on it is an
@@ -569,7 +592,7 @@ export function startWSServer(pageManager, port = config.wsPort, onQuit = null,
569
592
  // 归属:这一页算谁的。agent 开的页进它自己的组(上限按组算),
570
593
  // 人点「+」开的页进 human 组(没有上限)。
571
594
  const g = typeof pageManager.ensureGroup === 'function'
572
- ? pageManager.ensureGroup(sessionId, clientName) : null;
595
+ ? pageManager.ensureGroup(groupKey || sessionId, clientName, { sessionId, scope: groupScope }) : null;
573
596
  const groupId = g ? g.id : undefined;
574
597
  // 上限:**拒绝**而不是偷偷挤掉一页。挤掉是破坏性的(agent 可能正
575
598
  // 在那一页上跑流程),而拒绝是一次必须做的决定 —— 这正是上限存在
@@ -647,7 +670,11 @@ export function startWSServer(pageManager, port = config.wsPort, onQuit = null,
647
670
  id: g.id, label: g.label, count: g.count,
648
671
  // limit 只对受约束的组有意义;不限量的组别报一个假的 8
649
672
  limit: g.capped === true ? g.limit : null,
650
- ...(g.id === tabGroup.groupIdForSession(sessionId) ? { mine: true } : {}),
673
+ // mine:哪一组是**这条连接**的。不能拿 agent:<sessionId> 去比 ——
674
+ // 组可能是从上一个进程那儿收养来的,id 不是本连接的 sessionId。
675
+ ...(g.id === (typeof pageManager.groupOfSession === 'function'
676
+ ? pageManager.groupOfSession(sessionId)
677
+ : tabGroup.groupIdForSession(sessionId)) ? { mine: true } : {}),
651
678
  })),
652
679
  } : {}),
653
680
  ...(evicted.length ? { evicted } : {}),
@@ -781,6 +808,42 @@ export function startWSServer(pageManager, port = config.wsPort, onQuit = null,
781
808
  });
782
809
  break;
783
810
  }
811
+
812
+ // ui.groups_debug —— 只读诊断:每个组的内部状态(scope / 有谁在用 / 多久没动)。
813
+ //
814
+ // 为什么要有它(2026-11-29):排查"一个 agent 开了好几个组"时,光看
815
+ // list_tabs 的回执只能看到 id 和页数,看不出"这个组为什么没被收养"。
816
+ // 这几项正好决定收养能不能发生:scope 对不对得上、sessions 是不是空的、
817
+ // 闲置有没有超过保护期。和 ui.history 一样,是给排查用的,别在流程里依赖。
818
+ case 'ui.groups_debug': {
819
+ const rows = [];
820
+ for (const [gid, g] of pageManager._groups) {
821
+ rows.push({
822
+ id: gid,
823
+ label: g.label,
824
+ scope: g.scope || null,
825
+ seq: g.seq,
826
+ color: g.color,
827
+ capped: !!g.capped,
828
+ sessions: g.sessions instanceof Set ? g.sessions.size : null,
829
+ idle_ms: g.idleSince ? Date.now() - g.idleSince : null,
830
+ tabs: pageManager.tabsInGroup(gid).length,
831
+ created_at: g.createdAt,
832
+ last_used_at: g.lastUsedAt || null,
833
+ });
834
+ }
835
+ send({
836
+ jsonrpc: '2.0', id,
837
+ result: {
838
+ groups: rows,
839
+ key_to_group: Object.fromEntries(pageManager._keyToGroup || []),
840
+ idents: Object.fromEntries(
841
+ [...(pageManager._groupIdent || [])].map(([k, v]) => [k, { seq: v.seq, label: v.label, gid: v.gid, at: v.at }]),
842
+ ),
843
+ },
844
+ });
845
+ break;
846
+ }
784
847
  // === Subscribe/unsubscribe (with response) ===
785
848
  case 'ui.subscribe': {
786
849
  const events = params.events || [];
@@ -1,7 +1,7 @@
1
1
  // preload/bridge.cjs — AI Browser entry (CommonJS, loaded by Electron preload)
2
2
  // v6.9, split into modules: extractor.cjs / actions.cjs / watcher.cjs
3
3
  // Responsible only for: IPC wiring + module orchestration.
4
- // Requires local .cjs modules — the tab BrowserView MUST set sandbox:false
4
+ // Requires local .cjs modules — the tab view MUST set sandbox:false
5
5
  // (see page_manager.newTab) since a sandboxed preload can only require
6
6
  // Electron's whitelist, not arbitrary local files.
7
7
  const { contextBridge, ipcRenderer } = require("electron");
@@ -64,7 +64,7 @@
64
64
  /* ---- agent 分组 ----
65
65
  一个 agent 的一组标签页折叠成一个 pill(带组色和页数),悬停展开。
66
66
  为什么不做成"竖着掉下来的下拉菜单":标签栏是一个固定高度(74px)的
67
- BrowserView,往下弹会被自己裁掉;要能弹就得每次悬停都把标签栏长高、把页面
67
+ 独立视图,往下弹会被自己裁掉;要能弹就得每次悬停都把标签栏长高、把页面
68
68
  往下推 —— 那正是本项目历史上闪烁/几何脱节的来源(见 SPEC 的渲染三宗罪)。
69
69
  就地展开没有这个代价:一行之内把这一组的标签摊开,鼠标移开自动收起。 */
70
70
  .group-pill {
@@ -39,7 +39,7 @@ export const config = {
39
39
  // Height of the in-app tab strip below the window title bar.
40
40
  // 36px = tab row, 38px = toolbar row (address bar + back/forward/reload/stop).
41
41
  tabBarHeight: 36 + 38,
42
- // The session every tab's BrowserView uses. **Must stay a single source of
42
+ // The session every tab view uses. **Must stay a single source of
43
43
  // truth**: session-level hooks (will-download, permission requests) are
44
44
  // installed on `session.fromPartition(viewPartition)`. Hooking
45
45
  // `session.defaultSession` instead silently does nothing — the views are not
@@ -19,9 +19,9 @@
19
19
  // 补上产品已经在用的那招,再问一次;还是收不到就**不发事件**、如实报错。
20
20
  //
21
21
  // 排除掉的几根(都是当场量出来的,不是猜的):
22
- // setTopBrowserView(置顶) → visibility 照旧 hidden,事件照旧没
22
+ // 置顶(当时是 setTopBrowserView) → visibility 照旧 hidden,事件照旧没
23
23
  // webContents.focus()(原生焦点) → 同上
24
- // removeBrowserView + addBrowserView → 同上
24
+ // 摘掉再挂回(当时是 remove+addBrowserView)→ 同上
25
25
  // setBounds(重排)/ setBackgroundThrottling(false) / invalidate → 同上
26
26
  // Page.setWebLifecycleState{active} → 命令存在,但 visibility 不变
27
27
  // Emulation.setPageVisibilityOverride → Chromium 130 没有这条命令('wasn't found')
@@ -19,7 +19,7 @@
19
19
  // · 尺寸下限 100(Chrome 不允许弹窗小于此),上限就是我们能给的内容区。
20
20
  // · left/top 缺省居中(Chrome 用级联偏移;我们只有一个窗口,居中更稳定也可复现)。
21
21
  //
22
- // 坐标约定:这里算出来的是 **BrowserView 的 content 坐标**(y 已经含标签栏高度),
22
+ // 坐标约定:这里算出来的是 **视图的 content 坐标**(y 已经含标签栏高度),
23
23
  // 调用方给 box = 内容区矩形,返回值可以直接交给 view.setBounds()。
24
24
  // 夹取是幂等的:窗口缩放时对旧矩形再夹一次,只可能挪位置/缩尺寸,不会漂。
25
25
 
@@ -189,7 +189,7 @@ function joinShotFailures(failures) {
189
189
  /**
190
190
  * surface 档的尺寸回执:capturePage 给的是**设备像素**,而调用方按 CSS 像素
191
191
  * 传坐标、也按 CSS 像素读回执(CDP 档的 meta 就是 CSS 像素)。视图的 bounds
192
- * 恰等于这一页的视口(BrowserView 的边界就是它的可视区),所以:
192
+ * 恰等于这一页的视口(视图边界就是它的可视区),所以:
193
193
  * CSS 尺寸 = 视图 bounds,dpr = 位图宽 / bounds 宽。
194
194
  * 这样主线程被占死、连 devicePixelRatio 都 evaluate 不出来的页面,
195
195
  * 回执仍然带得齐 width/height/device_pixel_ratio —— 不必为了这两个数再去排队。
@@ -80,6 +80,9 @@ const NOT_PER_TAB = {
80
80
  _wsClients: '键是 sessionId,由 unregisterClient 清(连接生命周期)',
81
81
  _sessionTab: '键是 sessionId,由 unregisterClient 清(连接生命周期)',
82
82
  _groups: '键是 groupId,组空时由 _forgetTab 删;删 tab 不等于删组',
83
+ _sessionGroup: '键是 sessionId,由 unregisterClient 销号时清(连接生命周期,不随 tab 关闭)',
84
+ _keyToGroup: '键是 agent 的 groupKey(进程级身份),值是 groupId;由 _pruneKeyMap 清掉指向已删组的条目,与单个 tab 无关',
85
+ _groupIdent: '键是 scope(agent 身份)或 groupId,值是编号/颜色;专为"组被回收后再次出现"保留,按条数上限淘汰,绝不能随 tab 关闭清',
83
86
  _windowOpenTokens: '值是 {tabId},closeTab 里手写遍历清(结构不是直接 tabId)',
84
87
  _snapshots: '值是 {tab, ...},_cleanupTabResources 里手写遍历清(结构不是直接 tabId)',
85
88
  _networkRequestMap: '值是 {tabId, ...},_cleanupTabResources 里手写遍历清',
@@ -2,4 +2,4 @@
2
2
  // Kept in JS (not imported from package.json) to avoid Node import-attribute
3
3
  // (JSON module) compatibility issues inside Electron. package.json "version"
4
4
  // is manually synced to this value; tests assert they match.
5
- export const VERSION = '2.5.10';
5
+ export const VERSION = '2.6.1';
@@ -1,4 +1,4 @@
1
- // shared/view_liveness.cjs — "这个 BrowserView 现在还能不能碰"的唯一判定口径。
1
+ // shared/view_liveness.cjs — "这个视图现在还能不能碰"的唯一判定口径。
2
2
  //
3
3
  // 事故(2026-10-30,用户报"又白屏了",截图:标题栏正常、内容全白、底部一条深红横幅、
4
4
  // 左侧露一小块橙棕):主进程在 22 次 uncaught_exception 里反复抛
@@ -6,7 +6,7 @@
6
6
  // at pushNavState (index.js) ← setInterval(pushNavState, 1500)
7
7
  //
8
8
  // 根因是 Electron 的一个反直觉行为(用真 Electron 探针实测,见 git 提交说明):
9
- // - webContents.close() / destroy() 之后,BrowserView 对象**还在**,但
9
+ // - webContents.close() / destroy() 之后,视图对象**还在**,但
10
10
  // view.webContents 变成 undefined —— 此时 `view.webContents.isDestroyed()`
11
11
  // 直接抛 TypeError。
12
12
  // - 渲染进程崩溃(render-process-gone)之后,view.webContents**还在**,
@@ -18,7 +18,7 @@
18
18
  // 崩溃 → uncaughtException → reportFatal → showErrorBanner → layoutWithBanner
19
19
  // → 又碰到悬空视图 → 二次崩溃 → 进程半死(端口还在 listen 但 WS 握手完不成)。
20
20
  //
21
- // 这份模块只做一件事:给一个"可能是 BrowserView"的对象,回答
21
+ // 这份模块只做一件事:给一个"可能是视图"的对象,回答
22
22
  // "它现在有没有一个活着、可安全调用的 webContents?"
23
23
  // 判定与"怎么救"分开 —— 救(重建/reload/收横幅)有副作用,属于调用方职责,
24
24
  // 且必须有上限,不能在纯函数里做。
@@ -38,7 +38,7 @@
38
38
  * 3. webContents.isDestroyed() === true → false
39
39
  * 4. 读取 webContents / 调 isDestroyed 抛异常 → false(宁可判死,绝不让异常冒出去)
40
40
  *
41
- * @param {object} view 可能是 BrowserView 的东西
41
+ * @param {object} view 可能是视图的东西
42
42
  * @returns {boolean}
43
43
  */
44
44
  function isAlive(view) {
@@ -55,7 +55,7 @@ function isAlive(view) {
55
55
 
56
56
  /**
57
57
  * 失效的"原因",供日志/回执区分"悬空"与"已销毁",避免把两种形态混为一谈
58
- * (它们的救法不同:悬空必须重建 BrowserView,已销毁可考虑 reload)。
58
+ * (它们的救法不同:悬空必须重建视图,已销毁可考虑 reload)。
59
59
  *
60
60
  * @param {object} view
61
61
  * @returns {'ok'|'no-view'|'no-webcontents'|'destroyed'|'unknown'}
@@ -85,7 +85,7 @@ function liveness(view) {
85
85
  * (refreshTabBar 2s / pushNavState 1.5s)一直往一个死框架里撞,而
86
86
  * `ensureTabBarAlive` 因为 isAlive 为真,从来没有重建过标签栏。
87
87
  *
88
- * @param {object} view 可能是 BrowserView 的东西
88
+ * @param {object} view 可能是视图的东西
89
89
  * @returns {boolean} 主框架是否还能接收脚本
90
90
  */
91
91
  function frameAlive(view) {