barndog 0.1.1 → 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.
Files changed (173) hide show
  1. package/README.md +8 -7
  2. package/dist/cli.js +11 -7
  3. package/dist/commands/add.js +33 -21
  4. package/dist/commands/arguments.js +12 -0
  5. package/dist/commands/asset-install.js +160 -0
  6. package/dist/commands/asset-list.js +121 -0
  7. package/dist/commands/asset-share.js +94 -0
  8. package/dist/commands/auth.js +4 -0
  9. package/dist/commands/availability.js +54 -26
  10. package/dist/commands/capabilities.js +71 -415
  11. package/dist/commands/collaboration.js +194 -110
  12. package/dist/commands/environment.js +18 -411
  13. package/dist/commands/github.js +200 -0
  14. package/dist/commands/helpers.js +39 -0
  15. package/dist/commands/hooks.js +87 -0
  16. package/dist/commands/init.js +4 -2
  17. package/dist/commands/knowledge.js +73 -0
  18. package/dist/commands/mcp.js +62 -0
  19. package/dist/commands/origin.js +21 -23
  20. package/dist/commands/people.js +51 -0
  21. package/dist/commands/sets.js +141 -0
  22. package/dist/commands/status.js +34 -24
  23. package/dist/commands/sync.js +11 -4
  24. package/dist/commands/workspace.js +33 -19
  25. package/dist/config/environment.js +13 -27
  26. package/dist/config/paths.js +62 -0
  27. package/dist/config/selection.js +30 -0
  28. package/dist/config/settings.js +97 -108
  29. package/dist/config/state.js +57 -57
  30. package/dist/entry/knowledge-startup.js +32 -0
  31. package/dist/entry/mcp-launch.js +71 -0
  32. package/dist/entry/opencode-plugin.js +62 -0
  33. package/dist/{features/git-workspace/startup.js → entry/workspace-startup.js} +7 -2
  34. package/dist/errors.js +198 -55
  35. package/dist/features/asset-kinds/index.js +120 -0
  36. package/dist/features/asset-selection/index.js +51 -19
  37. package/dist/features/auth/credentials.js +80 -47
  38. package/dist/features/auth/session.js +80 -45
  39. package/dist/features/auth/transport.js +66 -15
  40. package/dist/features/catalog/index.js +32 -61
  41. package/dist/features/collaboration/api.js +160 -0
  42. package/dist/features/collaboration/index.js +321 -233
  43. package/dist/features/collaboration/remote.js +35 -115
  44. package/dist/features/collaboration/results.js +2 -0
  45. package/dist/features/collaboration/workspace-git.js +233 -149
  46. package/dist/features/collaboration/workspace-github.js +117 -54
  47. package/dist/features/collaboration/workspace-store.js +140 -100
  48. package/dist/features/collaboration/workspace.js +257 -172
  49. package/dist/features/discovery/index.js +68 -57
  50. package/dist/features/doctor/index.js +32 -24
  51. package/dist/features/environment/auto-sync.js +28 -27
  52. package/dist/features/environment/names.js +67 -47
  53. package/dist/features/environment/operations.js +321 -228
  54. package/dist/features/environment/own-entries.js +89 -0
  55. package/dist/features/environment/settle-names.js +91 -0
  56. package/dist/features/environment/stage.js +178 -0
  57. package/dist/features/environment/sync.js +113 -484
  58. package/dist/features/environment/transaction.js +179 -147
  59. package/dist/features/environment/writes.js +196 -0
  60. package/dist/features/github/account.js +25 -0
  61. package/dist/features/github/connections.js +119 -0
  62. package/dist/features/github/pulls.js +118 -0
  63. package/dist/features/hooks/adapt.js +32 -25
  64. package/dist/features/hooks/catalog.js +28 -0
  65. package/dist/features/hooks/compatibility.js +49 -0
  66. package/dist/features/hooks/divergence.js +9 -6
  67. package/dist/features/hooks/test.js +74 -49
  68. package/dist/features/hooks/types.js +1 -0
  69. package/dist/features/imports/discover.js +134 -61
  70. package/dist/features/imports/folder.js +48 -14
  71. package/dist/features/imports/local.js +113 -134
  72. package/dist/features/imports/remote.js +44 -25
  73. package/dist/features/inspection/index.js +17 -17
  74. package/dist/features/inventory/index.js +99 -110
  75. package/dist/features/knowledge/index.js +69 -49
  76. package/dist/features/mcps/catalog.js +31 -0
  77. package/dist/features/mcps/import.js +316 -0
  78. package/dist/features/mcps/index.js +98 -49
  79. package/dist/features/migration/migrate.js +263 -0
  80. package/dist/features/{git-workspace → migration}/move.js +165 -109
  81. package/dist/features/migration/promote.js +24 -0
  82. package/dist/features/native/adapters.js +53 -94
  83. package/dist/features/native/configuration.js +128 -82
  84. package/dist/features/native/hook-shell.js +55 -0
  85. package/dist/features/native/plugin-module.js +56 -0
  86. package/dist/features/onboarding/environment.js +67 -34
  87. package/dist/features/onboarding/setup.js +43 -40
  88. package/dist/features/repositories/context.js +30 -13
  89. package/dist/features/server/catalog.js +58 -0
  90. package/dist/features/server/client.js +13 -0
  91. package/dist/features/server/connection.js +37 -0
  92. package/dist/features/server/origin.js +74 -0
  93. package/dist/features/server/pages.js +24 -0
  94. package/dist/features/skills/catalog.js +42 -0
  95. package/dist/features/workspace/assets.js +57 -0
  96. package/dist/features/workspace/cloud-source.js +34 -0
  97. package/dist/features/workspace/directory.js +38 -0
  98. package/dist/features/{git-workspace → workspace}/format.js +114 -51
  99. package/dist/features/workspace/git.js +214 -0
  100. package/dist/features/workspace/installed.js +92 -0
  101. package/dist/features/{git-workspace → workspace}/overlay.js +43 -23
  102. package/dist/features/workspace/packages.js +160 -0
  103. package/dist/features/{git-workspace → workspace}/publish.js +120 -64
  104. package/dist/features/workspace/repo-source.js +192 -0
  105. package/dist/features/{git-workspace → workspace}/setup.js +60 -42
  106. package/dist/features/{git-workspace → workspace}/snapshot.js +53 -18
  107. package/dist/features/workspace/source.js +1 -0
  108. package/dist/features/workspace/tree.js +52 -0
  109. package/dist/features/workspace/workspace-source.js +27 -0
  110. package/dist/harnesses/claude.js +23 -1
  111. package/dist/harnesses/codex.js +57 -1
  112. package/dist/harnesses/cursor.js +22 -1
  113. package/dist/harnesses/detect.js +6 -50
  114. package/dist/harnesses/formats.js +45 -0
  115. package/dist/harnesses/opencode.js +111 -0
  116. package/dist/harnesses/registry.js +35 -2
  117. package/dist/platform.js +375 -0
  118. package/dist/program.js +225 -54
  119. package/dist/runtime.js +1 -0
  120. package/dist/shared/cli-error.js +22 -0
  121. package/dist/shared/constants.js +32 -0
  122. package/dist/shared/files.js +72 -0
  123. package/dist/shared/lock.js +49 -0
  124. package/dist/shared/lookup.js +11 -0
  125. package/dist/shared/patterns.js +94 -0
  126. package/dist/shared/shell.js +22 -0
  127. package/dist/shared/values.js +26 -0
  128. package/dist/ui/add-asset.js +41 -400
  129. package/dist/ui/add-hook.js +213 -0
  130. package/dist/ui/add-mcp.js +107 -0
  131. package/dist/ui/add-prompts.js +47 -0
  132. package/dist/ui/add-skills.js +115 -0
  133. package/dist/ui/asset-list.js +31 -13
  134. package/dist/ui/assets.js +33 -44
  135. package/dist/ui/auth.js +43 -33
  136. package/dist/ui/clash.js +11 -6
  137. package/dist/ui/collaboration.js +116 -44
  138. package/dist/ui/connection.js +10 -4
  139. package/dist/ui/decision-error.js +24 -0
  140. package/dist/ui/environment-onboarding.js +35 -13
  141. package/dist/ui/format.js +66 -18
  142. package/dist/ui/hook-compatibility.js +14 -6
  143. package/dist/ui/mcp.js +28 -3
  144. package/dist/ui/move.js +10 -5
  145. package/dist/ui/onboarding.js +113 -73
  146. package/dist/ui/options.js +9 -0
  147. package/dist/ui/output.js +106 -43
  148. package/dist/ui/pick.js +45 -33
  149. package/dist/ui/scope-install.js +15 -7
  150. package/dist/ui/selection.js +15 -11
  151. package/dist/ui/theme.js +37 -14
  152. package/dist/version.js +6 -3
  153. package/package.json +13 -3
  154. package/dist/features/capabilities/index.js +0 -106
  155. package/dist/features/collaboration/domain.js +0 -90
  156. package/dist/features/environment/cloud-source.js +0 -27
  157. package/dist/features/environment/installed.js +0 -124
  158. package/dist/features/environment/packages.js +0 -120
  159. package/dist/features/environment/remote.js +0 -71
  160. package/dist/features/environment/workspace-source.js +0 -19
  161. package/dist/features/git-hosts/index.js +0 -148
  162. package/dist/features/git-workspace/assets.js +0 -71
  163. package/dist/features/git-workspace/git.js +0 -135
  164. package/dist/features/git-workspace/migrate.js +0 -188
  165. package/dist/features/git-workspace/source.js +0 -159
  166. package/dist/features/imports/mcp.js +0 -219
  167. package/dist/features/knowledge/startup.js +0 -25
  168. package/dist/features/native/mcp-launch.js +0 -57
  169. package/dist/features/skills/install.js +0 -25
  170. package/dist/features/skills/remote.js +0 -177
  171. package/dist/features/skills/sync.js +0 -121
  172. package/dist/features/skills/validation.js +0 -34
  173. /package/dist/features/environment/{source.js → events.js} +0 -0
@@ -1,41 +1,63 @@
1
+ import { writePackage, readDirectory } from "../workspace/directory.js";
2
+ import { HTTP_STATUS } from "../../shared/constants.js";
1
3
  import { randomUUID } from "node:crypto";
2
- import { homedir } from "node:os";
3
4
  import { join, resolve, dirname } from "node:path";
4
5
  import { mkdir, readFile, realpath, rm } from "node:fs/promises";
5
6
  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";
7
+ import { ApiError } from "@barndog-ai/api-contract";
8
+ import { defaultLimits, packageFingerprint, } from "@barndog-ai/api-contract/package";
9
+ import { diffEntries, mergeEntries, textContent, } from "@barndog-ai/api-contract/collaboration";
10
+ import { packageApi } from "./api.js";
11
+ import { changeAssetId } from "./results.js";
12
+ import { metadata as packageMetadataFor, packageKindOf, assetKinds, } from "../asset-kinds/index.js";
13
+ import { downloadChange, downloadVersion, getChange, selectPackage, } from "./remote.js";
14
+ import { selectOrigin } from "../server/origin.js";
15
+ import { connection } from "../server/connection.js";
16
+ import { encodeFiles, decodeFiles, save, locked, find, applyFiles, NoWorkingCopyError, readWorkingCopy, } from "./workspace-store.js";
12
17
  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
+ import { defined } from "../../shared/lookup.js";
19
+ import { NAME } from "../../shared/patterns.js";
20
+ import { CliError, PackageError } from "../../errors.js";
21
+ import { barndogPaths } from "../../config/paths.js";
22
+ const publishedId = (work) => defined(work.assetId, "This working copy isn't published yet");
23
+ const linkedProposal = (work) => defined(work.proposal, "This working copy has no linked proposal");
24
+ const publishedVersion = (change) => defined(change.publishedVersion, "The merged change has no published version");
25
+ export { NoWorkingCopyError } from "./workspace-store.js";
26
+ /**
27
+ * `pull <name>` in Cloud: downloads the published Skill or Hook into a new
28
+ * working folder (`target`, default `./<name>`) and registers it under
29
+ * `~/.barndog/workspaces/`. Refuses a folder that already exists.
30
+ */
31
+ export async function pullCloudPackage({ name, origin, kind }, { target, runtime }) {
32
+ const { cwd } = runtime;
33
+ return locked(runtime, async () => {
34
+ const conn = await connection(runtime);
35
+ const published = await selectPackage({ name, selector: origin, kind }, conn, runtime);
18
36
  const path = resolve(cwd, target ?? name);
19
37
  await mkdir(dirname(path), { recursive: true });
20
38
  // Never overwrite an existing directory, even if it appears empty.
21
39
  await mkdir(path);
22
40
  const root = await realpath(path);
23
41
  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);
42
+ const files = await downloadVersion({
43
+ id: published.id,
44
+ number: published.version.number,
45
+ commit: published.version.commit,
46
+ }, conn, { runtime, kind });
47
+ await writePackage(root, files, runtime);
26
48
  await save({
27
49
  root,
28
50
  kind,
29
51
  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,
52
+ assetId: published.id,
53
+ name: published.name,
54
+ origin: { type: published.origin.type, id: published.origin.id },
55
+ baseVersionId: published.version.id,
56
+ baseNumber: published.version.number,
35
57
  base: encodeFiles(files),
36
58
  conflicts: [],
37
- }, home);
38
- return { root, version: skill.version.number };
59
+ }, runtime);
60
+ return { root, version: published.version.number };
39
61
  }
40
62
  catch (error) {
41
63
  await rm(root, { recursive: true, force: true });
@@ -43,9 +65,14 @@ export async function pullSkill(name, origin, target, cwd = process.cwd(), home
43
65
  }
44
66
  });
45
67
  }
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);
68
+ /**
69
+ * The Cloud working copy containing `cwd`, with its edits as diffs against the
70
+ * version it started from. Throws `NoWorkingCopyError` outside one.
71
+ */
72
+ export async function workspaceStatus(runtime) {
73
+ return locked(runtime, async () => {
74
+ const work = await find(runtime);
75
+ const local = await readWorkingCopy(work, runtime);
49
76
  return {
50
77
  root: work.root,
51
78
  name: work.name,
@@ -70,37 +97,52 @@ export async function workspaceStatus(cwd = process.cwd(), home = homedir()) {
70
97
  };
71
98
  });
72
99
  }
73
- export async function updateWorkspace(cwd = process.cwd(), home = homedir()) {
74
- return locked(home, async () => {
75
- const work = await find(cwd, home);
100
+ /**
101
+ * `pull` in a Cloud working copy: merges edits made to its open proposal, then
102
+ * the newest published version, into the folder. Hands over to GitHub
103
+ * reconciliation when GitHub is authoritative. Refuses while a submission
104
+ * awaits a retry; conflicting text gets markers and is listed in `conflicts`.
105
+ */
106
+ export async function updateWorkspace(runtime) {
107
+ return locked(runtime, async () => {
108
+ const work = await find(runtime);
76
109
  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);
110
+ await connection(runtime, work.connection);
111
+ if (work.pending || work.external?.pending) {
112
+ throw new CliError("Your last push is still waiting for the server to confirm it. Retry it before pulling.", "barndog push");
113
+ }
114
+ if (work.conflicts.length) {
115
+ throw new CliError("Resolve the existing conflicts before pulling again.", "barndog resolve <path>");
116
+ }
117
+ let entries = await readWorkingCopy(work, runtime);
83
118
  const conflicts = new Set();
84
- const source = await externalSource(work, home);
85
- if (source)
86
- return pullExternal(work, source, entries, previous, home);
119
+ const source = await externalSource(work, runtime);
120
+ if (source) {
121
+ return pullExternal(work, { source, entries, previous }, runtime);
122
+ }
87
123
  if (work.proposal) {
88
- const proposal = await getChange(work.proposal.id, work.connection, home, work.kind);
124
+ const proposal = await getChange(work.proposal.id, work.connection, runtime, work.kind);
89
125
  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);
126
+ const remote = await downloadChange(proposal, work.connection, {
127
+ runtime,
128
+ kind: work.kind,
129
+ });
91
130
  const merged = await mergeEntries(decodeFiles(work.proposal.files), entries, remote);
92
131
  entries = merged.entries;
93
132
  merged.conflicts.forEach((p) => conflicts.add(p));
94
133
  // The author's remote proposal may already have been rebased by another client.
95
134
  if (proposal.baseVersionId !== work.baseVersionId) {
96
135
  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;
136
+ const versions = await packageApi(work.kind, work.connection, runtime).versions(publishedId(work));
100
137
  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));
138
+ if (!version) {
139
+ throw new Error("The server didn't return the version this Suggested Change is based on");
140
+ }
141
+ work.base = encodeFiles(await downloadVersion({
142
+ id: publishedId(work),
143
+ number: version.number,
144
+ commit: version.commit,
145
+ }, work.connection, { runtime, kind: work.kind }));
104
146
  work.baseNumber = version.number;
105
147
  }
106
148
  else {
@@ -117,21 +159,23 @@ export async function updateWorkspace(cwd = process.cwd(), home = homedir()) {
117
159
  }
118
160
  }
119
161
  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);
162
+ const published = await packageApi(work.kind, work.connection, runtime).published(publishedId(work));
163
+ if (published.version.id !== work.baseVersionId) {
164
+ const latest = await downloadVersion({
165
+ id: published.id,
166
+ number: published.version.number,
167
+ commit: published.version.commit,
168
+ }, work.connection, { runtime, kind: work.kind });
125
169
  const merged = await mergeEntries(decodeFiles(work.base), entries, latest);
126
170
  entries = merged.entries;
127
171
  merged.conflicts.forEach((p) => conflicts.add(p));
128
172
  work.base = encodeFiles(latest);
129
- work.baseVersionId = skill.version.id;
130
- work.baseNumber = skill.version.number;
173
+ work.baseVersionId = published.version.id;
174
+ work.baseNumber = published.version.number;
131
175
  }
132
176
  }
133
177
  else if (work.proposal) {
134
- const proposal = await getChange(work.proposal.id, work.connection, home, work.kind);
178
+ const proposal = await getChange(work.proposal.id, work.connection, runtime, work.kind);
135
179
  if (proposal.publishedVersion) {
136
180
  work.baseVersionId = proposal.publishedVersion.id;
137
181
  work.baseNumber = proposal.publishedVersion.number;
@@ -139,7 +183,7 @@ export async function updateWorkspace(cwd = process.cwd(), home = homedir()) {
139
183
  }
140
184
  }
141
185
  work.conflicts = [...conflicts];
142
- await applyFiles(work, entries, home, previous);
186
+ await applyFiles(work, entries, runtime, previous);
143
187
  return {
144
188
  root: work.root,
145
189
  conflicts: work.conflicts,
@@ -147,38 +191,67 @@ export async function updateWorkspace(cwd = process.cwd(), home = homedir()) {
147
191
  };
148
192
  });
149
193
  }
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);
194
+ /**
195
+ * Marks one conflicted file in a Cloud working copy resolved, after checking
196
+ * its conflict markers are gone.
197
+ */
198
+ export async function resolveConflict(path, runtime) {
199
+ return locked(runtime, async () => {
200
+ const work = await find(runtime);
201
+ return markResolved(work, path, runtime);
154
202
  });
155
203
  }
204
+ /**
205
+ * A line a three-way merge writes around conflicting text: `<<<<<<< local`,
206
+ * `||||||| base`, `=======` or `>>>>>>> remote` (seven characters, then a space
207
+ * and a label or the end of the line).
208
+ */
209
+ const CONFLICT_MARKER = /^(?:<{7}|\|{7}|={7}|>{7})(?: |$)/m;
210
+ /**
211
+ * Whether text still holds a line the three-way merge wrote around a conflict.
212
+ *
213
+ * @internal Exported for its table test in `src/tests/patterns.test.ts`.
214
+ */
215
+ export const hasConflictMarkers = (text) => CONFLICT_MARKER.test(text);
156
216
  /** Clears one conflict once its markers are gone; shared by cloud and Git working copies. */
157
- export async function markResolved(work, path, cwd, home) {
217
+ export async function markResolved(work, path, runtime) {
218
+ const { cwd } = runtime;
158
219
  const absolute = resolve(await realpath(cwd), path);
159
220
  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");
221
+ if (!conflict) {
222
+ throw new CliError(`${path} isn't an unresolved conflict.`, "barndog status");
223
+ }
224
+ const files = await readDirectory(work.root, runtime);
225
+ const file = files.find((e) => e.path === conflict);
226
+ if (file && hasConflictMarkers(textContent(file.data) ?? "")) {
227
+ throw new CliError(`${conflict} still has conflict markers. Edit them out, then resolve it again.`, `barndog resolve ${path}`);
228
+ }
166
229
  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");
230
+ for (const p of paths) {
231
+ if ([...paths].some((other) => p.startsWith(other + "/"))) {
232
+ throw new CliError(`A file and a folder share the path ${p}. Keep one, then resolve again.`, `barndog resolve ${path}`);
233
+ }
234
+ }
170
235
  work.conflicts = work.conflicts.filter((p) => p !== conflict);
171
- await save(work, home);
236
+ await save(work, runtime);
172
237
  return { resolved: conflict, remaining: work.conflicts };
173
238
  }
174
- export async function archive(files, home) {
175
- const temp = join(directory(home), `upload-${randomUUID()}`), tar = temp + ".tar.gz";
239
+ /**
240
+ * Packs base64-encoded files into the gzipped tarball the server's upload
241
+ * routes take. Fails with `PACKAGE_TOO_LARGE` above the compressed size limit;
242
+ * uses a temporary folder under `~/.barndog/workspaces/`.
243
+ */
244
+ export async function archive(files, runtime) {
245
+ const { home } = runtime;
246
+ const temp = join(barndogPaths(home).workingCopies, `upload-${randomUUID()}`);
247
+ const tar = temp + ".tar.gz";
176
248
  try {
177
- await writePackage(temp, decodeFiles(files));
249
+ await writePackage(temp, decodeFiles(files), runtime);
178
250
  await c({ cwd: temp, file: tar, gzip: true, portable: true, noMtime: true }, files.map((e) => e.path));
179
251
  const bytes = await readFile(tar);
180
- if (bytes.length > defaultLimits.compressed)
181
- throw new Error("PACKAGE_TOO_LARGE");
252
+ if (bytes.length > defaultLimits.compressed) {
253
+ throw new PackageError("PACKAGE_TOO_LARGE");
254
+ }
182
255
  return new Blob([bytes]);
183
256
  }
184
257
  finally {
@@ -186,20 +259,35 @@ export async function archive(files, home) {
186
259
  await rm(tar, { force: true });
187
260
  }
188
261
  }
189
- export async function pushWorkspace(options = {}, cwd = process.cwd(), home = homedir()) {
190
- return locked(home, async () => {
262
+ /**
263
+ * `push` in Cloud: creates or revises the working copy's proposal and, with
264
+ * `publish`, merges it at once (scope or organization admins and personal
265
+ * owners only). Inside
266
+ * a folder that isn't a working copy yet, `origin` registers it first. The
267
+ * upload, retry key and payload are saved before sending, so an ambiguous
268
+ * failure can be retried exactly. GitHub-authoritative assets go to a pull
269
+ * request instead.
270
+ */
271
+ export async function pushWorkspace(options = {}, runtime) {
272
+ const { cwd } = runtime;
273
+ return locked(runtime, async () => {
191
274
  let work;
192
275
  try {
193
- work = await find(cwd, home);
276
+ work = await find(runtime);
194
277
  }
195
278
  catch (error) {
196
- if (!options.origin || !(error instanceof NoWorkingCopyError))
279
+ if (!options.origin || !(error instanceof NoWorkingCopyError)) {
197
280
  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);
281
+ }
282
+ const conn = await connection(runtime);
283
+ const root = await realpath(cwd);
284
+ const files = await readDirectory(root, runtime);
285
+ const kind = packageKindOf(files, options.kind);
286
+ const description = packageMetadataFor(files, kind);
287
+ const origin = await selectOrigin(options.origin, conn, runtime);
200
288
  work = {
201
289
  root,
202
- kind: options.kind ?? "skill",
290
+ kind,
203
291
  connection: conn,
204
292
  name: description.name,
205
293
  origin: { type: origin.type, id: origin.id },
@@ -208,168 +296,165 @@ export async function pushWorkspace(options = {}, cwd = process.cwd(), home = ho
208
296
  base: [],
209
297
  conflicts: [],
210
298
  };
211
- await save(work, home);
299
+ await save(work, runtime);
300
+ }
301
+ if (options.kind && work.kind !== options.kind) {
302
+ throw new CliError(`This working copy holds a ${work.kind}, not a ${options.kind}.`, "barndog status");
303
+ }
304
+ if (options.name && work.name !== options.name) {
305
+ throw new CliError(`This working copy is ${work.name}, not ${options.name}.`, "barndog status");
212
306
  }
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);
307
+ await connection(runtime, work.connection);
218
308
  if (options.origin) {
219
- const selected = await selectOrigin(options.origin, work.connection, home);
309
+ const selected = await selectOrigin(options.origin, work.connection, runtime);
220
310
  if (selected.type !== work.origin.type ||
221
311
  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");
312
+ if (work.assetId || work.proposal || work.pending) {
313
+ throw new CliError("This working copy was already submitted, so its origin can't change. Push without --origin.", "barndog push");
314
+ }
224
315
  work.origin = { type: selected.type, id: selected.id };
225
- await save(work, home);
316
+ await save(work, runtime);
226
317
  }
227
318
  }
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);
319
+ if (work.conflicts.length) {
320
+ throw new CliError("Resolve the conflicts before pushing.", "barndog resolve <path>");
321
+ }
322
+ const source = await externalSource(work, runtime);
323
+ if (source) {
324
+ return pushExternal(work, source, options, runtime);
325
+ }
326
+ const api = packageApi(work.kind, work.connection, runtime);
234
327
  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");
328
+ const scope = await selectOrigin(`${work.origin.type}:${work.origin.id}`, work.connection, runtime);
329
+ if (!scope.canPublish) {
330
+ throw new CliError(`Only a scope admin, an organization admin or the personal owner can publish directly. Propose it for review instead, then merge it with barndog ${assetKinds[work.kind].command} changes merge.`, "barndog push");
331
+ }
238
332
  }
239
333
  let proposal = work.proposal
240
- ? await getChange(work.proposal.id, work.connection, home, work.kind)
334
+ ? await getChange(work.proposal.id, work.connection, runtime, work.kind)
241
335
  : undefined;
242
336
  if (!work.pending &&
243
337
  options.publish &&
244
338
  proposal?.status === "merged" &&
245
339
  proposal.publishedVersion &&
246
- packageFingerprint(await readDirectory(work.root)) ===
247
- packageFingerprint(decodeFiles(work.proposal.files))) {
340
+ packageFingerprint(await readWorkingCopy(work, runtime)) ===
341
+ packageFingerprint(decodeFiles(linkedProposal(work).files))) {
248
342
  work.baseVersionId = proposal.publishedVersion.id;
249
343
  work.baseNumber = proposal.publishedVersion.number;
250
- work.base = work.proposal.files;
251
- await save(work, home);
344
+ work.base = linkedProposal(work).files;
345
+ await save(work, runtime);
252
346
  return proposal;
253
347
  }
254
348
  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");
349
+ if (proposal?.status === "rejected" && !options.new) {
350
+ throw new CliError("This Suggested Change was rejected.", "barndog push --new");
351
+ }
257
352
  if (proposal?.status === "merged" &&
258
- proposal.publishedVersion?.id !== work.baseVersionId)
259
- throw new Error("Run barndog pull before starting another Suggested Change");
353
+ proposal.publishedVersion?.id !== work.baseVersionId) {
354
+ throw new CliError("Your last Suggested Change was merged. Pull it before starting another.", "barndog pull");
355
+ }
260
356
  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");
357
+ proposal.revision !== linkedProposal(work).revision) {
358
+ throw new CliError("The Suggested Change changed on the server since your last pull.", "barndog pull");
359
+ }
360
+ const files = await readWorkingCopy(work, runtime);
361
+ if (packageMetadataFor(files, work.kind).name !== work.name) {
362
+ throw new CliError(`This working copy's name must stay ${work.name}. Change it back, then push again.`);
363
+ }
266
364
  const previous = proposal?.status === "open" && !options.new
267
- ? work.proposal.files
365
+ ? linkedProposal(work).files
268
366
  : work.base;
269
367
  if (packageFingerprint(files) ===
270
368
  packageFingerprint(decodeFiles(previous)) &&
271
369
  (!proposal || proposal.baseVersionId === work.baseVersionId) &&
272
370
  (!options.title || options.title === proposal?.title)) {
273
371
  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);
372
+ const merged = await api.merge(proposal.id, {
373
+ expectedRevision: proposal.revision,
374
+ });
375
+ work.baseVersionId = publishedVersion(merged).id;
376
+ work.baseNumber = publishedVersion(merged).number;
377
+ work.base = linkedProposal(work).files;
378
+ await save(work, runtime);
282
379
  return merged;
283
380
  }
284
381
  return { unchanged: true, proposalId: proposal?.id };
285
382
  }
286
383
  const changeId = proposal?.status === "open" && !options.new ? proposal.id : undefined;
287
384
  const metadata = {
288
- skillId: work.skillId,
385
+ assetId: work.assetId,
289
386
  name: work.name,
290
387
  origin: work.origin,
291
388
  baseVersionId: work.baseVersionId,
292
- expectedRevision: changeId ? work.proposal.revision : undefined,
389
+ expectedRevision: changeId ? linkedProposal(work).revision : undefined,
293
390
  title: options.title ?? proposal?.title ?? `Update ${work.name}`,
294
391
  idempotencyKey: randomUUID(),
295
392
  };
296
393
  work.pending = { metadata, files: encodeFiles(files), changeId };
297
- await save(work, home);
394
+ await save(work, runtime);
298
395
  }
299
396
  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");
397
+ const upload = await archive(pending.files, runtime);
303
398
  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);
399
+ proposal = pending.changeId
400
+ ? await api.revise(pending.changeId, pending.metadata, upload)
401
+ : await api.submit(pending.metadata, upload);
310
402
  }
311
403
  catch (error) {
312
404
  // A definitive rejection did not accept this submission. Keep local edits and
313
405
  // permit pull/reconciliation; ambiguous transport failures retain the exact key.
314
406
  if (error instanceof ApiError &&
315
- [400, 403, 404, 409, 413].includes(error.status)) {
407
+ [
408
+ HTTP_STATUS.badRequest,
409
+ HTTP_STATUS.forbidden,
410
+ HTTP_STATUS.notFound,
411
+ HTTP_STATUS.conflict,
412
+ HTTP_STATUS.payloadTooLarge,
413
+ ].includes(error.status)) {
316
414
  delete work.pending;
317
- await save(work, home);
415
+ await save(work, runtime);
318
416
  }
319
417
  throw error;
320
418
  }
321
- work.skillId = proposal.skillId;
419
+ work.assetId = changeAssetId(proposal);
322
420
  work.proposal = {
323
421
  id: proposal.id,
324
422
  revision: proposal.submittedRevision,
325
423
  files: pending.files,
326
424
  };
327
425
  delete work.pending;
328
- await save(work, home);
426
+ await save(work, runtime);
329
427
  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;
428
+ proposal = await api.merge(proposal.id, {
429
+ expectedRevision: work.proposal.revision,
430
+ });
431
+ work.baseVersionId = publishedVersion(proposal).id;
432
+ work.baseNumber = publishedVersion(proposal).number;
336
433
  work.base = work.proposal.files;
337
- await save(work, home);
434
+ await save(work, runtime);
338
435
  }
339
436
  return proposal;
340
437
  });
341
438
  }
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);
439
+ /**
440
+ * Starts a Cloud working copy from the kind's scaffold. Contacts the server to
441
+ * validate the connection and origin; asset content is uploaded only on `push`.
442
+ */
443
+ export async function createWorkspace({ name, origin, kind }, { target, runtime }) {
444
+ const { cwd } = runtime;
445
+ return locked(runtime, async () => {
446
+ if (!NAME.test(name)) {
447
+ throw new CliError(`${name} isn't a valid name. Use lowercase letters, digits and hyphens, such as my-skill.`);
448
+ }
449
+ const conn = await connection(runtime);
450
+ const scope = await selectOrigin(origin, conn, runtime);
347
451
  const path = resolve(cwd, target ?? name);
348
452
  await mkdir(dirname(path), { recursive: true });
349
453
  await mkdir(path);
350
454
  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
- ];
455
+ const entries = assetKinds[kind].scaffold(name);
371
456
  try {
372
- await writePackage(root, entries);
457
+ await writePackage(root, entries, runtime);
373
458
  await save({
374
459
  root,
375
460
  kind,
@@ -380,7 +465,7 @@ export async function createWorkspace(name, origin, target, cwd = process.cwd(),
380
465
  baseNumber: null,
381
466
  base: [],
382
467
  conflicts: [],
383
- }, home);
468
+ }, runtime);
384
469
  return { root, kind, name };
385
470
  }
386
471
  catch (error) {