@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.
- package/LICENSE +21 -0
- package/README.md +795 -2
- package/dev.schema.json +602 -0
- package/flows/chat-worktree.flow.ts +18 -0
- package/flows/dev.flow.ts +7 -0
- package/package.json +29 -4
- package/src/actions.ts +591 -0
- package/src/announce.ts +225 -0
- package/src/chat.ts +234 -0
- package/src/check.ts +80 -0
- package/src/commands.ts +417 -0
- package/src/editor.ts +55 -0
- package/src/flaky.ts +716 -0
- package/src/flow-compiler.ts +858 -0
- package/src/followups.ts +223 -0
- package/src/gate.ts +2897 -0
- package/src/git.ts +138 -0
- package/src/index.ts +226 -0
- package/src/integrate.ts +1201 -0
- package/src/integration-queue.ts +393 -0
- package/src/lock.ts +36 -0
- package/src/mainclone.ts +102 -0
- package/src/marks.ts +126 -0
- package/src/model.ts +1165 -0
- package/src/notcode.ts +266 -0
- package/src/place-order.ts +161 -0
- package/src/place-wait.ts +64 -0
- package/src/places.ts +368 -0
- package/src/prepare.ts +106 -0
- package/src/progress.ts +115 -0
- package/src/project-flows.ts +160 -0
- package/src/project-settings.ts +185 -0
- package/src/refusals.ts +336 -0
- package/src/rerun.ts +378 -0
- package/src/reruns.ts +62 -0
- package/src/retry.ts +212 -0
- package/src/rework.ts +366 -0
- package/src/schedule.ts +1052 -0
- package/src/service.ts +2160 -0
- package/src/settings.ts +1463 -0
- package/src/standing.ts +87 -0
- package/src/stdin.ts +105 -0
- package/src/steps.ts +1775 -0
- package/src/stops.ts +103 -0
- package/src/throughput.ts +347 -0
- package/src/timeouts.ts +60 -0
- package/src/views.ts +1167 -0
- package/src/waits.ts +105 -0
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The integration queue: at most one integration of a repository runs at a
|
|
3
|
+
* time. A task whose merge was chosen takes a ticket, a small file under
|
|
4
|
+
* `<home>/dev/integration-queue/<repository>/` numbered in the order tickets
|
|
5
|
+
* were taken. The first ticket whose process is alive has the turn; a ticket
|
|
6
|
+
* whose process died is dropped by whoever looks next, so a turn is given back
|
|
7
|
+
* however the integration ends. Waiting for a turn is not a check and holds no
|
|
8
|
+
* check place.
|
|
9
|
+
*
|
|
10
|
+
* A turn that merges a batch claims the tickets queued behind it. A claimed
|
|
11
|
+
* task does not take a turn of its own: it waits for the leader's result file.
|
|
12
|
+
* A ticket is written only by its own task. Claims and "integrate alone" marks
|
|
13
|
+
* are separate files the leader owns (`c-<leader>.json`, `s-<task>.json`), so a
|
|
14
|
+
* task that leaves while it is claimed is never written back.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
existsSync,
|
|
19
|
+
mkdirSync,
|
|
20
|
+
readdirSync,
|
|
21
|
+
readFileSync,
|
|
22
|
+
renameSync,
|
|
23
|
+
rmSync,
|
|
24
|
+
writeFileSync,
|
|
25
|
+
} from "node:fs";
|
|
26
|
+
import { join } from "node:path";
|
|
27
|
+
import { waitForChange, withLockFile } from "@operad-io/engine/platform";
|
|
28
|
+
|
|
29
|
+
/** How often a waiting integration looks again when nothing announced a change. */
|
|
30
|
+
const RECHECK_MS = 500;
|
|
31
|
+
|
|
32
|
+
/** One queued task: what a leader needs to merge it for the task. */
|
|
33
|
+
export interface Ticket {
|
|
34
|
+
readonly seq: number;
|
|
35
|
+
readonly task: string;
|
|
36
|
+
readonly run: string;
|
|
37
|
+
readonly pid: number;
|
|
38
|
+
readonly verified: string;
|
|
39
|
+
readonly message: string;
|
|
40
|
+
/** Set by a batch that fell back: this task integrates alone, never in a batch. */
|
|
41
|
+
readonly solo?: true;
|
|
42
|
+
/** The task whose batch this task is part of. */
|
|
43
|
+
readonly claimedBy?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** What a batch's leader leaves for the tasks it merged. */
|
|
47
|
+
export interface BatchResult {
|
|
48
|
+
readonly base: string;
|
|
49
|
+
/** The pushed head of the batch. */
|
|
50
|
+
readonly head: string;
|
|
51
|
+
readonly note?: string | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* `pending` from before the leader pushes until it has recorded the merge. A
|
|
54
|
+
* pending result whose leader is gone is settled by whether `head` landed.
|
|
55
|
+
* A result without a state is done.
|
|
56
|
+
*/
|
|
57
|
+
readonly state?: "pending" | "done" | undefined;
|
|
58
|
+
readonly members: readonly { readonly task: string; readonly sha: string }[];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Thrown when the step is cancelled while it waits for its turn. */
|
|
62
|
+
export class IntegrationQueueCancelled extends Error {
|
|
63
|
+
constructor() {
|
|
64
|
+
super("cancelled while waiting to integrate");
|
|
65
|
+
this.name = "IntegrationQueueCancelled";
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** What waiting ends with. */
|
|
70
|
+
export type Turn =
|
|
71
|
+
| { readonly kind: "turn" }
|
|
72
|
+
| { readonly kind: "batched"; readonly leader: string; readonly result: BatchResult };
|
|
73
|
+
|
|
74
|
+
export interface QueueOptions {
|
|
75
|
+
readonly home: string;
|
|
76
|
+
readonly repository: { readonly name: string; readonly path: string };
|
|
77
|
+
readonly task: string;
|
|
78
|
+
readonly run: string;
|
|
79
|
+
readonly verified: string;
|
|
80
|
+
readonly message: string;
|
|
81
|
+
readonly signal?: AbortSignal | undefined;
|
|
82
|
+
/** Says who has the turn; called when that changes. */
|
|
83
|
+
readonly say: (line: string, holder: string) => Promise<unknown> | unknown;
|
|
84
|
+
/** How a waiting task waits before it looks again; tests inject this. */
|
|
85
|
+
readonly wait?: ((ms: number) => Promise<void>) | undefined;
|
|
86
|
+
/** Whether a process is alive; tests inject this. */
|
|
87
|
+
readonly alive?: ((pid: number) => boolean) | undefined;
|
|
88
|
+
/** Whether a dead leader's pending batch reached the remote's main. */
|
|
89
|
+
readonly landed?: ((result: BatchResult) => boolean) | undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function integrationQueueDirectory(
|
|
93
|
+
home: string,
|
|
94
|
+
repository: { readonly name: string; readonly path: string },
|
|
95
|
+
): string {
|
|
96
|
+
const hash = new Bun.CryptoHasher("sha1").update(repository.path).digest("hex");
|
|
97
|
+
const name = repository.name.replace(/[^A-Za-z0-9._-]/g, "_");
|
|
98
|
+
return join(home, "dev", "integration-queue", `${name}-${hash.slice(0, 8)}`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function processAlive(pid: number): boolean {
|
|
102
|
+
try {
|
|
103
|
+
process.kill(pid, 0);
|
|
104
|
+
return true;
|
|
105
|
+
} catch (error) {
|
|
106
|
+
return (error as { code?: unknown }).code === "EPERM";
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function safeName(task: string): string {
|
|
111
|
+
return task.replace(/[^A-Za-z0-9._-]/g, "_");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function writeAtomic(path: string, value: unknown): void {
|
|
115
|
+
const temporary = `${path}.${process.pid}.tmp`;
|
|
116
|
+
writeFileSync(temporary, JSON.stringify(value));
|
|
117
|
+
renameSync(temporary, path);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function parseTicket(text: string): Ticket | undefined {
|
|
121
|
+
try {
|
|
122
|
+
const value: unknown = JSON.parse(text);
|
|
123
|
+
if (typeof value !== "object" || value === null) return undefined;
|
|
124
|
+
const record = value as Record<string, unknown>;
|
|
125
|
+
if (
|
|
126
|
+
typeof record.seq !== "number" ||
|
|
127
|
+
typeof record.task !== "string" ||
|
|
128
|
+
typeof record.run !== "string" ||
|
|
129
|
+
typeof record.pid !== "number" ||
|
|
130
|
+
typeof record.verified !== "string" ||
|
|
131
|
+
typeof record.message !== "string"
|
|
132
|
+
) {
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
seq: record.seq,
|
|
137
|
+
task: record.task,
|
|
138
|
+
run: record.run,
|
|
139
|
+
pid: record.pid,
|
|
140
|
+
verified: record.verified,
|
|
141
|
+
message: record.message,
|
|
142
|
+
};
|
|
143
|
+
} catch {
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export class IntegrationQueue {
|
|
149
|
+
private readonly directory: string;
|
|
150
|
+
private readonly alive: (pid: number) => boolean;
|
|
151
|
+
private joined = false;
|
|
152
|
+
|
|
153
|
+
constructor(private readonly options: QueueOptions) {
|
|
154
|
+
this.directory = integrationQueueDirectory(options.home, options.repository);
|
|
155
|
+
this.alive = options.alive ?? processAlive;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private ticketPath(task: string): string {
|
|
159
|
+
return join(this.directory, `t-${safeName(task)}.json`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private claimPath(leader: string): string {
|
|
163
|
+
return join(this.directory, `c-${safeName(leader)}.json`);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private soloPath(task: string): string {
|
|
167
|
+
return join(this.directory, `s-${safeName(task)}.json`);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private resultPath(leader: string): string {
|
|
171
|
+
return join(this.directory, `r-${safeName(leader)}.json`);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** The live tickets in queue order; tickets of dead processes are dropped. */
|
|
175
|
+
tickets(): readonly Ticket[] {
|
|
176
|
+
let names: string[];
|
|
177
|
+
try {
|
|
178
|
+
names = readdirSync(this.directory);
|
|
179
|
+
} catch {
|
|
180
|
+
return [];
|
|
181
|
+
}
|
|
182
|
+
const live: Ticket[] = [];
|
|
183
|
+
for (const name of names) {
|
|
184
|
+
if (!name.startsWith("t-") || !name.endsWith(".json")) continue;
|
|
185
|
+
const path = join(this.directory, name);
|
|
186
|
+
let text: string;
|
|
187
|
+
try {
|
|
188
|
+
text = readFileSync(path, "utf8");
|
|
189
|
+
} catch {
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
const ticket = parseTicket(text);
|
|
193
|
+
if (ticket === undefined) continue;
|
|
194
|
+
if (!this.alive(ticket.pid)) {
|
|
195
|
+
rmSync(path, { force: true });
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
live.push(ticket);
|
|
199
|
+
}
|
|
200
|
+
live.sort((left, right) => left.seq - right.seq);
|
|
201
|
+
// A claim holds while its leader is queued or has left a result; it
|
|
202
|
+
// names only tasks that still have a ticket.
|
|
203
|
+
const claims = new Map<string, string>();
|
|
204
|
+
for (const name of names) {
|
|
205
|
+
if (!name.startsWith("c-") || !name.endsWith(".json")) continue;
|
|
206
|
+
const file = name.slice(2, -5);
|
|
207
|
+
let claim: { leader?: unknown; members?: unknown } = {};
|
|
208
|
+
try {
|
|
209
|
+
claim = JSON.parse(readFileSync(join(this.directory, name), "utf8"));
|
|
210
|
+
} catch {
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
const leader = claim.leader;
|
|
214
|
+
const members = claim.members;
|
|
215
|
+
const claimed =
|
|
216
|
+
Array.isArray(members) ? live.filter((ticket) => members.includes(ticket.task)) : [];
|
|
217
|
+
const resulted = names.includes(`r-${file}.json`);
|
|
218
|
+
if (
|
|
219
|
+
typeof leader !== "string" ||
|
|
220
|
+
claimed.length === 0 ||
|
|
221
|
+
(!live.some((ticket) => ticket.task === leader) && !resulted)
|
|
222
|
+
) {
|
|
223
|
+
rmSync(join(this.directory, name), { force: true });
|
|
224
|
+
rmSync(join(this.directory, `r-${file}.json`), { force: true });
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
for (const ticket of claimed) claims.set(ticket.task, leader);
|
|
228
|
+
}
|
|
229
|
+
for (const name of names) {
|
|
230
|
+
if (!name.startsWith("r-") || !name.endsWith(".json")) continue;
|
|
231
|
+
if (!existsSync(join(this.directory, `c-${name.slice(2)}`))) {
|
|
232
|
+
rmSync(join(this.directory, name), { force: true });
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const solo = new Set<string>();
|
|
236
|
+
for (const name of names) {
|
|
237
|
+
if (!name.startsWith("s-") || !name.endsWith(".json")) continue;
|
|
238
|
+
const owner = live.find((ticket) => safeName(ticket.task) === name.slice(2, -5));
|
|
239
|
+
if (owner === undefined) rmSync(join(this.directory, name), { force: true });
|
|
240
|
+
else solo.add(owner.task);
|
|
241
|
+
}
|
|
242
|
+
return live.map((ticket) => {
|
|
243
|
+
const claimedBy = claims.get(ticket.task);
|
|
244
|
+
return {
|
|
245
|
+
...ticket,
|
|
246
|
+
...(claimedBy === undefined ? {} : { claimedBy }),
|
|
247
|
+
...(solo.has(ticket.task) ? { solo: true as const } : {}),
|
|
248
|
+
};
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Takes a ticket at the end of the queue. */
|
|
253
|
+
async join(): Promise<void> {
|
|
254
|
+
mkdirSync(this.directory, { recursive: true });
|
|
255
|
+
await withLockFile(
|
|
256
|
+
join(this.directory, "join.lock"),
|
|
257
|
+
() => {
|
|
258
|
+
const last = this.tickets().reduce((most, ticket) => Math.max(most, ticket.seq), 0);
|
|
259
|
+
const ticket: Ticket = {
|
|
260
|
+
seq: last + 1,
|
|
261
|
+
task: this.options.task,
|
|
262
|
+
run: this.options.run,
|
|
263
|
+
pid: process.pid,
|
|
264
|
+
verified: this.options.verified,
|
|
265
|
+
message: this.options.message,
|
|
266
|
+
};
|
|
267
|
+
writeAtomic(this.ticketPath(this.options.task), ticket);
|
|
268
|
+
this.joined = true;
|
|
269
|
+
},
|
|
270
|
+
{ label: "integration queue", onLog: () => {} },
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** Gives the turn back, and the ticket with it. */
|
|
275
|
+
leave(): void {
|
|
276
|
+
if (!this.joined) return;
|
|
277
|
+
this.joined = false;
|
|
278
|
+
rmSync(this.ticketPath(this.options.task), { force: true });
|
|
279
|
+
rmSync(this.soloPath(this.options.task), { force: true });
|
|
280
|
+
// Looking prunes what no one needs now: a leader's claim and result stay
|
|
281
|
+
// while a task it claimed is still queued.
|
|
282
|
+
this.tickets();
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
private readResult(leader: string): BatchResult | undefined {
|
|
286
|
+
try {
|
|
287
|
+
return JSON.parse(readFileSync(this.resultPath(leader), "utf8")) as BatchResult;
|
|
288
|
+
} catch {
|
|
289
|
+
return undefined;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Waits until this task has the turn or has been merged by a batch. */
|
|
294
|
+
async turn(): Promise<Turn> {
|
|
295
|
+
const me = this.options.task;
|
|
296
|
+
let said: string | undefined;
|
|
297
|
+
const wait =
|
|
298
|
+
this.options.wait ??
|
|
299
|
+
((ms: number) => waitForChange(this.directory, "", ms));
|
|
300
|
+
for (;;) {
|
|
301
|
+
const tickets = this.tickets();
|
|
302
|
+
const mine = tickets.find((ticket) => ticket.task === me);
|
|
303
|
+
let holder: string | undefined;
|
|
304
|
+
const claimed = mine?.claimedBy;
|
|
305
|
+
if (claimed !== undefined) {
|
|
306
|
+
// A merge already on main is reported even when the step was aborted
|
|
307
|
+
// after the push: the result is read before the abort is.
|
|
308
|
+
const result = this.readResult(claimed);
|
|
309
|
+
if (result !== undefined && result.state !== "pending") {
|
|
310
|
+
return { kind: "batched", leader: claimed, result };
|
|
311
|
+
}
|
|
312
|
+
if (result !== undefined && !tickets.some((ticket) => ticket.task === claimed)) {
|
|
313
|
+
// The leader died between writing the result and recording the merge.
|
|
314
|
+
if (this.options.landed?.(result) === true) {
|
|
315
|
+
return { kind: "batched", leader: claimed, result };
|
|
316
|
+
}
|
|
317
|
+
// Its push never landed: the claim is void and this task goes on alone.
|
|
318
|
+
rmSync(this.claimPath(claimed), { force: true });
|
|
319
|
+
rmSync(this.resultPath(claimed), { force: true });
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
holder = claimed;
|
|
323
|
+
}
|
|
324
|
+
if (this.options.signal?.aborted === true) throw new IntegrationQueueCancelled();
|
|
325
|
+
if (mine === undefined) throw new Error("the integration ticket is gone");
|
|
326
|
+
if (holder === undefined) {
|
|
327
|
+
const head = tickets.find((ticket) => ticket.claimedBy === undefined);
|
|
328
|
+
if (head !== undefined && head.task === me) return { kind: "turn" };
|
|
329
|
+
holder = head?.task;
|
|
330
|
+
}
|
|
331
|
+
if (holder !== undefined && said !== holder) {
|
|
332
|
+
said = holder;
|
|
333
|
+
await this.options.say(`waiting to integrate: ${holder} is integrating`, holder);
|
|
334
|
+
}
|
|
335
|
+
await wait(RECHECK_MS);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* At the start of a turn: claims up to `limit - 1` tickets queued behind
|
|
341
|
+
* this one that may join a batch, and returns them in queue order.
|
|
342
|
+
*/
|
|
343
|
+
claim(limit: number): readonly Ticket[] {
|
|
344
|
+
const me = this.options.task;
|
|
345
|
+
const mine = this.tickets().find((ticket) => ticket.task === me);
|
|
346
|
+
if (mine === undefined || mine.solo === true || limit <= 1) return [];
|
|
347
|
+
const followers = this.tickets()
|
|
348
|
+
.filter(
|
|
349
|
+
(ticket) =>
|
|
350
|
+
ticket.seq > mine.seq && ticket.solo !== true && ticket.claimedBy === undefined,
|
|
351
|
+
)
|
|
352
|
+
.slice(0, limit - 1);
|
|
353
|
+
if (followers.length > 0) {
|
|
354
|
+
writeAtomic(this.claimPath(me), {
|
|
355
|
+
leader: me,
|
|
356
|
+
members: followers.map((ticket) => ticket.task),
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
return followers;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/** The claimed tasks that are still queued: a task that left is not merged. */
|
|
363
|
+
stillQueued(followers: readonly Ticket[]): readonly Ticket[] {
|
|
364
|
+
const queued = new Set(this.tickets().map((ticket) => ticket.task));
|
|
365
|
+
return followers.filter((ticket) => queued.has(ticket.task));
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** A batch that could not land: its tasks integrate one by one, never batched again. */
|
|
369
|
+
fallBack(followers: readonly Ticket[]): void {
|
|
370
|
+
for (const ticket of followers) {
|
|
371
|
+
writeAtomic(this.soloPath(ticket.task), { task: ticket.task });
|
|
372
|
+
}
|
|
373
|
+
rmSync(this.claimPath(this.options.task), { force: true });
|
|
374
|
+
rmSync(this.resultPath(this.options.task), { force: true });
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** Leaves what the claimed tasks need to record their merges, before the push. */
|
|
378
|
+
publish(result: BatchResult): void {
|
|
379
|
+
writeAtomic(this.resultPath(this.options.task), { ...result, state: "pending" });
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** The push landed and the leader has recorded it: the result is final. */
|
|
383
|
+
finish(): void {
|
|
384
|
+
const result = this.readResult(this.options.task);
|
|
385
|
+
if (result === undefined) return;
|
|
386
|
+
writeAtomic(this.resultPath(this.options.task), { ...result, state: "done" });
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** A claimed task is done with the result. */
|
|
390
|
+
done(): void {
|
|
391
|
+
this.leave();
|
|
392
|
+
}
|
|
393
|
+
}
|
package/src/lock.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** The repository lock: one dev writer per repository, safe across processes. */
|
|
2
|
+
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
LOCK_TIMEOUT_MS,
|
|
6
|
+
LockTimeoutError,
|
|
7
|
+
withLockFile,
|
|
8
|
+
type LockFileOptions,
|
|
9
|
+
} from "@operad-io/engine/platform";
|
|
10
|
+
import { DevError } from "./model.ts";
|
|
11
|
+
|
|
12
|
+
/** What a waiter may inject: the clock, how it waits, and where a log goes. */
|
|
13
|
+
export type RepositoryLockOptions = Omit<LockFileOptions, "label">;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Holds one repository's lock while ids and counts change. The lock file makes
|
|
17
|
+
* two commands in two processes take turns; commands in one process wait too.
|
|
18
|
+
*/
|
|
19
|
+
export async function withRepositoryLock<T>(
|
|
20
|
+
repositoryPath: string,
|
|
21
|
+
work: () => Promise<T> | T,
|
|
22
|
+
options: RepositoryLockOptions = {},
|
|
23
|
+
): Promise<T> {
|
|
24
|
+
const lockPath = join(repositoryPath, ".operad", "dev.lock");
|
|
25
|
+
try {
|
|
26
|
+
return await withLockFile(lockPath, work, { ...options, label: "dev lock" });
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if (error instanceof LockTimeoutError) {
|
|
29
|
+
throw new DevError(
|
|
30
|
+
"lock",
|
|
31
|
+
`another dev command holds ${error.lockPath} (${error.holder}); gave up after ${LOCK_TIMEOUT_MS / 1_000}s`,
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/mainclone.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* After a merge landed on the remote main, bring the repository's main clone up
|
|
3
|
+
* to it when that is safe. The clone is what serve and every run execute, so a
|
|
4
|
+
* merge that leaves it behind leaves the old code running. Only a fast-forward
|
|
5
|
+
* through git is ever used: no reset, rebase or stash.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { git } from "./steps.ts";
|
|
9
|
+
|
|
10
|
+
/** What a command runner reports. */
|
|
11
|
+
export interface CommandResult {
|
|
12
|
+
readonly ok: boolean;
|
|
13
|
+
/** The last line of the output. */
|
|
14
|
+
readonly out: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Runs one prepare line in a directory. The merge step passes one that runs it
|
|
19
|
+
* as a checkout's prepare line runs; tests replace it.
|
|
20
|
+
*/
|
|
21
|
+
export type CommandRunner = (
|
|
22
|
+
cwd: string,
|
|
23
|
+
line: string,
|
|
24
|
+
) => Promise<CommandResult>;
|
|
25
|
+
|
|
26
|
+
/** What happened to the main clone. */
|
|
27
|
+
export interface MainCloneOutcome {
|
|
28
|
+
readonly updated: boolean;
|
|
29
|
+
/** Why the clone was not updated, or how the prepare lines ended. */
|
|
30
|
+
readonly note?: string | undefined;
|
|
31
|
+
/** True when the prepare lines ran and passed. */
|
|
32
|
+
readonly prepared?: boolean | undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function lastLine(text: string): string {
|
|
36
|
+
const lines = text.split("\n").filter((line) => line.trim().length > 0);
|
|
37
|
+
return lines[lines.length - 1] ?? "";
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Runs the lines in order in the clone; the first failure stops them. */
|
|
41
|
+
async function prepare(
|
|
42
|
+
repository: string,
|
|
43
|
+
lines: readonly string[],
|
|
44
|
+
run: CommandRunner,
|
|
45
|
+
): Promise<MainCloneOutcome> {
|
|
46
|
+
if (lines.length === 0) return { updated: true };
|
|
47
|
+
for (const line of lines) {
|
|
48
|
+
const done = await run(repository, line);
|
|
49
|
+
if (!done.ok) {
|
|
50
|
+
return {
|
|
51
|
+
updated: true,
|
|
52
|
+
prepared: false,
|
|
53
|
+
note: `prepare failed: ${line}: ${lastLine(done.out)}`,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return { updated: true, prepared: true, note: "prepare done" };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The text a task's Changes shows, or undefined when there is nothing to say. */
|
|
61
|
+
export function mainCloneChange(outcome: MainCloneOutcome): string | undefined {
|
|
62
|
+
if (!outcome.updated) return `main clone not updated: ${outcome.note ?? "unknown"}`;
|
|
63
|
+
return outcome.note === undefined ? undefined : `main clone: ${outcome.note}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Fast-forwards the main clone to `sha` when it is on `branch` and clean, then
|
|
68
|
+
* runs the prepare lines there when the clone moved.
|
|
69
|
+
*/
|
|
70
|
+
export async function updateMainClone(
|
|
71
|
+
repository: string,
|
|
72
|
+
branch: string,
|
|
73
|
+
sha: string,
|
|
74
|
+
lines: readonly string[],
|
|
75
|
+
run: CommandRunner,
|
|
76
|
+
): Promise<MainCloneOutcome> {
|
|
77
|
+
const current = git(repository, ["symbolic-ref", "--short", "-q", "HEAD"]);
|
|
78
|
+
if (!current.ok || current.out !== branch) {
|
|
79
|
+
return {
|
|
80
|
+
updated: false,
|
|
81
|
+
note: `the main clone is on ${current.ok ? `"${current.out}"` : "no branch"}, not "${branch}"`,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
const status = git(repository, ["status", "--porcelain", "--untracked-files=normal"]);
|
|
85
|
+
if (!status.ok) {
|
|
86
|
+
return { updated: false, note: `cannot read its status: ${status.err}` };
|
|
87
|
+
}
|
|
88
|
+
if (status.out.length > 0) {
|
|
89
|
+
return { updated: false, note: "uncommitted changes" };
|
|
90
|
+
}
|
|
91
|
+
if (git(repository, ["merge-base", "--is-ancestor", sha, "HEAD"]).ok) {
|
|
92
|
+
return { updated: true };
|
|
93
|
+
}
|
|
94
|
+
const merge = git(repository, ["merge", "--ff-only", sha]);
|
|
95
|
+
if (!merge.ok) {
|
|
96
|
+
return {
|
|
97
|
+
updated: false,
|
|
98
|
+
note: `cannot fast-forward: ${lastLine(merge.err || merge.out) || "git merge failed"}`,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
return prepare(repository, lines, run);
|
|
102
|
+
}
|
package/src/marks.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The mark and badge a dev row carries (presentation decision 10). The marks
|
|
3
|
+
* are read from the same task and plan data the views read: the phase, what
|
|
4
|
+
* the attempt has recorded so far, and the holds the scheduler keeps.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { RowMark } from "@operad-io/engine";
|
|
8
|
+
import {
|
|
9
|
+
isIntegrating,
|
|
10
|
+
planState,
|
|
11
|
+
type PlanData,
|
|
12
|
+
type PlanState,
|
|
13
|
+
type RepositoryData,
|
|
14
|
+
type TaskData,
|
|
15
|
+
type TaskPhase,
|
|
16
|
+
} from "./model.ts";
|
|
17
|
+
|
|
18
|
+
/** A review that passed, as the scheduler's auto merge reads it. */
|
|
19
|
+
function reviewPassed(task: TaskData): boolean {
|
|
20
|
+
const review = task.attempt?.review;
|
|
21
|
+
return (
|
|
22
|
+
review?.decision === "passed" ||
|
|
23
|
+
review?.result === "pass" ||
|
|
24
|
+
review?.result === "passed"
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A task that waits: for a merge, a rework answer, or something else. */
|
|
29
|
+
function waitingMark(task: TaskData): RowMark {
|
|
30
|
+
const attempt = task.attempt;
|
|
31
|
+
if (isIntegrating(task)) return "integrating";
|
|
32
|
+
const integration = attempt?.integration;
|
|
33
|
+
if (integration !== undefined && integration !== null) return "waiting";
|
|
34
|
+
const rework = attempt?.rework;
|
|
35
|
+
if (rework?.result === "ask") {
|
|
36
|
+
// An ask is written when the streak passed the configured limit (a limit
|
|
37
|
+
// of 0 included): the automatic passes ran out, which is giving up.
|
|
38
|
+
return rework.limit !== undefined && rework.streak > rework.limit
|
|
39
|
+
? "failed"
|
|
40
|
+
: "rework_pending";
|
|
41
|
+
}
|
|
42
|
+
return reviewPassed(task) ? "ready_to_merge" : "waiting";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** A task that runs: the step is told by what the attempt has recorded. */
|
|
46
|
+
function runningMark(task: TaskData): RowMark {
|
|
47
|
+
const attempt = task.attempt;
|
|
48
|
+
// A conflict or failed check sent the flow back to the worktree step; the
|
|
49
|
+
// record stays, but the step is no longer integrate.
|
|
50
|
+
if (isIntegrating(task)) return "integrating";
|
|
51
|
+
if (typeof attempt?.workRetryAt === "number") return "retry_wait";
|
|
52
|
+
// The work step waits for a worker another task holds: a started task in a queue.
|
|
53
|
+
if (typeof attempt?.workerWait === "string" && attempt.workerWait.length > 0) return "retry_wait";
|
|
54
|
+
if (attempt === undefined || typeof attempt.worktree !== "string") {
|
|
55
|
+
return "starting";
|
|
56
|
+
}
|
|
57
|
+
return attempt.worked === true ? "checking" : "running";
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A task in review: the gate and the review model check it. */
|
|
61
|
+
function reviewMark(task: TaskData): RowMark {
|
|
62
|
+
return typeof task.retryAt === "number" ? "retry_wait" : "checking";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const TASK_MARKS: Readonly<Record<TaskPhase, (task: TaskData) => RowMark>> = {
|
|
66
|
+
proposed: () => "proposed",
|
|
67
|
+
ready: (task) =>
|
|
68
|
+
task.dependencyHeld === true || task.areaHeldBy !== undefined
|
|
69
|
+
? "blocked"
|
|
70
|
+
: "todo",
|
|
71
|
+
running: runningMark,
|
|
72
|
+
review: reviewMark,
|
|
73
|
+
waiting: waitingMark,
|
|
74
|
+
paused: () => "paused",
|
|
75
|
+
merged: () => "done",
|
|
76
|
+
cancelled: () => "cancelled",
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/** The mark of a task row. */
|
|
80
|
+
export function taskMark(task: TaskData): RowMark {
|
|
81
|
+
// A step that waits for a check place is not running or checking yet.
|
|
82
|
+
const placeWait = task.attempt?.placeWait;
|
|
83
|
+
if (
|
|
84
|
+
placeWait !== undefined &&
|
|
85
|
+
placeWait !== null &&
|
|
86
|
+
(task.phase === "running" || task.phase === "review" || task.phase === "waiting")
|
|
87
|
+
) {
|
|
88
|
+
return "retry_wait";
|
|
89
|
+
}
|
|
90
|
+
return TASK_MARKS[task.phase](task);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** `#<attempt>` as history counts it; `#0` before the first. */
|
|
94
|
+
export function taskBadge(task: TaskData): string {
|
|
95
|
+
return `#${Math.max(task.attempts, task.attempt?.number ?? 0)}`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const PLAN_MARKS: Readonly<Record<PlanState, (plan: PlanData) => RowMark>> = {
|
|
99
|
+
draft: () => "proposed",
|
|
100
|
+
// A task waiting for a person wins over running: the owner has something to do.
|
|
101
|
+
confirmed: (plan) =>
|
|
102
|
+
(plan.waitingCount ?? 0) > 0
|
|
103
|
+
? "waiting"
|
|
104
|
+
: (plan.activeCount ?? 0) > 0
|
|
105
|
+
? "running"
|
|
106
|
+
: "todo",
|
|
107
|
+
paused: () => "paused",
|
|
108
|
+
cancelled: () => "cancelled",
|
|
109
|
+
finished: () => "done",
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/** The mark of a plan row. */
|
|
113
|
+
export function planMark(plan: PlanData): RowMark {
|
|
114
|
+
return PLAN_MARKS[planState(plan)](plan);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The mark of a repository row: it follows the repository's work. A removed
|
|
119
|
+
* repository reads as `cancelled`.
|
|
120
|
+
*/
|
|
121
|
+
export function repositoryMark(repository: RepositoryData): RowMark {
|
|
122
|
+
if (repository.removed) return "cancelled";
|
|
123
|
+
if ((repository.activeTasks ?? 0) > 0) return "running";
|
|
124
|
+
if ((repository.openPlans ?? 0) + (repository.openTasks ?? 0) > 0) return "todo";
|
|
125
|
+
return "done";
|
|
126
|
+
}
|