@skillstech/thunderlang 0.5.11 → 0.5.12
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.
- package/CHANGELOG.md +437 -0
- package/dist/core.cjs +30431 -7604
- package/dist/index.cjs +28577 -5331
- package/dist/learn.cjs +22676 -3870
- package/package.json +12 -3
- package/src/bicep.mjs +167 -0
- package/src/cli.mjs +169 -3
- package/src/commands.mjs +464 -0
- package/src/core.d.ts +12 -4
- package/src/core.mjs +12 -4
- package/src/emit.mjs +1 -1
- package/src/executed.d.mts +53 -0
- package/src/executed.mjs +465 -0
- package/src/faithful.mjs +137 -0
- package/src/file-formats.mjs +35 -0
- package/src/hcl.mjs +94 -0
- package/src/index.d.ts +7 -1
- package/src/index.mjs +6 -1
- package/src/infra-bicep.mjs +312 -0
- package/src/infra-cloud.mjs +348 -0
- package/src/infra-compose.mjs +315 -0
- package/src/infra-containers.mjs +100 -0
- package/src/infra-docker.mjs +267 -0
- package/src/infra-expr.mjs +509 -0
- package/src/infra-gha.mjs +350 -0
- package/src/infra-k8s.mjs +657 -0
- package/src/infra-terraform.mjs +499 -0
- package/src/infra-words.mjs +811 -0
- package/src/libraries.mjs +202 -0
- package/src/lift.mjs +48 -6
- package/src/meaning.d.mts +89 -0
- package/src/meaning.mjs +2544 -0
- package/src/model.d.mts +59 -12
- package/src/model.mjs +462 -80
- package/src/powershell.mjs +1361 -0
- package/src/read-repo-fs.mjs +29 -7
- package/src/read-repo.d.mts +5 -1
- package/src/read-repo.mjs +33 -7
- package/src/read.d.mts +21 -1
- package/src/read.mjs +1015 -42
- package/src/review.d.mts +35 -0
- package/src/review.mjs +671 -0
- package/src/rlang.mjs +1084 -0
- package/src/round-trip.d.mts +12 -0
- package/src/round-trip.mjs +174 -0
- package/src/runners/thunder-generate.mjs +56 -0
- package/src/runners/thunder-trace.cjs +284 -0
- package/src/runners/thunder_trace.py +599 -0
- package/src/running.d.mts +104 -0
- package/src/running.mjs +787 -0
- package/src/same.d.mts +1 -0
- package/src/same.mjs +24 -3
- package/src/shell.mjs +1212 -0
- package/src/sql.mjs +3 -2
- package/src/steps.d.mts +6 -1
- package/src/steps.mjs +1448 -86
- package/src/system.d.mts +12 -0
- package/src/system.mjs +845 -0
- package/src/things-langs.mjs +275 -0
- package/src/thunderize.mjs +3 -3
- package/src/visual.d.mts +5 -5
- package/src/visual.mjs +24 -3
- package/src/write-steps.d.mts +64 -0
- package/src/write-steps.mjs +1608 -0
- package/src/write.mjs +2 -1
- package/src/yaml.mjs +403 -0
package/src/running.mjs
ADDED
|
@@ -0,0 +1,787 @@
|
|
|
1
|
+
// The running system: what happened when the code ran, shown on Thunder's pictures.
|
|
2
|
+
//
|
|
3
|
+
// When something runs or breaks, people read logs and stack traces in the original language. Here that evidence is
|
|
4
|
+
// placed on the flows Thunder already draws: how many times each step ran, which side of each question was taken,
|
|
5
|
+
// how often each Stop happened (with the error and message as seen), which endings were reached, and, per action or
|
|
6
|
+
// rule, its calls, failures, and time. A stack trace is explained in Thunder: the action, the step, and, when the
|
|
7
|
+
// flow shows it, the answers that led there.
|
|
8
|
+
//
|
|
9
|
+
// Every source becomes one shape of evidence, an event:
|
|
10
|
+
//
|
|
11
|
+
// { file, line, kind: 'decision' | 'return' | 'raise' | 'call' | 'line', side?, error?, message?, value?,
|
|
12
|
+
// durationMs?, count?, function?, source }
|
|
13
|
+
//
|
|
14
|
+
// * decision: a question at `line` was answered; side is 'held' / 'not held' (the code's condition), 'case:<line>',
|
|
15
|
+
// 'each' / 'done' (a loop), or 'handled' (an error handler ran);
|
|
16
|
+
// * return: the code gave back at `line` (null: it reached the end of the function);
|
|
17
|
+
// * raise: the code raised or threw at `line` (a Stop when Thunder draws one there; otherwise a failure at a step);
|
|
18
|
+
// * call: an action or rule ran (by its file and first line, or by its function name), optionally with its time
|
|
19
|
+
// and whether it failed;
|
|
20
|
+
// * line: something reported from `line` (a log line), so the step on that line ran.
|
|
21
|
+
//
|
|
22
|
+
// Adapters: fromTrace (proof by running's recorders, executed.mjs), fromStack (Python tracebacks, Node/V8 stacks,
|
|
23
|
+
// Java and C# stack traces, Go panics), fromLogs (JSON lines with a file/line or function), fromOtlp (OpenTelemetry
|
|
24
|
+
// spans exported as OTLP/JSON with code.* attributes).
|
|
25
|
+
//
|
|
26
|
+
// Honesty: counts come only from evidence. Nothing is said about a path no evidence shows; a step with no evidence has
|
|
27
|
+
// no count (not zero). Evidence that cannot be placed on a step is listed under notMatched with the reason. The one
|
|
28
|
+
// thing worked out rather than observed is "how it got there" for an error: the answers that lead to a step in the
|
|
29
|
+
// flow as drawn (a step inside a question's Yes side, or after a question whose other side always stops). It is
|
|
30
|
+
// worded as such, and left out when the flow does not show it.
|
|
31
|
+
//
|
|
32
|
+
// Pure and browser-safe (no node: imports).
|
|
33
|
+
import { pathsOf, modelText } from './model.mjs';
|
|
34
|
+
|
|
35
|
+
export const RUNNING_SCHEMA = 'thunder-running-v1';
|
|
36
|
+
|
|
37
|
+
// ── Small helpers ───────────────────────────────────────────────────────────────────────────────────────────
|
|
38
|
+
|
|
39
|
+
/** 1204 -> "1,204". */
|
|
40
|
+
export const countWords = (n) => String(Math.round(n)).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
41
|
+
const pct = (part, whole) => { if (!whole) return null; const p = (part / whole) * 100; return p > 0 && p < 1 ? '<1%' : `${Math.round(p)}%`; };
|
|
42
|
+
const times = (n) => `${countWords(n)} time${n === 1 ? '' : 's'}`;
|
|
43
|
+
const ms = (x) => (x >= 1000 ? `${(x / 1000).toFixed(x >= 10000 ? 0 : 1)} s` : x >= 10 ? `${Math.round(x)} ms` : `${Number(x.toFixed(2))} ms`);
|
|
44
|
+
const norm = (p) => String(p || '').replace(/^file:\/\//, '').replace(/\\/g, '/').replace(/^\.\//, '');
|
|
45
|
+
const quoteText = (s, max = 80) => { const t = String(s ?? '').replace(/\s+/g, ' ').trim(); return t.length > max ? `${t.slice(0, max - 1)}…` : t; };
|
|
46
|
+
|
|
47
|
+
// ── Adapters: each source -> events ───────────────────────────────────────────────────────────────────────────
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Proof by running's record (executed.mjs: { files: { path: { functions, decisions, returns } } }) as events. The
|
|
51
|
+
* recorders note per line which way each question went, how each call ended, and what was raised.
|
|
52
|
+
*/
|
|
53
|
+
export function fromTrace(trace) {
|
|
54
|
+
const out = [];
|
|
55
|
+
for (const [file, f] of Object.entries(trace?.files || {})) {
|
|
56
|
+
for (const [L, d] of Object.entries(f.decisions || {})) {
|
|
57
|
+
for (const [k, count] of Object.entries(d.obs || {})) {
|
|
58
|
+
const [where, nRaw] = k.split('|');
|
|
59
|
+
const next = nRaw === '' || nRaw == null ? null : Number(nRaw);
|
|
60
|
+
const side = d.kind === 'handler' ? 'handled' : where.startsWith('case:') ? where : d.kind === 'for' ? (where === 'body' ? 'each' : 'done') : where === 'body' ? 'held' : 'not held';
|
|
61
|
+
out.push({ source: 'trace', kind: 'decision', file, line: Number(L), side, count, question: d.kind, ...(next != null ? { next } : {}) });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
for (const [L, fn] of Object.entries(f.functions || {})) {
|
|
65
|
+
const line = Number(L);
|
|
66
|
+
out.push({ source: 'trace', kind: 'call', file, line, fnLine: line, count: fn.runs || 0, failed: fn.unwound || 0 });
|
|
67
|
+
for (const [R, e] of Object.entries(fn.returns || {})) out.push({ source: 'trace', kind: 'return', file, line: Number(R), fnLine: line, count: e.count, ...(e.values?.length ? { value: e.values } : {}), ...(f.returns?.[R] != null ? { within: f.returns[R] } : {}) });
|
|
68
|
+
for (const [R, e] of Object.entries(fn.raises || {})) out.push({ source: 'trace', kind: 'raise', file, line: Number(R), fnLine: line, count: e.count, error: Object.keys(e.types || {}).join(' or ') || undefined });
|
|
69
|
+
if (fn.fellOff) out.push({ source: 'trace', kind: 'return', file, line: null, fnLine: line, count: fn.fellOff, ...(fn.fellOffAt ? { after: Object.keys(fn.fellOffAt).map(Number) } : {}) });
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Stack traces. Each parser returns { language, error, message, frames: [{ file, line, function }] } with the
|
|
76
|
+
// innermost frame (where it failed) first.
|
|
77
|
+
|
|
78
|
+
const PY_FRAME = /^\s*File "([^"]+)", line (\d+)(?:, in (.+))?\s*$/;
|
|
79
|
+
const PY_CHAIN = /^(?:During handling of the above exception, another exception occurred:|The above exception was the direct cause of the following exception:)\s*$/;
|
|
80
|
+
const JS_FRAME = /^\s*at (?:(?:async )?(.+?) \()?((?:node:|file:\/\/)?(?:[A-Za-z]:)?[^():]+):(\d+):(\d+)\)?\s*$/;
|
|
81
|
+
const JAVA_FRAME = /^\s*at ([\w$.<>/]+)\.([\w$<>]+)\(([^:()]+):(\d+)\)\s*$/;
|
|
82
|
+
const CS_FRAME = /^\s*at (.+?)\(.*?\) in (.+):line (\d+)\s*$/;
|
|
83
|
+
const GO_FUNC = /^([\w./*()-]+?(?:\.[\w$*()]+)+)\(.*\)\s*$/;
|
|
84
|
+
const GO_FILE = /^\t(.+?\.go):(\d+)(?: \+0x[0-9a-f]+)?\s*$/;
|
|
85
|
+
|
|
86
|
+
function parsePython(lines) {
|
|
87
|
+
// The last traceback in a chain is the one that ended the program; earlier ones are its causes.
|
|
88
|
+
const sections = [];
|
|
89
|
+
let cur = null;
|
|
90
|
+
for (const l of lines) {
|
|
91
|
+
if (/^Traceback \(most recent call last\):/.test(l.trim())) { cur = { frames: [] }; sections.push(cur); continue; }
|
|
92
|
+
if (PY_CHAIN.test(l.trim())) { cur = null; continue; }
|
|
93
|
+
if (!cur) continue;
|
|
94
|
+
const f = l.match(PY_FRAME);
|
|
95
|
+
if (f) { cur.frames.push({ file: f[1], line: Number(f[2]), function: f[3]?.trim() }); continue; }
|
|
96
|
+
// The error is the first line after the frames that is not indented (frames show their code indented).
|
|
97
|
+
if (cur.error == null && cur.frames.length && l.trim() && !/^\s/.test(l)) {
|
|
98
|
+
const m = l.trim().match(/^([A-Za-z_][\w.]*)(?::\s?(.*))?$/);
|
|
99
|
+
if (m) { cur.error = m[1].split('.').pop(); cur.message = (m[2] ?? '').trim(); }
|
|
100
|
+
cur = cur.error ? cur : null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const done = sections.filter((x) => x.frames.length && x.error);
|
|
104
|
+
if (!done.length) return null;
|
|
105
|
+
const last = done.at(-1);
|
|
106
|
+
return { language: 'python', error: last.error, message: last.message, frames: [...last.frames].reverse(), ...(done.length > 1 ? { causes: done.slice(0, -1).map((x) => ({ error: x.error, message: x.message })) } : {}) };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// pytest's own report of a failing test: code with `>` at the failing line, `E Error: message`, and a
|
|
110
|
+
// `path.py:42: Error` line after each frame (outermost first).
|
|
111
|
+
const PYTEST_LOC = /^([^\s:][^:]*\.py):(\d+):(?: in (\S+)| (\w[\w.]*))?\s*$/;
|
|
112
|
+
function parsePytest(lines) {
|
|
113
|
+
const frames = [];
|
|
114
|
+
let error = null, message = '';
|
|
115
|
+
let fn = null;
|
|
116
|
+
for (const l of lines) {
|
|
117
|
+
const d = l.match(/^\s*(?:async )?def (\w+)\(/);
|
|
118
|
+
if (d) fn = d[1];
|
|
119
|
+
const e = l.match(/^E\s+([A-Za-z_][\w.]*?)(?::\s?(.*))?$/);
|
|
120
|
+
if (e && error == null && /^[A-Z]/.test(e[1].split('.').pop())) { error = e[1].split('.').pop(); message = (e[2] ?? '').trim(); }
|
|
121
|
+
const loc = l.match(PYTEST_LOC);
|
|
122
|
+
if (loc) { const name = loc[3] ?? fn; frames.push({ file: loc[1], line: Number(loc[2]), ...(name ? { function: name } : {}) }); fn = null; if (loc[4] && error == null) error = loc[4].split('.').pop(); }
|
|
123
|
+
}
|
|
124
|
+
return frames.length && error ? { language: 'python', error, message, frames: frames.reverse() } : null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function parseJs(lines) {
|
|
128
|
+
const at = lines.findIndex((l) => JS_FRAME.test(l));
|
|
129
|
+
if (at < 0) return null;
|
|
130
|
+
// The error line is the last non-frame line above the first frame that names an error ("TypeError: …").
|
|
131
|
+
let error = 'Error', message = '';
|
|
132
|
+
for (let i = at - 1; i >= 0; i--) {
|
|
133
|
+
const m = lines[i].trim().match(/^(?:Uncaught )?([A-Z][\w$]*(?:Error|Exception)|Error|AssertionError(?: \[[A-Z_]+\])?)(?::\s?(.*))?$/);
|
|
134
|
+
if (m) { error = m[1].replace(/ \[.*\]$/, ''); message = (m[2] ?? '').trim(); break; }
|
|
135
|
+
}
|
|
136
|
+
const frames = [];
|
|
137
|
+
for (let i = at; i < lines.length; i++) {
|
|
138
|
+
const f = lines[i].match(JS_FRAME);
|
|
139
|
+
if (!f) { if (frames.length && lines[i].trim() && !/^\s*at /.test(lines[i])) break; continue; }
|
|
140
|
+
if (/^node:/.test(f[2])) { frames.push({ file: f[2], line: Number(f[3]), function: f[1], internal: true }); continue; }
|
|
141
|
+
frames.push({ file: f[2], line: Number(f[3]), function: f[1]?.replace(/^(?:new |Object\.|Function\.)/, '') });
|
|
142
|
+
}
|
|
143
|
+
return frames.length ? { language: 'javascript', error, message, frames } : null;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function parseJava(lines) {
|
|
147
|
+
// Java prints the outer exception first and its causes below ("Caused by:"); the deepest cause holds the line
|
|
148
|
+
// where it actually went wrong.
|
|
149
|
+
const blocks = [];
|
|
150
|
+
let cur = null;
|
|
151
|
+
for (const l of lines) {
|
|
152
|
+
const head = l.trim().match(/^(?:Exception in thread "[^"]*"\s+|Caused by:\s+)?((?:[a-z_$][\w$]*\.)+[A-Z][\w$]*(?:Exception|Error|Throwable)?|[A-Z][\w$]*(?:Exception|Error))(?::\s?(.*))?$/);
|
|
153
|
+
const f = l.match(JAVA_FRAME);
|
|
154
|
+
if (f) { if (cur) cur.frames.push({ file: f[3], line: Number(f[4]), function: `${f[1]}.${f[2]}`, className: f[1] }); continue; }
|
|
155
|
+
if (head && !/^\s+at /.test(l) && (/^(?:Exception in thread|Caused by:)/.test(l.trim()) || !cur)) { cur = { qualified: head[1], error: head[1].split('.').pop(), message: (head[2] ?? '').trim(), frames: [] }; blocks.push(cur); }
|
|
156
|
+
}
|
|
157
|
+
const withFrames = blocks.filter((b) => b.frames.length);
|
|
158
|
+
if (!withFrames.length) return null;
|
|
159
|
+
const root = withFrames.at(-1);
|
|
160
|
+
return { language: 'java', error: root.error, message: root.message, frames: root.frames, ...(withFrames.length > 1 ? { wrappedIn: withFrames.slice(0, -1).map((b) => ({ error: b.error, message: b.message })) } : {}) };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function parseCSharp(lines) {
|
|
164
|
+
const at = lines.findIndex((l) => CS_FRAME.test(l) || /^\s*at [\w.<>`]+\(.*\)\s*$/.test(l));
|
|
165
|
+
if (at < 0) return null;
|
|
166
|
+
let error = 'Exception', message = '';
|
|
167
|
+
for (let i = at - 1; i >= 0; i--) {
|
|
168
|
+
const m = lines[i].trim().match(/^(?:Unhandled exception\.\s*)?((?:[A-Za-z_][\w]*\.)*[A-Z]\w*Exception)(?::\s?(.*))?$/);
|
|
169
|
+
if (m) { error = m[1].split('.').pop(); message = (m[2] ?? '').replace(/\s*--->.*$/, '').trim(); break; }
|
|
170
|
+
}
|
|
171
|
+
const frames = [];
|
|
172
|
+
for (let i = at; i < lines.length; i++) {
|
|
173
|
+
const f = lines[i].match(CS_FRAME);
|
|
174
|
+
if (f) frames.push({ file: f[2], line: Number(f[3]), function: f[1] });
|
|
175
|
+
else if (/^\s*at /.test(lines[i])) frames.push({ file: null, line: null, function: lines[i].trim().slice(3).replace(/\(.*$/, ''), internal: true });
|
|
176
|
+
else if (frames.length && lines[i].trim() && !/^\s*---/.test(lines[i])) break;
|
|
177
|
+
}
|
|
178
|
+
return frames.some((f) => f.file) ? { language: 'csharp', error, message, frames } : null;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function parseGo(lines) {
|
|
182
|
+
const p = lines.findIndex((l) => /^panic: /.test(l) || /^fatal error: /.test(l));
|
|
183
|
+
if (p < 0) return null;
|
|
184
|
+
const head = lines[p].replace(/^panic: |^fatal error: /, '').replace(/ \[recovered\]$/, '');
|
|
185
|
+
const rt = head.match(/^runtime error: (.*)$/);
|
|
186
|
+
const err = head.match(/^([\w.*]+(?:Error|Err)\w*)?:?\s*(.*)$/);
|
|
187
|
+
const error = rt ? 'runtime error' : /^fatal/.test(lines[p]) ? 'fatal error' : 'panic';
|
|
188
|
+
const message = rt ? rt[1] : head;
|
|
189
|
+
const frames = [];
|
|
190
|
+
for (let i = p + 1; i < lines.length - 1; i++) {
|
|
191
|
+
if (/^goroutine \d+ \[/.test(lines[i]) && frames.length) break;
|
|
192
|
+
const fn = lines[i].match(GO_FUNC), loc = lines[i + 1].match(GO_FILE);
|
|
193
|
+
if (fn && loc) {
|
|
194
|
+
const internal = /^runtime\.|^panic\(/.test(fn[1]) || /\/src\/runtime\//.test(loc[1]);
|
|
195
|
+
frames.push({ file: loc[1], line: Number(loc[2]), function: fn[1], ...(internal ? { internal: true } : {}) });
|
|
196
|
+
i++;
|
|
197
|
+
} else if (/^panic\(/.test(lines[i]) && loc) { frames.push({ file: loc[1], line: Number(loc[2]), function: 'panic', internal: true }); i++; }
|
|
198
|
+
}
|
|
199
|
+
void err;
|
|
200
|
+
return frames.length ? { language: 'go', error, message, frames } : null;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Read one stack trace in any of the languages Thunder knows (Python, JavaScript/TypeScript, Java, C#, Go). */
|
|
204
|
+
export function parseStack(text) {
|
|
205
|
+
const lines = String(text ?? '').replace(/\r\n?/g, '\n').split('\n');
|
|
206
|
+
const tries = [];
|
|
207
|
+
if (lines.some((l) => /^Traceback \(most recent call last\):/.test(l.trim()))) tries.push(parsePython);
|
|
208
|
+
if (lines.some((l) => /^E\s{2,}\S/.test(l)) && lines.some((l) => PYTEST_LOC.test(l))) tries.push(parsePytest);
|
|
209
|
+
if (lines.some((l) => /^(?:panic|fatal error): /.test(l)) && lines.some((l) => GO_FILE.test(l))) tries.push(parseGo);
|
|
210
|
+
if (lines.some((l) => CS_FRAME.test(l))) tries.push(parseCSharp);
|
|
211
|
+
if (lines.some((l) => JAVA_FRAME.test(l))) tries.push(parseJava);
|
|
212
|
+
if (lines.some((l) => JS_FRAME.test(l))) tries.push(parseJs);
|
|
213
|
+
for (const t of tries) { const r = t(lines); if (r) return r; }
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Every stack trace in a text (a log of many failures): split on where each one starts. */
|
|
218
|
+
export function parseStacks(text) {
|
|
219
|
+
const lines = String(text ?? '').replace(/\r\n?/g, '\n').split('\n');
|
|
220
|
+
const starts = [];
|
|
221
|
+
lines.forEach((l, i) => {
|
|
222
|
+
const prev = lines[i - 1] ?? '';
|
|
223
|
+
if (/^Traceback \(most recent call last\):/.test(l.trim()) && !PY_CHAIN.test(lines[i - 2]?.trim() ?? '')) starts.push(i);
|
|
224
|
+
else if (/^_{3,} .+ _{3,}$/.test(l)) starts.push(i);
|
|
225
|
+
else if (/^(?:panic|fatal error): /.test(l)) starts.push(i);
|
|
226
|
+
else if (/^(?:Exception in thread |Unhandled exception\.)/.test(l)) starts.push(i);
|
|
227
|
+
else if (/^\s*at /.test(lines[i + 1] ?? '') && !/^\s*at /.test(l) && l.trim() && !/^\s/.test(l) && !/^Caused by:/.test(l) && !/^\s*at /.test(prev) && !/^\s*\.\.\. \d+ more/.test(prev)) starts.push(i);
|
|
228
|
+
});
|
|
229
|
+
// A Python chain keeps its sections together; everything else is cut at each start.
|
|
230
|
+
const cuts = [...new Set(starts)].sort((a, b) => a - b);
|
|
231
|
+
if (!cuts.length) { const one = parseStack(text); return one ? [one] : []; }
|
|
232
|
+
const out = [];
|
|
233
|
+
for (let k = 0; k < cuts.length; k++) {
|
|
234
|
+
const r = parseStack(lines.slice(cuts[k], cuts[k + 1] ?? lines.length).join('\n'));
|
|
235
|
+
if (r) out.push(r);
|
|
236
|
+
}
|
|
237
|
+
return out;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Stack traces as events: each failure is a `raise` at its frames (placed on the innermost frame Thunder read). */
|
|
241
|
+
export function fromStack(text, { count = 1 } = {}) {
|
|
242
|
+
return parseStacks(text).map((s) => ({ source: 'stack', kind: 'raise', file: s.frames[0]?.file ?? null, line: s.frames[0]?.line ?? null, error: s.error, message: s.message, frames: s.frames, language: s.language, count, ends: true }));
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const LEVEL_NUM = { 10: 'trace', 20: 'debug', 30: 'info', 40: 'warn', 50: 'error', 60: 'fatal' };
|
|
246
|
+
// A field by name: flat ("log.origin" as one key, as ECS writes it), or nested (log -> origin).
|
|
247
|
+
const pick = (o, keys) => {
|
|
248
|
+
for (const k of keys) {
|
|
249
|
+
const v = o?.[k] ?? k.split('.').reduce((a, p) => (a == null ? a : a[p]), o);
|
|
250
|
+
if (v != null && v !== '') return v;
|
|
251
|
+
}
|
|
252
|
+
return undefined;
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
/** Where a structured log line says it came from: { file, line, function }. */
|
|
256
|
+
function logSource(o) {
|
|
257
|
+
const nested = pick(o, ['logging.googleapis.com/sourceLocation', 'sourceLocation', 'source', 'src', 'location', 'log.origin', 'caller', 'callsite']);
|
|
258
|
+
let file = pick(o, ['file', 'filename', 'pathname', 'code.filepath', 'code.file.path', 'log.origin.file.name', 'source_file']);
|
|
259
|
+
let line = pick(o, ['line', 'lineno', 'lineNumber', 'code.lineno', 'code.line.number', 'log.origin.file.line', 'source_line']);
|
|
260
|
+
let fn = pick(o, ['function', 'funcName', 'func', 'method', 'code.function', 'code.function.name', 'log.origin.function']);
|
|
261
|
+
if (typeof nested === 'string') {
|
|
262
|
+
const m = nested.match(/^(.*?):(\d+)(?::\d+)?$/);
|
|
263
|
+
if (m) { file ??= m[1]; line ??= m[2]; }
|
|
264
|
+
} else if (nested && typeof nested === 'object') {
|
|
265
|
+
file ??= pick(nested, ['file.name', 'file', 'filename', 'path']);
|
|
266
|
+
line ??= pick(nested, ['file.line', 'line', 'lineno', 'lineNumber']);
|
|
267
|
+
fn ??= pick(nested, ['function', 'func', 'method', 'functionName']);
|
|
268
|
+
}
|
|
269
|
+
if (typeof file === 'object') file = undefined;
|
|
270
|
+
return { file: file != null ? String(file) : null, line: line != null && /^\d+$/.test(String(line)) ? Number(line) : null, function: fn != null ? String(fn) : null };
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Structured logs (JSON lines: one object per line) as events: a `line` event where a log line names its file and
|
|
275
|
+
* line, a `call` of the named function otherwise. An error logged with its stack (exc_info, err.stack) is also read
|
|
276
|
+
* as a stack trace. Lines that are not JSON, or name no place in the code, are kept as not matched.
|
|
277
|
+
*/
|
|
278
|
+
export function fromLogs(text) {
|
|
279
|
+
const out = [];
|
|
280
|
+
const lines = String(text ?? '').split(/\r?\n/);
|
|
281
|
+
let notJson = 0, noPlace = 0;
|
|
282
|
+
for (const raw of lines) {
|
|
283
|
+
const l = raw.trim();
|
|
284
|
+
if (!l) continue;
|
|
285
|
+
let o;
|
|
286
|
+
try { o = JSON.parse(l); } catch { notJson++; continue; }
|
|
287
|
+
if (!o || typeof o !== 'object') { notJson++; continue; }
|
|
288
|
+
const lv = pick(o, ['level', 'severity', 'levelname', 'lvl', 'log.level']);
|
|
289
|
+
const level = String(typeof lv === 'number' ? LEVEL_NUM[lv] ?? lv : lv ?? 'info').toLowerCase();
|
|
290
|
+
const message = pick(o, ['message', 'msg', 'event', 'text']);
|
|
291
|
+
const src = logSource(o);
|
|
292
|
+
const stack = pick(o, ['exc_info', 'stack', 'stack_trace', 'err.stack', 'error.stack', 'error.stack_trace', 'exception']);
|
|
293
|
+
if (typeof stack === 'string' && /\n/.test(stack)) out.push(...fromStack(stack).map((e) => ({ ...e, source: 'logs', ends: false })));
|
|
294
|
+
if (!src.file && !src.function) { noPlace++; continue; }
|
|
295
|
+
out.push({ source: 'logs', kind: src.line != null ? 'line' : 'call', file: src.file, line: src.line, function: src.function, level, ...(message != null ? { message: String(message) } : {}), count: 1, ...(src.line == null ? { fromLog: true } : {}) });
|
|
296
|
+
}
|
|
297
|
+
if (notJson) out.push({ source: 'logs', kind: 'line', file: null, line: null, count: notJson, unplaced: 'the line is not JSON, so Thunder cannot tell where in the code it came from' });
|
|
298
|
+
if (noPlace) out.push({ source: 'logs', kind: 'line', file: null, line: null, count: noPlace, unplaced: 'the log line names no file, line, or function' });
|
|
299
|
+
return out;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const otlpValue = (v) => (v == null ? undefined : v.stringValue ?? (v.intValue != null ? Number(v.intValue) : undefined) ?? v.doubleValue ?? v.boolValue);
|
|
303
|
+
const otlpAttrs = (list) => Object.fromEntries((list || []).map((a) => [a.key, otlpValue(a.value)]));
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* OpenTelemetry spans (an OTLP/JSON export: { resourceSpans: [...] }, or one such object per line, as the
|
|
307
|
+
* collector's file exporter writes) as events: each span with code.filepath / code.lineno / code.function (or the
|
|
308
|
+
* newer code.file.path / code.line.number / code.function.name) is a `call` with its duration and whether it failed;
|
|
309
|
+
* an exception event with a stack trace is read as one.
|
|
310
|
+
*/
|
|
311
|
+
export function fromOtlp(input) {
|
|
312
|
+
const docs = [];
|
|
313
|
+
if (typeof input === 'string') {
|
|
314
|
+
try { docs.push(JSON.parse(input)); } catch { for (const l of input.split(/\r?\n/)) { if (l.trim()) { try { docs.push(JSON.parse(l)); } catch { /* not a JSON line */ } } } }
|
|
315
|
+
} else if (input) docs.push(input);
|
|
316
|
+
const out = [];
|
|
317
|
+
let noCode = 0;
|
|
318
|
+
for (const d of docs) {
|
|
319
|
+
for (const rs of d.resourceSpans || []) {
|
|
320
|
+
for (const ss of [...(rs.scopeSpans || []), ...(rs.instrumentationLibrarySpans || [])]) {
|
|
321
|
+
for (const sp of ss.spans || []) {
|
|
322
|
+
const a = otlpAttrs(sp.attributes);
|
|
323
|
+
const file = a['code.filepath'] ?? a['code.file.path'] ?? null;
|
|
324
|
+
const line = a['code.lineno'] ?? a['code.line.number'] ?? null;
|
|
325
|
+
const fnFull = a['code.function'] ?? a['code.function.name'] ?? null;
|
|
326
|
+
const ns = a['code.namespace'] ?? null;
|
|
327
|
+
const start = Number(sp.startTimeUnixNano), end = Number(sp.endTimeUnixNano);
|
|
328
|
+
const durationMs = Number.isFinite(start) && Number.isFinite(end) && end >= start ? (end - start) / 1e6 : undefined;
|
|
329
|
+
const failed = sp.status && (sp.status.code === 2 || sp.status.code === 'STATUS_CODE_ERROR');
|
|
330
|
+
const exc = (sp.events || []).find((e) => e.name === 'exception');
|
|
331
|
+
const ex = exc ? otlpAttrs(exc.attributes) : {};
|
|
332
|
+
if (!file && !fnFull) { noCode++; continue; }
|
|
333
|
+
// code.function.name may be fully qualified ("Library.borrow"); the last part is the function.
|
|
334
|
+
const fn = fnFull ? String(fnFull).split(/[.:]+/).pop() : null;
|
|
335
|
+
const owner = ns ?? (fnFull && /[.:]/.test(String(fnFull)) ? String(fnFull).split(/[.:]+/).slice(-2, -1)[0] : null);
|
|
336
|
+
out.push({ source: 'otlp', kind: 'call', file, line: line != null ? Number(line) : null, function: fn, ...(owner ? { namespace: String(owner).split('.').pop() } : {}), span: sp.name, count: 1, ...(durationMs != null ? { durationMs } : {}), failed: failed ? 1 : 0, ...(failed ? { error: ex['exception.type'] ? String(ex['exception.type']).split('.').pop() : 'error', message: ex['exception.message'] ?? sp.status.message ?? '' } : {}) });
|
|
337
|
+
if (typeof ex['exception.stacktrace'] === 'string') out.push(...fromStack(ex['exception.stacktrace']).map((e) => ({ ...e, source: 'otlp', ends: false })));
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
if (noCode) out.push({ source: 'otlp', kind: 'call', file: null, line: null, count: noCode, unplaced: 'the span has no code.filepath, code.lineno, or code.function attributes' });
|
|
343
|
+
return out;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// ── Placing evidence on the model ─────────────────────────────────────────────────────────────────────────────
|
|
347
|
+
|
|
348
|
+
/** Find the model's file for a path from elsewhere (absolute, another machine's folder, a Java file name). */
|
|
349
|
+
function fileMatcher(model) {
|
|
350
|
+
const paths = [...new Set([...(model.files || []).map((f) => f.path), ...model.members.map((m) => m.file)])];
|
|
351
|
+
const split = paths.map((p) => ({ p, parts: norm(p).split('/').filter(Boolean) }));
|
|
352
|
+
return (raw, hint) => {
|
|
353
|
+
const want = norm(raw);
|
|
354
|
+
if (!want) return { reason: 'it names no file' };
|
|
355
|
+
if (paths.includes(want)) return { file: want };
|
|
356
|
+
const parts = want.split('/').filter(Boolean);
|
|
357
|
+
// Java names only the file ("Library.java"); its class's package says the folders (com.school.Library).
|
|
358
|
+
if (parts.length === 1 && hint) parts.unshift(...hint.split('.').slice(0, -1));
|
|
359
|
+
let best = 0, found = [];
|
|
360
|
+
for (const s of split) {
|
|
361
|
+
let k = 0;
|
|
362
|
+
while (k < s.parts.length && k < parts.length && s.parts[s.parts.length - 1 - k] === parts[parts.length - 1 - k]) k++;
|
|
363
|
+
if (k > best) { best = k; found = [s.p]; } else if (k === best && k > 0) found.push(s.p);
|
|
364
|
+
}
|
|
365
|
+
if (!best) return { reason: `no file Thunder read is ${parts.at(-1)}` };
|
|
366
|
+
if (found.length > 1) return { reason: `${found.length} files Thunder read end with ${parts.slice(-best).join('/')}; the evidence does not say which` };
|
|
367
|
+
return { file: found[0] };
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** The innermost member of a file holding a line. */
|
|
372
|
+
function memberAt(model, file, line) {
|
|
373
|
+
const hits = model.members.filter((m) => m.file === file && line >= m.lines[0] && line <= m.lines[1]);
|
|
374
|
+
return hits.sort((a, b) => (a.lines[1] - a.lines[0]) - (b.lines[1] - b.lines[0]))[0] ?? null;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** Members by function name (and its class, when given). */
|
|
378
|
+
function membersNamed(model, fn, { file, namespace } = {}) {
|
|
379
|
+
const name = String(fn).replace(/^.*[.:#]/, '').replace(/^<|>$/g, '');
|
|
380
|
+
let hits = model.members.filter((m) => m.name === name && (!file || m.file === file));
|
|
381
|
+
if (namespace && hits.length > 1) {
|
|
382
|
+
const ns = hits.filter((m) => m.owner && String(m.owner).split('#').pop() === namespace);
|
|
383
|
+
if (ns.length) hits = ns;
|
|
384
|
+
}
|
|
385
|
+
return hits;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* The node of a member's flow at a line, for one kind of evidence: a question (for a decision), a Stop or ending, or
|
|
390
|
+
* any step. `approximate` when the line is inside a step that starts above it (a statement written over several
|
|
391
|
+
* lines); never a question's or loop's lines, which hold other steps.
|
|
392
|
+
*/
|
|
393
|
+
function nodeAt(member, line, want) {
|
|
394
|
+
const here = member.flow.nodes.filter((n) => n.line === line && n.kind !== 'start');
|
|
395
|
+
if (want === 'decision') return here.find((n) => n.kind === 'decision' && !n.handler) ?? here.find((n) => n.kind === 'loop') ?? null;
|
|
396
|
+
if (here.length) {
|
|
397
|
+
const order = want === 'raise' ? ['stop', 'step', 'code', 'end', 'decision', 'loop'] : ['step', 'code', 'stop', 'end', 'decision', 'loop'];
|
|
398
|
+
return { node: [...here].sort((a, b) => order.indexOf(a.kind) - order.indexOf(b.kind))[0], approximate: false };
|
|
399
|
+
}
|
|
400
|
+
const above = member.flow.nodes.filter((n) => n.line != null && n.line < line && n.kind !== 'start').sort((a, b) => b.line - a.line)[0];
|
|
401
|
+
if (above && (above.kind === 'step' || above.kind === 'code' || above.kind === 'stop' || above.kind === 'end')) {
|
|
402
|
+
// Only when no other drawn node of this member starts between it and the line (it is the statement in progress).
|
|
403
|
+
return { node: above, approximate: true };
|
|
404
|
+
}
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** Which side of a question a decision event took, in Thunder's words, and its block key. */
|
|
409
|
+
function sideOf(node, block, ev) {
|
|
410
|
+
if (node.kind === 'loop') return ev.side === 'each' || ev.side === 'held' ? { key: 'each', label: 'For each' } : { key: 'done', label: 'Done' };
|
|
411
|
+
if (node.cases) {
|
|
412
|
+
const P = String(ev.side).startsWith('case:') ? Number(String(ev.side).slice(5)) : null;
|
|
413
|
+
const c = P != null ? (block?.cases || []).find((x) => x.line === P) : null;
|
|
414
|
+
return c ? { key: `case:${P}`, label: c.label } : P == null ? { key: 'none', label: 'None of these' } : null;
|
|
415
|
+
}
|
|
416
|
+
if (ev.side === 'yes' || ev.side === 'no') return { key: ev.side, label: ev.side === 'yes' ? 'Yes' : 'No' };
|
|
417
|
+
const yesWhen = node.whenTrue || 'yes';
|
|
418
|
+
const s = ev.side === 'held' ? yesWhen : yesWhen === 'yes' ? 'no' : 'yes';
|
|
419
|
+
return { key: s, label: s === 'yes' ? 'Yes' : 'No' };
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function blocksById(flow) {
|
|
423
|
+
const out = new Map();
|
|
424
|
+
const walk = (bs) => { for (const b of bs || []) { out.set(b.id, b); walk(b.yes); walk(b.no); walk(b.each); walk(b.body); for (const c of b.cases || []) walk(c.blocks); for (const h of b.handlers || []) { out.set(h.id, h); walk(h.blocks); } if (b.always) walk(b.always.blocks); } };
|
|
425
|
+
walk(flow.blocks);
|
|
426
|
+
return out;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** A plain literal from a recorded value ('"NoFee"', 'True', '3'), or undefined. */
|
|
430
|
+
function literal(v) {
|
|
431
|
+
const s = String(v ?? '').trim();
|
|
432
|
+
if (/^-?\d+(?:\.\d+)?$/.test(s)) return Number(s);
|
|
433
|
+
if (/^(True|true)$/.test(s)) return true;
|
|
434
|
+
if (/^(False|false)$/.test(s)) return false;
|
|
435
|
+
if (/^(None|null|nil|undefined)$/.test(s)) return null;
|
|
436
|
+
const q = s.match(/^(["'])(.*)\1$/);
|
|
437
|
+
return q ? q[2] : s ? s : undefined;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Project evidence onto the model's flows. `evidence` is a list of events (from the adapters above, or your own in
|
|
442
|
+
* the same shape). Returns thunder-running-v1:
|
|
443
|
+
*
|
|
444
|
+
* members: { [memberId]: { calls, failures, timing?, nodes: { [nodeId]: NodeRun }, hotPath, says } }
|
|
445
|
+
* notMatched: [{ source, kind, file, line, function?, what, reason, count }]
|
|
446
|
+
*
|
|
447
|
+
* A NodeRun has `ran` (times seen), and for a question `sides` ({ Yes: n, No: n } or each case), for a Stop
|
|
448
|
+
* `errors` ([{ error, message?, count }]), for a step that failed `failed` (the same), for a log `levels`.
|
|
449
|
+
*/
|
|
450
|
+
export function runtimeOverlay(model, evidence = []) {
|
|
451
|
+
const findFile = fileMatcher(model);
|
|
452
|
+
const members = {};
|
|
453
|
+
const notMatched = [];
|
|
454
|
+
const sources = {};
|
|
455
|
+
const unplaced = (ev, reason, what) => {
|
|
456
|
+
const key = `${ev.source}|${ev.kind}|${ev.file ?? ''}|${ev.line ?? ''}|${ev.function ?? ''}|${reason}`;
|
|
457
|
+
const had = notMatched.find((x) => x.key === key);
|
|
458
|
+
if (had) { had.count += ev.count ?? 1; return; }
|
|
459
|
+
notMatched.push({ key, source: ev.source ?? 'evidence', kind: ev.kind, file: ev.file ?? null, line: ev.line ?? null, ...(ev.function ? { function: String(ev.function) } : {}), what: what ?? describe(ev), reason, count: ev.count ?? 1 });
|
|
460
|
+
};
|
|
461
|
+
const mem = (m) => (members[m.id] ||= { id: m.id, name: m.name, label: m.label, file: m.file, lines: m.lines, calls: 0, failures: 0, nodes: {}, sources: [] });
|
|
462
|
+
const nodeRun = (r, n) => (r.nodes[n.id] ||= { id: n.id, kind: n.kind, text: n.text, line: n.line ?? null });
|
|
463
|
+
const addError = (list, ev, c) => {
|
|
464
|
+
const e = list.find((x) => x.error === (ev.error ?? 'an error') && (x.message ?? '') === (ev.message ?? ''));
|
|
465
|
+
if (e) e.count += c; else list.push({ error: ev.error ?? 'an error', ...(ev.message ? { message: ev.message } : {}), count: c });
|
|
466
|
+
};
|
|
467
|
+
// Where the evidence itself says functions start (a recorder's per-function record).
|
|
468
|
+
const fnStarts = new Set(evidence.filter((e) => e.fnLine != null).map((e) => `${norm(e.file)}|${e.fnLine}`));
|
|
469
|
+
const blocks = new Map();
|
|
470
|
+
const blockOf = (m, id) => { if (!blocks.has(m.id)) blocks.set(m.id, blocksById(m.flow)); return blocks.get(m.id).get(id); };
|
|
471
|
+
|
|
472
|
+
for (const ev of evidence) {
|
|
473
|
+
const c = ev.count ?? 1;
|
|
474
|
+
sources[ev.source ?? 'evidence'] = (sources[ev.source ?? 'evidence'] || 0) + c;
|
|
475
|
+
if (ev.unplaced) { unplaced(ev, ev.unplaced, ev.source === 'logs' ? 'log lines' : 'spans'); continue; }
|
|
476
|
+
if (!c) continue;
|
|
477
|
+
|
|
478
|
+
// Where: a stack's frames are tried innermost first, until one is in code Thunder read.
|
|
479
|
+
let file = null, line = ev.line ?? null, member = null, outside = [];
|
|
480
|
+
const frames = ev.frames?.length ? ev.frames : [{ file: ev.file, line: ev.line, function: ev.function, className: undefined }];
|
|
481
|
+
let why = null;
|
|
482
|
+
for (const f of frames) {
|
|
483
|
+
if (f.internal || f.file == null) { outside.push(f); continue; }
|
|
484
|
+
const got = findFile(f.file, f.className);
|
|
485
|
+
if (!got.file) { why ??= got.reason; outside.push(f); continue; }
|
|
486
|
+
file = got.file; line = f.line ?? null;
|
|
487
|
+
// A recorder's return or raise belongs to the function it was recorded in (by that function's first line).
|
|
488
|
+
// (A recorder may place a function where its header starts, a few lines below the member's first line; but a
|
|
489
|
+
// function inside it that starts there is another function, not this member.)
|
|
490
|
+
if (ev.fnLine != null) member = model.members.filter((m) => m.file === file && m.lines[0] <= ev.fnLine && ev.fnLine <= Math.min(m.lines[1], m.lines[0] + 3) && (m.lines[0] === ev.fnLine || !fnStarts.has(`${file}|${m.lines[0]}`))).sort((a, b) => b.lines[0] - a.lines[0])[0] ?? null;
|
|
491
|
+
else if (line != null && ev.kind !== 'call') member = memberAt(model, file, line);
|
|
492
|
+
else if (ev.kind === 'call' && ev.fnLine == null) {
|
|
493
|
+
if (line != null) {
|
|
494
|
+
const starts = model.members.filter((m) => m.file === file && m.lines[0] <= line && line <= m.lines[0] + 3).sort((a, b) => a.lines[0] - b.lines[0]);
|
|
495
|
+
member = starts.find((m) => m.lines[0] === line) ?? starts[0] ?? null;
|
|
496
|
+
// A span placed on a decorator or the line of a call into it: fall back to the function name.
|
|
497
|
+
if (!member && f.function) { const named = membersNamed(model, f.function, { file, namespace: ev.namespace }); if (named.length === 1) member = named[0]; }
|
|
498
|
+
if (!member) member = memberAt(model, file, line);
|
|
499
|
+
} else if (f.function) {
|
|
500
|
+
const named = membersNamed(model, f.function, { file, namespace: ev.namespace });
|
|
501
|
+
if (named.length === 1) member = named[0];
|
|
502
|
+
else { why = named.length ? `${named.length} actions or rules in ${file} are named ${f.function}` : `no action or rule in ${file} is named ${f.function}`; continue; }
|
|
503
|
+
}
|
|
504
|
+
} else if (f.function) {
|
|
505
|
+
const named = membersNamed(model, f.function, { file });
|
|
506
|
+
if (named.length === 1) member = named[0];
|
|
507
|
+
}
|
|
508
|
+
if (member) break;
|
|
509
|
+
why = ev.fnLine != null ? `no action or rule Thunder read starts at line ${ev.fnLine} of ${file}` : line != null ? `line ${line} of ${file} is not inside any action or rule Thunder read` : `${file} has no action or rule Thunder can place this on`;
|
|
510
|
+
outside.push(f);
|
|
511
|
+
}
|
|
512
|
+
if (!file && !member && ev.kind === 'call' && ev.function && !ev.file) {
|
|
513
|
+
const named = membersNamed(model, ev.function, { namespace: ev.namespace });
|
|
514
|
+
if (named.length === 1) { member = named[0]; file = member.file; }
|
|
515
|
+
else why = named.length ? `${named.length} actions or rules are named ${ev.function}; the evidence names no file` : `no action or rule Thunder read is named ${ev.function}`;
|
|
516
|
+
}
|
|
517
|
+
if (!member) { unplaced(ev, why ?? 'it names no place in the code'); continue; }
|
|
518
|
+
const r = mem(member);
|
|
519
|
+
if (!r.sources.includes(ev.source)) r.sources.push(ev.source);
|
|
520
|
+
|
|
521
|
+
if (ev.kind === 'call') {
|
|
522
|
+
r.calls += c;
|
|
523
|
+
if (ev.failed) { r.failures += ev.failed; if (ev.error) addError((r.errors ||= []), ev, ev.failed); }
|
|
524
|
+
if (ev.durationMs != null) {
|
|
525
|
+
const t = (r.timing ||= { count: 0, totalMs: 0, maxMs: 0 });
|
|
526
|
+
t.count += c; t.totalMs += ev.durationMs * c; t.maxMs = Math.max(t.maxMs, ev.durationMs);
|
|
527
|
+
}
|
|
528
|
+
if (ev.fromLog) { r.logged = (r.logged || 0) + c; r.calls -= c; }
|
|
529
|
+
continue;
|
|
530
|
+
}
|
|
531
|
+
if (ev.kind === 'decision') {
|
|
532
|
+
let n = nodeAt(member, line, 'decision');
|
|
533
|
+
let side = null;
|
|
534
|
+
// A handler: the "If it goes wrong" question on this line.
|
|
535
|
+
if (ev.side === 'handled') n = member.flow.nodes.find((x) => x.line === line && x.handler) ?? null;
|
|
536
|
+
// A `match` Thunder drew as a chain of questions: case P taken = the question at line P answered Yes.
|
|
537
|
+
if (!n && String(ev.side).startsWith('case:')) {
|
|
538
|
+
const P = Number(String(ev.side).slice(5));
|
|
539
|
+
const q = nodeAt(member, P, 'decision');
|
|
540
|
+
if (q && !q.cases) { n = q; side = { key: 'yes', label: 'Yes' }; }
|
|
541
|
+
}
|
|
542
|
+
if (!n) { unplaced(ev, `Thunder draws no question at line ${line} of ${member.label}`); continue; }
|
|
543
|
+
if ((ev.side === 'each' || ev.side === 'done') && n.kind !== 'loop') { unplaced(ev, `the run went round a loop at line ${line}, where Thunder draws the question "${n.text}"`); continue; }
|
|
544
|
+
const nr = nodeRun(r, n);
|
|
545
|
+
if (ev.side === 'handled') { nr.ran = (nr.ran || 0) + c; continue; }
|
|
546
|
+
side ??= sideOf(n, blockOf(member, n.id), ev);
|
|
547
|
+
if (!side) { unplaced(ev, `the case at line ${String(ev.side).slice(5)} is not one Thunder draws for "${n.text}"`); continue; }
|
|
548
|
+
(nr.sides ||= {})[side.label] = (nr.sides[side.label] || 0) + c;
|
|
549
|
+
if (n.kind !== 'loop') nr.ran = (nr.ran || 0) + c;
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
if (ev.kind === 'return') {
|
|
553
|
+
const ends = member.flow.nodes.filter((n) => n.kind === 'end');
|
|
554
|
+
let hit = [];
|
|
555
|
+
if (line == null) {
|
|
556
|
+
hit = ends.filter((n) => n.line == null);
|
|
557
|
+
// Reaching the end of a handler that answers with res.send(...) on its last line: that ending.
|
|
558
|
+
if (!hit.length && ev.after?.length) hit = ends.filter((n) => ev.after.includes(n.line));
|
|
559
|
+
if (!hit.length) {
|
|
560
|
+
const stops = member.flow.nodes.filter((n) => n.kind === 'stop' && ev.after?.includes(n.line));
|
|
561
|
+
if (stops.length === 1) { const nr = nodeRun(r, stops[0]); nr.ran = (nr.ran || 0) + c; continue; }
|
|
562
|
+
}
|
|
563
|
+
} else {
|
|
564
|
+
hit = ends.filter((n) => n.line === line);
|
|
565
|
+
// A rule's answer is drawn on its question's line; the return that gives it is on that line or below.
|
|
566
|
+
if (!hit.length) {
|
|
567
|
+
const answers = ends.filter((n) => n.answer && n.line != null);
|
|
568
|
+
const qs = member.flow.nodes.filter((n) => n.kind === 'decision' && n.line != null).map((n) => n.line);
|
|
569
|
+
if (ev.within != null) hit = answers.filter((n) => n.line === ev.within);
|
|
570
|
+
if (!hit.length) { const up = answers.filter((n) => n.line <= line).sort((a, b) => b.line - a.line)[0]; if (up && !qs.some((q) => q > up.line && q <= line)) hit = [up]; }
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
if (hit.length > 1 && ev.value?.length) {
|
|
574
|
+
const vals = ev.value.map(literal);
|
|
575
|
+
const byVal = hit.filter((n) => vals.some((v) => v !== undefined && (literal(n.value) === v || (typeof v === 'boolean' && n.value == null && String(n.text) === (v ? 'Yes' : 'No')))));
|
|
576
|
+
if (byVal.length) hit = byVal;
|
|
577
|
+
}
|
|
578
|
+
if (hit.length !== 1) {
|
|
579
|
+
unplaced(ev, hit.length ? `Thunder draws ${hit.length} answers at line ${line} of ${member.label}${ev.value?.length > 1 ? ` and the run gave back ${ev.value.length} different values` : ''}; the evidence does not say how many times each` : line == null ? `${member.label} has no "Done" ending, yet the run reached its end` : `Thunder draws no ending at line ${line} of ${member.label}`);
|
|
580
|
+
continue;
|
|
581
|
+
}
|
|
582
|
+
const nr = nodeRun(r, hit[0]);
|
|
583
|
+
nr.ran = (nr.ran || 0) + c;
|
|
584
|
+
if (ev.value?.length) { nr.values ||= []; for (const v of ev.value) if (!nr.values.includes(v) && nr.values.length < 6) nr.values.push(v); }
|
|
585
|
+
continue;
|
|
586
|
+
}
|
|
587
|
+
if (ev.kind === 'raise' || ev.kind === 'line') {
|
|
588
|
+
const at = line != null ? nodeAt(member, line, ev.kind) : null;
|
|
589
|
+
if (ev.kind === 'raise' && ev.ends) { r.failures += c; addError((r.errors ||= []), ev, c); }
|
|
590
|
+
if (!at) { unplaced(ev, line == null ? `it names ${member.label} but no line` : `Thunder draws no step at line ${line} of ${member.label}`); continue; }
|
|
591
|
+
const nr = nodeRun(r, at.node);
|
|
592
|
+
if (at.approximate) nr.approximate = true;
|
|
593
|
+
// A crash at a step says it ran (and failed) that often, not how often it ran in all: no `ran` from it.
|
|
594
|
+
if (ev.kind === 'line' || at.node.kind === 'stop') nr.ran = (nr.ran || 0) + c;
|
|
595
|
+
if (ev.kind === 'raise') addError(at.node.kind === 'stop' ? (nr.errors ||= []) : (nr.failed ||= []), ev, c);
|
|
596
|
+
else if (ev.level) (nr.levels ||= {})[ev.level] = (nr.levels[ev.level] || 0) + c;
|
|
597
|
+
if (outside.length && ev.kind === 'raise') nr.raisedInside = [...new Set([...(nr.raisedInside || []), ...outside.filter((f) => f.file && !f.internal).slice(0, 1).map((f) => norm(f.file).split('/').slice(-2).join('/'))])];
|
|
598
|
+
continue;
|
|
599
|
+
}
|
|
600
|
+
unplaced(ev, `"${ev.kind}" is not a kind of evidence Thunder reads`);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
for (const r of Object.values(members)) {
|
|
604
|
+
const m = model.members.find((x) => x.id === r.id);
|
|
605
|
+
if (r.timing) { r.timing.avgMs = r.timing.totalMs / r.timing.count; delete r.timing.totalMs; }
|
|
606
|
+
r.hotPath = hotPath(m, r);
|
|
607
|
+
r.says = runSays(m, r);
|
|
608
|
+
}
|
|
609
|
+
for (const x of notMatched) delete x.key;
|
|
610
|
+
return { schema: RUNNING_SCHEMA, members, notMatched, sources, summary: { members: Object.keys(members).length, events: evidence.length, notMatched: notMatched.reduce((a, x) => a + x.count, 0) } };
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
function describe(ev) {
|
|
614
|
+
const where = `${ev.file ?? '?'}${ev.line != null ? `:${ev.line}` : ''}${ev.function ? ` (${ev.function})` : ''}`;
|
|
615
|
+
if (ev.kind === 'raise') return `${ev.error ?? 'an error'}${ev.message ? `: ${quoteText(ev.message, 60)}` : ''} at ${where}`;
|
|
616
|
+
if (ev.kind === 'decision') return `a question answered at ${where}`;
|
|
617
|
+
if (ev.kind === 'return') return `a return at ${where}`;
|
|
618
|
+
if (ev.kind === 'line') return `a log line at ${where}`;
|
|
619
|
+
return `a call of ${where}`;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* The most-taken way through a flow: at each question the side seen most often. Only questions and endings with
|
|
624
|
+
* evidence are on it; it stops where the evidence does.
|
|
625
|
+
*/
|
|
626
|
+
function hotPath(m, r) {
|
|
627
|
+
const out = [];
|
|
628
|
+
const byId = new Map(m.flow.nodes.map((n) => [n.id, n]));
|
|
629
|
+
const go = (bs) => {
|
|
630
|
+
for (const b of bs || []) {
|
|
631
|
+
const n = byId.get(b.id);
|
|
632
|
+
const nr = r.nodes[b.id];
|
|
633
|
+
if (!n || !nr) { if (b.body && !b.nested) go(b.body); continue; }
|
|
634
|
+
if (nr.sides) {
|
|
635
|
+
const [label, count] = Object.entries(nr.sides).sort((a, b2) => b2[1] - a[1])[0];
|
|
636
|
+
out.push({ id: n.id, text: n.text, side: label, count });
|
|
637
|
+
const key = label === 'Yes' ? 'yes' : label === 'No' ? 'no' : label === 'For each' ? 'each' : null;
|
|
638
|
+
const next = key ? b[key] : (b.cases || []).find((c) => c.label === label)?.blocks;
|
|
639
|
+
if (next?.length) { if (go(next)) return true; }
|
|
640
|
+
if (n.kind === 'loop' && label === 'For each') continue;
|
|
641
|
+
} else if (nr.ran && (n.kind === 'stop' || n.kind === 'end')) {
|
|
642
|
+
out.push({ id: n.id, text: n.text, count: nr.ran });
|
|
643
|
+
return true;
|
|
644
|
+
}
|
|
645
|
+
if (b.body && !b.nested) { if (go(b.body)) return true; }
|
|
646
|
+
}
|
|
647
|
+
return false;
|
|
648
|
+
};
|
|
649
|
+
go(m.flow.blocks);
|
|
650
|
+
// "Done" (the end of the function) is not in the drawn blocks.
|
|
651
|
+
const done = m.flow.nodes.find((n) => n.kind === 'end' && n.line == null);
|
|
652
|
+
if (done && r.nodes[done.id]?.ran && out.length && !['stop', 'end'].includes(byId.get(out.at(-1).id)?.kind)) out.push({ id: done.id, text: done.text, count: r.nodes[done.id].ran });
|
|
653
|
+
return out.some((h) => h.side) ? out : [];
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
// "Is user set? was No"; an error handler's side reads "LocationParseError went wrong".
|
|
657
|
+
const whenWords = (when) => when.filter((w) => w.question).map((w) => {
|
|
658
|
+
const h = w.question.match(/^If (.+) goes wrong\??$/);
|
|
659
|
+
return h ? `${h[1]} ${w.answer === 'Yes' ? 'went' : 'did not go'} wrong` : `${w.question.replace(/\?$/, '')}? was ${w.answer}`;
|
|
660
|
+
}).join(' and ');
|
|
661
|
+
|
|
662
|
+
/** The Details sentence: "Ran 1,204 times; stopped with NotAvailable 37 times (3%), when …". */
|
|
663
|
+
function runSays(m, r) {
|
|
664
|
+
if (!m) return '';
|
|
665
|
+
const parts = [];
|
|
666
|
+
const total = r.calls;
|
|
667
|
+
const logged = (r.logged || 0) + Object.values(r.nodes).reduce((a, n) => a + (n.levels ? n.ran : 0), 0);
|
|
668
|
+
if (total) parts.push(`Ran ${times(total)}`);
|
|
669
|
+
else if (logged) parts.push(`Wrote ${countWords(logged)} log line${logged === 1 ? '' : 's'}`);
|
|
670
|
+
const paths = new Map(pathsOf(m.flow).map((p) => [p.node.id, p.when]));
|
|
671
|
+
for (const nr of Object.values(r.nodes)) {
|
|
672
|
+
if (nr.kind === 'stop' && nr.ran) {
|
|
673
|
+
const errs = (nr.errors || []).map((e) => `${e.error}${e.message ? ` "${quoteText(e.message, 60)}"` : ''}`);
|
|
674
|
+
const when = paths.get(nr.id);
|
|
675
|
+
parts.push(`stopped with ${errs.length ? errs.join(' or ') : nr.text.replace(/^Stop:\s*/, '')} ${times(nr.ran)}${pct(nr.ran, total) ? ` (${pct(nr.ran, total)})` : ''}${when?.length ? `, when ${whenWords(when)}` : ''}`);
|
|
676
|
+
}
|
|
677
|
+
if (nr.failed?.length) parts.push(`failed at “${nr.text}” (line ${nr.line}) with ${nr.failed.map((e) => `${e.error}${e.message ? ` "${quoteText(e.message, 60)}"` : ''}`).join(' or ')} ${times(nr.failed.reduce((a, e) => a + e.count, 0))}`);
|
|
678
|
+
}
|
|
679
|
+
// A rule's answers: how often each was given.
|
|
680
|
+
const answers = m.flow.nodes.filter((n) => n.kind === 'end' && n.answer && r.nodes[n.id]?.ran);
|
|
681
|
+
if (answers.length) parts.push(`answered ${answers.map((n) => `${n.text} ${times(r.nodes[n.id].ran)}${pct(r.nodes[n.id].ran, total) ? ` (${pct(r.nodes[n.id].ran, total)})` : ''}`).join(', ')}`);
|
|
682
|
+
const stopsSeen = Object.values(r.nodes).filter((n) => n.kind === 'stop').reduce((a, n) => a + (n.ran || 0), 0);
|
|
683
|
+
const crashes = Object.values(r.nodes).reduce((a, n) => a + (n.failed || []).reduce((b, e) => b + e.count, 0), 0);
|
|
684
|
+
const elsewhere = r.failures - stopsSeen - crashes;
|
|
685
|
+
if (elsewhere > 0) parts.push(`ended with an error ${times(elsewhere)} that is not one of its Stops${r.errors?.length ? ` (${[...new Set(r.errors.map((e) => e.error))].join(', ')})` : ''}${crashes ? '' : '; the evidence does not say at which step'}`);
|
|
686
|
+
if (r.timing) parts.push(`took ${ms(r.timing.avgMs)} on average, ${ms(r.timing.maxMs)} at the slowest`);
|
|
687
|
+
return parts.length ? `${parts.join('; ')}.` : '';
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
// ── Words: an error explained in Thunder, and the flow with its counts ──────────────────────────────────────────
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* Explain one stack trace in Thunder's words: the action or rule and the step where it failed, the error as seen,
|
|
694
|
+
* and, when the flow shows it, the answers that led there. Returns { matched, text, error, message, member, step,
|
|
695
|
+
* path, calledFrom, raisedInside, reason? }. `stack` may be the text or a parsed stack (parseStack).
|
|
696
|
+
*/
|
|
697
|
+
export function explainError(model, stack) {
|
|
698
|
+
const s = typeof stack === 'string' ? parseStack(stack) : stack;
|
|
699
|
+
if (!s) return { matched: false, reason: 'Thunder found no stack trace it can read (Python, JavaScript, Java, C#, or Go)', text: 'Thunder found no stack trace it can read in this text.' };
|
|
700
|
+
const findFile = fileMatcher(model);
|
|
701
|
+
const placed = [];
|
|
702
|
+
let reason = null;
|
|
703
|
+
for (const f of s.frames) {
|
|
704
|
+
if (f.internal || f.file == null) { placed.push({ frame: f }); continue; }
|
|
705
|
+
const got = findFile(f.file, f.className);
|
|
706
|
+
if (!got.file) { reason ??= got.reason; placed.push({ frame: f }); continue; }
|
|
707
|
+
const m = memberAt(model, got.file, f.line);
|
|
708
|
+
if (!m) { reason ??= `line ${f.line} of ${got.file} is not inside any action or rule Thunder read`; placed.push({ frame: f, file: got.file }); continue; }
|
|
709
|
+
placed.push({ frame: f, file: got.file, member: m, at: nodeAt(m, f.line, 'raise') });
|
|
710
|
+
}
|
|
711
|
+
const first = placed.findIndex((p) => p.member);
|
|
712
|
+
const errWords = `${s.error}${s.message ? `: ${s.message}` : ''}`;
|
|
713
|
+
if (first < 0) {
|
|
714
|
+
const top = s.frames.find((f) => !f.internal) ?? s.frames[0];
|
|
715
|
+
return { matched: false, error: s.error, message: s.message, language: s.language, reason: reason ?? 'none of its lines are in code Thunder read', text: `Thunder could not place this error on a flow: ${reason ?? 'none of its lines are in code Thunder read'}. ${errWords} (at ${top?.file ?? '?'}:${top?.line ?? '?'}).` };
|
|
716
|
+
}
|
|
717
|
+
const hit = placed[first];
|
|
718
|
+
const m = hit.member;
|
|
719
|
+
const raisedInside = placed.slice(0, first).filter((p) => p.frame.file && !p.frame.internal).map((p) => norm(p.frame.file).split('/').slice(-2).join('/'));
|
|
720
|
+
const step = hit.at?.node ?? null;
|
|
721
|
+
const when = step ? pathsOf(m.flow).find((p) => p.node.id === step.id)?.when ?? null : null;
|
|
722
|
+
const calledFrom = placed.slice(first + 1).filter((p) => p.member).map((p) => ({ member: { id: p.member.id, label: p.member.label }, step: p.at?.node ? { id: p.at.node.id, text: p.at.node.text, line: p.at.node.line } : null, line: p.frame.line, file: p.file }));
|
|
723
|
+
const isStop = step?.kind === 'stop';
|
|
724
|
+
const where = step ? `at “${step.text}” (line ${hit.frame.line}${hit.at.approximate ? `, part of the step that starts at line ${step.line}` : ''})` : `at line ${hit.frame.line}, which Thunder draws no step for`;
|
|
725
|
+
let text = `${isStop ? 'Stopped' : 'Failed'} in ${m.label}, ${where}: ${errWords.replace(/[.\s]+$/, '')}`;
|
|
726
|
+
if (raisedInside.length) text += ` (raised inside ${raisedInside[0]}, which this step called)`;
|
|
727
|
+
if (when && when.length) text += `; it got there because ${whenWords(when)}`;
|
|
728
|
+
else if (when && !when.length && step) text += '; no question comes before this step, so it happens every time';
|
|
729
|
+
text += '.';
|
|
730
|
+
if (isStop) text += ' This is one of the ways Thunder shows it can end.';
|
|
731
|
+
if (calledFrom.length) text += ` Called from ${calledFrom.slice(0, 3).map((c) => `${c.member.label}${c.step ? `, at “${c.step.text}”` : ''} (line ${c.line})`).join(', called from ')}${calledFrom.length > 3 ? `, and ${calledFrom.length - 3} more` : ''}.`;
|
|
732
|
+
if (s.causes?.length) text += ` It happened while handling ${s.causes.map((c) => c.error).join(', then ')}.`;
|
|
733
|
+
if (s.wrappedIn?.length) text += ` It reached the top as ${s.wrappedIn[0].error}.`;
|
|
734
|
+
return {
|
|
735
|
+
matched: true, text, error: s.error, message: s.message, language: s.language,
|
|
736
|
+
file: hit.file, line: hit.frame.line,
|
|
737
|
+
member: { id: m.id, label: m.label, name: m.name },
|
|
738
|
+
step: step ? { id: step.id, text: step.text, kind: step.kind, line: step.line ?? null, ...(hit.at.approximate ? { approximate: true } : {}) } : null,
|
|
739
|
+
path: when && when.length ? when : null,
|
|
740
|
+
calledFrom, raisedInside,
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/** The words after a node in the overlay's Thunder Text: "(ran 1,204×: Yes 1,167, No 37)". */
|
|
745
|
+
export function nodeNote(nr) {
|
|
746
|
+
if (!nr) return '';
|
|
747
|
+
const bits = [];
|
|
748
|
+
if (nr.sides) {
|
|
749
|
+
const s = Object.entries(nr.sides).map(([k, v]) => `${k} ${countWords(v)}`).join(', ');
|
|
750
|
+
bits.push(nr.kind === 'loop' ? `${nr.sides['For each'] ? `went round ${countWords(nr.sides['For each'])}×` : ''}${nr.sides['For each'] && nr.sides.Done ? '; ' : ''}${nr.sides.Done ? `left ${countWords(nr.sides.Done)}×` : ''}` : `ran ${countWords(nr.ran)}×: ${s}`);
|
|
751
|
+
} else if (nr.kind === 'stop' && nr.ran) bits.push(`happened ${countWords(nr.ran)}×${nr.errors?.length ? `: ${nr.errors.map((e) => `${e.error}${e.message ? ` "${quoteText(e.message, 50)}"` : ''} ${countWords(e.count)}`).join(', ')}` : ''}`);
|
|
752
|
+
else if (nr.kind === 'end' && nr.ran) bits.push(`reached ${countWords(nr.ran)}×`);
|
|
753
|
+
else if (nr.kind === 'decision' && nr.ran) bits.push(`caught ${countWords(nr.ran)}×`);
|
|
754
|
+
else if (nr.ran) bits.push(nr.levels ? `logged ${countWords(nr.ran)}×: ${Object.entries(nr.levels).map(([k, v]) => `${k} ${countWords(v)}`).join(', ')}` : `ran ${countWords(nr.ran)}×`);
|
|
755
|
+
if (nr.failed?.length) bits.push(`failed ${nr.failed.map((e) => `${countWords(e.count)}×: ${e.error}${e.message ? ` "${quoteText(e.message, 50)}"` : ''}`).join(', ')}`);
|
|
756
|
+
if (nr.approximate) bits.push('placed on the step that starts above');
|
|
757
|
+
return bits.length ? ` (${bits.join('; ')})` : '';
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* The overlay in Thunder Text: each member with evidence (most calls first), its flow with counts after the nodes,
|
|
762
|
+
* then what could not be matched. Options: member (an id or name: only that one), top (how many members; default 10).
|
|
763
|
+
*/
|
|
764
|
+
export function runtimeText(model, overlay, { member = null, top = 10 } = {}) {
|
|
765
|
+
const out = [];
|
|
766
|
+
let list = Object.values(overlay.members).sort((a, b) => (b.calls + b.failures) - (a.calls + a.failures) || Object.keys(b.nodes).length - Object.keys(a.nodes).length);
|
|
767
|
+
if (member) list = list.filter((r) => r.id === member || r.name === member || r.label === member);
|
|
768
|
+
const shown = list.slice(0, top);
|
|
769
|
+
for (const r of shown) {
|
|
770
|
+
const m = model.members.find((x) => x.id === r.id);
|
|
771
|
+
out.push(`${r.file}:${r.lines[0]} ${r.says || 'Seen in the evidence.'}`);
|
|
772
|
+
out.push(modelText(model, m.id, { note: (n) => nodeNote(r.nodes[n.id]) }).replace(/\n$/, ''));
|
|
773
|
+
// "Done" (reaching the end) is not a drawn step in the text; its count goes after the flow.
|
|
774
|
+
for (const n of m.flow.nodes) if (n.kind === 'end' && n.line == null && r.nodes[n.id]?.ran) out.push(` - ${n.text}${nodeNote(r.nodes[n.id])}`);
|
|
775
|
+
if (r.hotPath.length > 1) out.push(` Most often: ${r.hotPath.map((h) => (h.side ? `${h.text.replace(/\?$/, '')}? ${h.side}` : h.text)).join(' → ')}`);
|
|
776
|
+
out.push('');
|
|
777
|
+
}
|
|
778
|
+
if (list.length > shown.length) out.push(`(${list.length - shown.length} more with evidence; pass --member <name> or --top <n>)`, '');
|
|
779
|
+
if (!list.length) out.push(member ? `No evidence for ${member}.` : 'No evidence could be placed on any flow.', '');
|
|
780
|
+
if (overlay.notMatched.length) {
|
|
781
|
+
const total = overlay.notMatched.reduce((a, x) => a + x.count, 0);
|
|
782
|
+
out.push(`Not matched (${countWords(total)}): evidence Thunder could not place on a step, and why`);
|
|
783
|
+
for (const x of [...overlay.notMatched].sort((a, b) => b.count - a.count).slice(0, 12)) out.push(` - ${x.what}${x.count > 1 ? ` (${countWords(x.count)}×)` : ''}: ${x.reason}`);
|
|
784
|
+
if (overlay.notMatched.length > 12) out.push(` … and ${overlay.notMatched.length - 12} more (see --json)`);
|
|
785
|
+
}
|
|
786
|
+
return out.join('\n').replace(/\n+$/, '\n');
|
|
787
|
+
}
|