@bridge_gpt/mcp-server 0.2.68 → 0.2.69
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 +99 -41
- package/build/bridge-permissions.js +627 -94
- package/build/check-update-cli.js +74 -0
- package/build/claude-settings.js +84 -2
- package/build/cli-help.js +7 -1
- package/build/codex-home.js +9 -1
- package/build/codex-launcher-pin.js +598 -0
- package/build/commands.generated.js +5 -5
- package/build/connect-github-api.js +33 -14
- package/build/docs.generated.js +2 -2
- package/build/doctor.js +109 -6
- package/build/epic-implementer-bin.js +80 -85
- package/build/executor/completion-contract.js +2 -0
- package/build/executor/http-client.js +43 -4
- package/build/index.js +464 -246
- package/build/install-bridge.js +260 -297
- package/build/launcher-config-inspection.js +184 -19
- package/build/mcp-host-targets.js +4 -0
- package/build/mcp-identity.js +7 -0
- package/build/pipelines.generated.js +4 -4
- package/build/readme.generated.js +1 -1
- package/build/session-upgrade-notice.js +86 -0
- package/build/start-tickets.js +15 -0
- package/build/tool-call-advisory.js +162 -0
- package/build/tool-surface-gating.js +28 -89
- package/build/update-check.js +97 -16
- package/build/update-status.js +61 -71
- package/build/upgrade-advice.js +15 -11
- package/build/upgrade-cli.js +347 -20
- package/build/version.generated.js +3 -3
- package/package.json +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Bridge-managed Claude Code permission rules for the `/drive-epic-run`
|
|
3
|
-
* supervisor (BAPI-1398, AC-8)
|
|
3
|
+
* supervisor (BAPI-1398, AC-8), and the one pinned SessionStart upgrade-notice
|
|
4
|
+
* hook (BAPI-1446).
|
|
4
5
|
*
|
|
5
6
|
* The supervisor runs in auto mode. Every in-scope action it takes — a
|
|
6
7
|
* sanctioned CLI verb, a Bridge MCP tool, a read, a tally write — must resolve
|
|
@@ -18,7 +19,10 @@
|
|
|
18
19
|
* names Claude Code really sees. The auto-mode
|
|
19
20
|
* classifier ignores `autoMode` in project settings, so the prose
|
|
20
21
|
* `autoMode.allow` / `autoMode.environment` entries go into the USER's
|
|
21
|
-
* `~/.claude/settings.json` — and only with the user's explicit consent.
|
|
22
|
+
* `~/.claude/settings.json` — and only with the user's explicit consent. Install
|
|
23
|
+
* never asks for it (BAPI-1448): the `bridge-permissions` subcommand
|
|
24
|
+
* ({@link runBridgePermissionsCli}) owns that write, and `/drive-epic-run` offers
|
|
25
|
+
* it in-session only when the entries are missing.
|
|
22
26
|
*
|
|
23
27
|
* Ownership. A JSON string in an allow list cannot carry a marker, and Claude
|
|
24
28
|
* Code's settings loader is not given unknown keys. Bridge therefore records the
|
|
@@ -38,9 +42,19 @@
|
|
|
38
42
|
* Never broad: no `Bash(*)`, no wildcard launcher, and no rule that covers a
|
|
39
43
|
* credential file. A launcher override that cannot be written as an exact,
|
|
40
44
|
* secret-free command prefix is refused rather than approximated.
|
|
45
|
+
*
|
|
46
|
+
* The SessionStart hook (BAPI-1446) lives in the same local settings file and is
|
|
47
|
+
* owned the same way: the manifest's `hooks.SessionStart` records the one exact
|
|
48
|
+
* command Bridge wrote, removal deletes exactly that command, and permission and
|
|
49
|
+
* hook changes share one settings snapshot so neither overwrites the other.
|
|
41
50
|
*/
|
|
51
|
+
import fs from "node:fs/promises";
|
|
52
|
+
import os from "node:os";
|
|
42
53
|
import path from "node:path";
|
|
54
|
+
import readline from "node:readline";
|
|
55
|
+
import { inspectClaudeHookContainers, mergeClaudeSettingsWithCommandHookChecked, removeClaudeSettingsCommandHook, } from "./claude-settings.js";
|
|
43
56
|
import { MCP_PACKAGE_NAME, MCP_SERVER_NAME, resolveRegistrationKey } from "./mcp-identity.js";
|
|
57
|
+
import { isPlainReleaseVersion } from "./update-check.js";
|
|
44
58
|
// ---------------------------------------------------------------------------
|
|
45
59
|
// Constants
|
|
46
60
|
// ---------------------------------------------------------------------------
|
|
@@ -58,6 +72,14 @@ export const BRIDGE_PERMISSIONS_MANIFEST_RELPATH = ".bridge/bridge-permissions.j
|
|
|
58
72
|
export const BRIDGE_PROJECT_SETTINGS_RELPATH = ".claude/settings.local.json";
|
|
59
73
|
/** Current manifest schema version. */
|
|
60
74
|
export const BRIDGE_PERMISSIONS_MANIFEST_VERSION = 1;
|
|
75
|
+
/** The Claude Code hook event the upgrade notice runs on. */
|
|
76
|
+
export const SESSION_START_HOOK_EVENT = "SessionStart";
|
|
77
|
+
/** The exact arguments after the launcher: the internal offline check command. */
|
|
78
|
+
export const SESSION_START_HOOK_SUFFIX = "check-update --hook claude-session-start";
|
|
79
|
+
/** Run only on a fresh session start, not on resume, clear, or compact. */
|
|
80
|
+
export const SESSION_START_HOOK_MATCHER = "startup";
|
|
81
|
+
/** Seconds Claude Code waits for the hook. A cold `npx` launch can exceed it. */
|
|
82
|
+
export const SESSION_START_HOOK_TIMEOUT_SECONDS = 5;
|
|
61
83
|
/** The auto-mode sentinel that keeps Claude Code's built-in entries in a list. */
|
|
62
84
|
export const AUTO_MODE_DEFAULTS_SENTINEL = "$defaults";
|
|
63
85
|
/**
|
|
@@ -160,6 +182,10 @@ export function buildProjectPermissionRules(launcherPrefix, serverNames = [MCP_S
|
|
|
160
182
|
rules.push(`Bash(${launcherPrefix} epic-implementer continuation-receipt *)`);
|
|
161
183
|
rules.push(`Bash(${launcherPrefix} epic-implementer doctor)`);
|
|
162
184
|
rules.push(`Bash(${launcherPrefix} plane status)`);
|
|
185
|
+
// BAPI-1448: the read-only user-scope check `/drive-epic-run` runs first. There is
|
|
186
|
+
// deliberately NO rule for `--user-consented`: Claude Code's own permission prompt
|
|
187
|
+
// for that write is the second consent layer.
|
|
188
|
+
rules.push(`Bash(${launcherPrefix} bridge-permissions --check --json)`);
|
|
163
189
|
rules.push("Bash(pmset -g log)");
|
|
164
190
|
rules.push("Bash(git ls-tree *)");
|
|
165
191
|
rules.push("Bash(gh auth status)");
|
|
@@ -269,14 +295,19 @@ export function userSettingsPath(homeDir) {
|
|
|
269
295
|
export function bridgePermissionsManifestPath(cwd) {
|
|
270
296
|
return path.join(cwd, ...BRIDGE_PERMISSIONS_MANIFEST_RELPATH.split("/"));
|
|
271
297
|
}
|
|
272
|
-
/**
|
|
298
|
+
/**
|
|
299
|
+
* Read a settings file: missing is `{}`-equivalent, malformed is never used. Only
|
|
300
|
+
* `ENOENT` means missing. Any other read error (`EACCES` included) is refused like
|
|
301
|
+
* malformed JSON, so no writer replaces a file it could not read.
|
|
302
|
+
*/
|
|
273
303
|
export async function readSettingsFile(filePath, deps) {
|
|
274
304
|
let raw;
|
|
275
305
|
try {
|
|
276
306
|
raw = await deps.readFile(filePath);
|
|
277
307
|
}
|
|
278
|
-
catch {
|
|
279
|
-
|
|
308
|
+
catch (error) {
|
|
309
|
+
const code = error?.code;
|
|
310
|
+
return code === "ENOENT" ? { status: "missing" } : { status: "malformed" };
|
|
280
311
|
}
|
|
281
312
|
try {
|
|
282
313
|
const parsed = JSON.parse(raw);
|
|
@@ -329,10 +360,23 @@ export async function readBridgePermissionsManifest(cwd, deps) {
|
|
|
329
360
|
return { status: "invalid" };
|
|
330
361
|
if (launcher !== null && typeof launcher !== "string")
|
|
331
362
|
return { status: "invalid" };
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
363
|
+
const manifest = {
|
|
364
|
+
version: BRIDGE_PERMISSIONS_MANIFEST_VERSION,
|
|
365
|
+
launcher,
|
|
366
|
+
project,
|
|
367
|
+
userAllow,
|
|
368
|
+
userEnvironment,
|
|
335
369
|
};
|
|
370
|
+
if (doc.hooks !== undefined) {
|
|
371
|
+
const hooks = doc.hooks;
|
|
372
|
+
if (hooks === null || typeof hooks !== "object" || Array.isArray(hooks))
|
|
373
|
+
return { status: "invalid" };
|
|
374
|
+
const sessionStart = stringList(hooks[SESSION_START_HOOK_EVENT]);
|
|
375
|
+
if (!sessionStart)
|
|
376
|
+
return { status: "invalid" };
|
|
377
|
+
manifest.hooks = { SessionStart: sessionStart };
|
|
378
|
+
}
|
|
379
|
+
return { status: "valid", manifest };
|
|
336
380
|
}
|
|
337
381
|
catch {
|
|
338
382
|
return { status: "invalid" };
|
|
@@ -340,13 +384,19 @@ export async function readBridgePermissionsManifest(cwd, deps) {
|
|
|
340
384
|
}
|
|
341
385
|
/** Serialize the manifest with a stable key order. */
|
|
342
386
|
export function serializeBridgePermissionsManifest(manifest) {
|
|
343
|
-
|
|
387
|
+
const doc = {
|
|
344
388
|
version: manifest.version,
|
|
345
389
|
launcher: manifest.launcher,
|
|
346
390
|
project: manifest.project,
|
|
347
391
|
userAllow: manifest.userAllow,
|
|
348
392
|
userEnvironment: manifest.userEnvironment,
|
|
349
|
-
}
|
|
393
|
+
};
|
|
394
|
+
// Written last and only when something is owned, so a manifest without hooks
|
|
395
|
+
// serializes byte-for-byte as it did before hooks existed.
|
|
396
|
+
if (manifest.hooks && manifest.hooks.SessionStart.length > 0) {
|
|
397
|
+
doc.hooks = { SessionStart: manifest.hooks.SessionStart };
|
|
398
|
+
}
|
|
399
|
+
return `${JSON.stringify(doc, null, 2)}\n`;
|
|
350
400
|
}
|
|
351
401
|
async function writeManifestIfChanged(cwd, next, deps) {
|
|
352
402
|
const target = bridgePermissionsManifestPath(cwd);
|
|
@@ -422,56 +472,303 @@ function unique(values) {
|
|
|
422
472
|
return [...new Set(values)];
|
|
423
473
|
}
|
|
424
474
|
/**
|
|
425
|
-
*
|
|
475
|
+
* Build the SessionStart hook command for a resolved launcher and a validated pin.
|
|
476
|
+
* The default launcher is pinned by appending `@<pin>` to its package token; a
|
|
477
|
+
* valid `BAPI_MCP_CLI` override already names its own build and gets the exact
|
|
478
|
+
* hook suffix appended unchanged.
|
|
479
|
+
*/
|
|
480
|
+
export function buildSessionStartHookCommand(launcher, pinnedVersion) {
|
|
481
|
+
const prefix = launcher.source === "default" ? `${launcher.prefix}@${pinnedVersion}` : launcher.prefix;
|
|
482
|
+
return `${prefix} ${SESSION_START_HOOK_SUFFIX}`;
|
|
483
|
+
}
|
|
484
|
+
/** Every command leaf registered for an event, in order. */
|
|
485
|
+
function sessionStartCommands(settings) {
|
|
486
|
+
const hooks = settings.hooks;
|
|
487
|
+
if (hooks === null || typeof hooks !== "object" || Array.isArray(hooks))
|
|
488
|
+
return [];
|
|
489
|
+
const entries = hooks[SESSION_START_HOOK_EVENT];
|
|
490
|
+
if (!Array.isArray(entries))
|
|
491
|
+
return [];
|
|
492
|
+
const commands = [];
|
|
493
|
+
for (const entry of entries) {
|
|
494
|
+
const leaves = entry?.hooks;
|
|
495
|
+
if (!Array.isArray(leaves))
|
|
496
|
+
continue;
|
|
497
|
+
for (const leaf of leaves) {
|
|
498
|
+
const found = leaf;
|
|
499
|
+
if (found && found.type === "command" && typeof found.command === "string") {
|
|
500
|
+
commands.push(found.command);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
return commands;
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* True when `command` is registered exactly once, in an entry that carries the
|
|
508
|
+
* startup matcher, on a leaf that carries the five-second timeout.
|
|
509
|
+
*/
|
|
510
|
+
function isHookConfiguredCorrectly(settings, command) {
|
|
511
|
+
const hooks = settings.hooks;
|
|
512
|
+
const entries = hooks[SESSION_START_HOOK_EVENT];
|
|
513
|
+
let found = 0;
|
|
514
|
+
let correct = false;
|
|
515
|
+
for (const entry of entries) {
|
|
516
|
+
const leaves = entry?.hooks;
|
|
517
|
+
if (!Array.isArray(leaves))
|
|
518
|
+
continue;
|
|
519
|
+
for (const leaf of leaves) {
|
|
520
|
+
const candidate = leaf;
|
|
521
|
+
if (candidate && candidate.type === "command" && candidate.command === command) {
|
|
522
|
+
found += 1;
|
|
523
|
+
correct =
|
|
524
|
+
entry.matcher === SESSION_START_HOOK_MATCHER &&
|
|
525
|
+
candidate.timeout === SESSION_START_HOOK_TIMEOUT_SECONDS;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return found === 1 && correct;
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Is an UNRECORDED command narrowly recognizable as Bridge's own hook? Only on
|
|
533
|
+
* install or upgrade, and only when the whole string is the default exact-pinned
|
|
534
|
+
* launcher or the current override prefix, followed by the exact hook suffix.
|
|
535
|
+
* `@latest`, an unpinned launcher, extra flags, another executable, and another
|
|
536
|
+
* package path are all NOT recognized.
|
|
537
|
+
*/
|
|
538
|
+
function isRecognizedBridgeHookCommand(command, launcher) {
|
|
539
|
+
if (launcher.source === "override" && command === `${launcher.prefix} ${SESSION_START_HOOK_SUFFIX}`) {
|
|
540
|
+
return true;
|
|
541
|
+
}
|
|
542
|
+
const lead = `${BRIDGE_DEFAULT_LAUNCHER}@`;
|
|
543
|
+
const tail = ` ${SESSION_START_HOOK_SUFFIX}`;
|
|
544
|
+
if (!command.startsWith(lead) || !command.endsWith(tail))
|
|
545
|
+
return false;
|
|
546
|
+
return isPlainReleaseVersion(command.slice(lead.length, command.length - tail.length));
|
|
547
|
+
}
|
|
548
|
+
/** A command that looks like a Bridge hook without being recognized as one. */
|
|
549
|
+
function looksLikeBridgeHook(command) {
|
|
550
|
+
return command.includes("check-update") && command.includes("claude-session-start");
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* Compute the settings that leave exactly one correctly configured Bridge hook,
|
|
554
|
+
* in memory. Owned commands are the manifest-recorded ones plus, on install or
|
|
555
|
+
* upgrade only, narrowly recognized unrecorded ones. Every owned command is
|
|
556
|
+
* removed before the desired one is merged, so a changed pin is replaced rather
|
|
557
|
+
* than duplicated; commands that are not owned are never touched.
|
|
558
|
+
*/
|
|
559
|
+
function planSessionStartHook(settings, command, recorded, launcher) {
|
|
560
|
+
// A present-but-wrong-typed `hooks` or `SessionStart` is refused, never replaced.
|
|
561
|
+
if (inspectClaudeHookContainers(settings, [SESSION_START_HOOK_EVENT]) === "malformed") {
|
|
562
|
+
return "malformed";
|
|
563
|
+
}
|
|
564
|
+
const present = sessionStartCommands(settings);
|
|
565
|
+
const recordedSet = new Set(recorded);
|
|
566
|
+
const owned = unique(present.filter((c) => recordedSet.has(c) || isRecognizedBridgeHookCommand(c, launcher)));
|
|
567
|
+
const unrecognized = present.filter((c) => !owned.includes(c) && looksLikeBridgeHook(c)).length;
|
|
568
|
+
const others = owned.filter((c) => c !== command);
|
|
569
|
+
if (others.length === 0 && present.includes(command) && isHookConfiguredCorrectly(settings, command)) {
|
|
570
|
+
return { settings, changed: false, command, replaced: 0, unrecognized };
|
|
571
|
+
}
|
|
572
|
+
let working = settings;
|
|
573
|
+
for (const stale of owned) {
|
|
574
|
+
working = removeClaudeSettingsCommandHook(working, SESSION_START_HOOK_EVENT, stale);
|
|
575
|
+
}
|
|
576
|
+
const rebuilt = mergeClaudeSettingsWithCommandHookChecked(working, command, [SESSION_START_HOOK_EVENT], {
|
|
577
|
+
eventMatchers: { [SESSION_START_HOOK_EVENT]: SESSION_START_HOOK_MATCHER },
|
|
578
|
+
eventTimeouts: { [SESSION_START_HOOK_EVENT]: SESSION_START_HOOK_TIMEOUT_SECONDS },
|
|
579
|
+
});
|
|
580
|
+
if (!rebuilt.ok)
|
|
581
|
+
return "malformed";
|
|
582
|
+
return { settings: rebuilt.settings, changed: true, command, replaced: others.length, unrecognized };
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* Write the project `permissions.allow` catalog for the resolved launcher, and
|
|
586
|
+
* optionally the pinned SessionStart hook.
|
|
426
587
|
*
|
|
427
588
|
* Only missing strings are appended; every other entry and key is kept. Strings
|
|
428
589
|
* Bridge recorded earlier that are no longer desired (for example after the
|
|
429
590
|
* launcher changed) are removed. The manifest gains exactly the strings this run
|
|
430
591
|
* inserted. Nothing is written when nothing changes.
|
|
592
|
+
*
|
|
593
|
+
* Permissions and the hook are applied to ONE settings snapshot and written once,
|
|
594
|
+
* and the manifest is written only after the settings write succeeds. A scope that
|
|
595
|
+
* cannot be changed safely (malformed structure, failed write) keeps its prior
|
|
596
|
+
* ownership record, and a settings success followed by a manifest failure is
|
|
597
|
+
* reported as a `partial` hook outcome rather than as full provisioning.
|
|
431
598
|
*/
|
|
432
599
|
export async function applyProjectPermissions(params, deps) {
|
|
433
600
|
const launcher = resolvePermissionLauncher(params.env);
|
|
601
|
+
const request = params.sessionStartHook;
|
|
434
602
|
if (!launcher.ok) {
|
|
435
|
-
return {
|
|
603
|
+
return {
|
|
604
|
+
launcher,
|
|
605
|
+
project: { status: "launcher-refused", skipped: [] },
|
|
606
|
+
manifestRecorded: false,
|
|
607
|
+
...(request ? { hook: { status: "refused", reason: launcher.reason } } : {}),
|
|
608
|
+
};
|
|
436
609
|
}
|
|
437
610
|
const desired = buildProjectPermissionRules(launcher.prefix, await resolveSupervisorServerNames(params.cwd, deps));
|
|
438
611
|
const settingsPath = projectSettingsPath(params.cwd);
|
|
439
612
|
const manifestRead = await readBridgePermissionsManifest(params.cwd, deps);
|
|
440
613
|
const manifest = manifestRead.status === "valid" ? manifestRead.manifest : emptyManifest();
|
|
441
614
|
const read = await readSettingsFile(settingsPath, deps);
|
|
442
|
-
|
|
443
|
-
const allow = read.status === "malformed" ? null : projectAllowList(current);
|
|
444
|
-
if (allow === null) {
|
|
615
|
+
if (read.status === "malformed") {
|
|
445
616
|
return {
|
|
446
617
|
launcher,
|
|
447
618
|
project: { status: "malformed", path: settingsPath, skipped: desired },
|
|
448
619
|
manifestRecorded: false,
|
|
620
|
+
...(request ? { hook: invalidPinOr(request, { status: "malformed", path: settingsPath }) } : {}),
|
|
449
621
|
};
|
|
450
622
|
}
|
|
451
|
-
const
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
const
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
623
|
+
const current = read.status === "valid" ? read.settings : {};
|
|
624
|
+
let working = current;
|
|
625
|
+
// ---- Permission rules ----
|
|
626
|
+
const allow = projectAllowList(current);
|
|
627
|
+
let project;
|
|
628
|
+
let permissionsChanged = false;
|
|
629
|
+
let permissionsApplied = false;
|
|
630
|
+
let inserted = [];
|
|
631
|
+
let ownedStill = manifest.project;
|
|
632
|
+
if (allow === null) {
|
|
633
|
+
project = { status: "malformed", path: settingsPath, skipped: desired };
|
|
634
|
+
}
|
|
635
|
+
else {
|
|
636
|
+
const desiredSet = new Set(desired);
|
|
637
|
+
const stale = manifest.project.filter((rule) => !desiredSet.has(rule));
|
|
638
|
+
const staleSet = new Set(stale);
|
|
639
|
+
const removedStale = allow.filter((rule) => staleSet.has(rule));
|
|
640
|
+
const kept = allow.filter((rule) => !staleSet.has(rule));
|
|
641
|
+
const keptSet = new Set(kept);
|
|
642
|
+
inserted = desired.filter((rule) => !keptSet.has(rule));
|
|
643
|
+
const alreadyPresent = desired.filter((rule) => keptSet.has(rule));
|
|
644
|
+
if (inserted.length > 0 || removedStale.length > 0) {
|
|
645
|
+
working = withProjectAllowList(working, [...kept, ...inserted]);
|
|
646
|
+
permissionsChanged = true;
|
|
647
|
+
}
|
|
648
|
+
ownedStill = manifest.project.filter((rule) => desiredSet.has(rule));
|
|
649
|
+
permissionsApplied = true;
|
|
650
|
+
project = {
|
|
651
|
+
status: "unchanged",
|
|
652
|
+
path: settingsPath,
|
|
653
|
+
inserted,
|
|
654
|
+
alreadyPresent,
|
|
655
|
+
removedStale,
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
// ---- SessionStart hook ----
|
|
659
|
+
let hookPlan = null;
|
|
660
|
+
let hook;
|
|
661
|
+
if (request) {
|
|
662
|
+
if (!isPlainReleaseVersion(request.pinnedVersion)) {
|
|
663
|
+
hook = { status: "skipped", reason: "invalid-pin" };
|
|
664
|
+
}
|
|
665
|
+
else {
|
|
666
|
+
const command = buildSessionStartHookCommand(launcher, request.pinnedVersion);
|
|
667
|
+
const planned = planSessionStartHook(working, command, manifest.hooks?.SessionStart ?? [], launcher);
|
|
668
|
+
if (planned === "malformed") {
|
|
669
|
+
hook = { status: "malformed", path: settingsPath };
|
|
670
|
+
}
|
|
671
|
+
else {
|
|
672
|
+
hookPlan = planned;
|
|
673
|
+
working = planned.settings;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
const hookChanged = hookPlan?.changed === true;
|
|
678
|
+
// ---- One settings write ----
|
|
679
|
+
if (permissionsChanged || hookChanged) {
|
|
680
|
+
const ok = await writeSettings(settingsPath, working, deps);
|
|
463
681
|
if (!ok) {
|
|
464
|
-
return {
|
|
682
|
+
return {
|
|
683
|
+
launcher,
|
|
684
|
+
project: permissionsApplied
|
|
685
|
+
? { status: "io-error", path: settingsPath, skipped: inserted }
|
|
686
|
+
: project,
|
|
687
|
+
manifestRecorded: false,
|
|
688
|
+
...(request ? { hook: hookPlan ? { status: "io-error", path: settingsPath } : hook } : {}),
|
|
689
|
+
};
|
|
465
690
|
}
|
|
466
|
-
status
|
|
691
|
+
if (project.status === "unchanged" && permissionsChanged)
|
|
692
|
+
project = { ...project, status: "written" };
|
|
467
693
|
}
|
|
468
|
-
|
|
469
|
-
const
|
|
470
|
-
|
|
471
|
-
launcher
|
|
472
|
-
project
|
|
473
|
-
|
|
474
|
-
|
|
694
|
+
// ---- One manifest write, after the settings are durable ----
|
|
695
|
+
const nextManifest = { ...manifest };
|
|
696
|
+
if (permissionsApplied) {
|
|
697
|
+
nextManifest.launcher = launcher.prefix;
|
|
698
|
+
nextManifest.project = unique([...ownedStill, ...inserted]);
|
|
699
|
+
}
|
|
700
|
+
if (hookPlan)
|
|
701
|
+
nextManifest.hooks = { SessionStart: [hookPlan.command] };
|
|
702
|
+
const needsManifest = permissionsApplied || hookPlan !== null;
|
|
703
|
+
const manifestRecorded = needsManifest ? await writeManifestIfChanged(params.cwd, nextManifest, deps) : false;
|
|
704
|
+
if (hookPlan) {
|
|
705
|
+
const base = {
|
|
706
|
+
path: settingsPath,
|
|
707
|
+
command: hookPlan.command,
|
|
708
|
+
replaced: hookPlan.replaced,
|
|
709
|
+
unrecognized: hookPlan.unrecognized,
|
|
710
|
+
};
|
|
711
|
+
hook = manifestRecorded
|
|
712
|
+
? { status: hookChanged ? "written" : "unchanged", ...base }
|
|
713
|
+
: { status: "partial", ...base };
|
|
714
|
+
}
|
|
715
|
+
return { launcher, project, manifestRecorded, ...(request && hook ? { hook } : {}) };
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* Plain-language report lines for a hook outcome, shared by install and upgrade so
|
|
719
|
+
* the two cannot drift. `lines` are progress; `warnings` need attention but never
|
|
720
|
+
* change a primary exit code. Nothing here echoes a similar-looking user command:
|
|
721
|
+
* unrecognized candidates are counted only.
|
|
722
|
+
*/
|
|
723
|
+
export function describeSessionStartHookOutcome(hook) {
|
|
724
|
+
const lines = [];
|
|
725
|
+
const warnings = [];
|
|
726
|
+
switch (hook.status) {
|
|
727
|
+
case "written":
|
|
728
|
+
case "unchanged":
|
|
729
|
+
case "partial": {
|
|
730
|
+
if (hook.status === "written") {
|
|
731
|
+
lines.push(`SessionStart upgrade-notice hook (Claude Code only, per developer) in ${hook.path}:`);
|
|
732
|
+
lines.push(` wrote ${hook.command}`);
|
|
733
|
+
if (hook.replaced > 0) {
|
|
734
|
+
lines.push(` replaced ${hook.replaced} earlier Bridge hook command(s) with this one`);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
else if (hook.status === "unchanged") {
|
|
738
|
+
lines.push(`SessionStart upgrade-notice hook already present in ${hook.path}.`);
|
|
739
|
+
}
|
|
740
|
+
else {
|
|
741
|
+
warnings.push(`the SessionStart upgrade-notice hook was written to ${hook.path}, but ` +
|
|
742
|
+
`${BRIDGE_PERMISSIONS_MANIFEST_RELPATH} could not be updated; doctor and removal may not see it`);
|
|
743
|
+
}
|
|
744
|
+
if (hook.unrecognized > 0) {
|
|
745
|
+
warnings.push(`${hook.unrecognized} existing SessionStart command(s) resemble Bridge's hook but were not ` +
|
|
746
|
+
"recognized as Bridge's, so they were left in place");
|
|
747
|
+
}
|
|
748
|
+
break;
|
|
749
|
+
}
|
|
750
|
+
case "skipped":
|
|
751
|
+
warnings.push("the SessionStart upgrade-notice hook was not written: no valid pinned Bridge version could be " +
|
|
752
|
+
"determined for this project");
|
|
753
|
+
break;
|
|
754
|
+
case "refused":
|
|
755
|
+
warnings.push(`the SessionStart upgrade-notice hook was not written: ${hook.reason}`);
|
|
756
|
+
break;
|
|
757
|
+
case "malformed":
|
|
758
|
+
warnings.push(`${hook.path} has a malformed hooks structure and was left unchanged; ` +
|
|
759
|
+
"the SessionStart upgrade-notice hook was NOT written");
|
|
760
|
+
break;
|
|
761
|
+
case "io-error":
|
|
762
|
+
warnings.push(`${hook.path} could not be written; the SessionStart upgrade-notice hook was NOT written`);
|
|
763
|
+
break;
|
|
764
|
+
}
|
|
765
|
+
return { lines, warnings };
|
|
766
|
+
}
|
|
767
|
+
/** A malformed settings file cannot take the hook, but an invalid pin is reported first. */
|
|
768
|
+
function invalidPinOr(request, fallback) {
|
|
769
|
+
return isPlainReleaseVersion(request.pinnedVersion)
|
|
770
|
+
? fallback
|
|
771
|
+
: { status: "skipped", reason: "invalid-pin" };
|
|
475
772
|
}
|
|
476
773
|
/**
|
|
477
774
|
* Write Bridge's `autoMode.allow` and `autoMode.environment` prose into the
|
|
@@ -622,32 +919,96 @@ export async function removeBridgePermissions(params, deps) {
|
|
|
622
919
|
const manifestRead = await readBridgePermissionsManifest(params.cwd, deps);
|
|
623
920
|
const manifest = manifestRead.status === "valid" ? manifestRead.manifest : emptyManifest();
|
|
624
921
|
const next = { ...manifest };
|
|
625
|
-
// Project scope
|
|
922
|
+
// Project scope: the permission rules and the SessionStart hook share ONE
|
|
923
|
+
// settings snapshot and ONE write, so neither removal can overwrite the other.
|
|
626
924
|
const projectPath = projectSettingsPath(params.cwd);
|
|
925
|
+
const recordedHooks = manifest.hooks?.SessionStart ?? [];
|
|
627
926
|
let project;
|
|
628
|
-
|
|
927
|
+
let hook;
|
|
928
|
+
if (manifest.project.length === 0 && recordedHooks.length === 0) {
|
|
629
929
|
project = { status: "nothing-recorded", path: projectPath, removed: [] };
|
|
930
|
+
hook = { status: "nothing-recorded", path: projectPath, removed: [] };
|
|
630
931
|
}
|
|
631
932
|
else {
|
|
632
933
|
const read = await readSettingsFile(projectPath, deps);
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
934
|
+
if (read.status === "malformed") {
|
|
935
|
+
project =
|
|
936
|
+
manifest.project.length === 0
|
|
937
|
+
? { status: "nothing-recorded", path: projectPath, removed: [] }
|
|
938
|
+
: { status: "malformed", path: projectPath, unremovable: manifest.project };
|
|
939
|
+
hook =
|
|
940
|
+
recordedHooks.length === 0
|
|
941
|
+
? { status: "nothing-recorded", path: projectPath, removed: [] }
|
|
942
|
+
: { status: "malformed", path: projectPath, unremovable: recordedHooks };
|
|
636
943
|
}
|
|
637
944
|
else {
|
|
638
|
-
const
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
945
|
+
const settings = read.status === "valid" ? read.settings : {};
|
|
946
|
+
let working = settings;
|
|
947
|
+
// Rules.
|
|
948
|
+
const allow = projectAllowList(settings);
|
|
949
|
+
let rulesRemoved = [];
|
|
950
|
+
let rulesMalformed = false;
|
|
951
|
+
if (manifest.project.length > 0) {
|
|
952
|
+
if (allow === null) {
|
|
953
|
+
rulesMalformed = true;
|
|
954
|
+
}
|
|
955
|
+
else {
|
|
956
|
+
const owned = new Set(manifest.project);
|
|
957
|
+
rulesRemoved = allow.filter((rule) => owned.has(rule));
|
|
958
|
+
if (rulesRemoved.length > 0) {
|
|
959
|
+
working = withProjectAllowList(working, allow.filter((rule) => !owned.has(rule)));
|
|
960
|
+
}
|
|
961
|
+
}
|
|
643
962
|
}
|
|
644
|
-
|
|
645
|
-
|
|
963
|
+
// Hook: only the exact recorded commands, never a recognizable lookalike.
|
|
964
|
+
let hooksRemoved = [];
|
|
965
|
+
let hooksMalformed = false;
|
|
966
|
+
if (recordedHooks.length > 0) {
|
|
967
|
+
if (inspectClaudeHookContainers(working, [SESSION_START_HOOK_EVENT]) === "malformed") {
|
|
968
|
+
hooksMalformed = true;
|
|
969
|
+
}
|
|
970
|
+
else {
|
|
971
|
+
const present = new Set(sessionStartCommands(working));
|
|
972
|
+
for (const command of recordedHooks) {
|
|
973
|
+
if (present.has(command))
|
|
974
|
+
hooksRemoved.push(command);
|
|
975
|
+
working = removeClaudeSettingsCommandHook(working, SESSION_START_HOOK_EVENT, command);
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
// One write, only when something changed and the file exists.
|
|
980
|
+
let writeOk = true;
|
|
981
|
+
if ((rulesRemoved.length > 0 || hooksRemoved.length > 0) && read.status === "valid") {
|
|
982
|
+
writeOk = await writeSettings(projectPath, working, deps);
|
|
983
|
+
}
|
|
984
|
+
if (manifest.project.length === 0) {
|
|
985
|
+
project = { status: "nothing-recorded", path: projectPath, removed: [] };
|
|
986
|
+
}
|
|
987
|
+
else if (rulesMalformed) {
|
|
988
|
+
project = { status: "malformed", path: projectPath, unremovable: manifest.project };
|
|
989
|
+
}
|
|
990
|
+
else if (writeOk || rulesRemoved.length === 0) {
|
|
991
|
+
project = { status: "removed", path: projectPath, removed: rulesRemoved };
|
|
646
992
|
next.project = [];
|
|
647
993
|
next.launcher = null;
|
|
648
994
|
}
|
|
649
995
|
else {
|
|
650
|
-
project = { status: "io-error", path: projectPath, unremovable:
|
|
996
|
+
project = { status: "io-error", path: projectPath, unremovable: rulesRemoved };
|
|
997
|
+
}
|
|
998
|
+
if (recordedHooks.length === 0) {
|
|
999
|
+
hook = { status: "nothing-recorded", path: projectPath, removed: [] };
|
|
1000
|
+
}
|
|
1001
|
+
else if (hooksMalformed) {
|
|
1002
|
+
hook = { status: "malformed", path: projectPath, unremovable: recordedHooks };
|
|
1003
|
+
}
|
|
1004
|
+
else if (writeOk || hooksRemoved.length === 0) {
|
|
1005
|
+
// Ownership clears only once the settings write succeeded (or there was
|
|
1006
|
+
// nothing left in the file to remove).
|
|
1007
|
+
hook = { status: "removed", path: projectPath, removed: hooksRemoved };
|
|
1008
|
+
next.hooks = undefined;
|
|
1009
|
+
}
|
|
1010
|
+
else {
|
|
1011
|
+
hook = { status: "io-error", path: projectPath, unremovable: hooksRemoved };
|
|
651
1012
|
}
|
|
652
1013
|
}
|
|
653
1014
|
}
|
|
@@ -693,23 +1054,40 @@ export async function removeBridgePermissions(params, deps) {
|
|
|
693
1054
|
}
|
|
694
1055
|
const changed = next.project.length !== manifest.project.length ||
|
|
695
1056
|
next.userAllow.length !== manifest.userAllow.length ||
|
|
696
|
-
next.userEnvironment.length !== manifest.userEnvironment.length
|
|
1057
|
+
next.userEnvironment.length !== manifest.userEnvironment.length ||
|
|
1058
|
+
(next.hooks?.SessionStart.length ?? 0) !== (manifest.hooks?.SessionStart.length ?? 0);
|
|
697
1059
|
const manifestUpdated = manifestRead.status === "valid" && changed ? await writeManifestIfChanged(params.cwd, next, deps) : true;
|
|
698
|
-
return { project, user, manifestUpdated };
|
|
1060
|
+
return { project, hook, user, manifestUpdated };
|
|
699
1061
|
}
|
|
700
|
-
/**
|
|
701
|
-
|
|
702
|
-
|
|
1062
|
+
/**
|
|
1063
|
+
* The consent text for the user-settings write (BAPI-1448). The CLI's interactive
|
|
1064
|
+
* mode prints everything up to the last line and reads the answer at that line;
|
|
1065
|
+
* `/drive-epic-run` step 3a reproduces the same text byte for byte, and a test pins
|
|
1066
|
+
* the two together.
|
|
1067
|
+
*/
|
|
1068
|
+
export const USER_AUTO_MODE_CONSENT_PROMPT = "Epic runs work best in Claude Code's auto mode, where Claude acts without asking you each\n" +
|
|
1069
|
+
"time. Auto mode has a safety check that can pause routine run steps, like creating the epic's\n" +
|
|
1070
|
+
"branches or updating the run's own records, because it doesn't know they belong to the run.\n" +
|
|
1071
|
+
"\n" +
|
|
1072
|
+
"Bridge can add a short description of the run's sandbox to ~/.claude/settings.json so the\n" +
|
|
1073
|
+
"check recognizes this work. It describes:\n" +
|
|
1074
|
+
" • the run's own branches (epic/<KEY> and each child ticket's branch) and their worktrees\n" +
|
|
1075
|
+
" • the Bridge server\n" +
|
|
1076
|
+
" • any sandbox accounts your project names\n" +
|
|
1077
|
+
"It grants no other access, and you can remove it any time with --remove-bridge-permissions.\n" +
|
|
1078
|
+
"\n" +
|
|
1079
|
+
"Add it? (y/N) ";
|
|
703
1080
|
function logList(log, items) {
|
|
704
1081
|
for (const item of items)
|
|
705
1082
|
log(` ${item}`);
|
|
706
1083
|
}
|
|
707
1084
|
/**
|
|
708
|
-
* The installer's permission step. Writes the project rules
|
|
709
|
-
*
|
|
710
|
-
*
|
|
711
|
-
*
|
|
712
|
-
*
|
|
1085
|
+
* The installer's permission step. Writes the project rules and the pinned
|
|
1086
|
+
* SessionStart hook to `.claude/settings.local.json` and reports them. It never
|
|
1087
|
+
* reads, writes, or mentions the user's `~/.claude/settings.json`: that write is
|
|
1088
|
+
* the `bridge-permissions` subcommand's job (BAPI-1448). Every rule written is
|
|
1089
|
+
* listed, with the launcher prefix it covers. A malformed file is reported on
|
|
1090
|
+
* stderr and skipped; the rest of the install continues.
|
|
713
1091
|
*/
|
|
714
1092
|
export async function runBridgePermissionsInstallStep(deps) {
|
|
715
1093
|
const { log, errorLog } = deps;
|
|
@@ -725,7 +1103,7 @@ export async function runBridgePermissionsInstallStep(deps) {
|
|
|
725
1103
|
}
|
|
726
1104
|
}
|
|
727
1105
|
}
|
|
728
|
-
const project = await applyProjectPermissions({ cwd: deps.cwd, env: deps.env }, deps);
|
|
1106
|
+
const project = await applyProjectPermissions({ cwd: deps.cwd, env: deps.env, sessionStartHook: deps.sessionStartHook }, deps);
|
|
729
1107
|
if (!project.launcher.ok) {
|
|
730
1108
|
errorLog(` warning: project permission rules were not written: ${project.launcher.reason}.`);
|
|
731
1109
|
errorLog(` Unset ${BRIDGE_LAUNCHER_ENV}, or set it to a plain command prefix, then re-run install.`);
|
|
@@ -757,41 +1135,16 @@ export async function runBridgePermissionsInstallStep(deps) {
|
|
|
757
1135
|
errorLog(` warning: could not update ${BRIDGE_PERMISSIONS_MANIFEST_RELPATH}; doctor and removal may not see these rules.`);
|
|
758
1136
|
}
|
|
759
1137
|
}
|
|
760
|
-
//
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
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
|
-
}
|
|
1138
|
+
// The hook is reported on its own: a refused launcher or a malformed hooks
|
|
1139
|
+
// structure never hides, or is hidden by, the permission-rule result above.
|
|
1140
|
+
if (project.hook) {
|
|
1141
|
+
const described = describeSessionStartHookOutcome(project.hook);
|
|
1142
|
+
for (const line of described.lines)
|
|
1143
|
+
log(` ${line}`);
|
|
1144
|
+
for (const warning of described.warnings)
|
|
1145
|
+
errorLog(` warning: ${warning}.`);
|
|
788
1146
|
}
|
|
789
|
-
|
|
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 };
|
|
1147
|
+
return { project };
|
|
795
1148
|
}
|
|
796
1149
|
/**
|
|
797
1150
|
* The removal flag's step. Removes only Bridge-recorded strings and reports what
|
|
@@ -804,9 +1157,14 @@ export async function runBridgePermissionsRemoval(deps) {
|
|
|
804
1157
|
let failed = false;
|
|
805
1158
|
for (const [label, scope] of [
|
|
806
1159
|
["project permissions.allow", result.project],
|
|
1160
|
+
["project SessionStart hook", result.hook],
|
|
807
1161
|
["user autoMode", result.user],
|
|
808
1162
|
]) {
|
|
809
1163
|
if (scope.status === "nothing-recorded") {
|
|
1164
|
+
// The hook is a newer scope: say nothing when it never owned anything, so an
|
|
1165
|
+
// install that never wrote one keeps its original removal output.
|
|
1166
|
+
if (label === "project SessionStart hook")
|
|
1167
|
+
continue;
|
|
810
1168
|
log(`${label}: nothing Bridge wrote is recorded (${scope.path}).`);
|
|
811
1169
|
}
|
|
812
1170
|
else if (scope.status === "removed") {
|
|
@@ -827,3 +1185,178 @@ export async function runBridgePermissionsRemoval(deps) {
|
|
|
827
1185
|
log("Entries you added yourself were left in place.");
|
|
828
1186
|
return failed ? 1 : 0;
|
|
829
1187
|
}
|
|
1188
|
+
/**
|
|
1189
|
+
* Parse the subcommand's argv. Only no flags, `--check --json` (either order),
|
|
1190
|
+
* `--user-consented`, and `--help` are accepted; every other form — an unknown flag,
|
|
1191
|
+
* a repeated flag, `--check` without `--json`, `--check` with `--user-consented` — is
|
|
1192
|
+
* refused here, before any file is read.
|
|
1193
|
+
*/
|
|
1194
|
+
export function parseBridgePermissionsArgv(argv) {
|
|
1195
|
+
if (argv.length === 0)
|
|
1196
|
+
return { ok: true, mode: "interactive" };
|
|
1197
|
+
if (argv.length === 1 && (argv[0] === "--help" || argv[0] === "-h"))
|
|
1198
|
+
return { ok: true, mode: "help" };
|
|
1199
|
+
if (argv.length === 1 && argv[0] === "--user-consented")
|
|
1200
|
+
return { ok: true, mode: "user-consented" };
|
|
1201
|
+
if (argv.length === 2 && argv.includes("--check") && argv.includes("--json")) {
|
|
1202
|
+
return { ok: true, mode: "check-json" };
|
|
1203
|
+
}
|
|
1204
|
+
return { ok: false, error: `Unsupported arguments: ${BRIDGE_PERMISSIONS_USAGE_HINT}` };
|
|
1205
|
+
}
|
|
1206
|
+
const BRIDGE_PERMISSIONS_USAGE_HINT = "use no flags, `--check --json`, `--user-consented`, or `--help`.";
|
|
1207
|
+
/** Static help text for the subcommand. */
|
|
1208
|
+
export function getBridgePermissionsUsage() {
|
|
1209
|
+
return [
|
|
1210
|
+
"Usage:",
|
|
1211
|
+
` npx -y ${MCP_PACKAGE_NAME} bridge-permissions Ask, then add Bridge's auto-mode`,
|
|
1212
|
+
" sandbox description to ~/.claude/settings.json",
|
|
1213
|
+
` npx -y ${MCP_PACKAGE_NAME} bridge-permissions --check --json Read-only: print the user-scope state`,
|
|
1214
|
+
" (present, missing, or malformed) as JSON",
|
|
1215
|
+
` npx -y ${MCP_PACKAGE_NAME} bridge-permissions --user-consented`,
|
|
1216
|
+
" Write without asking. Reserved for /drive-epic-run,",
|
|
1217
|
+
" after you said yes to its question in the current",
|
|
1218
|
+
" session. Do not script it.",
|
|
1219
|
+
"",
|
|
1220
|
+
"Run from the project root: the ownership manifest is .bridge/bridge-permissions.json in the",
|
|
1221
|
+
"current directory. Without a terminal, no-flag mode writes nothing and prints what it would add.",
|
|
1222
|
+
`Remove the entries later with: npx -y ${MCP_PACKAGE_NAME} install --remove-bridge-permissions`,
|
|
1223
|
+
].join("\n");
|
|
1224
|
+
}
|
|
1225
|
+
/** Report one user-settings write: wording shared by the interactive and consented modes. */
|
|
1226
|
+
function reportUserAutoModeWrite(result, log, errorLog) {
|
|
1227
|
+
const scope = result.user;
|
|
1228
|
+
let exitCode = 0;
|
|
1229
|
+
if (scope.status === "written" || scope.status === "unchanged") {
|
|
1230
|
+
log(`${scope.path}:`);
|
|
1231
|
+
if (scope.inserted.length > 0) {
|
|
1232
|
+
log(`Wrote ${scope.inserted.length} autoMode entr${scope.inserted.length === 1 ? "y" : "ies"} ("$defaults" kept):`);
|
|
1233
|
+
logList(log, scope.inserted);
|
|
1234
|
+
}
|
|
1235
|
+
if (scope.alreadyPresent.length > 0) {
|
|
1236
|
+
log(`Already present (${scope.alreadyPresent.length}):`);
|
|
1237
|
+
logList(log, scope.alreadyPresent);
|
|
1238
|
+
}
|
|
1239
|
+
if (!result.manifestRecorded) {
|
|
1240
|
+
errorLog(`warning: could not update ${BRIDGE_PERMISSIONS_MANIFEST_RELPATH}; doctor and removal may not see these entries.`);
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
else if (scope.status === "malformed" || scope.status === "io-error") {
|
|
1244
|
+
exitCode = 1;
|
|
1245
|
+
errorLog(`warning: ${scope.path} ${scope.status === "malformed" ? "is not valid JSON settings and was left unchanged" : "could not be written"}; ` +
|
|
1246
|
+
"these autoMode entries were NOT written:");
|
|
1247
|
+
logList(errorLog, scope.skipped);
|
|
1248
|
+
}
|
|
1249
|
+
return exitCode;
|
|
1250
|
+
}
|
|
1251
|
+
/** Split the consent text into its printed body and its final prompt line. */
|
|
1252
|
+
function splitConsentPrompt() {
|
|
1253
|
+
const at = USER_AUTO_MODE_CONSENT_PROMPT.lastIndexOf("\n");
|
|
1254
|
+
return {
|
|
1255
|
+
body: USER_AUTO_MODE_CONSENT_PROMPT.slice(0, at),
|
|
1256
|
+
prompt: USER_AUTO_MODE_CONSENT_PROMPT.slice(at + 1),
|
|
1257
|
+
};
|
|
1258
|
+
}
|
|
1259
|
+
/**
|
|
1260
|
+
* Run the subcommand and return the process exit code. It never throws: an
|
|
1261
|
+
* unexpected failure is reported (without detail) on stderr and returned as `1`.
|
|
1262
|
+
*
|
|
1263
|
+
* - no flags, interactive: print the consent text, read one answer; only `y` or
|
|
1264
|
+
* `yes` writes. Blank, EOF, and a read failure write nothing and exit 0.
|
|
1265
|
+
* - no flags, no terminal: write nothing, print the entries it would add, exit 0.
|
|
1266
|
+
* - `--check --json`: read-only; one JSON object for the user scope, exit 0 even
|
|
1267
|
+
* when the scope is `malformed`.
|
|
1268
|
+
* - `--user-consented`: write without asking (reserved for `/drive-epic-run`).
|
|
1269
|
+
*
|
|
1270
|
+
* Only the user-settings step runs here; project rules and hooks are never touched.
|
|
1271
|
+
*/
|
|
1272
|
+
export async function runBridgePermissionsCli(argv, deps) {
|
|
1273
|
+
const parsed = parseBridgePermissionsArgv(argv);
|
|
1274
|
+
if (!parsed.ok) {
|
|
1275
|
+
deps.errorLog(parsed.error);
|
|
1276
|
+
deps.errorLog(getBridgePermissionsUsage());
|
|
1277
|
+
return 1;
|
|
1278
|
+
}
|
|
1279
|
+
try {
|
|
1280
|
+
if (parsed.mode === "help") {
|
|
1281
|
+
deps.log(getBridgePermissionsUsage());
|
|
1282
|
+
return 0;
|
|
1283
|
+
}
|
|
1284
|
+
if (parsed.mode === "check-json") {
|
|
1285
|
+
const inspection = await inspectBridgePermissions({ cwd: deps.cwd, homeDir: deps.homeDir, env: deps.env }, deps);
|
|
1286
|
+
const user = inspection.user;
|
|
1287
|
+
const payload = {
|
|
1288
|
+
state: user.state,
|
|
1289
|
+
path: user.path,
|
|
1290
|
+
};
|
|
1291
|
+
if ("missing" in user)
|
|
1292
|
+
payload.missing = user.missing;
|
|
1293
|
+
deps.log(JSON.stringify(payload));
|
|
1294
|
+
return 0;
|
|
1295
|
+
}
|
|
1296
|
+
if (parsed.mode === "user-consented") {
|
|
1297
|
+
const result = await applyUserAutoModeEntries({ cwd: deps.cwd, homeDir: deps.homeDir }, deps);
|
|
1298
|
+
return reportUserAutoModeWrite(result, deps.log, deps.errorLog);
|
|
1299
|
+
}
|
|
1300
|
+
// No flags.
|
|
1301
|
+
if (!deps.isTTY || !deps.promptLine) {
|
|
1302
|
+
const entries = buildUserAutoModeEntries();
|
|
1303
|
+
deps.log(`${userSettingsPath(deps.homeDir)} was not changed (no interactive terminal). It would add, to autoMode.allow and autoMode.environment:`);
|
|
1304
|
+
logList(deps.log, [...entries.allow, ...entries.environment]);
|
|
1305
|
+
deps.log("Run this command in a terminal to be asked, or let /drive-epic-run ask in your session.");
|
|
1306
|
+
return 0;
|
|
1307
|
+
}
|
|
1308
|
+
const { body, prompt } = splitConsentPrompt();
|
|
1309
|
+
deps.log(body);
|
|
1310
|
+
let answer = "";
|
|
1311
|
+
try {
|
|
1312
|
+
answer = String((await deps.promptLine(prompt)) ?? "").trim().toLowerCase();
|
|
1313
|
+
}
|
|
1314
|
+
catch {
|
|
1315
|
+
answer = "";
|
|
1316
|
+
}
|
|
1317
|
+
if (answer !== "y" && answer !== "yes") {
|
|
1318
|
+
deps.log(`${userSettingsPath(deps.homeDir)} was not changed.`);
|
|
1319
|
+
return 0;
|
|
1320
|
+
}
|
|
1321
|
+
const result = await applyUserAutoModeEntries({ cwd: deps.cwd, homeDir: deps.homeDir }, deps);
|
|
1322
|
+
return reportUserAutoModeWrite(result, deps.log, deps.errorLog);
|
|
1323
|
+
}
|
|
1324
|
+
catch {
|
|
1325
|
+
deps.errorLog("bridge-permissions: unexpected error; nothing further was changed.");
|
|
1326
|
+
return 1;
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
/** One-line prompt on stderr; EOF resolves as an empty answer so it can never hang. */
|
|
1330
|
+
function promptLineOnStderr(promptText) {
|
|
1331
|
+
return new Promise((resolve) => {
|
|
1332
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stderr });
|
|
1333
|
+
let answered = false;
|
|
1334
|
+
rl.on("close", () => {
|
|
1335
|
+
if (!answered)
|
|
1336
|
+
resolve("");
|
|
1337
|
+
});
|
|
1338
|
+
rl.question(promptText, (answer) => {
|
|
1339
|
+
answered = true;
|
|
1340
|
+
rl.close();
|
|
1341
|
+
resolve(answer);
|
|
1342
|
+
});
|
|
1343
|
+
});
|
|
1344
|
+
}
|
|
1345
|
+
/** Production entry for the `bridge-permissions` subcommand: real filesystem, real home. */
|
|
1346
|
+
export async function runBridgePermissionsCliFromProcess(argv) {
|
|
1347
|
+
const isTTY = Boolean(process.stdin.isTTY && process.stderr.isTTY);
|
|
1348
|
+
return runBridgePermissionsCli(argv, {
|
|
1349
|
+
// The manifest belongs to the project in the current directory, so there is no
|
|
1350
|
+
// Git-root lookup: help says "run from the project root".
|
|
1351
|
+
cwd: process.cwd(),
|
|
1352
|
+
homeDir: os.homedir(),
|
|
1353
|
+
env: process.env,
|
|
1354
|
+
isTTY,
|
|
1355
|
+
promptLine: isTTY ? promptLineOnStderr : undefined,
|
|
1356
|
+
readFile: (filePath) => fs.readFile(filePath, "utf-8"),
|
|
1357
|
+
writeFile: (filePath, data) => fs.writeFile(filePath, data, { encoding: "utf-8" }),
|
|
1358
|
+
mkdir: (dirPath, options) => fs.mkdir(dirPath, options),
|
|
1359
|
+
log: (message) => console.log(message),
|
|
1360
|
+
errorLog: (message) => console.error(message),
|
|
1361
|
+
});
|
|
1362
|
+
}
|