@nanobpm/nano-workforce 0.111.0 → 0.111.1
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/CHANGELOG.md +7 -0
- package/app/deliveryGraph.ts +46 -0
- package/app/deliveryGraphCompiler.test.ts +275 -0
- package/app/deliveryGraphCompiler.ts +487 -0
- package/app/github.test.ts +225 -1
- package/app/github.ts +102 -2
- package/app/readiness.test.ts +2 -0
- package/app/readiness.ts +3 -1
- package/app/service.ts +5 -2
- package/openapi.yaml +248 -0
- package/operations/compileDeliveryGraph.test.ts +60 -0
- package/operations/compileDeliveryGraph.ts +35 -0
- package/package.json +1 -1
package/app/github.test.ts
CHANGED
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
// the merge-exclusion graph. Force the token transport and stub `globalThis.fetch`.
|
|
4
4
|
import { test } from "node:test";
|
|
5
5
|
import { assertEquals, assertRejects } from "#test-assert";
|
|
6
|
-
import { BaseBranchMustExistError, classifyPrLiveness, coalesceTitle, createPullRequest, ensureBaseBranch, ensurePromotionPr, fetchIssueTitle, fetchPrFiles, isNotAPullRequestError, listPrsForHead, type PrState } from "./github.ts";
|
|
6
|
+
import { BaseBranchMustExistError, checkConclusions, classifyMergeability, classifyPrLiveness, coalesceTitle, createPullRequest, ensureBaseBranch, ensurePromotionPr, fetchIssueTitle, fetchPrFiles, isNotAPullRequestError, listPrsForHead, type Mergeability, type PrState } from "./github.ts";
|
|
7
|
+
import { DEFAULT_MERGE_PROTOCOL, type MergeProtocol, type RequiredCheck } from "./mergeProtocol.ts";
|
|
7
8
|
|
|
8
9
|
// A fake `fetch` that serves `pages` of file batches; each page N (1-based) returns `pages[N-1]`
|
|
9
10
|
// files (named `f{index}`), setting a `Link: rel="next"` header whenever a later page exists.
|
|
@@ -440,6 +441,8 @@ function prState(over: Partial<PrState>): PrState {
|
|
|
440
441
|
failingChecks: 0,
|
|
441
442
|
failingCheckNames: [],
|
|
442
443
|
presentCheckNames: [],
|
|
444
|
+
pendingCheckNames: [],
|
|
445
|
+
checkConclusions: {},
|
|
443
446
|
totalChecks: 0,
|
|
444
447
|
isDraft: false,
|
|
445
448
|
headRefOid: null,
|
|
@@ -463,3 +466,224 @@ test("classifyPrLiveness: a closed-not-merged PR is terminal (abandon)", () => {
|
|
|
463
466
|
test("classifyPrLiveness: a null read (transport hiccup) is unknown — never abandons blind", () => {
|
|
464
467
|
assertEquals(classifyPrLiveness(null), "unknown");
|
|
465
468
|
});
|
|
469
|
+
|
|
470
|
+
// ── classifyMergeability: protocol-aware required-checks backstop (issue #392) ────────────────────
|
|
471
|
+
//
|
|
472
|
+
// The merge poller must NOT merge a PR whose DECLARED-required check is red, even on a repo that
|
|
473
|
+
// under-specifies its GitHub-required checks (so GitHub reports the PR as UNSTABLE, i.e. "only
|
|
474
|
+
// non-required checks failing" from GitHub's view). `classifyMergeability` now intersects the repo's
|
|
475
|
+
// merge-protocol `requiredChecks[]`/`waitForChecks` against the head's latest-run-per-check
|
|
476
|
+
// conclusions (via `latestRunPerCheck`, preserving the #348 CANCELLED-supersede semantics) as an
|
|
477
|
+
// independent backstop that runs BEFORE the `mergeStateStatus` switch. These are pure unit tests.
|
|
478
|
+
|
|
479
|
+
// Build a `PrState` with sensible defaults; `over` supplies the fields a case cares about. `over`
|
|
480
|
+
// may pass a `rollup` shorthand (name → conclusion) that we compile into the exact per-check fields
|
|
481
|
+
// `classifyMergeability` reads (present/pending/conclusions), mirroring what `fetchPrState` derives.
|
|
482
|
+
function mergePrState(over: Partial<PrState> & { rollup?: { name: string; conclusion: string }[] } = {}): PrState {
|
|
483
|
+
const { rollup, ...rest } = over;
|
|
484
|
+
const base: PrState = {
|
|
485
|
+
merged: false,
|
|
486
|
+
state: "open",
|
|
487
|
+
mergeStateStatus: "CLEAN",
|
|
488
|
+
failingChecks: 0,
|
|
489
|
+
failingCheckNames: [],
|
|
490
|
+
totalChecks: 0,
|
|
491
|
+
presentCheckNames: [],
|
|
492
|
+
pendingCheckNames: [],
|
|
493
|
+
checkConclusions: {},
|
|
494
|
+
isDraft: false,
|
|
495
|
+
headRefOid: "abc123",
|
|
496
|
+
};
|
|
497
|
+
if (rollup) {
|
|
498
|
+
const bad = new Set(["FAILURE", "TIMED_OUT", "CANCELLED", "ACTION_REQUIRED", "STARTUP_FAILURE", "ERROR"]);
|
|
499
|
+
const pendingStates = new Set(["", "PENDING", "QUEUED", "IN_PROGRESS", "EXPECTED", "WAITING"]);
|
|
500
|
+
const present: string[] = [];
|
|
501
|
+
const pending: string[] = [];
|
|
502
|
+
const failing: string[] = [];
|
|
503
|
+
const conclusions: Record<string, string> = {};
|
|
504
|
+
for (const c of rollup) {
|
|
505
|
+
const v = c.conclusion.toUpperCase();
|
|
506
|
+
present.push(c.name);
|
|
507
|
+
conclusions[c.name] = pendingStates.has(v) ? "" : v;
|
|
508
|
+
if (pendingStates.has(v)) pending.push(c.name);
|
|
509
|
+
else if (bad.has(v)) failing.push(c.name);
|
|
510
|
+
}
|
|
511
|
+
base.presentCheckNames = present;
|
|
512
|
+
base.pendingCheckNames = pending;
|
|
513
|
+
base.checkConclusions = conclusions;
|
|
514
|
+
base.failingCheckNames = failing;
|
|
515
|
+
base.failingChecks = failing.length;
|
|
516
|
+
base.totalChecks = rollup.length;
|
|
517
|
+
}
|
|
518
|
+
return { ...base, ...rest };
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
function reqChecks(...names: string[]): RequiredCheck[] {
|
|
522
|
+
return names.map((name) => ({ name, acceptedConclusions: ["success"] }));
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function protocolWith(over: Partial<MergeProtocol>): MergeProtocol {
|
|
526
|
+
return { ...DEFAULT_MERGE_PROTOCOL, ...over };
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
interface MergeCase {
|
|
530
|
+
name: string;
|
|
531
|
+
state: Partial<PrState> & { rollup?: { name: string; conclusion: string }[] };
|
|
532
|
+
protocol?: MergeProtocol;
|
|
533
|
+
want: Mergeability;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
const MERGE_CASES: MergeCase[] = [
|
|
537
|
+
// The exact defect: UNSTABLE + a red DECLARED-required check used to classify `ready` and merge.
|
|
538
|
+
{
|
|
539
|
+
name: "UNSTABLE + red declared-required check -> blocked (the #392 defect)",
|
|
540
|
+
state: { mergeStateStatus: "UNSTABLE", rollup: [{ name: "test (22.x, simple)", conclusion: "FAILURE" }] },
|
|
541
|
+
protocol: protocolWith({ requiredChecks: [{ name: "test (22.x, simple)", acceptedConclusions: ["success"] }] }),
|
|
542
|
+
want: "blocked",
|
|
543
|
+
},
|
|
544
|
+
{
|
|
545
|
+
name: "CLEAN + red declared-required check -> blocked (backstop runs before the switch)",
|
|
546
|
+
state: { mergeStateStatus: "CLEAN", rollup: [{ name: "build", conclusion: "FAILURE" }] },
|
|
547
|
+
protocol: protocolWith({ requiredChecks: reqChecks("build") }),
|
|
548
|
+
want: "blocked",
|
|
549
|
+
},
|
|
550
|
+
{
|
|
551
|
+
name: "declared-required check pending -> waiting",
|
|
552
|
+
state: { mergeStateStatus: "CLEAN", rollup: [{ name: "build", conclusion: "IN_PROGRESS" }] },
|
|
553
|
+
protocol: protocolWith({ requiredChecks: reqChecks("build") }),
|
|
554
|
+
want: "waiting",
|
|
555
|
+
},
|
|
556
|
+
{
|
|
557
|
+
name: "declared-required check absent from head -> waiting (absence is not a pass)",
|
|
558
|
+
state: { mergeStateStatus: "CLEAN", rollup: [{ name: "lint", conclusion: "SUCCESS" }] },
|
|
559
|
+
protocol: protocolWith({ requiredChecks: reqChecks("build") }),
|
|
560
|
+
want: "waiting",
|
|
561
|
+
},
|
|
562
|
+
{
|
|
563
|
+
name: "declared-required check passing, CLEAN -> ready",
|
|
564
|
+
state: { mergeStateStatus: "CLEAN", rollup: [{ name: "build", conclusion: "SUCCESS" }] },
|
|
565
|
+
protocol: protocolWith({ requiredChecks: reqChecks("build") }),
|
|
566
|
+
want: "ready",
|
|
567
|
+
},
|
|
568
|
+
{
|
|
569
|
+
name: "declared-required check passing, UNSTABLE -> ready (falls through to the switch)",
|
|
570
|
+
state: { mergeStateStatus: "UNSTABLE", rollup: [{ name: "build", conclusion: "SUCCESS" }] },
|
|
571
|
+
protocol: protocolWith({ requiredChecks: reqChecks("build") }),
|
|
572
|
+
want: "ready",
|
|
573
|
+
},
|
|
574
|
+
{
|
|
575
|
+
name: "non-required check failing (not declared-required), UNSTABLE -> ready (today's behaviour)",
|
|
576
|
+
state: {
|
|
577
|
+
mergeStateStatus: "UNSTABLE",
|
|
578
|
+
rollup: [{ name: "build", conclusion: "SUCCESS" }, { name: "flaky-optional", conclusion: "FAILURE" }],
|
|
579
|
+
},
|
|
580
|
+
protocol: protocolWith({ requiredChecks: reqChecks("build") }),
|
|
581
|
+
want: "ready",
|
|
582
|
+
},
|
|
583
|
+
{
|
|
584
|
+
name: "CANCELLED superseded by a newer green run on a required check -> ready (#348 semantics)",
|
|
585
|
+
// `prState`'s rollup shorthand keeps one conclusion per name (latest wins); model the superseded
|
|
586
|
+
// + re-run by asserting the green outcome the rollup helpers collapse to.
|
|
587
|
+
state: { mergeStateStatus: "UNSTABLE", rollup: [{ name: "engine-core", conclusion: "SUCCESS" }] },
|
|
588
|
+
protocol: protocolWith({ requiredChecks: reqChecks("engine-core") }),
|
|
589
|
+
want: "ready",
|
|
590
|
+
},
|
|
591
|
+
{
|
|
592
|
+
name: "acceptedConclusions beyond [success] honoured: NEUTRAL accepted -> ready",
|
|
593
|
+
state: { mergeStateStatus: "CLEAN", rollup: [{ name: "build", conclusion: "NEUTRAL" }] },
|
|
594
|
+
protocol: protocolWith({ requiredChecks: [{ name: "build", acceptedConclusions: ["success", "neutral"] }] }),
|
|
595
|
+
want: "ready",
|
|
596
|
+
},
|
|
597
|
+
{
|
|
598
|
+
name: "acceptedConclusions [success] does NOT accept a NEUTRAL required conclusion -> blocked",
|
|
599
|
+
state: { mergeStateStatus: "CLEAN", rollup: [{ name: "build", conclusion: "NEUTRAL" }] },
|
|
600
|
+
protocol: protocolWith({ requiredChecks: [{ name: "build", acceptedConclusions: ["success"] }] }),
|
|
601
|
+
want: "blocked",
|
|
602
|
+
},
|
|
603
|
+
{
|
|
604
|
+
name: "waitForChecks:true + pending required check -> waiting even when CLEAN",
|
|
605
|
+
state: { mergeStateStatus: "CLEAN", rollup: [{ name: "build", conclusion: "QUEUED" }] },
|
|
606
|
+
protocol: protocolWith({ requiredChecks: reqChecks("build"), waitForChecks: true }),
|
|
607
|
+
want: "waiting",
|
|
608
|
+
},
|
|
609
|
+
];
|
|
610
|
+
|
|
611
|
+
for (const c of MERGE_CASES) {
|
|
612
|
+
test(`classifyMergeability: ${c.name}`, () => {
|
|
613
|
+
assertEquals(classifyMergeability(mergePrState(c.state), c.protocol), c.want);
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
// Empty requiredChecks (the DEFAULT protocol) — behaviour must be IDENTICAL to today across every
|
|
618
|
+
// mergeStateStatus, whether a protocol is passed or omitted entirely.
|
|
619
|
+
const DEFAULT_BEHAVIOUR: { status: string; failingChecks?: number; want: Mergeability }[] = [
|
|
620
|
+
{ status: "CLEAN", want: "ready" },
|
|
621
|
+
{ status: "HAS_HOOKS", want: "ready" },
|
|
622
|
+
{ status: "UNSTABLE", want: "ready" },
|
|
623
|
+
{ status: "BEHIND", want: "ready" },
|
|
624
|
+
{ status: "DIRTY", want: "conflict" },
|
|
625
|
+
{ status: "BLOCKED", failingChecks: 1, want: "blocked" },
|
|
626
|
+
{ status: "BLOCKED", failingChecks: 0, want: "waiting" },
|
|
627
|
+
{ status: "UNKNOWN", want: "waiting" },
|
|
628
|
+
{ status: "", want: "waiting" },
|
|
629
|
+
];
|
|
630
|
+
|
|
631
|
+
for (const c of DEFAULT_BEHAVIOUR) {
|
|
632
|
+
test(`classifyMergeability: empty requiredChecks keeps today's behaviour (${c.status || "''"} -> ${c.want})`, () => {
|
|
633
|
+
const s = prState({ mergeStateStatus: c.status, failingChecks: c.failingChecks ?? 0 });
|
|
634
|
+
// Explicit default protocol and omitted-protocol must agree.
|
|
635
|
+
assertEquals(classifyMergeability(s, DEFAULT_MERGE_PROTOCOL), c.want);
|
|
636
|
+
assertEquals(classifyMergeability(s), c.want);
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// Token mode: the transport can't enumerate checks (`failingChecks === -1`, empty per-check lists),
|
|
641
|
+
// so even a repo that declares requiredChecks must fall through to today's `mergeStateStatus`
|
|
642
|
+
// behaviour — the backstop must NEVER newly block or wait when checks are unenumerable.
|
|
643
|
+
const TOKEN_MODE: { status: string; want: Mergeability }[] = [
|
|
644
|
+
{ status: "CLEAN", want: "ready" },
|
|
645
|
+
{ status: "UNSTABLE", want: "ready" },
|
|
646
|
+
{ status: "BLOCKED", want: "waiting" }, // failingChecks<0 → conservative wait, exactly as before
|
|
647
|
+
{ status: "DIRTY", want: "conflict" },
|
|
648
|
+
{ status: "UNKNOWN", want: "waiting" },
|
|
649
|
+
];
|
|
650
|
+
|
|
651
|
+
for (const c of TOKEN_MODE) {
|
|
652
|
+
test(`classifyMergeability: token mode falls through, never newly blocks (${c.status} -> ${c.want})`, () => {
|
|
653
|
+
const s = prState({
|
|
654
|
+
mergeStateStatus: c.status,
|
|
655
|
+
failingChecks: -1,
|
|
656
|
+
totalChecks: -1,
|
|
657
|
+
presentCheckNames: [],
|
|
658
|
+
pendingCheckNames: [],
|
|
659
|
+
checkConclusions: {},
|
|
660
|
+
});
|
|
661
|
+
const protocol = protocolWith({ requiredChecks: reqChecks("build"), waitForChecks: true });
|
|
662
|
+
assertEquals(classifyMergeability(s, protocol), c.want);
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
// `checkConclusions` must report a terminal conclusion per check but normalise a STILL-IN-FLIGHT run
|
|
667
|
+
// to "" for BOTH rollup shapes — a CheckRun whose `status` is not COMPLETED, and a legacy
|
|
668
|
+
// StatusContext whose `state` is PENDING/EXPECTED — so a caller never mistakes a pending
|
|
669
|
+
// status-context's upper-cased `state` (e.g. "PENDING") for a terminal conclusion.
|
|
670
|
+
test("checkConclusions: in-flight runs map to '' for both CheckRun and StatusContext shapes", () => {
|
|
671
|
+
const got = checkConclusions([
|
|
672
|
+
{ name: "ci-success", status: "COMPLETED", conclusion: "SUCCESS" },
|
|
673
|
+
{ name: "ci-failure", status: "COMPLETED", conclusion: "FAILURE" },
|
|
674
|
+
{ name: "ci-running", status: "IN_PROGRESS" }, // CheckRun in flight -> ""
|
|
675
|
+
{ name: "ci-queued", status: "QUEUED" }, // CheckRun queued -> ""
|
|
676
|
+
{ context: "legacy-pending", state: "PENDING" }, // StatusContext in flight -> ""
|
|
677
|
+
{ context: "legacy-expected", state: "EXPECTED" }, // StatusContext in flight -> ""
|
|
678
|
+
{ context: "legacy-error", state: "ERROR" }, // StatusContext terminal -> preserved
|
|
679
|
+
]);
|
|
680
|
+
assertEquals(got, {
|
|
681
|
+
"ci-success": "SUCCESS",
|
|
682
|
+
"ci-failure": "FAILURE",
|
|
683
|
+
"ci-running": "",
|
|
684
|
+
"ci-queued": "",
|
|
685
|
+
"legacy-pending": "",
|
|
686
|
+
"legacy-expected": "",
|
|
687
|
+
"legacy-error": "ERROR",
|
|
688
|
+
});
|
|
689
|
+
});
|
package/app/github.ts
CHANGED
|
@@ -11,6 +11,11 @@
|
|
|
11
11
|
// The poller is app-side host glue (main.ts), so host-specific subprocess I/O is allowed here.
|
|
12
12
|
// Cross-runtime: runs under Node (`node:child_process`).
|
|
13
13
|
|
|
14
|
+
// Type-only import (erased at runtime, so no runtime cycle with mergeProtocol.ts, which imports
|
|
15
|
+
// `fetchRepoFile` from here): `classifyMergeability` reads a repo's declared required checks to gate
|
|
16
|
+
// a merge independently of GitHub branch protection.
|
|
17
|
+
import type { MergeProtocol } from "./mergeProtocol.ts";
|
|
18
|
+
|
|
14
19
|
/** A GitHub pull-request review, narrowed to the fields the poller needs. */
|
|
15
20
|
export interface GhReview {
|
|
16
21
|
id: number;
|
|
@@ -502,6 +507,17 @@ export interface PrState {
|
|
|
502
507
|
* unrelated always-on check (e.g. Mergify's "Merge Queue") must not read as "the required run
|
|
503
508
|
* already happened". */
|
|
504
509
|
presentCheckNames: string[];
|
|
510
|
+
/** Names of every head check still in flight (queued/in progress, not yet concluded and not a hard
|
|
511
|
+
* failure), derived over the newest run per check (`pendingCheckNames`). Empty in token mode (the
|
|
512
|
+
* REST fallback can't enumerate checks). Lets `classifyMergeability` hold a merge when a
|
|
513
|
+
* declared-required check has not yet concluded, without re-deriving conclusions by hand. */
|
|
514
|
+
pendingCheckNames: string[];
|
|
515
|
+
/** The newest concluded conclusion per head check (name → uppercase conclusion, e.g. `SUCCESS` /
|
|
516
|
+
* `FAILURE` / `NEUTRAL` / `SKIPPED`), derived over `latestRunPerCheck` so a `CANCELLED` run
|
|
517
|
+
* superseded by a newer green run on the same head reports the green result (#348). A still-pending
|
|
518
|
+
* run maps to `""` (it has no conclusion yet — use `pendingCheckNames`). Empty in token mode. Lets
|
|
519
|
+
* `classifyMergeability` honour a required check's `acceptedConclusions` precisely. */
|
|
520
|
+
checkConclusions: Record<string, string>;
|
|
505
521
|
/** Whether the PR is a draft (a fresh head run is produced by marking it ready, not reopen). */
|
|
506
522
|
isDraft: boolean;
|
|
507
523
|
/** Current head commit. Used to scope one-shot merge-protocol nudges to a landing attempt. */
|
|
@@ -633,6 +649,29 @@ export function allCheckNames(rollup: RollupEntry[]): string[] {
|
|
|
633
649
|
return names;
|
|
634
650
|
}
|
|
635
651
|
|
|
652
|
+
/** The ground-truth conclusion of each head check, keyed by check name, derived over the **newest run
|
|
653
|
+
* per check** (`latestRunPerCheck`) so a `CANCELLED` run superseded by a newer green run on the
|
|
654
|
+
* identical head SHA reports the green result, not the stale cancellation (issue #348). The value is
|
|
655
|
+
* the run's `conclusion` (CheckRun) or `state` (legacy StatusContext), upper-cased; a still-in-flight
|
|
656
|
+
* run that has not concluded maps to `""` (it has no conclusion — `pendingCheckNames` tracks those).
|
|
657
|
+
* In-flight is normalised to `""` for BOTH shapes: a CheckRun whose `status` is not `COMPLETED`, and a
|
|
658
|
+
* legacy StatusContext whose `state` is `PENDING`/`EXPECTED`, so a caller never mistakes a pending
|
|
659
|
+
* status-context's `PENDING`/`EXPECTED` `state` for a terminal conclusion.
|
|
660
|
+
* Lets `classifyMergeability` intersect a repo's declared `requiredChecks` against actual head
|
|
661
|
+
* conclusions and honour each check's `acceptedConclusions` without re-deriving per-run state. */
|
|
662
|
+
export function checkConclusions(rollup: RollupEntry[]): Record<string, string> {
|
|
663
|
+
const out: Record<string, string> = {};
|
|
664
|
+
for (const c of latestRunPerCheck(rollup)) {
|
|
665
|
+
const status = (c.status || "").toUpperCase();
|
|
666
|
+
const state = (c.state || "").toUpperCase();
|
|
667
|
+
// A still-in-flight run has no terminal conclusion — normalise both shapes to "" (mirrors
|
|
668
|
+
// `pendingCheckNames`): CheckRun status != COMPLETED, or legacy StatusContext state PENDING/EXPECTED.
|
|
669
|
+
const inFlight = status !== "" ? status !== "COMPLETED" : state === "PENDING" || state === "EXPECTED";
|
|
670
|
+
out[checkKey(c)] = inFlight ? "" : (c.conclusion || c.state || "").toUpperCase();
|
|
671
|
+
}
|
|
672
|
+
return out;
|
|
673
|
+
}
|
|
674
|
+
|
|
636
675
|
/** True when `err` is GitHub reporting that a ref which parsed as `owner/repo#N` is not a pull
|
|
637
676
|
* request — either it's an issue (issues and PRs share GitHub's number space, so an issue number
|
|
638
677
|
* is indistinguishable from a PR number by shape alone) or the number does not exist. Both
|
|
@@ -682,6 +721,8 @@ export async function fetchPrState(
|
|
|
682
721
|
failingCheckNames: names,
|
|
683
722
|
totalChecks: rollup.length,
|
|
684
723
|
presentCheckNames: allCheckNames(rollup),
|
|
724
|
+
pendingCheckNames: pendingCheckNames(rollup),
|
|
725
|
+
checkConclusions: checkConclusions(rollup),
|
|
685
726
|
isDraft: !!j.isDraft,
|
|
686
727
|
headRefOid: j.headRefOid ?? null,
|
|
687
728
|
};
|
|
@@ -713,6 +754,8 @@ export async function fetchPrState(
|
|
|
713
754
|
failingCheckNames: [], // …and the CI-fix agent gets no per-check list in token mode
|
|
714
755
|
totalChecks: -1, // …and the fresh-head-run remedy stays conservative (never reopens blind)
|
|
715
756
|
presentCheckNames: [], // …can't enumerate checks in token mode → no required-check presence signal
|
|
757
|
+
pendingCheckNames: [], // …no per-check pending signal either → classifier degrades to today's switch
|
|
758
|
+
checkConclusions: {}, // …no per-check conclusions → protocol-aware gate falls through in token mode
|
|
716
759
|
isDraft: !!j.draft,
|
|
717
760
|
headRefOid: j.head?.sha ?? null,
|
|
718
761
|
};
|
|
@@ -915,11 +958,68 @@ export async function baseBranchLanded(
|
|
|
915
958
|
* verdict; `waiting` means re-poll later. */
|
|
916
959
|
export type Mergeability = "ready" | "waiting" | "conflict" | "blocked";
|
|
917
960
|
|
|
918
|
-
|
|
961
|
+
/** Intersect a repo's declared `requiredChecks` against the head's actual per-check conclusions —
|
|
962
|
+
* an INDEPENDENT backstop that runs BEFORE the `mergeStateStatus` switch, so nwf never merges a red
|
|
963
|
+
* required check even on a repo that has NOT wired that check as a GitHub-required status check
|
|
964
|
+
* (issue #392). Returns:
|
|
965
|
+
* • `"blocked"` — a declared-required check is present, concluded, and its conclusion is NOT in that
|
|
966
|
+
* check's `acceptedConclusions` (a hard failure like `FAILURE`, or any other unaccepted terminal
|
|
967
|
+
* conclusion) → route to fix-ci, do not merge.
|
|
968
|
+
* • `"waiting"` — a declared-required check is still pending, or absent from the head entirely
|
|
969
|
+
* (not-yet-run counts as pending, NOT as pass): a declared-required check that has not
|
|
970
|
+
* concluded is never mergeable.
|
|
971
|
+
* • `"pass"` — every declared-required check is present and its conclusion accepted → fall through to
|
|
972
|
+
* today's `mergeStateStatus` logic (GitHub branch protection stays the primary gate).
|
|
973
|
+
* Degrades safely: with no declared `requiredChecks`, or in token mode where checks can't be
|
|
974
|
+
* enumerated (`failingChecks < 0`, so the per-check lists are empty), it returns `"pass"` and never
|
|
975
|
+
* newly blocks or waits — repos keep exactly today's behaviour. */
|
|
976
|
+
function requiredChecksVerdict(s: PrState, protocol?: MergeProtocol): "blocked" | "waiting" | "pass" {
|
|
977
|
+
const required = protocol?.requiredChecks ?? [];
|
|
978
|
+
if (required.length === 0) return "pass";
|
|
979
|
+
// Token mode: the transport can't enumerate checks (`failingChecks === -1`), so the per-check lists
|
|
980
|
+
// are empty and absence is indistinguishable from not-yet-run. Do NOT newly block/wait — fall
|
|
981
|
+
// through to today's `mergeStateStatus` behaviour. (A real gh-mode head with no checks yet reports
|
|
982
|
+
// `failingChecks === 0`, so absence there is correctly treated as not-yet-run below.)
|
|
983
|
+
if (s.failingChecks < 0) return "pass";
|
|
984
|
+
const present = new Set(s.presentCheckNames);
|
|
985
|
+
const pending = new Set(s.pendingCheckNames);
|
|
986
|
+
let anyBlocked = false;
|
|
987
|
+
let anyPending = false;
|
|
988
|
+
for (const rc of required) {
|
|
989
|
+
// Absent from the head, or still in flight → not-yet-run → wait (never treat absence as pass).
|
|
990
|
+
if (!present.has(rc.name) || pending.has(rc.name)) {
|
|
991
|
+
anyPending = true;
|
|
992
|
+
continue;
|
|
993
|
+
}
|
|
994
|
+
const conclusion = (s.checkConclusions[rc.name] ?? "").toUpperCase();
|
|
995
|
+
if (conclusion === "") {
|
|
996
|
+
// Present but no terminal conclusion yet (and not flagged pending) — treat conservatively as
|
|
997
|
+
// not-yet-concluded rather than as a pass.
|
|
998
|
+
anyPending = true;
|
|
999
|
+
continue;
|
|
1000
|
+
}
|
|
1001
|
+
const accepted = rc.acceptedConclusions.map((a) => a.toUpperCase());
|
|
1002
|
+
if (accepted.includes(conclusion)) continue; // satisfied
|
|
1003
|
+
anyBlocked = true; // present, concluded, NOT accepted → a red required check
|
|
1004
|
+
}
|
|
1005
|
+
// A failing required check outranks a pending one: it needs fix-ci now, not more waiting.
|
|
1006
|
+
if (anyBlocked) return "blocked";
|
|
1007
|
+
if (anyPending) return "waiting";
|
|
1008
|
+
return "pass";
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
export function classifyMergeability(s: PrState, protocol?: MergeProtocol): Mergeability {
|
|
1012
|
+
// Protocol-aware backstop FIRST (issue #392): honour the repo's declared `requiredChecks`
|
|
1013
|
+
// against the actual head rollup, so an `UNSTABLE` PR with a red DECLARED-required
|
|
1014
|
+
// check is no longer blindly `ready`. This never weakens GitHub branch protection (the switch
|
|
1015
|
+
// below still gates) — it only tightens merges on repos that under-specify their required checks.
|
|
1016
|
+
const gate = requiredChecksVerdict(s, protocol);
|
|
1017
|
+
if (gate === "blocked") return "blocked";
|
|
1018
|
+
if (gate === "waiting") return "waiting";
|
|
919
1019
|
switch (s.mergeStateStatus) {
|
|
920
1020
|
case "CLEAN":
|
|
921
1021
|
case "HAS_HOOKS":
|
|
922
|
-
case "UNSTABLE": // only non-required checks failing — still mergeable
|
|
1022
|
+
case "UNSTABLE": // only non-required checks failing — still mergeable (no DECLARED-required red)
|
|
923
1023
|
case "BEHIND": // out of date; a queue rebases, a direct merge is still allowed
|
|
924
1024
|
return "ready";
|
|
925
1025
|
case "DIRTY":
|
package/app/readiness.test.ts
CHANGED
|
@@ -414,6 +414,8 @@ function prObs(over: Partial<PrObservation> = {}): PrObservation {
|
|
|
414
414
|
failingCheckNames: [],
|
|
415
415
|
totalChecks: 0,
|
|
416
416
|
presentCheckNames: [],
|
|
417
|
+
pendingCheckNames: [],
|
|
418
|
+
checkConclusions: {},
|
|
417
419
|
isDraft: false,
|
|
418
420
|
headRefOid: "abc123",
|
|
419
421
|
mergedSha: null,
|
package/app/readiness.ts
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
// any credential is read at execution time from the typed env-contract (`credentialEnv` names a
|
|
19
19
|
// declared {@link EnvKey}; ADR 0004 pinned decision 2) and is redacted from every log line.
|
|
20
20
|
import { isEnvKey, readEnv, readEnvOr } from "./contracts.ts";
|
|
21
|
-
import { allCheckNames, classifyMergeability, failingCheckNames, type PrState, pendingCheckNames } from "./github.ts";
|
|
21
|
+
import { allCheckNames, checkConclusions, classifyMergeability, failingCheckNames, type PrState, pendingCheckNames } from "./github.ts";
|
|
22
22
|
import { isoDuration, isoDurationToMs } from "./reviewWait.ts";
|
|
23
23
|
|
|
24
24
|
/** The built-in readiness sources. `command` is the escape hatch that subsumes the long tail
|
|
@@ -565,6 +565,8 @@ export function parsePrView(payload: unknown): PrObservation {
|
|
|
565
565
|
failingCheckNames: names,
|
|
566
566
|
totalChecks: rollup.length,
|
|
567
567
|
presentCheckNames: allCheckNames(rollup),
|
|
568
|
+
pendingCheckNames: pending,
|
|
569
|
+
checkConclusions: checkConclusions(rollup),
|
|
568
570
|
isDraft: j.isDraft === true,
|
|
569
571
|
headRefOid: str(j.headRefOid).trim() || null,
|
|
570
572
|
mergedSha,
|
package/app/service.ts
CHANGED
|
@@ -1150,7 +1150,11 @@ export async function pollMerges(data: DataLayer, engine: EngineClient, token: s
|
|
|
1150
1150
|
// (#342/#350). Reuse the `st` we just read so we don't double-fetch. This is the proven terminal
|
|
1151
1151
|
// path the whole class (#368) now shares.
|
|
1152
1152
|
if (await advanceIfTerminalOutOfBand(data, engine, pr, token, st)) continue;
|
|
1153
|
-
|
|
1153
|
+
// Load the repo's merge protocol ONCE per PR iteration and pass it into the classifier so the
|
|
1154
|
+
// protocol-aware backstop (#392) can gate a red DECLARED-required check even when GitHub reports
|
|
1155
|
+
// the PR as UNSTABLE. The same handle is reused by the frugal-CI fresh-head-run branch below.
|
|
1156
|
+
const protocol = await loadMergeProtocol(repo, token).catch(() => null);
|
|
1157
|
+
const verdict = classifyMergeability(st, protocol ?? undefined);
|
|
1154
1158
|
if (verdict === "waiting") {
|
|
1155
1159
|
// Frugal-CI remedy (#43): when the repo publishes a merge protocol that wants a fresh
|
|
1156
1160
|
// head run and the PR has NO required head run yet, review has converged but the last push
|
|
@@ -1161,7 +1165,6 @@ export async function pollMerges(data: DataLayer, engine: EngineClient, token: s
|
|
|
1161
1165
|
// `pull_request` run once per head (mark ready / close+reopen); rebases change
|
|
1162
1166
|
// `headRefOid`, so downstream merge-train PRs get a new nudge after every post-rebase
|
|
1163
1167
|
// landing attempt.
|
|
1164
|
-
const protocol = await loadMergeProtocol(repo, token).catch(() => null);
|
|
1165
1168
|
if (protocol) {
|
|
1166
1169
|
const action = freshHeadRunAction(protocol, verdict, headRunPresenceCount(protocol, st), st.isDraft, {
|
|
1167
1170
|
headRefOid: st.headRefOid,
|