@tsplc/tmp-debug 0.0.0-stage → 0.6.2

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 (78) hide show
  1. package/README.md +38 -2
  2. package/dist/api.js +744 -0
  3. package/dist/api.js.map +12 -0
  4. package/dist/constant.js +33 -0
  5. package/dist/constant.js.map +10 -0
  6. package/dist/explain-file.js +468 -0
  7. package/dist/explain-file.js.map +11 -0
  8. package/dist/explain.js +1310 -0
  9. package/dist/explain.js.map +19 -0
  10. package/dist/format.js +214 -0
  11. package/dist/format.js.map +12 -0
  12. package/dist/index.js +4066 -0
  13. package/dist/index.js.map +44 -0
  14. package/dist/manifest.js +240 -0
  15. package/dist/manifest.js.map +10 -0
  16. package/dist/paths.js +242 -0
  17. package/dist/paths.js.map +10 -0
  18. package/package.json +72 -4
  19. package/src/adapter.ts +77 -0
  20. package/src/api.ts +446 -0
  21. package/src/app-server.ts +151 -0
  22. package/src/build-files.ts +46 -0
  23. package/src/constant.ts +41 -0
  24. package/src/debugger.ts +237 -0
  25. package/src/errors.ts +13 -0
  26. package/src/explain-expressions.ts +484 -0
  27. package/src/explain-file.ts +325 -0
  28. package/src/explain-frames.ts +155 -0
  29. package/src/explain-loops.ts +151 -0
  30. package/src/explain-operations.ts +232 -0
  31. package/src/explain-tables.ts +345 -0
  32. package/src/explain-value.ts +203 -0
  33. package/src/explain-verdicts.ts +548 -0
  34. package/src/explain.ts +82 -0
  35. package/src/hardware.ts +159 -0
  36. package/src/http.ts +218 -0
  37. package/src/index.ts +44 -0
  38. package/src/jobs.ts +139 -0
  39. package/src/login-value.ts +165 -0
  40. package/src/login.ts +315 -0
  41. package/src/manifest.ts +328 -0
  42. package/src/opcua.ts +80 -0
  43. package/src/paths.ts +363 -0
  44. package/src/requests.ts +99 -0
  45. package/src/server-cursor.ts +22 -0
  46. package/src/shown.ts +229 -0
  47. package/src/telemetry.ts +125 -0
  48. package/src/timeline.ts +148 -0
  49. package/src/trace.ts +62 -0
  50. package/src/ui/bits.ts +48 -0
  51. package/src/ui/client.ts +87 -0
  52. package/src/ui/dom.ts +61 -0
  53. package/src/ui/explain-read.ts +105 -0
  54. package/src/ui/format.ts +261 -0
  55. package/src/ui/hardware.ts +425 -0
  56. package/src/ui/header.ts +207 -0
  57. package/src/ui/health.ts +64 -0
  58. package/src/ui/index.ts +27 -0
  59. package/src/ui/jobs.ts +263 -0
  60. package/src/ui/links.ts +59 -0
  61. package/src/ui/live-value.ts +85 -0
  62. package/src/ui/main.ts +76 -0
  63. package/src/ui/overview.ts +178 -0
  64. package/src/ui/page.ts +286 -0
  65. package/src/ui/pages.ts +64 -0
  66. package/src/ui/recent-window.ts +49 -0
  67. package/src/ui/recent.ts +355 -0
  68. package/src/ui/request-trace.ts +38 -0
  69. package/src/ui/requests.ts +80 -0
  70. package/src/ui/routes.ts +98 -0
  71. package/src/ui/store.ts +295 -0
  72. package/src/ui/success-cache.ts +11 -0
  73. package/src/ui/tree.ts +162 -0
  74. package/src/ui/variables.ts +597 -0
  75. package/src/ui/why-watch.ts +11 -0
  76. package/src/ui/why.ts +694 -0
  77. package/src/values.ts +170 -0
  78. package/src/watch.ts +32 -0
@@ -0,0 +1,484 @@
1
+ import {
2
+ calleeFrame,
3
+ compareOrder,
4
+ type Evaluation,
5
+ type Frame,
6
+ frameKey,
7
+ } from "./explain-frames";
8
+ import { firstOf } from "./explain-loops";
9
+ import { binary, convert, unary } from "./explain-operations";
10
+ import {
11
+ constantAggregate,
12
+ equalValues,
13
+ known,
14
+ merge,
15
+ type Outcome,
16
+ sampleValue,
17
+ type UnknownOutcome,
18
+ unknown,
19
+ withEstimate,
20
+ } from "./explain-value";
21
+ import { anyIndex } from "./paths";
22
+
23
+ /** The most expression nodes one explanation evaluates. */
24
+ export const nodeBudget = 20_000;
25
+ /** The deepest nesting one evaluation follows, well inside the JavaScript stack. */
26
+ export const maxDepth = 400;
27
+
28
+ /** A stored path's value in the evaluation's read. */
29
+ export const sampleOutcome = (
30
+ { manifest, tables, read, needs }: Evaluation,
31
+ path: string,
32
+ type: string,
33
+ ): Outcome => {
34
+ const flags = tables.flagsOf(path);
35
+ if (flags.constantTable && flags.initial !== null) {
36
+ const value = sampleValue(manifest, type, flags.initial);
37
+ return value
38
+ ? known(value)
39
+ : unknown("a constant this debugger can't show");
40
+ }
41
+ const sample = read.sample(path);
42
+ if (!sample) {
43
+ needs.add(path);
44
+ return unknown("reading");
45
+ }
46
+ if (sample.quality !== "good") {
47
+ return unknown(`read ${sample.quality}`);
48
+ }
49
+ const value = sampleValue(manifest, type, sample.value);
50
+ return value ? known(value) : unknown("a value this debugger can't evaluate");
51
+ };
52
+
53
+ /** Whether a write of `path` in the frame's task runs after position `at` of the frame. */
54
+ const writtenAfter = (
55
+ { file, tables }: Evaluation,
56
+ path: string,
57
+ frame: Frame,
58
+ at: number,
59
+ ): boolean => {
60
+ const here = [...frame.order, at];
61
+ return tables.writersOf(path).some((writer) => {
62
+ const assignment = file.assignments[writer.assignment];
63
+ return (
64
+ assignment !== undefined &&
65
+ writer.chains.some(
66
+ (chain) =>
67
+ chain.task === frame.task &&
68
+ compareOrder(tables.writeOrder(chain, assignment.position), here) > 0,
69
+ )
70
+ );
71
+ });
72
+ };
73
+
74
+ /** A read of stored state at `capturedAt`, with how the sample relates to what the scan saw. */
75
+ const stored = (
76
+ evaluation: Evaluation,
77
+ path: string,
78
+ type: string,
79
+ capturedAt: number,
80
+ frame: Frame,
81
+ interleaved: boolean,
82
+ ): Outcome => {
83
+ const outcome = sampleOutcome(evaluation, path, type);
84
+ const flags = evaluation.tables.flagsOf(path);
85
+ const estimate = [
86
+ ...(flags.writerTasks.some((task) => task !== frame.task)
87
+ ? ["also written by another task"]
88
+ : []),
89
+ ...(writtenAfter(evaluation, path, frame, capturedAt)
90
+ ? ["from the previous scan"]
91
+ : []),
92
+ ...(interleaved && evaluation.tables.writersOf(path).length
93
+ ? ["a call between its read and its use may have changed it"]
94
+ : []),
95
+ ];
96
+ return withEstimate(outcome, estimate);
97
+ };
98
+
99
+ const project = (outcome: Outcome, path: readonly string[]): Outcome => {
100
+ const [first, ...rest] = path;
101
+ if (first === undefined || !outcome.known) {
102
+ return outcome;
103
+ }
104
+ const { value } = outcome;
105
+ if (value.kind === "record") {
106
+ const field = value.fields[first];
107
+ return field
108
+ ? project(withEstimate(field, outcome.estimate), rest)
109
+ : unknown("a field this value lacks");
110
+ }
111
+ if (value.kind === "array") {
112
+ const item = value.items[Number(first) - value.lower];
113
+ return item
114
+ ? project(withEstimate(item, outcome.estimate), rest)
115
+ : unknown("an element this value lacks");
116
+ }
117
+ return unknown("a part of a scalar");
118
+ };
119
+
120
+ /** The value of the PLC clock where a frame runs. */
121
+ const clock = ({ read }: Evaluation, frame: Frame): Outcome => {
122
+ const own = read.clock(frame.task);
123
+ return own
124
+ ? known({ kind: "interval", low: own.low, high: own.high })
125
+ : unknown("no PLC clock in this build");
126
+ };
127
+
128
+ /** The definitions of a local or written-through parameter before `at`, latest first. */
129
+ const definitionsBefore = ({ file }: Evaluation, local: number, at: number) =>
130
+ (file.locals[local]?.definitions ?? [])
131
+ .filter((definition) => definition.position < at)
132
+ .sort((a, b) => b.position - a.position);
133
+
134
+ const localValue = (
135
+ evaluation: Evaluation,
136
+ local: number,
137
+ path: readonly string[],
138
+ at: number,
139
+ frame: Frame,
140
+ ): Outcome | undefined => {
141
+ const { file, tables } = evaluation;
142
+ for (const [loop, value] of frame.counters) {
143
+ if (tables.loopCounterLocal(loop) === local) {
144
+ return known({ kind: "integer", value });
145
+ }
146
+ }
147
+ // A definition later in a loop being executed ran in the previous iteration, unless this is the first.
148
+ for (const definition of file.locals[local]?.definitions ?? []) {
149
+ const counter =
150
+ definition.loop === null
151
+ ? undefined
152
+ : frame.counters.get(definition.loop);
153
+ if (
154
+ definition.position < at ||
155
+ definition.loop === null ||
156
+ counter === undefined
157
+ ) {
158
+ continue;
159
+ }
160
+ const first = firstOf(evaluation, definition.loop, frame);
161
+ if (!first.known) {
162
+ return unknown(first.reason, first.estimate);
163
+ }
164
+ if (first.value.kind !== "integer" || counter !== first.value.value) {
165
+ return unknown("changed inside this loop");
166
+ }
167
+ }
168
+ for (const definition of definitionsBefore(evaluation, local, at)) {
169
+ const prefix = definition.path.every((name, index) => path[index] === name);
170
+ const part =
171
+ !prefix && path.every((name, index) => definition.path[index] === name);
172
+ if (!prefix && !part) {
173
+ continue;
174
+ }
175
+ const truth = evaluation.conditionsTruth(definition.conditions, frame);
176
+ if (truth.truth === "false") {
177
+ continue;
178
+ }
179
+ const site = tables.siteText(definition.site);
180
+ if (truth.truth !== "true") {
181
+ return unknown(`depends on whether ${site} ran`, truth.estimate);
182
+ }
183
+ if (part) {
184
+ return unknown(`partly set by ${site}`, truth.estimate);
185
+ }
186
+ if (definition.kind === "borrowed") {
187
+ return unknown(`changed by the call ${site}`, truth.estimate);
188
+ }
189
+ if (definition.kind === "counter" || definition.value === null) {
190
+ return unknown(`set by the loop ${site}`, truth.estimate);
191
+ }
192
+ return withEstimate(
193
+ project(
194
+ expression(evaluation, definition.value, frame),
195
+ path.slice(definition.path.length),
196
+ ),
197
+ truth.estimate,
198
+ );
199
+ }
200
+ return undefined;
201
+ };
202
+
203
+ const callResult = (
204
+ evaluation: Evaluation,
205
+ call: number,
206
+ frame: Frame,
207
+ ): Outcome => {
208
+ const inner = calleeFrame(evaluation, call, frame);
209
+ if ("known" in inner) {
210
+ return inner;
211
+ }
212
+ const returns = evaluation.file.returns
213
+ .filter((item) => item.routine === inner.row)
214
+ .sort((a, b) => a.position - b.position);
215
+ for (const returned of returns) {
216
+ const truth = evaluation.conditionsTruth(returned.conditions, inner);
217
+ if (truth.truth === "false") {
218
+ continue;
219
+ }
220
+ if (truth.truth !== "true" || returned.value === null) {
221
+ return unknown(
222
+ `depends on whether ${evaluation.tables.siteText(returned.site)} returned`,
223
+ truth.estimate,
224
+ );
225
+ }
226
+ return withEstimate(
227
+ expression(evaluation, returned.value, inner),
228
+ truth.estimate,
229
+ );
230
+ }
231
+ return unknown("returns nothing this debugger can follow");
232
+ };
233
+
234
+ /** The value of an expression of `explain.json` in a frame, once per evaluation and within its budget. */
235
+ export const expression = (
236
+ evaluation: Evaluation,
237
+ index: number,
238
+ frame: Frame,
239
+ ): Outcome => {
240
+ const { memo, budget } = evaluation;
241
+ const key = `${index}|${frameKey(frame)}`;
242
+ const cached = memo.get(key);
243
+ if (cached) {
244
+ return cached;
245
+ }
246
+ budget.spent += 1;
247
+ if (budget.spent > nodeBudget || budget.depth >= maxDepth) {
248
+ return unknown("too deep");
249
+ }
250
+ budget.depth += 1;
251
+ try {
252
+ const outcome = evaluate(evaluation, index, frame);
253
+ memo.set(key, outcome);
254
+ return outcome;
255
+ } finally {
256
+ budget.depth -= 1;
257
+ }
258
+ };
259
+
260
+ const evaluate = (
261
+ evaluation: Evaluation,
262
+ index: number,
263
+ frame: Frame,
264
+ ): Outcome => {
265
+ const { manifest, file, tables } = evaluation;
266
+ const node = file.expressions[index];
267
+ if (!node) {
268
+ return unknown("not traced");
269
+ }
270
+ switch (node.kind) {
271
+ case "read": {
272
+ const path = tables.pathAt(frame.row, node.path);
273
+ const bound = [...frame.counters].find(
274
+ ([loop]) => tables.loopCounterPath(loop) === path,
275
+ )?.[1];
276
+ return bound === undefined
277
+ ? stored(
278
+ evaluation,
279
+ path,
280
+ node.type,
281
+ node.capturedAt,
282
+ frame,
283
+ node.interleaved === true,
284
+ )
285
+ : known({ kind: "integer", value: bound });
286
+ }
287
+ case "pattern": {
288
+ const indexes = node.indexes.map((item) =>
289
+ expression(evaluation, item, frame),
290
+ );
291
+ const estimate = merge(...indexes.map((item) => item.estimate));
292
+ const missing = indexes.find(
293
+ (item): item is UnknownOutcome => !item.known,
294
+ );
295
+ if (missing) {
296
+ return unknown(missing.reason, estimate);
297
+ }
298
+ const values = indexes.flatMap((item) =>
299
+ item.known && item.value.kind === "integer" ? [item.value.value] : [],
300
+ );
301
+ if (values.length !== indexes.length) {
302
+ return unknown("an index this debugger can't evaluate", estimate);
303
+ }
304
+ const path = tables
305
+ .pathAt(frame.row, node.path)
306
+ .split(anyIndex)
307
+ .reduce(
308
+ (built, part, position) =>
309
+ `${built}${position ? `[${values[position - 1]}]` : ""}${part}`,
310
+ "",
311
+ );
312
+ return withEstimate(
313
+ stored(
314
+ evaluation,
315
+ path,
316
+ node.type,
317
+ node.capturedAt,
318
+ frame,
319
+ node.interleaved === true,
320
+ ),
321
+ estimate,
322
+ );
323
+ }
324
+ case "constant": {
325
+ const value = sampleValue(manifest, node.type, node.value);
326
+ return value ? known(value) : constantAggregate(node.value);
327
+ }
328
+ case "record":
329
+ return known({
330
+ kind: "record",
331
+ fields: Object.fromEntries(
332
+ Object.entries(node.fields).map(([name, field]) => [
333
+ name,
334
+ expression(evaluation, field, frame),
335
+ ]),
336
+ ),
337
+ });
338
+ case "array":
339
+ return known({
340
+ kind: "array",
341
+ lower: node.lower,
342
+ items: node.items.map((item) => expression(evaluation, item, frame)),
343
+ });
344
+ case "local":
345
+ return (
346
+ localValue(
347
+ evaluation,
348
+ tables.localAt(frame.row, node.local),
349
+ node.path,
350
+ node.at,
351
+ frame,
352
+ ) ?? unknown("no value before this point")
353
+ );
354
+ case "parameter": {
355
+ const own =
356
+ node.local === null
357
+ ? undefined
358
+ : localValue(
359
+ evaluation,
360
+ tables.localAt(frame.row, node.local),
361
+ node.path,
362
+ node.at,
363
+ frame,
364
+ );
365
+ if (own) {
366
+ return own;
367
+ }
368
+ const caller = frame.caller;
369
+ const call = caller ? file.calls[caller.call] : undefined;
370
+ const argument = call?.arguments[node.parameter];
371
+ if (!caller || argument === undefined) {
372
+ return unknown("depends on the call");
373
+ }
374
+ return project(expression(evaluation, argument, caller.frame), node.path);
375
+ }
376
+ case "call":
377
+ return project(
378
+ callResult(evaluation, tables.callAt(frame.row, node.call), frame),
379
+ node.path,
380
+ );
381
+ case "clock":
382
+ return clock(evaluation, frame);
383
+ case "reference": {
384
+ const operand = expression(evaluation, node.operand, frame);
385
+ if (!operand.known) {
386
+ return operand;
387
+ }
388
+ if (operand.value.kind !== "integer") {
389
+ return unknown(
390
+ "a reference this debugger can't follow",
391
+ operand.estimate,
392
+ );
393
+ }
394
+ const id = operand.value.value;
395
+ const path =
396
+ id < 0n ? null : file.paths[file.references[String(id)] ?? -1];
397
+ return known(
398
+ { kind: "instance", id, path: path ?? null },
399
+ operand.estimate,
400
+ );
401
+ }
402
+ case "unary": {
403
+ const operand = expression(evaluation, node.operand, frame);
404
+ return operand.known
405
+ ? withEstimate(unary(node, operand.value), operand.estimate)
406
+ : operand;
407
+ }
408
+ case "convert": {
409
+ const operand = expression(evaluation, node.operand, frame);
410
+ return operand.known
411
+ ? withEstimate(convert(node, operand.value), operand.estimate)
412
+ : operand;
413
+ }
414
+ case "binary": {
415
+ const left = expression(evaluation, node.left, frame);
416
+ const right = expression(evaluation, node.right, frame);
417
+ const estimate = merge(left.estimate, right.estimate);
418
+ if (!left.known) {
419
+ return withEstimate(left, right.estimate);
420
+ }
421
+ if (!right.known) {
422
+ return withEstimate(right, left.estimate);
423
+ }
424
+ return withEstimate(binary(node, left.value, right.value), estimate);
425
+ }
426
+ case "logical": {
427
+ const left = expression(evaluation, node.left, frame);
428
+ const decided = node.operator === "or";
429
+ if (
430
+ left.known &&
431
+ left.value.kind === "bool" &&
432
+ left.value.value === decided
433
+ ) {
434
+ return left;
435
+ }
436
+ const right = expression(evaluation, node.right, frame);
437
+ if (
438
+ right.known &&
439
+ right.value.kind === "bool" &&
440
+ right.value.value === decided
441
+ ) {
442
+ // Kleene: `? && false` and `? || true` decide without the other operand.
443
+ return withEstimate(right, left.estimate);
444
+ }
445
+ if (!left.known) {
446
+ return withEstimate(left, right.estimate);
447
+ }
448
+ return withEstimate(right, left.estimate);
449
+ }
450
+ case "select": {
451
+ const condition = expression(evaluation, node.condition, frame);
452
+ if (condition.known && condition.value.kind === "bool") {
453
+ return withEstimate(
454
+ expression(
455
+ evaluation,
456
+ condition.value.value ? node.then : node.else,
457
+ frame,
458
+ ),
459
+ condition.estimate,
460
+ );
461
+ }
462
+ const then = expression(evaluation, node.then, frame);
463
+ const otherwise = expression(evaluation, node.else, frame);
464
+ if (
465
+ then.known &&
466
+ otherwise.known &&
467
+ equalValues(then.value, otherwise.value)
468
+ ) {
469
+ return withEstimate(
470
+ then,
471
+ merge(condition.estimate, otherwise.estimate),
472
+ );
473
+ }
474
+ return unknown(
475
+ condition.known
476
+ ? "a condition this debugger can't evaluate"
477
+ : condition.reason,
478
+ condition.estimate,
479
+ );
480
+ }
481
+ case "opaque":
482
+ return unknown(`not traced: ${tables.text(node.text) ?? ""}`);
483
+ }
484
+ };