@open-tomato/rafa 0.47.0 → 0.49.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/README.md +5 -2
  2. package/dist/bugs/places.d.ts +1 -1
  3. package/dist/bundled/skills/dev-planner/SKILL.md +1 -1
  4. package/dist/cli.js +58774 -58722
  5. package/dist/config/index.js +4 -5
  6. package/dist/config/shape.d.ts +13 -11
  7. package/dist/config-schema-board-project.d.ts +3 -3
  8. package/dist/config-schema-checks.d.ts +3 -3
  9. package/dist/config-schema-hub.d.ts +3 -3
  10. package/dist/config-schema-issues.d.ts +4 -4
  11. package/dist/config-schema-loop-continue.d.ts +3 -3
  12. package/dist/config-schema-loop-retries.d.ts +4 -4
  13. package/dist/config-schema-release.d.ts +13 -13
  14. package/dist/config-schema-tests.d.ts +3 -3
  15. package/dist/config-schema-triage.d.ts +3 -3
  16. package/dist/config-schema-wrap-up.d.ts +4 -4
  17. package/dist/core/command/global-flags.d.ts +25 -0
  18. package/dist/core/command/index.d.ts +31 -25
  19. package/dist/core/command/registry-types.d.ts +4 -4
  20. package/dist/core/command/shared-flags.d.ts +6 -6
  21. package/dist/core/command/types.d.ts +3 -2
  22. package/dist/core/config/branch-template.d.ts +2 -0
  23. package/dist/core/config/config-items.d.ts +1 -1
  24. package/dist/core/config/config-schema.d.ts +6 -0
  25. package/dist/core/config/config-sections.d.ts +15 -14
  26. package/dist/core/config/config.d.ts +1 -1
  27. package/dist/core/config/core-section.d.ts +1 -1
  28. package/dist/core/config/default-routing.d.ts +1 -1
  29. package/dist/core/config/index.d.ts +4 -4
  30. package/dist/core/config/merge-method.d.ts +4 -4
  31. package/dist/core/config/sections.d.ts +6 -3
  32. package/dist/core/git/gh-runner.d.ts +1 -1
  33. package/dist/core/git/git.d.ts +34 -0
  34. package/dist/core/git/index.d.ts +13 -8
  35. package/dist/core/loop/claude.d.ts +11 -5
  36. package/dist/core/loop/index.d.ts +3 -2
  37. package/dist/core/loop/sessions.d.ts +1 -1
  38. package/dist/core/loop/suite-failure.d.ts +8 -6
  39. package/dist/core/output/active.d.ts +1 -1
  40. package/dist/core/runtime/identity.d.ts +13 -0
  41. package/dist/core/runtime/index.d.ts +12 -6
  42. package/dist/effort/store/check-rows.d.ts +1 -1
  43. package/dist/effort/store/commit-stats.d.ts +1 -1
  44. package/dist/effort/store/index.js +8 -9
  45. package/dist/effort/store/merge-commit-gaps.d.ts +1 -1
  46. package/dist/effort/store/plan-stubs.d.ts +1 -1
  47. package/dist/effort/store/session-row-types.d.ts +2 -2
  48. package/dist/{index-f1y86zbb.js → index-2a7nb3an.js} +31 -31
  49. package/dist/index-ajv88fkm.js +448 -0
  50. package/dist/{index-r5q2pdky.js → index-fpgmdmww.js} +4 -1
  51. package/dist/{index-pzpb1rf7.js → index-g91ys6kw.js} +97 -1
  52. package/dist/{index-7dgc5g1t.js → index-j154qpbj.js} +8696 -11193
  53. package/dist/{index-cbqvgda6.js → index-m672ptpg.js} +3 -1
  54. package/dist/index-thqpre1b.js +670 -0
  55. package/dist/{index-eay1bd2w.js → index-vfny3ryr.js} +1090 -652
  56. package/dist/{index-nwzx41g1.js → index-w5qbpdtp.js} +416 -843
  57. package/dist/{index-n3e31fmw.js → index-zsak0jy6.js} +172 -254
  58. package/dist/index.js +24867 -18958
  59. package/dist/learning/index.js +221 -12
  60. package/dist/plan/index.js +11 -10
  61. package/dist/ports/index.d.ts +6 -6
  62. package/dist/ports/learning.d.ts +5 -4
  63. package/dist/ports/spec-review.d.ts +1 -1
  64. package/dist/ready-mh5wdzq5.js +241 -0
  65. package/dist/release-templates/release-bot-fragment.yml +6 -6
  66. package/dist/release-templates/release-publish.yml +8 -8
  67. package/dist/release-templates/release-settle.yml +8 -7
  68. package/dist/release-templates/release-tag.yml +8 -7
  69. package/package.json +32 -2
  70. package/dist/index-38ktnyvz.js +0 -223
  71. package/dist/index-3mwsr6wg.js +0 -1070
  72. package/dist/index-z1w83f81.js +0 -4
  73. package/dist/ready-6p40c3gw.js +0 -2007
@@ -1,13 +1,270 @@
1
1
  // @bun
2
2
  import {
3
- activeOutput,
4
- isStampableStub
5
- } from "./index-f1y86zbb.js";
3
+ parsePlan,
4
+ readRafaBlocks
5
+ } from "./index-ajv88fkm.js";
6
+ import {
7
+ activeOutput
8
+ } from "./index-2a7nb3an.js";
6
9
  import {
7
10
  messageOf,
8
11
  parseTaskDeclaration
9
- } from "./index-r5q2pdky.js";
10
- // src/preflight/prerequisites-md.ts
12
+ } from "./index-fpgmdmww.js";
13
+
14
+ // packages/rafa/src/report/parse.ts
15
+ var REPORT_STATUSES = ["done", "blocked"];
16
+ var FINDING_KINDS = ["gotcha", "pattern", "location", "skill-suggestion"];
17
+ var FINDING_SIGNALS = ["loud", "silent"];
18
+ var FINDING_DOMAINS = [
19
+ "code-style",
20
+ "testing",
21
+ "git",
22
+ "debugging",
23
+ "workflow",
24
+ "security",
25
+ "general-best-practices"
26
+ ];
27
+ var CHANGE_LEVELS = ["patch", "minor", "major", "none"];
28
+ var BUG_KINDS = ["bug", "own-fallout", "own-gap", "convention", "docs", "machine"];
29
+ var DEFAULT_BUG_KIND = "bug";
30
+ var REPORT_KIND = "report";
31
+ var REQUIRED = true;
32
+ var OPTIONAL = false;
33
+ var REPORT_KEYS = [
34
+ "status",
35
+ "feedback",
36
+ "findings",
37
+ "skills_used",
38
+ "blockers",
39
+ "out_of_scope_bugs",
40
+ "changes",
41
+ "stale_docs"
42
+ ];
43
+ var FINDING_KEYS = [
44
+ "trigger",
45
+ "kind",
46
+ "what",
47
+ "cause",
48
+ "resolution",
49
+ "artifact",
50
+ "signal",
51
+ "domain"
52
+ ];
53
+ var BLOCKER_KEYS = ["what", "artifact"];
54
+ var BUG_KEYS = ["what", "artifact", "security", "kind", "module", "action", "reproduce", "cause"];
55
+ var CHANGE_KEYS = ["level", "area", "summary"];
56
+ var STALE_DOC_KEYS = ["where", "what"];
57
+ function isMapping(value) {
58
+ return typeof value === "object" && value !== null && !Array.isArray(value);
59
+ }
60
+ function isOneOf(choices, value) {
61
+ return choices.includes(value);
62
+ }
63
+ function describeValue(value) {
64
+ if (typeof value === "string")
65
+ return JSON.stringify(value);
66
+ if (value === null)
67
+ return "nothing";
68
+ if (Array.isArray(value))
69
+ return "a list";
70
+ if (typeof value === "object")
71
+ return "a mapping";
72
+ return `the ${typeof value} ${String(value)}`;
73
+ }
74
+ function messageOf2(error) {
75
+ return error instanceof Error ? error.message : String(error);
76
+ }
77
+ function reportIssue(reason, field, text) {
78
+ return { reason, field, text };
79
+ }
80
+ function absent(reason, block, text) {
81
+ return { present: false, reason, block, text };
82
+ }
83
+ function scopeOf(mapping, keys, prefix, issues) {
84
+ const fields = new Map;
85
+ const extras = [];
86
+ for (const [key, value] of Object.entries(mapping)) {
87
+ if (keys.includes(key))
88
+ fields.set(key, value);
89
+ else
90
+ extras.push({ key, value });
91
+ }
92
+ return { fields, extras, prefix, issues };
93
+ }
94
+ function valueOf(scope, key, required) {
95
+ const value = scope.fields.get(key) ?? null;
96
+ if (value === null && required) {
97
+ const field = `${scope.prefix}${key}`;
98
+ scope.issues.push(reportIssue("missing-field", field, `${field} is missing`));
99
+ }
100
+ return value;
101
+ }
102
+ function unusable(scope, key, why) {
103
+ const field = `${scope.prefix}${key}`;
104
+ scope.issues.push(reportIssue("unusable-field", field, `${field} ${why}`));
105
+ return null;
106
+ }
107
+ function stringProblem(value) {
108
+ if (typeof value !== "string")
109
+ return `is ${describeValue(value)}, not a string; quote it`;
110
+ if (value.trim().length === 0)
111
+ return "is blank";
112
+ return null;
113
+ }
114
+ function stringField(scope, key, required) {
115
+ const value = valueOf(scope, key, required);
116
+ if (value === null)
117
+ return null;
118
+ const problem = stringProblem(value);
119
+ return problem === null ? value : unusable(scope, key, problem);
120
+ }
121
+ function choiceField(scope, key, choices, required) {
122
+ const value = valueOf(scope, key, required);
123
+ if (value === null || isOneOf(choices, value))
124
+ return value;
125
+ return unusable(scope, key, `is ${describeValue(value)}, not one of ${choices.join(", ")}`);
126
+ }
127
+ function booleanField(scope, key, required) {
128
+ const value = valueOf(scope, key, required);
129
+ if (value === null || typeof value === "boolean")
130
+ return value;
131
+ return unusable(scope, key, `is ${describeValue(value)}, not true or false; write it unquoted`);
132
+ }
133
+ function listField(scope, key, readEntry) {
134
+ const value = valueOf(scope, key, OPTIONAL);
135
+ if (value === null)
136
+ return [];
137
+ if (!Array.isArray(value)) {
138
+ unusable(scope, key, `is ${describeValue(value)}, not a list`);
139
+ return [];
140
+ }
141
+ const entries = [];
142
+ const items = value;
143
+ for (const [index, item] of items.entries()) {
144
+ const entry = readEntry(item, `${scope.prefix}${key}[${index}]`, scope.issues);
145
+ if (entry !== null)
146
+ entries.push(entry);
147
+ }
148
+ return entries;
149
+ }
150
+ function entryScope(item, field, keys, issues) {
151
+ if (isMapping(item))
152
+ return scopeOf(item, keys, `${field}.`, issues);
153
+ const text = `${field} is ${describeValue(item)}, not a mapping of ${keys.join(", ")}; dropped`;
154
+ issues.push(reportIssue("unusable-field", field, text));
155
+ return null;
156
+ }
157
+ function readSkill(item, field, issues) {
158
+ const problem = stringProblem(item);
159
+ if (problem === null)
160
+ return item;
161
+ issues.push(reportIssue("unusable-field", field, `${field} ${problem}; dropped`));
162
+ return null;
163
+ }
164
+ function readFinding(item, field, issues) {
165
+ const scope = entryScope(item, field, FINDING_KEYS, issues);
166
+ if (scope === null)
167
+ return null;
168
+ return {
169
+ trigger: stringField(scope, "trigger", REQUIRED),
170
+ kind: choiceField(scope, "kind", FINDING_KINDS, REQUIRED),
171
+ what: stringField(scope, "what", REQUIRED),
172
+ cause: stringField(scope, "cause", OPTIONAL),
173
+ resolution: stringField(scope, "resolution", OPTIONAL),
174
+ artifact: stringField(scope, "artifact", OPTIONAL),
175
+ signal: choiceField(scope, "signal", FINDING_SIGNALS, REQUIRED),
176
+ domain: choiceField(scope, "domain", FINDING_DOMAINS, OPTIONAL),
177
+ extras: scope.extras
178
+ };
179
+ }
180
+ function readBlocker(item, field, issues) {
181
+ const scope = entryScope(item, field, BLOCKER_KEYS, issues);
182
+ if (scope === null)
183
+ return null;
184
+ return {
185
+ what: stringField(scope, "what", REQUIRED),
186
+ artifact: stringField(scope, "artifact", OPTIONAL),
187
+ extras: scope.extras
188
+ };
189
+ }
190
+ function readBug(item, field, issues) {
191
+ const scope = entryScope(item, field, BUG_KEYS, issues);
192
+ if (scope === null)
193
+ return null;
194
+ return {
195
+ what: stringField(scope, "what", REQUIRED),
196
+ artifact: stringField(scope, "artifact", OPTIONAL),
197
+ security: booleanField(scope, "security", REQUIRED),
198
+ kind: choiceField(scope, "kind", BUG_KINDS, OPTIONAL) ?? DEFAULT_BUG_KIND,
199
+ module: stringField(scope, "module", OPTIONAL),
200
+ action: stringField(scope, "action", OPTIONAL),
201
+ reproduce: stringField(scope, "reproduce", OPTIONAL),
202
+ cause: stringField(scope, "cause", OPTIONAL),
203
+ extras: scope.extras
204
+ };
205
+ }
206
+ function readChange(item, field, issues) {
207
+ const scope = entryScope(item, field, CHANGE_KEYS, issues);
208
+ if (scope === null)
209
+ return null;
210
+ return {
211
+ level: choiceField(scope, "level", CHANGE_LEVELS, REQUIRED),
212
+ area: stringField(scope, "area", OPTIONAL),
213
+ summary: stringField(scope, "summary", REQUIRED),
214
+ extras: scope.extras
215
+ };
216
+ }
217
+ function readStaleDoc(item, field, issues) {
218
+ const scope = entryScope(item, field, STALE_DOC_KEYS, issues);
219
+ if (scope === null)
220
+ return null;
221
+ const where = stringField(scope, "where", OPTIONAL);
222
+ const what = stringField(scope, "what", REQUIRED);
223
+ if (what === null)
224
+ return null;
225
+ return { where, what, extras: scope.extras };
226
+ }
227
+ function readReport(document, issues) {
228
+ const scope = scopeOf(document, REPORT_KEYS, "", issues);
229
+ return {
230
+ status: choiceField(scope, "status", REPORT_STATUSES, REQUIRED),
231
+ feedback: stringField(scope, "feedback", OPTIONAL),
232
+ findings: listField(scope, "findings", readFinding),
233
+ skillsUsed: listField(scope, "skills_used", readSkill),
234
+ blockers: listField(scope, "blockers", readBlocker),
235
+ outOfScopeBugs: listField(scope, "out_of_scope_bugs", readBug),
236
+ changes: listField(scope, "changes", readChange),
237
+ staleDocs: listField(scope, "stale_docs", readStaleDoc),
238
+ extras: scope.extras
239
+ };
240
+ }
241
+ function parseReport(output) {
242
+ const reports = readRafaBlocks(output).filter((block2) => block2.kind === REPORT_KIND);
243
+ const block = reports.at(-1);
244
+ if (block === undefined) {
245
+ return absent("no-block", null, "the session output holds no rafa:report block");
246
+ }
247
+ const line = block.span.first;
248
+ if (!block.closed) {
249
+ return absent("unclosed-block", block, `rafa:report block at line ${line} is never closed, so its body may have been cut short ` + "and is not read");
250
+ }
251
+ let document;
252
+ try {
253
+ document = Bun.YAML.parse(block.body);
254
+ } catch (error) {
255
+ const text = `rafa:report block at line ${line} is not valid YAML (${messageOf2(error)})`;
256
+ return absent("malformed-block", block, text);
257
+ }
258
+ if (!isMapping(document)) {
259
+ const text = `rafa:report block at line ${line} holds ${describeValue(document)}, ` + "not a mapping of report fields";
260
+ return absent("malformed-block", block, text);
261
+ }
262
+ const issues = [];
263
+ const report = readReport(document, issues);
264
+ return { present: true, report, block, issues };
265
+ }
266
+
267
+ // packages/rafa/src/preflight/prerequisites-md.ts
11
268
  import { basename, dirname, join } from "path";
12
269
  var SECTION_AUTO_RE = /\[auto\]/i;
13
270
  var SECTION_START_RE = /\[start\]/i;
@@ -194,17 +451,158 @@ async function loadPlanPrerequisites(planPath, settings) {
194
451
  const content = path === null ? null : await readIfPresent(path);
195
452
  return mergePlanPrerequisites(settings, content);
196
453
  }
197
- // src/preflight/run.ts
198
- var PROBE_TIMEOUT_MS = 30000;
454
+
455
+ // packages/rafa/src/plan/inject.ts
456
+ var PLAN_CONTEXT_HEADING = "## Plan context";
457
+ var STAGE_HEADING_PREFIX = "## Stage: ";
458
+ var CHECKLIST_HEADING = "## Checklist";
459
+ var STAGE_LIST_HEADING = "## Stages";
460
+ var CURRENT_STAGE_MARK = " (current stage)";
461
+ var CHECKBOXES = {
462
+ unchecked: "- [ ]",
463
+ blocked: "- [BLOCKED]",
464
+ done: "- [x]"
465
+ };
466
+ var HANDED_OVER = ", so the whole plan is handed over";
467
+ function placeOf(position) {
468
+ return position !== undefined && Number.isInteger(position) && position >= 0 ? position : null;
469
+ }
470
+ var DISPATCHED_TEXT = "the dispatched text";
471
+ var REPAIRED_TEXT = "the text the dispatched repair names";
472
+ function noTaskWithText(place) {
473
+ const beyond = place === null ? "" : ` and no task number ${place + 1}`;
474
+ const text = `the plan holds no task with ${DISPATCHED_TEXT}${beyond}${HANDED_OVER}; ` + "the plan may have changed since its tracker was copied, or the task line sits inside a rafa:* block";
475
+ return { found: false, fallback: { reason: "no-task-with-text", text } };
476
+ }
477
+ function severalTasksWithText(count, place, sought) {
478
+ const which = place === null ? "the dispatch gives no position among its tasks" : `task number ${place + 1} is none of them`;
479
+ const text = `the plan holds ${count} tasks with ${sought} and ${which}${HANDED_OVER}`;
480
+ return { found: false, fallback: { reason: "several-tasks-with-text", text } };
481
+ }
482
+ function noRepairedTask() {
483
+ const text = `the plan holds no task with ${REPAIRED_TEXT}${HANDED_OVER}; ` + "the plan may have changed since the repair was written";
484
+ return { found: false, fallback: { reason: "no-task-with-text", text } };
485
+ }
486
+ function locate(model, wanted, position) {
487
+ const place = placeOf(position);
488
+ const atPlace = place === null ? undefined : model.tasks[place];
489
+ const holders = model.tasks.filter((candidate) => candidate.task === wanted.task);
490
+ const [first] = holders;
491
+ if (first === undefined) {
492
+ return atPlace === undefined ? noTaskWithText(place) : { found: true, task: atPlace };
493
+ }
494
+ if (holders.length === 1)
495
+ return { found: true, task: first };
496
+ return atPlace !== undefined && holders.includes(atPlace) ? { found: true, task: atPlace } : severalTasksWithText(holders.length, place, DISPATCHED_TEXT);
497
+ }
498
+ function locateRepaired(model, named, position) {
499
+ const sentence = named.trim();
500
+ const holders = model.tasks.filter((candidate) => candidate.text.trim() === sentence);
501
+ const [first] = holders;
502
+ if (first === undefined)
503
+ return noRepairedTask();
504
+ if (holders.length === 1)
505
+ return { found: true, task: first };
506
+ const place = placeOf(position);
507
+ const atPlace = place === null ? undefined : model.tasks[place];
508
+ return atPlace !== undefined && holders.includes(atPlace) ? { found: true, task: atPlace } : severalTasksWithText(holders.length, place, REPAIRED_TEXT);
509
+ }
510
+ function contextBody(body) {
511
+ if (body === null || body.trim().length === 0)
512
+ return null;
513
+ return body.replace(/^(?:[ \t]*\n)+/, "").replace(/(?:\n[ \t]*)+$/, "");
514
+ }
515
+ function section(heading, ...parts) {
516
+ return [heading, ...parts].filter((part) => part !== null).join(`
517
+
518
+ `);
519
+ }
520
+ function renderDocument(sections) {
521
+ const present = sections.filter((part) => part !== null);
522
+ return `${present.join(`
523
+
524
+ `)}
525
+ `;
526
+ }
527
+ function shownStatus(task, dispatched) {
528
+ if (task.lineNum < dispatched.task.lineNum)
529
+ return "done";
530
+ if (task.lineNum > dispatched.task.lineNum)
531
+ return task.status;
532
+ return dispatched.repair === null ? "unchecked" : "done";
533
+ }
534
+ function taskLine(status, task) {
535
+ return `${CHECKBOXES[status]} ${task.text}`;
536
+ }
537
+ function openLine(dispatched) {
538
+ return taskLine("unchecked", { text: dispatched.repair ?? dispatched.task.text });
539
+ }
540
+ function checklistLines(task, dispatched) {
541
+ const line = taskLine(shownStatus(task, dispatched), task);
542
+ return task === dispatched.task && dispatched.repair !== null ? [line, openLine(dispatched)] : [line];
543
+ }
544
+ function planContextSection(model) {
545
+ const body = contextBody(model.context);
546
+ return body === null ? null : section(PLAN_CONTEXT_HEADING, body);
547
+ }
548
+ function stageSection(model, dispatched, lines) {
549
+ const owner = dispatched.stage === null ? undefined : model.stages[dispatched.stage];
550
+ if (owner === undefined)
551
+ return section(CHECKLIST_HEADING, lines.join(`
552
+ `));
553
+ return section(`${STAGE_HEADING_PREFIX}${owner.name}`, contextBody(owner.context), lines.join(`
554
+ `));
555
+ }
556
+ function stageListSection(model, dispatched) {
557
+ if (model.stages.length === 0)
558
+ return null;
559
+ const items = model.stages.map((stage, index) => {
560
+ const mark = index === dispatched.stage ? CURRENT_STAGE_MARK : "";
561
+ return `${index + 1}. ${stage.name}${mark}`;
562
+ });
563
+ return section(STAGE_LIST_HEADING, items.join(`
564
+ `));
565
+ }
566
+ function renderStage(model, dispatched) {
567
+ const checklist = model.tasks.filter((task) => task.stage === dispatched.task.stage).flatMap((task) => checklistLines(task, dispatched));
568
+ return renderDocument([
569
+ planContextSection(model),
570
+ stageSection(model, dispatched.task, checklist),
571
+ stageListSection(model, dispatched.task)
572
+ ]);
573
+ }
574
+ function renderTask(model, dispatched) {
575
+ return renderDocument([
576
+ planContextSection(model),
577
+ stageSection(model, dispatched.task, [openLine(dispatched)])
578
+ ]);
579
+ }
580
+ var RENDERERS = {
581
+ stage: renderStage,
582
+ task: renderTask
583
+ };
584
+ function renderInjection(request) {
585
+ const { mode, plan, task, position, repairs } = request;
586
+ if (mode === "full" || repairs === null)
587
+ return { requested: mode, mode: "full", text: plan, fallback: null };
588
+ const model = parsePlan(plan);
589
+ const location = repairs === undefined ? locate(model, task, position) : locateRepaired(model, repairs, position);
590
+ if (!location.found) {
591
+ return { requested: mode, mode: "full", text: plan, fallback: location.fallback };
592
+ }
593
+ const repair = repairs === undefined ? null : parseTaskDeclaration(task.task).text.trim();
594
+ return { requested: mode, mode, text: RENDERERS[mode](model, { task: location.task, repair }), fallback: null };
595
+ }
596
+
597
+ // packages/rafa/src/preflight/shell.ts
199
598
  var SHELL = "/bin/sh";
200
- var PASS = Object.freeze({ outcome: "pass", failure: null });
201
- function spawnShell(probe, options) {
202
- return Bun.spawn([SHELL, "-c", probe], {
599
+ function spawnShell(command, options) {
600
+ return Bun.spawn([SHELL, "-c", command], {
203
601
  cwd: options.cwd,
204
602
  env: { ...options.env },
205
603
  stdin: "ignore",
206
- stdout: "ignore",
207
- stderr: "pipe",
604
+ stdout: options.stdout,
605
+ stderr: options.stderr,
208
606
  detached: true
209
607
  });
210
608
  }
@@ -237,10 +635,14 @@ function collect(stream) {
237
635
  };
238
636
  return { text: read(), stop };
239
637
  }
638
+
639
+ // packages/rafa/src/preflight/run.ts
640
+ var PROBE_TIMEOUT_MS = 30000;
641
+ var PASS = Object.freeze({ outcome: "pass", failure: null });
240
642
  async function runShellProbe(probe, options) {
241
643
  let proc;
242
644
  try {
243
- proc = spawnShell(probe, options);
645
+ proc = spawnShell(probe, { cwd: options.cwd, env: options.env, stdout: "ignore", stderr: "pipe" });
244
646
  } catch (error) {
245
647
  const stderr2 = `could not run ${SHELL} in ${options.cwd}: ${messageOf(error)}`;
246
648
  return Object.freeze({ exitCode: null, stderr: stderr2, timedOut: false });
@@ -391,834 +793,5 @@ async function runPreflight(items, options) {
391
793
  knownMissing: Object.freeze(knownMissing)
392
794
  });
393
795
  }
394
- // packages/rafa/src/core/plan/blocks.ts
395
- var RAFA_BLOCK_KINDS = [
396
- "plan",
397
- "context",
398
- "stage-context",
399
- "report"
400
- ];
401
- var RAFA_PREFIX = "rafa:";
402
- var FENCE_RUN = /^( {0,3})(`{3,}|~{3,})/;
403
- var CLOSING_FENCE = /^ {0,3}(`{3,}|~{3,})[ \t]*$/;
404
- function isRafaBlockKind(kind) {
405
- return RAFA_BLOCK_KINDS.includes(kind);
406
- }
407
- function linesOf(markdown) {
408
- const lines = markdown.split(`
409
- `);
410
- if (lines[lines.length - 1] === "")
411
- lines.pop();
412
- return lines.map((line) => line.endsWith("\r") ? line.slice(0, -1) : line);
413
- }
414
- function kindOf(info) {
415
- const word = info.trim().split(/\s+/)[0] ?? "";
416
- return word.startsWith(RAFA_PREFIX) ? word.slice(RAFA_PREFIX.length) : null;
417
- }
418
- function openingFence(line) {
419
- const match = FENCE_RUN.exec(line);
420
- const opener = match?.[0];
421
- const indent = match?.[1];
422
- const run = match?.[2];
423
- if (opener === undefined || indent === undefined || run === undefined) {
424
- return null;
425
- }
426
- const marker = run.charAt(0);
427
- const info = line.slice(opener.length);
428
- if (marker === "`" && info.includes("`"))
429
- return null;
430
- return { marker, length: run.length, indent: indent.length, kind: kindOf(info) };
431
- }
432
- function closes(line, fence) {
433
- const run = CLOSING_FENCE.exec(line)?.[1];
434
- return run !== undefined && run.charAt(0) === fence.marker && run.length >= fence.length;
435
- }
436
- function closingIndex(lines, from, fence) {
437
- for (let index = from;index < lines.length; index += 1) {
438
- if (closes(lines[index] ?? "", fence))
439
- return index;
440
- }
441
- return lines.length;
442
- }
443
- function dedent(line, indent) {
444
- let cut = 0;
445
- while (cut < indent && line.charAt(cut) === " ")
446
- cut += 1;
447
- return line.slice(cut);
448
- }
449
- function readRafaBlocks(markdown) {
450
- const lines = linesOf(markdown);
451
- const blocks = [];
452
- let index = 0;
453
- while (index < lines.length) {
454
- const fence = openingFence(lines[index] ?? "");
455
- if (fence === null) {
456
- index += 1;
457
- continue;
458
- }
459
- const closing = closingIndex(lines, index + 1, fence);
460
- const closed = closing < lines.length;
461
- if (fence.kind !== null) {
462
- const body = lines.slice(index + 1, closing).map((line) => dedent(line, fence.indent)).join(`
463
- `);
464
- const last = closed ? closing + 1 : lines.length;
465
- blocks.push({ kind: fence.kind, body, span: { first: index + 1, last }, closed });
466
- }
467
- index = closing + 1;
468
- }
469
- return blocks;
470
- }
471
- // packages/rafa/src/report/parse.ts
472
- var REPORT_STATUSES = ["done", "blocked"];
473
- var FINDING_KINDS = ["gotcha", "pattern", "location", "skill-suggestion"];
474
- var FINDING_SIGNALS = ["loud", "silent"];
475
- var FINDING_DOMAINS = [
476
- "code-style",
477
- "testing",
478
- "git",
479
- "debugging",
480
- "workflow",
481
- "security",
482
- "general-best-practices"
483
- ];
484
- var CHANGE_LEVELS = ["patch", "minor", "major", "none"];
485
- var BUG_KINDS = ["bug", "own-fallout", "own-gap", "convention", "docs", "machine"];
486
- var DEFAULT_BUG_KIND = "bug";
487
- var REPORT_KIND = "report";
488
- var REQUIRED = true;
489
- var OPTIONAL = false;
490
- var REPORT_KEYS = [
491
- "status",
492
- "feedback",
493
- "findings",
494
- "skills_used",
495
- "blockers",
496
- "out_of_scope_bugs",
497
- "changes",
498
- "stale_docs"
499
- ];
500
- var FINDING_KEYS = [
501
- "trigger",
502
- "kind",
503
- "what",
504
- "cause",
505
- "resolution",
506
- "artifact",
507
- "signal",
508
- "domain"
509
- ];
510
- var BLOCKER_KEYS = ["what", "artifact"];
511
- var BUG_KEYS = ["what", "artifact", "security", "kind", "module", "action", "reproduce", "cause"];
512
- var CHANGE_KEYS = ["level", "area", "summary"];
513
- var STALE_DOC_KEYS = ["where", "what"];
514
- function isMapping(value) {
515
- return typeof value === "object" && value !== null && !Array.isArray(value);
516
- }
517
- function isOneOf(choices, value) {
518
- return choices.includes(value);
519
- }
520
- function describeValue(value) {
521
- if (typeof value === "string")
522
- return JSON.stringify(value);
523
- if (value === null)
524
- return "nothing";
525
- if (Array.isArray(value))
526
- return "a list";
527
- if (typeof value === "object")
528
- return "a mapping";
529
- return `the ${typeof value} ${String(value)}`;
530
- }
531
- function messageOf2(error) {
532
- return error instanceof Error ? error.message : String(error);
533
- }
534
- function reportIssue(reason, field, text) {
535
- return { reason, field, text };
536
- }
537
- function absent(reason, block, text) {
538
- return { present: false, reason, block, text };
539
- }
540
- function scopeOf(mapping, keys, prefix, issues) {
541
- const fields = new Map;
542
- const extras = [];
543
- for (const [key, value] of Object.entries(mapping)) {
544
- if (keys.includes(key))
545
- fields.set(key, value);
546
- else
547
- extras.push({ key, value });
548
- }
549
- return { fields, extras, prefix, issues };
550
- }
551
- function valueOf(scope, key, required) {
552
- const value = scope.fields.get(key) ?? null;
553
- if (value === null && required) {
554
- const field = `${scope.prefix}${key}`;
555
- scope.issues.push(reportIssue("missing-field", field, `${field} is missing`));
556
- }
557
- return value;
558
- }
559
- function unusable(scope, key, why) {
560
- const field = `${scope.prefix}${key}`;
561
- scope.issues.push(reportIssue("unusable-field", field, `${field} ${why}`));
562
- return null;
563
- }
564
- function stringProblem(value) {
565
- if (typeof value !== "string")
566
- return `is ${describeValue(value)}, not a string; quote it`;
567
- if (value.trim().length === 0)
568
- return "is blank";
569
- return null;
570
- }
571
- function stringField(scope, key, required) {
572
- const value = valueOf(scope, key, required);
573
- if (value === null)
574
- return null;
575
- const problem = stringProblem(value);
576
- return problem === null ? value : unusable(scope, key, problem);
577
- }
578
- function choiceField(scope, key, choices, required) {
579
- const value = valueOf(scope, key, required);
580
- if (value === null || isOneOf(choices, value))
581
- return value;
582
- return unusable(scope, key, `is ${describeValue(value)}, not one of ${choices.join(", ")}`);
583
- }
584
- function booleanField(scope, key, required) {
585
- const value = valueOf(scope, key, required);
586
- if (value === null || typeof value === "boolean")
587
- return value;
588
- return unusable(scope, key, `is ${describeValue(value)}, not true or false; write it unquoted`);
589
- }
590
- function listField(scope, key, readEntry) {
591
- const value = valueOf(scope, key, OPTIONAL);
592
- if (value === null)
593
- return [];
594
- if (!Array.isArray(value)) {
595
- unusable(scope, key, `is ${describeValue(value)}, not a list`);
596
- return [];
597
- }
598
- const entries = [];
599
- const items = value;
600
- for (const [index, item] of items.entries()) {
601
- const entry = readEntry(item, `${scope.prefix}${key}[${index}]`, scope.issues);
602
- if (entry !== null)
603
- entries.push(entry);
604
- }
605
- return entries;
606
- }
607
- function entryScope(item, field, keys, issues) {
608
- if (isMapping(item))
609
- return scopeOf(item, keys, `${field}.`, issues);
610
- const text = `${field} is ${describeValue(item)}, not a mapping of ${keys.join(", ")}; dropped`;
611
- issues.push(reportIssue("unusable-field", field, text));
612
- return null;
613
- }
614
- function readSkill(item, field, issues) {
615
- const problem = stringProblem(item);
616
- if (problem === null)
617
- return item;
618
- issues.push(reportIssue("unusable-field", field, `${field} ${problem}; dropped`));
619
- return null;
620
- }
621
- function readFinding(item, field, issues) {
622
- const scope = entryScope(item, field, FINDING_KEYS, issues);
623
- if (scope === null)
624
- return null;
625
- return {
626
- trigger: stringField(scope, "trigger", REQUIRED),
627
- kind: choiceField(scope, "kind", FINDING_KINDS, REQUIRED),
628
- what: stringField(scope, "what", REQUIRED),
629
- cause: stringField(scope, "cause", OPTIONAL),
630
- resolution: stringField(scope, "resolution", OPTIONAL),
631
- artifact: stringField(scope, "artifact", OPTIONAL),
632
- signal: choiceField(scope, "signal", FINDING_SIGNALS, REQUIRED),
633
- domain: choiceField(scope, "domain", FINDING_DOMAINS, OPTIONAL),
634
- extras: scope.extras
635
- };
636
- }
637
- function readBlocker(item, field, issues) {
638
- const scope = entryScope(item, field, BLOCKER_KEYS, issues);
639
- if (scope === null)
640
- return null;
641
- return {
642
- what: stringField(scope, "what", REQUIRED),
643
- artifact: stringField(scope, "artifact", OPTIONAL),
644
- extras: scope.extras
645
- };
646
- }
647
- function readBug(item, field, issues) {
648
- const scope = entryScope(item, field, BUG_KEYS, issues);
649
- if (scope === null)
650
- return null;
651
- return {
652
- what: stringField(scope, "what", REQUIRED),
653
- artifact: stringField(scope, "artifact", OPTIONAL),
654
- security: booleanField(scope, "security", REQUIRED),
655
- kind: choiceField(scope, "kind", BUG_KINDS, OPTIONAL) ?? DEFAULT_BUG_KIND,
656
- module: stringField(scope, "module", OPTIONAL),
657
- action: stringField(scope, "action", OPTIONAL),
658
- reproduce: stringField(scope, "reproduce", OPTIONAL),
659
- cause: stringField(scope, "cause", OPTIONAL),
660
- extras: scope.extras
661
- };
662
- }
663
- function readChange(item, field, issues) {
664
- const scope = entryScope(item, field, CHANGE_KEYS, issues);
665
- if (scope === null)
666
- return null;
667
- return {
668
- level: choiceField(scope, "level", CHANGE_LEVELS, REQUIRED),
669
- area: stringField(scope, "area", OPTIONAL),
670
- summary: stringField(scope, "summary", REQUIRED),
671
- extras: scope.extras
672
- };
673
- }
674
- function readStaleDoc(item, field, issues) {
675
- const scope = entryScope(item, field, STALE_DOC_KEYS, issues);
676
- if (scope === null)
677
- return null;
678
- const where = stringField(scope, "where", OPTIONAL);
679
- const what = stringField(scope, "what", REQUIRED);
680
- if (what === null)
681
- return null;
682
- return { where, what, extras: scope.extras };
683
- }
684
- function readReport(document, issues) {
685
- const scope = scopeOf(document, REPORT_KEYS, "", issues);
686
- return {
687
- status: choiceField(scope, "status", REPORT_STATUSES, REQUIRED),
688
- feedback: stringField(scope, "feedback", OPTIONAL),
689
- findings: listField(scope, "findings", readFinding),
690
- skillsUsed: listField(scope, "skills_used", readSkill),
691
- blockers: listField(scope, "blockers", readBlocker),
692
- outOfScopeBugs: listField(scope, "out_of_scope_bugs", readBug),
693
- changes: listField(scope, "changes", readChange),
694
- staleDocs: listField(scope, "stale_docs", readStaleDoc),
695
- extras: scope.extras
696
- };
697
- }
698
- function parseReport(output) {
699
- const reports = readRafaBlocks(output).filter((block2) => block2.kind === REPORT_KIND);
700
- const block = reports.at(-1);
701
- if (block === undefined) {
702
- return absent("no-block", null, "the session output holds no rafa:report block");
703
- }
704
- const line = block.span.first;
705
- if (!block.closed) {
706
- return absent("unclosed-block", block, `rafa:report block at line ${line} is never closed, so its body may have been cut short ` + "and is not read");
707
- }
708
- let document;
709
- try {
710
- document = Bun.YAML.parse(block.body);
711
- } catch (error) {
712
- const text = `rafa:report block at line ${line} is not valid YAML (${messageOf2(error)})`;
713
- return absent("malformed-block", block, text);
714
- }
715
- if (!isMapping(document)) {
716
- const text = `rafa:report block at line ${line} holds ${describeValue(document)}, ` + "not a mapping of report fields";
717
- return absent("malformed-block", block, text);
718
- }
719
- const issues = [];
720
- const report = readReport(document, issues);
721
- return { present: true, report, block, issues };
722
- }
723
- // packages/rafa/src/core/plan/blocker-comment.ts
724
- var BLOCKER_OPEN = "<!-- blocked: ";
725
- var REPAIRS_OPEN = "<!-- repairs: ";
726
- var BLOCKER_CLOSE = " -->";
727
- var BLOCKER_GAP = " ";
728
- var TRAILING_COMMENT = /\s<!-- (blocked|repairs): ((?:(?!<!--|-->).)*) -->$/;
729
- var ESCAPE_SEQUENCE = /\\(u[0-9A-Fa-f]{4}|[\s\S])/g;
730
- var TAB = 9;
731
- var FIRST_PRINTABLE = 32;
732
- var ESCAPED_CODE_UNITS = [127, 8232, 8233];
733
- function isEscapedCodeUnit(code) {
734
- if (code < FIRST_PRINTABLE)
735
- return code !== TAB;
736
- return ESCAPED_CODE_UNITS.includes(code);
737
- }
738
- function escapeCharacter(char) {
739
- if (char === "\\")
740
- return "\\\\";
741
- if (char === `
742
- `)
743
- return "\\n";
744
- if (char === "\r")
745
- return "\\r";
746
- const code = char.charCodeAt(0);
747
- return isEscapedCodeUnit(code) ? `\\u${code.toString(16).padStart(4, "0")}` : char;
748
- }
749
- function escapeBlockerText(text) {
750
- return Array.from(text, escapeCharacter).join("").replace(/(?<=--!?)>/g, "\\>").replace(/(?<=<)!(?=--)/g, "\\!");
751
- }
752
- function unescapeBlockerText(escaped) {
753
- return escaped.replace(ESCAPE_SEQUENCE, (_sequence, code) => {
754
- if (code === "n")
755
- return `
756
- `;
757
- if (code === "r")
758
- return "\r";
759
- return code.length === 5 ? String.fromCharCode(Number.parseInt(code.slice(1), 16)) : code;
760
- });
761
- }
762
- function blockerComment(blocker) {
763
- return `${BLOCKER_OPEN}${escapeBlockerText(blocker)}${BLOCKER_CLOSE}`;
764
- }
765
- function repairsComment(repaired) {
766
- return `${REPAIRS_OPEN}${escapeBlockerText(repaired)}${BLOCKER_CLOSE}`;
767
- }
768
- function findTrailingComment(text) {
769
- const trimmed = text.trimEnd();
770
- const match = TRAILING_COMMENT.exec(trimmed);
771
- const kind = match?.[1];
772
- const escaped = match?.[2];
773
- if (match === null || escaped === undefined || kind !== "blocked" && kind !== "repairs")
774
- return null;
775
- const before = trimmed.slice(0, match.index).trimEnd();
776
- return before.length === 0 ? null : { kind, before, escaped };
777
- }
778
- function splitTrailingComments(taskText) {
779
- const last = findTrailingComment(taskText);
780
- if (last === null)
781
- return { text: taskText, blocked: null, repairs: null };
782
- const earlier = findTrailingComment(last.before);
783
- const found = earlier === null || earlier.kind === last.kind ? [last] : [last, earlier];
784
- return {
785
- text: found.at(-1)?.before ?? last.before,
786
- blocked: found.find((comment) => comment.kind === "blocked")?.escaped ?? null,
787
- repairs: found.find((comment) => comment.kind === "repairs")?.escaped ?? null
788
- };
789
- }
790
- function commentText(escaped) {
791
- if (escaped === null)
792
- return null;
793
- const text = unescapeBlockerText(escaped);
794
- return text.trim().length === 0 ? null : text;
795
- }
796
- function keptRepairs(escaped) {
797
- return escaped === null ? "" : `${BLOCKER_GAP}${REPAIRS_OPEN}${escaped}${BLOCKER_CLOSE}`;
798
- }
799
- function splitBlockerComment(taskText) {
800
- const { text, blocked } = splitTrailingComments(taskText);
801
- return { text, blocker: commentText(blocked) };
802
- }
803
- // packages/rafa/src/core/plan/parse.ts
804
- var PLAN_BLOCK_KINDS = ["plan", "context", "stage-context"];
805
- var PLAN_HEADER_FIELDS = ["stub", "issue", "spec", "release"];
806
- var PLAN_RELEASE_LEVELS = ["patch", "minor", "major", "none"];
807
- var TASK_LINES = [
808
- ["unchecked", /^- \[ \] (.+)/],
809
- ["blocked", /^- \[BLOCKED\] (.+)/],
810
- ["done", /^- \[x\] (.+)/]
811
- ];
812
- var STAGE_HEADING = /^# Stage: (.+)/;
813
- function isPlanBlockKind(kind) {
814
- return PLAN_BLOCK_KINDS.includes(kind);
815
- }
816
- function isHeaderField(key) {
817
- return PLAN_HEADER_FIELDS.includes(key);
818
- }
819
- function isReleaseLevel(value) {
820
- return PLAN_RELEASE_LEVELS.includes(value);
821
- }
822
- function isMapping2(value) {
823
- return typeof value === "object" && value !== null && !Array.isArray(value);
824
- }
825
- function describeValue2(value) {
826
- if (typeof value === "string")
827
- return JSON.stringify(value);
828
- if (value === null)
829
- return "null";
830
- if (Array.isArray(value))
831
- return "a list";
832
- if (typeof value === "object")
833
- return "a mapping";
834
- return String(value);
835
- }
836
- function messageOf3(error) {
837
- return error instanceof Error ? error.message : String(error);
838
- }
839
- function planIssue(reason, line, text) {
840
- return { reason, line, text };
841
- }
842
- function emptyHeader() {
843
- return { stub: null, issue: null, spec: null, release: null, extras: [] };
844
- }
845
- function readTaskLine(line) {
846
- for (const [status, pattern] of TASK_LINES) {
847
- const capture = pattern.exec(line)?.[1];
848
- if (capture !== undefined)
849
- return { status, task: splitBlockerComment(capture.trim()).text };
850
- }
851
- return null;
852
- }
853
- function readStageHeading(line) {
854
- const capture = STAGE_HEADING.exec(line)?.[1];
855
- return capture === undefined ? null : capture.trim();
856
- }
857
- function blockAt(blocks, lineNum) {
858
- const line = lineNum + 1;
859
- return blocks.find((block) => block.span.first <= line && line <= block.span.last);
860
- }
861
- function taskInBlock(lineNum, status, block) {
862
- const { first, last } = block.span;
863
- const dispatcher = block.closed ? "findNextTask skips it" : "findNextTask still dispatches it, the block never being closed";
864
- return planIssue("task-in-block", lineNum + 1, `${status} task line inside the rafa:${block.kind} block at lines ${first}-${last}: ` + `${dispatcher}, and the model reads it as block body`);
865
- }
866
- function planTask(line, lineNum, headings) {
867
- const { text, declaration } = parseTaskDeclaration(line.task);
868
- const stage = headings.length > 0 ? headings.length - 1 : null;
869
- return { task: line.task, lineNum, status: line.status, text, declaration, stage };
870
- }
871
- function readChecklist(markdown, blocks) {
872
- const headings = [];
873
- const tasks = [];
874
- const hidden = [];
875
- const issues = [];
876
- for (const [lineNum, line] of markdown.split(`
877
- `).entries()) {
878
- const taskLine = readTaskLine(line);
879
- const block = blockAt(blocks, lineNum);
880
- if (block !== undefined) {
881
- if (taskLine !== null && taskLine.status !== "done") {
882
- issues.push(taskInBlock(lineNum, taskLine.status, block));
883
- if (!block.closed)
884
- hidden.push(planTask(taskLine, lineNum, headings));
885
- }
886
- continue;
887
- }
888
- const name = readStageHeading(line);
889
- if (name !== null) {
890
- headings.push({ name, lineNum });
891
- continue;
892
- }
893
- if (taskLine === null)
894
- continue;
895
- tasks.push(planTask(taskLine, lineNum, headings));
896
- }
897
- return { headings, tasks, hidden, issues };
898
- }
899
- function headingAbove(headings, lineNum) {
900
- let above = null;
901
- for (const [index, heading] of headings.entries()) {
902
- if (heading.lineNum >= lineNum)
903
- break;
904
- above = index;
905
- }
906
- return above;
907
- }
908
- function stageSlot(index) {
909
- return `stage-context@${index}`;
910
- }
911
- function slotOf(block, headings) {
912
- if (block.kind !== "stage-context")
913
- return { key: block.kind, scope: "" };
914
- const index = headingAbove(headings, block.span.first - 1);
915
- if (index === null)
916
- return null;
917
- const name = headings[index]?.name ?? "";
918
- return { key: stageSlot(index), scope: ` under # Stage: ${name}` };
919
- }
920
- function bindBlocks(blocks, headings) {
921
- const read = new Map;
922
- const issues = [];
923
- for (const block of blocks) {
924
- const line = block.span.first;
925
- if (!block.closed) {
926
- issues.push(planIssue("unclosed-block", line, `rafa:${block.kind} block is never closed, so it runs to the end of the document ` + "and nothing after its fence is read"));
927
- continue;
928
- }
929
- if (!isPlanBlockKind(block.kind))
930
- continue;
931
- const slot = slotOf(block, headings);
932
- if (slot === null) {
933
- issues.push(planIssue("orphan-stage-context", line, "rafa:stage-context block above every # Stage: heading belongs to no stage and is not read"));
934
- continue;
935
- }
936
- const earlier = read.get(slot.key);
937
- if (earlier === undefined) {
938
- read.set(slot.key, block);
939
- continue;
940
- }
941
- issues.push(planIssue("duplicate-block", line, `rafa:${block.kind} block${slot.scope} repeats the one at line ${earlier.span.first}, ` + "which is the one read"));
942
- }
943
- return {
944
- header: read.get("plan") ?? null,
945
- context: read.get("context") ?? null,
946
- stageContexts: headings.map((_, index) => read.get(stageSlot(index))?.body ?? null),
947
- issues
948
- };
949
- }
950
- function usableField(field, value) {
951
- if (field === "issue" && typeof value === "number") {
952
- return Number.isSafeInteger(value) ? String(value) : null;
953
- }
954
- if (typeof value !== "string" || value.trim().length === 0)
955
- return null;
956
- if (field === "stub" && !isStampableStub(value))
957
- return null;
958
- if (field === "release" && !isReleaseLevel(value))
959
- return null;
960
- return value;
961
- }
962
- function unusableText(field, value) {
963
- const subject = `rafa:plan ${field}`;
964
- if (value === null) {
965
- return `${subject} is empty; a value opening with # after a space is a YAML comment unless quoted`;
966
- }
967
- if (field === "release") {
968
- return `${subject} is ${describeValue2(value)}, not one of ${PLAN_RELEASE_LEVELS.join(", ")}`;
969
- }
970
- if (typeof value !== "string") {
971
- return field === "issue" ? `${subject} is ${describeValue2(value)}, not a string or a whole number; quote it` : `${subject} is ${describeValue2(value)}, not a string; quote it`;
972
- }
973
- if (value.trim().length === 0)
974
- return `${subject} is blank`;
975
- return `${subject} ${JSON.stringify(value)} is no stub a plan stamp can carry ` + '(letters, digits, ".", "_" and "-")';
976
- }
977
- function releaseOf(values) {
978
- const value = values.get("release");
979
- if (value === undefined || !isReleaseLevel(value))
980
- return null;
981
- return value;
982
- }
983
- function readFields(document, line) {
984
- const values = new Map;
985
- const extras = [];
986
- const issues = [];
987
- for (const [key, value] of Object.entries(document)) {
988
- if (!isHeaderField(key)) {
989
- extras.push({ key, value });
990
- continue;
991
- }
992
- const usable = usableField(key, value);
993
- if (usable === null)
994
- issues.push(planIssue("unusable-field", line, unusableText(key, value)));
995
- else
996
- values.set(key, usable);
997
- }
998
- const header = {
999
- stub: values.get("stub") ?? null,
1000
- issue: values.get("issue") ?? null,
1001
- spec: values.get("spec") ?? null,
1002
- release: releaseOf(values),
1003
- extras
1004
- };
1005
- return { header, issues };
1006
- }
1007
- function readHeader(block) {
1008
- if (block === null)
1009
- return { header: emptyHeader(), issues: [] };
1010
- const line = block.span.first;
1011
- let document;
1012
- try {
1013
- document = Bun.YAML.parse(block.body);
1014
- } catch (error) {
1015
- const text = `rafa:plan block is not valid YAML (${messageOf3(error)})`;
1016
- return { header: emptyHeader(), issues: [planIssue("malformed-header", line, text)] };
1017
- }
1018
- if (document === null)
1019
- return { header: emptyHeader(), issues: [] };
1020
- if (!isMapping2(document)) {
1021
- const text = `rafa:plan block holds ${describeValue2(document)}, not a mapping of fields`;
1022
- return { header: emptyHeader(), issues: [planIssue("malformed-header", line, text)] };
1023
- }
1024
- return readFields(document, line);
1025
- }
1026
- function parsePlan(markdown) {
1027
- const blocks = readRafaBlocks(markdown);
1028
- const checklist = readChecklist(markdown, blocks);
1029
- const bound = bindBlocks(blocks, checklist.headings);
1030
- const reading = readHeader(bound.header);
1031
- const stages = checklist.headings.map((heading, index) => ({
1032
- name: heading.name,
1033
- lineNum: heading.lineNum,
1034
- context: bound.stageContexts[index] ?? null
1035
- }));
1036
- const issues = [...checklist.issues, ...bound.issues, ...reading.issues].sort((a, b) => a.line - b.line);
1037
- return {
1038
- header: reading.header,
1039
- context: bound.context?.body ?? null,
1040
- stages,
1041
- tasks: checklist.tasks,
1042
- hiddenTasks: checklist.hidden,
1043
- blocks,
1044
- issues
1045
- };
1046
- }
1047
- // packages/rafa/src/core/plan/plan-files.ts
1048
- import { statSync } from "fs";
1049
- import { basename as basename2, dirname as dirname2, join as join2, resolve } from "path";
1050
- function plansDirAt(root, planDir) {
1051
- return { label: planDir, path: resolve(root, planDir) };
1052
- }
1053
- function countTasks(tasks) {
1054
- const done = tasks.filter((task) => task.status === "done").length;
1055
- const blocked = tasks.filter((task) => task.status === "blocked").length;
1056
- return { total: tasks.length, done, blocked, open: tasks.length - done - blocked };
1057
- }
1058
- function planFileName(stub, tracker) {
1059
- return tracker ? `PLAN_TRACKER-${stub}.md` : `PLAN-${stub}.md`;
1060
- }
1061
- function stubOfPlanFile(name) {
1062
- const stub = /^PLAN-(.+)\.md$/.exec(name)?.[1];
1063
- return stub !== undefined && isStampableStub(stub) ? stub : null;
1064
- }
1065
- function isFile(path) {
1066
- return statSync(path, { throwIfNoEntry: false })?.isFile() ?? false;
1067
- }
1068
- var REJECTED_DIR = "rejected";
1069
- function rejectedPath(path) {
1070
- const parent = dirname2(path);
1071
- return parent === "." ? join2(REJECTED_DIR, basename2(path)) : join2(parent, REJECTED_DIR, basename2(path));
1072
- }
1073
- function issueLine(file, issue) {
1074
- return `${file}:${issue.line}: ${issue.reason}: ${issue.text}`;
1075
- }
1076
- function plural(count, noun) {
1077
- return count === 1 ? `${count} ${noun}` : `${count} ${noun}s`;
1078
- }
1079
- // packages/rafa/src/core/plan/tracker-path.ts
1080
- function trackerPathFor(planPath) {
1081
- return planPath.replace(/PLAN(-[^/]*)?\.md$/, (_m, stub) => `PLAN_TRACKER${stub ?? ""}.md`);
1082
- }
1083
- // packages/rafa/src/plan/inject.ts
1084
- var PLAN_CONTEXT_HEADING = "## Plan context";
1085
- var STAGE_HEADING_PREFIX = "## Stage: ";
1086
- var CHECKLIST_HEADING = "## Checklist";
1087
- var STAGE_LIST_HEADING = "## Stages";
1088
- var CURRENT_STAGE_MARK = " (current stage)";
1089
- var CHECKBOXES = {
1090
- unchecked: "- [ ]",
1091
- blocked: "- [BLOCKED]",
1092
- done: "- [x]"
1093
- };
1094
- var HANDED_OVER = ", so the whole plan is handed over";
1095
- function placeOf(position) {
1096
- return position !== undefined && Number.isInteger(position) && position >= 0 ? position : null;
1097
- }
1098
- var DISPATCHED_TEXT = "the dispatched text";
1099
- var REPAIRED_TEXT = "the text the dispatched repair names";
1100
- function noTaskWithText(place) {
1101
- const beyond = place === null ? "" : ` and no task number ${place + 1}`;
1102
- const text = `the plan holds no task with ${DISPATCHED_TEXT}${beyond}${HANDED_OVER}; ` + "the plan may have changed since its tracker was copied, or the task line sits inside a rafa:* block";
1103
- return { found: false, fallback: { reason: "no-task-with-text", text } };
1104
- }
1105
- function severalTasksWithText(count, place, sought) {
1106
- const which = place === null ? "the dispatch gives no position among its tasks" : `task number ${place + 1} is none of them`;
1107
- const text = `the plan holds ${count} tasks with ${sought} and ${which}${HANDED_OVER}`;
1108
- return { found: false, fallback: { reason: "several-tasks-with-text", text } };
1109
- }
1110
- function noRepairedTask() {
1111
- const text = `the plan holds no task with ${REPAIRED_TEXT}${HANDED_OVER}; ` + "the plan may have changed since the repair was written";
1112
- return { found: false, fallback: { reason: "no-task-with-text", text } };
1113
- }
1114
- function locate(model, wanted, position) {
1115
- const place = placeOf(position);
1116
- const atPlace = place === null ? undefined : model.tasks[place];
1117
- const holders = model.tasks.filter((candidate) => candidate.task === wanted.task);
1118
- const [first] = holders;
1119
- if (first === undefined) {
1120
- return atPlace === undefined ? noTaskWithText(place) : { found: true, task: atPlace };
1121
- }
1122
- if (holders.length === 1)
1123
- return { found: true, task: first };
1124
- return atPlace !== undefined && holders.includes(atPlace) ? { found: true, task: atPlace } : severalTasksWithText(holders.length, place, DISPATCHED_TEXT);
1125
- }
1126
- function locateRepaired(model, named, position) {
1127
- const sentence = named.trim();
1128
- const holders = model.tasks.filter((candidate) => candidate.text.trim() === sentence);
1129
- const [first] = holders;
1130
- if (first === undefined)
1131
- return noRepairedTask();
1132
- if (holders.length === 1)
1133
- return { found: true, task: first };
1134
- const place = placeOf(position);
1135
- const atPlace = place === null ? undefined : model.tasks[place];
1136
- return atPlace !== undefined && holders.includes(atPlace) ? { found: true, task: atPlace } : severalTasksWithText(holders.length, place, REPAIRED_TEXT);
1137
- }
1138
- function contextBody(body) {
1139
- if (body === null || body.trim().length === 0)
1140
- return null;
1141
- return body.replace(/^(?:[ \t]*\n)+/, "").replace(/(?:\n[ \t]*)+$/, "");
1142
- }
1143
- function section(heading, ...parts) {
1144
- return [heading, ...parts].filter((part) => part !== null).join(`
1145
-
1146
- `);
1147
- }
1148
- function renderDocument(sections) {
1149
- const present = sections.filter((part) => part !== null);
1150
- return `${present.join(`
1151
796
 
1152
- `)}
1153
- `;
1154
- }
1155
- function shownStatus(task, dispatched) {
1156
- if (task.lineNum < dispatched.task.lineNum)
1157
- return "done";
1158
- if (task.lineNum > dispatched.task.lineNum)
1159
- return task.status;
1160
- return dispatched.repair === null ? "unchecked" : "done";
1161
- }
1162
- function taskLine(status, task) {
1163
- return `${CHECKBOXES[status]} ${task.text}`;
1164
- }
1165
- function openLine(dispatched) {
1166
- return taskLine("unchecked", { text: dispatched.repair ?? dispatched.task.text });
1167
- }
1168
- function checklistLines(task, dispatched) {
1169
- const line = taskLine(shownStatus(task, dispatched), task);
1170
- return task === dispatched.task && dispatched.repair !== null ? [line, openLine(dispatched)] : [line];
1171
- }
1172
- function planContextSection(model) {
1173
- const body = contextBody(model.context);
1174
- return body === null ? null : section(PLAN_CONTEXT_HEADING, body);
1175
- }
1176
- function stageSection(model, dispatched, lines) {
1177
- const owner = dispatched.stage === null ? undefined : model.stages[dispatched.stage];
1178
- if (owner === undefined)
1179
- return section(CHECKLIST_HEADING, lines.join(`
1180
- `));
1181
- return section(`${STAGE_HEADING_PREFIX}${owner.name}`, contextBody(owner.context), lines.join(`
1182
- `));
1183
- }
1184
- function stageListSection(model, dispatched) {
1185
- if (model.stages.length === 0)
1186
- return null;
1187
- const items = model.stages.map((stage, index) => {
1188
- const mark = index === dispatched.stage ? CURRENT_STAGE_MARK : "";
1189
- return `${index + 1}. ${stage.name}${mark}`;
1190
- });
1191
- return section(STAGE_LIST_HEADING, items.join(`
1192
- `));
1193
- }
1194
- function renderStage(model, dispatched) {
1195
- const checklist = model.tasks.filter((task) => task.stage === dispatched.task.stage).flatMap((task) => checklistLines(task, dispatched));
1196
- return renderDocument([
1197
- planContextSection(model),
1198
- stageSection(model, dispatched.task, checklist),
1199
- stageListSection(model, dispatched.task)
1200
- ]);
1201
- }
1202
- function renderTask(model, dispatched) {
1203
- return renderDocument([
1204
- planContextSection(model),
1205
- stageSection(model, dispatched.task, [openLine(dispatched)])
1206
- ]);
1207
- }
1208
- var RENDERERS = {
1209
- stage: renderStage,
1210
- task: renderTask
1211
- };
1212
- function renderInjection(request) {
1213
- const { mode, plan, task, position, repairs } = request;
1214
- if (mode === "full" || repairs === null)
1215
- return { requested: mode, mode: "full", text: plan, fallback: null };
1216
- const model = parsePlan(plan);
1217
- const location = repairs === undefined ? locate(model, task, position) : locateRepaired(model, repairs, position);
1218
- if (!location.found) {
1219
- return { requested: mode, mode: "full", text: plan, fallback: location.fallback };
1220
- }
1221
- const repair = repairs === undefined ? null : parseTaskDeclaration(task.task).text.trim();
1222
- return { requested: mode, mode, text: RENDERERS[mode](model, { task: location.task, repair }), fallback: null };
1223
- }
1224
- export { BLOCKER_GAP, escapeBlockerText, unescapeBlockerText, blockerComment, repairsComment, splitTrailingComments, commentText, keptRepairs, splitBlockerComment, RAFA_BLOCK_KINDS, isRafaBlockKind, readRafaBlocks, PLAN_BLOCK_KINDS, PLAN_HEADER_FIELDS, PLAN_RELEASE_LEVELS, parsePlan, plansDirAt, countTasks, planFileName, stubOfPlanFile, isFile, rejectedPath, issueLine, plural, trackerPathFor, REPORT_STATUSES, FINDING_KINDS, FINDING_SIGNALS, FINDING_DOMAINS, CHANGE_LEVELS, BUG_KINDS, parseReport, parsePrerequisites, planPrerequisites, malformedPrerequisiteLines, mergePlanPrerequisites, prerequisitesPathForPlan, loadPlanPrerequisites, PROBE_TIMEOUT_MS, runShellProbe, runPreflight, renderInjection };
797
+ export { REPORT_STATUSES, FINDING_KINDS, FINDING_SIGNALS, FINDING_DOMAINS, CHANGE_LEVELS, BUG_KINDS, parseReport, parsePrerequisites, planPrerequisites, malformedPrerequisiteLines, mergePlanPrerequisites, prerequisitesPathForPlan, loadPlanPrerequisites, renderInjection, SHELL, spawnShell, killGroup, collect, PROBE_TIMEOUT_MS, runShellProbe, runPreflight };