raytrace-cli 0.11.1__py3-none-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- raytrace/__init__.py +1 -0
- raytrace/__main__.py +33 -0
- raytrace/app/LICENSE +21 -0
- raytrace/app/README.md +599 -0
- raytrace/app/bin/raytrace.mjs +297 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-400-normal-BwCSEQnW.woff +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-400-normal-CW0RaeGs.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-500-normal-B9HHJjqV.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-500-normal-Dr3UlScf.woff +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-600-normal-Aqo67rzb.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-600-normal-BmdmIIQ2.woff +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-700-normal-CUSSCpQX.woff +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-700-normal-DvUfVpUG.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-400-normal-BjWJ59Pq.woff +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-400-normal-BtiwyxMk.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-500-normal-BJfUCQsA.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-500-normal-DR84L5F-.woff +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-600-normal-4vooXBpG.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-600-normal-DRtaDpgU.woff +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-700-normal-BLI3TTWz.woff +0 -0
- raytrace/app/dist/dashboard/assets/dm-sans-latin-ext-700-normal-CJIcxD6K.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-400-normal-BSMlKf0J.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-400-normal-CEL4l2ZJ.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-500-normal-Ael50iVv.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-500-normal-Bq9vWWag.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-600-normal-CTOM6hUh.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-600-normal-fLZuRloM.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-400-normal-DMdlQ8Kv.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-400-normal-xuaO2J-f.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-500-normal-BIfNGwUT.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-500-normal-BqneJy0T.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-600-normal-9HEixskS.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-cyrillic-ext-600-normal-V-xxqcpd.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-400-normal-CvHOgSBP.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-400-normal-DMJ8VG8y.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-500-normal-CB9ihrfo.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-500-normal-DSY6xOcd.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-600-normal-BgSNZQsw.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-600-normal-DWFSQ4vo.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-400-normal-BmRBH3aV.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-400-normal-D3D2R8hC.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-500-normal-CAhNIIs5.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-500-normal-CZ70TYgx.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-600-normal-D38SheWl.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-latin-ext-600-normal-DmB0ttJJ.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-400-normal-BulugwFq.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-400-normal-DDuiU_S-.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-500-normal-C8zxqsMH.woff +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-500-normal-DZ4AoWbu.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-600-normal-D2EvbN8M.woff2 +0 -0
- raytrace/app/dist/dashboard/assets/ibm-plex-mono-vietnamese-600-normal-iLQfcSjf.woff +0 -0
- raytrace/app/dist/dashboard/assets/index-DepcI8Fi.js +38 -0
- raytrace/app/dist/dashboard/assets/index-H97VLD5l.css +1 -0
- raytrace/app/dist/dashboard/favicon.svg +6 -0
- raytrace/app/dist/dashboard/index.html +15 -0
- raytrace/app/package.json +48 -0
- raytrace/app/proxy/agent-intent-schema.test.mjs +142 -0
- raytrace/app/proxy/call-assessment.mjs +385 -0
- raytrace/app/proxy/call-assessment.test.mjs +167 -0
- raytrace/app/proxy/call-category.mjs +461 -0
- raytrace/app/proxy/capture-health.mjs +30 -0
- raytrace/app/proxy/capture-health.test.mjs +44 -0
- raytrace/app/proxy/capture.mjs +38 -0
- raytrace/app/proxy/capture.test.mjs +49 -0
- raytrace/app/proxy/claude-code-adapter.mjs +301 -0
- raytrace/app/proxy/claude-code-hook.mjs +22 -0
- raytrace/app/proxy/codex-config.mjs +36 -0
- raytrace/app/proxy/codex.mjs +14 -0
- raytrace/app/proxy/evidence-schema.test.mjs +240 -0
- raytrace/app/proxy/evidence-store.mjs +993 -0
- raytrace/app/proxy/evidence-store.test.mjs +301 -0
- raytrace/app/proxy/exec-narrative.mjs +625 -0
- raytrace/app/proxy/exec-narrative.test.mjs +262 -0
- raytrace/app/proxy/execution-correlation.mjs +185 -0
- raytrace/app/proxy/execution-correlation.test.mjs +146 -0
- raytrace/app/proxy/execution-runner.mjs +89 -0
- raytrace/app/proxy/execution-runner.test.mjs +38 -0
- raytrace/app/proxy/experiment-engine.mjs +195 -0
- raytrace/app/proxy/experiment-engine.test.mjs +93 -0
- raytrace/app/proxy/explanations.mjs +36 -0
- raytrace/app/proxy/explanations.test.mjs +20 -0
- raytrace/app/proxy/failure-signature.mjs +56 -0
- raytrace/app/proxy/gvisor-forwarder.mjs +493 -0
- raytrace/app/proxy/gvisor-forwarder.test.mjs +433 -0
- raytrace/app/proxy/init-evidence-db.mjs +227 -0
- raytrace/app/proxy/local-sandboxes.mjs +94 -0
- raytrace/app/proxy/local-token.mjs +30 -0
- raytrace/app/proxy/playground-recovery.test.mjs +130 -0
- raytrace/app/proxy/playground.mjs +439 -0
- raytrace/app/proxy/prompt-context.mjs +278 -0
- raytrace/app/proxy/prompt-traces.mjs +40 -0
- raytrace/app/proxy/prompt-traces.test.mjs +32 -0
- raytrace/app/proxy/providers.mjs +65 -0
- raytrace/app/proxy/providers.test.mjs +76 -0
- raytrace/app/proxy/proxy.integration.test.mjs +135 -0
- raytrace/app/proxy/raytace-proxy.mjs +941 -0
- raytrace/app/proxy/request-metrics.mjs +13 -0
- raytrace/app/proxy/request-metrics.test.mjs +19 -0
- raytrace/app/proxy/request-policy.mjs +45 -0
- raytrace/app/proxy/request-policy.test.mjs +47 -0
- raytrace/app/proxy/run-recap.mjs +139 -0
- raytrace/app/proxy/runtime-attribution.mjs +110 -0
- raytrace/app/proxy/sandbox-bridge.mjs +49 -0
- raytrace/app/proxy/schema/EVIDENCE_DATABASE.md +276 -0
- raytrace/app/proxy/schema/agent_intent.sql +101 -0
- raytrace/app/proxy/schema/auxiliary.sql +90 -0
- raytrace/app/proxy/schema/capture_health.sql +14 -0
- raytrace/app/proxy/schema/classification.sql +89 -0
- raytrace/app/proxy/schema/file_context.sql +53 -0
- raytrace/app/proxy/schema/runtime_evidence.sql +201 -0
- raytrace/app/proxy/schema/search.sql +66 -0
- raytrace/app/proxy/schema/shared_storage.sql +21 -0
- raytrace/app/proxy/search.mjs +418 -0
- raytrace/app/proxy/search.test.mjs +161 -0
- raytrace/app/proxy/smoke.test.mjs +93 -0
- raytrace/app/proxy/step-attribution.mjs +380 -0
- raytrace/app/proxy/step-lab.mjs +78 -0
- raytrace/app/proxy/step-lab.test.mjs +38 -0
- raytrace/app/proxy/summaries.mjs +151 -0
- raytrace/app/proxy/summaries.test.mjs +69 -0
- raytrace/app/proxy/tool-metadata.mjs +111 -0
- raytrace/app/proxy/tool-metadata.test.mjs +38 -0
- raytrace/app/proxy/trace-view.mjs +324 -0
- raytrace/app/proxy/trace-view.test.mjs +92 -0
- raytrace/app/proxy/typesafe.mjs +63 -0
- raytrace/app/proxy/typesafe.test.mjs +39 -0
- raytrace/app/src/agents.mjs +7 -0
- raytrace/app/src/api.mjs +57 -0
- raytrace/app/src/auth.mjs +205 -0
- raytrace/app/src/beta.mjs +96 -0
- raytrace/app/src/claude-code.mjs +93 -0
- raytrace/app/src/commands.mjs +285 -0
- raytrace/app/src/config.mjs +49 -0
- raytrace/app/src/export.mjs +200 -0
- raytrace/app/src/lima.mjs +176 -0
- raytrace/app/src/local-sandbox.mjs +358 -0
- raytrace/app/src/mcp.mjs +688 -0
- raytrace/app/src/paths.mjs +33 -0
- raytrace/app/src/prompt.mjs +42 -0
- raytrace/app/src/proxy-process.mjs +143 -0
- raytrace/app/src/sandbox.mjs +306 -0
- raytrace/app/src/share.mjs +86 -0
- raytrace/app/src/terminal.mjs +68 -0
- raytrace/app/src/upload.mjs +198 -0
- raytrace/app/worker/sandbox/Dockerfile +8 -0
- raytrace/app/worker/sandbox/SYSCALL_CATALOG.md +197 -0
- raytrace/app/worker/sandbox/claude-hook.mjs +364 -0
- raytrace/app/worker/sandbox/collector.py +405 -0
- raytrace/app/worker/sandbox/dns_forwarder.py +255 -0
- raytrace/app/worker/sandbox/engine.py +940 -0
- raytrace/app/worker/sandbox/file_scan.py +491 -0
- raytrace/app/worker/sandbox/lima.yaml +29 -0
- raytrace/app/worker/sandbox/network.py +90 -0
- raytrace/app/worker/sandbox/projects.py +702 -0
- raytrace/app/worker/sandbox/setup.py +159 -0
- raytrace/app/worker/sandbox/syscall_catalog.sql +985 -0
- raytrace/app/worker/sandbox/transcript-delivery.test.mjs +153 -0
- raytrace/app/worker/sandbox/viewer.py +195 -0
- raytrace/node/LICENSE +2946 -0
- raytrace/node/node.exe +4 -0
- raytrace_cli-0.11.1.dist-info/LICENSE +21 -0
- raytrace_cli-0.11.1.dist-info/METADATA +42 -0
- raytrace_cli-0.11.1.dist-info/RECORD +165 -0
- raytrace_cli-0.11.1.dist-info/WHEEL +4 -0
- raytrace_cli-0.11.1.dist-info/entry_points.txt +2 -0
|
@@ -0,0 +1,993 @@
|
|
|
1
|
+
import { captureHealth, sourceStreamId } from './capture-health.mjs';
|
|
2
|
+
/**
|
|
3
|
+
* RayTrace storage layer over the evidence database (proxy/schema/*.sql,
|
|
4
|
+
* created and verified by init-evidence-db.mjs). This module is the only
|
|
5
|
+
* place that knows about tables.
|
|
6
|
+
*
|
|
7
|
+
* Two writers feed it:
|
|
8
|
+
* - the proxy, once per captured model request: session -> turn ->
|
|
9
|
+
* exchange -> proposed tool calls, with payloads stored once by digest;
|
|
10
|
+
* - the gVisor forwarder, once per sandbox process event: sandbox ->
|
|
11
|
+
* process -> event, plus an attribution when a process is tied to a call.
|
|
12
|
+
*
|
|
13
|
+
* Readers (the dashboard) get the row shapes they always had -- hydrated
|
|
14
|
+
* exchanges, per-call execution summaries -- so the evidence model can stay
|
|
15
|
+
* normalized without every consumer learning it. Tool calls are identified
|
|
16
|
+
* internally by their own ids; reads that the dashboard joins on use the
|
|
17
|
+
* provider's call id, always scoped to specific exchanges.
|
|
18
|
+
*/
|
|
19
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
20
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
21
|
+
import { chmodSync, existsSync, mkdirSync } from 'node:fs';
|
|
22
|
+
import { dirname } from 'node:path';
|
|
23
|
+
import { initializeEvidenceDatabase, verifyEvidenceDatabase } from './init-evidence-db.mjs';
|
|
24
|
+
import { promptInfo } from './prompt-traces.mjs';
|
|
25
|
+
import { TOOL_RESULT_KINDS, toolResultFacts } from './tool-metadata.mjs';
|
|
26
|
+
import { reportedOutcome, observedOutcome, combineOutcomes, payloadToText, extractFilePaths, pathsFromText, commandTextOf, clipText, runSearch, topFailureSignatures } from './search.mjs';
|
|
27
|
+
import { failureSignature } from './failure-signature.mjs';
|
|
28
|
+
import { callCategory } from './call-category.mjs';
|
|
29
|
+
|
|
30
|
+
const sha256 = (text) => createHash('sha256').update(text).digest('hex');
|
|
31
|
+
const toMs = (value) => { const parsed = Date.parse(value ?? ''); return Number.isFinite(parsed) ? parsed : null; };
|
|
32
|
+
const toIso = (ms) => (Number.isFinite(ms) ? new Date(ms).toISOString() : null);
|
|
33
|
+
const asJson = (value) => (value === undefined || value === null ? null : JSON.stringify(value));
|
|
34
|
+
const fromJson = (value) => { if (value === null || value === undefined) return null; try { return JSON.parse(value); } catch { return null; } };
|
|
35
|
+
const nsText = (value) => (typeof value === 'string' && /^(0|[1-9]\d*)$/.test(value) ? value : null);
|
|
36
|
+
const intOrNull = (value) => (Number.isInteger(Number(value)) && value !== null && value !== '' ? Number(value) : null);
|
|
37
|
+
const marks = (list) => list.map(() => '?').join(',');
|
|
38
|
+
const CALL_TYPES = /function_call|tool_use|tool_call/i;
|
|
39
|
+
const ROUTES = `(e.route LIKE '%/responses' OR e.route LIKE '%/messages' OR e.route LIKE '%/chat/completions')`;
|
|
40
|
+
const DECODER_VERSION = 'gvisor-collector-1';
|
|
41
|
+
const ATTRIBUTOR_VERSION = 'causal-window-1';
|
|
42
|
+
|
|
43
|
+
// Attribution confidence in the schema's terms, and the dashboard's older
|
|
44
|
+
// name for the same idea (see app/types.ts Tier).
|
|
45
|
+
const TIER = { exact: 'mediated', corroborated: 'corroborated', inferred: 'inferred' };
|
|
46
|
+
|
|
47
|
+
function previewOf(item) {
|
|
48
|
+
const content = item?.content ?? item?.text ?? item?.output ?? item;
|
|
49
|
+
const text = typeof content === 'string' ? content
|
|
50
|
+
: Array.isArray(content) ? content.map((part) => part?.text || part?.content || '').filter(Boolean).join(' ')
|
|
51
|
+
: JSON.stringify(content ?? '');
|
|
52
|
+
return (text || '').slice(0, 240);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** What a process's row says about how it ended, in the dashboard's terms.
|
|
56
|
+
* Never upgrades missing evidence into an outcome. */
|
|
57
|
+
function processOutcome(row) {
|
|
58
|
+
if (!row?.process_id) return { status: null, error: null };
|
|
59
|
+
if (!row.exec_ok && row.failed_errno != null) return { status: 'failed', error: `exec failed: errno ${row.failed_errno}` };
|
|
60
|
+
if (row.exited_at_ns != null) {
|
|
61
|
+
if (row.exit_signal) return { status: 'failed', error: `killed by signal ${row.exit_signal}` };
|
|
62
|
+
if (row.exit_code === 0) return { status: 'completed', error: null };
|
|
63
|
+
if (row.exit_code != null) return { status: 'failed', error: `exit ${row.exit_code}` };
|
|
64
|
+
return { status: 'unknown', error: null };
|
|
65
|
+
}
|
|
66
|
+
if (row.evidence_lost) return { status: 'unknown', error: `evidence lost: ${row.evidence_lost}` };
|
|
67
|
+
return { status: 'running', error: null };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Per-process facts derived from its events: the command it first ran, and
|
|
71
|
+
// whether its only exec attempts failed.
|
|
72
|
+
const PROCESS_FACTS = `
|
|
73
|
+
(SELECT json_extract(ev.payload_json, '$.argv') FROM runtime_events ev
|
|
74
|
+
WHERE ev.process_id = p.id AND ev.event_kind IN ('exec_succeeded','exec_failed') ORDER BY ev.stream_sequence LIMIT 1) AS argv_json,
|
|
75
|
+
(SELECT json_extract(ev.payload_json, '$.errno') FROM runtime_events ev
|
|
76
|
+
WHERE ev.process_id = p.id AND ev.event_kind = 'exec_failed' ORDER BY ev.stream_sequence DESC LIMIT 1) AS failed_errno,
|
|
77
|
+
EXISTS (SELECT 1 FROM runtime_events ev WHERE ev.process_id = p.id AND ev.event_kind = 'exec_succeeded') AS exec_ok`;
|
|
78
|
+
|
|
79
|
+
export function openEvidenceStore(file, { readOnly = false } = {}) {
|
|
80
|
+
let journal = 'wal';
|
|
81
|
+
let db;
|
|
82
|
+
if (readOnly) {
|
|
83
|
+
if (!existsSync(file)) throw Error(`Database file does not exist: ${file}`);
|
|
84
|
+
verifyEvidenceDatabase(file);
|
|
85
|
+
db = new DatabaseSync(file, { readOnly: true });
|
|
86
|
+
journal = 'read-only';
|
|
87
|
+
} else {
|
|
88
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
89
|
+
// Creates a fresh v2 database, or verifies an existing one; refuses
|
|
90
|
+
// anything else (an old-format database, a changed schema) untouched.
|
|
91
|
+
initializeEvidenceDatabase(file);
|
|
92
|
+
db = new DatabaseSync(file);
|
|
93
|
+
// WAL lets the dashboard read while the proxy writes, but needs shared
|
|
94
|
+
// memory network mounts cannot provide. Fall back rather than refuse.
|
|
95
|
+
try { db.exec('PRAGMA journal_mode = WAL'); db.exec('PRAGMA synchronous = NORMAL'); }
|
|
96
|
+
catch { db.exec('PRAGMA journal_mode = DELETE'); db.exec('PRAGMA synchronous = FULL'); journal = 'delete'; }
|
|
97
|
+
db.exec('PRAGMA foreign_keys = ON');
|
|
98
|
+
db.exec('PRAGMA busy_timeout = 5000');
|
|
99
|
+
// Prompts and tool output, for this user only. SQLite gives new -wal and
|
|
100
|
+
// -shm files the database's mode; ones made before this keep their own.
|
|
101
|
+
for (const path of [file, `${file}-wal`, `${file}-shm`]) {
|
|
102
|
+
try { chmodSync(path, 0o600); } catch { /* not there yet, or a filesystem without modes */ }
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const q = (sql) => db.prepare(sql);
|
|
107
|
+
const insertPayload = q('INSERT OR IGNORE INTO agent_payloads (sha, content) VALUES (?, ?)');
|
|
108
|
+
const selectPayload = q('SELECT content FROM agent_payloads WHERE sha = ?');
|
|
109
|
+
|
|
110
|
+
function putPayload(value) {
|
|
111
|
+
if (value === undefined) return null;
|
|
112
|
+
const text = JSON.stringify(value);
|
|
113
|
+
const sha = sha256(text);
|
|
114
|
+
insertPayload.run(sha, text);
|
|
115
|
+
return sha;
|
|
116
|
+
}
|
|
117
|
+
const getPayload = (sha) => (sha ? fromJson(selectPayload.get(sha)?.content ?? null) : null);
|
|
118
|
+
|
|
119
|
+
function transaction(work) {
|
|
120
|
+
db.exec('BEGIN IMMEDIATE');
|
|
121
|
+
try { const result = work(); db.exec('COMMIT'); return result; }
|
|
122
|
+
catch (error) { db.exec('ROLLBACK'); throw error; }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ------------------------------------------------------------ agent intent
|
|
126
|
+
|
|
127
|
+
const selectSession = q('SELECT id FROM agent_sessions WHERE external_session_id = ? ORDER BY started_at_ms LIMIT 1');
|
|
128
|
+
const insertSession = q('INSERT INTO agent_sessions (id, external_session_id, agent_name, started_at_ms) VALUES (?, ?, ?, ?)');
|
|
129
|
+
const selectContinuedTurn = q('SELECT id FROM agent_turns WHERE session_id = ? AND prompt_key = ? ORDER BY sequence_number DESC LIMIT 1');
|
|
130
|
+
const nextSequence = q('SELECT COALESCE(MAX(sequence_number) + 1, 0) AS n FROM agent_turns WHERE session_id = ?');
|
|
131
|
+
const insertTurn = q(`INSERT INTO agent_turns (id, session_id, sequence_number, user_prompt_sha, started_at_ms, status, prompt_key)
|
|
132
|
+
VALUES (?, ?, ?, ?, ?, 'active', ?)`);
|
|
133
|
+
const updateTurn = q(`UPDATE agent_turns SET status = ?, ended_at_ms = MAX(COALESCE(ended_at_ms, started_at_ms), ?),
|
|
134
|
+
final_response_sha = COALESCE(?, final_response_sha) WHERE id = ?`);
|
|
135
|
+
const selectExchangeId = q('SELECT id FROM agent_exchanges WHERE id = ?');
|
|
136
|
+
const insertExchange = q(`INSERT INTO agent_exchanges (id, turn_id, session_id, provider, model, started_at_ms, completed_at_ms,
|
|
137
|
+
http_status, request_sha, response_sha, metrics_json, route, method, tools_sha, input_key, transport_json)
|
|
138
|
+
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`);
|
|
139
|
+
const insertContext = q(`INSERT INTO agent_context_items (exchange_id, position, role, kind, external_call_id, payload_sha, preview)
|
|
140
|
+
VALUES (?,?,?,?,?,?,?)`);
|
|
141
|
+
const insertToolCall = q(`INSERT INTO agent_tool_calls (id, exchange_id, external_call_id, output_index, tool_name, arguments_sha, proposed_at_ms)
|
|
142
|
+
VALUES (?,?,?,?,?,?,?)`);
|
|
143
|
+
// The call a result answers: same session, proposed no later than the
|
|
144
|
+
// request carrying the result, not already answered.
|
|
145
|
+
const selectAnsweredCall = q(`SELECT c.id FROM agent_tool_calls c JOIN agent_exchanges e ON e.id = c.exchange_id
|
|
146
|
+
WHERE c.external_call_id = ? AND e.session_id = ? AND c.reported_result_sha IS NULL AND e.started_at_ms <= ?
|
|
147
|
+
ORDER BY e.started_at_ms DESC LIMIT 1`);
|
|
148
|
+
const setReportedResult = q('UPDATE agent_tool_calls SET reported_result_sha = ? WHERE id = ?');
|
|
149
|
+
|
|
150
|
+
// ------------------------------------------------------------ search index
|
|
151
|
+
// Derived rows (search.sql), rebuilt from the evidence tables whenever
|
|
152
|
+
// either side of a call lands: its proposal/result (recordExchange) or
|
|
153
|
+
// its observed process (attribute/recordProcessExit). Indexing must
|
|
154
|
+
// never cost us evidence, so hook sites guard these calls; a missed
|
|
155
|
+
// update self-heals on the next open (backfill) or a full reindex.
|
|
156
|
+
|
|
157
|
+
const SEARCH_COLUMNS = ['id', 'kind', 'ts_ms', 'session_id', 'session_external', 'turn_id', 'exchange_id', 'call_id',
|
|
158
|
+
'external_call_id', 'output_index', 'agent', 'model', 'provider', 'tool_name', 'category', 'outcome',
|
|
159
|
+
'outcome_reported', 'outcome_observed', 'reported_check', 'exit_code', 'http_status', 'error_text',
|
|
160
|
+
'failure_signature', 'signature_label', 'command_text', 'file_paths', 'prompt_text', 'args_text',
|
|
161
|
+
'result_text', 'duration_ms', 'indexed_at_ms'];
|
|
162
|
+
const upsertSearchEvent = q(`INSERT INTO search_events (${SEARCH_COLUMNS.join(',')})
|
|
163
|
+
VALUES (${SEARCH_COLUMNS.map(() => '?').join(',')})
|
|
164
|
+
ON CONFLICT(id) DO UPDATE SET ${SEARCH_COLUMNS.filter((c) => c !== 'id').map((c) => `${c}=excluded.${c}`).join(',')}`);
|
|
165
|
+
const putSearchEvent = (event) => upsertSearchEvent.run(...SEARCH_COLUMNS.map((c) => event[c] ?? null));
|
|
166
|
+
|
|
167
|
+
const selectCallForIndex = q(`SELECT c.id AS call_pk, c.external_call_id, c.output_index, c.tool_name,
|
|
168
|
+
c.arguments_sha, c.reported_result_sha, c.proposed_at_ms,
|
|
169
|
+
e.id AS exchange_id, e.turn_id, e.session_id, e.provider, e.model, e.started_at_ms, e.completed_at_ms, e.http_status,
|
|
170
|
+
s.external_session_id, s.agent_name, t.user_prompt_sha
|
|
171
|
+
FROM agent_tool_calls c
|
|
172
|
+
JOIN agent_exchanges e ON e.id = c.exchange_id
|
|
173
|
+
JOIN agent_sessions s ON s.id = e.session_id
|
|
174
|
+
LEFT JOIN agent_turns t ON t.id = e.turn_id
|
|
175
|
+
WHERE c.id = ?`);
|
|
176
|
+
const selectCallProcess = q(`SELECT a.reported_check,
|
|
177
|
+
p.id AS process_id, p.first_seen_ms, p.exited_at_ms, p.exited_at_ns, p.exit_code, p.exit_signal, p.evidence_lost,
|
|
178
|
+
${PROCESS_FACTS}
|
|
179
|
+
FROM runtime_attributions a
|
|
180
|
+
JOIN runtime_events ev ON ev.id = a.event_id
|
|
181
|
+
LEFT JOIN runtime_processes p ON p.id = ev.process_id
|
|
182
|
+
WHERE a.tool_call_id = ? AND a.status = 'accepted' AND a.method != 'inherited'
|
|
183
|
+
ORDER BY a.created_at_ms LIMIT 1`);
|
|
184
|
+
const selectSnapshotPaths = q('SELECT changes_json FROM workspace_snapshots WHERE call_id = ? ORDER BY taken_at_ms DESC LIMIT 1');
|
|
185
|
+
|
|
186
|
+
function indexCall(callPk) {
|
|
187
|
+
const row = selectCallForIndex.get(callPk);
|
|
188
|
+
if (!row) return;
|
|
189
|
+
const args = getPayload(row.arguments_sha);
|
|
190
|
+
const resultItem = getPayload(row.reported_result_sha);
|
|
191
|
+
const reported = reportedOutcome(resultItem);
|
|
192
|
+
const proc = selectCallProcess.get(callPk);
|
|
193
|
+
const observedStatus = proc ? processOutcome(proc) : { status: null, error: null };
|
|
194
|
+
const observed = observedOutcome(observedStatus.status);
|
|
195
|
+
const outcome = combineOutcomes(reported, observed);
|
|
196
|
+
const facts = resultItem != null ? toolResultFacts(resultItem) : { exitCode: null, wallMs: null };
|
|
197
|
+
const exitCode = proc?.exit_code ?? facts.exitCode ?? null;
|
|
198
|
+
const resultText = clipText(payloadToText(resultItem));
|
|
199
|
+
const failed = outcome === 'failed';
|
|
200
|
+
const errorText = failed ? clipText([observedStatus.error, resultText].filter(Boolean).join('\n'), 8000) : (observedStatus.error ?? null);
|
|
201
|
+
const sig = failed ? failureSignature({ errorText: resultText || errorText, exitCode, toolName: row.tool_name }) : null;
|
|
202
|
+
const files = pathsFromText(commandTextOf(args), extractFilePaths(args));
|
|
203
|
+
if (row.external_call_id) {
|
|
204
|
+
const snap = fromJson(selectSnapshotPaths.get(row.external_call_id)?.changes_json ?? null);
|
|
205
|
+
for (const change of snap?.changes ?? []) if (change?.path) files.add(change.path);
|
|
206
|
+
}
|
|
207
|
+
const observedMs = Number.isFinite(proc?.first_seen_ms) && Number.isFinite(proc?.exited_at_ms) ? proc.exited_at_ms - proc.first_seen_ms : null;
|
|
208
|
+
putSearchEvent({
|
|
209
|
+
id: `call:${callPk}`, kind: 'call', ts_ms: row.proposed_at_ms ?? row.completed_at_ms ?? row.started_at_ms,
|
|
210
|
+
session_id: row.session_id, session_external: row.external_session_id, turn_id: row.turn_id,
|
|
211
|
+
exchange_id: row.exchange_id, call_id: callPk, external_call_id: row.external_call_id,
|
|
212
|
+
output_index: row.output_index, agent: row.agent_name, model: row.model, provider: row.provider,
|
|
213
|
+
tool_name: row.tool_name, category: callCategory(row.tool_name, args)?.category ?? null,
|
|
214
|
+
outcome, outcome_reported: reported, outcome_observed: observed,
|
|
215
|
+
reported_check: proc?.reported_check ?? null, exit_code: exitCode, http_status: row.http_status,
|
|
216
|
+
error_text: errorText, failure_signature: sig?.signature ?? null, signature_label: sig?.label ?? null,
|
|
217
|
+
command_text: commandTextOf(args) || null, file_paths: [...files].join('\n') || null,
|
|
218
|
+
prompt_text: clipText(payloadToText(getPayload(row.user_prompt_sha)), 8000) || null,
|
|
219
|
+
args_text: clipText(payloadToText(args)) || null, result_text: resultText || null,
|
|
220
|
+
duration_ms: observedMs ?? facts.wallMs ?? null, indexed_at_ms: Date.now(),
|
|
221
|
+
});
|
|
222
|
+
if (row.turn_id) indexTurn(row.turn_id);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const selectTurnForIndex = q(`SELECT t.id, t.session_id, t.status, t.started_at_ms, t.ended_at_ms,
|
|
226
|
+
t.user_prompt_sha, t.final_response_sha, s.external_session_id, s.agent_name,
|
|
227
|
+
(SELECT e.model FROM agent_exchanges e WHERE e.turn_id = t.id ORDER BY e.started_at_ms DESC LIMIT 1) AS model,
|
|
228
|
+
(SELECT e.provider FROM agent_exchanges e WHERE e.turn_id = t.id ORDER BY e.started_at_ms DESC LIMIT 1) AS provider,
|
|
229
|
+
(SELECT MAX(e.http_status) FROM agent_exchanges e WHERE e.turn_id = t.id) AS http_status,
|
|
230
|
+
(SELECT e.id FROM agent_exchanges e WHERE e.turn_id = t.id ORDER BY e.started_at_ms DESC LIMIT 1) AS exchange_id
|
|
231
|
+
FROM agent_turns t JOIN agent_sessions s ON s.id = t.session_id WHERE t.id = ?`);
|
|
232
|
+
|
|
233
|
+
function indexTurn(turnId) {
|
|
234
|
+
const row = selectTurnForIndex.get(turnId);
|
|
235
|
+
if (!row) return;
|
|
236
|
+
const failedCalls = q(`SELECT COUNT(*) AS n FROM search_events WHERE turn_id = ? AND kind = 'call' AND outcome = 'failed'`).get(turnId).n;
|
|
237
|
+
const outcome = row.status === 'failed' || failedCalls > 0 ? 'failed' : row.status === 'completed' ? 'ok' : 'unknown';
|
|
238
|
+
putSearchEvent({
|
|
239
|
+
id: `turn:${turnId}`, kind: 'turn', ts_ms: row.started_at_ms,
|
|
240
|
+
session_id: row.session_id, session_external: row.external_session_id, turn_id: turnId,
|
|
241
|
+
exchange_id: row.exchange_id, agent: row.agent_name, model: row.model, provider: row.provider,
|
|
242
|
+
outcome, outcome_reported: 'unknown', outcome_observed: 'unknown', http_status: row.http_status,
|
|
243
|
+
prompt_text: clipText(payloadToText(getPayload(row.user_prompt_sha)), 16_000) || null,
|
|
244
|
+
result_text: clipText(payloadToText(getPayload(row.final_response_sha)), 16_000) || null,
|
|
245
|
+
duration_ms: row.ended_at_ms != null ? row.ended_at_ms - row.started_at_ms : null,
|
|
246
|
+
indexed_at_ms: Date.now(),
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function indexExchange(exchangeId) {
|
|
251
|
+
for (const call of q('SELECT id FROM agent_tool_calls WHERE exchange_id = ?').all(exchangeId)) indexCall(call.id);
|
|
252
|
+
const turn = q('SELECT turn_id FROM agent_exchanges WHERE id = ?').get(exchangeId);
|
|
253
|
+
if (turn?.turn_id) indexTurn(turn.turn_id);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function indexProcessCalls(processId) {
|
|
257
|
+
const rows = q(`SELECT DISTINCT a.tool_call_id FROM runtime_attributions a
|
|
258
|
+
JOIN runtime_events ev ON ev.id = a.event_id WHERE ev.process_id = ?`).all(processId);
|
|
259
|
+
for (const row of rows) indexCall(row.tool_call_id);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function indexProxyErrorRow(row) {
|
|
263
|
+
const sig = failureSignature({ errorText: row.error, toolName: 'proxy' });
|
|
264
|
+
putSearchEvent({
|
|
265
|
+
id: `proxy_error:${row.rowid}`, kind: 'proxy_error', ts_ms: row.occurred_at_ms ?? Date.now(),
|
|
266
|
+
provider: row.provider, outcome: 'failed', outcome_reported: 'unknown', outcome_observed: 'unknown',
|
|
267
|
+
error_text: clipText([row.route, row.error].filter(Boolean).join(' '), 8000) || null,
|
|
268
|
+
failure_signature: sig?.signature ?? null, signature_label: sig?.label ?? null,
|
|
269
|
+
result_text: clipText(payloadToText(fromJson(row.cause_json)), 8000) || null,
|
|
270
|
+
indexed_at_ms: Date.now(),
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function indexGuarded(work) {
|
|
275
|
+
try { work(); } catch (error) { console.error('[raytace] search indexing failed:', error.message); }
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function backfillSearchIndex() {
|
|
279
|
+
const missing = (sql) => q(sql).all();
|
|
280
|
+
const calls = missing(`SELECT c.id FROM agent_tool_calls c WHERE NOT EXISTS (SELECT 1 FROM search_events se WHERE se.id = 'call:' || c.id)`);
|
|
281
|
+
const turns = missing(`SELECT t.id FROM agent_turns t WHERE NOT EXISTS (SELECT 1 FROM search_events se WHERE se.id = 'turn:' || t.id)`);
|
|
282
|
+
const errors = missing(`SELECT rowid, * FROM proxy_errors pe WHERE NOT EXISTS (SELECT 1 FROM search_events se WHERE se.id = 'proxy_error:' || pe.rowid)`);
|
|
283
|
+
if (!calls.length && !turns.length && !errors.length) return 0;
|
|
284
|
+
return transaction(() => {
|
|
285
|
+
for (const call of calls) indexCall(call.id);
|
|
286
|
+
for (const turn of turns) indexTurn(turn.id);
|
|
287
|
+
for (const row of errors) indexProxyErrorRow(row);
|
|
288
|
+
return calls.length + turns.length + errors.length;
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function ensureSession(row, startedMs) {
|
|
293
|
+
const external = typeof row.session_id === 'string' && row.session_id ? row.session_id : null;
|
|
294
|
+
const found = external ? selectSession.get(external) : null;
|
|
295
|
+
if (found) return found.id;
|
|
296
|
+
const id = randomUUID();
|
|
297
|
+
insertSession.run(id, external, null, toMs(row.session_started_at) ?? startedMs);
|
|
298
|
+
return id;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Which turn a request belongs to. A turn opens on a request whose last
|
|
302
|
+
* real user message is the last input item (the user just asked); later
|
|
303
|
+
* requests that carry the same conversation prefix plus the agent's own
|
|
304
|
+
* work continue it. Background jobs (title generation, catch-ups) and
|
|
305
|
+
* requests with no user message belong to no turn. */
|
|
306
|
+
function resolveTurn(sessionId, row, request, startedMs) {
|
|
307
|
+
const prompt = promptInfo(request);
|
|
308
|
+
if (!prompt) return null;
|
|
309
|
+
// A sandbox files every Claude Code session under one rtp- id, and two
|
|
310
|
+
// sessions given the same prompt start identically, so the agent's own
|
|
311
|
+
// session id is part of the key when a row has one: a session never
|
|
312
|
+
// continues another's turn. Rows without one keep their original key.
|
|
313
|
+
const key = sha256(JSON.stringify([row.provider ?? null, request?.model ?? null, prompt.prefix,
|
|
314
|
+
...(row.agent_session_id ? [row.agent_session_id] : [])]));
|
|
315
|
+
if (prompt.continuation) {
|
|
316
|
+
const turn = selectContinuedTurn.get(sessionId, key);
|
|
317
|
+
if (turn) return turn.id;
|
|
318
|
+
}
|
|
319
|
+
const id = randomUUID();
|
|
320
|
+
insertTurn.run(id, sessionId, nextSequence.get(sessionId).n, putPayload(prompt.title), startedMs, key);
|
|
321
|
+
return id;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function recordExchange(row) {
|
|
325
|
+
if (!row?.span_id) return;
|
|
326
|
+
const request = row.request?.payload ?? null;
|
|
327
|
+
const response = row.response?.payload ?? null;
|
|
328
|
+
const inputKey = Array.isArray(request?.input) ? 'input' : Array.isArray(request?.messages) ? 'messages' : null;
|
|
329
|
+
const items = inputKey ? request[inputKey] : [];
|
|
330
|
+
const envelope = request ? Object.fromEntries(Object.entries(request).filter(([key]) => key !== inputKey && key !== 'tools')) : null;
|
|
331
|
+
const startedMs = toMs(row.timestamp) ?? Date.now();
|
|
332
|
+
const completedMs = Math.max(startedMs, toMs(row.completed_at) ?? startedMs);
|
|
333
|
+
const output = Array.isArray(response?.output) ? response.output : [];
|
|
334
|
+
const proposals = output.map((item, index) => [item, index]).filter(([item]) => item?.call_id && CALL_TYPES.test(item?.type || ''));
|
|
335
|
+
|
|
336
|
+
const answered = transaction(() => {
|
|
337
|
+
if (selectExchangeId.get(row.span_id)) return [];
|
|
338
|
+
const sessionId = ensureSession(row, startedMs);
|
|
339
|
+
const turnId = resolveTurn(sessionId, row, request, startedMs);
|
|
340
|
+
const responseSha = putPayload(response);
|
|
341
|
+
insertExchange.run(row.span_id, turnId, sessionId, row.provider ?? 'unknown', request?.model ?? null, startedMs, completedMs,
|
|
342
|
+
row.response?.status ?? null, putPayload(envelope), responseSha, asJson(row.metrics), row.route ?? null, row.method ?? null,
|
|
343
|
+
request?.tools === undefined ? null : putPayload(request.tools), inputKey,
|
|
344
|
+
asJson({ trace_id: row.trace_id ?? null, parent_span_id: row.parent_span_id ?? null,
|
|
345
|
+
request: { headers: row.request?.headers ?? null, bytes: row.request?.bytes ?? null, sha256: row.request?.sha256 ?? null },
|
|
346
|
+
response: { headers: row.response?.headers ?? null, bytes: row.response?.bytes ?? null, sha256: row.response?.sha256 ?? null } }));
|
|
347
|
+
|
|
348
|
+
const answeredCalls = [];
|
|
349
|
+
for (const [position, item] of items.entries()) {
|
|
350
|
+
const payloadSha = putPayload(item);
|
|
351
|
+
insertContext.run(row.span_id, position, item?.role ?? null, item?.type ?? null, item?.call_id ?? null, payloadSha, previewOf(item));
|
|
352
|
+
// A tool result the agent sends back is its report of what the call
|
|
353
|
+
// did -- recorded on the call as a report, never as evidence.
|
|
354
|
+
if (item?.call_id && TOOL_RESULT_KINDS.includes(item?.type)) {
|
|
355
|
+
const call = selectAnsweredCall.get(item.call_id, sessionId, startedMs);
|
|
356
|
+
if (call) { setReportedResult.run(payloadSha, call.id); answeredCalls.push(call.id); }
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
for (const [item, outputIndex] of proposals) {
|
|
360
|
+
insertToolCall.run(randomUUID(), row.span_id, item.call_id, outputIndex, item.name ?? item.function?.name ?? 'tool',
|
|
361
|
+
putPayload(item.arguments ?? item.input ?? item.function?.arguments ?? null), completedMs);
|
|
362
|
+
}
|
|
363
|
+
if (turnId) {
|
|
364
|
+
// A response that proposes nothing more is the agent's answer. This
|
|
365
|
+
// closes the turn for the agent; processes it started may still run.
|
|
366
|
+
const failed = Number(row.response?.status) >= 400;
|
|
367
|
+
const status = failed ? 'failed' : proposals.length ? 'active' : 'completed';
|
|
368
|
+
updateTurn.run(status, completedMs, status === 'completed' ? responseSha : null, turnId);
|
|
369
|
+
}
|
|
370
|
+
return answeredCalls;
|
|
371
|
+
});
|
|
372
|
+
for (const callId of answered) checkReported({ toolCallId: callId });
|
|
373
|
+
indexGuarded(() => { indexExchange(row.span_id); for (const callId of answered) indexCall(callId); });
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// Rebuild the capture-time row shape the dashboard and lab consume.
|
|
377
|
+
const selectContextPayloads = q('SELECT payload_sha FROM agent_context_items WHERE exchange_id = ? ORDER BY position');
|
|
378
|
+
const EXCHANGE_COLUMNS = `e.*, s.external_session_id, s.started_at_ms AS session_started_ms, t.user_prompt_sha, t.status AS turn_status`;
|
|
379
|
+
function hydrate(record) {
|
|
380
|
+
const payload = { ...getPayload(record.request_sha) };
|
|
381
|
+
const tools = getPayload(record.tools_sha);
|
|
382
|
+
if (tools !== null) payload.tools = tools;
|
|
383
|
+
if (record.input_key) payload[record.input_key] = selectContextPayloads.all(record.id).map((item) => getPayload(item.payload_sha));
|
|
384
|
+
const transport = fromJson(record.transport_json) ?? {};
|
|
385
|
+
return {
|
|
386
|
+
event_type: 'model.exchange',
|
|
387
|
+
session_id: record.external_session_id, session_started_at: toIso(record.session_started_ms),
|
|
388
|
+
// A trace is a turn: one prompt and everything the agent did for it.
|
|
389
|
+
trace_id: record.turn_id, promptTitle: record.user_prompt_sha ? getPayload(record.user_prompt_sha) : null,
|
|
390
|
+
turn_status: record.turn_status ?? null,
|
|
391
|
+
span_id: record.id, parent_span_id: transport.parent_span_id ?? null,
|
|
392
|
+
timestamp: toIso(record.started_at_ms), completed_at: toIso(record.completed_at_ms),
|
|
393
|
+
provider: record.provider, route: record.route, method: record.method,
|
|
394
|
+
metrics: fromJson(record.metrics_json),
|
|
395
|
+
request: { headers: transport.request?.headers ?? null, bytes: transport.request?.bytes ?? null, sha256: transport.request?.sha256 ?? null, payload },
|
|
396
|
+
response: { status: record.http_status, headers: transport.response?.headers ?? null, bytes: transport.response?.bytes ?? null,
|
|
397
|
+
sha256: transport.response?.sha256 ?? null, payload: getPayload(record.response_sha) },
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
const latestSession = q(`SELECT e.session_id FROM agent_exchanges e JOIN agent_sessions s ON s.id = e.session_id
|
|
401
|
+
WHERE e.turn_id IS NOT NULL AND ${ROUTES} ORDER BY s.started_at_ms DESC, e.started_at_ms DESC LIMIT 1`);
|
|
402
|
+
const turnExchangesQuery = (scoped) => q(`SELECT ${EXCHANGE_COLUMNS} FROM agent_exchanges e
|
|
403
|
+
JOIN agent_sessions s ON s.id = e.session_id JOIN agent_turns t ON t.id = e.turn_id
|
|
404
|
+
WHERE ${ROUTES} ${scoped ? 'AND e.session_id = ?' : ''} ORDER BY e.started_at_ms DESC LIMIT ?`);
|
|
405
|
+
const recentAll = turnExchangesQuery(false);
|
|
406
|
+
const recentBySession = turnExchangesQuery(true);
|
|
407
|
+
const recentByExternalId = q(`SELECT ${EXCHANGE_COLUMNS} FROM agent_exchanges e
|
|
408
|
+
JOIN agent_sessions s ON s.id = e.session_id JOIN agent_turns t ON t.id = e.turn_id
|
|
409
|
+
WHERE ${ROUTES} AND s.external_session_id = ? ORDER BY e.started_at_ms DESC LIMIT ?`);
|
|
410
|
+
const exchangesForTurn = q(`SELECT ${EXCHANGE_COLUMNS} FROM agent_exchanges e
|
|
411
|
+
JOIN agent_sessions s ON s.id = e.session_id JOIN agent_turns t ON t.id = e.turn_id
|
|
412
|
+
WHERE ${ROUTES} AND (e.turn_id = ? OR t.id = ? OR e.id = ?) ORDER BY e.started_at_ms ASC, e.id ASC LIMIT ?`);
|
|
413
|
+
const selectExchange = q(`SELECT ${EXCHANGE_COLUMNS} FROM agent_exchanges e
|
|
414
|
+
JOIN agent_sessions s ON s.id = e.session_id LEFT JOIN agent_turns t ON t.id = e.turn_id WHERE e.id = ?`);
|
|
415
|
+
|
|
416
|
+
// --------------------------------------------------------- runtime evidence
|
|
417
|
+
|
|
418
|
+
const selectSandbox = q('SELECT id, project_id FROM runtime_sandboxes WHERE id = ?');
|
|
419
|
+
const insertSandbox = q(`INSERT INTO runtime_sandboxes (id, external_container_id, project_id, runtime_name, started_at_ms)
|
|
420
|
+
VALUES (?, ?, ?, 'gvisor', ?)`);
|
|
421
|
+
const setSandboxProject = q('UPDATE runtime_sandboxes SET project_id = ? WHERE id = ? AND project_id IS NULL');
|
|
422
|
+
const selectEventId = q('SELECT id FROM runtime_events WHERE stream_id = ? AND stream_sequence = ?');
|
|
423
|
+
const insertEvent = q(`INSERT INTO runtime_events (id, sandbox_id, process_id, stream_id, stream_sequence, event_kind,
|
|
424
|
+
observed_at_ns, received_at_ms, tid, payload_json, decoder_version) VALUES (?,?,?,?,?,?,?,?,?,?,?)`);
|
|
425
|
+
const selectProcess = q('SELECT * FROM runtime_processes WHERE sandbox_id = ? AND pid = ? AND start_time_ns IS ? ORDER BY first_seen_ms DESC LIMIT 1');
|
|
426
|
+
const selectLiveParent = q(`SELECT id FROM runtime_processes WHERE sandbox_id = ? AND pid = ? AND exited_at_ns IS NULL
|
|
427
|
+
ORDER BY first_seen_ms DESC LIMIT 1`);
|
|
428
|
+
const insertProcess = q(`INSERT INTO runtime_processes (id, sandbox_id, pid, start_time_ns, parent_process_id, observed_ppid,
|
|
429
|
+
first_seen_ns, identity_basis, first_seen_ms) VALUES (?,?,?,?,?,?,?,?,?)`);
|
|
430
|
+
const closeProcess = q(`UPDATE runtime_processes SET exited_at_ns = ?, exited_at_ms = ?, exit_code = ?, exit_signal = ?, evidence_lost = NULL
|
|
431
|
+
WHERE id = ? AND exited_at_ns IS NULL`);
|
|
432
|
+
const loseProcesses = q(`UPDATE runtime_processes SET evidence_lost = ?
|
|
433
|
+
WHERE sandbox_id = ? AND exited_at_ns IS NULL AND evidence_lost IS NULL`);
|
|
434
|
+
const endSandboxStmt = q(`UPDATE runtime_sandboxes SET ended_at_ms = MAX(COALESCE(started_at_ms, ?), ?) WHERE id = ? AND ended_at_ms IS NULL`);
|
|
435
|
+
const selectAcceptedAttribution = q(`SELECT id FROM runtime_attributions WHERE tool_call_id = ? AND event_id = ? AND status = 'accepted'`);
|
|
436
|
+
const insertAttribution = q(`INSERT INTO runtime_attributions (id, tool_call_id, event_id, method, confidence, status, basis_json,
|
|
437
|
+
attributor_version, created_at_ms, score, window_start_ms, window_end_ms) VALUES (?,?,?,?,?,'accepted',?,?,?,?,?,?)`);
|
|
438
|
+
|
|
439
|
+
/** One sandbox lifetime per container. Process identity (pid + start
|
|
440
|
+
* time) keeps a restarted container's reused pids apart regardless. */
|
|
441
|
+
function ensureSandbox(containerId, projectId = null) {
|
|
442
|
+
const found = selectSandbox.get(containerId);
|
|
443
|
+
if (found) { if (projectId && !found.project_id) setSandboxProject.run(projectId, containerId); return containerId; }
|
|
444
|
+
insertSandbox.run(containerId, containerId, projectId, Date.now());
|
|
445
|
+
return containerId;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** Appends one collector event, once: the stream is the container and the
|
|
449
|
+
* sequence is the collector's own event id, so a replayed event is a no-op. */
|
|
450
|
+
function recordEvent({ sandboxId, processId = null, raw }) {
|
|
451
|
+
const streamId = sourceStreamId(sandboxId, raw);
|
|
452
|
+
const sequence = Number(raw?.event_id);
|
|
453
|
+
if (!Number.isInteger(sequence) || sequence < 0) return null;
|
|
454
|
+
const existing = selectEventId.get(streamId, sequence);
|
|
455
|
+
if (existing) return existing.id;
|
|
456
|
+
const id = `${streamId}:${sequence}`;
|
|
457
|
+
const tid = intOrNull(raw.tid);
|
|
458
|
+
insertEvent.run(id, sandboxId, processId, streamId, sequence, String(raw.kind || 'unknown'), nsText(raw.timestamp_ns), Date.now(),
|
|
459
|
+
tid && tid >= 1 ? tid : null, JSON.stringify(raw), DECODER_VERSION);
|
|
460
|
+
return id;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function findProcess(sandboxId, pid, startNs) { return selectProcess.get(sandboxId, pid, startNs); }
|
|
464
|
+
|
|
465
|
+
// Fill the index for captures that predate it (or a missed hook update).
|
|
466
|
+
if (!readOnly) indexGuarded(() => backfillSearchIndex());
|
|
467
|
+
|
|
468
|
+
return {
|
|
469
|
+
db, journal, recordExchange,
|
|
470
|
+
|
|
471
|
+
/** Captured exchanges that belong to a turn, newest-first bounded,
|
|
472
|
+
* returned oldest-first. Scoped to the latest session unless `history`. */
|
|
473
|
+
exchangeRows({ history = false, limit = history ? 2000 : 400, externalSession = null } = {}) {
|
|
474
|
+
const session = history ? null : latestSession.get()?.session_id ?? null;
|
|
475
|
+
const rows = externalSession ? recentByExternalId.all(externalSession, limit)
|
|
476
|
+
: session ? recentBySession.all(session, limit) : recentAll.all(limit);
|
|
477
|
+
return rows.map(hydrate).reverse();
|
|
478
|
+
},
|
|
479
|
+
|
|
480
|
+
/** All captured exchanges belonging to one turn (trace), ordered oldest-first. */
|
|
481
|
+
turnExchanges(turnId, {limit = 2001} = {}) {
|
|
482
|
+
if (!turnId) return [];
|
|
483
|
+
return exchangesForTurn.all(turnId, turnId, turnId, limit).map(hydrate);
|
|
484
|
+
},
|
|
485
|
+
|
|
486
|
+
findExchange(exchangeId) {
|
|
487
|
+
const record = selectExchange.get(exchangeId);
|
|
488
|
+
if (!record) return undefined;
|
|
489
|
+
const row = hydrate(record);
|
|
490
|
+
return { payload: row.request.payload, response: row.response.payload, provider: row.provider, route: row.route, session_id: row.session_id };
|
|
491
|
+
},
|
|
492
|
+
|
|
493
|
+
captureCheckpoint(containerId, sourceId) {
|
|
494
|
+
return q('SELECT cursor FROM runtime_capture_checkpoints WHERE sandbox_id=? AND source_id=?').get(containerId, sourceId)?.cursor ?? 0;
|
|
495
|
+
},
|
|
496
|
+
saveCaptureProgress({containerId, projectId, sourceId, cursor, coverage}) {
|
|
497
|
+
transaction(() => {
|
|
498
|
+
ensureSandbox(containerId, projectId);
|
|
499
|
+
const previous=q('SELECT body_json FROM runtime_capture_checkpoints WHERE sandbox_id=? AND source_id=?').get(containerId,sourceId);
|
|
500
|
+
const before=previous?JSON.parse(previous.body_json):{};
|
|
501
|
+
coverage={...coverage,forwarded_through_ms:Math.max(before.forwarded_through_ms??0,coverage?.forwarded_through_ms??0)||null};
|
|
502
|
+
for (const stream of coverage?.streams ?? []) q(`INSERT INTO runtime_capture_streams VALUES(?,?,?)
|
|
503
|
+
ON CONFLICT(id) DO UPDATE SET body_json=excluded.body_json`).run(`${containerId}:${stream.collector_id}:${stream.id}`, containerId, JSON.stringify({...stream,session_id:projectId}));
|
|
504
|
+
q(`INSERT INTO runtime_capture_checkpoints VALUES(?,?,?,?) ON CONFLICT(sandbox_id,source_id)
|
|
505
|
+
DO UPDATE SET cursor=excluded.cursor,body_json=excluded.body_json`).run(containerId, sourceId, cursor, JSON.stringify(coverage));
|
|
506
|
+
});
|
|
507
|
+
},
|
|
508
|
+
recordCaptureEvent({containerId, projectId, raw}) {
|
|
509
|
+
return transaction(() => recordEvent({sandboxId: ensureSandbox(containerId, projectId), raw}));
|
|
510
|
+
},
|
|
511
|
+
captureHealth(sessionId, {since, until, now} = {}) {
|
|
512
|
+
const boxes = q("SELECT id FROM runtime_sandboxes WHERE project_id=? OR id IN (SELECT sandbox_id FROM runtime_capture_streams WHERE json_extract(body_json,'$.session_id')=?)").all(sessionId,sessionId).map(r=>r.id);
|
|
513
|
+
const streams=[], checkpoints=[], events=[];
|
|
514
|
+
for(const box of boxes) {
|
|
515
|
+
streams.push(...q('SELECT body_json FROM runtime_capture_streams WHERE sandbox_id=?').all(box).map(r=>JSON.parse(r.body_json)));
|
|
516
|
+
checkpoints.push(...q('SELECT body_json,cursor FROM runtime_capture_checkpoints WHERE sandbox_id=?').all(box).map(r=>({...JSON.parse(r.body_json),cursor:r.cursor})));
|
|
517
|
+
events.push(...q("SELECT payload_json FROM runtime_events WHERE sandbox_id=? AND event_kind IN ('collection_gap','collection_error','collector_disconnected')").all(box).map(r=>JSON.parse(r.payload_json)));
|
|
518
|
+
}
|
|
519
|
+
return captureHealth({sessionId,streams,checkpoints,events,since,until,now});
|
|
520
|
+
},
|
|
521
|
+
|
|
522
|
+
// ---- runtime evidence writers (the gVisor forwarder)
|
|
523
|
+
|
|
524
|
+
/** A process began running a program (exec_succeeded), or tried to and
|
|
525
|
+
* failed (exec_failed). Records the process once, by pid + start time,
|
|
526
|
+
* and the event every time. Returns ids for attributing it. */
|
|
527
|
+
recordProcessStart({ containerId, projectId = null, raw, startedMs = null }) {
|
|
528
|
+
if (!containerId || !raw) return null;
|
|
529
|
+
return transaction(() => {
|
|
530
|
+
const sandboxId = ensureSandbox(containerId, projectId);
|
|
531
|
+
const pid = intOrNull(raw.pid);
|
|
532
|
+
if (pid == null || pid < 1) return { processId: null, eventId: recordEvent({ sandboxId, raw }) };
|
|
533
|
+
const startNs = nsText(raw.process_start_ns);
|
|
534
|
+
let process = findProcess(sandboxId, pid, startNs);
|
|
535
|
+
if (!process) {
|
|
536
|
+
const ppid = intOrNull(raw.ppid);
|
|
537
|
+
const parent = ppid != null ? selectLiveParent.get(sandboxId, ppid) : null;
|
|
538
|
+
const observed = nsText(raw.timestamp_ns) ?? startNs ?? '0';
|
|
539
|
+
const id = `${sandboxId}:${pid}:${startNs ?? `event-${raw.event_id}`}`;
|
|
540
|
+
insertProcess.run(id, sandboxId, pid, startNs, parent?.id ?? null, ppid != null && ppid >= 0 ? ppid : null, observed,
|
|
541
|
+
startNs ? 'pid+start_time' : 'pid+first_event (start time not reported)', Number.isFinite(startedMs) ? Math.round(startedMs) : null);
|
|
542
|
+
process = { id };
|
|
543
|
+
}
|
|
544
|
+
return { processId: process.id, eventId: recordEvent({ sandboxId, processId: process.id, raw }) };
|
|
545
|
+
});
|
|
546
|
+
},
|
|
547
|
+
|
|
548
|
+
/** Ties an observed process (its exec event) to the tool call it ran
|
|
549
|
+
* for. `method` is how: marker | window | text | inherited. */
|
|
550
|
+
attribute({ toolCallId, eventId, method, confidence = 'corroborated', score = null, windowStartMs = null, windowEndMs = null }) {
|
|
551
|
+
if (!toolCallId || !eventId || selectAcceptedAttribution.get(toolCallId, eventId)) return false;
|
|
552
|
+
insertAttribution.run(randomUUID(), toolCallId, eventId, method, confidence,
|
|
553
|
+
JSON.stringify({ method, score, window_start_ms: windowStartMs, window_end_ms: windowEndMs }), ATTRIBUTOR_VERSION, Date.now(),
|
|
554
|
+
score ?? null, windowStartMs ?? null, windowEndMs ?? null);
|
|
555
|
+
indexGuarded(() => indexCall(toolCallId));
|
|
556
|
+
return true;
|
|
557
|
+
},
|
|
558
|
+
|
|
559
|
+
/** A process exited. Found by pid + start time within its sandbox, so an
|
|
560
|
+
* exit still closes its process after a proxy restart. A process with no
|
|
561
|
+
* row (one that forked but never exec'd) is not recorded. */
|
|
562
|
+
recordProcessExit({ containerId, projectId = null, raw, endedMs = null, exit_code = null, signal = null }) {
|
|
563
|
+
if (!containerId || !raw) return false;
|
|
564
|
+
const closed = transaction(() => {
|
|
565
|
+
const sandboxId = ensureSandbox(containerId, projectId);
|
|
566
|
+
const pid = intOrNull(raw.pid);
|
|
567
|
+
const process = pid != null ? findProcess(sandboxId, pid, nsText(raw.process_start_ns)) : null;
|
|
568
|
+
if (!process || process.exited_at_ns != null) return null;
|
|
569
|
+
const exitSignal = Number.isInteger(signal) && signal > 0 ? signal : null;
|
|
570
|
+
const exitCode = exitSignal ? null : (Number.isInteger(exit_code) && exit_code >= 0 ? exit_code : null);
|
|
571
|
+
closeProcess.run(nsText(raw.timestamp_ns) ?? process.first_seen_ns, Number.isFinite(endedMs) ? Math.round(endedMs) : null,
|
|
572
|
+
exitCode, exitSignal, process.id);
|
|
573
|
+
recordEvent({ sandboxId, processId: process.id, raw });
|
|
574
|
+
return process.id;
|
|
575
|
+
});
|
|
576
|
+
if (!closed) return false;
|
|
577
|
+
checkReported({ processId: closed });
|
|
578
|
+
indexGuarded(() => indexProcessCalls(closed));
|
|
579
|
+
return true;
|
|
580
|
+
},
|
|
581
|
+
|
|
582
|
+
/** The evidence that would close a sandbox's open processes can no
|
|
583
|
+
* longer arrive. Marks them (never as failed) and records why. */
|
|
584
|
+
loseOpenProcesses({ containerId, reason, raw = null, ended = false }) {
|
|
585
|
+
if (!containerId) return 0;
|
|
586
|
+
const changes = transaction(() => {
|
|
587
|
+
const sandboxId = ensureSandbox(containerId);
|
|
588
|
+
if (raw) recordEvent({ sandboxId, raw });
|
|
589
|
+
const lost = Number(loseProcesses.run(String(reason || 'unknown'), sandboxId).changes);
|
|
590
|
+
if (ended) endSandboxStmt.run(Date.now(), Date.now(), sandboxId);
|
|
591
|
+
return lost;
|
|
592
|
+
});
|
|
593
|
+
if (changes) indexGuarded(() => {
|
|
594
|
+
const rows = q(`SELECT DISTINCT a.tool_call_id FROM runtime_attributions a
|
|
595
|
+
JOIN runtime_events ev ON ev.id = a.event_id JOIN runtime_processes p ON p.id = ev.process_id
|
|
596
|
+
WHERE p.sandbox_id = ? AND p.evidence_lost IS NOT NULL`).all(containerId);
|
|
597
|
+
for (const row of rows) indexCall(row.tool_call_id);
|
|
598
|
+
});
|
|
599
|
+
return changes;
|
|
600
|
+
},
|
|
601
|
+
|
|
602
|
+
/** The session's proposed calls that no process has been tied to yet,
|
|
603
|
+
* each with its causal window: from the request that proposed it to the
|
|
604
|
+
* first later request in the session that carried its result back (null
|
|
605
|
+
* while none has). `call_id` is the internal tool-call id. */
|
|
606
|
+
candidatesInWindow(startMs, endMs, { sessionId = null } = {}) {
|
|
607
|
+
if (!sessionId) return [];
|
|
608
|
+
const rows = q(`SELECT c.id, c.external_call_id, c.tool_name, c.arguments_sha, e.started_at_ms,
|
|
609
|
+
(SELECT MIN(r.started_at_ms) FROM agent_context_items ci JOIN agent_exchanges r ON r.id = ci.exchange_id
|
|
610
|
+
WHERE ci.external_call_id = c.external_call_id AND ci.kind IN (${marks(TOOL_RESULT_KINDS)})
|
|
611
|
+
AND r.session_id = e.session_id AND r.started_at_ms >= e.started_at_ms AND r.id != e.id) AS window_end
|
|
612
|
+
FROM agent_tool_calls c
|
|
613
|
+
JOIN agent_exchanges e ON e.id = c.exchange_id
|
|
614
|
+
JOIN agent_sessions s ON s.id = e.session_id
|
|
615
|
+
WHERE e.started_at_ms BETWEEN ? AND ? AND s.external_session_id = ?
|
|
616
|
+
AND NOT EXISTS (SELECT 1 FROM runtime_attributions a
|
|
617
|
+
WHERE a.tool_call_id = c.id AND a.status = 'accepted' AND a.method != 'inherited')
|
|
618
|
+
ORDER BY e.started_at_ms, c.output_index`).all(...TOOL_RESULT_KINDS, startMs, endMs, sessionId);
|
|
619
|
+
return rows.map((row) => ({ call_id: row.id, external_call_id: row.external_call_id, tool_name: row.tool_name, timestamp: row.started_at_ms,
|
|
620
|
+
args: getPayload(row.arguments_sha), window_start: row.started_at_ms, window_end: row.window_end ?? null }));
|
|
621
|
+
},
|
|
622
|
+
|
|
623
|
+
/** A session's sandbox processes that no call accounts for, first seen at
|
|
624
|
+
* or after `sinceMs`, oldest first, each with its first exec event, its
|
|
625
|
+
* parent process, and the call that parent is tied to (null when none). */
|
|
626
|
+
unattributedProcesses({ sessionId, sinceMs }) {
|
|
627
|
+
if (!sessionId) return [];
|
|
628
|
+
const accepted = (process) => `SELECT a.tool_call_id FROM runtime_attributions a JOIN runtime_events e ON e.id = a.event_id
|
|
629
|
+
WHERE e.process_id = ${process} AND a.status = 'accepted' LIMIT 1`;
|
|
630
|
+
return q(`SELECT p.id AS process_id, p.parent_process_id, p.first_seen_ms, ev.id AS event_id, ev.payload_json,
|
|
631
|
+
(${accepted('p.parent_process_id')}) AS parent_call_id
|
|
632
|
+
FROM runtime_processes p
|
|
633
|
+
JOIN runtime_sandboxes s ON s.id = p.sandbox_id
|
|
634
|
+
JOIN runtime_events ev ON ev.id = (SELECT e.id FROM runtime_events e
|
|
635
|
+
WHERE e.process_id = p.id AND e.event_kind = 'exec_succeeded' ORDER BY e.stream_sequence LIMIT 1)
|
|
636
|
+
WHERE s.project_id = ? AND p.first_seen_ms >= ? AND NOT EXISTS (${accepted('p.id')})
|
|
637
|
+
ORDER BY p.first_seen_ms, ev.stream_sequence`).all(sessionId, Math.round(sinceMs))
|
|
638
|
+
.map((row) => ({ process_id: row.process_id, parent_process_id: row.parent_process_id ?? null, event_id: row.event_id, first_seen_ms: row.first_seen_ms,
|
|
639
|
+
parent_call_id: row.parent_call_id ?? null, raw: fromJson(row.payload_json) ?? {} }));
|
|
640
|
+
},
|
|
641
|
+
|
|
642
|
+
/** Changes whenever anything the trace list shows could have: a cheap
|
|
643
|
+
* key for caching it. Counts only ever grow; a result reported back or a
|
|
644
|
+
* process exiting changes a count too. */
|
|
645
|
+
dataVersion() {
|
|
646
|
+
return Object.values(q(`SELECT (SELECT count(*) FROM agent_exchanges) AS exchanges,
|
|
647
|
+
(SELECT count(*) FROM agent_tool_calls WHERE reported_result_sha IS NOT NULL) AS results,
|
|
648
|
+
(SELECT count(*) FROM runtime_events) AS events, (SELECT count(*) FROM runtime_attributions) AS attributions,
|
|
649
|
+
(SELECT count(*) FROM workspace_snapshots) AS snapshots,
|
|
650
|
+
(SELECT COALESCE(MAX(json_extract(body_json,'$.checked_at_ms')),0) FROM runtime_capture_checkpoints) AS health`).get()).join(':');
|
|
651
|
+
},
|
|
652
|
+
|
|
653
|
+
// ---- workspace snapshots (a sandboxed Claude Code's hook)
|
|
654
|
+
|
|
655
|
+
/** Where the project folder stood after a call or at a prompt. The same
|
|
656
|
+
* snapshot sent twice (a resend after 409) is recorded once. */
|
|
657
|
+
recordSnapshot({ sessionId, agentSessionId = null, snapshot }) {
|
|
658
|
+
const id = createHash('sha256').update([sessionId, agentSessionId, snapshot.taken_on, snapshot.call_id, snapshot.commit, snapshot.taken_at].join('\0')).digest('hex');
|
|
659
|
+
q(`INSERT OR IGNORE INTO workspace_snapshots (id, session_id, agent_session_id, taken_on, call_id, commit_sha, parent_sha, changed, taken_at_ms, changes_json)
|
|
660
|
+
VALUES (?,?,?,?,?,?,?,?,?,?)`).run(id, sessionId, agentSessionId, snapshot.taken_on, snapshot.call_id ?? null, snapshot.commit,
|
|
661
|
+
snapshot.parent ?? null, snapshot.changed ? 1 : 0, Math.round(toMs(snapshot.taken_at) ?? Date.now()),
|
|
662
|
+
JSON.stringify({ changes: snapshot.changes ?? [], too_big: snapshot.too_big ?? [] }));
|
|
663
|
+
return id;
|
|
664
|
+
},
|
|
665
|
+
|
|
666
|
+
/** Every snapshot of one agent session filed under `sessionId`, oldest first. */
|
|
667
|
+
snapshotsForAgentSession({ sessionId, agentSessionId }) {
|
|
668
|
+
return q(`SELECT taken_on, call_id, commit_sha, parent_sha, changed, taken_at_ms FROM workspace_snapshots
|
|
669
|
+
WHERE session_id = ? AND agent_session_id = ? ORDER BY taken_at_ms`).all(sessionId, agentSessionId)
|
|
670
|
+
.map((row) => ({ taken_on: row.taken_on, call_id: row.call_id, commit: row.commit_sha, parent: row.parent_sha ?? null,
|
|
671
|
+
changed: row.changed === 1, taken_at_ms: row.taken_at_ms }));
|
|
672
|
+
},
|
|
673
|
+
|
|
674
|
+
/** The snapshot taken after each of these calls (external call ids), by call id. */
|
|
675
|
+
snapshotsForCalls(callIds, { sessionId } = {}) {
|
|
676
|
+
const list = [...new Set((callIds ?? []).filter(Boolean))];
|
|
677
|
+
if (!list.length) return new Map();
|
|
678
|
+
let sql = `SELECT call_id, commit_sha, parent_sha, changed, taken_at_ms, changes_json FROM workspace_snapshots
|
|
679
|
+
WHERE call_id IN (${marks(list)})`;
|
|
680
|
+
const params = [...list];
|
|
681
|
+
if (sessionId) {
|
|
682
|
+
sql += ' AND (session_id = ? OR agent_session_id = ?)';
|
|
683
|
+
params.push(sessionId, sessionId);
|
|
684
|
+
}
|
|
685
|
+
sql += ' ORDER BY taken_at_ms';
|
|
686
|
+
const rows = q(sql).all(...params);
|
|
687
|
+
return new Map(rows.map((row) => {
|
|
688
|
+
const body = fromJson(row.changes_json) ?? {};
|
|
689
|
+
return [row.call_id, { commit: row.commit_sha, parent: row.parent_sha, changed: row.changed === 1, taken_at: toIso(row.taken_at_ms),
|
|
690
|
+
changes: body.changes ?? [], too_big: body.too_big ?? [] }];
|
|
691
|
+
}));
|
|
692
|
+
},
|
|
693
|
+
|
|
694
|
+
// ---- readers (the dashboard)
|
|
695
|
+
|
|
696
|
+
/** Proposed vs observed for every call in these exchanges: the call, and
|
|
697
|
+
* the process it ran as (its accepted, non-inherited attribution). */
|
|
698
|
+
divergence(exchangeIds) {
|
|
699
|
+
if (!exchangeIds.length) return [];
|
|
700
|
+
return q(`SELECT c.external_call_id AS call_id, c.exchange_id AS span_id, c.output_index, c.tool_name AS name,
|
|
701
|
+
a.method, a.confidence, a.score, a.window_start_ms, a.window_end_ms, a.reported_check, a.event_id,
|
|
702
|
+
json_extract(ev.payload_json, '$.argv') AS attributed_argv_json,
|
|
703
|
+
p.id AS process_id, p.pid, p.start_time_ns, p.first_seen_ms, p.exited_at_ms, p.exited_at_ns, p.exit_code, p.exit_signal, p.evidence_lost,
|
|
704
|
+
${PROCESS_FACTS},
|
|
705
|
+
(SELECT COUNT(*) FROM runtime_attributions d WHERE d.tool_call_id = c.id AND d.status = 'accepted' AND d.method = 'inherited') AS descendant_count
|
|
706
|
+
FROM agent_tool_calls c
|
|
707
|
+
LEFT JOIN runtime_attributions a ON a.id = (SELECT a2.id FROM runtime_attributions a2
|
|
708
|
+
WHERE a2.tool_call_id = c.id AND a2.status = 'accepted' AND a2.method != 'inherited' ORDER BY a2.created_at_ms LIMIT 1)
|
|
709
|
+
LEFT JOIN runtime_events ev ON ev.id = a.event_id
|
|
710
|
+
LEFT JOIN runtime_processes p ON p.id = ev.process_id
|
|
711
|
+
WHERE c.exchange_id IN (${marks(exchangeIds)}) ORDER BY c.exchange_id, c.output_index`).all(...exchangeIds)
|
|
712
|
+
.map((row) => {
|
|
713
|
+
const { status, error } = processOutcome(row);
|
|
714
|
+
return {
|
|
715
|
+
call_id: row.call_id, span_id: row.span_id, output_index: row.output_index, name: row.name,
|
|
716
|
+
outcome: row.event_id ? 'as_proposed' : 'not_executed',
|
|
717
|
+
status, error, source: row.event_id ? 'gvisor' : null,
|
|
718
|
+
match_score: row.score, match_basis: row.method === 'marker' ? 'id' : row.method ?? null,
|
|
719
|
+
kernel_confirmed: row.event_id ? 1 : 0, kernel_match_score: row.score,
|
|
720
|
+
tier: row.confidence ? TIER[row.confidence] : null,
|
|
721
|
+
pid: row.pid ?? null, exit_code: row.exit_code ?? null, reported_check: row.reported_check ?? null,
|
|
722
|
+
exec_id: row.event_id ?? null, start_time_ns: row.start_time_ns ?? null,
|
|
723
|
+
// The command that was matched to the call. A process can exec
|
|
724
|
+
// more than once (Codex's environment setup, then the command it
|
|
725
|
+
// was asked to run), and the matched exec is the one to show.
|
|
726
|
+
started_at: toIso(row.first_seen_ms), ended_at: toIso(row.exited_at_ms), argv: fromJson(row.attributed_argv_json ?? row.argv_json),
|
|
727
|
+
window_start_ms: row.window_start_ms ?? null, window_end_ms: row.window_end_ms ?? null,
|
|
728
|
+
descendant_count: row.descendant_count ?? 0,
|
|
729
|
+
};
|
|
730
|
+
});
|
|
731
|
+
},
|
|
732
|
+
|
|
733
|
+
/** Processes that ran underneath each call in these exchanges (inherited
|
|
734
|
+
* attributions), in the order they started. */
|
|
735
|
+
descendants(exchangeIds) {
|
|
736
|
+
if (!exchangeIds.length) return [];
|
|
737
|
+
return q(`SELECT c.external_call_id AS parent_call_id, ev.id AS event_id, a.confidence,
|
|
738
|
+
json_extract(ev.payload_json, '$.argv') AS attributed_argv_json,
|
|
739
|
+
p.id AS process_id, p.pid, p.observed_ppid, p.start_time_ns, p.first_seen_ms, p.exited_at_ms, p.exited_at_ns,
|
|
740
|
+
p.exit_code, p.exit_signal, p.evidence_lost, ${PROCESS_FACTS}
|
|
741
|
+
FROM runtime_attributions a
|
|
742
|
+
JOIN agent_tool_calls c ON c.id = a.tool_call_id
|
|
743
|
+
JOIN runtime_events ev ON ev.id = a.event_id
|
|
744
|
+
JOIN runtime_processes p ON p.id = ev.process_id
|
|
745
|
+
WHERE a.status = 'accepted' AND a.method = 'inherited' AND c.exchange_id IN (${marks(exchangeIds)})
|
|
746
|
+
ORDER BY c.external_call_id, p.first_seen_ms, p.pid`).all(...exchangeIds)
|
|
747
|
+
.map((row) => ({ parent_call_id: row.parent_call_id, id: row.event_id, tier: TIER[row.confidence] ?? 'corroborated',
|
|
748
|
+
pid: row.pid, ppid: row.observed_ppid, start_time_ns: row.start_time_ns, argv: fromJson(row.attributed_argv_json ?? row.argv_json) ?? [],
|
|
749
|
+
...processOutcome(row), started_at: toIso(row.first_seen_ms), ended_at: toIso(row.exited_at_ms) }));
|
|
750
|
+
},
|
|
751
|
+
|
|
752
|
+
/** Which call each of a sandbox's processes belongs to, for annotating
|
|
753
|
+
* its process tree. A process with no accepted attribution comes back
|
|
754
|
+
* with both call ids null: it ran, and nothing accounts for it. */
|
|
755
|
+
attributionsForProcesses({ containerId, pids }) {
|
|
756
|
+
const list = [...new Set((pids ?? []).filter(Number.isInteger))];
|
|
757
|
+
if (!containerId || !list.length) return [];
|
|
758
|
+
return q(`SELECT p.pid, p.start_time_ns,
|
|
759
|
+
(SELECT c.external_call_id FROM runtime_attributions a JOIN runtime_events ev ON ev.id = a.event_id
|
|
760
|
+
JOIN agent_tool_calls c ON c.id = a.tool_call_id
|
|
761
|
+
WHERE ev.process_id = p.id AND a.status = 'accepted' AND a.method != 'inherited' LIMIT 1) AS call_id,
|
|
762
|
+
(SELECT c.external_call_id FROM runtime_attributions a JOIN runtime_events ev ON ev.id = a.event_id
|
|
763
|
+
JOIN agent_tool_calls c ON c.id = a.tool_call_id
|
|
764
|
+
WHERE ev.process_id = p.id AND a.status = 'accepted' AND a.method = 'inherited' LIMIT 1) AS parent_call_id,
|
|
765
|
+
(SELECT a.confidence FROM runtime_attributions a JOIN runtime_events ev ON ev.id = a.event_id
|
|
766
|
+
WHERE ev.process_id = p.id AND a.status = 'accepted' LIMIT 1) AS confidence,
|
|
767
|
+
(SELECT a.method FROM runtime_attributions a JOIN runtime_events ev ON ev.id = a.event_id
|
|
768
|
+
WHERE ev.process_id = p.id AND a.status = 'accepted' AND a.method != 'inherited' LIMIT 1) AS method
|
|
769
|
+
FROM runtime_processes p WHERE p.sandbox_id = ? AND p.pid IN (${marks(list)})`).all(containerId, ...list)
|
|
770
|
+
.map((row) => ({ pid: row.pid, start_time_ns: row.start_time_ns, call_id: row.call_id ?? null, parent_call_id: row.parent_call_id ?? null,
|
|
771
|
+
tier: row.confidence ? TIER[row.confidence] : 'unverified', match_basis: row.method === 'marker' ? 'id' : row.method ?? null }));
|
|
772
|
+
},
|
|
773
|
+
|
|
774
|
+
// ---- auxiliary features
|
|
775
|
+
|
|
776
|
+
recordProxyError(row) {
|
|
777
|
+
const inserted = q('INSERT INTO proxy_errors (trace_id, occurred_at_ms, provider, route, error, cause_json) VALUES (?,?,?,?,?,?)')
|
|
778
|
+
.run(row.trace_id ?? null, toMs(row.timestamp), row.provider ?? null, row.route ?? null, row.error ?? null, asJson(row.cause));
|
|
779
|
+
indexGuarded(() => indexProxyErrorRow({ rowid: Number(inserted.lastInsertRowid), occurred_at_ms: toMs(row.timestamp),
|
|
780
|
+
provider: row.provider ?? null, route: row.route ?? null, error: row.error ?? null, cause_json: asJson(row.cause) }));
|
|
781
|
+
},
|
|
782
|
+
saveExperiment(job) {
|
|
783
|
+
q(`INSERT OR REPLACE INTO lab_experiments (id, exchange_id, created_at_ms, status, kind, model, body_json) VALUES (?,?,?,?,?,?,?)`)
|
|
784
|
+
.run(job.id, job.exchange_id ?? null, toMs(job.created_at) ?? Date.now(), job.status ?? null, job.kind ?? 'experiment', job.model ?? null, JSON.stringify(job));
|
|
785
|
+
},
|
|
786
|
+
loadExperiments(limit = 200) {
|
|
787
|
+
return q('SELECT body_json FROM lab_experiments ORDER BY created_at_ms DESC LIMIT ?').all(limit)
|
|
788
|
+
.map((record) => fromJson(record.body_json)).filter(Boolean);
|
|
789
|
+
},
|
|
790
|
+
getExplanation(key) { return fromJson(q('SELECT body_json FROM step_explanations WHERE key = ?').get(key)?.body_json ?? null); },
|
|
791
|
+
putExplanation(key, value) { q('INSERT OR REPLACE INTO step_explanations (key, created_at_ms, body_json) VALUES (?,?,?)').run(key, Date.now(), JSON.stringify(value)); },
|
|
792
|
+
getSummary(key) { return fromJson(q('SELECT body_json FROM exchange_summaries WHERE key = ?').get(key)?.body_json ?? null); },
|
|
793
|
+
putSummary(key, exchangeId, value) {
|
|
794
|
+
q('INSERT OR REPLACE INTO exchange_summaries (key, exchange_id, created_at_ms, body_json) VALUES (?,?,?,?)').run(key, exchangeId ?? null, Date.now(), JSON.stringify(value));
|
|
795
|
+
},
|
|
796
|
+
summaryForSpan(exchangeId) {
|
|
797
|
+
return fromJson(q('SELECT body_json FROM exchange_summaries WHERE exchange_id = ? ORDER BY created_at_ms DESC LIMIT 1').get(exchangeId)?.body_json ?? null);
|
|
798
|
+
},
|
|
799
|
+
/** Model-written sentence for a sandbox command no rule could describe,
|
|
800
|
+
* keyed by the command so the same command is never paid for twice. */
|
|
801
|
+
getCommandDescription(command) {
|
|
802
|
+
return q('SELECT description FROM command_descriptions WHERE key = ?').get(sha256(String(command)))?.description ?? null;
|
|
803
|
+
},
|
|
804
|
+
putCommandDescription(command, description, model = null) {
|
|
805
|
+
q('INSERT OR REPLACE INTO command_descriptions (key, command, description, model, created_at_ms) VALUES (?,?,?,?,?)')
|
|
806
|
+
.run(sha256(String(command)), String(command), String(description), model, Date.now());
|
|
807
|
+
},
|
|
808
|
+
/** What each tool call asked for and what the agent was told back, keyed
|
|
809
|
+
* by the external call id the attribution rows carry. The raw material for
|
|
810
|
+
* a Jev assessment; call-assessment.mjs decides how much of it to send. */
|
|
811
|
+
toolCallContexts(externalCallIds) {
|
|
812
|
+
const list = [...new Set((externalCallIds ?? []).filter((id) => typeof id === 'string' && id))];
|
|
813
|
+
if (!list.length) return new Map();
|
|
814
|
+
const rows = q(`SELECT c.external_call_id, c.tool_name, c.arguments_sha, c.reported_result_sha
|
|
815
|
+
FROM agent_tool_calls c JOIN agent_exchanges e ON e.id = c.exchange_id
|
|
816
|
+
WHERE c.external_call_id IN (${marks(list)}) ORDER BY e.started_at_ms`).all(...list);
|
|
817
|
+
// Later rows win: a replayed call keeps the newest arguments and result.
|
|
818
|
+
return new Map(rows.map((row) => [row.external_call_id, {
|
|
819
|
+
tool_name: row.tool_name, args: getPayload(row.arguments_sha), result: getPayload(row.reported_result_sha) }]));
|
|
820
|
+
},
|
|
821
|
+
toolCallResult(externalCallId, { exchangeId, sessionId } = {}) {
|
|
822
|
+
if (!externalCallId) return null;
|
|
823
|
+
let sql = `SELECT c.reported_result_sha
|
|
824
|
+
FROM agent_tool_calls c JOIN agent_exchanges e ON e.id = c.exchange_id
|
|
825
|
+
WHERE c.external_call_id = ? AND c.reported_result_sha IS NOT NULL`;
|
|
826
|
+
const params = [externalCallId];
|
|
827
|
+
if (exchangeId) {
|
|
828
|
+
sql += ' AND c.exchange_id = ?';
|
|
829
|
+
params.push(exchangeId);
|
|
830
|
+
} else if (sessionId) {
|
|
831
|
+
sql += ' AND (e.session_id = ? OR e.session_id IN (SELECT id FROM agent_sessions WHERE external_session_id = ?))';
|
|
832
|
+
params.push(sessionId, sessionId);
|
|
833
|
+
}
|
|
834
|
+
sql += ' ORDER BY e.started_at_ms DESC LIMIT 1';
|
|
835
|
+
const row = q(sql).get(...params);
|
|
836
|
+
return row ? getPayload(row.reported_result_sha) : null;
|
|
837
|
+
},
|
|
838
|
+
recordToolResult(externalCallId, resultItem, { exchangeId, sessionId } = {}) {
|
|
839
|
+
if (!externalCallId || !resultItem) return null;
|
|
840
|
+
let sql = `SELECT c.id FROM agent_tool_calls c
|
|
841
|
+
JOIN agent_exchanges e ON e.id = c.exchange_id
|
|
842
|
+
WHERE c.external_call_id = ?`;
|
|
843
|
+
const params = [externalCallId];
|
|
844
|
+
if (exchangeId) {
|
|
845
|
+
sql += ' AND c.exchange_id = ?';
|
|
846
|
+
params.push(exchangeId);
|
|
847
|
+
} else if (sessionId) {
|
|
848
|
+
sql += ' AND (e.session_id = ? OR e.session_id IN (SELECT id FROM agent_sessions WHERE external_session_id = ?))';
|
|
849
|
+
params.push(sessionId, sessionId);
|
|
850
|
+
}
|
|
851
|
+
sql += ' ORDER BY e.started_at_ms DESC LIMIT 1';
|
|
852
|
+
const call = q(sql).get(...params);
|
|
853
|
+
if (!call) return null;
|
|
854
|
+
const payloadSha = putPayload(resultItem);
|
|
855
|
+
setReportedResult.run(payloadSha, call.id);
|
|
856
|
+
checkReported({ toolCallId: call.id });
|
|
857
|
+
indexGuarded(() => indexCall(call.id));
|
|
858
|
+
return call.id;
|
|
859
|
+
},
|
|
860
|
+
getCallAssessment(key) { return fromJson(q('SELECT body_json FROM call_assessments WHERE key = ?').get(key)?.body_json ?? null); },
|
|
861
|
+
putCallAssessment(key, callId, model, value) {
|
|
862
|
+
q('INSERT OR REPLACE INTO call_assessments (key, call_id, model, created_at_ms, body_json) VALUES (?,?,?,?,?)')
|
|
863
|
+
.run(key, callId ?? null, model ?? null, Date.now(), JSON.stringify(value));
|
|
864
|
+
},
|
|
865
|
+
|
|
866
|
+
/** Splunk-style search over the derived index (proxy/search.mjs). */
|
|
867
|
+
search(options) { return runSearch(db, options); },
|
|
868
|
+
failureSignatures(options) { return topFailureSignatures(db, options); },
|
|
869
|
+
/** Rebuild the derived search index from the evidence tables. */
|
|
870
|
+
reindexSearch() {
|
|
871
|
+
return transaction(() => {
|
|
872
|
+
db.exec('DELETE FROM search_events');
|
|
873
|
+
for (const call of q('SELECT id FROM agent_tool_calls').all()) indexCall(call.id);
|
|
874
|
+
for (const turn of q('SELECT id FROM agent_turns').all()) indexTurn(turn.id);
|
|
875
|
+
for (const row of q('SELECT rowid, * FROM proxy_errors').all()) indexProxyErrorRow(row);
|
|
876
|
+
return q('SELECT COUNT(*) AS n FROM search_events').get().n;
|
|
877
|
+
});
|
|
878
|
+
},
|
|
879
|
+
|
|
880
|
+
stats() {
|
|
881
|
+
const one = (sql) => db.prepare(sql).get();
|
|
882
|
+
return {
|
|
883
|
+
sessions: one('SELECT COUNT(*) AS n FROM agent_sessions').n,
|
|
884
|
+
turns: one('SELECT COUNT(*) AS n FROM agent_turns').n,
|
|
885
|
+
exchanges: one('SELECT COUNT(*) AS n FROM agent_exchanges').n,
|
|
886
|
+
payloads: one('SELECT COUNT(*) AS n, COALESCE(SUM(length(content)),0) AS bytes FROM agent_payloads'),
|
|
887
|
+
processes: one('SELECT COUNT(*) AS n FROM runtime_processes').n,
|
|
888
|
+
};
|
|
889
|
+
},
|
|
890
|
+
|
|
891
|
+
/** Deletes these sessions (agent_sessions.external_session_id: a local
|
|
892
|
+
* sandbox's rtp- id, or a Claude Code session id) and all that hangs off
|
|
893
|
+
* them -- turns, exchanges, tool calls, the sandbox's recorded processes
|
|
894
|
+
* and events, snapshots and search rows -- then any payload nothing
|
|
895
|
+
* refers to any more (payloads are shared by content, so one another
|
|
896
|
+
* session uses stays). Returns how many sessions went. Used when a local
|
|
897
|
+
* sandbox is destroyed with its logs. */
|
|
898
|
+
forgetSessions(sessionIds) {
|
|
899
|
+
if (!sessionIds.length) return 0;
|
|
900
|
+
// A sandbox's Playground forks (lab_experiments, kind fork) go with it,
|
|
901
|
+
// and so do their own sessions, which are filed under the fork's id.
|
|
902
|
+
const parents = sessionIds.map(() => '?').join(',');
|
|
903
|
+
const forks = q(`SELECT id FROM lab_experiments WHERE kind = 'fork' AND json_extract(body_json, '$.parent') IN (${parents})`)
|
|
904
|
+
.all(...sessionIds).map((row) => row.id);
|
|
905
|
+
const externalIds = [...sessionIds, ...forks];
|
|
906
|
+
const marks = externalIds.map(() => '?').join(',');
|
|
907
|
+
// Runtime events, syscalls and their classifications are append-only
|
|
908
|
+
// (schema/runtime_evidence.sql, classification.sql):
|
|
909
|
+
// nothing may change recorded evidence. Their owner deleting a whole
|
|
910
|
+
// sandbox is the one exception, so those guards are lifted for these
|
|
911
|
+
// deletes and put back as they were, in the same transaction: if any of
|
|
912
|
+
// it fails, nothing changed and the guards were never off.
|
|
913
|
+
const guards = q(`SELECT sql FROM sqlite_schema WHERE type = 'trigger'
|
|
914
|
+
AND name IN ('runtime_events_no_delete', 'runtime_syscall_calls_no_delete', 'classification_results_no_delete')`).all().map((row) => row.sql);
|
|
915
|
+
return transaction(() => {
|
|
916
|
+
db.exec('DROP TRIGGER IF EXISTS runtime_events_no_delete; DROP TRIGGER IF EXISTS runtime_syscall_calls_no_delete; DROP TRIGGER IF EXISTS classification_results_no_delete;');
|
|
917
|
+
const sessions = `SELECT id FROM agent_sessions WHERE external_session_id IN (${marks})`;
|
|
918
|
+
const exchanges = `SELECT id FROM agent_exchanges WHERE session_id IN (${sessions})`;
|
|
919
|
+
const calls = `SELECT id FROM agent_tool_calls WHERE exchange_id IN (${exchanges})`;
|
|
920
|
+
const boxes = `SELECT id FROM runtime_sandboxes WHERE project_id IN (${marks})`;
|
|
921
|
+
for (const sql of [
|
|
922
|
+
`DELETE FROM search_events WHERE session_external IN (${marks})`,
|
|
923
|
+
`DELETE FROM workspace_snapshots WHERE session_id IN (${marks})`,
|
|
924
|
+
`DELETE FROM runtime_attributions WHERE tool_call_id IN (${calls})
|
|
925
|
+
OR event_id IN (SELECT id FROM runtime_events WHERE sandbox_id IN (${boxes}))`,
|
|
926
|
+
`DELETE FROM classification_results WHERE syscall_call_id IN (SELECT id FROM runtime_syscall_calls WHERE sandbox_id IN (${boxes}))`,
|
|
927
|
+
`DELETE FROM file_label_assignments WHERE target_id IN (SELECT id FROM file_operation_targets WHERE sandbox_id IN (${boxes}))`,
|
|
928
|
+
`DELETE FROM file_operation_targets WHERE sandbox_id IN (${boxes})`,
|
|
929
|
+
`DELETE FROM file_objects WHERE sandbox_id IN (${boxes})`,
|
|
930
|
+
`DELETE FROM runtime_syscall_calls WHERE sandbox_id IN (${boxes})`,
|
|
931
|
+
`DELETE FROM runtime_events WHERE sandbox_id IN (${boxes})`,
|
|
932
|
+
`DELETE FROM runtime_processes WHERE sandbox_id IN (${boxes})`,
|
|
933
|
+
`DELETE FROM runtime_capture_streams WHERE sandbox_id IN (${boxes})`,
|
|
934
|
+
`DELETE FROM runtime_capture_checkpoints WHERE sandbox_id IN (${boxes})`,
|
|
935
|
+
`DELETE FROM runtime_sandboxes WHERE project_id IN (${marks})`,
|
|
936
|
+
`DELETE FROM agent_context_items WHERE exchange_id IN (${exchanges})`,
|
|
937
|
+
`DELETE FROM agent_tool_calls WHERE exchange_id IN (${exchanges})`,
|
|
938
|
+
`DELETE FROM exchange_summaries WHERE exchange_id IN (${exchanges})`,
|
|
939
|
+
`DELETE FROM lab_experiments WHERE exchange_id IN (${exchanges}) OR id IN (${marks})`,
|
|
940
|
+
`DELETE FROM agent_exchanges WHERE session_id IN (${sessions})`,
|
|
941
|
+
`DELETE FROM agent_turns WHERE session_id IN (${sessions})`,
|
|
942
|
+
]) {
|
|
943
|
+
const uses = (sql.match(/\?/g) || []).length / externalIds.length;
|
|
944
|
+
q(sql).run(...Array.from({ length: uses }, () => externalIds).flat());
|
|
945
|
+
}
|
|
946
|
+
const gone = q(`DELETE FROM agent_sessions WHERE external_session_id IN (${marks})`).run(...externalIds).changes;
|
|
947
|
+
db.exec(`DELETE FROM agent_payloads WHERE sha NOT IN (
|
|
948
|
+
SELECT user_prompt_sha FROM agent_turns WHERE user_prompt_sha IS NOT NULL
|
|
949
|
+
UNION SELECT final_response_sha FROM agent_turns WHERE final_response_sha IS NOT NULL
|
|
950
|
+
UNION SELECT request_sha FROM agent_exchanges WHERE request_sha IS NOT NULL
|
|
951
|
+
UNION SELECT response_sha FROM agent_exchanges WHERE response_sha IS NOT NULL
|
|
952
|
+
UNION SELECT tools_sha FROM agent_exchanges WHERE tools_sha IS NOT NULL
|
|
953
|
+
UNION SELECT arguments_sha FROM agent_tool_calls WHERE arguments_sha IS NOT NULL
|
|
954
|
+
UNION SELECT reported_result_sha FROM agent_tool_calls WHERE reported_result_sha IS NOT NULL
|
|
955
|
+
UNION SELECT payload_sha FROM agent_context_items)`);
|
|
956
|
+
for (const sql of guards) db.exec(sql);
|
|
957
|
+
return gone;
|
|
958
|
+
});
|
|
959
|
+
},
|
|
960
|
+
|
|
961
|
+
close() { db.close(); },
|
|
962
|
+
};
|
|
963
|
+
|
|
964
|
+
/** Compares the agent's own report of a finished call (the runner's exit
|
|
965
|
+
* code and wall time) with the observed exit of the process the call ran
|
|
966
|
+
* as. Runs whenever either side arrives. Duration is exec-to-exit on the
|
|
967
|
+
* proxy clock with one offset, so clock skew between the proxy and the
|
|
968
|
+
* sandbox cannot distort it. Leaves the check null when nothing comparable
|
|
969
|
+
* was reported (a command still running, no runner header). */
|
|
970
|
+
function checkReported({ toolCallId = null, processId = null }) {
|
|
971
|
+
const rows = q(`SELECT a.id, c.reported_result_sha, p.exit_code, p.exit_signal, p.exited_at_ns, p.first_seen_ms, p.exited_at_ms
|
|
972
|
+
FROM runtime_attributions a
|
|
973
|
+
JOIN runtime_events ev ON ev.id = a.event_id
|
|
974
|
+
JOIN runtime_processes p ON p.id = ev.process_id
|
|
975
|
+
JOIN agent_tool_calls c ON c.id = a.tool_call_id
|
|
976
|
+
WHERE a.status = 'accepted' AND a.method != 'inherited' AND a.reported_check IS NULL
|
|
977
|
+
AND ${toolCallId ? 'c.id = ?' : 'p.id = ?'}`).all(toolCallId ?? processId);
|
|
978
|
+
for (const row of rows) {
|
|
979
|
+
if (!row.reported_result_sha || row.exited_at_ns == null || row.exit_code == null) continue;
|
|
980
|
+
const reported = toolResultFacts(getPayload(row.reported_result_sha));
|
|
981
|
+
if (reported.exitCode === null) continue;
|
|
982
|
+
let verdict = reported.exitCode === row.exit_code ? 'agrees' : 'exit_code_differs';
|
|
983
|
+
const observedMs = Number.isFinite(row.first_seen_ms) && Number.isFinite(row.exited_at_ms) ? row.exited_at_ms - row.first_seen_ms : null;
|
|
984
|
+
if (verdict === 'agrees' && reported.wallMs !== null && observedMs !== null && observedMs >= 0) {
|
|
985
|
+
// Generous on purpose: the runner starts its clock before the spawn
|
|
986
|
+
// the runtime sees, and a false "differs" costs more trust than it earns.
|
|
987
|
+
const tolerance = 0.2 * Math.max(observedMs, reported.wallMs) + 250;
|
|
988
|
+
if (Math.abs(observedMs - reported.wallMs) > tolerance) verdict = 'duration_differs';
|
|
989
|
+
}
|
|
990
|
+
q('UPDATE runtime_attributions SET reported_check = ? WHERE id = ?').run(verdict, row.id);
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
}
|