@liang-hz/agent-in-chrome 0.55.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.ext-id.txt +4 -0
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/PRIVACY.md +86 -0
- package/README.en.md +419 -0
- package/README.md +406 -0
- package/bin/agent-in-chrome.mjs +97 -0
- package/commands/agent-in-chrome.md +18 -0
- package/commands/install-agent-in-chrome.md +22 -0
- package/docs/AGENTS.md +94 -0
- package/docs/CLI.md +297 -0
- package/docs/client-compat.json +23 -0
- package/extension/about-toc.js +25 -0
- package/extension/about.html +240 -0
- package/extension/about.js +44 -0
- package/extension/agent-window.html +100 -0
- package/extension/icons/icon-128.png +0 -0
- package/extension/icons/icon-16.png +0 -0
- package/extension/icons/icon-24.png +0 -0
- package/extension/icons/icon-32.png +0 -0
- package/extension/icons/icon-48.png +0 -0
- package/extension/icons/icon-64.png +0 -0
- package/extension/icons/icon-attention-128.png +0 -0
- package/extension/icons/icon-attention-16.png +0 -0
- package/extension/icons/icon-attention-24.png +0 -0
- package/extension/icons/icon-attention-32.png +0 -0
- package/extension/icons/icon-attention-48.png +0 -0
- package/extension/icons/icon-attention-64.png +0 -0
- package/extension/icons/icon-attention.svg +18 -0
- package/extension/icons/icon-failed-128.png +0 -0
- package/extension/icons/icon-failed-16.png +0 -0
- package/extension/icons/icon-failed-24.png +0 -0
- package/extension/icons/icon-failed-32.png +0 -0
- package/extension/icons/icon-failed-48.png +0 -0
- package/extension/icons/icon-failed-64.png +0 -0
- package/extension/icons/icon-failed.svg +18 -0
- package/extension/icons/icon-idle-128.png +0 -0
- package/extension/icons/icon-idle-16.png +0 -0
- package/extension/icons/icon-idle-24.png +0 -0
- package/extension/icons/icon-idle-32.png +0 -0
- package/extension/icons/icon-idle-48.png +0 -0
- package/extension/icons/icon-idle-64.png +0 -0
- package/extension/icons/icon-idle.svg +18 -0
- package/extension/icons/icon-mark.svg +10 -0
- package/extension/icons/icon-running-128.png +0 -0
- package/extension/icons/icon-running-16.png +0 -0
- package/extension/icons/icon-running-24.png +0 -0
- package/extension/icons/icon-running-32.png +0 -0
- package/extension/icons/icon-running-48.png +0 -0
- package/extension/icons/icon-running-64.png +0 -0
- package/extension/icons/icon-running.svg +18 -0
- package/extension/icons/icon.svg +18 -0
- package/extension/manifest.json +44 -0
- package/extension/popup-ui.js +38 -0
- package/extension/popup.html +350 -0
- package/extension/popup.js +741 -0
- package/extension/sw.js +11127 -0
- package/extension/update-check.js +66 -0
- package/extension/welcome.html +110 -0
- package/extension/welcome.js +18 -0
- package/mcp/agent-identity.mjs +1007 -0
- package/mcp/cdp/bridge.mjs +239 -0
- package/mcp/cdp/browser-launch.mjs +1127 -0
- package/mcp/cdp/chrome-shim.mjs +1124 -0
- package/mcp/cdp/client.mjs +443 -0
- package/mcp/cli-invocation.mjs +67 -0
- package/mcp/config.mjs +191 -0
- package/mcp/foreground-guard.mjs +295 -0
- package/mcp/proc-win.mjs +112 -0
- package/mcp/secret-shape.mjs +155 -0
- package/mcp/server.mjs +3437 -0
- package/mcp/session-id.mjs +281 -0
- package/mcp/token.mjs +260 -0
- package/mcp/tool-tiers.mjs +84 -0
- package/mcp/trace.mjs +528 -0
- package/mcp/update-cli.mjs +163 -0
- package/mcp/update-notice.mjs +194 -0
- package/native-host/host.mjs +356 -0
- package/native-host/mac-foreground/aic-fg.swift +114 -0
- package/native-host/mac-foreground/build.mjs +43 -0
- package/package.json +46 -0
- package/scripts/agents.mjs +921 -0
- package/scripts/borrow-login.mjs +935 -0
- package/scripts/cli-browser.mjs +148 -0
- package/scripts/e2e-sweep.mjs +192 -0
- package/scripts/e2e.mjs +3911 -0
- package/scripts/fixture-server.mjs +1419 -0
- package/scripts/guard-publish.mjs +175 -0
- package/scripts/install.mjs +1383 -0
- package/scripts/launcher-provenance.mjs +74 -0
- package/scripts/pack-crx.mjs +82 -0
- package/scripts/pick.mjs +114 -0
- package/scripts/test-agents.mjs +968 -0
- package/scripts/test-borrow.mjs +289 -0
- package/scripts/test-config.mjs +618 -0
- package/scripts/test-e2e-sweep.mjs +251 -0
- package/scripts/test-foreground-guard.mjs +383 -0
- package/scripts/test-identity.mjs +1141 -0
- package/scripts/test-install-skill.mjs +378 -0
- package/scripts/test-protocol.mjs +712 -0
- package/scripts/test-security.mjs +1360 -0
- package/scripts/test-sw.mjs +14526 -0
- package/scripts/test-update.mjs +301 -0
- package/skills/agent-in-chrome/SKILL.md +60 -0
- package/skills/agent-in-chrome/references/actions-and-waits.md +136 -0
- package/skills/agent-in-chrome/references/amap-dir-dropdown.md +66 -0
- package/skills/agent-in-chrome/references/geetest-click-captcha.md +55 -0
- package/skills/agent-in-chrome/references/network-debugging.md +20 -0
- package/skills/agent-in-chrome/references/reading-pages.md +30 -0
- package/skills/agent-in-chrome/references/recovery.md +25 -0
- package/skills/agent-in-chrome/references/tabs-and-sessions.md +34 -0
- package/skills/install-agent-in-chrome/SKILL.md +252 -0
|
@@ -0,0 +1,712 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { spawn, execFileSync } from "node:child_process";
|
|
4
|
+
import net from "node:net";
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import os from "node:os";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
import { getToken, bridgeEndpoint } from "../mcp/token.mjs";
|
|
10
|
+
|
|
11
|
+
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
12
|
+
const TMP = fs.mkdtempSync(path.join(os.tmpdir(), "agent-in-chrome-proto-"));
|
|
13
|
+
const ep = bridgeEndpoint;
|
|
14
|
+
|
|
15
|
+
let passed = 0;
|
|
16
|
+
let failed = 0;
|
|
17
|
+
const check = (name, cond, extra = "") => {
|
|
18
|
+
if (cond) {
|
|
19
|
+
console.log(` \x1b[32m✓\x1b[0m ${name}`);
|
|
20
|
+
passed++;
|
|
21
|
+
} else {
|
|
22
|
+
console.log(` \x1b[31m✗\x1b[0m ${name}${extra ? ` — ${extra}` : ""}`);
|
|
23
|
+
failed++;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
27
|
+
async function until(fn, ms = 5000, step = 25) {
|
|
28
|
+
const end = Date.now() + ms;
|
|
29
|
+
while (Date.now() < end) {
|
|
30
|
+
if (await fn()) return true;
|
|
31
|
+
await sleep(step);
|
|
32
|
+
}
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const modernMeta = (version = "2026-07-28", extra = {}) => ({
|
|
37
|
+
"io.modelcontextprotocol/protocolVersion": version,
|
|
38
|
+
"io.modelcontextprotocol/clientCapabilities": {},
|
|
39
|
+
"io.modelcontextprotocol/clientInfo": { name: "proto-test", version: "1.0.0" },
|
|
40
|
+
...extra,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
function startServer(name, env = {}) {
|
|
44
|
+
const dir = path.join(TMP, name);
|
|
45
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
46
|
+
const sock = path.join(dir, "t.sock");
|
|
47
|
+
const token = getToken(dir);
|
|
48
|
+
const p = spawn(process.execPath, [path.join(ROOT, "mcp", "server.mjs")], {
|
|
49
|
+
env: {
|
|
50
|
+
...process.env,
|
|
51
|
+
AGENT_IN_CHROME_SOCK: sock,
|
|
52
|
+
AGENT_IN_CHROME_TRACE_DIR: path.join(dir, "traces"),
|
|
53
|
+
AGENT_IN_CHROME_SHOT_DIR: path.join(dir, "shots"),
|
|
54
|
+
AGENT_IN_CHROME_COOKIE_DIR: path.join(dir, "cookies"),
|
|
55
|
+
AGENT_IN_CHROME_SESSION_ID: `proto-${name}`,
|
|
56
|
+
...env,
|
|
57
|
+
},
|
|
58
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
59
|
+
});
|
|
60
|
+
const frames = [];
|
|
61
|
+
const junk = [];
|
|
62
|
+
let stderr = "";
|
|
63
|
+
p.stderr.setEncoding("utf8");
|
|
64
|
+
p.stderr.on("data", (d) => (stderr += d));
|
|
65
|
+
let buf = "";
|
|
66
|
+
p.stdout.setEncoding("utf8");
|
|
67
|
+
p.stdout.on("data", (c) => {
|
|
68
|
+
buf += c;
|
|
69
|
+
let i;
|
|
70
|
+
while ((i = buf.indexOf("\n")) >= 0) {
|
|
71
|
+
const line = buf.slice(0, i);
|
|
72
|
+
buf = buf.slice(i + 1);
|
|
73
|
+
if (!line.trim()) continue;
|
|
74
|
+
try {
|
|
75
|
+
frames.push(JSON.parse(line));
|
|
76
|
+
} catch {
|
|
77
|
+
junk.push(line);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
let n = 0;
|
|
82
|
+
const send = (o) => p.stdin.write(JSON.stringify(o) + "\n");
|
|
83
|
+
const rpc = (method, params, { timeout = 15000 } = {}) => {
|
|
84
|
+
const id = ++n;
|
|
85
|
+
send({ jsonrpc: "2.0", id, method, params });
|
|
86
|
+
return new Promise((res, rej) => {
|
|
87
|
+
const t0 = Date.now();
|
|
88
|
+
const tick = setInterval(() => {
|
|
89
|
+
const hit = frames.find((f) => f.id === id);
|
|
90
|
+
if (hit) {
|
|
91
|
+
clearInterval(tick);
|
|
92
|
+
res(hit);
|
|
93
|
+
} else if (Date.now() - t0 > timeout) {
|
|
94
|
+
clearInterval(tick);
|
|
95
|
+
rej(new Error(`${method} 超时`));
|
|
96
|
+
}
|
|
97
|
+
}, 10);
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
return { p, sock, token, frames, junk, send, rpc, nextId: () => ++n, stderrText: () => stderr };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function fakeHost(sock, token, replies = {}, { delayMs = 0 } = {}) {
|
|
104
|
+
const c = net.connect(ep(sock));
|
|
105
|
+
c.setEncoding("utf8");
|
|
106
|
+
const seen = [];
|
|
107
|
+
let b = "";
|
|
108
|
+
c.on("data", (chunk) => {
|
|
109
|
+
b += chunk;
|
|
110
|
+
let i;
|
|
111
|
+
while ((i = b.indexOf("\n")) >= 0) {
|
|
112
|
+
const line = b.slice(0, i).trim();
|
|
113
|
+
b = b.slice(i + 1);
|
|
114
|
+
if (!line) continue;
|
|
115
|
+
let m;
|
|
116
|
+
try {
|
|
117
|
+
m = JSON.parse(line);
|
|
118
|
+
} catch {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
seen.push(m);
|
|
122
|
+
if (m.type === "call" && replies[m.tool]) {
|
|
123
|
+
const reply = () => c.write(JSON.stringify({ id: m.id, type: "result", ok: true, data: replies[m.tool](m) }) + "\n");
|
|
124
|
+
if (delayMs > 0) setTimeout(reply, delayMs);
|
|
125
|
+
else reply();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
c.write(JSON.stringify({ type: "hello", role: "host", pid: 1, token }) + "\n");
|
|
130
|
+
c.write(JSON.stringify({ type: "hello", role: "extension", version: "0.54.0" }) + "\n");
|
|
131
|
+
return { c, seen };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const sockUp = (s) =>
|
|
135
|
+
new Promise((resolve) => {
|
|
136
|
+
const c = net.connect(ep(s));
|
|
137
|
+
const done = (v) => {
|
|
138
|
+
c.destroy();
|
|
139
|
+
resolve(v);
|
|
140
|
+
};
|
|
141
|
+
c.on("connect", () => done(true));
|
|
142
|
+
c.on("error", () => done(false));
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
console.log("\n\x1b[1m1. 版本协商矩阵(legacy initialize)\x1b[0m");
|
|
146
|
+
{
|
|
147
|
+
const s = startServer("negotiate");
|
|
148
|
+
const cases = [
|
|
149
|
+
["2025-11-25", "2025-11-25"],
|
|
150
|
+
["2025-06-18", "2025-06-18"],
|
|
151
|
+
["2025-03-26", "2025-03-26"],
|
|
152
|
+
["2024-11-05", "2024-11-05"],
|
|
153
|
+
["1900-01-01", "2025-11-25"],
|
|
154
|
+
["2099-01-01", "2025-11-25"],
|
|
155
|
+
[undefined, "2025-11-25"],
|
|
156
|
+
];
|
|
157
|
+
for (const [asked, want] of cases) {
|
|
158
|
+
const r = await s.rpc("initialize", {
|
|
159
|
+
protocolVersion: asked,
|
|
160
|
+
capabilities: {},
|
|
161
|
+
clientInfo: { name: "proto-test", version: "0" },
|
|
162
|
+
});
|
|
163
|
+
check(
|
|
164
|
+
`initialize 报 ${asked === undefined ? "(缺省)" : asked} → 回 ${want}`,
|
|
165
|
+
r.result?.protocolVersion === want,
|
|
166
|
+
r.result?.protocolVersion
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
const r2026 = await s.rpc("initialize", {
|
|
170
|
+
protocolVersion: "2026-07-28",
|
|
171
|
+
capabilities: {},
|
|
172
|
+
clientInfo: { name: "proto-test", version: "0" },
|
|
173
|
+
});
|
|
174
|
+
check(
|
|
175
|
+
"客户端在 initialize 里报 2026-07-28,回的仍是 2025-11-25(legacy 永不回 modern 版本)",
|
|
176
|
+
r2026.result?.protocolVersion === "2025-11-25",
|
|
177
|
+
r2026.result?.protocolVersion
|
|
178
|
+
);
|
|
179
|
+
check("initialize 声明了 tools.listChanged", r2026.result?.capabilities?.tools?.listChanged === true);
|
|
180
|
+
check("serverInfo 带上了 name/version", r2026.result?.serverInfo?.name === "agent-in-chrome" && !!r2026.result?.serverInfo?.version);
|
|
181
|
+
check(
|
|
182
|
+
"serverInfo 带 description / websiteUrl(Implementation 的可选字段)",
|
|
183
|
+
typeof r2026.result?.serverInfo?.description === "string" &&
|
|
184
|
+
/^https:\/\//.test(r2026.result?.serverInfo?.websiteUrl || ""),
|
|
185
|
+
JSON.stringify(r2026.result?.serverInfo)
|
|
186
|
+
);
|
|
187
|
+
check("legacy initialize 的 result 里没有 resultType(那是 modern 才有的)", !("resultType" in (r2026.result || {})));
|
|
188
|
+
s.p.kill();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
console.log("\n\x1b[1m2. server/discover(2026-07-28 规定服务端 MUST 实现)\x1b[0m");
|
|
192
|
+
{
|
|
193
|
+
const s = startServer("discover");
|
|
194
|
+
const d = await s.rpc("server/discover", { _meta: modernMeta() });
|
|
195
|
+
const R = d.result || {};
|
|
196
|
+
check("回的是 result 不是 error(= 我们是 modern 服务端)", !!d.result, JSON.stringify(d).slice(0, 200));
|
|
197
|
+
check("resultType 是 complete(Result 必带)", R.resultType === "complete", R.resultType);
|
|
198
|
+
check("supportedVersions 含 2026-07-28", Array.isArray(R.supportedVersions) && R.supportedVersions.includes("2026-07-28"), JSON.stringify(R.supportedVersions));
|
|
199
|
+
check(
|
|
200
|
+
"supportedVersions 同时列出 legacy 版本(客户端才挑得到能用的那个)",
|
|
201
|
+
(R.supportedVersions || []).includes("2025-11-25"),
|
|
202
|
+
JSON.stringify(R.supportedVersions)
|
|
203
|
+
);
|
|
204
|
+
check("capabilities 报了 tools", !!R.capabilities?.tools, JSON.stringify(R.capabilities));
|
|
205
|
+
check("_meta 带 serverInfo(规范:Servers SHOULD include this field)", R._meta?.["io.modelcontextprotocol/serverInfo"]?.name === "agent-in-chrome", JSON.stringify(R._meta));
|
|
206
|
+
check("ttlMs 是正数(CacheableResult 必填)", typeof R.ttlMs === "number" && R.ttlMs > 0, String(R.ttlMs));
|
|
207
|
+
check("cacheScope 是 private(工具表因本机配置 / 客户端而异,标 public 是假的)", R.cacheScope === "private", R.cacheScope);
|
|
208
|
+
const init = await s.rpc("initialize", { protocolVersion: "2025-11-25", capabilities: {}, clientInfo: { name: "x", version: "0" } });
|
|
209
|
+
check("discover 的 instructions 与 initialize 的逐字节相同", R.instructions === init.result?.instructions, `${(R.instructions || "").length} vs ${(init.result?.instructions || "").length}`);
|
|
210
|
+
|
|
211
|
+
const bare = await s.rpc("server/discover", {});
|
|
212
|
+
check("不带 _meta 的 discover 回 -32602(缺必填字段 = malformed)", bare.error?.code === -32602, JSON.stringify(bare).slice(0, 200));
|
|
213
|
+
const noCaps = await s.rpc("server/discover", { _meta: { "io.modelcontextprotocol/protocolVersion": "2026-07-28" } });
|
|
214
|
+
check("只带 protocolVersion、缺 clientCapabilities 也回 -32602", noCaps.error?.code === -32602, JSON.stringify(noCaps).slice(0, 200));
|
|
215
|
+
s.p.kill();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
console.log("\n\x1b[1m3. modern 分流与 UnsupportedProtocolVersionError\x1b[0m");
|
|
219
|
+
{
|
|
220
|
+
const s = startServer("dispatch");
|
|
221
|
+
await s.rpc("initialize", { protocolVersion: "2025-11-25", capabilities: {}, clientInfo: { name: "proto-test", version: "0" } });
|
|
222
|
+
s.send({ jsonrpc: "2.0", method: "notifications/initialized" });
|
|
223
|
+
|
|
224
|
+
const legacyList = await s.rpc("tools/list", {});
|
|
225
|
+
const modernList = await s.rpc("tools/list", { _meta: modernMeta() });
|
|
226
|
+
|
|
227
|
+
check(
|
|
228
|
+
"legacy tools/list 的 result 只有 tools 这一个键(没有 resultType/ttlMs/cacheScope/_meta)",
|
|
229
|
+
JSON.stringify(Object.keys(legacyList.result || {})) === JSON.stringify(["tools"]),
|
|
230
|
+
JSON.stringify(Object.keys(legacyList.result || {}))
|
|
231
|
+
);
|
|
232
|
+
const legacyTools = legacyList.result.tools;
|
|
233
|
+
const badKeys = legacyTools
|
|
234
|
+
.map((t) => Object.keys(t).sort().join(","))
|
|
235
|
+
.filter((k) => k !== "annotations,description,inputSchema,name");
|
|
236
|
+
check("每个工具的键正好是 name/description/inputSchema/annotations", badKeys.length === 0, badKeys[0]);
|
|
237
|
+
|
|
238
|
+
check("modern tools/list 带 resultType:complete", modernList.result?.resultType === "complete");
|
|
239
|
+
check("modern tools/list 带 ttlMs / cacheScope(ListToolsResult extends CacheableResult)", typeof modernList.result?.ttlMs === "number" && modernList.result?.cacheScope === "private");
|
|
240
|
+
check("modern tools/list 带 _meta.serverInfo", modernList.result?._meta?.["io.modelcontextprotocol/serverInfo"]?.name === "agent-in-chrome");
|
|
241
|
+
check(
|
|
242
|
+
"两个时代的 tools 数组逐字节相同(年代差别只在外面那层壳上)",
|
|
243
|
+
JSON.stringify(modernList.result.tools) === JSON.stringify(legacyTools)
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
const bad = await s.rpc("tools/list", { _meta: modernMeta("1900-01-01") });
|
|
247
|
+
check("modern 帧报未知版本 → -32022", bad.error?.code === -32022, JSON.stringify(bad).slice(0, 220));
|
|
248
|
+
check("message 是 Unsupported protocol version", bad.error?.message === "Unsupported protocol version", bad.error?.message);
|
|
249
|
+
check("data.requested 原样回显", bad.error?.data?.requested === "1900-01-01", JSON.stringify(bad.error?.data));
|
|
250
|
+
check("data.supported 含 2026-07-28", (bad.error?.data?.supported || []).includes("2026-07-28"), JSON.stringify(bad.error?.data?.supported));
|
|
251
|
+
check(
|
|
252
|
+
"data.supported 与 discover 的 supportedVersions 是同一份(否则客户端会按你给的版本重发、你又拒了)",
|
|
253
|
+
JSON.stringify(bad.error.data.supported) === JSON.stringify((await s.rpc("server/discover", { _meta: modernMeta() })).result.supportedVersions)
|
|
254
|
+
);
|
|
255
|
+
const noCaps = await s.rpc("tools/list", { _meta: { "io.modelcontextprotocol/protocolVersion": "2026-07-28" } });
|
|
256
|
+
check("modern 帧缺 clientCapabilities → -32602(规范原文的 MUST)", noCaps.error?.code === -32602, JSON.stringify(noCaps).slice(0, 200));
|
|
257
|
+
|
|
258
|
+
const mcall = await s.rpc("tools/call", { name: "browser_status", arguments: {}, _meta: modernMeta() });
|
|
259
|
+
check("带 modern _meta 的 tools/call 走 modern 语义(result 带 resultType)", mcall.result?.resultType === "complete", JSON.stringify(mcall.result).slice(0, 160));
|
|
260
|
+
const lcall = await s.rpc("tools/call", { name: "browser_status", arguments: {} });
|
|
261
|
+
check("不带 _meta 的 tools/call 仍是老形状(没有 resultType)", !!lcall.result && !("resultType" in lcall.result));
|
|
262
|
+
|
|
263
|
+
const un = await s.rpc("resources/list", {});
|
|
264
|
+
check("没声明的 capability 对应的方法回 -32601", un.error?.code === -32601, JSON.stringify(un).slice(0, 160));
|
|
265
|
+
const before = s.frames.length;
|
|
266
|
+
s.send({ jsonrpc: "2.0", method: "notifications/whatever", params: {} });
|
|
267
|
+
await sleep(200);
|
|
268
|
+
check("未知 notifications 静默丢弃,不回任何东西", s.frames.length === before, `多出 ${s.frames.length - before} 帧`);
|
|
269
|
+
s.p.kill();
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
console.log("\n\x1b[1m4. stdout 纯净(规范:MUST NOT write anything to stdout that is not a valid MCP message)\x1b[0m");
|
|
273
|
+
{
|
|
274
|
+
const s = startServer("stdout");
|
|
275
|
+
await s.rpc("initialize", { protocolVersion: "2025-11-25", capabilities: {}, clientInfo: { name: "proto-test", version: "0" } });
|
|
276
|
+
s.send({ jsonrpc: "2.0", method: "notifications/initialized" });
|
|
277
|
+
await s.rpc("tools/list", {});
|
|
278
|
+
await s.rpc("tools/call", { name: "browser_status", arguments: {} });
|
|
279
|
+
await s.rpc("tools/call", { name: "browser_trace", arguments: { list: true } });
|
|
280
|
+
await s.rpc("server/discover", { _meta: modernMeta() });
|
|
281
|
+
check("跑完一串调用,stdout 上每一行都是合法 JSON", s.junk.length === 0, JSON.stringify(s.junk.slice(0, 2)));
|
|
282
|
+
s.p.kill();
|
|
283
|
+
|
|
284
|
+
const dir = path.join(TMP, "mutate");
|
|
285
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
286
|
+
const probe = spawn(
|
|
287
|
+
process.execPath,
|
|
288
|
+
[
|
|
289
|
+
"--input-type=module",
|
|
290
|
+
"-e",
|
|
291
|
+
`import ${JSON.stringify(new URL("../mcp/server.mjs", import.meta.url).href)};` +
|
|
292
|
+
`console.log("MUTANT-LOG");console.info("MUTANT-INFO");console.debug("MUTANT-DEBUG");` +
|
|
293
|
+
`setTimeout(() => process.exit(0), 300);`,
|
|
294
|
+
],
|
|
295
|
+
{
|
|
296
|
+
env: {
|
|
297
|
+
...process.env,
|
|
298
|
+
AGENT_IN_CHROME_SOCK: path.join(dir, "t.sock"),
|
|
299
|
+
AGENT_IN_CHROME_TRACE_DIR: path.join(dir, "traces"),
|
|
300
|
+
AGENT_IN_CHROME_SESSION_ID: "proto-mutate",
|
|
301
|
+
},
|
|
302
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
303
|
+
}
|
|
304
|
+
);
|
|
305
|
+
let mo = "";
|
|
306
|
+
let me = "";
|
|
307
|
+
probe.stdout.on("data", (d) => (mo += d));
|
|
308
|
+
probe.stderr.on("data", (d) => (me += d));
|
|
309
|
+
await new Promise((r) => probe.on("exit", r));
|
|
310
|
+
check("console.log 不落 stdout", !/MUTANT-LOG/.test(mo), mo.slice(0, 200));
|
|
311
|
+
check("console.info 不落 stdout", !/MUTANT-INFO/.test(mo));
|
|
312
|
+
check("console.debug 不落 stdout", !/MUTANT-DEBUG/.test(mo));
|
|
313
|
+
check("三条都落到了 stderr(是钉过去了,不是被吃掉了)", /MUTANT-LOG/.test(me) && /MUTANT-INFO/.test(me) && /MUTANT-DEBUG/.test(me), me.slice(0, 200));
|
|
314
|
+
|
|
315
|
+
const swSrc = fs.readFileSync(path.join(ROOT, "extension", "sw.js"), "utf8");
|
|
316
|
+
const offenders = [...swSrc.matchAll(/console\.(log|info|debug|dir|table|trace)\s*\(/g)].map((m) => m[0]);
|
|
317
|
+
check(
|
|
318
|
+
"extension/sw.js 里没有任何 console.log/info/debug(CLI 模式下它们写的是 JSON-RPC 通道)",
|
|
319
|
+
offenders.length === 0,
|
|
320
|
+
offenders.join("、")
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
console.log("\n\x1b[1m5. 被关掉的工具走 isError 的内容(SEP-1303)\x1b[0m");
|
|
325
|
+
{
|
|
326
|
+
const dir = path.join(TMP, "disabled");
|
|
327
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
328
|
+
const cfg = path.join(dir, "config.json");
|
|
329
|
+
fs.writeFileSync(cfg, JSON.stringify({ tools: { profile: "observe" } }));
|
|
330
|
+
const s = startServer("disabled", { AGENT_IN_CHROME_CONFIG: cfg });
|
|
331
|
+
await s.rpc("initialize", { protocolVersion: "2025-11-25", capabilities: {}, clientInfo: { name: "proto-test", version: "0" } });
|
|
332
|
+
const r = await s.rpc("tools/call", { name: "browser_click", arguments: { ref: "ref_1" } });
|
|
333
|
+
check("不是协议错误", !r.error, JSON.stringify(r).slice(0, 200));
|
|
334
|
+
check("是 isError:true 的内容(模型读得到,才可能自我纠正)", r.result?.isError === true && typeof r.result?.content?.[0]?.text === "string");
|
|
335
|
+
check("文案说的是「关掉了」而不是「未知工具」", /关掉了/.test(r.result?.content?.[0]?.text || ""));
|
|
336
|
+
const un = await s.rpc("tools/call", { name: "browser_nope", arguments: {} });
|
|
337
|
+
check("真不存在的工具仍回 -32602(Unknown tool 是规范点名的 Protocol Error)", un.error?.code === -32602, JSON.stringify(un).slice(0, 160));
|
|
338
|
+
const m = await s.rpc("tools/call", { name: "browser_click", arguments: { ref: "ref_1" }, _meta: modernMeta() });
|
|
339
|
+
check("modern 帧里同样是 isError 的内容,且带 resultType", m.result?.isError === true && m.result?.resultType === "complete");
|
|
340
|
+
s.p.kill();
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
console.log("\n\x1b[1m6. _meta 前缀零冲突:四家客户端实样过 sidFor 的结果\x1b[0m");
|
|
344
|
+
{
|
|
345
|
+
const s = startServer("sid");
|
|
346
|
+
await s.rpc("initialize", { protocolVersion: "2025-11-25", capabilities: {}, clientInfo: { name: "proto-test", version: "0" } });
|
|
347
|
+
const h = fakeHost(s.sock, s.token, { status: () => ({ version: "0.54.0", connected: true, session: "x", tabs: [] }) });
|
|
348
|
+
check("假 host 连上了", await until(() => h.seen.length >= 0 && sockUp(s.sock)));
|
|
349
|
+
await until(() => s.frames.length >= 0, 100);
|
|
350
|
+
|
|
351
|
+
const samples = [
|
|
352
|
+
["Claude Code", { "claudecode/toolUseId": "toolu_01ABCDEF", progressToken: 2 }, "proto-sid"],
|
|
353
|
+
["ZCode", { session_id: "zc-sess-123" }, "proto-sid::zc-sess-123"],
|
|
354
|
+
["Trae", { chatSessionId: "trae-chat-9" }, "proto-sid"],
|
|
355
|
+
["Antigravity", { "antigravity.google/conversation_id": "3f2504e0-4f89-11d3-9a0c-0305e82c3301" }, "proto-sid::3f2504e0-4f89-11d3-9a0c-0305e82c3301"],
|
|
356
|
+
];
|
|
357
|
+
for (const [who, meta, wantSid] of samples) {
|
|
358
|
+
const before = h.seen.length;
|
|
359
|
+
await s.rpc("tools/call", { name: "browser_status", arguments: {}, _meta: meta });
|
|
360
|
+
const frame = h.seen.slice(before).find((f) => f.type === "call" && f.tool === "status");
|
|
361
|
+
check(`${who} 的 _meta → 过桥那一帧的 session = ${wantSid}`, frame?.session === wantSid, frame?.session);
|
|
362
|
+
}
|
|
363
|
+
check("chatSessionId 不参与 sid 派生(回落成进程级 sid)", true);
|
|
364
|
+
|
|
365
|
+
const src = fs.readFileSync(path.join(ROOT, "mcp", "server.mjs"), "utf8");
|
|
366
|
+
const written = [...src.matchAll(/_meta:\s*\{\s*\[?([A-Za-z_.\/"']+)/g)].map((m) => m[1]);
|
|
367
|
+
check(
|
|
368
|
+
"server.mjs 里往 _meta 写的键只有 META_SERVER_INFO 这一个",
|
|
369
|
+
written.length > 0 && written.every((k) => k === "META_SERVER_INFO"),
|
|
370
|
+
written.join("、")
|
|
371
|
+
);
|
|
372
|
+
h.c.destroy();
|
|
373
|
+
s.p.kill();
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
console.log("\n\x1b[1m7. 工具 annotations(每个工具都要有,且不许自相矛盾)\x1b[0m");
|
|
377
|
+
{
|
|
378
|
+
const s = startServer("anno", { AGENT_IN_CHROME_DEV: "1" });
|
|
379
|
+
await s.rpc("initialize", { protocolVersion: "2025-11-25", capabilities: {}, clientInfo: { name: "proto-test", version: "0" } });
|
|
380
|
+
const tools = (await s.rpc("tools/list", {})).result.tools;
|
|
381
|
+
const missing = tools.filter((t) => !t.annotations).map((t) => t.name);
|
|
382
|
+
check(`每个工具都有 annotations(${tools.length} 个)`, missing.length === 0, missing.join("、"));
|
|
383
|
+
const KEYS = ["readOnlyHint", "destructiveHint", "idempotentHint", "openWorldHint"];
|
|
384
|
+
const incomplete = tools.filter((t) => KEYS.some((k) => typeof t.annotations?.[k] !== "boolean")).map((t) => t.name);
|
|
385
|
+
check("四个 hint 都显式给了布尔值(不靠规范默认值猜)", incomplete.length === 0, incomplete.join("、"));
|
|
386
|
+
const contradict = tools.filter((t) => t.annotations.readOnlyHint && t.annotations.destructiveHint).map((t) => t.name);
|
|
387
|
+
check("只读工具不许同时标 destructiveHint", contradict.length === 0, contradict.join("、"));
|
|
388
|
+
const ro = tools.filter((t) => t.annotations.readOnlyHint).map((t) => t.name);
|
|
389
|
+
check(`确实按工具逐个定的(只读 ${ro.length} 个 / 共 ${tools.length} 个,不是一刀切)`, ro.length > 3 && ro.length < tools.length - 3, ro.join("、"));
|
|
390
|
+
const by = new Map(tools.map((t) => [t.name, t.annotations]));
|
|
391
|
+
check("browser_status / tabs_list / read_page / find 是只读", ["browser_status", "browser_tabs_list", "browser_read_page", "browser_find"].every((n) => by.get(n)?.readOnlyHint === true));
|
|
392
|
+
check(
|
|
393
|
+
"browser_navigate 不是只读(以用户身份发 GET,退订/注销/删除都可能是 GET)",
|
|
394
|
+
by.get("browser_navigate")?.readOnlyHint === false && by.get("browser_navigate")?.destructiveHint === true
|
|
395
|
+
);
|
|
396
|
+
check("browser_new_tab 同一口径(带 url 就是同一次 GET)", by.get("browser_new_tab")?.readOnlyHint === false && by.get("browser_new_tab")?.destructiveHint === true);
|
|
397
|
+
check("browser_eval / cdp / close_all / close_tab 标了破坏性", ["browser_eval", "browser_cdp", "browser_close_all", "browser_close_tab"].every((n) => by.get(n)?.destructiveHint === true));
|
|
398
|
+
check(
|
|
399
|
+
"browser_wait_for 不是只读(js 参数在页面主世界里求值,和 browser_eval 同一个世界)",
|
|
400
|
+
by.get("browser_wait_for")?.readOnlyHint === false
|
|
401
|
+
);
|
|
402
|
+
check("browser_trace 是封闭域(只读本机文件,不碰网络)", by.get("browser_trace")?.openWorldHint === false && by.get("browser_trace")?.readOnlyHint === true);
|
|
403
|
+
check("DEV_MODE 下的 browser_reload_extension 也有 annotations", !!by.get("browser_reload_extension"));
|
|
404
|
+
s.p.kill();
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
console.log("\n\x1b[1m8. 进度通知(慢工具 + progressToken)\x1b[0m");
|
|
408
|
+
{
|
|
409
|
+
const s = startServer("progress");
|
|
410
|
+
await s.rpc("initialize", { protocolVersion: "2025-11-25", capabilities: {}, clientInfo: { name: "proto-test", version: "0" } });
|
|
411
|
+
const h = fakeHost(s.sock, s.token, { status: () => ({ connected: true, tabs: [] }) });
|
|
412
|
+
await until(() => sockUp(s.sock));
|
|
413
|
+
const callId = s.nextId();
|
|
414
|
+
s.send({
|
|
415
|
+
jsonrpc: "2.0",
|
|
416
|
+
id: callId,
|
|
417
|
+
method: "tools/call",
|
|
418
|
+
params: { name: "browser_navigate", arguments: { url: "https://example.com" }, _meta: { progressToken: "pt-1" } },
|
|
419
|
+
});
|
|
420
|
+
const prog = () => s.frames.filter((f) => f.method === "notifications/progress" && f.params?.progressToken === "pt-1");
|
|
421
|
+
check("发出后立刻有一条「已发出」", await until(() => prog().length >= 1, 3000), JSON.stringify(prog()));
|
|
422
|
+
check("等待期间有心跳(≥3 条)", await until(() => prog().length >= 3, 9000), `${prog().length} 条`);
|
|
423
|
+
const ps = prog();
|
|
424
|
+
check("progress 单调递增(规范:MUST increase with each notification)", ps.every((f, i) => i === 0 || f.params.progress > ps[i - 1].params.progress), ps.map((f) => f.params.progress).join(","));
|
|
425
|
+
check("每条都带 message,且没有编造出来的百分比", ps.every((f) => typeof f.params.message === "string" && f.params.message.length > 0) && ps.every((f) => f.params.total === undefined), JSON.stringify(ps[0]?.params));
|
|
426
|
+
check("第一条说的是「已发出」", /已发出/.test(ps[0]?.params?.message || ""), ps[0]?.params?.message);
|
|
427
|
+
check("心跳报的是实测已等时长", /已 \d+ms/.test(ps[1]?.params?.message || ""), ps[1]?.params?.message);
|
|
428
|
+
const navFrame = h.seen.find((f) => f.type === "call" && f.tool === "navigate");
|
|
429
|
+
check("navigate 确实过了桥(这一节测的是真的在等,不是当场失败)", !!navFrame, JSON.stringify(h.seen.slice(-2)));
|
|
430
|
+
h.c.write(JSON.stringify({ id: navFrame.id, type: "result", ok: true, data: { ok: true } }) + "\n");
|
|
431
|
+
check("调用本身正常返回", await until(() => s.frames.some((f) => f.id === callId && f.result?.isError === false), 5000), JSON.stringify(s.frames.find((f) => f.id === callId)));
|
|
432
|
+
check("调用结束后补一条「完成」", await until(() => /完成/.test(prog().slice(-1)[0]?.params?.message || ""), 5000), prog().slice(-1)[0]?.params?.message);
|
|
433
|
+
const after = prog().length;
|
|
434
|
+
await sleep(3500);
|
|
435
|
+
check("完成之后不再发(规范:Progress notifications MUST stop after completion)", prog().length === after, `又多了 ${prog().length - after} 条`);
|
|
436
|
+
|
|
437
|
+
const before2 = s.frames.filter((f) => f.method === "notifications/progress").length;
|
|
438
|
+
await s.rpc("tools/call", { name: "browser_status", arguments: {} });
|
|
439
|
+
check("不带 progressToken 就一条都不发", s.frames.filter((f) => f.method === "notifications/progress").length === before2);
|
|
440
|
+
await s.rpc("tools/call", { name: "browser_status", arguments: {}, _meta: { progressToken: "pt-2" } });
|
|
441
|
+
check("快工具带了 token 也不发(协议允许「选择不发」)", s.frames.filter((f) => f.params?.progressToken === "pt-2").length === 0);
|
|
442
|
+
h.c.destroy();
|
|
443
|
+
s.p.kill();
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
console.log("\n\x1b[1m9. 取消真的把活停掉(不是收下就丢)\x1b[0m");
|
|
447
|
+
{
|
|
448
|
+
const s = startServer("cancel");
|
|
449
|
+
await s.rpc("initialize", { protocolVersion: "2025-11-25", capabilities: {}, clientInfo: { name: "proto-test", version: "0" } });
|
|
450
|
+
const h = fakeHost(s.sock, s.token, {});
|
|
451
|
+
await until(() => sockUp(s.sock));
|
|
452
|
+
const callId = s.nextId();
|
|
453
|
+
s.send({
|
|
454
|
+
jsonrpc: "2.0",
|
|
455
|
+
id: callId,
|
|
456
|
+
method: "tools/call",
|
|
457
|
+
params: { name: "browser_wait_for", arguments: { selector: "#never", timeoutMs: 60000 } },
|
|
458
|
+
});
|
|
459
|
+
check("调用已经下发到桥接", await until(() => h.seen.some((f) => f.type === "call" && f.tool === "wait_for"), 5000), JSON.stringify(h.seen.slice(-1)));
|
|
460
|
+
const bridgeId = h.seen.find((f) => f.type === "call" && f.tool === "wait_for").id;
|
|
461
|
+
s.send({ jsonrpc: "2.0", method: "notifications/cancelled", params: { requestId: callId, reason: "user pressed Ctrl-C" } });
|
|
462
|
+
check(
|
|
463
|
+
"往桥接发了 cancel 帧(工具层据此停下轮询)",
|
|
464
|
+
await until(() => h.seen.some((f) => f.type === "cancel" && f.id === bridgeId), 3000),
|
|
465
|
+
JSON.stringify(h.seen.filter((f) => f.type === "cancel"))
|
|
466
|
+
);
|
|
467
|
+
await sleep(1500);
|
|
468
|
+
check("被取消的调用不回任何 response(规范的 SHOULD NOT send a response)", !s.frames.some((f) => f.id === callId), JSON.stringify(s.frames.find((f) => f.id === callId)));
|
|
469
|
+
const beforeSecond = s.frames.length;
|
|
470
|
+
s.send({ jsonrpc: "2.0", method: "notifications/cancelled", params: { requestId: callId } });
|
|
471
|
+
await sleep(300);
|
|
472
|
+
check("重复取消同一个 id:静默忽略,不回任何东西(规范 Error Handling)", s.frames.length === beforeSecond);
|
|
473
|
+
const beforeUnknown = s.frames.length;
|
|
474
|
+
s.send({ jsonrpc: "2.0", method: "notifications/cancelled", params: { requestId: "no-such-id" } });
|
|
475
|
+
await sleep(300);
|
|
476
|
+
check("取消一个不认识的 id:同样静默忽略", s.frames.length === beforeUnknown);
|
|
477
|
+
|
|
478
|
+
const ok = await s.rpc("tools/call", { name: "browser_trace", arguments: { list: true } });
|
|
479
|
+
check("取消之后 server 照常服务下一次调用", ok.result?.isError === false, JSON.stringify(ok).slice(0, 160));
|
|
480
|
+
h.c.destroy();
|
|
481
|
+
s.p.kill();
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
{
|
|
485
|
+
console.log("\n\x1b[1m10. 前台归还:foreground-restore 过桥\x1b[0m");
|
|
486
|
+
const dir = fs.mkdtempSync(path.join(TMP, "fg-"));
|
|
487
|
+
const log = path.join(dir, "helper-stdin.log");
|
|
488
|
+
const helper = path.join(dir, "fake-helper.sh");
|
|
489
|
+
fs.writeFileSync(
|
|
490
|
+
helper,
|
|
491
|
+
`#!/bin/sh\nexec ${JSON.stringify(process.execPath)} -e 'const fs=require("fs");process.stdin.on("data",d=>fs.appendFileSync(${JSON.stringify(log)},d));process.stdin.on("end",()=>process.exit(0));setInterval(()=>{},1e9)'\n`
|
|
492
|
+
);
|
|
493
|
+
fs.chmodSync(helper, 0o755);
|
|
494
|
+
const lines = () => {
|
|
495
|
+
try {
|
|
496
|
+
return fs.readFileSync(log, "utf8").split("\n").map((x) => x.trim()).filter(Boolean);
|
|
497
|
+
} catch {
|
|
498
|
+
return [];
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
|
|
502
|
+
const s = startServer("fgguard", { AGENT_IN_CHROME_FG_HELPER: helper });
|
|
503
|
+
await s.rpc("initialize", { protocolVersion: "2024-11-05", capabilities: {}, clientInfo: { name: "p", version: "1" } });
|
|
504
|
+
s.send({ jsonrpc: "2.0", method: "notifications/initialized" });
|
|
505
|
+
const h = fakeHost(s.sock, s.token, { status: () => ({ connected: true, tabs: [] }) });
|
|
506
|
+
await sleep(300);
|
|
507
|
+
|
|
508
|
+
const r1 = await s.rpc("tools/call", { name: "browser_status", arguments: {} });
|
|
509
|
+
check("前置:工具调用真的过了桥", h.seen.some((m) => m.type === "call" && m.tool === "status"), JSON.stringify(h.seen).slice(0, 200));
|
|
510
|
+
for (let i = 0; i < 60 && lines().length === 0; i++) await sleep(50);
|
|
511
|
+
if (process.platform !== "darwin") {
|
|
512
|
+
check("非 macOS:一次都没起 helper(整套空实现)", lines().length === 0, JSON.stringify(lines()));
|
|
513
|
+
check("非 macOS:foreground-restore 帧也不该让 server 出事", true);
|
|
514
|
+
h.c.write(JSON.stringify({ type: "foreground-restore", tabId: 1, sid: "x" }) + "\n");
|
|
515
|
+
await sleep(300);
|
|
516
|
+
const r2 = await s.rpc("tools/call", { name: "browser_status", arguments: {} });
|
|
517
|
+
check("非 macOS:收下那一帧之后 server 照常服务", r2.result?.isError === false, JSON.stringify(r2).slice(0, 160));
|
|
518
|
+
} else {
|
|
519
|
+
check("每一次工具下发都武装了一次", lines().filter((l) => l.startsWith("arm ")).length >= 1, JSON.stringify(lines()));
|
|
520
|
+
check("这时还一次 confirm 都没有(结果因还没来)", lines().filter((l) => l === "confirm").length === 0, JSON.stringify(lines()));
|
|
521
|
+
|
|
522
|
+
h.c.write(JSON.stringify({ type: "foreground-restore", tabId: 42, sid: "proto-fgguard", restored: { activeRestored: null, focusRestored: null } }) + "\n");
|
|
523
|
+
await sleep(500);
|
|
524
|
+
check("结果因过了桥:helper 的 stdin 上多出一行 confirm", lines().filter((l) => l === "confirm").length === 1, JSON.stringify(lines()));
|
|
525
|
+
|
|
526
|
+
const r2 = await s.rpc("tools/call", { name: "browser_status", arguments: {} });
|
|
527
|
+
check("收下那一帧之后桥接照常(没被当成 result、也没断线)", r2.result?.isError === false, JSON.stringify(r2).slice(0, 160));
|
|
528
|
+
|
|
529
|
+
const s2 = startServer("fgoff", { AGENT_IN_CHROME_FG_HELPER: helper, AGENT_IN_CHROME_NO_FG_GUARD: "1" });
|
|
530
|
+
const log0 = lines().length;
|
|
531
|
+
await s2.rpc("initialize", { protocolVersion: "2024-11-05", capabilities: {}, clientInfo: { name: "p", version: "1" } });
|
|
532
|
+
s2.send({ jsonrpc: "2.0", method: "notifications/initialized" });
|
|
533
|
+
const h2 = fakeHost(s2.sock, s2.token, { status: () => ({ connected: true, tabs: [] }) });
|
|
534
|
+
await sleep(300);
|
|
535
|
+
await s2.rpc("tools/call", { name: "browser_status", arguments: {} });
|
|
536
|
+
h2.c.write(JSON.stringify({ type: "foreground-restore", tabId: 7, sid: "proto-fgoff" }) + "\n");
|
|
537
|
+
await sleep(500);
|
|
538
|
+
check("AGENT_IN_CHROME_NO_FG_GUARD=1:一个字都不往 helper 写", lines().length === log0, JSON.stringify(lines().slice(log0)));
|
|
539
|
+
h2.c.destroy();
|
|
540
|
+
s2.p.kill();
|
|
541
|
+
}
|
|
542
|
+
h.c.destroy();
|
|
543
|
+
s.p.kill();
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
const initClient = async (s) => {
|
|
547
|
+
await s.rpc("initialize", { protocolVersion: "2024-11-05", capabilities: {}, clientInfo: { name: "p", version: "1" } });
|
|
548
|
+
s.send({ jsonrpc: "2.0", method: "notifications/initialized" });
|
|
549
|
+
};
|
|
550
|
+
const forwardedCalls = (h) => h.seen.filter((m) => m.type === "call" && /^f\d+$/.test(String(m.id)));
|
|
551
|
+
const ownCalls = (h) => h.seen.filter((m) => m.type === "call" && /^c\d+$/.test(String(m.id)));
|
|
552
|
+
|
|
553
|
+
console.log("\n\x1b[1m10. browser_status.update(手动查更新的口)\x1b[0m");
|
|
554
|
+
{
|
|
555
|
+
const s = startServer("update-field", { AGENT_IN_CHROME_NO_UPDATE_NOTIFIER: "1" });
|
|
556
|
+
await s.rpc("initialize", { protocolVersion: "2025-11-25", capabilities: {}, clientInfo: { name: "proto-test", version: "0" } });
|
|
557
|
+
s.send({ jsonrpc: "2.0", method: "notifications/initialized" });
|
|
558
|
+
const h = fakeHost(s.sock, s.token, { status: () => ({ connected: true, tabs: [] }) });
|
|
559
|
+
await sleep(300);
|
|
560
|
+
|
|
561
|
+
const r = await s.rpc("tools/call", { name: "browser_status", arguments: {} });
|
|
562
|
+
let data = {};
|
|
563
|
+
try {
|
|
564
|
+
data = JSON.parse(r.result?.content?.[0]?.text || "{}");
|
|
565
|
+
} catch {}
|
|
566
|
+
const u = data.update;
|
|
567
|
+
check("browser_status 带 update 字段", !!u && typeof u === "object", JSON.stringify(data).slice(0, 300));
|
|
568
|
+
check("update.current 就是本机 server 版本", u?.current === data.versions?.server && typeof u?.current === "string", JSON.stringify(u));
|
|
569
|
+
check("update 三个键齐全(少一个客户端就得靠 undefined 猜)", u && "current" in u && "latest" in u && "hasUpdate" in u, JSON.stringify(u));
|
|
570
|
+
check("查询关掉时 latest 是 null,不是 undefined 也不是瞎猜的版本号", u?.latest === null, JSON.stringify(u));
|
|
571
|
+
check("latest 不知道时 hasUpdate 必须是 false(宁可漏报不误报)", u?.hasUpdate === false, JSON.stringify(u));
|
|
572
|
+
|
|
573
|
+
const r2 = await s.rpc("tools/call", { name: "browser_status", arguments: {} });
|
|
574
|
+
let u2 = {};
|
|
575
|
+
try {
|
|
576
|
+
u2 = JSON.parse(r2.result?.content?.[0]?.text || "{}").update;
|
|
577
|
+
} catch {}
|
|
578
|
+
check("连问两次拿到同一份(这是快照不是一次性消息)", JSON.stringify(u2) === JSON.stringify(u), `${JSON.stringify(u)} vs ${JSON.stringify(u2)}`);
|
|
579
|
+
check("这一节跑下来 stdout 没脏(更新查询不许往协议流里写字)", s.junk.length === 0, JSON.stringify(s.junk.slice(0, 2)));
|
|
580
|
+
|
|
581
|
+
h.c.destroy();
|
|
582
|
+
s.p.kill();
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
console.log("\n\x1b[1m11. isNewerVersion(纯函数)\x1b[0m");
|
|
586
|
+
{
|
|
587
|
+
const cases = [
|
|
588
|
+
["0.9.0:0.10.0", "not-newer"],
|
|
589
|
+
["0.10.0:0.9.0", "newer"],
|
|
590
|
+
["0.55.0:0.55.0", "not-newer"],
|
|
591
|
+
["0.55.1:0.55.0", "newer"],
|
|
592
|
+
["0.54.9:0.55.0", "not-newer"],
|
|
593
|
+
["1.0.0:0.99.99", "newer"],
|
|
594
|
+
["0.55:0.55.0", "not-newer"],
|
|
595
|
+
["0.55.0.1:0.55.0", "newer"],
|
|
596
|
+
["0.56.0-beta.1:0.55.0", "newer"],
|
|
597
|
+
["0.55.0-beta.1:0.55.0", "not-newer"],
|
|
598
|
+
["abc:0.55.0", "not-newer"],
|
|
599
|
+
["0.55.0:abc", "not-newer"],
|
|
600
|
+
];
|
|
601
|
+
const out = execFileSync(process.execPath, [path.join(ROOT, "mcp", "server.mjs"), "--compare-versions", ...cases.map(([p]) => p)], {
|
|
602
|
+
encoding: "utf8",
|
|
603
|
+
})
|
|
604
|
+
.trim()
|
|
605
|
+
.split("\n");
|
|
606
|
+
check("--compare-versions 每对回一行", out.length === cases.length, out.join(" | "));
|
|
607
|
+
for (let i = 0; i < cases.length; i++) {
|
|
608
|
+
const [pair, want] = cases[i];
|
|
609
|
+
check(`isNewerVersion(${pair.replace(":", ", ")}) → ${want}`, out[i] === `${pair} ${want}`, out[i]);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
console.log("\n\x1b[1m11b. 同版本 / 更旧 / 不报版本的从:一律不让位\x1b[0m");
|
|
614
|
+
{
|
|
615
|
+
const A = startServer("noyield-a", { AGENT_IN_CHROME_FAKE_VERSION: "0.55.0" });
|
|
616
|
+
await initClient(A);
|
|
617
|
+
check("主起来了,端点在 listen", await until(() => sockUp(A.sock)));
|
|
618
|
+
const h = fakeHost(A.sock, A.token, { status: () => ({ connected: true, tabs: [] }) });
|
|
619
|
+
await sleep(300);
|
|
620
|
+
|
|
621
|
+
const B = startServer("noyield-b", { AGENT_IN_CHROME_SOCK: A.sock, AGENT_IN_CHROME_FAKE_VERSION: "0.55.0" });
|
|
622
|
+
await initClient(B);
|
|
623
|
+
const rb = await B.rpc("tools/call", { name: "browser_status", arguments: {} });
|
|
624
|
+
check("同版本的从:调用照样通", rb.result?.isError === false, JSON.stringify(rb).slice(0, 160));
|
|
625
|
+
check("同版本的从是**被代发**的(f* id)= 它没当上主", forwardedCalls(h).length >= 1, JSON.stringify(h.seen).slice(0, 200));
|
|
626
|
+
check("同版本不让位:主一个字的让位日志都没有", !/桥接让位/.test(A.stderrText()), A.stderrText().slice(-300));
|
|
627
|
+
|
|
628
|
+
const C = startServer("noyield-c", { AGENT_IN_CHROME_SOCK: A.sock, AGENT_IN_CHROME_FAKE_VERSION: "0.54.0" });
|
|
629
|
+
await initClient(C);
|
|
630
|
+
const rc = await C.rpc("tools/call", { name: "browser_status", arguments: {} });
|
|
631
|
+
check("更旧的从:调用照样通", rc.result?.isError === false, JSON.stringify(rc).slice(0, 160));
|
|
632
|
+
check("更旧的从不让位", !/桥接让位/.test(A.stderrText()), A.stderrText().slice(-300));
|
|
633
|
+
|
|
634
|
+
const raw = net.connect(ep(A.sock));
|
|
635
|
+
raw.on("error", () => {});
|
|
636
|
+
raw.write(JSON.stringify({ type: "hello", role: "peer", pid: 99999, token: A.token }) + "\n");
|
|
637
|
+
await sleep(600);
|
|
638
|
+
check("不报版本的从(升级前的老会话)不让位", !/桥接让位/.test(A.stderrText()), A.stderrText().slice(-300));
|
|
639
|
+
|
|
640
|
+
const before = forwardedCalls(h).length;
|
|
641
|
+
const rb2 = await B.rpc("tools/call", { name: "browser_status", arguments: {} });
|
|
642
|
+
check(
|
|
643
|
+
"三种从都连上之后,主还是那个主(同一条 host 连接上又代发了一次)",
|
|
644
|
+
rb2.result?.isError === false && forwardedCalls(h).length > before,
|
|
645
|
+
`before=${before} after=${forwardedCalls(h).length} ${JSON.stringify(rb2).slice(0, 160)}`
|
|
646
|
+
);
|
|
647
|
+
|
|
648
|
+
raw.destroy();
|
|
649
|
+
h.c.destroy();
|
|
650
|
+
A.p.kill();
|
|
651
|
+
B.p.kill();
|
|
652
|
+
C.p.kill();
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
console.log("\n\x1b[1m11c. 更新版本的从连上来:老主让位,在飞的调用不丢\x1b[0m");
|
|
656
|
+
{
|
|
657
|
+
const O = startServer("yield-old", { AGENT_IN_CHROME_FAKE_VERSION: "0.55.0" });
|
|
658
|
+
await initClient(O);
|
|
659
|
+
check("老主起来了,端点在 listen", await until(() => sockUp(O.sock)));
|
|
660
|
+
const h1 = fakeHost(O.sock, O.token, { status: () => ({ connected: true, tabs: [] }) }, { delayMs: 1200 });
|
|
661
|
+
await sleep(300);
|
|
662
|
+
const inflight = O.rpc("tools/call", { name: "browser_status", arguments: {} });
|
|
663
|
+
await sleep(250);
|
|
664
|
+
check("让位发起前,老主手上确实有一条在飞的调用", ownCalls(h1).length === 1 && !O.frames.some((f) => f.id === 2));
|
|
665
|
+
|
|
666
|
+
const P = startServer("yield-bystander", { AGENT_IN_CHROME_SOCK: O.sock, AGENT_IN_CHROME_FAKE_VERSION: "0.55.0" });
|
|
667
|
+
await initClient(P);
|
|
668
|
+
const N = startServer("yield-new", { AGENT_IN_CHROME_SOCK: O.sock, AGENT_IN_CHROME_FAKE_VERSION: "0.56.0" });
|
|
669
|
+
await initClient(N);
|
|
670
|
+
check("老主认出对面更新,开始让位", await until(() => /桥接让位/.test(O.stderrText()), 5000), O.stderrText().slice(-400));
|
|
671
|
+
|
|
672
|
+
const r1 = await inflight;
|
|
673
|
+
check("让位期间在飞的那条调用照常拿到结果(排空跑完才交端点)", r1.result?.isError === false, JSON.stringify(r1).slice(0, 200));
|
|
674
|
+
check("排空没有超时(日志里没有那句超时)", !/让位排空超时/.test(O.stderrText()), O.stderrText().slice(-400));
|
|
675
|
+
|
|
676
|
+
check("新版本的从收到 yield 帧并接管", await until(() => /正在接管/.test(N.stderrText()), 6000), N.stderrText().slice(-400));
|
|
677
|
+
check("端点重新 listen 上了(新主 bind 成功)", await until(() => sockUp(O.sock), 6000));
|
|
678
|
+
|
|
679
|
+
const h2 = fakeHost(O.sock, O.token, { status: () => ({ connected: true, tabs: [] }) });
|
|
680
|
+
const rn = await N.rpc("tools/call", { name: "browser_status", arguments: {} });
|
|
681
|
+
check("新主能干活:它的调用直达 host(c* id,不是代发)", rn.result?.isError === false && ownCalls(h2).length >= 1, JSON.stringify(h2.seen).slice(0, 200));
|
|
682
|
+
|
|
683
|
+
check(
|
|
684
|
+
"老主让位后没变成孤儿:它的调用现在走新主代发(f* id)",
|
|
685
|
+
await until(async () => {
|
|
686
|
+
const r = await O.rpc("tools/call", { name: "browser_status", arguments: {} }).catch(() => null);
|
|
687
|
+
return r?.result?.isError === false && forwardedCalls(h2).length >= 1;
|
|
688
|
+
}, 12000, 500),
|
|
689
|
+
`${O.stderrText().slice(-400)} | ${JSON.stringify(h2.seen).slice(0, 200)}`
|
|
690
|
+
);
|
|
691
|
+
|
|
692
|
+
check(
|
|
693
|
+
"旁观的第三个会话自己重连到了新主(复用 gone() 那条自愈路)",
|
|
694
|
+
await until(async () => {
|
|
695
|
+
const r = await P.rpc("tools/call", { name: "browser_status", arguments: {} }).catch(() => null);
|
|
696
|
+
return r?.result?.isError === false;
|
|
697
|
+
}, 12000, 500),
|
|
698
|
+
P.stderrText().slice(-400)
|
|
699
|
+
);
|
|
700
|
+
|
|
701
|
+
h2.c.destroy();
|
|
702
|
+
h1.c.destroy();
|
|
703
|
+
O.p.kill();
|
|
704
|
+
N.p.kill();
|
|
705
|
+
P.p.kill();
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
console.log(failed ? `\n\x1b[31m${passed} 通过, ${failed} 失败\x1b[0m` : `\n\x1b[32m${passed} 通过, ${failed} 失败\x1b[0m`);
|
|
709
|
+
try {
|
|
710
|
+
fs.rmSync(TMP, { recursive: true, force: true });
|
|
711
|
+
} catch {}
|
|
712
|
+
process.exit(failed ? 1 : 0);
|