agentroam 0.2.0-preview.61 → 0.2.0-preview.63
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/dist/cli.js +2 -2
- package/dist/instance-takeover.js +3 -27
- package/dist/network.js +32 -2
- package/dist/power/sleep-inhibitor.js +12 -2
- package/dist/startup-lock.js +81 -0
- package/package.json +3 -3
package/dist/cli.js
CHANGED
|
@@ -6,7 +6,7 @@ import { parseArgs } from "./args.js";
|
|
|
6
6
|
import { ensureCloudflared } from "./cloudflared/installer.js";
|
|
7
7
|
import { resolveCodexRuntime } from "./codex-runtime-manager.js";
|
|
8
8
|
import { probeSQLiteRuntime, probeTerminalRuntime, repairNativeRuntimePermissions } from "./native-runtime.js";
|
|
9
|
-
import {
|
|
9
|
+
import { findRoutedLanUrl } from "./network.js";
|
|
10
10
|
import { installedDataDir, pairingAdmin, printPairingCode, terminalText } from "./device-pairing.js";
|
|
11
11
|
import { detectPlatform } from "./platform.js";
|
|
12
12
|
import { AGENTROAM_VERSION, resolvePlatformRuntime, resolvePlatformTui } from "./platform-packages.js";
|
|
@@ -195,7 +195,7 @@ async function executeCommand(options, argv) {
|
|
|
195
195
|
}
|
|
196
196
|
void runtime.exited.then(() => controller.abort());
|
|
197
197
|
await serviceReporter?.localReady(runtime.localUrl);
|
|
198
|
-
const lanUrl =
|
|
198
|
+
const lanUrl = await findRoutedLanUrl(runtime.port) ?? runtime.localUrl;
|
|
199
199
|
const relayRuntime = runtime;
|
|
200
200
|
relay = await withCliProgress("Preparing phone access", () => selectRelay({
|
|
201
201
|
cli: options,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { readdir, readFile, mkdir,
|
|
1
|
+
import { readdir, readFile, mkdir, realpath, rename } from "node:fs/promises";
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
3
|
import { homedir } from "node:os";
|
|
4
|
-
import { dirname, join
|
|
4
|
+
import { dirname, join } from "node:path";
|
|
5
5
|
import { setTimeout as delay } from "node:timers/promises";
|
|
6
6
|
import { runCommand } from "./service/service-controller.js";
|
|
7
7
|
import { MacLaunchAgent } from "./service/macos-launch-agent.js";
|
|
@@ -140,28 +140,4 @@ export async function stopPreviousInstances(dataDir, log, dependencies = {}) {
|
|
|
140
140
|
log("✓ 旧 CLI 已停止,继续启动新实例");
|
|
141
141
|
}
|
|
142
142
|
}
|
|
143
|
-
|
|
144
|
-
export async function lockInstanceStartup(dataDir) {
|
|
145
|
-
await mkdir(dataDir, { recursive: true });
|
|
146
|
-
const path = resolve(await realpath(dataDir), ".cli-start.lock");
|
|
147
|
-
try {
|
|
148
|
-
await mkdir(path, { mode: 0o700 });
|
|
149
|
-
}
|
|
150
|
-
catch (error) {
|
|
151
|
-
if (error.code !== "EEXIST")
|
|
152
|
-
throw error;
|
|
153
|
-
throw new Error("此数据目录已有 CLI 正在启动;请等它完成后重试。若上次异常退出,请确认无启动进程后移除 .cli-start.lock。");
|
|
154
|
-
}
|
|
155
|
-
try {
|
|
156
|
-
await writeFile(join(path, "pid"), String(process.pid), { mode: 0o600 });
|
|
157
|
-
}
|
|
158
|
-
catch (error) {
|
|
159
|
-
await rm(path, { recursive: true, force: true });
|
|
160
|
-
throw error;
|
|
161
|
-
}
|
|
162
|
-
let released = false;
|
|
163
|
-
return async () => { if (!released) {
|
|
164
|
-
released = true;
|
|
165
|
-
await rm(path, { recursive: true, force: true });
|
|
166
|
-
} };
|
|
167
|
-
}
|
|
143
|
+
export { lockInstanceStartup } from "./startup-lock.js";
|
package/dist/network.js
CHANGED
|
@@ -1,12 +1,42 @@
|
|
|
1
1
|
import { networkInterfaces } from "node:os";
|
|
2
|
-
|
|
2
|
+
import { createSocket } from "node:dgram";
|
|
3
|
+
export function findLanUrl(port, interfaces = networkInterfaces(), routedAddress) {
|
|
3
4
|
const addresses = Object.values(interfaces)
|
|
4
5
|
.flatMap((entries) => entries ?? [])
|
|
5
6
|
.filter((entry) => entry.family === "IPv4" && !entry.internal)
|
|
6
7
|
.map((entry) => entry.address);
|
|
7
|
-
const address = addresses.
|
|
8
|
+
const address = routedAddress && addresses.includes(routedAddress) && isPrivateIpv4(routedAddress)
|
|
9
|
+
? routedAddress : addresses.find(isPreferredLanIpv4);
|
|
8
10
|
return address ? `http://${address}:${port}` : null;
|
|
9
11
|
}
|
|
12
|
+
/** UDP connect selects the OS route without sending a packet to the destination. */
|
|
13
|
+
export async function findRoutedLanUrl(port) {
|
|
14
|
+
const routedAddress = await new Promise((done) => {
|
|
15
|
+
const socket = createSocket("udp4");
|
|
16
|
+
let settled = false;
|
|
17
|
+
const finish = (address) => {
|
|
18
|
+
if (settled)
|
|
19
|
+
return;
|
|
20
|
+
settled = true;
|
|
21
|
+
clearTimeout(timer);
|
|
22
|
+
try {
|
|
23
|
+
socket.close();
|
|
24
|
+
}
|
|
25
|
+
catch { /* A failed bind may leave no socket to close. */ }
|
|
26
|
+
done(address);
|
|
27
|
+
};
|
|
28
|
+
const timer = setTimeout(() => finish(null), 1_000);
|
|
29
|
+
socket.once("error", () => finish(null));
|
|
30
|
+
socket.connect(53, "1.1.1.1", () => { if (!settled)
|
|
31
|
+
finish(socket.address().address); });
|
|
32
|
+
});
|
|
33
|
+
return findLanUrl(port, networkInterfaces(), routedAddress);
|
|
34
|
+
}
|
|
35
|
+
function isPrivateIpv4(address) {
|
|
36
|
+
const parts = address.split(".").map(Number);
|
|
37
|
+
return isPreferredLanIpv4(address) || (parts.length === 4 && parts.every(part => Number.isInteger(part) && part >= 0 && part <= 255)
|
|
38
|
+
&& parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31);
|
|
39
|
+
}
|
|
10
40
|
function isPreferredLanIpv4(address) {
|
|
11
41
|
const parts = address.split(".").map(Number);
|
|
12
42
|
if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255))
|
|
@@ -3,7 +3,9 @@ export async function acquireSleepInhibitor(options = {}) {
|
|
|
3
3
|
const platform = options.platform ?? process.platform;
|
|
4
4
|
const pid = options.pid ?? process.pid;
|
|
5
5
|
const spawnProcess = options.spawnProcess ?? spawn;
|
|
6
|
-
|
|
6
|
+
// Windows PowerShell compiles the P/Invoke helper on first use. Cold startup
|
|
7
|
+
// and antivirus scanning can take longer than five seconds.
|
|
8
|
+
const readyTimeoutMs = options.readyTimeoutMs ?? (platform === "win32" ? 30_000 : 5_000);
|
|
7
9
|
const stopTimeoutMs = options.stopTimeoutMs ?? 2_000;
|
|
8
10
|
if (platform === "darwin") {
|
|
9
11
|
const child = spawnProcess("/usr/bin/caffeinate", ["-i", "-w", String(pid)], {
|
|
@@ -24,7 +26,15 @@ export async function acquireSleepInhibitor(options = {}) {
|
|
|
24
26
|
stdio: ["ignore", "pipe", "pipe"],
|
|
25
27
|
windowsHide: true,
|
|
26
28
|
});
|
|
27
|
-
|
|
29
|
+
try {
|
|
30
|
+
await waitForReadyLine(child, readyTimeoutMs);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
// A timed-out child otherwise keeps Node alive after startup has failed.
|
|
34
|
+
if (child.exitCode === null && child.signalCode === null)
|
|
35
|
+
child.kill("SIGKILL");
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
28
38
|
return monitorChild(child, stopTimeoutMs, "Windows idle-sleep inhibitor");
|
|
29
39
|
}
|
|
30
40
|
throw new Error(`idle-sleep prevention is unsupported on ${platform}`);
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { mkdir, readFile, realpath, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { createServer } from "node:net";
|
|
4
|
+
import { join, resolve } from "node:path";
|
|
5
|
+
function alive(pid) {
|
|
6
|
+
try {
|
|
7
|
+
process.kill(pid, 0);
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
return error.code !== "ESRCH";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/** Serialize startup within a data directory; never remove a live owner's lock. */
|
|
15
|
+
export async function lockInstanceStartup(dataDir) {
|
|
16
|
+
await mkdir(dataDir, { recursive: true });
|
|
17
|
+
const path = resolve(await realpath(dataDir), ".cli-start.lock");
|
|
18
|
+
// Task Scheduler can terminate the CLI without running finally. A named pipe
|
|
19
|
+
// is released by Windows even on forced termination; retain the PID marker
|
|
20
|
+
// so older launchers still see an in-progress startup.
|
|
21
|
+
let closePipe;
|
|
22
|
+
const busy = () => new Error("此数据目录已有 CLI 正在启动;请等它完成后重试。");
|
|
23
|
+
if (process.platform === "win32") {
|
|
24
|
+
const pipe = createServer();
|
|
25
|
+
const name = createHash("sha256").update(path.toLowerCase()).digest("hex");
|
|
26
|
+
await new Promise((done, reject) => {
|
|
27
|
+
const onError = (error) => reject(error.code === "EADDRINUSE" ? busy() : error);
|
|
28
|
+
pipe.once("error", onError);
|
|
29
|
+
pipe.listen(`\\\\.\\pipe\\agentroam-startup-${name}`, () => { pipe.removeListener("error", onError); done(); });
|
|
30
|
+
});
|
|
31
|
+
closePipe = () => new Promise((done, reject) => pipe.close(error => error ? reject(error) : done()));
|
|
32
|
+
try {
|
|
33
|
+
const owner = (await readFile(join(path, "pid"), "utf8").catch(error => {
|
|
34
|
+
if (error.code === "ENOENT")
|
|
35
|
+
return null;
|
|
36
|
+
throw error;
|
|
37
|
+
}))?.trim();
|
|
38
|
+
if (owner !== undefined && owner !== null) {
|
|
39
|
+
const pid = Number(owner);
|
|
40
|
+
// Be conservative with corrupt markers and potentially reused live PIDs.
|
|
41
|
+
if (!Number.isSafeInteger(pid) || pid <= 1 || alive(pid))
|
|
42
|
+
throw busy();
|
|
43
|
+
await rm(path, { recursive: true, force: true });
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
await closePipe();
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
try {
|
|
53
|
+
await mkdir(path, { mode: 0o700 });
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
if (error.code !== "EEXIST")
|
|
57
|
+
throw error;
|
|
58
|
+
throw new Error("此数据目录已有 CLI 正在启动;请等它完成后重试。若上次异常退出,请确认无启动进程后移除 .cli-start.lock。");
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
await writeFile(join(path, "pid"), String(process.pid), { mode: 0o600 });
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
await rm(path, { recursive: true, force: true });
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
let release;
|
|
68
|
+
return () => release ??= (async () => {
|
|
69
|
+
try {
|
|
70
|
+
await rm(path, { recursive: true, force: true });
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
await closePipe?.();
|
|
74
|
+
}
|
|
75
|
+
})();
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
await closePipe?.();
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "agentroam",
|
|
3
|
-
"version": "0.2.0-preview.
|
|
3
|
+
"version": "0.2.0-preview.63",
|
|
4
4
|
"description": "Zero-install mobile remote terminal and file console",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"ws": "^8.21.3"
|
|
28
28
|
},
|
|
29
29
|
"optionalDependencies": {
|
|
30
|
-
"agentroam-runtime-darwin-arm64": "0.2.0-preview.
|
|
31
|
-
"agentroam-runtime-win32-x64": "0.2.0-preview.
|
|
30
|
+
"agentroam-runtime-darwin-arm64": "0.2.0-preview.63",
|
|
31
|
+
"agentroam-runtime-win32-x64": "0.2.0-preview.63",
|
|
32
32
|
"agentroam-cloudflared-darwin-arm64": "0.2.0-preview.58",
|
|
33
33
|
"agentroam-cloudflared-win32-x64": "0.2.0-preview.58",
|
|
34
34
|
"agentroam-tui-darwin-arm64": "0.2.0-preview.59",
|