@te-river/opencode-team-mode 1.5.15 → 1.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 +853 -690
- package/README.zh-CN.md +761 -631
- package/dist/agents.d.ts +5 -3
- package/dist/agents.d.ts.map +1 -1
- package/dist/agents.js +19 -8
- package/dist/agents.js.map +1 -1
- package/dist/capabilities.d.ts +1 -1
- package/dist/capabilities.js +2 -2
- package/dist/capabilities.js.map +1 -1
- package/dist/host/note.d.ts +7 -0
- package/dist/host/note.d.ts.map +1 -0
- package/dist/host/note.js +28 -0
- package/dist/host/note.js.map +1 -0
- package/dist/host/v1.d.ts +14 -0
- package/dist/host/v1.d.ts.map +1 -0
- package/dist/host/v1.js +340 -0
- package/dist/host/v1.js.map +1 -0
- package/dist/host/v2-browser-gate.d.ts +45 -0
- package/dist/host/v2-browser-gate.d.ts.map +1 -0
- package/dist/host/v2-browser-gate.js +319 -0
- package/dist/host/v2-browser-gate.js.map +1 -0
- package/dist/host/v2-capabilities.d.ts +68 -0
- package/dist/host/v2-capabilities.d.ts.map +1 -0
- package/dist/host/v2-capabilities.js +146 -0
- package/dist/host/v2-capabilities.js.map +1 -0
- package/dist/host/v2-client.d.ts +53 -0
- package/dist/host/v2-client.d.ts.map +1 -0
- package/dist/host/v2-client.js +193 -0
- package/dist/host/v2-client.js.map +1 -0
- package/dist/host/v2-events.d.ts +56 -0
- package/dist/host/v2-events.d.ts.map +1 -0
- package/dist/host/v2-events.js +158 -0
- package/dist/host/v2-events.js.map +1 -0
- package/dist/host/v2-guard.d.ts +101 -0
- package/dist/host/v2-guard.d.ts.map +1 -0
- package/dist/host/v2-guard.js +199 -0
- package/dist/host/v2-guard.js.map +1 -0
- package/dist/host/v2-offload.d.ts +148 -0
- package/dist/host/v2-offload.d.ts.map +1 -0
- package/dist/host/v2-offload.js +288 -0
- package/dist/host/v2-offload.js.map +1 -0
- package/dist/host/v2-permissions.d.ts +123 -0
- package/dist/host/v2-permissions.d.ts.map +1 -0
- package/dist/host/v2-permissions.js +190 -0
- package/dist/host/v2-permissions.js.map +1 -0
- package/dist/host/v2-probe.d.ts +85 -0
- package/dist/host/v2-probe.d.ts.map +1 -0
- package/dist/host/v2-probe.js +268 -0
- package/dist/host/v2-probe.js.map +1 -0
- package/dist/host/v2-scope.d.ts +51 -0
- package/dist/host/v2-scope.d.ts.map +1 -0
- package/dist/host/v2-scope.js +58 -0
- package/dist/host/v2-scope.js.map +1 -0
- package/dist/host/v2-session-client.d.ts +74 -0
- package/dist/host/v2-session-client.d.ts.map +1 -0
- package/dist/host/v2-session-client.js +171 -0
- package/dist/host/v2-session-client.js.map +1 -0
- package/dist/host/v2-session.d.ts +85 -0
- package/dist/host/v2-session.d.ts.map +1 -0
- package/dist/host/v2-session.js +171 -0
- package/dist/host/v2-session.js.map +1 -0
- package/dist/host/v2-subagent.d.ts +157 -0
- package/dist/host/v2-subagent.d.ts.map +1 -0
- package/dist/host/v2-subagent.js +286 -0
- package/dist/host/v2-subagent.js.map +1 -0
- package/dist/host/v2-tool.d.ts +67 -0
- package/dist/host/v2-tool.d.ts.map +1 -0
- package/dist/host/v2-tool.js +209 -0
- package/dist/host/v2-tool.js.map +1 -0
- package/dist/host/v2-transcript.d.ts +73 -0
- package/dist/host/v2-transcript.d.ts.map +1 -0
- package/dist/host/v2-transcript.js +151 -0
- package/dist/host/v2-transcript.js.map +1 -0
- package/dist/host/v2-types.d.ts +229 -0
- package/dist/host/v2-types.d.ts.map +1 -0
- package/dist/host/v2-types.js +17 -0
- package/dist/host/v2-types.js.map +1 -0
- package/dist/host/v2.d.ts +42 -0
- package/dist/host/v2.d.ts.map +1 -0
- package/dist/host/v2.js +706 -0
- package/dist/host/v2.js.map +1 -0
- package/dist/host-hooks.d.ts +13 -2
- package/dist/host-hooks.d.ts.map +1 -1
- package/dist/host-hooks.js +18 -5
- package/dist/host-hooks.js.map +1 -1
- package/dist/index.d.ts +23 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +29 -326
- package/dist/index.js.map +1 -1
- package/dist/prompts/lead.d.ts +1 -1
- package/dist/prompts/lead.d.ts.map +1 -1
- package/dist/prompts/lead.js +452 -359
- package/dist/prompts/lead.js.map +1 -1
- package/dist/prompts/shared.d.ts +2 -2
- package/dist/prompts/shared.d.ts.map +1 -1
- package/dist/prompts/shared.js +89 -17
- package/dist/prompts/shared.js.map +1 -1
- package/dist/prompts/specialists.d.ts +2 -2
- package/dist/prompts/specialists.d.ts.map +1 -1
- package/dist/prompts/specialists.js +44 -10
- package/dist/prompts/specialists.js.map +1 -1
- package/dist/task-offload.d.ts +83 -0
- package/dist/task-offload.d.ts.map +1 -0
- package/dist/task-offload.js +174 -0
- package/dist/task-offload.js.map +1 -0
- package/dist/tm/args-schema.d.ts +4 -0
- package/dist/tm/args-schema.d.ts.map +1 -1
- package/dist/tm/args-schema.js +36 -3
- package/dist/tm/args-schema.js.map +1 -1
- package/dist/tm/board.d.ts +113 -0
- package/dist/tm/board.d.ts.map +1 -0
- package/dist/tm/board.js +275 -0
- package/dist/tm/board.js.map +1 -0
- package/dist/tm/browser.d.ts +325 -6
- package/dist/tm/browser.d.ts.map +1 -1
- package/dist/tm/browser.js +1360 -135
- package/dist/tm/browser.js.map +1 -1
- package/dist/tm/config.d.ts +36 -25
- package/dist/tm/config.d.ts.map +1 -1
- package/dist/tm/config.js +29 -7
- package/dist/tm/config.js.map +1 -1
- package/dist/tm/dispatch.d.ts +133 -53
- package/dist/tm/dispatch.d.ts.map +1 -1
- package/dist/tm/dispatch.js +481 -279
- package/dist/tm/dispatch.js.map +1 -1
- package/dist/tm/dupe-guard.d.ts +16 -3
- package/dist/tm/dupe-guard.d.ts.map +1 -1
- package/dist/tm/dupe-guard.js +27 -7
- package/dist/tm/dupe-guard.js.map +1 -1
- package/dist/tm/egress.d.ts +44 -0
- package/dist/tm/egress.d.ts.map +1 -0
- package/dist/tm/egress.js +191 -0
- package/dist/tm/egress.js.map +1 -0
- package/dist/tm/guard.js +1 -1
- package/dist/tm/guard.js.map +1 -1
- package/dist/tm/index.d.ts +67 -2
- package/dist/tm/index.d.ts.map +1 -1
- package/dist/tm/index.js +228 -20
- package/dist/tm/index.js.map +1 -1
- package/dist/tm/ledger.d.ts +114 -0
- package/dist/tm/ledger.d.ts.map +1 -0
- package/dist/tm/ledger.js +308 -0
- package/dist/tm/ledger.js.map +1 -0
- package/dist/tm/perm-ask.d.ts +45 -5
- package/dist/tm/perm-ask.d.ts.map +1 -1
- package/dist/tm/perm-ask.js +107 -7
- package/dist/tm/perm-ask.js.map +1 -1
- package/dist/tm/pipelines.d.ts.map +1 -1
- package/dist/tm/pipelines.js +24 -2
- package/dist/tm/pipelines.js.map +1 -1
- package/dist/tm/preview.d.ts +55 -0
- package/dist/tm/preview.d.ts.map +1 -1
- package/dist/tm/preview.js +225 -0
- package/dist/tm/preview.js.map +1 -1
- package/dist/tm/ptc/tool.d.ts.map +1 -1
- package/dist/tm/ptc/tool.js +2 -4
- package/dist/tm/ptc/tool.js.map +1 -1
- package/dist/tm/pty.d.ts.map +1 -1
- package/dist/tm/pty.js +6 -3
- package/dist/tm/pty.js.map +1 -1
- package/dist/tm/search.d.ts +5 -0
- package/dist/tm/search.d.ts.map +1 -1
- package/dist/tm/search.js +40 -8
- package/dist/tm/search.js.map +1 -1
- package/dist/tm/serp-loop.d.ts +45 -0
- package/dist/tm/serp-loop.d.ts.map +1 -0
- package/dist/tm/serp-loop.js +80 -0
- package/dist/tm/serp-loop.js.map +1 -0
- package/dist/tm/stats.d.ts +26 -0
- package/dist/tm/stats.d.ts.map +1 -1
- package/dist/tm/stats.js +194 -4
- package/dist/tm/stats.js.map +1 -1
- package/dist/tm/webfetch.d.ts +56 -0
- package/dist/tm/webfetch.d.ts.map +1 -1
- package/dist/tm/webfetch.js +187 -19
- package/dist/tm/webfetch.js.map +1 -1
- package/dist/tool-coerce.d.ts +24 -0
- package/dist/tool-coerce.d.ts.map +1 -0
- package/dist/tool-coerce.js +63 -0
- package/dist/tool-coerce.js.map +1 -0
- package/dist/types.d.ts +7 -1
- package/dist/types.d.ts.map +1 -1
- package/docs/installation-v2.md +436 -0
- package/docs/installation.md +81 -2
- package/docs/research/agent-data-exchange.md +117 -0
- package/docs/research/browser-pane.md +89 -0
- package/docs/research/host-http-api.md +132 -0
- package/docs/research/host-subagent-injection.md +187 -0
- package/docs/research/plugin-loader-contract.md +261 -0
- package/docs/research/v2-builtin-tools.md +447 -0
- package/docs/v2-port-notes.md +315 -0
- package/index.js +7 -0
- package/package.json +4 -1
- package/scripts/gen-v2-config.mjs +304 -0
- package/scripts/lib/config-surgery.cjs +239 -0
- package/scripts/lib/fake-ctx.mjs +146 -0
package/dist/tm/browser.js
CHANGED
|
@@ -12,10 +12,10 @@
|
|
|
12
12
|
* verbatim as the fallback. On cdp-legacy only the core subset works
|
|
13
13
|
* (open/navigate/read/screenshot/close + navigate_page/take_screenshot/
|
|
14
14
|
* list_pages/evaluate_script aliases); the snapshot-first actions
|
|
15
|
-
* (take_snapshot + uid addressing,
|
|
15
|
+
* (take_snapshot + uid addressing, 18-verb chrome-devtools-mcp surface)
|
|
16
16
|
* require the playwright engine.
|
|
17
17
|
*
|
|
18
|
-
* Action surface aligns with chrome-devtools-mcp's
|
|
18
|
+
* Action surface aligns with chrome-devtools-mcp's 18 verbs (R2 mapping):
|
|
19
19
|
* navigate_page · take_snapshot · click · fill · hover · drag ·
|
|
20
20
|
* press_key · select_page · upload_file · wait_for · evaluate_script ·
|
|
21
21
|
* list_console_messages · list_network_requests · list_pages ·
|
|
@@ -61,17 +61,25 @@
|
|
|
61
61
|
* Role access mirrors tm_webfetch: network roles (team + researcher) only,
|
|
62
62
|
* tester browser-only (UI verification).
|
|
63
63
|
*/
|
|
64
|
-
import { execFileSync, spawn } from "node:child_process";
|
|
64
|
+
import { execFileSync, spawn, spawnSync } from "node:child_process";
|
|
65
65
|
import * as fs from "node:fs";
|
|
66
66
|
import * as os from "node:os";
|
|
67
67
|
import * as path from "node:path";
|
|
68
68
|
import { checkWebUrl, seedWebfetchDomains } from "./webfetch.js";
|
|
69
|
-
import { askUserForTarget } from "./perm-ask.js";
|
|
69
|
+
import { askGrantNote, askRefusalNote, askUserForTarget, askUserForTargetDetailed } from "./perm-ask.js";
|
|
70
70
|
import { assertReadablePath } from "./guard.js";
|
|
71
71
|
import { isEnvFilePath } from "../envprotect.js";
|
|
72
|
+
import { createSerpLoopGuard, serpRefusal } from "./serp-loop.js";
|
|
72
73
|
import { tmError, toToolResult } from "./result.js";
|
|
73
74
|
import { estimateTokens } from "./config.js";
|
|
74
75
|
import { rmForceSafe } from "../fs-safe.js";
|
|
76
|
+
/**
|
|
77
|
+
* Per-process by design, like the dupe guard: ONE shared browser tool instance
|
|
78
|
+
* serves every agent in this plugin process, and "how many search-result pages
|
|
79
|
+
* has this conversation opened" is a conversation-level fact, not a per-caller
|
|
80
|
+
* one (leases are per-caller; the loop is the conversation's).
|
|
81
|
+
*/
|
|
82
|
+
const serpLoop = createSerpLoopGuard();
|
|
75
83
|
/** Per-OS browser candidates, in preference order (first hit wins). ONLY a
|
|
76
84
|
* fallback: on Windows the registry probe below is what actually honours the
|
|
77
85
|
* user's chosen channel. Edge/Chrome ship each channel in its OWN install
|
|
@@ -291,6 +299,44 @@ export function findBrowserExecutable(env = process.env) {
|
|
|
291
299
|
* auto (default) → headless ONLY when there is no
|
|
292
300
|
* display possible (Linux without DISPLAY/WAYLAND_DISPLAY).
|
|
293
301
|
*/
|
|
302
|
+
/** Decide what profile directory TM_BROWSER_USER_DATA_DIR asks for.
|
|
303
|
+
*
|
|
304
|
+
* The tool description has claimed for a long time that "the real profile is
|
|
305
|
+
* never touched" while nothing enforced it: the string went straight into
|
|
306
|
+
* `launchPersistentContext`. Pointing it at the user's own browser data dir is
|
|
307
|
+
* not a neutral choice — with the browser open, the new process hands its URL
|
|
308
|
+
* to the running instance and exits (the confusing "本机已有同品牌浏览器在跑"
|
|
309
|
+
* failure), and with it closed the agent browses AS THE USER, with every cookie
|
|
310
|
+
* and saved session, while our orphan reaper force-kills (`taskkill /T /F`) any
|
|
311
|
+
* browser whose owner died — a real route to corrupting a profile.
|
|
312
|
+
*
|
|
313
|
+
* So: a real browser data dir is refused with the fix named, anything else is
|
|
314
|
+
* honored, and an unset value keeps the isolated temp profile (the default).
|
|
315
|
+
* Pure, so both engines and the tests share one answer. */
|
|
316
|
+
export function classifyProfileDir(env = process.env) {
|
|
317
|
+
const raw = String(env.TM_BROWSER_USER_DATA_DIR ?? "").trim();
|
|
318
|
+
if (!raw)
|
|
319
|
+
return { dir: null, refusal: null };
|
|
320
|
+
const norm = raw.replace(/\\/g, "/").replace(/\/+$/, "").toLowerCase();
|
|
321
|
+
const segs = norm.split("/").filter(Boolean);
|
|
322
|
+
const last = segs[segs.length - 1] ?? "";
|
|
323
|
+
const looksReal = last === "user data" ||
|
|
324
|
+
last === "google-chrome" ||
|
|
325
|
+
last === "chromium" ||
|
|
326
|
+
last === "profiles" ||
|
|
327
|
+
/(^|\/)(microsoft|google)\/(edge|chrome|chromium|chrome sxs)(\/(user data))?$/.test(norm) ||
|
|
328
|
+
/\/mozilla\/firefox\/(profiles|profile)$/.test(norm);
|
|
329
|
+
if (looksReal) {
|
|
330
|
+
return {
|
|
331
|
+
dir: null,
|
|
332
|
+
refusal: `TM_BROWSER_USER_DATA_DIR 指向的看起来是浏览器自己的数据目录(${raw}),拒绝用它启动。` +
|
|
333
|
+
`那样 agent 就是以你的真实身份上网,而孤儿回收是强杀进程,可能损坏这个目录。` +
|
|
334
|
+
`要保留登录态:把它改成一个专用空目录(例如 D:\\tm-browser-profile 或 ~/.tm-browser-profile),` +
|
|
335
|
+
`第一次让 agent 打开登录页、你人工登录一次,之后 cookie 就在这个目录里跨会话保留。`,
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
return { dir: raw, refusal: null };
|
|
339
|
+
}
|
|
294
340
|
export function resolveHeadless(env = process.env) {
|
|
295
341
|
const raw = String(env.TM_BROWSER_HEADLESS ?? "auto").trim().toLowerCase();
|
|
296
342
|
if (["1", "true", "force", "yes"].includes(raw))
|
|
@@ -378,6 +424,102 @@ export function rememberSite(allowedSites, url) {
|
|
|
378
424
|
/* unparseable — nothing to remember */
|
|
379
425
|
}
|
|
380
426
|
}
|
|
427
|
+
// ---------- a page that is not a page, but a wall ----------------------------
|
|
428
|
+
/**
|
|
429
|
+
* Human-verification walls. This exists because "0 个可寻址节点" and "this
|
|
430
|
+
* site wants a human to prove it is one" look identical to the agent, and the
|
|
431
|
+
* two have opposite next moves: an empty page is worth retrying, a wall never
|
|
432
|
+
* is — retrying just spends rounds on a captcha. Measured on baike.baidu.com
|
|
433
|
+
* item pages, where every arm (raw playwright included, all resources allowed)
|
|
434
|
+
* returns 0–6 nodes titled 百度安全验证, and the agent reported "该网站没有内容".
|
|
435
|
+
*
|
|
436
|
+
* Deliberately narrow: signatures we observed live, plus the unambiguous
|
|
437
|
+
* English ones. Callers consult it ONLY when the page came back thin, so an
|
|
438
|
+
* article that merely mentions 验证码 is never mislabelled as a wall.
|
|
439
|
+
*/
|
|
440
|
+
export const CHALLENGE_WALL_SIGNS = [
|
|
441
|
+
{ re: /百度安全验证|百度的安全验证/, label: "百度安全验证" },
|
|
442
|
+
{ re: /安全验证|访问验证|请完成验证|滑动验证|拖动滑块|人机验证|点击按钮进行验证/, label: "站点安全验证" },
|
|
443
|
+
{ re: /just a moment|checking before the link|cf-browser-verification/i, label: "Cloudflare 人机校验" },
|
|
444
|
+
{ re: /verify you are a human|are you a robot|enable javascript and cookies/i, label: "人机校验" },
|
|
445
|
+
{ re: /access denied|attention required|blocked because|request blocked/i, label: "访问被拒(反爬拦截)" },
|
|
446
|
+
];
|
|
447
|
+
/** Which wall this thin page actually is, or null when it is just empty. */
|
|
448
|
+
export function challengeWallOf(...texts) {
|
|
449
|
+
const hay = texts.filter(Boolean).join("\n").slice(0, 4000);
|
|
450
|
+
if (!hay.trim())
|
|
451
|
+
return null;
|
|
452
|
+
for (const s of CHALLENGE_WALL_SIGNS)
|
|
453
|
+
if (s.re.test(hay))
|
|
454
|
+
return s.label;
|
|
455
|
+
return null;
|
|
456
|
+
}
|
|
457
|
+
/** One hostname for `allow_host`, or "" when the argument is not a single bare
|
|
458
|
+
* domain. Strict on purpose: this value becomes the pattern in an official
|
|
459
|
+
* consent dialog, and a user approving "bkssl.bdimg.com" must not be getting a
|
|
460
|
+
* wildcard, a URL with a path, or a port. */
|
|
461
|
+
export function normalizeApprovalHost(raw) {
|
|
462
|
+
const s = String(raw ?? "").trim().toLowerCase().replace(/^[a-z]+:\/\//, "").replace(/^\.+|\.+$/g, "");
|
|
463
|
+
if (!s || s.length > 253)
|
|
464
|
+
return "";
|
|
465
|
+
if (/[\s/@:*?#%\\]/.test(s))
|
|
466
|
+
return "";
|
|
467
|
+
if (!/^[a-z0-9一-鿿-]+(\.[a-z0-9一-鿿-]+)+$/.test(s))
|
|
468
|
+
return "";
|
|
469
|
+
return s;
|
|
470
|
+
}
|
|
471
|
+
/** The document title, or "" — a title is a diagnostic, never a reason for the
|
|
472
|
+
* action that was asked for to fail. */
|
|
473
|
+
async function readTitle(page) {
|
|
474
|
+
try {
|
|
475
|
+
return String((await page.title?.()) ?? "");
|
|
476
|
+
}
|
|
477
|
+
catch {
|
|
478
|
+
return "";
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* The launch pid, asked more than once.
|
|
483
|
+
*
|
|
484
|
+
* Why this exists: `childProcRows` folds EVERY OS-query failure (PowerShell
|
|
485
|
+
* exiting non-zero, the 6 s timeout killing it mid-flight, a JSON parse miss)
|
|
486
|
+
* into an empty list, and the launcher used to ask exactly once. Under load —
|
|
487
|
+
* this plugin's own parallel test runner reproduces it, four suites hammering
|
|
488
|
+
* CIM at once — one such query fails now and then, and a single failed query
|
|
489
|
+
* used to cost the session two things permanently: the orphan-ledger entry (so
|
|
490
|
+
* the next boot could not reclaim that browser) and `close`'s process
|
|
491
|
+
* verification. Worse, an exception escaping the lookup failed `open` itself,
|
|
492
|
+
* reporting a browser that had launched fine as an error.
|
|
493
|
+
*
|
|
494
|
+
* So: a failed or empty answer is retried a bounded number of times, and any
|
|
495
|
+
* throw is swallowed into the retry counter — the pid is diagnostic and
|
|
496
|
+
* reclamation metadata, never a reason to lose the session. When it still
|
|
497
|
+
* cannot be resolved, `via: "none"` says so and close keeps refusing the
|
|
498
|
+
* 已确认关闭 sentence (pinned by §25).
|
|
499
|
+
*/
|
|
500
|
+
export const LAUNCH_PID_ATTEMPTS = 3;
|
|
501
|
+
export const LAUNCH_PID_RETRY_MS = 250;
|
|
502
|
+
export async function resolveLaunchPid(opts) {
|
|
503
|
+
const pwPid = Number(opts.playwrightPid?.() ?? 0) || 0;
|
|
504
|
+
if (pwPid > 0)
|
|
505
|
+
return { pid: pwPid, via: "playwright", failures: 0 };
|
|
506
|
+
const attempts = Math.max(1, Number(opts.attempts) || LAUNCH_PID_ATTEMPTS);
|
|
507
|
+
const sleep = opts.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
508
|
+
let failures = 0;
|
|
509
|
+
for (let i = 0; i < attempts; i++) {
|
|
510
|
+
if (i)
|
|
511
|
+
await sleep(LAUNCH_PID_RETRY_MS);
|
|
512
|
+
try {
|
|
513
|
+
const scan = launchedBrowserPid(opts.executablePath, { rows: opts.rows() });
|
|
514
|
+
if (scan > 0)
|
|
515
|
+
return { pid: scan, via: "child-scan", failures };
|
|
516
|
+
}
|
|
517
|
+
catch {
|
|
518
|
+
failures++;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
return { pid: 0, via: "none", failures };
|
|
522
|
+
}
|
|
381
523
|
// ---------- evaluate_script: consent + result redaction ---------------------
|
|
382
524
|
/** `evaluate_script` is the one browser verb the network gate cannot cover:
|
|
383
525
|
* the allowlist limits where we NAVIGATE, not what an already-loaded page
|
|
@@ -413,11 +555,375 @@ export function redactEvalResult(text) {
|
|
|
413
555
|
}
|
|
414
556
|
return { text: out, masked: kinds };
|
|
415
557
|
}
|
|
558
|
+
export function browserLedgerFile(storeRoot) {
|
|
559
|
+
return path.join(storeRoot, "browsers.jsonl");
|
|
560
|
+
}
|
|
561
|
+
/** Best-effort append: a ledger that cannot be written must never cost the
|
|
562
|
+
* user their browser session. */
|
|
563
|
+
export function appendBrowserLedger(file, rec) {
|
|
564
|
+
try {
|
|
565
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
566
|
+
fs.appendFileSync(file, JSON.stringify(rec) + "\n", "utf8");
|
|
567
|
+
}
|
|
568
|
+
catch {
|
|
569
|
+
/* observability + reclamation only */
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
/** Read the ledger, terminate the orphans, and rewrite what is still live.
|
|
573
|
+
* Returns the counts so the caller can log them; never throws. */
|
|
574
|
+
export function reapOrphanBrowsers(file, opts = {}) {
|
|
575
|
+
const alive = opts.alive ?? pidAlive;
|
|
576
|
+
const kill = opts.kill ?? ((p) => killBrowserTree(p));
|
|
577
|
+
const identity = opts.identity ?? procIdentity;
|
|
578
|
+
const out = { reaped: [], kept: 0, dropped: 0 };
|
|
579
|
+
let raw;
|
|
580
|
+
try {
|
|
581
|
+
raw = fs.readFileSync(file, "utf8");
|
|
582
|
+
}
|
|
583
|
+
catch {
|
|
584
|
+
return out;
|
|
585
|
+
}
|
|
586
|
+
const survivors = [];
|
|
587
|
+
const seen = new Set();
|
|
588
|
+
for (const line of raw.split("\n")) {
|
|
589
|
+
if (!line.trim())
|
|
590
|
+
continue;
|
|
591
|
+
let e;
|
|
592
|
+
try {
|
|
593
|
+
e = JSON.parse(line);
|
|
594
|
+
}
|
|
595
|
+
catch {
|
|
596
|
+
continue; // a torn line is not an instruction to kill anything
|
|
597
|
+
}
|
|
598
|
+
const pid = Number(e?.pid);
|
|
599
|
+
const owner = Number(e?.ownerPid);
|
|
600
|
+
if (!Number.isFinite(pid) || pid <= 0 || seen.has(pid)) {
|
|
601
|
+
out.dropped++;
|
|
602
|
+
continue;
|
|
603
|
+
}
|
|
604
|
+
seen.add(pid);
|
|
605
|
+
if (!alive(pid)) {
|
|
606
|
+
out.dropped++; // browser exited on its own — the entry is history
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
if (owner > 0 && !alive(owner)) {
|
|
610
|
+
// An OS pid is a recyclable number, and the kill below is a FORCE kill of
|
|
611
|
+
// a whole process tree. So the entry has to still describe the
|
|
612
|
+
// executable we launched: if it does not, that pid belongs to an
|
|
613
|
+
// unrelated program now and killing it would be our bug, not our job.
|
|
614
|
+
const exe = String(e?.exe ?? "").trim();
|
|
615
|
+
if (exe && !identityMatchesExecutable(exe, identity(pid))) {
|
|
616
|
+
out.dropped++;
|
|
617
|
+
continue;
|
|
618
|
+
}
|
|
619
|
+
try {
|
|
620
|
+
kill(pid);
|
|
621
|
+
out.reaped.push(pid);
|
|
622
|
+
}
|
|
623
|
+
catch {
|
|
624
|
+
survivors.push(e); // not ours to signal (or already gone) — keep the record, say nothing false
|
|
625
|
+
}
|
|
626
|
+
continue;
|
|
627
|
+
}
|
|
628
|
+
survivors.push(e);
|
|
629
|
+
}
|
|
630
|
+
out.kept = survivors.length;
|
|
631
|
+
try {
|
|
632
|
+
if (survivors.length)
|
|
633
|
+
fs.writeFileSync(file, survivors.map((s) => JSON.stringify(s) + "\n").join(""), "utf8");
|
|
634
|
+
else
|
|
635
|
+
fs.rmSync(file, { force: true });
|
|
636
|
+
}
|
|
637
|
+
catch {
|
|
638
|
+
/* the next boot retries */
|
|
639
|
+
}
|
|
640
|
+
return out;
|
|
641
|
+
}
|
|
642
|
+
/** Is this OS pid still alive? `process.kill(pid, 0)` is the portable probe:
|
|
643
|
+
* ESRCH means gone, EPERM means alive but owned by someone else — both are
|
|
644
|
+
* answers, and "no answer" is never one of them. */
|
|
645
|
+
export function pidAlive(pid) {
|
|
646
|
+
if (!Number.isFinite(pid) || pid <= 0)
|
|
647
|
+
return false;
|
|
648
|
+
try {
|
|
649
|
+
process.kill(pid, 0);
|
|
650
|
+
return true;
|
|
651
|
+
}
|
|
652
|
+
catch (err) {
|
|
653
|
+
return err?.code === "EPERM";
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
/** Wait up to `ms` for a pid to disappear, then terminate it once and give it
|
|
657
|
+
* a short grace. Returns whether the process is GONE — the only honest basis
|
|
658
|
+
* for a "已确认关闭" claim. */
|
|
659
|
+
export async function waitForPidExit(pid, ms = 3000, kill = (p) => killBrowserTree(p)) {
|
|
660
|
+
if (!pidAlive(pid))
|
|
661
|
+
return true;
|
|
662
|
+
const deadline = Date.now() + Math.max(0, ms);
|
|
663
|
+
while (Date.now() < deadline && pidAlive(pid)) {
|
|
664
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
665
|
+
}
|
|
666
|
+
if (!pidAlive(pid))
|
|
667
|
+
return true;
|
|
668
|
+
try {
|
|
669
|
+
kill(pid);
|
|
670
|
+
}
|
|
671
|
+
catch {
|
|
672
|
+
/* already gone, or not ours to signal — the re-check below decides */
|
|
673
|
+
}
|
|
674
|
+
await new Promise((r) => setTimeout(r, 250));
|
|
675
|
+
return !pidAlive(pid);
|
|
676
|
+
}
|
|
677
|
+
/** What a pid actually is right now — its image name and command line, or null
|
|
678
|
+
* when the pid is gone. Used to prove a recorded pid is still the browser we
|
|
679
|
+
* launched before anything force-kills it (see reapOrphanBrowsers). */
|
|
680
|
+
function procIdentity(pid) {
|
|
681
|
+
const p = Number(pid);
|
|
682
|
+
if (!Number.isInteger(p) || p <= 0)
|
|
683
|
+
return null;
|
|
684
|
+
if (process.platform === "win32") {
|
|
685
|
+
const script = `Get-CimInstance Win32_Process -Filter 'ProcessId=${p}' | Select-Object Name,CommandLine | ConvertTo-Json -Compress`;
|
|
686
|
+
const r = spawnSync("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", script], {
|
|
687
|
+
encoding: "utf8",
|
|
688
|
+
windowsHide: true,
|
|
689
|
+
timeout: 6000,
|
|
690
|
+
maxBuffer: 4 * 1024 * 1024,
|
|
691
|
+
});
|
|
692
|
+
if (r.status !== 0)
|
|
693
|
+
return null;
|
|
694
|
+
const text = String(r.stdout ?? "").trim();
|
|
695
|
+
if (!text)
|
|
696
|
+
return null;
|
|
697
|
+
try {
|
|
698
|
+
const row = JSON.parse(text);
|
|
699
|
+
const one = (Array.isArray(row) ? row[0] : row) ?? {};
|
|
700
|
+
return { name: String(one?.Name ?? ""), cmdline: String(one?.CommandLine ?? "") };
|
|
701
|
+
}
|
|
702
|
+
catch {
|
|
703
|
+
return null;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
try {
|
|
707
|
+
const raw = fs.readFileSync(`/proc/${p}/cmdline`);
|
|
708
|
+
const parts = String(raw).split("\0").filter((s) => s !== "");
|
|
709
|
+
if (!parts.length)
|
|
710
|
+
return null;
|
|
711
|
+
return { name: path.basename(parts[0]), cmdline: parts.join(" ") };
|
|
712
|
+
}
|
|
713
|
+
catch {
|
|
714
|
+
return null;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
/** Does this identity look like the executable we launched? Compared on the
|
|
718
|
+
* file NAME: the command line's quoting and path form can differ from ours
|
|
719
|
+
* (8.3-short vs long path, escaped quotes), while a pid recycled by an
|
|
720
|
+
* unrelated program almost never carries the same image name. A missing
|
|
721
|
+
* identity counts as NO — the safe answer before a force-kill. */
|
|
722
|
+
export function identityMatchesExecutable(exePath, ident) {
|
|
723
|
+
const want = path.basename(String(exePath ?? "")).toLowerCase();
|
|
724
|
+
if (!want || !ident)
|
|
725
|
+
return false;
|
|
726
|
+
const name = String(ident.name ?? "").toLowerCase();
|
|
727
|
+
const cmd = String(ident.cmdline ?? "").toLowerCase();
|
|
728
|
+
return name === want || cmd.includes(want);
|
|
729
|
+
}
|
|
730
|
+
/** The same question asked of a live pid (see identityMatchesExecutable). */
|
|
731
|
+
export function pidMatchesExecutable(pid, exePath) {
|
|
732
|
+
return identityMatchesExecutable(exePath, procIdentity(pid));
|
|
733
|
+
}
|
|
734
|
+
/** Terminate a browser process AND everything it spawned.
|
|
735
|
+
*
|
|
736
|
+
* `process.kill(pid)` signals the ROOT only. Measured on the desktop host the
|
|
737
|
+
* day this was written: the orphan reaper's default kill took the msedge root
|
|
738
|
+
* down and left all NINE of its child processes running — which is exactly the
|
|
739
|
+
* leftover the reaper exists to remove. The graceful path does not have this
|
|
740
|
+
* problem (playwright's `browser.close()` shuts its children down first, and
|
|
741
|
+
* the real-host leg verifies it), so this is the fallback's job.
|
|
742
|
+
*
|
|
743
|
+
* win32: `taskkill /T /F` walks the tree for us.
|
|
744
|
+
* POSIX: no tree flag, so descendants are enumerated and killed deepest-first.
|
|
745
|
+
* Best-effort throughout: a process that vanishes mid-walk is not an error.
|
|
746
|
+
*/
|
|
747
|
+
export function killBrowserTree(pid, opts = {}) {
|
|
748
|
+
const p = Number(pid);
|
|
749
|
+
if (!Number.isInteger(p) || p <= 0)
|
|
750
|
+
return;
|
|
751
|
+
const run = opts.run ?? ((cmd, args) => {
|
|
752
|
+
spawnSync(cmd, args, { windowsHide: true, timeout: 8000 });
|
|
753
|
+
});
|
|
754
|
+
if ((opts.platform ?? process.platform) === "win32") {
|
|
755
|
+
try {
|
|
756
|
+
run("taskkill.exe", ["/PID", String(p), "/T", "/F"]);
|
|
757
|
+
}
|
|
758
|
+
catch {
|
|
759
|
+
/* gone already, or not ours to signal */
|
|
760
|
+
}
|
|
761
|
+
return;
|
|
762
|
+
}
|
|
763
|
+
const children = opts.children ?? childProcRows;
|
|
764
|
+
const order = [];
|
|
765
|
+
const queue = [p];
|
|
766
|
+
while (queue.length) {
|
|
767
|
+
const cur = queue.shift();
|
|
768
|
+
let kids = [];
|
|
769
|
+
try {
|
|
770
|
+
kids = children(cur) ?? [];
|
|
771
|
+
}
|
|
772
|
+
catch {
|
|
773
|
+
kids = [];
|
|
774
|
+
}
|
|
775
|
+
for (const k of kids) {
|
|
776
|
+
const kid = Number(k?.pid);
|
|
777
|
+
if (Number.isInteger(kid) && kid > 0 && kid !== p && !order.includes(kid)) {
|
|
778
|
+
order.push(kid);
|
|
779
|
+
queue.push(kid);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
const signal = opts.signal ?? ((p) => process.kill(p));
|
|
784
|
+
for (const kid of order.reverse()) {
|
|
785
|
+
try {
|
|
786
|
+
signal(kid);
|
|
787
|
+
}
|
|
788
|
+
catch {
|
|
789
|
+
/* racy by design — the re-check above is what decides */
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
try {
|
|
793
|
+
signal(p);
|
|
794
|
+
}
|
|
795
|
+
catch {
|
|
796
|
+
/* already gone */
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
/** Children of `ppid`, straight from the OS. Best-effort by design: a host
|
|
800
|
+
* without PowerShell/CIM or a refused query returns [], and the caller then
|
|
801
|
+
* reports that the process could not be verified rather than guessing. */
|
|
802
|
+
function childProcRows(ppid) {
|
|
803
|
+
if (process.platform === "win32") {
|
|
804
|
+
// The filter runs server-side, so the reply holds ONLY our own children —
|
|
805
|
+
// never a full-table scan, never a name match on someone else's browser.
|
|
806
|
+
const script = `Get-CimInstance Win32_Process -Filter 'ParentProcessId=${ppid}' | ` +
|
|
807
|
+
"Select-Object ProcessId,Name,CommandLine | ConvertTo-Json -Compress";
|
|
808
|
+
const r = spawnSync("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", script], {
|
|
809
|
+
encoding: "utf8",
|
|
810
|
+
windowsHide: true,
|
|
811
|
+
timeout: 6000,
|
|
812
|
+
maxBuffer: 8 * 1024 * 1024,
|
|
813
|
+
});
|
|
814
|
+
if (r.status !== 0)
|
|
815
|
+
return [];
|
|
816
|
+
const text = String(r.stdout ?? "").trim();
|
|
817
|
+
if (!text)
|
|
818
|
+
return [];
|
|
819
|
+
let rows;
|
|
820
|
+
try {
|
|
821
|
+
rows = JSON.parse(text);
|
|
822
|
+
}
|
|
823
|
+
catch {
|
|
824
|
+
return [];
|
|
825
|
+
}
|
|
826
|
+
const list = Array.isArray(rows) ? rows : [rows];
|
|
827
|
+
return list
|
|
828
|
+
.map((x) => ({
|
|
829
|
+
pid: Number(x?.ProcessId) || 0,
|
|
830
|
+
name: String(x?.Name ?? ""),
|
|
831
|
+
cmdline: String(x?.CommandLine ?? ""),
|
|
832
|
+
}))
|
|
833
|
+
.filter((x) => x.pid > 0);
|
|
834
|
+
}
|
|
835
|
+
const r = spawnSync("ps", ["-eo", "pid=,ppid=,args="], { encoding: "utf8", timeout: 4000 });
|
|
836
|
+
if (r.status !== 0)
|
|
837
|
+
return [];
|
|
838
|
+
const out = [];
|
|
839
|
+
for (const line of String(r.stdout ?? "").split("\n")) {
|
|
840
|
+
const m = /^\s*(\d+)\s+(\d+)\s+(.+)$/.exec(line);
|
|
841
|
+
if (!m || Number(m[2]) !== ppid)
|
|
842
|
+
continue;
|
|
843
|
+
const cmdline = m[3].trim();
|
|
844
|
+
out.push({ pid: Number(m[1]), name: path.basename(cmdline.split(/\s+/)[0] ?? ""), cmdline });
|
|
845
|
+
}
|
|
846
|
+
return out;
|
|
847
|
+
}
|
|
848
|
+
/** The OS pid behind a playwright-launched browser.
|
|
849
|
+
*
|
|
850
|
+
* playwright-core has NO `Browser.process()` — that accessor belongs to
|
|
851
|
+
* ElectronApplication and BrowserServer. Measured on the desktop host with a
|
|
852
|
+
* real 1.63 session: `typeof browser.process === "undefined"`, so the pid
|
|
853
|
+
* stayed 0, the orphan ledger recorded nothing, and close printed 已确认关闭
|
|
854
|
+
* over a browser process it had never checked.
|
|
855
|
+
*
|
|
856
|
+
* The only exact anchor available is that the browser is a DIRECT CHILD of
|
|
857
|
+
* this process, started from the executable WE resolved. That matters: this
|
|
858
|
+
* machine carries a dozen unrelated msedge trees (the user's own windows,
|
|
859
|
+
* msedgewebview2 under SearchHost.exe and under a vendor utility), so matching
|
|
860
|
+
* by process name would be a loaded gun pointed at them.
|
|
861
|
+
*
|
|
862
|
+
* 0 = not found, and every caller treats 0 as "no verification" — never as
|
|
863
|
+
* "verified gone".
|
|
864
|
+
*/
|
|
865
|
+
export function launchedBrowserPid(executablePath, opts = {}) {
|
|
866
|
+
const exe = String(executablePath ?? "").trim();
|
|
867
|
+
const base = path.basename(exe).toLowerCase();
|
|
868
|
+
if (!base)
|
|
869
|
+
return 0;
|
|
870
|
+
let rows;
|
|
871
|
+
try {
|
|
872
|
+
rows = opts.rows ?? childProcRows(opts.ppid ?? process.pid);
|
|
873
|
+
}
|
|
874
|
+
catch {
|
|
875
|
+
return 0;
|
|
876
|
+
}
|
|
877
|
+
const mine = (rows ?? []).filter((r) => Number.isFinite(Number(r?.pid)) && Number(r.pid) > 0);
|
|
878
|
+
const byPath = mine.filter((r) => String(r.cmdline ?? "").toLowerCase().includes(exe.toLowerCase()));
|
|
879
|
+
if (byPath.length === 1)
|
|
880
|
+
return Number(byPath[0].pid);
|
|
881
|
+
// More than one of OUR children from the same executable = an older session
|
|
882
|
+
// that has not been reaped yet. pids climb, so the highest is this launch.
|
|
883
|
+
if (byPath.length > 1)
|
|
884
|
+
return Math.max(...byPath.map((r) => Number(r.pid)));
|
|
885
|
+
// Name fallback, and only for rows where the OS gave us NO command line at
|
|
886
|
+
// all. A row that carries a command line for some OTHER executable is
|
|
887
|
+
// evidence against, not a missing clue — adopting it would mean signalling a
|
|
888
|
+
// browser we never launched.
|
|
889
|
+
const byName = mine.filter((r) => String(r.cmdline ?? "").trim() === "" && String(r.name ?? "").toLowerCase() === base);
|
|
890
|
+
return byName.length === 1 ? Number(byName[0].pid) : 0;
|
|
891
|
+
}
|
|
416
892
|
/** Consent is per BROWSER SESSION, not per call: one dialog when the lead
|
|
417
893
|
* decides to script the page, then the round stops paying for it. */
|
|
418
894
|
export function needsEvalConsent(policy, alreadyApproved) {
|
|
419
895
|
return policy !== "off" && !alreadyApproved;
|
|
420
896
|
}
|
|
897
|
+
/** A function-shaped source has to be INVOKED. Every browser MCP documents
|
|
898
|
+
* `function: "() => …"`, but evaluate() treats a STRING as an expression: a
|
|
899
|
+
* function source evaluates to a function object, which is not serializable,
|
|
900
|
+
* so the call resolves to undefined and the agent reads "null" and goes
|
|
901
|
+
* looking for a bug that is in our call rather than in the page (measured
|
|
902
|
+
* live 2026-09-21 — six rounds lost this way). An expression the model
|
|
903
|
+
* already wrapped, or a bare `document.title`, passes through untouched. */
|
|
904
|
+
export function evalExpression(src) {
|
|
905
|
+
const t = src.trim();
|
|
906
|
+
if (!t)
|
|
907
|
+
return t;
|
|
908
|
+
const fnLike = /^(async\s+)?function\b/.test(t) ||
|
|
909
|
+
/^(async\s+)?\(\s*[^()]*?\s*\)\s*=>/.test(t) ||
|
|
910
|
+
/^(async\s+)?[A-Za-z_$][\w$]*\s*=>/.test(t);
|
|
911
|
+
return fnLike ? `(${t})()` : t;
|
|
912
|
+
}
|
|
913
|
+
/** `null` is not a diagnosis. The render names the type, so "the page has no
|
|
914
|
+
* such links" and "your function returned nothing" and "it was not called"
|
|
915
|
+
* stop looking like the same answer. */
|
|
916
|
+
export function renderEvalResult(value) {
|
|
917
|
+
if (value === undefined) {
|
|
918
|
+
return ("执行结果:undefined —— 函数没有 return 值(或返回了不可序列化的对象,比如 DOM 节点本身)。" +
|
|
919
|
+
"要拿数据就返回字符串或数组,例:() => [...document.querySelectorAll('a')].map(a => a.href)");
|
|
920
|
+
}
|
|
921
|
+
if (value === null)
|
|
922
|
+
return "执行结果:null(页面确实返回了 null —— 这是成功执行,不是失败)";
|
|
923
|
+
const kind = Array.isArray(value) ? `array(${value.length})` : typeof value;
|
|
924
|
+
const json = JSON.stringify(value) ?? String(value);
|
|
925
|
+
return `执行结果(${kind}):${json}`;
|
|
926
|
+
}
|
|
421
927
|
/** Host only — the consent line in the dialog and in the trajectory never
|
|
422
928
|
* carries a query string (a search URL can hold the very token we guard). */
|
|
423
929
|
export function hostOnly(url) {
|
|
@@ -428,6 +934,133 @@ export function hostOnly(url) {
|
|
|
428
934
|
return url.slice(0, 60);
|
|
429
935
|
}
|
|
430
936
|
}
|
|
937
|
+
const CLICK_EFFECT_BUDGET_MS = 900;
|
|
938
|
+
const CLICK_POLL_MS = 150;
|
|
939
|
+
const CLICK_SETTLE_MS = 5000;
|
|
940
|
+
/** Runs in the page. Kept free of TS annotations and outer-scope references
|
|
941
|
+
* because playwright serializes it. */
|
|
942
|
+
const clickProbeFn = (el) => {
|
|
943
|
+
const attrs = {};
|
|
944
|
+
for (const a of ["aria-expanded", "aria-checked", "aria-selected", "aria-pressed", "disabled"]) {
|
|
945
|
+
const v = el.getAttribute ? el.getAttribute(a) : null;
|
|
946
|
+
if (v !== null && v !== undefined)
|
|
947
|
+
attrs[a] = String(v);
|
|
948
|
+
}
|
|
949
|
+
if (typeof el.value === "string")
|
|
950
|
+
attrs.value = el.value;
|
|
951
|
+
return {
|
|
952
|
+
attrs,
|
|
953
|
+
url: String(location.href || ""),
|
|
954
|
+
ready: String(document.readyState || ""),
|
|
955
|
+
nodes: document.getElementsByTagName("*").length,
|
|
956
|
+
};
|
|
957
|
+
};
|
|
958
|
+
async function probeClickTarget(loc) {
|
|
959
|
+
if (typeof loc.evaluate !== "function")
|
|
960
|
+
return null;
|
|
961
|
+
try {
|
|
962
|
+
const raw = (await loc.evaluate(clickProbeFn));
|
|
963
|
+
if (!raw || typeof raw !== "object")
|
|
964
|
+
return null;
|
|
965
|
+
return {
|
|
966
|
+
attrs: (raw.attrs ?? {}),
|
|
967
|
+
url: String(raw.url ?? ""),
|
|
968
|
+
ready: String(raw.ready ?? ""),
|
|
969
|
+
nodes: Number(raw.nodes ?? 0),
|
|
970
|
+
};
|
|
971
|
+
}
|
|
972
|
+
catch {
|
|
973
|
+
return null; // a detached element is an answer, not a crash
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
/** Bounded settle. `open` deliberately returns at domcontentloaded — reading a
|
|
977
|
+
* page must not wait for analytics — so the wait belongs to the action that
|
|
978
|
+
* needs a handler attached, not to every navigation. */
|
|
979
|
+
async function settlePage(page, timeoutMs = CLICK_SETTLE_MS) {
|
|
980
|
+
const waiting = typeof page.waitForLoadState === "function" ? page.waitForLoadState("load", { timeout: timeoutMs }) : null;
|
|
981
|
+
if (waiting) {
|
|
982
|
+
await Promise.race([
|
|
983
|
+
Promise.resolve(waiting).catch(() => undefined),
|
|
984
|
+
new Promise((r) => setTimeout(r, timeoutMs)),
|
|
985
|
+
]);
|
|
986
|
+
}
|
|
987
|
+
await new Promise((r) => setTimeout(r, 250));
|
|
988
|
+
}
|
|
989
|
+
/** What differs between two probes, in the order a reader cares about. */
|
|
990
|
+
export function describeClickChange(before, after) {
|
|
991
|
+
const out = [];
|
|
992
|
+
const attrsBefore = before.attrs ?? {};
|
|
993
|
+
const attrsAfter = after.attrs ?? {};
|
|
994
|
+
for (const k of Object.keys(attrsBefore)) {
|
|
995
|
+
if (attrsAfter[k] !== attrsBefore[k])
|
|
996
|
+
out.push(`${k}: ${attrsBefore[k]} → ${attrsAfter[k] ?? "(消失)"}`);
|
|
997
|
+
}
|
|
998
|
+
for (const k of Object.keys(attrsAfter)) {
|
|
999
|
+
if (!(k in attrsBefore))
|
|
1000
|
+
out.push(`${k}: → ${attrsAfter[k]}`);
|
|
1001
|
+
}
|
|
1002
|
+
if (before.url && after.url && before.url !== after.url)
|
|
1003
|
+
out.push(`已跳转 → ${after.url.slice(0, 120)}`);
|
|
1004
|
+
if (before.nodes && after.nodes !== before.nodes)
|
|
1005
|
+
out.push(`DOM 节点 ${before.nodes} → ${after.nodes}`);
|
|
1006
|
+
return out;
|
|
1007
|
+
}
|
|
1008
|
+
/** Poll until something changed or the budget runs out. A navigating click can
|
|
1009
|
+
* detach the element, so the URL is checked independently of the probe. */
|
|
1010
|
+
async function observeClickEffect(loc, page, before, budgetMs = CLICK_EFFECT_BUDGET_MS) {
|
|
1011
|
+
const deadline = Date.now() + Math.max(0, budgetMs);
|
|
1012
|
+
for (;;) {
|
|
1013
|
+
const after = await probeClickTarget(loc);
|
|
1014
|
+
if (after) {
|
|
1015
|
+
const changed = describeClickChange(before, after);
|
|
1016
|
+
if (changed.length)
|
|
1017
|
+
return { changed, after };
|
|
1018
|
+
}
|
|
1019
|
+
else {
|
|
1020
|
+
const urlNow = typeof page.url === "function" ? String(page.url() ?? "") : "";
|
|
1021
|
+
if (urlNow && before.url && urlNow !== before.url)
|
|
1022
|
+
return { changed: [`已跳转 → ${urlNow.slice(0, 120)}`], after: null };
|
|
1023
|
+
}
|
|
1024
|
+
if (Date.now() >= deadline)
|
|
1025
|
+
return { changed: [], after };
|
|
1026
|
+
await new Promise((r) => setTimeout(r, CLICK_POLL_MS));
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
/** The reply. Pure, so the wording is pinnable without a browser: a click that
|
|
1030
|
+
* observably did something says what changed; one that did not says so and
|
|
1031
|
+
* names the next move instead of reporting success. */
|
|
1032
|
+
export function clickVerdict(target, before, changed, opts) {
|
|
1033
|
+
const head = `已点击 ${target}`;
|
|
1034
|
+
const dialogNote = opts.dialog ? ";有未处理对话框——handle_dialog(同轮观察,勿另起动作)" : "";
|
|
1035
|
+
if (!before)
|
|
1036
|
+
return head + dialogNote; // nothing could be probed: claim no more than we know
|
|
1037
|
+
if (changed.length) {
|
|
1038
|
+
const retryNote = opts.retried
|
|
1039
|
+
? `(第一次点击落在页面还没就绪时 readyState=${before.ready || "?"},未观测到变化;等加载完重试才生效)`
|
|
1040
|
+
: "";
|
|
1041
|
+
return `${head} · ${changed.join(" · ")}${retryNote}${dialogNote}`;
|
|
1042
|
+
}
|
|
1043
|
+
const watched = Object.keys(before.attrs).length
|
|
1044
|
+
? Object.entries(before.attrs).map(([k, v]) => `${k}=${v}`).join(" ")
|
|
1045
|
+
: "无可观测状态属性";
|
|
1046
|
+
const facts = `${watched} · URL 未变 · DOM 节点 ${before.nodes} 未变 · readyState=${before.ready || "?"}`;
|
|
1047
|
+
// Three different reasons produce the same "nothing changed", and the advice
|
|
1048
|
+
// that fits one of them is a dead end for the others. Measured live: a page
|
|
1049
|
+
// at readyState=complete whose framework bundles the governance gate had
|
|
1050
|
+
// trimmed was told to "wait for the text" — wait_for then threw a strict-mode
|
|
1051
|
+
// violation, so the recommended way out led nowhere.
|
|
1052
|
+
const why = opts.blocked && opts.blocked.count > 0
|
|
1053
|
+
? `最可能的原因不是"还没加载完",而是**这个页面的脚本被治理闸门拦掉了**:本次有 ${opts.blocked.count} 个子资源被拦截` +
|
|
1054
|
+
`${opts.blocked.hosts.length ? `(${opts.blocked.hosts.join(", ")})` : ""},而它们的可执行资源不属于你导航过的那个站。` +
|
|
1055
|
+
`出路:请操作者把这些主机加进 TM_WEBFETCH_ALLOWED_DOMAINS(或先在确认窗里批准该站点)后重新 open;` +
|
|
1056
|
+
`也可以直接 TM_BROWSER_SUBRESOURCE=off 之外逐案判断——总之现在这个按钮上没有 handler。`
|
|
1057
|
+
: before.ready === "complete"
|
|
1058
|
+
? `页面已经加载完成(readyState=complete),所以这不是加载时机问题:这个元素本身不响应这种点击(常见于 <a> 需要 navigate、事件绑在父节点、或被上层遮罩)。` +
|
|
1059
|
+
`下一步:重新 take_snapshot 看清结构再决定,或改用 navigate / evaluate_script 直接取你要的东西。`
|
|
1060
|
+
: `最常见的原因是页面脚本还没跑完(readyState=${before.ready || "?"}),而 playwright 的可见/稳定/可点检查在这种情况下全部通过。` +
|
|
1061
|
+
`下一步:wait_for 你要点的文字,或重新 take_snapshot 再点一次。`;
|
|
1062
|
+
return `${head},但页面没有任何可观测变化(${facts})。点击送达了,可是没有 handler 响应。${why}不要把这次点击当成成功。${dialogNote}`;
|
|
1063
|
+
}
|
|
431
1064
|
class PipeCdp {
|
|
432
1065
|
child;
|
|
433
1066
|
nextId = 1;
|
|
@@ -608,8 +1241,8 @@ export class SnapshotIndex {
|
|
|
608
1241
|
export function unescapeAriaName(raw) {
|
|
609
1242
|
return String(raw).replace(/\\(.)/g, (_, c) => (c === "s" ? " " : c === "n" ? "\n" : c === "t" ? "\t" : c));
|
|
610
1243
|
}
|
|
611
|
-
// ---------- action surface (R2: chrome-devtools-mcp
|
|
612
|
-
/** The
|
|
1244
|
+
// ---------- action surface (R2: chrome-devtools-mcp 18-verb alignment) --------
|
|
1245
|
+
/** The 18 canonical verbs, named byte-identical to chrome-devtools-mcp. */
|
|
613
1246
|
export const BROWSER_PLAYWRIGHT_ACTIONS = [
|
|
614
1247
|
"navigate_page",
|
|
615
1248
|
"take_snapshot",
|
|
@@ -619,6 +1252,8 @@ export const BROWSER_PLAYWRIGHT_ACTIONS = [
|
|
|
619
1252
|
"drag",
|
|
620
1253
|
"press_key",
|
|
621
1254
|
"select_page",
|
|
1255
|
+
"new_page",
|
|
1256
|
+
"close_page",
|
|
622
1257
|
"upload_file",
|
|
623
1258
|
"wait_for",
|
|
624
1259
|
"evaluate_script",
|
|
@@ -628,8 +1263,26 @@ export const BROWSER_PLAYWRIGHT_ACTIONS = [
|
|
|
628
1263
|
"take_screenshot",
|
|
629
1264
|
"handle_dialog",
|
|
630
1265
|
];
|
|
631
|
-
/** Legacy compat verbs that ride on top of the
|
|
1266
|
+
/** Legacy compat verbs that ride on top of the 18 (both engines). */
|
|
632
1267
|
export const BROWSER_COMPAT_ACTIONS = ["open", "navigate", "read", "screenshot", "close"];
|
|
1268
|
+
/** Verbs of our own that act on the GATE rather than the DOM: no page is
|
|
1269
|
+
* involved, so both engines serve them and neither can drift. */
|
|
1270
|
+
export const BROWSER_GATE_ACTIONS = ["allow_host"];
|
|
1271
|
+
/** Every verb the execute layer accepts — the ONE table the refusal menu and
|
|
1272
|
+
* the args schema are both built from. The hand-written copies drifted the
|
|
1273
|
+
* day `new_page` landed (§6o/unknown-action menu), and a schema that omits a
|
|
1274
|
+
* verb is a capability no agent knows it has. */
|
|
1275
|
+
export const ALL_BROWSER_ACTIONS = [
|
|
1276
|
+
...BROWSER_PLAYWRIGHT_ACTIONS,
|
|
1277
|
+
...BROWSER_COMPAT_ACTIONS,
|
|
1278
|
+
...BROWSER_GATE_ACTIONS,
|
|
1279
|
+
];
|
|
1280
|
+
export const BROWSER_ACTION_MENU = ALL_BROWSER_ACTIONS.join(" | ");
|
|
1281
|
+
/** The verbs that must still WORK when a browser has zero tabs: `new_page` is
|
|
1282
|
+
* the remedy the error text points at, `list_pages` is how you find out you
|
|
1283
|
+
* are at zero, and `close_page` answers with its own "index 越界(0..-1)"
|
|
1284
|
+
* rather than this sentence. */
|
|
1285
|
+
const ZERO_TAB_SAFE = new Set(["new_page", "list_pages", "close_page"]);
|
|
633
1286
|
/** What the cdp-legacy fallback can still do (R2: degradation must keep the
|
|
634
1287
|
* §6o behavior; everything snapshot-shaped is playwright-only). */
|
|
635
1288
|
export const BROWSER_LEGACY_ACTIONS = [
|
|
@@ -648,23 +1301,144 @@ export const BROWSER_LEGACY_ACTIONS = [
|
|
|
648
1301
|
const PW_DEFAULT_TIMEOUT_MS = 3000;
|
|
649
1302
|
const OBSERVE_BUFFER_CAP = 200;
|
|
650
1303
|
const HARDENED_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
|
|
1304
|
+
// The browser's OWN site-permission bubble ("…想要 访问此设备上的其他应用和服务",
|
|
1305
|
+
// 阻止/允许) is not our ctx.ask channel, so nothing in perm-ask or the approval
|
|
1306
|
+
// gate can bound it: measured on npmjs.com, an unanswered bubble held
|
|
1307
|
+
// `page.goto` past its 45 s timeout without even reaching domcontentloaded, and
|
|
1308
|
+
// a server-side session has nobody to click. Denying at launch is the safe
|
|
1309
|
+
// direction and the ONLY one this toolset takes — there is deliberately no
|
|
1310
|
+
// auto-allow counterpart (no `permissions:` in the context options either),
|
|
1311
|
+
// because a browser that grants a site device access on the plugin's say-so
|
|
1312
|
+
// would be the self-allowing the rest of this codebase refuses to do.
|
|
1313
|
+
export const BROWSER_DENY_PERMISSIONS_ARG = "--deny-permission-prompts";
|
|
651
1314
|
const SPAWN_TIMEOUT_MS = 15_000;
|
|
652
1315
|
const NAVIGATE_EVENT_TIMEOUT_MS = 15_000;
|
|
1316
|
+
export function callerOf(ctx) {
|
|
1317
|
+
const c = (ctx ?? {});
|
|
1318
|
+
return { owner: String(c.sessionID ?? "").trim(), agent: String(c.agent ?? "").trim() };
|
|
1319
|
+
}
|
|
653
1320
|
export function buildTmBrowserTool(deps) {
|
|
654
1321
|
const { pipelines } = deps;
|
|
655
1322
|
const env = deps.env ?? process.env;
|
|
656
1323
|
const notify = typeof deps.notify === "function" ? deps.notify : undefined;
|
|
657
1324
|
const tool = "tm_browser";
|
|
658
1325
|
const traj = (e) => pipelines.store.appendTrajectory({ tool, ...e });
|
|
659
|
-
|
|
1326
|
+
// The orphan ledger lives beside the run store (per workspace since the
|
|
1327
|
+
// tmpdir sharding), so a crash in one project can never reap another's
|
|
1328
|
+
// browser — and a test run, which gets its own store dir, never reaches a
|
|
1329
|
+
// real one either.
|
|
1330
|
+
const ledgerFile = (() => {
|
|
1331
|
+
const root = pipelines.store?.blackboardRoot;
|
|
1332
|
+
// No store root (a test double, or a host that never gave us one) means no
|
|
1333
|
+
// ledger — and then nothing is recorded and nothing is reaped. It must not
|
|
1334
|
+
// throw on the way to opening a browser.
|
|
1335
|
+
return typeof root === "string" && root ? browserLedgerFile(root) : "";
|
|
1336
|
+
})();
|
|
1337
|
+
const recordBrowser = (pid, engine, exe) => {
|
|
1338
|
+
if (!ledgerFile || !Number.isFinite(pid) || pid <= 0)
|
|
1339
|
+
return;
|
|
1340
|
+
appendBrowserLedger(ledgerFile, { pid, ownerPid: process.pid, engine, at: Date.now(), exe });
|
|
1341
|
+
};
|
|
1342
|
+
if (ledgerFile && String(env.TM_BROWSER_REAP ?? "on").trim().toLowerCase() !== "off") {
|
|
1343
|
+
const r = reapOrphanBrowsers(ledgerFile);
|
|
1344
|
+
if (r.reaped.length)
|
|
1345
|
+
traj({ step_id: "browser", event: "orphans_reaped", count: r.reaped.length, pids: r.reaped.join(",") });
|
|
1346
|
+
}
|
|
1347
|
+
/** Live browser leases by id — ONE browser per caller (see BrowserLease). */
|
|
1348
|
+
const leases = new Map();
|
|
1349
|
+
let leaseSeq = 0;
|
|
1350
|
+
const liveLeases = () => [...leases.values()];
|
|
1351
|
+
const ownedBy = (owner) => liveLeases().filter((l) => l.owner === owner);
|
|
1352
|
+
const leaseTable = () => liveLeases()
|
|
1353
|
+
.map((l) => `${l.id} · ${l.agent || "未知角色"} · ${String(l.session.currentUrl || "").slice(0, 80) || "空白页"}`)
|
|
1354
|
+
.join("\n ");
|
|
1355
|
+
/** Idle-reaper timers, one per lease: a forgotten window belongs to its
|
|
1356
|
+
* owner, so B's activity must not keep A's browser alive (or reap it). */
|
|
1357
|
+
const idleTimers = new Map();
|
|
1358
|
+
function clearIdle(id) {
|
|
1359
|
+
const t = idleTimers.get(id);
|
|
1360
|
+
if (t)
|
|
1361
|
+
clearTimeout(t);
|
|
1362
|
+
idleTimers.delete(id);
|
|
1363
|
+
}
|
|
1364
|
+
/** Drop the lease whose session died on its own (a cdp-legacy child exiting,
|
|
1365
|
+
* a browser the user closed) so a corpse is never handed back out. */
|
|
1366
|
+
const dropSession = (sess) => {
|
|
1367
|
+
for (const [id, l] of leases) {
|
|
1368
|
+
if (l.session === sess) {
|
|
1369
|
+
leases.delete(id);
|
|
1370
|
+
clearIdle(id);
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
};
|
|
660
1374
|
// m2 (fix round): SAME seed policy as tm_webfetch / tm_search — the
|
|
661
1375
|
// built-in default list gains the T4 engine home hosts (api.stackexchange
|
|
662
1376
|
// .com, hn.algolia.com) so jump-reading a search hit stops re-popping the
|
|
663
1377
|
// dialog; a narrowed TM_WEBFETCH_ALLOWED_DOMAINS is never widened.
|
|
664
1378
|
const allowlist = seedWebfetchDomains(deps.cfg.webfetchAllowedDomains ?? ["*"]);
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
1379
|
+
/** Hosts approved through the OFFICIAL dialog, keyed by the CALLER's session.
|
|
1380
|
+
* It used to be one process-wide set, which meant a host the lead approved
|
|
1381
|
+
* became a silent pass for the researcher's browser too — consent given to
|
|
1382
|
+
* one agent is not a pass for another. The network layer (both engines)
|
|
1383
|
+
* consults the caller's set in addition to the static allowlist. */
|
|
1384
|
+
const approvedBy = new Map();
|
|
1385
|
+
const approvedFor = (owner) => {
|
|
1386
|
+
let s = approvedBy.get(owner);
|
|
1387
|
+
if (!s) {
|
|
1388
|
+
s = new Set();
|
|
1389
|
+
approvedBy.set(owner, s);
|
|
1390
|
+
}
|
|
1391
|
+
return s;
|
|
1392
|
+
};
|
|
1393
|
+
/** Which browser an action may drive.
|
|
1394
|
+
*
|
|
1395
|
+
* The id is REQUIRED as soon as it could mean more than one thing: with a
|
|
1396
|
+
* single live browser that belongs to the caller, omitting it is unambiguous
|
|
1397
|
+
* and allowed (and every reply echoes the id back); the moment a second
|
|
1398
|
+
* agent opens one, every call must name its own. An id belonging to somebody
|
|
1399
|
+
* else is refused WITH the owner named, because `b1` is guessable and the
|
|
1400
|
+
* whole point is that guessing must not work. */
|
|
1401
|
+
function resolveLease(args, caller) {
|
|
1402
|
+
const want = String(args.id ?? "").trim();
|
|
1403
|
+
const all = liveLeases();
|
|
1404
|
+
const mine = ownedBy(caller.owner);
|
|
1405
|
+
if (want) {
|
|
1406
|
+
const hit = all.find((l) => l.id === want);
|
|
1407
|
+
if (!hit) {
|
|
1408
|
+
return {
|
|
1409
|
+
error: all.length
|
|
1410
|
+
? `没有 id 为 "${want}" 的浏览器。活着的:\n ${leaseTable()}`
|
|
1411
|
+
: `没有 id 为 "${want}" 的浏览器——现在一个都没开,先用 action:"open"。`,
|
|
1412
|
+
};
|
|
1413
|
+
}
|
|
1414
|
+
if (hit.owner !== caller.owner) {
|
|
1415
|
+
return {
|
|
1416
|
+
error: `${hit.id} 是 ${hit.agent || "另一个会话"} 的浏览器,不是你的——两个 agent 共用一个窗口会互相踩当前标签和 uid 编号,` +
|
|
1417
|
+
`而你看到的每一次"成功"都可能是对方的页面。` +
|
|
1418
|
+
(mine.length
|
|
1419
|
+
? `你自己的是 ${mine.map((l) => l.id).join(" / ")}。`
|
|
1420
|
+
: `你还没有浏览器:用 action:"open" 开一个,它会给你自己的 id。`),
|
|
1421
|
+
};
|
|
1422
|
+
}
|
|
1423
|
+
hit.lastActivity = Date.now();
|
|
1424
|
+
return { lease: hit };
|
|
1425
|
+
}
|
|
1426
|
+
if (all.length === 1 && mine.length === 1) {
|
|
1427
|
+
mine[0].lastActivity = Date.now();
|
|
1428
|
+
return { lease: mine[0] };
|
|
1429
|
+
}
|
|
1430
|
+
if (!mine.length) {
|
|
1431
|
+
return {
|
|
1432
|
+
error: all.length
|
|
1433
|
+
? `你还没有浏览器(活着的:\n ${leaseTable()})——先用 action:"open" 开一个,之后带上它给你的 id。`
|
|
1434
|
+
: '没有打开的浏览器会话——先用 action:"open"',
|
|
1435
|
+
};
|
|
1436
|
+
}
|
|
1437
|
+
return {
|
|
1438
|
+
error: `现在有 ${all.length} 个浏览器在跑,动作必须写明 id(你自己的:${mine.map((l) => l.id).join(" / ")})——` +
|
|
1439
|
+
`省略 id 只在"全场只有一个、且是你的"时才安全。\n ${leaseTable()}`,
|
|
1440
|
+
};
|
|
1441
|
+
}
|
|
668
1442
|
const snapshotBudget = Math.max(10, Number(deps.cfg?.browserSnapshotMaxTokens) || 1200);
|
|
669
1443
|
const subPolicy = deps.cfg?.browserSubresource ?? "same-site";
|
|
670
1444
|
const evalAskPolicy = deps.cfg?.browserAskEval ?? "on";
|
|
@@ -764,8 +1538,15 @@ export function buildTmBrowserTool(deps) {
|
|
|
764
1538
|
}
|
|
765
1539
|
/** One line telling the agent that the page is NOT actually empty — the
|
|
766
1540
|
* subresource gate dropped N requests. Without this the model reads a
|
|
767
|
-
* blank-looking page and reports "该网站没有图片".
|
|
768
|
-
|
|
1541
|
+
* blank-looking page and reports "该网站没有图片".
|
|
1542
|
+
*
|
|
1543
|
+
* `thin` means the page ALSO came back with nothing addressable, which
|
|
1544
|
+
* changes the sentence from a footnote into the diagnosis: on baike the
|
|
1545
|
+
* blocked request was the site's own bundle, so 0 nodes and "no content"
|
|
1546
|
+
* were our doing. An agent that is told the causality has two real moves
|
|
1547
|
+
* (allow_host, or the env seed + restart); one that is not, reports the
|
|
1548
|
+
* site as empty. */
|
|
1549
|
+
function blockedNote(sess, thin = false) {
|
|
769
1550
|
if (!sess.blocked.count)
|
|
770
1551
|
return "";
|
|
771
1552
|
const n = sess.blocked.count;
|
|
@@ -775,9 +1556,28 @@ export function buildTmBrowserTool(deps) {
|
|
|
775
1556
|
// drain (and the note would never appear again for the session).
|
|
776
1557
|
sess.blocked.count = 0;
|
|
777
1558
|
sess.blocked.hosts.clear();
|
|
778
|
-
traj({ step_id: "browser", event: "blocked", count: n, hosts: hosts.slice(0, 20).join(",") });
|
|
779
|
-
|
|
780
|
-
|
|
1559
|
+
traj({ step_id: "browser", event: "blocked", count: n, hosts: hosts.slice(0, 20).join(","), thin });
|
|
1560
|
+
const list = `${hosts.slice(0, 6).join(", ")}${hosts.length > 6 ? ` …+${hosts.length - 6}` : ""}`;
|
|
1561
|
+
const base = `\n注意:本页有 ${n} 个子资源请求被治理白名单拦截(${list})——不是站点没有内容。` +
|
|
1562
|
+
`当前策略 TM_BROWSER_SUBRESOURCE=${subPolicy};同域资源已自动放行,跨域脚本仍需白名单或弹窗批准。`;
|
|
1563
|
+
if (!thin)
|
|
1564
|
+
return base;
|
|
1565
|
+
return (base +
|
|
1566
|
+
`这一页的可寻址内容为 0,而被拦的正是页面自己的脚本域名——空白是我们的门禁造成的,不是页面没有内容。` +
|
|
1567
|
+
`两条出路:action:"allow_host" {host:"${hosts[0] ?? "被拦域名"}"} 由我拿官方确认窗去问用户(只放行这个域名、只对你这个浏览器、本次会话),` +
|
|
1568
|
+
`或请用户把域名加进 TM_WEBFETCH_ALLOWED_DOMAINS 后重启 OpenCode。批准后要重新 navigate 一次——门禁在请求时判定,已加载的页面不会自己补回来。` +
|
|
1569
|
+
`在拿到真实内容之前不要把这一页当成"网站没有内容"汇报。`);
|
|
1570
|
+
}
|
|
1571
|
+
/** A thin page may be a wall rather than an empty site. Say which, because
|
|
1572
|
+
* the next move is the opposite: retry an empty page, never a captcha. */
|
|
1573
|
+
function wallNote(...texts) {
|
|
1574
|
+
const wall = challengeWallOf(...texts);
|
|
1575
|
+
if (!wall)
|
|
1576
|
+
return "";
|
|
1577
|
+
traj({ step_id: "browser", event: "challenge_wall", sign: wall });
|
|
1578
|
+
return (`\n这一页不是"没有内容",它是一张${wall}墙——站点要求真人完成验证,自动化通道到此为止。` +
|
|
1579
|
+
`不要重试这一页来"等它加载完",也不要把"该站点无内容"写进结论:换来源(tm_search 的其它引擎 / 别的站点),` +
|
|
1580
|
+
`或者告诉用户需要他本人在这个窗口里完成一次验证后再继续。`);
|
|
781
1581
|
}
|
|
782
1582
|
function pageTextOutput(url, text) {
|
|
783
1583
|
return `页面文本(${url}):\n${text.slice(0, 20000)}${text.length > 20000 ? "\n…(截断)" : ""}`;
|
|
@@ -821,7 +1621,7 @@ export function buildTmBrowserTool(deps) {
|
|
|
821
1621
|
* under EVERY subresource policy: they are not consentable, so a passive
|
|
822
1622
|
* image or a same-site script can never relax them.
|
|
823
1623
|
*/
|
|
824
|
-
function gateDecide(st, url, resourceType) {
|
|
1624
|
+
function gateDecide(st, url, resourceType, approvedHosts) {
|
|
825
1625
|
const verdict = checkWebUrl(url, allowlist);
|
|
826
1626
|
if (!verdict.ok && !verdict.askable)
|
|
827
1627
|
return { pass: false, via: "red-line" };
|
|
@@ -858,11 +1658,18 @@ export function buildTmBrowserTool(deps) {
|
|
|
858
1658
|
// and flush as ONE line (blockedNote / close).
|
|
859
1659
|
return v;
|
|
860
1660
|
}
|
|
861
|
-
async function openLegacySession(headless) {
|
|
1661
|
+
async function openLegacySession(headless, approvedHosts) {
|
|
862
1662
|
const executable = discover(env);
|
|
863
1663
|
if (!executable)
|
|
864
1664
|
throw discoveryError();
|
|
865
|
-
|
|
1665
|
+
// The SAME knob on BOTH engines. This used to be temp-only, so a session
|
|
1666
|
+
// that degraded to cdp-legacy kept its logins nowhere while the reply still
|
|
1667
|
+
// read the env and announced 持久登录配置.
|
|
1668
|
+
const profile = classifyProfileDir(env);
|
|
1669
|
+
if (profile.refusal)
|
|
1670
|
+
throw new Error(profile.refusal);
|
|
1671
|
+
const persistent = profile.dir !== null;
|
|
1672
|
+
const profileDir = persistent ? profile.dir : fs.mkdtempSync(path.join(os.tmpdir(), "tm-browser-"));
|
|
866
1673
|
const child = spawn(executable, [
|
|
867
1674
|
headless ? "--headless=new" : "--start-maximized",
|
|
868
1675
|
"--remote-debugging-pipe",
|
|
@@ -873,6 +1680,7 @@ export function buildTmBrowserTool(deps) {
|
|
|
873
1680
|
"--accept-lang=zh-CN,zh;q=0.9,en;q=0.8",
|
|
874
1681
|
"--no-first-run", "--no-default-browser-check", "--disable-extensions",
|
|
875
1682
|
"--disable-background-networking", "--mute-audio",
|
|
1683
|
+
BROWSER_DENY_PERMISSIONS_ARG,
|
|
876
1684
|
"about:blank",
|
|
877
1685
|
], { stdio: ["ignore", "ignore", "ignore", "pipe", "pipe"], windowsHide: true });
|
|
878
1686
|
const cdp = new PipeCdp(child);
|
|
@@ -909,7 +1717,7 @@ export function buildTmBrowserTool(deps) {
|
|
|
909
1717
|
const requestId = String(m.params.requestId ?? "");
|
|
910
1718
|
const p = m.params.request ?? {};
|
|
911
1719
|
const url = String(p.url ?? "");
|
|
912
|
-
const { pass } = gateDecide({ allowedSites, blocked }, url, p.resourceType);
|
|
1720
|
+
const { pass } = gateDecide({ allowedSites, blocked }, url, p.resourceType, approvedHosts);
|
|
913
1721
|
if (pass) {
|
|
914
1722
|
void cdp.call("Fetch.continueRequest", { requestId }, sessionId, 5000).catch(() => { });
|
|
915
1723
|
}
|
|
@@ -917,14 +1725,18 @@ export function buildTmBrowserTool(deps) {
|
|
|
917
1725
|
void cdp.call("Fetch.failRequest", { requestId, errorReason: "BlockedByClient" }, sessionId, 5000).catch(() => { });
|
|
918
1726
|
}
|
|
919
1727
|
});
|
|
1728
|
+
recordBrowser(Number(child.pid ?? 0), "cdp-legacy", executable);
|
|
920
1729
|
const sess = {
|
|
921
1730
|
kind: "cdp-legacy",
|
|
922
1731
|
currentUrl: "about:blank",
|
|
923
1732
|
headless,
|
|
1733
|
+
persistentProfile: persistent ? profileDir : null,
|
|
924
1734
|
label: `cdp-legacy · ${path.basename(executable)} · pid ${child.pid ?? "?"}`,
|
|
925
1735
|
attachments: [],
|
|
926
1736
|
blocked,
|
|
927
1737
|
allowedSites,
|
|
1738
|
+
// the CDP child still running IS the browser being alive on this engine
|
|
1739
|
+
alive: () => child.exitCode === null,
|
|
928
1740
|
async act(action, args, stepId) {
|
|
929
1741
|
if (action === "navigate" || action === "navigate_page") {
|
|
930
1742
|
const url = String(args.url ?? "").trim();
|
|
@@ -939,7 +1751,17 @@ export function buildTmBrowserTool(deps) {
|
|
|
939
1751
|
const ev = await cdp.call("Runtime.evaluate", { expression: "document.body ? document.body.innerText : ''", returnByValue: true }, sessionId);
|
|
940
1752
|
const text = String(ev.result?.value ?? "");
|
|
941
1753
|
traj({ step_id: stepId, event: "result", tokens: Math.ceil(text.length / 4) });
|
|
942
|
-
|
|
1754
|
+
// Same diagnosis as the playwright leg — the two engines must not
|
|
1755
|
+
// drift on what a blank page is allowed to be called.
|
|
1756
|
+
const thin = text.replace(/\s+/g, "").length < 80;
|
|
1757
|
+
let title = "";
|
|
1758
|
+
if (thin) {
|
|
1759
|
+
const t = await cdp
|
|
1760
|
+
.call("Runtime.evaluate", { expression: "document.title", returnByValue: true }, sessionId)
|
|
1761
|
+
.catch(() => null);
|
|
1762
|
+
title = String(t?.result?.value ?? "");
|
|
1763
|
+
}
|
|
1764
|
+
return `${pageTextOutput(sess.currentUrl, text)}${wallNote(title, text)}${blockedNote(sess, thin)}`;
|
|
943
1765
|
}
|
|
944
1766
|
if (action === "screenshot" || action === "take_screenshot") {
|
|
945
1767
|
traj({ step_id: stepId, event: "call", kind: "screenshot", url: sess.currentUrl.slice(0, 200) });
|
|
@@ -962,9 +1784,17 @@ export function buildTmBrowserTool(deps) {
|
|
|
962
1784
|
if (!src)
|
|
963
1785
|
throw new Error('缺少 expression/function 参数(evaluate_script 需要一个 JS 表达式)');
|
|
964
1786
|
traj({ step_id: stepId, event: "call", kind: "evaluate" });
|
|
965
|
-
const ev = await cdp.call("Runtime.evaluate", { expression: src, returnByValue: true }, sessionId);
|
|
966
|
-
|
|
967
|
-
|
|
1787
|
+
const ev = await cdp.call("Runtime.evaluate", { expression: evalExpression(src), returnByValue: true, awaitPromise: true }, sessionId);
|
|
1788
|
+
// A page-side throw used to render as "执行结果:null", which is how an
|
|
1789
|
+
// agent spends six rounds wondering whether the selector was wrong.
|
|
1790
|
+
const det = ev?.exceptionDetails;
|
|
1791
|
+
if (det) {
|
|
1792
|
+
const first = String(det.exception?.description ?? det.text ?? JSON.stringify(det))
|
|
1793
|
+
.split("\n")[0]
|
|
1794
|
+
.slice(0, 300);
|
|
1795
|
+
throw new Error(`页面内抛出异常(不是"没找到"):${first}`);
|
|
1796
|
+
}
|
|
1797
|
+
return capTokens(renderEvalResult(ev.result?.value), "…(结果超长已截断)");
|
|
968
1798
|
}
|
|
969
1799
|
throw new Error(`action "${action}" 需要 playwright 引擎(当前为 cdp-legacy 降级:playwright-core 未安装或 node<20)。` +
|
|
970
1800
|
`cdp-legacy 可用: ${BROWSER_LEGACY_ACTIONS.filter((a) => a !== "open" && a !== "close").join(" | ")}`);
|
|
@@ -987,24 +1817,33 @@ export function buildTmBrowserTool(deps) {
|
|
|
987
1817
|
});
|
|
988
1818
|
});
|
|
989
1819
|
const dead = child.exitCode !== null;
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
1820
|
+
// Only a directory WE created is ours to remove. Deleting a user-named
|
|
1821
|
+
// persistent profile on close would destroy the logins the knob exists
|
|
1822
|
+
// to keep.
|
|
1823
|
+
if (!persistent) {
|
|
1824
|
+
try {
|
|
1825
|
+
rmForceSafe(profileDir, { recursive: true });
|
|
1826
|
+
}
|
|
1827
|
+
catch {
|
|
1828
|
+
/* a locked leftover temp dir is reclaimed by the OS — never fail close */
|
|
1829
|
+
}
|
|
995
1830
|
}
|
|
996
1831
|
return dead
|
|
997
|
-
? {
|
|
1832
|
+
? {
|
|
1833
|
+
closed: true,
|
|
1834
|
+
text: persistent
|
|
1835
|
+
? `浏览器进程已退出(cdp-legacy);持久配置目录已保留(下次 open 复用登录态):${profileDir}。`
|
|
1836
|
+
: "浏览器进程已退出(cdp-legacy),临时配置目录已清理。",
|
|
1837
|
+
}
|
|
998
1838
|
: {
|
|
999
1839
|
closed: false,
|
|
1000
1840
|
text: `警告:cdp-legacy 子进程 pid ${child.pid ?? "?"} 未在 3s 内退出,窗口可能仍在前台——` +
|
|
1001
|
-
|
|
1841
|
+
`请手动关闭该浏览器窗口(配置目录 ${profileDir}${persistent ? ",持久目录我不会删" : ",临时目录"})。`,
|
|
1002
1842
|
};
|
|
1003
1843
|
},
|
|
1004
1844
|
};
|
|
1005
1845
|
const onExit = () => {
|
|
1006
|
-
|
|
1007
|
-
session = null;
|
|
1846
|
+
dropSession(sess);
|
|
1008
1847
|
// best-effort: the profile may still be file-locked during shutdown —
|
|
1009
1848
|
// a leftover temp dir is reclaimed by the OS, never an error for the task
|
|
1010
1849
|
try {
|
|
@@ -1018,13 +1857,14 @@ export function buildTmBrowserTool(deps) {
|
|
|
1018
1857
|
return sess;
|
|
1019
1858
|
}
|
|
1020
1859
|
// ================= playwright engine (primary) ==============================
|
|
1021
|
-
async function openPlaywrightSession(pw, headless) {
|
|
1860
|
+
async function openPlaywrightSession(pw, headless, approvedHosts) {
|
|
1022
1861
|
const executable = discover(env);
|
|
1023
1862
|
if (!executable)
|
|
1024
1863
|
throw discoveryError();
|
|
1025
1864
|
const launchArgs = [
|
|
1026
1865
|
"--no-first-run", "--no-default-browser-check", "--disable-extensions",
|
|
1027
1866
|
"--disable-background-networking", "--mute-audio",
|
|
1867
|
+
BROWSER_DENY_PERMISSIONS_ARG,
|
|
1028
1868
|
...(headless ? [] : ["--start-maximized"]),
|
|
1029
1869
|
];
|
|
1030
1870
|
const contextOpts = {
|
|
@@ -1036,7 +1876,10 @@ export function buildTmBrowserTool(deps) {
|
|
|
1036
1876
|
// headful: honor --start-maximized (null viewport = the OS window size)
|
|
1037
1877
|
viewport: headless ? undefined : null,
|
|
1038
1878
|
};
|
|
1039
|
-
const
|
|
1879
|
+
const profile = classifyProfileDir(env);
|
|
1880
|
+
if (profile.refusal)
|
|
1881
|
+
throw new Error(profile.refusal);
|
|
1882
|
+
const persistentDir = profile.dir ?? "";
|
|
1040
1883
|
// R2 (revised 2026-09-18): playwright's `channel` resolves the executable
|
|
1041
1884
|
// ITSELF, which silently overrode our discovery (a Beta-default Windows
|
|
1042
1885
|
// host opened STABLE Edge). executablePath is now the primary launch and
|
|
@@ -1070,6 +1913,32 @@ export function buildTmBrowserTool(deps) {
|
|
|
1070
1913
|
context = await browser.newContext(contextOpts);
|
|
1071
1914
|
}
|
|
1072
1915
|
const snapIndex = new SnapshotIndex();
|
|
1916
|
+
// The OS process behind the connection. playwright exposes it as
|
|
1917
|
+
// browser.process(); on the persistent path the browser is reached through
|
|
1918
|
+
// the context. Without a pid there is no way to tell "the CDP connection
|
|
1919
|
+
// dropped" apart from "the browser is gone" — and they are NOT the same
|
|
1920
|
+
// thing (measured: close printed 已确认关闭 while the msedge tree was still
|
|
1921
|
+
// alive under OpenCode.exe).
|
|
1922
|
+
const owningBrowser = browser ??
|
|
1923
|
+
(() => {
|
|
1924
|
+
try {
|
|
1925
|
+
return context.browser?.() ?? null;
|
|
1926
|
+
}
|
|
1927
|
+
catch {
|
|
1928
|
+
return null;
|
|
1929
|
+
}
|
|
1930
|
+
})();
|
|
1931
|
+
const { pid: browserPid, via: pidRoute, failures: pidFailures } = await resolveLaunchPid({
|
|
1932
|
+
// playwright's own accessor first — it does not exist on 1.63, so the OS
|
|
1933
|
+
// scan is what answers today. Which route replied is AUDITED: a silent
|
|
1934
|
+
// fall-back to 0 would put us straight back to printing 已确认关闭 about a
|
|
1935
|
+
// process nobody looked at.
|
|
1936
|
+
playwrightPid: () => Number(owningBrowser?.process?.()?.pid ?? 0) || 0,
|
|
1937
|
+
executablePath: target.executablePath ?? executable,
|
|
1938
|
+
rows: () => (deps.listChildren ? deps.listChildren(process.pid) : undefined),
|
|
1939
|
+
});
|
|
1940
|
+
traj({ step_id: "browser", event: "launch_pid", via: pidRoute, pid: browserPid, tries: pidFailures });
|
|
1941
|
+
recordBrowser(browserPid, persistentDir ? "playwright-persistent" : "playwright", executable);
|
|
1073
1942
|
const state = {
|
|
1074
1943
|
page: context.pages()[0] ?? (await context.newPage()),
|
|
1075
1944
|
dialogs: [],
|
|
@@ -1130,7 +1999,7 @@ export function buildTmBrowserTool(deps) {
|
|
|
1130
1999
|
await route.continue().catch(() => { });
|
|
1131
2000
|
return;
|
|
1132
2001
|
}
|
|
1133
|
-
const { pass } = gateDecide(state, u, request.resourceType?.());
|
|
2002
|
+
const { pass } = gateDecide(state, u, request.resourceType?.(), approvedHosts);
|
|
1134
2003
|
if (pass) {
|
|
1135
2004
|
await route.continue().catch(() => { });
|
|
1136
2005
|
}
|
|
@@ -1185,11 +2054,26 @@ export function buildTmBrowserTool(deps) {
|
|
|
1185
2054
|
kind: "playwright",
|
|
1186
2055
|
currentUrl: "about:blank",
|
|
1187
2056
|
headless,
|
|
2057
|
+
persistentProfile: persistentDir || null,
|
|
1188
2058
|
label: `playwright/${via} · ${path.basename(executable)} · ${headless ? "headless" : "headful"}`,
|
|
1189
2059
|
attachments: [],
|
|
1190
2060
|
blocked: state.blocked,
|
|
1191
2061
|
allowedSites: state.allowedSites,
|
|
2062
|
+
// The browser object is the truth about liveness; a closed TAB is not it.
|
|
2063
|
+
alive: () => owningBrowser?.isConnected?.() !== false,
|
|
1192
2064
|
async act(action, args, stepId) {
|
|
2065
|
+
// A context with zero pages is ALIVE — it simply has no tab. Addressing
|
|
2066
|
+
// the tab close_page removed threw "Target page, context or browser has
|
|
2067
|
+
// been closed", which the recovery in execute() reads as a dead BROWSER:
|
|
2068
|
+
// the lease was dropped, `close` then reported "no browser open", and the
|
|
2069
|
+
// user kept a window nothing was claiming any more (measured live,
|
|
2070
|
+
// 1-6-0-click.json step 11 — right after our own reply promised
|
|
2071
|
+
// "浏览器仍在运行"). An empty window gets its own honest answer here and
|
|
2072
|
+
// keeps its lease.
|
|
2073
|
+
if (!ZERO_TAB_SAFE.has(action) && context.pages().length === 0) {
|
|
2074
|
+
throw new Error("这个浏览器已经没有标签页了(0 个,窗口和进程都还在,你的租约我没有丢)。" +
|
|
2075
|
+
'要继续用 new_page 在同一个窗口开一个标签;要结束整个会话用 action:"close"(它会验进程真的退出)。');
|
|
2076
|
+
}
|
|
1193
2077
|
if (action === "navigate" || action === "navigate_page") {
|
|
1194
2078
|
const url = String(args.url ?? "").trim();
|
|
1195
2079
|
// the navigation itself already cleared the allowlist (execute()
|
|
@@ -1205,7 +2089,9 @@ export function buildTmBrowserTool(deps) {
|
|
|
1205
2089
|
traj({ step_id: stepId, event: "call", url: sess.currentUrl.slice(0, 200) });
|
|
1206
2090
|
const text = String((await state.page.evaluate("document.body ? document.body.innerText : ''")) ?? "");
|
|
1207
2091
|
traj({ step_id: stepId, event: "result", tokens: Math.ceil(text.length / 4) });
|
|
1208
|
-
|
|
2092
|
+
const thin = text.replace(/\s+/g, "").length < 80;
|
|
2093
|
+
const title = thin ? await readTitle(state.page) : "";
|
|
2094
|
+
return `${pageTextOutput(sess.currentUrl, text)}${wallNote(title, text)}${blockedNote(sess, thin)}`;
|
|
1209
2095
|
}
|
|
1210
2096
|
if (action === "screenshot" || action === "take_screenshot") {
|
|
1211
2097
|
traj({ step_id: stepId, event: "call", kind: "screenshot", url: sess.currentUrl.slice(0, 200) });
|
|
@@ -1220,18 +2106,62 @@ export function buildTmBrowserTool(deps) {
|
|
|
1220
2106
|
yaml = await state.page.locator("body").ariaSnapshot();
|
|
1221
2107
|
}
|
|
1222
2108
|
catch (e) {
|
|
1223
|
-
|
|
2109
|
+
const m = String(e?.message ?? e);
|
|
2110
|
+
// Only talk about VERSIONS when the accessor itself is missing. It
|
|
2111
|
+
// threw "…has been closed" live and blamed playwright-core 1.63 for
|
|
2112
|
+
// it, which sent the agent off to check dependencies instead of
|
|
2113
|
+
// looking at the page — a wrong diagnosis costs more than none.
|
|
2114
|
+
throw new Error(/not a function|is not defined|unsupported|no method/i.test(m)
|
|
2115
|
+
? `ariaSnapshot 不可用:${m} —— locator.ariaSnapshot 需要 playwright-core ≥1.49(本包 optionalDependencies 锁 1.63);未安装或过旧会走 cdp-legacy 降级,那边没有快照动作。`
|
|
2116
|
+
: `快照读取失败:${m.slice(0, 300)}`);
|
|
1224
2117
|
}
|
|
1225
2118
|
const annotated = snapIndex.annotate(yaml);
|
|
1226
2119
|
traj({ step_id: stepId, event: "result", tokens: estimateTokens(annotated), nodes: snapIndex.size });
|
|
1227
2120
|
const body = capTokens(annotated, `…(快照超过 browserSnapshotMaxTokens=${snapshotBudget},后续行已截断——用 read 拿原文或先滚动再快照)`);
|
|
1228
2121
|
const hint = state.dialogs.length ? `\n注意:有 ${state.dialogs.length} 个未处理对话框——先 handle_dialog` : "";
|
|
1229
|
-
|
|
2122
|
+
// A snapshot with nothing addressable is the one case worth spending
|
|
2123
|
+
// a title read on: it is either our gate, or the site asking for a
|
|
2124
|
+
// human, and the agent has to be told which — "0 个可寻址节点" alone
|
|
2125
|
+
// reads as "this site is empty".
|
|
2126
|
+
const thin = snapIndex.size === 0;
|
|
2127
|
+
const wall = thin ? wallNote(await readTitle(state.page), yaml) : "";
|
|
2128
|
+
return `ARIA 快照(${sess.currentUrl} · ${snapIndex.size} 个可寻址节点)——后续动作按行内 [uid=eN] 寻址:\n${body}${hint}${wall}${blockedNote(sess, thin)}`;
|
|
1230
2129
|
}
|
|
1231
2130
|
if (action === "click") {
|
|
1232
2131
|
const loc = targetOf("uid", "selector", args);
|
|
2132
|
+
const target = describeTarget(args);
|
|
2133
|
+
const before = await probeClickTarget(loc);
|
|
1233
2134
|
await loc.click();
|
|
1234
|
-
|
|
2135
|
+
const dialogOpen = state.dialogs.length > 0;
|
|
2136
|
+
let seen = before ? await observeClickEffect(loc, state.page, before) : { changed: [], after: null };
|
|
2137
|
+
let retried = false;
|
|
2138
|
+
// A delivered click that changed NOTHING is the SPA hazard this
|
|
2139
|
+
// verification exists for. Retrying is safe precisely BECAUSE nothing
|
|
2140
|
+
// changed — a toggle that worked is never clicked twice — and a dialog
|
|
2141
|
+
// is open means the click did land, so it is never retried.
|
|
2142
|
+
if (before && !seen.changed.length && !dialogOpen) {
|
|
2143
|
+
await settlePage(state.page);
|
|
2144
|
+
const again = await probeClickTarget(loc);
|
|
2145
|
+
await loc.click().catch(() => { });
|
|
2146
|
+
retried = true;
|
|
2147
|
+
seen = await observeClickEffect(loc, state.page, again ?? before);
|
|
2148
|
+
}
|
|
2149
|
+
traj({
|
|
2150
|
+
step_id: stepId,
|
|
2151
|
+
event: "click_verified",
|
|
2152
|
+
effective: seen.changed.length > 0,
|
|
2153
|
+
retried,
|
|
2154
|
+
probed: Boolean(before),
|
|
2155
|
+
ready: String(before?.ready ?? ""),
|
|
2156
|
+
});
|
|
2157
|
+
return clickVerdict(target, before, seen.changed, {
|
|
2158
|
+
retried,
|
|
2159
|
+
dialog: state.dialogs.length > 0,
|
|
2160
|
+
// The gate's own ledger rides along: "nothing changed" on a page
|
|
2161
|
+
// whose scripts were trimmed needs a different next move than a page
|
|
2162
|
+
// that is still loading.
|
|
2163
|
+
blocked: { count: state.blocked.count, hosts: [...state.blocked.hosts].slice(0, 5) },
|
|
2164
|
+
});
|
|
1235
2165
|
}
|
|
1236
2166
|
if (action === "fill") {
|
|
1237
2167
|
const loc = targetOf("uid", "selector", args);
|
|
@@ -1277,17 +2207,24 @@ export function buildTmBrowserTool(deps) {
|
|
|
1277
2207
|
const what = uid ? `uid "${uid}"` : text ? `文本 "${text.slice(0, 80)}"` : "";
|
|
1278
2208
|
if (!uid && !text)
|
|
1279
2209
|
throw new Error('wait_for 需要 text 或 uid');
|
|
1280
|
-
const
|
|
2210
|
+
const base = uid ? targetOf("uid", "selector", { uid }) : state.page.getByText(text);
|
|
2211
|
+
// A visible label matches several nodes on a real page, and playwright's
|
|
2212
|
+
// STRICT mode answers that by throwing its whole internal report at the
|
|
2213
|
+
// agent — measured live, where `wait_for` was the very recovery step our
|
|
2214
|
+
// own click message had just recommended. So: wait for the first match
|
|
2215
|
+
// and SAY that there were several, instead of blocking the way out.
|
|
2216
|
+
const hits = typeof base.count === "function" ? Number(await base.count().catch(() => 1)) || 1 : 1;
|
|
2217
|
+
const loc = hits > 1 && typeof base.first === "function" ? base.first() : base;
|
|
1281
2218
|
await loc.waitFor({ state: "visible", timeout });
|
|
1282
|
-
return `等待命中:${what}(≤${timeout}ms
|
|
2219
|
+
return `等待命中:${what}(≤${timeout}ms 内可见)${hits > 1 ? `\n(这个文本命中 ${hits} 个元素,等的是第一个——要精确就改用 uid 寻址)` : ""}`;
|
|
1283
2220
|
}
|
|
1284
2221
|
if (action === "evaluate_script") {
|
|
1285
2222
|
const src = String(args.function ?? args.expression ?? "").trim();
|
|
1286
2223
|
if (!src)
|
|
1287
2224
|
throw new Error('缺少 expression/function 参数(JS 函数源或表达式)');
|
|
1288
2225
|
traj({ step_id: stepId, event: "call", kind: "evaluate" });
|
|
1289
|
-
const value = await state.page.evaluate(src);
|
|
1290
|
-
return capTokens(
|
|
2226
|
+
const value = await state.page.evaluate(evalExpression(src));
|
|
2227
|
+
return capTokens(renderEvalResult(value), "…(结果超长已截断)");
|
|
1291
2228
|
}
|
|
1292
2229
|
if (action === "list_pages") {
|
|
1293
2230
|
const pages = context.pages();
|
|
@@ -1311,6 +2248,52 @@ export function buildTmBrowserTool(deps) {
|
|
|
1311
2248
|
sess.currentUrl = String(state.page.url?.() ?? "");
|
|
1312
2249
|
return `已切换到标签页 ${idx}:${sess.currentUrl}`;
|
|
1313
2250
|
}
|
|
2251
|
+
if (action === "new_page") {
|
|
2252
|
+
// Multi-tab is an operator need, not a nicety: comparing two pages,
|
|
2253
|
+
// or reading a doc while keeping the first open, was impossible
|
|
2254
|
+
// because `open` on a live session only navigates the CURRENT tab.
|
|
2255
|
+
const p = await context.newPage();
|
|
2256
|
+
state.page = p;
|
|
2257
|
+
attach(p);
|
|
2258
|
+
const url = String(args.url ?? "").trim();
|
|
2259
|
+
const index = context.pages().indexOf(p);
|
|
2260
|
+
if (!url) {
|
|
2261
|
+
sess.currentUrl = String(p.url?.() ?? "about:blank");
|
|
2262
|
+
traj({ step_id: stepId, event: "call", kind: "new_page", url: "" });
|
|
2263
|
+
return `已新建空白标签页 ${index}(共 ${context.pages().length} 个,当前在它上面):${sess.currentUrl || "about:blank"}\n后续动作按 [uid] 寻址前请先 take_snapshot;切回去用 select_page { index }。`;
|
|
2264
|
+
}
|
|
2265
|
+
// The URL cleared the allowlist at the execute layer (that is where
|
|
2266
|
+
// the official dialog lives), exactly like navigate_page does.
|
|
2267
|
+
rememberSite(state.allowedSites, url);
|
|
2268
|
+
traj({ step_id: stepId, event: "call", kind: "new_page", url: url.slice(0, 200) });
|
|
2269
|
+
await p.goto(url, { waitUntil: "domcontentloaded", timeout: 20_000 });
|
|
2270
|
+
sess.currentUrl = p.url?.() || url;
|
|
2271
|
+
rememberSite(state.allowedSites, sess.currentUrl);
|
|
2272
|
+
return `已新建标签页 ${index} 并导航(共 ${context.pages().length} 个,当前在它上面):${url}${blockedNote(sess)}`;
|
|
2273
|
+
}
|
|
2274
|
+
if (action === "close_page") {
|
|
2275
|
+
const pages = context.pages();
|
|
2276
|
+
const raw = args.index;
|
|
2277
|
+
const idx = raw === undefined || raw === "" ? pages.indexOf(state.page) : Number(raw);
|
|
2278
|
+
if (!Number.isInteger(idx) || idx < 0 || idx >= pages.length) {
|
|
2279
|
+
throw new Error(`index 越界(0..${pages.length - 1})——先 list_pages`);
|
|
2280
|
+
}
|
|
2281
|
+
const victim = pages[idx];
|
|
2282
|
+
await Promise.resolve(victim.close?.()).catch(() => { });
|
|
2283
|
+
const rest = context.pages();
|
|
2284
|
+
const closedIndex = idx;
|
|
2285
|
+
if (!rest.length) {
|
|
2286
|
+
state.page = victim;
|
|
2287
|
+
sess.currentUrl = "";
|
|
2288
|
+
return `标签页 ${closedIndex} 已关闭,这是最后一个——浏览器仍在运行。` +
|
|
2289
|
+
`要结束整个会话用 action:"close"(它会验进程真的退出);要继续就用 new_page 再开一个。`;
|
|
2290
|
+
}
|
|
2291
|
+
state.page = rest.includes(state.page) ? state.page : rest[rest.length - 1];
|
|
2292
|
+
attach(state.page);
|
|
2293
|
+
const nextIdx = rest.indexOf(state.page);
|
|
2294
|
+
sess.currentUrl = String(state.page.url?.() ?? "");
|
|
2295
|
+
return `标签页 ${closedIndex} 已关闭,剩 ${rest.length} 个,当前在 ${nextIdx}:${sess.currentUrl}`;
|
|
2296
|
+
}
|
|
1314
2297
|
if (action === "list_console_messages") {
|
|
1315
2298
|
const lines = state.consoleBuf.map((m, i) => `${i + 1}. [${m.type}] ${m.text.slice(0, 300)}`);
|
|
1316
2299
|
if (args.clear)
|
|
@@ -1355,66 +2338,108 @@ export function buildTmBrowserTool(deps) {
|
|
|
1355
2338
|
alivePages = 0;
|
|
1356
2339
|
}
|
|
1357
2340
|
const connected = browser ? Boolean(browser.isConnected?.()) : false;
|
|
1358
|
-
|
|
2341
|
+
// The connection is NOT the process. Wait for the pid to actually
|
|
2342
|
+
// disappear; if it refuses, terminate it once and re-check. Only then
|
|
2343
|
+
// may this line say 已确认关闭 — an agent quotes it to the user as
|
|
2344
|
+
// proof the window is gone, and a lingering msedge tree is not gone.
|
|
2345
|
+
const pidWasThere = pidAlive(browserPid);
|
|
2346
|
+
const processGone = browserPid > 0 ? await waitForPidExit(browserPid) : true;
|
|
2347
|
+
const pidNote = browserPid > 0
|
|
2348
|
+
? processGone
|
|
2349
|
+
? pidWasThere
|
|
2350
|
+
? `进程 ${browserPid} 已退出`
|
|
2351
|
+
: `进程 ${browserPid} 早已不在`
|
|
2352
|
+
: `进程 ${browserPid} 仍在(已尝试终止但系统未放行)`
|
|
2353
|
+
: "未取得浏览器 pid,进程未核验";
|
|
2354
|
+
// The pid check is what makes the word 确认 mean anything. An
|
|
2355
|
+
// unverifiable close must not borrow the verified sentence, because an
|
|
2356
|
+
// agent quotes this line to the user as proof the window is gone.
|
|
2357
|
+
const verified = browserPid > 0 && processGone;
|
|
2358
|
+
const closed = errs.length === 0 && alivePages === 0 && !connected && processGone;
|
|
1359
2359
|
const profile = persistentDir
|
|
1360
2360
|
? `持久配置目录已保留(下次 open 复用登录态):${persistentDir}`
|
|
1361
2361
|
: "临时配置目录由 playwright 自行回收";
|
|
1362
|
-
if (closed)
|
|
1363
|
-
return { closed: true, text: `浏览器会话已确认关闭(${sess.label});${profile}。` };
|
|
2362
|
+
if (closed && verified)
|
|
2363
|
+
return { closed: true, text: `浏览器会话已确认关闭(${sess.label} · ${pidNote});${profile}。` };
|
|
2364
|
+
if (closed) {
|
|
2365
|
+
return {
|
|
2366
|
+
closed: false,
|
|
2367
|
+
text: `连接与窗口已释放,但进程未核验(${sess.label} · ${pidNote})——这次没拿到浏览器 pid,` +
|
|
2368
|
+
`所以这句不是"已确认关闭"。若桌面上窗口仍在请手动关闭;反复出现可换 TM_BROWSER_ENGINE=cdp-legacy 试一次。${profile}。`,
|
|
2369
|
+
};
|
|
2370
|
+
}
|
|
1364
2371
|
return {
|
|
1365
2372
|
closed: false,
|
|
1366
2373
|
text: `警告:关闭未完全成功(${sess.label}${errs.length ? `:${errs.join("; ")}` : ""})——` +
|
|
1367
|
-
`残留标签页 ${alivePages} 个${connected ? ",浏览器进程仍处于连接状态" : ""}。` +
|
|
1368
|
-
|
|
2374
|
+
`残留标签页 ${alivePages} 个${connected ? ",浏览器进程仍处于连接状态" : ""} · ${pidNote}。` +
|
|
2375
|
+
// Three states, and the sentence must match the one we are in. A
|
|
2376
|
+
// dead pid cannot have a window on the desktop, so telling the user
|
|
2377
|
+
// to go hunt it sends them after something that is not there — and
|
|
2378
|
+
// the leftover count is OUR own stale reference (close_page leaves
|
|
2379
|
+
// state.page on the tab it closed). No pid at all is a DIFFERENT
|
|
2380
|
+
// fact: nothing was verified, so the window may genuinely remain.
|
|
2381
|
+
(browserPid > 0 && processGone
|
|
2382
|
+
? `进程已确认不在(pid ${browserPid} 已退出),所以桌面上不该还有它的窗口——那个"残留标签页"是我自己留着的路由引用,不是窗口,你不用去找。${profile}。`
|
|
2383
|
+
: browserPid > 0
|
|
2384
|
+
? `窗口很可能仍在前台(pid ${browserPid} 还在),请用户手动关闭该浏览器窗口。${profile}。`
|
|
2385
|
+
: `这次没拿到浏览器 pid,进程无法核验——若桌面上窗口仍在请手动关闭。${profile}。`),
|
|
1369
2386
|
};
|
|
1370
2387
|
},
|
|
1371
2388
|
};
|
|
1372
2389
|
return sess;
|
|
1373
2390
|
}
|
|
1374
2391
|
// ---------- engine-neutral session lifecycle ----------
|
|
1375
|
-
/** Idle reaper. The pre-v1.5.13 layer had NO cleanup path other
|
|
1376
|
-
* agent remembering to call `close` — and an agent that believes it
|
|
1377
|
-
* closed the window (issue #3) leaves the user staring at a Chromium
|
|
1378
|
-
*
|
|
1379
|
-
*
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
idleTimer = null;
|
|
1385
|
-
if (!idleCloseMs || !session)
|
|
2392
|
+
/** Idle reaper, PER LEASE. The pre-v1.5.13 layer had NO cleanup path other
|
|
2393
|
+
* than the agent remembering to call `close` — and an agent that believes it
|
|
2394
|
+
* closed the window (issue #3) leaves the user staring at a Chromium window
|
|
2395
|
+
* nobody owns. With one browser per caller the timer belongs to the lease:
|
|
2396
|
+
* one agent going quiet must not keep another's window alive, and a reap must
|
|
2397
|
+
* never touch a browser somebody is still driving. */
|
|
2398
|
+
function armIdle(lease) {
|
|
2399
|
+
clearIdle(lease.id);
|
|
2400
|
+
if (!idleCloseMs)
|
|
1386
2401
|
return;
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
if (!
|
|
2402
|
+
const t = setTimeout(() => {
|
|
2403
|
+
idleTimers.delete(lease.id);
|
|
2404
|
+
if (!leases.has(lease.id))
|
|
1390
2405
|
return;
|
|
1391
|
-
traj({ step_id: "browser", event: "idle_close", idle_ms: idleCloseMs });
|
|
1392
|
-
void
|
|
1393
|
-
.then((r) => notify?.(`tm_browser
|
|
2406
|
+
traj({ step_id: "browser", event: "idle_close", idle_ms: idleCloseMs, id: lease.id, agent: lease.agent });
|
|
2407
|
+
void closeLease(lease)
|
|
2408
|
+
.then((r) => notify?.(`tm_browser ${lease.id}(${lease.agent || "agent"} 的浏览器)空闲 ${Math.round(idleCloseMs / 1000)}s,已自动关闭${r.closed ? "(窗口应已消失)" : "——但窗口可能仍需手动关闭"}`))
|
|
1394
2409
|
.catch(() => { });
|
|
1395
2410
|
}, idleCloseMs);
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
2411
|
+
t.unref?.();
|
|
2412
|
+
idleTimers.set(lease.id, t);
|
|
2413
|
+
}
|
|
2414
|
+
/** The caller's browser, launched on first use. `open` on an existing lease
|
|
2415
|
+
* navigates that window instead of spawning a second one. */
|
|
2416
|
+
async function ensureLease(headless, caller) {
|
|
2417
|
+
const mine = ownedBy(caller.owner);
|
|
2418
|
+
if (mine.length)
|
|
2419
|
+
return mine[0];
|
|
1401
2420
|
const sel = await selection();
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
2421
|
+
const approved = approvedFor(caller.owner);
|
|
2422
|
+
const session = sel.kind === "playwright" && sel.pw
|
|
2423
|
+
? await openPlaywrightSession(sel.pw, headless, approved)
|
|
2424
|
+
: await openLegacySession(headless, approved);
|
|
2425
|
+
const lease = {
|
|
2426
|
+
id: `b${++leaseSeq}`,
|
|
2427
|
+
owner: caller.owner,
|
|
2428
|
+
agent: caller.agent,
|
|
2429
|
+
session,
|
|
2430
|
+
at: Date.now(),
|
|
2431
|
+
lastActivity: Date.now(),
|
|
2432
|
+
};
|
|
2433
|
+
leases.set(lease.id, lease);
|
|
2434
|
+
traj({ step_id: "browser", event: "lease", id: lease.id, agent: lease.agent, live: leases.size });
|
|
2435
|
+
return lease;
|
|
2436
|
+
}
|
|
2437
|
+
async function closeLease(lease) {
|
|
2438
|
+
if (!lease)
|
|
1411
2439
|
return { text: "没有打开的浏览器会话。", closed: true };
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
idleTimer = null;
|
|
1416
|
-
}
|
|
1417
|
-
return s.close();
|
|
2440
|
+
leases.delete(lease.id);
|
|
2441
|
+
clearIdle(lease.id);
|
|
2442
|
+
return lease.session.close();
|
|
1418
2443
|
}
|
|
1419
2444
|
/** Move any screenshot pixels act() produced out of the session and onto
|
|
1420
2445
|
* the ToolResult. Drained per call so a stale attachment can never ride
|
|
@@ -1428,85 +2453,221 @@ export function buildTmBrowserTool(deps) {
|
|
|
1428
2453
|
const base = typeof res === "string" ? { output: res } : res;
|
|
1429
2454
|
return { ...base, attachments: atts };
|
|
1430
2455
|
}
|
|
1431
|
-
const ALL_ACTIONS = new Set(
|
|
2456
|
+
const ALL_ACTIONS = new Set(ALL_BROWSER_ACTIONS);
|
|
2457
|
+
// The menu IS the table the gate below reads. A hand-written copy drifted the
|
|
2458
|
+
// day `new_page` landed: an agent that mistyped a verb was shown a list that
|
|
2459
|
+
// did not contain the capability it wanted.
|
|
2460
|
+
const ACTION_MENU = BROWSER_ACTION_MENU;
|
|
1432
2461
|
const execute = async (rawArgs, ctx) => {
|
|
2462
|
+
// Set once a lease is resolved, so the catch below knows WHICH browser died
|
|
2463
|
+
// — with one browser per caller, dropping the wrong lease would kill an
|
|
2464
|
+
// agent that was working fine.
|
|
2465
|
+
let activeLease = null;
|
|
1433
2466
|
try {
|
|
1434
2467
|
const args = rawArgs ?? {};
|
|
1435
2468
|
const action = String(args.action ?? "").trim();
|
|
1436
2469
|
const url = String(args.url ?? "").trim();
|
|
2470
|
+
// Whose browser this is. The host hands every tool ctx a sessionID, and
|
|
2471
|
+
// a lease is keyed by it; without one (an older host, a test harness) all
|
|
2472
|
+
// callers share the single anonymous bucket, which is the old behaviour.
|
|
2473
|
+
const caller = callerOf(ctx);
|
|
2474
|
+
// The profile knob is checked BEFORE any engine runs: a refused value must
|
|
2475
|
+
// not spawn a browser that then discovers the problem.
|
|
2476
|
+
{
|
|
2477
|
+
const probe = classifyProfileDir(env);
|
|
2478
|
+
if (probe.refusal)
|
|
2479
|
+
return toToolResult(tmError(tool, "args", probe.refusal));
|
|
2480
|
+
}
|
|
2481
|
+
// Set when this call proceeded on a DIALOG approval rather than the
|
|
2482
|
+
// static allowlist; appended to whichever reply the action returns.
|
|
2483
|
+
let grantNote = "";
|
|
1437
2484
|
// allowlist FIRST — an out-of-allowlist URL only proceeds after the
|
|
1438
2485
|
// OFFICIAL dialog approves it (then the host passes the network layer
|
|
1439
2486
|
// too); hard red lines (scheme / env-file) reject with no dialog.
|
|
1440
2487
|
// NOTE: this gate runs BEFORE engine selection — a blocked target
|
|
1441
2488
|
// never touches playwright nor spawns anything (§6o invariant).
|
|
1442
|
-
|
|
2489
|
+
// new_page carries a URL too — it must clear the SAME allowlist gate, or
|
|
2490
|
+
// "open a second tab" becomes a way around the official dialog.
|
|
2491
|
+
if ((action === "open" || action === "navigate" || action === "navigate_page" || action === "new_page") && url) {
|
|
2492
|
+
// Search-results pages are tm_search's job: one call returns a deduped,
|
|
2493
|
+
// RRF-fused hit list, while a browser SERP grab costs a round trip and a
|
|
2494
|
+
// snapshot budget each time. A live session burned ~30 of them. The
|
|
2495
|
+
// first few still pass (bing's HTML is sometimes an anti-bot shell and a
|
|
2496
|
+
// real browser is the only way through), then the navigation is refused.
|
|
2497
|
+
const serp = serpLoop.observe(url);
|
|
2498
|
+
if (serp) {
|
|
2499
|
+
pipelines.store.appendTrajectory({
|
|
2500
|
+
tool,
|
|
2501
|
+
step_id: pipelines.nextStepId(),
|
|
2502
|
+
event: serp.blocked ? "serp_refused" : "serp_nav",
|
|
2503
|
+
engine: serp.target.engine,
|
|
2504
|
+
repeats: serp.count,
|
|
2505
|
+
});
|
|
2506
|
+
if (serp.blocked) {
|
|
2507
|
+
return toToolResult(tmError(tool, "permission", serpRefusal(serp)));
|
|
2508
|
+
}
|
|
2509
|
+
}
|
|
1443
2510
|
const verdict = checkWebUrl(url, allowlist);
|
|
1444
2511
|
if (!verdict.ok) {
|
|
1445
2512
|
if (!verdict.askable || !verdict.url) {
|
|
1446
2513
|
return toToolResult(tmError(tool, "permission", verdict.message));
|
|
1447
2514
|
}
|
|
1448
|
-
const
|
|
2515
|
+
const res = await askUserForTargetDetailed(ctx, {
|
|
1449
2516
|
permission: tool,
|
|
1450
2517
|
patterns: [verdict.url.toString()],
|
|
1451
2518
|
metadata: { tool, url: url.slice(0, 200) },
|
|
1452
2519
|
});
|
|
1453
|
-
if (outcome !== "approved") {
|
|
1454
|
-
return toToolResult(tmError(tool, "permission", verdict.message +
|
|
1455
|
-
(outcome === "rejected" ? "。用户未批准。" : "。宿主无法弹出确认窗口(旧版协议)。")));
|
|
2520
|
+
if (res.outcome !== "approved") {
|
|
2521
|
+
return toToolResult(tmError(tool, "permission", verdict.message + " " + askRefusalNote(res.outcome)));
|
|
1456
2522
|
}
|
|
1457
|
-
|
|
2523
|
+
approvedFor(caller.owner).add(verdict.url.hostname);
|
|
2524
|
+
// The approval is recorded in the REPLY, not only the trajectory: an
|
|
2525
|
+
// agent that cannot tell a saved "always" from a fresh click reports
|
|
2526
|
+
// "no dialog appeared" as evidence about the allowlist — measured live,
|
|
2527
|
+
// that is exactly what a researcher wrote into its deliverable.
|
|
2528
|
+
grantNote = askGrantNote(res);
|
|
2529
|
+
traj({
|
|
2530
|
+
step_id: "browser",
|
|
2531
|
+
event: res.autoGranted ? "silent_grant" : "dialog_approved",
|
|
2532
|
+
host: verdict.url.hostname,
|
|
2533
|
+
answered_ms: res.answeredInMs,
|
|
2534
|
+
});
|
|
1458
2535
|
}
|
|
1459
2536
|
}
|
|
2537
|
+
// The remedy for "our gate blanked this page". A blocked asset host used
|
|
2538
|
+
// to be a dead end: the only move was an env edit plus a restart, which no
|
|
2539
|
+
// agent can do mid-task, so it reported the site as empty instead. This
|
|
2540
|
+
// routes the SAME official dialog at ONE host, for ONE owner, for the
|
|
2541
|
+
// length of the session — and it cannot widen anything: a host already on
|
|
2542
|
+
// the static allowlist is refused (asking would train the user to approve
|
|
2543
|
+
// what is already allowed), a red-line host is refused with no dialog, and
|
|
2544
|
+
// the pattern handed to the dialog is the exact hostname, never a glob.
|
|
2545
|
+
if (action === "allow_host") {
|
|
2546
|
+
const host = normalizeApprovalHost(args.host);
|
|
2547
|
+
if (!host) {
|
|
2548
|
+
return toToolResult(tmError(tool, "args", '缺少或非法的 host 参数——要形如 "bkssl.bdimg.com" 的单个域名(不接受通配、URL、路径或端口)'));
|
|
2549
|
+
}
|
|
2550
|
+
const verdict = checkWebUrl(`https://${host}/`, allowlist);
|
|
2551
|
+
if (verdict.ok) {
|
|
2552
|
+
return toToolResult(`不用批准:${host} 本来就在静态白名单里,它的子资源不会被拦。这一页的空白另有原因——看 take_snapshot 的拦截说明(被拦的是别的域名)或用 read 拿原文。`);
|
|
2553
|
+
}
|
|
2554
|
+
if (!verdict.askable || !verdict.url) {
|
|
2555
|
+
return toToolResult(tmError(tool, "permission", `拒绝放行 ${host}:${verdict.message}`));
|
|
2556
|
+
}
|
|
2557
|
+
const outcome = await askUserForTarget(ctx, {
|
|
2558
|
+
permission: tool,
|
|
2559
|
+
patterns: [host],
|
|
2560
|
+
metadata: { tool, host, reason: "被拦的跨域脚本域名,放行后需重新导航" },
|
|
2561
|
+
});
|
|
2562
|
+
if (outcome !== "approved") {
|
|
2563
|
+
return toToolResult(tmError(tool, "permission", `未放行 ${host}。` + askRefusalNote(outcome)));
|
|
2564
|
+
}
|
|
2565
|
+
approvedFor(caller.owner).add(host);
|
|
2566
|
+
traj({ step_id: "browser", event: "host_approved", host, agent: caller.agent });
|
|
2567
|
+
const mine = ownedBy(caller.owner)[0];
|
|
2568
|
+
return toToolResult(`${mine ? `[${mine.id}] ` : ""}已按用户批准临时放行 ${host}(只对这个浏览器、本次会话,不改任何配置文件)。` +
|
|
2569
|
+
`现在重新 navigate 一次同一页——门禁在请求时判定,已经加载完的页面不会自己把脚本补回来。`);
|
|
2570
|
+
}
|
|
1460
2571
|
if (action === "open") {
|
|
1461
2572
|
if (!url)
|
|
1462
2573
|
return toToolResult(tmError(tool, "args", "缺少 url 参数"));
|
|
1463
|
-
|
|
2574
|
+
const existing = ownedBy(caller.owner)[0];
|
|
2575
|
+
if (existing) {
|
|
1464
2576
|
// already open: navigate instead of spawning a second browser.
|
|
1465
2577
|
// Report the mode the RUNNING instance is actually in — the old
|
|
1466
2578
|
// code echoed the REQUESTED mode, so a sticky session could be
|
|
1467
2579
|
// described as headful while it was a headless leftover.
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
2580
|
+
// Tracked as the active lease: a browser that dies HERE must be
|
|
2581
|
+
// dropped too, or the next open hands back the same corpse.
|
|
2582
|
+
activeLease = existing;
|
|
2583
|
+
const out = await existing.session.act("navigate", { url }, "browser");
|
|
2584
|
+
armIdle(existing);
|
|
2585
|
+
return withAttachments(toToolResult(`[${existing.id}] 复用你自己的浏览器(${existing.session.headless ? "无头" : "有头窗口"} · ${existing.session.label})。${out}${grantNote}`), existing.session);
|
|
1471
2586
|
}
|
|
1472
2587
|
// Mode is an OPERATOR decision (TM_BROWSER_HEADLESS), never a model
|
|
1473
2588
|
// arg: a model that once passed headless:"false" (string → truthy
|
|
1474
2589
|
// under Boolean()) pinned the whole process to headless and every
|
|
1475
2590
|
// anti-bot gate in the run failed.
|
|
1476
2591
|
const headless = resolveHeadless(env);
|
|
1477
|
-
const
|
|
2592
|
+
const lease = await ensureLease(headless, caller);
|
|
2593
|
+
activeLease = lease;
|
|
2594
|
+
const s = lease.session;
|
|
1478
2595
|
const out = await s.act("navigate", { url }, "browser");
|
|
1479
|
-
traj({ step_id: "browser", event: "open", headless, label: s.label });
|
|
2596
|
+
traj({ step_id: "browser", event: "open", headless, label: s.label, id: lease.id, agent: lease.agent });
|
|
1480
2597
|
const mode = s.headless ? "无头" : "有头窗口";
|
|
1481
|
-
|
|
2598
|
+
// From the SESSION, never from the environment: this line used to re-read
|
|
2599
|
+
// TM_BROWSER_USER_DATA_DIR at reply time, so a session that had degraded
|
|
2600
|
+
// to cdp-legacy (temp profile) announced 持久登录配置.
|
|
2601
|
+
const profile = s.persistentProfile ? "持久登录配置" : "隔离临时配置";
|
|
1482
2602
|
const note = lastSel && preference !== "cdp-legacy" && lastSel.kind === "cdp-legacy"
|
|
1483
2603
|
? `\n(引擎:cdp-legacy 降级——${lastSel.reason ?? ""};完整 16 动作需 npm install playwright-core + node≥20)`
|
|
1484
2604
|
: "";
|
|
1485
2605
|
const guard = subPolicy === "off"
|
|
1486
2606
|
? `\n(子资源策略=off:跨域图片/脚本一律拦截,页面可能显示不全——TM_BROWSER_SUBRESOURCE=same-site 可恢复)`
|
|
1487
2607
|
: "";
|
|
1488
|
-
|
|
1489
|
-
|
|
2608
|
+
const others = liveLeases().length - 1;
|
|
2609
|
+
// The close duty belongs in the reply that OPENS the window, not only in
|
|
2610
|
+
// the tool description: a long-lived host never re-reads a description
|
|
2611
|
+
// (the same finding that moved the date onto the search header), and the
|
|
2612
|
+
// agent's attention is here — the moment it stops needing the page is the
|
|
2613
|
+
// moment it forgets the window exists. When the reaper is off, say so
|
|
2614
|
+
// rather than promising a cleanup that will not come.
|
|
2615
|
+
const duty = idleCloseMs
|
|
2616
|
+
? `\n用完必须 action:"close"(窗口用户看得见;空闲 ${Math.round(idleCloseMs / 1000)}s 我会替你关掉并提示他)。`
|
|
2617
|
+
: `\n用完必须 action:"close"——TM_BROWSER_IDLE_MS=0,没有空闲回收,你不关它就一直开着。`;
|
|
2618
|
+
armIdle(lease);
|
|
2619
|
+
return withAttachments(toToolResult(`[${lease.id}] 浏览器已启动(${mode},${profile} · ${s.label})。${out}${note}${guard}` +
|
|
2620
|
+
`\n这个窗口的 id 是 ${lease.id},归你(${lease.agent || "本会话"}):后续动作带 id:"${lease.id}"。` +
|
|
2621
|
+
duty +
|
|
2622
|
+
(others > 0
|
|
2623
|
+
? `现在共有 ${others + 1} 个浏览器在跑(别的 agent 各有各的窗口),省略 id 会被拒绝。`
|
|
2624
|
+
: "") +
|
|
2625
|
+
grantNote), s);
|
|
1490
2626
|
}
|
|
1491
2627
|
if (action === "navigate" || action === "navigate_page") {
|
|
1492
2628
|
if (!url)
|
|
1493
2629
|
return toToolResult(tmError(tool, "args", "缺少 url 参数"));
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
2630
|
+
const got = resolveLease(args, caller);
|
|
2631
|
+
if ("error" in got)
|
|
2632
|
+
return toToolResult(tmError(tool, "args", got.error));
|
|
2633
|
+
activeLease = got.lease;
|
|
2634
|
+
const out = await got.lease.session.act(action, { url }, "browser");
|
|
2635
|
+
armIdle(got.lease);
|
|
2636
|
+
return withAttachments(toToolResult(`[${got.lease.id}] ${out}${grantNote}`), got.lease.session);
|
|
1499
2637
|
}
|
|
1500
2638
|
if (action === "close") {
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
2639
|
+
// `id:"all"` closes every browser the CALLER owns — never anybody
|
|
2640
|
+
// else's, which is the one thing a shared kill switch would get wrong.
|
|
2641
|
+
const mine = ownedBy(caller.owner);
|
|
2642
|
+
if (String(args.id ?? "").trim().toLowerCase() === "all") {
|
|
2643
|
+
if (!mine.length)
|
|
2644
|
+
return toToolResult(tmError(tool, "args", "你没有打开的浏览器。"));
|
|
2645
|
+
traj({ step_id: "browser", event: "close", id: "all", count: mine.length, agent: caller.agent });
|
|
2646
|
+
const parts = [];
|
|
2647
|
+
for (const l of mine)
|
|
2648
|
+
parts.push(`[${l.id}] ${(await closeLease(l)).text}`);
|
|
2649
|
+
return toToolResult(parts.join("\n"));
|
|
2650
|
+
}
|
|
2651
|
+
const got = resolveLease(args, caller);
|
|
2652
|
+
if ("error" in got)
|
|
2653
|
+
return toToolResult(tmError(tool, "args", got.error));
|
|
2654
|
+
traj({ step_id: "browser", event: "close", id: got.lease.id, agent: caller.agent });
|
|
2655
|
+
const r = await closeLease(got.lease);
|
|
2656
|
+
return toToolResult(`[${got.lease.id}] ${r.text}`);
|
|
1504
2657
|
}
|
|
1505
2658
|
if (!ALL_ACTIONS.has(action)) {
|
|
1506
|
-
return toToolResult(tmError(tool, "args", `未知 action "${String(action).slice(0, 30)}"——可用:
|
|
2659
|
+
return toToolResult(tmError(tool, "args", `未知 action "${String(action).slice(0, 30)}"——可用: ${ACTION_MENU}(快照优先:先 take_snapshot,按 [uid=…] 寻址)`));
|
|
1507
2660
|
}
|
|
1508
|
-
|
|
1509
|
-
|
|
2661
|
+
const got = resolveLease(args, caller);
|
|
2662
|
+
if ("error" in got)
|
|
2663
|
+
return toToolResult(tmError(tool, "args", got.error));
|
|
2664
|
+
const lease = got.lease;
|
|
2665
|
+
// Bound locally so the dispatch below reads the way it did when the whole
|
|
2666
|
+
// process had one browser — except `session` now means "the CALLER's
|
|
2667
|
+
// browser", never "the browser".
|
|
2668
|
+
const session = lease.session;
|
|
2669
|
+
activeLease = lease;
|
|
2670
|
+
armIdle(lease);
|
|
1510
2671
|
// M1: upload_file SOURCE containment runs at the execute layer — the
|
|
1511
2672
|
// engine act() has no host ctx. Out-of-P2 paths and R6 env files are
|
|
1512
2673
|
// refused before setInputFiles can move any bytes.
|
|
@@ -1523,6 +2684,16 @@ export function buildTmBrowserTool(deps) {
|
|
|
1523
2684
|
// cannot drift from the other.
|
|
1524
2685
|
if (action === "evaluate_script" && needsEvalConsent(evalAskPolicy, session.evalApproved)) {
|
|
1525
2686
|
const target = hostOnly(session.currentUrl);
|
|
2687
|
+
// An error page or a blank tab has no host to consent to. Asking the
|
|
2688
|
+
// official dialog about `evaluate_script:` with an EMPTY pattern gives
|
|
2689
|
+
// the user a prompt they cannot judge, and the refusal then reads
|
|
2690
|
+
// "未获批准(目标站点 )" — seen live on a chrome-error:// tab.
|
|
2691
|
+
if (!/^[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)+$/.test(target)) {
|
|
2692
|
+
const where = String(session.currentUrl ?? "").slice(0, 80) || "空白页";
|
|
2693
|
+
traj({ step_id: "browser", event: "eval_refused_no_host", where });
|
|
2694
|
+
return toToolResult(tmError(tool, "permission", `evaluate_script 被拒绝:当前页面不是可识别的 http(s) 站点(${where}),没有可批准的主机。` +
|
|
2695
|
+
`先 open/navigate 到一个真实页面再执行 JS——对着错误页要权限,用户无法判断该不该批。`));
|
|
2696
|
+
}
|
|
1526
2697
|
const outcome = await askUserForTarget(ctx, {
|
|
1527
2698
|
permission: tool,
|
|
1528
2699
|
patterns: [`evaluate_script:${target}`],
|
|
@@ -1535,17 +2706,18 @@ export function buildTmBrowserTool(deps) {
|
|
|
1535
2706
|
});
|
|
1536
2707
|
traj({ step_id: "browser", event: "eval_consent", host: target, verdict: outcome });
|
|
1537
2708
|
if (outcome !== "approved") {
|
|
1538
|
-
return toToolResult(tmError(tool, "permission", outcome === "unavailable"
|
|
2709
|
+
return toToolResult(tmError(tool, "permission", (outcome === "unavailable"
|
|
1539
2710
|
? `evaluate_script 需要官方确认窗授权,本宿主没有 ask 桥——已拒绝执行。TM_BROWSER_ASK_EVAL=off 可关掉这道确认(不建议:这个动词能在你已登录的浏览器里读任意页面数据)。`
|
|
1540
|
-
: `evaluate_script 未获批准(目标站点 ${target}),已拒绝执行。改用 take_snapshot/read 的受治理读取,或让用户单独批准。`)
|
|
2711
|
+
: `evaluate_script 未获批准(目标站点 ${target}),已拒绝执行。改用 take_snapshot/read 的受治理读取,或让用户单独批准。`) +
|
|
2712
|
+
" " +
|
|
2713
|
+
askRefusalNote(outcome)));
|
|
1541
2714
|
}
|
|
1542
2715
|
session.evalApproved = true;
|
|
1543
2716
|
}
|
|
1544
2717
|
const stepId = pipelines.nextStepId();
|
|
1545
2718
|
const out = await session.act(action, args, stepId);
|
|
1546
2719
|
if (action !== "evaluate_script") {
|
|
1547
|
-
|
|
1548
|
-
return withAttachments(toToolResult(out), session);
|
|
2720
|
+
return withAttachments(toToolResult(`[${lease.id}] ${out}${grantNote}`), session);
|
|
1549
2721
|
}
|
|
1550
2722
|
// The value is masked BEFORE it can reach the context window, the run
|
|
1551
2723
|
// store or the trajectory; the agent is told a mask happened (a silent
|
|
@@ -1553,28 +2725,70 @@ export function buildTmBrowserTool(deps) {
|
|
|
1553
2725
|
const red = redactEvalResult(out);
|
|
1554
2726
|
if (red.masked.length) {
|
|
1555
2727
|
traj({ step_id: "browser", event: "eval_redacted", kinds: red.masked.join(",") });
|
|
1556
|
-
|
|
1557
|
-
return withAttachments(toToolResult(`${red.text}\n注意:结果里有 ${red.masked.join("、")} 被识别为密钥形状并已脱敏——不是页面没有这些值,是插件不把它们送进上下文。需要它们请让用户自己在浏览器里看。`), session);
|
|
2728
|
+
return withAttachments(toToolResult(`[${lease.id}] ${red.text}\n注意:结果里有 ${red.masked.join("、")} 被识别为密钥形状并已脱敏——不是页面没有这些值,是插件不把它们送进上下文。需要它们请让用户自己在浏览器里看。`), session);
|
|
1558
2729
|
}
|
|
1559
|
-
|
|
1560
|
-
return withAttachments(toToolResult(red.text), session);
|
|
2730
|
+
return withAttachments(toToolResult(`[${lease.id}] ${red.text}`), session);
|
|
1561
2731
|
}
|
|
1562
2732
|
catch (err) {
|
|
1563
2733
|
const e = err;
|
|
1564
|
-
|
|
2734
|
+
const msg = String(e?.message ?? err ?? "browser 操作失败");
|
|
2735
|
+
// A browser the user closed — or a launch that handed its request to an
|
|
2736
|
+
// already-running Edge and exited — leaves a cached session whose every
|
|
2737
|
+
// action throws the SAME line. Live session: three identical failures,
|
|
2738
|
+
// then a fourth, with no way out except our own bookkeeping. Drop the
|
|
2739
|
+
// corpse, say that we did, and name the one move that works. With one
|
|
2740
|
+
// browser per caller, only THAT lease is dropped: another agent's window
|
|
2741
|
+
// is not ours to declare dead.
|
|
2742
|
+
if (/has been closed|Target closed|Browser closed|browser has been closed/i.test(msg)) {
|
|
2743
|
+
const dead = activeLease;
|
|
2744
|
+
const leaseId = dead?.id ?? "";
|
|
2745
|
+
// That one string covers two very different facts — a TAB the user closed
|
|
2746
|
+
// and a BROWSER that died. Only the second may cost the lease: measured
|
|
2747
|
+
// live, closing the last tab threw this line while the window was still
|
|
2748
|
+
// up, the lease got dropped, `close` then reported "no browser open", and
|
|
2749
|
+
// the user was left holding a browser no agent could address.
|
|
2750
|
+
const alive = dead ? dead.session.alive?.() !== false : false;
|
|
2751
|
+
if (dead && !alive) {
|
|
2752
|
+
leases.delete(dead.id);
|
|
2753
|
+
clearIdle(dead.id);
|
|
2754
|
+
}
|
|
2755
|
+
traj({
|
|
2756
|
+
step_id: "browser",
|
|
2757
|
+
event: alive ? "page_dead_browser_alive" : "session_dead",
|
|
2758
|
+
cleared: Boolean(dead && !alive),
|
|
2759
|
+
id: leaseId,
|
|
2760
|
+
reason: msg.slice(0, 160),
|
|
2761
|
+
});
|
|
2762
|
+
return toToolResult(tmError(tool, "execute", alive
|
|
2763
|
+
? `${msg.slice(0, 200)}\n但浏览器本身还连着(${leaseId} 我保留了,你的 id 不变)——死的是**这一个页面/标签**,不是实例。` +
|
|
2764
|
+
`下一步:list_pages 看还剩几个标签,或 new_page 在同一个窗口开一个;要结束整个会话用 close。`
|
|
2765
|
+
: `${msg.slice(0, 200)}\n浏览器实例已失效${dead ? `(${leaseId},${dead.agent || "本会话"} 的窗口):我已丢弃它,下一次 action:"open" 会真的重启一个新实例` : ",当前没有可复用的会话"}。` +
|
|
2766
|
+
`连着两次都这样,通常是本机已有同品牌浏览器在跑、新进程把请求移交给旧实例后退掉了——请用户关掉那个窗口再 open,或设 TM_BROWSER_ENGINE=cdp-legacy 换一条传输。` +
|
|
2767
|
+
`不要第三次重复同一个动作。`));
|
|
2768
|
+
}
|
|
2769
|
+
return toToolResult(tmError(tool, "execute", msg));
|
|
1565
2770
|
}
|
|
1566
2771
|
};
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
2772
|
+
// The description is MEASURED cost: on v2 with `codemode: false` every token here
|
|
2773
|
+
// rides every request the role makes, so this text is a budget line, not a
|
|
2774
|
+
// changelog. What stays is what the model cannot recover from a tool result — the
|
|
2775
|
+
// flow, the ownership rule, and the three close verdicts. What left is the history
|
|
2776
|
+
// and the reasoning behind each rule (both live in AGENTS.md and CHANGELOG.md,
|
|
2777
|
+
// where a future reader looks and where it costs nothing per request).
|
|
2778
|
+
const DESCRIPTION = `Interactive governed browser: drives the user's own Chromium-family browser, headful. playwright-core primary (Node>=20), auto-degrades to the zero-dep CDP pipe (snapshot verbs unavailable). Flow: open → take_snapshot → act by [uid] → observe again.
|
|
2779
|
+
- Verbs: navigate_page · take_snapshot · click · fill · hover · drag · press_key · select_page · new_page · close_page · list_pages · upload_file · wait_for · evaluate_script · list_console_messages · list_network_requests · take_screenshot · handle_dialog · open · navigate · read · screenshot · close · allow_host.
|
|
2780
|
+
- ONE BROWSER PER AGENT: open returns an id ("b1"); that window, its uid numbering and its approvals are yours. Pass id once more than one browser is live; another agent's id is refused with its owner named (a shared window means your take_snapshot renumbers the uids they hold). close { id:"all" } closes only yours.
|
|
2781
|
+
- Multi-tab: new_page {url?} opens and makes current (same gate as navigate), select_page/close_page by index, closing the LAST tab does NOT close the browser and says so.
|
|
2782
|
+
- Act only on uids from the LATEST snapshot; one action then one observation; dialogs fold into the same round; 3000 ms action budget; never wait for networkidle. take_screenshot writes the PNG to the run store and attaches a JPEG only with image:true (pixels cost context). evaluate_script needs one official dialog per session and its result is scanned so token/cookie shapes never enter the context.
|
|
2783
|
+
- Lifecycle: headful unless the operator sets TM_BROWSER_HEADLESS; idle sessions self-close (TM_BROWSER_IDLE_MS) and the user is told. ALWAYS action:"close" when done and quote the tool's own verdict — 已确认关闭 / 进程未核验 / 警告:关闭未完全成功 — instead of asserting the window is gone.
|
|
2784
|
+
- Network: navigation must clear the domain allowlist (out-of-allowlist asks through the official dialog BEFORE any spawn); subresources follow TM_BROWSER_SUBRESOURCE (default same-site: images/fonts/css load, scripts only for a site this session opened). A "N 个子资源请求被拦截" note means OUR gate trimmed the page, not that the site is empty. env-file URLs and non-http(s) schemes always reject.
|
|
2785
|
+
- A blank page gets a reason: the reply names the blocked host and offers allow_host { host } (one bare domain, this browser, this session, nothing written to config — then re-navigate) or TM_WEBFETCH_ALLOWED_DOMAINS + restart. A human-verification wall (安全验证 / Cloudflare / access denied) is reported as a wall: the move is another source, not another retry.
|
|
2786
|
+
- A dead instance is never retried: on "has been closed" the lease is dropped, the reply says the next open rebuilds, and a third identical open means a competing window or TM_BROWSER_ENGINE=cdp-legacy. No browser installed → structured error; fall back to tm_webfetch / MCP. Granted to team + researcher, browser-only for the tester.`;
|
|
1574
2787
|
return {
|
|
1575
2788
|
description: DESCRIPTION,
|
|
1576
2789
|
args: deps.args ?? {
|
|
1577
|
-
action: { descriptor: "action: take_snapshot|click|fill|navigate_page|... (
|
|
2790
|
+
action: { descriptor: "action: take_snapshot|click|fill|navigate_page|... (18 playwright verbs + open/navigate/read/screenshot/close)" },
|
|
2791
|
+
id: { descriptor: 'id: which browser — the id open gave you ("b1"). One browser per agent: required once more than one is live, and it must be YOURS (another agent\'s id is refused). close {id:"all"} closes all of yours' },
|
|
1578
2792
|
url: { descriptor: "url: string (open/navigate/navigate_page, allowlisted https)" },
|
|
1579
2793
|
image: { descriptor: "image: true (take_screenshot only — attach the PNG pixels to this result; default is path-only)" },
|
|
1580
2794
|
uid: { descriptor: "uid: snapshot [uid=eN] token (click/fill/hover/drag/upload_file/wait_for)" },
|
|
@@ -1594,18 +2808,29 @@ export function buildTmBrowserTool(deps) {
|
|
|
1594
2808
|
},
|
|
1595
2809
|
execute,
|
|
1596
2810
|
dispose: async () => {
|
|
1597
|
-
|
|
1598
|
-
clearTimeout(
|
|
1599
|
-
|
|
1600
|
-
}
|
|
2811
|
+
for (const t of idleTimers.values())
|
|
2812
|
+
clearTimeout(t);
|
|
2813
|
+
idleTimers.clear();
|
|
1601
2814
|
// AWAITED: the host's dispose hook is Promise-returning, and tearing
|
|
1602
2815
|
// down while a close was still in flight left the window on screen.
|
|
1603
|
-
|
|
2816
|
+
// EVERY lease, not just one caller's: plugin unload owns all of them.
|
|
2817
|
+
for (const l of liveLeases()) {
|
|
2818
|
+
await closeLease(l).catch(() => { });
|
|
2819
|
+
}
|
|
1604
2820
|
},
|
|
1605
2821
|
engineInfo: () => ({
|
|
1606
2822
|
kind: lastSel?.kind ?? null,
|
|
1607
2823
|
reason: lastSel?.reason ?? null,
|
|
1608
2824
|
}),
|
|
2825
|
+
leases: () => {
|
|
2826
|
+
const nowMs = Date.now();
|
|
2827
|
+
return liveLeases().map((l) => ({
|
|
2828
|
+
id: l.id,
|
|
2829
|
+
owner: l.owner,
|
|
2830
|
+
agent: l.agent,
|
|
2831
|
+
idleMs: Math.max(0, nowMs - l.lastActivity),
|
|
2832
|
+
}));
|
|
2833
|
+
},
|
|
1609
2834
|
};
|
|
1610
2835
|
}
|
|
1611
2836
|
//# sourceMappingURL=browser.js.map
|