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,364 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Claude Code hook inside a RayTrace sandbox. projects.py enable-claude
|
|
4
|
+
* installs it root-owned with a managed-settings file, so the agent can
|
|
5
|
+
* neither edit nor turn it off.
|
|
6
|
+
*
|
|
7
|
+
* PreToolUse (Bash): starts the command with `export RAYTRACE_CALL_ID=<the
|
|
8
|
+
* tool call's id>`. The shell carries the id in its argv and every program it
|
|
9
|
+
* starts carries it in its environment, where the gVisor collector reads it
|
|
10
|
+
* (collector.py MARKER_KEYS): the evidence joins the call exactly instead of
|
|
11
|
+
* by command text.
|
|
12
|
+
*
|
|
13
|
+
* PreToolUse (a file tool): refuses an edit to a file the project's owner
|
|
14
|
+
* restricted (the list projects.py guardrails() writes beside this script),
|
|
15
|
+
* saying why. From the sandbox's next start the file is locked on the host
|
|
16
|
+
* as well, which is what stops every other way of writing it.
|
|
17
|
+
*
|
|
18
|
+
* An attempt on a file the owner restricted or watches (an edit refused, a
|
|
19
|
+
* read, a command naming it) is queued as one line in /tmp/raytace-claude and
|
|
20
|
+
* goes to RayTrace as `events` with the next send: nothing is sent from here
|
|
21
|
+
* before a tool runs, so a refusal is as fast as before. The owner is told.
|
|
22
|
+
*
|
|
23
|
+
* PostToolUse (a tool that can change files) and UserPromptSubmit: snapshots
|
|
24
|
+
* the project folder, so the lab can later put it back exactly as it was at
|
|
25
|
+
* any step. See snapshot() below.
|
|
26
|
+
*
|
|
27
|
+
* Every other event, and those two after their snapshot: sends the
|
|
28
|
+
* transcript's new whole lines, and the snapshot taken, to RayTrace through
|
|
29
|
+
* the RayTrace CLI's sandbox bridge on the host -- the only host service this
|
|
30
|
+
* sandbox can reach -- with the sandbox's token. How far it got is kept in /tmp; if that is lost,
|
|
31
|
+
* RayTrace answers 409 with the size of its copy and sending resumes from
|
|
32
|
+
* there.
|
|
33
|
+
*
|
|
34
|
+
* Never blocks or fails Claude Code: RayTrace being down means no capture.
|
|
35
|
+
*/
|
|
36
|
+
import { readFileSync, writeFileSync, appendFileSync, mkdirSync, rmdirSync, statSync, lstatSync, readdirSync, readlinkSync, existsSync, openSync, readSync, fstatSync, closeSync } from 'node:fs';
|
|
37
|
+
import { execFileSync } from 'node:child_process';
|
|
38
|
+
import { join, resolve } from 'node:path';
|
|
39
|
+
|
|
40
|
+
// The RayTrace CLI's sandbox bridge on the host, as projects.py set it on this
|
|
41
|
+
// sandbox (Lima's address for the host when unset).
|
|
42
|
+
const BROKER = `${process.env.RAYTRACE_BROKER || 'http://192.168.5.2:8799'}/raytace/ingest/claude-code`;
|
|
43
|
+
const CHUNK = 512 * 1024; // even worst-case JSON escaping stays under 4 MiB
|
|
44
|
+
const home = process.env.HOME || '/home/node';
|
|
45
|
+
|
|
46
|
+
let input;
|
|
47
|
+
try { input = JSON.parse(readFileSync(0, 'utf8')); } catch { process.exit(0); }
|
|
48
|
+
|
|
49
|
+
const workspace = process.env.RAYTRACE_WORKSPACE || '/workspace';
|
|
50
|
+
const STATE_DIR = '/tmp/raytace-claude';
|
|
51
|
+
const MAX_QUEUED_BYTES = 128 * 1024;
|
|
52
|
+
|
|
53
|
+
// The owner's lists (projects.py guardrails()): `restricted` is refused,
|
|
54
|
+
// `watched` is reported. Nothing listed when the file is absent or unreadable.
|
|
55
|
+
const listed = (value) => (Array.isArray(value) ? value.filter((file) => typeof file === 'string' && file) : []);
|
|
56
|
+
let restricted = [];
|
|
57
|
+
let watched = [];
|
|
58
|
+
try {
|
|
59
|
+
const lists = JSON.parse(readFileSync(new URL('./guardrails.json', import.meta.url), 'utf8'));
|
|
60
|
+
restricted = listed(lists.restricted);
|
|
61
|
+
watched = listed(lists.watched);
|
|
62
|
+
} catch { /* nothing is restricted or watched */ }
|
|
63
|
+
|
|
64
|
+
/** A file the tool named, relative to the project; null when it is outside it. */
|
|
65
|
+
const inside = (named) => {
|
|
66
|
+
if (typeof named !== 'string' || !named) return null;
|
|
67
|
+
const path = resolve(input.cwd || workspace, named);
|
|
68
|
+
return path.startsWith(`${workspace}/`) ? path.slice(workspace.length + 1) : null;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** One line for the next send; never throws and never waits on the network. */
|
|
72
|
+
function queue(event) {
|
|
73
|
+
try {
|
|
74
|
+
if (!/^[0-9a-f-]{36}$/.test(input.session_id ?? '')) return;
|
|
75
|
+
mkdirSync(STATE_DIR, { recursive: true, mode: 0o700 });
|
|
76
|
+
const file = join(STATE_DIR, `${input.session_id}.events.jsonl`);
|
|
77
|
+
try { if (statSync(file).size > MAX_QUEUED_BYTES) return; } catch { /* none yet */ }
|
|
78
|
+
appendFileSync(file, `${JSON.stringify({ at: new Date().toISOString(), call_id: input.tool_use_id ?? null, tool: input.tool_name, ...event })}\n`, { mode: 0o600 });
|
|
79
|
+
} catch { /* never disturb Claude Code */ }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (input.hook_event_name === 'PreToolUse') {
|
|
83
|
+
const named = input.tool_input?.file_path ?? input.tool_input?.notebook_path;
|
|
84
|
+
if (input.tool_name !== 'Bash' && typeof named === 'string') {
|
|
85
|
+
const path = inside(named);
|
|
86
|
+
if (path && restricted.includes(path)) {
|
|
87
|
+
queue({ kind: 'blocked', path });
|
|
88
|
+
process.stdout.write(JSON.stringify({ hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: 'deny',
|
|
89
|
+
permissionDecisionReason: `RayTrace: ${path} is restricted by this project's owner and cannot be changed. `
|
|
90
|
+
+ 'Do not try another way to write it; tell the user it has to be unlocked under Initial scan in the RayTrace dashboard.' } }));
|
|
91
|
+
}
|
|
92
|
+
process.exit(0);
|
|
93
|
+
}
|
|
94
|
+
const id = input.tool_use_id;
|
|
95
|
+
const command = input.tool_input?.command;
|
|
96
|
+
// A command that names a restricted file is reported, not stopped: the text
|
|
97
|
+
// says what it mentions, not what it does. The file lock is what stops it.
|
|
98
|
+
if (input.tool_name === 'Bash' && typeof command === 'string') {
|
|
99
|
+
for (const path of restricted.slice(0, 200)) if (command.includes(path)) queue({ kind: 'named', path });
|
|
100
|
+
}
|
|
101
|
+
if (input.tool_name === 'Bash' && /^[A-Za-z0-9_-]{1,128}$/.test(id ?? '') && typeof command === 'string') {
|
|
102
|
+
// An input change needs a decision with it. Only a session that already
|
|
103
|
+
// skips permission prompts gets "allow"; any other still asks as usual,
|
|
104
|
+
// unless running in headless/CI agent mode (CI, GitHub Actions, or RAYTRACE_ALLOW_ALL/RAYTRACE_HEADLESS=1).
|
|
105
|
+
const isCI = process.env.CI === 'true' || process.env.CI === '1' || Boolean(process.env.GITHUB_ACTIONS);
|
|
106
|
+
const allowAll = isCI || process.env.RAYTRACE_ALLOW_ALL === '1' || process.env.RAYTRACE_ALLOW_ALL === 'true'
|
|
107
|
+
|| process.env.RAYTRACE_HEADLESS === '1' || process.env.HEADLESS === '1';
|
|
108
|
+
const isBypass = input.permission_mode === 'bypassPermissions' || input.permission_mode === 'dontAsk';
|
|
109
|
+
const decision = (isBypass || allowAll) ? 'allow' : 'ask';
|
|
110
|
+
process.stdout.write(JSON.stringify({ hookSpecificOutput: { hookEventName: 'PreToolUse', permissionDecision: decision,
|
|
111
|
+
updatedInput: { ...input.tool_input, command: `export RAYTRACE_CALL_ID=${id}\n${command}` } } }));
|
|
112
|
+
}
|
|
113
|
+
process.exit(0);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ---------------------------------------------------------------- snapshots
|
|
117
|
+
// A separate git history of /workspace, kept in the sandbox's home: never the
|
|
118
|
+
// project's own git, and blind to the project's .gitignore (-f), so ignored
|
|
119
|
+
// and never-committed files are captured too. Only SKIPPED folders (large,
|
|
120
|
+
// regenerable) and files over MAX_FILE are left out; those are listed on the
|
|
121
|
+
// snapshot so a restore can say it is not exact. Unchanged content is stored
|
|
122
|
+
// once, so a step that changed nothing costs nothing and is not committed.
|
|
123
|
+
// It sits beside the agent, not above it: the agent could alter it, so it is
|
|
124
|
+
// a lab tool, not evidence.
|
|
125
|
+
const WORKSPACE = process.env.RAYTRACE_WORKSPACE || '/workspace';
|
|
126
|
+
const STORE = join(home, '.raytace', 'snapshots.git');
|
|
127
|
+
const SKIPPED = ['node_modules', '.git', '.next', '.vinext', 'dist', 'build', 'out', 'coverage', '.cache', '.turbo',
|
|
128
|
+
'.venv', 'venv', '__pycache__', '.pytest_cache', '.mypy_cache', 'target', '.gradle', '.wrangler'];
|
|
129
|
+
const MAX_FILE = 5 * 1024 * 1024;
|
|
130
|
+
const MAX_LISTED = 300;
|
|
131
|
+
// Tools that only read. Anything else, including tools this list has never
|
|
132
|
+
// heard of, is assumed able to change files.
|
|
133
|
+
const READ_ONLY = new Set(['Read', 'Grep', 'Glob', 'LS', 'WebFetch', 'WebSearch', 'TodoWrite', 'TodoRead', 'ToolSearch',
|
|
134
|
+
'ListMcpResourcesTool', 'ReadMcpResourceTool', 'BashOutput', 'KillShell', 'ExitPlanMode', 'AskUserQuestion']);
|
|
135
|
+
|
|
136
|
+
/** Folders below /workspace with their own .git (a clone, or `git init` in
|
|
137
|
+
* a subfolder), outermost only. */
|
|
138
|
+
function nestedRepositories() {
|
|
139
|
+
const found = [];
|
|
140
|
+
const walk = (relative) => {
|
|
141
|
+
for (const entry of readdirSync(relative ? join(WORKSPACE, relative) : WORKSPACE, { withFileTypes: true })) {
|
|
142
|
+
if (!entry.isDirectory() || SKIPPED.includes(entry.name)) continue;
|
|
143
|
+
const path = relative ? `${relative}/${entry.name}` : entry.name;
|
|
144
|
+
if (existsSync(join(WORKSPACE, path, '.git'))) found.push(path); else walk(path);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
walk('');
|
|
148
|
+
return found;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* `git add` records a nested repository as a pointer to it, not its files,
|
|
153
|
+
* and refuses the whole add while it has no commit yet, so nested ones are
|
|
154
|
+
* kept out of the add and their files written into the snapshot directly:
|
|
155
|
+
* re-read in full each time (no change detection, so a very large nested
|
|
156
|
+
* repository makes snapshots slow). Returns the paths left out for size.
|
|
157
|
+
*/
|
|
158
|
+
function addNestedRepositories(git, nested) {
|
|
159
|
+
const tooBig = [];
|
|
160
|
+
// A folder whose names, sizes and times match last time is left as the
|
|
161
|
+
// index already has it: re-reading it costs several git calls per snapshot.
|
|
162
|
+
const seenFile = join(STORE, 'raytrace-nested.json');
|
|
163
|
+
let seen = {};
|
|
164
|
+
try { seen = JSON.parse(readFileSync(seenFile, 'utf8')); } catch { /* none yet */ }
|
|
165
|
+
const now = {};
|
|
166
|
+
for (const folder of nested) {
|
|
167
|
+
const files = [];
|
|
168
|
+
const walk = (relative) => {
|
|
169
|
+
for (const entry of readdirSync(join(WORKSPACE, relative), { withFileTypes: true })) {
|
|
170
|
+
const path = `${relative}/${entry.name}`;
|
|
171
|
+
if (entry.isDirectory()) { if (!SKIPPED.includes(entry.name)) walk(path); continue; }
|
|
172
|
+
if (!entry.isFile() && !entry.isSymbolicLink()) continue;
|
|
173
|
+
const info = lstatSync(join(WORKSPACE, path));
|
|
174
|
+
if (entry.isFile() && info.size > MAX_FILE) { tooBig.push(path); continue; }
|
|
175
|
+
files.push({ path, mode: entry.isSymbolicLink() ? '120000' : info.mode & 0o111 ? '100755' : '100644', size: info.size, time: info.mtimeMs });
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
walk(folder);
|
|
179
|
+
now[folder] = JSON.stringify(files.map(({ path, mode, size, time }) => [path, mode, size, time]));
|
|
180
|
+
if (seen[folder] === now[folder]) continue;
|
|
181
|
+
git('rm', '-q', '-r', '--cached', '--ignore-unmatch', '--', folder);
|
|
182
|
+
if (!files.length) continue;
|
|
183
|
+
// Symbolic links are stored as their target, the way git stores them.
|
|
184
|
+
const hashes = [];
|
|
185
|
+
for (const file of files) {
|
|
186
|
+
hashes.push(file.mode === '120000'
|
|
187
|
+
? execFileSync('git', ['hash-object', '-w', '--stdin'], { env: { ...process.env, GIT_DIR: STORE }, input: readlinkSync(join(WORKSPACE, file.path)), encoding: 'utf8' }).trim()
|
|
188
|
+
: null);
|
|
189
|
+
}
|
|
190
|
+
const regular = files.filter((file, index) => !hashes[index]);
|
|
191
|
+
const hashed = regular.length ? execFileSync('git', ['hash-object', '-w', '--no-filters', '--stdin-paths'],
|
|
192
|
+
{ cwd: WORKSPACE, env: { ...process.env, GIT_DIR: STORE }, input: regular.map((file) => file.path).join('\n'), encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 }).trim().split('\n') : [];
|
|
193
|
+
let next = 0;
|
|
194
|
+
const lines = files.map((file, index) => `${file.mode} ${hashes[index] ?? hashed[next++]}\t${file.path}`);
|
|
195
|
+
execFileSync('git', ['update-index', '--add', '--index-info'], { cwd: WORKSPACE, env: { ...process.env, GIT_DIR: STORE, GIT_WORK_TREE: WORKSPACE }, input: lines.join('\n') + '\n' });
|
|
196
|
+
}
|
|
197
|
+
writeFileSync(seenFile, JSON.stringify(now));
|
|
198
|
+
return tooBig;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function snapshot(takenOn, callId) {
|
|
202
|
+
const env = { ...process.env, GIT_DIR: STORE, GIT_WORK_TREE: WORKSPACE, GIT_AUTHOR_NAME: 'RayTrace', GIT_AUTHOR_EMAIL: 'snapshots@raytrace.local',
|
|
203
|
+
GIT_COMMITTER_NAME: 'RayTrace', GIT_COMMITTER_EMAIL: 'snapshots@raytrace.local', GIT_TERMINAL_PROMPT: '0' };
|
|
204
|
+
const git = (...args) => execFileSync('git', args, { cwd: WORKSPACE, env, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024, stdio: ['ignore', 'pipe', 'ignore'] });
|
|
205
|
+
// Tools that run at once snapshot one at a time; a lock older than 30s was
|
|
206
|
+
// left by a hook that died.
|
|
207
|
+
const lock = `${STORE}.lock`;
|
|
208
|
+
const deadline = Date.now() + 4000;
|
|
209
|
+
mkdirSync(join(home, '.raytace'), { recursive: true, mode: 0o700 });
|
|
210
|
+
for (;;) {
|
|
211
|
+
try { mkdirSync(lock); break; } catch (error) {
|
|
212
|
+
if (error.code !== 'EEXIST' || Date.now() > deadline) return null;
|
|
213
|
+
try { if (Date.now() - statSync(lock).mtimeMs > 30_000) { rmdirSync(lock); continue; } } catch { continue; }
|
|
214
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 50);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
try {
|
|
218
|
+
if (!existsSync(STORE)) {
|
|
219
|
+
git('init', '-q');
|
|
220
|
+
git('config', 'core.autocrlf', 'false');
|
|
221
|
+
}
|
|
222
|
+
const nested = nestedRepositories();
|
|
223
|
+
// A folder that was a nested repository last time and is not now (gone,
|
|
224
|
+
// or its .git removed) goes back to `git add` like any other folder.
|
|
225
|
+
try {
|
|
226
|
+
for (const folder of Object.keys(JSON.parse(readFileSync(join(STORE, 'raytrace-nested.json'), 'utf8')))) {
|
|
227
|
+
if (!nested.includes(folder)) git('rm', '-q', '-r', '--cached', '--ignore-unmatch', '--', folder);
|
|
228
|
+
}
|
|
229
|
+
} catch { /* none recorded yet */ }
|
|
230
|
+
git('add', '-A', '-f', '--', '.', ...SKIPPED.map((name) => `:(glob,exclude)**/${name}/**`),
|
|
231
|
+
...nested.map((folder) => `:(literal,exclude)${folder}`));
|
|
232
|
+
// Too big to keep: the snapshot holds its previous version, and says so.
|
|
233
|
+
const tooBig = git('diff', '--cached', '--name-only', '-z', '--diff-filter=AM').split('\0').filter(Boolean)
|
|
234
|
+
.filter((path) => { try { return statSync(join(WORKSPACE, path)).size > MAX_FILE; } catch { return false; } });
|
|
235
|
+
if (tooBig.length) git('reset', '-q', '--', ...tooBig);
|
|
236
|
+
tooBig.push(...addNestedRepositories(git, nested));
|
|
237
|
+
const tree = git('write-tree').trim();
|
|
238
|
+
let parent = null;
|
|
239
|
+
try { parent = git('rev-parse', '-q', '--verify', 'HEAD').trim(); } catch { /* first snapshot */ }
|
|
240
|
+
const parentTree = parent ? git('rev-parse', `${parent}^{tree}`).trim() : null;
|
|
241
|
+
let commit = parent;
|
|
242
|
+
const changes = [];
|
|
243
|
+
if (tree !== parentTree) {
|
|
244
|
+
commit = git('commit-tree', tree, ...(parent ? ['-p', parent] : []), '-m', `${takenOn} ${callId ?? ''}`.trim()).trim();
|
|
245
|
+
git('update-ref', 'HEAD', commit);
|
|
246
|
+
const range = parent ? [parent, commit] : ['--root', commit];
|
|
247
|
+
const counts = new Map(git('diff-tree', '-r', '--numstat', '-z', '--no-commit-id', ...range).split('\0').filter(Boolean)
|
|
248
|
+
.map((entry) => { const [added, removed, ...path] = entry.split('\t'); return [path.join('\t'), [added, removed]]; }));
|
|
249
|
+
const statuses = git('diff-tree', '-r', '--name-status', '-z', '--no-commit-id', ...range).split('\0').filter(Boolean);
|
|
250
|
+
for (let i = 0; i + 1 < statuses.length && changes.length < MAX_LISTED; i += 2) {
|
|
251
|
+
const [added, removed] = counts.get(statuses[i + 1]) ?? ['-', '-'];
|
|
252
|
+
changes.push({ path: statuses[i + 1], status: statuses[i][0],
|
|
253
|
+
added: added === '-' ? null : Number(added), removed: removed === '-' ? null : Number(removed) });
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return { commit, parent, taken_on: takenOn, call_id: callId ?? null, taken_at: new Date().toISOString(),
|
|
257
|
+
changed: tree !== parentTree, changes, too_big: tooBig.slice(0, MAX_LISTED) };
|
|
258
|
+
} catch (error) {
|
|
259
|
+
if (process.env.RAYTRACE_HOOK_DEBUG) console.error(error); // silent otherwise: never disturb Claude Code
|
|
260
|
+
return null;
|
|
261
|
+
} finally { try { rmdirSync(lock); } catch { /* already gone */ } }
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const session = input.session_id;
|
|
265
|
+
const transcript = input.transcript_path;
|
|
266
|
+
if (!/^[0-9a-f-]{36}$/.test(session ?? '') || typeof transcript !== 'string'
|
|
267
|
+
|| !transcript.startsWith(join(home, '.claude', 'projects') + '/')) process.exit(0);
|
|
268
|
+
|
|
269
|
+
const callId = /^[A-Za-z0-9_-]{1,128}$/.test(input.tool_use_id ?? '') ? input.tool_use_id : null;
|
|
270
|
+
// A read of a file the owner restricted or watches. A write that went through
|
|
271
|
+
// is in the snapshot below, with its size; a refused one was queued above.
|
|
272
|
+
if (input.hook_event_name === 'PostToolUse' && READ_ONLY.has(input.tool_name)) {
|
|
273
|
+
const path = inside(input.tool_input?.file_path ?? input.tool_input?.notebook_path);
|
|
274
|
+
if (path && (restricted.includes(path) || watched.includes(path))) queue({ kind: 'read', path });
|
|
275
|
+
}
|
|
276
|
+
const snap = input.hook_event_name === 'UserPromptSubmit' ? snapshot('prompt', null)
|
|
277
|
+
: input.hook_event_name === 'PostToolUse' && !READ_ONLY.has(input.tool_name) ? snapshot('tool', callId) : null;
|
|
278
|
+
|
|
279
|
+
try {
|
|
280
|
+
// The token projects.py set-token gave this sandbox.
|
|
281
|
+
const token = readFileSync(join(home, '.raytace', 'sandbox-token'), 'utf8').trim();
|
|
282
|
+
if (!token) process.exit(0);
|
|
283
|
+
const stateDir = STATE_DIR;
|
|
284
|
+
const stateFile = join(stateDir, `${session}.offset`);
|
|
285
|
+
let offset = 0;
|
|
286
|
+
try { offset = Number(readFileSync(stateFile, 'utf8')) || 0; } catch { /* first send, or /tmp was cleared */ }
|
|
287
|
+
// The attempts queued since the last send. They go with the first send that
|
|
288
|
+
// succeeds (RayTrace files each by an id of its own, so one that arrives
|
|
289
|
+
// twice counts once) and are then taken off the file, not what came after.
|
|
290
|
+
const queuedFile = join(stateDir, `${session}.events.jsonl`);
|
|
291
|
+
let queued = Buffer.alloc(0);
|
|
292
|
+
try { queued = readFileSync(queuedFile); } catch { /* none queued */ }
|
|
293
|
+
const events = queued.toString('utf8').split('\n').filter(Boolean).slice(0, 1000)
|
|
294
|
+
.map((line) => { try { return JSON.parse(line); } catch { return null; } }).filter(Boolean);
|
|
295
|
+
let delivered = events.length === 0;
|
|
296
|
+
|
|
297
|
+
// Drain the bytes present at entry, not an endlessly growing transcript.
|
|
298
|
+
// Successful fragments do not consume the bounded conflict retry budget.
|
|
299
|
+
let endOffset = null;
|
|
300
|
+
let conflicts = 0;
|
|
301
|
+
while (conflicts < 8) {
|
|
302
|
+
// A new session's first prompt fires before Claude Code has written its
|
|
303
|
+
// transcript: send the snapshot anyway, with no lines yet.
|
|
304
|
+
let fd = null;
|
|
305
|
+
try { fd = openSync(transcript, 'r'); } catch (error) { if (error.code !== 'ENOENT') throw error; }
|
|
306
|
+
let text = '';
|
|
307
|
+
let more = false;
|
|
308
|
+
if (fd !== null) try {
|
|
309
|
+
if (endOffset === null) endOffset = fstatSync(fd).size;
|
|
310
|
+
const available = endOffset - offset;
|
|
311
|
+
if (available > 0) {
|
|
312
|
+
const buffer = Buffer.alloc(Math.min(available, CHUNK));
|
|
313
|
+
let end = readSync(fd, buffer, 0, buffer.length, offset);
|
|
314
|
+
// A fragment can split a JSON line, but never a UTF-8 codepoint:
|
|
315
|
+
// decoding half a character would change its bytes and the offset.
|
|
316
|
+
let lead = end - 1;
|
|
317
|
+
while (lead >= 0 && (buffer[lead] & 0xc0) === 0x80) lead -= 1;
|
|
318
|
+
if (lead >= 0) {
|
|
319
|
+
const byte = buffer[lead];
|
|
320
|
+
const width = byte < 0x80 ? 1 : byte < 0xe0 ? 2 : byte < 0xf0 ? 3 : 4;
|
|
321
|
+
if (end - lead < width) end = lead;
|
|
322
|
+
}
|
|
323
|
+
text = buffer.subarray(0, end).toString('utf8');
|
|
324
|
+
more = offset + end < endOffset;
|
|
325
|
+
}
|
|
326
|
+
} finally { closeSync(fd); }
|
|
327
|
+
const envelope = () => JSON.stringify({ session_id: session, hook_event_name: input.hook_event_name, offset, text, more, ...(snap ? { snapshot: snap } : {}),
|
|
328
|
+
...(delivered ? {} : { events }),
|
|
329
|
+
// A Playground fork's sandbox carries its own id (projects.py): RayTrace
|
|
330
|
+
// files the fork's steps under it, not under the project it forks.
|
|
331
|
+
...(/^rtf-[a-f0-9]{32}$/.test(process.env.RAYTRACE_SESSION ?? '') ? { fork: process.env.RAYTRACE_SESSION } : {}) });
|
|
332
|
+
let requestBody = envelope();
|
|
333
|
+
while (Buffer.byteLength(requestBody) > 4 * 1024 * 1024 && text) {
|
|
334
|
+
const bytes = Buffer.from(text);
|
|
335
|
+
let end = Math.floor(bytes.length / 2);
|
|
336
|
+
// Cut before any incomplete codepoint at the new boundary.
|
|
337
|
+
while (end > 0 && (bytes[end] & 0xc0) === 0x80) end -= 1;
|
|
338
|
+
text = bytes.subarray(0, end).toString('utf8');
|
|
339
|
+
more = true;
|
|
340
|
+
requestBody = envelope();
|
|
341
|
+
}
|
|
342
|
+
if (Buffer.byteLength(requestBody) > 4 * 1024 * 1024) {
|
|
343
|
+
console.error('RayTrace: transcript metadata exceeds the request limit; pending data was retained.');
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
const response = await fetch(BROKER, {
|
|
347
|
+
method: 'POST',
|
|
348
|
+
headers: { 'content-type': 'application/json', 'x-raytace-sandbox-token': token },
|
|
349
|
+
body: requestBody,
|
|
350
|
+
signal: AbortSignal.timeout(4000),
|
|
351
|
+
});
|
|
352
|
+
const body = await response.json().catch(() => ({}));
|
|
353
|
+
if (response.status === 409 && Number.isInteger(body.size) && body.size >= 0) { offset = body.size; conflicts += 1; continue; }
|
|
354
|
+
if (!response.ok || body.size !== offset + Buffer.byteLength(text)) break;
|
|
355
|
+
if (!delivered) {
|
|
356
|
+
delivered = true;
|
|
357
|
+
try { writeFileSync(queuedFile, readFileSync(queuedFile).subarray(queued.length)); } catch { /* already gone */ }
|
|
358
|
+
}
|
|
359
|
+
offset = body.size;
|
|
360
|
+
mkdirSync(stateDir, { recursive: true, mode: 0o700 });
|
|
361
|
+
writeFileSync(stateFile, String(offset));
|
|
362
|
+
if (!more || !text) break; // an incomplete UTF-8 tail waits for the next hook
|
|
363
|
+
}
|
|
364
|
+
} catch { /* RayTrace or the manager not running: stay silent */ }
|