@yagni-app/code-staging 1.2.14-staging.2347.1 → 1.2.14-staging.2349.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -128,6 +128,12 @@ export interface AttemptResult extends CloudIdentity {
128
128
  * adds its own parts.
129
129
  */
130
130
  pullRequestText?: string;
131
+ /**
132
+ * How a plan turn asked for the plan's UI preview left it (`sessionLane.ts`):
133
+ * a whole page, its word that nothing on a screen changes, or neither.
134
+ * Absent on every other turn: the host then drafts the preview itself.
135
+ */
136
+ uiPreview?: "drawn" | "none" | "not_drawn";
131
137
  }
132
138
  /** The findings the result reports, blocking first. */
133
139
  export declare function openFindings(findings: readonly Finding[]): OpenFinding[];
@@ -78,6 +78,32 @@ export declare const MAX_PULL_REQUEST_TEXT_CHARS = 20000;
78
78
  export declare function pullRequestTextOf(text: string | null): string | null;
79
79
  /** What a plan turn may use: everything that reads or runs, nothing that edits. */
80
80
  export declare const PLAN_TURN_TOOLS: string[];
81
+ /**
82
+ * The brief the host writes beside the ticket when the plan turn draws the
83
+ * plan's UI preview itself (a yak's; `attempts/sessionPreview.ts`): the page's
84
+ * contract and how to work. Absent, the turn draws none and its result says
85
+ * nothing about one, so the host drafts the preview as it always did.
86
+ */
87
+ export declare const UI_PREVIEW_BRIEF_FILE_NAME = "ui-preview-brief.md";
88
+ /** The preview's page, in the output folder (the host's `SESSION_PREVIEW_FILE`). */
89
+ export declare const UI_PREVIEW_FILE_NAME = "preview.html";
90
+ /** What the turn writes instead of a page when the plan changes nothing on a screen. */
91
+ export declare const UI_PREVIEW_NONE_FILE_NAME = "ui-preview-none.md";
92
+ /** The most a preview's page may weigh (the host's `MAX_SESSION_PREVIEW_BYTES`). */
93
+ export declare const MAX_UI_PREVIEW_BYTES: number;
94
+ /** How a plan turn asked for its UI preview left it (`AttemptResult.uiPreview`). */
95
+ export type UiPreviewOutcome = "drawn" | "none" | "not_drawn";
96
+ /**
97
+ * What a plan turn that draws its preview may use: the plan turn's tools, and
98
+ * write and edit for the page. Anything they change in the repository is put
99
+ * back before the plan's gate, as a shell's change is ({@link putBackPlanTurn}).
100
+ */
101
+ export declare const PLAN_PREVIEW_TURN_TOOLS: string[];
102
+ /**
103
+ * Why a preview's page cannot stand, as the host's judge would refuse it
104
+ * (`parseDesignDocument`): empty when it is a whole page within size. Pure.
105
+ */
106
+ export declare function previewPageProblems(text: string | null): string[];
81
107
  /** What a turn that builds may use: the plan turn's tools and the writes. */
82
108
  export declare const BUILD_TURN_TOOLS: string[];
83
109
  /** The run's files under the run user's home: its one session, and the playbook it runs on. */
@@ -110,6 +136,11 @@ export interface TurnFacts {
110
136
  * (YAG-1162). Any stage, the plan's included.
111
137
  */
112
138
  newSinceLastTurn?: string | null;
139
+ /**
140
+ * The input folder has `ui-preview-brief.md`: this plan turn draws the
141
+ * plan's UI preview itself. Only ever on a plan turn.
142
+ */
143
+ preview?: boolean;
113
144
  /** Who writes the change, as the host said (`codeWriter.ts`); absent, the session. */
114
145
  codeWriter?: CodeWriter;
115
146
  }
@@ -198,6 +229,8 @@ export interface SessionTurnDeps {
198
229
  writeFile?: (path: string, text: string) => void;
199
230
  exists?: (path: string) => boolean;
200
231
  mkdir?: (path: string) => void;
232
+ /** Removes a file, missing or not: the last turn's preview files. */
233
+ remove?: (path: string) => void;
201
234
  exec?: WorktreeExec;
202
235
  snapshot?: typeof snapshotWorkspace;
203
236
  /** The run user's home; the Machine's `/home/user`. */
@@ -43,7 +43,7 @@
43
43
  * kept: the review loop and its round cap, the fixer, and the plan stage's
44
44
  * own clock.
45
45
  */
46
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
46
+ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
47
47
  import { homedir } from "node:os";
48
48
  import { isAbsolute, join, relative } from "node:path";
49
49
  import { openPlacedValues } from "../placedValues.js";
@@ -105,6 +105,48 @@ export const PLAN_TURN_TOOLS = [
105
105
  "prototype_read",
106
106
  "subagent",
107
107
  ];
108
+ /**
109
+ * The brief the host writes beside the ticket when the plan turn draws the
110
+ * plan's UI preview itself (a yak's; `attempts/sessionPreview.ts`): the page's
111
+ * contract and how to work. Absent, the turn draws none and its result says
112
+ * nothing about one, so the host drafts the preview as it always did.
113
+ */
114
+ export const UI_PREVIEW_BRIEF_FILE_NAME = "ui-preview-brief.md";
115
+ /** The preview's page, in the output folder (the host's `SESSION_PREVIEW_FILE`). */
116
+ export const UI_PREVIEW_FILE_NAME = "preview.html";
117
+ /** What the turn writes instead of a page when the plan changes nothing on a screen. */
118
+ export const UI_PREVIEW_NONE_FILE_NAME = "ui-preview-none.md";
119
+ /** The most a preview's page may weigh (the host's `MAX_SESSION_PREVIEW_BYTES`). */
120
+ export const MAX_UI_PREVIEW_BYTES = 4 * 1024 * 1024;
121
+ /**
122
+ * What a plan turn that draws its preview may use: the plan turn's tools, and
123
+ * write and edit for the page. Anything they change in the repository is put
124
+ * back before the plan's gate, as a shell's change is ({@link putBackPlanTurn}).
125
+ */
126
+ export const PLAN_PREVIEW_TURN_TOOLS = [...PLAN_TURN_TOOLS, "write", "edit"];
127
+ const VIEW_MARKER = /\sdata-yagni-(?:view|state|choice)\s*=/i;
128
+ /**
129
+ * Why a preview's page cannot stand, as the host's judge would refuse it
130
+ * (`parseDesignDocument`): empty when it is a whole page within size. Pure.
131
+ */
132
+ export function previewPageProblems(text) {
133
+ const html = text?.trim() ?? "";
134
+ if (!html)
135
+ return ["the file is missing or empty"];
136
+ const problems = [];
137
+ if (!/^(?:<!doctype html[^>]*>\s*)?<html[\s>]/i.test(html) && !/^<!doctype html/i.test(html)) {
138
+ problems.push("it must be a whole HTML document that starts with <!doctype html> or <html>");
139
+ }
140
+ if (!/<\/html>\s*$/i.test(html))
141
+ problems.push("it must end with </html>; it looks cut off");
142
+ if (VIEW_MARKER.test(html)) {
143
+ problems.push("it must carry no data-yagni-view, data-yagni-state or data-yagni-choice attribute");
144
+ }
145
+ if (Buffer.byteLength(html, "utf8") > MAX_UI_PREVIEW_BYTES) {
146
+ problems.push(`it is over ${MAX_UI_PREVIEW_BYTES / (1024 * 1024)} MB`);
147
+ }
148
+ return problems;
149
+ }
108
150
  /** What a turn that builds may use: the plan turn's tools and the writes. */
109
151
  export const BUILD_TURN_TOOLS = [
110
152
  ...PLAN_TURN_TOOLS,
@@ -212,16 +254,41 @@ const INPUTS = (inputDir) => [
212
254
  ].join("\n");
213
255
  /** A close recommendation sent back is a person's call that the ticket needs work. */
214
256
  const PLANNED_ANYWAY = "If the plan sent back recommended closing the ticket, the person chose to plan it anyway: plan the change, and do not recommend closing it again.";
215
- const PLAN_RULES = [
216
- "This turn you have read, grep, find, ls, bash, ask_yagni, team_read, prototype_read and subagent, and no edit or write tool.",
257
+ const PLAN_TOOLS = "read, grep, find, ls, bash, ask_yagni, team_read, prototype_read and subagent";
258
+ /**
259
+ * The plan turn's rules. With `preview` (the host's brief is in the input
260
+ * folder) the turn also draws the plan's UI preview: it gets write and edit
261
+ * for the preview's files, and the rule that says when to draw one.
262
+ */
263
+ const planRules = (preview) => [
264
+ preview
265
+ ? `This turn you have ${PLAN_TOOLS}, and write and edit for the UI preview's files in ${preview.outDir} only: never for a file in the repository.`
266
+ : `This turn you have ${PLAN_TOOLS}, and no edit or write tool.`,
267
+ ...PLAN_RULES_BEFORE_PREVIEW,
268
+ ...(preview ? [previewRule(preview.inputDir, preview.outDir)] : []),
269
+ ...PLAN_RULES_AFTER_PREVIEW,
270
+ ].join("\n\n");
271
+ /** When a plan turn draws the plan's UI preview, and what it writes when the plan has nothing to show. */
272
+ const previewRule = (inputDir, outDir) => `A person reads this plan beside its UI preview, and you draw it: read ${join(inputDir, UI_PREVIEW_BRIEF_FILE_NAME)} before you plan. ` +
273
+ `When the plan changes anything a person sees on a screen, write the preview to ${join(outDir, UI_PREVIEW_FILE_NAME)} as the brief says, before your plan message, ` +
274
+ "from what you read and ran this turn, and keep it in step with the plan whenever a review changes the plan. " +
275
+ `When the plan changes nothing on a screen, write ${join(outDir, UI_PREVIEW_NONE_FILE_NAME)} instead, with one line saying why, and draw nothing.`;
276
+ /** What a plan turn that left neither a page nor its none file is asked, once. */
277
+ const previewReask = (inputDir, outDir, problems) => `The plan has no UI preview yet: ${join(outDir, UI_PREVIEW_FILE_NAME)} cannot stand (${problems.join("; ")}), and ${join(outDir, UI_PREVIEW_NONE_FILE_NAME)} is not there. ` +
278
+ `If the plan changes anything a person sees on a screen, write the preview now as ${join(inputDir, UI_PREVIEW_BRIEF_FILE_NAME)} says. ` +
279
+ `If it changes nothing on a screen, write ${join(outDir, UI_PREVIEW_NONE_FILE_NAME)} with one line saying why. ` +
280
+ "Leave the plan as it is and do not send it again: answer in one line when the file is written.";
281
+ const PLAN_RULES_BEFORE_PREVIEW = [
217
282
  "Run whatever settles a fact: the tests, a type check, the app, Docker, a query. Change no tracked file and commit nothing: anything this turn changes in the repository is put back before the plan's gate.",
218
283
  `Call team_read with tool "${DECISIONS_READ_TOOL}" before you ask a person anything: a question an earlier decision on this repository already answers is not a question.`,
219
284
  "Check the ticket before you plan on it: each premise it states that the plan relies on, against the code, the git history or the data.",
285
+ ];
286
+ const PLAN_RULES_AFTER_PREVIEW = [
220
287
  "End the turn with the plan as your last message. Open it with ## Ticket check: a table of premise | checked, or not checkable | the command, file:line or query that shows it, one row per premise. Then ## Done when: the ticket's acceptance criteria, or yours when it has none, one line each. Then ## Goal (one sentence), ## Plan (numbered steps, each naming the file or function), ## Files to modify / create (say \"new\" beside a file the plan creates), ## Checks and ## Risks, then the plan-json block. Where there is a real choice of approach, ask it as a decision question with your recommendation and its alternatives; the plan's steps follow your recommendation.",
221
288
  'When the ticket check shows the problem is already gone, recommend closing the ticket instead of planning a change: ## Goal says so in one sentence, the ticket check holds the evidence, ## Plan says "Nothing to build." with no numbered steps, and the plan-json block carries "closeTicket" with the evidence and names no checks and no paths. A person then decides whether to close it.',
222
289
  PLAN_JSON_CONTRACT,
223
290
  "Then stop: a fresh reviewer reads the plan next, and its major findings come back to you in this session to revise. A person approves the plan before anything is built.",
224
- ].join("\n\n");
291
+ ];
225
292
  /** The one message a turn sends the session. Pure. */
226
293
  export function turnMessage(facts) {
227
294
  const at = (file) => join(facts.inputDir, file);
@@ -245,7 +312,7 @@ export function turnMessage(facts) {
245
312
  }
246
313
  if (newSince)
247
314
  parts.push(newSince);
248
- parts.push(PLAN_RULES);
315
+ parts.push(planRules(facts.preview ? { inputDir: facts.inputDir, outDir: facts.outDir } : undefined));
249
316
  return parts.join("\n\n");
250
317
  }
251
318
  if (facts.stage === "implement" && facts.resumed && !facts.continuation) {
@@ -527,6 +594,7 @@ async function runTurn(input, deps, pulse) {
527
594
  const writeFile = deps.writeFile ?? ((path, text) => writeFileSync(path, text));
528
595
  const exists = deps.exists ?? existsSync;
529
596
  const mkdir = deps.mkdir ?? ((path) => void mkdirSync(path, { recursive: true }));
597
+ const remove = deps.remove ?? ((path) => rmSync(path, { force: true }));
530
598
  const exec = deps.exec ?? finishExec;
531
599
  const snapshot = deps.snapshot ?? snapshotWorkspace;
532
600
  const runStage = deps.runStage ?? defaultRunStage;
@@ -550,6 +618,21 @@ async function runTurn(input, deps, pulse) {
550
618
  const baseSha = start.head;
551
619
  const preRunDirt = parseChangedPaths(start.status);
552
620
  const writes = input.stage !== "plan";
621
+ // The host's brief is there: this plan turn draws the plan's UI preview
622
+ // itself, and whatever an earlier turn left in its place goes first.
623
+ const drawsPreview = input.stage === "plan" && exists(join(input.inputDir, UI_PREVIEW_BRIEF_FILE_NAME));
624
+ const previewFile = join(input.outDir, UI_PREVIEW_FILE_NAME);
625
+ const previewNoneFile = join(input.outDir, UI_PREVIEW_NONE_FILE_NAME);
626
+ if (drawsPreview) {
627
+ for (const path of [previewFile, previewNoneFile]) {
628
+ try {
629
+ remove(path);
630
+ }
631
+ catch {
632
+ // One that cannot be removed is read as this turn's: the host's judge still reads the page.
633
+ }
634
+ }
635
+ }
553
636
  // A yak's System's other repositories, each as the turn found it, for the
554
637
  // read of a writing turn's change for a placed secret (YAG-1211).
555
638
  const others = [];
@@ -583,7 +666,7 @@ async function runTurn(input, deps, pulse) {
583
666
  id: stageId,
584
667
  agent: "session",
585
668
  model: SESSION_TIER,
586
- tools: writes ? BUILD_TURN_TOOLS : PLAN_TURN_TOOLS,
669
+ tools: writes ? BUILD_TURN_TOOLS : drawsPreview ? PLAN_PREVIEW_TURN_TOOLS : PLAN_TURN_TOOLS,
587
670
  taskTemplate: "{ticket}",
588
671
  };
589
672
  let decisionsRecorded = 0;
@@ -672,6 +755,7 @@ async function runTurn(input, deps, pulse) {
672
755
  sentBack: exists(join(input.inputDir, "sent-back-plan.md")),
673
756
  nextStepNote: input.stage === "plan" ? null : readOrNull(join(input.inputDir, NEXT_STEP_NOTE_FILE_NAME)),
674
757
  newSinceLastTurn: readOrNull(join(input.inputDir, NEW_SINCE_LAST_TURN_FILE_NAME)),
758
+ ...(drawsPreview ? { preview: true } : {}),
675
759
  // The env the turn's child runs on, so the message and the agents the
676
760
  // child is offered (`builtinAgents`) read one value.
677
761
  codeWriter: resolveCodeWriter(input.childEnv),
@@ -679,6 +763,7 @@ async function runTurn(input, deps, pulse) {
679
763
  const notes = [];
680
764
  const stages = [];
681
765
  let planText = "";
766
+ let uiPreview = null;
682
767
  let ran;
683
768
  try {
684
769
  let run = await turn(message);
@@ -746,6 +831,28 @@ async function runTurn(input, deps, pulse) {
746
831
  sections.length > 0
747
832
  ? [run.finalOutput.trimEnd(), ...sections].join("\n\n")
748
833
  : run.finalOutput;
834
+ if (drawsPreview && planText.trim()) {
835
+ // The preview is settled on the final plan, from the files alone: the
836
+ // session's words are the yak's in its thread, never a signal here.
837
+ const left = () => {
838
+ const problems = previewPageProblems(readOrNull(previewFile));
839
+ if (problems.length === 0)
840
+ return "drawn";
841
+ return readOrNull(previewNoneFile)?.trim() ? "none" : problems;
842
+ };
843
+ let settled = left();
844
+ if (typeof settled !== "string" &&
845
+ turnStop(run, false, input.signal?.aborted === true).stop === "clean") {
846
+ stages.push(await turn(previewReask(input.inputDir, input.outDir, settled)));
847
+ settled = left();
848
+ }
849
+ uiPreview = typeof settled === "string" ? settled : "not_drawn";
850
+ if (typeof settled !== "string") {
851
+ const note = `The plan turn left no UI preview that can stand (${settled.join("; ")}); the plan's gate opens without one.`;
852
+ notes.push(note);
853
+ input.writeErr(note);
854
+ }
855
+ }
749
856
  notes.push(...(await putBackPlanTurn(input.cwd, start, exec, snapshot, input.signal)));
750
857
  }
751
858
  const stop = turnStop(run, writes, input.signal?.aborted === true);
@@ -806,10 +913,13 @@ async function runTurn(input, deps, pulse) {
806
913
  const noted = { ...done.attempt, notes: [...notes, ...done.attempt.notes] };
807
914
  const attempt = writes
808
915
  ? withSessionAccount(noted, parseSessionReport(readOrNull(join(input.outDir, SESSION_REPORT_FILE_NAME)), knownBaselines(input.inputDir, readFile)), pullRequestTextOf(readOrNull(join(input.outDir, PULL_REQUEST_FILE_NAME))))
809
- : noted;
916
+ : uiPreview
917
+ ? { ...noted, uiPreview }
918
+ : noted;
810
919
  // The cloud lane's finish wrote `result.json`; it is written again when
811
- // this turn adds to it (the model's account, the plan turn's put-back).
812
- if (writes || notes.length > 0)
920
+ // this turn adds to it (the model's account, the plan turn's put-back, how
921
+ // it left its UI preview).
922
+ if (writes || notes.length > 0 || uiPreview)
813
923
  rewriteResult(input.outDir, attempt, writeFile);
814
924
  input.emit({ type: "pipeline_result", ...attempt });
815
925
  const ok = writes ? done.ok && attempt.stopReason === "clean" : done.ok;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yagni-app/code-staging",
3
- "version": "1.2.14-staging.2347.1",
3
+ "version": "1.2.14-staging.2349.1",
4
4
  "description": "YAGNI Code: a terminal coding agent that already knows your company. One YAGNI login routes the model and grounds the agent in your team's context.",
5
5
  "license": "SEE LICENSE IN LICENSE.md",
6
6
  "author": "YAGNI, Inc. <jack@yagni.app> (https://yagni.app)",
@@ -58,5 +58,5 @@
58
58
  "turndown": "^7.2.4",
59
59
  "typebox": "^1.3.15"
60
60
  },
61
- "yagniSourceSha": "a7e15e2da4a2f5b019b2b21df9208e4768fe5fb5"
61
+ "yagniSourceSha": "5dde9f4b58a461d01339e98d569f6f8739f0b0e8"
62
62
  }