everystate 0.1.0 → 0.3.1

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 (55) hide show
  1. package/README.md +121 -86
  2. package/bin/everystate.mjs +35 -0
  3. package/native/darwin-arm64/everystate +0 -0
  4. package/native/darwin-x64/everystate +0 -0
  5. package/native/linux-arm64/everystate +0 -0
  6. package/native/linux-x64/everystate +0 -0
  7. package/native/win32-x64/everystate.exe +0 -0
  8. package/package.json +29 -24
  9. package/skills/formal-verification/SKILL.md +115 -0
  10. package/skills/formal-verification/example/Charge.cfg +7 -0
  11. package/skills/formal-verification/example/Charge.tla +61 -0
  12. package/skills/formal-verification/example/ChargeBroken.cfg +7 -0
  13. package/wasm/cli.mjs +26 -0
  14. package/wasm/everystate.d.ts +33 -0
  15. package/wasm/everystate.mjs +112 -0
  16. package/wasm/everystate.wasm +0 -0
  17. package/CONFORMANCE.md +0 -64
  18. package/dist/bundled.d.ts +0 -1
  19. package/dist/bundled.js +0 -2
  20. package/dist/cfg.d.ts +0 -24
  21. package/dist/cfg.js +0 -252
  22. package/dist/checker.d.ts +0 -96
  23. package/dist/checker.js +0 -520
  24. package/dist/cli.d.ts +0 -2
  25. package/dist/cli.js +0 -4
  26. package/dist/community.d.ts +0 -17
  27. package/dist/community.js +0 -1381
  28. package/dist/entry.d.ts +0 -2
  29. package/dist/entry.js +0 -39
  30. package/dist/eval.d.ts +0 -119
  31. package/dist/eval.js +0 -1533
  32. package/dist/index.d.ts +0 -33
  33. package/dist/index.js +0 -24
  34. package/dist/java.d.ts +0 -2
  35. package/dist/java.js +0 -6
  36. package/dist/lexer.d.ts +0 -17
  37. package/dist/lexer.js +0 -255
  38. package/dist/liveness.d.ts +0 -80
  39. package/dist/liveness.js +0 -855
  40. package/dist/main.d.ts +0 -40
  41. package/dist/main.js +0 -494
  42. package/dist/model.d.ts +0 -56
  43. package/dist/model.js +0 -582
  44. package/dist/modules.d.ts +0 -91
  45. package/dist/modules.js +0 -649
  46. package/dist/node-host.d.ts +0 -2
  47. package/dist/node-host.js +0 -25
  48. package/dist/parser.d.ts +0 -30
  49. package/dist/parser.js +0 -1114
  50. package/dist/pprint.d.ts +0 -7
  51. package/dist/pprint.js +0 -647
  52. package/dist/stdlib.d.ts +0 -14
  53. package/dist/stdlib.js +0 -588
  54. package/dist/values.d.ts +0 -150
  55. package/dist/values.js +0 -822
@@ -0,0 +1,112 @@
1
+ // everystate as WebAssembly: the same `check({ root, modules, config, deadlock?, print? })` API as the
2
+ // TypeScript package's index.ts, backed by the Rust engine. Works in Node and Bun out of the box (the
3
+ // .wasm file next to this module is read synchronously); in a browser or worker call `init()` first
4
+ // with the compiled module, its bytes, or a fetch() Response.
5
+ //
6
+ // Differences from the TypeScript API: a trace state is an array of already formatted values (one
7
+ // string per variable, in `model.vars` order), so `fmt` is the identity on them; `model` only carries
8
+ // `vars`. Every call runs in a fresh WebAssembly instance (string ordering starts from scratch, as
9
+ // `resetInterning()` does in TypeScript) and its memory is released afterwards.
10
+
11
+ let compiled = null;
12
+
13
+ /** Optional: provide the compiled module (WebAssembly.Module, bytes, or a Response / Promise of one). */
14
+ export async function init(source) {
15
+ source = await source;
16
+ if (source instanceof WebAssembly.Module) compiled = source;
17
+ else if (typeof Response !== 'undefined' && source instanceof Response) compiled = await WebAssembly.compile(await source.arrayBuffer());
18
+ else if (source) compiled = await WebAssembly.compile(source);
19
+ else compiled = load();
20
+ return compiled;
21
+ }
22
+
23
+ function load() {
24
+ if (compiled) return compiled;
25
+ const url = new URL('./everystate.wasm', import.meta.url);
26
+ let bytes;
27
+ if (typeof process !== 'undefined' && process.getBuiltinModule) bytes = process.getBuiltinModule('node:fs').readFileSync(url);
28
+ else if (typeof Bun !== 'undefined' || typeof process !== 'undefined') bytes = globalThis.require?.('node:fs').readFileSync(url);
29
+ if (!bytes) throw new Error('everystate: call `await init(wasmModuleOrBytes)` before check() in this environment.');
30
+ compiled = new WebAssembly.Module(bytes);
31
+ return compiled;
32
+ }
33
+
34
+ const enc = new TextEncoder(), dec = new TextDecoder();
35
+
36
+ export class TlaError extends Error {
37
+ constructor(message, kind) { super(message); this.kind = kind; }
38
+ }
39
+
40
+ /** Formats a value of a trace state (they are already strings in TLC syntax). */
41
+ export const fmt = v => String(v);
42
+
43
+ /**
44
+ * Model-check a specification held in memory.
45
+ * @param {{root: string, modules: Record<string,string>, config: string, deadlock?: boolean, print?: (line: string) => void}} input
46
+ */
47
+ export function check(input) {
48
+ const module = load();
49
+ let memory;
50
+ const print = input.print ?? (() => {});
51
+ const instance = new WebAssembly.Instance(module, {
52
+ env: {
53
+ es_print: (ptr, len) => print(dec.decode(new Uint8Array(memory.buffer, ptr, len))),
54
+ es_now: () => Date.now(),
55
+ },
56
+ });
57
+ const ex = instance.exports;
58
+ memory = ex.memory;
59
+ const put = s => {
60
+ const b = enc.encode(s);
61
+ const p = ex.es_alloc(b.length);
62
+ new Uint8Array(memory.buffer, p, b.length).set(b);
63
+ return [p, b.length];
64
+ };
65
+ for (const [name, src] of Object.entries(input.modules)) ex.es_add_module(...put(name), ...put(src));
66
+ let json;
67
+ try {
68
+ const p = ex.es_check(...put(input.root), ...put(input.config), (input.deadlock ?? true) ? 1 : 0);
69
+ json = JSON.parse(dec.decode(new Uint8Array(memory.buffer, p, ex.es_result_len())));
70
+ } catch (e) {
71
+ // The engine's call stack (RangeError) or the 64 MiB linear-memory stack (RuntimeError) ran out.
72
+ if (e instanceof RangeError || (typeof WebAssembly !== 'undefined' && e instanceof WebAssembly.RuntimeError)) {
73
+ return {
74
+ result: { status: 'error', message: 'Stack overflow while evaluating the specification (recursion too deep). Re-run with a larger stack.', generated: 0, distinct: 0, queue: 0, depth: 0 },
75
+ model: { vars: [] },
76
+ };
77
+ }
78
+ throw e;
79
+ }
80
+ if (json.status === 'setup-error') throw new TlaError(json.message, json.kind);
81
+ const vars = json.vars ?? [];
82
+ const act = name => (name === null || name === undefined ? null : { name });
83
+ const result = {
84
+ status: json.status, name: json.name, message: json.message, code: json.code,
85
+ generated: json.generated, distinct: json.distinct, queue: json.queue, depth: json.depth,
86
+ loopTo: json.loopTo, loopAction: json.status === 'liveness' ? act(json.loopAction) : undefined,
87
+ trace: json.trace?.map(t => ({ action: act(t.action), state: vars.map(v => t.state[v]) })),
88
+ };
89
+ return { result, model: { vars: vars.map(name => ({ name })) } };
90
+ }
91
+
92
+ /**
93
+ * Like `check`, but (under Node) runs in a worker thread with a large call stack, because WebAssembly
94
+ * recursion uses the JavaScript engine's stack: the default ~1 MB allows roughly 500-1000 nested TLA+
95
+ * operator applications, 256 MB allows the evaluator's own limit of 6000. Elsewhere it falls back to `check`.
96
+ */
97
+ export async function checkAsync(input, { stackSizeMb = 256 } = {}) {
98
+ const isNode = typeof process !== 'undefined' && process.versions?.node && !process.versions.bun && typeof Deno === 'undefined';
99
+ if (!isNode) return check(input);
100
+ const { Worker } = await import('node:worker_threads');
101
+ const { print, ...plain } = input;
102
+ const code = `
103
+ import { parentPort, workerData } from 'node:worker_threads';
104
+ const { check } = await import(workerData.url);
105
+ const out = check({ ...workerData.input, print: line => parentPort.postMessage({ line }) });
106
+ parentPort.postMessage({ out });`;
107
+ return new Promise((resolve, reject) => {
108
+ const w = new Worker(code, { eval: true, type: 'module', workerData: { url: import.meta.url, input: plain }, resourceLimits: { stackSizeMb } });
109
+ w.on('message', m => { if (m.line !== undefined) print?.(m.line); else resolve(m.out); });
110
+ w.on('error', reject);
111
+ });
112
+ }
Binary file
package/CONFORMANCE.md DELETED
@@ -1,64 +0,0 @@
1
- # Conformance
2
-
3
- Real TLC is the specification. Numbers below are from 2026-11-13, commit `3c73470`, against
4
- `tla2tools.jar` 1.8.0 (and TLC 2.19 output for the statechart corpus).
5
-
6
- ## tlaplus/Examples manifest
7
-
8
- `npm run conformance:examples -- /path/to/Examples --max-runtime 5`
9
-
10
- Models whose upstream runtime is 5 seconds or less, exhaustive mode: **121 of 122** match on result
11
- kind and on exact generated and distinct state counts.
12
-
13
- - The one failure, `ewd998/EWD998ChanTrace.cfg`, reads a data file by a path relative to the
14
- Examples checkout; it passes when the script is run from that directory.
15
- - State depth is not compared: the upstream numbers were recorded with several workers, which makes
16
- depth nondeterministic. Single-worker TLC agrees with everystate on the cases checked by hand.
17
- - The manifest labels action-property violations "liveness failure" (TLC exit code 13); the script
18
- maps them the same way.
19
-
20
- Larger models (upstream runtime up to 120 seconds, 15-minute timeout each): **161 of 164** match;
21
- the other three (`MCCheckpointCoordination`, `DijkstraMutex` Safety-4-processors, `MCInnerSerial`)
22
- hit the timeout rather than disagreeing. Run before the lazy-set description fix below, which only
23
- made things faster.
24
-
25
- ## TLC's own `test-model` directory
26
-
27
- `npm run conformance:tlc -- /path/to/tlaplus/tlatools/org.lamport.tlatools/test-model`
28
-
29
- Every `X.tla` with an `X.cfg` is run through real TLC and through everystate; outcome class, violated
30
- invariant name, generated/distinct counts and trace length are compared.
31
-
32
- **377 of 414 match.** 8 are skipped because TLC itself does not finish in the time limit.
33
- The 29 mismatches:
34
-
35
- | Reason | Count | Specs |
36
- |---|---|---|
37
- | Result depends on a random seed | 4 | `RandomElement`, `RandomSubsetNext`, `SpanTreeRandom`, `RandomElementXandY` |
38
- | Out of scope: fingerprint-collision tests, Java module override | 4 | `Unicode*Fingerprint` (3), `UserModuleOverride` |
39
- | Deliberate: TLC rejects `\cdot` unless a flag is set, everystate implements it | 3 | `cdot/*` |
40
- | Labels and the full subexpression-selector syntax | 4 | `suite/test206`, `test209`, `test219`, `test219a` |
41
- | Errors only SANY reports (non-Leibniz substitution, level errors, `HIDE`) | 4 | `suite/test212`–`214`, `test217` |
42
- | Suites of hundreds of assumptions on exact TLC internals and message text | 4 | `EmptySetEqAssume`, `ValueSemanticsAssume`, `NormalizationAssume`, `KSubsetAssume` |
43
- | TLC keeps mixed-type sets unsorted until needed | 2 | `NormalizationLiveness`, `NormalizationTLCSet` |
44
- | TLC checks some temporal properties during the search, everystate only at the end | 3 | `InitialLivenessEvaluationError`, `Github710b`, `Github710d` |
45
- | `ALIAS` trace formatting | 1 | `PrintTraceException` |
46
-
47
- ## pstack `statechart` corpus
48
-
49
- `npm run conformance:statechart -- /path/to/pstack/skills/statechart/scripts`
50
-
51
- The unmodified script, with `everystate-java` in place of Java and no `tla2tools.jar`:
52
- **33 of 33** cases produce a byte-identical `chart.mmd` and `report.md`.
53
-
54
- ## Self-contained tests
55
-
56
- `npm test` — 34 pass, 0 fail, 4 todo. No Java needed.
57
-
58
- ## Known behavioural differences from TLC
59
-
60
- - Liveness is checked once, after the state space is complete (TLC's `-lncheck final`).
61
- - Fingerprints are 64-bit but not TLC's, so node ids in `-dump dot` output differ.
62
- - Sets are kept sorted from construction; TLC sorts lazily. A set mixing incomparable values fails
63
- earlier than in TLC.
64
- - `\cdot` (action composition) works without a flag.
package/dist/bundled.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare const BUNDLED: Record<string, string>;