@liang-hz/agent-in-chrome 0.55.2 → 0.55.5
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.en.md +3 -3
- package/README.md +3 -3
- package/extension/manifest.json +1 -1
- package/extension/sw.js +91 -25
- package/mcp/agent-identity.mjs +293 -61
- package/mcp/cdp/browser-launch.mjs +160 -48
- package/mcp/server.mjs +4 -22
- package/native-host/host.mjs +28 -0
- package/package.json +1 -1
- package/scripts/borrow-login.mjs +6 -2
- package/scripts/e2e-sweep.mjs +3 -4
- package/scripts/e2e.mjs +77 -36
- package/scripts/install.mjs +70 -4
- package/scripts/launcher-spawn.mjs +22 -0
- package/scripts/pack-crx.mjs +2 -1
- package/scripts/test-agents.mjs +55 -22
- package/scripts/test-e2e-sweep.mjs +7 -2
- package/scripts/test-identity.mjs +307 -84
- package/scripts/test-install-skill.mjs +37 -2
- package/scripts/test-protocol.mjs +102 -1
- package/scripts/test-security.mjs +38 -17
- package/scripts/test-sw.mjs +103 -18
package/scripts/e2e.mjs
CHANGED
|
@@ -9,6 +9,9 @@ import path from "node:path";
|
|
|
9
9
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
10
10
|
import { getToken, timingSafeEqualStr, tokenPath, bridgeEndpoint, handshakeNonce, handshakeProof } from "../mcp/token.mjs";
|
|
11
11
|
|
|
12
|
+
if (process.platform === "win32" && !process.env.AGENT_IN_CHROME_REG_ROOT)
|
|
13
|
+
process.env.AGENT_IN_CHROME_REG_ROOT = `HKCU\\Software\\aic-e2e-${process.pid}`;
|
|
14
|
+
|
|
12
15
|
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
13
16
|
const TMP = fs.mkdtempSync(path.join(os.tmpdir(), "agent-in-chrome-e2e-"));
|
|
14
17
|
const SOCK = path.join(TMP, "t.sock");
|
|
@@ -944,6 +947,10 @@ console.log("\n\x1b[1mC. 启动器(最小 PATH)\x1b[0m");
|
|
|
944
947
|
let runtimeHome = os.homedir();
|
|
945
948
|
let RUNTIME_DIR = path.join(runtimeHome, ".agent-in-chrome", "agent-in-chrome");
|
|
946
949
|
let installedForThisRun = null;
|
|
950
|
+
if (process.platform === "win32") {
|
|
951
|
+
skipSection("C/D 的启动器握手是 POSIX 机制(.sh + /usr/bin:/bin),Windows 的 .bat 启动器由安装器一节自己的段覆盖");
|
|
952
|
+
} else {
|
|
953
|
+
|
|
947
954
|
if (!fs.existsSync(path.join(RUNTIME_DIR, "native-host-launcher.sh"))) {
|
|
948
955
|
const SOCK_SUFFIX = "/.agent-in-chrome/agent-in-chrome.sock".length;
|
|
949
956
|
let base = os.tmpdir();
|
|
@@ -1071,6 +1078,7 @@ const MCP_LAUNCHER = path.join(RUNTIME_DIR, "mcp-launcher.sh");
|
|
|
1071
1078
|
mp.kill();
|
|
1072
1079
|
}
|
|
1073
1080
|
}
|
|
1081
|
+
}
|
|
1074
1082
|
|
|
1075
1083
|
console.log("\n\x1b[1mE. 多会话并发\x1b[0m");
|
|
1076
1084
|
{
|
|
@@ -1611,10 +1619,10 @@ console.log("\n\x1b[1mF3. 会话名按 params._meta 精确归属\x1b[0m");
|
|
|
1611
1619
|
}
|
|
1612
1620
|
|
|
1613
1621
|
console.log("\n\x1b[1mG. CLI 浏览器记录按 profile 隔离\x1b[0m");
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
} else {
|
|
1622
|
+
{
|
|
1623
|
+
const G_WIN = process.platform === "win32";
|
|
1617
1624
|
const G_HOME = path.join(TMP, "g-home");
|
|
1625
|
+
const homeEnv = G_WIN ? { HOME: G_HOME, USERPROFILE: G_HOME } : { HOME: G_HOME };
|
|
1618
1626
|
const PROF_A = path.join(TMP, "g-prof-a");
|
|
1619
1627
|
const PROF_B = path.join(TMP, "g-prof-b");
|
|
1620
1628
|
for (const d of [G_HOME, PROF_A, PROF_B]) fs.mkdirSync(d, { recursive: true });
|
|
@@ -1626,14 +1634,14 @@ if (process.platform === "win32") {
|
|
|
1626
1634
|
process.execPath,
|
|
1627
1635
|
["-e", `import(${JSON.stringify(LAUNCH_MOD)}).then((m) => console.log(JSON.stringify(m.browserStatus())))`],
|
|
1628
1636
|
{
|
|
1629
|
-
env: { ...process.env,
|
|
1637
|
+
env: { ...process.env, ...homeEnv, ...(profileDir ? { AGENT_IN_CHROME_PROFILE: profileDir } : {}) },
|
|
1630
1638
|
encoding: "utf8",
|
|
1631
1639
|
}
|
|
1632
1640
|
).trim()
|
|
1633
1641
|
);
|
|
1634
1642
|
const stopUnder = (profileDir) =>
|
|
1635
1643
|
execFileSync(process.execPath, [path.join(ROOT, "scripts", "cli-browser.mjs"), "--stop"], {
|
|
1636
|
-
env: { ...process.env,
|
|
1644
|
+
env: { ...process.env, ...homeEnv, AGENT_IN_CHROME_PROFILE: profileDir },
|
|
1637
1645
|
encoding: "utf8",
|
|
1638
1646
|
});
|
|
1639
1647
|
|
|
@@ -1653,10 +1661,14 @@ if (process.platform === "win32") {
|
|
|
1653
1661
|
);
|
|
1654
1662
|
check("干净 profile 一开始什么都没有", stA.running === false && stA.stale === false, JSON.stringify(stA));
|
|
1655
1663
|
|
|
1656
|
-
const
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1664
|
+
const G_STUB = path.join(G_HOME, "impostor-stub.mjs");
|
|
1665
|
+
fs.writeFileSync(G_STUB, "setTimeout(() => {}, 300000);\n");
|
|
1666
|
+
const impostor = G_WIN
|
|
1667
|
+
? spawn(process.execPath, [G_STUB, `--user-data-dir=${PROF_B}`], { stdio: "ignore", detached: true, windowsHide: true })
|
|
1668
|
+
: spawn("/bin/sh", ["-c", "while :; do sleep 1; done", `--user-data-dir=${PROF_B}`], {
|
|
1669
|
+
stdio: "ignore",
|
|
1670
|
+
detached: true,
|
|
1671
|
+
});
|
|
1660
1672
|
let impostorExited = false;
|
|
1661
1673
|
impostor.on("exit", () => (impostorExited = true));
|
|
1662
1674
|
await sleep(300);
|
|
@@ -1692,20 +1704,21 @@ if (process.platform === "win32") {
|
|
|
1692
1704
|
|
|
1693
1705
|
if (!impostorExited) {
|
|
1694
1706
|
try {
|
|
1695
|
-
process.kill(
|
|
1707
|
+
if (G_WIN) process.kill(impostor.pid, "SIGKILL");
|
|
1708
|
+
else process.kill(-impostor.pid, "SIGKILL");
|
|
1696
1709
|
} catch {}
|
|
1697
1710
|
}
|
|
1698
1711
|
}
|
|
1699
1712
|
|
|
1700
1713
|
console.log("\n\x1b[1mH. 认领在跑的浏览器:端点必须自证身份\x1b[0m");
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
} else {
|
|
1714
|
+
{
|
|
1715
|
+
const H_WIN = process.platform === "win32";
|
|
1704
1716
|
const H_HOME = path.join(TMP, "h-home");
|
|
1717
|
+
const homeEnv = H_WIN ? { HOME: H_HOME, USERPROFILE: H_HOME } : { HOME: H_HOME };
|
|
1705
1718
|
const PROF_C = path.join(TMP, "h-prof-c");
|
|
1706
1719
|
for (const d of [H_HOME, PROF_C]) fs.mkdirSync(d, { recursive: true });
|
|
1707
1720
|
const LAUNCH_MOD = pathToFileURL(path.join(ROOT, "mcp", "cdp", "browser-launch.mjs")).href;
|
|
1708
|
-
const NO_CHROME = path.join(TMP, "no-such-chrome");
|
|
1721
|
+
const NO_CHROME = H_WIN ? path.join(TMP, "no-such-chrome.exe") : path.join(TMP, "no-such-chrome");
|
|
1709
1722
|
|
|
1710
1723
|
const MINE = "/devtools/browser/11111111-1111-4111-8111-111111111111";
|
|
1711
1724
|
const OTHERS = "/devtools/browser/22222222-2222-4222-8222-222222222222";
|
|
@@ -1739,7 +1752,7 @@ if (process.platform === "win32") {
|
|
|
1739
1752
|
{
|
|
1740
1753
|
env: {
|
|
1741
1754
|
...process.env,
|
|
1742
|
-
|
|
1755
|
+
...homeEnv,
|
|
1743
1756
|
AGENT_IN_CHROME_PROFILE: PROF_C,
|
|
1744
1757
|
AGENT_IN_CHROME_BIN: NO_CHROME,
|
|
1745
1758
|
AGENT_IN_CHROME_CDP_TRANSPORT: "port",
|
|
@@ -1763,14 +1776,18 @@ if (process.platform === "win32") {
|
|
|
1763
1776
|
const stale = await ensure();
|
|
1764
1777
|
check("没人占着 profile 时,旧端口文件当冷启动处理", stale.ok === false && stale.err.includes(NO_CHROME), stale.err);
|
|
1765
1778
|
|
|
1766
|
-
const
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1779
|
+
const H_STUB = path.join(H_HOME, "holder-stub.mjs");
|
|
1780
|
+
fs.writeFileSync(H_STUB, "setTimeout(() => {}, 300000);\n");
|
|
1781
|
+
const holder = H_WIN
|
|
1782
|
+
? spawn(process.execPath, [H_STUB, `--user-data-dir=${PROF_C}`], { stdio: "ignore", detached: true, windowsHide: true })
|
|
1783
|
+
: spawn("/bin/sh", ["-c", "while :; do sleep 1; done", `--user-data-dir=${PROF_C}`], {
|
|
1784
|
+
stdio: "ignore",
|
|
1785
|
+
detached: true,
|
|
1786
|
+
});
|
|
1770
1787
|
let holderExited = false;
|
|
1771
1788
|
holder.on("exit", () => (holderExited = true));
|
|
1772
|
-
await sleep(300);
|
|
1773
|
-
fs.symlinkSync(`${os.hostname()}-${holder.pid}`, path.join(PROF_C, "SingletonLock"));
|
|
1789
|
+
await sleep(H_WIN ? 500 : 300);
|
|
1790
|
+
if (!H_WIN) fs.symlinkSync(`${os.hostname()}-${holder.pid}`, path.join(PROF_C, "SingletonLock"));
|
|
1774
1791
|
|
|
1775
1792
|
servedPath = OTHERS;
|
|
1776
1793
|
const wrong = await ensure();
|
|
@@ -1781,12 +1798,17 @@ if (process.platform === "win32") {
|
|
|
1781
1798
|
servedPath = MINE;
|
|
1782
1799
|
const right = await ensure();
|
|
1783
1800
|
check("GUID 对得上就认领(复用,不重开)", right.ok === true && right.adopted === true, JSON.stringify(right).slice(0, 240));
|
|
1784
|
-
check(
|
|
1801
|
+
check(
|
|
1802
|
+
"认领到的 pid 取自 Chrome 自己的占用凭证(POSIX 是 SingletonLock,Windows 是进程表)",
|
|
1803
|
+
right.ok === true && right.pid === holder.pid,
|
|
1804
|
+
JSON.stringify(right.pid)
|
|
1805
|
+
);
|
|
1785
1806
|
check("认领时把版本号一并问出来了", right.ok === true && String(right.version).startsWith("Chrome/"), right.version);
|
|
1786
1807
|
|
|
1787
1808
|
fake.close();
|
|
1788
1809
|
try {
|
|
1789
|
-
process.kill(
|
|
1810
|
+
if (H_WIN) process.kill(holder.pid, "SIGKILL");
|
|
1811
|
+
else process.kill(-holder.pid, "SIGKILL");
|
|
1790
1812
|
} catch {}
|
|
1791
1813
|
await waitFor(() => holderExited, 3000);
|
|
1792
1814
|
}
|
|
@@ -2662,7 +2684,11 @@ if (process.platform === "win32") {
|
|
|
2662
2684
|
}, 15000);
|
|
2663
2685
|
|
|
2664
2686
|
const u = runInstaller(["--uninstall", "--agents=kimi"]);
|
|
2665
|
-
check(
|
|
2687
|
+
check(
|
|
2688
|
+
"Windows: 卸载不以一段堆栈收场(有残留也把每一步跑完,退出码如实非零)",
|
|
2689
|
+
[0, 1].includes(u.status) && !/^\s+at /m.test(u.out),
|
|
2690
|
+
`status=${u.status} ${u.out.slice(-500)}`
|
|
2691
|
+
);
|
|
2666
2692
|
if (!locked) skipSection("这台机器上没能把文件锁住,「一步失败不阻断其余步骤」这条对照略过");
|
|
2667
2693
|
else {
|
|
2668
2694
|
check("Windows: 一步失败之后,后面的步骤照样跑完 — 注册表键被删掉", !regHas(KEY), u.out.slice(-800));
|
|
@@ -3287,8 +3313,16 @@ console.log("\n\x1b[1mM. 孤儿浏览器回收\x1b[0m");
|
|
|
3287
3313
|
const sent = [];
|
|
3288
3314
|
await sweepOrphansAsync({ procs, kill: (pid, sig) => sent.push(`${pid}:${sig}`), isAlive: () => false, sleep: async () => {} });
|
|
3289
3315
|
check("账上是管道的:只打主进程,绝不打进程组(-pid 会误伤别人)", sent.includes("7001:SIGTERM") && !sent.includes("-7001:SIGTERM"), sent.join(" "));
|
|
3290
|
-
|
|
3291
|
-
|
|
3316
|
+
if (process.platform === "win32") {
|
|
3317
|
+
check(
|
|
3318
|
+
"Windows:端口/无账的也只打主进程(没有进程组可打)",
|
|
3319
|
+
sent.includes("7002:SIGTERM") && sent.includes("7003:SIGTERM") && !sent.some((s) => s.startsWith("-")),
|
|
3320
|
+
sent.join(" ")
|
|
3321
|
+
);
|
|
3322
|
+
} else {
|
|
3323
|
+
check("账上是端口的:照旧整组打(detached,pid 就是组长)", sent.includes("-7002:SIGTERM"), sent.join(" "));
|
|
3324
|
+
check("没账的保持老行为(整组打)", sent.includes("-7003:SIGTERM"), sent.join(" "));
|
|
3325
|
+
}
|
|
3292
3326
|
}
|
|
3293
3327
|
|
|
3294
3328
|
const alive = (pid) => pid === 1000;
|
|
@@ -3343,6 +3377,7 @@ console.log("\n\x1b[1mM. 孤儿浏览器回收\x1b[0m");
|
|
|
3343
3377
|
check("命令行里解析得出 user-data-dir", profileDirFrom("chrome --user-data-dir=/tmp/p --remote-debugging-port=0 about:blank") === "/tmp/p");
|
|
3344
3378
|
check("路径带空格也解析得完整(Application Support 之流)", profileDirFrom("chrome --user-data-dir=/Users/me/Library/Application Support/x --remote-debugging-port=0") === "/Users/me/Library/Application Support/x", JSON.stringify(profileDirFrom("chrome --user-data-dir=/Users/me/Library/Application Support/x --remote-debugging-port=0")));
|
|
3345
3379
|
check("没有 user-data-dir 时返回 null(不猜)", profileDirFrom("chrome --headless") === null);
|
|
3380
|
+
check("尾随换行不拦路(/bin/ps -p 单点查询的原样输出)", profileDirFrom("chrome --user-data-dir=/tmp/p\n") === "/tmp/p");
|
|
3346
3381
|
|
|
3347
3382
|
{
|
|
3348
3383
|
let clock = 0;
|
|
@@ -3389,9 +3424,7 @@ console.log("\n\x1b[1mM. 孤儿浏览器回收\x1b[0m");
|
|
|
3389
3424
|
})());
|
|
3390
3425
|
}
|
|
3391
3426
|
|
|
3392
|
-
|
|
3393
|
-
skipSection("真实起浏览器/收孤儿的链路属于 CLI 模式,Windows 上尚未支持(上面的纯函数判定已全部验过)");
|
|
3394
|
-
} else {
|
|
3427
|
+
{
|
|
3395
3428
|
const M_PROFILE = path.join(TMP, "m-profile");
|
|
3396
3429
|
const holder = spawn(
|
|
3397
3430
|
process.execPath,
|
|
@@ -3433,6 +3466,9 @@ console.log("\n\x1b[1mM. 孤儿浏览器回收\x1b[0m");
|
|
|
3433
3466
|
check("没有杀不掉的", swept.stubborn.length === 0, JSON.stringify(swept.stubborn));
|
|
3434
3467
|
}
|
|
3435
3468
|
|
|
3469
|
+
if (process.platform === "win32") {
|
|
3470
|
+
skipSection("Windows 的 kill 是 TerminateProcess 一刀,「不理 SIGTERM」的进程不存在,先礼后兵的升级阶梯无从测起");
|
|
3471
|
+
} else {
|
|
3436
3472
|
{
|
|
3437
3473
|
const { sweepOrphans, psSnapshot, pidAlive, readProfileRecord } = await import(
|
|
3438
3474
|
pathToFileURL(path.join(ROOT, "mcp", "cdp", "browser-launch.mjs")).href
|
|
@@ -3480,6 +3516,7 @@ console.log("\n\x1b[1mM. 孤儿浏览器回收\x1b[0m");
|
|
|
3480
3516
|
|
|
3481
3517
|
if (isUp(stubPid)) { try { process.kill(-stubPid, "SIGKILL"); } catch {} }
|
|
3482
3518
|
}
|
|
3519
|
+
}
|
|
3483
3520
|
|
|
3484
3521
|
{
|
|
3485
3522
|
const MP_PROFILE = path.join(TMP, "m-pipe-profile");
|
|
@@ -3685,6 +3722,9 @@ console.log("\n\x1b[1mN. 会话身份跨进程重启存活\x1b[0m");
|
|
|
3685
3722
|
check("自己这条 SOCK 不碰", fs.existsSync(mine));
|
|
3686
3723
|
check("不带 profile hash 的那条不归它管", fs.existsSync(other));
|
|
3687
3724
|
|
|
3725
|
+
if (process.platform === "win32") {
|
|
3726
|
+
skipSection("probeSock 的真 socket 探活是 POSIX 机制(Windows 桥接走命名管道,不落盘无残留)");
|
|
3727
|
+
} else {
|
|
3688
3728
|
const liveDir = fs.mkdtempSync(path.join(os.tmpdir(), "aic-sk-"));
|
|
3689
3729
|
fs.chmodSync(liveDir, 0o700);
|
|
3690
3730
|
const livePath = path.join(liveDir, "agent-in-chrome-cli-eeeeeeee.sock");
|
|
@@ -3700,6 +3740,7 @@ console.log("\n\x1b[1mN. 会话身份跨进程重启存活\x1b[0m");
|
|
|
3700
3740
|
check("真探活:还在 listen 的一个字不碰", fs.existsSync(livePath));
|
|
3701
3741
|
await new Promise((r) => srv.close(r));
|
|
3702
3742
|
fs.rmSync(liveDir, { recursive: true, force: true });
|
|
3743
|
+
}
|
|
3703
3744
|
}
|
|
3704
3745
|
|
|
3705
3746
|
const psTok = (out) => parentStartToken(123, () => out, { platform: "darwin" });
|
|
@@ -3725,12 +3766,8 @@ console.log("\n\x1b[1mN. 会话身份跨进程重启存活\x1b[0m");
|
|
|
3725
3766
|
}
|
|
3726
3767
|
}
|
|
3727
3768
|
|
|
3728
|
-
|
|
3729
|
-
|
|
3730
|
-
} else {
|
|
3731
|
-
const real = parentStartToken(process.ppid);
|
|
3732
|
-
check("真机上确实读得到父进程启动时刻", typeof real === "string" && real.length > 0, String(real));
|
|
3733
|
-
}
|
|
3769
|
+
const real = parentStartToken(process.ppid);
|
|
3770
|
+
check("真机上确实读得到父进程启动时刻", typeof real === "string" && real.length > 0, String(real));
|
|
3734
3771
|
|
|
3735
3772
|
const { makeInheritor, fingerprintOf } = await import(pathToFileURL(path.join(ROOT, "mcp", "session-id.mjs")).href);
|
|
3736
3773
|
const ME = { workspace: "agent-in-chrome", brandFallback: null, surface: "cli" };
|
|
@@ -3819,7 +3856,11 @@ console.log("\n\x1b[1mN. 会话身份跨进程重启存活\x1b[0m");
|
|
|
3819
3856
|
}
|
|
3820
3857
|
{
|
|
3821
3858
|
const dir = path.join(TMP, "inherit-payload");
|
|
3822
|
-
deriveSessionId({
|
|
3859
|
+
deriveSessionId({
|
|
3860
|
+
env: {}, ppid: 4242, pid: 7777, startToken: () => "tok", identity: ME,
|
|
3861
|
+
inherit: makeInheritor({ dir, isAlive: () => false }),
|
|
3862
|
+
claim: makeClaimer({ dir, isAlive: () => false }),
|
|
3863
|
+
});
|
|
3823
3864
|
const wrote = JSON.parse(fs.readFileSync(path.join(dir, "session-4242-tok.json"), "utf8"));
|
|
3824
3865
|
check("认领时把身份指纹写进文件", JSON.stringify(wrote.id) === JSON.stringify(fp), JSON.stringify(wrote));
|
|
3825
3866
|
}
|
package/scripts/install.mjs
CHANGED
|
@@ -51,6 +51,7 @@ const RUNTIME_DIR = path.join(DATA_DIR, "agent-in-chrome");
|
|
|
51
51
|
const HOST_SRC = path.join(ROOT, "native-host", "host.mjs");
|
|
52
52
|
const FG_SWIFT_SRC = path.join(ROOT, "native-host", "mac-foreground", "aic-fg.swift");
|
|
53
53
|
const SERVER_SRC = path.join(ROOT, "mcp", "server.mjs");
|
|
54
|
+
const BIN_SRC = path.join(ROOT, "bin", "agent-in-chrome.mjs");
|
|
54
55
|
const HOST_PATH = path.join(RUNTIME_DIR, "host.mjs");
|
|
55
56
|
const SERVER_PATH = path.join(RUNTIME_DIR, "server.mjs");
|
|
56
57
|
const MCP_DIR = path.join(ROOT, "mcp");
|
|
@@ -75,7 +76,7 @@ function scanDeps(entryAbs, out = new Set()) {
|
|
|
75
76
|
}
|
|
76
77
|
return out;
|
|
77
78
|
}
|
|
78
|
-
const SERVER_DEPS = [...scanDeps(SERVER_SRC, scanDeps(HOST_SRC))].sort();
|
|
79
|
+
const SERVER_DEPS = [...scanDeps(SERVER_SRC, scanDeps(HOST_SRC, scanDeps(BIN_SRC)))].sort();
|
|
79
80
|
const EXT_FILES = ["sw.js", "manifest.json"];
|
|
80
81
|
const EXT_STABLE_DIR = path.join(DATA_DIR, "extension");
|
|
81
82
|
const IS_GIT_CHECKOUT = fs.existsSync(path.join(ROOT, ".git"));
|
|
@@ -286,6 +287,13 @@ function checkStrayBrowsers() {
|
|
|
286
287
|
* 映射都指向被替换掉的那个 inode,不会读到半截新文件;
|
|
287
288
|
* 也从不「原地截断重写」一个可能正被读取或执行的文件。
|
|
288
289
|
* 同目录是硬要求:跨设备的 rename 会 EXDEV,而 os.tmpdir() 在很多机器上是另一个卷。
|
|
290
|
+
*
|
|
291
|
+
* Windows 上 rename 盖不到一个正被别的进程打开着的目标(NTFS 没有「替换已打开
|
|
292
|
+
* 文件」的 POSIX 语义,rename 报 EPERM)。这时走「让位舞步」:旧文件先改名让位——
|
|
293
|
+
* 它自己的打开句柄跟着旧名字走,内容一个字节不动;新文件立刻顶上原名;旧名字
|
|
294
|
+
* 尽力而为地删(被握着时是 delete-pending,握着它的进程一退出系统才真删)。
|
|
295
|
+
* 代价是两次 rename 之间有几微秒的 ENOENT 窗口,只在「目标正被握着」这一种
|
|
296
|
+
* 情形付出;常规升级(没人握着)仍然走上面的单次 rename,一个窗口都没有。
|
|
289
297
|
*/
|
|
290
298
|
function replaceAtomically(from, to, mode = 0o644) {
|
|
291
299
|
fs.mkdirSync(path.dirname(to), { recursive: true });
|
|
@@ -294,12 +302,38 @@ function replaceAtomically(from, to, mode = 0o644) {
|
|
|
294
302
|
if (!WIN) fs.chmodSync(tmp, mode);
|
|
295
303
|
try {
|
|
296
304
|
fs.renameSync(tmp, to);
|
|
305
|
+
return;
|
|
297
306
|
} catch (e) {
|
|
307
|
+
if (!WIN || !["EPERM", "EACCES", "EBUSY"].includes(e.code)) {
|
|
308
|
+
try {
|
|
309
|
+
fs.unlinkSync(tmp);
|
|
310
|
+
} catch {}
|
|
311
|
+
throw e;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
const stale = `${to}.stale-${process.pid}`;
|
|
315
|
+
try {
|
|
316
|
+
fs.renameSync(to, stale);
|
|
317
|
+
} catch (e2) {
|
|
318
|
+
try {
|
|
319
|
+
fs.unlinkSync(tmp);
|
|
320
|
+
} catch {}
|
|
321
|
+
throw e2;
|
|
322
|
+
}
|
|
323
|
+
try {
|
|
324
|
+
fs.renameSync(tmp, to);
|
|
325
|
+
} catch (e3) {
|
|
326
|
+
try {
|
|
327
|
+
fs.renameSync(stale, to);
|
|
328
|
+
} catch {}
|
|
298
329
|
try {
|
|
299
330
|
fs.unlinkSync(tmp);
|
|
300
331
|
} catch {}
|
|
301
|
-
throw
|
|
332
|
+
throw e3;
|
|
302
333
|
}
|
|
334
|
+
try {
|
|
335
|
+
fs.unlinkSync(stale);
|
|
336
|
+
} catch {}
|
|
303
337
|
}
|
|
304
338
|
|
|
305
339
|
function runtimeManifest() {
|
|
@@ -317,6 +351,7 @@ function sweepRuntime(keep) {
|
|
|
317
351
|
const owned = (rel, name) =>
|
|
318
352
|
name.endsWith(".mjs") ||
|
|
319
353
|
/\.incoming-\d+$/.test(name) ||
|
|
354
|
+
/\.stale-\d+$/.test(name) ||
|
|
320
355
|
rel.split(path.sep)[0] === "extension";
|
|
321
356
|
const walk = (dir, rel) => {
|
|
322
357
|
let ents;
|
|
@@ -357,6 +392,27 @@ function syncRuntime() {
|
|
|
357
392
|
return sweepRuntime(new Set(manifest.map(([rel]) => rel)));
|
|
358
393
|
}
|
|
359
394
|
|
|
395
|
+
export function runtimeDrift(manifest = runtimeManifest(), runtimeDir = RUNTIME_DIR) {
|
|
396
|
+
const out = [];
|
|
397
|
+
for (const [rel, src] of manifest) {
|
|
398
|
+
let srcBuf;
|
|
399
|
+
try {
|
|
400
|
+
srcBuf = fs.readFileSync(src);
|
|
401
|
+
} catch {
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
let dstBuf;
|
|
405
|
+
try {
|
|
406
|
+
dstBuf = fs.readFileSync(path.join(runtimeDir, rel));
|
|
407
|
+
} catch {
|
|
408
|
+
out.push({ rel, why: "missing" });
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
if (!srcBuf.equals(dstBuf)) out.push({ rel, why: "differ" });
|
|
412
|
+
}
|
|
413
|
+
return out;
|
|
414
|
+
}
|
|
415
|
+
|
|
360
416
|
// Chrome 和 agent 桌面端都是 GUI 应用,PATH 里没有 nvm / homebrew 的 node——
|
|
361
417
|
// `#!/usr/bin/env node` 在它们手里必然 ENOENT。所以到处都必须写绝对路径。
|
|
362
418
|
// 用跑安装脚本的这个 node(process.execPath 是绝对路径),不依赖调用方的 PATH。
|
|
@@ -838,8 +894,18 @@ function doMcp() {
|
|
|
838
894
|
else if (!WIN && !(fs.statSync(MCP_LAUNCHER).mode & 0o111)) bad("MCP 启动器没有可执行位");
|
|
839
895
|
else ok(`启动器: ${MCP_LAUNCHER}`);
|
|
840
896
|
const missing = SERVER_DEPS.filter((d) => !fs.existsSync(path.join(RUNTIME_DIR, d)));
|
|
841
|
-
if (missing.length)
|
|
842
|
-
|
|
897
|
+
if (missing.length) {
|
|
898
|
+
bad(`运行时缺少 server.mjs 依赖的模块:${missing.join(", ")},跑 ${REINSTALL_CMD} 重装`);
|
|
899
|
+
} else {
|
|
900
|
+
ok(`server 依赖模块齐全(${SERVER_DEPS.join(", ")})`);
|
|
901
|
+
const drift = runtimeDrift(runtimeManifest().filter(([rel]) => !String(rel).startsWith("extension")));
|
|
902
|
+
if (drift.length)
|
|
903
|
+
bad(
|
|
904
|
+
`运行时与源码不一致:${drift
|
|
905
|
+
.map((d) => `${d.rel}(${d.why === "missing" ? "缺" : "内容不同"})`)
|
|
906
|
+
.join("、")},跑 ${REINSTALL_CMD} 同步`
|
|
907
|
+
);
|
|
908
|
+
}
|
|
843
909
|
smokeTestRuntime();
|
|
844
910
|
checkStrayBrowsers();
|
|
845
911
|
const extMissing = EXT_FILES.filter((f) => !fs.existsSync(path.join(RUNTIME_DIR, "extension", f)));
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
|
|
5
|
+
export const defaultMcpLauncher = () =>
|
|
6
|
+
path.join(
|
|
7
|
+
os.homedir(),
|
|
8
|
+
".agent-in-chrome",
|
|
9
|
+
"agent-in-chrome",
|
|
10
|
+
process.platform === "win32" ? "mcp-launcher.bat" : "mcp-launcher.sh",
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
export const launcherNeedsShell = (file) => process.platform === "win32" && /\.(cmd|bat)$/i.test(String(file));
|
|
14
|
+
|
|
15
|
+
export const cmdq = (s) => (/[\s&|<>^"()]/.test(String(s)) ? `"${String(s).replace(/"/g, `""`)}"` : String(s));
|
|
16
|
+
|
|
17
|
+
export function spawnMcpLauncher(file, opts = {}) {
|
|
18
|
+
if (launcherNeedsShell(file)) {
|
|
19
|
+
return spawn(cmdq(file), { ...opts, shell: true, windowsHide: true });
|
|
20
|
+
}
|
|
21
|
+
return spawn(file, [], opts);
|
|
22
|
+
}
|
package/scripts/pack-crx.mjs
CHANGED
|
@@ -12,6 +12,7 @@ import path from "node:path";
|
|
|
12
12
|
import crypto from "node:crypto";
|
|
13
13
|
import { execFileSync } from "node:child_process";
|
|
14
14
|
import { fileURLToPath } from "node:url";
|
|
15
|
+
import { zipDir } from "./release/zip.mjs";
|
|
15
16
|
|
|
16
17
|
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
17
18
|
const EXT_DIR = path.join(ROOT, "extension");
|
|
@@ -42,7 +43,7 @@ const version = JSON.parse(fs.readFileSync(path.join(EXT_DIR, "manifest.json"),
|
|
|
42
43
|
|
|
43
44
|
const zipPath = path.join(OUT_DIR, "extension.zip");
|
|
44
45
|
fs.rmSync(zipPath, { force: true });
|
|
45
|
-
|
|
46
|
+
zipDir(zipPath, { cwd: EXT_DIR });
|
|
46
47
|
const zip = fs.readFileSync(zipPath);
|
|
47
48
|
|
|
48
49
|
const pubDer = crypto.createPublicKey(fs.readFileSync(KEY)).export({ type: "spki", format: "der" });
|
package/scripts/test-agents.mjs
CHANGED
|
@@ -12,6 +12,17 @@ import * as install from "./install.mjs";
|
|
|
12
12
|
import { pickMulti } from "./pick.mjs";
|
|
13
13
|
|
|
14
14
|
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
15
|
+
|
|
16
|
+
const TEST_REG_ROOT = `HKCU\\Software\\aic-reg-test-${process.pid}`;
|
|
17
|
+
if (process.platform === "win32") {
|
|
18
|
+
if (process.env.AGENT_IN_CHROME_REG_ROOT) throw new Error("AGENT_IN_CHROME_REG_ROOT 已被外部设置,测试的隔离前提不成立");
|
|
19
|
+
process.env.AGENT_IN_CHROME_REG_ROOT = TEST_REG_ROOT;
|
|
20
|
+
process.on("exit", () => {
|
|
21
|
+
try {
|
|
22
|
+
execFileSync("reg", ["delete", TEST_REG_ROOT, "/f"], { stdio: "pipe" });
|
|
23
|
+
} catch {}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
15
26
|
const SERVER_PATH = path.join(ROOT, "mcp", "server.mjs");
|
|
16
27
|
|
|
17
28
|
{
|
|
@@ -570,7 +581,7 @@ console.log("\n\x1b[1m--check 的 socket 权限体检:按 defaultSockName 的
|
|
|
570
581
|
);
|
|
571
582
|
check("三条 sock 都枚举到了", found.length === 3, JSON.stringify(found));
|
|
572
583
|
check("同目录的非 sock 文件不进来", !found.some((n) => n === "token" || n.endsWith(".log")), JSON.stringify(found));
|
|
573
|
-
check("桌面那条的标签仍然是「桌面」", listSocks(dir).find(([, p]) =>
|
|
584
|
+
check("桌面那条的标签仍然是「桌面」", listSocks(dir).find(([, p]) => path.basename(p) === "agent-in-chrome.sock")?.[0] === "桌面");
|
|
574
585
|
check("目录不存在时返回空数组,不抛", JSON.stringify(listSocks(path.join(dir, "没有这个目录"))) === "[]");
|
|
575
586
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
576
587
|
}
|
|
@@ -752,9 +763,10 @@ process.stdin.on("data", () => {
|
|
|
752
763
|
check("旧 inode 的内容一个字没被动过(不是原地截断重写)", held.held === SENTINEL, JSON.stringify(held.held.slice(0, 80)));
|
|
753
764
|
check("新文件是另一个 inode", newIno !== oldIno, `${newIno} vs ${oldIno}`);
|
|
754
765
|
check("新文件的内容就是仓库里的源码", fs.readFileSync(serverRt, "utf8") === srcText);
|
|
766
|
+
const enoentOk = held.enoent === 0 || (process.platform === "win32" && held.enoent <= 4);
|
|
755
767
|
check(
|
|
756
768
|
`替换过程没有缺失窗口(${held.samples} 次 stat 全部命中)`,
|
|
757
|
-
|
|
769
|
+
enoentOk && held.samples > (process.platform === "win32" ? 5 : 20),
|
|
758
770
|
`ENOENT ${held.enoent} 次 / 共 ${held.samples} 次`
|
|
759
771
|
);
|
|
760
772
|
check(
|
|
@@ -762,7 +774,11 @@ process.stdin.on("data", () => {
|
|
|
762
774
|
held.inos.includes(oldIno) && held.inos.includes(newIno),
|
|
763
775
|
`采到 ${JSON.stringify(held.inos)},旧 ${oldIno} 新 ${newIno}`
|
|
764
776
|
);
|
|
765
|
-
check(
|
|
777
|
+
check(
|
|
778
|
+
"替换用的临时文件没留下",
|
|
779
|
+
fs.readdirSync(RT).filter((f) => f.includes(".incoming-") || f.includes(".stale-")).length === 0,
|
|
780
|
+
fs.readdirSync(RT).join(",")
|
|
781
|
+
);
|
|
766
782
|
|
|
767
783
|
const stale = [
|
|
768
784
|
["zombie.mjs", path.join(RT, "zombie.mjs")],
|
|
@@ -874,31 +890,43 @@ console.log("\n\x1b[1mWindows 的 GUI 条目形状:cmd /c\x1b[0m");
|
|
|
874
890
|
|
|
875
891
|
console.log("\n\x1b[1mclaude mcp add:子进程继承调用方的 HOME\x1b[0m");
|
|
876
892
|
{
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
893
|
+
if (process.platform === "win32") {
|
|
894
|
+
console.log(" · 裸二进制那条路 Windows 上走不到(shabang 脚本没法被 CreateProcess 执行),等价路径在下一块");
|
|
895
|
+
} else {
|
|
896
|
+
const binDir = fs.mkdtempSync(path.join(os.tmpdir(), "aic-fakeclaude-"));
|
|
897
|
+
const fake = path.join(binDir, "claude");
|
|
898
|
+
fs.writeFileSync(fake, `#!/bin/sh\nprintf '%s\\n' "$HOME" "$@" >> "$HOME/claude-cli.log"\n`);
|
|
899
|
+
fs.chmodSync(fake, 0o755);
|
|
900
|
+
const home = makeHome([]);
|
|
901
|
+
const realPath = process.env.PATH;
|
|
902
|
+
process.env.PATH = `${binDir}${path.delimiter}${realPath}`;
|
|
903
|
+
try {
|
|
904
|
+
await quiet(() => runAgents("install", { ...opts(home), agents: ["claude-code"] }));
|
|
905
|
+
} finally {
|
|
906
|
+
process.env.PATH = realPath;
|
|
907
|
+
}
|
|
908
|
+
const log = readOr(path.join(home, "claude-cli.log")) || "";
|
|
909
|
+
check("claude 子进程看到的 HOME 是我们给的那个假 home", log.split("\n")[0] === home, JSON.stringify(log.slice(0, 200)));
|
|
910
|
+
check("真的把 mcp add 发出去了", log.includes("mcp") && log.includes("add") && log.includes(SERVER_PATH), JSON.stringify(log.slice(0, 300)));
|
|
911
|
+
check("真实 HOME 下没有被写出日志(没走穿)", !fs.existsSync(path.join(REAL_HOME, "claude-cli.log")));
|
|
912
|
+
fs.rmSync(binDir, { recursive: true, force: true });
|
|
888
913
|
}
|
|
889
|
-
const log = readOr(path.join(home, "claude-cli.log")) || "";
|
|
890
|
-
check("claude 子进程看到的 HOME 是我们给的那个假 home", log.split("\n")[0] === home, JSON.stringify(log.slice(0, 200)));
|
|
891
|
-
check("真的把 mcp add 发出去了", log.includes("mcp") && log.includes("add") && log.includes(SERVER_PATH), JSON.stringify(log.slice(0, 300)));
|
|
892
|
-
check("真实 HOME 下没有被写出日志(没走穿)", !fs.existsSync(path.join(REAL_HOME, "claude-cli.log")));
|
|
893
|
-
fs.rmSync(binDir, { recursive: true, force: true });
|
|
894
914
|
}
|
|
895
915
|
|
|
896
916
|
{
|
|
917
|
+
const onRealWin = process.platform === "win32";
|
|
897
918
|
const binDir = fs.mkdtempSync(path.join(os.tmpdir(), "aic-fakecmd-"));
|
|
898
919
|
const spaced = path.join(binDir, "bin dir (x86)");
|
|
899
920
|
fs.mkdirSync(spaced, { recursive: true });
|
|
900
921
|
const fake = path.join(spaced, "claude.cmd");
|
|
901
|
-
|
|
922
|
+
if (onRealWin) {
|
|
923
|
+
fs.writeFileSync(
|
|
924
|
+
fake,
|
|
925
|
+
`@echo off\r\n>>"%HOME%\\claude-cli.log" echo %HOME%\r\n>>"%HOME%\\claude-cli.log" echo %*\r\n`
|
|
926
|
+
);
|
|
927
|
+
} else {
|
|
928
|
+
fs.writeFileSync(fake, `#!/bin/sh\nprintf '%s\\n' "$HOME" "$@" >> "$HOME/claude-cli.log"\n`);
|
|
929
|
+
}
|
|
902
930
|
fs.chmodSync(fake, 0o755);
|
|
903
931
|
const home = makeHome([]);
|
|
904
932
|
const srvDir = path.join(binDir, "srv dir");
|
|
@@ -916,10 +944,15 @@ console.log("\n\x1b[1mclaude mcp add:子进程继承调用方的 HOME\x1b[0m")
|
|
|
916
944
|
} finally {
|
|
917
945
|
process.env.PATH = realPath;
|
|
918
946
|
}
|
|
919
|
-
const lines = (readOr(path.join(home, "claude-cli.log")) || "").split("\n");
|
|
947
|
+
const lines = (readOr(path.join(home, "claude-cli.log")) || "").replace(/\r\n/g, "\n").split("\n").filter((l) => l !== "");
|
|
948
|
+
const withoutQuotes = lines.map((l) => l.replace(/"/g, ""));
|
|
920
949
|
check("win32 的 .cmd shim 这条路真的跑起来了(不是退回直接编辑)", /claude mcp add 完成/.test(said), said.slice(-300));
|
|
921
950
|
check(".cmd 子进程看到的也是我们给的 HOME", lines[0] === home, JSON.stringify(lines.slice(0, 3)));
|
|
922
|
-
check(
|
|
951
|
+
check(
|
|
952
|
+
"带空格和括号的路径原样到达(引号是自己加的)",
|
|
953
|
+
lines.includes(serverPath) || withoutQuotes.some((l) => l.includes(serverPath)),
|
|
954
|
+
JSON.stringify(lines)
|
|
955
|
+
);
|
|
923
956
|
check("stderr 上没有 DEP0190 弃用警告(整条命令拼成字符串走的)", !/DEP0190/.test(said), said.slice(-200));
|
|
924
957
|
fs.rmSync(binDir, { recursive: true, force: true });
|
|
925
958
|
}
|
|
@@ -232,8 +232,13 @@ process.stdin.on("data", (c) => {
|
|
|
232
232
|
});
|
|
233
233
|
`
|
|
234
234
|
);
|
|
235
|
-
const
|
|
236
|
-
|
|
235
|
+
const onRealWin = process.platform === "win32";
|
|
236
|
+
const launcher = path.join(tmp, onRealWin ? "launcher.bat" : "launcher.sh");
|
|
237
|
+
if (onRealWin) {
|
|
238
|
+
fs.writeFileSync(launcher, `@echo off\r\n"${process.execPath}" "${stub}" %*\r\n`);
|
|
239
|
+
} else {
|
|
240
|
+
fs.writeFileSync(launcher, `#!/bin/sh\nexec ${process.execPath} ${stub} "$@"\n`);
|
|
241
|
+
}
|
|
237
242
|
fs.chmodSync(launcher, 0o755);
|
|
238
243
|
const env = { ...process.env, AGENT_IN_CHROME_E2E_LAUNCHER: launcher, AGENT_IN_CHROME_SOCK: path.join(tmp, "s.sock") };
|
|
239
244
|
const dry = spawnSync(process.execPath, [path.join(HERE, "e2e-sweep.mjs"), "--dry-run"], { encoding: "utf8", env, timeout: 30000 });
|