raytrace-cli 0.11.1__py3-none-win_arm64.whl
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.
- raytrace/__init__.py +1 -0
- raytrace/__main__.py +33 -0
- raytrace/app/LICENSE +21 -0
- raytrace/app/README.md +599 -0
- raytrace/app/bin/raytrace.mjs +297 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-400-normal-BwCSEQnW.woff +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-400-normal-CW0RaeGs.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-500-normal-B9HHJjqV.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-500-normal-Dr3UlScf.woff +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-600-normal-Aqo67rzb.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-600-normal-BmdmIIQ2.woff +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-700-normal-CUSSCpQX.woff +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-700-normal-DvUfVpUG.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-400-normal-BjWJ59Pq.woff +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-400-normal-BtiwyxMk.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-500-normal-BJfUCQsA.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-500-normal-DR84L5F-.woff +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-600-normal-4vooXBpG.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-600-normal-DRtaDpgU.woff +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-700-normal-BLI3TTWz.woff +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-700-normal-CJIcxD6K.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-400-normal-BSMlKf0J.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-400-normal-CEL4l2ZJ.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-500-normal-Ael50iVv.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-500-normal-Bq9vWWag.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-600-normal-CTOM6hUh.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-600-normal-fLZuRloM.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-400-normal-DMdlQ8Kv.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-400-normal-xuaO2J-f.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-500-normal-BIfNGwUT.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-500-normal-BqneJy0T.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-600-normal-9HEixskS.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-600-normal-V-xxqcpd.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-400-normal-CvHOgSBP.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-400-normal-DMJ8VG8y.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-500-normal-CB9ihrfo.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-500-normal-DSY6xOcd.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-600-normal-BgSNZQsw.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-600-normal-DWFSQ4vo.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-400-normal-BmRBH3aV.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-400-normal-D3D2R8hC.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-500-normal-CAhNIIs5.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-500-normal-CZ70TYgx.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-600-normal-D38SheWl.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-600-normal-DmB0ttJJ.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-400-normal-BulugwFq.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-400-normal-DDuiU_S-.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-500-normal-C8zxqsMH.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-500-normal-DZ4AoWbu.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-600-normal-D2EvbN8M.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-600-normal-iLQfcSjf.woff +0 -0
- raytrace/app/dist/dashboard/assets/index-DepcI8Fi.js +38 -0
- raytrace/app/dist/dashboard/assets/index-H97VLD5l.css +1 -0
- raytrace/app/dist/dashboard/favicon.svg +6 -0
- raytrace/app/dist/dashboard/index.html +15 -0
- raytrace/app/package.json +48 -0
- raytrace/app/proxy/agent-intent-schema.test.mjs +142 -0
- raytrace/app/proxy/call-assessment.mjs +385 -0
- raytrace/app/proxy/call-assessment.test.mjs +167 -0
- raytrace/app/proxy/call-category.mjs +461 -0
- raytrace/app/proxy/capture-health.mjs +30 -0
- raytrace/app/proxy/capture-health.test.mjs +44 -0
- raytrace/app/proxy/capture.mjs +38 -0
- raytrace/app/proxy/capture.test.mjs +49 -0
- raytrace/app/proxy/claude-code-adapter.mjs +301 -0
- raytrace/app/proxy/claude-code-hook.mjs +22 -0
- raytrace/app/proxy/codex-config.mjs +36 -0
- raytrace/app/proxy/codex.mjs +14 -0
- raytrace/app/proxy/evidence-schema.test.mjs +240 -0
- raytrace/app/proxy/evidence-store.mjs +993 -0
- raytrace/app/proxy/evidence-store.test.mjs +301 -0
- raytrace/app/proxy/exec-narrative.mjs +625 -0
- raytrace/app/proxy/exec-narrative.test.mjs +262 -0
- raytrace/app/proxy/execution-correlation.mjs +185 -0
- raytrace/app/proxy/execution-correlation.test.mjs +146 -0
- raytrace/app/proxy/execution-runner.mjs +89 -0
- raytrace/app/proxy/execution-runner.test.mjs +38 -0
- raytrace/app/proxy/experiment-engine.mjs +195 -0
- raytrace/app/proxy/experiment-engine.test.mjs +93 -0
- raytrace/app/proxy/explanations.mjs +36 -0
- raytrace/app/proxy/explanations.test.mjs +20 -0
- raytrace/app/proxy/failure-signature.mjs +56 -0
- raytrace/app/proxy/gvisor-forwarder.mjs +493 -0
- raytrace/app/proxy/gvisor-forwarder.test.mjs +433 -0
- raytrace/app/proxy/init-evidence-db.mjs +227 -0
- raytrace/app/proxy/local-sandboxes.mjs +94 -0
- raytrace/app/proxy/local-token.mjs +30 -0
- raytrace/app/proxy/playground-recovery.test.mjs +130 -0
- raytrace/app/proxy/playground.mjs +439 -0
- raytrace/app/proxy/prompt-context.mjs +278 -0
- raytrace/app/proxy/prompt-traces.mjs +40 -0
- raytrace/app/proxy/prompt-traces.test.mjs +32 -0
- raytrace/app/proxy/providers.mjs +65 -0
- raytrace/app/proxy/providers.test.mjs +76 -0
- raytrace/app/proxy/proxy.integration.test.mjs +135 -0
- raytrace/app/proxy/raytace-proxy.mjs +941 -0
- raytrace/app/proxy/request-metrics.mjs +13 -0
- raytrace/app/proxy/request-metrics.test.mjs +19 -0
- raytrace/app/proxy/request-policy.mjs +45 -0
- raytrace/app/proxy/request-policy.test.mjs +47 -0
- raytrace/app/proxy/run-recap.mjs +139 -0
- raytrace/app/proxy/runtime-attribution.mjs +110 -0
- raytrace/app/proxy/sandbox-bridge.mjs +49 -0
- raytrace/app/proxy/schema/EVIDENCE_DATABASE.md +276 -0
- raytrace/app/proxy/schema/agent_intent.sql +101 -0
- raytrace/app/proxy/schema/auxiliary.sql +90 -0
- raytrace/app/proxy/schema/capture_health.sql +14 -0
- raytrace/app/proxy/schema/classification.sql +89 -0
- raytrace/app/proxy/schema/file_context.sql +53 -0
- raytrace/app/proxy/schema/runtime_evidence.sql +201 -0
- raytrace/app/proxy/schema/search.sql +66 -0
- raytrace/app/proxy/schema/shared_storage.sql +21 -0
- raytrace/app/proxy/search.mjs +418 -0
- raytrace/app/proxy/search.test.mjs +161 -0
- raytrace/app/proxy/smoke.test.mjs +93 -0
- raytrace/app/proxy/step-attribution.mjs +380 -0
- raytrace/app/proxy/step-lab.mjs +78 -0
- raytrace/app/proxy/step-lab.test.mjs +38 -0
- raytrace/app/proxy/summaries.mjs +151 -0
- raytrace/app/proxy/summaries.test.mjs +69 -0
- raytrace/app/proxy/tool-metadata.mjs +111 -0
- raytrace/app/proxy/tool-metadata.test.mjs +38 -0
- raytrace/app/proxy/trace-view.mjs +324 -0
- raytrace/app/proxy/trace-view.test.mjs +92 -0
- raytrace/app/proxy/typesafe.mjs +63 -0
- raytrace/app/proxy/typesafe.test.mjs +39 -0
- raytrace/app/src/agents.mjs +7 -0
- raytrace/app/src/api.mjs +57 -0
- raytrace/app/src/auth.mjs +205 -0
- raytrace/app/src/beta.mjs +96 -0
- raytrace/app/src/claude-code.mjs +93 -0
- raytrace/app/src/commands.mjs +285 -0
- raytrace/app/src/config.mjs +49 -0
- raytrace/app/src/export.mjs +200 -0
- raytrace/app/src/lima.mjs +176 -0
- raytrace/app/src/local-sandbox.mjs +358 -0
- raytrace/app/src/mcp.mjs +688 -0
- raytrace/app/src/paths.mjs +33 -0
- raytrace/app/src/prompt.mjs +42 -0
- raytrace/app/src/proxy-process.mjs +143 -0
- raytrace/app/src/sandbox.mjs +306 -0
- raytrace/app/src/share.mjs +86 -0
- raytrace/app/src/terminal.mjs +68 -0
- raytrace/app/src/upload.mjs +198 -0
- raytrace/app/worker/sandbox/Dockerfile +8 -0
- raytrace/app/worker/sandbox/SYSCALL_CATALOG.md +197 -0
- raytrace/app/worker/sandbox/claude-hook.mjs +364 -0
- raytrace/app/worker/sandbox/collector.py +405 -0
- raytrace/app/worker/sandbox/dns_forwarder.py +255 -0
- raytrace/app/worker/sandbox/engine.py +940 -0
- raytrace/app/worker/sandbox/file_scan.py +491 -0
- raytrace/app/worker/sandbox/lima.yaml +29 -0
- raytrace/app/worker/sandbox/network.py +90 -0
- raytrace/app/worker/sandbox/projects.py +702 -0
- raytrace/app/worker/sandbox/setup.py +159 -0
- raytrace/app/worker/sandbox/syscall_catalog.sql +985 -0
- raytrace/app/worker/sandbox/transcript-delivery.test.mjs +153 -0
- raytrace/app/worker/sandbox/viewer.py +195 -0
- raytrace/node/LICENSE +2946 -0
- raytrace/node/node.exe +4 -0
- raytrace_cli-0.11.1.dist-info/LICENSE +21 -0
- raytrace_cli-0.11.1.dist-info/METADATA +42 -0
- raytrace_cli-0.11.1.dist-info/RECORD +165 -0
- raytrace_cli-0.11.1.dist-info/WHEEL +4 -0
- raytrace_cli-0.11.1.dist-info/entry_points.txt +2 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// Lima, which runs the Linux VM local sandboxes live in, and the checks made
|
|
2
|
+
// before one is created (`raytrace sandbox create --local`, `raytrace doctor`).
|
|
3
|
+
//
|
|
4
|
+
// A Lima the user installed is used when it is recent enough. Otherwise
|
|
5
|
+
// RayTrace downloads Lima's own release for this OS and CPU into
|
|
6
|
+
// ~/.raytrace/lima/<version>, after asking: no admin rights, nothing outside
|
|
7
|
+
// that folder. The archive is checked against the checksum pinned here, not
|
|
8
|
+
// one fetched beside it, so a changed release is refused. Lima keeps its VMs
|
|
9
|
+
// in ~/.lima either way.
|
|
10
|
+
//
|
|
11
|
+
// What cannot be downloaded is named with the command that installs it: QEMU
|
|
12
|
+
// on Linux (and on macOS before 13.5) comes from the system's package manager.
|
|
13
|
+
import { spawnSync } from 'node:child_process';
|
|
14
|
+
import { createHash } from 'node:crypto';
|
|
15
|
+
import { accessSync, constants, existsSync, mkdirSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
16
|
+
import { totalmem } from 'node:os';
|
|
17
|
+
import { join } from 'node:path';
|
|
18
|
+
import { paths } from './paths.mjs';
|
|
19
|
+
import { portInUse } from './proxy-process.mjs';
|
|
20
|
+
|
|
21
|
+
export const LIMA_VERSION = '2.2.1';
|
|
22
|
+
// Lima's release archives for each host, and their SHA-256 from the release's SHA256SUMS.
|
|
23
|
+
const RELEASES = {
|
|
24
|
+
'darwin-arm64': ['Darwin-arm64', '9e9eacce88f37e185c346bad73aa6136f738d8cdf8c3bb23cd42b071824bc66e'],
|
|
25
|
+
'darwin-x64': ['Darwin-x86_64', '6b776e4bc41f358e6ee01fefcc78e2eb26f11635a0fcff632234af77a566c6c4'],
|
|
26
|
+
'linux-arm64': ['Linux-aarch64', 'fbcf4d127936666aa2cf6013b663c250431f2a59394757b404070f8d6a160e10'],
|
|
27
|
+
'linux-x64': ['Linux-x86_64', 'b391ac7fcac2b2a5a1628107756463188ded4d18d3104a40a66a8bb6fec01bcb'],
|
|
28
|
+
};
|
|
29
|
+
// The oldest Lima of the user's own that is used; an older one is left alone and the managed one used.
|
|
30
|
+
const MINIMUM = [2, 0];
|
|
31
|
+
const MANAGED = join(paths.home, 'lima', LIMA_VERSION);
|
|
32
|
+
const MANAGED_LIMACTL = join(MANAGED, 'bin', 'limactl');
|
|
33
|
+
// What the sandbox VM forwards to this machine: the evidence viewer.
|
|
34
|
+
const VIEWER_PORT = 8798;
|
|
35
|
+
|
|
36
|
+
function version(binary) {
|
|
37
|
+
const result = spawnSync(binary, ['--version'], { encoding: 'utf8' });
|
|
38
|
+
if (result.error || result.status !== 0) return null;
|
|
39
|
+
const match = /(\d+)\.(\d+)\.(\d+)/.exec(result.stdout);
|
|
40
|
+
return match ? match.slice(1).map(Number) : null;
|
|
41
|
+
}
|
|
42
|
+
const atLeast = (have, want) => have[0] > want[0] || (have[0] === want[0] && have[1] >= want[1]);
|
|
43
|
+
const onPath = (name) => !spawnSync(process.platform === 'win32' ? 'where' : 'which', [name], { stdio: 'ignore' }).status;
|
|
44
|
+
|
|
45
|
+
/** The limactl to run, and where it came from; null when there is none yet. */
|
|
46
|
+
export function findLima() {
|
|
47
|
+
const own = version('limactl');
|
|
48
|
+
if (own && atLeast(own, MINIMUM)) return { binary: 'limactl', version: own.join('.'), source: 'installed' };
|
|
49
|
+
if (existsSync(MANAGED_LIMACTL) && version(MANAGED_LIMACTL)) return { binary: MANAGED_LIMACTL, version: LIMA_VERSION, source: 'managed', old: own?.join('.') };
|
|
50
|
+
return own ? { binary: null, old: own.join('.') } : null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const canDownload = () => Boolean(RELEASES[`${process.platform}-${process.arch}`]);
|
|
54
|
+
|
|
55
|
+
/** Lima's release for this machine, downloaded, checked and unpacked into ~/.raytrace/lima. */
|
|
56
|
+
export async function downloadLima(say) {
|
|
57
|
+
const [name, sha256] = RELEASES[`${process.platform}-${process.arch}`];
|
|
58
|
+
const url = `https://github.com/lima-vm/lima/releases/download/v${LIMA_VERSION}/lima-${LIMA_VERSION}-${name}.tar.gz`;
|
|
59
|
+
say(`Downloading Lima ${LIMA_VERSION} for ${name}...`);
|
|
60
|
+
let response;
|
|
61
|
+
try { response = await fetch(url, { signal: AbortSignal.timeout(300_000) }); } catch (error) {
|
|
62
|
+
throw new Error(`Could not download Lima (${error.message}). Check your connection, or install Lima yourself: https://lima-vm.io/docs/installation/`);
|
|
63
|
+
}
|
|
64
|
+
if (!response.ok) throw new Error(`Could not download Lima: HTTP ${response.status} from ${url}`);
|
|
65
|
+
const archive = Buffer.from(await response.arrayBuffer());
|
|
66
|
+
if (createHash('sha256').update(archive).digest('hex') !== sha256) {
|
|
67
|
+
throw new Error(`The Lima download did not match its pinned checksum, so it was not used. Install Lima yourself: https://lima-vm.io/docs/installation/`);
|
|
68
|
+
}
|
|
69
|
+
// Unpacked beside the final folder and moved over, so a half-unpacked one is never used.
|
|
70
|
+
const staging = `${MANAGED}.partial`;
|
|
71
|
+
rmSync(staging, { recursive: true, force: true });
|
|
72
|
+
mkdirSync(staging, { recursive: true });
|
|
73
|
+
const file = join(staging, 'lima.tar.gz');
|
|
74
|
+
writeFileSync(file, archive);
|
|
75
|
+
const unpacked = spawnSync('tar', ['-xzf', file, '-C', staging], { encoding: 'utf8' });
|
|
76
|
+
if (unpacked.status !== 0) throw new Error(`Could not unpack Lima: ${unpacked.stderr || unpacked.error?.message}`);
|
|
77
|
+
rmSync(file);
|
|
78
|
+
rmSync(MANAGED, { recursive: true, force: true });
|
|
79
|
+
renameSync(staging, MANAGED);
|
|
80
|
+
if (!version(MANAGED_LIMACTL)) throw new Error(`The downloaded Lima does not run on this machine (${MANAGED_LIMACTL}).`);
|
|
81
|
+
say(`Lima ${LIMA_VERSION} is in ${MANAGED}`);
|
|
82
|
+
return { binary: MANAGED_LIMACTL, version: LIMA_VERSION, source: 'managed' };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function macVersion() {
|
|
86
|
+
const shown = spawnSync('sw_vers', ['-productVersion'], { encoding: 'utf8' }).stdout?.trim() ?? '';
|
|
87
|
+
return shown.split('.').map(Number);
|
|
88
|
+
}
|
|
89
|
+
const qemu = () => (process.arch === 'arm64' ? 'qemu-system-aarch64' : 'qemu-system-x86_64');
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* What stands between this machine and a local sandbox, as rows of
|
|
93
|
+
* {ok: true | false | null (a warning), label, detail, fix?}. Lima itself is
|
|
94
|
+
* reported, not fetched: create offers the download.
|
|
95
|
+
*/
|
|
96
|
+
/** What the VM's firewall leaves a sandbox able to reach beyond the bridge
|
|
97
|
+
* (:8799). Lima maps its host address to this machine's localhost, so these
|
|
98
|
+
* rules are all that keeps a sandbox off the proxy. */
|
|
99
|
+
export function sandboxReach(rules) {
|
|
100
|
+
const lines = rules.split('\n').filter((line) => line.includes('-i br-raytace'));
|
|
101
|
+
if (!lines.some((line) => line.includes('-d 192.168.0.0/16') && line.includes('-j REJECT'))) return ['every local port'];
|
|
102
|
+
return lines.filter((line) => line.includes('-j ACCEPT') && !line.includes('--dport 8799')).map((line) => line.match(/--dport (\d+)/)?.[1] ?? 'every local port');
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export async function checks({ vmRunning = false, runInVm = null } = {}) {
|
|
106
|
+
const rows = [];
|
|
107
|
+
const row = (ok, label, detail, fix) => rows.push({ ok, label, detail, fix });
|
|
108
|
+
const platform = process.platform;
|
|
109
|
+
|
|
110
|
+
if (platform === 'win32') row(null, 'platform', 'Windows: local sandboxes are experimental here (Lima on Windows is)');
|
|
111
|
+
else if (platform !== 'darwin' && platform !== 'linux') row(false, 'platform', `${platform} is not supported for local sandboxes`);
|
|
112
|
+
|
|
113
|
+
const lima = findLima();
|
|
114
|
+
if (lima?.binary) row(true, 'lima', `${lima.version} (${lima.source === 'managed' ? `downloaded by RayTrace, ${MANAGED}` : 'installed'})`);
|
|
115
|
+
else if (canDownload()) {
|
|
116
|
+
row(null, 'lima', `${lima?.old ? `${lima.old} is older than ${MINIMUM.join('.')}` : 'not installed'}; RayTrace downloads Lima ${LIMA_VERSION} on the first \`raytrace sandbox create --local\``);
|
|
117
|
+
} else {
|
|
118
|
+
row(false, 'lima', lima?.old ? `${lima.old} is older than ${MINIMUM.join('.')}` : 'not installed', 'install Lima: https://lima-vm.io/docs/installation/');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (platform === 'darwin') {
|
|
122
|
+
const [major, minor = 0] = macVersion();
|
|
123
|
+
// Lima uses Apple's own virtualization (vz) from macOS 13.5; before that, QEMU.
|
|
124
|
+
if (major > 13 || (major === 13 && minor >= 5)) row(true, 'virtualization', `macOS ${major}.${minor}: Apple Virtualization, nothing to install`);
|
|
125
|
+
else if (onPath(qemu())) row(true, 'virtualization', `macOS ${major}.${minor}: QEMU`);
|
|
126
|
+
else row(false, 'virtualization', `macOS ${major}.${minor} needs QEMU for the VM`, 'brew install qemu');
|
|
127
|
+
} else if (platform === 'linux') {
|
|
128
|
+
row(onPath(qemu()) ? true : false, 'qemu', onPath(qemu()) ? qemu() : `${qemu()} not found`,
|
|
129
|
+
'install QEMU: sudo apt install qemu-system (Fedora: sudo dnf install qemu-kvm; Arch: sudo pacman -S qemu-full)');
|
|
130
|
+
if (!existsSync('/dev/kvm')) row(null, 'kvm', 'no /dev/kvm: the VM will be very slow (turn on virtualization, or nested virtualization in a VM)');
|
|
131
|
+
else {
|
|
132
|
+
try { accessSync('/dev/kvm', constants.R_OK | constants.W_OK); row(true, 'kvm', '/dev/kvm'); }
|
|
133
|
+
catch { row(false, 'kvm', 'no access to /dev/kvm', 'sudo usermod -aG kvm $USER, then log out and in again'); }
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// The VM forwards the evidence viewer here; while it runs, the port is its own.
|
|
138
|
+
if (!vmRunning && await portInUse(VIEWER_PORT)) {
|
|
139
|
+
row(false, 'port 8798', 'in use; the evidence viewer needs it', 'stop what holds it (another Lima VM? limactl list)');
|
|
140
|
+
}
|
|
141
|
+
const gib = totalmem() / 1024 ** 3;
|
|
142
|
+
if (gib < 8) row(null, 'memory', `${gib.toFixed(0)} GiB; the VM takes 4 GiB while it runs`);
|
|
143
|
+
|
|
144
|
+
if (vmRunning && typeof runInVm === 'function') {
|
|
145
|
+
try {
|
|
146
|
+
const docker = runInVm(['docker', 'info', '--format', '{{json .Runtimes}}'], { check: false });
|
|
147
|
+
if (docker?.status === 0) {
|
|
148
|
+
row(true, 'docker (in VM)', 'responding');
|
|
149
|
+
const hasGvisor = typeof docker.stdout === 'string' && docker.stdout.includes('raytace-gvisor');
|
|
150
|
+
row(hasGvisor ? true : false, 'raytace-gvisor', hasGvisor ? 'runtime registered' : 'runtime missing from Docker', 'run setup in VM');
|
|
151
|
+
} else {
|
|
152
|
+
row(false, 'docker (in VM)', 'not responding or daemon down', 'sudo systemctl restart docker in VM');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const runsc = runInVm(['/usr/local/bin/runsc', '--version'], { check: false });
|
|
156
|
+
row(runsc?.status === 0 ? true : false, 'runsc (in VM)', runsc?.status === 0 ? String(runsc.stdout).trim().split('\n')[0] : 'missing from /usr/local/bin/runsc');
|
|
157
|
+
|
|
158
|
+
for (const svc of ['collector', 'viewer', 'dns']) {
|
|
159
|
+
const active = runInVm(['systemctl', 'is-active', `raytace-${svc}`], { check: false });
|
|
160
|
+
const isOk = active?.status === 0 && String(active.stdout).trim() === 'active';
|
|
161
|
+
row(isOk ? true : false, `raytace-${svc}`, isOk ? 'active' : `inactive (${String(active?.stdout ?? '').trim() || 'failed'})`, `sudo systemctl restart raytace-${svc}`);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const sock = runInVm(['test', '-S', '/run/raytace/events.sock'], { check: false });
|
|
165
|
+
row(sock?.status === 0 ? true : false, 'collector socket', sock?.status === 0 ? '/run/raytace/events.sock exists' : 'missing /run/raytace/events.sock');
|
|
166
|
+
|
|
167
|
+
const firewall = runInVm(['sudo', 'iptables', '-S', 'DOCKER-USER'], { check: false });
|
|
168
|
+
const open = firewall?.status === 0 ? sandboxReach(String(firewall.stdout)) : null;
|
|
169
|
+
row(open?.length === 0, 'sandbox firewall', !open ? 'could not read the rules' : open.length ? `sandboxes can reach ${open.join(', ')} on this machine` : 'sandboxes reach only the bridge', 'sudo python3 /opt/raytace/network.py in VM');
|
|
170
|
+
} catch (err) {
|
|
171
|
+
row(false, 'vm diagnostics', `could not inspect VM: ${err.message}`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return rows;
|
|
176
|
+
}
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
// `raytrace sandbox … --local`: sandboxes on this machine, in a Lima Linux VM.
|
|
2
|
+
//
|
|
3
|
+
// Your project never leaves the machine. The VM runs each sandbox as a Docker
|
|
4
|
+
// container under gVisor; the collector beside it (worker/sandbox) records the
|
|
5
|
+
// commands, files and connections it makes, independently of the agent, and the
|
|
6
|
+
// dashboard shows them, read from the evidence viewer. The sandbox's Claude Code
|
|
7
|
+
// hook sends its transcript to this CLI's proxy through the sandbox bridge
|
|
8
|
+
// (proxy/sandbox-bridge.mjs), so its sessions show in the local dashboard.
|
|
9
|
+
//
|
|
10
|
+
// Lima picks the VM type for the host: macOS and Linux work; Windows is
|
|
11
|
+
// experimental. Sign in to Claude Code or Codex inside the sandbox: no key is
|
|
12
|
+
// passed in from here.
|
|
13
|
+
import { spawnSync } from 'node:child_process';
|
|
14
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
15
|
+
import { closeSync, existsSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
16
|
+
import { basename, join } from 'node:path';
|
|
17
|
+
import { AGENTS } from './agents.mjs';
|
|
18
|
+
import { paths } from './paths.mjs';
|
|
19
|
+
import { portInUse, runningPid, start as startProxy, stop as stopProxy } from './proxy-process.mjs';
|
|
20
|
+
import { portOf, readConfig } from './config.mjs';
|
|
21
|
+
import { ensureToken } from '../proxy/local-token.mjs';
|
|
22
|
+
import { viewerTokenFor } from '../proxy/request-policy.mjs';
|
|
23
|
+
import { LIMA_VERSION, canDownload, checks, downloadLima, findLima } from './lima.mjs';
|
|
24
|
+
|
|
25
|
+
const say = (line = '') => console.log(line);
|
|
26
|
+
const VM = 'raytrace';
|
|
27
|
+
const ID = /^rtp-[a-f0-9]{32}$/;
|
|
28
|
+
// How a local sandbox is named on the command line: unique among them.
|
|
29
|
+
const NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
30
|
+
const SOURCE = join(paths.root, 'worker', 'sandbox');
|
|
31
|
+
const HOOK = join(SOURCE, 'claude-hook.mjs');
|
|
32
|
+
const REGISTRY = join(paths.home, 'sandboxes.json');
|
|
33
|
+
// Where the proxy keeps each sandbox's copy of its Claude Code transcripts,
|
|
34
|
+
// and where the first local sandboxes' bridge kept them.
|
|
35
|
+
const transcripts = (id) => join(paths.home, 'claude-code', id);
|
|
36
|
+
const earlierTranscripts = (id) => join(paths.home, 'sandboxes', id, 'claude');
|
|
37
|
+
// Logs kept when a sandbox is destroyed without them.
|
|
38
|
+
const ARCHIVE = join(paths.home, 'archive');
|
|
39
|
+
// What the VM's setup was last run from; a CLI upgrade that changes it runs setup again.
|
|
40
|
+
const SETUP_MARK = '/etc/raytace/cli-setup';
|
|
41
|
+
// Lima's and the VM setup's own output, which is long: shown only when a step fails.
|
|
42
|
+
const VM_LOG = join(paths.logs, 'sandbox-vm.log');
|
|
43
|
+
|
|
44
|
+
/** Local sandboxes this CLI made: id → {name, root, token, created}. */
|
|
45
|
+
export function registry() {
|
|
46
|
+
try { return JSON.parse(readFileSync(REGISTRY, 'utf8')); } catch { return {}; }
|
|
47
|
+
}
|
|
48
|
+
function save(boxes) {
|
|
49
|
+
mkdirSync(paths.home, { recursive: true });
|
|
50
|
+
writeFileSync(REGISTRY, `${JSON.stringify(boxes, null, 2)}\n`, { mode: 0o600 });
|
|
51
|
+
}
|
|
52
|
+
const byName = (boxes, name) => Object.keys(boxes).find((id) => boxes[id].name === name);
|
|
53
|
+
/** Whether a sandbox id or name is one of this machine's local sandboxes. */
|
|
54
|
+
export const isLocal = (ref) => Boolean(ref && (registry()[ref] || byName(registry(), ref)));
|
|
55
|
+
|
|
56
|
+
let lima = null;
|
|
57
|
+
/** The limactl to run: the user's own Lima, or the one RayTrace downloaded. */
|
|
58
|
+
function limactlBinary() {
|
|
59
|
+
lima ??= findLima()?.binary;
|
|
60
|
+
if (!lima) throw new Error('Lima is not set up. `raytrace sandbox create --local` sets it up; `raytrace doctor` says what is missing.');
|
|
61
|
+
return lima;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const tail = (file, lines = 25) => { try { return readFileSync(file, 'utf8').trimEnd().split('\n').slice(-lines).join('\n'); } catch { return ''; } };
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Runs limactl. `inherit` gives it the terminal (a shell); `log` sends its
|
|
68
|
+
* output to the VM log instead, and on failure shows the log's last lines.
|
|
69
|
+
*/
|
|
70
|
+
function limactl(args, { input, inherit = false, log = false, check = true } = {}) {
|
|
71
|
+
let fd = null;
|
|
72
|
+
if (log) {
|
|
73
|
+
mkdirSync(paths.logs, { recursive: true });
|
|
74
|
+
fd = openSync(VM_LOG, 'a');
|
|
75
|
+
writeFileSync(fd, `\n=== ${new Date().toISOString()} limactl ${args.join(' ')} ===\n`);
|
|
76
|
+
}
|
|
77
|
+
let result;
|
|
78
|
+
try {
|
|
79
|
+
result = spawnSync(limactlBinary(), args, {
|
|
80
|
+
input, stdio: inherit ? 'inherit' : [input === undefined ? 'ignore' : 'pipe', fd ?? 'pipe', fd ?? 'pipe'],
|
|
81
|
+
encoding: inherit || log ? undefined : 'utf8', maxBuffer: 64 * 1024 * 1024,
|
|
82
|
+
});
|
|
83
|
+
} finally { if (fd !== null) closeSync(fd); }
|
|
84
|
+
if (result.error) throw result.error;
|
|
85
|
+
if (check && result.status !== 0) {
|
|
86
|
+
const detail = log ? `${tail(VM_LOG)}\n(full log: ${VM_LOG})` : String(result.stderr ?? '').trim().split('\n').slice(-6).join('\n');
|
|
87
|
+
throw new Error(`limactl ${args[0]} failed${detail ? `:\n${detail}` : ''}`);
|
|
88
|
+
}
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** A command in the VM as root: projects.py and setup.py run there. */
|
|
93
|
+
export const inVm = (args, options) => limactl(['shell', '--workdir', '/', VM, 'sudo', ...args], options);
|
|
94
|
+
const projects = (args, options) => inVm(['python3', '/opt/raytace/projects.py', ...args], options);
|
|
95
|
+
|
|
96
|
+
/** Removes the RayTrace VM and every local sandbox in it; false when there was none. */
|
|
97
|
+
export function deleteVm() {
|
|
98
|
+
if (!findLima()?.binary || !vmStatus()) return false;
|
|
99
|
+
limactl(['delete', '--force', VM], { log: true });
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function vmStatus() {
|
|
104
|
+
const listed = limactl(['list', '--format', '{{.Name}} {{.Status}}']).stdout;
|
|
105
|
+
const line = listed.split('\n').find((row) => row.split(' ')[0] === VM);
|
|
106
|
+
return line ? line.split(' ')[1] : null;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function setupVersion() {
|
|
110
|
+
const hash = createHash('sha256');
|
|
111
|
+
for (const name of readdirSync(SOURCE).sort()) hash.update(name).update(readFileSync(join(SOURCE, name)));
|
|
112
|
+
return hash.digest('hex').slice(0, 16);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The VM up, with gVisor, the collector and the sandbox image as this CLI ships them. */
|
|
116
|
+
function ensureVm() {
|
|
117
|
+
const status = vmStatus();
|
|
118
|
+
if (!status) {
|
|
119
|
+
say(`Creating the RayTrace VM "${VM}" (first time only: it downloads Ubuntu, a few minutes)...`);
|
|
120
|
+
limactl(['start', '--name', VM, '--tty=false', join(SOURCE, 'lima.yaml')], { log: true });
|
|
121
|
+
} else if (status !== 'Running') {
|
|
122
|
+
say(`Starting the RayTrace VM "${VM}"...`);
|
|
123
|
+
limactl(['start', '--tty=false', VM], { log: true });
|
|
124
|
+
}
|
|
125
|
+
const version = setupVersion();
|
|
126
|
+
const installed = inVm(['cat', SETUP_MARK], { check: false }).stdout?.trim();
|
|
127
|
+
if (installed !== version) installInVm(version);
|
|
128
|
+
// Through stdin: arguments are visible to every account in `ps`. Written each
|
|
129
|
+
// time, so a new token or a rebuilt VM never leaves the viewer locked.
|
|
130
|
+
inVm(['sh', '-c', 'umask 077; cat > /etc/raytace/viewer-token'], { input: `${viewerTokenFor(ensureToken(paths.home))}\n` });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function installInVm(version) {
|
|
134
|
+
say(`Installing gVisor, the evidence collector and the sandbox image in the VM (a few minutes; progress in ${VM_LOG})...`);
|
|
135
|
+
inVm(['rm', '-rf', '/tmp/raytrace-sandbox-source']);
|
|
136
|
+
limactl(['shell', '--workdir', '/', VM, 'mkdir', '-p', '/tmp/raytrace-sandbox-source']);
|
|
137
|
+
limactl(['copy', '-r', `${SOURCE}/.`, `${VM}:/tmp/raytrace-sandbox-source/`]);
|
|
138
|
+
inVm(['python3', '/tmp/raytrace-sandbox-source/setup.py'], { log: true });
|
|
139
|
+
inVm(['sh', '-c', `echo ${version} > ${SETUP_MARK}`]);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Transcripts reach the dashboard through the proxy's sandbox bridge. */
|
|
143
|
+
async function ensureBridge() {
|
|
144
|
+
try { await startProxy(); } catch (error) {
|
|
145
|
+
say(`Note: the RayTrace proxy did not start (${error.message.split('\n')[0]}). `
|
|
146
|
+
+ 'Commands are still recorded in the VM; Claude Code transcripts reach the dashboard once `raytrace start` works.');
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
// A proxy from before local sandboxes is reused as it is, and has no bridge.
|
|
150
|
+
if (!(await portInUse(Number(readConfig().RAYTRACE_BRIDGE_PORT || 8799)))) {
|
|
151
|
+
say('Note: the running RayTrace proxy has no sandbox bridge (it predates local sandboxes). '
|
|
152
|
+
+ 'Restart it so Claude Code sessions reach the dashboard: raytrace stop && raytrace start');
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Before anything is packed or started: what this machine lacks for a local
|
|
158
|
+
* sandbox, each with its fix, and Lima downloaded if the user agrees.
|
|
159
|
+
*/
|
|
160
|
+
export async function preflight(flags, { confirm, interactive }) {
|
|
161
|
+
let vmRunning = false;
|
|
162
|
+
try { vmRunning = Boolean(findLima()?.binary) && vmStatus() === 'Running'; } catch { /* reported below */ }
|
|
163
|
+
const rows = await checks({ vmRunning });
|
|
164
|
+
for (const { ok, label, detail } of rows) if (ok === null && label !== 'lima') say(`Note (${label}): ${detail}`);
|
|
165
|
+
const failed = rows.filter((row) => row.ok === false);
|
|
166
|
+
if (failed.length) {
|
|
167
|
+
throw new Error(`This machine is not ready for local sandboxes:\n${failed.map((row) => ` ${row.label}: ${row.detail}${row.fix ? `\n fix: ${row.fix}` : ''}`).join('\n')}`);
|
|
168
|
+
}
|
|
169
|
+
if (findLima()?.binary) return;
|
|
170
|
+
if (!canDownload()) throw new Error('Local sandboxes need Lima: https://lima-vm.io/docs/installation/');
|
|
171
|
+
const question = `Local sandboxes run in a Linux VM made with Lima (open source, Apache-2.0), which is not installed. `
|
|
172
|
+
+ `Download Lima ${LIMA_VERSION} (about 25-40 MB) into ${join(paths.home, 'lima')}? No admin rights needed.`;
|
|
173
|
+
if (!flags.yes) {
|
|
174
|
+
if (!interactive()) throw new Error(`${question}\nRun again with --yes to download it, or install Lima yourself: https://lima-vm.io/docs/installation/`);
|
|
175
|
+
if (!(await confirm(question, true))) throw new Error('Not downloaded. Install Lima yourself to use local sandboxes: https://lima-vm.io/docs/installation/');
|
|
176
|
+
}
|
|
177
|
+
lima = (await downloadLima(say)).binary;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** A local sandbox's id, from its id or name; with neither, the only one there is. */
|
|
181
|
+
function pickId(ref) {
|
|
182
|
+
const boxes = registry();
|
|
183
|
+
if (ref) {
|
|
184
|
+
const id = boxes[ref] ? ref : byName(boxes, ref);
|
|
185
|
+
if (!id) throw new Error(`No local sandbox named ${ref}: raytrace sandbox list --local`);
|
|
186
|
+
return id;
|
|
187
|
+
}
|
|
188
|
+
const ids = Object.keys(boxes);
|
|
189
|
+
if (ids.length === 1) return ids[0];
|
|
190
|
+
throw new Error(ids.length ? 'More than one local sandbox; name one (raytrace sandbox list --local).'
|
|
191
|
+
: 'No local sandboxes yet: raytrace sandbox create --local');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** The new sandbox's name: --name, or the repository folder's; unique among local sandboxes. */
|
|
195
|
+
export function sandboxName(flags, root) {
|
|
196
|
+
const name = flags.name ?? basename(root).replace(/[^A-Za-z0-9._-]+/g, '-').replace(/^[^A-Za-z0-9]+/, '').slice(0, 64);
|
|
197
|
+
if (!NAME.test(name)) throw new Error(`"${name}" cannot be a sandbox name: use letters, digits, dot, dash and underscore (up to 64), starting with a letter or digit.`);
|
|
198
|
+
if (byName(registry(), name)) throw new Error(`There is already a local sandbox named ${name}. Pick another with --name, or destroy that one: raytrace sandbox destroy ${name}`);
|
|
199
|
+
return name;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export async function create(name, { root, files, skipped, total, archive }) {
|
|
203
|
+
ensureVm();
|
|
204
|
+
const id = `rtp-${randomBytes(16).toString('hex')}`;
|
|
205
|
+
const token = randomBytes(32).toString('base64url');
|
|
206
|
+
say(`Copying ${files.length} files (${(total / 1048576).toFixed(1)} MiB) from ${root} into a new sandbox`);
|
|
207
|
+
if (skipped.length) say(`Left out (${skipped.length}): ${skipped.slice(0, 8).join(', ')}${skipped.length > 8 ? ', ...' : ''}`);
|
|
208
|
+
projects(['create', id, '--archive', '-'], { input: archive });
|
|
209
|
+
// Recorded before anything else can fail, so destroy can always find it.
|
|
210
|
+
save({ ...registry(), [id]: { name, root, token, created: new Date().toISOString() } });
|
|
211
|
+
projects(['start', id]);
|
|
212
|
+
projects(['enable-claude', id], { input: readFileSync(HOOK) });
|
|
213
|
+
projects(['set-token', id], { input: token });
|
|
214
|
+
await ensureBridge();
|
|
215
|
+
say(`\nLocal sandbox ${name} (${id}) is running.`);
|
|
216
|
+
say('Claude Code sessions in it go to your local dashboard; commands, files and connections are recorded in the VM.');
|
|
217
|
+
say(`Open it: raytrace connect ${name}`);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** This machine's local sandboxes with their state: running, stopped, or
|
|
221
|
+
* missing (in the registry, gone from the VM). `vmRunning` says whether the
|
|
222
|
+
* states were read from the VM; when it is not running, all are stopped. */
|
|
223
|
+
export function localSandboxes() {
|
|
224
|
+
const boxes = registry();
|
|
225
|
+
if (!Object.keys(boxes).length) return { vmRunning: false, sandboxes: [] };
|
|
226
|
+
let running = false;
|
|
227
|
+
try { running = vmStatus() === 'Running'; } catch { /* no Lima: nothing can be running */ }
|
|
228
|
+
const states = running ? Object.fromEntries(JSON.parse(projects(['list']).stdout || '[]').map((box) => [box.id, box.status])) : {};
|
|
229
|
+
const sandboxes = Object.entries(boxes).map(([id, { token, ...box }]) => ({
|
|
230
|
+
id, ...box, status: running ? (states[id] === 'running' ? 'running' : states[id] ? 'stopped' : 'missing') : 'stopped',
|
|
231
|
+
}));
|
|
232
|
+
return { vmRunning: running, sandboxes };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export async function list() {
|
|
236
|
+
const { vmRunning: running, sandboxes } = localSandboxes();
|
|
237
|
+
if (!sandboxes.length) { say('No local sandboxes. Create one from a Git repository: raytrace sandbox create --local'); return; }
|
|
238
|
+
for (const box of sandboxes) {
|
|
239
|
+
const age = Math.round((Date.now() - Date.parse(box.created)) / 60000);
|
|
240
|
+
say(`${box.name.padEnd(20)} ${box.status.padEnd(8)} ${box.id} (${age < 120 ? `${age} min` : `${Math.round(age / 60)} h`} old, local)`);
|
|
241
|
+
}
|
|
242
|
+
if (!running) say(`\nThe RayTrace VM is stopped; \`raytrace sandbox start <name>\` starts it.`);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export async function start(id) {
|
|
246
|
+
id = pickId(id);
|
|
247
|
+
ensureVm();
|
|
248
|
+
projects(['start', id]);
|
|
249
|
+
await ensureBridge();
|
|
250
|
+
say(`${registry()[id].name} running`);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export async function stop(id) {
|
|
254
|
+
id = pickId(id);
|
|
255
|
+
if (vmStatus() === 'Running') projects(['stop', id]);
|
|
256
|
+
say(`${registry()[id].name} stopped; its files are kept`);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const stamp = () => new Date().toISOString().replace(/[-:]/g, '').replace('T', '-').slice(0, 15);
|
|
260
|
+
|
|
261
|
+
/** The sandbox's Claude Code session ids, from the bridge's transcript copies. */
|
|
262
|
+
function sessionIds(id) {
|
|
263
|
+
const listed = (folder) => { try { return readdirSync(folder).filter((file) => file.endsWith('.jsonl')).map((file) => file.slice(0, -6)); } catch { return []; } };
|
|
264
|
+
return [...listed(transcripts(id)), ...listed(earlierTranscripts(id))];
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Deletes a local sandbox, and asks about the logs captured in it: deleted
|
|
269
|
+
* with it, or moved to ~/.raytrace/archive/<name>-<time>/ (the Claude Code
|
|
270
|
+
* transcripts and the VM's command, file and network evidence), staying in
|
|
271
|
+
* the dashboard. --delete-logs / --keep-logs answer without asking; without a
|
|
272
|
+
* terminal and neither flag, the logs are kept.
|
|
273
|
+
*/
|
|
274
|
+
export async function destroy(ref, flags, { ask, interactive }) {
|
|
275
|
+
const id = pickId(ref);
|
|
276
|
+
const box = registry()[id];
|
|
277
|
+
if (!flags.yes) {
|
|
278
|
+
if (!interactive()) throw new Error('Pass --yes to destroy without a prompt.');
|
|
279
|
+
say(`This deletes local sandbox ${box.name}: its files, and any sign-in made inside it.`);
|
|
280
|
+
if ((await ask('Type the sandbox name to confirm: ')) !== box.name) { say('Not destroyed.'); return; }
|
|
281
|
+
}
|
|
282
|
+
// A destroyed sandbox can no longer be uploaded, so this is the last chance:
|
|
283
|
+
// asked once, default no, and a failed upload leaves the sandbox in place.
|
|
284
|
+
if (!flags.yes && interactive()
|
|
285
|
+
&& /^y(es)?$/i.test((await ask('Upload its evidences to RayTrace cloud first? (yes/no) [no]: ')).trim())) {
|
|
286
|
+
const { uploadLogs } = await import('./upload.mjs');
|
|
287
|
+
await uploadLogs({ sandbox: id, yes: true });
|
|
288
|
+
}
|
|
289
|
+
let deleteLogs = flags['delete-logs'] ? true : flags['keep-logs'] ? false : null;
|
|
290
|
+
if (deleteLogs === null) {
|
|
291
|
+
deleteLogs = interactive()
|
|
292
|
+
&& /^y(es)?$/i.test((await ask('Also delete the logs captured in this sandbox? (yes/no) [no]: ')).trim());
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
ensureVm();
|
|
296
|
+
const inVmNow = JSON.parse(projects(['list']).stdout || '[]');
|
|
297
|
+
const exists = inVmNow.some((item) => item.id === id);
|
|
298
|
+
// Its Playground forks, which are sandboxes of their own in the VM.
|
|
299
|
+
const forks = inVmNow.filter((item) => item.fork_of === id).map((item) => item.id);
|
|
300
|
+
// Stopped first: stopping is recorded too, and must not land after the logs are dealt with.
|
|
301
|
+
if (exists) projects(['stop', id]);
|
|
302
|
+
if (deleteLogs) {
|
|
303
|
+
// Its sessions are filed under its id; ones recorded before that, under their own.
|
|
304
|
+
const { openEvidenceStore } = await import('../proxy/evidence-store.mjs');
|
|
305
|
+
const store = openEvidenceStore(paths.database);
|
|
306
|
+
try { store.forgetSessions([id, ...sessionIds(id)]); } finally { store.close(); }
|
|
307
|
+
rmSync(transcripts(id), { recursive: true, force: true });
|
|
308
|
+
rmSync(join(paths.home, 'sandboxes', id), { recursive: true, force: true });
|
|
309
|
+
} else {
|
|
310
|
+
const folder = join(ARCHIVE, `${box.name}-${stamp()}`);
|
|
311
|
+
mkdirSync(folder, { recursive: true, mode: 0o700 });
|
|
312
|
+
const { token, ...about } = box;
|
|
313
|
+
writeFileSync(join(folder, 'sandbox.json'), `${JSON.stringify({ id, ...about, destroyed: new Date().toISOString() }, null, 2)}\n`);
|
|
314
|
+
if (exists) writeFileSync(join(folder, 'runtime-evidence.jsonl'), projects(['evidence', id]).stdout);
|
|
315
|
+
for (const from of [transcripts(id), earlierTranscripts(id)]) {
|
|
316
|
+
if (existsSync(from)) { renameSync(from, join(folder, 'claude-code')); break; }
|
|
317
|
+
}
|
|
318
|
+
// Forks' transcripts are filed under their own ids; their recorded
|
|
319
|
+
// commands are already in the dashboard, with the forks themselves.
|
|
320
|
+
for (const fork of forks) {
|
|
321
|
+
if (!existsSync(transcripts(fork))) continue;
|
|
322
|
+
mkdirSync(join(folder, 'forks'), { recursive: true, mode: 0o700 });
|
|
323
|
+
renameSync(transcripts(fork), join(folder, 'forks', fork));
|
|
324
|
+
}
|
|
325
|
+
rmSync(join(paths.home, 'sandboxes', id), { recursive: true, force: true });
|
|
326
|
+
say(`Logs moved to ${folder}`);
|
|
327
|
+
say('Its Claude Code sessions stay in your dashboard.');
|
|
328
|
+
}
|
|
329
|
+
// The VM's record of it goes either way: deleted, or by now in the archive.
|
|
330
|
+
for (const fork of forks) projects(['fork-delete', fork, '--forget-evidence']);
|
|
331
|
+
if (deleteLogs) for (const fork of forks) rmSync(transcripts(fork), { recursive: true, force: true });
|
|
332
|
+
if (exists) projects(['delete', id, '--confirm', id, '--forget-evidence']);
|
|
333
|
+
const boxes = registry();
|
|
334
|
+
delete boxes[id];
|
|
335
|
+
save(boxes);
|
|
336
|
+
say(`${box.name} destroyed${forks.length ? `, with its ${forks.length} Playground fork${forks.length === 1 ? '' : 's'}` : ''}${deleteLogs ? ', and its logs' : ''}.`);
|
|
337
|
+
// The proxy keeps the Playground's forks in memory: one this CLI started is
|
|
338
|
+
// restarted, so the forks just deleted are not listed any more.
|
|
339
|
+
if (deleteLogs && forks.length && runningPid()) { await stopProxy(); await startProxy(); }
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** A terminal in the sandbox, through the VM; with `claude`, Claude Code
|
|
343
|
+
* starts in it first. Leaving it keeps the sandbox running. */
|
|
344
|
+
/** The local dashboard's Prompts, only those that ran in this sandbox. */
|
|
345
|
+
export const promptsLink = (id) => `http://127.0.0.1:${portOf(readConfig())}/?box=${encodeURIComponent(id)}`;
|
|
346
|
+
|
|
347
|
+
export async function shell(id, { agent } = {}) {
|
|
348
|
+
id = pickId(id);
|
|
349
|
+
ensureVm();
|
|
350
|
+
await ensureBridge();
|
|
351
|
+
const { name } = registry()[id];
|
|
352
|
+
say(agent ? `Connected to ${name}, starting ${AGENTS[agent].name} without approval prompts (the sandbox is what contains it); when it exits you are in the sandbox's shell. The sandbox keeps running.`
|
|
353
|
+
: `Connected to ${name}. Run \`claude\` (or codex) here; exit to leave. The sandbox keeps running.`);
|
|
354
|
+
say(`Its prompts, and only its: ${promptsLink(id)}`);
|
|
355
|
+
say('Recorded commands, files and connections: raytrace open, then Sandbox evidence\n');
|
|
356
|
+
projects(['shell', id, ...(agent ? ['--agent', agent] : [])], { inherit: true, check: false });
|
|
357
|
+
say(`\nLeft ${name}. It is still running. Back in: raytrace connect ${name} Stop it: raytrace sandbox stop ${name}`);
|
|
358
|
+
}
|