@bridge_gpt/mcp-server 0.2.69 → 0.2.71

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 (164) hide show
  1. package/README.md +140 -292
  2. package/build/agent-launchers/claude-executor-adapter.js +27 -5
  3. package/build/agent-registry.js +5 -5
  4. package/build/agents.generated.js +15 -5
  5. package/build/artifact-diff.js +110 -0
  6. package/build/bin-entrypoint.js +36 -0
  7. package/build/bounded-wait.js +9 -1
  8. package/build/bridge-permissions.js +36 -9
  9. package/build/claude-review-workflow-drift.js +3 -3
  10. package/build/codex-bridge-profile.js +1 -1
  11. package/build/commands.generated.js +6 -6
  12. package/build/conductor/bridge-api-client.js +858 -21
  13. package/build/conductor/bring-up-facts.js +9 -9
  14. package/build/conductor/command-drift-production.js +7 -2
  15. package/build/conductor/doctor.js +472 -18
  16. package/build/conductor/epic-child-continuation.js +54 -3
  17. package/build/conductor/epic-implementer-cli.js +136 -34
  18. package/build/conductor/epic-runtime.js +2 -2
  19. package/build/conductor/epic-state.js +1 -1
  20. package/build/conductor/gate-discovery.js +524 -10
  21. package/build/conductor/gate-settings-review.js +451 -0
  22. package/build/conductor/hook-runtime-qualification.js +1 -1
  23. package/build/conductor/install-doctor.js +4 -2
  24. package/build/conductor/launch-need-probes.js +342 -0
  25. package/build/conductor/launch-plan.js +145 -0
  26. package/build/conductor/local-merge.js +241 -56
  27. package/build/conductor/merge-credential.js +172 -0
  28. package/build/conductor/merge-execution-eligibility.js +1 -1
  29. package/build/conductor/merged-child-check.js +95 -0
  30. package/build/conductor/plan-trim.js +30 -0
  31. package/build/conductor/pr-ci-producer.js +1 -1
  32. package/build/conductor/precheck-timings.js +14 -0
  33. package/build/conductor/preview-observations.js +10 -0
  34. package/build/conductor/readiness-cli.js +11 -6
  35. package/build/conductor/readiness-prerequisites.js +41 -0
  36. package/build/conductor/readiness-sections.js +348 -55
  37. package/build/conductor/readiness.js +99 -87
  38. package/build/conductor/recovery-cli.js +665 -23
  39. package/build/conductor/recovery-operations.js +350 -8
  40. package/build/conductor/repair-contract.js +47 -2
  41. package/build/conductor/repository-workflow-remedy.js +8 -6
  42. package/build/conductor/stored-merge-credential.js +76 -0
  43. package/build/conductor/stored-merge-token-probe.js +28 -0
  44. package/build/conductor/temporary-plan.js +50 -0
  45. package/build/conductor/wait-run-cli.js +759 -0
  46. package/build/conductor/worker-database-doctor-inspector.js +49 -0
  47. package/build/conductor-bin.js +2 -2
  48. package/build/conductor-claude-hook-bin.js +2 -2
  49. package/build/conductor-claude-hook-removed-stub-bin.js +2 -5
  50. package/build/conductor-removed-stub-bin.js +2 -5
  51. package/build/credential-store.js +356 -13
  52. package/build/docs.generated.js +26 -13
  53. package/build/doctor.js +94 -32
  54. package/build/drive-epic-help.js +212 -0
  55. package/build/drive-epic.js +1989 -379
  56. package/build/epic-ci-workflow-drift.js +125 -0
  57. package/build/epic-ci-workflow-install.js +240 -0
  58. package/build/epic-ci-workflow-v1.js +132 -0
  59. package/build/epic-ci-workflow.js +318 -0
  60. package/build/epic-contract-context.js +326 -0
  61. package/build/epic-implementer-bin.js +58 -42
  62. package/build/epic-implementer-claude-hook-bin.js +2 -2
  63. package/build/escalation-reachability.js +134 -0
  64. package/build/executor/ci-rerun-job.js +220 -14
  65. package/build/executor/cleanup-report.js +99 -0
  66. package/build/executor/cli.js +12 -1
  67. package/build/executor/completion-contract.js +66 -11
  68. package/build/executor/completion-supervision.js +99 -0
  69. package/build/executor/delivery-outcome.js +179 -0
  70. package/build/executor/deps.js +3 -0
  71. package/build/executor/env.js +276 -1
  72. package/build/executor/failure-reason-codes.js +126 -0
  73. package/build/executor/gate-qualification-job.js +20 -5
  74. package/build/executor/heartbeat.js +38 -0
  75. package/build/executor/http-client.js +72 -0
  76. package/build/executor/integration-job.js +298 -5
  77. package/build/executor/job-errors.js +36 -6
  78. package/build/executor/job-runner.js +830 -45
  79. package/build/executor/likely-tests.js +42 -3
  80. package/build/executor/merge-job.js +145 -2
  81. package/build/executor/observation.js +73 -4
  82. package/build/executor/permissions.js +193 -97
  83. package/build/executor/preflight.js +19 -3
  84. package/build/executor/process-containment.js +46 -2
  85. package/build/executor/prompt-spec.js +113 -9
  86. package/build/executor/rebase-finalization.js +38 -3
  87. package/build/executor/reconcile-overlap-job.js +41 -8
  88. package/build/executor/required-work-ledger.js +236 -81
  89. package/build/executor/resume-pre-spawn.js +38 -1
  90. package/build/executor/runner.js +91 -3
  91. package/build/executor/test-clock.js +8 -0
  92. package/build/executor/types.js +37 -2
  93. package/build/executor/worker-cli-launcher.js +259 -0
  94. package/build/executor/worker-command.js +177 -5
  95. package/build/executor/worker-database-target.js +91 -0
  96. package/build/executor/worker-database.js +927 -0
  97. package/build/executor/worker-finalization.js +106 -0
  98. package/build/executor/worker-guard-hook.js +203 -0
  99. package/build/executor/worker-resources.js +58 -0
  100. package/build/executor/worker-verification-guidance.js +44 -2
  101. package/build/executor/worktree.js +303 -15
  102. package/build/executor-launch.js +11 -1
  103. package/build/generated-assets-doctor.js +9 -1
  104. package/build/generated-assets-health.js +13 -4
  105. package/build/generated-assets-legacy.js +39 -0
  106. package/build/generated-assets-paths.js +12 -1
  107. package/build/generated-assets-repair.js +4 -1
  108. package/build/generated-assets-test-support.js +12 -0
  109. package/build/github-remote.js +33 -0
  110. package/build/github-sign-in.js +62 -0
  111. package/build/index.js +1133 -546
  112. package/build/init.js +39 -1
  113. package/build/install-bridge-conductor-gate.js +270 -11
  114. package/build/install-bridge-conductor-setup.js +427 -0
  115. package/build/install-bridge-conductor.js +1545 -268
  116. package/build/install-bridge.js +376 -60
  117. package/build/mcp-install-state.js +8 -2
  118. package/build/merge-pull-request.js +6 -0
  119. package/build/merge-workflow-install.js +782 -344
  120. package/build/phase-result-artifacts.js +75 -20
  121. package/build/pipeline-orchestrator.js +247 -4
  122. package/build/pipelines.generated.js +35 -35
  123. package/build/plan-discovery.js +15 -2
  124. package/build/plan-execution-ledger.js +321 -17
  125. package/build/plan-phase-routing.js +48 -13
  126. package/build/plane/cli.js +105 -51
  127. package/build/plane/defaults.js +3 -0
  128. package/build/plane/plane-freshness.js +298 -0
  129. package/build/plane/preflight.js +19 -0
  130. package/build/plane/schema-compatibility.js +137 -0
  131. package/build/plane/shutdown-deps.js +38 -0
  132. package/build/plane/shutdown.js +83 -8
  133. package/build/plane/terminal-run-cleanup.js +303 -0
  134. package/build/printed-remedy-commands.js +33 -4
  135. package/build/readiness-check.js +6 -2
  136. package/build/readiness-prerequisites.generated.js +532 -0
  137. package/build/readme.generated.js +1 -1
  138. package/build/repository-toolchains.js +581 -0
  139. package/build/setup-epic.js +421 -34
  140. package/build/shared-resources.generated.js +5 -0
  141. package/build/start-tickets.js +28 -15
  142. package/build/test-command-detection.js +284 -0
  143. package/build/test-command-validation.js +73 -0
  144. package/build/testing-config-client.js +150 -0
  145. package/build/ticket-delivery-artifacts.js +265 -0
  146. package/build/ticket-delivery-enrollment.js +369 -0
  147. package/build/ticket-delivery.js +205 -0
  148. package/build/ticket-review-summary.js +14 -0
  149. package/build/upgrade-cli.js +13 -0
  150. package/build/v2-readiness-requirements.js +552 -0
  151. package/build/version.generated.js +3 -3
  152. package/build/worker-guard-hook-bin.js +2 -2
  153. package/build/worktree-core.js +22 -7
  154. package/docs/BRIDGE-DIRECTORY-README.md +48 -0
  155. package/docs/EPIC-IMPLEMENTER-INSTALL.md +12 -867
  156. package/docs/EPIC-IMPLEMENTER.md +142 -63
  157. package/docs/epic-implementer/getting-started.md +187 -0
  158. package/docs/epic-implementer/self-hosting.md +133 -0
  159. package/package.json +4 -4
  160. package/pipelines/idea-to-ticket.json +1 -1
  161. package/pipelines/implement-ticket.json +8 -8
  162. package/pipelines/review-ticket.json +2 -2
  163. package/design-assets/favicon/favicon-32x32.png +0 -0
  164. package/design-assets/just-logo-rough-draft.png +0 -0
@@ -43,9 +43,19 @@
43
43
  * Bridge API repository's own layout.
44
44
  * - Deterministic order everywhere: file names sorted by code unit, jobs in file
45
45
  * order, local candidates in a fixed order.
46
+ *
47
+ * ## Worker variables and reproduction hints (BAPI-1520)
48
+ *
49
+ * Discovery also proposes two optional, owner-confirmed settings. Both are
50
+ * conservative to the point of omission: a literal is proposed only when this
51
+ * scanner is certain of it, and anything it cannot read with certainty is left
52
+ * out (by NAME only, never by value). The proposal's values live in memory for
53
+ * the interactive confirmation and nowhere else: they are not copied into
54
+ * {@link GateEvidence}, summaries, reports, or MCP output.
46
55
  */
47
56
  import fs from "node:fs/promises";
48
57
  import path from "node:path";
58
+ import { LEGACY_EPIC_IMPLEMENTER_NODE_PATH_ENV } from "../epic-implementer-env.js";
49
59
  // ---------------------------------------------------------------------------
50
60
  // Bounds and fixed vocabularies
51
61
  // ---------------------------------------------------------------------------
@@ -66,6 +76,69 @@ export const JENKINS_LOOKUP_NAMES = [
66
76
  ];
67
77
  /** The script body `npm init` writes; it is a placeholder, not a test command. */
68
78
  export const NPM_PLACEHOLDER_TEST_SCRIPT = 'echo "Error: no test specified" && exit 1';
79
+ /** BAPI-1520 — at most this many worker-variable names are proposed. */
80
+ export const MAX_WORKER_ENV_CANDIDATES = 32;
81
+ /** BAPI-1520 — the longest value, in UTF-8 bytes, that may be proposed. */
82
+ export const MAX_WORKER_ENV_VALUE_BYTES = 1024;
83
+ /** BAPI-1520 — the name syntax the server enforces. */
84
+ export const WORKER_ENV_NAME_PATTERN = /^[A-Z_][A-Z0-9_]{0,63}$/;
85
+ /**
86
+ * BAPI-1520 — names a confirmed worker variable may never use. This is a COPY:
87
+ * `api/library/epic_implementer/gate_profile.py` (`WORKER_ENV_RESERVED_NAMES`)
88
+ * is the authority, and a drift test compares the two sorted lists.
89
+ */
90
+ export const WORKER_ENV_RESERVED_NAMES = [
91
+ "BASH_ENV",
92
+ "BRIDGE_SKIP_PREPUSH",
93
+ "CI",
94
+ LEGACY_EPIC_IMPLEMENTER_NODE_PATH_ENV,
95
+ "DATABASE_URL",
96
+ "EPIC_IMPLEMENTER_NODE_PATH",
97
+ "GITHUB_MERGE_TOKEN",
98
+ "GITHUB_TOKEN",
99
+ "GH_TOKEN",
100
+ "HOME",
101
+ "LANG",
102
+ "LC_ALL",
103
+ "LD_PRELOAD",
104
+ "LOGNAME",
105
+ "MAX_MCP_OUTPUT_TOKENS",
106
+ "NODE_OPTIONS",
107
+ "NO_COLOR",
108
+ "OPENAI_API_KEY",
109
+ "PATH",
110
+ "PGDATABASE",
111
+ "PGHOST",
112
+ "PGPASSFILE",
113
+ "PGPASSWORD",
114
+ "PGPORT",
115
+ "PGSERVICE",
116
+ "PGUSER",
117
+ "POSTGRES_DATABASE",
118
+ "POSTGRES_DB",
119
+ "POSTGRES_HOST",
120
+ "POSTGRES_PASSWORD",
121
+ "POSTGRES_PORT",
122
+ "POSTGRES_USER",
123
+ "SHELL",
124
+ "TEMP",
125
+ "TERM",
126
+ "TMP",
127
+ "TMPDIR",
128
+ "USER",
129
+ "XDG_CONFIG_HOME",
130
+ ];
131
+ /** BAPI-1520 — reserved name prefixes; a copy of `WORKER_ENV_RESERVED_PREFIXES`. */
132
+ export const WORKER_ENV_RESERVED_PREFIXES = [
133
+ "ANTHROPIC_",
134
+ "BAPI_",
135
+ "BRIDGE_",
136
+ "CLAUDE_",
137
+ "DYLD_",
138
+ "GIT_CONFIG_",
139
+ ];
140
+ /** BAPI-1520 — case-insensitive fragments that make a name secret-looking. */
141
+ export const WORKER_ENV_SECRET_FRAGMENTS = ["TOKEN", "SECRET", "PASSWORD", "API_KEY"];
69
142
  const WORKFLOWS_DIR = ".github/workflows";
70
143
  const CIRCLECI_CONFIG = ".circleci/config.yml";
71
144
  /**
@@ -312,7 +385,7 @@ export function parseInlineScalar(rest) {
312
385
  * also recognised anywhere beneath `strategy:`). Anything the scanner cannot
313
386
  * read confidently marks the job `unclear` rather than guessing a name.
314
387
  */
315
- export function scanJobsBlock(text) {
388
+ export function scanJobsBlock(text, options = {}) {
316
389
  const lines = significantLines(text);
317
390
  const start = lines.findIndex((l) => {
318
391
  if (l.indent !== 0)
@@ -333,6 +406,10 @@ export function scanJobsBlock(text) {
333
406
  let childIndent = null;
334
407
  let inStrategy = false;
335
408
  let afterName = false;
409
+ // Step tracking (BAPI-1520); only consulted when `options.structure` is set.
410
+ let inSteps = false;
411
+ let stepIndent = null;
412
+ let stepKeyIndent = null;
336
413
  for (let i = start + 1; i < lines.length; i++) {
337
414
  const line = lines[i];
338
415
  if (line.indent === 0)
@@ -355,12 +432,23 @@ export function scanJobsBlock(text) {
355
432
  childIndent = null;
356
433
  inStrategy = false;
357
434
  afterName = false;
435
+ inSteps = false;
436
+ stepIndent = null;
437
+ stepKeyIndent = null;
358
438
  if (kv === null) {
359
439
  unclear = true;
360
440
  current = null;
361
441
  continue;
362
442
  }
363
- current = { key: kv.key, name: null, matrix: false, expression: false, reusable: false, unclear: kv.rest !== "" };
443
+ current = {
444
+ key: kv.key,
445
+ name: null,
446
+ matrix: false,
447
+ expression: false,
448
+ reusable: false,
449
+ unclear: kv.rest !== "",
450
+ structure: { runSteps: 0, runCommand: null, blocked: false, opaque: false },
451
+ };
364
452
  jobs.push(current);
365
453
  continue;
366
454
  }
@@ -374,9 +462,13 @@ export function scanJobsBlock(text) {
374
462
  if (line.indent === childIndent) {
375
463
  inStrategy = false;
376
464
  afterName = false;
465
+ inSteps = false;
466
+ stepIndent = null;
467
+ stepKeyIndent = null;
377
468
  const kv = splitKeyLine(line.text);
378
469
  if (kv === null) {
379
470
  current.unclear = true;
471
+ current.structure.opaque = true;
380
472
  continue;
381
473
  }
382
474
  if (kv.key === "name") {
@@ -404,6 +496,12 @@ export function scanJobsBlock(text) {
404
496
  else if (kv.key === "uses") {
405
497
  current.reusable = true;
406
498
  }
499
+ else if (kv.key === "steps" && kv.rest === "") {
500
+ inSteps = true;
501
+ }
502
+ else if (kv.key === "container" || kv.key === "defaults") {
503
+ current.structure.blocked = true;
504
+ }
407
505
  continue;
408
506
  }
409
507
  // Deeper than the job's direct children.
@@ -414,6 +512,18 @@ export function scanJobsBlock(text) {
414
512
  }
415
513
  if (inStrategy && splitKeyLine(line.text)?.key === "matrix")
416
514
  current.matrix = true;
515
+ if (options.structure === true && inSteps) {
516
+ const isItem = line.text.startsWith("-") && (stepIndent === null || line.indent === stepIndent);
517
+ if (isItem) {
518
+ stepIndent = line.indent;
519
+ const after = line.text.slice(1);
520
+ stepKeyIndent = line.indent + 1 + (after.length - after.trimStart().length);
521
+ noteStepKey(current.structure, splitKeyLine(after.trim()));
522
+ }
523
+ else if (stepKeyIndent !== null && line.indent === stepKeyIndent) {
524
+ noteStepKey(current.structure, splitKeyLine(line.text));
525
+ }
526
+ }
417
527
  }
418
528
  return {
419
529
  found: true,
@@ -430,15 +540,33 @@ export function scanJobsBlock(text) {
430
540
  : j.reusable
431
541
  ? "reusable_workflow"
432
542
  : null,
543
+ ...(options.structure === true ? { structure: j.structure } : {}),
433
544
  })),
434
545
  };
435
546
  }
547
+ /** Record one step-level key into a job's {@link JobStructure}. */
548
+ function noteStepKey(structure, kv) {
549
+ if (kv === null) {
550
+ structure.opaque = true;
551
+ return;
552
+ }
553
+ if (kv.key === "run") {
554
+ structure.runSteps += 1;
555
+ if (structure.runSteps === 1) {
556
+ const parsed = kv.rest === "" ? { kind: "unclear" } : parseInlineScalar(kv.rest);
557
+ structure.runCommand = parsed.kind === "value" && parsed.value !== "" ? parsed.value : null;
558
+ }
559
+ }
560
+ else if (kv.key === "working-directory" || kv.key === "shell") {
561
+ structure.blocked = true;
562
+ }
563
+ }
436
564
  /**
437
565
  * A GitHub Actions workflow's jobs. The check-run name a job reports is its
438
566
  * `name:` when present, else its key — exact only when `variance` is null.
439
567
  */
440
- export function scanWorkflowJobs(text) {
441
- const scan = scanJobsBlock(text);
568
+ export function scanWorkflowJobs(text, options = {}) {
569
+ const scan = scanJobsBlock(text, options);
442
570
  return {
443
571
  ...scan,
444
572
  jobs: scan.jobs.map((j) => j.variance === null && (j.name ?? j.key).trim() === "" ? { ...j, variance: "unclear" } : j),
@@ -460,6 +588,310 @@ export function workflowJobReportedName(job) {
460
588
  export function circleCiStatusName(jobKey) {
461
589
  return `ci/circleci: ${jobKey.trim()}`;
462
590
  }
591
+ /** Plain scalars YAML would read as something other than a string. */
592
+ const NON_STRING_PLAIN = /^(?:~|null|true|false|yes|no|on|off|y|n)$/i;
593
+ /**
594
+ * Read one single-line env value as a string literal, or null when it is
595
+ * anything else. Deliberately stricter than {@link parseInlineScalar}: quoted
596
+ * values keep their exact characters (no trimming), and escapes, comments,
597
+ * expressions, anchors, aliases, tags, block scalars, flow collections, and
598
+ * plain scalars YAML could type as a number, boolean, null, or date are all
599
+ * refused rather than partly interpreted.
600
+ */
601
+ export function parseEnvScalar(rest) {
602
+ const text = rest.trim();
603
+ if (text === "")
604
+ return null;
605
+ let value;
606
+ if (text[0] === '"') {
607
+ const close = text.indexOf('"', 1);
608
+ if (close < 0 || text.slice(close + 1).trim() !== "")
609
+ return null;
610
+ value = text.slice(1, close);
611
+ if (value.includes("\\"))
612
+ return null;
613
+ }
614
+ else if (text[0] === "'") {
615
+ let out = "";
616
+ let closed = -1;
617
+ for (let i = 1; i < text.length; i++) {
618
+ if (text[i] === "'") {
619
+ if (text[i + 1] === "'") {
620
+ out += "'";
621
+ i++;
622
+ continue;
623
+ }
624
+ closed = i;
625
+ break;
626
+ }
627
+ out += text[i];
628
+ }
629
+ if (closed < 0 || text.slice(closed + 1).trim() !== "")
630
+ return null;
631
+ value = out;
632
+ }
633
+ else {
634
+ if (UNSUPPORTED_SCALAR_STARTS.has(text[0] ?? "") || /^[-?:](\s|$)/.test(text))
635
+ return null;
636
+ if (/\s#/.test(text) || text.includes("\t"))
637
+ return null;
638
+ if (/:(\s|$)/.test(text))
639
+ return null;
640
+ if (NON_STRING_PLAIN.test(text))
641
+ return null;
642
+ // Numbers, signed numbers, dates and `.inf`/`.nan` all start with a digit, sign, or dot.
643
+ if (/^[-+.\d]/.test(text))
644
+ return null;
645
+ value = text;
646
+ }
647
+ if (value.includes("${{") || value.includes("$(") || value.includes("${"))
648
+ return null;
649
+ return value;
650
+ }
651
+ /** Display-safe name for a skipped declaration; anything stranger is not named. */
652
+ const SKIPPED_NAME_PATTERN = /^[A-Za-z_][A-Za-z0-9_.-]{0,63}$/;
653
+ /**
654
+ * Read the block map whose key line is `lines[at]`, appending each entry to
655
+ * `out`. Entries are read at the first nested indentation; a deeper line (a
656
+ * nested structure or continuation) or an unreadable line marks the entry — or,
657
+ * when no entry can be named, the whole block — unsupported. Returns whether
658
+ * the block was fully readable.
659
+ */
660
+ function readEnvMap(lines, at, parentIndent, out) {
661
+ let readable = true;
662
+ let entryIndent = null;
663
+ let last = null;
664
+ for (let i = at + 1; i < lines.length && lines[i].indent > parentIndent; i++) {
665
+ const line = lines[i];
666
+ if (line.tabIndent) {
667
+ readable = false;
668
+ last = null;
669
+ continue;
670
+ }
671
+ entryIndent ??= line.indent;
672
+ if (line.indent > entryIndent) {
673
+ // Nested structure or a continued scalar: the entry above is not a plain literal.
674
+ if (last)
675
+ last.value = null;
676
+ else
677
+ readable = false;
678
+ continue;
679
+ }
680
+ if (line.indent < entryIndent) {
681
+ readable = false;
682
+ last = null;
683
+ continue;
684
+ }
685
+ const kv = splitKeyLine(line.text);
686
+ if (kv === null || !SKIPPED_NAME_PATTERN.test(kv.key)) {
687
+ readable = false;
688
+ last = null;
689
+ continue;
690
+ }
691
+ last = { name: kv.key, value: parseEnvScalar(kv.rest) };
692
+ out.push(last);
693
+ }
694
+ return readable;
695
+ }
696
+ /**
697
+ * Scan block-style env maps: workflow-, job-, and step-level `env:` for GitHub
698
+ * Actions, job-level `environment:` for CircleCI. Flow-style, aliased, or
699
+ * otherwise unreadable maps are not read and set `unclear`.
700
+ */
701
+ export function scanEnvMaps(text, mode) {
702
+ const lines = significantLines(text);
703
+ const declarations = [];
704
+ let unclear = false;
705
+ const envKey = mode === "github" ? "env" : "environment";
706
+ const readBlock = (at, parentIndent, rest) => {
707
+ if (rest !== "") {
708
+ unclear = true;
709
+ return;
710
+ }
711
+ if (!readEnvMap(lines, at, parentIndent, declarations))
712
+ unclear = true;
713
+ };
714
+ if (mode === "github") {
715
+ lines.forEach((line, i) => {
716
+ if (line.indent !== 0)
717
+ return;
718
+ const kv = splitKeyLine(line.text);
719
+ if (kv?.key === envKey)
720
+ readBlock(i, 0, kv.rest);
721
+ });
722
+ }
723
+ const start = lines.findIndex((l) => l.indent === 0 && splitKeyLine(l.text)?.key === "jobs");
724
+ if (start < 0)
725
+ return { declarations, unclear };
726
+ let jobIndent = null;
727
+ let childIndent = null;
728
+ let inSteps = false;
729
+ let stepIndent = null;
730
+ let stepKeyIndent = null;
731
+ const stepKey = (kv, at, keyIndent) => {
732
+ if (kv?.key === envKey)
733
+ readBlock(at, keyIndent, kv.rest);
734
+ };
735
+ for (let i = start + 1; i < lines.length; i++) {
736
+ const line = lines[i];
737
+ if (line.indent === 0)
738
+ break;
739
+ if (line.tabIndent) {
740
+ unclear = true;
741
+ continue;
742
+ }
743
+ jobIndent ??= line.indent;
744
+ if (line.indent < jobIndent)
745
+ continue;
746
+ if (line.indent === jobIndent) {
747
+ childIndent = null;
748
+ inSteps = false;
749
+ stepIndent = null;
750
+ stepKeyIndent = null;
751
+ continue;
752
+ }
753
+ childIndent ??= line.indent;
754
+ if (line.indent < childIndent)
755
+ continue;
756
+ if (line.indent === childIndent && !line.text.startsWith("-")) {
757
+ inSteps = false;
758
+ stepIndent = null;
759
+ stepKeyIndent = null;
760
+ const kv = splitKeyLine(line.text);
761
+ if (kv?.key === envKey)
762
+ readBlock(i, childIndent, kv.rest);
763
+ else if (mode === "github" && kv?.key === "steps" && kv.rest === "")
764
+ inSteps = true;
765
+ continue;
766
+ }
767
+ if (mode !== "github" || !inSteps)
768
+ continue;
769
+ const isItem = line.text.startsWith("-") && (stepIndent === null || line.indent === stepIndent);
770
+ if (isItem) {
771
+ stepIndent = line.indent;
772
+ const after = line.text.slice(1);
773
+ stepKeyIndent = line.indent + 1 + (after.length - after.trimStart().length);
774
+ stepKey(splitKeyLine(after.trim()), i, stepKeyIndent);
775
+ }
776
+ else if (stepKeyIndent !== null && line.indent === stepKeyIndent) {
777
+ stepKey(splitKeyLine(line.text), i, stepKeyIndent);
778
+ }
779
+ }
780
+ return { declarations, unclear };
781
+ }
782
+ /** True when `name` is reserved (exactly, or by prefix) and may never be a worker variable. */
783
+ export function isReservedWorkerEnvName(name) {
784
+ return (WORKER_ENV_RESERVED_NAMES.includes(name) || WORKER_ENV_RESERVED_PREFIXES.some((prefix) => name.startsWith(prefix)));
785
+ }
786
+ /** True when `name` looks secret, so its literal needs the owner's individual confirmation. */
787
+ export function isSecretLookingName(name) {
788
+ const upper = name.toUpperCase();
789
+ return WORKER_ENV_SECRET_FRAGMENTS.some((fragment) => upper.includes(fragment));
790
+ }
791
+ /** True when `value` satisfies the server's value bounds (no NUL/CR/LF, at most 1,024 UTF-8 bytes). */
792
+ export function isAcceptableWorkerEnvValue(value) {
793
+ return !/[\u0000\r\n]/.test(value) && Buffer.byteLength(value, "utf8") <= MAX_WORKER_ENV_VALUE_BYTES;
794
+ }
795
+ /**
796
+ * Turn every observed declaration into proposals, conflicts, and name-only
797
+ * notices. A name is proposed only when ALL of its observed declarations are
798
+ * supported literals with one identical value; otherwise it is left out.
799
+ */
800
+ export function resolveWorkerEnv(observations) {
801
+ const byName = new Map();
802
+ for (const { source, declaration } of observations) {
803
+ const entry = byName.get(declaration.name) ?? { values: new Set(), sources: new Set(), unsupported: false };
804
+ if (declaration.value === null)
805
+ entry.unsupported = true;
806
+ else
807
+ entry.values.add(declaration.value);
808
+ entry.sources.add(source);
809
+ byName.set(declaration.name, entry);
810
+ }
811
+ const skipped = new Set();
812
+ const conflicts = new Set();
813
+ const eligible = [];
814
+ for (const [name, entry] of byName) {
815
+ if (!WORKER_ENV_NAME_PATTERN.test(name) || isReservedWorkerEnvName(name) || entry.unsupported) {
816
+ skipped.add(name);
817
+ continue;
818
+ }
819
+ if (entry.values.size > 1) {
820
+ conflicts.add(name);
821
+ continue;
822
+ }
823
+ const [value] = [...entry.values];
824
+ if (value === undefined || !isAcceptableWorkerEnvValue(value)) {
825
+ skipped.add(name);
826
+ continue;
827
+ }
828
+ eligible.push({
829
+ name,
830
+ value,
831
+ committedLiteral: isSecretLookingName(name),
832
+ sources: [...entry.sources].sort(byCodeUnit),
833
+ });
834
+ }
835
+ eligible.sort((a, b) => byCodeUnit(a.name, b.name));
836
+ return {
837
+ candidates: eligible.slice(0, MAX_WORKER_ENV_CANDIDATES),
838
+ conflicts: [...conflicts].sort(byCodeUnit),
839
+ skipped: [...skipped].sort(byCodeUnit),
840
+ overflow: Math.max(0, eligible.length - MAX_WORKER_ENV_CANDIDATES),
841
+ };
842
+ }
843
+ // ---------------------------------------------------------------------------
844
+ // Reproduction hints (BAPI-1520)
845
+ // ---------------------------------------------------------------------------
846
+ /** A token a recognised command may contain: no shell metacharacter at all. */
847
+ const SAFE_COMMAND_TOKEN = /^[A-Za-z0-9_][A-Za-z0-9_.:-]*$/;
848
+ /** Split a one-line command into tokens, or null when it is not plainly one command. */
849
+ function plainCommandTokens(command) {
850
+ const trimmed = command.trim();
851
+ if (trimmed === "" || /[\r\n]/.test(trimmed))
852
+ return null;
853
+ const tokens = trimmed.split(/\s+/).map((token) => {
854
+ // Matching quotes around a whole token are harmless; nothing else is.
855
+ const quoted = /^(["'])(.*)\1$/.exec(token);
856
+ return quoted ? (quoted[2] ?? "") : token;
857
+ });
858
+ return tokens.every((token) => SAFE_COMMAND_TOKEN.test(token)) ? tokens : null;
859
+ }
860
+ /**
861
+ * The structured argv a single-line CI command is exactly equal to, or null.
862
+ * Only `make <test|check|lint>` for a Makefile target that exists, and
863
+ * `npm`/`pnpm`/`yarn` invocations of a `package.json` script that exists, are
864
+ * recognised. Pipes, chaining, substitution, wrappers, and anything with an
865
+ * extra argument are not.
866
+ */
867
+ export function recognizeReproductionCommand(command, context) {
868
+ const tokens = plainCommandTokens(command);
869
+ if (tokens === null)
870
+ return null;
871
+ const [program, ...args] = tokens;
872
+ if (program === "make") {
873
+ const target = args[0];
874
+ if (args.length !== 1 || !MAKE_GATE_TARGETS.includes(target))
875
+ return null;
876
+ return context.makeTargets.includes(target) ? ["make", target] : null;
877
+ }
878
+ if (program !== "npm" && program !== "pnpm" && program !== "yarn")
879
+ return null;
880
+ let script;
881
+ if (args.length === 1 && args[0] !== "run" && args[0] !== "run-script") {
882
+ // `npm test`, `pnpm test`, `yarn lint`: the bare form names a script directly,
883
+ // except that npm only has such a shortcut for `test`.
884
+ if (program === "npm" && args[0] !== "test")
885
+ return null;
886
+ script = args[0];
887
+ }
888
+ else if (args.length === 2 && args[0] === "run") {
889
+ script = args[1];
890
+ }
891
+ if (script === undefined || !context.packageScripts.includes(script))
892
+ return null;
893
+ return packageScriptArgv(program, script);
894
+ }
463
895
  const MAKE_GATE_TARGETS = ["test", "check", "lint"];
464
896
  /**
465
897
  * Makefile targets named exactly `test`, `check`, or `lint`, in that fixed
@@ -597,7 +1029,7 @@ function jobDetail(job) {
597
1029
  detail += ` (name may vary: ${VARIANCE_TEXT[job.variance]})`;
598
1030
  return detail;
599
1031
  }
600
- async function collectWorkflows(fsSeam, evidence, lookups) {
1032
+ async function collectWorkflows(fsSeam, evidence, lookups, extras) {
601
1033
  const entries = await fsSeam.listDir(WORKFLOWS_DIR);
602
1034
  if (entries === null)
603
1035
  return;
@@ -610,7 +1042,11 @@ async function collectWorkflows(fsSeam, evidence, lookups) {
610
1042
  const text = await readBounded(fsSeam, source);
611
1043
  if (text === null)
612
1044
  continue;
613
- const scan = scanWorkflowJobs(text);
1045
+ const scan = scanWorkflowJobs(text, { structure: true });
1046
+ for (const declaration of scanEnvMaps(text, "github").declarations) {
1047
+ extras.envObservations.push({ source, declaration });
1048
+ }
1049
+ const workflowBlocked = significantLines(text).some((l) => l.indent === 0 && splitKeyLine(l.text)?.key === "defaults");
614
1050
  if (scan.jobs.length === 0) {
615
1051
  // A workflow file whose jobs could not be read is still evidence of provider CI.
616
1052
  evidence.push({
@@ -630,15 +1066,24 @@ async function collectWorkflows(fsSeam, evidence, lookups) {
630
1066
  nameMayVary: reported === null,
631
1067
  };
632
1068
  evidence.push(e);
633
- if (reported !== null)
1069
+ if (reported !== null) {
634
1070
  lookups.add(reported, e);
1071
+ if (job.structure) {
1072
+ const jobs = extras.hintJobs.get(reported) ?? [];
1073
+ jobs.push({ source, structure: job.structure, workflowBlocked });
1074
+ extras.hintJobs.set(reported, jobs);
1075
+ }
1076
+ }
635
1077
  }
636
1078
  }
637
1079
  }
638
- async function collectCircleCi(fsSeam, evidence, lookups) {
1080
+ async function collectCircleCi(fsSeam, evidence, lookups, extras) {
639
1081
  const text = await readBounded(fsSeam, CIRCLECI_CONFIG);
640
1082
  if (text === null)
641
1083
  return;
1084
+ for (const declaration of scanEnvMaps(text, "circleci").declarations) {
1085
+ extras.envObservations.push({ source: CIRCLECI_CONFIG, declaration });
1086
+ }
642
1087
  const scan = scanCircleCiJobs(text);
643
1088
  if (scan.jobs.length === 0) {
644
1089
  evidence.push({
@@ -843,8 +1288,9 @@ export async function discoverGate(input) {
843
1288
  const evidence = [];
844
1289
  const localCandidates = [];
845
1290
  const lookups = new LookupNames();
846
- await collectWorkflows(fsSeam, evidence, lookups);
847
- await collectCircleCi(fsSeam, evidence, lookups);
1291
+ const extras = { envObservations: [], hintJobs: new Map() };
1292
+ await collectWorkflows(fsSeam, evidence, lookups, extras);
1293
+ await collectCircleCi(fsSeam, evidence, lookups, extras);
848
1294
  await collectOtherProviders(fsSeam, evidence, lookups);
849
1295
  await collectHooks(fsSeam, evidence);
850
1296
  await collectPackageJson(fsSeam, evidence, localCandidates);
@@ -878,6 +1324,8 @@ export async function discoverGate(input) {
878
1324
  const validLocal = localCandidates.filter((c) => isGateCommandName(c.name));
879
1325
  const providerChecks = providerCandidates.map((c) => c.name);
880
1326
  const localCommandIds = providerChecks.length > 0 ? [] : validLocal.filter((c) => c.confidence === "high").map((c) => c.id);
1327
+ const workerEnv = resolveWorkerEnv(extras.envObservations);
1328
+ const hints = await proposeReproductionHints(fsSeam, providerChecks, extras.hintJobs);
881
1329
  return {
882
1330
  providerCandidates,
883
1331
  unreportedProviderNames,
@@ -886,5 +1334,71 @@ export async function discoverGate(input) {
886
1334
  history,
887
1335
  proposed: { kind: deriveProposalKind(providerChecks, localCommandIds), providerChecks, localCommandIds },
888
1336
  noCandidates: providerCandidates.length === 0 && validLocal.length === 0,
1337
+ workerEnvCandidates: workerEnv.candidates,
1338
+ workerEnvConflicts: workerEnv.conflicts,
1339
+ workerEnvSkipped: workerEnv.skipped,
1340
+ workerEnvOverflow: workerEnv.overflow,
1341
+ reproductionHints: hints.proposed,
1342
+ reproductionHintsSkipped: hints.skipped,
889
1343
  };
890
1344
  }
1345
+ /**
1346
+ * The Makefile targets and package scripts a CI command may name (BAPI-1520).
1347
+ * Read through the same bounded seam as every other file.
1348
+ */
1349
+ async function loadHintContext(fsSeam) {
1350
+ const makefile = await readBounded(fsSeam, "Makefile");
1351
+ const makeTargets = makefile === null ? [] : scanMakefileTargets(makefile);
1352
+ let packageScripts = [];
1353
+ const text = await readBounded(fsSeam, "package.json");
1354
+ if (text !== null) {
1355
+ try {
1356
+ const parsed = JSON.parse(text);
1357
+ const raw = typeof parsed === "object" && parsed !== null ? parsed.scripts : undefined;
1358
+ if (typeof raw === "object" && raw !== null && !Array.isArray(raw)) {
1359
+ const scripts = raw;
1360
+ packageScripts = PACKAGE_GATE_SCRIPTS.filter((s) => Object.prototype.hasOwnProperty.call(scripts, s) && isUsablePackageScript(scripts[s]));
1361
+ }
1362
+ }
1363
+ catch {
1364
+ // Unparseable package.json: no script is recognised.
1365
+ }
1366
+ }
1367
+ return { makeTargets, packageScripts };
1368
+ }
1369
+ /**
1370
+ * A hint exists only for a REPORTED provider check that maps to exactly one
1371
+ * workflow job, whose only `run:` step is one plain line equal to a recognised
1372
+ * Makefile target or package script, with nothing around it that an argv at the
1373
+ * worktree root could not reproduce. Every other mapped check is named, never
1374
+ * guessed at. A check with no workflow job (CircleCI, Jenkins) is not mentioned.
1375
+ */
1376
+ async function proposeReproductionHints(fsSeam, reportedChecks, hintJobs) {
1377
+ const proposed = [];
1378
+ const skipped = [];
1379
+ const mapped = reportedChecks.filter((check) => hintJobs.has(check));
1380
+ if (mapped.length === 0)
1381
+ return { proposed, skipped };
1382
+ const context = await loadHintContext(fsSeam);
1383
+ for (const check of mapped) {
1384
+ const jobs = hintJobs.get(check) ?? [];
1385
+ const job = jobs.length === 1 ? jobs[0] : undefined;
1386
+ const structure = job?.structure;
1387
+ const argv = job !== undefined &&
1388
+ structure !== undefined &&
1389
+ !job.workflowBlocked &&
1390
+ !structure.blocked &&
1391
+ !structure.opaque &&
1392
+ structure.runSteps === 1 &&
1393
+ structure.runCommand !== null
1394
+ ? recognizeReproductionCommand(structure.runCommand, context)
1395
+ : null;
1396
+ if (job !== undefined && argv !== null) {
1397
+ proposed.push({ check, argv, timeoutSeconds: LOCAL_COMMAND_TIMEOUT_SECONDS, source: job.source });
1398
+ }
1399
+ else {
1400
+ skipped.push(check);
1401
+ }
1402
+ }
1403
+ return { proposed, skipped };
1404
+ }