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.
- 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,625 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns raw gVisor process events into something a person can read: one line
|
|
3
|
+
* per command the agent actually ran, in plain English, with the noise folded
|
|
4
|
+
* away and counted.
|
|
5
|
+
*
|
|
6
|
+
* Why this is needed: the collector records every exec faithfully, and that is
|
|
7
|
+
* the point -- but faithfully includes Codex's own machinery. A single
|
|
8
|
+
* `npm test` produces four exec events in one pid:
|
|
9
|
+
*
|
|
10
|
+
* /bin/bash -c __CODEX_SNAPSHOT_OVERRIDE_SET_0=... 16,027 characters
|
|
11
|
+
* /bin/bash -c cd /workspace/demo && npm test the command as written
|
|
12
|
+
* /usr/local/bin/npm test re-exec, same pid
|
|
13
|
+
* node /usr/local/bin/npm test re-exec, same pid
|
|
14
|
+
*
|
|
15
|
+
* Rendered verbatim that is unreadable, and the 16k preamble collapses any
|
|
16
|
+
* table it lands in. So events are folded -- never dropped. `fold()` returns
|
|
17
|
+
* the commands plus a count of exactly what it folded, and callers are
|
|
18
|
+
* expected to show that count and offer the raw events. A tool that claims to
|
|
19
|
+
* show what really happened does not get to quietly hide two thirds of it.
|
|
20
|
+
*
|
|
21
|
+
* Claude Code has machinery of its own. It runs each Bash tool call as
|
|
22
|
+
*
|
|
23
|
+
* /bin/bash -c source <shell snapshot> … && eval '<the command>' && pwd -P >| /tmp/claude-…-cwd
|
|
24
|
+
*
|
|
25
|
+
* so the command the agent wrote is the eval's argument, unwrapped here. And
|
|
26
|
+
* around the tool calls it starts processes no tool call asked for: it writes
|
|
27
|
+
* that shell snapshot, lists the project's files, reads the git identity and
|
|
28
|
+
* the environment, and runs RayTrace's own hook (which snapshots the project
|
|
29
|
+
* with git). Those, and everything they start, are folded as machinery.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
const CODEX_PREAMBLE = /__CODEX_SNAPSHOT_(?:OVERRIDE|PROXY)/;
|
|
33
|
+
/** npm probes the architecture before running a script; it says nothing about
|
|
34
|
+
* what the agent asked for. Matched exactly, never by prefix. */
|
|
35
|
+
const MACHINERY = [/^getconf (?:LONG_BIT|_NPROCESSORS_ONLN)$/];
|
|
36
|
+
/** What Claude Code and RayTrace's hook start by themselves (see the header).
|
|
37
|
+
* Matched on the whole command line as executed; whatever such a process
|
|
38
|
+
* starts is folded with it. An agent's own `env` or `rg` arrives inside the
|
|
39
|
+
* tool-call wrapper, so it never has these shapes. */
|
|
40
|
+
const HARNESS = [
|
|
41
|
+
/^(?:\S*\/)?(?:sh|bash) -c node \/opt\/raytace-claude\/hook\.mjs$/,
|
|
42
|
+
/^node \/opt\/raytace-claude\/hook\.mjs$/,
|
|
43
|
+
/^(?:\S*\/)?bash -c -l SNAPSHOT_FILE=\S*\/\.claude\/shell-snapshots\//,
|
|
44
|
+
/^(?:\S*\/)?bash -c env$/,
|
|
45
|
+
/^(?:\S*\/)?git (?:-c \S+ )+config --get user\.(?:email|name)$/,
|
|
46
|
+
/^(?:\S*\/)?rg --no-config --files --follow --hidden /,
|
|
47
|
+
];
|
|
48
|
+
/** Claude Code's wrapper around one Bash tool call; group 1 is the command, quoted for eval. */
|
|
49
|
+
// Newer Claude Code versions feed the eval from /dev/null (`eval '...' < /dev/null`).
|
|
50
|
+
const CLAUDE_TOOL_CALL = /^source \S*\/\.claude\/shell-snapshots\/\S+[\s\S]*? && eval '([\s\S]*)'(?: < \/dev\/null)? && pwd -P >\| \S+$/;
|
|
51
|
+
/** RayTrace's PreToolUse hook puts the call's id first, so the kernel's record names it. */
|
|
52
|
+
const CALL_MARKER = /^export RAYTRACE_CALL_ID=([A-Za-z0-9_-]{1,128})\s*\n?/;
|
|
53
|
+
|
|
54
|
+
/** Codex wraps every tool call as `/bin/bash -c <cmd>` (or -lc). Stripping the
|
|
55
|
+
* wrapper leaves the command the agent actually wrote -- the same normalisation
|
|
56
|
+
* execution-correlation.mjs does before matching. */
|
|
57
|
+
const SHELL_WRAPPER = /^(?:\S*\/)?(?:sh|bash|zsh|dash)\s+-[a-z]*c\s+/;
|
|
58
|
+
|
|
59
|
+
/** Quoting differs between a shell's argv and its child's, so compare on the
|
|
60
|
+
* text alone: `node -e "x"` and `node -e x` are the same work. */
|
|
61
|
+
function normalizeForCompare(command) {
|
|
62
|
+
return String(command).replace(/["']/g, '').replace(/\s+/g, ' ').trim();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function commandText(argv) {
|
|
66
|
+
return (Array.isArray(argv) ? argv : []).join(' ').trim();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function isPreamble(argv) { return CODEX_PREAMBLE.test(commandText(argv)); }
|
|
70
|
+
|
|
71
|
+
export function isMachinery(argv) {
|
|
72
|
+
const text = commandText(argv);
|
|
73
|
+
return MACHINERY.some((pattern) => pattern.test(text));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Started by Claude Code or RayTrace's hook, not by a tool call. */
|
|
77
|
+
export function isHarness(argv) {
|
|
78
|
+
const text = commandText(argv);
|
|
79
|
+
return HARNESS.some((pattern) => pattern.test(text));
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** A Claude Code Bash tool call, out of its wrapper: the command as the agent
|
|
83
|
+
* wrote it and the call id RayTrace's hook marked it with (null without the
|
|
84
|
+
* hook). Null when the text is not that wrapper. */
|
|
85
|
+
export function claudeToolCall(text) {
|
|
86
|
+
const wrapped = CLAUDE_TOOL_CALL.exec(String(text ?? ''));
|
|
87
|
+
if (!wrapped) return null;
|
|
88
|
+
// eval's argument is single-quoted, each quote inside written as '"'"'.
|
|
89
|
+
const inner = wrapped[1].replaceAll(`'"'"'`, "'");
|
|
90
|
+
const marker = CALL_MARKER.exec(inner);
|
|
91
|
+
return { command: (marker ? inner.slice(marker[0].length) : inner).trim(), call_id: marker ? marker[1] : null };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The command without Codex's or Claude Code's shell wrapper, for display and for the rules. */
|
|
95
|
+
export function bareCommand(argv) {
|
|
96
|
+
const text = commandText(argv).replace(SHELL_WRAPPER, '').trim();
|
|
97
|
+
return claudeToolCall(text)?.command ?? text;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Folds a page of collector events into one entry per command.
|
|
102
|
+
*
|
|
103
|
+
* Same-pid re-execs collapse onto the FIRST non-preamble exec in that pid:
|
|
104
|
+
* `/bin/bash -c cd … && npm test` is what the agent asked for, and
|
|
105
|
+
* `npm test` -> `node …/npm test` are that command implementing itself. Keeping
|
|
106
|
+
* the first is what makes the line match the tool call above it.
|
|
107
|
+
*
|
|
108
|
+
* @param {Array<object>} events - raw viewer events (any order)
|
|
109
|
+
* @returns {{commands: Array<object>, folded: {preamble: number, machinery: number, reexec: number, exits: number, preambleChars: number}}}
|
|
110
|
+
*/
|
|
111
|
+
export function fold(events) {
|
|
112
|
+
const folded = { preamble: 0, machinery: 0, reexec: 0, exits: 0, preambleChars: 0 };
|
|
113
|
+
const ordered = (Array.isArray(events) ? events : [])
|
|
114
|
+
.filter((event) => Number.isFinite(Number(event?.event_id)))
|
|
115
|
+
.sort((a, b) => Number(a.event_id) - Number(b.event_id));
|
|
116
|
+
|
|
117
|
+
const byPid = new Map(); // pid -> the command entry that owns it
|
|
118
|
+
// pid -> ppid for EVERY process seen, whether or not it became a command.
|
|
119
|
+
// Two kinds of process are missing from `byPid` but still sit between a
|
|
120
|
+
// command and its real parent: ones we folded (machinery, preamble), and
|
|
121
|
+
// ones that forked without ever exec'ing -- a shell running `a | b | c`
|
|
122
|
+
// forks a subshell per stage, and a subshell inherits its parent's image, so
|
|
123
|
+
// gVisor has no exec to report for it. Without their ppid the stages below
|
|
124
|
+
// them look parentless and float to the top level as though nothing had
|
|
125
|
+
// started them. `process_exit` names the ppid even when no exec did.
|
|
126
|
+
const lineage = new Map();
|
|
127
|
+
const remember = (pid, ppid) => { if (!lineage.has(pid) && ppid != null) lineage.set(pid, ppid); };
|
|
128
|
+
const commands = [];
|
|
129
|
+
// What the harness started, and everything started under that: a hook's
|
|
130
|
+
// git commands are the hook's work, not the agent's. (A probe like getconf
|
|
131
|
+
// folds alone: what ran under it was the agent's pipeline.)
|
|
132
|
+
const harness = new Set();
|
|
133
|
+
const underHarness = (pid) => {
|
|
134
|
+
const seen = new Set();
|
|
135
|
+
for (let at = pid; at != null && !seen.has(at); at = lineage.get(at) ?? null) { if (harness.has(at)) return true; seen.add(at); }
|
|
136
|
+
return false;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
for (const event of ordered) {
|
|
140
|
+
const pid = Number(event.pid);
|
|
141
|
+
const eventPpid = Number.isFinite(Number(event.ppid)) ? Number(event.ppid) : null;
|
|
142
|
+
remember(pid, eventPpid);
|
|
143
|
+
if (event.kind === 'process_exit') {
|
|
144
|
+
folded.exits += 1;
|
|
145
|
+
const owner = byPid.get(pid);
|
|
146
|
+
if (owner) {
|
|
147
|
+
owner.exit_code = typeof event.exit_code === 'number' ? event.exit_code : null;
|
|
148
|
+
owner.signal = typeof event.signal === 'number' && event.signal > 0 ? event.signal : null;
|
|
149
|
+
owner.ended_ns = event.timestamp_ns ?? null;
|
|
150
|
+
}
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (event.kind !== 'exec_succeeded') continue;
|
|
154
|
+
|
|
155
|
+
const argv = Array.isArray(event.argv) ? event.argv : [];
|
|
156
|
+
if (isPreamble(argv)) { folded.preamble += 1; folded.preambleChars += commandText(argv).length; continue; }
|
|
157
|
+
if (underHarness(pid) || isHarness(argv)) { harness.add(pid); folded.machinery += 1; continue; }
|
|
158
|
+
if (isMachinery(argv)) { folded.machinery += 1; continue; }
|
|
159
|
+
|
|
160
|
+
// A pid that already has a command is re-execing: same job, new image.
|
|
161
|
+
if (byPid.has(pid)) { folded.reexec += 1; continue; }
|
|
162
|
+
|
|
163
|
+
const entry = {
|
|
164
|
+
event_id: Number(event.event_id),
|
|
165
|
+
pid,
|
|
166
|
+
ppid: eventPpid,
|
|
167
|
+
argv,
|
|
168
|
+
command: bareCommand(argv),
|
|
169
|
+
// Set for a Claude Code tool call that RayTrace's hook marked: the call
|
|
170
|
+
// this process is, by its own command line (attributeMarked).
|
|
171
|
+
marker_call_id: claudeToolCall(commandText(argv).replace(SHELL_WRAPPER, '').trim())?.call_id ?? null,
|
|
172
|
+
started_ns: event.timestamp_ns ?? null,
|
|
173
|
+
// The process's own start time: with pid, the identity the dashboard
|
|
174
|
+
// uses to find which tool call this process belongs to. Stable across
|
|
175
|
+
// the re-execs this entry absorbs, unlike the event id kept above.
|
|
176
|
+
process_start_ns: typeof event.process_start_ns === 'string' ? event.process_start_ns : null,
|
|
177
|
+
ended_ns: null,
|
|
178
|
+
exit_code: null,
|
|
179
|
+
signal: null,
|
|
180
|
+
// Set by the caller once parents are known; a child of a command the
|
|
181
|
+
// agent ran is shown nested under it rather than as its own step.
|
|
182
|
+
parent_event_id: null,
|
|
183
|
+
};
|
|
184
|
+
byPid.set(pid, entry);
|
|
185
|
+
commands.push(entry);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Nest: a command whose ppid belongs to another command in this window is a
|
|
189
|
+
// consequence of it, not a separate thing the agent did. Walk the lineage
|
|
190
|
+
// upward rather than taking a single hop, so a folded parent hands its
|
|
191
|
+
// children to the nearest ancestor that survived. A process whose parent ran
|
|
192
|
+
// outside this window is genuinely unattributed and stays at the top level.
|
|
193
|
+
for (const entry of commands) {
|
|
194
|
+
let ancestor = entry.ppid;
|
|
195
|
+
const seen = new Set([entry.pid]);
|
|
196
|
+
while (ancestor != null && !seen.has(ancestor)) {
|
|
197
|
+
const parent = byPid.get(ancestor);
|
|
198
|
+
if (parent && parent !== entry) { entry.parent_event_id = parent.event_id; break; }
|
|
199
|
+
seen.add(ancestor);
|
|
200
|
+
ancestor = lineage.has(ancestor) ? lineage.get(ancestor) : null;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// `sh -c "node -e X"` forks a `node -e X`. Two processes, one action -- the
|
|
205
|
+
// shell is doing precisely what it was handed, so showing both reads as the
|
|
206
|
+
// agent having deleted the same file twice. Collapse the child onto the
|
|
207
|
+
// parent when their commands are the same once quoting is ignored, keeping
|
|
208
|
+
// the child's outcome (it is the process that actually did the work).
|
|
209
|
+
const sameWork = (a, b) => a && b && normalizeForCompare(a) === normalizeForCompare(b);
|
|
210
|
+
const byEventId = new Map(commands.map((entry) => [entry.event_id, entry]));
|
|
211
|
+
const collapsed = commands.filter((entry) => {
|
|
212
|
+
const parent = entry.parent_event_id != null ? byEventId.get(entry.parent_event_id) : null;
|
|
213
|
+
if (!parent || !sameWork(entry.command, parent.command)) return true;
|
|
214
|
+
parent.exit_code = entry.exit_code ?? parent.exit_code;
|
|
215
|
+
parent.signal = entry.signal ?? parent.signal;
|
|
216
|
+
parent.ended_ns = entry.ended_ns ?? parent.ended_ns;
|
|
217
|
+
byEventId.set(entry.event_id, parent); // re-point any grandchildren
|
|
218
|
+
folded.reexec += 1;
|
|
219
|
+
return false;
|
|
220
|
+
});
|
|
221
|
+
for (const entry of collapsed) {
|
|
222
|
+
const parent = entry.parent_event_id != null ? byEventId.get(entry.parent_event_id) : null;
|
|
223
|
+
entry.parent_event_id = parent && parent !== entry ? parent.event_id : null;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return { commands: collapsed, folded };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// --- plain-English rules -----------------------------------------------------
|
|
230
|
+
//
|
|
231
|
+
// Deterministic, in the spirit of describeCall() in experiment-engine.mjs.
|
|
232
|
+
// A rule may only say what the command text already proves. Anything else
|
|
233
|
+
// falls through to `null`, and the caller either shows the command verbatim or
|
|
234
|
+
// asks a model -- clearly marked as a model's reading, never as observation.
|
|
235
|
+
|
|
236
|
+
const clip = (text, max) => (text.length > max ? text.slice(0, max - 1) + '…' : text);
|
|
237
|
+
/** Last two path segments: enough to recognise, short enough to read. */
|
|
238
|
+
const shortPath = (value) => {
|
|
239
|
+
const parts = String(value).replace(/\/+$/, '').split('/').filter(Boolean);
|
|
240
|
+
return parts.length <= 2 ? String(value) : '…/' + parts.slice(-2).join('/');
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
/** Shell words with their quotes taken off: `rg 'a b' src/` -> [rg, a b, src/]. */
|
|
244
|
+
function shellWords(text) {
|
|
245
|
+
const words = [];
|
|
246
|
+
let word = ''; let quote = null; let started = false;
|
|
247
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
248
|
+
const char = text[i];
|
|
249
|
+
if (quote) { if (char === quote) quote = null; else word += char; continue; }
|
|
250
|
+
if (char === '"' || char === "'") { quote = char; started = true; continue; }
|
|
251
|
+
if (char === '\\' && i + 1 < text.length) { word += text[i + 1]; i += 1; started = true; continue; }
|
|
252
|
+
if (/\s/.test(char)) { if (started || word) { words.push(word); word = ''; started = false; } continue; }
|
|
253
|
+
word += char; started = true;
|
|
254
|
+
}
|
|
255
|
+
if (started || word) words.push(word);
|
|
256
|
+
return words;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Split on separators that sit outside quotes (`;`, `&&`, `||`, newlines,
|
|
260
|
+
* or one lone `|`), so a `;` inside an inline script stays in its clause. */
|
|
261
|
+
function splitShell(text, lonePipe = false) {
|
|
262
|
+
const parts = [];
|
|
263
|
+
let part = ''; let quote = null;
|
|
264
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
265
|
+
const char = text[i];
|
|
266
|
+
if (quote) { part += char; if (char === quote) quote = null; continue; }
|
|
267
|
+
if (char === '"' || char === "'") { quote = char; part += char; continue; }
|
|
268
|
+
if (char === '\\' && i + 1 < text.length) { part += char + text[i + 1]; i += 1; continue; }
|
|
269
|
+
const pair = text.slice(i, i + 2);
|
|
270
|
+
if (!lonePipe && (pair === '&&' || pair === '||')) { parts.push(part); part = ''; i += 1; continue; }
|
|
271
|
+
if (!lonePipe && (char === ';' || char === '\n')) { parts.push(part); part = ''; continue; }
|
|
272
|
+
if (lonePipe && char === '|' && pair !== '||') { parts.push(part); part = ''; continue; }
|
|
273
|
+
part += char;
|
|
274
|
+
}
|
|
275
|
+
parts.push(part);
|
|
276
|
+
return parts.map((piece) => piece.trim()).filter(Boolean);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** `grep`/`rg` in words: the actual text searched for, and where. The bare
|
|
280
|
+
* phrase "a pattern" is never an answer -- it tells the reader nothing. */
|
|
281
|
+
function describeSearch(words) {
|
|
282
|
+
const args = words.slice(1);
|
|
283
|
+
if (args.includes('--files')) {
|
|
284
|
+
const where = args.find((arg) => !arg.startsWith('-'));
|
|
285
|
+
return where ? `listed the files in ${shortPath(where)}` : 'listed the files';
|
|
286
|
+
}
|
|
287
|
+
// Options whose next word is a value, not the pattern or a place to search.
|
|
288
|
+
const TAKES_VALUE = new Set(['-A', '-B', '-C', '-m', '-f', '-g', '--glob', '--include', '--exclude',
|
|
289
|
+
'-t', '--type', '--type-not', '--max-count', '--after-context', '--before-context', '--context']);
|
|
290
|
+
let pattern = null;
|
|
291
|
+
const places = [];
|
|
292
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
293
|
+
const arg = args[i];
|
|
294
|
+
if (arg === '-e' || arg === '--regexp') { pattern = args[i + 1] ?? pattern; i += 1; continue; }
|
|
295
|
+
if (arg.startsWith('--regexp=')) { pattern = arg.slice('--regexp='.length); continue; }
|
|
296
|
+
if (TAKES_VALUE.has(arg)) { i += 1; continue; }
|
|
297
|
+
if (arg.startsWith('-')) continue;
|
|
298
|
+
if (pattern == null) pattern = arg; else places.push(arg);
|
|
299
|
+
}
|
|
300
|
+
if (!pattern) return 'searched the contents of files';
|
|
301
|
+
const onlyNames = args.some((arg) => arg === '-l' || arg === '--files-with-matches' || /^-[a-z]*l/.test(arg));
|
|
302
|
+
const where = places.length ? ` in ${places.map(shortPath).slice(0, 2).join(', ')}` : '';
|
|
303
|
+
return `${onlyNames ? 'found which files mention' : 'searched for'} "${clip(pattern, 40)}"${where}`;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function describeFind(words) {
|
|
307
|
+
const args = words.slice(1);
|
|
308
|
+
const root = args.find((arg) => !arg.startsWith('-'));
|
|
309
|
+
const nameAt = args.findIndex((arg) => arg === '-name' || arg === '-iname');
|
|
310
|
+
const under = root ? ` under ${shortPath(root)}` : '';
|
|
311
|
+
return nameAt >= 0 && args[nameAt + 1]
|
|
312
|
+
? `looked for files named ${clip(args[nameAt + 1], 40)}${under}`
|
|
313
|
+
: `looked for files${under}`;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const GIT_PLAIN = {
|
|
317
|
+
status: 'checked which files have unsaved changes (git status)',
|
|
318
|
+
diff: 'looked at what changed in the files (git diff)',
|
|
319
|
+
log: 'looked at the commit history (git log)',
|
|
320
|
+
add: 'marked files to be saved in the next commit (git add)',
|
|
321
|
+
commit: 'saved the changes as a commit (git commit)',
|
|
322
|
+
checkout: 'switched branch (git checkout)',
|
|
323
|
+
switch: 'switched branch (git switch)',
|
|
324
|
+
branch: 'listed or changed branches (git branch)',
|
|
325
|
+
clone: 'downloaded a copy of a project (git clone)',
|
|
326
|
+
pull: 'downloaded the latest changes (git pull)',
|
|
327
|
+
fetch: 'checked for newer changes (git fetch)',
|
|
328
|
+
push: 'uploaded its commits (git push)',
|
|
329
|
+
show: 'looked at a past commit (git show)',
|
|
330
|
+
config: 'checked a git setting (git config)',
|
|
331
|
+
'rev-parse': 'checked where git is pointing (git rev-parse)',
|
|
332
|
+
init: 'started tracking this folder with git (git init)',
|
|
333
|
+
stash: 'set the changes aside for later (git stash)',
|
|
334
|
+
merge: 'combined another branch in (git merge)',
|
|
335
|
+
rebase: 'replayed its commits onto another branch (git rebase)',
|
|
336
|
+
reset: 'moved the project back to an earlier state (git reset)',
|
|
337
|
+
rm: 'removed files from git (git rm)',
|
|
338
|
+
mv: 'renamed a file in git (git mv)',
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
function firstUrl(command) { return command.match(/https?:\/\/\S+/)?.[0] ?? null; }
|
|
342
|
+
|
|
343
|
+
/** `node -e "<script>"` is common enough to be worth reading into, but only
|
|
344
|
+
* for operations the script text states outright. */
|
|
345
|
+
function describeInlineNode(command) {
|
|
346
|
+
// The script usually arrives still wrapped in the quotes the shell saw
|
|
347
|
+
// (`node -e "console.log('x')"`); strip them so anchored patterns match.
|
|
348
|
+
const script = command.replace(/^node\s+-e\s*/, '').replace(/^(["'])([\s\S]*)\1$/, '$2').trim();
|
|
349
|
+
const unlink = script.match(/unlinkSync\(\s*["']([^"']+)["']/);
|
|
350
|
+
if (unlink) return `deleted ${shortPath(unlink[1])}`;
|
|
351
|
+
const write = script.match(/writeFileSync\(\s*["']([^"']+)["']/);
|
|
352
|
+
if (write) return `wrote ${shortPath(write[1])}`;
|
|
353
|
+
const mkdir = script.match(/mkdirSync\(\s*["']([^"']+)["']/);
|
|
354
|
+
if (mkdir) return `created folder ${shortPath(mkdir[1])}`;
|
|
355
|
+
const log = script.match(/^\s*console\.log\(\s*["']([^"']*)["']\s*\)\s*$/);
|
|
356
|
+
if (log) return `printed "${clip(log[1], 40)}"`;
|
|
357
|
+
return null; // an inline script doing something else: show it verbatim
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** A command line that carries its tool call's id is that call, whatever the
|
|
361
|
+
* store has (or has not yet) recorded for the process: the wrapper is what
|
|
362
|
+
* the agent's call became, and its children are the call's steps. Sets
|
|
363
|
+
* `attribution` on such commands; call it after the store's own answers. */
|
|
364
|
+
export function attributeMarked(commands) {
|
|
365
|
+
for (const entry of Array.isArray(commands) ? commands : []) {
|
|
366
|
+
if (entry.marker_call_id && !entry.attribution?.call_id) entry.attribution = { call_id: entry.marker_call_id, parent_call_id: null, tier: 'mediated' };
|
|
367
|
+
}
|
|
368
|
+
return commands;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* One clause (`pwd`, `ls -la src/`) in plain English. Returns '' for shell
|
|
373
|
+
* set-up that does no work of its own (`set -e`), and null when the clause
|
|
374
|
+
* cannot be proved from its text. Plain words come first; the command's own
|
|
375
|
+
* name rides along in brackets where a reader may not know it (`pwd`).
|
|
376
|
+
*/
|
|
377
|
+
function describeClause(clause) {
|
|
378
|
+
const written = clause.match(/^(?:cat|echo|printf)\b[^>]*>{1,2}\s*(\S+)/);
|
|
379
|
+
if (written) return `wrote ${shortPath(written[1])}`;
|
|
380
|
+
const clean = clause.replace(/\s*\d*>&\d+/g, '').replace(/\s*\d*>>?\s*\S+/g, '').replace(/\s*<\s*\S+/g, '').trim();
|
|
381
|
+
const words = shellWords(clean);
|
|
382
|
+
const settings = [];
|
|
383
|
+
while (words.length && /^[A-Za-z_]\w*=/.test(words[0])) settings.push(words.shift().split('=')[0]);
|
|
384
|
+
if (!words.length) return settings.length ? `set the ${settings.join(', ')} environment setting${settings.length > 1 ? 's' : ''}` : null;
|
|
385
|
+
if (words[0] === 'sudo') words.shift();
|
|
386
|
+
if (!words.length) return null;
|
|
387
|
+
const prog = words[0].split('/').filter(Boolean).at(-1) ?? words[0];
|
|
388
|
+
const args = words.slice(1);
|
|
389
|
+
const plain = args.filter((arg) => !arg.startsWith('-'));
|
|
390
|
+
const last = plain.at(-1) ?? '';
|
|
391
|
+
const first = plain[0] ?? '';
|
|
392
|
+
const hasFlag = (letter) => args.some((arg) => arg === `--${letter}` || new RegExp(`^-[a-z]*${letter}`).test(arg));
|
|
393
|
+
switch (prog) {
|
|
394
|
+
case 'cd': return first ? `changed folder to ${shortPath(first)}` : 'changed folder to the home folder';
|
|
395
|
+
case 'pwd': return 'checked which folder it was in (pwd)';
|
|
396
|
+
case 'whoami': return 'checked which user it was running as (whoami)';
|
|
397
|
+
case 'id': return 'checked which user and groups it was running as (id)';
|
|
398
|
+
case 'hostname': return "checked the machine's name (hostname)";
|
|
399
|
+
case 'date': return 'checked the current date and time (date)';
|
|
400
|
+
case 'uname': {
|
|
401
|
+
const flags = args.join('');
|
|
402
|
+
if (/^-m+$/.test(args[0] ?? '')) return 'checked what kind of machine this is (uname -m)';
|
|
403
|
+
if (/^-r+$/.test(args[0] ?? '')) return 'checked the operating system version (uname -r)';
|
|
404
|
+
if (/^-s+$/.test(args[0] ?? '')) return 'checked the operating system name (uname -s)';
|
|
405
|
+
return flags.includes('a') ? 'checked all the system details (uname -a)' : 'checked the operating system (uname)';
|
|
406
|
+
}
|
|
407
|
+
case 'ls': {
|
|
408
|
+
const hidden = hasFlag('all') || hasFlag('a') || hasFlag('A');
|
|
409
|
+
const where = first ? ` in ${shortPath(first)}` : ' here';
|
|
410
|
+
return `listed the files${where}${hidden ? ', including hidden ones' : ''}`;
|
|
411
|
+
}
|
|
412
|
+
case 'mkdir': return first ? `created folder ${shortPath(last)}` : null;
|
|
413
|
+
case 'rmdir': return first ? `removed folder ${shortPath(last)}` : null;
|
|
414
|
+
case 'rm': return first ? `deleted ${shortPath(first)}${plain.length > 1 ? ` and ${plain.length - 1} more` : ''}` : null;
|
|
415
|
+
case 'touch': return first ? `created empty file ${shortPath(last)}` : null;
|
|
416
|
+
case 'cp': return plain.length >= 2 ? `copied ${shortPath(first)} to ${shortPath(last)}` : null;
|
|
417
|
+
case 'mv': return plain.length >= 2 ? `moved ${shortPath(first)} to ${shortPath(last)}` : null;
|
|
418
|
+
case 'chmod': return last ? `changed permissions on ${shortPath(last)}` : null;
|
|
419
|
+
case 'cat': return first ? `read ${plain.map(shortPath).slice(0, 3).join(', ')}` : null;
|
|
420
|
+
case 'head': case 'tail': {
|
|
421
|
+
if (!last) return null;
|
|
422
|
+
const count = args.find((arg) => /^-\d+$/.test(arg))?.slice(1) ?? args[args.indexOf('-n') + 1];
|
|
423
|
+
return `read the ${prog === 'head' ? 'first' : 'last'}${count && /^\d+$/.test(count) ? ` ${count}` : ''} lines of ${shortPath(last)}`;
|
|
424
|
+
}
|
|
425
|
+
case 'grep': case 'rg': case 'egrep': case 'fgrep': return describeSearch(words);
|
|
426
|
+
case 'find': return describeFind(words);
|
|
427
|
+
case 'git': {
|
|
428
|
+
const rest = [...args];
|
|
429
|
+
while (rest.length && rest[0].startsWith('-')) rest.splice(0, rest[0] === '-C' || rest[0] === '-c' ? 2 : 1);
|
|
430
|
+
const sub = rest[0];
|
|
431
|
+
return sub ? (GIT_PLAIN[sub] ?? `ran git ${sub}`) : null;
|
|
432
|
+
}
|
|
433
|
+
case 'npm': case 'yarn': case 'pnpm': {
|
|
434
|
+
const script = args[0] === 'run' ? args[1] : args[0];
|
|
435
|
+
if (!script) return null;
|
|
436
|
+
if (script === 'install' || script === 'i' || script === 'ci' || script === 'add') return 'installed dependencies';
|
|
437
|
+
if (script === 'test') return 'ran the test script';
|
|
438
|
+
if (prog === 'npm' && (script === 'exec' || script === 'x')) return args[1] ? `ran ${args[1]} (via npm exec)` : null;
|
|
439
|
+
return `ran the ${script} script`;
|
|
440
|
+
}
|
|
441
|
+
case 'npx': return first ? `ran ${first} (via npx)` : null;
|
|
442
|
+
case 'node': {
|
|
443
|
+
if (args.includes('--test')) return 'ran the tests';
|
|
444
|
+
if (args.some((arg) => arg === '-e' || arg === '--eval')) return describeInlineNode(clean) ?? 'ran an inline JavaScript script';
|
|
445
|
+
const file = args.find((arg) => /\.(?:js|mjs|cjs|ts)$/.test(arg));
|
|
446
|
+
return file ? `ran ${shortPath(file)}` : null;
|
|
447
|
+
}
|
|
448
|
+
case 'python': case 'python3': {
|
|
449
|
+
if (args.includes('-c') || args[0] === '-') return 'ran an inline Python script';
|
|
450
|
+
const moduleAt = args.indexOf('-m');
|
|
451
|
+
if (moduleAt >= 0) {
|
|
452
|
+
const mod = args[moduleAt + 1] ?? '';
|
|
453
|
+
return mod === 'pytest' || mod === 'unittest' ? 'ran the Python tests' : mod ? `ran the Python module ${mod}` : null;
|
|
454
|
+
}
|
|
455
|
+
const file = args.find((arg) => arg.endsWith('.py'));
|
|
456
|
+
return file ? `ran ${shortPath(file)}` : null;
|
|
457
|
+
}
|
|
458
|
+
case 'pytest': return 'ran the Python tests';
|
|
459
|
+
case 'jest': case 'vitest': case 'mocha': return 'ran the JavaScript tests';
|
|
460
|
+
case 'go': case 'cargo': return args[0] === 'test' ? 'ran the tests' : args[0] ? `ran ${prog} ${args[0]}` : null;
|
|
461
|
+
case 'curl': case 'wget': return `fetched ${clip(firstUrl(clean) || 'a URL', 50)}`;
|
|
462
|
+
case 'which': return first ? `checked whether ${plain.join(', ')} is installed (which)` : null;
|
|
463
|
+
case 'command': return args[0] === '-v' && args[1] ? `checked whether ${args.slice(1).join(', ')} is installed` : null;
|
|
464
|
+
case 'env': case 'printenv': return prog === 'printenv' || !args.length ? 'listed its environment settings' : describeClause(args.join(' '));
|
|
465
|
+
case 'export': {
|
|
466
|
+
const names = args.map((arg) => arg.split('=')[0]).filter((name) => /^[A-Za-z_]\w*$/.test(name));
|
|
467
|
+
return names.length ? `set the ${names.join(', ')} environment setting${names.length > 1 ? 's' : ''}` : null;
|
|
468
|
+
}
|
|
469
|
+
case 'source': case '.': return first ? `loaded shell settings from ${shortPath(first)}` : null;
|
|
470
|
+
case 'echo': case 'printf': {
|
|
471
|
+
const said = args.filter((arg) => arg !== '-n' && arg !== '-e').join(' ');
|
|
472
|
+
return said ? `printed "${clip(said, 40)}"` : 'printed a blank line';
|
|
473
|
+
}
|
|
474
|
+
case 'wc': return last ? `counted the ${hasFlag('w') ? 'words' : hasFlag('c') ? 'bytes' : 'lines'} in ${shortPath(last)}` : null;
|
|
475
|
+
case 'sed': return hasFlag('i') && plain.length >= 2 ? `edited ${shortPath(last)} with a find-and-replace` : null;
|
|
476
|
+
case 'test': case '[': {
|
|
477
|
+
const checked = args.filter((arg) => arg !== ']');
|
|
478
|
+
return (checked[0] === '-f' || checked[0] === '-d' || checked[0] === '-e') && checked[1]
|
|
479
|
+
? `checked whether ${shortPath(checked[1])} exists` : null;
|
|
480
|
+
}
|
|
481
|
+
case 'sleep': return first ? `waited ${first}` : null;
|
|
482
|
+
case 'set': case 'shopt': case 'true': return '';
|
|
483
|
+
case 'sh': case 'bash': case 'zsh': case 'dash': {
|
|
484
|
+
const dashC = args.findIndex((arg) => /^-[a-z]*c$/.test(arg));
|
|
485
|
+
if (dashC >= 0) return args[dashC + 1] ? describeCommand(args[dashC + 1]) : null;
|
|
486
|
+
const file = args.find((arg) => arg.endsWith('.sh'));
|
|
487
|
+
return file ? `ran the shell script ${shortPath(file)}` : null;
|
|
488
|
+
}
|
|
489
|
+
default: return null;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/** `prog - <<'E' … E` plus whatever follows the closing marker. */
|
|
494
|
+
function describeHeredoc(text) {
|
|
495
|
+
const opened = text.match(/<<-?\s*['"]?([A-Za-z_][\w]*)['"]?/);
|
|
496
|
+
if (!opened) return null;
|
|
497
|
+
const language = { python: 'Python', python3: 'Python', node: 'JavaScript', ruby: 'Ruby', perl: 'Perl', bash: 'shell', sh: 'shell' };
|
|
498
|
+
// The opener may not be the first clause: `cd /workspace; python3 - <<'E'`.
|
|
499
|
+
const clauses = splitShell(text.slice(0, text.indexOf('<<')));
|
|
500
|
+
const opener = clauses.pop() ?? '';
|
|
501
|
+
const prog = shellWords(opener)[0]?.split('/').at(-1) ?? '';
|
|
502
|
+
const before = clauses.length ? describeCommand(clauses.join(' && ')) : null;
|
|
503
|
+
const inline = `ran an inline ${language[prog] ?? prog ?? ''} script`.replace(/\s+/g, ' ').trim();
|
|
504
|
+
const head = before ? `${before}, then ${inline}` : inline;
|
|
505
|
+
const lines = text.split('\n');
|
|
506
|
+
const closedAt = lines.findIndex((line, index) => index > 0 && line.trim() === opened[1]);
|
|
507
|
+
const after = closedAt >= 0 ? lines.slice(closedAt + 1).join('\n').trim() : '';
|
|
508
|
+
const rest = after ? describeCommand(after) : null;
|
|
509
|
+
return rest ? `${head}, then ${rest}` : head;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* A sentence for one command, or null when no rule applies with certainty.
|
|
514
|
+
* Null is a real answer: it means "show the command itself", which is always
|
|
515
|
+
* honest, rather than inventing a description that might be wrong.
|
|
516
|
+
*
|
|
517
|
+
* A compound command is told clause by clause (`changed folder to X, then
|
|
518
|
+
* listed the files here`): every clause is part of what ran, the `cd`
|
|
519
|
+
* included. If any clause cannot be proved, the whole sentence is withheld.
|
|
520
|
+
*/
|
|
521
|
+
export function describeCommand(command) {
|
|
522
|
+
// Strip the shell wrapper, then any quotes it left behind: `sh -c "npm test"`
|
|
523
|
+
// becomes `npm test`, so the rules see the command rather than the quoting.
|
|
524
|
+
const text = String(command ?? '')
|
|
525
|
+
.replace(SHELL_WRAPPER, '')
|
|
526
|
+
.replace(/^(["'])([\s\S]*)\1$/, '$2')
|
|
527
|
+
.trim();
|
|
528
|
+
if (!text) return null;
|
|
529
|
+
// `for c in npm yarn pnpm; do command -v "$c" …; done`: one check, many names.
|
|
530
|
+
const loop = text.match(/^for\s+\w+\s+in\s+([^;]+?)\s*;\s*do\s+command\s+-v\b/);
|
|
531
|
+
if (loop) return `checked which of ${loop[1].split(/\s+/).filter(Boolean).join(', ')} are installed`;
|
|
532
|
+
if (/<<-?\s*['"]?[A-Za-z_]/.test(text)) { const heredoc = describeHeredoc(text); if (heredoc) return heredoc; }
|
|
533
|
+
const described = [];
|
|
534
|
+
for (const clause of splitShell(text)) {
|
|
535
|
+
// A pipe's later stages only shape the output (`| head -3`); the work
|
|
536
|
+
// itself is the first stage.
|
|
537
|
+
const saying = describeClause(splitShell(clause, true)[0] ?? clause);
|
|
538
|
+
if (saying == null) return null;
|
|
539
|
+
if (saying && described.at(-1) !== saying) described.push(saying);
|
|
540
|
+
}
|
|
541
|
+
return described.length ? described.join(', then ') : null;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/** Every command in `commands` that no rule could describe, deduplicated --
|
|
545
|
+
* the batch a caller sends to a model in ONE request rather than per line. */
|
|
546
|
+
export function undescribed(commands) {
|
|
547
|
+
const seen = new Set();
|
|
548
|
+
for (const entry of Array.isArray(commands) ? commands : []) {
|
|
549
|
+
if (entry.description) continue;
|
|
550
|
+
if (describeCommand(entry.command)) continue;
|
|
551
|
+
if (entry.command) seen.add(entry.command);
|
|
552
|
+
}
|
|
553
|
+
return [...seen];
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* Attaches a `description` and `description_source` to each command.
|
|
558
|
+
* `lookup` maps a command string to a model-written sentence (cache or fresh);
|
|
559
|
+
* anything it does not cover keeps `source: 'raw'` and is rendered verbatim.
|
|
560
|
+
*/
|
|
561
|
+
export function describeAll(commands, lookup = () => null) {
|
|
562
|
+
for (const entry of Array.isArray(commands) ? commands : []) {
|
|
563
|
+
const rule = describeCommand(entry.command);
|
|
564
|
+
if (rule) { entry.description = rule; entry.description_source = 'rule'; continue; }
|
|
565
|
+
const model = lookup(entry.command);
|
|
566
|
+
if (model) { entry.description = model; entry.description_source = 'model'; continue; }
|
|
567
|
+
entry.description = null; entry.description_source = 'raw';
|
|
568
|
+
}
|
|
569
|
+
return commands;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
// --- model fallback for commands no rule can describe ------------------------
|
|
573
|
+
//
|
|
574
|
+
// Batched: every unknown command in a turn goes in ONE request, deduplicated,
|
|
575
|
+
// and the result is cached by command text (migration 006). A per-line call
|
|
576
|
+
// would be both slow and a real cost on every trace view.
|
|
577
|
+
|
|
578
|
+
const MAX_BATCH = 25;
|
|
579
|
+
const MAX_COMMAND_CHARS = 600;
|
|
580
|
+
|
|
581
|
+
export const DESCRIBE_INSTRUCTIONS = [
|
|
582
|
+
'You are labelling shell commands that were observed running inside a sandbox.',
|
|
583
|
+
'For each numbered command, reply with the same number and one short phrase saying what the command does.',
|
|
584
|
+
'Use plain past tense, under 14 words, no trailing period. Example: 3. compiled the native addon',
|
|
585
|
+
'Name the actual folder, file, or text searched for when the command shows it; never say just "a pattern" or "a file".',
|
|
586
|
+
'Describe only what the command text itself shows. If you cannot tell, reply exactly: unclear.',
|
|
587
|
+
'Never guess at intent, outcome, or whether it succeeded.',
|
|
588
|
+
].join(' ');
|
|
589
|
+
|
|
590
|
+
/** Request for a batch of unknown commands. Shape mirrors summaryRequest() in
|
|
591
|
+
* summaries.mjs so both go through the same provider path. */
|
|
592
|
+
export function describeRequest(commands, model) {
|
|
593
|
+
const batch = [...new Set(commands)].slice(0, MAX_BATCH)
|
|
594
|
+
.map((command) => String(command).slice(0, MAX_COMMAND_CHARS));
|
|
595
|
+
return {
|
|
596
|
+
commands: batch,
|
|
597
|
+
payload: {
|
|
598
|
+
model, store: false, stream: false,
|
|
599
|
+
max_output_tokens: 40 * batch.length + 120,
|
|
600
|
+
// A labelling task, not a reasoning one -- and reasoning tokens compete
|
|
601
|
+
// with the output budget, which is what broke summaries at v3.
|
|
602
|
+
reasoning: { effort: 'low' },
|
|
603
|
+
instructions: DESCRIBE_INSTRUCTIONS,
|
|
604
|
+
input: batch.map((command, index) => `${index + 1}. ${command}`).join('\n'),
|
|
605
|
+
},
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Parses the numbered reply back into command -> sentence.
|
|
611
|
+
* A line the model marked `unclear`, or did not answer at all, is left out
|
|
612
|
+
* entirely: the caller then shows the raw command, which is always honest.
|
|
613
|
+
*/
|
|
614
|
+
export function parseDescriptions(text, commands) {
|
|
615
|
+
const out = new Map();
|
|
616
|
+
for (const line of String(text ?? '').split('\n')) {
|
|
617
|
+
const match = line.match(/^\s*(\d{1,2})[.)]\s*(.+?)\s*$/);
|
|
618
|
+
if (!match) continue;
|
|
619
|
+
const command = commands[Number(match[1]) - 1];
|
|
620
|
+
const description = match[2].replace(/\s+/g, ' ').replace(/\.$/, '').slice(0, 120);
|
|
621
|
+
if (!command || !description || /^unclear$/i.test(description)) continue;
|
|
622
|
+
out.set(command, description);
|
|
623
|
+
}
|
|
624
|
+
return out;
|
|
625
|
+
}
|