@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,330 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
export const REQUIRED_RENDER_CAPABILITIES = Object.freeze([
|
|
5
|
+
'rules-entry',
|
|
6
|
+
'product-buildr-skill',
|
|
7
|
+
'workspace-project-skills',
|
|
8
|
+
'skill-install-plans',
|
|
9
|
+
'runtime-check',
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
export const UNSUPPORTED_AGENT_GUIDANCE = Object.freeze({
|
|
13
|
+
message: 'Buildr 暂不支持当前 Agent runtime 的自动渲染。',
|
|
14
|
+
nextStep: '请联系 Buildr 作者反馈该 Agent。',
|
|
15
|
+
mustNotUseFallbackAdapter: true,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const AGENT_ID_PATTERN = /^[A-Za-z0-9._-]+$/;
|
|
19
|
+
|
|
20
|
+
function freeze(value) {
|
|
21
|
+
if (!value || typeof value !== 'object' || Object.isFrozen(value)) return value;
|
|
22
|
+
for (const child of Object.values(value)) freeze(child);
|
|
23
|
+
return Object.freeze(value);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function planFromProjection(context, projection) {
|
|
27
|
+
const rules = projection === 'native'
|
|
28
|
+
? {
|
|
29
|
+
writes: [],
|
|
30
|
+
removals: [],
|
|
31
|
+
nativeAssets: context.rules.nativeAssets,
|
|
32
|
+
actions: [],
|
|
33
|
+
}
|
|
34
|
+
: context.rules;
|
|
35
|
+
return createRuntimePlan({
|
|
36
|
+
adapterId: context.adapterId,
|
|
37
|
+
targetRoot: context.targetRoot,
|
|
38
|
+
scope: context.scope,
|
|
39
|
+
writes: [...rules.writes, ...context.skills.writes],
|
|
40
|
+
nativeAssets: rules.nativeAssets,
|
|
41
|
+
removals: [...rules.removals, ...context.skills.removals],
|
|
42
|
+
capabilityEvidence: REQUIRED_RENDER_CAPABILITIES.map((capability) => ({
|
|
43
|
+
capability,
|
|
44
|
+
supported: true,
|
|
45
|
+
adapterId: context.adapterId,
|
|
46
|
+
})),
|
|
47
|
+
findings: [...context.findings],
|
|
48
|
+
repairs: [...context.repairs],
|
|
49
|
+
warnings: [...context.warnings],
|
|
50
|
+
ruleActions: rules.actions,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const DESCRIPTORS = [
|
|
55
|
+
{
|
|
56
|
+
id: 'claude-code',
|
|
57
|
+
displayName: 'Claude Code',
|
|
58
|
+
projection: 'reference-bridge',
|
|
59
|
+
skillsRoot: '.claude',
|
|
60
|
+
implementation: { skills: 'claude-code', rules: 'reference-bridge', checker: 'claude-code' },
|
|
61
|
+
runtimeTargets: ['CLAUDE.md', '.claude/skills/', '.claude/buildr/skill-install-plans/'],
|
|
62
|
+
renderCapabilities: {
|
|
63
|
+
'rules-entry': { supported: true, mode: 'rendered', scopeSyntax: 'workspace-relative-path', sourceDiscovery: { pattern: '**/AGENTS.md', mode: 'recursive-scope', includesAncestors: true }, projection: { mode: 'rendered', targetPattern: '<source-dir>/CLAUDE.md' }, writesFiles: true, targets: ['<source-dir>/CLAUDE.md'] },
|
|
64
|
+
'product-buildr-skill': { supported: true, mode: 'install', writesFiles: true, targets: ['.claude/skills/buildr/SKILL.md'] },
|
|
65
|
+
'workspace-project-skills': { supported: true, mode: 'rendered', writesFiles: true, targets: ['.claude/skills/<skill>/SKILL.md'] },
|
|
66
|
+
'skill-install-plans': { supported: true, mode: 'plan', writesFiles: true, targets: ['.claude/buildr/skill-install-plans/<skill>.md'] },
|
|
67
|
+
'runtime-check': { supported: true, mode: 'diagnostic', writesFiles: false },
|
|
68
|
+
},
|
|
69
|
+
recommendedCommands: {
|
|
70
|
+
doctor: 'buildr doctor --agent claude-code --target <dir> --json',
|
|
71
|
+
syncWorkspaceEntry: 'buildr sync claude-code --target <dir>',
|
|
72
|
+
renderScope: 'buildr render claude-code --scope <workspace-relative-path> --target <dir>',
|
|
73
|
+
renderSkillsScope: 'buildr skills render claude-code --scope <scope> --target <dir>',
|
|
74
|
+
renderRulesScope: 'buildr rules render claude-code --scope <scope> --target <dir>',
|
|
75
|
+
runtimeCheckScope: 'buildr runtime check claude-code --scope <workspace-relative-path> --target <dir>',
|
|
76
|
+
installProductSkill: 'buildr skill install claude-code --target <dir>',
|
|
77
|
+
},
|
|
78
|
+
planRuntime(context) { return planFromProjection(context, 'reference-bridge'); },
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
id: 'codex',
|
|
82
|
+
displayName: 'Codex',
|
|
83
|
+
projection: 'native-agents',
|
|
84
|
+
skillsRoot: '.agents',
|
|
85
|
+
implementation: { skills: 'codex', rules: 'native-agents', checker: 'codex' },
|
|
86
|
+
runtimeTargets: ['AGENTS.md', '.agents/skills/', '.agents/buildr/skill-install-plans/'],
|
|
87
|
+
renderCapabilities: {
|
|
88
|
+
'rules-entry': { supported: true, mode: 'native', scopeSyntax: 'workspace-relative-path', sourceDiscovery: { pattern: '**/AGENTS.md', mode: 'recursive-scope', includesAncestors: true }, projection: { mode: 'native' }, writesFiles: false, sourceAssets: ['**/AGENTS.md'] },
|
|
89
|
+
'product-buildr-skill': { supported: true, mode: 'install', writesFiles: true, targets: ['.agents/skills/buildr/SKILL.md'] },
|
|
90
|
+
'workspace-project-skills': { supported: true, mode: 'rendered', writesFiles: true, targets: ['.agents/skills/<skill>/SKILL.md'] },
|
|
91
|
+
'skill-install-plans': { supported: true, mode: 'plan', writesFiles: true, targets: ['.agents/buildr/skill-install-plans/<skill>.md'] },
|
|
92
|
+
'runtime-check': { supported: true, mode: 'diagnostic', writesFiles: false },
|
|
93
|
+
},
|
|
94
|
+
recommendedCommands: {
|
|
95
|
+
doctor: 'buildr doctor --agent codex --target <dir> --json',
|
|
96
|
+
syncWorkspaceEntry: 'buildr sync codex --target <dir>',
|
|
97
|
+
renderScope: 'buildr render codex --scope <workspace-relative-path> --target <dir>',
|
|
98
|
+
renderSkillsScope: 'buildr skills render codex --scope <scope> --target <dir>',
|
|
99
|
+
runtimeCheckScope: 'buildr runtime check codex --scope <workspace-relative-path> --target <dir>',
|
|
100
|
+
installProductSkill: 'buildr skill install codex --target <dir>',
|
|
101
|
+
},
|
|
102
|
+
planRuntime(context) { return planFromProjection(context, 'native'); },
|
|
103
|
+
},
|
|
104
|
+
];
|
|
105
|
+
|
|
106
|
+
export function validateAdapterDescriptor(descriptor) {
|
|
107
|
+
const errors = [];
|
|
108
|
+
if (!descriptor || typeof descriptor !== 'object') return ['adapter descriptor must be an object'];
|
|
109
|
+
if (!AGENT_ID_PATTERN.test(descriptor.id || '')) errors.push(`adapter id is invalid: ${descriptor.id || '<missing>'}`);
|
|
110
|
+
if (!descriptor.displayName) errors.push(`adapter ${descriptor.id || '<missing>'} displayName is required`);
|
|
111
|
+
if (!Array.isArray(descriptor.runtimeTargets) || descriptor.runtimeTargets.length === 0) errors.push(`adapter ${descriptor.id} runtimeTargets are required`);
|
|
112
|
+
if (new Set(descriptor.runtimeTargets || []).size !== (descriptor.runtimeTargets || []).length) errors.push(`adapter ${descriptor.id} runtimeTargets must be unique`);
|
|
113
|
+
for (const capability of REQUIRED_RENDER_CAPABILITIES) {
|
|
114
|
+
if (descriptor.renderCapabilities?.[capability]?.supported !== true) errors.push(`adapter ${descriptor.id} is missing required capability ${capability}`);
|
|
115
|
+
}
|
|
116
|
+
for (const capability of Object.keys(descriptor.renderCapabilities || {})) {
|
|
117
|
+
if (!REQUIRED_RENDER_CAPABILITIES.includes(capability)) errors.push(`adapter ${descriptor.id} declares unknown capability ${capability}`);
|
|
118
|
+
}
|
|
119
|
+
if (typeof descriptor.planRuntime !== 'function') errors.push(`adapter ${descriptor.id} planRuntime is required`);
|
|
120
|
+
if (!descriptor.implementation || !['string', 'object'].includes(typeof descriptor.implementation)) errors.push(`adapter ${descriptor.id} implementation entry is required`);
|
|
121
|
+
if (!descriptor.recommendedCommands || typeof descriptor.recommendedCommands !== 'object') errors.push(`adapter ${descriptor.id} recommendedCommands are required`);
|
|
122
|
+
return errors;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function createRuntimeAdapterRegistry(descriptors, options = {}) {
|
|
126
|
+
const registry = {};
|
|
127
|
+
const errors = [];
|
|
128
|
+
for (const descriptor of descriptors) {
|
|
129
|
+
errors.push(...validateAdapterDescriptor(descriptor));
|
|
130
|
+
if (registry[descriptor?.id]) errors.push(`duplicate adapter id: ${descriptor.id}`);
|
|
131
|
+
if (descriptor?.id) registry[descriptor.id] = descriptor;
|
|
132
|
+
}
|
|
133
|
+
if (errors.length > 0) throw new Error(`Invalid runtime adapter registry:\n- ${errors.join('\n- ')}`);
|
|
134
|
+
const result = freeze({ ...registry });
|
|
135
|
+
if (options.testOnly === true) return result;
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export const RUNTIME_ADAPTERS = createRuntimeAdapterRegistry(DESCRIPTORS);
|
|
140
|
+
export const SUPPORTED_AGENT_IDS = Object.freeze(Object.keys(RUNTIME_ADAPTERS));
|
|
141
|
+
|
|
142
|
+
export function isSupportedAgent(agent) {
|
|
143
|
+
return Object.hasOwn(RUNTIME_ADAPTERS, agent);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function getRuntimeAdapter(agent) {
|
|
147
|
+
const adapter = RUNTIME_ADAPTERS[agent];
|
|
148
|
+
if (!adapter) throw new Error(`Unsupported Agent runtime: ${agent}`);
|
|
149
|
+
return adapter;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function runtimeDiscoveryPayload() {
|
|
153
|
+
return {
|
|
154
|
+
supportedAgents: [...SUPPORTED_AGENT_IDS],
|
|
155
|
+
requiredRenderCapabilities: [...REQUIRED_RENDER_CAPABILITIES],
|
|
156
|
+
agents: RUNTIME_ADAPTERS,
|
|
157
|
+
unsupportedAgentGuidance: UNSUPPORTED_AGENT_GUIDANCE,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function createRuntimeContext(value) {
|
|
162
|
+
const context = {
|
|
163
|
+
adapterId: value.adapterId,
|
|
164
|
+
targetRoot: path.resolve(value.targetRoot),
|
|
165
|
+
scope: value.scope,
|
|
166
|
+
rules: value.rules || { writes: [], nativeAssets: [], removals: [], actions: [] },
|
|
167
|
+
skills: value.skills || { writes: [], removals: [] },
|
|
168
|
+
findings: value.findings || [],
|
|
169
|
+
repairs: value.repairs || [],
|
|
170
|
+
warnings: value.warnings || [],
|
|
171
|
+
};
|
|
172
|
+
return freeze(context);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function createRuntimePlan(value) {
|
|
176
|
+
return freeze({
|
|
177
|
+
schemaVersion: 'buildr.runtime-plan/v1',
|
|
178
|
+
adapterId: value.adapterId,
|
|
179
|
+
targetRoot: path.resolve(value.targetRoot),
|
|
180
|
+
scope: value.scope,
|
|
181
|
+
writes: value.writes || [],
|
|
182
|
+
nativeAssets: value.nativeAssets || [],
|
|
183
|
+
removals: value.removals || [],
|
|
184
|
+
capabilityEvidence: value.capabilityEvidence || [],
|
|
185
|
+
findings: value.findings || [],
|
|
186
|
+
repairs: value.repairs || [],
|
|
187
|
+
warnings: value.warnings || [],
|
|
188
|
+
ruleActions: value.ruleActions || [],
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function safeTarget(targetRoot, targetFile) {
|
|
193
|
+
const root = path.resolve(targetRoot);
|
|
194
|
+
const target = path.resolve(targetFile);
|
|
195
|
+
return target !== root && target.startsWith(`${root}${path.sep}`);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function firstSymbolicLinkSegment(targetRoot, targetFile) {
|
|
199
|
+
const root = path.resolve(targetRoot);
|
|
200
|
+
const target = path.resolve(targetFile);
|
|
201
|
+
const relative = path.relative(root, target);
|
|
202
|
+
if (!relative || relative.startsWith('..') || path.isAbsolute(relative)) return null;
|
|
203
|
+
let current = root;
|
|
204
|
+
for (const segment of ['.', ...relative.split(path.sep)]) {
|
|
205
|
+
if (segment !== '.') current = path.join(current, segment);
|
|
206
|
+
try {
|
|
207
|
+
if (fs.lstatSync(current).isSymbolicLink()) return current;
|
|
208
|
+
} catch (error) {
|
|
209
|
+
if (error?.code === 'ENOENT') break;
|
|
210
|
+
throw error;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export function assertRuntimeTargetPath(targetRoot, targetFile, label = 'Runtime target') {
|
|
217
|
+
if (!safeTarget(targetRoot, targetFile)) {
|
|
218
|
+
throw new Error(`${label} is outside target root: ${targetFile}`);
|
|
219
|
+
}
|
|
220
|
+
const symbolicLink = firstSymbolicLinkSegment(targetRoot, targetFile);
|
|
221
|
+
if (symbolicLink) {
|
|
222
|
+
throw new Error(`${label} crosses a symbolic link: ${symbolicLink}`);
|
|
223
|
+
}
|
|
224
|
+
return path.resolve(targetFile);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
export function validateRuntimePlan(plan, adapter = getRuntimeAdapter(plan?.adapterId)) {
|
|
228
|
+
const errors = [];
|
|
229
|
+
if (plan?.schemaVersion !== 'buildr.runtime-plan/v1') errors.push('runtime plan schemaVersion is invalid');
|
|
230
|
+
if (plan?.adapterId !== adapter.id) errors.push(`runtime plan adapterId does not match descriptor: ${plan?.adapterId}`);
|
|
231
|
+
const writes = new Map();
|
|
232
|
+
for (const item of plan?.writes || []) {
|
|
233
|
+
try { assertRuntimeTargetPath(plan.targetRoot, item.targetFile, 'runtime write target'); }
|
|
234
|
+
catch (error) { errors.push(error.message); }
|
|
235
|
+
if (typeof item.content !== 'string') errors.push(`runtime write content must be text: ${item.targetFile}`);
|
|
236
|
+
const existing = writes.get(path.resolve(item.targetFile));
|
|
237
|
+
if (existing && existing.content !== item.content) errors.push(`runtime writes contain conflicting target: ${item.targetFile}`);
|
|
238
|
+
else writes.set(path.resolve(item.targetFile), item);
|
|
239
|
+
}
|
|
240
|
+
const removals = new Set();
|
|
241
|
+
for (const item of plan?.removals || []) {
|
|
242
|
+
const targetFile = typeof item === 'string' ? item : item.targetFile;
|
|
243
|
+
try { assertRuntimeTargetPath(plan.targetRoot, targetFile, 'runtime removal target'); }
|
|
244
|
+
catch (error) { errors.push(error.message); }
|
|
245
|
+
if (writes.has(path.resolve(targetFile))) errors.push(`runtime target cannot be written and removed: ${targetFile}`);
|
|
246
|
+
if (removals.has(path.resolve(targetFile))) errors.push(`runtime removals contain duplicate target: ${targetFile}`);
|
|
247
|
+
removals.add(path.resolve(targetFile));
|
|
248
|
+
}
|
|
249
|
+
const evidence = new Map((plan?.capabilityEvidence || []).map((item) => [item.capability, item]));
|
|
250
|
+
for (const capability of REQUIRED_RENDER_CAPABILITIES) {
|
|
251
|
+
if (evidence.get(capability)?.supported !== true) errors.push(`runtime plan is missing capability evidence: ${capability}`);
|
|
252
|
+
}
|
|
253
|
+
for (const capability of evidence.keys()) {
|
|
254
|
+
if (!REQUIRED_RENDER_CAPABILITIES.includes(capability)) errors.push(`runtime plan has unknown capability evidence: ${capability}`);
|
|
255
|
+
}
|
|
256
|
+
if (errors.length > 0) throw new Error(`Invalid runtime plan for ${adapter.id}:\n- ${errors.join('\n- ')}`);
|
|
257
|
+
return plan;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function runtimePath(targetRoot, targetFile) {
|
|
261
|
+
return path.relative(targetRoot, targetFile).split(path.sep).join('/');
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function diagnosticFinding(item, observedStatus, plan) {
|
|
265
|
+
const diagnostic = item.diagnostic || {};
|
|
266
|
+
const status = observedStatus === 'ok' && diagnostic.currentStatus
|
|
267
|
+
? diagnostic.currentStatus
|
|
268
|
+
: observedStatus === 'ok' && diagnostic.actionRequiredWhenCurrent ? 'stale' : observedStatus;
|
|
269
|
+
const code = diagnostic.codes?.[status] || diagnostic.code;
|
|
270
|
+
const message = diagnostic.messages?.[status]
|
|
271
|
+
|| (status === 'ok' ? `${diagnostic.label || item.source || 'runtime target'} is up to date.` : `${diagnostic.label || item.source || 'runtime target'} is ${status}.`);
|
|
272
|
+
const repair = status === 'ok' ? undefined : diagnostic.repairs?.[status] || diagnostic.repair;
|
|
273
|
+
return {
|
|
274
|
+
status,
|
|
275
|
+
path: runtimePath(plan.targetRoot, item.targetFile),
|
|
276
|
+
adapter: plan.adapterId,
|
|
277
|
+
source: item.source,
|
|
278
|
+
message,
|
|
279
|
+
...(code ? { code } : {}),
|
|
280
|
+
...(repair ? { repair } : {}),
|
|
281
|
+
userActionRequired: status !== 'ok' && status !== 'info' && status !== 'warning',
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export function reconcileRuntimePlan(plan, options = {}) {
|
|
286
|
+
validateRuntimePlan(plan);
|
|
287
|
+
const compareOnly = options.compareOnly === true;
|
|
288
|
+
const conflicts = [];
|
|
289
|
+
const changed = [];
|
|
290
|
+
const removed = [];
|
|
291
|
+
for (const item of plan.writes) {
|
|
292
|
+
if (!fs.existsSync(item.targetFile)) continue;
|
|
293
|
+
const current = fs.readFileSync(item.targetFile, 'utf8');
|
|
294
|
+
const matches = current === item.content || item.matchesCurrent?.(current) === true;
|
|
295
|
+
if (!matches && current !== item.sourceContent && item.isManaged && !item.isManaged(current)) conflicts.push(item);
|
|
296
|
+
}
|
|
297
|
+
const findings = [...plan.findings];
|
|
298
|
+
for (const item of conflicts) findings.push(diagnosticFinding(item, 'conflict', plan));
|
|
299
|
+
const plannedConflicts = findings.filter((finding) => finding.status === 'conflict');
|
|
300
|
+
if (!compareOnly && plannedConflicts.length > 0) {
|
|
301
|
+
throw new Error(`Runtime reconcile found conflict(s); no files were changed:\n- ${plannedConflicts.map((finding) => finding.message || finding.path).sort().join('\n- ')}`);
|
|
302
|
+
}
|
|
303
|
+
for (const item of plan.writes) {
|
|
304
|
+
if (conflicts.includes(item)) continue;
|
|
305
|
+
const current = fs.existsSync(item.targetFile) ? fs.readFileSync(item.targetFile, 'utf8') : null;
|
|
306
|
+
const status = current === null ? 'missing' : current === item.content || item.matchesCurrent?.(current) === true ? 'ok' : 'stale';
|
|
307
|
+
findings.push(diagnosticFinding(item, status, plan));
|
|
308
|
+
if (!compareOnly && status !== 'ok') {
|
|
309
|
+
fs.mkdirSync(path.dirname(item.targetFile), { recursive: true });
|
|
310
|
+
fs.writeFileSync(item.targetFile, item.content, 'utf8');
|
|
311
|
+
changed.push(item.targetFile);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
for (const nativeAsset of plan.nativeAssets) {
|
|
315
|
+
const item = typeof nativeAsset === 'string' ? { targetFile: nativeAsset, source: nativeAsset } : nativeAsset;
|
|
316
|
+
const status = fs.existsSync(item.targetFile) ? 'ok' : 'missing';
|
|
317
|
+
findings.push(diagnosticFinding(item, status, plan));
|
|
318
|
+
}
|
|
319
|
+
for (const removal of plan.removals) {
|
|
320
|
+
const item = typeof removal === 'string' ? { targetFile: removal } : removal;
|
|
321
|
+
if (!fs.existsSync(item.targetFile)) continue;
|
|
322
|
+
if (item.isManaged && !item.isManaged(item.type === 'directory' ? null : fs.readFileSync(item.targetFile, 'utf8'))) continue;
|
|
323
|
+
findings.push(diagnosticFinding(item, 'orphan', plan));
|
|
324
|
+
if (!compareOnly) {
|
|
325
|
+
fs.rmSync(item.targetFile, { recursive: item.type === 'directory', force: true });
|
|
326
|
+
removed.push(item.targetFile);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return { targetRoot: plan.targetRoot, adapterId: plan.adapterId, scope: plan.scope, changed, removed, findings, repairs: plan.repairs, warnings: plan.warnings, ruleActions: plan.ruleActions };
|
|
330
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import process from 'node:process';
|
|
5
|
+
import { parseRenderClaudeCodeArgs } from './render-claude-code.mjs';
|
|
6
|
+
import { checkRuntimeProjection, printRuntimeProjectionReport, repairCommands } from './projection.mjs';
|
|
7
|
+
|
|
8
|
+
export function claudeCodeRepairCommands(result) { return repairCommands(result, 'claude-code'); }
|
|
9
|
+
export function printRuntimeCheckReport(result) { printRuntimeProjectionReport(result, 'Claude Code'); }
|
|
10
|
+
|
|
11
|
+
export function checkClaudeCodeRuntime(argv, options = {}) {
|
|
12
|
+
const repoRoot = options.repoRoot ?? process.cwd();
|
|
13
|
+
const args = parseRenderClaudeCodeArgs(argv, options.command ?? 'buildr runtime check claude-code');
|
|
14
|
+
return checkRuntimeProjection({ repoRoot, targetRoot: path.resolve(repoRoot, args.target), scope: args.scope, adapterId: 'claude-code' });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
18
|
+
try {
|
|
19
|
+
const result = checkClaudeCodeRuntime(process.argv.slice(2), { command: 'node tools/runtime/check-claude-code.mjs' });
|
|
20
|
+
printRuntimeCheckReport(result);
|
|
21
|
+
process.exit(result.exitCode);
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.error(error.message);
|
|
24
|
+
process.exit(2);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import process from 'node:process';
|
|
5
|
+
import { parseRenderClaudeCodeArgs } from './render-claude-code.mjs';
|
|
6
|
+
import { checkRuntimeProjection, printRuntimeProjectionReport, repairCommands } from './projection.mjs';
|
|
7
|
+
|
|
8
|
+
export function codexRepairCommands(result) { return repairCommands(result, 'codex'); }
|
|
9
|
+
export function printCodexRuntimeCheckReport(result) { printRuntimeProjectionReport(result, 'Codex'); }
|
|
10
|
+
|
|
11
|
+
export function checkCodexRuntime(argv, options = {}) {
|
|
12
|
+
const repoRoot = options.repoRoot ?? process.cwd();
|
|
13
|
+
const args = parseRenderClaudeCodeArgs(argv, options.command ?? 'buildr runtime check codex');
|
|
14
|
+
return checkRuntimeProjection({ repoRoot, targetRoot: path.resolve(repoRoot, args.target), scope: args.scope, adapterId: 'codex' });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
18
|
+
try {
|
|
19
|
+
const result = checkCodexRuntime(process.argv.slice(2), { command: 'node tools/runtime/check-codex.mjs' });
|
|
20
|
+
printCodexRuntimeCheckReport(result);
|
|
21
|
+
process.exit(result.exitCode);
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.error(error.message);
|
|
24
|
+
process.exit(2);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import crypto from 'node:crypto';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import {
|
|
5
|
+
createRuntimeContext,
|
|
6
|
+
getRuntimeAdapter,
|
|
7
|
+
reconcileRuntimePlan,
|
|
8
|
+
} from './adapter-contract.mjs';
|
|
9
|
+
import {
|
|
10
|
+
buildSkillRenderPlan,
|
|
11
|
+
hasManagedSkillMarker,
|
|
12
|
+
resolvePackageAgentSkill,
|
|
13
|
+
resolveRenderSkills,
|
|
14
|
+
} from './render-claude-code.mjs';
|
|
15
|
+
import {
|
|
16
|
+
buildRuleDiscoveryPlan,
|
|
17
|
+
hasManagedRulesMarker,
|
|
18
|
+
planRulesRender,
|
|
19
|
+
resolveRuleScope,
|
|
20
|
+
} from './render-claude-code-rules.mjs';
|
|
21
|
+
|
|
22
|
+
const INSTALL_PLAN_MARKER = '<!-- Generated by Buildr. Agent action required.';
|
|
23
|
+
|
|
24
|
+
export function runtimeSkillScope(scope) {
|
|
25
|
+
const parts = scope.split('/');
|
|
26
|
+
return parts[0] === 'projects' && parts[1] ? `projects/${parts[1]}` : '.';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function skillDiagnostic(item, product) {
|
|
30
|
+
const installPlan = item.targetFile.includes(`${path.sep}skill-install-plans${path.sep}`);
|
|
31
|
+
if (installPlan) {
|
|
32
|
+
return {
|
|
33
|
+
label: `workspace Skill install plan ${path.basename(item.targetFile, '.md')}`,
|
|
34
|
+
code: 'runtime.skill_agent_action_required',
|
|
35
|
+
repair: 'agent-install',
|
|
36
|
+
repairs: { missing: 'skills-render', stale: 'agent-install' },
|
|
37
|
+
currentStatus: 'warning',
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
const conflict = `Refusing to overwrite non-Buildr-managed file: ${item.targetFile}`;
|
|
41
|
+
return product
|
|
42
|
+
? { label: 'product Agent Skill buildr', codes: { ok: 'runtime.product_skill_ok', missing: 'runtime.product_skill_missing', stale: 'runtime.product_skill_stale', conflict: 'runtime.product_skill_conflict' }, messages: { conflict }, repair: 'skill-install' }
|
|
43
|
+
: { label: `workspace Skill ${path.basename(path.dirname(item.targetFile))}`, codes: { ok: 'runtime.skill_ok', missing: 'runtime.skill_missing', stale: 'runtime.skill_stale', conflict: 'runtime.skill_conflict' }, messages: { conflict }, repair: 'skills-render' };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function decorateSkillWrites(items, product = false) {
|
|
47
|
+
return items.map((item) => ({
|
|
48
|
+
...item,
|
|
49
|
+
capability: product ? 'product-buildr-skill' : item.targetFile.includes(`${path.sep}skill-install-plans${path.sep}`) ? 'skill-install-plans' : 'workspace-project-skills',
|
|
50
|
+
isManaged: (content) => hasManagedSkillMarker(content) || content.includes(INSTALL_PLAN_MARKER),
|
|
51
|
+
diagnostic: skillDiagnostic(item, product),
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function ruleFinding(status, scope, message, metadata = {}) {
|
|
56
|
+
return { status, path: scope, message, userActionRequired: status !== 'info' && status !== 'ok', ...metadata };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function liveReferenceBridge(current, targetFile, sourceFile) {
|
|
60
|
+
if (!hasManagedRulesMarker(current) || !current.includes('@AGENTS.md')) return false;
|
|
61
|
+
return path.resolve(path.dirname(targetFile), 'AGENTS.md') === path.resolve(sourceFile);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function referenceMetadataStale(current, sourceFile) {
|
|
65
|
+
const hash = current.match(/\bhash:\s*([a-f0-9]{64})\b/i)?.[1]?.toLowerCase();
|
|
66
|
+
if (!hash) return false;
|
|
67
|
+
return hash !== crypto.createHash('sha256').update(fs.readFileSync(sourceFile)).digest('hex');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function assembleRules(repoRoot, targetRoot, scopeInfo, adapter) {
|
|
71
|
+
const discovery = buildRuleDiscoveryPlan(repoRoot, scopeInfo);
|
|
72
|
+
const findings = [
|
|
73
|
+
...discovery.warnings.map((message) => ruleFinding('warning', scopeInfo.scope, message, { code: 'runtime.scope_legacy', canonicalScope: scopeInfo.scope })),
|
|
74
|
+
...discovery.boundaries.map((boundary) => ruleFinding('info', boundary.path, `Rules discovery skipped boundary: ${boundary.reason}.`, { code: 'runtime.rules_discovery_boundary', boundaryReason: boundary.reason, userActionRequired: false })),
|
|
75
|
+
];
|
|
76
|
+
if (discovery.files.length === 0) {
|
|
77
|
+
findings.push(ruleFinding(adapter.projection === 'native-agents' ? 'missing' : 'conflict', adapter.projection === 'native-agents' ? 'AGENTS.md' : 'CLAUDE.md', `No AGENTS.md files found for scope: ${scopeInfo.scope}.`, { code: adapter.projection === 'native-agents' ? 'runtime.codex_rules_missing' : 'runtime.rules_bridge_missing' }));
|
|
78
|
+
return { writes: [], removals: [], nativeAssets: [], actions: [], findings, discovery };
|
|
79
|
+
}
|
|
80
|
+
if (adapter.projection === 'native-agents') {
|
|
81
|
+
return {
|
|
82
|
+
writes: [], removals: [], actions: [], findings, discovery,
|
|
83
|
+
nativeAssets: discovery.files.map((targetFile) => ({ targetFile, source: path.relative(repoRoot, targetFile), diagnostic: { label: 'Codex native AGENTS.md rule asset', code: 'runtime.codex_rules_ok' } })),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
const planned = planRulesRender(repoRoot, targetRoot, scopeInfo);
|
|
87
|
+
for (const conflict of planned.conflicts) findings.push(ruleFinding('conflict', scopeInfo.scope, conflict, { code: 'runtime.rules_bridge_conflict' }));
|
|
88
|
+
const diagnostic = { label: 'Claude Code rules bridge', codes: { ok: 'runtime.reference_bridge_ok', missing: 'runtime.rules_bridge_missing', stale: 'runtime.rules_bridge_stale', conflict: 'runtime.rules_bridge_conflict', orphan: 'runtime.rules_bridge_orphan' }, repair: 'rules-render' };
|
|
89
|
+
for (const item of planned.actions) {
|
|
90
|
+
if (!item.sourceFile || !fs.existsSync(item.targetFile)) continue;
|
|
91
|
+
const current = fs.readFileSync(item.targetFile, 'utf8');
|
|
92
|
+
if (liveReferenceBridge(current, item.targetFile, item.sourceFile) && referenceMetadataStale(current, item.sourceFile)) {
|
|
93
|
+
findings.push(ruleFinding('info', path.relative(targetRoot, item.targetFile).split(path.sep).join('/'), `${path.relative(repoRoot, item.sourceFile).split(path.sep).join('/')} bridge metadata hash is stale, but AGENTS.md is referenced live.`, { code: 'runtime.reference_bridge_metadata_stale', bridgeType: 'reference', impact: 'none', userActionRequired: false, repair: 'optional-rules-render', suggestion: 'No action required. Source is referenced live. Run rules render only if you want to refresh metadata.' }));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
writes: planned.actions.filter((item) => item.content !== null).map((item) => ({ ...item, capability: 'rules-entry', isManaged: hasManagedRulesMarker, matchesCurrent: (current) => liveReferenceBridge(current, item.targetFile, item.sourceFile), diagnostic })),
|
|
98
|
+
removals: planned.actions.filter((item) => item.content === null).map((item) => ({ targetFile: item.targetFile, capability: 'rules-entry', isManaged: hasManagedRulesMarker, diagnostic })),
|
|
99
|
+
nativeAssets: [], actions: planned.actions, findings, discovery: planned.discovery,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function assembleRuntimeProjection(options) {
|
|
104
|
+
const repoRoot = path.resolve(options.repoRoot);
|
|
105
|
+
const targetRoot = path.resolve(options.targetRoot ?? repoRoot);
|
|
106
|
+
if (!fs.existsSync(targetRoot) || !fs.statSync(targetRoot).isDirectory()) throw new Error(`Target directory does not exist: ${targetRoot}`);
|
|
107
|
+
const adapter = getRuntimeAdapter(options.adapterId);
|
|
108
|
+
const selection = { productSkill: false, rules: false, workspaceSkills: false, ...(options.selection || {}) };
|
|
109
|
+
const scopeInfo = resolveRuleScope(repoRoot, options.scope ?? '.');
|
|
110
|
+
const skillScope = runtimeSkillScope(scopeInfo.scope);
|
|
111
|
+
const skillConflicts = [];
|
|
112
|
+
const productWrites = selection.productSkill
|
|
113
|
+
? decorateSkillWrites(buildSkillRenderPlan(repoRoot, targetRoot, [resolvePackageAgentSkill(adapter.id, 'buildr')], adapter.id, { deferConflicts: true, conflicts: skillConflicts }), true)
|
|
114
|
+
: [];
|
|
115
|
+
const workspaceSkills = selection.workspaceSkills ? resolveRenderSkills(repoRoot, skillScope, adapter.id) : [];
|
|
116
|
+
const workspaceWrites = decorateSkillWrites(buildSkillRenderPlan(repoRoot, targetRoot, workspaceSkills, adapter.id, { deferConflicts: true, conflicts: skillConflicts }));
|
|
117
|
+
const rules = selection.rules ? assembleRules(repoRoot, targetRoot, scopeInfo, adapter) : { writes: [], removals: [], nativeAssets: [], actions: [], findings: [], discovery: { boundaries: [], warnings: [] } };
|
|
118
|
+
const warnings = workspaceSkills.filter((skill) => skill.resolved && !skill.resolved.integrity)
|
|
119
|
+
.map((skill) => `workspace Skill ${skill.id} uses a remote resolved source without integrity.`);
|
|
120
|
+
const context = createRuntimeContext({
|
|
121
|
+
adapterId: adapter.id, targetRoot, scope: scopeInfo.scope,
|
|
122
|
+
rules, skills: { writes: [...productWrites, ...workspaceWrites], removals: options.removals || [] },
|
|
123
|
+
findings: [...rules.findings, ...skillConflicts.map((message) => ({ status: 'conflict', path: skillScope, message, code: 'runtime.skill_conflict', userActionRequired: true }))], warnings,
|
|
124
|
+
});
|
|
125
|
+
return { plan: adapter.planRuntime(context), scopeInfo, discovery: rules.discovery, selection };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function summarize(findings) {
|
|
129
|
+
const counts = { ok: 0, info: 0, warning: 0, missing: 0, stale: 0, orphan: 0, conflict: 0 };
|
|
130
|
+
for (const finding of findings) counts[finding.status] = (counts[finding.status] ?? 0) + 1;
|
|
131
|
+
return counts;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function repairCommands(result, adapterId) {
|
|
135
|
+
const target = path.relative(process.cwd(), result.targetRoot).split(path.sep).join('/') || '.';
|
|
136
|
+
const has = (repair) => result.findings.some((item) => ['missing', 'stale', 'orphan'].includes(item.status) && item.repair === repair);
|
|
137
|
+
const commands = [];
|
|
138
|
+
if (has('skill-install')) commands.push(`buildr skill install ${adapterId} --target ${target}`);
|
|
139
|
+
if (has('rules-render')) commands.push(`buildr rules render ${adapterId} --scope ${result.scope} --target ${target}`);
|
|
140
|
+
if (has('skills-render')) commands.push(`buildr skills render ${adapterId} --scope ${runtimeSkillScope(result.scope)} --target ${target}`);
|
|
141
|
+
return commands;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function checkRuntimeProjection(options) {
|
|
145
|
+
const assembled = assembleRuntimeProjection({ ...options, selection: { productSkill: true, rules: true, workspaceSkills: true } });
|
|
146
|
+
const reconciled = reconcileRuntimePlan(assembled.plan, { compareOnly: true });
|
|
147
|
+
const counts = summarize(reconciled.findings);
|
|
148
|
+
const result = { ...reconciled, counts, exitCode: counts.conflict ? 2 : counts.missing || counts.stale || counts.orphan ? 1 : 0, requestedScope: assembled.scopeInfo.requestedScope, discoveryBoundaries: assembled.discovery.boundaries };
|
|
149
|
+
result.repairCommands = repairCommands(result, options.adapterId);
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function printRuntimeProjectionReport(result, label) {
|
|
154
|
+
console.log(`${label} runtime check for ${result.scope}`);
|
|
155
|
+
console.log(`Target: ${result.targetRoot}`);
|
|
156
|
+
console.log(`Status: ${Object.entries(result.counts).map(([key, value]) => `${key}=${value}`).join(' ')}`);
|
|
157
|
+
console.log('');
|
|
158
|
+
for (const finding of result.findings) console.log(`[${finding.status}] ${finding.path} - ${finding.message}`);
|
|
159
|
+
if (result.repairCommands.length) {
|
|
160
|
+
console.log('\nSuggested commands:');
|
|
161
|
+
for (const command of result.repairCommands) console.log(` ${command}`);
|
|
162
|
+
}
|
|
163
|
+
}
|