barndog 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +117 -2
- package/dist/cli.js +40 -0
- package/dist/commands/add.js +99 -0
- package/dist/commands/auth.js +25 -0
- package/dist/commands/availability.js +87 -0
- package/dist/commands/capabilities.js +488 -0
- package/dist/commands/collaboration.js +203 -0
- package/dist/commands/context.js +1 -0
- package/dist/commands/environment.js +416 -0
- package/dist/commands/init.js +20 -0
- package/dist/commands/origin.js +48 -0
- package/dist/commands/status.js +66 -0
- package/dist/commands/sync.js +30 -0
- package/dist/commands/workspace.js +84 -0
- package/dist/config/build.js +2 -0
- package/dist/config/environment.js +41 -0
- package/dist/config/settings.js +127 -0
- package/dist/config/state.js +70 -0
- package/dist/errors.js +154 -0
- package/dist/features/asset-selection/index.js +212 -0
- package/dist/features/asset-selection/types.js +1 -0
- package/dist/features/auth/credentials.js +62 -0
- package/dist/features/auth/session.js +120 -0
- package/dist/features/auth/transport.js +44 -0
- package/dist/features/capabilities/index.js +106 -0
- package/dist/features/catalog/index.js +102 -0
- package/dist/features/collaboration/domain.js +90 -0
- package/dist/features/collaboration/index.js +254 -0
- package/dist/features/collaboration/remote.js +121 -0
- package/dist/features/collaboration/workspace-git.js +362 -0
- package/dist/features/collaboration/workspace-github.js +130 -0
- package/dist/features/collaboration/workspace-store.js +256 -0
- package/dist/features/collaboration/workspace.js +391 -0
- package/dist/features/discovery/index.js +66 -0
- package/dist/features/doctor/index.js +154 -0
- package/dist/features/environment/auto-sync.js +96 -0
- package/dist/features/environment/cloud-source.js +27 -0
- package/dist/features/environment/installed.js +124 -0
- package/dist/features/environment/names.js +153 -0
- package/dist/features/environment/operations.js +277 -0
- package/dist/features/environment/packages.js +120 -0
- package/dist/features/environment/remote.js +71 -0
- package/dist/features/environment/source.js +1 -0
- package/dist/features/environment/sync.js +547 -0
- package/dist/features/environment/transaction.js +284 -0
- package/dist/features/environment/workspace-source.js +19 -0
- package/dist/features/git-hosts/index.js +148 -0
- package/dist/features/git-workspace/assets.js +71 -0
- package/dist/features/git-workspace/format.js +310 -0
- package/dist/features/git-workspace/git.js +135 -0
- package/dist/features/git-workspace/migrate.js +188 -0
- package/dist/features/git-workspace/move.js +337 -0
- package/dist/features/git-workspace/overlay.js +131 -0
- package/dist/features/git-workspace/publish.js +172 -0
- package/dist/features/git-workspace/setup.js +135 -0
- package/dist/features/git-workspace/snapshot.js +105 -0
- package/dist/features/git-workspace/source.js +159 -0
- package/dist/features/git-workspace/startup.js +16 -0
- package/dist/features/hooks/adapt.js +103 -0
- package/dist/features/hooks/divergence.js +38 -0
- package/dist/features/hooks/test.js +114 -0
- package/dist/features/imports/discover.js +249 -0
- package/dist/features/imports/folder.js +52 -0
- package/dist/features/imports/local.js +242 -0
- package/dist/features/imports/mcp.js +219 -0
- package/dist/features/imports/remote.js +85 -0
- package/dist/features/inspection/index.js +42 -0
- package/dist/features/inventory/index.js +224 -0
- package/dist/features/knowledge/index.js +118 -0
- package/dist/features/knowledge/startup.js +25 -0
- package/dist/features/mcps/index.js +102 -0
- package/dist/features/native/adapters.js +143 -0
- package/dist/features/native/configuration.js +196 -0
- package/dist/features/native/mcp-launch.js +57 -0
- package/dist/features/onboarding/environment.js +160 -0
- package/dist/features/onboarding/setup.js +123 -0
- package/dist/features/repositories/context.js +55 -0
- package/dist/features/skills/install.js +25 -0
- package/dist/features/skills/remote.js +177 -0
- package/dist/features/skills/sync.js +121 -0
- package/dist/features/skills/validation.js +34 -0
- package/dist/harnesses/claude.js +57 -0
- package/dist/harnesses/codex.js +46 -0
- package/dist/harnesses/cursor.js +44 -0
- package/dist/harnesses/detect.js +61 -0
- package/dist/harnesses/registry.js +16 -0
- package/dist/harnesses/types.js +1 -0
- package/dist/program.js +272 -0
- package/dist/ui/add-asset.js +440 -0
- package/dist/ui/asset-list.js +50 -0
- package/dist/ui/assets.js +142 -0
- package/dist/ui/auth.js +138 -0
- package/dist/ui/clash.js +43 -0
- package/dist/ui/collaboration.js +142 -0
- package/dist/ui/connection.js +102 -0
- package/dist/ui/environment-onboarding.js +125 -0
- package/dist/ui/format.js +154 -0
- package/dist/ui/hook-compatibility.js +33 -0
- package/dist/ui/mcp.js +40 -0
- package/dist/ui/move.js +22 -0
- package/dist/ui/onboarding.js +293 -0
- package/dist/ui/output.js +155 -0
- package/dist/ui/pick.js +153 -0
- package/dist/ui/scope-install.js +47 -0
- package/dist/ui/selection.js +51 -0
- package/dist/ui/theme.js +37 -0
- package/dist/version.js +12 -0
- package/package.json +73 -4
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { CliError } from "../../errors.js";
|
|
2
|
+
import { randomUUID, createHash } from "node:crypto";
|
|
3
|
+
import { join, dirname, isAbsolute } from "node:path";
|
|
4
|
+
import { mkdir, readFile, writeFile, rename, readdir, realpath, rm, lstat, } from "node:fs/promises";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { changeUploadSchema } from "@barndog-ai/api-contract";
|
|
7
|
+
import { packageFingerprint, writePackage, } from "@barndog-ai/api-contract/package";
|
|
8
|
+
import { readDirectory } from "@barndog-ai/api-contract/collaboration";
|
|
9
|
+
const filesSchema = z.array(z.object({ path: z.string(), content: z.string(), executable: z.boolean() }));
|
|
10
|
+
const workspaceSchema = z.object({
|
|
11
|
+
kind: z.enum(["skill", "hook"]).default("skill"),
|
|
12
|
+
root: z.string().refine(isAbsolute),
|
|
13
|
+
connection: z.object({
|
|
14
|
+
server: z.string(),
|
|
15
|
+
organizationId: z.uuid(),
|
|
16
|
+
developerId: z.uuid(),
|
|
17
|
+
}),
|
|
18
|
+
skillId: z.uuid().optional(),
|
|
19
|
+
name: z.string(),
|
|
20
|
+
origin: z.object({
|
|
21
|
+
type: z.enum(["org", "team", "repo", "dev"]),
|
|
22
|
+
id: z.uuid(),
|
|
23
|
+
}),
|
|
24
|
+
baseVersionId: z.uuid().nullable(),
|
|
25
|
+
baseNumber: z.number().int().positive().nullable(),
|
|
26
|
+
base: filesSchema,
|
|
27
|
+
proposal: z
|
|
28
|
+
.object({
|
|
29
|
+
id: z.uuid(),
|
|
30
|
+
revision: z.number().int().positive(),
|
|
31
|
+
files: filesSchema,
|
|
32
|
+
})
|
|
33
|
+
.optional(),
|
|
34
|
+
conflicts: z.array(z.string()),
|
|
35
|
+
pending: z
|
|
36
|
+
.object({
|
|
37
|
+
metadata: changeUploadSchema,
|
|
38
|
+
files: filesSchema,
|
|
39
|
+
changeId: z.uuid().optional(),
|
|
40
|
+
})
|
|
41
|
+
.optional(),
|
|
42
|
+
external: z
|
|
43
|
+
.object({
|
|
44
|
+
id: z.uuid(),
|
|
45
|
+
pull: z
|
|
46
|
+
.object({
|
|
47
|
+
number: z.number().int().positive(),
|
|
48
|
+
head: z.string(),
|
|
49
|
+
files: filesSchema,
|
|
50
|
+
})
|
|
51
|
+
.optional(),
|
|
52
|
+
pending: z
|
|
53
|
+
.object({
|
|
54
|
+
number: z.number().int().positive().optional(),
|
|
55
|
+
expectedCommit: z.string(),
|
|
56
|
+
files: filesSchema,
|
|
57
|
+
title: z.string(),
|
|
58
|
+
idempotencyKey: z.string(),
|
|
59
|
+
})
|
|
60
|
+
.optional(),
|
|
61
|
+
})
|
|
62
|
+
.optional(),
|
|
63
|
+
pendingWrite: filesSchema.optional(),
|
|
64
|
+
pendingSnapshot: z.string().optional(),
|
|
65
|
+
});
|
|
66
|
+
const gitWorkspaceSchema = z.object({
|
|
67
|
+
kind: z.enum(["skill", "hook"]),
|
|
68
|
+
root: z.string().refine(isAbsolute),
|
|
69
|
+
git: z.object({
|
|
70
|
+
url: z.string(),
|
|
71
|
+
branch: z.string(),
|
|
72
|
+
workspaceId: z.uuid(),
|
|
73
|
+
}),
|
|
74
|
+
name: z.string(),
|
|
75
|
+
scope: z.object({
|
|
76
|
+
type: z.enum(["org", "team", "repo"]),
|
|
77
|
+
slug: z.string(),
|
|
78
|
+
path: z.string(),
|
|
79
|
+
}),
|
|
80
|
+
baseCommit: z.string().regex(/^[0-9a-f]{40}$/),
|
|
81
|
+
base: filesSchema,
|
|
82
|
+
conflicts: z.array(z.string()),
|
|
83
|
+
/** The branch under review; it stays until its content reaches the default branch. */
|
|
84
|
+
review: z
|
|
85
|
+
.object({
|
|
86
|
+
branch: z.string(),
|
|
87
|
+
head: z.string().regex(/^[0-9a-f]{40}$/),
|
|
88
|
+
files: filesSchema,
|
|
89
|
+
url: z.string().optional(),
|
|
90
|
+
})
|
|
91
|
+
.optional(),
|
|
92
|
+
pendingWrite: filesSchema.optional(),
|
|
93
|
+
pendingSnapshot: z.string().optional(),
|
|
94
|
+
});
|
|
95
|
+
export const encodeFiles = (entries) => entries.map((e) => ({
|
|
96
|
+
path: e.path,
|
|
97
|
+
content: e.data.toString("base64"),
|
|
98
|
+
executable: e.executable,
|
|
99
|
+
}));
|
|
100
|
+
export const decodeFiles = (files) => files.map((e) => ({
|
|
101
|
+
path: e.path,
|
|
102
|
+
data: Buffer.from(e.content, "base64"),
|
|
103
|
+
executable: e.executable,
|
|
104
|
+
}));
|
|
105
|
+
export function directory(home) {
|
|
106
|
+
return join(home, ".barndog", "workspaces");
|
|
107
|
+
}
|
|
108
|
+
function recordPath(root, home) {
|
|
109
|
+
return join(directory(home), createHash("sha256").update(root).digest("hex") + ".json");
|
|
110
|
+
}
|
|
111
|
+
export async function save(work, home) {
|
|
112
|
+
await mkdir(directory(home), { recursive: true, mode: 0o700 });
|
|
113
|
+
const file = recordPath(work.root, home), temp = `${file}.${randomUUID()}.tmp`;
|
|
114
|
+
await writeFile(temp, JSON.stringify(work), { mode: 0o600 });
|
|
115
|
+
await rename(temp, file);
|
|
116
|
+
}
|
|
117
|
+
// Serialize local mutations across processes. A dead process's lock is reclaimable.
|
|
118
|
+
export async function locked(home, run) {
|
|
119
|
+
const root = directory(home), lock = join(root, "lock");
|
|
120
|
+
await mkdir(root, { recursive: true, mode: 0o700 });
|
|
121
|
+
try {
|
|
122
|
+
await mkdir(lock);
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
if (error.code !== "EEXIST")
|
|
126
|
+
throw error;
|
|
127
|
+
const pid = Number(await readFile(join(lock, "pid"), "utf8").catch(() => "0"));
|
|
128
|
+
if (!pid)
|
|
129
|
+
throw new Error("Another BarnDog workspace operation is starting; retry shortly", { cause: error });
|
|
130
|
+
try {
|
|
131
|
+
process.kill(pid, 0);
|
|
132
|
+
}
|
|
133
|
+
catch (failure) {
|
|
134
|
+
if (failure.code !== "ESRCH")
|
|
135
|
+
throw failure;
|
|
136
|
+
await rm(lock, { recursive: true });
|
|
137
|
+
return locked(home, run);
|
|
138
|
+
}
|
|
139
|
+
throw new Error("Another BarnDog workspace operation is running", {
|
|
140
|
+
cause: error,
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
await writeFile(join(lock, "pid"), String(process.pid));
|
|
144
|
+
try {
|
|
145
|
+
return await run();
|
|
146
|
+
}
|
|
147
|
+
finally {
|
|
148
|
+
await rm(lock, { recursive: true, force: true });
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** Root working-copy commands only make sense inside a pulled or created folder. */
|
|
152
|
+
export class NoWorkingCopyError extends CliError {
|
|
153
|
+
}
|
|
154
|
+
export const find = (cwd, home) => findRecord(cwd, home, (value) => {
|
|
155
|
+
if (value && typeof value === "object" && "git" in value)
|
|
156
|
+
throw new CliError("This working copy belongs to a Git repo workspace, but this machine no longer uses one.", "barndog init");
|
|
157
|
+
return workspaceSchema.parse(value);
|
|
158
|
+
});
|
|
159
|
+
export const findGit = (cwd, home) => findRecord(cwd, home, (value) => {
|
|
160
|
+
if (!value || typeof value !== "object" || !("git" in value))
|
|
161
|
+
throw new CliError("This working copy belongs to a BarnDog Cloud Workspace, but this machine now uses a Git repo.", "barndog init --server <url>");
|
|
162
|
+
return gitWorkspaceSchema.parse(value);
|
|
163
|
+
});
|
|
164
|
+
async function findRecord(cwd, home, parse) {
|
|
165
|
+
let path = await realpath(cwd);
|
|
166
|
+
while (true) {
|
|
167
|
+
try {
|
|
168
|
+
const work = parse(JSON.parse(await readFile(recordPath(path, home), "utf8")));
|
|
169
|
+
if (work.root !== path)
|
|
170
|
+
throw new Error("Invalid workspace identity");
|
|
171
|
+
if (work.pendingWrite) {
|
|
172
|
+
if (!work.pendingSnapshot?.startsWith(join(directory(home), "snapshots") + "/"))
|
|
173
|
+
throw new Error("Interrupted workspace update has no recovery snapshot");
|
|
174
|
+
const snapshot = JSON.parse(await readFile(work.pendingSnapshot, "utf8"));
|
|
175
|
+
const allowed = [
|
|
176
|
+
...decodeFiles(snapshot.files),
|
|
177
|
+
...decodeFiles(work.pendingWrite),
|
|
178
|
+
];
|
|
179
|
+
for (const file of await readDirectory(work.root))
|
|
180
|
+
if (!allowed.some((old) => old.path === file.path &&
|
|
181
|
+
old.data.equals(file.data) &&
|
|
182
|
+
old.executable === file.executable))
|
|
183
|
+
throw new Error(`Interrupted update contains additional local edits: ${file.path}. Preserve them before retrying; recovery snapshot: ${work.pendingSnapshot}`);
|
|
184
|
+
await replaceFiles(work.root, decodeFiles(work.pendingWrite));
|
|
185
|
+
delete work.pendingWrite;
|
|
186
|
+
delete work.pendingSnapshot;
|
|
187
|
+
await save(work, home);
|
|
188
|
+
}
|
|
189
|
+
return work;
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
if (error.code !== "ENOENT")
|
|
193
|
+
throw error;
|
|
194
|
+
}
|
|
195
|
+
if (dirname(path) === path)
|
|
196
|
+
break;
|
|
197
|
+
path = dirname(path);
|
|
198
|
+
}
|
|
199
|
+
throw new NoWorkingCopyError("This folder isn't an editable Skill or Hook. Working-copy commands run inside a folder made by barndog skills pull, barndog hooks pull or barndog skills create.", "barndog skills pull <name>");
|
|
200
|
+
}
|
|
201
|
+
async function replaceFiles(root, entries) {
|
|
202
|
+
// Validate paths and directory topology before touching local files.
|
|
203
|
+
const paths = new Set(entries.map((e) => e.path));
|
|
204
|
+
for (const path of paths) {
|
|
205
|
+
if (path.startsWith("/") ||
|
|
206
|
+
path
|
|
207
|
+
.split("/")
|
|
208
|
+
.some((p) => !p || p === "." || p === ".." || p.toLowerCase() === ".git") ||
|
|
209
|
+
path.includes("\\"))
|
|
210
|
+
throw new Error("UNSAFE_PACKAGE_ENTRY");
|
|
211
|
+
for (const parent of path
|
|
212
|
+
.split("/")
|
|
213
|
+
.slice(0, -1)
|
|
214
|
+
.map((_, i, a) => a.slice(0, i + 1).join("/")))
|
|
215
|
+
if (paths.has(parent))
|
|
216
|
+
throw new Error("Resolve the file/directory collision before updating this working copy");
|
|
217
|
+
}
|
|
218
|
+
const old = await readDirectory(root);
|
|
219
|
+
for (const entry of old)
|
|
220
|
+
await rm(join(root, entry.path));
|
|
221
|
+
// Remove empty directories bottom-up while preserving local .git directories.
|
|
222
|
+
async function empty(path) {
|
|
223
|
+
for (const name of await readdir(path))
|
|
224
|
+
if (name.toLowerCase() !== ".git") {
|
|
225
|
+
const child = join(path, name);
|
|
226
|
+
if ((await lstat(child)).isDirectory()) {
|
|
227
|
+
await empty(child);
|
|
228
|
+
if (!(await readdir(child)).length)
|
|
229
|
+
await rm(child, { recursive: true });
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
await empty(root);
|
|
234
|
+
await writePackage(root, entries);
|
|
235
|
+
}
|
|
236
|
+
export async function applyFiles(work, entries, home, previous = work) {
|
|
237
|
+
const local = await readDirectory(work.root);
|
|
238
|
+
if (packageFingerprint(local) === packageFingerprint(entries)) {
|
|
239
|
+
await save(work, home);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
const snapshot = join(directory(home), "snapshots", `${Date.now()}-${randomUUID()}.json`);
|
|
243
|
+
await mkdir(dirname(snapshot), { recursive: true, mode: 0o700 });
|
|
244
|
+
await writeFile(snapshot, JSON.stringify({
|
|
245
|
+
root: work.root,
|
|
246
|
+
files: encodeFiles(local),
|
|
247
|
+
workspace: previous,
|
|
248
|
+
}), { mode: 0o600 });
|
|
249
|
+
work.pendingWrite = encodeFiles(entries);
|
|
250
|
+
work.pendingSnapshot = snapshot;
|
|
251
|
+
await save(work, home);
|
|
252
|
+
await replaceFiles(work.root, entries);
|
|
253
|
+
delete work.pendingWrite;
|
|
254
|
+
delete work.pendingSnapshot;
|
|
255
|
+
await save(work, home);
|
|
256
|
+
}
|
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join, resolve, dirname } from "node:path";
|
|
4
|
+
import { mkdir, readFile, realpath, rm } from "node:fs/promises";
|
|
5
|
+
import { c } from "tar";
|
|
6
|
+
import { ApiError, changeSchema, skillSchema, } from "@barndog-ai/api-contract";
|
|
7
|
+
import { defaultLimits, packageFingerprint, writePackage, } from "@barndog-ai/api-contract/package";
|
|
8
|
+
import { diffEntries, mergeEntries, readDirectory, textContent, } from "@barndog-ai/api-contract/collaboration";
|
|
9
|
+
import { collaborationClient, metadata as packageMetadataFor, } from "./domain.js";
|
|
10
|
+
import { connection, download, getChange, selectSkill, selectOrigin, } from "./remote.js";
|
|
11
|
+
import { encodeFiles, decodeFiles, directory, save, locked, find, applyFiles, } from "./workspace-store.js";
|
|
12
|
+
import { externalSource, pullExternal, pushExternal, } from "./workspace-github.js";
|
|
13
|
+
import { NoWorkingCopyError } from "./workspace-store.js";
|
|
14
|
+
export { NoWorkingCopyError, encodeFiles, decodeFiles, } from "./workspace-store.js";
|
|
15
|
+
export async function pullSkill(name, origin, target, cwd = process.cwd(), home = homedir(), kind = "skill") {
|
|
16
|
+
return locked(home, async () => {
|
|
17
|
+
const conn = await connection(home), skill = await selectSkill(name, origin, conn, home, kind);
|
|
18
|
+
const path = resolve(cwd, target ?? name);
|
|
19
|
+
await mkdir(dirname(path), { recursive: true });
|
|
20
|
+
// Never overwrite an existing directory, even if it appears empty.
|
|
21
|
+
await mkdir(path);
|
|
22
|
+
const root = await realpath(path);
|
|
23
|
+
try {
|
|
24
|
+
const files = await download(`/v1/skills/${skill.id}/versions/${skill.version.number}/package`, conn, home, skill.version.commit, kind);
|
|
25
|
+
await writePackage(root, files);
|
|
26
|
+
await save({
|
|
27
|
+
root,
|
|
28
|
+
kind,
|
|
29
|
+
connection: conn,
|
|
30
|
+
skillId: skill.id,
|
|
31
|
+
name: skill.name,
|
|
32
|
+
origin: { type: skill.origin.type, id: skill.origin.id },
|
|
33
|
+
baseVersionId: skill.version.id,
|
|
34
|
+
baseNumber: skill.version.number,
|
|
35
|
+
base: encodeFiles(files),
|
|
36
|
+
conflicts: [],
|
|
37
|
+
}, home);
|
|
38
|
+
return { root, version: skill.version.number };
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
await rm(root, { recursive: true, force: true });
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
export async function workspaceStatus(cwd = process.cwd(), home = homedir()) {
|
|
47
|
+
return locked(home, async () => {
|
|
48
|
+
const work = await find(cwd, home), local = await readDirectory(work.root);
|
|
49
|
+
return {
|
|
50
|
+
root: work.root,
|
|
51
|
+
name: work.name,
|
|
52
|
+
baseVersion: work.baseNumber,
|
|
53
|
+
proposal: work.proposal
|
|
54
|
+
? { id: work.proposal.id, revision: work.proposal.revision }
|
|
55
|
+
: null,
|
|
56
|
+
conflicts: work.conflicts,
|
|
57
|
+
pending: !!work.pending || !!work.external?.pending,
|
|
58
|
+
external: work.external
|
|
59
|
+
? {
|
|
60
|
+
connectionId: work.external.id,
|
|
61
|
+
pull: work.external.pull
|
|
62
|
+
? {
|
|
63
|
+
number: work.external.pull.number,
|
|
64
|
+
head: work.external.pull.head,
|
|
65
|
+
}
|
|
66
|
+
: null,
|
|
67
|
+
}
|
|
68
|
+
: null,
|
|
69
|
+
changes: await diffEntries(decodeFiles(work.base), local),
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
export async function updateWorkspace(cwd = process.cwd(), home = homedir()) {
|
|
74
|
+
return locked(home, async () => {
|
|
75
|
+
const work = await find(cwd, home);
|
|
76
|
+
const previous = structuredClone(work);
|
|
77
|
+
await connection(home, work.connection);
|
|
78
|
+
if (work.pending || work.external?.pending)
|
|
79
|
+
throw new Error("Retry barndog push before pulling; a submission is awaiting confirmation");
|
|
80
|
+
if (work.conflicts.length)
|
|
81
|
+
throw new Error("Resolve existing conflicts before pulling again");
|
|
82
|
+
let entries = await readDirectory(work.root);
|
|
83
|
+
const conflicts = new Set();
|
|
84
|
+
const source = await externalSource(work, home);
|
|
85
|
+
if (source)
|
|
86
|
+
return pullExternal(work, source, entries, previous, home);
|
|
87
|
+
if (work.proposal) {
|
|
88
|
+
const proposal = await getChange(work.proposal.id, work.connection, home, work.kind);
|
|
89
|
+
if (proposal.revision !== work.proposal.revision) {
|
|
90
|
+
const remote = await download(`/v1/skill-changes/${proposal.id}/package?revision=${proposal.revision}`, work.connection, home, proposal.commit, work.kind);
|
|
91
|
+
const merged = await mergeEntries(decodeFiles(work.proposal.files), entries, remote);
|
|
92
|
+
entries = merged.entries;
|
|
93
|
+
merged.conflicts.forEach((p) => conflicts.add(p));
|
|
94
|
+
// The author's remote proposal may already have been rebased by another client.
|
|
95
|
+
if (proposal.baseVersionId !== work.baseVersionId) {
|
|
96
|
+
if (proposal.baseVersionId) {
|
|
97
|
+
const versions = (await collaborationClient(work.connection.server, home, work.kind).GET("/v1/skills/{id}/versions", {
|
|
98
|
+
params: { path: { id: work.skillId } },
|
|
99
|
+
})).data;
|
|
100
|
+
const version = versions.find((v) => v.id === proposal.baseVersionId);
|
|
101
|
+
if (!version)
|
|
102
|
+
throw new Error("Proposal base version is unavailable");
|
|
103
|
+
work.base = encodeFiles(await download(`/v1/skills/${work.skillId}/versions/${version.number}/package`, work.connection, home, version.commit, work.kind));
|
|
104
|
+
work.baseNumber = version.number;
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
work.base = [];
|
|
108
|
+
work.baseNumber = null;
|
|
109
|
+
}
|
|
110
|
+
work.baseVersionId = proposal.baseVersionId;
|
|
111
|
+
}
|
|
112
|
+
work.proposal = {
|
|
113
|
+
id: proposal.id,
|
|
114
|
+
revision: proposal.revision,
|
|
115
|
+
files: encodeFiles(remote),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (work.baseVersionId) {
|
|
120
|
+
const skill = skillSchema.parse((await collaborationClient(work.connection.server, home, work.kind).GET("/v1/skills/{id}", {
|
|
121
|
+
params: { path: { id: work.skillId } },
|
|
122
|
+
})).data);
|
|
123
|
+
if (skill.version.id !== work.baseVersionId) {
|
|
124
|
+
const latest = await download(`/v1/skills/${skill.id}/versions/${skill.version.number}/package`, work.connection, home, skill.version.commit, work.kind);
|
|
125
|
+
const merged = await mergeEntries(decodeFiles(work.base), entries, latest);
|
|
126
|
+
entries = merged.entries;
|
|
127
|
+
merged.conflicts.forEach((p) => conflicts.add(p));
|
|
128
|
+
work.base = encodeFiles(latest);
|
|
129
|
+
work.baseVersionId = skill.version.id;
|
|
130
|
+
work.baseNumber = skill.version.number;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else if (work.proposal) {
|
|
134
|
+
const proposal = await getChange(work.proposal.id, work.connection, home, work.kind);
|
|
135
|
+
if (proposal.publishedVersion) {
|
|
136
|
+
work.baseVersionId = proposal.publishedVersion.id;
|
|
137
|
+
work.baseNumber = proposal.publishedVersion.number;
|
|
138
|
+
work.base = work.proposal.files;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
work.conflicts = [...conflicts];
|
|
142
|
+
await applyFiles(work, entries, home, previous);
|
|
143
|
+
return {
|
|
144
|
+
root: work.root,
|
|
145
|
+
conflicts: work.conflicts,
|
|
146
|
+
baseVersion: work.baseNumber,
|
|
147
|
+
};
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
export async function resolveConflict(path, cwd = process.cwd(), home = homedir()) {
|
|
151
|
+
return locked(home, async () => {
|
|
152
|
+
const work = await find(cwd, home);
|
|
153
|
+
return markResolved(work, path, cwd, home);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
/** Clears one conflict once its markers are gone; shared by cloud and Git working copies. */
|
|
157
|
+
export async function markResolved(work, path, cwd, home) {
|
|
158
|
+
const absolute = resolve(await realpath(cwd), path);
|
|
159
|
+
const conflict = work.conflicts.find((p) => join(work.root, p) === absolute);
|
|
160
|
+
if (!conflict)
|
|
161
|
+
throw new Error("Path is not an unresolved conflict");
|
|
162
|
+
const files = await readDirectory(work.root), file = files.find((e) => e.path === conflict);
|
|
163
|
+
if (file &&
|
|
164
|
+
/^(<{7}|\|{7}|={7}|>{7})(?: |$)/m.test(textContent(file.data) ?? ""))
|
|
165
|
+
throw new Error("Remove conflict markers before resolving this file");
|
|
166
|
+
const paths = new Set(files.map((e) => e.path));
|
|
167
|
+
for (const p of paths)
|
|
168
|
+
if ([...paths].some((other) => p.startsWith(other + "/")))
|
|
169
|
+
throw new Error("File/directory collision remains");
|
|
170
|
+
work.conflicts = work.conflicts.filter((p) => p !== conflict);
|
|
171
|
+
await save(work, home);
|
|
172
|
+
return { resolved: conflict, remaining: work.conflicts };
|
|
173
|
+
}
|
|
174
|
+
export async function archive(files, home) {
|
|
175
|
+
const temp = join(directory(home), `upload-${randomUUID()}`), tar = temp + ".tar.gz";
|
|
176
|
+
try {
|
|
177
|
+
await writePackage(temp, decodeFiles(files));
|
|
178
|
+
await c({ cwd: temp, file: tar, gzip: true, portable: true, noMtime: true }, files.map((e) => e.path));
|
|
179
|
+
const bytes = await readFile(tar);
|
|
180
|
+
if (bytes.length > defaultLimits.compressed)
|
|
181
|
+
throw new Error("PACKAGE_TOO_LARGE");
|
|
182
|
+
return new Blob([bytes]);
|
|
183
|
+
}
|
|
184
|
+
finally {
|
|
185
|
+
await rm(temp, { recursive: true, force: true });
|
|
186
|
+
await rm(tar, { force: true });
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
export async function pushWorkspace(options = {}, cwd = process.cwd(), home = homedir()) {
|
|
190
|
+
return locked(home, async () => {
|
|
191
|
+
let work;
|
|
192
|
+
try {
|
|
193
|
+
work = await find(cwd, home);
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
if (!options.origin || !(error instanceof NoWorkingCopyError))
|
|
197
|
+
throw error;
|
|
198
|
+
const conn = await connection(home), root = await realpath(cwd), description = packageMetadataFor(await readDirectory(root), options.kind ?? "skill");
|
|
199
|
+
const origin = await selectOrigin(options.origin, conn, home);
|
|
200
|
+
work = {
|
|
201
|
+
root,
|
|
202
|
+
kind: options.kind ?? "skill",
|
|
203
|
+
connection: conn,
|
|
204
|
+
name: description.name,
|
|
205
|
+
origin: { type: origin.type, id: origin.id },
|
|
206
|
+
baseVersionId: null,
|
|
207
|
+
baseNumber: null,
|
|
208
|
+
base: [],
|
|
209
|
+
conflicts: [],
|
|
210
|
+
};
|
|
211
|
+
await save(work, home);
|
|
212
|
+
}
|
|
213
|
+
if (options.kind && work.kind !== options.kind)
|
|
214
|
+
throw new Error("Working copy belongs to a different asset kind");
|
|
215
|
+
if (options.name && work.name !== options.name)
|
|
216
|
+
throw new Error("Working-copy name does not match the requested asset");
|
|
217
|
+
await connection(home, work.connection);
|
|
218
|
+
if (options.origin) {
|
|
219
|
+
const selected = await selectOrigin(options.origin, work.connection, home);
|
|
220
|
+
if (selected.type !== work.origin.type ||
|
|
221
|
+
selected.id !== work.origin.id) {
|
|
222
|
+
if (work.skillId || work.proposal || work.pending)
|
|
223
|
+
throw new Error("A submitted working copy cannot change its origin");
|
|
224
|
+
work.origin = { type: selected.type, id: selected.id };
|
|
225
|
+
await save(work, home);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (work.conflicts.length)
|
|
229
|
+
throw new Error("Resolve conflicts with barndog resolve <path> before pushing");
|
|
230
|
+
const source = await externalSource(work, home);
|
|
231
|
+
if (source)
|
|
232
|
+
return pushExternal(work, source, options, home);
|
|
233
|
+
const client = collaborationClient(work.connection.server, home, work.kind);
|
|
234
|
+
if (options.publish) {
|
|
235
|
+
const scope = await selectOrigin(`${work.origin.type}:${work.origin.id}`, work.connection, home);
|
|
236
|
+
if (!scope.canPublish)
|
|
237
|
+
throw new Error("Direct publication requires a scope admin, organization admin, or personal owner; use barndog changes merge after review");
|
|
238
|
+
}
|
|
239
|
+
let proposal = work.proposal
|
|
240
|
+
? await getChange(work.proposal.id, work.connection, home, work.kind)
|
|
241
|
+
: undefined;
|
|
242
|
+
if (!work.pending &&
|
|
243
|
+
options.publish &&
|
|
244
|
+
proposal?.status === "merged" &&
|
|
245
|
+
proposal.publishedVersion &&
|
|
246
|
+
packageFingerprint(await readDirectory(work.root)) ===
|
|
247
|
+
packageFingerprint(decodeFiles(work.proposal.files))) {
|
|
248
|
+
work.baseVersionId = proposal.publishedVersion.id;
|
|
249
|
+
work.baseNumber = proposal.publishedVersion.number;
|
|
250
|
+
work.base = work.proposal.files;
|
|
251
|
+
await save(work, home);
|
|
252
|
+
return proposal;
|
|
253
|
+
}
|
|
254
|
+
if (!work.pending) {
|
|
255
|
+
if (proposal?.status === "rejected" && !options.new)
|
|
256
|
+
throw new Error("Suggested Change was rejected; use barndog push --new for a new proposal");
|
|
257
|
+
if (proposal?.status === "merged" &&
|
|
258
|
+
proposal.publishedVersion?.id !== work.baseVersionId)
|
|
259
|
+
throw new Error("Run barndog pull before starting another Suggested Change");
|
|
260
|
+
if (proposal?.status === "open" &&
|
|
261
|
+
proposal.revision !== work.proposal.revision)
|
|
262
|
+
throw new Error("Suggested Change changed remotely; run barndog pull before pushing");
|
|
263
|
+
const files = await readDirectory(work.root);
|
|
264
|
+
if (packageMetadataFor(files, work.kind).name !== work.name)
|
|
265
|
+
throw new Error("Package name cannot change in this working copy");
|
|
266
|
+
const previous = proposal?.status === "open" && !options.new
|
|
267
|
+
? work.proposal.files
|
|
268
|
+
: work.base;
|
|
269
|
+
if (packageFingerprint(files) ===
|
|
270
|
+
packageFingerprint(decodeFiles(previous)) &&
|
|
271
|
+
(!proposal || proposal.baseVersionId === work.baseVersionId) &&
|
|
272
|
+
(!options.title || options.title === proposal?.title)) {
|
|
273
|
+
if (options.publish && proposal?.status === "open") {
|
|
274
|
+
const merged = changeSchema.parse((await client.POST("/v1/skill-changes/{id}/merge", {
|
|
275
|
+
params: { path: { id: proposal.id } },
|
|
276
|
+
body: { expectedRevision: proposal.revision },
|
|
277
|
+
})).data);
|
|
278
|
+
work.baseVersionId = merged.publishedVersion.id;
|
|
279
|
+
work.baseNumber = merged.publishedVersion.number;
|
|
280
|
+
work.base = work.proposal.files;
|
|
281
|
+
await save(work, home);
|
|
282
|
+
return merged;
|
|
283
|
+
}
|
|
284
|
+
return { unchanged: true, proposalId: proposal?.id };
|
|
285
|
+
}
|
|
286
|
+
const changeId = proposal?.status === "open" && !options.new ? proposal.id : undefined;
|
|
287
|
+
const metadata = {
|
|
288
|
+
skillId: work.skillId,
|
|
289
|
+
name: work.name,
|
|
290
|
+
origin: work.origin,
|
|
291
|
+
baseVersionId: work.baseVersionId,
|
|
292
|
+
expectedRevision: changeId ? work.proposal.revision : undefined,
|
|
293
|
+
title: options.title ?? proposal?.title ?? `Update ${work.name}`,
|
|
294
|
+
idempotencyKey: randomUUID(),
|
|
295
|
+
};
|
|
296
|
+
work.pending = { metadata, files: encodeFiles(files), changeId };
|
|
297
|
+
await save(work, home);
|
|
298
|
+
}
|
|
299
|
+
const pending = work.pending;
|
|
300
|
+
const form = new FormData();
|
|
301
|
+
form.set("metadata", JSON.stringify(pending.metadata));
|
|
302
|
+
form.set("package", await archive(pending.files, home), "skill.tar.gz");
|
|
303
|
+
try {
|
|
304
|
+
proposal = changeSchema.parse(pending.changeId
|
|
305
|
+
? (await client.PUT("/v1/skill-changes/{id}/revisions", {
|
|
306
|
+
params: { path: { id: pending.changeId } },
|
|
307
|
+
body: form,
|
|
308
|
+
})).data
|
|
309
|
+
: (await client.POST("/v1/skill-changes", { body: form })).data);
|
|
310
|
+
}
|
|
311
|
+
catch (error) {
|
|
312
|
+
// A definitive rejection did not accept this submission. Keep local edits and
|
|
313
|
+
// permit pull/reconciliation; ambiguous transport failures retain the exact key.
|
|
314
|
+
if (error instanceof ApiError &&
|
|
315
|
+
[400, 403, 404, 409, 413].includes(error.status)) {
|
|
316
|
+
delete work.pending;
|
|
317
|
+
await save(work, home);
|
|
318
|
+
}
|
|
319
|
+
throw error;
|
|
320
|
+
}
|
|
321
|
+
work.skillId = proposal.skillId;
|
|
322
|
+
work.proposal = {
|
|
323
|
+
id: proposal.id,
|
|
324
|
+
revision: proposal.submittedRevision,
|
|
325
|
+
files: pending.files,
|
|
326
|
+
};
|
|
327
|
+
delete work.pending;
|
|
328
|
+
await save(work, home);
|
|
329
|
+
if (options.publish) {
|
|
330
|
+
proposal = changeSchema.parse((await client.POST("/v1/skill-changes/{id}/merge", {
|
|
331
|
+
params: { path: { id: proposal.id } },
|
|
332
|
+
body: { expectedRevision: work.proposal.revision },
|
|
333
|
+
})).data);
|
|
334
|
+
work.baseVersionId = proposal.publishedVersion.id;
|
|
335
|
+
work.baseNumber = proposal.publishedVersion.number;
|
|
336
|
+
work.base = work.proposal.files;
|
|
337
|
+
await save(work, home);
|
|
338
|
+
}
|
|
339
|
+
return proposal;
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
export async function createWorkspace(name, origin, target, cwd = process.cwd(), home = homedir(), kind = "skill") {
|
|
343
|
+
return locked(home, async () => {
|
|
344
|
+
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name))
|
|
345
|
+
throw new Error("Use a lowercase hyphenated package name");
|
|
346
|
+
const conn = await connection(home), scope = await selectOrigin(origin, conn, home);
|
|
347
|
+
const path = resolve(cwd, target ?? name);
|
|
348
|
+
await mkdir(dirname(path), { recursive: true });
|
|
349
|
+
await mkdir(path);
|
|
350
|
+
const root = await realpath(path);
|
|
351
|
+
const entries = kind === "skill"
|
|
352
|
+
? [
|
|
353
|
+
{
|
|
354
|
+
path: "SKILL.md",
|
|
355
|
+
data: Buffer.from(`---\nname: ${name}\ndescription: Describe this Skill\n---\n\nAdd instructions here.\n`),
|
|
356
|
+
executable: false,
|
|
357
|
+
},
|
|
358
|
+
]
|
|
359
|
+
: [
|
|
360
|
+
{
|
|
361
|
+
path: "hook.yaml",
|
|
362
|
+
data: Buffer.from(`name: ${name}\ndescription: Describe this Hook\nimplementations:\n claude:\n compatibility: supported\n scripts: [scripts/hook.sh]\n requirements:\n commands: [sh]\n events:\n SessionStart:\n - hooks:\n - type: command\n command: '\${BARNDOG_HOOK_ROOT}/scripts/hook.sh'\n`),
|
|
363
|
+
executable: false,
|
|
364
|
+
},
|
|
365
|
+
{
|
|
366
|
+
path: "scripts/hook.sh",
|
|
367
|
+
data: Buffer.from("#!/bin/sh\n# Read native input; implement the desired behavior here.\ncat >/dev/null\n"),
|
|
368
|
+
executable: true,
|
|
369
|
+
},
|
|
370
|
+
];
|
|
371
|
+
try {
|
|
372
|
+
await writePackage(root, entries);
|
|
373
|
+
await save({
|
|
374
|
+
root,
|
|
375
|
+
kind,
|
|
376
|
+
connection: conn,
|
|
377
|
+
name,
|
|
378
|
+
origin: { type: scope.type, id: scope.id },
|
|
379
|
+
baseVersionId: null,
|
|
380
|
+
baseNumber: null,
|
|
381
|
+
base: [],
|
|
382
|
+
conflicts: [],
|
|
383
|
+
}, home);
|
|
384
|
+
return { root, kind, name };
|
|
385
|
+
}
|
|
386
|
+
catch (error) {
|
|
387
|
+
await rm(root, { recursive: true, force: true });
|
|
388
|
+
throw error;
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
}
|