@specdest/devkit 0.3.0 → 0.3.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 CHANGED
@@ -21,3 +21,7 @@ npx @specdest/devkit doctor
21
21
  The CLI recognizes the supported Expo/Vite + Convex/Supabase stacks from the target repository's `package.json`. `.specdest/installed.json` and `.specdest/state.json` remain the committed project status used by the Base rules matrix.
22
22
 
23
23
  See [`docs/operations.md`](docs/operations.md) for cache, dashboard, CI, and release behavior.
24
+
25
+ ## Canonical cache runtime
26
+
27
+ Canonical authoring lives under `modules/**`. The CLI downloads those modules into the local Devkit cache and materializes their `.agents/skills/**` target layout **inside the cache** so checker-relative imports keep working without copying canonical skills/checkers into project repositories. `catalog/**` is compatibility/test data, not a rule-authoring source.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@specdest/devkit",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "specdest": "src/cli.js"
@@ -1,6 +1,6 @@
1
1
  import { spawn } from 'node:child_process';
2
- import { readFile } from 'node:fs/promises';
3
- import { join } from 'node:path';
2
+ import { readdir, readFile, rm, rmdir } from 'node:fs/promises';
3
+ import { join, relative } from 'node:path';
4
4
 
5
5
  import { createCanonicalSource, detectProjectStack } from './canonical-source.js';
6
6
  import { applicableCatalogEntries, prepareCachedManifest } from './canonical-project.js';
@@ -25,10 +25,10 @@ function flags(args) {
25
25
  return { refresh: args.includes('--refresh'), json: args.includes('--json') };
26
26
  }
27
27
 
28
- async function cachedManifest(source, entry, canonicalCheckRoot, { offline = false } = {}) {
28
+ async function cachedManifest(source, entry, canonicalCheckRoot, checkerRuntimeRoot, { offline = false } = {}) {
29
29
  if (offline && !(await source.hasModule(entry.id, entry.version))) return null;
30
30
  const root = await source.module(entry.id, entry.version);
31
- return await prepareCachedManifest(entry, root, canonicalCheckRoot);
31
+ return await prepareCachedManifest(entry, root, canonicalCheckRoot, checkerRuntimeRoot);
32
32
  }
33
33
 
34
34
  async function canonicalContext(projectRoot, source, options = {}) {
@@ -36,13 +36,20 @@ async function canonicalContext(projectRoot, source, options = {}) {
36
36
  const stack = await detectProjectStack(projectRoot);
37
37
  const applicable = applicableCatalogEntries(catalog.entries ?? [], stack);
38
38
  const canonicalCheck = (catalog.entries ?? []).find((entry) => entry.id === 'canonical-check');
39
+ const checkerRuntime = (catalog.entries ?? []).find((entry) => entry.id === 'checker-runtime');
39
40
  let canonicalCheckRoot = null;
41
+ let checkerRuntimeRoot = null;
40
42
  if (options.withCheckers && canonicalCheck) {
41
43
  if (!options.offline || await source.hasModule(canonicalCheck.id, canonicalCheck.version)) {
42
44
  canonicalCheckRoot = await source.module(canonicalCheck.id, canonicalCheck.version);
43
45
  }
44
46
  }
45
- return { catalog, stack, applicable, canonicalCheckRoot };
47
+ if (options.withCheckers && checkerRuntime) {
48
+ if (!options.offline || await source.hasModule(checkerRuntime.id, checkerRuntime.version)) {
49
+ checkerRuntimeRoot = await source.module(checkerRuntime.id, checkerRuntime.version);
50
+ }
51
+ }
52
+ return { catalog, stack, applicable, canonicalCheckRoot, checkerRuntimeRoot };
46
53
  }
47
54
 
48
55
  export async function canonicalStatus(projectRoot, source, options = {}) {
@@ -69,40 +76,66 @@ export async function canonicalStatus(projectRoot, source, options = {}) {
69
76
  };
70
77
  }
71
78
 
79
+ async function cleanupCanonicalSkillTree(projectRoot, allowedFiles) {
80
+ const root = join(projectRoot, '.agents', 'skills');
81
+ async function walk(directory) {
82
+ let entries;
83
+ try { entries = await readdir(directory, { withFileTypes: true }); }
84
+ catch (error) { if (error?.code === 'ENOENT') return; throw error; }
85
+ for (const entry of entries) {
86
+ const path = join(directory, entry.name);
87
+ if (entry.isDirectory()) {
88
+ await walk(path);
89
+ try { await rmdir(path); } catch (error) { if (error?.code !== 'ENOTEMPTY' && error?.code !== 'ENOENT') throw error; }
90
+ continue;
91
+ }
92
+ const projectPath = relative(projectRoot, path).split('\\').join('/');
93
+ if (!allowedFiles.has(projectPath)) await rm(path, { force: true });
94
+ }
95
+ }
96
+ await walk(root);
97
+ }
98
+
72
99
  export async function canonicalSync(projectRoot, source) {
73
100
  const current = await readInstalledState(projectRoot);
74
- const { stack, applicable, canonicalCheckRoot } = await canonicalContext(projectRoot, source, { refresh: true, withCheckers: true });
101
+ const { stack, applicable, canonicalCheckRoot, checkerRuntimeRoot } = await canonicalContext(projectRoot, source, { refresh: true, withCheckers: true });
75
102
  const changed = [];
103
+ const allowedSkillFiles = new Set();
76
104
  for (const entry of applicable) {
77
- const prepared = await cachedManifest(source, entry, canonicalCheckRoot);
105
+ const prepared = await cachedManifest(source, entry, canonicalCheckRoot, checkerRuntimeRoot);
106
+ for (const copy of prepared.manifest.copies ?? []) {
107
+ const target = String(copy.target ?? copy.path ?? '');
108
+ if (target.startsWith('.agents/skills/')) allowedSkillFiles.add(target);
109
+ }
78
110
  const installed = current.installed[entry.id];
79
111
  if (!installed) {
80
112
  await installRule({ projectRoot, payloadRoot: source.cachedModulePath(entry.id, entry.version), manifest: prepared.manifest });
81
113
  changed.push({ id: entry.id, action: 'installed', version: entry.version });
82
- } else if (installed.version !== entry.version || installed.contentHash !== entry.contentHash) {
114
+ } else if (installed.version !== entry.version || installed.contentHash !== entry.contentHash || (prepared.checkerPath && installed.check?.command?.[0] !== 'specdest-cache')) {
83
115
  await syncRule({ projectRoot, payloadRoot: source.cachedModulePath(entry.id, entry.version), manifest: prepared.manifest });
84
116
  changed.push({ id: entry.id, action: 'updated', version: entry.version });
85
117
  }
86
118
  }
119
+ await cleanupCanonicalSkillTree(projectRoot, allowedSkillFiles);
87
120
  return { stack, applicable: applicable.length, changed };
88
121
  }
89
122
 
90
123
  export async function canonicalCheck(projectRoot, source) {
91
124
  const current = await readInstalledState(projectRoot);
92
- const { applicable, canonicalCheckRoot } = await canonicalContext(projectRoot, source, { offline: true, withCheckers: true });
125
+ const { applicable, canonicalCheckRoot, checkerRuntimeRoot } = await canonicalContext(projectRoot, source, { offline: false, withCheckers: true });
93
126
  const byId = new Map(applicable.map((entry) => [entry.id, entry]));
94
127
  const results = {};
95
128
  let exitCode = 0;
96
129
  for (const [id, installed] of Object.entries(current.installed).sort(([a], [b]) => a.localeCompare(b))) {
97
130
  const entry = byId.get(id);
98
131
  if (!entry || installed.kind !== 'rule') continue;
99
- if (!(await source.hasModule(id, entry.version))) {
100
- results[id] = { status: 'fail', exitCode: 1, stderr: 'Rule cache missing; run specdest sync first.' };
132
+ const prepared = await cachedManifest(source, entry, canonicalCheckRoot, checkerRuntimeRoot);
133
+ if (!prepared?.checkerPath) {
134
+ if ((entry.enforcement ?? 'guidance') === 'guidance') continue;
135
+ results[id] = { status: 'fail', exitCode: 1, stderr: 'Canonical checker unavailable for enforced installed rule.' };
101
136
  exitCode = 1;
102
137
  continue;
103
138
  }
104
- const prepared = await cachedManifest(source, entry, canonicalCheckRoot, { offline: true });
105
- if (!prepared?.checkerPath) continue;
106
139
  const execution = await execute('node', [prepared.checkerPath], projectRoot);
107
140
  results[id] = { status: execution.exitCode === 0 ? 'pass' : 'fail', ...execution };
108
141
  if (execution.exitCode !== 0) exitCode = 1;
@@ -1,9 +1,56 @@
1
- import { readFile } from 'node:fs/promises';
2
- import { join } from 'node:path';
1
+ import { cp, mkdir, readFile, rm } from 'node:fs/promises';
2
+ import { basename, dirname, join } from 'node:path';
3
3
 
4
- function keepCopy(copy) {
5
- const target = String(copy?.target ?? copy?.path ?? '');
6
- return !/(^|\/)SKILL\.md$|(^|\/)check\.ts$|(^|\/)runtime\.ts$/u.test(target);
4
+ function isSkillTarget(copy) {
5
+ return String(copy?.target ?? copy?.path ?? '').startsWith('.agents/skills/');
6
+ }
7
+
8
+ function isProjectScaffold(copy) {
9
+ return copy?.mode === 'scaffold' || copy?.mode === 'create-only';
10
+ }
11
+
12
+ function keepProjectCopy(copy) {
13
+ return !isSkillTarget(copy) || isProjectScaffold(copy);
14
+ }
15
+
16
+ async function materializeCopies(manifest, sourceRoot, runtimeRoot) {
17
+ for (const copy of manifest?.copies ?? []) {
18
+ if (!isSkillTarget(copy) || isProjectScaffold(copy)) continue;
19
+ const source = join(sourceRoot, String(copy.source ?? ''));
20
+ const target = join(runtimeRoot, String(copy.target ?? copy.path ?? ''));
21
+ await mkdir(dirname(target), { recursive: true });
22
+ await cp(source, target, { recursive: copy.type === 'directory' || copy.directory === true, force: true });
23
+ }
24
+ }
25
+
26
+ async function readManifest(root) {
27
+ return JSON.parse(await readFile(join(root, 'manifest.json'), 'utf8'));
28
+ }
29
+
30
+ async function cacheProjectCopies(copies, sourceRoot, ruleRoot, label) {
31
+ const result = [];
32
+ for (const copy of copies.filter(keepProjectCopy)) {
33
+ if (sourceRoot === ruleRoot) {
34
+ result.push(copy);
35
+ continue;
36
+ }
37
+ const source = String(copy.source ?? '');
38
+ const cachedSource = join('.project-scaffolds', label, source);
39
+ const target = join(ruleRoot, cachedSource);
40
+ await mkdir(dirname(target), { recursive: true });
41
+ await cp(join(sourceRoot, source), target, {
42
+ recursive: copy.type === 'directory' || copy.directory === true,
43
+ force: true,
44
+ });
45
+ result.push({ ...copy, source: cachedSource });
46
+ }
47
+ return result;
48
+ }
49
+
50
+ function runtimeCheckerPath(manifest, runtimeRoot) {
51
+ if (manifest?.check?.command?.[0] !== 'node') return null;
52
+ const target = String(manifest.check.command[1] ?? '');
53
+ return target.startsWith('.agents/skills/') ? join(runtimeRoot, target) : null;
7
54
  }
8
55
 
9
56
  export function applicableCatalogEntries(entries, stack) {
@@ -13,36 +60,43 @@ export function applicableCatalogEntries(entries, stack) {
13
60
  .sort((a, b) => a.id.localeCompare(b.id));
14
61
  }
15
62
 
16
- export async function prepareCachedManifest(entry, ruleRoot, canonicalCheckRoot) {
17
- const raw = JSON.parse(await readFile(join(ruleRoot, 'manifest.json'), 'utf8'));
18
- const copies = (raw.copies ?? []).filter(keepCopy);
19
- const markdownSections = (raw.markdownSections ?? []).filter(
20
- (section) => !String(section?.body ?? section?.content ?? '').includes('.agents/skills/'),
21
- );
22
- let checkerPath = null;
23
- if (raw.check?.command?.[0] === 'node') {
24
- const local = String(raw.check.command[1] ?? '');
25
- const basename = local.split('/').at(-1);
26
- if (basename === 'check.ts') {
27
- try {
28
- checkerPath = join(ruleRoot, 'files', 'check.ts');
29
- await readFile(checkerPath, 'utf8');
30
- } catch {
31
- checkerPath = null;
32
- }
33
- }
63
+ export async function prepareCachedManifest(entry, ruleRoot, canonicalCheckRoot, checkerRuntimeRoot = null) {
64
+ const raw = await readManifest(ruleRoot);
65
+ const runtimeRoot = join(ruleRoot, '.runtime');
66
+ await rm(runtimeRoot, { recursive: true, force: true });
67
+ await mkdir(runtimeRoot, { recursive: true });
68
+
69
+ if (checkerRuntimeRoot) {
70
+ await materializeCopies(await readManifest(checkerRuntimeRoot), checkerRuntimeRoot, runtimeRoot);
34
71
  }
72
+ await materializeCopies(raw, ruleRoot, runtimeRoot);
73
+
74
+ const copies = await cacheProjectCopies(raw.copies ?? [], ruleRoot, ruleRoot, entry.id);
75
+ let checkerPath = runtimeCheckerPath(raw, runtimeRoot);
35
76
  if (!checkerPath && raw.checker?.path && canonicalCheckRoot) {
36
77
  const relative = String(raw.checker.path).replace(/^canonical-check\//u, '');
37
- checkerPath = join(canonicalCheckRoot, relative);
78
+ const implementationRoot = dirname(join(canonicalCheckRoot, relative));
79
+ const implementationManifest = await readManifest(implementationRoot);
80
+ await materializeCopies(implementationManifest, implementationRoot, runtimeRoot);
81
+ copies.push(...await cacheProjectCopies(
82
+ implementationManifest.copies ?? [],
83
+ implementationRoot,
84
+ ruleRoot,
85
+ basename(implementationRoot),
86
+ ));
87
+ checkerPath = runtimeCheckerPath(implementationManifest, runtimeRoot);
38
88
  }
89
+
90
+ const markdownSections = (raw.markdownSections ?? []).filter(
91
+ (section) => !String(section?.body ?? section?.content ?? '').includes('.agents/skills/'),
92
+ );
39
93
  const manifest = {
40
94
  ...raw,
41
95
  kind: raw.kind ?? entry.kind ?? 'rule',
42
96
  version: entry.version ?? raw.version,
43
97
  ...(entry.contentHash ? { contentHash: entry.contentHash } : {}),
44
98
  ...(entry.revision ? { revision: entry.revision } : {}),
45
- copies,
99
+ copies: [...new Map(copies.map((copy) => [String(copy.target ?? copy.path ?? copy.source), copy])).values()],
46
100
  markdownSections,
47
101
  ...(checkerPath ? { check: { command: ['specdest-cache', entry.id] } } : {}),
48
102
  };
@@ -8,7 +8,7 @@ import { removeMarkdownSection, resolveProjectPath, upsertMarkdownSection } from
8
8
  const INSTALLED_PATH = '.specdest/installed.json';
9
9
  const STATE_PATH = '.specdest/state.json';
10
10
  const SCRIPT_PHASES = new Set(['pre_install', 'post_install', 'pre_sync', 'post_sync', 'pre_remove', 'post_remove']);
11
- const OP_TYPES = new Set(['copy_file', 'copy_directory', 'managed_section', 'json_set', 'json_merge', 'yaml_set', 'yaml_merge', 'env_set', 'env_remove', 'delete_owned_artifact', 'run_custom_script', 'run_script_file']);
11
+ const OP_TYPES = new Set(['copy_file', 'scaffold_file', 'copy_directory', 'managed_section', 'json_set', 'json_merge', 'yaml_set', 'yaml_merge', 'env_set', 'env_remove', 'delete_owned_artifact', 'run_custom_script', 'run_script_file']);
12
12
  const clone = (value) => value === undefined ? undefined : JSON.parse(JSON.stringify(value));
13
13
  const hash = (value) => createHash('sha256').update(typeof value === 'string' ? value : JSON.stringify(value)).digest('hex');
14
14
  const hasOwn = (object, key) => Object.prototype.hasOwnProperty.call(object, key);
@@ -77,7 +77,7 @@ function normalizeManifest(manifest) {
77
77
  : undefined);
78
78
  for (const copy of manifest?.copies ?? []) {
79
79
  operations.push({
80
- type: copy.type === 'directory' || copy.directory === true ? 'copy_directory' : 'copy_file',
80
+ type: copy.type === 'directory' || copy.directory === true ? 'copy_directory' : copy.mode === 'scaffold' || copy.mode === 'create-only' ? 'scaffold_file' : 'copy_file',
81
81
  source: copy.source,
82
82
  path: copy.target ?? copy.path,
83
83
  mode: copy.mode,
@@ -324,6 +324,22 @@ async function claimFile(context, sourcePath, destination, touched) {
324
324
  async function applyOperation(op, context, touched) {
325
325
  const { projectRoot, payloadRoot } = context;
326
326
  if (op.type === 'copy_file') return claimFile(context, payloadPath(payloadRoot, op.source), op.path, touched);
327
+ if (op.type === 'scaffold_file') {
328
+ const target = resolveProjectPath(projectRoot, op.path);
329
+ const key = ownershipKey('file', op.path);
330
+ const prior = context.prior[key];
331
+ if (prior) {
332
+ // Reclassifying a formerly managed file as a scaffold intentionally hands
333
+ // ownership to the project. Preserve whatever the project currently has.
334
+ delete context.next[key];
335
+ touched.add(key);
336
+ return;
337
+ }
338
+ if (!(await exists(target))) {
339
+ await writeText(projectRoot, op.path, await readFile(payloadPath(payloadRoot, op.source), 'utf8'));
340
+ }
341
+ return;
342
+ }
327
343
  if (op.type === 'copy_directory') {
328
344
  const sourceRoot = payloadPath(payloadRoot, op.source);
329
345
  const walk = async (dir, prefix = '') => {