@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
@@ -0,0 +1,223 @@
1
+ /**
2
+ * The plan's review follow-up. Findings that did not fail a review (below the
3
+ * fix level, or moved as pre-existing) are recorded on the task per round.
4
+ * A task that merges with such findings is marked; when the plan has no open
5
+ * task, one task is proposed that collects the findings of every marked task.
6
+ *
7
+ * Collection runs under the repository lock. The proposed task names the tasks
8
+ * it collects, and the marks are written after it, so a crash between the two
9
+ * is finished by the next call, which marks those tasks and proposes nothing.
10
+ */
11
+
12
+ import type { ObjectStore, StoredObject } from "@operad-io/engine";
13
+ import {
14
+ bareId,
15
+ changeClock,
16
+ listAll,
17
+ parseId,
18
+ planData,
19
+ taskData,
20
+ type FollowupMark,
21
+ type ReviewNote,
22
+ type TaskData,
23
+ } from "./model.ts";
24
+ import { addTaskLocked, getPlan, withRepoLock } from "./service.ts";
25
+ import type { DevSettings } from "./settings.ts";
26
+
27
+ /** Test-only: runs after the follow-up was proposed and before its tasks are marked. */
28
+ export const followupTestHooks: {
29
+ afterPropose?: (() => Promise<void> | void) | undefined;
30
+ } = {};
31
+
32
+ const OPEN_PHASES: readonly string[] = [
33
+ "ready",
34
+ "running",
35
+ "review",
36
+ "waiting",
37
+ "paused",
38
+ ];
39
+ const SLUG_MAX = 48;
40
+ const SUFFIX = "-followups";
41
+
42
+ /** The mark a task gets when it merges; empty when it has nothing to collect. */
43
+ export function followupMark(
44
+ task: TaskData,
45
+ settings: DevSettings,
46
+ ): { readonly followups?: FollowupMark } {
47
+ if (!settings.reviewer.followups || task.followupOf !== undefined) return {};
48
+ if (task.followups !== undefined) return {};
49
+ if (!(task.reviewNotes ?? []).some((round) => round.findings.length > 0)) {
50
+ return {};
51
+ }
52
+ return { followups: { mergedAt: changeClock.now(), state: "collect" } };
53
+ }
54
+
55
+ interface Line {
56
+ readonly note: ReviewNote;
57
+ readonly task: string;
58
+ readonly round: number;
59
+ }
60
+
61
+ /** The text without a leading tag that repeats the finding's own level, whitespace collapsed. */
62
+ function textOf(note: ReviewNote): string {
63
+ const text = note.summary.replace(/\s+/g, " ").trim();
64
+ const tag = /^\[\s*([^\]]*?)\s*\]\s*/.exec(text);
65
+ if (tag !== null && tag[1]?.toLowerCase() === note.severity.toLowerCase()) {
66
+ return text.slice(tag[0].length);
67
+ }
68
+ return text;
69
+ }
70
+
71
+ /**
72
+ * The findings of the tasks, oldest first. Within one task a finding with the
73
+ * same file, line and text (whitespace collapsed) is kept once, as its newest
74
+ * copy with its own round (the rule of arc's follow-up); findings of
75
+ * different tasks are never merged.
76
+ */
77
+ function linesOf(tasks: readonly StoredObject[]): Line[] {
78
+ const lines: Line[] = [];
79
+ for (const object of tasks) {
80
+ const seen = new Map<string, number>();
81
+ for (const round of taskData(object.data).reviewNotes ?? []) {
82
+ for (const note of round.findings) {
83
+ const key = JSON.stringify([note.file, note.line, textOf(note)]);
84
+ const line = { note, task: bareId(object.id), round: round.round };
85
+ const at = seen.get(key);
86
+ if (at !== undefined) {
87
+ lines[at] = line;
88
+ continue;
89
+ }
90
+ seen.set(key, lines.length);
91
+ lines.push(line);
92
+ }
93
+ }
94
+ }
95
+ return lines;
96
+ }
97
+
98
+ /** The brief of the follow-up: findings grouped by task, each with level, place and text. */
99
+ export function followupBrief(planId: string, lines: readonly Line[]): string {
100
+ const out: string[] = [
101
+ `The reviews of ${planId}'s tasks found these, none of which failed a review.`,
102
+ "Fix what still applies, and say which of them no longer apply.",
103
+ ];
104
+ for (const task of new Set(lines.map((line) => line.task))) {
105
+ out.push("", `## From ${task}`, "");
106
+ for (const { note, round } of lines.filter((line) => line.task === task)) {
107
+ const where = note.line === null ? note.file : `${note.file}:${note.line}`;
108
+ out.push(`- [${note.severity}] ${where} ${textOf(note)} (round ${round})`);
109
+ }
110
+ }
111
+ return out.join("\n");
112
+ }
113
+
114
+ function slugFor(planSlug: string, taken: ReadonlySet<string>): string {
115
+ for (let n = 1; ; n += 1) {
116
+ const tail = n === 1 ? SUFFIX : `${SUFFIX}-${n}`;
117
+ const head = planSlug.slice(0, SLUG_MAX - tail.length).replace(/-+$/, "");
118
+ const slug = `${head}${tail}`;
119
+ if (!taken.has(slug)) return slug;
120
+ }
121
+ }
122
+
123
+ async function markCollected(
124
+ store: ObjectStore,
125
+ repository: string,
126
+ ids: readonly string[],
127
+ tasks: readonly StoredObject[],
128
+ ): Promise<void> {
129
+ for (const object of tasks) {
130
+ const task = taskData(object.data);
131
+ if (task.repository !== repository) continue;
132
+ if (!ids.includes(bareId(object.id)) || task.followups?.state !== "collect") continue;
133
+ await store.put(
134
+ "task",
135
+ object.id,
136
+ { ...task, followups: { ...task.followups, state: "collected" } },
137
+ "dev:followups",
138
+ );
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Proposes the follow-up of every plan of the repository that has no open
144
+ * task and marked merged tasks. Returns the ids of the tasks it proposed.
145
+ */
146
+ export async function collectFollowups(
147
+ store: ObjectStore,
148
+ repository: string,
149
+ settings: DevSettings,
150
+ ): Promise<string[]> {
151
+ // A tick that has nothing to collect must not wait for the repository lock.
152
+ const pending = (await listAll(store, "task")).some((object) => {
153
+ const task = taskData(object.data);
154
+ return task.repository === repository && task.followups?.state === "collect";
155
+ });
156
+ if (!pending) return [];
157
+ return withRepoLock(store, repository, async (fresh) => {
158
+ // Finish a collection a crash cut short: a follow-up names its tasks.
159
+ const before = (await listAll(store, "task")).filter(
160
+ (object) => taskData(object.data).repository === repository,
161
+ );
162
+ for (const object of before) {
163
+ const of = taskData(object.data).followupOf;
164
+ if (of !== undefined) await markCollected(store, repository, of, before);
165
+ }
166
+ if (!settings.reviewer.followups) return [];
167
+
168
+ const proposed: string[] = [];
169
+ for (const planObject of await listAll(store, "plan")) {
170
+ const plan = planData(planObject.data);
171
+ if (plan.repository !== repository || plan.status !== "confirmed") continue;
172
+ const planId = bareId(planObject.id);
173
+ const tasks = (await listAll(store, "task")).filter(
174
+ (object) =>
175
+ taskData(object.data).repository === repository &&
176
+ taskData(object.data).plan === planId,
177
+ );
178
+ if (tasks.some((object) => OPEN_PHASES.includes(taskData(object.data).phase))) continue;
179
+ const marked = tasks
180
+ .filter((object) => {
181
+ const task = taskData(object.data);
182
+ return (
183
+ task.phase === "merged" &&
184
+ task.followupOf === undefined &&
185
+ task.followups?.state === "collect"
186
+ );
187
+ })
188
+ .sort(
189
+ (a, b) =>
190
+ (taskData(a.data).followups?.mergedAt ?? 0) -
191
+ (taskData(b.data).followups?.mergedAt ?? 0) ||
192
+ a.id.localeCompare(b.id),
193
+ );
194
+ const lines = linesOf(marked);
195
+ if (lines.length === 0) continue;
196
+ const taken = new Set<string>();
197
+ for (const object of await listAll(store, "task")) {
198
+ if (taskData(object.data).repository !== repository) continue;
199
+ const slug = parseId(bareId(object.id), "task")?.slug;
200
+ if (slug !== undefined) taken.add(slug);
201
+ }
202
+ const planSlug = parseId(planId, "plan")?.slug ?? "plan";
203
+ const ids = marked.map((object) => bareId(object.id));
204
+ const task = await addTaskLocked(
205
+ store,
206
+ {
207
+ plan: (await getPlan(store, planObject.id)).id,
208
+ slug: slugFor(planSlug, taken),
209
+ title: `Follow-ups from ${planId}'s reviews`,
210
+ brief: followupBrief(planId, lines),
211
+ after: ids,
212
+ followupOf: ids,
213
+ openedBy: "dev",
214
+ },
215
+ fresh,
216
+ );
217
+ await followupTestHooks.afterPropose?.();
218
+ await markCollected(store, repository, ids, marked);
219
+ proposed.push(bareId(task.id));
220
+ }
221
+ return proposed;
222
+ });
223
+ }