tla-ts 0.0.10-dev.c92e6a4ea → 0.0.10-dev.d166e0ae9

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 (235) hide show
  1. package/dist/compile.cjs +3 -3
  2. package/dist/compile.js +1 -1
  3. package/dist/deciders.cjs +36 -36
  4. package/dist/deciders.js +54 -54
  5. package/dist/index.cjs +1 -1
  6. package/dist/index.d.cts +13 -3
  7. package/dist/index.d.ts +13 -3
  8. package/dist/index.js +1 -1
  9. package/dist/parallel-worker.cjs +1 -1
  10. package/dist/parallel-worker.js +1 -1
  11. package/dist/prove-worker.cjs +1 -1
  12. package/dist/prove-worker.js +1 -1
  13. package/dist/prove.cjs +68 -68
  14. package/dist/prove.js +78 -78
  15. package/dist/run.cjs +42 -42
  16. package/dist/run.js +44 -44
  17. package/dist/tlapm.cjs +1 -1
  18. package/dist/tlapm.js +2 -2
  19. package/dist/transfer.cjs +45 -43
  20. package/dist/transfer.js +45 -43
  21. package/dist/worker.cjs +7 -7
  22. package/dist/worker.js +7 -7
  23. package/package.json +1 -1
  24. package/src/ast.ts +11 -1
  25. package/src/bags.ts +50 -29
  26. package/src/builtins.ts +4 -3
  27. package/src/check-failure.ts +9 -6
  28. package/src/check-trace.ts +116 -64
  29. package/src/check.ts +356 -173
  30. package/src/cli/tlc-output.ts +1 -0
  31. package/src/cli/tlc.ts +4 -3
  32. package/src/cli/xml-exporter.ts +1 -1
  33. package/src/codegen.ts +34 -13
  34. package/src/coverage.ts +3 -0
  35. package/src/dependencies.ts +61 -13
  36. package/src/dfid.ts +22 -10
  37. package/src/environment.ts +71 -35
  38. package/src/errors.ts +7 -0
  39. package/src/evaluate.ts +149 -54
  40. package/src/exit-codes.ts +29 -15
  41. package/src/instances.ts +16 -6
  42. package/src/java-hash.ts +14 -0
  43. package/src/java-overrides.ts +2 -2
  44. package/src/levels.ts +186 -97
  45. package/src/liveness-checker.ts +131 -83
  46. package/src/liveness.ts +52 -11
  47. package/src/obligations/index.ts +1 -1
  48. package/src/obligations/normalize.ts +1 -4
  49. package/src/obligations/printer.ts +5 -5
  50. package/src/obligations/source.ts +2 -0
  51. package/src/parser.ts +115 -145
  52. package/src/prover/isabelle/isar/attrib.ts +4 -4
  53. package/src/prover/isabelle/isar/rule-cases.ts +8 -6
  54. package/src/prover/isabelle/provers/clasimp.ts +1 -1
  55. package/src/prover/isabelle/provers/hypsubst.ts +2 -2
  56. package/src/prover/isabelle/pure/rewriter.ts +3 -3
  57. package/src/prover/isabelle/pure/term.ts +3 -3
  58. package/src/prover/prep/md5.ts +26 -29
  59. package/src/prover/smt/rewrite.ts +2 -7
  60. package/src/prover/smt/simplify.ts +1 -1
  61. package/src/prover/smt/solver/arith-rewriter.ts +11 -3
  62. package/src/prover/smt/solver/array-rewriter.ts +12 -2
  63. package/src/prover/smt/solver/asserted-formulas.ts +169 -58
  64. package/src/prover/smt/solver/ast-lt.ts +1 -1
  65. package/src/prover/smt/solver/ast.ts +63 -14
  66. package/src/prover/smt/solver/auf-solver.ts +23 -12
  67. package/src/prover/smt/solver/bit-blast-tactic.ts +26 -0
  68. package/src/prover/smt/solver/blast-term-ite.ts +47 -0
  69. package/src/prover/smt/solver/bound-simplifier.ts +11 -13
  70. package/src/prover/smt/solver/bv-decl-plugin.ts +13 -10
  71. package/src/prover/smt/solver/bv-rewriter.ts +1 -1
  72. package/src/prover/smt/solver/card2bv.ts +35 -18
  73. package/src/prover/smt/solver/cofactor-elim-term-ite.ts +39 -24
  74. package/src/prover/smt/solver/context.ts +67 -13
  75. package/src/prover/smt/solver/ctx-simplify.ts +1 -2
  76. package/src/prover/smt/solver/datatype-decl-plugin.ts +15 -7
  77. package/src/prover/smt/solver/dependent-expr-state.ts +26 -15
  78. package/src/prover/smt/solver/diff-neq.ts +1 -2
  79. package/src/prover/smt/solver/dt2bv-tactic.ts +36 -20
  80. package/src/prover/smt/solver/elim-bounds.ts +89 -0
  81. package/src/prover/smt/solver/elim-term-ite-tactic.ts +48 -0
  82. package/src/prover/smt/solver/elim-uncnstr.ts +5 -8
  83. package/src/prover/smt/solver/euf/arith-internalize.ts +1 -1
  84. package/src/prover/smt/solver/euf/array-internalize.ts +1 -1
  85. package/src/prover/smt/solver/euf/bv-internalize.ts +1 -1
  86. package/src/prover/smt/solver/euf/egraph.ts +4 -11
  87. package/src/prover/smt/solver/euf/euf-solver.ts +1 -2
  88. package/src/prover/smt/solver/euf/mam.ts +1 -5
  89. package/src/prover/smt/solver/euf/q-ematch.ts +1 -2
  90. package/src/prover/smt/solver/euf/th-solver.ts +0 -7
  91. package/src/prover/smt/solver/expr2polynomial.ts +204 -79
  92. package/src/prover/smt/solver/factor.ts +36 -14
  93. package/src/prover/smt/solver/fd-solver.ts +148 -39
  94. package/src/prover/smt/solver/fix-dl-var.ts +1 -2
  95. package/src/prover/smt/solver/fpa-rewriter.ts +1 -2
  96. package/src/prover/smt/solver/fpa2bv-converter.ts +3 -1
  97. package/src/prover/smt/solver/fpa2bv-rewriter.ts +1 -0
  98. package/src/prover/smt/solver/goal.ts +6 -3
  99. package/src/prover/smt/solver/goal2nlsat.ts +57 -30
  100. package/src/prover/smt/solver/hashtable.ts +4 -0
  101. package/src/prover/smt/solver/internalizer.ts +1 -1
  102. package/src/prover/smt/solver/logic-tactics.ts +9 -2
  103. package/src/prover/smt/solver/lp/lp-settings.ts +5 -1
  104. package/src/prover/smt/solver/lp/nla-core.ts +1 -1
  105. package/src/prover/smt/solver/lp/nla-grobner.ts +2 -2
  106. package/src/prover/smt/solver/lp/nra-solver.ts +45 -39
  107. package/src/prover/smt/solver/lp/pdd.ts +2 -3
  108. package/src/prover/smt/solver/lp/union-find.ts +1 -1
  109. package/src/prover/smt/solver/mam-compiler.ts +1 -1
  110. package/src/prover/smt/solver/maxcore.ts +2 -1
  111. package/src/prover/smt/solver/maxsmt-base.ts +1 -0
  112. package/src/prover/smt/solver/model-based-opt.ts +1 -1
  113. package/src/prover/smt/solver/model-checker.ts +1 -1
  114. package/src/prover/smt/solver/model-evaluator.ts +2 -0
  115. package/src/prover/smt/solver/model-generator.ts +85 -43
  116. package/src/prover/smt/solver/mpf.ts +2 -4
  117. package/src/prover/smt/solver/mus.ts +2 -3
  118. package/src/prover/smt/solver/muz/dl-context.ts +22 -44
  119. package/src/prover/smt/solver/muz/dl-mk-coi-filter.ts +1 -1
  120. package/src/prover/smt/solver/muz/dl-mk-slice.ts +3 -3
  121. package/src/prover/smt/solver/muz/dl-rule.ts +8 -9
  122. package/src/prover/smt/solver/muz/hnf.ts +2 -2
  123. package/src/prover/smt/solver/muz/horn-subsume-model-converter.ts +1 -1
  124. package/src/prover/smt/solver/muz/horn-tactic.ts +15 -2
  125. package/src/prover/smt/solver/muz/rule-properties.ts +22 -0
  126. package/src/prover/smt/solver/muz/spacer-context.ts +2 -4
  127. package/src/prover/smt/solver/muz/spacer-pdr.ts +1 -0
  128. package/src/prover/smt/solver/muz/spacer-prop-solver.ts +1 -1
  129. package/src/prover/smt/solver/nlqsat.ts +1 -1
  130. package/src/prover/smt/solver/nlsat/algebraic.ts +196 -92
  131. package/src/prover/smt/solver/nlsat/atoms.ts +52 -33
  132. package/src/prover/smt/solver/nlsat/explain.ts +375 -217
  133. package/src/prover/smt/solver/nlsat/levelwise.ts +460 -240
  134. package/src/prover/smt/solver/nlsat/polynomial.ts +2366 -681
  135. package/src/prover/smt/solver/nlsat/solver.ts +74 -64
  136. package/src/prover/smt/solver/nlsat/transcendentals.ts +41 -17
  137. package/src/prover/smt/solver/nlsat/upolynomial-factorization.ts +8 -4
  138. package/src/prover/smt/solver/nlsat/upolynomial.ts +41 -18
  139. package/src/prover/smt/solver/nlsat-tactic.ts +1 -1
  140. package/src/prover/smt/solver/nnf.ts +6 -2
  141. package/src/prover/smt/solver/opt-context.ts +21 -7
  142. package/src/prover/smt/solver/opt-nlsat.ts +29 -18
  143. package/src/prover/smt/solver/opt-pareto.ts +19 -0
  144. package/src/prover/smt/solver/opt-preprocess.ts +1 -1
  145. package/src/prover/smt/solver/opt-solver.ts +10 -6
  146. package/src/prover/smt/solver/optsmt.ts +1 -1
  147. package/src/prover/smt/solver/parallel-tactic.ts +1 -1
  148. package/src/prover/smt/solver/pb-rewriter.ts +1 -4
  149. package/src/prover/smt/solver/poly-rewriter.ts +75 -18
  150. package/src/prover/smt/solver/probes.ts +88 -23
  151. package/src/prover/smt/solver/proto-model.ts +8 -8
  152. package/src/prover/smt/solver/push-app-ite.ts +37 -0
  153. package/src/prover/smt/solver/qe-arith-plugin.ts +2 -4
  154. package/src/prover/smt/solver/qe-lite.ts +2 -2
  155. package/src/prover/smt/solver/qe.ts +3 -2
  156. package/src/prover/smt/solver/qsat.ts +23 -8
  157. package/src/prover/smt/solver/quantifier-manager.ts +14 -9
  158. package/src/prover/smt/solver/recfun-decl-plugin.ts +2 -1
  159. package/src/prover/smt/solver/rewriter-tpl.ts +7 -6
  160. package/src/prover/smt/solver/rewriter.ts +31 -25
  161. package/src/prover/smt/solver/rlimit.ts +4 -0
  162. package/src/prover/smt/solver/sat/config.ts +1 -1
  163. package/src/prover/smt/solver/sat/goal2sat.ts +0 -1
  164. package/src/prover/smt/solver/sat/inc-sat-solver.ts +81 -28
  165. package/src/prover/smt/solver/sat/model-converter.ts +1 -1
  166. package/src/prover/smt/solver/sat/pb-solver.ts +1 -1
  167. package/src/prover/smt/solver/sat/sat-tactic.ts +17 -7
  168. package/src/prover/smt/solver/sat/sat2goal.ts +10 -7
  169. package/src/prover/smt/solver/sat/simplifier.ts +1 -1
  170. package/src/prover/smt/solver/sat/solver.ts +25 -47
  171. package/src/prover/smt/solver/seq-axioms.ts +20 -35
  172. package/src/prover/smt/solver/seq-decl-plugin.ts +1 -1
  173. package/src/prover/smt/solver/seq-derive.ts +2 -2
  174. package/src/prover/smt/solver/seq-eq-solver.ts +2 -2
  175. package/src/prover/smt/solver/seq-monadic.ts +7 -5
  176. package/src/prover/smt/solver/seq-re-rewriter.ts +2 -1
  177. package/src/prover/smt/solver/seq-re-util.ts +1 -1
  178. package/src/prover/smt/solver/seq-regex-bisim.ts +1 -1
  179. package/src/prover/smt/solver/seq-regex-live.ts +2 -2
  180. package/src/prover/smt/solver/seq-rewriter.ts +31 -29
  181. package/src/prover/smt/solver/seq-skolem.ts +0 -23
  182. package/src/prover/smt/solver/seq-view-witness.ts +4 -4
  183. package/src/prover/smt/solver/smt-consequences.ts +1 -1
  184. package/src/prover/smt/solver/smt-kernel.ts +48 -7
  185. package/src/prover/smt/solver/smt-setup.ts +96 -10
  186. package/src/prover/smt/solver/smtlib-parser.ts +589 -224
  187. package/src/prover/smt/solver/smtlib-script.ts +674 -195
  188. package/src/prover/smt/solver/solve-eqs.ts +36 -37
  189. package/src/prover/smt/solver/solver.ts +18 -3
  190. package/src/prover/smt/solver/tactic.ts +332 -189
  191. package/src/prover/smt/solver/theory-bv.ts +13 -3
  192. package/src/prover/smt/solver/theory-fpa.ts +1 -1
  193. package/src/prover/smt/solver/theory-lra-final-check.ts +1 -0
  194. package/src/prover/smt/solver/theory-lra-internalize.ts +2 -0
  195. package/src/prover/smt/solver/theory-lra-propagate.ts +11 -3
  196. package/src/prover/smt/solver/theory-lra.ts +1 -1
  197. package/src/prover/smt/solver/theory-pb.ts +24 -7
  198. package/src/prover/smt/solver/theory-seq-solve.ts +1 -1
  199. package/src/prover/smt/solver/theory-seq-state.ts +2 -21
  200. package/src/prover/smt/solver/tseitin-cnf.ts +1 -0
  201. package/src/prover/tlapm/cache.ts +8 -8
  202. package/src/prover/zenon/index-table.ts +5 -2
  203. package/src/prover/zenon/isar-case.ts +1 -1
  204. package/src/prover/zenon/isar.ts +2 -2
  205. package/src/prover/zenon/phrase.ts +1 -1
  206. package/src/resolve.ts +1071 -251
  207. package/src/runtime.ts +5 -1
  208. package/src/sany/builtins.ts +10 -10
  209. package/src/sany/generation.ts +2 -13
  210. package/src/sany/generator-definitions.ts +3 -1
  211. package/src/sany/generator-expressions.ts +38 -30
  212. package/src/sany/generator-proofs.ts +6 -3
  213. package/src/sany/generator.ts +2 -1
  214. package/src/sany/lexer.ts +3 -3
  215. package/src/sany/modules.ts +4 -2
  216. package/src/sany/nodes.ts +1 -1
  217. package/src/sany/selectors.ts +11 -6
  218. package/src/sany/xml.ts +2 -3
  219. package/src/search.ts +197 -117
  220. package/src/simulate.ts +165 -102
  221. package/src/spec-description.ts +8 -6
  222. package/src/standard-modules.ts +56 -22
  223. package/src/subexpressions.ts +518 -123
  224. package/src/successors.ts +10 -3
  225. package/src/symmetry.ts +2 -1
  226. package/src/tlc/jar-data.ts +10 -0
  227. package/src/tlc/messages.ts +3 -3
  228. package/src/tlc/random.ts +2 -1
  229. package/src/tlc/replay.ts +27 -14
  230. package/src/tlc/tlc-operators.ts +10 -8
  231. package/src/tlc/tlcext-operators.ts +26 -10
  232. package/src/tlc/trace-spec.ts +9 -22
  233. package/src/typed-tier.ts +44 -27
  234. package/src/typescript/types.ts +2 -2
  235. package/src/values.ts +135 -78
package/src/check.ts CHANGED
@@ -37,6 +37,7 @@ import {
37
37
  } from "./check-failure";
38
38
  import {
39
39
  createTraceBuilder,
40
+ NEXT_RECOVERY_FAILURE,
40
41
  type Behavior,
41
42
  type SearchPosition,
42
43
  type TracedStep,
@@ -108,7 +109,11 @@ import {
108
109
  walkSuccessors,
109
110
  } from "./tlc/replay";
110
111
  import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc/summary";
111
- import { createActionRecord, createCounterExample } from "./tlc/tlcext-operators";
112
+ import {
113
+ createActionRecord,
114
+ createCounterExample,
115
+ type CounterExampleLoop,
116
+ } from "./tlc/tlcext-operators";
112
117
  import type { TlcVersion } from "./tlc/version";
113
118
  import { runTypedSearch, type TypedResult } from "./typed-tier";
114
119
  import {
@@ -267,6 +272,11 @@ export interface Postcondition {
267
272
  counts?: string;
268
273
  }
269
274
 
275
+ export interface TraceFailure extends FailureMessage {
276
+ /** What the spec prints as TLC rebuilds the step it then fails to print. */
277
+ output: OutputLine[];
278
+ }
279
+
270
280
  /** An evaluation that failed during the search, with the behavior up to the state TLC was working on. */
271
281
  export interface EvaluationFailure {
272
282
  code: number;
@@ -283,7 +293,7 @@ export interface EvaluationFailure {
283
293
  /** The FingerprintException the replay raises, which TLC reports in place of the call stack. */
284
294
  fingerprint?: FingerprintFailure;
285
295
  /** How TLC reports failing again as it prints the trace, since it can't fingerprint the state. */
286
- traceFailure?: FailureMessage;
296
+ traceFailure?: TraceFailure;
287
297
  /** A POSTCONDITION that is false or fails after the trace, which TLC checks as it prints one. */
288
298
  postcondition?: Postcondition;
289
299
  /** Whether TLC stops before it prints the statistics, as it does for an illegal initial state. */
@@ -309,7 +319,7 @@ export interface CheckResult {
309
319
  outDegrees: number[] | undefined;
310
320
  /** The smallest distance between two fingerprints, when TLC estimates a collision from them. */
311
321
  fingerprintDistance: bigint | undefined;
312
- /** A temporal property TLC finds violated as it adds a deadlocked state, reported after. */
322
+ /** A temporal property TLC finds violated as it adds the state it stopped at to the behavior graph, reported after. */
313
323
  additionalViolation?: Violation;
314
324
  /** Where TLC's worker fails to add the state to the behavior graph after it reports a violation. */
315
325
  liveCallStack?: Expression[];
@@ -327,11 +337,30 @@ interface ReportedViolation extends Omit<Violation, "loopStart"> {
327
337
  loopStart?: number;
328
338
  }
329
339
 
340
+ interface CounterExampleOptions {
341
+ isConsole: boolean;
342
+ }
343
+
330
344
  const appendTraceOutput = <Traced extends Behavior>(behavior: Traced, lines: OutputLine[]) => ({
331
345
  ...behavior,
332
346
  traceOutput: [...(behavior.traceOutput ?? []), ...lines],
333
347
  });
334
348
 
349
+ const toUnexpectedError = (error: TlaEvaluationError) =>
350
+ new TlaEvaluationError(
351
+ `${isEvalException(error) ? UNEXPECTED_EVAL_EXCEPTION : UNEXPECTED_EXCEPTION}${error.message}`,
352
+ );
353
+
354
+ const getTraceFailure = (error: TlaEvaluationError, output: OutputLine[]): TraceFailure => ({
355
+ ...getFailureMessage(
356
+ isEvalException(error)
357
+ ? new TlaEvaluationError(`${UNEXPECTED_EVAL_EXCEPTION}${error.message}`)
358
+ : error,
359
+ true,
360
+ ),
361
+ output,
362
+ });
363
+
335
364
  const STOP_REPLAY = new Error("The replay stopped");
336
365
  const STOP_WALK = new Error("The walk stopped");
337
366
 
@@ -508,19 +537,24 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
508
537
  specification.moduleOperators.some((operator) => operator.kind === "postcondition");
509
538
  const tracedSteps = new WeakMap<TraceStep, TracedStep>();
510
539
  const getStepState = (step: TraceStep) => tracedSteps.get(step)?.state ?? step.state;
540
+ // TLC's Worker#doPostCondition and LiveCheck bind CounterExample to the alias of each state, while its initial states and lassos keep the states.
541
+ const getStepAlias = (step: TraceStep) => tracedSteps.get(step)?.alias ?? step.state;
511
542
  const getStepAction = (step: TraceStep) => {
512
543
  const action = tracedSteps.get(step)?.action;
513
544
  return action ? toActionValue(action) : createActionRecord(step.action);
514
545
  };
515
546
  const checkTracePostconditions = (
516
547
  trace: TraceStep[],
517
- loop: { start: number; action: Value } | undefined,
548
+ loop: CounterExampleLoop | undefined,
549
+ getState: (step: TraceStep) => Record<string, Value>,
550
+ isConsole = true,
518
551
  ) => {
519
552
  if (hasPostconditions) {
520
553
  runtime.counterExample = createCounterExample(
521
- trace.map(getStepState),
554
+ trace.map(getState),
522
555
  trace.slice(1).map(getStepAction),
523
556
  loop,
557
+ isConsole,
524
558
  );
525
559
  }
526
560
  const unwitnessed = search.findUnwitnessedPossible();
@@ -532,12 +566,14 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
532
566
  const reportViolation = (
533
567
  status: CheckStatus,
534
568
  violation: ReportedViolation,
569
+ getState = getStepAlias,
535
570
  getLoopAction?: () => Value,
536
571
  ) => {
537
572
  const start = violation.loopStart;
538
573
  const checked = checkTracePostconditions(
539
574
  violation.trace,
540
575
  start === undefined || !getLoopAction ? undefined : { start, action: getLoopAction() },
576
+ getState,
541
577
  );
542
578
  return createResult(status, {
543
579
  ...violation,
@@ -594,6 +630,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
594
630
  onState(successor);
595
631
  },
596
632
  (partial) => {
633
+ if (coverage) coverage.primary[action.node]++;
597
634
  const missing = environment.variables.filter(
598
635
  (_variable, index) => partial[index] === undefined,
599
636
  );
@@ -605,6 +642,23 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
605
642
  );
606
643
  },
607
644
  );
645
+ const hasCoveredIncompleteSuccessor = (action: CoveredAction, state: Value[]) => {
646
+ let complete = 0;
647
+ let incomplete = 0;
648
+ walkSuccessors(
649
+ action,
650
+ action.node,
651
+ state,
652
+ environment,
653
+ () => complete++,
654
+ () => {
655
+ incomplete++;
656
+ return true;
657
+ },
658
+ );
659
+ if (coverage) coverage.primary[action.node] += complete + incomplete;
660
+ return incomplete > 0;
661
+ };
608
662
  const getCoveredSuccessors = (action: CoveredAction, state: Value[]) => {
609
663
  const successors: Successor[] = [];
610
664
  walkCoveredSuccessors(action, state, (successor) =>
@@ -657,7 +711,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
657
711
  const deferred: unknown[] = [];
658
712
  const addReplayed = createReplayedStates();
659
713
  return getReplayFailure(environment, (tracing) => {
660
- replayInitialStates(getDescription().inits, tracing, (state) => {
714
+ replayInitialStates(coverage ? coverageInits : getDescription().inits, tracing, (state) => {
661
715
  reportInitialStatesProgress(replayedCount, (progress) => capturedLines?.push(progress));
662
716
  replayedCount++;
663
717
  runtime.generatedStates = replayedCount;
@@ -668,16 +722,17 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
668
722
  runtime.queueSize++;
669
723
  liveness?.addInitialState(states.length, state, tracing);
670
724
  };
671
- const violation = search.checkReplayedInitialState(state, tracing, addFingerprint);
672
- if (violation) {
725
+ search.checkReplayedInitialState(state, tracing, addFingerprint, (violation) => {
673
726
  const traced = traces.toTrace([{ action: undefined, state }], []).trace[0].state;
674
727
  capturedLines?.push({
675
728
  kind: "initial-state-violation",
676
729
  ...violation,
677
730
  state: traced,
678
731
  });
679
- throw STOP_REPLAY;
680
- }
732
+ if (!options.isContinued || violation.status !== "invariant-violation") {
733
+ throw STOP_REPLAY;
734
+ }
735
+ });
681
736
  } catch (error) {
682
737
  const isFingerprintFailure =
683
738
  error instanceof TlaEvaluationError && tracing.callStack?.fingerprint !== undefined;
@@ -693,7 +748,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
693
748
  const state = states[index];
694
749
  const source = stateWriter && getGraphState(state, depths[index]);
695
750
  const addReplayed = createReplayedStates();
696
- replaySuccessors(nextActions, state, tracing, (next, action) => {
751
+ replaySuccessors(coverage ? coverageActions : nextActions, state, tracing, (next, action) => {
697
752
  const addFingerprint = () => {
698
753
  const isUnseen = addReplayed(next, tracing.callStack);
699
754
  if (source) {
@@ -726,11 +781,8 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
726
781
  (tlcFingerprints[successor.index] ??= Number(
727
782
  BigInt.asIntN(32, runtime.getFingerprint(successor.state)),
728
783
  )),
729
- getSuccessors: (source, state) => {
730
- const successors: LiveSuccessor[] = [];
731
- if (spec.next === undefined) return successors;
732
- const livenessSource = current.livenessSource;
733
- current.livenessSource = source;
784
+ forEachActionSuccessors: (source, state, onSuccessors) => {
785
+ let successors: LiveSuccessor[] = [];
734
786
  const addSuccessor = (successor: Value[]) => {
735
787
  canonicalizeState(successor);
736
788
  const isInModel =
@@ -745,28 +797,34 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
745
797
  state: successor,
746
798
  });
747
799
  };
748
- try {
749
- // TLC's coverage counts the successors of each action the graph generates again.
750
- if (coverage) {
751
- coverageActions.forEach((action) =>
752
- walkCoveredSuccessors(action, state, addSuccessor),
753
- );
754
- } else {
755
- forEachSuccessor(
756
- spec.next,
757
- state,
758
- environment,
759
- (successor) => {
760
- addSuccessor(successor.state);
761
- return false;
762
- },
763
- spec.scope,
764
- );
800
+ const visitSuccessor = (successor: Successor) => {
801
+ addSuccessor(successor.state);
802
+ return false;
803
+ };
804
+ const actionCount = coverage ? coverageActions.length : nextActions.length;
805
+ for (let position = 0; position < actionCount; position++) {
806
+ successors = [];
807
+ const livenessSource = current.livenessSource;
808
+ current.livenessSource = source;
809
+ try {
810
+ // TLC's coverage counts the successors of each action the graph generates again.
811
+ if (coverage) {
812
+ walkCoveredSuccessors(coverageActions[position], state, addSuccessor);
813
+ } else {
814
+ const action = nextActions[position];
815
+ forEachSuccessor(
816
+ action.expression,
817
+ state,
818
+ environment,
819
+ visitSuccessor,
820
+ action.scope,
821
+ );
822
+ }
823
+ } finally {
824
+ current.livenessSource = livenessSource;
765
825
  }
766
- } finally {
767
- current.livenessSource = livenessSource;
826
+ onSuccessors(successors);
768
827
  }
769
- return successors;
770
828
  },
771
829
  }),
772
830
  ),
@@ -801,10 +859,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
801
859
  const canonicalizeState = createCanonicalizer();
802
860
 
803
861
  const toRecord = search.toRecord;
804
- const getSpecInitialStates = () => {
805
- if (coverage) return getCoveredInitialStates();
806
- return spec.init === undefined ? [] : getInitialStates(spec.init, environment, spec.scope);
807
- };
862
+ const getUncoveredInitialStates = () =>
863
+ spec.init === undefined ? [] : getInitialStates(spec.init, environment, spec.scope);
864
+ const getSpecInitialStates = () =>
865
+ coverage ? getCoveredInitialStates() : getUncoveredInitialStates();
808
866
  const isUnfingerprintable = (state: Value[]) => {
809
867
  try {
810
868
  return getFingerprintState(state).some((value) => getFingerprintError(value) !== undefined);
@@ -813,19 +871,27 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
813
871
  return true;
814
872
  }
815
873
  };
874
+ const checkFailedTrace = (behavior: Behavior, counterExample?: CounterExampleOptions) => {
875
+ if (!counterExample) return { behavior, postcondition: undefined };
876
+ const checked = checkTracePostconditions(
877
+ behavior.trace,
878
+ undefined,
879
+ getStepAlias,
880
+ counterExample.isConsole,
881
+ );
882
+ return {
883
+ behavior: appendTraceOutput(behavior, checked.lines),
884
+ postcondition: checked.unwitnessed ?? checked.postcondition,
885
+ };
886
+ };
816
887
  const createFailureResult = (
817
888
  failed: unknown,
818
889
  behavior: Behavior,
819
890
  failureOptions: FailureOptions = {},
891
+ postcondition?: Postcondition,
820
892
  ) => {
821
893
  const error = isStackOverflow(failed) ? new TlaStackOverflowError() : failed;
822
894
  if (!(error instanceof TlaEvaluationError)) throw error;
823
- const checked =
824
- behavior.trace.length > 0 && !failureOptions.traceFailure
825
- ? checkTracePostconditions(behavior.trace, undefined)
826
- : undefined;
827
- const postcondition = checked?.unwitnessed ?? checked?.postcondition;
828
- const traceOutput = [...(behavior.traceOutput ?? []), ...(checked?.lines ?? [])];
829
895
  return {
830
896
  ...createResult("error", undefined, {
831
897
  ...getFailureMessage(
@@ -833,7 +899,6 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
833
899
  failureOptions.phase !== undefined && failureOptions.phase !== "initial",
834
900
  ),
835
901
  ...behavior,
836
- ...(traceOutput.length > 0 && { traceOutput }),
837
902
  ...(postcondition && { postcondition }),
838
903
  initialState:
839
904
  failureOptions.initialState && !(error instanceof TlaModuleError)
@@ -848,29 +913,67 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
848
913
  outDegrees: failureOptions.outDegrees,
849
914
  };
850
915
  };
851
- const reportIncompleteSuccessor = (index: number, error: TlaIncompleteSuccessorError) => {
916
+ // Deviation from TLC: Worker#doPostCondition throws an ArrayIndexOutOfBoundsException at an incomplete successor, where tla-ts checks the postconditions with the successor in CounterExample.
917
+ const reportIncompleteSuccessor = (
918
+ index: number,
919
+ error: TlaIncompleteSuccessorError,
920
+ liveSuccessors: LiveSuccessor[],
921
+ ) => {
852
922
  runtime.generatedStates++;
853
- const named =
854
- nextActions.length === 1
855
- ? nextActions
856
- : nextActions.filter((item) => item.name === error.action);
857
- const action =
858
- named.length === 1
859
- ? named[0]
860
- : captureOutput(() =>
861
- named.find((item) =>
862
- hasIncompleteSuccessor(item.expression, states[index], environment, item.scope),
863
- ),
864
- ).result;
923
+ const found = captureOutput(() => {
924
+ try {
925
+ return {
926
+ action: coverage
927
+ ? coverageActions.find((item) => hasCoveredIncompleteSuccessor(item, states[index]))
928
+ : nextActions.find((item) =>
929
+ hasIncompleteSuccessor(item.expression, states[index], environment, item.scope),
930
+ ),
931
+ failed: undefined,
932
+ };
933
+ } catch (failed) {
934
+ if (!(failed instanceof TlaEvaluationError)) throw failed;
935
+ return { action: undefined, failed };
936
+ }
937
+ });
938
+ const action = found.result.action;
939
+ const traceFailure = found.result.failed && getTraceFailure(found.result.failed, found.lines);
865
940
  const message = getIncompleteSuccessorMessage(
866
941
  error.variables,
867
942
  error.action,
868
943
  nextActions.length,
869
944
  options.version,
870
945
  );
871
- const undefinedValue = createModelValue("null");
872
- const partial = error.state.map((value) => value ?? undefinedValue);
873
- const failure = createFailureResult(error, traces.getStateTrace(index));
946
+ const live = captureOutput(() => addFailedLiveState(index, liveSuccessors));
947
+ const trace = traces.getStateTrace(index, { incomplete: error.state, isFailed: true }).trace;
948
+ const checked = checkFailedTrace(
949
+ appendTraceOutput(
950
+ {
951
+ trace: traceFailure
952
+ ? trace
953
+ : [
954
+ ...trace,
955
+ traces.getIncompleteStep(
956
+ error.state,
957
+ trace.length + 1,
958
+ {
959
+ action: error.action,
960
+ label: action && getActionLabel(action, options.version),
961
+ output: found.lines,
962
+ },
963
+ action,
964
+ ),
965
+ ],
966
+ },
967
+ live.lines,
968
+ ),
969
+ { isConsole: true },
970
+ );
971
+ const failure = createFailureResult(
972
+ error,
973
+ checked.behavior,
974
+ { callStack: live.result, traceFailure },
975
+ checked.postcondition,
976
+ );
874
977
  return withOutDegrees({
875
978
  ...failure,
876
979
  failure: failure.failure && {
@@ -879,14 +982,6 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
879
982
  message,
880
983
  toolMessage: message,
881
984
  exitCode: EXIT_STATUS.specEvaluationFailure,
882
- trace: [
883
- ...failure.failure.trace,
884
- {
885
- action: error.action,
886
- label: action && getActionLabel(action, options.version),
887
- state: toRecord(partial),
888
- },
889
- ],
890
985
  },
891
986
  });
892
987
  };
@@ -939,6 +1034,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
939
1034
  tracedSteps,
940
1035
  captureOutput,
941
1036
  getInitialStates: getSpecInitialStates,
1037
+ getUncoveredInitialStates,
942
1038
  findSuccessorAction,
943
1039
  });
944
1040
  const getPath = (index: number): Value[][] =>
@@ -965,7 +1061,8 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
965
1061
  const index = state === current.candidate ? -1 : states.indexOf(state);
966
1062
  return index === -1 ? [...getPath(current.index), state] : getPath(index);
967
1063
  };
968
- runtime.getCurrentState = () => (current.index === -1 ? undefined : states[current.index]);
1064
+ runtime.getCurrentState = () =>
1065
+ current.rebuilt?.state ?? (current.index === -1 ? undefined : states[current.index]);
969
1066
  runtime.setLivenessSource = (index) => {
970
1067
  current.livenessSource = index ?? -1;
971
1068
  };
@@ -1001,14 +1098,15 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1001
1098
  });
1002
1099
 
1003
1100
  const visitInitialState = (state: Value[]) => {
1004
- canonicalizeState(state);
1005
1101
  runtime.generatedStates++;
1006
1102
  runtime.initialStates++;
1007
1103
  current.candidate = state;
1008
1104
  current.candidateLevel = 1;
1009
- const fingerprintState = getFingerprintState(state);
1010
- const hash = getStateHash(fingerprintState);
1011
1105
  const isInModel = search.satisfiesConstraints(state);
1106
+ // TLC fingerprints only a state in the model, which sorts its records in place.
1107
+ if (isInModel) canonicalizeState(state);
1108
+ const fingerprintState = isInModel ? getFingerprintState(state) : state;
1109
+ const hash = isInModel ? getStateHash(fingerprintState) : 0;
1012
1110
  if (isInModel) {
1013
1111
  search.trackStatePossible(state);
1014
1112
  fingerprintModelState(fingerprintState);
@@ -1024,7 +1122,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1024
1122
  stateWriter?.writeInitialState(getGraphState(state, 1));
1025
1123
  }
1026
1124
  if (options.isContinued) {
1027
- search.getViolatedInvariants(state).forEach((invariant) =>
1125
+ search.forEachViolatedInvariant(state, (invariant) =>
1028
1126
  reportContinuedViolation("invariant-violation", {
1029
1127
  name: invariant.name,
1030
1128
  ...traces.toTrace([{ action: undefined, state }], []),
@@ -1033,17 +1131,19 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1033
1131
  }
1034
1132
  const violated = options.isContinued ? undefined : search.findViolatedInvariant(state);
1035
1133
  if (violated) {
1036
- return reportViolation("invariant-violation", {
1037
- name: violated.name,
1038
- ...traces.toTrace([{ action: undefined, state }], []),
1039
- });
1134
+ return reportViolation(
1135
+ "invariant-violation",
1136
+ { name: violated.name, ...traces.toTrace([{ action: undefined, state }], []) },
1137
+ getStepState,
1138
+ );
1040
1139
  }
1041
1140
  const failedPredicate = search.findFailedInitialPredicate(state);
1042
1141
  if (failedPredicate) {
1043
- return reportViolation("liveness-violation", {
1044
- name: failedPredicate.name,
1045
- ...traces.toTrace([{ action: undefined, state }], []),
1046
- });
1142
+ return reportViolation(
1143
+ "liveness-violation",
1144
+ { name: failedPredicate.name, ...traces.toTrace([{ action: undefined, state }], []) },
1145
+ getStepState,
1146
+ );
1047
1147
  }
1048
1148
  if (!isInModel) return undefined;
1049
1149
  markSeenState(seenStates, hash, states.length);
@@ -1053,18 +1153,17 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1053
1153
  // Looks a successor up, where the constraints keep it in the model, as the search does before it
1054
1154
  // checks the successor.
1055
1155
  const lookUpSuccessor = (state: Value[], successor: Successor) => {
1056
- const fingerprintState = getFingerprintState(successor.state);
1057
- const hash = getStateHash(fingerprintState);
1058
- const foundIndex = findStateIndex(seenStates, hash, fingerprintState);
1059
1156
  const isStateInModel = search.satisfiesConstraints(successor.state, state);
1060
1157
  if (isStateInModel) search.trackStatePossible(successor.state);
1061
1158
  const isInModel = isStateInModel && search.satisfiesActionConstraints(state, successor.state);
1159
+ const fingerprintState = isInModel ? getFingerprintState(successor.state) : successor.state;
1160
+ const hash = isInModel ? getStateHash(fingerprintState) : 0;
1062
1161
  if (isInModel) {
1063
1162
  search.trackActionPossible(state, successor.state);
1064
1163
  fingerprintModelState(fingerprintState);
1065
1164
  successor.state.forEach(markFingerprinted);
1066
1165
  }
1067
- const existing = isInModel ? foundIndex : -1;
1166
+ const existing = isInModel ? findStateIndex(seenStates, hash, fingerprintState) : -1;
1068
1167
  return {
1069
1168
  fingerprintState,
1070
1169
  hash,
@@ -1136,10 +1235,11 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1136
1235
  if (violation) reportContinuedViolation("invariant-violation", violation);
1137
1236
  } catch (error) {
1138
1237
  if (isStackOverflow(error)) throw error;
1238
+ const behavior = traces.getTrace(index, successor, { isFailed: true });
1139
1239
  const replay = captureOutput(() => replayNext(index));
1140
1240
  return createFailureResult(
1141
1241
  error,
1142
- appendTraceOutput(traces.getFailedTrace(index, successor), replay.lines),
1242
+ appendTraceOutput(behavior, replay.lines),
1143
1243
  replay.result ?? { outDegrees: getFailureOutDegrees() },
1144
1244
  );
1145
1245
  }
@@ -1208,7 +1308,6 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1208
1308
  kind: "liveness-started",
1209
1309
  branches: liveness.branches,
1210
1310
  isComplete: true,
1211
- // The tier stores and expands every successor, so the graph of each branch has every state.
1212
1311
  states: liveness.branches * typed.distinctStates,
1213
1312
  statistics,
1214
1313
  });
@@ -1273,14 +1372,29 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1273
1372
  }
1274
1373
  } catch (error) {
1275
1374
  if (error instanceof TlaIllegalStateError) {
1375
+ reportInitialStatesProgress(runtime.initialStates, options.onProgress);
1376
+ runtime.generatedStates++;
1377
+ runtime.initialStates++;
1378
+ const state = error.state.map((value) => value ?? createModelValue("null"));
1379
+ // Deviation from TLC: CounterExample holds a Java null for an unassigned variable and leaks a NullPointerException, where tla-ts binds the model value null.
1380
+ const checked = checkTracePostconditions(
1381
+ [{ action: undefined, label: undefined, state: toRecord(state) }],
1382
+ undefined,
1383
+ getStepState,
1384
+ );
1385
+ const postcondition = checked.unwitnessed ?? checked.postcondition;
1276
1386
  const illegal = createFailureResult(
1277
1387
  error,
1278
- { trace: [] },
1279
- { initialState: error.state.map((value) => value ?? createModelValue("null")) },
1388
+ { trace: [], traceOutput: checked.lines },
1389
+ { initialState: state },
1280
1390
  );
1281
1391
  return {
1282
1392
  ...illegal,
1283
- failure: illegal.failure && { ...illegal.failure, isWithoutStatistics: true },
1393
+ failure: illegal.failure && {
1394
+ ...illegal.failure,
1395
+ isWithoutStatistics: true,
1396
+ ...(postcondition && { postcondition }),
1397
+ },
1284
1398
  };
1285
1399
  }
1286
1400
  const replay = captureOutput(replayInit);
@@ -1366,6 +1480,7 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1366
1480
  );
1367
1481
  }, 0);
1368
1482
  };
1483
+ // Deviation from TLC: Worker#doNextCheckLiveness doesn't catch a FingerprintException from its rerun, so TLC prints the behavior without the failure.
1369
1484
  const getLiveCallStack = (index: number, liveSuccessors: LiveSuccessor[]) => {
1370
1485
  if (!liveness || is174) return undefined;
1371
1486
  const replay = getReplayFailure(environment, (tracing) =>
@@ -1389,23 +1504,87 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1389
1504
  return { result, lines: live.lines };
1390
1505
  }
1391
1506
  const replay = captureOutput(() => replayNext(index));
1392
- return { result: replay.result, lines: [...live.lines, ...replay.lines] };
1507
+ const result: FailureOptions =
1508
+ replay.result ?? (is174 ? {} : { outDegrees: getFailureOutDegrees() });
1509
+ return { result, lines: [...live.lines, ...replay.lines] };
1510
+ };
1511
+ const getNextFailureTrace = (index: number, counterExample?: CounterExampleOptions) =>
1512
+ checkFailedTrace(traces.getStateTrace(index, { isFailed: !counterExample }), counterExample);
1513
+ const reportNextStateOverflow = (index: number, counterExample?: CounterExampleOptions) => {
1514
+ const checked = getNextFailureTrace(index, counterExample);
1515
+ return createFailureResult(
1516
+ createStackOverflowError(),
1517
+ checked.behavior,
1518
+ { phase: "next" },
1519
+ checked.postcondition,
1520
+ );
1393
1521
  };
1394
- const reportNextStateOverflow = (index: number) =>
1395
- createFailureResult(createStackOverflowError(), traces.getStateTrace(index), { phase: "next" });
1522
+ // Deviation from TLC: under -continue, TLC's Worker reports a state whose next-state relation failed as deadlocked, or else reports the failure twice as its replay fails again.
1396
1523
  const reportNextStateFailure = (
1397
1524
  error: unknown,
1398
1525
  index: number,
1399
1526
  liveSuccessors: LiveSuccessor[],
1527
+ generated: number,
1400
1528
  ) => {
1401
- if (isStackOverflow(error)) return reportNextStateOverflow(index);
1529
+ // TLC's Worker goes on to find a state without successors deadlocked after it fails, which checks the postconditions.
1530
+ const counterExample =
1531
+ config.checkDeadlock && runtime.generatedStates === generated
1532
+ ? { isConsole: false }
1533
+ : undefined;
1534
+ if (isStackOverflow(error)) return reportNextStateOverflow(index, counterExample);
1535
+ const checked = getNextFailureTrace(index, counterExample);
1402
1536
  const replay = replayFailedNext(index, liveSuccessors);
1403
1537
  return createFailureResult(
1404
1538
  withNextStateExitCode(error),
1405
- appendTraceOutput(traces.getStateTrace(index), replay.lines),
1539
+ appendTraceOutput(checked.behavior, replay.lines),
1406
1540
  { phase: "next", ...replay.result },
1541
+ checked.postcondition,
1407
1542
  );
1408
1543
  };
1544
+ const addLiveState = (
1545
+ index: number,
1546
+ liveSuccessors: LiveSuccessor[],
1547
+ source: GraphState | undefined,
1548
+ ) => {
1549
+ if (liveness && source && (is174 || options.dump?.stuttering)) {
1550
+ stateWriter?.writeStuttering(source);
1551
+ }
1552
+ const accepted = liveness?.addState(index, states[index], liveSuccessors);
1553
+ if (!liveness || !accepted) return undefined;
1554
+ // Deviation from TLC: under -continue, TLC's LiveCheck throws an InvariantViolatedException after it reports the property, which its Worker reports as unexpected.
1555
+ const steps = traces.getPathSteps(accepted);
1556
+ const properties = getViolatedProperties(
1557
+ liveness,
1558
+ steps.map((step) => step.state),
1559
+ steps.length - 1,
1560
+ );
1561
+ return reportViolation("property-violation", {
1562
+ name: properties[0],
1563
+ properties,
1564
+ ...traces.toTrace(steps, accepted, { known: 0 }),
1565
+ });
1566
+ };
1567
+ const addReportedLiveState = (
1568
+ index: number,
1569
+ liveSuccessors: LiveSuccessor[],
1570
+ source: GraphState | undefined,
1571
+ ) =>
1572
+ captureOutput(() => {
1573
+ try {
1574
+ return {
1575
+ additional: addLiveState(index, liveSuccessors, source)?.violation,
1576
+ isFailed: false,
1577
+ callStack: undefined,
1578
+ };
1579
+ } catch (error) {
1580
+ if (!(error instanceof TlaEvaluationError)) throw error;
1581
+ return {
1582
+ additional: undefined,
1583
+ isFailed: true,
1584
+ callStack: getLiveCallStack(index, liveSuccessors),
1585
+ };
1586
+ }
1587
+ });
1409
1588
  const expandState = (index: number, liveSuccessors: LiveSuccessor[]) => {
1410
1589
  const state = states[index];
1411
1590
  const source = stateWriter && getGraphState(state, depths[index]);
@@ -1427,10 +1606,8 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1427
1606
  outcome = reportNextStateOverflow(index);
1428
1607
  return true;
1429
1608
  }
1430
- const replay = is174
1431
- ? captureOutput(() => replayNext(index))
1432
- : replayFailedNext(index, liveSuccessors);
1433
1609
  if (is174 && error instanceof TlaEvaluationError && !isHeadedError(error)) {
1610
+ const replay = captureOutput(() => replayNext(index));
1434
1611
  outcome = createFailureResult(
1435
1612
  new TlaEvaluationError(`${getWrappedException(error)}${error.message}`, error),
1436
1613
  appendTraceOutput({ trace: [] }, replay.lines),
@@ -1438,71 +1615,56 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1438
1615
  );
1439
1616
  return true;
1440
1617
  }
1441
- const isTraceFailed = isUnfingerprintable(successor.state);
1442
- const traceFailure =
1443
- isTraceFailed && error instanceof TlaEvaluationError
1444
- ? getFailureMessage(
1445
- isEvalException(error)
1446
- ? new TlaEvaluationError(`${UNEXPECTED_EVAL_EXCEPTION}${error.message}`)
1447
- : error,
1448
- true,
1449
- )
1618
+ const traceError =
1619
+ error instanceof TlaEvaluationError && isUnfingerprintable(successor.state)
1620
+ ? error
1450
1621
  : undefined;
1451
- outcome = createFailureResult(
1452
- error,
1453
- appendTraceOutput(
1454
- traceFailure ? traces.getStateTrace(index) : traces.getFailedTrace(index, successor),
1455
- replay.lines,
1456
- ),
1457
- { phase: "next", ...replay.result, traceFailure },
1458
- );
1622
+ const behavior = traceError
1623
+ ? traces.getStateTrace(index, { isFailed: true })
1624
+ : traces.getTrace(index, successor, { isFailed: true });
1625
+ const traceFailure =
1626
+ traceError &&
1627
+ getTraceFailure(
1628
+ traceError,
1629
+ captureOutput(() => {
1630
+ try {
1631
+ traces.findStepAction(index, state, successor.state, true);
1632
+ } catch (failed) {
1633
+ if (!(failed instanceof TlaEvaluationError)) throw failed;
1634
+ }
1635
+ }).lines,
1636
+ );
1637
+ const replay = is174
1638
+ ? captureOutput(() => replayNext(index))
1639
+ : replayFailedNext(index, liveSuccessors);
1640
+ outcome = createFailureResult(error, appendTraceOutput(behavior, replay.lines), {
1641
+ phase: "next",
1642
+ ...replay.result,
1643
+ traceFailure,
1644
+ });
1459
1645
  }
1460
1646
  return outcome !== undefined;
1461
1647
  });
1462
1648
  current.isExpanding = false;
1463
1649
  const result = outcome;
1464
1650
  if (result?.violation && !is174) {
1465
- const live = captureOutput(() => addFailedLiveState(index, liveSuccessors));
1651
+ const live = addReportedLiveState(index, liveSuccessors, source);
1466
1652
  return {
1467
1653
  ...result,
1468
1654
  violation: appendTraceOutput(result.violation, live.lines),
1469
1655
  // TLC adds the state to the behavior graph before it reports the coverage.
1470
1656
  coverage: getCoverage(),
1471
- ...(live.result && { liveCallStack: live.result }),
1657
+ ...(live.result.additional && { additionalViolation: live.result.additional }),
1658
+ ...(live.result.callStack && { liveCallStack: live.result.callStack }),
1472
1659
  };
1473
1660
  }
1474
1661
  if (result) return result;
1475
- const addLiveState = () => {
1476
- if (liveness && source && (is174 || options.dump?.stuttering)) {
1477
- stateWriter?.writeStuttering(source);
1478
- }
1479
- const accepted = liveness?.addState(index, state, liveSuccessors);
1480
- if (!liveness || !accepted) return undefined;
1481
- const steps = traces.getPathSteps(accepted);
1482
- const properties = getViolatedProperties(
1483
- liveness,
1484
- steps.map((step) => step.state),
1485
- steps.length - 1,
1486
- );
1487
- return reportViolation("property-violation", {
1488
- name: properties[0],
1489
- properties,
1490
- ...traces.toTrace(steps, accepted, { known: 0 }),
1491
- });
1492
- };
1493
1662
  if (successorCount === 0 && config.checkDeadlock) {
1494
1663
  const deadlock = reportViolation("deadlock", {
1495
1664
  name: "deadlock",
1496
1665
  ...traces.getStateTrace(index),
1497
1666
  });
1498
- const live = captureOutput(() => {
1499
- try {
1500
- return { additional: addLiveState()?.violation };
1501
- } catch (error) {
1502
- if (!(error instanceof TlaEvaluationError)) throw error;
1503
- return { isFailed: true, callStack: getLiveCallStack(index, liveSuccessors) };
1504
- }
1505
- });
1667
+ const live = addReportedLiveState(index, liveSuccessors, source);
1506
1668
  const reported = deadlock.violation
1507
1669
  ? { ...deadlock, violation: appendTraceOutput(deadlock.violation, live.lines) }
1508
1670
  : deadlock;
@@ -1515,14 +1677,11 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1515
1677
  return live.result.callStack ? { ...counted, liveCallStack: live.result.callStack } : counted;
1516
1678
  }
1517
1679
  try {
1518
- return addLiveState();
1680
+ return addLiveState(index, liveSuccessors, source);
1519
1681
  } catch (error) {
1520
1682
  if (is174 && error instanceof TlaEvaluationError) {
1521
- const unexpected = new TlaEvaluationError(
1522
- `${isEvalException(error) ? UNEXPECTED_EVAL_EXCEPTION : UNEXPECTED_EXCEPTION}${error.message}`,
1523
- );
1524
1683
  return createFailureResult(
1525
- setExitCode(unexpected, EXIT_STATUS.error),
1684
+ setExitCode(toUnexpectedError(error), EXIT_STATUS.error),
1526
1685
  { trace: [] },
1527
1686
  { outDegrees: getFailureOutDegrees() },
1528
1687
  );
@@ -1530,7 +1689,9 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1530
1689
  const replay = getReplayFailure(environment, (tracing) =>
1531
1690
  liveness?.addState(index, state, liveSuccessors, tracing),
1532
1691
  );
1533
- return createFailureResult(error, traces.getStateTrace(index), {
1692
+ const behavior = traces.getStateTrace(index, { isFailed: true });
1693
+ const replayed = captureOutput(() => replayNext(index));
1694
+ return createFailureResult(error, appendTraceOutput(behavior, replayed.lines), {
1534
1695
  outDegrees: getFailureOutDegrees(),
1535
1696
  callStack: replay?.callStack.length ? replay.callStack : undefined,
1536
1697
  phase: "liveness",
@@ -1554,10 +1715,20 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1554
1715
  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}`,
1555
1716
  };
1556
1717
  };
1718
+ const toLivenessFailure = (error: TlaEvaluationError) =>
1719
+ setExitCode(toUnexpectedError(error), EXIT_STATUS.abort);
1720
+ const getLassoSteps = (path: number[]) => {
1721
+ try {
1722
+ return traces.getPathSteps(path);
1723
+ } catch (error) {
1724
+ if (!(error instanceof TlaEvaluationError)) throw error;
1725
+ throw toLivenessFailure(error);
1726
+ }
1727
+ };
1557
1728
  const reportLasso = (checker: LivenessChecker, lasso: Lasso) => {
1558
1729
  const path = [...lasso.prefix, ...lasso.cycle];
1559
1730
  const loopStart = lasso.prefix.length;
1560
- const steps = traces.getPathSteps(path, loopStart);
1731
+ const steps = getLassoSteps(path);
1561
1732
  const trace = traces.toTrace(steps, path, { loopStart, known: 0 }).trace;
1562
1733
  const properties = getViolatedProperties(
1563
1734
  checker,
@@ -1567,13 +1738,28 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1567
1738
  const cycleStart = path[loopStart];
1568
1739
  const cycleEnd = path[path.length - 1];
1569
1740
  const lastState = steps[steps.length - 1].state;
1570
- const loop =
1571
- cycleStart === cycleEnd
1572
- ? { result: undefined, lines: [] }
1573
- : captureOutput(() =>
1574
- traces.findStepAction(cycleEnd, lastState, states[cycleStart], false),
1575
- );
1576
- const isStutteringWarned = cycleStart === cycleEnd && lasso.isCycleConstrained;
1741
+ const isStuttering = cycleStart === cycleEnd;
1742
+ const loop = isStuttering
1743
+ ? { result: undefined, lines: [] }
1744
+ : captureOutput(() => traces.findStepAction(cycleEnd, lastState, states[cycleStart], false));
1745
+ const back =
1746
+ isStuttering || loop.result
1747
+ ? undefined
1748
+ : traces.findSuccessorWithFingerprint(lastState, cycleEnd, cycleStart);
1749
+ if (!isStuttering && !loop.result && !back) {
1750
+ options.onProgress?.({
1751
+ kind: "violation",
1752
+ status: "property-violation",
1753
+ violation: {
1754
+ name: properties[0],
1755
+ properties,
1756
+ trace,
1757
+ traceOutput: loop.lines,
1758
+ loopStart: undefined,
1759
+ },
1760
+ });
1761
+ throw toLivenessFailure(new TlaEvaluationError(NEXT_RECOVERY_FAILURE));
1762
+ }
1577
1763
  return reportViolation(
1578
1764
  "liveness-violation",
1579
1765
  {
@@ -1583,28 +1769,25 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1583
1769
  traceOutput: loop.lines,
1584
1770
  loopStart,
1585
1771
  loopLabel: loop.result && getActionLabel(loop.result, options.version),
1586
- warning: isStutteringWarned ? getStutteringWarning() : undefined,
1772
+ warning: isStuttering && lasso.isCycleConstrained ? getStutteringWarning() : undefined,
1587
1773
  },
1774
+ getStepState,
1588
1775
  () =>
1589
- cycleStart === cycleEnd
1776
+ isStuttering
1590
1777
  ? getStepAction(trace[trace.length - 1])
1591
1778
  : loop.result
1592
1779
  ? toActionValue(loop.result)
1593
- : createActionRecord(
1594
- traces.findSuccessorWithFingerprint(lastState, cycleStart)?.action,
1595
- ),
1780
+ : createActionRecord(back?.action),
1596
1781
  );
1597
1782
  };
1598
1783
  const isPeriodic = liveness !== undefined && (options.livenessCheck ?? "default") === "default";
1599
1784
  const startedAt = performance.now();
1600
1785
  const periodic = {
1601
1786
  at: startedAt + PERIODIC_START,
1602
- // The size of each branch's graph when TLC last checked it, which starts at 1.
1603
- sizes: liveness?.getStateCounts().map(() => 1) ?? [],
1787
+ checkedCounts: liveness?.getStateCounts().map(() => 1) ?? [],
1604
1788
  time: 0,
1605
1789
  coverageCount: Math.trunc(options.coverage ?? 0),
1606
1790
  };
1607
- // TLC counts the states of the graphs of all branches, which can differ.
1608
1791
  const getGraphStates = (checker: LivenessChecker) =>
1609
1792
  checker.getStateCounts().reduce((sum, count) => sum + count, 0);
1610
1793
  const findPeriodicViolation = (checker: LivenessChecker) => {
@@ -1612,10 +1795,10 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1612
1795
  const counts = checker.getStateCounts();
1613
1796
  const isGrown = counts.some(
1614
1797
  (count, branch) =>
1615
- (count - periodic.sizes[branch]) / periodic.sizes[branch] > LIVENESS_GROWTH,
1798
+ (count - periodic.checkedCounts[branch]) / periodic.checkedCounts[branch] > LIVENESS_GROWTH,
1616
1799
  );
1617
1800
  if (!isGrown || periodic.time > (now - startedAt) * LIVENESS_RATIO) return undefined;
1618
- periodic.sizes = counts;
1801
+ periodic.checkedCounts = counts;
1619
1802
  options.onProgress?.({
1620
1803
  kind: "liveness-started",
1621
1804
  branches: checker.branches,
@@ -1736,13 +1919,14 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1736
1919
  runtime.queueSize = states.length - index - 1;
1737
1920
  current.isExpanding = true;
1738
1921
  const liveSuccessors: LiveSuccessor[] = [];
1922
+ const generated = runtime.generatedStates;
1739
1923
  try {
1740
1924
  outcome = expandState(index, liveSuccessors);
1741
1925
  } catch (error) {
1742
1926
  outcome =
1743
1927
  error instanceof TlaIncompleteSuccessorError
1744
- ? reportIncompleteSuccessor(index, error)
1745
- : reportNextStateFailure(error, index, liveSuccessors);
1928
+ ? reportIncompleteSuccessor(index, error, liveSuccessors)
1929
+ : reportNextStateFailure(error, index, liveSuccessors, generated);
1746
1930
  }
1747
1931
  if (outcome) return outcome;
1748
1932
  outDegrees[getOutDegreeBucket()]++;
@@ -1771,7 +1955,6 @@ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefine
1771
1955
  kind: "liveness-started",
1772
1956
  branches: finalLiveness.branches,
1773
1957
  isComplete: true,
1774
- // The tier that found the lasso stored and expanded every successor, so each graph has every state.
1775
1958
  states: typedLasso
1776
1959
  ? finalLiveness.branches * runtime.distinctStates
1777
1960
  : getGraphStates(finalLiveness),