tla-ts 0.0.3-dev.037ac3b → 0.0.3-dev.0ad0249

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 (80) hide show
  1. package/dist/cli.cjs +1 -1
  2. package/dist/cli.js +1 -1
  3. package/dist/index.cjs +1 -1
  4. package/dist/index.d.cts +146 -8
  5. package/dist/index.d.ts +146 -8
  6. package/dist/index.js +1 -1
  7. package/dist/parallel-worker.cjs +1 -1
  8. package/dist/parallel-worker.js +2 -2
  9. package/dist/parallel.cjs +1 -1
  10. package/dist/parallel.js +2 -2
  11. package/dist/run-cli.cjs +1351 -20
  12. package/dist/run-cli.js +1351 -20
  13. package/dist/transfer.cjs +86 -28
  14. package/dist/transfer.js +86 -28
  15. package/package.json +4 -1
  16. package/src/ast.ts +48 -6
  17. package/src/bags.ts +4 -7
  18. package/src/builtins.ts +29 -11
  19. package/src/bundled-modules.ts +3 -3
  20. package/src/canonical.ts +4 -3
  21. package/src/check.ts +1330 -215
  22. package/src/cli.ts +8 -1
  23. package/src/codegen.ts +68 -18
  24. package/src/config.ts +252 -179
  25. package/src/environment.ts +451 -129
  26. package/src/errors.ts +217 -3
  27. package/src/evaluate.ts +724 -117
  28. package/src/exit-codes.ts +5 -4
  29. package/src/index.ts +1 -0
  30. package/src/instances.ts +4 -2
  31. package/src/java-overrides.ts +104 -25
  32. package/src/levels.ts +183 -37
  33. package/src/lexer.ts +14 -1
  34. package/src/live-check.ts +220 -84
  35. package/src/liveness.ts +192 -65
  36. package/src/obligation-expression.ts +2 -2
  37. package/src/obligation-source.ts +2 -0
  38. package/src/parallel-worker.ts +8 -3
  39. package/src/parallel.ts +56 -5
  40. package/src/parser.ts +423 -126
  41. package/src/pcal-ast.ts +770 -0
  42. package/src/pcal-parse.ts +1523 -0
  43. package/src/pcal-tokenize.ts +516 -0
  44. package/src/pcal-translate.ts +1230 -0
  45. package/src/pcal-validate.ts +454 -0
  46. package/src/randomization.ts +24 -7
  47. package/src/resolve.ts +1363 -405
  48. package/src/run-cli.ts +1167 -155
  49. package/src/runtime.ts +16 -0
  50. package/src/sany-builtins.ts +147 -0
  51. package/src/sany-generator.ts +3112 -0
  52. package/src/sany-levels.ts +1208 -0
  53. package/src/sany-lexer.ts +545 -0
  54. package/src/sany-linter.ts +122 -0
  55. package/src/sany-modules.ts +1398 -0
  56. package/src/sany-nodes.ts +463 -0
  57. package/src/sany-parse-error.ts +2161 -0
  58. package/src/sany-report.ts +354 -0
  59. package/src/sany-selectors.ts +738 -0
  60. package/src/sany-source.ts +248 -0
  61. package/src/sany-xml.ts +1017 -0
  62. package/src/spec-description.ts +26 -7
  63. package/src/standard-modules.ts +203 -146
  64. package/src/standard-ranges.ts +116 -0
  65. package/src/state-graph.ts +29 -6
  66. package/src/subexpressions.ts +221 -161
  67. package/src/substitution-levels.ts +38 -17
  68. package/src/successors.ts +162 -114
  69. package/src/symmetry.ts +180 -52
  70. package/src/tableau.ts +4 -2
  71. package/src/tlc-ext.ts +13 -17
  72. package/src/tlc-messages.ts +329 -0
  73. package/src/tlc-module.ts +46 -18
  74. package/src/tlc-order.ts +56 -6
  75. package/src/tlc-random.ts +28 -3
  76. package/src/tlc-replay.ts +655 -0
  77. package/src/tlc-version.ts +18 -0
  78. package/src/transfer.ts +15 -3
  79. package/src/value-sources.ts +29 -0
  80. package/src/values.ts +684 -204
package/src/check.ts CHANGED
@@ -11,22 +11,33 @@ import { getReadVariables } from "./dependencies";
11
11
  import { createEnvironment } from "./environment";
12
12
  import {
13
13
  getErrorExitCode,
14
+ isStackOverflow,
14
15
  TlaConfigError,
15
16
  TlaEvaluationError,
17
+ TlaIllegalStateError,
18
+ TlaIncompleteSuccessorError,
16
19
  TlaModuleError,
20
+ TlaStackOverflowError,
21
+ getUnexpectedException,
17
22
  UNEXPECTED_EXCEPTION,
18
23
  } from "./errors";
19
- import { EXIT_STATUS, setExitCode, withLivenessExitCode } from "./exit-codes";
24
+ import type { JavaOverride } from "./java-overrides";
25
+ import { EXIT_STATUS, getExitCode, setExitCode, withLivenessExitCode } from "./exit-codes";
20
26
  import {
27
+ describeNode,
21
28
  describeRange,
29
+ createTracingEnvironment,
22
30
  evaluate,
31
+ evaluateStartupDefinitions,
23
32
  expectBoolean,
24
33
  formatRange,
25
34
  getDefinition,
26
35
  isConstantLevel,
27
36
  isImpure,
37
+ type CallStack,
28
38
  type Context,
29
39
  type Environment,
40
+ type FingerprintFailure,
30
41
  type Scope,
31
42
  } from "./evaluate";
32
43
  import {
@@ -48,8 +59,10 @@ import {
48
59
  import {
49
60
  getActionLabel,
50
61
  getActionSignature,
62
+ getInitActions,
51
63
  getNextActions,
52
64
  getSpecificationValue,
65
+ toActionValue,
53
66
  type SpecAction,
54
67
  } from "./spec-description";
55
68
  import {
@@ -60,20 +73,34 @@ import {
60
73
  } from "./state-graph";
61
74
  import { getRepresentative, getSymmetryGroup } from "./symmetry";
62
75
  import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
76
+ import {
77
+ formatNestedMessage,
78
+ getCodedMessage,
79
+ isEvalException,
80
+ MESSAGE_CODE,
81
+ MISSING_INIT_174,
82
+ } from "./tlc-messages";
83
+ import { replayInitialStates, replaySuccessors } from "./tlc-replay";
63
84
  import { createTlcFingerprinter, getTlcFingerprint } from "./tlc-random";
64
85
  import { createCanonicalizer } from "./canonical";
65
86
  import { getPossibleCounts, type CheckerRuntime } from "./runtime";
66
- import { createCounterExample } from "./tlc-ext";
87
+ import { createActionRecord, createCounterExample } from "./tlc-ext";
67
88
  import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
89
+ import type { TlcVersion } from "./tlc-version";
68
90
  import {
69
91
  combineHash,
92
+ createModelValue,
70
93
  formatState,
71
94
  formatValue,
95
+ getHeldValues,
96
+ getFingerprintError,
97
+ markFingerprinted,
72
98
  getValueHash,
73
99
  isFunctionValue,
74
100
  isSameValue,
75
101
  isUpdatedElementOf,
76
102
  setStringOrder,
103
+ setValueVersion,
77
104
  toFiniteFunction,
78
105
  type Value,
79
106
  } from "./values";
@@ -93,8 +120,71 @@ export interface CheckOptions {
93
120
  livenessCheck?: "default" | "final";
94
121
  // TLC's -dump: writes the reachable states, or with `dot` the state graph, through writeFile.
95
122
  dump?: StateDump;
123
+ // TLC's -tool, which changes the messages of TLC's errors.
124
+ isTool?: boolean;
125
+ // Whether TLC records a binary trace for its trace exploration spec, for which it loads Integers.
126
+ isTraceRecorded?: boolean;
127
+ // Whether the caller printed SANY's report, its warnings included, before the check.
128
+ isSanyReported?: boolean;
129
+ // False for TLC's -nowarning, which changes how TLC computes the successors of some actions.
130
+ isWarningEnabled?: boolean;
131
+ // The TLC whose output tla-ts reproduces, which changes how TLC labels an action.
132
+ version?: TlcVersion;
133
+ // Reports the points of the search at which TLC prints a message.
134
+ onProgress?: (progress: CheckProgress) => void;
135
+ }
136
+
137
+ export interface InitialStatesProgress {
138
+ kind: "initial-states-progress";
139
+ count: number;
140
+ }
141
+
142
+ // A check that TLC's DoInitFunctor reports as violated before it stops its replay of the initial
143
+ // states.
144
+ export interface InitialStateViolation {
145
+ kind: "initial-state-violation";
146
+ status: "invariant-violation" | "liveness-violation";
147
+ name: string;
148
+ state: Record<string, Value>;
96
149
  }
97
150
 
151
+ // A line the spec prints, or what TLC reports as it replays the initial states.
152
+ export type OutputLine = string | InitialStatesProgress | InitialStateViolation;
153
+
154
+ // The spec is parsed, TLC warns about each invariant that is a true constant formula and each
155
+ // property that is a state formula, finds the branches of the temporal properties, computes the
156
+ // initial states, reporting each time their count reaches a power of two, reports how far the
157
+ // search got 3 seconds after it starts and every minute after, and checks the temporal properties
158
+ // on the state graph so far at those times and on the complete state graph at the end.
159
+ export type CheckProgress =
160
+ | { kind: "parsed" }
161
+ | { kind: "constant-level-invariant"; name: string }
162
+ | { kind: "state-level-property"; name: string }
163
+ | { kind: "liveness-initializing"; isSymmetric: boolean; isConstrained: boolean }
164
+ | { kind: "liveness-prepared"; branches: number }
165
+ | { kind: "initial-states-started" }
166
+ | InitialStatesProgress
167
+ | { kind: "initial-states-computed"; initialStates: InitialStates }
168
+ | { kind: "progress"; statistics: SearchStatistics }
169
+ | {
170
+ kind: "liveness-started";
171
+ branches: number;
172
+ // Whether TLC checks the complete state graph, and how many states of it each branch has.
173
+ isComplete: boolean;
174
+ states: number;
175
+ statistics: SearchStatistics;
176
+ }
177
+ | { kind: "liveness-finished" }
178
+ // A warning TLC prints as it evaluates the spec, with its message code.
179
+ | { kind: "warning"; code: number; text: string }
180
+ // A method of the class named after a module, which TLC reports as it loads the class.
181
+ | ({ kind: "java-override" } & JavaOverride);
182
+
183
+ export interface SearchStatistics extends Pick<
184
+ CheckResult,
185
+ "generatedStates" | "distinctStates" | "queuedStates" | "depth"
186
+ > {}
187
+
98
188
  export type CheckStatus =
99
189
  | "success"
100
190
  | "assumption-violation"
@@ -110,6 +200,9 @@ export interface TraceStep {
110
200
  // TLC's name, parameters and location for the action that took this step.
111
201
  label: string | undefined;
112
202
  state: Record<string, Value>;
203
+ // What the spec prints, before TLC prints this step, as TLC evaluates it again to rebuild the
204
+ // trace. The first step has what TLC prints before the trace.
205
+ output?: OutputLine[];
113
206
  }
114
207
 
115
208
  export interface Violation {
@@ -117,25 +210,64 @@ export interface Violation {
117
210
  // Every temporal property that a liveness counterexample violates, in the configuration's order.
118
211
  properties?: string[];
119
212
  trace: TraceStep[];
213
+ // What the spec prints after TLC prints the states of the trace, as TLC finds the step back into
214
+ // the cycle of a lasso or rebuilds the trace once more.
215
+ traceOutput?: OutputLine[];
120
216
  loopStart: number | undefined;
121
217
  loopLabel?: string;
122
218
  message?: string;
123
219
  counts?: string;
124
220
  unwitnessed?: Postcondition;
221
+ // A POSTCONDITION that is false or fails after the counterexample, which TLC checks as it
222
+ // prints one.
223
+ postcondition?: Postcondition;
224
+ // What TLC warns about after a counterexample, with its code.
225
+ warning?: { code: number; text: string };
125
226
  }
126
227
 
127
228
  interface Postcondition {
128
229
  name: string;
230
+ // TLC's message code.
231
+ code: number;
129
232
  message: string;
130
233
  counts?: string;
131
234
  }
132
235
 
133
236
  // An evaluation that failed during the search, with the behavior up to the state TLC was working on.
134
237
  export interface EvaluationFailure {
238
+ // TLC's code for the message, and its text with and without -tool.
239
+ code: number;
135
240
  message: string;
241
+ toolMessage: string;
136
242
  trace: TraceStep[];
243
+ traceOutput?: OutputLine[];
137
244
  initialState: Record<string, Value> | undefined;
138
245
  exitCode: number;
246
+ // The expressions TLC was evaluating when the failure occurred, outermost first, when TLC finds
247
+ // them by replaying the failure.
248
+ callStack: Expression[] | undefined;
249
+ // Whether TLC prints the call stack before the error, as it does for a failed liveness check.
250
+ isCallStackFirst?: boolean;
251
+ // The FingerprintException the replay raises, which TLC reports in place of the call stack.
252
+ fingerprint?: FingerprintFailure;
253
+ // How TLC reports failing again as it prints the trace, since it can't fingerprint the state the
254
+ // failure occurred in.
255
+ traceFailure?: FailureMessage;
256
+ // A POSTCONDITION that is false or fails after the trace, which TLC checks as it prints one.
257
+ postcondition?: Postcondition;
258
+ // Whether TLC stops before it prints the statistics, as it does for an illegal initial state.
259
+ isWithoutStatistics?: boolean;
260
+ }
261
+
262
+ // Where TLC's replay of a failure fails.
263
+ interface ReplayFailure {
264
+ callStack: Expression[];
265
+ fingerprint: FingerprintFailure | undefined;
266
+ }
267
+
268
+ export interface InitialStates {
269
+ generated: number;
270
+ distinct: number;
139
271
  }
140
272
 
141
273
  export interface CheckResult {
@@ -146,14 +278,55 @@ export interface CheckResult {
146
278
  distinctStates: number;
147
279
  queuedStates: number;
148
280
  depth: number;
281
+ // How many initial states TLC generated and how many of them were distinct, once it computed
282
+ // them all without a violation or an error.
283
+ initialStates: InitialStates | undefined;
149
284
  // How many states that TLC counts as expanded found each number of new states, when TLC prints
150
285
  // the outdegree of the state graph.
151
286
  outDegrees: number[] | undefined;
152
287
  // The smallest distance between the fingerprints of two distinct states, when TLC estimates a
153
288
  // collision from the actual fingerprints.
154
289
  fingerprintDistance: bigint | undefined;
290
+ // A temporal property TLC finds violated as it adds a deadlocked state to the behavior graph,
291
+ // which it reports after the deadlock.
292
+ additionalViolation?: Violation;
293
+ // Where TLC's worker fails to add the state to the behavior graph after it reports a violation.
294
+ liveCallStack?: Expression[];
295
+ // How many states that TLC counts as expanded found each number of new states, when its workers
296
+ // stop on a violation without an error code, which TLC 1.7.4 reports as a successful search.
297
+ violationOutDegrees?: number[];
298
+ }
299
+
300
+ interface Behavior {
301
+ trace: TraceStep[];
302
+ traceOutput?: OutputLine[];
303
+ }
304
+
305
+ interface TraceOptions {
306
+ loopStart?: number;
307
+ known?: number;
155
308
  }
156
309
 
310
+ interface FailureOptions {
311
+ initialState?: Value[];
312
+ isInitial?: boolean;
313
+ outDegrees?: number[];
314
+ callStack?: Expression[];
315
+ fingerprint?: FingerprintFailure;
316
+ isNextState?: boolean;
317
+ isCallStackFirst?: boolean;
318
+ traceFailure?: FailureMessage;
319
+ // Whether the call stack is of adding the state to the behavior graph, which TLC prints instead
320
+ // of replaying the error.
321
+ isLiveCallStack?: boolean;
322
+ }
323
+
324
+ // TLC prints what the spec prints as it replays a failure after the trace.
325
+ const appendTraceOutput = <Traced extends Behavior>(behavior: Traced, lines: OutputLine[]) => ({
326
+ ...behavior,
327
+ traceOutput: [...(behavior.traceOutput ?? []), ...lines],
328
+ });
329
+
157
330
  interface NamedExpression {
158
331
  name: string;
159
332
  expression: Expression;
@@ -163,6 +336,8 @@ interface NamedExpression {
163
336
  interface StateCheck extends NamedExpression {
164
337
  variables: number[] | undefined;
165
338
  membership: Membership | undefined;
339
+ // The whole predicate that TLC evaluates for the check, of which the expression is a conjunct.
340
+ predicate: Expression;
166
341
  }
167
342
 
168
343
  // A conjunct `x \in S` whose S reads no variables.
@@ -231,14 +406,6 @@ const getConfigOperator = (kind: string, name: string, environment: Environment)
231
406
  return definition;
232
407
  };
233
408
 
234
- const getConfigDefinition = (kind: string, name: string, environment: Environment) => {
235
- const definition = getConfigOperator(kind, name, environment);
236
- if (isConstantLevel(definition, environment)) {
237
- throw new TlaConfigError(`The ${kind} ${name} cannot be a constant.`);
238
- }
239
- return definition;
240
- };
241
-
242
409
  // TLC evaluates every constant definition before it reads the configuration. It keeps a literal
243
410
  // number or string, and a definition that fails to evaluate, as an operator.
244
411
  const getConstantValue = (name: string, definition: Definition, environment: Environment) => {
@@ -247,20 +414,77 @@ const getConstantValue = (name: string, definition: Definition, environment: Env
247
414
  try {
248
415
  return evaluate(createIdentifier(name), getContext(environment, undefined, undefined));
249
416
  } catch (error) {
250
- if (error instanceof TlaEvaluationError) return undefined;
417
+ // TLC swallows whatever the definition throws, a stack overflow included.
418
+ if (error instanceof TlaEvaluationError || isStackOverflow(error)) return undefined;
251
419
  throw error;
252
420
  }
253
421
  };
254
422
 
255
- const getConfigPredicate = (kind: string, name: string, environment: Environment) => {
423
+ // TLC takes for an operator only a definition it hasn't replaced with its value.
424
+ const getConfigDefinition = (kind: string, name: string, environment: Environment) => {
256
425
  const definition = getConfigOperator(kind, name, environment);
257
- const value = getConstantValue(name, definition, environment);
426
+ if (getConstantValue(name, definition, environment) !== undefined) {
427
+ throw new TlaConfigError(`The ${kind} ${name} cannot be a constant.`);
428
+ }
429
+ return definition;
430
+ };
431
+
432
+ // TLC also takes a constant that the configuration assigns a value, which must be TRUE.
433
+ const getConfigPredicate = (kind: string, name: string, environment: Environment) => {
434
+ const definition = environment.constants.has(name)
435
+ ? undefined
436
+ : getConfigOperator(kind, name, environment);
437
+ const value = definition
438
+ ? getConstantValue(name, definition, environment)
439
+ : environment.constants.get(name);
258
440
  if (value !== undefined && value !== true) {
259
441
  throw new TlaConfigError(`The ${kind} of ${name} is equal to ${formatValue(value)}`);
260
442
  }
261
443
  return value === undefined ? definition : undefined;
262
444
  };
263
445
 
446
+ // TLC reports how many initial states it has computed each time the count is a power of two.
447
+ export const reportInitialStatesProgress = (
448
+ count: number,
449
+ onProgress: ((progress: InitialStatesProgress) => void) | undefined,
450
+ ) => {
451
+ if (count > 1 && (count & (count - 1)) === 0) {
452
+ onProgress?.({ kind: "initial-states-progress", count });
453
+ }
454
+ };
455
+
456
+ // What TLC does once SANY is done: it computes the constant definitions, then warns about each
457
+ // invariant that is a true constant formula, which it doesn't check. TLC prints each distinct
458
+ // warning once.
459
+ export const reportStartup = (
460
+ specification: {
461
+ environment: Environment;
462
+ startupDefinitions: Definition[];
463
+ javaOverrides: JavaOverride[];
464
+ },
465
+ config: ModelConfig,
466
+ onProgress: CheckOptions["onProgress"],
467
+ ) => {
468
+ const environment = specification.environment;
469
+ const warned = new Set<string>();
470
+ environment.runtime.warn = (code, text) => {
471
+ if (warned.has(text)) return;
472
+ warned.add(text);
473
+ onProgress?.({ kind: "warning", code, text });
474
+ };
475
+ onProgress?.({ kind: "parsed" });
476
+ // TLC loads the classes of the modules before it computes the constant definitions.
477
+ specification.javaOverrides.forEach((override) =>
478
+ onProgress?.({ kind: "java-override", ...override }),
479
+ );
480
+ evaluateStartupDefinitions(specification.startupDefinitions, environment);
481
+ config.invariants.forEach((name) => {
482
+ if (!getConfigPredicate("invariant", name, environment)) {
483
+ onProgress?.({ kind: "constant-level-invariant", name });
484
+ }
485
+ });
486
+ };
487
+
264
488
  const getInvariants = (names: string[], environment: Environment) => {
265
489
  const analyzer = createLevelAnalyzer({
266
490
  constants: new Set(environment.constants.keys()),
@@ -276,8 +500,11 @@ const getInvariants = (names: string[], environment: Environment) => {
276
500
  levels.params.size === 0
277
501
  ? ""
278
502
  : "\nNote that a bug can cause TLC to incorrectly report this error.\nIf you believe your TLA+ or PlusCal specification to be correct,\nplease check if this bug described in LevelNode.java starting at line 590ff affects you.";
279
- throw new TlaConfigError(
280
- `The invariant ${name} is not a state predicate (one with no primes or temporal operators).${note}`,
503
+ throw setExitCode(
504
+ new TlaConfigError(
505
+ `The invariant ${name} is not a state predicate (one with no primes or temporal operators).${note}`,
506
+ ),
507
+ EXIT_STATUS.safetyEvaluationFailure,
281
508
  );
282
509
  }
283
510
  return [{ name, expression: createIdentifier(name), scope: undefined }];
@@ -330,14 +557,8 @@ const getSpecification = (config: ModelConfig, environment: Environment): Specif
330
557
  if (config.specification === undefined) {
331
558
  const getOperator = (name: string | undefined, description: string) => {
332
559
  if (name === undefined) return undefined;
333
- const body = getConfigOperator(description, name, environment).body;
334
- const identifier = createIdentifier(name);
335
- // TLC evaluates a literal body later and reports that it is not a boolean.
336
- const isLiteral = body.kind === "Number" || body.kind === "String";
337
- if (!isLiteral && getLevel(identifier, undefined, environment) === 0) {
338
- throw new TlaConfigError(`The ${description} ${name} cannot be a constant.`);
339
- }
340
- return identifier;
560
+ getConfigDefinition(description, name, environment);
561
+ return createIdentifier(name);
341
562
  };
342
563
  return {
343
564
  init: getOperator(config.init, "initial predicate"),
@@ -346,6 +567,11 @@ const getSpecification = (config: ModelConfig, environment: Environment): Specif
346
567
  temporals: [],
347
568
  };
348
569
  }
570
+ if (config.init !== undefined || config.next !== undefined) {
571
+ throw new TlaConfigError(
572
+ "The configuration file cannot specify both INIT/NEXT and SPECIFICATION fields.",
573
+ );
574
+ }
349
575
  const initConjuncts: ScopedExpression[] = [];
350
576
  const nextActions: ScopedExpression[] = [];
351
577
  const temporals: ScopedExpression[] = [];
@@ -375,8 +601,12 @@ const getSpecification = (config: ModelConfig, environment: Environment): Specif
375
601
  conjunct.kind === "Always" &&
376
602
  getLevel(conjunct.expression, item.scope, environment) < ACTION_LEVEL
377
603
  ) {
378
- throw new TlaConfigError(
379
- `TLC cannot handle []P in the specification ${config.specification} when P is a state predicate`,
604
+ // TLC accepts the spec and fails when it builds the tableau of the temporal formulas.
605
+ throw setExitCode(
606
+ new TlaEvaluationError(
607
+ `TLC cannot handle the temporal formula ${describeRange(conjunct.expression)}`,
608
+ ),
609
+ EXIT_STATUS.error,
380
610
  );
381
611
  }
382
612
  const level = getLevel(conjunct, item.scope, environment);
@@ -436,7 +666,11 @@ const getSpecification = (config: ModelConfig, environment: Environment): Specif
436
666
  };
437
667
  };
438
668
 
439
- const getProperties = (config: ModelConfig, environment: Environment): Properties => {
669
+ const getProperties = (
670
+ config: ModelConfig,
671
+ environment: Environment,
672
+ onProgress: CheckOptions["onProgress"],
673
+ ): Properties => {
440
674
  const properties: Properties = {
441
675
  initialPredicates: [],
442
676
  invariants: [],
@@ -446,12 +680,21 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
446
680
  config.properties.forEach((name) => {
447
681
  const definition = getConfigPredicate("property", name, environment);
448
682
  if (!definition) return;
449
- expandTemporal(definition.body, undefined, environment, true, name).forEach((item) => {
683
+ // TLC checks a property that is a state formula only on the initial states, so it warns about
684
+ // one as it processes the property, before the constraints.
685
+ if (getLevel(definition.body, undefined, environment) === 1) {
686
+ onProgress?.({ kind: "state-level-property", name });
687
+ }
688
+ // TLC 1.7.4 expands a property as it does a specification, so it doesn't unroll one that
689
+ // quantifies over a constant set.
690
+ const isProperty = environment.runtime.version !== "1.7.4";
691
+ expandTemporal(definition.body, undefined, environment, isProperty).forEach((item) => {
450
692
  const conjunct = item.expression;
451
693
  if (isConstantTrue(item, environment, "property")) return;
694
+ const partName = item.name ?? name;
452
695
  if (conjunct.kind === "Always" && conjunct.expression.kind === "BoxAction") {
453
696
  properties.actionProperties.push({
454
- name: item.name,
697
+ name: partName,
455
698
  box: conjunct.expression,
456
699
  action: conjunct.expression.action,
457
700
  subscript: conjunct.expression.subscript,
@@ -463,11 +706,12 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
463
706
  conjunct.kind === "Always" &&
464
707
  getLevel(conjunct.expression, item.scope, environment) <= 1
465
708
  ) {
466
- // TLC names the invariant []P after P when P is an operator name.
467
- const expression = conjunct.expression;
709
+ // TLC names the invariant by the operator the property asserts always holds, when it
710
+ // applies the operator to no arguments.
711
+ const body = conjunct.expression;
468
712
  properties.invariants.push({
469
- name: expression.kind === "Identifier" ? expression.name : item.name,
470
- expression,
713
+ name: body.kind === "Identifier" ? body.name : partName,
714
+ expression: body,
471
715
  scope: item.scope,
472
716
  });
473
717
  return;
@@ -475,7 +719,7 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
475
719
  const level = getLevel(conjunct, item.scope, environment);
476
720
  if (level < ACTION_LEVEL) {
477
721
  properties.initialPredicates.push({
478
- name: item.name,
722
+ name: partName,
479
723
  expression: conjunct,
480
724
  scope: item.scope,
481
725
  });
@@ -535,6 +779,17 @@ const getMembership = (
535
779
  : undefined;
536
780
  };
537
781
 
782
+ // TLC evaluates the body of an invariant that the configuration names.
783
+ const getPredicate = (expression: Expression, environment: Environment) => {
784
+ if (expression.kind !== "Identifier" || environment.constants.has(expression.name)) {
785
+ return expression;
786
+ }
787
+ const definition = getDefinition(expression.name, environment);
788
+ return definition?.kind === "Operator" && definition.params.length === 0
789
+ ? definition.body
790
+ : expression;
791
+ };
792
+
538
793
  const getStateChecks = (named: NamedExpression[], environment: Environment): StateCheck[] =>
539
794
  named.flatMap((item) =>
540
795
  getConjuncts(item.expression, environment).map((expression) => {
@@ -547,6 +802,7 @@ const getStateChecks = (named: NamedExpression[], environment: Environment): Sta
547
802
  scope: item.scope,
548
803
  variables,
549
804
  membership: getMembership(expression, variables, environment),
805
+ predicate: getPredicate(item.expression, environment),
550
806
  };
551
807
  }),
552
808
  );
@@ -595,7 +851,25 @@ interface Search {
595
851
  getStoredFingerprints: (states: Value[][], fingerprintStates: Value[][]) => BigInt64Array;
596
852
  getTlcOrder: () => number[];
597
853
  isSymmetric: boolean;
854
+ // TLC drops a constraint that is a true constant formula.
855
+ isConstrained: boolean;
598
856
  findViolatedInvariant: (state: Value[], parent?: Value[]) => StateCheck | undefined;
857
+ // The checks of a state that TLC evaluates when it replays the computation of the initial states
858
+ // or of the successors of a state, with an environment that records the call stack. The first
859
+ // returns the check that stops the replay, and the second false where TLC stops it.
860
+ // TLC fingerprints a replayed state that satisfies the constraints before it checks the rest.
861
+ checkReplayedInitialState: (
862
+ state: Value[],
863
+ tracing: Environment,
864
+ addFingerprint: () => void,
865
+ ) => Pick<InitialStateViolation, "status" | "name"> | undefined;
866
+ // Adding the fingerprint of a successor tells whether TLC had not seen it yet.
867
+ checkReplayedSuccessor: (
868
+ state: Value[],
869
+ next: Value[],
870
+ tracing: Environment,
871
+ addFingerprint: () => boolean,
872
+ ) => boolean;
599
873
  satisfiesConstraints: (state: Value[], parent?: Value[]) => boolean;
600
874
  findFailedInitialPredicate: (state: Value[]) => NamedExpression | undefined;
601
875
  findViolatedActionProperty: (state: Value[], next: Value[]) => ActionProperty | undefined;
@@ -603,7 +877,8 @@ interface Search {
603
877
  trackStatePossible: (state: Value[]) => void;
604
878
  trackActionPossible: (state: Value[], next: Value[]) => void;
605
879
  findUnwitnessedPossible: () => Postcondition | undefined;
606
- findPostconditionViolation: () => Postcondition | undefined;
880
+ // After a counterexample, TLC skips the postconditions when one of them is constant.
881
+ findPostconditionViolation: (isAfterViolation?: boolean) => Postcondition | undefined;
607
882
  }
608
883
 
609
884
  const getContext = (
@@ -629,21 +904,23 @@ const holdsLikeTlc = (
629
904
  return value;
630
905
  };
631
906
 
632
- // SANY prints a number by its digits and any other expression by its location.
633
907
  const getExpectedBoolean = (expression: Expression, environment: Environment) => {
634
908
  const definition =
635
909
  expression.kind === "Identifier" ? getDefinition(expression.name, environment) : undefined;
636
910
  const body = definition?.kind === "Operator" ? definition.body : expression;
637
911
  const range = definition?.kind === "Operator" ? definition.bodyRange : undefined;
638
- const location =
639
- body.kind === "Number"
640
- ? (body.text ?? String(body.value))
641
- : range
642
- ? formatRange(range)
643
- : describeRange(body);
912
+ const location = range && body.kind !== "Number" ? formatRange(range) : describeNode(body);
644
913
  return `TLC expected a boolean value, but did not find one. ${location}`;
645
914
  };
646
915
 
916
+ // TLC evaluates the whole predicate of a check, which fails at a conjunct that isn't a boolean as
917
+ // the conjunction does.
918
+ const holdsAsConjunct = (check: StateCheck, context: Context) =>
919
+ holdsLikeTlc(check.expression, context, (expression, environment) => {
920
+ holdsLikeTlc(check.predicate, context);
921
+ return getExpectedBoolean(expression, environment);
922
+ });
923
+
647
924
  const ignorePossible = () => {};
648
925
 
649
926
  const trackPossible = (items: NamedExpression[], runtime: CheckerRuntime, context: Context) =>
@@ -685,11 +962,16 @@ export const createSearch = (
685
962
  config: ModelConfig,
686
963
  environment: Environment,
687
964
  getVariableOrder: () => string[],
965
+ onProgress?: CheckOptions["onProgress"],
688
966
  ): Search | undefined => {
689
967
  const configInvariants = getInvariants(config.invariants, environment);
690
968
  const spec = getSpecification(config, environment);
691
- const properties = getProperties(config, environment);
969
+ const properties = getProperties(config, environment, onProgress);
692
970
  const invariants = getStateChecks([...configInvariants, ...properties.invariants], environment);
971
+ // The invariants TLC evaluates, each of which may have several conjuncts among the checks.
972
+ const invariantPredicates = [
973
+ ...new Map(invariants.map((invariant) => [invariant.predicate, invariant])).values(),
974
+ ];
693
975
  const isCheckingBehaviors =
694
976
  invariants.length > 0 ||
695
977
  properties.actionProperties.length > 0 ||
@@ -700,7 +982,11 @@ export const createSearch = (
700
982
  properties.initialPredicates.length > 0 ||
701
983
  environment.variables.length > 0)
702
984
  ) {
703
- throw new TlaConfigError("The configuration file did not specify the initial state predicate.");
985
+ throw new TlaConfigError(
986
+ environment.runtime.version === "1.7.4"
987
+ ? MISSING_INIT_174
988
+ : "The configuration file did not specify the initial state predicate.",
989
+ );
704
990
  }
705
991
  if (spec.next === undefined && isCheckingBehaviors) {
706
992
  throw new TlaConfigError("The configuration file did not specify the next state predicate.");
@@ -719,7 +1005,9 @@ export const createSearch = (
719
1005
  const possible = getPossiblePredicates(config.possible, environment);
720
1006
  if (environment.variables.length === 0) return undefined;
721
1007
  const symmetryGroup =
722
- config.symmetry === undefined ? [] : getSymmetryGroup(config.symmetry, environment);
1008
+ config.symmetry === undefined
1009
+ ? undefined
1010
+ : getSymmetryGroup(config.symmetry, environment, config.substitutions);
723
1011
  if (config.view !== undefined) getConfigOperator("view function", config.view, environment);
724
1012
  const view =
725
1013
  config.view === undefined
@@ -751,14 +1039,12 @@ export const createSearch = (
751
1039
  .sort((left, right) => getTlcPosition(left) - getTlcPosition(right));
752
1040
  return tlcOrder.indices;
753
1041
  };
754
- // A representative in TLC's variable order is the one whose fingerprint TLC reports.
755
- const order = symmetryGroup.length > 0 ? getTlcOrder() : undefined;
756
1042
  const getViewValue = (expression: Expression, state: Value[]) =>
757
1043
  evaluate(expression, getContext(environment, state, undefined));
758
1044
  const getTlcStateFingerprint = (state: Value[]) => {
759
1045
  const indices = getTlcOrder();
760
1046
  const representative =
761
- symmetryGroup.length === 0 ? state : getRepresentative(state, symmetryGroup, indices);
1047
+ symmetryGroup === undefined ? state : getRepresentative(state, symmetryGroup, indices);
762
1048
  return getTlcFingerprint(
763
1049
  view === undefined
764
1050
  ? indices.map((index) => representative[index])
@@ -774,6 +1060,7 @@ export const createSearch = (
774
1060
  const location = range ? ` at ${formatRange(range)}` : "";
775
1061
  return {
776
1062
  name: unwitnessed.name,
1063
+ code: MESSAGE_CODE.possibleUnwitnessed,
777
1064
  message: `The _POSSIBLE predicate ${unwitnessed.name}${location} was never witnessed.\nOthers may be unwitnessed too; only witnessed predicates appear with a\nnon-zero count in the record printed above.\nSee https://explain.tlapl.us/possible-conditions for additional details.`,
778
1065
  counts: prettyPrint(formatValue(getPossibleCounts(runtime), true)),
779
1066
  };
@@ -781,7 +1068,8 @@ export const createSearch = (
781
1068
  return {
782
1069
  properties,
783
1070
  spec,
784
- isSymmetric: symmetryGroup.length > 0,
1071
+ isSymmetric: symmetryGroup !== undefined,
1072
+ isConstrained: constraints.length > 0 || actionConstraints.length > 0,
785
1073
  getTlcOrder,
786
1074
  getStoredFingerprints: (states, fingerprintStates) => {
787
1075
  if (view !== undefined || isFingerprintedLikeTlc) {
@@ -797,8 +1085,11 @@ export const createSearch = (
797
1085
  },
798
1086
  getFingerprintState: (state) => {
799
1087
  if (isFingerprintedLikeTlc) return [String(getTlcStateFingerprint(state))];
1088
+ // A representative in TLC's variable order is the one whose fingerprint TLC reports.
800
1089
  const representative =
801
- symmetryGroup.length === 0 ? state : getRepresentative(state, symmetryGroup, order);
1090
+ symmetryGroup === undefined
1091
+ ? state
1092
+ : getRepresentative(state, symmetryGroup, getTlcOrder());
802
1093
  return view === undefined ? representative : [getViewValue(view.expression, representative)];
803
1094
  },
804
1095
  findViolatedInvariant: (state, parent) =>
@@ -814,21 +1105,63 @@ export const createSearch = (
814
1105
  parent[membership.variable],
815
1106
  evaluate(membership.set, context),
816
1107
  )
817
- : !holdsLikeTlc(invariant.expression, context);
1108
+ : !holdsAsConjunct(invariant, context);
818
1109
  } catch (error) {
819
1110
  // TLC reports a failure only on a state after the initial states as an invariant failure.
820
1111
  if (!parent) throw error;
821
1112
  throw withHeadline(
822
1113
  setExitCode(error, EXIT_STATUS.safetyEvaluationFailure),
1114
+ MESSAGE_CODE.invariantEvaluationFailed,
823
1115
  `Evaluating invariant ${invariant.name} failed.`,
824
1116
  );
825
1117
  }
826
1118
  }),
1119
+ checkReplayedInitialState: (state, tracing, addFingerprint) => {
1120
+ const holdsIn = (expression: Expression, scope: Scope | undefined) =>
1121
+ holdsLikeTlc(expression, getContext(tracing, state, undefined, scope));
1122
+ if (constraints.every((constraint) => holdsIn(constraint.predicate, constraint.scope))) {
1123
+ addFingerprint();
1124
+ }
1125
+ const invariant = invariantPredicates.find((check) => !holdsIn(check.predicate, check.scope));
1126
+ if (invariant) return { status: "invariant-violation", name: invariant.name };
1127
+ const predicate = properties.initialPredicates.find(
1128
+ (check) => !holdsIn(check.expression, check.scope),
1129
+ );
1130
+ return predicate && { status: "liveness-violation", name: predicate.name };
1131
+ },
1132
+ checkReplayedSuccessor: (state, next, tracing, addFingerprint) => {
1133
+ // TLC evaluates the action constraints only on a state that satisfies the constraints.
1134
+ const isInModel =
1135
+ constraints.every((constraint) =>
1136
+ holdsLikeTlc(
1137
+ constraint.predicate,
1138
+ getContext(tracing, next, undefined, constraint.scope),
1139
+ ),
1140
+ ) &&
1141
+ actionConstraints.every((constraint) =>
1142
+ holdsLikeTlc(constraint.expression, getContext(tracing, state, next, constraint.scope)),
1143
+ );
1144
+ // ModelChecker.doNext checks the invariants of a successor it has not seen, which includes
1145
+ // one outside the model.
1146
+ const isUnseen = isInModel ? addFingerprint() : true;
1147
+ return (
1148
+ (!isUnseen ||
1149
+ invariantPredicates.every((invariant) =>
1150
+ holdsLikeTlc(
1151
+ invariant.predicate,
1152
+ getContext(tracing, next, undefined, invariant.scope),
1153
+ ),
1154
+ )) &&
1155
+ actionProperties.every((property) =>
1156
+ holdsLikeTlc(property.box, getContext(tracing, state, next, property.scope)),
1157
+ )
1158
+ );
1159
+ },
827
1160
  satisfiesConstraints: (state, parent) =>
828
1161
  constraints.every(
829
1162
  (constraint) =>
830
1163
  isUnchangedFor(constraint, state, parent) ||
831
- holdsLikeTlc(constraint.expression, getContext(environment, state, undefined)),
1164
+ holdsAsConjunct(constraint, getContext(environment, state, undefined)),
832
1165
  ),
833
1166
  findFailedInitialPredicate: (state) =>
834
1167
  properties.initialPredicates.find(
@@ -857,6 +1190,7 @@ export const createSearch = (
857
1190
  } catch (error) {
858
1191
  throw withHeadline(
859
1192
  withNextStateExitCode(error),
1193
+ MESSAGE_CODE.actionPropertyEvaluationFailed,
860
1194
  `Evaluating action property ${property.name} failed.`,
861
1195
  );
862
1196
  }
@@ -880,14 +1214,40 @@ export const createSearch = (
880
1214
  : (state, next) =>
881
1215
  trackPossible(actionPossible, runtime, getContext(environment, state, next)),
882
1216
  findUnwitnessedPossible,
883
- findPostconditionViolation: () => {
1217
+ findPostconditionViolation: (isAfterViolation = false) => {
1218
+ // TLC replaces a definition it computes when it starts by its value, and then rejects it as
1219
+ // a postcondition before it checks any of them.
1220
+ const constant = config.postconditions.find((name) => {
1221
+ const definition = getDefinition(name, environment);
1222
+ return definition !== undefined && environment.definitionValues.has(definition);
1223
+ });
1224
+ if (constant !== undefined) {
1225
+ if (isAfterViolation) return undefined;
1226
+ throw setExitCode(
1227
+ new TlaEvaluationError(
1228
+ `${UNEXPECTED_EXCEPTION}The post condition ${constant} cannot be a constant.`,
1229
+ ),
1230
+ EXIT_STATUS.error,
1231
+ );
1232
+ }
884
1233
  const unwitnessed = findUnwitnessedPossible();
885
1234
  if (unwitnessed) return unwitnessed;
886
1235
  for (const name of config.postconditions) {
887
1236
  const failure = getConstantFailure(createIdentifier(name), environment, "a postcondition");
888
- if (failure) {
889
- return { name, message: failure.message ?? `Postcondition ${name} is false.` };
890
- }
1237
+ if (!failure) continue;
1238
+ const range = getBodyRange(name, environment);
1239
+ const location = range ? ` at ${formatRange(range)}` : "";
1240
+ return failure.message === undefined
1241
+ ? {
1242
+ name,
1243
+ code: MESSAGE_CODE.postconditionFalse,
1244
+ message: `Postcondition ${name}${location} is false.`,
1245
+ }
1246
+ : {
1247
+ name,
1248
+ code: MESSAGE_CODE.postconditionFailed,
1249
+ message: `Evaluating postcondition ${name}${location} failed.\n${failure.message}`,
1250
+ };
891
1251
  }
892
1252
  return undefined;
893
1253
  },
@@ -899,38 +1259,179 @@ export const createSearch = (
899
1259
  const withNextStateExitCode = (error: unknown) =>
900
1260
  error instanceof TlaModuleError ? setExitCode(error, error.exitCode) : error;
901
1261
 
902
- const withHeadline = (error: unknown, headline: string) =>
903
- error instanceof TlaEvaluationError
904
- ? setExitCode(
905
- new TlaEvaluationError(`${headline}\n${error.message}`, error),
906
- getErrorExitCode(error),
907
- )
908
- : error;
1262
+ // Stops a replay where TLC stops it without a failure, as at a violated invariant.
1263
+ const STOP_REPLAY = new Error("The replay stopped");
1264
+
1265
+ // The codes of the errors in the configuration and the spec that TLC's EC.ExitStatus maps to a
1266
+ // configuration error.
1267
+ const CONFIG_ERROR_CODES = new Set([
1268
+ ...Array.from({ length: 24 }, (_, index) => 2222 + index),
1269
+ 2272,
1270
+ 2273,
1271
+ 2274,
1272
+ 2280,
1273
+ 2281,
1274
+ ]);
1275
+
1276
+ // When TLC's replay of the initial states doesn't fail again, it exits with the code of the
1277
+ // TLCRuntimeException it reported.
1278
+ const getInitialExitCode = (error: TlaEvaluationError) => {
1279
+ const coded = getCodedMessage(error.message);
1280
+ return coded && !coded.isEvalException && CONFIG_ERROR_CODES.has(coded.code)
1281
+ ? EXIT_STATUS.configParseError
1282
+ : getErrorExitCode(error);
1283
+ };
1284
+
1285
+ // When TLC's replay of an error fails again, an EvalException from a standard module's Java code
1286
+ // decides the exit status, and any other failure exits as a nested expression failure. An error
1287
+ // that heads or wraps another exits as the other.
1288
+ const getReplayExitCode = (error: TlaEvaluationError): number => {
1289
+ const inner = error.cause;
1290
+ if (inner instanceof TlaEvaluationError) return getReplayExitCode(inner);
1291
+ return error instanceof TlaModuleError ? error.exitCode : EXIT_STATUS.specEvaluationFailure;
1292
+ };
1293
+
1294
+ // An error that TLC reports with a message of its own, by that message's code and first line, and
1295
+ // the error whose message follows.
1296
+ interface HeadedError {
1297
+ code: number;
1298
+ headline: string;
1299
+ inner: TlaEvaluationError;
1300
+ }
1301
+
1302
+ const headedErrors = new WeakMap<Error, HeadedError>();
1303
+
1304
+ const withHeadline = (error: unknown, code: number, headline: string) => {
1305
+ if (!(error instanceof TlaEvaluationError)) return error;
1306
+ const headed = setExitCode(
1307
+ new TlaEvaluationError(`${headline}\n${error.message}`, error),
1308
+ getErrorExitCode(error),
1309
+ );
1310
+ headedErrors.set(headed, { code, headline, inner: error });
1311
+ return headed;
1312
+ };
1313
+
1314
+ const UNEXPECTED_HEADLINE = UNEXPECTED_EXCEPTION.slice(0, UNEXPECTED_EXCEPTION.indexOf("\n"));
1315
+
1316
+ // TLC 1.7.4's Worker wraps an error in adding a successor in a RuntimeException, whose message
1317
+ // names the class of the error.
1318
+ const WRAPPED_EVAL_EXCEPTION = `${UNEXPECTED_EXCEPTION}tlc2.tool.EvalException: `;
1319
+ const WRAPPED_RUNTIME_EXCEPTION = `${UNEXPECTED_EXCEPTION}util.Assert$TLCRuntimeException: `;
1320
+
1321
+ const UNEXPECTED_EVAL_EXCEPTION = getUnexpectedException("tlc2.tool.EvalException");
1322
+
1323
+ const getWrappedException = (error: TlaEvaluationError) =>
1324
+ isEvalException(error) ? WRAPPED_EVAL_EXCEPTION : WRAPPED_RUNTIME_EXCEPTION;
1325
+
1326
+ // TLC reports an EvalException, or a TLCRuntimeException without a message of its own, as an
1327
+ // exception it didn't expect.
1328
+ const withUnexpectedException = <Result>(compute: () => Result) => {
1329
+ try {
1330
+ return compute();
1331
+ } catch (error) {
1332
+ if (!(error instanceof TlaEvaluationError)) throw error;
1333
+ if (isEvalException(error)) {
1334
+ throw new TlaEvaluationError(`${UNEXPECTED_EVAL_EXCEPTION}${error.message}`);
1335
+ }
1336
+ if (getCodedMessage(error.message)) throw error;
1337
+ throw new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${error.message}`);
1338
+ }
1339
+ };
1340
+
1341
+ // The starts of TLC's messages for the exceptions it didn't expect.
1342
+ const UNEXPECTED_EXCEPTIONS = [
1343
+ WRAPPED_EVAL_EXCEPTION,
1344
+ WRAPPED_RUNTIME_EXCEPTION,
1345
+ UNEXPECTED_EXCEPTION,
1346
+ UNEXPECTED_EVAL_EXCEPTION,
1347
+ ];
1348
+
1349
+ // The text of an error's message, with the message of a coded error inside it marked in -tool
1350
+ // output as TLC marks it.
1351
+ const formatErrorMessage = (error: TlaEvaluationError, isTool: boolean): string => {
1352
+ const headed = headedErrors.get(error);
1353
+ if (headed) return `${headed.headline}\n${formatErrorMessage(headed.inner, isTool)}`;
1354
+ const message = error.message;
1355
+ const prefix = UNEXPECTED_EXCEPTIONS.find((start) => message.startsWith(start)) ?? "";
1356
+ return `${prefix}${formatNestedMessage(message.slice(prefix.length), isTool)}`;
1357
+ };
1358
+
1359
+ // How TLC reports an error: with the code of its own message for an error that it reports with a
1360
+ // headline; and, when the error escapes the computation of next states, with the code of an
1361
+ // EvalException, or as an unexpected exception for any other error.
1362
+ interface FailureMessage {
1363
+ code: number;
1364
+ message: string;
1365
+ toolMessage: string;
1366
+ }
1367
+
1368
+ const getFailureMessage = (error: TlaEvaluationError, isNextState: boolean): FailureMessage => {
1369
+ const headed = headedErrors.get(error);
1370
+ const message = error.message;
1371
+ if (headed) {
1372
+ return {
1373
+ code: headed.code,
1374
+ message,
1375
+ toolMessage: formatErrorMessage(error, true),
1376
+ };
1377
+ }
1378
+ if (isNextState && isEvalException(error)) {
1379
+ return {
1380
+ code: getCodedMessage(message)?.code ?? MESSAGE_CODE.general,
1381
+ message,
1382
+ toolMessage: message,
1383
+ };
1384
+ }
1385
+ const isUnexpected = isNextState && !message.startsWith(UNEXPECTED_HEADLINE);
1386
+ const prefix = isUnexpected ? UNEXPECTED_EXCEPTION : "";
1387
+ return {
1388
+ code: MESSAGE_CODE.general,
1389
+ message: `${prefix}${message}`,
1390
+ toolMessage: `${prefix}${formatErrorMessage(error, true)}`,
1391
+ };
1392
+ };
909
1393
 
910
1394
  // TLC finishes the -dump file whether or not the search finds a violation.
911
1395
  export const checkSequentially = (options: CheckOptions): CheckResult => {
912
- const stateWriter = options.dump && createStateWriter(options.dump, options.writeFile);
913
- const result = searchStates(options, stateWriter);
914
- stateWriter?.close();
915
- return result;
1396
+ const stateWriter =
1397
+ options.dump && createStateWriter(options.dump, options.writeFile, options.version);
1398
+ try {
1399
+ return searchStates(options, stateWriter);
1400
+ } catch (error) {
1401
+ // TLC reports a stack overflow outside the search on its own, and exits with 255.
1402
+ throw isStackOverflow(error) ? createStackOverflowError() : error;
1403
+ } finally {
1404
+ stateWriter?.close();
1405
+ }
916
1406
  };
917
1407
 
1408
+ const createStackOverflowError = () => setExitCode(new TlaStackOverflowError(), EXIT_STATUS.error);
1409
+
918
1410
  const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefined): CheckResult => {
919
- const config = typeof options.config === "string" ? parseConfig(options.config) : options.config;
1411
+ const config =
1412
+ typeof options.config === "string"
1413
+ ? parseConfig(options.config, options.version)
1414
+ : options.config;
920
1415
  const onPrint = options.onPrint ?? console.log;
921
- // TLC prints again while it rebuilds a trace, between the states it prints. Printing those lines
922
- // before the trace would misplace them, so rebuilding a trace prints nothing.
923
- const output = { isMuted: false };
1416
+ // TLC prints again while it rebuilds a trace, between the states it prints, so rebuilding a trace
1417
+ // keeps what it prints for the trace.
1418
+ const output = { lines: undefined as OutputLine[] | undefined };
924
1419
  const print = (text: string) => {
925
- if (!output.isMuted) onPrint(text);
1420
+ if (output.lines) output.lines.push(text);
1421
+ else onPrint(text);
926
1422
  };
927
- const withoutOutput = <Result>(rebuild: () => Result) => {
928
- output.isMuted = true;
929
- const result = rebuild();
930
- output.isMuted = false;
931
- return result;
1423
+ const getOutput = <Result>(rebuild: () => Result) => {
1424
+ const outer = output.lines;
1425
+ const lines: OutputLine[] = [];
1426
+ output.lines = lines;
1427
+ try {
1428
+ return { result: rebuild(), lines };
1429
+ } finally {
1430
+ output.lines = outer;
1431
+ }
932
1432
  };
933
1433
  setStringOrder(getTlcStringOrder(options.source, config, options.loadModule));
1434
+ setValueVersion(options.version);
934
1435
  const specification = createEnvironment(config, {
935
1436
  source: options.source,
936
1437
  loadModule: options.loadModule,
@@ -938,9 +1439,16 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
938
1439
  print,
939
1440
  files: { readFile: options.readFile, writeFile: options.writeFile },
940
1441
  seed: options.seed,
1442
+ isTool: options.isTool,
1443
+ isTraceRecorded: options.isTraceRecorded,
1444
+ isSanyReported: options.isSanyReported,
1445
+ isWarningEnabled: options.isWarningEnabled,
1446
+ version: options.version,
941
1447
  });
1448
+ reportStartup(specification, config, options.onProgress);
942
1449
  const environment = specification.environment;
943
1450
  const runtime = environment.runtime;
1451
+ const is174 = runtime.version === "1.7.4";
944
1452
  runtime.pause = options.onPause ?? (() => {});
945
1453
  runtime.getSpecification = () =>
946
1454
  getSpecificationValue(
@@ -949,7 +1457,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
949
1457
  specification.variableLocations,
950
1458
  specification.instanceActions,
951
1459
  );
952
- const statistics = { depth: 0 };
1460
+ const statistics = { depth: 0, initialStates: undefined as InitialStates | undefined };
953
1461
  const createResult = (
954
1462
  status: CheckStatus,
955
1463
  violation?: Omit<Violation, "loopStart"> & { loopStart?: number },
@@ -962,15 +1470,26 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
962
1470
  distinctStates: runtime.distinctStates,
963
1471
  queuedStates: runtime.queueSize,
964
1472
  depth: runtime.generatedStates === 0 ? 0 : Math.max(statistics.depth, 1),
1473
+ initialStates: statistics.initialStates,
965
1474
  outDegrees: undefined,
966
1475
  fingerprintDistance: undefined,
967
1476
  });
968
1477
  const outDegrees = Array.from({ length: OUT_DEGREE_BUCKETS }, () => 0);
969
1478
  const expansion = { newStates: 0 };
970
1479
  const getOutDegreeBucket = () => Math.min(expansion.newStates, OUT_DEGREE_BUCKETS - 1);
971
- // TLC counts the state it was expanding when an evaluation failed.
1480
+ // TLC counts the state it was expanding when an evaluation failed, except TLC 1.7.4, whose
1481
+ // worker stops before it counts the state.
972
1482
  const getPartialOutDegrees = () =>
973
1483
  outDegrees.map((count, degree) => (degree === getOutDegreeBucket() ? count + 1 : count));
1484
+ const getFailureOutDegrees = () => (is174 ? [...outDegrees] : getPartialOutDegrees());
1485
+ // TLC 1.7.4 doesn't name the properties a counterexample violates, so it evaluates none of them.
1486
+ const getViolatedProperties = (checker: LivenessChecker, path: Value[][], cycleStart: number) =>
1487
+ is174 ? [] : checker.getViolatedProperties(path, cycleStart);
1488
+ // A violation in a successor ends the expansion of a state before TLC counts it.
1489
+ const withOutDegrees = (result: CheckResult, degrees = [...outDegrees]): CheckResult => ({
1490
+ ...result,
1491
+ violationOutDegrees: degrees,
1492
+ });
974
1493
  const createSuccess = (): CheckResult => ({
975
1494
  ...createResult("success"),
976
1495
  outDegrees: [...outDegrees],
@@ -984,64 +1503,235 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
984
1503
  ),
985
1504
  });
986
1505
 
987
- const search = createSearch(config, environment, () =>
988
- getTlcVariableOrder(options.source, options.loadModule),
1506
+ const search = createSearch(
1507
+ config,
1508
+ environment,
1509
+ () => getTlcVariableOrder(options.source, options.loadModule),
1510
+ options.onProgress,
989
1511
  );
990
- const assumptionViolation = findAssumptionViolation(specification.assumptions, environment);
991
- if (assumptionViolation) return createResult("assumption-violation", assumptionViolation);
992
- if (!search) return createSuccess();
1512
+ const findViolatedAssumption = () =>
1513
+ findAssumptionViolation(specification.assumptions, environment);
1514
+ if (!search) {
1515
+ runtime.isStarted = true;
1516
+ const assumptionViolation = findViolatedAssumption();
1517
+ if (assumptionViolation) return createResult("assumption-violation", assumptionViolation);
1518
+ // TLC computes the initial states of a spec without variables, of which there are none.
1519
+ statistics.initialStates = { generated: 0, distinct: 0 };
1520
+ options.onProgress?.({ kind: "initial-states-started" });
1521
+ options.onProgress?.({
1522
+ kind: "initial-states-computed",
1523
+ initialStates: statistics.initialStates,
1524
+ });
1525
+ return createSuccess();
1526
+ }
993
1527
  const hasPostconditions = config.postconditions.length > 0 || config.possible.length > 0;
994
- const reportViolation = (
995
- status: CheckStatus,
996
- violation: Omit<Violation, "loopStart"> & { loopStart?: number },
997
- getLoopAction?: () => string | undefined,
1528
+ // The state of each step of a trace without the alias, and the action TLC finds for the step,
1529
+ // which CounterExample records.
1530
+ const traceSteps = new WeakMap<
1531
+ TraceStep,
1532
+ { state: Record<string, Value>; action: SpecAction | undefined }
1533
+ >();
1534
+ const getStepState = (step: TraceStep) => traceSteps.get(step)?.state ?? step.state;
1535
+ const getStepAction = (step: TraceStep) => {
1536
+ const action = traceSteps.get(step)?.action;
1537
+ return action ? toActionValue(action) : createActionRecord(step.action);
1538
+ };
1539
+ // Worker#printTrace checks the postconditions on the CounterExample of each trace it prints, so
1540
+ // what they print follows the trace.
1541
+ const checkTracePostconditions = (
1542
+ trace: TraceStep[],
1543
+ loop: { start: number; action: Value } | undefined,
998
1544
  ) => {
999
1545
  if (hasPostconditions) {
1000
- const start = violation.loopStart;
1001
1546
  runtime.counterExample = createCounterExample(
1002
- violation.trace,
1003
- start === undefined || !getLoopAction ? undefined : { start, action: getLoopAction() },
1547
+ trace.map(getStepState),
1548
+ trace.slice(1).map(getStepAction),
1549
+ loop,
1004
1550
  );
1005
1551
  }
1006
1552
  const unwitnessed = search.findUnwitnessedPossible();
1007
- if (!unwitnessed) search.findPostconditionViolation();
1008
- return createResult(status, unwitnessed ? { ...violation, unwitnessed } : violation);
1553
+ const checked = unwitnessed
1554
+ ? { result: undefined, lines: [] }
1555
+ : getOutput(() => search.findPostconditionViolation(true));
1556
+ return { unwitnessed, postcondition: checked.result, lines: checked.lines };
1557
+ };
1558
+ const reportViolation = (
1559
+ status: CheckStatus,
1560
+ violation: Omit<Violation, "loopStart"> & { loopStart?: number },
1561
+ getLoopAction?: () => Value,
1562
+ ) => {
1563
+ const start = violation.loopStart;
1564
+ const checked = checkTracePostconditions(
1565
+ violation.trace,
1566
+ start === undefined || !getLoopAction ? undefined : { start, action: getLoopAction() },
1567
+ );
1568
+ const traceOutput = [...(violation.traceOutput ?? []), ...checked.lines];
1569
+ return createResult(status, {
1570
+ ...violation,
1571
+ ...(traceOutput.length > 0 && { traceOutput }),
1572
+ ...(checked.unwitnessed && { unwitnessed: checked.unwitnessed }),
1573
+ ...(checked.postcondition && { postcondition: checked.postcondition }),
1574
+ });
1009
1575
  };
1010
1576
  const spec = search.spec;
1011
1577
  const getFingerprintState = search.getFingerprintState;
1012
1578
  const nextActions = { value: undefined as SpecAction[] | undefined };
1579
+ const getSpecNextActions = () =>
1580
+ (nextActions.value ??= getNextActions(config, environment, specification.instanceActions));
1581
+ // Where TLC's replay of a failed computation fails, or undefined when the replay completes.
1582
+ const getReplayFailure = (replay: (tracing: Environment) => void): ReplayFailure | undefined => {
1583
+ const tracing = createTracingEnvironment(environment);
1584
+ const callStack = tracing.callStack;
1585
+ try {
1586
+ replay(tracing);
1587
+ return undefined;
1588
+ } catch (error) {
1589
+ // A replay that overflows the stack again has an empty call stack.
1590
+ if (isStackOverflow(error)) return { callStack: [], fingerprint: undefined };
1591
+ if (!(error instanceof TlaEvaluationError) || !callStack) return undefined;
1592
+ return { callStack: callStack.expressions, fingerprint: callStack.fingerprint };
1593
+ }
1594
+ };
1595
+ // The sources of the values a fingerprint fails inside, outermost first: each value fails as it
1596
+ // fingerprints a value it holds.
1597
+ const findFailedSources = (value: Value, sources: CallStack["sources"]): Expression[] => {
1598
+ if (typeof value !== "object") return [];
1599
+ const source = sources.get(value);
1600
+ const failed = getHeldValues(value).find((held) => getFingerprintError(held) !== undefined);
1601
+ return [
1602
+ ...(source ? [source] : []),
1603
+ ...(failed === undefined ? [] : findFailedSources(failed, sources)),
1604
+ ];
1605
+ };
1606
+ // TLC's replay fingerprints the states it finds, and a value the replay built raises a
1607
+ // FingerprintException when it fails to.
1608
+ const fingerprintReplayed = (state: Value[], callStack: CallStack | undefined) => {
1609
+ for (const value of getFingerprintState(state)) {
1610
+ const error = getFingerprintError(value);
1611
+ if (!error) continue;
1612
+ const sources = callStack ? findFailedSources(value, callStack.sources) : [];
1613
+ // TLC fingerprints a state outside any evaluation, so its call stack is empty.
1614
+ if (callStack && sources.length > 0) {
1615
+ callStack.expressions.length = 0;
1616
+ callStack.isFrozen = true;
1617
+ callStack.fingerprint = { sources, reason: error.message };
1618
+ }
1619
+ throw error;
1620
+ }
1621
+ state.forEach(markFingerprinted);
1622
+ };
1623
+ // The replay adds the states it hasn't seen to TLC's set of states, which already holds the state
1624
+ // that failed after TLC added it.
1625
+ const createReplayedStates = () => {
1626
+ const replayed = unstoredState.fingerprint ? [unstoredState.fingerprint] : [];
1627
+ return (state: Value[], callStack: CallStack | undefined) => {
1628
+ fingerprintReplayed(state, callStack);
1629
+ const fingerprintState = getFingerprintState(state);
1630
+ const isSeen =
1631
+ findStateIndex(getStateHash(fingerprintState), fingerprintState) !== -1 ||
1632
+ replayed.some((seen) => isSameState(seen, fingerprintState));
1633
+ if (isSeen) return false;
1634
+ replayed.push(fingerprintState);
1635
+ runtime.distinctStates++;
1636
+ return true;
1637
+ };
1638
+ };
1639
+ // TLC's DoInitFunctor reports its progress again as it replays the initial states, and adds each
1640
+ // one it hasn't seen to the behavior graph. After a FingerprintException, it only counts the
1641
+ // states left, then throws the exception.
1642
+ const replayInit = () => {
1643
+ const replayed = { count: 0 };
1644
+ const deferred: TlaEvaluationError[] = [];
1645
+ const addReplayed = createReplayedStates();
1646
+ return getReplayFailure((tracing) => {
1647
+ replayInitialStates(
1648
+ getInitActions(config, environment, specification.instanceActions),
1649
+ tracing,
1650
+ (state) => {
1651
+ reportInitialStatesProgress(replayed.count, (progress) => output.lines?.push(progress));
1652
+ replayed.count++;
1653
+ runtime.generatedStates = replayed.count;
1654
+ if (deferred.length > 0) return;
1655
+ try {
1656
+ const addFingerprint = () => {
1657
+ if (!addReplayed(state, tracing.callStack)) return;
1658
+ runtime.queueSize++;
1659
+ liveness?.addInitialState(states.length, state, tracing);
1660
+ };
1661
+ const violation = search.checkReplayedInitialState(state, tracing, addFingerprint);
1662
+ if (violation) {
1663
+ const traced = toTrace([{ action: undefined, state }], []).trace[0].state;
1664
+ output.lines?.push({ kind: "initial-state-violation", ...violation, state: traced });
1665
+ throw STOP_REPLAY;
1666
+ }
1667
+ } catch (error) {
1668
+ if (!(error instanceof TlaEvaluationError) || !tracing.callStack?.fingerprint) {
1669
+ throw error;
1670
+ }
1671
+ deferred.push(error);
1672
+ }
1673
+ },
1674
+ );
1675
+ if (deferred.length > 0) throw deferred[0];
1676
+ });
1677
+ };
1678
+ const replayNext = (state: Value[]) =>
1679
+ getReplayFailure((tracing) => {
1680
+ const addReplayed = createReplayedStates();
1681
+ replaySuccessors(getSpecNextActions(), state, tracing, (next) => {
1682
+ const addFingerprint = () => addReplayed(next, tracing.callStack);
1683
+ if (!search.checkReplayedSuccessor(state, next, tracing, addFingerprint)) {
1684
+ throw STOP_REPLAY;
1685
+ }
1686
+ });
1687
+ });
1013
1688
  const isReducing = config.view !== undefined || config.symmetry !== undefined;
1014
1689
  const tlcFingerprints: number[] = [];
1690
+ // TLC warns about the liveness checker before it builds it.
1691
+ if (search.properties.liveness.length > 0) {
1692
+ options.onProgress?.({
1693
+ kind: "liveness-initializing",
1694
+ isSymmetric: search.isSymmetric,
1695
+ isConstrained: search.isConstrained,
1696
+ });
1697
+ }
1015
1698
  const liveness = withLivenessExitCode(() =>
1016
- createLivenessChecker(search.properties.liveness, spec.temporals, environment, {
1017
- getFingerprint: (index) =>
1018
- (tlcFingerprints[index] ??= Number(
1019
- BigInt.asIntN(32, runtime.getFingerprint(states[index])),
1020
- )),
1021
- getSuccessors: (state) => {
1022
- const successors: LiveSuccessor[] = [];
1023
- if (spec.next === undefined) return successors;
1024
- forEachSuccessor(
1025
- spec.next,
1026
- state,
1027
- environment,
1028
- (successor) => {
1029
- canonicalizeState(successor.state);
1030
- const isInModel =
1031
- search.satisfiesConstraints(successor.state, state) &&
1032
- search.satisfiesActionConstraints(state, successor.state);
1033
- if (!isInModel) return false;
1034
- const fingerprintState = getFingerprintState(successor.state);
1035
- const index = findStateIndex(getStateHash(fingerprintState), fingerprintState);
1036
- successors.push({ index, state: successor.state });
1037
- return false;
1038
- },
1039
- spec.scope,
1040
- );
1041
- return successors;
1042
- },
1043
- }),
1699
+ withUnexpectedException(() =>
1700
+ createLivenessChecker(search.properties.liveness, spec.temporals, environment, {
1701
+ getFingerprint: ({ index, state }) =>
1702
+ (tlcFingerprints[index] ??= Number(BigInt.asIntN(32, runtime.getFingerprint(state)))),
1703
+ getSuccessors: (source, state) => {
1704
+ const successors: LiveSuccessor[] = [];
1705
+ if (spec.next === undefined) return successors;
1706
+ // The constraints read the level of the state whose successors they check, as when TLC
1707
+ // explored it.
1708
+ const livenessSource = current.livenessSource;
1709
+ current.livenessSource = source;
1710
+ forEachSuccessor(
1711
+ spec.next,
1712
+ state,
1713
+ environment,
1714
+ (successor) => {
1715
+ canonicalizeState(successor.state);
1716
+ const isInModel =
1717
+ search.satisfiesConstraints(successor.state, state) &&
1718
+ search.satisfiesActionConstraints(state, successor.state);
1719
+ if (!isInModel) return false;
1720
+ const fingerprintState = getFingerprintState(successor.state);
1721
+ const index = findStateIndex(getStateHash(fingerprintState), fingerprintState);
1722
+ successors.push({ index, state: successor.state });
1723
+ return false;
1724
+ },
1725
+ spec.scope,
1726
+ );
1727
+ current.livenessSource = livenessSource;
1728
+ return successors;
1729
+ },
1730
+ }),
1731
+ ),
1044
1732
  );
1733
+ // TLC 1.7.4 creates its model checker, which TLCGet reads, once it has built the liveness checker.
1734
+ runtime.isStarted = true;
1045
1735
  const isCanonicalizedFirst = search.isSymmetric || liveness !== undefined;
1046
1736
 
1047
1737
  const states: Value[][] = [];
@@ -1081,58 +1771,107 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1081
1771
  return toAliasRecord(value, fieldOrder) ?? toRecord(state);
1082
1772
  } catch (error) {
1083
1773
  if (!(error instanceof TlaConfigError || error instanceof TlaEvaluationError)) throw error;
1084
- return { ...toRecord(state), _ALIASEvalError: error.message };
1774
+ return {
1775
+ ...toRecord(state),
1776
+ _ALIASEvalError: formatNestedMessage(error.message, options.isTool ?? false),
1777
+ };
1085
1778
  }
1086
1779
  };
1087
- // TLC labels a step with the first action that takes it, evaluated as when the search expanded
1088
- // the previous state.
1089
- const getLabel = (index: number, previous: Value[], state: Value[]) => {
1090
- nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
1780
+ // TLC finds the action of a step as the first action that takes it, evaluated as when the search
1781
+ // expanded the previous state.
1782
+ // TLC finds the action of a successor it holds by the successor's values, and of any other state
1783
+ // by its fingerprint.
1784
+ const findStepAction = (index: number, previous: Value[], state: Value[], isHeld: boolean) => {
1785
+ const actions = getSpecNextActions();
1091
1786
  const expanding = { ...current };
1092
1787
  Object.assign(current, { index, isExpanding: true, candidate: undefined });
1093
- const fingerprintState = getFingerprintState(state);
1094
- const action = nextActions.value.find((item) =>
1788
+ const getKey = isHeld ? (values: Value[]) => values : getFingerprintState;
1789
+ const key = getKey(state);
1790
+ const action = actions.find((item) =>
1095
1791
  getSuccessors(item.expression, previous, environment, item.scope).some((successor) =>
1096
- isSameState(getFingerprintState(successor.state), fingerprintState),
1792
+ isSameState(getKey(successor.state), key),
1097
1793
  ),
1098
1794
  );
1099
1795
  Object.assign(current, expanding);
1100
- return action && getActionLabel(action);
1796
+ return action;
1101
1797
  };
1102
- const toTrace = (steps: Successor[], path: number[], loopStart?: number): TraceStep[] => {
1798
+ // Before TLC prints a trace, it rebuilds from the initial predicate the states it does not keep,
1799
+ // all but the last `known` ones. As it prints each later step, it finds the action that takes
1800
+ // the step. Then it rebuilds the trace once more, unless the trace is a liveness counterexample
1801
+ // or TLC is 1.7.4.
1802
+ const toTrace = (
1803
+ steps: Successor[],
1804
+ path: number[],
1805
+ traceOptions: TraceOptions = {},
1806
+ ): Behavior => {
1103
1807
  const alias = config.alias;
1104
- const labels = withoutOutput(() => {
1105
- // TLC rebuilds a trace from the initial predicate, which can reset what TLCSet stored.
1106
- if (steps.length > 1 && isImpure(environment)) getSpecInitialStates();
1107
- // TLC finds the action that takes each step before it evaluates the alias.
1108
- return steps.map((step, position) =>
1109
- position === 0 || step.action === "Stuttering"
1110
- ? undefined
1111
- : getLabel(path[position - 1], steps[position - 1].state, step.state),
1112
- );
1113
- });
1114
- return steps.map((step, position) => ({
1115
- action: step.action,
1116
- label: labels[position],
1117
- state:
1118
- alias === undefined
1119
- ? toRecord(step.state)
1120
- : getAliasState(alias, steps, position, loopStart),
1121
- }));
1808
+ const known = traceOptions.known ?? 1;
1809
+ // Rebuilding the trace from the initial predicate can reset what TLCSet stored.
1810
+ const initialOutput = steps.length > 1 ? getOutput(getSpecInitialStates).lines : [];
1811
+ // TLC finds the action that takes each step before it evaluates the alias.
1812
+ const found = steps.map((step, position) =>
1813
+ position === 0 || step.action === "Stuttering"
1814
+ ? { result: undefined, lines: [] }
1815
+ : getOutput(() =>
1816
+ findStepAction(
1817
+ path[position - 1],
1818
+ steps[position - 1].state,
1819
+ step.state,
1820
+ known === 2 && position === steps.length - 1,
1821
+ ),
1822
+ ),
1823
+ );
1824
+ const outputs = found.map((step) => step.lines);
1825
+ const loopStart = traceOptions.loopStart;
1826
+ // 1.7.4's LiveWorker#printTrace rebuilds a lasso up to the start of its cycle at once, and then
1827
+ // each state of the cycle as it prints it.
1828
+ const rebuilt = is174 && loopStart !== undefined ? loopStart + 1 : steps.length - known;
1829
+ const prefixOutput = rebuilt > 0 ? [initialOutput, ...outputs.slice(1, rebuilt)].flat() : [];
1830
+ return {
1831
+ trace: steps.map((step, position) => {
1832
+ const action = found[position].result;
1833
+ const traceStep: TraceStep = {
1834
+ action: step.action,
1835
+ label: action && getActionLabel(action, options.version),
1836
+ state:
1837
+ alias === undefined
1838
+ ? toRecord(step.state)
1839
+ : getAliasState(alias, steps, position, loopStart),
1840
+ output: position === 0 ? prefixOutput : position < rebuilt ? [] : outputs[position],
1841
+ };
1842
+ if (alias !== undefined || action) {
1843
+ traceSteps.set(traceStep, { state: toRecord(step.state), action });
1844
+ }
1845
+ return traceStep;
1846
+ }),
1847
+ traceOutput: known > 0 && !is174 ? [initialOutput, ...outputs].flat() : [],
1848
+ };
1122
1849
  };
1123
1850
  const getSpecInitialStates = () =>
1124
1851
  spec.init === undefined ? [] : getInitialStates(spec.init, environment, spec.scope);
1125
- const getTraceSuccessors = (state: Value[]) =>
1126
- spec.next === undefined ? [] : getSuccessors(spec.next, state, environment, spec.scope);
1852
+ // TLC's Tool#getState rebuilds a successor action by action, up to the first action that
1853
+ // generates it. A trace keeps what the spec prints as TLC rebuilds it, so finding its states
1854
+ // prints nothing.
1855
+ const findTraceSuccessor = (state: Value[], isMatch: (successor: Successor) => boolean) =>
1856
+ getOutput(() => {
1857
+ for (const action of getSpecNextActions()) {
1858
+ const successor = getSuccessors(action.expression, state, environment, action.scope).find(
1859
+ isMatch,
1860
+ );
1861
+ if (successor) return { ...successor, action: successor.action ?? action.name };
1862
+ }
1863
+ return undefined;
1864
+ }).result;
1127
1865
  const hasFingerprint = (state: Value[], index: number) =>
1128
1866
  isSameState(getFingerprintState(state), fingerprintStates[index]);
1129
1867
  const findSuccessorWithFingerprint = (state: Value[], index: number) =>
1130
- getTraceSuccessors(state).find((successor) => hasFingerprint(successor.state, index));
1868
+ findTraceSuccessor(state, (successor) => hasFingerprint(successor.state, index));
1131
1869
  const getPathStates = (path: number[], nextFailure: string) => {
1132
- const initial =
1870
+ const initial = getOutput(() =>
1133
1871
  isReducing || isImpure(environment)
1134
1872
  ? getSpecInitialStates().find((state) => hasFingerprint(state, path[0]))
1135
- : states[path[0]];
1873
+ : states[path[0]],
1874
+ ).result;
1136
1875
  if (!initial) throw new TlaEvaluationError(INITIAL_RECOVERY_FAILURE);
1137
1876
  const steps: Successor[] = [{ state: initial, action: undefined }];
1138
1877
  for (let position = 1; position < path.length; position++) {
@@ -1158,36 +1897,119 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1158
1897
  : path.map((position) => ({ state: states[position], action: actions[position] }));
1159
1898
  if (!last) return toTrace(steps, path);
1160
1899
  const action = isReducing
1161
- ? getTraceSuccessors(states[index]).find((successor) =>
1162
- isSameState(successor.state, last.state),
1163
- )?.action
1900
+ ? findTraceSuccessor(states[index], (successor) => isSameState(successor.state, last.state))
1901
+ ?.action
1164
1902
  : last.action;
1165
- return toTrace([...steps, { state: last.state, action }], path);
1903
+ return toTrace([...steps, { state: last.state, action }], path, { known: 2 });
1166
1904
  };
1167
1905
  // TLC prints an initial state on its own without evaluating the alias.
1168
- const getStateTrace = (index: number) =>
1906
+ // TLCTrace#getTrace recovers even a lone initial state from the initial predicate, printing
1907
+ // again, though TLC 1.7.4 doesn't.
1908
+ const getStateTrace = (index: number): Behavior =>
1169
1909
  parents[index] === -1
1170
- ? [{ action: undefined, label: undefined, state: toRecord(states[index]) }]
1910
+ ? {
1911
+ trace: [{ action: undefined, label: undefined, state: toRecord(states[index]) }],
1912
+ traceOutput: is174 ? [] : getOutput(getSpecInitialStates).lines,
1913
+ }
1171
1914
  : getTrace(index);
1915
+ // TLC rebuilds the trace again for the postcondition after it prints a violation, but not after
1916
+ // an evaluation fails.
1917
+ const getFailedTrace = (index: number, successor: Successor): Behavior => ({
1918
+ trace: getTrace(index, successor).trace,
1919
+ });
1172
1920
  // TLC names the initial state it was checking unless a standard module's Java code failed.
1173
- const toFailure = (
1174
- error: unknown,
1175
- trace: TraceStep[],
1176
- initialState?: Value[],
1177
- failureOutDegrees?: number[],
1178
- ) => {
1921
+ const getFailureExitCode = (error: TlaEvaluationError, failureOptions: FailureOptions) => {
1922
+ // TLC 1.7.4 exits with the result of its replay of the initial states, which only fails again
1923
+ // when the error isn't in adding a state it has already seen to the behavior graph.
1924
+ if (
1925
+ failureOptions.isInitial &&
1926
+ !failureOptions.callStack &&
1927
+ !failureOptions.fingerprint &&
1928
+ is174
1929
+ ) {
1930
+ return EXIT_STATUS.success;
1931
+ }
1932
+ if (!failureOptions.callStack) {
1933
+ return failureOptions.isInitial ? getInitialExitCode(error) : getErrorExitCode(error);
1934
+ }
1935
+ if (failureOptions.isCallStackFirst) return getErrorExitCode(error);
1936
+ // Without a replay, TLC exits with the code of the error, as for any error it didn't expect.
1937
+ if (failureOptions.isLiveCallStack) {
1938
+ return (
1939
+ getExitCode(error) ?? (error instanceof TlaModuleError ? error.exitCode : EXIT_STATUS.error)
1940
+ );
1941
+ }
1942
+ // TLC exits with the code of a failed replay of the initial states regardless of the error, as
1943
+ // it does for a replay that fails to fingerprint.
1944
+ return failureOptions.isInitial || failureOptions.fingerprint
1945
+ ? EXIT_STATUS.specEvaluationFailure
1946
+ : getReplayExitCode(error);
1947
+ };
1948
+ const toFailure = (failed: unknown, behavior: Behavior, failureOptions: FailureOptions = {}) => {
1949
+ const error = isStackOverflow(failed) ? new TlaStackOverflowError() : failed;
1179
1950
  if (!(error instanceof TlaEvaluationError)) throw error;
1951
+ const checked =
1952
+ behavior.trace.length > 0 && !failureOptions.traceFailure
1953
+ ? checkTracePostconditions(behavior.trace, undefined)
1954
+ : undefined;
1955
+ const postcondition = checked?.unwitnessed ?? checked?.postcondition;
1956
+ const traceOutput = [...(behavior.traceOutput ?? []), ...(checked?.lines ?? [])];
1180
1957
  return {
1181
1958
  ...createResult("error", undefined, {
1182
- message: error.message,
1183
- trace,
1959
+ ...getFailureMessage(error, failureOptions.isNextState ?? false),
1960
+ ...behavior,
1961
+ ...(traceOutput.length > 0 && { traceOutput }),
1962
+ ...(postcondition && { postcondition }),
1184
1963
  initialState:
1185
- initialState && !(error instanceof TlaModuleError) ? toRecord(initialState) : undefined,
1186
- exitCode: getErrorExitCode(error),
1964
+ failureOptions.initialState && !(error instanceof TlaModuleError)
1965
+ ? toRecord(failureOptions.initialState)
1966
+ : undefined,
1967
+ exitCode: getFailureExitCode(error, failureOptions),
1968
+ callStack: failureOptions.callStack,
1969
+ ...(failureOptions.isCallStackFirst && { isCallStackFirst: true }),
1970
+ ...(failureOptions.fingerprint && { fingerprint: failureOptions.fingerprint }),
1971
+ ...(failureOptions.traceFailure && { traceFailure: failureOptions.traceFailure }),
1187
1972
  }),
1188
- outDegrees: failureOutDegrees,
1973
+ outDegrees: failureOptions.outDegrees,
1189
1974
  };
1190
1975
  };
1976
+ // TLC reports a successor state that an action leaves incomplete with the trace to it, which ends
1977
+ // with the partial state. It names the action when the next-state relation has several.
1978
+ const reportIncompleteSuccessor = (index: number, error: TlaIncompleteSuccessorError) => {
1979
+ runtime.generatedStates++;
1980
+ const actions = getSpecNextActions();
1981
+ const action =
1982
+ actions.length === 1 ? actions[0] : actions.find((item) => item.name === error.action);
1983
+ const missing = error.variables;
1984
+ const subject =
1985
+ actions.length === 1
1986
+ ? "the next-state action"
1987
+ : `action ${error.action} of the next-state relation`;
1988
+ // TLC 1.7.4 says a variable is not assigned rather than not defined.
1989
+ const verb = is174 ? "assigned" : "defined";
1990
+ const message = `Successor state is not completely specified by ${subject}. The following variable${missing.length > 1 ? "s are" : " is"} not ${verb}: ${missing.join(", ")}.\n`;
1991
+ const undefinedValue = createModelValue("null");
1992
+ const partial = error.state.map((value) => value ?? undefinedValue);
1993
+ const failure = toFailure(error, getStateTrace(index));
1994
+ return withOutDegrees({
1995
+ ...failure,
1996
+ failure: failure.failure && {
1997
+ ...failure.failure,
1998
+ code: MESSAGE_CODE.incompleteSuccessor,
1999
+ message,
2000
+ toolMessage: message,
2001
+ exitCode: EXIT_STATUS.specEvaluationFailure,
2002
+ trace: [
2003
+ ...failure.failure.trace,
2004
+ {
2005
+ action: error.action,
2006
+ label: action && getActionLabel(action, options.version),
2007
+ state: toRecord(partial),
2008
+ },
2009
+ ],
2010
+ },
2011
+ });
2012
+ };
1191
2013
  const getPathSteps = (path: number[], loopStart?: number) => {
1192
2014
  const steps = getPathStates(path, NEXT_RECOVERY_FAILURE);
1193
2015
  const last = steps[steps.length - 1].state;
@@ -1205,6 +2027,13 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1205
2027
  hash = combineHash(hash, getValueHash(state[index]));
1206
2028
  return hash & 0x3fffffff;
1207
2029
  };
2030
+ // TLC fingerprints a state once it finds the state in the model.
2031
+ const fingerprintModelState = (state: Value[]) => {
2032
+ for (const value of state) {
2033
+ const error = getFingerprintError(value);
2034
+ if (error) throw error;
2035
+ }
2036
+ };
1208
2037
  const isSameState = (stored: Value[], state: Value[]) => {
1209
2038
  for (let position = 0; position < state.length; position++) {
1210
2039
  if (!isSameValue(stored[position], state[position])) return false;
@@ -1241,6 +2070,9 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1241
2070
  }
1242
2071
  insertSeen(hash, index + 1);
1243
2072
  };
2073
+ // A new successor that TLC has added to its set of states before it checks the successor, which
2074
+ // the check stores after.
2075
+ const unstoredState = { fingerprint: undefined as Value[] | undefined };
1244
2076
  const countDistinctState = (depth: number) => {
1245
2077
  runtime.distinctStates = states.length + 1;
1246
2078
  statistics.depth = Math.max(statistics.depth, depth);
@@ -1283,6 +2115,11 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1283
2115
  };
1284
2116
  runtime.getLevel = (state) => {
1285
2117
  const source = current.livenessSource;
2118
+ // TLC 1.7.4 reads the level of the state its worker explores, whichever state it evaluates.
2119
+ if (is174) {
2120
+ const explored = source !== -1 ? source : current.isExpanding ? current.index : -1;
2121
+ return explored === -1 ? 0 : depths[explored];
2122
+ }
1286
2123
  if (source !== -1) return state === states[source] ? depths[source] : depths[source] + 1;
1287
2124
  if (state === current.candidate) return current.candidateLevel;
1288
2125
  if (current.index === -1) return 0;
@@ -1311,9 +2148,14 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1311
2148
  const fingerprintState = getFingerprintState(state);
1312
2149
  const hash = getStateHash(fingerprintState);
1313
2150
  const isInModel = search.satisfiesConstraints(state);
1314
- if (isInModel) search.trackStatePossible(state);
2151
+ if (isInModel) {
2152
+ search.trackStatePossible(state);
2153
+ fingerprintModelState(fingerprintState);
2154
+ state.forEach(markFingerprinted);
2155
+ }
1315
2156
  const isSeen = isInModel && findStateIndex(hash, fingerprintState) !== -1;
1316
2157
  if (isSeen) return undefined;
2158
+ unstoredState.fingerprint = isInModel ? fingerprintState : undefined;
1317
2159
  if (isInModel) {
1318
2160
  countDistinctState(1);
1319
2161
  runtime.queueSize++;
@@ -1324,14 +2166,14 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1324
2166
  if (violated) {
1325
2167
  return reportViolation("invariant-violation", {
1326
2168
  name: violated.name,
1327
- trace: toTrace([{ action: undefined, state }], []),
2169
+ ...toTrace([{ action: undefined, state }], []),
1328
2170
  });
1329
2171
  }
1330
2172
  const failedPredicate = search.findFailedInitialPredicate(state);
1331
2173
  if (failedPredicate) {
1332
2174
  return reportViolation("liveness-violation", {
1333
2175
  name: failedPredicate.name,
1334
- trace: toTrace([{ action: undefined, state }], []),
2176
+ ...toTrace([{ action: undefined, state }], []),
1335
2177
  });
1336
2178
  }
1337
2179
  if (!isInModel) return undefined;
@@ -1357,7 +2199,11 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1357
2199
  const isStateInModel = search.satisfiesConstraints(successor.state, state);
1358
2200
  if (isStateInModel) search.trackStatePossible(successor.state);
1359
2201
  const isInModel = isStateInModel && search.satisfiesActionConstraints(state, successor.state);
1360
- if (isInModel) search.trackActionPossible(state, successor.state);
2202
+ if (isInModel) {
2203
+ search.trackActionPossible(state, successor.state);
2204
+ fingerprintModelState(fingerprintState);
2205
+ successor.state.forEach(markFingerprinted);
2206
+ }
1361
2207
  const existing = isInModel ? found : -1;
1362
2208
  if (source && isInModel) {
1363
2209
  stateWriter?.writeSuccessor(
@@ -1373,6 +2219,9 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1373
2219
  liveSuccessors.push({ index: existing, state: isStored ? stored : successor.state });
1374
2220
  }
1375
2221
  const isNew = isInModel && existing === -1;
2222
+ // TLC adds a new state to the behavior graph's successors before it checks the invariants.
2223
+ if (isNew && liveness) liveSuccessors.push({ index: states.length, state: successor.state });
2224
+ unstoredState.fingerprint = isNew ? fingerprintState : undefined;
1376
2225
  if (isNew) {
1377
2226
  countDistinctState(depths[index] + 1);
1378
2227
  expansion.newStates++;
@@ -1381,53 +2230,105 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1381
2230
  try {
1382
2231
  const violated = search.findViolatedInvariant(successor.state, state);
1383
2232
  if (violated) {
1384
- return reportViolation("invariant-violation", {
1385
- name: violated.name,
1386
- trace: getTrace(index, successor),
1387
- });
2233
+ return withOutDegrees(
2234
+ reportViolation("invariant-violation", {
2235
+ name: violated.name,
2236
+ ...getTrace(index, successor),
2237
+ }),
2238
+ );
1388
2239
  }
1389
2240
  } catch (error) {
1390
- // TLC prints the outdegree when it cannot reproduce the failure, as for an invariant here.
1391
- return toFailure(error, getTrace(index, successor), undefined, getPartialOutDegrees());
2241
+ if (isStackOverflow(error)) throw error;
2242
+ // TLC replays the expansion and skips the invariants of a state it has seen, the failed one
2243
+ // included unless the constraints exclude it. When the replay fails, TLC reports where and
2244
+ // leaves out the outdegree.
2245
+ const replay = getOutput(() => replayNext(state));
2246
+ return toFailure(
2247
+ error,
2248
+ appendTraceOutput(getFailedTrace(index, successor), replay.lines),
2249
+ replay.result ?? { outDegrees: getFailureOutDegrees() },
2250
+ );
1392
2251
  }
1393
2252
  }
1394
2253
  const violatedProperty = search.findViolatedActionProperty(state, successor.state);
1395
2254
  if (violatedProperty) {
1396
- return reportViolation("liveness-violation", {
1397
- name: violatedProperty.name,
1398
- trace: getTrace(index, successor),
1399
- });
2255
+ return withOutDegrees(
2256
+ reportViolation("liveness-violation", {
2257
+ name: violatedProperty.name,
2258
+ ...getTrace(index, successor),
2259
+ }),
2260
+ );
1400
2261
  }
1401
2262
  if (!isNew) return undefined;
1402
2263
  if (!isCanonicalizedFirst) canonicalizeState(successor.state);
1403
2264
  markSeen(hash, states.length);
1404
- if (liveness) liveSuccessors.push({ index: states.length, state: successor.state });
1405
2265
  addState(successor.state, fingerprintState, index, successor.action);
1406
2266
  runtime.queueSize++;
1407
2267
  return undefined;
1408
2268
  };
1409
2269
 
1410
2270
  const outcome: { result: CheckResult | undefined } = { result: undefined };
2271
+ if (liveness) {
2272
+ options.onProgress?.({ kind: "liveness-prepared", branches: liveness.branches });
2273
+ }
2274
+ // TLC checks the assumptions once it has built the liveness checker in ModelChecker's
2275
+ // constructor.
2276
+ const assumptionViolation = findViolatedAssumption();
2277
+ if (assumptionViolation) return createResult("assumption-violation", assumptionViolation);
2278
+ options.onProgress?.({ kind: "initial-states-started" });
1411
2279
  if (spec.init !== undefined) {
1412
2280
  try {
1413
2281
  forEachInitialState(
1414
2282
  spec.init,
1415
2283
  environment,
1416
2284
  (state) => {
2285
+ reportInitialStatesProgress(runtime.initialStates, options.onProgress);
1417
2286
  try {
1418
2287
  outcome.result = visitInitialState(state);
1419
2288
  } catch (error) {
1420
- outcome.result = toFailure(error, [], state);
2289
+ const replay = getOutput(replayInit);
2290
+ outcome.result = toFailure(
2291
+ error,
2292
+ { trace: [], traceOutput: replay.lines },
2293
+ { initialState: state, ...replay.result, isInitial: true },
2294
+ );
1421
2295
  }
1422
2296
  return outcome.result !== undefined;
1423
2297
  },
1424
2298
  spec.scope,
1425
2299
  );
1426
2300
  } catch (error) {
1427
- return toFailure(error, []);
2301
+ // TLC prints an illegal initial state with null for each variable it leaves undefined, and
2302
+ // stops before it prints the statistics.
2303
+ if (error instanceof TlaIllegalStateError) {
2304
+ const illegal = toFailure(
2305
+ error,
2306
+ { trace: [] },
2307
+ { initialState: error.state.map((value) => value ?? createModelValue("null")) },
2308
+ );
2309
+ return {
2310
+ ...illegal,
2311
+ failure: illegal.failure && { ...illegal.failure, isWithoutStatistics: true },
2312
+ };
2313
+ }
2314
+ // TLC prints the error before it replays the initial predicate with its call stack.
2315
+ const replay = getOutput(replayInit);
2316
+ return toFailure(
2317
+ error,
2318
+ { trace: [], traceOutput: replay.lines },
2319
+ { ...replay.result, isInitial: true },
2320
+ );
1428
2321
  }
1429
2322
  }
1430
2323
  if (outcome.result) return outcome.result;
2324
+ statistics.initialStates = {
2325
+ generated: runtime.initialStates,
2326
+ distinct: runtime.distinctStates,
2327
+ };
2328
+ options.onProgress?.({
2329
+ kind: "initial-states-computed",
2330
+ initialStates: statistics.initialStates,
2331
+ });
1431
2332
 
1432
2333
  const next = spec.next;
1433
2334
  if (next === undefined) {
@@ -1456,9 +2357,8 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1456
2357
  spec.scope,
1457
2358
  );
1458
2359
  }
1459
- nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
1460
2360
  const progress = { isStopped: false };
1461
- return nextActions.value.reduce((count, action) => {
2361
+ return getSpecNextActions().reduce((count, action) => {
1462
2362
  if (progress.isStopped) return count;
1463
2363
  return (
1464
2364
  count +
@@ -1475,62 +2375,240 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1475
2375
  );
1476
2376
  }, 0);
1477
2377
  };
1478
- const expandState = (index: number) => {
2378
+ // TLC's worker adds a state to the behavior graph even after its next states fail or violate a
2379
+ // property, and prints the call stack of an error in that, unless the error is a
2380
+ // FingerprintException, which the worker doesn't catch. TLC 1.7.4 prints none.
2381
+ const getLiveCallStack = (index: number, liveSuccessors: LiveSuccessor[]) => {
2382
+ if (!liveness || is174) return undefined;
2383
+ const replay = getReplayFailure((tracing) =>
2384
+ liveness.addState(index, states[index], liveSuccessors, tracing),
2385
+ );
2386
+ return replay?.callStack.length && !replay.fingerprint ? replay.callStack : undefined;
2387
+ };
2388
+ // TLC's worker adds the state to the behavior graph after it prints the trace. On an error in that,
2389
+ // it adds the state again to print the call stack instead of replaying the next-state relation,
2390
+ // which TLC 1.7.4 doesn't.
2391
+ const addFailedLiveState = (index: number, liveSuccessors: LiveSuccessor[]) => {
2392
+ try {
2393
+ liveness?.addState(index, states[index], liveSuccessors);
2394
+ return undefined;
2395
+ } catch (error) {
2396
+ if (!(error instanceof TlaEvaluationError)) throw error;
2397
+ return getLiveCallStack(index, liveSuccessors);
2398
+ }
2399
+ };
2400
+ const replayFailedNext = (index: number, liveSuccessors: LiveSuccessor[]) => {
2401
+ const live = getOutput(() => addFailedLiveState(index, liveSuccessors));
2402
+ if (live.result) {
2403
+ const result = { callStack: live.result, fingerprint: undefined, isLiveCallStack: true };
2404
+ return { result, lines: live.lines };
2405
+ }
2406
+ const replay = getOutput(() => replayNext(states[index]));
2407
+ return { result: replay.result, lines: [...live.lines, ...replay.lines] };
2408
+ };
2409
+ // TLC reports a stack overflow while it computes next states without replaying it, with the
2410
+ // behavior up to the state it expands.
2411
+ const reportNextStateOverflow = (index: number) =>
2412
+ toFailure(createStackOverflowError(), getStateTrace(index));
2413
+ // After the trace, TLC replays the next-state relation with its call stack, printing again.
2414
+ const reportNextStateFailure = (
2415
+ error: unknown,
2416
+ index: number,
2417
+ liveSuccessors: LiveSuccessor[],
2418
+ ) => {
2419
+ if (isStackOverflow(error)) return reportNextStateOverflow(index);
2420
+ const replay = replayFailedNext(index, liveSuccessors);
2421
+ return toFailure(
2422
+ withNextStateExitCode(error),
2423
+ appendTraceOutput(getStateTrace(index), replay.lines),
2424
+ { isNextState: true, ...replay.result },
2425
+ );
2426
+ };
2427
+ const expandState = (index: number, liveSuccessors: LiveSuccessor[]) => {
1479
2428
  const state = states[index];
1480
- const liveSuccessors: LiveSuccessor[] = [];
1481
2429
  const source = stateWriter && getGraphState(state, depths[index]);
1482
2430
  const successorCount = forEachActionSuccessor(state, (successor, action) => {
1483
2431
  try {
1484
2432
  outcome.result = visitSuccessor(index, successor, liveSuccessors, source, action);
1485
2433
  } catch (error) {
1486
- outcome.result = toFailure(error, getTrace(index, successor));
2434
+ if (isStackOverflow(error)) {
2435
+ outcome.result = reportNextStateOverflow(index);
2436
+ return true;
2437
+ }
2438
+ // TLC 1.7.4 reports the wrapped error without the trace, except for a failed property,
2439
+ // which it reports with the trace before it rethrows. The error stops its worker before it
2440
+ // adds the state to the behavior graph.
2441
+ const replay = is174
2442
+ ? getOutput(() => replayNext(state))
2443
+ : replayFailedNext(index, liveSuccessors);
2444
+ if (is174 && error instanceof TlaEvaluationError && !headedErrors.has(error)) {
2445
+ outcome.result = toFailure(
2446
+ new TlaEvaluationError(`${getWrappedException(error)}${error.message}`, error),
2447
+ appendTraceOutput({ trace: [] }, replay.lines),
2448
+ { isNextState: true, ...replay.result },
2449
+ );
2450
+ return true;
2451
+ }
2452
+ // A successor TLC can't fingerprint fails again as TLC prints it in the trace, which TLC
2453
+ // reports as an exception it didn't expect.
2454
+ const isTraceFailed = getFingerprintState(successor.state).some(
2455
+ (value) => getFingerprintError(value) !== undefined,
2456
+ );
2457
+ const traceFailure =
2458
+ isTraceFailed && error instanceof TlaEvaluationError
2459
+ ? getFailureMessage(
2460
+ isEvalException(error)
2461
+ ? new TlaEvaluationError(`${UNEXPECTED_EVAL_EXCEPTION}${error.message}`)
2462
+ : error,
2463
+ true,
2464
+ )
2465
+ : undefined;
2466
+ outcome.result = toFailure(
2467
+ error,
2468
+ appendTraceOutput(
2469
+ traceFailure ? getStateTrace(index) : getFailedTrace(index, successor),
2470
+ replay.lines,
2471
+ ),
2472
+ { isNextState: true, ...replay.result, traceFailure },
2473
+ );
1487
2474
  }
1488
2475
  return outcome.result !== undefined;
1489
2476
  });
1490
2477
  current.isExpanding = false;
1491
- if (outcome.result) return outcome.result;
1492
- if (successorCount === 0 && config.checkDeadlock) {
1493
- return reportViolation("deadlock", { name: "deadlock", trace: getStateTrace(index) });
2478
+ const result = outcome.result;
2479
+ // TLC 1.7.4's worker stops at a violation.
2480
+ if (result?.violation && !is174) {
2481
+ const live = getOutput(() => addFailedLiveState(index, liveSuccessors));
2482
+ return {
2483
+ ...result,
2484
+ violation: appendTraceOutput(result.violation, live.lines),
2485
+ ...(live.result && { liveCallStack: live.result }),
2486
+ };
1494
2487
  }
1495
- try {
2488
+ if (result) return result;
2489
+ const addLiveState = () => {
2490
+ if (liveness && source && is174) stateWriter?.writeStuttering(source);
1496
2491
  const accepted = liveness?.addState(index, state, liveSuccessors);
1497
2492
  if (!liveness || !accepted) return undefined;
1498
2493
  const steps = getPathSteps(accepted);
1499
- const properties = liveness.getViolatedProperties(
2494
+ const properties = getViolatedProperties(
2495
+ liveness,
1500
2496
  steps.map((step) => step.state),
1501
2497
  steps.length - 1,
1502
2498
  );
1503
2499
  return reportViolation("property-violation", {
1504
2500
  name: properties[0],
1505
2501
  properties,
1506
- trace: toTrace(steps, accepted),
2502
+ ...toTrace(steps, accepted, { known: 0 }),
1507
2503
  });
2504
+ };
2505
+ if (successorCount === 0 && config.checkDeadlock) {
2506
+ const deadlock = reportViolation("deadlock", { name: "deadlock", ...getStateTrace(index) });
2507
+ // TLC's worker still adds the deadlocked state to the behavior graph after it reports the
2508
+ // deadlock, and reports a temporal property that this violates. Then it counts the state as
2509
+ // expanded.
2510
+ const live = getOutput(() => {
2511
+ try {
2512
+ return { additional: addLiveState()?.violation };
2513
+ } catch (error) {
2514
+ if (!(error instanceof TlaEvaluationError)) throw error;
2515
+ return { isFailed: true, callStack: getLiveCallStack(index, liveSuccessors) };
2516
+ }
2517
+ });
2518
+ const reported = deadlock.violation
2519
+ ? { ...deadlock, violation: appendTraceOutput(deadlock.violation, live.lines) }
2520
+ : deadlock;
2521
+ // An error in adding the state stops TLC 1.7.4's worker before it counts the state.
2522
+ const counted = withOutDegrees(
2523
+ reported,
2524
+ live.result.isFailed ? getFailureOutDegrees() : getPartialOutDegrees(),
2525
+ );
2526
+ if (live.result.additional)
2527
+ return { ...counted, additionalViolation: live.result.additional };
2528
+ return live.result.callStack ? { ...counted, liveCallStack: live.result.callStack } : counted;
2529
+ }
2530
+ try {
2531
+ return addLiveState();
1508
2532
  } catch (error) {
1509
- return toFailure(error, getStateTrace(index), undefined, getPartialOutDegrees());
2533
+ // TLC 1.7.4's worker reports an error in adding a state to the behavior graph as an exception
2534
+ // it didn't expect, without the trace or a call stack, and exits as for any general error.
2535
+ if (is174 && error instanceof TlaEvaluationError) {
2536
+ const unexpected = new TlaEvaluationError(
2537
+ `${isEvalException(error) ? UNEXPECTED_EVAL_EXCEPTION : UNEXPECTED_EXCEPTION}${error.message}`,
2538
+ );
2539
+ return toFailure(
2540
+ setExitCode(unexpected, EXIT_STATUS.error),
2541
+ { trace: [] },
2542
+ { outDegrees: getFailureOutDegrees() },
2543
+ );
2544
+ }
2545
+ // TLC's Worker#doNextCheckLiveness adds the state again to find the call stack, prints it
2546
+ // before the error, and reports the error as one in computing next states.
2547
+ const replay = getReplayFailure((tracing) =>
2548
+ liveness?.addState(index, state, liveSuccessors, tracing),
2549
+ );
2550
+ return toFailure(error, getStateTrace(index), {
2551
+ outDegrees: getFailureOutDegrees(),
2552
+ callStack: replay?.callStack.length ? replay.callStack : undefined,
2553
+ isNextState: true,
2554
+ isCallStackFirst: true,
2555
+ });
2556
+ }
2557
+ };
2558
+ // TLC's Liveness#printStutteringCounterExampleWarning.
2559
+ const getStutteringWarning = () => {
2560
+ const name = config.specification;
2561
+ const book =
2562
+ "(compare Chapter 8, page 87ff of Specifying Systems at https://lamport.azurewebsites.net/tla/book.html).";
2563
+ if (name === undefined) {
2564
+ return {
2565
+ code: MESSAGE_CODE.noSpecificationButProperty,
2566
+ text: `The stuttering counterexample above may be caused by the absence of a behavior specification (SPECIFICATION). Only INIT and NEXT have been provided, so TLC permits infinite stuttering. To rule out such counterexamples, use SPECIFICATION Spec, with Spec asserting a suitable fairness constraint ${book}`,
2567
+ };
1510
2568
  }
2569
+ const range = getDefinition(name, environment)?.location.range;
2570
+ if (spec.temporals.length > 0 || !range) return undefined;
2571
+ return {
2572
+ code: MESSAGE_CODE.noFairnessButProperty,
2573
+ text: `The stuttering counterexample above may be caused by the absence of a fairness constraint in the behavior specification ${name} defined at ${formatRange(range)}. To rule out such counterexamples, conjoin a suitable fairness constraint to ${name} ${book}`,
2574
+ };
1511
2575
  };
1512
2576
  // TLC names the properties and the step back into the cycle from the states it prints.
1513
2577
  const reportLasso = (checker: LivenessChecker, lasso: Lasso) => {
1514
2578
  const path = [...lasso.prefix, ...lasso.cycle];
1515
2579
  const loopStart = lasso.prefix.length;
1516
2580
  const steps = getPathSteps(path, loopStart);
1517
- const trace = toTrace(steps, path, loopStart);
1518
- const properties = checker.getViolatedProperties(
2581
+ const trace = toTrace(steps, path, { loopStart, known: 0 }).trace;
2582
+ const properties = getViolatedProperties(
2583
+ checker,
1519
2584
  steps.map((step) => step.state),
1520
2585
  loopStart,
1521
2586
  );
1522
2587
  const back = path[loopStart];
1523
2588
  const last = path[path.length - 1];
1524
2589
  const lastState = steps[steps.length - 1].state;
1525
- const loopLabel =
1526
- back === last ? undefined : withoutOutput(() => getLabel(last, lastState, states[back]));
2590
+ const loop =
2591
+ back === last
2592
+ ? { result: undefined, lines: [] }
2593
+ : getOutput(() => findStepAction(last, lastState, states[back], false));
2594
+ const isStutteringWarned = back === last && lasso.isCycleConstrained;
1527
2595
  return reportViolation(
1528
2596
  "liveness-violation",
1529
- { name: properties[0], properties, trace, loopStart, loopLabel },
2597
+ {
2598
+ name: properties[0],
2599
+ properties,
2600
+ trace,
2601
+ traceOutput: loop.lines,
2602
+ loopStart,
2603
+ loopLabel: loop.result && getActionLabel(loop.result, options.version),
2604
+ warning: isStutteringWarned ? getStutteringWarning() : undefined,
2605
+ },
1530
2606
  () =>
1531
2607
  back === last
1532
- ? trace[trace.length - 1].action
1533
- : findSuccessorWithFingerprint(lastState, back)?.action,
2608
+ ? getStepAction(trace[trace.length - 1])
2609
+ : loop.result
2610
+ ? toActionValue(loop.result)
2611
+ : createActionRecord(findSuccessorWithFingerprint(lastState, back)?.action),
1534
2612
  );
1535
2613
  };
1536
2614
  const isPeriodic = liveness !== undefined && options.livenessCheck !== "final";
@@ -1538,11 +2616,19 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1538
2616
  const periodic = { at: startedAt + PERIODIC_START, size: 1, time: 0 };
1539
2617
  const findPeriodicViolation = (expanded: number) => {
1540
2618
  const now = performance.now();
1541
- periodic.at = now + PROGRESS_INTERVAL;
1542
2619
  const isGrown = (expanded - periodic.size) / periodic.size > LIVENESS_GROWTH;
1543
2620
  if (!isGrown || periodic.time > (now - startedAt) * LIVENESS_RATIO) return undefined;
1544
2621
  periodic.size = expanded;
1545
- const lasso = liveness?.findViolation();
2622
+ if (!liveness) return undefined;
2623
+ options.onProgress?.({
2624
+ kind: "liveness-started",
2625
+ branches: liveness.branches,
2626
+ isComplete: false,
2627
+ states: expanded,
2628
+ statistics: createResult("success"),
2629
+ });
2630
+ const lasso = liveness.findViolation();
2631
+ if (!lasso) options.onProgress?.({ kind: "liveness-finished" });
1546
2632
  periodic.time += performance.now() - now;
1547
2633
  return lasso;
1548
2634
  };
@@ -1551,28 +2637,57 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1551
2637
  current.candidate = undefined;
1552
2638
  runtime.queueSize = states.length - index - 1;
1553
2639
  current.isExpanding = true;
2640
+ const liveSuccessors: LiveSuccessor[] = [];
1554
2641
  try {
1555
- outcome.result = expandState(index);
2642
+ outcome.result = expandState(index, liveSuccessors);
1556
2643
  } catch (error) {
1557
- outcome.result = toFailure(withNextStateExitCode(error), getStateTrace(index));
2644
+ outcome.result =
2645
+ error instanceof TlaIncompleteSuccessorError
2646
+ ? reportIncompleteSuccessor(index, error)
2647
+ : reportNextStateFailure(error, index, liveSuccessors);
1558
2648
  }
1559
2649
  if (outcome.result) return outcome.result;
1560
2650
  outDegrees[getOutDegreeBucket()]++;
1561
2651
  expansion.newStates = 0;
1562
- if (isPeriodic && performance.now() >= periodic.at) {
1563
- const lasso = findPeriodicViolation(index + 1);
2652
+ // TLC's AbstractChecker#runTLC checks the liveness properties on the state graph so far, then
2653
+ // reports how far the search got.
2654
+ if ((isPeriodic || options.onProgress) && performance.now() >= periodic.at) {
2655
+ periodic.at = performance.now() + PROGRESS_INTERVAL;
2656
+ const lasso = isPeriodic ? findPeriodicViolation(index + 1) : undefined;
1564
2657
  if (liveness && lasso) return reportLasso(liveness, lasso);
2658
+ options.onProgress?.({ kind: "progress", statistics: createResult("success") });
1565
2659
  }
1566
2660
  }
1567
2661
 
1568
2662
  current.index = -1;
1569
2663
  current.candidate = undefined;
2664
+ if (liveness) {
2665
+ options.onProgress?.({
2666
+ kind: "liveness-started",
2667
+ branches: liveness.branches,
2668
+ isComplete: true,
2669
+ states: runtime.distinctStates,
2670
+ statistics: createResult("success"),
2671
+ });
2672
+ }
1570
2673
  const lasso = liveness?.findViolation();
1571
2674
  if (liveness && lasso) return reportLasso(liveness, lasso);
1572
2675
 
1573
- const postconditionViolation = search.findPostconditionViolation();
1574
- if (postconditionViolation) {
1575
- return createResult("postcondition-violation", { ...postconditionViolation, trace: [] });
2676
+ // TLC finishes the check of the temporal properties before it checks the postconditions.
2677
+ if (liveness) options.onProgress?.({ kind: "liveness-finished" });
2678
+ try {
2679
+ const postcondition = search.findPostconditionViolation();
2680
+ if (postcondition) {
2681
+ return createResult("postcondition-violation", {
2682
+ name: postcondition.name,
2683
+ message: postcondition.message,
2684
+ counts: postcondition.counts,
2685
+ trace: [],
2686
+ postcondition,
2687
+ });
2688
+ }
2689
+ } catch (error) {
2690
+ return toFailure(error, { trace: [] });
1576
2691
  }
1577
2692
  return createSuccess();
1578
2693
  };