@specdest/devkit 0.4.0 → 0.4.2
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/README.md +4 -2
- package/package.json +1 -1
- package/src/canonical-project-operations.js +89 -14
- package/src/canonical-project.js +18 -2
- package/src/canonical-source.js +31 -4
- package/src/git-catalog.js +23 -0
- package/src/index.js +1 -1
- package/src/project-targeting.js +48 -0
- package/src/rule-profile.js +10 -2
package/README.md
CHANGED
|
@@ -9,7 +9,9 @@ The npm package contains only the CLI/core runtime. Canonical rules stay in the
|
|
|
9
9
|
```bash
|
|
10
10
|
npx @specdest/devkit status
|
|
11
11
|
npx @specdest/devkit sync
|
|
12
|
-
npx @specdest/devkit check
|
|
12
|
+
npx @specdest/devkit check [--refresh]
|
|
13
|
+
npx @specdest/devkit check --staged
|
|
14
|
+
npx @specdest/devkit check --files src/path/file.ts
|
|
13
15
|
npx @specdest/devkit rules
|
|
14
16
|
npx @specdest/devkit doctor
|
|
15
17
|
npx @specdest/devkit fuzz
|
|
@@ -17,7 +19,7 @@ npx @specdest/devkit fuzz
|
|
|
17
19
|
|
|
18
20
|
- `status` reads the cached canonical catalog and reports applicable/current/missing/outdated rules. Use `--refresh` to refresh the catalog from GitHub and `--json` for machine-readable output.
|
|
19
21
|
- `sync` refreshes the canonical catalog, downloads only missing rule-cache entries, installs missing applicable rules, and updates outdated ones.
|
|
20
|
-
- `check`
|
|
22
|
+
- `check` executes each distinct canonical checker implementation once from the local Devkit cache and projects that result onto every rule backed by that checker. Use `--refresh` to refresh the canonical catalog/checker cache first without reconciling project files. `--staged` resolves target paths/content from the Git index; `--files` supplies explicit project-relative paths. Repository-scoped checkers still run globally. Targeted checks are ephemeral and do not update `.specdest/state.json`; only a full check records canonical project status.
|
|
21
23
|
- `rules` loads AI-facing guidance without dumping the whole catalog. Use `--id <rule>`, `--category <category>`, `--failed`, or `--json`; with no selector it returns only rules marked `ai.load = "always"`.
|
|
22
24
|
- `doctor` reports project-stack/cache setup problems.
|
|
23
25
|
- `fuzz` dynamically exercises Zod boundary contracts with `fast-check` plus Specdest semantic amplifiers for nullish, blank-string, numeric/boolean, and array edge cases. It is separate from static `check` and from Stryker mutation testing.
|
package/package.json
CHANGED
|
@@ -6,6 +6,7 @@ import { createCanonicalSource, detectProjectStack } from './canonical-source.js
|
|
|
6
6
|
import { applicableCatalogEntries, prepareCachedManifest } from './canonical-project.js';
|
|
7
7
|
import { installRule, syncRule } from './remote-installer.js';
|
|
8
8
|
import { assertKnownRuleOverrides, readRuleProfile } from './rule-profile.js';
|
|
9
|
+
import { resolveProjectType } from './project-targeting.js';
|
|
9
10
|
import { readChecksState, readInstalledState, writeChecksState, writeInstalledState } from './sync-core.js';
|
|
10
11
|
|
|
11
12
|
function execute(command, args, cwd) {
|
|
@@ -39,6 +40,34 @@ function flags(args) {
|
|
|
39
40
|
return { refresh: args.includes('--refresh'), json: args.includes('--json') };
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
function checkFlags(args) {
|
|
44
|
+
const result = { refresh: false, mode: 'full', files: null };
|
|
45
|
+
const remaining = [];
|
|
46
|
+
for (const arg of args) {
|
|
47
|
+
if (arg === '--refresh') result.refresh = true;
|
|
48
|
+
else remaining.push(arg);
|
|
49
|
+
}
|
|
50
|
+
if (remaining.includes('--staged') && remaining.includes('--files')) {
|
|
51
|
+
throw new Error('Use either --staged or --files, not both');
|
|
52
|
+
}
|
|
53
|
+
if (remaining.includes('--staged')) {
|
|
54
|
+
if (remaining.length !== 1) throw new Error('Usage: specdest check [--refresh] [--staged | --files <path...>]');
|
|
55
|
+
result.mode = 'staged';
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
const filesIndex = remaining.indexOf('--files');
|
|
59
|
+
if (filesIndex >= 0) {
|
|
60
|
+
if (filesIndex !== 0 || remaining.length < 2) {
|
|
61
|
+
throw new Error('Usage: specdest check [--refresh] [--staged | --files <path...>]');
|
|
62
|
+
}
|
|
63
|
+
result.mode = 'files';
|
|
64
|
+
result.files = remaining.slice(1);
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
if (remaining.length) throw new Error(`Unknown option: ${remaining[0]}`);
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
|
|
42
71
|
async function cachedManifest(source, entry, canonicalCheckRoot, checkerRuntimeRoot, { offline = false } = {}) {
|
|
43
72
|
if (offline && !(await source.hasModule(entry.id, entry.version, entry.contentHash))) return null;
|
|
44
73
|
const root = await source.module(entry.id, entry.version, { contentHash: entry.contentHash });
|
|
@@ -50,6 +79,7 @@ async function canonicalContext(projectRoot, source, options = {}) {
|
|
|
50
79
|
const stack = await detectProjectStack(projectRoot);
|
|
51
80
|
const profile = await readRuleProfile(projectRoot);
|
|
52
81
|
assertKnownRuleOverrides(catalog.entries ?? [], profile);
|
|
82
|
+
const projectType = resolveProjectType(stack, profile);
|
|
53
83
|
const applicable = applicableCatalogEntries(catalog.entries ?? [], stack, profile);
|
|
54
84
|
const canonicalCheck = (catalog.entries ?? []).find((entry) => entry.id === 'canonical-check');
|
|
55
85
|
const checkerRuntime = (catalog.entries ?? []).find((entry) => entry.id === 'checker-runtime');
|
|
@@ -65,12 +95,12 @@ async function canonicalContext(projectRoot, source, options = {}) {
|
|
|
65
95
|
checkerRuntimeRoot = await source.module(checkerRuntime.id, checkerRuntime.version, { contentHash: checkerRuntime.contentHash });
|
|
66
96
|
}
|
|
67
97
|
}
|
|
68
|
-
return { catalog, stack, profile, applicable, canonicalCheckRoot, checkerRuntimeRoot };
|
|
98
|
+
return { catalog, stack, projectType, profile, applicable, canonicalCheckRoot, checkerRuntimeRoot };
|
|
69
99
|
}
|
|
70
100
|
|
|
71
101
|
export async function canonicalStatus(projectRoot, source, options = {}) {
|
|
72
102
|
const current = await readInstalledState(projectRoot);
|
|
73
|
-
const { stack, applicable } = await canonicalContext(projectRoot, source, options);
|
|
103
|
+
const { stack, projectType, applicable } = await canonicalContext(projectRoot, source, options);
|
|
74
104
|
const installed = current.installed;
|
|
75
105
|
const rows = applicable.map((entry) => {
|
|
76
106
|
const found = installed[entry.id];
|
|
@@ -81,6 +111,7 @@ export async function canonicalStatus(projectRoot, source, options = {}) {
|
|
|
81
111
|
const installedNonApplicable = Object.keys(installed).filter((id) => !applicableIds.has(id)).sort();
|
|
82
112
|
return {
|
|
83
113
|
stack,
|
|
114
|
+
projectType,
|
|
84
115
|
totals: {
|
|
85
116
|
applicable: rows.length,
|
|
86
117
|
current: rows.filter((row) => row.state === 'current').length,
|
|
@@ -152,7 +183,7 @@ async function cleanupCanonicalSkillTree(projectRoot, allowedFiles) {
|
|
|
152
183
|
|
|
153
184
|
export async function canonicalSync(projectRoot, source) {
|
|
154
185
|
const current = await readInstalledState(projectRoot);
|
|
155
|
-
const { catalog, stack, applicable, canonicalCheckRoot, checkerRuntimeRoot } = await canonicalContext(projectRoot, source, { refresh: true, withCheckers: true });
|
|
186
|
+
const { catalog, stack, projectType, applicable, canonicalCheckRoot, checkerRuntimeRoot } = await canonicalContext(projectRoot, source, { refresh: true, withCheckers: true });
|
|
156
187
|
const changed = [];
|
|
157
188
|
const allowedSkillFiles = new Set();
|
|
158
189
|
for (const entry of applicable) {
|
|
@@ -180,18 +211,50 @@ export async function canonicalSync(projectRoot, source) {
|
|
|
180
211
|
}
|
|
181
212
|
}
|
|
182
213
|
}
|
|
214
|
+
const applicableIds = new Set(applicable.map((entry) => entry.id));
|
|
215
|
+
const catalogRules = new Map(
|
|
216
|
+
(catalog.entries ?? []).filter((entry) => entry.kind === 'rule').map((entry) => [entry.id, entry]),
|
|
217
|
+
);
|
|
218
|
+
for (const id of Object.keys(current.installed).filter((installedId) => !applicableIds.has(installedId))) {
|
|
219
|
+
const entry = catalogRules.get(id);
|
|
220
|
+
if (!entry) continue;
|
|
221
|
+
const prepared = await cachedManifest(source, entry, canonicalCheckRoot, checkerRuntimeRoot);
|
|
222
|
+
for (const copy of prepared.manifest.copies ?? []) {
|
|
223
|
+
const target = String(copy.target ?? copy.path ?? '');
|
|
224
|
+
if (target.startsWith('.agents/skills/')) allowedSkillFiles.add(target);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
183
227
|
const removed = await cleanupCanonicalInstallState(projectRoot, catalog.entries ?? []);
|
|
184
228
|
await cleanupCanonicalSkillTree(projectRoot, allowedSkillFiles);
|
|
185
|
-
return { stack, applicable: applicable.length, changed, removed };
|
|
229
|
+
return { stack, projectType, applicable: applicable.length, changed, removed };
|
|
186
230
|
}
|
|
187
231
|
|
|
188
232
|
export async function canonicalCheck(projectRoot, source, options = {}) {
|
|
189
233
|
const current = await readInstalledState(projectRoot);
|
|
190
|
-
const { applicable, canonicalCheckRoot, checkerRuntimeRoot } = await canonicalContext(projectRoot, source, {
|
|
234
|
+
const { catalog, applicable, canonicalCheckRoot, checkerRuntimeRoot } = await canonicalContext(projectRoot, source, {
|
|
235
|
+
offline: false,
|
|
236
|
+
withCheckers: true,
|
|
237
|
+
refresh: options.refresh ?? false,
|
|
238
|
+
});
|
|
191
239
|
const byId = new Map(applicable.map((entry) => [entry.id, entry]));
|
|
240
|
+
const catalogRules = new Map(
|
|
241
|
+
(catalog.entries ?? []).filter((entry) => entry.kind === 'rule').map((entry) => [entry.id, entry]),
|
|
242
|
+
);
|
|
243
|
+
for (const id of Object.keys(current.installed)) {
|
|
244
|
+
const entry = catalogRules.get(id);
|
|
245
|
+
if (entry && !byId.has(id)) byId.set(id, entry);
|
|
246
|
+
}
|
|
192
247
|
const results = {};
|
|
193
248
|
const groups = new Map();
|
|
194
249
|
const executor = options.executor ?? execute;
|
|
250
|
+
const targetMode = options.mode ?? 'full';
|
|
251
|
+
let targetFiles = options.files ?? null;
|
|
252
|
+
if (targetMode === 'staged' && targetFiles === null) {
|
|
253
|
+
const staged = await executor('git', ['diff', '--cached', '--name-only', '-z', '--diff-filter=ACMR'], projectRoot);
|
|
254
|
+
if (staged.exitCode !== 0) throw new Error(staged.stderr || 'Unable to read staged files');
|
|
255
|
+
targetFiles = staged.stdout.split('\0').filter(Boolean);
|
|
256
|
+
}
|
|
257
|
+
const normalizedTargetFiles = (targetFiles ?? []).map((file) => String(file).replaceAll('\\', '/'));
|
|
195
258
|
let exitCode = 0;
|
|
196
259
|
for (const [id, installed] of Object.entries(current.installed).sort(([a], [b]) => a.localeCompare(b))) {
|
|
197
260
|
const entry = byId.get(id);
|
|
@@ -203,17 +266,25 @@ export async function canonicalCheck(projectRoot, source, options = {}) {
|
|
|
203
266
|
exitCode = 1;
|
|
204
267
|
continue;
|
|
205
268
|
}
|
|
206
|
-
|
|
269
|
+
if (prepared.execution === 'manual') continue;
|
|
270
|
+
const baseKey = prepared.checkerKey ?? `path:${prepared.checkerPath}:${JSON.stringify(prepared.checkerArgs ?? [])}`;
|
|
271
|
+
const key = `${baseKey}:targeting:${prepared.targeting?.scope ?? 'repository'}:execution:${prepared.execution ?? 'automatic'}`;
|
|
207
272
|
const group = groups.get(key) ?? {
|
|
208
273
|
checkerPath: prepared.checkerPath,
|
|
209
274
|
checkerArgs: prepared.checkerArgs ?? [],
|
|
275
|
+
targeting: prepared.targeting ?? { scope: 'repository' },
|
|
210
276
|
ids: [],
|
|
211
277
|
};
|
|
212
278
|
group.ids.push(id);
|
|
213
279
|
groups.set(key, group);
|
|
214
280
|
}
|
|
215
281
|
for (const group of groups.values()) {
|
|
216
|
-
const
|
|
282
|
+
const args = [group.checkerPath, ...group.checkerArgs];
|
|
283
|
+
if (targetMode !== 'full' && group.targeting.scope !== 'repository') {
|
|
284
|
+
args.push('--target-mode', targetMode);
|
|
285
|
+
for (const file of normalizedTargetFiles) args.push('--target-file', file);
|
|
286
|
+
}
|
|
287
|
+
const execution = await executor('node', args, projectRoot);
|
|
217
288
|
const result = { status: execution.exitCode === 0 ? 'pass' : 'fail', ...execution };
|
|
218
289
|
for (const id of group.ids) results[id] = result;
|
|
219
290
|
if (execution.exitCode !== 0) exitCode = 1;
|
|
@@ -221,11 +292,13 @@ export async function canonicalCheck(projectRoot, source, options = {}) {
|
|
|
221
292
|
const orderedResults = Object.fromEntries(
|
|
222
293
|
Object.entries(results).sort(([a], [b]) => a.localeCompare(b)),
|
|
223
294
|
);
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
295
|
+
if (targetMode === 'full') {
|
|
296
|
+
await writeChecksState(projectRoot, {
|
|
297
|
+
schemaVersion: 2,
|
|
298
|
+
checkedAt: new Date().toISOString(),
|
|
299
|
+
results: Object.fromEntries(Object.entries(orderedResults).map(([id, result]) => [id, { status: result.status }])),
|
|
300
|
+
});
|
|
301
|
+
}
|
|
229
302
|
return { exitCode, results: orderedResults };
|
|
230
303
|
}
|
|
231
304
|
|
|
@@ -334,8 +407,10 @@ export function createCanonicalCommandHandlers(projectRoot = process.cwd(), opti
|
|
|
334
407
|
return 0;
|
|
335
408
|
},
|
|
336
409
|
check: async (args, io) => {
|
|
337
|
-
|
|
338
|
-
|
|
410
|
+
let parsed;
|
|
411
|
+
try { parsed = checkFlags(args); }
|
|
412
|
+
catch (error) { io.stderr.write(`${error.message}\n`); return 2; }
|
|
413
|
+
const result = await canonicalCheck(projectRoot, source, parsed);
|
|
339
414
|
for (const [id, check] of Object.entries(result.results)) io.stdout.write(`${check.status.toUpperCase()} ${id}\n`);
|
|
340
415
|
return result.exitCode;
|
|
341
416
|
},
|
package/src/canonical-project.js
CHANGED
|
@@ -47,6 +47,18 @@ async function cacheProjectCopies(copies, sourceRoot, ruleRoot, label) {
|
|
|
47
47
|
return result;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
function checkerPolicy(manifest) {
|
|
51
|
+
const scope = manifest?.targeting?.scope ?? 'repository';
|
|
52
|
+
if (!['file', 'related', 'repository'].includes(scope)) {
|
|
53
|
+
throw new Error(`Invalid checker targeting scope: ${scope}`);
|
|
54
|
+
}
|
|
55
|
+
const execution = manifest?.execution ?? 'automatic';
|
|
56
|
+
if (!['automatic', 'manual'].includes(execution)) {
|
|
57
|
+
throw new Error(`Invalid checker execution class: ${execution}`);
|
|
58
|
+
}
|
|
59
|
+
return { targeting: { scope }, execution };
|
|
60
|
+
}
|
|
61
|
+
|
|
50
62
|
function runtimeChecker(manifest, runtimeRoot) {
|
|
51
63
|
if (manifest?.check?.command?.[0] !== 'node') return null;
|
|
52
64
|
const target = String(manifest.check.command[1] ?? '');
|
|
@@ -57,12 +69,12 @@ function runtimeChecker(manifest, runtimeRoot) {
|
|
|
57
69
|
};
|
|
58
70
|
}
|
|
59
71
|
|
|
60
|
-
import {
|
|
72
|
+
import { projectRuleApplies } from './project-targeting.js';
|
|
61
73
|
|
|
62
74
|
export function applicableCatalogEntries(entries, stack, profile) {
|
|
63
75
|
return entries
|
|
64
76
|
.filter((entry) => entry.kind === 'rule')
|
|
65
|
-
.filter((entry) =>
|
|
77
|
+
.filter((entry) => projectRuleApplies(entry, stack, profile, entries))
|
|
66
78
|
.sort((a, b) => a.id.localeCompare(b.id));
|
|
67
79
|
}
|
|
68
80
|
|
|
@@ -84,10 +96,12 @@ export async function prepareCachedManifest(entry, ruleRoot, canonicalCheckRoot,
|
|
|
84
96
|
|
|
85
97
|
const copies = await cacheProjectCopies(raw.copies ?? [], ruleRoot, ruleRoot, entry.id);
|
|
86
98
|
let checker = runtimeChecker(raw, runtimeRoot);
|
|
99
|
+
let { targeting, execution } = checkerPolicy(raw);
|
|
87
100
|
if (!checker && raw.checker?.path && canonicalCheckRoot) {
|
|
88
101
|
const relative = String(raw.checker.path).replace(/^canonical-check\//u, '');
|
|
89
102
|
const implementationRoot = dirname(join(canonicalCheckRoot, relative));
|
|
90
103
|
const implementationManifest = await readManifest(implementationRoot);
|
|
104
|
+
({ targeting, execution } = checkerPolicy(implementationManifest));
|
|
91
105
|
await materializeCopies(implementationManifest, implementationRoot, runtimeRoot);
|
|
92
106
|
copies.push(...await cacheProjectCopies(
|
|
93
107
|
implementationManifest.copies ?? [],
|
|
@@ -116,5 +130,7 @@ export async function prepareCachedManifest(entry, ruleRoot, canonicalCheckRoot,
|
|
|
116
130
|
checkerPath: checker?.path ?? null,
|
|
117
131
|
checkerArgs: checker?.args ?? [],
|
|
118
132
|
checkerKey,
|
|
133
|
+
targeting,
|
|
134
|
+
execution,
|
|
119
135
|
};
|
|
120
136
|
}
|
package/src/canonical-source.js
CHANGED
|
@@ -207,10 +207,7 @@ export async function fetchGitHubModule(id, options = {}) {
|
|
|
207
207
|
return { files: await directoryFiles(root) };
|
|
208
208
|
}
|
|
209
209
|
|
|
210
|
-
|
|
211
|
-
let pkg = {};
|
|
212
|
-
try { pkg = JSON.parse(await readFile(join(projectRoot, 'package.json'), 'utf8')); } catch (error) { if (error?.code !== 'ENOENT') throw error; }
|
|
213
|
-
const deps = { ...(pkg.dependencies ?? {}), ...(pkg.devDependencies ?? {}) };
|
|
210
|
+
function stackFromDependencies(deps) {
|
|
214
211
|
const expo = Boolean(deps.expo);
|
|
215
212
|
const vite = Boolean(deps.vite || deps['@vitejs/plugin-react']);
|
|
216
213
|
const convex = Boolean(deps.convex);
|
|
@@ -221,3 +218,33 @@ export async function detectProjectStack(projectRoot) {
|
|
|
221
218
|
if (vite && supabase) return 'stack-vite-supabase';
|
|
222
219
|
return null;
|
|
223
220
|
}
|
|
221
|
+
|
|
222
|
+
async function readPackageDependencies(path) {
|
|
223
|
+
try {
|
|
224
|
+
const pkg = JSON.parse(await readFile(path, 'utf8'));
|
|
225
|
+
return { ...(pkg.dependencies ?? {}), ...(pkg.devDependencies ?? {}) };
|
|
226
|
+
} catch (error) {
|
|
227
|
+
if (error?.code === 'ENOENT') return null;
|
|
228
|
+
throw error;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export async function detectProjectStack(projectRoot) {
|
|
233
|
+
const dependencySets = [];
|
|
234
|
+
const rootDeps = await readPackageDependencies(join(projectRoot, 'package.json'));
|
|
235
|
+
if (rootDeps) dependencySets.push(rootDeps);
|
|
236
|
+
for (const group of ['apps', 'packages']) {
|
|
237
|
+
let children = [];
|
|
238
|
+
try { children = await readdir(join(projectRoot, group), { withFileTypes: true }); }
|
|
239
|
+
catch (error) { if (error?.code !== 'ENOENT') throw error; }
|
|
240
|
+
for (const child of children.filter((entry) => entry.isDirectory())) {
|
|
241
|
+
const deps = await readPackageDependencies(join(projectRoot, group, child.name, 'package.json'));
|
|
242
|
+
if (deps) dependencySets.push(deps);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
const detected = new Set(dependencySets.map(stackFromDependencies).filter(Boolean));
|
|
246
|
+
if (detected.size === 1) return [...detected][0];
|
|
247
|
+
if (detected.size > 1) throw new Error(`Multiple canonical stacks detected: ${[...detected].sort().join(', ')}`);
|
|
248
|
+
const combined = Object.assign({}, ...dependencySets);
|
|
249
|
+
return stackFromDependencies(combined);
|
|
250
|
+
}
|
package/src/git-catalog.js
CHANGED
|
@@ -67,6 +67,21 @@ function notApplicableRuleIds(manifest) {
|
|
|
67
67
|
return [...new Set(manifest.notApplicableRuleIds.map(String))].sort();
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
+
function projectProfiles(manifest) {
|
|
71
|
+
if (!manifest.projectProfiles) return undefined;
|
|
72
|
+
if (typeof manifest.projectProfiles !== 'object' || Array.isArray(manifest.projectProfiles)) {
|
|
73
|
+
throw new Error(`Invalid projectProfiles: ${manifest.id}`);
|
|
74
|
+
}
|
|
75
|
+
return Object.fromEntries(Object.entries(manifest.projectProfiles).sort(([a], [b]) => a.localeCompare(b)).map(
|
|
76
|
+
([name, definition]) => {
|
|
77
|
+
if (!definition || !Array.isArray(definition.ruleIds) || definition.ruleIds.length === 0) {
|
|
78
|
+
throw new Error(`Invalid project profile: ${manifest.id}:${name}`);
|
|
79
|
+
}
|
|
80
|
+
return [name, { ruleIds: [...new Set(definition.ruleIds.map(String))].sort() }];
|
|
81
|
+
},
|
|
82
|
+
));
|
|
83
|
+
}
|
|
84
|
+
|
|
70
85
|
function stringList(value) {
|
|
71
86
|
if (!Array.isArray(value) || value.length === 0) return undefined;
|
|
72
87
|
return [...new Set(value.map(String))].sort();
|
|
@@ -126,6 +141,9 @@ export async function buildGitCatalog({ modulesRoot, repositoryRevision = 'worki
|
|
|
126
141
|
...(kind === 'module' && notApplicableRuleIds(manifest)
|
|
127
142
|
? { notApplicableRuleIds: notApplicableRuleIds(manifest) }
|
|
128
143
|
: {}),
|
|
144
|
+
...(kind === 'module' && projectProfiles(manifest)
|
|
145
|
+
? { projectProfiles: projectProfiles(manifest) }
|
|
146
|
+
: {}),
|
|
129
147
|
});
|
|
130
148
|
}
|
|
131
149
|
const ruleIds = new Set(entries.filter((entry) => entry.kind === 'rule').map((entry) => entry.id));
|
|
@@ -133,6 +151,11 @@ export async function buildGitCatalog({ modulesRoot, repositoryRevision = 'worki
|
|
|
133
151
|
for (const ruleId of entry.notApplicableRuleIds ?? []) {
|
|
134
152
|
if (!ruleIds.has(ruleId)) throw new Error(`Unknown not-applicable rule: ${entry.id}:${ruleId}`);
|
|
135
153
|
}
|
|
154
|
+
for (const [profileName, definition] of Object.entries(entry.projectProfiles ?? {})) {
|
|
155
|
+
for (const ruleId of definition.ruleIds) {
|
|
156
|
+
if (!ruleIds.has(ruleId)) throw new Error(`Unknown project-profile rule: ${entry.id}:${profileName}:${ruleId}`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
136
159
|
for (const childId of entry.composedOf ?? []) {
|
|
137
160
|
if (!ruleIds.has(childId)) throw new Error(`Unknown composed rule: ${entry.id}:${childId}`);
|
|
138
161
|
if (childId === entry.id) throw new Error(`Rule cannot compose itself: ${entry.id}`);
|
package/src/index.js
CHANGED
|
@@ -13,7 +13,7 @@ export * from './fuzz.js';
|
|
|
13
13
|
|
|
14
14
|
export const COMMAND_NAMES = ['status', 'sync', 'check', 'rules', 'ai-instruction', 'doctor', 'fuzz'];
|
|
15
15
|
|
|
16
|
-
const HELP = `Usage: specdest <command> [options]\n\nCommands:\n status [--refresh] [--json] Show canonical applicability/install status\n sync Refresh catalog/cache and install/update applicable rules\n check
|
|
16
|
+
const HELP = `Usage: specdest <command> [options]\n\nCommands:\n status [--refresh] [--json] Show canonical applicability/install status\n sync Refresh catalog/cache and install/update applicable rules\n check [--refresh] [--staged | --files <path...>] Run canonical checks, optionally targeted\n rules [options] Load applicable rule guidance for AI/task context\n ai-instruction Print the minimal AI bootstrap instruction\n doctor Diagnose project/cache setup\n fuzz Dynamically fuzz Zod contract semantics\n`;
|
|
17
17
|
|
|
18
18
|
export async function runCli(argv, io = process, handlers = {}) {
|
|
19
19
|
const [command, ...args] = argv;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { EMPTY_RULE_PROFILE } from './rule-profile.js';
|
|
2
|
+
|
|
3
|
+
export const PROJECT_TYPES = Object.freeze([
|
|
4
|
+
'product',
|
|
5
|
+
'static-site',
|
|
6
|
+
'tooling',
|
|
7
|
+
'backend',
|
|
8
|
+
'monorepo',
|
|
9
|
+
'generic',
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
export function resolveProjectType(stack, profile = EMPTY_RULE_PROFILE) {
|
|
13
|
+
return profile.projectType ?? (stack ? 'product' : 'generic');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function profileDefinition(entries, projectType) {
|
|
17
|
+
if (projectType === 'product') return null;
|
|
18
|
+
const module = entries.find(
|
|
19
|
+
(entry) => entry.kind === 'module' && entry.id === 'project-profiles',
|
|
20
|
+
);
|
|
21
|
+
const definition = module?.projectProfiles?.[projectType];
|
|
22
|
+
if (!definition) throw new Error(`Canonical project profile not found: ${projectType}`);
|
|
23
|
+
return definition;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function capabilitySatisfied(entry, profile) {
|
|
27
|
+
const capabilities = new Set(profile.capabilities ?? []);
|
|
28
|
+
return !entry.requiredCapabilities?.some((capability) => !capabilities.has(capability));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function projectRuleApplies(entry, stack, profile = EMPTY_RULE_PROFILE, entries = []) {
|
|
32
|
+
const included = new Set(profile.include ?? []);
|
|
33
|
+
const excluded = new Set((profile.exclude ?? []).map((item) => item.id));
|
|
34
|
+
if (excluded.has(entry.id)) return false;
|
|
35
|
+
if (included.has(entry.id)) return true;
|
|
36
|
+
|
|
37
|
+
const projectType = resolveProjectType(stack, profile);
|
|
38
|
+
if (projectType === 'product') {
|
|
39
|
+
if (entry.applicableStacks?.length && (!stack || !entry.applicableStacks.includes(stack))) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return capabilitySatisfied(entry, profile);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const definition = profileDefinition(entries, projectType);
|
|
46
|
+
if (!definition.ruleIds.includes(entry.id)) return false;
|
|
47
|
+
return capabilitySatisfied(entry, profile);
|
|
48
|
+
}
|
package/src/rule-profile.js
CHANGED
|
@@ -2,7 +2,13 @@ import { readFile } from 'node:fs/promises';
|
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
|
|
4
4
|
export const RULE_PROFILE_RELATIVE_PATH = '.specdest/rules.json';
|
|
5
|
-
export const EMPTY_RULE_PROFILE = Object.freeze({
|
|
5
|
+
export const EMPTY_RULE_PROFILE = Object.freeze({
|
|
6
|
+
schemaVersion: 1,
|
|
7
|
+
projectType: null,
|
|
8
|
+
capabilities: [],
|
|
9
|
+
include: [],
|
|
10
|
+
exclude: [],
|
|
11
|
+
});
|
|
6
12
|
|
|
7
13
|
function uniqueStrings(values, label) {
|
|
8
14
|
if (!Array.isArray(values ?? [])) throw new Error(`Invalid ${label}`);
|
|
@@ -13,6 +19,8 @@ export function validateRuleProfile(profile) {
|
|
|
13
19
|
if (!profile || typeof profile !== 'object' || Array.isArray(profile) || profile.schemaVersion !== 1) {
|
|
14
20
|
throw new Error('Invalid .specdest/rules.json profile');
|
|
15
21
|
}
|
|
22
|
+
const projectType = profile.projectType == null ? null : String(profile.projectType).trim();
|
|
23
|
+
if (profile.projectType != null && !projectType) throw new Error('Invalid rule profile projectType');
|
|
16
24
|
const capabilities = uniqueStrings(profile.capabilities, 'rule profile capabilities');
|
|
17
25
|
const include = uniqueStrings(profile.include, 'rule profile include');
|
|
18
26
|
if (!Array.isArray(profile.exclude ?? [])) throw new Error('Invalid rule profile exclude');
|
|
@@ -22,7 +30,7 @@ export function validateRuleProfile(profile) {
|
|
|
22
30
|
}
|
|
23
31
|
return { id: String(item.id).trim(), reason: String(item.reason).trim() };
|
|
24
32
|
}).sort((a, b) => a.id.localeCompare(b.id));
|
|
25
|
-
return { schemaVersion: 1, capabilities, include, exclude };
|
|
33
|
+
return { schemaVersion: 1, projectType, capabilities, include, exclude };
|
|
26
34
|
}
|
|
27
35
|
|
|
28
36
|
export async function readRuleProfile(projectRoot) {
|