raytrace-cli 0.11.1__py3-none-win_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. raytrace/__init__.py +1 -0
  2. raytrace/__main__.py +33 -0
  3. raytrace/app/LICENSE +21 -0
  4. raytrace/app/README.md +599 -0
  5. raytrace/app/bin/raytrace.mjs +297 -0
  6. raytrace/app/dist/dashboard/assets/dm-sans-latin-400-normal-BwCSEQnW.woff +0 -0
  7. raytrace/app/dist/dashboard/assets/dm-sans-latin-400-normal-CW0RaeGs.woff2 +0 -0
  8. raytrace/app/dist/dashboard/assets/dm-sans-latin-500-normal-B9HHJjqV.woff2 +0 -0
  9. raytrace/app/dist/dashboard/assets/dm-sans-latin-500-normal-Dr3UlScf.woff +0 -0
  10. raytrace/app/dist/dashboard/assets/dm-sans-latin-600-normal-Aqo67rzb.woff2 +0 -0
  11. raytrace/app/dist/dashboard/assets/dm-sans-latin-600-normal-BmdmIIQ2.woff +0 -0
  12. raytrace/app/dist/dashboard/assets/dm-sans-latin-700-normal-CUSSCpQX.woff +0 -0
  13. raytrace/app/dist/dashboard/assets/dm-sans-latin-700-normal-DvUfVpUG.woff2 +0 -0
  14. raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-400-normal-BjWJ59Pq.woff +0 -0
  15. raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-400-normal-BtiwyxMk.woff2 +0 -0
  16. raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-500-normal-BJfUCQsA.woff2 +0 -0
  17. raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-500-normal-DR84L5F-.woff +0 -0
  18. raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-600-normal-4vooXBpG.woff2 +0 -0
  19. raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-600-normal-DRtaDpgU.woff +0 -0
  20. raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-700-normal-BLI3TTWz.woff +0 -0
  21. raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-700-normal-CJIcxD6K.woff2 +0 -0
  22. raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-400-normal-BSMlKf0J.woff2 +0 -0
  23. raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-400-normal-CEL4l2ZJ.woff +0 -0
  24. raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-500-normal-Ael50iVv.woff +0 -0
  25. raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-500-normal-Bq9vWWag.woff2 +0 -0
  26. raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-600-normal-CTOM6hUh.woff2 +0 -0
  27. raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-600-normal-fLZuRloM.woff +0 -0
  28. raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-400-normal-DMdlQ8Kv.woff +0 -0
  29. raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-400-normal-xuaO2J-f.woff2 +0 -0
  30. raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-500-normal-BIfNGwUT.woff +0 -0
  31. raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-500-normal-BqneJy0T.woff2 +0 -0
  32. raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-600-normal-9HEixskS.woff +0 -0
  33. raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-600-normal-V-xxqcpd.woff2 +0 -0
  34. raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-400-normal-CvHOgSBP.woff +0 -0
  35. raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-400-normal-DMJ8VG8y.woff2 +0 -0
  36. raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-500-normal-CB9ihrfo.woff +0 -0
  37. raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-500-normal-DSY6xOcd.woff2 +0 -0
  38. raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-600-normal-BgSNZQsw.woff2 +0 -0
  39. raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-600-normal-DWFSQ4vo.woff +0 -0
  40. raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-400-normal-BmRBH3aV.woff2 +0 -0
  41. raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-400-normal-D3D2R8hC.woff +0 -0
  42. raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-500-normal-CAhNIIs5.woff2 +0 -0
  43. raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-500-normal-CZ70TYgx.woff +0 -0
  44. raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-600-normal-D38SheWl.woff2 +0 -0
  45. raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-600-normal-DmB0ttJJ.woff +0 -0
  46. raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-400-normal-BulugwFq.woff2 +0 -0
  47. raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-400-normal-DDuiU_S-.woff +0 -0
  48. raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-500-normal-C8zxqsMH.woff +0 -0
  49. raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-500-normal-DZ4AoWbu.woff2 +0 -0
  50. raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-600-normal-D2EvbN8M.woff2 +0 -0
  51. raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-600-normal-iLQfcSjf.woff +0 -0
  52. raytrace/app/dist/dashboard/assets/index-DepcI8Fi.js +38 -0
  53. raytrace/app/dist/dashboard/assets/index-H97VLD5l.css +1 -0
  54. raytrace/app/dist/dashboard/favicon.svg +6 -0
  55. raytrace/app/dist/dashboard/index.html +15 -0
  56. raytrace/app/package.json +48 -0
  57. raytrace/app/proxy/agent-intent-schema.test.mjs +142 -0
  58. raytrace/app/proxy/call-assessment.mjs +385 -0
  59. raytrace/app/proxy/call-assessment.test.mjs +167 -0
  60. raytrace/app/proxy/call-category.mjs +461 -0
  61. raytrace/app/proxy/capture-health.mjs +30 -0
  62. raytrace/app/proxy/capture-health.test.mjs +44 -0
  63. raytrace/app/proxy/capture.mjs +38 -0
  64. raytrace/app/proxy/capture.test.mjs +49 -0
  65. raytrace/app/proxy/claude-code-adapter.mjs +301 -0
  66. raytrace/app/proxy/claude-code-hook.mjs +22 -0
  67. raytrace/app/proxy/codex-config.mjs +36 -0
  68. raytrace/app/proxy/codex.mjs +14 -0
  69. raytrace/app/proxy/evidence-schema.test.mjs +240 -0
  70. raytrace/app/proxy/evidence-store.mjs +993 -0
  71. raytrace/app/proxy/evidence-store.test.mjs +301 -0
  72. raytrace/app/proxy/exec-narrative.mjs +625 -0
  73. raytrace/app/proxy/exec-narrative.test.mjs +262 -0
  74. raytrace/app/proxy/execution-correlation.mjs +185 -0
  75. raytrace/app/proxy/execution-correlation.test.mjs +146 -0
  76. raytrace/app/proxy/execution-runner.mjs +89 -0
  77. raytrace/app/proxy/execution-runner.test.mjs +38 -0
  78. raytrace/app/proxy/experiment-engine.mjs +195 -0
  79. raytrace/app/proxy/experiment-engine.test.mjs +93 -0
  80. raytrace/app/proxy/explanations.mjs +36 -0
  81. raytrace/app/proxy/explanations.test.mjs +20 -0
  82. raytrace/app/proxy/failure-signature.mjs +56 -0
  83. raytrace/app/proxy/gvisor-forwarder.mjs +493 -0
  84. raytrace/app/proxy/gvisor-forwarder.test.mjs +433 -0
  85. raytrace/app/proxy/init-evidence-db.mjs +227 -0
  86. raytrace/app/proxy/local-sandboxes.mjs +94 -0
  87. raytrace/app/proxy/local-token.mjs +30 -0
  88. raytrace/app/proxy/playground-recovery.test.mjs +130 -0
  89. raytrace/app/proxy/playground.mjs +439 -0
  90. raytrace/app/proxy/prompt-context.mjs +278 -0
  91. raytrace/app/proxy/prompt-traces.mjs +40 -0
  92. raytrace/app/proxy/prompt-traces.test.mjs +32 -0
  93. raytrace/app/proxy/providers.mjs +65 -0
  94. raytrace/app/proxy/providers.test.mjs +76 -0
  95. raytrace/app/proxy/proxy.integration.test.mjs +135 -0
  96. raytrace/app/proxy/raytace-proxy.mjs +941 -0
  97. raytrace/app/proxy/request-metrics.mjs +13 -0
  98. raytrace/app/proxy/request-metrics.test.mjs +19 -0
  99. raytrace/app/proxy/request-policy.mjs +45 -0
  100. raytrace/app/proxy/request-policy.test.mjs +47 -0
  101. raytrace/app/proxy/run-recap.mjs +139 -0
  102. raytrace/app/proxy/runtime-attribution.mjs +110 -0
  103. raytrace/app/proxy/sandbox-bridge.mjs +49 -0
  104. raytrace/app/proxy/schema/EVIDENCE_DATABASE.md +276 -0
  105. raytrace/app/proxy/schema/agent_intent.sql +101 -0
  106. raytrace/app/proxy/schema/auxiliary.sql +90 -0
  107. raytrace/app/proxy/schema/capture_health.sql +14 -0
  108. raytrace/app/proxy/schema/classification.sql +89 -0
  109. raytrace/app/proxy/schema/file_context.sql +53 -0
  110. raytrace/app/proxy/schema/runtime_evidence.sql +201 -0
  111. raytrace/app/proxy/schema/search.sql +66 -0
  112. raytrace/app/proxy/schema/shared_storage.sql +21 -0
  113. raytrace/app/proxy/search.mjs +418 -0
  114. raytrace/app/proxy/search.test.mjs +161 -0
  115. raytrace/app/proxy/smoke.test.mjs +93 -0
  116. raytrace/app/proxy/step-attribution.mjs +380 -0
  117. raytrace/app/proxy/step-lab.mjs +78 -0
  118. raytrace/app/proxy/step-lab.test.mjs +38 -0
  119. raytrace/app/proxy/summaries.mjs +151 -0
  120. raytrace/app/proxy/summaries.test.mjs +69 -0
  121. raytrace/app/proxy/tool-metadata.mjs +111 -0
  122. raytrace/app/proxy/tool-metadata.test.mjs +38 -0
  123. raytrace/app/proxy/trace-view.mjs +324 -0
  124. raytrace/app/proxy/trace-view.test.mjs +92 -0
  125. raytrace/app/proxy/typesafe.mjs +63 -0
  126. raytrace/app/proxy/typesafe.test.mjs +39 -0
  127. raytrace/app/src/agents.mjs +7 -0
  128. raytrace/app/src/api.mjs +57 -0
  129. raytrace/app/src/auth.mjs +205 -0
  130. raytrace/app/src/beta.mjs +96 -0
  131. raytrace/app/src/claude-code.mjs +93 -0
  132. raytrace/app/src/commands.mjs +285 -0
  133. raytrace/app/src/config.mjs +49 -0
  134. raytrace/app/src/export.mjs +200 -0
  135. raytrace/app/src/lima.mjs +176 -0
  136. raytrace/app/src/local-sandbox.mjs +358 -0
  137. raytrace/app/src/mcp.mjs +688 -0
  138. raytrace/app/src/paths.mjs +33 -0
  139. raytrace/app/src/prompt.mjs +42 -0
  140. raytrace/app/src/proxy-process.mjs +143 -0
  141. raytrace/app/src/sandbox.mjs +306 -0
  142. raytrace/app/src/share.mjs +86 -0
  143. raytrace/app/src/terminal.mjs +68 -0
  144. raytrace/app/src/upload.mjs +198 -0
  145. raytrace/app/worker/sandbox/Dockerfile +8 -0
  146. raytrace/app/worker/sandbox/SYSCALL_CATALOG.md +197 -0
  147. raytrace/app/worker/sandbox/claude-hook.mjs +364 -0
  148. raytrace/app/worker/sandbox/collector.py +405 -0
  149. raytrace/app/worker/sandbox/dns_forwarder.py +255 -0
  150. raytrace/app/worker/sandbox/engine.py +940 -0
  151. raytrace/app/worker/sandbox/file_scan.py +491 -0
  152. raytrace/app/worker/sandbox/lima.yaml +29 -0
  153. raytrace/app/worker/sandbox/network.py +90 -0
  154. raytrace/app/worker/sandbox/projects.py +702 -0
  155. raytrace/app/worker/sandbox/setup.py +159 -0
  156. raytrace/app/worker/sandbox/syscall_catalog.sql +985 -0
  157. raytrace/app/worker/sandbox/transcript-delivery.test.mjs +153 -0
  158. raytrace/app/worker/sandbox/viewer.py +195 -0
  159. raytrace/node/LICENSE +2946 -0
  160. raytrace/node/node.exe +4 -0
  161. raytrace_cli-0.11.1.dist-info/LICENSE +21 -0
  162. raytrace_cli-0.11.1.dist-info/METADATA +42 -0
  163. raytrace_cli-0.11.1.dist-info/RECORD +165 -0
  164. raytrace_cli-0.11.1.dist-info/WHEEL +4 -0
  165. raytrace_cli-0.11.1.dist-info/entry_points.txt +2 -0
@@ -0,0 +1,13 @@
1
+ const numeric = value => typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : null;
2
+ export function requestMetrics(row) {
3
+ const payload = row.response?.payload;
4
+ const frames = payload?.events || [];
5
+ const usage = payload?.usage || frames.findLast(e => e.usage || e.response?.usage)?.usage || frames.findLast(e => e.response?.usage)?.response?.usage || {};
6
+ const elapsed = row.completed_at ? Date.parse(row.completed_at) - Date.parse(row.timestamp) : NaN;
7
+ return {
8
+ input: numeric(usage.input_tokens ?? usage.prompt_tokens),
9
+ output: numeric(usage.output_tokens ?? usage.completion_tokens),
10
+ cost: row.provider === 'openrouter' ? numeric(usage.cost) : null,
11
+ durationMs: numeric(elapsed),
12
+ };
13
+ }
@@ -0,0 +1,19 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { requestMetrics } from './request-metrics.mjs';
4
+ const row = usage => ({ provider: 'openrouter', timestamp: '2026-09-10T00:00:00Z', completed_at: '2026-09-10T00:00:02Z', response: { payload: { usage } } });
5
+ test('normalizes provider usage, preserving free responses', () => {
6
+ assert.deepEqual(requestMetrics(row({ input_tokens: 100, output_tokens: 20, cost: 0 })), { input: 100, output: 20, cost: 0, durationMs: 2000 });
7
+ assert.equal(requestMetrics(row({ prompt_tokens: 50, completion_tokens: 10, cost: .01 })).input, 50);
8
+ });
9
+ test('unknown and previously redacted values never become zero', () => {
10
+ assert.deepEqual(requestMetrics({}), { input: null, output: null, cost: null, durationMs: null });
11
+ assert.equal(requestMetrics(row({ input_tokens: '[REDACTED]' })).input, null);
12
+ assert.equal(requestMetrics({ ...row({ cost: 5 }), provider: 'openai' }).cost, null);
13
+ });
14
+ test('uses terminal streaming usage rather than summing cumulative frames', () => {
15
+ const data = row({}); data.response.payload = { events: [{ usage: { prompt_tokens: 1 } }, { usage: { prompt_tokens: 10, cost: .2 } }] };
16
+ assert.equal(requestMetrics(data).input, 10);
17
+ data.response.payload = { events: [{ response: { usage: { input_tokens: 15 } } }] };
18
+ assert.equal(requestMetrics(data).input, 15);
19
+ });
@@ -0,0 +1,45 @@
1
+ import { createHash, timingSafeEqual } from 'node:crypto';
2
+
3
+ /** Browser requests must satisfy the same policy before any local API or
4
+ * provider route runs. A CORS denial alone does not stop a simple POST from
5
+ * spending the proxy's configured provider key. */
6
+ export function trustedOrigin(origin, allowedOrigins = new Set()) {
7
+ if (!origin) return true; // Native CLI clients do not send Origin.
8
+ if (allowedOrigins.has(origin)) return true;
9
+ try {
10
+ const url = new URL(origin);
11
+ return url.protocol === 'http:' && ['localhost', '127.0.0.1', '[::1]'].includes(url.hostname);
12
+ } catch { return false; }
13
+ }
14
+
15
+ export function trustedRequest(req, allowedOrigins = new Set()) {
16
+ return trustedOrigin(req.headers.origin, allowedOrigins)
17
+ && /^(localhost|127\.0\.0\.1|\[::1\])(?::\d+)?$/.test(req.headers.host || '');
18
+ }
19
+
20
+ const digest = (value) => createHash('sha256').update(String(value)).digest();
21
+ const same = (a, b) => timingSafeEqual(digest(a), digest(b));
22
+
23
+ /** What the dashboard's browser holds in place of the token itself. */
24
+ export const sessionFor = (token) => digest(`dashboard:${token}`).toString('hex');
25
+ export const sessionCookie = (port) => `raytrace_session_${port}`;
26
+ /** What the sandbox VM's evidence viewer holds: one-way, so a copy in the VM opens only the viewer. */
27
+ export const viewerTokenFor = (token) => digest(`viewer:${token}`).toString('hex');
28
+
29
+ /** A client that read the token file: the CLI, the hook, `raytrace codex`. */
30
+ export function hasToken(req, token) {
31
+ const given = /^Bearer (.+)$/.exec(req.headers.authorization || '')?.[1];
32
+ return Boolean(given) && same(given, token);
33
+ }
34
+
35
+ /** The dashboard, in a browser `raytrace open` signed in. Cookies are shared
36
+ * between the ports of a host, so a page on another localhost port sends this
37
+ * one too: it counts only from this origin, and only on the local API
38
+ * (hasToken, not this, guards the provider routes). */
39
+ export function hasSession(req, token, port) {
40
+ const { origin, host, cookie } = req.headers;
41
+ if (origin && origin !== `http://${host}`) return false;
42
+ const name = `${sessionCookie(port)}=`;
43
+ const value = (cookie || '').split(/;\s*/).find((part) => part.startsWith(name))?.slice(name.length);
44
+ return Boolean(value) && same(value, sessionFor(token));
45
+ }
@@ -0,0 +1,47 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { hasSession, hasToken, sessionCookie, sessionFor, trustedRequest, viewerTokenFor } from './request-policy.mjs';
4
+
5
+ const request = (url, headers) => ({method: 'POST', url, headers: {host: '127.0.0.1:8797', ...headers}});
6
+ test('simple browser POSTs from untrusted origins are refused on provider and dashboard routes', () => {
7
+ for (const url of ['/v1/responses', '/v1/chat/completions', '/v1/messages', '/raytace/experiments']) {
8
+ assert.equal(trustedRequest(request(url, {origin: 'http://untrusted.invalid', 'content-type': 'text/plain'})), false);
9
+ assert.equal(trustedRequest(request(url, {origin: 'null'})), false);
10
+ }
11
+ });
12
+ test('provider requests reject non-loopback Host values, including rebinding names', () => {
13
+ for (const host of ['untrusted.invalid:8797', 'localhost.untrusted.invalid:8797', '192.0.2.1:8797', '']) {
14
+ assert.equal(trustedRequest(request('/v1/responses', {host})), false);
15
+ }
16
+ });
17
+ test('native clients and local dashboards retain access', () => {
18
+ for (const host of ['127.0.0.1:8797', 'localhost:8797', '[::1]:8797']) {
19
+ assert.equal(trustedRequest(request('/v1/responses', {host})), true);
20
+ assert.equal(trustedRequest(request('/raytace/traces', {host, origin: 'http://localhost:3000'})), true);
21
+ }
22
+ });
23
+ test('explicit UI origin permissions do not permit a non-loopback Host', () => {
24
+ const origins = new Set(['https://dashboard.example']);
25
+ assert.equal(trustedRequest(request('/v1/responses', {origin: 'https://dashboard.example'}), origins), true);
26
+ assert.equal(trustedRequest(request('/v1/responses', {host: 'untrusted.invalid', origin: 'https://dashboard.example'}), origins), false);
27
+ });
28
+ test('only the token file\'s value authorizes a client, whatever Origin and Host it sends', () => {
29
+ const headers = (authorization) => ({headers: {authorization}});
30
+ assert.equal(hasToken(headers('Bearer right'), 'right'), true);
31
+ for (const value of ['Bearer wrong', 'Bearer ', 'right', undefined]) assert.equal(hasToken(headers(value), 'right'), false);
32
+ });
33
+ test('the dashboard cookie counts only from the dashboard\'s own origin', () => {
34
+ const cookie = `other=1; ${sessionCookie(8797)}=${sessionFor('right')}`;
35
+ const seen = (headers) => hasSession({headers: {host: '127.0.0.1:8797', ...headers}}, 'right', 8797);
36
+ assert.equal(seen({cookie}), true);
37
+ assert.equal(seen({cookie, origin: 'http://127.0.0.1:8797'}), true);
38
+ assert.equal(seen({cookie, origin: 'http://127.0.0.1:3000'}), false);
39
+ assert.equal(seen({cookie: `${sessionCookie(8797)}=right`}), false);
40
+ assert.equal(seen({}), false);
41
+ assert.equal(hasSession({headers: {host: '127.0.0.1:18797', cookie}}, 'right', 18797), false);
42
+ });
43
+ test('the VM\'s viewer token is neither the API token nor the dashboard session', () => {
44
+ assert.notEqual(viewerTokenFor('right'), 'right');
45
+ assert.notEqual(viewerTokenFor('right'), sessionFor('right'));
46
+ assert.equal(hasToken({headers: {authorization: `Bearer ${viewerTokenFor('right')}`}}, 'right'), false);
47
+ });
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Recap: what one prompt's run did, and the observer's reading of why.
3
+ *
4
+ * A stronger model than the ones that label single steps (the observer) reads
5
+ * the run as a sequence of short step lines, not the transcript, and writes:
6
+ *
7
+ * happened what the run did, each sentence tied to the steps it covers
8
+ * reasoning why the agent probably went that way: a reading, not a
9
+ * measurement, each with how sure the observer is
10
+ * flags where what the agent said and what was observed disagree
11
+ *
12
+ * outline the run, compressed: the prompt, each tool call with what it
13
+ * asked for, what came back, what the sandbox saw of it, the
14
+ * files it changed, and what the agent said just before it
15
+ * request one chat request for the observer; its key changes when the
16
+ * outline, the model or this version does
17
+ * parse the answer, checked: a sentence must name steps that exist,
18
+ * and may not state a percentage (none was measured here)
19
+ *
20
+ * Pure functions; the proxy and the hosted evidence service do the I/O.
21
+ */
22
+ import { digest } from './experiment-engine.mjs';
23
+
24
+ export const RECAP_VERSION = 1;
25
+ export const DEFAULT_OBSERVER = 'anthropic/claude-sonnet-5.5';
26
+ const MAX_STEPS = 80;
27
+ const KEEP_FIRST = 30;
28
+ const clip = (text, max) => { const line = String(text ?? '').replace(/\s+/g, ' ').trim(); return line.length > max ? `${line.slice(0, max - 1)}…` : line; };
29
+
30
+ function parsed(raw) { try { return JSON.parse(raw); } catch { return null; } }
31
+
32
+ /** What the agent was told back for one call, from the trace's events. */
33
+ function reported(resultsByCall, callId) {
34
+ const raw = resultsByCall.get(callId);
35
+ if (!raw) return null;
36
+ const output = typeof raw.output === 'string' ? raw.output : JSON.stringify(raw.output ?? '');
37
+ const code = Number(output.match(/Process exited with code (\d+)/)?.[1] ?? NaN);
38
+ const failed = Number.isFinite(code) ? code !== 0 : /^\s*(error|failed)|Rejected\(/i.test(output.slice(0, 120));
39
+ return { state: failed ? 'failed' : 'ok', says: clip(output, 160) };
40
+ }
41
+
42
+ /** The run as the observer reads it, or null when there is nothing to recap
43
+ * (no tool calls: the answer is the whole story). */
44
+ export function recapOutline(trace) {
45
+ const calls = trace?.callVerifications ?? [];
46
+ if (!calls.length) return null;
47
+ const events = trace.events ?? [];
48
+ const resultsByCall = new Map();
49
+ // What the agent wrote in the response that proposed a call: its own account of why.
50
+ const notesByExchange = new Map();
51
+ for (const event of events) {
52
+ if (event.title?.startsWith('Tool result:')) { const raw = parsed(event.raw); if (raw?.call_id) resultsByCall.set(raw.call_id, raw); }
53
+ else if (event.title === 'Model answer' && event.exchange_id && event.detail) {
54
+ notesByExchange.set(event.exchange_id, [...(notesByExchange.get(event.exchange_id) ?? []), event.detail]);
55
+ }
56
+ }
57
+ const told = new Set();
58
+ const steps = calls.map((call, index) => {
59
+ const note = !told.has(call.exchange_id) ? (notesByExchange.get(call.exchange_id) ?? []).join(' ') : '';
60
+ told.add(call.exchange_id);
61
+ const changes = call.snapshot?.changed ? (call.snapshot.changes ?? []) : [];
62
+ // Only a sandbox can contradict the agent: outside one, nothing independent watched.
63
+ const observed = !trace.sandboxId ? undefined
64
+ : call.kernel_confirmed || call.verified ? { ran: true, ...(call.exit_code != null ? { exit_code: call.exit_code } : {}) }
65
+ : call.name === 'Bash' || /exec|shell/i.test(call.name) ? { ran: false } : undefined;
66
+ return {
67
+ step: index + 1, tool: call.name, asked: clip(call.proposed ?? call.name, 220),
68
+ ...(note ? { agent_said_before: clip(note, 260) } : {}),
69
+ ...(reported(resultsByCall, call.call_id) ? { agent_was_told: reported(resultsByCall, call.call_id) } : {}),
70
+ ...(observed ? { sandbox_observed: observed } : {}),
71
+ ...(changes.length ? { files_changed: changes.slice(0, 6).map((change) => `${change.status} ${change.path}`), ...(changes.length > 6 ? { more_files: changes.length - 6 } : {}) } : {}),
72
+ };
73
+ });
74
+ // A long run keeps its start and its end; the middle is counted, not sent.
75
+ const shown = steps.length > MAX_STEPS ? [...steps.slice(0, KEEP_FIRST), ...steps.slice(-(MAX_STEPS - KEEP_FIRST))] : steps;
76
+ return {
77
+ prompt: clip(trace.title, 1500), agent: trace.provider, model: trace.model, in_sandbox: Boolean(trace.sandboxId),
78
+ steps: shown, total_steps: steps.length, ...(steps.length > MAX_STEPS ? { omitted_steps: `${KEEP_FIRST + 1}-${steps.length - (MAX_STEPS - KEEP_FIRST)}` } : {}),
79
+ final_answer: trace.answer ? clip(trace.answer, 1200) : null,
80
+ };
81
+ }
82
+
83
+ export const RECAP_INSTRUCTIONS = [
84
+ 'You are an observer reading the record of one run of a coding agent. Someone who did not watch the run wants to know what it did and, as far as the record shows, why.',
85
+ 'The record is JSON: the prompt, numbered steps (what the agent asked a tool to do, what it said just before, what it was told back, and, when the run was in a sandbox, what the sandbox itself observed and which files changed), and the final answer.',
86
+ 'Reply with one JSON object and nothing else:',
87
+ '{"headline": string, "happened": [{"text": string, "steps": [number]}], "reasoning": [{"text": string, "steps": [number], "confidence": "likely" | "possible" | "unclear"}], "flags": [{"text": string, "steps": [number]}]}',
88
+ 'headline: one plain sentence, at most 25 words, saying what the run achieved or failed to achieve.',
89
+ 'happened: 3 to 7 sentences that together tell the run in order. Group steps that did one thing into one sentence. State only what the record shows. Prefer what the sandbox observed over what the agent was told or said.',
90
+ 'reasoning: 2 to 4 sentences on why the agent most probably went the way it did: what it seemed to be working toward, and what it reacted to (the prompt, something a step returned, its own earlier plan). This is your reading of the sequence, so say "probably" or "appears to" where the record does not settle it, and use "unclear" when it does not say.',
91
+ 'flags: only real disagreements in the record, such as the agent reporting success where the sandbox saw a non-zero exit, a command the sandbox never saw run, or an answer that claims something no step did. Usually empty.',
92
+ 'Every item lists the step numbers it rests on, which must be numbers from the record. Never state a percentage or a probability as a number: none was measured. Do not invent steps, files, commands or results. Plain sentences, no markdown.',
93
+ ].join('\n');
94
+
95
+ /** One chat request for the observer; `key` identifies this outline, model and version. */
96
+ export function recapRequest(outline, model = DEFAULT_OBSERVER) {
97
+ const input = JSON.stringify(outline);
98
+ return { key: digest({ version: RECAP_VERSION, kind: 'run-recap', model, input }), payload: {
99
+ model, max_tokens: 1100, temperature: 0.2,
100
+ messages: [{ role: 'system', content: RECAP_INSTRUCTIONS }, { role: 'user', content: input }],
101
+ } };
102
+ }
103
+
104
+ /** The observer's text out of a chat completion (or a Responses-style body). */
105
+ export function recapText(response) {
106
+ const content = response?.choices?.[0]?.message?.content;
107
+ if (typeof content === 'string') return content;
108
+ if (Array.isArray(content)) return content.map((part) => part?.text ?? '').join('');
109
+ return (response?.output ?? []).filter((item) => item?.type === 'message').map((item) => (item.content ?? []).map((part) => part?.text ?? '').join('')).join('');
110
+ }
111
+
112
+ /** The recap, checked against the outline; null when the answer is not usable. */
113
+ export function parseRecap(text, outline) {
114
+ const body = String(text ?? '');
115
+ const value = parsed(body.slice(body.indexOf('{'), body.lastIndexOf('}') + 1));
116
+ if (!value || typeof value !== 'object') return null;
117
+ const known = new Set(outline.steps.map((step) => step.step));
118
+ const sentence = (item, max) => {
119
+ const said = typeof item?.text === 'string' ? clip(item.text, max) : '';
120
+ // No number here was measured, so a percentage would be made up.
121
+ if (!said || /\d\s?%|\bpercent\b/i.test(said)) return null;
122
+ const steps = [...new Set((Array.isArray(item.steps) ? item.steps : []).filter((step) => known.has(step)))].sort((a, b) => a - b);
123
+ // A sentence that names steps, none of them real, rests on nothing.
124
+ if (Array.isArray(item.steps) && item.steps.length && !steps.length) return null;
125
+ return { text: said, steps };
126
+ };
127
+ const list = (items, max, limit, extra = () => ({})) => (Array.isArray(items) ? items : []).slice(0, limit)
128
+ .map((item) => { const kept = sentence(item, max); return kept && { ...kept, ...extra(item) }; }).filter(Boolean);
129
+ const happened = list(value.happened, 360, 8);
130
+ if (!happened.length) return null;
131
+ return {
132
+ headline: typeof value.headline === 'string' && !/\d\s?%/.test(value.headline) ? clip(value.headline, 220) : null,
133
+ happened,
134
+ reasoning: list(value.reasoning, 420, 5, (item) => ({ confidence: ['likely', 'possible', 'unclear'].includes(item.confidence) ? item.confidence : 'possible' })),
135
+ // A flag must point at the steps that disagree.
136
+ flags: list(value.flags, 320, 4).filter((flag) => flag.steps.length),
137
+ steps_read: outline.steps.length, total_steps: outline.total_steps,
138
+ };
139
+ }
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Ties sandbox processes the gVisor forwarder observed to the tool calls they
3
+ * ran for. Shared by the local proxy (raytace-proxy.mjs, its SQLite store)
4
+ * and the hosted evidence service (RayTrace's cloud, a
5
+ * Postgres store bound to one org); `db` is either, and every call into it is
6
+ * awaited, which a synchronous store answers at once.
7
+ */
8
+ import { matchBatch, OPEN_WINDOW_CAP_MS } from './execution-correlation.mjs';
9
+ import { commandMarker } from './gvisor-forwarder.mjs';
10
+
11
+ // Tools that run a command. Only their calls are matched to a process by its
12
+ // text: Claude Code's Read reads the file itself, so a `cat` that ran nearby
13
+ // (its own shell setup runs several) is never that call. A call recorded
14
+ // without a tool name is still considered, as before. Call-id markers are
15
+ // exact and need no such check.
16
+ const COMMAND_TOOLS = /^(bash|shell|exec_command|local_shell|container\.exec)$/i;
17
+ const runsCommands = (candidate) => !candidate.tool_name || COMMAND_TOOLS.test(candidate.tool_name);
18
+
19
+ /**
20
+ * Records sandbox processes the gVisor forwarder observed and ties each to
21
+ * the proposed tool call it ran for, when one can be identified. Every
22
+ * process is recorded whether or not it matches: a process with no
23
+ * attribution is what "ran, but no tool call accounts for it" means.
24
+ *
25
+ * Candidates are the session's proposed calls not yet tied to a process,
26
+ * each with its causal window (proposal -> result sent back). A process
27
+ * carrying RAYTRACE_CALL_ID needs no matching: the call id travelled with
28
+ * it, so that join is exact and overrides the text match. It is still
29
+ * checked against this session's own candidates, so a forged or stale
30
+ * marker cannot attach a process to a call in another session.
31
+ * Returns exec id -> match for whatever matched; see
32
+ * execution-correlation.mjs for why this is not a plain id lookup.
33
+ */
34
+ export async function ingestExecutions(db, events, { sessionId = null } = {}) {
35
+ const matches = new Map();
36
+ if (!Array.isArray(events) || !events.length) return matches;
37
+ const recorded = new Map();
38
+ for (const event of events) {
39
+ recorded.set(event.id, await db.recordProcessStart({
40
+ containerId: event.container_id, projectId: sessionId, raw: event.raw, startedMs: event.timestamp }));
41
+ }
42
+ const timestamps = events.map((e) => e.timestamp).filter(Number.isFinite);
43
+ if (!sessionId || !timestamps.length) return matches;
44
+ // The pool reaches back as far as an open window may stay open, plus the
45
+ // clock margin between the proxy and the sandbox.
46
+ const margin = Math.max(0, ...events.map((e) => e.margin).filter(Number.isFinite));
47
+ const candidates = await db.candidatesInWindow(Math.min(...timestamps) - OPEN_WINDOW_CAP_MS - margin, Math.max(...timestamps) + margin, { sessionId });
48
+ const found = matchBatch(events, candidates.filter(runsCommands));
49
+ const byExternalId = new Map(candidates.map((c) => [c.external_call_id, c.call_id]));
50
+ for (const event of events) {
51
+ const eventId = recorded.get(event.id)?.eventId;
52
+ const markedCall = event.call_marker ? byExternalId.get(event.call_marker) : null;
53
+ const match = markedCall ? { call_id: markedCall, score: 1, basis: 'marker' } : found.get(event.id);
54
+ if (!match || !eventId) continue; // no confident match: the process stays recorded, unattributed -- never a guess
55
+ await db.attribute({ toolCallId: match.call_id, eventId, method: match.basis === 'id' ? 'marker' : match.basis,
56
+ // A marker join is exact; a text match that gVisor itself observed is
57
+ // corroborated. Recorded at capture, never promoted later.
58
+ confidence: markedCall ? 'exact' : 'corroborated', score: match.score,
59
+ windowStartMs: match.window_start ?? null, windowEndMs: match.window_end ?? null });
60
+ matches.set(event.id, match);
61
+ }
62
+ return matches;
63
+ }
64
+
65
+ /**
66
+ * Ties a sandbox's processes to the calls they ran for, after the fact.
67
+ * Claude Code starts a tool before its answer has finished streaming, and a
68
+ * call is only recorded once the answer is complete, so the gVisor forwarder
69
+ * has usually seen the process before the call it belongs to exists. Once
70
+ * the call is recorded, the same rules apply as at exec time: the call id the
71
+ * process carries (checked against this session's own calls), else a process
72
+ * started by one already tied to a call inherits it, else the text match.
73
+ */
74
+ export async function attributeLateProcesses(db, sessionId, log = console.log) {
75
+ const now = Date.now();
76
+ const candidates = await db.candidatesInWindow(now - 24 * 3_600_000, now + 60_000, { sessionId });
77
+ if (!candidates.length) return;
78
+ const margin = 2_000;
79
+ const processes = await db.unattributedProcesses({ sessionId, sinceMs: Math.min(...candidates.map((c) => c.window_start)) - margin });
80
+ const byExternalId = new Map(candidates.map((c) => [c.external_call_id, c]));
81
+ const claimed = new Set();
82
+ const callOfProcess = new Map(); // process id -> call id, for children within this pass
83
+ const unmatched = [];
84
+ let tied = 0;
85
+ for (const proc of processes) {
86
+ const argv = Array.isArray(proc.raw.argv) ? proc.raw.argv.filter((part) => typeof part === 'string') : [];
87
+ const command = argv.join(' ').trim();
88
+ const marker = proc.raw.markers?.RAYTRACE_CALL_ID ?? commandMarker(command);
89
+ const marked = marker ? byExternalId.get(marker) : null;
90
+ const parentCall = proc.parent_call_id ?? callOfProcess.get(proc.parent_process_id);
91
+ let call = null; let method = null;
92
+ if (marked && !claimed.has(marked.call_id)) { call = marked.call_id; method = 'marker'; claimed.add(call); }
93
+ else if (parentCall) { call = parentCall; method = 'inherited'; }
94
+ else if (marked) { call = marked.call_id; method = 'inherited'; }
95
+ if (call) {
96
+ await db.attribute({ toolCallId: call, eventId: proc.event_id, method, confidence: method === 'marker' ? 'exact' : 'corroborated', score: method === 'marker' ? 1 : null });
97
+ callOfProcess.set(proc.process_id, call);
98
+ tied += 1;
99
+ } else if (command && Number.isFinite(proc.first_seen_ms)) unmatched.push({ id: proc.event_id, command, timestamp: proc.first_seen_ms, margin, process_id: proc.process_id });
100
+ }
101
+ const found = matchBatch(unmatched, candidates.filter((c) => !claimed.has(c.call_id) && runsCommands(c)));
102
+ for (const exec of unmatched) {
103
+ const match = found.get(exec.id);
104
+ if (!match) continue;
105
+ await db.attribute({ toolCallId: match.call_id, eventId: exec.id, method: match.basis === 'id' ? 'marker' : match.basis, confidence: 'corroborated',
106
+ score: match.score, windowStartMs: match.window_start ?? null, windowEndMs: match.window_end ?? null });
107
+ tied += 1;
108
+ }
109
+ if (tied) log(`[raytace][claude-code] ${sessionId.slice(0, 12)}: tied ${tied} sandbox process(es) to calls recorded after they ran`);
110
+ }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * The sandbox bridge: where a local sandbox's Claude Code hook
3
+ * (worker/sandbox/claude-hook.mjs) sends its transcript. A sandbox in the
4
+ * Lima VM reaches this machine's loopback at 192.168.5.2, and the VM's
5
+ * firewall (worker/sandbox/network.py) lets it reach this one port and no
6
+ * other, so it listens on 127.0.0.1 only.
7
+ *
8
+ * Each request carries the sandbox's token (set at create, kept in
9
+ * ~/.raytrace/sandboxes.json), which names the sandbox, and the transcript's
10
+ * next whole lines from `offset`. `ingest(sandbox, note)` does the rest (the
11
+ * proxy's ingestFromSandbox): an offset that is not where its copy ends is
12
+ * answered 409 with the copy's size, and the hook resends from there.
13
+ */
14
+ import { createServer } from 'node:http';
15
+ import { readFile } from 'node:fs/promises';
16
+ import { join } from 'node:path';
17
+
18
+ const MAX_BODY = 4_500_000;
19
+
20
+ export function startSandboxBridge({ dataDir, port, ingest }) {
21
+ async function sandboxFor(token) {
22
+ if (typeof token !== 'string' || token.length < 40) return null;
23
+ try {
24
+ const boxes = JSON.parse(await readFile(join(dataDir, 'sandboxes.json'), 'utf8'));
25
+ return Object.keys(boxes).find((id) => boxes[id]?.token === token) ?? null;
26
+ } catch { return null; }
27
+ }
28
+
29
+ const server = createServer(async (req, res) => {
30
+ const json = (status, body) => { res.writeHead(status, { 'content-type': 'application/json' }); res.end(JSON.stringify(body)); };
31
+ if (req.method === 'GET' && req.url === '/raytace/identity') {
32
+ return json(200, { service: 'raytrace-bridge', version: 1 });
33
+ }
34
+ if (req.method !== 'POST' || req.url !== '/raytace/ingest/claude-code') return json(404, { error: 'Unknown endpoint.' });
35
+ const sandbox = await sandboxFor(req.headers['x-raytace-sandbox-token']);
36
+ if (!sandbox) return json(403, { error: 'Unknown sandbox token.' });
37
+ const chunks = []; let size = 0;
38
+ for await (const part of req) { size += part.length; if (size > MAX_BODY) return json(413, { error: 'Request too large.' }); chunks.push(part); }
39
+ let note;
40
+ try { note = JSON.parse(Buffer.concat(chunks).toString('utf8')); } catch { return json(400, { error: 'Invalid JSON.' }); }
41
+ try {
42
+ const { status, body } = await ingest(sandbox, note);
43
+ json(status, body);
44
+ } catch (error) { json(500, { error: error.message }); }
45
+ });
46
+ server.on('error', (error) => console.error(`Sandbox bridge on :${port} did not start: ${error.message}. Local sandboxes' Claude Code sessions will not reach the dashboard.`));
47
+ server.listen(port, '127.0.0.1', () => console.log(`Sandbox bridge listening on http://127.0.0.1:${port}`));
48
+ return server;
49
+ }