@giljae/pi-pstack 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +67 -2
- package/agents/comment-sicko.md +35 -0
- package/agents/poteto-agent.md +12 -0
- package/harness/pi.md +87 -0
- package/package.json +37 -4
- package/skills/architect/SKILL.md +14 -0
- package/skills/arena/SKILL.md +51 -0
- package/skills/automate-me/SKILL.md +107 -0
- package/skills/benchmark-checklist/SKILL.md +42 -0
- package/skills/blast-radius/SKILL.md +53 -0
- package/skills/bro/SKILL.md +10 -0
- package/skills/correct/SKILL.md +35 -0
- package/skills/create-formal-verification-skill/SKILL.md +46 -0
- package/skills/create-verification-skill/SKILL.md +47 -0
- package/skills/create-verification-skill/references/feature-map-example/README.md +47 -0
- package/skills/create-verification-skill/references/feature-map-example/create-note.md +39 -0
- package/skills/create-verification-skill/references/feature-map-example/search.md +45 -0
- package/skills/figure-it-out/SKILL.md +55 -0
- package/skills/how/SKILL.md +24 -0
- package/skills/in-your-own-words/SKILL.md +10 -0
- package/skills/interrogate/SKILL.md +34 -0
- package/skills/ketchup/SKILL.md +46 -0
- package/skills/ketchup-video/SKILL.md +32 -0
- package/skills/maintain-formal-verification-skill/SKILL.md +61 -0
- package/skills/maintain-verification-skill/SKILL.md +42 -0
- package/skills/make-bot-ui/SKILL.md +118 -0
- package/skills/no-comments/SKILL.md +27 -0
- package/skills/poteto-help/SKILL.md +165 -0
- package/skills/poteto-help/references/prompting.md +51 -0
- package/skills/poteto-help/references/recipes.md +51 -0
- package/skills/poteto-mode/SKILL.md +111 -0
- package/skills/poteto-mode/playbooks/authoring-a-skill.md +12 -0
- package/skills/poteto-mode/playbooks/autonomous-run.md +13 -0
- package/skills/poteto-mode/playbooks/autopilot-full.md +25 -0
- package/skills/poteto-mode/playbooks/autopilot-stack.md +8 -0
- package/skills/poteto-mode/playbooks/babysit.md +10 -0
- package/skills/poteto-mode/playbooks/bug-fix.md +15 -0
- package/skills/poteto-mode/playbooks/eval.md +25 -0
- package/skills/poteto-mode/playbooks/feature.md +21 -0
- package/skills/poteto-mode/playbooks/hillclimb.md +21 -0
- package/skills/poteto-mode/playbooks/investigation.md +14 -0
- package/skills/poteto-mode/playbooks/multi-phase-plan.md +11 -0
- package/skills/poteto-mode/playbooks/opening-a-pr.md +9 -0
- package/skills/poteto-mode/playbooks/pause-safely.md +10 -0
- package/skills/poteto-mode/playbooks/perf-issue.md +25 -0
- package/skills/poteto-mode/playbooks/prototype.md +14 -0
- package/skills/poteto-mode/playbooks/refactoring.md +16 -0
- package/skills/poteto-mode/playbooks/runtime-forensics.md +11 -0
- package/skills/poteto-mode/playbooks/session-pickup.md +11 -0
- package/skills/poteto-mode/playbooks/shipping.md +12 -0
- package/skills/poteto-mode/playbooks/trace-forensics.md +14 -0
- package/skills/poteto-mode/playbooks/visual-parity.md +11 -0
- package/skills/poteto-mode/playbooks/worktree-cleanup.md +12 -0
- package/skills/poteto-mode/references/bugbot-triage.md +142 -0
- package/skills/poteto-mode/scripts/bootstrap.ts +62 -0
- package/skills/poteto-mode/scripts/bun.lock +67 -0
- package/skills/poteto-mode/scripts/package.json +16 -0
- package/skills/poteto-mode/scripts/watch-pr/cli.test.ts +224 -0
- package/skills/poteto-mode/scripts/watch-pr/cli.ts +223 -0
- package/skills/poteto-mode/scripts/watch-pr/fakes.test-helper.ts +129 -0
- package/skills/poteto-mode/scripts/watch-pr/github.test.ts +498 -0
- package/skills/poteto-mode/scripts/watch-pr/github.ts +726 -0
- package/skills/poteto-mode/scripts/watch-pr/policy.test.ts +482 -0
- package/skills/poteto-mode/scripts/watch-pr/policy.ts +842 -0
- package/skills/poteto-mode/scripts/watch-pr/render.ts +169 -0
- package/skills/poteto-mode/scripts/watch-pr/tsconfig.json +13 -0
- package/skills/poteto-mode/scripts/watch-pr/types.compile.ts +93 -0
- package/skills/poteto-mode/scripts/watch-pr/types.ts +414 -0
- package/skills/poteto-mode/scripts/watch-pr/watch-pr +6 -0
- package/skills/poteto-mode/scripts/worktree-audit.sh +134 -0
- package/skills/poteto-mode/worker.md +11 -0
- package/skills/principle-attack-the-premise/SKILL.md +18 -0
- package/skills/principle-boundary-discipline/SKILL.md +14 -0
- package/skills/principle-build-the-lever/SKILL.md +17 -0
- package/skills/principle-encode-lessons-in-structure/SKILL.md +14 -0
- package/skills/principle-exhaust-the-design-space/SKILL.md +12 -0
- package/skills/principle-experience-first/SKILL.md +22 -0
- package/skills/principle-explain-the-number/SKILL.md +17 -0
- package/skills/principle-fix-root-causes/SKILL.md +17 -0
- package/skills/principle-foundational-thinking/SKILL.md +16 -0
- package/skills/principle-guard-the-context-window/SKILL.md +15 -0
- package/skills/principle-laziness-protocol/SKILL.md +13 -0
- package/skills/principle-make-operations-idempotent/SKILL.md +18 -0
- package/skills/principle-migrate-callers-then-delete-legacy-apis/SKILL.md +17 -0
- package/skills/principle-minimize-reader-load/SKILL.md +18 -0
- package/skills/principle-model-the-domain/SKILL.md +18 -0
- package/skills/principle-never-block-on-the-human/SKILL.md +18 -0
- package/skills/principle-outcome-oriented-execution/SKILL.md +24 -0
- package/skills/principle-prove-it-works/SKILL.md +14 -0
- package/skills/principle-redesign-from-first-principles/SKILL.md +19 -0
- package/skills/principle-separate-before-serializing-shared-state/SKILL.md +14 -0
- package/skills/principle-sequence-verifiable-units/SKILL.md +16 -0
- package/skills/principle-subtract-before-you-add/SKILL.md +21 -0
- package/skills/principle-test-behavior-not-implementation/SKILL.md +19 -0
- package/skills/principle-type-system-discipline/SKILL.md +18 -0
- package/skills/recall/SKILL.md +38 -0
- package/skills/reflect/SKILL.md +20 -0
- package/skills/setup-pstack/SKILL.md +81 -0
- package/skills/sharpen/SKILL.md +19 -0
- package/skills/show-me-your-work/SKILL.md +85 -0
- package/skills/show-me-your-work/references/decision-log-template.tsv +1 -0
- package/skills/show-me-your-work/scripts/log.sh +42 -0
- package/skills/statechart/SKILL.md +85 -0
- package/skills/statechart/references/render.md +93 -0
- package/skills/statechart/scripts/backends/cfg.ts +33 -0
- package/skills/statechart/scripts/backends/mermaid.ts +249 -0
- package/skills/statechart/scripts/backends/plain-text.ts +72 -0
- package/skills/statechart/scripts/backends/report.ts +177 -0
- package/skills/statechart/scripts/frontends/config.ts +276 -0
- package/skills/statechart/scripts/frontends/dot.ts +74 -0
- package/skills/statechart/scripts/frontends/marks.ts +93 -0
- package/skills/statechart/scripts/frontends/messages.ts +52 -0
- package/skills/statechart/scripts/frontends/spec.ts +114 -0
- package/skills/statechart/scripts/frontends/trace.ts +24 -0
- package/skills/statechart/scripts/frontends/value.ts +166 -0
- package/skills/statechart/scripts/frontends/verdict.ts +40 -0
- package/skills/statechart/scripts/frontends/view.ts +119 -0
- package/skills/statechart/scripts/ir.ts +547 -0
- package/skills/statechart/scripts/package.json +11 -0
- package/skills/statechart/scripts/passes/annotate.ts +128 -0
- package/skills/statechart/scripts/passes/label.ts +269 -0
- package/skills/statechart/scripts/passes/lift.ts +85 -0
- package/skills/statechart/scripts/passes/loop-check.ts +68 -0
- package/skills/statechart/scripts/passes/paths.ts +60 -0
- package/skills/statechart/scripts/passes/reach.ts +135 -0
- package/skills/statechart/scripts/passes/region-check.ts +97 -0
- package/skills/statechart/scripts/passes/regions.ts +295 -0
- package/skills/statechart/scripts/pipeline.ts +76 -0
- package/skills/statechart/scripts/run.ts +506 -0
- package/skills/statechart/scripts/statechart +29 -0
- package/skills/statechart/scripts/statechart.ts +164 -0
- package/skills/statechart/scripts/tsconfig.json +11 -0
- package/skills/statechart/scripts/worker.ts +35 -0
- package/skills/swarm/SKILL.md +44 -0
- package/skills/tdd/SKILL.md +45 -0
- package/skills/teach/SKILL.md +24 -0
- package/skills/technical-writing/SKILL.md +73 -0
- package/skills/typescript-best-practices/SKILL.md +33 -0
- package/skills/typescript-best-practices/references/patterns.md +328 -0
- package/skills/unslop/SKILL.md +43 -0
- package/skills/why/SKILL.md +12 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { CHECK_DEADLOCK, type Config } from "../ir.ts";
|
|
2
|
+
|
|
3
|
+
type ListField = "invariants" | "properties" | "constraints" | "actionConstraints";
|
|
4
|
+
type Section = {
|
|
5
|
+
key: string;
|
|
6
|
+
plural?: string;
|
|
7
|
+
field?:
|
|
8
|
+
| ListField
|
|
9
|
+
| "checkDeadlock"
|
|
10
|
+
| "init"
|
|
11
|
+
| "next"
|
|
12
|
+
| "spec"
|
|
13
|
+
| "view"
|
|
14
|
+
| "symmetry"
|
|
15
|
+
| "type"
|
|
16
|
+
| "typeConstraint";
|
|
17
|
+
undefinedNoun?: string;
|
|
18
|
+
};
|
|
19
|
+
const isListField = (field: Section["field"]): field is ListField =>
|
|
20
|
+
field === "invariants" ||
|
|
21
|
+
field === "properties" ||
|
|
22
|
+
field === "constraints" ||
|
|
23
|
+
field === "actionConstraints";
|
|
24
|
+
|
|
25
|
+
const SECTIONS = new Map<string, Section>();
|
|
26
|
+
for (const section of [
|
|
27
|
+
{ key: "CONSTANT", plural: "CONSTANTS" },
|
|
28
|
+
{ key: CHECK_DEADLOCK, field: "checkDeadlock" },
|
|
29
|
+
{ key: "INIT", field: "init", undefinedNoun: "initial predicate" },
|
|
30
|
+
{ key: "NEXT", field: "next", undefinedNoun: "next state action" },
|
|
31
|
+
{ key: "SPECIFICATION", field: "spec", undefinedNoun: "name" },
|
|
32
|
+
{ key: "VIEW", field: "view", undefinedNoun: "view function" },
|
|
33
|
+
{ key: "SYMMETRY", field: "symmetry", undefinedNoun: "symmetry function" },
|
|
34
|
+
{ key: "TYPE", field: "type" },
|
|
35
|
+
{ key: "TYPE_CONSTRAINT", field: "typeConstraint" },
|
|
36
|
+
{ key: "INVARIANT", plural: "INVARIANTS", field: "invariants", undefinedNoun: "invariant" },
|
|
37
|
+
{ key: "PROPERTY", plural: "PROPERTIES", field: "properties", undefinedNoun: "property" },
|
|
38
|
+
{ key: "CONSTRAINT", plural: "CONSTRAINTS", field: "constraints", undefinedNoun: "constraint" },
|
|
39
|
+
{
|
|
40
|
+
key: "ACTION_CONSTRAINT",
|
|
41
|
+
plural: "ACTION_CONSTRAINTS",
|
|
42
|
+
field: "actionConstraints",
|
|
43
|
+
undefinedNoun: "action constraint",
|
|
44
|
+
},
|
|
45
|
+
] satisfies Section[]) {
|
|
46
|
+
SECTIONS.set(section.key, section);
|
|
47
|
+
if (section.plural) SECTIONS.set(section.plural, section);
|
|
48
|
+
}
|
|
49
|
+
export const NAME = /^\w*[A-Za-z]\w*$/;
|
|
50
|
+
const WORD = /\w+/y;
|
|
51
|
+
const CONFIG_HOLDS =
|
|
52
|
+
"A config can hold only names, numbers, strings, comments, and the symbols { } ( ) [ ] , = ! <-.";
|
|
53
|
+
const shown = (c: string) => {
|
|
54
|
+
const code = c.codePointAt(0) ?? 0;
|
|
55
|
+
const hex = `U+${code.toString(16).toUpperCase().padStart(4, "0")}`;
|
|
56
|
+
if (code < 0x20 || code === 0x7f) return hex;
|
|
57
|
+
return code < 0x7f ? `"${c}"` : `"${c}" (${hex})`;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
type Token = { image: string; line: number };
|
|
61
|
+
|
|
62
|
+
function tokenizeConfig(text: string, cannot: (line: number, words: string) => Error) {
|
|
63
|
+
if (text.startsWith("\uFEFF"))
|
|
64
|
+
throw cannot(1, "The file starts with a byte order mark. Save it as UTF-8 without one.");
|
|
65
|
+
const tokens: Token[] = [];
|
|
66
|
+
let line = 1;
|
|
67
|
+
let last = 1;
|
|
68
|
+
let i = 0;
|
|
69
|
+
const skip = (to: number) => {
|
|
70
|
+
for (; i < to; i++) {
|
|
71
|
+
last = line;
|
|
72
|
+
if (text[i] === "\n" || (text[i] === "\r" && text[i + 1] !== "\n")) line++;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const push = (image: string) => {
|
|
76
|
+
tokens.push({ image, line });
|
|
77
|
+
skip(i + image.length);
|
|
78
|
+
};
|
|
79
|
+
while (i < text.length) {
|
|
80
|
+
const c = text[i];
|
|
81
|
+
const two = text.slice(i, i + 2);
|
|
82
|
+
const start = line;
|
|
83
|
+
if (c === " " || c === "\t" || c === "\n" || c === "\r") skip(i + 1);
|
|
84
|
+
else if (two === "\\*") {
|
|
85
|
+
const end = text.slice(i).search(/[\n\r]/);
|
|
86
|
+
skip(end < 0 ? text.length : i + end);
|
|
87
|
+
} else if (two === "(*") {
|
|
88
|
+
const marks = /\(\*|\*\)/g;
|
|
89
|
+
let depth = 0;
|
|
90
|
+
do {
|
|
91
|
+
marks.lastIndex = i;
|
|
92
|
+
const m = marks.exec(text);
|
|
93
|
+
if (!m) throw cannot(start, "A comment starts here and does not end.");
|
|
94
|
+
depth += m[0] === "(*" ? 1 : -1;
|
|
95
|
+
skip(m.index + 2);
|
|
96
|
+
} while (depth);
|
|
97
|
+
} else if (c === '"') {
|
|
98
|
+
let j = i + 1;
|
|
99
|
+
while (text[j] !== '"') {
|
|
100
|
+
const ch = text[j];
|
|
101
|
+
if (ch === undefined) throw cannot(start, "A string starts here and does not end.");
|
|
102
|
+
if (ch === "\n" || ch === "\r")
|
|
103
|
+
throw cannot(
|
|
104
|
+
start,
|
|
105
|
+
"A string starts here and does not end on its line. Write \\n for a line break in a string.",
|
|
106
|
+
);
|
|
107
|
+
if (ch === "\0") throw cannot(start, "A string has the character U+0000.");
|
|
108
|
+
if (ch === "\\" && text[j + 1] !== undefined && !'"\\ntrf'.includes(text[j + 1]))
|
|
109
|
+
throw cannot(
|
|
110
|
+
start,
|
|
111
|
+
`A string has a backslash before ${shown(text[j + 1])}. A string can use only the escapes \\" \\\\ \\n \\t \\r \\f.`,
|
|
112
|
+
);
|
|
113
|
+
j += ch === "\\" ? 2 : 1;
|
|
114
|
+
}
|
|
115
|
+
push(text.slice(i, j + 1));
|
|
116
|
+
} else if (/\w/.test(c)) {
|
|
117
|
+
WORD.lastIndex = i;
|
|
118
|
+
const word = WORD.exec(text)?.[0] ?? c;
|
|
119
|
+
if (/^[WS]F_/.test(word))
|
|
120
|
+
throw cannot(
|
|
121
|
+
start,
|
|
122
|
+
`TLC reads ${word.slice(0, 3)} at the start of a word as a TLA+ keyword, so it cannot read ${word} as a name.`,
|
|
123
|
+
);
|
|
124
|
+
if (!/[A-Za-z]/.test(word) && word !== "_") {
|
|
125
|
+
if (/\D/.test(word))
|
|
126
|
+
throw cannot(
|
|
127
|
+
start,
|
|
128
|
+
`TLC splits ${word} into ${word.match(/\d+|_/g)?.join(" ")}, because a name needs a letter.`,
|
|
129
|
+
);
|
|
130
|
+
if (Number(word) > 2147483647)
|
|
131
|
+
throw cannot(start, `TLC cannot read a number above 2147483647, such as ${word}.`);
|
|
132
|
+
}
|
|
133
|
+
push(word);
|
|
134
|
+
} else if (two === "<-") push(two);
|
|
135
|
+
else if (["[]", "]_", "==", "=<", "!!"].includes(two))
|
|
136
|
+
throw cannot(start, `TLC reads ${two} as one TLA+ symbol, and a config cannot hold it.`);
|
|
137
|
+
else if ("{}()[],=!".includes(c)) push(c);
|
|
138
|
+
else {
|
|
139
|
+
const ch = String.fromCodePoint(text.codePointAt(i) ?? 0);
|
|
140
|
+
const where = ch > "\x7f" ? " outside a string" : "";
|
|
141
|
+
throw cannot(start, `It cannot read the character ${shown(ch)}${where}. ${CONFIG_HOLDS}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return { tokens, end: last };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function readConfig({ file, text }: { file: string; text: string }): Config {
|
|
148
|
+
const refuse = (line: number, words: string) =>
|
|
149
|
+
new Error(`TLC refuses the config ${file} at line ${line}. ${words}`);
|
|
150
|
+
const cannot = (line: number, words: string) =>
|
|
151
|
+
new Error(`The command cannot read the config ${file} at line ${line}. ${words}`);
|
|
152
|
+
const { tokens, end } = tokenizeConfig(text, cannot);
|
|
153
|
+
const eof: Token = { image: "", line: end };
|
|
154
|
+
let at = 0;
|
|
155
|
+
const next = () => tokens[at++] ?? eof;
|
|
156
|
+
let deferred: Error | undefined;
|
|
157
|
+
const defer = (error: Error) => (deferred ??= error);
|
|
158
|
+
const defined = (t: Token, undefinedNoun: string | undefined) => {
|
|
159
|
+
if (!NAME.test(t.image))
|
|
160
|
+
defer(
|
|
161
|
+
refuse(
|
|
162
|
+
t.line,
|
|
163
|
+
`The ${undefinedNoun} ${t.image} specified in the configuration file is not defined in the specification.`,
|
|
164
|
+
),
|
|
165
|
+
);
|
|
166
|
+
};
|
|
167
|
+
const name = (t: Token, what: string) => {
|
|
168
|
+
if (SECTIONS.has(t.image))
|
|
169
|
+
defer(cannot(t.line, `TLC would read the keyword ${t.image} as ${what}.`));
|
|
170
|
+
else if (!NAME.test(t.image))
|
|
171
|
+
defer(cannot(t.line, `TLC would read ${t.image} as ${what}, and it is not a name.`));
|
|
172
|
+
};
|
|
173
|
+
const value = (t: Token): void => {
|
|
174
|
+
if (t === eof) throw refuse(t.line, "It was expecting a value, but did not find it.");
|
|
175
|
+
if (t.image === "{") {
|
|
176
|
+
t = next();
|
|
177
|
+
if (t.image === "}") return;
|
|
178
|
+
value(t);
|
|
179
|
+
while ((t = next()).image === ",") value(next());
|
|
180
|
+
if (t.image !== "}") throw refuse(t.line, "It was expecting }, but did not find it.");
|
|
181
|
+
} else if (SECTIONS.has(t.image))
|
|
182
|
+
defer(cannot(t.line, `TLC would read the keyword ${t.image} as a value.`));
|
|
183
|
+
else if (!NAME.test(t.image) && !/^(\d+|".*")$/.test(t.image))
|
|
184
|
+
defer(
|
|
185
|
+
cannot(
|
|
186
|
+
t.line,
|
|
187
|
+
`TLC would read ${t.image} as a value. A value is a name, a number, a string, or a set in braces.`,
|
|
188
|
+
),
|
|
189
|
+
);
|
|
190
|
+
};
|
|
191
|
+
const constant = (t: Token, loc: number) => {
|
|
192
|
+
name(t, "a constant name");
|
|
193
|
+
for (t = next(); t.image === "!"; t = next()) name((t = next()), "a constant name");
|
|
194
|
+
if (t.image === "<-") {
|
|
195
|
+
t = next();
|
|
196
|
+
if (t.image === "[") {
|
|
197
|
+
t = next();
|
|
198
|
+
if (t === eof) throw refuse(t.line, "Expected an identifier after <-[.");
|
|
199
|
+
name(t, "a module name");
|
|
200
|
+
t = next();
|
|
201
|
+
if (t.image !== "]") throw refuse(t.line, "It was expecting ], but did not find it.");
|
|
202
|
+
t = next();
|
|
203
|
+
if (t === eof) throw refuse(t.line, "Expected an identifier after <-[mod].");
|
|
204
|
+
} else if (t === eof) throw refuse(t.line, "Expected an identifier after <-.");
|
|
205
|
+
name(t, "the name after <-");
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
if (t.image === "(") {
|
|
209
|
+
value(next());
|
|
210
|
+
while ((t = next()).image === ",") value(next());
|
|
211
|
+
if (t.image !== ")")
|
|
212
|
+
throw refuse(
|
|
213
|
+
loc,
|
|
214
|
+
"TLC gives no reason here. An operator's arguments are one or more values in ( ), with commas between them.",
|
|
215
|
+
);
|
|
216
|
+
t = next();
|
|
217
|
+
}
|
|
218
|
+
if (t.image !== "=") throw refuse(t.line, "It was expecting = or <-, but did not find it.");
|
|
219
|
+
t = next();
|
|
220
|
+
if (t.image === "[") {
|
|
221
|
+
t = next();
|
|
222
|
+
if (t === eof) throw refuse(t.line, "Expected an identifier after =[.");
|
|
223
|
+
name(t, "a module name");
|
|
224
|
+
t = next();
|
|
225
|
+
if (t.image !== "]") throw refuse(t.line, "It was expecting ], but did not find it.");
|
|
226
|
+
t = next();
|
|
227
|
+
}
|
|
228
|
+
value(t);
|
|
229
|
+
};
|
|
230
|
+
const config: Config = {
|
|
231
|
+
sections: [],
|
|
232
|
+
invariants: [],
|
|
233
|
+
properties: [],
|
|
234
|
+
constraints: [],
|
|
235
|
+
actionConstraints: [],
|
|
236
|
+
};
|
|
237
|
+
for (let t = next(); t !== eof;) {
|
|
238
|
+
const keyword = t;
|
|
239
|
+
const section = SECTIONS.get(keyword.image);
|
|
240
|
+
if (!section) throw refuse(t.line, "It was expecting a keyword, but did not find it.");
|
|
241
|
+
const { key, field, undefinedNoun } = section;
|
|
242
|
+
const begin = at;
|
|
243
|
+
if (field === undefined) {
|
|
244
|
+
for (t = next(); t !== eof && !SECTIONS.has(t.image); t = next()) constant(t, keyword.line);
|
|
245
|
+
} else if (isListField(field)) {
|
|
246
|
+
for (t = next(); t !== eof && !SECTIONS.has(t.image); t = next()) {
|
|
247
|
+
config[field].push(t.image);
|
|
248
|
+
defined(t, undefinedNoun);
|
|
249
|
+
}
|
|
250
|
+
} else {
|
|
251
|
+
t = next();
|
|
252
|
+
if (t === eof)
|
|
253
|
+
throw refuse(
|
|
254
|
+
keyword.line,
|
|
255
|
+
`The keyword ${keyword.image} was not followed by an identifier.`,
|
|
256
|
+
);
|
|
257
|
+
if (field === "checkDeadlock" && t.image !== "TRUE" && t.image !== "FALSE")
|
|
258
|
+
throw refuse(t.line, "It was expecting TRUE or FALSE, but did not find it.");
|
|
259
|
+
if (config[field] !== undefined)
|
|
260
|
+
throw refuse(keyword.line, `The keyword ${keyword.image} appeared twice.`);
|
|
261
|
+
if (field === "checkDeadlock") config[field] = t.image === "TRUE";
|
|
262
|
+
else {
|
|
263
|
+
config[field] = t.image;
|
|
264
|
+
if (SECTIONS.has(t.image))
|
|
265
|
+
defer(cannot(t.line, `TLC would read the keyword ${t.image} as the name after ${key}.`));
|
|
266
|
+
else if (undefinedNoun) defined(t, undefinedNoun);
|
|
267
|
+
}
|
|
268
|
+
t = next();
|
|
269
|
+
}
|
|
270
|
+
const images: string[] = [];
|
|
271
|
+
for (let k = begin; k < (t === eof ? tokens.length : at - 1); k++) images.push(tokens[k].image);
|
|
272
|
+
config.sections.push({ key, line: keyword.line, tokens: images });
|
|
273
|
+
}
|
|
274
|
+
if (deferred) throw deferred;
|
|
275
|
+
return config;
|
|
276
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { type Graph, type State, type Step, type Value, type Vars } from "../ir.ts";
|
|
2
|
+
import { parseValue } from "./value.ts";
|
|
3
|
+
|
|
4
|
+
const unescapeDot = (s: string) => {
|
|
5
|
+
let i = s.indexOf("\\");
|
|
6
|
+
if (i === -1) return s;
|
|
7
|
+
let out = "";
|
|
8
|
+
let from = 0;
|
|
9
|
+
for (; i !== -1 && i + 1 < s.length; i = s.indexOf("\\", i)) {
|
|
10
|
+
const c = s[i + 1];
|
|
11
|
+
if (c === "\n" || c === "\r" || c === "\u2028" || c === "\u2029") {
|
|
12
|
+
i++;
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
out += s.slice(from, i) + (c === "n" ? "\n" : c);
|
|
16
|
+
i += 2;
|
|
17
|
+
from = i;
|
|
18
|
+
}
|
|
19
|
+
return out + s.slice(from);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const parseState = (label: string, parsed = new Map<string, [string, Value]>()): Vars => {
|
|
23
|
+
const pairs: [string, Value][] = [];
|
|
24
|
+
for (const part of (label.startsWith("/\\ ") ? label.slice(3) : label).split("\n/\\ ")) {
|
|
25
|
+
let pair = parsed.get(part);
|
|
26
|
+
if (!pair) {
|
|
27
|
+
const [, name, value] = /^(\w+)\s*=\s*([\s\S]*)$/.exec(part) ?? [];
|
|
28
|
+
if (!name) throw new Error(`cannot read the TLC state line "${part}"`);
|
|
29
|
+
parsed.set(part, (pair = [name, parseValue(value)]));
|
|
30
|
+
}
|
|
31
|
+
pairs.push(pair);
|
|
32
|
+
}
|
|
33
|
+
return Object.fromEntries(pairs);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type Lines = string | Iterable<string>;
|
|
37
|
+
|
|
38
|
+
export const linesOf = (input: Lines) => (typeof input === "string" ? input.split("\n") : input);
|
|
39
|
+
|
|
40
|
+
export function parseDot(input: Lines): Graph {
|
|
41
|
+
const states = new Map<string, State>();
|
|
42
|
+
const steps = new Map<string, Step>();
|
|
43
|
+
const parsed = new Map<string, [string, Value]>();
|
|
44
|
+
const owned = new Map<string, string>();
|
|
45
|
+
const own = (s: string) => {
|
|
46
|
+
let copy = owned.get(s);
|
|
47
|
+
if (copy === undefined) owned.set((copy = String(JSON.parse(JSON.stringify(s)))), copy);
|
|
48
|
+
return copy;
|
|
49
|
+
};
|
|
50
|
+
for (const line of linesOf(input)) {
|
|
51
|
+
const edge = /^(-?\d+) -> (-?\d+) \[label="([^"]*)"/.exec(line);
|
|
52
|
+
if (edge) {
|
|
53
|
+
steps.set(edge.slice(1).join(" "), {
|
|
54
|
+
from: own(edge[1]),
|
|
55
|
+
to: own(edge[2]),
|
|
56
|
+
action: own(edge[3]),
|
|
57
|
+
});
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const node = /^(-?\d+) \[label="((?:[^"\\]|\\.)*)"(,style = filled)?/.exec(line);
|
|
61
|
+
if (node)
|
|
62
|
+
states.set(own(node[1]), {
|
|
63
|
+
vars: parseState(unescapeDot(node[2]), parsed),
|
|
64
|
+
start: !!node[3],
|
|
65
|
+
breaks: [],
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
for (const loose of steps.values())
|
|
69
|
+
if (!states.has(loose.from) || !states.has(loose.to))
|
|
70
|
+
throw new Error(
|
|
71
|
+
`The TLC graph dump has a step ${loose.action} to or from a state that it does not list.`,
|
|
72
|
+
);
|
|
73
|
+
return { states, steps: [...steps.values()] };
|
|
74
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { EC, type Mark, memo, type Part, type TraceState, type Value } from "../ir.ts";
|
|
2
|
+
import { actionOf, messageState } from "./messages.ts";
|
|
3
|
+
import { BRACKET, isSpace, parseValue, tokenEnd, unreadable } from "./value.ts";
|
|
4
|
+
|
|
5
|
+
const markTexts = (chunk: string, top: number[], inner: number[]) => {
|
|
6
|
+
if (top.length !== 2) return undefined;
|
|
7
|
+
const [a, b] = top;
|
|
8
|
+
const open = chunk.indexOf("<<", a);
|
|
9
|
+
const close = chunk.lastIndexOf(">>", b);
|
|
10
|
+
const end = chunk.trimEnd().length - 2;
|
|
11
|
+
if (open < 0 || chunk.slice(a + 1, open).trim() || chunk.slice(close + 2, b).trim())
|
|
12
|
+
return undefined;
|
|
13
|
+
if (!chunk.startsWith(">>", end)) return undefined;
|
|
14
|
+
const cuts = [open + 1];
|
|
15
|
+
for (const k of inner) if (k > open && k < close) cuts.push(k);
|
|
16
|
+
cuts.push(close);
|
|
17
|
+
const texts: string[] = [];
|
|
18
|
+
for (let k = 1; k < cuts.length; k++) texts.push(chunk.slice(cuts[k - 1] + 1, cuts[k]));
|
|
19
|
+
return { values: texts, rules: chunk.slice(b + 1, end) };
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const ruleNames = (v: Value) => {
|
|
23
|
+
if (typeof v !== "object" || v.kind !== "set") return undefined;
|
|
24
|
+
const names: string[] = [];
|
|
25
|
+
for (const m of v.members) {
|
|
26
|
+
if (typeof m !== "string") return undefined;
|
|
27
|
+
names.push(m);
|
|
28
|
+
}
|
|
29
|
+
return names;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export function* readMarks(messages: Iterable<Part>, tag: string): Generator<Mark> {
|
|
33
|
+
const read = memo(parseValue);
|
|
34
|
+
const start = `"${tag}",`;
|
|
35
|
+
let chunk = "";
|
|
36
|
+
let depth = 0;
|
|
37
|
+
let top: number[] = [];
|
|
38
|
+
let inner: number[] = [];
|
|
39
|
+
for (const line of messages) {
|
|
40
|
+
if (typeof line !== "string") continue;
|
|
41
|
+
if (!chunk) {
|
|
42
|
+
if (!line.startsWith("<<") || !line.slice(2).trimStart().startsWith(start)) continue;
|
|
43
|
+
depth = 0;
|
|
44
|
+
top = [];
|
|
45
|
+
inner = [];
|
|
46
|
+
}
|
|
47
|
+
const base = chunk.length;
|
|
48
|
+
chunk += line + "\n";
|
|
49
|
+
let i = 0;
|
|
50
|
+
let last = 0;
|
|
51
|
+
while (i < line.length) {
|
|
52
|
+
const c = line.charCodeAt(i);
|
|
53
|
+
if (isSpace(c)) {
|
|
54
|
+
i++;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const j = tokenEnd(line, i);
|
|
58
|
+
if (j === -1) throw unreadable(line + "\n", last);
|
|
59
|
+
if (c === 44 && depth === 1) top.push(base + i);
|
|
60
|
+
else if (c === 44 && depth === 2) inner.push(base + i);
|
|
61
|
+
depth += BRACKET[c];
|
|
62
|
+
i = last = j;
|
|
63
|
+
}
|
|
64
|
+
if (depth > 0) continue;
|
|
65
|
+
const texts = markTexts(chunk, top, inner);
|
|
66
|
+
const values: Value[] = [];
|
|
67
|
+
if (texts) for (const v of texts.values) values.push(read(v));
|
|
68
|
+
const broken = texts && ruleNames(read(texts.rules));
|
|
69
|
+
if (!broken)
|
|
70
|
+
throw new Error(`TLC printed a mark that the command cannot read:\n${chunk.slice(0, 2000)}`);
|
|
71
|
+
chunk = "";
|
|
72
|
+
yield { values, broken };
|
|
73
|
+
}
|
|
74
|
+
if (chunk)
|
|
75
|
+
throw new Error(
|
|
76
|
+
`TLC's output ends inside a marked state, so the command cannot tell which rules it breaks:\n${chunk.slice(0, 2000)}`,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function readCounterexample(messages: Iterable<Part>) {
|
|
81
|
+
const trace: TraceState[] = [];
|
|
82
|
+
for (const m of messages) {
|
|
83
|
+
if (typeof m === "string") continue;
|
|
84
|
+
const action = m.code === EC.TLC_STATE_PRINT2 ? actionOf(m) : undefined;
|
|
85
|
+
if (action !== undefined) trace.push({ action, vars: messageState(m) });
|
|
86
|
+
else if (
|
|
87
|
+
m.code === EC.TLC_INVARIANT_VIOLATED_INITIAL ||
|
|
88
|
+
m.code === EC.TLC_PROPERTY_VIOLATED_INITIAL
|
|
89
|
+
)
|
|
90
|
+
trace.push({ action: "", vars: messageState(m) });
|
|
91
|
+
}
|
|
92
|
+
return trace;
|
|
93
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Message, Part } from "../ir.ts";
|
|
2
|
+
import { linesOf, parseState } from "./dot.ts";
|
|
3
|
+
|
|
4
|
+
const START = /@!@!@STARTMSG (\d+):(\d+) @!@!@$/;
|
|
5
|
+
const END = /^@!@!@ENDMSG \d+ @!@!@$/;
|
|
6
|
+
|
|
7
|
+
const isCutMarker = (lastLine: string) => lastLine.startsWith("@!@!@");
|
|
8
|
+
|
|
9
|
+
function cutMessage(open: Message[]) {
|
|
10
|
+
for (let i = open.length - 1; i > 0 && !open[i].body.length; i--) open[i - 1].body.pop();
|
|
11
|
+
return open[0]?.body.length ? open[0] : undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function* readMessages(tlcOut: string | Iterable<string>): Generator<Part> {
|
|
15
|
+
const lines = linesOf(tlcOut)[Symbol.iterator]();
|
|
16
|
+
const open: Message[] = [];
|
|
17
|
+
try {
|
|
18
|
+
for (let next = lines.next(); !next.done;) {
|
|
19
|
+
const line = next.value;
|
|
20
|
+
next = lines.next();
|
|
21
|
+
if (next.done && (!line || isCutMarker(line))) break;
|
|
22
|
+
const start = START.exec(line);
|
|
23
|
+
if (start) {
|
|
24
|
+
const message: Message = { code: Number(start[1]), level: Number(start[2]), body: [] };
|
|
25
|
+
if (start.index) message.beforeMarker = line.slice(0, start.index);
|
|
26
|
+
open.at(-1)?.body.push(message);
|
|
27
|
+
open.push(message);
|
|
28
|
+
} else if (END.test(line)) {
|
|
29
|
+
const done = open.pop();
|
|
30
|
+
if (done && !open.length) yield done;
|
|
31
|
+
} else if (open.length) open[open.length - 1].body.push(line);
|
|
32
|
+
else yield line;
|
|
33
|
+
}
|
|
34
|
+
} finally {
|
|
35
|
+
lines.return?.();
|
|
36
|
+
}
|
|
37
|
+
const cut = cutMessage(open);
|
|
38
|
+
if (cut) yield cut;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const textOf = ({ body }: Message) =>
|
|
42
|
+
body.filter((part) => typeof part === "string").join("\n");
|
|
43
|
+
|
|
44
|
+
export const actionOf = (m: Message) =>
|
|
45
|
+
/^\d+: (?:Back to state: )?<([^\s>]+)/.exec(textOf(m))?.[1];
|
|
46
|
+
|
|
47
|
+
export const messageState = (m: Message) => {
|
|
48
|
+
const lines = textOf(m).split("\n");
|
|
49
|
+
const body: string[] = [];
|
|
50
|
+
for (let i = 1; i < lines.length && lines[i].trim(); i++) body.push(lines[i]);
|
|
51
|
+
return parseState(body.join("\n"));
|
|
52
|
+
};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { Config, ConfigSection, Spec } from "../ir.ts";
|
|
2
|
+
import { NAME } from "./config.ts";
|
|
3
|
+
|
|
4
|
+
export type Checks = Pick<Spec, "invariants" | "properties" | "deadlock" | "written">;
|
|
5
|
+
|
|
6
|
+
export function readChecks({ configFile, config }: { configFile: string; config: Config }): Checks {
|
|
7
|
+
const refuse = (words: string) => new Error(`TLC refuses the config ${configFile}. ${words}`);
|
|
8
|
+
if (config.spec && (config.init || config.next))
|
|
9
|
+
throw refuse("The configuration file cannot specify both INIT/NEXT and SPECIFICATION fields.");
|
|
10
|
+
if (!config.spec && !config.init)
|
|
11
|
+
throw refuse("The configuration file did not specify the initial state predicate.");
|
|
12
|
+
const written: ConfigSection[] = [];
|
|
13
|
+
for (const s of config.sections)
|
|
14
|
+
if (!["INVARIANT", "PROPERTY", "CHECK_DEADLOCK"].includes(s.key)) written.push(s);
|
|
15
|
+
return {
|
|
16
|
+
invariants: config.invariants,
|
|
17
|
+
properties: config.properties,
|
|
18
|
+
deadlock: config.checkDeadlock !== false,
|
|
19
|
+
written,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type XmlNode = { tag: string; text: string; children: XmlNode[] };
|
|
24
|
+
|
|
25
|
+
const ENTITIES: { [name: string]: string } = { amp: "&", apos: "'", gt: ">", lt: "<", quot: '"' };
|
|
26
|
+
|
|
27
|
+
const decode = (text: string) =>
|
|
28
|
+
text.replace(/&(#x?)?(\w+);/g, (_, number: string | undefined, name: string) =>
|
|
29
|
+
number
|
|
30
|
+
? String.fromCodePoint(Number.parseInt(name, number === "#x" ? 16 : 10))
|
|
31
|
+
: ENTITIES[name],
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
function parseXml(xml: string) {
|
|
35
|
+
const document: XmlNode = { tag: "", text: "", children: [] };
|
|
36
|
+
const open = [document];
|
|
37
|
+
for (const [, close, tag, empty, text] of xml.matchAll(
|
|
38
|
+
/<(\/?)([^\s/>?!]+)[^>]*?(\/?)>|<[?!][^>]*>|([^<]+)/g,
|
|
39
|
+
)) {
|
|
40
|
+
const parent = open[open.length - 1];
|
|
41
|
+
if (text !== undefined) parent.text += decode(text);
|
|
42
|
+
else if (close) open.pop();
|
|
43
|
+
else if (tag) {
|
|
44
|
+
const node: XmlNode = { tag, text: "", children: [] };
|
|
45
|
+
parent.children.push(node);
|
|
46
|
+
if (!empty) open.push(node);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return document;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const child = (node: XmlNode | undefined, tag: string) => node?.children.find((c) => c.tag === tag);
|
|
53
|
+
const textOf = (node: XmlNode | undefined, tag: string) => child(node, tag)?.text ?? "";
|
|
54
|
+
|
|
55
|
+
export type Sany = {
|
|
56
|
+
variables: string[];
|
|
57
|
+
definitions: Map<string, XmlNode>;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const VARIABLE_KIND = "3";
|
|
61
|
+
|
|
62
|
+
export function readSany(xml: string): Sany {
|
|
63
|
+
const modules = child(parseXml(xml), "modules");
|
|
64
|
+
const symbols = new Map<string, XmlNode>();
|
|
65
|
+
for (const entry of child(modules, "context")?.children ?? [])
|
|
66
|
+
symbols.set(textOf(entry, "UID"), entry.children[1]);
|
|
67
|
+
const root = textOf(modules, "RootModule");
|
|
68
|
+
let module: XmlNode | undefined;
|
|
69
|
+
for (const node of symbols.values())
|
|
70
|
+
if (node.tag === "ModuleNode" && textOf(node, "uniquename") === root) module = node;
|
|
71
|
+
const definitions = new Map<string, XmlNode>();
|
|
72
|
+
const variables: { uid: number; name: string }[] = [];
|
|
73
|
+
for (const ref of module?.children ?? []) {
|
|
74
|
+
if (ref.tag !== "OpDeclNodeRef" && ref.tag !== "UserDefinedOpKindRef") continue;
|
|
75
|
+
const uid = textOf(ref, "UID");
|
|
76
|
+
const node = symbols.get(uid);
|
|
77
|
+
if (!node) continue;
|
|
78
|
+
const name = textOf(node, "uniquename");
|
|
79
|
+
definitions.set(name, node);
|
|
80
|
+
if (node.tag === "OpDeclNode" && textOf(node, "kind") === VARIABLE_KIND)
|
|
81
|
+
variables.push({ uid: Number(uid), name });
|
|
82
|
+
}
|
|
83
|
+
variables.sort((a, b) => a.uid - b.uid);
|
|
84
|
+
return { variables: variables.map((v) => v.name), definitions };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function readSpec({
|
|
88
|
+
module,
|
|
89
|
+
sany,
|
|
90
|
+
checks,
|
|
91
|
+
}: {
|
|
92
|
+
module: string;
|
|
93
|
+
sany: Sany;
|
|
94
|
+
checks: Checks;
|
|
95
|
+
}): Spec {
|
|
96
|
+
return { module, variables: sany.variables, ...checks };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const STATE_LEVEL = 1;
|
|
100
|
+
|
|
101
|
+
export function initialOnlyProperties({ definitions }: Sany, { written, properties }: Spec) {
|
|
102
|
+
const replaced = new Map<string, string>();
|
|
103
|
+
for (const s of written)
|
|
104
|
+
if (s.key === "CONSTANT")
|
|
105
|
+
for (let i = 1; i + 1 < s.tokens.length; i++)
|
|
106
|
+
if (s.tokens[i] === "<-" && NAME.test(s.tokens[i + 1]))
|
|
107
|
+
replaced.set(s.tokens[i - 1], s.tokens[i + 1]);
|
|
108
|
+
const names = new Set<string>();
|
|
109
|
+
for (const name of properties) {
|
|
110
|
+
const definition = definitions.get(replaced.get(name) ?? name);
|
|
111
|
+
if (definition && Number(textOf(definition, "level")) <= STATE_LEVEL) names.add(name);
|
|
112
|
+
}
|
|
113
|
+
return names;
|
|
114
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { EC, type Part, type Trace } from "../ir.ts";
|
|
2
|
+
import { actionOf, messageState, textOf } from "./messages.ts";
|
|
3
|
+
|
|
4
|
+
export function readTrace(messages: Iterable<Part>): Trace {
|
|
5
|
+
const seq: Trace["seq"] = [];
|
|
6
|
+
for (const m of messages) {
|
|
7
|
+
if (typeof m === "string") continue;
|
|
8
|
+
if (m.code === EC.TLC_STATE_PRINT3) return { seq, loop: seq.length - 1 };
|
|
9
|
+
const back = m.code === EC.TLC_BACK_TO_STATE && /^(\d+): Back to state: /.exec(textOf(m));
|
|
10
|
+
if (back) {
|
|
11
|
+
const loop = Number(back[1]) - 1;
|
|
12
|
+
seq.push({ action: actionOf(m) ?? "", vars: seq[loop]?.vars });
|
|
13
|
+
return { seq, loop };
|
|
14
|
+
}
|
|
15
|
+
const action = m.code === EC.TLC_STATE_PRINT2 ? actionOf(m) : undefined;
|
|
16
|
+
if (action !== undefined) seq.push({ action, vars: messageState(m) });
|
|
17
|
+
else if (
|
|
18
|
+
m.code === EC.TLC_INVARIANT_VIOLATED_INITIAL ||
|
|
19
|
+
m.code === EC.TLC_PROPERTY_VIOLATED_INITIAL
|
|
20
|
+
)
|
|
21
|
+
seq.push({ action: "", vars: messageState(m) });
|
|
22
|
+
}
|
|
23
|
+
return { seq, loop: undefined };
|
|
24
|
+
}
|