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.
- package/README.md +21 -0
- package/dist/cli.js +29 -3
- package/dist/commands/add.js +4 -2
- package/dist/commands/asset-install.js +7 -3
- package/dist/commands/asset-list.js +5 -1
- package/dist/commands/asset-share.js +15 -3
- package/dist/commands/capabilities.js +23 -5
- package/dist/commands/collaboration.js +9 -3
- package/dist/commands/harnesses.js +33 -3
- package/dist/commands/hooks.js +16 -1
- package/dist/commands/mcp-env.js +159 -43
- package/dist/commands/mcp.js +13 -3
- package/dist/commands/origin.js +7 -4
- package/dist/commands/sets.js +16 -5
- package/dist/commands/status.js +3 -1
- package/dist/commands/sync.js +1 -1
- package/dist/commands/uninstall.js +64 -0
- package/dist/commands/workspace.js +19 -0
- package/dist/config/paths.js +2 -0
- package/dist/config/state.js +19 -0
- package/dist/entry/update-check.js +15 -0
- package/dist/errors.js +4 -0
- package/dist/features/adoption/adopt.js +47 -29
- package/dist/features/adoption/scan.js +5 -0
- package/dist/features/asset-kinds/url-query.js +26 -0
- package/dist/features/auth/session.js +22 -0
- package/dist/features/collaboration/workspace-store.js +2 -1
- package/dist/features/doctor/check.js +1 -0
- package/dist/features/doctor/grouped.js +127 -0
- package/dist/features/doctor/index.js +29 -27
- package/dist/features/environment/events.js +18 -1
- package/dist/features/environment/kept-value-events.js +40 -0
- package/dist/features/environment/kept-values.js +68 -0
- package/dist/features/environment/mcp-records.js +57 -0
- package/dist/features/environment/moves.js +17 -1
- package/dist/features/environment/stage.js +32 -28
- package/dist/features/environment/sync.js +13 -3
- package/dist/features/environment/writes.js +25 -27
- package/dist/features/hooks/compatibility.js +37 -17
- package/dist/features/hooks/test.js +2 -2
- package/dist/features/inventory/hook-matrix.js +14 -0
- package/dist/features/inventory/hook-readiness.js +47 -0
- package/dist/features/inventory/index.js +54 -3
- package/dist/features/inventory/show.js +10 -2
- package/dist/features/mcps/import.js +74 -21
- package/dist/features/mcps/index.js +28 -5
- package/dist/features/mcps/values.js +161 -23
- package/dist/features/migration/migrate.js +15 -0
- package/dist/features/migration/move.js +38 -9
- package/dist/features/migration/promote.js +1 -0
- package/dist/features/native/adapters.js +5 -46
- package/dist/features/native/hook-shell.js +11 -0
- package/dist/features/native/requirements.js +98 -0
- package/dist/features/secrets/index.js +95 -29
- package/dist/features/uninstall/index.js +126 -0
- package/dist/features/uninstall/leave.js +152 -0
- package/dist/features/uninstall/survey.js +103 -0
- package/dist/features/update-check/index.js +108 -0
- package/dist/features/workspace/cloud-source.js +38 -4
- package/dist/features/workspace/format.js +6 -1
- package/dist/harnesses/claude.js +1 -1
- package/dist/harnesses/codex.js +46 -22
- package/dist/harnesses/cursor.js +1 -1
- package/dist/harnesses/opencode.js +1 -1
- package/dist/harnesses/read-renamed-variables.js +141 -0
- package/dist/harnesses/renamed-variables.js +99 -0
- package/dist/platform.js +12 -1
- package/dist/program.js +24 -9
- package/dist/ui/add-hook.js +2 -2
- package/dist/ui/add-mcp.js +54 -16
- package/dist/ui/add-skills.js +2 -2
- package/dist/ui/adopt.js +90 -19
- package/dist/ui/assets.js +15 -2
- package/dist/ui/collaboration.js +5 -3
- package/dist/ui/connection.js +2 -0
- package/dist/ui/format.js +81 -2
- package/dist/ui/harness-picker.js +7 -2
- package/dist/ui/hook-compatibility.js +176 -22
- package/dist/ui/mcp.js +169 -24
- package/dist/ui/move.js +3 -2
- package/dist/ui/onboarding.js +11 -2
- package/dist/ui/output.js +116 -37
- package/dist/ui/package-details.js +7 -2
- package/dist/ui/pick.js +20 -6
- package/dist/ui/secret-input.js +9 -0
- package/dist/ui/selection.js +1 -1
- package/dist/ui/uninstall.js +186 -0
- package/dist/ui/update.js +10 -0
- package/dist/ui/variable-problems.js +23 -0
- package/dist/version.js +2 -2
- package/package.json +2 -2
- package/dist/entry/mcp-launch.js +0 -71
|
@@ -2,6 +2,7 @@ import { chmod, lstat, mkdir, readFile } from "node:fs/promises";
|
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { barndogPaths } from "../../config/paths.js";
|
|
4
4
|
import { CliError } from "../../errors.js";
|
|
5
|
+
import { secretRefs } from "../../harnesses/formats.js";
|
|
5
6
|
import { privatePermissions, protectPrivate, removeCommand, } from "../../platform.js";
|
|
6
7
|
import { POSIX_MODE } from "../../shared/constants.js";
|
|
7
8
|
import { ifExists, writeAtomic } from "../../shared/files.js";
|
|
@@ -9,16 +10,35 @@ import { withPidLock } from "../../shared/lock.js";
|
|
|
9
10
|
import { VARIABLE_NAME } from "../../shared/patterns.js";
|
|
10
11
|
import { posixQuote } from "../../shared/shell.js";
|
|
11
12
|
const variable = z.string().regex(new RegExp(`^${VARIABLE_NAME.source}$`));
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
/** A kept value, or another environment variable the server reads in its place. */
|
|
14
|
+
const keptValueSchema = z.union([
|
|
15
|
+
z.strictObject({ value: z.string() }),
|
|
16
|
+
z.strictObject({ from: variable }),
|
|
17
|
+
]);
|
|
18
|
+
const secretsSchema = z.discriminatedUnion("version", [
|
|
19
|
+
z.strictObject({
|
|
20
|
+
version: z.literal(1),
|
|
21
|
+
values: z.record(variable, z.string()),
|
|
22
|
+
}),
|
|
23
|
+
z.strictObject({
|
|
24
|
+
version: z.literal(2),
|
|
25
|
+
/** MCP asset ID → variable → its value. */
|
|
26
|
+
servers: z.record(z.string(), z.record(variable, keptValueSchema)),
|
|
27
|
+
/** Values from a version 1 file that no installed server was using. */
|
|
28
|
+
unassigned: z.record(variable, z.string()),
|
|
29
|
+
}),
|
|
30
|
+
]);
|
|
31
|
+
/** The variable a kept value says to read instead; undefined for a value kept as written. */
|
|
32
|
+
export const readsFrom = (kept) => kept && "from" in kept ? kept.from : undefined;
|
|
33
|
+
/** The value kept as written; undefined for one that reads another variable. */
|
|
34
|
+
export const literalOf = (kept) => kept && "value" in kept ? kept.value : undefined;
|
|
35
|
+
const empty = { version: 2, servers: {}, unassigned: {} };
|
|
36
|
+
/** The file as stored; an empty current one when there's no file yet. */
|
|
37
|
+
export async function readStoredSecrets(runtime) {
|
|
18
38
|
const path = barndogPaths(runtime.home).secrets;
|
|
19
39
|
const info = await ifExists(lstat(path));
|
|
20
40
|
if (!info) {
|
|
21
|
-
return
|
|
41
|
+
return empty;
|
|
22
42
|
}
|
|
23
43
|
if (!info.isFile()) {
|
|
24
44
|
throw new CliError(`${path} isn't a regular file, so BarnDog can't read the MCP values it keeps.`, "Delete it, then set the values again with barndog mcp env set.", { command: removeCommand(path, runtime.platform) });
|
|
@@ -31,7 +51,7 @@ export async function readSecrets(runtime) {
|
|
|
31
51
|
if (!parsed.success) {
|
|
32
52
|
throw new CliError(`${path} is damaged, so BarnDog can't read the MCP values it keeps.`, "Delete it, then set the values again with barndog mcp env set.", { command: removeCommand(path, runtime.platform) });
|
|
33
53
|
}
|
|
34
|
-
return parsed.data
|
|
54
|
+
return parsed.data;
|
|
35
55
|
}
|
|
36
56
|
function parseJson(text) {
|
|
37
57
|
try {
|
|
@@ -41,37 +61,83 @@ function parseJson(text) {
|
|
|
41
61
|
return undefined;
|
|
42
62
|
}
|
|
43
63
|
}
|
|
44
|
-
/**
|
|
45
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Replaces the file with what `change` makes of it, under a lock, and returns
|
|
66
|
+
* the file's path and what it now holds. `change` receives the file as stored,
|
|
67
|
+
* so a version 1 file has to be converted there.
|
|
68
|
+
*/
|
|
69
|
+
export async function changeSecrets(change, runtime) {
|
|
46
70
|
const paths = barndogPaths(runtime.home);
|
|
47
|
-
for (const name of Object.keys(changes)) {
|
|
48
|
-
if (!variable.safeParse(name).success) {
|
|
49
|
-
throw new CliError(`${name} isn't an environment variable name.`, "Use letters, digits and underscores, not starting with a digit.");
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
71
|
await mkdir(paths.root, {
|
|
53
72
|
recursive: true,
|
|
54
73
|
mode: POSIX_MODE.privateDirectory,
|
|
55
74
|
});
|
|
56
75
|
await chmod(paths.root, POSIX_MODE.privateDirectory);
|
|
57
76
|
await protectPrivate(paths.root, runtime);
|
|
58
|
-
await withPidLock(paths.secretsLock, {
|
|
77
|
+
const secrets = await withPidLock(paths.secretsLock, {
|
|
59
78
|
starting: "Another change to the kept MCP values is starting. Wait for it to finish, then try again.",
|
|
60
79
|
running: "Another change to the kept MCP values is running. Wait for it to finish, then try again.",
|
|
61
80
|
}, async () => {
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
};
|
|
65
|
-
for (const [name, value] of Object.entries(changes)) {
|
|
66
|
-
if (value === null) {
|
|
67
|
-
delete values[name];
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
values[name] = value;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
await writeAtomic(paths.secrets, JSON.stringify({ version: 1, values }, null, 2) + "\n");
|
|
81
|
+
const next = withoutEmptyServers(change(await readStoredSecrets(runtime)));
|
|
82
|
+
await writeAtomic(paths.secrets, JSON.stringify({ version: 2, ...next }, null, 2) + "\n");
|
|
74
83
|
await protectPrivate(paths.secrets, runtime);
|
|
84
|
+
return next;
|
|
75
85
|
});
|
|
76
|
-
return paths.secrets;
|
|
86
|
+
return { file: paths.secrets, secrets };
|
|
87
|
+
}
|
|
88
|
+
function withoutEmptyServers(secrets) {
|
|
89
|
+
return {
|
|
90
|
+
servers: Object.fromEntries(Object.entries(secrets.servers).filter(([, values]) => Object.keys(values).length > 0)),
|
|
91
|
+
unassigned: secrets.unassigned,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/** Throws unless `name` can be an environment variable's name. */
|
|
95
|
+
export function assertVariableName(name) {
|
|
96
|
+
if (!variable.safeParse(name).success) {
|
|
97
|
+
throw new CliError(`${name} isn't an environment variable name.`, "Use letters, digits and underscores, not starting with a digit.");
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* A server's definition with each variable kept as `from` read from that other
|
|
102
|
+
* variable instead, and the values written into its entries as they are.
|
|
103
|
+
*/
|
|
104
|
+
export function applyKeptValues(definition, values = {}) {
|
|
105
|
+
const rewrite = (refs) => Object.fromEntries(Object.entries(refs).map(([key, ref]) => {
|
|
106
|
+
const from = readsFrom(values[ref.secretRef]);
|
|
107
|
+
return [key, from ? { ...ref, secretRef: from } : ref];
|
|
108
|
+
}));
|
|
109
|
+
const kept = Object.fromEntries(Object.entries(values).flatMap(([name, value]) => {
|
|
110
|
+
const literal = literalOf(value);
|
|
111
|
+
return literal === undefined ? [] : [[name, literal]];
|
|
112
|
+
}));
|
|
113
|
+
return {
|
|
114
|
+
definition: definition.transport === "stdio"
|
|
115
|
+
? { ...definition, environment: rewrite(definition.environment) }
|
|
116
|
+
: { ...definition, headers: rewrite(definition.headers) },
|
|
117
|
+
kept,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The variables a definition names that have no value on this machine:
|
|
122
|
+
* BarnDog keeps none for this server and `env` doesn't set the variable read,
|
|
123
|
+
* even to an empty string. Each once, in the order the definition names them.
|
|
124
|
+
*/
|
|
125
|
+
export function missingValues(definition, values, env) {
|
|
126
|
+
const missing = new Map();
|
|
127
|
+
for (const { secretRef } of Object.values(secretRefs(definition))) {
|
|
128
|
+
const kept = values[secretRef];
|
|
129
|
+
const from = readsFrom(kept);
|
|
130
|
+
const read = from ?? secretRef;
|
|
131
|
+
const found = literalOf(kept) ??
|
|
132
|
+
literalOf(from ? values[from] : undefined) ??
|
|
133
|
+
env[read];
|
|
134
|
+
// An empty value is still a value: a tool passes it as it is.
|
|
135
|
+
if (found === undefined) {
|
|
136
|
+
missing.set(secretRef, {
|
|
137
|
+
variable: secretRef,
|
|
138
|
+
...(from ? { from } : {}),
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return [...missing.values()];
|
|
77
143
|
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `barndog uninstall`: takes BarnDog off this machine. BarnDog's own
|
|
3
|
+
* registrations (the session-start sync, the knowledge index, OpenCode's
|
|
4
|
+
* plugin module) always go. The developer's Skills, MCP servers and Hooks
|
|
5
|
+
* either stay in each coding tool as plain files of its own, or go too, with
|
|
6
|
+
* adopted assets put back where they came from. Then every session is signed
|
|
7
|
+
* out and `~/.barndog/` is deleted, except the local workspace when kept.
|
|
8
|
+
*/
|
|
9
|
+
import { readdir, rm } from "node:fs/promises";
|
|
10
|
+
import { basename, join, relative, sep } from "node:path";
|
|
11
|
+
import { barndogPaths } from "../../config/paths.js";
|
|
12
|
+
import { readSettings } from "../../config/settings.js";
|
|
13
|
+
import { readState } from "../../config/state.js";
|
|
14
|
+
import { fileText, ifExists, isInside } from "../../shared/files.js";
|
|
15
|
+
import { readAdoptions } from "../adoption/record.js";
|
|
16
|
+
import { undoAdoption } from "../adoption/undo.js";
|
|
17
|
+
import { signOutEverywhere } from "../auth/session.js";
|
|
18
|
+
import { locked, transact } from "../environment/transaction.js";
|
|
19
|
+
import { entryNames, linkWrites, nativeWrites, stateText, } from "../environment/writes.js";
|
|
20
|
+
import { leaveInPlace } from "./leave.js";
|
|
21
|
+
import { mcpVariables } from "./survey.js";
|
|
22
|
+
/** Undoes each adoption holding a chosen asset: whole when unfinished or fully chosen. */
|
|
23
|
+
async function restoreAdopted(options, runtime) {
|
|
24
|
+
if (options.keepSetup || !options.restore.length) {
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
const restored = [];
|
|
28
|
+
for (const record of await readAdoptions(runtime.home)) {
|
|
29
|
+
const open = record.items.filter((item) => !item.undone);
|
|
30
|
+
const chosen = open.filter((item) => options.restore.includes(item.key));
|
|
31
|
+
if (record.status === "undone" || !chosen.length) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
const whole = record.status === "pending" || chosen.length === open.length;
|
|
35
|
+
const result = await undoAdoption({
|
|
36
|
+
id: record.id,
|
|
37
|
+
only: whole ? undefined : chosen.map((item) => item.key),
|
|
38
|
+
force: options.force,
|
|
39
|
+
}, runtime);
|
|
40
|
+
restored.push(...result.restored);
|
|
41
|
+
}
|
|
42
|
+
return restored;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Takes BarnDog's entries out of the coding tools' settings (all of them, or
|
|
46
|
+
* only its own registrations when the setup stays, with Hooks pointed at
|
|
47
|
+
* their new folders), removes Skill links, and empties `state.json`, as one
|
|
48
|
+
* journaled change.
|
|
49
|
+
*/
|
|
50
|
+
async function removeEntries(keepSetup, runtime) {
|
|
51
|
+
const { home } = runtime;
|
|
52
|
+
const stateFile = barndogPaths(home).state;
|
|
53
|
+
const state = await readState(home);
|
|
54
|
+
const kept = keepSetup
|
|
55
|
+
? await leaveInPlace(state, runtime)
|
|
56
|
+
: { left: [], hooks: [] };
|
|
57
|
+
// MCP entries that stay aren't BarnDog's to edit any more, so they are left out.
|
|
58
|
+
const previous = keepSetup
|
|
59
|
+
? state.native.filter((entry) => entry.kind !== "mcp")
|
|
60
|
+
: state.native;
|
|
61
|
+
const nameOf = entryNames(state.installed.map((entry) => [entry.id, basename(entry.path)]));
|
|
62
|
+
const settings = await nativeWrites(previous, kept.hooks, {
|
|
63
|
+
trees: [],
|
|
64
|
+
updated: new Set(),
|
|
65
|
+
nameOf,
|
|
66
|
+
platform: runtime.platform,
|
|
67
|
+
});
|
|
68
|
+
// A Skill left in place is a folder now, so only links still to remove are written.
|
|
69
|
+
const links = keepSetup
|
|
70
|
+
? { writes: [], events: [] }
|
|
71
|
+
: await linkWrites(state.links, [], new Set());
|
|
72
|
+
const writes = [
|
|
73
|
+
...links.writes,
|
|
74
|
+
...settings.writes,
|
|
75
|
+
{
|
|
76
|
+
kind: "file",
|
|
77
|
+
path: stateFile,
|
|
78
|
+
before: await fileText(stateFile),
|
|
79
|
+
after: stateText({ links: [], native: [], trees: [], mcpServers: [] }),
|
|
80
|
+
},
|
|
81
|
+
];
|
|
82
|
+
await transact(runtime, writes);
|
|
83
|
+
const removed = [...links.events, ...settings.events].filter((event) => event.type === "removed" ||
|
|
84
|
+
(event.type === "native" && event.action === "removed"));
|
|
85
|
+
return {
|
|
86
|
+
left: kept.left,
|
|
87
|
+
removed,
|
|
88
|
+
variables: keepSetup ? mcpVariables(state) : [],
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/** Deletes `root`, or everything in it except the top folder holding `keep`. */
|
|
92
|
+
async function deleteRoot(root, keep) {
|
|
93
|
+
if (!keep) {
|
|
94
|
+
await rm(root, { recursive: true, force: true });
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const [top] = relative(root, keep).split(sep);
|
|
98
|
+
for (const name of (await ifExists(readdir(root))) ?? []) {
|
|
99
|
+
if (name !== top) {
|
|
100
|
+
await rm(join(root, name), { recursive: true, force: true });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export async function uninstall(options, runtime) {
|
|
105
|
+
const root = barndogPaths(runtime.home).root;
|
|
106
|
+
// Read before anything changes; a damaged config.yaml only means no workspace to keep.
|
|
107
|
+
const settings = await readSettings(runtime.home).catch(() => null);
|
|
108
|
+
const workspace = settings?.localWorkspace?.path;
|
|
109
|
+
const restored = await restoreAdopted(options, runtime);
|
|
110
|
+
const { left, removed, variables } = await locked(runtime, () => removeEntries(options.keepSetup, runtime));
|
|
111
|
+
const signedOut = await signOutEverywhere(runtime);
|
|
112
|
+
const keptWorkspace = options.keepWorkspace && workspace && isInside(root, workspace)
|
|
113
|
+
? workspace
|
|
114
|
+
: undefined;
|
|
115
|
+
await deleteRoot(root, keptWorkspace);
|
|
116
|
+
return {
|
|
117
|
+
keptSetup: options.keepSetup,
|
|
118
|
+
left,
|
|
119
|
+
removed,
|
|
120
|
+
variables,
|
|
121
|
+
restored,
|
|
122
|
+
signedOut,
|
|
123
|
+
root,
|
|
124
|
+
keptWorkspace,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Leaves BarnDog's Skills and Hooks in each coding tool as plain files of the
|
|
3
|
+
* tool's own, so they keep working once `~/.barndog/` is gone: each Skill link
|
|
4
|
+
* becomes a copy of the folder it pointed at, and each Hook folder is copied
|
|
5
|
+
* beside the tool's hook settings, where its registrations are pointed next.
|
|
6
|
+
* MCP entries need nothing: they never point into BarnDog's files.
|
|
7
|
+
*
|
|
8
|
+
* Every step checks what is already done, so an interrupted uninstall can run
|
|
9
|
+
* again.
|
|
10
|
+
*/
|
|
11
|
+
import { cp, lstat, rename, rm, unlink } from "node:fs/promises";
|
|
12
|
+
import { basename, dirname, join } from "node:path";
|
|
13
|
+
import { CliError } from "../../errors.js";
|
|
14
|
+
import { isHookHarness, nativeFile } from "../../harnesses/registry.js";
|
|
15
|
+
import { ifExists } from "../../shared/files.js";
|
|
16
|
+
import { isRecord } from "../../shared/values.js";
|
|
17
|
+
import { movedHookCommand } from "../native/hook-shell.js";
|
|
18
|
+
import { linkTarget } from "../environment/transaction.js";
|
|
19
|
+
import { treeFingerprint } from "../workspace/tree.js";
|
|
20
|
+
/** Where a Hook's folder goes: `hooks/<name>` beside the tool's hook settings. */
|
|
21
|
+
const hookFolder = (harness, name, home) => join(dirname(nativeFile(harness, "hook", home)), "hooks", name);
|
|
22
|
+
function placements(state, home) {
|
|
23
|
+
const skills = state.links.map((link) => ({
|
|
24
|
+
kind: "skill",
|
|
25
|
+
name: basename(link.target),
|
|
26
|
+
harness: link.harness,
|
|
27
|
+
path: link.target,
|
|
28
|
+
source: link.source,
|
|
29
|
+
}));
|
|
30
|
+
const hooks = [];
|
|
31
|
+
for (const entry of state.native) {
|
|
32
|
+
const folder = state.installed.find((installed) => installed.kind === "hook" && installed.id === entry.id);
|
|
33
|
+
if (entry.kind !== "hook" ||
|
|
34
|
+
!folder ||
|
|
35
|
+
hooks.some((hook) => hook.source === folder.path && hook.harness === entry.harness)) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const name = basename(folder.path);
|
|
39
|
+
hooks.push({
|
|
40
|
+
kind: "hook",
|
|
41
|
+
name,
|
|
42
|
+
harness: entry.harness,
|
|
43
|
+
path: hookFolder(entry.harness, name, home),
|
|
44
|
+
source: folder.path,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return [...skills, ...hooks];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Whether the folder is already in place: true when `path` holds the same
|
|
51
|
+
* files as `source`. Throws when something else is there.
|
|
52
|
+
*/
|
|
53
|
+
async function placed(placement, runtime) {
|
|
54
|
+
const { path, source } = placement;
|
|
55
|
+
const info = await ifExists(lstat(path));
|
|
56
|
+
if (!info) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
if (info.isSymbolicLink() && placement.kind === "skill") {
|
|
60
|
+
if ((await linkTarget(path)) !== source) {
|
|
61
|
+
throw new CliError(`${path} was changed outside BarnDog, so uninstall stopped before changing anything.`, "Restore or remove that link, then run barndog uninstall again");
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
const [here, there] = await Promise.all([
|
|
66
|
+
treeFingerprint(path, runtime),
|
|
67
|
+
treeFingerprint(source, runtime),
|
|
68
|
+
]);
|
|
69
|
+
if (here === null || here !== there) {
|
|
70
|
+
throw new CliError(`${path} already exists, so BarnDog can't leave its ${placement.name} ${placement.kind === "skill" ? "Skill" : "Hook"} there.`, "Move it aside, then run barndog uninstall again");
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
/** Copies beside the destination first, so a Skill is never missing for long. */
|
|
75
|
+
async function place(placement) {
|
|
76
|
+
const { path, source } = placement;
|
|
77
|
+
const staging = `${path}.barndog-uninstall`;
|
|
78
|
+
await rm(staging, { recursive: true, force: true });
|
|
79
|
+
await cp(source, staging, { recursive: true });
|
|
80
|
+
if (await ifExists(lstat(path))) {
|
|
81
|
+
await unlink(path);
|
|
82
|
+
}
|
|
83
|
+
await rename(staging, path);
|
|
84
|
+
}
|
|
85
|
+
/** The Hook registration with its root variables pointing at `to`. */
|
|
86
|
+
function movedHook(entry, folders, platform) {
|
|
87
|
+
const { harness } = entry;
|
|
88
|
+
if (!isHookHarness(harness)) {
|
|
89
|
+
throw new Error(`${harness} has no hook settings to move a Hook in`);
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
...entry,
|
|
93
|
+
value: mapCommands(entry.value, (command) => {
|
|
94
|
+
const moved = movedHookCommand(command, folders, harness, platform);
|
|
95
|
+
if (moved === undefined) {
|
|
96
|
+
throw new Error(`A ${entry.key} Hook registration doesn't start the way BarnDog writes it`);
|
|
97
|
+
}
|
|
98
|
+
return moved;
|
|
99
|
+
}),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/** Rewrites the handler's `command`, or each of its nested `hooks[].command`. */
|
|
103
|
+
function mapCommands(value, change) {
|
|
104
|
+
const next = { ...value };
|
|
105
|
+
if (typeof next.command === "string") {
|
|
106
|
+
next.command = change(next.command);
|
|
107
|
+
}
|
|
108
|
+
if (Array.isArray(next.hooks)) {
|
|
109
|
+
next.hooks = next.hooks.map((hook) => isRecord(hook) && typeof hook.command === "string"
|
|
110
|
+
? { ...hook, command: change(hook.command) }
|
|
111
|
+
: hook);
|
|
112
|
+
}
|
|
113
|
+
return next;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Puts every Skill and Hook folder in place and returns the Hook registrations
|
|
117
|
+
* pointed at their new folders, for the caller to write with the rest.
|
|
118
|
+
*/
|
|
119
|
+
export async function leaveInPlace(state, runtime) {
|
|
120
|
+
const planned = placements(state, runtime.home);
|
|
121
|
+
// Check every destination before changing any.
|
|
122
|
+
const done = new Set();
|
|
123
|
+
for (const placement of planned) {
|
|
124
|
+
if (await placed(placement, runtime)) {
|
|
125
|
+
done.add(placement);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
for (const placement of planned) {
|
|
129
|
+
if (!done.has(placement)) {
|
|
130
|
+
await place(placement);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const hooks = state.native.flatMap((entry) => {
|
|
134
|
+
const folder = planned.find((placement) => placement.kind === "hook" &&
|
|
135
|
+
placement.harness === entry.harness &&
|
|
136
|
+
state.installed.some((installed) => installed.id === entry.id && installed.path === placement.source));
|
|
137
|
+
return entry.kind === "hook" && folder
|
|
138
|
+
? [
|
|
139
|
+
movedHook(entry, { from: folder.source, to: folder.path }, runtime.platform),
|
|
140
|
+
]
|
|
141
|
+
: [];
|
|
142
|
+
});
|
|
143
|
+
return {
|
|
144
|
+
left: planned.map(({ kind, name, harness, path }) => ({
|
|
145
|
+
kind,
|
|
146
|
+
name,
|
|
147
|
+
harness,
|
|
148
|
+
path,
|
|
149
|
+
})),
|
|
150
|
+
hooks,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/** What `barndog uninstall` would find to remove or leave on this machine. */
|
|
2
|
+
import { lstat } from "node:fs/promises";
|
|
3
|
+
import { basename } from "node:path";
|
|
4
|
+
import { barndogPaths } from "../../config/paths.js";
|
|
5
|
+
import { readSettings } from "../../config/settings.js";
|
|
6
|
+
import { readState } from "../../config/state.js";
|
|
7
|
+
import { CliError } from "../../errors.js";
|
|
8
|
+
import { secretRefs } from "../../harnesses/formats.js";
|
|
9
|
+
import { adapters } from "../../harnesses/registry.js";
|
|
10
|
+
import { ifExists, isInside } from "../../shared/files.js";
|
|
11
|
+
import { readCredentials } from "../auth/credentials.js";
|
|
12
|
+
import { readAdoptions, summary, } from "../adoption/record.js";
|
|
13
|
+
import { convertServer } from "../mcps/import.js";
|
|
14
|
+
/** The installed folder name a Hook registration's asset ID refers to. */
|
|
15
|
+
const hookName = (id, state) => {
|
|
16
|
+
const folder = state.installed.find((entry) => entry.kind === "hook" && entry.id === id);
|
|
17
|
+
return folder ? basename(folder.path) : id;
|
|
18
|
+
};
|
|
19
|
+
/** Each asset once per tool; a Hook has one entry per event it handles. */
|
|
20
|
+
function installedAssets(state) {
|
|
21
|
+
const assets = state.links.map((link) => ({
|
|
22
|
+
kind: "skill",
|
|
23
|
+
name: basename(link.target),
|
|
24
|
+
harness: link.harness,
|
|
25
|
+
}));
|
|
26
|
+
for (const entry of state.native) {
|
|
27
|
+
if (entry.kind !== "mcp" && entry.kind !== "hook") {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
const name = entry.kind === "mcp" ? entry.key : hookName(entry.id, state);
|
|
31
|
+
if (!assets.some((asset) => asset.kind === entry.kind &&
|
|
32
|
+
asset.name === name &&
|
|
33
|
+
asset.harness === entry.harness)) {
|
|
34
|
+
assets.push({ kind: entry.kind, name, harness: entry.harness });
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return assets;
|
|
38
|
+
}
|
|
39
|
+
/** An MCP entry BarnDog wrote, read back the way `mcp add --from` reads one. */
|
|
40
|
+
function definitionOf(entry) {
|
|
41
|
+
const { imported } = adapters[entry.harness].mcp;
|
|
42
|
+
try {
|
|
43
|
+
return convertServer({
|
|
44
|
+
name: entry.key,
|
|
45
|
+
server: imported ? imported(entry.value) : entry.value,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
if (error instanceof CliError) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The variables BarnDog's MCP entries read from the environment, each once.
|
|
57
|
+
* A value BarnDog kept is written into the entry, so it isn't one of them.
|
|
58
|
+
*/
|
|
59
|
+
export function mcpVariables(state) {
|
|
60
|
+
const variables = new Map();
|
|
61
|
+
for (const entry of state.native) {
|
|
62
|
+
const converted = entry.kind === "mcp" ? definitionOf(entry) : undefined;
|
|
63
|
+
if (!converted) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
const written = new Set(converted.literals.map(({ variable }) => variable));
|
|
67
|
+
for (const { secretRef } of Object.values(secretRefs(converted.definition))) {
|
|
68
|
+
if (!written.has(secretRef)) {
|
|
69
|
+
variables.set(secretRef, (variables.get(secretRef) ?? new Set()).add(entry.key));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return [...variables].map(([name, servers]) => ({
|
|
74
|
+
name,
|
|
75
|
+
servers: [...servers],
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
export async function uninstallSurvey(runtime) {
|
|
79
|
+
const { home } = runtime;
|
|
80
|
+
const root = barndogPaths(home).root;
|
|
81
|
+
// A damaged config.yaml shouldn't stop an uninstall; only the workspace path comes from it.
|
|
82
|
+
const settings = await readSettings(home).catch(() => null);
|
|
83
|
+
const state = await readState(home);
|
|
84
|
+
const adoptions = await readAdoptions(home);
|
|
85
|
+
const { servers } = await readCredentials(runtime);
|
|
86
|
+
const local = settings?.localWorkspace?.path;
|
|
87
|
+
// Setup always puts it in ~/.barndog/workspace; a path edited to point
|
|
88
|
+
// elsewhere is outside what uninstall deletes, so there is nothing to ask.
|
|
89
|
+
const workspace = local && isInside(root, local) && (await ifExists(lstat(local)))
|
|
90
|
+
? local
|
|
91
|
+
: undefined;
|
|
92
|
+
return {
|
|
93
|
+
root,
|
|
94
|
+
installed: installedAssets(state),
|
|
95
|
+
adopted: adoptions
|
|
96
|
+
.filter((record) => record.status !== "undone")
|
|
97
|
+
.flatMap((record) => summary(record).items)
|
|
98
|
+
.filter((item) => !item.undone),
|
|
99
|
+
workspace,
|
|
100
|
+
servers: Object.keys(servers),
|
|
101
|
+
variables: mcpVariables(state),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a newer BarnDog CLI is published. The answer comes from the public
|
|
3
|
+
* npm registry, whichever workspace or server the developer uses, and is
|
|
4
|
+
* cached in `~/.barndog/update-check.json`, so a command only reads a file;
|
|
5
|
+
* a detached process (`entry/update-check.ts`) refreshes it at most once a day.
|
|
6
|
+
*/
|
|
7
|
+
import { stat } from "node:fs/promises";
|
|
8
|
+
import semver from "semver";
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
import { barndogPaths } from "../../config/paths.js";
|
|
11
|
+
import { HOURS_PER_DAY, MILLISECONDS_PER_MINUTE, MINUTES_PER_HOUR, } from "../../shared/constants.js";
|
|
12
|
+
import { ifExists, readJsonIfExists, writeAtomic } from "../../shared/files.js";
|
|
13
|
+
/** The package's dist-tags, from the npm registry API. */
|
|
14
|
+
const DIST_TAGS_URL = "https://registry.npmjs.org/-/package/barndog/dist-tags";
|
|
15
|
+
/** Once a day is often enough to hear about a release. */
|
|
16
|
+
const CHECK_INTERVAL_MS = HOURS_PER_DAY * MINUTES_PER_HOUR * MILLISECONDS_PER_MINUTE;
|
|
17
|
+
/** The background check gives up rather than linger. */
|
|
18
|
+
const REQUEST_TIMEOUT_MS = 5000;
|
|
19
|
+
/**
|
|
20
|
+
* Variables that turn the check off. `NO_UPDATE_NOTIFIER` is the one the
|
|
21
|
+
* `update-notifier` package made common across npm CLIs.
|
|
22
|
+
*/
|
|
23
|
+
const OPT_OUT_VARIABLES = ["BARNDOG_NO_UPDATE_CHECK", "NO_UPDATE_NOTIFIER"];
|
|
24
|
+
const distTagsSchema = z.object({
|
|
25
|
+
latest: z.string(),
|
|
26
|
+
next: z.string().optional(),
|
|
27
|
+
});
|
|
28
|
+
const cacheSchema = z.object({
|
|
29
|
+
checkedAt: z.string(),
|
|
30
|
+
/** Absent until the registry has answered once. */
|
|
31
|
+
latest: z.string().optional(),
|
|
32
|
+
next: z.string().optional(),
|
|
33
|
+
});
|
|
34
|
+
/** A missing or unreadable cache counts as never checked. */
|
|
35
|
+
async function readCache(home) {
|
|
36
|
+
try {
|
|
37
|
+
return ((await readJsonIfExists(barndogPaths(home).updateCheck, cacheSchema)) ??
|
|
38
|
+
undefined);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Asks the registry for the published versions and caches them. A failure is
|
|
46
|
+
* cached as a check that keeps the earlier answer, so an offline machine
|
|
47
|
+
* still waits a day before trying again.
|
|
48
|
+
*/
|
|
49
|
+
export async function refreshUpdateCheck(runtime, fetchTags = fetch) {
|
|
50
|
+
const previous = await readCache(runtime.home);
|
|
51
|
+
let tags;
|
|
52
|
+
try {
|
|
53
|
+
const response = await fetchTags(DIST_TAGS_URL, {
|
|
54
|
+
headers: { accept: "application/json" },
|
|
55
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
56
|
+
});
|
|
57
|
+
tags = response.ok
|
|
58
|
+
? distTagsSchema.parse(await response.json())
|
|
59
|
+
: undefined;
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
tags = undefined;
|
|
63
|
+
}
|
|
64
|
+
const cache = {
|
|
65
|
+
checkedAt: new Date().toISOString(),
|
|
66
|
+
latest: tags ? tags.latest : previous?.latest,
|
|
67
|
+
next: tags ? tags.next : previous?.next,
|
|
68
|
+
};
|
|
69
|
+
await writeAtomic(barndogPaths(runtime.home).updateCheck, `${JSON.stringify(cache)}\n`);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The newest cached version above `current`. Someone on a prerelease also
|
|
73
|
+
* hears about newer prereleases; everyone else only about `latest`.
|
|
74
|
+
*/
|
|
75
|
+
function newestAbove(current, cache) {
|
|
76
|
+
if (!semver.valid(current)) {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
const candidates = [];
|
|
80
|
+
if (cache.latest && semver.valid(cache.latest)) {
|
|
81
|
+
candidates.push({ current, version: cache.latest, tag: "latest" });
|
|
82
|
+
}
|
|
83
|
+
if (cache.next && semver.valid(cache.next) && semver.prerelease(current)) {
|
|
84
|
+
candidates.push({ current, version: cache.next, tag: "next" });
|
|
85
|
+
}
|
|
86
|
+
return candidates
|
|
87
|
+
.filter((candidate) => semver.gt(candidate.version, current))
|
|
88
|
+
.sort((a, b) => semver.rcompare(a.version, b.version))[0];
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* What to do about updates after a command, from the cache alone. Undefined
|
|
92
|
+
* when the check is off: by an opt-out variable, in CI, or while BarnDog isn't
|
|
93
|
+
* set up (before `init`, after `uninstall`), so it never creates `~/.barndog/`.
|
|
94
|
+
*/
|
|
95
|
+
export async function updateCheck(current, runtime) {
|
|
96
|
+
const { env, home } = runtime;
|
|
97
|
+
if (env.CI || OPT_OUT_VARIABLES.some((name) => env[name]?.trim())) {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
if (!(await ifExists(stat(barndogPaths(home).settings)))) {
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
const cache = await readCache(home);
|
|
104
|
+
return {
|
|
105
|
+
update: cache && newestAbove(current, cache),
|
|
106
|
+
due: !cache || !(Date.now() - Date.parse(cache.checkedAt) < CHECK_INTERVAL_MS),
|
|
107
|
+
};
|
|
108
|
+
}
|