tla-ts 0.0.3-dev.3c70a97 → 0.0.3-dev.3ce2f00

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 (50) hide show
  1. package/README.md +5 -1
  2. package/dist/index.cjs +1 -1
  3. package/dist/index.d.cts +53 -22
  4. package/dist/index.d.ts +53 -22
  5. package/dist/index.js +1 -1
  6. package/dist/parallel-worker.cjs +1 -1
  7. package/dist/parallel-worker.js +2 -2
  8. package/dist/parallel.cjs +1 -1
  9. package/dist/parallel.js +2 -2
  10. package/dist/run-cli.cjs +19 -17
  11. package/dist/run-cli.js +20 -18
  12. package/dist/transfer.cjs +33 -17
  13. package/dist/transfer.js +33 -17
  14. package/package.json +1 -1
  15. package/src/ast.ts +3 -0
  16. package/src/builtins.ts +9 -3
  17. package/src/check.ts +455 -93
  18. package/src/cli-options.ts +11 -5
  19. package/src/codegen.ts +6 -3
  20. package/src/config.ts +224 -170
  21. package/src/environment.ts +115 -19
  22. package/src/errors.ts +78 -1
  23. package/src/evaluate.ts +182 -26
  24. package/src/exit-codes.ts +22 -3
  25. package/src/fingerprints.ts +2 -1
  26. package/src/instances.ts +4 -4
  27. package/src/levels.ts +82 -36
  28. package/src/live-check.ts +132 -70
  29. package/src/liveness.ts +117 -22
  30. package/src/parallel-worker.ts +2 -2
  31. package/src/parallel.ts +5 -2
  32. package/src/parser.ts +57 -16
  33. package/src/resolve.ts +106 -29
  34. package/src/run-cli.ts +103 -18
  35. package/src/runtime.ts +2 -0
  36. package/src/spec-description.ts +12 -6
  37. package/src/standard-modules.ts +54 -45
  38. package/src/state-graph.ts +157 -0
  39. package/src/substitution-levels.ts +35 -11
  40. package/src/successors.ts +169 -33
  41. package/src/symmetry.ts +16 -6
  42. package/src/tableau.ts +51 -24
  43. package/src/tlc-ext.ts +27 -2
  44. package/src/tlc-module.ts +14 -33
  45. package/src/tlc-modules.ts +4 -0
  46. package/src/tlc-order.ts +8 -4
  47. package/src/tlc-random.ts +9 -6
  48. package/src/tlc-summary.ts +20 -5
  49. package/src/value-sources.ts +29 -0
  50. package/src/values.ts +242 -61
package/src/check.ts CHANGED
@@ -11,8 +11,13 @@ import { getReadVariables } from "./dependencies";
11
11
  import { createEnvironment } from "./environment";
12
12
  import {
13
13
  getErrorExitCode,
14
+ isStackOverflow,
15
+ formatRange,
16
+ STACK_OVERFLOW,
14
17
  TlaConfigError,
15
18
  TlaEvaluationError,
19
+ TlaIllegalStateError,
20
+ TlaIncompleteStateError,
16
21
  TlaModuleError,
17
22
  UNEXPECTED_EXCEPTION,
18
23
  } from "./errors";
@@ -21,7 +26,6 @@ import {
21
26
  describeRange,
22
27
  evaluate,
23
28
  expectBoolean,
24
- formatRange,
25
29
  getDefinition,
26
30
  isConstantLevel,
27
31
  isImpure,
@@ -32,6 +36,7 @@ import {
32
36
  import {
33
37
  createLivenessChecker,
34
38
  type Lasso,
39
+ type LiveGraph,
35
40
  type LivenessChecker,
36
41
  type LiveSuccessor,
37
42
  } from "./live-check";
@@ -47,19 +52,30 @@ import {
47
52
  } from "./successors";
48
53
  import {
49
54
  getActionLabel,
55
+ getActionSignature,
50
56
  getNextActions,
51
57
  getSpecificationValue,
52
58
  type SpecAction,
53
59
  } from "./spec-description";
60
+ import {
61
+ createStateWriter,
62
+ type GraphState,
63
+ type StateDump,
64
+ type StateWriter,
65
+ } from "./state-graph";
54
66
  import { getRepresentative, getSymmetryGroup } from "./symmetry";
55
- import { getTlcStringOrder, getTlcVariableOrder } from "./tlc-order";
67
+ import { getTlcStringOrder, getTlcDeclarationOrder } from "./tlc-order";
56
68
  import { createTlcFingerprinter, getTlcFingerprint } from "./tlc-random";
57
69
  import { createCanonicalizer } from "./canonical";
58
70
  import { getPossibleCounts, type CheckerRuntime } from "./runtime";
59
71
  import { createCounterExample } from "./tlc-ext";
72
+ import { getFingerprintTrace, valueSources } from "./value-sources";
60
73
  import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
61
74
  import {
75
+ assertFingerprintable,
62
76
  combineHash,
77
+ createModelValue,
78
+ formatState,
63
79
  formatValue,
64
80
  getValueHash,
65
81
  isFunctionValue,
@@ -83,6 +99,8 @@ export interface CheckOptions {
83
99
  workers?: number;
84
100
  // TLC's -lncheck: "final" checks liveness only on the complete state graph.
85
101
  livenessCheck?: "default" | "final";
102
+ // TLC's -dump: writes the reachable states, or with `dot` the state graph, through writeFile.
103
+ dump?: StateDump;
86
104
  }
87
105
 
88
106
  export type CheckStatus =
@@ -126,6 +144,25 @@ export interface EvaluationFailure {
126
144
  trace: TraceStep[];
127
145
  initialState: Record<string, Value> | undefined;
128
146
  exitCode: number;
147
+ replay?: Replay;
148
+ isWithoutStatistics?: boolean;
149
+ }
150
+
151
+ // TLC replays a failed step to print the expressions it was evaluating when the replay failed.
152
+ export interface Replay {
153
+ output: string[];
154
+ callStack: string[] | undefined;
155
+ fingerprint?: Fingerprint;
156
+ // The worker that fails to check liveness replays the step at once, so TLC prints the stack
157
+ // before the error, and only when it holds a frame.
158
+ isBeforeError?: boolean;
159
+ }
160
+
161
+ // TLC reports an error from a method of a value that has a source as one it was unable to
162
+ // fingerprint. With an empty stack, it lists the sources of the values instead.
163
+ export interface Fingerprint {
164
+ sources: string[];
165
+ reason: string;
129
166
  }
130
167
 
131
168
  export interface CheckResult {
@@ -237,7 +274,7 @@ const getConstantValue = (name: string, definition: Definition, environment: Env
237
274
  try {
238
275
  return evaluate(createIdentifier(name), getContext(environment, undefined, undefined));
239
276
  } catch (error) {
240
- if (error instanceof TlaEvaluationError) return undefined;
277
+ if (error instanceof TlaEvaluationError || isStackOverflow(error)) return undefined;
241
278
  throw error;
242
279
  }
243
280
  };
@@ -266,8 +303,11 @@ const getInvariants = (names: string[], environment: Environment) => {
266
303
  levels.params.size === 0
267
304
  ? ""
268
305
  : "\nNote that a bug can cause TLC to incorrectly report this error.\nIf you believe your TLA+ or PlusCal specification to be correct,\nplease check if this bug described in LevelNode.java starting at line 590ff affects you.";
269
- throw new TlaConfigError(
270
- `The invariant ${name} is not a state predicate (one with no primes or temporal operators).${note}`,
306
+ throw setExitCode(
307
+ new TlaConfigError(
308
+ `The invariant ${name} is not a state predicate (one with no primes or temporal operators).${note}`,
309
+ ),
310
+ EXIT_STATUS.safetyEvaluationFailure,
271
311
  );
272
312
  }
273
313
  return [{ name, expression: createIdentifier(name), scope: undefined }];
@@ -365,8 +405,11 @@ const getSpecification = (config: ModelConfig, environment: Environment): Specif
365
405
  conjunct.kind === "Always" &&
366
406
  getLevel(conjunct.expression, item.scope, environment) < ACTION_LEVEL
367
407
  ) {
368
- throw new TlaConfigError(
369
- `TLC cannot handle []P in the specification ${config.specification} when P is a state predicate`,
408
+ throw setExitCode(
409
+ new TlaConfigError(
410
+ `TLC cannot handle the temporal formula ${describeRange(conjunct.expression)}`,
411
+ ),
412
+ EXIT_STATUS.error,
370
413
  );
371
414
  }
372
415
  const level = getLevel(conjunct, item.scope, environment);
@@ -436,12 +479,12 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
436
479
  config.properties.forEach((name) => {
437
480
  const definition = getConfigPredicate("property", name, environment);
438
481
  if (!definition) return;
439
- expandTemporal(definition.body, undefined, environment, true).forEach((item) => {
482
+ expandTemporal(definition.body, undefined, environment, true, name).forEach((item) => {
440
483
  const conjunct = item.expression;
441
484
  if (isConstantTrue(item, environment, "property")) return;
442
485
  if (conjunct.kind === "Always" && conjunct.expression.kind === "BoxAction") {
443
486
  properties.actionProperties.push({
444
- name,
487
+ name: item.name,
445
488
  box: conjunct.expression,
446
489
  action: conjunct.expression.action,
447
490
  subscript: conjunct.expression.subscript,
@@ -453,12 +496,22 @@ const getProperties = (config: ModelConfig, environment: Environment): Propertie
453
496
  conjunct.kind === "Always" &&
454
497
  getLevel(conjunct.expression, item.scope, environment) <= 1
455
498
  ) {
456
- properties.invariants.push({ name, expression: conjunct.expression, scope: item.scope });
499
+ // TLC names the invariant []P after P when P is an operator name.
500
+ const expression = conjunct.expression;
501
+ properties.invariants.push({
502
+ name: expression.kind === "Identifier" ? expression.name : item.name,
503
+ expression,
504
+ scope: item.scope,
505
+ });
457
506
  return;
458
507
  }
459
508
  const level = getLevel(conjunct, item.scope, environment);
460
509
  if (level < ACTION_LEVEL) {
461
- properties.initialPredicates.push({ name, expression: conjunct, scope: item.scope });
510
+ properties.initialPredicates.push({
511
+ name: item.name,
512
+ expression: conjunct,
513
+ scope: item.scope,
514
+ });
462
515
  return;
463
516
  }
464
517
  if (level === ACTION_LEVEL) {
@@ -518,7 +571,9 @@ const getMembership = (
518
571
  const getStateChecks = (named: NamedExpression[], environment: Environment): StateCheck[] =>
519
572
  named.flatMap((item) =>
520
573
  getConjuncts(item.expression, environment).map((expression) => {
521
- const variables = getReadVariables(expression, environment);
574
+ // A LET or an argument in the scope can read variables the expression doesn't name.
575
+ const variables =
576
+ item.scope === undefined ? getReadVariables(expression, environment) : undefined;
522
577
  return {
523
578
  name: item.name,
524
579
  expression,
@@ -765,11 +820,13 @@ export const createSearch = (
765
820
  if (view !== undefined || isFingerprintedLikeTlc) {
766
821
  return BigInt64Array.from(states, getTlcStateFingerprint);
767
822
  }
768
- const indices = getTlcOrder();
769
- const getFingerprint = createTlcFingerprinter();
770
- return BigInt64Array.from(fingerprintStates, (state) =>
771
- getFingerprint(indices.map((index) => state[index])),
823
+ const fingerprints = new BigInt64Array(fingerprintStates.length);
824
+ const fingerprint = createTlcFingerprinter(
825
+ getTlcOrder(),
826
+ new Int32Array(fingerprints.buffer),
772
827
  );
828
+ fingerprintStates.forEach(fingerprint);
829
+ return fingerprints;
773
830
  },
774
831
  getFingerprintState: (state) => {
775
832
  if (isFingerprintedLikeTlc) return [String(getTlcStateFingerprint(state))];
@@ -801,11 +858,17 @@ export const createSearch = (
801
858
  }
802
859
  }),
803
860
  satisfiesConstraints: (state, parent) =>
804
- constraints.every(
805
- (constraint) =>
806
- isUnchangedFor(constraint, state, parent) ||
807
- holdsLikeTlc(constraint.expression, getContext(environment, state, undefined)),
808
- ),
861
+ constraints.every((constraint) => {
862
+ try {
863
+ return (
864
+ isUnchangedFor(constraint, state, parent) ||
865
+ holdsLikeTlc(constraint.expression, getContext(environment, state, undefined))
866
+ );
867
+ } catch (error) {
868
+ // TLC reports a failing constraint on a successor like a failure of the next-state relation.
869
+ throw parent ? toNextStateError(error) : error;
870
+ }
871
+ }),
809
872
  findFailedInitialPredicate: (state) =>
810
873
  properties.initialPredicates.find(
811
874
  (predicate) =>
@@ -838,13 +901,13 @@ export const createSearch = (
838
901
  }
839
902
  }),
840
903
  satisfiesActionConstraints: (state, next) =>
841
- actionConstraints.every((constraint) =>
842
- holdsLikeTlc(
843
- constraint.expression,
844
- getContext(environment, state, next),
845
- (expression) => `${UNEXPECTED_EXCEPTION}${getExpectedBoolean(expression, environment)}`,
846
- ),
847
- ),
904
+ actionConstraints.every((constraint) => {
905
+ try {
906
+ return holdsLikeTlc(constraint.expression, getContext(environment, state, next));
907
+ } catch (error) {
908
+ throw toNextStateError(error);
909
+ }
910
+ }),
848
911
  trackStatePossible:
849
912
  statePossible.length === 0
850
913
  ? ignorePossible
@@ -875,6 +938,59 @@ export const createSearch = (
875
938
  const withNextStateExitCode = (error: unknown) =>
876
939
  error instanceof TlaModuleError ? setExitCode(error, error.exitCode) : error;
877
940
 
941
+ // TLC reports a failure of the next-state relation, or of the liveness check of a step, as an
942
+ // unexpected exception, unless a standard module raised it as an EvalException.
943
+ const toNextStateError = (error: unknown) =>
944
+ error instanceof TlaEvaluationError &&
945
+ !(error instanceof TlaModuleError) &&
946
+ !(error instanceof TlaIncompleteStateError)
947
+ ? setExitCode(
948
+ new TlaEvaluationError(`${UNEXPECTED_EXCEPTION}${error.message}`, error),
949
+ getErrorExitCode(error),
950
+ )
951
+ : withNextStateExitCode(error);
952
+
953
+ const isRepeated = (frame: string, index: number, frames: string[]) =>
954
+ index > 0 && frames[index - 1] === frame;
955
+
956
+ // FingerprintException#getTrace numbers the sources from the outermost value and lists the innermost
957
+ // first.
958
+ const getSourceTrace = (sources: Expression[]) =>
959
+ sources
960
+ .flatMap((source) => (source.location.range ? [formatRange(source.location.range)] : []))
961
+ .filter((source, index, all) => !isRepeated(source, index, all))
962
+ .map((source, index) => `${index}) ${source}`)
963
+ .reverse();
964
+
965
+ // TLC keeps the stack only when an evaluation fails, and prints each expression on it once, however
966
+ // many times in a row it pushed it.
967
+ const getReplayedCallStack = (
968
+ replay: () => void,
969
+ trace: Expression[],
970
+ ): Pick<Replay, "callStack" | "fingerprint"> => {
971
+ try {
972
+ replay();
973
+ return { callStack: undefined };
974
+ } catch (error) {
975
+ if (!(error instanceof TlaEvaluationError)) return { callStack: [] };
976
+ const callStack = trace
977
+ .flatMap((expression) => {
978
+ const range = expression.location.range;
979
+ return range
980
+ ? [
981
+ `Line ${range.start.line}, column ${range.start.column} to line ${range.end.line}, column ${range.end.column} in ${range.module}`,
982
+ ]
983
+ : [];
984
+ })
985
+ .filter((frame, index, frames) => !isRepeated(frame, index, frames));
986
+ const sources = getFingerprintTrace(error);
987
+ return {
988
+ callStack,
989
+ fingerprint: sources && { sources: getSourceTrace(sources), reason: error.message },
990
+ };
991
+ }
992
+ };
993
+
878
994
  const withHeadline = (error: unknown, headline: string) =>
879
995
  error instanceof TlaEvaluationError
880
996
  ? setExitCode(
@@ -883,14 +999,30 @@ const withHeadline = (error: unknown, headline: string) =>
883
999
  )
884
1000
  : error;
885
1001
 
1002
+ // TLC finishes the -dump file whether or not the search finds a violation.
886
1003
  export const checkSequentially = (options: CheckOptions): CheckResult => {
1004
+ const stateWriter = options.dump && createStateWriter(options.dump, options.writeFile);
1005
+ try {
1006
+ return searchStates(options, stateWriter);
1007
+ } catch (error) {
1008
+ // TLC reports a stack overflow outside the search as a failure of its own, with exit code 255.
1009
+ throw isStackOverflow(error)
1010
+ ? setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error)
1011
+ : error;
1012
+ } finally {
1013
+ stateWriter?.close();
1014
+ }
1015
+ };
1016
+
1017
+ const searchStates = (options: CheckOptions, stateWriter: StateWriter | undefined): CheckResult => {
887
1018
  const config = typeof options.config === "string" ? parseConfig(options.config) : options.config;
888
1019
  const onPrint = options.onPrint ?? console.log;
889
1020
  // TLC prints again while it rebuilds a trace, between the states it prints. Printing those lines
890
1021
  // before the trace would misplace them, so rebuilding a trace prints nothing.
891
- const output = { isMuted: false };
1022
+ const output = { isMuted: false, replayed: undefined as string[] | undefined };
892
1023
  const print = (text: string) => {
893
- if (!output.isMuted) onPrint(text);
1024
+ if (output.replayed) output.replayed.push(text);
1025
+ else if (!output.isMuted) onPrint(text);
894
1026
  };
895
1027
  const withoutOutput = <Result>(rebuild: () => Result) => {
896
1028
  output.isMuted = true;
@@ -952,9 +1084,9 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
952
1084
  ),
953
1085
  });
954
1086
 
955
- const search = createSearch(config, environment, () =>
956
- getTlcVariableOrder(options.source, options.loadModule),
957
- );
1087
+ const getVariableOrder = () =>
1088
+ getTlcDeclarationOrder(options.source, options.loadModule, "variable");
1089
+ const search = createSearch(config, environment, getVariableOrder);
958
1090
  const assumptionViolation = findAssumptionViolation(specification.assumptions, environment);
959
1091
  if (assumptionViolation) return createResult("assumption-violation", assumptionViolation);
960
1092
  if (!search) return createSuccess();
@@ -978,37 +1110,114 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
978
1110
  const spec = search.spec;
979
1111
  const getFingerprintState = search.getFingerprintState;
980
1112
  const nextActions = { value: undefined as SpecAction[] | undefined };
1113
+ const replayFailure = (
1114
+ result: CheckResult,
1115
+ replay: (tracedSearch: Search, traced: Environment) => void,
1116
+ isBeforeError = false,
1117
+ ): CheckResult => {
1118
+ if (!result.failure) return result;
1119
+ const trace: Expression[] = [];
1120
+ const traced: Environment = { ...environment, evaluators: new WeakMap(), trace };
1121
+ const tracedSearch = createSearch(config, traced, getVariableOrder);
1122
+ if (!tracedSearch) return result;
1123
+ const replayed: string[] = [];
1124
+ output.replayed = replayed;
1125
+ valueSources.current = new WeakMap();
1126
+ const replayedStack = getReplayedCallStack(() => replay(tracedSearch, traced), trace);
1127
+ valueSources.current = undefined;
1128
+ output.replayed = undefined;
1129
+ return {
1130
+ ...result,
1131
+ failure: { ...result.failure, replay: { output: replayed, ...replayedStack, isBeforeError } },
1132
+ };
1133
+ };
1134
+ // TLC fingerprints a state outside CallStackTool, so a failure of a value with a source leaves
1135
+ // the stack empty. Any other failure keeps the stack of the initial predicate.
1136
+ const fingerprintReplayed = (state: Value[], traced: Environment) => {
1137
+ try {
1138
+ getStateHash(getFingerprintState(state));
1139
+ } catch (error) {
1140
+ if (traced.trace && getFingerprintTrace(error)) traced.trace.length = 0;
1141
+ throw error;
1142
+ }
1143
+ };
1144
+ // TLC checks every initial state again, as when it first generated them.
1145
+ const replayInitialStates = (result: CheckResult) =>
1146
+ replayFailure(result, (tracedSearch, traced) => {
1147
+ if (spec.init === undefined) return;
1148
+ forEachInitialState(
1149
+ spec.init,
1150
+ traced,
1151
+ (state) => {
1152
+ if (tracedSearch.satisfiesConstraints(state)) fingerprintReplayed(state, traced);
1153
+ return (
1154
+ tracedSearch.findViolatedInvariant(state) !== undefined ||
1155
+ tracedSearch.findFailedInitialPredicate(state) !== undefined
1156
+ );
1157
+ },
1158
+ spec.scope,
1159
+ );
1160
+ });
1161
+ // TLC rebuilds the behavior up to the state it expanded before it replays the step.
1162
+ const rebuildBehavior = (index: number): void => {
1163
+ const parent = parents[index];
1164
+ if (parent === -1) {
1165
+ getSpecInitialStates();
1166
+ return;
1167
+ }
1168
+ rebuildBehavior(parent);
1169
+ getLabel(parent, states[parent], states[index]);
1170
+ };
1171
+ // TLC generates the successors of each action before it checks them. It checks the invariants
1172
+ // only of a successor outside the model, since it has seen the others.
1173
+ const replaySuccessors = (result: CheckResult, index: number) =>
1174
+ replayFailure(result, (tracedSearch, traced) => {
1175
+ rebuildBehavior(index);
1176
+ const state = states[index];
1177
+ nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
1178
+ nextActions.value.find((action) =>
1179
+ getSuccessors(action.expression, state, traced, action.scope).some((successor) => {
1180
+ const isInModel =
1181
+ tracedSearch.satisfiesConstraints(successor.state) &&
1182
+ tracedSearch.satisfiesActionConstraints(state, successor.state);
1183
+ if (isInModel) fingerprintReplayed(successor.state, traced);
1184
+ return (
1185
+ (!isInModel && tracedSearch.findViolatedInvariant(successor.state) !== undefined) ||
1186
+ tracedSearch.findViolatedActionProperty(state, successor.state) !== undefined
1187
+ );
1188
+ }),
1189
+ );
1190
+ });
981
1191
  const isReducing = config.view !== undefined || config.symmetry !== undefined;
982
1192
  const tlcFingerprints: number[] = [];
1193
+ const liveGraph: LiveGraph = {
1194
+ getFingerprint: (index) =>
1195
+ (tlcFingerprints[index] ??= Number(BigInt.asIntN(32, runtime.getFingerprint(states[index])))),
1196
+ getSuccessors: (state) => {
1197
+ const successors: LiveSuccessor[] = [];
1198
+ if (spec.next === undefined) return successors;
1199
+ forEachSuccessor(
1200
+ spec.next,
1201
+ state,
1202
+ environment,
1203
+ (successor) => {
1204
+ canonicalizeState(successor.state);
1205
+ const isInModel =
1206
+ search.satisfiesConstraints(successor.state, state) &&
1207
+ search.satisfiesActionConstraints(state, successor.state);
1208
+ if (!isInModel) return false;
1209
+ const fingerprintState = getFingerprintState(successor.state);
1210
+ const index = findStateIndex(getStateHash(fingerprintState), fingerprintState);
1211
+ successors.push({ index, state: successor.state });
1212
+ return false;
1213
+ },
1214
+ spec.scope,
1215
+ );
1216
+ return successors;
1217
+ },
1218
+ };
983
1219
  const liveness = withLivenessExitCode(() =>
984
- createLivenessChecker(search.properties.liveness, spec.temporals, environment, {
985
- getFingerprint: (index) =>
986
- (tlcFingerprints[index] ??= Number(
987
- BigInt.asIntN(32, runtime.getFingerprint(states[index])),
988
- )),
989
- getSuccessors: (state) => {
990
- const successors: LiveSuccessor[] = [];
991
- if (spec.next === undefined) return successors;
992
- forEachSuccessor(
993
- spec.next,
994
- state,
995
- environment,
996
- (successor) => {
997
- canonicalizeState(successor.state);
998
- const isInModel =
999
- search.satisfiesConstraints(successor.state, state) &&
1000
- search.satisfiesActionConstraints(state, successor.state);
1001
- if (!isInModel) return false;
1002
- const fingerprintState = getFingerprintState(successor.state);
1003
- const index = findStateIndex(getStateHash(fingerprintState), fingerprintState);
1004
- successors.push({ index, state: successor.state });
1005
- return false;
1006
- },
1007
- spec.scope,
1008
- );
1009
- return successors;
1010
- },
1011
- }),
1220
+ createLivenessChecker(search.properties.liveness, spec.temporals, environment, liveGraph),
1012
1221
  );
1013
1222
  const isCanonicalizedFirst = search.isSymmetric || liveness !== undefined;
1014
1223
 
@@ -1017,7 +1226,9 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1017
1226
  const parents: number[] = [];
1018
1227
  const actions: (string | undefined)[] = [];
1019
1228
  const depths: number[] = [];
1020
- const buckets = new Map<number, number | number[]>();
1229
+ // A Map holds at most 2^24 entries, so an open-addressing table keeps each seen state's hash
1230
+ // and its index plus one side by side, probed by the hash.
1231
+ const seen = { slots: new Int32Array(1 << 17), size: 0, shift: 16 };
1021
1232
  const canonicalizeState = createCanonicalizer();
1022
1233
 
1023
1234
  const toRecord = (state: Value[]): Record<string, Value> =>
@@ -1030,11 +1241,16 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1030
1241
  position: number,
1031
1242
  loopStart: number | undefined,
1032
1243
  ) => {
1033
- const state = steps[position].state;
1034
- const next = (steps[position + 1] ?? steps[loopStart ?? position]).state;
1035
1244
  // TLC evaluates TLCGet("level") and Trace in an alias at the state's position in the trace.
1036
1245
  runtime.getLevel = () => position + 1;
1037
1246
  runtime.getBehavior = () => steps.slice(0, position + 1).map((step) => step.state);
1247
+ return evaluateAlias(
1248
+ alias,
1249
+ steps[position].state,
1250
+ (steps[position + 1] ?? steps[loopStart ?? position]).state,
1251
+ );
1252
+ };
1253
+ const evaluateAlias = (alias: string, state: Value[], next: Value[] | undefined) => {
1038
1254
  try {
1039
1255
  const body = getConfigDefinition("alias", alias, environment).body;
1040
1256
  const value = evaluate(createIdentifier(alias), getContext(environment, state, next));
@@ -1136,7 +1352,17 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1136
1352
  trace: TraceStep[],
1137
1353
  initialState?: Value[],
1138
1354
  failureOutDegrees?: number[],
1139
- ) => {
1355
+ ): CheckResult => {
1356
+ // TLC exits with 255 on a stack overflow while it computes next states, and prints the
1357
+ // behavior up to the state it was expanding.
1358
+ if (isStackOverflow(error)) {
1359
+ return current.isExpanding
1360
+ ? toFailure(
1361
+ setExitCode(new TlaEvaluationError(STACK_OVERFLOW), EXIT_STATUS.error),
1362
+ getStateTrace(current.index),
1363
+ )
1364
+ : toFailure(new TlaEvaluationError(STACK_OVERFLOW), trace, initialState);
1365
+ }
1140
1366
  if (!(error instanceof TlaEvaluationError)) throw error;
1141
1367
  return {
1142
1368
  ...createResult("error", undefined, {
@@ -1164,6 +1390,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1164
1390
  let hash = 0x811c9dc5;
1165
1391
  for (let index = 0; index < state.length; index++)
1166
1392
  hash = combineHash(hash, getValueHash(state[index]));
1393
+ assertFingerprintable(state);
1167
1394
  return hash & 0x3fffffff;
1168
1395
  };
1169
1396
  const isSameState = (stored: Value[], state: Value[]) => {
@@ -1172,19 +1399,35 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1172
1399
  }
1173
1400
  return true;
1174
1401
  };
1402
+ const getSlot = (hash: number) => (Math.imul(hash, 0x9e3779b1) >>> seen.shift) << 1;
1175
1403
  const findStateIndex = (hash: number, state: Value[]) => {
1176
- const bucket = buckets.get(hash);
1177
- if (bucket === undefined) return -1;
1178
- if (typeof bucket === "number") {
1179
- return isSameState(fingerprintStates[bucket], state) ? bucket : -1;
1404
+ const mask = seen.slots.length - 1;
1405
+ for (let slot = getSlot(hash); ; slot = (slot + 2) & mask) {
1406
+ const entry = seen.slots[slot + 1];
1407
+ if (entry === 0) return -1;
1408
+ if (seen.slots[slot] === hash && isSameState(fingerprintStates[entry - 1], state)) {
1409
+ return entry - 1;
1410
+ }
1180
1411
  }
1181
- return bucket.find((index) => isSameState(fingerprintStates[index], state)) ?? -1;
1412
+ };
1413
+ const insertSeen = (hash: number, entry: number) => {
1414
+ const mask = seen.slots.length - 1;
1415
+ let slot = getSlot(hash);
1416
+ while (seen.slots[slot + 1] !== 0) slot = (slot + 2) & mask;
1417
+ seen.slots[slot] = hash;
1418
+ seen.slots[slot + 1] = entry;
1182
1419
  };
1183
1420
  const markSeen = (hash: number, index: number) => {
1184
- const bucket = buckets.get(hash);
1185
- if (bucket === undefined) buckets.set(hash, index);
1186
- else if (typeof bucket === "number") buckets.set(hash, [bucket, index]);
1187
- else bucket.push(index);
1421
+ seen.size++;
1422
+ if (seen.size * 4 > seen.slots.length) {
1423
+ const slots = seen.slots;
1424
+ seen.slots = new Int32Array(slots.length * 2);
1425
+ seen.shift--;
1426
+ for (let slot = 0; slot < slots.length; slot += 2) {
1427
+ if (slots[slot + 1] !== 0) insertSeen(slots[slot], slots[slot + 1]);
1428
+ }
1429
+ }
1430
+ insertSeen(hash, index + 1);
1188
1431
  };
1189
1432
  const countDistinctState = (depth: number) => {
1190
1433
  runtime.distinctStates = states.length + 1;
@@ -1210,6 +1453,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1210
1453
  isExpanding: false,
1211
1454
  candidate: undefined as Value[] | undefined,
1212
1455
  candidateLevel: 0,
1456
+ isFingerprinting: false,
1213
1457
  livenessSource: -1,
1214
1458
  };
1215
1459
  const getPath = (index: number): Value[][] =>
@@ -1235,6 +1479,18 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1235
1479
  return current.isExpanding ? depths[current.index] + 1 : 0;
1236
1480
  };
1237
1481
 
1482
+ const getGraphState = (state: Value[], level: number): GraphState => ({
1483
+ fingerprint: BigInt.asIntN(64, runtime.getFingerprint(state)),
1484
+ level,
1485
+ getLabel: () =>
1486
+ formatState(
1487
+ config.alias === undefined
1488
+ ? toRecord(state)
1489
+ : evaluateAlias(config.alias, state, undefined),
1490
+ ),
1491
+ getText: () => formatState(toRecord(state)),
1492
+ });
1493
+
1238
1494
  const visitInitialState = (state: Value[]) => {
1239
1495
  canonicalizeState(state);
1240
1496
  runtime.generatedStates++;
@@ -1251,6 +1507,7 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1251
1507
  countDistinctState(1);
1252
1508
  runtime.queueSize++;
1253
1509
  liveness?.addInitialState(states.length, state);
1510
+ stateWriter?.writeInitialState(getGraphState(state, 1));
1254
1511
  }
1255
1512
  const violated = search.findViolatedInvariant(state);
1256
1513
  if (violated) {
@@ -1271,20 +1528,36 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1271
1528
  addState(state, fingerprintState, -1, undefined);
1272
1529
  return undefined;
1273
1530
  };
1274
- const visitSuccessor = (index: number, successor: Successor, liveSuccessors: LiveSuccessor[]) => {
1531
+ const visitSuccessor = (
1532
+ index: number,
1533
+ successor: Successor,
1534
+ liveSuccessors: LiveSuccessor[],
1535
+ source: GraphState | undefined,
1536
+ action: SpecAction | undefined,
1537
+ ) => {
1275
1538
  const state = states[index];
1276
1539
  runtime.generatedStates++;
1277
1540
  current.candidate = successor.state;
1278
1541
  current.candidateLevel = depths[index] + 1;
1279
1542
  if (isCanonicalizedFirst) canonicalizeState(successor.state);
1543
+ current.isFingerprinting = true;
1280
1544
  const fingerprintState = getFingerprintState(successor.state);
1281
1545
  const hash = getStateHash(fingerprintState);
1546
+ current.isFingerprinting = false;
1282
1547
  const found = findStateIndex(hash, fingerprintState);
1283
1548
  const isStateInModel = search.satisfiesConstraints(successor.state, state);
1284
1549
  if (isStateInModel) search.trackStatePossible(successor.state);
1285
1550
  const isInModel = isStateInModel && search.satisfiesActionConstraints(state, successor.state);
1286
1551
  if (isInModel) search.trackActionPossible(state, successor.state);
1287
1552
  const existing = isInModel ? found : -1;
1553
+ if (source && isInModel) {
1554
+ stateWriter?.writeSuccessor(
1555
+ source,
1556
+ getGraphState(successor.state, depths[index] + 1),
1557
+ existing !== -1,
1558
+ action && { name: action.name, signature: getActionSignature(action) },
1559
+ );
1560
+ }
1288
1561
  if (existing !== -1 && liveness) {
1289
1562
  const stored = states[existing];
1290
1563
  const isStored = stored.every((value, position) => value === successor.state[position]);
@@ -1342,10 +1615,22 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1342
1615
  spec.scope,
1343
1616
  );
1344
1617
  } catch (error) {
1345
- return toFailure(error, []);
1618
+ if (!(error instanceof TlaIllegalStateError))
1619
+ return replayInitialStates(toFailure(error, []));
1620
+ // TLC prints an illegal initial state with null for each variable it leaves undefined, and
1621
+ // stops before it prints the statistics.
1622
+ const illegal = toFailure(
1623
+ error,
1624
+ [],
1625
+ error.state.map((value) => value ?? createModelValue("null")),
1626
+ );
1627
+ return {
1628
+ ...illegal,
1629
+ failure: illegal.failure && { ...illegal.failure, isWithoutStatistics: true },
1630
+ };
1346
1631
  }
1347
1632
  }
1348
- if (outcome.result) return outcome.result;
1633
+ if (outcome.result) return replayInitialStates(outcome.result);
1349
1634
 
1350
1635
  const next = spec.next;
1351
1636
  if (next === undefined) {
@@ -1356,23 +1641,58 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1356
1641
  }
1357
1642
  return createSuccess();
1358
1643
  }
1644
+ // TLC labels and colors an edge of the -dump state graph by the action that generated it. When
1645
+ // the graph names its edges, the search generates successors action by action, as TLC does.
1646
+ const isNamingEdges = Boolean(
1647
+ options.dump?.dot && (options.dump.actionLabels || options.dump.colorize),
1648
+ );
1649
+ const forEachActionSuccessor = (
1650
+ state: Value[],
1651
+ onSuccessor: (successor: Successor, action: SpecAction | undefined) => boolean,
1652
+ ) => {
1653
+ if (!isNamingEdges) {
1654
+ return forEachSuccessor(
1655
+ next,
1656
+ state,
1657
+ environment,
1658
+ (successor) => onSuccessor(successor, undefined),
1659
+ spec.scope,
1660
+ );
1661
+ }
1662
+ nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
1663
+ const progress = { isStopped: false };
1664
+ return nextActions.value.reduce((count, action) => {
1665
+ if (progress.isStopped) return count;
1666
+ return (
1667
+ count +
1668
+ forEachSuccessor(
1669
+ action.expression,
1670
+ state,
1671
+ environment,
1672
+ (successor) => {
1673
+ progress.isStopped = onSuccessor(successor, action);
1674
+ return progress.isStopped;
1675
+ },
1676
+ action.scope,
1677
+ )
1678
+ );
1679
+ }, 0);
1680
+ };
1359
1681
  const expandState = (index: number) => {
1360
1682
  const state = states[index];
1361
1683
  const liveSuccessors: LiveSuccessor[] = [];
1362
- const successorCount = forEachSuccessor(
1363
- next,
1364
- state,
1365
- environment,
1366
- (successor) => {
1367
- try {
1368
- outcome.result = visitSuccessor(index, successor, liveSuccessors);
1369
- } catch (error) {
1370
- outcome.result = toFailure(error, getTrace(index, successor));
1371
- }
1372
- return outcome.result !== undefined;
1373
- },
1374
- spec.scope,
1375
- );
1684
+ const source = stateWriter && getGraphState(state, depths[index]);
1685
+ const successorCount = forEachActionSuccessor(state, (successor, action) => {
1686
+ try {
1687
+ outcome.result = visitSuccessor(index, successor, liveSuccessors, source, action);
1688
+ } catch (error) {
1689
+ // TLC reports a successor it cannot fingerprint as a failure of the state it expands.
1690
+ if (current.isFingerprinting) throw error;
1691
+ const failure = toFailure(error, getTrace(index, successor));
1692
+ outcome.result = isStackOverflow(error) ? failure : replaySuccessors(failure, index);
1693
+ }
1694
+ return outcome.result !== undefined;
1695
+ });
1376
1696
  current.isExpanding = false;
1377
1697
  if (outcome.result) return outcome.result;
1378
1698
  if (successorCount === 0 && config.checkDeadlock) {
@@ -1392,9 +1712,44 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1392
1712
  trace: toTrace(steps, accepted),
1393
1713
  });
1394
1714
  } catch (error) {
1395
- return toFailure(error, getStateTrace(index), undefined, getPartialOutDegrees());
1715
+ return replayFailure(
1716
+ toFailure(toNextStateError(error), getStateTrace(index), undefined, getPartialOutDegrees()),
1717
+ (_, traced) =>
1718
+ createLivenessChecker(
1719
+ search.properties.liveness,
1720
+ spec.temporals,
1721
+ traced,
1722
+ liveGraph,
1723
+ )?.addState(index, state, liveSuccessors),
1724
+ true,
1725
+ );
1396
1726
  }
1397
1727
  };
1728
+ // TLC names the action when the next-state relation has more than one, and prints the
1729
+ // incomplete successor with null for each variable it leaves undefined.
1730
+ const reportIncompleteSuccessor = (error: TlaIncompleteStateError, index: number) => {
1731
+ nextActions.value ??= getNextActions(config, environment, specification.instanceActions);
1732
+ const actions = nextActions.value;
1733
+ const action =
1734
+ actions.length === 1 ? actions[0] : actions.find((item) => item.name === error.action);
1735
+ const variables = `variable${error.missing.length > 1 ? "s are" : " is"} not defined: ${error.missing.join(", ")}.\n`;
1736
+ runtime.generatedStates++;
1737
+ return toFailure(
1738
+ actions.length === 1
1739
+ ? error
1740
+ : new TlaEvaluationError(
1741
+ `Successor state is not completely specified by action ${error.action} of the next-state relation. The following ${variables}`,
1742
+ ),
1743
+ [
1744
+ ...getStateTrace(index),
1745
+ {
1746
+ action: error.action,
1747
+ label: action && getActionLabel(action),
1748
+ state: toRecord(error.state.map((value) => value ?? createModelValue("null"))),
1749
+ },
1750
+ ],
1751
+ );
1752
+ };
1398
1753
  // TLC names the properties and the step back into the cycle from the states it prints.
1399
1754
  const reportLasso = (checker: LivenessChecker, lasso: Lasso) => {
1400
1755
  const path = [...lasso.prefix, ...lasso.cycle];
@@ -1440,7 +1795,14 @@ export const checkSequentially = (options: CheckOptions): CheckResult => {
1440
1795
  try {
1441
1796
  outcome.result = expandState(index);
1442
1797
  } catch (error) {
1443
- outcome.result = toFailure(withNextStateExitCode(error), getStateTrace(index));
1798
+ const failure =
1799
+ error instanceof TlaIncompleteStateError
1800
+ ? reportIncompleteSuccessor(error, index)
1801
+ : toFailure(toNextStateError(error), getStateTrace(index));
1802
+ outcome.result =
1803
+ error instanceof TlaIncompleteStateError || isStackOverflow(error)
1804
+ ? failure
1805
+ : replaySuccessors(failure, index);
1444
1806
  }
1445
1807
  if (outcome.result) return outcome.result;
1446
1808
  outDegrees[getOutDegreeBucket()]++;