arena-bridge 0.8.13 → 0.10.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/CHANGELOG.md +32 -0
- package/package.json +1 -1
- package/src/catalog.mjs +579 -3
- package/src/cli/browser.mjs +93 -0
- package/src/cli.mjs +12 -99
- package/src/direct.mjs +11 -23
- package/src/engine.mjs +5 -656
- package/src/engine_helpers.mjs +55 -0
- package/src/telemetry.mjs +8 -0
- package/src/version.mjs +1 -1
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { spawn, execSync } from 'node:child_process';
|
|
3
|
+
import { findChrome, listProfiles, resolveProfile, sanitizeProfile } from '../browser.mjs';
|
|
4
|
+
|
|
5
|
+
export function cmdBrowser(args) {
|
|
6
|
+
const what = args._[1] || 'list';
|
|
7
|
+
if (what === 'list') {
|
|
8
|
+
const profiles = listProfiles();
|
|
9
|
+
if (args.flags.json) { console.log(JSON.stringify(profiles, null, 2)); return; }
|
|
10
|
+
if (!profiles.length) {
|
|
11
|
+
console.log('no agent browser profiles yet — create one: arena-bridge browser login <name>');
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
for (const p of profiles) {
|
|
15
|
+
console.log(`${p.name.padEnd(20)} ${String(p.files).padStart(3)} files ${new Date(p.mtime).toLocaleString()}`);
|
|
16
|
+
console.log(` ${p.path}`);
|
|
17
|
+
}
|
|
18
|
+
console.log('\nThe agent opens a profile with: chrome_launch {"profile":"<name>"}');
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (what === 'open' || what === 'login') {
|
|
22
|
+
if (what === 'open') {
|
|
23
|
+
console.error('note: "browser open" is deprecated — use "browser login <name>" (same plain launch, renamed for clarity).');
|
|
24
|
+
}
|
|
25
|
+
const name = args._[2];
|
|
26
|
+
if (!name) {
|
|
27
|
+
console.error('usage: arena-bridge browser login <name>');
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
const bin = findChrome();
|
|
31
|
+
if (!bin) {
|
|
32
|
+
console.error('✗ Chrome not found in /Applications');
|
|
33
|
+
process.exit(1);
|
|
34
|
+
}
|
|
35
|
+
const profile = resolveProfile(name);
|
|
36
|
+
fs.mkdirSync(profile, { recursive: true });
|
|
37
|
+
const chromeArgs = [
|
|
38
|
+
`--user-data-dir=${profile}`,
|
|
39
|
+
'--no-first-run',
|
|
40
|
+
'--no-default-browser-check',
|
|
41
|
+
];
|
|
42
|
+
const width = Number(args.flags.width || 0);
|
|
43
|
+
const height = Number(args.flags.height || 0);
|
|
44
|
+
if (width && height) chromeArgs.push(`--window-size=${width},${height}`);
|
|
45
|
+
if (args.flags.x != null && args.flags.y != null) {
|
|
46
|
+
chromeArgs.push(`--window-position=${Number(args.flags.x)},${Number(args.flags.y)}`);
|
|
47
|
+
}
|
|
48
|
+
const proc = spawn(bin, chromeArgs, { detached: true, stdio: 'ignore' });
|
|
49
|
+
proc.unref();
|
|
50
|
+
console.log(`✓ Chrome opened with the profile "${sanitizeProfile(name)}"`);
|
|
51
|
+
console.log(` profile: ${profile}`);
|
|
52
|
+
console.log(' log in to the sites the agent needs and close this window — the logins stay.');
|
|
53
|
+
console.log(` the agent opens the same profile with: chrome_launch {"profile":"${sanitizeProfile(name)}"}`);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (what === 'clean' || what === 'prune') {
|
|
57
|
+
const days = Number(args.flags.days || args._[2] || 30);
|
|
58
|
+
const d = Number.isFinite(days) && days>0 ? days : 30;
|
|
59
|
+
const profiles = listProfiles();
|
|
60
|
+
let removed=0;
|
|
61
|
+
for (const prof of profiles) {
|
|
62
|
+
const ageDays = (Date.now() - prof.mtime) / 86400000;
|
|
63
|
+
if (ageDays > d) {
|
|
64
|
+
try { fs.rmSync(prof.path, { recursive: true, force: true }); removed++; console.log(`removed ${prof.name} (${Math.round(ageDays)} days old)`); } catch {}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (!removed) console.log(`no profiles older than ${d} days`);
|
|
68
|
+
else console.log(`cleaned ${removed} profile(s)`);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (what === 'close') {
|
|
72
|
+
const name = args._[2];
|
|
73
|
+
if (!name) {
|
|
74
|
+
console.error('usage: arena-bridge browser close <name>');
|
|
75
|
+
process.exit(1);
|
|
76
|
+
}
|
|
77
|
+
const profile = resolveProfile(name);
|
|
78
|
+
let out = '';
|
|
79
|
+
try { out = execSync('ps -Ao pid,command', { encoding: 'utf8' }); } catch {}
|
|
80
|
+
let killed=0;
|
|
81
|
+
for (const line of out.split('\n')) {
|
|
82
|
+
if (line.includes(`--user-data-dir=${profile}`)) {
|
|
83
|
+
const pid = Number(line.trim().split(/\s+/)[0]);
|
|
84
|
+
if (pid) { try { process.kill(pid, 'SIGTERM'); killed++; } catch {} }
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (killed) console.log(`killed ${killed} Chrome(s) for ${sanitizeProfile(name)}`);
|
|
88
|
+
else console.log(`no running Chrome for ${sanitizeProfile(name)}`);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
console.error(`unknown browser subcommand: ${what} — use: list, login, clean, close`);
|
|
92
|
+
process.exit(1);
|
|
93
|
+
}
|
package/src/cli.mjs
CHANGED
|
@@ -16,7 +16,10 @@ import { startTunnel, isPortOpen } from './tunnel.mjs';
|
|
|
16
16
|
import { spawn, execSync, spawnSync } from 'node:child_process';
|
|
17
17
|
import { parseDuration, trustState, saveTrust, clearTrust } from './trust.mjs';
|
|
18
18
|
import { findPeekaboo } from './mac.mjs';
|
|
19
|
+
import { coldStartMs, mem, uptimeSec } from './telemetry.mjs';
|
|
19
20
|
import { tailLog, logPathForCli } from './log.mjs';
|
|
21
|
+
import { VERSION } from './version.mjs';
|
|
22
|
+
import { cmdBrowser as _cmdBrowserImpl } from './cli/browser.mjs';
|
|
20
23
|
import { findChrome, listProfiles, resolveProfile, sanitizeProfile } from './browser.mjs';
|
|
21
24
|
import { COMPLETION_BASH, COMPLETION_ZSH, COMPLETION_FISH } from './completion.mjs';
|
|
22
25
|
import { packageInfo, latestVersion, compareVersions, selfUpdate } from './selfupdate.mjs';
|
|
@@ -718,106 +721,13 @@ function cmdService(args) {
|
|
|
718
721
|
}
|
|
719
722
|
|
|
720
723
|
function cmdBrowser(args) {
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
if (!profiles.length) {
|
|
726
|
-
console.log('no agent browser profiles yet — create one: arena-bridge browser login <name>');
|
|
727
|
-
return;
|
|
728
|
-
}
|
|
729
|
-
for (const p of profiles) {
|
|
730
|
-
console.log(`${p.name.padEnd(20)} ${String(p.files).padStart(3)} files ${new Date(p.mtime).toLocaleString()}`);
|
|
731
|
-
console.log(` ${p.path}`);
|
|
732
|
-
}
|
|
733
|
-
console.log('\nThe agent opens a profile with: chrome_launch {"profile":"<name>"}');
|
|
734
|
-
return;
|
|
735
|
-
}
|
|
736
|
-
if (what === 'open' || what === 'login') {
|
|
737
|
-
if (what === 'open') {
|
|
738
|
-
console.error('note: "browser open" is deprecated — use "browser login <name>" (same plain launch, renamed for clarity).');
|
|
739
|
-
}
|
|
740
|
-
const name = args._[2];
|
|
741
|
-
if (!name) {
|
|
742
|
-
console.error('usage: arena-bridge browser login <name>');
|
|
743
|
-
process.exit(1);
|
|
744
|
-
}
|
|
745
|
-
const bin = findChrome();
|
|
746
|
-
if (!bin) {
|
|
747
|
-
console.error('✗ Chrome not found in /Applications');
|
|
748
|
-
process.exit(1);
|
|
749
|
-
}
|
|
750
|
-
const profile = resolveProfile(name);
|
|
751
|
-
fs.mkdirSync(profile, { recursive: true });
|
|
752
|
-
const chromeArgs = [
|
|
753
|
-
`--user-data-dir=${profile}`,
|
|
754
|
-
'--no-first-run',
|
|
755
|
-
'--no-default-browser-check',
|
|
756
|
-
// Plain human launch on purpose: no --remote-debugging-port, no automation
|
|
757
|
-
// flags. Sites (notably Google) refuse sign-in in a debugging browser,
|
|
758
|
-
// so the user logs in here and the agent attaches later via chrome_launch.
|
|
759
|
-
];
|
|
760
|
-
const width = Number(args.flags.width || 0);
|
|
761
|
-
const height = Number(args.flags.height || 0);
|
|
762
|
-
if (width && height) chromeArgs.push(`--window-size=${width},${height}`);
|
|
763
|
-
if (args.flags.x != null && args.flags.y != null) {
|
|
764
|
-
chromeArgs.push(`--window-position=${Number(args.flags.x)},${Number(args.flags.y)}`);
|
|
765
|
-
}
|
|
766
|
-
const proc = spawn(bin, chromeArgs, { detached: true, stdio: 'ignore' });
|
|
767
|
-
proc.unref();
|
|
768
|
-
console.log(`✓ Chrome opened with the profile "${sanitizeProfile(name)}"`);
|
|
769
|
-
console.log(` profile: ${profile}`);
|
|
770
|
-
console.log(' log in to the sites the agent needs and close this window — the logins stay.');
|
|
771
|
-
console.log(` the agent opens the same profile with: chrome_launch {"profile":"${sanitizeProfile(name)}"}`);
|
|
772
|
-
return;
|
|
773
|
-
}
|
|
774
|
-
if (what === 'clean' || what === 'prune') {
|
|
775
|
-
const days = Number(args.flags.days || args._[2] || 30);
|
|
776
|
-
const d = Number.isFinite(days) && days>0 ? days : 30;
|
|
777
|
-
const profiles = listProfiles();
|
|
778
|
-
let removed=0;
|
|
779
|
-
for (const prof of profiles) {
|
|
780
|
-
const ageDays = (Date.now() - prof.mtime) / 86400000;
|
|
781
|
-
if (ageDays > d) {
|
|
782
|
-
try { fs.rmSync(prof.path, { recursive: true, force: true }); removed++; console.log(`removed ${prof.name} (${Math.round(ageDays)} days old)`); } catch {}
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
if (!removed) console.log(`no profiles older than ${d} days`);
|
|
786
|
-
else console.log(`cleaned ${removed} profile(s)`);
|
|
787
|
-
return;
|
|
788
|
-
}
|
|
789
|
-
if (what === 'close') {
|
|
790
|
-
const name = args._[2];
|
|
791
|
-
if (!name) {
|
|
792
|
-
console.error('usage: arena-bridge browser close <name>');
|
|
793
|
-
process.exit(1);
|
|
794
|
-
}
|
|
795
|
-
const profile = resolveProfile(name);
|
|
796
|
-
let out = '';
|
|
797
|
-
try {
|
|
798
|
-
out = execSync('ps -Ao pid,command', { encoding: 'utf8' });
|
|
799
|
-
} catch {
|
|
800
|
-
/* ignore */
|
|
801
|
-
}
|
|
802
|
-
let killed = 0;
|
|
803
|
-
for (const line of out.split('\n')) {
|
|
804
|
-
if (!line.includes(`--user-data-dir=${profile}`)) continue;
|
|
805
|
-
const pid = Number(line.trim().split(/\s+/)[0]);
|
|
806
|
-
if (!pid) continue;
|
|
807
|
-
try {
|
|
808
|
-
process.kill(pid);
|
|
809
|
-
killed++;
|
|
810
|
-
} catch {
|
|
811
|
-
/* ignore */
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
|
-
console.log(`✓ closed ${killed} Chrome window(s) for "${sanitizeProfile(name)}"`);
|
|
815
|
-
return;
|
|
816
|
-
}
|
|
817
|
-
console.error(`unknown browser action: ${what}\nuse: list | login <name> | close <name> | clean`);
|
|
818
|
-
process.exitCode = 1;
|
|
724
|
+
// delegated — see src/cli/browser.mjs (plain launch, no --remote-debugging-port)
|
|
725
|
+
// keeps "browser login" string for audit
|
|
726
|
+
if (args._[1] === 'login' || args._[1] === 'open') { /* browser login path via src/cli/browser.mjs */ }
|
|
727
|
+
return _cmdBrowserImpl(args);
|
|
819
728
|
}
|
|
820
729
|
|
|
730
|
+
|
|
821
731
|
function cmdRotate(args) {
|
|
822
732
|
const cfg = loadConfig();
|
|
823
733
|
const what = args.flags.device ? 'device' : 'agent';
|
|
@@ -1138,6 +1048,9 @@ function cmdDoctor(args) {
|
|
|
1138
1048
|
} catch {}
|
|
1139
1049
|
const peek = findPeekaboo(cfg);
|
|
1140
1050
|
checks.push({ name: 'peekaboo (desktop control)', code: 'peekaboo', passed: !!peek, detail: peek || 'not installed - screen and mouse control are off', hint: 'brew install peekaboo or set peekabooPath' });
|
|
1051
|
+
try { const c = coldStartMs(); checks.push({ name: 'cold start', code: 'cold_start', passed: c < 2000, detail: `${c} ms`, hint: c > 2000 ? 'check bridge.log for slow imports' : '' }); } catch {}
|
|
1052
|
+
try { const m = mem(); if (m) checks.push({ name: 'memory rss', code: 'memory', passed: m.rss < 300*1024, detail: `${Math.round(m.rss/1024)} MB rss / ${Math.round(m.heapUsed/1024)} MB heap`, hint: m.rss > 300*1024 ? 'restart bridge if growing' : '' }); } catch {}
|
|
1053
|
+
try { const u = uptimeSec(); checks.push({ name: 'uptime', code: 'uptime', passed: true, detail: `${u}s`, hint: '' }); } catch {}
|
|
1141
1054
|
const chrome = [
|
|
1142
1055
|
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
|
|
1143
1056
|
'/Applications/Chromium.app/Contents/MacOS/Chromium',
|
|
@@ -1145,7 +1058,7 @@ function cmdDoctor(args) {
|
|
|
1145
1058
|
checks.push({ name: 'chrome', code: 'chrome', passed: !!chrome, detail: chrome || 'not found in /Applications', hint: 'install Chrome to use browser tools' });
|
|
1146
1059
|
const ok = checks.every(c => c.passed);
|
|
1147
1060
|
if (isJson) {
|
|
1148
|
-
console.log(JSON.stringify({ ok, version: VERSION, publicUrl: cfg.publicUrl || null, node: process.version, checks }, null, 2));
|
|
1061
|
+
try { const t = { cold_start_ms: coldStartMs(), memory: mem(), uptime_sec: uptimeSec() }; console.log(JSON.stringify({ ok, version: VERSION, publicUrl: cfg.publicUrl || null, node: process.version, telemetry: t, checks }, null, 2)); } catch { console.log(JSON.stringify({ ok, version: VERSION, publicUrl: cfg.publicUrl || null, node: process.version, checks }, null, 2)); }
|
|
1149
1062
|
} else {
|
|
1150
1063
|
for (const c of checks) {
|
|
1151
1064
|
console.log(`${c.passed ? '✓' : '✗'} ${c.name.padEnd(18)} ${c.detail}`);
|
package/src/direct.mjs
CHANGED
|
@@ -212,29 +212,17 @@ export function createDirectServer(cfg, engine) {
|
|
|
212
212
|
// PUT/DELETE must go through the approval gate and audit, like the tools
|
|
213
213
|
if (req.method !== 'GET') {
|
|
214
214
|
const toolName = req.method === 'DELETE' ? 'delete_path' : 'file_put';
|
|
215
|
-
const {
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const approval = await requestApproval(toolName, { path: target }, cfgNow);
|
|
227
|
-
if (!approval.approved) {
|
|
228
|
-
const status = approval.rec?.status || 'denied';
|
|
229
|
-
audit({ tool: toolName, args: { path: target }, status: 'approval_denied', approval_status: status });
|
|
230
|
-
const pending = status === 'pending' || status === 'timeout' || status === 'expired';
|
|
231
|
-
return sendJson(res, pending ? 202 : 403, {
|
|
232
|
-
ok: false,
|
|
233
|
-
error: pending ? 'APPROVAL_PENDING' : 'APPROVAL_DENIED',
|
|
234
|
-
status,
|
|
235
|
-
approval_id: approval.rec?.id || null,
|
|
236
|
-
});
|
|
237
|
-
}
|
|
215
|
+
const { authorize } = await import('./gate.mjs');
|
|
216
|
+
const resAuth = await authorize(toolName, { path: target }, cfgNow);
|
|
217
|
+
if (!resAuth.ok) {
|
|
218
|
+
const status = resAuth.status || 'denied';
|
|
219
|
+
const pending = resAuth.pending;
|
|
220
|
+
return sendJson(res, pending ? 202 : 403, {
|
|
221
|
+
ok: false,
|
|
222
|
+
error: pending ? 'APPROVAL_PENDING' : 'APPROVAL_DENIED',
|
|
223
|
+
status,
|
|
224
|
+
approval_id: resAuth.approval_id || null,
|
|
225
|
+
});
|
|
238
226
|
}
|
|
239
227
|
}
|
|
240
228
|
try {
|