@bridge_gpt/mcp-server 0.2.72 → 0.2.74

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 (181) hide show
  1. package/README.md +5 -5
  2. package/build/agent-launchers/claude-executor-adapter.js +15 -0
  3. package/build/agents.generated.js +6 -6
  4. package/build/bridge-config.js +7 -1
  5. package/build/bridge-permissions.js +30 -0
  6. package/build/ci-system-detection.js +50 -0
  7. package/build/ci-trigger-adapters.js +631 -0
  8. package/build/ci-trigger-install.js +106 -0
  9. package/build/codex-launcher-pin.js +33 -2
  10. package/build/codex-skill-adapter.js +3 -1
  11. package/build/command-host.js +33 -0
  12. package/build/command-provisioning.js +4 -3
  13. package/build/commands.generated.js +7 -6
  14. package/build/conductor/bridge-api-client.js +867 -35
  15. package/build/conductor/bring-up-facts.js +18 -5
  16. package/build/conductor/command-drift-production.js +42 -17
  17. package/build/conductor/command-drift.js +48 -1
  18. package/build/conductor/declare-blocked.js +554 -0
  19. package/build/conductor/declared-extras.js +335 -0
  20. package/build/conductor/doctor.js +108 -21
  21. package/build/conductor/driver-commands.js +141 -0
  22. package/build/conductor/driver-guard.js +277 -0
  23. package/build/conductor/driver-lapse.js +321 -0
  24. package/build/conductor/driver-pass-output.js +104 -0
  25. package/build/conductor/driver-pass.js +571 -0
  26. package/build/conductor/driver-register.js +176 -0
  27. package/build/conductor/driver-release.js +174 -0
  28. package/build/conductor/driver-session-state.js +315 -0
  29. package/build/conductor/epic-branch-ci-inspection.js +281 -43
  30. package/build/conductor/epic-implementer-cli.js +199 -15
  31. package/build/conductor/epic-server-database.js +162 -0
  32. package/build/conductor/epic-server-guidance.js +278 -0
  33. package/build/conductor/epic-server-journal.js +239 -0
  34. package/build/conductor/epic-server-processes.js +324 -0
  35. package/build/conductor/epic-server-recipe.js +397 -0
  36. package/build/conductor/epic-server-runtime.js +1207 -0
  37. package/build/conductor/epic-server-sfcc-upload.js +1567 -0
  38. package/build/conductor/epic-server-sfcc.js +85 -0
  39. package/build/conductor/epic-server-state.js +427 -0
  40. package/build/conductor/epic-server.js +647 -0
  41. package/build/conductor/gate-discovery.js +196 -11
  42. package/build/conductor/gate-settings-review.js +261 -4
  43. package/build/conductor/launch-need-probes.js +6 -53
  44. package/build/conductor/launch-plan.js +313 -156
  45. package/build/conductor/prerequisite-inputs.js +879 -45
  46. package/build/conductor/readiness-assessment.js +16 -1
  47. package/build/conductor/readiness-cli.js +20 -9
  48. package/build/conductor/readiness-prerequisites.js +28 -9
  49. package/build/conductor/readiness-sections.js +133 -126
  50. package/build/conductor/readiness.js +1 -1
  51. package/build/conductor/recovery-cli.js +13 -2
  52. package/build/conductor/tools.js +6 -5
  53. package/build/conductor/wait-run-attention.js +2 -2
  54. package/build/conductor/wait-run-cli.js +208 -22
  55. package/build/conductor/worker-database-doctor-inspector.js +33 -2
  56. package/build/conductor/worker-setup-tally.js +27 -0
  57. package/build/credential-delivery.js +280 -0
  58. package/build/credential-launch-safety.js +142 -0
  59. package/build/credential-materialization.js +91 -16
  60. package/build/credential-references-node-deps.js +29 -0
  61. package/build/credential-references.js +1394 -0
  62. package/build/credential-references.test-fixtures.js +211 -0
  63. package/build/credential-scan-conventions.js +145 -0
  64. package/build/credential-scan.js +517 -0
  65. package/build/credential-store.js +121 -1
  66. package/build/credentials-cli.js +252 -3
  67. package/build/dependency-install-declarations.js +102 -1
  68. package/build/docs.generated.js +10 -3
  69. package/build/doctor.js +95 -3
  70. package/build/drive-epic-help.js +21 -2
  71. package/build/drive-epic.js +2095 -375
  72. package/build/engine-paths.js +20 -0
  73. package/build/epic-branch-ci-summary.js +157 -0
  74. package/build/epic-capability-statement.js +175 -0
  75. package/build/epic-ci-workflow-drift.js +77 -23
  76. package/build/epic-ci-workflow-install.js +137 -44
  77. package/build/epic-ci-workflow-v2.js +274 -0
  78. package/build/epic-ci-workflow-v3.js +273 -0
  79. package/build/epic-ci-workflow.js +165 -18
  80. package/build/epic-implementer-bin.js +2204 -56
  81. package/build/epic-launch-context.js +702 -0
  82. package/build/epic-server-record.js +288 -0
  83. package/build/epic-shared/bridge-client.js +11 -3
  84. package/build/escalation-reachability.js +43 -0
  85. package/build/executor/ci-fix-test-runs.js +334 -0
  86. package/build/executor/completion-contract.js +8 -1
  87. package/build/executor/credential-delivery-step.js +168 -0
  88. package/build/executor/deps.js +3 -1
  89. package/build/executor/env.js +98 -23
  90. package/build/executor/epic-server-context.js +157 -0
  91. package/build/executor/gate-qualification-job.js +127 -26
  92. package/build/executor/guarded-fast-forward-push.js +13 -8
  93. package/build/executor/http-client.js +52 -5
  94. package/build/executor/job-runner.js +577 -81
  95. package/build/executor/observation.js +8 -0
  96. package/build/executor/permissions.js +85 -3
  97. package/build/executor/pre-pr-checks.js +363 -0
  98. package/build/executor/prompt-spec.js +149 -3
  99. package/build/executor/reconcile-overlap-job.js +5 -3
  100. package/build/executor/required-work-diagnostics.js +4 -2
  101. package/build/executor/required-work-ledger.js +936 -38
  102. package/build/executor/results.js +58 -0
  103. package/build/executor/terminal-mutation.js +8 -0
  104. package/build/executor/worker-command.js +72 -3
  105. package/build/executor/worker-database-config.js +281 -0
  106. package/build/executor/worker-database-target.js +16 -12
  107. package/build/executor/worker-database.js +320 -101
  108. package/build/executor/worker-finalization.js +7 -1
  109. package/build/executor/worker-resources.js +25 -3
  110. package/build/executor/worker-verification-guidance.js +18 -3
  111. package/build/executor/worktree.js +20 -1
  112. package/build/executor-launch.js +58 -4
  113. package/build/generated-assets-catalog.js +2 -2
  114. package/build/git-exclude-lock.js +187 -0
  115. package/build/index.js +824 -366
  116. package/build/init.js +26 -11
  117. package/build/install-bridge-conductor-answers.js +528 -0
  118. package/build/install-bridge-conductor-apply.js +288 -0
  119. package/build/install-bridge-conductor-assessment.js +1222 -0
  120. package/build/install-bridge-conductor-compact.js +186 -0
  121. package/build/install-bridge-conductor-contracts.js +72 -0
  122. package/build/install-bridge-conductor-gate.js +1312 -198
  123. package/build/install-bridge-conductor-setup.js +577 -83
  124. package/build/install-bridge-conductor.js +3381 -544
  125. package/build/install-bridge.js +280 -25
  126. package/build/install-doctor.js +38 -8
  127. package/build/install-maintenance.js +26 -3
  128. package/build/install-sfcc-detection.js +120 -0
  129. package/build/install-sfcc-setup.js +185 -0
  130. package/build/launcher-config-inspection.js +162 -1
  131. package/build/mcp-grant-candidates.js +779 -0
  132. package/build/mcp-grant-doctor.js +63 -0
  133. package/build/mcp-grant.js +288 -0
  134. package/build/mcp-host-targets.js +8 -0
  135. package/build/mcp-identity.js +23 -0
  136. package/build/mcp-invoke.js +292 -15
  137. package/build/mcp-presets.js +107 -0
  138. package/build/mcp-provisioning.js +155 -6
  139. package/build/mcp-registration-doctor.js +31 -2
  140. package/build/mcp-server-invocation.js +19 -1
  141. package/build/merge-workflow-install.js +243 -110
  142. package/build/pipelines.generated.js +7 -6
  143. package/build/plan-discovery.js +97 -4
  144. package/build/plan-execution-ledger.js +100 -7
  145. package/build/plan-generation-io.js +262 -0
  146. package/build/plan-generation.js +424 -0
  147. package/build/plane/build-freshness.js +6 -1
  148. package/build/plane/cli.js +75 -9
  149. package/build/plane/plane-freshness.js +17 -0
  150. package/build/plane/preflight.js +50 -10
  151. package/build/plane/shutdown.js +19 -0
  152. package/build/plane/status.js +8 -2
  153. package/build/plane/terminal-run-cleanup.js +18 -4
  154. package/build/printed-remedy-commands.js +211 -12
  155. package/build/project-root.js +40 -0
  156. package/build/readiness-check.js +77 -0
  157. package/build/readiness-prerequisites.generated.js +122 -14
  158. package/build/readme.generated.js +1 -1
  159. package/build/repository-state-paths.js +41 -0
  160. package/build/repository-toolchains.js +6 -3
  161. package/build/run-unit-tests-launcher.js +8 -1
  162. package/build/setup-epic.js +1305 -210
  163. package/build/setup-progress.js +44 -0
  164. package/build/setup-question.js +122 -4
  165. package/build/sfcc/credentials.js +3 -2
  166. package/build/sfcc/hostname.js +98 -2
  167. package/build/sfcc/tool-wrapper.js +6 -95
  168. package/build/start-tickets.js +193 -29
  169. package/build/test-support/executor-source-files.js +200 -0
  170. package/build/testing-config-client.js +25 -2
  171. package/build/third-party-mcp-targets.js +63 -2
  172. package/build/ticket-delivery-enrollment.js +9 -0
  173. package/build/upgrade-cli.js +85 -2
  174. package/build/v2-readiness-requirements.js +28 -1
  175. package/build/version.generated.js +3 -3
  176. package/build/with-credentials.js +208 -0
  177. package/build/worktree-credential-delivery.js +340 -0
  178. package/docs/EPIC-IMPLEMENTER-INSTALL.md +9 -1
  179. package/docs/epic-implementer/getting-started.md +3 -1
  180. package/docs/epic-implementer/self-hosting.md +14 -1
  181. package/package.json +2 -2
@@ -167,14 +167,47 @@ function filterPatterns(node) {
167
167
  }
168
168
  return null;
169
169
  }
170
- function classifyBranches(node) {
170
+ /**
171
+ * BAPI-1654 — classify one pattern against ONE concrete branch name. The answer
172
+ * is `all` (it matches), `none` (it does not), or `some` when the pattern uses a
173
+ * glob form this matcher does not evaluate (`[...]` or `+`), which keeps the
174
+ * filter uncertain rather than guessed.
175
+ */
176
+ export function classifyBranchPatternForTarget(pattern, branch) {
177
+ if (pattern.includes("[") || pattern.includes("+"))
178
+ return "some";
179
+ let re = "^";
180
+ for (let i = 0; i < pattern.length; i++) {
181
+ const ch = pattern[i];
182
+ if (ch === "*" && pattern[i + 1] === "*") {
183
+ re += ".*";
184
+ i++;
185
+ }
186
+ else if (ch === "*") {
187
+ re += "[^/]*";
188
+ }
189
+ else if (ch === "?") {
190
+ re += "[^/]";
191
+ }
192
+ else {
193
+ re += ch.replace(/[\\^$.|(){}]/g, "\\$&");
194
+ }
195
+ }
196
+ return new RegExp(`${re}$`).test(branch) ? "all" : "none";
197
+ }
198
+ function classifierFor(targetBranch) {
199
+ return targetBranch === undefined
200
+ ? classifyEpicBranchPattern
201
+ : (pattern) => classifyBranchPatternForTarget(pattern, targetBranch);
202
+ }
203
+ function classifyBranches(node, classify) {
171
204
  const patterns = filterPatterns(node);
172
205
  if (patterns === null || patterns.length === 0)
173
206
  return couldNotTell("branch_filter_uncertain");
174
207
  let state = "none_in";
175
208
  for (const raw of patterns) {
176
209
  const negated = raw.startsWith("!");
177
- const match = classifyEpicBranchPattern(negated ? raw.slice(1) : raw);
210
+ const match = classify(negated ? raw.slice(1) : raw);
178
211
  if (match === "none")
179
212
  continue;
180
213
  if (!negated) {
@@ -196,21 +229,21 @@ function classifyBranches(node) {
196
229
  return EXCLUDES;
197
230
  return couldNotTell("branch_filter_uncertain");
198
231
  }
199
- function classifyBranchesIgnore(node) {
232
+ function classifyBranchesIgnore(node, classify) {
200
233
  if (node !== undefined && node.t === "seq" && node.items.length === 0)
201
234
  return COVERS;
202
235
  const patterns = filterPatterns(node);
203
236
  if (patterns === null || patterns.some((p) => p.startsWith("!"))) {
204
237
  return couldNotTell("branch_filter_uncertain");
205
238
  }
206
- const matches = patterns.map(classifyEpicBranchPattern);
239
+ const matches = patterns.map((p) => classify(p));
207
240
  if (matches.includes("all"))
208
241
  return EXCLUDES;
209
242
  if (matches.every((m) => m === "none"))
210
243
  return COVERS;
211
244
  return couldNotTell("branch_filter_uncertain");
212
245
  }
213
- function classifyPrEvent(node) {
246
+ function classifyPrEvent(node, classify) {
214
247
  if (node.t === "null")
215
248
  return COVERS;
216
249
  if (node.t !== "map")
@@ -235,9 +268,9 @@ function classifyPrEvent(node) {
235
268
  if (hasBranches && hasIgnore)
236
269
  return couldNotTell("branch_filter_uncertain");
237
270
  if (hasBranches)
238
- return classifyBranches(e.get("s:branches"));
271
+ return classifyBranches(e.get("s:branches"), classify);
239
272
  if (hasIgnore)
240
- return classifyBranchesIgnore(e.get("s:branches-ignore"));
273
+ return classifyBranchesIgnore(e.get("s:branches-ignore"), classify);
241
274
  return COVERS;
242
275
  }
243
276
  // ---------------------------------------------------------------------------
@@ -741,7 +774,7 @@ function jobsHaveUses(entries) {
741
774
  }
742
775
  return false;
743
776
  }
744
- function classifyOrThrow(text) {
777
+ function classifyOrThrow(text, classify) {
745
778
  if (typeof text !== "string" || text.trim() === "")
746
779
  return couldNotTell("unparseable");
747
780
  const root = scanRoot(text);
@@ -776,7 +809,7 @@ function classifyOrThrow(text) {
776
809
  }
777
810
  else if (node.t === "map") {
778
811
  triggerNames = [...node.entries.keys()].filter((k) => k.startsWith("s:")).map((k) => k.slice(2));
779
- prResults = PR_TRIGGER_KEYS.filter((k) => node.entries.has(`s:${k}`)).map((k) => classifyPrEvent(node.entries.get(`s:${k}`)));
812
+ prResults = PR_TRIGGER_KEYS.filter((k) => node.entries.has(`s:${k}`)).map((k) => classifyPrEvent(node.entries.get(`s:${k}`), classify));
780
813
  if (prResults.some((r) => r.coverage === "covers"))
781
814
  return COVERS;
782
815
  const uncertain = prResults.find((r) => r.coverage === "could_not_tell");
@@ -794,16 +827,91 @@ function classifyOrThrow(text) {
794
827
  }
795
828
  /**
796
829
  * Does this workflow's PR trigger run for a pull request whose base is
797
- * `epic/<EPIC>`? Never throws; see the module truth table.
830
+ * `epic/<EPIC>` (or, with `options.targetBranch`, that branch)? Never throws;
831
+ * see the module truth table.
798
832
  */
799
- export function classifyEpicPullRequestTrigger(text) {
833
+ export function classifyEpicPullRequestTrigger(text, options = {}) {
800
834
  try {
801
- return classifyOrThrow(text);
835
+ return classifyOrThrow(text, classifierFor(options.targetBranch));
802
836
  }
803
837
  catch (err) {
804
838
  return couldNotTell(err instanceof UnsupportedYaml ? "unsupported_structure" : "unparseable");
805
839
  }
806
840
  }
841
+ /**
842
+ * BAPI-1654 — the trigger's event names, as the same scanner reads them, or
843
+ * `null` when the trigger cannot be read. Used to prove an edit kept every
844
+ * existing trigger. Never returns other workflow content.
845
+ */
846
+ export function readWorkflowTriggerNames(text) {
847
+ try {
848
+ if (typeof text !== "string" || text.trim() === "")
849
+ return null;
850
+ const root = scanRoot(text);
851
+ if (root.kind !== "ok")
852
+ return null;
853
+ const trigger = findTrigger(root.entries);
854
+ if (trigger === null)
855
+ return null;
856
+ const node = parseEntryValue(trigger);
857
+ if (node.t === "str")
858
+ return [node.v];
859
+ if (node.t === "seq")
860
+ return node.items.every((i) => i.t === "str") ? node.items.map((i) => i.v) : null;
861
+ if (node.t === "map")
862
+ return [...node.entries.keys()].map((k) => (k.startsWith("s:") ? k.slice(2) : k));
863
+ return null;
864
+ }
865
+ catch {
866
+ return null;
867
+ }
868
+ }
869
+ export function readPullRequestBranchFilters(text) {
870
+ try {
871
+ if (typeof text !== "string" || text.trim() === "")
872
+ return { kind: "could_not_tell" };
873
+ const root = scanRoot(text);
874
+ if (root.kind !== "ok")
875
+ return { kind: "could_not_tell" };
876
+ const trigger = findTrigger(root.entries);
877
+ if (trigger === null)
878
+ return { kind: "could_not_tell" };
879
+ const node = parseEntryValue(trigger);
880
+ const isPr = (name) => PR_TRIGGER_KEYS.includes(name);
881
+ if (node.t === "str")
882
+ return isPr(node.v) ? { kind: "filters", branches: null } : { kind: "no_pr_trigger" };
883
+ if (node.t === "seq") {
884
+ return node.items.some((i) => i.t === "str" && isPr(i.v))
885
+ ? { kind: "filters", branches: null }
886
+ : { kind: "no_pr_trigger" };
887
+ }
888
+ if (node.t !== "map")
889
+ return { kind: "could_not_tell" };
890
+ const prKey = PR_TRIGGER_KEYS.find((k) => node.entries.has(`s:${k}`));
891
+ if (prKey === undefined)
892
+ return { kind: "no_pr_trigger" };
893
+ const event = node.entries.get(`s:${prKey}`);
894
+ if (event.t === "null")
895
+ return { kind: "filters", branches: null };
896
+ if (event.t !== "map")
897
+ return { kind: "could_not_tell" };
898
+ // A `branches-ignore` list is not "every branch": report it as unread rather than misstate it.
899
+ if (event.entries.has("s:branches-ignore"))
900
+ return { kind: "could_not_tell" };
901
+ const branches = event.entries.get("s:branches");
902
+ if (branches === undefined)
903
+ return { kind: "filters", branches: null };
904
+ if (branches.t === "str")
905
+ return { kind: "filters", branches: [branches.v] };
906
+ if (branches.t === "seq" && branches.items.every((i) => i.t === "str")) {
907
+ return { kind: "filters", branches: branches.items.map((i) => i.v) };
908
+ }
909
+ return { kind: "could_not_tell" };
910
+ }
911
+ catch {
912
+ return { kind: "could_not_tell" };
913
+ }
914
+ }
807
915
  // ---------------------------------------------------------------------------
808
916
  // Repository aggregate
809
917
  // ---------------------------------------------------------------------------
@@ -876,42 +984,55 @@ export async function inspectEpicBranchWorkflowCoverage(fs, budget = {}) {
876
984
  return { coverage: "does_not_cover", reason: "no_workflows", workflows: [] };
877
985
  const started = now();
878
986
  const workflows = [];
987
+ const targetResults = [];
988
+ const target = budget.targetBranch;
879
989
  for (const file of files) {
880
990
  const path = `${WORKFLOWS_DIR}/${file}`;
881
991
  const remaining = overall - (now() - started);
882
992
  if (remaining <= 0) {
883
993
  workflows.push({ path, coverage: "could_not_tell", reason: "read_failed" });
994
+ targetResults.push({ path, coverage: "could_not_tell", reason: "read_failed" });
884
995
  continue;
885
996
  }
886
997
  const text = await readWithTimeout(fs, path, Math.min(perRead, remaining));
887
998
  if (text === null) {
888
999
  workflows.push({ path, coverage: "could_not_tell", reason: "read_failed" });
1000
+ targetResults.push({ path, coverage: "could_not_tell", reason: "read_failed" });
889
1001
  continue;
890
1002
  }
891
1003
  workflows.push({ path, ...classifyEpicPullRequestTrigger(text) });
1004
+ if (target !== undefined)
1005
+ targetResults.push({ path, ...classifyEpicPullRequestTrigger(text, { targetBranch: target }) });
892
1006
  }
893
- if (workflows.some((w) => w.coverage === "covers")) {
894
- return { coverage: "covers", reason: "pr_trigger_covers", workflows };
895
- }
896
- if (truncated)
897
- return { coverage: "could_not_tell", reason: "file_limit_reached", workflows };
898
- if (workflows.some((w) => w.reason === "read_failed")) {
899
- return { coverage: "could_not_tell", reason: "read_failed", workflows };
900
- }
901
- const unresolved = workflows.find((w) => w.coverage === "could_not_tell");
902
- if (unresolved)
903
- return { coverage: "could_not_tell", reason: unresolved.reason, workflows };
904
- const excluded = workflows.some((w) => w.reason === "branch_filter_excludes_epic");
905
- return {
906
- coverage: "does_not_cover",
907
- reason: excluded ? "branch_filter_excludes_epic" : "no_pr_trigger",
908
- workflows,
909
- };
1007
+ const aggregate = aggregateCoverage(workflows, truncated);
1008
+ return target === undefined
1009
+ ? aggregate
1010
+ : { ...aggregate, integrationTarget: { branch: target, coverage: aggregateCoverage(targetResults, truncated).coverage } };
910
1011
  }
911
1012
  catch {
912
1013
  return { coverage: "could_not_tell", reason: "listing_failed", workflows: [] };
913
1014
  }
914
1015
  }
1016
+ /** The repository aggregate over per-workflow results; see the module documentation. */
1017
+ function aggregateCoverage(workflows, truncated) {
1018
+ if (workflows.some((w) => w.coverage === "covers")) {
1019
+ return { coverage: "covers", reason: "pr_trigger_covers", workflows };
1020
+ }
1021
+ if (truncated)
1022
+ return { coverage: "could_not_tell", reason: "file_limit_reached", workflows };
1023
+ if (workflows.some((w) => w.reason === "read_failed")) {
1024
+ return { coverage: "could_not_tell", reason: "read_failed", workflows };
1025
+ }
1026
+ const unresolved = workflows.find((w) => w.coverage === "could_not_tell");
1027
+ if (unresolved)
1028
+ return { coverage: "could_not_tell", reason: unresolved.reason, workflows };
1029
+ const excluded = workflows.some((w) => w.reason === "branch_filter_excludes_epic");
1030
+ return {
1031
+ coverage: "does_not_cover",
1032
+ reason: excluded ? "branch_filter_excludes_epic" : "no_pr_trigger",
1033
+ workflows,
1034
+ };
1035
+ }
915
1036
  /** Closed reason tokens returned by the `gh` functions. */
916
1037
  export const EPIC_BRANCH_RULES_REASONS = [
917
1038
  "invalid_input",
@@ -926,6 +1047,10 @@ export const EPIC_BRANCH_RULES_REASONS = [
926
1047
  "approvals_required",
927
1048
  "approvals_not_required",
928
1049
  "no_rules_apply",
1050
+ // Additive closed token: GitHub's definitive "your plan has no rulesets / branch
1051
+ // protection here" answer. It is confirmed absence of applicable rules, unlike
1052
+ // the other non-200 reasons, which are uncertainty.
1053
+ "plan_unavailable",
929
1054
  ];
930
1055
  /** Closed reason tokens for {@link assessApprovalExemptionScope} / {@link applyApprovalExemption}. */
931
1056
  export const EXEMPTION_REASONS = [
@@ -968,7 +1093,16 @@ function encodeBranchPath(branch) {
968
1093
  return branch.split("/").map(encodeURIComponent).join("/");
969
1094
  }
970
1095
  const GH_JSON_HEADERS = ["-H", "Accept: application/vnd.github+json", "-H", "X-GitHub-Api-Version: 2022-11-28"];
971
- /** Run `gh api ...` and parse JSON. Output text is inspected for status only and never returned. */
1096
+ /**
1097
+ * The one definitive plan-limit reply: HTTP 403 whose message, right after the
1098
+ * `gh: ` wrapper at the very start of stderr, begins with this text. Anchored so
1099
+ * a bare 403 or an unrelated "upgrade" message never matches.
1100
+ */
1101
+ const PLAN_LIMIT_STDERR_RE = /^\s*gh: Upgrade to GitHub Pro or make this repository public/;
1102
+ /**
1103
+ * Run `gh api ...` and parse JSON. Output text is inspected for the HTTP status and
1104
+ * the exact plan-limit message condition only, and is never returned.
1105
+ */
972
1106
  async function ghApiJson(run, args, input) {
973
1107
  let result;
974
1108
  try {
@@ -983,6 +1117,9 @@ async function ghApiJson(run, args, input) {
983
1117
  const err = typeof result.stderr === "string" ? result.stderr : "";
984
1118
  if (/HTTP 404\b/.test(err))
985
1119
  return { ok: false, reason: "not_found" };
1120
+ if (/HTTP 403\b/.test(err) && PLAN_LIMIT_STDERR_RE.test(err) && !/HTTP (?:401|404|500)\b/.test(err)) {
1121
+ return { ok: false, reason: "plan_unavailable" };
1122
+ }
986
1123
  if (/HTTP 401\b|gh auth login|not logged in|authentication required/i.test(err)) {
987
1124
  return { ok: false, reason: "gh_unauthenticated" };
988
1125
  }
@@ -1076,6 +1213,8 @@ export async function readDefaultBranchClassicContextViaGh(run, ownerRepo) {
1076
1213
  const branchPath = encodeBranchPath(defaultBranch);
1077
1214
  const prot = await ghApiJson(run, [`repos/${ownerRepo}/branches/${branchPath}/protection`]);
1078
1215
  if (!prot.ok) {
1216
+ if (prot.reason === "plan_unavailable")
1217
+ return { defaultBranch, approvals: "no", count: null };
1079
1218
  if (prot.reason !== "not_found")
1080
1219
  return unknown;
1081
1220
  const exists = await branchExists(run, ownerRepo, defaultBranch);
@@ -1097,6 +1236,84 @@ export async function readDefaultBranchClassicContextViaGh(run, ownerRepo) {
1097
1236
  return NO_DEFAULT_CONTEXT;
1098
1237
  }
1099
1238
  }
1239
+ /**
1240
+ * Read the effective required status checks on the default branch, from BOTH
1241
+ * sources GitHub enforces: repository rulesets (`rules/branches/<branch>`) and
1242
+ * classic branch protection. GET-only and bounded; it authorizes no rule change and
1243
+ * returns no response text. Any call that gives no reliable answer is `unknown`, so
1244
+ * a caller can warn on a confirmed requirement and stay silent on an uncertain one.
1245
+ * Never throws.
1246
+ */
1247
+ export async function readDefaultBranchRequiredChecksViaGh(run, ownerRepo, knownDefaultBranch) {
1248
+ try {
1249
+ if (!validOwnerRepo(ownerRepo))
1250
+ return { status: "unknown", reason: "invalid_input" };
1251
+ let defaultBranch = knownDefaultBranch;
1252
+ if (defaultBranch === undefined) {
1253
+ const repo = await ghApiJson(run, [`repos/${ownerRepo}`]);
1254
+ if (!repo.ok)
1255
+ return { status: "unknown", reason: repo.reason };
1256
+ if (!isRecord(repo.json) || typeof repo.json.default_branch !== "string") {
1257
+ return { status: "unknown", reason: "unparseable_response" };
1258
+ }
1259
+ defaultBranch = repo.json.default_branch;
1260
+ }
1261
+ if (!validBranch(defaultBranch))
1262
+ return { status: "unknown", reason: "invalid_input" };
1263
+ const rules = await readBranchRules(run, ownerRepo, defaultBranch);
1264
+ // A plan-limited source is an empty source, judged independently of the other.
1265
+ const rulesPlanLimited = !rules.ok && rules.reason === "plan_unavailable";
1266
+ if (!rules.ok && !rulesPlanLimited) {
1267
+ return { status: "unknown", reason: rules.reason };
1268
+ }
1269
+ const checks = new Set(rules.ok ? rules.rules.statusChecks : []);
1270
+ const protection = await ghApiJson(run, [
1271
+ `repos/${ownerRepo}/branches/${encodeBranchPath(defaultBranch)}/protection`,
1272
+ ]);
1273
+ if (protection.ok) {
1274
+ if (!isRecord(protection.json))
1275
+ return { status: "unknown", reason: "unparseable_response" };
1276
+ const required = protection.json.required_status_checks;
1277
+ if (required !== undefined && required !== null) {
1278
+ if (!isRecord(required))
1279
+ return { status: "unknown", reason: "malformed_rule" };
1280
+ const contexts = required.contexts;
1281
+ if (contexts !== undefined) {
1282
+ if (!Array.isArray(contexts) || contexts.some((c) => typeof c !== "string")) {
1283
+ return { status: "unknown", reason: "malformed_rule" };
1284
+ }
1285
+ for (const c of contexts)
1286
+ checks.add(c);
1287
+ }
1288
+ const entries = required.checks;
1289
+ if (entries !== undefined) {
1290
+ if (!Array.isArray(entries) || entries.some((c) => !isRecord(c) || typeof c.context !== "string")) {
1291
+ return { status: "unknown", reason: "malformed_rule" };
1292
+ }
1293
+ for (const c of entries)
1294
+ checks.add(c.context);
1295
+ }
1296
+ }
1297
+ }
1298
+ else if (protection.reason === "plan_unavailable") {
1299
+ // Empty source; the rules source (if readable) already supplied its checks.
1300
+ }
1301
+ else if (protection.reason === "not_found") {
1302
+ // A 404 can mean "no classic protection" or "cannot see it": only a branch
1303
+ // proven visible makes it the former.
1304
+ if ((await branchExists(run, ownerRepo, defaultBranch)) !== true) {
1305
+ return { status: "unknown", reason: "not_found" };
1306
+ }
1307
+ }
1308
+ else {
1309
+ return { status: "unknown", reason: protection.reason };
1310
+ }
1311
+ return { status: "known", defaultBranch, requiredChecks: [...checks].sort(byCodeUnit) };
1312
+ }
1313
+ catch {
1314
+ return { status: "unknown", reason: "gh_failed" };
1315
+ }
1316
+ }
1100
1317
  function withDefaultContext(f, c) {
1101
1318
  return {
1102
1319
  ...f,
@@ -1125,8 +1342,11 @@ function rulesCouldNotTell(branch, reason) {
1125
1342
  * a branch with that name will be returned"
1126
1343
  * (https://docs.github.com/en/rest/repos/rules#get-rules-for-a-branch). That is
1127
1344
  * documented but not yet live-verified (Bridge-GPT/bridge-api is a private
1128
- * repository whose plan lacks rulesets), hence `inferred`. A non-200 reply,
1129
- * including the 403 a plan without rulesets returns, stays `could_not_tell`. It also
1345
+ * repository whose plan lacks rulesets), hence `inferred`. The one definitive
1346
+ * exception is the exact plan-limit 403 ("Upgrade to GitHub Pro or make this
1347
+ * repository public"): the plan has no rules, so that is `no` / `exact` with reason
1348
+ * `plan_unavailable`. Every other non-200 reply (a bare 403, permissions, SSO, rate
1349
+ * limit, 500) is uncertainty and stays `could_not_tell`. It also
1130
1350
  * attaches the default branch's classic protection as separately labelled
1131
1351
  * CONTEXT ({@link readDefaultBranchClassicContextViaGh}); that context never
1132
1352
  * changes `approvalsRequired`. Never throws.
@@ -1148,6 +1368,17 @@ export async function readEpicBranchRulesViaGh(run, ownerRepo, branch) {
1148
1368
  }
1149
1369
  async function readEpicBranchRulesOnly(run, ownerRepo, branch) {
1150
1370
  const read = await readBranchRules(run, ownerRepo, branch);
1371
+ if (!read.ok && read.reason === "plan_unavailable") {
1372
+ // Definitive: the plan has no rules to apply. No branch probe is needed.
1373
+ return {
1374
+ branch,
1375
+ approvalsRequired: "no",
1376
+ requiredApprovingReviewCount: null,
1377
+ requiredStatusChecks: [],
1378
+ observationQuality: "exact",
1379
+ reason: "plan_unavailable",
1380
+ };
1381
+ }
1151
1382
  if (!read.ok)
1152
1383
  return rulesCouldNotTell(branch, read.reason);
1153
1384
  const { pullRequestRules, statusChecks } = read.rules;
@@ -1240,8 +1471,12 @@ export async function assessApprovalExemptionScope(run, ownerRepo, branch) {
1240
1471
  if (!validOwnerRepo(ownerRepo) || !validBranch(branch))
1241
1472
  return ineligible("rules_unreadable");
1242
1473
  const read = await readBranchRules(run, ownerRepo, branch);
1243
- if (!read.ok)
1474
+ if (!read.ok) {
1475
+ // A plan without rulesets has no approval rule to exempt; no ruleset details are read.
1476
+ if (read.reason === "plan_unavailable")
1477
+ return ineligible("no_approval_rule");
1244
1478
  return ineligible(read.reason === "malformed_rule" ? "malformed_rule" : "rules_unreadable");
1479
+ }
1245
1480
  const prRules = read.rules.pullRequestRules;
1246
1481
  if (prRules.length === 0)
1247
1482
  return ineligible("no_approval_rule");
@@ -1277,6 +1512,8 @@ function sameList(a, b) {
1277
1512
  * Never attempts a repair. The caller must have the user's explicit consent.
1278
1513
  */
1279
1514
  export async function applyApprovalExemption(run, ownerRepo, branch, assessment) {
1515
+ // Pre-write refusals return attempted:false; once the PUT loop may start, or an
1516
+ // exception is caught, the write is conservatively treated as attempted.
1280
1517
  try {
1281
1518
  if (!assessment ||
1282
1519
  assessment.eligible !== true ||
@@ -1285,35 +1522,36 @@ export async function applyApprovalExemption(run, ownerRepo, branch, assessment)
1285
1522
  !assessment.rulesetIds.every(isNonNegInt) ||
1286
1523
  !validOwnerRepo(ownerRepo) ||
1287
1524
  !validBranch(branch)) {
1288
- return { confirmed: false, reason: "scope_not_eligible" };
1525
+ return { confirmed: false, reason: "scope_not_eligible", attempted: false };
1289
1526
  }
1290
1527
  const before = await readBranchRules(run, ownerRepo, branch);
1291
1528
  if (!before.ok)
1292
- return { confirmed: false, reason: "ruleset_changed" };
1529
+ return { confirmed: false, reason: "ruleset_changed", attempted: false };
1293
1530
  const bodies = [];
1294
1531
  for (const id of assessment.rulesetIds) {
1295
1532
  const check = await checkRuleset(run, ownerRepo, id);
1296
1533
  if (!check.ok)
1297
- return { confirmed: false, reason: "ruleset_changed" };
1534
+ return { confirmed: false, reason: "ruleset_changed", attempted: false };
1298
1535
  bodies.push({ id, body: check.body });
1299
1536
  }
1300
1537
  for (const { id, body } of bodies) {
1301
1538
  const put = await ghApiJson(run, ["--method", "PUT", `repos/${ownerRepo}/rulesets/${id}`, "--input", "-"], JSON.stringify(body));
1302
1539
  if (!put.ok)
1303
- return { confirmed: false, reason: "apply_failed" };
1540
+ return { confirmed: false, reason: "apply_failed", attempted: true };
1304
1541
  }
1305
1542
  const after = await readBranchRules(run, ownerRepo, branch);
1306
1543
  if (!after.ok)
1307
- return { confirmed: false, reason: "verification_failed" };
1308
- if (after.rules.pullRequestRules.length > 0)
1309
- return { confirmed: false, reason: "approvals_still_required" };
1544
+ return { confirmed: false, reason: "verification_failed", attempted: true };
1545
+ if (after.rules.pullRequestRules.length > 0) {
1546
+ return { confirmed: false, reason: "approvals_still_required", attempted: true };
1547
+ }
1310
1548
  if (!sameList(before.rules.statusChecks, after.rules.statusChecks)) {
1311
- return { confirmed: false, reason: "status_checks_changed" };
1549
+ return { confirmed: false, reason: "status_checks_changed", attempted: true };
1312
1550
  }
1313
- return { confirmed: true, reason: "confirmed" };
1551
+ return { confirmed: true, reason: "confirmed", attempted: true };
1314
1552
  }
1315
1553
  catch {
1316
- return { confirmed: false, reason: "verification_failed" };
1554
+ return { confirmed: false, reason: "verification_failed", attempted: true };
1317
1555
  }
1318
1556
  }
1319
1557
  /**