barndog 0.1.1 → 0.2.1

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