@oakgreencc/cambium 0.0.26 → 0.0.28
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 +80 -59
- 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/pr.mjs +420 -0
- package/src/graphs/task.mjs +61 -313
- package/src/marker.mjs +3 -1
- package/src/phases/dispatch.mjs +1 -0
- package/src/phases/task.mjs +4 -0
package/src/graphs/task.mjs
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
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, MIN, 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'
|
|
@@ -51,17 +52,17 @@ import { baseRed, isRepair } from '../base-health.mjs'
|
|
|
51
52
|
* @template {import('../graph/define.mjs').GraphNode} N @param {N} node @param {Declared} decl
|
|
52
53
|
* @returns {N & Declared}
|
|
53
54
|
*/
|
|
54
|
-
const declare = (node, decl) => Object.freeze({ ...node, ...decl })
|
|
55
|
+
export const declare = (node, decl) => Object.freeze({ ...node, ...decl })
|
|
55
56
|
|
|
56
57
|
/** @param {TaskCtx} ctx */
|
|
57
|
-
function issueOf(ctx) {
|
|
58
|
+
export function issueOf(ctx) {
|
|
58
59
|
const { issue } = ctx.subject
|
|
59
60
|
if (!issue) throw new Error(`${ctx.subject.slug}#${ctx.subject.number} is not in the snapshot`)
|
|
60
61
|
return issue
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
/** @param {TaskCtx} ctx */
|
|
64
|
-
function snapOf(ctx) {
|
|
65
|
+
export function snapOf(ctx) {
|
|
65
66
|
const { snap } = ctx.subject
|
|
66
67
|
if (!snap) throw new Error(`${ctx.subject.slug} has no snapshot this tick`)
|
|
67
68
|
return snap
|
|
@@ -71,20 +72,21 @@ function snapOf(ctx) {
|
|
|
71
72
|
const graphMarker = (m) => /** @type {Marker} */ (Object.fromEntries(Object.entries(m).filter(([k]) => k !== 'stage')))
|
|
72
73
|
|
|
73
74
|
/**
|
|
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.
|
|
75
|
+
* Put the issue at `node` of graph `g`: the marker first, then the labels `node` declares, via `move()`. A
|
|
76
|
+
* half-applied transition is re-done (and over-counted) on the next tick, never under-counted.
|
|
76
77
|
*
|
|
77
|
-
* @param {TaskCtx} ctx @param {string} node @param {Marker} marker
|
|
78
|
+
* @param {import('../graph/define.mjs').Graph} g @param {TaskCtx} ctx @param {string} node @param {Marker} marker
|
|
78
79
|
* @param {{ extra?: string[], status?: Marker['status'] }} [opts]
|
|
79
80
|
*/
|
|
80
|
-
async function
|
|
81
|
+
export async function landAt(g, ctx, node, marker, { extra = [], status = 'waiting' } = {}) {
|
|
81
82
|
const { slug } = ctx.subject
|
|
82
83
|
const issue = issueOf(ctx)
|
|
83
|
-
const decl =
|
|
84
|
+
const decl = /** @type {Declared | undefined} */ (g.nodes.find((n) => n.name === node))
|
|
85
|
+
if (!decl) throw new Error(`${g.name} graph has no node "${node}"`)
|
|
84
86
|
await setMarker(ctx, slug, issue, {
|
|
85
87
|
...graphMarker(marker),
|
|
86
|
-
graph:
|
|
87
|
-
graphVersion:
|
|
88
|
+
graph: g.name,
|
|
89
|
+
graphVersion: g.version,
|
|
88
90
|
node,
|
|
89
91
|
status,
|
|
90
92
|
enteredAt: iso(ctx.tick.now),
|
|
@@ -93,9 +95,12 @@ async function land(ctx, node, marker, { extra = [], status = 'waiting' } = {})
|
|
|
93
95
|
ctx.tick.touched.add(key(slug, issue.number))
|
|
94
96
|
}
|
|
95
97
|
|
|
98
|
+
/** {@link landAt} in the task graph. @param {TaskCtx} ctx @param {string} node @param {Marker} marker @param {{ extra?: string[], status?: Marker['status'] }} [opts] */
|
|
99
|
+
const land = (ctx, node, marker, opts) => landAt(task, ctx, node, marker, opts)
|
|
100
|
+
|
|
96
101
|
// ---- Schemas
|
|
97
102
|
|
|
98
|
-
const failure = {
|
|
103
|
+
export const failure = {
|
|
99
104
|
reason: z.string(),
|
|
100
105
|
outcome: z.string(),
|
|
101
106
|
pr: z.number().int().optional(),
|
|
@@ -107,88 +112,18 @@ const failure = {
|
|
|
107
112
|
const Failure = z.object(failure)
|
|
108
113
|
const Empty = z.object({})
|
|
109
114
|
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
115
|
// ---- Edge predicates (their names label the Mermaid edges)
|
|
120
116
|
|
|
121
117
|
const started = (/** @type {{ to: string }} */ o) => o.to === 'running'
|
|
122
118
|
const budgetSpent = (/** @type {{ to: string }} */ o) => o.to === 'human'
|
|
123
119
|
const issueGone = (/** @type {{ exit: string }} */ o) => o.exit === 'gone'
|
|
124
120
|
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
121
|
const attemptsLeft = (/** @type {{ to: string }} */ o) => o.to === 'start'
|
|
130
122
|
|
|
131
123
|
// ---- Shared checks
|
|
132
124
|
|
|
133
125
|
/** 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
|
-
}
|
|
126
|
+
export const nodeAt = (what) => (what.at ? { node: what.at } : {})
|
|
192
127
|
|
|
193
128
|
// ---- Nodes
|
|
194
129
|
|
|
@@ -381,219 +316,39 @@ export const lostLeaseOf = (ctx) => ({
|
|
|
381
316
|
outcome: 'lost',
|
|
382
317
|
})
|
|
383
318
|
|
|
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() })
|
|
319
|
+
const SubmitIn = z.object({ pr: z.number().int(), branch: z.string(), log: z.string().optional() })
|
|
428
320
|
|
|
429
|
-
const
|
|
321
|
+
const submit = declare(
|
|
430
322
|
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
|
-
}),
|
|
323
|
+
name: 'submit',
|
|
324
|
+
input: SubmitIn,
|
|
325
|
+
output: Empty,
|
|
439
326
|
/**
|
|
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
|
|
327
|
+
* The worker published its PR: the task graph ends. The issue leaves every queue and the PR graph takes the PR
|
|
328
|
+
* from the next tick (graphs/pr.mjs). A PR found only by its branch gets `Closes #n`, so it can be paired.
|
|
329
|
+
* @param {z.infer<typeof SubmitIn>} input @param {TaskCtx} ctx
|
|
448
330
|
*/
|
|
449
331
|
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)
|
|
332
|
+
const m = markerOf(issueOf(ctx))
|
|
505
333
|
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',
|
|
334
|
+
await land(
|
|
335
|
+
ctx,
|
|
336
|
+
'submit',
|
|
337
|
+
{
|
|
338
|
+
...withHistory(m, { at, attempt: m.attempt, outcome: 'submitted', pr: input.pr, log: input.log }),
|
|
515
339
|
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)
|
|
340
|
+
branch: input.branch,
|
|
341
|
+
submittedAt: at,
|
|
342
|
+
queued: false,
|
|
343
|
+
lastFailure: null,
|
|
344
|
+
input: null,
|
|
345
|
+
},
|
|
346
|
+
{ status: 'done' },
|
|
347
|
+
)
|
|
348
|
+
ctx.log('info', `submitted PR #${input.pr}; watching CI`, { repo: ctx.subject.slug, issue: ctx.subject.number })
|
|
349
|
+
const snap = ctx.subject.snap
|
|
350
|
+
const pr = snap?.prs.find((p) => p.number === input.pr) ?? null
|
|
351
|
+
await linkIssue(ctx, ctx.subject.slug, pr, ctx.subject.number)
|
|
597
352
|
return {}
|
|
598
353
|
},
|
|
599
354
|
}),
|
|
@@ -678,39 +433,33 @@ const toHuman = declare(
|
|
|
678
433
|
{ queue: Q.human, running: false },
|
|
679
434
|
)
|
|
680
435
|
|
|
681
|
-
/**
|
|
436
|
+
/**
|
|
437
|
+
* The Task phase. Bump `version` when a node is removed or renamed (see marker.mjs `graphVersionDecision`).
|
|
438
|
+
* v2: the PR nodes (`ci-running`, `merge`, `merging`, `merge-queue`, `close-out`) moved to the PR graph; a
|
|
439
|
+
* marker resting at one of them is the PR graph's (graph-engine.mjs `locate`), not a restart.
|
|
440
|
+
*/
|
|
682
441
|
export const task = defineGraph({
|
|
683
442
|
name: 'task',
|
|
684
|
-
version:
|
|
443
|
+
version: 2,
|
|
685
444
|
entry: { queue: Q.task, node: 'start-worker', input: () => ({}) },
|
|
686
|
-
nodes: [startWorker, workerRunning,
|
|
445
|
+
nodes: [startWorker, workerRunning, submit, requeue, toHuman],
|
|
687
446
|
edges: [
|
|
688
447
|
['start-worker', 'worker-running', started],
|
|
689
448
|
['start-worker', 'to-human', budgetSpent],
|
|
690
449
|
['start-worker', 'requeue'],
|
|
691
450
|
['worker-running', END, issueGone],
|
|
692
|
-
['worker-running', '
|
|
451
|
+
['worker-running', 'submit', submitted],
|
|
693
452
|
['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'],
|
|
453
|
+
['submit', END],
|
|
707
454
|
['requeue', 'start-worker', attemptsLeft],
|
|
708
455
|
['requeue', 'to-human'],
|
|
709
|
-
['close-out', END],
|
|
710
456
|
['to-human', END],
|
|
711
457
|
],
|
|
712
458
|
})
|
|
713
459
|
|
|
460
|
+
/** Task-graph nodes of v1 (and stage-era `submitted`/`done`) that the PR graph now owns: an issue resting at one has a PR. */
|
|
461
|
+
export const HANDED_OFF = new Set(['submit', 'ci-running', 'merge', 'merging', 'merge-queue', 'close-out'])
|
|
462
|
+
|
|
714
463
|
/** @param {string} name @returns {TaskNode} */
|
|
715
464
|
export function nodeOf(name) {
|
|
716
465
|
const node = task.nodes.find((n) => n.name === name)
|
|
@@ -734,6 +483,5 @@ export const TASK_QUEUES = {
|
|
|
734
483
|
/** The input a resting node resumes with, rebuilt from the marker. @param {string} node @param {Marker} m */
|
|
735
484
|
export function inputFor(node, m) {
|
|
736
485
|
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
486
|
return {}
|
|
739
487
|
}
|
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/dispatch.mjs
CHANGED
|
@@ -132,6 +132,7 @@ Verdicts:
|
|
|
132
132
|
|
|
133
133
|
How to look:
|
|
134
134
|
- Read the code here (Read, Glob, Grep) to judge what the change touches and whether it already exists.
|
|
135
|
+
- First, has it already been done? Its comments (\`graft api GET /repos/${repo}/issues/${issue.number}/comments\`) and its timeline (\`graft api GET '/repos/${repo}/issues/${issue.number}/timeline?per_page=100'\`: cross-referenced PRs, and whether they merged) often say so. A merged PR whose change is on ${baseBranch}, and nothing left of the issue, is obsolete; a PR still open or in CI is defer.
|
|
135
136
|
- Read an issue with \`graft issue read <n>\`; search with \`graft api GET '/search/issues?q=repo:${repo}+is:issue+<words>'\`. Never \`gh\`.
|
|
136
137
|
- Be quick: a verdict, not an investigation. When in doubt whether the work is clear enough to do, choose needs-human; when in doubt whether something is true, check it, or defer until it can be checked.
|
|
137
138
|
|
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}
|