barndog 0.2.0 → 0.2.1
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 +29 -27
- 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 +7 -2
- 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 +74 -0
- package/dist/commands/mcp-env.js +78 -0
- package/dist/commands/mcp.js +4 -2
- package/dist/commands/status.js +2 -1
- package/dist/commands/sync.js +2 -1
- package/dist/commands/workspace.js +43 -5
- package/dist/config/paths.js +19 -0
- package/dist/config/settings.js +5 -3
- package/dist/config/state.js +18 -6
- package/dist/features/adoption/adopt.js +234 -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 +4 -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 +23 -3
- package/dist/features/collaboration/workspace.js +22 -2
- package/dist/features/doctor/index.js +24 -2
- package/dist/features/environment/harnesses.js +36 -0
- package/dist/features/environment/moves.js +171 -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 +13 -2
- package/dist/features/inventory/show.js +74 -0
- package/dist/features/mcps/import.js +74 -21
- package/dist/features/mcps/index.js +12 -7
- package/dist/features/mcps/values.js +33 -0
- package/dist/features/migration/move.js +72 -17
- package/dist/features/migration/promote.js +2 -0
- package/dist/features/native/adapters.js +20 -6
- package/dist/features/native/configuration.js +8 -4
- package/dist/features/native/secret-values.js +74 -0
- package/dist/features/secrets/index.js +77 -0
- package/dist/features/workspace/format.js +13 -9
- 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 +19 -9
- package/dist/harnesses/cursor.js +2 -2
- package/dist/harnesses/formats.js +12 -5
- package/dist/harnesses/opencode.js +3 -6
- package/dist/platform.js +39 -11
- package/dist/program.js +51 -3
- 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 +69 -6
- package/dist/ui/add-skills.js +7 -5
- package/dist/ui/adopt.js +197 -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 +7 -2
- package/dist/ui/harness-picker.js +18 -0
- package/dist/ui/move.js +48 -2
- package/dist/ui/onboarding.js +40 -27
- package/dist/ui/output.js +3 -1
- package/dist/ui/package-details.js +34 -0
- package/dist/ui/scope-install.js +4 -1
- package/dist/ui/secret-input.js +19 -0
- package/dist/ui/selection.js +3 -1
- package/package.json +1 -1
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds the Skills, MCP servers and Hooks the user set up in each selected
|
|
3
|
+
* coding tool themselves, leaving out everything `state.json` says BarnDog
|
|
4
|
+
* installed. Nothing here changes a file.
|
|
5
|
+
*/
|
|
6
|
+
import { lstat, readFile, readdir, readlink, realpath } from "node:fs/promises";
|
|
7
|
+
import { basename, extname, isAbsolute, join, resolve } from "node:path";
|
|
8
|
+
import { isDeepStrictEqual } from "node:util";
|
|
9
|
+
import { hookImplementationSchema, } from "@barndog-ai/api-contract";
|
|
10
|
+
import { hookMetadata, packageFingerprint, validatePackageEntries, } from "@barndog-ai/api-contract/package";
|
|
11
|
+
import { parse as parseJsonc } from "jsonc-parser";
|
|
12
|
+
import { stringify } from "yaml";
|
|
13
|
+
import { readSettings, SetupRequiredError } from "../../config/settings.js";
|
|
14
|
+
import { readState } from "../../config/state.js";
|
|
15
|
+
import { CliError, friendlyMessage } from "../../errors.js";
|
|
16
|
+
import { adapters, hookHarnesses, nativeFile, skillDirectory, } from "../../harnesses/registry.js";
|
|
17
|
+
import { isWindows } from "../../platform.js";
|
|
18
|
+
import { ifExists, readIfExists } from "../../shared/files.js";
|
|
19
|
+
import { slugify, validAssetName } from "../../shared/patterns.js";
|
|
20
|
+
import { asRecord, isRecord } from "../../shared/values.js";
|
|
21
|
+
import { discoverSkills, ROOT_TOKEN } from "../imports/discover.js";
|
|
22
|
+
import { readFolder } from "../imports/folder.js";
|
|
23
|
+
import { renamedSkill } from "../imports/local.js";
|
|
24
|
+
import { convertServer, harnessServers } from "../mcps/import.js";
|
|
25
|
+
import { assetKinds } from "../asset-kinds/index.js";
|
|
26
|
+
import { assetPath, NAME_RULE } from "../workspace/format.js";
|
|
27
|
+
import { localMcpNamed } from "../mcps/import.js";
|
|
28
|
+
/** What can be adopted, without the file contents. */
|
|
29
|
+
export async function scanNative(runtime) {
|
|
30
|
+
const { found, skipped } = await findNativeAssets(runtime);
|
|
31
|
+
return { assets: found.map(publicAsset), skipped };
|
|
32
|
+
}
|
|
33
|
+
const publicAsset = (asset) => ({
|
|
34
|
+
key: asset.key,
|
|
35
|
+
harness: asset.harness,
|
|
36
|
+
kind: asset.kind,
|
|
37
|
+
name: asset.name,
|
|
38
|
+
description: asset.description,
|
|
39
|
+
location: asset.location,
|
|
40
|
+
sameName: asset.sameName,
|
|
41
|
+
identical: asset.identical,
|
|
42
|
+
notes: asset.notes,
|
|
43
|
+
values: asset.values,
|
|
44
|
+
});
|
|
45
|
+
/** Every adoptable asset in the selected tools, with its contents. */
|
|
46
|
+
export async function findNativeAssets(runtime) {
|
|
47
|
+
const { home } = runtime;
|
|
48
|
+
const settings = await readSettings(home);
|
|
49
|
+
if (!settings) {
|
|
50
|
+
throw new SetupRequiredError();
|
|
51
|
+
}
|
|
52
|
+
const state = await readState(home);
|
|
53
|
+
const selected = settings.harnesses;
|
|
54
|
+
const local = settings.localWorkspace?.path;
|
|
55
|
+
/** The local asset a candidate's name collides with; for an MCP server, ignoring case. */
|
|
56
|
+
const taken = async (kind, name) => {
|
|
57
|
+
if (kind === "mcp") {
|
|
58
|
+
return localMcpNamed(name, runtime);
|
|
59
|
+
}
|
|
60
|
+
const path = local && join(local, assetPath("org", kind, name));
|
|
61
|
+
return path && (await ifExists(lstat(path))) !== null ? name : null;
|
|
62
|
+
};
|
|
63
|
+
const skipped = [];
|
|
64
|
+
const candidates = [];
|
|
65
|
+
for (const harness of selected) {
|
|
66
|
+
const skills = await toolSkills(harness, state, runtime);
|
|
67
|
+
candidates.push(...skills.found);
|
|
68
|
+
skipped.push(...skills.skipped);
|
|
69
|
+
}
|
|
70
|
+
const servers = await toolServers(selected, runtime);
|
|
71
|
+
candidates.push(...servers.found);
|
|
72
|
+
skipped.push(...servers.skipped);
|
|
73
|
+
for (const harness of hookHarnesses) {
|
|
74
|
+
if (selected.includes(harness)) {
|
|
75
|
+
const hooks = await toolHooks(harness, state, runtime);
|
|
76
|
+
candidates.push(...hooks.found);
|
|
77
|
+
skipped.push(...hooks.skipped);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// Skill and MCP names come from the tool and can't change; a Hook's name is
|
|
81
|
+
// made up here, so it moves aside instead.
|
|
82
|
+
const kept = [];
|
|
83
|
+
for (const candidate of candidates) {
|
|
84
|
+
const existing = candidate.kind === "hook"
|
|
85
|
+
? null
|
|
86
|
+
: await taken(candidate.kind, candidate.name);
|
|
87
|
+
if (existing !== null) {
|
|
88
|
+
skipped.push({
|
|
89
|
+
...describe(candidate),
|
|
90
|
+
reason: existing === candidate.name
|
|
91
|
+
? `Your local folder already has ${assetKinds[candidate.kind].a} named ${candidate.name}.`
|
|
92
|
+
: `Your local folder has ${existing}, which differs only in case and would share its file on this disk.`,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
kept.push(candidate);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const hookNames = new Set();
|
|
100
|
+
for (const candidate of kept.filter((c) => c.kind === "hook")) {
|
|
101
|
+
let name = candidate.name;
|
|
102
|
+
for (let suffix = 2; hookNames.has(name) || (await taken("hook", name)); suffix++) {
|
|
103
|
+
name = `${candidate.name}-${suffix}`;
|
|
104
|
+
}
|
|
105
|
+
hookNames.add(name);
|
|
106
|
+
if (name !== candidate.name && candidate.content.kind === "hook") {
|
|
107
|
+
candidate.content = {
|
|
108
|
+
kind: "hook",
|
|
109
|
+
entries: renameHook(candidate.content.entries, name),
|
|
110
|
+
};
|
|
111
|
+
candidate.name = name;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return { found: compare(kept), skipped };
|
|
115
|
+
}
|
|
116
|
+
const describe = (asset) => ({
|
|
117
|
+
harness: asset.harness,
|
|
118
|
+
kind: asset.kind,
|
|
119
|
+
name: asset.name,
|
|
120
|
+
location: asset.location,
|
|
121
|
+
});
|
|
122
|
+
/** Fills in which other tools have an asset of the same kind and name, and whether it matches. */
|
|
123
|
+
function compare(candidates) {
|
|
124
|
+
return candidates.map((candidate) => {
|
|
125
|
+
const others = candidate.kind === "hook"
|
|
126
|
+
? []
|
|
127
|
+
: candidates.filter((other) => other !== candidate &&
|
|
128
|
+
other.kind === candidate.kind &&
|
|
129
|
+
other.name === candidate.name);
|
|
130
|
+
return {
|
|
131
|
+
...candidate,
|
|
132
|
+
key: assetKey(candidate),
|
|
133
|
+
copy: assetPath("org", candidate.kind, candidate.name),
|
|
134
|
+
sameName: others.map((other) => ({
|
|
135
|
+
key: assetKey(other),
|
|
136
|
+
harness: other.harness,
|
|
137
|
+
})),
|
|
138
|
+
identical: others.every((other) => sameContent(other.content, candidate.content)),
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
const assetKey = (asset) => `${asset.harness}/${asset.kind}/${asset.name}`;
|
|
143
|
+
function sameContent(left, right) {
|
|
144
|
+
if (left.kind === "mcp" || right.kind === "mcp") {
|
|
145
|
+
return (left.kind === "mcp" &&
|
|
146
|
+
right.kind === "mcp" &&
|
|
147
|
+
isDeepStrictEqual(left.definition, right.definition));
|
|
148
|
+
}
|
|
149
|
+
return packageFingerprint(left.entries) === packageFingerprint(right.entries);
|
|
150
|
+
}
|
|
151
|
+
/** Skill folders in one tool's Skill directory, followed through links the user made. */
|
|
152
|
+
async function toolSkills(harness, state, runtime) {
|
|
153
|
+
const directory = skillDirectory(harness, runtime.home);
|
|
154
|
+
const owned = new Set(state.links
|
|
155
|
+
.filter((link) => link.harness === harness)
|
|
156
|
+
.map((link) => resolve(link.target)));
|
|
157
|
+
const found = [];
|
|
158
|
+
const skipped = [];
|
|
159
|
+
const names = (await ifExists(readdir(directory))) ?? [];
|
|
160
|
+
for (const name of names.sort()) {
|
|
161
|
+
const path = join(directory, name);
|
|
162
|
+
// Hidden folders hold the tool's own Skills, such as Codex's `.system`.
|
|
163
|
+
if (name.startsWith(".") || owned.has(resolve(path))) {
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
const skill = await readSkill({ harness, name, path }, runtime);
|
|
167
|
+
if (skill && "reason" in skill) {
|
|
168
|
+
skipped.push(skill);
|
|
169
|
+
}
|
|
170
|
+
else if (skill) {
|
|
171
|
+
found.push(skill);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return { found, skipped };
|
|
175
|
+
}
|
|
176
|
+
/** One entry of a Skill directory: a Skill, why it can't be adopted, or null when it isn't a Skill. */
|
|
177
|
+
async function readSkill(entry, runtime) {
|
|
178
|
+
const { name, path } = entry;
|
|
179
|
+
const where = {
|
|
180
|
+
harness: entry.harness,
|
|
181
|
+
kind: "skill",
|
|
182
|
+
name,
|
|
183
|
+
location: path,
|
|
184
|
+
};
|
|
185
|
+
const link = (await lstat(path)).isSymbolicLink()
|
|
186
|
+
? await readlink(path)
|
|
187
|
+
: null;
|
|
188
|
+
const folder = link === null ? path : await ifExists(realpath(path));
|
|
189
|
+
if (folder === null) {
|
|
190
|
+
return {
|
|
191
|
+
...where,
|
|
192
|
+
reason: `It links to ${link ?? path}, which doesn't exist.`,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
if (!(await lstat(folder)).isDirectory() ||
|
|
196
|
+
(await ifExists(lstat(join(folder, "SKILL.md")))) === null) {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
if (!validAssetName(name)) {
|
|
200
|
+
return {
|
|
201
|
+
...where,
|
|
202
|
+
reason: `${name} can't be a BarnDog name. ${NAME_RULE}.`,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
const { skills, skipped } = discoverSkills(await readFolder(folder, runtime));
|
|
206
|
+
const skill = skills.find((candidate) => candidate.root === "");
|
|
207
|
+
if (!skill) {
|
|
208
|
+
return {
|
|
209
|
+
...where,
|
|
210
|
+
reason: skipped.find((problem) => problem.path === "SKILL.md")?.reason ??
|
|
211
|
+
"It isn't a valid Skill.",
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
return {
|
|
215
|
+
...where,
|
|
216
|
+
description: skill.description,
|
|
217
|
+
notes: [],
|
|
218
|
+
literals: [],
|
|
219
|
+
values: [],
|
|
220
|
+
content: { kind: "skill", entries: renamedSkill(skill, name) },
|
|
221
|
+
original: link === null
|
|
222
|
+
? { type: "folder", path }
|
|
223
|
+
: { type: "link", path, target: link },
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
/** MCP servers configured for the whole user, not for one project. */
|
|
227
|
+
async function toolServers(selected, runtime) {
|
|
228
|
+
const found = [];
|
|
229
|
+
const skipped = [];
|
|
230
|
+
for (const server of await harnessServers(runtime)) {
|
|
231
|
+
if (!selected.includes(server.harness) ||
|
|
232
|
+
server.label.includes("project")) {
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
const asset = {
|
|
236
|
+
harness: server.harness,
|
|
237
|
+
kind: "mcp",
|
|
238
|
+
name: server.name,
|
|
239
|
+
location: server.file,
|
|
240
|
+
};
|
|
241
|
+
if (!assetKinds.mcp.validName(server.name)) {
|
|
242
|
+
skipped.push({
|
|
243
|
+
...asset,
|
|
244
|
+
reason: `${server.name} can't be a BarnDog name. ${assetKinds.mcp.nameRule}.`,
|
|
245
|
+
});
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
// OpenCode reads several files; one name in two of them is two servers.
|
|
249
|
+
const twice = found.find((other) => other.harness === server.harness && other.name === server.name);
|
|
250
|
+
if (twice) {
|
|
251
|
+
skipped.push({
|
|
252
|
+
...asset,
|
|
253
|
+
reason: `${twice.location} also configures ${server.name}.`,
|
|
254
|
+
});
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
try {
|
|
258
|
+
const converted = convertServer(server);
|
|
259
|
+
found.push({
|
|
260
|
+
...asset,
|
|
261
|
+
description: converted.definition.description,
|
|
262
|
+
notes: converted.notes,
|
|
263
|
+
literals: converted.literals,
|
|
264
|
+
values: converted.literals.map(({ key, variable }) => ({
|
|
265
|
+
key,
|
|
266
|
+
variable,
|
|
267
|
+
})),
|
|
268
|
+
content: { kind: "mcp", definition: converted.definition },
|
|
269
|
+
original: {
|
|
270
|
+
type: "entry",
|
|
271
|
+
kind: "mcp",
|
|
272
|
+
file: server.file,
|
|
273
|
+
key: server.name,
|
|
274
|
+
value: server.native,
|
|
275
|
+
},
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
skipped.push({ ...asset, reason: friendlyMessage(error) });
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return { found, skipped };
|
|
283
|
+
}
|
|
284
|
+
/** The hook entries in one tool's user settings file, one Hook per event entry. */
|
|
285
|
+
async function toolHooks(harness, state, runtime) {
|
|
286
|
+
const file = nativeFile(harness, "hook", runtime.home);
|
|
287
|
+
const found = [];
|
|
288
|
+
const skipped = [];
|
|
289
|
+
const text = await readIfExists(file);
|
|
290
|
+
if (text === null) {
|
|
291
|
+
return { found, skipped };
|
|
292
|
+
}
|
|
293
|
+
const owned = state.native.filter((entry) => entry.harness === harness && entry.file === file);
|
|
294
|
+
for (const [event, groups] of Object.entries(asRecord(asRecord(parseJsonc(text)).hooks))) {
|
|
295
|
+
if (!Array.isArray(groups)) {
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
for (const group of groups.filter(isRecord)) {
|
|
299
|
+
if (owned.some((entry) => entry.key === event && isDeepStrictEqual(entry.value, group))) {
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
const where = { harness, kind: "hook", location: file };
|
|
303
|
+
const label = `${event} hook`;
|
|
304
|
+
// Undo and adoption find an entry by its value, so it must be unique.
|
|
305
|
+
if (groups.filter((other) => isDeepStrictEqual(other, group)).length > 1) {
|
|
306
|
+
skipped.push({
|
|
307
|
+
...where,
|
|
308
|
+
name: label,
|
|
309
|
+
reason: `${file} lists the same ${event} hook twice. Remove one copy first.`,
|
|
310
|
+
});
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
try {
|
|
314
|
+
const hook = await hookPackage({ harness, event, group }, runtime);
|
|
315
|
+
found.push({
|
|
316
|
+
...where,
|
|
317
|
+
name: hook.name,
|
|
318
|
+
description: hook.description,
|
|
319
|
+
notes: hook.notes,
|
|
320
|
+
literals: [],
|
|
321
|
+
values: [],
|
|
322
|
+
content: { kind: "hook", entries: hook.entries },
|
|
323
|
+
original: {
|
|
324
|
+
type: "entry",
|
|
325
|
+
kind: "hook",
|
|
326
|
+
file,
|
|
327
|
+
key: event,
|
|
328
|
+
value: group,
|
|
329
|
+
},
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
catch (error) {
|
|
333
|
+
skipped.push({ ...where, name: label, reason: friendlyMessage(error) });
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return { found, skipped };
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* The absolute paths a hook command line mentions, with the word each came
|
|
341
|
+
* from: `"$HOME/.claude/hooks/x.sh" --fix` → `$HOME/.claude/hooks/x.sh` as
|
|
342
|
+
* `<home>/.claude/hooks/x.sh`. `~/`, `$HOME/` and `${HOME}/` count as the home
|
|
343
|
+
* folder; relative paths name nothing, since the tool's working folder varies.
|
|
344
|
+
*
|
|
345
|
+
* @internal Exported for its table test in `src/tests/patterns.test.ts`.
|
|
346
|
+
*/
|
|
347
|
+
export function commandPaths(command, home) {
|
|
348
|
+
const words = [];
|
|
349
|
+
for (const raw of command.split(" ")) {
|
|
350
|
+
const quote = raw.at(0);
|
|
351
|
+
const word = raw.length > 1 && (quote === '"' || quote === "'") && raw.endsWith(quote)
|
|
352
|
+
? raw.slice(1, -1)
|
|
353
|
+
: raw;
|
|
354
|
+
const prefix = ["~/", "$HOME/", "${HOME}/"].find((p) => word.startsWith(p));
|
|
355
|
+
const path = prefix ? join(home, word.slice(prefix.length)) : word;
|
|
356
|
+
if (path && isAbsolute(path)) {
|
|
357
|
+
words.push({ word, path });
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return words;
|
|
361
|
+
}
|
|
362
|
+
/** The first script a command runs, copied into the package, and the command pointed at it. */
|
|
363
|
+
async function packagedCommand(command, scriptName, runtime) {
|
|
364
|
+
for (const { word, path } of commandPaths(command, runtime.home)) {
|
|
365
|
+
const info = await ifExists(lstat(path));
|
|
366
|
+
if (info?.isFile()) {
|
|
367
|
+
const file = basename(path);
|
|
368
|
+
return {
|
|
369
|
+
command: command.replace(word, () => `${ROOT_TOKEN}/${file}`),
|
|
370
|
+
script: { path: file, data: await readFile(path) },
|
|
371
|
+
from: path,
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
// A command with no script file becomes one; that needs a POSIX shell.
|
|
376
|
+
if (isWindows(runtime.platform)) {
|
|
377
|
+
throw new CliError(`"${command}" doesn't run a script file, and on Windows BarnDog only adopts hooks that do.`);
|
|
378
|
+
}
|
|
379
|
+
return {
|
|
380
|
+
command: `${ROOT_TOKEN}/${scriptName}`,
|
|
381
|
+
script: {
|
|
382
|
+
path: scriptName,
|
|
383
|
+
data: Buffer.from(`#!/bin/sh\n${command}\n`),
|
|
384
|
+
},
|
|
385
|
+
from: undefined,
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Hook names are made from a script's name; this leaves room for a `-2`
|
|
390
|
+
* suffix within the 64-character asset name limit.
|
|
391
|
+
*/
|
|
392
|
+
const HOOK_NAME_LENGTH = 60;
|
|
393
|
+
/** Each command handler pointed at a script inside the package. */
|
|
394
|
+
async function packageHandlers(handlers, event, runtime) {
|
|
395
|
+
const scripts = new Map();
|
|
396
|
+
const copied = [];
|
|
397
|
+
const rewritten = [];
|
|
398
|
+
for (const [index, handler] of handlers.entries()) {
|
|
399
|
+
if (!isRecord(handler) ||
|
|
400
|
+
typeof handler.command !== "string" ||
|
|
401
|
+
(handler.type ?? "command") !== "command") {
|
|
402
|
+
const type = asRecord(handler).type;
|
|
403
|
+
throw new CliError(`It runs a ${typeof type === "string" ? type : "non-command"} hook, and BarnDog adopts only command hooks.`);
|
|
404
|
+
}
|
|
405
|
+
const packaged = await packagedCommand(handler.command, `${slugify(event) || "hook"}-${index + 1}.sh`, runtime);
|
|
406
|
+
const already = scripts.get(packaged.script.path);
|
|
407
|
+
if (already && !already.equals(packaged.script.data)) {
|
|
408
|
+
throw new CliError(`It runs two different scripts named ${packaged.script.path}.`);
|
|
409
|
+
}
|
|
410
|
+
scripts.set(packaged.script.path, packaged.script.data);
|
|
411
|
+
if (packaged.from) {
|
|
412
|
+
copied.push(packaged.from);
|
|
413
|
+
}
|
|
414
|
+
rewritten.push({ ...handler, command: packaged.command });
|
|
415
|
+
}
|
|
416
|
+
return { scripts, copied, rewritten };
|
|
417
|
+
}
|
|
418
|
+
/** A Hook package that runs what one native hook entry ran. */
|
|
419
|
+
async function hookPackage(input, runtime) {
|
|
420
|
+
const { harness, event, group } = input;
|
|
421
|
+
const nested = Array.isArray(group.hooks);
|
|
422
|
+
const { scripts, copied, rewritten } = await packageHandlers(Array.isArray(group.hooks) ? group.hooks : [group], event, runtime);
|
|
423
|
+
// Named after the script it runs, else after its event.
|
|
424
|
+
const [script] = copied;
|
|
425
|
+
const name = slugify(script ? basename(script, extname(script)) : event).slice(0, HOOK_NAME_LENGTH) || "hook";
|
|
426
|
+
// The home-relative path, since a description may be shared beyond this machine.
|
|
427
|
+
const description = `${adapters[harness].name} ${event} hook from ~/${adapters[harness].hooks.file}`;
|
|
428
|
+
const events = hookImplementationSchema.shape.events.safeParse({
|
|
429
|
+
[event]: [nested ? { ...group, hooks: rewritten } : rewritten[0]],
|
|
430
|
+
});
|
|
431
|
+
if (!events.success) {
|
|
432
|
+
throw new CliError(`It uses settings a BarnDog Hook can't hold: ${events.error.issues.map((issue) => issue.path.join(".")).join(", ")}.`);
|
|
433
|
+
}
|
|
434
|
+
const manifest = {
|
|
435
|
+
name,
|
|
436
|
+
description,
|
|
437
|
+
implementations: {
|
|
438
|
+
[harness]: {
|
|
439
|
+
compatibility: "supported",
|
|
440
|
+
scripts: [...scripts.keys()],
|
|
441
|
+
requirements: { commands: [] },
|
|
442
|
+
events: events.data,
|
|
443
|
+
},
|
|
444
|
+
},
|
|
445
|
+
};
|
|
446
|
+
const entries = hookEntries(manifest, scripts);
|
|
447
|
+
return {
|
|
448
|
+
name,
|
|
449
|
+
description,
|
|
450
|
+
entries,
|
|
451
|
+
notes: copied.map((path) => `BarnDog runs its own copy of ${path}. The original stays, but edits to it no longer reach this hook.`),
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
function hookEntries(manifest, scripts) {
|
|
455
|
+
const entries = [
|
|
456
|
+
...[...scripts].map(([path, data]) => ({ path, data, executable: true })),
|
|
457
|
+
{
|
|
458
|
+
path: "hook.yaml",
|
|
459
|
+
data: Buffer.from(stringify(manifest)),
|
|
460
|
+
executable: false,
|
|
461
|
+
},
|
|
462
|
+
];
|
|
463
|
+
hookMetadata(validatePackageEntries(entries, undefined, "hook.yaml"));
|
|
464
|
+
return entries;
|
|
465
|
+
}
|
|
466
|
+
/** The same Hook package under another name, for when the first choice is taken. */
|
|
467
|
+
function renameHook(entries, name) {
|
|
468
|
+
const scripts = new Map();
|
|
469
|
+
let manifest;
|
|
470
|
+
for (const entry of entries) {
|
|
471
|
+
if (entry.path === "hook.yaml") {
|
|
472
|
+
manifest = hookMetadata(entries);
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
scripts.set(entry.path, entry.data);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
if (!manifest) {
|
|
479
|
+
throw new Error("An adopted Hook package has no hook.yaml");
|
|
480
|
+
}
|
|
481
|
+
return hookEntries({ ...manifest, name }, scripts);
|
|
482
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** `barndog adopt undo`: put adopted assets back in their coding tools. */
|
|
2
|
+
import { CliError } from "../../errors.js";
|
|
3
|
+
import { locked } from "../environment/transaction.js";
|
|
4
|
+
import { copyFingerprint } from "./copies.js";
|
|
5
|
+
import { findAdoption, summary } from "./record.js";
|
|
6
|
+
import { restore } from "./restore.js";
|
|
7
|
+
export async function undoAdoption(target, runtime) {
|
|
8
|
+
const { home } = runtime;
|
|
9
|
+
return locked(runtime, async () => {
|
|
10
|
+
const record = await findAdoption(target.id, home);
|
|
11
|
+
const open = record.items.filter((item) => !item.undone);
|
|
12
|
+
const unknown = (target.only ?? []).filter((key) => !open.some((item) => item.key === key));
|
|
13
|
+
if (unknown.length) {
|
|
14
|
+
throw new CliError(`Adoption ${record.id} has nothing to undo for ${unknown.join(", ")}.`, "barndog adopt list");
|
|
15
|
+
}
|
|
16
|
+
if (target.only && record.status === "pending") {
|
|
17
|
+
throw new CliError(`Adoption ${record.id} didn't finish, so it can only be undone as a whole.`, `barndog adopt undo ${record.id}`);
|
|
18
|
+
}
|
|
19
|
+
const chosen = target.only
|
|
20
|
+
? open.filter((item) => open.some((picked) => target.only?.includes(picked.key) && picked.copy === item.copy))
|
|
21
|
+
: open;
|
|
22
|
+
if (!chosen.length) {
|
|
23
|
+
throw new CliError(`Adoption ${record.id} was already undone.`, "barndog adopt list");
|
|
24
|
+
}
|
|
25
|
+
// A pending adoption may have stopped before its copies were fingerprinted.
|
|
26
|
+
if (!target.force && record.status !== "pending") {
|
|
27
|
+
for (const item of chosen) {
|
|
28
|
+
const now = await copyFingerprint(item, runtime);
|
|
29
|
+
if (now !== null && now !== item.fingerprint) {
|
|
30
|
+
throw new CliError(`${item.copy} changed since ${item.name} was adopted, and undoing would lose those changes.`, "Save your changes somewhere else, then undo with --force", { command: `barndog adopt undo ${record.id} --force` });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
await restore(record, chosen, runtime);
|
|
35
|
+
const { items } = summary(record);
|
|
36
|
+
return {
|
|
37
|
+
id: record.id,
|
|
38
|
+
restored: items.filter((item) => chosen.some((c) => c.key === item.key)),
|
|
39
|
+
remaining: items.filter((item) => !item.undone),
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
}
|