@oakgreencc/cambium 0.0.21 → 0.0.22

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 CHANGED
@@ -319,6 +319,7 @@ Neither of our repos runs a GitHub merge queue (it costs), so `merge` says who m
319
319
 
320
320
  - `"none"` (default): not cambium. A merge queue, auto-merge or a human merges it; `mergeQueueMinutes` after submission an unmerged PR sends its issue to `q:human` (`stalled`).
321
321
  - `"green"`: cambium merges it. Needs `engine: "graph"` (config refuses it on legacy). When `ci-running` sees the PR's checks `SUCCESS`, with no auto-merge armed and not in a merge queue, it goes to `merge`: `graft pr merge <n> --auto`, so graft's merge policy (bases, protected paths, a red base) still decides: a clean PR merges now, otherwise auto-merge is armed. Then `merging` waits for the merge. A refusal is recorded in the marker (`mergeRefusal`, history `merge-refused`) and asked again on a new head or after 10 minutes. Red CI still requeues, and `mergeQueueMinutes` still stalls to `q:human`.
322
+ - **Up to date first.** Before merging, `merge` asks GitHub live how far the PR's head is behind its base (`compare`, so a merge earlier in the same tick counts). Behind → `graft pr update <n>` instead of the merge (history `branch-updated`, held like a refusal on the old head), and the updated head merges once its CI is green; red there requeues like any red CI. This is a merge queue without the cost: two PRs green on the same base can be red together (sektorapp/monorepo#4624 + #4625 broke develop 8 s apart), and the base does not require up-to-date branches. An update GitHub refuses (a conflict) is a `merge-refused` and stalls to `q:human` like any other.
322
323
 
323
324
  The worker prompt says which: "Green: stop; cambium merges the PR once CI is green" or "…the PR is merged outside this session".
324
325
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oakgreencc/cambium",
3
- "version": "0.0.21",
3
+ "version": "0.0.22",
4
4
  "description": "The scheduler daemon: reads the q:* queues from GitHub every tick, runs headless claude workers in their own worktrees up to a cap, and makes every queue transition itself.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "type": "module",
package/src/github.mjs CHANGED
@@ -47,8 +47,12 @@ import { Q, QUEUE_LABELS } from './labels.mjs'
47
47
  * editPull(slug: string, number: number, change: { title: string, body: string }): Promise<void>,
48
48
  * closePull(slug: string, number: number, comment: string): Promise<void>,
49
49
  * mergePull(slug: string, number: number): Promise<void>,
50
+ * behindBase(slug: string, number: number): Promise<{ behind: number, base: string }>,
51
+ * updatePull(slug: string, number: number): Promise<void>,
50
52
  * linkIssue(slug: string, pr: number, issue: number): Promise<void>,
51
53
  * }} GitHub
54
+ * `behindBase`: how many commits of its base the PR's head lacks, read live (not from the tick's snapshot), so a
55
+ * merge earlier in the same tick counts. `updatePull`: merge the base into the PR (`graft pr update`).
52
56
  * `linkIssue`: put `Closes #<issue>` at the top of the PR's body, so the PR names the issue it closes.
53
57
  */
54
58
 
@@ -368,6 +372,17 @@ export function graftGitHub({ config, exec: run = exec }) {
368
372
  async mergePull(slug, number) {
369
373
  await graft(slug, ['pr', 'merge', String(number), '--auto'])
370
374
  },
375
+ async behindBase(slug, number) {
376
+ const pr = await json(graft(slug, ['api', 'GET', `/repos/${slug}/pulls/${number}`]))
377
+ const base = pr.base.ref
378
+ const cmp = await json(
379
+ graft(slug, ['api', 'GET', `/repos/${slug}/compare/${encodeURIComponent(base)}...${pr.head.sha}`]),
380
+ )
381
+ return { behind: Number(cmp.behind_by ?? 0), base }
382
+ },
383
+ async updatePull(slug, number) {
384
+ await graft(slug, ['pr', 'update', String(number)])
385
+ },
371
386
  async linkIssue(slug, pr, issue) {
372
387
  const path = `/repos/${slug}/pulls/${pr}`
373
388
  const { body } = await json(graft(slug, ['api', 'GET', path]))
@@ -429,18 +429,36 @@ const merge = declare(
429
429
  defineNode({
430
430
  name: 'merge',
431
431
  input: MergeIn,
432
- output: z.object({ pr: z.number().int(), branch: z.string(), refused: z.string().nullable() }),
432
+ output: z.object({
433
+ pr: z.number().int(),
434
+ branch: z.string(),
435
+ refused: z.string().nullable(),
436
+ updated: z.string().nullable().optional(),
437
+ }),
433
438
  /**
434
439
  * Green: merge through `graft pr merge --auto` (a merge now when the PR is clean, else auto-merge), so graft's
435
440
  * merge policy decides. A refusal is not a failure of the work: `merging` holds and asks again later.
441
+ *
442
+ * Behind its base (read live, so a merge earlier in this tick counts): update the branch instead. Its green
443
+ * was earned against an older base, and two PRs green on the same base can be red together (no merge queue,
444
+ * and the base does not require up-to-date branches). `merging` holds on the old head and merges once the
445
+ * updated head is green; red goes back to a worker like any red CI.
436
446
  * @param {z.infer<typeof MergeIn>} input @param {TaskCtx} ctx
437
447
  */
438
448
  run: async (input, ctx) => {
449
+ const { slug } = ctx.subject
439
450
  try {
440
- await ctx.github.mergePull(ctx.subject.slug, input.pr)
441
- return { ...input, refused: null }
451
+ const { behind, base } = await ctx.github.behindBase(slug, input.pr)
452
+ if (behind > 0) {
453
+ await ctx.github.updatePull(slug, input.pr)
454
+ const commits = `${behind} commit${behind === 1 ? '' : 's'}`
455
+ const updated = `PR #${input.pr} was ${commits} behind ${base}; updated it, merging once CI is green on the new head`
456
+ return { ...input, refused: null, updated }
457
+ }
458
+ await ctx.github.mergePull(slug, input.pr)
459
+ return { ...input, refused: null, updated: null }
442
460
  } catch (err) {
443
- return { ...input, refused: `merge refused: ${/** @type {Error} */ (err)?.message ?? err}` }
461
+ return { ...input, refused: `merge refused: ${/** @type {Error} */ (err)?.message ?? err}`, updated: null }
444
462
  }
445
463
  },
446
464
  }),
@@ -477,7 +495,7 @@ const merging = declare(
477
495
  {
478
496
  queue: null,
479
497
  running: false,
480
- /** @param {z.infer<typeof MergeIn> & { refused: string | null }} input @param {TaskCtx} ctx */
498
+ /** @param {z.infer<typeof MergeIn> & { refused: string | null, updated?: string | null }} input @param {TaskCtx} ctx */
481
499
  enter: async (input, ctx) => {
482
500
  const { slug } = ctx.subject
483
501
  const issue = issueOf(ctx)
@@ -485,19 +503,23 @@ const merging = declare(
485
503
  const pr = prOf(ctx, input)
486
504
  const at = iso(ctx.tick.now)
487
505
  const { refused } = input
506
+ const updated = input.updated ?? null
507
+ // An update holds like a refusal on the head it replaced: the new head is due as soon as it is green.
508
+ const hold = refused ?? updated
488
509
  await land(ctx, 'merging', {
489
510
  ...withHistory(m, {
490
511
  at,
491
512
  attempt: m.attempt,
492
- outcome: refused ? 'merge-refused' : 'merge-armed',
513
+ outcome: refused ? 'merge-refused' : updated ? 'branch-updated' : 'merge-armed',
493
514
  pr: input.pr,
494
- ...(refused ? { reason: refused } : {}),
515
+ ...(hold ? { reason: hold } : {}),
495
516
  }),
496
- mergeRefusal: refused ? { sha: pr?.headSha ?? null, at, reason: refused } : null,
517
+ mergeRefusal: hold ? { sha: pr?.headSha ?? null, at, reason: hold } : null,
497
518
  lastFailure: refused ?? m.lastFailure ?? null,
498
519
  input: { pr: input.pr, branch: input.branch },
499
520
  })
500
521
  if (refused) ctx.log('warn', refused.split('\n')[0], { repo: slug, issue: issue.number })
522
+ else if (updated) ctx.log('info', updated, { repo: slug, issue: issue.number })
501
523
  else ctx.log('info', `merging PR #${input.pr}`, { repo: slug, issue: issue.number })
502
524
  },
503
525
  },