@operad-io/dev 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +12 -2
- package/dev.schema.json +631 -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 +2950 -0
- package/src/git.ts +138 -0
- package/src/index.ts +227 -0
- package/src/integrate.ts +1213 -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 +1171 -0
- package/src/notcode.ts +305 -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 +192 -0
- package/src/refusals.ts +336 -0
- package/src/rerun.ts +390 -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 +1495 -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
package/src/integrate.ts
ADDED
|
@@ -0,0 +1,1213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The end of a task's flow: land the verified commit on main, then clean the
|
|
3
|
+
* worktrees and branches away. Both steps run git and shell here; the kernel
|
|
4
|
+
* never runs either itself.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { isRecord, messageOf, type PluginStepResult, type StepContext, type StepRunOptions } from "@operad-io/engine";
|
|
8
|
+
import { appendChange, appendRework, storeKey, type CleanupFailure, type CleanupRecord, type IntegrationRecord, type TaskAttempt, type TaskData } from "./model.ts";
|
|
9
|
+
import {
|
|
10
|
+
branchHead,
|
|
11
|
+
gateWorktreePath,
|
|
12
|
+
recreateGateWorktree,
|
|
13
|
+
} from "./gate.ts";
|
|
14
|
+
import { mainCloneRunner, runPrepare, stepOptions } from "./prepare.ts";
|
|
15
|
+
import { checkPlaces } from "./places.ts";
|
|
16
|
+
import {
|
|
17
|
+
IntegrationQueue,
|
|
18
|
+
IntegrationQueueCancelled,
|
|
19
|
+
type BatchResult,
|
|
20
|
+
type Ticket,
|
|
21
|
+
type Turn,
|
|
22
|
+
} from "./integration-queue.ts";
|
|
23
|
+
import {
|
|
24
|
+
mainCloneChange,
|
|
25
|
+
updateMainClone,
|
|
26
|
+
type CommandRunner,
|
|
27
|
+
type MainCloneOutcome,
|
|
28
|
+
} from "./mainclone.ts";
|
|
29
|
+
import { runCheckSteadily } from "./notcode.ts";
|
|
30
|
+
import { runCheckCommands } from "./rerun.ts";
|
|
31
|
+
import { reportFlakyTests } from "./flaky.ts";
|
|
32
|
+
import { recordRerunPasses } from "./reruns.ts";
|
|
33
|
+
import { getPlan, refreshPlanCounts } from "./service.ts";
|
|
34
|
+
import {
|
|
35
|
+
cancelledStep,
|
|
36
|
+
claimAllows,
|
|
37
|
+
conflictedFiles,
|
|
38
|
+
git,
|
|
39
|
+
headOf,
|
|
40
|
+
mergeInProgress,
|
|
41
|
+
saveTaskGuarded,
|
|
42
|
+
taskContext,
|
|
43
|
+
worktreePath,
|
|
44
|
+
type TaskContext,
|
|
45
|
+
} from "./steps.ts";
|
|
46
|
+
import { followupMark } from "./followups.ts";
|
|
47
|
+
import type { DevSettings, IntegrationStyle } from "./settings.ts";
|
|
48
|
+
|
|
49
|
+
/** How many times a moved main sends the candidate back to the fetch. */
|
|
50
|
+
export const MAX_INTEGRATION_ROUNDS = 3;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Test-only hook, run after a successful push and before the step records it.
|
|
54
|
+
* A test child kills itself here to prove a recovery pushes nothing again.
|
|
55
|
+
* Production code never sets it.
|
|
56
|
+
*/
|
|
57
|
+
export const integrateTestHooks: {
|
|
58
|
+
afterPush?: ((candidate: string) => void) | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Called at each point where a kill leaves outside state changed: a test child
|
|
61
|
+
* kills itself at a named point. The points are `candidate-built`,
|
|
62
|
+
* `pending-saved`, `clone-followed` and `merged-saved`; the push has its own
|
|
63
|
+
* hook above, and cleanup calls `hook-ran` after each after-merge hook.
|
|
64
|
+
*/
|
|
65
|
+
at?: ((point: string) => void) | undefined;
|
|
66
|
+
/** Replaces the command runner the main clone's install uses. */
|
|
67
|
+
runCommand?: CommandRunner | undefined;
|
|
68
|
+
/** Called once a task has taken its ticket in the integration queue. */
|
|
69
|
+
queued?: ((task: string) => void) | undefined;
|
|
70
|
+
/** Awaited once a task has the turn, before it builds anything. */
|
|
71
|
+
turnTaken?: ((task: string) => Promise<void> | void) | undefined;
|
|
72
|
+
/** Awaited by a batch's leader once it has claimed the tasks queued behind it. */
|
|
73
|
+
claimed?: ((followers: readonly string[]) => Promise<void> | void) | undefined;
|
|
74
|
+
/** How a task waits for its turn before it looks again. */
|
|
75
|
+
wait?: ((ms: number) => Promise<void>) | undefined;
|
|
76
|
+
} = {};
|
|
77
|
+
|
|
78
|
+
export async function followMain(
|
|
79
|
+
repository: { readonly name: string; readonly path: string },
|
|
80
|
+
branch: string,
|
|
81
|
+
sha: string,
|
|
82
|
+
prepare: { readonly lines: readonly string[]; readonly run: CommandRunner } = {
|
|
83
|
+
lines: [],
|
|
84
|
+
run: async () => ({ ok: true, out: "" }),
|
|
85
|
+
},
|
|
86
|
+
): Promise<MainCloneOutcome> {
|
|
87
|
+
return updateMainClone(
|
|
88
|
+
repository.path,
|
|
89
|
+
branch,
|
|
90
|
+
sha,
|
|
91
|
+
prepare.lines,
|
|
92
|
+
integrateTestHooks.runCommand ?? prepare.run,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function shortSha(sha: string): string {
|
|
97
|
+
return sha.slice(0, 7);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function revParse(cwd: string, ref: string): string | undefined {
|
|
101
|
+
const result = git(cwd, ["rev-parse", "--verify", "--quiet", `${ref}^{commit}`]);
|
|
102
|
+
return result.ok && result.out.length > 0 ? result.out : undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** True when `ancestor` is reachable from `descendant`. */
|
|
106
|
+
function contains(
|
|
107
|
+
cwd: string,
|
|
108
|
+
ancestor: string,
|
|
109
|
+
descendant: string,
|
|
110
|
+
): boolean {
|
|
111
|
+
return git(cwd, ["merge-base", "--is-ancestor", ancestor, descendant]).ok;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
interface Candidate {
|
|
115
|
+
readonly kind: "ok" | "conflict" | "failed";
|
|
116
|
+
readonly sha?: string | undefined;
|
|
117
|
+
readonly message?: string | undefined;
|
|
118
|
+
readonly files?: readonly string[] | undefined;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Builds the candidate on main: a merge commit, or one squashed commit. */
|
|
122
|
+
function mergeCandidate(
|
|
123
|
+
path: string,
|
|
124
|
+
verified: string,
|
|
125
|
+
style: IntegrationStyle,
|
|
126
|
+
message: string,
|
|
127
|
+
): Candidate {
|
|
128
|
+
if (style === "squash") {
|
|
129
|
+
// `--ff` overrides a user's `merge.ff=only`, which would refuse a squash
|
|
130
|
+
// onto a main that moved.
|
|
131
|
+
const merge = git(path, ["merge", "--squash", "--ff", verified]);
|
|
132
|
+
if (!merge.ok) {
|
|
133
|
+
const files = conflictedFiles(path);
|
|
134
|
+
if (files.length > 0) return { kind: "conflict", files };
|
|
135
|
+
return {
|
|
136
|
+
kind: "failed",
|
|
137
|
+
message: `cannot squash ${verified}: ${merge.err || merge.out}`,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
if (git(path, ["diff", "--cached", "--name-only"]).out.length > 0) {
|
|
141
|
+
const commit = git(path, ["commit", "-m", message]);
|
|
142
|
+
if (!commit.ok) {
|
|
143
|
+
return {
|
|
144
|
+
kind: "failed",
|
|
145
|
+
message: `cannot commit the squashed change: ${commit.err || commit.out}`,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const sha = headOf(path);
|
|
150
|
+
return sha === undefined
|
|
151
|
+
? { kind: "failed", message: "the candidate has no commit" }
|
|
152
|
+
: { kind: "ok", sha };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const merge = git(path, ["merge", "--no-ff", "-m", message, verified]);
|
|
156
|
+
if (!merge.ok) {
|
|
157
|
+
const files = conflictedFiles(path);
|
|
158
|
+
if (files.length > 0 || mergeInProgress(path)) {
|
|
159
|
+
return { kind: "conflict", files };
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
kind: "failed",
|
|
163
|
+
message: `cannot merge ${verified}: ${merge.err || merge.out}`,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
const sha = headOf(path);
|
|
167
|
+
return sha === undefined
|
|
168
|
+
? { kind: "failed", message: "the candidate has no commit" }
|
|
169
|
+
: { kind: "ok", sha };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Leaves the gate worktree clean after a conflict, before it is recreated. */
|
|
173
|
+
function clearConflict(path: string): void {
|
|
174
|
+
if (mergeInProgress(path)) git(path, ["merge", "--abort"]);
|
|
175
|
+
git(path, ["reset", "--hard", "HEAD"]);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
interface SaveOptions {
|
|
179
|
+
readonly phase?: TaskData["phase"] | undefined;
|
|
180
|
+
readonly change?: string | readonly string[] | undefined;
|
|
181
|
+
/** Keep the task marked as integrating: the push has not happened yet. */
|
|
182
|
+
readonly integrating?: boolean | undefined;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async function writeIntegration(
|
|
186
|
+
ctx: StepContext,
|
|
187
|
+
context: TaskContext,
|
|
188
|
+
task: TaskData,
|
|
189
|
+
attempt: TaskAttempt,
|
|
190
|
+
record: IntegrationRecord,
|
|
191
|
+
options: SaveOptions = {},
|
|
192
|
+
): Promise<TaskData | undefined> {
|
|
193
|
+
const brief =
|
|
194
|
+
options.change === undefined
|
|
195
|
+
? task.brief
|
|
196
|
+
: [options.change].flat().reduce((text, line) => appendChange(text, line), task.brief);
|
|
197
|
+
const next: TaskData = {
|
|
198
|
+
...task,
|
|
199
|
+
...(options.phase === undefined ? {} : { phase: options.phase }),
|
|
200
|
+
...(options.phase === "merged" ? followupMark(task, context.settings) : {}),
|
|
201
|
+
run: ctx.runId,
|
|
202
|
+
attempts: Math.max(task.attempts, attempt.number),
|
|
203
|
+
attempt: {
|
|
204
|
+
...attempt,
|
|
205
|
+
run: ctx.runId,
|
|
206
|
+
integration: record,
|
|
207
|
+
...(options.integrating === true ? { integrating: ctx.runId } : {}),
|
|
208
|
+
},
|
|
209
|
+
brief,
|
|
210
|
+
};
|
|
211
|
+
const written = await saveTaskGuarded(
|
|
212
|
+
ctx,
|
|
213
|
+
context.taskKey,
|
|
214
|
+
next,
|
|
215
|
+
"dev:integrate",
|
|
216
|
+
);
|
|
217
|
+
return written ? next : undefined;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async function refreshPlan(
|
|
221
|
+
ctx: StepContext,
|
|
222
|
+
context: TaskContext,
|
|
223
|
+
): Promise<void> {
|
|
224
|
+
const plan = await getPlan(
|
|
225
|
+
ctx.store,
|
|
226
|
+
storeKey(context.repository.name, context.task.plan),
|
|
227
|
+
);
|
|
228
|
+
await refreshPlanCounts(ctx.store, plan);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function integrationData(
|
|
232
|
+
record: IntegrationRecord,
|
|
233
|
+
attempt: TaskAttempt,
|
|
234
|
+
extra: Readonly<Record<string, unknown>> = {},
|
|
235
|
+
): Record<string, unknown> {
|
|
236
|
+
return {
|
|
237
|
+
attempt: attempt.number,
|
|
238
|
+
result: record.result,
|
|
239
|
+
...(record.verified === undefined ? {} : { verified: record.verified }),
|
|
240
|
+
...(record.candidate === undefined ? {} : { candidate: record.candidate }),
|
|
241
|
+
...(record.base === undefined ? {} : { base: record.base }),
|
|
242
|
+
...(record.head === undefined ? {} : { head: record.head }),
|
|
243
|
+
...(record.headMoved === undefined ? {} : { headMoved: record.headMoved }),
|
|
244
|
+
...(record.pushed === undefined ? {} : { pushed: record.pushed }),
|
|
245
|
+
...(record.message === undefined ? {} : { message: record.message }),
|
|
246
|
+
...(record.command === undefined ? {} : { command: record.command }),
|
|
247
|
+
...(record.log === undefined ? {} : { log: record.log }),
|
|
248
|
+
...(record.lineNumber === undefined
|
|
249
|
+
? {}
|
|
250
|
+
: { lineNumber: record.lineNumber }),
|
|
251
|
+
...(record.files === undefined ? {} : { files: record.files }),
|
|
252
|
+
...(typeof record.checkNote === "string" ? { note: record.checkNote } : {}),
|
|
253
|
+
...(isRecord(record.mainClone) ? { mainClone: record.mainClone } : {}),
|
|
254
|
+
...extra,
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** Step `dev/integrate`: land the verified commit on main. */
|
|
259
|
+
function treeOf(cwd: string, commit: string): string | undefined {
|
|
260
|
+
const result = git(cwd, ["rev-parse", "--verify", "--quiet", `${commit}^{tree}`]);
|
|
261
|
+
return result.ok && result.out.length > 0 ? result.out : undefined;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** The patch id of a diff, which ignores line numbers and whitespace. */
|
|
265
|
+
function patchIdOf(cwd: string, args: readonly string[]): string | undefined {
|
|
266
|
+
const diff = git(cwd, args, { raw: true });
|
|
267
|
+
if (!diff.ok || diff.out.length === 0) return undefined;
|
|
268
|
+
const id = git(cwd, ["patch-id", "--stable"], { input: diff.out });
|
|
269
|
+
const first = id.out.split(/\s+/)[0];
|
|
270
|
+
return id.ok && first !== undefined && first.length > 0 ? first : undefined;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* The commit on `main` that carries the change of `verified`, found by patch
|
|
275
|
+
* id, so a squash or a merge made under another message is still recognised.
|
|
276
|
+
*/
|
|
277
|
+
function carrierOnMain(cwd: string, verified: string, main: string): string | undefined {
|
|
278
|
+
// A verified commit that main contains arrived in the first commit of main's
|
|
279
|
+
// own line that has it.
|
|
280
|
+
if (git(cwd, ["merge-base", "--is-ancestor", verified, main]).ok) {
|
|
281
|
+
const arrived = git(cwd, [
|
|
282
|
+
"rev-list",
|
|
283
|
+
"--first-parent",
|
|
284
|
+
"--ancestry-path",
|
|
285
|
+
"--reverse",
|
|
286
|
+
`${verified}..${main}`,
|
|
287
|
+
]);
|
|
288
|
+
return arrived.ok ? arrived.out.split("\n")[0] || undefined : undefined;
|
|
289
|
+
}
|
|
290
|
+
const base = git(cwd, ["merge-base", verified, main]);
|
|
291
|
+
if (!base.ok || base.out.length === 0) return undefined;
|
|
292
|
+
const want = patchIdOf(cwd, ["diff", base.out, verified]);
|
|
293
|
+
if (want === undefined) return undefined;
|
|
294
|
+
const recent = git(cwd, ["rev-list", "--first-parent", "--max-count=200", main]);
|
|
295
|
+
if (!recent.ok) return undefined;
|
|
296
|
+
for (const commit of recent.out.split("\n")) {
|
|
297
|
+
if (commit.length === 0) continue;
|
|
298
|
+
if (patchIdOf(cwd, ["diff", `${commit}^`, commit]) === want) return commit;
|
|
299
|
+
}
|
|
300
|
+
return undefined;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export async function runIntegrate(
|
|
304
|
+
ctx: StepContext,
|
|
305
|
+
): Promise<PluginStepResult> {
|
|
306
|
+
const context = await taskContext(ctx);
|
|
307
|
+
if (!claimAllows(ctx, context.task)) return cancelledStep();
|
|
308
|
+
const task = context.task;
|
|
309
|
+
const settings = context.settings;
|
|
310
|
+
// Every result this step writes ends the "integrating" mark; only the start
|
|
311
|
+
// and the pending record, written before the push, set it.
|
|
312
|
+
const attempt: TaskAttempt = {
|
|
313
|
+
...(task.attempt ?? {
|
|
314
|
+
number: Math.max(task.attempts, 0) + 1,
|
|
315
|
+
run: ctx.runId,
|
|
316
|
+
}),
|
|
317
|
+
integrating: null,
|
|
318
|
+
integrationWait: null,
|
|
319
|
+
};
|
|
320
|
+
const verified = task.attempt?.verified;
|
|
321
|
+
const repository = context.repository.path;
|
|
322
|
+
const gate = gateWorktreePath(
|
|
323
|
+
repository,
|
|
324
|
+
settings,
|
|
325
|
+
context.taskId,
|
|
326
|
+
);
|
|
327
|
+
const head = branchHead(repository, settings, context.workName);
|
|
328
|
+
const mainRef = `refs/remotes/${settings.remote}/${settings.mainBranch}`;
|
|
329
|
+
const keyCommands = settings.integration.keyCommands;
|
|
330
|
+
const runStep = ctx.runStep;
|
|
331
|
+
|
|
332
|
+
const fail = async (
|
|
333
|
+
message: string,
|
|
334
|
+
extra: Readonly<Record<string, unknown>> = {},
|
|
335
|
+
): Promise<PluginStepResult> => {
|
|
336
|
+
const record: IntegrationRecord = {
|
|
337
|
+
result: "failed",
|
|
338
|
+
run: ctx.runId,
|
|
339
|
+
style: settings.integration.style,
|
|
340
|
+
...(typeof verified === "string" ? { verified } : {}),
|
|
341
|
+
...(head === undefined ? {} : { head }),
|
|
342
|
+
...(verified === undefined ? {} : { headMoved: head !== verified }),
|
|
343
|
+
message,
|
|
344
|
+
};
|
|
345
|
+
if (
|
|
346
|
+
(await writeIntegration(ctx, context, task, attempt, record, {
|
|
347
|
+
phase: "waiting",
|
|
348
|
+
})) === undefined
|
|
349
|
+
) {
|
|
350
|
+
return cancelledStep();
|
|
351
|
+
}
|
|
352
|
+
return {
|
|
353
|
+
result: "failed",
|
|
354
|
+
data: integrationData(record, attempt, { message, ...extra }),
|
|
355
|
+
};
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
let flakyReason: string | undefined;
|
|
359
|
+
/** The other tasks this task was merged with in one batch. */
|
|
360
|
+
let batchWith: readonly string[] = [];
|
|
361
|
+
const recordMerged = async (
|
|
362
|
+
record: IntegrationRecord,
|
|
363
|
+
sha: string,
|
|
364
|
+
extra: Readonly<Record<string, unknown>> = {},
|
|
365
|
+
cloneSha: string = sha,
|
|
366
|
+
mergedLine: string = `merged as ${shortSha(sha)}`,
|
|
367
|
+
): Promise<PluginStepResult> => {
|
|
368
|
+
const note = record.checkNote;
|
|
369
|
+
const clone = await followMain(
|
|
370
|
+
context.repository,
|
|
371
|
+
settings.mainBranch,
|
|
372
|
+
cloneSha,
|
|
373
|
+
{ lines: settings.prepare, run: mainCloneRunner(ctx, settings) },
|
|
374
|
+
);
|
|
375
|
+
integrateTestHooks.at?.("clone-followed");
|
|
376
|
+
const cloneChange = mainCloneChange(clone);
|
|
377
|
+
const withClone: IntegrationRecord = { ...record, mainClone: { ...clone } };
|
|
378
|
+
if (
|
|
379
|
+
(await writeIntegration(ctx, context, task, attempt, withClone, {
|
|
380
|
+
phase: "merged",
|
|
381
|
+
change: [
|
|
382
|
+
...(typeof note === "string" ? [note] : []),
|
|
383
|
+
...(batchWith.length === 0
|
|
384
|
+
? []
|
|
385
|
+
: [`integrated in a batch with ${batchWith.join(", ")}`]),
|
|
386
|
+
mergedLine,
|
|
387
|
+
...(cloneChange === undefined ? [] : [cloneChange]),
|
|
388
|
+
],
|
|
389
|
+
})) === undefined
|
|
390
|
+
) {
|
|
391
|
+
return cancelledStep();
|
|
392
|
+
}
|
|
393
|
+
integrateTestHooks.at?.("merged-saved");
|
|
394
|
+
await refreshPlan(ctx, context);
|
|
395
|
+
return {
|
|
396
|
+
result: "merged",
|
|
397
|
+
data: integrationData(withClone, attempt, {
|
|
398
|
+
...extra,
|
|
399
|
+
...(batchWith.length === 0 ? {} : { batch: batchWith }),
|
|
400
|
+
...(flakyReason === undefined ? {} : { reason: flakyReason }),
|
|
401
|
+
}),
|
|
402
|
+
};
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
if (typeof verified !== "string" || verified.length === 0) {
|
|
406
|
+
return fail("the task has no verified commit to integrate");
|
|
407
|
+
}
|
|
408
|
+
const headMoved = head !== undefined && head !== verified;
|
|
409
|
+
const message = `Merge work/${context.workName} (${shortSha(verified)})`;
|
|
410
|
+
|
|
411
|
+
// A replay after the push must not push again. The candidate saved before the
|
|
412
|
+
// push and the remote's main decide whether the push already landed.
|
|
413
|
+
const previous = task.attempt?.integration;
|
|
414
|
+
if (
|
|
415
|
+
previous !== undefined &&
|
|
416
|
+
previous !== null &&
|
|
417
|
+
previous.run === ctx.runId &&
|
|
418
|
+
typeof previous.candidate === "string" &&
|
|
419
|
+
previous.candidate.length > 0
|
|
420
|
+
) {
|
|
421
|
+
const fetched = git(repository, ["fetch", "--prune", settings.remote]);
|
|
422
|
+
const remoteMain = revParse(repository, mainRef);
|
|
423
|
+
if (
|
|
424
|
+
fetched.ok &&
|
|
425
|
+
remoteMain !== undefined &&
|
|
426
|
+
contains(repository, previous.candidate, remoteMain)
|
|
427
|
+
) {
|
|
428
|
+
const record: IntegrationRecord = {
|
|
429
|
+
...previous,
|
|
430
|
+
result: "merged",
|
|
431
|
+
run: ctx.runId,
|
|
432
|
+
verified,
|
|
433
|
+
candidate: previous.candidate,
|
|
434
|
+
head: head ?? previous.head ?? null,
|
|
435
|
+
headMoved,
|
|
436
|
+
pushed: true,
|
|
437
|
+
style: settings.integration.style,
|
|
438
|
+
};
|
|
439
|
+
// A batch's leader saved the batch's head and its other tasks with the
|
|
440
|
+
// candidate: the clone follows the head, and the brief says the batch.
|
|
441
|
+
const previousBatch = Array.isArray(previous.batch)
|
|
442
|
+
? previous.batch.filter((name): name is string => typeof name === "string")
|
|
443
|
+
: [];
|
|
444
|
+
const clone = await followMain(
|
|
445
|
+
context.repository,
|
|
446
|
+
settings.mainBranch,
|
|
447
|
+
typeof previous.batchHead === "string" ? previous.batchHead : previous.candidate,
|
|
448
|
+
{ lines: settings.prepare, run: mainCloneRunner(ctx, settings) },
|
|
449
|
+
);
|
|
450
|
+
const cloneChange = mainCloneChange(clone);
|
|
451
|
+
const withClone: IntegrationRecord = {
|
|
452
|
+
...record,
|
|
453
|
+
mainClone: { ...clone },
|
|
454
|
+
};
|
|
455
|
+
if (
|
|
456
|
+
(await writeIntegration(ctx, context, task, attempt, withClone, {
|
|
457
|
+
phase: "merged",
|
|
458
|
+
// A kill after the merge was recorded leaves its lines in the brief;
|
|
459
|
+
// say them once.
|
|
460
|
+
change:
|
|
461
|
+
previous.result === "merged"
|
|
462
|
+
? []
|
|
463
|
+
: [
|
|
464
|
+
...(typeof previous.checkNote === "string"
|
|
465
|
+
? [previous.checkNote]
|
|
466
|
+
: []),
|
|
467
|
+
...(previousBatch.length === 0
|
|
468
|
+
? []
|
|
469
|
+
: [`integrated in a batch with ${previousBatch.join(", ")}`]),
|
|
470
|
+
`merged as ${shortSha(previous.candidate)}`,
|
|
471
|
+
...(cloneChange === undefined ? [] : [cloneChange]),
|
|
472
|
+
],
|
|
473
|
+
})) === undefined
|
|
474
|
+
) {
|
|
475
|
+
return cancelledStep();
|
|
476
|
+
}
|
|
477
|
+
await refreshPlan(ctx, context);
|
|
478
|
+
return {
|
|
479
|
+
result: "merged",
|
|
480
|
+
data: integrationData(withClone, attempt, { replayed: true }),
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const queue = new IntegrationQueue({
|
|
486
|
+
home: ctx.home,
|
|
487
|
+
repository: context.repository,
|
|
488
|
+
task: context.taskId,
|
|
489
|
+
run: ctx.runId,
|
|
490
|
+
verified,
|
|
491
|
+
message,
|
|
492
|
+
signal: ctx.signal,
|
|
493
|
+
wait: integrateTestHooks.wait,
|
|
494
|
+
landed: (result) => {
|
|
495
|
+
if (!git(repository, ["fetch", "--prune", settings.remote]).ok) return false;
|
|
496
|
+
const main = revParse(repository, mainRef);
|
|
497
|
+
return main !== undefined && contains(repository, result.head, main);
|
|
498
|
+
},
|
|
499
|
+
say: async (line, holder) => {
|
|
500
|
+
await ctx.logger.write(line);
|
|
501
|
+
const saved = await saveTaskGuarded(
|
|
502
|
+
ctx,
|
|
503
|
+
context.taskKey,
|
|
504
|
+
{
|
|
505
|
+
...task,
|
|
506
|
+
attempt: {
|
|
507
|
+
...attempt,
|
|
508
|
+
integrating: ctx.runId,
|
|
509
|
+
integrationWait: `${holder} is integrating`,
|
|
510
|
+
},
|
|
511
|
+
},
|
|
512
|
+
"dev:integrate",
|
|
513
|
+
);
|
|
514
|
+
if (!saved) throw new IntegrationQueueCancelled();
|
|
515
|
+
},
|
|
516
|
+
});
|
|
517
|
+
await queue.join();
|
|
518
|
+
integrateTestHooks.queued?.(context.taskId);
|
|
519
|
+
|
|
520
|
+
/** This task's merge commit when main already has it, and the main that has it. */
|
|
521
|
+
const alreadyOnMain = (): { sha: string; main: string } | undefined => {
|
|
522
|
+
if (!git(repository, ["fetch", "--prune", settings.remote]).ok) return undefined;
|
|
523
|
+
const main = revParse(repository, mainRef);
|
|
524
|
+
if (main === undefined) return undefined;
|
|
525
|
+
const found = git(repository, [
|
|
526
|
+
"log",
|
|
527
|
+
"--first-parent",
|
|
528
|
+
"--fixed-strings",
|
|
529
|
+
`--grep=${message}`,
|
|
530
|
+
"--format=%H%x09%s",
|
|
531
|
+
main,
|
|
532
|
+
]);
|
|
533
|
+
if (!found.ok) return undefined;
|
|
534
|
+
for (const line of found.out.split("\n")) {
|
|
535
|
+
const [sha, subject] = line.split("\t");
|
|
536
|
+
if (sha !== undefined && subject === message) return { sha, main };
|
|
537
|
+
}
|
|
538
|
+
return undefined;
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
/** Lands the tasks of a batch with one candidate; undefined means fall back to one by one. */
|
|
542
|
+
const landBatch = async (
|
|
543
|
+
followers: readonly Ticket[],
|
|
544
|
+
): Promise<PluginStepResult | undefined> => {
|
|
545
|
+
const entries = [
|
|
546
|
+
{ task: context.taskId, verified, message },
|
|
547
|
+
...followers.map((ticket) => ({
|
|
548
|
+
task: ticket.task,
|
|
549
|
+
verified: ticket.verified,
|
|
550
|
+
message: ticket.message,
|
|
551
|
+
})),
|
|
552
|
+
];
|
|
553
|
+
for (let round = 0; round < MAX_INTEGRATION_ROUNDS; round += 1) {
|
|
554
|
+
const fetched = git(repository, ["fetch", "--prune", settings.remote]);
|
|
555
|
+
if (!fetched.ok) return undefined;
|
|
556
|
+
const base = revParse(repository, mainRef);
|
|
557
|
+
if (base === undefined) return undefined;
|
|
558
|
+
if (recreateGateWorktree(repository, gate, base, settings) !== undefined) {
|
|
559
|
+
return undefined;
|
|
560
|
+
}
|
|
561
|
+
const members: { task: string; sha: string }[] = [];
|
|
562
|
+
let treeBefore = treeOf(gate, "HEAD");
|
|
563
|
+
for (const entry of entries) {
|
|
564
|
+
const built = mergeCandidate(
|
|
565
|
+
gate,
|
|
566
|
+
entry.verified,
|
|
567
|
+
settings.integration.style,
|
|
568
|
+
entry.message,
|
|
569
|
+
);
|
|
570
|
+
if (built.kind === "conflict") clearConflict(gate);
|
|
571
|
+
if (built.kind !== "ok" || built.sha === undefined) return undefined;
|
|
572
|
+
// A member with nothing left to merge is already on main: one by one.
|
|
573
|
+
if (treeOf(gate, built.sha) === treeBefore) return undefined;
|
|
574
|
+
treeBefore = treeOf(gate, built.sha);
|
|
575
|
+
members.push({ task: entry.task, sha: built.sha });
|
|
576
|
+
}
|
|
577
|
+
const batchHead = headOf(gate);
|
|
578
|
+
const own = members[0];
|
|
579
|
+
if (batchHead === undefined || own === undefined) return undefined;
|
|
580
|
+
|
|
581
|
+
let checkNote: string | undefined;
|
|
582
|
+
if (keyCommands.length > 0) {
|
|
583
|
+
if ((await runPrepare(ctx, settings, gate)) !== undefined) return undefined;
|
|
584
|
+
if (runStep === undefined) return undefined;
|
|
585
|
+
const steady = await runCheckSteadily({
|
|
586
|
+
run: () =>
|
|
587
|
+
runCheckCommands(
|
|
588
|
+
runStep,
|
|
589
|
+
{
|
|
590
|
+
cwd: gate,
|
|
591
|
+
...(settings.gate.stallSecs === undefined
|
|
592
|
+
? {}
|
|
593
|
+
: { stallSecs: settings.gate.stallSecs }),
|
|
594
|
+
},
|
|
595
|
+
keyCommands,
|
|
596
|
+
stepOptions(ctx, gate),
|
|
597
|
+
settings.check.rerun,
|
|
598
|
+
checkPlaces(
|
|
599
|
+
ctx,
|
|
600
|
+
context.repository,
|
|
601
|
+
settings.gate.places,
|
|
602
|
+
context.taskKey,
|
|
603
|
+
"integration",
|
|
604
|
+
),
|
|
605
|
+
),
|
|
606
|
+
limit: settings.check.loadLimit,
|
|
607
|
+
flakyLimit: settings.check.flakyLoadLimit,
|
|
608
|
+
signal: ctx.signal,
|
|
609
|
+
log: (text) => ctx.logger.write(text),
|
|
610
|
+
});
|
|
611
|
+
if (steady.cancelled === true) return cancelledStep();
|
|
612
|
+
const { checked } = steady;
|
|
613
|
+
await recordRerunPasses(ctx.store, checked.passedOnRerun);
|
|
614
|
+
if (steady.notCode !== undefined) {
|
|
615
|
+
// The batch's candidate was not accepted: the leader waits for a
|
|
616
|
+
// person, and the tasks it claimed integrate alone.
|
|
617
|
+
const data =
|
|
618
|
+
checked.outcome !== undefined && isRecord(checked.outcome.data)
|
|
619
|
+
? checked.outcome.data
|
|
620
|
+
: {};
|
|
621
|
+
const command = typeof data.line === "string" ? data.line : undefined;
|
|
622
|
+
const log = typeof data.log === "string" ? data.log : undefined;
|
|
623
|
+
const lineNumber =
|
|
624
|
+
typeof data.lineNumber === "number" ? data.lineNumber : undefined;
|
|
625
|
+
const detail =
|
|
626
|
+
checked.hostError ??
|
|
627
|
+
(typeof data.message === "string"
|
|
628
|
+
? data.message
|
|
629
|
+
: "the integration checks failed");
|
|
630
|
+
const record: IntegrationRecord = {
|
|
631
|
+
result: "failed",
|
|
632
|
+
run: ctx.runId,
|
|
633
|
+
style: settings.integration.style,
|
|
634
|
+
verified,
|
|
635
|
+
candidate: own.sha,
|
|
636
|
+
base,
|
|
637
|
+
head: head ?? null,
|
|
638
|
+
headMoved,
|
|
639
|
+
...(command === undefined ? {} : { command }),
|
|
640
|
+
...(log === undefined ? {} : { log }),
|
|
641
|
+
...(lineNumber === undefined ? {} : { lineNumber }),
|
|
642
|
+
message: `${steady.notCode.cause}; ${detail}`,
|
|
643
|
+
notCode: steady.notCode.cause,
|
|
644
|
+
};
|
|
645
|
+
queue.fallBack(followers);
|
|
646
|
+
if (
|
|
647
|
+
(await writeIntegration(ctx, context, task, attempt, record, {
|
|
648
|
+
phase: "waiting",
|
|
649
|
+
})) === undefined
|
|
650
|
+
) {
|
|
651
|
+
return cancelledStep();
|
|
652
|
+
}
|
|
653
|
+
return {
|
|
654
|
+
result: "failed",
|
|
655
|
+
data: integrationData(record, attempt, { message: record.message }),
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
if (checked.hostError !== undefined || checked.outcome?.result !== "pass") {
|
|
659
|
+
return undefined;
|
|
660
|
+
}
|
|
661
|
+
const flakyNotice = await reportFlakyTests({
|
|
662
|
+
ctx,
|
|
663
|
+
repository: context.repository,
|
|
664
|
+
taskId: context.taskId,
|
|
665
|
+
threshold: context.settings.check.flakyTask,
|
|
666
|
+
flakyPlan: context.settings.check.flakyPlan,
|
|
667
|
+
check: checked,
|
|
668
|
+
loads: steady.loads,
|
|
669
|
+
loadLimit: settings.check.flakyLoadLimit,
|
|
670
|
+
});
|
|
671
|
+
checkNote =
|
|
672
|
+
flakyNotice === undefined
|
|
673
|
+
? checked.note
|
|
674
|
+
: [checked.note, flakyNotice].filter((part) => part !== undefined).join("; ");
|
|
675
|
+
flakyReason = flakyNotice;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
const pending: IntegrationRecord = {
|
|
679
|
+
result: "pending",
|
|
680
|
+
run: ctx.runId,
|
|
681
|
+
style: settings.integration.style,
|
|
682
|
+
verified,
|
|
683
|
+
candidate: own.sha,
|
|
684
|
+
base,
|
|
685
|
+
head: head ?? null,
|
|
686
|
+
headMoved,
|
|
687
|
+
pushed: false,
|
|
688
|
+
batchHead,
|
|
689
|
+
batch: members.filter((member) => member.task !== context.taskId).map((member) => member.task),
|
|
690
|
+
...(checkNote === undefined ? {} : { checkNote }),
|
|
691
|
+
};
|
|
692
|
+
if ((await writeIntegration(ctx, context, task, attempt, pending, {
|
|
693
|
+
integrating: true,
|
|
694
|
+
})) === undefined) {
|
|
695
|
+
return cancelledStep();
|
|
696
|
+
}
|
|
697
|
+
// The result is left before the push, pending: a leader that dies after the
|
|
698
|
+
// push leaves what its followers need to recognise their merges on main.
|
|
699
|
+
const result: BatchResult = {
|
|
700
|
+
base,
|
|
701
|
+
head: batchHead,
|
|
702
|
+
...(checkNote === undefined ? {} : { note: checkNote }),
|
|
703
|
+
members,
|
|
704
|
+
};
|
|
705
|
+
const land = async (extra: Readonly<Record<string, unknown>>): Promise<PluginStepResult> => {
|
|
706
|
+
batchWith = members.filter((member) => member.task !== context.taskId).map((member) => member.task);
|
|
707
|
+
const merged = await recordMerged({ ...pending, result: "merged", pushed: true }, own.sha, extra, batchHead);
|
|
708
|
+
queue.finish();
|
|
709
|
+
return merged;
|
|
710
|
+
};
|
|
711
|
+
// A claimed task that left (cancelled) is not merged: one by one instead.
|
|
712
|
+
// Checked last, so the window between this and the push is as short as it can be.
|
|
713
|
+
if (queue.stillQueued(followers).length !== followers.length) return undefined;
|
|
714
|
+
queue.publish(result);
|
|
715
|
+
const push = git(gate, ["push", settings.remote, `HEAD:refs/heads/${settings.mainBranch}`]);
|
|
716
|
+
if (push.ok) {
|
|
717
|
+
integrateTestHooks.afterPush?.(batchHead);
|
|
718
|
+
return land({});
|
|
719
|
+
}
|
|
720
|
+
git(repository, ["fetch", "--prune", settings.remote]);
|
|
721
|
+
const now = revParse(repository, mainRef);
|
|
722
|
+
if (now !== undefined && contains(repository, batchHead, now)) {
|
|
723
|
+
return land({ landed: true });
|
|
724
|
+
}
|
|
725
|
+
if (now !== undefined && now !== base) continue;
|
|
726
|
+
return undefined;
|
|
727
|
+
}
|
|
728
|
+
return undefined;
|
|
729
|
+
};
|
|
730
|
+
|
|
731
|
+
/** Records the merge a batch's leader made for this task. */
|
|
732
|
+
const recordBatched = async (
|
|
733
|
+
leaderResult: BatchResult,
|
|
734
|
+
): Promise<PluginStepResult> => {
|
|
735
|
+
const mine = leaderResult.members.find((member) => member.task === context.taskId);
|
|
736
|
+
if (mine === undefined) return fail("the batch that merged this task does not list it");
|
|
737
|
+
batchWith = leaderResult.members
|
|
738
|
+
.filter((member) => member.task !== context.taskId)
|
|
739
|
+
.map((member) => member.task);
|
|
740
|
+
const record: IntegrationRecord = {
|
|
741
|
+
result: "merged",
|
|
742
|
+
run: ctx.runId,
|
|
743
|
+
style: settings.integration.style,
|
|
744
|
+
verified,
|
|
745
|
+
candidate: mine.sha,
|
|
746
|
+
base: leaderResult.base,
|
|
747
|
+
head: head ?? null,
|
|
748
|
+
headMoved,
|
|
749
|
+
pushed: true,
|
|
750
|
+
...(leaderResult.note === undefined ? {} : { checkNote: leaderResult.note }),
|
|
751
|
+
};
|
|
752
|
+
return recordMerged(record, mine.sha, {}, leaderResult.head);
|
|
753
|
+
};
|
|
754
|
+
|
|
755
|
+
try {
|
|
756
|
+
let turn: Turn;
|
|
757
|
+
try {
|
|
758
|
+
turn = await queue.turn();
|
|
759
|
+
} catch (error) {
|
|
760
|
+
if (error instanceof IntegrationQueueCancelled) return cancelledStep();
|
|
761
|
+
throw error;
|
|
762
|
+
}
|
|
763
|
+
if (turn.kind === "batched") return await recordBatched(turn.result);
|
|
764
|
+
await integrateTestHooks.turnTaken?.(context.taskId);
|
|
765
|
+
|
|
766
|
+
// A leader that died after its push left this task's merge on main, and the
|
|
767
|
+
// task's own record knows nothing of it: find the merge there instead of
|
|
768
|
+
// merging the same commit again into an empty candidate.
|
|
769
|
+
const landed = alreadyOnMain();
|
|
770
|
+
if (landed !== undefined) {
|
|
771
|
+
// The dead leader's note and batch are gone; main still has the base.
|
|
772
|
+
const landedBase = revParse(repository, `${landed.sha}^1`);
|
|
773
|
+
return recordMerged(
|
|
774
|
+
{
|
|
775
|
+
result: "merged",
|
|
776
|
+
run: ctx.runId,
|
|
777
|
+
style: settings.integration.style,
|
|
778
|
+
verified,
|
|
779
|
+
candidate: landed.sha,
|
|
780
|
+
...(landedBase === undefined ? {} : { base: landedBase }),
|
|
781
|
+
head: head ?? null,
|
|
782
|
+
headMoved,
|
|
783
|
+
pushed: true,
|
|
784
|
+
},
|
|
785
|
+
landed.sha,
|
|
786
|
+
{ replayed: true },
|
|
787
|
+
landed.main,
|
|
788
|
+
);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
// From here the step builds and checks the candidate: the row says integrating.
|
|
792
|
+
if (
|
|
793
|
+
!(await saveTaskGuarded(
|
|
794
|
+
ctx,
|
|
795
|
+
context.taskKey,
|
|
796
|
+
{ ...task, attempt: { ...attempt, integrating: ctx.runId } },
|
|
797
|
+
"dev:integrate",
|
|
798
|
+
))
|
|
799
|
+
) {
|
|
800
|
+
return cancelledStep();
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
const followers = queue.claim(settings.integration.batch);
|
|
804
|
+
if (followers.length > 0) {
|
|
805
|
+
await integrateTestHooks.claimed?.(followers.map((ticket) => ticket.task));
|
|
806
|
+
const landed = await landBatch(followers);
|
|
807
|
+
if (landed !== undefined) return landed;
|
|
808
|
+
queue.fallBack(followers);
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
let lastError = "";
|
|
812
|
+
for (let round = 0; round < MAX_INTEGRATION_ROUNDS; round += 1) {
|
|
813
|
+
const fetched = git(repository, ["fetch", "--prune", settings.remote]);
|
|
814
|
+
if (!fetched.ok) {
|
|
815
|
+
return fail(
|
|
816
|
+
`cannot fetch "${settings.remote}": ${fetched.err || fetched.out || "git fetch failed"}`,
|
|
817
|
+
);
|
|
818
|
+
}
|
|
819
|
+
const base = revParse(repository, mainRef);
|
|
820
|
+
if (base === undefined) {
|
|
821
|
+
return fail(`"${settings.remote}/${settings.mainBranch}" does not exist after the fetch`);
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
const prepared = recreateGateWorktree(repository, gate, base, settings);
|
|
825
|
+
if (prepared !== undefined) return fail(prepared);
|
|
826
|
+
|
|
827
|
+
const candidate = mergeCandidate(
|
|
828
|
+
gate,
|
|
829
|
+
verified,
|
|
830
|
+
settings.integration.style,
|
|
831
|
+
message,
|
|
832
|
+
);
|
|
833
|
+
if (candidate.kind === "conflict") {
|
|
834
|
+
clearConflict(gate);
|
|
835
|
+
const files = candidate.files ?? [];
|
|
836
|
+
const text = [
|
|
837
|
+
files.length === 0
|
|
838
|
+
? "The verified commit conflicts with the latest main."
|
|
839
|
+
: [
|
|
840
|
+
"The verified commit conflicts with the latest main in these files:",
|
|
841
|
+
...files.map((file) => `- ${file}`),
|
|
842
|
+
].join("\n"),
|
|
843
|
+
"Merge the latest main into the task branch, resolve the conflict, and continue.",
|
|
844
|
+
].join("\n\n");
|
|
845
|
+
const withRework = appendRework(
|
|
846
|
+
task,
|
|
847
|
+
"conflict with main",
|
|
848
|
+
text,
|
|
849
|
+
files.length === 0
|
|
850
|
+
? "the verified commit conflicts with the latest main"
|
|
851
|
+
: `conflicts with the latest main in ${files.join(", ")}`,
|
|
852
|
+
);
|
|
853
|
+
const record: IntegrationRecord = {
|
|
854
|
+
result: "conflict",
|
|
855
|
+
run: ctx.runId,
|
|
856
|
+
style: settings.integration.style,
|
|
857
|
+
verified,
|
|
858
|
+
base,
|
|
859
|
+
head: head ?? null,
|
|
860
|
+
headMoved,
|
|
861
|
+
files,
|
|
862
|
+
};
|
|
863
|
+
const written = await saveTaskGuarded(
|
|
864
|
+
ctx,
|
|
865
|
+
context.taskKey,
|
|
866
|
+
{
|
|
867
|
+
...withRework,
|
|
868
|
+
reviewFailed: undefined,
|
|
869
|
+
phase: "running",
|
|
870
|
+
run: ctx.runId,
|
|
871
|
+
attempts: Math.max(task.attempts, attempt.number),
|
|
872
|
+
attempt: { ...attempt, run: ctx.runId, integration: record },
|
|
873
|
+
},
|
|
874
|
+
"dev:integrate",
|
|
875
|
+
);
|
|
876
|
+
if (!written) return cancelledStep();
|
|
877
|
+
return {
|
|
878
|
+
result: "conflict",
|
|
879
|
+
data: integrationData(record, attempt, { files, rework: true }),
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
if (candidate.kind === "failed" || candidate.sha === undefined) {
|
|
883
|
+
return fail(candidate.message ?? "cannot build the candidate");
|
|
884
|
+
}
|
|
885
|
+
const sha = candidate.sha;
|
|
886
|
+
integrateTestHooks.at?.("candidate-built");
|
|
887
|
+
// A candidate with main's own tree has nothing left to merge: the change
|
|
888
|
+
// is already there, in whatever commit carries it. Nothing is pushed and
|
|
889
|
+
// no commit is claimed as this task's.
|
|
890
|
+
if (treeOf(gate, sha) === treeOf(repository, base)) {
|
|
891
|
+
const carrier = carrierOnMain(repository, verified, base);
|
|
892
|
+
return recordMerged(
|
|
893
|
+
{
|
|
894
|
+
result: "merged",
|
|
895
|
+
run: ctx.runId,
|
|
896
|
+
style: settings.integration.style,
|
|
897
|
+
verified,
|
|
898
|
+
candidate: carrier ?? base,
|
|
899
|
+
base,
|
|
900
|
+
head: head ?? null,
|
|
901
|
+
headMoved,
|
|
902
|
+
pushed: true,
|
|
903
|
+
},
|
|
904
|
+
carrier ?? base,
|
|
905
|
+
{ alreadyOnMain: true },
|
|
906
|
+
base,
|
|
907
|
+
carrier === undefined ? "already on main" : `already on main as ${shortSha(carrier)}`,
|
|
908
|
+
);
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
let checkNote: string | undefined;
|
|
912
|
+
if (keyCommands.length > 0) {
|
|
913
|
+
const prepareFailure = await runPrepare(ctx, settings, gate);
|
|
914
|
+
if (prepareFailure !== undefined) {
|
|
915
|
+
return fail(prepareFailure.message, {
|
|
916
|
+
candidate: sha,
|
|
917
|
+
base,
|
|
918
|
+
reason: prepareFailure.reason,
|
|
919
|
+
...(prepareFailure.log === undefined ? {} : { log: prepareFailure.log }),
|
|
920
|
+
});
|
|
921
|
+
}
|
|
922
|
+
if (runStep === undefined) {
|
|
923
|
+
return fail("the kernel did not offer a way to run the integration checks");
|
|
924
|
+
}
|
|
925
|
+
const steady = await runCheckSteadily({
|
|
926
|
+
run: () =>
|
|
927
|
+
runCheckCommands(
|
|
928
|
+
runStep,
|
|
929
|
+
{
|
|
930
|
+
cwd: gate,
|
|
931
|
+
...(settings.gate.stallSecs === undefined
|
|
932
|
+
? {}
|
|
933
|
+
: { stallSecs: settings.gate.stallSecs }),
|
|
934
|
+
},
|
|
935
|
+
keyCommands,
|
|
936
|
+
stepOptions(ctx, gate),
|
|
937
|
+
settings.check.rerun,
|
|
938
|
+
checkPlaces(
|
|
939
|
+
ctx,
|
|
940
|
+
context.repository,
|
|
941
|
+
settings.gate.places,
|
|
942
|
+
context.taskKey,
|
|
943
|
+
"integration",
|
|
944
|
+
),
|
|
945
|
+
),
|
|
946
|
+
limit: settings.check.loadLimit,
|
|
947
|
+
flakyLimit: settings.check.flakyLoadLimit,
|
|
948
|
+
signal: ctx.signal,
|
|
949
|
+
log: (text) => ctx.logger.write(text),
|
|
950
|
+
});
|
|
951
|
+
if (steady.cancelled === true) return cancelledStep();
|
|
952
|
+
const { checked } = steady;
|
|
953
|
+
await recordRerunPasses(ctx.store, checked.passedOnRerun);
|
|
954
|
+
const flakyNotice = await reportFlakyTests({
|
|
955
|
+
ctx,
|
|
956
|
+
repository: context.repository,
|
|
957
|
+
taskId: context.taskId,
|
|
958
|
+
threshold: context.settings.check.flakyTask,
|
|
959
|
+
flakyPlan: context.settings.check.flakyPlan,
|
|
960
|
+
check: checked,
|
|
961
|
+
loads: steady.loads,
|
|
962
|
+
loadLimit: settings.check.flakyLoadLimit,
|
|
963
|
+
});
|
|
964
|
+
const { outcome, hostError } = checked;
|
|
965
|
+
checkNote =
|
|
966
|
+
flakyNotice === undefined
|
|
967
|
+
? checked.note
|
|
968
|
+
: [checked.note, flakyNotice].filter((part) => part !== undefined).join("; ");
|
|
969
|
+
flakyReason = flakyNotice;
|
|
970
|
+
if (hostError !== undefined || outcome?.result !== "pass") {
|
|
971
|
+
const data =
|
|
972
|
+
outcome !== undefined && isRecord(outcome.data) ? outcome.data : {};
|
|
973
|
+
const command =
|
|
974
|
+
typeof data.line === "string" ? data.line : undefined;
|
|
975
|
+
const log = typeof data.log === "string" ? data.log : undefined;
|
|
976
|
+
const lineNumber =
|
|
977
|
+
typeof data.lineNumber === "number" ? data.lineNumber : undefined;
|
|
978
|
+
const detail =
|
|
979
|
+
hostError ??
|
|
980
|
+
(typeof data.message === "string" ? data.message : undefined) ??
|
|
981
|
+
`the integration checks failed (${outcome?.result ?? "unknown"})`;
|
|
982
|
+
if (steady.notCode !== undefined) {
|
|
983
|
+
// Not the code: no rework, a person decides, and the line says why.
|
|
984
|
+
const record: IntegrationRecord = {
|
|
985
|
+
result: "failed",
|
|
986
|
+
run: ctx.runId,
|
|
987
|
+
style: settings.integration.style,
|
|
988
|
+
verified,
|
|
989
|
+
candidate: sha,
|
|
990
|
+
base,
|
|
991
|
+
head: head ?? null,
|
|
992
|
+
headMoved,
|
|
993
|
+
...(command === undefined ? {} : { command }),
|
|
994
|
+
...(log === undefined ? {} : { log }),
|
|
995
|
+
...(lineNumber === undefined ? {} : { lineNumber }),
|
|
996
|
+
message: `${steady.notCode.cause}; ${detail}`,
|
|
997
|
+
notCode: steady.notCode.cause,
|
|
998
|
+
};
|
|
999
|
+
if (
|
|
1000
|
+
(await writeIntegration(ctx, context, task, attempt, record, {
|
|
1001
|
+
phase: "waiting",
|
|
1002
|
+
})) === undefined
|
|
1003
|
+
) {
|
|
1004
|
+
return cancelledStep();
|
|
1005
|
+
}
|
|
1006
|
+
return {
|
|
1007
|
+
result: "failed",
|
|
1008
|
+
data: integrationData(record, attempt, { message: record.message }),
|
|
1009
|
+
};
|
|
1010
|
+
}
|
|
1011
|
+
const pieces = [
|
|
1012
|
+
command === undefined
|
|
1013
|
+
? "An integration check failed."
|
|
1014
|
+
: `The integration check failed: \`${command}\``,
|
|
1015
|
+
...(log === undefined ? [] : [`Log: \`${log}\``]),
|
|
1016
|
+
detail,
|
|
1017
|
+
...(flakyNotice === undefined ? [] : [`Note: ${flakyNotice}`]),
|
|
1018
|
+
];
|
|
1019
|
+
const withRework = appendRework(
|
|
1020
|
+
task,
|
|
1021
|
+
"integration",
|
|
1022
|
+
pieces.join("\n\n"),
|
|
1023
|
+
command === undefined
|
|
1024
|
+
? detail
|
|
1025
|
+
: `the integration check failed: ${command}`,
|
|
1026
|
+
);
|
|
1027
|
+
const record: IntegrationRecord = {
|
|
1028
|
+
result: "fail",
|
|
1029
|
+
run: ctx.runId,
|
|
1030
|
+
style: settings.integration.style,
|
|
1031
|
+
verified,
|
|
1032
|
+
candidate: sha,
|
|
1033
|
+
base,
|
|
1034
|
+
head: head ?? null,
|
|
1035
|
+
headMoved,
|
|
1036
|
+
...(command === undefined ? {} : { command }),
|
|
1037
|
+
...(log === undefined ? {} : { log }),
|
|
1038
|
+
...(lineNumber === undefined ? {} : { lineNumber }),
|
|
1039
|
+
message: flakyNotice === undefined ? detail : `${detail}; ${flakyNotice}`,
|
|
1040
|
+
};
|
|
1041
|
+
const written = await saveTaskGuarded(
|
|
1042
|
+
ctx,
|
|
1043
|
+
context.taskKey,
|
|
1044
|
+
{
|
|
1045
|
+
...withRework,
|
|
1046
|
+
phase: "running",
|
|
1047
|
+
run: ctx.runId,
|
|
1048
|
+
attempts: Math.max(task.attempts, attempt.number),
|
|
1049
|
+
attempt: { ...attempt, run: ctx.runId, integration: record },
|
|
1050
|
+
},
|
|
1051
|
+
"dev:integrate",
|
|
1052
|
+
);
|
|
1053
|
+
if (!written) return cancelledStep();
|
|
1054
|
+
return {
|
|
1055
|
+
result: "fail",
|
|
1056
|
+
data: integrationData(record, attempt, { rework: true }),
|
|
1057
|
+
};
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
// Record the candidate before the push: a crash after the push finds it and
|
|
1062
|
+
// the remote's main, and finishes without pushing again.
|
|
1063
|
+
const pending: IntegrationRecord = {
|
|
1064
|
+
result: "pending",
|
|
1065
|
+
run: ctx.runId,
|
|
1066
|
+
style: settings.integration.style,
|
|
1067
|
+
verified,
|
|
1068
|
+
candidate: sha,
|
|
1069
|
+
base,
|
|
1070
|
+
head: head ?? null,
|
|
1071
|
+
headMoved,
|
|
1072
|
+
pushed: false,
|
|
1073
|
+
...(checkNote === undefined ? {} : { checkNote }),
|
|
1074
|
+
};
|
|
1075
|
+
if ((await writeIntegration(ctx, context, task, attempt, pending, {
|
|
1076
|
+
integrating: true,
|
|
1077
|
+
})) === undefined) {
|
|
1078
|
+
return cancelledStep();
|
|
1079
|
+
}
|
|
1080
|
+
integrateTestHooks.at?.("pending-saved");
|
|
1081
|
+
|
|
1082
|
+
const push = git(gate, [
|
|
1083
|
+
"push",
|
|
1084
|
+
settings.remote,
|
|
1085
|
+
`HEAD:refs/heads/${settings.mainBranch}`,
|
|
1086
|
+
]);
|
|
1087
|
+
if (push.ok) {
|
|
1088
|
+
integrateTestHooks.afterPush?.(sha);
|
|
1089
|
+
const record: IntegrationRecord = {
|
|
1090
|
+
...pending,
|
|
1091
|
+
result: "merged",
|
|
1092
|
+
pushed: true,
|
|
1093
|
+
};
|
|
1094
|
+
return recordMerged(record, sha);
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
lastError = `cannot push "${settings.mainBranch}" to "${settings.remote}": ${push.err || push.out || "git push failed"}`;
|
|
1098
|
+
git(repository, ["fetch", "--prune", settings.remote]);
|
|
1099
|
+
const now = revParse(repository, mainRef);
|
|
1100
|
+
// A push can land on the remote and still report an error (the client lost
|
|
1101
|
+
// the answer). The remote's main then contains the candidate: it is
|
|
1102
|
+
// merged, and building and pushing a second candidate would be wrong.
|
|
1103
|
+
if (now !== undefined && contains(repository, sha, now)) {
|
|
1104
|
+
const record: IntegrationRecord = {
|
|
1105
|
+
...pending,
|
|
1106
|
+
result: "merged",
|
|
1107
|
+
pushed: true,
|
|
1108
|
+
};
|
|
1109
|
+
return recordMerged(record, sha, { landed: true });
|
|
1110
|
+
}
|
|
1111
|
+
// Only a main that moved makes the same candidate worth building again; a
|
|
1112
|
+
// remote that refused the push for another reason is a failure.
|
|
1113
|
+
if (now !== undefined && now !== base) continue;
|
|
1114
|
+
return fail(lastError, { candidate: sha, base, head, headMoved });
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
return fail(
|
|
1118
|
+
`main moved ${MAX_INTEGRATION_ROUNDS} times while the candidate was built; ${lastError}`,
|
|
1119
|
+
);
|
|
1120
|
+
} finally {
|
|
1121
|
+
queue.leave();
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
/** Step `dev/cleanup`: run the after-merge hooks and remove the task's traces. */
|
|
1126
|
+
export async function runCleanup(
|
|
1127
|
+
ctx: StepContext,
|
|
1128
|
+
): Promise<PluginStepResult> {
|
|
1129
|
+
const context = await taskContext(ctx);
|
|
1130
|
+
if (!claimAllows(ctx, context.task)) return cancelledStep();
|
|
1131
|
+
const task = context.task;
|
|
1132
|
+
const settings: DevSettings = context.settings;
|
|
1133
|
+
const attempt = task.attempt ?? {
|
|
1134
|
+
number: Math.max(task.attempts, 0) + 1,
|
|
1135
|
+
run: ctx.runId,
|
|
1136
|
+
};
|
|
1137
|
+
// A task opened on a chat's worktree has none of its own to clean: the chat keeps its worktree.
|
|
1138
|
+
const worktree = worktreePath(
|
|
1139
|
+
context.repository.path,
|
|
1140
|
+
settings,
|
|
1141
|
+
context.taskId,
|
|
1142
|
+
);
|
|
1143
|
+
const env: Record<string, string> = {
|
|
1144
|
+
OPERAD_TASK: context.taskId,
|
|
1145
|
+
OPERAD_WORKTREE: worktree,
|
|
1146
|
+
OPERAD_REMOTE: settings.remote,
|
|
1147
|
+
};
|
|
1148
|
+
const failures: CleanupFailure[] = [];
|
|
1149
|
+
const runStep = ctx.runStep;
|
|
1150
|
+
|
|
1151
|
+
if (runStep === undefined) {
|
|
1152
|
+
if (settings.hooks.afterMerge.length > 0) {
|
|
1153
|
+
failures.push({
|
|
1154
|
+
line: 0,
|
|
1155
|
+
text: "",
|
|
1156
|
+
message: "the kernel did not offer a way to run the cleanup hooks",
|
|
1157
|
+
});
|
|
1158
|
+
}
|
|
1159
|
+
} else {
|
|
1160
|
+
for (const [index, line] of settings.hooks.afterMerge.entries()) {
|
|
1161
|
+
let outcome: PluginStepResult | undefined;
|
|
1162
|
+
let hostError: string | undefined;
|
|
1163
|
+
try {
|
|
1164
|
+
outcome = await runStep(
|
|
1165
|
+
"shell/command",
|
|
1166
|
+
{ line, cwd: context.repository.path, env },
|
|
1167
|
+
stepOptions(ctx, context.repository.path),
|
|
1168
|
+
);
|
|
1169
|
+
} catch (error) {
|
|
1170
|
+
hostError = messageOf(error);
|
|
1171
|
+
}
|
|
1172
|
+
integrateTestHooks.at?.("hook-ran");
|
|
1173
|
+
if (hostError !== undefined || outcome?.result !== "pass") {
|
|
1174
|
+
const data =
|
|
1175
|
+
outcome !== undefined && isRecord(outcome.data) ? outcome.data : {};
|
|
1176
|
+
failures.push({
|
|
1177
|
+
line: index + 1,
|
|
1178
|
+
text: line,
|
|
1179
|
+
message:
|
|
1180
|
+
hostError ??
|
|
1181
|
+
(typeof data.message === "string"
|
|
1182
|
+
? data.message
|
|
1183
|
+
: `the hook ended "${outcome?.result ?? "unknown"}"`),
|
|
1184
|
+
});
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
const record: CleanupRecord = { run: ctx.runId, failures };
|
|
1190
|
+
const next: TaskData = {
|
|
1191
|
+
...task,
|
|
1192
|
+
phase: "merged",
|
|
1193
|
+
run: ctx.runId,
|
|
1194
|
+
attempts: Math.max(task.attempts, attempt.number),
|
|
1195
|
+
attempt: { ...attempt, run: ctx.runId, cleanup: record },
|
|
1196
|
+
brief:
|
|
1197
|
+
failures.length === 0
|
|
1198
|
+
? task.brief
|
|
1199
|
+
: appendChange(task.brief, `cleanup: ${failures.length} hook(s) failed`),
|
|
1200
|
+
};
|
|
1201
|
+
if (!(await saveTaskGuarded(ctx, context.taskKey, next, "dev:cleanup"))) {
|
|
1202
|
+
return cancelledStep();
|
|
1203
|
+
}
|
|
1204
|
+
await refreshPlan(ctx, context);
|
|
1205
|
+
return {
|
|
1206
|
+
result: "done",
|
|
1207
|
+
data: {
|
|
1208
|
+
attempt: attempt.number,
|
|
1209
|
+
failures: failures.length,
|
|
1210
|
+
...(failures.length === 0 ? {} : { failed: failures }),
|
|
1211
|
+
},
|
|
1212
|
+
};
|
|
1213
|
+
}
|