tla-ts 0.0.10-dev.28c823e4b → 0.0.10-dev.63a6693bd

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 (172) hide show
  1. package/dist/compile.cjs +6 -6
  2. package/dist/compile.js +10 -10
  3. package/dist/deciders.cjs +29 -29
  4. package/dist/deciders.js +40 -40
  5. package/dist/index.cjs +1 -1
  6. package/dist/index.js +1 -1
  7. package/dist/parallel-worker.cjs +1 -1
  8. package/dist/parallel-worker.js +1 -1
  9. package/dist/prove-worker.cjs +1 -1
  10. package/dist/prove-worker.js +1 -1
  11. package/dist/prove.cjs +54 -54
  12. package/dist/prove.js +52 -52
  13. package/dist/run.cjs +42 -42
  14. package/dist/run.js +44 -44
  15. package/dist/tlapm.cjs +2 -2
  16. package/dist/tlapm.js +2 -2
  17. package/dist/transfer.cjs +39 -39
  18. package/dist/transfer.js +41 -41
  19. package/dist/worker.cjs +4 -4
  20. package/dist/worker.js +4 -4
  21. package/package.json +1 -1
  22. package/src/ast.ts +44 -39
  23. package/src/builtins.ts +4 -13
  24. package/src/check-failure.ts +116 -0
  25. package/src/check-trace.ts +282 -0
  26. package/src/check.ts +152 -518
  27. package/src/cli/options.ts +6 -31
  28. package/src/cli/shared.ts +1 -1
  29. package/src/cli/tlapm.ts +14 -13
  30. package/src/cli/tlc-arguments.ts +222 -210
  31. package/src/cli/tlc-output.ts +687 -0
  32. package/src/cli/tlc-usage.ts +1 -1
  33. package/src/cli/tlc.ts +34 -701
  34. package/src/cli/xml-exporter.ts +23 -33
  35. package/src/codegen.ts +5 -10
  36. package/src/coverage.ts +3 -10
  37. package/src/dependencies.ts +8 -13
  38. package/src/dfid.ts +16 -16
  39. package/src/environment-config.ts +315 -0
  40. package/src/environment.ts +45 -333
  41. package/src/errors.ts +3 -2
  42. package/src/evaluate.ts +5 -4
  43. package/src/fingerprints.ts +65 -1
  44. package/src/instances.ts +47 -10
  45. package/src/java-hash.ts +7 -0
  46. package/src/json.ts +4 -7
  47. package/src/levels.ts +39 -42
  48. package/src/liveness-checker.ts +24 -37
  49. package/src/liveness.ts +47 -54
  50. package/src/obligations/action.ts +4 -11
  51. package/src/obligations/anonymize.ts +4 -2
  52. package/src/obligations/expression.ts +18 -2
  53. package/src/obligations/generate.ts +45 -38
  54. package/src/obligations/index.ts +2 -2
  55. package/src/obligations/module.ts +27 -38
  56. package/src/obligations/normalize.ts +2 -1
  57. package/src/obligations/simplify.ts +39 -44
  58. package/src/obligations/source.ts +47 -57
  59. package/src/obligations/temporal.ts +5 -5
  60. package/src/parallel-worker.ts +10 -30
  61. package/src/parallel.ts +22 -45
  62. package/src/parser.ts +19 -34
  63. package/src/prover/decide-pool.ts +3 -3
  64. package/src/prover/deciders.ts +3 -3
  65. package/src/prover/isabelle/encode.ts +1 -1
  66. package/src/prover/isabelle/isar/inner-syntax.ts +9 -4
  67. package/src/prover/isabelle/isar/method.ts +14 -25
  68. package/src/prover/isabelle/isar/theory.ts +11 -10
  69. package/src/prover/isabelle/isar/type-check.ts +6 -6
  70. package/src/prover/isabelle/provers/blast/blast.ts +7 -63
  71. package/src/prover/isabelle/provers/hypsubst.ts +17 -26
  72. package/src/prover/isabelle/pure/bicompose.ts +9 -25
  73. package/src/prover/isabelle/pure/conv.ts +2 -8
  74. package/src/prover/isabelle/pure/drule.ts +9 -2
  75. package/src/prover/isabelle/pure/logic.ts +1 -1
  76. package/src/prover/isabelle/pure/raw-simplifier.ts +4 -13
  77. package/src/prover/isabelle/pure/rewriter.ts +5 -9
  78. package/src/prover/isabelle/pure/table.ts +2 -1
  79. package/src/prover/isabelle/pure/tactical.ts +42 -35
  80. package/src/prover/isabelle/pure/thm.ts +14 -11
  81. package/src/prover/isabelle/tla/classical-setup.ts +17 -14
  82. package/src/prover/isabelle/tla/context.ts +7 -2
  83. package/src/prover/isabelle/tla/logic.ts +10 -21
  84. package/src/prover/isabelle/tla/simpdata.ts +1 -10
  85. package/src/prover/prep/action.ts +11 -19
  86. package/src/prover/prep/fingerprint.ts +2 -5
  87. package/src/prover/prep/methods.ts +24 -3
  88. package/src/prover/prove.ts +87 -64
  89. package/src/prover/smt/context.ts +1 -2
  90. package/src/prover/smt/data.ts +418 -406
  91. package/src/prover/smt/encode.ts +30 -18
  92. package/src/prover/smt/rewrite.ts +657 -626
  93. package/src/prover/smt/solver/ast.ts +0 -22
  94. package/src/prover/smt/solver/auf-solver.ts +2 -5
  95. package/src/prover/smt/solver/context.ts +3 -3
  96. package/src/prover/smt/solver/e-matching.ts +1 -1
  97. package/src/prover/smt/solver/expr-safe-replace.ts +1 -1
  98. package/src/prover/smt/solver/internalizer.ts +1 -2
  99. package/src/prover/smt/solver/lp/dioph-eq.ts +1 -8
  100. package/src/prover/smt/solver/lp/hnf-cutter.ts +1 -1
  101. package/src/prover/smt/solver/lp/nla-core.ts +2 -6
  102. package/src/prover/smt/solver/mam-compiler.ts +4 -2
  103. package/src/prover/smt/solver/model-checker.ts +1 -1
  104. package/src/prover/smt/solver/model-finder.ts +12 -17
  105. package/src/prover/smt/solver/model-macro-solver.ts +11 -19
  106. package/src/prover/smt/solver/nlsat/algebraic.ts +9 -9
  107. package/src/prover/smt/solver/nlsat/atoms.ts +20 -4
  108. package/src/prover/smt/solver/nlsat/explain.ts +7 -22
  109. package/src/prover/smt/solver/nlsat/levelwise.ts +1 -6
  110. package/src/prover/smt/solver/nlsat/polynomial.ts +12 -3
  111. package/src/prover/smt/solver/nlsat/solver.ts +12 -24
  112. package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +1 -1
  113. package/src/prover/smt/solver/proto-model.ts +0 -4
  114. package/src/prover/smt/solver/quantifier-analyzer.ts +10 -10
  115. package/src/prover/smt/solver/quantifier-manager.ts +1 -5
  116. package/src/prover/smt/solver/smtlib-parser.ts +78 -94
  117. package/src/prover/smt/solver/theory-lra-bounds.ts +1 -2
  118. package/src/prover/smt/solver/theory-lra-internalize.ts +5 -16
  119. package/src/prover/tlapm/cache.ts +28 -18
  120. package/src/prover/tlapm/tlapm.ts +15 -9
  121. package/src/prover/verdict.ts +7 -0
  122. package/src/prover/zenon/check/check.ts +1 -2
  123. package/src/prover/zenon/check/records.ts +1 -1
  124. package/src/prover/zenon/check/relations.ts +2 -1
  125. package/src/prover/zenon/check/rewrites.ts +2 -15
  126. package/src/prover/zenon/check/sets.ts +40 -58
  127. package/src/prover/zenon/check/terms.ts +1 -2
  128. package/src/prover/zenon/eqrel.ts +2 -0
  129. package/src/prover/zenon/expr.ts +7 -0
  130. package/src/prover/zenon/index-table.ts +9 -19
  131. package/src/prover/zenon/isar.ts +35 -35
  132. package/src/prover/zenon/llproof.ts +2 -11
  133. package/src/prover/zenon/mltoll.ts +12 -28
  134. package/src/prover/zenon/parse.ts +3 -6
  135. package/src/prover/zenon/search.ts +4 -14
  136. package/src/prover/zenon/tla/functions.ts +2 -2
  137. package/src/prover/zenon/tla/llproof.ts +9 -11
  138. package/src/prover/zenon/tla/records.ts +1 -6
  139. package/src/prover/zenon/tla/sets.ts +15 -14
  140. package/src/prover/zenon/tla/tla.ts +7 -6
  141. package/src/resolve.ts +55 -72
  142. package/src/sany/generation.ts +394 -0
  143. package/src/sany/generator-def-entries.ts +210 -0
  144. package/src/sany/generator-definitions.ts +686 -0
  145. package/src/sany/generator-expressions.ts +1100 -0
  146. package/src/sany/generator-proofs.ts +662 -0
  147. package/src/sany/generator.ts +155 -2625
  148. package/src/sany/lexer.ts +17 -16
  149. package/src/sany/nodes.ts +6 -13
  150. package/src/sany/report.ts +3 -6
  151. package/src/sany/selectors.ts +35 -38
  152. package/src/sany/xml.ts +23 -55
  153. package/src/search.ts +65 -27
  154. package/src/simulate.ts +157 -133
  155. package/src/spec-description.ts +33 -48
  156. package/src/standard-modules.ts +16 -32
  157. package/src/state-graph.ts +2 -7
  158. package/src/substitution-levels.ts +5 -4
  159. package/src/successors.ts +5 -7
  160. package/src/tlc/messages.ts +20 -15
  161. package/src/tlc/order.ts +2 -9
  162. package/src/tlc/random.ts +4 -4
  163. package/src/tlc/replay.ts +8 -15
  164. package/src/tlc/tlc-operators.ts +2 -3
  165. package/src/tlc/trace-spec.ts +25 -45
  166. package/src/typescript/actions.ts +2 -4
  167. package/src/typescript/collections.ts +34 -20
  168. package/src/typescript/compile.ts +1 -3
  169. package/src/typescript/formula.ts +5 -3
  170. package/src/typescript/model.ts +4 -5
  171. package/src/typescript/steps.ts +20 -18
  172. package/src/values.ts +63 -158
package/src/check.ts CHANGED
@@ -9,10 +9,8 @@ import {
9
9
  getCoverageReport,
10
10
  type CoverageReport,
11
11
  } from "./coverage";
12
- import { createEnvironment } from "./environment";
13
12
  import {
14
13
  createStackOverflowError,
15
- getErrorExitCode,
16
14
  isStackOverflow,
17
15
  TlaEvaluationError,
18
16
  TlaIllegalStateError,
@@ -24,21 +22,32 @@ import {
24
22
  formatRange,
25
23
  } from "./errors";
26
24
  import {
27
- createTracingEnvironment,
28
25
  describeRange,
29
26
  getDefinition,
30
- isImpure,
31
27
  type CallStack,
32
28
  type Environment,
33
29
  type FingerprintFailure,
34
30
  } from "./evaluate";
31
+ import { EXIT_STATUS, setExitCode, withLivenessExitCode } from "./exit-codes";
35
32
  import {
36
- EXIT_STATUS,
37
- getExitCode,
38
- getMessageExitStatus,
39
- setExitCode,
40
- withLivenessExitCode,
41
- } from "./exit-codes";
33
+ fingerprintReplayed,
34
+ getFailureExitCode,
35
+ getReplayFailure,
36
+ type FailureOptions,
37
+ } from "./check-failure";
38
+ import {
39
+ createTraceBuilder,
40
+ type Behavior,
41
+ type SearchPosition,
42
+ type TracedStep,
43
+ } from "./check-trace";
44
+ import {
45
+ createStateTable,
46
+ findStateIndex,
47
+ getStateHash,
48
+ isSameState,
49
+ markSeenState,
50
+ } from "./fingerprints";
42
51
  import type { JavaOverride } from "./java-overrides";
43
52
  import {
44
53
  createLivenessChecker,
@@ -47,6 +56,7 @@ import {
47
56
  type LiveSuccessor,
48
57
  } from "./liveness-checker";
49
58
  import {
59
+ createCheckEnvironment,
50
60
  createMissingOperatorError,
51
61
  createSearch,
52
62
  getIncompleteSuccessorMessage,
@@ -58,11 +68,11 @@ import {
58
68
  import {
59
69
  getActionLabel,
60
70
  getActionSignature,
61
- getInitActions,
62
- getNextActions,
71
+ getSpecDescription,
63
72
  getSpecificationValue,
64
73
  toActionValue,
65
74
  type SpecAction,
75
+ type SpecDescription,
66
76
  } from "./spec-description";
67
77
  import {
68
78
  createStateWriter,
@@ -79,7 +89,6 @@ import {
79
89
  type Successor,
80
90
  } from "./successors";
81
91
  import {
82
- getCodedMessage,
83
92
  getFailureMessage,
84
93
  getWrappedException,
85
94
  isEvalException,
@@ -90,9 +99,7 @@ import {
90
99
  } from "./tlc/messages";
91
100
  import { createTautologyError } from "./liveness";
92
101
  import { KEY_SEPARATOR } from "./resolve";
93
- import { setFingerprintPolynomial } from "./tlc/fingerprint";
94
102
  import { writeTraceSpec } from "./tlc/trace-spec";
95
- import { createTlcOrder } from "./tlc/order";
96
103
  import {
97
104
  getPrimedRanges,
98
105
  replayInitialStates,
@@ -103,18 +110,13 @@ import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc/summary";
103
110
  import { createActionRecord, createCounterExample } from "./tlc/tlcext-operators";
104
111
  import type { TlcVersion } from "./tlc/version";
105
112
  import {
106
- combineHash,
107
113
  createCanonicalizer,
108
114
  createModelValue,
109
115
  formatState,
110
116
  getFingerprintError,
111
- getHeldValues,
112
117
  getModelValues,
113
- getValueHash,
114
118
  isSameValue,
115
119
  markFingerprinted,
116
- setStringOrder,
117
- setValueOptions,
118
120
  type Value,
119
121
  } from "./values";
120
122
 
@@ -284,12 +286,6 @@ export interface EvaluationFailure {
284
286
  isWithoutStatistics?: boolean;
285
287
  }
286
288
 
287
- interface ReplayFailure {
288
- callStack: Expression[];
289
- fingerprint: FingerprintFailure | undefined;
290
- replayError?: TlaEvaluationError;
291
- }
292
-
293
289
  export interface InitialStates {
294
290
  generated: number;
295
291
  distinct: number;
@@ -323,73 +319,18 @@ interface CoveredAction extends SpecAction {
323
319
  node: number;
324
320
  }
325
321
 
326
- interface Behavior {
327
- trace: TraceStep[];
328
- traceOutput?: OutputLine[];
329
- }
330
-
331
- interface TraceOptions {
332
- loopStart?: number;
333
- known?: number;
334
- }
335
-
336
322
  interface ReportedViolation extends Omit<Violation, "loopStart"> {
337
323
  loopStart?: number;
338
324
  }
339
325
 
340
- interface RebuiltState {
341
- state: Value[];
342
- level: number;
343
- }
344
-
345
- interface SearchPosition {
346
- index: number;
347
- isExpanding: boolean;
348
- candidate: Value[] | undefined;
349
- candidateLevel: number;
350
- livenessSource: number;
351
- rebuilt: RebuiltState | undefined;
352
- }
353
-
354
- interface FailureOptions {
355
- initialState?: Value[];
356
- isInitial?: boolean;
357
- outDegrees?: number[];
358
- callStack?: Expression[];
359
- fingerprint?: FingerprintFailure;
360
- replayError?: TlaEvaluationError;
361
- isNextState?: boolean;
362
- isCallStackFirst?: boolean;
363
- traceFailure?: FailureMessage;
364
- isLiveCallStack?: boolean;
365
- }
366
-
367
326
  const appendTraceOutput = <Traced extends Behavior>(behavior: Traced, lines: OutputLine[]) => ({
368
327
  ...behavior,
369
328
  traceOutput: [...(behavior.traceOutput ?? []), ...lines],
370
329
  });
371
330
 
372
- const INITIAL_RECOVERY_FAILURE = "Failed to recover the initial state from its fingerprint.";
373
- const NEXT_RECOVERY_FAILURE = "Failed to recover the next state from its fingerprint.";
374
-
375
331
  const STOP_REPLAY = new Error("The replay stopped");
376
332
  const STOP_WALK = new Error("The walk stopped");
377
333
 
378
- const getInitialExitCode = (error: TlaEvaluationError) => {
379
- const coded = getCodedMessage(error.message);
380
- return coded &&
381
- !coded.isEvalException &&
382
- getMessageExitStatus(coded.code) === EXIT_STATUS.configParseError
383
- ? EXIT_STATUS.configParseError
384
- : getErrorExitCode(error);
385
- };
386
-
387
- const getReplayExitCode = (error: TlaEvaluationError): number => {
388
- const inner = error.cause;
389
- if (inner instanceof TlaEvaluationError) return getReplayExitCode(inner);
390
- return error instanceof TlaModuleError ? error.exitCode : EXIT_STATUS.specEvaluationFailure;
391
- };
392
-
393
334
  export const checkSequentially = (options: CheckOptions): CheckResult => {
394
335
  const stateWriter =
395
336
  options.dump && createStateWriter(options.dump, options.writeFile, options.version);
@@ -428,29 +369,8 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
428
369
  capturedLines = outer;
429
370
  }
430
371
  };
431
- const order = createTlcOrder(options.source, options.loadModule);
432
- setStringOrder(order.getStringOrder(config));
433
- setValueOptions(options);
434
- setFingerprintPolynomial(options.fingerprintPolynomial ?? 0);
435
- const specification = createEnvironment(config, {
436
- source: options.source,
437
- loadModule: options.loadModule,
438
- order,
439
- loadClass: options.loadClass,
440
- print,
441
- files: {
442
- readFile: options.readFile,
443
- writeFile: options.writeFile,
444
- makeDirectory: options.makeDirectory,
445
- },
446
- seed: options.seed,
447
- isTool: options.isTool,
448
- traceFile: options.traceSpec?.traceFile,
449
- isSanyReported: options.isSanyReported,
450
- isWarningEnabled: options.isWarningEnabled,
451
- version: options.version,
452
- moduleOperators: options.moduleOperators,
453
- });
372
+ const created = createCheckEnvironment(config, options, print);
373
+ const specification = created.specification;
454
374
  reportStartup(specification, config, options.onProgress);
455
375
  const coverage =
456
376
  options.coverage === undefined
@@ -468,13 +388,11 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
468
388
  const runtime = environment.runtime;
469
389
  const is174 = runtime.version === "1.7.4";
470
390
  runtime.pause = options.onPause ?? (() => {});
391
+ let description: SpecDescription | undefined;
392
+ const getDescription = () =>
393
+ (description ??= getSpecDescription(config, environment, specification.instanceActions));
471
394
  runtime.getSpecification = () =>
472
- getSpecificationValue(
473
- config,
474
- environment,
475
- specification.variableLocations,
476
- specification.instanceActions,
477
- );
395
+ getSpecificationValue(getDescription(), config, environment, specification.variableLocations);
478
396
  let searchDepth = 0;
479
397
  let computedInitialStates: InitialStates | undefined;
480
398
  const getTraceSpec = (trace: TraceStep[], loopStart: number | undefined) =>
@@ -553,7 +471,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
553
471
  config,
554
472
  environment,
555
473
  specification.variableLocations,
556
- () => order.getDeclarationOrder("variable"),
474
+ () => created.order.getDeclarationOrder("variable"),
557
475
  options.onProgress,
558
476
  specification.moduleOperators,
559
477
  );
@@ -575,13 +493,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
575
493
  config.postconditions.length > 0 ||
576
494
  config.possible.length > 0 ||
577
495
  specification.moduleOperators.some((operator) => operator.kind === "postcondition");
578
- const traceSteps = new WeakMap<
579
- TraceStep,
580
- { state: Record<string, Value>; action: SpecAction | undefined }
581
- >();
582
- const getStepState = (step: TraceStep) => traceSteps.get(step)?.state ?? step.state;
496
+ const tracedSteps = new WeakMap<TraceStep, TracedStep>();
497
+ const getStepState = (step: TraceStep) => tracedSteps.get(step)?.state ?? step.state;
583
498
  const getStepAction = (step: TraceStep) => {
584
- const action = traceSteps.get(step)?.action;
499
+ const action = tracedSteps.get(step)?.action;
585
500
  return action ? toActionValue(action) : createActionRecord(step.action);
586
501
  };
587
502
  const checkTracePostconditions = (
@@ -633,13 +548,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
633
548
  };
634
549
  const spec = search.spec;
635
550
  const getFingerprintState = search.getFingerprintState;
636
- let specNextActions: SpecAction[] | undefined;
637
- const getSpecNextActions = () =>
638
- (specNextActions ??= getNextActions(config, environment, specification.instanceActions));
639
551
  // Tool's constructor splits the next-state relation into actions before TLC checks anything.
640
- getSpecNextActions();
552
+ const nextActions = getDescription().actions;
641
553
  runtime.getSuccessorAction = (state, next) =>
642
- getSpecNextActions().find((action) =>
554
+ nextActions.find((action) =>
643
555
  getSuccessors(action.expression, state, environment, action.scope).some((successor) =>
644
556
  successor.state.every((value, index) => isSameValue(value, next[index])),
645
557
  ),
@@ -657,11 +569,8 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
657
569
  )
658
570
  : -1,
659
571
  }));
660
- const coverageInits = addCoverageRelations(
661
- "init",
662
- coverage ? getInitActions(config, environment, specification.instanceActions) : [],
663
- );
664
- const coverageActions = addCoverageRelations("next", coverage ? getSpecNextActions() : []);
572
+ const coverageInits = addCoverageRelations("init", coverage ? getDescription().inits : []);
573
+ const coverageActions = addCoverageRelations("next", coverage ? nextActions : []);
665
574
  if (coverage) buildCoverage(coverage, environment, getPrimedRanges(coverageActions, environment));
666
575
  const walkCoveredSuccessors = (
667
576
  action: CoveredAction,
@@ -696,64 +605,26 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
696
605
  );
697
606
  return successors;
698
607
  };
608
+ const rejectIncompleteInitialState = (partial: (Value | undefined)[]) => {
609
+ throw setExitCode(new TlaIllegalStateError([...partial]), EXIT_STATUS.error);
610
+ };
699
611
  const getCoveredInitialStates = () => {
700
612
  const initialStates: Value[][] = [];
701
613
  replayInitialStates(
702
614
  coverageInits,
703
615
  environment,
704
616
  (state) => initialStates.push(state),
705
- (partial) => {
706
- throw setExitCode(new TlaIllegalStateError([...partial]), EXIT_STATUS.error);
707
- },
617
+ rejectIncompleteInitialState,
708
618
  );
709
619
  return initialStates;
710
620
  };
711
- const getReplayFailure = (replay: (tracing: Environment) => void): ReplayFailure | undefined => {
712
- const tracing = createTracingEnvironment(environment);
713
- const callStack = tracing.callStack;
714
- try {
715
- replay(tracing);
716
- return undefined;
717
- } catch (error) {
718
- if (isStackOverflow(error)) return { callStack: [], fingerprint: undefined };
719
- if (!(error instanceof TlaEvaluationError) || !callStack) return undefined;
720
- return {
721
- callStack: callStack.expressions,
722
- fingerprint: callStack.fingerprint,
723
- replayError: error,
724
- };
725
- }
726
- };
727
- const findFailedSources = (value: Value, sources: CallStack["sources"]): Expression[] => {
728
- if (typeof value !== "object") return [];
729
- const source = sources.get(value);
730
- const failed = getHeldValues(value).find((held) => getFingerprintError(held) !== undefined);
731
- return [
732
- ...(source ? [source] : []),
733
- ...(failed === undefined ? [] : findFailedSources(failed, sources)),
734
- ];
735
- };
736
- const fingerprintReplayed = (state: Value[], callStack: CallStack | undefined) => {
737
- for (const value of getFingerprintState(state)) {
738
- const error = getFingerprintError(value);
739
- if (!error) continue;
740
- const sources = callStack ? findFailedSources(value, callStack.sources) : [];
741
- if (callStack && sources.length > 0) {
742
- callStack.expressions.length = 0;
743
- callStack.isFrozen = true;
744
- callStack.fingerprint = { sources, reason: error.message };
745
- }
746
- throw error;
747
- }
748
- state.forEach(markFingerprinted);
749
- };
750
621
  const createReplayedStates = () => {
751
622
  const replayed = unstoredFingerprint ? [unstoredFingerprint] : [];
752
623
  return (state: Value[], callStack: CallStack | undefined) => {
753
- fingerprintReplayed(state, callStack);
624
+ fingerprintReplayed(state, getFingerprintState(state), callStack);
754
625
  const fingerprintState = getFingerprintState(state);
755
626
  const isSeen =
756
- findStateIndex(getStateHash(fingerprintState), fingerprintState) !== -1 ||
627
+ findStateIndex(seenStates, getStateHash(fingerprintState), fingerprintState) !== -1 ||
757
628
  replayed.some((seen) => isSameState(seen, fingerprintState));
758
629
  if (isSeen) return false;
759
630
  replayed.push(fingerprintState);
@@ -765,48 +636,44 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
765
636
  let replayedCount = 0;
766
637
  const deferred: unknown[] = [];
767
638
  const addReplayed = createReplayedStates();
768
- return getReplayFailure((tracing) => {
769
- replayInitialStates(
770
- getInitActions(config, environment, specification.instanceActions),
771
- tracing,
772
- (state) => {
773
- reportInitialStatesProgress(replayedCount, (progress) => capturedLines?.push(progress));
774
- replayedCount++;
775
- runtime.generatedStates = replayedCount;
776
- if (deferred.length > 0) return;
777
- try {
778
- const addFingerprint = () => {
779
- if (!addReplayed(state, tracing.callStack)) return;
780
- runtime.queueSize++;
781
- liveness?.addInitialState(states.length, state, tracing);
782
- };
783
- const violation = search.checkReplayedInitialState(state, tracing, addFingerprint);
784
- if (violation) {
785
- const traced = toTrace([{ action: undefined, state }], []).trace[0].state;
786
- capturedLines?.push({
787
- kind: "initial-state-violation",
788
- ...violation,
789
- state: traced,
790
- });
791
- throw STOP_REPLAY;
792
- }
793
- } catch (error) {
794
- const isFingerprintFailure =
795
- error instanceof TlaEvaluationError && tracing.callStack?.fingerprint !== undefined;
796
- if (!isFingerprintFailure && !isStackOverflow(error)) throw error;
797
- deferred.push(error);
639
+ return getReplayFailure(environment, (tracing) => {
640
+ replayInitialStates(getDescription().inits, tracing, (state) => {
641
+ reportInitialStatesProgress(replayedCount, (progress) => capturedLines?.push(progress));
642
+ replayedCount++;
643
+ runtime.generatedStates = replayedCount;
644
+ if (deferred.length > 0) return;
645
+ try {
646
+ const addFingerprint = () => {
647
+ if (!addReplayed(state, tracing.callStack)) return;
648
+ runtime.queueSize++;
649
+ liveness?.addInitialState(states.length, state, tracing);
650
+ };
651
+ const violation = search.checkReplayedInitialState(state, tracing, addFingerprint);
652
+ if (violation) {
653
+ const traced = traces.toTrace([{ action: undefined, state }], []).trace[0].state;
654
+ capturedLines?.push({
655
+ kind: "initial-state-violation",
656
+ ...violation,
657
+ state: traced,
658
+ });
659
+ throw STOP_REPLAY;
798
660
  }
799
- },
800
- );
661
+ } catch (error) {
662
+ const isFingerprintFailure =
663
+ error instanceof TlaEvaluationError && tracing.callStack?.fingerprint !== undefined;
664
+ if (!isFingerprintFailure && !isStackOverflow(error)) throw error;
665
+ deferred.push(error);
666
+ }
667
+ });
801
668
  if (deferred.length > 0) throw deferred[0];
802
669
  });
803
670
  };
804
671
  const replayNext = (index: number) =>
805
- getReplayFailure((tracing) => {
672
+ getReplayFailure(environment, (tracing) => {
806
673
  const state = states[index];
807
674
  const source = stateWriter && getGraphState(state, depths[index]);
808
675
  const addReplayed = createReplayedStates();
809
- replaySuccessors(getSpecNextActions(), state, tracing, (next, action) => {
676
+ replaySuccessors(nextActions, state, tracing, (next, action) => {
810
677
  const addFingerprint = () => {
811
678
  const isUnseen = addReplayed(next, tracing.callStack);
812
679
  if (source) {
@@ -824,7 +691,6 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
824
691
  }
825
692
  });
826
693
  });
827
- const isReducing = config.view !== undefined || config.symmetry !== undefined;
828
694
  const tlcFingerprints: number[] = [];
829
695
  if (search.properties.liveness.length > 0) {
830
696
  options.onProgress?.({
@@ -857,7 +723,11 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
857
723
  search.satisfiesActionConstraints(state, successor.state);
858
724
  if (!isInModel) return false;
859
725
  const fingerprintState = getFingerprintState(successor.state);
860
- const index = findStateIndex(getStateHash(fingerprintState), fingerprintState);
726
+ const index = findStateIndex(
727
+ seenStates,
728
+ getStateHash(fingerprintState),
729
+ fingerprintState,
730
+ );
861
731
  successors.push({ index, state: successor.state });
862
732
  return false;
863
733
  },
@@ -880,202 +750,14 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
880
750
  const parents: number[] = [];
881
751
  const actions: (string | undefined)[] = [];
882
752
  const depths: number[] = [];
883
- // A Map holds at most 2^24 entries, so an open-addressing table keeps each hash and index + 1.
884
- const seenStates = { slots: new Int32Array(1 << 17), size: 0, shift: 16 };
753
+ const seenStates = createStateTable(fingerprintStates);
885
754
  const canonicalizeState = createCanonicalizer();
886
755
 
887
756
  const toRecord = search.toRecord;
888
- const getAliasState = (
889
- alias: string,
890
- steps: Successor[],
891
- position: number,
892
- loopStart: number | undefined,
893
- ) => {
894
- const searchLevel = runtime.getLevel;
895
- const searchBehavior = runtime.getBehavior;
896
- runtime.getLevel = () => position + 1;
897
- runtime.getBehavior = () => steps.slice(0, position + 1).map((step) => step.state);
898
- try {
899
- return evaluateAlias(
900
- alias,
901
- steps[position].state,
902
- (steps[position + 1] ?? steps[loopStart ?? position]).state,
903
- );
904
- } finally {
905
- runtime.getLevel = searchLevel;
906
- runtime.getBehavior = searchBehavior;
907
- }
908
- };
909
- const evaluateAlias = (alias: string, state: Value[], next: Value[] | undefined) =>
910
- search.evaluateAlias(alias, state, next, options.isTool ?? false);
911
- const findStepAction = (index: number, previous: Value[], state: Value[], isHeld: boolean) => {
912
- const actions = getSpecNextActions();
913
- const expanding = { ...current };
914
- Object.assign(current, {
915
- index,
916
- isExpanding: true,
917
- candidate: undefined,
918
- rebuilt: { state: previous, level: depths[index] },
919
- });
920
- const getKey = isHeld ? (values: Value[]) => values : getFingerprintState;
921
- const key = getKey(state);
922
- try {
923
- return coverage
924
- ? coverageActions.find((item) =>
925
- getCoveredSuccessors(item, previous).some((successor) =>
926
- isSameState(getKey(successor.state), key),
927
- ),
928
- )
929
- : actions.find((item) =>
930
- getSuccessors(item.expression, previous, environment, item.scope).some((successor) =>
931
- isSameState(getKey(successor.state), key),
932
- ),
933
- );
934
- } finally {
935
- Object.assign(current, expanding);
936
- }
937
- };
938
- const toTrace = (
939
- steps: Successor[],
940
- path: number[],
941
- traceOptions: TraceOptions = {},
942
- ): Behavior => {
943
- const alias = config.alias;
944
- const known = traceOptions.known ?? 1;
945
- const initialOutput = steps.length > 1 ? captureOutput(getSpecInitialStates).lines : [];
946
- const stepActions = steps.map((step, position) =>
947
- position === 0 || step.action === "Stuttering"
948
- ? { result: undefined, lines: [] }
949
- : captureOutput(() =>
950
- findStepAction(
951
- path[position - 1],
952
- steps[position - 1].state,
953
- step.state,
954
- known === 2 && position === steps.length - 1,
955
- ),
956
- ),
957
- );
958
- const outputs = stepActions.map((step) => step.lines);
959
- const loopStart = traceOptions.loopStart;
960
- const rebuilt = is174 && loopStart !== undefined ? loopStart + 1 : steps.length - known;
961
- if (coverage && steps.length > 1) {
962
- const isRebuiltAgain = known > 0 && !is174;
963
- if (rebuilt > 0 && isRebuiltAgain) captureOutput(getSpecInitialStates);
964
- if (isRebuiltAgain) {
965
- steps.slice(1).forEach((step, position) => {
966
- if (step.action === "Stuttering") return;
967
- captureOutput(() =>
968
- findStepAction(
969
- path[position],
970
- steps[position].state,
971
- step.state,
972
- known === 2 && position === steps.length - 2,
973
- ),
974
- );
975
- });
976
- }
977
- }
978
- const prefixOutput = rebuilt > 0 ? [initialOutput, ...outputs.slice(1, rebuilt)].flat() : [];
979
- return {
980
- trace: steps.map((step, position) => {
981
- const action = stepActions[position].result;
982
- const traceStep: TraceStep = {
983
- action: step.action,
984
- label: action && getActionLabel(action, options.version),
985
- state:
986
- alias === undefined
987
- ? toRecord(step.state)
988
- : getAliasState(alias, steps, position, loopStart),
989
- output: position === 0 ? prefixOutput : position < rebuilt ? [] : outputs[position],
990
- };
991
- if (alias !== undefined || action) {
992
- traceSteps.set(traceStep, { state: toRecord(step.state), action });
993
- }
994
- return traceStep;
995
- }),
996
- traceOutput: known > 0 && !is174 ? [initialOutput, ...outputs].flat() : [],
997
- };
998
- };
999
757
  const getSpecInitialStates = () => {
1000
758
  if (coverage) return getCoveredInitialStates();
1001
759
  return spec.init === undefined ? [] : getInitialStates(spec.init, environment, spec.scope);
1002
760
  };
1003
- const findTraceSuccessor = (state: Value[], isMatch: (successor: Successor) => boolean) =>
1004
- captureOutput(() => {
1005
- for (const action of getSpecNextActions()) {
1006
- const successor = getSuccessors(action.expression, state, environment, action.scope).find(
1007
- isMatch,
1008
- );
1009
- if (successor) return { ...successor, action: successor.action ?? action.name };
1010
- }
1011
- return undefined;
1012
- }).result;
1013
- const hasFingerprint = (state: Value[], index: number) =>
1014
- isSameState(getFingerprintState(state), fingerprintStates[index]);
1015
- const findSuccessorWithFingerprint = (state: Value[], index: number) =>
1016
- findTraceSuccessor(state, (successor) => hasFingerprint(successor.state, index));
1017
- const getPathStates = (path: number[], nextFailure: string) => {
1018
- const initial = captureOutput(() =>
1019
- isReducing || isImpure(environment)
1020
- ? getSpecInitialStates().find((state) => hasFingerprint(state, path[0]))
1021
- : states[path[0]],
1022
- ).result;
1023
- if (!initial) throw new TlaEvaluationError(INITIAL_RECOVERY_FAILURE);
1024
- const steps: Successor[] = [{ state: initial, action: undefined }];
1025
- for (let position = 1; position < path.length; position++) {
1026
- const previous = steps[position - 1].state;
1027
- if (path[position - 1] === path[position]) {
1028
- steps.push({ state: previous, action: "Stuttering" });
1029
- continue;
1030
- }
1031
- const rebuilt = current.rebuilt;
1032
- current.rebuilt = { state: previous, level: depths[path[position - 1]] };
1033
- const successor = findSuccessorWithFingerprint(previous, path[position]);
1034
- current.rebuilt = rebuilt;
1035
- if (!successor) throw new TlaEvaluationError(nextFailure);
1036
- steps.push(successor);
1037
- }
1038
- return steps;
1039
- };
1040
- const getTrace = (index: number, lastSuccessor?: Successor) => {
1041
- const path: number[] = [];
1042
- for (let position = index; position !== -1; position = parents[position]) {
1043
- path.unshift(position);
1044
- }
1045
- const steps =
1046
- isReducing && path.length > 1
1047
- ? getPathStates(path, INITIAL_RECOVERY_FAILURE)
1048
- : path.map((position) => ({ state: states[position], action: actions[position] }));
1049
- if (!lastSuccessor) return toTrace(steps, path);
1050
- const action = isReducing
1051
- ? findTraceSuccessor(states[index], (successor) =>
1052
- isSameState(successor.state, lastSuccessor.state),
1053
- )?.action
1054
- : lastSuccessor.action;
1055
- return toTrace([...steps, { state: lastSuccessor.state, action }], path, { known: 2 });
1056
- };
1057
- // Deviation from TLC: TLCTrace#printTrace prints a lone initial state without the alias.
1058
- const getStateTrace = (index: number): Behavior =>
1059
- parents[index] === -1
1060
- ? {
1061
- trace: [
1062
- {
1063
- action: undefined,
1064
- label: undefined,
1065
- state:
1066
- config.alias === undefined
1067
- ? toRecord(states[index])
1068
- : getAliasState(
1069
- config.alias,
1070
- [{ state: states[index], action: undefined }],
1071
- 0,
1072
- undefined,
1073
- ),
1074
- },
1075
- ],
1076
- traceOutput: is174 ? [] : captureOutput(getSpecInitialStates).lines,
1077
- }
1078
- : getTrace(index);
1079
761
  const isUnfingerprintable = (state: Value[]) => {
1080
762
  try {
1081
763
  return getFingerprintState(state).some((value) => getFingerprintError(value) !== undefined);
@@ -1084,31 +766,6 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1084
766
  return true;
1085
767
  }
1086
768
  };
1087
- const getFailedTrace = (index: number, successor: Successor): Behavior => ({
1088
- trace: getTrace(index, successor).trace,
1089
- });
1090
- const getFailureExitCode = (error: TlaEvaluationError, failureOptions: FailureOptions) => {
1091
- if (
1092
- failureOptions.isInitial &&
1093
- !failureOptions.callStack &&
1094
- !failureOptions.fingerprint &&
1095
- is174
1096
- ) {
1097
- return EXIT_STATUS.success;
1098
- }
1099
- if (!failureOptions.callStack) {
1100
- return failureOptions.isInitial ? getInitialExitCode(error) : getErrorExitCode(error);
1101
- }
1102
- if (failureOptions.isCallStackFirst) return getErrorExitCode(error);
1103
- if (failureOptions.isLiveCallStack) {
1104
- return (
1105
- getExitCode(error) ?? (error instanceof TlaModuleError ? error.exitCode : EXIT_STATUS.error)
1106
- );
1107
- }
1108
- return failureOptions.isInitial || failureOptions.fingerprint
1109
- ? EXIT_STATUS.specEvaluationFailure
1110
- : getReplayExitCode(failureOptions.replayError ?? error);
1111
- };
1112
769
  const createFailureResult = (
1113
770
  failed: unknown,
1114
771
  behavior: Behavior,
@@ -1124,7 +781,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1124
781
  const traceOutput = [...(behavior.traceOutput ?? []), ...(checked?.lines ?? [])];
1125
782
  return {
1126
783
  ...createResult("error", undefined, {
1127
- ...getFailureMessage(error, failureOptions.isNextState ?? false),
784
+ ...getFailureMessage(
785
+ error,
786
+ failureOptions.phase !== undefined && failureOptions.phase !== "initial",
787
+ ),
1128
788
  ...behavior,
1129
789
  ...(traceOutput.length > 0 && { traceOutput }),
1130
790
  ...(postcondition && { postcondition }),
@@ -1132,9 +792,9 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1132
792
  failureOptions.initialState && !(error instanceof TlaModuleError)
1133
793
  ? toRecord(failureOptions.initialState)
1134
794
  : undefined,
1135
- exitCode: getFailureExitCode(error, failureOptions),
795
+ exitCode: getFailureExitCode(error, failureOptions, is174),
1136
796
  callStack: failureOptions.callStack,
1137
- ...(failureOptions.isCallStackFirst && { isCallStackFirst: true }),
797
+ ...(failureOptions.phase === "liveness" && { isCallStackFirst: true }),
1138
798
  ...(failureOptions.fingerprint && { fingerprint: failureOptions.fingerprint }),
1139
799
  ...(failureOptions.traceFailure && { traceFailure: failureOptions.traceFailure }),
1140
800
  }),
@@ -1143,9 +803,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1143
803
  };
1144
804
  const reportIncompleteSuccessor = (index: number, error: TlaIncompleteSuccessorError) => {
1145
805
  runtime.generatedStates++;
1146
- const actions = getSpecNextActions();
1147
806
  const named =
1148
- actions.length === 1 ? actions : actions.filter((item) => item.name === error.action);
807
+ nextActions.length === 1
808
+ ? nextActions
809
+ : nextActions.filter((item) => item.name === error.action);
1149
810
  const action =
1150
811
  named.length === 1
1151
812
  ? named[0]
@@ -1157,12 +818,12 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1157
818
  const message = getIncompleteSuccessorMessage(
1158
819
  error.variables,
1159
820
  error.action,
1160
- actions.length,
821
+ nextActions.length,
1161
822
  options.version,
1162
823
  );
1163
824
  const undefinedValue = createModelValue("null");
1164
825
  const partial = error.state.map((value) => value ?? undefinedValue);
1165
- const failure = createFailureResult(error, getStateTrace(index));
826
+ const failure = createFailureResult(error, traces.getStateTrace(index));
1166
827
  return withOutDegrees({
1167
828
  ...failure,
1168
829
  failure: failure.failure && {
@@ -1182,65 +843,12 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1182
843
  },
1183
844
  });
1184
845
  };
1185
- const getPathSteps = (path: number[], loopStart?: number) => {
1186
- const steps = getPathStates(path, NEXT_RECOVERY_FAILURE);
1187
- const lastState = steps[steps.length - 1].state;
1188
- const isClosed =
1189
- !isReducing ||
1190
- loopStart === undefined ||
1191
- loopStart === path.length - 1 ||
1192
- findSuccessorWithFingerprint(lastState, path[loopStart]) !== undefined;
1193
- if (!isClosed) throw new TlaEvaluationError(NEXT_RECOVERY_FAILURE);
1194
- return steps;
1195
- };
1196
- const getStateHash = (state: Value[]) => {
1197
- let hash = 0x811c9dc5;
1198
- for (let index = 0; index < state.length; index++)
1199
- hash = combineHash(hash, getValueHash(state[index]));
1200
- return hash & 0x3fffffff;
1201
- };
1202
846
  const fingerprintModelState = (state: Value[]) => {
1203
847
  for (const value of state) {
1204
848
  const error = getFingerprintError(value);
1205
849
  if (error) throw error;
1206
850
  }
1207
851
  };
1208
- const isSameState = (stored: Value[], state: Value[]) => {
1209
- for (let position = 0; position < state.length; position++) {
1210
- if (!isSameValue(stored[position], state[position])) return false;
1211
- }
1212
- return true;
1213
- };
1214
- const getSlot = (hash: number) => (Math.imul(hash, 0x9e3779b1) >>> seenStates.shift) << 1;
1215
- const findStateIndex = (hash: number, state: Value[]) => {
1216
- const mask = seenStates.slots.length - 1;
1217
- for (let slot = getSlot(hash); ; slot = (slot + 2) & mask) {
1218
- const entry = seenStates.slots[slot + 1];
1219
- if (entry === 0) return -1;
1220
- if (seenStates.slots[slot] === hash && isSameState(fingerprintStates[entry - 1], state)) {
1221
- return entry - 1;
1222
- }
1223
- }
1224
- };
1225
- const insertSeen = (hash: number, entry: number) => {
1226
- const mask = seenStates.slots.length - 1;
1227
- let slot = getSlot(hash);
1228
- while (seenStates.slots[slot + 1] !== 0) slot = (slot + 2) & mask;
1229
- seenStates.slots[slot] = hash;
1230
- seenStates.slots[slot + 1] = entry;
1231
- };
1232
- const markSeen = (hash: number, index: number) => {
1233
- seenStates.size++;
1234
- if (seenStates.size * 4 > seenStates.slots.length) {
1235
- const slots = seenStates.slots;
1236
- seenStates.slots = new Int32Array(slots.length * 2);
1237
- seenStates.shift--;
1238
- for (let slot = 0; slot < slots.length; slot += 2) {
1239
- if (slots[slot + 1] !== 0) insertSeen(slots[slot], slots[slot + 1]);
1240
- }
1241
- }
1242
- insertSeen(hash, index + 1);
1243
- };
1244
852
  let unstoredFingerprint: Value[] | undefined;
1245
853
  const countDistinctState = (depth: number) => {
1246
854
  runtime.distinctStates = states.length + 1;
@@ -1269,11 +877,33 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1269
877
  livenessSource: -1,
1270
878
  rebuilt: undefined,
1271
879
  };
880
+ const traces = createTraceBuilder({
881
+ config,
882
+ options,
883
+ environment,
884
+ search,
885
+ nextActions,
886
+ current,
887
+ states,
888
+ fingerprintStates,
889
+ parents,
890
+ actions,
891
+ depths,
892
+ tracedSteps,
893
+ captureOutput,
894
+ getInitialStates: getSpecInitialStates,
895
+ findSuccessorAction: (state, isMatch) =>
896
+ coverage
897
+ ? coverageActions.find((item) => getCoveredSuccessors(item, state).some(isMatch))
898
+ : nextActions.find((item) =>
899
+ getSuccessors(item.expression, state, environment, item.scope).some(isMatch),
900
+ ),
901
+ });
1272
902
  const getPath = (index: number): Value[][] =>
1273
903
  index === -1 ? [] : [...getPath(parents[index]), states[index]];
1274
904
  runtime.isSeen = (state) => {
1275
905
  const fingerprintState = getFingerprintState(state);
1276
- return findStateIndex(getStateHash(fingerprintState), fingerprintState) !== -1;
906
+ return findStateIndex(seenStates, getStateHash(fingerprintState), fingerprintState) !== -1;
1277
907
  };
1278
908
  runtime.getBehavior = (state) => {
1279
909
  const index = state === current.candidate ? -1 : states.indexOf(state);
@@ -1309,7 +939,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1309
939
  formatState(
1310
940
  config.alias === undefined
1311
941
  ? toRecord(state)
1312
- : evaluateAlias(config.alias, state, undefined),
942
+ : search.evaluateAlias(config.alias, state, undefined, options.isTool ?? false),
1313
943
  ),
1314
944
  getText: () => formatState(toRecord(state)),
1315
945
  });
@@ -1328,7 +958,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1328
958
  fingerprintModelState(fingerprintState);
1329
959
  state.forEach(markFingerprinted);
1330
960
  }
1331
- const isSeen = isInModel && findStateIndex(hash, fingerprintState) !== -1;
961
+ const isSeen = isInModel && findStateIndex(seenStates, hash, fingerprintState) !== -1;
1332
962
  if (isSeen) return undefined;
1333
963
  unstoredFingerprint = isInModel ? fingerprintState : undefined;
1334
964
  if (isInModel) {
@@ -1341,7 +971,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1341
971
  search.getViolatedInvariants(state).forEach((invariant) =>
1342
972
  reportContinuedViolation("invariant-violation", {
1343
973
  name: invariant.name,
1344
- ...toTrace([{ action: undefined, state }], []),
974
+ ...traces.toTrace([{ action: undefined, state }], []),
1345
975
  }),
1346
976
  );
1347
977
  }
@@ -1349,18 +979,18 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1349
979
  if (violated) {
1350
980
  return reportViolation("invariant-violation", {
1351
981
  name: violated.name,
1352
- ...toTrace([{ action: undefined, state }], []),
982
+ ...traces.toTrace([{ action: undefined, state }], []),
1353
983
  });
1354
984
  }
1355
985
  const failedPredicate = search.findFailedInitialPredicate(state);
1356
986
  if (failedPredicate) {
1357
987
  return reportViolation("liveness-violation", {
1358
988
  name: failedPredicate.name,
1359
- ...toTrace([{ action: undefined, state }], []),
989
+ ...traces.toTrace([{ action: undefined, state }], []),
1360
990
  });
1361
991
  }
1362
992
  if (!isInModel) return undefined;
1363
- markSeen(hash, states.length);
993
+ markSeenState(seenStates, hash, states.length);
1364
994
  addState(state, fingerprintState, -1, undefined);
1365
995
  return undefined;
1366
996
  };
@@ -1378,7 +1008,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1378
1008
  if (isCanonicalizedFirst) canonicalizeState(successor.state);
1379
1009
  const fingerprintState = getFingerprintState(successor.state);
1380
1010
  const hash = getStateHash(fingerprintState);
1381
- const foundIndex = findStateIndex(hash, fingerprintState);
1011
+ const foundIndex = findStateIndex(seenStates, hash, fingerprintState);
1382
1012
  const isStateInModel = search.satisfiesConstraints(successor.state, state);
1383
1013
  if (isStateInModel) search.trackStatePossible(successor.state);
1384
1014
  const isInModel = isStateInModel && search.satisfiesActionConstraints(state, successor.state);
@@ -1425,7 +1055,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1425
1055
  if (existing === -1) {
1426
1056
  try {
1427
1057
  const violated = search.findViolatedInvariant(successor.state, state);
1428
- const violation = violated && { name: violated.name, ...getTrace(index, successor) };
1058
+ const violation = violated && { name: violated.name, ...traces.getTrace(index, successor) };
1429
1059
  if (violation && !options.isContinued) {
1430
1060
  return withOutDegrees(reportViolation("invariant-violation", violation));
1431
1061
  }
@@ -1435,7 +1065,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1435
1065
  const replay = captureOutput(() => replayNext(index));
1436
1066
  return createFailureResult(
1437
1067
  error,
1438
- appendTraceOutput(getFailedTrace(index, successor), replay.lines),
1068
+ appendTraceOutput(traces.getFailedTrace(index, successor), replay.lines),
1439
1069
  replay.result ?? { outDegrees: getFailureOutDegrees() },
1440
1070
  );
1441
1071
  }
@@ -1443,7 +1073,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1443
1073
  const violatedProperty = search.findViolatedActionProperty(state, successor.state);
1444
1074
  const propertyViolation = violatedProperty && {
1445
1075
  name: violatedProperty.name,
1446
- ...getTrace(index, successor),
1076
+ ...traces.getTrace(index, successor),
1447
1077
  };
1448
1078
  if (propertyViolation && !options.isContinued) {
1449
1079
  return withOutDegrees(reportViolation("liveness-violation", propertyViolation));
@@ -1452,7 +1082,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1452
1082
  if (!isNew) return undefined;
1453
1083
  if (coverage) countCoverageVariables(coverage, successor.state);
1454
1084
  if (!isCanonicalizedFirst) canonicalizeState(successor.state);
1455
- markSeen(hash, states.length);
1085
+ markSeenState(seenStates, hash, states.length);
1456
1086
  addState(successor.state, fingerprintState, index, successor.action);
1457
1087
  runtime.queueSize++;
1458
1088
  return undefined;
@@ -1473,7 +1103,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1473
1103
  return createFailureResult(
1474
1104
  error,
1475
1105
  { trace: [], traceOutput: replay.lines },
1476
- { initialState: state, ...replay.result, isInitial: true },
1106
+ { initialState: state, ...replay.result, phase: "initial" },
1477
1107
  );
1478
1108
  };
1479
1109
  try {
@@ -1500,9 +1130,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1500
1130
  (state) => {
1501
1131
  if (onInitialState(state)) throw STOP_WALK;
1502
1132
  },
1503
- (partial) => {
1504
- throw setExitCode(new TlaIllegalStateError([...partial]), EXIT_STATUS.error);
1505
- },
1133
+ rejectIncompleteInitialState,
1506
1134
  );
1507
1135
  } catch (error) {
1508
1136
  if (error !== STOP_WALK) throw error;
@@ -1529,7 +1157,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1529
1157
  return createFailureResult(
1530
1158
  error,
1531
1159
  { trace: [], traceOutput: replay.lines },
1532
- { ...replay.result, isInitial: true },
1160
+ { ...replay.result, phase: "initial" },
1533
1161
  );
1534
1162
  }
1535
1163
  }
@@ -1589,7 +1217,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1589
1217
  );
1590
1218
  }
1591
1219
  let isStopped = false;
1592
- return getSpecNextActions().reduce((count, action) => {
1220
+ return nextActions.reduce((count, action) => {
1593
1221
  if (isStopped) return count;
1594
1222
  return (
1595
1223
  count +
@@ -1610,7 +1238,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1610
1238
  };
1611
1239
  const getLiveCallStack = (index: number, liveSuccessors: LiveSuccessor[]) => {
1612
1240
  if (!liveness || is174) return undefined;
1613
- const replay = getReplayFailure((tracing) =>
1241
+ const replay = getReplayFailure(environment, (tracing) =>
1614
1242
  liveness.addState(index, states[index], liveSuccessors, tracing),
1615
1243
  );
1616
1244
  return replay?.callStack.length && !replay.fingerprint ? replay.callStack : undefined;
@@ -1627,14 +1255,14 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1627
1255
  const replayFailedNext = (index: number, liveSuccessors: LiveSuccessor[]) => {
1628
1256
  const live = captureOutput(() => addFailedLiveState(index, liveSuccessors));
1629
1257
  if (live.result) {
1630
- const result = { callStack: live.result, fingerprint: undefined, isLiveCallStack: true };
1258
+ const result: FailureOptions = { phase: "behavior-graph", callStack: live.result };
1631
1259
  return { result, lines: live.lines };
1632
1260
  }
1633
1261
  const replay = captureOutput(() => replayNext(index));
1634
1262
  return { result: replay.result, lines: [...live.lines, ...replay.lines] };
1635
1263
  };
1636
1264
  const reportNextStateOverflow = (index: number) =>
1637
- createFailureResult(createStackOverflowError(), getStateTrace(index), { isNextState: true });
1265
+ createFailureResult(createStackOverflowError(), traces.getStateTrace(index), { phase: "next" });
1638
1266
  const reportNextStateFailure = (
1639
1267
  error: unknown,
1640
1268
  index: number,
@@ -1644,8 +1272,8 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1644
1272
  const replay = replayFailedNext(index, liveSuccessors);
1645
1273
  return createFailureResult(
1646
1274
  withNextStateExitCode(error),
1647
- appendTraceOutput(getStateTrace(index), replay.lines),
1648
- { isNextState: true, ...replay.result },
1275
+ appendTraceOutput(traces.getStateTrace(index), replay.lines),
1276
+ { phase: "next", ...replay.result },
1649
1277
  );
1650
1278
  };
1651
1279
  const expandState = (index: number, liveSuccessors: LiveSuccessor[]) => {
@@ -1676,7 +1304,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1676
1304
  outcome = createFailureResult(
1677
1305
  new TlaEvaluationError(`${getWrappedException(error)}${error.message}`, error),
1678
1306
  appendTraceOutput({ trace: [] }, replay.lines),
1679
- { isNextState: true, ...replay.result },
1307
+ { phase: "next", ...replay.result },
1680
1308
  );
1681
1309
  return true;
1682
1310
  }
@@ -1693,10 +1321,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1693
1321
  outcome = createFailureResult(
1694
1322
  error,
1695
1323
  appendTraceOutput(
1696
- traceFailure ? getStateTrace(index) : getFailedTrace(index, successor),
1324
+ traceFailure ? traces.getStateTrace(index) : traces.getFailedTrace(index, successor),
1697
1325
  replay.lines,
1698
1326
  ),
1699
- { isNextState: true, ...replay.result, traceFailure },
1327
+ { phase: "next", ...replay.result, traceFailure },
1700
1328
  );
1701
1329
  }
1702
1330
  return outcome !== undefined;
@@ -1718,7 +1346,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1718
1346
  }
1719
1347
  const accepted = liveness?.addState(index, state, liveSuccessors);
1720
1348
  if (!liveness || !accepted) return undefined;
1721
- const steps = getPathSteps(accepted);
1349
+ const steps = traces.getPathSteps(accepted);
1722
1350
  const properties = getViolatedProperties(
1723
1351
  liveness,
1724
1352
  steps.map((step) => step.state),
@@ -1727,11 +1355,14 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1727
1355
  return reportViolation("property-violation", {
1728
1356
  name: properties[0],
1729
1357
  properties,
1730
- ...toTrace(steps, accepted, { known: 0 }),
1358
+ ...traces.toTrace(steps, accepted, { known: 0 }),
1731
1359
  });
1732
1360
  };
1733
1361
  if (successorCount === 0 && config.checkDeadlock) {
1734
- const deadlock = reportViolation("deadlock", { name: "deadlock", ...getStateTrace(index) });
1362
+ const deadlock = reportViolation("deadlock", {
1363
+ name: "deadlock",
1364
+ ...traces.getStateTrace(index),
1365
+ });
1735
1366
  const live = captureOutput(() => {
1736
1367
  try {
1737
1368
  return { additional: addLiveState()?.violation };
@@ -1764,14 +1395,13 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1764
1395
  { outDegrees: getFailureOutDegrees() },
1765
1396
  );
1766
1397
  }
1767
- const replay = getReplayFailure((tracing) =>
1398
+ const replay = getReplayFailure(environment, (tracing) =>
1768
1399
  liveness?.addState(index, state, liveSuccessors, tracing),
1769
1400
  );
1770
- return createFailureResult(error, getStateTrace(index), {
1401
+ return createFailureResult(error, traces.getStateTrace(index), {
1771
1402
  outDegrees: getFailureOutDegrees(),
1772
1403
  callStack: replay?.callStack.length ? replay.callStack : undefined,
1773
- isNextState: true,
1774
- isCallStackFirst: true,
1404
+ phase: "liveness",
1775
1405
  });
1776
1406
  }
1777
1407
  };
@@ -1795,8 +1425,8 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1795
1425
  const reportLasso = (checker: LivenessChecker, lasso: Lasso) => {
1796
1426
  const path = [...lasso.prefix, ...lasso.cycle];
1797
1427
  const loopStart = lasso.prefix.length;
1798
- const steps = getPathSteps(path, loopStart);
1799
- const trace = toTrace(steps, path, { loopStart, known: 0 }).trace;
1428
+ const steps = traces.getPathSteps(path, loopStart);
1429
+ const trace = traces.toTrace(steps, path, { loopStart, known: 0 }).trace;
1800
1430
  const properties = getViolatedProperties(
1801
1431
  checker,
1802
1432
  steps.map((step) => step.state),
@@ -1808,7 +1438,9 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1808
1438
  const loop =
1809
1439
  cycleStart === cycleEnd
1810
1440
  ? { result: undefined, lines: [] }
1811
- : captureOutput(() => findStepAction(cycleEnd, lastState, states[cycleStart], false));
1441
+ : captureOutput(() =>
1442
+ traces.findStepAction(cycleEnd, lastState, states[cycleStart], false),
1443
+ );
1812
1444
  const isStutteringWarned = cycleStart === cycleEnd && lasso.isCycleConstrained;
1813
1445
  return reportViolation(
1814
1446
  "liveness-violation",
@@ -1826,7 +1458,9 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1826
1458
  ? getStepAction(trace[trace.length - 1])
1827
1459
  : loop.result
1828
1460
  ? toActionValue(loop.result)
1829
- : createActionRecord(findSuccessorWithFingerprint(lastState, cycleStart)?.action),
1461
+ : createActionRecord(
1462
+ traces.findSuccessorWithFingerprint(lastState, cycleStart)?.action,
1463
+ ),
1830
1464
  );
1831
1465
  };
1832
1466
  const isPeriodic = liveness !== undefined && (options.livenessCheck ?? "default") === "default";