barndog 0.1.0 → 0.2.0
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 +11 -10
- package/dist/cli.js +11 -7
- package/dist/commands/add.js +33 -21
- package/dist/commands/arguments.js +12 -0
- package/dist/commands/asset-install.js +160 -0
- package/dist/commands/asset-list.js +121 -0
- package/dist/commands/asset-share.js +94 -0
- package/dist/commands/auth.js +4 -0
- package/dist/commands/availability.js +54 -26
- package/dist/commands/capabilities.js +71 -415
- package/dist/commands/collaboration.js +194 -110
- package/dist/commands/environment.js +18 -411
- package/dist/commands/github.js +200 -0
- package/dist/commands/helpers.js +39 -0
- package/dist/commands/hooks.js +87 -0
- package/dist/commands/init.js +4 -2
- package/dist/commands/knowledge.js +73 -0
- package/dist/commands/mcp.js +62 -0
- package/dist/commands/origin.js +21 -23
- package/dist/commands/people.js +51 -0
- package/dist/commands/sets.js +141 -0
- package/dist/commands/status.js +34 -24
- package/dist/commands/sync.js +11 -4
- package/dist/commands/workspace.js +33 -19
- package/dist/config/environment.js +13 -27
- package/dist/config/paths.js +62 -0
- package/dist/config/selection.js +30 -0
- package/dist/config/settings.js +97 -108
- package/dist/config/state.js +57 -57
- package/dist/entry/knowledge-startup.js +32 -0
- package/dist/entry/mcp-launch.js +71 -0
- package/dist/entry/opencode-plugin.js +62 -0
- package/dist/{features/git-workspace/startup.js → entry/workspace-startup.js} +7 -2
- package/dist/errors.js +198 -55
- package/dist/features/asset-kinds/index.js +120 -0
- package/dist/features/asset-selection/index.js +51 -19
- package/dist/features/auth/credentials.js +80 -47
- package/dist/features/auth/session.js +80 -45
- package/dist/features/auth/transport.js +66 -15
- package/dist/features/catalog/index.js +32 -61
- package/dist/features/collaboration/api.js +160 -0
- package/dist/features/collaboration/index.js +321 -233
- package/dist/features/collaboration/remote.js +35 -115
- package/dist/features/collaboration/results.js +2 -0
- package/dist/features/collaboration/workspace-git.js +233 -149
- package/dist/features/collaboration/workspace-github.js +117 -54
- package/dist/features/collaboration/workspace-store.js +140 -100
- package/dist/features/collaboration/workspace.js +257 -172
- package/dist/features/discovery/index.js +68 -57
- package/dist/features/doctor/index.js +32 -24
- package/dist/features/environment/auto-sync.js +28 -27
- package/dist/features/environment/names.js +67 -47
- package/dist/features/environment/operations.js +321 -228
- package/dist/features/environment/own-entries.js +89 -0
- package/dist/features/environment/settle-names.js +91 -0
- package/dist/features/environment/stage.js +178 -0
- package/dist/features/environment/sync.js +113 -484
- package/dist/features/environment/transaction.js +179 -147
- package/dist/features/environment/writes.js +196 -0
- package/dist/features/github/account.js +25 -0
- package/dist/features/github/connections.js +119 -0
- package/dist/features/github/pulls.js +118 -0
- package/dist/features/hooks/adapt.js +32 -25
- package/dist/features/hooks/catalog.js +28 -0
- package/dist/features/hooks/compatibility.js +49 -0
- package/dist/features/hooks/divergence.js +9 -6
- package/dist/features/hooks/test.js +74 -49
- package/dist/features/hooks/types.js +1 -0
- package/dist/features/imports/discover.js +134 -61
- package/dist/features/imports/folder.js +48 -14
- package/dist/features/imports/local.js +113 -134
- package/dist/features/imports/remote.js +44 -25
- package/dist/features/inspection/index.js +17 -17
- package/dist/features/inventory/index.js +99 -110
- package/dist/features/knowledge/index.js +69 -49
- package/dist/features/mcps/catalog.js +31 -0
- package/dist/features/mcps/import.js +316 -0
- package/dist/features/mcps/index.js +98 -49
- package/dist/features/migration/migrate.js +263 -0
- package/dist/features/{git-workspace → migration}/move.js +165 -109
- package/dist/features/migration/promote.js +24 -0
- package/dist/features/native/adapters.js +53 -94
- package/dist/features/native/configuration.js +128 -82
- package/dist/features/native/hook-shell.js +55 -0
- package/dist/features/native/plugin-module.js +56 -0
- package/dist/features/onboarding/environment.js +67 -34
- package/dist/features/onboarding/setup.js +43 -40
- package/dist/features/repositories/context.js +30 -13
- package/dist/features/server/catalog.js +58 -0
- package/dist/features/server/client.js +13 -0
- package/dist/features/server/connection.js +37 -0
- package/dist/features/server/origin.js +74 -0
- package/dist/features/server/pages.js +24 -0
- package/dist/features/skills/catalog.js +42 -0
- package/dist/features/workspace/assets.js +57 -0
- package/dist/features/workspace/cloud-source.js +34 -0
- package/dist/features/workspace/directory.js +38 -0
- package/dist/features/{git-workspace → workspace}/format.js +114 -51
- package/dist/features/workspace/git.js +214 -0
- package/dist/features/workspace/installed.js +92 -0
- package/dist/features/{git-workspace → workspace}/overlay.js +43 -23
- package/dist/features/workspace/packages.js +160 -0
- package/dist/features/{git-workspace → workspace}/publish.js +120 -64
- package/dist/features/workspace/repo-source.js +192 -0
- package/dist/features/{git-workspace → workspace}/setup.js +60 -42
- package/dist/features/{git-workspace → workspace}/snapshot.js +53 -18
- package/dist/features/workspace/source.js +1 -0
- package/dist/features/workspace/tree.js +52 -0
- package/dist/features/workspace/workspace-source.js +27 -0
- package/dist/harnesses/claude.js +23 -1
- package/dist/harnesses/codex.js +57 -1
- package/dist/harnesses/cursor.js +22 -1
- package/dist/harnesses/detect.js +6 -50
- package/dist/harnesses/formats.js +45 -0
- package/dist/harnesses/opencode.js +111 -0
- package/dist/harnesses/registry.js +35 -2
- package/dist/platform.js +375 -0
- package/dist/program.js +225 -54
- package/dist/runtime.js +1 -0
- package/dist/shared/cli-error.js +22 -0
- package/dist/shared/constants.js +32 -0
- package/dist/shared/files.js +72 -0
- package/dist/shared/lock.js +49 -0
- package/dist/shared/lookup.js +11 -0
- package/dist/shared/patterns.js +94 -0
- package/dist/shared/shell.js +22 -0
- package/dist/shared/values.js +26 -0
- package/dist/ui/add-asset.js +41 -400
- package/dist/ui/add-hook.js +213 -0
- package/dist/ui/add-mcp.js +107 -0
- package/dist/ui/add-prompts.js +47 -0
- package/dist/ui/add-skills.js +115 -0
- package/dist/ui/asset-list.js +31 -13
- package/dist/ui/assets.js +33 -44
- package/dist/ui/auth.js +43 -33
- package/dist/ui/clash.js +11 -6
- package/dist/ui/collaboration.js +116 -44
- package/dist/ui/connection.js +10 -4
- package/dist/ui/decision-error.js +24 -0
- package/dist/ui/environment-onboarding.js +35 -13
- package/dist/ui/format.js +66 -18
- package/dist/ui/hook-compatibility.js +14 -6
- package/dist/ui/mcp.js +28 -3
- package/dist/ui/move.js +10 -5
- package/dist/ui/onboarding.js +113 -73
- package/dist/ui/options.js +9 -0
- package/dist/ui/output.js +106 -43
- package/dist/ui/pick.js +45 -33
- package/dist/ui/scope-install.js +15 -7
- package/dist/ui/selection.js +15 -11
- package/dist/ui/theme.js +37 -14
- package/dist/version.js +6 -3
- package/package.json +24 -11
- package/dist/features/capabilities/index.js +0 -106
- package/dist/features/collaboration/domain.js +0 -90
- package/dist/features/environment/cloud-source.js +0 -27
- package/dist/features/environment/installed.js +0 -124
- package/dist/features/environment/packages.js +0 -120
- package/dist/features/environment/remote.js +0 -71
- package/dist/features/environment/workspace-source.js +0 -19
- package/dist/features/git-hosts/index.js +0 -148
- package/dist/features/git-workspace/assets.js +0 -71
- package/dist/features/git-workspace/git.js +0 -135
- package/dist/features/git-workspace/migrate.js +0 -188
- package/dist/features/git-workspace/source.js +0 -159
- package/dist/features/imports/mcp.js +0 -219
- package/dist/features/knowledge/startup.js +0 -25
- package/dist/features/native/mcp-launch.js +0 -57
- package/dist/features/skills/install.js +0 -25
- package/dist/features/skills/remote.js +0 -177
- package/dist/features/skills/sync.js +0 -121
- package/dist/features/skills/validation.js +0 -34
- /package/dist/features/environment/{source.js → events.js} +0 -0
|
@@ -1,16 +1,21 @@
|
|
|
1
|
+
import { directoryLinkType } from "../../platform.js";
|
|
2
|
+
import { POSIX_MODE } from "../../shared/constants.js";
|
|
1
3
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
-
import { createHash
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
3
5
|
import { rmSync } from "node:fs";
|
|
4
|
-
import { lstat, mkdir,
|
|
6
|
+
import { lstat, mkdir, readlink, rm, symlink, unlink } from "node:fs/promises";
|
|
5
7
|
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
|
6
8
|
import { packageFingerprint } from "@barndog-ai/api-contract/package";
|
|
7
|
-
import {
|
|
8
|
-
import { skillDirectory, harnesses } from "../../harnesses/registry.js";
|
|
9
|
-
import { nativeFile } from "../native/configuration.js";
|
|
9
|
+
import { skillDirectory, harnesses, nativeFile, } from "../../harnesses/registry.js";
|
|
10
10
|
import { z } from "zod";
|
|
11
|
-
import { connectionContext } from "../
|
|
12
|
-
import {
|
|
13
|
-
import { desiredEntries,
|
|
11
|
+
import { connectionContext } from "../auth/session.js";
|
|
12
|
+
import { NAME } from "../../shared/patterns.js";
|
|
13
|
+
import { desiredEntries, mirrorTree } from "../workspace/installed.js";
|
|
14
|
+
import { treeFingerprint } from "../workspace/tree.js";
|
|
15
|
+
import { fileText, ifExists, writeAtomic } from "../../shared/files.js";
|
|
16
|
+
import { withPidLock } from "../../shared/lock.js";
|
|
17
|
+
import { CliError } from "../../errors.js";
|
|
18
|
+
import { barndogPaths } from "../../config/paths.js";
|
|
14
19
|
const writeSchema = z.union([
|
|
15
20
|
z
|
|
16
21
|
.object({
|
|
@@ -46,47 +51,26 @@ const journalSchema = z
|
|
|
46
51
|
})
|
|
47
52
|
.strict();
|
|
48
53
|
const locks = new AsyncLocalStorage();
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return await readFile(path, "utf8");
|
|
55
|
-
}
|
|
56
|
-
catch (error) {
|
|
57
|
-
if (error.code === "ENOENT")
|
|
58
|
-
return null;
|
|
59
|
-
throw error;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
54
|
+
/**
|
|
55
|
+
* Where the Skill link at `path` points, or null when nothing is there. Throws
|
|
56
|
+
* when something other than a symbolic link sits at `path`: BarnDog never takes
|
|
57
|
+
* over a folder it didn't create.
|
|
58
|
+
*/
|
|
62
59
|
export async function linkTarget(path) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
throw new Error(`Existing unmanaged or externally changed Skill: ${path}`);
|
|
67
|
-
return resolve(dirname(path), await readlink(path));
|
|
60
|
+
const info = await ifExists(lstat(path));
|
|
61
|
+
if (!info) {
|
|
62
|
+
return null;
|
|
68
63
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return null;
|
|
72
|
-
throw error;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
export async function atomicFile(path, text) {
|
|
76
|
-
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
|
|
77
|
-
const temp = `${path}.${randomUUID()}.tmp`;
|
|
78
|
-
try {
|
|
79
|
-
await writeFile(temp, text, { mode: 0o600, flag: "wx" });
|
|
80
|
-
await rename(temp, path);
|
|
81
|
-
}
|
|
82
|
-
finally {
|
|
83
|
-
await rm(temp, { force: true });
|
|
64
|
+
if (!info.isSymbolicLink()) {
|
|
65
|
+
throw new CliError(`${path} is a Skill folder BarnDog didn't install, so it won't replace it. Move or rename it, then sync again.`, "barndog sync");
|
|
84
66
|
}
|
|
67
|
+
return resolve(dirname(path), await readlink(path));
|
|
85
68
|
}
|
|
86
|
-
function validateWrites(writes,
|
|
69
|
+
function validateWrites(writes, runtime) {
|
|
70
|
+
const { home } = runtime;
|
|
87
71
|
const files = new Set([
|
|
88
|
-
|
|
89
|
-
|
|
72
|
+
barndogPaths(home).settings,
|
|
73
|
+
barndogPaths(home).state,
|
|
90
74
|
...harnesses.flatMap((h) => [
|
|
91
75
|
nativeFile(h, "mcp", home),
|
|
92
76
|
nativeFile(h, "hook", home),
|
|
@@ -94,69 +78,83 @@ function validateWrites(writes, home) {
|
|
|
94
78
|
]);
|
|
95
79
|
const paths = new Set();
|
|
96
80
|
for (const write of writes) {
|
|
97
|
-
if (paths.has(write.path) || resolve(write.path) !== write.path)
|
|
98
|
-
throw new Error("
|
|
81
|
+
if (paths.has(write.path) || resolve(write.path) !== write.path) {
|
|
82
|
+
throw new Error("A sync planned an invalid or duplicate destination");
|
|
83
|
+
}
|
|
99
84
|
paths.add(write.path);
|
|
100
85
|
if (write.kind === "tree") {
|
|
101
|
-
if (![
|
|
102
|
-
|
|
86
|
+
if (![
|
|
87
|
+
barndogPaths(home).installed("skill"),
|
|
88
|
+
barndogPaths(home).installed("hook"),
|
|
89
|
+
].includes(dirname(write.path)) ||
|
|
90
|
+
!NAME.test(basename(write.path)) ||
|
|
103
91
|
(write.source !== null && !isAbsolute(write.source)) ||
|
|
104
92
|
(write.after !== null) !== (write.source !== null) ||
|
|
105
|
-
(write.rename !== null && !
|
|
106
|
-
throw new Error("
|
|
93
|
+
(write.rename !== null && !NAME.test(write.rename))) {
|
|
94
|
+
throw new Error("A sync planned an invalid destination");
|
|
95
|
+
}
|
|
107
96
|
continue;
|
|
108
97
|
}
|
|
109
98
|
if (write.kind === "file"
|
|
110
99
|
? !files.has(write.path)
|
|
111
100
|
: !harnesses.some((h) => dirname(write.path) === skillDirectory(h, home)) ||
|
|
112
|
-
[write.before, write.after].some((v) => v !== null && !isAbsolute(v)))
|
|
113
|
-
throw new Error("
|
|
101
|
+
[write.before, write.after].some((v) => v !== null && !isAbsolute(v))) {
|
|
102
|
+
throw new Error("A sync planned an invalid destination");
|
|
103
|
+
}
|
|
114
104
|
}
|
|
115
105
|
}
|
|
116
|
-
async function current(write) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
106
|
+
async function current(write, runtime) {
|
|
107
|
+
if (write.kind === "file") {
|
|
108
|
+
return fileText(write.path);
|
|
109
|
+
}
|
|
110
|
+
if (write.kind === "link") {
|
|
111
|
+
return linkTarget(write.path);
|
|
112
|
+
}
|
|
113
|
+
return treeFingerprint(write.path, runtime);
|
|
122
114
|
}
|
|
123
|
-
async function applyTree(write) {
|
|
115
|
+
async function applyTree(write, runtime) {
|
|
124
116
|
if (write.source === null) {
|
|
125
117
|
await rm(write.path, { recursive: true, force: true });
|
|
126
118
|
return;
|
|
127
119
|
}
|
|
128
120
|
// A folder kept in place has no other copy to restore; the next sync reinstalls it.
|
|
129
|
-
if (write.source === write.path)
|
|
121
|
+
if (write.source === write.path) {
|
|
130
122
|
return;
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
123
|
+
}
|
|
124
|
+
const entries = await desiredEntries(write.source, write.rename, runtime);
|
|
125
|
+
if (packageFingerprint(entries) !== write.after) {
|
|
126
|
+
throw new CliError(`The downloaded copy in ${write.source} changed before it was installed.`, "barndog sync");
|
|
127
|
+
}
|
|
128
|
+
await mirrorTree(write.path, entries, runtime);
|
|
135
129
|
}
|
|
136
|
-
async function apply(writes,
|
|
137
|
-
validateWrites(writes,
|
|
130
|
+
async function apply(writes, runtime, checkpoint, recovering = false) {
|
|
131
|
+
validateWrites(writes, runtime);
|
|
138
132
|
// Check the entire recovery plan before writing any remaining destination.
|
|
139
133
|
for (const write of writes) {
|
|
140
134
|
// An interrupted in-place copy leaves a BarnDog-owned folder half written.
|
|
141
|
-
if (write.kind === "tree" && recovering)
|
|
135
|
+
if (write.kind === "tree" && recovering) {
|
|
142
136
|
continue;
|
|
143
|
-
|
|
137
|
+
}
|
|
138
|
+
const value = await current(write, runtime);
|
|
144
139
|
if (value !== write.before &&
|
|
145
140
|
value !== write.after &&
|
|
146
141
|
!(write.kind === "link" &&
|
|
147
142
|
value === null &&
|
|
148
143
|
write.before !== null &&
|
|
149
|
-
write.after !== null))
|
|
150
|
-
throw new
|
|
144
|
+
write.after !== null)) {
|
|
145
|
+
throw new CliError(`${write.path} changed after a sync was interrupted. Save your edits elsewhere, then sync again.`, "barndog sync");
|
|
146
|
+
}
|
|
151
147
|
}
|
|
152
148
|
for (const [index, write] of writes.entries()) {
|
|
153
|
-
const value = await current(write);
|
|
154
|
-
if (value === write.after)
|
|
149
|
+
const value = await current(write, runtime);
|
|
150
|
+
if (value === write.after) {
|
|
155
151
|
continue;
|
|
152
|
+
}
|
|
156
153
|
if (write.kind === "tree") {
|
|
157
|
-
if (value !== write.before && !recovering)
|
|
158
|
-
throw new
|
|
159
|
-
|
|
154
|
+
if (value !== write.before && !recovering) {
|
|
155
|
+
throw new CliError(`${write.path} changed while syncing.`, "barndog sync");
|
|
156
|
+
}
|
|
157
|
+
await applyTree(write, runtime);
|
|
160
158
|
await checkpoint?.(index);
|
|
161
159
|
continue;
|
|
162
160
|
}
|
|
@@ -164,121 +162,155 @@ async function apply(writes, home, checkpoint, recovering = false) {
|
|
|
164
162
|
!(write.kind === "link" &&
|
|
165
163
|
value === null &&
|
|
166
164
|
write.before !== null &&
|
|
167
|
-
write.after !== null))
|
|
168
|
-
throw new
|
|
169
|
-
|
|
165
|
+
write.after !== null)) {
|
|
166
|
+
throw new CliError(`${write.path} changed while syncing.`, "barndog sync");
|
|
167
|
+
}
|
|
168
|
+
await mkdir(dirname(write.path), {
|
|
169
|
+
recursive: true,
|
|
170
|
+
mode: POSIX_MODE.privateDirectory,
|
|
171
|
+
});
|
|
170
172
|
if (write.kind === "file") {
|
|
171
|
-
if (write.after === null)
|
|
173
|
+
if (write.after === null) {
|
|
172
174
|
await unlink(write.path);
|
|
173
|
-
|
|
174
|
-
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
await writeAtomic(write.path, write.after);
|
|
178
|
+
}
|
|
175
179
|
}
|
|
176
180
|
else {
|
|
177
|
-
if (value !== null)
|
|
181
|
+
if (value !== null) {
|
|
178
182
|
await unlink(write.path);
|
|
179
|
-
|
|
180
|
-
|
|
183
|
+
}
|
|
184
|
+
if (write.after !== null) {
|
|
185
|
+
await symlink(write.after, write.path, directoryLinkType(runtime.platform));
|
|
186
|
+
}
|
|
181
187
|
}
|
|
182
188
|
await checkpoint?.(index);
|
|
183
189
|
}
|
|
184
190
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
191
|
+
/**
|
|
192
|
+
* The journal an interrupted sync left. One that doesn't parse or names a
|
|
193
|
+
* destination BarnDog never writes was changed by hand or damaged, so it is
|
|
194
|
+
* kept and nothing is applied.
|
|
195
|
+
*/
|
|
196
|
+
function readJournal(text, runtime) {
|
|
197
|
+
try {
|
|
198
|
+
const journal = journalSchema.parse(JSON.parse(text));
|
|
199
|
+
validateWrites(journal.writes, runtime);
|
|
200
|
+
return journal;
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
throw new CliError(`${barndogPaths(runtime.home).pendingSync} is damaged, so BarnDog can't safely finish the interrupted sync.`, "Move that file aside (keep it), check the files it lists, then run barndog sync", { cause: error });
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Rolls forward an interrupted sync from ~/.barndog/pending-sync.json.
|
|
208
|
+
* Rejects a cloud journal when its authenticated organization or developer
|
|
209
|
+
* has changed.
|
|
210
|
+
*/
|
|
211
|
+
async function recover(runtime) {
|
|
212
|
+
const { home } = runtime;
|
|
213
|
+
const path = barndogPaths(home).pendingSync;
|
|
214
|
+
const text = await fileText(path);
|
|
215
|
+
if (text === null) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
const journal = readJournal(text, runtime);
|
|
190
219
|
if (journal.connection) {
|
|
191
|
-
const actual = await connectionContext(journal.connection.server,
|
|
220
|
+
const actual = await connectionContext(journal.connection.server, runtime);
|
|
192
221
|
if (actual.organizationId !== journal.connection.organizationId ||
|
|
193
|
-
actual.developerId !== journal.connection.developerId)
|
|
194
|
-
throw new
|
|
222
|
+
actual.developerId !== journal.connection.developerId) {
|
|
223
|
+
throw new CliError("An interrupted sync belongs to another organization or developer. Sign in as them to finish it.", "barndog login");
|
|
224
|
+
}
|
|
195
225
|
}
|
|
196
|
-
await apply(journal.writes,
|
|
226
|
+
await apply(journal.writes, runtime, undefined, true);
|
|
197
227
|
await unlink(path);
|
|
198
|
-
return true;
|
|
199
228
|
}
|
|
200
|
-
|
|
229
|
+
/**
|
|
230
|
+
* Applies `writes` as one journaled operation: records them in
|
|
231
|
+
* `~/.barndog/pending-sync.json`, applies them in order and removes the
|
|
232
|
+
* journal, so `recover()` can finish the job after a crash. Backs up the
|
|
233
|
+
* previous `config.yaml` under `~/.barndog/migrations/` before changing it.
|
|
234
|
+
* `checkpoint` runs after each write is applied. Call it inside `locked()`.
|
|
235
|
+
*/
|
|
236
|
+
export async function transact(runtime, writes, identity, checkpoint) {
|
|
237
|
+
const { home } = runtime;
|
|
201
238
|
writes = writes.filter((w) => w.before !== w.after);
|
|
202
|
-
if (!writes.length)
|
|
239
|
+
if (!writes.length) {
|
|
203
240
|
return;
|
|
204
|
-
|
|
205
|
-
|
|
241
|
+
}
|
|
242
|
+
validateWrites(writes, runtime);
|
|
243
|
+
const config = writes.find((w) => w.path === barndogPaths(home).settings);
|
|
206
244
|
if (config?.before) {
|
|
207
|
-
const backup = join(home
|
|
245
|
+
const backup = join(barndogPaths(home).migrations, `config-${createHash("sha256").update(config.before).digest("hex")}.yaml`);
|
|
208
246
|
const previous = await fileText(backup);
|
|
209
|
-
if (previous === null)
|
|
210
|
-
await
|
|
211
|
-
|
|
212
|
-
|
|
247
|
+
if (previous === null) {
|
|
248
|
+
await writeAtomic(backup, config.before);
|
|
249
|
+
}
|
|
250
|
+
else if (previous !== config.before) {
|
|
251
|
+
throw new CliError(`${backup} was changed after BarnDog saved it, so ${barndogPaths(home).settings} was left as it is.`, "Move that backup aside (keep it), then run the command again");
|
|
252
|
+
}
|
|
213
253
|
}
|
|
214
|
-
const path =
|
|
215
|
-
await
|
|
254
|
+
const path = barndogPaths(home).pendingSync;
|
|
255
|
+
await writeAtomic(path, JSON.stringify(identity && "workspaceId" in identity
|
|
216
256
|
? { version: 1, writes, workspaceId: identity.workspaceId }
|
|
217
257
|
: { version: 1, writes, connection: identity }));
|
|
218
|
-
await apply(writes,
|
|
258
|
+
await apply(writes, runtime, checkpoint);
|
|
219
259
|
await unlink(path);
|
|
220
260
|
}
|
|
221
261
|
const INTERRUPTS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
222
262
|
const interruptions = new Set();
|
|
223
263
|
function interrupted(signal) {
|
|
224
|
-
for (const cleanup of interruptions)
|
|
264
|
+
for (const cleanup of interruptions) {
|
|
225
265
|
cleanup();
|
|
266
|
+
}
|
|
226
267
|
interruptions.clear();
|
|
227
|
-
for (const name of INTERRUPTS)
|
|
268
|
+
for (const name of INTERRUPTS) {
|
|
228
269
|
process.removeListener(name, interrupted);
|
|
270
|
+
}
|
|
229
271
|
process.kill(process.pid, signal);
|
|
230
272
|
}
|
|
231
273
|
/** Runs `cleanup` if the process is told to stop; call the result once it's no longer needed. */
|
|
232
274
|
export function cleanUpIfInterrupted(cleanup) {
|
|
233
|
-
if (!interruptions.size)
|
|
234
|
-
for (const name of INTERRUPTS)
|
|
275
|
+
if (!interruptions.size) {
|
|
276
|
+
for (const name of INTERRUPTS) {
|
|
235
277
|
process.on(name, interrupted);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
236
280
|
interruptions.add(cleanup);
|
|
237
281
|
return () => {
|
|
238
282
|
interruptions.delete(cleanup);
|
|
239
|
-
if (!interruptions.size)
|
|
240
|
-
for (const name of INTERRUPTS)
|
|
283
|
+
if (!interruptions.size) {
|
|
284
|
+
for (const name of INTERRUPTS) {
|
|
241
285
|
process.removeListener(name, interrupted);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
242
288
|
};
|
|
243
289
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
290
|
+
/**
|
|
291
|
+
* Runs `run` holding the user-level sync lock, after rolling forward any
|
|
292
|
+
* interrupted operation. Nested calls for the same home reuse the lock.
|
|
293
|
+
*/
|
|
294
|
+
export async function locked(runtime, run) {
|
|
295
|
+
runtime = { ...runtime, home: resolve(runtime.cwd, runtime.home) };
|
|
296
|
+
const { home } = runtime;
|
|
297
|
+
if (locks.getStore()?.has(home)) {
|
|
247
298
|
return run();
|
|
248
|
-
const path = join(home, ".barndog/sync.lock");
|
|
249
|
-
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
|
|
250
|
-
try {
|
|
251
|
-
await mkdir(path);
|
|
252
299
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
300
|
+
const path = barndogPaths(home).syncLock;
|
|
301
|
+
return withPidLock(path, {
|
|
302
|
+
starting: `Another sync is starting. If an old process crashed, check and remove ${path}`,
|
|
303
|
+
running: "Another BarnDog environment operation is running",
|
|
304
|
+
}, async () => {
|
|
305
|
+
const release = cleanUpIfInterrupted(() => rmSync(path, { recursive: true, force: true }));
|
|
259
306
|
try {
|
|
260
|
-
|
|
307
|
+
return await locks.run(new Set([...(locks.getStore() ?? []), home]), async () => {
|
|
308
|
+
await recover(runtime);
|
|
309
|
+
return run();
|
|
310
|
+
});
|
|
261
311
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
throw failure;
|
|
265
|
-
await rm(path, { recursive: true });
|
|
266
|
-
return locked(home, run);
|
|
312
|
+
finally {
|
|
313
|
+
release();
|
|
267
314
|
}
|
|
268
|
-
|
|
269
|
-
cause: error,
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
const release = cleanUpIfInterrupted(() => rmSync(path, { recursive: true, force: true }));
|
|
273
|
-
try {
|
|
274
|
-
await writeFile(join(path, "pid"), String(process.pid), { mode: 0o600 });
|
|
275
|
-
return await locks.run(new Set([...(locks.getStore() ?? []), home]), async () => {
|
|
276
|
-
await recover(home);
|
|
277
|
-
return run();
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
finally {
|
|
281
|
-
release();
|
|
282
|
-
await rm(path, { recursive: true, force: true });
|
|
283
|
-
}
|
|
315
|
+
});
|
|
284
316
|
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { CliError } from "../../errors.js";
|
|
2
|
+
import { fileText } from "../../shared/files.js";
|
|
3
|
+
import { fixSteps, removeCommand } from "../../platform.js";
|
|
4
|
+
import { treeFingerprint } from "../workspace/tree.js";
|
|
5
|
+
import { assetKinds } from "../asset-kinds/index.js";
|
|
6
|
+
import { patchNative } from "../native/configuration.js";
|
|
7
|
+
import { inPluginModule, pluginModule } from "../native/plugin-module.js";
|
|
8
|
+
import { linkTarget } from "./transaction.js";
|
|
9
|
+
function changeType(change) {
|
|
10
|
+
if (!change.sameAsset) {
|
|
11
|
+
return "installed";
|
|
12
|
+
}
|
|
13
|
+
if (change.current && !change.contentChanged) {
|
|
14
|
+
return "unchanged";
|
|
15
|
+
}
|
|
16
|
+
return "updated";
|
|
17
|
+
}
|
|
18
|
+
/** Stops a sync that would overwrite an installed folder someone edited by hand. */
|
|
19
|
+
export async function assertInstalledUntouched(installed, settings, runtime) {
|
|
20
|
+
for (const entry of installed) {
|
|
21
|
+
const actual = await treeFingerprint(entry.path, runtime);
|
|
22
|
+
if (actual !== null && actual !== entry.fingerprint) {
|
|
23
|
+
throw new CliError(`The installed copy at ${entry.path} was edited by hand. Syncing would overwrite those edits. ${settings.localWorkspace &&
|
|
24
|
+
!settings.connection &&
|
|
25
|
+
!settings.gitWorkspace
|
|
26
|
+
? "To keep them, copy that folder somewhere else first."
|
|
27
|
+
: `To keep them, make an editable copy with barndog ${assetKinds[entry.kind].command} pull first.`}`, "Delete the installed copy; the next sync installs it again.", {
|
|
28
|
+
command: fixSteps([removeCommand(entry.path, runtime.platform), "barndog sync"], runtime.platform),
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* One write per installed folder, old or new, and the paths of the folders
|
|
35
|
+
* whose content changes (`updated`), which is what makes a link or Hook
|
|
36
|
+
* registration count as updated.
|
|
37
|
+
*/
|
|
38
|
+
export async function treeWrites(installed, trees, runtime) {
|
|
39
|
+
const writes = [];
|
|
40
|
+
const updated = new Set();
|
|
41
|
+
for (const treePath of new Set([
|
|
42
|
+
...installed.map((entry) => entry.path),
|
|
43
|
+
...trees.map((tree) => tree.path),
|
|
44
|
+
])) {
|
|
45
|
+
const next = trees.find((tree) => tree.path === treePath);
|
|
46
|
+
const before = await treeFingerprint(treePath, runtime);
|
|
47
|
+
const after = next?.fingerprint ?? null;
|
|
48
|
+
if (before !== null && after !== null && before !== after) {
|
|
49
|
+
updated.add(treePath);
|
|
50
|
+
}
|
|
51
|
+
writes.push({
|
|
52
|
+
kind: "tree",
|
|
53
|
+
path: treePath,
|
|
54
|
+
before,
|
|
55
|
+
after,
|
|
56
|
+
source: next?.source ?? null,
|
|
57
|
+
rename: next?.rename ?? null,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return { writes, updated };
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* One write per Skill link, old or new. Refuses a link changed outside BarnDog
|
|
64
|
+
* and a destination holding a Skill BarnDog didn't install.
|
|
65
|
+
*/
|
|
66
|
+
export async function linkWrites(previous, desired, updated) {
|
|
67
|
+
for (const link of previous) {
|
|
68
|
+
const actual = await linkTarget(link.target);
|
|
69
|
+
if (actual !== null && actual !== link.source) {
|
|
70
|
+
throw new CliError(`${link.target} was changed outside BarnDog. Restore or remove it, then sync again.`, "barndog sync");
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
for (const link of desired) {
|
|
74
|
+
const actual = await linkTarget(link.target);
|
|
75
|
+
const old = previous.find((l) => l.target === link.target);
|
|
76
|
+
if (actual !== null && !old) {
|
|
77
|
+
throw new CliError(`${link.target} is a Skill BarnDog didn't install, so it won't replace it. Move or rename it, then sync again.`, "barndog sync");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const writes = [];
|
|
81
|
+
const events = [];
|
|
82
|
+
for (const target of new Set([
|
|
83
|
+
...previous.map((l) => l.target),
|
|
84
|
+
...desired.map((l) => l.target),
|
|
85
|
+
])) {
|
|
86
|
+
const old = previous.find((l) => l.target === target);
|
|
87
|
+
const next = desired.find((l) => l.target === target);
|
|
88
|
+
const actual = await linkTarget(target);
|
|
89
|
+
if (old && (!next || next.id !== old.id)) {
|
|
90
|
+
events.push({ type: "removed", link: old });
|
|
91
|
+
}
|
|
92
|
+
if (next) {
|
|
93
|
+
events.push({
|
|
94
|
+
type: changeType({
|
|
95
|
+
sameAsset: old?.id === next.id,
|
|
96
|
+
current: actual === next.source,
|
|
97
|
+
contentChanged: updated.has(next.source),
|
|
98
|
+
}),
|
|
99
|
+
link: next,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
writes.push({
|
|
103
|
+
kind: "link",
|
|
104
|
+
path: target,
|
|
105
|
+
before: actual,
|
|
106
|
+
after: next?.source ?? null,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return { writes, events };
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* One write per native settings file that holds, or will hold, an entry of
|
|
113
|
+
* BarnDog's. `nameOf` gives the name an entry is reported under.
|
|
114
|
+
*/
|
|
115
|
+
export async function nativeWrites(previous, desired, context) {
|
|
116
|
+
const { trees, updated, nameOf, platform } = context;
|
|
117
|
+
const writes = [];
|
|
118
|
+
const events = [];
|
|
119
|
+
for (const file of new Set([
|
|
120
|
+
...previous.map((e) => e.file),
|
|
121
|
+
...desired.map((e) => e.file),
|
|
122
|
+
])) {
|
|
123
|
+
const old = previous.filter((e) => e.file === file);
|
|
124
|
+
const next = desired.filter((e) => e.file === file);
|
|
125
|
+
const text = await fileText(file);
|
|
126
|
+
writes.push({
|
|
127
|
+
kind: "file",
|
|
128
|
+
path: file,
|
|
129
|
+
before: text,
|
|
130
|
+
after: inPluginModule([...old, ...next])
|
|
131
|
+
? pluginModule(text, { before: old, after: next }, platform)
|
|
132
|
+
: patchNative(text, old, next),
|
|
133
|
+
});
|
|
134
|
+
for (const entry of next) {
|
|
135
|
+
events.push({
|
|
136
|
+
type: "native",
|
|
137
|
+
action: changeType({
|
|
138
|
+
sameAsset: old.some((e) => e.id === entry.id && e.harness === entry.harness),
|
|
139
|
+
current: old.some((e) => JSON.stringify(e) === JSON.stringify(entry)),
|
|
140
|
+
contentChanged: entry.kind === "hook" &&
|
|
141
|
+
trees.some((t) => t.id === entry.id && updated.has(t.path)),
|
|
142
|
+
}),
|
|
143
|
+
kind: entry.kind,
|
|
144
|
+
harness: entry.harness,
|
|
145
|
+
name: nameOf(entry),
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
for (const entry of old) {
|
|
149
|
+
if (!next.some((e) => JSON.stringify(e) === JSON.stringify(entry) ||
|
|
150
|
+
(e.id === entry.id && e.harness === entry.harness))) {
|
|
151
|
+
events.push({
|
|
152
|
+
type: "native",
|
|
153
|
+
action: "removed",
|
|
154
|
+
kind: entry.kind,
|
|
155
|
+
harness: entry.harness,
|
|
156
|
+
name: nameOf(entry),
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return { writes, events };
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* How a native entry is named in reports. Hook registrations carry IDs, so
|
|
165
|
+
* `known` maps an asset ID to its name or installed folder; a later pair wins.
|
|
166
|
+
*/
|
|
167
|
+
export function entryNames(known) {
|
|
168
|
+
const names = new Map(known);
|
|
169
|
+
return (entry) => {
|
|
170
|
+
if (entry.kind === "mcp") {
|
|
171
|
+
return entry.key;
|
|
172
|
+
}
|
|
173
|
+
if (entry.kind === "knowledge") {
|
|
174
|
+
return "knowledge index";
|
|
175
|
+
}
|
|
176
|
+
if (entry.kind === "sync") {
|
|
177
|
+
return "workspace sync";
|
|
178
|
+
}
|
|
179
|
+
return names.get(entry.id) ?? entry.id;
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
/** The text of `~/.barndog/state.json` once the plan is applied. */
|
|
183
|
+
export function stateText(plan) {
|
|
184
|
+
return (JSON.stringify({
|
|
185
|
+
version: 1,
|
|
186
|
+
links: plan.links,
|
|
187
|
+
native: plan.native,
|
|
188
|
+
installed: plan.trees.map(({ id, kind, path, fingerprint, versionId }) => ({
|
|
189
|
+
id,
|
|
190
|
+
kind,
|
|
191
|
+
path,
|
|
192
|
+
fingerprint,
|
|
193
|
+
versionId,
|
|
194
|
+
})),
|
|
195
|
+
}, null, 2) + "\n");
|
|
196
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { githubStatusSchema, gitAuthorizationSchema, gitInstallationSchema, } from "@barndog-ai/api-contract";
|
|
3
|
+
import { connection } from "../server/connection.js";
|
|
4
|
+
import { parsed } from "../server/client.js";
|
|
5
|
+
import { apiClient } from "../auth/transport.js";
|
|
6
|
+
export async function githubStatus(runtime) {
|
|
7
|
+
const conn = await connection(runtime);
|
|
8
|
+
return parsed(githubStatusSchema, apiClient(conn.server, runtime).GET("/v1/github/status"));
|
|
9
|
+
}
|
|
10
|
+
/** The page that links the developer's GitHub account to BarnDog. */
|
|
11
|
+
export async function githubAuth(runtime) {
|
|
12
|
+
const conn = await connection(runtime);
|
|
13
|
+
const { url } = await parsed(gitAuthorizationSchema, apiClient(conn.server, runtime).POST("/v1/github/authorize"));
|
|
14
|
+
return { url };
|
|
15
|
+
}
|
|
16
|
+
/** The page that installs the BarnDog GitHub App on an account or organization. */
|
|
17
|
+
export async function githubInstall(runtime) {
|
|
18
|
+
const conn = await connection(runtime);
|
|
19
|
+
const { url } = await parsed(gitAuthorizationSchema, apiClient(conn.server, runtime).POST("/v1/github/install"));
|
|
20
|
+
return { url };
|
|
21
|
+
}
|
|
22
|
+
export async function githubInstallations(runtime) {
|
|
23
|
+
const conn = await connection(runtime);
|
|
24
|
+
return parsed(z.array(gitInstallationSchema), apiClient(conn.server, runtime).GET("/v1/github/installations"));
|
|
25
|
+
}
|