@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.
- package/README.md +38 -2
- package/dist/api.js +744 -0
- package/dist/api.js.map +12 -0
- package/dist/constant.js +33 -0
- package/dist/constant.js.map +10 -0
- package/dist/explain-file.js +468 -0
- package/dist/explain-file.js.map +11 -0
- package/dist/explain.js +1310 -0
- package/dist/explain.js.map +19 -0
- package/dist/format.js +214 -0
- package/dist/format.js.map +12 -0
- package/dist/index.js +4066 -0
- package/dist/index.js.map +44 -0
- package/dist/manifest.js +240 -0
- package/dist/manifest.js.map +10 -0
- package/dist/paths.js +242 -0
- package/dist/paths.js.map +10 -0
- package/package.json +72 -4
- package/src/adapter.ts +77 -0
- package/src/api.ts +446 -0
- package/src/app-server.ts +151 -0
- package/src/build-files.ts +46 -0
- package/src/constant.ts +41 -0
- package/src/debugger.ts +237 -0
- package/src/errors.ts +13 -0
- package/src/explain-expressions.ts +484 -0
- package/src/explain-file.ts +325 -0
- package/src/explain-frames.ts +155 -0
- package/src/explain-loops.ts +151 -0
- package/src/explain-operations.ts +232 -0
- package/src/explain-tables.ts +345 -0
- package/src/explain-value.ts +203 -0
- package/src/explain-verdicts.ts +548 -0
- package/src/explain.ts +82 -0
- package/src/hardware.ts +159 -0
- package/src/http.ts +218 -0
- package/src/index.ts +44 -0
- package/src/jobs.ts +139 -0
- package/src/login-value.ts +165 -0
- package/src/login.ts +315 -0
- package/src/manifest.ts +328 -0
- package/src/opcua.ts +80 -0
- package/src/paths.ts +363 -0
- package/src/requests.ts +99 -0
- package/src/server-cursor.ts +22 -0
- package/src/shown.ts +229 -0
- package/src/telemetry.ts +125 -0
- package/src/timeline.ts +148 -0
- package/src/trace.ts +62 -0
- package/src/ui/bits.ts +48 -0
- package/src/ui/client.ts +87 -0
- package/src/ui/dom.ts +61 -0
- package/src/ui/explain-read.ts +105 -0
- package/src/ui/format.ts +261 -0
- package/src/ui/hardware.ts +425 -0
- package/src/ui/header.ts +207 -0
- package/src/ui/health.ts +64 -0
- package/src/ui/index.ts +27 -0
- package/src/ui/jobs.ts +263 -0
- package/src/ui/links.ts +59 -0
- package/src/ui/live-value.ts +85 -0
- package/src/ui/main.ts +76 -0
- package/src/ui/overview.ts +178 -0
- package/src/ui/page.ts +286 -0
- package/src/ui/pages.ts +64 -0
- package/src/ui/recent-window.ts +49 -0
- package/src/ui/recent.ts +355 -0
- package/src/ui/request-trace.ts +38 -0
- package/src/ui/requests.ts +80 -0
- package/src/ui/routes.ts +98 -0
- package/src/ui/store.ts +295 -0
- package/src/ui/success-cache.ts +11 -0
- package/src/ui/tree.ts +162 -0
- package/src/ui/variables.ts +597 -0
- package/src/ui/why-watch.ts +11 -0
- package/src/ui/why.ts +694 -0
- package/src/values.ts +170 -0
- package/src/watch.ts +32 -0
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
import { sampleOutcome } from "./explain-expressions";
|
|
2
|
+
import type { ExplainCondition } from "./explain-file";
|
|
3
|
+
import {
|
|
4
|
+
calleeFrame,
|
|
5
|
+
compareOrder,
|
|
6
|
+
type Evaluation,
|
|
7
|
+
type Frame,
|
|
8
|
+
rootFrame,
|
|
9
|
+
} from "./explain-frames";
|
|
10
|
+
import { executeLoop, type LoopRun } from "./explain-loops";
|
|
11
|
+
import type { Chain, Explanation, Writer } from "./explain-tables";
|
|
12
|
+
import {
|
|
13
|
+
and,
|
|
14
|
+
constantAggregate,
|
|
15
|
+
equalValues,
|
|
16
|
+
merge,
|
|
17
|
+
type Outcome,
|
|
18
|
+
sampleValue,
|
|
19
|
+
type Truth,
|
|
20
|
+
} from "./explain-value";
|
|
21
|
+
import type { DebugIo } from "./manifest";
|
|
22
|
+
|
|
23
|
+
/** A condition with its truth, for the page to show. */
|
|
24
|
+
export interface ConditionResult {
|
|
25
|
+
readonly condition: ExplainCondition;
|
|
26
|
+
/** The routine row it was evaluated in, whose slots its expressions name. */
|
|
27
|
+
readonly row: number;
|
|
28
|
+
readonly truth: Truth;
|
|
29
|
+
readonly reasons: readonly string[];
|
|
30
|
+
readonly estimate: readonly string[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The conditions a statement runs under, each with its truth, and the truth of them all. */
|
|
34
|
+
export interface ConditionsTruth {
|
|
35
|
+
truth: Truth;
|
|
36
|
+
results: ConditionResult[];
|
|
37
|
+
estimate: string[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface Verdict {
|
|
41
|
+
readonly writer: Writer;
|
|
42
|
+
readonly chain: Chain;
|
|
43
|
+
readonly state: "runs" | "blocked" | "unknown" | "candidate";
|
|
44
|
+
/** Why it is unknown or a candidate, or what blocks it. */
|
|
45
|
+
readonly reasons: readonly string[];
|
|
46
|
+
readonly estimate: readonly string[];
|
|
47
|
+
/** The writer's own conditions and each call's on the chain, outermost first. */
|
|
48
|
+
readonly conditions: readonly ConditionResult[];
|
|
49
|
+
/** The value it writes, when it runs. */
|
|
50
|
+
readonly value: Outcome | null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export type Answer =
|
|
54
|
+
| { kind: "forced"; value: Outcome }
|
|
55
|
+
/** The last-writer slot names it: the verdict of that writer, or null when nothing wrote it since the controller started. */
|
|
56
|
+
| { kind: "exact"; writer: Verdict | null }
|
|
57
|
+
| { kind: "input"; io: DebugIo; value: Outcome }
|
|
58
|
+
| {
|
|
59
|
+
kind: "set";
|
|
60
|
+
writer: Verdict;
|
|
61
|
+
/** Later writers that may have run instead. */
|
|
62
|
+
unless: readonly Verdict[];
|
|
63
|
+
estimate: readonly string[];
|
|
64
|
+
}
|
|
65
|
+
| {
|
|
66
|
+
kind: "held";
|
|
67
|
+
/** The writers that could have produced the current value. */
|
|
68
|
+
writers: readonly Verdict[];
|
|
69
|
+
estimate: readonly string[];
|
|
70
|
+
}
|
|
71
|
+
| { kind: "changed"; writers: readonly Verdict[] }
|
|
72
|
+
| {
|
|
73
|
+
kind: "unknown";
|
|
74
|
+
writers: readonly Verdict[];
|
|
75
|
+
reasons: readonly string[];
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export interface AnswerResult {
|
|
79
|
+
readonly answer: Answer;
|
|
80
|
+
readonly verdicts: readonly Verdict[];
|
|
81
|
+
/** Clients may write the path. */
|
|
82
|
+
readonly clientWritable: boolean;
|
|
83
|
+
/** The HMI control that writes the path. */
|
|
84
|
+
readonly hmiControl: string | null;
|
|
85
|
+
/** The path is kept across restarts. */
|
|
86
|
+
readonly retained: boolean;
|
|
87
|
+
/** Tasks besides the one that reads the path also write it. */
|
|
88
|
+
readonly writerTasks: readonly string[];
|
|
89
|
+
/** Stored paths the evaluation needed and this read didn't have. */
|
|
90
|
+
readonly needs: readonly string[];
|
|
91
|
+
/** The writers a last-writer slot that isn't exact recorded, as a hint. */
|
|
92
|
+
readonly hint: readonly Verdict[];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
type Result = (
|
|
96
|
+
truth: Truth,
|
|
97
|
+
reasons?: readonly string[],
|
|
98
|
+
estimate?: readonly string[],
|
|
99
|
+
) => ConditionResult;
|
|
100
|
+
|
|
101
|
+
/** A job step condition: true while the job waits at it, else a candidate of this scan or earlier. */
|
|
102
|
+
const jobTruth = (
|
|
103
|
+
{ manifest, read }: Evaluation,
|
|
104
|
+
job: number,
|
|
105
|
+
step: number,
|
|
106
|
+
result: Result,
|
|
107
|
+
): ConditionResult => {
|
|
108
|
+
const state = read.job(job);
|
|
109
|
+
const declared = manifest.jobs.find((item) => item.id === job);
|
|
110
|
+
const steps = declared?.steps ?? [];
|
|
111
|
+
const stepText =
|
|
112
|
+
steps.find((item) => item.step === step)?.text ?? `step ${step}`;
|
|
113
|
+
if (!state || state.busy === null) {
|
|
114
|
+
return result("unknown", ["the job's state isn't known"]);
|
|
115
|
+
}
|
|
116
|
+
if (state.unread) {
|
|
117
|
+
return result("unknown", ["over the jobs read per poll"]);
|
|
118
|
+
}
|
|
119
|
+
const current = steps.find((item) => item.step === state.step);
|
|
120
|
+
const cancelling = state.cancelling ? ["the job is being cancelled"] : [];
|
|
121
|
+
if (state.busy && state.step === step && current?.kind === "await") {
|
|
122
|
+
return result(
|
|
123
|
+
"true",
|
|
124
|
+
[`waiting at ${stepText}`],
|
|
125
|
+
["a job's step can change between scans", ...cancelling],
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
return result(
|
|
129
|
+
"candidate",
|
|
130
|
+
[
|
|
131
|
+
`ran when job ${declared?.name ?? `job ${job}`} passed ${stepText}, this scan or earlier`,
|
|
132
|
+
],
|
|
133
|
+
cancelling,
|
|
134
|
+
);
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export const conditionTruth = (
|
|
138
|
+
evaluation: Evaluation,
|
|
139
|
+
condition: ExplainCondition,
|
|
140
|
+
frame: Frame,
|
|
141
|
+
): ConditionResult => {
|
|
142
|
+
const { tables, read } = evaluation;
|
|
143
|
+
const result: Result = (truth, reasons = [], estimate = []) => ({
|
|
144
|
+
condition,
|
|
145
|
+
row: frame.row,
|
|
146
|
+
truth,
|
|
147
|
+
reasons,
|
|
148
|
+
estimate,
|
|
149
|
+
});
|
|
150
|
+
switch (condition.kind) {
|
|
151
|
+
case "test": {
|
|
152
|
+
const outcome = evaluation.expression(condition.test, frame);
|
|
153
|
+
if (!outcome.known) {
|
|
154
|
+
return result("unknown", [outcome.reason], outcome.estimate);
|
|
155
|
+
}
|
|
156
|
+
if (outcome.value.kind !== "bool") {
|
|
157
|
+
return result(
|
|
158
|
+
"unknown",
|
|
159
|
+
["a condition this debugger can't evaluate"],
|
|
160
|
+
outcome.estimate,
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
return result(
|
|
164
|
+
outcome.value.value === condition.holds ? "true" : "false",
|
|
165
|
+
[],
|
|
166
|
+
outcome.estimate,
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
case "cases": {
|
|
170
|
+
const outcome = evaluation.expression(condition.test, frame);
|
|
171
|
+
if (!outcome.known) {
|
|
172
|
+
return result("unknown", [outcome.reason], outcome.estimate);
|
|
173
|
+
}
|
|
174
|
+
const member = condition.values.some((value) => {
|
|
175
|
+
const constant = constantAggregate(value);
|
|
176
|
+
return (
|
|
177
|
+
constant.known && equalValues(constant.value, outcome.value) === true
|
|
178
|
+
);
|
|
179
|
+
});
|
|
180
|
+
return result(
|
|
181
|
+
member === condition.member ? "true" : "false",
|
|
182
|
+
[],
|
|
183
|
+
outcome.estimate,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
case "exits": {
|
|
187
|
+
const parts = condition.any.map((all) =>
|
|
188
|
+
all.map((part) => conditionTruth(evaluation, part, frame)),
|
|
189
|
+
);
|
|
190
|
+
const any = parts.map((all) => and(all.map((part) => part.truth)));
|
|
191
|
+
const estimate = merge(...parts.flat().map((part) => part.estimate));
|
|
192
|
+
if (any.includes("true")) {
|
|
193
|
+
return result("false", [], estimate);
|
|
194
|
+
}
|
|
195
|
+
if (any.includes("unknown") || any.includes("candidate")) {
|
|
196
|
+
return result(
|
|
197
|
+
"unknown",
|
|
198
|
+
["depends on whether an earlier statement left"],
|
|
199
|
+
estimate,
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
return result("true", [], estimate);
|
|
203
|
+
}
|
|
204
|
+
case "loop":
|
|
205
|
+
return frame.counters.has(tables.loopAt(frame.row, condition.loop))
|
|
206
|
+
? result("true")
|
|
207
|
+
: result("unknown", ["inside a loop"]);
|
|
208
|
+
case "loopReturns": {
|
|
209
|
+
const run = executeLoop(
|
|
210
|
+
evaluation,
|
|
211
|
+
tables.loopAt(frame.row, condition.loop),
|
|
212
|
+
frame,
|
|
213
|
+
null,
|
|
214
|
+
);
|
|
215
|
+
if (run.kind === "unknown") {
|
|
216
|
+
return result("unknown", [run.reason], run.estimate);
|
|
217
|
+
}
|
|
218
|
+
return result(run.returned ? "false" : "true", [], run.estimate);
|
|
219
|
+
}
|
|
220
|
+
case "job":
|
|
221
|
+
return jobTruth(
|
|
222
|
+
evaluation,
|
|
223
|
+
tables.slots(frame.row)?.job ?? -1,
|
|
224
|
+
condition.step,
|
|
225
|
+
result,
|
|
226
|
+
);
|
|
227
|
+
case "jobBusy": {
|
|
228
|
+
const state = read.job(tables.slots(frame.row)?.job ?? -1);
|
|
229
|
+
if (!state || state.busy === null) {
|
|
230
|
+
return result("unknown", ["the job's state isn't known"]);
|
|
231
|
+
}
|
|
232
|
+
return state.busy
|
|
233
|
+
? result("true", [], ["the job may have moved on since"])
|
|
234
|
+
: result("candidate", ["the job isn't running now"]);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
export const conditionsTruth = (
|
|
240
|
+
evaluation: Evaluation,
|
|
241
|
+
entry: number | null,
|
|
242
|
+
frame: Frame,
|
|
243
|
+
): ConditionsTruth => {
|
|
244
|
+
const results = evaluation.tables
|
|
245
|
+
.conditionList(entry)
|
|
246
|
+
.map((condition) => conditionTruth(evaluation, condition, frame));
|
|
247
|
+
return {
|
|
248
|
+
truth: and(results.map((item) => item.truth)),
|
|
249
|
+
results,
|
|
250
|
+
estimate: merge(...results.map((item) => item.estimate)),
|
|
251
|
+
};
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
/** Whether an index a pattern writes at runtime is the one the explained path has. */
|
|
255
|
+
const indexTruth = (outcome: Outcome, wanted: bigint | undefined): Truth => {
|
|
256
|
+
if (!outcome.known) {
|
|
257
|
+
return "unknown";
|
|
258
|
+
}
|
|
259
|
+
return outcome.value.kind === "integer" && outcome.value.value === wanted
|
|
260
|
+
? "true"
|
|
261
|
+
: "false";
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
/** How a writer inside a loop fared over the loop's execution. */
|
|
265
|
+
const loopResult = (
|
|
266
|
+
run: LoopRun,
|
|
267
|
+
): Pick<ConditionResult, "truth" | "reasons"> => {
|
|
268
|
+
if (run.kind === "unknown") {
|
|
269
|
+
return { truth: "unknown", reasons: [run.reason] };
|
|
270
|
+
}
|
|
271
|
+
return run.ran
|
|
272
|
+
? { truth: "true", reasons: [] }
|
|
273
|
+
: { truth: "false", reasons: ["no iteration ran it"] };
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
const states = {
|
|
277
|
+
true: "runs",
|
|
278
|
+
false: "blocked",
|
|
279
|
+
unknown: "unknown",
|
|
280
|
+
candidate: "candidate",
|
|
281
|
+
} as const;
|
|
282
|
+
|
|
283
|
+
/** A writer's verdict through one chain. */
|
|
284
|
+
const verdict = (
|
|
285
|
+
evaluation: Evaluation,
|
|
286
|
+
writer: Writer,
|
|
287
|
+
chain: Chain,
|
|
288
|
+
): Verdict => {
|
|
289
|
+
const { file, tables } = evaluation;
|
|
290
|
+
const assignment = file.assignments[writer.assignment];
|
|
291
|
+
if (!assignment) {
|
|
292
|
+
return {
|
|
293
|
+
writer,
|
|
294
|
+
chain,
|
|
295
|
+
state: "unknown",
|
|
296
|
+
reasons: ["not traced"],
|
|
297
|
+
estimate: [],
|
|
298
|
+
conditions: [],
|
|
299
|
+
value: null,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
const results: ConditionResult[] = [];
|
|
303
|
+
let frame = rootFrame(evaluation, { ...chain, calls: [] });
|
|
304
|
+
const reasons: string[] = [];
|
|
305
|
+
for (const call of chain.calls) {
|
|
306
|
+
const record = file.calls[call];
|
|
307
|
+
if (!record) {
|
|
308
|
+
continue;
|
|
309
|
+
}
|
|
310
|
+
if (record.loop !== null) {
|
|
311
|
+
reasons.push("called inside a loop");
|
|
312
|
+
results.push({
|
|
313
|
+
condition: {
|
|
314
|
+
kind: "loop",
|
|
315
|
+
loop: tables.loopSlot(frame.row, record.loop),
|
|
316
|
+
},
|
|
317
|
+
row: frame.row,
|
|
318
|
+
truth: "unknown",
|
|
319
|
+
reasons: ["called inside a loop"],
|
|
320
|
+
estimate: [],
|
|
321
|
+
});
|
|
322
|
+
} else {
|
|
323
|
+
results.push(
|
|
324
|
+
...conditionsTruth(evaluation, record.conditions, frame).results,
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
const next = calleeFrame(evaluation, call, frame);
|
|
328
|
+
if ("known" in next) {
|
|
329
|
+
return {
|
|
330
|
+
writer,
|
|
331
|
+
chain,
|
|
332
|
+
state: "unknown",
|
|
333
|
+
reasons: [next.reason],
|
|
334
|
+
estimate: [...next.estimate],
|
|
335
|
+
conditions: results,
|
|
336
|
+
value: null,
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
frame = next;
|
|
340
|
+
}
|
|
341
|
+
// A place written through a parameter is named, with its indexes, in the outermost caller.
|
|
342
|
+
const namingFrame = (inner: Frame): Frame =>
|
|
343
|
+
assignment.via.reduce<Frame>(
|
|
344
|
+
(frame) => frame.caller?.frame ?? frame,
|
|
345
|
+
inner,
|
|
346
|
+
);
|
|
347
|
+
// A write at runtime indexes reaches the explained path only when each index matches.
|
|
348
|
+
const indexMatch = (inner: Frame): ConditionResult[] =>
|
|
349
|
+
assignment.writes.kind === "pattern"
|
|
350
|
+
? assignment.writes.indexes.map((index, position) => {
|
|
351
|
+
const outcome = evaluation.expression(index, namingFrame(inner));
|
|
352
|
+
return {
|
|
353
|
+
condition: {
|
|
354
|
+
kind: "test",
|
|
355
|
+
test: index,
|
|
356
|
+
holds: true,
|
|
357
|
+
at: assignment.position,
|
|
358
|
+
text: null,
|
|
359
|
+
},
|
|
360
|
+
row: namingFrame(inner).row,
|
|
361
|
+
truth: indexTruth(outcome, writer.indexes[position]),
|
|
362
|
+
reasons: outcome.known ? [] : [outcome.reason],
|
|
363
|
+
estimate: outcome.estimate,
|
|
364
|
+
};
|
|
365
|
+
})
|
|
366
|
+
: [];
|
|
367
|
+
const reaches = (inner: Frame) => {
|
|
368
|
+
const own = conditionsTruth(evaluation, assignment.conditions, inner);
|
|
369
|
+
const matched = indexMatch(inner);
|
|
370
|
+
return {
|
|
371
|
+
truth: and([own.truth, ...matched.map((item) => item.truth)]),
|
|
372
|
+
estimate: merge(own.estimate, ...matched.map((item) => item.estimate)),
|
|
373
|
+
results: [...own.results, ...matched],
|
|
374
|
+
};
|
|
375
|
+
};
|
|
376
|
+
if (assignment.loop !== null) {
|
|
377
|
+
const run = executeLoop(evaluation, assignment.loop, frame, reaches);
|
|
378
|
+
results.push({
|
|
379
|
+
condition: {
|
|
380
|
+
kind: "loop",
|
|
381
|
+
loop: tables.loopSlot(frame.row, assignment.loop),
|
|
382
|
+
},
|
|
383
|
+
row: frame.row,
|
|
384
|
+
...loopResult(run),
|
|
385
|
+
estimate: run.estimate,
|
|
386
|
+
});
|
|
387
|
+
if (run.kind === "run" && run.ran) {
|
|
388
|
+
frame = run.ran;
|
|
389
|
+
}
|
|
390
|
+
} else {
|
|
391
|
+
results.push(...reaches(frame).results);
|
|
392
|
+
}
|
|
393
|
+
const truth = and(results.map((item) => item.truth));
|
|
394
|
+
return {
|
|
395
|
+
writer,
|
|
396
|
+
chain,
|
|
397
|
+
state: states[truth],
|
|
398
|
+
reasons: [
|
|
399
|
+
...reasons,
|
|
400
|
+
...results
|
|
401
|
+
.filter((item) => item.truth === truth && truth !== "true")
|
|
402
|
+
.flatMap((item) => item.reasons),
|
|
403
|
+
],
|
|
404
|
+
estimate: merge(...results.map((item) => item.estimate)),
|
|
405
|
+
conditions: results,
|
|
406
|
+
value:
|
|
407
|
+
truth === "true" && assignment.value !== null
|
|
408
|
+
? evaluation.expression(assignment.value, frame)
|
|
409
|
+
: null,
|
|
410
|
+
};
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
/** Why the path has its value in the evaluation's read; `type` is the path's manifest type. */
|
|
414
|
+
export const answer = (
|
|
415
|
+
evaluation: Evaluation,
|
|
416
|
+
explanation: Explanation,
|
|
417
|
+
type: string,
|
|
418
|
+
): AnswerResult => {
|
|
419
|
+
const { manifest, file, tables, read, needs } = evaluation;
|
|
420
|
+
const current = sampleOutcome(evaluation, explanation.path, type);
|
|
421
|
+
const verdicts = explanation.writers.flatMap((writer) =>
|
|
422
|
+
writer.chains.length
|
|
423
|
+
? writer.chains.map((chain) => verdict(evaluation, writer, chain))
|
|
424
|
+
: [
|
|
425
|
+
{
|
|
426
|
+
writer,
|
|
427
|
+
chain: { task: "", entry: -1, calls: [] },
|
|
428
|
+
state: "blocked" as const,
|
|
429
|
+
reasons: ["no task reaches it"],
|
|
430
|
+
estimate: [],
|
|
431
|
+
conditions: [],
|
|
432
|
+
value: null,
|
|
433
|
+
},
|
|
434
|
+
],
|
|
435
|
+
);
|
|
436
|
+
const slot = read.slot(explanation.path);
|
|
437
|
+
const slotted = verdicts.filter((item) =>
|
|
438
|
+
slot?.assignments?.includes(item.writer.assignment),
|
|
439
|
+
);
|
|
440
|
+
const result = (answer: Answer): AnswerResult => ({
|
|
441
|
+
answer,
|
|
442
|
+
verdicts,
|
|
443
|
+
clientWritable: explanation.flags.clientWritable,
|
|
444
|
+
hmiControl: explanation.flags.hmiControl,
|
|
445
|
+
retained: explanation.flags.retained,
|
|
446
|
+
writerTasks: explanation.flags.writerTasks,
|
|
447
|
+
needs: [...needs],
|
|
448
|
+
hint: slot && !slot.exact ? slotted : [],
|
|
449
|
+
});
|
|
450
|
+
if (read.forced?.has(explanation.path)) {
|
|
451
|
+
return result({ kind: "forced", value: current });
|
|
452
|
+
}
|
|
453
|
+
if (slot?.exact) {
|
|
454
|
+
return result({
|
|
455
|
+
kind: "exact",
|
|
456
|
+
writer:
|
|
457
|
+
slot.assignments === null
|
|
458
|
+
? null
|
|
459
|
+
: (slotted.find((item) => item.state === "runs") ??
|
|
460
|
+
slotted[0] ??
|
|
461
|
+
null),
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
if (explanation.io?.direction === "input") {
|
|
465
|
+
return result({ kind: "input", io: explanation.io, value: current });
|
|
466
|
+
}
|
|
467
|
+
const orderOf = (item: Verdict) =>
|
|
468
|
+
tables.writeOrder(
|
|
469
|
+
item.chain,
|
|
470
|
+
file.assignments[item.writer.assignment]?.position ?? 0,
|
|
471
|
+
);
|
|
472
|
+
const ordered = [...verdicts].sort((a, b) =>
|
|
473
|
+
compareOrder(orderOf(a), orderOf(b)),
|
|
474
|
+
);
|
|
475
|
+
const consistent = (item: Verdict): boolean => {
|
|
476
|
+
if (!item.value?.known || item.value.estimate.length || !current.known) {
|
|
477
|
+
return true;
|
|
478
|
+
}
|
|
479
|
+
return equalValues(item.value.value, current.value) !== false;
|
|
480
|
+
};
|
|
481
|
+
const running = ordered.filter((item) => item.state === "runs");
|
|
482
|
+
const excluded = running.filter((item) => !consistent(item));
|
|
483
|
+
const candidates = running.filter((item) => consistent(item));
|
|
484
|
+
const last = candidates.at(-1);
|
|
485
|
+
if (last) {
|
|
486
|
+
const tasks = new Set(running.map((item) => item.chain.task));
|
|
487
|
+
const later = ordered
|
|
488
|
+
.slice(ordered.indexOf(last) + 1)
|
|
489
|
+
.filter(
|
|
490
|
+
(item) =>
|
|
491
|
+
item.state === "unknown" ||
|
|
492
|
+
item.state === "candidate" ||
|
|
493
|
+
(item.state === "runs" && item.chain.task !== last.chain.task),
|
|
494
|
+
);
|
|
495
|
+
const others =
|
|
496
|
+
tasks.size > 1
|
|
497
|
+
? running.filter(
|
|
498
|
+
(item) => item !== last && item.chain.task !== last.chain.task,
|
|
499
|
+
)
|
|
500
|
+
: [];
|
|
501
|
+
const unless = [...new Set([...later, ...others])];
|
|
502
|
+
return result({
|
|
503
|
+
kind: "set",
|
|
504
|
+
writer: last,
|
|
505
|
+
unless,
|
|
506
|
+
estimate: merge(
|
|
507
|
+
last.estimate,
|
|
508
|
+
unless.length ? ["another writer may have run after it"] : [],
|
|
509
|
+
excluded.some((item) => ordered.indexOf(item) > ordered.indexOf(last))
|
|
510
|
+
? ["a later writer's value didn't match"]
|
|
511
|
+
: [],
|
|
512
|
+
),
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
if (excluded.length) {
|
|
516
|
+
return result({ kind: "changed", writers: excluded });
|
|
517
|
+
}
|
|
518
|
+
if (
|
|
519
|
+
verdicts.every(
|
|
520
|
+
(item) => item.state === "blocked" || item.state === "candidate",
|
|
521
|
+
)
|
|
522
|
+
) {
|
|
523
|
+
const could = verdicts.filter((item) => {
|
|
524
|
+
const assignment = file.assignments[item.writer.assignment];
|
|
525
|
+
const constant =
|
|
526
|
+
assignment?.value === null || assignment?.value === undefined
|
|
527
|
+
? undefined
|
|
528
|
+
: file.expressions[assignment.value];
|
|
529
|
+
if (constant?.kind !== "constant" || !current.known) {
|
|
530
|
+
return true;
|
|
531
|
+
}
|
|
532
|
+
const value = sampleValue(manifest, constant.type, constant.value);
|
|
533
|
+
return !value || equalValues(value, current.value) !== false;
|
|
534
|
+
});
|
|
535
|
+
const jobs = verdicts.filter((item) => item.state === "candidate");
|
|
536
|
+
return result({
|
|
537
|
+
kind: "held",
|
|
538
|
+
writers: could,
|
|
539
|
+
estimate: jobs.length ? ["set by a job step, this scan or earlier"] : [],
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
const open = verdicts.filter((item) => item.state === "unknown");
|
|
543
|
+
return result({
|
|
544
|
+
kind: "unknown",
|
|
545
|
+
writers: open,
|
|
546
|
+
reasons: merge(...open.map((item) => item.reasons)),
|
|
547
|
+
});
|
|
548
|
+
};
|
package/src/explain.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { expression } from "./explain-expressions";
|
|
2
|
+
import type { ExplainFile } from "./explain-file";
|
|
3
|
+
import { type Evaluation, type ExplainRead, rootFrame } from "./explain-frames";
|
|
4
|
+
import {
|
|
5
|
+
type Chain,
|
|
6
|
+
createExplainTables,
|
|
7
|
+
type Explanation,
|
|
8
|
+
} from "./explain-tables";
|
|
9
|
+
import { answer, conditionsTruth, conditionTruth } from "./explain-verdicts";
|
|
10
|
+
import type { DebugManifest } from "./manifest";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Why a stored value is what it is: the statements that write it, the conditions they run under
|
|
14
|
+
* and the live values those read. Pure, so the browser UI and the VS Code extension evaluate with
|
|
15
|
+
* the same rules. Every evaluation reads one `ExplainRead`: values the login read together.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export { maxDepth, nodeBudget } from "./explain-expressions";
|
|
19
|
+
export type { ExplainRead } from "./explain-frames";
|
|
20
|
+
export { maxLoopIterations } from "./explain-loops";
|
|
21
|
+
export type { Chain, Explanation, Writer } from "./explain-tables";
|
|
22
|
+
export {
|
|
23
|
+
type Outcome,
|
|
24
|
+
type PlcValue,
|
|
25
|
+
sampleValue,
|
|
26
|
+
type Truth,
|
|
27
|
+
} from "./explain-value";
|
|
28
|
+
export type {
|
|
29
|
+
Answer,
|
|
30
|
+
AnswerResult,
|
|
31
|
+
ConditionResult,
|
|
32
|
+
Verdict,
|
|
33
|
+
} from "./explain-verdicts";
|
|
34
|
+
|
|
35
|
+
export const createExplainer = (manifest: DebugManifest, file: ExplainFile) => {
|
|
36
|
+
const tables = createExplainTables(manifest, file);
|
|
37
|
+
|
|
38
|
+
/** One explanation's evaluation over one read. */
|
|
39
|
+
const evaluation = (read: ExplainRead): Evaluation => {
|
|
40
|
+
const context: Evaluation = {
|
|
41
|
+
manifest,
|
|
42
|
+
file,
|
|
43
|
+
tables,
|
|
44
|
+
read,
|
|
45
|
+
needs: new Set(),
|
|
46
|
+
memo: new Map(),
|
|
47
|
+
budget: { spent: 0, depth: 0 },
|
|
48
|
+
expression: (index, frame) => expression(context, index, frame),
|
|
49
|
+
conditionTruth: (condition, frame) =>
|
|
50
|
+
conditionTruth(context, condition, frame),
|
|
51
|
+
conditionsTruth: (entry, frame) => conditionsTruth(context, entry, frame),
|
|
52
|
+
};
|
|
53
|
+
return context;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
explain: tables.explain,
|
|
58
|
+
/** Why `path` has its value in `read`; `type` is the path's manifest type. */
|
|
59
|
+
answer: (explanation: Explanation, type: string, read: ExplainRead) =>
|
|
60
|
+
answer(evaluation(read), explanation, type),
|
|
61
|
+
/**
|
|
62
|
+
* The value of an expression of `explain.json` in `read`, evaluated in routine row `row` as
|
|
63
|
+
* the chain reaches it: its innermost frame of that row.
|
|
64
|
+
*/
|
|
65
|
+
evaluate: (index: number, chain: Chain, read: ExplainRead, row: number) => {
|
|
66
|
+
const run = evaluation(read);
|
|
67
|
+
const frames = chain.calls.map((_, depth) =>
|
|
68
|
+
rootFrame(run, { ...chain, calls: chain.calls.slice(0, depth + 1) }),
|
|
69
|
+
);
|
|
70
|
+
const frame =
|
|
71
|
+
[rootFrame(run, { ...chain, calls: [] }), ...frames]
|
|
72
|
+
.reverse()
|
|
73
|
+
.find((item) => item.row === row) ?? rootFrame(run, chain);
|
|
74
|
+
return expression(run, index, frame);
|
|
75
|
+
},
|
|
76
|
+
/** The stored path a read or pattern of routine row `row` names by `slot`. */
|
|
77
|
+
pathAt: tables.pathAt,
|
|
78
|
+
file,
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export type Explainer = ReturnType<typeof createExplainer>;
|