@operad-io/dev 0.1.1 → 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.
@@ -7,8 +7,12 @@
7
7
  * merges.
8
8
  */
9
9
 
10
+ import { messageOf } from "@operad-io/engine";
10
11
  import type { ProjectSettingsDeclaration } from "@operad-io/engine";
12
+ import { flowClash, listRepositoryFlows } from "./project-flows.ts";
13
+ import { defaultDevSettings, readDevSettings, settingsProblem, settingsValueProblem, useNewStageNames } from "./settings.ts";
11
14
  import { listRepositories } from "./service.ts";
15
+ import type { JsonValue } from "@operad-io/engine";
12
16
 
13
17
  export const DEV_PROJECT_SETTINGS: ProjectSettingsDeclaration = {
14
18
  repositories: async ({ store }) =>
@@ -17,6 +21,49 @@ export const DEV_PROJECT_SETTINGS: ProjectSettingsDeclaration = {
17
21
  {
18
22
  name: "dev",
19
23
  path: "dev.json",
24
+ // dev's own check of the file as it would be written: its schema and rules
25
+ // (ADR 0020 D4), then the name clash with a TypeScript flow (D6). It runs
26
+ // for a flow change only, so an invalid flow already in the file does not
27
+ // block an unrelated setting.
28
+ check: ({ repository, value, name }) => {
29
+ if (name !== "dev.flows" && !name.startsWith("dev.flows.")) return undefined;
30
+ const problem =
31
+ settingsValueProblem(repository.path, value) ??
32
+ flowClash(repository.path, value.flows);
33
+ if (problem === undefined) useNewStageNames(value);
34
+ return problem;
35
+ },
36
+ derived: async ({ repository }) => {
37
+ const problem = settingsProblem(repository.path);
38
+ let settings;
39
+ try {
40
+ settings = await readDevSettings(repository.path);
41
+ } catch (error) {
42
+ // The file was never valid, so there are no dev.json flows to list;
43
+ // the read-only list stays and says why, beside the TypeScript flows.
44
+ const reason =
45
+ problem ?? (messageOf(error));
46
+ const list = await listRepositoryFlows(
47
+ repository.path,
48
+ repository.name,
49
+ defaultDevSettings(),
50
+ );
51
+ return {
52
+ flowList: [
53
+ { name: "dev.json", source: "dev.json", refused: reason },
54
+ ...list,
55
+ ] as unknown as JsonValue,
56
+ };
57
+ }
58
+ const list = await listRepositoryFlows(repository.path, repository.name, settings);
59
+ return {
60
+ flowList: (
61
+ problem === undefined
62
+ ? list
63
+ : [{ name: "dev.json", source: "dev.json", refused: problem }, ...list]
64
+ ) as unknown as JsonValue,
65
+ };
66
+ },
20
67
  schema: {
21
68
  type: "object",
22
69
  $defs: {
@@ -49,6 +96,7 @@ export const DEV_PROJECT_SETTINGS: ProjectSettingsDeclaration = {
49
96
  properties: {
50
97
  concurrency: {
51
98
  type: "integer",
99
+ title: "Tasks at once",
52
100
  minimum: 1,
53
101
  editable: true,
54
102
  description:
@@ -56,6 +104,7 @@ export const DEV_PROJECT_SETTINGS: ProjectSettingsDeclaration = {
56
104
  },
57
105
  autoDefault: {
58
106
  type: "string",
107
+ title: "Auto for new plans",
59
108
  enum: ["off", "run", "merge"],
60
109
  default: "off",
61
110
  editable: true,
@@ -63,11 +112,32 @@ export const DEV_PROJECT_SETTINGS: ProjectSettingsDeclaration = {
63
112
  description:
64
113
  "The auto level a new plan starts at: off waits for you to start tasks, run starts them by itself, merge also merges a task whose review passed. Plans already made keep their level.",
65
114
  },
115
+ flows: {
116
+ type: "object",
117
+ title: "Custom flows",
118
+ editable: true,
119
+ description:
120
+ "The repository's own flows, by name. A flow is { \"stages\": [...] }, each stage one of code, test, review or human (the older names work and check still load); dev checks the flow when it is saved and refuses one that breaks its rules. A name may also be changed on its own (dev.flows.<name>); null removes it.",
121
+ additionalProperties: {
122
+ type: "object",
123
+ properties: {
124
+ stages: { type: "array", items: { type: "object" } },
125
+ },
126
+ },
127
+ },
128
+ flowList: {
129
+ type: "array",
130
+ title: "Flows",
131
+ items: { type: "object" },
132
+ description:
133
+ "Every flow of the repository, read only: its name, where it comes from (dev.json, typescript or built-in), its stages for a dev.json flow or the built-in default, the steps dev reads for a TypeScript flow, or why dev refuses the file.",
134
+ },
66
135
  gate: {
67
136
  type: "object",
68
137
  properties: {
69
138
  commands: {
70
139
  type: "array",
140
+ title: "Check commands",
71
141
  items: { $ref: "#/$defs/checkCommand" },
72
142
  default: [],
73
143
  editable: true,
@@ -76,6 +146,7 @@ export const DEV_PROJECT_SETTINGS: ProjectSettingsDeclaration = {
76
146
  },
77
147
  commandTimeoutSecs: {
78
148
  type: "number",
149
+ title: "Check timeout",
79
150
  minimum: 1,
80
151
  default: 1800,
81
152
  editable: true,
@@ -84,6 +155,7 @@ export const DEV_PROJECT_SETTINGS: ProjectSettingsDeclaration = {
84
155
  },
85
156
  stallSecs: {
86
157
  type: "number",
158
+ title: "Check stall limit",
87
159
  minimum: 1,
88
160
  editable: true,
89
161
  description:
@@ -96,6 +168,7 @@ export const DEV_PROJECT_SETTINGS: ProjectSettingsDeclaration = {
96
168
  properties: {
97
169
  use: {
98
170
  type: "string",
171
+ title: "Reviewer",
99
172
  minLength: 1,
100
173
  default: "radian/agent",
101
174
  editable: true,
@@ -104,6 +177,7 @@ export const DEV_PROJECT_SETTINGS: ProjectSettingsDeclaration = {
104
177
  },
105
178
  args: {
106
179
  type: "array",
180
+ title: "Reviewer arguments",
107
181
  items: { type: "string" },
108
182
  default: [],
109
183
  editable: true,
@@ -112,6 +186,7 @@ export const DEV_PROJECT_SETTINGS: ProjectSettingsDeclaration = {
112
186
  },
113
187
  timeoutSecs: {
114
188
  type: "number",
189
+ title: "Review timeout",
115
190
  minimum: 1,
116
191
  default: 600,
117
192
  editable: true,
@@ -120,6 +195,7 @@ export const DEV_PROJECT_SETTINGS: ProjectSettingsDeclaration = {
120
195
  },
121
196
  fixLevel: {
122
197
  type: "string",
198
+ title: "Reviewer rework threshold",
123
199
  enum: ["critical", "major", "minor"],
124
200
  default: "major",
125
201
  editable: true,
@@ -146,6 +222,7 @@ export const DEV_PROJECT_SETTINGS: ProjectSettingsDeclaration = {
146
222
  properties: {
147
223
  critical: {
148
224
  type: "string",
225
+ title: "Critical level",
149
226
  editable: true,
150
227
  confirm: true,
151
228
  description:
@@ -153,6 +230,7 @@ export const DEV_PROJECT_SETTINGS: ProjectSettingsDeclaration = {
153
230
  },
154
231
  major: {
155
232
  type: "string",
233
+ title: "Major level",
156
234
  editable: true,
157
235
  confirm: true,
158
236
  description:
@@ -160,6 +238,7 @@ export const DEV_PROJECT_SETTINGS: ProjectSettingsDeclaration = {
160
238
  },
161
239
  minor: {
162
240
  type: "string",
241
+ title: "Minor level",
163
242
  editable: true,
164
243
  confirm: true,
165
244
  description:
@@ -169,6 +248,7 @@ export const DEV_PROJECT_SETTINGS: ProjectSettingsDeclaration = {
169
248
  },
170
249
  rules: {
171
250
  type: "array",
251
+ title: "Review rules",
172
252
  items: { type: "string" },
173
253
  editable: true,
174
254
  confirm: true,
@@ -177,6 +257,7 @@ export const DEV_PROJECT_SETTINGS: ProjectSettingsDeclaration = {
177
257
  },
178
258
  fixLevel: {
179
259
  type: "string",
260
+ title: "Rework threshold",
180
261
  enum: ["critical", "major", "minor"],
181
262
  editable: true,
182
263
  confirm: true,
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Repositories that an approved Setup Dev asks `dev` to register (ADR 0030
3
+ * D15). The `bot` plugin leaves one file per request in `<home>/dev-register/`
4
+ * because `dev` may not be loaded when the proposal is approved; `dev` reads
5
+ * the directory on every tick, registers each repository as `operad dev repo
6
+ * add` does and removes the file. A request that cannot be registered stays,
7
+ * with the reason written into it, and is shown as a pending item until it is
8
+ * fixed or removed.
9
+ */
10
+
11
+ import { existsSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
12
+ import { join } from "node:path";
13
+ import { isRecord, messageOf, type ObjectStore, type StandingItem } from "@operad-io/engine";
14
+ import { addRepository } from "./service.ts";
15
+
16
+ /** The directory under the home that holds the requests. */
17
+ export function registerRequestsDirectory(home: string): string {
18
+ return join(home, "dev-register");
19
+ }
20
+
21
+ interface Request {
22
+ readonly file: string;
23
+ readonly path: string;
24
+ readonly error: string | undefined;
25
+ }
26
+
27
+ function readRequests(home: string): Request[] {
28
+ const directory = registerRequestsDirectory(home);
29
+ if (!existsSync(directory)) return [];
30
+ const found: Request[] = [];
31
+ let names: string[];
32
+ try {
33
+ names = readdirSync(directory).sort();
34
+ } catch {
35
+ // An unreadable directory holds no requests; it must not stop the tick.
36
+ return found;
37
+ }
38
+ for (const name of names) {
39
+ if (!name.endsWith(".json")) continue;
40
+ const file = join(directory, name);
41
+ try {
42
+ const parsed: unknown = JSON.parse(readFileSync(file, "utf8"));
43
+ if (!isRecord(parsed) || typeof parsed.path !== "string") continue;
44
+ found.push({
45
+ file,
46
+ path: parsed.path,
47
+ error: typeof parsed.error === "string" ? parsed.error : undefined,
48
+ });
49
+ } catch {
50
+ // A file that is not written yet or is not a request is left alone.
51
+ }
52
+ }
53
+ return found;
54
+ }
55
+
56
+ /** Registers every requested repository; a failure stays in its file. */
57
+ export async function registerRequested(ctx: {
58
+ readonly home: string;
59
+ readonly cwd: string;
60
+ readonly store: ObjectStore;
61
+ }): Promise<void> {
62
+ for (const request of readRequests(ctx.home)) {
63
+ try {
64
+ await addRepository(ctx.store, { path: request.path, cwd: ctx.cwd });
65
+ rmSync(request.file, { force: true });
66
+ } catch (error) {
67
+ const message = messageOf(error);
68
+ if (message.includes("is already registered")) {
69
+ rmSync(request.file, { force: true });
70
+ continue;
71
+ }
72
+ if (message !== request.error) {
73
+ writeFileSync(request.file, `${JSON.stringify({ path: request.path, error: message })}\n`);
74
+ }
75
+ }
76
+ }
77
+ }
78
+
79
+ /** The pending items of requests that could not be registered. */
80
+ export function registerStanding(home: string): readonly StandingItem[] {
81
+ return readRequests(home)
82
+ .filter((request) => request.error !== undefined)
83
+ .map((request) => ({
84
+ id: `register:${request.path}`,
85
+ prompt: `the repository ${request.path} could not be registered: ${request.error ?? ""}; fix it and it is registered on its own, or remove ${request.file}`,
86
+ }));
87
+ }
@@ -0,0 +1,4 @@
1
+ /** A stable key for a repository path, for files kept under the operad home. */
2
+ export function repositoryPathKey(repositoryPath: string): string {
3
+ return new Bun.CryptoHasher("sha1").update(repositoryPath).digest("hex");
4
+ }
package/src/rerun.ts CHANGED
@@ -36,6 +36,8 @@ export interface CheckRun {
36
36
  readonly failedBy?: ReadonlyMap<string, FailedRerun> | undefined;
37
37
  /** Set when the result is the failed test's own rerun, run alone, failing again. */
38
38
  readonly aloneRerun?: boolean | undefined;
39
+ /** Where the run's first command began writing in the log, when the step said. */
40
+ readonly logStart?: number | undefined;
39
41
  }
40
42
 
41
43
  /** The line drawn between the two runs in the check log. */
@@ -75,10 +77,10 @@ function logOf(outcome: PluginStepResult | undefined): string | undefined {
75
77
  return typeof log === "string" ? log : undefined;
76
78
  }
77
79
 
78
- /** Where the step's own output began in the shared log; 0 when it does not say. */
79
- function logStartOf(outcome: PluginStepResult | undefined): number {
80
+ /** Where the step's own output began in the shared log; undefined when it does not say. */
81
+ function logStartOf(outcome: PluginStepResult | undefined): number | undefined {
80
82
  const start = dataOf(outcome).logStart;
81
- return typeof start === "number" ? start : 0;
83
+ return typeof start === "number" ? start : undefined;
82
84
  }
83
85
 
84
86
  /** How many bytes the log holds now; a log that cannot be read holds none. */
@@ -119,6 +121,8 @@ export async function runCheckWithRerun(
119
121
  ): Promise<CheckRun> {
120
122
  const first = await once(runStep, params, options, places);
121
123
  const firstData = dataOf(first.outcome);
124
+ const firstStart = logStartOf(first.outcome);
125
+ const started = firstStart === undefined ? {} : { logStart: firstStart };
122
126
  const failed = first.hostError !== undefined || first.outcome?.result === "fail";
123
127
  // Only a plain failure is rerun: not a cancel, not a hang.
124
128
  if (
@@ -128,7 +132,7 @@ export async function runCheckWithRerun(
128
132
  firstData.reason === "stall" ||
129
133
  first.hostError !== undefined
130
134
  ) {
131
- return { ...first, rerun: false, note: undefined, passedOnRerun: [] };
135
+ return { ...first, ...started, rerun: false, note: undefined, passedOnRerun: [] };
132
136
  }
133
137
  const label =
134
138
  typeof firstData.lineNumber === "number"
@@ -147,6 +151,7 @@ export async function runCheckWithRerun(
147
151
  ) {
148
152
  return {
149
153
  ...last,
154
+ ...started,
150
155
  rerun: true,
151
156
  passedOnRerun: [],
152
157
  note: `${label} failed, passed on rerun${attempts > 1 ? ` ${attempts}` : ""}`,
@@ -162,7 +167,7 @@ export async function runCheckWithRerun(
162
167
  break;
163
168
  }
164
169
  }
165
- return { ...last, rerun: true, note: undefined, passedOnRerun: [] };
170
+ return { ...last, ...started, rerun: true, note: undefined, passedOnRerun: [] };
166
171
  }
167
172
 
168
173
  /* ---------------------------------------------------------------------------
@@ -248,6 +253,13 @@ function describePass(names: readonly string[]): string | undefined {
248
253
  : `passed on rerun: ${names.join("; ")}`;
249
254
  }
250
255
 
256
+ export interface RunCheckBase {
257
+ readonly cwd: string;
258
+ readonly timeoutSecs?: number | undefined;
259
+ /** The environment added to the check line; the shell inherits the rest. */
260
+ readonly env?: Readonly<Record<string, string>> | undefined;
261
+ }
262
+
251
263
  /**
252
264
  * Runs a list of check commands. A list of plain strings is one `shell/check`
253
265
  * with the whole-list rerun. A command with `failed` runs alone; when it exits
@@ -255,7 +267,7 @@ function describePass(names: readonly string[]): string | undefined {
255
267
  */
256
268
  export async function runCheckCommands(
257
269
  runStep: NonNullable<StepContext["runStep"]>,
258
- base: { readonly cwd: string; readonly timeoutSecs?: number },
270
+ base: RunCheckBase,
259
271
  commands: readonly CheckCommand[],
260
272
  options: StepRunOptions,
261
273
  rerun: number,
@@ -281,6 +293,7 @@ export async function runCheckCommands(
281
293
  const passed: string[] = [];
282
294
  const failedBy = new Map<string, FailedRerun>();
283
295
  let reran = false;
296
+ let firstStart: number | undefined;
284
297
  let last: Pick<CheckRun, "outcome" | "hostError"> = {
285
298
  outcome: { result: "pass", data: { lines: 0, tail: [] } },
286
299
  hostError: undefined,
@@ -290,6 +303,7 @@ export async function runCheckCommands(
290
303
  aloneRerun = false,
291
304
  ): CheckRun => ({
292
305
  ...result,
306
+ ...(firstStart === undefined ? {} : { logStart: firstStart }),
293
307
  ...(aloneRerun ? { aloneRerun: true } : {}),
294
308
  rerun: reran,
295
309
  passedOnRerun: passed,
@@ -319,6 +333,7 @@ export async function runCheckCommands(
319
333
  index,
320
334
  );
321
335
  reran = reran || group.rerun;
336
+ firstStart ??= group.logStart;
322
337
  if (group.note !== undefined) notes.push(group.note);
323
338
  const lineNumber = dataOf(group.outcome).lineNumber;
324
339
  const outcome =
@@ -335,6 +350,7 @@ export async function runCheckCommands(
335
350
 
336
351
  const lineNumber = index + 1;
337
352
  const first = await once(runStep, paramsFor([runOf(command)]), options, places);
353
+ firstStart ??= logStartOf(first.outcome);
338
354
  const firstData = dataOf(first.outcome);
339
355
  const numbered = {
340
356
  outcome: withLineNumber(first.outcome, lineNumber),
@@ -353,7 +369,7 @@ export async function runCheckCommands(
353
369
  return finish(numbered);
354
370
  }
355
371
  const log = logOf(first.outcome);
356
- const names = failedNames(failed, readSince(log, logStartOf(first.outcome)));
372
+ const names = failedNames(failed, readSince(log, logStartOf(first.outcome) ?? 0));
357
373
  if (names.length === 0 || names.length > failed.max) return finish(numbered);
358
374
 
359
375
  reran = true;
package/src/retry.ts CHANGED
@@ -10,6 +10,7 @@ import type { PluginStepResult, StepContext } from "@operad-io/engine";
10
10
  import { reviewTestHooks, waitFor } from "./gate.ts";
11
11
  import type { RetryRecord, TaskData } from "./model.ts";
12
12
  import { DEFAULT_DEV_SETTINGS, readDevSettings } from "./settings.ts";
13
+ import { edgeParamsOf } from "./step-edge.ts";
13
14
  import { classifyStop } from "./stops.ts";
14
15
  import {
15
16
  cancelledStep,
@@ -28,17 +29,6 @@ export interface RetryParams {
28
29
  readonly result: string;
29
30
  }
30
31
 
31
- function paramsOf(params: unknown): RetryParams {
32
- const record =
33
- typeof params === "object" && params !== null && !Array.isArray(params)
34
- ? (params as Record<string, unknown>)
35
- : {};
36
- return {
37
- from: typeof record.from === "string" ? record.from : "",
38
- result: typeof record.result === "string" ? record.result : "",
39
- };
40
- }
41
-
42
32
  function attemptNumber(task: TaskData): number {
43
33
  return task.attempt?.number ?? task.attempts;
44
34
  }
@@ -63,7 +53,7 @@ export async function runRetry(
63
53
  const context = await looseTaskContext(ctx);
64
54
  const task = context.task;
65
55
  if (!claimAllows(ctx, task)) return cancelledStep();
66
- const edge = paramsOf(stepParams);
56
+ const edge = edgeParamsOf(stepParams);
67
57
  const used = usedRetry(task);
68
58
 
69
59
  const write = async (
package/src/rework.ts CHANGED
@@ -30,6 +30,14 @@ export const CHANGE_WINDOW = 3;
30
30
  /** How near a finding must be to a previous blocking finding to stay blocking. */
31
31
  export const PREVIOUS_WINDOW = 10;
32
32
 
33
+ /** How serious a finding or a fix level is: critical, major, minor, then none. */
34
+ export function severityRank(severity: string): number {
35
+ if (severity === "critical") return 3;
36
+ if (severity === "major") return 2;
37
+ if (severity === "minor") return 1;
38
+ return 0;
39
+ }
40
+
33
41
  /**
34
42
  * The review the next one follows: the newest review report of the task, in
35
43
  * any attempt, for a commit other than `sha`. A review of the same commit is