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,240 @@
1
+ import { test } from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { DatabaseSync } from 'node:sqlite';
4
+ import { mkdtempSync, rmSync } from 'node:fs';
5
+ import { tmpdir } from 'node:os';
6
+ import { join } from 'node:path';
7
+ import { initializeEvidenceDatabase, CATALOG_VERSION_ID, SCHEMA_VERSION } from './init-evidence-db.mjs';
8
+
9
+ function isolated(run) {
10
+ const dir = mkdtempSync(join(tmpdir(), 'raytace-evidence-test-'));
11
+ const path = join(dir, 'evidence.db');
12
+ let db;
13
+ try {
14
+ initializeEvidenceDatabase(path);
15
+ db = new DatabaseSync(path);
16
+ db.exec('PRAGMA foreign_keys=ON');
17
+ return run(db, path, dir);
18
+ } finally { db?.close(); rmSync(dir, { recursive: true, force: true }); }
19
+ }
20
+
21
+ function insert(db, table, values, prefix = 'INSERT') {
22
+ db.prepare(`${prefix} INTO ${table} (${Object.keys(values).join(',')}) VALUES (${Object.keys(values).map(() => '?').join(',')})`).run(...Object.values(values));
23
+ }
24
+
25
+ function fixtures(db) {
26
+ for (const id of ['a', 'b']) {
27
+ insert(db, 'runtime_sandboxes', { id, external_container_id: id, runtime_name: 'gvisor', runtime_version: 'test', architecture: 'aarch64', capture_config_json: '{}' });
28
+ insert(db, 'runtime_processes', { id: `p-${id}`, sandbox_id: id, pid: 101, first_seen_ns: '9007199254740993000', identity_basis: 'test' });
29
+ }
30
+ insert(db, 'agent_sessions', { id: 's', started_at_ms: 100 });
31
+ insert(db, 'agent_turns', { id: 't', session_id: 's', sequence_number: 0, started_at_ms: 100 });
32
+ insert(db, 'agent_exchanges', { id: 'x', turn_id: 't', session_id: 's', provider: 'test', started_at_ms: 100 });
33
+ insert(db, 'agent_tool_calls', { id: 'tool', exchange_id: 'x', output_index: 0, tool_name: 'exec_command' });
34
+ }
35
+
36
+ let sequence = 0;
37
+ function event(db, id, overrides = {}) {
38
+ insert(db, 'runtime_events', { id, sandbox_id: 'a', process_id: 'p-a', stream_id: 'stream', stream_sequence: sequence++,
39
+ event_kind: 'syscall_enter', observed_at_ns: '9007199254740993001', received_at_ms: 100,
40
+ tid: 101, payload_json: '{"syscall_name":"unlinkat","syscall_number":35}', decoder_version: 'test', ...overrides });
41
+ }
42
+
43
+ function call(db, id, options = {}) {
44
+ const { outcome = 'succeeded', ...overrides } = options;
45
+ event(db, `${id}-entry`);
46
+ event(db, `${id}-exit`, { event_kind: 'syscall_exit', payload_json: JSON.stringify({ syscall_name: 'unlinkat', syscall_number: 35,
47
+ return_value: outcome === 'failed' ? '-1' : '0', errno: outcome === 'failed' ? 13 : 0 }) });
48
+ const row = { id, sandbox_id: 'a', process_id: 'p-a', tid: 101, syscall_name: 'unlinkat', syscall_number: 35,
49
+ entry_event_id: `${id}-entry`, exit_event_id: `${id}-exit`, arguments_json: '{"pathname":"notes.md"}',
50
+ return_value: outcome === 'failed' ? '-1' : '0', errno: outcome === 'failed' ? 13 : 0,
51
+ outcome, correlation_status: 'paired', normalizer_version: 'test', ...overrides };
52
+ insert(db, 'runtime_syscall_calls', row);
53
+ return row;
54
+ }
55
+
56
+ function result(db, id, callId, extra = {}) {
57
+ insert(db, 'classification_results', { id, syscall_call_id: callId, version_id: CATALOG_VERSION_ID,
58
+ input_sha256: 'a'.repeat(64), category_id: 'delete', operation_level: 3, confirmed_effect_level: 3,
59
+ effect_status: 'confirmed', reason: 'Test fixture, not automatic classification', classified_at_ms: 101, ...extra });
60
+ }
61
+
62
+ test('35 tables, catalog seeds and repeat initialization preserve evidence', () => isolated((db, path) => {
63
+ fixtures(db); call(db, 'delete');
64
+ assert.equal(db.prepare("SELECT count(*) n FROM sqlite_schema WHERE type='table' AND name NOT LIKE 'sqlite_%'").get().n, 35); // 20 evidence + 7 auxiliary + search_events + FTS5 (virtual + 4 shadows)
65
+ for (const [table, count] of [['classification_impact_levels', 4], ['classification_categories', 34], ['classification_syscall_rules', 353], ['file_labels', 6], ['classification_results', 0]]) {
66
+ assert.equal(db.prepare(`SELECT count(*) n FROM ${table}`).get().n, count);
67
+ }
68
+ const before = db.prepare('SELECT * FROM runtime_syscall_calls').all();
69
+ assert.equal(initializeEvidenceDatabase(path).created, false);
70
+ assert.deepEqual(db.prepare('SELECT * FROM runtime_syscall_calls').all(), before);
71
+ assert.deepEqual(db.prepare('PRAGMA foreign_key_check').all(), []);
72
+ assert.equal(db.prepare('SELECT classifier_version FROM classification_versions').get().classifier_version, 'not-implemented');
73
+ }));
74
+
75
+ test('cross-sandbox references are rejected, including nullable process context', () => isolated(db => {
76
+ fixtures(db);
77
+ assert.throws(() => db.exec("UPDATE runtime_processes SET parent_process_id='p-b' WHERE id='p-a'"), /FOREIGN KEY/);
78
+ assert.throws(() => event(db, 'wrong', { process_id: 'p-b' }), /FOREIGN KEY/);
79
+ assert.throws(() => event(db, 'no-sandbox', { sandbox_id: null }), /CHECK/);
80
+ event(db, 'other', { sandbox_id: 'b', process_id: 'p-b' });
81
+ assert.throws(() => insert(db, 'runtime_syscall_calls', { id: 'wrong', sandbox_id: 'a', syscall_number: 35,
82
+ entry_event_id: 'other', arguments_json: '{}', outcome: 'unknown', correlation_status: 'entry_only', normalizer_version: 'test' }), /FOREIGN KEY/);
83
+ call(db, 'ok');
84
+ insert(db, 'file_objects', { id: 'object-b', sandbox_id: 'b', object_type: 'regular', identity_basis: 'test', first_seen_ns: '1', last_seen_ns: '1' });
85
+ for (const values of [{ sandbox_id: 'b' }, { sandbox_id: 'a', file_object_id: 'object-b' }]) {
86
+ assert.throws(() => insert(db, 'file_operation_targets', { id: 'wrong-target', syscall_call_id: 'ok', target_role: 'target', resolution_status: 'unresolved', ...values }), /FOREIGN KEY/);
87
+ }
88
+ }));
89
+
90
+ test('PID reuse, repeated exec and collector-wide events are representable', () => isolated(db => {
91
+ fixtures(db);
92
+ insert(db, 'runtime_processes', { id: 'p-reused', sandbox_id: 'a', pid: 101, start_time_ns: '9007199254740994000', first_seen_ns: '9007199254740994001', identity_basis: 'start_time' });
93
+ event(db, 'exec1', { event_kind: 'exec_succeeded', payload_json: '{"executable":"sh"}' });
94
+ event(db, 'exec2', { event_kind: 'exec_succeeded', payload_json: '{"executable":"node"}' });
95
+ event(db, 'gap', { sandbox_id: null, process_id: null, tid: null, observed_at_ns: null, event_kind: 'collection_gap', payload_json: '{"dropped_delta":3}' });
96
+ assert.equal(db.prepare('SELECT started_at_ms FROM runtime_sandboxes LIMIT 1').get().started_at_ms, null);
97
+ const original = db.prepare("SELECT * FROM runtime_events WHERE id='exec1'").get();
98
+ assert.throws(() => insert(db, 'runtime_events', { ...original, id: 'duplicate' }), /cannot be replaced/);
99
+ assert.throws(() => insert(db, 'runtime_events', { ...original, id: 'replacement' }, 'INSERT OR REPLACE'), /cannot be replaced/);
100
+ assert.equal(db.prepare('SELECT count(*) n FROM runtime_attributions').get().n, 0);
101
+ }));
102
+
103
+ test('known outcomes require reliable phases, matching identities and decoded results', () => isolated(db => {
104
+ fixtures(db);
105
+ const success = call(db, 'success');
106
+ call(db, 'failure', { outcome: 'failed' });
107
+ insert(db, 'runtime_syscall_calls', { ...success, id: 'entry', exit_event_id: null, outcome: 'unknown', return_value: null, errno: null, correlation_status: 'entry_only' });
108
+ insert(db, 'runtime_syscall_calls', { ...success, id: 'exit', entry_event_id: null, correlation_status: 'exit_only' });
109
+ insert(db, 'runtime_syscall_calls', { ...success, id: 'uncertain', correlation_status: 'uncertain', outcome: 'unknown' });
110
+ for (const override of [
111
+ { outcome: 'failed' }, { outcome: 'unknown', errno: 13 }, { return_value: '1' }, { correlation_status: 'uncertain' },
112
+ { exit_event_id: null }, { syscall_number: 99 }, { syscall_name: 'write' }, { tid: 102 },
113
+ { entry_event_id: success.exit_event_id, exit_event_id: success.entry_event_id },
114
+ { return_value: '-0' }, { return_value: '00' }, { return_value: '1.5' },
115
+ ]) assert.throws(() => insert(db, 'runtime_syscall_calls', { ...success, id: 'bad', ...override }));
116
+ event(db, 'mismatched-thread', { tid: 102 });
117
+ assert.throws(() => insert(db, 'runtime_syscall_calls', { ...success, id: 'hidden-tid', tid: null, entry_event_id: 'mismatched-thread' }), /disagree/);
118
+ event(db, 'earlier-exit', { event_kind: 'syscall_exit', observed_at_ns: '9007199254740993000', payload_json: '{"return_value":"0","errno":0}' });
119
+ assert.throws(() => insert(db, 'runtime_syscall_calls', { ...success, id: 'backwards', exit_event_id: 'earlier-exit' }), /disagree/);
120
+ }));
121
+
122
+ test('classification preserves impact versus effect and is append-only even through REPLACE', () => isolated(db => {
123
+ fixtures(db);
124
+ call(db, 'success'); call(db, 'failure', { outcome: 'failed' });
125
+ call(db, 'unknown', { outcome: 'unknown', correlation_status: 'uncertain' });
126
+ result(db, 'r', 'success');
127
+ assert.throws(() => result(db, 'bad-fail', 'failure'), /successful syscall/);
128
+ assert.throws(() => result(db, 'bad-unknown', 'unknown'), /successful syscall/);
129
+ result(db, 'failed-class', 'failure', { confirmed_effect_level: null, effect_status: 'no_effect' });
130
+ assert.throws(() => result(db, 'bad-level', 'success', { input_sha256: 'b'.repeat(64), effect_status: 'unknown' }), /CHECK/);
131
+ for (const table of ['classification_versions', 'classification_syscall_rules', 'classification_results', 'classification_categories', 'classification_impact_levels']) {
132
+ assert.throws(() => db.exec(`DELETE FROM ${table}`), /append-only/);
133
+ const row = db.prepare(`SELECT * FROM ${table} LIMIT 1`).get();
134
+ const key = Object.keys(row)[0];
135
+ assert.throws(() => db.exec(`UPDATE ${table} SET ${key}=${key}`), /append-only/);
136
+ assert.throws(() => insert(db, table, row, 'INSERT OR REPLACE'), /cannot be replaced/);
137
+ }
138
+ const saved = db.prepare("SELECT * FROM classification_results WHERE id='r'").get();
139
+ assert.throws(() => insert(db, 'classification_results', { ...saved, id: 'replacement' }, 'INSERT OR REPLACE'), /cannot be replaced/);
140
+ }));
141
+
142
+ test('deletion joins to accepted tool attribution, classification and multiple file labels', () => isolated(db => {
143
+ fixtures(db); call(db, 'delete'); result(db, 'r', 'delete');
144
+ insert(db, 'runtime_attributions', { id: 'link', tool_call_id: 'tool', event_id: 'delete-exit', method: 'marker', confidence: 'exact', status: 'accepted', basis_json: '{}', attributor_version: 'test', created_at_ms: 100 });
145
+ for (const id of ['old-file', 'new-file']) insert(db, 'file_objects', { id, sandbox_id: 'a', object_type: 'regular', identity_basis: 'fixture', first_seen_ns: '1', last_seen_ns: '2' });
146
+ for (const [id, object] of [['target', 'old-file'], ['reused-path', 'new-file']]) {
147
+ insert(db, 'file_operation_targets', { id, syscall_call_id: 'delete', sandbox_id: 'a', file_object_id: object, target_role: 'target', observed_path: '/workspace/notes.md', resolution_status: 'runtime_resolved' });
148
+ }
149
+ for (const label of ['documentation', 'potentially_sensitive']) insert(db, 'file_label_assignments', { id: label, target_id: 'target', label_id: label, basis: 'fixture', confidence: 'inferred', rule_version: 'test', reason: 'test only', assigned_at_ms: 100 });
150
+ const rows = db.prepare(`SELECT tc.tool_name,c.label AS category,r.operation_level,r.effect_status,t.observed_path,l.label AS file_label
151
+ FROM runtime_attributions a JOIN agent_tool_calls tc ON tc.id=a.tool_call_id
152
+ JOIN runtime_syscall_calls s ON s.exit_event_id=a.event_id
153
+ JOIN classification_results r ON r.syscall_call_id=s.id
154
+ JOIN classification_categories c ON c.id=r.category_id
155
+ JOIN file_operation_targets t ON t.syscall_call_id=s.id
156
+ JOIN file_label_assignments fl ON fl.target_id=t.id JOIN file_labels l ON l.id=fl.label_id
157
+ WHERE a.status='accepted'`).all();
158
+ assert.equal(rows.length, 2);
159
+ assert.ok(rows.every(r => r.tool_name === 'exec_command' && r.operation_level === 3 && r.effect_status === 'confirmed' && r.observed_path === '/workspace/notes.md'));
160
+ assert.ok(rows.every(r => r.category === 'Delete'));
161
+ event(db, 'rename-entry', { payload_json: '{"syscall_name":"renameat2","syscall_number":276}' });
162
+ insert(db, 'runtime_syscall_calls', { id: 'rename', sandbox_id: 'a', syscall_name: 'renameat2', syscall_number: 276,
163
+ entry_event_id: 'rename-entry', arguments_json: '{}', outcome: 'unknown', correlation_status: 'entry_only', normalizer_version: 'test' });
164
+ for (const role of ['source', 'destination', 'replaced_destination']) insert(db, 'file_operation_targets', { id: role, syscall_call_id: 'rename', sandbox_id: 'a', target_role: role, resolution_status: 'unresolved' });
165
+ }));
166
+
167
+ test('JSON, enums, exact timestamps, context and foreign-key deletion constraints', () => isolated(db => {
168
+ fixtures(db); call(db, 'delete');
169
+ for (const sql of [
170
+ "UPDATE runtime_sandboxes SET architecture='unknown' WHERE id='a'",
171
+ "UPDATE runtime_sandboxes SET ended_at_ms='tomorrow' WHERE id='a'",
172
+ "UPDATE runtime_sandboxes SET started_at_ms=10,ended_at_ms=9 WHERE id='a'",
173
+ "UPDATE runtime_sandboxes SET capture_config_json='[]' WHERE id='a'",
174
+ "UPDATE runtime_processes SET first_seen_ns='01' WHERE id='p-a'",
175
+ "UPDATE runtime_processes SET exited_at_ns='9007199254740992999' WHERE id='p-a'",
176
+ "DELETE FROM runtime_sandboxes WHERE id='a'",
177
+ "DELETE FROM agent_exchanges WHERE id='x'",
178
+ ]) assert.throws(() => db.exec(sql));
179
+ assert.throws(() => event(db, 'invalid-json', { payload_json: 'invalid' }));
180
+ assert.throws(() => event(db, 'invalid-seq', { stream_sequence: -1 }));
181
+ insert(db, 'agent_payloads', { sha: 'fixture', content: '"prompt"' });
182
+ insert(db, 'agent_context_items', { exchange_id: 'x', position: 0, payload_sha: 'fixture' });
183
+ assert.throws(() => insert(db, 'agent_context_items', { exchange_id: 'x', position: -1, payload_sha: 'fixture' }));
184
+ assert.throws(() => db.exec("DELETE FROM agent_payloads WHERE sha='fixture'"), /FOREIGN KEY/);
185
+ }));
186
+
187
+ test('initializer rejects unrelated/changed databases and rolls back on verification errors', () => isolated((db, path, dir) => {
188
+ const unrelatedPath = join(dir, 'unrelated.db');
189
+ const unrelated = new DatabaseSync(unrelatedPath);
190
+ try {
191
+ unrelated.exec('CREATE TABLE keep(value); INSERT INTO keep VALUES (42)');
192
+ assert.throws(() => initializeEvidenceDatabase(unrelatedPath), /Incompatible/);
193
+ assert.equal(unrelated.prepare('SELECT value FROM keep').get().value, 42);
194
+ assert.equal(unrelated.prepare("SELECT count(*) n FROM sqlite_schema WHERE type='table'").get().n, 1);
195
+ } finally { unrelated.close(); }
196
+ db.exec('PRAGMA user_version=99');
197
+ assert.throws(() => initializeEvidenceDatabase(path), /schema version/);
198
+ db.exec(`PRAGMA user_version=${SCHEMA_VERSION}`);
199
+ db.exec("UPDATE file_labels SET description='tampered' WHERE id='temporary'");
200
+ assert.throws(() => initializeEvidenceDatabase(path), /seed/);
201
+ assert.equal(db.prepare("SELECT description FROM file_labels WHERE id='temporary'").get().description, 'tampered');
202
+ db.exec('CREATE TABLE unrelated(value)');
203
+ assert.throws(() => initializeEvidenceDatabase(path), /schema structure/);
204
+ assert.throws(() => initializeEvidenceDatabase(), /explicit/);
205
+ }));
206
+
207
+ test('a version-2 database is upgraded in place to add call_assessments, keeping its data', () => isolated((db, path) => {
208
+ // Recreate exactly what a v2 database looked like: no call_assessments, and
209
+ // none of the later tables either.
210
+ db.exec('DROP TABLE runtime_capture_checkpoints; DROP TABLE runtime_capture_streams; DROP INDEX call_assessments_call; DROP TABLE call_assessments; DROP TABLE workspace_snapshots; DROP TRIGGER search_events_ai; DROP TRIGGER search_events_ad; DROP TRIGGER search_events_au; DROP TABLE search_fts; DROP TABLE search_events; PRAGMA user_version=2');
211
+ insert(db, 'command_descriptions', { key: 'k', command: 'ls', description: 'listed files', created_at_ms: 1 });
212
+ const result = initializeEvidenceDatabase(path);
213
+ assert.equal(result.schemaVersion, SCHEMA_VERSION);
214
+ assert.equal(result.created, false);
215
+ assert.equal(db.prepare('PRAGMA user_version').get().user_version, SCHEMA_VERSION);
216
+ assert.equal(db.prepare("SELECT count(*) n FROM sqlite_schema WHERE name='call_assessments'").get().n, 1);
217
+ assert.equal(db.prepare("SELECT count(*) n FROM sqlite_schema WHERE name='workspace_snapshots'").get().n, 1);
218
+ assert.equal(db.prepare("SELECT description FROM command_descriptions WHERE key='k'").get().description, 'listed files');
219
+ }));
220
+
221
+ test('same catalog version with changed content is rejected without rewriting history', () => isolated((db, path) => {
222
+ // Simulate out-of-band administrative corruption, then restore the schema
223
+ // exactly so reinitialization must validate seed contents, not only DDL.
224
+ const trigger = db.prepare("SELECT sql FROM sqlite_schema WHERE name='classification_versions_no_update'").get().sql;
225
+ db.exec('DROP TRIGGER classification_versions_no_update');
226
+ db.prepare('UPDATE classification_versions SET content_sha256=? WHERE id=?').run('f'.repeat(64), CATALOG_VERSION_ID);
227
+ db.exec(trigger);
228
+ assert.throws(() => initializeEvidenceDatabase(path), /catalog version/);
229
+ assert.equal(db.prepare('SELECT content_sha256 FROM classification_versions').get().content_sha256, 'f'.repeat(64));
230
+ assert.equal(db.prepare('SELECT count(*) n FROM classification_syscall_rules').get().n, 353);
231
+ // A rollback releases the write transaction even while this reader stays open.
232
+ db.exec('BEGIN IMMEDIATE; ROLLBACK');
233
+ }));
234
+
235
+ test('a version-6 database upgrades recording metadata without inventing healthy history',()=>isolated((db,path)=>{
236
+ db.exec('DROP TABLE runtime_capture_checkpoints; DROP TABLE runtime_capture_streams; PRAGMA user_version=6');
237
+ initializeEvidenceDatabase(path);
238
+ assert.equal(db.prepare('PRAGMA user_version').get().user_version,7);
239
+ assert.equal(db.prepare('SELECT COUNT(*) n FROM runtime_capture_streams').get().n,0);
240
+ }));