@raytrace-cli/cli 0.19.8 → 0.19.9
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 +5 -0
- package/bin/raytrace.mjs +13 -0
- package/package.json +1 -1
- package/src/github.mjs +78 -0
- package/src/sandbox.mjs +114 -0
package/README.md
CHANGED
|
@@ -266,6 +266,11 @@ The upload is the files Git would track (including new files it doesn't ignore),
|
|
|
266
266
|
files, private keys and common credential files. Check for secrets inside other
|
|
267
267
|
files before you upload.
|
|
268
268
|
|
|
269
|
+
To let the agent in it fetch and push your GitHub repository (to its own
|
|
270
|
+
`raytrace/...` branches only, with no GitHub key in the sandbox), use
|
|
271
|
+
`raytrace sandbox create --connect`; an org admin connects GitHub once first.
|
|
272
|
+
See [Connect GitHub](docs/cloud.md#connect-github).
|
|
273
|
+
|
|
269
274
|
Limits, beta invites, and what happens when you destroy a cloud sandbox are in
|
|
270
275
|
[docs/cloud.md](docs/cloud.md).
|
|
271
276
|
|
package/bin/raytrace.mjs
CHANGED
|
@@ -33,6 +33,14 @@ Cloud sandboxes:
|
|
|
33
33
|
raytrace sandbox create [--name <name>] [--size small|medium|large]
|
|
34
34
|
upload this Git repository to a new sandbox
|
|
35
35
|
(up to 200 MiB, 25 MiB a file, 20,000 files; default: small)
|
|
36
|
+
raytrace sandbox create --connect [--repo owner/name]
|
|
37
|
+
the same, then make it a clone of the repository's
|
|
38
|
+
GitHub origin (or --repo) that its agent can fetch
|
|
39
|
+
and push to raytrace/<id>/... branches; an org admin
|
|
40
|
+
connects GitHub on the account page first
|
|
41
|
+
raytrace sandbox repos [id] [--add owner/name] [--remove owner/name] [--connect]
|
|
42
|
+
the GitHub repositories its git may open; --connect
|
|
43
|
+
with one --add also makes /workspace a clone of it
|
|
36
44
|
raytrace connect [id] [--claude|--codex] a terminal in it; with one sandbox the id is
|
|
37
45
|
optional (same as: raytrace sandbox shell [id]);
|
|
38
46
|
--claude or --codex starts that agent in it at
|
|
@@ -221,6 +229,10 @@ if (command === 'sandbox') {
|
|
|
221
229
|
restrict: { type: 'string', multiple: true },
|
|
222
230
|
report: { type: 'string', multiple: true },
|
|
223
231
|
clear: { type: 'string', multiple: true },
|
|
232
|
+
connect: { type: 'boolean' },
|
|
233
|
+
repo: { type: 'string' },
|
|
234
|
+
add: { type: 'string', multiple: true },
|
|
235
|
+
remove: { type: 'string', multiple: true },
|
|
224
236
|
} });
|
|
225
237
|
} catch (error) { console.error(`${error.message}\n\nRun \`raytrace help\` for usage.`); process.exit(2); }
|
|
226
238
|
const [sub, id] = parsed.positionals;
|
|
@@ -236,6 +248,7 @@ if (command === 'sandbox') {
|
|
|
236
248
|
'keep-alive': () => sandbox.keepAlive(id, parsed.values),
|
|
237
249
|
destroy: () => sandbox.destroy(id, parsed.values),
|
|
238
250
|
files: () => sandbox.files(id, parsed.values),
|
|
251
|
+
repos: () => sandbox.repos(id, parsed.values),
|
|
239
252
|
};
|
|
240
253
|
const action = actions[sub];
|
|
241
254
|
if (!sub) { console.error(SANDBOX_HELP); process.exit(2); }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@raytrace-cli/cli",
|
|
3
|
-
"version": "0.19.
|
|
3
|
+
"version": "0.19.9",
|
|
4
4
|
"mcpName": "io.github.Godcreatebugs/raytrace",
|
|
5
5
|
"description": "See what your coding agent did: traces, tool calls, cost and step-by-step evidence for Claude Code and Codex.",
|
|
6
6
|
"license": "MIT",
|
package/src/github.mjs
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// The git connector, from the CLI's side: a cloud sandbox with GitHub
|
|
2
|
+
// repositories attached clones, fetches and pushes them through RayTrace,
|
|
3
|
+
// which opens them with the organization's RayTrace GitHub App; no GitHub
|
|
4
|
+
// key is ever in the sandbox. An org admin installs the App and connects it
|
|
5
|
+
// on the account page first.
|
|
6
|
+
//
|
|
7
|
+
// githubRepo, workBranch and connectScript are the SDKs' (sdk/ts/src/client.mjs,
|
|
8
|
+
// sdk/python): the CLI package ships without them, so they are repeated here,
|
|
9
|
+
// and test/github.test.mjs checks the script is still the same.
|
|
10
|
+
import { spawnSync } from 'node:child_process';
|
|
11
|
+
|
|
12
|
+
export const ACCOUNT_URL = 'https://raytracer.si/account.html';
|
|
13
|
+
|
|
14
|
+
// GitHub's rules for account and repository names (RayTrace's API checks the same).
|
|
15
|
+
const GITHUB_NAME = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,38})\/[A-Za-z0-9._-]{1,100}$/;
|
|
16
|
+
const GITHUB_URL = /^(?:https:\/\/github\.com\/|ssh:\/\/git@github\.com\/|git@github\.com:)?([^/\s]+\/[^/\s]+?)(?:\.git)?\/?$/i;
|
|
17
|
+
|
|
18
|
+
/** `owner/name` for a GitHub repository given as owner/name or a github.com https or ssh URL; null for anything else. */
|
|
19
|
+
export function githubRepo(repo) {
|
|
20
|
+
const name = GITHUB_URL.exec(String(repo ?? '').trim())?.[1];
|
|
21
|
+
if (!name || !GITHUB_NAME.test(name) || ['.', '..'].includes(name.split('/')[1])) return null;
|
|
22
|
+
return name;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The branch a connected sandbox works on. Its agent may push only to raytrace/<these 8 characters>/... */
|
|
26
|
+
export function workBranch(sandboxId) {
|
|
27
|
+
return `raytrace/${String(sandboxId).slice(4, 12)}/work`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The branches a sandbox's agent may push to. */
|
|
31
|
+
export const pushPrefix = (sandboxId) => `raytrace/${String(sandboxId).slice(4, 12)}/`;
|
|
32
|
+
|
|
33
|
+
const GIT_REF = /^(?!-)(?!.*\.\.)[A-Za-z0-9._/-]{1,200}$/;
|
|
34
|
+
const GIT_COMMIT = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
|
|
35
|
+
const quote = (value) => `'${String(value).replace(/'/g, `'\\''`)}'`;
|
|
36
|
+
|
|
37
|
+
/** The shell commands that make a sandbox's uploaded files a clone of `repo` on its work branch (see the SDKs' connectScript). */
|
|
38
|
+
export function connectScript({ repo, sandboxId, ref = undefined, commit = undefined, dir = '/workspace' }) {
|
|
39
|
+
const name = githubRepo(repo);
|
|
40
|
+
if (!name) throw new Error(`Not a GitHub repository: ${repo} (use owner/name)`);
|
|
41
|
+
if (ref !== undefined && !GIT_REF.test(String(ref))) throw new Error(`Not a branch or tag name: ${ref}`);
|
|
42
|
+
if (commit !== undefined && !GIT_COMMIT.test(String(commit))) throw new Error(`Not a commit id: ${commit}`);
|
|
43
|
+
const fallback = quote(ref ?? 'HEAD');
|
|
44
|
+
const branch = workBranch(sandboxId);
|
|
45
|
+
return [
|
|
46
|
+
'set -e',
|
|
47
|
+
`cd ${quote(dir)}`,
|
|
48
|
+
`if [ -e .git ]; then echo "RayTrace: $(pwd) is already a Git repository; left as it is." >&2; exit 0; fi`,
|
|
49
|
+
'git -c init.defaultBranch=main init -q',
|
|
50
|
+
`git remote add origin ${quote(`https://github.com/${name}.git`)}`,
|
|
51
|
+
commit ? `git fetch -q --depth 1 origin ${commit} 2>/dev/null || git fetch -q --depth 1 origin ${fallback}`
|
|
52
|
+
: `git fetch -q --depth 1 origin ${fallback}`,
|
|
53
|
+
'git reset -q FETCH_HEAD',
|
|
54
|
+
'if [ -n "$(git ls-files --deleted)" ]; then git ls-files -z --deleted | git checkout -q --pathspec-from-file=- --pathspec-file-nul; fi',
|
|
55
|
+
`git checkout -q -b ${quote(branch)}`,
|
|
56
|
+
`echo ${quote(`Connected to ${name} on branch ${branch}. Push with: git push -u origin ${branch}`)}`,
|
|
57
|
+
].join('\n');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const git = (root, ...args) => {
|
|
61
|
+
const done = spawnSync('git', ['-C', root, ...args], { encoding: 'utf8' });
|
|
62
|
+
return done.status === 0 ? done.stdout.trim() : null;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* What the repository at `root` says about connecting a sandbox made from it:
|
|
67
|
+
* `repo`, its origin's owner/name when that is GitHub (else null); `commit`,
|
|
68
|
+
* what it has checked out, which the sandbox lines up with when GitHub has it;
|
|
69
|
+
* and `ref`, the branch it is on, only when origin has that branch too (as
|
|
70
|
+
* this machine last saw it), else the sandbox falls back to GitHub's default branch.
|
|
71
|
+
*/
|
|
72
|
+
export function localRepo(root) {
|
|
73
|
+
const repo = githubRepo(git(root, 'remote', 'get-url', 'origin'));
|
|
74
|
+
const commit = git(root, 'rev-parse', '--verify', '-q', 'HEAD^{commit}');
|
|
75
|
+
const branch = git(root, 'symbolic-ref', '--short', '-q', 'HEAD');
|
|
76
|
+
const onOrigin = branch && GIT_REF.test(branch) && git(root, 'rev-parse', '--verify', '-q', `refs/remotes/origin/${branch}`) !== null;
|
|
77
|
+
return { repo, commit: GIT_COMMIT.test(commit ?? '') ? commit : undefined, ref: onOrigin ? branch : undefined };
|
|
78
|
+
}
|
package/src/sandbox.mjs
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
// login` token; RAYTRACE_API_KEY overrides it for a server running without
|
|
10
10
|
// accounts (development).
|
|
11
11
|
import { spawn, spawnSync } from 'node:child_process';
|
|
12
|
+
import { randomBytes } from 'node:crypto';
|
|
12
13
|
import { lstatSync } from 'node:fs';
|
|
13
14
|
import { request as httpRequest } from 'node:http';
|
|
14
15
|
import { request as httpsRequest } from 'node:https';
|
|
@@ -19,6 +20,7 @@ import { betaEnds, betaStatus, formatCreditDeadline } from './beta.mjs';
|
|
|
19
20
|
import { ask, confirm, interactive } from './prompt.mjs';
|
|
20
21
|
import * as local from './local-sandbox.mjs';
|
|
21
22
|
import { AGENTS } from './agents.mjs';
|
|
23
|
+
import { ACCOUNT_URL, connectScript, githubRepo, localRepo, pushPrefix } from './github.mjs';
|
|
22
24
|
|
|
23
25
|
const say = (line = '') => console.log(line);
|
|
24
26
|
// What one project upload may be. RayTrace's API holds the dashboard's folder
|
|
@@ -181,10 +183,109 @@ export function upload(archive, name, useCredit, size = null) {
|
|
|
181
183
|
/** The API refused the credit: 409 already used on another sandbox, 410 expired. */
|
|
182
184
|
export const isCreditRefusal = (error) => error instanceof ApiError && [409, 410].includes(error.status);
|
|
183
185
|
|
|
186
|
+
// ------------------------------------------------------- GitHub (src/github.mjs)
|
|
187
|
+
|
|
188
|
+
const JSON_HEADERS = { 'content-type': 'application/json' };
|
|
189
|
+
|
|
190
|
+
/** An API refusal about repositories, worded for this command line. */
|
|
191
|
+
function repoError(error) {
|
|
192
|
+
if (!(error instanceof ApiError)) return error;
|
|
193
|
+
if (error.status === 404 && error.message === 'Not found') return new Error('This RayTrace server does not support GitHub repositories yet.');
|
|
194
|
+
if (/account page/.test(error.message)) return new ApiError(`${error.message}\nAccount page: ${ACCOUNT_URL}`, { status: error.status, data: error.data });
|
|
195
|
+
return error;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async function attachedRepos(id) {
|
|
199
|
+
try { return (await call('GET', `/v1/sandboxes/${id}/repos`)).repos || []; } catch (error) { throw repoError(error); }
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async function setRepos(id, repos) {
|
|
203
|
+
try { return (await call('PUT', `/v1/sandboxes/${id}/repos`, { body: JSON.stringify({ repos }), headers: JSON_HEADERS })).repos || []; }
|
|
204
|
+
catch (error) { throw repoError(error); }
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const COMMAND_DONE = new Set(['completed', 'cancelled', 'timed_out', 'interrupted']);
|
|
208
|
+
const sleep = (ms) => new Promise((done) => setTimeout(done, ms));
|
|
209
|
+
|
|
210
|
+
/** Runs `command` in a cloud sandbox and waits for its result (the command API the SDKs' exec uses). */
|
|
211
|
+
export async function runInSandbox(id, command, { timeoutSeconds = 300, pollMs = 1000 } = {}) {
|
|
212
|
+
const body = JSON.stringify({ command, timeout_seconds: timeoutSeconds, request_id: `req_${randomBytes(12).toString('hex')}` });
|
|
213
|
+
let job = await call('POST', `/v1/sandboxes/${id}/commands`, { body, headers: JSON_HEADERS });
|
|
214
|
+
// An interrupted command whose host could not be reached may still be running: its answer is not final yet.
|
|
215
|
+
while (!COMMAND_DONE.has(job.state) || job.recovery_pending) {
|
|
216
|
+
await sleep(pollMs);
|
|
217
|
+
job = await call('GET', `/v1/sandboxes/${id}/commands/${job.command_id}`);
|
|
218
|
+
}
|
|
219
|
+
return job;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** The commit and branch to line a sandbox up with, when it is connected to the repository this command runs in. */
|
|
223
|
+
function hereFor(repo) {
|
|
224
|
+
const root = repoRoot();
|
|
225
|
+
const here = root ? localRepo(root) : {};
|
|
226
|
+
return here.repo && here.repo.toLowerCase() === repo.toLowerCase() ? { commit: here.commit, ref: here.ref } : {};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Attaches `repo` to a cloud sandbox (keeping what is attached), then makes its /workspace a clone of it.
|
|
230
|
+
* Says what was done: "/workspace ... is a clone of ... on branch ...", or why it was left as it was. */
|
|
231
|
+
export async function connectRepo(id, repo, { ref, commit } = {}) {
|
|
232
|
+
const script = connectScript({ repo, sandboxId: id, ref, commit });
|
|
233
|
+
const attached = await attachedRepos(id);
|
|
234
|
+
if (!attached.some((other) => other.toLowerCase() === repo.toLowerCase())) await setRepos(id, [...attached, repo]);
|
|
235
|
+
const job = await runInSandbox(id, script);
|
|
236
|
+
if (job.exit_code !== 0) throw new Error(String(job.stderr || job.stdout || `the setup command ended ${job.state}`).trim());
|
|
237
|
+
const branch = String(job.stdout ?? '').match(/on branch (\S+)\./)?.[1];
|
|
238
|
+
return branch ? `/workspace in ${id} is a clone of ${repo} on branch ${branch}.`
|
|
239
|
+
: `${repo} is attached to ${id}. ${String(job.stderr ?? '').trim().replace(/^RayTrace: /, '')}`.trim();
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const pushRule = (id) => `Agents in it push only to ${pushPrefix(id)}<branch>; any other branch, main included, is refused.`;
|
|
243
|
+
|
|
244
|
+
/** `raytrace sandbox repos [id] [--add owner/name ...] [--remove owner/name ...] [--connect]` */
|
|
245
|
+
export async function repos(id, flags = {}) {
|
|
246
|
+
if (flags.local || (id && local.isLocal(id))) throw new Error('GitHub repositories attach to cloud sandboxes only.');
|
|
247
|
+
const names = (given = []) => given.map((repo) => githubRepo(repo) ?? (() => { throw new Error(`Not a GitHub repository: ${repo} (use owner/name)`); })());
|
|
248
|
+
const add = names(flags.add), remove = names(flags.remove);
|
|
249
|
+
if (flags.connect && (add.length !== 1 || remove.length)) {
|
|
250
|
+
throw new Error('--connect makes /workspace a clone of one repository: give it exactly one --add owner/name.');
|
|
251
|
+
}
|
|
252
|
+
const targetId = await pickId(id);
|
|
253
|
+
let list;
|
|
254
|
+
if (flags.connect) {
|
|
255
|
+
say(await connectRepo(targetId, add[0], hereFor(add[0])));
|
|
256
|
+
list = await attachedRepos(targetId);
|
|
257
|
+
} else if (add.length || remove.length) {
|
|
258
|
+
const same = (a) => (b) => a.toLowerCase() === b.toLowerCase();
|
|
259
|
+
const current = await attachedRepos(targetId);
|
|
260
|
+
list = await setRepos(targetId, [...current.filter((repo) => !remove.some(same(repo))), ...add.filter((repo) => !current.some(same(repo)))]);
|
|
261
|
+
} else {
|
|
262
|
+
list = await attachedRepos(targetId);
|
|
263
|
+
}
|
|
264
|
+
if (!list.length) {
|
|
265
|
+
say(`No GitHub repositories attached to ${targetId}. Attach one: raytrace sandbox repos ${targetId} --add owner/name`);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
say(`GitHub repositories ${targetId} may open:`);
|
|
269
|
+
for (const repo of list) say(` ${repo}`);
|
|
270
|
+
say(pushRule(targetId));
|
|
271
|
+
}
|
|
272
|
+
|
|
184
273
|
export async function create(flags) {
|
|
274
|
+
if (flags.repo && !flags.connect) throw new Error('--repo names the GitHub repository to connect to: use it with --connect.');
|
|
275
|
+
if (flags.connect && flags.local) throw new Error('GitHub repositories connect to cloud sandboxes only.');
|
|
185
276
|
const root = repoRoot();
|
|
186
277
|
if (!root) throw new Error('Run this from inside a Git repository.');
|
|
187
278
|
const name = flags.name || basename(root);
|
|
279
|
+
// Which GitHub repository to connect to, worked out before anything is uploaded.
|
|
280
|
+
let connecting = null;
|
|
281
|
+
if (flags.connect) {
|
|
282
|
+
const repo = flags.repo ? githubRepo(flags.repo) : localRepo(root).repo;
|
|
283
|
+
if (!repo) {
|
|
284
|
+
throw new Error(flags.repo ? `Not a GitHub repository: ${flags.repo} (use owner/name)`
|
|
285
|
+
: "This repository's origin is not on GitHub. Name the one to connect to: --repo owner/name");
|
|
286
|
+
}
|
|
287
|
+
connecting = { repo, ...hereFor(repo) };
|
|
288
|
+
}
|
|
188
289
|
const requestedSize = flags.local ? null : validateSize(flags.size);
|
|
189
290
|
// What the machine lacks is said before the project is read, and Lima fetched if agreed.
|
|
190
291
|
const localName = flags.local ? local.sandboxName(flags, root) : null;
|
|
@@ -230,8 +331,21 @@ export async function create(flags) {
|
|
|
230
331
|
}
|
|
231
332
|
say(box.transcripts ? 'Claude Code sessions in it are recorded.'
|
|
232
333
|
: `Claude Code transcripts are off: ${box.transcripts_off}. Commands are still recorded.`);
|
|
334
|
+
let notConnected = null;
|
|
335
|
+
if (connecting) {
|
|
336
|
+
say(`Connecting it to GitHub: ${connecting.repo}...`);
|
|
337
|
+
try {
|
|
338
|
+
say(`${await connectRepo(box.id, connecting.repo, connecting)} ${pushRule(box.id)}`);
|
|
339
|
+
} catch (error) {
|
|
340
|
+
notConnected = error;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
233
343
|
say('The first time, Claude Code (or Codex) asks you to sign in inside the sandbox: use your own account.');
|
|
234
344
|
say(`Open it: raytrace connect ${box.id}`);
|
|
345
|
+
if (notConnected) {
|
|
346
|
+
throw new Error(`${box.id} was created, but not connected to GitHub: ${notConnected.message}\n`
|
|
347
|
+
+ `Try again: raytrace sandbox repos ${box.id} --add ${connecting.repo} --connect`);
|
|
348
|
+
}
|
|
235
349
|
}
|
|
236
350
|
|
|
237
351
|
export async function list(flags = {}) {
|