@bridge_gpt/mcp-server 0.2.72 → 0.2.73

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 (146) hide show
  1. package/README.md +5 -5
  2. package/build/agent-launchers/claude-executor-adapter.js +3 -0
  3. package/build/agents.generated.js +6 -6
  4. package/build/bridge-permissions.js +27 -0
  5. package/build/ci-system-detection.js +50 -0
  6. package/build/ci-trigger-adapters.js +631 -0
  7. package/build/ci-trigger-install.js +106 -0
  8. package/build/codex-launcher-pin.js +33 -2
  9. package/build/codex-skill-adapter.js +3 -1
  10. package/build/command-host.js +33 -0
  11. package/build/commands.generated.js +7 -6
  12. package/build/conductor/bridge-api-client.js +842 -35
  13. package/build/conductor/bring-up-facts.js +18 -5
  14. package/build/conductor/command-drift-production.js +42 -17
  15. package/build/conductor/command-drift.js +47 -0
  16. package/build/conductor/declare-blocked.js +554 -0
  17. package/build/conductor/doctor.js +106 -21
  18. package/build/conductor/driver-commands.js +141 -0
  19. package/build/conductor/driver-guard.js +277 -0
  20. package/build/conductor/driver-lapse.js +321 -0
  21. package/build/conductor/driver-register.js +176 -0
  22. package/build/conductor/driver-release.js +174 -0
  23. package/build/conductor/driver-session-state.js +315 -0
  24. package/build/conductor/epic-branch-ci-inspection.js +222 -29
  25. package/build/conductor/epic-implementer-cli.js +166 -15
  26. package/build/conductor/epic-server-database.js +162 -0
  27. package/build/conductor/epic-server-guidance.js +278 -0
  28. package/build/conductor/epic-server-journal.js +239 -0
  29. package/build/conductor/epic-server-processes.js +324 -0
  30. package/build/conductor/epic-server-recipe.js +397 -0
  31. package/build/conductor/epic-server-runtime.js +1207 -0
  32. package/build/conductor/epic-server-sfcc.js +107 -0
  33. package/build/conductor/epic-server-state.js +427 -0
  34. package/build/conductor/epic-server.js +594 -0
  35. package/build/conductor/gate-discovery.js +150 -5
  36. package/build/conductor/gate-settings-review.js +224 -4
  37. package/build/conductor/launch-need-probes.js +6 -53
  38. package/build/conductor/launch-plan.js +313 -156
  39. package/build/conductor/prerequisite-inputs.js +879 -45
  40. package/build/conductor/readiness-assessment.js +16 -1
  41. package/build/conductor/readiness-cli.js +20 -9
  42. package/build/conductor/readiness-prerequisites.js +28 -9
  43. package/build/conductor/readiness-sections.js +133 -126
  44. package/build/conductor/readiness.js +1 -1
  45. package/build/conductor/recovery-cli.js +13 -2
  46. package/build/conductor/tools.js +6 -5
  47. package/build/conductor/wait-run-attention.js +2 -2
  48. package/build/conductor/wait-run-cli.js +208 -22
  49. package/build/conductor/worker-database-doctor-inspector.js +33 -2
  50. package/build/conductor/worker-setup-tally.js +27 -0
  51. package/build/credential-store.js +71 -1
  52. package/build/dependency-install-declarations.js +55 -0
  53. package/build/docs.generated.js +2 -2
  54. package/build/doctor.js +54 -3
  55. package/build/drive-epic-help.js +21 -2
  56. package/build/drive-epic.js +1811 -351
  57. package/build/engine-paths.js +20 -0
  58. package/build/epic-branch-ci-summary.js +140 -0
  59. package/build/epic-capability-statement.js +107 -0
  60. package/build/epic-ci-workflow-drift.js +30 -15
  61. package/build/epic-ci-workflow-install.js +110 -42
  62. package/build/epic-ci-workflow-v2.js +274 -0
  63. package/build/epic-ci-workflow.js +38 -12
  64. package/build/epic-implementer-bin.js +2101 -44
  65. package/build/epic-launch-context.js +607 -0
  66. package/build/epic-server-record.js +273 -0
  67. package/build/epic-shared/bridge-client.js +11 -3
  68. package/build/escalation-reachability.js +43 -0
  69. package/build/executor/ci-fix-test-runs.js +334 -0
  70. package/build/executor/completion-contract.js +8 -1
  71. package/build/executor/env.js +59 -22
  72. package/build/executor/epic-server-context.js +141 -0
  73. package/build/executor/gate-qualification-job.js +127 -26
  74. package/build/executor/http-client.js +52 -5
  75. package/build/executor/job-runner.js +423 -69
  76. package/build/executor/observation.js +8 -0
  77. package/build/executor/pre-pr-checks.js +363 -0
  78. package/build/executor/prompt-spec.js +114 -2
  79. package/build/executor/reconcile-overlap-job.js +5 -3
  80. package/build/executor/required-work-diagnostics.js +4 -2
  81. package/build/executor/required-work-ledger.js +936 -38
  82. package/build/executor/results.js +58 -0
  83. package/build/executor/terminal-mutation.js +8 -0
  84. package/build/executor/worker-command.js +72 -3
  85. package/build/executor/worker-database-config.js +281 -0
  86. package/build/executor/worker-database-target.js +16 -12
  87. package/build/executor/worker-database.js +320 -101
  88. package/build/executor/worker-finalization.js +7 -1
  89. package/build/executor/worker-resources.js +25 -3
  90. package/build/executor/worker-verification-guidance.js +18 -3
  91. package/build/executor/worktree.js +20 -1
  92. package/build/executor-launch.js +54 -2
  93. package/build/generated-assets-catalog.js +2 -2
  94. package/build/index.js +664 -335
  95. package/build/init.js +26 -11
  96. package/build/install-bridge-conductor-answers.js +465 -0
  97. package/build/install-bridge-conductor-apply.js +259 -0
  98. package/build/install-bridge-conductor-assessment.js +895 -0
  99. package/build/install-bridge-conductor-compact.js +161 -0
  100. package/build/install-bridge-conductor-contracts.js +63 -0
  101. package/build/install-bridge-conductor-gate.js +1197 -196
  102. package/build/install-bridge-conductor-setup.js +535 -79
  103. package/build/install-bridge-conductor.js +2885 -560
  104. package/build/install-bridge.js +199 -21
  105. package/build/install-maintenance.js +26 -3
  106. package/build/launcher-config-inspection.js +162 -1
  107. package/build/mcp-grant-candidates.js +767 -0
  108. package/build/mcp-grant-doctor.js +63 -0
  109. package/build/mcp-grant.js +288 -0
  110. package/build/mcp-host-targets.js +8 -0
  111. package/build/mcp-identity.js +23 -0
  112. package/build/mcp-invoke.js +126 -13
  113. package/build/mcp-presets.js +107 -0
  114. package/build/mcp-provisioning.js +134 -2
  115. package/build/mcp-registration-doctor.js +31 -2
  116. package/build/mcp-server-invocation.js +6 -1
  117. package/build/merge-workflow-install.js +234 -109
  118. package/build/pipelines.generated.js +5 -5
  119. package/build/plan-discovery.js +97 -4
  120. package/build/plan-execution-ledger.js +100 -7
  121. package/build/plan-generation-io.js +262 -0
  122. package/build/plan-generation.js +424 -0
  123. package/build/plane/build-freshness.js +6 -1
  124. package/build/plane/cli.js +75 -9
  125. package/build/plane/plane-freshness.js +17 -0
  126. package/build/plane/preflight.js +50 -10
  127. package/build/plane/shutdown.js +19 -0
  128. package/build/plane/terminal-run-cleanup.js +18 -4
  129. package/build/printed-remedy-commands.js +206 -12
  130. package/build/readiness-check.js +77 -0
  131. package/build/readiness-prerequisites.generated.js +122 -14
  132. package/build/readme.generated.js +1 -1
  133. package/build/repository-state-paths.js +38 -0
  134. package/build/run-unit-tests-launcher.js +8 -1
  135. package/build/setup-epic.js +1190 -196
  136. package/build/setup-progress.js +44 -0
  137. package/build/setup-question.js +122 -4
  138. package/build/third-party-mcp-targets.js +30 -0
  139. package/build/ticket-delivery-enrollment.js +9 -0
  140. package/build/upgrade-cli.js +85 -2
  141. package/build/v2-readiness-requirements.js +28 -1
  142. package/build/version.generated.js +3 -3
  143. package/docs/EPIC-IMPLEMENTER-INSTALL.md +9 -1
  144. package/docs/epic-implementer/getting-started.md +3 -1
  145. package/docs/epic-implementer/self-hosting.md +14 -1
  146. package/package.json +2 -2
@@ -1,185 +1,342 @@
1
- import { sanitizeHarnessCoverage } from "../executor/harness-coverage.js";
2
1
  import { EPIC_KEY_PATTERN } from "../drive-epic-key.js";
3
- import { ESCALATION_REACHABILITY_CHECK_ID } from "../escalation-reachability.js";
4
- import { LAUNCH_NEEDS, LAUNCH_NEED_ROW_IDS, LAUNCH_NEED_REMEDIES } from "./readiness-sections.js";
5
- import { WORKER_DATABASE_DOCTOR_OWNER, WORKER_DATABASE_DOCTOR_RUNBOOK_SECTION, formatOwnerRoles, sanitizeOwnerRoles, sanitizeOwnerRoleTotal, } from "./doctor.js";
6
- import { normalizeRepoRelativePath } from "./file-scope-guard.js";
2
+ import { WORKER_DATABASE_DOCTOR_OWNER, WORKER_DATABASE_DOCTOR_RUNBOOK_SECTION } from "./doctor.js";
3
+ import { LAUNCH_NEED_REMEDIES, LAUNCH_NEED_ROW_IDS, LAUNCH_NEEDS } from "./readiness-sections.js";
4
+ export const NO_LAUNCH_PLAN_FACTS = Object.freeze({
5
+ prerequisites: { status: "not_collected" },
6
+ handoff: { explicit: null, projectDefault: "unknown" },
7
+ });
8
+ /** Fixed meaning of the per-run parallel limit, shared by every surface that names it. */
9
+ export const PARALLEL_LIMIT_MEANING_COPY = "Up to this many independent children may run concurrently, subject to executor or plane capacity.";
10
+ /** Fixed advisory for changing an approved parallel limit. */
11
+ export const PARALLEL_LIMIT_CHANGE_ADVISORY = "Raising the approved limit requires a run policy PATCH and re-approval, then a new executor or relaunched plane lanes; lowering via parallel_throttle takes effect at the next claim and needs nothing local.";
12
+ /** Approximate time to the first job. Fixed copy: it measures start-up, not the whole run. */
13
+ export const LAUNCH_PLAN_DURATION = {
14
+ local: "About 10 minutes to the first job.",
15
+ remote: "About 5 minutes to the first job.",
16
+ nothing: "Nothing is created.",
17
+ };
18
+ export const LAUNCH_PLAN_LOCAL_HANDOFF = "Because this is a local run, I'll give you one command to run in your own terminal.";
19
+ const WORKER_DATABASE_REMEDY = `${WORKER_DATABASE_DOCTOR_OWNER}: see ${WORKER_DATABASE_DOCTOR_RUNBOOK_SECTION}`;
20
+ /** The two server prerequisite rows that can be an authoritative refusal. */
21
+ export const SERVER_WEBHOOK_ROW_ID = "server.escalation-webhook-reachable";
22
+ export const SERVER_PROVIDER_BRANCH_ROW_ID = "server.provider-default-branch-readable";
7
23
  /**
8
- * Advisory decision table (source order within each group):
9
- * reachability unconfirmed | proceed | [] | fix the channel before retrying
10
- * already merged child (will skip) | skip | [] | keep: [--keep-child, KEY]
11
- * probably_done child | run it | [] | skip: [--skip-child, KEY]
12
- * applicable relevant role / other unhealthy DB | fix first | [] | launch anyway: []
13
- * asked/present runner | fix first | [] | launch anyway: []
14
- * asked/present power | proceed, keep the lid open | [] | fix first: []
15
- * asked/present usage | proceed | [] | fix first: []
16
- * Fix-first acceptance accepts unresolved risk; it never performs remediation.
17
- * verification_gap belongs exclusively to the command session, not this union.
24
+ * BAPI-1585 — the two pre-create refusal codes for those rows. Spelled once here;
25
+ * `drive-epic.ts` builds its refusal-id table from them, and
26
+ * `readiness-prerequisites-contract.test.ts` pins them against `pre_create_refusal.py`.
18
27
  */
19
- /** Bridge-specific hints are consulted only after positive repository applicability. */
20
- export const DB_OR_HARNESS_PATH_HINTS = ["db/alembic/", "tests/integration/conductor_harness/", "migrations/"];
21
- export function planDatabaseRelevance(nodes, applicable) {
22
- if (!applicable || !Array.isArray(nodes))
23
- return "unknown";
24
- let declared = false;
25
- let malformed = false;
26
- let relevant = false;
27
- for (const node of nodes) {
28
- if (!isObject(node)) {
29
- malformed = true;
30
- continue;
31
- }
32
- if (!("touched_files" in node))
33
- continue;
34
- if (!Array.isArray(node.touched_files)) {
35
- malformed = true;
36
- continue;
37
- }
38
- declared = true;
39
- for (const raw of node.touched_files) {
40
- const path = normalizeRepoRelativePath(raw);
41
- if (path === null) {
42
- malformed = true;
43
- continue;
44
- }
45
- if (DB_OR_HARNESS_PATH_HINTS.some((hint) => path.startsWith(hint)) || path.includes("/migrations/"))
46
- relevant = true;
47
- }
48
- }
49
- return relevant ? true : malformed || !declared ? "unknown" : false;
50
- }
51
- export const LAUNCH_PLAN_REACHABILITY_COPY = {
52
- summary: "Escalation reachability",
53
- reason: "Live delivery is not confirmed; escalations are also shown in the driving session.",
54
- remedy: "Declare notify.webhook_url in the run policy, or configure live email.",
55
- };
56
- export const LAUNCH_PLAN_MERGED_CHILD_COPY = {
57
- summary: "Already merged child",
58
- reason: "The child is already merged on the indexed commit and is scheduled to be skipped.",
59
- remedy: "Nothing to do, or keep the child in this plan to run it again.",
28
+ export const LAUNCH_PRE_CREATE_CODES = {
29
+ ESCALATION_WEBHOOK_UNREACHABLE: "escalation_webhook_unreachable",
30
+ PROVIDER_DEFAULT_BRANCH_UNREADABLE: "provider_default_branch_unreadable",
60
31
  };
61
- export const LAUNCH_PLAN_CHILD_COPY = {
62
- summary: "Probably done child",
63
- reason: "The child is on the indexed commit but has no confirmed delivery record; run it to be sure.",
64
- remedy: "Run the child, or explicitly skip it in this plan.",
65
- };
66
- export const LAUNCH_PLAN_NEED_COPY = {
67
- worker_db_role: { summary: "Worker DB role", reason: "This database-relevant plan needs a safe worker database role.", recommendation: "fix first" },
68
- self_hosted_runner: { summary: "Self-hosted runner", reason: "Child CI cannot become green while the required runner is offline.", recommendation: "fix first" },
69
- host_power: { summary: "Host sleep", reason: "The executor needs the host to remain awake during the run.", recommendation: "proceed, keep the lid open" },
70
- usage_reset_after_run: { summary: "Usage reset after the run", reason: "Capacity waits resume automatically when usage resets.", recommendation: "proceed" },
71
- };
72
- export const LAUNCH_PLAN_DB_COPY = {
73
- "worker-db-boundary": { summary: "Worker database boundary", reason: "The worker database boundary is not confirmed healthy." },
74
- "worker-db-alembic": { summary: "Worker database migrations", reason: "The operator migration state is not confirmed healthy." },
75
- "worker-db-leftovers": { summary: "Worker database leftovers", reason: "Worker database cleanup needs attention." },
76
- "worker-db-unverifiable": { summary: "Worker database inspection", reason: "Worker database safety could not be verified." },
77
- "worker-db-harness": { summary: "Worker database harness", reason: "Harness database cleanup is not confirmed healthy." },
32
+ export const LAUNCH_PLAN_COPY = {
33
+ blockedSummary: "Bridge found a proven problem that could cause harm if the run started.",
34
+ blockedWebhook: {
35
+ summary: "Your escalation webhook doesn't answer, and no account email can take over.",
36
+ reason: "An unattended run could ask for help and reach nobody.",
37
+ },
38
+ blockedProviderBranch: {
39
+ summary: "Bridge can't read your repository's default branch.",
40
+ reason: "Merge and delivery guards can't check the base branch without it.",
41
+ },
42
+ webhookFallback: {
43
+ summary: "Your webhook didn't answer. Escalations will go to your account email.",
44
+ reason: "The run keeps going; you will be reached by email instead.",
45
+ },
46
+ boundary: {
47
+ summary: "The worker database role can reach a protected database.",
48
+ reason: "A worker could write outside the run. This is proven, not a guess.",
49
+ },
50
+ workerRole: {
51
+ summary: "The worker database role isn't confirmed safe.",
52
+ reason: "Database-backed checks may be unable to run. The run keeps going and hands those off.",
53
+ },
54
+ selfHostedRunner: {
55
+ summary: "A self-hosted runner your required checks need is offline.",
56
+ reason: "Child checks cannot turn green while it is offline.",
57
+ },
58
+ hostPower: {
59
+ summary: "This computer may go to sleep during the run.",
60
+ reason: "The run needs the machine awake to keep working.",
61
+ },
78
62
  };
79
63
  function isObject(value) {
80
64
  return value !== null && typeof value === "object" && !Array.isArray(value);
81
65
  }
82
- /** Only complete, asked/present typed rows participate. Untrusted prose is never copied. */
83
- function eligibleNeed(row) {
84
- const meta = row.launch_need;
85
- if (!isObject(meta) || !LAUNCH_NEEDS.includes(meta.need))
86
- return undefined;
87
- const need = meta.need;
88
- if (need === "epic_branch_behind_base" || row.id !== LAUNCH_NEED_ROW_IDS[need] ||
89
- meta.asked !== true || meta.state !== "present" || row.status !== "warn" ||
90
- row.blocking !== false || typeof row.remedy !== "string" || row.remedy.trim() === "")
91
- return undefined;
92
- return { need, state: "present", asked: true };
66
+ /** An eligible, present launch-need row: the typed metadata is complete and the row warns. */
67
+ function presentNeed(rows, need) {
68
+ return rows.find((row) => {
69
+ const meta = row.launch_need;
70
+ return isObject(meta) && LAUNCH_NEEDS.includes(meta.need) && meta.need === need &&
71
+ meta.state === "present" && row.id === LAUNCH_NEED_ROW_IDS[need] && row.status === "warn" &&
72
+ row.blocking === false && typeof row.remedy === "string" && row.remedy.trim() !== "";
73
+ });
74
+ }
75
+ /** The effective policy this launch will stamp: the explicit choice, else the project default. */
76
+ function effectiveHandoff(facts) {
77
+ return facts.explicit ?? facts.projectDefault;
78
+ }
79
+ function handoffLine(facts, children) {
80
+ const effective = effectiveHandoff(facts);
81
+ const who = children === 1 ? "One child has" : `${children} children have`;
82
+ const base = `${who} a check that neither the workers nor CI may be able to run.`;
83
+ let reason;
84
+ if (effective === "block") {
85
+ reason = "This launch is set to stop such a child instead of handing the check off. You decide what to do with it.";
86
+ }
87
+ else if (effective === "proceed") {
88
+ reason = "If so, the worker records a hand-off and the run keeps going. You review it at the end.";
89
+ }
90
+ else {
91
+ reason =
92
+ "Your Bridge server didn't report the project default: unknown (today's default: proceed). " +
93
+ "If a check can't run, the worker records a hand-off and the run keeps going.";
94
+ }
95
+ if (facts.explicit !== null && facts.projectDefault !== "unknown" && facts.explicit !== facts.projectDefault) {
96
+ reason += ` The project default is ${facts.projectDefault}; this launch uses ${facts.explicit}.`;
97
+ }
98
+ return { summary: base, reason };
99
+ }
100
+ function summaryLine(preview, blocked) {
101
+ const local = preview.target.kind === "local_plane";
102
+ const where = local ? "on your local plane" : "on the server";
103
+ if (blocked)
104
+ return `${preview.epic_key} can't launch yet.`;
105
+ const count = preview.summary.children_to_run ?? preview.summary.child_count;
106
+ const parts = [];
107
+ if (typeof count === "number")
108
+ parts.push(`${count} ${count === 1 ? "child" : "children"}`);
109
+ const limit = preview.summary.parallel_limit;
110
+ if (typeof limit === "number" && limit > 0)
111
+ parts.push(`up to ${limit} at a time`);
112
+ parts.push(where);
113
+ return `${preview.epic_key} is ready to launch: ${parts.join(", ")}.`;
114
+ }
115
+ function launchOption(number, label, local, recommended) {
116
+ return {
117
+ number,
118
+ label,
119
+ description: local
120
+ ? "Creates the run and starts it on your local plane."
121
+ : "Creates the run and starts it on the server.",
122
+ duration: local ? LAUNCH_PLAN_DURATION.local : LAUNCH_PLAN_DURATION.remote,
123
+ recommended,
124
+ };
125
+ }
126
+ function nothingOption(number, label, recommended) {
127
+ return { number, label, description: "Creates nothing and changes nothing.", duration: LAUNCH_PLAN_DURATION.nothing, recommended };
93
128
  }
94
- function item(id, kind, copy, recommendation, alternatives) {
95
- return { id, kind, ...copy, recommendation, alternatives, flags_if_accepted: [] };
129
+ /** The health-check template: always built, inserted by the session only after a non-zero exit. */
130
+ function healthCheckFailedFragment(local) {
131
+ return {
132
+ item: {
133
+ summary: "The Epic Implementer health check did not pass.",
134
+ reason: "The engine code on this checkout may not work. Launching on it could waste the run.",
135
+ },
136
+ recommendation: "stop and fix first.",
137
+ reason: "The health check is how Bridge confirms this checkout's engine works before it spends a run.",
138
+ options: [
139
+ launchOption(1, "Launch anyway", local, false),
140
+ nothingOption(2, "Stop and fix", true),
141
+ ],
142
+ option_actions: [
143
+ { number: 1, action: "launch", argv: [] },
144
+ { number: 2, action: "create_nothing", argv: [] },
145
+ ],
146
+ };
96
147
  }
97
148
  /**
98
- * The worker-database remedy. Fixed wording; the only variable part is the list of
99
- * owner roles, revalidated here from the typed `owner_roles` field. Free prose in
100
- * `detail` is never read. No usable owner keeps the historical remedy byte for byte.
149
+ * Pure projection of a finished preview and normalized facts. Returns `undefined`
150
+ * for a path with no launch decision to present: an attach/reuse, a stop, or a
151
+ * refusal the preview did not select as authoritative.
101
152
  */
102
- function workerDatabaseRemedy(id, row) {
103
- const historical = `${WORKER_DATABASE_DOCTOR_OWNER}: see ${WORKER_DATABASE_DOCTOR_RUNBOOK_SECTION}`;
104
- if (id !== "worker-db-harness")
105
- return historical;
106
- const roles = sanitizeOwnerRoles(row.owner_roles);
107
- if (roles.length === 0)
108
- return historical;
109
- const shown = formatOwnerRoles(roles, sanitizeOwnerRoleTotal(row.owner_role_total, roles.length));
110
- return (`${WORKER_DATABASE_DOCTOR_OWNER}: terminate sessions and drop the leftover harness databases as their owner ` +
111
- `role (${shown}), not the CI role; see ${WORKER_DATABASE_DOCTOR_RUNBOOK_SECTION}`);
153
+ /** BAPI-1621 — the details id of the generated-plan item. */
154
+ export const GENERATED_PLAN_ITEM_ID = "plan-generated";
155
+ const SERIALIZED_REASON_COPY = {
156
+ unknown_touched_files: (unknown) => `It runs one child at a time, because the files ${unknown.length === 1 ? "this child touches" : "these children touch"} ` +
157
+ `could not be determined from their tickets: ${unknown.join(", ")}.`,
158
+ key_order_fallback: () => "It runs one child at a time, in ticket-key order, because the epic has no usable ordered child manifest.",
159
+ };
160
+ /**
161
+ * BAPI-1621 — the generated-plan explanation: why it is serial (first), where
162
+ * the order came from, which spikes were left out, then every child in order
163
+ * with its dependency. Built from the preview's own plan fields only.
164
+ */
165
+ function generatedPlanItem(preview) {
166
+ const plan = preview.plan;
167
+ if (plan.generated !== true)
168
+ return null;
169
+ const source = plan.inline ?? plan.generation;
170
+ if (source === undefined)
171
+ return null;
172
+ const summary = source.summary;
173
+ const parts = [];
174
+ if (summary?.serialized_reason) {
175
+ parts.push(SERIALIZED_REASON_COPY[summary.serialized_reason]?.(summary.unknown_files) ?? "It runs one child at a time.");
176
+ }
177
+ if (summary !== null) {
178
+ parts.push(summary.order_source === "epic_manifest"
179
+ ? "Order and dependencies come from the epic's ordered child manifest."
180
+ : "Order is ascending ticket key.");
181
+ for (const entry of summary.excluded) {
182
+ parts.push(`Left out: ${entry.ticket_key} (${entry.reason}; the Epic Implementer cannot run an unfinished spike).`);
183
+ }
184
+ }
185
+ const order = source.nodes.map((node, index) => {
186
+ const after = node.depends_on.length > 0 ? `runs after ${node.depends_on.join(", ")}` : "no dependency";
187
+ return `${index + 1}. ${node.ticket_key} (${node.status}) — ${after}`;
188
+ });
189
+ parts.push(`Order: ${order.join("; ")}.`);
190
+ parts.push("It was built from stored ticket data by fixed rules, with no model.");
191
+ if (plan.status === "generated" && plan.would_write) {
192
+ parts.push(`This preview wrote nothing; launching rebuilds it from current data and saves it to ${plan.would_write}.`);
193
+ }
194
+ return { summary: "GENERATED — Generated from current epic children.", reason: parts.join(" ") };
112
195
  }
113
- /** Pure projection of a finished preview; no admission decision or input is changed. */
114
- export function buildLaunchPlan(preview) {
115
- if (preview.decision !== "create")
196
+ export function buildLaunchPlan(preview, facts = NO_LAUNCH_PLAN_FACTS) {
197
+ const refusal = preview.decision === "refuse" && preview.refusal?.authoritative === true ? preview.refusal : null;
198
+ if (preview.decision !== "create" && refusal === null)
116
199
  return undefined;
200
+ const local = preview.target.kind === "local_plane";
117
201
  const rows = Array.isArray(preview.launch_checks) ? preview.launch_checks.filter(isObject) : [];
118
- const items = [];
119
- const seen = new Set();
120
- const add = (entry) => {
121
- if (!seen.has(entry.id)) {
122
- seen.add(entry.id);
123
- items.push(entry);
124
- }
202
+ const items = { blocks_launch: [], needs_choice: [], worth_knowing: [] };
203
+ const detailItems = [];
204
+ const add = (group, entry, id, flags) => {
205
+ detailItems.push({ group, index: items[group].length, id, ...(flags !== undefined ? { flags } : {}) });
206
+ items[group].push(entry);
125
207
  };
126
- if (preview.summary?.escalation_channel !== "webhook" &&
127
- !rows.some((row) => row.id === ESCALATION_REACHABILITY_CHECK_ID && row.status === "pass")) {
128
- add(item(ESCALATION_REACHABILITY_CHECK_ID, "reachability", LAUNCH_PLAN_REACHABILITY_COPY, "proceed", [{ choice: "fix first", flags: [] }]));
208
+ const prerequisites = Array.isArray(preview.prerequisites) ? preview.prerequisites : [];
209
+ const prerequisiteRow = (id) => prerequisites.find((row) => row.id === id);
210
+ // --- blocks the launch: the one refusal the preview already selected -------------
211
+ if (refusal !== null) {
212
+ const webhook = prerequisiteRow(SERVER_WEBHOOK_ROW_ID);
213
+ const branch = prerequisiteRow(SERVER_PROVIDER_BRANCH_ROW_ID);
214
+ const copy = webhook?.check_status === "not_met" && webhook.impact === "blocking" && refusal.reason_id === LAUNCH_PRE_CREATE_CODES.ESCALATION_WEBHOOK_UNREACHABLE
215
+ ? LAUNCH_PLAN_COPY.blockedWebhook
216
+ : branch?.check_status === "not_met" && branch.impact === "blocking" && refusal.reason_id === LAUNCH_PRE_CREATE_CODES.PROVIDER_DEFAULT_BRANCH_UNREADABLE
217
+ ? LAUNCH_PLAN_COPY.blockedProviderBranch
218
+ : { summary: LAUNCH_PLAN_COPY.blockedSummary, reason: "Bridge stops here so nothing is started that could cause harm." };
219
+ add("blocks_launch", { summary: copy.summary, reason: copy.reason, remedy: refusal.remedy }, refusal.reason_id);
220
+ }
221
+ // --- worth knowing: webhook fell back to email (never a refusal) ------------------
222
+ const webhookRow = prerequisiteRow(SERVER_WEBHOOK_ROW_ID);
223
+ if (refusal === null && webhookRow !== undefined && webhookRow.impact === "warning" &&
224
+ (webhookRow.check_status === "met" || webhookRow.check_status === "not_met")) {
225
+ add("worth_knowing", { ...LAUNCH_PLAN_COPY.webhookFallback }, SERVER_WEBHOOK_ROW_ID);
226
+ }
227
+ // --- needs your choice -----------------------------------------------------------
228
+ let boundaryProven = false;
229
+ if (refusal === null) {
230
+ for (const row of rows) {
231
+ // A boundary row is proven harm only when the producer stamped its typed state `unsafe`.
232
+ if (row.id === "worker-db-boundary" && row.worker_state === "unsafe" && row.blocking === false) {
233
+ boundaryProven = true;
234
+ add("needs_choice", { ...LAUNCH_PLAN_COPY.boundary, remedy: WORKER_DATABASE_REMEDY }, "worker-db-boundary");
235
+ break;
236
+ }
237
+ }
129
238
  }
239
+ const prerequisiteFacts = facts.prerequisites;
240
+ const missingCore = refusal === null && prerequisiteFacts.status === "collected" ? prerequisiteFacts.missingCore : [];
241
+ for (const entry of missingCore) {
242
+ const sure = entry.uncertain ? " We are not sure this one is essential." : "";
243
+ add("needs_choice", {
244
+ summary: `${entry.requirement} isn't set in this terminal.`,
245
+ reason: `${entry.dependents} will stop without it. Tickets that don't depend on it keep going.${sure}`,
246
+ }, entry.name);
247
+ }
248
+ // --- worth knowing: surviving non-blocking evidence ------------------------------
249
+ if (refusal === null) {
250
+ const roleRow = presentNeed(rows, "worker_db_role");
251
+ if (roleRow)
252
+ add("worth_knowing", { ...LAUNCH_PLAN_COPY.workerRole, remedy: LAUNCH_NEED_REMEDIES.worker_db_role }, roleRow.id);
253
+ const runnerRow = presentNeed(rows, "self_hosted_runner");
254
+ if (runnerRow)
255
+ add("worth_knowing", { ...LAUNCH_PLAN_COPY.selfHostedRunner, remedy: LAUNCH_NEED_REMEDIES.self_hosted_runner }, runnerRow.id);
256
+ const powerRow = presentNeed(rows, "host_power");
257
+ if (powerRow)
258
+ add("worth_knowing", { ...LAUNCH_PLAN_COPY.hostPower, remedy: LAUNCH_NEED_REMEDIES.host_power }, powerRow.id);
259
+ if (prerequisiteFacts.status === "collected" && prerequisiteFacts.workerRuntimeChildren.length > 0) {
260
+ add("worth_knowing", handoffLine(facts.handoff, prerequisiteFacts.workerRuntimeChildren.length), "verification-handoff");
261
+ }
262
+ // BAPI-1621 — one item for a generated plan, built only from the DAG and the
263
+ // stored summary the preview already carries. No new question.
264
+ const generated = generatedPlanItem(preview);
265
+ if (generated !== null)
266
+ add("worth_knowing", generated, GENERATED_PLAN_ITEM_ID);
267
+ }
268
+ // --- already merged children: summary and details only ---------------------------
269
+ const alreadyMerged = [];
130
270
  const children = preview.already_done_children;
131
271
  if (isObject(children) && children.status === "checked" && Array.isArray(children.children)) {
132
272
  for (const child of children.children) {
133
273
  if (!isObject(child) || child.classification !== "done" || child.will_skip !== true ||
134
274
  typeof child.ticket_key !== "string" || !EPIC_KEY_PATTERN.test(child.ticket_key))
135
275
  continue;
136
- add(item(`already-merged-${child.ticket_key}`, "already_merged", {
137
- ...LAUNCH_PLAN_MERGED_CHILD_COPY, summary: `${LAUNCH_PLAN_MERGED_CHILD_COPY.summary} ${child.ticket_key}`,
138
- }, "skip", [{ choice: "keep", flags: ["--keep-child", child.ticket_key] }]));
139
- }
140
- for (const child of children.children) {
141
- if (!isObject(child) || child.classification !== "probably_done" || child.will_skip !== false ||
142
- typeof child.ticket_key !== "string" || !EPIC_KEY_PATTERN.test(child.ticket_key))
143
- continue;
144
- add(item(`probably-done-${child.ticket_key}`, "probably_done", {
145
- ...LAUNCH_PLAN_CHILD_COPY, summary: `${LAUNCH_PLAN_CHILD_COPY.summary} ${child.ticket_key}`,
146
- }, "run it", [{ choice: "skip", flags: ["--skip-child", child.ticket_key] }]));
276
+ alreadyMerged.push({ ticket_key: child.ticket_key, keep_flags: ["--keep-child", child.ticket_key] });
147
277
  }
148
278
  }
149
- const addNeed = (row, need) => {
150
- const copy = LAUNCH_PLAN_NEED_COPY[need];
151
- add(item(LAUNCH_NEED_ROW_IDS[need], need === "worker_db_role" ? "worker_db" : "launch_need", {
152
- summary: copy.summary, reason: copy.reason, remedy: LAUNCH_NEED_REMEDIES[need],
153
- }, copy.recommendation, [{ choice: copy.recommendation === "fix first" ? "launch anyway" : "fix first", flags: [] }]));
279
+ // --- verdict ---------------------------------------------------------------------
280
+ const blocked = refusal !== null;
281
+ const verdict = blocked
282
+ ? "blocked"
283
+ : items.needs_choice.length > 0 || items.worth_knowing.length > 0 ? "ready_with_notes" : "ready";
284
+ // --- numbered options ------------------------------------------------------------
285
+ const options = [];
286
+ const actions = [];
287
+ let recommendation;
288
+ let reason;
289
+ const push = (option, action) => {
290
+ const number = options.length + 1;
291
+ options.push({ number, ...option });
292
+ actions.push({ number, action, argv: [] });
154
293
  };
155
- for (const row of rows) {
156
- // The role never falls through into generic database handling, even if malformed.
157
- if (row.id === LAUNCH_NEED_ROW_IDS.worker_db_role) {
158
- if (eligibleNeed(row)?.need === "worker_db_role" && preview.plan_database_relevance === true)
159
- addNeed(row, "worker_db_role");
160
- continue;
161
- }
162
- if (typeof row.id !== "string" || !Object.hasOwn(LAUNCH_PLAN_DB_COPY, row.id) ||
163
- !["warn", "fail", "unverified"].includes(row.status) || row.blocking !== false)
164
- continue;
165
- const id = row.id;
166
- const coverage = id === "worker-db-harness" ? sanitizeHarnessCoverage(row.harness_coverage) : undefined;
167
- if (coverage && coverage.total > 0 && coverage.activity === "known" && coverage.uncovered === 0) {
168
- add(item(id, "worker_db", {
169
- summary: "Harness databases in use or CI-owned",
170
- remedy: "Proceed; no harness cleanup is requested by launch.",
171
- reason: "Every harness database is in use or CI-owned; no harness cleanup is requested by launch.",
172
- }, "proceed", []));
173
- continue;
174
- }
175
- add(item(id, "worker_db", { ...LAUNCH_PLAN_DB_COPY[id], remedy: workerDatabaseRemedy(id, row) }, "fix first", [{ choice: "launch anyway", flags: [] }]));
294
+ if (blocked) {
295
+ push(nothingOption(1, "Don't launch yet", true), "create_nothing");
296
+ recommendation = "don't launch yet, and fix the problem below.";
297
+ reason = "Bridge found a proven problem. Nothing has been created.";
176
298
  }
177
- for (const need of LAUNCH_NEEDS) {
178
- if (need === "worker_db_role" || need === "epic_branch_behind_base")
179
- continue;
180
- const row = rows.find((row) => eligibleNeed(row)?.need === need);
181
- if (row)
182
- addNeed(row, need);
299
+ else if (boundaryProven) {
300
+ push(launchOption(1, "Launch anyway", local, false), "launch");
301
+ if (missingCore.length > 0)
302
+ push(nothingOption(options.length + 1, "Stop so you can supply what's missing", false), "create_nothing");
303
+ push(nothingOption(options.length + 1, "Don't launch yet", true), "create_nothing");
304
+ recommendation = "don't launch yet, and fix the worker database role.";
305
+ reason = "The worker role can reach a protected database. That is proven, and fixing it is quick.";
183
306
  }
184
- return { launch_plan: { version: 1, items, fix_first: items.some((entry) => entry.recommendation === "fix first") } };
307
+ else {
308
+ const missing = missingCore.length > 0;
309
+ push(launchOption(1, missing ? "Launch now and supply it later" : "Launch now", local, true), "launch");
310
+ if (missing)
311
+ push(nothingOption(options.length + 1, "Stop so you can supply it", false), "create_nothing");
312
+ push(nothingOption(options.length + 1, "Don't launch yet", false), "create_nothing");
313
+ recommendation = "launch now.";
314
+ reason = verdict === "ready"
315
+ ? "Every check that protects your code passed."
316
+ : missing
317
+ ? "Every check that protects your code passed. Some children need something you can supply later."
318
+ : "Every check that protects your code passed. A few things are worth knowing.";
319
+ }
320
+ const plan = {
321
+ version: 2,
322
+ verdict,
323
+ summary: summaryLine(preview, blocked),
324
+ recommendation,
325
+ reason,
326
+ items,
327
+ options,
328
+ details: {
329
+ items: detailItems,
330
+ options: actions,
331
+ handoff: {
332
+ effective: effectiveHandoff(facts.handoff),
333
+ project_default: facts.handoff.projectDefault,
334
+ explicit: facts.handoff.explicit !== null,
335
+ },
336
+ already_merged: alreadyMerged,
337
+ },
338
+ ...(local && !blocked ? { local_handoff: LAUNCH_PLAN_LOCAL_HANDOFF } : {}),
339
+ ...(!blocked ? { health_check_failed: healthCheckFailedFragment(local) } : {}),
340
+ };
341
+ return { launch_plan: plan };
185
342
  }