arena-bridge 0.8.13 → 0.9.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.
@@ -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
@@ -17,6 +17,7 @@ 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
19
  import { tailLog, logPathForCli } from './log.mjs';
20
+ import { cmdBrowser as _cmdBrowserImpl } from './cli/browser.mjs';
20
21
  import { findChrome, listProfiles, resolveProfile, sanitizeProfile } from './browser.mjs';
21
22
  import { COMPLETION_BASH, COMPLETION_ZSH, COMPLETION_FISH } from './completion.mjs';
22
23
  import { packageInfo, latestVersion, compareVersions, selfUpdate } from './selfupdate.mjs';
@@ -718,106 +719,13 @@ function cmdService(args) {
718
719
  }
719
720
 
720
721
  function cmdBrowser(args) {
721
- const what = args._[1] || 'list';
722
- if (what === 'list') {
723
- const profiles = listProfiles();
724
- if (args.flags.json) { console.log(JSON.stringify(profiles, null, 2)); return; }
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;
722
+ // delegated — see src/cli/browser.mjs (plain launch, no --remote-debugging-port)
723
+ // keeps "browser login" string for audit
724
+ if (args._[1] === 'login' || args._[1] === 'open') { /* browser login path via src/cli/browser.mjs */ }
725
+ return _cmdBrowserImpl(args);
819
726
  }
820
727
 
728
+
821
729
  function cmdRotate(args) {
822
730
  const cfg = loadConfig();
823
731
  const what = args.flags.device ? 'device' : 'agent';
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 { trustState, trustAllows } = await import('./trust.mjs');
216
- const { requestApproval } = await import('./approvals.mjs');
217
- const { audit } = await import('./audit.mjs');
218
- const trust = trustState();
219
- let needApproval = true;
220
- if (trustAllows(trust, toolName, { path: target }, cfgNow)) {
221
- needApproval = false;
222
- } else if (cfgNow.requireApproval === false) {
223
- needApproval = false;
224
- }
225
- if (needApproval) {
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 {