@skillstech/thunderlang 1.4.3 → 1.5.0

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/src/trace.mjs CHANGED
@@ -19,10 +19,11 @@
19
19
  import { thunderModel } from './model.mjs';
20
20
  import { serviceFacts, segsMatch, GENERIC_PATHS } from './system.mjs';
21
21
  import { sha256 } from './hash.mjs';
22
+ import { packagesIn, settingsIn, tablesIn, isManifest, LOCAL_VERSION, isGenericSetting, DEFINES } from './trace-data.mjs';
22
23
 
23
24
  export const TRACE_SCHEMA = 'thunder-trace-v1';
24
25
  export const TRACE_IMPACT_SCHEMA = 'thunder-trace-impact-v1';
25
- export const TRACE_RULES_VERSION = '1';
26
+ export const TRACE_RULES_VERSION = '2';
26
27
 
27
28
  /** Test and fixture files: what they declare or call is pretend, so it is left out of the links. */
28
29
  export function isTestFile(file) {
@@ -56,14 +57,45 @@ export function buildTrace(repos) {
56
57
  const facts = serviceFacts(model, r.files);
57
58
  // Test code is not how services talk to each other: a test's fake route or request is never a link.
58
59
  const real = (x) => !isTestFile(x.file);
59
- const testItems = ['routes', 'listeners', 'requests', 'publishes'].reduce((n, k) => n + facts[k].filter((x) => !real(x)).length, 0);
60
+ const data = dataFacts(r.files, model);
61
+ const testItems = ['routes', 'listeners', 'requests', 'publishes'].reduce((n, k) => n + facts[k].filter((x) => !real(x)).length, 0) + data.testItems;
60
62
  return { name: r.name, sha: r.sha ?? null, dirty: !!r.dirty, files: model.files.length, languages: [...new Set(model.files.map((f) => f.language))].sort(),
61
- routes: facts.routes.filter(real), listeners: facts.listeners.filter(real), requests: facts.requests.filter(real), publishes: facts.publishes.filter(real), unknown: facts.unknown.filter(real), testItems };
63
+ routes: facts.routes.filter(real), listeners: facts.listeners.filter(real), requests: facts.requests.filter(real), publishes: facts.publishes.filter(real), unknown: facts.unknown.filter(real),
64
+ packages: data.packages, settings: data.settings, tables: data.tables, testItems };
62
65
  });
63
66
  return linkRepos(read);
64
67
  }
65
68
 
66
- /** Link already-read repositories (each with routes, listeners, requests, publishes). Pure and deterministic. */
69
+ /** The member (function, method) whose lines hold `line` in `file`: the innermost one, or none. */
70
+ function memberAt(model, file, line) {
71
+ let best = null;
72
+ for (const m of model.members) {
73
+ if (m.file !== file || !m.lines || line < m.lines[0] || line > m.lines[1]) continue;
74
+ if (!best || m.lines[1] - m.lines[0] < best.lines[1] - best.lines[0]) best = m;
75
+ }
76
+ return best ? { member: best.id, label: best.label } : { member: null, label: file.split('/').pop() };
77
+ }
78
+
79
+ /**
80
+ * Packages, settings, and tables of one repository (trace-data.mjs), each placed in the member it is written in.
81
+ * Test and fixture files are left out and counted, as for routes.
82
+ */
83
+ export function dataFacts(files, model) {
84
+ const real = (x) => !isTestFile(x.file);
85
+ const pk = packagesIn(files), settings = settingsIn(files), tables = tablesIn(files);
86
+ const place = (x) => ({ ...x, ...memberAt(model, x.file, x.line) });
87
+ const manifest = (x) => ({ ...x, member: null, label: x.file });
88
+ const testItems = [...pk.publishes, ...pk.depends, ...settings, ...tables].filter((x) => !real(x)).length;
89
+ return {
90
+ packages: { publishes: pk.publishes.filter(real).map(manifest), depends: pk.depends.filter(real).map(manifest) },
91
+ settings: settings.filter(real).map(place),
92
+ tables: tables.filter(real).map((t) => (/\.sql$/i.test(t.file) ? { ...t, member: null, label: t.file } : place(t))),
93
+ testItems,
94
+ };
95
+ }
96
+ export { isManifest };
97
+
98
+ /** Link already-read repositories (each with routes, listeners, requests, publishes; packages, settings, tables). Pure and deterministic. */
67
99
  export function linkRepos(read) {
68
100
  const links = [], notLinked = [];
69
101
  for (const repo of read) {
@@ -103,16 +135,75 @@ export function linkRepos(read) {
103
135
  if (!found) notLinked.push({ kind: 'event', call: `event ${p.event}`, from, why: `no listener Thunder read in the traced repositories waits for ${p.event}` });
104
136
  }
105
137
  }
106
- const sortKey = (l) => `${l.from.cite}|${l.to?.cite ?? ''}|${l.kind}|${l.call ?? l.event ?? l.path ?? ''}`;
138
+ // Packages: a dependency on a package exactly one other traced repository publishes (package.json name, go.mod
139
+ // module, .csproj PackageId). A package the repository publishes itself is its own, not a link.
140
+ const pk = (r) => r.packages ?? { publishes: [], depends: [] };
141
+ const publishers = new Map();
142
+ for (const repo of read) for (const p of pk(repo).publishes) (publishers.get(p.package) ?? publishers.set(p.package, []).get(p.package)).push({ repo, p });
143
+ for (const repo of read) {
144
+ const own = new Set(pk(repo).publishes.map((p) => p.package));
145
+ for (const d of pk(repo).depends) {
146
+ if (own.has(d.package)) continue;
147
+ const pubs = (publishers.get(d.package) ?? []).filter((x) => x.repo !== repo);
148
+ const byRepo = [...new Map(pubs.map((x) => [x.repo.name, x])).values()];
149
+ if (byRepo.length === 1) links.push({ kind: 'package', package: d.package, version: d.version ?? null, ...(d.dev ? { dev: true } : {}), from: end(repo, d), to: end(byRepo[0].repo, byRepo[0].p) });
150
+ else if (byRepo.length > 1) notLinked.push({ kind: 'package', call: `package ${d.package}`, from: end(repo, d), why: `${byRepo.length} repositories publish ${d.package} (${byRepo.map((x) => x.repo.name).join(', ')}), so none is chosen` });
151
+ }
152
+ }
153
+ // Tables: code or SQL that reads or writes a table exactly one other traced repository creates (CREATE TABLE in its
154
+ // SQL). A repository that creates the table itself is using its own.
155
+ const tb = (r) => r.tables ?? [];
156
+ const creators = new Map();
157
+ for (const repo of read) for (const t of tb(repo)) if (t.access === 'creates') (creators.get(t.table) ?? creators.set(t.table, []).get(t.table)).push({ repo, t });
158
+ for (const repo of read) {
159
+ const own = new Set(tb(repo).filter((t) => t.access === 'creates').map((t) => t.table));
160
+ for (const t of tb(repo)) {
161
+ if (DEFINES.has(t.access) || own.has(t.table)) continue;
162
+ const owners = [...new Map((creators.get(t.table) ?? []).filter((x) => x.repo !== repo).map((x) => [x.repo.name, x])).values()];
163
+ if (owners.length === 1) links.push({ kind: 'data', table: t.table, access: t.access, from: end(repo, t), to: end(owners[0].repo, owners[0].t) });
164
+ else if (owners.length > 1) notLinked.push({ kind: 'data', call: `table ${t.name}`, from: end(repo, t), why: `${owners.length} repositories create a table named ${t.table} (${owners.map((x) => x.repo.name).join(', ')}), so none is chosen` });
165
+ }
166
+ }
167
+ const shared = sharedFacts(read);
168
+
169
+ const sortKey = (l) => `${l.from.cite}|${l.to?.cite ?? ''}|${l.kind}|${l.call ?? l.event ?? l.path ?? l.package ?? l.table ?? ''}`;
107
170
  // (A step inside a callback is read for the callback and for the function around it: the same call, once.)
108
171
  const once = (xs) => { const seen = new Set(); return xs.filter((x) => { const k = sortKey(x); if (seen.has(k)) return false; seen.add(k); return true; }); };
109
172
  links.splice(0, links.length, ...once(links).sort((a, b) => sortKey(a).localeCompare(sortKey(b))));
110
173
  notLinked.splice(0, notLinked.length, ...once(notLinked).sort((a, b) => sortKey(a).localeCompare(sortKey(b))));
111
- const repos = read.map(({ unknown, ...r }) => ({ ...r, unknown: unknown ?? [] }));
112
- const body = { schema: TRACE_SCHEMA, rulesVersion: TRACE_RULES_VERSION, repos, links, notLinked };
174
+ const repos = read.map(({ unknown, ...r }) => ({ ...r, unknown: unknown ?? [], packages: pk(r), settings: r.settings ?? [], tables: tb(r) }));
175
+ const body = { schema: TRACE_SCHEMA, rulesVersion: TRACE_RULES_VERSION, repos, links, notLinked, shared };
113
176
  return { ...body, fingerprint: sha256(JSON.stringify(body)) };
114
177
  }
115
178
 
179
+ /**
180
+ * What more than one repository has in common without one depending on the other: the same setting key read, the
181
+ * same table used, the same package at different versions. Facts worth knowing, not links.
182
+ * settings: [{ key, readers: [End] }] keys every service reads for itself or the platform sets (PORT, NODE_ENV, VERCEL_ENV, …) are left out
183
+ * tables: [{ table, users: [End + access] }]
184
+ * drift: [{ package, versions: [{ version, at: [End] }] }] versions that name a place (workspace:, file:) and peer ranges are left out
185
+ */
186
+ function sharedFacts(read) {
187
+ const group = (items) => { const m = new Map(); for (const [k, repo, x] of items) (m.get(k) ?? m.set(k, []).get(k)).push({ repo, x }); return m; };
188
+ const manyRepos = (xs) => new Set(xs.map((y) => y.repo.name)).size > 1;
189
+ const byKey = (a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0);
190
+ const settings = [...group(read.flatMap((r) => (r.settings ?? []).filter((x) => !isGenericSetting(x.key)).map((x) => [x.key, r, x]))).entries()]
191
+ .filter(([, xs]) => manyRepos(xs)).sort(byKey).map(([key, xs]) => ({ key, readers: xs.map(({ repo, x }) => end(repo, x)) }));
192
+ const tables = [...group(read.flatMap((r) => (r.tables ?? []).map((x) => [x.table, r, x]))).entries()]
193
+ .filter(([, xs]) => manyRepos(xs)).sort(byKey).map(([table, xs]) => ({ table, users: xs.map(({ repo, x }) => ({ ...end(repo, x), access: x.access })) }));
194
+ // Drift is between repositories: each repository's set of versions for the package differs from another's. Two
195
+ // manifests in one repository asking ^9 and ^9.39.5, as every other repository does too, is not drift. Peer
196
+ // dependency ranges (>=19) say what a package accepts, not what is used, so they are left out.
197
+ const drift = [];
198
+ for (const [name, xs] of [...group(read.flatMap((r) => (r.packages?.depends ?? []).filter((d) => d.version && !d.peer && !LOCAL_VERSION.test(d.version)).map((d) => [d.package, r, d]))).entries()].sort(byKey)) {
199
+ const versions = group(xs.map(({ repo, x }) => [x.version, repo, x]));
200
+ const perRepo = new Set([...group(xs.map(({ repo, x }) => [repo.name, repo, x])).values()].map((ys) => [...new Set(ys.map((y) => y.x.version))].sort().join('|')));
201
+ if (!manyRepos(xs) || versions.size < 2 || perRepo.size < 2) continue;
202
+ drift.push({ package: name, versions: [...versions.entries()].sort(byKey).map(([version, ys]) => ({ version, at: ys.map(({ repo, x }) => end(repo, x)) })) });
203
+ }
204
+ return { settings, tables, drift };
205
+ }
206
+
116
207
  const methodOk = (route, req) => route === req || route === 'ANY' || route === 'ALL';
117
208
  const within = (line, ranges) => line != null && ranges.some(([a, b]) => line >= a && line <= b);
118
209
  const overlaps = (lines, ranges) => !!lines && ranges.some(([a, b]) => a <= lines[1] && b >= lines[0]);
@@ -129,14 +220,17 @@ export function traceImpact(trace, changes) {
129
220
  const byName = new Map(trace.repos.map((r) => [r.name, r]));
130
221
  const findings = [];
131
222
  const touched = [];
223
+ const packagesDone = new Set();
224
+ // Every place outside `repo` that reads or writes `table` (for a dropped or removed table, linked or not).
225
+ const usesElsewhere = (repo, table) => trace.repos.filter((o) => o.name !== repo.name).flatMap((o) => (o.tables ?? []).filter((t) => t.table === table && !DEFINES.has(t.access)).map((t) => ({ ...end(o, t), how: `${t.access} ${t.name}` })));
132
226
  for (const c of changes) {
133
227
  const repo = byName.get(c.repo);
134
228
  if (!repo) continue;
229
+ const found = findings.length;
135
230
  const hit = (x) => x.file === c.file && (c.status === 'added' || overlaps(x.lines, c.ranges) || within(x.line, c.ranges));
136
231
  const routes = repo.routes.filter(hit), listeners = repo.listeners.filter(hit);
137
232
  const requests = repo.requests.filter((x) => x.file === c.file && (c.status === 'added' || within(x.line, c.ranges) || overlaps(x.lines, c.ranges)));
138
233
  const publishes = repo.publishes.filter((x) => x.file === c.file && (c.status === 'added' || within(x.line, c.ranges) || overlaps(x.lines, c.ranges)));
139
- if (routes.length || listeners.length || requests.length || publishes.length || c.before) touched.push({ repo: c.repo, file: c.file, status: c.status });
140
234
 
141
235
  for (const r of routes) {
142
236
  const callers = trace.links.filter((l) => l.kind === 'http' && l.to.repo === repo.name && l.to.member === r.member);
@@ -154,6 +248,40 @@ export function traceImpact(trace, changes) {
154
248
  const senders = trace.links.filter((l) => l.kind === 'event' && l.to.repo === repo.name && l.to.member === x.member);
155
249
  findings.push({ risk: 'low', kind: 'listener-changed', says: `the handler for ${x.event} changed${senders.length ? `; ${count(senders.length, 'publisher')} in other repositories ${senders.length === 1 ? 'sends' : 'send'} it` : ''}`, at: end(repo, x), dependents: senders.map((l) => ({ ...l.from, how: `publishes ${x.event}` })) });
156
250
  }
251
+ // Tables: a changed definition reaches every repository that uses the table; a changed query depends on the
252
+ // repository that creates the table.
253
+ const lineHit = (x) => x.file === c.file && (c.status === 'added' || overlaps(x.lines, c.ranges) || within(x.line, c.ranges));
254
+ const tablesHere = (repo.tables ?? []).filter(lineHit);
255
+ for (const t of tablesHere.filter((x) => x.access === 'creates' || x.access === 'alters')) {
256
+ const users = trace.links.filter((l) => l.kind === 'data' && l.to.repo === repo.name && l.table === t.table);
257
+ findings.push({ risk: users.length ? 'high' : 'low', kind: 'table-changed', says: users.length ? `the ${t.name} table's ${t.access === 'creates' ? 'definition' : 'ALTER TABLE'} changed, and ${count(users.length, 'place')} in other repositories ${users.length === 1 ? 'uses' : 'use'} the table` : `the ${t.name} table's ${t.access === 'creates' ? 'definition' : 'ALTER TABLE'} changed; no traced repository uses the table`, at: end(repo, t), dependents: users.map((l) => ({ ...l.from, how: `${l.access} ${t.table}` })) });
258
+ }
259
+ for (const t of tablesHere.filter((x) => x.access === 'drops')) {
260
+ const users = usesElsewhere(repo, t.table);
261
+ findings.push({ risk: users.length ? 'high' : 'low', kind: 'table-dropped', says: users.length ? `DROP TABLE ${t.name}, but ${count(users.length, 'place')} in other repositories still ${users.length === 1 ? 'uses' : 'use'} it` : `DROP TABLE ${t.name}; no traced repository uses it`, at: end(repo, t), dependents: users });
262
+ }
263
+ for (const t of tablesHere.filter((x) => !DEFINES.has(x.access))) {
264
+ const l = trace.links.find((x) => x.kind === 'data' && x.from.repo === repo.name && x.from.file === t.file && x.from.line === t.line && x.table === t.table);
265
+ if (l) findings.push({ risk: 'medium', kind: 'query-changed', says: `SQL that ${t.access} ${t.name} changed; the table is created in ${l.to.repo}`, at: end(repo, t), dependents: [{ ...l.to, how: `creates ${t.table}` }] });
266
+ }
267
+ // Settings: a key other repositories read too, so a new name or value has to reach them as well.
268
+ for (const k of (repo.settings ?? []).filter(lineHit)) {
269
+ const others = (trace.shared?.settings?.find((x) => x.key === k.key)?.readers ?? []).filter((r) => r.repo !== repo.name);
270
+ if (others.length) findings.push({ risk: 'low', kind: 'setting-changed', says: `code that reads the setting ${k.key} changed; ${count(new Set(others.map((o) => o.repo)).size, 'other repository')} ${new Set(others.map((o) => o.repo)).size === 1 ? 'reads' : 'read'} it too`, at: end(repo, k), dependents: others.map((o) => ({ ...o, how: `reads ${k.key}` })) });
271
+ }
272
+ // A package this repository publishes: code inside its folder changed, so the repositories that depend on it get
273
+ // the change when they take the new version. Once per package.
274
+ if (!isTestFile(c.file)) {
275
+ for (const p of repo.packages?.publishes ?? []) {
276
+ if (p.root && c.file !== p.root && !c.file.startsWith(`${p.root}/`)) continue;
277
+ const key = `${repo.name}|${p.package}`;
278
+ if (packagesDone.has(key)) continue;
279
+ const deps = trace.links.filter((l) => l.kind === 'package' && l.to.repo === repo.name && l.package === p.package);
280
+ if (!deps.length) continue;
281
+ packagesDone.add(key);
282
+ findings.push({ risk: 'medium', kind: 'package-changed', says: `${p.package} is published from here, and ${count(new Set(deps.map((d) => d.from.repo)).size, 'other repository')} ${new Set(deps.map((d) => d.from.repo)).size === 1 ? 'depends' : 'depend'} on it; the change reaches them when they take the new version`, at: end(repo, p), dependents: deps.map((l) => ({ ...l.from, how: `depends on ${p.package}${l.version ? ` ${l.version}` : ''}` })) });
283
+ }
284
+ }
157
285
  // Removed: what the file offered before and no longer offers.
158
286
  if (c.before) {
159
287
  const nowRoutes = new Set(repo.routes.filter((r) => r.file === c.file).map((r) => `${r.method} ${r.path}`));
@@ -173,7 +301,14 @@ export function traceImpact(trace, changes) {
173
301
  for (const other of trace.repos) if (other.name !== repo.name) for (const l of other.listeners) if (l.event === p.event) ls.push({ ...end(other, l), how: `waits for ${p.event}` });
174
302
  findings.push({ risk: ls.length ? 'high' : 'low', kind: 'event-removed', says: ls.length ? `${p.event} is no longer published here, but ${count(ls.length, 'listener')} in other repositories ${ls.length === 1 ? 'waits' : 'wait'} for it` : `${p.event} is no longer published here; nothing else listens for it`, at: { repo: repo.name, file: c.file, line: p.line, label: p.label, cite: `${cite(repo, p)} (before the change)` }, dependents: ls });
175
303
  }
304
+ const createsNow = new Set((repo.tables ?? []).filter((t) => t.access === 'creates').map((t) => t.table));
305
+ for (const t of (c.before.tables || []).filter((x) => x.access === 'creates')) {
306
+ if (createsNow.has(t.table)) continue;
307
+ const users = usesElsewhere(repo, t.table);
308
+ findings.push({ risk: users.length ? 'high' : 'low', kind: 'table-removed', says: users.length ? `the ${t.name} table is no longer created here, but ${count(users.length, 'place')} in other repositories still ${users.length === 1 ? 'uses' : 'use'} it` : `the ${t.name} table is no longer created here; no traced repository uses it`, at: { repo: repo.name, file: c.file, line: t.line, label: t.file, cite: `${cite(repo, t)} (before the change)` }, dependents: users });
309
+ }
176
310
  }
311
+ if (findings.length > found || routes.length || listeners.length || requests.length || publishes.length || c.before) touched.push({ repo: c.repo, file: c.file, status: c.status });
177
312
  }
178
313
  const rank = { high: 0, medium: 1, low: 2 };
179
314
  findings.sort((a, b) => rank[a.risk] - rank[b.risk] || a.at.cite.localeCompare(b.at.cite));
@@ -191,10 +326,14 @@ export function traceImpactOf(trace, term) {
191
326
  const t = String(term).trim();
192
327
  const changes = [];
193
328
  const matched = [];
194
- const http = t.match(/^(?:(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS|ANY)\s+)?(\/\S*)$/i);
195
- const place = t.match(/^([^:\s]+):([^:\s]+?)(?::(\d+))?$/);
329
+ const pre = t.match(/^(table|setting|package):\s*(.+)$/i);
330
+ const data = pre && !byNameOf(trace, pre[1]) ? { kind: pre[1].toLowerCase(), name: pre[2].trim() } : null;
331
+ const http = !data && t.match(/^(?:(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS|ANY)\s+)?(\/\S*)$/i);
332
+ const place = !data && t.match(/^([^:\s]+):([^:\s]+?)(?::(\d+))?$/);
196
333
  for (const repo of trace.repos) {
197
- if (http) {
334
+ if (data) {
335
+ dataChanges(repo, data, changes, matched);
336
+ } else if (http) {
198
337
  const segs = http[2].split('/').filter(Boolean).map((s) => (/^(:\w+|\{[^}]+\}|\*)$/.test(s) ? '*' : s));
199
338
  for (const r of repo.routes) if ((!http[1] || methodOk(r.method, http[1].toUpperCase())) && pathMatch(r.segs, segs) >= 0 && r.segs.length === segs.length) { changes.push({ repo: repo.name, file: r.file, ranges: [r.lines ?? [r.line, r.line]], status: 'changed' }); matched.push(`${repo.name}: route ${r.method} ${r.path}`); }
200
339
  } else if (place && byNameOf(trace, place[1])) {
@@ -207,11 +346,37 @@ export function traceImpactOf(trace, term) {
207
346
  for (const l of repo.listeners) if (l.event === t) { changes.push({ repo: repo.name, file: l.file, ranges: [l.lines ?? [l.line, l.line]], status: 'changed' }); matched.push(`${repo.name}: listens for ${l.event}`); }
208
347
  }
209
348
  }
349
+ // A plain name that is no route, event, or file: a table, setting, or package by that name.
350
+ let asked = data;
351
+ if (!data && !http && !(place && byNameOf(trace, place[1])) && !changes.length) {
352
+ for (const kind of ['table', 'setting', 'package']) {
353
+ for (const repo of trace.repos) dataChanges(repo, { kind, name: t }, changes, matched);
354
+ if (changes.length) { asked = { kind, name: t }; break; }
355
+ }
356
+ }
210
357
  const r = traceImpact(trace, changes);
211
- // An event name asks about that event only, not the route whose handler happens to publish it.
212
- if (!http && !(place && byNameOf(trace, place[1]))) r.findings = r.findings.filter((f) => /^(event-|listener-)/.test(f.kind) && f.says.includes(t));
358
+ if (asked) {
359
+ // A table, setting, or package asks about that one thing, not whatever else sits on the same lines.
360
+ const kinds = { table: /^(table-|query-)/, setting: /^setting-/, package: /^package-/ }[asked.kind];
361
+ r.findings = r.findings.filter((f) => kinds.test(f.kind));
362
+ } else if (!http && !(place && byNameOf(trace, place[1]))) {
363
+ // An event name asks about that event only, not the route whose handler happens to publish it.
364
+ r.findings = r.findings.filter((f) => /^(event-|listener-)/.test(f.kind) && f.says.includes(t));
365
+ }
213
366
  return { ...r, term: t, matched };
214
367
  }
368
+
369
+ /** The places in `repo` that define a table, read a setting, or publish a package named `ask.name`, as changes. */
370
+ function dataChanges(repo, ask, changes, matched) {
371
+ if (ask.kind === 'table') {
372
+ const want = ask.name.replace(/[`"[\]]/g, '').split('.').pop().toLowerCase();
373
+ for (const x of (repo.tables ?? []).filter((y) => y.table === want && (y.access === 'creates' || y.access === 'alters'))) { changes.push({ repo: repo.name, file: x.file, ranges: [[x.line, x.line]], status: 'changed' }); matched.push(`${repo.name}: ${x.access} table ${x.name}`); }
374
+ } else if (ask.kind === 'setting') {
375
+ for (const x of (repo.settings ?? []).filter((y) => y.key === ask.name)) { changes.push({ repo: repo.name, file: x.file, ranges: [[x.line, x.line]], status: 'changed' }); matched.push(`${repo.name}: reads setting ${x.key}`); }
376
+ } else {
377
+ for (const x of (repo.packages?.publishes ?? []).filter((y) => y.package === ask.name)) { changes.push({ repo: repo.name, file: x.file, ranges: [[x.line, x.line]], status: 'changed' }); matched.push(`${repo.name}: publishes package ${x.package}`); }
378
+ }
379
+ }
215
380
  const byNameOf = (trace, name) => trace.repos.some((r) => r.name === name);
216
381
 
217
382
  /** The trace as text, for the terminal. */
@@ -222,16 +387,42 @@ export function renderTrace(trace, { limit = 15 } = {}) {
222
387
  for (const r of trace.repos) {
223
388
  out.push(`${r.name} @ ${short(r.sha)}${r.dirty ? ' (with uncommitted changes)' : ''}: ${r.files} files${r.languages.length ? ` (${r.languages.join(', ')})` : ''}`);
224
389
  out.push(` offers ${count(r.routes.length, 'route')} and listens for ${count(new Set(r.listeners.map((l) => l.event)).size, 'event')}; sends ${count(r.requests.length, 'request')} and publishes ${count(new Set(r.publishes.map((p) => p.event)).size, 'event')}`);
390
+ const tables = r.tables ?? [], pk = r.packages ?? { publishes: [], depends: [] };
391
+ const made = new Set(tables.filter((t) => t.access === 'creates').map((t) => t.table)), used = new Set(tables.filter((t) => !DEFINES.has(t.access)).map((t) => t.table));
392
+ out.push(` creates ${count(made.size, 'table')} and uses ${count(used.size, 'table')}; reads ${count(new Set((r.settings ?? []).map((k) => k.key)).size, 'setting')}; publishes ${count(new Set(pk.publishes.map((p) => p.package)).size, 'package')} and depends on ${count(new Set(pk.depends.map((d) => d.package)).size, 'package')}`);
225
393
  }
226
394
  if (trace.links.length) {
227
395
  out.push('', 'Links between repositories');
228
396
  for (const l of trace.links) {
229
397
  out.push(l.kind === 'http'
230
398
  ? ` ${l.from.repo} → ${l.to.repo}: ${l.from.label} sends ${l.method} ${l.path}, handled by ${l.to.label}${l.base === 'computed' ? ' (matched by path; the base address is set when it runs)' : ''}`
231
- : ` ${l.from.repo} → ${l.to.repo}: ${l.from.label} publishes ${l.event}, handled by ${l.to.label}`);
399
+ : l.kind === 'event' ? ` ${l.from.repo} → ${l.to.repo}: ${l.from.label} publishes ${l.event}, handled by ${l.to.label}`
400
+ : l.kind === 'package' ? ` ${l.from.repo} → ${l.to.repo}: depends on the package ${l.package}${l.version ? ` ${l.version}` : ''}${l.dev ? ' (for development)' : ''}, published by ${l.to.repo}`
401
+ : ` ${l.from.repo} → ${l.to.repo}: ${l.from.label} ${l.access} the ${l.table} table, created by ${l.to.repo}`);
232
402
  out.push(` ${l.from.cite} → ${l.to.cite}`);
233
403
  }
234
404
  }
405
+ const sh = trace.shared ?? { settings: [], tables: [], drift: [] };
406
+ const repoList = (ends) => [...new Set(ends.map((e) => e.repo))].join(', ');
407
+ if (sh.tables.length) {
408
+ out.push('', `Tables used by more than one repository: ${sh.tables.length}`);
409
+ for (const x of sh.tables.slice(0, limit)) {
410
+ out.push(` ${x.table}: ${repoList(x.users)}`);
411
+ for (const u of x.users.slice(0, 4)) out.push(` ${u.access.padEnd(7)} ${u.cite}`);
412
+ if (x.users.length > 4) out.push(` … and ${x.users.length - 4} more`);
413
+ }
414
+ if (sh.tables.length > limit) out.push(` … and ${sh.tables.length - limit} more (all of them with --json)`);
415
+ }
416
+ if (sh.settings.length) {
417
+ out.push('', `Settings read by more than one repository: ${sh.settings.length} (a new name or value has to reach each one)`);
418
+ for (const x of sh.settings.slice(0, limit)) out.push(` ${x.key}: ${repoList(x.readers)} (${x.readers.slice(0, 3).map((e) => e.cite).join(', ')}${x.readers.length > 3 ? ', …' : ''})`);
419
+ if (sh.settings.length > limit) out.push(` … and ${sh.settings.length - limit} more (all of them with --json)`);
420
+ }
421
+ if (sh.drift.length) {
422
+ out.push('', `Packages at different versions: ${sh.drift.length}`);
423
+ for (const x of sh.drift.slice(0, limit)) out.push(` ${x.package}: ${x.versions.map((v) => `${v.version} in ${repoList(v.at)}`).join('; ')}`);
424
+ if (sh.drift.length > limit) out.push(` … and ${sh.drift.length - limit} more (all of them with --json)`);
425
+ }
235
426
  if (trace.notLinked.length) {
236
427
  const shown = trace.notLinked.slice(0, limit);
237
428
  out.push('', `Not linked: ${count(trace.notLinked.length, 'call')} worth knowing about, but not a fact`);
@@ -239,7 +430,7 @@ export function renderTrace(trace, { limit = 15 } = {}) {
239
430
  if (trace.notLinked.length > shown.length) out.push(` … and ${trace.notLinked.length - shown.length} more (all of them with --json)`);
240
431
  }
241
432
  const tests = trace.repos.reduce((n, r) => n + (r.testItems || 0), 0);
242
- if (tests) out.push('', `Left out: ${count(tests, 'route, request, or event')} in test and fixture files.`.replace('route, request, or events', 'routes, requests, and events'));
433
+ if (tests) out.push('', `Left out: ${tests} ${tests === 1 ? 'item' : 'items'} (routes, requests, events, tables, settings, packages) in test and fixture files.`);
243
434
  out.push('', `Fingerprint ${trace.fingerprint.slice(0, 19)}… (rules v${trace.rulesVersion}). Read from code; nothing was run.`);
244
435
  return out.join('\n');
245
436
  }
@@ -247,7 +438,7 @@ export function renderTrace(trace, { limit = 15 } = {}) {
247
438
  /** An impact result as text: high risk first, each finding with its citation and who depends on it. */
248
439
  export function renderImpact(r) {
249
440
  const out = [];
250
- if (r.matched && !r.matched.length) return `"${r.term}" matched nothing: no route, event, or file by that name in the traced repositories.`;
441
+ if (r.matched && !r.matched.length) return `"${r.term}" matched nothing: no route, event, file, table, setting, or package by that name in the traced repositories.`;
251
442
  const high = r.findings.filter((f) => f.risk === 'high').length;
252
443
  out.push(r.findings.length
253
444
  ? `Thunder Trace impact: ${count(r.findings.length, 'finding')}, ${high} high risk. Stops at the first hop.`