@operad-io/dev 0.0.0-stage → 0.0.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.
Files changed (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +795 -2
  3. package/dev.schema.json +602 -0
  4. package/flows/chat-worktree.flow.ts +18 -0
  5. package/flows/dev.flow.ts +7 -0
  6. package/package.json +29 -4
  7. package/src/actions.ts +591 -0
  8. package/src/announce.ts +225 -0
  9. package/src/chat.ts +234 -0
  10. package/src/check.ts +80 -0
  11. package/src/commands.ts +417 -0
  12. package/src/editor.ts +55 -0
  13. package/src/flaky.ts +716 -0
  14. package/src/flow-compiler.ts +858 -0
  15. package/src/followups.ts +223 -0
  16. package/src/gate.ts +2897 -0
  17. package/src/git.ts +138 -0
  18. package/src/index.ts +226 -0
  19. package/src/integrate.ts +1201 -0
  20. package/src/integration-queue.ts +393 -0
  21. package/src/lock.ts +36 -0
  22. package/src/mainclone.ts +102 -0
  23. package/src/marks.ts +126 -0
  24. package/src/model.ts +1165 -0
  25. package/src/notcode.ts +266 -0
  26. package/src/place-order.ts +161 -0
  27. package/src/place-wait.ts +64 -0
  28. package/src/places.ts +368 -0
  29. package/src/prepare.ts +106 -0
  30. package/src/progress.ts +115 -0
  31. package/src/project-flows.ts +160 -0
  32. package/src/project-settings.ts +185 -0
  33. package/src/refusals.ts +336 -0
  34. package/src/rerun.ts +378 -0
  35. package/src/reruns.ts +62 -0
  36. package/src/retry.ts +212 -0
  37. package/src/rework.ts +366 -0
  38. package/src/schedule.ts +1052 -0
  39. package/src/service.ts +2160 -0
  40. package/src/settings.ts +1463 -0
  41. package/src/standing.ts +87 -0
  42. package/src/stdin.ts +105 -0
  43. package/src/steps.ts +1775 -0
  44. package/src/stops.ts +103 -0
  45. package/src/throughput.ts +347 -0
  46. package/src/timeouts.ts +60 -0
  47. package/src/views.ts +1167 -0
  48. package/src/waits.ts +105 -0
package/src/rework.ts ADDED
@@ -0,0 +1,366 @@
1
+ /**
2
+ * Rework rounds of a review: what the previous review found, which lines the
3
+ * increment holds, and which findings dev moves out of the failing set. All of
4
+ * it is decided from git and from recorded findings, never by the model.
5
+ */
6
+
7
+ /** A blocking finding of an earlier review, as the next review is told. */
8
+ export interface PreviousFinding {
9
+ readonly severity: string;
10
+ readonly file: string;
11
+ readonly line: number | null;
12
+ readonly summary: string;
13
+ }
14
+
15
+ /** What a review recorded for the task, kept for the review after it. */
16
+ export interface ReviewHistoryEntry {
17
+ /** The merge base of main and the commit, when the review ran. */
18
+ readonly base?: string | undefined;
19
+ readonly attempt: number;
20
+ readonly round: number;
21
+ readonly sha: string;
22
+ readonly blocking: readonly PreviousFinding[];
23
+ }
24
+
25
+ /** Lines of the new side of a diff, by file. */
26
+ export type LineMap = ReadonlyMap<string, ReadonlySet<number>>;
27
+
28
+ /** How near a finding must be to a changed line to stay blocking. */
29
+ export const CHANGE_WINDOW = 3;
30
+ /** How near a finding must be to a previous blocking finding to stay blocking. */
31
+ export const PREVIOUS_WINDOW = 10;
32
+
33
+ /**
34
+ * The review the next one follows: the newest review report of the task, in
35
+ * any attempt, for a commit other than `sha`. A review of the same commit is
36
+ * skipped, so a reverify of an unchanged head follows the one before it.
37
+ */
38
+ export function previousReview(
39
+ history: readonly ReviewHistoryEntry[] | undefined,
40
+ sha: string,
41
+ ): ReviewHistoryEntry | undefined {
42
+ const other = (history ?? []).filter((entry) => entry.sha !== sha);
43
+ return other[other.length - 1];
44
+ }
45
+
46
+ /**
47
+ * The lines a `-U0` diff text changed, by new path: the lines it added and,
48
+ * for a pure deletion, the two lines either side of the cut.
49
+ */
50
+ export function addedLinesOf(diff: string): LineMap {
51
+ const map = new Map<string, Set<number>>();
52
+ let current: Set<number> | undefined;
53
+ for (const line of diff.split("\n")) {
54
+ if (line.startsWith("+++ ")) {
55
+ const raw = line.slice(4).replace(/\t.*$/, "");
56
+ if (raw === "/dev/null") {
57
+ current = undefined;
58
+ continue;
59
+ }
60
+ const path = raw.startsWith("b/") ? raw.slice(2) : raw;
61
+ current = map.get(path) ?? new Set<number>();
62
+ map.set(path, current);
63
+ continue;
64
+ }
65
+ const hunk = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/.exec(line);
66
+ if (hunk !== null && current !== undefined) {
67
+ const start = Number(hunk[1]);
68
+ const count = hunk[2] === undefined ? 1 : Number(hunk[2]);
69
+ for (let at = start; at < start + count; at += 1) current.add(at);
70
+ // A pure deletion changed the place between two lines: both count.
71
+ if (count === 0) {
72
+ if (start > 0) current.add(start);
73
+ current.add(start + 1);
74
+ }
75
+ }
76
+ }
77
+ return map;
78
+ }
79
+
80
+ /** The patch of each file in a `git diff` text, by new path. */
81
+ export function filePatches(diff: string): ReadonlyMap<string, string> {
82
+ const map = new Map<string, string>();
83
+ const blocks = diff.split(/^(?=diff --git )/m);
84
+ for (const block of blocks) {
85
+ if (!block.startsWith("diff --git ")) continue;
86
+ let path: string | undefined;
87
+ for (const line of block.split("\n")) {
88
+ if (line.startsWith("@@ ")) break;
89
+ if (line.startsWith("+++ ") && line !== "+++ /dev/null") {
90
+ const raw = line.slice(4).replace(/\t.*$/, "");
91
+ path = raw.startsWith("b/") ? raw.slice(2) : raw;
92
+ } else if (line.startsWith("--- a/") && path === undefined) {
93
+ path = line.slice(6).replace(/\t.*$/, "");
94
+ }
95
+ }
96
+ if (path !== undefined) map.set(path, block);
97
+ }
98
+ return map;
99
+ }
100
+
101
+ interface SplitPatch {
102
+ readonly header: string;
103
+ readonly hunks: readonly string[];
104
+ }
105
+
106
+ function splitPatch(patch: string): SplitPatch {
107
+ const lines = patch.split("\n");
108
+ if (lines[lines.length - 1] === "") lines.pop();
109
+ const first = lines.findIndex((line) => line.startsWith("@@ "));
110
+ if (first < 0) return { header: "", hunks: [] };
111
+ const hunks: string[] = [];
112
+ let start = first;
113
+ for (let at = first + 1; at <= lines.length; at += 1) {
114
+ if (at === lines.length || (lines[at] ?? "").startsWith("@@ ")) {
115
+ hunks.push(`${lines.slice(start, at).join("\n")}\n`);
116
+ start = at;
117
+ }
118
+ }
119
+ return { header: `${lines.slice(0, first).join("\n")}\n`, hunks };
120
+ }
121
+
122
+ /** The changed lines (`+` and `-`) of a hunk, without line numbers or context. */
123
+ function changedOf(hunk: string): string[] {
124
+ return hunk
125
+ .split("\n")
126
+ .slice(1)
127
+ .filter((line) => line.startsWith("+") || line.startsWith("-"));
128
+ }
129
+
130
+ /** A hunk's identity: its changed lines, in order. */
131
+ function keyOf(hunk: string): string {
132
+ return changedOf(hunk).join("\n");
133
+ }
134
+
135
+ /** The hunks with no hunk of the same changed lines on the other side. */
136
+ function unmatched(
137
+ hunks: readonly string[],
138
+ other: readonly string[],
139
+ ): readonly string[] {
140
+ const left = new Map<string, number>();
141
+ for (const hunk of other) {
142
+ const key = keyOf(hunk);
143
+ left.set(key, (left.get(key) ?? 0) + 1);
144
+ }
145
+ return hunks.filter((hunk) => {
146
+ const key = keyOf(hunk);
147
+ const have = left.get(key) ?? 0;
148
+ if (have > 0) {
149
+ left.set(key, have - 1);
150
+ return false;
151
+ }
152
+ return true;
153
+ });
154
+ }
155
+
156
+ /** What changed in the task's own diff between two reviews. */
157
+ export interface Increment {
158
+ /** Hunks of the current own diff with a changed line the previous one lacks. */
159
+ readonly added: string;
160
+ /** Hunks of the previous own diff with a changed line the current one lacks. */
161
+ readonly removed: string;
162
+ }
163
+
164
+ /**
165
+ * The increment, as `git range-diff` pairs commits: both sides are the task's
166
+ * own diff (merge base to head), so a line that only came with a merge of main
167
+ * is on neither side. A hunk counts as new when no hunk of the previous diff of the same
168
+ * file has exactly its `-`/`+` lines (line numbers and context are ignored); a hunk of
169
+ * the previous diff that is gone is reported as removed with its old content.
170
+ */
171
+ export function incrementOf(
172
+ previous: ReadonlyMap<string, string>,
173
+ current: ReadonlyMap<string, string>,
174
+ ): Increment {
175
+ let added = "";
176
+ let removed = "";
177
+ for (const [path, patch] of current) {
178
+ const now = splitPatch(patch);
179
+ const before = splitPatch(previous.get(path) ?? "");
180
+ const fresh = unmatched(now.hunks, before.hunks);
181
+ if (fresh.length > 0) added += now.header + fresh.join("");
182
+ }
183
+ for (const [path, patch] of previous) {
184
+ const before = splitPatch(patch);
185
+ const now = splitPatch(current.get(path) ?? "");
186
+ const gone = unmatched(before.hunks, now.hunks);
187
+ if (gone.length > 0) removed += before.header + gone.join("");
188
+ }
189
+ return { added, removed };
190
+ }
191
+
192
+ /** A finding as dev judges it: only the parts that decide its place. */
193
+ export interface Placed {
194
+ readonly severity: string;
195
+ readonly file: string | undefined;
196
+ readonly line: number | undefined;
197
+ }
198
+
199
+ function near(lines: ReadonlySet<number> | undefined, line: number, window: number): boolean {
200
+ if (lines === undefined) return false;
201
+ for (const changed of lines) {
202
+ if (Math.abs(changed - line) <= window) return true;
203
+ }
204
+ return false;
205
+ }
206
+
207
+ /**
208
+ * True when the finding is moved out of the failing set. It stays when it is
209
+ * within 3 lines of a line the task's own change added in that file, within 10
210
+ * lines of a previous blocking finding in that file, critical, or without a
211
+ * file or a line.
212
+ */
213
+ export function movesAsPreExisting(
214
+ finding: Placed,
215
+ own: LineMap,
216
+ previous: readonly PreviousFinding[],
217
+ normalize: (path: string) => string,
218
+ ): boolean {
219
+ if (finding.severity === "critical") return false;
220
+ if (
221
+ finding.file === undefined ||
222
+ finding.file.length === 0 ||
223
+ finding.line === undefined ||
224
+ !Number.isFinite(finding.line)
225
+ ) {
226
+ return false;
227
+ }
228
+ const file = normalize(finding.file);
229
+ if (near(own.get(file), finding.line, CHANGE_WINDOW)) return false;
230
+ for (const old of previous) {
231
+ if (
232
+ old.line !== null &&
233
+ normalize(old.file) === file &&
234
+ Math.abs(old.line - finding.line) <= PREVIOUS_WINDOW
235
+ ) {
236
+ return false;
237
+ }
238
+ }
239
+ return true;
240
+ }
241
+
242
+ /** Which review is being decided: the round fixes which pre-existing rule applies. */
243
+ export type ReviewKind = "first" | "reverify" | "rework";
244
+
245
+ /**
246
+ * Whether a finding moves as pre-existing in a review of this kind.
247
+ * A first review moves nothing: a finding in a file the change does not touch
248
+ * can still be a defect the change causes (T-0315; before it, the per-file rule
249
+ * let majors in untouched files pass a review). A reverify of the same commit
250
+ * moves nothing. Only a rework round applies the line rules of
251
+ * `movesAsPreExisting`.
252
+ */
253
+ export function movesIn(
254
+ kind: ReviewKind,
255
+ finding: Placed,
256
+ own: LineMap,
257
+ previous: readonly PreviousFinding[],
258
+ normalize: (path: string) => string,
259
+ ): boolean {
260
+ return kind === "rework"
261
+ ? movesAsPreExisting(finding, own, previous, normalize)
262
+ : false;
263
+ }
264
+
265
+ /** How many previous findings have no blocking finding of this review near them. */
266
+ export function fixedCount(
267
+ previous: readonly PreviousFinding[],
268
+ blocking: readonly Placed[],
269
+ normalize: (path: string) => string,
270
+ ): number {
271
+ return previous.filter(
272
+ (old) =>
273
+ !blocking.some(
274
+ (now) =>
275
+ now.file !== undefined &&
276
+ now.line !== undefined &&
277
+ old.line !== null &&
278
+ normalize(now.file) === normalize(old.file) &&
279
+ Math.abs(now.line - old.line) <= PREVIOUS_WINDOW,
280
+ ),
281
+ ).length;
282
+ }
283
+
284
+ /** `[level] file:line summary` for one previous finding. */
285
+ export function previousLine(finding: PreviousFinding): string {
286
+ const place =
287
+ finding.line === null ? finding.file : `${finding.file}:${finding.line}`;
288
+ return `- [${finding.severity}] ${place} ${finding.summary}`;
289
+ }
290
+
291
+ /** Which block of reviewer settings a value came from. */
292
+ export type ReviewSettingSource =
293
+ | "first"
294
+ | "firstArgs"
295
+ | "fallback"
296
+ | "default";
297
+
298
+ /** The settings one review round ran with, and where each came from. */
299
+ export interface ReviewSettingsUsed {
300
+ /** `first` for the task's first review; `rework` for every later one. */
301
+ readonly round: "first" | "rework";
302
+ readonly use: string;
303
+ readonly args: readonly string[];
304
+ readonly timeoutSecs: number;
305
+ readonly stallSecs?: number;
306
+ readonly sources: {
307
+ readonly use: ReviewSettingSource;
308
+ readonly args: ReviewSettingSource;
309
+ readonly timeoutSecs: ReviewSettingSource;
310
+ readonly stallSecs: ReviewSettingSource;
311
+ };
312
+ }
313
+
314
+ /** The reviewer values the round's settings are chosen from. */
315
+ export interface ReviewerValues {
316
+ readonly use: string;
317
+ readonly args: readonly string[];
318
+ readonly firstArgs?: readonly string[] | undefined;
319
+ readonly first?:
320
+ | {
321
+ readonly use?: string | undefined;
322
+ readonly args?: readonly string[] | undefined;
323
+ readonly timeoutSecs?: number | undefined;
324
+ readonly stallSecs?: number | undefined;
325
+ }
326
+ | undefined;
327
+ readonly timeoutSecs: number;
328
+ readonly stallSecs?: number | undefined;
329
+ }
330
+
331
+ /**
332
+ * The settings of a review round. Only a first review reads the `first`
333
+ * block, value by value; a rework round and a review of the same commit read
334
+ * the default values. `firstArgs` (an older setting) still gives a first
335
+ * review of an attempt its arguments when `first.args` is not set.
336
+ */
337
+ export function reviewSettingsOf(
338
+ kind: ReviewKind,
339
+ reviewer: ReviewerValues,
340
+ firstOfAttempt: boolean,
341
+ ): ReviewSettingsUsed {
342
+ const first = kind === "first" ? reviewer.first : undefined;
343
+ // `firstArgs` is older than rounds: it belongs to the first review of every
344
+ // attempt, whatever the round's kind.
345
+ const legacy = firstOfAttempt ? reviewer.firstArgs : undefined;
346
+ const args = first?.args ?? legacy;
347
+ const stall = first?.stallSecs ?? reviewer.stallSecs;
348
+ return {
349
+ round: kind === "first" ? "first" : "rework",
350
+ use: first?.use ?? reviewer.use,
351
+ args: args ?? reviewer.args,
352
+ timeoutSecs: first?.timeoutSecs ?? reviewer.timeoutSecs,
353
+ ...(stall === undefined ? {} : { stallSecs: stall }),
354
+ sources: {
355
+ use: first?.use === undefined ? "default" : "first",
356
+ args:
357
+ first?.args !== undefined
358
+ ? "first"
359
+ : legacy !== undefined
360
+ ? "firstArgs"
361
+ : "default",
362
+ timeoutSecs: first?.timeoutSecs === undefined ? "default" : "first",
363
+ stallSecs: first?.stallSecs === undefined ? "default" : "first",
364
+ },
365
+ };
366
+ }