@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.
Files changed (142) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +67 -2
  3. package/agents/comment-sicko.md +35 -0
  4. package/agents/poteto-agent.md +12 -0
  5. package/harness/pi.md +87 -0
  6. package/package.json +37 -4
  7. package/skills/architect/SKILL.md +14 -0
  8. package/skills/arena/SKILL.md +51 -0
  9. package/skills/automate-me/SKILL.md +107 -0
  10. package/skills/benchmark-checklist/SKILL.md +42 -0
  11. package/skills/blast-radius/SKILL.md +53 -0
  12. package/skills/bro/SKILL.md +10 -0
  13. package/skills/correct/SKILL.md +35 -0
  14. package/skills/create-formal-verification-skill/SKILL.md +46 -0
  15. package/skills/create-verification-skill/SKILL.md +47 -0
  16. package/skills/create-verification-skill/references/feature-map-example/README.md +47 -0
  17. package/skills/create-verification-skill/references/feature-map-example/create-note.md +39 -0
  18. package/skills/create-verification-skill/references/feature-map-example/search.md +45 -0
  19. package/skills/figure-it-out/SKILL.md +55 -0
  20. package/skills/how/SKILL.md +24 -0
  21. package/skills/in-your-own-words/SKILL.md +10 -0
  22. package/skills/interrogate/SKILL.md +34 -0
  23. package/skills/ketchup/SKILL.md +46 -0
  24. package/skills/ketchup-video/SKILL.md +32 -0
  25. package/skills/maintain-formal-verification-skill/SKILL.md +61 -0
  26. package/skills/maintain-verification-skill/SKILL.md +42 -0
  27. package/skills/make-bot-ui/SKILL.md +118 -0
  28. package/skills/no-comments/SKILL.md +27 -0
  29. package/skills/poteto-help/SKILL.md +165 -0
  30. package/skills/poteto-help/references/prompting.md +51 -0
  31. package/skills/poteto-help/references/recipes.md +51 -0
  32. package/skills/poteto-mode/SKILL.md +111 -0
  33. package/skills/poteto-mode/playbooks/authoring-a-skill.md +12 -0
  34. package/skills/poteto-mode/playbooks/autonomous-run.md +13 -0
  35. package/skills/poteto-mode/playbooks/autopilot-full.md +25 -0
  36. package/skills/poteto-mode/playbooks/autopilot-stack.md +8 -0
  37. package/skills/poteto-mode/playbooks/babysit.md +10 -0
  38. package/skills/poteto-mode/playbooks/bug-fix.md +15 -0
  39. package/skills/poteto-mode/playbooks/eval.md +25 -0
  40. package/skills/poteto-mode/playbooks/feature.md +21 -0
  41. package/skills/poteto-mode/playbooks/hillclimb.md +21 -0
  42. package/skills/poteto-mode/playbooks/investigation.md +14 -0
  43. package/skills/poteto-mode/playbooks/multi-phase-plan.md +11 -0
  44. package/skills/poteto-mode/playbooks/opening-a-pr.md +9 -0
  45. package/skills/poteto-mode/playbooks/pause-safely.md +10 -0
  46. package/skills/poteto-mode/playbooks/perf-issue.md +25 -0
  47. package/skills/poteto-mode/playbooks/prototype.md +14 -0
  48. package/skills/poteto-mode/playbooks/refactoring.md +16 -0
  49. package/skills/poteto-mode/playbooks/runtime-forensics.md +11 -0
  50. package/skills/poteto-mode/playbooks/session-pickup.md +11 -0
  51. package/skills/poteto-mode/playbooks/shipping.md +12 -0
  52. package/skills/poteto-mode/playbooks/trace-forensics.md +14 -0
  53. package/skills/poteto-mode/playbooks/visual-parity.md +11 -0
  54. package/skills/poteto-mode/playbooks/worktree-cleanup.md +12 -0
  55. package/skills/poteto-mode/references/bugbot-triage.md +142 -0
  56. package/skills/poteto-mode/scripts/bootstrap.ts +62 -0
  57. package/skills/poteto-mode/scripts/bun.lock +67 -0
  58. package/skills/poteto-mode/scripts/package.json +16 -0
  59. package/skills/poteto-mode/scripts/watch-pr/cli.test.ts +224 -0
  60. package/skills/poteto-mode/scripts/watch-pr/cli.ts +223 -0
  61. package/skills/poteto-mode/scripts/watch-pr/fakes.test-helper.ts +129 -0
  62. package/skills/poteto-mode/scripts/watch-pr/github.test.ts +498 -0
  63. package/skills/poteto-mode/scripts/watch-pr/github.ts +726 -0
  64. package/skills/poteto-mode/scripts/watch-pr/policy.test.ts +482 -0
  65. package/skills/poteto-mode/scripts/watch-pr/policy.ts +842 -0
  66. package/skills/poteto-mode/scripts/watch-pr/render.ts +169 -0
  67. package/skills/poteto-mode/scripts/watch-pr/tsconfig.json +13 -0
  68. package/skills/poteto-mode/scripts/watch-pr/types.compile.ts +93 -0
  69. package/skills/poteto-mode/scripts/watch-pr/types.ts +414 -0
  70. package/skills/poteto-mode/scripts/watch-pr/watch-pr +6 -0
  71. package/skills/poteto-mode/scripts/worktree-audit.sh +134 -0
  72. package/skills/poteto-mode/worker.md +11 -0
  73. package/skills/principle-attack-the-premise/SKILL.md +18 -0
  74. package/skills/principle-boundary-discipline/SKILL.md +14 -0
  75. package/skills/principle-build-the-lever/SKILL.md +17 -0
  76. package/skills/principle-encode-lessons-in-structure/SKILL.md +14 -0
  77. package/skills/principle-exhaust-the-design-space/SKILL.md +12 -0
  78. package/skills/principle-experience-first/SKILL.md +22 -0
  79. package/skills/principle-explain-the-number/SKILL.md +17 -0
  80. package/skills/principle-fix-root-causes/SKILL.md +17 -0
  81. package/skills/principle-foundational-thinking/SKILL.md +16 -0
  82. package/skills/principle-guard-the-context-window/SKILL.md +15 -0
  83. package/skills/principle-laziness-protocol/SKILL.md +13 -0
  84. package/skills/principle-make-operations-idempotent/SKILL.md +18 -0
  85. package/skills/principle-migrate-callers-then-delete-legacy-apis/SKILL.md +17 -0
  86. package/skills/principle-minimize-reader-load/SKILL.md +18 -0
  87. package/skills/principle-model-the-domain/SKILL.md +18 -0
  88. package/skills/principle-never-block-on-the-human/SKILL.md +18 -0
  89. package/skills/principle-outcome-oriented-execution/SKILL.md +24 -0
  90. package/skills/principle-prove-it-works/SKILL.md +14 -0
  91. package/skills/principle-redesign-from-first-principles/SKILL.md +19 -0
  92. package/skills/principle-separate-before-serializing-shared-state/SKILL.md +14 -0
  93. package/skills/principle-sequence-verifiable-units/SKILL.md +16 -0
  94. package/skills/principle-subtract-before-you-add/SKILL.md +21 -0
  95. package/skills/principle-test-behavior-not-implementation/SKILL.md +19 -0
  96. package/skills/principle-type-system-discipline/SKILL.md +18 -0
  97. package/skills/recall/SKILL.md +38 -0
  98. package/skills/reflect/SKILL.md +20 -0
  99. package/skills/setup-pstack/SKILL.md +81 -0
  100. package/skills/sharpen/SKILL.md +19 -0
  101. package/skills/show-me-your-work/SKILL.md +85 -0
  102. package/skills/show-me-your-work/references/decision-log-template.tsv +1 -0
  103. package/skills/show-me-your-work/scripts/log.sh +42 -0
  104. package/skills/statechart/SKILL.md +85 -0
  105. package/skills/statechart/references/render.md +93 -0
  106. package/skills/statechart/scripts/backends/cfg.ts +33 -0
  107. package/skills/statechart/scripts/backends/mermaid.ts +249 -0
  108. package/skills/statechart/scripts/backends/plain-text.ts +72 -0
  109. package/skills/statechart/scripts/backends/report.ts +177 -0
  110. package/skills/statechart/scripts/frontends/config.ts +276 -0
  111. package/skills/statechart/scripts/frontends/dot.ts +74 -0
  112. package/skills/statechart/scripts/frontends/marks.ts +93 -0
  113. package/skills/statechart/scripts/frontends/messages.ts +52 -0
  114. package/skills/statechart/scripts/frontends/spec.ts +114 -0
  115. package/skills/statechart/scripts/frontends/trace.ts +24 -0
  116. package/skills/statechart/scripts/frontends/value.ts +166 -0
  117. package/skills/statechart/scripts/frontends/verdict.ts +40 -0
  118. package/skills/statechart/scripts/frontends/view.ts +119 -0
  119. package/skills/statechart/scripts/ir.ts +547 -0
  120. package/skills/statechart/scripts/package.json +11 -0
  121. package/skills/statechart/scripts/passes/annotate.ts +128 -0
  122. package/skills/statechart/scripts/passes/label.ts +269 -0
  123. package/skills/statechart/scripts/passes/lift.ts +85 -0
  124. package/skills/statechart/scripts/passes/loop-check.ts +68 -0
  125. package/skills/statechart/scripts/passes/paths.ts +60 -0
  126. package/skills/statechart/scripts/passes/reach.ts +135 -0
  127. package/skills/statechart/scripts/passes/region-check.ts +97 -0
  128. package/skills/statechart/scripts/passes/regions.ts +295 -0
  129. package/skills/statechart/scripts/pipeline.ts +76 -0
  130. package/skills/statechart/scripts/run.ts +506 -0
  131. package/skills/statechart/scripts/statechart +29 -0
  132. package/skills/statechart/scripts/statechart.ts +164 -0
  133. package/skills/statechart/scripts/tsconfig.json +11 -0
  134. package/skills/statechart/scripts/worker.ts +35 -0
  135. package/skills/swarm/SKILL.md +44 -0
  136. package/skills/tdd/SKILL.md +45 -0
  137. package/skills/teach/SKILL.md +24 -0
  138. package/skills/technical-writing/SKILL.md +73 -0
  139. package/skills/typescript-best-practices/SKILL.md +33 -0
  140. package/skills/typescript-best-practices/references/patterns.md +328 -0
  141. package/skills/unslop/SKILL.md +43 -0
  142. package/skills/why/SKILL.md +12 -0
@@ -0,0 +1,269 @@
1
+ import {
2
+ type Answers,
3
+ type Bug,
4
+ type Graph,
5
+ type Hidden,
6
+ isRecord,
7
+ key,
8
+ type Labels,
9
+ list,
10
+ loopOf,
11
+ must,
12
+ type Rule,
13
+ type Shape,
14
+ sortBy,
15
+ stateText,
16
+ type Step,
17
+ type Story,
18
+ tupleKey,
19
+ type Vars,
20
+ type View,
21
+ type Walk,
22
+ } from "../ir.ts";
23
+
24
+ const isLabel = (l: unknown): l is string =>
25
+ typeof l === "string" && l !== "" && !/\bundefined\b|\bNaN\b|\[object Object\]/.test(l);
26
+
27
+ const shapeOf = (shape: unknown, vars: Vars): Shape => {
28
+ function fail(): never {
29
+ throw new Error(
30
+ `The view's state() returned ${JSON.stringify(shape) ?? "undefined"} for the state ${stateText(vars)}. Return a string, an array of strings, or an object of those.`,
31
+ );
32
+ }
33
+ const path = (p: unknown) => {
34
+ const labels: string[] = [];
35
+ for (const l of [p].flat()) {
36
+ if (!isLabel(l)) fail();
37
+ labels.push(l);
38
+ }
39
+ if (!labels.length) fail();
40
+ return labels;
41
+ };
42
+ const regions: [string, unknown][] = isRecord(shape) ? Object.entries(shape) : [["", shape]];
43
+ const pairs: [string, string[]][] = [];
44
+ for (const [k, v] of regions) pairs.push([k, path(v)]);
45
+ return pairs.length ? Object.fromEntries(pairs) : fail();
46
+ };
47
+
48
+ const stepOf = (view: View) => (action: string, from: Vars, to: Vars) => {
49
+ const label = (view.step ?? ((a: string) => a))(action, from, to);
50
+ if (label === null || isLabel(label)) return label;
51
+ throw new Error(
52
+ `The view's step() returned ${JSON.stringify(label) ?? "undefined"} for the step ${action}.`,
53
+ );
54
+ };
55
+
56
+ function unlabeled(
57
+ action: string,
58
+ before: Vars,
59
+ after: Vars,
60
+ bug: { rule: Rule; loop?: number },
61
+ ): never {
62
+ const changed: string[] = [];
63
+ for (const k of Object.keys(after)) if (key(before[k]) !== key(after[k])) changed.push(k);
64
+ throw new Error(
65
+ `The view's step() returned null for the step ${action} that changes ${changed.join(", ") || "nothing"}. It is on the path to the ${bug.rule.name} bug${bug.loop === undefined ? "" : " or leaves its loop"}, so label it.`,
66
+ );
67
+ }
68
+
69
+ export function ask(graph: Graph, { states: tla, nameOf, from, to }: Walk, view: View): Answers {
70
+ const n = nameOf.length;
71
+ const m = from.length;
72
+ const state = view.state;
73
+ const shapes = new Array<Shape>(n);
74
+ for (let i = 0; i < n; i++)
75
+ shapes[i] = shapeOf(state ? state(tla[i].vars) : nameOf[i], tla[i].vars);
76
+ const names = Object.keys(shapes[0] ?? { "": [] });
77
+ const regionsKey = tupleKey(names);
78
+ for (let i = 0; i < n; i++)
79
+ if (tupleKey(Object.keys(shapes[i])) !== regionsKey)
80
+ throw new Error(
81
+ `The view's state() returned the regions ${names.join(", ")} for one state and ${Object.keys(shapes[i]).join(", ")} for the state ${stateText(tla[i].vars)}. Return the same regions for every state.`,
82
+ );
83
+ const step = stepOf(view);
84
+ const labels = new Array<string | Hidden>(m);
85
+ for (let j = 0; j < m; j++)
86
+ labels[j] = step(graph.steps[j].action, tla[from[j]].vars, tla[to[j]].vars);
87
+ const groups = new Array<unknown>(n);
88
+ const asked = new Uint8Array(n);
89
+ const group = (i: number) => {
90
+ if (view.group && !asked[i]) {
91
+ groups[i] = view.group(tla[i].vars);
92
+ asked[i] = 1;
93
+ }
94
+ return groups[i];
95
+ };
96
+ return { names, shapes, labels, group };
97
+ }
98
+
99
+ export function asParents(
100
+ { states: tla }: Walk,
101
+ { names, shapes, labels, group }: Answers,
102
+ ): Answers {
103
+ const parents = new Array<Shape>(shapes.length);
104
+ for (let i = 0; i < shapes.length; i++) {
105
+ const path: string[] = [];
106
+ for (const n of names) for (const p of shapes[i][n]) path.push(`${n} ${p}`);
107
+ parents[i] = shapeOf(path, tla[i].vars);
108
+ }
109
+ return { names: [""], shapes: parents, labels, group };
110
+ }
111
+
112
+ export function labelGraph(
113
+ graph: Graph,
114
+ { index, from, to, action }: Walk,
115
+ answers: Answers,
116
+ rank: Int32Array,
117
+ found: Bug[],
118
+ ): Labels {
119
+ const { names, shapes, labels: stepLabels } = answers;
120
+ const n = shapes.length;
121
+ const m = stepLabels.length;
122
+ const onPath = new Set<Step>();
123
+ for (const b of found) for (const p of b.path) onPath.add(p.step);
124
+ const pathStepIndex = new Map<Step, number>();
125
+ if (onPath.size)
126
+ for (let j = 0; j < m; j++)
127
+ if (onPath.has(graph.steps[j])) pathStepIndex.set(graph.steps[j], j);
128
+ const R = names.length;
129
+ const keys: string[][] = [];
130
+ const kinds: Int32Array[] = [];
131
+ const sizes: number[] = [];
132
+ for (const name of names) {
133
+ const key = new Array<string>(n);
134
+ const kind = new Int32Array(n);
135
+ const tops = new Map<string, number>();
136
+ const nested = new Map<string, number>();
137
+ const keyOf: string[] = [];
138
+ for (let i = 0; i < n; i++) {
139
+ const path = shapes[i][name];
140
+ const top = path.length === 1;
141
+ const seen = top ? tops : nested;
142
+ const k = top ? path[0] : tupleKey(path);
143
+ let c = seen.get(k);
144
+ if (c === undefined) {
145
+ seen.set(k, (c = keyOf.length));
146
+ keyOf.push(top ? tupleKey(path) : k);
147
+ }
148
+ key[i] = keyOf[c];
149
+ kind[i] = c;
150
+ }
151
+ keys.push(key);
152
+ kinds.push(kind);
153
+ sizes.push(keyOf.length);
154
+ }
155
+ for (const b of found) {
156
+ let t: Step | undefined;
157
+ for (const p of b.path)
158
+ if (stepLabels[must(pathStepIndex, p.step)] === null) {
159
+ t = p.step;
160
+ break;
161
+ }
162
+ if (!t) {
163
+ const inLoop = loopOf(b, index);
164
+ if (inLoop.size)
165
+ for (let j = 0; j < m; j++)
166
+ if (inLoop.has(from[j]) && !inLoop.has(to[j]) && stepLabels[j] === null) {
167
+ t = graph.steps[j];
168
+ break;
169
+ }
170
+ }
171
+ if (t) unlabeled(t.action, must(graph.states, t.from).vars, must(graph.states, t.to).vars, b);
172
+ }
173
+ const drawn: number[] = [];
174
+ const drawnLabels = new Array<string>(m);
175
+ for (let j = 0; j < m; j++) {
176
+ const label = stepLabels[j];
177
+ if (label === null) continue;
178
+ drawn.push(j);
179
+ drawnLabels[j] = label;
180
+ }
181
+ const fromRank = new Int32Array(m);
182
+ const toRank = new Int32Array(m);
183
+ for (let j = 0; j < m; j++) {
184
+ fromRank[j] = rank[from[j]];
185
+ toRank[j] = rank[to[j]];
186
+ }
187
+ const sorted = sortBy(Int32Array.from(drawn), [fromRank, toRank, action]);
188
+ const pathSteps = new Uint8Array(m);
189
+ for (const j of pathStepIndex.values()) pathSteps[j] = 1;
190
+ const byLabel = new Map<string, { changed: Uint8Array; sets: Set<string> }>();
191
+ if (R > 1)
192
+ for (const j of sorted) {
193
+ let entry = byLabel.get(drawnLabels[j]);
194
+ if (!entry)
195
+ byLabel.set(drawnLabels[j], (entry = { changed: new Uint8Array(R), sets: new Set() }));
196
+ const changed: string[] = [];
197
+ for (let r = 0; r < R; r++)
198
+ if (kinds[r][from[j]] !== kinds[r][to[j]]) {
199
+ changed.push(names[r]);
200
+ entry.changed[r] = 1;
201
+ }
202
+ if (changed.length > 1) entry.sets.add(list(changed));
203
+ }
204
+ const together = new Set<string>();
205
+ for (const [text, { sets }] of byLabel) if (sets.size) together.add(text);
206
+ const notes: string[] = [];
207
+ for (const [text, entry] of byLabel) {
208
+ const changed: string[] = [];
209
+ for (let r = 0; r < R; r++) if (entry.changed[r]) changed.push(names[r]);
210
+ if (changed.length < 2) continue;
211
+ if (!together.has(text)) {
212
+ notes.push(`The step ${text} changes only one of ${list(changed)} at a time.`);
213
+ continue;
214
+ }
215
+ const sets = [...entry.sets];
216
+ notes.push(
217
+ `The step ${text} changes ${sets.length > 1 ? `${sets.join(", or ")},` : sets[0]} at the same time.`,
218
+ );
219
+ }
220
+ return {
221
+ ...answers,
222
+ drawnLabels,
223
+ keys,
224
+ kinds,
225
+ sizes,
226
+ sorted,
227
+ pathStepIndex,
228
+ pathSteps,
229
+ together,
230
+ notes,
231
+ };
232
+ }
233
+
234
+ type Told<Id> = { rule: Rule; start: Id; path: { action: string; state: Id }[]; loop?: number };
235
+
236
+ function tell<Id, B extends Told<Id>>(
237
+ states: ReadonlyMap<Id, { vars: Vars }>,
238
+ found: B[],
239
+ shapeAt: (id: Id) => Shape,
240
+ labelAt: (bug: B, k: number, from: Id) => string | Hidden,
241
+ ) {
242
+ const vars = (id: Id) => must(states, id).vars;
243
+ const stories = new Map<B, Story>();
244
+ for (const bug of found) {
245
+ const story: Story = [{ state: shapeAt(bug.start) }];
246
+ let from: Id = bug.start;
247
+ for (const [k, p] of bug.path.entries()) {
248
+ const step = labelAt(bug, k, from);
249
+ if (step === null) unlabeled(p.action, vars(from), vars(p.state), bug);
250
+ story.push({ step, state: shapeAt(p.state) });
251
+ from = p.state;
252
+ }
253
+ stories.set(bug, story);
254
+ }
255
+ return stories;
256
+ }
257
+
258
+ export const storiesOf = (
259
+ graph: Graph,
260
+ found: Bug[],
261
+ { index }: Walk,
262
+ { shapes, drawnLabels, pathStepIndex }: Labels,
263
+ ) =>
264
+ tell(
265
+ graph.states,
266
+ found,
267
+ (id) => shapes[must(index, id)],
268
+ (bug, k) => drawnLabels[must(pathStepIndex, bug.path[k].step)],
269
+ );
@@ -0,0 +1,85 @@
1
+ import {
2
+ append,
3
+ type Chart,
4
+ type ChartState,
5
+ type ChartStep,
6
+ must,
7
+ type Region,
8
+ tupleKey,
9
+ unkey,
10
+ } from "../ir.ts";
11
+
12
+ export function liftToParents(states: ChartState[], steps: ChartStep[]) {
13
+ const depths = new Map<string, number>();
14
+ const leaves = new Map<string, number>();
15
+ for (const s of states.values())
16
+ for (let i = 1; i < s.path.length; i++) {
17
+ const prefix = tupleKey(s.path.slice(0, i));
18
+ depths.set(prefix, i);
19
+ leaves.set(prefix, (leaves.get(prefix) ?? 0) + 1);
20
+ }
21
+ if (!depths.size) return steps;
22
+ const leaving = new Map<string, ChartStep[]>();
23
+ for (const s of steps) {
24
+ let same = 0;
25
+ while (same < s.from.length && s.from[same] === s.to[same]) same++;
26
+ for (let i = 1; i < s.from.length; i++) {
27
+ if (i <= same && i < s.to.length) continue;
28
+ const prefix = tupleKey(s.from.slice(0, i));
29
+ if (depths.has(prefix)) append(leaving, prefix, s);
30
+ }
31
+ }
32
+ const removed = new Set<ChartStep>();
33
+ const lifted: ChartStep[] = [];
34
+ for (const [prefix] of [...depths].sort((a, b) => a[1] - b[1])) {
35
+ const groups = new Map<string, ChartStep[]>();
36
+ for (const s of leaving.get(prefix) ?? [])
37
+ if (!removed.has(s)) append(groups, tupleKey([s.label, s.when, s.to]), s);
38
+ const under = must(leaves, prefix);
39
+ for (const group of groups.values()) {
40
+ if (under < 2 || group.length < under) continue;
41
+ for (const s of group) removed.add(s);
42
+ lifted.push({ ...group[0], from: unkey(prefix) });
43
+ }
44
+ }
45
+ const kept: ChartStep[] = [];
46
+ for (const s of steps) if (!removed.has(s)) kept.push(s);
47
+ for (const s of lifted) kept.push(s);
48
+ return kept;
49
+ }
50
+
51
+ export function lift(chart: Chart): Chart {
52
+ const regions: Region[] = [];
53
+ for (const r of chart.regions) {
54
+ const known = new Map<string[], number>();
55
+ for (const s of r.states) known.set(s.path, known.size);
56
+ let numbers: Map<string, number> | undefined;
57
+ const pathIndex = (path: string[]) => {
58
+ let i = known.get(path);
59
+ if (i !== undefined) return i;
60
+ if (!numbers) {
61
+ numbers = new Map();
62
+ for (const s of r.states) numbers.set(tupleKey(s.path), numbers.size);
63
+ }
64
+ const k = tupleKey(path);
65
+ i = numbers.get(k);
66
+ if (i === undefined) numbers.set(k, (i = numbers.size));
67
+ known.set(path, i);
68
+ return i;
69
+ };
70
+ const merged = new Map<number | string, ChartStep>();
71
+ for (const s of liftToParents(r.states, r.steps)) {
72
+ const f = pathIndex(s.from);
73
+ const t = pathIndex(s.to);
74
+ const k =
75
+ s.when === undefined && f < 2 ** 26 && t < 2 ** 26
76
+ ? f * 2 ** 26 + t
77
+ : `${f},${t}:${s.when}`;
78
+ const prior = merged.get(k);
79
+ if (prior) prior.label += ` or ${s.label}`;
80
+ else merged.set(k, { ...s });
81
+ }
82
+ regions.push({ ...r, steps: [...merged.values()] });
83
+ }
84
+ return { ...chart, regions };
85
+ }
@@ -0,0 +1,68 @@
1
+ import {
2
+ append,
3
+ type Arrow,
4
+ type Bug,
5
+ type Chart,
6
+ holds,
7
+ type Labels,
8
+ loopOf,
9
+ must,
10
+ tupleKey,
11
+ unkey,
12
+ type Walk,
13
+ } from "../ir.ts";
14
+
15
+ export function loopCheck(
16
+ { regions, loops }: Chart,
17
+ { names, keys, drawnLabels, sorted }: Labels,
18
+ { index, from, to }: Walk,
19
+ found: Bug[],
20
+ ) {
21
+ const R = names.length;
22
+ const combo = (i: number) => {
23
+ const parts: string[] = [];
24
+ for (let r = 0; r < R; r++) parts.push(keys[r][i]);
25
+ return tupleKey(parts);
26
+ };
27
+ const stepsFrom = new Map<number, number[]>();
28
+ const arrowsFrom: Map<string, Arrow[]>[] = [];
29
+ if (loops.length) {
30
+ for (const j of sorted) append(stepsFrom, from[j], j);
31
+ for (const { arrows } of regions) {
32
+ const out = new Map<string, Arrow[]>();
33
+ for (const a of arrows) append(out, a.from, a);
34
+ arrowsFrom.push(out);
35
+ }
36
+ }
37
+ for (const b of found) {
38
+ if (b.loop === undefined) continue;
39
+ const inLoop = new Set<string>();
40
+ const twinsOf = new Map<string, number[]>();
41
+ for (const i of loopOf(b, index)) {
42
+ inLoop.add(combo(i));
43
+ append(twinsOf, combo(i), i);
44
+ }
45
+ for (const i of loopOf(b, index)) {
46
+ const twins = must(twinsOf, combo(i));
47
+ for (const [r, name] of names.entries()) {
48
+ const takes = (a: Arrow) => {
49
+ for (const s of twins)
50
+ for (const j of stepsFrom.get(s) ?? [])
51
+ if (drawnLabels[j] === a.label && keys[r][to[j]] === a.to) return true;
52
+ return false;
53
+ };
54
+ const rest: string[] = [];
55
+ for (let o = 0; o < R; o++) if (o !== r) rest.push(keys[o][i]);
56
+ for (const a of arrowsFrom[r].get(keys[r][i]) ?? []) {
57
+ if (!holds(a.condition, rest)) continue;
58
+ const next: string[] = [];
59
+ for (let o = 0; o < R; o++) next.push(o === r ? a.to : keys[o][i]);
60
+ if (!inLoop.has(tupleKey(next)) && !takes(a))
61
+ throw new Error(
62
+ `The chart draws the step ${a.label} out of ${name ? `${name} ` : ""}${unkey(a.from).join(" / ")}, but the ${b.rule.name} loop cannot take it there. Show in a label what stops it.`,
63
+ );
64
+ }
65
+ }
66
+ }
67
+ }
68
+ }
@@ -0,0 +1,60 @@
1
+ import {
2
+ type Bug,
3
+ type Graph,
4
+ hasRule,
5
+ type Lasso,
6
+ must,
7
+ type PathStep,
8
+ type Rule,
9
+ stateKey,
10
+ type Step,
11
+ type Trace,
12
+ type TraceState,
13
+ type Vars,
14
+ } from "../ir.ts";
15
+
16
+ export function pathGraph(names: string[]) {
17
+ const graph: Graph = { states: new Map(), steps: [] };
18
+ const ids = new Map<string, string>();
19
+ const taken = new Map<string, Step>();
20
+ const idOf = (vars: Vars | undefined, start: boolean) => {
21
+ if (!vars) throw new Error("TLC's trace has a state with no values.");
22
+ const k = stateKey(names.map((name) => vars[name]));
23
+ let id = ids.get(k);
24
+ if (id === undefined) {
25
+ ids.set(k, (id = String(ids.size)));
26
+ graph.states.set(id, { vars, start, breaks: [] });
27
+ } else if (start) must(graph.states, id).start = true;
28
+ return id;
29
+ };
30
+ const route = (seq: { action: string; vars: Vars | undefined }[]) => {
31
+ const start = idOf(seq[0].vars, true);
32
+ const path: PathStep[] = [];
33
+ let from = start;
34
+ for (let i = 1; i < seq.length; i++) {
35
+ const { action } = seq[i];
36
+ const to = idOf(seq[i].vars, false);
37
+ const k = `${from} ${to} ${action}`;
38
+ let step = taken.get(k);
39
+ if (!step) {
40
+ taken.set(k, (step = { from, to, action }));
41
+ graph.steps.push(step);
42
+ }
43
+ path.push({ action, state: to, step });
44
+ from = to;
45
+ }
46
+ return { start, path };
47
+ };
48
+ return {
49
+ graph,
50
+ bug: (rule: Rule, trace: TraceState[]): Bug => {
51
+ if (!trace.length) throw new Error(`TLC's trace for ${rule.name} has no states.`);
52
+ const bug = { rule, ...route(trace) };
53
+ const end = must(graph.states, bug.path.at(-1)?.state ?? bug.start);
54
+ if (!hasRule(end.breaks, rule)) end.breaks.push(rule);
55
+ return bug;
56
+ },
57
+ lasso: ({ seq, loop }: Trace): Lasso | undefined =>
58
+ seq.length ? { ...route(seq), loop, stutters: loop === seq.length - 1 } : undefined,
59
+ };
60
+ }
@@ -0,0 +1,135 @@
1
+ import {
2
+ type Bug,
3
+ byText,
4
+ count,
5
+ defaultState,
6
+ type Graph,
7
+ must,
8
+ type PathStep,
9
+ type Rule,
10
+ ruleKey,
11
+ sortBy,
12
+ type State,
13
+ type Walk,
14
+ } from "../ir.ts";
15
+
16
+ export function walk(graph: Graph): Walk {
17
+ const ids: string[] = [];
18
+ const states: State[] = [];
19
+ const nameOf: string[] = [];
20
+ const starts: number[] = [];
21
+ const unique = new Set<string>();
22
+ for (const [id, s] of graph.states) {
23
+ const name = defaultState(s.vars);
24
+ if (s.start) starts.push(ids.length);
25
+ ids.push(id);
26
+ states.push(s);
27
+ nameOf.push(name);
28
+ unique.add(name);
29
+ }
30
+ const names = Array.from(unique).sort(byText);
31
+ const rankOf = new Map<string, number>();
32
+ for (let i = 0; i < names.length; i++) rankOf.set(names[i], i);
33
+ const n = ids.length;
34
+ const index = new Map<string, number>();
35
+ const nameRank = new Int32Array(n);
36
+ for (let i = 0; i < n; i++) {
37
+ index.set(ids[i], i);
38
+ nameRank[i] = must(rankOf, nameOf[i]);
39
+ }
40
+ const m = graph.steps.length;
41
+ const from = new Int32Array(m);
42
+ const to = new Int32Array(m);
43
+ const action = new Int32Array(m);
44
+ const actions = new Map<string, number>();
45
+ for (let j = 0; j < m; j++) {
46
+ const t = graph.steps[j];
47
+ from[j] = must(index, t.from);
48
+ to[j] = must(index, t.to);
49
+ let a = actions.get(t.action);
50
+ if (a === undefined) actions.set(t.action, (a = actions.size));
51
+ action[j] = a;
52
+ }
53
+ const actionRank = new Int32Array(actions.size);
54
+ for (const [i, a] of [...actions.keys()].sort(byText).entries()) actionRank[must(actions, a)] = i;
55
+ const toName = new Int32Array(m);
56
+ for (let j = 0; j < m; j++) {
57
+ action[j] = actionRank[action[j]];
58
+ toName[j] = nameRank[to[j]];
59
+ }
60
+ const first = new Int32Array(n + 1);
61
+ for (let j = 0; j < m; j++) first[from[j] + 1]++;
62
+ for (let i = 0; i < n; i++) first[i + 1] += first[i];
63
+ const fill = first.slice(0, n);
64
+ const next = new Int32Array(m);
65
+ for (const j of sortBy(count(m), [action, toName])) next[fill[from[j]]++] = j;
66
+ starts.sort((a, b) => nameRank[a] - nameRank[b]);
67
+ const depth = new Int32Array(n).fill(-1);
68
+ const via = new Int32Array(n).fill(-1);
69
+ const reached = new Int32Array(n);
70
+ let size = 0;
71
+ for (const s of starts) {
72
+ depth[s] = 0;
73
+ reached[size++] = s;
74
+ }
75
+ for (let q = 0; q < size; q++) {
76
+ const s = reached[q];
77
+ for (let e = first[s]; e < first[s + 1]; e++) {
78
+ const t = to[next[e]];
79
+ if (depth[t] >= 0) continue;
80
+ depth[t] = depth[s] + 1;
81
+ via[t] = next[e];
82
+ reached[size++] = t;
83
+ }
84
+ }
85
+ return {
86
+ ids,
87
+ states,
88
+ index,
89
+ nameOf,
90
+ nameRank,
91
+ from,
92
+ to,
93
+ action,
94
+ depth,
95
+ via,
96
+ reached: reached.subarray(0, size),
97
+ starts,
98
+ };
99
+ }
100
+
101
+ export function drawOrder({ depth, nameRank }: Walk) {
102
+ const n = depth.length;
103
+ let deepest = 0;
104
+ for (let i = 0; i < n; i++) if (depth[i] > deepest) deepest = depth[i];
105
+ const level = new Int32Array(n);
106
+ for (let i = 0; i < n; i++) level[i] = depth[i] < 0 ? deepest + 1 : depth[i];
107
+ const order = sortBy(count(n), [level, nameRank]);
108
+ const rank = new Int32Array(n);
109
+ for (let p = 0; p < n; p++) rank[order[p]] = p;
110
+ return { order, rank };
111
+ }
112
+
113
+ export function bugs(graph: Graph, { ids, from, via, reached }: Walk = walk(graph)) {
114
+ const ends = new Map<string, string>();
115
+ for (const [id, s] of graph.states) for (const rule of s.ends ?? []) ends.set(ruleKey(rule), id);
116
+ const first = new Map<string, { rule: Rule; i: number }>();
117
+ for (const i of reached) {
118
+ const s = must(graph.states, ids[i]);
119
+ for (const rule of s.past ? s.ends : s.breaks) {
120
+ const k = ruleKey(rule);
121
+ if (!first.has(k) && (ends.get(k) ?? ids[i]) === ids[i]) first.set(k, { rule, i });
122
+ }
123
+ }
124
+ const found: Bug[] = [];
125
+ for (const { rule, i } of first.values()) {
126
+ const path: PathStep[] = [];
127
+ let start = i;
128
+ for (let j: number; (j = via[start]) >= 0; start = from[j]) {
129
+ const step = graph.steps[j];
130
+ path.unshift({ action: step.action, state: step.to, step });
131
+ }
132
+ found.push({ rule, start: ids[start], path });
133
+ }
134
+ return found;
135
+ }