@skillstech/thunderlang 1.7.0 → 2.0.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.
Files changed (73) hide show
  1. package/CHANGELOG.md +153 -1
  2. package/README.md +15 -3
  3. package/dist/core.cjs +303 -41
  4. package/dist/index.cjs +9906 -1387
  5. package/dist/learn.cjs +1420 -93
  6. package/package.json +17 -15
  7. package/src/cli-main.mjs +381 -149
  8. package/src/core/ThunderRuntime.cs +22 -1
  9. package/src/core/check.mjs +320 -6
  10. package/src/core/cli.mjs +60 -8
  11. package/src/core/csharp-run.mjs +2 -2
  12. package/src/core/diagnostics.mjs +9 -0
  13. package/src/core/engine-core.mjs +148 -19
  14. package/src/core/engine-values.mjs +3 -0
  15. package/src/core/engine.mjs +131 -7
  16. package/src/core/from-python.mjs +1 -1
  17. package/src/core/go-run.mjs +5 -3
  18. package/src/core/index.mjs +12 -3
  19. package/src/core/java/thunder/ThunderRuntime.java +1 -0
  20. package/src/core/java-run.mjs +2 -2
  21. package/src/core/kind.mjs +6 -0
  22. package/src/core/lexer.mjs +16 -2
  23. package/src/core/parser.mjs +215 -29
  24. package/src/core/project-root.mjs +18 -0
  25. package/src/core/project.mjs +34 -16
  26. package/src/core/react-run.mjs +2 -2
  27. package/src/core/runtime-go/thunder_runtime.go +310 -2
  28. package/src/core/runtime-python.mjs +365 -3
  29. package/src/core/runtime-typescript.mjs +150 -5
  30. package/src/core/rust/thunder_runtime.rs +1 -0
  31. package/src/core/rust-run.mjs +2 -2
  32. package/src/core/serving.mjs +12 -0
  33. package/src/core/store-format.mjs +110 -0
  34. package/src/core/target-csharp.mjs +14 -0
  35. package/src/core/target-go.mjs +24 -3
  36. package/src/core/target-java.mjs +14 -0
  37. package/src/core/target-python.mjs +46 -5
  38. package/src/core/target-rust.mjs +14 -0
  39. package/src/core/target-typescript.mjs +76 -6
  40. package/src/core/typescript-run.mjs +6 -4
  41. package/src/core-changes-fs.mjs +28 -0
  42. package/src/core-changes.d.mts +67 -0
  43. package/src/core-changes.mjs +603 -0
  44. package/src/core-rules.mjs +146 -0
  45. package/src/core-services.d.mts +4 -0
  46. package/src/core-services.mjs +49 -0
  47. package/src/emit.mjs +3 -3
  48. package/src/evidence-words.d.mts +6 -0
  49. package/src/evidence-words.mjs +47 -0
  50. package/src/evidence.mjs +36 -1
  51. package/src/examples.d.mts +21 -0
  52. package/src/examples.mjs +175 -0
  53. package/src/index.d.ts +19 -0
  54. package/src/index.mjs +8 -1
  55. package/src/learn-lessons.mjs +1 -1
  56. package/src/lsp.mjs +110 -0
  57. package/src/mcp.mjs +47 -12
  58. package/src/obligations.d.mts +5 -0
  59. package/src/obligations.mjs +30 -2
  60. package/src/program.d.mts +7 -0
  61. package/src/program.mjs +7 -0
  62. package/src/read-repo-fs.mjs +2 -1
  63. package/src/review.mjs +75 -6
  64. package/src/same.mjs +16 -6
  65. package/src/scan-queries.mjs +2 -1
  66. package/src/symbols.d.mts +23 -0
  67. package/src/symbols.mjs +351 -0
  68. package/src/system.mjs +1 -1
  69. package/src/template.d.mts +31 -0
  70. package/src/template.mjs +189 -0
  71. package/src/trace-cli.mjs +8 -3
  72. package/src/trace-fs.mjs +29 -3
  73. package/src/trace.mjs +23 -7
package/src/trace-fs.mjs CHANGED
@@ -5,6 +5,8 @@ import { basename, resolve } from 'node:path';
5
5
  import { statSync } from 'node:fs';
6
6
  import { collectReadableFiles } from './read-repo-fs.mjs';
7
7
  import { thunderModel } from './model.mjs';
8
+ import { coreServiceFacts } from './core-services.mjs';
9
+ import { coreChanges } from './core-changes.mjs';
8
10
  import { serviceFacts } from './system.mjs';
9
11
  import { tablesIn } from './trace-data.mjs';
10
12
 
@@ -25,7 +27,9 @@ export function readRepoDir(dir, { maxFiles = 5000 } = {}) {
25
27
  const dirty = top ? !!git(root, ['status', '--porcelain'])?.trim() : false;
26
28
  // Manifests come too: package.json, go.mod, requirements.txt, and *.csproj say which packages a repository
27
29
  // publishes and depends on (they are not read as code).
28
- return { name: basename(root), root, sha, dirty, files: collectReadableFiles(root, { maxFiles, manifests: true, manifestNames: TRACE_MANIFESTS }) };
30
+ const files = collectReadableFiles(root, { maxFiles, manifests: true, manifestNames: TRACE_MANIFESTS, thunder: true });
31
+ // Thunder Core programs' endpoints and calls, read here so buildTrace (in the small /core entry) needs no compiler.
32
+ return { name: basename(root), root, sha, dirty, files, coreFacts: coreServiceFacts(files) };
29
33
  }
30
34
 
31
35
  /** The commit a branch started from: --base if given, else where HEAD left main/master (origin first), else HEAD. */
@@ -67,6 +71,7 @@ export function gitChanges(repo, base) {
67
71
  if (!byFile.has(file)) byFile.set(file, { file, ranges: [], status: 'added' });
68
72
  }
69
73
  const changes = [];
74
+ const oldSources = new Map();
70
75
  for (const c of byFile.values()) {
71
76
  if (!c.file.startsWith(prefix)) continue;
72
77
  const file = c.file.slice(prefix.length); // paths relative to the folder that was traced
@@ -74,10 +79,31 @@ export function gitChanges(repo, base) {
74
79
  if (c.status !== 'added') {
75
80
  const old = git(dir, ['show', `${b}:${c.file}`]);
76
81
  if (old != null) {
77
- try { const files = [{ path: file, source: old }]; const f = serviceFacts(thunderModel(files), files); before = { routes: f.routes, listeners: f.listeners, publishes: f.publishes, tables: tablesIn(files) }; } catch { before = null; }
82
+ const thunder = /\.thunder$/i.test(file);
83
+ try {
84
+ const files = [{ path: file, source: old }];
85
+ const f = thunder ? { ...coreServiceFacts(files), listeners: [], publishes: [] } : serviceFacts(thunderModel(files), files);
86
+ before = { routes: f.routes, listeners: f.listeners, publishes: f.publishes, tables: thunder ? [] : tablesIn(files) };
87
+ } catch { before = null; }
88
+ // A Thunder Core program's old text, so both versions can be compared (coreBehaviourOf).
89
+ if (thunder) oldSources.set(file, old);
78
90
  }
79
91
  }
80
92
  changes.push({ repo: repo.name, file, ranges: c.ranges, status: c.status, ...(before ? { before } : {}) });
81
93
  }
82
- return { base: b, changes };
94
+ return { base: b, changes, oldSources };
95
+ }
96
+
97
+ /**
98
+ * Both versions of a repository's Thunder Core programs compared (core-changes.mjs; this runs them on the Thunder
99
+ * engine), when any changed: the new side from the folder, the old side with the changed files as they were at the
100
+ * base. Null when no Thunder Core program changed.
101
+ */
102
+ export function coreBehaviourOf(repo, g) {
103
+ const changed = (g.changes ?? []).filter((c) => /\.thunder$/i.test(c.file));
104
+ if (!changed.length) return null;
105
+ const now = repo.files.filter((f) => /\.thunder$/i.test(f.path));
106
+ const gone = new Set(changed.map((c) => c.file));
107
+ const before = [...now.filter((f) => !gone.has(f.path)), ...changed.filter((c) => g.oldSources?.has(c.file)).map((c) => ({ path: c.file, source: g.oldSources.get(c.file) }))];
108
+ return coreChanges(before, now);
83
109
  }
package/src/trace.mjs CHANGED
@@ -49,18 +49,29 @@ const pathMatch = (a, b) => segsMatch(lower(a), lower(b));
49
49
  * Read each repository and link them. repos: [{ name, files: [{ path, source }], sha?, dirty? }].
50
50
  * Gives back thunder-trace-v1: { repos, links, notLinked, rules, fingerprint }.
51
51
  */
52
- export function buildTrace(repos) {
52
+ /**
53
+ * repos: [{ name, files, sha?, dirty?, coreFacts? }]. Thunder Core programs (.thunder) are read when their facts are
54
+ * given: each repository's `coreFacts`, or `core` (coreServiceFacts from core-services.mjs, which brings the Core
55
+ * compiler, so this module stays small without it). readRepoDir gives coreFacts.
56
+ */
57
+ export function buildTrace(repos, { core = null } = {}) {
53
58
  const names = new Set();
54
59
  const read = repos.map((r) => {
55
60
  if (!r.name || names.has(r.name)) throw new Error(`Each repository needs its own name; "${r.name}" is used twice.`);
56
61
  names.add(r.name);
57
- const model = thunderModel(r.files, { name: r.name });
58
- const facts = serviceFacts(model, r.files);
62
+ // .thunder files are read by core-services.mjs below, not as code in another language.
63
+ const other = r.files.filter((f) => !/\.thunder$/i.test(f.path));
64
+ const model = thunderModel(other, { name: r.name });
65
+ const facts = serviceFacts(model, other);
66
+ // Thunder Core programs (.thunder): their endpoints and calls to other services, read by core-services.mjs.
67
+ const cf = r.coreFacts ?? (core ? core(r.files) : { routes: [], requests: [], unknown: [], files: 0 });
68
+ for (const k of ['routes', 'requests', 'unknown']) facts[k] = [...facts[k], ...cf[k]];
69
+ const coreFiles = cf.files;
59
70
  // Test code is not how services talk to each other: a test's fake route or request is never a link.
60
71
  const real = (x) => !isTestFile(x.file);
61
- const data = dataFacts(r.files, model);
72
+ const data = dataFacts(other, model);
62
73
  const testItems = ['routes', 'listeners', 'requests', 'publishes'].reduce((n, k) => n + facts[k].filter((x) => !real(x)).length, 0) + data.testItems;
63
- 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(),
74
+ return { name: r.name, sha: r.sha ?? null, dirty: !!r.dirty, files: model.files.length + coreFiles, languages: [...new Set([...model.files.map((f) => f.language), ...(coreFiles ? ['thunder'] : [])])].sort(),
64
75
  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),
65
76
  packages: data.packages, settings: data.settings, tables: data.tables, testItems };
66
77
  });
@@ -229,13 +240,17 @@ export function traceImpact(trace, changes) {
229
240
  if (!repo) continue;
230
241
  const found = findings.length;
231
242
  const hit = (x) => x.file === c.file && (c.status === 'added' || overlaps(x.lines, c.ranges) || within(x.line, c.ranges));
232
- const routes = repo.routes.filter(hit), listeners = repo.listeners.filter(hit);
243
+ // A Thunder Core route is also changed when something it reaches changed (a helper, a kept value): `through` names it.
244
+ const reachHit = (x) => (x.reach ?? []).filter((y) => y.file === c.file && (c.status === 'added' || overlaps(y.lines, c.ranges))).map((y) => y.subject);
245
+ const routes = repo.routes.filter((x) => hit(x) || reachHit(x).length).map((x) => (hit(x) ? x : { ...x, through: reachHit(x) }));
246
+ const listeners = repo.listeners.filter(hit);
233
247
  const requests = repo.requests.filter((x) => x.file === c.file && (c.status === 'added' || within(x.line, c.ranges) || overlaps(x.lines, c.ranges)));
234
248
  const publishes = repo.publishes.filter((x) => x.file === c.file && (c.status === 'added' || within(x.line, c.ranges) || overlaps(x.lines, c.ranges)));
235
249
 
236
250
  for (const r of routes) {
237
251
  const callers = trace.links.filter((l) => l.kind === 'http' && l.to.repo === repo.name && l.to.member === r.member);
238
- findings.push({ risk: callers.length ? 'high' : 'low', kind: 'route-changed', says: callers.length ? `${r.method} ${r.path} changed, and ${count(callers.length, 'caller')} in other repositories ${callers.length === 1 ? 'depends' : 'depend'} on it` : `${r.method} ${r.path} changed; no traced repository calls it`, at: end(repo, r), dependents: callers.map((l) => ({ ...l.from, how: l.base === 'computed' ? 'matched by path; the base address is set when it runs' : l.base === 'this app' ? 'same path from another app' : `through ${l.base}` })) });
252
+ const how = r.through ? ` (through ${r.through.join(', ')}, which changed)` : '';
253
+ findings.push({ risk: callers.length ? 'high' : 'low', kind: 'route-changed', says: callers.length ? `${r.method} ${r.path} changed${how}, and ${count(callers.length, 'caller')} in other repositories ${callers.length === 1 ? 'depends' : 'depend'} on it` : `${r.method} ${r.path} changed${how}; no traced repository calls it`, at: end(repo, r), ...(r.through ? { through: r.through } : {}), dependents: callers.map((l) => ({ ...l.from, how: l.base === 'computed' ? 'matched by path; the base address is set when it runs' : l.base === 'this app' ? 'same path from another app' : `through ${l.base}` })) });
239
254
  }
240
255
  for (const p of publishes) {
241
256
  const ls = trace.links.filter((l) => l.kind === 'event' && l.from.repo === repo.name && l.from.member === p.member && l.event === p.event);
@@ -447,6 +462,7 @@ export function renderImpact(r) {
447
462
  if (r.matched) out.push(`Read "${r.term}" as: ${r.matched.join('; ')}`);
448
463
  for (const f of r.findings) {
449
464
  out.push('', `${f.risk.toUpperCase().padEnd(6)} ${f.at.repo}: ${f.says}`, ` ${f.at.cite}`);
465
+ for (const x of f.behaviour ?? []) out.push(` what changes: ${x.says}`);
450
466
  for (const d of f.dependents) out.push(` ← ${d.repo}: ${d.label} (${d.how}) ${d.cite}`);
451
467
  }
452
468
  return out.join('\n');