@specdest/devkit 0.4.0 → 0.4.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/README.md +2 -2
- package/package.json +1 -1
- package/src/canonical-project-operations.js +35 -7
- package/src/canonical-project.js +2 -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,7 @@ 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
13
|
npx @specdest/devkit rules
|
|
14
14
|
npx @specdest/devkit doctor
|
|
15
15
|
npx @specdest/devkit fuzz
|
|
@@ -17,7 +17,7 @@ npx @specdest/devkit fuzz
|
|
|
17
17
|
|
|
18
18
|
- `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
19
|
- `sync` refreshes the canonical catalog, downloads only missing rule-cache entries, installs missing applicable rules, and updates outdated ones.
|
|
20
|
-
- `check`
|
|
20
|
+
- `check` executes each distinct canonical checker implementation once from the local Devkit cache, projects that result onto every rule backed by that checker, and updates `.specdest/state.json`. Use `--refresh` to refresh the canonical catalog/checker cache first without reconciling project files.
|
|
21
21
|
- `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
22
|
- `doctor` reports project-stack/cache setup problems.
|
|
23
23
|
- `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) {
|
|
@@ -50,6 +51,7 @@ async function canonicalContext(projectRoot, source, options = {}) {
|
|
|
50
51
|
const stack = await detectProjectStack(projectRoot);
|
|
51
52
|
const profile = await readRuleProfile(projectRoot);
|
|
52
53
|
assertKnownRuleOverrides(catalog.entries ?? [], profile);
|
|
54
|
+
const projectType = resolveProjectType(stack, profile);
|
|
53
55
|
const applicable = applicableCatalogEntries(catalog.entries ?? [], stack, profile);
|
|
54
56
|
const canonicalCheck = (catalog.entries ?? []).find((entry) => entry.id === 'canonical-check');
|
|
55
57
|
const checkerRuntime = (catalog.entries ?? []).find((entry) => entry.id === 'checker-runtime');
|
|
@@ -65,12 +67,12 @@ async function canonicalContext(projectRoot, source, options = {}) {
|
|
|
65
67
|
checkerRuntimeRoot = await source.module(checkerRuntime.id, checkerRuntime.version, { contentHash: checkerRuntime.contentHash });
|
|
66
68
|
}
|
|
67
69
|
}
|
|
68
|
-
return { catalog, stack, profile, applicable, canonicalCheckRoot, checkerRuntimeRoot };
|
|
70
|
+
return { catalog, stack, projectType, profile, applicable, canonicalCheckRoot, checkerRuntimeRoot };
|
|
69
71
|
}
|
|
70
72
|
|
|
71
73
|
export async function canonicalStatus(projectRoot, source, options = {}) {
|
|
72
74
|
const current = await readInstalledState(projectRoot);
|
|
73
|
-
const { stack, applicable } = await canonicalContext(projectRoot, source, options);
|
|
75
|
+
const { stack, projectType, applicable } = await canonicalContext(projectRoot, source, options);
|
|
74
76
|
const installed = current.installed;
|
|
75
77
|
const rows = applicable.map((entry) => {
|
|
76
78
|
const found = installed[entry.id];
|
|
@@ -81,6 +83,7 @@ export async function canonicalStatus(projectRoot, source, options = {}) {
|
|
|
81
83
|
const installedNonApplicable = Object.keys(installed).filter((id) => !applicableIds.has(id)).sort();
|
|
82
84
|
return {
|
|
83
85
|
stack,
|
|
86
|
+
projectType,
|
|
84
87
|
totals: {
|
|
85
88
|
applicable: rows.length,
|
|
86
89
|
current: rows.filter((row) => row.state === 'current').length,
|
|
@@ -152,7 +155,7 @@ async function cleanupCanonicalSkillTree(projectRoot, allowedFiles) {
|
|
|
152
155
|
|
|
153
156
|
export async function canonicalSync(projectRoot, source) {
|
|
154
157
|
const current = await readInstalledState(projectRoot);
|
|
155
|
-
const { catalog, stack, applicable, canonicalCheckRoot, checkerRuntimeRoot } = await canonicalContext(projectRoot, source, { refresh: true, withCheckers: true });
|
|
158
|
+
const { catalog, stack, projectType, applicable, canonicalCheckRoot, checkerRuntimeRoot } = await canonicalContext(projectRoot, source, { refresh: true, withCheckers: true });
|
|
156
159
|
const changed = [];
|
|
157
160
|
const allowedSkillFiles = new Set();
|
|
158
161
|
for (const entry of applicable) {
|
|
@@ -180,15 +183,39 @@ export async function canonicalSync(projectRoot, source) {
|
|
|
180
183
|
}
|
|
181
184
|
}
|
|
182
185
|
}
|
|
186
|
+
const applicableIds = new Set(applicable.map((entry) => entry.id));
|
|
187
|
+
const catalogRules = new Map(
|
|
188
|
+
(catalog.entries ?? []).filter((entry) => entry.kind === 'rule').map((entry) => [entry.id, entry]),
|
|
189
|
+
);
|
|
190
|
+
for (const id of Object.keys(current.installed).filter((installedId) => !applicableIds.has(installedId))) {
|
|
191
|
+
const entry = catalogRules.get(id);
|
|
192
|
+
if (!entry) continue;
|
|
193
|
+
const prepared = await cachedManifest(source, entry, canonicalCheckRoot, checkerRuntimeRoot);
|
|
194
|
+
for (const copy of prepared.manifest.copies ?? []) {
|
|
195
|
+
const target = String(copy.target ?? copy.path ?? '');
|
|
196
|
+
if (target.startsWith('.agents/skills/')) allowedSkillFiles.add(target);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
183
199
|
const removed = await cleanupCanonicalInstallState(projectRoot, catalog.entries ?? []);
|
|
184
200
|
await cleanupCanonicalSkillTree(projectRoot, allowedSkillFiles);
|
|
185
|
-
return { stack, applicable: applicable.length, changed, removed };
|
|
201
|
+
return { stack, projectType, applicable: applicable.length, changed, removed };
|
|
186
202
|
}
|
|
187
203
|
|
|
188
204
|
export async function canonicalCheck(projectRoot, source, options = {}) {
|
|
189
205
|
const current = await readInstalledState(projectRoot);
|
|
190
|
-
const { applicable, canonicalCheckRoot, checkerRuntimeRoot } = await canonicalContext(projectRoot, source, {
|
|
206
|
+
const { catalog, applicable, canonicalCheckRoot, checkerRuntimeRoot } = await canonicalContext(projectRoot, source, {
|
|
207
|
+
offline: false,
|
|
208
|
+
withCheckers: true,
|
|
209
|
+
refresh: options.refresh ?? false,
|
|
210
|
+
});
|
|
191
211
|
const byId = new Map(applicable.map((entry) => [entry.id, entry]));
|
|
212
|
+
const catalogRules = new Map(
|
|
213
|
+
(catalog.entries ?? []).filter((entry) => entry.kind === 'rule').map((entry) => [entry.id, entry]),
|
|
214
|
+
);
|
|
215
|
+
for (const id of Object.keys(current.installed)) {
|
|
216
|
+
const entry = catalogRules.get(id);
|
|
217
|
+
if (entry && !byId.has(id)) byId.set(id, entry);
|
|
218
|
+
}
|
|
192
219
|
const results = {};
|
|
193
220
|
const groups = new Map();
|
|
194
221
|
const executor = options.executor ?? execute;
|
|
@@ -334,8 +361,9 @@ export function createCanonicalCommandHandlers(projectRoot = process.cwd(), opti
|
|
|
334
361
|
return 0;
|
|
335
362
|
},
|
|
336
363
|
check: async (args, io) => {
|
|
337
|
-
|
|
338
|
-
|
|
364
|
+
const refresh = args.length === 1 && args[0] === '--refresh';
|
|
365
|
+
if (args.length && !refresh) { io.stderr.write('Usage: specdest check [--refresh]\n'); return 2; }
|
|
366
|
+
const result = await canonicalCheck(projectRoot, source, { refresh });
|
|
339
367
|
for (const [id, check] of Object.entries(result.results)) io.stdout.write(`${check.status.toUpperCase()} ${id}\n`);
|
|
340
368
|
return result.exitCode;
|
|
341
369
|
},
|
package/src/canonical-project.js
CHANGED
|
@@ -57,12 +57,12 @@ function runtimeChecker(manifest, runtimeRoot) {
|
|
|
57
57
|
};
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
import {
|
|
60
|
+
import { projectRuleApplies } from './project-targeting.js';
|
|
61
61
|
|
|
62
62
|
export function applicableCatalogEntries(entries, stack, profile) {
|
|
63
63
|
return entries
|
|
64
64
|
.filter((entry) => entry.kind === 'rule')
|
|
65
|
-
.filter((entry) =>
|
|
65
|
+
.filter((entry) => projectRuleApplies(entry, stack, profile, entries))
|
|
66
66
|
.sort((a, b) => a.id.localeCompare(b.id));
|
|
67
67
|
}
|
|
68
68
|
|
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] Run installed rule checks; optionally refresh canonical cache first\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) {
|