@operad-io/dev 0.1.1 → 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 +70 -16
- 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 +82 -42
- package/src/integration-queue.ts +23 -10
- package/src/lock.ts +101 -5
- package/src/mainclone.ts +3 -3
- package/src/model-error.ts +70 -0
- package/src/model.ts +48 -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 +116 -152
- 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,
|
|
@@ -32,12 +34,17 @@ import { runCheckCommands } from "./rerun.ts";
|
|
|
32
34
|
import { reportFlakyTests } from "./flaky.ts";
|
|
33
35
|
import { recordRerunPasses } from "./reruns.ts";
|
|
34
36
|
import { getPlan, refreshPlanCounts } from "./service.ts";
|
|
37
|
+
import {
|
|
38
|
+
commitOf,
|
|
39
|
+
git,
|
|
40
|
+
headOf,
|
|
41
|
+
isAncestor,
|
|
42
|
+
treeOf,
|
|
43
|
+
} from "./git.ts";
|
|
35
44
|
import {
|
|
36
45
|
cancelledStep,
|
|
37
46
|
claimAllows,
|
|
38
47
|
conflictedFiles,
|
|
39
|
-
git,
|
|
40
|
-
headOf,
|
|
41
48
|
mergeInProgress,
|
|
42
49
|
saveTaskGuarded,
|
|
43
50
|
taskContext,
|
|
@@ -105,20 +112,6 @@ function shortSha(sha: string): string {
|
|
|
105
112
|
return sha.slice(0, 7);
|
|
106
113
|
}
|
|
107
114
|
|
|
108
|
-
function revParse(cwd: string, ref: string): string | undefined {
|
|
109
|
-
const result = git(cwd, ["rev-parse", "--verify", "--quiet", `${ref}^{commit}`]);
|
|
110
|
-
return result.ok && result.out.length > 0 ? result.out : undefined;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/** True when `ancestor` is reachable from `descendant`. */
|
|
114
|
-
function contains(
|
|
115
|
-
cwd: string,
|
|
116
|
-
ancestor: string,
|
|
117
|
-
descendant: string,
|
|
118
|
-
): boolean {
|
|
119
|
-
return git(cwd, ["merge-base", "--is-ancestor", ancestor, descendant]).ok;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
115
|
interface Candidate {
|
|
123
116
|
readonly kind: "ok" | "conflict" | "failed";
|
|
124
117
|
readonly sha?: string | undefined;
|
|
@@ -263,12 +256,6 @@ function integrationData(
|
|
|
263
256
|
};
|
|
264
257
|
}
|
|
265
258
|
|
|
266
|
-
/** Step `dev/integrate`: land the verified commit on main. */
|
|
267
|
-
function treeOf(cwd: string, commit: string): string | undefined {
|
|
268
|
-
const result = git(cwd, ["rev-parse", "--verify", "--quiet", `${commit}^{tree}`]);
|
|
269
|
-
return result.ok && result.out.length > 0 ? result.out : undefined;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
259
|
/** The patch id of a diff, which ignores line numbers and whitespace. */
|
|
273
260
|
function patchIdOf(cwd: string, args: readonly string[]): string | undefined {
|
|
274
261
|
const diff = git(cwd, args, { raw: true });
|
|
@@ -285,7 +272,7 @@ function patchIdOf(cwd: string, args: readonly string[]): string | undefined {
|
|
|
285
272
|
function carrierOnMain(cwd: string, verified: string, main: string): string | undefined {
|
|
286
273
|
// A verified commit that main contains arrived in the first commit of main's
|
|
287
274
|
// own line that has it.
|
|
288
|
-
if (
|
|
275
|
+
if (isAncestor(cwd, verified, main)) {
|
|
289
276
|
const arrived = git(cwd, [
|
|
290
277
|
"rev-list",
|
|
291
278
|
"--first-parent",
|
|
@@ -308,6 +295,7 @@ function carrierOnMain(cwd: string, verified: string, main: string): string | un
|
|
|
308
295
|
return undefined;
|
|
309
296
|
}
|
|
310
297
|
|
|
298
|
+
/** Step `dev/integrate`: land the verified commit on main. */
|
|
311
299
|
export async function runIntegrate(
|
|
312
300
|
ctx: StepContext,
|
|
313
301
|
): Promise<PluginStepResult> {
|
|
@@ -363,12 +351,38 @@ export async function runIntegrate(
|
|
|
363
351
|
};
|
|
364
352
|
};
|
|
365
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
|
+
|
|
366
375
|
let flakyReason: string | undefined;
|
|
367
376
|
/** The other tasks this task was merged with in one batch. */
|
|
368
377
|
let batchWith: readonly string[] = [];
|
|
369
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;
|
|
370
384
|
const prepareMarker = (record: IntegrationRecord): PrepareMarker => ({
|
|
371
|
-
due: record.prepareDue === true,
|
|
385
|
+
due: record.prepareDue === true || leftDue,
|
|
372
386
|
save: async (due) => {
|
|
373
387
|
const pending: IntegrationRecord = {
|
|
374
388
|
...record,
|
|
@@ -390,18 +404,20 @@ export async function runIntegrate(
|
|
|
390
404
|
mergedLine: string = `merged as ${shortSha(sha)}`,
|
|
391
405
|
): Promise<PluginStepResult> => {
|
|
392
406
|
const note = record.checkNote;
|
|
407
|
+
const marker = prepareMarker(record);
|
|
393
408
|
const clone = await followMain(
|
|
394
409
|
context.repository,
|
|
395
410
|
settings.mainBranch,
|
|
396
411
|
cloneSha,
|
|
397
412
|
{ lines: settings.prepare, run: mainCloneRunner(ctx, settings) },
|
|
398
|
-
|
|
413
|
+
marker,
|
|
399
414
|
);
|
|
400
415
|
integrateTestHooks.at?.("clone-followed");
|
|
401
416
|
const cloneChange = mainCloneChange(clone);
|
|
417
|
+
// A clone that did not move keeps the prepare the killed run left due.
|
|
402
418
|
const withClone: IntegrationRecord = {
|
|
403
419
|
...record,
|
|
404
|
-
prepareDue:
|
|
420
|
+
prepareDue: !clone.updated && marker.due,
|
|
405
421
|
mainClone: { ...clone },
|
|
406
422
|
};
|
|
407
423
|
if (
|
|
@@ -448,11 +464,11 @@ export async function runIntegrate(
|
|
|
448
464
|
previous.candidate.length > 0
|
|
449
465
|
) {
|
|
450
466
|
const fetched = git(repository, ["fetch", "--prune", settings.remote]);
|
|
451
|
-
const remoteMain =
|
|
467
|
+
const remoteMain = commitOf(repository, mainRef);
|
|
452
468
|
if (
|
|
453
469
|
fetched.ok &&
|
|
454
470
|
remoteMain !== undefined &&
|
|
455
|
-
|
|
471
|
+
isAncestor(repository, previous.candidate, remoteMain)
|
|
456
472
|
) {
|
|
457
473
|
const record: IntegrationRecord = {
|
|
458
474
|
...previous,
|
|
@@ -470,17 +486,18 @@ export async function runIntegrate(
|
|
|
470
486
|
const previousBatch = Array.isArray(previous.batch)
|
|
471
487
|
? previous.batch.filter((name): name is string => typeof name === "string")
|
|
472
488
|
: [];
|
|
489
|
+
const marker = prepareMarker(record);
|
|
473
490
|
const clone = await followMain(
|
|
474
491
|
context.repository,
|
|
475
492
|
settings.mainBranch,
|
|
476
493
|
typeof previous.batchHead === "string" ? previous.batchHead : previous.candidate,
|
|
477
494
|
{ lines: settings.prepare, run: mainCloneRunner(ctx, settings) },
|
|
478
|
-
|
|
495
|
+
marker,
|
|
479
496
|
);
|
|
480
497
|
const cloneChange = mainCloneChange(clone);
|
|
481
498
|
const withClone: IntegrationRecord = {
|
|
482
499
|
...record,
|
|
483
|
-
prepareDue:
|
|
500
|
+
prepareDue: !clone.updated && marker.due,
|
|
484
501
|
mainClone: { ...clone },
|
|
485
502
|
};
|
|
486
503
|
if (
|
|
@@ -524,8 +541,8 @@ export async function runIntegrate(
|
|
|
524
541
|
wait: integrateTestHooks.wait,
|
|
525
542
|
landed: (result) => {
|
|
526
543
|
if (!git(repository, ["fetch", "--prune", settings.remote]).ok) return false;
|
|
527
|
-
const main =
|
|
528
|
-
return main !== undefined &&
|
|
544
|
+
const main = commitOf(repository, mainRef);
|
|
545
|
+
return main !== undefined && isAncestor(repository, result.head, main);
|
|
529
546
|
},
|
|
530
547
|
say: async (line, holder) => {
|
|
531
548
|
await ctx.logger.write(line);
|
|
@@ -551,7 +568,7 @@ export async function runIntegrate(
|
|
|
551
568
|
/** This task's merge commit when main already has it, and the main that has it. */
|
|
552
569
|
const alreadyOnMain = (): { sha: string; main: string } | undefined => {
|
|
553
570
|
if (!git(repository, ["fetch", "--prune", settings.remote]).ok) return undefined;
|
|
554
|
-
const main =
|
|
571
|
+
const main = commitOf(repository, mainRef);
|
|
555
572
|
if (main === undefined) return undefined;
|
|
556
573
|
const found = git(repository, [
|
|
557
574
|
"log",
|
|
@@ -584,7 +601,7 @@ export async function runIntegrate(
|
|
|
584
601
|
for (let round = 0; round < MAX_INTEGRATION_ROUNDS; round += 1) {
|
|
585
602
|
const fetched = git(repository, ["fetch", "--prune", settings.remote]);
|
|
586
603
|
if (!fetched.ok) return undefined;
|
|
587
|
-
const base =
|
|
604
|
+
const base = commitOf(repository, mainRef);
|
|
588
605
|
if (base === undefined) return undefined;
|
|
589
606
|
if (recreateGateWorktree(repository, gate, base, settings) !== undefined) {
|
|
590
607
|
return undefined;
|
|
@@ -622,6 +639,7 @@ export async function runIntegrate(
|
|
|
622
639
|
...(settings.gate.stallSecs === undefined
|
|
623
640
|
? {}
|
|
624
641
|
: { stallSecs: settings.gate.stallSecs }),
|
|
642
|
+
env: checkEnvironment(ctx.settings),
|
|
625
643
|
},
|
|
626
644
|
keyCommands,
|
|
627
645
|
stepOptions(ctx, gate),
|
|
@@ -638,6 +656,7 @@ export async function runIntegrate(
|
|
|
638
656
|
flakyLimit: settings.check.flakyLoadLimit,
|
|
639
657
|
signal: ctx.signal,
|
|
640
658
|
log: (text) => ctx.logger.write(text),
|
|
659
|
+
logPath: ctx.logger.path,
|
|
641
660
|
});
|
|
642
661
|
if (steady.cancelled === true) return cancelledStep();
|
|
643
662
|
const { checked } = steady;
|
|
@@ -742,22 +761,35 @@ export async function runIntegrate(
|
|
|
742
761
|
// A claimed task that left (cancelled) is not merged: one by one instead.
|
|
743
762
|
// The check and the push share the queue's land lock with a withdrawal,
|
|
744
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;
|
|
745
767
|
const landed = await queue.landWhenStillQueued(followers, result, async () => {
|
|
746
768
|
await integrateTestHooks.beforePush?.(batchHead);
|
|
747
|
-
|
|
769
|
+
if (!(await pushAllowed(batchHead, "the batch merge"))) return false;
|
|
770
|
+
const pushed = git(gate, [
|
|
748
771
|
"push",
|
|
749
772
|
settings.remote,
|
|
750
773
|
`HEAD:refs/heads/${settings.mainBranch}`,
|
|
751
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;
|
|
752
780
|
});
|
|
753
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
|
+
}
|
|
754
785
|
if (landed === "pushed") {
|
|
786
|
+
if (landedDespiteError) return land({ landed: true });
|
|
755
787
|
integrateTestHooks.afterPush?.(batchHead);
|
|
756
788
|
return land({});
|
|
757
789
|
}
|
|
758
790
|
git(repository, ["fetch", "--prune", settings.remote]);
|
|
759
|
-
const now =
|
|
760
|
-
if (now !== undefined &&
|
|
791
|
+
const now = commitOf(repository, mainRef);
|
|
792
|
+
if (now !== undefined && isAncestor(repository, batchHead, now)) {
|
|
761
793
|
return land({ landed: true });
|
|
762
794
|
}
|
|
763
795
|
if (now !== undefined && now !== base) continue;
|
|
@@ -785,6 +817,9 @@ export async function runIntegrate(
|
|
|
785
817
|
head: head ?? null,
|
|
786
818
|
headMoved,
|
|
787
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,
|
|
788
823
|
...(leaderResult.note === undefined ? {} : { checkNote: leaderResult.note }),
|
|
789
824
|
};
|
|
790
825
|
return recordMerged(record, mine.sha, {}, leaderResult.head);
|
|
@@ -807,7 +842,7 @@ export async function runIntegrate(
|
|
|
807
842
|
const landed = alreadyOnMain();
|
|
808
843
|
if (landed !== undefined) {
|
|
809
844
|
// The dead leader's note and batch are gone; main still has the base.
|
|
810
|
-
const landedBase =
|
|
845
|
+
const landedBase = commitOf(repository, `${landed.sha}^1`);
|
|
811
846
|
return recordMerged(
|
|
812
847
|
{
|
|
813
848
|
result: "merged",
|
|
@@ -854,7 +889,7 @@ export async function runIntegrate(
|
|
|
854
889
|
`cannot fetch "${settings.remote}": ${fetched.err || fetched.out || "git fetch failed"}`,
|
|
855
890
|
);
|
|
856
891
|
}
|
|
857
|
-
const base =
|
|
892
|
+
const base = commitOf(repository, mainRef);
|
|
858
893
|
if (base === undefined) {
|
|
859
894
|
return fail(`"${settings.remote}/${settings.mainBranch}" does not exist after the fetch`);
|
|
860
895
|
}
|
|
@@ -969,6 +1004,7 @@ export async function runIntegrate(
|
|
|
969
1004
|
...(settings.gate.stallSecs === undefined
|
|
970
1005
|
? {}
|
|
971
1006
|
: { stallSecs: settings.gate.stallSecs }),
|
|
1007
|
+
env: checkEnvironment(ctx.settings),
|
|
972
1008
|
},
|
|
973
1009
|
keyCommands,
|
|
974
1010
|
stepOptions(ctx, gate),
|
|
@@ -985,6 +1021,7 @@ export async function runIntegrate(
|
|
|
985
1021
|
flakyLimit: settings.check.flakyLoadLimit,
|
|
986
1022
|
signal: ctx.signal,
|
|
987
1023
|
log: (text) => ctx.logger.write(text),
|
|
1024
|
+
logPath: ctx.logger.path,
|
|
988
1025
|
});
|
|
989
1026
|
if (steady.cancelled === true) return cancelledStep();
|
|
990
1027
|
const { checked } = steady;
|
|
@@ -1117,6 +1154,9 @@ export async function runIntegrate(
|
|
|
1117
1154
|
}
|
|
1118
1155
|
integrateTestHooks.at?.("pending-saved");
|
|
1119
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
|
+
}
|
|
1120
1160
|
const push = git(gate, [
|
|
1121
1161
|
"push",
|
|
1122
1162
|
settings.remote,
|
|
@@ -1134,11 +1174,11 @@ export async function runIntegrate(
|
|
|
1134
1174
|
|
|
1135
1175
|
lastError = `cannot push "${settings.mainBranch}" to "${settings.remote}": ${push.err || push.out || "git push failed"}`;
|
|
1136
1176
|
git(repository, ["fetch", "--prune", settings.remote]);
|
|
1137
|
-
const now =
|
|
1177
|
+
const now = commitOf(repository, mainRef);
|
|
1138
1178
|
// A push can land on the remote and still report an error (the client lost
|
|
1139
1179
|
// the answer). The remote's main then contains the candidate: it is
|
|
1140
1180
|
// merged, and building and pushing a second candidate would be wrong.
|
|
1141
|
-
if (now !== undefined &&
|
|
1181
|
+
if (now !== undefined && isAncestor(repository, sha, now)) {
|
|
1142
1182
|
const record: IntegrationRecord = {
|
|
1143
1183
|
...pending,
|
|
1144
1184
|
result: "merged",
|
package/src/integration-queue.ts
CHANGED
|
@@ -24,12 +24,23 @@ import {
|
|
|
24
24
|
mkdirSync,
|
|
25
25
|
readdirSync,
|
|
26
26
|
readFileSync,
|
|
27
|
-
renameSync,
|
|
28
27
|
rmSync,
|
|
29
|
-
writeFileSync,
|
|
30
28
|
} from "node:fs";
|
|
31
29
|
import { join } from "node:path";
|
|
32
|
-
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;
|
|
33
44
|
|
|
34
45
|
/** How often a waiting integration looks again when nothing announced a change. */
|
|
35
46
|
const RECHECK_MS = 500;
|
|
@@ -117,7 +128,7 @@ export function integrationQueueDirectory(
|
|
|
117
128
|
home: string,
|
|
118
129
|
repository: { readonly name: string; readonly path: string },
|
|
119
130
|
): string {
|
|
120
|
-
const hash =
|
|
131
|
+
const hash = repositoryPathKey(repository.path);
|
|
121
132
|
const name = repository.name.replace(/[^A-Za-z0-9._-]/g, "_");
|
|
122
133
|
return join(home, "dev", "integration-queue", `${name}-${hash.slice(0, 8)}`);
|
|
123
134
|
}
|
|
@@ -140,16 +151,14 @@ function landLockPath(directory: string): string {
|
|
|
140
151
|
}
|
|
141
152
|
|
|
142
153
|
function writeAtomic(path: string, value: unknown): void {
|
|
143
|
-
|
|
144
|
-
writeFileSync(temporary, JSON.stringify(value));
|
|
145
|
-
renameSync(temporary, path);
|
|
154
|
+
writeJsonAtomicSync(path, value, { newline: false });
|
|
146
155
|
}
|
|
147
156
|
|
|
148
157
|
function parseTicket(text: string): Ticket | undefined {
|
|
149
158
|
try {
|
|
150
159
|
const value: unknown = JSON.parse(text);
|
|
151
|
-
if (
|
|
152
|
-
const record = value
|
|
160
|
+
if (!isRecord(value)) return undefined;
|
|
161
|
+
const record = value;
|
|
153
162
|
if (
|
|
154
163
|
typeof record.seq !== "number" ||
|
|
155
164
|
typeof record.task !== "string" ||
|
|
@@ -205,6 +214,7 @@ export class IntegrationQueue {
|
|
|
205
214
|
return withLockFile(landLockPath(this.directory), work, {
|
|
206
215
|
label: "integration queue",
|
|
207
216
|
onLog: () => {},
|
|
217
|
+
timeoutMs: LAND_LOCK_TIMEOUT_MS,
|
|
208
218
|
...(wait === undefined ? {} : { wait }),
|
|
209
219
|
});
|
|
210
220
|
}
|
|
@@ -512,7 +522,10 @@ export class IntegrationQueue {
|
|
|
512
522
|
/**
|
|
513
523
|
* Takes a task out of the integration queue on behalf of a cancel, with the
|
|
514
524
|
* land lock a leader's last check-and-push takes. It answers `merged` when the
|
|
515
|
-
* leader already pushed a batch that lists the task.
|
|
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.
|
|
516
529
|
*/
|
|
517
530
|
export async function withdrawIntegration(
|
|
518
531
|
home: string,
|
package/src/lock.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/** The repository lock: one dev writer per repository, safe across processes. */
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { existsSync, readFileSync, realpathSync, statSync, unlinkSync } from "node:fs";
|
|
4
|
+
import { join, resolve } from "node:path";
|
|
5
|
+
import { defaultOperadHome } from "@operad-io/engine";
|
|
4
6
|
import {
|
|
5
7
|
LOCK_TIMEOUT_MS,
|
|
6
8
|
LockTimeoutError,
|
|
@@ -9,8 +11,100 @@ import {
|
|
|
9
11
|
} from "@operad-io/engine/platform";
|
|
10
12
|
import { DevError } from "./model.ts";
|
|
11
13
|
|
|
12
|
-
/**
|
|
13
|
-
|
|
14
|
+
/**
|
|
15
|
+
* What a waiter may inject: the clock, how it waits, where a log goes, and the
|
|
16
|
+
* operad home that holds the lock file.
|
|
17
|
+
*/
|
|
18
|
+
export type RepositoryLockOptions = Omit<LockFileOptions, "label"> & {
|
|
19
|
+
/** The home whose `dev/repository-locks` holds the file; defaults to `$OPERAD_HOME`. */
|
|
20
|
+
readonly home?: string | undefined;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** Where an older dev kept its lock, inside the working tree. */
|
|
24
|
+
const LEGACY_LOCK_FILE = join(".operad", "dev.lock");
|
|
25
|
+
|
|
26
|
+
function realpathOr(path: string): string {
|
|
27
|
+
try {
|
|
28
|
+
return realpathSync.native(path);
|
|
29
|
+
} catch {
|
|
30
|
+
return path;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The git directory a repository shares with its worktrees. A normal checkout
|
|
36
|
+
* has `.git` as a directory; a linked worktree has a `.git` file naming its git
|
|
37
|
+
* directory, whose `commondir` file names the shared one. Reading both keeps
|
|
38
|
+
* the lock path off a git process and off the working tree.
|
|
39
|
+
*/
|
|
40
|
+
function commonGitDirectory(repositoryPath: string): string {
|
|
41
|
+
const dotGit = join(repositoryPath, ".git");
|
|
42
|
+
try {
|
|
43
|
+
if (statSync(dotGit).isDirectory()) return realpathOr(dotGit);
|
|
44
|
+
const line = /^gitdir:\s*(.+?)\s*$/m.exec(readFileSync(dotGit, "utf8"));
|
|
45
|
+
const named = line?.[1];
|
|
46
|
+
if (named !== undefined) {
|
|
47
|
+
const gitDirectory = resolve(repositoryPath, named);
|
|
48
|
+
const commonFile = join(gitDirectory, "commondir");
|
|
49
|
+
if (existsSync(commonFile)) {
|
|
50
|
+
const common = readFileSync(commonFile, "utf8").trim();
|
|
51
|
+
if (common.length > 0) return realpathOr(resolve(gitDirectory, common));
|
|
52
|
+
}
|
|
53
|
+
return realpathOr(gitDirectory);
|
|
54
|
+
}
|
|
55
|
+
} catch {
|
|
56
|
+
// Not a readable git checkout; its own path is the best identity left.
|
|
57
|
+
}
|
|
58
|
+
return realpathOr(resolve(repositoryPath));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The identity of a repository, shared by every worktree, so a lock taken
|
|
63
|
+
* through one holds through all.
|
|
64
|
+
*/
|
|
65
|
+
function repositoryKey(repositoryPath: string): string {
|
|
66
|
+
return new Bun.CryptoHasher("sha1")
|
|
67
|
+
.update(commonGitDirectory(repositoryPath))
|
|
68
|
+
.digest("hex");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The lock file of one repository. It lives in the operad home, never in the
|
|
73
|
+
* working tree: a held lock must not make the main clone look dirty to a merge.
|
|
74
|
+
*/
|
|
75
|
+
export function repositoryLockPath(
|
|
76
|
+
repositoryPath: string,
|
|
77
|
+
home: string = defaultOperadHome(),
|
|
78
|
+
): string {
|
|
79
|
+
return join(home, "dev", "repository-locks", `${repositoryKey(repositoryPath)}.lock`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Removes the lock older versions left in the working tree, but only when
|
|
84
|
+
* nobody holds it. An old holder keeps it until it ends; that lock is not ours
|
|
85
|
+
* and its process still relies on it.
|
|
86
|
+
*/
|
|
87
|
+
async function removeLegacyLock(repositoryPath: string): Promise<void> {
|
|
88
|
+
const legacy = join(repositoryPath, LEGACY_LOCK_FILE);
|
|
89
|
+
if (!existsSync(legacy)) return;
|
|
90
|
+
const held = new Error("the legacy dev lock is held");
|
|
91
|
+
try {
|
|
92
|
+
await withLockFile(
|
|
93
|
+
legacy,
|
|
94
|
+
() => {
|
|
95
|
+
unlinkSync(legacy);
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
label: "dev lock",
|
|
99
|
+
onLog: () => {},
|
|
100
|
+
timeoutMs: 0,
|
|
101
|
+
wait: () => Promise.reject(held),
|
|
102
|
+
},
|
|
103
|
+
);
|
|
104
|
+
} catch {
|
|
105
|
+
// A held or unreadable old lock is left as it is; the new lock is elsewhere.
|
|
106
|
+
}
|
|
107
|
+
}
|
|
14
108
|
|
|
15
109
|
/**
|
|
16
110
|
* Holds one repository's lock while ids and counts change. The lock file makes
|
|
@@ -21,9 +115,11 @@ export async function withRepositoryLock<T>(
|
|
|
21
115
|
work: () => Promise<T> | T,
|
|
22
116
|
options: RepositoryLockOptions = {},
|
|
23
117
|
): Promise<T> {
|
|
24
|
-
|
|
118
|
+
await removeLegacyLock(repositoryPath);
|
|
119
|
+
const { home, ...lockOptions } = options;
|
|
120
|
+
const lockPath = repositoryLockPath(repositoryPath, home);
|
|
25
121
|
try {
|
|
26
|
-
return await withLockFile(lockPath, work, { ...
|
|
122
|
+
return await withLockFile(lockPath, work, { ...lockOptions, label: "dev lock" });
|
|
27
123
|
} catch (error) {
|
|
28
124
|
if (error instanceof LockTimeoutError) {
|
|
29
125
|
throw new DevError(
|
package/src/mainclone.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* through git is ever used: no reset, rebase or stash.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import { git } from "./
|
|
8
|
+
import { git, isAncestor } from "./git.ts";
|
|
9
9
|
|
|
10
10
|
/** What a command runner reports. */
|
|
11
11
|
export interface CommandResult {
|
|
@@ -115,7 +115,7 @@ export async function updateMainClone(
|
|
|
115
115
|
if (status.out.length > 0) {
|
|
116
116
|
return { updated: false, note: "uncommitted changes" };
|
|
117
117
|
}
|
|
118
|
-
const already =
|
|
118
|
+
const already = isAncestor(repository, sha, "HEAD");
|
|
119
119
|
if (already) {
|
|
120
120
|
if (marker?.due !== true) return { updated: true };
|
|
121
121
|
if (lines.length === 0) {
|
|
@@ -131,7 +131,7 @@ export async function updateMainClone(
|
|
|
131
131
|
}
|
|
132
132
|
const merge = git(repository, ["merge", "--ff-only", sha]);
|
|
133
133
|
if (!merge.ok) {
|
|
134
|
-
if (lines.length > 0 && marker !== undefined) await marker.save(
|
|
134
|
+
if (lines.length > 0 && marker !== undefined) await marker.save(marker.due);
|
|
135
135
|
return {
|
|
136
136
|
updated: false,
|
|
137
137
|
note: `cannot fast-forward: ${lastLine(merge.err || merge.out) || "git merge failed"}`,
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a harness's `modelError` says, as dev reads it. A harness reports the
|
|
3
|
+
* kind, the model's own message and, when it knows it, how long to wait.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { isRecord } from "@operad-io/engine";
|
|
7
|
+
import { MAX_MODEL_RETRY_SECS } from "./timeouts.ts";
|
|
8
|
+
|
|
9
|
+
export interface ModelErrorInfo {
|
|
10
|
+
/** `unavailable` or `usage_limit`; another kind is not a model outage. */
|
|
11
|
+
readonly kind: string;
|
|
12
|
+
/** The model's own message, when it gave one. */
|
|
13
|
+
readonly message?: string;
|
|
14
|
+
/** How long the model said to wait, in seconds; absent when it did not say. */
|
|
15
|
+
readonly retryAfterSecs?: number;
|
|
16
|
+
/** When the model reported the wait, in epoch milliseconds; absent when unknown. */
|
|
17
|
+
readonly reportedAt?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** A positive epoch time, or undefined when the value is not one. */
|
|
21
|
+
function positiveTime(value: unknown): number | undefined {
|
|
22
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0
|
|
23
|
+
? value
|
|
24
|
+
: undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function retryAfterSecs(value: unknown): number | undefined {
|
|
28
|
+
return positiveTime(value);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A normalized model error, or undefined when the value is not one. */
|
|
32
|
+
export function readModelError(value: unknown): ModelErrorInfo | undefined {
|
|
33
|
+
if (!isRecord(value) || typeof value.kind !== "string") return undefined;
|
|
34
|
+
if (value.kind !== "unavailable" && value.kind !== "usage_limit") {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
const message =
|
|
38
|
+
typeof value.message === "string" && value.message.length > 0
|
|
39
|
+
? value.message
|
|
40
|
+
: undefined;
|
|
41
|
+
const retry = retryAfterSecs(value.retry_after_secs);
|
|
42
|
+
const reported = positiveTime(value.reported_at);
|
|
43
|
+
return {
|
|
44
|
+
kind: value.kind,
|
|
45
|
+
...(message === undefined ? {} : { message }),
|
|
46
|
+
...(retry === undefined ? {} : { retryAfterSecs: retry }),
|
|
47
|
+
...(reported === undefined ? {} : { reportedAt: reported }),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The model error a harness step put in its result data, when it put one. */
|
|
52
|
+
export function modelErrorIn(
|
|
53
|
+
data: Readonly<Record<string, unknown>> | undefined,
|
|
54
|
+
): ModelErrorInfo | undefined {
|
|
55
|
+
return readModelError(data?.modelError);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* True when dev must not retry the run on its own. A usage limit is not an
|
|
60
|
+
* outage to wait out: the account cannot serve until its period ends. A
|
|
61
|
+
* retry-after longer than half an hour is the same kind of wait, and a person
|
|
62
|
+
* decides whether to switch the worker or wait for the model.
|
|
63
|
+
*/
|
|
64
|
+
export function modelWaitsForPerson(error: ModelErrorInfo): boolean {
|
|
65
|
+
return (
|
|
66
|
+
error.kind === "usage_limit" ||
|
|
67
|
+
(error.retryAfterSecs !== undefined &&
|
|
68
|
+
error.retryAfterSecs > MAX_MODEL_RETRY_SECS)
|
|
69
|
+
);
|
|
70
|
+
}
|