@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.
- package/LICENSE +21 -0
- package/README.md +15 -0
- package/docs/cli-architecture.md +62 -0
- package/docs/cli-reference.md +67 -0
- package/docs/known-limitations.md +13 -0
- package/examples/minimal-workspace/README.md +24 -0
- package/package/README.md +28 -0
- package/package/bootstrap/contract.yml +122 -0
- package/package/bootstrap/guide.md +79 -0
- package/package/manifest.yml +144 -0
- package/package/targets/runtime/skills/buildr/SKILL.md +151 -0
- package/package/targets/workspace/.buildr/workspace.yml +5 -0
- package/package/targets/workspace/AGENTS.md +5 -0
- package/package/targets/workspace/commands/buildr/openspec/manifest.yml +15 -0
- package/package/targets/workspace/commands/manifest.yml +2 -0
- package/package/targets/workspace/components/buildr/openspec/component.yml +16 -0
- package/package/targets/workspace/components/buildr/openspec/contributions/openspec-apply-sidebar.md +3 -0
- package/package/targets/workspace/components/buildr/openspec/contributions/openspec-archive-sidebar.md +3 -0
- package/package/targets/workspace/components/buildr/openspec/contributions/openspec-explore-sidebar.md +3 -0
- package/package/targets/workspace/components/buildr/openspec/contributions/openspec-propose-sidebar.md +3 -0
- package/package/targets/workspace/components/buildr/openspec/contributions/openspec-sync-sidebar.md +3 -0
- package/package/targets/workspace/components/buildr/openspec/contributions/task-finish-post-sync.md +1 -0
- package/package/targets/workspace/components/buildr/openspec/contributions/task-finish-pre-sync.md +1 -0
- package/package/targets/workspace/components/buildr/openspec/contributions/task-triage-change-ready.md +10 -0
- package/package/targets/workspace/components/manifest.yml +2 -0
- package/package/targets/workspace/gitignore +13 -0
- package/package/targets/workspace/projects/AGENTS.project.md +25 -0
- package/package/targets/workspace/projects/manifest.yml +2 -0
- package/package/targets/workspace/projects/services/manifest.yml +3 -0
- package/package/targets/workspace/projects/skills/manifest.yml +2 -0
- package/package/targets/workspace/rules/buildr/core.md +63 -0
- package/package/targets/workspace/rules/manifest.yml +9 -0
- package/package/targets/workspace/skills/buildr/git-ops/SKILL.md +73 -0
- package/package/targets/workspace/skills/buildr/openspec-contract-guard/SKILL.md +67 -0
- package/package/targets/workspace/skills/buildr/task-finish/SKILL.md +104 -0
- package/package/targets/workspace/skills/buildr/task-triage/SKILL.md +103 -0
- package/package/targets/workspace/skills/buildr/task-worktree/SKILL.md +104 -0
- package/package/targets/workspace/skills/manifest.yml +38 -0
- package/package/targets/workspace/skills/openspec/openspec-apply-change/SKILL.md +159 -0
- package/package/targets/workspace/skills/openspec/openspec-archive-change/SKILL.md +117 -0
- package/package/targets/workspace/skills/openspec/openspec-explore/SKILL.md +287 -0
- package/package/targets/workspace/skills/openspec/openspec-propose/SKILL.md +111 -0
- package/package/targets/workspace/skills/openspec/openspec-sync-specs/SKILL.md +147 -0
- package/package.json +67 -0
- package/tools/buildr +11 -0
- package/tools/cli/application/cli-update.mjs +219 -0
- package/tools/cli/application/compose-runtime.mjs +38 -0
- package/tools/cli/application/doctor/runtime-diagnostics.mjs +226 -0
- package/tools/cli/application/doctor/scope-diagnostics.mjs +440 -0
- package/tools/cli/application/doctor/service-diagnostics.mjs +184 -0
- package/tools/cli/application/doctor.mjs +201 -0
- package/tools/cli/application/package-maintenance/builtin-receipts.mjs +73 -0
- package/tools/cli/application/package-maintenance/output.mjs +148 -0
- package/tools/cli/application/package-maintenance/smoke-checks.mjs +469 -0
- package/tools/cli/application/package-maintenance/static-validation.mjs +678 -0
- package/tools/cli/application/package-maintenance.mjs +538 -0
- package/tools/cli/application/runtime.mjs +118 -0
- package/tools/cli/application/workspace-operations.mjs +310 -0
- package/tools/cli/bootstrap.mjs +6 -0
- package/tools/cli/command/help.mjs +292 -0
- package/tools/cli/command/registry.mjs +97 -0
- package/tools/cli/domains/commands.mjs +661 -0
- package/tools/cli/domains/components.mjs +922 -0
- package/tools/cli/domains/openspec.mjs +603 -0
- package/tools/cli/domains/package-assets.mjs +652 -0
- package/tools/cli/domains/rules.mjs +380 -0
- package/tools/cli/domains/runtime.mjs +94 -0
- package/tools/cli/domains/skills.mjs +647 -0
- package/tools/cli/domains/workspace.mjs +571 -0
- package/tools/cli/shared/infrastructure.mjs +411 -0
- package/tools/cli/shared/json-contracts.mjs +22 -0
- package/tools/cli/shared/platform.mjs +52 -0
- package/tools/runtime/adapter-contract.mjs +330 -0
- package/tools/runtime/check-claude-code.mjs +26 -0
- package/tools/runtime/check-codex.mjs +26 -0
- package/tools/runtime/projection.mjs +163 -0
- package/tools/runtime/render-claude-code-rules.mjs +459 -0
- package/tools/runtime/render-claude-code.mjs +59 -0
- package/tools/runtime/skills/arguments.mjs +51 -0
- package/tools/runtime/skills/contributions.mjs +171 -0
- package/tools/runtime/skills/manifests.mjs +167 -0
- package/tools/runtime/skills/primitives.mjs +86 -0
- package/tools/runtime/skills/render-plan.mjs +244 -0
- package/tools/runtime/skills/sources.mjs +276 -0
- package/tools/shared/fetch-remote-text.mjs +78 -0
- package/tools/verification/integrity/managed-data-integrity.mjs +107 -0
- package/tools/verification/integrity/managed-mutations.mjs +72 -0
- package/tools/verification/openspec/contract.mjs +212 -0
- package/tools/verification/runtime/adapter-contract.mjs +102 -0
- package/tools/verification/runtime/adapter-parity.mjs +99 -0
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import YAML from 'yaml';
|
|
5
|
+
import { parseBooleanScalar } from './manifests.mjs';
|
|
6
|
+
import { ensureFile, normalizeRelativePath, unquoteYamlScalar } from './primitives.mjs';
|
|
7
|
+
|
|
8
|
+
function parseInstalledComponentsManifest(manifestPath) {
|
|
9
|
+
if (!fs.existsSync(manifestPath)) return [];
|
|
10
|
+
const entries = [];
|
|
11
|
+
let inComponents = false;
|
|
12
|
+
let current = null;
|
|
13
|
+
for (const rawLine of fs.readFileSync(manifestPath, 'utf8').split(/\r?\n/)) {
|
|
14
|
+
const line = rawLine.trimEnd();
|
|
15
|
+
const trimmed = line.trim();
|
|
16
|
+
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
17
|
+
if (trimmed === 'components:') {
|
|
18
|
+
inComponents = true;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
if (!inComponents || trimmed === 'components: []') continue;
|
|
22
|
+
const idMatch = trimmed.match(/^-\s+id:\s*(.+)$/);
|
|
23
|
+
if (idMatch) {
|
|
24
|
+
if (current) entries.push(current);
|
|
25
|
+
current = { id: unquoteYamlScalar(idMatch[1]) };
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
const fieldMatch = trimmed.match(/^(path|enabled|state):\s*(.+)$/);
|
|
29
|
+
if (fieldMatch && current) {
|
|
30
|
+
current[fieldMatch[1]] = fieldMatch[1] === 'enabled'
|
|
31
|
+
? parseBooleanScalar(unquoteYamlScalar(fieldMatch[2]))
|
|
32
|
+
: unquoteYamlScalar(fieldMatch[2]);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (current) entries.push(current);
|
|
36
|
+
return entries.filter((entry) => entry.enabled !== false && entry.state !== 'uninstalled');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function parseComponentContributions(definitionPath) {
|
|
40
|
+
const definition = YAML.parse(fs.readFileSync(definitionPath, 'utf8')) || {};
|
|
41
|
+
const allowedTop = new Set(['schemaVersion', 'id', 'kind', 'version', 'source', 'upstream', 'members', 'contributions', 'integrity']);
|
|
42
|
+
for (const key of Object.keys(definition)) {
|
|
43
|
+
if (!allowedTop.has(key)) throw new Error(`Component field is not supported and cannot extend runtime adapters: ${key}`);
|
|
44
|
+
}
|
|
45
|
+
definition.members ||= {};
|
|
46
|
+
definition.contributions ||= {};
|
|
47
|
+
for (const key of Object.keys(definition.members)) {
|
|
48
|
+
if (!['rules', 'skills', 'commandCollections', 'skillContributions'].includes(key)) throw new Error(`Component member type is not supported and cannot extend runtime adapters: ${key}`);
|
|
49
|
+
}
|
|
50
|
+
for (const key of Object.keys(definition.contributions)) {
|
|
51
|
+
if (key !== 'skillFragments') throw new Error(`Component contribution type is not supported and cannot extend runtime adapters: ${key}`);
|
|
52
|
+
}
|
|
53
|
+
for (const key of ['rules', 'skills', 'commandCollections', 'skillContributions']) {
|
|
54
|
+
definition.members[key] ||= [];
|
|
55
|
+
if (!Array.isArray(definition.members[key]) || !definition.members[key].every((item) => typeof item === 'string')) throw new Error(`Component members.${key} must be an array of strings.`);
|
|
56
|
+
}
|
|
57
|
+
definition.contributions.skillFragments ||= [];
|
|
58
|
+
if (!Array.isArray(definition.contributions.skillFragments) || !definition.contributions.skillFragments.every((item) => typeof item === 'string')) throw new Error('Component contributions.skillFragments must be an array of strings.');
|
|
59
|
+
if (!Array.isArray(definition.integrity) || !definition.integrity.every((item) => typeof item === 'string')) throw new Error('Component integrity must be an array of strings.');
|
|
60
|
+
return definition;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function parseSkillContributionDeclaration(value) {
|
|
64
|
+
const slotMatch = String(value).match(/^([A-Za-z0-9._-]+)#([a-z][a-z0-9-]*)=(.+)$/);
|
|
65
|
+
if (slotMatch) return { skillId: slotMatch[1], placement: 'slot', slot: slotMatch[2], fragment: normalizeRelativePath(slotMatch[3]).split(path.sep).join('/') };
|
|
66
|
+
const boundaryMatch = String(value).match(/^([A-Za-z0-9._-]+)@(prepend|append)=(.+)$/);
|
|
67
|
+
if (boundaryMatch) return { skillId: boundaryMatch[1], placement: boundaryMatch[2], slot: null, fragment: normalizeRelativePath(boundaryMatch[3]).split(path.sep).join('/') };
|
|
68
|
+
throw new Error(`Invalid Skill contribution declaration: ${value}. Expected <skill-id>#<slot>=<fragment-path> or <skill-id>@<prepend|append>=<fragment-path>.`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function safeWorkspaceFile(organizationRoot, relativePath, label) {
|
|
72
|
+
if (!relativePath || relativePath === '.') throw new Error(`${label} path is empty.`);
|
|
73
|
+
const normalized = normalizeRelativePath(relativePath);
|
|
74
|
+
const absolute = path.resolve(organizationRoot, normalized);
|
|
75
|
+
const relative = path.relative(organizationRoot, absolute);
|
|
76
|
+
if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) throw new Error(`${label} must stay inside workspace: ${relativePath}`);
|
|
77
|
+
let current = organizationRoot;
|
|
78
|
+
for (const segment of relative.split(path.sep)) {
|
|
79
|
+
current = path.join(current, segment);
|
|
80
|
+
if (fs.existsSync(current) && fs.lstatSync(current).isSymbolicLink()) throw new Error(`${label} crosses a symbolic link: ${current}`);
|
|
81
|
+
}
|
|
82
|
+
ensureFile(absolute, `${label} not found: ${absolute}`);
|
|
83
|
+
return absolute;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function workspaceAssetIntegrity(organizationRoot, relativePath, label) {
|
|
87
|
+
const normalized = normalizeRelativePath(relativePath);
|
|
88
|
+
const absolute = path.resolve(organizationRoot, normalized);
|
|
89
|
+
const relative = path.relative(organizationRoot, absolute);
|
|
90
|
+
if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) throw new Error(`${label} must stay inside workspace: ${relativePath}`);
|
|
91
|
+
let current = organizationRoot;
|
|
92
|
+
for (const segment of relative.split(path.sep)) {
|
|
93
|
+
current = path.join(current, segment);
|
|
94
|
+
if (fs.existsSync(current) && fs.lstatSync(current).isSymbolicLink()) throw new Error(`${label} crosses a symbolic link: ${current}`);
|
|
95
|
+
}
|
|
96
|
+
if (!fs.existsSync(absolute)) throw new Error(`${label} not found: ${absolute}`);
|
|
97
|
+
const hash = crypto.createHash('sha256');
|
|
98
|
+
if (fs.statSync(absolute).isFile()) hash.update(fs.readFileSync(absolute));
|
|
99
|
+
else if (fs.statSync(absolute).isDirectory()) {
|
|
100
|
+
const files = [];
|
|
101
|
+
const visit = (directory) => {
|
|
102
|
+
for (const entry of fs.readdirSync(directory, { withFileTypes: true }).sort((left, right) => left.name.localeCompare(right.name))) {
|
|
103
|
+
const item = path.join(directory, entry.name);
|
|
104
|
+
if (entry.isSymbolicLink()) throw new Error(`${label} contains a symbolic link: ${item}`);
|
|
105
|
+
if (entry.isDirectory()) visit(item);
|
|
106
|
+
else if (entry.isFile()) files.push(item);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
visit(absolute);
|
|
110
|
+
for (const file of files) {
|
|
111
|
+
hash.update(path.relative(absolute, file).split(path.sep).join('/'));
|
|
112
|
+
hash.update('\0');
|
|
113
|
+
hash.update(fs.readFileSync(file));
|
|
114
|
+
hash.update('\0');
|
|
115
|
+
}
|
|
116
|
+
} else throw new Error(`${label} must be a file or directory: ${absolute}`);
|
|
117
|
+
return `sha256-${hash.digest('hex')}`;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function resolveSkillContributions(organizationRoot) {
|
|
121
|
+
const manifestPath = path.join(organizationRoot, 'components', 'manifest.yml');
|
|
122
|
+
const contributions = [];
|
|
123
|
+
const ownership = new Map();
|
|
124
|
+
for (const entry of parseInstalledComponentsManifest(manifestPath)) {
|
|
125
|
+
if (!entry.path) throw new Error(`Installed Component path is missing: ${entry.id}`);
|
|
126
|
+
const definitionPath = safeWorkspaceFile(organizationRoot, `${entry.path}/component.yml`, `Component ${entry.id} definition`);
|
|
127
|
+
const definition = parseComponentContributions(definitionPath);
|
|
128
|
+
if (definition.id !== entry.id) throw new Error(`Component id differs from registry: ${definition.id} != ${entry.id}`);
|
|
129
|
+
if (definition.schemaVersion !== 'buildr.component/v1') throw new Error(`Component schemaVersion is invalid: ${entry.id}`);
|
|
130
|
+
if (!definition.source || entry.path !== `components/${definition.source}/${definition.id}`) throw new Error(`Component path or source differs from definition: ${entry.id}`);
|
|
131
|
+
const allMembers = Object.values(definition.members).flat();
|
|
132
|
+
if (new Set(allMembers).size !== allMembers.length) throw new Error(`Component contains duplicate members: ${entry.id}`);
|
|
133
|
+
const members = new Set(definition.members.skillContributions);
|
|
134
|
+
const integrity = new Map(definition.integrity.map((item) => {
|
|
135
|
+
const index = item.lastIndexOf('=');
|
|
136
|
+
if (index === -1) throw new Error(`Component integrity entry is invalid: ${item}`);
|
|
137
|
+
return [item.slice(0, index), item.slice(index + 1)];
|
|
138
|
+
}));
|
|
139
|
+
if (integrity.size !== definition.integrity.length) throw new Error(`Component integrity contains duplicate members: ${entry.id}`);
|
|
140
|
+
for (const member of allMembers) {
|
|
141
|
+
if (ownership.has(member)) throw new Error(`Component ownership conflict: ${member} (${ownership.get(member)}, ${entry.id})`);
|
|
142
|
+
ownership.set(member, entry.id);
|
|
143
|
+
const expected = integrity.get(member);
|
|
144
|
+
if (!expected || !/^sha256-[a-f0-9]{64}$/.test(expected)) throw new Error(`Component member integrity is missing or invalid: ${member}`);
|
|
145
|
+
const actual = workspaceAssetIntegrity(organizationRoot, member, `Component ${entry.id} member`);
|
|
146
|
+
if (expected !== actual) throw new Error(`Component member integrity mismatch: ${member}`);
|
|
147
|
+
}
|
|
148
|
+
for (const member of integrity.keys()) if (!allMembers.includes(member)) throw new Error(`Component integrity references unknown member: ${member}`);
|
|
149
|
+
const seenDeclarations = new Set();
|
|
150
|
+
const seenFragments = new Set();
|
|
151
|
+
for (const [index, rawDeclaration] of definition.contributions.skillFragments.entries()) {
|
|
152
|
+
if (seenDeclarations.has(rawDeclaration)) throw new Error(`Duplicate Skill contribution declaration: ${rawDeclaration}`);
|
|
153
|
+
seenDeclarations.add(rawDeclaration);
|
|
154
|
+
const declaration = parseSkillContributionDeclaration(rawDeclaration);
|
|
155
|
+
if (!members.has(declaration.fragment)) throw new Error(`Skill contribution is not a Component member: ${declaration.fragment}`);
|
|
156
|
+
if (seenFragments.has(declaration.fragment)) throw new Error(`Skill contribution fragment is referenced more than once: ${declaration.fragment}`);
|
|
157
|
+
seenFragments.add(declaration.fragment);
|
|
158
|
+
const fragmentFile = safeWorkspaceFile(organizationRoot, declaration.fragment, `Component ${entry.id} Skill contribution`);
|
|
159
|
+
const content = fs.readFileSync(fragmentFile, 'utf8').trim();
|
|
160
|
+
if (!content) throw new Error(`Skill contribution fragment is empty: ${declaration.fragment}`);
|
|
161
|
+
contributions.push({
|
|
162
|
+
...declaration,
|
|
163
|
+
componentId: entry.id,
|
|
164
|
+
order: index,
|
|
165
|
+
content,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
for (const member of members) if (!seenFragments.has(member)) throw new Error(`Skill contribution member has no declaration: ${member}`);
|
|
169
|
+
}
|
|
170
|
+
return contributions.sort((left, right) => left.componentId.localeCompare(right.componentId) || left.order - right.order);
|
|
171
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { ensureFile, unquoteYamlScalar } from './primitives.mjs';
|
|
3
|
+
|
|
4
|
+
export function parseSkillsManifest(manifestPath) {
|
|
5
|
+
ensureFile(manifestPath, `Manifest not found: ${manifestPath}`);
|
|
6
|
+
const lines = fs.readFileSync(manifestPath, 'utf8').split(/\r?\n/);
|
|
7
|
+
const skills = [];
|
|
8
|
+
let inSkills = false;
|
|
9
|
+
let current = null;
|
|
10
|
+
let currentObject = null;
|
|
11
|
+
|
|
12
|
+
for (const rawLine of lines) {
|
|
13
|
+
const line = rawLine.trimEnd();
|
|
14
|
+
const trimmed = line.trim();
|
|
15
|
+
if (!trimmed || trimmed.startsWith('#')) {
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
if (trimmed === 'skills:') {
|
|
19
|
+
inSkills = true;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (!inSkills) {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
const idMatch = trimmed.match(/^-\s+id:\s*(.+)$/);
|
|
26
|
+
if (idMatch) {
|
|
27
|
+
if (current) {
|
|
28
|
+
skills.push(current);
|
|
29
|
+
}
|
|
30
|
+
current = { id: unquoteYamlScalar(idMatch[1]) };
|
|
31
|
+
currentObject = null;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
const objectStartMatch = trimmed.match(/^(source|resolved|install):\s*$/);
|
|
35
|
+
if (objectStartMatch && current) {
|
|
36
|
+
currentObject = objectStartMatch[1];
|
|
37
|
+
current[currentObject] = {};
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
const nestedMatch = rawLine.match(/^\s{6}([A-Za-z][A-Za-z0-9_-]*):\s*(.+)$/);
|
|
41
|
+
if (nestedMatch && current && currentObject) {
|
|
42
|
+
current[currentObject][nestedMatch[1]] = unquoteYamlScalar(nestedMatch[2]);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
currentObject = null;
|
|
46
|
+
const pathMatch = trimmed.match(/^path:\s*(.+)$/);
|
|
47
|
+
if (pathMatch && current) {
|
|
48
|
+
current.path = unquoteYamlScalar(pathMatch[1]);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const sourceMatch = trimmed.match(/^source:\s*(.+)$/);
|
|
52
|
+
if (sourceMatch && current) {
|
|
53
|
+
current.source = unquoteYamlScalar(sourceMatch[1]);
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
const resolvedMatch = trimmed.match(/^resolved:\s*(.+)$/);
|
|
57
|
+
if (resolvedMatch && current) {
|
|
58
|
+
current.resolved = unquoteYamlScalar(resolvedMatch[1]);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const installMatch = trimmed.match(/^install:\s*(.+)$/);
|
|
62
|
+
if (installMatch && current) {
|
|
63
|
+
current.install = unquoteYamlScalar(installMatch[1]);
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
const runtimePathMatch = trimmed.match(/^runtimePath:\s*(.+)$/);
|
|
67
|
+
if (runtimePathMatch && current) {
|
|
68
|
+
current.runtimePath = unquoteYamlScalar(runtimePathMatch[1]);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const enabledMatch = trimmed.match(/^enabled:\s*(.+)$/);
|
|
72
|
+
if (enabledMatch && current) {
|
|
73
|
+
current.enabled = parseBooleanScalar(unquoteYamlScalar(enabledMatch[1]));
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const requiredMatch = trimmed.match(/^required:\s*(.+)$/);
|
|
77
|
+
if (requiredMatch && current) {
|
|
78
|
+
current.required = parseBooleanScalar(unquoteYamlScalar(requiredMatch[1]));
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const stateMatch = trimmed.match(/^state:\s*(.+)$/);
|
|
82
|
+
if (stateMatch && current) {
|
|
83
|
+
current.state = unquoteYamlScalar(stateMatch[1]);
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
const runtimesMatch = trimmed.match(/^runtimes:\s*(.+)$/);
|
|
87
|
+
if (runtimesMatch && current) {
|
|
88
|
+
current.runtimes = parseInlineStringArray(unquoteYamlScalar(runtimesMatch[1]));
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (/^[A-Za-z_][A-Za-z0-9_-]*:/.test(trimmed) && !trimmed.startsWith('-')) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
throw new Error(`Unsupported manifest syntax in ${manifestPath}: ${rawLine}`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (current) {
|
|
98
|
+
skills.push(current);
|
|
99
|
+
}
|
|
100
|
+
for (const skill of skills) {
|
|
101
|
+
const hasPath = skill.path !== undefined;
|
|
102
|
+
const hasSource = skill.source !== undefined;
|
|
103
|
+
const hasResolved = skill.resolved !== undefined;
|
|
104
|
+
const hasSourceLabel = hasPath && typeof skill.source === 'string' && isSourceLabel(skill.source);
|
|
105
|
+
if (!skill.id || (hasPath && ((hasSource && !hasSourceLabel) || hasResolved)) || (!hasPath && !hasSource && !hasResolved)) {
|
|
106
|
+
throw new Error(`Skill entry must include id and path, source, or resolved in ${manifestPath}`);
|
|
107
|
+
}
|
|
108
|
+
if (hasSourceLabel) {
|
|
109
|
+
// source is an ownership label for manifest-managed local Skills.
|
|
110
|
+
} else if (hasSource && typeof skill.source !== 'string') {
|
|
111
|
+
validateSkillSourceObject(skill.source, `Skill source is invalid in ${manifestPath}`);
|
|
112
|
+
}
|
|
113
|
+
if (hasResolved) {
|
|
114
|
+
if (!isPlainObject(skill.resolved)) {
|
|
115
|
+
throw new Error(`Skill resolved must be an object in ${manifestPath}`);
|
|
116
|
+
}
|
|
117
|
+
validateResolvedObject(skill.resolved, manifestPath);
|
|
118
|
+
}
|
|
119
|
+
if (skill.install !== undefined) {
|
|
120
|
+
if (!isPlainObject(skill.install)) {
|
|
121
|
+
throw new Error(`Skill install must be an object in ${manifestPath}`);
|
|
122
|
+
}
|
|
123
|
+
if (skill.install.mode !== undefined && !['agent', 'buildr'].includes(skill.install.mode)) {
|
|
124
|
+
throw new Error(`Skill install.mode must be agent or buildr in ${manifestPath}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return skills;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function parseBooleanScalar(value) {
|
|
132
|
+
if (value === 'true') return true;
|
|
133
|
+
if (value === 'false') return false;
|
|
134
|
+
throw new Error(`Expected boolean scalar, got: ${value}`);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function parseInlineStringArray(value) {
|
|
138
|
+
try {
|
|
139
|
+
const parsed = JSON.parse(value);
|
|
140
|
+
if (Array.isArray(parsed) && parsed.every((item) => typeof item === 'string')) return parsed;
|
|
141
|
+
} catch {
|
|
142
|
+
// Fall through.
|
|
143
|
+
}
|
|
144
|
+
throw new Error(`Expected JSON string array, got: ${value}`);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
export function isSourceLabel(value) {
|
|
149
|
+
return ['buildr', 'openspec', 'workspace', 'project', 'service'].includes(value);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function isPlainObject(value) {
|
|
153
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function validateSkillSourceObject(source, message) {
|
|
157
|
+
if (!source.kind || typeof source.kind !== 'string' || !source.url || typeof source.url !== 'string') {
|
|
158
|
+
throw new Error(message);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function validateResolvedObject(resolved, manifestPath) {
|
|
163
|
+
validateSkillSourceObject(resolved, `Skill resolved must include kind and url in ${manifestPath}`);
|
|
164
|
+
if (resolved.kind !== 'skill-url') {
|
|
165
|
+
throw new Error(`Unsupported resolved Skill kind in ${manifestPath}: ${resolved.kind}`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
|
|
5
|
+
export const MANAGED_PREFIX = '<!-- Generated by Buildr.';
|
|
6
|
+
export const FRONTMATTER_BOUNDARY = '---';
|
|
7
|
+
export const SKILL_CONTRIBUTION_MARKER = /^<!-- buildr:skill-contributions ([a-z][a-z0-9-]*) -->$/gm;
|
|
8
|
+
|
|
9
|
+
export function normalizeRelativePath(input) {
|
|
10
|
+
const normalized = path.normalize(input).replace(/^\.\//, '');
|
|
11
|
+
if (path.isAbsolute(normalized) || normalized === '..' || normalized.startsWith(`..${path.sep}`)) {
|
|
12
|
+
throw new Error(`Path must stay inside repository: ${input}`);
|
|
13
|
+
}
|
|
14
|
+
return normalized;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
export function productRoot() {
|
|
19
|
+
return path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../..');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function packageRoot() {
|
|
23
|
+
return path.join(productRoot(), 'package');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function resolveSkillScope(repoRoot, scopeInput) {
|
|
27
|
+
const scope = normalizeRelativePath(scopeInput);
|
|
28
|
+
const parts = scope === '.' ? ['.'] : scope.split(path.sep);
|
|
29
|
+
let organizationRoot;
|
|
30
|
+
let projectRoot = null;
|
|
31
|
+
if (parts[0] === '.') {
|
|
32
|
+
organizationRoot = repoRoot;
|
|
33
|
+
} else if (parts[0] === 'projects') {
|
|
34
|
+
if (parts.length !== 2) throw new Error(`Project scope must be projects/<project>: ${scopeInput}`);
|
|
35
|
+
organizationRoot = repoRoot;
|
|
36
|
+
projectRoot = path.join(repoRoot, 'projects', parts[1]);
|
|
37
|
+
} else {
|
|
38
|
+
throw new Error(`Unsupported scope. Use . or projects/<project>: ${scopeInput}`);
|
|
39
|
+
}
|
|
40
|
+
ensureDirectory(organizationRoot, `Organization scope does not exist: ${path.relative(repoRoot, organizationRoot)}`);
|
|
41
|
+
if (projectRoot) {
|
|
42
|
+
ensureDirectory(projectRoot, `Project scope does not exist: ${path.relative(repoRoot, projectRoot)}`);
|
|
43
|
+
}
|
|
44
|
+
return { scope, organizationRoot, projectRoot };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function ensureDirectory(dir, message) {
|
|
48
|
+
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) {
|
|
49
|
+
throw new Error(message);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function ensureFile(file, message) {
|
|
54
|
+
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) {
|
|
55
|
+
throw new Error(message);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
export function unquoteYamlScalar(value) {
|
|
61
|
+
const trimmed = value.trim();
|
|
62
|
+
if ((trimmed.startsWith('"') && trimmed.endsWith('"')) || (trimmed.startsWith("'") && trimmed.endsWith("'"))) {
|
|
63
|
+
return trimmed.slice(1, -1);
|
|
64
|
+
}
|
|
65
|
+
return trimmed;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function parseSkillFrontmatterName(skillFile) {
|
|
69
|
+
return parseSkillFrontmatterNameFromContent(fs.readFileSync(skillFile, 'utf8'), skillFile);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function parseSkillFrontmatterNameFromContent(content, label) {
|
|
73
|
+
const lines = content.split(/\r?\n/);
|
|
74
|
+
if (lines[0] !== FRONTMATTER_BOUNDARY) {
|
|
75
|
+
throw new Error(`SKILL.md must start with YAML frontmatter: ${label}`);
|
|
76
|
+
}
|
|
77
|
+
const endIndex = lines.findIndex((line, index) => index > 0 && line === FRONTMATTER_BOUNDARY);
|
|
78
|
+
if (endIndex === -1) {
|
|
79
|
+
throw new Error(`SKILL.md frontmatter is not closed: ${label}`);
|
|
80
|
+
}
|
|
81
|
+
for (const rawLine of lines.slice(1, endIndex)) {
|
|
82
|
+
const match = rawLine.trim().match(/^name:\s*(.+)$/);
|
|
83
|
+
if (match) return unquoteYamlScalar(match[1]);
|
|
84
|
+
}
|
|
85
|
+
throw new Error(`SKILL.md frontmatter must declare name: ${label}`);
|
|
86
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { assertRuntimeTargetPath, getRuntimeAdapter } from '../adapter-contract.mjs';
|
|
5
|
+
import { FRONTMATTER_BOUNDARY, MANAGED_PREFIX, SKILL_CONTRIBUTION_MARKER, resolveSkillScope } from './primitives.mjs';
|
|
6
|
+
import { resolvePackageAgentSkill, resolveSkills } from './sources.mjs';
|
|
7
|
+
|
|
8
|
+
export function hasManagedSkillMarker(content) {
|
|
9
|
+
const lines = content.split(/\r?\n/);
|
|
10
|
+
if (lines[0]?.startsWith(MANAGED_PREFIX)) {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
if (lines[0] !== FRONTMATTER_BOUNDARY) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const endIndex = lines.findIndex((line, index) => index > 0 && line === FRONTMATTER_BOUNDARY);
|
|
18
|
+
return endIndex !== -1 && lines[endIndex + 1]?.startsWith(MANAGED_PREFIX);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function addManagedMarker(source, marker) {
|
|
22
|
+
const lines = source.split(/\r?\n/);
|
|
23
|
+
if (lines[0] !== FRONTMATTER_BOUNDARY) {
|
|
24
|
+
return `${marker}\n${source}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const endIndex = lines.findIndex((line, index) => index > 0 && line === FRONTMATTER_BOUNDARY);
|
|
28
|
+
if (endIndex === -1) {
|
|
29
|
+
return `${marker}\n${source}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
lines.splice(endIndex + 1, 0, marker);
|
|
33
|
+
return lines.join('\n');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function contributionBlock(contribution) {
|
|
37
|
+
const identity = contribution.placement === 'slot' ? contribution.slot : contribution.placement;
|
|
38
|
+
return [
|
|
39
|
+
`<!-- buildr:contribution ${contribution.componentId}#${identity} -->`,
|
|
40
|
+
contribution.content,
|
|
41
|
+
'<!-- /buildr:contribution -->',
|
|
42
|
+
].join('\n');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function prependAfterFrontmatter(source, block) {
|
|
46
|
+
const lines = source.split(/\r?\n/);
|
|
47
|
+
if (lines[0] !== FRONTMATTER_BOUNDARY) return `${block}\n\n${source}`;
|
|
48
|
+
const endIndex = lines.findIndex((line, index) => index > 0 && line === FRONTMATTER_BOUNDARY);
|
|
49
|
+
if (endIndex === -1) throw new Error('Boundary Skill contribution requires valid closed frontmatter.');
|
|
50
|
+
lines.splice(endIndex + 1, 0, '', block);
|
|
51
|
+
return lines.join('\n');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function buildSkillTarget(targetRoot, skill) {
|
|
55
|
+
const runtimePath = skill.runtimePath ?? skill.id;
|
|
56
|
+
return path.join(targetRoot, '.claude', 'skills', ...runtimePath.split('/'), 'SKILL.md');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function buildRuntimeSkillTarget(targetRoot, skill, runtime) {
|
|
60
|
+
const runtimePath = skill.runtimePath ?? skill.id;
|
|
61
|
+
const root = getRuntimeAdapter(runtime).skillsRoot;
|
|
62
|
+
return path.join(targetRoot, root, 'skills', ...runtimePath.split('/'), 'SKILL.md');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function sourceHash(content) {
|
|
66
|
+
return crypto.createHash('sha256').update(content).digest('hex');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function buildAdapterRuntimeContext(skill) {
|
|
70
|
+
if (skill.origin !== 'product' || skill.id !== 'buildr') {
|
|
71
|
+
return '';
|
|
72
|
+
}
|
|
73
|
+
const adapter = getRuntimeAdapter(skill.runtime);
|
|
74
|
+
const commands = adapter.recommendedCommands;
|
|
75
|
+
const ruleGuidance = adapter.renderCapabilities['rules-entry'].mode === 'native'
|
|
76
|
+
? `${adapter.displayName} 原生读取 scope \`AGENTS.md\``
|
|
77
|
+
: `${adapter.displayName} 使用 ${adapter.renderCapabilities['rules-entry'].projection.targetPattern} Rule bridge`;
|
|
78
|
+
const maintenanceCommands = adapter.renderCapabilities['rules-entry'].writesFiles
|
|
79
|
+
? [commands.installProductSkill, commands.runtimeCheckScope.replace('<workspace-relative-path>', '<scope>'), commands.renderRulesScope, commands.renderSkillsScope, commands.syncWorkspaceEntry]
|
|
80
|
+
: [commands.syncWorkspaceEntry];
|
|
81
|
+
return [
|
|
82
|
+
'## 当前 Agent Adapter',
|
|
83
|
+
'',
|
|
84
|
+
`当前安装 adapter:\`${adapter.id}\`。`,
|
|
85
|
+
'',
|
|
86
|
+
'默认先确认支持矩阵,再用带当前 adapter 的 doctor 判断 workspace 和 runtime 状态:',
|
|
87
|
+
'',
|
|
88
|
+
'```bash',
|
|
89
|
+
'buildr runtime list --json',
|
|
90
|
+
commands.doctor,
|
|
91
|
+
'```',
|
|
92
|
+
'',
|
|
93
|
+
adapter.renderCapabilities['rules-entry'].writesFiles
|
|
94
|
+
? 'doctor 指向具体 runtime 问题后,再使用专项维护命令:'
|
|
95
|
+
: '用户要求同步 workspace 时运行;用户要求更新 Buildr 时先运行 buildr update,再使用新入口运行:',
|
|
96
|
+
'',
|
|
97
|
+
'```bash',
|
|
98
|
+
...maintenanceCommands,
|
|
99
|
+
'```',
|
|
100
|
+
'',
|
|
101
|
+
`当前 adapter 的 runtime 入口:${ruleGuidance};Skills 渲染到当前项目根目录 \`${adapter.skillsRoot}/skills/\`。如果当前 Agent 不是该 adapter,停止当前 Buildr 操作;请联系 Buildr 作者反馈该 Agent。`,
|
|
102
|
+
'',
|
|
103
|
+
].join('\n');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function buildSkillContent(repoRoot, skill) {
|
|
107
|
+
const rawSource = skill.sourceContent ?? fs.readFileSync(skill.sourceFile, 'utf8');
|
|
108
|
+
const contributionsBySlot = new Map();
|
|
109
|
+
const prepended = [];
|
|
110
|
+
const appended = [];
|
|
111
|
+
for (const contribution of skill.skillContributions || []) {
|
|
112
|
+
if (contribution.placement === 'prepend') {
|
|
113
|
+
prepended.push(contribution);
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (contribution.placement === 'append') {
|
|
117
|
+
appended.push(contribution);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (!contributionsBySlot.has(contribution.slot)) contributionsBySlot.set(contribution.slot, []);
|
|
121
|
+
contributionsBySlot.get(contribution.slot).push(contribution);
|
|
122
|
+
}
|
|
123
|
+
let source = rawSource.replace(SKILL_CONTRIBUTION_MARKER, (_marker, slot) => {
|
|
124
|
+
const contributions = contributionsBySlot.get(slot) || [];
|
|
125
|
+
return contributions.map(contributionBlock).join('\n\n');
|
|
126
|
+
});
|
|
127
|
+
if (prepended.length) source = prependAfterFrontmatter(source, prepended.map(contributionBlock).join('\n\n'));
|
|
128
|
+
if (appended.length) source = `${source.trimEnd()}\n\n${appended.map(contributionBlock).join('\n\n')}\n`;
|
|
129
|
+
const runtimeContext = buildAdapterRuntimeContext(skill);
|
|
130
|
+
const content = runtimeContext ? `${source.trimEnd()}\n\n${runtimeContext}` : source;
|
|
131
|
+
const marker = `<!-- Generated by Buildr. Hash: ${sourceHash(content)}. Do not edit. -->`;
|
|
132
|
+
return addManagedMarker(content, marker);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function buildAgentInstallPlanTarget(targetRoot, skill) {
|
|
136
|
+
return path.join(targetRoot, '.claude', 'buildr', 'skill-install-plans', `${skill.id}.md`);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function formatSkillSourceDetails(skill) {
|
|
140
|
+
const lines = [];
|
|
141
|
+
if (skill.source) {
|
|
142
|
+
lines.push('- source:');
|
|
143
|
+
lines.push(` - kind: ${skill.source.kind}`);
|
|
144
|
+
lines.push(` - url: ${skill.source.url}`);
|
|
145
|
+
}
|
|
146
|
+
if (skill.resolved) {
|
|
147
|
+
lines.push('- resolved:');
|
|
148
|
+
lines.push(` - kind: ${skill.resolved.kind}`);
|
|
149
|
+
lines.push(` - url: ${skill.resolved.url}`);
|
|
150
|
+
if (skill.resolved.version) lines.push(` - version: ${skill.resolved.version}`);
|
|
151
|
+
if (skill.resolved.integrity) lines.push(` - integrity: ${skill.resolved.integrity}`);
|
|
152
|
+
}
|
|
153
|
+
return lines.length > 0 ? lines.join('\n') : '- no source or resolved information recorded';
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function buildAgentInstallPlanContent(skill) {
|
|
157
|
+
return [
|
|
158
|
+
`# Buildr Skill Install Plan: ${skill.id}`,
|
|
159
|
+
'',
|
|
160
|
+
'<!-- Generated by Buildr. Agent action required. Do not treat this file as an installed Skill. -->',
|
|
161
|
+
'',
|
|
162
|
+
'This Skill is declared in Buildr but requires Agent installation or resolution.',
|
|
163
|
+
'',
|
|
164
|
+
'## Manifest Information',
|
|
165
|
+
'',
|
|
166
|
+
formatSkillSourceDetails(skill),
|
|
167
|
+
'',
|
|
168
|
+
'## Required Agent Action',
|
|
169
|
+
'',
|
|
170
|
+
'1. Read the source/resolved information above.',
|
|
171
|
+
'2. Resolve a precise install source when possible.',
|
|
172
|
+
'3. Update Buildr manifest with `buildr skills add <id> --resolved-source <url> --replace` when a precise source is found.',
|
|
173
|
+
'4. Install the Skill into the current Agent runtime only after the source is understood.',
|
|
174
|
+
'',
|
|
175
|
+
].join('\n');
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function resolveRenderSkills(repoRoot, scope, runtime) {
|
|
179
|
+
const { organizationRoot, projectRoot } = resolveSkillScope(repoRoot, scope);
|
|
180
|
+
if (projectRoot) return resolveSkills(organizationRoot, projectRoot, { runtime });
|
|
181
|
+
const groups = [{ scope: '.', skills: resolveSkills(organizationRoot, null, { runtime }) }];
|
|
182
|
+
const projectsRoot = path.join(organizationRoot, 'projects');
|
|
183
|
+
if (fs.existsSync(projectsRoot)) {
|
|
184
|
+
for (const name of fs.readdirSync(projectsRoot).sort()) {
|
|
185
|
+
const root = path.join(projectsRoot, name);
|
|
186
|
+
if (!fs.lstatSync(root).isDirectory() || !fs.existsSync(path.join(root, 'skills', 'manifest.yml'))) continue;
|
|
187
|
+
groups.push({
|
|
188
|
+
scope: `projects/${name}`,
|
|
189
|
+
skills: resolveSkills(organizationRoot, root, { runtime, includeWorkspace: false }),
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return groups.flatMap((group) => group.skills.map((skill) => ({ ...skill, declaredScope: group.scope })));
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function buildSkillRenderPlan(repoRoot, targetRoot, skills, runtime, options = {}) {
|
|
197
|
+
const adapter = getRuntimeAdapter(runtime);
|
|
198
|
+
const byTarget = new Map();
|
|
199
|
+
const conflicts = [];
|
|
200
|
+
for (const skill of skills) {
|
|
201
|
+
const installPlan = skill.installMode === 'agent';
|
|
202
|
+
const targetFile = installPlan
|
|
203
|
+
? path.join(targetRoot, adapter.skillsRoot, 'buildr', 'skill-install-plans', `${skill.id}.md`)
|
|
204
|
+
: buildRuntimeSkillTarget(targetRoot, skill, runtime);
|
|
205
|
+
const content = installPlan ? buildAgentInstallPlanContent(skill) : buildSkillContent(repoRoot, skill);
|
|
206
|
+
const sourceContent = installPlan ? null : skill.sourceContent ?? fs.readFileSync(skill.sourceFile, 'utf8');
|
|
207
|
+
const item = { targetFile, content, sourceContent, source: `${skill.declaredScope || skill.origin}:${skill.id}` };
|
|
208
|
+
const existing = byTarget.get(targetFile);
|
|
209
|
+
if (existing && existing.content !== content) {
|
|
210
|
+
conflicts.push(`${targetFile}: ${existing.source} 与 ${item.source} 内容不同`);
|
|
211
|
+
} else if (!existing) {
|
|
212
|
+
byTarget.set(targetFile, item);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
for (const item of byTarget.values()) {
|
|
216
|
+
if (!fs.existsSync(item.targetFile)) continue;
|
|
217
|
+
const current = fs.readFileSync(item.targetFile, 'utf8');
|
|
218
|
+
const managed = hasManagedSkillMarker(current) || current.includes('<!-- Generated by Buildr. Agent action required.');
|
|
219
|
+
if (!managed && current !== item.sourceContent && current !== item.content) {
|
|
220
|
+
conflicts.push(`Refusing to overwrite non-Buildr-managed file: ${item.targetFile}`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (Array.isArray(options.conflicts)) options.conflicts.push(...conflicts);
|
|
224
|
+
if (conflicts.length && options.deferConflicts !== true) throw new Error(`运行时写入冲突:\n- ${conflicts.sort().join('\n- ')}`);
|
|
225
|
+
return [...byTarget.values()].sort((left, right) => left.targetFile.localeCompare(right.targetFile));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export function applySkillRenderPlan(plan, targetRoot) {
|
|
229
|
+
for (const item of plan) assertRuntimeTargetPath(targetRoot, item.targetFile, 'Runtime Skill target');
|
|
230
|
+
const files = [];
|
|
231
|
+
for (const item of plan) {
|
|
232
|
+
if (!fs.existsSync(item.targetFile) || fs.readFileSync(item.targetFile, 'utf8') !== item.content) {
|
|
233
|
+
fs.mkdirSync(path.dirname(item.targetFile), { recursive: true });
|
|
234
|
+
fs.writeFileSync(item.targetFile, item.content, 'utf8');
|
|
235
|
+
}
|
|
236
|
+
files.push(item.targetFile);
|
|
237
|
+
}
|
|
238
|
+
return files;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function renderSkill(repoRoot, targetRoot, skill, runtime = 'claude-code') {
|
|
242
|
+
return applySkillRenderPlan(buildSkillRenderPlan(repoRoot, targetRoot, [skill], runtime), targetRoot)[0]
|
|
243
|
+
?? (skill.installMode === 'agent' ? buildAgentInstallPlanTarget(targetRoot, skill) : buildRuntimeSkillTarget(targetRoot, skill, runtime));
|
|
244
|
+
}
|