@bridge_gpt/mcp-server 0.1.17 → 0.2.0

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 (36) hide show
  1. package/README.md +333 -197
  2. package/build/agent-capabilities/cli.js +152 -0
  3. package/build/agent-capabilities/default-deps.js +45 -0
  4. package/build/agent-capabilities/probe-context.js +111 -0
  5. package/build/agent-capabilities/probes.js +278 -0
  6. package/build/agent-capabilities/reporter.js +50 -0
  7. package/build/agent-capabilities/runner.js +56 -0
  8. package/build/agent-capabilities/types.js +10 -0
  9. package/build/agent-launchers/claude.js +4 -4
  10. package/build/agents.generated.js +1 -1
  11. package/build/brainstorm-files.js +89 -0
  12. package/build/bridge-config.js +404 -0
  13. package/build/chain-orchestrator.js +247 -33
  14. package/build/commands.generated.js +5 -5
  15. package/build/credential-materialization.js +128 -0
  16. package/build/credential-store.js +232 -0
  17. package/build/decision-page-schema.js +39 -6
  18. package/build/decision-page-template.js +54 -18
  19. package/build/doctor.js +18 -2
  20. package/build/git-ignore-utils.js +63 -0
  21. package/build/index.js +1510 -560
  22. package/build/mcp-invoke.js +417 -0
  23. package/build/mcp-provisioning.js +249 -0
  24. package/build/mcp-registration-doctor.js +96 -0
  25. package/build/pipeline-orchestrator.js +9 -1
  26. package/build/pipeline-utils.js +33 -0
  27. package/build/pipelines.generated.js +36 -5
  28. package/build/schedule-run.js +6 -6
  29. package/build/start-tickets-prereqs.js +90 -1
  30. package/build/start-tickets.js +106 -14
  31. package/build/third-party-mcp-targets.js +75 -0
  32. package/build/version.generated.js +1 -1
  33. package/package.json +3 -3
  34. package/pipelines/full-automation.json +3 -1
  35. package/pipelines/implement-ticket.json +28 -2
  36. package/smoke-test/SMOKE-TEST.md +4 -2
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Non-throwing probe runner. For each selected agent it builds a probe context,
3
+ * filters probes by `--only` and per-agent applicability, applies the skip-gate
4
+ * (binary absent, or an `env`-auth agent whose credential is unset → skip BEFORE
5
+ * spawning), runs each probe catching any throw as `fail`, and cleans up temp dirs.
6
+ */
7
+ import { resolveAgentSpec } from "../agent-registry.js";
8
+ import { createProbeContext } from "./probe-context.js";
9
+ import { ALL_PROBES } from "./probes.js";
10
+ import { AGENT_AUTH, } from "./types.js";
11
+ /** Apply the skip-gate, then run the probe (catching throws as `fail`). */
12
+ async function runOneProbe(deps, agentName, probe, ctx) {
13
+ if (probe.spawnsAgent) {
14
+ if (ctx.resolvedBinary === null) {
15
+ return { status: "skip", detail: `${ctx.agent.command} not on PATH — see binary-resolves` };
16
+ }
17
+ const auth = AGENT_AUTH[agentName];
18
+ if (auth.kind === "env") {
19
+ const value = deps.env[auth.varName];
20
+ if (!value || value.length === 0) {
21
+ return { status: "skip", detail: `${auth.varName} not set — set it to run this probe` };
22
+ }
23
+ }
24
+ // interactive auth (claude) cannot be verified from env — run and report the outcome.
25
+ }
26
+ try {
27
+ return await probe.run(ctx);
28
+ }
29
+ catch (err) {
30
+ return { status: "fail", detail: err instanceof Error ? err.message : String(err) };
31
+ }
32
+ }
33
+ export async function collectCapabilityResults(deps, options) {
34
+ const records = [];
35
+ for (const agentName of options.agents) {
36
+ const agent = resolveAgentSpec(agentName);
37
+ if (!agent)
38
+ continue; // defensive: agent names are validated by the CLI parser
39
+ const applicable = ALL_PROBES.filter((p) => p.appliesTo.includes(agentName) && (!options.only || options.only.includes(p.id)));
40
+ const { ctx, cleanup } = await createProbeContext(deps, agent, options.timeoutMs);
41
+ try {
42
+ for (const probe of applicable) {
43
+ const result = await runOneProbe(deps, agentName, probe, ctx);
44
+ records.push({ agent: agentName, probeId: probe.id, title: probe.title, result });
45
+ }
46
+ }
47
+ finally {
48
+ await cleanup();
49
+ }
50
+ }
51
+ return { records };
52
+ }
53
+ /** True when any probe failed or hung — drives the nonzero CLI exit code. */
54
+ export function hasFailureOrHang(collection) {
55
+ return collection.records.some((r) => r.result.status === "fail" || r.result.status === "hang");
56
+ }
@@ -0,0 +1,10 @@
1
+ /** Per-agent auth requirement used by the runner's skip-gate and the reporter. */
2
+ export const AGENT_AUTH = {
3
+ claude: {
4
+ kind: "interactive",
5
+ note: "Claude Code authenticates via keychain/OAuth — cannot be verified from the environment.",
6
+ },
7
+ "cursor-agent": { kind: "env", varName: "CURSOR_API_KEY" },
8
+ };
9
+ /** Default hard timeout per headless probe run (ms). The cursor `-p` hang bug is version-sensitive. */
10
+ export const DEFAULT_PROBE_TIMEOUT_MS = 90_000;
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Resolves the `claude` binary against the *baked schedule-time PATH* (not the
5
5
  * ambient process default), builds the locked
6
- * `/full-automation --scheduled-at <T> --idea-file <abs> [--auto-approve]`
6
+ * `/full-automation --scheduled-at <T> --idea-file <abs> [--auto]`
7
7
  * prompt, and emits `{ exe, args: ["-p", prompt] }`. Claude Code has no
8
8
  * working-directory flag, so the cwd is always set by the scheduler unit — this
9
9
  * adapter must never add a cwd argument to the invocation.
@@ -51,13 +51,13 @@ export function quoteIdeaFileForPrompt(ideaFile) {
51
51
  return `"${ideaFile.replace(/"/g, '\\"')}"`;
52
52
  }
53
53
  /**
54
- * Build the exact full-automation prompt. `--auto-approve` is appended by
55
- * default; it is omitted only when the caller selected `--no-auto-approve`.
54
+ * Build the exact full-automation prompt. `--auto` is appended by
55
+ * default; it is omitted only when the caller selected `--no-auto`.
56
56
  */
57
57
  export function buildClaudePrompt(input) {
58
58
  const base = `/full-automation --scheduled-at ${input.runAtIso} ` +
59
59
  `--idea-file ${quoteIdeaFileForPrompt(input.ideaFile)}`;
60
- return input.autoApprove ? `${base} --auto-approve` : base;
60
+ return input.autoApprove ? `${base} --auto` : base;
61
61
  }
62
62
  const CLAUDE_CAPABILITY = {
63
63
  name: "claude",
@@ -8,6 +8,6 @@ export const AGENTS = {
8
8
  "model": "opus",
9
9
  "color": "blue"
10
10
  },
11
- "body": "\nYou are an elite software engineering project manager and technical analyst with deep expertise in codebase archaeology and Jira ticket crafting. You excel at understanding complex codebases, identifying relevant existing code, and translating problem descriptions into precisely-scoped, actionable Jira tickets that engineers can pick up and execute with minimal ambiguity.\n\n## Your Mission\n\nGiven a problem description from the user, you will:\n1. Conduct thorough codebase research to understand the existing architecture, patterns, and relevant code\n2. Write a structured Jira ticket as a new markdown file that references specific files, functions, and patterns from the codebase\n\n## Phase 1: Deep Codebase Research\n\nThis is the most critical phase. You MUST spend significant time here before writing anything. Do NOT rush this phase.\n\n### Research Protocol\n\n1. **Understand the Problem Space**: Re-read the user's problem description carefully. Identify the domain, the affected areas, and the type of change needed (new feature, bug fix, refactor, enhancement).\n\n2. **Map the Relevant Architecture**: \n - Search for files, modules, and directories related to the problem domain\n - Read the key source files thoroughly — do not skim\n - Trace code paths: how does data flow through the relevant parts of the system?\n - Identify controller -> helper -> service -> model chains if applicable\n\n3. **Identify Extension Points**:\n - What existing code can be reused or extended?\n - What patterns does the codebase already use for similar functionality?\n - Are there helper functions, utilities, or base classes that should be leveraged?\n - Are there configuration files, metadata definitions, or templates that need modification?\n\n4. **Identify Constraints**:\n - What conventions does the project follow? (Check CLAUDE.md, README, existing patterns)\n - What testing patterns are used?\n - Are there ES5 limitations, specific framework patterns, or platform constraints?\n\n5. **Catalog Your Findings**: Keep mental notes of every relevant file path, function name, pattern, and architectural decision you discover. You will reference these in the ticket.\n\n### Research Depth Guidelines\n- Read at least 5-15 relevant source files in full, more if the problem is complex\n- Follow import chains to understand dependencies\n- Check test files to understand expected behaviors and testing patterns\n- Review configuration and metadata files if relevant\n- Search for TODO comments, known limitations, or related existing issues in the code\n\n## Phase 2: Write the Jira Ticket\n\nAfter completing research, create a new markdown file with the ticket. Use the naming convention `tickets/TICKET-<short-descriptive-name>.md`. If the `tickets/` directory does not exist, create it.\n\n### Ticket Structure\n\nThe markdown file MUST contain exactly these sections:\n\n```markdown\n# [Concise Title Describing the Task]\n\n## Summary\n\n[2-4 sentences describing what this task is about, why it matters, and the high-level approach. Be specific — reference the actual system components involved.]\n\n## Requirements\n\n[Numbered list of specific, actionable requirements. Each requirement should be a clear unit of work.]\n\n1. **[Requirement Title]**: [Description of what needs to be done.]\n - *Relevant code*: `path/to/file.js` — `functionName()` [brief note on how this code relates]\n - *Relevant code*: `path/to/other/file.js` — [brief note]\n\n2. **[Requirement Title]**: [Description]\n - *Relevant code*: ...\n\n[Continue for all requirements]\n\n## Acceptance Criteria\n\n[Checklist format using markdown checkboxes. Each criterion is a testable, verifiable condition.]\n\n- [ ] [Specific, testable criterion]\n- [ ] [Another criterion]\n- [ ] [Continue as needed]\n```\n\n### Writing Guidelines\n\n**Summary**:\n- Be concrete, not abstract. Name the actual components, cartridges, or subsystems involved.\n- State the \"why\" — what problem does this solve or what value does it add?\n- Mention the general technical approach if it's clear from the research.\n\n**Requirements**:\n- Each requirement should represent a logical unit of work\n- Order requirements in a logical implementation sequence when possible\n- ALWAYS cite relevant existing files and functions when they exist. Use exact file paths relative to the project root.\n- Explain HOW the existing code relates: \"extend this function\", \"follow this pattern\", \"reuse this helper\", \"modify this configuration\"\n- If a requirement involves creating new files, suggest where they should live based on existing project structure conventions\n- Be specific about what needs to change vs. what needs to be created new\n- Include requirements for tests, documentation, and configuration/metadata changes if applicable\n\n**Acceptance Criteria**:\n- Every criterion must be independently verifiable\n- Cover functional requirements, edge cases, testing, and non-functional requirements\n- Include criteria for backwards compatibility if relevant\n- Include criteria for test coverage\n- Use checkbox format (`- [ ]`) so engineers can track progress\n\n## Quality Standards\n\n- **No vague language**: Replace \"should handle errors properly\" with \"should catch LLM provider timeouts and return a normalized error response with errorType 'TimeoutError'\"\n- **No assumptions without evidence**: Only reference code you actually read during research. If you're unsure about something, say so explicitly in the ticket.\n- **Appropriate scope**: The ticket should represent a coherent, deliverable unit of work. If the problem is too large, note that it may need to be broken into sub-tasks, but still write the parent ticket.\n- **Developer empathy**: Write as if the developer picking this up has general project knowledge but hasn't recently worked on this specific area. Give them enough context to get started quickly.\n\n## Important Reminders\n\n- Do NOT skip or abbreviate the research phase. The quality of the ticket depends entirely on the depth of your codebase understanding.\n- Do NOT make up file paths or function names. Only reference code you have actually found and read.\n- DO create the markdown file — do not just output the content to the chat. Write it to disk.\n- If the project has specific conventions (from CLAUDE.md or similar), ensure your ticket's requirements align with those conventions.\n"
11
+ "body": "\nYou are an elite software engineering project manager and technical analyst with deep expertise in codebase archaeology and Jira ticket crafting. You excel at understanding complex codebases, identifying relevant existing code, and translating problem descriptions into precisely-scoped, actionable Jira tickets that engineers can pick up and execute with minimal ambiguity.\n\n## Your Mission\n\nGiven a problem description from the user, you will:\n1. Conduct thorough codebase research to understand the existing architecture, patterns, and relevant code\n2. Write a structured Jira ticket as a new markdown file that references specific files, functions, and patterns from the codebase\n\n## Phase 1: Deep Codebase Research\n\nThis is the most critical phase. You MUST spend significant time here before writing anything. Do NOT rush this phase.\n\n### Research Protocol\n\n1. **Understand the Problem Space**: Re-read the user's problem description carefully. Identify the domain, the affected areas, and the type of change needed (new feature, bug fix, refactor, enhancement).\n\n2. **Map the Relevant Architecture**: \n - Search for files, modules, and directories related to the problem domain\n - Read the key source files thoroughly — do not skim\n - Trace code paths: how does data flow through the relevant parts of the system?\n - Identify controller -> helper -> service -> model chains if applicable\n\n3. **Identify Extension Points**:\n - What existing code can be reused or extended?\n - What patterns does the codebase already use for similar functionality?\n - Are there helper functions, utilities, or base classes that should be leveraged?\n - Are there configuration files, metadata definitions, or templates that need modification?\n\n4. **Identify Constraints**:\n - What conventions does the project follow? (Check CLAUDE.md, README, existing patterns)\n - What testing patterns are used?\n - Are there ES5 limitations, specific framework patterns, or platform constraints?\n\n5. **Catalog Your Findings**: Keep mental notes of every relevant file path, function name, pattern, and architectural decision you discover. You will reference these in the ticket.\n\n### Research Depth Guidelines\n- Read at least 5-15 relevant source files in full, more if the problem is complex\n- Follow import chains to understand dependencies\n- Check test files to understand expected behaviors and testing patterns\n- Review configuration and metadata files if relevant\n- Search for TODO comments, known limitations, or related existing issues in the code\n\n## Phase 2: Write the Jira Ticket\n\nAfter completing research, create a new markdown file with the ticket. Use the naming convention `tickets/TICKET-<short-descriptive-name>.md`. If the `tickets/` directory does not exist, create it.\n\n### Ticket Structure\n\nThe markdown file MUST contain exactly these sections:\n\n```markdown\n# [Concise Title Describing the Task]\n\n## Summary\n\n[2-4 sentences describing what this task is about, why it matters, and the high-level approach. Be specific — reference the actual system components involved.]\n\n## Requirements\n\n[Numbered list of specific, actionable requirements. Each requirement should be a clear unit of work.]\n\n1. **[Requirement Title]**: [Description of what needs to be done.]\n - *Relevant code*: `path/to/file.js` — `functionName()` [brief note on how this code relates]\n - *Relevant code*: `path/to/other/file.js` — [brief note]\n\n2. **[Requirement Title]**: [Description]\n - *Relevant code*: ...\n\n[Continue for all requirements]\n\n## Acceptance Criteria\n\n[Bullet list. Each criterion is a testable, verifiable condition.]\n\n- [Specific, testable criterion]\n- [Another criterion]\n- [Continue as needed]\n```\n\n### Writing Guidelines\n\n**Summary**:\n- Be concrete, not abstract. Name the actual components, cartridges, or subsystems involved.\n- State the \"why\" — what problem does this solve or what value does it add?\n- Mention the general technical approach if it's clear from the research.\n\n**Requirements**:\n- Each requirement should represent a logical unit of work\n- Order requirements in a logical implementation sequence when possible\n- ALWAYS cite relevant existing files and functions when they exist. Use exact file paths relative to the project root.\n- Explain HOW the existing code relates: \"extend this function\", \"follow this pattern\", \"reuse this helper\", \"modify this configuration\"\n- If a requirement involves creating new files, suggest where they should live based on existing project structure conventions\n- Be specific about what needs to change vs. what needs to be created new\n- Include requirements for tests, documentation, and configuration/metadata changes if applicable\n\n**Acceptance Criteria**:\n- Every criterion must be independently verifiable\n- Cover functional requirements, edge cases, testing, and non-functional requirements\n- Include criteria for backwards compatibility if relevant\n- Include criteria for test coverage\n- Use plain `-` bullets (Jira's ADF has no native checkbox, so `- [ ]` renders as literal text)\n\n### Output Formatting (Jira upload)\n\nThe ticket is uploaded to Jira, which converts the Markdown to Atlassian Document Format (ADF) and hard-caps the description at **32,767 characters**. Keep the output clean and within budget:\n\n- **Length**: aim for under ~30,000 characters. If the scope genuinely needs more, split into a parent ticket plus sub-tickets rather than one oversized ticket.\n- **Acceptance Criteria**: plain `-` bullets, not `- [ ]` (ADF has no native checkbox).\n- **No images**: do not embed images or use relative image links.\n- **No empty headings**: every heading must have text on its line.\n- **Placeholders**: prefer `{placeholder}` over `<placeholder>`.\n\n## Quality Standards\n\n- **No vague language**: Replace \"should handle errors properly\" with \"should catch LLM provider timeouts and return a normalized error response with errorType 'TimeoutError'\"\n- **No assumptions without evidence**: Only reference code you actually read during research. If you're unsure about something, say so explicitly in the ticket.\n- **Appropriate scope**: The ticket should represent a coherent, deliverable unit of work. If the problem is too large, note that it may need to be broken into sub-tasks, but still write the parent ticket.\n- **Developer empathy**: Write as if the developer picking this up has general project knowledge but hasn't recently worked on this specific area. Give them enough context to get started quickly.\n\n## Important Reminders\n\n- Do NOT skip or abbreviate the research phase. The quality of the ticket depends entirely on the depth of your codebase understanding.\n- Do NOT make up file paths or function names. Only reference code you have actually found and read.\n- DO create the markdown file — do not just output the content to the chat. Write it to disk.\n- If the project has specific conventions (from CLAUDE.md or similar), ensure your ticket's requirements align with those conventions.\n"
12
12
  }
13
13
  };
@@ -0,0 +1,89 @@
1
+ // ---------------------------------------------------------------------------
2
+ // brainstorm-files
3
+ // ---------------------------------------------------------------------------
4
+ // Pure, side-effect-free helpers for naming and writing brainstorm result
5
+ // files. Extracted from ``index.ts`` so they can be unit-tested directly —
6
+ // ``index.ts`` self-executes its CLI/server at module top level and therefore
7
+ // cannot be imported from a test. ``index.ts`` imports these as the single
8
+ // source of truth (no logic is duplicated there).
9
+ import { writeFile, mkdir } from "fs/promises";
10
+ import path from "path";
11
+ /**
12
+ * Lowercase, strip non-alphanumeric runs, collapse to single hyphens, cap to
13
+ * ``maxLength`` characters, and trim a trailing hyphen. Shared generic helper
14
+ * (also used for deep-research query slugs in ``index.ts``).
15
+ */
16
+ export function slugify(text, maxLength = 60) {
17
+ return text
18
+ .toLowerCase()
19
+ .replace(/[^a-z0-9\s-]/g, "")
20
+ .trim()
21
+ .replace(/\s+/g, "-")
22
+ .replace(/-+/g, "-")
23
+ .slice(0, maxLength)
24
+ .replace(/-$/, "");
25
+ }
26
+ export function sanitizeProviderForFilename(provider) {
27
+ // Defensive: allow letters/digits/hyphen/underscore only; collapse runs and
28
+ // trim trailing punctuation. Falls back to "provider" for an empty result.
29
+ const cleaned = provider
30
+ .toLowerCase()
31
+ .replace(/[^a-z0-9_-]+/g, "-")
32
+ .replace(/-+/g, "-")
33
+ .replace(/^-+|-+$/g, "");
34
+ return cleaned || "provider";
35
+ }
36
+ /**
37
+ * Build the on-disk filename for one brainstorm result row.
38
+ *
39
+ * When the original task ``subject`` is available and slugifies to a non-empty
40
+ * string, use a human-readable semantic name:
41
+ * ``{slugified-subject}-{short_brainstorm_id}-{provider}.md``
42
+ * where ``short_brainstorm_id`` is the first 8 chars of the brainstorm UUID
43
+ * (enough for practical uniqueness / idempotent overwrite on re-runs).
44
+ *
45
+ * The UUID-only pattern ``{brainstorm_id}-{provider}.md`` is the intentional
46
+ * backward-compatible fallback, used only when no subject is provided, the
47
+ * subject is whitespace-only, or ``slugify(subject)`` returns an empty string
48
+ * (e.g. punctuation-only). This keeps callers without a subject — notably the
49
+ * ``get_brainstorm`` retrieval path, whose result envelope does not echo
50
+ * ``task_description`` — safe and unchanged.
51
+ */
52
+ export function buildBrainstormResultFilename(envelope, row, subject) {
53
+ const providerSegment = sanitizeProviderForFilename(row.provider);
54
+ const subjectSlug = subject ? slugify(subject) : "";
55
+ const shortId = envelope.brainstorm_id.slice(0, 8);
56
+ if (subjectSlug) {
57
+ return `${subjectSlug}-${shortId}-${providerSegment}.md`;
58
+ }
59
+ return `${envelope.brainstorm_id}-${providerSegment}.md`;
60
+ }
61
+ /**
62
+ * Write each brainstorm result row's markdown into ``dir`` using
63
+ * ``buildBrainstormResultFilename`` for the name. ``index.ts`` wraps this with
64
+ * the resolved ``BAPI_DOCS_DIR/brainstorm`` directory.
65
+ *
66
+ * Behavior is fail-open: rows without markdown are skipped, the directory is
67
+ * created recursively, markdown is written as UTF-8, and per-row write failures
68
+ * are swallowed so saving never blocks the tool response.
69
+ */
70
+ export async function saveBrainstormResultsToDir(envelope, dir, subject) {
71
+ const savedPaths = [];
72
+ for (const row of envelope.results) {
73
+ const markdown = row.markdown;
74
+ if (!markdown) {
75
+ continue;
76
+ }
77
+ const filename = buildBrainstormResultFilename(envelope, row, subject);
78
+ const filePath = path.join(dir, filename);
79
+ try {
80
+ await mkdir(dir, { recursive: true });
81
+ await writeFile(filePath, markdown, "utf-8");
82
+ savedPaths.push(filePath);
83
+ }
84
+ catch {
85
+ // Skip rows that fail to write — never block the response.
86
+ }
87
+ }
88
+ return savedPaths;
89
+ }
@@ -0,0 +1,404 @@
1
+ /**
2
+ * Reader and validator for the committed, secret-free `.bridge/config` manifest.
3
+ *
4
+ * The manifest is a tiny, hand-written TOML subset — `repo_name` plus repeated
5
+ * `[[mcp]]` target blocks — committed at the repo root and inherited by every
6
+ * git worktree. It NEVER contains credentials; this module only resolves repo
7
+ * identity and which MCP targets a worktree should be provisioned for.
8
+ *
9
+ * Everything is dependency-free (no TOML runtime dependency) and dependency
10
+ * injected (`readFile`, optional `runCommand`) so it is unit-testable without
11
+ * touching the filesystem or invoking real `git`.
12
+ */
13
+ import path from "path";
14
+ /**
15
+ * True if a string contains any ASCII control character (0x00-0x1f or 0x7f).
16
+ * Keeps identifiers path-safe and free of anything that could smuggle into a
17
+ * shell argument or filename. Implemented by char-code scan to avoid a
18
+ * control-character regex literal in source.
19
+ */
20
+ function hasControlChars(value) {
21
+ for (let i = 0; i < value.length; i++) {
22
+ const code = value.charCodeAt(i);
23
+ if (code <= 0x1f || code === 0x7f)
24
+ return true;
25
+ }
26
+ return false;
27
+ }
28
+ // ---------------------------------------------------------------------------
29
+ // Identifier validation
30
+ // ---------------------------------------------------------------------------
31
+ /**
32
+ * Validate a repo-name identifier. Requires a trimmed, non-empty string with no
33
+ * path separators or ASCII control characters. Returns a structured result with
34
+ * the trimmed value rather than throwing.
35
+ */
36
+ export function validateRepoName(raw) {
37
+ if (typeof raw !== "string") {
38
+ return { ok: false, error: "repo_name must be a string" };
39
+ }
40
+ const value = raw.trim();
41
+ if (value.length === 0) {
42
+ return { ok: false, error: "repo_name must be a non-empty string" };
43
+ }
44
+ if (value.includes("/") || value.includes("\\")) {
45
+ return { ok: false, error: "repo_name must not contain path separators" };
46
+ }
47
+ if (hasControlChars(value)) {
48
+ return { ok: false, error: "repo_name must not contain control characters" };
49
+ }
50
+ return { ok: true, value };
51
+ }
52
+ /**
53
+ * Validate an MCP target identifier. Same path-safety rules as repo names.
54
+ * Non-`bapi` targets validate successfully (parsing is not hard-coded to
55
+ * `bapi`); only `bapi` is acted on elsewhere for this MVP.
56
+ */
57
+ export function validateMcpTarget(raw) {
58
+ if (typeof raw !== "string") {
59
+ return { ok: false, error: "mcp target must be a string" };
60
+ }
61
+ const value = raw.trim();
62
+ if (value.length === 0) {
63
+ return { ok: false, error: "mcp target must be a non-empty string" };
64
+ }
65
+ if (value.includes("/") || value.includes("\\")) {
66
+ return { ok: false, error: "mcp target must not contain path separators" };
67
+ }
68
+ if (hasControlChars(value)) {
69
+ return { ok: false, error: "mcp target must not contain control characters" };
70
+ }
71
+ return { ok: true, value };
72
+ }
73
+ // ---------------------------------------------------------------------------
74
+ // TOML subset parser
75
+ // ---------------------------------------------------------------------------
76
+ /** Extract the inner content of a `"..."` double-quoted string, or null. */
77
+ function parseQuotedString(value) {
78
+ if (value.length < 2)
79
+ return null;
80
+ if (value[0] !== '"' || value[value.length - 1] !== '"')
81
+ return null;
82
+ const inner = value.slice(1, -1);
83
+ // The supported subset does not include escapes or embedded quotes.
84
+ if (inner.includes('"'))
85
+ return null;
86
+ return inner;
87
+ }
88
+ /**
89
+ * Parse a minimal, dependency-free TOML string array: `[ "a", "b" ]`. Elements
90
+ * are double-quoted, comma-free strings; whitespace around brackets, commas, and
91
+ * elements is ignored. An empty `[]` yields `[]`. Returns null on any malformed
92
+ * input (so callers can emit a secret-safe error that never echoes the value).
93
+ */
94
+ function parseStringArray(value) {
95
+ const trimmed = value.trim();
96
+ if (trimmed.length < 2 || trimmed[0] !== "[" || trimmed[trimmed.length - 1] !== "]") {
97
+ return null;
98
+ }
99
+ const inner = trimmed.slice(1, -1).trim();
100
+ if (inner.length === 0)
101
+ return [];
102
+ const parts = inner.split(",");
103
+ const out = [];
104
+ for (const part of parts) {
105
+ const element = parseQuotedString(part.trim());
106
+ if (element === null)
107
+ return null;
108
+ out.push(element);
109
+ }
110
+ return out;
111
+ }
112
+ /**
113
+ * Parse the supported `.bridge/config` TOML subset:
114
+ * - a single top-level `repo_name = "..."`
115
+ * - zero or more `[[mcp]]` sections, each with `target = "..."`
116
+ * - `#` comments, blank lines, and surrounding whitespace
117
+ *
118
+ * Anything outside this subset (other keys, single-bracket tables, unquoted or
119
+ * unterminated strings, duplicate `repo_name`, `target` outside `[[mcp]]`) is a
120
+ * structured error. Error messages reference line numbers and key names only —
121
+ * never the raw manifest content or a value — so secrets can never leak through
122
+ * a misplaced line.
123
+ */
124
+ export function parseBridgeConfigToml(text) {
125
+ const lines = text.split("\n");
126
+ let repoName;
127
+ let sawRepoName = false;
128
+ const mcp = [];
129
+ // null = top-level scope; otherwise points at the in-progress [[mcp]] block.
130
+ let currentMcp = null;
131
+ for (let i = 0; i < lines.length; i++) {
132
+ const lineNo = i + 1;
133
+ const line = lines[i].trim();
134
+ if (line.length === 0 || line.startsWith("#"))
135
+ continue;
136
+ if (line === "[[mcp]]") {
137
+ currentMcp = { headerLine: lineNo };
138
+ mcp.push(currentMcp);
139
+ continue;
140
+ }
141
+ // Any other bracketed table header is unsupported.
142
+ if (line.startsWith("[")) {
143
+ return {
144
+ ok: false,
145
+ kind: "parse-error",
146
+ error: `Unsupported table header on line ${lineNo}; only [[mcp]] is allowed`,
147
+ };
148
+ }
149
+ const eq = line.indexOf("=");
150
+ if (eq === -1) {
151
+ return {
152
+ ok: false,
153
+ kind: "parse-error",
154
+ error: `Malformed line ${lineNo}; expected key = "value"`,
155
+ };
156
+ }
157
+ const key = line.slice(0, eq).trim();
158
+ const rawValue = line.slice(eq + 1).trim();
159
+ if (key.length === 0) {
160
+ return {
161
+ ok: false,
162
+ kind: "parse-error",
163
+ error: `Malformed line ${lineNo}; missing key`,
164
+ };
165
+ }
166
+ if (currentMcp === null) {
167
+ // Top-level scope: only repo_name is allowed.
168
+ if (key === "repo_name") {
169
+ if (sawRepoName) {
170
+ return {
171
+ ok: false,
172
+ kind: "parse-error",
173
+ error: `Duplicate repo_name on line ${lineNo}`,
174
+ };
175
+ }
176
+ sawRepoName = true;
177
+ const stringValue = parseQuotedString(rawValue);
178
+ if (stringValue === null) {
179
+ return {
180
+ ok: false,
181
+ kind: "parse-error",
182
+ error: `Expected a double-quoted string for '${key}' on line ${lineNo}`,
183
+ };
184
+ }
185
+ const validated = validateRepoName(stringValue);
186
+ if (!validated.ok) {
187
+ return { ok: false, kind: "validation-error", error: validated.error };
188
+ }
189
+ repoName = validated.value;
190
+ continue;
191
+ }
192
+ if (key === "target") {
193
+ return {
194
+ ok: false,
195
+ kind: "parse-error",
196
+ error: `target on line ${lineNo} must appear inside an [[mcp]] section`,
197
+ };
198
+ }
199
+ return {
200
+ ok: false,
201
+ kind: "parse-error",
202
+ error: `Unsupported key '${key}' on line ${lineNo}`,
203
+ };
204
+ }
205
+ // Inside an [[mcp]] block: target / command / args / secret_bundle.
206
+ if (key === "args") {
207
+ if (currentMcp.args !== undefined) {
208
+ return { ok: false, kind: "parse-error", error: `Duplicate args on line ${lineNo}` };
209
+ }
210
+ const arr = parseStringArray(rawValue);
211
+ if (arr === null) {
212
+ return {
213
+ ok: false,
214
+ kind: "parse-error",
215
+ error: `Expected a string array for 'args' on line ${lineNo}`,
216
+ };
217
+ }
218
+ currentMcp.args = arr;
219
+ continue;
220
+ }
221
+ if (key === "target" || key === "command" || key === "secret_bundle") {
222
+ const stringValue = parseQuotedString(rawValue);
223
+ if (stringValue === null) {
224
+ return {
225
+ ok: false,
226
+ kind: "parse-error",
227
+ error: `Expected a double-quoted string for '${key}' on line ${lineNo}`,
228
+ };
229
+ }
230
+ if (key === "target") {
231
+ if (currentMcp.target !== undefined) {
232
+ return { ok: false, kind: "parse-error", error: `Duplicate target on line ${lineNo}` };
233
+ }
234
+ const validated = validateMcpTarget(stringValue);
235
+ if (!validated.ok) {
236
+ return { ok: false, kind: "validation-error", error: validated.error };
237
+ }
238
+ currentMcp.target = validated.value;
239
+ continue;
240
+ }
241
+ if (key === "command") {
242
+ if (currentMcp.command !== undefined) {
243
+ return { ok: false, kind: "parse-error", error: `Duplicate command on line ${lineNo}` };
244
+ }
245
+ currentMcp.command = stringValue;
246
+ continue;
247
+ }
248
+ // secret_bundle
249
+ if (currentMcp.secretBundle !== undefined) {
250
+ return { ok: false, kind: "parse-error", error: `Duplicate secret_bundle on line ${lineNo}` };
251
+ }
252
+ currentMcp.secretBundle = stringValue;
253
+ continue;
254
+ }
255
+ return {
256
+ ok: false,
257
+ kind: "parse-error",
258
+ error: `Unsupported key '${key}' inside [[mcp]] on line ${lineNo}`,
259
+ };
260
+ }
261
+ if (!sawRepoName || repoName === undefined) {
262
+ return { ok: false, kind: "validation-error", error: "Missing required repo_name" };
263
+ }
264
+ const cleaned = [];
265
+ for (const entry of mcp) {
266
+ if (entry.target === undefined) {
267
+ return {
268
+ ok: false,
269
+ kind: "validation-error",
270
+ error: `An [[mcp]] section on line ${entry.headerLine} is missing its target`,
271
+ };
272
+ }
273
+ if (entry.target !== "bapi") {
274
+ // Tier-2 targets must declare a launch command, an args array, and the
275
+ // store bundle that provides their secrets.
276
+ if (entry.command === undefined || entry.command.trim().length === 0) {
277
+ return {
278
+ ok: false,
279
+ kind: "validation-error",
280
+ error: `[[mcp]] target '${entry.target}' on line ${entry.headerLine} requires a non-empty command`,
281
+ };
282
+ }
283
+ if (entry.args === undefined) {
284
+ return {
285
+ ok: false,
286
+ kind: "validation-error",
287
+ error: `[[mcp]] target '${entry.target}' on line ${entry.headerLine} requires an args array`,
288
+ };
289
+ }
290
+ if (entry.secretBundle === undefined || entry.secretBundle.trim().length === 0) {
291
+ return {
292
+ ok: false,
293
+ kind: "validation-error",
294
+ error: `[[mcp]] target '${entry.target}' on line ${entry.headerLine} requires a non-empty secret_bundle`,
295
+ };
296
+ }
297
+ }
298
+ const clean = { target: entry.target };
299
+ if (entry.command !== undefined)
300
+ clean.command = entry.command;
301
+ if (entry.args !== undefined)
302
+ clean.args = entry.args;
303
+ if (entry.secretBundle !== undefined)
304
+ clean.secretBundle = entry.secretBundle;
305
+ cleaned.push(clean);
306
+ }
307
+ return { ok: true, manifest: { repoName, mcp: cleaned } };
308
+ }
309
+ // ---------------------------------------------------------------------------
310
+ // Reading + identity resolution
311
+ // ---------------------------------------------------------------------------
312
+ /** Absolute path to a project root's committed manifest. */
313
+ export function bridgeConfigPath(projectRoot) {
314
+ return path.join(projectRoot, ".bridge", "config");
315
+ }
316
+ /**
317
+ * Read and parse `<projectRoot>/.bridge/config`. A missing file is the common,
318
+ * non-fatal case (`kind: "missing"`); malformed/invalid content surfaces as a
319
+ * structured error. Raw manifest content is never echoed back.
320
+ */
321
+ export async function readBridgeConfig(projectRoot, deps) {
322
+ const filePath = bridgeConfigPath(projectRoot);
323
+ let raw;
324
+ try {
325
+ raw = await deps.readFile(filePath);
326
+ }
327
+ catch (err) {
328
+ if (err && typeof err === "object" && err.code === "ENOENT") {
329
+ return { ok: false, kind: "missing" };
330
+ }
331
+ return { ok: false, kind: "parse-error", error: "Unable to read .bridge/config" };
332
+ }
333
+ return parseBridgeConfigToml(raw);
334
+ }
335
+ /** True only when the manifest contains an exact target match (e.g. `"bapi"`). */
336
+ export function hasMcpTarget(manifest, target) {
337
+ return manifest.mcp.some((entry) => entry.target === target);
338
+ }
339
+ /**
340
+ * Derive the repo name from `git rev-parse --git-common-dir`, run with the
341
+ * passed `projectRoot` as `cwd` (so a worktree resolves to its parent repo's
342
+ * identity). The common dir is the directory immediately before the `.git`
343
+ * path segment, e.g. `<repo>/bapi/.git` and `<repo>/bapi/.git/worktrees/X` both
344
+ * derive `bapi`.
345
+ */
346
+ export async function deriveRepoNameFromGitCommonDir(projectRoot, deps) {
347
+ if (!deps.runCommand) {
348
+ return { ok: false, error: "Cannot derive repo name: no command runner available" };
349
+ }
350
+ let result;
351
+ try {
352
+ result = await deps.runCommand("git", ["rev-parse", "--git-common-dir"], {
353
+ cwd: projectRoot,
354
+ });
355
+ }
356
+ catch (err) {
357
+ const message = err instanceof Error ? err.message : String(err);
358
+ return { ok: false, error: `git rev-parse --git-common-dir failed: ${message}` };
359
+ }
360
+ if (result.exitCode !== 0) {
361
+ const reason = (result.stderr || result.stdout || "").trim();
362
+ return {
363
+ ok: false,
364
+ error: `git rev-parse --git-common-dir failed${reason ? `: ${reason}` : ""}`,
365
+ };
366
+ }
367
+ const commonDir = result.stdout.trim();
368
+ if (commonDir.length === 0) {
369
+ return { ok: false, error: "git rev-parse --git-common-dir returned no output" };
370
+ }
371
+ const resolved = path.isAbsolute(commonDir)
372
+ ? commonDir
373
+ : path.resolve(projectRoot, commonDir);
374
+ const segments = resolved.split(/[\\/]+/).filter((s) => s.length > 0);
375
+ const gitIndex = segments.lastIndexOf(".git");
376
+ if (gitIndex < 1) {
377
+ return {
378
+ ok: false,
379
+ error: "Unable to derive repo name from git common dir",
380
+ };
381
+ }
382
+ const derived = segments[gitIndex - 1];
383
+ const validated = validateRepoName(derived);
384
+ if (!validated.ok) {
385
+ return { ok: false, error: `Derived repo name is invalid: ${validated.error}` };
386
+ }
387
+ return { ok: true, value: validated.value };
388
+ }
389
+ /**
390
+ * Resolve the repo name for a project root. A valid manifest `repo_name` wins;
391
+ * a *missing* manifest falls back to the git-common-dir derivation; a malformed
392
+ * or invalid manifest returns a structured error rather than silently falling
393
+ * back (so a broken committed file is never papered over by git guessing).
394
+ */
395
+ export async function resolveRepoNameForProjectRoot(projectRoot, deps) {
396
+ const read = await readBridgeConfig(projectRoot, deps);
397
+ if (read.ok) {
398
+ return { ok: true, value: read.manifest.repoName };
399
+ }
400
+ if (read.kind === "missing") {
401
+ return deriveRepoNameFromGitCommonDir(projectRoot, deps);
402
+ }
403
+ return { ok: false, error: read.error };
404
+ }