@turri-dev/cli 0.1.3-next.2babf06 → 0.1.3-next.420ce7e

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 (40) hide show
  1. package/README.md +1 -1
  2. package/dist/config.js +3 -2
  3. package/dist/extension.js +86 -35
  4. package/dist/identity.js +1 -1
  5. package/dist/loop-cli.js +1 -1
  6. package/dist/loop-tools.js +1 -1
  7. package/dist/pi-launch.js +5 -0
  8. package/dist/question-dialog.js +8 -3
  9. package/dist/questioning.js +1 -1
  10. package/dist/regenerate/activity.js +2 -0
  11. package/dist/regenerate/confine.js +1 -0
  12. package/dist/regenerate/conform.js +5 -5
  13. package/dist/regenerate/conformance-agents.js +220 -11
  14. package/dist/regenerate/conformance.js +91 -35
  15. package/dist/regenerate/debugger.js +27 -3
  16. package/dist/regenerate/environment-error.js +63 -9
  17. package/dist/regenerate/loop.js +22 -4
  18. package/dist/regenerate/notes.js +66 -17
  19. package/dist/regenerate/panel-dock.js +31 -2
  20. package/dist/regenerate/prompt.js +3 -1
  21. package/dist/regenerate/regenerate.js +35 -14
  22. package/dist/regenerate/regression.js +2 -1
  23. package/dist/regenerate/requires.js +55 -2
  24. package/dist/regenerate/side-panel.js +1 -1
  25. package/dist/regenerate/stop-report.js +13 -0
  26. package/dist/regenerate/test-scripts.js +22 -1
  27. package/dist/regenerate/unit-review.js +11 -0
  28. package/dist/regenerate-cli.js +1 -1
  29. package/dist/resume-line.js +4 -0
  30. package/dist/spec-tools.js +8 -44
  31. package/docs/authoring-plain.md +9 -6
  32. package/docs/config.md +3 -2
  33. package/docs/headless.md +2 -2
  34. package/docs/regenerating.md +75 -18
  35. package/docs/skills-and-gates.md +2 -2
  36. package/docs/using.md +5 -3
  37. package/genome/skills/bud/SKILL.md +67 -36
  38. package/genome/skills/bud/references/intent.md +4 -0
  39. package/package.json +3 -3
  40. package/genome/skills/bud/references/checklist.md +0 -44
@@ -1,5 +1,5 @@
1
1
  import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs";
2
- import { basename, dirname, join } from "node:path";
2
+ import { basename, dirname, join, resolve } from "node:path";
3
3
  import { defineTool } from "@earendil-works/pi-coding-agent";
4
4
  import { Type } from "typebox";
5
5
  import { reportActivity } from "./activity.js";
@@ -40,7 +40,6 @@ export const SECTIONS = {
40
40
  where: "Where things are",
41
41
  run: "How to build, run and test",
42
42
  assumptions: "Assumptions",
43
- gaps: "Spec gaps",
44
43
  pitfalls: "Pitfalls",
45
44
  pulses: "Pulses",
46
45
  };
@@ -53,8 +52,13 @@ export function notesPath(buildDir) {
53
52
  return join(buildDir, NOTES_FILE);
54
53
  }
55
54
  function empty() {
56
- return { where: [], run: [], assumptions: [], gaps: [], pitfalls: [], pulses: [] };
55
+ return { where: [], run: [], assumptions: [], pitfalls: [], pulses: [] };
57
56
  }
57
+ /**
58
+ * Sections the notes no longer have, their lines dropped as they are read: the spec gaps are the person's, reported
59
+ * with report_spec_gap - the choice made meanwhile is an assumption, which stays.
60
+ */
61
+ const DROPPED = new Set(["spec gaps"]);
58
62
  /** The notes, as lines per section. Lines under no known heading go to pitfalls, not lost. */
59
63
  export function parseNotes(text) {
60
64
  const notes = empty();
@@ -63,11 +67,12 @@ export function parseNotes(text) {
63
67
  const line = raw.trim();
64
68
  const heading = /^##\s+(.+)$/.exec(line);
65
69
  if (heading) {
66
- section = ORDER.find((key) => SECTIONS[key].toLowerCase() === heading[1].trim().toLowerCase()) ?? section;
70
+ const named = heading[1].trim().toLowerCase();
71
+ section = DROPPED.has(named) ? undefined : (ORDER.find((key) => SECTIONS[key].toLowerCase() === named) ?? section ?? "pitfalls");
67
72
  continue;
68
73
  }
69
74
  const item = /^[-*]\s+(.+)$/.exec(line);
70
- if (item)
75
+ if (item && section !== undefined)
71
76
  notes[section].push(item[1].trim());
72
77
  }
73
78
  return notes;
@@ -93,14 +98,14 @@ const GUIDANCE = [
93
98
  `- ${SECTIONS.where}: entry points, layout, where each functionality and its tests live.`,
94
99
  `- ${SECTIONS.run}: the commands that build, run and test it, and what had to be installed for them.`,
95
100
  `- ${SECTIONS.assumptions}: what you assumed where the specification is silent - a format, a default, a`,
96
- " choice it leaves open. Build on the assumptions already there; change or remove one the moment the code",
97
- " or the specification shows it wrong.",
98
- `- ${SECTIONS.gaps}: what the specification should say and does not - a behaviour you had to decide that the`,
99
- " person would want to decide, not a detail of how. Each is put to the person, to be written into the",
100
- " specification. One a line, as the question it leaves: \"What happens to a contact archived in HubSpot after it",
101
- " was synced?\"",
101
+ " choice it leaves open - and the tests that assert it, when any do: \"A Dash drains Pulses at 1/s - asserted in",
102
+ " predators.test.ts, The Dash\". One no test asserts is written down all the same. Build on the assumptions",
103
+ " already there; change or remove one the moment the code or the specification shows it wrong - and the tests",
104
+ " it names with it.",
102
105
  `- ${SECTIONS.pitfalls}: what went wrong and what fixed it, approaches that did not work.`,
103
106
  `- ${SECTIONS.pulses}: written by turri as each pulse ends - read them, never write them.`,
107
+ "A behaviour you had to decide that the person would want to decide - not a detail of how - is not a note: report",
108
+ "it with report_spec_gap, with the assumption you made meanwhile.",
104
109
  "Each line stands on its own, for someone who never saw your work. When and where it came from is added",
105
110
  "to it for you - do not write it. Where two lines disagree, the later one is right: change the older one",
106
111
  'rather than add a contradicting line. Leave a section with nothing new alone: "none" is not a note.',
@@ -232,7 +237,7 @@ export function createNotesTool(buildDir, onWrite, before = () => { }, source) {
232
237
  label: "Update notes",
233
238
  description: "Update the working notes on this codebase for the next agent - add a line, change one that is no longer " +
234
239
  "true, remove one that is wrong - in its sections: where things are, how to build, run and test, " +
235
- "assumptions made where the specification is silent, spec gaps the person should decide, and pitfalls. Never " +
240
+ "assumptions made where the specification is silent, and pitfalls. Never " +
236
241
  "requirements: the specification is never an assumption.",
237
242
  promptSnippet: "leave the next agent what you learned and assumed about this codebase",
238
243
  parameters: Type.Object({
@@ -283,7 +288,7 @@ export function sectionNamed(name) {
283
288
  /** How many times an agent that finishes without update_notes is asked to call it. */
284
289
  export const NOTES_ASKS = 3;
285
290
  /** The turn that asks for the notes: update_notes is the only tool it may use - the work it follows is done. */
286
- export const NOTES_TURN = { onlyTools: ["update_notes"] };
291
+ export const NOTES_TURN = { onlyTools: ["update_notes", "report_spec_gap"] };
287
292
  /** What the notes turn says first, once the harness's own run of `script` has passed on the work. */
288
293
  export function passedOutcome(script) {
289
294
  return (`The harness ran the project's ${script} on your work itself, and it passed: your work is done and accepted. ` +
@@ -327,10 +332,53 @@ export function recordPulse(buildDir, text, source) {
327
332
  // The notes are a help, not the work.
328
333
  }
329
334
  }
330
- /** The lines of a section added since `before` - a pulse's new spec gaps, say - without when and where they came from. */
331
- export function linesAddedSince(before, buildDir, section) {
332
- const known = new Set(before[section]);
333
- return readNotes(buildDir)[section].filter((line) => !known.has(line)).map((line) => line.replace(/^\[[^\]]*\]\s*/, ""));
335
+ /** Every spec gap reported in this process, with the codebase it was met in: a pulse reads those since it started. */
336
+ const reportedGaps = [];
337
+ /** Where the spec gaps stand now: what `gapsSince` reads from. */
338
+ export function gapMark() {
339
+ return reportedGaps.length;
340
+ }
341
+ /** The spec gaps reported in these codebases since `mark`. */
342
+ export function gapsSince(mark, dirs) {
343
+ const wanted = new Set(dirs.map((dir) => resolve(dir)));
344
+ return reportedGaps.slice(mark).filter((entry) => wanted.has(entry.dir)).map((entry) => entry.gap);
345
+ }
346
+ /**
347
+ * `report_spec_gap`: a behaviour the specification leaves open that the person would want to decide, put to them as
348
+ * the pulse ends - never into the notes, which are the next agent's. What the agent decided meanwhile goes into the
349
+ * notes' assumptions, so the code built on it is known for what it is until the specification settles it.
350
+ */
351
+ export function createSpecGapTool(buildDir, source) {
352
+ return defineTool({
353
+ name: "report_spec_gap",
354
+ label: "Report a spec gap",
355
+ description: "Report a spec gap: a behaviour the specification leaves open that the person would want to decide - not a detail " +
356
+ "of how. Decide it meanwhile and carry on: the person is asked once the work ends, and what they decide is " +
357
+ "written into the specification. The assumption you made is added to the notes' assumptions for you.",
358
+ promptSnippet: "tell the person about a behaviour the specification leaves open, and what you decided meanwhile",
359
+ parameters: Type.Object({
360
+ question: Type.String({ description: 'The question it leaves the person, one line: "What happens to a contact archived in HubSpot after it was synced?"' }),
361
+ assumption: Type.String({
362
+ description: "What you decided meanwhile, as its line in the notes' assumptions - with the tests that assert it, when any do: " +
363
+ '"An archived contact is left as synced - asserted in sync.test.ts, archived contacts".',
364
+ }),
365
+ }),
366
+ async execute(_id, params) {
367
+ const question = params.question.replace(/\s+/g, " ").trim();
368
+ const assumption = params.assumption.replace(/\s+/g, " ").trim();
369
+ if (question === "" || assumption === "")
370
+ throw new Error("Nothing reported - a spec gap needs both the question it leaves and the assumption you made meanwhile.");
371
+ // However many there are, spec gaps are put to the person - never a reason to stop the pulse.
372
+ reportedGaps.push({ dir: resolve(buildDir), gap: { question, assumption } });
373
+ const known = readNotes(buildDir).assumptions.some((line) => body(line) === assumption);
374
+ if (!known)
375
+ recordNote(buildDir, "assumptions", assumption, source ?? "spec gap");
376
+ return {
377
+ content: [{ type: "text", text: `Reported. The assumption is in the notes' assumptions${known ? " already" : " now"} - do not add it again.` }],
378
+ details: { question, assumption },
379
+ };
380
+ },
381
+ });
334
382
  }
335
383
  /** The notes, for an agent that reads them but leaves none - a judge. Empty when there are none. */
336
384
  export function notesToRead(buildDir) {
@@ -367,6 +415,7 @@ export function notesForSession(buildDir, source) {
367
415
  return true;
368
416
  };
369
417
  return {
418
+ gapTool: createSpecGapTool(buildDir, source),
370
419
  tool: createNotesTool(buildDir, (text) => {
371
420
  written = true;
372
421
  knownNotes.set(file, text);
@@ -9,9 +9,9 @@ import { HStack } from "@earendil-works/pi-tui";
9
9
  */
10
10
  /** Narrower than this, the chat and the panel have no room side by side: the panel takes the whole screen. */
11
11
  const SHARED_FROM = 100;
12
- /** The columns the panel takes beside the chat - 0 when the terminal is too narrow to share. */
12
+ /** The columns the panel takes beside the chat: half the screen, the rule between them in the middle - 0 when the terminal is too narrow to share. */
13
13
  export function dockedWidth(columns) {
14
- return columns < SHARED_FROM ? 0 : Math.max(48, Math.floor(columns * 0.45));
14
+ return columns < SHARED_FROM ? 0 : Math.floor(columns / 2);
15
15
  }
16
16
  /**
17
17
  * The chat narrowed by `panelWidth()` columns, read at each frame - 0 gives it the whole width. Returns what gives it the
@@ -55,3 +55,32 @@ export function clickToFocus(tui, chatWidth, side) {
55
55
  screen.handleMouseEvent = handle;
56
56
  };
57
57
  }
58
+ /**
59
+ * A line drawn faint - the side that does not have the keys, in the shade. Faint (SGR 2) holds through a line's colours;
60
+ * only a reset - SGR 0, or 22 - ends it, so it is set again after each one. A line holding an image is left as it is.
61
+ */
62
+ export function shade(line) {
63
+ // Kitty's image escape, or iTerm2's.
64
+ if (line.includes("\x1b_G") || line.includes("\x1b]1337;File="))
65
+ return line;
66
+ const again = line.replace(/\x1b\[([\d;]*)m/g, (sgr, params) => (params.split(";").some((param) => param === "" || param === "0" || param === "22") ? `${sgr}${FAINT}` : sgr));
67
+ return `${FAINT}${again}${NOT_FAINT}`;
68
+ }
69
+ const FAINT = "\x1b[2m";
70
+ const NOT_FAINT = "\x1b[22m";
71
+ /**
72
+ * The chat in the shade while `shaded()`: each of its rows drawn faint as the screen lays the overlays over it - the
73
+ * docked panel the last of them, which covers its own columns. Returns what takes the shade off.
74
+ */
75
+ export function shadeChat(tui, shaded) {
76
+ const screen = tui;
77
+ const composite = screen.compositeLineAt;
78
+ if (typeof composite !== "function")
79
+ return () => { };
80
+ screen.compositeLineAt = function (base, overlay, col, width, total) {
81
+ return composite.call(this, shaded() ? shade(base) : base, overlay, col, width, total);
82
+ };
83
+ return () => {
84
+ screen.compositeLineAt = composite;
85
+ };
86
+ }
@@ -278,8 +278,10 @@ function itemText(serialized) {
278
278
  return item;
279
279
  const { markdown, [ACCEPTANCE_TESTS]: tests } = (item ?? {});
280
280
  const lines = [markdown ?? JSON.stringify(item)];
281
+ // Each test is an item of the tree, its text in its markdown - as spec-steps.ts `itemText` reads it.
282
+ const testText = (test) => (typeof test === "string" ? test : (test?.markdown ?? JSON.stringify(test)));
281
283
  if (Array.isArray(tests) && tests.length > 0)
282
- lines.push(" <acceptance tests>", ...tests.map((test) => ` ${String(test)}`), " </acceptance tests>");
284
+ lines.push(" <acceptance tests>", ...tests.map((test) => ` ${testText(test).replace(/\n/g, "\n ")}`), " </acceptance tests>");
283
285
  return lines.join("\n");
284
286
  }
285
287
  const indent = (text, by) => text.split("\n").map((line) => `${by}${line}`).join("\n");
@@ -14,13 +14,13 @@ import { buildSpecDiffPrompt, describeSpecChange, reviewedSpec, sectionModules }
14
14
  import { abandonWork, recordHistory, startWork } from "./lineage.js";
15
15
  import { createWorkLedger, SUBMIT_WORK_NOTE } from "./submit-work.js";
16
16
  import { CODER_COMPACTION } from "./compaction.js";
17
- import { linesAddedSince, NOTES_TURN, notesForSession, passedOutcome, readNotes, recordNote, withNotes } from "./notes.js";
17
+ import { gapMark, gapsSince, NOTES_TURN, notesForSession, passedOutcome, recordNote, withNotes } from "./notes.js";
18
18
  import { createLoopWatch, createScriptTool, failureSignature, findTestScripts, projectRootFor, requireTestScripts, runUnitScript, STUCK_ROUNDS, withoutConformance, withTestingScripts, } from "./test-scripts.js";
19
19
  import { createWebTools, WEB_TOOLS } from "./web.js";
20
20
  import { createCanItFailTool, createMyDiffTool, selfCheckNote } from "./self-check.js";
21
21
  import { createServiceTools } from "./services.js";
22
22
  import { createUnitGate } from "./unit-gate.js";
23
- import { toolchainError } from "./environment-error.js";
23
+ import { timeLimitError, toolchainError } from "./environment-error.js";
24
24
  import { workspaceBlock } from "./workspace.js";
25
25
  import { ATTEMPTS, describeStop } from "./stop-report.js";
26
26
  import { summarizeDiff } from "./job-panel.js";
@@ -268,8 +268,8 @@ export async function renderSpecDiff(options) {
268
268
  ...(options.cases !== undefined ? { cases: options.cases } : {}),
269
269
  });
270
270
  const digestsBefore = digestFiles(buildDir);
271
- // The notes as the agent finds them: what it adds - a spec gap above all - is told with the result.
272
- const notesBefore = readNotes(buildDir);
271
+ // The spec gaps its agents report from here on are told with the result.
272
+ const gapsFrom = gapMark();
273
273
  /** What the debugger found, each time it was asked: the pulse's line in the notes says it. */
274
274
  const diagnosed = [];
275
275
  const runs = [];
@@ -305,6 +305,7 @@ export async function renderSpecDiff(options) {
305
305
  cwd: buildDir,
306
306
  customTools: [
307
307
  notes.tool,
308
+ notes.gapTool,
308
309
  work.tool,
309
310
  ...createWebTools(),
310
311
  createScriptTool({ kind: "unit", script: scripts.unit, run: () => runUnitScript(buildDir, scripts.unit), runs, watch: loop }),
@@ -313,7 +314,7 @@ export async function renderSpecDiff(options) {
313
314
  ...fence.tools,
314
315
  createCopyResourceTool({ resources: linkedResources, dir: buildDir }),
315
316
  ],
316
- tools: [...fence.names, "run_unit_tests", ...selfChecks.map((tool) => tool.name), ...services.names, "update_notes", "submit_work", "copy_resource", ...WEB_TOOLS],
317
+ tools: [...fence.names, "run_unit_tests", ...selfChecks.map((tool) => tool.name), ...services.names, "update_notes", "report_spec_gap", "submit_work", "copy_resource", ...WEB_TOOLS],
317
318
  model: options.model,
318
319
  label: `${moduleName} spec change coding agent`,
319
320
  role: "coding",
@@ -333,6 +334,8 @@ export async function renderSpecDiff(options) {
333
334
  let draftAsks = 0;
334
335
  /** What the unit testing script needs and does not find, last run. */
335
336
  let missing;
337
+ /** The unit testing script needs more time than a test run may take - the debugger's finding: the pulse stops on it. */
338
+ let slow;
336
339
  /** The last round failed on the script's static checks alone: the unit tests passed. */
337
340
  let staticOnly = false;
338
341
  const testRemovals = [];
@@ -423,24 +426,35 @@ export async function renderSpecDiff(options) {
423
426
  seenFailures.set(key, seen);
424
427
  let prompt = verdict.prompt;
425
428
  const circling = loop.stuck;
426
- if (circling || (key !== undefined && seen >= STUCK_ROUNDS)) {
429
+ // A run the time limit stopped is looked at at once: whether something hangs, or the suite only needs more time.
430
+ const timedOut = verdict.run.timedOut === true;
431
+ if (circling || timedOut || (key !== undefined && seen >= STUCK_ROUNDS)) {
427
432
  if (key !== undefined)
428
433
  seenFailures.delete(key);
429
434
  const times = circling ? Math.max(seen, loop.failing) : seen;
430
435
  loop.reset();
431
436
  stage(circling
432
437
  ? `the coding agent is going round in circles - ${times} failing runs - a debugger looks at it with fresh eyes`
433
- : `the same failure ${seen} times - a debugger looks at it with fresh eyes`);
438
+ : timedOut && seen < STUCK_ROUNDS
439
+ ? `the unit testing script did not finish in ${Math.round(verdict.run.timeoutMs / 1000)}s - a debugger works out whether something hangs`
440
+ : `the same failure ${seen} times - a debugger looks at it with fresh eyes`);
434
441
  const found = await debugFailure({
435
442
  buildDir,
436
443
  failure: verdict.run.output,
437
444
  times,
438
445
  task: steeredChange() ?? codingText ?? changeText ?? options.diff ?? "",
439
446
  script: verdict.script,
447
+ run: verdict.run,
440
448
  label: `${moduleName} spec change`,
441
449
  ...(debuggerModel !== undefined ? { model: debuggerModel } : {}),
442
450
  onDelta: options.onDelta,
443
451
  }).catch(() => undefined);
452
+ // Nothing hangs: the suite needs more time than a test run may take, and no agent can give it that.
453
+ if (found?.tooSlow === true) {
454
+ stage(`the unit testing script needs more than the ${Math.round(verdict.run.timeoutMs / 1000)}s a test run may take - stopping`);
455
+ slow = timeLimitError(verdict.run, { moduleName, script: verdict.script, kind: "unit", reason: found.rootCause });
456
+ return false;
457
+ }
444
458
  if (found !== undefined) {
445
459
  const cause = found.rootCause.split("\n")[0].slice(0, 240);
446
460
  reportActivity(` the debugger: ${cause.slice(0, 160)}`);
@@ -486,6 +500,7 @@ export async function renderSpecDiff(options) {
486
500
  ...(options.cases !== undefined ? { cases: options.cases } : {}),
487
501
  label: `${moduleName} spec change`,
488
502
  ...(scripts.runOn === "copy" ? { unitScript: scripts.unit } : {}),
503
+ testingScript: { path: relative(root, scripts.unit), content: readFileSync(scripts.unit, "utf8") },
489
504
  model: options.judgeModel ?? modelSpec(inheritModel(configForSpec(tip.plainFile).judgeModel, modelOnly(options.model))),
490
505
  send: async (next) => {
491
506
  response = await ask(executor, next, options.onDelta);
@@ -528,8 +543,8 @@ export async function renderSpecDiff(options) {
528
543
  testRemovals,
529
544
  files,
530
545
  ...(steering !== undefined && steering.steers > 0 ? { steered: [...steering.notes] } : {}),
531
- // Read before a rollback takes the notes back: a gap the agent met is the person's to decide, whether it landed or not.
532
- specGaps: linesAddedSince(notesBefore, buildDir, "gaps"),
546
+ // A gap the agent met is the person's to decide, whether it landed or not.
547
+ specGaps: gapsSince(gapsFrom, [buildDir]),
533
548
  diagnosed,
534
549
  };
535
550
  /** The change landed: what the code is built from is now the spec with the change in, committed with it. */
@@ -543,13 +558,17 @@ export async function renderSpecDiff(options) {
543
558
  stage(unitReviewStopped ? "the unit tests pass but still leave the change untested - undoing the change" : "unit tests NOT passing - undoing the change");
544
559
  abandonWork(unit, moduleName, missing !== undefined
545
560
  ? "the unit testing script could not run"
546
- : staticOnly
547
- ? "the spec change did not pass its static checks"
548
- : "the spec change did not pass its unit tests");
561
+ : slow !== undefined
562
+ ? "the unit testing script needs more time than a test run may take"
563
+ : staticOnly
564
+ ? "the spec change did not pass its static checks"
565
+ : "the spec change did not pass its unit tests");
549
566
  // The environment lacks what the testing script needs: the pulse stops, rolled back, and the person is told.
550
567
  if (missing !== undefined) {
551
568
  throw toolchainError(missing, { moduleName, script: scripts.unit, kind: "unit" });
552
569
  }
570
+ if (slow !== undefined)
571
+ throw slow;
553
572
  return { ...base, passed: false, stoppedAt: "unit tests", ...(staticOnly && !unitReviewStopped ? { staticChecks: true } : {}) };
554
573
  }
555
574
  stage("unit tests pass - landing the change on them");
@@ -562,8 +581,10 @@ export function specGapsText(gaps) {
562
581
  return [];
563
582
  return [
564
583
  "",
565
- "Spec gaps its agents met - the spec does not say, so they decided. Put each to the user, and write the ones they decide into the spec:",
566
- ...gaps.map((gap) => `- ${gap}`),
584
+ "Spec gaps its agents met - the spec does not say, so they decided, each as an assumption in the codebase's notes. Put " +
585
+ "each to the user, and write the ones they decide into the spec - the pulse that builds it is told the assumption " +
586
+ "it replaces:",
587
+ ...gaps.map((gap) => `- ${gap.question} Meanwhile: ${gap.assumption}`),
567
588
  ];
568
589
  }
569
590
  /** What a rendered spec change did, for the agent and the person. */
@@ -21,7 +21,7 @@ import { keepingTestsUnchanged, listFiles } from "./testing.js";
21
21
  * job ends on it: tests that pass on their own are not done until the whole suite passes with them.
22
22
  */
23
23
  /** On top of the file tools, which are fenced to the build directory by `confine.ts`. */
24
- const FIX_TOOLS = ["run_unit_tests", "run_conformance_tests", "update_notes"];
24
+ const FIX_TOOLS = ["run_unit_tests", "run_conformance_tests", "update_notes", "report_spec_gap"];
25
25
  const DEFAULT_MAX_REGRESSION_ROUNDS = ATTEMPTS.regression;
26
26
  /** The module's conformance tests, when there are any: the chain's, copied up, and its own. */
27
27
  function conformanceDirs(conformanceRoot, moduleName) {
@@ -39,6 +39,7 @@ async function repairRegression(input) {
39
39
  cwd: input.buildDir,
40
40
  customTools: [
41
41
  notes.tool,
42
+ notes.gapTool,
42
43
  ...createWebTools(),
43
44
  ...fence.tools,
44
45
  createScriptTool({ kind: "unit", script: input.scripts.unit, run: () => runUnitScript(input.buildDir, input.scripts.unit), runs }),
@@ -1,6 +1,6 @@
1
- import { copyFileSync, existsSync, mkdirSync, readdirSync, rmSync } from "node:fs";
1
+ import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync } from "node:fs";
2
2
  import { basename, dirname, join, resolve } from "node:path";
3
- import { PLAIN_SOURCE_FILE_EXTENSION, plainFileParser, withParseCache } from "../plain/parser.js";
3
+ import { IMPORT_DIRECTIVE, PLAIN_SOURCE_FILE_EXTENSION, plainFileParser, readPlainSourceMetadata, withParseCache } from "../plain/parser.js";
4
4
  import { getFrids, getInheritedFunctionalSpecs } from "../plain/spec.js";
5
5
  import { buildDirUnder, modulesRoot, projectDirOf } from "./paths.js";
6
6
  import { notesPath } from "./notes.js";
@@ -54,6 +54,59 @@ options = {}) {
54
54
  visit(resolve(plainFile), []);
55
55
  return chain;
56
56
  }
57
+ /** The .plain files that import `plainFile` - read from beside it, and, kept in a `template/` folder, from the folder it is in. */
58
+ function importersOf(plainFile) {
59
+ const file = resolve(plainFile);
60
+ const dir = dirname(file);
61
+ const name = basename(file, PLAIN_SOURCE_FILE_EXTENSION);
62
+ const dirs = [dir, ...(basename(dir) === "template" ? [dirname(dir)] : [])];
63
+ return dirs.flatMap((at) => {
64
+ let names;
65
+ try {
66
+ names = readdirSync(at).filter((entry) => entry.endsWith(PLAIN_SOURCE_FILE_EXTENSION));
67
+ }
68
+ catch {
69
+ return [];
70
+ }
71
+ return names
72
+ .map((entry) => join(at, entry))
73
+ .filter((path) => {
74
+ if (path === file || !resolveTemplateDirs(path).includes(dir))
75
+ return false;
76
+ try {
77
+ return (readPlainSourceMetadata(readFileSync(path, "utf8")).metadata[IMPORT_DIRECTIVE] ?? []).includes(name);
78
+ }
79
+ catch {
80
+ return false;
81
+ }
82
+ });
83
+ });
84
+ }
85
+ /**
86
+ * Whether a .plain file is an import module - one a .plain file imports, or one kept in a `template/` folder - rather than
87
+ * a module of its own. It is no codebase: its reqs and definitions are built in the modules that import it.
88
+ */
89
+ export function isImportModule(plainFile) {
90
+ return basename(dirname(resolve(plainFile))) === "template" || importersOf(plainFile).length > 0;
91
+ }
92
+ /** The modules of their own that import an import module, through any import modules between. */
93
+ export function importingModules(plainFile) {
94
+ const found = new Set();
95
+ const seen = new Set();
96
+ const walk = (file) => {
97
+ if (seen.has(file))
98
+ return;
99
+ seen.add(file);
100
+ for (const importer of importersOf(file)) {
101
+ if (isImportModule(importer))
102
+ walk(importer);
103
+ else
104
+ found.add(importer);
105
+ }
106
+ };
107
+ walk(resolve(plainFile));
108
+ return [...found];
109
+ }
57
110
  /**
58
111
  * The codebase a change to `plainFile`'s module lands in: the furthest-built module whose
59
112
  * requires chain includes it, with that chain.
@@ -16,7 +16,7 @@ export function createSidePanel(options) {
16
16
  const backKeys = () => {
17
17
  if (options.dock === undefined)
18
18
  return [`esc or ${options.key} back to the chat`];
19
- return options.dock.docked() ? ["s full screen", "esc to the chat", `${options.key} close`] : ["s beside the chat", `esc or ${options.key} back to the chat`];
19
+ return options.dock.docked() ? ["s full screen", "esc or ⌥⌘← to the chat", `${options.key} close`] : ["s beside the chat", `esc or ${options.key} back to the chat`];
20
20
  };
21
21
  const waitingNote = () =>
22
22
  // A question asked while it showed waits in the chat, unanswered: the person is told it is there.
@@ -89,6 +89,19 @@ export function describeStop(facts) {
89
89
  `though they were sent back for it again and again. Still missing:\n${facts.conformance.unlabelled.map((line) => ` - ${line}`).join("\n")}`;
90
90
  todo = ["If the test reqs leave open where the tests go or how they are built, say so exactly - the folder, the files, the build."];
91
91
  }
92
+ else if (facts.at === "conformance" && facts.conformance?.escalation !== undefined) {
93
+ const { error, reason, evidence, changedNothing } = facts.conformance.escalation;
94
+ why =
95
+ `its conformance tests kept failing with ${error}. The conformance agent's fix of the tests did not make it go away, so ` +
96
+ `a judge looked again and found it the code's; the coding agent ${changedNothing === true ? "changed nothing for it" : "fixed the code, and the run still failed the same way"}. ` +
97
+ `Why the judge found it the code's:\n${reason.trim().split("\n").map((line) => ` ${line}`).join("\n")}` +
98
+ (evidence.length > 0 ? `\n On the evidence:\n${evidence.map((item) => ` - ${item.source.trim()}: ${item.shows.trim().replace(/\s+/g, " ")}`).join("\n")}` : "");
99
+ todo = [
100
+ "If the code has to produce something the spec leaves open - a build output, an entry point, a module the conformance testing " +
101
+ "script runs or checks for - say exactly what in the implementation reqs: its path and how it is built.",
102
+ "If the conformance testing script expects something the spec does not ask for, change the script or the test reqs so the two agree.",
103
+ ];
104
+ }
92
105
  else if (facts.at === "conformance" && facts.conformance?.stuck !== undefined) {
93
106
  const { error, times, rounds, changedNothing } = facts.conformance.stuck;
94
107
  why =
@@ -335,7 +335,15 @@ async function runScript(cwd, script, args, logDir, options = {}) {
335
335
  const noted = withScriptLineNote(text, script);
336
336
  // Its failures, counts and end are what the agent is shown; the whole is kept, a read_output away.
337
337
  const log = keepTestOutput(logDir, basename(script), noted);
338
- return { command: shown(script), exitCode: timedOut === true ? 124 : exitCode, output: shownTestOutput(noted, log), timedOut: timedOut === true, timeoutMs, ...(log !== undefined ? { log } : {}) };
338
+ return {
339
+ command: shown(script),
340
+ exitCode: timedOut === true ? 124 : exitCode,
341
+ output: shownTestOutput(noted, log),
342
+ timedOut: timedOut === true,
343
+ timeoutMs,
344
+ elapsedMs: Date.now() - started,
345
+ ...(log !== undefined ? { log } : {}),
346
+ };
339
347
  }
340
348
  /** What bash says when a line of the script itself cannot run: a command or file that is not there, or not runnable. */
341
349
  const OWN_LINE_FAILURE = /: (No such file or directory|command not found|Permission denied)$/;
@@ -589,6 +597,19 @@ function loopNotice(loop) {
589
597
  "the failure with fresh eyes, and you will be given what it finds.",
590
598
  ].join("\n");
591
599
  }
600
+ /**
601
+ * How long a run took against its time limit, in a line: for whoever judges it or works out its cause - so a run that
602
+ * ended on its own is never taken for one the limit stopped, nor the other way.
603
+ */
604
+ export function runTiming(run) {
605
+ const seconds = (ms) => `${(ms / 1000).toFixed(1)}s`;
606
+ const limit = seconds(run.timeoutMs);
607
+ if (run.timedOut === true) {
608
+ return `The time limit stopped this run: it had run ${seconds(run.elapsedMs ?? run.timeoutMs)} of the ${limit} a test run may take, and had not finished.`;
609
+ }
610
+ const took = run.elapsedMs !== undefined ? `after ${seconds(run.elapsedMs)} of the ${limit} a test run may take` : `well within the ${limit} a test run may take`;
611
+ return `This run ended on its own - exit ${run.exitCode}, ${took}. The time limit did not stop it and played no part in how it ended.`;
612
+ }
592
613
  /** A run the time limit stopped: failed, and what most often keeps a test run from ending. */
593
614
  function timedOutFailure(run, what) {
594
615
  const limit = Math.round(run.timeoutMs / 1000);
@@ -63,6 +63,17 @@ export function unitReviewTask(options) {
63
63
  options.spec.trim(),
64
64
  "</specification>",
65
65
  "",
66
+ ...(options.testingScript !== undefined
67
+ ? [
68
+ "The harness runs the unit tests with this script, and it decides whether they pass. Read it for which tests it",
69
+ "runs and how: a test it never reaches - a file it does not pick up, a folder it does not look in - is not run, and",
70
+ "checks nothing.",
71
+ `<unit testing script path="${options.testingScript.path}">`,
72
+ options.testingScript.content.trimEnd(),
73
+ "</unit testing script>",
74
+ "",
75
+ ]
76
+ : []),
66
77
  ...(options.cases !== undefined ? [...codeReviewCasesBlock(options.cases), ""] : []),
67
78
  // A job that touches no functional spec is held only to that: the checks of behaviour are not its.
68
79
  ...(mechanical ? [] : ["Check that the code does what the specification states for this work:"]),
@@ -19,7 +19,7 @@ its conformance tests too when they are on.
19
19
  --start-over start over, without asking, a codebase regenerated without a module it now requires -
20
20
  with no terminal to ask and without it, the run stops there
21
21
  --from-scratch delete the module's code and conformance tests (its lineage keeps them) and regenerate all of it
22
- --harness-model <p/id> config.turri.yaml's harness-model for this run: the planner, the conformance chooser, a diagnosis
22
+ --harness-model <p/id> config.turri.yaml's harness-model for this run: the planner and the conformance chooser
23
23
  --coding-model <p/id> its coding-model, --model too: the agents that build; p/id:level for how hard a model thinks
24
24
  --judge-model <p/id> its judge-model: the judge and the test review
25
25
  --review <true|false> its review: whether the code and its tests are reviewed once they pass
@@ -0,0 +1,4 @@
1
+ /** pi's line on the way out that says how to resume the session, with turri in place of pi as the command to run. */
2
+ export function resumeLine(text) {
3
+ return text.replace(/^((?:\x1b\[[0-9;]*m)*To resume this session:(?:\x1b\[[0-9;]*m)*\s+)pi(?= )/m, "$1turri");
4
+ }
@@ -1,8 +1,8 @@
1
- import { readdirSync, readFileSync } from "node:fs";
2
- import { basename, dirname, join, relative, resolve } from "node:path";
1
+ import { basename, relative, resolve } from "node:path";
3
2
  import { defineTool } from "@earendil-works/pi-coding-agent";
4
3
  import { Type } from "typebox";
5
- import { IMPORT_DIRECTIVE, PLAIN_SOURCE_FILE_EXTENSION, plainFileParser, readPlainSourceMetadata } from "./plain/parser.js";
4
+ import { plainFileParser } from "./plain/parser.js";
5
+ import { importingModules, isImportModule } from "./regenerate/requires.js";
6
6
  import { resolveTemplateDirs } from "./regenerate/paths.js";
7
7
  import { applySpecDiff, plainFilesOf, revertSpecDiff } from "./regenerate/regenerate.js";
8
8
  import { MissingFunctionalitiesError } from "./plain/errors.js";
@@ -62,24 +62,12 @@ export function createSpecDiffTools(options) {
62
62
  // file that cannot be parsed. A module with no functional specs yet is not broken. An import
63
63
  // module - definitions only, say - is not a module of its own: it is parsed as the modules
64
64
  // that import it read it, the one the change is built for among them.
65
- const importers = (file) => {
66
- const name = basename(file, ".plain");
67
- const dir = dirname(resolve(cwd, file));
68
- return readdirSync(dir)
69
- .filter((entry) => entry.endsWith(".plain"))
70
- .map((entry) => join(dir, entry))
71
- .filter((path) => {
72
- try {
73
- return (readPlainSourceMetadata(readFileSync(path, "utf8")).metadata[IMPORT_DIRECTIVE] ?? []).includes(name);
74
- }
75
- catch {
76
- return false;
77
- }
78
- });
79
- };
80
- const imported = importedNames(resolveTemplateDirs(resolve(cwd, params.plain_file)));
81
65
  const toParse = [
82
- ...new Set([params.plain_file, ...touched].flatMap((file) => imported.has(basename(file, ".plain")) ? importers(file).filter((path) => !imported.has(basename(path, ".plain"))) : [resolve(cwd, file)])),
66
+ ...new Set([params.plain_file, ...touched].flatMap((file) => {
67
+ const path = resolve(cwd, file);
68
+ const importing = isImportModule(path) ? importingModules(path) : [];
69
+ return importing.length > 0 ? importing : [path];
70
+ })),
83
71
  ];
84
72
  for (const path of toParse) {
85
73
  const file = relative(cwd, path);
@@ -129,27 +117,3 @@ export function createSpecDiffTools(options) {
129
117
  },
130
118
  };
131
119
  }
132
- /** The module names some .plain file in these folders imports: import modules, not codebases. */
133
- export function importedNames(dirs) {
134
- const names = new Set();
135
- for (const dir of dirs) {
136
- let entries;
137
- try {
138
- entries = readdirSync(dir).filter((name) => name.endsWith(PLAIN_SOURCE_FILE_EXTENSION));
139
- }
140
- catch {
141
- continue;
142
- }
143
- for (const entry of entries) {
144
- try {
145
- const imports = readPlainSourceMetadata(readFileSync(join(dir, entry), "utf8")).metadata[IMPORT_DIRECTIVE];
146
- for (const name of imports ?? [])
147
- names.add(name);
148
- }
149
- catch {
150
- // A file that does not read imports nothing.
151
- }
152
- }
153
- }
154
- return names;
155
- }