@mnaimfaizy/agent-kit 1.0.0-beta.15 → 1.0.0-beta.16
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/package.json +1 -1
- package/src/cli.mjs +314 -4
- package/src/doctor.mjs +53 -1
- package/src/exec.mjs +14 -5
- package/src/factory/budget/budget.mjs +259 -0
- package/src/factory/build/command.mjs +321 -0
- package/src/factory/build/factory.mjs +473 -0
- package/src/factory/build/findings.mjs +55 -0
- package/src/factory/build/host.mjs +230 -0
- package/src/factory/build/notes.mjs +191 -0
- package/src/factory/build/pull-request.mjs +74 -0
- package/src/factory/bundle.mjs +146 -0
- package/src/factory/checks/allowed-paths.mjs +50 -0
- package/src/factory/checks/plan-header.mjs +296 -0
- package/src/factory/checks/qa.mjs +85 -0
- package/src/factory/checks/review.mjs +73 -0
- package/src/factory/checks/security.mjs +50 -0
- package/src/factory/checks/stage-check.mjs +89 -0
- package/src/factory/claims/command.mjs +187 -0
- package/src/factory/claims/release.mjs +111 -0
- package/src/factory/decision/answers.mjs +103 -0
- package/src/factory/decision/command.mjs +64 -0
- package/src/factory/decision/plan.mjs +99 -0
- package/src/factory/decision/request.mjs +116 -0
- package/src/factory/doctor/files.mjs +329 -0
- package/src/factory/doctor/index.mjs +56 -0
- package/src/factory/doctor/local.mjs +497 -0
- package/src/factory/doctor/orchestrate.mjs +146 -0
- package/src/factory/engine/lease.mjs +146 -0
- package/src/factory/engine/local-engine.mjs +177 -0
- package/src/factory/engine/lockfile.mjs +64 -0
- package/src/factory/engine/workflow-engine.mjs +124 -0
- package/src/factory/gate/comment.mjs +88 -0
- package/src/factory/gate/config.mjs +57 -0
- package/src/factory/gate/gate-core.mjs +250 -0
- package/src/factory/gate/guarded.mjs +17 -0
- package/src/factory/gate/reads.mjs +105 -0
- package/src/factory/gate/task.mjs +193 -0
- package/src/factory/gate/trip-reads.mjs +82 -0
- package/src/factory/gate/trip.mjs +87 -0
- package/src/factory/init/blueprint.mjs +174 -0
- package/src/factory/init/index.mjs +337 -0
- package/src/factory/labels/evidence.mjs +162 -0
- package/src/factory/labels/moves.mjs +32 -0
- package/src/factory/labels/reconcile.mjs +267 -0
- package/src/factory/labels/revert.mjs +87 -0
- package/src/factory/labels/timeline.mjs +72 -0
- package/src/factory/labels/voided.mjs +59 -0
- package/src/factory/limits/harness.mjs +39 -0
- package/src/factory/limits/limits.mjs +123 -0
- package/src/factory/merge/command.mjs +172 -0
- package/src/factory/outbox/check.mjs +240 -0
- package/src/factory/outbox/rulings.mjs +195 -0
- package/src/factory/plan/command.mjs +66 -0
- package/src/factory/plan/comment.mjs +156 -0
- package/src/factory/plan/publish.mjs +149 -0
- package/src/factory/plan/risk.mjs +153 -0
- package/src/factory/qa/command.mjs +163 -0
- package/src/factory/records/engine-record.mjs +133 -0
- package/src/factory/records/main.mjs +96 -0
- package/src/factory/records/record.mjs +226 -0
- package/src/factory/records/sink.mjs +57 -0
- package/src/factory/report/build.mjs +92 -0
- package/src/factory/report/command.mjs +80 -0
- package/src/factory/report/facts.mjs +226 -0
- package/src/factory/report/gate-section.mjs +122 -0
- package/src/factory/report/metrics.mjs +111 -0
- package/src/factory/report/reads.mjs +246 -0
- package/src/factory/report/render.mjs +121 -0
- package/src/factory/report/rollout.mjs +154 -0
- package/src/factory/review/command.mjs +182 -0
- package/src/factory/run/awake.mjs +70 -0
- package/src/factory/run/collect.mjs +77 -0
- package/src/factory/run/command.mjs +490 -0
- package/src/factory/run/gh.mjs +46 -0
- package/src/factory/run/launch.mjs +47 -0
- package/src/factory/run/pipelines.mjs +261 -0
- package/src/factory/run/raw.mjs +96 -0
- package/src/factory/run/restart.mjs +83 -0
- package/src/factory/run/s3.mjs +193 -0
- package/src/factory/run/secrets.mjs +142 -0
- package/src/factory/run/teardown.mjs +242 -0
- package/src/factory/run/token.mjs +68 -0
- package/src/factory/security/command.mjs +157 -0
- package/src/factory/setup/app.mjs +122 -0
- package/src/factory/setup/command.mjs +409 -0
- package/src/factory/setup/labels.mjs +25 -0
- package/src/factory/setup/reads.mjs +129 -0
- package/src/factory/setup/writes.mjs +126 -0
- package/src/factory/snapshot/write.mjs +176 -0
- package/src/factory/stop/command.mjs +136 -0
- package/src/factory/sweep/actions-main.mjs +117 -0
- package/src/factory/sweep/command.mjs +65 -0
- package/src/factory/sweep/knobs.mjs +78 -0
- package/src/factory/sweep/marker-version.mjs +15 -0
- package/src/factory/sweep/markers.mjs +112 -0
- package/src/factory/sweep/overlap.mjs +24 -0
- package/src/factory/sweep/run.mjs +198 -0
- package/src/factory/sweep/shell.mjs +394 -0
- package/src/factory/sweep/split.mjs +104 -0
- package/src/factory/sweep/states.mjs +87 -0
- package/src/factory/sweep/sweep-core.mjs +1781 -0
- package/src/factory/tracker/github.mjs +674 -0
- package/src/factory/tracker/operations.mjs +153 -0
- package/src/factory/upgrade/index.mjs +83 -0
- package/src/init.mjs +62 -24
- package/src/kit-owned-paths.mjs +1 -1
- package/src/stage.mjs +19 -0
- package/src/upgrade.mjs +39 -7
|
@@ -0,0 +1,1781 @@
|
|
|
1
|
+
// The sweep core: one tracker read, the config, the Kill switch and the clock
|
|
2
|
+
// in; an ordered list of actions out. It does no IO and reads no clock, no
|
|
3
|
+
// environment and no randomness of its own: the nonce of a new Claim comes from
|
|
4
|
+
// `ctx.nonce`, which the shell supplies (and a test fixes). It judges nothing
|
|
5
|
+
// about a Stage's work. ADR 0034, ADR 0037; the contract suite is
|
|
6
|
+
// scripts/factory-sweep-core.test.mjs over the fixtures in
|
|
7
|
+
// scripts/fixtures/factory/sweep/.
|
|
8
|
+
//
|
|
9
|
+
// It covers `queued`, the first Claim and the plan Stage's result:
|
|
10
|
+
//
|
|
11
|
+
// queued + ready-for-agent -> claim, set-state planning, start-stage plan
|
|
12
|
+
// queued, no ready-for-agent -> comment why, set-state needs-decision
|
|
13
|
+
// a Claim that expired -> release (expired), back to queued
|
|
14
|
+
// (or needs-decision once Attempts run out)
|
|
15
|
+
// planning + a valid plan -> set-state planned, release (done)
|
|
16
|
+
// planning + a failed or
|
|
17
|
+
// malformed plan -> release (failed), back to queued: one failed Attempt
|
|
18
|
+
// planned + a valid plan -> claim, set-state building, start-stage build
|
|
19
|
+
// building + an open PR on this Attempt's branch with green CI
|
|
20
|
+
// -> set-state reviewing, release (done)
|
|
21
|
+
// building + red CI, or a failure note for the Claim
|
|
22
|
+
// -> release (failed), back to planned: one failed Attempt
|
|
23
|
+
// building + a wider-paths Decision Request for the Claim
|
|
24
|
+
// -> release (question), set-state needs-decision
|
|
25
|
+
// a build Claim that expired -> release (expired), back to planned
|
|
26
|
+
// (or needs-decision once Attempts run out)
|
|
27
|
+
// reviewing + no review Claim, or one that expired
|
|
28
|
+
// -> claim review, start-stage review
|
|
29
|
+
// reviewing + the engine's `factory/review` success on the PR's head
|
|
30
|
+
// -> set-state checking, release (done)
|
|
31
|
+
// reviewing + the engine's `factory/review` failure
|
|
32
|
+
// -> comment the findings, release (failed),
|
|
33
|
+
// claim build, set-state building, start-stage build
|
|
34
|
+
// (or needs-decision once Attempts run out)
|
|
35
|
+
// checking + the engine's `factory/qa` success and `factory/security` `passed`
|
|
36
|
+
// on the PR's head -> set-state ready-to-merge
|
|
37
|
+
// checking + either the engine's check a failure
|
|
38
|
+
// -> comment (no findings), claim build,
|
|
39
|
+
// set-state building, start-stage build
|
|
40
|
+
// (or needs-decision once Attempts run out)
|
|
41
|
+
// A Stage check counts only when its creator is the engine's identity
|
|
42
|
+
// (ADR 0035): a status from anyone else is ignored, as if it were not there.
|
|
43
|
+
//
|
|
44
|
+
// planning + a valid plan with questions, or held by plan_approval, or for an
|
|
45
|
+
// issue with no acceptance criteria
|
|
46
|
+
// -> post-decision-request, release (question),
|
|
47
|
+
// set-state needs-decision
|
|
48
|
+
// queued + a Decision Request with an empty answer
|
|
49
|
+
// -> comment naming it, set-state needs-decision
|
|
50
|
+
// queued + an answered Decision Request
|
|
51
|
+
// -> the Attempt count starts again from it; an approved plan
|
|
52
|
+
// goes straight to planned, else the plan Stage runs again
|
|
53
|
+
// (the answers reach its brief). Answering alone moves
|
|
54
|
+
// nothing: `needs-decision` is left only by a person.
|
|
55
|
+
//
|
|
56
|
+
// Expiry, `error` and the limit (docs/factory/attempts.md):
|
|
57
|
+
//
|
|
58
|
+
// an expired Claim -> cancel-stage (by its nonce), release (expired)
|
|
59
|
+
// reviewing + an `error` check newer than the live review Claim
|
|
60
|
+
// -> release (error), claim review again: free the
|
|
61
|
+
// first time in a row, one failed Attempt each
|
|
62
|
+
// `error` after it
|
|
63
|
+
// max_attempts failed Attempts
|
|
64
|
+
// -> post-decision-request (kind `attempts`, once),
|
|
65
|
+
// set-state needs-decision
|
|
66
|
+
//
|
|
67
|
+
// A plan that splits (docs/factory/split.md, #508):
|
|
68
|
+
//
|
|
69
|
+
// planning (or an approved plan in queued) + a valid plan with `split`
|
|
70
|
+
// -> split (the shell creates each child as a queued sub-issue, links
|
|
71
|
+
// the blockers, lists the children in a comment), release (done),
|
|
72
|
+
// remove-label (the parent's Factory state)
|
|
73
|
+
// a child whose own plan splits
|
|
74
|
+
// -> post-decision-request, release (question), set-state
|
|
75
|
+
// needs-decision: one level only
|
|
76
|
+
// a queued child whose parent is still splitting -> waits
|
|
77
|
+
// a child in `merged`, or one the gate records merged now, whose siblings are all merged
|
|
78
|
+
// -> close-parent (before that child's own set-state merged)
|
|
79
|
+
//
|
|
80
|
+
// Pause and drain (docs/factory/pause.md):
|
|
81
|
+
//
|
|
82
|
+
// factory:paused (a write user's)
|
|
83
|
+
// -> per live Claim: cancel-stage (by its nonce), release
|
|
84
|
+
// (paused): never an Attempt; nothing else starts or moves
|
|
85
|
+
// the label removed, the newest Claim released `paused`
|
|
86
|
+
// planning / building -> set-state queued / planned (resumed); the other states
|
|
87
|
+
// pick up from their own evidence
|
|
88
|
+
// a Kill switch that is not exactly `true`
|
|
89
|
+
// -> one notice and nothing else: no Claim, move, comment or
|
|
90
|
+
// merge; a running Stage finishes and is read when it is on
|
|
91
|
+
//
|
|
92
|
+
// Every other state is observed only: its issue yields no action. The ordered
|
|
93
|
+
// list holds, per issue and in ascending issue number, the actions in the order
|
|
94
|
+
// the shell must apply them. If one fails, the shell skips that issue's later
|
|
95
|
+
// actions (so a Claim that is not read back never moves the issue).
|
|
96
|
+
//
|
|
97
|
+
// ready-to-merge -> the merge gate (gate/task.mjs): gate-comment, gate-update,
|
|
98
|
+
// copy-stage-checks, merge (`on` only), set-state merged or checking
|
|
99
|
+
// merge.mode on -> the trip (gate/trip.mjs), as the shell read it into `read.trip`:
|
|
100
|
+
// a new one writes its marker on the control issue (trip-marker)
|
|
101
|
+
//
|
|
102
|
+
// The Daily cap (docs/factory/budget.md, ADR 0042), when the config or the owner's
|
|
103
|
+
// variable names one (`config.budget`, `config.daily_override`, `read.spend`,
|
|
104
|
+
// `read.control`):
|
|
105
|
+
//
|
|
106
|
+
// a Claim to write -> only if spent today + the full cap of every Stage still running
|
|
107
|
+
// + its own cap fit the cap; else the task waits (a notice) and a
|
|
108
|
+
// smaller, newer one may start before it
|
|
109
|
+
// spent >= 80%, >= 100% -> budget-notice on the control issue, once per threshold per day
|
|
110
|
+
// no readable cap or spend -> no new Claim; running Stages and merges go on
|
|
111
|
+
//
|
|
112
|
+
// Actions: claim, release, set-state, remove-label, comment, post-decision-request, start-stage,
|
|
113
|
+
// cancel-stage, split, close-parent, gate-comment, gate-update, copy-stage-checks, merge, trip-marker,
|
|
114
|
+
// budget-notice, notice.
|
|
115
|
+
|
|
116
|
+
import { buildFailureFor, buildQuestionsFor, widerPathsRequestFor } from "../build/notes.mjs";
|
|
117
|
+
import { acceptanceCriteria, checkPlanHeader, untrustedText } from "../checks/plan-header.mjs";
|
|
118
|
+
import { QA_CONTEXT } from "../checks/qa.mjs";
|
|
119
|
+
import { REVIEW_CONTEXT } from "../checks/review.mjs";
|
|
120
|
+
import { SECURITY_CONTEXT, securityPassed } from "../checks/security.mjs";
|
|
121
|
+
import { engineStageCheck } from "../checks/stage-check.mjs";
|
|
122
|
+
import { latestPlan, planFailureFor } from "../plan/comment.mjs";
|
|
123
|
+
import { taskPathsOverlap } from "./overlap.mjs";
|
|
124
|
+
import {
|
|
125
|
+
CHILD_LABELS,
|
|
126
|
+
SPLIT_TOO_DEEP_TITLE,
|
|
127
|
+
parseChildMarker,
|
|
128
|
+
parseSplitComment,
|
|
129
|
+
renderChildBody,
|
|
130
|
+
splitTooDeepRequest,
|
|
131
|
+
} from "./split.mjs";
|
|
132
|
+
import {
|
|
133
|
+
latestDecisionRequest,
|
|
134
|
+
nameAnswers,
|
|
135
|
+
parseAnswers,
|
|
136
|
+
readAnswers,
|
|
137
|
+
} from "../decision/answers.mjs";
|
|
138
|
+
import {
|
|
139
|
+
budgetFits,
|
|
140
|
+
budgetPercent,
|
|
141
|
+
budgetStageCap,
|
|
142
|
+
budgetDayStart,
|
|
143
|
+
effectiveCap,
|
|
144
|
+
noticeDue,
|
|
145
|
+
renderBudgetNotice,
|
|
146
|
+
todaysReleases,
|
|
147
|
+
tokensSpent,
|
|
148
|
+
} from "../budget/budget.mjs";
|
|
149
|
+
import { taskSizeOf } from "../limits/limits.mjs";
|
|
150
|
+
import { decisionForPlan } from "../decision/plan.mjs";
|
|
151
|
+
import { parseDecisionRequest, renderDecisionRequest } from "../decision/request.mjs";
|
|
152
|
+
import { gateActions } from "../gate/task.mjs";
|
|
153
|
+
import { evidenceOf } from "../labels/evidence.mjs";
|
|
154
|
+
import { reconcileIssue } from "../labels/reconcile.mjs";
|
|
155
|
+
import { restartsOf } from "../labels/revert.mjs";
|
|
156
|
+
import { applyVoid } from "../labels/voided.mjs";
|
|
157
|
+
import { hasNote, noteMarker, parseClaim, renderClaimComment } from "./markers.mjs";
|
|
158
|
+
import { VERSION_PATTERN, VERSION_TOKEN } from "./marker-version.mjs";
|
|
159
|
+
import { parseReleaseComment } from "../claims/release.mjs";
|
|
160
|
+
import {
|
|
161
|
+
CLAIM_GRACE_MINUTES,
|
|
162
|
+
DEFAULT_CONFIG,
|
|
163
|
+
READY_LABEL,
|
|
164
|
+
HELD_STATES,
|
|
165
|
+
RUNNING_STATES,
|
|
166
|
+
PAUSED_LABEL,
|
|
167
|
+
buildBranch,
|
|
168
|
+
stateLabel,
|
|
169
|
+
stateOfLabel,
|
|
170
|
+
} from "./states.mjs";
|
|
171
|
+
|
|
172
|
+
const sweepBare = (login) =>
|
|
173
|
+
String(login ?? "")
|
|
174
|
+
.replace(/\[bot\]$/, "")
|
|
175
|
+
.toLowerCase();
|
|
176
|
+
|
|
177
|
+
/** The highest Attempt number among the Claims of one Stage, or 0. */
|
|
178
|
+
const lastAttempt = (claims, stage) =>
|
|
179
|
+
Math.max(0, ...claims.filter((c) => c.stage === stage).map((c) => c.attempt));
|
|
180
|
+
|
|
181
|
+
/** The open pull request of the task's newest build Attempt, on that Attempt's own branch. */
|
|
182
|
+
const buildPullOf = (issue, claims) => {
|
|
183
|
+
const lastBuild = claims.filter((c) => c.stage === "build").at(-1);
|
|
184
|
+
return lastBuild
|
|
185
|
+
? (issue.pulls ?? []).find(
|
|
186
|
+
(p) => p.state === "OPEN" && p.branch === buildBranch(issue.number, lastBuild.attempt),
|
|
187
|
+
)
|
|
188
|
+
: undefined;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/** The marker of the engine's note that a Stage check failed on a head. */
|
|
192
|
+
const checkFailedMarker = (stage, head) =>
|
|
193
|
+
`<!-- factory:check-failed${VERSION_TOKEN} stage=${stage} head=${head} -->`;
|
|
194
|
+
|
|
195
|
+
/** The heads the engine has noted a failed QA or security check on. */
|
|
196
|
+
const failedCheckHeads = (issue, isEngine) =>
|
|
197
|
+
new Set(
|
|
198
|
+
(issue.comments ?? [])
|
|
199
|
+
.filter((c) => isEngine(c.author))
|
|
200
|
+
.flatMap((c) =>
|
|
201
|
+
[
|
|
202
|
+
...String(c.body).matchAll(
|
|
203
|
+
new RegExp(
|
|
204
|
+
`<!-- factory:check-failed${VERSION_PATTERN} stage=(?:qa|security) head=([0-9a-f]{40}) -->`,
|
|
205
|
+
"g",
|
|
206
|
+
),
|
|
207
|
+
),
|
|
208
|
+
].map((m) => m[1]),
|
|
209
|
+
),
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* An expired Claim (or, with `reason` `paused`, a Claim on a task a person
|
|
214
|
+
* paused): the Stage run is cancelled by its nonce, then the Claim is released
|
|
215
|
+
* with that reason. Cancelling first means a run that is still going cannot
|
|
216
|
+
* answer a Claim that is gone; one that already ended is not an error.
|
|
217
|
+
*/
|
|
218
|
+
const cancelAndRelease = (issue, claim, reason = "expired") => [
|
|
219
|
+
{ type: "cancel-stage", issue: issue.number, stage: claim.stage, claim: claim.claim },
|
|
220
|
+
{
|
|
221
|
+
type: "release",
|
|
222
|
+
issue: issue.number,
|
|
223
|
+
commentId: claim.commentId,
|
|
224
|
+
reason,
|
|
225
|
+
// A voided Claim is rewritten, not just edited: its text may already say released.
|
|
226
|
+
...(claim.voided
|
|
227
|
+
? {
|
|
228
|
+
voided: true,
|
|
229
|
+
claimData: {
|
|
230
|
+
engine: claim.engine,
|
|
231
|
+
claim: claim.claim,
|
|
232
|
+
stage: claim.stage,
|
|
233
|
+
attempt: claim.attempt,
|
|
234
|
+
run: claim.run,
|
|
235
|
+
expires: claim.originalExpires,
|
|
236
|
+
},
|
|
237
|
+
}
|
|
238
|
+
: {}),
|
|
239
|
+
},
|
|
240
|
+
];
|
|
241
|
+
|
|
242
|
+
const toMs = (clock) => (clock instanceof Date ? clock.getTime() : new Date(clock).getTime());
|
|
243
|
+
|
|
244
|
+
const iso = (ms) => new Date(ms).toISOString().replace(/\.\d+Z$/, "Z");
|
|
245
|
+
|
|
246
|
+
/** The effective config: the defaults, with the Consumer's values laid over them. */
|
|
247
|
+
export function effectiveConfig(config = {}) {
|
|
248
|
+
return {
|
|
249
|
+
...DEFAULT_CONFIG,
|
|
250
|
+
...config,
|
|
251
|
+
limits: {
|
|
252
|
+
...DEFAULT_CONFIG.limits,
|
|
253
|
+
...(config.limits ?? {}),
|
|
254
|
+
// The build row is keyed by Task size: a size the config leaves out keeps its default.
|
|
255
|
+
build: { ...DEFAULT_CONFIG.limits.build, ...(config.limits?.build ?? {}) },
|
|
256
|
+
},
|
|
257
|
+
merge: { ...DEFAULT_CONFIG.merge, ...(config.merge ?? {}) },
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** The Claims in an issue's comments that the engine wrote, oldest first. */
|
|
262
|
+
export function claimsOf(issue, engine) {
|
|
263
|
+
const out = [];
|
|
264
|
+
for (const comment of issue.comments ?? []) {
|
|
265
|
+
if (sweepBare(comment.author) !== sweepBare(engine.identity)) continue;
|
|
266
|
+
const claim = parseClaim(comment.body);
|
|
267
|
+
if (!claim || claim.engine !== engine.name) continue;
|
|
268
|
+
// Evidence last edited by someone else is void: a voided Claim is expired.
|
|
269
|
+
if (comment.voided) {
|
|
270
|
+
const { released: _released, ...rest } = claim;
|
|
271
|
+
out.push({
|
|
272
|
+
...rest,
|
|
273
|
+
originalExpires: claim.expires,
|
|
274
|
+
expires: "1970-01-01T00:00:00Z",
|
|
275
|
+
voided: true,
|
|
276
|
+
commentId: comment.id,
|
|
277
|
+
createdAt: comment.createdAt,
|
|
278
|
+
});
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
out.push({ ...claim, commentId: comment.id, createdAt: comment.createdAt });
|
|
282
|
+
}
|
|
283
|
+
return out;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* @param {{
|
|
288
|
+
* read: { issues: Array<{ number: number, title?: string, labels: string[], comments: Array<{ id: string|number, author: string, body: string }> }> },
|
|
289
|
+
* config?: object,
|
|
290
|
+
* killSwitch?: string | undefined,
|
|
291
|
+
* now: Date | number | string,
|
|
292
|
+
* ctx: { engine: { name: string, identity: string }, run: string, nonce: (issue: number, attempt: number) => string },
|
|
293
|
+
* }} input
|
|
294
|
+
* @returns {object[]} the ordered actions
|
|
295
|
+
*/
|
|
296
|
+
export function sweep({ read, config, killSwitch, now, ctx }) {
|
|
297
|
+
// The Kill switch is exactly `true`. Anything else, unset included, drains.
|
|
298
|
+
if (killSwitch !== "true") {
|
|
299
|
+
return [
|
|
300
|
+
{
|
|
301
|
+
type: "notice",
|
|
302
|
+
code: "kill-switch-off",
|
|
303
|
+
message: "AGENT_KIT_FACTORY_ENABLED is not exactly `true`: nothing is claimed or moved.",
|
|
304
|
+
},
|
|
305
|
+
];
|
|
306
|
+
}
|
|
307
|
+
const cfg = effectiveConfig(config);
|
|
308
|
+
const nowMs = toMs(now);
|
|
309
|
+
const actions = [];
|
|
310
|
+
|
|
311
|
+
const stateLabels = (issue) =>
|
|
312
|
+
(issue.labels ?? []).filter((label) => stateOfLabel(label) !== null);
|
|
313
|
+
const stateOf = (issue) => {
|
|
314
|
+
const names = stateLabels(issue);
|
|
315
|
+
return names.length === 1 ? stateOfLabel(names[0]) : null;
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
// A spec issue is never a Factory task (#507): one with sub-issues of its own, on
|
|
319
|
+
// which the engine has written no Claim, is left out of everything below. It holds
|
|
320
|
+
// no slot, is never claimed or moved, and a Factory label on it gets a notice each sweep.
|
|
321
|
+
const numbered = [...(read.issues ?? [])].sort((a, b) => a.number - b.number);
|
|
322
|
+
const specIssues = numbered.filter(
|
|
323
|
+
(i) => (i.subIssueCount ?? 0) > 0 && claimsOf(i, ctx.engine).length === 0,
|
|
324
|
+
);
|
|
325
|
+
for (const spec of specIssues) {
|
|
326
|
+
if (stateLabels(spec).length === 0) continue;
|
|
327
|
+
actions.push({
|
|
328
|
+
type: "notice",
|
|
329
|
+
issue: spec.number,
|
|
330
|
+
code: "spec-issue-is-not-a-task",
|
|
331
|
+
message: `This issue has ${spec.subIssueCount} sub-issues, so it is a spec issue, not a Factory task: left alone.`,
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
// Queued tasks are taken oldest `factory:queued` first: by the time of the latest
|
|
335
|
+
// event that added the label (a restart counts from its own), then by number. They
|
|
336
|
+
// fill the places the queued issues held, so no other state changes its turn.
|
|
337
|
+
const queuedAt = (issue) => {
|
|
338
|
+
const times = (issue.timeline ?? [])
|
|
339
|
+
.filter((e) => e.kind === "labeled" && e.label === stateLabel("queued"))
|
|
340
|
+
.map((e) => Date.parse(e.at))
|
|
341
|
+
.filter(Number.isFinite);
|
|
342
|
+
return times.length ? Math.max(...times) : Infinity;
|
|
343
|
+
};
|
|
344
|
+
const eligible = numbered.filter((i) => !specIssues.includes(i));
|
|
345
|
+
const queuedOldestFirst = eligible
|
|
346
|
+
.filter((i) => stateOf(i) === "queued")
|
|
347
|
+
.sort((a, b) => queuedAt(a) - queuedAt(b) || a.number - b.number);
|
|
348
|
+
let nextQueued = 0;
|
|
349
|
+
const issues = eligible.map((i) =>
|
|
350
|
+
stateOf(i) === "queued" ? queuedOldestFirst[nextQueued++] : i,
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
// The minutes a Stage's Claim lasts: `limits.<stage>.minutes`; for build, the row of the
|
|
354
|
+
// task's Task size (the largest when the plan cannot be read, as the Daily cap does).
|
|
355
|
+
const claimMinutes = (stage, view) => {
|
|
356
|
+
const row = cfg.limits[stage];
|
|
357
|
+
if (stage !== "build") return row?.minutes ?? DEFAULT_CONFIG.limits[stage].minutes;
|
|
358
|
+
const size = view ? taskSizeFor(view) : undefined;
|
|
359
|
+
const sizes = size ? [size] : ["s", "m", "l"];
|
|
360
|
+
return Math.max(
|
|
361
|
+
...sizes.map((z) => row?.[z]?.minutes ?? DEFAULT_CONFIG.limits.build[z].minutes),
|
|
362
|
+
);
|
|
363
|
+
};
|
|
364
|
+
const newClaim = (stage, issue, attempt, view) => ({
|
|
365
|
+
engine: ctx.engine.name,
|
|
366
|
+
claim: ctx.nonce(issue, attempt),
|
|
367
|
+
stage,
|
|
368
|
+
attempt,
|
|
369
|
+
run: String(ctx.run),
|
|
370
|
+
expires: iso(nowMs + (claimMinutes(stage, view) + CLAIM_GRACE_MINUTES) * 60_000),
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
let mergeTaken = false;
|
|
374
|
+
// The gate's trip (merge.mode on): a new one is written on the control issue, and any
|
|
375
|
+
// trip that stands is said once in the log.
|
|
376
|
+
const trip = cfg.merge.mode === "on" ? (read.trip ?? { state: "unreadable" }) : undefined;
|
|
377
|
+
if (trip?.write && trip.controlIssue) {
|
|
378
|
+
actions.push({
|
|
379
|
+
type: "trip-marker",
|
|
380
|
+
issue: trip.controlIssue,
|
|
381
|
+
body: trip.body,
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
if (trip && trip.state !== "none") {
|
|
385
|
+
actions.push({
|
|
386
|
+
type: "notice",
|
|
387
|
+
code: "merge-gate-trip",
|
|
388
|
+
message:
|
|
389
|
+
trip.state === "cleared"
|
|
390
|
+
? `the merge gate's trip is cleared; it acts as ${trip.barMet === true ? "on" : "shadow"} (the shadow bar is ${trip.barMet === true ? "met" : "not met"} since the trip)`
|
|
391
|
+
: `the merge gate is tripped${trip.why ? ` (${trip.why})` : ""}: it acts as shadow`,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
// A paused task is frozen, not running: it holds no `max_running` slot.
|
|
395
|
+
let running = issues.filter(
|
|
396
|
+
(i) => RUNNING_STATES.includes(stateOf(i)) && !(i.labels ?? []).includes(PAUSED_LABEL),
|
|
397
|
+
).length;
|
|
398
|
+
|
|
399
|
+
const isEngine = (login) => sweepBare(login) === sweepBare(ctx.engine.identity);
|
|
400
|
+
|
|
401
|
+
// A plan's split (#508). A child is an issue whose body carries the child marker: a person
|
|
402
|
+
// can add it, which only stops a split there (one level), never starts one.
|
|
403
|
+
const childOf = (issue) => (typeof issue.body === "string" ? parseChildMarker(issue.body) : null);
|
|
404
|
+
const readByNumber = new Map(numbered.map((i) => [i.number, i]));
|
|
405
|
+
/** Whether `parent` is still splitting: read in `queued` or `planning`, with a plan that splits. */
|
|
406
|
+
const stillSplitting = (parent) => {
|
|
407
|
+
const raw = readByNumber.get(parent);
|
|
408
|
+
if (!raw || !["queued", "planning"].includes(stateOf(raw))) return false;
|
|
409
|
+
const plan = latestPlan(applyVoid(raw, isEngine).comments, isEngine);
|
|
410
|
+
const checked = plan ? checkPlanHeader(plan.data, { trusted: true }) : null;
|
|
411
|
+
return Boolean(checked?.ok && checked.header.split?.length);
|
|
412
|
+
};
|
|
413
|
+
/** The engine's own list of the children of plan `n`, when it is on the issue (an edited one is gone). */
|
|
414
|
+
const splitListed = (issue, n) =>
|
|
415
|
+
(issue.comments ?? []).some((c) => isEngine(c.author) && parseSplitComment(c.body)?.plan === n);
|
|
416
|
+
/** The actions that split `issue` by `split`, idempotent: a child the read already holds and a list already posted are not made again. */
|
|
417
|
+
const splitActions = (issue, plan, split, { from, release }) => {
|
|
418
|
+
const out = [];
|
|
419
|
+
if (!splitListed(issue, plan.n)) {
|
|
420
|
+
const existing = new Map();
|
|
421
|
+
for (const other of numbered) {
|
|
422
|
+
const mark = childOf(other);
|
|
423
|
+
// Only an issue the engine made: a person can type the marker, but not be the author.
|
|
424
|
+
if (
|
|
425
|
+
mark &&
|
|
426
|
+
mark.parent === issue.number &&
|
|
427
|
+
mark.plan === plan.n &&
|
|
428
|
+
(other.author === undefined || isEngine(other.author))
|
|
429
|
+
) {
|
|
430
|
+
existing.set(mark.index, other.number);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
out.push({
|
|
434
|
+
type: "split",
|
|
435
|
+
issue: issue.number,
|
|
436
|
+
plan: plan.n,
|
|
437
|
+
entries: split.map((entry, index) => ({
|
|
438
|
+
index,
|
|
439
|
+
title: entry.title,
|
|
440
|
+
body: renderChildBody({
|
|
441
|
+
summary: entry.summary,
|
|
442
|
+
allowed_paths: entry.allowed_paths,
|
|
443
|
+
parent: issue.number,
|
|
444
|
+
plan: plan.n,
|
|
445
|
+
index,
|
|
446
|
+
}),
|
|
447
|
+
labels: [...CHILD_LABELS],
|
|
448
|
+
blocked_by: entry.blocked_by ?? [],
|
|
449
|
+
...(existing.has(index) ? { existing: existing.get(index) } : {}),
|
|
450
|
+
})),
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
if (release !== undefined) {
|
|
454
|
+
out.push({ type: "release", issue: issue.number, commentId: release, reason: "done" });
|
|
455
|
+
}
|
|
456
|
+
out.push({
|
|
457
|
+
type: "remove-label",
|
|
458
|
+
issue: issue.number,
|
|
459
|
+
label: stateLabel(from),
|
|
460
|
+
reason: "split",
|
|
461
|
+
});
|
|
462
|
+
return out;
|
|
463
|
+
};
|
|
464
|
+
/** The Decision Request that stops a child whose plan splits, posted once per plan. */
|
|
465
|
+
const splitTooDeepActions = (issue, plan, child, attempt) => {
|
|
466
|
+
const asked = (issue.comments ?? []).some(
|
|
467
|
+
(c) =>
|
|
468
|
+
isEngine(c.author) &&
|
|
469
|
+
parseDecisionRequest(c.body)?.plan === plan.n &&
|
|
470
|
+
String(c.body).includes(`## Decision Request: plan ${plan.n} ${SPLIT_TOO_DEEP_TITLE}`),
|
|
471
|
+
);
|
|
472
|
+
return asked
|
|
473
|
+
? []
|
|
474
|
+
: [
|
|
475
|
+
{
|
|
476
|
+
type: "post-decision-request",
|
|
477
|
+
issue: issue.number,
|
|
478
|
+
body: renderDecisionRequest(
|
|
479
|
+
splitTooDeepRequest({
|
|
480
|
+
issue: issue.number,
|
|
481
|
+
plan: plan.n,
|
|
482
|
+
count: plan.split.length,
|
|
483
|
+
attempt,
|
|
484
|
+
parent: child.parent,
|
|
485
|
+
}),
|
|
486
|
+
),
|
|
487
|
+
},
|
|
488
|
+
];
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
// The Allowed paths of every task that holds a build (#507): `building` through
|
|
492
|
+
// `ready-to-merge`, a merged one aside. `paths` is null when the plan cannot be read,
|
|
493
|
+
// which blocks, because nothing is known about what that task touches.
|
|
494
|
+
const heldPaths = [];
|
|
495
|
+
for (const raw of issues) {
|
|
496
|
+
if (!HELD_STATES.includes(stateOf(raw)) || raw.state === "CLOSED") continue;
|
|
497
|
+
if (
|
|
498
|
+
(raw.pulls ?? []).some(
|
|
499
|
+
(p) => p.state === "MERGED" && p.branch.startsWith(`factory/${raw.number}/attempt-`),
|
|
500
|
+
)
|
|
501
|
+
)
|
|
502
|
+
continue;
|
|
503
|
+
const plan = latestPlan(applyVoid(raw, isEngine).comments, isEngine);
|
|
504
|
+
const checked = plan ? checkPlanHeader(plan.data, { trusted: true }) : null;
|
|
505
|
+
heldPaths.push({
|
|
506
|
+
number: raw.number,
|
|
507
|
+
paths: checked?.ok ? checked.header.allowed_paths : null,
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// The Daily cap. `budget` stays null when no cap is named, so nothing below changes.
|
|
512
|
+
const budget = setUpBudget();
|
|
513
|
+
function setUpBudget() {
|
|
514
|
+
const cap = effectiveCap({ budget: config?.budget, override: config?.daily_override });
|
|
515
|
+
if (cap.none) return null;
|
|
516
|
+
const state = {
|
|
517
|
+
cap: cap.cap,
|
|
518
|
+
source: cap.source,
|
|
519
|
+
notify: cap.notify,
|
|
520
|
+
blocked: cap.error ? `the Daily cap is not readable: ${cap.error}` : null,
|
|
521
|
+
spent: 0,
|
|
522
|
+
reservedBy: new Map(),
|
|
523
|
+
reservedTotal: 0,
|
|
524
|
+
};
|
|
525
|
+
if (state.blocked) return state;
|
|
526
|
+
if (!read.spend || read.spend.error || !Array.isArray(read.spend.comments)) {
|
|
527
|
+
state.blocked = `tokens spent today could not be read${read.spend?.error ? ` (${read.spend.error})` : ""}`;
|
|
528
|
+
return state;
|
|
529
|
+
}
|
|
530
|
+
const limits = config?.budget?.limits;
|
|
531
|
+
const views = new Map(issues.map((i) => [i.number, applyVoid(i, isEngine)]));
|
|
532
|
+
const sizes = new Map();
|
|
533
|
+
const sizeFor = (number) => {
|
|
534
|
+
// A release on an issue the sweep did not read (a closed one) has no plan to read: the
|
|
535
|
+
// largest size stands.
|
|
536
|
+
if (!sizes.has(number)) {
|
|
537
|
+
sizes.set(number, views.has(number) ? taskSizeFor(views.get(number)) : undefined);
|
|
538
|
+
}
|
|
539
|
+
return sizes.get(number);
|
|
540
|
+
};
|
|
541
|
+
const capFor = (stage, number) =>
|
|
542
|
+
budgetStageCap({ limits, stage, size: stage === "build" ? sizeFor(number) : undefined });
|
|
543
|
+
state.capFor = capFor;
|
|
544
|
+
const releases = todaysReleases({ comments: read.spend.comments, isEngine, nowMs });
|
|
545
|
+
const dayStart = budgetDayStart(nowMs);
|
|
546
|
+
const missing = [];
|
|
547
|
+
for (const raw of issues) {
|
|
548
|
+
const view = views.get(raw.number);
|
|
549
|
+
const claimsHere = claimsOf(view, ctx.engine);
|
|
550
|
+
const paused = (view.labels ?? []).includes(PAUSED_LABEL);
|
|
551
|
+
for (const claim of claimsHere) {
|
|
552
|
+
const key = `${claim.stage}:${claim.claim}`;
|
|
553
|
+
const live = claim.released === undefined && Date.parse(claim.expires) > nowMs;
|
|
554
|
+
if (live && !paused && !releases.has(key)) {
|
|
555
|
+
const own = capFor(claim.stage, raw.number);
|
|
556
|
+
if (own === null) {
|
|
557
|
+
state.blocked = `the ${claim.stage} Stage's token cap is not readable`;
|
|
558
|
+
return state;
|
|
559
|
+
}
|
|
560
|
+
state.reservedBy.set(raw.number, (state.reservedBy.get(raw.number) ?? 0) + own);
|
|
561
|
+
} else if (
|
|
562
|
+
!live &&
|
|
563
|
+
claim.released !== "lost-race" &&
|
|
564
|
+
Date.parse(claim.createdAt) >= dayStart &&
|
|
565
|
+
!releases.has(key)
|
|
566
|
+
) {
|
|
567
|
+
// A run today that left no total counts at its full cap.
|
|
568
|
+
missing.push({ stage: claim.stage, issue: raw.number });
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
// A task in `checking` has its QA and security Stages running until their checks answer.
|
|
572
|
+
if (stateOfLabel(stateLabels(view)[0] ?? "") === "checking" && !paused) {
|
|
573
|
+
const pull = buildPullOf(view, claimsHere);
|
|
574
|
+
const statuses = pull?.head ? read.statuses?.[pull.head] : undefined;
|
|
575
|
+
for (const [stage, context] of [
|
|
576
|
+
["qa", QA_CONTEXT],
|
|
577
|
+
["security", SECURITY_CONTEXT],
|
|
578
|
+
]) {
|
|
579
|
+
const check = engineStageCheck(statuses, context, isEngine);
|
|
580
|
+
if (check && check.state !== "pending") continue;
|
|
581
|
+
const own = capFor(stage, raw.number);
|
|
582
|
+
if (own === null) {
|
|
583
|
+
state.blocked = `the ${stage} Stage's token cap is not readable`;
|
|
584
|
+
return state;
|
|
585
|
+
}
|
|
586
|
+
state.reservedBy.set(raw.number, (state.reservedBy.get(raw.number) ?? 0) + own);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
const tally = tokensSpent({ releases, missing, capFor });
|
|
591
|
+
if (tally.error) {
|
|
592
|
+
state.blocked = tally.error;
|
|
593
|
+
return state;
|
|
594
|
+
}
|
|
595
|
+
state.spent = tally.spent;
|
|
596
|
+
state.reservedTotal = [...state.reservedBy.values()].reduce((a, b) => a + b, 0);
|
|
597
|
+
return state;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/** The Task size a build would run at: the plan's, raised by any `A<n>: size m|l` after it
|
|
601
|
+
* (whoever wrote it, so the reservation is never smaller than the run's cap). Undefined when
|
|
602
|
+
* the plan cannot be read: the reservation then takes the largest size. */
|
|
603
|
+
function taskSizeFor(issue) {
|
|
604
|
+
const plan = latestPlan(issue.comments, isEngine);
|
|
605
|
+
const checked = plan ? checkPlanHeader(plan.data, { trusted: true }) : null;
|
|
606
|
+
if (!checked?.ok) return undefined;
|
|
607
|
+
const comments = issue.comments ?? [];
|
|
608
|
+
const planAt = comments.findIndex((c) => String(c.id) === String(plan.commentId));
|
|
609
|
+
const answers = comments
|
|
610
|
+
.slice(planAt + 1)
|
|
611
|
+
.filter((c) => !isEngine(c.author))
|
|
612
|
+
.flatMap((c) => [...parseAnswers(c.body)].map(([n, answer]) => ({ n, answer })));
|
|
613
|
+
const sized = taskSizeOf({ header: checked.header, answers });
|
|
614
|
+
return sized.size;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* Whether `stage` may start for `issue` under the Daily cap, and if it may, reserved for it
|
|
619
|
+
* (replacing what the task held, unless `keepHeld`).
|
|
620
|
+
*/
|
|
621
|
+
const admit = (issue, stage, { keepHeld = false } = {}) => {
|
|
622
|
+
if (!budget) return true;
|
|
623
|
+
const wait = (message) => {
|
|
624
|
+
actions.push({
|
|
625
|
+
type: "notice",
|
|
626
|
+
issue: issue.number,
|
|
627
|
+
code: "daily-cap-wait",
|
|
628
|
+
message,
|
|
629
|
+
});
|
|
630
|
+
return false;
|
|
631
|
+
};
|
|
632
|
+
if (budget.blocked) return wait(`${budget.blocked}: no new Claim until it can be read`);
|
|
633
|
+
const own = budget.capFor(stage, issue.number);
|
|
634
|
+
if (own === null) return wait(`the ${stage} Stage's token cap is not readable: the task waits`);
|
|
635
|
+
const held = budget.reservedBy.get(issue.number) ?? 0;
|
|
636
|
+
const reserved = budget.reservedTotal - (keepHeld ? 0 : held);
|
|
637
|
+
if (!budgetFits({ cap: budget.cap, spent: budget.spent, reserved, own })) {
|
|
638
|
+
return wait(
|
|
639
|
+
budget.spent >= budget.cap
|
|
640
|
+
? `the Daily cap (${budget.cap} tokens, ${budget.source}) is spent: no new Claim until 00:00 UTC`
|
|
641
|
+
: `the ${stage} Stage (cap ${own}) does not fit the Daily cap: ${budget.spent} spent + ${reserved} reserved of ${budget.cap} tokens (${budget.source}): the task waits`,
|
|
642
|
+
);
|
|
643
|
+
}
|
|
644
|
+
// The reservation a Claim reaches here replaces is the one its release ended; in `checking`
|
|
645
|
+
// the other Stage check may still be running, so its reservation stays.
|
|
646
|
+
const next = keepHeld ? held + own : own;
|
|
647
|
+
budget.reservedBy.set(issue.number, next);
|
|
648
|
+
budget.reservedTotal += next - held;
|
|
649
|
+
return true;
|
|
650
|
+
};
|
|
651
|
+
|
|
652
|
+
// The notice at 80% and 100% of the cap, once per threshold per day, on the control issue.
|
|
653
|
+
if (budget && !budget.blocked) {
|
|
654
|
+
const due = noticeDue({
|
|
655
|
+
cap: budget.cap,
|
|
656
|
+
spent: budget.spent,
|
|
657
|
+
nowMs,
|
|
658
|
+
comments: read.control?.comments ?? [],
|
|
659
|
+
isEngine,
|
|
660
|
+
});
|
|
661
|
+
if (due !== null && read.control?.error === undefined && read.control?.number) {
|
|
662
|
+
actions.push({
|
|
663
|
+
type: "budget-notice",
|
|
664
|
+
issue: read.control.number,
|
|
665
|
+
threshold: due,
|
|
666
|
+
body: renderBudgetNotice({
|
|
667
|
+
nowMs,
|
|
668
|
+
threshold: due,
|
|
669
|
+
cap: budget.cap,
|
|
670
|
+
spent: budget.spent,
|
|
671
|
+
source: budget.source,
|
|
672
|
+
notify: budget.notify,
|
|
673
|
+
}),
|
|
674
|
+
});
|
|
675
|
+
} else if (due !== null) {
|
|
676
|
+
actions.push({
|
|
677
|
+
type: "notice",
|
|
678
|
+
code: "daily-cap-no-control-issue",
|
|
679
|
+
message: `the Daily cap is ${budgetPercent({ cap: budget.cap, spent: budget.spent })}% spent, but there is no Factory control issue to post the ${due}% notice on`,
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
for (const rawIssue of issues) {
|
|
685
|
+
// Evidence last edited by someone other than its writer is void.
|
|
686
|
+
const issue = applyVoid(rawIssue, isEngine);
|
|
687
|
+
const labels = issue.labels ?? [];
|
|
688
|
+
const states = stateLabels(issue);
|
|
689
|
+
if (states.length === 0) continue;
|
|
690
|
+
// Label ownership by evidence (docs/factory/labels.md): Trigger labels, Human
|
|
691
|
+
// moves, reverts, two state labels. Without a timeline only Trigger labels.
|
|
692
|
+
const claimsRead = claimsOf(issue, ctx.engine);
|
|
693
|
+
const reconciled = reconcileIssue({
|
|
694
|
+
issue,
|
|
695
|
+
claims: claimsRead,
|
|
696
|
+
evidence: evidenceOf({
|
|
697
|
+
issue,
|
|
698
|
+
rawComments: rawIssue.comments,
|
|
699
|
+
claims: claimsRead,
|
|
700
|
+
statuses: read.statuses,
|
|
701
|
+
isEngine,
|
|
702
|
+
nowMs,
|
|
703
|
+
}),
|
|
704
|
+
isEngine,
|
|
705
|
+
writers: read.writers,
|
|
706
|
+
});
|
|
707
|
+
actions.push(...reconciled.actions);
|
|
708
|
+
if (reconciled.stop) continue;
|
|
709
|
+
if (states.length > 1) {
|
|
710
|
+
actions.push({
|
|
711
|
+
type: "notice",
|
|
712
|
+
issue: issue.number,
|
|
713
|
+
code: "two-states",
|
|
714
|
+
message: `Issue carries ${states.join(" and ")}: left alone until a person keeps one.`,
|
|
715
|
+
});
|
|
716
|
+
continue;
|
|
717
|
+
}
|
|
718
|
+
const state = stateOfLabel(states[0]);
|
|
719
|
+
// Claims a Human move released in this sweep are released for the rest of it.
|
|
720
|
+
const claims = claimsRead.map((c) =>
|
|
721
|
+
reconciled.released.includes(c.commentId) ? { ...c, released: "stopped" } : c,
|
|
722
|
+
);
|
|
723
|
+
const live = claims.filter((c) => c.released === undefined);
|
|
724
|
+
// `factory:paused` (only a write user's label gets this far): the Stage run
|
|
725
|
+
// of each live Claim is cancelled by its nonce and the Claim is released
|
|
726
|
+
// `paused`. That is never an Attempt (only `expired`, `failed` and `error`
|
|
727
|
+
// are), whatever the Claim's expiry says. Nothing else starts or moves.
|
|
728
|
+
if (labels.includes(PAUSED_LABEL)) {
|
|
729
|
+
for (const claim of live) actions.push(...cancelAndRelease(issue, claim, "paused"));
|
|
730
|
+
continue;
|
|
731
|
+
}
|
|
732
|
+
const expiredNow = live.filter((c) => Date.parse(c.expires) <= nowMs);
|
|
733
|
+
const unexpired = live.filter((c) => Date.parse(c.expires) > nowMs);
|
|
734
|
+
// Failed Attempts of one Stage: its Claims released `expired` or `failed`,
|
|
735
|
+
// and those that have expired since.
|
|
736
|
+
// A restart (a Human move) starts the Attempts again: only Claims written
|
|
737
|
+
// after its comment count, and after every comment already read when the
|
|
738
|
+
// restart is taken in this very sweep.
|
|
739
|
+
const restartFloor = reconciled.restartPending
|
|
740
|
+
? Math.max(0, ...(issue.comments ?? []).map((c) => Number(c.id)))
|
|
741
|
+
: Math.max(0, ...restartsOf(issue.comments, isEngine).map((r) => Number(r.commentId)));
|
|
742
|
+
// A Decision Request starts the Attempt count again: only Claims written
|
|
743
|
+
// after the latest one count, so an answered task gets a fresh set.
|
|
744
|
+
const lastRequest = latestDecisionRequest(issue.comments, isEngine);
|
|
745
|
+
// An `attempts` request resets the count once a person re-queues the task
|
|
746
|
+
// (it is queued, or a Claim was written after the request). Only while its
|
|
747
|
+
// request is posted and the move to `needs-decision` has not landed do the
|
|
748
|
+
// failed Attempts still stand.
|
|
749
|
+
const resets =
|
|
750
|
+
lastRequest &&
|
|
751
|
+
(state === "queued" ||
|
|
752
|
+
!lastRequest.kinds.includes("attempts") ||
|
|
753
|
+
claims.some((c) => Number(c.commentId) > Number(lastRequest.commentId)));
|
|
754
|
+
const afterRequest = (c) =>
|
|
755
|
+
Number(c.commentId) > restartFloor &&
|
|
756
|
+
(!resets || Number(c.commentId) > Number(lastRequest.commentId));
|
|
757
|
+
// A Claim whose Stage check said `error` in this sweep (it is released below).
|
|
758
|
+
const erroredNow = new Set();
|
|
759
|
+
const outcomeOf = (c) =>
|
|
760
|
+
c.released ?? (expiredNow.includes(c) ? "expired" : erroredNow.has(c) ? "error" : null);
|
|
761
|
+
// Failed Attempts of one Stage, after the latest Decision Request. A Claim
|
|
762
|
+
// that expired or was released `failed` is one. An `error` (no valid result)
|
|
763
|
+
// is free the first time in a row; each `error` that follows one counts.
|
|
764
|
+
// A Claim whose release comment says a limit stopped the run (`stopped:turns`,
|
|
765
|
+
// `stopped:minutes`, `stopped:tokens`) is one failed Attempt whatever else
|
|
766
|
+
// ended it, and never a free `error`.
|
|
767
|
+
const stoppedRuns = new Set(
|
|
768
|
+
(issue.comments ?? [])
|
|
769
|
+
.filter((comment) => isEngine(comment.author))
|
|
770
|
+
.map((comment) => parseReleaseComment(comment.body))
|
|
771
|
+
.filter((release) => release?.outcome.startsWith("stopped:"))
|
|
772
|
+
.map((release) => `${release.stage}:${release.claim}`),
|
|
773
|
+
);
|
|
774
|
+
const failedOf = (stage) => {
|
|
775
|
+
let count = 0;
|
|
776
|
+
let streak = 0;
|
|
777
|
+
for (const c of claims) {
|
|
778
|
+
if (c.stage !== stage || !afterRequest(c)) continue;
|
|
779
|
+
// An engine restart that released the Claim is an `error` (docs/factory/engine.md).
|
|
780
|
+
const raw = outcomeOf(c);
|
|
781
|
+
const outcome = raw === "engine-restart" ? "error" : raw;
|
|
782
|
+
if (
|
|
783
|
+
["error", "expired", "failed"].includes(outcome) &&
|
|
784
|
+
stoppedRuns.has(`${c.stage}:${c.claim}`)
|
|
785
|
+
) {
|
|
786
|
+
streak = 0;
|
|
787
|
+
count += 1;
|
|
788
|
+
continue;
|
|
789
|
+
}
|
|
790
|
+
if (outcome === "error") {
|
|
791
|
+
streak += 1;
|
|
792
|
+
if (streak >= 2) count += 1;
|
|
793
|
+
continue;
|
|
794
|
+
}
|
|
795
|
+
streak = 0;
|
|
796
|
+
if (outcome === "expired" || outcome === "failed") count += 1;
|
|
797
|
+
}
|
|
798
|
+
return count;
|
|
799
|
+
};
|
|
800
|
+
const failed = failedOf("plan");
|
|
801
|
+
|
|
802
|
+
// `max_attempts` failed Attempts: one Decision Request through the shared
|
|
803
|
+
// renderer, posted once (a sweep that ended after the comment finds it
|
|
804
|
+
// again and only moves the task). Answering it re-queues the task and the
|
|
805
|
+
// Attempt count starts again; the plan Stage runs again.
|
|
806
|
+
const askAfterAttempts = (target, what, count) => {
|
|
807
|
+
const newestClaim = Math.max(0, ...claims.map((c) => Number(c.commentId)));
|
|
808
|
+
if (lastRequest && Number(lastRequest.commentId) > newestClaim) return [];
|
|
809
|
+
return [
|
|
810
|
+
{
|
|
811
|
+
type: "post-decision-request",
|
|
812
|
+
issue: target.number,
|
|
813
|
+
body: renderDecisionRequest({
|
|
814
|
+
attempt: count,
|
|
815
|
+
plan: latestPlan(target.comments, isEngine)?.n ?? 1,
|
|
816
|
+
kinds: ["attempts"],
|
|
817
|
+
title: "The Factory stopped after repeated failed Attempts",
|
|
818
|
+
intro: `${what}, the limit (max_attempts is ${cfg.max_attempts}). The task is moved to ${stateLabel("needs-decision")} so that it does not loop.`,
|
|
819
|
+
questions: [
|
|
820
|
+
{
|
|
821
|
+
question: "Should the Factory plan and build this task again?",
|
|
822
|
+
options: [
|
|
823
|
+
"Yes: the task is queued again, its Attempts start again and the plan Stage runs again.",
|
|
824
|
+
"No: leave the task here, or withdraw it.",
|
|
825
|
+
],
|
|
826
|
+
recommendation:
|
|
827
|
+
"1, after you have looked at why the Attempts failed: the Stage notes and runs on this issue name each one.",
|
|
828
|
+
evidence: `${what}. Failed Attempts in this round: ${count}.`,
|
|
829
|
+
},
|
|
830
|
+
],
|
|
831
|
+
}),
|
|
832
|
+
},
|
|
833
|
+
];
|
|
834
|
+
};
|
|
835
|
+
|
|
836
|
+
// The pause was lifted: the task's newest Claim was released `paused`, with
|
|
837
|
+
// no Claim live. A Stage that is no longer running goes back one state, as
|
|
838
|
+
// an expired one does, but no Attempt was spent.
|
|
839
|
+
const resumed = live.length === 0 && claims.at(-1)?.released === "paused";
|
|
840
|
+
|
|
841
|
+
if (state === "planning") {
|
|
842
|
+
if (resumed) {
|
|
843
|
+
actions.push({
|
|
844
|
+
type: "set-state",
|
|
845
|
+
issue: issue.number,
|
|
846
|
+
from: "planning",
|
|
847
|
+
to: "queued",
|
|
848
|
+
reason: "resumed",
|
|
849
|
+
});
|
|
850
|
+
continue;
|
|
851
|
+
}
|
|
852
|
+
if (live.length === 0) {
|
|
853
|
+
// A split whose Claim was released but whose state label is still on: finish the removal.
|
|
854
|
+
const latest = latestPlan(issue.comments, isEngine);
|
|
855
|
+
const header = latest ? checkPlanHeader(latest.data, { trusted: true }) : null;
|
|
856
|
+
if (header?.ok && header.header.split?.length && splitListed(issue, latest.n)) {
|
|
857
|
+
actions.push(...splitActions(issue, latest, header.header.split, { from: "planning" }));
|
|
858
|
+
continue;
|
|
859
|
+
}
|
|
860
|
+
actions.push({
|
|
861
|
+
type: "notice",
|
|
862
|
+
issue: issue.number,
|
|
863
|
+
code: "planning-without-claim",
|
|
864
|
+
message: "In factory:planning with no Claim of this engine: left alone.",
|
|
865
|
+
});
|
|
866
|
+
continue;
|
|
867
|
+
}
|
|
868
|
+
// The plan Stage's result, for the newest Claim that has not expired. Only
|
|
869
|
+
// the engine's own comments count, and only the highest plan number is read.
|
|
870
|
+
const current = unexpired.at(-1);
|
|
871
|
+
if (current) {
|
|
872
|
+
const plan = latestPlan(issue.comments, isEngine);
|
|
873
|
+
const answered = plan && plan.claim === current.claim;
|
|
874
|
+
const checked = answered ? checkPlanHeader(plan.data, { trusted: true }) : null;
|
|
875
|
+
if (checked?.ok) {
|
|
876
|
+
const { split } = checked.header;
|
|
877
|
+
const want = decisionForPlan({
|
|
878
|
+
header: checked.header,
|
|
879
|
+
planNumber: plan.n,
|
|
880
|
+
planApproval: cfg.plan_approval,
|
|
881
|
+
// null when this read did not carry the issue's text: no criteria rule then.
|
|
882
|
+
// Nor once criteria were asked: an answer settles it, so it is not asked again.
|
|
883
|
+
criteria:
|
|
884
|
+
typeof issue.body === "string" &&
|
|
885
|
+
!(issue.comments ?? []).some(
|
|
886
|
+
(c) =>
|
|
887
|
+
isEngine(c.author) && parseDecisionRequest(c.body)?.kinds.includes("criteria"),
|
|
888
|
+
)
|
|
889
|
+
? acceptanceCriteria(issue.body)
|
|
890
|
+
: null,
|
|
891
|
+
});
|
|
892
|
+
if (want) {
|
|
893
|
+
// One request for the plan, posted once: a sweep that ended after
|
|
894
|
+
// the comment finds it again and only moves the task.
|
|
895
|
+
const posted = (issue.comments ?? []).slice(
|
|
896
|
+
(issue.comments ?? []).findIndex((c) => c.id === plan.commentId) + 1,
|
|
897
|
+
);
|
|
898
|
+
const already = posted.some(
|
|
899
|
+
(c) => isEngine(c.author) && parseDecisionRequest(c.body)?.plan === plan.n,
|
|
900
|
+
);
|
|
901
|
+
if (!already) {
|
|
902
|
+
actions.push({
|
|
903
|
+
type: "post-decision-request",
|
|
904
|
+
issue: issue.number,
|
|
905
|
+
body: renderDecisionRequest({
|
|
906
|
+
attempt: current.attempt,
|
|
907
|
+
plan: plan.n,
|
|
908
|
+
kinds: want.kinds,
|
|
909
|
+
title: want.title,
|
|
910
|
+
intro: want.intro,
|
|
911
|
+
questions: want.questions,
|
|
912
|
+
}),
|
|
913
|
+
});
|
|
914
|
+
}
|
|
915
|
+
actions.push({
|
|
916
|
+
type: "set-state",
|
|
917
|
+
issue: issue.number,
|
|
918
|
+
from: "planning",
|
|
919
|
+
to: "needs-decision",
|
|
920
|
+
reason: `decision-request:${want.kinds.join("+")}`,
|
|
921
|
+
});
|
|
922
|
+
actions.push({
|
|
923
|
+
type: "release",
|
|
924
|
+
issue: issue.number,
|
|
925
|
+
commentId: current.commentId,
|
|
926
|
+
reason: "question",
|
|
927
|
+
});
|
|
928
|
+
running -= 1;
|
|
929
|
+
continue;
|
|
930
|
+
}
|
|
931
|
+
if (split?.length) {
|
|
932
|
+
const child = childOf(issue);
|
|
933
|
+
if (child) {
|
|
934
|
+
// One level only: a child whose own plan splits waits for a person.
|
|
935
|
+
actions.push(
|
|
936
|
+
...splitTooDeepActions(issue, { n: plan.n, split }, child, current.attempt),
|
|
937
|
+
);
|
|
938
|
+
actions.push({
|
|
939
|
+
type: "set-state",
|
|
940
|
+
issue: issue.number,
|
|
941
|
+
from: "planning",
|
|
942
|
+
to: "needs-decision",
|
|
943
|
+
reason: "split-too-deep",
|
|
944
|
+
});
|
|
945
|
+
actions.push({
|
|
946
|
+
type: "release",
|
|
947
|
+
issue: issue.number,
|
|
948
|
+
commentId: current.commentId,
|
|
949
|
+
reason: "question",
|
|
950
|
+
});
|
|
951
|
+
} else {
|
|
952
|
+
actions.push(
|
|
953
|
+
...splitActions(issue, plan, split, {
|
|
954
|
+
from: "planning",
|
|
955
|
+
release: current.commentId,
|
|
956
|
+
}),
|
|
957
|
+
);
|
|
958
|
+
}
|
|
959
|
+
running -= 1;
|
|
960
|
+
continue;
|
|
961
|
+
}
|
|
962
|
+
// The move first, then the release: a release that fails afterwards
|
|
963
|
+
// leaves a planned issue with a live plan Claim, which is released below.
|
|
964
|
+
actions.push({
|
|
965
|
+
type: "set-state",
|
|
966
|
+
issue: issue.number,
|
|
967
|
+
from: "planning",
|
|
968
|
+
to: "planned",
|
|
969
|
+
reason: "plan-valid",
|
|
970
|
+
});
|
|
971
|
+
actions.push({
|
|
972
|
+
type: "release",
|
|
973
|
+
issue: issue.number,
|
|
974
|
+
commentId: current.commentId,
|
|
975
|
+
reason: "done",
|
|
976
|
+
});
|
|
977
|
+
running -= 1;
|
|
978
|
+
continue;
|
|
979
|
+
}
|
|
980
|
+
const failure = planFailureFor(issue.comments, isEngine, current.claim);
|
|
981
|
+
if (answered || failure) {
|
|
982
|
+
// A plan the sweep cannot read, or a Stage that left none: one failed Attempt.
|
|
983
|
+
actions.push({
|
|
984
|
+
type: "release",
|
|
985
|
+
issue: issue.number,
|
|
986
|
+
commentId: current.commentId,
|
|
987
|
+
reason: "failed",
|
|
988
|
+
});
|
|
989
|
+
actions.push({
|
|
990
|
+
type: "set-state",
|
|
991
|
+
issue: issue.number,
|
|
992
|
+
from: "planning",
|
|
993
|
+
to: "queued",
|
|
994
|
+
reason: answered ? "plan-malformed" : "plan-failed",
|
|
995
|
+
});
|
|
996
|
+
running -= 1;
|
|
997
|
+
continue;
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
for (const claim of expiredNow) {
|
|
1001
|
+
actions.push(...cancelAndRelease(issue, claim));
|
|
1002
|
+
}
|
|
1003
|
+
if (expiredNow.length && unexpired.length === 0) {
|
|
1004
|
+
actions.push({
|
|
1005
|
+
type: "set-state",
|
|
1006
|
+
issue: issue.number,
|
|
1007
|
+
from: "planning",
|
|
1008
|
+
to: "queued",
|
|
1009
|
+
reason: "claim-expired",
|
|
1010
|
+
});
|
|
1011
|
+
running -= 1;
|
|
1012
|
+
}
|
|
1013
|
+
continue;
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
if (state === "planned") {
|
|
1017
|
+
// A plan Claim still live on a planned issue: the release after the move did not land.
|
|
1018
|
+
const stale = live.filter((c) => c.stage === "plan");
|
|
1019
|
+
if (stale.length) {
|
|
1020
|
+
for (const claim of stale) {
|
|
1021
|
+
actions.push({
|
|
1022
|
+
type: "release",
|
|
1023
|
+
issue: issue.number,
|
|
1024
|
+
commentId: claim.commentId,
|
|
1025
|
+
reason: "done",
|
|
1026
|
+
});
|
|
1027
|
+
}
|
|
1028
|
+
continue;
|
|
1029
|
+
}
|
|
1030
|
+
for (const claim of expiredNow) {
|
|
1031
|
+
actions.push(...cancelAndRelease(issue, claim));
|
|
1032
|
+
}
|
|
1033
|
+
// A build Claim this engine wrote, still live, on an issue that is still
|
|
1034
|
+
// planned: a sweep ended between the Claim and the move. Carry on with it.
|
|
1035
|
+
const resumable = unexpired.find((c) => c.stage === "build");
|
|
1036
|
+
if (resumable) {
|
|
1037
|
+
actions.push({
|
|
1038
|
+
type: "set-state",
|
|
1039
|
+
issue: issue.number,
|
|
1040
|
+
from: "planned",
|
|
1041
|
+
to: "building",
|
|
1042
|
+
reason: "resume-claim",
|
|
1043
|
+
});
|
|
1044
|
+
actions.push({
|
|
1045
|
+
type: "start-stage",
|
|
1046
|
+
issue: issue.number,
|
|
1047
|
+
stage: "build",
|
|
1048
|
+
attempt: resumable.attempt,
|
|
1049
|
+
claim: resumable.claim,
|
|
1050
|
+
});
|
|
1051
|
+
running += 1;
|
|
1052
|
+
continue;
|
|
1053
|
+
}
|
|
1054
|
+
const plan = latestPlan(issue.comments, isEngine);
|
|
1055
|
+
if (!plan || !checkPlanHeader(plan.data, { trusted: true }).ok) {
|
|
1056
|
+
actions.push({
|
|
1057
|
+
type: "notice",
|
|
1058
|
+
issue: issue.number,
|
|
1059
|
+
code: "planned-without-a-plan",
|
|
1060
|
+
message: "In factory:planned with no valid plan of this engine: left alone.",
|
|
1061
|
+
});
|
|
1062
|
+
continue;
|
|
1063
|
+
}
|
|
1064
|
+
// A review that asked for changes is a failed Attempt too, and every
|
|
1065
|
+
// build Attempt has its own branch, so the next number follows the last.
|
|
1066
|
+
const failedBuilds =
|
|
1067
|
+
failedOf("build") + failedOf("review") + failedCheckHeads(issue, isEngine).size;
|
|
1068
|
+
const lastBuild = lastAttempt(claims, "build");
|
|
1069
|
+
if (failedBuilds >= cfg.max_attempts) {
|
|
1070
|
+
actions.push(
|
|
1071
|
+
...askAfterAttempts(
|
|
1072
|
+
issue,
|
|
1073
|
+
`The build Stage ran ${failedBuilds} times without a verified head`,
|
|
1074
|
+
failedBuilds,
|
|
1075
|
+
),
|
|
1076
|
+
);
|
|
1077
|
+
actions.push({
|
|
1078
|
+
type: "set-state",
|
|
1079
|
+
issue: issue.number,
|
|
1080
|
+
from: "planned",
|
|
1081
|
+
to: "needs-decision",
|
|
1082
|
+
reason: "attempts-exhausted",
|
|
1083
|
+
});
|
|
1084
|
+
continue;
|
|
1085
|
+
}
|
|
1086
|
+
if (running >= cfg.max_running) continue;
|
|
1087
|
+
// Two builds never collide: wait while a task that holds a build overlaps.
|
|
1088
|
+
const mine = checkPlanHeader(plan.data, { trusted: true }).header.allowed_paths;
|
|
1089
|
+
const holder = heldPaths.find(
|
|
1090
|
+
(h) => h.number !== issue.number && (h.paths === null || taskPathsOverlap(mine, h.paths)),
|
|
1091
|
+
);
|
|
1092
|
+
if (holder) {
|
|
1093
|
+
actions.push({
|
|
1094
|
+
type: "notice",
|
|
1095
|
+
issue: issue.number,
|
|
1096
|
+
code: "waiting-on-overlap",
|
|
1097
|
+
message:
|
|
1098
|
+
holder.paths === null
|
|
1099
|
+
? `Waiting to build: #${holder.number} holds a build and its Allowed paths cannot be read.`
|
|
1100
|
+
: `Waiting to build: its Allowed paths overlap #${holder.number}, which holds a build.`,
|
|
1101
|
+
});
|
|
1102
|
+
continue;
|
|
1103
|
+
}
|
|
1104
|
+
if (!admit(issue, "build")) continue;
|
|
1105
|
+
heldPaths.push({ number: issue.number, paths: mine });
|
|
1106
|
+
const attempt = Math.max(failedBuilds, lastBuild) + 1;
|
|
1107
|
+
const claim = newClaim("build", issue.number, attempt, applyVoid(issue, isEngine));
|
|
1108
|
+
actions.push({ type: "claim", issue: issue.number, claim, body: renderClaimComment(claim) });
|
|
1109
|
+
actions.push({ type: "set-state", issue: issue.number, from: "planned", to: "building" });
|
|
1110
|
+
actions.push({
|
|
1111
|
+
type: "start-stage",
|
|
1112
|
+
issue: issue.number,
|
|
1113
|
+
stage: "build",
|
|
1114
|
+
attempt,
|
|
1115
|
+
claim: claim.claim,
|
|
1116
|
+
});
|
|
1117
|
+
running += 1;
|
|
1118
|
+
continue;
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
if (state === "building") {
|
|
1122
|
+
if (resumed) {
|
|
1123
|
+
actions.push({
|
|
1124
|
+
type: "set-state",
|
|
1125
|
+
issue: issue.number,
|
|
1126
|
+
from: "building",
|
|
1127
|
+
to: "planned",
|
|
1128
|
+
reason: "resumed",
|
|
1129
|
+
});
|
|
1130
|
+
continue;
|
|
1131
|
+
}
|
|
1132
|
+
if (live.length === 0) {
|
|
1133
|
+
actions.push({
|
|
1134
|
+
type: "notice",
|
|
1135
|
+
issue: issue.number,
|
|
1136
|
+
code: "building-without-claim",
|
|
1137
|
+
message: "In factory:building with no Claim of this engine: left alone.",
|
|
1138
|
+
});
|
|
1139
|
+
continue;
|
|
1140
|
+
}
|
|
1141
|
+
// The build Stage's result, for the newest Claim that has not expired.
|
|
1142
|
+
const current = unexpired.filter((c) => c.stage === "build").at(-1);
|
|
1143
|
+
if (current) {
|
|
1144
|
+
const release = (reason) => ({
|
|
1145
|
+
type: "release",
|
|
1146
|
+
issue: issue.number,
|
|
1147
|
+
commentId: current.commentId,
|
|
1148
|
+
reason,
|
|
1149
|
+
});
|
|
1150
|
+
const move = (to, reason) => ({
|
|
1151
|
+
type: "set-state",
|
|
1152
|
+
issue: issue.number,
|
|
1153
|
+
from: "building",
|
|
1154
|
+
to,
|
|
1155
|
+
reason,
|
|
1156
|
+
});
|
|
1157
|
+
if (widerPathsRequestFor(issue.comments, isEngine, current.claim)) {
|
|
1158
|
+
// The builder asked for wider Allowed paths: a Decision Request is
|
|
1159
|
+
// posted, and the choice is a person's.
|
|
1160
|
+
actions.push(move("needs-decision", "wider-paths"), release("question"));
|
|
1161
|
+
running -= 1;
|
|
1162
|
+
continue;
|
|
1163
|
+
}
|
|
1164
|
+
if (buildQuestionsFor(issue.comments, isEngine, current.claim)) {
|
|
1165
|
+
// A factory-mode Chief of Staff ended on questions it cannot rule on
|
|
1166
|
+
// (ADR 0040): not a failed Attempt. Re-queuing re-plans with the answers.
|
|
1167
|
+
actions.push(move("needs-decision", "build-questions"), release("question"));
|
|
1168
|
+
running -= 1;
|
|
1169
|
+
continue;
|
|
1170
|
+
}
|
|
1171
|
+
if (buildFailureFor(issue.comments, isEngine, current.claim)) {
|
|
1172
|
+
actions.push(release("failed"), move("planned", "build-failed"));
|
|
1173
|
+
running -= 1;
|
|
1174
|
+
continue;
|
|
1175
|
+
}
|
|
1176
|
+
// Only the pull request on this Attempt's own branch answers this Claim.
|
|
1177
|
+
const pull = (issue.pulls ?? []).find(
|
|
1178
|
+
(p) => p.state === "OPEN" && p.branch === buildBranch(issue.number, current.attempt),
|
|
1179
|
+
);
|
|
1180
|
+
if (pull?.ci === "SUCCESS") {
|
|
1181
|
+
// A verified head: pushed, a pull request that closes the issue, CI green.
|
|
1182
|
+
actions.push(move("reviewing", "head-verified"), release("done"));
|
|
1183
|
+
running -= 1;
|
|
1184
|
+
continue;
|
|
1185
|
+
}
|
|
1186
|
+
if (pull?.ci === "FAILURE" || pull?.ci === "ERROR") {
|
|
1187
|
+
actions.push(release("failed"), move("planned", "ci-failed"));
|
|
1188
|
+
running -= 1;
|
|
1189
|
+
continue;
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
for (const claim of expiredNow) {
|
|
1193
|
+
actions.push(...cancelAndRelease(issue, claim));
|
|
1194
|
+
}
|
|
1195
|
+
if (expiredNow.length && unexpired.length === 0) {
|
|
1196
|
+
actions.push({
|
|
1197
|
+
type: "set-state",
|
|
1198
|
+
issue: issue.number,
|
|
1199
|
+
from: "building",
|
|
1200
|
+
to: "planned",
|
|
1201
|
+
reason: "claim-expired",
|
|
1202
|
+
});
|
|
1203
|
+
running -= 1;
|
|
1204
|
+
}
|
|
1205
|
+
continue;
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
if (state === "reviewing") {
|
|
1209
|
+
// The pull request the build Attempt opened, on that Attempt's own branch.
|
|
1210
|
+
const pull = buildPullOf(issue, claims);
|
|
1211
|
+
if (!pull?.head) {
|
|
1212
|
+
actions.push({
|
|
1213
|
+
type: "notice",
|
|
1214
|
+
issue: issue.number,
|
|
1215
|
+
code: "reviewing-without-a-pull-request",
|
|
1216
|
+
message:
|
|
1217
|
+
"In factory:reviewing with no open pull request on the build Attempt's branch: left alone.",
|
|
1218
|
+
});
|
|
1219
|
+
continue;
|
|
1220
|
+
}
|
|
1221
|
+
const reviewClaims = live.filter((c) => c.stage === "review");
|
|
1222
|
+
const releaseReviews = (reason) =>
|
|
1223
|
+
reviewClaims.map((claim) => ({
|
|
1224
|
+
type: "release",
|
|
1225
|
+
issue: issue.number,
|
|
1226
|
+
commentId: claim.commentId,
|
|
1227
|
+
reason,
|
|
1228
|
+
}));
|
|
1229
|
+
// The head's Stage check, read only when the engine wrote it. A new head
|
|
1230
|
+
// has none, so a fix push is always reviewed again.
|
|
1231
|
+
const check = engineStageCheck(read.statuses?.[pull.head], REVIEW_CONTEXT, isEngine);
|
|
1232
|
+
if (check?.state === "success") {
|
|
1233
|
+
actions.push(
|
|
1234
|
+
{
|
|
1235
|
+
type: "set-state",
|
|
1236
|
+
issue: issue.number,
|
|
1237
|
+
from: "reviewing",
|
|
1238
|
+
to: "checking",
|
|
1239
|
+
reason: "review-passed",
|
|
1240
|
+
},
|
|
1241
|
+
...releaseReviews("done"),
|
|
1242
|
+
);
|
|
1243
|
+
running -= 1;
|
|
1244
|
+
continue;
|
|
1245
|
+
}
|
|
1246
|
+
if (check?.state === "failure") {
|
|
1247
|
+
// One failed Attempt, whoever the Claim was: back to building with the findings.
|
|
1248
|
+
const failedAttempts = failedOf("build") + failedOf("review") + 1;
|
|
1249
|
+
// Under the Daily cap a task that cannot start its next build waits as it is: nothing
|
|
1250
|
+
// is released or noted yet, so the Attempt is counted once, when the build starts.
|
|
1251
|
+
if (failedAttempts < cfg.max_attempts && !admit(issue, "build")) continue;
|
|
1252
|
+
const note = `<!-- factory:review-findings${VERSION_TOKEN} head=${pull.head} -->`;
|
|
1253
|
+
const previousNote = note.replace(VERSION_TOKEN, "");
|
|
1254
|
+
const last = (issue.comments ?? []).filter(
|
|
1255
|
+
(c) => isEngine(c.author) && (c.body.includes(note) || c.body.includes(previousNote)),
|
|
1256
|
+
);
|
|
1257
|
+
if (last.length === 0) {
|
|
1258
|
+
actions.push({
|
|
1259
|
+
type: "comment",
|
|
1260
|
+
issue: issue.number,
|
|
1261
|
+
body:
|
|
1262
|
+
`${note}\nThe review Stage asked for changes on pull request #${pull.number} ` +
|
|
1263
|
+
`(${untrustedText(check.description, 140)}). The findings are its open review threads, ` +
|
|
1264
|
+
`which the next build Attempt works from.`,
|
|
1265
|
+
});
|
|
1266
|
+
}
|
|
1267
|
+
actions.push(...releaseReviews("failed"));
|
|
1268
|
+
if (failedAttempts >= cfg.max_attempts) {
|
|
1269
|
+
actions.push(
|
|
1270
|
+
...askAfterAttempts(
|
|
1271
|
+
issue,
|
|
1272
|
+
`The build and review Stages ran ${failedAttempts} times without a passing review`,
|
|
1273
|
+
failedAttempts,
|
|
1274
|
+
),
|
|
1275
|
+
);
|
|
1276
|
+
actions.push({
|
|
1277
|
+
type: "set-state",
|
|
1278
|
+
issue: issue.number,
|
|
1279
|
+
from: "reviewing",
|
|
1280
|
+
to: "needs-decision",
|
|
1281
|
+
reason: "attempts-exhausted",
|
|
1282
|
+
});
|
|
1283
|
+
running -= 1;
|
|
1284
|
+
continue;
|
|
1285
|
+
}
|
|
1286
|
+
const attempt = lastAttempt(claims, "build") + 1;
|
|
1287
|
+
const claim = newClaim("build", issue.number, attempt, applyVoid(issue, isEngine));
|
|
1288
|
+
actions.push({
|
|
1289
|
+
type: "claim",
|
|
1290
|
+
issue: issue.number,
|
|
1291
|
+
claim,
|
|
1292
|
+
body: renderClaimComment(claim),
|
|
1293
|
+
});
|
|
1294
|
+
actions.push({
|
|
1295
|
+
type: "set-state",
|
|
1296
|
+
issue: issue.number,
|
|
1297
|
+
from: "reviewing",
|
|
1298
|
+
to: "building",
|
|
1299
|
+
reason: "review-failed",
|
|
1300
|
+
});
|
|
1301
|
+
actions.push({
|
|
1302
|
+
type: "start-stage",
|
|
1303
|
+
issue: issue.number,
|
|
1304
|
+
stage: "build",
|
|
1305
|
+
attempt,
|
|
1306
|
+
claim: claim.claim,
|
|
1307
|
+
});
|
|
1308
|
+
continue;
|
|
1309
|
+
}
|
|
1310
|
+
// No check yet, `pending`, or `error`: the Stage is running or has ended
|
|
1311
|
+
// without a result. A Claim that is still live is waited for; the Claim's
|
|
1312
|
+
// expiry is what retries an `error` (a stale `error` stays on the head
|
|
1313
|
+
// until the next run writes `pending`, so it is not read as an answer).
|
|
1314
|
+
// An `error` newer than the live review Claim's own comment is that run's
|
|
1315
|
+
// answer (an older one is a leftover of an earlier run): the Claim is
|
|
1316
|
+
// released `error` and the review starts again, free the first time.
|
|
1317
|
+
const liveReview = unexpired.filter((c) => c.stage === "review").at(-1);
|
|
1318
|
+
if (
|
|
1319
|
+
check?.state === "error" &&
|
|
1320
|
+
liveReview &&
|
|
1321
|
+
Date.parse(check.createdAt) >= Date.parse(liveReview.createdAt)
|
|
1322
|
+
) {
|
|
1323
|
+
erroredNow.add(liveReview);
|
|
1324
|
+
actions.push({
|
|
1325
|
+
type: "release",
|
|
1326
|
+
issue: issue.number,
|
|
1327
|
+
commentId: liveReview.commentId,
|
|
1328
|
+
reason: "error",
|
|
1329
|
+
});
|
|
1330
|
+
}
|
|
1331
|
+
const reviewExpired = expiredNow.filter((c) => c.stage === "review");
|
|
1332
|
+
for (const claim of reviewExpired) {
|
|
1333
|
+
actions.push(...cancelAndRelease(issue, claim));
|
|
1334
|
+
}
|
|
1335
|
+
if (unexpired.some((c) => c.stage === "review" && !erroredNow.has(c))) continue;
|
|
1336
|
+
const failedReviews = failedOf("review");
|
|
1337
|
+
if (failedReviews >= cfg.max_attempts) {
|
|
1338
|
+
actions.push(
|
|
1339
|
+
...askAfterAttempts(
|
|
1340
|
+
issue,
|
|
1341
|
+
`The review Stage ran ${failedReviews} times without a result`,
|
|
1342
|
+
failedReviews,
|
|
1343
|
+
),
|
|
1344
|
+
);
|
|
1345
|
+
actions.push({
|
|
1346
|
+
type: "set-state",
|
|
1347
|
+
issue: issue.number,
|
|
1348
|
+
from: "reviewing",
|
|
1349
|
+
to: "needs-decision",
|
|
1350
|
+
reason: "attempts-exhausted",
|
|
1351
|
+
});
|
|
1352
|
+
running -= 1;
|
|
1353
|
+
continue;
|
|
1354
|
+
}
|
|
1355
|
+
if (!admit(issue, "review")) continue;
|
|
1356
|
+
const attempt = failedReviews + 1;
|
|
1357
|
+
const claim = newClaim("review", issue.number, attempt);
|
|
1358
|
+
actions.push({ type: "claim", issue: issue.number, claim, body: renderClaimComment(claim) });
|
|
1359
|
+
actions.push({
|
|
1360
|
+
type: "start-stage",
|
|
1361
|
+
issue: issue.number,
|
|
1362
|
+
stage: "review",
|
|
1363
|
+
attempt,
|
|
1364
|
+
claim: claim.claim,
|
|
1365
|
+
});
|
|
1366
|
+
continue;
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
if (state === "checking") {
|
|
1370
|
+
const pull = buildPullOf(issue, claims);
|
|
1371
|
+
if (!pull?.head) {
|
|
1372
|
+
actions.push({
|
|
1373
|
+
type: "notice",
|
|
1374
|
+
issue: issue.number,
|
|
1375
|
+
code: "checking-without-a-pull-request",
|
|
1376
|
+
message:
|
|
1377
|
+
"In factory:checking with no open pull request on the build Attempt's branch: left alone.",
|
|
1378
|
+
});
|
|
1379
|
+
continue;
|
|
1380
|
+
}
|
|
1381
|
+
// Both Stage checks on the pull request's current head, read only when
|
|
1382
|
+
// the engine wrote them. A new head has neither, so it is checked again.
|
|
1383
|
+
const statuses = read.statuses?.[pull.head];
|
|
1384
|
+
const qa = engineStageCheck(statuses, QA_CONTEXT, isEngine);
|
|
1385
|
+
const security = engineStageCheck(statuses, SECURITY_CONTEXT, isEngine);
|
|
1386
|
+
const failedStages = [
|
|
1387
|
+
qa?.state === "failure" ? "qa" : null,
|
|
1388
|
+
security?.state === "failure" ? "security" : null,
|
|
1389
|
+
].filter(Boolean);
|
|
1390
|
+
if (failedStages.length === 0) {
|
|
1391
|
+
if (qa?.state === "success" && securityPassed(security)) {
|
|
1392
|
+
actions.push({
|
|
1393
|
+
type: "set-state",
|
|
1394
|
+
issue: issue.number,
|
|
1395
|
+
from: "checking",
|
|
1396
|
+
to: "ready-to-merge",
|
|
1397
|
+
reason: "checks-passed",
|
|
1398
|
+
});
|
|
1399
|
+
running -= 1;
|
|
1400
|
+
}
|
|
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
|
+
continue;
|
|
1404
|
+
}
|
|
1405
|
+
// Either check failing sends the task back to building. The note names
|
|
1406
|
+
// the Stage and never what it found: a security hold's findings are in
|
|
1407
|
+
// the private draft advisory only.
|
|
1408
|
+
const notes = {
|
|
1409
|
+
qa: `The QA Stage failed on pull request #${pull.number}.`,
|
|
1410
|
+
security: `The security Stage held pull request #${pull.number} for a maintainer. Anything it found is in the private draft advisory only.`,
|
|
1411
|
+
};
|
|
1412
|
+
for (const stage of failedStages) {
|
|
1413
|
+
const marker = checkFailedMarker(stage, pull.head);
|
|
1414
|
+
if (
|
|
1415
|
+
(issue.comments ?? []).some(
|
|
1416
|
+
(c) =>
|
|
1417
|
+
isEngine(c.author) &&
|
|
1418
|
+
(c.body.includes(marker) || c.body.includes(marker.replace(VERSION_TOKEN, ""))),
|
|
1419
|
+
)
|
|
1420
|
+
) {
|
|
1421
|
+
continue;
|
|
1422
|
+
}
|
|
1423
|
+
actions.push({
|
|
1424
|
+
type: "comment",
|
|
1425
|
+
issue: issue.number,
|
|
1426
|
+
body: `${marker}\n${notes[stage]} The next build Attempt works on a new head.`,
|
|
1427
|
+
});
|
|
1428
|
+
}
|
|
1429
|
+
// One failed Attempt: the earlier failed builds and reviews, the heads
|
|
1430
|
+
// noted failed before this one, and this head.
|
|
1431
|
+
const earlier = [...failedCheckHeads(issue, isEngine)].filter((h) => h !== pull.head).length;
|
|
1432
|
+
const failedAttempts = failedOf("build") + failedOf("review") + earlier + 1;
|
|
1433
|
+
if (failedAttempts >= cfg.max_attempts) {
|
|
1434
|
+
actions.push(
|
|
1435
|
+
...askAfterAttempts(
|
|
1436
|
+
issue,
|
|
1437
|
+
`The build, review and check Stages ran ${failedAttempts} times without passing every check`,
|
|
1438
|
+
failedAttempts,
|
|
1439
|
+
),
|
|
1440
|
+
);
|
|
1441
|
+
actions.push({
|
|
1442
|
+
type: "set-state",
|
|
1443
|
+
issue: issue.number,
|
|
1444
|
+
from: "checking",
|
|
1445
|
+
to: "needs-decision",
|
|
1446
|
+
reason: "attempts-exhausted",
|
|
1447
|
+
});
|
|
1448
|
+
running -= 1;
|
|
1449
|
+
continue;
|
|
1450
|
+
}
|
|
1451
|
+
if (!admit(issue, "build", { keepHeld: true })) continue;
|
|
1452
|
+
const attempt = lastAttempt(claims, "build") + 1;
|
|
1453
|
+
const claim = newClaim("build", issue.number, attempt, applyVoid(issue, isEngine));
|
|
1454
|
+
actions.push({ type: "claim", issue: issue.number, claim, body: renderClaimComment(claim) });
|
|
1455
|
+
actions.push({
|
|
1456
|
+
type: "set-state",
|
|
1457
|
+
issue: issue.number,
|
|
1458
|
+
from: "checking",
|
|
1459
|
+
to: "building",
|
|
1460
|
+
reason: "checks-failed",
|
|
1461
|
+
});
|
|
1462
|
+
actions.push({
|
|
1463
|
+
type: "start-stage",
|
|
1464
|
+
issue: issue.number,
|
|
1465
|
+
stage: "build",
|
|
1466
|
+
attempt,
|
|
1467
|
+
claim: claim.claim,
|
|
1468
|
+
});
|
|
1469
|
+
continue;
|
|
1470
|
+
}
|
|
1471
|
+
|
|
1472
|
+
if (state === "ready-to-merge") {
|
|
1473
|
+
// The merge gate (docs/factory/merge-gate.md). A hand merge shows as a
|
|
1474
|
+
// merged pull request on the build Attempt's branch, on an issue that
|
|
1475
|
+
// GitHub has closed, so that is read here whatever the pull's state was.
|
|
1476
|
+
const lastBuild = claims.filter((c) => c.stage === "build").at(-1);
|
|
1477
|
+
const onBranch = (issue.pulls ?? []).filter(
|
|
1478
|
+
(p) => lastBuild && p.branch === buildBranch(issue.number, lastBuild.attempt),
|
|
1479
|
+
);
|
|
1480
|
+
// Any merged pull request of this task's own branches counts, whichever Attempt wrote it.
|
|
1481
|
+
const mergedPull = (issue.pulls ?? []).some(
|
|
1482
|
+
(p) => p.state === "MERGED" && p.branch.startsWith(`factory/${issue.number}/attempt-`),
|
|
1483
|
+
);
|
|
1484
|
+
const pull = onBranch.find((p) => p.state === "OPEN") ?? onBranch[0];
|
|
1485
|
+
const gate = gateActions({
|
|
1486
|
+
issue,
|
|
1487
|
+
pull,
|
|
1488
|
+
mergedPull,
|
|
1489
|
+
read,
|
|
1490
|
+
cfg,
|
|
1491
|
+
ctx,
|
|
1492
|
+
isEngine,
|
|
1493
|
+
mergeTaken,
|
|
1494
|
+
trip,
|
|
1495
|
+
});
|
|
1496
|
+
actions.push(...gate.actions);
|
|
1497
|
+
if (gate.merges) mergeTaken = true;
|
|
1498
|
+
continue;
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
if (state !== "queued") continue;
|
|
1502
|
+
|
|
1503
|
+
if (!labels.includes(READY_LABEL)) {
|
|
1504
|
+
// A live Claim on an issue that will not run is released, never left to hold a slot.
|
|
1505
|
+
for (const claim of live) {
|
|
1506
|
+
actions.push({
|
|
1507
|
+
type: "release",
|
|
1508
|
+
issue: issue.number,
|
|
1509
|
+
commentId: claim.commentId,
|
|
1510
|
+
reason: "stopped",
|
|
1511
|
+
});
|
|
1512
|
+
}
|
|
1513
|
+
const last = (issue.comments ?? []).at(-1);
|
|
1514
|
+
const marker = noteMarker("not-ready");
|
|
1515
|
+
// Only the engine's own note counts: anyone can type the marker.
|
|
1516
|
+
const noted =
|
|
1517
|
+
last &&
|
|
1518
|
+
sweepBare(last.author) === sweepBare(ctx.engine.identity) &&
|
|
1519
|
+
hasNote(last.body, "not-ready");
|
|
1520
|
+
if (!noted) {
|
|
1521
|
+
actions.push({
|
|
1522
|
+
type: "comment",
|
|
1523
|
+
issue: issue.number,
|
|
1524
|
+
body:
|
|
1525
|
+
`${marker}\nThis issue was queued without \`${READY_LABEL}\`, so the Factory moved it to ` +
|
|
1526
|
+
`\`${stateLabel("needs-decision")}\`. Once it is triaged and carries \`${READY_LABEL}\`, ` +
|
|
1527
|
+
`a person can set \`${stateLabel("queued")}\` again.`,
|
|
1528
|
+
});
|
|
1529
|
+
}
|
|
1530
|
+
actions.push({
|
|
1531
|
+
type: "set-state",
|
|
1532
|
+
issue: issue.number,
|
|
1533
|
+
from: "queued",
|
|
1534
|
+
to: "needs-decision",
|
|
1535
|
+
reason: "missing-ready-for-agent",
|
|
1536
|
+
});
|
|
1537
|
+
continue;
|
|
1538
|
+
}
|
|
1539
|
+
|
|
1540
|
+
// A person swapped `needs-decision` for `queued` after a Decision Request:
|
|
1541
|
+
// the swap is the re-queue, and it needs the answer to be there.
|
|
1542
|
+
let approved = false;
|
|
1543
|
+
if (lastRequest) {
|
|
1544
|
+
const answered = readAnswers(issue.comments, { isEngine, writers: issue.writers });
|
|
1545
|
+
if (answered.status === "unknown") {
|
|
1546
|
+
actions.push({
|
|
1547
|
+
type: "notice",
|
|
1548
|
+
issue: issue.number,
|
|
1549
|
+
code: "answer-unreadable",
|
|
1550
|
+
message:
|
|
1551
|
+
"The Decision Request's replies cannot be judged: who has write access could not be read. Left queued until the next sweep.",
|
|
1552
|
+
});
|
|
1553
|
+
continue;
|
|
1554
|
+
}
|
|
1555
|
+
if (answered.status === "empty") {
|
|
1556
|
+
const marker = noteMarker("empty-answer");
|
|
1557
|
+
const body =
|
|
1558
|
+
`${marker}\nThis task was set to \`${stateLabel("queued")}\`, but ${nameAnswers(answered.empty)} ` +
|
|
1559
|
+
`${answered.empty.length === 1 ? "is" : "are"} empty in the replies from people with write access, ` +
|
|
1560
|
+
`so the Factory moved it back to \`${stateLabel("needs-decision")}\`. ` +
|
|
1561
|
+
"Reply to the Decision Request with every `A<n>:` filled in, then set `" +
|
|
1562
|
+
`${stateLabel("queued")}\` again.`;
|
|
1563
|
+
const last = (issue.comments ?? []).at(-1);
|
|
1564
|
+
// The same note written by the previous version (no `v` in its marker) is the same note.
|
|
1565
|
+
const sameBody = (text) => text === body || text === body.replace(VERSION_TOKEN, "");
|
|
1566
|
+
if (!(last && isEngine(last.author) && sameBody(last.body))) {
|
|
1567
|
+
actions.push({ type: "comment", issue: issue.number, body });
|
|
1568
|
+
}
|
|
1569
|
+
actions.push({
|
|
1570
|
+
type: "set-state",
|
|
1571
|
+
issue: issue.number,
|
|
1572
|
+
from: "queued",
|
|
1573
|
+
to: "needs-decision",
|
|
1574
|
+
reason: "empty-answer",
|
|
1575
|
+
});
|
|
1576
|
+
continue;
|
|
1577
|
+
}
|
|
1578
|
+
// Approval or drafted criteria, answered yes for the plan still on the issue:
|
|
1579
|
+
// that plan stands, so the task goes on to build without planning again.
|
|
1580
|
+
const plan = latestPlan(issue.comments, isEngine);
|
|
1581
|
+
approved =
|
|
1582
|
+
answered.affirmative &&
|
|
1583
|
+
answered.request.kinds.length > 0 &&
|
|
1584
|
+
!answered.request.kinds.includes("questions") &&
|
|
1585
|
+
// A "try again" after failed Attempts approves no plan: the plan Stage runs again.
|
|
1586
|
+
!answered.request.kinds.includes("attempts") &&
|
|
1587
|
+
plan !== null &&
|
|
1588
|
+
plan.n === answered.request.plan &&
|
|
1589
|
+
checkPlanHeader(plan.data, { trusted: true }).ok;
|
|
1590
|
+
}
|
|
1591
|
+
if (approved) {
|
|
1592
|
+
for (const claim of live) {
|
|
1593
|
+
actions.push({
|
|
1594
|
+
type: "release",
|
|
1595
|
+
issue: issue.number,
|
|
1596
|
+
commentId: claim.commentId,
|
|
1597
|
+
reason: "stopped",
|
|
1598
|
+
});
|
|
1599
|
+
}
|
|
1600
|
+
// An approved plan that splits is not built: it is split (a child's, stopped).
|
|
1601
|
+
const approvedPlan = latestPlan(issue.comments, isEngine);
|
|
1602
|
+
const split = checkPlanHeader(approvedPlan.data, { trusted: true }).header.split;
|
|
1603
|
+
if (split?.length) {
|
|
1604
|
+
const child = childOf(issue);
|
|
1605
|
+
if (child) {
|
|
1606
|
+
actions.push(
|
|
1607
|
+
...splitTooDeepActions(
|
|
1608
|
+
issue,
|
|
1609
|
+
{ n: approvedPlan.n, split },
|
|
1610
|
+
child,
|
|
1611
|
+
lastRequest?.attempt ?? 1,
|
|
1612
|
+
),
|
|
1613
|
+
);
|
|
1614
|
+
actions.push({
|
|
1615
|
+
type: "set-state",
|
|
1616
|
+
issue: issue.number,
|
|
1617
|
+
from: "queued",
|
|
1618
|
+
to: "needs-decision",
|
|
1619
|
+
reason: "split-too-deep",
|
|
1620
|
+
});
|
|
1621
|
+
} else {
|
|
1622
|
+
actions.push(...splitActions(issue, approvedPlan, split, { from: "queued" }));
|
|
1623
|
+
}
|
|
1624
|
+
continue;
|
|
1625
|
+
}
|
|
1626
|
+
actions.push({
|
|
1627
|
+
type: "set-state",
|
|
1628
|
+
issue: issue.number,
|
|
1629
|
+
from: "queued",
|
|
1630
|
+
to: "planned",
|
|
1631
|
+
reason: "plan-approved",
|
|
1632
|
+
});
|
|
1633
|
+
continue;
|
|
1634
|
+
}
|
|
1635
|
+
|
|
1636
|
+
for (const claim of expiredNow) {
|
|
1637
|
+
actions.push(...cancelAndRelease(issue, claim));
|
|
1638
|
+
}
|
|
1639
|
+
|
|
1640
|
+
// A Claim this engine wrote, still live, on an issue that is still queued:
|
|
1641
|
+
// a sweep ended between the Claim and the move. Carry on with that Claim,
|
|
1642
|
+
// never write a second one. The start follows the move, so no Stage runs yet.
|
|
1643
|
+
if (unexpired.length) {
|
|
1644
|
+
const claim = unexpired[0];
|
|
1645
|
+
actions.push({
|
|
1646
|
+
type: "set-state",
|
|
1647
|
+
issue: issue.number,
|
|
1648
|
+
from: "queued",
|
|
1649
|
+
to: "planning",
|
|
1650
|
+
reason: "resume-claim",
|
|
1651
|
+
});
|
|
1652
|
+
actions.push({
|
|
1653
|
+
type: "start-stage",
|
|
1654
|
+
issue: issue.number,
|
|
1655
|
+
stage: claim.stage,
|
|
1656
|
+
attempt: claim.attempt,
|
|
1657
|
+
claim: claim.claim,
|
|
1658
|
+
});
|
|
1659
|
+
running += 1;
|
|
1660
|
+
continue;
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
if (failed >= cfg.max_attempts) {
|
|
1664
|
+
actions.push(
|
|
1665
|
+
...askAfterAttempts(issue, `The plan Stage ran ${failed} times without a result`, failed),
|
|
1666
|
+
);
|
|
1667
|
+
actions.push({
|
|
1668
|
+
type: "set-state",
|
|
1669
|
+
issue: issue.number,
|
|
1670
|
+
from: "queued",
|
|
1671
|
+
to: "needs-decision",
|
|
1672
|
+
reason: "attempts-exhausted",
|
|
1673
|
+
});
|
|
1674
|
+
continue;
|
|
1675
|
+
}
|
|
1676
|
+
|
|
1677
|
+
// A task an open issue blocks (GitHub's native "blocked by") is not planned.
|
|
1678
|
+
const blockers = (issue.blockedBy ?? []).filter((b) => b.state !== "CLOSED");
|
|
1679
|
+
if (blockers.length > 0 || issue.blockedByTruncated) {
|
|
1680
|
+
actions.push({
|
|
1681
|
+
type: "notice",
|
|
1682
|
+
issue: issue.number,
|
|
1683
|
+
code: "blocked",
|
|
1684
|
+
message: blockers.length
|
|
1685
|
+
? `Waiting to plan: blocked by open ${blockers.map((b) => `#${b.number}`).join(", ")}.`
|
|
1686
|
+
: "Waiting to plan: it has more blockers than the sweep reads, so it cannot tell that none is open.",
|
|
1687
|
+
});
|
|
1688
|
+
continue;
|
|
1689
|
+
}
|
|
1690
|
+
// A child waits while its parent is still splitting: its blockers may not exist yet.
|
|
1691
|
+
const parentOfChild = childOf(issue)?.parent;
|
|
1692
|
+
if (parentOfChild !== undefined && stillSplitting(parentOfChild)) {
|
|
1693
|
+
actions.push({
|
|
1694
|
+
type: "notice",
|
|
1695
|
+
issue: issue.number,
|
|
1696
|
+
code: "split-pending",
|
|
1697
|
+
message: `Waiting to plan: #${parentOfChild} is still splitting, so this sub-issue's blockers may not exist yet.`,
|
|
1698
|
+
});
|
|
1699
|
+
continue;
|
|
1700
|
+
}
|
|
1701
|
+
if (running >= cfg.max_running) continue;
|
|
1702
|
+
if (!admit(issue, "plan")) continue;
|
|
1703
|
+
|
|
1704
|
+
const attempt = failed + 1;
|
|
1705
|
+
const stage = "plan";
|
|
1706
|
+
const claim = newClaim(stage, issue.number, attempt);
|
|
1707
|
+
actions.push({ type: "claim", issue: issue.number, claim, body: renderClaimComment(claim) });
|
|
1708
|
+
actions.push({ type: "set-state", issue: issue.number, from: "queued", to: "planning" });
|
|
1709
|
+
actions.push({
|
|
1710
|
+
type: "start-stage",
|
|
1711
|
+
issue: issue.number,
|
|
1712
|
+
stage,
|
|
1713
|
+
attempt,
|
|
1714
|
+
claim: claim.claim,
|
|
1715
|
+
});
|
|
1716
|
+
running += 1;
|
|
1717
|
+
}
|
|
1718
|
+
closeParents();
|
|
1719
|
+
return actions;
|
|
1720
|
+
|
|
1721
|
+
/**
|
|
1722
|
+
* A parent closes once every child its list names is merged (#508). A child counts when it is
|
|
1723
|
+
* in `merged`, or the gate records it merged in this very sweep; a sibling the sweep no longer
|
|
1724
|
+
* reads counts when the shell read it closed with `factory:merged`. The close goes before the
|
|
1725
|
+
* child's own `set-state merged`, so a close that fails leaves that child where the next sweep
|
|
1726
|
+
* finds it again.
|
|
1727
|
+
*/
|
|
1728
|
+
function closeParents() {
|
|
1729
|
+
const triggers = new Map();
|
|
1730
|
+
for (const raw of numbered) {
|
|
1731
|
+
const mark = childOf(raw);
|
|
1732
|
+
if (!mark) continue;
|
|
1733
|
+
const at = actions.findIndex(
|
|
1734
|
+
(a) => a.type === "set-state" && a.issue === raw.number && a.to === "merged",
|
|
1735
|
+
);
|
|
1736
|
+
if (at === -1 && stateOf(raw) !== "merged") continue;
|
|
1737
|
+
if (!triggers.has(mark.parent)) triggers.set(mark.parent, []);
|
|
1738
|
+
triggers.get(mark.parent).push({ number: raw.number, at });
|
|
1739
|
+
}
|
|
1740
|
+
for (const [parent, merging] of triggers) {
|
|
1741
|
+
const info = read.splitParents?.[parent];
|
|
1742
|
+
if (!info) continue;
|
|
1743
|
+
if (info.error) {
|
|
1744
|
+
actions.push({
|
|
1745
|
+
type: "notice",
|
|
1746
|
+
issue: merging[0].number,
|
|
1747
|
+
code: "split-parent-unreadable",
|
|
1748
|
+
message: `#${parent} could not be read (${info.error}): it is not closed this sweep.`,
|
|
1749
|
+
});
|
|
1750
|
+
continue;
|
|
1751
|
+
}
|
|
1752
|
+
if (info.state === "CLOSED") continue;
|
|
1753
|
+
const listed = applyVoid({ comments: info.comments ?? [] }, isEngine)
|
|
1754
|
+
.comments.filter((c) => isEngine(c.author))
|
|
1755
|
+
.map((c) => parseSplitComment(c.body))
|
|
1756
|
+
.filter(Boolean)
|
|
1757
|
+
.at(-1);
|
|
1758
|
+
if (!listed) continue;
|
|
1759
|
+
const done = (n) =>
|
|
1760
|
+
merging.some((m) => m.number === n) ||
|
|
1761
|
+
(info.children ?? []).some((c) => c.number === n && c.merged === true);
|
|
1762
|
+
if (!listed.children.every(done)) continue;
|
|
1763
|
+
const placed = merging.filter((m) => m.at !== -1).at(-1);
|
|
1764
|
+
const close = {
|
|
1765
|
+
type: "close-parent",
|
|
1766
|
+
issue: (placed ?? merging[0]).number,
|
|
1767
|
+
parent,
|
|
1768
|
+
children: listed.children,
|
|
1769
|
+
};
|
|
1770
|
+
if (placed) {
|
|
1771
|
+
actions.splice(
|
|
1772
|
+
actions.findIndex(
|
|
1773
|
+
(a) => a.type === "set-state" && a.issue === placed.number && a.to === "merged",
|
|
1774
|
+
),
|
|
1775
|
+
0,
|
|
1776
|
+
close,
|
|
1777
|
+
);
|
|
1778
|
+
} else actions.push(close);
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
}
|