raytrace-cli 0.11.1__py3-none-win_amd64.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,439 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Playground forks: rerun a sandboxed Claude Code session from one step, with
|
|
3
|
+
* that step's context or files changed, and compare where it ends up.
|
|
4
|
+
*
|
|
5
|
+
* A fork is its own sandbox (worker/sandbox/projects.py fork-*), so every path
|
|
6
|
+
* in the session means what it meant originally and the project's real
|
|
7
|
+
* workspace is out of reach. It starts from the snapshot the project folder
|
|
8
|
+
* was at just before the forked step (workspace_snapshots), resumes Claude
|
|
9
|
+
* Code from the session cut at that step (forkTranscript), and is compared
|
|
10
|
+
* file by file with the original run's final snapshot. With a check command
|
|
11
|
+
* (`npm test`, a script), that command then runs where the fork left the
|
|
12
|
+
* project, and its exit code is the fork's pass or fail.
|
|
13
|
+
*
|
|
14
|
+
* Models do not answer the same way twice, so one change can be run several
|
|
15
|
+
* times: a batch is that many forks sharing a `group`, queued and run one
|
|
16
|
+
* after another (one fork runs at a time), each from the same snapshot.
|
|
17
|
+
* A changed batch can bring a baseline: the same step run unchanged as many
|
|
18
|
+
* times, with the same check, interleaved with it so both see the same
|
|
19
|
+
* conditions. An unchanged batch already run that often is reused instead.
|
|
20
|
+
*
|
|
21
|
+
* Lifecycle: queued -> create -> run -> collect -> check (the fork's snapshot history moves into
|
|
22
|
+
* the project's own, so its files outlive it) -> stop. The newest fork is
|
|
23
|
+
* kept, stopped, for a day to look around in; any older one is deleted
|
|
24
|
+
* unless kept on purpose. Fork records are lab_experiments rows (kind fork).
|
|
25
|
+
*/
|
|
26
|
+
import { spawn } from 'node:child_process';
|
|
27
|
+
import { limactl, VM } from './local-sandboxes.mjs';
|
|
28
|
+
import { randomBytes } from 'node:crypto';
|
|
29
|
+
import { readdir, readFile } from 'node:fs/promises';
|
|
30
|
+
import { join } from 'node:path';
|
|
31
|
+
import { forkTranscript } from './claude-code-adapter.mjs';
|
|
32
|
+
|
|
33
|
+
export const FORK_ID = /^rtf-[a-f0-9]{32}$/;
|
|
34
|
+
const PROJECT_ID = /^rtp-[a-f0-9]{32}$/;
|
|
35
|
+
// What the resumed session is asked: `claude -p` needs a prompt, so the
|
|
36
|
+
// forked step is one short user message away from the original request.
|
|
37
|
+
export const RESUME_PROMPT = 'Continue the task from where you left off.';
|
|
38
|
+
const KEEP_LATEST_MS = 24 * 3_600_000;
|
|
39
|
+
const DEFAULT_MINUTES = 10;
|
|
40
|
+
// The check has a limit of its own, apart from Claude Code's: a test suite
|
|
41
|
+
// that hangs is a failed check, not a fork stuck forever.
|
|
42
|
+
const CHECK_SECONDS = 10 * 60;
|
|
43
|
+
export const MAX_REPEAT = 10;
|
|
44
|
+
const MAX_EVENTS = 400;
|
|
45
|
+
|
|
46
|
+
/** The command that runs one projects.py action in the local sandbox VM. */
|
|
47
|
+
export function workerCommand(action, id) {
|
|
48
|
+
const args = [action, ...(id ? [id] : [])];
|
|
49
|
+
return [limactl(), ['shell', '--workdir', '/', VM, 'sudo', 'python3', '/opt/raytace/projects.py', ...args]];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Runs one projects.py action in the VM with a JSON stdin; resolves with its
|
|
53
|
+
* exit code and output. `onLine` sees stdout line by line as it arrives. */
|
|
54
|
+
export function vmAction(action, id, input, { onLine = null, signal = null } = {}) {
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
const child = spawn(...workerCommand(action, id), { stdio: ['pipe', 'pipe', 'pipe'] });
|
|
57
|
+
let stdout = ''; let stderr = ''; let pending = '';
|
|
58
|
+
child.stdout.on('data', (chunk) => {
|
|
59
|
+
const text = chunk.toString();
|
|
60
|
+
if (!onLine) { stdout += text; return; }
|
|
61
|
+
pending += text;
|
|
62
|
+
const lines = pending.split('\n'); pending = lines.pop();
|
|
63
|
+
for (const line of lines) if (line.trim()) onLine(line);
|
|
64
|
+
});
|
|
65
|
+
child.stderr.on('data', (chunk) => { stderr = (stderr + chunk.toString()).slice(-4000); });
|
|
66
|
+
child.on('error', reject);
|
|
67
|
+
child.on('close', (code) => { if (onLine && pending.trim()) onLine(pending); resolve({ code, stdout, stderr }); });
|
|
68
|
+
signal?.addEventListener('abort', () => child.kill('SIGTERM'), { once: true });
|
|
69
|
+
child.stdin.on('error', () => {});
|
|
70
|
+
child.stdin.end(input == null ? '' : JSON.stringify(input));
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function vmJson(action, id, input) {
|
|
75
|
+
const { code, stdout, stderr } = await vmAction(action, id, input);
|
|
76
|
+
if (code !== 0) throw new Error(`${action} failed: ${(stderr.trim().split('\n').at(-1) || `exit ${code}`).slice(0, 300)}`);
|
|
77
|
+
return JSON.parse(stdout.trim().split('\n').at(-1) || '{}');
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** The snapshot the project folder was at just before a step: after the last
|
|
81
|
+
* call proposed before it, or when its prompt was sent. Snapshots after a
|
|
82
|
+
* later call never qualify, whatever their time.
|
|
83
|
+
*
|
|
84
|
+
* With neither (a session whose prompt snapshot never arrived), the first
|
|
85
|
+
* snapshot after the step still knows the state just before its own call:
|
|
86
|
+
* its commit when that call changed nothing, else its parent. Only used when
|
|
87
|
+
* that first snapshot belongs to this step or later, so nothing that ran
|
|
88
|
+
* before the step is skipped. */
|
|
89
|
+
export function snapshotBefore(snapshots, { priorCalls, startedAt }) {
|
|
90
|
+
const prior = new Set(priorCalls);
|
|
91
|
+
const started = Date.parse(startedAt ?? '');
|
|
92
|
+
const eligible = snapshots.filter((snap) => (snap.call_id && prior.has(snap.call_id))
|
|
93
|
+
|| (snap.taken_on === 'prompt' && Number.isFinite(started) && snap.taken_at_ms <= started + 5_000));
|
|
94
|
+
if (eligible.length) return eligible.at(-1);
|
|
95
|
+
const first = snapshots[0];
|
|
96
|
+
if (!first || (first.call_id && prior.has(first.call_id))) return null;
|
|
97
|
+
const commit = first.changed ? first.parent : first.commit;
|
|
98
|
+
return commit ? { ...first, commit, taken_on: 'before-first-call' } : null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Claude Code's stream-json events -> the progress a fork shows while running. */
|
|
102
|
+
export function applyRunEvent(job, event) {
|
|
103
|
+
if (event?.type === 'assistant') {
|
|
104
|
+
for (const block of event.message?.content ?? []) {
|
|
105
|
+
if (block?.type === 'tool_use') {
|
|
106
|
+
job.progress.tool_calls += 1;
|
|
107
|
+
const input = block.input ?? {};
|
|
108
|
+
job.events.push({ at: new Date().toISOString(), kind: 'tool', name: block.name,
|
|
109
|
+
detail: String(input.command ?? input.file_path ?? input.pattern ?? input.url ?? JSON.stringify(input)).slice(0, 300) });
|
|
110
|
+
} else if (block?.type === 'text' && block.text?.trim()) {
|
|
111
|
+
job.events.push({ at: new Date().toISOString(), kind: 'text', detail: block.text.trim().slice(0, 600) });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
} else if (event?.type === 'result') {
|
|
115
|
+
job.result = { text: String(event.result ?? '').slice(0, 20_000), is_error: event.is_error === true, turns: event.num_turns ?? null,
|
|
116
|
+
cost_usd: event.total_cost_usd ?? null, duration_ms: event.duration_ms ?? null };
|
|
117
|
+
}
|
|
118
|
+
if (job.events.length > MAX_EVENTS) job.events.splice(0, job.events.length - MAX_EVENTS);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** `db` keeps the fork records and reads the snapshots. The hosted evidence
|
|
122
|
+
* service runs one Playground per organisation over a shared sandbox host,
|
|
123
|
+
* and passes what that needs: `records` (the organisation's forks, already
|
|
124
|
+
* loaded), `ownsSandbox` (which sandboxes' sessions it may fork), `exclusive`
|
|
125
|
+
* (runs one fork at a time across every Playground on the host), tighter
|
|
126
|
+
* `maxRepeat` and `maxMinutes`, and `orphans: false` (other organisations'
|
|
127
|
+
* forks on the host are not this one's to report). */
|
|
128
|
+
export function startPlayground({ db, transcriptsDir, hookSource, log = console.log, records = null, ownsSandbox = null, exclusive = null,
|
|
129
|
+
maxRepeat = MAX_REPEAT, maxMinutes = 30, orphans = true }) {
|
|
130
|
+
const forks = new Map((records ?? db.loadExperiments(500)).filter((job) => job.kind === 'fork').map((job) => [job.id, job]));
|
|
131
|
+
const controllers = new Map();
|
|
132
|
+
// Forks of a batch waiting their turn, with what they are run from. Only
|
|
133
|
+
// in memory: a restart interrupts them (recover), like a running one.
|
|
134
|
+
const queue = [];
|
|
135
|
+
const save = (job) => { job.updated_at = new Date().toISOString(); db.saveExperiment(job); };
|
|
136
|
+
|
|
137
|
+
/** Which sandboxed session a recorded step belongs to, cut at that step. */
|
|
138
|
+
async function locate(exchangeId, results) {
|
|
139
|
+
for (const sandbox of (await readdir(transcriptsDir).catch(() => [])).filter((name) => PROJECT_ID.test(name))) {
|
|
140
|
+
if (ownsSandbox && !(await ownsSandbox(sandbox))) continue;
|
|
141
|
+
for (const file of (await readdir(join(transcriptsDir, sandbox))).filter((name) => name.endsWith('.jsonl'))) {
|
|
142
|
+
const cut = forkTranscript(await readFile(join(transcriptsDir, sandbox, file), 'utf8'), { spanId: exchangeId, results });
|
|
143
|
+
if (cut) return { sandbox, ...cut };
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** What forking a step involves, before running anything: whether it can
|
|
150
|
+
* be forked (and why not), the tool results the model had seen by then
|
|
151
|
+
* (what can be edited), the model, and the snapshot it would start from. */
|
|
152
|
+
async function preview(exchangeId) {
|
|
153
|
+
const found = await locate(exchangeId, {});
|
|
154
|
+
if (!found) return { forkable: false, reason: 'Only steps of a Claude Code session run in the sandbox can be forked.' };
|
|
155
|
+
const snapshots = await db.snapshotsForAgentSession({ sessionId: found.sandbox, agentSessionId: found.session });
|
|
156
|
+
const start = snapshotBefore(snapshots, found);
|
|
157
|
+
const calls = new Map();
|
|
158
|
+
for (const line of found.text.split('\n')) {
|
|
159
|
+
let entry; try { entry = JSON.parse(line); } catch { continue; }
|
|
160
|
+
if (entry.isSidechain) continue;
|
|
161
|
+
for (const block of Array.isArray(entry.message?.content) ? entry.message.content : []) {
|
|
162
|
+
if (entry.type === 'assistant' && block?.type === 'tool_use') {
|
|
163
|
+
const input = block.input ?? {};
|
|
164
|
+
calls.set(block.id, { call_id: block.id, name: block.name, detail: String(input.command ?? input.file_path ?? input.pattern ?? input.url ?? JSON.stringify(input)).slice(0, 400), output: null });
|
|
165
|
+
} else if (entry.type === 'user' && block?.type === 'tool_result' && calls.has(block.tool_use_id)) {
|
|
166
|
+
const content = block.content;
|
|
167
|
+
calls.get(block.tool_use_id).output = typeof content === 'string' ? content
|
|
168
|
+
: Array.isArray(content) ? content.map((part) => (part?.type === 'text' ? part.text : part?.type === 'image' ? '[image]' : '')).join('\n') : '';
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const LIMIT = 100_000;
|
|
173
|
+
return { forkable: Boolean(start), reason: start ? null : 'This run was recorded before snapshots existed, so the project cannot be put back as it was.',
|
|
174
|
+
sandbox: found.sandbox, session: found.session, model: found.model, start_snapshot: start?.commit ?? null,
|
|
175
|
+
original_final: snapshots.at(-1)?.commit ?? null, resume_prompt: RESUME_PROMPT,
|
|
176
|
+
results: [...calls.values()].filter((call) => call.output != null).map((call) => ({ ...call,
|
|
177
|
+
chars: call.output.length, truncated: call.output.length > LIMIT, output: call.output.slice(0, LIMIT) })) };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Read-only snapshot views (projects.py snapshot): list | show | diff. */
|
|
181
|
+
function snapshot(project, request) {
|
|
182
|
+
if (!PROJECT_ID.test(project)) return Promise.reject(new Error('Invalid project sandbox.'));
|
|
183
|
+
return vmJson('snapshot', project, request);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async function create(config) {
|
|
187
|
+
const results = config.results && typeof config.results === 'object' ? config.results : {};
|
|
188
|
+
const files = config.files && typeof config.files === 'object' ? config.files : {};
|
|
189
|
+
const workspacePath = (path) => typeof path === 'string' && path && !path.startsWith('/') && !path.split('/').includes('..') && path.length <= 1024;
|
|
190
|
+
for (const [path, text] of Object.entries(files)) {
|
|
191
|
+
if (typeof text !== 'string' || !workspacePath(path)) throw new Error(`Invalid file edit: ${path}`);
|
|
192
|
+
}
|
|
193
|
+
// Excluded files are taken out of the fork's project (their earlier
|
|
194
|
+
// outputs arrive as `results` edits); a message replaces the neutral
|
|
195
|
+
// resume prompt as what the model is told at the forked step.
|
|
196
|
+
const exclude = Array.isArray(config.exclude) ? config.exclude : [];
|
|
197
|
+
if (exclude.length > 200 || !exclude.every(workspacePath)) throw new Error('Invalid excluded file.');
|
|
198
|
+
const message = typeof config.message === 'string' ? config.message.trim() : '';
|
|
199
|
+
if (message.length > 4000) throw new Error('The message must be under 4000 characters.');
|
|
200
|
+
const command = typeof config.check === 'string' ? config.check.trim() : '';
|
|
201
|
+
if (command.length > 2000) throw new Error('The check command must be under 2000 characters.');
|
|
202
|
+
if (controllers.size || queue.length) throw new Error('A fork is already running. Wait for it or delete it first.');
|
|
203
|
+
if ([...forks.values()].some((job) => job.recovery_stop_pending && !job.deleted_at)) {
|
|
204
|
+
throw new Error('A previous fork could not be stopped after a restart. Retry recovery or delete that fork before starting another.');
|
|
205
|
+
}
|
|
206
|
+
const repeat = Math.min(maxRepeat, Math.max(1, Math.trunc(Number(config.repeat)) || 1));
|
|
207
|
+
const found = await locate(config.exchange_id, results);
|
|
208
|
+
if (!found) throw new Error('That step is not from a sandboxed Claude Code session, so it cannot be forked.');
|
|
209
|
+
if (found.missing.length) throw new Error(`No result to edit for call(s) before this step: ${found.missing.join(', ')}`);
|
|
210
|
+
const snapshots = await db.snapshotsForAgentSession({ sessionId: found.sandbox, agentSessionId: found.session });
|
|
211
|
+
const start = snapshotBefore(snapshots, found);
|
|
212
|
+
if (!start) throw new Error('No snapshot of the project before this step: the run was recorded before snapshots existed.');
|
|
213
|
+
const minutes = Math.min(maxMinutes, Math.max(1, Number(config.minutes) || DEFAULT_MINUTES));
|
|
214
|
+
const model = typeof config.model === 'string' && config.model ? config.model : found.model;
|
|
215
|
+
const unchanged = !Object.keys(results).length && !Object.keys(files).length && !exclude.length && !message && model === found.model;
|
|
216
|
+
const created_at = new Date().toISOString();
|
|
217
|
+
const batch = ({ transcript, prompt, edits, model: batchModel, unchanged: isUnchanged, baseline = null }) => {
|
|
218
|
+
const group = `rtg-${randomBytes(16).toString('hex')}`;
|
|
219
|
+
return Array.from({ length: repeat }, (_, index) => ({ id: `rtf-${randomBytes(16).toString('hex')}`, kind: 'fork',
|
|
220
|
+
status: 'queued', created_at, group, attempt: index + 1, attempts: repeat, unchanged: isUnchanged, baseline, exchange_id: config.exchange_id,
|
|
221
|
+
parent: found.sandbox, session: found.session, model: batchModel, prompt: prompt || RESUME_PROMPT, message: prompt || null, minutes,
|
|
222
|
+
start_snapshot: start.commit, original_final: snapshots.at(-1)?.commit ?? start.commit, edits, keep: false,
|
|
223
|
+
progress: { tool_calls: 0 }, events: [], result: null, comparison: null, error: null,
|
|
224
|
+
check: command ? { command, status: 'pending', exit_code: null, output: '', duration_ms: null } : null, _input: { transcript } }));
|
|
225
|
+
};
|
|
226
|
+
let base = [];
|
|
227
|
+
let baseline = null;
|
|
228
|
+
if (config.baseline === true && !unchanged) {
|
|
229
|
+
baseline = reusableBaseline({ exchangeId: config.exchange_id, model: found.model, command, runs: repeat });
|
|
230
|
+
if (!baseline) {
|
|
231
|
+
// The unchanged transcript: no tool result edited.
|
|
232
|
+
const plain = await locate(config.exchange_id, {});
|
|
233
|
+
base = batch({ transcript: plain.text, prompt: '', edits: { results: [], files: [], excluded: [] }, model: found.model, unchanged: true });
|
|
234
|
+
baseline = base[0].group;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const jobs = batch({ transcript: found.text, prompt: message, edits: { results: Object.keys(results), files: Object.keys(files), excluded: exclude },
|
|
238
|
+
model, unchanged, baseline });
|
|
239
|
+
// Change and baseline alternate, so neither gets all the early (or late) runs.
|
|
240
|
+
for (const [index, job] of jobs.entries()) for (const item of [job, base[index]].filter(Boolean)) {
|
|
241
|
+
const { _input, ...record } = item;
|
|
242
|
+
forks.set(record.id, record); save(record);
|
|
243
|
+
queue.push({ job: record, input: { ..._input, files: record.unchanged ? {} : files, exclude: record.unchanged ? [] : exclude } });
|
|
244
|
+
}
|
|
245
|
+
next();
|
|
246
|
+
return forks.get(jobs[0].id);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** An unchanged batch from this step, with this model and check, that
|
|
250
|
+
* already has at least `runs` finished runs: a baseline to compare with. */
|
|
251
|
+
function reusableBaseline({ exchangeId, model, command, runs }) {
|
|
252
|
+
const groups = new Map();
|
|
253
|
+
for (const job of forks.values()) {
|
|
254
|
+
if (!job.unchanged || !job.group || job.exchange_id !== exchangeId || job.model !== model || (job.check?.command ?? '') !== command) continue;
|
|
255
|
+
if (['completed', 'failed'].includes(job.status)) groups.set(job.group, (groups.get(job.group) ?? 0) + 1);
|
|
256
|
+
}
|
|
257
|
+
let best = null;
|
|
258
|
+
for (const [group, count] of groups) if (count >= runs && (!best || count > best.count)) best = { group, count };
|
|
259
|
+
return best?.group ?? null;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** Starts the next queued fork, once none is running. */
|
|
263
|
+
function next() {
|
|
264
|
+
if (controllers.size || !queue.length) return;
|
|
265
|
+
const { job, input } = queue.shift();
|
|
266
|
+
job.status = 'creating'; save(job);
|
|
267
|
+
const controller = new AbortController();
|
|
268
|
+
controllers.set(job.id, controller);
|
|
269
|
+
// On a shared host the fork waits its turn; one deleted meanwhile never starts.
|
|
270
|
+
const start = () => (controller.signal.aborted ? undefined : run(job, input, controller));
|
|
271
|
+
void Promise.resolve(exclusive ? exclusive(start) : start()).catch(() => {}).finally(() => { controllers.delete(job.id); next(); });
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async function run(job, { transcript, files, exclude }, controller) {
|
|
275
|
+
const deadline = setTimeout(() => { job.timed_out = true; controller.abort(); }, job.minutes * 60_000);
|
|
276
|
+
try {
|
|
277
|
+
await vmJson('fork-create', job.id, { parent: job.parent, commit: job.start_snapshot, session: job.session, transcript, files, delete: exclude, hook: hookSource() });
|
|
278
|
+
job.status = 'running'; save(job);
|
|
279
|
+
log(`[raytace][playground] ${job.id.slice(0, 12)} forked ${job.parent.slice(0, 12)} at snapshot ${job.start_snapshot.slice(0, 8)}; running ${job.model}`);
|
|
280
|
+
let lastSave = 0;
|
|
281
|
+
const { code, stderr } = await vmAction('fork-run', job.id, { session: job.session, model: job.model, prompt: job.prompt }, {
|
|
282
|
+
signal: controller.signal,
|
|
283
|
+
onLine: (line) => {
|
|
284
|
+
try { applyRunEvent(job, JSON.parse(line)); } catch { return; }
|
|
285
|
+
if (Date.now() - lastSave > 1500) { lastSave = Date.now(); save(job); }
|
|
286
|
+
},
|
|
287
|
+
});
|
|
288
|
+
clearTimeout(deadline);
|
|
289
|
+
if (code !== 0 && !job.result) job.error = job.timed_out ? `Stopped after ${job.minutes} minutes.` : (stderr.trim().split('\n').at(-1) || `Claude Code exited with ${code}`).slice(0, 300);
|
|
290
|
+
job.status = 'collecting'; save(job);
|
|
291
|
+
// The Stop hook's last transcript send runs as Claude Code exits.
|
|
292
|
+
await new Promise((resolve) => setTimeout(resolve, 3000));
|
|
293
|
+
const collected = await vmJson('fork-collect', job.id, { original: job.original_final });
|
|
294
|
+
job.comparison = { original: job.original_final, fork: collected.final, changes: collected.changes ?? [],
|
|
295
|
+
same_files: collected.final ? (collected.changes ?? []).length === 0 : null };
|
|
296
|
+
// Checked even when Claude Code failed or ran out of time: what it left
|
|
297
|
+
// behind is still what gets graded.
|
|
298
|
+
if (job.check && !(controller.signal.aborted && !job.timed_out)) {
|
|
299
|
+
// Claude Code's time limit aborted the run's controller; the check
|
|
300
|
+
// gets a fresh one, which deleting the fork still stops.
|
|
301
|
+
if (controller.signal.aborted) { controller = new AbortController(); controllers.set(job.id, controller); }
|
|
302
|
+
await check(job, controller);
|
|
303
|
+
}
|
|
304
|
+
job.status = job.error ? 'failed' : 'completed';
|
|
305
|
+
} catch (error) {
|
|
306
|
+
job.status = 'failed'; job.error = String(error.message ?? error).slice(0, 400);
|
|
307
|
+
} finally {
|
|
308
|
+
clearTimeout(deadline);
|
|
309
|
+
// Never reached the check: the fork could not start, or was deleted.
|
|
310
|
+
if (job.check?.status === 'pending') job.check.status = 'skipped';
|
|
311
|
+
save(job);
|
|
312
|
+
await vmJson('fork-stop', job.id, null).catch(() => {});
|
|
313
|
+
await retire(job.id);
|
|
314
|
+
log(`[raytace][playground] ${job.id.slice(0, 12)} ${job.status}${job.comparison ? `, ${job.comparison.changes.length} file(s) differ from the original` : ''}${job.check ? `, check ${job.check.status}` : ''}${job.error ? `: ${job.error}` : ''}`);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** Runs the fork's check command in its sandbox; exit 0 passes. */
|
|
319
|
+
async function check(job, controller) {
|
|
320
|
+
job.status = 'checking'; job.check.status = 'running'; save(job);
|
|
321
|
+
const started = Date.now();
|
|
322
|
+
try {
|
|
323
|
+
const { code, stdout, stderr } = await vmAction('fork-check', job.id, { command: job.check.command, seconds: CHECK_SECONDS }, { signal: controller.signal });
|
|
324
|
+
if (code !== 0) throw new Error((stderr.trim().split('\n').at(-1) || `exit ${code}`).slice(0, 300));
|
|
325
|
+
const outcome = JSON.parse(stdout.trim().split('\n').at(-1) || '{}');
|
|
326
|
+
Object.assign(job.check, { exit_code: outcome.exit_code, output: outcome.output ?? '',
|
|
327
|
+
status: outcome.timed_out ? 'timed_out' : outcome.exit_code === 0 ? 'passed' : 'failed' });
|
|
328
|
+
} catch (error) {
|
|
329
|
+
// The check could not run at all: neither a pass nor a fail.
|
|
330
|
+
Object.assign(job.check, { status: 'error', output: String(error.message ?? error).slice(0, 400) });
|
|
331
|
+
} finally {
|
|
332
|
+
job.check.duration_ms = Date.now() - started;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async function remove(id) {
|
|
337
|
+
const job = forks.get(id);
|
|
338
|
+
if (!job || !FORK_ID.test(id)) throw new Error('Fork not found.');
|
|
339
|
+
const waiting = queue.findIndex((item) => item.job.id === id);
|
|
340
|
+
if (waiting !== -1) {
|
|
341
|
+
// Never started: there is no sandbox to delete.
|
|
342
|
+
queue.splice(waiting, 1);
|
|
343
|
+
job.status = 'cancelled'; job.deleted_at = new Date().toISOString();
|
|
344
|
+
if (job.check) job.check.status = 'skipped';
|
|
345
|
+
save(job); return job;
|
|
346
|
+
}
|
|
347
|
+
controllers.get(id)?.abort();
|
|
348
|
+
await vmJson('fork-delete', id, null).catch((error) => { if (!/No such|not found|Invalid sandbox/i.test(error.message)) throw error; });
|
|
349
|
+
job.deleted_at = new Date().toISOString(); save(job);
|
|
350
|
+
return job;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** Stops a whole batch and a baseline started with it: the queued forks
|
|
354
|
+
* are cancelled, the running one deleted. */
|
|
355
|
+
async function stopGroup(id) {
|
|
356
|
+
const fork = forks.get(id);
|
|
357
|
+
if (!fork?.group) return remove(id);
|
|
358
|
+
// With its baseline, or the change a baseline was started with: stopping
|
|
359
|
+
// one half leaves nothing to compare the other with.
|
|
360
|
+
const groups = new Set([fork.group, fork.baseline].filter(Boolean));
|
|
361
|
+
for (const job of forks.values()) if (job.baseline === fork.group && (queue.some((entry) => entry.job === job) || controllers.has(job.id))) groups.add(job.group);
|
|
362
|
+
for (const item of queue.filter((entry) => groups.has(entry.job.group))) await remove(item.job.id);
|
|
363
|
+
for (const job of forks.values()) if (groups.has(job.group) && controllers.has(job.id)) await remove(job.id);
|
|
364
|
+
return forks.get(id);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** Keeps the newest fork (and any kept on purpose); deletes the rest, and
|
|
368
|
+
* the newest too once it is a day old. */
|
|
369
|
+
async function retire(newest = null) {
|
|
370
|
+
const now = Date.now();
|
|
371
|
+
for (const job of forks.values()) {
|
|
372
|
+
if (job.deleted_at || job.keep || job.recovery_stop_pending || job.status === 'queued' || controllers.has(job.id)) continue;
|
|
373
|
+
const stale = job.id !== newest && (newest || now - Date.parse(job.created_at) > KEEP_LATEST_MS);
|
|
374
|
+
if (stale) await remove(job.id).catch((error) => log(`[raytace][playground] could not delete ${job.id.slice(0, 12)}: ${error.message}`));
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** After a restart: a fork that was mid-run cannot be followed any more. */
|
|
379
|
+
async function recover() {
|
|
380
|
+
for (const job of forks.values()) {
|
|
381
|
+
if (!job.deleted_at && ['queued', 'creating', 'running', 'collecting', 'checking'].includes(job.status) && !controllers.has(job.id)) {
|
|
382
|
+
if (job.status !== 'queued') {
|
|
383
|
+
// The worker may have survived the service and its timeout controller.
|
|
384
|
+
// Retaining a recent or kept fork must retain its files, not its execution.
|
|
385
|
+
job.recovery_stop_pending = true; // block new work while the stop is in flight too
|
|
386
|
+
try {
|
|
387
|
+
await vmJson('fork-stop', job.id, null);
|
|
388
|
+
} catch (error) {
|
|
389
|
+
// A creating job may not have made its container. Confirm absence
|
|
390
|
+
// with a successful host inventory; an unreachable host proves nothing.
|
|
391
|
+
let absent = false;
|
|
392
|
+
try {
|
|
393
|
+
const inventory = await vmAction('list', null, null);
|
|
394
|
+
const boxes = inventory.code === 0 ? JSON.parse(inventory.stdout) : null;
|
|
395
|
+
absent = Array.isArray(boxes) && !boxes.some((box) => box.id === job.id);
|
|
396
|
+
} catch { /* keep the unknown state visible and retryable */ }
|
|
397
|
+
if (!absent) {
|
|
398
|
+
job.recovery_stop_pending = true;
|
|
399
|
+
job.error = `RayTrace restarted. Could not confirm this fork stopped: ${String(error.message ?? error).slice(0, 300)}. Restart RayTrace to retry recovery, or delete the fork.`;
|
|
400
|
+
save(job);
|
|
401
|
+
log(`[raytace][playground] ${job.id.slice(0, 12)} recovery stop failed: ${job.error}`);
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
delete job.recovery_stop_pending;
|
|
407
|
+
if (job.status === 'queued') job.deleted_at = new Date().toISOString(); // never had a sandbox
|
|
408
|
+
job.error = job.status === 'queued' ? 'RayTrace restarted before this run started.' : 'RayTrace restarted while this fork was running; its sandbox is stopped or no longer exists.';
|
|
409
|
+
job.status = 'interrupted';
|
|
410
|
+
if (job.check && ['pending', 'running'].includes(job.check.status)) job.check.status = 'error';
|
|
411
|
+
save(job);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
// Fork sandboxes this database has no record of: reported, never deleted,
|
|
415
|
+
// since another RayTrace (a test run, another checkout) may own them.
|
|
416
|
+
const { code, stdout } = orphans ? await vmAction('list', null, null).catch(() => ({ code: 1 })) : { code: 1 };
|
|
417
|
+
if (code === 0) {
|
|
418
|
+
for (const box of JSON.parse(stdout || '[]')) {
|
|
419
|
+
if (FORK_ID.test(box.id) && !forks.has(box.id)) {
|
|
420
|
+
log(`[raytace][playground] fork sandbox ${box.id} is not in this RayTrace's records; delete it with: `
|
|
421
|
+
+ workerCommand('fork-delete', box.id).flat().join(' '));
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
await retire();
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
return {
|
|
429
|
+
create, remove, stopGroup, recover, preview, snapshot,
|
|
430
|
+
get: (id) => forks.get(id) ?? null,
|
|
431
|
+
list: () => [...forks.values()].sort((a, b) => b.created_at.localeCompare(a.created_at) || (a.attempt ?? 1) - (b.attempt ?? 1)).slice(0, 100),
|
|
432
|
+
keep(id, value = true) {
|
|
433
|
+
const job = forks.get(id); if (!job) throw new Error('Fork not found.');
|
|
434
|
+
job.keep = value === true; save(job); return job;
|
|
435
|
+
},
|
|
436
|
+
/** The fork a sandboxed hook says it is, if it really forks that project. */
|
|
437
|
+
forkOf(forkId, parent) { const job = forks.get(forkId); return job && job.parent === parent && !job.deleted_at ? job : null; },
|
|
438
|
+
};
|
|
439
|
+
}
|