@operad-io/dev 0.1.0 → 0.2.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.
package/src/announce.ts CHANGED
@@ -8,7 +8,10 @@
8
8
 
9
9
  import type { PluginStepResult, StepContext } from "@operad-io/engine";
10
10
  import { messageOf } from "@operad-io/engine";
11
- import type { TaskData } from "./model.ts";
11
+ import type { ReviewFinding, TaskData } from "./model.ts";
12
+ import { severityRank } from "./rework.ts";
13
+ import { edgeParamsOf, type EdgeParams } from "./step-edge.ts";
14
+ import { WAIT_ACTIONS, waitChoiceInfo, type WaitAction } from "./waits.ts";
12
15
  import {
13
16
  cancelledStep,
14
17
  claimAllows,
@@ -21,10 +24,7 @@ import {
21
24
  export const WAITING_EVENT = "dev.waiting";
22
25
 
23
26
  /** The edge a task took into the wait, as the flow names it in its params. */
24
- export interface AnnounceParams {
25
- readonly from: string;
26
- readonly result: string;
27
- }
27
+ export type AnnounceParams = EdgeParams;
28
28
 
29
29
  /** Why a task waits: the short reason for the key, and what to add to the line. */
30
30
  export interface WaitingReason {
@@ -34,16 +34,6 @@ export interface WaitingReason {
34
34
  readonly step?: string | undefined;
35
35
  }
36
36
 
37
- function paramsOf(params: unknown): AnnounceParams {
38
- const record =
39
- typeof params === "object" && params !== null && !Array.isArray(params)
40
- ? (params as Record<string, unknown>)
41
- : {};
42
- const from = typeof record.from === "string" ? record.from : "";
43
- const result = typeof record.result === "string" ? record.result : "";
44
- return { from, result };
45
- }
46
-
47
37
  /** The step a repair run says stopped, from the line the worktree step wrote. */
48
38
  function stoppedStep(task: TaskData, fallback: string): string {
49
39
  const lines = task.brief.replace(/\r\n/g, "\n").split("\n");
@@ -145,6 +135,104 @@ export function waitingReason(
145
135
  }
146
136
  }
147
137
 
138
+ /** The most serious finding of the review that is at or above the fix level. */
139
+ function highestFinding(
140
+ task: TaskData,
141
+ fixLevel: string | undefined,
142
+ ): ReviewFinding | undefined {
143
+ const findings = (task.attempt?.review?.findings ?? []).filter(
144
+ (finding) =>
145
+ finding.preExisting !== true &&
146
+ finding.pre_existing !== true &&
147
+ (fixLevel === undefined ||
148
+ severityRank(finding.severity) >= severityRank(fixLevel)),
149
+ );
150
+ return [...findings].sort(
151
+ (left, right) =>
152
+ severityRank(right.severity) - severityRank(left.severity) ||
153
+ left.file.localeCompare(right.file) ||
154
+ left.line - right.line,
155
+ )[0];
156
+ }
157
+
158
+ function findingSentence(finding: ReviewFinding): string {
159
+ const trigger = finding.trigger.trim();
160
+ return `${finding.severity} finding at ${finding.file}:${finding.line}: ${finding.summary}${
161
+ trigger.length === 0 ? "" : ` (${trigger})`
162
+ }`;
163
+ }
164
+
165
+ /**
166
+ * Why the task waits, in one or two plain sentences. It reads the task's own
167
+ * record for the review's highest finding, the failed check's command and the
168
+ * integration's reason; the choice list is never part of a prompt.
169
+ */
170
+ export function waitingPrompt(
171
+ found: WaitingReason,
172
+ task: TaskData,
173
+ fixLevel: string | undefined,
174
+ ): string {
175
+ const attempt = task.attempt;
176
+ const finding = highestFinding(task, fixLevel);
177
+ if (found.reason === "ready to merge") {
178
+ return "The checks and the review passed; the task is ready to merge.";
179
+ }
180
+ if (found.reason === "work did nothing") {
181
+ return "The worker changed nothing.";
182
+ }
183
+ if (found.reason === "work timed out") {
184
+ return `The worker timed out.${found.detail === undefined ? "" : ` ${found.detail}.`}`;
185
+ }
186
+ if (found.reason === "work failed") {
187
+ return `The worker failed${found.detail === undefined ? "" : `: ${found.detail}`}.`;
188
+ }
189
+ if (found.reason === "conflict") {
190
+ return "The worker's changes conflict with main.";
191
+ }
192
+ if (found.reason === "review timed out twice") {
193
+ return "review timed out twice";
194
+ }
195
+ if (found.reason === "review did not finish") {
196
+ return `review did not finish${found.detail === undefined ? "" : ` (${found.detail})`}`;
197
+ }
198
+ if (found.reason === "review failed twice in a row") {
199
+ return `The review failed twice in a row.${
200
+ finding === undefined ? "" : ` Highest finding: ${findingSentence(finding)}.`
201
+ }`;
202
+ }
203
+ if (found.reason === "review findings" || found.reason === "rework limit") {
204
+ const source = attempt?.rework?.source;
205
+ if (source === "review" || (source === undefined && attempt?.review !== undefined)) {
206
+ return finding === undefined
207
+ ? "The review failed."
208
+ : `The review failed: ${findingSentence(finding)}.`;
209
+ }
210
+ const command = attempt?.verify?.command;
211
+ return typeof command !== "string" || command.length === 0
212
+ ? `The check failed${attempt?.verify?.message === undefined ? "." : `: ${attempt.verify.message}.`}`
213
+ : `The check failed: \`${command}\`.`;
214
+ }
215
+ if (found.reason === "gate failed") {
216
+ const command = attempt?.verify?.command;
217
+ return typeof command !== "string" || command.length === 0
218
+ ? `The check failed${attempt?.verify?.message === undefined ? "." : `: ${attempt.verify.message}.`}`
219
+ : `The check failed: \`${command}\`.`;
220
+ }
221
+ if (found.reason === "merge refused") {
222
+ return `The merge failed${found.detail === undefined ? "" : `: ${found.detail}`}.`;
223
+ }
224
+ if (found.reason === "no verified commit") {
225
+ return "The task has no verified commit to merge.";
226
+ }
227
+ if (found.reason === "dependency cancelled") {
228
+ return "A task this one waits for was cancelled.";
229
+ }
230
+ if (found.reason.startsWith("run stopped during ")) {
231
+ return `The run stopped during ${found.reason.slice("run stopped during ".length)}.`;
232
+ }
233
+ return `${found.reason.charAt(0).toUpperCase()}${found.reason.slice(1)}.`;
234
+ }
235
+
148
236
  /** One line a person reads: the task, why it waits, and the detail if any. */
149
237
  export function waitingSummary(taskId: string, found: WaitingReason): string {
150
238
  const detail = found.detail === undefined ? "" : ` (${found.detail})`;
@@ -158,7 +246,7 @@ export async function runAnnounce(
158
246
  ): Promise<PluginStepResult> {
159
247
  const context = await taskContext(ctx);
160
248
  if (!claimAllows(ctx, context.task)) return cancelledStep();
161
- const edge = paramsOf(stepParams);
249
+ const edge = edgeParamsOf(stepParams);
162
250
  const path = context.task.attempt?.worktree;
163
251
  const conflicted =
164
252
  edge.from === "work" &&
@@ -213,6 +301,30 @@ export async function runAnnounce(
213
301
  await ctx.logger.write(`could not mark the task waiting: ${messageOf(error)}`);
214
302
  }
215
303
  }
304
+ // The wait that follows asks the person. It carries this task's own reason
305
+ // and choice information; asking with no grace hands the question over to
306
+ // the wait step at once, keeping one item and one wait id.
307
+ const choices = edge.choices;
308
+ if (choices !== undefined && choices.length > 0) {
309
+ const actions = choices.filter((choice): choice is WaitAction =>
310
+ (WAIT_ACTIONS as readonly string[]).includes(choice),
311
+ );
312
+ await ctx.ask({
313
+ prompt: waitingPrompt(
314
+ found,
315
+ context.task,
316
+ context.settings.reviewer.fixLevel,
317
+ ),
318
+ choices,
319
+ choiceInfo: waitChoiceInfo(
320
+ actions,
321
+ found.reason === "ready to merge",
322
+ ),
323
+ graceSecs: 0,
324
+ onTimeout: "waiting",
325
+ resume: null,
326
+ });
327
+ }
216
328
  // A stopped run keeps its result, so the wait can say which step stopped.
217
329
  return {
218
330
  result: found.step === undefined ? "done" : "interrupted",