@oakgreencc/cambium 0.0.27 → 0.0.29
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 +81 -60
- package/package.json +1 -1
- package/src/cli.mjs +1 -1
- package/src/config.mjs +5 -2
- package/src/github.mjs +8 -1
- package/src/graph-engine.mjs +95 -39
- package/src/graphs/dispatch.mjs +6 -8
- package/src/graphs/pr.mjs +420 -0
- package/src/graphs/task.mjs +67 -321
- package/src/marker.mjs +3 -1
- package/src/phases/task.mjs +4 -0
- package/src/tick.mjs +2 -8
- package/src/workers.mjs +177 -14
package/src/graphs/task.mjs
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
/**
|
|
3
|
-
* The Task phase as a graph:
|
|
4
|
-
*
|
|
5
|
-
* through `
|
|
3
|
+
* The Task phase as a graph: a worker turns a `q:task` issue into a published PR, and the graph ends there
|
|
4
|
+
* (`submit`). What happens to the PR after that (merge, rework, close, the Human Queue) is the PR graph's
|
|
5
|
+
* (graphs/pr.mjs); a rework comes back here through `q:task`. Each node declares the queue label it sits in and
|
|
6
|
+
* whether it holds `running`; every label change goes through `move()` from that declaration (see {@link landAt}).
|
|
6
7
|
*
|
|
7
|
-
* Step nodes write their own transition (marker, then labels) before or after their side effect
|
|
8
|
-
*
|
|
8
|
+
* Step nodes write their own transition (marker, then labels) before or after their side effect. A wait node
|
|
9
|
+
* with `enter` is written by the runtime when an edge lands on it.
|
|
9
10
|
*/
|
|
10
11
|
import { z } from 'zod'
|
|
11
12
|
import { defineGraph, defineNode, defineWait, END } from '../graph/index.mjs'
|
|
12
13
|
import { branchName } from '../convention.mjs'
|
|
13
|
-
import {
|
|
14
|
+
import { iso, key, markerOf, setLabels, setMarker } from '../issue-state.mjs'
|
|
14
15
|
import { move, Q, WHY } from '../labels.mjs'
|
|
15
16
|
import { withHistory } from '../marker.mjs'
|
|
16
17
|
import { linkIssue, prFor, taskExit, taskPrompt } from '../phases/task.mjs'
|
|
17
18
|
import { PREPARE_FAILED_LINE } from '../prepare.mjs'
|
|
18
19
|
import { traceWorker } from '../tracing.mjs'
|
|
19
20
|
import { baseRed, isRepair } from '../base-health.mjs'
|
|
21
|
+
import { stopOverdue } from '../workers.mjs'
|
|
20
22
|
|
|
21
23
|
/**
|
|
22
24
|
* @typedef {import('../github.mjs').Issue} Issue
|
|
@@ -51,17 +53,17 @@ import { baseRed, isRepair } from '../base-health.mjs'
|
|
|
51
53
|
* @template {import('../graph/define.mjs').GraphNode} N @param {N} node @param {Declared} decl
|
|
52
54
|
* @returns {N & Declared}
|
|
53
55
|
*/
|
|
54
|
-
const declare = (node, decl) => Object.freeze({ ...node, ...decl })
|
|
56
|
+
export const declare = (node, decl) => Object.freeze({ ...node, ...decl })
|
|
55
57
|
|
|
56
58
|
/** @param {TaskCtx} ctx */
|
|
57
|
-
function issueOf(ctx) {
|
|
59
|
+
export function issueOf(ctx) {
|
|
58
60
|
const { issue } = ctx.subject
|
|
59
61
|
if (!issue) throw new Error(`${ctx.subject.slug}#${ctx.subject.number} is not in the snapshot`)
|
|
60
62
|
return issue
|
|
61
63
|
}
|
|
62
64
|
|
|
63
65
|
/** @param {TaskCtx} ctx */
|
|
64
|
-
function snapOf(ctx) {
|
|
66
|
+
export function snapOf(ctx) {
|
|
65
67
|
const { snap } = ctx.subject
|
|
66
68
|
if (!snap) throw new Error(`${ctx.subject.slug} has no snapshot this tick`)
|
|
67
69
|
return snap
|
|
@@ -71,20 +73,21 @@ function snapOf(ctx) {
|
|
|
71
73
|
const graphMarker = (m) => /** @type {Marker} */ (Object.fromEntries(Object.entries(m).filter(([k]) => k !== 'stage')))
|
|
72
74
|
|
|
73
75
|
/**
|
|
74
|
-
* Put the issue at `node`: the marker first, then the labels `node` declares, via `move()`. A
|
|
75
|
-
* transition is re-done (and over-counted) on the next tick, never under-counted.
|
|
76
|
+
* Put the issue at `node` of graph `g`: the marker first, then the labels `node` declares, via `move()`. A
|
|
77
|
+
* half-applied transition is re-done (and over-counted) on the next tick, never under-counted.
|
|
76
78
|
*
|
|
77
|
-
* @param {TaskCtx} ctx @param {string} node @param {Marker} marker
|
|
79
|
+
* @param {import('../graph/define.mjs').Graph} g @param {TaskCtx} ctx @param {string} node @param {Marker} marker
|
|
78
80
|
* @param {{ extra?: string[], status?: Marker['status'] }} [opts]
|
|
79
81
|
*/
|
|
80
|
-
async function
|
|
82
|
+
export async function landAt(g, ctx, node, marker, { extra = [], status = 'waiting' } = {}) {
|
|
81
83
|
const { slug } = ctx.subject
|
|
82
84
|
const issue = issueOf(ctx)
|
|
83
|
-
const decl =
|
|
85
|
+
const decl = /** @type {Declared | undefined} */ (g.nodes.find((n) => n.name === node))
|
|
86
|
+
if (!decl) throw new Error(`${g.name} graph has no node "${node}"`)
|
|
84
87
|
await setMarker(ctx, slug, issue, {
|
|
85
88
|
...graphMarker(marker),
|
|
86
|
-
graph:
|
|
87
|
-
graphVersion:
|
|
89
|
+
graph: g.name,
|
|
90
|
+
graphVersion: g.version,
|
|
88
91
|
node,
|
|
89
92
|
status,
|
|
90
93
|
enteredAt: iso(ctx.tick.now),
|
|
@@ -93,9 +96,12 @@ async function land(ctx, node, marker, { extra = [], status = 'waiting' } = {})
|
|
|
93
96
|
ctx.tick.touched.add(key(slug, issue.number))
|
|
94
97
|
}
|
|
95
98
|
|
|
99
|
+
/** {@link landAt} in the task graph. @param {TaskCtx} ctx @param {string} node @param {Marker} marker @param {{ extra?: string[], status?: Marker['status'] }} [opts] */
|
|
100
|
+
const land = (ctx, node, marker, opts) => landAt(task, ctx, node, marker, opts)
|
|
101
|
+
|
|
96
102
|
// ---- Schemas
|
|
97
103
|
|
|
98
|
-
const failure = {
|
|
104
|
+
export const failure = {
|
|
99
105
|
reason: z.string(),
|
|
100
106
|
outcome: z.string(),
|
|
101
107
|
pr: z.number().int().optional(),
|
|
@@ -107,88 +113,18 @@ const failure = {
|
|
|
107
113
|
const Failure = z.object(failure)
|
|
108
114
|
const Empty = z.object({})
|
|
109
115
|
const WorkerIn = z.object({ branch: z.string().nullable(), attempt: z.number().int() })
|
|
110
|
-
const PrIn = z.object({ pr: z.number().int().nullable(), branch: z.string().nullable(), log: z.string().optional() })
|
|
111
|
-
const PrOut = z.discriminatedUnion('to', [
|
|
112
|
-
z.object({ to: z.literal('merged'), pr: z.number().int() }),
|
|
113
|
-
z.object({ to: z.literal('queued'), pr: z.number().int(), branch: z.string() }),
|
|
114
|
-
z.object({ to: z.literal('merge'), pr: z.number().int(), branch: z.string() }),
|
|
115
|
-
z.object({ to: z.literal('human'), ...failure }),
|
|
116
|
-
z.object({ to: z.literal('requeue'), ...failure }),
|
|
117
|
-
])
|
|
118
|
-
|
|
119
116
|
// ---- Edge predicates (their names label the Mermaid edges)
|
|
120
117
|
|
|
121
118
|
const started = (/** @type {{ to: string }} */ o) => o.to === 'running'
|
|
122
119
|
const budgetSpent = (/** @type {{ to: string }} */ o) => o.to === 'human'
|
|
123
120
|
const issueGone = (/** @type {{ exit: string }} */ o) => o.exit === 'gone'
|
|
124
121
|
const submitted = (/** @type {{ exit: string }} */ o) => o.exit === 'submitted'
|
|
125
|
-
const merged = (/** @type {{ to: string }} */ o) => o.to === 'merged'
|
|
126
|
-
const inMergeQueue = (/** @type {{ to: string }} */ o) => o.to === 'queued'
|
|
127
|
-
const green = (/** @type {{ to: string }} */ o) => o.to === 'merge'
|
|
128
|
-
const stalled = (/** @type {{ to: string }} */ o) => o.to === 'human'
|
|
129
122
|
const attemptsLeft = (/** @type {{ to: string }} */ o) => o.to === 'start'
|
|
130
123
|
|
|
131
124
|
// ---- Shared checks
|
|
132
125
|
|
|
133
126
|
/** A history entry's `node` from a failure's `at`, when it names one. @param {{ at?: string }} what */
|
|
134
|
-
const nodeAt = (what) => (what.at ? { node: what.at } : {})
|
|
135
|
-
|
|
136
|
-
/** The PR a wait is watching: Watch's pair, else the marker's PR, else the issue's. @param {TaskCtx} ctx @param {{ pr: number | null, branch: string | null }} input */
|
|
137
|
-
function prOf(ctx, input) {
|
|
138
|
-
if (ctx.subject.pr) return ctx.subject.pr
|
|
139
|
-
const snap = snapOf(ctx)
|
|
140
|
-
return (
|
|
141
|
-
(input.pr !== null && snap.prs.find((p) => p.number === input.pr)) ||
|
|
142
|
-
prFor(snap, { issue: ctx.subject.number, branch: input.branch })
|
|
143
|
-
)
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/** Red on a commit CI has not already failed on. @param {PullRequest} pr @param {Marker} m */
|
|
147
|
-
const redCi = (pr, m) =>
|
|
148
|
-
(pr.checks === 'FAILURE' || pr.checks === 'ERROR') && pr.headSha !== m.failedSha
|
|
149
|
-
? /** @type {const} */ ({
|
|
150
|
-
to: 'requeue',
|
|
151
|
-
reason: failureSummary(pr),
|
|
152
|
-
outcome: 'ci-red',
|
|
153
|
-
pr: pr.number,
|
|
154
|
-
failedSha: pr.headSha,
|
|
155
|
-
})
|
|
156
|
-
: null
|
|
157
|
-
|
|
158
|
-
/** Not merged within `mergeQueueMinutes` of submission. @param {TaskCtx} ctx @param {PullRequest} pr @param {Marker} m */
|
|
159
|
-
function stall(ctx, pr, m) {
|
|
160
|
-
const cap = ctx.config.mergeQueueMinutes
|
|
161
|
-
if (!m.submittedAt || ctx.tick.now - Date.parse(m.submittedAt) <= cap * MIN) return null
|
|
162
|
-
return /** @type {const} */ ({
|
|
163
|
-
to: 'human',
|
|
164
|
-
reason: `PR #${pr.number} not merged within ${cap} min of submission`,
|
|
165
|
-
outcome: 'stalled',
|
|
166
|
-
pr: pr.number,
|
|
167
|
-
})
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/** How long a refused merge holds before cambium asks again on the same head (a red base clears without a push). */
|
|
171
|
-
export const MERGE_RETRY_MINUTES = 10
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Green, and cambium merges this repo's PRs (`merge: "green"`): nothing armed yet, and no refusal on this head
|
|
175
|
-
* within {@link MERGE_RETRY_MINUTES}.
|
|
176
|
-
* @param {TaskCtx} ctx @param {PullRequest} pr @param {Marker} m
|
|
177
|
-
*/
|
|
178
|
-
function mergeDue(ctx, pr, m) {
|
|
179
|
-
const repo = ctx.config.repos.find((r) => r.slug === ctx.subject.slug)
|
|
180
|
-
if (repo?.merge !== 'green' || pr.checks !== 'SUCCESS' || pr.autoMerge || pr.inMergeQueue) return false
|
|
181
|
-
const r = m.mergeRefusal
|
|
182
|
-
return !r || r.sha !== pr.headSha || ctx.tick.now - Date.parse(r.at) > MERGE_RETRY_MINUTES * MIN
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/** Delete a merged PR's branch once per tick. @param {TaskCtx} ctx @param {PullRequest} pr */
|
|
186
|
-
export async function deleteMergedBranch(ctx, pr) {
|
|
187
|
-
const deleted = ctx.subject.deleted ?? new Set()
|
|
188
|
-
if (!pr.branchExists || deleted.has(pr.branch)) return
|
|
189
|
-
deleted.add(pr.branch)
|
|
190
|
-
await ctx.github.deleteBranch(ctx.subject.slug, pr.branch)
|
|
191
|
-
}
|
|
127
|
+
export const nodeAt = (what) => (what.at ? { node: what.at } : {})
|
|
192
128
|
|
|
193
129
|
// ---- Nodes
|
|
194
130
|
|
|
@@ -331,14 +267,11 @@ const workerRunning = declare(
|
|
|
331
267
|
return { ready: true, output: { exit: 'failed', ...lostLeaseOf(ctx) } }
|
|
332
268
|
}
|
|
333
269
|
const caps = config.phases[/** @type {'task'} */ (w.phase)] ?? config.phases.task
|
|
334
|
-
if (
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
w.killedReason = reason
|
|
340
|
-
ctx.log('warn', `${reason} (pid ${w.pid})`, { repo: w.repo, issue: w.issue })
|
|
341
|
-
}
|
|
270
|
+
if (
|
|
271
|
+
!reaped?.has(w.pid) &&
|
|
272
|
+
stopOverdue({ procs, store, log: ctx.log }, w, caps.wallClockMinutes, ctx.tick.now) !== 'gone'
|
|
273
|
+
)
|
|
274
|
+
return { ready: false }
|
|
342
275
|
if (!snap) return { ready: false } // a dead worker waits for a readable snapshot
|
|
343
276
|
if (issue?.state !== 'OPEN') {
|
|
344
277
|
ctx.log('info', 'worker gone; issue is closed or no longer queued', { repo: w.repo, issue: w.issue })
|
|
@@ -381,219 +314,39 @@ export const lostLeaseOf = (ctx) => ({
|
|
|
381
314
|
outcome: 'lost',
|
|
382
315
|
})
|
|
383
316
|
|
|
384
|
-
const
|
|
385
|
-
defineWait({
|
|
386
|
-
name: 'ci-running',
|
|
387
|
-
input: PrIn,
|
|
388
|
-
output: PrOut,
|
|
389
|
-
/** @param {z.infer<typeof PrIn>} input @param {TaskCtx} ctx */
|
|
390
|
-
poll: async (input, ctx) => {
|
|
391
|
-
const issue = issueOf(ctx)
|
|
392
|
-
const pr = prOf(ctx, input)
|
|
393
|
-
if (!pr) return { ready: false }
|
|
394
|
-
if (pr.state === 'MERGED') return { ready: true, output: { to: 'merged', pr: pr.number } }
|
|
395
|
-
if (pr.state !== 'OPEN' || issue.state !== 'OPEN') return { ready: false }
|
|
396
|
-
const m = markerOf(issue)
|
|
397
|
-
const red = redCi(pr, m)
|
|
398
|
-
if (red) return { ready: true, output: red }
|
|
399
|
-
if (pr.inMergeQueue) return { ready: true, output: { to: 'queued', pr: pr.number, branch: pr.branch } }
|
|
400
|
-
if (mergeDue(ctx, pr, m)) return { ready: true, output: { to: 'merge', pr: pr.number, branch: pr.branch } }
|
|
401
|
-
const late = stall(ctx, pr, m)
|
|
402
|
-
return late ? { ready: true, output: late } : { ready: false }
|
|
403
|
-
},
|
|
404
|
-
}),
|
|
405
|
-
{
|
|
406
|
-
queue: null,
|
|
407
|
-
running: false,
|
|
408
|
-
/** @param {z.infer<typeof PrIn> & { pr: number, branch: string, log?: string }} input @param {TaskCtx} ctx */
|
|
409
|
-
enter: async (input, ctx) => {
|
|
410
|
-
const m = markerOf(issueOf(ctx))
|
|
411
|
-
const at = iso(ctx.tick.now)
|
|
412
|
-
await land(ctx, 'ci-running', {
|
|
413
|
-
...withHistory(m, { at, attempt: m.attempt, outcome: 'submitted', pr: input.pr, log: input.log }),
|
|
414
|
-
pr: input.pr,
|
|
415
|
-
branch: input.branch,
|
|
416
|
-
submittedAt: at,
|
|
417
|
-
queued: false,
|
|
418
|
-
lastFailure: null,
|
|
419
|
-
input: { pr: input.pr, branch: input.branch },
|
|
420
|
-
})
|
|
421
|
-
ctx.log('info', `submitted PR #${input.pr}; watching CI`, { repo: ctx.subject.slug, issue: ctx.subject.number })
|
|
422
|
-
await linkIssue(ctx, ctx.subject.slug, prOf(ctx, input), ctx.subject.number)
|
|
423
|
-
},
|
|
424
|
-
},
|
|
425
|
-
)
|
|
426
|
-
|
|
427
|
-
const MergeIn = z.object({ pr: z.number().int(), branch: z.string() })
|
|
317
|
+
const SubmitIn = z.object({ pr: z.number().int(), branch: z.string(), log: z.string().optional() })
|
|
428
318
|
|
|
429
|
-
const
|
|
319
|
+
const submit = declare(
|
|
430
320
|
defineNode({
|
|
431
|
-
name: '
|
|
432
|
-
input:
|
|
433
|
-
output:
|
|
434
|
-
pr: z.number().int(),
|
|
435
|
-
branch: z.string(),
|
|
436
|
-
refused: z.string().nullable(),
|
|
437
|
-
updated: z.string().nullable().optional(),
|
|
438
|
-
}),
|
|
321
|
+
name: 'submit',
|
|
322
|
+
input: SubmitIn,
|
|
323
|
+
output: Empty,
|
|
439
324
|
/**
|
|
440
|
-
*
|
|
441
|
-
*
|
|
442
|
-
*
|
|
443
|
-
* Behind its base (read live, so a merge earlier in this tick counts): update the branch instead. Its green
|
|
444
|
-
* was earned against an older base, and two PRs green on the same base can be red together (no merge queue,
|
|
445
|
-
* and the base does not require up-to-date branches). `merging` holds on the old head and merges once the
|
|
446
|
-
* updated head is green; red goes back to a worker like any red CI.
|
|
447
|
-
* @param {z.infer<typeof MergeIn>} input @param {TaskCtx} ctx
|
|
325
|
+
* The worker published its PR: the task graph ends. The issue leaves every queue and the PR graph takes the PR
|
|
326
|
+
* from the next tick (graphs/pr.mjs). A PR found only by its branch gets `Closes #n`, so it can be paired.
|
|
327
|
+
* @param {z.infer<typeof SubmitIn>} input @param {TaskCtx} ctx
|
|
448
328
|
*/
|
|
449
329
|
run: async (input, ctx) => {
|
|
450
|
-
const
|
|
451
|
-
try {
|
|
452
|
-
const { behind, base } = await ctx.github.behindBase(slug, input.pr)
|
|
453
|
-
if (behind > 0) {
|
|
454
|
-
await ctx.github.updatePull(slug, input.pr)
|
|
455
|
-
const commits = `${behind} commit${behind === 1 ? '' : 's'}`
|
|
456
|
-
const updated = `PR #${input.pr} was ${commits} behind ${base}; updated it, merging once CI is green on the new head`
|
|
457
|
-
return { ...input, refused: null, updated }
|
|
458
|
-
}
|
|
459
|
-
await ctx.github.mergePull(slug, input.pr)
|
|
460
|
-
return { ...input, refused: null, updated: null }
|
|
461
|
-
} catch (err) {
|
|
462
|
-
return { ...input, refused: `merge refused: ${/** @type {Error} */ (err)?.message ?? err}`, updated: null }
|
|
463
|
-
}
|
|
464
|
-
},
|
|
465
|
-
}),
|
|
466
|
-
{ queue: null, running: false },
|
|
467
|
-
)
|
|
468
|
-
|
|
469
|
-
const MergingIn = PrIn.extend({ refused: z.string().nullable().optional() })
|
|
470
|
-
|
|
471
|
-
const merging = declare(
|
|
472
|
-
defineWait({
|
|
473
|
-
name: 'merging',
|
|
474
|
-
input: MergingIn,
|
|
475
|
-
output: PrOut,
|
|
476
|
-
/**
|
|
477
|
-
* Merged → close out. Red → requeue. Due again (a refusal on an older head, or one past the retry window) →
|
|
478
|
-
* merge. Otherwise hold until `mergeQueueMinutes` after submission.
|
|
479
|
-
* @param {z.infer<typeof MergingIn>} input @param {TaskCtx} ctx
|
|
480
|
-
*/
|
|
481
|
-
poll: async (input, ctx) => {
|
|
482
|
-
const issue = issueOf(ctx)
|
|
483
|
-
const pr = prOf(ctx, input)
|
|
484
|
-
if (!pr) return { ready: false }
|
|
485
|
-
if (pr.state === 'MERGED') return { ready: true, output: { to: 'merged', pr: pr.number } }
|
|
486
|
-
if (pr.state !== 'OPEN' || issue.state !== 'OPEN') return { ready: false }
|
|
487
|
-
const m = markerOf(issue)
|
|
488
|
-
const red = redCi(pr, m)
|
|
489
|
-
if (red) return { ready: true, output: red }
|
|
490
|
-
if (m.mergeRefusal && mergeDue(ctx, pr, m))
|
|
491
|
-
return { ready: true, output: { to: 'merge', pr: pr.number, branch: pr.branch } }
|
|
492
|
-
const late = stall(ctx, pr, m)
|
|
493
|
-
return late ? { ready: true, output: late } : { ready: false }
|
|
494
|
-
},
|
|
495
|
-
}),
|
|
496
|
-
{
|
|
497
|
-
queue: null,
|
|
498
|
-
running: false,
|
|
499
|
-
/** @param {z.infer<typeof MergeIn> & { refused: string | null, updated?: string | null }} input @param {TaskCtx} ctx */
|
|
500
|
-
enter: async (input, ctx) => {
|
|
501
|
-
const { slug } = ctx.subject
|
|
502
|
-
const issue = issueOf(ctx)
|
|
503
|
-
const m = markerOf(issue)
|
|
504
|
-
const pr = prOf(ctx, input)
|
|
330
|
+
const m = markerOf(issueOf(ctx))
|
|
505
331
|
const at = iso(ctx.tick.now)
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
...withHistory(m, {
|
|
512
|
-
at,
|
|
513
|
-
attempt: m.attempt,
|
|
514
|
-
outcome: refused ? 'merge-refused' : updated ? 'branch-updated' : 'merge-armed',
|
|
332
|
+
await land(
|
|
333
|
+
ctx,
|
|
334
|
+
'submit',
|
|
335
|
+
{
|
|
336
|
+
...withHistory(m, { at, attempt: m.attempt, outcome: 'submitted', pr: input.pr, log: input.log }),
|
|
515
337
|
pr: input.pr,
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
)
|
|
528
|
-
|
|
529
|
-
const mergeQueue = declare(
|
|
530
|
-
defineWait({
|
|
531
|
-
name: 'merge-queue',
|
|
532
|
-
input: PrIn,
|
|
533
|
-
output: PrOut,
|
|
534
|
-
/** @param {z.infer<typeof PrIn>} input @param {TaskCtx} ctx */
|
|
535
|
-
poll: async (input, ctx) => {
|
|
536
|
-
const issue = issueOf(ctx)
|
|
537
|
-
const pr = prOf(ctx, input)
|
|
538
|
-
if (!pr) return { ready: false }
|
|
539
|
-
if (pr.state === 'MERGED') return { ready: true, output: { to: 'merged', pr: pr.number } }
|
|
540
|
-
if (pr.state !== 'OPEN' || issue.state !== 'OPEN') return { ready: false }
|
|
541
|
-
const m = markerOf(issue)
|
|
542
|
-
const red = redCi(pr, m)
|
|
543
|
-
if (red) return { ready: true, output: red }
|
|
544
|
-
const settled = pr.checks !== 'PENDING' && pr.checks !== 'EXPECTED'
|
|
545
|
-
if (!pr.inMergeQueue && !pr.autoMerge && settled)
|
|
546
|
-
return {
|
|
547
|
-
ready: true,
|
|
548
|
-
output: {
|
|
549
|
-
to: 'requeue',
|
|
550
|
-
reason: `PR #${pr.number} left the merge queue without merging`,
|
|
551
|
-
outcome: 'dequeued',
|
|
552
|
-
pr: pr.number,
|
|
553
|
-
},
|
|
554
|
-
}
|
|
555
|
-
const late = stall(ctx, pr, m)
|
|
556
|
-
return late ? { ready: true, output: late } : { ready: false }
|
|
557
|
-
},
|
|
558
|
-
}),
|
|
559
|
-
{
|
|
560
|
-
queue: null,
|
|
561
|
-
running: false,
|
|
562
|
-
pollOnEntry: true,
|
|
563
|
-
/** @param {{ pr: number, branch: string }} input @param {TaskCtx} ctx */
|
|
564
|
-
enter: async (input, ctx) => {
|
|
565
|
-
await land(ctx, 'merge-queue', {
|
|
566
|
-
...markerOf(issueOf(ctx)),
|
|
567
|
-
queued: true,
|
|
568
|
-
input: { pr: input.pr, branch: input.branch },
|
|
569
|
-
})
|
|
570
|
-
},
|
|
571
|
-
},
|
|
572
|
-
)
|
|
573
|
-
|
|
574
|
-
const closeOut = declare(
|
|
575
|
-
defineNode({
|
|
576
|
-
name: 'close-out',
|
|
577
|
-
input: z.object({ pr: z.number().int() }),
|
|
578
|
-
output: Empty,
|
|
579
|
-
/** Merged: done, out of every queue, issue closed, branch deleted. @param {{ pr: number }} input @param {TaskCtx} ctx */
|
|
580
|
-
run: async (input, ctx) => {
|
|
581
|
-
const { slug } = ctx.subject
|
|
582
|
-
const issue = issueOf(ctx)
|
|
583
|
-
const pr = prOf(ctx, { pr: input.pr, branch: null })
|
|
584
|
-
if (issue.state === 'OPEN') {
|
|
585
|
-
const m = markerOf(issue)
|
|
586
|
-
await land(
|
|
587
|
-
ctx,
|
|
588
|
-
'close-out',
|
|
589
|
-
withHistory(m, { at: iso(ctx.tick.now), attempt: m.attempt, outcome: 'merged', pr: input.pr }),
|
|
590
|
-
{ status: 'done' },
|
|
591
|
-
)
|
|
592
|
-
await ctx.github.close(slug, issue.number, `Merged in #${input.pr}. Closed by cambium.`)
|
|
593
|
-
issue.state = 'CLOSED'
|
|
594
|
-
ctx.log('info', `PR #${input.pr} merged; closed`, { repo: slug, issue: issue.number })
|
|
595
|
-
}
|
|
596
|
-
if (pr) await deleteMergedBranch(ctx, pr)
|
|
338
|
+
branch: input.branch,
|
|
339
|
+
submittedAt: at,
|
|
340
|
+
queued: false,
|
|
341
|
+
lastFailure: null,
|
|
342
|
+
input: null,
|
|
343
|
+
},
|
|
344
|
+
{ status: 'done' },
|
|
345
|
+
)
|
|
346
|
+
ctx.log('info', `submitted PR #${input.pr}; watching CI`, { repo: ctx.subject.slug, issue: ctx.subject.number })
|
|
347
|
+
const snap = ctx.subject.snap
|
|
348
|
+
const pr = snap?.prs.find((p) => p.number === input.pr) ?? null
|
|
349
|
+
await linkIssue(ctx, ctx.subject.slug, pr, ctx.subject.number)
|
|
597
350
|
return {}
|
|
598
351
|
},
|
|
599
352
|
}),
|
|
@@ -678,39 +431,33 @@ const toHuman = declare(
|
|
|
678
431
|
{ queue: Q.human, running: false },
|
|
679
432
|
)
|
|
680
433
|
|
|
681
|
-
/**
|
|
434
|
+
/**
|
|
435
|
+
* The Task phase. Bump `version` when a node is removed or renamed (see marker.mjs `graphVersionDecision`).
|
|
436
|
+
* v2: the PR nodes (`ci-running`, `merge`, `merging`, `merge-queue`, `close-out`) moved to the PR graph; a
|
|
437
|
+
* marker resting at one of them is the PR graph's (graph-engine.mjs `locate`), not a restart.
|
|
438
|
+
*/
|
|
682
439
|
export const task = defineGraph({
|
|
683
440
|
name: 'task',
|
|
684
|
-
version:
|
|
441
|
+
version: 2,
|
|
685
442
|
entry: { queue: Q.task, node: 'start-worker', input: () => ({}) },
|
|
686
|
-
nodes: [startWorker, workerRunning,
|
|
443
|
+
nodes: [startWorker, workerRunning, submit, requeue, toHuman],
|
|
687
444
|
edges: [
|
|
688
445
|
['start-worker', 'worker-running', started],
|
|
689
446
|
['start-worker', 'to-human', budgetSpent],
|
|
690
447
|
['start-worker', 'requeue'],
|
|
691
448
|
['worker-running', END, issueGone],
|
|
692
|
-
['worker-running', '
|
|
449
|
+
['worker-running', 'submit', submitted],
|
|
693
450
|
['worker-running', 'requeue'],
|
|
694
|
-
['
|
|
695
|
-
['ci-running', 'merge-queue', inMergeQueue],
|
|
696
|
-
['ci-running', 'merge', green],
|
|
697
|
-
['ci-running', 'to-human', stalled],
|
|
698
|
-
['ci-running', 'requeue'],
|
|
699
|
-
['merge', 'merging'],
|
|
700
|
-
['merging', 'close-out', merged],
|
|
701
|
-
['merging', 'merge', green],
|
|
702
|
-
['merging', 'to-human', stalled],
|
|
703
|
-
['merging', 'requeue'],
|
|
704
|
-
['merge-queue', 'close-out', merged],
|
|
705
|
-
['merge-queue', 'to-human', stalled],
|
|
706
|
-
['merge-queue', 'requeue'],
|
|
451
|
+
['submit', END],
|
|
707
452
|
['requeue', 'start-worker', attemptsLeft],
|
|
708
453
|
['requeue', 'to-human'],
|
|
709
|
-
['close-out', END],
|
|
710
454
|
['to-human', END],
|
|
711
455
|
],
|
|
712
456
|
})
|
|
713
457
|
|
|
458
|
+
/** Task-graph nodes of v1 (and stage-era `submitted`/`done`) that the PR graph now owns: an issue resting at one has a PR. */
|
|
459
|
+
export const HANDED_OFF = new Set(['submit', 'ci-running', 'merge', 'merging', 'merge-queue', 'close-out'])
|
|
460
|
+
|
|
714
461
|
/** @param {string} name @returns {TaskNode} */
|
|
715
462
|
export function nodeOf(name) {
|
|
716
463
|
const node = task.nodes.find((n) => n.name === name)
|
|
@@ -734,6 +481,5 @@ export const TASK_QUEUES = {
|
|
|
734
481
|
/** The input a resting node resumes with, rebuilt from the marker. @param {string} node @param {Marker} m */
|
|
735
482
|
export function inputFor(node, m) {
|
|
736
483
|
if (node === 'worker-running') return { branch: m.branch, attempt: m.attempt }
|
|
737
|
-
if (node === 'ci-running' || node === 'merging' || node === 'merge-queue') return { pr: m.pr, branch: m.branch }
|
|
738
484
|
return {}
|
|
739
485
|
}
|
package/src/marker.mjs
CHANGED
|
@@ -42,12 +42,14 @@
|
|
|
42
42
|
* failedSha?: string | null,
|
|
43
43
|
* lastFailure?: string | null,
|
|
44
44
|
* mergeRefusal?: { sha: string | null, at: string, reason: string } | null,
|
|
45
|
+
* reworks?: number,
|
|
45
46
|
* triage?: { title?: string, acceptance?: string[] } | null,
|
|
46
47
|
* defers?: number,
|
|
47
48
|
* deferUntil?: string | null,
|
|
48
49
|
* deferReason?: string | null,
|
|
49
50
|
* }} Marker
|
|
50
|
-
* `mergeRefusal`: the last `graft pr merge` cambium was refused (
|
|
51
|
+
* `mergeRefusal`: the last `graft pr merge` cambium was refused (PR graph `merge`), and on which head.
|
|
52
|
+
* `reworks`: how many times the PR graph sent this issue's PR back to a worker.
|
|
51
53
|
* `defers`, `deferUntil`, `deferReason`: the dispatch graph's `deferred` node: how many times triage deferred, until
|
|
52
54
|
* when, and what the next triage should check.
|
|
53
55
|
* `triage`: what the dispatch graph's triage added when it handed the issue to `q:task`; the task prompt shows it.
|
package/src/phases/task.mjs
CHANGED
|
@@ -31,6 +31,10 @@ ${
|
|
|
31
31
|
pr
|
|
32
32
|
? `\nA PR already exists: #${pr.number} (${pr.url}) on \`${pr.branch}\`. Start from it: \`graft advance ${pr.branch}\`, then fix what failed.\n`
|
|
33
33
|
: ''
|
|
34
|
+
}${
|
|
35
|
+
marker.reworks
|
|
36
|
+
? `\nThis is rework ${marker.reworks} of PR #${marker.pr ?? pr?.number}: cambium's PR graph sent it back (the reason is below). Push the fix to the same branch; the PR graph merges it once it is green.\n`
|
|
37
|
+
: ''
|
|
34
38
|
}${marker.lastFailure ? `\nWhy the last attempt failed:\n\`\`\`\n${marker.lastFailure}\n\`\`\`\n` : ''}${history ? `\nHistory of this issue:\n${history}\n` : ''}
|
|
35
39
|
--- Issue #${issue.number}: ${issue.title} (${issue.url})
|
|
36
40
|
${issue.body}
|
package/src/tick.mjs
CHANGED
|
@@ -17,6 +17,7 @@ import { releaseDue, releaseNotBuilt } from './phases/release.mjs'
|
|
|
17
17
|
import { releaseGraphs } from './graphs/release.mjs'
|
|
18
18
|
import { DISPATCH_NOT_BUILT, dispatchNotBuilt } from './phases/dispatch.mjs'
|
|
19
19
|
import { isRepair } from './base-health.mjs'
|
|
20
|
+
import { stopOverdue } from './workers.mjs'
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
23
|
* @typedef {import('./github.mjs').Issue} Issue
|
|
@@ -235,14 +236,7 @@ async function reconcile(deps, ctx) {
|
|
|
235
236
|
store.workers(),
|
|
236
237
|
async (w) => {
|
|
237
238
|
const caps = config.phases[/** @type {'task'} */ (w.phase)] ?? config.phases.task
|
|
238
|
-
if (procs.
|
|
239
|
-
if (!w.killedReason && ctx.now - w.startedAt <= caps.wallClockMinutes * MIN) return
|
|
240
|
-
const reason = w.killedReason ?? `killed: over the ${caps.wallClockMinutes} min wall-clock cap`
|
|
241
|
-
procs.kill(w.pid)
|
|
242
|
-
store.markKilled(w.repo, w.issue, reason)
|
|
243
|
-
w.killedReason = reason
|
|
244
|
-
deps.log('warn', `${reason} (pid ${w.pid})`, { repo: w.repo, issue: w.issue })
|
|
245
|
-
}
|
|
239
|
+
if (stopOverdue({ procs, store, log: deps.log }, w, caps.wallClockMinutes, ctx.now) !== 'gone') return
|
|
246
240
|
const snap = ctx.snaps.get(w.repo)
|
|
247
241
|
if (snap) await finish(deps, ctx, w, snap)
|
|
248
242
|
},
|