barndog 0.2.1 → 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 (92) hide show
  1. package/README.md +21 -0
  2. package/dist/cli.js +29 -3
  3. package/dist/commands/add.js +4 -2
  4. package/dist/commands/asset-install.js +7 -3
  5. package/dist/commands/asset-list.js +5 -1
  6. package/dist/commands/asset-share.js +15 -3
  7. package/dist/commands/capabilities.js +23 -5
  8. package/dist/commands/collaboration.js +9 -3
  9. package/dist/commands/harnesses.js +33 -3
  10. package/dist/commands/hooks.js +16 -1
  11. package/dist/commands/mcp-env.js +159 -43
  12. package/dist/commands/mcp.js +13 -3
  13. package/dist/commands/origin.js +7 -4
  14. package/dist/commands/sets.js +16 -5
  15. package/dist/commands/status.js +3 -1
  16. package/dist/commands/sync.js +1 -1
  17. package/dist/commands/uninstall.js +64 -0
  18. package/dist/commands/workspace.js +19 -0
  19. package/dist/config/paths.js +2 -0
  20. package/dist/config/state.js +19 -0
  21. package/dist/entry/update-check.js +15 -0
  22. package/dist/errors.js +4 -0
  23. package/dist/features/adoption/adopt.js +47 -29
  24. package/dist/features/adoption/scan.js +5 -0
  25. package/dist/features/asset-kinds/url-query.js +26 -0
  26. package/dist/features/auth/session.js +22 -0
  27. package/dist/features/collaboration/workspace-store.js +2 -1
  28. package/dist/features/doctor/check.js +1 -0
  29. package/dist/features/doctor/grouped.js +127 -0
  30. package/dist/features/doctor/index.js +29 -27
  31. package/dist/features/environment/events.js +18 -1
  32. package/dist/features/environment/kept-value-events.js +40 -0
  33. package/dist/features/environment/kept-values.js +68 -0
  34. package/dist/features/environment/mcp-records.js +57 -0
  35. package/dist/features/environment/moves.js +17 -1
  36. package/dist/features/environment/stage.js +32 -28
  37. package/dist/features/environment/sync.js +13 -3
  38. package/dist/features/environment/writes.js +25 -27
  39. package/dist/features/hooks/compatibility.js +37 -17
  40. package/dist/features/hooks/test.js +2 -2
  41. package/dist/features/inventory/hook-matrix.js +14 -0
  42. package/dist/features/inventory/hook-readiness.js +47 -0
  43. package/dist/features/inventory/index.js +54 -3
  44. package/dist/features/inventory/show.js +10 -2
  45. package/dist/features/mcps/import.js +74 -21
  46. package/dist/features/mcps/index.js +28 -5
  47. package/dist/features/mcps/values.js +161 -23
  48. package/dist/features/migration/migrate.js +15 -0
  49. package/dist/features/migration/move.js +38 -9
  50. package/dist/features/migration/promote.js +1 -0
  51. package/dist/features/native/adapters.js +5 -46
  52. package/dist/features/native/hook-shell.js +11 -0
  53. package/dist/features/native/requirements.js +98 -0
  54. package/dist/features/secrets/index.js +95 -29
  55. package/dist/features/uninstall/index.js +126 -0
  56. package/dist/features/uninstall/leave.js +152 -0
  57. package/dist/features/uninstall/survey.js +103 -0
  58. package/dist/features/update-check/index.js +108 -0
  59. package/dist/features/workspace/cloud-source.js +38 -4
  60. package/dist/features/workspace/format.js +6 -1
  61. package/dist/harnesses/claude.js +1 -1
  62. package/dist/harnesses/codex.js +46 -22
  63. package/dist/harnesses/cursor.js +1 -1
  64. package/dist/harnesses/opencode.js +1 -1
  65. package/dist/harnesses/read-renamed-variables.js +141 -0
  66. package/dist/harnesses/renamed-variables.js +99 -0
  67. package/dist/platform.js +12 -1
  68. package/dist/program.js +24 -9
  69. package/dist/ui/add-hook.js +2 -2
  70. package/dist/ui/add-mcp.js +54 -16
  71. package/dist/ui/add-skills.js +2 -2
  72. package/dist/ui/adopt.js +90 -19
  73. package/dist/ui/assets.js +15 -2
  74. package/dist/ui/collaboration.js +5 -3
  75. package/dist/ui/connection.js +2 -0
  76. package/dist/ui/format.js +81 -2
  77. package/dist/ui/harness-picker.js +7 -2
  78. package/dist/ui/hook-compatibility.js +176 -22
  79. package/dist/ui/mcp.js +169 -24
  80. package/dist/ui/move.js +3 -2
  81. package/dist/ui/onboarding.js +11 -2
  82. package/dist/ui/output.js +116 -37
  83. package/dist/ui/package-details.js +7 -2
  84. package/dist/ui/pick.js +20 -6
  85. package/dist/ui/secret-input.js +9 -0
  86. package/dist/ui/selection.js +1 -1
  87. package/dist/ui/uninstall.js +186 -0
  88. package/dist/ui/update.js +10 -0
  89. package/dist/ui/variable-problems.js +23 -0
  90. package/dist/version.js +2 -2
  91. package/package.json +2 -2
  92. package/dist/entry/mcp-launch.js +0 -71
@@ -1,33 +1,171 @@
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";
4
+ import { keptValues, updateKeptValues, withServerValues, } from "../environment/kept-values.js";
3
5
  import { synchronize } from "../environment/sync.js";
4
- import { recordedVariables } from "../native/secret-values.js";
5
- import { readSecrets, updateSecrets } from "../secrets/index.js";
6
- /** Keeps `value` for `variable` and writes it into the coding tools. */
7
- export async function keepMcpValue(variable, value, runtime) {
8
- if (!value) {
9
- throw new CliError(`The value for ${variable} is empty, so nothing was kept.`, `barndog mcp env set ${variable}`);
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");
10
15
  }
11
- const file = await updateSecrets({ [variable]: value }, runtime);
12
- return { file, events: await synchronize(undefined, runtime) };
16
+ return mcpServers;
13
17
  }
14
- /** Forgets a kept value; the tools refer to the environment variable again. */
15
- export async function forgetMcpValue(variable, runtime) {
16
- if ((await readSecrets(runtime))[variable] === undefined) {
17
- throw new CliError(`BarnDog doesn't keep a value for ${variable}.`, "barndog mcp env list");
18
+ /** The MCP server called `name` on this machine: its installed name, published name or asset ID. */
19
+ async function findServer(name, runtime) {
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);
22
+ const [only] = exact.length === 1 ? exact : matches;
23
+ if (!only || matches.length > 1) {
24
+ throw new CliError(only
25
+ ? `More than one MCP server is called ${name}.`
26
+ : `There's no MCP server called ${name} on this machine.`, "barndog mcp list");
18
27
  }
19
- const file = await updateSecrets({ [variable]: null }, runtime);
20
- return { file, events: await synchronize(undefined, runtime) };
28
+ return only;
21
29
  }
22
- /** The kept variables, with the settings files sync wrote each into. */
30
+ function assertUses(server, variable) {
31
+ assertVariableName(variable);
32
+ if (!server.variables.includes(variable)) {
33
+ throw new CliError(server.variables.length
34
+ ? `${server.installedName} doesn't use ${variable}. It uses ${server.variables.join(", ")}.`
35
+ : `${server.installedName} doesn't use any variables.`, `barndog mcp show ${server.installedName}`);
36
+ }
37
+ }
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
+ }
75
+ }
76
+ /** Keeps a value, or another variable to read, for one server's variable and writes it into the coding tools. */
77
+ export async function keepMcpValue(target, kept, runtime) {
78
+ const server = await findServer(target.server, runtime);
79
+ assertUses(server, target.variable);
80
+ const fix = `barndog mcp env set ${server.installedName} ${target.variable}`;
81
+ const from = readsFrom(kept);
82
+ if (literalOf(kept) === "") {
83
+ throw new CliError(`The value for ${target.variable} is empty, so nothing was kept.`, fix);
84
+ }
85
+ if (from !== undefined) {
86
+ assertVariableName(from);
87
+ if (from === target.variable) {
88
+ throw new CliError(`${target.variable} already reads ${from} from the environment.`, `barndog mcp env unset ${server.installedName} ${target.variable}`);
89
+ }
90
+ }
91
+ const file = await updateKeptValues((secrets) => withServerValues(secrets, server.id, { [target.variable]: kept }), runtime);
92
+ return deliver(file, server, runtime);
93
+ }
94
+ /** Gives a value kept before values belonged to servers to one server. */
95
+ export async function assignKeptValue(target, runtime) {
96
+ const server = await findServer(target.server, runtime);
97
+ assertUses(server, target.variable);
98
+ const value = (await keptValues(runtime)).unassigned[target.variable];
99
+ if (value === undefined) {
100
+ throw new CliError(`BarnDog has no unassigned value for ${target.variable}.`, `barndog mcp env set ${server.installedName} ${target.variable}`);
101
+ }
102
+ const file = await updateKeptValues((secrets) => {
103
+ const unassigned = { ...secrets.unassigned };
104
+ delete unassigned[target.variable];
105
+ return withServerValues({ ...secrets, unassigned }, server.id, {
106
+ [target.variable]: { value },
107
+ });
108
+ }, runtime);
109
+ return deliver(file, server, runtime);
110
+ }
111
+ /** Forgets one server's kept value; the tools read the variable again. */
112
+ export async function forgetMcpValue(target, runtime) {
113
+ const secrets = await keptValues(runtime);
114
+ const { mcpServers = [] } = await readState(runtime.home);
115
+ // A server that's gone is still named by the asset ID `mcp env list` shows.
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;
120
+ if (!secrets.servers[id]?.[target.variable]) {
121
+ throw new CliError(`BarnDog doesn't keep ${target.variable} for ${target.server}.`, "barndog mcp env list");
122
+ }
123
+ const file = await updateKeptValues((latest) => withServerValues(latest, id, { [target.variable]: null }), runtime);
124
+ return deliver(file, server, runtime);
125
+ }
126
+ /** Forgets a value kept before values belonged to servers. */
127
+ export async function forgetUnassignedValue(variable, runtime) {
128
+ if ((await keptValues(runtime)).unassigned[variable] === undefined) {
129
+ throw new CliError(`BarnDog has no unassigned value for ${variable}.`, "barndog mcp env list");
130
+ }
131
+ const file = await updateKeptValues((secrets) => {
132
+ const unassigned = { ...secrets.unassigned };
133
+ delete unassigned[variable];
134
+ return { ...secrets, unassigned };
135
+ }, runtime);
136
+ return deliver(file, undefined, runtime);
137
+ }
138
+ /** Every kept value, by server, with the settings files sync wrote it into. */
23
139
  export async function listKeptValues(runtime) {
24
- const names = Object.keys(await readSecrets(runtime)).sort();
25
- const { native } = await readState(runtime.home);
26
- return names.map((variable) => ({
140
+ const secrets = await keptValues(runtime);
141
+ const { native, mcpServers = [] } = await readState(runtime.home);
142
+ const entries = native.filter((entry) => entry.kind === "mcp");
143
+ const assigned = Object.entries(secrets.servers).flatMap(([id, values]) => {
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;
148
+ return Object.entries(values).map(([variable, kept]) => {
149
+ const from = readsFrom(kept);
150
+ return {
151
+ server: name,
152
+ assetId: id,
153
+ enabled: record?.enabled ?? null,
154
+ variable,
155
+ ...(from ? { from } : {}),
156
+ writtenTo: [...new Set(written.map((entry) => entry.file))],
157
+ };
158
+ });
159
+ });
160
+ const unassigned = Object.keys(secrets.unassigned).map((variable) => ({
161
+ server: null,
162
+ assetId: null,
163
+ enabled: null,
27
164
  variable,
28
- writtenTo: native
29
- .filter((entry) => entry.kind === "mcp" &&
30
- recordedVariables(entry.value).includes(variable))
31
- .map((entry) => ({ file: entry.file, server: entry.key })),
165
+ writtenTo: [],
32
166
  }));
167
+ const byName = (a, b) => (a.server ?? "").localeCompare(b.server ?? "") ||
168
+ a.variable.localeCompare(b.variable);
169
+ // Values no server has come last.
170
+ return [...assigned.sort(byName), ...unassigned.sort(byName)];
33
171
  }
@@ -11,6 +11,7 @@ import { originLabel, scopes } from "../server/origin.js";
11
11
  import { environmentCatalog } from "../server/catalog.js";
12
12
  import { parsed } from "../server/client.js";
13
13
  import { synchronize } from "../environment/sync.js";
14
+ import { keptValues, updateKeptValues, withMovedServers, } from "../environment/kept-values.js";
14
15
  import { locked } from "../environment/transaction.js";
15
16
  import { openRepoSource } from "../workspace/workspace-source.js";
16
17
  import { connectionContext } from "../auth/session.js";
@@ -89,6 +90,19 @@ function carryNames(next, names = {}, cloudId, kept) {
89
90
  next.localNames = carried;
90
91
  }
91
92
  }
93
+ /** Kept MCP values follow their servers to Cloud; a kept local folder's stay as they are. */
94
+ async function carryKeptValues(cloudId, kept, runtime) {
95
+ const { servers } = await keptValues(runtime);
96
+ const moves = Object.keys(servers).flatMap((from) => {
97
+ const to = kept?.assets.some((asset) => asset.id === from)
98
+ ? undefined
99
+ : cloudId(from);
100
+ return to ? [{ from, to, copy: false }] : [];
101
+ });
102
+ if (moves.length) {
103
+ await updateKeptValues((secrets) => withMovedServers(secrets, moves), runtime);
104
+ }
105
+ }
92
106
  /**
93
107
  * Knowledge is organization-wide. A broken document blocks the move like a
94
108
  * broken asset, so nothing is left behind.
@@ -255,6 +269,7 @@ export async function migrate(options, runtime) {
255
269
  delete next.gitWorkspace;
256
270
  carryNames(next, settings.localNames, cloudId, kept);
257
271
  await saveSettings(next, home);
272
+ await carryKeptValues(cloudId, kept, runtime);
258
273
  // Not the Cloud catalog fetched above: a kept local folder's assets are
259
274
  // selected too, and only the full source (Cloud plus that folder) has them.
260
275
  await synchronize(undefined, runtime, next);
@@ -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";
@@ -14,7 +15,8 @@ import { environmentCatalog } from "../server/catalog.js";
14
15
  import { parsed } from "../server/client.js";
15
16
  import { synchronize } from "../environment/sync.js";
16
17
  import { locked } from "../environment/transaction.js";
17
- import { assetPath, stableId } from "../workspace/format.js";
18
+ import { assetPath, repoAssetId, stableId, } from "../workspace/format.js";
19
+ import { updateKeptValues, withMovedServers, } from "../environment/kept-values.js";
18
20
  import { checkRepositoryUrl, cloneOrigin, git } from "../workspace/git.js";
19
21
  import { connectGitWorkspace, createGitWorkspace, inspectRepository, } from "../workspace/setup.js";
20
22
  import { LoginRequiredError, normalizeServer, apiClient, } from "../auth/transport.js";
@@ -152,7 +154,8 @@ function candidateFiles(item, path) {
152
154
  path: `${path}/${entry.path}`,
153
155
  }));
154
156
  }
155
- async function moveToGit(workspace, items, to, runtime) {
157
+ async function moveToGit(workspace, move, runtime) {
158
+ const { items, to, sharedIds = new Map() } = move;
156
159
  const { home } = runtime;
157
160
  const shared = await gitRepoSource(workspace, runtime).model();
158
161
  const tip = defined(await acceptedCommit(workspace.workspaceId, runtime), "This Git workspace hasn't been fetched yet");
@@ -227,9 +230,15 @@ async function moveToGit(workspace, items, to, runtime) {
227
230
  name: item.name,
228
231
  status: "moved",
229
232
  })));
233
+ for (const { item } of moving) {
234
+ if (item.kind === "mcp") {
235
+ sharedIds.set(item.id, repoAssetId(workspace.workspaceId, "mcp", scope, item.name));
236
+ }
237
+ }
230
238
  return result;
231
239
  }
232
- async function moveToCloud(conn, items, to, runtime) {
240
+ async function moveToCloud(conn, move, runtime) {
241
+ const { items, to, sharedIds = new Map() } = move;
233
242
  const scope = await selectOrigin(to ?? "me", conn, runtime);
234
243
  if (!scope.canPublish) {
235
244
  throw new CliError(`You can't publish to ${originLabel(scope)}.`, "barndog local move --to me");
@@ -284,7 +293,7 @@ async function moveToCloud(conn, items, to, runtime) {
284
293
  definition: item.definition,
285
294
  };
286
295
  if (asset.kind === "mcp") {
287
- await publishMcp(asset, scope, conn, runtime);
296
+ sharedIds.set(item.id, (await publishMcp(asset, scope, conn, runtime)).id);
288
297
  }
289
298
  else {
290
299
  await publishPackage({ ...asset, kind: asset.kind }, scope, conn, runtime);
@@ -308,6 +317,18 @@ async function moveToCloud(conn, items, to, runtime) {
308
317
  direct: true,
309
318
  };
310
319
  }
320
+ /**
321
+ * A shared copy that sits beside its local server starts with the local one's
322
+ * kept values; after that each keeps its own. A full move carries them over
323
+ * when sync finishes it (`environment/moves.ts`).
324
+ */
325
+ async function copyKeptValues(sharedIds, runtime) {
326
+ const copies = [...sharedIds].map(([from, to]) => ({ from, to, copy: true }));
327
+ if (!copies.length) {
328
+ return;
329
+ }
330
+ await updateKeptValues((secrets) => withMovedServers(secrets, copies), runtime);
331
+ }
311
332
  /** A one-off destination (`--repo`, `--server`) leaves this machine as it is, so it only copies. */
312
333
  const keepsLocal = (options) => options.keepLocal || Boolean(options.repo ?? options.server);
313
334
  /**
@@ -365,17 +386,22 @@ export function moveLocalAssets(options, runtime) {
365
386
  throw new CliError("--as renames one asset, so name exactly one.", "barndog local move <name> --as <new-name>");
366
387
  }
367
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
+ }
392
+ // Only this machine's own workspace: a one-off destination isn't installed here.
393
+ const sharedIds = new Map();
368
394
  const moveItems = async () => {
369
395
  if (options.repo) {
370
- return moveToGit(await repoDestination(options.repo, runtime), items, options.to, runtime);
396
+ return moveToGit(await repoDestination(options.repo, runtime), { items, to: options.to }, runtime);
371
397
  }
372
398
  if (options.server) {
373
- return moveToCloud(await serverDestination(options.server, runtime), items, options.to, runtime);
399
+ return moveToCloud(await serverDestination(options.server, runtime), { items, to: options.to }, runtime);
374
400
  }
375
401
  if (settings.gitWorkspace) {
376
- return moveToGit(settings.gitWorkspace, items, options.to, runtime);
402
+ return moveToGit(settings.gitWorkspace, { items, to: options.to, sharedIds }, runtime);
377
403
  }
378
- return moveToCloud(await connection(runtime), items, options.to, runtime);
404
+ return moveToCloud(await connection(runtime), { items, to: options.to, sharedIds }, runtime);
379
405
  };
380
406
  const result = await moveItems();
381
407
  result.rows.unshift(...invalid);
@@ -384,6 +410,9 @@ export function moveLocalAssets(options, runtime) {
384
410
  if (!result.keepLocal) {
385
411
  result.removed = await finishMove(overlay.label, result, moved, items);
386
412
  }
413
+ else {
414
+ await copyKeptValues(sharedIds, runtime);
415
+ }
387
416
  if (moved.length) {
388
417
  await synchronize(undefined, runtime, undefined, { prompt: false });
389
418
  }
@@ -412,7 +441,7 @@ export async function listLocalAssets(runtime) {
412
441
  state: "moving",
413
442
  movingTo: moveDestination(entry),
414
443
  }
415
- : { state: "active" };
444
+ : { state: "available" };
416
445
  };
417
446
  return {
418
447
  folder: settings.localWorkspace.path,
@@ -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
  }
@@ -1,49 +1,7 @@
1
- import { fileURLToPath } from "node:url";
2
- import { coerce, satisfies, validRange } from "semver";
3
- import { programOutput, findExecutable, runtimeExecutable, } from "../../platform.js";
4
1
  import { adapters, nativeFile } from "../../harnesses/registry.js";
5
2
  import { secretRefs } from "../../harnesses/formats.js";
6
3
  import { hookCommand } from "./hook-shell.js";
7
- /** A `--version` answer is one short line; anything slower or longer is broken. */
8
- const VERSION_TIMEOUT_MS = 5000;
9
- const VERSION_OUTPUT_LIMIT_BYTES = 4096;
10
- /**
11
- * Checks required executables and optionally runs the harness version command;
12
- * returns the first issue or null.
13
- */
14
- export async function runtimeRequirements(harness, requirements, runtime) {
15
- for (const command of requirements.commands) {
16
- if (!(await runtimeExecutable(command, runtime))) {
17
- return `Missing runtime: ${command}`;
18
- }
19
- }
20
- if (requirements.harnessVersion) {
21
- if (!validRange(requirements.harnessVersion)) {
22
- return `Invalid declared harness version requirement: ${requirements.harnessVersion}`;
23
- }
24
- const command = (await Promise.all(adapters[harness].commands.map((c) => findExecutable(c, runtime)))).find(Boolean);
25
- if (!command) {
26
- return `Cannot verify ${harness} version: harness command is missing`;
27
- }
28
- try {
29
- const stdout = await programOutput(command, ["--version"], runtime, {
30
- timeoutMs: VERSION_TIMEOUT_MS,
31
- maxBytes: VERSION_OUTPUT_LIMIT_BYTES,
32
- });
33
- const version = coerce(stdout);
34
- if (!version) {
35
- return `Cannot verify ${harness} version: ${command} --version printed no version`;
36
- }
37
- if (!satisfies(version, requirements.harnessVersion)) {
38
- return `${harness} ${version.version} does not meet ${requirements.harnessVersion}`;
39
- }
40
- }
41
- catch {
42
- return `Cannot verify ${harness} version`;
43
- }
44
- }
45
- return null;
46
- }
4
+ import { runtimeRequirements } from "./requirements.js";
47
5
  /**
48
6
  * Why a Hook can't be installed for a tool: no implementation for it, or
49
7
  * something its implementation needs is missing here. Null when it can.
@@ -51,9 +9,10 @@ export async function runtimeRequirements(harness, requirements, runtime) {
51
9
  export async function hookIssue(harness, manifest, runtime) {
52
10
  const implementation = manifest.implementations[harness];
53
11
  if (!implementation) {
54
- return "No implementation for this harness";
12
+ return { type: "no-implementation" };
55
13
  }
56
- return runtimeRequirements(harness, implementation.requirements, runtime);
14
+ const requirement = await runtimeRequirements(harness, implementation.requirements, runtime);
15
+ return requirement && { type: "missing-requirement", requirement };
57
16
  }
58
17
  /**
59
18
  * Builds a harness-specific MCP configuration entry; does not write native
@@ -72,7 +31,7 @@ export function mcpEntry(harness, server, kept, runtime) {
72
31
  harness,
73
32
  file: nativeFile(harness, "mcp", home),
74
33
  key: server.name,
75
- value: adapters[harness].mcp.server(server.definition, fileURLToPath(new URL("../../entry/mcp-launch.js", import.meta.url)), used),
34
+ value: adapters[harness].mcp.server(server.definition, used, runtime.platform),
76
35
  ...(Object.keys(used).length
77
36
  ? {
78
37
  secrets: Object.entries(used).map(([variable, value]) => ({
@@ -43,6 +43,17 @@ function hookLine(root, command, harness, platform) {
43
43
  };
44
44
  }
45
45
  export const hookCommand = (root, command, harness, platform) => lineFor(harness, hookLine(root, command, harness, platform), platform);
46
+ /**
47
+ * A command `hookCommand()` wrote for the folder `from`, rewritten to set the
48
+ * root variables to `to` instead; the Hook author's own command is kept as
49
+ * is. Undefined when the command doesn't start the way BarnDog writes it.
50
+ */
51
+ export function movedHookCommand(command, folders, harness, platform) {
52
+ const prefix = hookCommand(folders.from, "", harness, platform);
53
+ return command.startsWith(prefix)
54
+ ? hookCommand(folders.to, command.slice(prefix.length), harness, platform)
55
+ : undefined;
56
+ }
46
57
  /** How `hooks test` runs a Hook: in the shell its tool would use. */
47
58
  export async function hookInvocation(root, command, harness, runtime) {
48
59
  const line = hookLine(root, command, harness, runtime.platform);
@@ -0,0 +1,98 @@
1
+ import { coerce, satisfies, validRange } from "semver";
2
+ import { programOutput, findExecutable, runtimeExecutable, } from "../../platform.js";
3
+ import { adapters } from "../../harnesses/registry.js";
4
+ /** A `--version` answer is one short line; anything slower or longer is broken. */
5
+ const VERSION_TIMEOUT_MS = 5000;
6
+ const VERSION_OUTPUT_LIMIT_BYTES = 4096;
7
+ /** The tool's own version, or why it couldn't be read. */
8
+ async function toolVersion(harness, range, runtime) {
9
+ const command = (await Promise.all(adapters[harness].commands.map((c) => findExecutable(c, runtime)))).find(Boolean);
10
+ if (!command) {
11
+ return { type: "tool-missing", range };
12
+ }
13
+ try {
14
+ const stdout = await programOutput(command, ["--version"], runtime, {
15
+ timeoutMs: VERSION_TIMEOUT_MS,
16
+ maxBytes: VERSION_OUTPUT_LIMIT_BYTES,
17
+ });
18
+ return coerce(stdout)?.version ?? { type: "version-unknown", range };
19
+ }
20
+ catch {
21
+ return { type: "version-unknown", range };
22
+ }
23
+ }
24
+ /**
25
+ * Checks required executables and optionally runs the harness version command;
26
+ * returns every requirement this machine misses, in the order declared.
27
+ */
28
+ export async function missingRequirements(harness, requirements, runtime) {
29
+ const missing = [];
30
+ for (const command of requirements.commands) {
31
+ if (!(await runtimeExecutable(command, runtime))) {
32
+ missing.push({ type: "command", command });
33
+ }
34
+ }
35
+ const range = requirements.harnessVersion;
36
+ if (!range) {
37
+ return missing;
38
+ }
39
+ if (!validRange(range)) {
40
+ return [...missing, { type: "invalid-range", range }];
41
+ }
42
+ const installed = await toolVersion(harness, range, runtime);
43
+ if (typeof installed !== "string") {
44
+ return [...missing, installed];
45
+ }
46
+ return satisfies(installed, range)
47
+ ? missing
48
+ : [...missing, { type: "version", range, installed }];
49
+ }
50
+ /** The first requirement this machine misses, or null when it has them all. */
51
+ export async function runtimeRequirements(harness, requirements, runtime) {
52
+ const [first] = await missingRequirements(harness, requirements, runtime);
53
+ return first ?? null;
54
+ }
55
+ /**
56
+ * What is missing, as the end of a sentence about the Hook:
57
+ * `lint-on-save` + ` needs the deno command, which isn't installed here or isn't on your PATH`.
58
+ */
59
+ export function requirementMessage(harness, missing) {
60
+ const tool = adapters[harness].name;
61
+ switch (missing.type) {
62
+ case "command":
63
+ return `needs the ${missing.command} command, which isn't installed here or isn't on your PATH`;
64
+ case "invalid-range":
65
+ return `declares a ${tool} version BarnDog can't read: ${missing.range}`;
66
+ case "tool-missing":
67
+ return `needs ${tool} ${missing.range}, but BarnDog can't find ${tool} to check its version`;
68
+ case "version-unknown":
69
+ return `needs ${tool} ${missing.range}, but BarnDog couldn't read ${tool}'s version`;
70
+ case "version":
71
+ return `needs ${tool} ${missing.range}; this machine has ${missing.installed}`;
72
+ }
73
+ }
74
+ /** What the developer can do about it, in words. */
75
+ export function requirementFix(harness, missing) {
76
+ const tool = adapters[harness].name;
77
+ switch (missing.type) {
78
+ case "command":
79
+ return `Install ${missing.command}`;
80
+ case "invalid-range":
81
+ return "Ask the Hook's author to fix requirements.harnessVersion in hook.yaml";
82
+ case "tool-missing":
83
+ case "version-unknown":
84
+ case "version":
85
+ return `Install or update ${tool}`;
86
+ }
87
+ }
88
+ /** A skip as one sentence fragment, such as `No implementation for this harness`. */
89
+ export function skipReason(harness, skip) {
90
+ switch (skip.type) {
91
+ case "no-implementation":
92
+ return "No implementation for this harness";
93
+ case "no-command-hooks":
94
+ return `${adapters[harness].name} has no command hooks`;
95
+ case "missing-requirement":
96
+ return requirementMessage(harness, skip.requirement);
97
+ }
98
+ }