@oakgreencc/cambium 0.0.7 → 0.0.8
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 +66 -10
- package/package.json +2 -2
- package/src/cli.mjs +18 -3
- package/src/config.mjs +4 -0
- package/src/graph-engine.mjs +289 -0
- package/src/graphs/task.mjs +587 -0
- package/src/issue-state.mjs +46 -0
- package/src/tick.mjs +97 -76
package/src/tick.mjs
CHANGED
|
@@ -6,8 +6,10 @@
|
|
|
6
6
|
* step that throws or times out is recorded and the tick moves on. Nothing
|
|
7
7
|
* here waits on a worker.
|
|
8
8
|
*/
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
9
|
+
import { byPriorityThenAge, move, Q, queueOf, RUNNING, WHY } from './labels.mjs'
|
|
10
|
+
import { withHistory } from './marker.mjs'
|
|
11
|
+
import { failureSummary, iso, key, markerOf, MIN, setLabels, setMarker } from './issue-state.mjs'
|
|
12
|
+
import { GRAPH_ENGINE } from './graph-engine.mjs'
|
|
11
13
|
import { prFor, taskExit, taskPrompt } from './phases/task.mjs'
|
|
12
14
|
import { branchName } from './convention.mjs'
|
|
13
15
|
import { PREPARE_FAILED_LINE } from './prepare.mjs'
|
|
@@ -42,9 +44,12 @@ import { DISPATCH_NOT_BUILT, dispatchNotBuilt } from './phases/dispatch.mjs'
|
|
|
42
44
|
* }} Ctx
|
|
43
45
|
*/
|
|
44
46
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
/**
|
|
48
|
+
* @typedef {(deps: Deps, ctx: Ctx) => Promise<void>} StepFn
|
|
49
|
+
* @typedef {{ reconcile: StepFn, watch: StepFn, sweep: StepFn, scheduleTasks: StepFn }} Engine
|
|
50
|
+
* The Task-phase steps. `config.engine` picks the legacy stage tick (this file) or the `task` graph
|
|
51
|
+
* (graph-engine.mjs); Read, dispatch, Release and Heartbeat are shared.
|
|
52
|
+
*/
|
|
48
53
|
|
|
49
54
|
/** @param {Deps} deps @returns {Ctx} */
|
|
50
55
|
export function newCtx(deps) {
|
|
@@ -65,17 +70,23 @@ export function newCtx(deps) {
|
|
|
65
70
|
export async function tick(deps) {
|
|
66
71
|
const ctx = newCtx(deps)
|
|
67
72
|
const paused = ctx.mode === 'paused'
|
|
68
|
-
|
|
73
|
+
const engine = engineOf(deps)
|
|
74
|
+
/** @type {StepFn} */
|
|
75
|
+
const schedule = async (d, c) => {
|
|
76
|
+
await engine.scheduleTasks(d, c)
|
|
77
|
+
await scheduleDispatch(d, c)
|
|
78
|
+
}
|
|
79
|
+
/** @type {[string, StepFn][]} */
|
|
69
80
|
const steps = [
|
|
70
81
|
['read', read],
|
|
71
82
|
...(paused
|
|
72
83
|
? []
|
|
73
|
-
: /** @type {[string,
|
|
74
|
-
['reconcile', reconcile],
|
|
75
|
-
['watch', watch],
|
|
76
|
-
['sweep', sweep],
|
|
84
|
+
: /** @type {[string, StepFn][]} */ ([
|
|
85
|
+
['reconcile', engine.reconcile],
|
|
86
|
+
['watch', engine.watch],
|
|
87
|
+
['sweep', engine.sweep],
|
|
77
88
|
])),
|
|
78
|
-
...(ctx.mode === 'running' ? /** @type {[string,
|
|
89
|
+
...(ctx.mode === 'running' ? /** @type {[string, StepFn][]} */ ([['schedule', schedule]]) : []),
|
|
79
90
|
...(paused ? [] : /** @type {[string, any][]} */ ([['release', release]])),
|
|
80
91
|
]
|
|
81
92
|
for (const [name, fn] of steps) await runStep(deps, ctx, name, () => fn(deps, ctx))
|
|
@@ -84,6 +95,12 @@ export async function tick(deps) {
|
|
|
84
95
|
return state
|
|
85
96
|
}
|
|
86
97
|
|
|
98
|
+
/** @type {Engine} */
|
|
99
|
+
const LEGACY = { reconcile, watch, sweep, scheduleTasks }
|
|
100
|
+
|
|
101
|
+
/** @param {Deps} deps @returns {Engine} */
|
|
102
|
+
export const engineOf = (deps) => (deps.config.engine === 'graph' ? GRAPH_ENGINE : LEGACY)
|
|
103
|
+
|
|
87
104
|
/**
|
|
88
105
|
* @param {Deps} deps @param {Ctx} ctx @param {string} name @param {() => Promise<void>} fn
|
|
89
106
|
*/
|
|
@@ -116,7 +133,7 @@ async function runStep(deps, ctx, name, fn) {
|
|
|
116
133
|
* @param {Deps} deps @param {T[]} items @param {(item: T) => Promise<void>} fn
|
|
117
134
|
* @param {(item: T) => { repo?: string, issue?: number }} where
|
|
118
135
|
*/
|
|
119
|
-
async function each(deps, items, fn, where) {
|
|
136
|
+
export async function each(deps, items, fn, where) {
|
|
120
137
|
for (const item of items) {
|
|
121
138
|
try {
|
|
122
139
|
await fn(item)
|
|
@@ -143,30 +160,6 @@ async function read(deps, ctx) {
|
|
|
143
160
|
throw new Error(`no repository could be read`)
|
|
144
161
|
}
|
|
145
162
|
|
|
146
|
-
// ---- GitHub writes. Each updates the in-memory issue so later steps in the same tick see it.
|
|
147
|
-
|
|
148
|
-
/** @param {Deps} deps @param {string} slug @param {Issue} issue @param {{ add: string[], remove: string[] }} change */
|
|
149
|
-
async function setLabels(deps, slug, issue, change) {
|
|
150
|
-
if (!change.add.length && !change.remove.length) return
|
|
151
|
-
await deps.github.label(slug, issue.number, change)
|
|
152
|
-
issue.labels = apply(issue.labels, change)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** @param {Deps} deps @param {string} slug @param {Issue} issue @param {import('./marker.mjs').Marker} marker */
|
|
156
|
-
async function setMarker(deps, slug, issue, marker) {
|
|
157
|
-
const maxAttempts = deps.config.phases[marker.phase]?.maxAttempts
|
|
158
|
-
const id = await deps.github.writeState(
|
|
159
|
-
slug,
|
|
160
|
-
issue.number,
|
|
161
|
-
issue.stateComment?.id ?? null,
|
|
162
|
-
renderMarker(marker, { maxAttempts }),
|
|
163
|
-
)
|
|
164
|
-
issue.stateComment = { id, marker }
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/** @param {Issue} issue */
|
|
168
|
-
const markerOf = (issue) => issue.stateComment?.marker ?? newMarker()
|
|
169
|
-
|
|
170
163
|
/**
|
|
171
164
|
* Into the Human Queue with a reason label. The marker is written before the
|
|
172
165
|
* labels: a half-applied transition is then re-done (and over-counted) on the
|
|
@@ -307,28 +300,13 @@ export async function finish(deps, ctx, w, snap) {
|
|
|
307
300
|
|
|
308
301
|
// ---- Watch (no agent)
|
|
309
302
|
|
|
310
|
-
/** @param {import('./github.mjs').PullRequest} pr */
|
|
311
|
-
function failureSummary(pr) {
|
|
312
|
-
const lines = pr.failing.map(
|
|
313
|
-
(f) => `${f.name}${f.url ? ` (${f.url})` : ''}${f.summary ? `\n ${f.summary.replace(/\n/g, '\n ')}` : ''}`,
|
|
314
|
-
)
|
|
315
|
-
return `CI red on PR #${pr.number} at ${(pr.headSha ?? '').slice(0, 7)}:\n${lines.join('\n') || '(no failing check reported)'}`
|
|
316
|
-
}
|
|
317
|
-
|
|
318
303
|
/** @param {Deps} deps @param {Ctx} ctx */
|
|
319
304
|
async function watch(deps, ctx) {
|
|
320
305
|
for (const [slug, snap] of ctx.snaps) {
|
|
321
306
|
const deleted = new Set()
|
|
322
|
-
/** @type {[import('./github.mjs').PullRequest, Issue][]} */
|
|
323
|
-
const pairs = []
|
|
324
|
-
for (const pr of snap.prs)
|
|
325
|
-
for (const n of pr.closes) {
|
|
326
|
-
const issue = snap.issues.get(n)
|
|
327
|
-
if (issue?.stateComment) pairs.push([pr, issue])
|
|
328
|
-
}
|
|
329
307
|
await each(
|
|
330
308
|
deps,
|
|
331
|
-
|
|
309
|
+
watchPairs(snap),
|
|
332
310
|
async ([pr, issue]) => {
|
|
333
311
|
if (ctx.touched.has(key(slug, issue.number))) return
|
|
334
312
|
const m = markerOf(issue)
|
|
@@ -352,7 +330,7 @@ async function watch(deps, ctx) {
|
|
|
352
330
|
return
|
|
353
331
|
}
|
|
354
332
|
if (pr.state !== 'OPEN' || issue.state !== 'OPEN') return
|
|
355
|
-
if (
|
|
333
|
+
if (!watched(deps, slug, issue)) return
|
|
356
334
|
if ((pr.checks === 'FAILURE' || pr.checks === 'ERROR') && pr.headSha !== m.failedSha)
|
|
357
335
|
return requeue(deps, ctx, slug, issue, {
|
|
358
336
|
reason: failureSummary(pr),
|
|
@@ -381,34 +359,80 @@ async function watch(deps, ctx) {
|
|
|
381
359
|
}
|
|
382
360
|
}
|
|
383
361
|
|
|
362
|
+
/**
|
|
363
|
+
* Watch's work list: every (PR, issue it closes) pair where cambium already owns the issue's state comment.
|
|
364
|
+
* @param {Snapshot} snap
|
|
365
|
+
*/
|
|
366
|
+
export function watchPairs(snap) {
|
|
367
|
+
/** @type {[import('./github.mjs').PullRequest, Issue][]} */
|
|
368
|
+
const pairs = []
|
|
369
|
+
for (const pr of snap.prs)
|
|
370
|
+
for (const n of pr.closes) {
|
|
371
|
+
const issue = snap.issues.get(n)
|
|
372
|
+
if (issue?.stateComment) pairs.push([pr, issue])
|
|
373
|
+
}
|
|
374
|
+
return pairs
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Watch acts on an open PR only while its issue is in no queue, holds no lease and has no tracked worker.
|
|
379
|
+
* @param {Deps} deps @param {string} slug @param {Issue} issue
|
|
380
|
+
*/
|
|
381
|
+
export const watched = (deps, slug, issue) =>
|
|
382
|
+
!(queueOf(issue.labels) || issue.labels.includes(RUNNING) || deps.store.worker(slug, issue.number))
|
|
383
|
+
|
|
384
384
|
// ---- Sweep: stale leases (a `running` label on this host with no tracked worker)
|
|
385
385
|
|
|
386
386
|
/** @param {Deps} deps @param {Ctx} ctx */
|
|
387
387
|
async function sweep(deps, ctx) {
|
|
388
|
-
for (const [slug, snap] of ctx.snaps)
|
|
389
|
-
const stale = [...snap.issues.values()].filter((issue) => {
|
|
390
|
-
if (issue.state !== 'OPEN' || !issue.labels.includes(RUNNING)) return false
|
|
391
|
-
if (ctx.touched.has(key(slug, issue.number)) || deps.store.worker(slug, issue.number)) return false
|
|
392
|
-
const host = issue.stateComment?.marker.host
|
|
393
|
-
return !host || host === deps.config.host
|
|
394
|
-
})
|
|
388
|
+
for (const [slug, snap] of ctx.snaps)
|
|
395
389
|
await each(
|
|
396
390
|
deps,
|
|
397
|
-
|
|
398
|
-
(issue) =>
|
|
399
|
-
requeue(deps, ctx, slug, issue, {
|
|
400
|
-
reason: `worker lost: \`running\` on ${deps.config.host} with no live process`,
|
|
401
|
-
outcome: 'lost',
|
|
402
|
-
}),
|
|
391
|
+
staleLeases(deps, ctx, slug, snap),
|
|
392
|
+
(issue) => requeue(deps, ctx, slug, issue, lostLease(deps)),
|
|
403
393
|
(issue) => ({ repo: slug, issue: issue.number }),
|
|
404
394
|
)
|
|
405
|
-
}
|
|
406
395
|
}
|
|
407
396
|
|
|
397
|
+
/**
|
|
398
|
+
* Open issues labelled `running` on this host (or no host) with no tracked worker, untouched this tick.
|
|
399
|
+
* @param {Deps} deps @param {Ctx} ctx @param {string} slug @param {Snapshot} snap
|
|
400
|
+
*/
|
|
401
|
+
export function staleLeases(deps, ctx, slug, snap) {
|
|
402
|
+
return [...snap.issues.values()].filter((issue) => {
|
|
403
|
+
if (issue.state !== 'OPEN' || !issue.labels.includes(RUNNING)) return false
|
|
404
|
+
if (ctx.touched.has(key(slug, issue.number)) || deps.store.worker(slug, issue.number)) return false
|
|
405
|
+
const host = issue.stateComment?.marker.host
|
|
406
|
+
return !host || host === deps.config.host
|
|
407
|
+
})
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** @param {Deps} deps */
|
|
411
|
+
export const lostLease = (deps) => ({
|
|
412
|
+
reason: `worker lost: \`running\` on ${deps.config.host} with no live process`,
|
|
413
|
+
outcome: 'lost',
|
|
414
|
+
})
|
|
415
|
+
|
|
408
416
|
// ---- Schedule
|
|
409
417
|
|
|
410
418
|
/** @param {Deps} deps @param {Ctx} ctx */
|
|
411
|
-
async function
|
|
419
|
+
async function scheduleTasks(deps, ctx) {
|
|
420
|
+
await each(
|
|
421
|
+
deps,
|
|
422
|
+
taskCandidates(deps, ctx),
|
|
423
|
+
async (c) => {
|
|
424
|
+
await startTask(deps, ctx, c.slug, c.issue, c.snap)
|
|
425
|
+
},
|
|
426
|
+
(c) => ({ repo: c.slug, issue: c.issue.number }),
|
|
427
|
+
)
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* The `q:task` issues to start this tick: no `running`, no tracked worker, by priority then age, up to the free
|
|
432
|
+
* slots counted from live task workers.
|
|
433
|
+
* @param {Deps} deps @param {Ctx} ctx
|
|
434
|
+
*/
|
|
435
|
+
export function taskCandidates(deps, ctx) {
|
|
412
436
|
const { config, store } = deps
|
|
413
437
|
const free = config.phases.task.cap - store.workers().filter((w) => w.phase === 'task').length
|
|
414
438
|
/** @type {{ slug: string, issue: Issue, snap: Snapshot }[]} */
|
|
@@ -424,15 +448,12 @@ async function schedule(deps, ctx) {
|
|
|
424
448
|
candidates.push({ slug, issue, snap })
|
|
425
449
|
const order = byPriorityThenAge(config.priorityLabels)
|
|
426
450
|
candidates.sort((a, b) => order(a.issue, b.issue))
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
candidates.slice(0, Math.max(0, free)),
|
|
430
|
-
async (c) => {
|
|
431
|
-
await startTask(deps, ctx, c.slug, c.issue, c.snap)
|
|
432
|
-
},
|
|
433
|
-
(c) => ({ repo: c.slug, issue: c.issue.number }),
|
|
434
|
-
)
|
|
451
|
+
return candidates.slice(0, Math.max(0, free))
|
|
452
|
+
}
|
|
435
453
|
|
|
454
|
+
/** @param {Deps} deps @param {Ctx} ctx */
|
|
455
|
+
async function scheduleDispatch(deps, ctx) {
|
|
456
|
+
const { config } = deps
|
|
436
457
|
const dispatchCap = config.phases.dispatch.cap
|
|
437
458
|
if (dispatchCap > 0) {
|
|
438
459
|
const dispatch = deps.dispatch ?? dispatchNotBuilt
|