tla-ts 0.0.3-dev.6979965 → 0.0.3-dev.6d0aeb6
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.
- package/README.md +16 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +60 -22
- package/dist/index.d.ts +60 -22
- package/dist/index.js +1 -1
- package/dist/parallel-worker.cjs +2 -2
- package/dist/parallel-worker.js +2 -2
- package/dist/parallel.cjs +1 -1
- package/dist/parallel.js +2 -2
- package/dist/run-cli.cjs +20 -17
- package/dist/run-cli.js +22 -19
- package/dist/transfer.cjs +45 -16
- package/dist/transfer.js +45 -16
- package/package.json +1 -1
- package/src/ast.ts +101 -1
- package/src/builtins.ts +9 -3
- package/src/check.ts +514 -99
- package/src/cli-options.ts +11 -5
- package/src/codegen.ts +15 -22
- package/src/config.ts +224 -170
- package/src/dependencies.ts +2 -2
- package/src/environment.ts +158 -49
- package/src/errors.ts +98 -3
- package/src/evaluate.ts +255 -159
- package/src/exit-codes.ts +39 -42
- package/src/fingerprints.ts +2 -1
- package/src/instances.ts +16 -15
- package/src/levels.ts +181 -45
- package/src/lexer.ts +5 -1
- package/src/live-check.ts +772 -0
- package/src/liveness.ts +120 -543
- package/src/parallel-worker.ts +102 -115
- package/src/parallel.ts +34 -23
- package/src/parser.ts +83 -20
- package/src/resolve.ts +490 -95
- package/src/run-cli.ts +113 -20
- package/src/runtime.ts +3 -1
- package/src/sany-lexer.ts +615 -0
- package/src/sany-parse-error.ts +3143 -0
- package/src/spec-description.ts +19 -8
- package/src/standard-modules.ts +54 -45
- package/src/state-graph.ts +157 -0
- package/src/subexpressions.ts +35 -23
- package/src/substitution-levels.ts +38 -17
- package/src/successors.ts +183 -39
- package/src/symmetry.ts +30 -8
- package/src/tableau.ts +51 -24
- package/src/tlc-ext.ts +29 -12
- package/src/tlc-module.ts +14 -33
- package/src/tlc-modules.ts +4 -0
- package/src/tlc-order.ts +25 -7
- package/src/tlc-random.ts +9 -6
- package/src/tlc-summary.ts +20 -5
- package/src/value-sources.ts +29 -0
- package/src/values.ts +249 -65
package/src/parallel-worker.ts
CHANGED
|
@@ -15,62 +15,27 @@ import type {
|
|
|
15
15
|
WorkerMessage,
|
|
16
16
|
} from "./parallel";
|
|
17
17
|
import { getSuccessors, type Successor } from "./successors";
|
|
18
|
-
import { getTlcStringOrder,
|
|
18
|
+
import { getTlcStringOrder, getTlcDeclarationOrder } from "./tlc-order";
|
|
19
19
|
import { OUT_DEGREE_BUCKETS } from "./tlc-summary";
|
|
20
20
|
import { createStateDecoder, createStateEncoder, type EncodedStates } from "./transfer";
|
|
21
21
|
import { getRankedStringCount, setStringOrder, type Value } from "./values";
|
|
22
22
|
|
|
23
23
|
const WORKER_THREADS_MODULE = "node:worker_threads";
|
|
24
|
-
|
|
25
|
-
interface PeerMessage {
|
|
26
|
-
phase: "balance";
|
|
27
|
-
level: number;
|
|
28
|
-
from: number;
|
|
29
|
-
states: EncodedStates;
|
|
30
|
-
}
|
|
24
|
+
const LOCKED = -1;
|
|
31
25
|
|
|
32
26
|
interface Expansion {
|
|
27
|
+
states: Value[][];
|
|
33
28
|
origin: number;
|
|
29
|
+
end: number;
|
|
34
30
|
successors: Successor[] | undefined;
|
|
35
31
|
index: number;
|
|
32
|
+
added: number;
|
|
36
33
|
generated: number;
|
|
37
|
-
|
|
34
|
+
outDegrees: number[];
|
|
38
35
|
next: Value[][];
|
|
39
36
|
fingerprintStates: Value[][];
|
|
40
37
|
}
|
|
41
38
|
|
|
42
|
-
interface Transfer {
|
|
43
|
-
from: number;
|
|
44
|
-
to: number;
|
|
45
|
-
count: number;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const getBalancePlan = (sizes: number[]) => {
|
|
49
|
-
const total = sizes.reduce((sum, size) => sum + size, 0);
|
|
50
|
-
const average = total / sizes.length;
|
|
51
|
-
if (Math.max(...sizes) - Math.min(...sizes) <= Math.max(64, average / 4)) return [];
|
|
52
|
-
const targets = sizes.map(
|
|
53
|
-
(_, index) => Math.floor(average) + (index < total % sizes.length ? 1 : 0),
|
|
54
|
-
);
|
|
55
|
-
const donors = sizes.flatMap((size, index) =>
|
|
56
|
-
size > targets[index] ? [{ index, amount: size - targets[index] }] : [],
|
|
57
|
-
);
|
|
58
|
-
const receivers = sizes.flatMap((size, index) =>
|
|
59
|
-
size < targets[index] ? [{ index, amount: targets[index] - size }] : [],
|
|
60
|
-
);
|
|
61
|
-
const transfers: Transfer[] = [];
|
|
62
|
-
for (const donor of donors) {
|
|
63
|
-
for (const receiver of receivers) {
|
|
64
|
-
const moved = Math.min(donor.amount, receiver.amount);
|
|
65
|
-
if (moved === 0) continue;
|
|
66
|
-
transfers.push({ from: donor.index, to: receiver.index, count: moved });
|
|
67
|
-
donor.amount -= moved;
|
|
68
|
-
receiver.amount -= moved;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
return transfers;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
39
|
const getParentPort = async () => {
|
|
75
40
|
if (typeof (globalThis as { postMessage?: unknown }).postMessage === "function") {
|
|
76
41
|
return globalThis as unknown as Port;
|
|
@@ -81,33 +46,6 @@ const getParentPort = async () => {
|
|
|
81
46
|
return threads.parentPort as unknown as Port;
|
|
82
47
|
};
|
|
83
48
|
|
|
84
|
-
const createInbox = () => {
|
|
85
|
-
const messages = new Map<string, PeerMessage[]>();
|
|
86
|
-
const waiters = new Map<string, { count: number; resolve: (messages: PeerMessage[]) => void }>();
|
|
87
|
-
const getKey = (phase: PeerMessage["phase"], level: number) => `${phase}:${level}`;
|
|
88
|
-
const settle = (key: string) => {
|
|
89
|
-
const waiter = waiters.get(key);
|
|
90
|
-
const received = messages.get(key) ?? [];
|
|
91
|
-
if (!waiter || received.length < waiter.count) return;
|
|
92
|
-
waiters.delete(key);
|
|
93
|
-
messages.delete(key);
|
|
94
|
-
waiter.resolve(received);
|
|
95
|
-
};
|
|
96
|
-
return {
|
|
97
|
-
deliver: (message: PeerMessage) => {
|
|
98
|
-
const key = getKey(message.phase, message.level);
|
|
99
|
-
messages.set(key, [...(messages.get(key) ?? []), message]);
|
|
100
|
-
settle(key);
|
|
101
|
-
},
|
|
102
|
-
receive: (phase: PeerMessage["phase"], level: number, count: number) =>
|
|
103
|
-
new Promise<PeerMessage[]>((resolve) => {
|
|
104
|
-
const key = getKey(phase, level);
|
|
105
|
-
waiters.set(key, { count, resolve });
|
|
106
|
-
settle(key);
|
|
107
|
-
}),
|
|
108
|
-
};
|
|
109
|
-
};
|
|
110
|
-
|
|
111
49
|
const run = (start: StartMessage, parent: Port) => {
|
|
112
50
|
const modules = new Map(start.modules);
|
|
113
51
|
const loadModule = (name: string) => modules.get(name);
|
|
@@ -123,7 +61,7 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
123
61
|
});
|
|
124
62
|
const environment = specification.environment;
|
|
125
63
|
const search = createSearch(start.config, environment, () =>
|
|
126
|
-
|
|
64
|
+
getTlcDeclarationOrder(start.source, loadModule, "variable"),
|
|
127
65
|
);
|
|
128
66
|
const nextAction = search?.spec.next;
|
|
129
67
|
if (!search || nextAction === undefined)
|
|
@@ -132,26 +70,55 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
132
70
|
const canonicalize = createCanonicalizer();
|
|
133
71
|
const decode = createStateDecoder(environment.variables.length);
|
|
134
72
|
const shared = { fingerprints: start.fingerprints };
|
|
73
|
+
const requests = start.requests;
|
|
74
|
+
const remaining = start.remaining;
|
|
135
75
|
// Under symmetry, a successor the worker generated before has the representative it had then,
|
|
136
76
|
// so the worker skips the search for one.
|
|
137
77
|
const generatedStates = search.isSymmetric ? createFingerprintTable() : undefined;
|
|
138
78
|
const fingerprint = new Int32Array(2);
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
peer.port.start?.();
|
|
79
|
+
const waiters = { resume: () => {}, loot: (_states: EncodedStates) => {} };
|
|
80
|
+
start.ports.forEach((port) => {
|
|
81
|
+
port?.addEventListener("message", (event) => waiters.loot(event.data as EncodedStates));
|
|
82
|
+
port?.start?.();
|
|
144
83
|
});
|
|
145
84
|
const frontier = { states: decode(start.seed).map(canonicalize) };
|
|
146
|
-
|
|
147
|
-
|
|
85
|
+
|
|
86
|
+
// A worker that runs out of states asks a peer for some by writing its index + 1 to the peer's
|
|
87
|
+
// request word. The peer sends half of the states it hasn't started, keeps expanding, and wakes
|
|
88
|
+
// the workers waiting to ask it next.
|
|
89
|
+
const answer = (expansion: Expansion, request: number) => {
|
|
90
|
+
const thief = Atomics.load(requests, self) - 1;
|
|
91
|
+
const given = Math.floor((expansion.end - expansion.origin - 1) / 2);
|
|
92
|
+
const encoder = createStateEncoder();
|
|
93
|
+
const states = expansion.states.slice(expansion.end - given, expansion.end);
|
|
94
|
+
const isEncoded = given > 0 && states.every(encoder.encode);
|
|
95
|
+
if (isEncoded) expansion.end -= given;
|
|
96
|
+
const loot = isEncoded ? encoder.finish() : createStateEncoder().finish();
|
|
97
|
+
start.ports[thief]?.postMessage(loot, [loot.words.buffer]);
|
|
98
|
+
Atomics.store(requests, self, request);
|
|
99
|
+
Atomics.notify(requests, self);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// A worker answers any request before it pauses or runs out of states, and takes no more until
|
|
103
|
+
// it resumes.
|
|
104
|
+
const lock = (expansion: Expansion) => {
|
|
105
|
+
Atomics.store(remaining, self, 0);
|
|
106
|
+
if (Atomics.compareExchange(requests, self, 0, LOCKED) === 0) return;
|
|
107
|
+
answer(expansion, LOCKED);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const unlock = (expansion: Expansion) => {
|
|
111
|
+
Atomics.store(remaining, self, expansion.end - expansion.origin);
|
|
112
|
+
Atomics.store(requests, self, 0);
|
|
113
|
+
};
|
|
148
114
|
|
|
149
115
|
// The worker pauses before a successor when the shared table needs to grow, and resumes there.
|
|
150
116
|
const expand = (expansion: Expansion) => {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const state = states[expansion.origin];
|
|
117
|
+
for (; expansion.origin < expansion.end; expansion.origin++) {
|
|
118
|
+
const state = expansion.states[expansion.origin];
|
|
154
119
|
if (!expansion.successors) {
|
|
120
|
+
if (Atomics.load(requests, self) > 0) answer(expansion, 0);
|
|
121
|
+
Atomics.store(remaining, self, expansion.end - expansion.origin - 1);
|
|
155
122
|
expansion.successors = getSuccessors(nextAction, state, environment, search.spec.scope);
|
|
156
123
|
expansion.generated += expansion.successors.length;
|
|
157
124
|
if (expansion.successors.length === 0 && start.config.checkDeadlock) return undefined;
|
|
@@ -161,7 +128,11 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
161
128
|
if (isSharedFingerprintsFull(shared.fingerprints)) return "paused";
|
|
162
129
|
const successor = successors[expansion.index];
|
|
163
130
|
if (search.findViolatedActionProperty(state, successor.state)) return undefined;
|
|
164
|
-
if (!search.satisfiesActionConstraints(state, successor.state))
|
|
131
|
+
if (!search.satisfiesActionConstraints(state, successor.state)) {
|
|
132
|
+
// TLC checks the invariants of every successor it has not seen, even outside the model.
|
|
133
|
+
if (search.findViolatedInvariant(successor.state, state)) return undefined;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
165
136
|
if (generatedStates) {
|
|
166
137
|
writeStateFingerprint(successor.state, fingerprint);
|
|
167
138
|
if (!generatedStates.insert(fingerprint[0], fingerprint[1])) continue;
|
|
@@ -174,69 +145,85 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
174
145
|
if (!search.satisfiesConstraints(successor.state, state)) continue;
|
|
175
146
|
expansion.next.push(successor.state);
|
|
176
147
|
expansion.fingerprintStates.push(fingerprintState);
|
|
177
|
-
expansion.
|
|
148
|
+
expansion.added++;
|
|
178
149
|
}
|
|
150
|
+
expansion.outDegrees[Math.min(expansion.added, OUT_DEGREE_BUCKETS - 1)]++;
|
|
179
151
|
expansion.successors = undefined;
|
|
180
152
|
expansion.index = 0;
|
|
153
|
+
expansion.added = 0;
|
|
181
154
|
}
|
|
182
155
|
return "done";
|
|
183
156
|
};
|
|
184
157
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
const
|
|
192
|
-
|
|
158
|
+
// Each attempt asks the peer with the most states left. While another worker's request to that
|
|
159
|
+
// peer is open, the worker waits for the peer to answer it. The worker stops once no peer that
|
|
160
|
+
// takes requests has two states left.
|
|
161
|
+
const steal = async (expansion: Expansion) => {
|
|
162
|
+
const skipped = new Set<number>();
|
|
163
|
+
for (;;) {
|
|
164
|
+
const victim = start.ports.reduce<number | undefined>((best, port, index) => {
|
|
165
|
+
if (!port || skipped.has(index) || Atomics.load(remaining, index) < 2) return best;
|
|
166
|
+
return best === undefined || Atomics.load(remaining, index) > Atomics.load(remaining, best)
|
|
167
|
+
? index
|
|
168
|
+
: best;
|
|
169
|
+
}, undefined);
|
|
170
|
+
if (victim === undefined) return false;
|
|
171
|
+
const request = Atomics.compareExchange(requests, victim, 0, self + 1);
|
|
172
|
+
if (request === LOCKED) skipped.add(victim);
|
|
173
|
+
if (request > 0) Atomics.wait(requests, victim, request);
|
|
174
|
+
if (request !== 0) continue;
|
|
175
|
+
const loot = await new Promise<EncodedStates>((resolve) => (waiters.loot = resolve));
|
|
176
|
+
if (loot.count === 0) {
|
|
177
|
+
skipped.add(victim);
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
expansion.states = decode(loot).map(canonicalize);
|
|
181
|
+
expansion.origin = 0;
|
|
182
|
+
expansion.end = expansion.states.length;
|
|
183
|
+
unlock(expansion);
|
|
184
|
+
return true;
|
|
193
185
|
}
|
|
194
|
-
outgoing.forEach((encoder, index) => {
|
|
195
|
-
const states = encoder.finish();
|
|
196
|
-
send(index, { phase: "balance", level, from: self, states }, [states.words.buffer]);
|
|
197
|
-
});
|
|
198
|
-
const incoming = await inbox.receive("balance", level, peers.length);
|
|
199
|
-
incoming.forEach((message) => next.push(...decode(message.states).map(canonicalize)));
|
|
200
|
-
return true;
|
|
201
186
|
};
|
|
202
187
|
|
|
203
|
-
const waiters = { resume: () => {}, balance: (_sizes: number[]) => {} };
|
|
204
188
|
const report = (message: WorkerMessage) =>
|
|
205
189
|
parent.postMessage(message, message.type === "level" ? [message.fingerprints.buffer] : []);
|
|
206
190
|
|
|
207
191
|
const runLevel = async (level: number): Promise<LevelMessage | undefined> => {
|
|
208
192
|
const expansion: Expansion = {
|
|
193
|
+
states: frontier.states,
|
|
209
194
|
origin: 0,
|
|
195
|
+
end: frontier.states.length,
|
|
210
196
|
successors: undefined,
|
|
211
197
|
index: 0,
|
|
198
|
+
added: 0,
|
|
212
199
|
generated: 0,
|
|
213
|
-
|
|
200
|
+
outDegrees: Array.from({ length: OUT_DEGREE_BUCKETS }, () => 0),
|
|
214
201
|
next: [],
|
|
215
202
|
fingerprintStates: [],
|
|
216
203
|
};
|
|
217
|
-
for (
|
|
204
|
+
for (;;) {
|
|
205
|
+
const status = expand(expansion);
|
|
218
206
|
if (status === undefined) return undefined;
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
207
|
+
lock(expansion);
|
|
208
|
+
if (status === "paused") {
|
|
209
|
+
const resumed = new Promise<void>((resolve) => (waiters.resume = resolve));
|
|
210
|
+
report({ type: "paused" });
|
|
211
|
+
await resumed;
|
|
212
|
+
unlock(expansion);
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (!(await steal(expansion))) break;
|
|
222
216
|
}
|
|
223
217
|
if (getRankedStringCount() !== startupStrings) return undefined;
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
const outDegrees = Array.from({ length: OUT_DEGREE_BUCKETS }, () => 0);
|
|
227
|
-
expansion.newStates.forEach((degree) => outDegrees[Math.min(degree, OUT_DEGREE_BUCKETS - 1)]++);
|
|
228
|
-
const fingerprints = search.getStoredFingerprints(next, expansion.fingerprintStates);
|
|
229
|
-
const sizes = new Promise<number[]>((resolve) => (waiters.balance = resolve));
|
|
230
|
-
report({ type: "expanded", from: self, added });
|
|
231
|
-
if (!(await balance(level, next, await sizes))) return undefined;
|
|
232
|
-
frontier.states = next;
|
|
218
|
+
frontier.states = expansion.next;
|
|
219
|
+
Atomics.store(remaining, self, frontier.states.length);
|
|
233
220
|
return {
|
|
234
221
|
type: "level",
|
|
235
222
|
level,
|
|
236
223
|
generated: expansion.generated,
|
|
237
|
-
added,
|
|
238
|
-
outDegrees,
|
|
239
|
-
fingerprints,
|
|
224
|
+
added: expansion.next.length,
|
|
225
|
+
outDegrees: expansion.outDegrees,
|
|
226
|
+
fingerprints: search.getStoredFingerprints(expansion.next, expansion.fingerprintStates),
|
|
240
227
|
};
|
|
241
228
|
};
|
|
242
229
|
|
|
@@ -253,8 +240,8 @@ const run = (start: StartMessage, parent: Port) => {
|
|
|
253
240
|
shared.fingerprints = message.fingerprints;
|
|
254
241
|
waiters.resume();
|
|
255
242
|
}
|
|
256
|
-
if (message.type === "balance") waiters.balance(message.sizes);
|
|
257
243
|
});
|
|
244
|
+
Atomics.store(remaining, self, frontier.states.length);
|
|
258
245
|
report({ type: "ready" });
|
|
259
246
|
};
|
|
260
247
|
|
package/src/parallel.ts
CHANGED
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
type SharedFingerprints,
|
|
21
21
|
} from "./fingerprints";
|
|
22
22
|
import { getInitialStates } from "./successors";
|
|
23
|
-
import { getTlcStringOrder,
|
|
23
|
+
import { getTlcStringOrder, getTlcDeclarationOrder } from "./tlc-order";
|
|
24
24
|
import { getFingerprintDistance, OUT_DEGREE_BUCKETS } from "./tlc-summary";
|
|
25
25
|
import { createStateEncoder, type EncodedStates } from "./transfer";
|
|
26
26
|
import { getRankedStringCount, setStringOrder, type Value } from "./values";
|
|
@@ -44,6 +44,8 @@ export interface StartMessage {
|
|
|
44
44
|
ports: (Port | undefined)[];
|
|
45
45
|
seed: EncodedStates;
|
|
46
46
|
fingerprints: SharedFingerprints;
|
|
47
|
+
requests: Int32Array;
|
|
48
|
+
remaining: Int32Array;
|
|
47
49
|
}
|
|
48
50
|
|
|
49
51
|
export interface LevelMessage {
|
|
@@ -59,13 +61,11 @@ export type WorkerMessage =
|
|
|
59
61
|
| { type: "ready" }
|
|
60
62
|
| { type: "abort" }
|
|
61
63
|
| { type: "paused" }
|
|
62
|
-
| { type: "expanded"; from: number; added: number }
|
|
63
64
|
| LevelMessage;
|
|
64
65
|
|
|
65
66
|
export type CoordinatorMessage =
|
|
66
67
|
| { type: "level"; level: number }
|
|
67
|
-
| { type: "resize"; fingerprints: SharedFingerprints }
|
|
68
|
-
| { type: "balance"; sizes: number[] };
|
|
68
|
+
| { type: "resize"; fingerprints: SharedFingerprints };
|
|
69
69
|
|
|
70
70
|
interface WorkerHandle {
|
|
71
71
|
postMessage: (message: unknown, transfer?: unknown[]) => void;
|
|
@@ -102,11 +102,24 @@ const spawnWorker = async (onMessage: (message: WorkerMessage) => void): Promise
|
|
|
102
102
|
|
|
103
103
|
export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
104
104
|
const config = typeof options.config === "string" ? parseConfig(options.config) : options.config;
|
|
105
|
-
const
|
|
105
|
+
const print = options.onPrint ?? console.log;
|
|
106
|
+
const printed = { count: 0 };
|
|
107
|
+
// The search on the calling thread prints what this one printed while it started, so it skips that.
|
|
108
|
+
const fallBack = () => {
|
|
109
|
+
const skipped = { count: printed.count };
|
|
110
|
+
return checkSequentially({
|
|
111
|
+
...options,
|
|
112
|
+
config,
|
|
113
|
+
onPrint: (text) => (skipped.count-- > 0 ? undefined : print(text)),
|
|
114
|
+
});
|
|
115
|
+
};
|
|
106
116
|
const count = options.workers ?? 1;
|
|
117
|
+
// TLC's workers write the -dump state graph in whichever order they find it, so one worker
|
|
118
|
+
// writes the graph a single TLC worker does.
|
|
107
119
|
const isSequential =
|
|
108
120
|
count < 2 ||
|
|
109
121
|
typeof SharedArrayBuffer !== "function" ||
|
|
122
|
+
options.dump !== undefined ||
|
|
110
123
|
config.view !== undefined ||
|
|
111
124
|
config.postconditions.length > 0 ||
|
|
112
125
|
config.possible.length > 0;
|
|
@@ -129,12 +142,15 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
|
129
142
|
source: options.source,
|
|
130
143
|
loadModule,
|
|
131
144
|
loadClass,
|
|
132
|
-
print:
|
|
145
|
+
print: (text) => {
|
|
146
|
+
printed.count++;
|
|
147
|
+
print(text);
|
|
148
|
+
},
|
|
133
149
|
files: { readFile: options.readFile, writeFile: options.writeFile },
|
|
134
150
|
});
|
|
135
151
|
const environment = specification.environment;
|
|
136
152
|
const search = createSearch(config, environment, () =>
|
|
137
|
-
|
|
153
|
+
getTlcDeclarationOrder(options.source, loadModule, "variable"),
|
|
138
154
|
);
|
|
139
155
|
if (findAssumptionViolation(specification.assumptions, environment)) return fallBack();
|
|
140
156
|
if (
|
|
@@ -225,6 +241,8 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
|
225
241
|
pending.resolve = resolve;
|
|
226
242
|
});
|
|
227
243
|
const ready = waitForReports(count);
|
|
244
|
+
const requests = new Int32Array(new SharedArrayBuffer(count * 4));
|
|
245
|
+
const remaining = new Int32Array(new SharedArrayBuffer(count * 4));
|
|
228
246
|
const workers = await Promise.all(Array.from({ length: count }, () => spawnWorker(onMessage)));
|
|
229
247
|
const terminate = () => workers.forEach((worker) => worker.terminate());
|
|
230
248
|
const channels = new Map<string, MessageChannel>();
|
|
@@ -249,6 +267,8 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
|
249
267
|
ports,
|
|
250
268
|
seed,
|
|
251
269
|
fingerprints: shared.fingerprints,
|
|
270
|
+
requests,
|
|
271
|
+
remaining,
|
|
252
272
|
};
|
|
253
273
|
worker.postMessage(message, [...ports.filter((port) => port !== undefined), seed.words.buffer]);
|
|
254
274
|
});
|
|
@@ -257,34 +277,25 @@ export const check = async (options: CheckOptions): Promise<CheckResult> => {
|
|
|
257
277
|
const send = (message: CoordinatorMessage) =>
|
|
258
278
|
workers.forEach((worker) => worker.postMessage(message));
|
|
259
279
|
// Workers pause when the shared table needs to grow, and resume once it has.
|
|
260
|
-
const
|
|
280
|
+
const collectLevels = async (messages: WorkerMessage[]): Promise<WorkerMessage[]> => {
|
|
261
281
|
const paused = messages.filter((message) => message.type === "paused").length;
|
|
262
282
|
if (paused === 0 || isAborted(messages)) return messages;
|
|
263
283
|
shared.fingerprints = growSharedFingerprints(shared.fingerprints);
|
|
264
284
|
const resumed = waitForReports(paused);
|
|
265
285
|
send({ type: "resize", fingerprints: shared.fingerprints });
|
|
266
|
-
const
|
|
267
|
-
return [...
|
|
286
|
+
const reported = messages.filter((message) => message.type !== "paused");
|
|
287
|
+
return [...reported, ...(await collectLevels(await resumed))];
|
|
268
288
|
};
|
|
269
289
|
if (isAborted(await ready)) {
|
|
270
290
|
terminate();
|
|
271
291
|
return fallBack();
|
|
272
292
|
}
|
|
273
293
|
for (let level = 1; ; level++) {
|
|
274
|
-
const expanding = waitForReports(count);
|
|
275
|
-
send({ type: "level", level });
|
|
276
|
-
const expansions = await collectExpansions(await expanding);
|
|
277
|
-
if (isAborted(expansions)) {
|
|
278
|
-
terminate();
|
|
279
|
-
return fallBack();
|
|
280
|
-
}
|
|
281
|
-
const sizes = Array.from({ length: count }, () => 0);
|
|
282
|
-
expansions.forEach((message) => {
|
|
283
|
-
if (message.type === "expanded") sizes[message.from] = message.added;
|
|
284
|
-
});
|
|
285
294
|
const reported = waitForReports(count);
|
|
286
|
-
|
|
287
|
-
|
|
295
|
+
// Every worker is idle, so peers can ask any of them for states once the level starts.
|
|
296
|
+
requests.forEach((_, index) => Atomics.store(requests, index, 0));
|
|
297
|
+
send({ type: "level", level });
|
|
298
|
+
const messages = await collectLevels(await reported);
|
|
288
299
|
if (isAborted(messages)) {
|
|
289
300
|
terminate();
|
|
290
301
|
return fallBack();
|