@parcha/agentrun-dsl 0.1.0-beta.26 → 0.1.0-beta.260
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/NOTICE +2 -0
- package/README.md +22 -17
- package/dist/author-references.d.ts +8 -0
- package/dist/author-references.js +8 -0
- package/dist/author.d.ts +0 -2
- package/dist/author.js +13 -8
- package/dist/authoring.js +3 -2
- package/dist/cli.js +13 -12
- package/dist/code-exec.d.ts +19 -0
- package/dist/code-exec.js +54 -5
- package/dist/decision-provider.d.ts +166 -0
- package/dist/decision-provider.js +238 -0
- package/dist/decision-receipts.d.ts +64 -3
- package/dist/decision-receipts.js +50 -8
- package/dist/dry-run.d.ts +31 -1
- package/dist/dry-run.js +120 -14
- package/dist/edit.d.ts +158 -0
- package/dist/edit.js +1486 -0
- package/dist/evidence.d.ts +19 -0
- package/dist/evidence.js +63 -0
- package/dist/graph.d.ts +17 -0
- package/dist/graph.js +71 -6
- package/dist/index.d.ts +18 -12
- package/dist/index.js +17 -10
- package/dist/inspection.d.ts +5 -0
- package/dist/inspection.js +14 -1
- package/dist/pattern-screen.d.ts +14 -0
- package/dist/pattern-screen.js +451 -0
- package/dist/pointer-warning.d.ts +14 -0
- package/dist/pointer-warning.js +22 -0
- package/dist/predicates.d.ts +32 -2
- package/dist/predicates.js +66 -6
- package/dist/record.d.ts +4 -0
- package/dist/record.js +6 -0
- package/dist/recovery/binding.js +2 -1
- package/dist/recovery/call-key.d.ts +0 -4
- package/dist/recovery/call-key.js +0 -7
- package/dist/recovery/driver.d.ts +43 -20
- package/dist/recovery/driver.js +140 -41
- package/dist/recovery/frozen-snapshot.d.ts +30 -1
- package/dist/recovery/frozen-snapshot.js +26 -8
- package/dist/recovery/handoff-receipts.d.ts +0 -35
- package/dist/recovery/handoff-receipts.js +2 -44
- package/dist/recovery/index.d.ts +4 -8
- package/dist/recovery/index.js +4 -7
- package/dist/recovery/item-state.d.ts +2 -1
- package/dist/recovery/item-state.js +7 -13
- package/dist/recovery/with-recovery.d.ts +9 -3
- package/dist/recovery/with-recovery.js +72 -15
- package/dist/submit-envelope.d.ts +9 -0
- package/dist/submit-envelope.js +44 -0
- package/dist/system-one.d.ts +21 -2
- package/dist/system-one.js +96 -18
- package/dist/testing/decision-provider.d.ts +32 -0
- package/dist/testing/decision-provider.js +183 -0
- package/dist/testing/index.d.ts +1 -0
- package/dist/testing/index.js +2 -0
- package/dist/vocabulary.d.ts +16 -3
- package/dist/vocabulary.js +23 -8
- package/dist/workflow-shape.js +4 -2
- package/dist/workflow.d.ts +144 -18
- package/dist/workflow.js +871 -247
- package/dist/zod-json-contract.js +6 -8
- package/package.json +16 -4
- package/schema/workflow.schema.json +352 -1
- package/skills/author/SKILL.md +4 -1
- package/skills/author/references/jev-decisions.md +50 -5
- package/skills/author/references/language.md +16 -11
- package/dist/recovery/effect-failure.d.ts +0 -34
- package/dist/recovery/effect-failure.js +0 -62
- package/dist/recovery/handoff-block.d.ts +0 -36
- package/dist/recovery/handoff-block.js +0 -148
- package/dist/recovery/handoff-digest.d.ts +0 -58
- package/dist/recovery/handoff-digest.js +0 -195
- package/dist/recovery/handoff-files.d.ts +0 -54
- package/dist/recovery/handoff-files.js +0 -162
- package/dist/recovery/handoff-redact.d.ts +0 -12
- package/dist/recovery/handoff-redact.js +0 -44
- package/dist/recovery/handoff.d.ts +0 -44
- package/dist/recovery/handoff.js +0 -46
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
// A JSON-schema `pattern` read before any string is tested against it. Schema validation runs each pattern in
|
|
2
|
+
// JavaScript's backtracking engine (typebox compiles it as `new RegExp(pattern, "u")`), where some shapes take time
|
|
3
|
+
// exponential in the string's length: `^(a+)+$` on thirty a's and a "!" holds the thread for half a minute, and every
|
|
4
|
+
// character more doubles it. No engine here runs in linear time, so the shapes that backtrack that way are refused before
|
|
5
|
+
// any string is tested:
|
|
6
|
+
//
|
|
7
|
+
// - a repeated part that repeats something inside it, or holds an optional part, when one pass of the outer repeat can
|
|
8
|
+
// match a run of what the inner one matches (`(a+)+`, `(a?a?)+`, `(\w+\s?)*`, `(.*,){20}`): the run splits between
|
|
9
|
+
// the passes in exponentially many ways. A repeat whose every pass must also match something the inner one can't
|
|
10
|
+
// (`(-[a-z0-9]+)*`, `(?:[.-][a-z0-9]+)*`) is unambiguous and passes.
|
|
11
|
+
// - an alternation repeated without end (or more than 16 times) whose branches can start with the same character
|
|
12
|
+
// (`(a|ab)*`, `(\w|\d)+`).
|
|
13
|
+
// - a backreference (`(a*)\1`): the engine has no bound for it at all.
|
|
14
|
+
//
|
|
15
|
+
// It reads the pattern's own syntax (the `u` flag's), so it never runs it. A pattern it can't read, or one past
|
|
16
|
+
// PATTERN_SCREEN_LIMITS, is refused too: a pattern is refused unless it is read and found safe. Polynomial shapes
|
|
17
|
+
// (`\s*\s*\s*$`) are not refused: their time grows with a power of the length, not exponentially.
|
|
18
|
+
/** The longest pattern read (characters), and the most quantifiers in one: a schema pattern is a few dozen of each. */
|
|
19
|
+
export const PATTERN_SCREEN_LIMITS = { chars: 4_096, quantifiers: 128 };
|
|
20
|
+
const TOP = 0x10ffff;
|
|
21
|
+
function normal(ranges) {
|
|
22
|
+
const sorted = [...ranges].sort((a, b) => a[0] - b[0]);
|
|
23
|
+
const out = [];
|
|
24
|
+
for (const [lo, hi] of sorted) {
|
|
25
|
+
const last = out[out.length - 1];
|
|
26
|
+
if (last && lo <= last[1] + 1)
|
|
27
|
+
last[1] = Math.max(last[1], hi);
|
|
28
|
+
else
|
|
29
|
+
out.push([lo, hi]);
|
|
30
|
+
}
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
const union = (...sets) => normal(sets.flat());
|
|
34
|
+
function complement(set) {
|
|
35
|
+
const out = [];
|
|
36
|
+
let next = 0;
|
|
37
|
+
for (const [lo, hi] of set) {
|
|
38
|
+
if (lo > next)
|
|
39
|
+
out.push([next, lo - 1]);
|
|
40
|
+
next = hi + 1;
|
|
41
|
+
}
|
|
42
|
+
if (next <= TOP)
|
|
43
|
+
out.push([next, TOP]);
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
function meets(a, b) {
|
|
47
|
+
for (const [lo, hi] of a)
|
|
48
|
+
for (const [blo, bhi] of b)
|
|
49
|
+
if (lo <= bhi && blo <= hi)
|
|
50
|
+
return true;
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
const one = (code) => [[code, code]];
|
|
54
|
+
const DIGIT = [[0x30, 0x39]];
|
|
55
|
+
const WORD = normal([[0x30, 0x39], [0x41, 0x5a], [0x5f, 0x5f], [0x61, 0x7a]]);
|
|
56
|
+
const SPACE = normal([[0x09, 0x0d], [0x20, 0x20], [0xa0, 0xa0], [0x1680, 0x1680], [0x2000, 0x200a], [0x2028, 0x2029], [0x202f, 0x202f], [0x205f, 0x205f], [0x3000, 0x3000], [0xfeff, 0xfeff]]);
|
|
57
|
+
const ANY = [[0, TOP]];
|
|
58
|
+
const DOT = complement(normal([[0x0a, 0x0a], [0x0d, 0x0d], [0x2028, 0x2029]]));
|
|
59
|
+
const SINGLE = { t: 0x09, n: 0x0a, v: 0x0b, f: 0x0c, r: 0x0d };
|
|
60
|
+
const CLASS_ESCAPES = { d: DIGIT, D: complement(DIGIT), w: WORD, W: complement(WORD), s: SPACE, S: complement(SPACE) };
|
|
61
|
+
/** The characters a `u`-flag pattern lets an escape stand for (and `-` in a class). */
|
|
62
|
+
const SYNTAX = new Set([..."^$\\.*+?()[]{}|/"]);
|
|
63
|
+
class Unreadable extends Error {
|
|
64
|
+
}
|
|
65
|
+
/** A `u`-flag pattern's parse: alternatives, sequences, repeats, character sets, anchors, lookarounds, references. */
|
|
66
|
+
class Parser {
|
|
67
|
+
source;
|
|
68
|
+
i = 0;
|
|
69
|
+
quantifiers = 0;
|
|
70
|
+
cps;
|
|
71
|
+
constructor(source) {
|
|
72
|
+
this.source = source;
|
|
73
|
+
this.cps = [...source].map((c) => c.codePointAt(0));
|
|
74
|
+
}
|
|
75
|
+
get at() {
|
|
76
|
+
return this.i < this.cps.length ? String.fromCodePoint(this.cps[this.i]) : undefined;
|
|
77
|
+
}
|
|
78
|
+
take() {
|
|
79
|
+
const c = this.at;
|
|
80
|
+
if (c === undefined)
|
|
81
|
+
throw new Unreadable("the pattern ends early");
|
|
82
|
+
this.i++;
|
|
83
|
+
return c;
|
|
84
|
+
}
|
|
85
|
+
eat(text) {
|
|
86
|
+
const there = this.cps.slice(this.i, this.i + text.length).map((c) => String.fromCodePoint(c)).join("");
|
|
87
|
+
if (there !== text)
|
|
88
|
+
return false;
|
|
89
|
+
this.i += text.length;
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
whole() {
|
|
93
|
+
const re = this.alt();
|
|
94
|
+
if (this.i < this.cps.length)
|
|
95
|
+
throw new Unreadable(`unexpected "${this.at}"`);
|
|
96
|
+
return re;
|
|
97
|
+
}
|
|
98
|
+
alt() {
|
|
99
|
+
const alts = [this.seq()];
|
|
100
|
+
while (this.eat("|"))
|
|
101
|
+
alts.push(this.seq());
|
|
102
|
+
return alts.length === 1 ? alts[0] : { k: "alt", alts };
|
|
103
|
+
}
|
|
104
|
+
seq() {
|
|
105
|
+
const items = [];
|
|
106
|
+
while (this.at !== undefined && this.at !== "|" && this.at !== ")") {
|
|
107
|
+
const start = this.i;
|
|
108
|
+
items.push(this.quantified(this.atom(), start));
|
|
109
|
+
}
|
|
110
|
+
return items.length === 1 ? items[0] : { k: "seq", items };
|
|
111
|
+
}
|
|
112
|
+
quantified(atom, start) {
|
|
113
|
+
let min;
|
|
114
|
+
let max;
|
|
115
|
+
if (this.eat("*"))
|
|
116
|
+
[min, max] = [0, Infinity];
|
|
117
|
+
else if (this.eat("+"))
|
|
118
|
+
[min, max] = [1, Infinity];
|
|
119
|
+
else if (this.eat("?"))
|
|
120
|
+
[min, max] = [0, 1];
|
|
121
|
+
else if (this.at === "{") {
|
|
122
|
+
const match = /^\{(\d+)(,(\d*))?\}/.exec(this.cps.slice(this.i).map((c) => String.fromCodePoint(c)).join(""));
|
|
123
|
+
if (!match)
|
|
124
|
+
throw new Unreadable("a { that is no quantifier");
|
|
125
|
+
this.i += match[0].length;
|
|
126
|
+
min = Number(match[1]);
|
|
127
|
+
max = match[2] === undefined ? min : match[3] === "" ? Infinity : Number(match[3]);
|
|
128
|
+
}
|
|
129
|
+
else
|
|
130
|
+
return atom;
|
|
131
|
+
this.eat("?");
|
|
132
|
+
if (++this.quantifiers > PATTERN_SCREEN_LIMITS.quantifiers)
|
|
133
|
+
throw new Unreadable(`more than ${PATTERN_SCREEN_LIMITS.quantifiers} quantifiers`);
|
|
134
|
+
const text = this.cps.slice(start, this.i).map((c) => String.fromCodePoint(c)).join("");
|
|
135
|
+
return { k: "rep", body: atom, min, max, text };
|
|
136
|
+
}
|
|
137
|
+
group() {
|
|
138
|
+
const body = this.alt();
|
|
139
|
+
if (!this.eat(")"))
|
|
140
|
+
throw new Unreadable("a group that does not close");
|
|
141
|
+
return body;
|
|
142
|
+
}
|
|
143
|
+
atom() {
|
|
144
|
+
const c = this.take();
|
|
145
|
+
if (c === "(") {
|
|
146
|
+
if (this.eat("?:"))
|
|
147
|
+
return this.group();
|
|
148
|
+
if (this.eat("?=") || this.eat("?!") || this.eat("?<=") || this.eat("?<!"))
|
|
149
|
+
return { k: "zero", body: this.group() };
|
|
150
|
+
if (this.eat("?<")) {
|
|
151
|
+
while (this.at !== ">")
|
|
152
|
+
this.take();
|
|
153
|
+
this.take();
|
|
154
|
+
return this.group();
|
|
155
|
+
}
|
|
156
|
+
if (this.at === "?")
|
|
157
|
+
throw new Unreadable("an unknown group");
|
|
158
|
+
return this.group();
|
|
159
|
+
}
|
|
160
|
+
if (c === "^" || c === "$")
|
|
161
|
+
return { k: "zero" };
|
|
162
|
+
if (c === ".")
|
|
163
|
+
return { k: "set", set: DOT };
|
|
164
|
+
if (c === "[")
|
|
165
|
+
return { k: "set", set: this.cls() };
|
|
166
|
+
if (c === "\\")
|
|
167
|
+
return this.escape(false);
|
|
168
|
+
if (c === ")" || c === "*" || c === "+" || c === "?" || c === "{" || c === "}" || c === "]")
|
|
169
|
+
throw new Unreadable(`a stray "${c}"`);
|
|
170
|
+
return { k: "set", set: one(c.codePointAt(0)) };
|
|
171
|
+
}
|
|
172
|
+
hex(digits) {
|
|
173
|
+
let text = "";
|
|
174
|
+
for (let n = 0; n < digits; n++)
|
|
175
|
+
text += this.take();
|
|
176
|
+
if (!/^[0-9a-fA-F]+$/.test(text))
|
|
177
|
+
throw new Unreadable("a bad hex escape");
|
|
178
|
+
return parseInt(text, 16);
|
|
179
|
+
}
|
|
180
|
+
/** An escape after its backslash: a set (one character or a class), or outside a class an anchor or a reference. */
|
|
181
|
+
escape(inClass) {
|
|
182
|
+
const c = this.take();
|
|
183
|
+
if (CLASS_ESCAPES[c])
|
|
184
|
+
return { k: "set", set: CLASS_ESCAPES[c] };
|
|
185
|
+
if (SINGLE[c] !== undefined)
|
|
186
|
+
return { k: "set", set: one(SINGLE[c]) };
|
|
187
|
+
if (c === "b")
|
|
188
|
+
return inClass ? { k: "set", set: one(0x08) } : { k: "zero" };
|
|
189
|
+
if (c === "B" && !inClass)
|
|
190
|
+
return { k: "zero" };
|
|
191
|
+
if (c === "p" || c === "P") {
|
|
192
|
+
if (!this.eat("{"))
|
|
193
|
+
throw new Unreadable("a property escape without {");
|
|
194
|
+
while (this.at !== "}")
|
|
195
|
+
this.take();
|
|
196
|
+
this.take();
|
|
197
|
+
return { k: "set", set: ANY };
|
|
198
|
+
}
|
|
199
|
+
if (c === "k" && !inClass) {
|
|
200
|
+
if (!this.eat("<"))
|
|
201
|
+
throw new Unreadable("a named reference without <");
|
|
202
|
+
while (this.at !== ">")
|
|
203
|
+
this.take();
|
|
204
|
+
this.take();
|
|
205
|
+
return { k: "ref" };
|
|
206
|
+
}
|
|
207
|
+
if (/[1-9]/.test(c)) {
|
|
208
|
+
if (inClass)
|
|
209
|
+
throw new Unreadable("a reference in a class");
|
|
210
|
+
while (this.at !== undefined && /[0-9]/.test(this.at))
|
|
211
|
+
this.take();
|
|
212
|
+
return { k: "ref" };
|
|
213
|
+
}
|
|
214
|
+
if (c === "0") {
|
|
215
|
+
if (this.at !== undefined && /[0-9]/.test(this.at))
|
|
216
|
+
throw new Unreadable("an octal escape");
|
|
217
|
+
return { k: "set", set: one(0) };
|
|
218
|
+
}
|
|
219
|
+
if (c === "x")
|
|
220
|
+
return { k: "set", set: one(this.hex(2)) };
|
|
221
|
+
if (c === "u") {
|
|
222
|
+
if (this.eat("{")) {
|
|
223
|
+
let text = "";
|
|
224
|
+
while (this.at !== "}")
|
|
225
|
+
text += this.take();
|
|
226
|
+
this.take();
|
|
227
|
+
if (!/^[0-9a-fA-F]{1,6}$/.test(text) || parseInt(text, 16) > TOP)
|
|
228
|
+
throw new Unreadable("a bad code point escape");
|
|
229
|
+
return { k: "set", set: one(parseInt(text, 16)) };
|
|
230
|
+
}
|
|
231
|
+
const unit = this.hex(4);
|
|
232
|
+
// A surrogate pair written as two escapes is one character.
|
|
233
|
+
if (unit >= 0xd800 && unit <= 0xdbff && this.eat("\\u")) {
|
|
234
|
+
const low = this.hex(4);
|
|
235
|
+
return { k: "set", set: one(0x10000 + ((unit - 0xd800) << 10) + (low - 0xdc00)) };
|
|
236
|
+
}
|
|
237
|
+
return { k: "set", set: one(unit) };
|
|
238
|
+
}
|
|
239
|
+
if (c === "c") {
|
|
240
|
+
const letter = this.take();
|
|
241
|
+
if (!/[A-Za-z]/.test(letter))
|
|
242
|
+
throw new Unreadable("a bad control escape");
|
|
243
|
+
return { k: "set", set: one(letter.charCodeAt(0) % 32) };
|
|
244
|
+
}
|
|
245
|
+
if (SYNTAX.has(c) || (inClass && c === "-"))
|
|
246
|
+
return { k: "set", set: one(c.codePointAt(0)) };
|
|
247
|
+
throw new Unreadable(`an unknown escape \\${c}`);
|
|
248
|
+
}
|
|
249
|
+
/** A class after its [: its characters, negated when it opens with ^. */
|
|
250
|
+
cls() {
|
|
251
|
+
const negated = this.eat("^");
|
|
252
|
+
const parts = [];
|
|
253
|
+
const member = () => {
|
|
254
|
+
const c = this.take();
|
|
255
|
+
if (c === "\\")
|
|
256
|
+
return this.escape(true).set;
|
|
257
|
+
return one(c.codePointAt(0));
|
|
258
|
+
};
|
|
259
|
+
while (!this.eat("]")) {
|
|
260
|
+
const low = member();
|
|
261
|
+
if (this.at === "-" && this.cps[this.i + 1] !== undefined && String.fromCodePoint(this.cps[this.i + 1]) !== "]") {
|
|
262
|
+
this.take();
|
|
263
|
+
const high = member();
|
|
264
|
+
if (low.length !== 1 || high.length !== 1 || low[0][0] !== low[0][1] || high[0][0] !== high[0][1])
|
|
265
|
+
throw new Unreadable("a range of a class");
|
|
266
|
+
if (low[0][0] > high[0][0])
|
|
267
|
+
throw new Unreadable("a range out of order");
|
|
268
|
+
parts.push([[low[0][0], high[0][0]]]);
|
|
269
|
+
}
|
|
270
|
+
else
|
|
271
|
+
parts.push(low);
|
|
272
|
+
}
|
|
273
|
+
const set = union(...parts);
|
|
274
|
+
return negated ? complement(set) : set;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/** Every character `re` may consume. */
|
|
278
|
+
function alphabet(re) {
|
|
279
|
+
switch (re.k) {
|
|
280
|
+
case "set": return re.set;
|
|
281
|
+
case "seq": return union(...re.items.map(alphabet));
|
|
282
|
+
case "alt": return union(...re.alts.map(alphabet));
|
|
283
|
+
case "rep": return re.max === 0 ? [] : alphabet(re.body);
|
|
284
|
+
default: return [];
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function nullable(re) {
|
|
288
|
+
switch (re.k) {
|
|
289
|
+
case "set": return false;
|
|
290
|
+
case "seq": return re.items.every(nullable);
|
|
291
|
+
case "alt": return re.alts.some(nullable);
|
|
292
|
+
case "rep": return re.min === 0 || nullable(re.body);
|
|
293
|
+
default: return true;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
/** The characters a match of `re` may start with. */
|
|
297
|
+
function first(re) {
|
|
298
|
+
switch (re.k) {
|
|
299
|
+
case "set": return re.set;
|
|
300
|
+
case "seq": {
|
|
301
|
+
const sets = [];
|
|
302
|
+
for (const item of re.items) {
|
|
303
|
+
sets.push(first(item));
|
|
304
|
+
if (!nullable(item))
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
return union(...sets);
|
|
308
|
+
}
|
|
309
|
+
case "alt": return union(...re.alts.map(first));
|
|
310
|
+
case "rep": return re.max === 0 ? [] : first(re.body);
|
|
311
|
+
default: return [];
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
/** Whether `re` can match a string of characters all in `chars`: not at all (0), only the empty string (1), or a
|
|
315
|
+
* non-empty one (2). */
|
|
316
|
+
function within(re, chars) {
|
|
317
|
+
switch (re.k) {
|
|
318
|
+
case "set": return meets(re.set, chars) ? 2 : 0;
|
|
319
|
+
case "seq": {
|
|
320
|
+
let best = 1;
|
|
321
|
+
for (const item of re.items) {
|
|
322
|
+
const got = within(item, chars);
|
|
323
|
+
if (got === 0)
|
|
324
|
+
return 0;
|
|
325
|
+
if (got === 2)
|
|
326
|
+
best = 2;
|
|
327
|
+
}
|
|
328
|
+
return best;
|
|
329
|
+
}
|
|
330
|
+
case "alt": return Math.max(...re.alts.map((alt) => within(alt, chars)));
|
|
331
|
+
case "rep": {
|
|
332
|
+
if (re.max === 0)
|
|
333
|
+
return 1;
|
|
334
|
+
const got = within(re.body, chars);
|
|
335
|
+
return got === 0 ? (re.min === 0 ? 1 : 0) : got;
|
|
336
|
+
}
|
|
337
|
+
case "ref": return 1;
|
|
338
|
+
default: return 1;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
/** Every part of `re` below it (itself excluded), lookaround bodies included. */
|
|
342
|
+
function* parts(re) {
|
|
343
|
+
const children = re.k === "seq" ? re.items : re.k === "alt" ? re.alts : re.k === "rep" ? [re.body] : re.k === "zero" && re.body ? [re.body] : [];
|
|
344
|
+
for (const child of children) {
|
|
345
|
+
yield child;
|
|
346
|
+
yield* parts(child);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
/** The most passes of a repeat whose alternatives start alike that is let through: its branches' tries, multiplied
|
|
350
|
+
* across the passes, stay bounded. */
|
|
351
|
+
const ALTERNATION_PASSES = 16;
|
|
352
|
+
const repeats = (re) => re.k === "rep" && re.max >= 2;
|
|
353
|
+
/** Why `pattern` may backtrack exponentially (or could not be read), or undefined when it is safe to test strings
|
|
354
|
+
* against. */
|
|
355
|
+
export function patternRisk(pattern) {
|
|
356
|
+
// One the engine refuses never runs: the schema's compile says so.
|
|
357
|
+
try {
|
|
358
|
+
new RegExp(pattern, "u");
|
|
359
|
+
}
|
|
360
|
+
catch {
|
|
361
|
+
return undefined;
|
|
362
|
+
}
|
|
363
|
+
if (pattern.length > PATTERN_SCREEN_LIMITS.chars)
|
|
364
|
+
return `it is longer than the ${PATTERN_SCREEN_LIMITS.chars} characters a pattern may be`;
|
|
365
|
+
let re;
|
|
366
|
+
try {
|
|
367
|
+
re = new Parser(pattern).whole();
|
|
368
|
+
}
|
|
369
|
+
catch (error) {
|
|
370
|
+
return `it could not be read (${error instanceof Unreadable ? error.message : "an unreadable pattern"})`;
|
|
371
|
+
}
|
|
372
|
+
for (const outer of [re, ...parts(re)]) {
|
|
373
|
+
if (outer.k === "ref")
|
|
374
|
+
return "it has a backreference, which can take exponential time";
|
|
375
|
+
if (!repeats(outer))
|
|
376
|
+
continue;
|
|
377
|
+
for (const inner of [outer.body, ...parts(outer.body)]) {
|
|
378
|
+
// An inner repeat of a varying count (an optional part `a?` among them), a run of whose characters one pass of the
|
|
379
|
+
// outer repeat can match: a run splits between the passes in exponentially many ways (`(a+)+`, `(a?a?)+`).
|
|
380
|
+
if (inner.k === "rep" && inner.max > inner.min && within(outer.body, alphabet(inner.body)) === 2) {
|
|
381
|
+
return `"${outer.text}" repeats "${inner.text}" inside a repeat that can match the same text more than one way, which can take exponential time`;
|
|
382
|
+
}
|
|
383
|
+
// Alternatives that can start alike, repeated without end (or many times): each pass may take either. A few passes
|
|
384
|
+
// (`(25[0-5]|2[0-4][0-9]|[0-9]{1,2})\.){3}`, an IPv4 address) try a few branches each, a bounded number in all.
|
|
385
|
+
if (inner.k === "alt" && outer.max > ALTERNATION_PASSES) {
|
|
386
|
+
const starts = inner.alts.map(first);
|
|
387
|
+
if (starts.some((start, a) => starts.slice(a + 1).some((other) => meets(start, other)))) {
|
|
388
|
+
return `"${outer.text}" repeats alternatives that can start with the same character, which can take exponential time`;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return undefined;
|
|
394
|
+
}
|
|
395
|
+
/** Keywords whose values are data, never a schema: a "pattern" key inside them is no pattern. */
|
|
396
|
+
const VALUE_KEYWORDS = new Set(["const", "enum", "default", "examples"]);
|
|
397
|
+
/** Keywords whose values map names (never keywords) to schemas. */
|
|
398
|
+
const NAME_MAPS = new Set(["properties", "patternProperties", "definitions", "$defs", "dependentSchemas"]);
|
|
399
|
+
/** Patterns already read, each with why it is refused ("" when it is not): a workflow is validated more than once. */
|
|
400
|
+
const verdicts = new Map();
|
|
401
|
+
const VERDICTS = 1_000;
|
|
402
|
+
/** Each pattern `schema` would test strings with (a `pattern`, a `patternProperties` key) that may backtrack
|
|
403
|
+
* exponentially, with why. */
|
|
404
|
+
export function riskyPatterns(schema) {
|
|
405
|
+
const found = [];
|
|
406
|
+
const seen = new Set();
|
|
407
|
+
const check = (pattern) => {
|
|
408
|
+
if (seen.has(pattern))
|
|
409
|
+
return;
|
|
410
|
+
seen.add(pattern);
|
|
411
|
+
let why = verdicts.get(pattern);
|
|
412
|
+
if (why === undefined) {
|
|
413
|
+
why = patternRisk(pattern) ?? "";
|
|
414
|
+
if (verdicts.size >= VERDICTS)
|
|
415
|
+
verdicts.clear();
|
|
416
|
+
verdicts.set(pattern, why);
|
|
417
|
+
}
|
|
418
|
+
if (why)
|
|
419
|
+
found.push({ pattern, why });
|
|
420
|
+
};
|
|
421
|
+
// Every value reached, however deep (a document's own bounds limit its depth): a pattern the walk did not reach would
|
|
422
|
+
// be tested unscreened. Each object once, so a value reached two ways (or a cycle a host built) is read once.
|
|
423
|
+
const seenObjects = new Set();
|
|
424
|
+
const stack = [schema];
|
|
425
|
+
while (stack.length) {
|
|
426
|
+
const value = stack.pop();
|
|
427
|
+
if (!value || typeof value !== "object" || seenObjects.has(value))
|
|
428
|
+
continue;
|
|
429
|
+
seenObjects.add(value);
|
|
430
|
+
if (Array.isArray(value)) {
|
|
431
|
+
stack.push(...value);
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
for (const [key, child] of Object.entries(value)) {
|
|
435
|
+
if (VALUE_KEYWORDS.has(key))
|
|
436
|
+
continue;
|
|
437
|
+
if (key === "pattern" && typeof child === "string")
|
|
438
|
+
check(child);
|
|
439
|
+
else if (NAME_MAPS.has(key) && child && typeof child === "object" && !Array.isArray(child)) {
|
|
440
|
+
for (const [name, sub] of Object.entries(child)) {
|
|
441
|
+
if (key === "patternProperties")
|
|
442
|
+
check(name);
|
|
443
|
+
stack.push(sub);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
else
|
|
447
|
+
stack.push(child);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
return found;
|
|
451
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** A warning's parts: the validation code, the JSON Pointer (RFC 6901) of what it is about ("" for the document), and
|
|
2
|
+
* the message. */
|
|
3
|
+
export type PointerWarning = {
|
|
4
|
+
code: string;
|
|
5
|
+
pointer: string;
|
|
6
|
+
message: string;
|
|
7
|
+
};
|
|
8
|
+
/** `<code> at <pointer>: <message>`, or `<code>: <message>` for the pointer "". A route branch's name may hold ": ", and
|
|
9
|
+
* so may a pointer through it: in the text, the pointer's ":" is written `%3A` and its "%" `%25` (as a URI fragment
|
|
10
|
+
* writes them), so the first ":" after " at " ends the pointer. A pointer with neither reads as it is. */
|
|
11
|
+
export declare function formatPointerWarning(code: string, pointer: string, message: string): string;
|
|
12
|
+
/** The parts of a text `formatPointerWarning` wrote, its pointer as written in the workflow, or null for any other text
|
|
13
|
+
* (or a value that is not a string). */
|
|
14
|
+
export declare function parsePointerWarning(text: unknown): PointerWarning | null;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// A warning about one part of a workflow, as text: its code, the JSON Pointer of what it is about, and its words. The
|
|
2
|
+
// validator writes one when it checks references against keys it derived from the input schema (a finding that would be
|
|
3
|
+
// an error with the input given), and a reader (the console's canvas, say) reads the pointer back to draw the warning on
|
|
4
|
+
// its node. Both sides use this pair, so the format has one definition. This module imports nothing: a reader takes it
|
|
5
|
+
// without the validator.
|
|
6
|
+
/** `<code> at <pointer>: <message>`, or `<code>: <message>` for the pointer "". A route branch's name may hold ": ", and
|
|
7
|
+
* so may a pointer through it: in the text, the pointer's ":" is written `%3A` and its "%" `%25` (as a URI fragment
|
|
8
|
+
* writes them), so the first ":" after " at " ends the pointer. A pointer with neither reads as it is. */
|
|
9
|
+
export function formatPointerWarning(code, pointer, message) {
|
|
10
|
+
return `${code}${pointer ? ` at ${pointer.replace(/[%:]/g, (c) => (c === "%" ? "%25" : "%3A"))}` : ""}: ${message}`;
|
|
11
|
+
}
|
|
12
|
+
const POINTER_WARNING = /^([A-Z][A-Z0-9_]*)(?: at ([^:]+))?: ([\s\S]*)$/;
|
|
13
|
+
/** The parts of a text `formatPointerWarning` wrote, its pointer as written in the workflow, or null for any other text
|
|
14
|
+
* (or a value that is not a string). */
|
|
15
|
+
export function parsePointerWarning(text) {
|
|
16
|
+
if (typeof text !== "string")
|
|
17
|
+
return null;
|
|
18
|
+
const parts = POINTER_WARNING.exec(text);
|
|
19
|
+
if (!parts)
|
|
20
|
+
return null;
|
|
21
|
+
return { code: parts[1], pointer: (parts[2] ?? "").replace(/%(25|3A)/g, (_, hex) => (hex === "25" ? "%" : ":")), message: parts[3] };
|
|
22
|
+
}
|
package/dist/predicates.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type FieldType } from "./vocabulary.js";
|
|
1
2
|
export type StopPredicate = {
|
|
2
3
|
predicate: "no_new_items";
|
|
3
4
|
key: string;
|
|
@@ -28,11 +29,40 @@ export type AcceptPredicate = StopPredicate | {
|
|
|
28
29
|
predicate: "in";
|
|
29
30
|
path: string;
|
|
30
31
|
values: Array<string | number | boolean>;
|
|
32
|
+
} | {
|
|
33
|
+
predicate: "field_type";
|
|
34
|
+
path: string;
|
|
35
|
+
type: FieldType;
|
|
36
|
+
} | QuoteMissingPredicate | NotPredicate;
|
|
37
|
+
/** Holds when a quote at `path` is in none of the page text at `pages`. `path` names a quote (a string) or a list of them;
|
|
38
|
+
* with `field`, a list (or one record) of records, each holding its quote at `field`. Each of `pages` names a state value
|
|
39
|
+
* this run wrote, a call's result most often: every string under it is page text. */
|
|
40
|
+
export type QuoteMissingPredicate = {
|
|
41
|
+
predicate: "quote_missing";
|
|
42
|
+
path: string;
|
|
43
|
+
field?: string;
|
|
44
|
+
pages: string[];
|
|
45
|
+
};
|
|
46
|
+
/** Holds when `of` does not. `of` is mechanical: a judged question never sits inside a negation. */
|
|
47
|
+
export type NotPredicate = {
|
|
48
|
+
predicate: "not";
|
|
49
|
+
of: AcceptPredicate;
|
|
31
50
|
};
|
|
32
51
|
export declare const MECHANICAL_PREDICATE_NAMES: ReadonlySet<string>;
|
|
33
|
-
/** Resolve a dotted state path the one way every reader does: a record by key, an array by a
|
|
52
|
+
/** Resolve a dotted state path the one way every reader does: a record by its own key, an array by a
|
|
34
53
|
* non-negative integer index, anything else (a string, a number, a missing value) to undefined.
|
|
35
54
|
* `requires`, interpolation, `itemsPath`, `output.path` and predicates all read through here, so a
|
|
36
|
-
* path that names a value for one names it for all.
|
|
55
|
+
* path that names a value for one names it for all. A key is the record's own: `constructor` or
|
|
56
|
+
* `toString` that nothing wrote is undefined, never the prototype's. */
|
|
37
57
|
export declare function getPath(value: unknown, key: string): unknown;
|
|
58
|
+
/** A quote shorter than this, normalised, is not checked: too short to say which page it came from. */
|
|
59
|
+
export declare const QUOTE_MIN_CHARS = 8;
|
|
60
|
+
/** quote_missing's reading of `value`: how many quotes it checked (each at least QUOTE_MIN_CHARS once normalised) and how
|
|
61
|
+
* many of them are in no page. A quote is found when its normalised text is in one page string's (each string under the
|
|
62
|
+
* pages read alone, so no quote runs from one into the next), or when its letters and digits are in that string's; the
|
|
63
|
+
* pages are only what `value` holds, the run's own state. Counts alone: no quote or page text leaves here. */
|
|
64
|
+
export declare function quoteCheck(predicate: QuoteMissingPredicate, value: unknown): {
|
|
65
|
+
checked: number;
|
|
66
|
+
missed: number;
|
|
67
|
+
};
|
|
38
68
|
export declare function predicateMatches(predicate: StopPredicate | AcceptPredicate, value: unknown): boolean;
|
package/dist/predicates.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
+
import { isRecord } from "./record.js";
|
|
1
2
|
import { MECHANICAL_PREDICATES } from "./vocabulary.js";
|
|
2
3
|
export const MECHANICAL_PREDICATE_NAMES = new Set(MECHANICAL_PREDICATES);
|
|
3
|
-
|
|
4
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5
|
-
}
|
|
6
|
-
/** Resolve a dotted state path the one way every reader does: a record by key, an array by a
|
|
4
|
+
/** Resolve a dotted state path the one way every reader does: a record by its own key, an array by a
|
|
7
5
|
* non-negative integer index, anything else (a string, a number, a missing value) to undefined.
|
|
8
6
|
* `requires`, interpolation, `itemsPath`, `output.path` and predicates all read through here, so a
|
|
9
|
-
* path that names a value for one names it for all.
|
|
7
|
+
* path that names a value for one names it for all. A key is the record's own: `constructor` or
|
|
8
|
+
* `toString` that nothing wrote is undefined, never the prototype's. */
|
|
10
9
|
export function getPath(value, key) {
|
|
11
10
|
if (!key)
|
|
12
11
|
return value;
|
|
13
12
|
return key.split(".").reduce((current, part) => {
|
|
14
13
|
if (isRecord(current))
|
|
15
|
-
return current[part];
|
|
14
|
+
return Object.hasOwn(current, part) ? current[part] : undefined;
|
|
16
15
|
if (Array.isArray(current)) {
|
|
17
16
|
// Only a position inside the array resolves: a canonical integer below its length. Anything
|
|
18
17
|
// else (a property name, an out-of-range or non-canonical number) is not an element.
|
|
@@ -24,6 +23,60 @@ export function getPath(value, key) {
|
|
|
24
23
|
return undefined;
|
|
25
24
|
}, value);
|
|
26
25
|
}
|
|
26
|
+
/** Whether a value is of a JSON type. A number is a finite one, and an absent value is of no type. */
|
|
27
|
+
function hasType(value, type) {
|
|
28
|
+
switch (type) {
|
|
29
|
+
case "number": return typeof value === "number" && Number.isFinite(value);
|
|
30
|
+
case "string": return typeof value === "string";
|
|
31
|
+
case "boolean": return typeof value === "boolean";
|
|
32
|
+
case "array": return Array.isArray(value);
|
|
33
|
+
case "object": return isRecord(value);
|
|
34
|
+
case "null": return value === null;
|
|
35
|
+
default:
|
|
36
|
+
throw new Error(`agentrun: unsupported field_type type "${String(type)}" — fail closed, never silently false`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** A quote shorter than this, normalised, is not checked: too short to say which page it came from. */
|
|
40
|
+
export const QUOTE_MIN_CHARS = 8;
|
|
41
|
+
const QUOTE_MARKS = { "‘": "'", "’": "'", "“": "\"", "”": "\"", "–": "-", "—": "-" };
|
|
42
|
+
/** A quote or a page as quote_missing compares them: lowercase, curly quotes straight, en and em dashes "-", each run of
|
|
43
|
+
* whitespace one space, trimmed. */
|
|
44
|
+
const quoteText = (text) => text.toLowerCase().replace(/[‘’“”–—]/g, (mark) => QUOTE_MARKS[mark]).replace(/\s+/g, " ").trim();
|
|
45
|
+
/** The looser reading: the letters and digits of `text` alone, each run joined by one space. */
|
|
46
|
+
const quoteTokens = (text) => (text.toLowerCase().match(/[\p{L}\p{N}]+/gu) ?? []).join(" ");
|
|
47
|
+
/** How deep under a page path strings are read: past it a value is no page. */
|
|
48
|
+
const PAGE_DEPTH = 64;
|
|
49
|
+
function pageStrings(value, out, depth = 0) {
|
|
50
|
+
if (typeof value === "string")
|
|
51
|
+
out.push(value);
|
|
52
|
+
else if (depth < PAGE_DEPTH && value !== null && typeof value === "object")
|
|
53
|
+
for (const inner of Array.isArray(value) ? value : Object.values(value))
|
|
54
|
+
pageStrings(inner, out, depth + 1);
|
|
55
|
+
}
|
|
56
|
+
function quotesAt(value, field) {
|
|
57
|
+
if (field === undefined)
|
|
58
|
+
return typeof value === "string" ? [value] : Array.isArray(value) ? value.filter((item) => typeof item === "string") : [];
|
|
59
|
+
const records = Array.isArray(value) ? value : isRecord(value) ? [value] : [];
|
|
60
|
+
return records.map((record) => getPath(record, field)).filter((quote) => typeof quote === "string");
|
|
61
|
+
}
|
|
62
|
+
/** quote_missing's reading of `value`: how many quotes it checked (each at least QUOTE_MIN_CHARS once normalised) and how
|
|
63
|
+
* many of them are in no page. A quote is found when its normalised text is in one page string's (each string under the
|
|
64
|
+
* pages read alone, so no quote runs from one into the next), or when its letters and digits are in that string's; the
|
|
65
|
+
* pages are only what `value` holds, the run's own state. Counts alone: no quote or page text leaves here. */
|
|
66
|
+
export function quoteCheck(predicate, value) {
|
|
67
|
+
const quotes = quotesAt(getPath(value, predicate.path), predicate.field).map(quoteText).filter((quote) => quote.length >= QUOTE_MIN_CHARS);
|
|
68
|
+
if (!quotes.length)
|
|
69
|
+
return { checked: 0, missed: 0 };
|
|
70
|
+
const strings = [];
|
|
71
|
+
for (const page of predicate.pages)
|
|
72
|
+
pageStrings(getPath(value, page), strings);
|
|
73
|
+
const texts = strings.map((string) => { const text = quoteText(string); return { text, tokens: quoteTokens(text) }; });
|
|
74
|
+
const missed = quotes.filter((quote) => {
|
|
75
|
+
const loose = quoteTokens(quote);
|
|
76
|
+
return !texts.some(({ text, tokens }) => text.includes(quote) || (loose !== "" && tokens.includes(loose)));
|
|
77
|
+
}).length;
|
|
78
|
+
return { checked: quotes.length, missed };
|
|
79
|
+
}
|
|
27
80
|
export function predicateMatches(predicate, value) {
|
|
28
81
|
switch (predicate.predicate) {
|
|
29
82
|
case "field_equals":
|
|
@@ -54,6 +107,13 @@ export function predicateMatches(predicate, value) {
|
|
|
54
107
|
return false;
|
|
55
108
|
return predicate.predicate === "gte" ? target >= predicate.n : target < predicate.n;
|
|
56
109
|
}
|
|
110
|
+
case "field_type":
|
|
111
|
+
return hasType(getPath(value, predicate.path), predicate.type);
|
|
112
|
+
case "quote_missing":
|
|
113
|
+
return quoteCheck(predicate, value).missed > 0;
|
|
114
|
+
case "not":
|
|
115
|
+
// A judged `of` reaches the default case below and throws: validation refuses it first.
|
|
116
|
+
return !predicateMatches(predicate.of, value);
|
|
57
117
|
default:
|
|
58
118
|
throw new Error(`agentrun: unsupported predicate "${String(predicate?.predicate ?? "")}" — fail closed, never silently false`);
|
|
59
119
|
}
|
package/dist/record.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Whether `value` is an object that is not null and not an array. That is all it checks: a class instance, a Map, a
|
|
2
|
+
* Date or a null-prototype object passes too. The package's modules use this one copy, except graph.ts and
|
|
3
|
+
* recovery/item-state.ts: each of those imports nothing, so a static viewer can bundle it alone, and keeps its own. */
|
|
4
|
+
export declare function isRecord(value: unknown): value is Record<string, unknown>;
|
package/dist/record.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Whether `value` is an object that is not null and not an array. That is all it checks: a class instance, a Map, a
|
|
2
|
+
* Date or a null-prototype object passes too. The package's modules use this one copy, except graph.ts and
|
|
3
|
+
* recovery/item-state.ts: each of those imports nothing, so a static viewer can bundle it alone, and keeps its own. */
|
|
4
|
+
export function isRecord(value) {
|
|
5
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
6
|
+
}
|
package/dist/recovery/binding.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// per-input digests let a refused open name the inputs that moved.
|
|
3
3
|
import { canonicalHash } from "./canonical-hash.js";
|
|
4
4
|
import { RecoveryError } from "./errors.js";
|
|
5
|
+
import { isRecord } from "../record.js";
|
|
5
6
|
/** The digest of each input a binding covers, named by its path: one entry per key of `parts`, and
|
|
6
7
|
* one per key of a part that is itself an object ("config.question", "binding.budget"). Stored beside
|
|
7
8
|
* the binding, they let a mismatch name the inputs that moved instead of only saying one did. */
|
|
@@ -12,7 +13,7 @@ export function bindingDigests(parts) {
|
|
|
12
13
|
for (const [name, value] of Object.entries(parts)) {
|
|
13
14
|
if (value === undefined)
|
|
14
15
|
continue;
|
|
15
|
-
if (
|
|
16
|
+
if (isRecord(value) && Object.keys(value).length) {
|
|
16
17
|
for (const [key, inner] of Object.entries(value))
|
|
17
18
|
if (inner !== undefined)
|
|
18
19
|
digests[`${name}.${key}`] = canonicalHash(inner);
|