@mnaimfaizy/agent-kit 1.0.0-beta.16 → 1.0.0-beta.17
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 +2 -1
- package/package.json +1 -1
- package/src/cli.mjs +23 -6
- package/src/deep-scan/docker-driver.mjs +70 -3
- package/src/deep-scan/lifecycle.mjs +2 -2
- package/src/deep-scan/sbx-driver.mjs +34 -47
- package/src/exec.mjs +5 -1
- package/src/factory/budget/budget.mjs +2 -1
- package/src/factory/doctor/files.mjs +21 -1
- package/src/factory/doctor/local.mjs +24 -8
- package/src/factory/doctor/orchestrate.mjs +14 -14
- package/src/factory/init/blueprint.mjs +9 -2
- package/src/factory/init/index.mjs +4 -3
- package/src/factory/limits/limits.mjs +8 -0
- package/src/factory/qa/command.mjs +139 -1
- package/src/factory/qa/decide.mjs +86 -0
- package/src/factory/qa/report.mjs +50 -0
- package/src/factory/review/brief.mjs +195 -0
- package/src/factory/run/command.mjs +90 -24
- package/src/factory/run/launch.mjs +25 -6
- package/src/factory/run/pipelines.mjs +467 -34
- package/src/factory/run/token.mjs +5 -3
- package/src/factory/run/worktree.mjs +160 -0
- package/src/factory/security/audit.mjs +353 -0
- package/src/factory/setup/command.mjs +80 -17
- package/src/factory/setup/reads.mjs +30 -0
- package/src/factory/setup/writes.mjs +12 -0
- package/src/factory/sweep/actions-main.mjs +5 -3
- package/src/factory/sweep/shell.mjs +7 -3
- package/src/factory/sweep/sweep-core.mjs +240 -12
- package/src/factory/tracker/github.mjs +57 -18
- package/src/stage/build.mjs +100 -0
- package/src/stage/factory-live.mjs +288 -0
- package/src/stage.mjs +81 -4
|
@@ -77,6 +77,25 @@ export async function readControlIssue(gh, repo) {
|
|
|
77
77
|
return issues.find((i) => isControlBody(i.body))?.number ?? null;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Whether an issue is locked to collaborators (`locked` on the issue object). Anything but a plain
|
|
82
|
+
* `true` or `false` throws, so setup never locks on a guess.
|
|
83
|
+
*/
|
|
84
|
+
export async function readIssueLocked(gh, repo, number) {
|
|
85
|
+
const r = await gh(["api", `repos/${repo}/issues/${number}`, "--jq", ".locked"]);
|
|
86
|
+
const text = String(r.stdout ?? "").trim();
|
|
87
|
+
if (r.status !== 0 || !["true", "false"].includes(text)) {
|
|
88
|
+
throw new ReadError(
|
|
89
|
+
`read issue #${number}: ${
|
|
90
|
+
String(r.stderr || text)
|
|
91
|
+
.trim()
|
|
92
|
+
.split("\n")[0] || "gh gave no locked state"
|
|
93
|
+
}`,
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
return text === "true";
|
|
97
|
+
}
|
|
98
|
+
|
|
80
99
|
export async function readPinned(gh, repo) {
|
|
81
100
|
const [owner, name] = repo.split("/");
|
|
82
101
|
const data = await json(
|
|
@@ -127,3 +146,14 @@ export async function readRulesetNames(gh, repo) {
|
|
|
127
146
|
const rulesets = await json(gh, ["api", `repos/${repo}/rulesets`], "read the rulesets");
|
|
128
147
|
return new Set(rulesets.map((r) => r.name));
|
|
129
148
|
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* The numeric user ID of an App's bot, from the public `GET /users/<slug>[bot]` (the brackets
|
|
152
|
+
* URL-encoded). The Dispatcher recognises the Factory's own App by it (#640). Returns null when
|
|
153
|
+
* GitHub does not answer with a whole number: setup never guesses.
|
|
154
|
+
*/
|
|
155
|
+
export async function readBotId(gh, slug) {
|
|
156
|
+
const r = await gh(["api", `users/${encodeURIComponent(`${slug}[bot]`)}`, "--jq", ".id"]);
|
|
157
|
+
const id = String(r.stdout ?? "").trim();
|
|
158
|
+
return r.status === 0 && /^[1-9]\d*$/.test(id) ? id : null;
|
|
159
|
+
}
|
|
@@ -52,6 +52,18 @@ export async function pinIssue(gh, repo, number) {
|
|
|
52
52
|
return done(r, `gh issue pin ${number}`);
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Lock an issue to collaborators: `PUT /repos/{repo}/issues/{n}/lock`, no reason given. On a
|
|
57
|
+
* public repository only people with write access can then comment. Locking a locked issue
|
|
58
|
+
* is the state we want, and GitHub answers it the same way.
|
|
59
|
+
*/
|
|
60
|
+
export async function lockIssue(gh, repo, number) {
|
|
61
|
+
return done(
|
|
62
|
+
await gh(["api", "--method", "PUT", `repos/${repo}/issues/${number}/lock`]),
|
|
63
|
+
`lock issue ${number}`,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
55
67
|
/**
|
|
56
68
|
* The `factory-publish` environment, its deployment branches limited to the
|
|
57
69
|
* default branch. Refused on a private repository of a Free plan (environment
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
// cli/src/factory/build/command.mjs. `review-target`, `review-start` and
|
|
12
12
|
// `review-check` are the review Stage's trusted steps (the `factory-review`
|
|
13
13
|
// Caller's own jobs): see cli/src/factory/review/command.mjs. `qa-target`,
|
|
14
|
-
// `qa-start` and `qa-check` are the QA Stage's (the `factory-qa` Caller's own
|
|
14
|
+
// `qa-start`, `qa-declare` and `qa-check` are the QA Stage's (the `factory-qa` Caller's own
|
|
15
15
|
// jobs): see cli/src/factory/qa/command.mjs. `security-target`, `security-start`
|
|
16
16
|
// and `security-check` are the security Stage's (the `factory-security` Caller's
|
|
17
17
|
// own jobs): see cli/src/factory/security/command.mjs. `stage-limits` and
|
|
@@ -27,7 +27,7 @@ import { runBuildCheck, runBuildPlan, runBuildPr } from "../build/command.mjs";
|
|
|
27
27
|
import { runPlanAnswers } from "../decision/command.mjs";
|
|
28
28
|
import { runMergeRecord, runMergeRun } from "../merge/command.mjs";
|
|
29
29
|
import { runPlanPublish } from "../plan/command.mjs";
|
|
30
|
-
import { runQaCheck, runQaStart, runQaTarget } from "../qa/command.mjs";
|
|
30
|
+
import { runQaCheck, runQaDeclare, runQaStart, runQaTarget } from "../qa/command.mjs";
|
|
31
31
|
import { runReviewCheck, runReviewStart, runReviewTarget } from "../review/command.mjs";
|
|
32
32
|
import { runSecurityCheck, runSecurityStart, runSecurityTarget } from "../security/command.mjs";
|
|
33
33
|
import { runFromEnvironment } from "./run.mjs";
|
|
@@ -93,6 +93,8 @@ if (command === undefined) {
|
|
|
93
93
|
process.exitCode = runQaTarget({ ...io, args: rest, appendOutput });
|
|
94
94
|
} else if (command === "qa-start") {
|
|
95
95
|
process.exitCode = runQaStart({ ...io, args: rest });
|
|
96
|
+
} else if (command === "qa-declare") {
|
|
97
|
+
process.exitCode = runQaDeclare({ ...io, args: rest });
|
|
96
98
|
} else if (command === "qa-check") {
|
|
97
99
|
process.exitCode = runQaCheck({ ...io, args: rest, appendOutput });
|
|
98
100
|
} else if (command === "security-target") {
|
|
@@ -111,7 +113,7 @@ if (command === undefined) {
|
|
|
111
113
|
process.exitCode = runMergeRecord({ ...io, args: rest });
|
|
112
114
|
} else {
|
|
113
115
|
io.err(
|
|
114
|
-
`unknown command ${JSON.stringify(command)}: run with no argument to sweep, or plan-publish, plan-answers, build-plan, build-check, build-pr, review-target, review-start, review-check, qa-target, qa-start, qa-check, security-target, security-start, security-check, stage-limits, stage-release, merge-run or merge-record`,
|
|
116
|
+
`unknown command ${JSON.stringify(command)}: run with no argument to sweep, or plan-publish, plan-answers, build-plan, build-check, build-pr, review-target, review-start, review-check, qa-target, qa-start, qa-declare, qa-check, security-target, security-start, security-check, stage-limits, stage-release, merge-run or merge-record`,
|
|
115
117
|
);
|
|
116
118
|
process.exitCode = 2;
|
|
117
119
|
}
|
|
@@ -124,7 +124,7 @@ export function runSweep({
|
|
|
124
124
|
});
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
|
-
if (!off) attachSpend(read, { tracker, config, now: clock(), log });
|
|
127
|
+
if (!off) attachSpend(read, { tracker, engine, config, now: clock(), log });
|
|
128
128
|
if (!off) attachSplitParents(read, tracker, log);
|
|
129
129
|
if (!off && tracker.readWriters) attachWriters(read, tracker, engine, log);
|
|
130
130
|
if (!off && tracker.readWriters) attachMovers(read, tracker, log);
|
|
@@ -368,7 +368,7 @@ function attachMovers(read, tracker, log) {
|
|
|
368
368
|
* notices go). A read that fails is carried as `{ error }`, which the core reads as "no new
|
|
369
369
|
* Claim this sweep".
|
|
370
370
|
*/
|
|
371
|
-
function attachSpend(read, { tracker, config, now, log }) {
|
|
371
|
+
function attachSpend(read, { tracker, engine, config, now, log }) {
|
|
372
372
|
const cap = effectiveCap({ budget: config?.budget, override: config?.daily_override });
|
|
373
373
|
if (cap.none || cap.error) {
|
|
374
374
|
if (cap.error) log(`notice: the Daily cap is not readable (${cap.error}): no new Claim`);
|
|
@@ -378,7 +378,11 @@ function attachSpend(read, { tracker, config, now, log }) {
|
|
|
378
378
|
try {
|
|
379
379
|
if (typeof tracker.readReleases !== "function")
|
|
380
380
|
throw new Error("the tracker cannot read releases");
|
|
381
|
-
|
|
381
|
+
// Only the engine's own comments have their editor read (an outsider's cannot be trusted
|
|
382
|
+
// evidence anyway, and must not be able to stop the read).
|
|
383
|
+
const isEngine = (login) =>
|
|
384
|
+
lower(login).replace(/\[bot\]$/, "") === lower(engine.identity).replace(/\[bot\]$/, "");
|
|
385
|
+
read.spend = { comments: tracker.readReleases(since, { isEngine }) };
|
|
382
386
|
} catch (error) {
|
|
383
387
|
read.spend = { error: error instanceof Error ? error.message : String(error) };
|
|
384
388
|
log(`notice: tokens spent today could not be read (${read.spend.error}): no new Claim`);
|
|
@@ -104,8 +104,13 @@
|
|
|
104
104
|
// `read.control`):
|
|
105
105
|
//
|
|
106
106
|
// a Claim to write -> only if spent today + the full cap of every Stage still running
|
|
107
|
+
// (once per Stage: a live Claim and its pending check are one cap;
|
|
108
|
+
// QA counts 0, so does a Stage the engine's `stages` list leaves out,
|
|
109
|
+
// and a `checking` task with no pull request head holds nothing)
|
|
107
110
|
// + its own cap fit the cap; else the task waits (a notice) and a
|
|
108
111
|
// smaller, newer one may start before it
|
|
112
|
+
// a Claim with no release -> counts its full cap in spent (open or not, read or not by the
|
|
113
|
+
// sweep), reserved while unexpired on an issue the sweep did not read
|
|
109
114
|
// spent >= 80%, >= 100% -> budget-notice on the control issue, once per threshold per day
|
|
110
115
|
// no readable cap or spend -> no new Claim; running Stages and merges go on
|
|
111
116
|
//
|
|
@@ -146,14 +151,14 @@ import {
|
|
|
146
151
|
todaysReleases,
|
|
147
152
|
tokensSpent,
|
|
148
153
|
} from "../budget/budget.mjs";
|
|
149
|
-
import { taskSizeOf } from "../limits/limits.mjs";
|
|
154
|
+
import { stageSpendsTokens, taskSizeOf } from "../limits/limits.mjs";
|
|
150
155
|
import { decisionForPlan } from "../decision/plan.mjs";
|
|
151
156
|
import { parseDecisionRequest, renderDecisionRequest } from "../decision/request.mjs";
|
|
152
157
|
import { gateActions } from "../gate/task.mjs";
|
|
153
158
|
import { evidenceOf } from "../labels/evidence.mjs";
|
|
154
159
|
import { reconcileIssue } from "../labels/reconcile.mjs";
|
|
155
160
|
import { restartsOf } from "../labels/revert.mjs";
|
|
156
|
-
import { applyVoid } from "../labels/voided.mjs";
|
|
161
|
+
import { applyVoid, editedByOther } from "../labels/voided.mjs";
|
|
157
162
|
import { hasNote, noteMarker, parseClaim, renderClaimComment } from "./markers.mjs";
|
|
158
163
|
import { VERSION_PATTERN, VERSION_TOKEN } from "./marker-version.mjs";
|
|
159
164
|
import { parseReleaseComment } from "../claims/release.mjs";
|
|
@@ -188,6 +193,10 @@ const buildPullOf = (issue, claims) => {
|
|
|
188
193
|
: undefined;
|
|
189
194
|
};
|
|
190
195
|
|
|
196
|
+
/** The Stages that run from `checking`, each under its own Claim. */
|
|
197
|
+
const CHECK_STAGES = ["qa", "security"];
|
|
198
|
+
const CHECK_STAGE_NAMES = { qa: "QA", security: "security" };
|
|
199
|
+
|
|
191
200
|
/** The marker of the engine's note that a Stage check failed on a head. */
|
|
192
201
|
const checkFailedMarker = (stage, head) =>
|
|
193
202
|
`<!-- factory:check-failed${VERSION_TOKEN} stage=${stage} head=${head} -->`;
|
|
@@ -538,9 +547,39 @@ export function sweep({ read, config, killSwitch, now, ctx }) {
|
|
|
538
547
|
}
|
|
539
548
|
return sizes.get(number);
|
|
540
549
|
};
|
|
550
|
+
// What a Stage run is charged when its total is missing, unknown or edited: its full cap,
|
|
551
|
+
// except a Stage that spends no tokens (QA). A Stage the engine does not run is still
|
|
552
|
+
// charged: it may have run earlier today, and tampering must never save budget.
|
|
553
|
+
const spendCapFor = (stage, number) =>
|
|
554
|
+
!stageSpendsTokens(stage)
|
|
555
|
+
? 0
|
|
556
|
+
: budgetStageCap({ limits, stage, size: stage === "build" ? sizeFor(number) : undefined });
|
|
557
|
+
// What a Stage still running holds: also nothing for a Stage the engine does not run (its own
|
|
558
|
+
// list of Stages, `engine.stages`; an engine with no list runs every Stage), which cannot run.
|
|
559
|
+
const engineStages = Array.isArray(ctx.engine.stages) ? ctx.engine.stages : null;
|
|
541
560
|
const capFor = (stage, number) =>
|
|
542
|
-
|
|
561
|
+
engineStages && !engineStages.includes(stage) ? 0 : spendCapFor(stage, number);
|
|
543
562
|
state.capFor = capFor;
|
|
563
|
+
// A voided Claim (edited by someone else) is no evidence of its stage: it counts the largest
|
|
564
|
+
// Stage cap. Null when a cap cannot be read.
|
|
565
|
+
const worstSpendStage = (number) => {
|
|
566
|
+
let worst = { stage: "build", cap: -1 };
|
|
567
|
+
for (const stage of ["plan", "build", "review", "security"]) {
|
|
568
|
+
const cap = spendCapFor(stage, number);
|
|
569
|
+
if (cap === null) return null;
|
|
570
|
+
if (cap > worst.cap) worst = { stage, cap };
|
|
571
|
+
}
|
|
572
|
+
return worst.stage;
|
|
573
|
+
};
|
|
574
|
+
// What each running Stage of an issue holds: `Map<issue, Map<stage, tokens>>`.
|
|
575
|
+
const heldBy = new Map();
|
|
576
|
+
state.heldBy = heldBy;
|
|
577
|
+
const hold = (number, stage, tokens) => {
|
|
578
|
+
const stages = heldBy.get(number) ?? new Map();
|
|
579
|
+
stages.set(stage, (stages.get(stage) ?? 0) + tokens);
|
|
580
|
+
heldBy.set(number, stages);
|
|
581
|
+
state.reservedBy.set(number, (state.reservedBy.get(number) ?? 0) + tokens);
|
|
582
|
+
};
|
|
544
583
|
const releases = todaysReleases({ comments: read.spend.comments, isEngine, nowMs });
|
|
545
584
|
const dayStart = budgetDayStart(nowMs);
|
|
546
585
|
const missing = [];
|
|
@@ -557,7 +596,16 @@ export function sweep({ read, config, killSwitch, now, ctx }) {
|
|
|
557
596
|
state.blocked = `the ${claim.stage} Stage's token cap is not readable`;
|
|
558
597
|
return state;
|
|
559
598
|
}
|
|
560
|
-
|
|
599
|
+
hold(raw.number, claim.stage, own);
|
|
600
|
+
} else if (claim.voided && Date.parse(claim.createdAt) >= dayStart) {
|
|
601
|
+
// Edited by someone else: void, and what it says of its stage, nonce and release is no
|
|
602
|
+
// evidence, so no release covers it and it counts the largest Stage cap.
|
|
603
|
+
const stage = worstSpendStage(raw.number);
|
|
604
|
+
if (stage === null) {
|
|
605
|
+
state.blocked = "a Stage's token cap is not readable";
|
|
606
|
+
return state;
|
|
607
|
+
}
|
|
608
|
+
missing.push({ stage, issue: raw.number });
|
|
561
609
|
} else if (
|
|
562
610
|
!live &&
|
|
563
611
|
claim.released !== "lost-race" &&
|
|
@@ -569,25 +617,63 @@ export function sweep({ read, config, killSwitch, now, ctx }) {
|
|
|
569
617
|
}
|
|
570
618
|
}
|
|
571
619
|
// A task in `checking` has its QA and security Stages running until their checks answer.
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
620
|
+
// The Claim loop above already holds a Stage with a live Claim; with no pull request head
|
|
621
|
+
// nothing starts, so nothing is held.
|
|
622
|
+
const pull =
|
|
623
|
+
stateOfLabel(stateLabels(view)[0] ?? "") === "checking" && !paused
|
|
624
|
+
? buildPullOf(view, claimsHere)
|
|
625
|
+
: null;
|
|
626
|
+
if (pull?.head) {
|
|
627
|
+
const statuses = read.statuses?.[pull.head];
|
|
575
628
|
for (const [stage, context] of [
|
|
576
629
|
["qa", QA_CONTEXT],
|
|
577
630
|
["security", SECURITY_CONTEXT],
|
|
578
631
|
]) {
|
|
579
632
|
const check = engineStageCheck(statuses, context, isEngine);
|
|
580
633
|
if (check && check.state !== "pending") continue;
|
|
634
|
+
if (heldBy.get(raw.number)?.has(stage)) continue;
|
|
581
635
|
const own = capFor(stage, raw.number);
|
|
582
636
|
if (own === null) {
|
|
583
637
|
state.blocked = `the ${stage} Stage's token cap is not readable`;
|
|
584
638
|
return state;
|
|
585
639
|
}
|
|
586
|
-
|
|
640
|
+
hold(raw.number, stage, own);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
// Claims on issues the sweep did not read (closed ones, a split parent that lost its state
|
|
645
|
+
// label): the same rules as above, from the Claim comments the spend read keeps.
|
|
646
|
+
for (const comment of read.spend.comments) {
|
|
647
|
+
if (!isEngine(comment.author) || views.has(comment.issue)) continue;
|
|
648
|
+
const claim = parseClaim(comment.body);
|
|
649
|
+
if (!claim || claim.engine !== ctx.engine.name) continue;
|
|
650
|
+
const createdAt = Date.parse(comment.createdAt);
|
|
651
|
+
if (!(createdAt >= dayStart)) continue;
|
|
652
|
+
// A Claim someone else edited is void (the read names the editor of every edited Claim): as
|
|
653
|
+
// `claimsOf` does, it loses its `released` mark, and what it says of its stage and nonce is
|
|
654
|
+
// not trusted, so no release covers it and it counts the largest Stage cap.
|
|
655
|
+
const voided = editedByOther(comment);
|
|
656
|
+
if (!voided && releases.has(`${claim.stage}:${claim.claim}`)) continue;
|
|
657
|
+
if (!voided && claim.released === "lost-race") continue;
|
|
658
|
+
const live = claim.released === undefined && !voided && Date.parse(claim.expires) > nowMs;
|
|
659
|
+
if (live) {
|
|
660
|
+
// Nothing is watching that run: it holds its full cap while it may be running.
|
|
661
|
+
const own = capFor(claim.stage, comment.issue);
|
|
662
|
+
if (own === null) {
|
|
663
|
+
state.blocked = `the ${claim.stage} Stage's token cap is not readable`;
|
|
664
|
+
return state;
|
|
587
665
|
}
|
|
666
|
+
hold(comment.issue, claim.stage, own);
|
|
667
|
+
} else {
|
|
668
|
+
const stage = voided ? worstSpendStage(comment.issue) : claim.stage;
|
|
669
|
+
if (stage === null) {
|
|
670
|
+
state.blocked = "a Stage's token cap is not readable";
|
|
671
|
+
return state;
|
|
672
|
+
}
|
|
673
|
+
missing.push({ stage, issue: comment.issue });
|
|
588
674
|
}
|
|
589
675
|
}
|
|
590
|
-
const tally = tokensSpent({ releases, missing, capFor });
|
|
676
|
+
const tally = tokensSpent({ releases, missing, capFor: spendCapFor });
|
|
591
677
|
if (tally.error) {
|
|
592
678
|
state.blocked = tally.error;
|
|
593
679
|
return state;
|
|
@@ -644,11 +730,34 @@ export function sweep({ read, config, killSwitch, now, ctx }) {
|
|
|
644
730
|
// The reservation a Claim reaches here replaces is the one its release ended; in `checking`
|
|
645
731
|
// the other Stage check may still be running, so its reservation stays.
|
|
646
732
|
const next = keepHeld ? held + own : own;
|
|
733
|
+
if (!keepHeld) budget.heldBy.set(issue.number, new Map());
|
|
734
|
+
const stages = budget.heldBy.get(issue.number) ?? new Map();
|
|
735
|
+
stages.set(stage, (stages.get(stage) ?? 0) + own);
|
|
736
|
+
budget.heldBy.set(issue.number, stages);
|
|
647
737
|
budget.reservedBy.set(issue.number, next);
|
|
648
738
|
budget.reservedTotal += next - held;
|
|
649
739
|
return true;
|
|
650
740
|
};
|
|
651
741
|
|
|
742
|
+
/**
|
|
743
|
+
* Whether the QA or security Stage may start for a task in `checking` under the Daily cap.
|
|
744
|
+
* Admitting one Stage replaces the task's reservation with its own cap, and what the other
|
|
745
|
+
* Stage holds (its live Claim's cap, or its check's while it has not answered: one cap, never
|
|
746
|
+
* two) is put back at once.
|
|
747
|
+
*/
|
|
748
|
+
const admitCheckStage = (issue, stage) => {
|
|
749
|
+
if (!budget) return true;
|
|
750
|
+
const other = stage === "qa" ? "security" : "qa";
|
|
751
|
+
const back = budget.heldBy.get(issue.number)?.get(other) ?? 0;
|
|
752
|
+
if (!admit(issue, stage)) return false;
|
|
753
|
+
if (back > 0) {
|
|
754
|
+
budget.heldBy.get(issue.number).set(other, back);
|
|
755
|
+
budget.reservedBy.set(issue.number, (budget.reservedBy.get(issue.number) ?? 0) + back);
|
|
756
|
+
budget.reservedTotal += back;
|
|
757
|
+
}
|
|
758
|
+
return true;
|
|
759
|
+
};
|
|
760
|
+
|
|
652
761
|
// The notice at 80% and 100% of the cap, once per threshold per day, on the control issue.
|
|
653
762
|
if (budget && !budget.blocked) {
|
|
654
763
|
const due = noticeDue({
|
|
@@ -1387,9 +1496,45 @@ export function sweep({ read, config, killSwitch, now, ctx }) {
|
|
|
1387
1496
|
qa?.state === "failure" ? "qa" : null,
|
|
1388
1497
|
security?.state === "failure" ? "security" : null,
|
|
1389
1498
|
].filter(Boolean);
|
|
1499
|
+
// The QA and security Stages run under their own Claims, as the review Stage does. A
|
|
1500
|
+
// check that answered (`success` or `failure`) ends its Stage; every other state (none,
|
|
1501
|
+
// `pending`, `error`) means the Stage is running or ended without a result.
|
|
1502
|
+
const checkOf = { qa, security };
|
|
1503
|
+
const answered = (stage) =>
|
|
1504
|
+
checkOf[stage]?.state === "success" || checkOf[stage]?.state === "failure";
|
|
1505
|
+
const stageClaims = (stage) => live.filter((c) => c.stage === stage);
|
|
1506
|
+
// Claims already released in this pass (an `error`, an expiry, or an answered Stage's Claim
|
|
1507
|
+
// released `done`): never released twice.
|
|
1508
|
+
const handled = new Set();
|
|
1509
|
+
// Close the Claims of both Stages as the task leaves `checking`, or when a Stage's check
|
|
1510
|
+
// answered: an answered Stage's Claim is always `done`; one still running is
|
|
1511
|
+
// cancelled by its nonce and released `stopped` (never an Attempt), or `expired`.
|
|
1512
|
+
const closeStageClaims = () =>
|
|
1513
|
+
CHECK_STAGES.flatMap((stage) =>
|
|
1514
|
+
stageClaims(stage).flatMap((claim) => {
|
|
1515
|
+
if (handled.has(claim)) return [];
|
|
1516
|
+
// An answered Stage ran to a result (a `failure` is that result, counted through the
|
|
1517
|
+
// failed head, not as a Stage run without one): its Claim is `done`.
|
|
1518
|
+
if (answered(stage)) {
|
|
1519
|
+
return [
|
|
1520
|
+
{
|
|
1521
|
+
type: "release",
|
|
1522
|
+
issue: issue.number,
|
|
1523
|
+
commentId: claim.commentId,
|
|
1524
|
+
reason: "done",
|
|
1525
|
+
},
|
|
1526
|
+
];
|
|
1527
|
+
}
|
|
1528
|
+
return cancelAndRelease(
|
|
1529
|
+
issue,
|
|
1530
|
+
claim,
|
|
1531
|
+
expiredNow.includes(claim) ? "expired" : "stopped",
|
|
1532
|
+
);
|
|
1533
|
+
}),
|
|
1534
|
+
);
|
|
1390
1535
|
if (failedStages.length === 0) {
|
|
1391
1536
|
if (qa?.state === "success" && securityPassed(security)) {
|
|
1392
|
-
actions.push({
|
|
1537
|
+
actions.push(...closeStageClaims(), {
|
|
1393
1538
|
type: "set-state",
|
|
1394
1539
|
issue: issue.number,
|
|
1395
1540
|
from: "checking",
|
|
@@ -1397,9 +1542,90 @@ export function sweep({ read, config, killSwitch, now, ctx }) {
|
|
|
1397
1542
|
reason: "checks-passed",
|
|
1398
1543
|
});
|
|
1399
1544
|
running -= 1;
|
|
1545
|
+
continue;
|
|
1546
|
+
}
|
|
1547
|
+
// Each Stage that has not answered: a live Claim is waited for; an `error` newer than it
|
|
1548
|
+
// releases it `error`, and an expired one is cancelled and released `expired`; then the
|
|
1549
|
+
// Stage starts again under a new Claim, or the task goes to needs-decision once the Stage
|
|
1550
|
+
// has run `max_attempts` times without a result.
|
|
1551
|
+
const starts = [];
|
|
1552
|
+
let exhausted = false;
|
|
1553
|
+
for (const stage of CHECK_STAGES) {
|
|
1554
|
+
if (answered(stage)) {
|
|
1555
|
+
// A Claim an answered Stage left live (its own release not yet written).
|
|
1556
|
+
for (const claim of stageClaims(stage)) {
|
|
1557
|
+
handled.add(claim);
|
|
1558
|
+
actions.push({
|
|
1559
|
+
type: "release",
|
|
1560
|
+
issue: issue.number,
|
|
1561
|
+
commentId: claim.commentId,
|
|
1562
|
+
reason: "done",
|
|
1563
|
+
});
|
|
1564
|
+
}
|
|
1565
|
+
continue;
|
|
1566
|
+
}
|
|
1567
|
+
const liveStage = unexpired.filter((c) => c.stage === stage).at(-1);
|
|
1568
|
+
if (
|
|
1569
|
+
checkOf[stage]?.state === "error" &&
|
|
1570
|
+
liveStage &&
|
|
1571
|
+
Date.parse(checkOf[stage].createdAt) >= Date.parse(liveStage.createdAt)
|
|
1572
|
+
) {
|
|
1573
|
+
erroredNow.add(liveStage);
|
|
1574
|
+
handled.add(liveStage);
|
|
1575
|
+
actions.push({
|
|
1576
|
+
type: "release",
|
|
1577
|
+
issue: issue.number,
|
|
1578
|
+
commentId: liveStage.commentId,
|
|
1579
|
+
reason: "error",
|
|
1580
|
+
});
|
|
1581
|
+
}
|
|
1582
|
+
for (const claim of expiredNow.filter((c) => c.stage === stage)) {
|
|
1583
|
+
handled.add(claim);
|
|
1584
|
+
actions.push(...cancelAndRelease(issue, claim));
|
|
1585
|
+
}
|
|
1586
|
+
if (unexpired.some((c) => c.stage === stage && !erroredNow.has(c))) continue;
|
|
1587
|
+
const failedRuns = failedOf(stage);
|
|
1588
|
+
if (failedRuns >= cfg.max_attempts) {
|
|
1589
|
+
actions.push(
|
|
1590
|
+
...askAfterAttempts(
|
|
1591
|
+
issue,
|
|
1592
|
+
`The ${CHECK_STAGE_NAMES[stage]} Stage ran ${failedRuns} times without a result`,
|
|
1593
|
+
failedRuns,
|
|
1594
|
+
),
|
|
1595
|
+
...closeStageClaims(),
|
|
1596
|
+
{
|
|
1597
|
+
type: "set-state",
|
|
1598
|
+
issue: issue.number,
|
|
1599
|
+
from: "checking",
|
|
1600
|
+
to: "needs-decision",
|
|
1601
|
+
reason: "attempts-exhausted",
|
|
1602
|
+
},
|
|
1603
|
+
);
|
|
1604
|
+
running -= 1;
|
|
1605
|
+
exhausted = true;
|
|
1606
|
+
break;
|
|
1607
|
+
}
|
|
1608
|
+
starts.push({ stage, attempt: failedRuns + 1 });
|
|
1609
|
+
}
|
|
1610
|
+
if (exhausted) continue;
|
|
1611
|
+
// The Daily cap: a Stage's own cap is reserved with the other Stage's cap still held.
|
|
1612
|
+
for (const { stage, attempt } of starts) {
|
|
1613
|
+
if (!admitCheckStage(issue, stage)) continue;
|
|
1614
|
+
const claim = newClaim(stage, issue.number, attempt);
|
|
1615
|
+
actions.push({
|
|
1616
|
+
type: "claim",
|
|
1617
|
+
issue: issue.number,
|
|
1618
|
+
claim,
|
|
1619
|
+
body: renderClaimComment(claim),
|
|
1620
|
+
});
|
|
1621
|
+
actions.push({
|
|
1622
|
+
type: "start-stage",
|
|
1623
|
+
issue: issue.number,
|
|
1624
|
+
stage,
|
|
1625
|
+
attempt,
|
|
1626
|
+
claim: claim.claim,
|
|
1627
|
+
});
|
|
1400
1628
|
}
|
|
1401
|
-
// No check yet, `pending`, or `error`: the Stage has not answered. There
|
|
1402
|
-
// is no Claim to expire in `checking` yet, so the sweep only waits.
|
|
1403
1629
|
continue;
|
|
1404
1630
|
}
|
|
1405
1631
|
// Either check failing sends the task back to building. The note names
|
|
@@ -1438,6 +1664,7 @@ export function sweep({ read, config, killSwitch, now, ctx }) {
|
|
|
1438
1664
|
failedAttempts,
|
|
1439
1665
|
),
|
|
1440
1666
|
);
|
|
1667
|
+
actions.push(...closeStageClaims());
|
|
1441
1668
|
actions.push({
|
|
1442
1669
|
type: "set-state",
|
|
1443
1670
|
issue: issue.number,
|
|
@@ -1449,6 +1676,7 @@ export function sweep({ read, config, killSwitch, now, ctx }) {
|
|
|
1449
1676
|
continue;
|
|
1450
1677
|
}
|
|
1451
1678
|
if (!admit(issue, "build", { keepHeld: true })) continue;
|
|
1679
|
+
actions.push(...closeStageClaims());
|
|
1452
1680
|
const attempt = lastAttempt(claims, "build") + 1;
|
|
1453
1681
|
const claim = newClaim("build", issue.number, attempt, applyVoid(issue, isEngine));
|
|
1454
1682
|
actions.push({ type: "claim", issue: issue.number, claim, body: renderClaimComment(claim) });
|
|
@@ -413,11 +413,14 @@ export function createGithubTracker({ gh, repo }) {
|
|
|
413
413
|
},
|
|
414
414
|
|
|
415
415
|
/**
|
|
416
|
-
* The release comments (#515) of every issue, closed ones too, posted
|
|
417
|
-
* (the Daily cap sums the day's
|
|
418
|
-
*
|
|
416
|
+
* The release comments (#515) and the Claim comments of every issue, closed ones too, posted
|
|
417
|
+
* or edited since `sinceIso` (the Daily cap sums the day's releases, and counts a Claim on an
|
|
418
|
+
* issue the sweep did not read): `[{ issue, id, author, body, createdAt, updatedAt, editor }]`.
|
|
419
|
+
* Only comments that carry the release or the Claim marker are kept. The REST read does not
|
|
420
|
+
* name an editor, so `editor` is read (GraphQL) only for a Claim whose `updated_at` is after its `created_at`, and is null
|
|
421
|
+
* elsewhere, and is read only for the engine's own Claims (`isEngine(login)`, given as `{ isEngine }`; an outsider's comment cannot make the read fail). A failed read throws.
|
|
419
422
|
*/
|
|
420
|
-
readReleases(sinceIso) {
|
|
423
|
+
readReleases(sinceIso, { isEngine = () => true } = {}) {
|
|
421
424
|
if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/.test(String(sinceIso))) {
|
|
422
425
|
throw new Error(`not a UTC time: ${JSON.stringify(sinceIso)}`);
|
|
423
426
|
}
|
|
@@ -428,21 +431,57 @@ export function createGithubTracker({ gh, repo }) {
|
|
|
428
431
|
"--paginate",
|
|
429
432
|
"--slurp",
|
|
430
433
|
]) ?? [];
|
|
431
|
-
|
|
434
|
+
const kept = pages
|
|
432
435
|
.flat()
|
|
433
|
-
.filter(
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
String(c.
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
436
|
+
.filter(
|
|
437
|
+
(c) =>
|
|
438
|
+
String(c.body ?? "").includes("<!-- factory:release ") ||
|
|
439
|
+
String(c.body ?? "").includes("<!-- factory:claim "),
|
|
440
|
+
);
|
|
441
|
+
// The REST read names no editor. A Claim someone else edited is void (its stage, nonce and
|
|
442
|
+
// `released` mark cannot be trusted, and an edit can make it count nothing): the last editor
|
|
443
|
+
// of each edited Claim is read (GraphQL, by node id), and a failed read throws, so no new
|
|
444
|
+
// Claim starts on a spend that cannot be trusted.
|
|
445
|
+
const editors = new Map();
|
|
446
|
+
const edited = kept.filter(
|
|
447
|
+
(c) =>
|
|
448
|
+
String(c.body ?? "").includes("<!-- factory:claim ") &&
|
|
449
|
+
isEngine(c.user?.login ?? "") &&
|
|
450
|
+
Date.parse(c.updated_at) !== Date.parse(c.created_at),
|
|
451
|
+
);
|
|
452
|
+
for (let from = 0; from < edited.length; from += 100) {
|
|
453
|
+
const ids = edited.slice(from, from + 100).map((c) => c.node_id);
|
|
454
|
+
if (ids.some((id) => typeof id !== "string" || id === "")) {
|
|
455
|
+
throw new Error("a Claim comment has no node id: its editor could not be read");
|
|
456
|
+
}
|
|
457
|
+
const data = json([
|
|
458
|
+
"api",
|
|
459
|
+
"graphql",
|
|
460
|
+
"-f",
|
|
461
|
+
"query=query($ids: [ID!]!) { nodes(ids: $ids) { ... on IssueComment { id editor { login } } } }",
|
|
462
|
+
...ids.flatMap((id) => ["-f", `ids[]=${id}`]),
|
|
463
|
+
]);
|
|
464
|
+
if (!Array.isArray(data?.data?.nodes) || data.data.nodes.length !== ids.length) {
|
|
465
|
+
throw new Error("the editors of the edited Claims could not be read");
|
|
466
|
+
}
|
|
467
|
+
for (const node of data.data.nodes) editors.set(node?.id, node?.editor?.login ?? null);
|
|
468
|
+
if (ids.some((id) => !editors.has(id))) {
|
|
469
|
+
throw new Error("the editors of the edited Claims could not be read");
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return kept.map((c) => ({
|
|
473
|
+
issue: Number(
|
|
474
|
+
String(c.issue_url ?? "")
|
|
475
|
+
.split("/")
|
|
476
|
+
.pop(),
|
|
477
|
+
),
|
|
478
|
+
id: c.id,
|
|
479
|
+
author: c.user?.login ?? "",
|
|
480
|
+
body: c.body ?? "",
|
|
481
|
+
createdAt: c.created_at,
|
|
482
|
+
updatedAt: c.updated_at,
|
|
483
|
+
editor: editors.get(c.node_id) ?? null,
|
|
484
|
+
}));
|
|
446
485
|
},
|
|
447
486
|
|
|
448
487
|
/** Asks GitHub to merge the base into a pull request's branch, only if the head is still `expectedHead`. */
|