@glittering0717/ai-browser 2.4.4 → 2.4.6
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 +3 -1
- package/examples/AGENT_INSTRUCTIONS.md +4 -2
- package/llms-install.md +8 -5
- package/package.json +1 -1
- package/skills/ai-browser/SKILL.md +1 -1
- package/skills/ai-browser/references/tabs.md +16 -0
- package/src/main/canvas_monitor.js +19 -0
- package/src/main/mcp_server.js +9 -1
- package/src/main/page_manager.js +22 -24
- package/src/main/ws_server.js +83 -28
- package/src/shared/tab_state.cjs +115 -0
- package/src/shared/version.js +1 -1
package/README.md
CHANGED
|
@@ -461,7 +461,9 @@ covers the things an agent genuinely cannot do:
|
|
|
461
461
|
side, and nothing needs to. It is a conversation between the agent and its user:
|
|
462
462
|
|
|
463
463
|
1. The agent hits something it cannot do.
|
|
464
|
-
2. It says so in
|
|
464
|
+
2. It says so in its own reply, as plain text — *"Please scan the QR code in the browser window, then
|
|
465
|
+
reply 'done'."* No tool call is involved: there is no `chat` / `message` / `ask_user` tool in this
|
|
466
|
+
project, and no "spawn a side conversation" feature. Talking to the user is the agent's own job.
|
|
465
467
|
3. It stops and waits for the reply — no retry loops on clicks that cannot work.
|
|
466
468
|
4. After "done" it re-reads with `browse_get_tree` to confirm the new state, then continues.
|
|
467
469
|
|
|
@@ -38,8 +38,10 @@ success:true 只代表动作发出去了,不代表页面变了。要证明生
|
|
|
38
38
|
canvas / 富文本),换动作不是换选择器;点了没反应 → browse_screenshot 看视觉 或
|
|
39
39
|
browse_network 看请求;怀疑掉登录 → browse_auth_state;都不是 → browse_errors。
|
|
40
40
|
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
需要人(登录/扫码/滑块/支付):**在你这轮回复里直接说一句**,请用户在窗口里操作,然后结束这轮等
|
|
42
|
+
回话;收到"好了"再 browse_get_tree 确认。这一步不需要任何工具——本项目没有 chat / message /
|
|
43
|
+
ask_user 这类工具,也没有"发起一次性会话",别去调宿主的临时对话能力。不要编造凭据,不要重试
|
|
44
|
+
注定失败的点击。窗口是真的,人随时能抢鼠标。
|
|
43
45
|
|
|
44
46
|
收尾:browse_quit 关掉窗口和进程(人正在操作时不要调)。
|
|
45
47
|
```
|
package/llms-install.md
CHANGED
|
@@ -201,11 +201,14 @@ notifications. Use `browse_network` for pull-based inspection.
|
|
|
201
201
|
## Human hand-off
|
|
202
202
|
|
|
203
203
|
When blocked by login, SSO, QR code, slider/image CAPTCHA, SMS/email code, or
|
|
204
|
-
payment confirmation: stop
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
204
|
+
payment confirmation: stop and ask the user **in your own reply** (plain text —
|
|
205
|
+
there is no `chat`/`message` tool here, and no side-conversation feature) to do
|
|
206
|
+
it in the visible browser window and reply "done", wait for that reply, then
|
|
207
|
+
re-read with `browse_get_tree` and continue. Never fabricate credentials or retry
|
|
208
|
+
clicks that cannot succeed. Logins persist in the `persist:ai-browser` session
|
|
209
|
+
partition (on disk under `~/.ai-browser`), so intervention is usually needed once
|
|
210
|
+
per site. While the user is typing or scanning, do **not** call `browse_quit` —
|
|
211
|
+
it closes the window and takes any half-finished work with it.
|
|
209
212
|
|
|
210
213
|
## Troubleshooting (common cases)
|
|
211
214
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@glittering0717/ai-browser",
|
|
3
|
-
"version": "2.4.
|
|
3
|
+
"version": "2.4.6",
|
|
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": {
|
|
@@ -55,6 +55,22 @@ browse_tabs {"op":"switch","tab":7,"show":true}
|
|
|
55
55
|
用户点开你那一页只是"看",**不会**抢走你的目标;你继续在上面操作,他看到的是
|
|
56
56
|
实时画面。要避免"我正要输入他却盯着看"的尴尬,就提前说一句。
|
|
57
57
|
|
|
58
|
+
### 怎么"说一句"——不需要任何工具
|
|
59
|
+
|
|
60
|
+
**就在你这轮回复里写一句人话,然后结束这一轮、停下等回话。** 例如:
|
|
61
|
+
|
|
62
|
+
> 请在浏览器窗口里扫这个码,好了回我一句"好了"。
|
|
63
|
+
|
|
64
|
+
收到"好了"之后再用 `browse_get_tree` 确认状态变了,才继续 —— 别把"用户说好了"
|
|
65
|
+
当成"页面真的变了"。
|
|
66
|
+
|
|
67
|
+
- 本项目**没有** `chat` / `message` / `ask_user` 这类工具,也**没有**"发起一次性会话"
|
|
68
|
+
的功能。去调宿主的临时对话/子对话不会让人去扫码,只会白绕一轮 —— 它是不是用完
|
|
69
|
+
自动关闭,跟你这一轮任务没有任何关系,不用管。
|
|
70
|
+
- 交接发生在 agent 的对话里,**浏览器侧零改动**:窗口一直开着,人随时能抢鼠标。
|
|
71
|
+
- **人正在操作时不要 `browse_quit`**:它连窗口一起带走,做到一半的全没了。
|
|
72
|
+
- 别编造密码/验证码,也别重试注定失败的点击(滑块、风控拦截页)。
|
|
73
|
+
|
|
58
74
|
## 关于"后台"
|
|
59
75
|
|
|
60
76
|
```json
|
|
@@ -131,6 +131,11 @@ export class CanvasMonitor {
|
|
|
131
131
|
return { accepted, dropped };
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
+
/**
|
|
135
|
+
* 只丢抓取到的**数据**,保留该 tab 的配置(mode / hook / warnings)。
|
|
136
|
+
* `canvas_configure{clear:true}` 走这里 —— 它要的是"从零开始记",
|
|
137
|
+
* 不是"顺手把我刚设的 mode 也忘掉"。
|
|
138
|
+
*/
|
|
134
139
|
clear(tabId) {
|
|
135
140
|
const st = this.tabs.get(tabId);
|
|
136
141
|
if (!st) return { cleared_calls: 0 };
|
|
@@ -142,6 +147,20 @@ export class CanvasMonitor {
|
|
|
142
147
|
return { cleared_calls: n };
|
|
143
148
|
}
|
|
144
149
|
|
|
150
|
+
/**
|
|
151
|
+
* 彻底忘掉一个 tab(关 tab 时调用)。与 clear 的区别:
|
|
152
|
+
* 连 mode / hookInstalled / warnings 一起丢,并把 tabs 里的条目**删掉**。
|
|
153
|
+
*
|
|
154
|
+
* 为什么必须删条目:这个 Map 没有任何淘汰上限,而此前关 tab 只清数据、
|
|
155
|
+
* 留一个空 state 对象 —— 每关一页就多一条,长会话下无界累积(TODO 里
|
|
156
|
+
* "关掉的 tab 保留 canvas 抓取状态"指的就是这里)。之后 list/read 会按需重建,
|
|
157
|
+
* 行为不变。
|
|
158
|
+
*/
|
|
159
|
+
forget(tabId) {
|
|
160
|
+
const had = this.tabs.delete(tabId);
|
|
161
|
+
return { forgotten: !!had };
|
|
162
|
+
}
|
|
163
|
+
|
|
145
164
|
/** 列出该 tab 的 canvas。 */
|
|
146
165
|
list(tabId) {
|
|
147
166
|
const st = this._tab(tabId);
|
package/src/main/mcp_server.js
CHANGED
|
@@ -148,7 +148,15 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
|
148
148
|
// 都会实际调用一次工具,不透明的 internal error 会被当成"坏了"。
|
|
149
149
|
return { content: [{ type: 'text', text: `Error: ${guardErr}` }], isError: true };
|
|
150
150
|
}
|
|
151
|
-
|
|
151
|
+
// frame_ref 必须转发:旧版这里只传 js/tab,schema 却承诺了"显式指定运行帧",
|
|
152
|
+
// 于是 agent 传了也照样在顶层帧跑、对 iframe 内元素返回 null —— 不报错、
|
|
153
|
+
// 只是拿不到(同类事故第 5 次,见 docs/AUDIT-2026-10-30.md P1-1)。
|
|
154
|
+
// tests/test_mcp_forwarding.js 用表驱动钉住"声明即转发"。
|
|
155
|
+
const result = await wsClient.call('ui.evaluate', {
|
|
156
|
+
js,
|
|
157
|
+
tab: args.tab,
|
|
158
|
+
frame_ref: args.frame_ref || undefined,
|
|
159
|
+
});
|
|
152
160
|
return { content: [{ type: 'text', text: JSON.stringify(result) }] };
|
|
153
161
|
}
|
|
154
162
|
|
package/src/main/page_manager.js
CHANGED
|
@@ -38,6 +38,12 @@ import blankPage from '../shared/blank_page.cjs';
|
|
|
38
38
|
// .cjs 用默认导入(全项目约定,且 tests/test_imports.js 的静态分析只认 ESM export)。
|
|
39
39
|
import viewLiveness from '../shared/view_liveness.cjs';
|
|
40
40
|
const { isAlive, liveness } = viewLiveness;
|
|
41
|
+
// "关掉一个 tab 时哪些 per-tab 容器必须跟着消失"的唯一清单(纯函数)。
|
|
42
|
+
// 为什么是表而不是逐行 delete:这类清理历史上漏过两轮(Bug 5 补了 4 个字段,
|
|
43
|
+
// 2026-10-30 评估又查出 3 个漏网)。清单数据化 + 完整性闸门单测之后,
|
|
44
|
+
// "新增一个 per-tab Map 忘了清"会变成构建失败而不是半年后的一次泄漏。
|
|
45
|
+
// 见 shared/tab_state.cjs 顶部与 docs/AUDIT-2026-10-30.md P1-2。
|
|
46
|
+
import tabState from '../shared/tab_state.cjs';
|
|
41
47
|
import { diffValues, previewOf, safeByteLength, DEFAULT_SNAPSHOT_JS, DIFF_MAX_ENTRIES, evictOldest } from './snapshot_diff.js';
|
|
42
48
|
import { withDeadline, actionDeadlineMs } from '../shared/deadline.js';
|
|
43
49
|
import { DownloadMonitor } from './download_monitor.js';
|
|
@@ -4725,16 +4731,12 @@ return {ok:true,name:file.name,size:file.size,via:via,host_sel:hostSel,clicked:c
|
|
|
4725
4731
|
for (const [token, entry] of this._windowOpenTokens) {
|
|
4726
4732
|
if (entry.tabId === tabId) this._windowOpenTokens.delete(token);
|
|
4727
4733
|
}
|
|
4728
|
-
|
|
4729
|
-
|
|
4730
|
-
|
|
4731
|
-
//
|
|
4732
|
-
this._lastDialog.delete(tabId);
|
|
4733
|
-
this._openDialogs.delete(tabId);
|
|
4734
|
-
this._dialogWatched.delete(tabId);
|
|
4735
|
-
// Release per-tab network resources so a closed tab can never leak its
|
|
4736
|
-
// debugger attachment or cached request entries / stale monitors.
|
|
4734
|
+
// per-tab 纯容器的清理由 shared/tab_state.cjs 的 PER_TAB 表统一执行
|
|
4735
|
+
// (唯一清单)。这里不再逐行 delete:两处清单一定会分叉 —— Bug 5 与
|
|
4736
|
+
// 2026-10-30 评估各漏一次,就是这么漏的。有副作用的步骤(detach 调试器、
|
|
4737
|
+
// 释放抓包内存、canvas 账本)也在同一个函数里,但**晚于**纯删除。
|
|
4737
4738
|
this._cleanupTabResources(tabId);
|
|
4739
|
+
|
|
4738
4740
|
if (this.activeTab === tabId || this.activeTab === null) {
|
|
4739
4741
|
// switch to first remaining tab — must re-addBrowserView
|
|
4740
4742
|
const first = this.tabs.keys().next();
|
|
@@ -4758,6 +4760,13 @@ return {ok:true,name:file.name,size:file.size,via:via,host_sel:hostSel,clicked:c
|
|
|
4758
4760
|
// unaffected — each tab owns its own webContents debugger)
|
|
4759
4761
|
// - drop cached request entries made on that tab
|
|
4760
4762
|
_cleanupTabResources(tabId) {
|
|
4763
|
+
// 第一件事:把纯数据容器全部清掉。
|
|
4764
|
+
// 放在最前面是因为它是**纯删除、不会抛**,而后面几步都有副作用(detach 调试器、
|
|
4765
|
+
// 释放抓包内存)——万一哪一步抛了,至少 tabId 不会带着上一任的状态被复用。
|
|
4766
|
+
// 清单只有一份:shared/tab_state.cjs 的 PER_TAB(新增 per-tab 容器必须登记,
|
|
4767
|
+
// 否则 tests/test_tab_state_reset.js 的完整性闸门会红)。
|
|
4768
|
+
tabState.clearPerTab(this, tabId);
|
|
4769
|
+
|
|
4761
4770
|
const entry = this._cdpTabs.get(tabId);
|
|
4762
4771
|
if (entry) {
|
|
4763
4772
|
try { entry.wc.debugger.detach(); } catch(e) {}
|
|
@@ -4780,21 +4789,10 @@ return {ok:true,name:file.name,size:file.size,via:via,host_sel:hostSel,clicked:c
|
|
|
4780
4789
|
// tabId 会被复用,媒体文档记录跟着一起清 —— 否则新 tab 一打开就带着
|
|
4781
4790
|
// 上一个 tab 的「下载」按钮状态(地址对不上时 recordOf 会兜住,但别依赖兜底)。
|
|
4782
4791
|
if (this.mediaInline) this.mediaInline.forget(tabId);
|
|
4783
|
-
//
|
|
4784
|
-
//
|
|
4785
|
-
//
|
|
4786
|
-
|
|
4787
|
-
this._axDiffCache.delete(tabId);
|
|
4788
|
-
if (this._failureGuard) this._failureGuard.delete(tabId);
|
|
4789
|
-
this.canvasMonitor.clear(tabId);
|
|
4790
|
-
// 白屏自检 / 崩溃自救的 per-tab 状态。不清的话 tabId 会被新 tab 复用,
|
|
4791
|
-
// 新 tab 一开就继承上一任的"已经救过两步",白屏时直接被判定成 give-up。
|
|
4792
|
-
this._paintState.delete(tabId);
|
|
4793
|
-
this._paintSteps.delete(tabId);
|
|
4794
|
-
this._paintSelfReload.delete(tabId);
|
|
4795
|
-
this._crashReloadAt.delete(tabId);
|
|
4796
|
-
const pt = this._paintTimers.get(tabId);
|
|
4797
|
-
if (pt) { clearTimeout(pt); this._paintTimers.delete(tabId); }
|
|
4792
|
+
// canvas 抓取状态有自己的配额账本与 hook 句柄,需要连带释放;用 forget 而不是
|
|
4793
|
+
// clear —— clear 是"重记"(canvas_configure{clear:true} 用),会保留 mode/hook,
|
|
4794
|
+
// 关 tab 要的是连那个空壳一起删,否则 tabs Map 无界累积(没有淘汰上限)。
|
|
4795
|
+
this.canvasMonitor.forget(tabId);
|
|
4798
4796
|
}
|
|
4799
4797
|
|
|
4800
4798
|
/** 白屏相关的"值得报出去的结论";正常页面返回 null(listTabs 不带这个字段)。 */
|
package/src/main/ws_server.js
CHANGED
|
@@ -722,14 +722,32 @@ export function startWSServer(pageManager, port = config.wsPort, onQuit = null)
|
|
|
722
722
|
// === ui.wait — poll until condition or timeout ===
|
|
723
723
|
case 'ui.wait': {
|
|
724
724
|
// params: { condition: 'button_enabled'|'modal_appeared'|'text_contains', target, text, timeout_ms }
|
|
725
|
-
|
|
725
|
+
//
|
|
726
|
+
// timeout_ms 必须钳制:调用方写 600000 会让这条 RPC 和一次 500ms 一轮的
|
|
727
|
+
// 树抽取循环一直跑着,而 agent 那边通常早就超时放弃了。
|
|
728
|
+
const timeoutMs = Math.max(100, Math.min(120000, Number(params.timeout_ms) || 10000));
|
|
726
729
|
const pollMs = 500;
|
|
727
730
|
const startTime = Date.now();
|
|
728
731
|
let satisfied = false;
|
|
732
|
+
let attempts = 0;
|
|
733
|
+
// 最后一次"这一轮没读成"的原因。仅仅不满足**不是**错误,但读失败
|
|
734
|
+
// 必须如实报出来 —— 否则调用方分不清"条件没出现"和"我一直在读空"。
|
|
735
|
+
let lastError = null;
|
|
729
736
|
|
|
730
737
|
const poll = async () => {
|
|
731
738
|
while (Date.now() - startTime < timeoutMs) {
|
|
732
|
-
|
|
739
|
+
attempts++;
|
|
740
|
+
// ⚠️ 单次抽取失败不是"等待失败"。导航之后的几百毫秒里文档正在换,
|
|
741
|
+
// preload 的 ai:extract 会因为旧文档已拆而超时(getTree 硬 5s 后
|
|
742
|
+
// reject)。此前这里没有 try/catch → `browse_wait({url_contains})`
|
|
743
|
+
// 这个最典型的用法直接变成 -32603 internal error,而调用方看到
|
|
744
|
+
// "内部错误"的合理反应是**重试导航**,把已经成功的导航重做一遍。
|
|
745
|
+
let result = null;
|
|
746
|
+
try {
|
|
747
|
+
result = await pageManager.getTree(false, tabId);
|
|
748
|
+
} catch (e) {
|
|
749
|
+
lastError = String((e && e.message) || e);
|
|
750
|
+
}
|
|
733
751
|
const tree = result?.tree;
|
|
734
752
|
const ctx = result?.context;
|
|
735
753
|
|
|
@@ -744,12 +762,20 @@ export function startWSServer(pageManager, port = config.wsPort, onQuit = null)
|
|
|
744
762
|
if (ctx?.modals && ctx.modals.length > 0) { satisfied = true; break; }
|
|
745
763
|
}
|
|
746
764
|
if (params.condition === 'text_contains' && params.text) {
|
|
747
|
-
|
|
748
|
-
|
|
765
|
+
try {
|
|
766
|
+
const bodyText = await pageManager.evaluate('document.body.innerText', tabId);
|
|
767
|
+
if (bodyText && bodyText.indexOf(params.text) >= 0) { satisfied = true; break; }
|
|
768
|
+
} catch (e) {
|
|
769
|
+
lastError = String((e && e.message) || e);
|
|
770
|
+
}
|
|
749
771
|
}
|
|
750
772
|
if (params.condition === 'url_contains' && params.text) {
|
|
751
|
-
|
|
752
|
-
|
|
773
|
+
try {
|
|
774
|
+
const url = await pageManager.evaluate('location.href', tabId);
|
|
775
|
+
if (url && url.indexOf(params.text) >= 0) { satisfied = true; break; }
|
|
776
|
+
} catch (e) {
|
|
777
|
+
lastError = String((e && e.message) || e);
|
|
778
|
+
}
|
|
753
779
|
}
|
|
754
780
|
|
|
755
781
|
await new Promise(r => setTimeout(r, pollMs));
|
|
@@ -757,7 +783,17 @@ export function startWSServer(pageManager, port = config.wsPort, onQuit = null)
|
|
|
757
783
|
};
|
|
758
784
|
|
|
759
785
|
await poll();
|
|
760
|
-
send({
|
|
786
|
+
send({
|
|
787
|
+
jsonrpc: '2.0', id,
|
|
788
|
+
result: {
|
|
789
|
+
satisfied,
|
|
790
|
+
elapsed_ms: Date.now() - startTime,
|
|
791
|
+
// 抽了几轮。0 轮 = 一次都没轮到(超时被钳到极小或页面立即失效),
|
|
792
|
+
// 这个数字让"等了两秒但只读了 3 次"这种事可诊断。
|
|
793
|
+
attempts,
|
|
794
|
+
...(satisfied || !lastError ? {} : { last_error: lastError }),
|
|
795
|
+
},
|
|
796
|
+
});
|
|
761
797
|
break;
|
|
762
798
|
}
|
|
763
799
|
// === ui.scroll — scroll page or element ===
|
|
@@ -778,8 +814,19 @@ export function startWSServer(pageManager, port = config.wsPort, onQuit = null)
|
|
|
778
814
|
window.scrollBy(0, d === 'down' ? a : -a);
|
|
779
815
|
return { ok: true };
|
|
780
816
|
})(${JSON.stringify(target)}, ${JSON.stringify(direction)}, ${amount})`;
|
|
781
|
-
const result = await pageManager.evaluate(js, tabId).catch(() => ({
|
|
782
|
-
|
|
817
|
+
const result = await pageManager.evaluate(js, tabId).catch((e) => ({
|
|
818
|
+
ok: false,
|
|
819
|
+
error: String((e && e.message) || e),
|
|
820
|
+
}));
|
|
821
|
+
// 以前这里写 `result || { ok: true }`:evaluate 返回 undefined 时也报成功。
|
|
822
|
+
// 「命令发出去了」不等于「页面动了」—— 这个项目为这条判据付过代价
|
|
823
|
+
// (不变量 5:任何写入必须 操作→回读→diff)。没有回执就如实说没有。
|
|
824
|
+
send({
|
|
825
|
+
jsonrpc: '2.0', id,
|
|
826
|
+
result: result && typeof result === 'object'
|
|
827
|
+
? result
|
|
828
|
+
: { ok: false, error: 'scroll produced no result from the page' },
|
|
829
|
+
});
|
|
783
830
|
break;
|
|
784
831
|
}
|
|
785
832
|
// === 网络抓包(可查询的 Network 面板)===
|
|
@@ -802,22 +849,21 @@ export function startWSServer(pageManager, port = config.wsPort, onQuit = null)
|
|
|
802
849
|
break;
|
|
803
850
|
}
|
|
804
851
|
case 'ui.network_get': {
|
|
805
|
-
try
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
}
|
|
852
|
+
// 这里**不**自带 try/catch:以前自带一份,catch 里 send 完继续往下走,
|
|
853
|
+
// 最后落到统一的 noteCall(..., true) —— 于是"body 被淘汰"这类失败会被
|
|
854
|
+
// 记成一次**成功**调用,错误日志里"崩溃前最近调用"就成了假证据。
|
|
855
|
+
// 现在只有一个错误出口(外层 catch),它认 e.rpcCode 并回精确错误码。
|
|
856
|
+
const result = await pageManager.networkGet(String(params.network_id || ''), {
|
|
857
|
+
include_request_headers: params.include_request_headers === true,
|
|
858
|
+
include_request_body: params.include_request_body === true,
|
|
859
|
+
include_response_headers: params.include_response_headers === true,
|
|
860
|
+
include_response_body: params.include_response_body === true,
|
|
861
|
+
include_sensitive_headers: params.include_sensitive_headers === true,
|
|
862
|
+
request_body_offset: params.request_body_offset,
|
|
863
|
+
response_body_offset: params.response_body_offset,
|
|
864
|
+
body_limit: params.body_limit,
|
|
865
|
+
});
|
|
866
|
+
send({ jsonrpc: '2.0', id, result });
|
|
821
867
|
break;
|
|
822
868
|
}
|
|
823
869
|
case 'ui.network_clear': {
|
|
@@ -892,17 +938,26 @@ export function startWSServer(pageManager, port = config.wsPort, onQuit = null)
|
|
|
892
938
|
// 而不是给每次点击都记一行日志。
|
|
893
939
|
noteCall(method, params, Date.now() - t0, true);
|
|
894
940
|
} catch (e) {
|
|
895
|
-
|
|
941
|
+
const errMsg = (e && e.message) || String(e);
|
|
942
|
+
noteCall(method, params, Date.now() - t0, 'error: ' + errMsg);
|
|
943
|
+
// 带 rpcCode 的是**可预期的业务失败**(记录不存在 / body 已被淘汰 /
|
|
944
|
+
// 敏感头未开):回精确错误码,但**不落盘** —— 它不是"主进程出问题了",
|
|
945
|
+
// 落盘只会把真正的 uncaught/崩溃淹掉。此前 ui.network_get 在自己的
|
|
946
|
+
// catch 里这么做,代价是错误调用被记成了成功(见 AUDIT P2-1)。
|
|
947
|
+
if (e && e.rpcCode) {
|
|
948
|
+
send({ jsonrpc: '2.0', id, error: { code: e.rpcCode, message: errMsg, data: e.data || undefined } });
|
|
949
|
+
return;
|
|
950
|
+
}
|
|
896
951
|
// RPC 层异常**必须落盘**。此前它只变成一句 JSON-RPC error 回给调用方,
|
|
897
952
|
// 一旦客户端那边没截住(或用户只看到界面里闪一下),现场就永久丢了 ——
|
|
898
953
|
// 事后既查不到崩在哪,也证不了"刚才是不是真的报过错"。
|
|
899
954
|
recordError('ws_error', {
|
|
900
955
|
method,
|
|
901
956
|
params,
|
|
902
|
-
message:
|
|
957
|
+
message: errMsg,
|
|
903
958
|
stack: String((e && e.stack) || '').slice(0, 8000),
|
|
904
959
|
});
|
|
905
|
-
send({ jsonrpc: '2.0', id, error: { code: -32603, message:
|
|
960
|
+
send({ jsonrpc: '2.0', id, error: { code: -32603, message: errMsg } });
|
|
906
961
|
}
|
|
907
962
|
});
|
|
908
963
|
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// shared/tab_state.cjs — "关掉一个 tab 时,哪些 per-tab 状态必须跟着消失"的唯一清单。
|
|
2
|
+
//
|
|
3
|
+
// 为什么要有这份表(而不是继续在 _cleanupTabResources 里逐行写 delete):
|
|
4
|
+
// PageManager 现在有 26 个 `new Map()/Set()` 字段,其中 20 多个以 tabId 为键。
|
|
5
|
+
// tabId **会被复用**,所以任何一处漏清都会变成"新 tab 带着上一任的状态活着"——
|
|
6
|
+
// 它不报错,只让新 tab 的第一次白屏自检直接被判成 give-up、让旧 URL 一直挂在
|
|
7
|
+
// _openedTabs 里。历史上已经发生过两轮:
|
|
8
|
+
// · Bug 5(2026-10-30):漏了 _refTrackers / _axDiffCache / _failureGuard /
|
|
9
|
+
// canvasMonitor —— 当时只补了被点名的 4 个;
|
|
10
|
+
// · 本次评估(AUDIT-2026-10-30 P1-2):又查出 _openedTabs / _canvasBindings /
|
|
11
|
+
// _paintAuditInflight 三项漏网。
|
|
12
|
+
// 根因不是"谁忘了写",而是**机制**:新增一个 per-tab 容器时,没有任何东西会
|
|
13
|
+
// 提醒你去清理它。所以把清单变成数据,再用"构造函数里的每个 Map/Set 必须登记
|
|
14
|
+
// 在表里或白名单里"的单测当闸门 —— 忘记登记 = 构建失败,而不是半年后一次泄漏。
|
|
15
|
+
//
|
|
16
|
+
// 判定与执行分开(同 view_liveness.cjs 的理由):这里是纯函数,node:electron 不参与,
|
|
17
|
+
// 于是可以用假 owner 把"哪类容器怎么清"逐类钉死。
|
|
18
|
+
//
|
|
19
|
+
// 三类容器,三种清法(表里用标记区分):
|
|
20
|
+
// · 默认 —— Map<K, V> / Set<T>,**键/成员就是 tabId**:delete(tabId)
|
|
21
|
+
// · byValue: true —— Map<K, tabId>,**值是 tabId**(bindingName → tabId 这种):
|
|
22
|
+
// 只删值相等的条目,键是别的东西
|
|
23
|
+
// · timers: true —— Map<tabId, Timeout>:先 clearTimeout 再 delete,
|
|
24
|
+
// 否则关掉的 tab 还会在 5s 后来敲一次已经不存在的视图
|
|
25
|
+
|
|
26
|
+
'use strict';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 以 tabId 为键(或值为 tabId)的容器。**新增这类字段必须在这里登记**,
|
|
30
|
+
* 否则 tests/test_tab_state_reset.js 的完整性闸门会红。
|
|
31
|
+
*/
|
|
32
|
+
const PER_TAB = [
|
|
33
|
+
// ---- 标签页归属 / LRU ----
|
|
34
|
+
{ field: '_tabGroup' },
|
|
35
|
+
{ field: '_tabUsedAt' },
|
|
36
|
+
// ---- window.open 垫片记账 ----
|
|
37
|
+
{ field: '_openedTabs' }, // 键 = 发起 window.open 的那个 tab
|
|
38
|
+
{ field: '_windowOpenShimTabs' },
|
|
39
|
+
{ field: '_windowOpenShimReady' },
|
|
40
|
+
// ---- 导航状态机 ----
|
|
41
|
+
{ field: '_tabNavState' },
|
|
42
|
+
// ---- 浏览器级对话框 ----
|
|
43
|
+
{ field: '_lastDialog' },
|
|
44
|
+
{ field: '_openDialogs' },
|
|
45
|
+
{ field: '_dialogWatched' },
|
|
46
|
+
// ---- 增量 diff / ref 纪元 ----
|
|
47
|
+
{ field: '_axDiffCache' },
|
|
48
|
+
{ field: '_refTrackers' },
|
|
49
|
+
{ field: '_failureGuard' },
|
|
50
|
+
// ---- 重绘 / 白屏自检 / 崩溃自救 ----
|
|
51
|
+
{ field: '_repaintTimers', timers: true },
|
|
52
|
+
{ field: '_paintState' },
|
|
53
|
+
{ field: '_paintSteps' },
|
|
54
|
+
{ field: '_paintTimers', timers: true },
|
|
55
|
+
{ field: '_paintAuditInflight' },
|
|
56
|
+
{ field: '_paintSelfReload' },
|
|
57
|
+
{ field: '_crashReloadAt' },
|
|
58
|
+
// ---- canvas hook 回传路由 ----
|
|
59
|
+
// 键是 `__aiCanvasReport_<tabId>_<rand>`,值是 tabId —— 每次重装 hook 都生成
|
|
60
|
+
// 一个新名字,所以这里只能按**值**清,否则每次 configure 都留一条永不释放。
|
|
61
|
+
{ field: '_canvasBindings', byValue: true },
|
|
62
|
+
];
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 看起来像 per-tab、但**不该由这里清**的容器。每条都必须写清理由,
|
|
66
|
+
* 否则闸门测试只是被我们用手写白名单骗过去而已。
|
|
67
|
+
*/
|
|
68
|
+
const NOT_PER_TAB = {
|
|
69
|
+
_pendingRequests: '键是自增 requestId,promise settle 时自己删;与 tab 生死无关',
|
|
70
|
+
_wsClients: '键是 sessionId,由 unregisterClient 清(连接生命周期)',
|
|
71
|
+
_sessionTab: '键是 sessionId,由 unregisterClient 清(连接生命周期)',
|
|
72
|
+
_groups: '键是 groupId,组空时由 _forgetTab 删;删 tab 不等于删组',
|
|
73
|
+
_windowOpenTokens: '值是 {tabId},closeTab 里手写遍历清(结构不是直接 tabId)',
|
|
74
|
+
_snapshots: '值是 {tab, ...},_cleanupTabResources 里手写遍历清(结构不是直接 tabId)',
|
|
75
|
+
_networkRequestMap: '值是 {tabId, ...},_cleanupTabResources 里手写遍历清',
|
|
76
|
+
_cdpTabs: '值是 {wc},清理前必须 debugger.detach()(有副作用,不能放进纯函数)',
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 清掉一个 tab 的全部状态。
|
|
81
|
+
* @param {object} owner PageManager 实例(或任何带同名字段的假对象)
|
|
82
|
+
* @param {number|string} tabId
|
|
83
|
+
* @param {Array} table 默认用 PER_TAB;测试可传自定义表
|
|
84
|
+
* @returns {string[]} 真正执行过清理的字段名(供调用方/测试观测)
|
|
85
|
+
*/
|
|
86
|
+
function clearPerTab(owner, tabId, table = PER_TAB) {
|
|
87
|
+
const cleared = [];
|
|
88
|
+
if (!owner || tabId === undefined || tabId === null) return cleared;
|
|
89
|
+
|
|
90
|
+
for (const row of table) {
|
|
91
|
+
const c = owner[row.field];
|
|
92
|
+
// 懒初始化的字段此刻可能还不存在;类型不对的字段(有人手改成数组)也别抛。
|
|
93
|
+
if (!c || typeof c.delete !== 'function') continue;
|
|
94
|
+
|
|
95
|
+
if (row.byValue) {
|
|
96
|
+
// Map<K, tabId>:只能遍历值。Set 没有 entries(),故要求是 Map。
|
|
97
|
+
if (typeof c.entries !== 'function') continue;
|
|
98
|
+
for (const [k, v] of c.entries()) {
|
|
99
|
+
if (v === tabId) c.delete(k);
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
if (row.timers && typeof c.get === 'function') {
|
|
103
|
+
const t = c.get(tabId);
|
|
104
|
+
if (t) {
|
|
105
|
+
try { clearTimeout(t); } catch (_) { /* 已触发的 timer 清不掉,正常 */ }
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
c.delete(tabId);
|
|
109
|
+
}
|
|
110
|
+
cleared.push(row.field);
|
|
111
|
+
}
|
|
112
|
+
return cleared;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
module.exports = { PER_TAB, NOT_PER_TAB, clearPerTab };
|
package/src/shared/version.js
CHANGED
|
@@ -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.4.
|
|
5
|
+
export const VERSION = '2.4.6';
|