@bridge_gpt/mcp-server 0.2.67 → 0.2.68
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.
- package/README.md +178 -15
- package/build/agent-launchers/claude-executor-adapter.js +27 -0
- package/build/agents.generated.js +22 -2
- package/build/bitbucket-workspace-remote.js +136 -0
- package/build/bootstrap-project-identity.js +24 -0
- package/build/bridge-permissions.js +829 -0
- package/build/claude-settings.js +18 -0
- package/build/cli-help.js +21 -0
- package/build/command-assets-doctor.js +24 -70
- package/build/command-provisioning.js +29 -5
- package/build/commands.generated.js +8 -8
- package/build/conductor/bridge-api-client.js +714 -0
- package/build/conductor/command-drift-production.js +92 -0
- package/build/conductor/command-drift.js +89 -0
- package/build/conductor/doctor.js +69 -26
- package/build/conductor/epic-branch-ci-inspection.js +1375 -0
- package/build/conductor/epic-implementer-cli.js +76 -22
- package/build/conductor/gate-discovery.js +890 -0
- package/build/conductor/install-doctor.js +15 -3
- package/build/conductor/json-output.js +63 -0
- package/build/conductor/readiness-cli.js +95 -10
- package/build/conductor/readiness-sections.js +369 -3
- package/build/conductor/readiness.js +211 -8
- package/build/conductor/recovery-cli.js +110 -4
- package/build/conductor/recovery-operations.js +99 -1
- package/build/connect-bitbucket-api.js +41 -6
- package/build/connect-bitbucket.js +106 -33
- package/build/credential-store.js +207 -47
- package/build/docs.generated.js +57 -3
- package/build/doctor.js +92 -1
- package/build/drive-epic.js +45 -8
- package/build/epic-coherence-input.test-fixtures.js +4 -3
- package/build/epic-implementer-bin.js +1374 -25
- package/build/epic-workarounds-path.js +39 -0
- package/build/executor/cli.js +30 -0
- package/build/executor/completion-contract.js +18 -5
- package/build/executor/deps.js +4 -1
- package/build/executor/gate-qualification-job.js +499 -0
- package/build/executor/guarded-fast-forward-push.js +129 -0
- package/build/executor/integration-job.js +122 -8
- package/build/executor/job-errors.js +8 -1
- package/build/executor/job-runner.js +589 -47
- package/build/executor/likely-tests.js +290 -0
- package/build/executor/live-worker-registry.js +1 -1
- package/build/executor/observation.js +31 -2
- package/build/executor/packaged-recipe.js +106 -0
- package/build/executor/permissions.js +115 -28
- package/build/executor/platform.js +1 -1
- package/build/executor/preflight.js +61 -12
- package/build/executor/process-containment.js +36 -12
- package/build/executor/process.js +15 -7
- package/build/executor/prompt-spec.js +33 -1
- package/build/executor/recently-failed-tests-file.js +71 -0
- package/build/executor/reconcile-overlap-job.js +41 -23
- package/build/executor/required-verification.js +1 -1
- package/build/executor/required-work-ledger.js +105 -17
- package/build/executor/retirement.js +69 -0
- package/build/executor/runner.js +114 -11
- package/build/executor/test-clock.js +15 -0
- package/build/executor/types.js +32 -0
- package/build/executor/viewer-tabs.js +58 -8
- package/build/executor/worker-command.js +23 -7
- package/build/executor/worker-diagnostics.js +37 -0
- package/build/executor/worker-finalization.js +31 -5
- package/build/executor/worker-guard-hook.js +179 -25
- package/build/executor/worker-log.js +4 -3
- package/build/executor/worker-verification-guidance.js +37 -0
- package/build/executor/worktree.js +88 -11
- package/build/first-build-advisory.js +97 -0
- package/build/generated-assets-advisory.js +14 -0
- package/build/generated-assets-catalog.js +66 -0
- package/build/generated-assets-context.js +46 -0
- package/build/generated-assets-dependencies.js +26 -0
- package/build/generated-assets-doctor.js +50 -0
- package/build/generated-assets-health.js +65 -0
- package/build/generated-assets-host.js +17 -0
- package/build/generated-assets-inventory.js +17 -0
- package/build/generated-assets-paths.js +60 -0
- package/build/generated-assets-publisher.js +133 -0
- package/build/generated-assets-remedy.js +25 -0
- package/build/generated-assets-repair.js +72 -0
- package/build/generated-assets-session.js +71 -0
- package/build/generated-assets-test-support.js +35 -0
- package/build/index.js +964 -768
- package/build/init.js +54 -177
- package/build/install-bridge-conductor-gate.js +1372 -0
- package/build/install-bridge-conductor.js +289 -36
- package/build/install-bridge.js +544 -37
- package/build/mcp-install-state.js +50 -4
- package/build/merge-workflow-install.js +3 -4
- package/build/pipeline-utils.js +6 -2
- package/build/pipelines.generated.js +22 -142
- package/build/plan-execution-ledger.js +2 -32
- package/build/plane/manifest.js +16 -1
- package/build/plane/preflight.js +54 -1
- package/build/printed-remedy-commands.js +16 -1
- package/build/readme.generated.js +1 -1
- package/build/repair-assets.js +93 -0
- package/build/run-unit-tests-launcher.js +130 -49
- package/build/secret-redaction.js +33 -0
- package/build/setup-epic.js +108 -3
- package/build/start-tickets-prereqs.js +70 -16
- package/build/start-tickets.js +29 -5
- package/build/tool-surface-gating.js +36 -0
- package/build/upgrade-cli.js +55 -1
- package/build/version.generated.js +3 -3
- package/build/worker-guard-hook-bin.js +3 -2
- package/build/worker-guard-hook-main.js +1 -1
- package/build/worktree-core.js +15 -2
- package/docs/EPIC-IMPLEMENTER-INSTALL.md +30 -12
- package/docs/EPIC-IMPLEMENTER.md +68 -2
- package/package.json +3 -3
- package/pipelines/greenfield-setup.json +4 -128
- package/pipelines/implement-ticket.json +3 -2
|
@@ -0,0 +1,829 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bridge-managed Claude Code permission rules for the `/drive-epic-run`
|
|
3
|
+
* supervisor (BAPI-1398, AC-8).
|
|
4
|
+
*
|
|
5
|
+
* The supervisor runs in auto mode. Every in-scope action it takes — a
|
|
6
|
+
* sanctioned CLI verb, a Bridge MCP tool, a read, a tally write — must resolve
|
|
7
|
+
* without a permission prompt or a classifier denial. Narrow
|
|
8
|
+
* `permissions.allow` rules resolve before the auto-mode classifier, so the
|
|
9
|
+
* installer writes one exact rule for every shell and tool form the posture
|
|
10
|
+
* prescribes, into the project's LOCAL settings file,
|
|
11
|
+
* `.claude/settings.local.json`. Claude Code reads that file as project-scoped
|
|
12
|
+
* settings, and it is per developer and gitignored. The rules must never go into
|
|
13
|
+
* the committed `.claude/settings.json`: they can hold a developer's own
|
|
14
|
+
* launcher path, they belong to one developer's manifest, and a committed allow
|
|
15
|
+
* rule would widen every teammate's sessions. The MCP tool rules are written
|
|
16
|
+
* under the key the server is actually registered as in `.mcp.json` (canonical
|
|
17
|
+
* `bridge`, or a kept legacy registration key), so they match the tool
|
|
18
|
+
* names Claude Code really sees. The auto-mode
|
|
19
|
+
* classifier ignores `autoMode` in project settings, so the prose
|
|
20
|
+
* `autoMode.allow` / `autoMode.environment` entries go into the USER's
|
|
21
|
+
* `~/.claude/settings.json` — and only with the user's explicit consent.
|
|
22
|
+
*
|
|
23
|
+
* Ownership. A JSON string in an allow list cannot carry a marker, and Claude
|
|
24
|
+
* Code's settings loader is not given unknown keys. Bridge therefore records the
|
|
25
|
+
* exact strings it inserted in its own manifest, `.bridge/bridge-permissions.json`
|
|
26
|
+
* (gitignored, per developer). `doctor` reads it to report present / missing /
|
|
27
|
+
* drifted, and removal deletes exactly those strings. A string the user already
|
|
28
|
+
* had before Bridge wrote it is never claimed, so removal never takes it away.
|
|
29
|
+
* The manifest is separate from `.bridge/install-state.json` because that file's
|
|
30
|
+
* contract forbids absolute home paths, and a local launcher override is often
|
|
31
|
+
* one.
|
|
32
|
+
*
|
|
33
|
+
* File discipline matches `claude-settings.ts`: a missing settings file is `{}`;
|
|
34
|
+
* a malformed one is reported as `malformed` and never written; changed files
|
|
35
|
+
* are written with a 2-space indent and one trailing newline; an unchanged file
|
|
36
|
+
* is not rewritten, so a second run leaves every file byte-identical.
|
|
37
|
+
*
|
|
38
|
+
* Never broad: no `Bash(*)`, no wildcard launcher, and no rule that covers a
|
|
39
|
+
* credential file. A launcher override that cannot be written as an exact,
|
|
40
|
+
* secret-free command prefix is refused rather than approximated.
|
|
41
|
+
*/
|
|
42
|
+
import path from "node:path";
|
|
43
|
+
import { MCP_PACKAGE_NAME, MCP_SERVER_NAME, resolveRegistrationKey } from "./mcp-identity.js";
|
|
44
|
+
// ---------------------------------------------------------------------------
|
|
45
|
+
// Constants
|
|
46
|
+
// ---------------------------------------------------------------------------
|
|
47
|
+
/** The environment variable the posture reads for a local launcher override. */
|
|
48
|
+
export const BRIDGE_LAUNCHER_ENV = "BAPI_MCP_CLI";
|
|
49
|
+
/** The default packaged launcher prefix — byte-identical to the posture's default. */
|
|
50
|
+
export const BRIDGE_DEFAULT_LAUNCHER = `npx -y ${MCP_PACKAGE_NAME}`;
|
|
51
|
+
/** Repository-relative path of Bridge's permission-ownership manifest. */
|
|
52
|
+
export const BRIDGE_PERMISSIONS_MANIFEST_RELPATH = ".bridge/bridge-permissions.json";
|
|
53
|
+
/**
|
|
54
|
+
* Repository-relative path of the project settings file the rules go into. It is
|
|
55
|
+
* the per-developer, gitignored local file — never the committed
|
|
56
|
+
* `.claude/settings.json`.
|
|
57
|
+
*/
|
|
58
|
+
export const BRIDGE_PROJECT_SETTINGS_RELPATH = ".claude/settings.local.json";
|
|
59
|
+
/** Current manifest schema version. */
|
|
60
|
+
export const BRIDGE_PERMISSIONS_MANIFEST_VERSION = 1;
|
|
61
|
+
/** The auto-mode sentinel that keeps Claude Code's built-in entries in a list. */
|
|
62
|
+
export const AUTO_MODE_DEFAULTS_SENTINEL = "$defaults";
|
|
63
|
+
/**
|
|
64
|
+
* The sanctioned recovery verbs, as their packaged CLI subcommands, in
|
|
65
|
+
* the posture's verb-table order. Adding a verb here without adding it to the
|
|
66
|
+
* posture's verb table (or the reverse) is caught by the static coverage test.
|
|
67
|
+
*/
|
|
68
|
+
export const BRIDGE_SANCTIONED_VERB_SUBCOMMANDS = [
|
|
69
|
+
"unpark",
|
|
70
|
+
"continue-child",
|
|
71
|
+
"grant-continuation",
|
|
72
|
+
"adopt-current-head-and-unpark",
|
|
73
|
+
"stop-run",
|
|
74
|
+
"abandon-run",
|
|
75
|
+
"scope-reparse",
|
|
76
|
+
"dispatch-remediation",
|
|
77
|
+
"merge-child",
|
|
78
|
+
"scope-catch-up",
|
|
79
|
+
"patch-policy",
|
|
80
|
+
"resume-run",
|
|
81
|
+
"resume-ci",
|
|
82
|
+
"rerun-ci",
|
|
83
|
+
];
|
|
84
|
+
/** The Bridge MCP tools the supervisor calls. */
|
|
85
|
+
export const BRIDGE_SUPERVISOR_MCP_TOOLS = [
|
|
86
|
+
"ping",
|
|
87
|
+
"explain_run",
|
|
88
|
+
"get_epic_snapshot",
|
|
89
|
+
"poll_ci_checks",
|
|
90
|
+
"second_opinion",
|
|
91
|
+
"report_mcp_bug",
|
|
92
|
+
// BAPI-1399: the prerequisite scan reads the epic and its child tickets.
|
|
93
|
+
"get_ticket",
|
|
94
|
+
];
|
|
95
|
+
/** The private run-state directory the tally, patch, and context files live in. */
|
|
96
|
+
export const BRIDGE_RUN_STATE_DIR = "~/.local/state/bapi-conductor-runs";
|
|
97
|
+
/**
|
|
98
|
+
* Launcher override characters that cannot be part of an exact, safe rule
|
|
99
|
+
* prefix: shell operators, quotes (the posture writes the launcher unquoted),
|
|
100
|
+
* rule-pattern metacharacters, an environment assignment (`=`), and a home
|
|
101
|
+
* shorthand that the shell would expand differently from the rule.
|
|
102
|
+
*/
|
|
103
|
+
const UNSAFE_LAUNCHER_PATTERN = /[;&|<>$`\\'"*?()[\]{}=~\n\r\t\0]/;
|
|
104
|
+
/** Upper bound on a launcher override, so a pasted blob is never a rule. */
|
|
105
|
+
const MAX_LAUNCHER_CHARS = 256;
|
|
106
|
+
/** Tokens that suggest a credential was pasted into the launcher value. */
|
|
107
|
+
const SECRET_LIKE_PATTERN = /(api[_-]?key|token|secret|passw(or)?d|bearer|bapi_[a-z0-9]{8,})/i;
|
|
108
|
+
/**
|
|
109
|
+
* Resolve the launcher prefix exactly as the posture resolves it: unset, empty,
|
|
110
|
+
* or whitespace-only means the default packaged launcher; anything else is used
|
|
111
|
+
* verbatim. Internal runs of whitespace collapse to one space, because that is
|
|
112
|
+
* how the posture writes the launcher's words. An override that cannot be an
|
|
113
|
+
* exact, secret-free prefix is refused — never approximated with a wildcard.
|
|
114
|
+
* The refusal reason never echoes the value.
|
|
115
|
+
*/
|
|
116
|
+
export function resolvePermissionLauncher(env) {
|
|
117
|
+
const raw = env[BRIDGE_LAUNCHER_ENV];
|
|
118
|
+
if (typeof raw !== "string" || raw.trim().length === 0) {
|
|
119
|
+
return { ok: true, prefix: BRIDGE_DEFAULT_LAUNCHER, source: "default" };
|
|
120
|
+
}
|
|
121
|
+
const trimmed = raw.trim();
|
|
122
|
+
if (trimmed.length > MAX_LAUNCHER_CHARS) {
|
|
123
|
+
return { ok: false, reason: `${BRIDGE_LAUNCHER_ENV} is longer than ${MAX_LAUNCHER_CHARS} characters` };
|
|
124
|
+
}
|
|
125
|
+
if (UNSAFE_LAUNCHER_PATTERN.test(trimmed)) {
|
|
126
|
+
return {
|
|
127
|
+
ok: false,
|
|
128
|
+
reason: `${BRIDGE_LAUNCHER_ENV} contains a quote, shell operator, wildcard, or assignment, ` +
|
|
129
|
+
"so it cannot be matched by an exact permission rule",
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
if (SECRET_LIKE_PATTERN.test(trimmed)) {
|
|
133
|
+
return {
|
|
134
|
+
ok: false,
|
|
135
|
+
reason: `${BRIDGE_LAUNCHER_ENV} looks like it carries a credential, which must never be part of a rule`,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
return { ok: true, prefix: trimmed.split(/\s+/).join(" "), source: "override" };
|
|
139
|
+
}
|
|
140
|
+
// ---------------------------------------------------------------------------
|
|
141
|
+
// Rule catalogs
|
|
142
|
+
// ---------------------------------------------------------------------------
|
|
143
|
+
/**
|
|
144
|
+
* The project `permissions.allow` catalog for one launcher prefix. Every entry is
|
|
145
|
+
* an exact command or an exact prefix followed by its arguments — never a broad
|
|
146
|
+
* rule. `serverNames` are the MCP registration keys the tool rules are written
|
|
147
|
+
* under (see {@link resolveSupervisorServerNames}). Order is stable so output and
|
|
148
|
+
* the manifest are deterministic.
|
|
149
|
+
*/
|
|
150
|
+
export function buildProjectPermissionRules(launcherPrefix, serverNames = [MCP_SERVER_NAME]) {
|
|
151
|
+
const rules = [];
|
|
152
|
+
// The sanctioned verbs, in the launcher's exact form.
|
|
153
|
+
for (const verb of BRIDGE_SANCTIONED_VERB_SUBCOMMANDS) {
|
|
154
|
+
rules.push(`Bash(${launcherPrefix} epic-implementer ${verb} *)`);
|
|
155
|
+
}
|
|
156
|
+
// drive-epic preview and attach, and the readiness reads.
|
|
157
|
+
rules.push(`Bash(${launcherPrefix} drive-epic *)`);
|
|
158
|
+
rules.push(`Bash(${launcherPrefix} epic-implementer readiness *)`);
|
|
159
|
+
// Read-only supporting commands.
|
|
160
|
+
rules.push(`Bash(${launcherPrefix} epic-implementer continuation-receipt *)`);
|
|
161
|
+
rules.push(`Bash(${launcherPrefix} epic-implementer doctor)`);
|
|
162
|
+
rules.push(`Bash(${launcherPrefix} plane status)`);
|
|
163
|
+
rules.push("Bash(pmset -g log)");
|
|
164
|
+
rules.push("Bash(git ls-tree *)");
|
|
165
|
+
rules.push("Bash(gh auth status)");
|
|
166
|
+
// Stage 0's local checks, exactly as the posture spells them.
|
|
167
|
+
rules.push("Bash(source .venv/bin/activate)");
|
|
168
|
+
rules.push("Bash(ENVIRONMENT=TEST python scripts/conductor_health_check.py --json)");
|
|
169
|
+
rules.push("Bash(alembic -c alembic.ini heads)");
|
|
170
|
+
rules.push("Bash(alembic -c alembic.ini current)");
|
|
171
|
+
// The private tally directory: create, restrict, read, and append.
|
|
172
|
+
rules.push(`Bash(mkdir -p ${BRIDGE_RUN_STATE_DIR}/*)`);
|
|
173
|
+
rules.push(`Bash(chmod 700 ${BRIDGE_RUN_STATE_DIR}/*)`);
|
|
174
|
+
rules.push(`Bash(chmod 600 ${BRIDGE_RUN_STATE_DIR}/*)`);
|
|
175
|
+
rules.push(`Read(${BRIDGE_RUN_STATE_DIR}/**)`);
|
|
176
|
+
rules.push(`Edit(${BRIDGE_RUN_STATE_DIR}/**)`);
|
|
177
|
+
rules.push(`Write(${BRIDGE_RUN_STATE_DIR}/**)`);
|
|
178
|
+
// BAPI-1399: the value-free prerequisite presence check, exactly as prescribed.
|
|
179
|
+
// Only a validated variable name fills the `*`; the form prints present or missing.
|
|
180
|
+
rules.push('Bash([ -n "${*:+x}" ] && echo present || echo missing)');
|
|
181
|
+
// Claude Code checks each `&&` / `||` segment of a compound command on its
|
|
182
|
+
// own, so each segment of the prescribed form has its own exact rule. A
|
|
183
|
+
// segment outside these three (for example one that prints a value) matches
|
|
184
|
+
// no rule.
|
|
185
|
+
rules.push('Bash([ -n "${*:+x}" ])');
|
|
186
|
+
rules.push("Bash(echo present)");
|
|
187
|
+
rules.push("Bash(echo missing)");
|
|
188
|
+
// BAPI-1399: reading `.bridge/epic-workarounds/` at the fetched epic head, and
|
|
189
|
+
// the marker-checked hand-off comment on the run's own integration PR. The
|
|
190
|
+
// `workarounds.md` write is covered by the run-state Write rule above.
|
|
191
|
+
rules.push("Bash(git fetch origin 'epic/*')");
|
|
192
|
+
rules.push("Bash(git rev-parse 'origin/epic/*')");
|
|
193
|
+
rules.push("Bash(git show '*:.bridge/epic-workarounds/*.md')");
|
|
194
|
+
rules.push("Bash(gh pr view 'epic/*' --json number,isDraft,state,headRefOid,baseRefName,comments)");
|
|
195
|
+
rules.push(`Bash(gh pr comment * --body-file ${BRIDGE_RUN_STATE_DIR}/*/workarounds.md)`);
|
|
196
|
+
// The notes-log commit on docs/<KEY>-conductor-run-notes.
|
|
197
|
+
rules.push("Bash(git switch -c 'docs/*-conductor-run-notes')");
|
|
198
|
+
rules.push("Bash(git add docs/epic-implementer/run-notes/)");
|
|
199
|
+
rules.push("Bash(git commit -m * -- docs/epic-implementer/run-notes/)");
|
|
200
|
+
// The Bridge MCP tools the supervisor uses, under each installed server key.
|
|
201
|
+
for (const serverName of serverNames) {
|
|
202
|
+
for (const tool of BRIDGE_SUPERVISOR_MCP_TOOLS) {
|
|
203
|
+
rules.push(`mcp__${serverName}__${tool}`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return rules;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* The user-settings `autoMode` prose. It describes the run sandbox — the run's
|
|
210
|
+
* branches and worktrees, the Bridge server, and third-party sandbox accounts the
|
|
211
|
+
* project explicitly names — so the classifier treats the supervisor's in-scope
|
|
212
|
+
* work as routine. `sandboxAccounts` adds named accounts when a caller knows them;
|
|
213
|
+
* none is ever inferred.
|
|
214
|
+
*/
|
|
215
|
+
export function buildUserAutoModeEntries(sandboxAccounts = []) {
|
|
216
|
+
const named = sandboxAccounts.map((a) => a.trim()).filter((a) => a.length > 0);
|
|
217
|
+
const accounts = named.length > 0
|
|
218
|
+
? `the third-party sandbox accounts the project names (${named.join(", ")})`
|
|
219
|
+
: "any third-party sandbox account the project explicitly names as a sandbox in its configuration or documentation";
|
|
220
|
+
return {
|
|
221
|
+
environment: [
|
|
222
|
+
"Bridge Epic Implementer runs (/drive-epic-run): a run's sandbox is its own branches " +
|
|
223
|
+
"(epic/<KEY> and each child ticket's branch_name) and the worktrees made for them, " +
|
|
224
|
+
`the Bridge server, and ${accounts}.`,
|
|
225
|
+
],
|
|
226
|
+
allow: [
|
|
227
|
+
"Bridge /drive-epic-run supervisor: the packaged Bridge CLI's sanctioned " +
|
|
228
|
+
"epic-implementer recovery verbs, drive-epic preview and attach, and readiness, applied " +
|
|
229
|
+
"to the run's own sandbox; reads, waits, the private tally under " +
|
|
230
|
+
`${BRIDGE_RUN_STATE_DIR}/, and Bridge bug reports.`,
|
|
231
|
+
],
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
/** Absolute path of the project's local (gitignored) Claude settings file. */
|
|
235
|
+
export function projectSettingsPath(cwd) {
|
|
236
|
+
return path.join(cwd, ...BRIDGE_PROJECT_SETTINGS_RELPATH.split("/"));
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* The MCP registration keys the tool rules must be written under, read from the
|
|
240
|
+
* project's `.mcp.json`. A canonical or legacy registration yields its own key;
|
|
241
|
+
* a conflict (both keys present) yields both, so whichever one Claude Code loads
|
|
242
|
+
* is covered. A missing, malformed, or Bridge-less file yields the canonical key,
|
|
243
|
+
* because that is the key a fresh install writes.
|
|
244
|
+
*/
|
|
245
|
+
export async function resolveSupervisorServerNames(cwd, deps) {
|
|
246
|
+
let servers;
|
|
247
|
+
try {
|
|
248
|
+
const parsed = JSON.parse(await deps.readFile(path.join(cwd, ".mcp.json")));
|
|
249
|
+
servers =
|
|
250
|
+
parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)
|
|
251
|
+
? parsed.mcpServers
|
|
252
|
+
: undefined;
|
|
253
|
+
}
|
|
254
|
+
catch {
|
|
255
|
+
servers = undefined;
|
|
256
|
+
}
|
|
257
|
+
const resolution = resolveRegistrationKey(servers);
|
|
258
|
+
if (resolution.state === "canonical" || resolution.state === "legacy")
|
|
259
|
+
return [resolution.key];
|
|
260
|
+
if (resolution.state === "conflict")
|
|
261
|
+
return [...resolution.keys];
|
|
262
|
+
return [MCP_SERVER_NAME];
|
|
263
|
+
}
|
|
264
|
+
/** Absolute path of the user's Claude settings file. */
|
|
265
|
+
export function userSettingsPath(homeDir) {
|
|
266
|
+
return path.join(homeDir, ".claude", "settings.json");
|
|
267
|
+
}
|
|
268
|
+
/** Absolute path of Bridge's permission-ownership manifest. */
|
|
269
|
+
export function bridgePermissionsManifestPath(cwd) {
|
|
270
|
+
return path.join(cwd, ...BRIDGE_PERMISSIONS_MANIFEST_RELPATH.split("/"));
|
|
271
|
+
}
|
|
272
|
+
/** Read a settings file: missing is `{}`-equivalent, malformed is never used. */
|
|
273
|
+
export async function readSettingsFile(filePath, deps) {
|
|
274
|
+
let raw;
|
|
275
|
+
try {
|
|
276
|
+
raw = await deps.readFile(filePath);
|
|
277
|
+
}
|
|
278
|
+
catch {
|
|
279
|
+
return { status: "missing" };
|
|
280
|
+
}
|
|
281
|
+
try {
|
|
282
|
+
const parsed = JSON.parse(raw);
|
|
283
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
284
|
+
return { status: "malformed" };
|
|
285
|
+
}
|
|
286
|
+
return { status: "valid", settings: parsed, raw };
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
return { status: "malformed" };
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
function serializeSettings(settings) {
|
|
293
|
+
return `${JSON.stringify(settings, null, 2)}\n`;
|
|
294
|
+
}
|
|
295
|
+
function emptyManifest() {
|
|
296
|
+
return {
|
|
297
|
+
version: BRIDGE_PERMISSIONS_MANIFEST_VERSION,
|
|
298
|
+
launcher: null,
|
|
299
|
+
project: [],
|
|
300
|
+
userAllow: [],
|
|
301
|
+
userEnvironment: [],
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
function stringList(value) {
|
|
305
|
+
if (!Array.isArray(value))
|
|
306
|
+
return null;
|
|
307
|
+
return value.every((v) => typeof v === "string") ? value : null;
|
|
308
|
+
}
|
|
309
|
+
/** Read and validate the manifest. An invalid manifest owns nothing. */
|
|
310
|
+
export async function readBridgePermissionsManifest(cwd, deps) {
|
|
311
|
+
let raw;
|
|
312
|
+
try {
|
|
313
|
+
raw = await deps.readFile(bridgePermissionsManifestPath(cwd));
|
|
314
|
+
}
|
|
315
|
+
catch {
|
|
316
|
+
return { status: "missing" };
|
|
317
|
+
}
|
|
318
|
+
try {
|
|
319
|
+
const doc = JSON.parse(raw);
|
|
320
|
+
if (doc === null || typeof doc !== "object" || Array.isArray(doc))
|
|
321
|
+
return { status: "invalid" };
|
|
322
|
+
if (doc.version !== BRIDGE_PERMISSIONS_MANIFEST_VERSION)
|
|
323
|
+
return { status: "invalid" };
|
|
324
|
+
const project = stringList(doc.project);
|
|
325
|
+
const userAllow = stringList(doc.userAllow);
|
|
326
|
+
const userEnvironment = stringList(doc.userEnvironment);
|
|
327
|
+
const launcher = doc.launcher;
|
|
328
|
+
if (!project || !userAllow || !userEnvironment)
|
|
329
|
+
return { status: "invalid" };
|
|
330
|
+
if (launcher !== null && typeof launcher !== "string")
|
|
331
|
+
return { status: "invalid" };
|
|
332
|
+
return {
|
|
333
|
+
status: "valid",
|
|
334
|
+
manifest: { version: BRIDGE_PERMISSIONS_MANIFEST_VERSION, launcher, project, userAllow, userEnvironment },
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
catch {
|
|
338
|
+
return { status: "invalid" };
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
/** Serialize the manifest with a stable key order. */
|
|
342
|
+
export function serializeBridgePermissionsManifest(manifest) {
|
|
343
|
+
return `${JSON.stringify({
|
|
344
|
+
version: manifest.version,
|
|
345
|
+
launcher: manifest.launcher,
|
|
346
|
+
project: manifest.project,
|
|
347
|
+
userAllow: manifest.userAllow,
|
|
348
|
+
userEnvironment: manifest.userEnvironment,
|
|
349
|
+
}, null, 2)}\n`;
|
|
350
|
+
}
|
|
351
|
+
async function writeManifestIfChanged(cwd, next, deps) {
|
|
352
|
+
const target = bridgePermissionsManifestPath(cwd);
|
|
353
|
+
const text = serializeBridgePermissionsManifest(next);
|
|
354
|
+
let current = null;
|
|
355
|
+
try {
|
|
356
|
+
current = await deps.readFile(target);
|
|
357
|
+
}
|
|
358
|
+
catch {
|
|
359
|
+
current = null;
|
|
360
|
+
}
|
|
361
|
+
if (current === text)
|
|
362
|
+
return true;
|
|
363
|
+
try {
|
|
364
|
+
await deps.mkdir(path.dirname(target), { recursive: true });
|
|
365
|
+
await deps.writeFile(target, text);
|
|
366
|
+
return true;
|
|
367
|
+
}
|
|
368
|
+
catch {
|
|
369
|
+
return false;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
async function writeSettings(filePath, settings, deps) {
|
|
373
|
+
try {
|
|
374
|
+
await deps.mkdir(path.dirname(filePath), { recursive: true });
|
|
375
|
+
await deps.writeFile(filePath, serializeSettings(settings));
|
|
376
|
+
return true;
|
|
377
|
+
}
|
|
378
|
+
catch {
|
|
379
|
+
return false;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
// ---------------------------------------------------------------------------
|
|
383
|
+
// List helpers
|
|
384
|
+
// ---------------------------------------------------------------------------
|
|
385
|
+
/** The `permissions.allow` list, `[]` when absent, or null when not a string list. */
|
|
386
|
+
function projectAllowList(settings) {
|
|
387
|
+
const permissions = settings.permissions;
|
|
388
|
+
if (permissions === undefined)
|
|
389
|
+
return [];
|
|
390
|
+
if (permissions === null || typeof permissions !== "object" || Array.isArray(permissions))
|
|
391
|
+
return null;
|
|
392
|
+
const allow = permissions.allow;
|
|
393
|
+
if (allow === undefined)
|
|
394
|
+
return [];
|
|
395
|
+
return stringList(allow);
|
|
396
|
+
}
|
|
397
|
+
function withProjectAllowList(settings, allow) {
|
|
398
|
+
const permissions = settings.permissions && typeof settings.permissions === "object"
|
|
399
|
+
? settings.permissions
|
|
400
|
+
: {};
|
|
401
|
+
return { ...settings, permissions: { ...permissions, allow } };
|
|
402
|
+
}
|
|
403
|
+
/** An `autoMode` list, `undefined` when absent, or null when not a string list. */
|
|
404
|
+
function autoModeList(settings, key) {
|
|
405
|
+
const autoMode = settings.autoMode;
|
|
406
|
+
if (autoMode === undefined)
|
|
407
|
+
return undefined;
|
|
408
|
+
if (autoMode === null || typeof autoMode !== "object" || Array.isArray(autoMode))
|
|
409
|
+
return null;
|
|
410
|
+
const list = autoMode[key];
|
|
411
|
+
if (list === undefined)
|
|
412
|
+
return undefined;
|
|
413
|
+
return stringList(list);
|
|
414
|
+
}
|
|
415
|
+
function withAutoModeLists(settings, lists) {
|
|
416
|
+
const autoMode = settings.autoMode && typeof settings.autoMode === "object"
|
|
417
|
+
? settings.autoMode
|
|
418
|
+
: {};
|
|
419
|
+
return { ...settings, autoMode: { ...autoMode, ...lists } };
|
|
420
|
+
}
|
|
421
|
+
function unique(values) {
|
|
422
|
+
return [...new Set(values)];
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Write the project `permissions.allow` catalog for the resolved launcher.
|
|
426
|
+
*
|
|
427
|
+
* Only missing strings are appended; every other entry and key is kept. Strings
|
|
428
|
+
* Bridge recorded earlier that are no longer desired (for example after the
|
|
429
|
+
* launcher changed) are removed. The manifest gains exactly the strings this run
|
|
430
|
+
* inserted. Nothing is written when nothing changes.
|
|
431
|
+
*/
|
|
432
|
+
export async function applyProjectPermissions(params, deps) {
|
|
433
|
+
const launcher = resolvePermissionLauncher(params.env);
|
|
434
|
+
if (!launcher.ok) {
|
|
435
|
+
return { launcher, project: { status: "launcher-refused", skipped: [] }, manifestRecorded: false };
|
|
436
|
+
}
|
|
437
|
+
const desired = buildProjectPermissionRules(launcher.prefix, await resolveSupervisorServerNames(params.cwd, deps));
|
|
438
|
+
const settingsPath = projectSettingsPath(params.cwd);
|
|
439
|
+
const manifestRead = await readBridgePermissionsManifest(params.cwd, deps);
|
|
440
|
+
const manifest = manifestRead.status === "valid" ? manifestRead.manifest : emptyManifest();
|
|
441
|
+
const read = await readSettingsFile(settingsPath, deps);
|
|
442
|
+
const current = read.status === "valid" ? read.settings : {};
|
|
443
|
+
const allow = read.status === "malformed" ? null : projectAllowList(current);
|
|
444
|
+
if (allow === null) {
|
|
445
|
+
return {
|
|
446
|
+
launcher,
|
|
447
|
+
project: { status: "malformed", path: settingsPath, skipped: desired },
|
|
448
|
+
manifestRecorded: false,
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
const desiredSet = new Set(desired);
|
|
452
|
+
const stale = manifest.project.filter((rule) => !desiredSet.has(rule));
|
|
453
|
+
const staleSet = new Set(stale);
|
|
454
|
+
const removedStale = allow.filter((rule) => staleSet.has(rule));
|
|
455
|
+
const kept = allow.filter((rule) => !staleSet.has(rule));
|
|
456
|
+
const keptSet = new Set(kept);
|
|
457
|
+
const inserted = desired.filter((rule) => !keptSet.has(rule));
|
|
458
|
+
const alreadyPresent = desired.filter((rule) => keptSet.has(rule));
|
|
459
|
+
const nextAllow = [...kept, ...inserted];
|
|
460
|
+
let status = "unchanged";
|
|
461
|
+
if (inserted.length > 0 || removedStale.length > 0) {
|
|
462
|
+
const ok = await writeSettings(settingsPath, withProjectAllowList(current, nextAllow), deps);
|
|
463
|
+
if (!ok) {
|
|
464
|
+
return { launcher, project: { status: "io-error", path: settingsPath, skipped: inserted }, manifestRecorded: false };
|
|
465
|
+
}
|
|
466
|
+
status = "written";
|
|
467
|
+
}
|
|
468
|
+
const ownedStill = manifest.project.filter((rule) => desiredSet.has(rule));
|
|
469
|
+
const manifestRecorded = await writeManifestIfChanged(params.cwd, { ...manifest, launcher: launcher.prefix, project: unique([...ownedStill, ...inserted]) }, deps);
|
|
470
|
+
return {
|
|
471
|
+
launcher,
|
|
472
|
+
project: { status, path: settingsPath, inserted, alreadyPresent, removedStale },
|
|
473
|
+
manifestRecorded,
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Write Bridge's `autoMode.allow` and `autoMode.environment` prose into the
|
|
478
|
+
* user's settings. Call ONLY after the user consented. A list Bridge creates
|
|
479
|
+
* starts with `"$defaults"` so Claude Code's built-in entries are kept; an
|
|
480
|
+
* existing list keeps every entry it had, `"$defaults"` included, except an
|
|
481
|
+
* entry this project's manifest recorded as Bridge's own that Bridge no longer
|
|
482
|
+
* wants (its wording changed): that one is replaced, so an upgrade never leaves
|
|
483
|
+
* stale Bridge prose beside the new text. Entries Bridge did not record are
|
|
484
|
+
* never touched.
|
|
485
|
+
*/
|
|
486
|
+
export async function applyUserAutoModeEntries(params, deps) {
|
|
487
|
+
const entries = buildUserAutoModeEntries(params.sandboxAccounts);
|
|
488
|
+
const settingsPath = userSettingsPath(params.homeDir);
|
|
489
|
+
const proposed = [...entries.allow, ...entries.environment];
|
|
490
|
+
const read = await readSettingsFile(settingsPath, deps);
|
|
491
|
+
if (read.status === "malformed") {
|
|
492
|
+
return { user: { status: "malformed", path: settingsPath, skipped: proposed }, manifestRecorded: false };
|
|
493
|
+
}
|
|
494
|
+
const current = read.status === "valid" ? read.settings : {};
|
|
495
|
+
const allowList = autoModeList(current, "allow");
|
|
496
|
+
const environmentList = autoModeList(current, "environment");
|
|
497
|
+
if (allowList === null || environmentList === null) {
|
|
498
|
+
return { user: { status: "malformed", path: settingsPath, skipped: proposed }, manifestRecorded: false };
|
|
499
|
+
}
|
|
500
|
+
const manifestRead = await readBridgePermissionsManifest(params.cwd, deps);
|
|
501
|
+
const manifest = manifestRead.status === "valid" ? manifestRead.manifest : emptyManifest();
|
|
502
|
+
const merge = (existing, wanted, recorded) => {
|
|
503
|
+
const wantedSet = new Set(wanted);
|
|
504
|
+
const staleSet = new Set(recorded.filter((entry) => !wantedSet.has(entry)));
|
|
505
|
+
const prior = existing ?? [AUTO_MODE_DEFAULTS_SENTINEL];
|
|
506
|
+
const removed = prior.filter((entry) => staleSet.has(entry));
|
|
507
|
+
const base = prior.filter((entry) => !staleSet.has(entry));
|
|
508
|
+
const have = new Set(base);
|
|
509
|
+
const added = wanted.filter((entry) => !have.has(entry));
|
|
510
|
+
const present = wanted.filter((entry) => have.has(entry));
|
|
511
|
+
const owned = recorded.filter((entry) => wantedSet.has(entry));
|
|
512
|
+
return { next: [...base, ...added], added, present, removed, owned, created: existing === undefined };
|
|
513
|
+
};
|
|
514
|
+
const allowMerge = merge(allowList, entries.allow, manifest.userAllow);
|
|
515
|
+
const environmentMerge = merge(environmentList, entries.environment, manifest.userEnvironment);
|
|
516
|
+
const inserted = [...allowMerge.added, ...environmentMerge.added];
|
|
517
|
+
const alreadyPresent = [...allowMerge.present, ...environmentMerge.present];
|
|
518
|
+
const removedStale = [...allowMerge.removed, ...environmentMerge.removed];
|
|
519
|
+
let status = "unchanged";
|
|
520
|
+
if (inserted.length > 0 || removedStale.length > 0) {
|
|
521
|
+
const next = withAutoModeLists(current, { allow: allowMerge.next, environment: environmentMerge.next });
|
|
522
|
+
const ok = await writeSettings(settingsPath, next, deps);
|
|
523
|
+
if (!ok) {
|
|
524
|
+
return { user: { status: "io-error", path: settingsPath, skipped: inserted }, manifestRecorded: false };
|
|
525
|
+
}
|
|
526
|
+
status = "written";
|
|
527
|
+
}
|
|
528
|
+
const manifestRecorded = await writeManifestIfChanged(params.cwd, {
|
|
529
|
+
...manifest,
|
|
530
|
+
userAllow: unique([...allowMerge.owned, ...allowMerge.added]),
|
|
531
|
+
userEnvironment: unique([...environmentMerge.owned, ...environmentMerge.added]),
|
|
532
|
+
}, deps);
|
|
533
|
+
return {
|
|
534
|
+
user: { status, path: settingsPath, inserted, alreadyPresent, removedStale },
|
|
535
|
+
manifestRecorded,
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* Inspect both scopes without writing anything. Project rules are `present`
|
|
540
|
+
* when every desired rule is in the allow list, `missing` when any is absent,
|
|
541
|
+
* and `drifted` when the manifest's launcher differs from the one resolved now
|
|
542
|
+
* or a Bridge-owned rule is no longer desired. User entries are `present` when
|
|
543
|
+
* every proposed entry is in its list and `missing` otherwise.
|
|
544
|
+
*/
|
|
545
|
+
export async function inspectBridgePermissions(params, deps) {
|
|
546
|
+
const launcher = resolvePermissionLauncher(params.env);
|
|
547
|
+
const manifestRead = await readBridgePermissionsManifest(params.cwd, deps);
|
|
548
|
+
const manifest = manifestRead.status === "valid" ? manifestRead.manifest : emptyManifest();
|
|
549
|
+
let project;
|
|
550
|
+
const projectPath = projectSettingsPath(params.cwd);
|
|
551
|
+
if (!launcher.ok) {
|
|
552
|
+
project = { state: "launcher-refused" };
|
|
553
|
+
}
|
|
554
|
+
else {
|
|
555
|
+
const read = await readSettingsFile(projectPath, deps);
|
|
556
|
+
const allow = read.status === "malformed" ? null : projectAllowList(read.status === "valid" ? read.settings : {});
|
|
557
|
+
if (allow === null) {
|
|
558
|
+
project = { state: "malformed", path: projectPath };
|
|
559
|
+
}
|
|
560
|
+
else {
|
|
561
|
+
const desired = buildProjectPermissionRules(launcher.prefix, await resolveSupervisorServerNames(params.cwd, deps));
|
|
562
|
+
const have = new Set(allow);
|
|
563
|
+
const missing = desired.filter((rule) => !have.has(rule));
|
|
564
|
+
const desiredSet = new Set(desired);
|
|
565
|
+
const stale = manifest.project.filter((rule) => !desiredSet.has(rule) && have.has(rule));
|
|
566
|
+
const launcherChanged = manifest.launcher !== null && manifest.launcher !== launcher.prefix;
|
|
567
|
+
if (launcherChanged || stale.length > 0) {
|
|
568
|
+
project = {
|
|
569
|
+
state: "drifted",
|
|
570
|
+
path: projectPath,
|
|
571
|
+
missing,
|
|
572
|
+
stale,
|
|
573
|
+
reason: launcherChanged
|
|
574
|
+
? "the launcher prefix changed since the rules were written"
|
|
575
|
+
: "Bridge-written rules no longer match the current catalog or MCP server key",
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
else if (missing.length > 0) {
|
|
579
|
+
project = { state: "missing", path: projectPath, missing };
|
|
580
|
+
}
|
|
581
|
+
else {
|
|
582
|
+
project = { state: "present", path: projectPath, count: desired.length };
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
const userPath = userSettingsPath(params.homeDir);
|
|
587
|
+
const userRead = await readSettingsFile(userPath, deps);
|
|
588
|
+
let user;
|
|
589
|
+
if (userRead.status === "malformed") {
|
|
590
|
+
user = { state: "malformed", path: userPath };
|
|
591
|
+
}
|
|
592
|
+
else {
|
|
593
|
+
const settings = userRead.status === "valid" ? userRead.settings : {};
|
|
594
|
+
const allowList = autoModeList(settings, "allow");
|
|
595
|
+
const environmentList = autoModeList(settings, "environment");
|
|
596
|
+
if (allowList === null || environmentList === null) {
|
|
597
|
+
user = { state: "malformed", path: userPath };
|
|
598
|
+
}
|
|
599
|
+
else {
|
|
600
|
+
const entries = buildUserAutoModeEntries();
|
|
601
|
+
const haveAllow = new Set(allowList ?? []);
|
|
602
|
+
const haveEnvironment = new Set(environmentList ?? []);
|
|
603
|
+
const missing = [
|
|
604
|
+
...entries.allow.filter((e) => !haveAllow.has(e)),
|
|
605
|
+
...entries.environment.filter((e) => !haveEnvironment.has(e)),
|
|
606
|
+
];
|
|
607
|
+
user =
|
|
608
|
+
missing.length === 0
|
|
609
|
+
? { state: "present", path: userPath, count: entries.allow.length + entries.environment.length }
|
|
610
|
+
: { state: "missing", path: userPath, missing };
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
return { launcher, recordedLauncher: manifest.launcher, project, user };
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* Remove ONLY the strings Bridge recorded in its manifest, from both settings
|
|
617
|
+
* files. Every other entry and key stays, including a user's own identical
|
|
618
|
+
* string that predates Bridge (it was never recorded). A malformed file is never
|
|
619
|
+
* written; its recorded strings stay recorded and are reported as unremovable.
|
|
620
|
+
*/
|
|
621
|
+
export async function removeBridgePermissions(params, deps) {
|
|
622
|
+
const manifestRead = await readBridgePermissionsManifest(params.cwd, deps);
|
|
623
|
+
const manifest = manifestRead.status === "valid" ? manifestRead.manifest : emptyManifest();
|
|
624
|
+
const next = { ...manifest };
|
|
625
|
+
// Project scope.
|
|
626
|
+
const projectPath = projectSettingsPath(params.cwd);
|
|
627
|
+
let project;
|
|
628
|
+
if (manifest.project.length === 0) {
|
|
629
|
+
project = { status: "nothing-recorded", path: projectPath, removed: [] };
|
|
630
|
+
}
|
|
631
|
+
else {
|
|
632
|
+
const read = await readSettingsFile(projectPath, deps);
|
|
633
|
+
const allow = read.status === "malformed" ? null : projectAllowList(read.status === "valid" ? read.settings : {});
|
|
634
|
+
if (allow === null) {
|
|
635
|
+
project = { status: "malformed", path: projectPath, unremovable: manifest.project };
|
|
636
|
+
}
|
|
637
|
+
else {
|
|
638
|
+
const owned = new Set(manifest.project);
|
|
639
|
+
const removed = allow.filter((rule) => owned.has(rule));
|
|
640
|
+
let ok = true;
|
|
641
|
+
if (removed.length > 0 && read.status === "valid") {
|
|
642
|
+
ok = await writeSettings(projectPath, withProjectAllowList(read.settings, allow.filter((rule) => !owned.has(rule))), deps);
|
|
643
|
+
}
|
|
644
|
+
if (ok) {
|
|
645
|
+
project = { status: "removed", path: projectPath, removed };
|
|
646
|
+
next.project = [];
|
|
647
|
+
next.launcher = null;
|
|
648
|
+
}
|
|
649
|
+
else {
|
|
650
|
+
project = { status: "io-error", path: projectPath, unremovable: removed };
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
// User scope.
|
|
655
|
+
const userPath = userSettingsPath(params.homeDir);
|
|
656
|
+
let user;
|
|
657
|
+
const ownedUser = [...manifest.userAllow, ...manifest.userEnvironment];
|
|
658
|
+
if (ownedUser.length === 0) {
|
|
659
|
+
user = { status: "nothing-recorded", path: userPath, removed: [] };
|
|
660
|
+
}
|
|
661
|
+
else {
|
|
662
|
+
const read = await readSettingsFile(userPath, deps);
|
|
663
|
+
const settings = read.status === "valid" ? read.settings : {};
|
|
664
|
+
const allowList = read.status === "malformed" ? null : autoModeList(settings, "allow");
|
|
665
|
+
const environmentList = read.status === "malformed" ? null : autoModeList(settings, "environment");
|
|
666
|
+
if (allowList === null || environmentList === null) {
|
|
667
|
+
user = { status: "malformed", path: userPath, unremovable: ownedUser };
|
|
668
|
+
}
|
|
669
|
+
else {
|
|
670
|
+
const ownedAllow = new Set(manifest.userAllow);
|
|
671
|
+
const ownedEnvironment = new Set(manifest.userEnvironment);
|
|
672
|
+
const removedAllow = (allowList ?? []).filter((e) => ownedAllow.has(e));
|
|
673
|
+
const removedEnvironment = (environmentList ?? []).filter((e) => ownedEnvironment.has(e));
|
|
674
|
+
const removed = [...removedAllow, ...removedEnvironment];
|
|
675
|
+
let ok = true;
|
|
676
|
+
if (removed.length > 0 && read.status === "valid") {
|
|
677
|
+
const lists = {};
|
|
678
|
+
if (allowList !== undefined)
|
|
679
|
+
lists.allow = allowList.filter((e) => !ownedAllow.has(e));
|
|
680
|
+
if (environmentList !== undefined)
|
|
681
|
+
lists.environment = environmentList.filter((e) => !ownedEnvironment.has(e));
|
|
682
|
+
ok = await writeSettings(userPath, withAutoModeLists(read.settings, lists), deps);
|
|
683
|
+
}
|
|
684
|
+
if (ok) {
|
|
685
|
+
user = { status: "removed", path: userPath, removed };
|
|
686
|
+
next.userAllow = [];
|
|
687
|
+
next.userEnvironment = [];
|
|
688
|
+
}
|
|
689
|
+
else {
|
|
690
|
+
user = { status: "io-error", path: userPath, unremovable: removed };
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
const changed = next.project.length !== manifest.project.length ||
|
|
695
|
+
next.userAllow.length !== manifest.userAllow.length ||
|
|
696
|
+
next.userEnvironment.length !== manifest.userEnvironment.length;
|
|
697
|
+
const manifestUpdated = manifestRead.status === "valid" && changed ? await writeManifestIfChanged(params.cwd, next, deps) : true;
|
|
698
|
+
return { project, user, manifestUpdated };
|
|
699
|
+
}
|
|
700
|
+
/** The one consent question for the user-settings write. */
|
|
701
|
+
export const USER_AUTO_MODE_CONSENT_PROMPT = "Add Bridge's auto-mode sandbox description to ~/.claude/settings.json, so /drive-epic-run's " +
|
|
702
|
+
"in-scope work is not stopped by the auto-mode classifier? (y/N) ";
|
|
703
|
+
function logList(log, items) {
|
|
704
|
+
for (const item of items)
|
|
705
|
+
log(` ${item}`);
|
|
706
|
+
}
|
|
707
|
+
/**
|
|
708
|
+
* The installer's permission step. Writes the project rules, then asks ONCE for
|
|
709
|
+
* consent before touching the user's settings. A declined or non-interactive
|
|
710
|
+
* install leaves the user file untouched and prints what it would have added.
|
|
711
|
+
* Every rule written is listed, with the launcher prefix it covers. A malformed
|
|
712
|
+
* file is reported on stderr and skipped; the rest of the install continues.
|
|
713
|
+
*/
|
|
714
|
+
export async function runBridgePermissionsInstallStep(deps) {
|
|
715
|
+
const { log, errorLog } = deps;
|
|
716
|
+
log("");
|
|
717
|
+
log("Bridge permission rules for /drive-epic-run (auto mode):");
|
|
718
|
+
if (deps.ensureGitignored) {
|
|
719
|
+
for (const relPath of [BRIDGE_PERMISSIONS_MANIFEST_RELPATH, BRIDGE_PROJECT_SETTINGS_RELPATH]) {
|
|
720
|
+
try {
|
|
721
|
+
await deps.ensureGitignored(relPath);
|
|
722
|
+
}
|
|
723
|
+
catch {
|
|
724
|
+
errorLog(` warning: could not add ${relPath} to .gitignore (non-fatal).`);
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
const project = await applyProjectPermissions({ cwd: deps.cwd, env: deps.env }, deps);
|
|
729
|
+
if (!project.launcher.ok) {
|
|
730
|
+
errorLog(` warning: project permission rules were not written: ${project.launcher.reason}.`);
|
|
731
|
+
errorLog(` Unset ${BRIDGE_LAUNCHER_ENV}, or set it to a plain command prefix, then re-run install.`);
|
|
732
|
+
}
|
|
733
|
+
else {
|
|
734
|
+
log(` Launcher prefix covered: ${project.launcher.prefix}`);
|
|
735
|
+
const scope = project.project;
|
|
736
|
+
if (scope.status === "written" || scope.status === "unchanged") {
|
|
737
|
+
log(` ${scope.path}:`);
|
|
738
|
+
if (scope.inserted.length > 0) {
|
|
739
|
+
log(` Wrote ${scope.inserted.length} permissions.allow rule(s):`);
|
|
740
|
+
logList(log, scope.inserted);
|
|
741
|
+
}
|
|
742
|
+
if (scope.alreadyPresent.length > 0) {
|
|
743
|
+
log(` Already present (${scope.alreadyPresent.length}):`);
|
|
744
|
+
logList(log, scope.alreadyPresent);
|
|
745
|
+
}
|
|
746
|
+
if (scope.removedStale.length > 0) {
|
|
747
|
+
log(` Removed ${scope.removedStale.length} rule(s) Bridge wrote for an earlier launcher:`);
|
|
748
|
+
logList(log, scope.removedStale);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
else if (scope.status === "malformed" || scope.status === "io-error") {
|
|
752
|
+
errorLog(` warning: ${scope.path} ${scope.status === "malformed" ? "is not valid JSON settings and was left unchanged" : "could not be written"}; ` +
|
|
753
|
+
`these ${scope.skipped.length} rule(s) were NOT written:`);
|
|
754
|
+
logList(errorLog, scope.skipped);
|
|
755
|
+
}
|
|
756
|
+
if (!project.manifestRecorded && (scope.status === "written" || scope.status === "unchanged")) {
|
|
757
|
+
errorLog(` warning: could not update ${BRIDGE_PERMISSIONS_MANIFEST_RELPATH}; doctor and removal may not see these rules.`);
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
// User settings: one question, and nothing written without a yes.
|
|
761
|
+
const entries = buildUserAutoModeEntries();
|
|
762
|
+
const proposed = [...entries.allow, ...entries.environment];
|
|
763
|
+
const userPath = userSettingsPath(deps.homeDir);
|
|
764
|
+
if (!deps.promptLine) {
|
|
765
|
+
log(` ${userPath} was not changed (non-interactive install). It would add, to autoMode.allow and autoMode.environment:`);
|
|
766
|
+
logList(log, proposed);
|
|
767
|
+
log(" Re-run install in a terminal to add them.");
|
|
768
|
+
return { project, userConsent: "non-interactive", user: null };
|
|
769
|
+
}
|
|
770
|
+
const answer = (await deps.promptLine(USER_AUTO_MODE_CONSENT_PROMPT)).trim().toLowerCase();
|
|
771
|
+
if (answer !== "y" && answer !== "yes") {
|
|
772
|
+
log(` ${userPath} was not changed. These entries were not added:`);
|
|
773
|
+
logList(log, proposed);
|
|
774
|
+
return { project, userConsent: "declined", user: null };
|
|
775
|
+
}
|
|
776
|
+
const user = await applyUserAutoModeEntries({ cwd: deps.cwd, homeDir: deps.homeDir }, deps);
|
|
777
|
+
const scope = user.user;
|
|
778
|
+
if (scope.status === "written" || scope.status === "unchanged") {
|
|
779
|
+
log(` ${scope.path}:`);
|
|
780
|
+
if (scope.inserted.length > 0) {
|
|
781
|
+
log(` Wrote ${scope.inserted.length} autoMode entr${scope.inserted.length === 1 ? "y" : "ies"} ("$defaults" kept):`);
|
|
782
|
+
logList(log, scope.inserted);
|
|
783
|
+
}
|
|
784
|
+
if (scope.alreadyPresent.length > 0) {
|
|
785
|
+
log(` Already present (${scope.alreadyPresent.length}):`);
|
|
786
|
+
logList(log, scope.alreadyPresent);
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
else if (scope.status === "malformed" || scope.status === "io-error") {
|
|
790
|
+
errorLog(` warning: ${scope.path} ${scope.status === "malformed" ? "is not valid JSON settings and was left unchanged" : "could not be written"}; ` +
|
|
791
|
+
"these autoMode entries were NOT written:");
|
|
792
|
+
logList(errorLog, scope.skipped);
|
|
793
|
+
}
|
|
794
|
+
return { project, userConsent: "granted", user };
|
|
795
|
+
}
|
|
796
|
+
/**
|
|
797
|
+
* The removal flag's step. Removes only Bridge-recorded strings and reports what
|
|
798
|
+
* it removed and what it could not. Returns a process exit code: 0 when every
|
|
799
|
+
* recorded string is gone, 1 when a malformed or unwritable file kept some.
|
|
800
|
+
*/
|
|
801
|
+
export async function runBridgePermissionsRemoval(deps) {
|
|
802
|
+
const { log, errorLog } = deps;
|
|
803
|
+
const result = await removeBridgePermissions({ cwd: deps.cwd, homeDir: deps.homeDir }, deps);
|
|
804
|
+
let failed = false;
|
|
805
|
+
for (const [label, scope] of [
|
|
806
|
+
["project permissions.allow", result.project],
|
|
807
|
+
["user autoMode", result.user],
|
|
808
|
+
]) {
|
|
809
|
+
if (scope.status === "nothing-recorded") {
|
|
810
|
+
log(`${label}: nothing Bridge wrote is recorded (${scope.path}).`);
|
|
811
|
+
}
|
|
812
|
+
else if (scope.status === "removed") {
|
|
813
|
+
log(`${label}: removed ${scope.removed.length} Bridge entr${scope.removed.length === 1 ? "y" : "ies"} from ${scope.path}.`);
|
|
814
|
+
logList(log, scope.removed);
|
|
815
|
+
}
|
|
816
|
+
else if (scope.status === "malformed" || scope.status === "io-error") {
|
|
817
|
+
failed = true;
|
|
818
|
+
errorLog(`${label}: ${scope.path} ${scope.status === "malformed" ? "is not valid JSON settings" : "could not be written"}; ` +
|
|
819
|
+
"these Bridge entries could not be removed:");
|
|
820
|
+
logList(errorLog, scope.unremovable);
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
if (!result.manifestUpdated) {
|
|
824
|
+
failed = true;
|
|
825
|
+
errorLog(`warning: could not update ${BRIDGE_PERMISSIONS_MANIFEST_RELPATH}.`);
|
|
826
|
+
}
|
|
827
|
+
log("Entries you added yourself were left in place.");
|
|
828
|
+
return failed ? 1 : 0;
|
|
829
|
+
}
|