@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
@@ -16,12 +16,22 @@
16
16
  * provisioned as its own shim entry. Unsupported or incomplete non-`bapi`
17
17
  * targets produce a non-fatal, secret-free warning.
18
18
  *
19
+ * BAPI-1545 adds the GRANT PATH. When the caller supplies a trusted repository
20
+ * name (the executor passes the claimed job's `repo_name`) and that repository
21
+ * has an MCP grant file outside the worktree, the worktree's `.bridge/config` is
22
+ * never read. The registration is rebuilt from the grant on every spawn — Bridge
23
+ * plus one shim per granted server, nothing else — replacing the whole server
24
+ * map so a worker's edits cannot survive. An unusable grant yields Bridge only.
25
+ * With no trusted name, or no grant file, the legacy path below is unchanged.
26
+ *
19
27
  * All filesystem access is dependency-injected so this is unit-testable, and the
20
28
  * platform path API is resolved locally (never imported from `start-tickets.ts`)
21
29
  * to avoid a runtime import cycle.
22
30
  */
31
+ import os from "os";
23
32
  import path from "path";
24
33
  import { readBridgeConfig } from "./bridge-config.js";
34
+ import { readMcpGrant } from "./mcp-grant.js";
25
35
  import { getThirdPartyTargetDefinition, validateThirdPartyTargetManifestEntry, } from "./third-party-mcp-targets.js";
26
36
  import { buildMcpShimCommand, } from "./mcp-server-invocation.js";
27
37
  import { getWorktreeHostTargets, hostAdapterForTarget } from "./mcp-host-targets.js";
@@ -90,8 +100,8 @@ export function serverNameForMcpTarget(target) {
90
100
  * `command` and `args`; credentials continue to be resolved by the `mcp-invoke`
91
101
  * shim at launch and are never written into the worktree.
92
102
  */
93
- export function buildShimMcpServerEntry(target, absoluteWorktreePath, invocation, bridgeApiBaseUrl) {
94
- const entry = buildMcpShimCommand(invocation, target, absoluteWorktreePath);
103
+ export function buildShimMcpServerEntry(target, absoluteWorktreePath, invocation, bridgeApiBaseUrl, grantRepo) {
104
+ const entry = buildMcpShimCommand(invocation, target, absoluteWorktreePath, grantRepo);
95
105
  // Tier-2 targets are structurally unchanged: `--base-url` is a `bapi`-only flag
96
106
  // and `mcp-invoke` rejects it for any other target, so appending it elsewhere
97
107
  // would generate a registration that refuses to launch.
@@ -111,14 +121,14 @@ export function buildBridgeApiShimMcpServerEntry(absoluteWorktreePath, invocatio
111
121
  * or incomplete non-`bapi` targets are skipped with a secret-free warning (they
112
122
  * never appear in the returned entries).
113
123
  */
114
- export function buildMcpServerEntriesForManifest(manifest, absoluteWorktreePath, invocation, bridgeApiBaseUrl) {
124
+ export function buildMcpServerEntriesForManifest(manifest, absoluteWorktreePath, invocation, bridgeApiBaseUrl, reference = {}) {
115
125
  const entries = {};
116
126
  const warnings = [];
117
127
  for (const mcp of manifest.mcp) {
118
128
  if (mcp.target === "bapi") {
119
129
  // The optional base URL reaches ONLY this branch — Tier-2 entries below are
120
130
  // built without it and stay byte-identical to their pre-BAPI-727 shape.
121
- entries[serverNameForMcpTarget("bapi")] = buildShimMcpServerEntry("bapi", absoluteWorktreePath, invocation, bridgeApiBaseUrl);
131
+ entries[serverNameForMcpTarget("bapi")] = buildShimMcpServerEntry("bapi", absoluteWorktreePath, invocation, bridgeApiBaseUrl, reference.bapiReferenceRepo);
122
132
  continue;
123
133
  }
124
134
  const definition = getThirdPartyTargetDefinition(mcp.target);
@@ -126,7 +136,12 @@ export function buildMcpServerEntriesForManifest(manifest, absoluteWorktreePath,
126
136
  warnings.push(`MCP target '${mcp.target}' is not a supported third-party target; skipping its registration.`);
127
137
  continue;
128
138
  }
129
- const validation = validateThirdPartyTargetManifestEntry(mcp);
139
+ // BAPI-1647: a bundle-less registry entry registers only when a
140
+ // fingerprint-matched approved reference names it; otherwise the
141
+ // skip-with-warning below is unchanged.
142
+ const validation = validateThirdPartyTargetManifestEntry(mcp, {
143
+ referenceBacked: reference.referenceBackedTargets?.has(mcp.target) === true,
144
+ });
130
145
  if (!validation.ok) {
131
146
  warnings.push(`MCP target '${mcp.target}' registration skipped: ${validation.error}.`);
132
147
  continue;
@@ -240,6 +255,25 @@ export function mergeBridgeApiMcpRegistration(existing, topLevelKey, entry) {
240
255
  // canonical name is sufficient — and keeps ONE place that decides the key.
241
256
  return mergeMcpRegistrations(existing, topLevelKey, { [MCP_SERVER_NAME]: entry });
242
257
  }
258
+ /**
259
+ * BAPI-1545: the authoritative server map for the grant path. Bridge is always
260
+ * present (with `--grant-repo`, so the shim takes its repository identity from
261
+ * the trusted name). A usable grant adds exactly one shim per granted server; an
262
+ * unusable grant adds none.
263
+ */
264
+ export function buildGrantMcpServerEntries(grant, trustedRepoName, absoluteWorktreePath, invocation, bridgeApiBaseUrl) {
265
+ const entries = {
266
+ [serverNameForMcpTarget("bapi")]: buildShimMcpServerEntry("bapi", absoluteWorktreePath, invocation, bridgeApiBaseUrl, trustedRepoName),
267
+ };
268
+ if (grant.state === "ok") {
269
+ for (const server of grant.grant.servers) {
270
+ entries[serverNameForMcpTarget(server.name)] = buildShimMcpServerEntry(server.name, absoluteWorktreePath, invocation, undefined, trustedRepoName);
271
+ }
272
+ }
273
+ return entries;
274
+ }
275
+ /** Fixed, secret-free warning for an unusable grant. */
276
+ export const UNUSABLE_GRANT_WARNING = "the MCP grant for this repository is unusable; this worker gets Bridge only. Re-run `install epic-implementer` to repair it.";
243
277
  // ---------------------------------------------------------------------------
244
278
  // Filesystem writes
245
279
  // ---------------------------------------------------------------------------
@@ -281,6 +315,55 @@ export async function writeMcpRegistrationFile(target, entries, deps) {
281
315
  }
282
316
  return { ok: true };
283
317
  }
318
+ /**
319
+ * BAPI-1545: write a registration file AUTHORITATIVELY — the whole server map
320
+ * under `topLevelKey` is replaced by `entries`, removing stale extras and any
321
+ * Bridge alias. Unrelated top-level fields of a valid existing document are
322
+ * kept. A malformed existing document is refused, never overwritten blindly.
323
+ * The file is then read back and its server map compared with `entries`.
324
+ */
325
+ export async function writeAuthoritativeMcpRegistrationFile(target, entries, deps) {
326
+ const api = pathApiForProvisioningPlatform(deps.platform);
327
+ let existing = {};
328
+ try {
329
+ const raw = await deps.readFile(target.filePath);
330
+ let parsed;
331
+ try {
332
+ parsed = JSON.parse(raw);
333
+ }
334
+ catch {
335
+ return { ok: false, error: `existing ${target.filePath} contains malformed JSON; not overwriting` };
336
+ }
337
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
338
+ return { ok: false, error: `existing ${target.filePath} is not a JSON object; not overwriting` };
339
+ }
340
+ existing = { ...parsed };
341
+ }
342
+ catch (err) {
343
+ const code = err && typeof err === "object" ? err.code : undefined;
344
+ if (code !== "ENOENT")
345
+ return { ok: false, error: `unable to read ${target.filePath}` };
346
+ }
347
+ const document = { ...existing, [target.topLevelKey]: { ...entries } };
348
+ try {
349
+ await deps.mkdir(api.dirname(target.filePath), { recursive: true });
350
+ await deps.writeFile(target.filePath, `${JSON.stringify(document, null, 2)}\n`);
351
+ }
352
+ catch {
353
+ return { ok: false, error: `failed to write ${target.filePath}` };
354
+ }
355
+ try {
356
+ const readBack = JSON.parse(await deps.readFile(target.filePath));
357
+ const servers = readBack?.[target.topLevelKey];
358
+ if (JSON.stringify(servers) !== JSON.stringify(entries)) {
359
+ return { ok: false, error: `${target.filePath} did not read back as written` };
360
+ }
361
+ }
362
+ catch {
363
+ return { ok: false, error: `${target.filePath} could not be read back` };
364
+ }
365
+ return { ok: true };
366
+ }
284
367
  /**
285
368
  * Write (or merge) the worktree's `.claude/settings.local.json` so the given
286
369
  * `.mcp.json` server names are pre-approved via `enabledMcpjsonServers`. This
@@ -381,6 +464,25 @@ export async function provisionMcpRegistrationForWorktree(row, deps) {
381
464
  if (row.status !== "created" || !row.path) {
382
465
  return row;
383
466
  }
467
+ // BAPI-1545: choose the authority BEFORE reading anything from the worktree.
468
+ if (deps.trustedRepoName !== undefined) {
469
+ const readGrant = deps.readMcpGrant ??
470
+ ((repo) => readMcpGrant(repo, {
471
+ env: deps.env ?? process.env,
472
+ homedir: deps.homedir ?? os.homedir,
473
+ readFile: deps.readFile,
474
+ }));
475
+ let grant;
476
+ try {
477
+ grant = await readGrant(deps.trustedRepoName);
478
+ }
479
+ catch {
480
+ grant = { state: "unusable", reason: "the grant file could not be read" };
481
+ }
482
+ if (grant.state !== "absent") {
483
+ return provisionGrantMcpRegistration(row, row.path, deps.trustedRepoName, grant, deps);
484
+ }
485
+ }
384
486
  const read = await readBridgeConfig(row.path, { readFile: deps.readFile });
385
487
  if (!read.ok) {
386
488
  if (read.kind === "missing") {
@@ -392,7 +494,19 @@ export async function provisionMcpRegistrationForWorktree(row, deps) {
392
494
  if (!normalized.ok) {
393
495
  return { ...row, status: "spawn-failed", error: `MCP provisioning failed: ${normalized.error}` };
394
496
  }
395
- const built = buildMcpServerEntriesForManifest(read.manifest, normalized.path, deps.mcpServerInvocation, deps.bridgeApiBaseUrl);
497
+ // BAPI-1647: the caller's names-only eligibility, computed from its one
498
+ // explicit record read. A throwing callback means "no reference".
499
+ let referenceBackedTargets;
500
+ try {
501
+ referenceBackedTargets = deps.credentialReferenceTargets?.(read.manifest, normalized.path);
502
+ }
503
+ catch {
504
+ referenceBackedTargets = undefined;
505
+ }
506
+ const built = buildMcpServerEntriesForManifest(read.manifest, normalized.path, deps.mcpServerInvocation, deps.bridgeApiBaseUrl, {
507
+ ...(referenceBackedTargets !== undefined ? { referenceBackedTargets } : {}),
508
+ ...(deps.bapiReferenceRepo !== undefined ? { bapiReferenceRepo: deps.bapiReferenceRepo } : {}),
509
+ });
396
510
  if (Object.keys(built.entries).length === 0) {
397
511
  // Nothing supported to write (e.g. a manifest with no bapi target and no
398
512
  // supported Tier-2 targets). Surface any warnings but leave status unchanged.
@@ -422,6 +536,41 @@ export async function provisionMcpRegistrationForWorktree(row, deps) {
422
536
  }
423
537
  return result;
424
538
  }
539
+ /**
540
+ * BAPI-1545: the grant path. Never reads `.bridge/config`. Writes BOTH
541
+ * registration files authoritatively from the grant, reads them back, and
542
+ * records the outcome on `row.mcpGrantRegistration` so the executor can refuse
543
+ * a spawn whose registration is not authoritative. Trust pre-approval stays
544
+ * best-effort.
545
+ */
546
+ async function provisionGrantMcpRegistration(row, worktreePath, trustedRepoName, grant, deps) {
547
+ const normalized = normalizeWorktreePathForRegistration(worktreePath, deps);
548
+ if (!normalized.ok) {
549
+ const error = `MCP provisioning failed: ${normalized.error}`;
550
+ return { ...row, status: "spawn-failed", error, mcpGrantRegistration: { ok: false, error } };
551
+ }
552
+ const entries = buildGrantMcpServerEntries(grant, trustedRepoName, normalized.path, deps.mcpServerInvocation, deps.bridgeApiBaseUrl);
553
+ let result = { ...row, mcpRegistrationForm: deps.mcpServerInvocation.form };
554
+ if (grant.state === "unusable")
555
+ result = withWarning(result, UNUSABLE_GRANT_WARNING);
556
+ for (const target of getWorktreeMcpRegistrationTargets(normalized.path, deps.platform)) {
557
+ const written = await writeAuthoritativeMcpRegistrationFile(target, entries, deps);
558
+ if (!written.ok) {
559
+ const error = `MCP provisioning failed: ${written.error}`;
560
+ return { ...result, status: "spawn-failed", error, mcpGrantRegistration: { ok: false, error } };
561
+ }
562
+ }
563
+ const serverNames = Object.keys(entries);
564
+ result = { ...result, mcpGrantRegistration: { ok: true, expectedServerNames: [...serverNames].sort() } };
565
+ if (deps.mcpServerInvocation.form !== "absolute-build-path") {
566
+ result = withWarning(result, "MCP registration used npm-channel fallback because an on-disk build entry was not resolvable.");
567
+ }
568
+ const trust = await writeClaudeServerTrustSettings(normalized.path, serverNames, deps);
569
+ if (!trust.ok) {
570
+ result = withWarning(result, `Claude MCP trust pre-approval skipped: ${trust.error}`);
571
+ }
572
+ return result;
573
+ }
425
574
  /**
426
575
  * Provision MCP registrations for every created worktree row, in order.
427
576
  * Per-row failures are isolated — a failed row never aborts later rows.
@@ -10,6 +10,7 @@ import path from "path";
10
10
  import { getWorktreeHostTargets, hostAdapterForTarget } from "./mcp-host-targets.js";
11
11
  import { MCP_SERVER_NAME, MCP_PACKAGE_NAME, resolveRegistrationKey, } from "./mcp-identity.js";
12
12
  import { DUPLICATE_REGISTRATION_GUIDANCE } from "./launcher-config-inspection.js";
13
+ import { validateRepoName } from "./bridge-config.js";
13
14
  /**
14
15
  * Read and parse a JSON file.
15
16
  *
@@ -63,8 +64,33 @@ function flagValue(args, flag) {
63
64
  * In both forms `--target` must be `bapi` and `--project-root` must match the
64
65
  * given worktree exactly. The server entry path is NOT required to sit under any
65
66
  * particular username/path prefix.
67
+ *
68
+ * BAPI-1545: an executor worktree on the MCP-grant path carries a trailing
69
+ * `--grant-repo <repo>` pair. It is tolerated, and when present its value must
70
+ * be a usable repository name. Authority is never re-checked against
71
+ * `.bridge/config` here.
66
72
  */
67
73
  export function isBridgeApiShimEntry(entry, worktreeRoot) {
74
+ if (!isMcpInvokeShimEntry(entry, worktreeRoot))
75
+ return false;
76
+ const args = entry.args.filter((a) => typeof a === "string");
77
+ if (flagValue(args, "--target") !== "bapi")
78
+ return false;
79
+ return true;
80
+ }
81
+ /**
82
+ * BAPI-1545: the same shape check for any shim target, so a granted server's
83
+ * registration (`--target <name> --project-root <worktree> --grant-repo <repo>`)
84
+ * can be recognized. A granted entry must carry `--grant-repo`.
85
+ */
86
+ export function isGrantedShimEntry(entry, worktreeRoot, target) {
87
+ if (!isMcpInvokeShimEntry(entry, worktreeRoot))
88
+ return false;
89
+ const args = entry.args.filter((a) => typeof a === "string");
90
+ return flagValue(args, "--target") === target && flagValue(args, "--grant-repo") !== undefined;
91
+ }
92
+ /** The shared shim shape: launcher form, `mcp-invoke`, project root, and an optional valid `--grant-repo`. */
93
+ function isMcpInvokeShimEntry(entry, worktreeRoot) {
68
94
  if (!entry || typeof entry !== "object" || Array.isArray(entry))
69
95
  return false;
70
96
  const candidate = entry;
@@ -85,10 +111,13 @@ export function isBridgeApiShimEntry(entry, worktreeRoot) {
85
111
  }
86
112
  if (!args.includes("mcp-invoke"))
87
113
  return false;
88
- if (flagValue(args, "--target") !== "bapi")
89
- return false;
90
114
  if (flagValue(args, "--project-root") !== worktreeRoot)
91
115
  return false;
116
+ if (args.includes("--grant-repo")) {
117
+ const repo = flagValue(args, "--grant-repo");
118
+ if (repo === undefined || !validateRepoName(repo).ok)
119
+ return false;
120
+ }
92
121
  return true;
93
122
  }
94
123
  /**
@@ -50,7 +50,10 @@ export function isTransientNpxEntryPath(entryPath) {
50
50
  * `nodeExecutable` is returned as the standalone `command`, never concatenated
51
51
  * with the server entry path, so `EPIC_IMPLEMENTER_NODE_PATH` can substitute it later.
52
52
  */
53
- export function buildMcpShimCommand(invocation, target, absoluteWorktreePath) {
53
+ export function buildMcpShimCommand(invocation, target, absoluteWorktreePath, grantRepo) {
54
+ // BAPI-1545: `--grant-repo` is appended ONLY on the grant path, after the
55
+ // legacy arguments, so a call without it is byte-identical to before.
56
+ const grantArgs = grantRepo === undefined ? [] : ["--grant-repo", grantRepo];
54
57
  if (invocation.form === "absolute-build-path") {
55
58
  return {
56
59
  command: invocation.nodeExecutable,
@@ -61,6 +64,7 @@ export function buildMcpShimCommand(invocation, target, absoluteWorktreePath) {
61
64
  target,
62
65
  "--project-root",
63
66
  absoluteWorktreePath,
67
+ ...grantArgs,
64
68
  ],
65
69
  };
66
70
  }
@@ -74,6 +78,7 @@ export function buildMcpShimCommand(invocation, target, absoluteWorktreePath) {
74
78
  target,
75
79
  "--project-root",
76
80
  absoluteWorktreePath,
81
+ ...grantArgs,
77
82
  ],
78
83
  };
79
84
  }
@@ -166,3 +171,16 @@ export function resolveMcpShimInvocationForRuntime(deps) {
166
171
  packageSpec: deps.npmPackageSpec ?? DEFAULT_MCP_SERVER_NPM_CHANNEL_SPEC,
167
172
  };
168
173
  }
174
+ /**
175
+ * BAPI-1647: the argv prefix that runs an agent through the internal
176
+ * `with-credentials` wrapper, built from the same structured invocation the MCP
177
+ * shim uses (never by string-rewriting an MCP command). The agent argv follows
178
+ * the trailing `--`. Pure.
179
+ */
180
+ export function buildWithCredentialsPrefix(invocation, repoName) {
181
+ const tail = ["with-credentials", "--repo", repoName, "--"];
182
+ if (invocation.form === "absolute-build-path") {
183
+ return [invocation.nodeExecutable, invocation.serverEntryPath, ...tail];
184
+ }
185
+ return ["npx", "-y", invocation.packageSpec, ...tail];
186
+ }