@buildr-ai/buildr 0.1.0-rc.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 (90) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +15 -0
  3. package/docs/cli-architecture.md +62 -0
  4. package/docs/cli-reference.md +67 -0
  5. package/docs/known-limitations.md +13 -0
  6. package/examples/minimal-workspace/README.md +24 -0
  7. package/package/README.md +28 -0
  8. package/package/bootstrap/contract.yml +122 -0
  9. package/package/bootstrap/guide.md +79 -0
  10. package/package/manifest.yml +144 -0
  11. package/package/targets/runtime/skills/buildr/SKILL.md +151 -0
  12. package/package/targets/workspace/.buildr/workspace.yml +5 -0
  13. package/package/targets/workspace/AGENTS.md +5 -0
  14. package/package/targets/workspace/commands/buildr/openspec/manifest.yml +15 -0
  15. package/package/targets/workspace/commands/manifest.yml +2 -0
  16. package/package/targets/workspace/components/buildr/openspec/component.yml +16 -0
  17. package/package/targets/workspace/components/buildr/openspec/contributions/openspec-apply-sidebar.md +3 -0
  18. package/package/targets/workspace/components/buildr/openspec/contributions/openspec-archive-sidebar.md +3 -0
  19. package/package/targets/workspace/components/buildr/openspec/contributions/openspec-explore-sidebar.md +3 -0
  20. package/package/targets/workspace/components/buildr/openspec/contributions/openspec-propose-sidebar.md +3 -0
  21. package/package/targets/workspace/components/buildr/openspec/contributions/openspec-sync-sidebar.md +3 -0
  22. package/package/targets/workspace/components/buildr/openspec/contributions/task-finish-post-sync.md +1 -0
  23. package/package/targets/workspace/components/buildr/openspec/contributions/task-finish-pre-sync.md +1 -0
  24. package/package/targets/workspace/components/buildr/openspec/contributions/task-triage-change-ready.md +10 -0
  25. package/package/targets/workspace/components/manifest.yml +2 -0
  26. package/package/targets/workspace/gitignore +13 -0
  27. package/package/targets/workspace/projects/AGENTS.project.md +25 -0
  28. package/package/targets/workspace/projects/manifest.yml +2 -0
  29. package/package/targets/workspace/projects/services/manifest.yml +3 -0
  30. package/package/targets/workspace/projects/skills/manifest.yml +2 -0
  31. package/package/targets/workspace/rules/buildr/core.md +63 -0
  32. package/package/targets/workspace/rules/manifest.yml +9 -0
  33. package/package/targets/workspace/skills/buildr/git-ops/SKILL.md +73 -0
  34. package/package/targets/workspace/skills/buildr/openspec-contract-guard/SKILL.md +67 -0
  35. package/package/targets/workspace/skills/buildr/task-finish/SKILL.md +104 -0
  36. package/package/targets/workspace/skills/buildr/task-triage/SKILL.md +103 -0
  37. package/package/targets/workspace/skills/buildr/task-worktree/SKILL.md +104 -0
  38. package/package/targets/workspace/skills/manifest.yml +38 -0
  39. package/package/targets/workspace/skills/openspec/openspec-apply-change/SKILL.md +159 -0
  40. package/package/targets/workspace/skills/openspec/openspec-archive-change/SKILL.md +117 -0
  41. package/package/targets/workspace/skills/openspec/openspec-explore/SKILL.md +287 -0
  42. package/package/targets/workspace/skills/openspec/openspec-propose/SKILL.md +111 -0
  43. package/package/targets/workspace/skills/openspec/openspec-sync-specs/SKILL.md +147 -0
  44. package/package.json +67 -0
  45. package/tools/buildr +11 -0
  46. package/tools/cli/application/cli-update.mjs +219 -0
  47. package/tools/cli/application/compose-runtime.mjs +38 -0
  48. package/tools/cli/application/doctor/runtime-diagnostics.mjs +226 -0
  49. package/tools/cli/application/doctor/scope-diagnostics.mjs +440 -0
  50. package/tools/cli/application/doctor/service-diagnostics.mjs +184 -0
  51. package/tools/cli/application/doctor.mjs +201 -0
  52. package/tools/cli/application/package-maintenance/builtin-receipts.mjs +73 -0
  53. package/tools/cli/application/package-maintenance/output.mjs +148 -0
  54. package/tools/cli/application/package-maintenance/smoke-checks.mjs +469 -0
  55. package/tools/cli/application/package-maintenance/static-validation.mjs +678 -0
  56. package/tools/cli/application/package-maintenance.mjs +538 -0
  57. package/tools/cli/application/runtime.mjs +118 -0
  58. package/tools/cli/application/workspace-operations.mjs +310 -0
  59. package/tools/cli/bootstrap.mjs +6 -0
  60. package/tools/cli/command/help.mjs +292 -0
  61. package/tools/cli/command/registry.mjs +97 -0
  62. package/tools/cli/domains/commands.mjs +661 -0
  63. package/tools/cli/domains/components.mjs +922 -0
  64. package/tools/cli/domains/openspec.mjs +603 -0
  65. package/tools/cli/domains/package-assets.mjs +652 -0
  66. package/tools/cli/domains/rules.mjs +380 -0
  67. package/tools/cli/domains/runtime.mjs +94 -0
  68. package/tools/cli/domains/skills.mjs +647 -0
  69. package/tools/cli/domains/workspace.mjs +571 -0
  70. package/tools/cli/shared/infrastructure.mjs +411 -0
  71. package/tools/cli/shared/json-contracts.mjs +22 -0
  72. package/tools/cli/shared/platform.mjs +52 -0
  73. package/tools/runtime/adapter-contract.mjs +330 -0
  74. package/tools/runtime/check-claude-code.mjs +26 -0
  75. package/tools/runtime/check-codex.mjs +26 -0
  76. package/tools/runtime/projection.mjs +163 -0
  77. package/tools/runtime/render-claude-code-rules.mjs +459 -0
  78. package/tools/runtime/render-claude-code.mjs +59 -0
  79. package/tools/runtime/skills/arguments.mjs +51 -0
  80. package/tools/runtime/skills/contributions.mjs +171 -0
  81. package/tools/runtime/skills/manifests.mjs +167 -0
  82. package/tools/runtime/skills/primitives.mjs +86 -0
  83. package/tools/runtime/skills/render-plan.mjs +244 -0
  84. package/tools/runtime/skills/sources.mjs +276 -0
  85. package/tools/shared/fetch-remote-text.mjs +78 -0
  86. package/tools/verification/integrity/managed-data-integrity.mjs +107 -0
  87. package/tools/verification/integrity/managed-mutations.mjs +72 -0
  88. package/tools/verification/openspec/contract.mjs +212 -0
  89. package/tools/verification/runtime/adapter-contract.mjs +102 -0
  90. package/tools/verification/runtime/adapter-parity.mjs +99 -0
@@ -0,0 +1,647 @@
1
+ import { fs, path, process, YAML } from '../shared/platform.mjs';
2
+
3
+ export function registerDomainsSkills(runtime) {
4
+ const doctor = (...args) => runtime.doctor(...args);
5
+ const isPlainObject = (...args) => runtime.isPlainObject(...args);
6
+ const assertNoUnknownOptions = (...args) => runtime.assertNoUnknownOptions(...args);
7
+ const positionalArgs = (...args) => runtime.positionalArgs(...args);
8
+ const componentOwnerForMember = (...args) => runtime.componentOwnerForMember(...args);
9
+ const readPackageManifest = (...args) => runtime.readPackageManifest(...args);
10
+ const isValidAssetId = (...args) => runtime.isValidAssetId(...args);
11
+ const assertName = (...args) => runtime.assertName(...args);
12
+ const quoteYaml = (...args) => runtime.quoteYaml(...args);
13
+ const parseYamlValue = (...args) => runtime.parseYamlValue(...args);
14
+ const optionValue = (...args) => runtime.optionValue(...args);
15
+ const ensureDirectory = (...args) => runtime.ensureDirectory(...args);
16
+ const atomicWriteFile = (...args) => runtime.atomicWriteFile(...args);
17
+ const parseYamlDocument = (...args) => runtime.parseYamlDocument(...args);
18
+ const assertSafeAssetTarget = (...args) => runtime.assertSafeAssetTarget(...args);
19
+ const withWorkspaceMutation = (...args) => runtime.withWorkspaceMutation(...args);
20
+ const productRoot = (...args) => runtime.productRoot(...args);
21
+ const hasFlag = (...args) => runtime.hasFlag(...args);
22
+ const toPosixRelative = (...args) => runtime.toPosixRelative(...args);
23
+ const existsDirectory = (...args) => runtime.existsDirectory(...args);
24
+ const existsFile = (...args) => runtime.existsFile(...args);
25
+ const assertInitializedBuildrWorkspace = (...args) => runtime.assertInitializedBuildrWorkspace(...args);
26
+
27
+ function readSkillManifest(file) {
28
+ {
29
+ const parsed = parseYamlDocument(fs.readFileSync(file, 'utf8'), toPosixRelative(process.cwd(), file));
30
+ return Array.isArray(parsed.skills) ? parsed.skills : [];
31
+ }
32
+ const skills = [];
33
+ let currentSkill = null;
34
+ let currentObject = null;
35
+
36
+ function finishSkill() {
37
+ if (!currentSkill) return;
38
+ skills.push(currentSkill);
39
+ currentSkill = null;
40
+ currentObject = null;
41
+ }
42
+
43
+ for (const rawLine of fs.readFileSync(file, 'utf8').split(/\r?\n/)) {
44
+ const line = rawLine.trimEnd();
45
+ const trimmed = line.trim();
46
+ if (!trimmed || trimmed.startsWith('#')) continue;
47
+
48
+ const idMatch = trimmed.match(/^-\s+id:\s*(.+)$/);
49
+ if (idMatch) {
50
+ finishSkill();
51
+ currentSkill = { id: parseYamlValue(idMatch[1].trim()) };
52
+ currentObject = null;
53
+ continue;
54
+ }
55
+
56
+ if (!currentSkill) continue;
57
+
58
+ const objectStartMatch = trimmed.match(/^(source|resolved|install):\s*$/);
59
+ if (objectStartMatch) {
60
+ currentObject = objectStartMatch[1];
61
+ currentSkill[currentObject] = {};
62
+ continue;
63
+ }
64
+
65
+ const nestedMatch = rawLine.match(/^\s{6}([A-Za-z][A-Za-z0-9_-]*):\s*(.+)$/);
66
+ if (nestedMatch && currentObject) {
67
+ currentSkill[currentObject][nestedMatch[1]] = parseYamlValue(nestedMatch[2].trim());
68
+ continue;
69
+ }
70
+
71
+ currentObject = null;
72
+
73
+ const pathMatch = trimmed.match(/^path:\s*(.+)$/);
74
+ if (pathMatch) {
75
+ currentSkill.path = parseYamlValue(pathMatch[1].trim());
76
+ continue;
77
+ }
78
+ const sourceMatch = trimmed.match(/^source:\s*(.+)$/);
79
+ if (sourceMatch) {
80
+ currentSkill.source = parseYamlValue(sourceMatch[1].trim());
81
+ continue;
82
+ }
83
+ const resolvedMatch = trimmed.match(/^resolved:\s*(.+)$/);
84
+ if (resolvedMatch) {
85
+ currentSkill.resolved = parseYamlValue(resolvedMatch[1].trim());
86
+ continue;
87
+ }
88
+ const installMatch = trimmed.match(/^install:\s*(.+)$/);
89
+ if (installMatch) {
90
+ currentSkill.install = parseYamlValue(installMatch[1].trim());
91
+ continue;
92
+ }
93
+ const descriptionMatch = trimmed.match(/^description:\s*(.+)$/);
94
+ if (descriptionMatch) {
95
+ currentSkill.description = parseYamlValue(descriptionMatch[1].trim());
96
+ continue;
97
+ }
98
+ const enabledMatch = trimmed.match(/^enabled:\s*(.+)$/);
99
+ if (enabledMatch) {
100
+ currentSkill.enabled = parseYamlValue(enabledMatch[1].trim());
101
+ continue;
102
+ }
103
+ const requiredMatch = trimmed.match(/^required:\s*(.+)$/);
104
+ if (requiredMatch) {
105
+ currentSkill.required = parseYamlValue(requiredMatch[1].trim());
106
+ continue;
107
+ }
108
+ const stateMatch = trimmed.match(/^state:\s*(.+)$/);
109
+ if (stateMatch) {
110
+ currentSkill.state = parseYamlValue(stateMatch[1].trim());
111
+ continue;
112
+ }
113
+ const runtimesMatch = trimmed.match(/^runtimes:\s*(.+)$/);
114
+ if (runtimesMatch) {
115
+ currentSkill.runtimes = parseYamlValue(runtimesMatch[1].trim());
116
+ continue;
117
+ }
118
+ const runtimePathMatch = trimmed.match(/^runtimePath:\s*(.+)$/);
119
+ if (runtimePathMatch) {
120
+ currentSkill.runtimePath = parseYamlValue(runtimePathMatch[1].trim());
121
+ continue;
122
+ }
123
+ }
124
+
125
+ finishSkill();
126
+ return skills;
127
+ }
128
+
129
+ function readSkillManifestSchemaVersion(file) {
130
+ if (!existsFile(file)) return null;
131
+ return parseYamlDocument(fs.readFileSync(file, 'utf8'), toPosixRelative(process.cwd(), file)).schemaVersion || null;
132
+ }
133
+
134
+ function renderYamlObject(lines, indent, key, value) {
135
+ if (value === undefined) return;
136
+ if (isPlainObject(value)) {
137
+ lines.push(`${indent}${key}:`);
138
+ for (const [nestedKey, nestedValue] of Object.entries(value)) {
139
+ if (nestedValue !== undefined) lines.push(`${indent} ${nestedKey}: ${quoteYaml(nestedValue)}`);
140
+ }
141
+ } else {
142
+ lines.push(`${indent}${key}: ${quoteYaml(value)}`);
143
+ }
144
+ }
145
+
146
+ function renderSkillsManifestYaml(skills) {
147
+ const lines = ['schemaVersion: buildr.skills/v1'];
148
+ if (!skills || skills.length === 0) {
149
+ lines.push('skills: []');
150
+ return `${lines.join('\n')}\n`;
151
+ }
152
+ lines.push('skills:');
153
+ for (const skill of skills) {
154
+ lines.push(` - id: ${quoteYaml(skill.id)}`);
155
+ if (skill.source !== undefined && skill.path !== undefined && typeof skill.source === 'string' && isManifestSourceLabel(skill.source)) {
156
+ lines.push(` source: ${quoteYaml(skill.source)}`);
157
+ }
158
+ if (skill.path !== undefined) {
159
+ lines.push(` path: ${quoteYaml(skill.path)}`);
160
+ } else {
161
+ renderYamlObject(lines, ' ', 'source', skill.source);
162
+ renderYamlObject(lines, ' ', 'resolved', skill.resolved);
163
+ renderYamlObject(lines, ' ', 'install', skill.install);
164
+ }
165
+ if (skill.description !== undefined) {
166
+ lines.push(` description: ${quoteYaml(skill.description)}`);
167
+ }
168
+ if (skill.enabled !== undefined) lines.push(` enabled: ${quoteYaml(Boolean(skill.enabled))}`);
169
+ if (skill.required !== undefined) lines.push(` required: ${quoteYaml(Boolean(skill.required))}`);
170
+ if (skill.state !== undefined) lines.push(` state: ${quoteYaml(skill.state)}`);
171
+ if (skill.runtimes !== undefined) lines.push(` runtimes: ${quoteYaml(skill.runtimes)}`);
172
+ if (skill.runtimePath !== undefined) lines.push(` runtimePath: ${quoteYaml(skill.runtimePath)}`);
173
+ }
174
+ return `${lines.join('\n')}\n`;
175
+ }
176
+
177
+ function validateSkillManifestEntries(skills, manifestPath) {
178
+ const ids = new Set();
179
+ for (const [index, skill] of skills.entries()) {
180
+ const label = `skills[${index}]`;
181
+ if (!isValidAssetId(skill.id)) {
182
+ throw new Error(`${label}.id must contain only letters, digits, dots, underscores, or dashes in ${manifestPath}`);
183
+ }
184
+ if (ids.has(skill.id)) {
185
+ throw new Error(`Duplicate skill id in ${manifestPath}: ${skill.id}`);
186
+ }
187
+ ids.add(skill.id);
188
+ const hasPath = skill.path !== undefined;
189
+ const hasSource = skill.source !== undefined;
190
+ const hasResolved = skill.resolved !== undefined;
191
+ const hasSourceLabel = hasPath && typeof skill.source === 'string' && isManifestSourceLabel(skill.source);
192
+ if (hasPath && ((hasSource && !hasSourceLabel) || hasResolved)) {
193
+ throw new Error(`${label} must not combine path with source or resolved in ${manifestPath}`);
194
+ }
195
+ if (!hasPath && !hasSource && !hasResolved) {
196
+ throw new Error(`${label} must include path, source, or resolved in ${manifestPath}`);
197
+ }
198
+ if (hasPath) {
199
+ if (!skill.path || typeof skill.path !== 'string') {
200
+ throw new Error(`${label}.path must be a string in ${manifestPath}`);
201
+ }
202
+ normalizeRelativePathForBuildr(skill.path, `Skill path must stay inside skills root: ${skill.path}`);
203
+ }
204
+ if (hasSource && !hasSourceLabel) {
205
+ if (typeof skill.source === 'string') {
206
+ parseSkillSourceRef(skill.source);
207
+ } else if (isPlainObject(skill.source)) {
208
+ validateSkillUrlObject(skill.source, `${label}.source`, manifestPath);
209
+ } else {
210
+ throw new Error(`${label}.source must be a string or object in ${manifestPath}`);
211
+ }
212
+ }
213
+ if (hasResolved) {
214
+ if (!isPlainObject(skill.resolved)) {
215
+ throw new Error(`${label}.resolved must be an object in ${manifestPath}`);
216
+ }
217
+ validateResolvedSkillSource(skill.resolved, `${label}.resolved`, manifestPath);
218
+ }
219
+ if (skill.install !== undefined) {
220
+ if (!isPlainObject(skill.install)) {
221
+ throw new Error(`${label}.install must be an object in ${manifestPath}`);
222
+ }
223
+ if (skill.install.mode !== undefined && !['agent', 'buildr'].includes(skill.install.mode)) {
224
+ throw new Error(`${label}.install.mode must be agent or buildr in ${manifestPath}`);
225
+ }
226
+ }
227
+ if (skill.description !== undefined && typeof skill.description !== 'string') {
228
+ throw new Error(`${label}.description must be a string in ${manifestPath}`);
229
+ }
230
+ if (skill.enabled !== undefined && typeof skill.enabled !== 'boolean') {
231
+ throw new Error(`${label}.enabled must be a boolean in ${manifestPath}`);
232
+ }
233
+ if (skill.required !== undefined && typeof skill.required !== 'boolean') {
234
+ throw new Error(`${label}.required must be a boolean in ${manifestPath}`);
235
+ }
236
+ if (skill.state !== undefined && !['installed', 'modified', 'uninstalled', 'missing'].includes(skill.state)) {
237
+ throw new Error(`${label}.state must be installed, modified, uninstalled, or missing in ${manifestPath}`);
238
+ }
239
+ if (skill.runtimes !== undefined && (!Array.isArray(skill.runtimes) || !skill.runtimes.every((runtime) => typeof runtime === 'string'))) {
240
+ throw new Error(`${label}.runtimes must be an array of strings in ${manifestPath}`);
241
+ }
242
+ }
243
+ }
244
+
245
+ function isManifestSourceLabel(value) {
246
+ return ['buildr', 'openspec', 'workspace', 'project', 'service'].includes(value);
247
+ }
248
+
249
+ function validateSkillUrlObject(value, label, manifestPath) {
250
+ if (!value.kind || typeof value.kind !== 'string') {
251
+ throw new Error(`${label}.kind must be a string in ${manifestPath}`);
252
+ }
253
+ if (!value.url || typeof value.url !== 'string') {
254
+ throw new Error(`${label}.url must be a string in ${manifestPath}`);
255
+ }
256
+ }
257
+
258
+ function validateResolvedSkillSource(value, label, manifestPath) {
259
+ validateSkillUrlObject(value, label, manifestPath);
260
+ if (value.kind !== 'skill-url') {
261
+ throw new Error(`${label}.kind is not supported by this CLI: ${value.kind}`);
262
+ }
263
+ if (value.version !== undefined && typeof value.version !== 'string') {
264
+ throw new Error(`${label}.version must be a string in ${manifestPath}`);
265
+ }
266
+ if (value.integrity !== undefined && typeof value.integrity !== 'string') {
267
+ throw new Error(`${label}.integrity must be a string in ${manifestPath}`);
268
+ }
269
+ }
270
+
271
+ function normalizeRelativePathForBuildr(input, message) {
272
+ const normalized = path.normalize(input).replace(/^\.[\\/]/, '');
273
+ if (!normalized || path.isAbsolute(normalized) || normalized === '..' || normalized.startsWith(`..${path.sep}`)) {
274
+ throw new Error(message);
275
+ }
276
+ return normalized.split(path.sep).join('/');
277
+ }
278
+
279
+ function parseSkillSourceRef(sourceRef) {
280
+ if (!sourceRef || typeof sourceRef !== 'string') {
281
+ throw new Error(`Skill source reference must be a string: ${sourceRef || ''}`);
282
+ }
283
+ const match = sourceRef.match(/^package:([A-Za-z0-9._-]+)$/);
284
+ if (!match) {
285
+ throw new Error(`Unsupported Skill source reference: ${sourceRef}. Supported format: package:<source-id>`);
286
+ }
287
+ return { type: 'package', id: match[1] };
288
+ }
289
+
290
+ function assertHttpUrl(value, label) {
291
+ let parsed;
292
+ try {
293
+ parsed = new URL(value);
294
+ } catch {
295
+ throw new Error(`${label} must be a valid URL: ${value}`);
296
+ }
297
+ if (!['http:', 'https:'].includes(parsed.protocol)) {
298
+ throw new Error(`${label} must use http or https: ${value}`);
299
+ }
300
+ }
301
+
302
+ function resolvePackageSkillSourceRef(sourceRef, options = {}) {
303
+ const parsed = parseSkillSourceRef(sourceRef);
304
+ const manifest = readPackageManifest();
305
+ const source = manifest.skillSources.find((entry) => entry.id === parsed.id);
306
+ if (!source) {
307
+ throw new Error(`Package Skill source not found: ${sourceRef}`);
308
+ }
309
+ if (!source.path || typeof source.path !== 'string') {
310
+ throw new Error(`Package Skill source must include path: ${source.id}`);
311
+ }
312
+ if (!Array.isArray(source.runtimes) || source.runtimes.length === 0) {
313
+ throw new Error(`Package Skill source must declare at least one runtime: ${source.id}`);
314
+ }
315
+ if (options.runtime && !source.runtimes.includes(options.runtime)) {
316
+ throw new Error(`Package Skill source ${sourceRef} does not support runtime: ${options.runtime}`);
317
+ }
318
+ if (path.isAbsolute(source.path) || source.path.startsWith('..')) {
319
+ throw new Error(`Package Skill source path must stay inside product root: ${source.path}`);
320
+ }
321
+ if (source.runtimePath !== undefined) {
322
+ normalizeRelativePathForBuildr(source.runtimePath, `Package Skill source runtimePath must stay relative: ${source.runtimePath}`);
323
+ }
324
+ const sourceDir = path.resolve(productRoot(), source.path);
325
+ const skillFile = path.join(sourceDir, 'SKILL.md');
326
+ if (!existsFile(skillFile)) {
327
+ throw new Error(`Package Skill source SKILL.md does not exist: ${source.path}/SKILL.md`);
328
+ }
329
+ const metadata = parseSkillFrontmatter(skillFile);
330
+ return { ...parsed, entry: source, sourceDir, skillFile, metadata };
331
+ }
332
+
333
+ function scopeRootForSkills(targetRoot, scope) {
334
+ const normalizedScope = scope === '.' ? '.' : normalizeRelativePathForBuildr(scope, `Unsupported skills scope: ${scope}`);
335
+ const parts = normalizedScope === '.' ? ['.'] : normalizedScope.split(path.sep);
336
+ if (parts[0] === '.') return { scope: '.', scopeRoot: targetRoot };
337
+ if (parts[0] === 'projects' && parts.length === 2) {
338
+ const scopeRoot = path.join(targetRoot, 'projects', parts[1]);
339
+ if (!existsDirectory(scopeRoot)) throw new Error(`Project scope does not exist: ${normalizedScope}`);
340
+ return { scope: normalizedScope.split(path.sep).join('/'), scopeRoot };
341
+ }
342
+ throw new Error(`Unsupported skills scope. Use . or projects/<project>: ${scope}`);
343
+ }
344
+
345
+ function skillsManifestPath(scopeRoot) {
346
+ return path.join(scopeRoot, 'skills', 'manifest.yml');
347
+ }
348
+
349
+ function readSkillsManifestForWrite(scopeRoot) {
350
+ const manifestPath = skillsManifestPath(scopeRoot);
351
+ if (!existsFile(manifestPath)) return [];
352
+ const skills = readSkillManifest(manifestPath);
353
+ validateSkillManifestEntries(skills, manifestPath);
354
+ return skills;
355
+ }
356
+
357
+ function writeSkillsManifest(scopeRoot, skills) {
358
+ const manifestPath = skillsManifestPath(scopeRoot);
359
+ atomicWriteFile(manifestPath, renderSkillsManifestYaml(skills));
360
+ return manifestPath;
361
+ }
362
+
363
+ function parseSkillFrontmatter(skillFile) {
364
+ const content = fs.readFileSync(skillFile, 'utf8');
365
+ const lines = content.split(/\r?\n/);
366
+ if (lines[0] !== '---') {
367
+ throw new Error(`SKILL.md must start with YAML frontmatter: ${skillFile}`);
368
+ }
369
+ const endIndex = lines.findIndex((line, index) => index > 0 && line === '---');
370
+ if (endIndex === -1) {
371
+ throw new Error(`SKILL.md frontmatter is not closed: ${skillFile}`);
372
+ }
373
+ const metadata = parseYamlDocument(lines.slice(1, endIndex).join('\n'), `SKILL.md frontmatter: ${skillFile}`);
374
+ if (!metadata.name || typeof metadata.name !== 'string') {
375
+ throw new Error(`SKILL.md frontmatter must declare name: ${skillFile}`);
376
+ }
377
+ assertName(metadata.name, 'Skill name');
378
+ if (metadata.description !== undefined && typeof metadata.description !== 'string') {
379
+ throw new Error(`SKILL.md frontmatter description must be a string: ${skillFile}`);
380
+ }
381
+ return metadata;
382
+ }
383
+
384
+ function supportedSkillSourceEntries() {
385
+ return new Set(['SKILL.md', 'scripts', 'templates', 'assets', 'examples', 'references']);
386
+ }
387
+
388
+ function inspectSkillSource(sourceDir) {
389
+ if (!existsDirectory(sourceDir)) throw new Error(`Skill source directory does not exist: ${sourceDir}`);
390
+ const skillFile = path.join(sourceDir, 'SKILL.md');
391
+ if (!existsFile(skillFile)) throw new Error(`Skill source must contain SKILL.md: ${sourceDir}`);
392
+ const metadata = parseSkillFrontmatter(skillFile);
393
+ const supported = supportedSkillSourceEntries();
394
+ const entries = fs.readdirSync(sourceDir).sort();
395
+ const unknownEntries = entries.filter((entry) => !supported.has(entry));
396
+ return { skillFile, metadata, entries, unknownEntries };
397
+ }
398
+
399
+ function samePath(left, right) {
400
+ const leftResolved = path.resolve(left);
401
+ const rightResolved = path.resolve(right);
402
+ try {
403
+ return fs.realpathSync(leftResolved) === fs.realpathSync(rightResolved);
404
+ } catch {
405
+ return leftResolved === rightResolved;
406
+ }
407
+ }
408
+
409
+ function copySupportedSkillSource(sourceDir, targetDir, entries, options = {}) {
410
+ if (samePath(sourceDir, targetDir)) return;
411
+ if (existsDirectory(targetDir)) {
412
+ if (!options.replace) {
413
+ throw new Error(`Skill target directory already exists: ${targetDir}. Use --replace to replace the whole directory.`);
414
+ }
415
+ fs.rmSync(targetDir, { recursive: true, force: true });
416
+ }
417
+ ensureDirectory(targetDir);
418
+ const supported = supportedSkillSourceEntries();
419
+ for (const entry of entries) {
420
+ if (!supported.has(entry)) continue;
421
+ const sourcePath = path.join(sourceDir, entry);
422
+ const targetPath = path.join(targetDir, entry);
423
+ if (existsDirectory(sourcePath)) {
424
+ fs.cpSync(sourcePath, targetPath, { recursive: true });
425
+ } else if (existsFile(sourcePath)) {
426
+ ensureDirectory(path.dirname(targetPath));
427
+ fs.copyFileSync(sourcePath, targetPath);
428
+ }
429
+ }
430
+ }
431
+
432
+ function printSkillsMutationReceipt(action, targetRoot, id, updatedPaths, skippedEntries, nextAction, assetLabel = 'Skill 源资产') {
433
+ console.log(`已${action} ${assetLabel}:${id}`);
434
+ console.log('已更新 Buildr 源资产:');
435
+ for (const file of updatedPaths) console.log(` ${toPosixRelative(targetRoot, file)}`);
436
+ if (skippedEntries.length > 0) {
437
+ console.log('未装载的顶层内容:');
438
+ for (const entry of skippedEntries) console.log(` ${entry}`);
439
+ }
440
+ console.log('下一步:');
441
+ console.log(` ${nextAction}`);
442
+ }
443
+
444
+ function skillsAddUnsafe(args) {
445
+ const allowedFlags = new Set([
446
+ '--target',
447
+ '--scope',
448
+ '--source',
449
+ '--remote-source',
450
+ '--source-kind',
451
+ '--resolved-source',
452
+ '--resolved-kind',
453
+ '--version',
454
+ '--integrity',
455
+ '--description',
456
+ '--replace',
457
+ '--ignore-unsupported',
458
+ ]);
459
+ assertNoUnknownOptions(args, allowedFlags, new Set(['--replace', '--ignore-unsupported']));
460
+ const [explicitId] = positionalArgs(args);
461
+ const targetRoot = path.resolve(optionValue(args, '--target', process.cwd()));
462
+ const scopeInput = optionValue(args, '--scope', null);
463
+ const sourceInput = optionValue(args, '--source', null);
464
+ const remoteSourceInput = optionValue(args, '--remote-source', null);
465
+ const sourceKindInput = optionValue(args, '--source-kind', 'url');
466
+ const resolvedSourceInput = optionValue(args, '--resolved-source', null);
467
+ const resolvedKindInput = optionValue(args, '--resolved-kind', 'skill-url');
468
+ const versionInput = optionValue(args, '--version', null);
469
+ const integrityInput = optionValue(args, '--integrity', null);
470
+ const descriptionInput = optionValue(args, '--description', null);
471
+ if (!scopeInput) throw new Error('Missing required option: --scope');
472
+ if (sourceInput && (remoteSourceInput || resolvedSourceInput)) {
473
+ throw new Error('--source cannot be combined with --remote-source or --resolved-source.');
474
+ }
475
+ if (!sourceInput && !remoteSourceInput && !resolvedSourceInput) {
476
+ throw new Error('Specify one of --source, --remote-source, or --resolved-source.');
477
+ }
478
+ assertInitializedBuildrWorkspace(targetRoot);
479
+
480
+ const { scopeRoot } = scopeRootForSkills(targetRoot, scopeInput);
481
+ const manifest = readSkillsManifestForWrite(scopeRoot);
482
+ const replace = hasFlag(args, '--replace');
483
+ const ignoreUnsupported = hasFlag(args, '--ignore-unsupported');
484
+
485
+ if (remoteSourceInput || resolvedSourceInput) {
486
+ if (!explicitId) throw new Error('Missing skill id for remote Skill registration.');
487
+ assertName(explicitId, 'Skill id');
488
+ if (ignoreUnsupported) throw new Error('--ignore-unsupported can only be used with --source.');
489
+ if (resolvedSourceInput && resolvedKindInput !== 'skill-url') {
490
+ throw new Error(`Unsupported resolved kind: ${resolvedKindInput}. Supported kind: skill-url`);
491
+ }
492
+ if (remoteSourceInput) assertHttpUrl(remoteSourceInput, '--remote-source');
493
+ if (resolvedSourceInput) assertHttpUrl(resolvedSourceInput, '--resolved-source');
494
+ const existingIndex = manifest.findIndex((skill) => skill.id === explicitId);
495
+ if (existingIndex !== -1 && !replace) {
496
+ throw new Error(`Skill already exists in skills/manifest.yml: ${explicitId}. Use --replace to replace the whole entry.`);
497
+ }
498
+ const manifestEntry = { id: explicitId };
499
+ if (remoteSourceInput) {
500
+ manifestEntry.source = { kind: sourceKindInput, url: remoteSourceInput };
501
+ }
502
+ if (resolvedSourceInput) {
503
+ manifestEntry.resolved = { kind: resolvedKindInput, url: resolvedSourceInput };
504
+ if (versionInput) manifestEntry.resolved.version = versionInput;
505
+ if (integrityInput) manifestEntry.resolved.integrity = integrityInput;
506
+ manifestEntry.install = { mode: 'buildr' };
507
+ } else {
508
+ manifestEntry.install = { mode: 'agent' };
509
+ }
510
+ if (descriptionInput) manifestEntry.description = descriptionInput;
511
+ if (existingIndex === -1) {
512
+ manifest.push(manifestEntry);
513
+ } else {
514
+ manifest[existingIndex] = manifestEntry;
515
+ }
516
+ const manifestPath = writeSkillsManifest(scopeRoot, manifest);
517
+ printSkillsMutationReceipt(
518
+ existingIndex === -1 ? '添加' : '替换',
519
+ targetRoot,
520
+ explicitId,
521
+ [manifestPath],
522
+ [],
523
+ '如果当前 Agent 需要 Skills runtime 渲染,按当前 Agent runtime 能力执行 Skills render、runtime check 或 doctor。',
524
+ resolvedSourceInput ? 'Skill 已解析远端资产' : 'Skill 远端信息源'
525
+ );
526
+ return;
527
+ }
528
+
529
+ if (sourceKindInput !== 'url' || resolvedKindInput !== 'skill-url' || versionInput || integrityInput || descriptionInput) {
530
+ throw new Error('--source-kind, --resolved-kind, --version, --integrity, and --description for remote registration cannot be used with --source.');
531
+ }
532
+ const sourceDir = path.resolve(sourceInput);
533
+ const source = inspectSkillSource(sourceDir);
534
+ const skillId = source.metadata.name;
535
+ if (explicitId && explicitId !== skillId) {
536
+ throw new Error(`Explicit skill id does not match SKILL.md frontmatter name: ${explicitId} != ${skillId}`);
537
+ }
538
+ if (source.unknownEntries.length > 0 && !ignoreUnsupported) {
539
+ throw new Error(`Skill source contains unsupported top-level entries: ${source.unknownEntries.join(', ')}. Use --ignore-unsupported to skip them.`);
540
+ }
541
+
542
+ const skillsRoot = path.join(scopeRoot, 'skills');
543
+ const targetDir = path.join(skillsRoot, skillId);
544
+ const existingIndex = manifest.findIndex((skill) => skill.id === skillId);
545
+ if (existingIndex !== -1 && !replace) {
546
+ throw new Error(`Skill already exists in skills/manifest.yml: ${skillId}. Use --replace to replace the whole entry.`);
547
+ }
548
+
549
+ copySupportedSkillSource(sourceDir, targetDir, source.entries, { replace });
550
+ const manifestEntry = { id: skillId, path: skillId };
551
+ if (source.metadata.description) manifestEntry.description = source.metadata.description;
552
+ if (existingIndex === -1) {
553
+ manifest.push(manifestEntry);
554
+ } else {
555
+ manifest[existingIndex] = manifestEntry;
556
+ }
557
+ const manifestPath = writeSkillsManifest(scopeRoot, manifest);
558
+ const updatedPaths = samePath(sourceDir, targetDir) ? [manifestPath] : [manifestPath, targetDir];
559
+ printSkillsMutationReceipt(
560
+ existingIndex === -1 ? '添加' : '替换',
561
+ targetRoot,
562
+ skillId,
563
+ updatedPaths,
564
+ ignoreUnsupported ? source.unknownEntries : [],
565
+ '如果当前 Agent 需要 Skills runtime 渲染,按当前 Agent runtime 能力执行 Skills render、runtime check 或 doctor。'
566
+ );
567
+ }
568
+
569
+ function skillsAdd(args) {
570
+ const targetRoot = path.resolve(optionValue(args, '--target', process.cwd()));
571
+ const scopeInput = optionValue(args, '--scope', '.');
572
+ const { scopeRoot } = scopeRootForSkills(targetRoot, scopeInput);
573
+ return withWorkspaceMutation(targetRoot, 'skills.add', [path.join(scopeRoot, 'skills')], () => skillsAddUnsafe(args));
574
+ }
575
+
576
+ function safeSkillSourceDir(scopeRoot, skillPath) {
577
+ const normalized = normalizeRelativePathForBuildr(skillPath, `Skill path must stay inside skills root: ${skillPath}`);
578
+ const skillsRoot = path.join(scopeRoot, 'skills');
579
+ const sourceDir = path.resolve(skillsRoot, normalized);
580
+ const relative = path.relative(skillsRoot, sourceDir);
581
+ if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) {
582
+ throw new Error(`Refusing to delete unsafe Skill path: ${skillPath}`);
583
+ }
584
+ return assertSafeAssetTarget(scopeRoot, sourceDir, skillsRoot, 'Skill delete target');
585
+ }
586
+
587
+ function skillsRemoveUnsafe(args) {
588
+ const allowedFlags = new Set(['--target', '--scope']);
589
+ assertNoUnknownOptions(args, allowedFlags);
590
+ const [id] = positionalArgs(args);
591
+ if (!id) throw new Error('Missing skill id');
592
+ assertName(id, 'Skill id');
593
+ const targetRoot = path.resolve(optionValue(args, '--target', process.cwd()));
594
+ const scopeInput = optionValue(args, '--scope', null);
595
+ if (!scopeInput) throw new Error('Missing required option: --scope');
596
+ assertInitializedBuildrWorkspace(targetRoot);
597
+
598
+ const { scopeRoot } = scopeRootForSkills(targetRoot, scopeInput);
599
+ const manifest = readSkillsManifestForWrite(scopeRoot);
600
+ const existingIndex = manifest.findIndex((skill) => skill.id === id);
601
+ if (existingIndex === -1) {
602
+ throw new Error(`Skill not found in skills/manifest.yml: ${id}`);
603
+ }
604
+ const removed = manifest[existingIndex];
605
+ if (scopeRoot === targetRoot && removed.path) {
606
+ const owner = componentOwnerForMember(targetRoot, `skills/${removed.path}`);
607
+ if (owner) throw new Error(`Skill is managed by Component ${owner}: skills/${removed.path}. Use buildr component lifecycle commands.`);
608
+ }
609
+ manifest.splice(existingIndex, 1);
610
+ const updatedPaths = [];
611
+ let assetLabel = 'Skill 源资产';
612
+ if (removed.path) {
613
+ const sharedReference = manifest.find((skill) => skill.path === removed.path);
614
+ if (sharedReference) {
615
+ throw new Error(`Refusing to remove Skill source shared by another manifest entry: ${removed.path}`);
616
+ }
617
+ const sourceDir = safeSkillSourceDir(scopeRoot, removed.path);
618
+ const manifestPath = writeSkillsManifest(scopeRoot, manifest);
619
+ updatedPaths.push(manifestPath);
620
+ if (existsDirectory(sourceDir)) fs.rmSync(sourceDir, { recursive: true, force: true });
621
+ updatedPaths.push(sourceDir);
622
+ } else {
623
+ const manifestPath = writeSkillsManifest(scopeRoot, manifest);
624
+ updatedPaths.push(manifestPath);
625
+ assetLabel = 'Skill 远端资产';
626
+ }
627
+ printSkillsMutationReceipt(
628
+ '删除',
629
+ targetRoot,
630
+ id,
631
+ updatedPaths,
632
+ [],
633
+ '如果当前 Agent runtime 已渲染该 Skill,按当前 Agent runtime 能力执行 Skills render、runtime check 或 doctor。',
634
+ assetLabel
635
+ );
636
+ }
637
+
638
+ function skillsRemove(args) {
639
+ const targetRoot = path.resolve(optionValue(args, '--target', process.cwd()));
640
+ const scopeInput = optionValue(args, '--scope', '.');
641
+ const { scopeRoot } = scopeRootForSkills(targetRoot, scopeInput);
642
+ return withWorkspaceMutation(targetRoot, 'skills.remove', [path.join(scopeRoot, 'skills')], () => skillsRemoveUnsafe(args));
643
+ }
644
+
645
+ Object.assign(runtime, { readSkillManifest, readSkillManifestSchemaVersion, renderYamlObject, renderSkillsManifestYaml, validateSkillManifestEntries, isManifestSourceLabel, validateSkillUrlObject, validateResolvedSkillSource, normalizeRelativePathForBuildr, parseSkillSourceRef, assertHttpUrl, resolvePackageSkillSourceRef, scopeRootForSkills, skillsManifestPath, readSkillsManifestForWrite, writeSkillsManifest, parseSkillFrontmatter, supportedSkillSourceEntries, inspectSkillSource, samePath, copySupportedSkillSource, printSkillsMutationReceipt, skillsAddUnsafe, skillsAdd, safeSkillSourceDir, skillsRemoveUnsafe, skillsRemove });
646
+ return runtime;
647
+ }