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,38 +1,58 @@
1
+ import { gitAccessCommand } from "../../platform.js";
2
+ import { POSIX_MODE } from "../../shared/constants.js";
1
3
  import { randomUUID } from "node:crypto";
2
- import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
4
+ import { mkdir, rename, rm, writeFile } from "node:fs/promises";
3
5
  import { dirname, join } from "node:path";
4
6
  import { CliError } from "../../errors.js";
5
7
  import { CONFIG_FILE, parseConfig, skeleton } from "./format.js";
6
8
  import { git, gitInTerminal, gitText, remoteHead, } from "./git.js";
7
9
  import { commitFiles, pushCommit } from "./publish.js";
8
- import { ensureBareClone, fetchBranch, gitDirectory, recordFetch, trackingRef, } from "./source.js";
9
- export const DEFAULT_BRANCH = "main";
10
- export const inspectRepository = (url) => remoteHead(url, true);
11
- const stagingClone = (home) => join(home, ".barndog", "git", `.setup-${randomUUID()}.git`);
12
- async function adoptClone(staging, workspaceId, home) {
13
- const target = gitDirectory(workspaceId, home);
10
+ import { ensureBareClone, fetchBranch, recordFetch, trackingRef, } from "./repo-source.js";
11
+ import { barndogPaths } from "../../config/paths.js";
12
+ import { readIfExists } from "../../shared/files.js";
13
+ import { errorCode } from "../../shared/values.js";
14
+ /**
15
+ * Branch name used when creating the first commit of an empty Git workspace.
16
+ */
17
+ const DEFAULT_BRANCH = "main";
18
+ /**
19
+ * Queries a remote repository's default head with credential prompts allowed
20
+ * for interactive setup.
21
+ */
22
+ export const inspectRepository = (url, runtime) => remoteHead(url, runtime, true);
23
+ const stagingClone = (runtime) => {
24
+ const { home } = runtime;
25
+ return barndogPaths(home).setupClone(randomUUID());
26
+ };
27
+ async function adoptClone(staging, workspaceId, runtime) {
28
+ const { home } = runtime;
29
+ const target = barndogPaths(home).bareClone(workspaceId);
14
30
  await rm(target, { recursive: true, force: true });
15
31
  await rename(staging, target);
16
32
  }
17
33
  /** Pushes the first commit of a brand-new workspace to an empty repository. */
18
- export async function createGitWorkspace(url, name, home) {
34
+ export async function createGitWorkspace(url, name, runtime) {
19
35
  const workspaceId = randomUUID();
20
- const staging = stagingClone(home);
36
+ const staging = stagingClone(runtime);
21
37
  try {
22
- await ensureBareClone(staging, url);
38
+ await ensureBareClone(staging, url, runtime);
23
39
  const commit = await commitFiles(staging, null, {
24
40
  files: skeleton({ workspaceId, name }).map((file) => ({
25
41
  ...file,
26
42
  executable: false,
27
43
  })),
28
- }, "Set up BarnDog workspace", { allowPrompt: true });
29
- await pushCommit(staging, commit, DEFAULT_BRANCH, true);
30
- await git(["update-ref", trackingRef(DEFAULT_BRANCH), commit], {
44
+ message: "Set up BarnDog workspace",
45
+ }, { allowPrompt: true, runtime });
46
+ await pushCommit(staging, commit, DEFAULT_BRANCH, {
47
+ runtime,
48
+ allowPrompt: true,
49
+ });
50
+ await git(["update-ref", trackingRef(DEFAULT_BRANCH), commit], runtime, {
31
51
  repo: staging,
32
52
  });
33
- await adoptClone(staging, workspaceId, home);
53
+ await adoptClone(staging, workspaceId, runtime);
34
54
  const workspace = { url, branch: DEFAULT_BRANCH, workspaceId };
35
- await recordFetch(workspace, commit, home);
55
+ await recordFetch(workspace, commit, runtime);
36
56
  return workspace;
37
57
  }
38
58
  finally {
@@ -40,17 +60,17 @@ export async function createGitWorkspace(url, name, home) {
40
60
  }
41
61
  }
42
62
  /** Connects this machine to a repository that already holds a workspace. */
43
- export async function connectGitWorkspace(url, branch, home) {
44
- const staging = stagingClone(home);
63
+ export async function connectGitWorkspace(url, branch, runtime) {
64
+ const staging = stagingClone(runtime);
45
65
  try {
46
- await ensureBareClone(staging, url);
47
- await fetchBranch(staging, branch, true);
48
- const commit = await gitText(["rev-parse", trackingRef(branch)], {
66
+ await ensureBareClone(staging, url, runtime);
67
+ await fetchBranch(staging, branch, runtime, true);
68
+ const commit = await gitText(["rev-parse", trackingRef(branch)], runtime, {
49
69
  repo: staging,
50
70
  });
51
71
  let text;
52
72
  try {
53
- text = await gitText(["show", `${commit}:${CONFIG_FILE}`], {
73
+ text = await gitText(["show", `${commit}:${CONFIG_FILE}`], runtime, {
54
74
  repo: staging,
55
75
  });
56
76
  }
@@ -58,9 +78,9 @@ export async function connectGitWorkspace(url, branch, home) {
58
78
  throw new CliError(`${url} has commits but no ${CONFIG_FILE}, so it isn't a BarnDog workspace. Use an empty repository to start one.`, undefined, { cause: error });
59
79
  }
60
80
  const { workspaceId } = parseConfig(text);
61
- await adoptClone(staging, workspaceId, home);
81
+ await adoptClone(staging, workspaceId, runtime);
62
82
  const workspace = { url, branch, workspaceId };
63
- await recordFetch(workspace, commit, home);
83
+ await recordFetch(workspace, commit, runtime);
64
84
  return workspace;
65
85
  }
66
86
  finally {
@@ -71,33 +91,38 @@ export async function connectGitWorkspace(url, branch, home) {
71
91
  * Clones through the terminal when Git couldn't read the repository quietly, so
72
92
  * credential and host-key prompts work. Null means the repository is empty.
73
93
  */
74
- export async function cloneGitWorkspace(url, home) {
75
- const staging = stagingClone(home);
76
- await mkdir(dirname(staging), { recursive: true, mode: 0o700 });
94
+ export async function cloneGitWorkspace(url, runtime) {
95
+ const staging = stagingClone(runtime);
96
+ await mkdir(dirname(staging), {
97
+ recursive: true,
98
+ mode: POSIX_MODE.privateDirectory,
99
+ });
77
100
  try {
78
101
  try {
79
- await gitInTerminal(["clone", "--bare", "--", url, staging]);
102
+ await gitInTerminal(["clone", "--bare", "--", url, staging], runtime);
80
103
  }
81
104
  catch (error) {
82
- throw new CliError(`Git couldn't clone ${url}. Check that you have access, and that this URL is right.`, `git clone ${url}`, { cause: error });
105
+ throw new CliError(`Git couldn't clone ${url}. Check that you have access, and that this URL is right.`, gitAccessCommand(url, runtime.platform), { cause: error });
83
106
  }
84
107
  let commit;
85
108
  let branch;
86
109
  try {
87
- commit = await gitText(["rev-parse", "--verify", "HEAD"], {
110
+ commit = await gitText(["rev-parse", "--verify", "HEAD"], runtime, {
88
111
  repo: staging,
89
112
  });
90
- branch = await gitText(["symbolic-ref", "--short", "HEAD"], {
113
+ branch = await gitText(["symbolic-ref", "--short", "HEAD"], runtime, {
91
114
  repo: staging,
92
115
  });
93
116
  }
94
117
  catch {
95
118
  return null;
96
119
  }
97
- await git(["update-ref", trackingRef(branch), commit], { repo: staging });
120
+ await git(["update-ref", trackingRef(branch), commit], runtime, {
121
+ repo: staging,
122
+ });
98
123
  let text;
99
124
  try {
100
- text = await gitText(["show", `${commit}:${CONFIG_FILE}`], {
125
+ text = await gitText(["show", `${commit}:${CONFIG_FILE}`], runtime, {
101
126
  repo: staging,
102
127
  });
103
128
  }
@@ -105,31 +130,33 @@ export async function cloneGitWorkspace(url, home) {
105
130
  throw new CliError(`${url} has commits but no ${CONFIG_FILE}, so it isn't a BarnDog workspace. Use an empty repository to start one.`, undefined, { cause: error });
106
131
  }
107
132
  const { workspaceId } = parseConfig(text);
108
- await adoptClone(staging, workspaceId, home);
133
+ await adoptClone(staging, workspaceId, runtime);
109
134
  const workspace = { url, branch, workspaceId };
110
- await recordFetch(workspace, commit, home);
135
+ await recordFetch(workspace, commit, runtime);
111
136
  return workspace;
112
137
  }
113
138
  finally {
114
139
  await rm(staging, { recursive: true, force: true });
115
140
  }
116
141
  }
117
- export const defaultLocalPath = (home) => join(home, ".barndog", "workspace");
118
142
  /** Opens the folder that holds this machine's assets, creating it the first time. */
119
- export async function localWorkspace(home, path = defaultLocalPath(home)) {
120
- try {
121
- const { workspaceId } = parseConfig(await readFile(join(path, CONFIG_FILE), "utf8"));
122
- return { path, workspaceId };
123
- }
124
- catch (error) {
125
- if (error.code !== "ENOENT")
126
- throw error;
143
+ export async function localWorkspace(runtime, path = barndogPaths(runtime.home).localWorkspace) {
144
+ const config = await readIfExists(join(path, CONFIG_FILE));
145
+ if (config !== null) {
146
+ return { path, workspaceId: parseConfig(config).workspaceId };
127
147
  }
148
+ // barndog.yaml goes last, so a run that stopped part way is finished by the
149
+ // next one; files that run already wrote are kept as they are.
128
150
  const workspaceId = randomUUID();
129
- for (const file of skeleton({ workspaceId, name: "My workspace" })) {
151
+ const files = skeleton({ workspaceId, name: "My workspace" }).sort((a, b) => Number(a.path === CONFIG_FILE) - Number(b.path === CONFIG_FILE));
152
+ for (const file of files) {
130
153
  const target = join(path, file.path);
131
154
  await mkdir(dirname(target), { recursive: true });
132
- await writeFile(target, file.data, { flag: "wx" });
155
+ await writeFile(target, file.data, { flag: "wx" }).catch((error) => {
156
+ if (errorCode(error) !== "EEXIST" || file.path === CONFIG_FILE) {
157
+ throw error;
158
+ }
159
+ });
133
160
  }
134
161
  return { path, workspaceId };
135
162
  }
@@ -1,17 +1,41 @@
1
+ import { fileExecutable } from "../../platform.js";
1
2
  import { createHash } from "node:crypto";
2
3
  import { lstat, readFile, readdir, readlink } from "node:fs/promises";
3
4
  import { join } from "node:path";
4
5
  import { git } from "./git.js";
5
- export const TREE_MODE = "040000";
6
- export const isBlob = (mode) => mode === "100644" || mode === "100755";
7
- export function gitObjectHash(type, content) {
6
+ import { defined } from "../../shared/lookup.js";
7
+ /** Git cat-file --batch terminates each object header with ASCII LF. */
8
+ const LF_BYTE = 0x0a;
9
+ /**
10
+ * Git index/tree modes are octal strings: regular file, executable, symbolic
11
+ * link, gitlink (submodule), and directory. See gitformat-index and git ls-tree.
12
+ * git update-index --index-info reads <mode> <hash> <path> records; keep these
13
+ * strings intact rather than converting them to decimal permission numbers.
14
+ */
15
+ export const GIT_MODE = {
16
+ file: "100644",
17
+ executable: "100755",
18
+ symlink: "120000",
19
+ submodule: "160000",
20
+ tree: "040000",
21
+ };
22
+ /**
23
+ * Recognizes regular-file Git modes, including executables; symlinks and trees
24
+ * are excluded.
25
+ */
26
+ export const isBlob = (mode) => mode === GIT_MODE.file || mode === GIT_MODE.executable;
27
+ /**
28
+ * Computes the hexadecimal SHA-1 Git object ID from a blob or tree's raw bytes,
29
+ * including its object header.
30
+ */
31
+ function gitObjectHash(type, content) {
8
32
  return createHash("sha1")
9
33
  .update(`${type} ${content.length}\0`)
10
34
  .update(content)
11
35
  .digest("hex");
12
36
  }
13
37
  function treeObject(children) {
14
- const key = (child) => Buffer.from(child.mode === TREE_MODE ? `${child.name}/` : child.name);
38
+ const key = (child) => Buffer.from(child.mode === GIT_MODE.tree ? `${child.name}/` : child.name);
15
39
  return Buffer.concat([...children]
16
40
  .sort((a, b) => Buffer.compare(key(a), key(b)))
17
41
  .flatMap((child) => [
@@ -22,14 +46,15 @@ function treeObject(children) {
22
46
  // Finder scatters these through any folder a developer browses.
23
47
  const ignored = new Set([".git", ".DS_Store"]);
24
48
  /** Reads a folder and names it exactly as Git would, without needing Git. */
25
- export async function folderSnapshot(root) {
49
+ export async function folderSnapshot(root, runtime) {
26
50
  const entries = [];
27
51
  const paths = new Map();
28
52
  async function visit(relative) {
29
53
  const children = [];
30
54
  for (const name of await readdir(join(root, relative))) {
31
- if (ignored.has(name))
55
+ if (ignored.has(name)) {
32
56
  continue;
57
+ }
33
58
  const path = relative ? `${relative}/${name}` : name;
34
59
  const absolute = join(root, path);
35
60
  const info = await lstat(absolute);
@@ -37,19 +62,22 @@ export async function folderSnapshot(root) {
37
62
  let hash;
38
63
  if (info.isDirectory()) {
39
64
  hash = await visit(path);
40
- mode = TREE_MODE;
65
+ mode = GIT_MODE.tree;
41
66
  }
42
67
  else if (info.isSymbolicLink()) {
43
68
  hash = gitObjectHash("blob", Buffer.from(await readlink(absolute)));
44
- mode = "120000";
69
+ mode = GIT_MODE.symlink;
45
70
  }
46
71
  else if (info.isFile()) {
47
72
  hash = gitObjectHash("blob", await readFile(absolute));
48
- mode = info.mode & 0o111 ? "100755" : "100644";
73
+ mode = (await fileExecutable(absolute, info.mode, runtime))
74
+ ? GIT_MODE.executable
75
+ : GIT_MODE.file;
49
76
  paths.set(hash, absolute);
50
77
  }
51
- else
78
+ else {
52
79
  continue;
80
+ }
53
81
  entries.push({ path, mode, hash });
54
82
  children.push({ name, mode, hash });
55
83
  }
@@ -60,15 +88,18 @@ export async function folderSnapshot(root) {
60
88
  entries,
61
89
  readBlobs: async (hashes) => {
62
90
  const blobs = new Map();
63
- for (const hash of new Set(hashes))
64
- blobs.set(hash, await readFile(paths.get(hash)));
91
+ for (const hash of new Set(hashes)) {
92
+ blobs.set(hash, await readFile(defined(paths.get(hash), `Unknown blob ${hash}`)));
93
+ }
65
94
  return blobs;
66
95
  },
67
96
  };
68
97
  }
69
98
  /** Reads a commit through Git plumbing, so .gitattributes cannot alter the bytes. */
70
- export async function gitSnapshot(repo, commit) {
71
- const listing = await git(["ls-tree", "-r", "-t", "-z", commit], { repo });
99
+ export async function gitSnapshot(repo, commit, runtime) {
100
+ const listing = await git(["ls-tree", "-r", "-t", "-z", commit], runtime, {
101
+ repo,
102
+ });
72
103
  const entries = listing
73
104
  .toString("utf8")
74
105
  .split("\0")
@@ -79,23 +110,33 @@ export async function gitSnapshot(repo, commit) {
79
110
  line.slice(line.indexOf("\t") + 1),
80
111
  ];
81
112
  const [mode, , hash] = meta.split(" ");
82
- return { path, mode: mode, hash: hash };
113
+ if (!mode || !hash) {
114
+ throw new Error(`Unexpected git ls-tree line: ${line}`);
115
+ }
116
+ return { path, mode, hash };
83
117
  });
84
118
  return {
85
119
  entries,
86
120
  readBlobs: async (hashes) => {
87
121
  const unique = [...new Set(hashes)];
88
- if (!unique.length)
122
+ if (!unique.length) {
89
123
  return new Map();
90
- const output = await git(["cat-file", "--batch"], {
124
+ }
125
+ const output = await git(["cat-file", "--batch"], runtime, {
91
126
  repo,
92
127
  input: unique.join("\n") + "\n",
93
128
  });
94
129
  const blobs = new Map();
95
130
  let offset = 0;
96
131
  for (const hash of unique) {
97
- const end = output.indexOf(0x0a, offset);
98
- const size = Number(output.toString("utf8", offset, end).split(" ")[2]);
132
+ const end = output.indexOf(LF_BYTE, offset);
133
+ const header = output.toString("utf8", offset, end);
134
+ // `<hash> <type> <size>`, or `<hash> missing` for an absent object.
135
+ const [, type, sizeText] = header.split(" ");
136
+ const size = Number(sizeText);
137
+ if (end < 0 || type === "missing" || !Number.isSafeInteger(size)) {
138
+ throw new Error(`Git object ${hash} is missing from ${repo}: git cat-file answered "${header}"`);
139
+ }
99
140
  blobs.set(hash, output.subarray(end + 1, end + 1 + size));
100
141
  offset = end + 1 + size + 1;
101
142
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,52 @@
1
+ import { lstat, readdir, readFile } from "node:fs/promises";
2
+ import { join } from "node:path";
3
+ import { packageFingerprint, } from "@barndog-ai/api-contract/package";
4
+ import { CliError } from "../../errors.js";
5
+ import { fileExecutable } from "../../platform.js";
6
+ import { ifExists } from "../../shared/files.js";
7
+ /**
8
+ * Every file in an installed folder. Not `readDirectory()` from the contract
9
+ * package: that one reads a package to submit, so it skips `.git` and stops at
10
+ * package size limits. Here everything counts: a `.git` or large file added by
11
+ * hand must show up as a hand edit and be removed by mirroring.
12
+ */
13
+ export async function readTree(directory, runtime) {
14
+ const entries = [];
15
+ async function visit(base) {
16
+ for (const child of (await readdir(join(directory, base))).sort()) {
17
+ const path = base ? `${base}/${child}` : child;
18
+ const info = await lstat(join(directory, path));
19
+ if (info.isDirectory()) {
20
+ await visit(path);
21
+ }
22
+ else if (info.isFile()) {
23
+ entries.push({
24
+ path,
25
+ data: await readFile(join(directory, path)),
26
+ executable: await fileExecutable(join(directory, path), info.mode, runtime),
27
+ });
28
+ }
29
+ else {
30
+ throw new CliError(`${join(directory, path)} isn't a regular file or folder, so BarnDog can't read it.`);
31
+ }
32
+ }
33
+ }
34
+ await visit("");
35
+ return entries;
36
+ }
37
+ /** Fingerprint of an installed folder, or null when it does not exist. */
38
+ export async function treeFingerprint(directory, runtime) {
39
+ const info = await ifExists(lstat(directory));
40
+ if (!info) {
41
+ return null;
42
+ }
43
+ if (!info.isDirectory()) {
44
+ return `not-a-directory:${info.mode}`;
45
+ }
46
+ try {
47
+ return packageFingerprint(await readTree(directory, runtime));
48
+ }
49
+ catch {
50
+ return "unreadable";
51
+ }
52
+ }
@@ -0,0 +1,27 @@
1
+ import { cloudSource } from "./cloud-source.js";
2
+ import { localOverlay, withOverlay } from "./overlay.js";
3
+ import { gitRepoSource, localRepoSource, } from "./repo-source.js";
4
+ /**
5
+ * Builds a lazy Git or local-folder source from settings; returns undefined for
6
+ * a cloud-only setup.
7
+ */
8
+ export function openRepoSource(settings, runtime, options = {}) {
9
+ if (settings.gitWorkspace) {
10
+ return gitRepoSource(settings.gitWorkspace, runtime, options);
11
+ }
12
+ if (settings.localWorkspace && !settings.connection) {
13
+ return localRepoSource(settings.localWorkspace, runtime);
14
+ }
15
+ return undefined;
16
+ }
17
+ /**
18
+ * Builds the configured primary source and merges a retained local overlay when
19
+ * present.
20
+ */
21
+ export function openWorkspaceSource(settings, runtime, options = {}) {
22
+ const primary = settings.connection
23
+ ? cloudSource(settings.connection, runtime)
24
+ : openRepoSource(settings, runtime, options);
25
+ const overlay = localOverlay(settings, runtime);
26
+ return primary && overlay ? withOverlay(primary, overlay) : primary;
27
+ }
@@ -1,16 +1,23 @@
1
+ import { hookSpecificContext, jsonMcpServer, nestedHookEntry, } from "./formats.js";
1
2
  const toolMatcher = "Tool name, such as Bash or Edit|Write. Leave empty for all";
2
3
  export const claude = {
3
4
  id: "claude",
4
5
  name: "Claude Code",
5
6
  skillRoot: ".claude/skills",
6
7
  commands: ["claude"],
7
- macApp: "Claude.app",
8
+ desktopApp: { name: "Claude" },
8
9
  hooks: {
9
10
  settings: [
10
11
  /(^|\/)\.claude\/settings(\.local)?\.json$/,
11
12
  /(^|\/)hooks\/hooks\.json$/,
12
13
  ],
14
+ // Git Bash, as Claude Code's hook reference says (PowerShell only when Git Bash is missing).
15
+ windowsShell: "bash",
13
16
  rootVariables: ["CLAUDE_PLUGIN_ROOT"],
17
+ file: ".claude/settings.json",
18
+ sessionStart: "SessionStart",
19
+ entry: nestedHookEntry,
20
+ sessionContext: hookSpecificContext,
14
21
  events: [
15
22
  {
16
23
  name: "SessionStart",
@@ -54,4 +61,19 @@ export const claude = {
54
61
  },
55
62
  ],
56
63
  },
64
+ mcp: {
65
+ file: ".claude.json",
66
+ files: [".claude.json"],
67
+ path: ["mcpServers"],
68
+ server: (definition, _launcher, kept) => jsonMcpServer(definition, {
69
+ reference: (variable) => `\${${variable}}`,
70
+ typed: true,
71
+ }, kept),
72
+ login: { command: ["claude", "mcp", "login"] },
73
+ },
74
+ configHome: {
75
+ variable: "CLAUDE_CONFIG_DIR",
76
+ directory: ".claude",
77
+ defaultIsSupported: false,
78
+ },
57
79
  };
@@ -1,13 +1,67 @@
1
+ import { hookSpecificContext, nestedHookEntry, secretRefs } from "./formats.js";
2
+ /**
3
+ * Codex forwards variables by name (`env_vars`, `env_http_headers`) instead of
4
+ * interpolating references. It can't give a stdio server a variable under
5
+ * another name; BarnDog's launcher does that. A value BarnDog keeps goes in
6
+ * `env` or `http_headers` as written, the way Codex stores values it's given.
7
+ */
8
+ function mcpServer(definition, launcher, kept) {
9
+ const refs = Object.entries(secretRefs(definition));
10
+ const literal = refs.filter(([, ref]) => kept[ref.secretRef] !== undefined);
11
+ const forwarded = refs.filter(([, ref]) => kept[ref.secretRef] === undefined);
12
+ const values = Object.fromEntries(literal.map(([name, ref]) => [name, kept[ref.secretRef] ?? ""]));
13
+ const variables = forwarded.map(([, ref]) => ref.secretRef);
14
+ if (definition.transport === "http") {
15
+ return {
16
+ url: definition.url,
17
+ ...(literal.length ? { http_headers: values } : {}),
18
+ ...(forwarded.length
19
+ ? {
20
+ env_http_headers: Object.fromEntries(forwarded.map(([name, ref]) => [name, ref.secretRef])),
21
+ }
22
+ : {}),
23
+ };
24
+ }
25
+ const inline = literal.length ? { env: values } : {};
26
+ const renamed = forwarded.some(([name, ref]) => name !== ref.secretRef);
27
+ if (renamed) {
28
+ // The launcher checks only the variables it forwards; kept ones arrive in `env`.
29
+ const environment = Object.fromEntries(forwarded);
30
+ return {
31
+ command: process.execPath,
32
+ args: [
33
+ launcher,
34
+ Buffer.from(JSON.stringify({ ...definition, environment })).toString("base64url"),
35
+ ],
36
+ ...inline,
37
+ env_vars: variables,
38
+ };
39
+ }
40
+ return {
41
+ command: definition.command,
42
+ args: definition.args,
43
+ ...inline,
44
+ env_vars: variables,
45
+ };
46
+ }
1
47
  const toolMatcher = "Tool name, such as Bash or Edit|Write. Leave empty for all";
2
48
  export const codex = {
3
49
  id: "codex",
4
50
  name: "Codex",
5
51
  skillRoot: ".agents/skills",
6
52
  commands: ["codex"],
7
- macApp: "Codex.app",
53
+ desktopApp: { name: "Codex" },
8
54
  hooks: {
9
55
  settings: [/(^|\/)\.codex\/hooks\.json$/, /(^|\/)\.codex\/config\.toml$/],
56
+ // Recent Codex runs hooks in the session shell, PowerShell on Windows;
57
+ // older releases (such as 0.142) used cmd.exe, where these lines fail.
58
+ windowsShell: "powershell",
10
59
  rootVariables: ["PLUGIN_ROOT"],
60
+ file: ".codex/hooks.json",
61
+ sessionStart: "SessionStart",
62
+ entry: nestedHookEntry,
63
+ sessionContext: hookSpecificContext,
64
+ trustNote: "Codex skips new Hooks until you trust them with /hooks.",
11
65
  events: [
12
66
  {
13
67
  name: "SessionStart",
@@ -43,4 +97,16 @@ export const codex = {
43
97
  { name: "SessionEnd" },
44
98
  ],
45
99
  },
100
+ mcp: {
101
+ file: ".codex/config.toml",
102
+ files: [".codex/config.toml"],
103
+ path: ["mcp_servers"],
104
+ server: mcpServer,
105
+ login: { command: ["codex", "mcp", "login"] },
106
+ },
107
+ configHome: {
108
+ variable: "CODEX_HOME",
109
+ directory: ".codex",
110
+ defaultIsSupported: true,
111
+ },
46
112
  };
@@ -1,16 +1,27 @@
1
+ import { jsonMcpServer } from "./formats.js";
1
2
  const toolMatcher = "Tool type, such as Shell, Read or Write";
2
3
  export const cursor = {
3
4
  id: "cursor",
4
5
  name: "Cursor",
5
6
  skillRoot: ".cursor/skills",
6
7
  commands: ["cursor", "cursor-agent"],
7
- macApp: "Cursor.app",
8
+ desktopApp: { name: "Cursor" },
8
9
  hooks: {
9
10
  settings: [
10
11
  /(^|\/)\.cursor\/hooks\.json$/,
11
12
  /(^|\/)hooks\/hooks-cursor\.json$/,
12
13
  ],
14
+ // Cursor's desktop app runs hook commands through PowerShell.
15
+ windowsShell: "powershell",
13
16
  rootVariables: ["CURSOR_PLUGIN_ROOT"],
17
+ file: ".cursor/hooks.json",
18
+ fileVersion: 1,
19
+ sessionStart: "sessionStart",
20
+ entry: (command, matcher) => ({
21
+ command,
22
+ ...(matcher ? { matcher } : {}),
23
+ }),
24
+ sessionContext: (text) => ({ additional_context: text }),
14
25
  events: [
15
26
  { name: "sessionStart" },
16
27
  { name: "sessionEnd" },
@@ -41,4 +52,14 @@ export const cursor = {
41
52
  { name: "stop" },
42
53
  ],
43
54
  },
55
+ mcp: {
56
+ file: ".cursor/mcp.json",
57
+ files: [".cursor/mcp.json"],
58
+ path: ["mcpServers"],
59
+ server: (definition, _launcher, kept) => jsonMcpServer(definition, {
60
+ reference: (variable) => `\${env:${variable}}`,
61
+ typed: false,
62
+ }, kept),
63
+ login: { settings: "Settings → MCP" },
64
+ },
44
65
  };