@skillstech/thunderlang 1.0.3 → 1.1.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 (74) hide show
  1. package/CHANGELOG.md +207 -0
  2. package/dist/core.cjs +13779 -10043
  3. package/dist/index.cjs +28500 -24392
  4. package/dist/learn.cjs +12853 -9579
  5. package/intent-graph.schema.json +11 -0
  6. package/package.json +6 -2
  7. package/src/cli.mjs +141 -58
  8. package/src/codegen.mjs +145 -55
  9. package/src/conformance.mjs +15 -6
  10. package/src/core/check.mjs +725 -0
  11. package/src/core/cli.mjs +133 -0
  12. package/src/core/diagnostics.mjs +81 -0
  13. package/src/core/format.mjs +20 -0
  14. package/src/core/from-python.mjs +645 -0
  15. package/src/core/index.mjs +38 -0
  16. package/src/core/lexer.mjs +115 -0
  17. package/src/core/parser.mjs +742 -0
  18. package/src/core/project.mjs +167 -0
  19. package/src/core/round-trip.mjs +150 -0
  20. package/src/core/runtime-python.mjs +801 -0
  21. package/src/core/target-python.mjs +486 -0
  22. package/src/core.d.ts +5 -2
  23. package/src/core.mjs +5 -2
  24. package/src/data-schema.mjs +8 -1
  25. package/src/emit.mjs +1 -1
  26. package/src/evidence.mjs +1 -1
  27. package/src/expr.mjs +81 -4
  28. package/src/form-words.mjs +2 -0
  29. package/src/index.d.ts +8 -1
  30. package/src/index.mjs +5 -1
  31. package/src/intent-schema.mjs +1 -0
  32. package/src/libraries.mjs +2 -2
  33. package/src/mcp.mjs +23 -0
  34. package/src/meaning.mjs +47 -7
  35. package/src/model.d.mts +7 -3
  36. package/src/model.mjs +30 -9
  37. package/src/money.d.mts +18 -0
  38. package/src/money.mjs +124 -0
  39. package/src/plain-contract.mjs +18 -2
  40. package/src/powershell.mjs +24 -5
  41. package/src/read-repo.mjs +1 -0
  42. package/src/read.d.mts +11 -0
  43. package/src/read.mjs +121 -13
  44. package/src/review-comment.d.mts +16 -0
  45. package/src/review-comment.mjs +145 -0
  46. package/src/rlang.mjs +10 -3
  47. package/src/say-less.mjs +1432 -0
  48. package/src/shell.mjs +45 -8
  49. package/src/sql-proc.mjs +1198 -0
  50. package/src/sql.mjs +87 -119
  51. package/src/steps.mjs +188 -37
  52. package/src/system.mjs +153 -44
  53. package/src/target-cs.mjs +39 -34
  54. package/src/target-elixir.mjs +40 -30
  55. package/src/target-go.mjs +41 -36
  56. package/src/target-java.mjs +34 -27
  57. package/src/target-kotlin.mjs +43 -35
  58. package/src/target-py.mjs +57 -55
  59. package/src/target-result.d.mts +48 -0
  60. package/src/target-result.mjs +112 -0
  61. package/src/target-rust.mjs +42 -38
  62. package/src/target-scala.mjs +45 -35
  63. package/src/target-ts.mjs +37 -45
  64. package/src/target-types.mjs +41 -5
  65. package/src/target-util.mjs +55 -2
  66. package/src/trace-cli.mjs +58 -0
  67. package/src/trace-fs.mjs +76 -0
  68. package/src/trace.mjs +261 -0
  69. package/src/translate.d.mts +3 -0
  70. package/src/translate.mjs +62 -32
  71. package/src/why-cli.mjs +31 -0
  72. package/src/why.d.mts +28 -0
  73. package/src/why.mjs +532 -0
  74. package/src/write-steps.mjs +22 -5
@@ -0,0 +1,145 @@
1
+ // The pull request comment for a Thunder review (thunder-review-v1, see review.mjs): what the change does, in plain
2
+ // sentences, grouped by the action, rule, or function it changed, with the risks first and honest counts of what was
3
+ // explained in sentences, what is only shown as code, and what a person has to check because Thunder cannot.
4
+ //
5
+ // Pure and browser-safe, with no imports, so the GitHub Action (action/thunder-review.mjs) can render a review made by
6
+ // any published compiler version. The wording comes from the review itself: this file never says "safe" or "same";
7
+ // it only repeats what the review showed and counts it.
8
+
9
+ export const REVIEW_MARKER = '<!-- thunder-review -->';
10
+ /** GitHub rejects comment bodies over 65,536 characters; stay under it with room for the marker and footer. */
11
+ export const GITHUB_COMMENT_LIMIT = 65536;
12
+ export const COMMENT_TITLE = 'Thunder review: what this change does, in plain sentences';
13
+ /** When a member no longer fits in full, at most this many of its sentences are shown. */
14
+ const COMPACT_SENTENCES = 3;
15
+ const DOCS_URL = 'https://thunderlang.dev/docs/github-action';
16
+ const SITE_URL = 'https://thunderlang.dev';
17
+
18
+ // What a person has to check because Thunder could not read or does not read it.
19
+ const CANNOT_CHECK = new Set(['unreadable', 'infrastructure', 'not-read']);
20
+ // Changes Thunder shows as the code before and after, rather than explaining in sentences.
21
+ const SHOWN_AS_CODE = new Set(['hidden', 'outside']);
22
+
23
+ /** How an item reached the reviewer: 'explained' (in sentences), 'code' (shown as code), or 'person' (cannot check). */
24
+ export function itemHow(x) {
25
+ if (CANNOT_CHECK.has(x.kind) || (x.kind === 'added' && x.severity === 'risk')) return 'person';
26
+ if (SHOWN_AS_CODE.has(x.kind)) return 'code';
27
+ return 'explained';
28
+ }
29
+
30
+ const plural = (n, one, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;
31
+
32
+ /** Honest counts for the comment's second line. Each item counts once, as in the review's own summary. */
33
+ export function reviewCounts(r) {
34
+ const c = { explained: 0, code: 0, person: 0, notRead: r.notRead?.length ?? 0 };
35
+ for (const x of r.items) c[itemHow(x)]++;
36
+ return c;
37
+ }
38
+
39
+ export function countsLine(c) {
40
+ const parts = [`${plural(c.explained, 'change')} explained in sentences`, `${c.code} shown as code (Thunder did not put ${c.code === 1 ? 'it' : 'them'} into words)`];
41
+ parts.push(`${c.person} Thunder cannot check (check ${c.person === 1 ? 'it' : 'them'} yourself)`);
42
+ if (c.notRead) parts.push(`${plural(c.notRead, 'file')} Thunder does not read`);
43
+ return `${parts.join(' · ')}.`;
44
+ }
45
+
46
+ const esc = (s) => String(s).replace(/[<>]/g, (ch) => (ch === '<' ? '&lt;' : '&gt;'));
47
+ const fence = (lines) => ['```', ...lines.map((l) => l.replace(/```/g, "'''")), '```'];
48
+ const cutLines = (lines, max) => (lines.length > max ? [...lines.slice(0, max), `… ${lines.length - max} more lines`] : lines);
49
+
50
+ function evidence(ev, maxLines) {
51
+ if (!ev) return [];
52
+ const out = [];
53
+ if (ev.counterexample) out.push(`Tried on both versions: when ${ev.counterexample.facts}, before: **${ev.counterexample.before}**, after: **${ev.counterexample.after}**.`, '');
54
+ if (ev.before?.length) out.push('Before:', ...fence(cutLines(ev.before, maxLines)));
55
+ if (ev.after?.length) out.push('After:', ...fence(cutLines(ev.after, maxLines)));
56
+ return out;
57
+ }
58
+
59
+ const HEADINGS = {
60
+ risk: ['Needs a careful look', 'A Stop removed or under new conditions, a new address, something that now reaches outside the program, or code Thunder cannot check.'],
61
+ meaning: ['Changes in meaning', 'Anything else that changes what the code does.'],
62
+ form: ['Only the form changed', 'Shown, not guessed: the same code apart from spacing, comments, or names it declares itself, or a rule with the same answer on every edge case.'],
63
+ };
64
+
65
+ /**
66
+ * Render a thunder-review-v1 review as one sticky pull request comment.
67
+ * options: { maxChars (default 60000, under GITHUB_COMMENT_LIMIT; pass more for a job summary), fileUrl(file, line) => url|null,
68
+ * notes: string[] (shown as quotes under the counts), footer, evidenceLines (per before or after block) }
69
+ * Returns the Markdown; it always starts with REVIEW_MARKER and is never longer than maxChars: members that do not fit
70
+ * are left out whole and counted ("N more not shown"), so no code block or <details> is cut in half.
71
+ */
72
+ export function renderReviewComment(r, { maxChars = 60000, fileUrl = null, notes = [], footer = '', evidenceLines = 40 } = {}) {
73
+ const where = (w) => {
74
+ const label = `\`${esc(w.file)}:${w.line}\``;
75
+ const url = fileUrl?.(w.file, w.line);
76
+ return url ? `[${label}](${url})` : label;
77
+ };
78
+ const head = [REVIEW_MARKER, `### ${COMMENT_TITLE}`, '', `**${r.says}**`, '', countsLine(reviewCounts(r)), ''];
79
+ for (const n of notes.filter(Boolean)) head.push(`> ${n}`, '');
80
+
81
+ // One block per changed action, rule, or function (a "member"), in the review's order of importance.
82
+ const blocks = [];
83
+ for (const sev of ['risk', 'meaning', 'form']) {
84
+ const list = r.items.filter((x) => x.severity === sev);
85
+ if (!list.length) continue;
86
+ const members = new Map();
87
+ for (const x of list) {
88
+ const k = `${x.where.file}\u0000${x.subject}`;
89
+ if (!members.has(k)) members.set(k, []);
90
+ members.get(k).push(x);
91
+ }
92
+ const [title, why] = HEADINGS[sev];
93
+ const open = sev === 'form' ? [`<details><summary>${title} (${list.length})</summary>`, '', `_${why}_`, ''] : [`#### ${title} (${list.length})`, '', `_${why}_`, ''];
94
+ const close = sev === 'form' ? ['</details>', ''] : [];
95
+ // Each member twice: in full, and compact (its first sentences, without the code) for when the full one no longer fits.
96
+ const memberBlocks = [...members.values()].map((xs) => {
97
+ const render = (compact) => {
98
+ const lines = [`- **${esc(xs[0].subject)}** · ${where(xs[0].where)}`];
99
+ const list = compact ? xs.slice(0, COMPACT_SENTENCES) : xs;
100
+ for (const x of list) {
101
+ const ev = sev === 'form' ? [] : evidence(x.evidence, evidenceLines);
102
+ const at = x.where.line !== xs[0].where.line || x.where.file !== xs[0].where.file ? ` (${where(x.where)})` : '';
103
+ lines.push(` - ${esc(x.says)}${at}${compact && (ev.length || x.also?.length) ? ' _(Before and after, and other places: in the job summary.)_' : ''}`);
104
+ if (compact) continue;
105
+ if (ev.length) lines.push('', ` <details><summary>Before and after</summary>`, '', ...ev.map((l) => (l ? ` ${l}` : '')), '', ' </details>', '');
106
+ if (x.also?.length) {
107
+ lines.push('', ` <details><summary>The same in ${x.also.length} more</summary>`, '', ...x.also.slice(0, 50).map((a) => ` - **${esc(a.subject)}** · ${where(a.where)}`),
108
+ ...(x.also.length > 50 ? [` - and ${x.also.length - 50} more`] : []), '', ' </details>', '');
109
+ }
110
+ }
111
+ if (list.length < xs.length) lines.push(` - _…and ${xs.length - list.length} more changes to it, in the job summary._`);
112
+ return { lines, items: list.length };
113
+ };
114
+ return { full: render(false), compact: render(true) };
115
+ });
116
+ blocks.push({ open, close, members: memberBlocks });
117
+ }
118
+ const tail = [];
119
+ if (r.notRead?.length) tail.push(`Also changed, not read by Thunder (check ${r.notRead.length === 1 ? 'it' : 'them'} yourself if ${r.notRead.length === 1 ? 'it matters' : 'they matter'}): ${r.notRead.slice(0, 20).map((f) => `\`${esc(f.path)}\``).join(', ')}${r.notRead.length > 20 ? `, and ${r.notRead.length - 20} more` : ''}.`, '');
120
+ const foot = ['---', `<sub>Read from the code by the Thunder compiler, with no AI and without running the code. "Only the form changed" is said only when it is shown. Thunder describes the change; a person still decides whether it is right. [How this works](${DOCS_URL}) · [thunderlang.dev](${SITE_URL})${footer ? ` · ${footer}` : ''}</sub>`, ''];
121
+
122
+ // Fill blocks in order of importance. A member that does not fit in full is shown compact (its sentences); when
123
+ // even that does not fit, stop there and say how many were left out.
124
+ const size = (lines) => lines.join('\n').length + 1;
125
+ const total = r.items.length;
126
+ const moreLine = (n) => [`**${n} more not shown** to stay under GitHub's comment size limit. The job summary and \`thunder review\` on your computer have all of them.`, ''];
127
+ const reserve = size(head) + size(tail) + size(foot) + size(moreLine(total)) + 64;
128
+ if (reserve > maxChars) throw new Error(`maxChars ${maxChars} is too small for the comment header and footer.`);
129
+ let used = reserve, shown = 0, full = true;
130
+ const body = [];
131
+ for (const b of blocks) {
132
+ if (!full) break;
133
+ const opened = size(b.open) + size(b.close);
134
+ if (used + opened + (b.members[0] ? size(b.members[0].compact.lines) : 0) > maxChars) { full = false; break; }
135
+ body.push(...b.open); used += opened;
136
+ for (const m of b.members) {
137
+ const pick = used + size(m.full.lines) <= maxChars ? m.full : used + size(m.compact.lines) <= maxChars ? m.compact : null;
138
+ if (!pick) { full = false; break; }
139
+ body.push(...pick.lines); used += size(pick.lines); shown += pick.items;
140
+ }
141
+ body.push('', ...b.close);
142
+ }
143
+ const out = [...head, ...body, ...(shown < total ? moreLine(total - shown) : []), ...tail, ...foot];
144
+ return out.join('\n');
145
+ }
package/src/rlang.mjs CHANGED
@@ -140,7 +140,8 @@ function argsOf(list) {
140
140
 
141
141
  // ── Words ─────────────────────────────────────────────────────────────────────────────────────────────
142
142
 
143
- const rWords = (name) => { const w = words(String(name).replace(/^\.+/, '').replace(/\./g, ' ')); return w || String(name); };
143
+ // (A name that starts with a dot, `.stroke`, is its own name: "stroke" would be a different variable.)
144
+ const rWords = (name) => { if (/^\.[A-Za-z_]\w*$/.test(String(name))) return String(name); const w = words(String(name).replace(/^\.+/, '').replace(/\./g, ' ')); return w || String(name); };
144
145
  const quote = (s) => `"${s.length > 100 ? `${s.slice(0, 99)}…` : s}"`;
145
146
 
146
147
  // Text with values inside (docs/spec.md): the literal parts are kept exactly as the code writes them (escapes
@@ -841,13 +842,19 @@ function makeReader(src) {
841
842
  if (call) {
842
843
  const { bare, args } = call;
843
844
  const a0 = args[0] ? sayValue(args[0].toks) : null;
844
- if (bare === 'stop' || bare === 'abort' || bare === 'cli_abort' || bare === 'stopf') {
845
+ // rlang's `stop_input_type(x, "a formula")` always stops: x is not what was wanted.
846
+ if (bare === 'stop_input_type') {
847
+ const what = at0(args[1]?.toks ?? [])[0];
848
+ const x = at0(args[0]?.toks ?? []);
849
+ return [{ kind: 'fail', says: `Fails with an input-type error${what?.t === 'str' || typeof what?.value === 'string' ? ` (${x.length === 1 ? rWords(x[0].text) : 'the value'} is not ${what.value})` : ''}`, error: 'an input-type error', ...base(s, e) }];
850
+ }
851
+ if (bare === 'stop' || bare === 'abort' || bare === 'cli_abort' || bare === 'stopf' || bare === 'ui_abort' || /^(?:abort_\w+|\w+_abort|stop_\w+)$/.test(bare)) {
845
852
  // Only cli_abort fills in {…}; stop() and rlang's abort() show their text as written.
846
853
  const parts = args.filter((a) => !a.name || a.name === 'message').map((a) => ({ ...a, name: null }));
847
854
  const one = at0(parts[0]?.toks ?? []);
848
855
  const isVar = parts.length === 1 && one.length === 1 && one[0].t === 'id';
849
856
  const single = !isVar && oneValue(parts) ? sayValue(parts[0].toks) : null;
850
- const msg = isVar || single || !parts.length ? null : textOf(parts, '', bare === 'cli_abort');
857
+ const msg = isVar || single || !parts.length ? null : textOf(parts, '', bare === 'cli_abort' || bare === 'ui_abort');
851
858
  if (msg) errors.set(msg, errors.get(msg) ?? lineAt(toks[s].at));
852
859
  const cls = args.find((a) => a.name === 'class');
853
860
  const name = cls ? (at0(cls.toks)[0]?.value ?? 'an error') : 'an error';