gdp-wired 0.53.2 → 0.54.0
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 +7 -4
- package/dist/core/candidate-seeds.d.ts +5 -2
- package/dist/core/candidate-seeds.js +5 -3
- package/dist/core/covering.d.ts +14 -0
- package/dist/core/covering.js +269 -0
- package/dist/core/dsl.d.ts +1 -0
- package/dist/core/dsl.js +89 -3
- package/dist/core/effect.d.ts +1 -1
- package/dist/core/effect.js +1 -1
- package/dist/core/extract-c.d.ts +1 -1
- package/dist/core/extract-c.js +1 -1
- package/dist/core/extract-ex.d.ts +1 -1
- package/dist/core/extract-ex.js +1 -1
- package/dist/core/extract-go.d.ts +1 -1
- package/dist/core/extract-go.js +1 -1
- package/dist/core/extract-jvm.d.ts +1 -1
- package/dist/core/extract-jvm.js +1 -1
- package/dist/core/extract-py.d.ts +1 -1
- package/dist/core/extract-py.js +1 -1
- package/dist/core/extract-rb.d.ts +1 -1
- package/dist/core/extract-rb.js +1 -1
- package/dist/core/extract-rs.d.ts +1 -1
- package/dist/core/extract-rs.js +1 -1
- package/dist/core/extract-sol.d.ts +1 -1
- package/dist/core/extract-sol.js +1 -1
- package/dist/core/extract-swift.d.ts +1 -1
- package/dist/core/extract-swift.js +1 -1
- package/dist/core/extract.d.ts +1 -1
- package/dist/core/extract.js +1 -1
- package/dist/core/infer.js +144 -28
- package/dist/core/minimize.d.ts +32 -0
- package/dist/core/minimize.js +319 -0
- package/dist/core/unguard-seeds.d.ts +7 -0
- package/dist/core/unguard-seeds.js +138 -0
- package/docs/minimize.md +79 -0
- package/package.json +6 -1
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
// Minimal refuting runs. Untrusted, like every search: the answer is a run anyone replays through the rule.
|
|
2
|
+
//
|
|
3
|
+
// `minimizeRun(rule, law, run, opts?)` takes a run that breaks `law` (the requests of a witness: a verdict's `run`, or
|
|
4
|
+
// its trace alone) and returns a run that still breaks it on the ORIGINAL rule and is 1-minimal: with any one request
|
|
5
|
+
// removed it no longer breaks the law.
|
|
6
|
+
//
|
|
7
|
+
// What "breaks" means here (the reading core/watch.ts and core/bmc.ts's `replay` give a run): from the rule's start
|
|
8
|
+
// state at the run's params, the requests are taken in order by the interpreter, and the law is read at the start and
|
|
9
|
+
// after every step (a step law: at every `steps` steps in a row), at the keys core/watch.ts reads a law at. A request
|
|
10
|
+
// the rule refuses (its assumption, a request out of order, a fixed-width failure) is not taken. The run returned
|
|
11
|
+
// holds only requests the rule takes, and its LAST step is the first that breaks the law, so it breaks the law both
|
|
12
|
+
// for a replay that stops at a refused request and for one that skips it; and no run with one request removed breaks
|
|
13
|
+
// it under either reading (a removal is tried with the refused requests skipped, the wider of the two).
|
|
14
|
+
//
|
|
15
|
+
// How: (1) the run is cut at its first breaking step, refused requests dropped; (2) chunks are removed, halving
|
|
16
|
+
// (delta debugging), then single requests until a whole pass removes none: that pass is the 1-minimality argument,
|
|
17
|
+
// each removal replayed and seen not to break the law; (3) while time is left, pairs of requests, and (for a law or a
|
|
18
|
+
// one-step law) a breadth-first search for a shortest breaking subsequence of the run, which is 1-minimal by being
|
|
19
|
+
// shortest. Every candidate adopted was replayed to its breaking step, so the result is valid whenever it returns.
|
|
20
|
+
// A removal replays only from the cut (the states before it are kept), and stops as soon as its state meets the
|
|
21
|
+
// state the kept run had there (then the rest goes as before): a request that changes nothing costs one step.
|
|
22
|
+
//
|
|
23
|
+
// The budget (`ms`, wall time, 2000 by default) stops the work where it is: the run returned still breaks the law,
|
|
24
|
+
// and `minimal` (minimizeRunDetailed) says whether the closing pass finished.
|
|
25
|
+
import { divisorsIn, interpret, evalC } from "./machine.js";
|
|
26
|
+
import { allowed, isStepLaw, watch } from "./watch.js";
|
|
27
|
+
const now = () => performance.now();
|
|
28
|
+
const isPlain = (x) => typeof x === 'object' && x !== null && !(x instanceof Map) && !Array.isArray(x);
|
|
29
|
+
const decoded = (rule, shown) => {
|
|
30
|
+
const vals = Object.values(shown);
|
|
31
|
+
const wire = vals.every(x => typeof x === 'number' || x instanceof Map || isPlain(x) && Object.values(x).every(y => typeof y === 'number'));
|
|
32
|
+
if (!wire) {
|
|
33
|
+
const enc = rule.encodeInput;
|
|
34
|
+
if (typeof enc !== 'function')
|
|
35
|
+
throw new Error('minimizeRun: a request holds a value that is neither a number nor a map of numbers');
|
|
36
|
+
return enc.call(rule, shown);
|
|
37
|
+
}
|
|
38
|
+
return Object.fromEntries(Object.entries(shown).map(([n, x]) => [n, typeof x === 'number' || x instanceof Map ? x : new Map(Object.entries(x).map(([k, v]) => [Number(k), v]))]));
|
|
39
|
+
};
|
|
40
|
+
const sameCells = (a, b) => { if (a.size !== b.size)
|
|
41
|
+
return false; for (const [k, v] of a)
|
|
42
|
+
if (b.get(k) !== v)
|
|
43
|
+
return false; return true; };
|
|
44
|
+
const sameRecord = (a, b) => {
|
|
45
|
+
if (a === b)
|
|
46
|
+
return true;
|
|
47
|
+
const ka = Object.keys(a ?? {}), kb = Object.keys(b ?? {});
|
|
48
|
+
return ka.length === kb.length && ka.every(k => a[k] === b?.[k]);
|
|
49
|
+
};
|
|
50
|
+
const sameState = (a, b) => {
|
|
51
|
+
if (a === b)
|
|
52
|
+
return true;
|
|
53
|
+
if (!sameRecord(a.scalars, b.scalars) || !sameRecord(a.defaults, b.defaults) || a.last !== b.last)
|
|
54
|
+
return false;
|
|
55
|
+
const ma = Object.keys(a.maps);
|
|
56
|
+
if (ma.length !== Object.keys(b.maps).length)
|
|
57
|
+
return false;
|
|
58
|
+
for (const n of ma) {
|
|
59
|
+
const y = b.maps[n];
|
|
60
|
+
if (!y || !sameCells(a.maps[n], y))
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
const ha = a.history, hb = b.history;
|
|
64
|
+
if (!ha !== !hb)
|
|
65
|
+
return false;
|
|
66
|
+
if (ha && hb) {
|
|
67
|
+
if (ha.length !== hb.length)
|
|
68
|
+
return false;
|
|
69
|
+
for (let i = 0; i < ha.length; i++)
|
|
70
|
+
if (ha[i] !== hb[i] && !sameRecord(ha[i], hb[i]))
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
return true;
|
|
74
|
+
};
|
|
75
|
+
const recKey = (r) => (r ? Object.keys(r).sort().map(k => `${k}:${r[k]}`).join(',') : '');
|
|
76
|
+
const cellsKey = (c) => [...c].sort((x, y) => x[0] - y[0]).map(([k, v]) => `${k}:${v}`).join(',');
|
|
77
|
+
const stateKey = (st) => `${recKey(st.scalars)}|${Object.keys(st.maps).sort().map(n => `${n}{${cellsKey(st.maps[n])}}`).join('')}|${recKey(st.defaults)}|${st.last ?? ''}|${st.history ? st.history.map(recKey).join(';') : ''}`;
|
|
78
|
+
const requestKey = (r) => Object.keys(r).sort().map(n => { const x = r[n]; return `${n}=${typeof x === 'number' ? x : `{${cellsKey(x)}}`}`; }).join('&');
|
|
79
|
+
class OutOfTime extends Error {
|
|
80
|
+
}
|
|
81
|
+
export function minimizeRunDetailed(rule, law, run, opts = {}) {
|
|
82
|
+
const t0 = now(), deadline = t0 + Math.max(0, opts.ms ?? 2000);
|
|
83
|
+
let ruleObj = 'machine' in rule && typeof rule.machine === 'object' ? rule : undefined;
|
|
84
|
+
if (ruleObj) {
|
|
85
|
+
const ks = divisorsIn([law]);
|
|
86
|
+
if (ks.size && typeof ruleObj.withDivisors === 'function')
|
|
87
|
+
ruleObj = ruleObj.withDivisors(...ks);
|
|
88
|
+
if (typeof ruleObj.withSumsOf === 'function')
|
|
89
|
+
ruleObj = ruleObj.withSumsOf(law);
|
|
90
|
+
}
|
|
91
|
+
const m = ruleObj ? ruleObj.machine : rule;
|
|
92
|
+
const isArray = Array.isArray(run);
|
|
93
|
+
const params = { ...(isArray ? opts.params ?? {} : run.params) };
|
|
94
|
+
const given = isArray ? run : run.trace;
|
|
95
|
+
const it = interpret(m), k = isStepLaw(law) ? law.steps ?? 1 : 0;
|
|
96
|
+
const wrap = (shown) => (isArray ? shown : { ...run, params: run.params, trace: shown });
|
|
97
|
+
if (m.pre && !evalC(m.pre, params, { scalars: {}, maps: {} }, m))
|
|
98
|
+
throw new Error(`minimizeRun: the params fail ${m.name}'s precondition, so the run is not one of the rule's`);
|
|
99
|
+
const all = given.map(s => decoded(rule, s));
|
|
100
|
+
let replays = 0, steps = 0, bound = opts.bound ?? 1;
|
|
101
|
+
const timed = () => { if (now() > deadline)
|
|
102
|
+
throw new OutOfTime(); };
|
|
103
|
+
// One step, or undefined where the rule refuses the request.
|
|
104
|
+
const take = (st, input) => {
|
|
105
|
+
steps++;
|
|
106
|
+
try {
|
|
107
|
+
return allowed(m, it, params, st, input) ? it.step(params, st, input) : undefined;
|
|
108
|
+
}
|
|
109
|
+
catch (e) {
|
|
110
|
+
if (e instanceof RangeError)
|
|
111
|
+
return undefined;
|
|
112
|
+
throw e;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
// A fresh reading of the law along one run: `start` and `step` say whether it is broken there.
|
|
116
|
+
const reader = () => {
|
|
117
|
+
const on = watch(m, it, law, params, bound);
|
|
118
|
+
return { start: (st) => !on.start(st), step: (before, input, after) => !on.step(before, input, after) };
|
|
119
|
+
};
|
|
120
|
+
// The given run, refused requests skipped, cut at its first breaking step (undefined: it never breaks the law).
|
|
121
|
+
const normal = () => {
|
|
122
|
+
replays++;
|
|
123
|
+
const on = reader(), idx = [];
|
|
124
|
+
let st = it.init(params);
|
|
125
|
+
const states = [st];
|
|
126
|
+
if (on.start(st))
|
|
127
|
+
return { idx, states };
|
|
128
|
+
for (let q = 0; q < all.length; q++) {
|
|
129
|
+
const next = take(st, all[q]);
|
|
130
|
+
if (!next)
|
|
131
|
+
continue;
|
|
132
|
+
idx.push(q);
|
|
133
|
+
states.push(next);
|
|
134
|
+
if (on.step(st, all[q], next))
|
|
135
|
+
return { idx, states };
|
|
136
|
+
st = next;
|
|
137
|
+
}
|
|
138
|
+
return undefined;
|
|
139
|
+
};
|
|
140
|
+
let cur;
|
|
141
|
+
if (opts.bound !== undefined)
|
|
142
|
+
cur = normal();
|
|
143
|
+
else
|
|
144
|
+
for (const b of [1, 3, 8, 24]) {
|
|
145
|
+
bound = b;
|
|
146
|
+
if ((cur = normal()))
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
if (!cur)
|
|
150
|
+
throw new Error(`minimizeRun: the run does not break ${JSON.stringify(law.name)} on ${m.name} (replayed from the start${isArray && !opts.params ? ', with no params given' : ''})`);
|
|
151
|
+
const from = cur.idx.length;
|
|
152
|
+
const done = (kept, minimal) => ({ run: wrap(kept.idx.map(q => given[q])), minimal, from: given.length, to: kept.idx.length, replays, steps, ms: now() - t0 });
|
|
153
|
+
// The kept run with the places `drop` (sorted, of the kept run) removed, if it still breaks the law: replayed from
|
|
154
|
+
// the first place dropped, refused requests skipped, cut at the first breaking step.
|
|
155
|
+
const without = (kept, drop) => {
|
|
156
|
+
replays++;
|
|
157
|
+
const n = kept.idx.length, first = drop[0], last = drop[drop.length - 1];
|
|
158
|
+
// (a k-step law reads k steps in a row: the k-1 steps before the cut are read again, into a fresh window)
|
|
159
|
+
const s = k > 1 ? Math.max(0, first - (k - 1)) : first;
|
|
160
|
+
const on = reader();
|
|
161
|
+
let st = kept.states[s], d = 0;
|
|
162
|
+
const idx = kept.idx.slice(0, s), states = kept.states.slice(0, s + 1);
|
|
163
|
+
for (let p = s; p < n; p++) {
|
|
164
|
+
if ((p & 63) === 0)
|
|
165
|
+
timed();
|
|
166
|
+
if (d < drop.length && drop[d] === p) {
|
|
167
|
+
d++;
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
const input = all[kept.idx[p]], next = take(st, input);
|
|
171
|
+
if (next) {
|
|
172
|
+
idx.push(kept.idx[p]);
|
|
173
|
+
states.push(next);
|
|
174
|
+
if (on.step(st, input, next))
|
|
175
|
+
return { idx, states };
|
|
176
|
+
st = next;
|
|
177
|
+
}
|
|
178
|
+
// The state the kept run had here: from here on the two runs are one, and the kept run's last step breaks the law.
|
|
179
|
+
if (k <= 1 && p >= last && p + 1 < n && sameState(st, kept.states[p + 1])) {
|
|
180
|
+
for (let q = p + 1; q < n; q++) {
|
|
181
|
+
idx.push(kept.idx[q]);
|
|
182
|
+
states.push(kept.states[q + 1]);
|
|
183
|
+
}
|
|
184
|
+
return { idx, states };
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return undefined;
|
|
188
|
+
};
|
|
189
|
+
const range = (i, j) => Array.from({ length: j - i }, (_, x) => i + x);
|
|
190
|
+
let minimal = false;
|
|
191
|
+
try {
|
|
192
|
+
// Chunks, halving; then single requests until a whole pass removes none.
|
|
193
|
+
for (let c = Math.ceil(cur.idx.length / 2); c > 1; c = Math.ceil(c / 2)) {
|
|
194
|
+
for (let i = 0; i < cur.idx.length;) {
|
|
195
|
+
timed();
|
|
196
|
+
const less = without(cur, range(i, Math.min(i + c, cur.idx.length)));
|
|
197
|
+
if (less)
|
|
198
|
+
cur = less;
|
|
199
|
+
else
|
|
200
|
+
i += c;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const singles = () => {
|
|
204
|
+
for (let changed = true; changed;) {
|
|
205
|
+
changed = false;
|
|
206
|
+
// (from the end: a later request's removal replays less)
|
|
207
|
+
for (let i = cur.idx.length - 1; i >= 0; i--) {
|
|
208
|
+
timed();
|
|
209
|
+
const less = without(cur, [i]);
|
|
210
|
+
if (less) {
|
|
211
|
+
cur = less;
|
|
212
|
+
changed = true;
|
|
213
|
+
i = Math.min(i, cur.idx.length);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
singles();
|
|
219
|
+
minimal = true;
|
|
220
|
+
// Shorter still, while time is left. Each result here is 1-minimal again before it replaces the kept run.
|
|
221
|
+
let best = cur;
|
|
222
|
+
try {
|
|
223
|
+
// Pairs of requests (a request and the one that undoes it), then single requests again.
|
|
224
|
+
for (let again = true; again && cur.idx.length <= 64;) {
|
|
225
|
+
again = false;
|
|
226
|
+
search: for (let i = 0; i < cur.idx.length; i++)
|
|
227
|
+
for (let j = i + 1; j < cur.idx.length; j++) {
|
|
228
|
+
timed();
|
|
229
|
+
const less = without(cur, [i, j]);
|
|
230
|
+
if (less) {
|
|
231
|
+
cur = less;
|
|
232
|
+
singles();
|
|
233
|
+
best = cur;
|
|
234
|
+
again = true;
|
|
235
|
+
break search;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
catch (e) {
|
|
241
|
+
if (!(e instanceof OutOfTime))
|
|
242
|
+
throw e;
|
|
243
|
+
return done(best, true);
|
|
244
|
+
}
|
|
245
|
+
// A shortest breaking subsequence of the given run (breadth first: a state reached at an earlier place of the run
|
|
246
|
+
// stands for the same state reached later, since every request after the later place is after the earlier one too).
|
|
247
|
+
if (opts.shortest !== false && k <= 1 && cur.idx.length > 1) {
|
|
248
|
+
const shorter = shortest(cur.idx.length);
|
|
249
|
+
if (shorter)
|
|
250
|
+
cur = shorter;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
catch (e) {
|
|
254
|
+
if (!(e instanceof OutOfTime))
|
|
255
|
+
throw e;
|
|
256
|
+
}
|
|
257
|
+
return done(cur, minimal);
|
|
258
|
+
function shortest(below) {
|
|
259
|
+
// The requests by what they are: from a place, each distinct request is tried at its first place after it.
|
|
260
|
+
const classes = new Map();
|
|
261
|
+
const pool = from <= 4096 ? range(0, all.length) : cur.idx;
|
|
262
|
+
for (const q of pool) {
|
|
263
|
+
const key = requestKey(all[q]);
|
|
264
|
+
const at = classes.get(key);
|
|
265
|
+
if (at)
|
|
266
|
+
at.push(q);
|
|
267
|
+
else
|
|
268
|
+
classes.set(key, [q]);
|
|
269
|
+
}
|
|
270
|
+
if (classes.size > 256)
|
|
271
|
+
return undefined;
|
|
272
|
+
const places = [...classes.values()];
|
|
273
|
+
const on = reader(), start = { st: it.init(params), at: -1 };
|
|
274
|
+
const seen = new Map([[stateKey(start.st), -1]]);
|
|
275
|
+
let level = [start], nodes = 0;
|
|
276
|
+
// (a share of the budget: the kept run is already 1-minimal)
|
|
277
|
+
const until = Math.min(deadline, now() + Math.max(50, (deadline - t0) / 4));
|
|
278
|
+
for (let depth = 1; depth < below && level.length; depth++) {
|
|
279
|
+
const next = [];
|
|
280
|
+
for (const node of level) {
|
|
281
|
+
for (const at of places) {
|
|
282
|
+
if ((++nodes & 31) === 0 && now() > until || nodes > 50_000)
|
|
283
|
+
return undefined;
|
|
284
|
+
let q = -1;
|
|
285
|
+
for (const x of at)
|
|
286
|
+
if (x > node.at) {
|
|
287
|
+
q = x;
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
if (q < 0)
|
|
291
|
+
continue;
|
|
292
|
+
const after = take(node.st, all[q]);
|
|
293
|
+
if (!after)
|
|
294
|
+
continue;
|
|
295
|
+
if (on.step(node.st, all[q], after)) {
|
|
296
|
+
const idx = [q], states = [after];
|
|
297
|
+
for (let u = node; u; u = u.up) {
|
|
298
|
+
states.push(u.st);
|
|
299
|
+
if (u.at >= 0)
|
|
300
|
+
idx.push(u.at);
|
|
301
|
+
}
|
|
302
|
+
return { idx: idx.reverse(), states: states.reverse() };
|
|
303
|
+
}
|
|
304
|
+
const key = stateKey(after), had = seen.get(key);
|
|
305
|
+
if (had !== undefined && had <= q)
|
|
306
|
+
continue;
|
|
307
|
+
seen.set(key, q);
|
|
308
|
+
next.push({ st: after, at: q, up: node });
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
level = next;
|
|
312
|
+
}
|
|
313
|
+
return undefined;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
// The run, minimized (see the head of this file). Throws if the run given does not break the law.
|
|
317
|
+
export function minimizeRun(rule, law, run, opts = {}) {
|
|
318
|
+
return minimizeRunDetailed(rule, law, run, opts).run;
|
|
319
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Law, type Machine, type Rand } from './machine.js';
|
|
2
|
+
export type UnguardDraws = {
|
|
3
|
+
params: (rand: Rand, bound: number) => Record<string, number>;
|
|
4
|
+
input: (rand: Rand, bound: number, params: Record<string, number>) => Record<string, number | ReadonlyMap<number, number>>;
|
|
5
|
+
};
|
|
6
|
+
export declare function proposeUnguardedSeeds(m: Machine, law: Law): Law[];
|
|
7
|
+
export declare function filterUnguardedSeeds(m: Machine, cands: Law[], draws: UnguardDraws): Law[];
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// Untrusted unguarded per-key invariant seeds: when a universally quantified law has the shape
|
|
2
|
+
// ∀ k. guard(k) ⇒ (P₁(k) ∧ P₂(k) ∧ ...)
|
|
3
|
+
// where a conjunct P_i(k) is an atomic comparison relating at least two distinct maps at k and
|
|
4
|
+
// reading none of the guard's state variables or maps, the guard often acts as a latch (e.g.
|
|
5
|
+
// recording a majority or leader) that starts false so the induction hypothesis is silent at the
|
|
6
|
+
// step the latch flips. Lifting P_i(k) to an unguarded candidate ∀ k. P_i(k), filtering it on
|
|
7
|
+
// short reachable runs, and handing surviving seeds to ordinary kernel-checked proveLaws closes
|
|
8
|
+
// such laws without modifying the kernel or adding ghost state.
|
|
9
|
+
import { evalC, interpret, seeded, walk, } from "./machine.js";
|
|
10
|
+
import { allowed, watch } from "./watch.js";
|
|
11
|
+
import { checkWorkEndpoint } from "./wall.js";
|
|
12
|
+
const flatAnd = (c) => (c.op === 'and' ? c.xs.flatMap(flatAnd) : [c]);
|
|
13
|
+
const flatOr = (c) => (c.op === 'or' ? c.xs.flatMap(flatOr) : [c]);
|
|
14
|
+
function namesIn(t) {
|
|
15
|
+
const out = new Set();
|
|
16
|
+
walk(t, o => { if (o.op === 'var' && typeof o.name === 'string')
|
|
17
|
+
out.add(o.name); });
|
|
18
|
+
return out;
|
|
19
|
+
}
|
|
20
|
+
function mapsIn(t) {
|
|
21
|
+
const out = new Set();
|
|
22
|
+
walk(t, o => { if (o.op === 'get' && typeof o.map === 'string')
|
|
23
|
+
out.add(o.map); });
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
function unwrapImplies(c) {
|
|
27
|
+
const guards = [];
|
|
28
|
+
let cur = c;
|
|
29
|
+
while (cur.op === 'or') {
|
|
30
|
+
const parts = flatOr(cur);
|
|
31
|
+
const negs = parts.filter((p) => p.op === 'not');
|
|
32
|
+
const pos = parts.filter(p => p.op !== 'not');
|
|
33
|
+
if (pos.length !== 1 || !negs.length)
|
|
34
|
+
break;
|
|
35
|
+
for (const g of negs)
|
|
36
|
+
guards.push(g.x);
|
|
37
|
+
cur = pos[0];
|
|
38
|
+
}
|
|
39
|
+
return { guards, body: cur };
|
|
40
|
+
}
|
|
41
|
+
// Propose unguarded per-key multi-map accounting helpers purely from the syntax of `(m, law)`.
|
|
42
|
+
export function proposeUnguardedSeeds(m, law) {
|
|
43
|
+
if (!law.forall || law.keys?.length || law.some?.length || law.ex || law.finite !== undefined)
|
|
44
|
+
return [];
|
|
45
|
+
if (m.history || m.primitiveFailure || Object.keys(m.counts ?? {}).length)
|
|
46
|
+
return [];
|
|
47
|
+
const k = law.forall;
|
|
48
|
+
const { guards, body } = unwrapImplies(law.holds);
|
|
49
|
+
if (!guards.length)
|
|
50
|
+
return [];
|
|
51
|
+
const stateVars = new Set(Object.keys(m.state));
|
|
52
|
+
const guardStateVars = new Set(guards.flatMap(g => [...namesIn(g)]).filter(v => stateVars.has(v)));
|
|
53
|
+
const guardMaps = new Set(guards.flatMap(g => [...mapsIn(g)]));
|
|
54
|
+
const cjs = flatAnd(body);
|
|
55
|
+
// Require a scalar state latch, or a map latch gating a multi-conjunct body.
|
|
56
|
+
if (!guardStateVars.size && !(guardMaps.size && cjs.length >= 2))
|
|
57
|
+
return [];
|
|
58
|
+
const used = JSON.stringify([m, law]);
|
|
59
|
+
let stem = 'ung$';
|
|
60
|
+
while (used.includes(stem))
|
|
61
|
+
stem += 'u';
|
|
62
|
+
const out = [];
|
|
63
|
+
const seen = new Set();
|
|
64
|
+
for (let i = 0; i < cjs.length && out.length < 8; i++) {
|
|
65
|
+
const cj = cjs[i];
|
|
66
|
+
if (cj.op !== '=' && cj.op !== '<=' && cj.op !== '<')
|
|
67
|
+
continue;
|
|
68
|
+
const cjMaps = mapsIn(cj);
|
|
69
|
+
// Require at least two distinct base maps (ignoring signed-integer `$neg` twin splits).
|
|
70
|
+
const baseMaps = new Set([...cjMaps].map(mp => mp.replace(/\$neg$/, '')));
|
|
71
|
+
if (baseMaps.size < 2)
|
|
72
|
+
continue;
|
|
73
|
+
const cjVars = namesIn(cj);
|
|
74
|
+
if (!cjVars.has(k))
|
|
75
|
+
continue;
|
|
76
|
+
if ([...guardStateVars].some(v => cjVars.has(v)))
|
|
77
|
+
continue;
|
|
78
|
+
if ([...guardMaps].some(mp => cjMaps.has(mp)))
|
|
79
|
+
continue;
|
|
80
|
+
const sig = JSON.stringify(cj);
|
|
81
|
+
if (seen.has(sig))
|
|
82
|
+
continue;
|
|
83
|
+
seen.add(sig);
|
|
84
|
+
out.push({ name: `${stem}${i}`, forall: k, holds: cj });
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
// Fast untrusted sample filter: drop candidates refuted on short reachable runs (~1 ms).
|
|
89
|
+
export function filterUnguardedSeeds(m, cands, draws) {
|
|
90
|
+
if (!cands.length)
|
|
91
|
+
return [];
|
|
92
|
+
checkWorkEndpoint('unguarded seed filter');
|
|
93
|
+
let runtime;
|
|
94
|
+
try {
|
|
95
|
+
runtime = interpret(m);
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return [];
|
|
99
|
+
}
|
|
100
|
+
const bound = 4;
|
|
101
|
+
const rand = seeded(59);
|
|
102
|
+
const states = [];
|
|
103
|
+
for (let trial = 0; trial < 20; trial++) {
|
|
104
|
+
checkWorkEndpoint('unguarded seed trial');
|
|
105
|
+
let ps, st;
|
|
106
|
+
try {
|
|
107
|
+
ps = draws.params(rand, bound);
|
|
108
|
+
if (m.pre && !evalC(m.pre, ps, { scalars: {}, maps: {} }, m))
|
|
109
|
+
continue;
|
|
110
|
+
st = runtime.init(ps);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
states.push({ ps, st });
|
|
116
|
+
for (let d = 0, tries = 0; d < 12 && tries < 36; tries++) {
|
|
117
|
+
checkWorkEndpoint('unguarded seed step');
|
|
118
|
+
try {
|
|
119
|
+
const input = draws.input(rand, bound, ps);
|
|
120
|
+
if (!allowed(m, runtime, ps, st, input))
|
|
121
|
+
continue;
|
|
122
|
+
st = runtime.step(ps, st, input);
|
|
123
|
+
states.push({ ps, st });
|
|
124
|
+
d++;
|
|
125
|
+
}
|
|
126
|
+
catch { /* refused */ }
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return cands.filter(h => states.every(({ ps, st }) => {
|
|
130
|
+
checkWorkEndpoint('unguarded seed check');
|
|
131
|
+
try {
|
|
132
|
+
return watch(m, runtime, h, ps, bound).start(st);
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
}));
|
|
138
|
+
}
|
package/docs/minimize.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Minimizing refuting runs
|
|
2
|
+
|
|
3
|
+
`minimizeRun(rule, law, run, opts?)` shrinks a concrete counterexample run until
|
|
4
|
+
no single request can be removed while still breaking `law` on the original
|
|
5
|
+
`rule`. It is untrusted, like every search: every candidate is replayed from the
|
|
6
|
+
start through the interpreter on the original rule, and the result is a
|
|
7
|
+
subsequence of the caller's own requests that anyone can replay directly.
|
|
8
|
+
|
|
9
|
+
## Entry points
|
|
10
|
+
|
|
11
|
+
`minimizeRun` and `minimizeRunDetailed` are exported from:
|
|
12
|
+
|
|
13
|
+
- `gdp-wired` (the root entry point)
|
|
14
|
+
- `gdp-wired/minimize` (the standalone subpath)
|
|
15
|
+
- every language frontend subpath (`gdp-wired/extract`, `gdp-wired/effect`,
|
|
16
|
+
`gdp-wired/extract-py`, `gdp-wired/extract-rb`, `gdp-wired/extract-ex`,
|
|
17
|
+
`gdp-wired/extract-rs`, `gdp-wired/extract-swift`, `gdp-wired/extract-go`,
|
|
18
|
+
`gdp-wired/extract-c`, `gdp-wired/extract-cpp`, `gdp-wired/extract-java`,
|
|
19
|
+
`gdp-wired/extract-kotlin`, `gdp-wired/extract-sol`)
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import {min, minimizeRun, minimizeRunDetailed, nat, proveRule, rule, when} from 'gdp-wired'
|
|
23
|
+
|
|
24
|
+
const uncapped = rule('uncapped')
|
|
25
|
+
.params({b: nat, m: nat})
|
|
26
|
+
.inputs({c: nat})
|
|
27
|
+
.state({s: 0})
|
|
28
|
+
.step(({b, c, s}) => ({s: when(s.lt(b), s.plus(c), s)}))
|
|
29
|
+
|
|
30
|
+
const bounded = uncapped.law('spend stays within b + m', ({s, b, m}) => s.atMost(b.plus(m)))
|
|
31
|
+
const v = proveRule(uncapped, [bounded])
|
|
32
|
+
|
|
33
|
+
if (v.run) {
|
|
34
|
+
const short = minimizeRun(uncapped, bounded, v.run)
|
|
35
|
+
// Or with replay statistics and minimality status:
|
|
36
|
+
const detailed = minimizeRunDetailed(uncapped, bounded, v.run, {ms: 2000})
|
|
37
|
+
// detailed: {run, minimal, from, to, replays, steps, ms}
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Inputs and options
|
|
42
|
+
|
|
43
|
+
- `rule`: a `Rule` builder or a plain `Machine`.
|
|
44
|
+
- `law`: a `Law` or `StepLaw`.
|
|
45
|
+
- `run`: either a `Verdict.run` (`{params, trace}`) or a plain array of requests
|
|
46
|
+
(pass `{params}` in `opts` when the rule has parameters). Requests may be wire
|
|
47
|
+
objects (`Verdict.run.trace`), `Map`-bearing requests, or raw rule inputs
|
|
48
|
+
encoded via `rule.encodeInput`. The return value preserves the shape given and
|
|
49
|
+
keeps the caller's own request objects in their original order.
|
|
50
|
+
- `opts.ms`: wall-time budget in milliseconds (default `2000`).
|
|
51
|
+
- `opts.params`: parameters for a run passed as a plain request array.
|
|
52
|
+
- `opts.bound`: key bound for `forall` laws (default: the smallest of `1`, `3`,
|
|
53
|
+
`8`, `24` at which the given run breaks `law`).
|
|
54
|
+
- `opts.shortest`: whether to run a bounded breadth-first pass for a shortest
|
|
55
|
+
breaking subsequence after 1-minimality is reached (default `true`, for
|
|
56
|
+
invariants and single-step laws).
|
|
57
|
+
|
|
58
|
+
`minimizeRun` throws if the parameters fail the rule's precondition or if the
|
|
59
|
+
given run does not break `law` when replayed from the start state.
|
|
60
|
+
|
|
61
|
+
## Guarantees and scope
|
|
62
|
+
|
|
63
|
+
- **Validity.** Every adopted candidate is replayed through `interpret` on the
|
|
64
|
+
original rule (`core/watch.ts`'s `watch` reader). The returned run contains
|
|
65
|
+
only requests admitted by the rule, with unchanged parameters, and ends at its
|
|
66
|
+
first breaking step, so it breaks `law` under both strict (stop on refused
|
|
67
|
+
request) and skip-refused replay.
|
|
68
|
+
- **1-minimality.** After cutting at the first break and removing chunks by
|
|
69
|
+
halving, single-request removals repeat to a fixpoint under the wider
|
|
70
|
+
skip-refused reading, followed by pair removals and (when enabled) bounded
|
|
71
|
+
shortest-subsequence search.
|
|
72
|
+
- **Budget exhaustion.** If `opts.ms` expires before the closing single-removal
|
|
73
|
+
pass finishes, `minimizeRun` still returns a valid breaking subsequence;
|
|
74
|
+
`minimizeRunDetailed` sets `minimal: false` to report that 1-minimality was
|
|
75
|
+
not confirmed within the budget.
|
|
76
|
+
- **What is not minimized.** Parameters and individual request field values are
|
|
77
|
+
kept unchanged; only requests are dropped. Multi-step (`steps > 1`) laws skip
|
|
78
|
+
the shortest-subsequence search and state-equivalence early stop while still
|
|
79
|
+
shrinking via chunk and single-request removals.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gdp-wired",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.54.0",
|
|
4
4
|
"description": "An automatic prover for stateful rules (budgets, caps, windows, workflows, policies): laws proven with no proof to write, checked again in Lean, counterexamples found on the GPU, the rule shipped as code",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"verification",
|
|
@@ -36,6 +36,10 @@
|
|
|
36
36
|
"types": "./dist/core/mutate.d.ts",
|
|
37
37
|
"default": "./dist/core/mutate.js"
|
|
38
38
|
},
|
|
39
|
+
"./minimize": {
|
|
40
|
+
"types": "./dist/core/minimize.d.ts",
|
|
41
|
+
"default": "./dist/core/minimize.js"
|
|
42
|
+
},
|
|
39
43
|
"./machine": {
|
|
40
44
|
"types": "./dist/core/machine.d.ts",
|
|
41
45
|
"default": "./dist/core/machine.js"
|
|
@@ -193,6 +197,7 @@
|
|
|
193
197
|
"LICENSE",
|
|
194
198
|
"docs/agent-consult.md",
|
|
195
199
|
"docs/lean-aggregate.md",
|
|
200
|
+
"docs/minimize.md",
|
|
196
201
|
"docs/agent-consult-workspace-check.md",
|
|
197
202
|
"examples/agent-consult.json"
|
|
198
203
|
],
|