@feigi/fleet-ctl 0.0.0 → 3.16.3

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.
Files changed (153) hide show
  1. package/agents/{fleet-implementer.agent.md → fleet-implementer-slow-high.agent.md} +23 -2
  2. package/agents/fleet-implementer-slow-medium.agent.md +231 -0
  3. package/agents/fleet-implementer-smol-high.agent.md +231 -0
  4. package/agents/{fleet-implementer-alt.agent.md → fleet-implementer-task-high.agent.md} +23 -2
  5. package/agents/fleet-implementer-task-max.agent.md +231 -0
  6. package/agents/fleet-review-comments.agent.md +4 -2
  7. package/agents/fleet-review-correctness.agent.md +2 -1
  8. package/agents/fleet-review-runner.agent.md +14 -10
  9. package/agents/fleet-review-silent-failure.agent.md +4 -3
  10. package/agents/fleet-review-simplify.agent.md +2 -1
  11. package/agents/fleet-review-snapshot.agent.md +2 -1
  12. package/agents/fleet-review-test-run.agent.md +29 -0
  13. package/agents/fleet-review-tests.agent.md +10 -5
  14. package/agents/fleet-review-types.agent.md +4 -2
  15. package/agents/fleet-review-verifier.agent.md +4 -3
  16. package/commands/review-and-fix.md +7 -6
  17. package/commands/run-merge-bot.md +14 -4
  18. package/package.json +4 -2
  19. package/scripts/agent-test.test.mjs +36 -32
  20. package/scripts/ambient-git-vars-mjs-prose.test.mjs +23 -13
  21. package/scripts/apply-ruleset.test.mjs +4 -3
  22. package/scripts/arg-header-probes-prose.test.mjs +1 -0
  23. package/scripts/arg.test.mjs +18 -18
  24. package/scripts/board-cli.test.mjs +18 -21
  25. package/scripts/board-identity.test.mjs +9 -9
  26. package/scripts/board-liveness-banner.test.mjs +6 -6
  27. package/scripts/board-prev-shape.test.mjs +12 -13
  28. package/scripts/board-tryparse-null.test.mjs +7 -7
  29. package/scripts/board.mjs +4 -7
  30. package/scripts/board.test.mjs +57 -61
  31. package/scripts/candidates.test.mjs +228 -17
  32. package/scripts/ci-completes-premise-prose.test.mjs +1 -1
  33. package/scripts/ci-state-prose.test.mjs +2 -2
  34. package/scripts/ci-state.mjs +389 -187
  35. package/scripts/ci-state.test.mjs +458 -165
  36. package/scripts/citation-sweep-prose.test.mjs +59 -1
  37. package/scripts/claim-lifecycle.test.mjs +2 -1
  38. package/scripts/claim-ticket.test.mjs +35 -34
  39. package/scripts/closing-keyword-prose.test.mjs +1 -1
  40. package/scripts/cockpit-tick-nonblocking.test.mjs +8 -8
  41. package/scripts/compute-board.mjs +13 -3
  42. package/scripts/compute-board.test.mjs +64 -1
  43. package/scripts/compute-spend.mjs +46 -8
  44. package/scripts/compute-spend.test.mjs +64 -10
  45. package/scripts/cross-repo-citation-prose.test.mjs +18 -19
  46. package/scripts/delete-merged-branch.sh +218 -0
  47. package/scripts/delete-merged-branch.test.mjs +576 -0
  48. package/scripts/derive-testcmd.sh +18 -7
  49. package/scripts/derive-testcmd.test.mjs +9 -8
  50. package/scripts/diff-stats.mjs +2 -2
  51. package/scripts/diff-stats.test.mjs +7 -11
  52. package/scripts/dispatch-block-golden-prose.test.mjs +111 -6
  53. package/scripts/dispatch-block-pins-prose.test.mjs +5 -5
  54. package/scripts/dispositions-check.mjs +507 -0
  55. package/scripts/dispositions-check.test.mjs +843 -0
  56. package/scripts/dispositions-gate-prose.test.mjs +114 -0
  57. package/scripts/drop-merged-label.test.mjs +3 -4
  58. package/scripts/exec-stub.mjs +93 -0
  59. package/scripts/exec-stub.test.mjs +69 -0
  60. package/scripts/filing-bar-seat-prose.test.mjs +1 -1
  61. package/scripts/finisher-test-verdict-prose.test.mjs +102 -0
  62. package/scripts/fix-applier-correction-rules-prose.test.mjs +11 -10
  63. package/scripts/fleet-bootstrap +5 -20
  64. package/scripts/fleet-bootstrap.test.mjs +75 -0
  65. package/scripts/fleet-heartbeat.mjs +2 -2
  66. package/scripts/fleet-provenance +15 -24
  67. package/scripts/fleet-provenance.test.mjs +86 -12
  68. package/scripts/fleet-run.test.mjs +5 -5
  69. package/scripts/fleet-state.mjs +33 -11
  70. package/scripts/fleet-state.test.mjs +90 -3
  71. package/scripts/fleet-tick.mjs +346 -70
  72. package/scripts/fleet-tick.test.mjs +566 -16
  73. package/scripts/force-with-lease-reason-prose.test.mjs +1 -1
  74. package/scripts/frontmatter-check.mjs +3 -2
  75. package/scripts/frontmatter-check.test.mjs +6 -6
  76. package/scripts/immutable-body-claim-prose.test.mjs +53 -262
  77. package/scripts/implementer-model-tier.test.mjs +98 -506
  78. package/scripts/inflight.test.mjs +18 -127
  79. package/scripts/is-cli.mjs +26 -0
  80. package/scripts/is-cli.test.mjs +99 -0
  81. package/scripts/json.test.mjs +3 -3
  82. package/scripts/ledger-dispatch.test.mjs +368 -7
  83. package/scripts/ledger-grammar.mjs +85 -1
  84. package/scripts/ledger-grammar.test.mjs +86 -1
  85. package/scripts/ledger-read-require-file.test.mjs +7 -7
  86. package/scripts/ledger-rotate.test.mjs +142 -0
  87. package/scripts/ledger.mjs +181 -17
  88. package/scripts/ledger.test.mjs +12 -130
  89. package/scripts/main-checkout.mjs +312 -0
  90. package/scripts/main-checkout.test.mjs +332 -0
  91. package/scripts/member-outcomes-header.test.mjs +3 -3
  92. package/scripts/member-outcomes.mjs +4 -5
  93. package/scripts/member-outcomes.test.mjs +92 -18
  94. package/scripts/member-prompt-prose.test.mjs +17 -13
  95. package/scripts/member-record.mjs +8 -3
  96. package/scripts/member-record.test.mjs +5 -5
  97. package/scripts/member-write-guard.mjs +5 -5
  98. package/scripts/member-write-guard.test.mjs +6 -3
  99. package/scripts/no-undo-audit.sh +24 -21
  100. package/scripts/no-undo-audit.test.mjs +9 -13
  101. package/scripts/pointer-target-prose.test.mjs +1 -1
  102. package/scripts/pr-overlap.test.mjs +6 -9
  103. package/scripts/prompt-renderer.mjs +10 -9
  104. package/scripts/prose-pin.mjs +10 -11
  105. package/scripts/prove-merge.test.mjs +3 -3
  106. package/scripts/quiet-payload-prose.test.mjs +1 -1
  107. package/scripts/reap.test.mjs +3 -4
  108. package/scripts/release-label-split-prose.test.mjs +17 -2
  109. package/scripts/release-ticket.test.mjs +35 -44
  110. package/scripts/review-core-cwd-isolation.test.mjs +7 -6
  111. package/scripts/review-core-reads.test.mjs +1 -1
  112. package/scripts/review-core-scratch-refusal.test.mjs +64 -0
  113. package/scripts/review-core-shared-test-run.test.mjs +238 -0
  114. package/scripts/review-core-snapshot-path.test.mjs +59 -12
  115. package/scripts/review-core-specialist-scratch.test.mjs +12 -10
  116. package/scripts/review-core-testcmd.test.mjs +76 -45
  117. package/scripts/review-core-unrun.test.mjs +154 -168
  118. package/scripts/review-core.mjs +250 -56
  119. package/scripts/review-eval.mjs +11 -4
  120. package/scripts/review-host-fixture.mjs +12 -3
  121. package/scripts/review-in-run-retry.test.mjs +27 -3
  122. package/scripts/review-runner-spawns.test.mjs +5 -3
  123. package/scripts/review-runner.test.mjs +14 -3
  124. package/scripts/review-tier-audit.test.mjs +1 -1
  125. package/scripts/run-merge-bot-prose.test.mjs +29 -6
  126. package/scripts/scratch-partition-invariant-prose.test.mjs +1 -1
  127. package/scripts/section-between-guard-prose.test.mjs +4 -3
  128. package/scripts/select-dimensions.test.mjs +1 -1
  129. package/scripts/shell-traps-prose.test.mjs +6 -9
  130. package/scripts/shortlist.mjs +4 -1
  131. package/scripts/shortlist.test.mjs +30 -16
  132. package/scripts/snapshot-repo.test.mjs +177 -4
  133. package/scripts/stash-prohibition-prose.test.mjs +1 -1
  134. package/scripts/temp-dir.mjs +47 -0
  135. package/scripts/tier-check.mjs +95 -28
  136. package/scripts/tier-check.test.mjs +131 -29
  137. package/scripts/tier-outcomes-header.test.mjs +11 -54
  138. package/scripts/tier-outcomes.mjs +34 -12
  139. package/scripts/tier-outcomes.test.mjs +157 -4
  140. package/scripts/tier-roles.mjs +118 -29
  141. package/scripts/tier-roles.test.mjs +226 -61
  142. package/scripts/tracker-block-copy-prose.test.mjs +3 -3
  143. package/scripts/verify-sha.test.mjs +4 -5
  144. package/scripts/watcher-degraded-block.test.mjs +5 -8
  145. package/scripts/within-run-pair-prose.test.mjs +43 -75
  146. package/scripts/worktree-audit.test.mjs +2 -1
  147. package/skills/next-ticket/SKILL.md +2 -2
  148. package/skills/run-team/SKILL.md +433 -303
  149. package/skills/run-team/references/ci-and-staleness.md +7 -6
  150. package/skills/run-team/references/correction-tickets.md +3 -3
  151. package/skills/run-team/references/isolation.md +1 -1
  152. package/skills/sizing-a-ticket/SKILL.md +8 -8
  153. package/scripts/tier-guard-gate-prose.test.mjs +0 -216
@@ -7,12 +7,13 @@
7
7
  //
8
8
  // `gh` is stubbed on PATH and logs every call it receives, so a test can
9
9
  // assert `run list`/`run view` were never reached under no-ci — the point of
10
- // skipping them (#262's REST budget) is unverifiable without that log. `git`
11
- // is real, and the repo fixture IS `git init`-ed: discovery anchors itself to
12
- // `git rev-parse --show-toplevel`, so a non-repo fixture would exit 2 before
13
- // reaching any of this. No `origin` is added, so `git remote get-url origin`
14
- // still fails on its own and the behind-count block degrades to `null`,
15
- // exactly the path it already has a contract for.
10
+ // skipping them (#262's REST budget) is unverifiable without that log. The
11
+ // workflow trees are served by that stub too, as the GraphQL reply the
12
+ // script reads them from at the PR's head and base commits: `repoFiles`
13
+ // paths under `.github/workflows/` become both trees unless `headFiles` or
14
+ // `baseFiles` says otherwise, and nothing is read off the fixture's disk.
15
+ // The repository and its host come from the stub's `gh repo view` reply, as
16
+ // the script takes them from gh's own resolution rather than from git.
16
17
 
17
18
  import { test } from "node:test";
18
19
  import assert from "node:assert/strict";
@@ -22,6 +23,7 @@ import { tmpdir } from "node:os";
22
23
  import { join } from "node:path";
23
24
  import { fileURLToPath } from "node:url";
24
25
  import { stripComments } from "./strip-comments.mjs";
26
+ import { writeExecStub } from "./exec-stub.mjs";
25
27
 
26
28
  const SCRIPT = fileURLToPath(new URL("./ci-state.mjs", import.meta.url));
27
29
 
@@ -29,11 +31,24 @@ const SCRIPT = fileURLToPath(new URL("./ci-state.mjs", import.meta.url));
29
31
  // caller can act on lives only in that message. `$GH_FAIL_MSG` is how a test
30
32
  // picks which cause this stub refuses with; empty — the default every fixture
31
33
  // above already relies on — keeps the bare `exit 1` with a silent stderr.
34
+ //
35
+ // Two more behaviours of the real gh are reproduced because the script
36
+ // depends on them. A GraphQL reply carrying `errors` is printed on stdout all
37
+ // the same and exits 1 (measured: a `file(path:)` on a missing path). And gh
38
+ // resolves the repository by running git, so an ambient GIT_DIR or
39
+ // GIT_WORK_TREE reaching it would answer for another repository — the stub
40
+ // logs either one it receives, so a test can assert none ever does.
32
41
  const GH_STUB = `#!/bin/sh
42
+ [ -n "\${GIT_DIR+x}\${GIT_WORK_TREE+x}" ] && echo "ambient GIT_DIR=\${GIT_DIR-} GIT_WORK_TREE=\${GIT_WORK_TREE-}" >> "$GH_LOG"
33
43
  echo "$*" >> "$GH_LOG"
34
44
  fail() { [ -n "$GH_FAIL_MSG" ] && echo "$GH_FAIL_MSG" >&2; exit 1; }
35
45
  case "$1 $2" in
36
- "pr view") [ -f "$PR_VIEW_FILE" ] && cat "$PR_VIEW_FILE" || fail ;;
46
+ "api graphql") case "$*" in *baseExpr=*) f="$BASE_READ_FILE" ;; *) f="$PR_READ_FILE" ;; esac
47
+ [ -f "$f" ] || fail
48
+ cat "$f"
49
+ if grep -q '"errors":' "$f"; then echo "gh: Could not resolve file for path" >&2; exit 1; fi ;;
50
+ "api repos/{owner}/{repo}/rules/branches/"*) [ -f "$RULES_FILE" ] && cat "$RULES_FILE" || fail ;;
51
+ "api repos/{owner}/{repo}/branches/"*) [ -f "$PROTECTION_FILE" ] && cat "$PROTECTION_FILE" || fail ;;
37
52
  "run list") [ -f "$RUN_LIST_FILE" ] && cat "$RUN_LIST_FILE" || fail ;;
38
53
  "run view") [ -f "$RUN_VIEW_FILE" ] && cat "$RUN_VIEW_FILE" || fail ;;
39
54
  "repo view") [ -f "$REPO_VIEW_FILE" ] && cat "$REPO_VIEW_FILE" || fail ;;
@@ -42,10 +57,13 @@ esac
42
57
  `;
43
58
 
44
59
  const PR_HEAD = "abc123def";
60
+ const BASE_OID = "bbb000bbb";
45
61
  const BRANCH = "fix/1";
46
62
  const PR_VIEW = JSON.stringify({
47
63
  headRefName: BRANCH,
48
64
  headRefOid: PR_HEAD,
65
+ baseRefName: "main",
66
+ baseRefOid: BASE_OID,
49
67
  state: "OPEN",
50
68
  mergeStateStatus: "CLEAN",
51
69
  });
@@ -59,41 +77,96 @@ const RUN_VIEW = JSON.stringify({
59
77
  conclusion: "success",
60
78
  headSha: PR_HEAD,
61
79
  });
80
+ const REPO_VIEW = JSON.stringify({ nameWithOwner: "acme/repo", url: "https://github.com/acme/repo" });
81
+
82
+ const WF = ".github/workflows";
83
+ const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
84
+
85
+ // The GraphQL tree for `.github/workflows` at one commit, built from a
86
+ // `{ path: content }` map: null when no path lies under it (the directory
87
+ // does not exist there). A content that is an object stands for a blob the
88
+ // API could not hand back whole — `{ text: null }` (binary or absent) or
89
+ // `{ truncated: true }` — and `{ dir: true }` for a subdirectory entry.
90
+ function treeOf(files) {
91
+ const entries = Object.entries(files)
92
+ .filter(([p]) => p.startsWith(`${WF}/`))
93
+ .map(([p, c]) => {
94
+ const name = p.slice(WF.length + 1);
95
+ if (isObj(c) && c.dir) return { name, type: "tree", object: { __typename: "Tree" } };
96
+ if (isObj(c)) return { name, type: "blob", object: { __typename: "Blob", text: c.truncated ? "name: CI\n" : (c.text ?? null), isTruncated: !!c.truncated } };
97
+ return { name, type: "blob", object: { __typename: "Blob", text: c, isTruncated: false } };
98
+ });
99
+ return entries.length ? { __typename: "Tree", entries } : null;
100
+ }
62
101
 
63
- // repoFiles: { "relative/path": "content" }, written under a fresh cwd.
64
- // unreadable: repo-relative files OR directories chmod'ed 0o000 for the run and
65
- // restored after, so a permission probe cannot leave an undeletable tmpdir.
66
- // cwd: repo-relative directory to run from, for the repo-root anchoring test.
67
- // git: `false` leaves the fixture outside any repo, for the routes that have to
68
- // answer without a repo root.
102
+ // One commit's `file(path: ".github/workflows")` entry around such a tree, and
103
+ // the error GitHub sends beside the null where the path does not exist.
104
+ const entryOf = (tree) => (tree === null ? null : { type: "tree", object: tree });
105
+ const notFoundAt = (...path) => ({ type: "NOT_FOUND", path, message: `Could not resolve file for path '${WF}'.` });
106
+ const withErrors = (data, errors) => JSON.stringify(errors.length ? { data, errors } : { data });
107
+
108
+ // The required-checks replies: `rules` is the contexts every ruleset requires,
109
+ // `classic` the classic branch protection's own list.
110
+ const rulesReply = (contexts) =>
111
+ JSON.stringify([[{ type: "deletion" }, { type: "required_status_checks", parameters: { required_status_checks: contexts.map((context) => ({ context, integration_id: 15368 })) } }]]);
112
+ const protectionReply = (contexts) =>
113
+ JSON.stringify({ name: "main", protected: contexts.length > 0, protection: { enabled: contexts.length > 0, required_status_checks: { enforcement_level: contexts.length ? "non_admins" : "off", contexts, checks: [] } } });
114
+
115
+ // repoFiles: { "relative/path": "content" } — the workflow files at BOTH
116
+ // commits unless headFiles/baseFiles replaces one side. A side with none gets
117
+ // a null `file` entry plus GitHub's NOT_FOUND for it, as the real API sends.
118
+ // baseEntryRaw: the base's `file` entry verbatim, with no error beside it.
119
+ // baseTip: the oid the base branch's tip reports; anything but BASE_OID sends
120
+ // the script to its second, oid-bound base read, which answers baseReadOid.
121
+ // headOid: what `refs/pull/N/head` resolves to.
122
+ // prErrors: extra entries for the first GraphQL reply's `errors`.
123
+ // rules/classic: required contexts (ruleset / classic protection); pass
124
+ // rulesRaw/protectionRaw to serve a reply verbatim, `null` to make it fail.
125
+ // cwd: repo-relative directory to run from.
69
126
  // pr: the `--pr` value, defaulting to the digits every other fixture wants;
70
127
  // `null` omits the flag entirely, for the tests that probe how the argument
71
128
  // itself is refused rather than what it selects.
72
129
  // gh responses default to the green fixtures above; pass `null` to make that gh
73
130
  // subcommand fail (exit 1) if reached, so an unexpected call surfaces as a
74
- // crash rather than silently serving the wrong fixture.
75
- function run(args, { repoFiles = {}, unreadable = [], cwd = ".", pr = "42", prView = PR_VIEW, runList = RUN_LIST, runView = RUN_VIEW, ghFailMsg = "", tolerateUnparsedStdout = false, readOnlyStdout = false, nonBlockingStdout = false, git = true, origin = null, repoView = null, spawnEnv = {} } = {}) {
131
+ // crash rather than silently serving the wrong fixture. `prView` is the
132
+ // pullRequest object inside the first GraphQL reply; null fails that read.
133
+ // `repoView` is gh's own resolution of the cwd's repository, which is where
134
+ // the script takes the host from; `remotes` ({ name: url }) are added to the
135
+ // fixture's git repository, which the script must never read a host from.
136
+ function run(args, { repoFiles = {}, headFiles = null, baseFiles = null, tipFiles = null, baseEntryRaw, baseTip = BASE_OID, baseReadOid = BASE_OID, headOid = PR_HEAD, prErrors = [], rules = [], classic = [], rulesRaw, protectionRaw, cwd = ".", localFiles = {}, pr = "42", prView = PR_VIEW, runList = RUN_LIST, runView = RUN_VIEW, ghFailMsg = "", tolerateUnparsedStdout = false, readOnlyStdout = false, nonBlockingStdout = false, repoView = REPO_VIEW, remotes = {}, spawnEnv = {} } = {}) {
76
137
  const repoDir = mkdtempSync(join(tmpdir(), "ci-state-repo-"));
77
- // Discovery resolves `.github/workflows` off `git rev-parse --show-toplevel`,
78
- // never the cwd, so a fixture that reaches discovery has to be a real repo.
79
- // No remote is added by default: the behind-count block still degrades to
80
- // null as before. `origin` opts a fixture in, for the behind-count block's
81
- // own tests.
82
- if (git) spawnSync("git", ["init", "-q", repoDir], { stdio: "ignore" });
83
- if (origin !== null) spawnSync("git", ["remote", "add", "origin", origin], { cwd: repoDir, stdio: "ignore" });
138
+ spawnSync("git", ["init", "-q", repoDir], { stdio: "ignore" });
139
+ for (const [name, url] of Object.entries(remotes)) spawnSync("git", ["remote", "add", name, url], { cwd: repoDir, stdio: "ignore" });
84
140
  const binDir = mkdtempSync(join(tmpdir(), "ci-state-bin-"));
85
- for (const [rel, content] of Object.entries(repoFiles)) {
141
+ // localFiles: written into the caller's working tree, which the script must
142
+ // never read — the cwd-independence tests put a DIFFERENT workflow here.
143
+ for (const [rel, content] of Object.entries(localFiles)) {
86
144
  const full = join(repoDir, rel);
87
145
  mkdirSync(join(full, ".."), { recursive: true });
88
146
  writeFileSync(full, content);
89
147
  }
90
148
  mkdirSync(join(repoDir, cwd), { recursive: true });
91
149
  const gh = join(binDir, "gh");
92
- writeFileSync(gh, GH_STUB);
93
- chmodSync(gh, 0o755);
150
+ writeExecStub(gh, GH_STUB);
94
151
  const ghLog = join(binDir, "gh.log");
95
152
  writeFileSync(ghLog, "");
96
153
 
154
+ const baseEntry = baseEntryRaw !== undefined ? baseEntryRaw : entryOf(treeOf(baseFiles ?? repoFiles));
155
+ const baseAbsent = baseEntryRaw === undefined && baseEntry === null;
156
+ const tipEntry = tipFiles === null ? baseEntry : entryOf(treeOf(tipFiles));
157
+ const tipAbsent = tipFiles === null ? baseAbsent : tipEntry === null;
158
+ const headEntry = entryOf(treeOf(headFiles ?? repoFiles));
159
+ let firstReply = null;
160
+ if (prView !== null) {
161
+ const pull = JSON.parse(prView);
162
+ const errors = [...prErrors];
163
+ if (isObj(pull) && !Object.hasOwn(pull, "baseRef")) {
164
+ pull.baseRef = { target: { oid: baseTip, file: tipEntry } };
165
+ if (tipAbsent) errors.push(notFoundAt("repository", "pullRequest", "baseRef", "target", "file"));
166
+ }
167
+ if (headEntry === null) errors.push(notFoundAt("repository", "head", "file"));
168
+ firstReply = withErrors({ repository: { pullRequest: pull, head: { oid: headOid, file: headEntry } } }, errors);
169
+ }
97
170
  const fixtureFile = (name, content) => {
98
171
  if (content === null) return "";
99
172
  const p = join(binDir, name);
@@ -105,21 +178,18 @@ function run(args, { repoFiles = {}, unreadable = [], cwd = ".", pr = "42", prVi
105
178
  PATH: `${binDir}:${process.env.PATH}`,
106
179
  GH_LOG: ghLog,
107
180
  GH_FAIL_MSG: ghFailMsg,
108
- PR_VIEW_FILE: fixtureFile("pr-view.json", prView),
181
+ PR_READ_FILE: fixtureFile("pr-read.json", firstReply),
182
+ BASE_READ_FILE: fixtureFile("base-read.json", withErrors({ repository: { base: { oid: baseReadOid, file: baseEntry } } }, baseAbsent ? [notFoundAt("repository", "base", "file")] : [])),
183
+ RULES_FILE: fixtureFile("rules.json", rulesRaw === undefined ? rulesReply(rules) : rulesRaw),
184
+ PROTECTION_FILE: fixtureFile("protection.json", protectionRaw === undefined ? protectionReply(classic) : protectionRaw),
109
185
  RUN_LIST_FILE: fixtureFile("run-list.json", runList),
110
186
  RUN_VIEW_FILE: fixtureFile("run-view.json", runView),
111
187
  REPO_VIEW_FILE: fixtureFile("repo-view.json", repoView),
112
- // Last, so a test can deliberately put back a var #1599's scrub in
113
- // tryRun() removes — that is the whole point of the ambient GIT_DIR/
114
- // GIT_WORK_TREE tests below.
188
+ // Last, so a test can deliberately put back a var spawn()'s gitEnv()
189
+ // scrub removes — that is the whole point of the ambient GIT_DIR test
190
+ // below.
115
191
  ...spawnEnv,
116
192
  };
117
- const restore = [];
118
- for (const rel of unreadable) {
119
- const full = join(repoDir, rel);
120
- restore.push([full, statSync(full).mode & 0o777]);
121
- chmodSync(full, 0o000);
122
- }
123
193
  // readOnlyStdout: hand the child a stdout it cannot write to, so its first
124
194
  // write fails with EBADF rather than by racing a reader. CLOSING fd 1 does
125
195
  // not do it — libuv reopens a closed standard fd onto /dev/null and the write
@@ -147,7 +217,6 @@ function run(args, { repoFiles = {}, unreadable = [], cwd = ".", pr = "42", prVi
147
217
  });
148
218
  } finally {
149
219
  if (roStdout !== null) closeSync(roStdout);
150
- for (const [full, mode] of restore.reverse()) chmodSync(full, mode);
151
220
  }
152
221
  const log = readFileSync(ghLog, "utf8");
153
222
  // Parsing eagerly is what lets every other test assert straight off `payload`,
@@ -225,59 +294,86 @@ test("two workflow files share the target name: ambiguous, dies (exit 2) rather
225
294
  assert.match(r.stderr, /pass --workflow-file to pick one/);
226
295
  });
227
296
 
228
- // Both routes to an unreadable workflow file refuse with exit 2, and they
229
- // refuse at different depths: discovery opens each candidate as it scans, while
230
- // an explicit --workflow-file target is not read until expectedJobs(). Kept as
231
- // two cases because that difference is the point — a fix that closes only the
232
- // scan leaves the explicit route reading the file as absent, and absent is the
233
- // one shape that can be declared away as no-ci.
234
- //
235
- // The explicit route also runs OUTSIDE a repo — the second discrimination these
236
- // two cases carry. Naming the file answers the question without a repo root, so
237
- // a root lookup made eager again refuses a caller who never asked for
238
- // discovery, and nothing else in this file runs the script from a non-repo cwd.
239
- // The refusal is asserted down to its errno because `cannot read` alone is also
240
- // what a path that never existed produces: drift between the argument and the
241
- // fixture would leave this case pinning absence, the shape above.
242
- for (const [route, args, opts] of [
243
- ["the discovery scan", [], {}],
244
- ["an explicit --workflow-file", ["--workflow-file", ".github/workflows/ci.yml"], { git: false }],
297
+ // Both routes to a workflow blob the API could not hand back whole refuse with
298
+ // exit 2: discovery has to read every YAML blob to learn which one carries the
299
+ // workflow's name, and an explicit --workflow-file names its target outright.
300
+ // A text that is absent (binary) and one that is truncated are both cases: a
301
+ // truncated text is not a shorter workflow, and parsing it would derive a job
302
+ // set from half a file.
303
+ for (const [route, args] of [
304
+ ["the discovery scan", []],
305
+ ["an explicit --workflow-file", ["--workflow-file", ".github/workflows/ci.yml"]],
245
306
  ]) {
246
- test(`an unreadable workflow file reached through ${route}: exit 2, never no-ci`, (t) => {
247
- if (process.getuid?.() === 0) return t.skip("root reads every file");
248
- const r = run(args, {
249
- repoFiles: { ".github/workflows/ci.yml": CI_WORKFLOW },
250
- unreadable: [".github/workflows/ci.yml"],
251
- ...opts,
307
+ for (const [what, blob] of [["absent", { text: null }], ["truncated", { truncated: true }]]) {
308
+ test(`a workflow blob whose text came back ${what}, reached through ${route}: exit 2, never no-ci`, () => {
309
+ for (const extra of [[], ["--declare-no-ci"]]) {
310
+ const r = run([...args, ...extra], { repoFiles: { ".github/workflows/ci.yml": blob } });
311
+ assert.equal(r.status, 2, r.stdout + r.stderr);
312
+ assert.match(r.stderr, /cannot read \.github\/workflows\/ci\.yml at the base commit/);
313
+ assert.equal(r.payload, null);
314
+ }
252
315
  });
253
- assert.equal(r.status, 2, r.stdout + r.stderr);
254
- assert.match(r.stderr, /cannot read [^\n]*: EACCES: permission denied/);
255
- });
316
+ }
256
317
  }
257
318
 
258
319
  // --- Error policy (#111): only a genuinely absent workflow set is `no-ci` ---
259
- // The verdict must be reachable one way only: `.github/workflows/` absent, or
260
- // present and holding no workflow files. Every other outcome — the directory
261
- // unreadable, the target unreadable, files present under other names — is exit
262
- // 2, "the question could not be answered", and `--declare-no-ci` must not
263
- // convert any of them to exit 0. The four tests below pin one branch each,
264
- // because the first review of this file reached merge with two of them wrong.
320
+ // The verdict must be reachable one way only: `.github/workflows/` absent at
321
+ // the base, or present and holding no workflow files. Every other outcome —
322
+ // the path not a directory, a blob not read whole, files present under other
323
+ // names — is exit 2, "the question could not be answered", and
324
+ // `--declare-no-ci` must not convert any of them to exit 0.
265
325
 
266
326
  const CI_WORKFLOW_COMMENTED = CI_WORKFLOW.replace("name: CI", `name: "CI" # main pipeline`);
267
327
 
268
- test("unreadable .github/workflows directory: exit 2, never no-ci — --declare-no-ci cannot let it through", (t) => {
269
- if (process.getuid?.() === 0) return t.skip("root reads every directory");
270
- for (const args of [[], ["--declare-no-ci"]]) {
271
- // Same repo, same real CI: only the directory's mode differs. Reading this
272
- // as no-ci reported "no CI configured" for a repo whose run was `failure`.
273
- const r = run(args, {
274
- repoFiles: { ".github/workflows/ci.yml": CI_WORKFLOW },
275
- unreadable: [".github/workflows"],
276
- });
328
+ // Absence is what GitHub says NOT_FOUND about; anything else standing at the
329
+ // path is not it. A submodule there reads back with a null `object` exactly
330
+ // as a missing path does, so only the entry's `type` tells them apart.
331
+ for (const [what, entry] of [
332
+ ["a file", { type: "blob", object: { __typename: "Blob", text: "x", isTruncated: false } }],
333
+ ["a submodule", { type: "commit", object: null }],
334
+ ["a null entry GitHub sent no NOT_FOUND for", null],
335
+ ]) {
336
+ test(`\`.github/workflows\` at the base that is ${what}: exit 2, never no-ci — --declare-no-ci cannot let it through`, () => {
337
+ for (const args of [[], ["--declare-no-ci"]]) {
338
+ const r = run(args, { repoFiles: { ".github/workflows/ci.yml": CI_WORKFLOW }, baseEntryRaw: entry });
339
+ assert.equal(r.status, 2, r.stdout + r.stderr);
340
+ assert.match(r.stderr, /\.github\/workflows at the base commit is not a readable directory/);
341
+ assert.equal(r.payload, null);
342
+ }
343
+ });
344
+ }
345
+
346
+ // gh exits 1 on a reply carrying a NOT_FOUND for a missing path, so the
347
+ // absence a no-ci verdict rests on arrives as a failed read — on either base
348
+ // read, the tip's or the oid-bound second one.
349
+ for (const [route, opts] of [
350
+ ["the base tip's", {}],
351
+ ["the oid-bound second", { baseTip: "ccc111ccc", tipFiles: { ".github/workflows/ci.yml": CI_WORKFLOW } }],
352
+ ]) {
353
+ test(`no workflows directory at the base, as ${route} read reports it (NOT_FOUND, gh exit 1): no-ci`, () => {
354
+ const r = run([], { ...opts, baseFiles: {}, headFiles: { ".github/workflows/ci.yml": CI_WORKFLOW } });
355
+ assert.equal(r.status, 1, r.stdout + r.stderr);
356
+ assert.equal(r.payload.verdict, "no-ci");
357
+ });
358
+ }
359
+
360
+ for (const [what, err] of [
361
+ ["a NOT_FOUND somewhere other than a workflow directory", { type: "NOT_FOUND", path: ["repository", "pullRequest"] }],
362
+ ["another error on the head's workflow directory", { type: "FORBIDDEN", path: ["repository", "head", "file"] }],
363
+ ["an error with no path", { type: "NOT_FOUND" }],
364
+ ]) {
365
+ test(`a GraphQL reply carrying ${what} fails the read (exit 2), whatever data came with it`, () => {
366
+ const r = run([], { repoFiles: { ".github/workflows/ci.yml": CI_WORKFLOW }, prErrors: [err] });
277
367
  assert.equal(r.status, 2, r.stdout + r.stderr);
278
- assert.match(r.stderr, /cannot read/);
368
+ assert.match(r.stderr, /ci-state: gh failed: exit 1/);
279
369
  assert.equal(r.payload, null);
280
- }
370
+ });
371
+ }
372
+
373
+ test("a workflows directory holding no YAML at the base is a genuine no-ci", () => {
374
+ const r = run([], { repoFiles: { ".github/workflows/README.md": "nothing here" } });
375
+ assert.equal(r.status, 1, r.stdout + r.stderr);
376
+ assert.equal(r.payload.verdict, "no-ci");
281
377
  });
282
378
 
283
379
  test("workflow name with a trailing YAML comment (and quotes) still matches — a configured repo never reads as no-ci", () => {
@@ -303,82 +399,113 @@ test("workflow files present but none named CI: exit 2 naming them, never a decl
303
399
  }
304
400
  });
305
401
 
306
- test("an unreadable irrelevant sibling does not blind discovery to a readable target", (t) => {
307
- if (process.getuid?.() === 0) return t.skip("root reads every file");
402
+ // Discovery cannot skip a YAML blob it could not read: until its text is
403
+ // read, nothing says it is not a second file carrying the workflow's name.
404
+ test("an unreadable sibling YAML blob refuses rather than being skipped — its name cannot be settled", () => {
308
405
  const r = run([], {
309
406
  repoFiles: {
310
407
  ".github/workflows/ci.yml": CI_WORKFLOW,
311
- ".github/workflows/zz-other.yml": OTHER_WORKFLOW,
408
+ ".github/workflows/zz-other.yml": { truncated: true },
312
409
  },
313
- unreadable: [".github/workflows/zz-other.yml"],
314
410
  });
315
- assert.equal(r.status, 0, r.stdout + r.stderr);
316
- assert.equal(r.payload.verdict, "green");
411
+ assert.equal(r.status, 2, r.stdout + r.stderr);
412
+ assert.match(r.stderr, /cannot read \.github\/workflows\/zz-other\.yml/);
317
413
  });
318
414
 
319
- test("discovery is anchored to the repo root, not the cwd — a subdirectory answers the same", () => {
415
+ // A non-YAML entry and a subdirectory are neither workflows nor unreadable
416
+ // workflows: they must not refuse a repo whose CI workflow reads fine.
417
+ test("a non-YAML file or a subdirectory in the workflows directory does not disturb discovery", () => {
320
418
  const r = run([], {
321
- repoFiles: { ".github/workflows/ci.yml": CI_WORKFLOW },
322
- cwd: "scripts",
419
+ repoFiles: {
420
+ ".github/workflows/ci.yml": CI_WORKFLOW,
421
+ ".github/workflows/README.md": { text: null },
422
+ ".github/workflows/shared.yml": { dir: true },
423
+ },
323
424
  });
324
425
  assert.equal(r.status, 0, r.stdout + r.stderr);
325
426
  assert.equal(r.payload.verdict, "green");
326
427
  });
327
428
 
328
- // #1599: tryRun()'s single spawn primitive (used for both this script's git
329
- // AND gh calls) passed no env at all before this fix, and #1020's own census
330
- // could not see either of its two git call sites — its scan is `.sh`-only.
331
- // Measured directly: an ambient GIT_DIR or GIT_WORK_TREE (a git hook,
332
- // `rebase --exec`, `bisect run`) corrupts each call in its own distinct way,
333
- // silently, at exit 0.
334
- test("workflowsPath()'s rev-parse: an inherited GIT_WORK_TREE must not substitute a foreign toplevel for the caller's own repo root", () => {
335
- const other = mkdtempSync(join(tmpdir(), "ci-state-other-"));
336
- spawnSync("git", ["init", "-q", other], { stdio: "ignore" });
337
- try {
429
+ // The verdict is a property of the PR and its run, not of whoever asks. The
430
+ // caller's working tree used to supply the expected set, so a `main` checkout
431
+ // and the PR's own worktree read the same run differently. Here the working
432
+ // tree carries a CI workflow with a job the run never had, and the answer from
433
+ // it, from a subdirectory, and from an empty tree must be identical.
434
+ test("the same PR and run give the same verdict whatever the caller's working tree holds", () => {
435
+ const repoFiles = { ".github/workflows/ci.yml": CI_WORKFLOW };
436
+ const stale = { ".github/workflows/ci.yml": `${CI_WORKFLOW} stale-job:\n runs-on: ubuntu-latest\n steps:\n - run: echo hi\n` };
437
+ const answers = [
438
+ run([], { repoFiles }),
439
+ run([], { repoFiles, localFiles: stale }),
440
+ run([], { repoFiles, localFiles: stale, cwd: "scripts" }),
441
+ ];
442
+ for (const r of answers) assert.equal(r.status, 0, r.stdout + r.stderr);
443
+ assert.deepEqual(answers[1].payload, answers[0].payload);
444
+ assert.deepEqual(answers[2].payload, answers[0].payload);
445
+ });
446
+
447
+ test("a cwd gh resolves no repository from refuses (exit 2) before any other read", () => {
448
+ const r = run([], { repoFiles: { ".github/workflows/ci.yml": CI_WORKFLOW }, repoView: null });
449
+ assert.equal(r.status, 2, r.stdout + r.stderr);
450
+ assert.match(r.stderr, /no GitHub repository resolves from this cwd's git remotes/);
451
+ assert.equal(r.log, "repo view --json nameWithOwner,url\n", "nothing may be read for a repository that was never resolved");
452
+ });
453
+
454
+ for (const [what, view, said] of [
455
+ ["a url with no host", { nameWithOwner: "acme/repo", url: "not a url" }, /url carries no host \(got "not a url"\)/],
456
+ ["no nameWithOwner", { url: "https://github.com/acme/repo" }, /nameWithOwner is not owner\/name \(the key is absent\)/],
457
+ ]) {
458
+ test(`gh's repository resolution answering with ${what} refuses (exit 2) before any other read`, () => {
459
+ const r = run([], { repoFiles: { ".github/workflows/ci.yml": CI_WORKFLOW }, repoView: JSON.stringify(view) });
460
+ assert.equal(r.status, 2, r.stdout + r.stderr);
461
+ assert.match(r.stderr, said);
462
+ assert.equal(r.log, "repo view --json nameWithOwner,url\n");
463
+ });
464
+ }
465
+
466
+ // The host is gh's resolution of the repository, never a parse of a remote
467
+ // URL: credentials in the URL, an ssh host alias and a remote not named
468
+ // `origin` each made that parse name a host gh could not reach, or none.
469
+ // Every `gh api` read names it — the rules and protection reads included,
470
+ // which only a dropped-and-absent job reaches.
471
+ const GHE_VIEW = JSON.stringify({ nameWithOwner: "acme/repo", url: "https://ghe-real.example/acme/repo" });
472
+ for (const [what, remotes] of [
473
+ ["an origin URL carrying credentials", { origin: "https://x-access-token:tok@ghe-real.example/acme/repo.git" }],
474
+ ["an origin through an ssh host alias", { origin: "git@ghe-work:acme/repo.git" }],
475
+ ["no remote named origin", { upstream: "ssh://git@ghe-real.example/acme/repo.git" }],
476
+ ]) {
477
+ test(`${what}: every gh api read names the host gh resolved`, () => {
338
478
  const r = run([], {
339
- repoFiles: { ".github/workflows/ci.yml": CI_WORKFLOW },
340
- // A subdirectory, the realistic shape: workflowsPath() never receives
341
- // a repo root as an argument, it derives one from wherever the process
342
- // happens to be running — measured, unscrubbed, an ambient
343
- // GIT_WORK_TREE answers `--show-toplevel` with the AMBIENT path
344
- // outright regardless of the real cwd, which then sends discovery
345
- // looking for `.github/workflows` in a directory that is not this
346
- // repository at all.
347
- cwd: "scripts",
348
- spawnEnv: { GIT_WORK_TREE: other },
479
+ remotes,
480
+ repoView: GHE_VIEW,
481
+ baseFiles: { ".github/workflows/ci.yml": TWO_JOB_WORKFLOW },
482
+ headFiles: { ".github/workflows/ci.yml": CI_WORKFLOW },
483
+ classic: ["integration"],
349
484
  });
350
- assert.equal(r.status, 0, r.stdout + r.stderr);
351
- assert.equal(r.payload.verdict, "green",
352
- "an ambient GIT_WORK_TREE must not relocate workflow discovery into a foreign directory that has no .github/workflows");
353
- } finally {
354
- rmSync(other, { recursive: true, force: true });
355
- }
356
- });
485
+ assert.equal(r.status, 1, r.stdout + r.stderr);
486
+ const reads = r.log.split("\n").filter((l) => l.startsWith("api "));
487
+ for (const read of [/^api graphql /, /^api repos\/\{owner\}\/\{repo\}\/rules\/branches\/main /, /^api repos\/\{owner\}\/\{repo\}\/branches\/main /, /^api --hostname \S+ repos\/acme\/repo\/compare\//]) {
488
+ assert.ok(reads.some((l) => read.test(l)), `expected a read matching ${read}, the log reads: ${r.log}`);
489
+ }
490
+ for (const l of reads) assert.match(l, /--hostname ghe-real\.example( |$)/, `a gh api read without the resolved host: ${l}`);
491
+ });
492
+ }
357
493
 
358
- test("the behind-count probe: an inherited GIT_DIR must not bind gh's --hostname to another repository's origin", () => {
359
- // The real repository's own origin, and the OTHER (ambient) repository's —
360
- // deliberately different hosts, so the logged `--hostname` argument tells
361
- // the two apart unambiguously. `gh repo view` is stubbed to answer for the
362
- // real repository regardless (it consults no git state at all), so the
363
- // only way the wrong host can reach the logged `gh api` call is through
364
- // `git remote get-url origin` answering for the ambient repository instead
365
- // of the real one — measured, unscrubbed, that is exactly what an ambient
366
- // GIT_DIR does.
494
+ // gh resolves the repository by running git, and an ambient GIT_DIR or
495
+ // GIT_WORK_TREE (a git hook, `rebase --exec`, `bisect run`) outranks its cwd —
496
+ // measured, `gh repo view` and `gh api repos/{owner}/{repo}` then answer for
497
+ // the repository GIT_DIR names. So neither may reach any gh call; the stub
498
+ // logs either one it receives.
499
+ test("an inherited GIT_DIR or GIT_WORK_TREE never reaches gh", () => {
367
500
  const other = mkdtempSync(join(tmpdir(), "ci-state-other-"));
368
- spawnSync("git", ["init", "-q", other], { stdio: "ignore" });
369
- spawnSync("git", ["remote", "add", "origin", "https://ghe-other.example/other/other-repo.git"], { cwd: other, stdio: "ignore" });
370
501
  try {
371
502
  const r = run([], {
372
503
  repoFiles: { ".github/workflows/ci.yml": CI_WORKFLOW },
373
- origin: "https://ghe-real.example/acme/real-repo.git",
374
- repoView: JSON.stringify({ nameWithOwner: "acme/real-repo" }),
375
- spawnEnv: { GIT_DIR: join(other, ".git") },
504
+ spawnEnv: { GIT_DIR: join(other, ".git"), GIT_WORK_TREE: other },
376
505
  });
377
506
  assert.equal(r.status, 0, r.stdout + r.stderr);
378
- assert.match(r.log, /api --hostname ghe-real\.example /,
379
- "an ambient GIT_DIR must not substitute the OTHER repository's origin host for this repository's own");
380
- assert.doesNotMatch(r.log, /ghe-other\.example/,
381
- `the ambient repository's host must never reach gh at all, and the log reads: ${r.log}`);
507
+ assert.match(r.log, /^repo view /m);
508
+ assert.doesNotMatch(r.log, /ambient GIT_DIR/, `an ambient git variable reached gh: ${r.log}`);
382
509
  } finally {
383
510
  rmSync(other, { recursive: true, force: true });
384
511
  }
@@ -475,6 +602,177 @@ test("no run in the list matches the PR head at all: not-green, exit 1 — the u
475
602
  assert.match(r.payload.reasons.join("; "), /no CI run whose headSha equals the PR head abc123def/);
476
603
  });
477
604
 
605
+ // --- Expected = H ∪ (D ∩ required): a PR may drop a job no rule requires ----
606
+ // H is the head's CI job set, B the base's, D = B − H the jobs the PR drops.
607
+ // The base here runs `check` and `integration`, the head only `check`, and the
608
+ // run (the default fixture) carries `check` alone, succeeded — so whether
609
+ // `integration` may be absent is decided by the base branch's rules alone.
610
+ const DROPS_INTEGRATION = {
611
+ baseFiles: { ".github/workflows/ci.yml": TWO_JOB_WORKFLOW },
612
+ headFiles: { ".github/workflows/ci.yml": CI_WORKFLOW },
613
+ };
614
+ const RULES_READ = /^api repos\/\{owner\}\/\{repo\}\/rules\/branches\/main /m;
615
+ const PROTECTION_READ = /^api repos\/\{owner\}\/\{repo\}\/branches\/main /m;
616
+
617
+ test("a job the head drops and no rule requires: green, named in `dropped`, never in `missing`", () => {
618
+ const r = run([], DROPS_INTEGRATION);
619
+ assert.equal(r.status, 0, r.stdout + r.stderr);
620
+ assert.equal(r.payload.verdict, "green");
621
+ assert.deepEqual(r.payload.dropped, ["integration"]);
622
+ assert.deepEqual(r.payload.missing, []);
623
+ assert.match(r.log, RULES_READ, "an allowed drop has to be established against the rules, never assumed");
624
+ assert.match(r.log, PROTECTION_READ);
625
+ });
626
+
627
+ for (const [authority, opts] of [
628
+ ["a ruleset", { rules: ["check", "integration"] }],
629
+ ["classic branch protection alone", { classic: ["integration"] }],
630
+ ]) {
631
+ test(`a job the head drops that ${authority} still requires: not-green, in \`missing\`, annotated`, () => {
632
+ const r = run([], { ...DROPS_INTEGRATION, ...opts });
633
+ assert.equal(r.status, 1, r.stdout + r.stderr);
634
+ assert.equal(r.payload.verdict, "not-green");
635
+ assert.deepEqual(r.payload.missing, ["integration"]);
636
+ assert.deepEqual(r.payload.dropped, []);
637
+ assert.match(r.payload.reasons.join("; "), /expected jobs absent from the run: integration \(dropped by the head's workflow, still required by the base ruleset\)/);
638
+ });
639
+ }
640
+
641
+ // What the rule must ACCEPT: a required context is matched to a job id by
642
+ // exact string, so a context that only resembles the dropped job — another
643
+ // workflow's job, a display name — does not keep it expected.
644
+ test("a required context that merely resembles the dropped job does not keep it expected", () => {
645
+ const r = run([], { ...DROPS_INTEGRATION, rules: ["CI / integration", "Integration"], classic: ["integration-docker"] });
646
+ assert.equal(r.status, 0, r.stdout + r.stderr);
647
+ assert.deepEqual(r.payload.dropped, ["integration"]);
648
+ });
649
+
650
+ test("no dropped job absent from the run: the rules are never read", () => {
651
+ for (const opts of [
652
+ { repoFiles: { ".github/workflows/ci.yml": CI_WORKFLOW } },
653
+ // The head drops `integration`, but the run reports it anyway — its own
654
+ // conclusion is judged like any job's, so the rules cannot matter.
655
+ { ...DROPS_INTEGRATION, runView: JSON.stringify({ ...JSON.parse(RUN_VIEW), jobs: [{ name: "check", status: "completed", conclusion: "success" }, { name: "integration", status: "completed", conclusion: "success" }] }) },
656
+ ]) {
657
+ const r = run([], { ...opts, rulesRaw: null, protectionRaw: null });
658
+ assert.equal(r.status, 0, r.stdout + r.stderr);
659
+ assert.deepEqual(r.payload.dropped, []);
660
+ assert.doesNotMatch(r.log, RULES_READ);
661
+ assert.doesNotMatch(r.log, PROTECTION_READ);
662
+ }
663
+ });
664
+
665
+ test("a job the head adds and the run lacks is missing like any other, and reads no rules", () => {
666
+ const r = run([], {
667
+ baseFiles: { ".github/workflows/ci.yml": CI_WORKFLOW },
668
+ headFiles: { ".github/workflows/ci.yml": TWO_JOB_WORKFLOW },
669
+ rulesRaw: null,
670
+ protectionRaw: null,
671
+ });
672
+ assert.equal(r.status, 1, r.stdout + r.stderr);
673
+ assert.deepEqual(r.payload.missing, ["integration"]);
674
+ assert.match(r.payload.reasons.join("; "), /expected jobs absent from the run: integration$|expected jobs absent from the run: integration;/);
675
+ assert.doesNotMatch(r.log, RULES_READ);
676
+ });
677
+
678
+ // A head with no CI workflow contributes no jobs (H = ∅): every base job is a
679
+ // drop, and the still-required ones stay expected. Removed outright and
680
+ // renamed away (YAML present, none named CI) are both that case at the head,
681
+ // where at the base the second one is a refusal.
682
+ for (const [shape, headFiles] of [
683
+ ["no CI workflow at all", {}],
684
+ ["only workflows under other names", { ".github/workflows/release-label.yml": OTHER_WORKFLOW }],
685
+ ]) {
686
+ test(`a head with ${shape}: its still-required base jobs stay expected`, () => {
687
+ const r = run([], { baseFiles: { ".github/workflows/ci.yml": TWO_JOB_WORKFLOW }, headFiles, rules: ["integration"] });
688
+ assert.equal(r.status, 1, r.stdout + r.stderr);
689
+ assert.deepEqual(r.payload.missing, ["integration"]);
690
+ });
691
+ }
692
+
693
+ test("the base tree is the one at baseRefOid, never the base branch's newer tip", () => {
694
+ // The tip has since gained `integration`, which the rules require; the
695
+ // commit the PR is based on never had it. Judged against the tip, the PR
696
+ // would "drop" a job it never had and read not-green.
697
+ const r = run([], {
698
+ baseTip: "ccc111ccc",
699
+ tipFiles: { ".github/workflows/ci.yml": TWO_JOB_WORKFLOW },
700
+ baseFiles: { ".github/workflows/ci.yml": CI_WORKFLOW },
701
+ headFiles: { ".github/workflows/ci.yml": CI_WORKFLOW },
702
+ rules: ["integration"],
703
+ });
704
+ assert.equal(r.status, 0, r.stdout + r.stderr);
705
+ assert.equal(r.payload.verdict, "green");
706
+ assert.match(r.log, /baseExpr=bbb000bbb/, "the base has to be read at baseRefOid itself");
707
+ });
708
+
709
+ test("a base branch that no longer exists still reads the base at baseRefOid", () => {
710
+ const r = run([], {
711
+ repoFiles: { ".github/workflows/ci.yml": CI_WORKFLOW },
712
+ prView: JSON.stringify({ ...JSON.parse(PR_VIEW), baseRef: null }),
713
+ });
714
+ assert.equal(r.status, 0, r.stdout + r.stderr);
715
+ assert.match(r.log, /baseExpr=bbb000bbb/);
716
+ });
717
+
718
+ // Every way the expected set's sources can fail to answer is exit 2 (or the
719
+ // rate-limited payload) — never the base set alone, never green.
720
+ for (const [what, opts, said] of [
721
+ ["the head ref answering for another commit", { headOid: "fff999fff" }, /refs\/pull\/42\/head resolves to fff999fff, not the PR head abc123def/],
722
+ ["the base read answering for another commit", { baseTip: "ccc111ccc", baseReadOid: "ddd222ddd" }, /base commit bbb000bbb reads back as ddd222ddd/],
723
+ ["two head files sharing the CI name", { headFiles: { ".github/workflows/a.yml": CI_WORKFLOW, ".github/workflows/b.yml": CI_WORKFLOW } }, /2 workflow files under \.github\/workflows\/ at the head commit are named 'CI'/],
724
+ ["a head CI blob truncated", { headFiles: { ".github/workflows/ci.yml": { truncated: true } } }, /cannot read \.github\/workflows\/ci\.yml at the head commit/],
725
+ ["the PR read missing baseRefOid", { prView: JSON.stringify({ headRefName: BRANCH, headRefOid: PR_HEAD, baseRefName: "main" }) }, /baseRefOid \(the base sha\) is not a non-empty string \(the key is absent\)/],
726
+ ["the ruleset read failing", { ...DROPS_INTEGRATION, rulesRaw: null }, /gh failed/],
727
+ ["the classic protection read failing", { ...DROPS_INTEGRATION, protectionRaw: null }, /gh failed/],
728
+ ["the ruleset read the wrong shape", { ...DROPS_INTEGRATION, rulesRaw: JSON.stringify([[{ type: "required_status_checks", parameters: {} }]]) }, /required_status_checks rule without a list of contexts/],
729
+ ["the protection read without a protection object", { ...DROPS_INTEGRATION, protectionRaw: JSON.stringify({ name: "main" }) }, /protection object \(the key is absent\)/],
730
+ ]) {
731
+ test(`${what}: exit 2, never a verdict on the base set or green`, () => {
732
+ const r = run([], { repoFiles: { ".github/workflows/ci.yml": CI_WORKFLOW }, ...opts });
733
+ assert.equal(r.status, 2, r.stdout + r.stderr);
734
+ assert.match(r.stderr, said);
735
+ assert.equal(r.payload, null);
736
+ });
737
+ }
738
+
739
+ test("a quota refusal on the rules read is the rate-limited payload, naming that read", () => {
740
+ const r = run([], { ...DROPS_INTEGRATION, rulesRaw: null, ghFailMsg: "API rate limit exceeded for user ID 1." });
741
+ assert.equal(r.status, 2, r.stdout + r.stderr);
742
+ assert.equal(r.payload.verdict, "rate-limited");
743
+ assert.match(r.payload.reasons.join("; "), /gh api repos\/\{owner\}\/\{repo\}\/rules\/branches\/main/);
744
+ });
745
+
746
+ // --workflow-file names a repo-relative path read at both commits. Missing at
747
+ // the base it refuses — an explicit target is never no-ci; missing at the head
748
+ // it contributes no jobs, like any head without its CI workflow.
749
+ test("--workflow-file absent at the base: exit 2 even under --declare-no-ci", () => {
750
+ const r = run(["--workflow-file", ".github/workflows/ci.yml", "--declare-no-ci"], {
751
+ baseFiles: { ".github/workflows/other.yml": CI_WORKFLOW },
752
+ headFiles: { ".github/workflows/ci.yml": CI_WORKFLOW },
753
+ });
754
+ assert.equal(r.status, 2, r.stdout + r.stderr);
755
+ assert.match(r.stderr, /\.github\/workflows\/ci\.yml does not exist at the base commit bbb000bbb/);
756
+ });
757
+
758
+ test("--workflow-file absent at the head: the head contributes no jobs and the drop rule decides", () => {
759
+ const r = run(["--workflow-file", "./.github/workflows/ci.yml"], {
760
+ baseFiles: { ".github/workflows/ci.yml": TWO_JOB_WORKFLOW },
761
+ headFiles: { ".github/workflows/other.yml": CI_WORKFLOW },
762
+ });
763
+ assert.equal(r.status, 0, r.stdout + r.stderr);
764
+ assert.deepEqual(r.payload.dropped, ["integration"]);
765
+ });
766
+
767
+ for (const bad of ["/abs/.github/workflows/ci.yml", ".github/../ci.yml", ".github/./ci.yml", "ci.yml", ".github/workflows/"]) {
768
+ test(`--workflow-file ${bad}: refused before any read — not a repo-relative file path`, () => {
769
+ const r = run(["--workflow-file", bad], { repoFiles: { ".github/workflows/ci.yml": CI_WORKFLOW } });
770
+ assert.equal(r.status, 2, r.stdout + r.stderr);
771
+ assert.match(r.stderr, /--workflow-file takes a repo-relative path/);
772
+ assert.equal(r.log, "");
773
+ });
774
+ }
775
+
478
776
  // --- #1410: two runs tied on head SHA AND createdAt to the second ----------
479
777
  // A stable sort with no tie-break keeps gh's own (unspecified) API order on
480
778
  // an exact createdAt tie. Measured live on PR #1402: two runs on the same
@@ -616,7 +914,7 @@ test("trailing --base (no value) dies (exit 2) rather than silently comparing ag
616
914
  const r = run(["--base"]);
617
915
  assert.equal(r.status, 2);
618
916
  assert.match(r.stderr, /--base needs a value/);
619
- assert.doesNotMatch(r.log, /pr view/, "must die before ever asking gh anything");
917
+ assert.equal(r.log, "", "must die before ever asking gh anything");
620
918
  });
621
919
 
622
920
  test("--workflow=CI form dies by name, not silently read as absent (indexOf cannot see it)", () => {
@@ -636,7 +934,7 @@ for (const flag of ["declare-no-ci", "quiet"]) {
636
934
  const r = run([`--${flag}${v}`]);
637
935
  assert.equal(r.status, 2);
638
936
  assert.match(r.stderr, new RegExp(`--${flag} is a boolean flag, not --${flag}=`));
639
- assert.doesNotMatch(r.log, /pr view/, "must die before ever asking gh anything");
937
+ assert.equal(r.log, "", "must die before ever asking gh anything");
640
938
  });
641
939
  }
642
940
  }
@@ -652,7 +950,7 @@ test("--declare-no-ci=true dies behind another flag too, not only at the front o
652
950
  const r = run(["--quiet", "--declare-no-ci=true"]);
653
951
  assert.equal(r.status, 2);
654
952
  assert.match(r.stderr, /--declare-no-ci is a boolean flag, not --declare-no-ci=/);
655
- assert.doesNotMatch(r.log, /pr view/, "must die before ever asking gh anything");
953
+ assert.equal(r.log, "", "must die before ever asking gh anything");
656
954
  });
657
955
 
658
956
  // The control: the new `=` guard must not touch the bare spelling. --quiet's
@@ -663,28 +961,28 @@ test("--declare-no-ci=true dies behind another flag too, not only at the front o
663
961
  test("--quiet still reads as present in its bare spelling — the = refusal is not a blanket one", () => {
664
962
  const loud = run([], { repoFiles: { ".github/workflows/ci.yml": CI_WORKFLOW } });
665
963
  assert.equal(loud.status, 0, loud.stdout + loud.stderr);
666
- assert.match(loud.stderr, /\$ gh pr view/);
964
+ assert.match(loud.stderr, /\$ gh api graphql/);
667
965
 
668
966
  const quiet = run(["--quiet"], { repoFiles: { ".github/workflows/ci.yml": CI_WORKFLOW } });
669
967
  assert.equal(quiet.status, 0, quiet.stdout + quiet.stderr);
670
- assert.doesNotMatch(quiet.stderr, /\$ gh pr view/);
671
- assert.match(quiet.log, /pr view/, "gh still ran despite the quieter stderr");
968
+ assert.doesNotMatch(quiet.stderr, /\$ gh api graphql/);
969
+ assert.match(quiet.log, /api graphql/, "gh still ran despite the quieter stderr");
672
970
  });
673
971
 
674
972
  // #677. The other half of what `--quiet` does, and the half the flag exists for:
675
- // `jobs` and `missing` leave the JSON payload with it and are present without
973
+ // `jobs`, `missing` and `dropped` leave the JSON payload with it and are present without
676
974
  // it. Same fixture both ways, so the flag is the only difference. The field
677
975
  // names are spelled out rather than derived from ci-state.mjs — deriving them
678
976
  // from the assignment under test would make this pass vacuously if that
679
977
  // assignment changed, which is the one thing it must not do. The deriving is
680
978
  // `quiet-payload-prose.test.mjs`'s job, over the prose that has to agree.
681
- test("--quiet drops `jobs` and `missing` from the payload; without it they are there", () => {
979
+ test("--quiet drops `jobs`, `missing` and `dropped` from the payload; without it they are there", () => {
682
980
  const opts = { repoFiles: { ".github/workflows/ci.yml": CI_WORKFLOW } };
683
981
  const loud = run([], opts);
684
982
  const quiet = run(["--quiet"], opts);
685
983
  assert.equal(loud.status, 0, loud.stdout + loud.stderr);
686
984
  assert.equal(quiet.status, 0, quiet.stdout + quiet.stderr);
687
- for (const field of ["jobs", "missing"]) {
985
+ for (const field of ["jobs", "missing", "dropped"]) {
688
986
  assert.ok(field in loud.payload, `without --quiet the payload must carry \`${field}\`, and it reads ${JSON.stringify(loud.payload)}`);
689
987
  assert.ok(!(field in quiet.payload), `--quiet must drop \`${field}\` from the payload, and it reads ${JSON.stringify(quiet.payload)}`);
690
988
  }
@@ -703,7 +1001,7 @@ test("--base followed by another flag is rejected, not read as the string \"--wo
703
1001
  const r = run(["--base", "--workflow", "CI"]);
704
1002
  assert.equal(r.status, 2);
705
1003
  assert.match(r.stderr, /--base needs a value/);
706
- assert.doesNotMatch(r.log, /pr view/, "must die before ever asking gh anything");
1004
+ assert.equal(r.log, "", "must die before ever asking gh anything");
707
1005
  });
708
1006
 
709
1007
  // #463 fallout: `--workflow-file` is the one flag this file used to read below
@@ -715,7 +1013,7 @@ test("trailing --workflow-file names --workflow-file, not the innocent value of
715
1013
  const r = run(["--workflow-file", "--base", "main"]);
716
1014
  assert.equal(r.status, 2);
717
1015
  assert.match(r.stderr, /--workflow-file needs a value/);
718
- assert.doesNotMatch(r.log, /pr view/, "must die before ever asking gh anything");
1016
+ assert.equal(r.log, "", "must die before ever asking gh anything");
719
1017
  });
720
1018
 
721
1019
  test("--base given a whitespace-only value dies rather than comparing against the default base", () => {
@@ -736,8 +1034,8 @@ test("--base given a whitespace-only value dies rather than comparing against th
736
1034
 
737
1035
  // Every assertion in this section is about the REFUSAL LINE, not about stderr
738
1036
  // as a whole: the diagnostic stream echoes each gh command before running it,
739
- // and that echo carries `pr view`, `run view` and every field name in the
740
- // `--json` list — `$ gh pr view 42 --json headRefName,headRefOid,state,...`
1037
+ // and that echo carries `api graphql`, `run view` and every field name the
1038
+ // query asks for — `$ gh api graphql … headRefName headRefOid baseRefName …`
741
1039
  // (measured). `assert.match(r.stderr, /headRefName/)` is therefore satisfied
742
1040
  // by a refusal that names nothing at all, which leaves the exit status as the
743
1041
  // only load-bearing half of such a case. Asserting on the line itself is what
@@ -779,7 +1077,7 @@ for (const [what, field, prFields, saw, notSaw] of [
779
1077
  prView: JSON.stringify({ state: "OPEN", mergeStateStatus: "CLEAN", ...prFields }),
780
1078
  });
781
1079
  assert.equal(r.status, 2, r.stdout + r.stderr);
782
- assert.match(r.stderr, /pr view/);
1080
+ assert.match(r.stderr, /api graphql/);
783
1081
  assert.match(refusal(r), new RegExp(field));
784
1082
  assert.match(refusal(r), saw);
785
1083
  assert.doesNotMatch(refusal(r), notSaw);
@@ -993,25 +1291,21 @@ test("in-progress job with conclusion:null is accepted — not refused as malfor
993
1291
  // also redden tests above, which happen to pass them.
994
1292
  //
995
1293
  // So: every flag ci-state.mjs accepts, in ONE green run. --workflow-file is the
996
- // one that would not otherwise be here, because its arg() call sits far below
997
- // the sweep, next to discoverWorkflowFile — the sweep needs the NAME, and a set
998
- // built by reading down to the first gh call would miss it.
1294
+ // one that would not otherwise be here, because its value is first needed far
1295
+ // below the sweep, where the workflow trees are read — the sweep needs the
1296
+ // NAME, and a set built by reading down to the first gh call would miss it.
999
1297
  //
1000
- // Relative to cwd on purpose: run() builds its repo in a fresh tmpdir this
1001
- // scope cannot name, and the script resolves an explicit --workflow-file
1002
- // against cwd, which run() sets to that repo.
1298
+ // A repo-relative path: the script reads it at the PR's head and base
1299
+ // commits, which run() serves from the same files.
1003
1300
  test("every flag ci-state.mjs accepts survives the unknown-flag sweep in one invocation", () => {
1004
1301
  const r = run(["--base", "main", "--workflow", "CI", "--workflow-file", ".github/workflows/ci.yml", "--declare-no-ci", "--quiet"], {
1005
1302
  repoFiles: { ".github/workflows/ci.yml": CI_WORKFLOW },
1006
1303
  });
1007
1304
  assert.equal(r.status, 0, `a working invocation was refused: ${r.stderr}`);
1008
1305
  // Live, not subsumed by the status assertion above: the behind-count block
1009
- // TOLERATES its children — tryRun() swallows the failure and returns null
1306
+ // TOLERATES its child — tryRun() swallows the failure and returns null
1010
1307
  // while execFileSync has already forwarded the child's stderr — so a child
1011
- // refusing a flag lands here at exit 0 with `behind` silently null. Matches
1012
- // git's "unknown option" as well as the fleet's own "unknown flag", because
1013
- // the tolerated children are git's: measured, a bogus flag on the `git
1014
- // remote get-url` call is otherwise 35/35 green.
1308
+ // refusing a flag lands here at exit 0 with `behind` silently null.
1015
1309
  assert.doesNotMatch(r.stderr, /unknown (flag|option)/);
1016
1310
  assert.equal(r.payload.verdict, "green");
1017
1311
  });
@@ -1053,7 +1347,7 @@ test("a rate-limited gh read names the quota as its cause in the payload, at the
1053
1347
  // indistinguishable from a correct report at the point a caller reads it.
1054
1348
  // assert/strict, so this pins the type as well as the value.
1055
1349
  assert.equal(r.payload.pr, 42);
1056
- // The refused query, not merely that a quota was mentioned: `pr view` is
1350
+ // The refused query, not merely that a quota was mentioned: the PR read is
1057
1351
  // GraphQL and `run list`/`run view` are REST, so which one was refused is
1058
1352
  // what separates an exhausted REST quota from a token or repo problem. The
1059
1353
  // fixture refuses `run list` — keep this literal in step with it.
@@ -1102,7 +1396,7 @@ test("a gh read failing for any other reason reports exactly as it did before: e
1102
1396
  // observed state.
1103
1397
  test("the outage payload reports no CI state it could not observe", () => {
1104
1398
  const r = ghFailure(RATE_LIMIT_STDERR);
1105
- for (const field of ["status", "conclusion", "jobs", "missing", "runId"]) {
1399
+ for (const field of ["status", "conclusion", "jobs", "missing", "dropped", "runId"]) {
1106
1400
  assert.ok(
1107
1401
  !(field in r.payload),
1108
1402
  `a probe that never read CI must not report \`${field}\`, and the payload reads ${JSON.stringify(r.payload)}`,
@@ -1126,7 +1420,7 @@ test("a payload for a question never asked — no-ci — omits `jobs`/`missing`
1126
1420
  assert.equal(noCi.status, 1, noCi.stdout + noCi.stderr);
1127
1421
  assert.equal(noCi.payload.verdict, "no-ci");
1128
1422
 
1129
- for (const field of ["jobs", "missing"]) {
1423
+ for (const field of ["jobs", "missing", "dropped"]) {
1130
1424
  assert.ok(
1131
1425
  !(field in noCi.payload),
1132
1426
  `no-ci never binds a run, so \`${field}\` must be absent, not an empty array — and the payload reads ${JSON.stringify(noCi.payload)}`,
@@ -1140,7 +1434,7 @@ test("a payload for a question never asked — no-ci — omits `jobs`/`missing`
1140
1434
  test("no CI + --declare-no-ci: `jobs`/`missing` stay absent — the flag changes the gate, not what was read", () => {
1141
1435
  const r = run(["--declare-no-ci"]);
1142
1436
  assert.equal(r.status, 0, r.stdout + r.stderr);
1143
- for (const field of ["jobs", "missing"]) {
1437
+ for (const field of ["jobs", "missing", "dropped"]) {
1144
1438
  assert.ok(!(field in r.payload), `\`${field}\` must stay absent under --declare-no-ci too — got ${JSON.stringify(r.payload)}`);
1145
1439
  }
1146
1440
  });
@@ -1204,7 +1498,7 @@ test("a non-numeric --pr refuses before any query, rather than reporting `pr: nu
1204
1498
  // so each value refuses only while its own anchor is present and neither mutant
1205
1499
  // survives the pair. What a surviving mutant lets through is this block's whole
1206
1500
  // defect back: Number("42x") is NaN, the payload's only identifying field
1207
- // serializes to null, and `gh pr view 42x` resolves the value as a BRANCH — the
1501
+ // serializes to null, and the PR read would build `refs/pull/42x/head` out of it — the
1208
1502
  // ambiguity the digits-only shape is chosen to forfeit against. Every other
1209
1503
  // --pr this suite feeds the guard is all digits or none, and both mutants agree
1210
1504
  // with the real guard on those.
@@ -1560,8 +1854,7 @@ test("the verdict line on stderr, too large for one non-blocking write, survives
1560
1854
  // through the async stream without having ended its line — arg.mjs's die()
1561
1855
  // documents the same shape for the same reason. That RACE is what does not
1562
1856
  // reproduce here: the text before the summary has already ended its own line,
1563
- // in this fixture a vlog trace and under --quiet git's forwarded `error: No
1564
- // such remote 'origin'`. The BYTE is another matter — against already-ended
1857
+ // in this fixture a vlog trace. The BYTE is another matter — against already-ended
1565
1858
  // text the leading newline leaves a blank line and dropping it leaves none —
1566
1859
  // so it is pinned below, doubled as well as missing. What that leaves
1567
1860
  // untested is the mid-line landing the newline exists to prevent, not the