barndog 0.2.0 → 0.2.2
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 +45 -24
- package/dist/cli.js +27 -1
- package/dist/commands/add.js +1 -0
- package/dist/commands/adopt.js +91 -0
- package/dist/commands/asset-install.js +1 -0
- package/dist/commands/asset-list.js +12 -3
- package/dist/commands/availability.js +25 -8
- package/dist/commands/capabilities.js +8 -2
- package/dist/commands/collaboration.js +22 -2
- package/dist/commands/harnesses.js +104 -0
- package/dist/commands/mcp-env.js +167 -0
- package/dist/commands/mcp.js +4 -2
- package/dist/commands/sets.js +13 -2
- package/dist/commands/status.js +2 -1
- package/dist/commands/sync.js +2 -1
- package/dist/commands/uninstall.js +62 -0
- package/dist/commands/workspace.js +43 -5
- package/dist/config/paths.js +21 -0
- package/dist/config/settings.js +5 -3
- package/dist/config/state.js +18 -6
- package/dist/entry/update-check.js +15 -0
- package/dist/features/adoption/adopt.js +235 -0
- package/dist/features/adoption/copies.js +29 -0
- package/dist/features/adoption/originals.js +116 -0
- package/dist/features/adoption/record.js +120 -0
- package/dist/features/adoption/restore.js +28 -0
- package/dist/features/adoption/scan.js +482 -0
- package/dist/features/adoption/undo.js +42 -0
- package/dist/features/asset-kinds/index.js +9 -0
- package/dist/features/auth/credentials.js +7 -14
- package/dist/features/auth/session.js +26 -5
- package/dist/features/auth/transport.js +3 -2
- package/dist/features/collaboration/workspace-git.js +13 -7
- package/dist/features/collaboration/workspace-github.js +2 -1
- package/dist/features/collaboration/workspace-store.js +25 -4
- package/dist/features/collaboration/workspace.js +22 -2
- package/dist/features/doctor/index.js +46 -7
- package/dist/features/environment/events.js +8 -1
- package/dist/features/environment/harnesses.js +36 -0
- package/dist/features/environment/kept-values.js +68 -0
- package/dist/features/environment/moves.js +187 -0
- package/dist/features/environment/names.js +10 -6
- package/dist/features/environment/operations.js +7 -7
- package/dist/features/environment/stage.js +25 -1
- package/dist/features/environment/sync.js +17 -6
- package/dist/features/environment/writes.js +55 -8
- package/dist/features/hooks/compatibility.js +11 -5
- package/dist/features/hooks/divergence.js +30 -9
- package/dist/features/hooks/test.js +25 -25
- package/dist/features/imports/discover.js +1 -1
- package/dist/features/imports/local.js +15 -2
- package/dist/features/imports/remote.js +2 -1
- package/dist/features/inventory/index.js +57 -3
- package/dist/features/inventory/show.js +74 -0
- package/dist/features/mcps/import.js +95 -21
- package/dist/features/mcps/index.js +24 -9
- package/dist/features/mcps/values.js +121 -0
- package/dist/features/migration/migrate.js +15 -0
- package/dist/features/migration/move.js +105 -25
- package/dist/features/migration/promote.js +2 -0
- package/dist/features/native/adapters.js +20 -7
- package/dist/features/native/configuration.js +8 -4
- package/dist/features/native/hook-shell.js +11 -0
- package/dist/features/native/secret-values.js +74 -0
- package/dist/features/secrets/index.js +142 -0
- 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/format.js +19 -10
- package/dist/features/workspace/git.js +11 -4
- package/dist/features/workspace/overlay.js +23 -36
- package/dist/features/workspace/packages.js +16 -3
- package/dist/features/workspace/publish.js +1 -1
- package/dist/features/workspace/repo-source.js +4 -3
- package/dist/features/workspace/setup.js +12 -3
- package/dist/features/workspace/snapshot.js +7 -1
- package/dist/harnesses/claude.js +2 -2
- package/dist/harnesses/codex.js +54 -20
- package/dist/harnesses/cursor.js +2 -2
- package/dist/harnesses/formats.js +12 -5
- package/dist/harnesses/opencode.js +3 -6
- package/dist/harnesses/read-renamed-variables.js +141 -0
- package/dist/harnesses/renamed-variables.js +99 -0
- package/dist/platform.js +51 -12
- package/dist/program.js +68 -8
- package/dist/shared/files.js +35 -0
- package/dist/shared/lock.js +87 -23
- package/dist/shared/patterns.js +8 -0
- package/dist/shared/shell.js +0 -2
- package/dist/ui/add-hook.js +8 -2
- package/dist/ui/add-mcp.js +82 -13
- package/dist/ui/add-skills.js +7 -5
- package/dist/ui/adopt.js +202 -0
- package/dist/ui/assets.js +5 -3
- package/dist/ui/auth.js +2 -1
- package/dist/ui/clash.js +2 -3
- package/dist/ui/connection.js +9 -2
- package/dist/ui/harness-picker.js +23 -0
- package/dist/ui/mcp.js +89 -23
- package/dist/ui/move.js +49 -2
- package/dist/ui/onboarding.js +40 -27
- package/dist/ui/output.js +35 -8
- package/dist/ui/package-details.js +34 -0
- package/dist/ui/scope-install.js +4 -1
- package/dist/ui/secret-input.js +28 -0
- package/dist/ui/selection.js +3 -1
- package/dist/ui/uninstall.js +174 -0
- package/dist/ui/update.js +10 -0
- package/dist/version.js +2 -2
- package/package.json +1 -1
- package/dist/entry/mcp-launch.js +0 -71
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `barndog adopt`: copy assets the user set up in their coding tools into
|
|
3
|
+
* BarnDog's local folder, take the originals out of the tools, and sync so
|
|
4
|
+
* BarnDog's copies install in their place. `undo.ts` puts them back.
|
|
5
|
+
*/
|
|
6
|
+
import { randomUUID } from "node:crypto";
|
|
7
|
+
import { copyFile, lstat, mkdir } from "node:fs/promises";
|
|
8
|
+
import { basename, dirname, join } from "node:path";
|
|
9
|
+
import { stringify } from "yaml";
|
|
10
|
+
import { barndogPaths } from "../../config/paths.js";
|
|
11
|
+
import { readSettings } from "../../config/settings.js";
|
|
12
|
+
import { readState } from "../../config/state.js";
|
|
13
|
+
import { CliError, friendlyMessage } from "../../errors.js";
|
|
14
|
+
import { POSIX_MODE } from "../../shared/constants.js";
|
|
15
|
+
import { ifExists, writeAtomic } from "../../shared/files.js";
|
|
16
|
+
import { defined } from "../../shared/lookup.js";
|
|
17
|
+
import { assetKinds } from "../asset-kinds/index.js";
|
|
18
|
+
import { synchronize } from "../environment/sync.js";
|
|
19
|
+
import { locked } from "../environment/transaction.js";
|
|
20
|
+
import { ensureLocalWorkspace } from "../imports/local.js";
|
|
21
|
+
import { keepLiteralValues, literalValues, } from "../mcps/import.js";
|
|
22
|
+
import { writePackage } from "../workspace/directory.js";
|
|
23
|
+
import { localRepoSource } from "../workspace/repo-source.js";
|
|
24
|
+
import { copyFingerprint, installed } from "./copies.js";
|
|
25
|
+
import { takeOut } from "./originals.js";
|
|
26
|
+
import { restore } from "./restore.js";
|
|
27
|
+
import { readAdoptions, writeAdoption, } from "./record.js";
|
|
28
|
+
import { findNativeAssets, } from "./scan.js";
|
|
29
|
+
/**
|
|
30
|
+
* What adopting `keys` takes over: each chosen asset, plus other tools'
|
|
31
|
+
* Skills and MCP servers of the same name, which BarnDog's one copy replaces.
|
|
32
|
+
*/
|
|
33
|
+
export function adoptionPlan(scan, keys) {
|
|
34
|
+
const byKey = new Map(scan.assets.map((asset) => [asset.key, asset]));
|
|
35
|
+
const unknown = keys.filter((key) => !byKey.has(key));
|
|
36
|
+
if (unknown.length) {
|
|
37
|
+
throw new CliError(`${unknown.join(", ")} ${unknown.length === 1 ? "isn't an asset" : "aren't assets"} BarnDog can adopt.`, "barndog adopt --dry-run");
|
|
38
|
+
}
|
|
39
|
+
const planned = new Map();
|
|
40
|
+
for (const key of keys) {
|
|
41
|
+
const asset = byKey.get(key);
|
|
42
|
+
if (!asset || planned.has(key)) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const group = [key, ...asset.sameName.map((other) => other.key)];
|
|
46
|
+
const chosen = group.filter((other) => keys.includes(other));
|
|
47
|
+
const differing = chosen.find((other) => other !== key && !byKey.get(other)?.identical);
|
|
48
|
+
if (differing) {
|
|
49
|
+
throw new CliError(`${key} and ${differing} are different ${assetKinds[asset.kind].many} with the same name, and BarnDog installs one copy in every tool.`, `barndog adopt --only ${key}`);
|
|
50
|
+
}
|
|
51
|
+
for (const member of group) {
|
|
52
|
+
const found = byKey.get(member);
|
|
53
|
+
if (found) {
|
|
54
|
+
planned.set(member, {
|
|
55
|
+
...found,
|
|
56
|
+
source: { key, harness: asset.harness },
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return [...planned.values()];
|
|
62
|
+
}
|
|
63
|
+
async function hasSharedWorkspace(home) {
|
|
64
|
+
const settings = await readSettings(home);
|
|
65
|
+
return Boolean(settings?.connection ?? settings?.gitWorkspace);
|
|
66
|
+
}
|
|
67
|
+
/** Same-named copies share one source, so each source's notes appear once. */
|
|
68
|
+
function adoptionNotes(plan, sourceOf, keep) {
|
|
69
|
+
const sources = [
|
|
70
|
+
...new Map(plan.map((asset) => [asset.source.key, asset])).values(),
|
|
71
|
+
];
|
|
72
|
+
return {
|
|
73
|
+
notes: sources.flatMap((asset) => sourceOf(asset).notes.map((text) => ({
|
|
74
|
+
kind: asset.kind,
|
|
75
|
+
name: asset.name,
|
|
76
|
+
text,
|
|
77
|
+
}))),
|
|
78
|
+
values: sources.flatMap((asset) => literalValues(asset.name, sourceOf(asset).literals, keep)),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/** Takes over the chosen assets; on any failure, puts back what it changed. */
|
|
82
|
+
export async function adoptNative(keys,
|
|
83
|
+
/** The MCP values BarnDog keeps on this machine (see `mcp env`). */
|
|
84
|
+
keep, runtime) {
|
|
85
|
+
const { home } = runtime;
|
|
86
|
+
return locked(runtime, async () => {
|
|
87
|
+
const unfinished = (await readAdoptions(home)).find((record) => record.status === "pending");
|
|
88
|
+
if (unfinished) {
|
|
89
|
+
throw new CliError("An earlier adoption didn't finish. Undo it before adopting more.", `barndog adopt undo ${unfinished.id}`);
|
|
90
|
+
}
|
|
91
|
+
const { found, skipped } = await findNativeAssets(runtime);
|
|
92
|
+
const plan = adoptionPlan({ assets: found, skipped }, keys);
|
|
93
|
+
if (!plan.length) {
|
|
94
|
+
throw new CliError("Choose at least one asset to adopt.", "barndog adopt");
|
|
95
|
+
}
|
|
96
|
+
const workspace = await ensureLocalWorkspace(runtime);
|
|
97
|
+
const id = randomUUID();
|
|
98
|
+
const paths = barndogPaths(home).adoption(id);
|
|
99
|
+
const byKey = new Map(found.map((asset) => [asset.key, asset]));
|
|
100
|
+
const scanned = (key) => defined(byKey.get(key), `The scan found no ${key}`);
|
|
101
|
+
const sourceOf = (asset) => scanned(asset.source.key);
|
|
102
|
+
const record = {
|
|
103
|
+
id,
|
|
104
|
+
createdAt: new Date().toISOString(),
|
|
105
|
+
status: "pending",
|
|
106
|
+
files: [],
|
|
107
|
+
items: plan.map((asset) => {
|
|
108
|
+
const { copy, original } = scanned(asset.key);
|
|
109
|
+
return {
|
|
110
|
+
key: asset.key,
|
|
111
|
+
harness: asset.harness,
|
|
112
|
+
kind: asset.kind,
|
|
113
|
+
name: asset.name,
|
|
114
|
+
description: asset.description,
|
|
115
|
+
copy: join(workspace.path, copy),
|
|
116
|
+
assetId: "",
|
|
117
|
+
fingerprint: "",
|
|
118
|
+
original,
|
|
119
|
+
backup: original.type === "folder"
|
|
120
|
+
? join(paths.skills, asset.harness, asset.name)
|
|
121
|
+
: undefined,
|
|
122
|
+
undone: false,
|
|
123
|
+
};
|
|
124
|
+
}),
|
|
125
|
+
};
|
|
126
|
+
// Undo removes every copy, so none may be there already.
|
|
127
|
+
for (const item of record.items) {
|
|
128
|
+
if ((await ifExists(lstat(item.copy))) !== null) {
|
|
129
|
+
throw new CliError(`${item.copy} already exists, so ${item.name} can't be adopted.`, "barndog adopt --dry-run");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// Written before anything changes, so undo can always find what moved.
|
|
133
|
+
await writeAdoption(record, home);
|
|
134
|
+
const values = { forget: async () => { } };
|
|
135
|
+
try {
|
|
136
|
+
const events = await apply({ record, plan, sourceOf, workspace, keep, values }, runtime);
|
|
137
|
+
return {
|
|
138
|
+
id,
|
|
139
|
+
adopted: plan,
|
|
140
|
+
backup: paths.folder,
|
|
141
|
+
...adoptionNotes(plan, sourceOf, keep),
|
|
142
|
+
events,
|
|
143
|
+
canShare: await hasSharedWorkspace(home),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
await restore(record, record.items, runtime).catch((undoError) => {
|
|
148
|
+
throw new CliError(`Adopting failed (${friendlyMessage(error)}), and putting things back failed too: ${friendlyMessage(undoError)}`, `barndog adopt undo ${id}`, { cause: error });
|
|
149
|
+
});
|
|
150
|
+
await values.forget();
|
|
151
|
+
throw error;
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
async function apply(input, runtime) {
|
|
156
|
+
const { home } = runtime;
|
|
157
|
+
const { record, plan, sourceOf, workspace } = input;
|
|
158
|
+
// One copy per name: tools that had the same asset share it.
|
|
159
|
+
const written = new Map();
|
|
160
|
+
for (const [index, asset] of plan.entries()) {
|
|
161
|
+
const item = defined(record.items[index], "Each planned asset has an item");
|
|
162
|
+
if (!written.has(item.copy)) {
|
|
163
|
+
await writeCopy(item.copy, sourceOf(asset), runtime);
|
|
164
|
+
written.set(item.copy, defined(await copyFingerprint(item, runtime), "The copy was written"));
|
|
165
|
+
}
|
|
166
|
+
item.fingerprint = defined(written.get(item.copy), "The copy was written");
|
|
167
|
+
}
|
|
168
|
+
const model = await localRepoSource(workspace, runtime).model();
|
|
169
|
+
for (const item of record.items) {
|
|
170
|
+
const asset = model.assets.find((candidate) => join(workspace.path, candidate.path) === item.copy);
|
|
171
|
+
if (!asset || asset.invalid) {
|
|
172
|
+
throw new CliError(`BarnDog couldn't read its copy of ${item.name}: ${asset?.invalid ?? "it's missing"}.`);
|
|
173
|
+
}
|
|
174
|
+
item.assetId = asset.id;
|
|
175
|
+
}
|
|
176
|
+
// Kept by asset ID, which exists only now; saved before the sync writes them.
|
|
177
|
+
// One source per name, and every item of it shares one copy and asset ID.
|
|
178
|
+
const servers = new Map(plan.flatMap((asset, index) => asset.kind === "mcp"
|
|
179
|
+
? [
|
|
180
|
+
[
|
|
181
|
+
asset.source.key,
|
|
182
|
+
{
|
|
183
|
+
id: defined(record.items[index], "Each planned asset has an item").assetId,
|
|
184
|
+
name: asset.name,
|
|
185
|
+
literals: sourceOf(asset).literals,
|
|
186
|
+
},
|
|
187
|
+
],
|
|
188
|
+
]
|
|
189
|
+
: []));
|
|
190
|
+
input.values.forget = await keepLiteralValues([...servers.values()], input.keep, runtime);
|
|
191
|
+
// A settings file is saved as the user left it, before BarnDog's first change.
|
|
192
|
+
for (const item of record.items) {
|
|
193
|
+
const { original } = item;
|
|
194
|
+
if (original.type === "entry" &&
|
|
195
|
+
!record.files.some((saved) => saved.file === original.file)) {
|
|
196
|
+
const backup = join(barndogPaths(home).adoption(record.id).settings, `${record.files.length + 1}-${basename(original.file)}`);
|
|
197
|
+
await mkdir(dirname(backup), { recursive: true });
|
|
198
|
+
await copyFile(original.file, backup);
|
|
199
|
+
record.files.push({ file: original.file, backup });
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
await writeAdoption(record, home);
|
|
203
|
+
for (const item of record.items) {
|
|
204
|
+
await takeOut(item.harness, item.original, item.backup);
|
|
205
|
+
}
|
|
206
|
+
const events = await synchronize(undefined, runtime, undefined, {
|
|
207
|
+
prompt: false,
|
|
208
|
+
});
|
|
209
|
+
const state = await readState(home);
|
|
210
|
+
const missing = record.items.filter((item) => !installed(item, state));
|
|
211
|
+
if (missing.length) {
|
|
212
|
+
const clashes = events.flatMap((event) => event.type === "clash" &&
|
|
213
|
+
missing.some((item) => item.name === event.skipped.name)
|
|
214
|
+
? [
|
|
215
|
+
`BarnDog already installs ${event.kind === "skill" ? "a Skill" : "an MCP server"} named ${event.installed.name} from ${event.installed.origin}.`,
|
|
216
|
+
]
|
|
217
|
+
: []);
|
|
218
|
+
throw new CliError(`BarnDog couldn't install ${missing.map((item) => item.key).join(", ")}, so nothing was adopted. ${clashes.join(" ")}`.trim(), "Rename yours in its coding tool, then run barndog adopt again");
|
|
219
|
+
}
|
|
220
|
+
record.status = "applied";
|
|
221
|
+
await writeAdoption(record, home);
|
|
222
|
+
return events;
|
|
223
|
+
}
|
|
224
|
+
async function writeCopy(path, asset, runtime) {
|
|
225
|
+
const { content } = asset;
|
|
226
|
+
// The workspace's own folders keep default permissions; writeAtomic would make them private.
|
|
227
|
+
await mkdir(dirname(path), { recursive: true });
|
|
228
|
+
if (content.kind === "mcp") {
|
|
229
|
+
await writeAtomic(path, stringify(content.definition), {
|
|
230
|
+
mode: POSIX_MODE.regularFile,
|
|
231
|
+
});
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
await writePackage(path, content.entries, runtime);
|
|
235
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** BarnDog's copy of an adopted asset: what it holds, and whether sync installed it. */
|
|
2
|
+
import { lstat, readFile } from "node:fs/promises";
|
|
3
|
+
import { basename } from "node:path";
|
|
4
|
+
import { packageFingerprint } from "@barndog-ai/api-contract/package";
|
|
5
|
+
import { ifExists } from "../../shared/files.js";
|
|
6
|
+
import { readDirectory } from "../workspace/directory.js";
|
|
7
|
+
/** What BarnDog's copy holds now, to tell later whether someone edited it. */
|
|
8
|
+
export async function copyFingerprint(item, runtime) {
|
|
9
|
+
if (item.kind === "mcp") {
|
|
10
|
+
const data = await ifExists(readFile(item.copy));
|
|
11
|
+
return data === null
|
|
12
|
+
? null
|
|
13
|
+
: packageFingerprint([
|
|
14
|
+
{ path: basename(item.copy), data, executable: false },
|
|
15
|
+
]);
|
|
16
|
+
}
|
|
17
|
+
return (await ifExists(lstat(item.copy))) === null
|
|
18
|
+
? null
|
|
19
|
+
: packageFingerprint(await readDirectory(item.copy, runtime));
|
|
20
|
+
}
|
|
21
|
+
/** Whether the last sync installed an adopted asset in the tool it came from. */
|
|
22
|
+
export function installed(item, state) {
|
|
23
|
+
if (item.kind === "skill") {
|
|
24
|
+
return state.links.some((link) => link.id === item.assetId && link.harness === item.harness);
|
|
25
|
+
}
|
|
26
|
+
return state.native.some((entry) => entry.id === item.assetId &&
|
|
27
|
+
entry.harness === item.harness &&
|
|
28
|
+
entry.kind === item.kind);
|
|
29
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Taking an asset out of its coding tool and putting it back. Each step checks
|
|
3
|
+
* the disk first, so running it again after a crash does only what is left.
|
|
4
|
+
*/
|
|
5
|
+
import { cp, lstat, mkdir, readlink, rename, rm, symlink, unlink, } from "node:fs/promises";
|
|
6
|
+
import { dirname } from "node:path";
|
|
7
|
+
import { isDeepStrictEqual } from "node:util";
|
|
8
|
+
import { parse as parseJsonc } from "jsonc-parser";
|
|
9
|
+
import { parse as parseToml } from "smol-toml";
|
|
10
|
+
import { CliError } from "../../errors.js";
|
|
11
|
+
import { adapters } from "../../harnesses/registry.js";
|
|
12
|
+
import { directoryLinkType } from "../../platform.js";
|
|
13
|
+
import { ifExists, readIfExists, writeAtomic } from "../../shared/files.js";
|
|
14
|
+
import { asRecord } from "../../shared/values.js";
|
|
15
|
+
import { errorCode } from "../../shared/values.js";
|
|
16
|
+
import { patchNative } from "../native/configuration.js";
|
|
17
|
+
/** A rename can't cross disks, so a folder on another one is copied, then removed. */
|
|
18
|
+
async function moveFolder(from, to) {
|
|
19
|
+
await mkdir(dirname(to), { recursive: true });
|
|
20
|
+
try {
|
|
21
|
+
await rename(from, to);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
if (errorCode(error) !== "EXDEV") {
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
await cp(from, to, { recursive: true, verbatimSymlinks: true });
|
|
28
|
+
await rm(from, { recursive: true, force: true });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const nativeEntry = (harness, original) => ({
|
|
32
|
+
id: "adoption",
|
|
33
|
+
kind: original.kind,
|
|
34
|
+
harness,
|
|
35
|
+
file: original.file,
|
|
36
|
+
key: original.key,
|
|
37
|
+
value: original.value,
|
|
38
|
+
});
|
|
39
|
+
/** Whether the tool's settings file holds this entry exactly as it was adopted. */
|
|
40
|
+
function holds(text, harness, original) {
|
|
41
|
+
if (text === null) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
const data = asRecord(original.file.endsWith(".toml") ? parseToml(text) : parseJsonc(text));
|
|
45
|
+
if (original.kind === "mcp") {
|
|
46
|
+
const servers = adapters[harness].mcp.path.reduce((value, key) => asRecord(value)[key], data);
|
|
47
|
+
return isDeepStrictEqual(asRecord(servers)[original.key], original.value);
|
|
48
|
+
}
|
|
49
|
+
const list = asRecord(data.hooks)[original.key];
|
|
50
|
+
return (Array.isArray(list) &&
|
|
51
|
+
list.some((entry) => isDeepStrictEqual(entry, original.value)));
|
|
52
|
+
}
|
|
53
|
+
/** Takes an asset out of its tool; a Skill folder moves to `backup`. */
|
|
54
|
+
export async function takeOut(harness, original, backup) {
|
|
55
|
+
switch (original.type) {
|
|
56
|
+
case "folder": {
|
|
57
|
+
if (!backup) {
|
|
58
|
+
throw new Error("A Skill folder is adopted without a backup path");
|
|
59
|
+
}
|
|
60
|
+
if ((await ifExists(lstat(original.path))) !== null) {
|
|
61
|
+
await moveFolder(original.path, backup);
|
|
62
|
+
}
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
case "link": {
|
|
66
|
+
if ((await ifExists(lstat(original.path))) !== null) {
|
|
67
|
+
await unlink(original.path);
|
|
68
|
+
}
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
case "entry": {
|
|
72
|
+
const text = await readIfExists(original.file);
|
|
73
|
+
if (holds(text, harness, original)) {
|
|
74
|
+
await writeAtomic(original.file, patchNative(text, [nativeEntry(harness, original)], []));
|
|
75
|
+
}
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** Puts an asset back where it was, refusing to replace anything that took its place. */
|
|
81
|
+
export async function putBack(harness, original, backup, runtime) {
|
|
82
|
+
const occupied = (path) => new CliError(`${path} exists again, so BarnDog can't put the adopted copy back there.`, "Move it aside, then run barndog adopt undo again");
|
|
83
|
+
switch (original.type) {
|
|
84
|
+
case "folder": {
|
|
85
|
+
const saved = backup && (await ifExists(lstat(backup)));
|
|
86
|
+
if (!backup || !saved) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if ((await ifExists(lstat(original.path))) !== null) {
|
|
90
|
+
throw occupied(original.path);
|
|
91
|
+
}
|
|
92
|
+
await moveFolder(backup, original.path);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
case "link": {
|
|
96
|
+
const existing = await ifExists(lstat(original.path));
|
|
97
|
+
if (existing) {
|
|
98
|
+
if (existing.isSymbolicLink() &&
|
|
99
|
+
(await readlink(original.path)) === original.target) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
throw occupied(original.path);
|
|
103
|
+
}
|
|
104
|
+
await mkdir(dirname(original.path), { recursive: true });
|
|
105
|
+
await symlink(original.target, original.path, directoryLinkType(runtime.platform));
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
case "entry": {
|
|
109
|
+
const text = await readIfExists(original.file);
|
|
110
|
+
if (!holds(text, harness, original)) {
|
|
111
|
+
await writeAtomic(original.file, patchNative(text, [], [nativeEntry(harness, original)]));
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The record of one `barndog adopt`, kept at
|
|
3
|
+
* `~/.barndog/adoptions/<id>/record.json` with the originals it moved aside.
|
|
4
|
+
* Undo reads it to put everything back, and checks each step against the disk
|
|
5
|
+
* so it can finish an adoption that stopped halfway.
|
|
6
|
+
*/
|
|
7
|
+
import { readdir } from "node:fs/promises";
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
import { barndogPaths } from "../../config/paths.js";
|
|
10
|
+
import { CliError } from "../../errors.js";
|
|
11
|
+
import { harnesses } from "../../harnesses/registry.js";
|
|
12
|
+
import { ifExists, readIfExists, writeAtomic } from "../../shared/files.js";
|
|
13
|
+
import { UUID_V4 } from "../../shared/patterns.js";
|
|
14
|
+
/** The kinds of asset a coding tool keeps that BarnDog can adopt. */
|
|
15
|
+
const nativeAssetKinds = ["skill", "mcp", "hook"];
|
|
16
|
+
const originalSchema = z.discriminatedUnion("type", [
|
|
17
|
+
/** A Skill folder of the tool's own, moved into the adoption's folder. */
|
|
18
|
+
z.strictObject({ type: z.literal("folder"), path: z.string() }),
|
|
19
|
+
/** A link the user made to a Skill kept elsewhere; its target is never touched. */
|
|
20
|
+
z.strictObject({
|
|
21
|
+
type: z.literal("link"),
|
|
22
|
+
path: z.string(),
|
|
23
|
+
target: z.string(),
|
|
24
|
+
}),
|
|
25
|
+
/** An MCP server or one hook event entry in a tool's settings file, as it was written there. */
|
|
26
|
+
z.strictObject({
|
|
27
|
+
type: z.literal("entry"),
|
|
28
|
+
kind: z.enum(["mcp", "hook"]),
|
|
29
|
+
file: z.string(),
|
|
30
|
+
/** The MCP server's name, or the hook's event. */
|
|
31
|
+
key: z.string(),
|
|
32
|
+
value: z.record(z.string(), z.unknown()),
|
|
33
|
+
}),
|
|
34
|
+
]);
|
|
35
|
+
const itemSchema = z.strictObject({
|
|
36
|
+
/** `claude/mcp/github`, as `--only` names it. */
|
|
37
|
+
key: z.string(),
|
|
38
|
+
harness: z.enum(harnesses),
|
|
39
|
+
kind: z.enum(nativeAssetKinds),
|
|
40
|
+
name: z.string(),
|
|
41
|
+
description: z.string(),
|
|
42
|
+
/** BarnDog's copy in the local folder; tools that had the same name share it. */
|
|
43
|
+
copy: z.string(),
|
|
44
|
+
assetId: z.string(),
|
|
45
|
+
/** The copy as adopted, so undo can tell whether it was edited since. */
|
|
46
|
+
fingerprint: z.string(),
|
|
47
|
+
original: originalSchema,
|
|
48
|
+
/** Where a moved Skill folder waits until undo puts it back. */
|
|
49
|
+
backup: z.string().optional(),
|
|
50
|
+
undone: z.boolean(),
|
|
51
|
+
});
|
|
52
|
+
const recordSchema = z.looseObject({
|
|
53
|
+
id: z.string().regex(UUID_V4),
|
|
54
|
+
createdAt: z.string(),
|
|
55
|
+
/** `pending` until every step finished; undo can finish or reverse one. */
|
|
56
|
+
status: z.enum(["pending", "applied", "undone"]),
|
|
57
|
+
/** Copies of each settings file taken before BarnDog first changed it. */
|
|
58
|
+
files: z.array(z.strictObject({ file: z.string(), backup: z.string() })),
|
|
59
|
+
items: z.array(itemSchema),
|
|
60
|
+
});
|
|
61
|
+
const recordFile = (id, home) => barndogPaths(home).adoption(id).record;
|
|
62
|
+
export async function writeAdoption(record, home) {
|
|
63
|
+
await writeAtomic(recordFile(record.id, home), JSON.stringify(record, null, 2) + "\n");
|
|
64
|
+
}
|
|
65
|
+
async function readAdoption(id, home) {
|
|
66
|
+
// Only an ID BarnDog made names a folder, so `../x` can't reach elsewhere.
|
|
67
|
+
const file = UUID_V4.test(id) ? recordFile(id, home) : null;
|
|
68
|
+
const text = file === null ? null : await readIfExists(file);
|
|
69
|
+
if (file === null || text === null) {
|
|
70
|
+
throw new CliError(`There's no adoption ${id}.`, "barndog adopt list");
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
return recordSchema.parse(JSON.parse(text));
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
throw new CliError(`${file} is damaged, so BarnDog can't tell what that adoption moved.`, "Undo any edit to that file, then try again", { cause: error });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** Every adoption on this machine, newest first. */
|
|
80
|
+
export async function readAdoptions(home) {
|
|
81
|
+
const folders = (await ifExists(readdir(barndogPaths(home).adoptions))) ?? [];
|
|
82
|
+
const records = await Promise.all(folders
|
|
83
|
+
.filter((name) => UUID_V4.test(name))
|
|
84
|
+
.map((id) => readAdoption(id, home)));
|
|
85
|
+
return records.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
|
|
86
|
+
}
|
|
87
|
+
/** The adoption `id` names, or the newest one that still has something to undo. */
|
|
88
|
+
export async function findAdoption(id, home) {
|
|
89
|
+
if (id) {
|
|
90
|
+
return readAdoption(id, home);
|
|
91
|
+
}
|
|
92
|
+
const open = (await readAdoptions(home)).find((record) => record.status !== "undone");
|
|
93
|
+
if (!open) {
|
|
94
|
+
throw new CliError("There's nothing to undo.", "barndog adopt list");
|
|
95
|
+
}
|
|
96
|
+
return open;
|
|
97
|
+
}
|
|
98
|
+
export function summary(record) {
|
|
99
|
+
return {
|
|
100
|
+
id: record.id,
|
|
101
|
+
createdAt: record.createdAt,
|
|
102
|
+
status: record.status,
|
|
103
|
+
items: record.items.map((item) => ({
|
|
104
|
+
key: item.key,
|
|
105
|
+
harness: item.harness,
|
|
106
|
+
kind: item.kind,
|
|
107
|
+
name: item.name,
|
|
108
|
+
description: item.description,
|
|
109
|
+
location: item.original.type === "entry"
|
|
110
|
+
? item.original.file
|
|
111
|
+
: item.original.path,
|
|
112
|
+
copy: item.copy,
|
|
113
|
+
undone: item.undone,
|
|
114
|
+
})),
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/** The adoptions on this machine, newest first, as `adopt list` shows them. */
|
|
118
|
+
export async function listAdoptions(home) {
|
|
119
|
+
return (await readAdoptions(home)).map(summary);
|
|
120
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { rm } from "node:fs/promises";
|
|
2
|
+
import { readState } from "../../config/state.js";
|
|
3
|
+
import { synchronize } from "../environment/sync.js";
|
|
4
|
+
import { installed } from "./copies.js";
|
|
5
|
+
import { putBack } from "./originals.js";
|
|
6
|
+
import { writeAdoption } from "./record.js";
|
|
7
|
+
/**
|
|
8
|
+
* Removes BarnDog's copies of `items`, syncs so its installations go, and puts
|
|
9
|
+
* the originals back. Safe to run again: each step checks what is still to do.
|
|
10
|
+
*/
|
|
11
|
+
export async function restore(record, items, runtime) {
|
|
12
|
+
const { home } = runtime;
|
|
13
|
+
for (const copy of new Set(items.map((item) => item.copy))) {
|
|
14
|
+
await rm(copy, { recursive: true, force: true });
|
|
15
|
+
}
|
|
16
|
+
const state = await readState(home);
|
|
17
|
+
if (items.some((item) => installed(item, state))) {
|
|
18
|
+
await synchronize(undefined, runtime, undefined, { prompt: false });
|
|
19
|
+
}
|
|
20
|
+
for (const item of items) {
|
|
21
|
+
await putBack(item.harness, item.original, item.backup, runtime);
|
|
22
|
+
item.undone = true;
|
|
23
|
+
}
|
|
24
|
+
record.status = record.items.every((item) => item.undone)
|
|
25
|
+
? "undone"
|
|
26
|
+
: "applied";
|
|
27
|
+
await writeAdoption(record, home);
|
|
28
|
+
}
|