barndog 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/README.md +2 -0
  2. package/dist/cli.js +2 -2
  3. package/dist/commands/add.js +4 -2
  4. package/dist/commands/asset-install.js +7 -3
  5. package/dist/commands/asset-share.js +15 -3
  6. package/dist/commands/capabilities.js +23 -5
  7. package/dist/commands/collaboration.js +9 -3
  8. package/dist/commands/harnesses.js +1 -1
  9. package/dist/commands/hooks.js +16 -1
  10. package/dist/commands/mcp-env.js +39 -12
  11. package/dist/commands/mcp.js +13 -3
  12. package/dist/commands/origin.js +7 -4
  13. package/dist/commands/sets.js +3 -3
  14. package/dist/commands/status.js +3 -1
  15. package/dist/commands/sync.js +1 -1
  16. package/dist/commands/uninstall.js +4 -2
  17. package/dist/commands/workspace.js +19 -0
  18. package/dist/config/state.js +19 -0
  19. package/dist/errors.js +4 -0
  20. package/dist/features/adoption/adopt.js +18 -1
  21. package/dist/features/adoption/scan.js +5 -0
  22. package/dist/features/asset-kinds/url-query.js +26 -0
  23. package/dist/features/doctor/check.js +1 -0
  24. package/dist/features/doctor/grouped.js +127 -0
  25. package/dist/features/doctor/index.js +18 -33
  26. package/dist/features/environment/events.js +10 -0
  27. package/dist/features/environment/kept-value-events.js +40 -0
  28. package/dist/features/environment/mcp-records.js +57 -0
  29. package/dist/features/environment/stage.js +12 -8
  30. package/dist/features/environment/sync.js +11 -1
  31. package/dist/features/environment/writes.js +25 -27
  32. package/dist/features/hooks/compatibility.js +37 -17
  33. package/dist/features/hooks/test.js +2 -2
  34. package/dist/features/inventory/hook-matrix.js +14 -0
  35. package/dist/features/inventory/hook-readiness.js +47 -0
  36. package/dist/features/inventory/index.js +9 -1
  37. package/dist/features/inventory/show.js +10 -2
  38. package/dist/features/mcps/import.js +40 -8
  39. package/dist/features/mcps/index.js +23 -10
  40. package/dist/features/mcps/values.js +71 -21
  41. package/dist/features/migration/move.js +4 -0
  42. package/dist/features/migration/promote.js +1 -0
  43. package/dist/features/native/adapters.js +4 -44
  44. package/dist/features/native/requirements.js +98 -0
  45. package/dist/features/secrets/index.js +4 -3
  46. package/dist/features/uninstall/index.js +1 -1
  47. package/dist/features/workspace/cloud-source.js +38 -4
  48. package/dist/program.js +4 -1
  49. package/dist/ui/add-hook.js +2 -2
  50. package/dist/ui/add-mcp.js +37 -5
  51. package/dist/ui/add-skills.js +2 -2
  52. package/dist/ui/adopt.js +80 -14
  53. package/dist/ui/assets.js +15 -2
  54. package/dist/ui/collaboration.js +5 -3
  55. package/dist/ui/format.js +81 -2
  56. package/dist/ui/hook-compatibility.js +176 -22
  57. package/dist/ui/mcp.js +98 -19
  58. package/dist/ui/move.js +1 -1
  59. package/dist/ui/onboarding.js +11 -2
  60. package/dist/ui/output.js +96 -42
  61. package/dist/ui/package-details.js +7 -2
  62. package/dist/ui/pick.js +20 -6
  63. package/dist/ui/selection.js +1 -1
  64. package/dist/ui/uninstall.js +14 -2
  65. package/dist/ui/variable-problems.js +23 -0
  66. package/package.json +2 -2
@@ -2,7 +2,7 @@ import { hookSchema } from "@barndog-ai/api-contract";
2
2
  import { readSettings } from "../../config/settings.js";
3
3
  import { selectAsset } from "../server/catalog.js";
4
4
  import { repoForOrigin, selectRepoAsset } from "../workspace/assets.js";
5
- import { runtimeRequirements } from "../native/adapters.js";
5
+ import { missingRequirements, skipReason, } from "../native/requirements.js";
6
6
  import { hookHarnesses } from "../../harnesses/registry.js";
7
7
  import { hookMetadata } from "@barndog-ai/api-contract/package";
8
8
  /**
@@ -12,32 +12,37 @@ import { hookMetadata } from "@barndog-ai/api-contract/package";
12
12
  export async function hookCompatibility(name, origin, runtime) {
13
13
  const { home } = runtime;
14
14
  const settings = await readSettings(home);
15
- const managed = (result) => settings
15
+ const repo = settings && repoForOrigin(settings, runtime, origin);
16
+ if (repo) {
17
+ return repoHookCompatibility(selectRepoAsset(await repo.model(), "hook", name, origin), runtime);
18
+ }
19
+ const selected = await selectAsset("hook", name, origin, runtime);
20
+ return cloudHookCompatibility(hookSchema.parse(selected.asset), runtime);
21
+ }
22
+ /** Only the tools this machine has BarnDog manage, once it is set up. */
23
+ async function managed(result, runtime) {
24
+ const settings = await readSettings(runtime.home);
25
+ return settings
16
26
  ? {
17
27
  ...result,
18
28
  compatibility: result.compatibility.filter((row) => settings.harnesses.includes(row.harness)),
19
29
  }
20
30
  : result;
21
- const repo = settings && repoForOrigin(settings, runtime, origin);
22
- if (repo) {
23
- return managed(await repoHookCompatibility(selectRepoAsset(await repo.model(), "hook", name, origin), runtime));
24
- }
25
- const selected = await selectAsset("hook", name, origin, runtime);
26
- const asset = hookSchema.parse(selected.asset);
31
+ }
32
+ /** What a Cloud Hook declares for each coding tool, and what this machine lacks. */
33
+ export async function cloudHookCompatibility(asset, runtime) {
27
34
  return managed({
28
35
  version: asset.version.number,
29
36
  compatibility: await Promise.all(asset.compatibility.map(async (row) => ({
30
37
  ...row,
31
- runtimeIssue: row.implementation
32
- ? await runtimeRequirements(row.harness, row.implementation.requirements, runtime)
33
- : "No implementation for this harness",
38
+ ...(await readiness(row.harness, row.implementation, runtime)),
34
39
  }))),
35
- });
40
+ }, runtime);
36
41
  }
37
42
  /** What a Git or local workspace Hook declares for each coding tool, and what this machine lacks. */
38
- async function repoHookCompatibility(asset, runtime) {
43
+ export async function repoHookCompatibility(asset, runtime) {
39
44
  const manifest = hookMetadata(asset.entries);
40
- return {
45
+ return managed({
41
46
  version: asset.publishedVersion,
42
47
  compatibility: await Promise.all(hookHarnesses.map(async (harness) => {
43
48
  const implementation = manifest.implementations[harness] ?? null;
@@ -46,10 +51,25 @@ async function repoHookCompatibility(asset, runtime) {
46
51
  status: implementation?.compatibility ?? "unsupported",
47
52
  declared: true,
48
53
  implementation,
49
- runtimeIssue: implementation
50
- ? await runtimeRequirements(harness, implementation.requirements, runtime)
51
- : "No implementation for this harness",
54
+ ...(await readiness(harness, implementation, runtime)),
52
55
  };
53
56
  })),
57
+ }, runtime);
58
+ }
59
+ /** What an implementation lacks on this machine, typed and in words. */
60
+ async function readiness(harness, implementation, runtime) {
61
+ if (!implementation) {
62
+ return {
63
+ missingRequirements: [],
64
+ runtimeIssue: skipReason(harness, { type: "no-implementation" }),
65
+ };
66
+ }
67
+ const missing = await missingRequirements(harness, implementation.requirements, runtime);
68
+ const [first] = missing;
69
+ return {
70
+ missingRequirements: missing,
71
+ runtimeIssue: first
72
+ ? skipReason(harness, { type: "missing-requirement", requirement: first })
73
+ : null,
54
74
  };
55
75
  }
@@ -7,7 +7,7 @@ import { parse } from "yaml";
7
7
  import { hookManifestSchema, hookSchema } from "@barndog-ai/api-contract";
8
8
  import { selectAsset } from "../server/catalog.js";
9
9
  import { cachedPackage } from "../workspace/packages.js";
10
- import { runtimeRequirements } from "../native/adapters.js";
10
+ import { requirementFix, requirementMessage, runtimeRequirements, } from "../native/requirements.js";
11
11
  import { hookInvocation } from "../native/hook-shell.js";
12
12
  import { readSettings } from "../../config/settings.js";
13
13
  import { repoForOrigin, selectRepoAsset } from "../workspace/assets.js";
@@ -45,7 +45,7 @@ export async function testHook(name, origin, options, runtime) {
45
45
  }
46
46
  const unmet = await runtimeRequirements(options.harness, implementation.requirements, runtime);
47
47
  if (unmet) {
48
- throw new CliError(unmet);
48
+ throw new CliError(`${name} ${requirementMessage(options.harness, unmet)}.`, requirementFix(options.harness, unmet));
49
49
  }
50
50
  const event = options.event ??
51
51
  (Object.keys(implementation.events).length === 1
@@ -0,0 +1,14 @@
1
+ import { hookCompatibility } from "../hooks/compatibility.js";
2
+ import { inventory } from "./index.js";
3
+ /** Every selected Hook's support and readiness per tool, in the inventory's order. */
4
+ export async function allHookCompatibility(runtime) {
5
+ const { items } = await inventory(runtime);
6
+ // Leftovers have no origin to look them up by: they are no longer selected.
7
+ const hooks = items.filter((item) => item.kind === "hook" && item.origin !== "—");
8
+ return Promise.all(hooks.map(async (item) => ({
9
+ name: item.installedName,
10
+ origin: item.origin,
11
+ compatibility: (await hookCompatibility(item.name, item.origin, runtime))
12
+ .compatibility,
13
+ })));
14
+ }
@@ -0,0 +1,47 @@
1
+ import { join } from "node:path";
2
+ import { parseDocument } from "yaml";
3
+ import { hookManifestSchema } from "@barndog-ai/api-contract";
4
+ import { readIfExists } from "../../shared/files.js";
5
+ import { isHookHarness } from "../../harnesses/registry.js";
6
+ import { hookIssue } from "../native/adapters.js";
7
+ /**
8
+ * Why sync installs a selected Hook in none of the selected tools, read from
9
+ * its materialized package: `no-version` when it has no implementation for any
10
+ * of them, `cant-run` when it has one but this machine lacks what it needs.
11
+ * Null for anything else: another kind, a disabled or installed Hook, one some
12
+ * tool can run, or a package that isn't there or can't be read.
13
+ */
14
+ export async function hookBlocked(asset, harnesses, runtime) {
15
+ if (asset.kind !== "hook" ||
16
+ !asset.enabled ||
17
+ asset.present ||
18
+ !asset.installed) {
19
+ return null;
20
+ }
21
+ const text = await readIfExists(join(asset.installed.path, "hook.yaml"));
22
+ if (text === null) {
23
+ return null;
24
+ }
25
+ // A hand-edited package is doctor's to report; here it only can't be judged.
26
+ const document = parseDocument(text);
27
+ const parsed = hookManifestSchema.safeParse(document.errors.length ? null : document.toJS());
28
+ if (!parsed.success) {
29
+ return null;
30
+ }
31
+ const skips = [];
32
+ for (const harness of harnesses) {
33
+ const skip = isHookHarness(harness)
34
+ ? await hookIssue(harness, parsed.data, runtime)
35
+ : { type: "no-command-hooks" };
36
+ if (!skip) {
37
+ return null;
38
+ }
39
+ skips.push(skip);
40
+ }
41
+ if (!skips.length) {
42
+ return null;
43
+ }
44
+ return skips.some((skip) => skip.type === "missing-requirement")
45
+ ? "cant-run"
46
+ : "no-version";
47
+ }
@@ -16,6 +16,7 @@ import { installedName, resolveClashes } from "../environment/names.js";
16
16
  import { originLabel, scopes } from "../server/origin.js";
17
17
  import { isAssetKind } from "../asset-kinds/index.js";
18
18
  import { isHarness } from "../../harnesses/registry.js";
19
+ import { hookBlocked } from "./hook-readiness.js";
19
20
  /** Local assets still moving to the shared workspace; none without a local folder. */
20
21
  async function pendingMoves(settings) {
21
22
  const folder = settings.localWorkspace?.path;
@@ -39,7 +40,7 @@ function inventoryState(asset) {
39
40
  return "modified";
40
41
  }
41
42
  if (!asset.present) {
42
- return "pending";
43
+ return asset.blocked ?? "pending";
43
44
  }
44
45
  return asset.outdated ? "outdated" : "installed";
45
46
  }
@@ -168,6 +169,12 @@ export async function inventory(runtime, options = {}) {
168
169
  const entries = nativeOf(asset.id);
169
170
  const tree = installed.find((t) => t.id === asset.id);
170
171
  const present = links.length > 0 || entries.length > 0;
172
+ const blocked = await hookBlocked({
173
+ kind: asset.kind,
174
+ enabled: entry.enabled,
175
+ present,
176
+ installed: tree,
177
+ }, settings.harnesses, runtime);
171
178
  const harnesses = [
172
179
  ...new Set([
173
180
  ...links.map((l) => l.harness),
@@ -189,6 +196,7 @@ export async function inventory(runtime, options = {}) {
189
196
  modified: modified.has(asset.id),
190
197
  present,
191
198
  outdated: Boolean(tree?.versionId && tree.versionId !== asset.publishedVersion),
199
+ blocked,
192
200
  }),
193
201
  harnesses,
194
202
  paths: [
@@ -5,13 +5,14 @@ import { selectAsset } from "../server/catalog.js";
5
5
  import { originLabel } from "../server/origin.js";
6
6
  import { repoForOrigin, selectRepoAsset, showRepoAsset, } from "../workspace/assets.js";
7
7
  import { inventory } from "./index.js";
8
+ import { cloudHookCompatibility, repoHookCompatibility, } from "../hooks/compatibility.js";
8
9
  /** What a Skill or Hook is, where it comes from, and where this machine has it. */
9
10
  export async function showPackage(kind, name, origin, runtime) {
10
11
  const { home } = runtime;
11
12
  const settings = await readSettings(home);
12
13
  const repo = settings && repoForOrigin(settings, runtime, origin);
13
14
  const described = repo
14
- ? repoDetails(selectRepoAsset(await repo.model(), kind, name, origin), kind)
15
+ ? await repoDetails(selectRepoAsset(await repo.model(), kind, name, origin), kind, runtime)
15
16
  : await cloudDetails(kind, name, origin, runtime);
16
17
  const item = (await inventory(runtime)).items.find((entry) => entry.id === described.id);
17
18
  return {
@@ -21,7 +22,7 @@ export async function showPackage(kind, name, origin, runtime) {
21
22
  : null,
22
23
  };
23
24
  }
24
- function repoDetails(asset, kind) {
25
+ async function repoDetails(asset, kind, runtime) {
25
26
  const view = showRepoAsset(asset);
26
27
  const events = view.manifest
27
28
  ? eventsByTool((harness) => view.manifest?.implementations[harness]?.events)
@@ -34,6 +35,12 @@ function repoDetails(asset, kind) {
34
35
  version: view.version,
35
36
  description: view.description,
36
37
  ...(events ? { events } : {}),
38
+ ...(kind === "hook"
39
+ ? {
40
+ compatibility: (await repoHookCompatibility(asset, runtime))
41
+ .compatibility,
42
+ }
43
+ : {}),
37
44
  };
38
45
  }
39
46
  async function cloudDetails(kind, name, origin, runtime) {
@@ -59,6 +66,7 @@ async function cloudDetails(kind, name, origin, runtime) {
59
66
  description: hook.version.description,
60
67
  events: eventsByTool((harness) => hook.compatibility.find((row) => row.harness === harness)
61
68
  ?.implementation?.events),
69
+ compatibility: (await cloudHookCompatibility(hook, runtime)).compatibility,
62
70
  };
63
71
  }
64
72
  /** Event names per tool, leaving out tools the Hook has no implementation for. */
@@ -16,7 +16,7 @@ import { synchronize } from "../environment/sync.js";
16
16
  import { patchNative } from "../native/configuration.js";
17
17
  import { validMcpName, VARIABLE_NAME } from "../../shared/patterns.js";
18
18
  import { assetKinds } from "../asset-kinds/index.js";
19
- import { LOCAL_ORIGIN } from "../workspace/overlay.js";
19
+ import { queryParameters } from "../asset-kinds/url-query.js";
20
20
  import { ensureLocalWorkspace } from "../imports/local.js";
21
21
  import { keptValues, updateKeptValues, withServerValues, } from "../environment/kept-values.js";
22
22
  import { localRepoSource } from "../workspace/repo-source.js";
@@ -179,9 +179,6 @@ export function convertServer({ name, server }, description, oauth) {
179
179
  }
180
180
  // Claude Code writes `oauth` and Cursor writes `auth` for servers that sign in natively.
181
181
  const signsIn = oauth || !!server.oauth || !!server.auth;
182
- if (signsIn) {
183
- notes.push(`Sign in once it's added: barndog mcp auth ${name} --origin ${LOCAL_ORIGIN} --harness <tool>`);
184
- }
185
182
  candidate = {
186
183
  ...common,
187
184
  transport: "http",
@@ -219,7 +216,21 @@ export function convertServer({ name, server }, description, oauth) {
219
216
  if (!parsed.success) {
220
217
  throw new CliError(`${name} can't be added: ${parsed.error.issues.map((issue) => `${issue.path.join(".") || "server"}: ${issue.message}`).join("; ")}`);
221
218
  }
222
- return { name, definition: parsed.data, notes, literals };
219
+ return {
220
+ name,
221
+ definition: parsed.data,
222
+ notes: [...notes, ...urlQueryNotes(parsed.data)],
223
+ literals,
224
+ };
225
+ }
226
+ /** What to check before sharing a server whose URL has a query string. */
227
+ function urlQueryNotes(definition) {
228
+ const parameters = queryParameters(definition);
229
+ return parameters.length
230
+ ? [
231
+ `Its URL keeps the query parameters ${parameters.join(", ")} as written. Check none of them holds a secret before you share it.`,
232
+ ]
233
+ : [];
223
234
  }
224
235
  /** How an imported HTTP server authenticates. */
225
236
  function httpAuthentication(signsIn, headers) {
@@ -316,6 +327,25 @@ export async function localMcpNamed(name, runtime) {
316
327
  .map((file) => file.slice(0, -".yaml".length));
317
328
  return (names.find((other) => other.toLowerCase() === name.toLowerCase()) ?? null);
318
329
  }
330
+ const toolList = new Intl.ListFormat("en", { type: "conjunction" });
331
+ /**
332
+ * The servers that sign in through each coding tool, which `mcp auth` starts
333
+ * once per tool. Read by asset ID from `state.json` after the sync, since a
334
+ * clash rename changes the name a tool has the server under.
335
+ */
336
+ export async function signInTools(servers, runtime) {
337
+ const { native } = await readState(runtime.home);
338
+ return servers
339
+ .filter(({ definition }) => definition.authentication === "native-oauth")
340
+ .map(({ id, name }) => ({
341
+ server: name,
342
+ harnesses: [
343
+ ...new Set(native
344
+ .filter((entry) => entry.kind === "mcp" && entry.id === id)
345
+ .map((entry) => entry.harness)),
346
+ ],
347
+ }));
348
+ }
319
349
  /**
320
350
  * Writes converted MCP servers and synchronizes them. Existing local workspace
321
351
  * names are always rejected; replace permits backing up and removing
@@ -338,7 +368,7 @@ export async function addMcpServers(servers, options, runtime) {
338
368
  }
339
369
  const conflicts = await conflictingEntries(name, runtime);
340
370
  if (conflicts.length && !options.replace) {
341
- throw new CliError(`${name} is already configured in ${conflicts.map((c) => c.file).join(" and ")}. Replacing it is required for BarnDog to manage it.`, `barndog mcp add ${name} --replace`);
371
+ throw new CliError(`${name} is already configured in ${toolList.format(conflicts.map((c) => adapters[c.harness].name))}. Replacing it is required for BarnDog to manage it.`, `barndog mcp add ${name} --replace`);
342
372
  }
343
373
  }
344
374
  // One backup per settings file, taken before its first change, so a file
@@ -371,11 +401,12 @@ export async function addMcpServers(servers, options, runtime) {
371
401
  }
372
402
  // Saved before the sync, which writes them into the tools' settings.
373
403
  const model = await localRepoSource(workspace, runtime).model();
374
- await keepLiteralValues(servers.map((server) => ({
404
+ const saved = servers.map((server) => ({
375
405
  ...server,
376
406
  id: defined(model.assets.find((asset) => asset.kind === "mcp" &&
377
407
  asset.path === `org/mcp/${server.name}.yaml`)?.id, `The local folder has no ${server.name} after writing it`),
378
- })), options.keep, runtime);
408
+ }));
409
+ await keepLiteralValues(saved, options.keep, runtime);
379
410
  const events = await synchronize(undefined, runtime, undefined, {
380
411
  prompt: false,
381
412
  });
@@ -384,6 +415,7 @@ export async function addMcpServers(servers, options, runtime) {
384
415
  notes: servers.flatMap((s) => s.notes.map((text) => ({ server: s.name, text }))),
385
416
  values: servers.flatMap((s) => literalValues(s.name, s.literals, options.keep)),
386
417
  backups,
418
+ signIn: await signInTools(saved, runtime),
387
419
  events,
388
420
  };
389
421
  });
@@ -14,26 +14,32 @@ import { readSettings } from "../../config/settings.js";
14
14
  import { repoForOrigin, selectRepoAsset, showRepoAsset, } from "../workspace/assets.js";
15
15
  import { gitVersions } from "../collaboration/workspace-git.js";
16
16
  import { adapters } from "../../harnesses/registry.js";
17
- import { missingValues } from "../secrets/index.js";
17
+ import { missingValues, readsFrom } from "../secrets/index.js";
18
18
  import { keptValues } from "../environment/kept-values.js";
19
19
  import { patchNative } from "../native/configuration.js";
20
20
  import { fileText, inputJson } from "../../shared/files.js";
21
+ import { mayShareUrlQueries, } from "../asset-kinds/url-query.js";
21
22
  import { defined } from "../../shared/lookup.js";
22
23
  import { CliError, friendlyMessage } from "../../errors.js";
23
24
  import { barndogPaths } from "../../config/paths.js";
24
25
  import { apiClient } from "../auth/transport.js";
25
- async function readDefinition(file, runtime) {
26
+ /** The definition in `input.file`, once the developer agrees to share any URL query it has. */
27
+ async function readDefinition(input, runtime) {
28
+ const { file } = input;
26
29
  const result = mcpDefinitionSchema.safeParse(await inputJson(localPath(file, runtime)));
27
30
  if (!result.success) {
28
31
  throw new CliError(`${file} isn't an MCP definition: ${friendlyMessage(result.error)}.`, "Check the fields against the MCP definition format at barndog.dev/docs", { cause: result.error });
29
32
  }
33
+ if (!(await mayShareUrlQueries([{ name: input.name, definition: result.data }], input.confirmUrlQuery))) {
34
+ throw new CliError("Nothing was published.");
35
+ }
30
36
  return result.data;
31
37
  }
32
38
  /** Publishes a new MCP configuration in a scope. */
33
39
  export async function createMcp(input, runtime) {
34
40
  const conn = await connection(runtime);
35
41
  const scope = await selectOrigin(input.origin, conn, runtime);
36
- const definition = await readDefinition(input.file, runtime);
42
+ const definition = await readDefinition(input, runtime);
37
43
  return parsed(mcpSchema, apiClient(conn.server, runtime).POST("/v1/mcps", {
38
44
  body: {
39
45
  name: input.name,
@@ -47,7 +53,7 @@ export async function createMcp(input, runtime) {
47
53
  export async function updateMcp(input, runtime) {
48
54
  const conn = await connection(runtime);
49
55
  await selectOrigin(input.origin, conn, runtime);
50
- const definition = await readDefinition(input.file, runtime);
56
+ const definition = await readDefinition(input, runtime);
51
57
  const selected = await selectAsset("mcp", input.name, input.origin, runtime);
52
58
  return parsed(mcpSchema, apiClient(conn.server, runtime).POST("/v1/mcps/{id}/versions", {
53
59
  params: { path: { id: selected.reference.id } },
@@ -63,12 +69,19 @@ export async function showMcp(name, origin, runtime) {
63
69
  const { home } = runtime;
64
70
  const settings = await readSettings(home);
65
71
  const repo = settings && repoForOrigin(settings, runtime, origin);
66
- const withUnset = async (asset, definition) => ({
67
- ...asset,
68
- unsetVariables: definition
69
- ? missingValues(definition, (await keptValues(runtime)).servers[asset.id] ?? {}, runtime.env).map((missing) => missing.variable)
70
- : [],
71
- });
72
+ const withUnset = async (asset, definition) => {
73
+ const values = (await keptValues(runtime)).servers[asset.id] ?? {};
74
+ return {
75
+ ...asset,
76
+ keptVariables: Object.entries(values).map(([variable, kept]) => {
77
+ const from = readsFrom(kept);
78
+ return from ? { variable, from } : { variable };
79
+ }),
80
+ unsetVariables: definition
81
+ ? missingValues(definition, values, runtime.env).map((missing) => missing.variable)
82
+ : [],
83
+ };
84
+ };
72
85
  if (repo) {
73
86
  const view = showRepoAsset(selectRepoAsset(await repo.model(), "mcp", name, origin));
74
87
  return withUnset(view, view.definition);
@@ -1,15 +1,24 @@
1
1
  import { readState } from "../../config/state.js";
2
- import { CliError } from "../../errors.js";
2
+ import { CliError, friendlyMessage } from "../../errors.js";
3
+ import { LoginRequiredError } from "../auth/transport.js";
3
4
  import { keptValues, updateKeptValues, withServerValues, } from "../environment/kept-values.js";
4
5
  import { synchronize } from "../environment/sync.js";
5
- import { inventory } from "../inventory/index.js";
6
6
  import { assertVariableName, literalOf, readsFrom, } from "../secrets/index.js";
7
+ /**
8
+ * Every MCP server selected on this machine, disabled ones included, as the
9
+ * last sync recorded them.
10
+ */
11
+ export async function recordedMcpServers(runtime) {
12
+ const { mcpServers } = await readState(runtime.home);
13
+ if (!mcpServers) {
14
+ throw new CliError("BarnDog hasn't recorded this machine's MCP servers yet.", "barndog sync");
15
+ }
16
+ return mcpServers;
17
+ }
7
18
  /** The MCP server called `name` on this machine: its installed name, published name or asset ID. */
8
19
  async function findServer(name, runtime) {
9
- const { items } = await inventory(runtime, { checkVariables: true });
10
- const matches = items.filter((item) => item.kind === "mcp" &&
11
- [item.installedName, item.name, item.id].includes(name));
12
- const exact = matches.filter((item) => item.installedName === name);
20
+ const matches = (await recordedMcpServers(runtime)).filter((server) => [server.installedName, server.name, server.id].includes(name));
21
+ const exact = matches.filter((server) => server.installedName === name);
13
22
  const [only] = exact.length === 1 ? exact : matches;
14
23
  if (!only || matches.length > 1) {
15
24
  throw new CliError(only
@@ -20,14 +29,49 @@ async function findServer(name, runtime) {
20
29
  }
21
30
  function assertUses(server, variable) {
22
31
  assertVariableName(variable);
23
- if (server.variables && !server.variables.includes(variable)) {
32
+ if (!server.variables.includes(variable)) {
24
33
  throw new CliError(server.variables.length
25
34
  ? `${server.installedName} doesn't use ${variable}. It uses ${server.variables.join(", ")}.`
26
35
  : `${server.installedName} doesn't use any variables.`, `barndog mcp show ${server.installedName}`);
27
36
  }
28
37
  }
29
- async function changed(file, runtime) {
30
- return { file, events: await synchronize(undefined, runtime) };
38
+ /**
39
+ * Writes a changed value into the tools, unless the server is disabled. The
40
+ * value is already kept, so a sync that fails says why and leaves it for the
41
+ * next one instead of failing the change.
42
+ */
43
+ async function deliver(file, server, runtime) {
44
+ if (server && !server.enabled) {
45
+ return { file, events: [], notWritten: { because: "disabled" } };
46
+ }
47
+ try {
48
+ return { file, events: await synchronize(undefined, runtime) };
49
+ }
50
+ catch (error) {
51
+ if (error instanceof CliError) {
52
+ return {
53
+ file,
54
+ events: [],
55
+ notWritten: {
56
+ because: "sync-failed",
57
+ message: error.message,
58
+ ...(error.hint ? { fix: error.hint } : {}),
59
+ },
60
+ };
61
+ }
62
+ if (error instanceof LoginRequiredError) {
63
+ return {
64
+ file,
65
+ events: [],
66
+ notWritten: {
67
+ because: "sync-failed",
68
+ message: friendlyMessage(error),
69
+ fix: "barndog login",
70
+ },
71
+ };
72
+ }
73
+ throw error;
74
+ }
31
75
  }
32
76
  /** Keeps a value, or another variable to read, for one server's variable and writes it into the coding tools. */
33
77
  export async function keepMcpValue(target, kept, runtime) {
@@ -45,7 +89,7 @@ export async function keepMcpValue(target, kept, runtime) {
45
89
  }
46
90
  }
47
91
  const file = await updateKeptValues((secrets) => withServerValues(secrets, server.id, { [target.variable]: kept }), runtime);
48
- return changed(file, runtime);
92
+ return deliver(file, server, runtime);
49
93
  }
50
94
  /** Gives a value kept before values belonged to servers to one server. */
51
95
  export async function assignKeptValue(target, runtime) {
@@ -62,20 +106,22 @@ export async function assignKeptValue(target, runtime) {
62
106
  [target.variable]: { value },
63
107
  });
64
108
  }, runtime);
65
- return changed(file, runtime);
109
+ return deliver(file, server, runtime);
66
110
  }
67
111
  /** Forgets one server's kept value; the tools read the variable again. */
68
112
  export async function forgetMcpValue(target, runtime) {
69
113
  const secrets = await keptValues(runtime);
114
+ const { mcpServers = [] } = await readState(runtime.home);
70
115
  // A server that's gone is still named by the asset ID `mcp env list` shows.
71
- const id = Object.hasOwn(secrets.servers, target.server)
72
- ? target.server
73
- : (await findServer(target.server, runtime)).id;
116
+ const server = Object.hasOwn(secrets.servers, target.server)
117
+ ? mcpServers.find((record) => record.id === target.server)
118
+ : await findServer(target.server, runtime);
119
+ const id = server?.id ?? target.server;
74
120
  if (!secrets.servers[id]?.[target.variable]) {
75
121
  throw new CliError(`BarnDog doesn't keep ${target.variable} for ${target.server}.`, "barndog mcp env list");
76
122
  }
77
123
  const file = await updateKeptValues((latest) => withServerValues(latest, id, { [target.variable]: null }), runtime);
78
- return changed(file, runtime);
124
+ return deliver(file, server, runtime);
79
125
  }
80
126
  /** Forgets a value kept before values belonged to servers. */
81
127
  export async function forgetUnassignedValue(variable, runtime) {
@@ -87,30 +133,34 @@ export async function forgetUnassignedValue(variable, runtime) {
87
133
  delete unassigned[variable];
88
134
  return { ...secrets, unassigned };
89
135
  }, runtime);
90
- return changed(file, runtime);
136
+ return deliver(file, undefined, runtime);
91
137
  }
92
138
  /** Every kept value, by server, with the settings files sync wrote it into. */
93
139
  export async function listKeptValues(runtime) {
94
140
  const secrets = await keptValues(runtime);
95
- const { native } = await readState(runtime.home);
96
- const servers = native.filter((entry) => entry.kind === "mcp");
141
+ const { native, mcpServers = [] } = await readState(runtime.home);
142
+ const entries = native.filter((entry) => entry.kind === "mcp");
97
143
  const assigned = Object.entries(secrets.servers).flatMap(([id, values]) => {
98
- const entries = servers.filter((entry) => entry.id === id);
99
- const name = entries[0]?.key ?? id;
144
+ const record = mcpServers.find((server) => server.id === id);
145
+ const written = entries.filter((entry) => entry.id === id);
146
+ // A server that's gone has only its asset ID left.
147
+ const name = record?.installedName ?? written[0]?.key ?? id;
100
148
  return Object.entries(values).map(([variable, kept]) => {
101
149
  const from = readsFrom(kept);
102
150
  return {
103
151
  server: name,
104
152
  assetId: id,
153
+ enabled: record?.enabled ?? null,
105
154
  variable,
106
155
  ...(from ? { from } : {}),
107
- writtenTo: [...new Set(entries.map((entry) => entry.file))],
156
+ writtenTo: [...new Set(written.map((entry) => entry.file))],
108
157
  };
109
158
  });
110
159
  });
111
160
  const unassigned = Object.keys(secrets.unassigned).map((variable) => ({
112
161
  server: null,
113
162
  assetId: null,
163
+ enabled: null,
114
164
  variable,
115
165
  writtenTo: [],
116
166
  }));
@@ -1,3 +1,4 @@
1
+ import { mayShareUrlQueries, } from "../asset-kinds/url-query.js";
1
2
  import { randomBytes } from "node:crypto";
2
3
  import { basename, join } from "node:path";
3
4
  import { rm } from "node:fs/promises";
@@ -385,6 +386,9 @@ export function moveLocalAssets(options, runtime) {
385
386
  throw new CliError("--as renames one asset, so name exactly one.", "barndog local move <name> --as <new-name>");
386
387
  }
387
388
  const { items, invalid } = await candidates(overlay, options.names, options.as, options.kind);
389
+ if (!(await mayShareUrlQueries(items, options.confirmUrlQuery))) {
390
+ throw new CliError("Nothing was moved.");
391
+ }
388
392
  // Only this machine's own workspace: a one-off destination isn't installed here.
389
393
  const sharedIds = new Map();
390
394
  const moveItems = async () => {
@@ -16,6 +16,7 @@ export async function promote(input, runtime) {
16
16
  keepLocal: true,
17
17
  to: input.target,
18
18
  as: input.newName === input.name ? undefined : input.newName,
19
+ confirmUrlQuery: input.confirmUrlQuery,
19
20
  }, runtime),
20
21
  };
21
22
  }