aicomputer 0.1.14 → 0.1.16
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 +9 -2
- package/dist/chunk-5IEWKH52.js +883 -0
- package/dist/chunk-KXLTHWW3.js +184 -0
- package/dist/chunk-OWK5N76S.js +70 -0
- package/dist/index.js +735 -644
- package/dist/lib/mount-config.d.ts +72 -0
- package/dist/lib/mount-config.js +40 -0
- package/dist/lib/mount-host.d.ts +14 -0
- package/dist/lib/mount-host.js +10 -0
- package/dist/lib/mount-reconcile.d.ts +44 -0
- package/dist/lib/mount-reconcile.js +13 -0
- package/package.json +4 -3
|
@@ -0,0 +1,883 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MOUNT_SERVICE_LABEL,
|
|
3
|
+
ensureHandleDirectories,
|
|
4
|
+
ensureMountDirectories,
|
|
5
|
+
readMountStatusSnapshot,
|
|
6
|
+
removeMountHandleState,
|
|
7
|
+
writeMountHandleMeta,
|
|
8
|
+
writeMountStatusSnapshot
|
|
9
|
+
} from "./chunk-KXLTHWW3.js";
|
|
10
|
+
|
|
11
|
+
// src/lib/mount-reconcile.ts
|
|
12
|
+
import { readdir, mkdir, rm } from "fs/promises";
|
|
13
|
+
import { join as join3 } from "path";
|
|
14
|
+
|
|
15
|
+
// src/lib/config.ts
|
|
16
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "fs";
|
|
17
|
+
import { homedir } from "os";
|
|
18
|
+
import { join } from "path";
|
|
19
|
+
var CONFIG_DIR = join(homedir(), ".computer");
|
|
20
|
+
var CONFIG_FILE = join(CONFIG_DIR, "config.json");
|
|
21
|
+
function ensureConfigDir() {
|
|
22
|
+
if (!existsSync(CONFIG_DIR)) {
|
|
23
|
+
mkdirSync(CONFIG_DIR, { recursive: true, mode: 448 });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function readConfig() {
|
|
27
|
+
ensureConfigDir();
|
|
28
|
+
if (!existsSync(CONFIG_FILE)) {
|
|
29
|
+
return {};
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
return JSON.parse(readFileSync(CONFIG_FILE, "utf8"));
|
|
33
|
+
} catch {
|
|
34
|
+
return {};
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function writeConfig(config) {
|
|
38
|
+
ensureConfigDir();
|
|
39
|
+
const tempFile = `${CONFIG_FILE}.${process.pid}.tmp`;
|
|
40
|
+
writeFileSync(tempFile, JSON.stringify(config, null, 2), { mode: 384 });
|
|
41
|
+
renameSync(tempFile, CONFIG_FILE);
|
|
42
|
+
}
|
|
43
|
+
function getAPIKey() {
|
|
44
|
+
const envValue = process.env.COMPUTER_API_KEY ?? process.env.AGENTCOMPUTER_API_KEY;
|
|
45
|
+
if (envValue) {
|
|
46
|
+
return envValue.trim();
|
|
47
|
+
}
|
|
48
|
+
return getStoredAPIKey();
|
|
49
|
+
}
|
|
50
|
+
function getStoredAPIKey() {
|
|
51
|
+
return readConfig().auth?.apiKey?.trim() || null;
|
|
52
|
+
}
|
|
53
|
+
function hasEnvAPIKey() {
|
|
54
|
+
return Boolean(process.env.COMPUTER_API_KEY ?? process.env.AGENTCOMPUTER_API_KEY);
|
|
55
|
+
}
|
|
56
|
+
function setAPIKey(apiKey) {
|
|
57
|
+
const config = readConfig();
|
|
58
|
+
config.auth = { apiKey: apiKey.trim() };
|
|
59
|
+
writeConfig(config);
|
|
60
|
+
}
|
|
61
|
+
function clearAPIKey() {
|
|
62
|
+
const config = readConfig();
|
|
63
|
+
delete config.auth;
|
|
64
|
+
writeConfig(config);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/lib/api.ts
|
|
68
|
+
var BASE_URL = process.env.COMPUTER_API_URL ?? process.env.AGENTCOMPUTER_API_URL ?? "https://api.computer.agentcomputer.ai";
|
|
69
|
+
var WEB_URL = process.env.COMPUTER_WEB_URL ?? process.env.AGENTCOMPUTER_WEB_URL ?? resolveDefaultWebURL(BASE_URL);
|
|
70
|
+
var ApiError = class extends Error {
|
|
71
|
+
constructor(status, message) {
|
|
72
|
+
super(message);
|
|
73
|
+
this.status = status;
|
|
74
|
+
this.name = "ApiError";
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
function getBaseURL() {
|
|
78
|
+
return BASE_URL;
|
|
79
|
+
}
|
|
80
|
+
function getWebURL() {
|
|
81
|
+
return WEB_URL;
|
|
82
|
+
}
|
|
83
|
+
async function api(path, options = {}) {
|
|
84
|
+
const apiKey = getAPIKey();
|
|
85
|
+
if (!apiKey) {
|
|
86
|
+
throw new ApiError(401, "not logged in; run 'computer login' first");
|
|
87
|
+
}
|
|
88
|
+
return requestWithKey(apiKey, path, options);
|
|
89
|
+
}
|
|
90
|
+
function resolveDefaultWebURL(apiURL) {
|
|
91
|
+
try {
|
|
92
|
+
const parsed = new URL(apiURL);
|
|
93
|
+
if (parsed.hostname === "api.computer.agentcomputer.ai") {
|
|
94
|
+
return "https://agentcomputer.ai";
|
|
95
|
+
}
|
|
96
|
+
if (parsed.hostname === "localhost" || parsed.hostname === "127.0.0.1") {
|
|
97
|
+
return `${parsed.protocol}//${parsed.hostname}:3000`;
|
|
98
|
+
}
|
|
99
|
+
} catch {
|
|
100
|
+
return "https://agentcomputer.ai";
|
|
101
|
+
}
|
|
102
|
+
return "https://agentcomputer.ai";
|
|
103
|
+
}
|
|
104
|
+
async function apiWithKey(apiKey, path, options = {}) {
|
|
105
|
+
return requestWithKey(apiKey, path, options);
|
|
106
|
+
}
|
|
107
|
+
async function requestWithKey(apiKey, path, options) {
|
|
108
|
+
const headers = {
|
|
109
|
+
Accept: "application/json",
|
|
110
|
+
...options.headers ?? {}
|
|
111
|
+
};
|
|
112
|
+
if (options.body !== void 0) {
|
|
113
|
+
headers["Content-Type"] = "application/json";
|
|
114
|
+
}
|
|
115
|
+
if (apiKey) {
|
|
116
|
+
headers.Authorization = `Bearer ${apiKey}`;
|
|
117
|
+
}
|
|
118
|
+
const response = await fetch(`${BASE_URL}${path}`, {
|
|
119
|
+
...options,
|
|
120
|
+
headers
|
|
121
|
+
});
|
|
122
|
+
if (!response.ok) {
|
|
123
|
+
throw new ApiError(response.status, await readErrorMessage(response));
|
|
124
|
+
}
|
|
125
|
+
if (response.status === 204) {
|
|
126
|
+
return void 0;
|
|
127
|
+
}
|
|
128
|
+
return await response.json();
|
|
129
|
+
}
|
|
130
|
+
async function readErrorMessage(response) {
|
|
131
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
132
|
+
if (contentType.includes("application/json")) {
|
|
133
|
+
try {
|
|
134
|
+
const payload = await response.json();
|
|
135
|
+
if (payload.error) {
|
|
136
|
+
return payload.error;
|
|
137
|
+
}
|
|
138
|
+
return JSON.stringify(payload);
|
|
139
|
+
} catch {
|
|
140
|
+
return response.statusText || "request failed";
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const body = await response.text();
|
|
144
|
+
return body || response.statusText || "request failed";
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// src/lib/computers.ts
|
|
148
|
+
async function listComputers() {
|
|
149
|
+
const response = await api("/v1/computers");
|
|
150
|
+
return response.computers;
|
|
151
|
+
}
|
|
152
|
+
async function getComputerByID(id) {
|
|
153
|
+
return api(`/v1/computers/${id}`);
|
|
154
|
+
}
|
|
155
|
+
async function createComputer(input) {
|
|
156
|
+
return api("/v1/computers", {
|
|
157
|
+
method: "POST",
|
|
158
|
+
body: JSON.stringify(input)
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
async function deleteComputer(computerID) {
|
|
162
|
+
return api(`/v1/computers/${computerID}`, {
|
|
163
|
+
method: "DELETE"
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
async function getFilesystemSettings() {
|
|
167
|
+
return api("/v1/me/filesystem");
|
|
168
|
+
}
|
|
169
|
+
async function getConnectionInfo(computerID) {
|
|
170
|
+
return api(`/v1/computers/${computerID}/connection`);
|
|
171
|
+
}
|
|
172
|
+
async function createBrowserAccess(computerID) {
|
|
173
|
+
return api(`/v1/computers/${computerID}/access/browser`, {
|
|
174
|
+
method: "POST"
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
async function listPublishedPorts(computerID) {
|
|
178
|
+
const response = await api(`/v1/computers/${computerID}/ports`);
|
|
179
|
+
return response.ports;
|
|
180
|
+
}
|
|
181
|
+
async function publishPort(computerID, input) {
|
|
182
|
+
return api(`/v1/computers/${computerID}/ports`, {
|
|
183
|
+
method: "POST",
|
|
184
|
+
body: JSON.stringify(input)
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
async function deletePublishedPort(computerID, targetPort) {
|
|
188
|
+
return api(`/v1/computers/${computerID}/ports/${targetPort}`, {
|
|
189
|
+
method: "DELETE"
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
async function resolveComputer(identifier) {
|
|
193
|
+
try {
|
|
194
|
+
return await getComputerByID(identifier);
|
|
195
|
+
} catch (error) {
|
|
196
|
+
if (!(error instanceof Error) || !("status" in error)) {
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
const status = Reflect.get(error, "status");
|
|
200
|
+
if (status !== 404) {
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const computers = await listComputers();
|
|
205
|
+
const exact = computers.find(
|
|
206
|
+
(computer) => computer.handle === identifier || computer.id === identifier
|
|
207
|
+
);
|
|
208
|
+
if (exact) {
|
|
209
|
+
return exact;
|
|
210
|
+
}
|
|
211
|
+
throw new Error(`computer '${identifier}' not found`);
|
|
212
|
+
}
|
|
213
|
+
function webURL(computer) {
|
|
214
|
+
return `https://${computer.primary_web_host}${normalizePrimaryPath(computer.primary_path)}`;
|
|
215
|
+
}
|
|
216
|
+
function vncURL(computer) {
|
|
217
|
+
if (!computer.vnc_enabled) {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
const domain = computer.primary_web_host.replace(/^[^.]+\./, "");
|
|
221
|
+
return `https://6080--${computer.handle}.${domain}`;
|
|
222
|
+
}
|
|
223
|
+
function normalizePrimaryPath(primaryPath) {
|
|
224
|
+
const trimmed = primaryPath?.trim();
|
|
225
|
+
if (!trimmed) {
|
|
226
|
+
return "/";
|
|
227
|
+
}
|
|
228
|
+
return trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// src/lib/computer-picker.ts
|
|
232
|
+
import { select } from "@inquirer/prompts";
|
|
233
|
+
import chalk2 from "chalk";
|
|
234
|
+
|
|
235
|
+
// src/lib/format.ts
|
|
236
|
+
import chalk from "chalk";
|
|
237
|
+
function padEnd(str, len) {
|
|
238
|
+
const visible = str.replace(/\u001b\[[0-9;]*m/g, "");
|
|
239
|
+
return str + " ".repeat(Math.max(0, len - visible.length));
|
|
240
|
+
}
|
|
241
|
+
function timeAgo(dateStr) {
|
|
242
|
+
const seconds = Math.floor((Date.now() - new Date(dateStr).getTime()) / 1e3);
|
|
243
|
+
if (seconds < 60) return `${seconds}s ago`;
|
|
244
|
+
const minutes = Math.floor(seconds / 60);
|
|
245
|
+
if (minutes < 60) return `${minutes}m ago`;
|
|
246
|
+
const hours = Math.floor(minutes / 60);
|
|
247
|
+
if (hours < 24) return `${hours}h ago`;
|
|
248
|
+
const days = Math.floor(hours / 24);
|
|
249
|
+
return `${days}d ago`;
|
|
250
|
+
}
|
|
251
|
+
function formatStatus(status) {
|
|
252
|
+
switch (status) {
|
|
253
|
+
case "running":
|
|
254
|
+
return chalk.green(status);
|
|
255
|
+
case "pending":
|
|
256
|
+
case "provisioning":
|
|
257
|
+
case "starting":
|
|
258
|
+
return chalk.yellow(status);
|
|
259
|
+
case "stopping":
|
|
260
|
+
case "stopped":
|
|
261
|
+
case "deleted":
|
|
262
|
+
return chalk.gray(status);
|
|
263
|
+
case "error":
|
|
264
|
+
return chalk.red(status);
|
|
265
|
+
default:
|
|
266
|
+
return status;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// src/lib/computer-picker.ts
|
|
271
|
+
async function promptForSSHComputer(computers, message) {
|
|
272
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
273
|
+
throw new Error("computer id or handle is required when not running interactively");
|
|
274
|
+
}
|
|
275
|
+
const available = computers.filter(isSSHSelectable);
|
|
276
|
+
if (available.length === 0) {
|
|
277
|
+
if (computers.length === 0) {
|
|
278
|
+
throw new Error("no computers found");
|
|
279
|
+
}
|
|
280
|
+
throw new Error("no running computers with SSH enabled");
|
|
281
|
+
}
|
|
282
|
+
const handleWidth = Math.max(6, ...available.map((computer) => computer.handle.length));
|
|
283
|
+
const selectedID = await select({
|
|
284
|
+
message,
|
|
285
|
+
pageSize: Math.min(available.length, 10),
|
|
286
|
+
choices: available.map((computer) => ({
|
|
287
|
+
name: `${padEnd(chalk2.white(computer.handle), handleWidth + 2)}${padEnd(formatStatus(computer.status), 12)}${chalk2.dim(describeSSHChoice(computer))}`,
|
|
288
|
+
value: computer.id
|
|
289
|
+
}))
|
|
290
|
+
});
|
|
291
|
+
return available.find((computer) => computer.id === selectedID) ?? available[0];
|
|
292
|
+
}
|
|
293
|
+
function isSSHSelectable(computer) {
|
|
294
|
+
return computer.ssh_enabled && computer.status === "running";
|
|
295
|
+
}
|
|
296
|
+
function describeSSHChoice(computer) {
|
|
297
|
+
const displayName = computer.display_name.trim();
|
|
298
|
+
if (displayName && displayName !== computer.handle) {
|
|
299
|
+
return `${displayName} ${timeAgo(computer.updated_at)}`;
|
|
300
|
+
}
|
|
301
|
+
return `${computer.runtime_family} ${timeAgo(computer.updated_at)}`;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// src/lib/mount-mutagen.ts
|
|
305
|
+
import { chmodSync, readFileSync as readFileSync2, symlinkSync, unlinkSync, writeFileSync as writeFileSync2 } from "fs";
|
|
306
|
+
import { spawnSync } from "child_process";
|
|
307
|
+
import { basename, join as join2, relative, resolve } from "path";
|
|
308
|
+
var SYNC_NAME_PREFIX = "agentcomputer-mount-";
|
|
309
|
+
var DEFAULT_IGNORE_PATHS = [
|
|
310
|
+
".codex/tmp",
|
|
311
|
+
".local",
|
|
312
|
+
".ssh/sshd.log"
|
|
313
|
+
];
|
|
314
|
+
function ensureMutagenSshEnvironment(config, paths) {
|
|
315
|
+
ensureMountDirectories(paths);
|
|
316
|
+
const sshPath = resolveCommandPath("ssh");
|
|
317
|
+
const scpPath = resolveCommandPath("scp");
|
|
318
|
+
writeExecutableLink(paths.sshToolsDir, "ssh", sshPath);
|
|
319
|
+
writeExecutableLink(paths.sshToolsDir, "scp", scpPath);
|
|
320
|
+
writeExecutableLink(paths.sshToolsDir, basename(sshPath), sshPath);
|
|
321
|
+
writeExecutableLink(paths.sshToolsDir, basename(scpPath), scpPath);
|
|
322
|
+
}
|
|
323
|
+
function getHandleSessionName(handle) {
|
|
324
|
+
return `${SYNC_NAME_PREFIX}${handle}`;
|
|
325
|
+
}
|
|
326
|
+
function createHandleSession(handle, config, paths) {
|
|
327
|
+
ensureHandleDirectories(handle, config.rootPath);
|
|
328
|
+
const sessionName = getHandleSessionName(handle);
|
|
329
|
+
const args = [
|
|
330
|
+
"sync",
|
|
331
|
+
"create",
|
|
332
|
+
join2(paths.rootPath, handle),
|
|
333
|
+
`${handle}@${config.alias}:/home/node`,
|
|
334
|
+
"--name",
|
|
335
|
+
sessionName,
|
|
336
|
+
"--label",
|
|
337
|
+
`${MOUNT_SERVICE_LABEL}=true`,
|
|
338
|
+
"--label",
|
|
339
|
+
`${MOUNT_SERVICE_LABEL}.handle=${handle}`,
|
|
340
|
+
"--symlink-mode",
|
|
341
|
+
"posix-raw"
|
|
342
|
+
];
|
|
343
|
+
for (const ignorePath of DEFAULT_IGNORE_PATHS) {
|
|
344
|
+
args.push("--ignore", ignorePath);
|
|
345
|
+
}
|
|
346
|
+
runMutagen(args, config, paths, handle);
|
|
347
|
+
runMutagen(["sync", "flush", sessionName, "--skip-wait"], config, paths, handle);
|
|
348
|
+
}
|
|
349
|
+
function terminateSession(session, config, paths) {
|
|
350
|
+
runMutagen(
|
|
351
|
+
["sync", "terminate", session.identifier],
|
|
352
|
+
config,
|
|
353
|
+
paths,
|
|
354
|
+
session.handle,
|
|
355
|
+
true
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
function listOwnedSessions(config, paths) {
|
|
359
|
+
const result = runMutagen(["sync", "list", "-l"], config, paths, "mount", true);
|
|
360
|
+
const raw = [result.stdout, result.stderr].filter(Boolean).join("\n").trim();
|
|
361
|
+
if (!raw) {
|
|
362
|
+
return [];
|
|
363
|
+
}
|
|
364
|
+
if (!result.ok && raw.includes("No synchronization sessions found")) {
|
|
365
|
+
return [];
|
|
366
|
+
}
|
|
367
|
+
const rootPath = resolve(paths.rootPath);
|
|
368
|
+
const sessions = parseMutagenSyncList(raw).filter((session) => session.alphaPath).map((session) => {
|
|
369
|
+
const alphaPath = resolve(session.alphaPath);
|
|
370
|
+
const handle = basename(alphaPath);
|
|
371
|
+
const expectedName = getHandleSessionName(handle);
|
|
372
|
+
const expectedBeta = `${handle}@${config.alias}:/home/node`;
|
|
373
|
+
const owned = alphaPath === rootPath || relative(rootPath, alphaPath) === "" || !relative(rootPath, alphaPath).startsWith("..") && !relative(rootPath, alphaPath).startsWith("../");
|
|
374
|
+
if (!owned || !session.identifier) {
|
|
375
|
+
return null;
|
|
376
|
+
}
|
|
377
|
+
return {
|
|
378
|
+
identifier: session.identifier,
|
|
379
|
+
name: session.name,
|
|
380
|
+
handle,
|
|
381
|
+
alphaPath,
|
|
382
|
+
betaUrl: session.betaUrl,
|
|
383
|
+
alphaConnected: session.alphaConnected,
|
|
384
|
+
betaConnected: session.betaConnected,
|
|
385
|
+
status: session.status,
|
|
386
|
+
lastError: session.lastError,
|
|
387
|
+
scanProblemCount: session.scanProblemCount,
|
|
388
|
+
conflictCount: session.conflictCount,
|
|
389
|
+
legacy: session.name !== expectedName || session.betaUrl !== expectedBeta || alphaPath !== resolve(join2(rootPath, handle))
|
|
390
|
+
};
|
|
391
|
+
}).filter((session) => session !== null);
|
|
392
|
+
return sessions.sort((left, right) => left.handle.localeCompare(right.handle));
|
|
393
|
+
}
|
|
394
|
+
function selectPreferredSession(handle, sessions) {
|
|
395
|
+
const expectedName = getHandleSessionName(handle);
|
|
396
|
+
const exact = sessions.find((session) => session.name === expectedName);
|
|
397
|
+
return exact ?? sessions[0];
|
|
398
|
+
}
|
|
399
|
+
function parseMutagenSyncList(output) {
|
|
400
|
+
const sessions = [];
|
|
401
|
+
let current = null;
|
|
402
|
+
let section = "";
|
|
403
|
+
const finishCurrent = () => {
|
|
404
|
+
if (current?.identifier) {
|
|
405
|
+
sessions.push(current);
|
|
406
|
+
}
|
|
407
|
+
current = null;
|
|
408
|
+
section = "";
|
|
409
|
+
};
|
|
410
|
+
for (const line of output.split(/\r?\n/)) {
|
|
411
|
+
if (/^-{20,}$/.test(line.trim())) {
|
|
412
|
+
finishCurrent();
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
const trimmed = line.trim();
|
|
416
|
+
if (!trimmed) {
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
419
|
+
if (!current) {
|
|
420
|
+
current = {
|
|
421
|
+
alphaConnected: false,
|
|
422
|
+
betaConnected: false,
|
|
423
|
+
scanProblemCount: 0,
|
|
424
|
+
conflictCount: 0
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
if (trimmed.endsWith(":")) {
|
|
428
|
+
switch (trimmed.slice(0, -1)) {
|
|
429
|
+
case "Alpha":
|
|
430
|
+
case "Beta":
|
|
431
|
+
case "Scan problems":
|
|
432
|
+
case "Conflicts":
|
|
433
|
+
section = trimmed.slice(0, -1);
|
|
434
|
+
continue;
|
|
435
|
+
default:
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
if (trimmed.startsWith("Name: ")) {
|
|
440
|
+
current.name = trimmed.slice("Name: ".length);
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
if (trimmed.startsWith("Identifier: ")) {
|
|
444
|
+
current.identifier = trimmed.slice("Identifier: ".length);
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
if (trimmed.startsWith("Status: ")) {
|
|
448
|
+
current.status = trimmed.slice("Status: ".length);
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
if (trimmed.startsWith("Last error: ")) {
|
|
452
|
+
current.lastError = trimmed.slice("Last error: ".length);
|
|
453
|
+
continue;
|
|
454
|
+
}
|
|
455
|
+
if (section === "Alpha") {
|
|
456
|
+
if (trimmed.startsWith("URL: ")) {
|
|
457
|
+
current.alphaPath = trimmed.slice("URL: ".length);
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
if (trimmed.startsWith("Connected: ")) {
|
|
461
|
+
current.alphaConnected = parseConnected(trimmed);
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
if (section === "Beta") {
|
|
466
|
+
if (trimmed.startsWith("URL: ")) {
|
|
467
|
+
current.betaUrl = trimmed.slice("URL: ".length);
|
|
468
|
+
continue;
|
|
469
|
+
}
|
|
470
|
+
if (trimmed.startsWith("Connected: ")) {
|
|
471
|
+
current.betaConnected = parseConnected(trimmed);
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
if (section === "Scan problems") {
|
|
476
|
+
current.scanProblemCount += 1;
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
if (section === "Conflicts") {
|
|
480
|
+
if (trimmed.startsWith("(alpha)")) {
|
|
481
|
+
current.conflictCount += 1;
|
|
482
|
+
}
|
|
483
|
+
continue;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
finishCurrent();
|
|
487
|
+
return sessions;
|
|
488
|
+
}
|
|
489
|
+
function parseConnected(line) {
|
|
490
|
+
return line.slice("Connected: ".length).trim().toLowerCase() === "yes";
|
|
491
|
+
}
|
|
492
|
+
function runMutagen(args, config, paths, handle, ignoreFailure = false) {
|
|
493
|
+
const result = spawnSync("mutagen", args, {
|
|
494
|
+
encoding: "utf8",
|
|
495
|
+
env: {
|
|
496
|
+
...process.env,
|
|
497
|
+
MUTAGEN_SSH_PATH: paths.sshToolsDir,
|
|
498
|
+
MUTAGEN_SSH_CONNECT_TIMEOUT: String(config.connectTimeoutSeconds)
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
const stdout = result.stdout?.trim() ?? "";
|
|
502
|
+
const stderr = result.stderr?.trim() ?? "";
|
|
503
|
+
if (result.status !== 0 && !ignoreFailure) {
|
|
504
|
+
throw new Error(stderr || stdout || `mutagen failed for ${handle}`);
|
|
505
|
+
}
|
|
506
|
+
return {
|
|
507
|
+
ok: result.status === 0,
|
|
508
|
+
stdout,
|
|
509
|
+
stderr,
|
|
510
|
+
status: result.status
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
function resolveCommandPath(command) {
|
|
514
|
+
const result = spawnSync("which", [command], { encoding: "utf8" });
|
|
515
|
+
if (result.status !== 0) {
|
|
516
|
+
throw new Error(`failed to resolve ${command}`);
|
|
517
|
+
}
|
|
518
|
+
return result.stdout.trim();
|
|
519
|
+
}
|
|
520
|
+
function writeExecutableLink(directory, name, target) {
|
|
521
|
+
const linkPath = join2(directory, name);
|
|
522
|
+
try {
|
|
523
|
+
unlinkSync(linkPath);
|
|
524
|
+
} catch {
|
|
525
|
+
}
|
|
526
|
+
try {
|
|
527
|
+
symlinkSync(target, linkPath);
|
|
528
|
+
} catch {
|
|
529
|
+
const script = `#!/bin/sh
|
|
530
|
+
exec "${escapeShell(target)}" "$@"
|
|
531
|
+
`;
|
|
532
|
+
writeFileSync2(linkPath, script, { mode: 493 });
|
|
533
|
+
chmodSync(linkPath, 493);
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
try {
|
|
537
|
+
const stat = readFileSync2(linkPath);
|
|
538
|
+
if (!stat) {
|
|
539
|
+
throw new Error("empty");
|
|
540
|
+
}
|
|
541
|
+
} catch {
|
|
542
|
+
const script = `#!/bin/sh
|
|
543
|
+
exec "${escapeShell(target)}" "$@"
|
|
544
|
+
`;
|
|
545
|
+
writeFileSync2(linkPath, script, { mode: 493 });
|
|
546
|
+
chmodSync(linkPath, 493);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
function escapeShell(value) {
|
|
550
|
+
return value.replaceAll("\\", "\\\\").replaceAll("`", "\\`").replaceAll("$", "\\$").replaceAll('"', '\\"');
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
// src/lib/mount-reconcile.ts
|
|
554
|
+
function computeMountPlan(input) {
|
|
555
|
+
const desiredHandles = new Set(input.desired.map((entry) => entry.handle));
|
|
556
|
+
const sessionGroups = /* @__PURE__ */ new Map();
|
|
557
|
+
for (const session of input.ownedSessions) {
|
|
558
|
+
const group = sessionGroups.get(session.handle);
|
|
559
|
+
if (group) {
|
|
560
|
+
group.push(session);
|
|
561
|
+
} else {
|
|
562
|
+
sessionGroups.set(session.handle, [session]);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
const toCreate = [];
|
|
566
|
+
const toInspect = [];
|
|
567
|
+
const toReset = /* @__PURE__ */ new Set();
|
|
568
|
+
for (const entry of input.desired) {
|
|
569
|
+
const sessions = sessionGroups.get(entry.handle) ?? [];
|
|
570
|
+
const reusable = sessions.length === 1 && !sessions[0].legacy;
|
|
571
|
+
if (reusable) {
|
|
572
|
+
toInspect.push(entry);
|
|
573
|
+
continue;
|
|
574
|
+
}
|
|
575
|
+
if (sessions.length > 0) {
|
|
576
|
+
toReset.add(entry.handle);
|
|
577
|
+
}
|
|
578
|
+
toCreate.push(entry);
|
|
579
|
+
}
|
|
580
|
+
const toStop = /* @__PURE__ */ new Set();
|
|
581
|
+
for (const session of input.ownedSessions) {
|
|
582
|
+
if (!desiredHandles.has(session.handle)) {
|
|
583
|
+
toStop.add(session.handle);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
for (const entry of input.rootEntries) {
|
|
587
|
+
if (!desiredHandles.has(entry)) {
|
|
588
|
+
toStop.add(entry);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
for (const handle of toReset) {
|
|
592
|
+
toStop.delete(handle);
|
|
593
|
+
}
|
|
594
|
+
return {
|
|
595
|
+
toCreate,
|
|
596
|
+
toInspect,
|
|
597
|
+
toReset: Array.from(toReset).sort(),
|
|
598
|
+
toStop: Array.from(toStop).sort(),
|
|
599
|
+
pending: input.pending
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
async function planMountReconcile(config, paths) {
|
|
603
|
+
const computers = await listComputers();
|
|
604
|
+
const desired = [];
|
|
605
|
+
const pending = [];
|
|
606
|
+
for (const computer of computers.filter(isSSHSelectable)) {
|
|
607
|
+
const mountPath = join3(paths.rootPath, computer.handle);
|
|
608
|
+
try {
|
|
609
|
+
const info = await getConnectionInfo(computer.id);
|
|
610
|
+
if (!info.connection.ssh_available) {
|
|
611
|
+
pending.push({
|
|
612
|
+
handle: computer.handle,
|
|
613
|
+
mountPath,
|
|
614
|
+
ready: false,
|
|
615
|
+
message: "SSH is not ready yet"
|
|
616
|
+
});
|
|
617
|
+
continue;
|
|
618
|
+
}
|
|
619
|
+
} catch (error) {
|
|
620
|
+
pending.push({
|
|
621
|
+
handle: computer.handle,
|
|
622
|
+
mountPath,
|
|
623
|
+
ready: false,
|
|
624
|
+
message: error instanceof Error ? error.message : "Failed to confirm SSH readiness"
|
|
625
|
+
});
|
|
626
|
+
continue;
|
|
627
|
+
}
|
|
628
|
+
desired.push({
|
|
629
|
+
handle: computer.handle,
|
|
630
|
+
mountPath,
|
|
631
|
+
ready: true
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
const ownedSessions = listOwnedSessions(config, paths);
|
|
635
|
+
const rootEntries = await listRootHandleDirectories(paths.rootPath);
|
|
636
|
+
return {
|
|
637
|
+
plan: computeMountPlan({
|
|
638
|
+
desired,
|
|
639
|
+
pending,
|
|
640
|
+
ownedSessions,
|
|
641
|
+
rootEntries
|
|
642
|
+
}),
|
|
643
|
+
ownedSessions
|
|
644
|
+
};
|
|
645
|
+
}
|
|
646
|
+
async function reconcileMounts(config, paths, controllerPid = process.pid) {
|
|
647
|
+
ensureMutagenSshEnvironment(config, paths);
|
|
648
|
+
const { plan, ownedSessions } = await planMountReconcile(config, paths);
|
|
649
|
+
const mounts = [];
|
|
650
|
+
const issues = [];
|
|
651
|
+
const ownedByHandle = groupSessionsByHandle(ownedSessions);
|
|
652
|
+
for (const handle of [...plan.toStop, ...plan.toReset]) {
|
|
653
|
+
const sessions = ownedByHandle.get(handle) ?? [];
|
|
654
|
+
try {
|
|
655
|
+
terminateOwnedSessions(sessions, config, paths);
|
|
656
|
+
await removeHandleFromRoot(handle, paths);
|
|
657
|
+
removeMountHandleState(handle, config.rootPath);
|
|
658
|
+
} catch (error) {
|
|
659
|
+
issues.push(
|
|
660
|
+
error instanceof Error ? `${handle}: ${error.message}` : `${handle}: teardown failed`
|
|
661
|
+
);
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
for (const entry of plan.toCreate) {
|
|
665
|
+
try {
|
|
666
|
+
await ensureMountedHandle(entry, config, paths);
|
|
667
|
+
const session = selectPreferredSession(
|
|
668
|
+
entry.handle,
|
|
669
|
+
listOwnedSessions(config, paths).filter((candidate) => candidate.handle === entry.handle)
|
|
670
|
+
);
|
|
671
|
+
const inspected = inspectSnapshotEntry(session, entry.mountPath);
|
|
672
|
+
mounts.push(inspected.snapshot);
|
|
673
|
+
if (inspected.issue) {
|
|
674
|
+
issues.push(inspected.issue);
|
|
675
|
+
}
|
|
676
|
+
} catch (error) {
|
|
677
|
+
const message = error instanceof Error ? error.message : "sync start failed";
|
|
678
|
+
mounts.push(snapshotEntry(entry.handle, entry.mountPath, "error", message));
|
|
679
|
+
issues.push(`${entry.handle}: ${message}`);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
const postCreateSessions = groupSessionsByHandle(listOwnedSessions(config, paths));
|
|
683
|
+
for (const entry of plan.toInspect) {
|
|
684
|
+
try {
|
|
685
|
+
const sessions = postCreateSessions.get(entry.handle) ?? [];
|
|
686
|
+
if (sessions.length === 0) {
|
|
687
|
+
await ensureMountedHandle(entry, config, paths);
|
|
688
|
+
const createdSession = selectPreferredSession(
|
|
689
|
+
entry.handle,
|
|
690
|
+
listOwnedSessions(config, paths).filter((candidate) => candidate.handle === entry.handle)
|
|
691
|
+
);
|
|
692
|
+
const inspected2 = inspectSnapshotEntry(createdSession, entry.mountPath);
|
|
693
|
+
mounts.push(inspected2.snapshot);
|
|
694
|
+
if (inspected2.issue) {
|
|
695
|
+
issues.push(inspected2.issue);
|
|
696
|
+
}
|
|
697
|
+
continue;
|
|
698
|
+
}
|
|
699
|
+
const session = selectPreferredSession(entry.handle, sessions);
|
|
700
|
+
const inspected = inspectSnapshotEntry(session, entry.mountPath);
|
|
701
|
+
mounts.push(inspected.snapshot);
|
|
702
|
+
if (inspected.issue) {
|
|
703
|
+
issues.push(inspected.issue);
|
|
704
|
+
}
|
|
705
|
+
} catch (error) {
|
|
706
|
+
const message = error instanceof Error ? error.message : "sync inspection failed";
|
|
707
|
+
mounts.push(snapshotEntry(entry.handle, entry.mountPath, "error", message));
|
|
708
|
+
issues.push(`${entry.handle}: ${message}`);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
for (const entry of plan.pending) {
|
|
712
|
+
mounts.push(snapshotEntry(entry.handle, entry.mountPath, "pending", entry.message));
|
|
713
|
+
}
|
|
714
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
715
|
+
const previousSnapshot = readMountStatusSnapshot(config.rootPath);
|
|
716
|
+
const healthy = mounts.every((mount) => mount.state === "mounted" || mount.state === "pending");
|
|
717
|
+
const snapshot = {
|
|
718
|
+
updatedAt: now,
|
|
719
|
+
controllerPid,
|
|
720
|
+
running: true,
|
|
721
|
+
lastHealthySyncAt: healthy ? now : previousSnapshot?.lastHealthySyncAt,
|
|
722
|
+
lastSuccessfulSyncAt: healthy ? now : previousSnapshot?.lastSuccessfulSyncAt,
|
|
723
|
+
lastIssueAt: issues.length > 0 ? now : previousSnapshot?.lastIssueAt,
|
|
724
|
+
lastIssue: issues[0],
|
|
725
|
+
lastError: mounts.some((mount) => mount.state === "error") ? issues[0] : void 0,
|
|
726
|
+
mounts: sortSnapshots(mounts)
|
|
727
|
+
};
|
|
728
|
+
writeMountStatusSnapshot(snapshot, config.rootPath);
|
|
729
|
+
return snapshot;
|
|
730
|
+
}
|
|
731
|
+
async function teardownManagedSessions(config, paths) {
|
|
732
|
+
const ownedSessions = listOwnedSessions(config, paths);
|
|
733
|
+
terminateOwnedSessions(ownedSessions, config, paths);
|
|
734
|
+
const handles = new Set(ownedSessions.map((session) => session.handle));
|
|
735
|
+
for (const handle of await listKnownHandleStates(paths)) {
|
|
736
|
+
handles.add(handle);
|
|
737
|
+
}
|
|
738
|
+
for (const handle of await listRootHandleDirectories(paths.rootPath)) {
|
|
739
|
+
handles.add(handle);
|
|
740
|
+
}
|
|
741
|
+
for (const handle of handles) {
|
|
742
|
+
removeMountHandleState(handle, config.rootPath);
|
|
743
|
+
}
|
|
744
|
+
await rm(paths.rootPath, { recursive: true, force: true });
|
|
745
|
+
}
|
|
746
|
+
async function ensureMountedHandle(entry, config, paths) {
|
|
747
|
+
await mkdir(entry.mountPath, { recursive: true });
|
|
748
|
+
createHandleSession(entry.handle, config, paths);
|
|
749
|
+
writeMountHandleMeta(
|
|
750
|
+
entry.handle,
|
|
751
|
+
{
|
|
752
|
+
handle: entry.handle,
|
|
753
|
+
sessionName: getHandleSessionName(entry.handle),
|
|
754
|
+
startedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
755
|
+
lastStartedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
756
|
+
},
|
|
757
|
+
config.rootPath
|
|
758
|
+
);
|
|
759
|
+
}
|
|
760
|
+
function inspectSnapshotEntry(session, mountPath) {
|
|
761
|
+
const status = (session.status ?? "").toLowerCase();
|
|
762
|
+
const problemParts = [];
|
|
763
|
+
if (session.conflictCount > 0) {
|
|
764
|
+
problemParts.push(
|
|
765
|
+
`${session.conflictCount} conflict${session.conflictCount === 1 ? "" : "s"}`
|
|
766
|
+
);
|
|
767
|
+
}
|
|
768
|
+
if (session.scanProblemCount > 0) {
|
|
769
|
+
problemParts.push(
|
|
770
|
+
`${session.scanProblemCount} scan problem${session.scanProblemCount === 1 ? "" : "s"}`
|
|
771
|
+
);
|
|
772
|
+
}
|
|
773
|
+
if (problemParts.length > 0) {
|
|
774
|
+
const message = problemParts.join(", ");
|
|
775
|
+
return {
|
|
776
|
+
snapshot: snapshotEntry(session.handle, mountPath, "degraded", message),
|
|
777
|
+
issue: `${session.handle}: ${message}`
|
|
778
|
+
};
|
|
779
|
+
}
|
|
780
|
+
if (!session.alphaConnected) {
|
|
781
|
+
const message = session.lastError ?? "local sync endpoint disconnected";
|
|
782
|
+
return {
|
|
783
|
+
snapshot: snapshotEntry(session.handle, mountPath, "error", message),
|
|
784
|
+
issue: `${session.handle}: ${message}`
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
if (!session.betaConnected || status.includes("connecting") || status.includes("reconnect")) {
|
|
788
|
+
const message = session.lastError ?? "reconnecting to remote machine";
|
|
789
|
+
return {
|
|
790
|
+
snapshot: snapshotEntry(session.handle, mountPath, "reconnecting", message),
|
|
791
|
+
issue: `${session.handle}: ${message}`
|
|
792
|
+
};
|
|
793
|
+
}
|
|
794
|
+
if (session.lastError && !status.includes("watching")) {
|
|
795
|
+
return {
|
|
796
|
+
snapshot: snapshotEntry(session.handle, mountPath, "degraded", session.lastError),
|
|
797
|
+
issue: `${session.handle}: ${session.lastError}`
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
return {
|
|
801
|
+
snapshot: snapshotEntry(session.handle, mountPath, "mounted")
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
function snapshotEntry(handle, mountPath, state, message) {
|
|
805
|
+
return {
|
|
806
|
+
handle,
|
|
807
|
+
mountPath,
|
|
808
|
+
state,
|
|
809
|
+
message,
|
|
810
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
function sortSnapshots(mounts) {
|
|
814
|
+
return mounts.slice().sort((left, right) => left.handle.localeCompare(right.handle));
|
|
815
|
+
}
|
|
816
|
+
function groupSessionsByHandle(sessions) {
|
|
817
|
+
const grouped = /* @__PURE__ */ new Map();
|
|
818
|
+
for (const session of sessions) {
|
|
819
|
+
const group = grouped.get(session.handle);
|
|
820
|
+
if (group) {
|
|
821
|
+
group.push(session);
|
|
822
|
+
} else {
|
|
823
|
+
grouped.set(session.handle, [session]);
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
return grouped;
|
|
827
|
+
}
|
|
828
|
+
function terminateOwnedSessions(sessions, config, paths) {
|
|
829
|
+
for (const session of sessions) {
|
|
830
|
+
terminateSession(session, config, paths);
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
async function removeHandleFromRoot(handle, paths) {
|
|
834
|
+
await rm(join3(paths.rootPath, handle), { recursive: true, force: true });
|
|
835
|
+
}
|
|
836
|
+
async function listRootHandleDirectories(rootPath) {
|
|
837
|
+
try {
|
|
838
|
+
return (await readdir(rootPath, { withFileTypes: true })).filter((entry) => entry.isDirectory()).map((entry) => entry.name).sort();
|
|
839
|
+
} catch {
|
|
840
|
+
return [];
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
async function listKnownHandleStates(paths) {
|
|
844
|
+
try {
|
|
845
|
+
return (await readdir(paths.handlesDir, { withFileTypes: true })).filter((entry) => entry.isDirectory()).map((entry) => entry.name).sort();
|
|
846
|
+
} catch {
|
|
847
|
+
return [];
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
export {
|
|
852
|
+
getAPIKey,
|
|
853
|
+
getStoredAPIKey,
|
|
854
|
+
hasEnvAPIKey,
|
|
855
|
+
setAPIKey,
|
|
856
|
+
clearAPIKey,
|
|
857
|
+
ApiError,
|
|
858
|
+
getBaseURL,
|
|
859
|
+
getWebURL,
|
|
860
|
+
api,
|
|
861
|
+
apiWithKey,
|
|
862
|
+
listComputers,
|
|
863
|
+
getComputerByID,
|
|
864
|
+
createComputer,
|
|
865
|
+
deleteComputer,
|
|
866
|
+
getFilesystemSettings,
|
|
867
|
+
getConnectionInfo,
|
|
868
|
+
createBrowserAccess,
|
|
869
|
+
listPublishedPorts,
|
|
870
|
+
publishPort,
|
|
871
|
+
deletePublishedPort,
|
|
872
|
+
resolveComputer,
|
|
873
|
+
webURL,
|
|
874
|
+
vncURL,
|
|
875
|
+
padEnd,
|
|
876
|
+
timeAgo,
|
|
877
|
+
formatStatus,
|
|
878
|
+
promptForSSHComputer,
|
|
879
|
+
computeMountPlan,
|
|
880
|
+
planMountReconcile,
|
|
881
|
+
reconcileMounts,
|
|
882
|
+
teardownManagedSessions
|
|
883
|
+
};
|