@raytrace-cli/cli 0.3.0 → 0.5.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/README.md +155 -132
- package/bin/raytrace.mjs +51 -26
- package/dist/dashboard/assets/dm-sans-latin-400-normal-BwCSEQnW.woff +0 -0
- package/dist/dashboard/assets/dm-sans-latin-400-normal-CW0RaeGs.woff2 +0 -0
- package/dist/dashboard/assets/dm-sans-latin-500-normal-B9HHJjqV.woff2 +0 -0
- package/dist/dashboard/assets/dm-sans-latin-500-normal-Dr3UlScf.woff +0 -0
- package/dist/dashboard/assets/dm-sans-latin-600-normal-Aqo67rzb.woff2 +0 -0
- package/dist/dashboard/assets/dm-sans-latin-600-normal-BmdmIIQ2.woff +0 -0
- package/dist/dashboard/assets/dm-sans-latin-700-normal-CUSSCpQX.woff +0 -0
- package/dist/dashboard/assets/dm-sans-latin-700-normal-DvUfVpUG.woff2 +0 -0
- package/dist/dashboard/assets/dm-sans-latin-ext-400-normal-BjWJ59Pq.woff +0 -0
- package/dist/dashboard/assets/dm-sans-latin-ext-400-normal-BtiwyxMk.woff2 +0 -0
- package/dist/dashboard/assets/dm-sans-latin-ext-500-normal-BJfUCQsA.woff2 +0 -0
- package/dist/dashboard/assets/dm-sans-latin-ext-500-normal-DR84L5F-.woff +0 -0
- package/dist/dashboard/assets/dm-sans-latin-ext-600-normal-4vooXBpG.woff2 +0 -0
- package/dist/dashboard/assets/dm-sans-latin-ext-600-normal-DRtaDpgU.woff +0 -0
- package/dist/dashboard/assets/dm-sans-latin-ext-700-normal-BLI3TTWz.woff +0 -0
- package/dist/dashboard/assets/dm-sans-latin-ext-700-normal-CJIcxD6K.woff2 +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-cyrillic-400-normal-BSMlKf0J.woff2 +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-cyrillic-400-normal-CEL4l2ZJ.woff +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-cyrillic-500-normal-Ael50iVv.woff +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-cyrillic-500-normal-Bq9vWWag.woff2 +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-cyrillic-600-normal-CTOM6hUh.woff2 +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-cyrillic-600-normal-fLZuRloM.woff +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-400-normal-DMdlQ8Kv.woff +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-400-normal-xuaO2J-f.woff2 +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-500-normal-BIfNGwUT.woff +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-500-normal-BqneJy0T.woff2 +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-600-normal-9HEixskS.woff +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-600-normal-V-xxqcpd.woff2 +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-latin-400-normal-CvHOgSBP.woff +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-latin-400-normal-DMJ8VG8y.woff2 +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-latin-500-normal-CB9ihrfo.woff +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-latin-500-normal-DSY6xOcd.woff2 +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-latin-600-normal-BgSNZQsw.woff2 +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-latin-600-normal-DWFSQ4vo.woff +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-latin-ext-400-normal-BmRBH3aV.woff2 +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-latin-ext-400-normal-D3D2R8hC.woff +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-latin-ext-500-normal-CAhNIIs5.woff2 +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-latin-ext-500-normal-CZ70TYgx.woff +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-latin-ext-600-normal-D38SheWl.woff2 +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-latin-ext-600-normal-DmB0ttJJ.woff +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-vietnamese-400-normal-BulugwFq.woff2 +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-vietnamese-400-normal-DDuiU_S-.woff +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-vietnamese-500-normal-C8zxqsMH.woff +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-vietnamese-500-normal-DZ4AoWbu.woff2 +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-vietnamese-600-normal-D2EvbN8M.woff2 +0 -0
- package/dist/dashboard/assets/ibm-plex-mono-vietnamese-600-normal-iLQfcSjf.woff +0 -0
- package/dist/dashboard/assets/index-DtV3UdEB.css +1 -0
- package/dist/dashboard/assets/index-jyV5iYHJ.js +38 -0
- package/dist/dashboard/index.html +2 -2
- package/package.json +3 -1
- package/proxy/call-assessment.mjs +385 -0
- package/proxy/call-category.mjs +461 -0
- package/proxy/capture.mjs +31 -0
- package/proxy/claude-code-adapter.mjs +46 -3
- package/proxy/evidence-store.mjs +617 -15
- package/proxy/exec-narrative.mjs +625 -0
- package/proxy/execution-correlation.mjs +185 -0
- package/proxy/experiment-engine.mjs +7 -4
- package/proxy/failure-signature.mjs +56 -0
- package/proxy/gvisor-forwarder.mjs +454 -0
- package/proxy/init-evidence-db.mjs +136 -12
- package/proxy/local-sandboxes.mjs +94 -0
- package/proxy/playground.mjs +411 -0
- package/proxy/prompt-context.mjs +12 -7
- package/proxy/prompt-traces.mjs +6 -0
- package/proxy/raytace-proxy.mjs +460 -176
- package/proxy/run-recap.mjs +139 -0
- package/proxy/runtime-attribution.mjs +110 -0
- package/proxy/sandbox-bridge.mjs +46 -0
- package/proxy/schema/EVIDENCE_DATABASE.md +276 -0
- package/proxy/schema/auxiliary.sql +48 -1
- package/proxy/schema/classification.sql +89 -0
- package/proxy/schema/file_context.sql +53 -0
- package/proxy/schema/runtime_evidence.sql +201 -0
- package/proxy/schema/search.sql +66 -0
- package/proxy/search.mjs +418 -0
- package/proxy/step-attribution.mjs +380 -0
- package/proxy/tool-metadata.mjs +36 -0
- package/proxy/trace-view.mjs +276 -0
- package/proxy/typesafe.mjs +63 -0
- package/src/auth.mjs +8 -1
- package/src/commands.mjs +80 -12
- package/src/lima.mjs +135 -0
- package/src/local-sandbox.mjs +324 -0
- package/src/proxy-process.mjs +20 -1
- package/src/sandbox.mjs +24 -6
- package/worker/sandbox/Dockerfile +8 -0
- package/worker/sandbox/SYSCALL_CATALOG.md +197 -0
- package/worker/sandbox/claude-hook.mjs +331 -0
- package/worker/sandbox/collector.py +357 -0
- package/worker/sandbox/dns_forwarder.py +255 -0
- package/worker/sandbox/engine.py +940 -0
- package/worker/sandbox/file_scan.py +491 -0
- package/worker/sandbox/lima.yaml +29 -0
- package/worker/sandbox/network.py +90 -0
- package/worker/sandbox/projects.py +678 -0
- package/worker/sandbox/setup.py +159 -0
- package/worker/sandbox/syscall_catalog.sql +985 -0
- package/worker/sandbox/viewer.py +132 -0
- package/dist/dashboard/assets/index-BQ9_JxGq.js +0 -27
- package/dist/dashboard/assets/index-L5cUAvhE.css +0 -1
package/src/lima.mjs
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
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
|
+
export async function checks({ vmRunning = false } = {}) {
|
|
97
|
+
const rows = [];
|
|
98
|
+
const row = (ok, label, detail, fix) => rows.push({ ok, label, detail, fix });
|
|
99
|
+
const platform = process.platform;
|
|
100
|
+
|
|
101
|
+
if (platform === 'win32') row(null, 'platform', 'Windows: local sandboxes are experimental here (Lima on Windows is)');
|
|
102
|
+
else if (platform !== 'darwin' && platform !== 'linux') row(false, 'platform', `${platform} is not supported for local sandboxes`);
|
|
103
|
+
|
|
104
|
+
const lima = findLima();
|
|
105
|
+
if (lima?.binary) row(true, 'lima', `${lima.version} (${lima.source === 'managed' ? `downloaded by RayTrace, ${MANAGED}` : 'installed'})`);
|
|
106
|
+
else if (canDownload()) {
|
|
107
|
+
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\``);
|
|
108
|
+
} else {
|
|
109
|
+
row(false, 'lima', lima?.old ? `${lima.old} is older than ${MINIMUM.join('.')}` : 'not installed', 'install Lima: https://lima-vm.io/docs/installation/');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (platform === 'darwin') {
|
|
113
|
+
const [major, minor = 0] = macVersion();
|
|
114
|
+
// Lima uses Apple's own virtualization (vz) from macOS 13.5; before that, QEMU.
|
|
115
|
+
if (major > 13 || (major === 13 && minor >= 5)) row(true, 'virtualization', `macOS ${major}.${minor}: Apple Virtualization, nothing to install`);
|
|
116
|
+
else if (onPath(qemu())) row(true, 'virtualization', `macOS ${major}.${minor}: QEMU`);
|
|
117
|
+
else row(false, 'virtualization', `macOS ${major}.${minor} needs QEMU for the VM`, 'brew install qemu');
|
|
118
|
+
} else if (platform === 'linux') {
|
|
119
|
+
row(onPath(qemu()) ? true : false, 'qemu', onPath(qemu()) ? qemu() : `${qemu()} not found`,
|
|
120
|
+
'install QEMU: sudo apt install qemu-system (Fedora: sudo dnf install qemu-kvm; Arch: sudo pacman -S qemu-full)');
|
|
121
|
+
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)');
|
|
122
|
+
else {
|
|
123
|
+
try { accessSync('/dev/kvm', constants.R_OK | constants.W_OK); row(true, 'kvm', '/dev/kvm'); }
|
|
124
|
+
catch { row(false, 'kvm', 'no access to /dev/kvm', 'sudo usermod -aG kvm $USER, then log out and in again'); }
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// The VM forwards the evidence viewer here; while it runs, the port is its own.
|
|
129
|
+
if (!vmRunning && await portInUse(VIEWER_PORT)) {
|
|
130
|
+
row(false, 'port 8798', 'in use; the evidence viewer needs it', 'stop what holds it (another Lima VM? limactl list)');
|
|
131
|
+
}
|
|
132
|
+
const gib = totalmem() / 1024 ** 3;
|
|
133
|
+
if (gib < 8) row(null, 'memory', `${gib.toFixed(0)} GiB; the VM takes 4 GiB while it runs`);
|
|
134
|
+
return rows;
|
|
135
|
+
}
|
|
@@ -0,0 +1,324 @@
|
|
|
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
|
+
// evidence viewer shows them on http://localhost:8798. 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 { paths } from './paths.mjs';
|
|
18
|
+
import { portInUse, runningPid, start as startProxy, stop as stopProxy } from './proxy-process.mjs';
|
|
19
|
+
import { readConfig } from './config.mjs';
|
|
20
|
+
import { LIMA_VERSION, canDownload, checks, downloadLima, findLima } from './lima.mjs';
|
|
21
|
+
|
|
22
|
+
const say = (line = '') => console.log(line);
|
|
23
|
+
const VM = 'raytrace';
|
|
24
|
+
const ID = /^rtp-[a-f0-9]{32}$/;
|
|
25
|
+
// How a local sandbox is named on the command line: unique among them.
|
|
26
|
+
const NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
27
|
+
const SOURCE = join(paths.root, 'worker', 'sandbox');
|
|
28
|
+
const HOOK = join(SOURCE, 'claude-hook.mjs');
|
|
29
|
+
const REGISTRY = join(paths.home, 'sandboxes.json');
|
|
30
|
+
// Where the proxy keeps each sandbox's copy of its Claude Code transcripts,
|
|
31
|
+
// and where the first local sandboxes' bridge kept them.
|
|
32
|
+
const transcripts = (id) => join(paths.home, 'claude-code', id);
|
|
33
|
+
const earlierTranscripts = (id) => join(paths.home, 'sandboxes', id, 'claude');
|
|
34
|
+
// Logs kept when a sandbox is destroyed without them.
|
|
35
|
+
const ARCHIVE = join(paths.home, 'archive');
|
|
36
|
+
// What the VM's setup was last run from; a CLI upgrade that changes it runs setup again.
|
|
37
|
+
const SETUP_MARK = '/etc/raytace/cli-setup';
|
|
38
|
+
// Lima's and the VM setup's own output, which is long: shown only when a step fails.
|
|
39
|
+
const VM_LOG = join(paths.logs, 'sandbox-vm.log');
|
|
40
|
+
|
|
41
|
+
/** Local sandboxes this CLI made: id → {name, root, token, created}. */
|
|
42
|
+
export function registry() {
|
|
43
|
+
try { return JSON.parse(readFileSync(REGISTRY, 'utf8')); } catch { return {}; }
|
|
44
|
+
}
|
|
45
|
+
function save(boxes) {
|
|
46
|
+
mkdirSync(paths.home, { recursive: true });
|
|
47
|
+
writeFileSync(REGISTRY, `${JSON.stringify(boxes, null, 2)}\n`, { mode: 0o600 });
|
|
48
|
+
}
|
|
49
|
+
const byName = (boxes, name) => Object.keys(boxes).find((id) => boxes[id].name === name);
|
|
50
|
+
/** Whether a sandbox id or name is one of this machine's local sandboxes. */
|
|
51
|
+
export const isLocal = (ref) => Boolean(ref && (registry()[ref] || byName(registry(), ref)));
|
|
52
|
+
|
|
53
|
+
let lima = null;
|
|
54
|
+
/** The limactl to run: the user's own Lima, or the one RayTrace downloaded. */
|
|
55
|
+
function limactlBinary() {
|
|
56
|
+
lima ??= findLima()?.binary;
|
|
57
|
+
if (!lima) throw new Error('Lima is not set up. `raytrace sandbox create --local` sets it up; `raytrace doctor` says what is missing.');
|
|
58
|
+
return lima;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const tail = (file, lines = 25) => { try { return readFileSync(file, 'utf8').trimEnd().split('\n').slice(-lines).join('\n'); } catch { return ''; } };
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Runs limactl. `inherit` gives it the terminal (a shell); `log` sends its
|
|
65
|
+
* output to the VM log instead, and on failure shows the log's last lines.
|
|
66
|
+
*/
|
|
67
|
+
function limactl(args, { input, inherit = false, log = false, check = true } = {}) {
|
|
68
|
+
let fd = null;
|
|
69
|
+
if (log) {
|
|
70
|
+
mkdirSync(paths.logs, { recursive: true });
|
|
71
|
+
fd = openSync(VM_LOG, 'a');
|
|
72
|
+
writeFileSync(fd, `\n=== ${new Date().toISOString()} limactl ${args.join(' ')} ===\n`);
|
|
73
|
+
}
|
|
74
|
+
let result;
|
|
75
|
+
try {
|
|
76
|
+
result = spawnSync(limactlBinary(), args, {
|
|
77
|
+
input, stdio: inherit ? 'inherit' : [input === undefined ? 'ignore' : 'pipe', fd ?? 'pipe', fd ?? 'pipe'],
|
|
78
|
+
encoding: inherit || log ? undefined : 'utf8', maxBuffer: 64 * 1024 * 1024,
|
|
79
|
+
});
|
|
80
|
+
} finally { if (fd !== null) closeSync(fd); }
|
|
81
|
+
if (result.error) throw result.error;
|
|
82
|
+
if (check && result.status !== 0) {
|
|
83
|
+
const detail = log ? `${tail(VM_LOG)}\n(full log: ${VM_LOG})` : String(result.stderr ?? '').trim().split('\n').slice(-6).join('\n');
|
|
84
|
+
throw new Error(`limactl ${args[0]} failed${detail ? `:\n${detail}` : ''}`);
|
|
85
|
+
}
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** A command in the VM as root: projects.py and setup.py run there. */
|
|
90
|
+
const inVm = (args, options) => limactl(['shell', '--workdir', '/', VM, 'sudo', ...args], options);
|
|
91
|
+
const projects = (args, options) => inVm(['python3', '/opt/raytace/projects.py', ...args], options);
|
|
92
|
+
|
|
93
|
+
/** Removes the RayTrace VM and every local sandbox in it; false when there was none. */
|
|
94
|
+
export function deleteVm() {
|
|
95
|
+
if (!findLima()?.binary || !vmStatus()) return false;
|
|
96
|
+
limactl(['delete', '--force', VM], { log: true });
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function vmStatus() {
|
|
101
|
+
const listed = limactl(['list', '--format', '{{.Name}} {{.Status}}']).stdout;
|
|
102
|
+
const line = listed.split('\n').find((row) => row.split(' ')[0] === VM);
|
|
103
|
+
return line ? line.split(' ')[1] : null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function setupVersion() {
|
|
107
|
+
const hash = createHash('sha256');
|
|
108
|
+
for (const name of readdirSync(SOURCE).sort()) hash.update(name).update(readFileSync(join(SOURCE, name)));
|
|
109
|
+
return hash.digest('hex').slice(0, 16);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The VM up, with gVisor, the collector and the sandbox image as this CLI ships them. */
|
|
113
|
+
function ensureVm() {
|
|
114
|
+
const status = vmStatus();
|
|
115
|
+
if (!status) {
|
|
116
|
+
say(`Creating the RayTrace VM "${VM}" (first time only: it downloads Ubuntu, a few minutes)...`);
|
|
117
|
+
limactl(['start', '--name', VM, '--tty=false', join(SOURCE, 'lima.yaml')], { log: true });
|
|
118
|
+
} else if (status !== 'Running') {
|
|
119
|
+
say(`Starting the RayTrace VM "${VM}"...`);
|
|
120
|
+
limactl(['start', '--tty=false', VM], { log: true });
|
|
121
|
+
}
|
|
122
|
+
const version = setupVersion();
|
|
123
|
+
const installed = inVm(['cat', SETUP_MARK], { check: false }).stdout?.trim();
|
|
124
|
+
if (installed === version) return;
|
|
125
|
+
say(`Installing gVisor, the evidence collector and the sandbox image in the VM (a few minutes; progress in ${VM_LOG})...`);
|
|
126
|
+
inVm(['rm', '-rf', '/tmp/raytrace-sandbox-source']);
|
|
127
|
+
limactl(['shell', '--workdir', '/', VM, 'mkdir', '-p', '/tmp/raytrace-sandbox-source']);
|
|
128
|
+
limactl(['copy', '-r', `${SOURCE}/.`, `${VM}:/tmp/raytrace-sandbox-source/`]);
|
|
129
|
+
inVm(['python3', '/tmp/raytrace-sandbox-source/setup.py'], { log: true });
|
|
130
|
+
inVm(['sh', '-c', `echo ${version} > ${SETUP_MARK}`]);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Transcripts reach the dashboard through the proxy's sandbox bridge. */
|
|
134
|
+
async function ensureBridge() {
|
|
135
|
+
try { await startProxy(); } catch (error) {
|
|
136
|
+
say(`Note: the RayTrace proxy did not start (${error.message.split('\n')[0]}). `
|
|
137
|
+
+ 'Commands are still recorded in the VM; Claude Code transcripts reach the dashboard once `raytrace start` works.');
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
// A proxy from before local sandboxes is reused as it is, and has no bridge.
|
|
141
|
+
if (!(await portInUse(Number(readConfig().RAYTRACE_BRIDGE_PORT || 8799)))) {
|
|
142
|
+
say('Note: the running RayTrace proxy has no sandbox bridge (it predates local sandboxes). '
|
|
143
|
+
+ 'Restart it so Claude Code sessions reach the dashboard: raytrace stop && raytrace start');
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Before anything is packed or started: what this machine lacks for a local
|
|
149
|
+
* sandbox, each with its fix, and Lima downloaded if the user agrees.
|
|
150
|
+
*/
|
|
151
|
+
export async function preflight(flags, { confirm, interactive }) {
|
|
152
|
+
let vmRunning = false;
|
|
153
|
+
try { vmRunning = Boolean(findLima()?.binary) && vmStatus() === 'Running'; } catch { /* reported below */ }
|
|
154
|
+
const rows = await checks({ vmRunning });
|
|
155
|
+
for (const { ok, label, detail } of rows) if (ok === null && label !== 'lima') say(`Note (${label}): ${detail}`);
|
|
156
|
+
const failed = rows.filter((row) => row.ok === false);
|
|
157
|
+
if (failed.length) {
|
|
158
|
+
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')}`);
|
|
159
|
+
}
|
|
160
|
+
if (findLima()?.binary) return;
|
|
161
|
+
if (!canDownload()) throw new Error('Local sandboxes need Lima: https://lima-vm.io/docs/installation/');
|
|
162
|
+
const question = `Local sandboxes run in a Linux VM made with Lima (open source, Apache-2.0), which is not installed. `
|
|
163
|
+
+ `Download Lima ${LIMA_VERSION} (about 25-40 MB) into ${join(paths.home, 'lima')}? No admin rights needed.`;
|
|
164
|
+
if (!flags.yes) {
|
|
165
|
+
if (!interactive()) throw new Error(`${question}\nRun again with --yes to download it, or install Lima yourself: https://lima-vm.io/docs/installation/`);
|
|
166
|
+
if (!(await confirm(question, true))) throw new Error('Not downloaded. Install Lima yourself to use local sandboxes: https://lima-vm.io/docs/installation/');
|
|
167
|
+
}
|
|
168
|
+
lima = (await downloadLima(say)).binary;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** A local sandbox's id, from its id or name; with neither, the only one there is. */
|
|
172
|
+
function pickId(ref) {
|
|
173
|
+
const boxes = registry();
|
|
174
|
+
if (ref) {
|
|
175
|
+
const id = boxes[ref] ? ref : byName(boxes, ref);
|
|
176
|
+
if (!id) throw new Error(`No local sandbox named ${ref}: raytrace sandbox list --local`);
|
|
177
|
+
return id;
|
|
178
|
+
}
|
|
179
|
+
const ids = Object.keys(boxes);
|
|
180
|
+
if (ids.length === 1) return ids[0];
|
|
181
|
+
throw new Error(ids.length ? 'More than one local sandbox; name one (raytrace sandbox list --local).'
|
|
182
|
+
: 'No local sandboxes yet: raytrace sandbox create --local');
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** The new sandbox's name: --name, or the repository folder's; unique among local sandboxes. */
|
|
186
|
+
export function sandboxName(flags, root) {
|
|
187
|
+
const name = flags.name ?? basename(root).replace(/[^A-Za-z0-9._-]+/g, '-').replace(/^[^A-Za-z0-9]+/, '').slice(0, 64);
|
|
188
|
+
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.`);
|
|
189
|
+
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}`);
|
|
190
|
+
return name;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export async function create(name, { root, files, skipped, total, archive }) {
|
|
194
|
+
ensureVm();
|
|
195
|
+
const id = `rtp-${randomBytes(16).toString('hex')}`;
|
|
196
|
+
const token = randomBytes(32).toString('base64url');
|
|
197
|
+
say(`Copying ${files.length} files (${(total / 1048576).toFixed(1)} MiB) from ${root} into a new sandbox`);
|
|
198
|
+
if (skipped.length) say(`Left out (${skipped.length}): ${skipped.slice(0, 8).join(', ')}${skipped.length > 8 ? ', ...' : ''}`);
|
|
199
|
+
projects(['create', id, '--archive', '-'], { input: archive });
|
|
200
|
+
// Recorded before anything else can fail, so destroy can always find it.
|
|
201
|
+
save({ ...registry(), [id]: { name, root, token, created: new Date().toISOString() } });
|
|
202
|
+
projects(['start', id]);
|
|
203
|
+
projects(['enable-claude', id], { input: readFileSync(HOOK) });
|
|
204
|
+
projects(['set-token', id], { input: token });
|
|
205
|
+
await ensureBridge();
|
|
206
|
+
say(`\nLocal sandbox ${name} (${id}) is running.`);
|
|
207
|
+
say('Claude Code sessions in it go to your local dashboard; commands, files and connections are recorded in the VM.');
|
|
208
|
+
say(`Open it: raytrace connect ${name}`);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export async function list() {
|
|
212
|
+
const boxes = registry();
|
|
213
|
+
if (!Object.keys(boxes).length) { say('No local sandboxes. Create one from a Git repository: raytrace sandbox create --local'); return; }
|
|
214
|
+
const running = vmStatus() === 'Running';
|
|
215
|
+
const states = running ? Object.fromEntries(JSON.parse(projects(['list']).stdout || '[]').map((box) => [box.id, box.status])) : {};
|
|
216
|
+
for (const [id, box] of Object.entries(boxes)) {
|
|
217
|
+
const age = Math.round((Date.now() - Date.parse(box.created)) / 60000);
|
|
218
|
+
const status = running ? (states[id] === 'running' ? 'running' : states[id] ? 'stopped' : 'missing') : 'stopped';
|
|
219
|
+
say(`${box.name.padEnd(20)} ${status.padEnd(8)} ${id} (${age < 120 ? `${age} min` : `${Math.round(age / 60)} h`} old, local)`);
|
|
220
|
+
}
|
|
221
|
+
if (!running) say(`\nThe RayTrace VM is stopped; \`raytrace sandbox start <name>\` starts it.`);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export async function start(id) {
|
|
225
|
+
id = pickId(id);
|
|
226
|
+
ensureVm();
|
|
227
|
+
projects(['start', id]);
|
|
228
|
+
await ensureBridge();
|
|
229
|
+
say(`${registry()[id].name} running`);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export async function stop(id) {
|
|
233
|
+
id = pickId(id);
|
|
234
|
+
if (vmStatus() === 'Running') projects(['stop', id]);
|
|
235
|
+
say(`${registry()[id].name} stopped; its files are kept`);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const stamp = () => new Date().toISOString().replace(/[-:]/g, '').replace('T', '-').slice(0, 15);
|
|
239
|
+
|
|
240
|
+
/** The sandbox's Claude Code session ids, from the bridge's transcript copies. */
|
|
241
|
+
function sessionIds(id) {
|
|
242
|
+
const listed = (folder) => { try { return readdirSync(folder).filter((file) => file.endsWith('.jsonl')).map((file) => file.slice(0, -6)); } catch { return []; } };
|
|
243
|
+
return [...listed(transcripts(id)), ...listed(earlierTranscripts(id))];
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Deletes a local sandbox, and asks about the logs captured in it: deleted
|
|
248
|
+
* with it, or moved to ~/.raytrace/archive/<name>-<time>/ (the Claude Code
|
|
249
|
+
* transcripts and the VM's command, file and network evidence), staying in
|
|
250
|
+
* the dashboard. --delete-logs / --keep-logs answer without asking; without a
|
|
251
|
+
* terminal and neither flag, the logs are kept.
|
|
252
|
+
*/
|
|
253
|
+
export async function destroy(ref, flags, { ask, interactive }) {
|
|
254
|
+
const id = pickId(ref);
|
|
255
|
+
const box = registry()[id];
|
|
256
|
+
if (!flags.yes) {
|
|
257
|
+
if (!interactive()) throw new Error('Pass --yes to destroy without a prompt.');
|
|
258
|
+
say(`This deletes local sandbox ${box.name}: its files, and any sign-in made inside it.`);
|
|
259
|
+
if ((await ask('Type the sandbox name to confirm: ')) !== box.name) { say('Not destroyed.'); return; }
|
|
260
|
+
}
|
|
261
|
+
let deleteLogs = flags['delete-logs'] ? true : flags['keep-logs'] ? false : null;
|
|
262
|
+
if (deleteLogs === null) {
|
|
263
|
+
deleteLogs = interactive()
|
|
264
|
+
&& /^y(es)?$/i.test((await ask('Also delete the logs captured in this sandbox? (yes/no) [no]: ')).trim());
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
ensureVm();
|
|
268
|
+
const inVmNow = JSON.parse(projects(['list']).stdout || '[]');
|
|
269
|
+
const exists = inVmNow.some((item) => item.id === id);
|
|
270
|
+
// Its Playground forks, which are sandboxes of their own in the VM.
|
|
271
|
+
const forks = inVmNow.filter((item) => item.fork_of === id).map((item) => item.id);
|
|
272
|
+
// Stopped first: stopping is recorded too, and must not land after the logs are dealt with.
|
|
273
|
+
if (exists) projects(['stop', id]);
|
|
274
|
+
if (deleteLogs) {
|
|
275
|
+
// Its sessions are filed under its id; ones recorded before that, under their own.
|
|
276
|
+
const { openEvidenceStore } = await import('../proxy/evidence-store.mjs');
|
|
277
|
+
const store = openEvidenceStore(paths.database);
|
|
278
|
+
try { store.forgetSessions([id, ...sessionIds(id)]); } finally { store.close(); }
|
|
279
|
+
rmSync(transcripts(id), { recursive: true, force: true });
|
|
280
|
+
rmSync(join(paths.home, 'sandboxes', id), { recursive: true, force: true });
|
|
281
|
+
} else {
|
|
282
|
+
const folder = join(ARCHIVE, `${box.name}-${stamp()}`);
|
|
283
|
+
mkdirSync(folder, { recursive: true, mode: 0o700 });
|
|
284
|
+
const { token, ...about } = box;
|
|
285
|
+
writeFileSync(join(folder, 'sandbox.json'), `${JSON.stringify({ id, ...about, destroyed: new Date().toISOString() }, null, 2)}\n`);
|
|
286
|
+
if (exists) writeFileSync(join(folder, 'runtime-evidence.jsonl'), projects(['evidence', id]).stdout);
|
|
287
|
+
for (const from of [transcripts(id), earlierTranscripts(id)]) {
|
|
288
|
+
if (existsSync(from)) { renameSync(from, join(folder, 'claude-code')); break; }
|
|
289
|
+
}
|
|
290
|
+
// Forks' transcripts are filed under their own ids; their recorded
|
|
291
|
+
// commands are already in the dashboard, with the forks themselves.
|
|
292
|
+
for (const fork of forks) {
|
|
293
|
+
if (!existsSync(transcripts(fork))) continue;
|
|
294
|
+
mkdirSync(join(folder, 'forks'), { recursive: true, mode: 0o700 });
|
|
295
|
+
renameSync(transcripts(fork), join(folder, 'forks', fork));
|
|
296
|
+
}
|
|
297
|
+
rmSync(join(paths.home, 'sandboxes', id), { recursive: true, force: true });
|
|
298
|
+
say(`Logs moved to ${folder}`);
|
|
299
|
+
say('Its Claude Code sessions stay in your dashboard.');
|
|
300
|
+
}
|
|
301
|
+
// The VM's record of it goes either way: deleted, or by now in the archive.
|
|
302
|
+
for (const fork of forks) projects(['fork-delete', fork, '--forget-evidence']);
|
|
303
|
+
if (deleteLogs) for (const fork of forks) rmSync(transcripts(fork), { recursive: true, force: true });
|
|
304
|
+
if (exists) projects(['delete', id, '--confirm', id, '--forget-evidence']);
|
|
305
|
+
const boxes = registry();
|
|
306
|
+
delete boxes[id];
|
|
307
|
+
save(boxes);
|
|
308
|
+
say(`${box.name} destroyed${forks.length ? `, with its ${forks.length} Playground fork${forks.length === 1 ? '' : 's'}` : ''}${deleteLogs ? ', and its logs' : ''}.`);
|
|
309
|
+
// The proxy keeps the Playground's forks in memory: one this CLI started is
|
|
310
|
+
// restarted, so the forks just deleted are not listed any more.
|
|
311
|
+
if (deleteLogs && forks.length && runningPid()) { await stopProxy(); await startProxy(); }
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** A terminal in the sandbox, through the VM. Leaving it keeps the sandbox running. */
|
|
315
|
+
export async function shell(id) {
|
|
316
|
+
id = pickId(id);
|
|
317
|
+
ensureVm();
|
|
318
|
+
await ensureBridge();
|
|
319
|
+
const { name } = registry()[id];
|
|
320
|
+
say(`Connected to ${name}. Run \`claude\` (or codex) here; exit to leave. The sandbox keeps running.`);
|
|
321
|
+
say('Recorded commands, files and connections: http://localhost:8798\n');
|
|
322
|
+
projects(['shell', id], { inherit: true, check: false });
|
|
323
|
+
say(`\nLeft ${name}. It is still running. Back in: raytrace connect ${name} Stop it: raytrace sandbox stop ${name}`);
|
|
324
|
+
}
|
package/src/proxy-process.mjs
CHANGED
|
@@ -10,6 +10,18 @@ import { setTimeout as sleep } from 'node:timers/promises';
|
|
|
10
10
|
import { paths } from './paths.mjs';
|
|
11
11
|
import { portOf, proxyEnv, readConfig } from './config.mjs';
|
|
12
12
|
|
|
13
|
+
// The proxy API this CLI's dashboard needs (proxy/raytace-proxy.mjs PROXY_API).
|
|
14
|
+
// A proxy from an older install answers without it, or with less.
|
|
15
|
+
export const PROXY_API = 2;
|
|
16
|
+
|
|
17
|
+
/** The running proxy's API version: 0 for one from before it said. */
|
|
18
|
+
export async function proxyApi(port) {
|
|
19
|
+
try {
|
|
20
|
+
const response = await fetch(`http://127.0.0.1:${port}/raytace/version`, { signal: AbortSignal.timeout(2000) });
|
|
21
|
+
return response.ok ? Number((await response.json()).api) || 0 : 0;
|
|
22
|
+
} catch { return 0; }
|
|
23
|
+
}
|
|
24
|
+
|
|
13
25
|
export async function healthy(port) {
|
|
14
26
|
try {
|
|
15
27
|
const response = await fetch(`http://127.0.0.1:${port}/raytace/models`, { signal: AbortSignal.timeout(2000) });
|
|
@@ -58,7 +70,14 @@ function tail(file, lines = 8) {
|
|
|
58
70
|
export async function start() {
|
|
59
71
|
const config = readConfig();
|
|
60
72
|
const port = portOf(config);
|
|
61
|
-
if (await healthy(port))
|
|
73
|
+
if (await healthy(port)) {
|
|
74
|
+
if (await proxyApi(port) >= PROXY_API) return { started: false, port, pid: runningPid() };
|
|
75
|
+
// An older proxy, still running from before an upgrade: the dashboard it
|
|
76
|
+
// serves now is the new one, and it would answer it "Unknown local endpoint".
|
|
77
|
+
if (!runningPid()) throw new Error(`An older RayTrace proxy, not started by this CLI, is running on :${port}. Stop it where it was started, then run: raytrace start`);
|
|
78
|
+
await stop();
|
|
79
|
+
return { ...(await start()), replaced: true };
|
|
80
|
+
}
|
|
62
81
|
if (await portInUse(port)) throw new Error(`Port ${port} is in use by something that is not answering as RayTrace. Free it, or set RAYTACE_PORT in ${paths.config}.`);
|
|
63
82
|
if (!existsSync(paths.dashboard)) throw new Error(`The dashboard is missing from ${paths.dashboard}. Reinstall @raytrace/cli.`);
|
|
64
83
|
|
package/src/sandbox.mjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// `raytrace sandbox`: cloud sandboxes, through RayTrace's API
|
|
2
|
-
// (https://api.raytracer.si; RAYTRACE_API_URL points elsewhere)
|
|
2
|
+
// (https://api.raytracer.si; RAYTRACE_API_URL points elsewhere), or with
|
|
3
|
+
// --local on this machine (local-sandbox.mjs). A local sandbox's id is
|
|
4
|
+
// recognised wherever an id is given, so connect/stop/destroy need no flag.
|
|
3
5
|
//
|
|
4
6
|
// The project is packed here, so files that must stay on this machine never
|
|
5
7
|
// leave it, then uploaded once; RayTrace creates an isolated sandbox from it
|
|
@@ -13,7 +15,8 @@ import { request as httpsRequest } from 'node:https';
|
|
|
13
15
|
import { basename, join } from 'node:path';
|
|
14
16
|
import { accessToken, account, login } from './auth.mjs';
|
|
15
17
|
import { readConfig } from './config.mjs';
|
|
16
|
-
import { ask, interactive } from './prompt.mjs';
|
|
18
|
+
import { ask, confirm, interactive } from './prompt.mjs';
|
|
19
|
+
import * as local from './local-sandbox.mjs';
|
|
17
20
|
|
|
18
21
|
const say = (line = '') => console.log(line);
|
|
19
22
|
// What one project upload may be. RayTrace's API holds the dashboard's folder
|
|
@@ -125,8 +128,13 @@ export async function create(flags) {
|
|
|
125
128
|
if (top.status !== 0) throw new Error('Run this from inside a Git repository.');
|
|
126
129
|
const root = top.stdout.trim();
|
|
127
130
|
const name = flags.name || basename(root);
|
|
131
|
+
// What the machine lacks is said before the project is read, and Lima fetched if agreed.
|
|
132
|
+
const localName = flags.local ? local.sandboxName(flags, root) : null;
|
|
133
|
+
if (flags.local) await local.preflight(flags, { confirm, interactive });
|
|
128
134
|
const { files, skipped, total } = projectFiles(root);
|
|
129
135
|
if (!files.length) throw new Error('Nothing to upload.');
|
|
136
|
+
// The same files, packed the same way, as a cloud sandbox gets: only the copy stays here.
|
|
137
|
+
if (flags.local) return local.create(localName, { root, files, skipped, total, archive: await pack(root, files) });
|
|
130
138
|
say(`Packing ${files.length} files (${(total / 1048576).toFixed(1)} MiB) from ${root}`);
|
|
131
139
|
if (skipped.length) say(`Not uploaded (${skipped.length}): ${skipped.slice(0, 8).join(', ')}${skipped.length > 8 ? ', ...' : ''}`);
|
|
132
140
|
const archive = await pack(root, files);
|
|
@@ -141,7 +149,8 @@ export async function create(flags) {
|
|
|
141
149
|
say(`Open it: raytrace connect ${box.id}`);
|
|
142
150
|
}
|
|
143
151
|
|
|
144
|
-
export async function list() {
|
|
152
|
+
export async function list(flags = {}) {
|
|
153
|
+
if (flags.local) return local.list();
|
|
145
154
|
const boxes = (await call('GET', '/v1/sandboxes')).filter((box) => box.status !== 'deleted');
|
|
146
155
|
if (!boxes.length) { say('No sandboxes. Create one from a Git repository: raytrace sandbox create'); return; }
|
|
147
156
|
for (const box of boxes) {
|
|
@@ -152,10 +161,17 @@ export async function list() {
|
|
|
152
161
|
}
|
|
153
162
|
}
|
|
154
163
|
|
|
155
|
-
export async function start(id
|
|
156
|
-
|
|
164
|
+
export async function start(id, flags = {}) {
|
|
165
|
+
if (flags.local || local.isLocal(id)) return local.start(id);
|
|
166
|
+
const box = await call('POST', `/v1/sandboxes/${await pickId(id)}/start`); say(`${box.id} running`);
|
|
167
|
+
}
|
|
168
|
+
export async function stop(id, flags = {}) {
|
|
169
|
+
if (flags.local || local.isLocal(id)) return local.stop(id);
|
|
170
|
+
const box = await call('POST', `/v1/sandboxes/${await pickId(id)}/stop`); say(`${box.id} stopped; its files are kept`);
|
|
171
|
+
}
|
|
157
172
|
|
|
158
173
|
export async function destroy(id, flags) {
|
|
174
|
+
if (flags.local || local.isLocal(id)) return local.destroy(id, flags, { ask, interactive });
|
|
159
175
|
id = await pickId(id);
|
|
160
176
|
if (!flags.yes) {
|
|
161
177
|
if (!interactive()) throw new Error('Pass --yes to destroy without a prompt.');
|
|
@@ -168,8 +184,10 @@ export async function destroy(id, flags) {
|
|
|
168
184
|
|
|
169
185
|
/** An interactive terminal in the sandbox, carried over one upgraded HTTP request.
|
|
170
186
|
* `raytrace connect <id>` and `raytrace sandbox shell [id]` are the same thing. */
|
|
171
|
-
export async function shell(id) {
|
|
187
|
+
export async function shell(id, flags = {}) {
|
|
172
188
|
if (!interactive()) throw new Error('Connecting to a sandbox needs a terminal.');
|
|
189
|
+
// With no id and no cloud sign-in, the one local sandbox is the likely one.
|
|
190
|
+
if (flags.local || local.isLocal(id) || (!id && Object.keys(local.registry()).length && !account())) return local.shell(id);
|
|
173
191
|
id = await pickId(id);
|
|
174
192
|
const { url, key } = await api();
|
|
175
193
|
const { stdin, stdout } = process;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
FROM node:22-bookworm-slim
|
|
2
|
+
RUN apt-get update && apt-get install -y --no-install-recommends git ca-certificates python3 curl ripgrep \
|
|
3
|
+
&& rm -rf /var/lib/apt/lists/*
|
|
4
|
+
RUN npm install -g @openai/codex @anthropic-ai/claude-code
|
|
5
|
+
RUN mkdir /workspace && chown node:node /workspace
|
|
6
|
+
USER node
|
|
7
|
+
WORKDIR /workspace
|
|
8
|
+
ENV HOME=/home/node CODEX_HOME=/home/node/.codex
|