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.
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,433 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { toExecutionEvent, toFailedExec, exitOutcome, msFromNs, processEvents, SandboxTracker, startGvisorForwarder, viewerAuth, clockFromSamples, UNMEASURED_CLOCK } from './gvisor-forwarder.mjs';
4
+
5
+ // Event shapes below mirror what worker/gvisor/collector.py writes and
6
+ // viewer.py serves (see `decode()` there): timestamp_ns / process_start_ns
7
+ // are strings because they don't fit a JS number losslessly.
8
+ const CONTAINER = 'c'.repeat(64);
9
+ const SESSION = 'rtp-' + 'a'.repeat(32);
10
+ const NS = (ms) => String(BigInt(ms) * 1_000_000n);
11
+ const exec = (event_id, pid, ppid, argv, ms = 1_700_000_000_000 + event_id) => ({ event_id, kind: 'exec_succeeded', pid, ppid, argv, container_id: CONTAINER, timestamp_ns: NS(ms), cwd: '/workspace' });
12
+ const exit = (event_id, pid, exit_code, signal = null, ms = 1_700_000_000_000 + event_id) => ({ event_id, kind: 'process_exit', pid, exit_code, signal, container_id: CONTAINER, timestamp_ns: NS(ms) });
13
+
14
+ test('msFromNs converts the collector\'s ns strings and rejects garbage', () => {
15
+ assert.equal(msFromNs('1700000000000000000'), 1_700_000_000_000);
16
+ assert.equal(msFromNs(''), null);
17
+ assert.equal(msFromNs(null), null);
18
+ assert.equal(msFromNs('not-a-number'), null);
19
+ assert.equal(msFromNs('0'), null);
20
+ });
21
+
22
+ test('toExecutionEvent maps exec_succeeded into the matcher shape with a collision-proof id', () => {
23
+ const raw = exec(42, 10, 1, ['/bin/bash', '-lc', 'git status']);
24
+ assert.deepEqual(toExecutionEvent(raw), {
25
+ id: `gvisor-${'c'.repeat(12)}-42`, pid: 10, ppid: 1, command: '/bin/bash -lc git status',
26
+ argv: ['/bin/bash', '-lc', 'git status'], cwd: '/workspace', timestamp: 1_700_000_000_042,
27
+ start_time_ns: null, call_marker: null, margin: 2000,
28
+ // The observation itself travels with it, for the evidence store.
29
+ container_id: CONTAINER, raw,
30
+ });
31
+ });
32
+
33
+ test('toExecutionEvent carries RAYTRACE_CALL_ID through, and ignores other markers', () => {
34
+ const marked = { ...exec(7, 10, 1, ['ls']), markers: { RAYTRACE_CALL_ID: 'call_abc', RAYTRACE_SESSION: 'rtp-x' } };
35
+ assert.equal(toExecutionEvent(marked).call_marker, 'call_abc');
36
+ // A session marker alone is not a per-call join; it must not become one.
37
+ assert.equal(toExecutionEvent({ ...exec(8, 10, 1, ['ls']), markers: { RAYTRACE_SESSION: 'rtp-x' } }).call_marker, null);
38
+ assert.equal(toExecutionEvent({ ...exec(9, 10, 1, ['ls']), markers: { RAYTRACE_CALL_ID: '' } }).call_marker, null);
39
+ });
40
+
41
+ test('recreated collector event IDs cannot overwrite earlier executions',()=>{
42
+ const raw={...exec(1,10,1,['ls']),source_instance_id:'boot-a',source_connection_id:'connection'};
43
+ assert.notEqual(toExecutionEvent(raw).id,toExecutionEvent({...raw,source_instance_id:'boot-b'}).id);
44
+ assert.notEqual(toExecutionEvent(raw).id,toExecutionEvent({...raw,source_connection_id:'replacement'}).id);
45
+ });
46
+
47
+ test('toExecutionEvent rejects anything that is not a usable exec', () => {
48
+ assert.equal(toExecutionEvent(exit(1, 10, 0)), null);
49
+ assert.equal(toExecutionEvent(exec(2, 10, 1, [])), null);
50
+ assert.equal(toExecutionEvent({ ...exec(3, 10, 1, ['ls']), timestamp_ns: 'nope' }), null);
51
+ assert.equal(toExecutionEvent({ ...exec(4, 10, 1, ['ls']), pid: 'x' }), null);
52
+ assert.equal(toExecutionEvent(null), null);
53
+ });
54
+
55
+ test('exitOutcome never upgrades an unreadable exit to success', () => {
56
+ assert.deepEqual(exitOutcome({ exit_code: 0, signal: null }), { status: 'completed', error: null, exit_code: 0 });
57
+ assert.deepEqual(exitOutcome({ exit_code: 2, signal: null }), { status: 'failed', error: 'exit 2', exit_code: 2 });
58
+ assert.deepEqual(exitOutcome({ exit_code: null, signal: 9 }), { status: 'failed', error: 'killed by signal 9', exit_code: null });
59
+ assert.deepEqual(exitOutcome({ exit_code: null, signal: null }), { status: 'unknown', error: null, exit_code: null });
60
+ assert.deepEqual(exitOutcome(undefined), { status: 'unknown', error: null, exit_code: null });
61
+ });
62
+
63
+ /** ingest() fake: matches when the command text contains one of the given
64
+ * substrings; records every call so tests can assert on what was sent. */
65
+ function fakeIngest(matchIf) {
66
+ const calls = [];
67
+ const ingest = async (events, opts) => {
68
+ calls.push({ events, opts });
69
+ const out = new Map();
70
+ for (const e of events) if (matchIf.some((needle) => e.command.includes(needle))) out.set(e.id, { call_id: `call-for-${e.id}`, score: 1, basis: 'text' });
71
+ return out;
72
+ };
73
+ return { ingest, calls };
74
+ }
75
+
76
+ test('a top-level bash -lc exec is forwarded and scoped to the sandbox session; its children are recorded, not matched', async () => {
77
+ const { ingest, calls } = fakeIngest(['git status']);
78
+ const finished = [];
79
+ const descended = [];
80
+ const tracker = new SandboxTracker();
81
+ const counts = await processEvents([
82
+ exec(1, 100, 1, ['/bin/bash', '-lc', 'git status']),
83
+ exec(2, 101, 100, ['git', 'status']), // child of the matched bash: never offered to the matcher
84
+ exit(3, 101, 0), // child exit: closes the CHILD's own row
85
+ exit(4, 100, 0), // the command itself finishing
86
+ ], { sessionId: SESSION, tracker, ingest, finish: async (row) => { finished.push(row); return true; },
87
+ descend: async (row) => { descended.push(row); } });
88
+
89
+ assert.deepEqual(counts, { forwarded: 1, matched: 1, finished: 2, descendants: 1, unattributed: 0, failedExecs: 0, lost: 0 });
90
+ assert.equal(calls.length, 1, 'only the top-level exec reached the matcher');
91
+ assert.equal(calls[0].opts.sessionId, SESSION);
92
+ assert.equal(calls[0].events[0].command, '/bin/bash -lc git status');
93
+
94
+ // The child is stored against the call its parent resolved to.
95
+ assert.equal(descended.length, 1);
96
+ assert.equal(descended[0].parent_call_id, `call-for-gvisor-${'c'.repeat(12)}-1`);
97
+ assert.equal(descended[0].tier, 'corroborated');
98
+ assert.equal(descended[0].exec.pid, 101);
99
+
100
+ // Each pid closes its OWN row -- a child's exit must never finish its parent.
101
+ assert.deepEqual(finished.map((r) => r.id), [`gvisor-${'c'.repeat(12)}-2`, `gvisor-${'c'.repeat(12)}-1`]);
102
+ assert.equal(tracker.cursor, 4);
103
+ assert.equal(tracker.live.size, 0, 'exits released both pids');
104
+ });
105
+
106
+ test('a grandchild inherits attribution: the one-level check never reached it', async () => {
107
+ // npm test -> sh -c node -e "...unlinkSync" -> node. The deletion is two
108
+ // levels down; before this it fell past the parent check into the matcher,
109
+ // scored too low against every candidate, and was discarded.
110
+ const { ingest } = fakeIngest(['npm test']);
111
+ const descended = [];
112
+ const tracker = new SandboxTracker();
113
+ const counts = await processEvents([
114
+ exec(1, 265, 156, ['/bin/bash', '-lc', 'npm test']),
115
+ exec(2, 277, 265, ['sh', '-c', 'node -e "require(\'fs\').unlinkSync(\'keep.txt\')"']),
116
+ exec(3, 278, 277, ['node', '-e', "require('fs').unlinkSync('keep.txt')"]),
117
+ ], { sessionId: SESSION, tracker, ingest, finish: async () => true, descend: async (row) => { descended.push(row); } });
118
+
119
+ assert.equal(counts.descendants, 2);
120
+ const call = `call-for-gvisor-${'c'.repeat(12)}-1`;
121
+ assert.deepEqual(descended.map((r) => [r.exec.pid, r.parent_call_id]), [[277, call], [278, call]]);
122
+ });
123
+
124
+ test('an exec that matches nothing is recorded as unattributed rather than discarded', async () => {
125
+ const { ingest } = fakeIngest([]); // nothing matches
126
+ const descended = [];
127
+ const tracker = new SandboxTracker();
128
+ const counts = await processEvents([
129
+ exec(1, 900, 1, ['curl', 'https://example.com']),
130
+ ], { sessionId: SESSION, tracker, ingest, finish: async () => true, descend: async (row) => { descended.push(row); } });
131
+
132
+ assert.deepEqual(counts, { forwarded: 1, matched: 0, finished: 0, descendants: 0, unattributed: 1, failedExecs: 0, lost: 0 });
133
+ assert.equal(descended.length, 1);
134
+ assert.equal(descended[0].parent_call_id, null, 'belongs to no proposed call');
135
+ assert.equal(descended[0].tier, 'unverified');
136
+ });
137
+
138
+ test('a same-pid re-exec (bash exec()ing its last command) is not offered as a separate candidate', async () => {
139
+ const { ingest, calls } = fakeIngest(['ls']);
140
+ const tracker = new SandboxTracker();
141
+ await processEvents([
142
+ exec(1, 100, 1, ['/bin/bash', '-lc', 'ls -la']),
143
+ exec(2, 100, 1, ['ls', '-la']), // same pid 100, ppid still 1
144
+ ], { sessionId: SESSION, tracker, ingest, finish: async () => true });
145
+ assert.equal(calls.length, 1, 'only the wrapper exec was ingested');
146
+ });
147
+
148
+ test('unmatched execs never occupy a pid, so their children remain eligible', async () => {
149
+ // e.g. the container entrypoint (`codex` itself) is a top-level exec that
150
+ // matches nothing -- its children (the real tool calls) must still flow.
151
+ const { ingest, calls } = fakeIngest(['npm test']);
152
+ const tracker = new SandboxTracker();
153
+ const counts = await processEvents([
154
+ exec(1, 1, 0, ['codex']),
155
+ exec(2, 50, 1, ['/bin/bash', '-lc', 'npm test']),
156
+ ], { sessionId: SESSION, tracker, ingest, finish: async () => true });
157
+ assert.deepEqual(counts, { forwarded: 2, matched: 1, finished: 0, descendants: 0, unattributed: 1, failedExecs: 0, lost: 0 });
158
+ assert.equal(calls.length, 2);
159
+ assert.equal(tracker.live.get(50).execId, `gvisor-${'c'.repeat(12)}-2`);
160
+ assert.equal(tracker.live.has(1), false);
161
+ });
162
+
163
+ test('a failing command closes out as failed with the exit code, a signal as killed', async () => {
164
+ const { ingest } = fakeIngest(['make', 'sleep']);
165
+ const finished = [];
166
+ const tracker = new SandboxTracker();
167
+ await processEvents([
168
+ exec(1, 7, 1, ['/bin/bash', '-lc', 'make']), exit(2, 7, 2),
169
+ exec(3, 8, 1, ['/bin/bash', '-lc', 'sleep 100']), exit(4, 8, null, 9),
170
+ ], { sessionId: SESSION, tracker, ingest, finish: async (row) => { finished.push(row); return true; } });
171
+ assert.deepEqual(finished.map((r) => [r.status, r.error]), [['failed', 'exit 2'], ['failed', 'killed by signal 9']]);
172
+ });
173
+
174
+ test('events are processed in id order regardless of page order, and only past the cursor', async () => {
175
+ const { ingest, calls } = fakeIngest(['one', 'two']);
176
+ const tracker = new SandboxTracker();
177
+ tracker.cursor = 10;
178
+ // Old viewers ignore `after` and return newest-first; the page also
179
+ // contains events at/below the cursor which must be ignored.
180
+ await processEvents([
181
+ exec(12, 20, 1, ['/bin/bash', '-lc', 'two']),
182
+ exec(11, 19, 1, ['/bin/bash', '-lc', 'one']),
183
+ exec(10, 18, 1, ['/bin/bash', '-lc', 'already seen']),
184
+ exec(9, 17, 1, ['/bin/bash', '-lc', 'older']),
185
+ ], { sessionId: SESSION, tracker, ingest, finish: async () => true });
186
+ assert.deepEqual(calls.map((c) => c.events[0].command), ['/bin/bash -lc one', '/bin/bash -lc two']);
187
+ assert.equal(tracker.cursor, 12);
188
+ });
189
+
190
+ test('malformed pages and events never throw', async () => {
191
+ const tracker = new SandboxTracker();
192
+ const deps = { sessionId: SESSION, tracker, ingest: async () => new Map(), finish: async () => true };
193
+ const none = { forwarded: 0, matched: 0, finished: 0, descendants: 0, unattributed: 0, failedExecs: 0, lost: 0 };
194
+ assert.deepEqual(await processEvents(undefined, deps), none);
195
+ assert.deepEqual(await processEvents([null, {}, { kind: 'exec_succeeded' }, { event_id: 'x', kind: 'process_exit' }], deps), none);
196
+ });
197
+
198
+ test('startGvisorForwarder discovers sandboxes from the manager and tails each container from the viewer', async () => {
199
+ const requested = [];
200
+ const fetchImpl = async (url) => {
201
+ requested.push(url);
202
+ if (url.endsWith('/api/projects')) {
203
+ return { ok: true, json: async () => [
204
+ { id: SESSION, container_id: CONTAINER, status: 'running' },
205
+ { id: 'rtp-short', container_id: CONTAINER }, // invalid ids are ignored
206
+ { id: 'rtp-' + 'b'.repeat(32), container_id: null }, // no container yet: ignored
207
+ ] };
208
+ }
209
+ // limit=1 is the cursor seed probe; this container has no history yet.
210
+ if (url.includes('limit=1')) return { ok: true, json: async () => ({ events: [], next_before: null }) };
211
+ if (url.includes('/events?')) return { ok: true, json: async () => ({ events: [exec(1, 5, 1, ['/bin/bash', '-lc', 'pwd']), exit(2, 5, 0)], next_before: null }) };
212
+ if(url.includes('/coverage?'))return {ok:false,status:404};
213
+ throw new Error('unexpected ' + url);
214
+ };
215
+ const { ingest } = fakeIngest(['pwd']);
216
+ const finished = [];
217
+ const logs = [];
218
+ const forwarder = startGvisorForwarder({ ingest, finish: async (row) => { finished.push(row); return true; }, fetchImpl, log: (m) => logs.push(m), pollMs: 60_000, discoverMs: 60_000 });
219
+ try {
220
+ await forwarder.discover();
221
+ assert.equal(forwarder.sandboxes.size, 1);
222
+ assert.equal(forwarder.sandboxes.get(CONTAINER).sessionId, SESSION);
223
+ await forwarder.poll();
224
+ const viewerUrl = requested.find((u) => u.includes('limit=500'));
225
+ assert.match(viewerUrl, new RegExp(`container=${CONTAINER}&after=0&limit=500&processes=1`));
226
+ assert.equal(finished.length, 1);
227
+ assert.equal(forwarder.sandboxes.get(CONTAINER).tracker.cursor, 2);
228
+ assert.ok(logs.some((m) => m.includes('forwarding evidence for sandbox')));
229
+ assert.ok(logs.some((m) => m.includes('1 exec(s) matched')));
230
+ // A second poll asks from the advanced cursor.
231
+ await forwarder.poll();
232
+ assert.ok(requested.some((u) => u.includes('&after=2&')));
233
+ } finally { forwarder.stop(); }
234
+ });
235
+
236
+ test('a newly discovered sandbox resumes a durable checkpoint instead of skipping history', async () => {
237
+ // The collector's evidence store lives in the VM and is append-only, so it
238
+ // outlives the proxy. Beginning at 0 re-reads every past session: none of it
239
+ // can match (no proposed calls were captured then), so it all lands as
240
+ // unattributed rows and pushes the live session behind days of backlog.
241
+ const requested = [];
242
+ const fetchImpl = async (url) => {
243
+ requested.push(url);
244
+ if (url.endsWith('/api/projects')) return { ok: true, json: async () => [{ id: SESSION, container_id: CONTAINER }] };
245
+ if (url.includes('limit=1')) return { ok: true, json: async () => ({ events: [exec(4096, 9, 1, ['old', 'history'])] }) };
246
+ return { ok: true, json: async () => ({ events: [] }) };
247
+ };
248
+ const logs = [];
249
+ const forwarder = startGvisorForwarder({ ingest: async () => new Map(), finish: async () => true, loadCheckpoint:async()=>4096, fetchImpl, log: (m) => logs.push(m), pollMs: 60_000, discoverMs: 60_000 });
250
+ try {
251
+ await forwarder.discover();
252
+ assert.equal(forwarder.sandboxes.get(CONTAINER).tracker.cursor, 0, 'does not skip history at discovery');
253
+ await forwarder.poll();
254
+ assert.ok(requested.some((u) => u.includes('&after=4096&')), 'tails forward from there');
255
+ assert.equal(forwarder.sandboxes.get(CONTAINER).tracker.cursor,4096,'loads the saved checkpoint on first poll');
256
+ } finally { forwarder.stop(); }
257
+ });
258
+
259
+ test('an unreachable viewer during discovery falls back to replaying rather than skipping evidence', async () => {
260
+ const fetchImpl = async (url) => {
261
+ if (url.endsWith('/api/projects')) return { ok: true, json: async () => [{ id: SESSION, container_id: CONTAINER }] };
262
+ throw new Error('ECONNREFUSED');
263
+ };
264
+ const forwarder = startGvisorForwarder({ ingest: async () => new Map(), finish: async () => true, fetchImpl, log: () => {}, pollMs: 60_000, discoverMs: 60_000 });
265
+ try {
266
+ await forwarder.discover();
267
+ // Over-inclusive is recoverable; starting blind loses evidence silently.
268
+ assert.equal(forwarder.sandboxes.get(CONTAINER).tracker.cursor, 0);
269
+ } finally { forwarder.stop(); }
270
+ });
271
+
272
+ test('startGvisorForwarder logs an unreachable manager once and stays idle rather than failing', async () => {
273
+ const logs = [];
274
+ const forwarder = startGvisorForwarder({ ingest: async () => new Map(), finish: async () => true, fetchImpl: async () => { throw new Error('ECONNREFUSED'); }, log: (m) => logs.push(m), pollMs: 60_000, discoverMs: 60_000 });
275
+ try {
276
+ await forwarder.discover();
277
+ await forwarder.discover();
278
+ assert.equal(logs.filter((m) => m.includes('sandbox manager not reachable')).length, 1);
279
+ await forwarder.poll(); // nothing to poll; must not throw
280
+ assert.equal(forwarder.sandboxes.size, 0);
281
+ } finally { forwarder.stop(); }
282
+ });
283
+
284
+ const BASE = 1_700_000_000_000;
285
+ const PREFIX = `gvisor-${'c'.repeat(12)}-`;
286
+
287
+ test('clockFromSamples trusts the shortest round trip, and is unmeasured with no samples', () => {
288
+ assert.deepEqual(clockFromSamples([{ offsetMs: 3100, rttMs: 40 }, { offsetMs: 2990, rttMs: 4 }, { offsetMs: 3500, rttMs: 900 }]),
289
+ { offsetMs: 2990, marginMs: 252, measured: true });
290
+ assert.equal(clockFromSamples([]), UNMEASURED_CLOCK);
291
+ assert.equal(clockFromSamples([{ offsetMs: NaN, rttMs: 1 }]), UNMEASURED_CLOCK);
292
+ });
293
+
294
+ test('exec times are shifted onto the proxy clock, carrying the measured margin', () => {
295
+ // The VM clock runs 3 s ahead: an exec it stamps at +3 s happened at +0 s for us.
296
+ const event = toExecutionEvent(exec(1, 10, 1, ['ls'], BASE + 3000), { offsetMs: 3000, marginMs: 260 });
297
+ assert.equal(event.timestamp, BASE);
298
+ assert.equal(event.margin, 260);
299
+ });
300
+
301
+ test('a failed execve under a matched command gets its own failed row instead of vanishing', async () => {
302
+ const tracker = new SandboxTracker();
303
+ tracker.live.set(100, { execId: `${PREFIX}1`, callId: 'call_x' });
304
+ const descended = [];
305
+ const failed = { event_id: 5, kind: 'exec_failed', pid: 101, ppid: 100, argv: ['./script.sh'], errno: 13, container_id: CONTAINER, timestamp_ns: NS(BASE + 5) };
306
+ const counts = await processEvents([failed], { sessionId: SESSION, tracker, ingest: async () => new Map(), finish: async () => true,
307
+ descend: async (row) => { descended.push(row); } });
308
+ assert.equal(counts.failedExecs, 1);
309
+ assert.equal(descended[0].parent_call_id, 'call_x');
310
+ assert.equal(descended[0].tier, 'corroborated');
311
+ assert.equal(descended[0].exec.status, 'failed');
312
+ assert.equal(descended[0].exec.error, 'exec failed: errno 13');
313
+ assert.equal(descended[0].exec.ended_at, new Date(BASE + 5).toISOString());
314
+ assert.equal(tracker.live.has(101), false, 'nothing ran, so nothing is tracked');
315
+ assert.equal(toFailedExec({ ...failed, argv: [], executable: '' }), null);
316
+ });
317
+
318
+ test('an exit with no in-memory link (proxy restarted) is closed by pid + start time', async () => {
319
+ const closed = [];
320
+ const counts = await processEvents([{ ...exit(9, 42, 3), process_start_ns: '555' }], {
321
+ sessionId: SESSION, tracker: new SandboxTracker(), ingest: async () => new Map(), finish: async () => { throw new Error('no live entry to finish'); },
322
+ finishByProcess: async (row) => { closed.push(row); return true; } });
323
+ assert.equal(counts.finished, 1);
324
+ const { raw, ...row } = closed[0];
325
+ assert.deepEqual(row, { container_id: CONTAINER, session_id: SESSION, pid: 42, start_time_ns: '555', ended_at: new Date(BASE + 9).toISOString(),
326
+ ended_ms: BASE + 9, status: 'failed', error: 'exit 3', exit_code: 3, signal: null });
327
+ assert.equal(raw.event_id, 9);
328
+ });
329
+
330
+ test('a sandbox\'s collector disconnecting marks its open rows unknown and forgets its live pids', async () => {
331
+ const tracker = new SandboxTracker();
332
+ tracker.live.set(100, { execId: `${PREFIX}1`, callId: 'call_x' });
333
+ const lost = [];
334
+ const counts = await processEvents([{ event_id: 7, kind: 'collector_disconnected', container_id: CONTAINER }], {
335
+ sessionId: SESSION, tracker, ingest: async () => new Map(), finish: async () => true,
336
+ lose: async (row) => { lost.push(row); return 2; } });
337
+ assert.deepEqual(lost, [{ container_id: CONTAINER, session_id: SESSION, reason: 'collector disconnected', raw: { event_id: 7, kind: 'collector_disconnected', container_id: CONTAINER } }]);
338
+ assert.equal(counts.lost, 2);
339
+ assert.equal(tracker.live.size, 0);
340
+ });
341
+
342
+ test('startGvisorForwarder measures the VM clock, falls back when it cannot, and closes out removed sandboxes', async () => {
343
+ let listed = true;
344
+ let timeUp = true;
345
+ const fetchImpl = async (url) => {
346
+ if (url.endsWith('/api/projects')) return { ok: true, json: async () => (listed ? [{ id: SESSION, container_id: CONTAINER }] : []) };
347
+ if (url.endsWith('/time')) {
348
+ if (!timeUp) throw new Error('connection refused');
349
+ return { ok: true, json: async () => ({ realtime_ns: NS(Date.now() + 3000) }) };
350
+ }
351
+ if (url.includes('/events?')) return { ok: true, json: async () => ({ events: [] }) };
352
+ if(url.includes('/coverage?'))return {ok:false,status:404};
353
+ throw new Error('unexpected ' + url);
354
+ };
355
+ const lost = [];
356
+ const logs = [];
357
+ const forwarder = startGvisorForwarder({ ingest: async () => new Map(), finish: async () => true, lose: async (row) => { lost.push(row); return 1; },
358
+ fetchImpl, log: (m) => logs.push(m), pollMs: 60_000, discoverMs: 60_000 });
359
+ try {
360
+ await forwarder.discover();
361
+ assert.equal(forwarder.clock().measured, true);
362
+ assert.ok(Math.abs(forwarder.clock().offsetMs - 3000) < 100, `offset ${forwarder.clock().offsetMs}`);
363
+
364
+ timeUp = false;
365
+ await forwarder.discover();
366
+ await forwarder.discover();
367
+ assert.equal(forwarder.clock().measured, true, 'a failed sample keeps the last good mapping');
368
+ assert.equal(logs.filter((m) => m.includes('could not read the sandbox clock')).length, 1, 'logged once per outage');
369
+
370
+ listed = false;
371
+ await forwarder.discover();
372
+ assert.equal(forwarder.sandboxes.size, 0);
373
+ assert.deepEqual(lost, [{ container_id: CONTAINER, session_id: SESSION, reason: 'sandbox removed', ended: true }]);
374
+ assert.ok(logs.some((m) => m.includes('is gone') && m.includes('1 open execution(s) marked unknown')));
375
+ } finally { forwarder.stop(); }
376
+ });
377
+
378
+ test('the viewer gets its token and the manager does not', async () => {
379
+ const seen = [];
380
+ const fetchImpl = async (url, init) => {
381
+ seen.push([url, init.headers.authorization]);
382
+ if (url.endsWith('/api/projects')) return { ok: true, json: async () => [{ id: SESSION, container_id: CONTAINER }] };
383
+ if (url.endsWith('/time')) return { ok: true, json: async () => ({ realtime_ns: String(Date.now() * 1e6) }) };
384
+ return { ok: true, json: async () => ({ events: [] }) };
385
+ };
386
+ const forwarder = startGvisorForwarder({ ingest: async () => new Map(), finish: async () => true, fetchImpl, viewerHeaders: () => ({ authorization: 'Bearer viewer' }), log: () => {}, pollMs: 1e6, discoverMs: 1e6, initialDelayMs: 1e6 });
387
+ try {
388
+ await forwarder.discover();
389
+ await forwarder.poll();
390
+ assert.ok(seen.some(([url]) => url.includes('/events?')));
391
+ for (const [url, header] of seen) assert.equal(header, url.endsWith('/api/projects') ? undefined : 'Bearer viewer', url);
392
+ } finally { forwarder.stop(); }
393
+ });
394
+
395
+ test('viewerAuth prefers the environment and sends nothing without a token', () => {
396
+ const saved = process.env.RAYTACE_VIEWER_TOKEN;
397
+ try {
398
+ process.env.RAYTACE_VIEWER_TOKEN = 'from-env';
399
+ assert.deepEqual(viewerAuth('/nonexistent'), { authorization: 'Bearer from-env' });
400
+ delete process.env.RAYTACE_VIEWER_TOKEN;
401
+ assert.deepEqual(viewerAuth('/nonexistent'), {});
402
+ } finally { if (saved) process.env.RAYTACE_VIEWER_TOKEN = saved; }
403
+ });
404
+
405
+ test('a failed evidence write leaves the cursor unchanged and a retry records the event',async()=>{
406
+ const tracker=new SandboxTracker();let fails=true;
407
+ const deps={sessionId:SESSION,tracker,ingest:async()=>new Map(),finish:()=>{},descend:async()=>{if(fails)throw Error('disk full');}};
408
+ const rows=[exec(10,20,1,['npm','test'])];
409
+ await assert.rejects(processEvents(rows,deps),/disk full/);assert.equal(tracker.cursor,0);
410
+ fails=false;await processEvents(rows,deps);assert.equal(tracker.cursor,10);
411
+ });
412
+ test('a failed final raw-event write is retried without advancing past it',async()=>{
413
+ const tracker=new SandboxTracker();const event={event_id:1,kind:'collection_gap',container_id:CONTAINER,received_ms:100};
414
+ await assert.rejects(processEvents([event],{sessionId:SESSION,tracker,ingest:async()=>new Map(),finish:()=>{},recordCapture:async()=>{throw Error('write failed');}}));
415
+ assert.equal(tracker.cursor,0);
416
+ });
417
+
418
+ test('a failed exit write restores the tracked process for retry',async()=>{
419
+ const tracker=new SandboxTracker();tracker.live.set(20,{execId:'execution',callId:'call'});
420
+ let fail=true,finished=0;
421
+ const deps={sessionId:SESSION,tracker,ingest:async()=>new Map(),finish:async()=>{if(fail)throw Error('disk full');finished++;}};
422
+ const event={event_id:2,kind:'process_exit',pid:20,exit_code:1,container_id:CONTAINER};
423
+ await assert.rejects(processEvents([event],deps),/disk full/);
424
+ assert.equal(tracker.cursor,0);assert.equal(tracker.live.get(20).execId,'execution');
425
+ fail=false;await processEvents([event],deps);assert.equal(finished,1);assert.equal(tracker.cursor,2);
426
+ });
427
+
428
+ test('a source checkpoint failure does not reject the background poll and retries on the next poll',async()=>{
429
+ let fail=true,saved=0;
430
+ const fetchImpl=async url=>({ok:true,json:async()=>url.endsWith('/api/projects')?[{id:SESSION,container_id:CONTAINER}]:url.includes('/coverage?')?{source_instance_id:'boot',streams:[],latest:0}:{events:[]}});
431
+ const forwarder=startGvisorForwarder({ingest:async()=>new Map(),finish:()=>{},fetchImpl,log:()=>{},saveProgress:async()=>{if(fail)throw Error('disk full');saved++;},pollMs:60000,discoverMs:60000});
432
+ try{await forwarder.discover();await assert.doesNotReject(forwarder.poll());fail=false;await forwarder.poll();assert.equal(saved,1);}finally{forwarder.stop();}
433
+ });
@@ -0,0 +1,227 @@
1
+ import { DatabaseSync } from 'node:sqlite';
2
+ import { createHash } from 'node:crypto';
3
+ import { readFileSync, realpathSync, existsSync } from 'node:fs';
4
+ import { resolve } from 'node:path';
5
+ import { fileURLToPath } from 'node:url';
6
+
7
+ export const SCHEMA_VERSION = 7;
8
+ // Forward steps for databases created by an older schema, keyed by the
9
+ // version they upgrade FROM. Each must reproduce the schema files exactly:
10
+ // the structure check below still runs afterwards, so a step that drifts from
11
+ // the .sql text is refused rather than silently accepted.
12
+ const MIGRATIONS = {
13
+ 6: readFileSync(new URL('./schema/capture_health.sql', import.meta.url), 'utf8'),
14
+ 2: `CREATE TABLE call_assessments (
15
+ key TEXT NOT NULL PRIMARY KEY,
16
+ call_id TEXT,
17
+ model TEXT,
18
+ created_at_ms INTEGER NOT NULL CHECK (typeof(created_at_ms)='integer'),
19
+ body_json TEXT NOT NULL CHECK (json_valid(body_json))
20
+ );
21
+ CREATE INDEX call_assessments_call ON call_assessments(call_id, created_at_ms);`,
22
+ 3: `CREATE TABLE workspace_snapshots (
23
+ id TEXT NOT NULL PRIMARY KEY,
24
+ session_id TEXT NOT NULL,
25
+ agent_session_id TEXT,
26
+ taken_on TEXT NOT NULL CHECK (taken_on IN ('prompt','tool')),
27
+ call_id TEXT,
28
+ commit_sha TEXT NOT NULL,
29
+ parent_sha TEXT,
30
+ changed INTEGER NOT NULL CHECK (changed IN (0,1)),
31
+ taken_at_ms INTEGER NOT NULL CHECK (typeof(taken_at_ms)='integer'),
32
+ changes_json TEXT NOT NULL CHECK (json_valid(changes_json))
33
+ );
34
+ CREATE INDEX workspace_snapshots_session ON workspace_snapshots(session_id, taken_at_ms);
35
+ CREATE INDEX workspace_snapshots_call ON workspace_snapshots(call_id);`,
36
+ // No table changes. Version 5 seeds catalog version 2 (every syscall group,
37
+ // not files only) beside whatever catalog the database already holds: the
38
+ // classification tables are append-only, so the older rows stay.
39
+ 4: '',
40
+ // Version 6 adds the derived search index. Read from the schema file
41
+ // itself so the step cannot drift from what a fresh database gets.
42
+ 5: readFileSync(new URL('./schema/search.sql', import.meta.url), 'utf8'),
43
+ };
44
+ /** The version from which a database holds catalog version 2. */
45
+ const CATALOG_SINCE = 5;
46
+ export const CATALOG_VERSION_ID = 'syscall-catalog-v2';
47
+ const modules = ['agent_intent.sql', 'runtime_evidence.sql', 'classification.sql', 'file_context.sql', 'shared_storage.sql', 'auxiliary.sql', 'search.sql', 'capture_health.sql'];
48
+ const fileLabels = [
49
+ ['source_code', 'Source code', 'Program source files.'],
50
+ ['configuration', 'Configuration', 'Configuration for applications or tools.'],
51
+ ['potentially_sensitive', 'Potentially sensitive', 'May contain secrets or other sensitive information; not proof of contents.'],
52
+ ['documentation', 'Documentation', 'Project documentation and notes.'],
53
+ ['generated_output', 'Generated output', 'Files generated by tools or builds.'],
54
+ ['temporary', 'Temporary', 'Files intended for temporary use.'],
55
+ ];
56
+
57
+ function schemaSql() {
58
+ return modules.map(name => {
59
+ let sql = readFileSync(new URL(`./schema/${name}`, import.meta.url), 'utf8');
60
+ // The agent-only script remains directly importable. The combined loader
61
+ // owns the transaction and foreign-key PRAGMA for all modules.
62
+ if (name === 'agent_intent.sql') {
63
+ sql = sql.replace(/^PRAGMA foreign_keys = ON;\r?\n/m, '')
64
+ .replace(/^BEGIN TRANSACTION;\r?\n/m, '').replace(/^COMMIT;\s*$/m, '');
65
+ }
66
+ return sql;
67
+ }).join('\n');
68
+ }
69
+
70
+ function structure(db) {
71
+ return db.prepare("SELECT type,name,tbl_name,sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%' ORDER BY type,name").all();
72
+ }
73
+
74
+ function catalogSeed() {
75
+ const source = new DatabaseSync(':memory:');
76
+ try {
77
+ source.exec(readFileSync(new URL('../worker/sandbox/syscall_catalog.sql', import.meta.url), 'utf8'));
78
+ const versions = source.prepare('SELECT DISTINCT catalog_version FROM syscall_catalog').all();
79
+ if (versions.length !== 1 || versions[0].catalog_version !== 2) throw Error('Unsupported source catalog version');
80
+ const levels = source.prepare('SELECT level,label,description FROM syscall_impact_levels ORDER BY level').all();
81
+ const categories = source.prepare('SELECT category_id AS id,label,description FROM syscall_categories ORDER BY category_id').all();
82
+ const rules = source.prepare(`SELECT syscall_name,description,category_id,default_impact_level,
83
+ classification_rule AS rule_identifier,required_evidence,interpretation_notes
84
+ FROM syscall_catalog ORDER BY syscall_name`).all().map(({ required_evidence, ...row }) => ({
85
+ ...row, required_evidence_json: JSON.stringify([required_evidence]),
86
+ }));
87
+ const digest = createHash('sha256').update(JSON.stringify({ catalog_version: '2', levels, categories, rules })).digest('hex');
88
+ return { levels, categories, rules, digest };
89
+ } finally { source.close(); }
90
+ }
91
+
92
+ function same(actual, expected, description) {
93
+ // Compare values independent of object property insertion order.
94
+ const canonical = value => Array.isArray(value) ? value.map(canonical)
95
+ : value && typeof value === 'object' ? Object.fromEntries(Object.keys(value).sort().map(k => [k, canonical(value[k])])) : value;
96
+ if (JSON.stringify(canonical(actual)) !== JSON.stringify(canonical(expected))) {
97
+ throw Error(`Incompatible ${description}; refusing to overwrite existing data`);
98
+ }
99
+ }
100
+
101
+ function insertOrVerify(db, table, key, row, fresh) {
102
+ const existing = db.prepare(`SELECT * FROM ${table} WHERE ${key}=?`).get(row[key]);
103
+ if (existing) same(existing, row, `${table} seed ${row[key]}`);
104
+ else {
105
+ if (!fresh) throw Error(`Missing ${table} seed ${row[key]}`);
106
+ db.prepare(`INSERT INTO ${table} (${Object.keys(row).join(',')}) VALUES (${Object.keys(row).map(() => '?').join(',')})`).run(...Object.values(row));
107
+ }
108
+ }
109
+
110
+ /** `adding` is true for a database that predates this catalog version: its
111
+ * new categories, version and rules are then added, not reported missing. */
112
+ function seed(db, data, fresh, adding = false) {
113
+ for (const row of data.levels) insertOrVerify(db, 'classification_impact_levels', 'level', row, fresh);
114
+ for (const row of data.categories) insertOrVerify(db, 'classification_categories', 'id', row, fresh || adding);
115
+ const version = db.prepare('SELECT * FROM classification_versions WHERE id=?').get(CATALOG_VERSION_ID);
116
+ if (version) {
117
+ same({ catalog_version: version.catalog_version, classifier_version: version.classifier_version, content_sha256: version.content_sha256 },
118
+ { catalog_version: '2', classifier_version: 'not-implemented', content_sha256: data.digest }, 'catalog version');
119
+ const rules = db.prepare(`SELECT syscall_name,description,category_id,default_impact_level,rule_identifier,
120
+ required_evidence_json,interpretation_notes FROM classification_syscall_rules WHERE version_id=? ORDER BY syscall_name`).all(CATALOG_VERSION_ID);
121
+ same(rules, data.rules, 'catalog rules');
122
+ } else {
123
+ if (!fresh && !adding) throw Error('Missing catalog version in initialized database');
124
+ db.prepare('INSERT INTO classification_versions VALUES (?,?,?,?,?,?)').run(
125
+ CATALOG_VERSION_ID, '2', 'not-implemented', data.digest, Date.now(),
126
+ 'Syscall catalog version 2, every group. No classifier writes results here: the sandbox host draws its facts from the same catalog.');
127
+ for (const row of data.rules) {
128
+ const full = { version_id: CATALOG_VERSION_ID, ...row };
129
+ db.prepare(`INSERT INTO classification_syscall_rules (${Object.keys(full).join(',')}) VALUES (${Object.keys(full).map(() => '?').join(',')})`).run(...Object.values(full));
130
+ }
131
+ }
132
+ for (const [id, label, description] of fileLabels) insertOrVerify(db, 'file_labels', 'id', { id, label, description }, fresh);
133
+ }
134
+
135
+ /** Verifies an existing database is compatible without mutating it. */
136
+ export function verifyEvidenceDatabase(outputPath) {
137
+ if (typeof outputPath !== 'string' || !outputPath.trim() || outputPath === ':memory:') {
138
+ throw Error('An explicit standalone database file path is required');
139
+ }
140
+ if (!existsSync(outputPath)) {
141
+ throw Error(`Database file does not exist: ${outputPath}`);
142
+ }
143
+ const path = realpathSync(outputPath);
144
+ const sql = schemaSql();
145
+ const reference = new DatabaseSync(':memory:');
146
+ let db;
147
+ try {
148
+ reference.exec('PRAGMA foreign_keys=ON');
149
+ reference.exec(sql);
150
+ const expected = structure(reference);
151
+ db = new DatabaseSync(path, { readOnly: true });
152
+ const existing = structure(db);
153
+ const version = db.prepare('PRAGMA user_version').get()?.user_version;
154
+ if (version !== SCHEMA_VERSION) {
155
+ throw Error(`Incompatible schema version ${version}; expected ${SCHEMA_VERSION}`);
156
+ }
157
+ same(existing, expected, 'schema structure');
158
+ return { path, schemaVersion: version };
159
+ } finally {
160
+ db?.close();
161
+ reference.close();
162
+ }
163
+ }
164
+
165
+ /** Creates a new standalone database, or verifies an existing compatible one. */
166
+ export function initializeEvidenceDatabase(outputPath) {
167
+ if (typeof outputPath !== 'string' || !outputPath.trim() || outputPath === ':memory:') {
168
+ throw Error('An explicit standalone database file path is required');
169
+ }
170
+ const path = existsSync(outputPath) ? realpathSync(outputPath) : resolve(outputPath);
171
+ const livePaths = [resolve('.raytace/raytace.db'), '/var/lib/raytace/runtime.db'];
172
+ if (livePaths.includes(path)) throw Error('Refusing a live database path');
173
+ const sql = schemaSql();
174
+ const data = catalogSeed();
175
+ const reference = new DatabaseSync(':memory:');
176
+ let db;
177
+ try {
178
+ reference.exec('PRAGMA foreign_keys=ON');
179
+ reference.exec(sql);
180
+ const expected = structure(reference);
181
+ db = new DatabaseSync(path);
182
+ db.exec('PRAGMA foreign_keys=ON; PRAGMA busy_timeout=5000; BEGIN IMMEDIATE');
183
+ try {
184
+ const existing = structure(db);
185
+ const version = db.prepare('PRAGMA user_version').get().user_version;
186
+ const fresh = existing.length === 0 && version === 0;
187
+ // The RayTrace CLI's own first schema (version 1) was the agent-intent,
188
+ // shared-storage and auxiliary modules alone, before the CLI took the
189
+ // sandbox schema on. Everything it lacks is added; everything it has must
190
+ // match exactly, which the structure check below still makes sure of.
191
+ const cliFirst = !fresh && version === 1 && !existing.some((object) => object.name === 'runtime_sandboxes');
192
+ if (fresh) db.exec(sql);
193
+ else if (cliFirst) {
194
+ const have = new Set(existing.map((object) => `${object.type}:${object.name}`));
195
+ const missing = expected.filter((object) => object.sql && !have.has(`${object.type}:${object.name}`));
196
+ for (const type of ['table', 'index', 'trigger', 'view']) {
197
+ // A full-text table makes its own shadow tables: one made that way is skipped.
198
+ const exists = db.prepare('SELECT 1 FROM sqlite_schema WHERE type = ? AND name = ?');
199
+ for (const object of missing.filter((item) => item.type === type)) if (!exists.get(object.type, object.name)) db.exec(object.sql);
200
+ }
201
+ same(structure(db), expected, 'schema structure');
202
+ } else {
203
+ if (!(version in MIGRATIONS) && version !== SCHEMA_VERSION) throw Error(`Incompatible schema version ${version}`);
204
+ for (let step = version; step < SCHEMA_VERSION; step += 1) if (MIGRATIONS[step]) db.exec(MIGRATIONS[step]);
205
+ same(structure(db), expected, 'schema structure');
206
+ }
207
+ seed(db, data, fresh || cliFirst, !fresh && version < CATALOG_SINCE);
208
+ if (db.prepare('PRAGMA foreign_key_check').all().length) throw Error('Foreign key integrity check failed');
209
+ db.exec(`PRAGMA user_version=${SCHEMA_VERSION}; COMMIT`);
210
+ return { path, schemaVersion: SCHEMA_VERSION, tables: expected.filter(o => o.type === 'table').length,
211
+ catalogVersion: CATALOG_VERSION_ID, catalogRules: data.rules.length, created: fresh };
212
+ } catch (error) {
213
+ db.exec('ROLLBACK');
214
+ throw error;
215
+ }
216
+ } finally { db?.close(); reference.close(); }
217
+ }
218
+
219
+ if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
220
+ if (process.argv.length !== 3) {
221
+ console.error('Usage: node proxy/init-evidence-db.mjs <standalone-database-path>');
222
+ process.exitCode = 1;
223
+ } else {
224
+ try { console.log(JSON.stringify(initializeEvidenceDatabase(process.argv[2]), null, 2)); }
225
+ catch (error) { console.error(error.message); process.exitCode = 1; }
226
+ }
227
+ }