@operad-io/dev 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dev.schema.json +41 -28
- package/package.json +9 -6
- package/skills/setup/SKILL.md +57 -0
- package/src/access-grants.ts +59 -0
- package/src/actions.ts +78 -17
- package/src/announce.ts +128 -16
- package/src/chat.ts +512 -36
- package/src/check-host.ts +111 -0
- package/src/check-load.ts +67 -0
- package/src/check.ts +17 -12
- package/src/clock.ts +29 -0
- package/src/commands.ts +103 -34
- package/src/create-actions.ts +210 -0
- package/src/flaky.ts +45 -29
- package/src/flow-compiler.ts +155 -68
- package/src/followups.ts +20 -22
- package/src/gate.ts +192 -73
- package/src/git.ts +47 -0
- package/src/index.ts +46 -6
- package/src/integrate.ts +121 -43
- package/src/integration-queue.ts +176 -25
- package/src/lock.ts +101 -5
- package/src/mainclone.ts +44 -5
- package/src/model-error.ts +70 -0
- package/src/model.ts +50 -35
- package/src/notcode.ts +51 -15
- package/src/places.ts +2 -2
- package/src/prepare.ts +7 -2
- package/src/project-flows.ts +135 -4
- package/src/project-settings.ts +81 -0
- package/src/register-requests.ts +87 -0
- package/src/repository-key.ts +4 -0
- package/src/rerun.ts +23 -7
- package/src/retry.ts +2 -12
- package/src/rework.ts +8 -0
- package/src/schedule.ts +317 -86
- package/src/service.ts +177 -148
- package/src/settings.ts +305 -60
- package/src/standing.ts +23 -6
- package/src/step-edge.ts +22 -0
- package/src/steps.ts +469 -123
- package/src/throughput.ts +4 -21
- package/src/timeouts.ts +10 -2
- package/src/views.ts +147 -81
- package/src/waits.ts +59 -0
package/src/integrate.ts
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* never runs either itself.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { guardOutward, personStarted } from "@operad-io/access-match";
|
|
8
|
+
import { isRecord, messageOf, type PluginStepResult, type StepContext } from "@operad-io/engine";
|
|
8
9
|
import { appendChange, appendRework, storeKey, type CleanupFailure, type CleanupRecord, type IntegrationRecord, type TaskAttempt, type TaskData } from "./model.ts";
|
|
9
10
|
import {
|
|
10
11
|
branchHead,
|
|
@@ -12,6 +13,7 @@ import {
|
|
|
12
13
|
recreateGateWorktree,
|
|
13
14
|
} from "./gate.ts";
|
|
14
15
|
import { mainCloneRunner, runPrepare, stepOptions } from "./prepare.ts";
|
|
16
|
+
import { checkEnvironment } from "./check-host.ts";
|
|
15
17
|
import { checkPlaces } from "./places.ts";
|
|
16
18
|
import {
|
|
17
19
|
IntegrationQueue,
|
|
@@ -25,18 +27,24 @@ import {
|
|
|
25
27
|
updateMainClone,
|
|
26
28
|
type CommandRunner,
|
|
27
29
|
type MainCloneOutcome,
|
|
30
|
+
type PrepareMarker,
|
|
28
31
|
} from "./mainclone.ts";
|
|
29
32
|
import { runCheckSteadily } from "./notcode.ts";
|
|
30
33
|
import { runCheckCommands } from "./rerun.ts";
|
|
31
34
|
import { reportFlakyTests } from "./flaky.ts";
|
|
32
35
|
import { recordRerunPasses } from "./reruns.ts";
|
|
33
36
|
import { getPlan, refreshPlanCounts } from "./service.ts";
|
|
37
|
+
import {
|
|
38
|
+
commitOf,
|
|
39
|
+
git,
|
|
40
|
+
headOf,
|
|
41
|
+
isAncestor,
|
|
42
|
+
treeOf,
|
|
43
|
+
} from "./git.ts";
|
|
34
44
|
import {
|
|
35
45
|
cancelledStep,
|
|
36
46
|
claimAllows,
|
|
37
47
|
conflictedFiles,
|
|
38
|
-
git,
|
|
39
|
-
headOf,
|
|
40
48
|
mergeInProgress,
|
|
41
49
|
saveTaskGuarded,
|
|
42
50
|
taskContext,
|
|
@@ -71,6 +79,11 @@ export const integrateTestHooks: {
|
|
|
71
79
|
turnTaken?: ((task: string) => Promise<void> | void) | undefined;
|
|
72
80
|
/** Awaited by a batch's leader once it has claimed the tasks queued behind it. */
|
|
73
81
|
claimed?: ((followers: readonly string[]) => Promise<void> | void) | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* Awaited under the queue's land lock between the leader's last check and
|
|
84
|
+
* its push. A test holds the push to make a cancel race it.
|
|
85
|
+
*/
|
|
86
|
+
beforePush?: ((candidate: string) => Promise<void> | void) | undefined;
|
|
74
87
|
/** How a task waits for its turn before it looks again. */
|
|
75
88
|
wait?: ((ms: number) => Promise<void>) | undefined;
|
|
76
89
|
} = {};
|
|
@@ -83,6 +96,7 @@ export async function followMain(
|
|
|
83
96
|
lines: [],
|
|
84
97
|
run: async () => ({ ok: true, out: "" }),
|
|
85
98
|
},
|
|
99
|
+
marker?: PrepareMarker | undefined,
|
|
86
100
|
): Promise<MainCloneOutcome> {
|
|
87
101
|
return updateMainClone(
|
|
88
102
|
repository.path,
|
|
@@ -90,6 +104,7 @@ export async function followMain(
|
|
|
90
104
|
sha,
|
|
91
105
|
prepare.lines,
|
|
92
106
|
integrateTestHooks.runCommand ?? prepare.run,
|
|
107
|
+
marker,
|
|
93
108
|
);
|
|
94
109
|
}
|
|
95
110
|
|
|
@@ -97,20 +112,6 @@ function shortSha(sha: string): string {
|
|
|
97
112
|
return sha.slice(0, 7);
|
|
98
113
|
}
|
|
99
114
|
|
|
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
115
|
interface Candidate {
|
|
115
116
|
readonly kind: "ok" | "conflict" | "failed";
|
|
116
117
|
readonly sha?: string | undefined;
|
|
@@ -255,12 +256,6 @@ function integrationData(
|
|
|
255
256
|
};
|
|
256
257
|
}
|
|
257
258
|
|
|
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
259
|
/** The patch id of a diff, which ignores line numbers and whitespace. */
|
|
265
260
|
function patchIdOf(cwd: string, args: readonly string[]): string | undefined {
|
|
266
261
|
const diff = git(cwd, args, { raw: true });
|
|
@@ -277,7 +272,7 @@ function patchIdOf(cwd: string, args: readonly string[]): string | undefined {
|
|
|
277
272
|
function carrierOnMain(cwd: string, verified: string, main: string): string | undefined {
|
|
278
273
|
// A verified commit that main contains arrived in the first commit of main's
|
|
279
274
|
// own line that has it.
|
|
280
|
-
if (
|
|
275
|
+
if (isAncestor(cwd, verified, main)) {
|
|
281
276
|
const arrived = git(cwd, [
|
|
282
277
|
"rev-list",
|
|
283
278
|
"--first-parent",
|
|
@@ -300,6 +295,7 @@ function carrierOnMain(cwd: string, verified: string, main: string): string | un
|
|
|
300
295
|
return undefined;
|
|
301
296
|
}
|
|
302
297
|
|
|
298
|
+
/** Step `dev/integrate`: land the verified commit on main. */
|
|
303
299
|
export async function runIntegrate(
|
|
304
300
|
ctx: StepContext,
|
|
305
301
|
): Promise<PluginStepResult> {
|
|
@@ -355,9 +351,51 @@ export async function runIntegrate(
|
|
|
355
351
|
};
|
|
356
352
|
};
|
|
357
353
|
|
|
354
|
+
/** Asks before a push in a background run; a person-started run and an allowlisted target do not ask. */
|
|
355
|
+
const pushAllowed = async (sha: string | undefined, what: string): Promise<boolean> =>
|
|
356
|
+
guardOutward(
|
|
357
|
+
{
|
|
358
|
+
access: ctx.access,
|
|
359
|
+
ask: (request) => ctx.ask(request),
|
|
360
|
+
personStarted: personStarted(ctx.trigger),
|
|
361
|
+
onTimeout: "reject_once" as const,
|
|
362
|
+
home: ctx.home,
|
|
363
|
+
...(ctx.flow === undefined ? {} : { flow: ctx.flow }),
|
|
364
|
+
onLog: (message) => {
|
|
365
|
+
void ctx.logger.write(message);
|
|
366
|
+
},
|
|
367
|
+
},
|
|
368
|
+
{
|
|
369
|
+
kind: "repository",
|
|
370
|
+
target: `${settings.remote}/${settings.mainBranch}`,
|
|
371
|
+
content: `push ${sha ?? "HEAD"} to ${settings.remote}/${settings.mainBranch} (${what})`,
|
|
372
|
+
},
|
|
373
|
+
);
|
|
374
|
+
|
|
358
375
|
let flakyReason: string | undefined;
|
|
359
376
|
/** The other tasks this task was merged with in one batch. */
|
|
360
377
|
let batchWith: readonly string[] = [];
|
|
378
|
+
/** Saves that this merge's prepare is due before the clone moves, and done after. */
|
|
379
|
+
// A killed run of this step may have left prepare due; a restart that cannot
|
|
380
|
+
// fetch builds fresh records and must not forget it.
|
|
381
|
+
const leftDue =
|
|
382
|
+
task.attempt?.integration?.run === ctx.runId &&
|
|
383
|
+
task.attempt.integration.prepareDue === true;
|
|
384
|
+
const prepareMarker = (record: IntegrationRecord): PrepareMarker => ({
|
|
385
|
+
due: record.prepareDue === true || leftDue,
|
|
386
|
+
save: async (due) => {
|
|
387
|
+
const pending: IntegrationRecord = {
|
|
388
|
+
...record,
|
|
389
|
+
result: "pending",
|
|
390
|
+
prepareDue: due,
|
|
391
|
+
};
|
|
392
|
+
return (
|
|
393
|
+
(await writeIntegration(ctx, context, task, attempt, pending, {
|
|
394
|
+
integrating: true,
|
|
395
|
+
})) !== undefined
|
|
396
|
+
);
|
|
397
|
+
},
|
|
398
|
+
});
|
|
361
399
|
const recordMerged = async (
|
|
362
400
|
record: IntegrationRecord,
|
|
363
401
|
sha: string,
|
|
@@ -366,15 +404,22 @@ export async function runIntegrate(
|
|
|
366
404
|
mergedLine: string = `merged as ${shortSha(sha)}`,
|
|
367
405
|
): Promise<PluginStepResult> => {
|
|
368
406
|
const note = record.checkNote;
|
|
407
|
+
const marker = prepareMarker(record);
|
|
369
408
|
const clone = await followMain(
|
|
370
409
|
context.repository,
|
|
371
410
|
settings.mainBranch,
|
|
372
411
|
cloneSha,
|
|
373
412
|
{ lines: settings.prepare, run: mainCloneRunner(ctx, settings) },
|
|
413
|
+
marker,
|
|
374
414
|
);
|
|
375
415
|
integrateTestHooks.at?.("clone-followed");
|
|
376
416
|
const cloneChange = mainCloneChange(clone);
|
|
377
|
-
|
|
417
|
+
// A clone that did not move keeps the prepare the killed run left due.
|
|
418
|
+
const withClone: IntegrationRecord = {
|
|
419
|
+
...record,
|
|
420
|
+
prepareDue: !clone.updated && marker.due,
|
|
421
|
+
mainClone: { ...clone },
|
|
422
|
+
};
|
|
378
423
|
if (
|
|
379
424
|
(await writeIntegration(ctx, context, task, attempt, withClone, {
|
|
380
425
|
phase: "merged",
|
|
@@ -419,11 +464,11 @@ export async function runIntegrate(
|
|
|
419
464
|
previous.candidate.length > 0
|
|
420
465
|
) {
|
|
421
466
|
const fetched = git(repository, ["fetch", "--prune", settings.remote]);
|
|
422
|
-
const remoteMain =
|
|
467
|
+
const remoteMain = commitOf(repository, mainRef);
|
|
423
468
|
if (
|
|
424
469
|
fetched.ok &&
|
|
425
470
|
remoteMain !== undefined &&
|
|
426
|
-
|
|
471
|
+
isAncestor(repository, previous.candidate, remoteMain)
|
|
427
472
|
) {
|
|
428
473
|
const record: IntegrationRecord = {
|
|
429
474
|
...previous,
|
|
@@ -441,15 +486,18 @@ export async function runIntegrate(
|
|
|
441
486
|
const previousBatch = Array.isArray(previous.batch)
|
|
442
487
|
? previous.batch.filter((name): name is string => typeof name === "string")
|
|
443
488
|
: [];
|
|
489
|
+
const marker = prepareMarker(record);
|
|
444
490
|
const clone = await followMain(
|
|
445
491
|
context.repository,
|
|
446
492
|
settings.mainBranch,
|
|
447
493
|
typeof previous.batchHead === "string" ? previous.batchHead : previous.candidate,
|
|
448
494
|
{ lines: settings.prepare, run: mainCloneRunner(ctx, settings) },
|
|
495
|
+
marker,
|
|
449
496
|
);
|
|
450
497
|
const cloneChange = mainCloneChange(clone);
|
|
451
498
|
const withClone: IntegrationRecord = {
|
|
452
499
|
...record,
|
|
500
|
+
prepareDue: !clone.updated && marker.due,
|
|
453
501
|
mainClone: { ...clone },
|
|
454
502
|
};
|
|
455
503
|
if (
|
|
@@ -493,8 +541,8 @@ export async function runIntegrate(
|
|
|
493
541
|
wait: integrateTestHooks.wait,
|
|
494
542
|
landed: (result) => {
|
|
495
543
|
if (!git(repository, ["fetch", "--prune", settings.remote]).ok) return false;
|
|
496
|
-
const main =
|
|
497
|
-
return main !== undefined &&
|
|
544
|
+
const main = commitOf(repository, mainRef);
|
|
545
|
+
return main !== undefined && isAncestor(repository, result.head, main);
|
|
498
546
|
},
|
|
499
547
|
say: async (line, holder) => {
|
|
500
548
|
await ctx.logger.write(line);
|
|
@@ -520,7 +568,7 @@ export async function runIntegrate(
|
|
|
520
568
|
/** This task's merge commit when main already has it, and the main that has it. */
|
|
521
569
|
const alreadyOnMain = (): { sha: string; main: string } | undefined => {
|
|
522
570
|
if (!git(repository, ["fetch", "--prune", settings.remote]).ok) return undefined;
|
|
523
|
-
const main =
|
|
571
|
+
const main = commitOf(repository, mainRef);
|
|
524
572
|
if (main === undefined) return undefined;
|
|
525
573
|
const found = git(repository, [
|
|
526
574
|
"log",
|
|
@@ -553,7 +601,7 @@ export async function runIntegrate(
|
|
|
553
601
|
for (let round = 0; round < MAX_INTEGRATION_ROUNDS; round += 1) {
|
|
554
602
|
const fetched = git(repository, ["fetch", "--prune", settings.remote]);
|
|
555
603
|
if (!fetched.ok) return undefined;
|
|
556
|
-
const base =
|
|
604
|
+
const base = commitOf(repository, mainRef);
|
|
557
605
|
if (base === undefined) return undefined;
|
|
558
606
|
if (recreateGateWorktree(repository, gate, base, settings) !== undefined) {
|
|
559
607
|
return undefined;
|
|
@@ -591,6 +639,7 @@ export async function runIntegrate(
|
|
|
591
639
|
...(settings.gate.stallSecs === undefined
|
|
592
640
|
? {}
|
|
593
641
|
: { stallSecs: settings.gate.stallSecs }),
|
|
642
|
+
env: checkEnvironment(ctx.settings),
|
|
594
643
|
},
|
|
595
644
|
keyCommands,
|
|
596
645
|
stepOptions(ctx, gate),
|
|
@@ -607,6 +656,7 @@ export async function runIntegrate(
|
|
|
607
656
|
flakyLimit: settings.check.flakyLoadLimit,
|
|
608
657
|
signal: ctx.signal,
|
|
609
658
|
log: (text) => ctx.logger.write(text),
|
|
659
|
+
logPath: ctx.logger.path,
|
|
610
660
|
});
|
|
611
661
|
if (steady.cancelled === true) return cancelledStep();
|
|
612
662
|
const { checked } = steady;
|
|
@@ -709,17 +759,37 @@ export async function runIntegrate(
|
|
|
709
759
|
return merged;
|
|
710
760
|
};
|
|
711
761
|
// A claimed task that left (cancelled) is not merged: one by one instead.
|
|
712
|
-
//
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
762
|
+
// The check and the push share the queue's land lock with a withdrawal,
|
|
763
|
+
// so a cancel either comes before the check or after the push.
|
|
764
|
+
// A push that reports an error may still have landed: that is settled
|
|
765
|
+
// inside the lock, so a cancel waiting on it sees a pushed batch.
|
|
766
|
+
let landedDespiteError = false;
|
|
767
|
+
const landed = await queue.landWhenStillQueued(followers, result, async () => {
|
|
768
|
+
await integrateTestHooks.beforePush?.(batchHead);
|
|
769
|
+
if (!(await pushAllowed(batchHead, "the batch merge"))) return false;
|
|
770
|
+
const pushed = git(gate, [
|
|
771
|
+
"push",
|
|
772
|
+
settings.remote,
|
|
773
|
+
`HEAD:refs/heads/${settings.mainBranch}`,
|
|
774
|
+
]).ok;
|
|
775
|
+
if (pushed) return true;
|
|
776
|
+
git(repository, ["fetch", "--prune", settings.remote]);
|
|
777
|
+
const after = commitOf(repository, mainRef);
|
|
778
|
+
landedDespiteError = after !== undefined && isAncestor(repository, batchHead, after);
|
|
779
|
+
return landedDespiteError;
|
|
780
|
+
});
|
|
781
|
+
if (landed === "withdrawn") return undefined;
|
|
782
|
+
if (landed === "failed") {
|
|
783
|
+
return fail(`the person did not allow the push to "${settings.remote}/${settings.mainBranch}"`);
|
|
784
|
+
}
|
|
785
|
+
if (landed === "pushed") {
|
|
786
|
+
if (landedDespiteError) return land({ landed: true });
|
|
717
787
|
integrateTestHooks.afterPush?.(batchHead);
|
|
718
788
|
return land({});
|
|
719
789
|
}
|
|
720
790
|
git(repository, ["fetch", "--prune", settings.remote]);
|
|
721
|
-
const now =
|
|
722
|
-
if (now !== undefined &&
|
|
791
|
+
const now = commitOf(repository, mainRef);
|
|
792
|
+
if (now !== undefined && isAncestor(repository, batchHead, now)) {
|
|
723
793
|
return land({ landed: true });
|
|
724
794
|
}
|
|
725
795
|
if (now !== undefined && now !== base) continue;
|
|
@@ -747,6 +817,9 @@ export async function runIntegrate(
|
|
|
747
817
|
head: head ?? null,
|
|
748
818
|
headMoved,
|
|
749
819
|
pushed: true,
|
|
820
|
+
// What a replay needs to follow the batch's head and say the batch.
|
|
821
|
+
batchHead: leaderResult.head,
|
|
822
|
+
batch: batchWith,
|
|
750
823
|
...(leaderResult.note === undefined ? {} : { checkNote: leaderResult.note }),
|
|
751
824
|
};
|
|
752
825
|
return recordMerged(record, mine.sha, {}, leaderResult.head);
|
|
@@ -769,7 +842,7 @@ export async function runIntegrate(
|
|
|
769
842
|
const landed = alreadyOnMain();
|
|
770
843
|
if (landed !== undefined) {
|
|
771
844
|
// The dead leader's note and batch are gone; main still has the base.
|
|
772
|
-
const landedBase =
|
|
845
|
+
const landedBase = commitOf(repository, `${landed.sha}^1`);
|
|
773
846
|
return recordMerged(
|
|
774
847
|
{
|
|
775
848
|
result: "merged",
|
|
@@ -816,7 +889,7 @@ export async function runIntegrate(
|
|
|
816
889
|
`cannot fetch "${settings.remote}": ${fetched.err || fetched.out || "git fetch failed"}`,
|
|
817
890
|
);
|
|
818
891
|
}
|
|
819
|
-
const base =
|
|
892
|
+
const base = commitOf(repository, mainRef);
|
|
820
893
|
if (base === undefined) {
|
|
821
894
|
return fail(`"${settings.remote}/${settings.mainBranch}" does not exist after the fetch`);
|
|
822
895
|
}
|
|
@@ -931,6 +1004,7 @@ export async function runIntegrate(
|
|
|
931
1004
|
...(settings.gate.stallSecs === undefined
|
|
932
1005
|
? {}
|
|
933
1006
|
: { stallSecs: settings.gate.stallSecs }),
|
|
1007
|
+
env: checkEnvironment(ctx.settings),
|
|
934
1008
|
},
|
|
935
1009
|
keyCommands,
|
|
936
1010
|
stepOptions(ctx, gate),
|
|
@@ -947,6 +1021,7 @@ export async function runIntegrate(
|
|
|
947
1021
|
flakyLimit: settings.check.flakyLoadLimit,
|
|
948
1022
|
signal: ctx.signal,
|
|
949
1023
|
log: (text) => ctx.logger.write(text),
|
|
1024
|
+
logPath: ctx.logger.path,
|
|
950
1025
|
});
|
|
951
1026
|
if (steady.cancelled === true) return cancelledStep();
|
|
952
1027
|
const { checked } = steady;
|
|
@@ -1079,6 +1154,9 @@ export async function runIntegrate(
|
|
|
1079
1154
|
}
|
|
1080
1155
|
integrateTestHooks.at?.("pending-saved");
|
|
1081
1156
|
|
|
1157
|
+
if (!(await pushAllowed(sha, "the task merge"))) {
|
|
1158
|
+
return fail(`the person did not allow the push to "${settings.remote}/${settings.mainBranch}"`);
|
|
1159
|
+
}
|
|
1082
1160
|
const push = git(gate, [
|
|
1083
1161
|
"push",
|
|
1084
1162
|
settings.remote,
|
|
@@ -1096,11 +1174,11 @@ export async function runIntegrate(
|
|
|
1096
1174
|
|
|
1097
1175
|
lastError = `cannot push "${settings.mainBranch}" to "${settings.remote}": ${push.err || push.out || "git push failed"}`;
|
|
1098
1176
|
git(repository, ["fetch", "--prune", settings.remote]);
|
|
1099
|
-
const now =
|
|
1177
|
+
const now = commitOf(repository, mainRef);
|
|
1100
1178
|
// A push can land on the remote and still report an error (the client lost
|
|
1101
1179
|
// the answer). The remote's main then contains the candidate: it is
|
|
1102
1180
|
// merged, and building and pushing a second candidate would be wrong.
|
|
1103
|
-
if (now !== undefined &&
|
|
1181
|
+
if (now !== undefined && isAncestor(repository, sha, now)) {
|
|
1104
1182
|
const record: IntegrationRecord = {
|
|
1105
1183
|
...pending,
|
|
1106
1184
|
result: "merged",
|
|
@@ -1118,7 +1196,7 @@ export async function runIntegrate(
|
|
|
1118
1196
|
`main moved ${MAX_INTEGRATION_ROUNDS} times while the candidate was built; ${lastError}`,
|
|
1119
1197
|
);
|
|
1120
1198
|
} finally {
|
|
1121
|
-
queue.leave();
|
|
1199
|
+
await queue.leave();
|
|
1122
1200
|
}
|
|
1123
1201
|
}
|
|
1124
1202
|
|
package/src/integration-queue.ts
CHANGED
|
@@ -12,6 +12,11 @@
|
|
|
12
12
|
* A ticket is written only by its own task. Claims and "integrate alone" marks
|
|
13
13
|
* are separate files the leader owns (`c-<leader>.json`, `s-<task>.json`), so a
|
|
14
14
|
* task that leaves while it is claimed is never written back.
|
|
15
|
+
*
|
|
16
|
+
* Withdrawing a ticket and a leader's last check-and-push take the same lock
|
|
17
|
+
* (`land.lock`): either the withdrawal comes first, so the leader sees it and
|
|
18
|
+
* lands without that task, or the push comes first, so the task stays in the
|
|
19
|
+
* batch. The lock is held across the push, and nothing slower.
|
|
15
20
|
*/
|
|
16
21
|
|
|
17
22
|
import {
|
|
@@ -19,12 +24,23 @@ import {
|
|
|
19
24
|
mkdirSync,
|
|
20
25
|
readdirSync,
|
|
21
26
|
readFileSync,
|
|
22
|
-
renameSync,
|
|
23
27
|
rmSync,
|
|
24
|
-
writeFileSync,
|
|
25
28
|
} from "node:fs";
|
|
26
29
|
import { join } from "node:path";
|
|
27
|
-
import {
|
|
30
|
+
import {
|
|
31
|
+
waitForChange,
|
|
32
|
+
withLockFile,
|
|
33
|
+
writeJsonAtomicSync,
|
|
34
|
+
} from "@operad-io/engine/platform";
|
|
35
|
+
import { isRecord } from "@operad-io/engine";
|
|
36
|
+
import { repositoryPathKey } from "./repository-key.ts";
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* How long a waiter accepts a live holder of the land lock. The holder is
|
|
40
|
+
* across a git push, which has no bound of its own, so the engine's 10 s
|
|
41
|
+
* default would fail every cancel and leave behind a slow push.
|
|
42
|
+
*/
|
|
43
|
+
const LAND_LOCK_TIMEOUT_MS = 30 * 60 * 1000;
|
|
28
44
|
|
|
29
45
|
/** How often a waiting integration looks again when nothing announced a change. */
|
|
30
46
|
const RECHECK_MS = 500;
|
|
@@ -50,11 +66,12 @@ export interface BatchResult {
|
|
|
50
66
|
readonly head: string;
|
|
51
67
|
readonly note?: string | undefined;
|
|
52
68
|
/**
|
|
53
|
-
* `pending` from before the leader pushes until
|
|
54
|
-
*
|
|
55
|
-
*
|
|
69
|
+
* `pending` from before the leader pushes until the push succeeds, `pushed`
|
|
70
|
+
* once it has, and `done` after the leader recorded the merge. A pending
|
|
71
|
+
* result whose leader is gone is settled by whether `head` landed. A result
|
|
72
|
+
* without a state is done.
|
|
56
73
|
*/
|
|
57
|
-
readonly state?: "pending" | "done" | undefined;
|
|
74
|
+
readonly state?: "pending" | "pushed" | "done" | undefined;
|
|
58
75
|
readonly members: readonly { readonly task: string; readonly sha: string }[];
|
|
59
76
|
}
|
|
60
77
|
|
|
@@ -71,6 +88,24 @@ export type Turn =
|
|
|
71
88
|
| { readonly kind: "turn" }
|
|
72
89
|
| { readonly kind: "batched"; readonly leader: string; readonly result: BatchResult };
|
|
73
90
|
|
|
91
|
+
/** What withdrawing a ticket found: it went, or the leader had already pushed it. */
|
|
92
|
+
export type Withdrawal =
|
|
93
|
+
| { readonly kind: "withdrawn" }
|
|
94
|
+
| { readonly kind: "merged"; readonly leader: string; readonly result: BatchResult };
|
|
95
|
+
|
|
96
|
+
/** What a leader's last check, publish and push did. */
|
|
97
|
+
export type Landing = "withdrawn" | "pushed" | "failed";
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Test-only hooks. Production code never sets them. `lockWait` replaces the
|
|
101
|
+
* wait of the land lock so a test can see a withdrawal wait behind a push;
|
|
102
|
+
* `withdrawn` reports what that withdrawal found.
|
|
103
|
+
*/
|
|
104
|
+
export const integrationQueueTestHooks: {
|
|
105
|
+
lockWait?: ((ms: number) => Promise<void>) | undefined;
|
|
106
|
+
withdrawn?: ((task: string, kind: Withdrawal["kind"]) => void) | undefined;
|
|
107
|
+
} = {};
|
|
108
|
+
|
|
74
109
|
export interface QueueOptions {
|
|
75
110
|
readonly home: string;
|
|
76
111
|
readonly repository: { readonly name: string; readonly path: string };
|
|
@@ -93,7 +128,7 @@ export function integrationQueueDirectory(
|
|
|
93
128
|
home: string,
|
|
94
129
|
repository: { readonly name: string; readonly path: string },
|
|
95
130
|
): string {
|
|
96
|
-
const hash =
|
|
131
|
+
const hash = repositoryPathKey(repository.path);
|
|
97
132
|
const name = repository.name.replace(/[^A-Za-z0-9._-]/g, "_");
|
|
98
133
|
return join(home, "dev", "integration-queue", `${name}-${hash.slice(0, 8)}`);
|
|
99
134
|
}
|
|
@@ -111,17 +146,19 @@ function safeName(task: string): string {
|
|
|
111
146
|
return task.replace(/[^A-Za-z0-9._-]/g, "_");
|
|
112
147
|
}
|
|
113
148
|
|
|
149
|
+
function landLockPath(directory: string): string {
|
|
150
|
+
return join(directory, "land.lock");
|
|
151
|
+
}
|
|
152
|
+
|
|
114
153
|
function writeAtomic(path: string, value: unknown): void {
|
|
115
|
-
|
|
116
|
-
writeFileSync(temporary, JSON.stringify(value));
|
|
117
|
-
renameSync(temporary, path);
|
|
154
|
+
writeJsonAtomicSync(path, value, { newline: false });
|
|
118
155
|
}
|
|
119
156
|
|
|
120
157
|
function parseTicket(text: string): Ticket | undefined {
|
|
121
158
|
try {
|
|
122
159
|
const value: unknown = JSON.parse(text);
|
|
123
|
-
if (
|
|
124
|
-
const record = value
|
|
160
|
+
if (!isRecord(value)) return undefined;
|
|
161
|
+
const record = value;
|
|
125
162
|
if (
|
|
126
163
|
typeof record.seq !== "number" ||
|
|
127
164
|
typeof record.task !== "string" ||
|
|
@@ -171,6 +208,27 @@ export class IntegrationQueue {
|
|
|
171
208
|
return join(this.directory, `r-${safeName(leader)}.json`);
|
|
172
209
|
}
|
|
173
210
|
|
|
211
|
+
/** The lock a withdrawal and a leader's last check-and-push share. */
|
|
212
|
+
private withLandLock<T>(work: () => Promise<T> | T): Promise<T> {
|
|
213
|
+
const wait = integrationQueueTestHooks.lockWait;
|
|
214
|
+
return withLockFile(landLockPath(this.directory), work, {
|
|
215
|
+
label: "integration queue",
|
|
216
|
+
onLog: () => {},
|
|
217
|
+
timeoutMs: LAND_LOCK_TIMEOUT_MS,
|
|
218
|
+
...(wait === undefined ? {} : { wait }),
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Removes this task's ticket and its "integrate alone" mark. */
|
|
223
|
+
private removeTicket(): void {
|
|
224
|
+
this.joined = false;
|
|
225
|
+
rmSync(this.ticketPath(this.options.task), { force: true });
|
|
226
|
+
rmSync(this.soloPath(this.options.task), { force: true });
|
|
227
|
+
// Looking prunes what no one needs now: a leader's claim and result stay
|
|
228
|
+
// while a task it claimed is still queued.
|
|
229
|
+
this.tickets();
|
|
230
|
+
}
|
|
231
|
+
|
|
174
232
|
/** The live tickets in queue order; tickets of dead processes are dropped. */
|
|
175
233
|
tickets(): readonly Ticket[] {
|
|
176
234
|
let names: string[];
|
|
@@ -272,14 +330,65 @@ export class IntegrationQueue {
|
|
|
272
330
|
}
|
|
273
331
|
|
|
274
332
|
/** Gives the turn back, and the ticket with it. */
|
|
275
|
-
leave(): void {
|
|
333
|
+
async leave(): Promise<void> {
|
|
276
334
|
if (!this.joined) return;
|
|
277
|
-
this.
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
335
|
+
await this.withLandLock(() => this.removeTicket());
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* A withdrawal takes the land lock, so it cannot slip between a leader's last
|
|
340
|
+
* check and its push. It removes the ticket unless the leader already pushed
|
|
341
|
+
* a batch that lists this task; then it says so and leaves the task to record
|
|
342
|
+
* its merge.
|
|
343
|
+
*/
|
|
344
|
+
async withdraw(): Promise<Withdrawal> {
|
|
345
|
+
const withdrawal = await this.withLandLock(() => {
|
|
346
|
+
const me = this.options.task;
|
|
347
|
+
const mine = this.tickets().find((ticket) => ticket.task === me);
|
|
348
|
+
// A follower reads its leader's result; the leader reads its own. The
|
|
349
|
+
// leader's ticket is still here while it pushes, so it must be asked too.
|
|
350
|
+
const leader = mine === undefined ? undefined : (mine.claimedBy ?? me);
|
|
351
|
+
if (leader !== undefined) {
|
|
352
|
+
const result = this.readResult(leader);
|
|
353
|
+
if (
|
|
354
|
+
result !== undefined &&
|
|
355
|
+
result.state !== "pending" &&
|
|
356
|
+
result.members.some((member) => member.task === me)
|
|
357
|
+
) {
|
|
358
|
+
return { kind: "merged" as const, leader, result };
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
this.removeTicket();
|
|
362
|
+
return { kind: "withdrawn" as const };
|
|
363
|
+
});
|
|
364
|
+
integrationQueueTestHooks.withdrawn?.(this.options.task, withdrawal.kind);
|
|
365
|
+
return withdrawal;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* The leader's last check, its result and the push, under the land lock. A
|
|
370
|
+
* ticket that left first makes this land without its batch; a push that
|
|
371
|
+
* lands first leaves a result a waiting withdrawal reads as merged.
|
|
372
|
+
*/
|
|
373
|
+
async landWhenStillQueued(
|
|
374
|
+
followers: readonly Ticket[],
|
|
375
|
+
result: BatchResult,
|
|
376
|
+
push: () => Promise<boolean> | boolean,
|
|
377
|
+
): Promise<Landing> {
|
|
378
|
+
return this.withLandLock(async () => {
|
|
379
|
+
const queued = new Set(this.tickets().map((ticket) => ticket.task));
|
|
380
|
+
if (
|
|
381
|
+
!queued.has(this.options.task) ||
|
|
382
|
+
this.stillQueued(followers).length !== followers.length
|
|
383
|
+
) {
|
|
384
|
+
return "withdrawn" as const;
|
|
385
|
+
}
|
|
386
|
+
this.publish(result);
|
|
387
|
+
const pushed = await push();
|
|
388
|
+
if (!pushed) return "failed" as const;
|
|
389
|
+
this.markPushed();
|
|
390
|
+
return "pushed" as const;
|
|
391
|
+
});
|
|
283
392
|
}
|
|
284
393
|
|
|
285
394
|
private readResult(leader: string): BatchResult | undefined {
|
|
@@ -321,8 +430,19 @@ export class IntegrationQueue {
|
|
|
321
430
|
}
|
|
322
431
|
holder = claimed;
|
|
323
432
|
}
|
|
324
|
-
if (this.options.signal?.aborted === true)
|
|
325
|
-
|
|
433
|
+
if (this.options.signal?.aborted === true) {
|
|
434
|
+
const withdrawal = await this.withdraw();
|
|
435
|
+
if (withdrawal.kind === "merged") {
|
|
436
|
+
return {
|
|
437
|
+
kind: "batched",
|
|
438
|
+
leader: withdrawal.leader,
|
|
439
|
+
result: withdrawal.result,
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
throw new IntegrationQueueCancelled();
|
|
443
|
+
}
|
|
444
|
+
// Only a withdrawal removes a live task's ticket; its run is cancelled.
|
|
445
|
+
if (mine === undefined) throw new IntegrationQueueCancelled();
|
|
326
446
|
if (holder === undefined) {
|
|
327
447
|
const head = tickets.find((ticket) => ticket.claimedBy === undefined);
|
|
328
448
|
if (head !== undefined && head.task === me) return { kind: "turn" };
|
|
@@ -379,7 +499,14 @@ export class IntegrationQueue {
|
|
|
379
499
|
writeAtomic(this.resultPath(this.options.task), { ...result, state: "pending" });
|
|
380
500
|
}
|
|
381
501
|
|
|
382
|
-
/** The push landed
|
|
502
|
+
/** The push landed: a waiting withdrawal now reads the batch as merged. */
|
|
503
|
+
private markPushed(): void {
|
|
504
|
+
const result = this.readResult(this.options.task);
|
|
505
|
+
if (result === undefined) return;
|
|
506
|
+
writeAtomic(this.resultPath(this.options.task), { ...result, state: "pushed" });
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/** The leader has recorded the merge: the result is final. */
|
|
383
510
|
finish(): void {
|
|
384
511
|
const result = this.readResult(this.options.task);
|
|
385
512
|
if (result === undefined) return;
|
|
@@ -387,7 +514,31 @@ export class IntegrationQueue {
|
|
|
387
514
|
}
|
|
388
515
|
|
|
389
516
|
/** A claimed task is done with the result. */
|
|
390
|
-
done(): void {
|
|
391
|
-
this.leave();
|
|
517
|
+
async done(): Promise<void> {
|
|
518
|
+
await this.leave();
|
|
392
519
|
}
|
|
393
520
|
}
|
|
521
|
+
|
|
522
|
+
/**
|
|
523
|
+
* Takes a task out of the integration queue on behalf of a cancel, with the
|
|
524
|
+
* land lock a leader's last check-and-push takes. It answers `merged` when the
|
|
525
|
+
* leader already pushed a batch that lists the task. The ticket goes at once,
|
|
526
|
+
* before the cancelled run has seen its abort: that run can no longer push
|
|
527
|
+
* (its last check finds no ticket), and the next task may start its turn while
|
|
528
|
+
* the cancelled run is still winding down.
|
|
529
|
+
*/
|
|
530
|
+
export async function withdrawIntegration(
|
|
531
|
+
home: string,
|
|
532
|
+
repository: { readonly name: string; readonly path: string },
|
|
533
|
+
task: string,
|
|
534
|
+
): Promise<Withdrawal> {
|
|
535
|
+
return new IntegrationQueue({
|
|
536
|
+
home,
|
|
537
|
+
repository,
|
|
538
|
+
task,
|
|
539
|
+
run: "",
|
|
540
|
+
verified: "",
|
|
541
|
+
message: "",
|
|
542
|
+
say: () => {},
|
|
543
|
+
}).withdraw();
|
|
544
|
+
}
|