@saydone/dab 0.1.1 → 0.1.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.
Files changed (118) hide show
  1. package/AGENTS.md +7 -0
  2. package/CHANGELOG.md +26 -1
  3. package/CHANGELOG.zh.md +26 -1
  4. package/README.i18n.yaml +8 -8
  5. package/README.md +16 -6
  6. package/README.zh.md +16 -6
  7. package/dist/adapter/claude-code/adapter.js +5 -2
  8. package/dist/adapter/claude-code/adapter.js.map +1 -1
  9. package/dist/adapter/claude-code/adoption.d.ts +58 -0
  10. package/dist/adapter/claude-code/adoption.js +749 -0
  11. package/dist/adapter/claude-code/adoption.js.map +1 -0
  12. package/dist/adapter/claude-code/policy-compile.js +1 -1
  13. package/dist/adapter/claude-code/policy-compile.js.map +1 -1
  14. package/dist/adapter/claude-code/projection.d.ts +1 -0
  15. package/dist/adapter/claude-code/projection.js +7 -1
  16. package/dist/adapter/claude-code/projection.js.map +1 -1
  17. package/dist/adapter/types.d.ts +7 -0
  18. package/dist/cli.js +9 -19
  19. package/dist/cli.js.map +1 -1
  20. package/dist/commands/detect.d.ts +14 -0
  21. package/dist/commands/detect.js +383 -54
  22. package/dist/commands/detect.js.map +1 -1
  23. package/dist/commands/init.d.ts +4 -1
  24. package/dist/commands/init.js +177 -40
  25. package/dist/commands/init.js.map +1 -1
  26. package/dist/commands/normalize.d.ts +53 -0
  27. package/dist/commands/normalize.js +378 -29
  28. package/dist/commands/normalize.js.map +1 -1
  29. package/dist/commands/policy-explain.js +9 -0
  30. package/dist/commands/policy-explain.js.map +1 -1
  31. package/dist/core/diagnostics.d.ts +2 -0
  32. package/dist/core/diagnostics.js +18 -0
  33. package/dist/core/diagnostics.js.map +1 -1
  34. package/dist/core/fs-atomic.d.ts +6 -0
  35. package/dist/core/fs-atomic.js +29 -0
  36. package/dist/core/fs-atomic.js.map +1 -1
  37. package/dist/core/paths.js +29 -6
  38. package/dist/core/paths.js.map +1 -1
  39. package/dist/core/practice-paths.d.ts +73 -0
  40. package/dist/core/practice-paths.js +310 -0
  41. package/dist/core/practice-paths.js.map +1 -0
  42. package/dist/core/skill-marker.d.ts +28 -0
  43. package/dist/core/skill-marker.js +57 -0
  44. package/dist/core/skill-marker.js.map +1 -0
  45. package/dist/core/version-compare.d.ts +8 -0
  46. package/dist/core/version-compare.js +23 -0
  47. package/dist/core/version-compare.js.map +1 -0
  48. package/dist/core/version.d.ts +2 -0
  49. package/dist/core/version.js +13 -0
  50. package/dist/core/version.js.map +1 -0
  51. package/dist/detect/checklist.d.ts +42 -0
  52. package/dist/detect/checklist.js +137 -0
  53. package/dist/detect/checklist.js.map +1 -0
  54. package/dist/detect/checks.d.ts +1 -1
  55. package/dist/detect/checks.js +15 -3
  56. package/dist/detect/checks.js.map +1 -1
  57. package/dist/detect/practice.d.ts +43 -0
  58. package/dist/detect/practice.js +782 -0
  59. package/dist/detect/practice.js.map +1 -0
  60. package/dist/detect/preflight.d.ts +21 -0
  61. package/dist/detect/preflight.js +408 -0
  62. package/dist/detect/preflight.js.map +1 -0
  63. package/dist/docs/config.js +5 -0
  64. package/dist/docs/config.js.map +1 -1
  65. package/dist/init/adoption.d.ts +138 -0
  66. package/dist/init/adoption.js +747 -0
  67. package/dist/init/adoption.js.map +1 -0
  68. package/dist/init/docs-language.d.ts +12 -0
  69. package/dist/init/docs-language.js +110 -0
  70. package/dist/init/docs-language.js.map +1 -0
  71. package/dist/init/managed-skills.d.ts +81 -0
  72. package/dist/init/managed-skills.js +407 -0
  73. package/dist/init/managed-skills.js.map +1 -0
  74. package/dist/init/minimal-source.d.ts +14 -0
  75. package/dist/init/minimal-source.js +42 -0
  76. package/dist/init/minimal-source.js.map +1 -0
  77. package/dist/init/practice-authoring.d.ts +5 -0
  78. package/dist/init/practice-authoring.js +36 -0
  79. package/dist/init/practice-authoring.js.map +1 -0
  80. package/dist/init/practice-docs.d.ts +13 -0
  81. package/dist/init/practice-docs.js +278 -0
  82. package/dist/init/practice-docs.js.map +1 -0
  83. package/dist/init/practice.d.ts +78 -0
  84. package/dist/init/practice.js +528 -0
  85. package/dist/init/practice.js.map +1 -0
  86. package/dist/init/scanner.js +7 -0
  87. package/dist/init/scanner.js.map +1 -1
  88. package/dist/init/skill-templates.d.ts +7 -0
  89. package/dist/init/skill-templates.js +25 -0
  90. package/dist/init/skill-templates.js.map +1 -0
  91. package/dist/init/templates.d.ts +22 -10
  92. package/dist/init/templates.js +32 -88
  93. package/dist/init/templates.js.map +1 -1
  94. package/dist/model/agents-md.js +1 -1
  95. package/dist/model/agents-md.js.map +1 -1
  96. package/dist/model/agents.d.ts +10 -0
  97. package/dist/model/agents.js +110 -86
  98. package/dist/model/agents.js.map +1 -1
  99. package/dist/model/diagnostics-codes.d.ts +1 -0
  100. package/dist/model/diagnostics-codes.js +1 -0
  101. package/dist/model/diagnostics-codes.js.map +1 -1
  102. package/dist/model/facts.js +24 -4
  103. package/dist/model/facts.js.map +1 -1
  104. package/dist/model/project-manifest.js +2 -0
  105. package/dist/model/project-manifest.js.map +1 -1
  106. package/dist/model/schemas.d.ts +30 -1
  107. package/dist/model/schemas.js +8 -1
  108. package/dist/model/schemas.js.map +1 -1
  109. package/dist/model/scope.d.ts +13 -0
  110. package/dist/model/scope.js +57 -0
  111. package/dist/model/scope.js.map +1 -1
  112. package/dist/model/skills.d.ts +10 -0
  113. package/dist/model/skills.js +126 -56
  114. package/dist/model/skills.js.map +1 -1
  115. package/package.json +2 -1
  116. package/skills/dab-architecture-writing/SKILL.md +58 -0
  117. package/skills/dab-decision-notes/SKILL.md +57 -0
  118. package/skills/dab-doc-writing/SKILL.md +58 -0
@@ -0,0 +1,782 @@
1
+ import { dirname, join, resolve } from 'node:path';
2
+ import { readTextFile } from '../core/fs-atomic.js';
3
+ import { isPathInside } from '../core/paths.js';
4
+ import { scanMarkdown, slugifyHeading } from '../docs/markdown.js';
5
+ import { resolveDocsLanguage, } from '../init/docs-language.js';
6
+ import { AUTHORING_SKILL_NAMES, authoringPack } from '../init/practice-authoring.js';
7
+ import { documentationPack } from '../init/practice-docs.js';
8
+ import { MANAGED_SKILL_DIAGNOSTIC_CODES, planManagedSkills, } from '../init/managed-skills.js';
9
+ import { planAuthoredScaffold, } from '../init/practice.js';
10
+ import { authoredDirectoryState, authoredFileProblem, practiceParentProblems, probeLstat, problemMessage, } from '../core/practice-paths.js';
11
+ import { scanProject } from '../init/scanner.js';
12
+ import { validateSkillSource } from '../model/skills.js';
13
+ /** Documentation files owned by the documentation pack. */
14
+ export const PRACTICE_DOC_FILES = ['docs/AGENTS.md', 'docs/architecture.md'];
15
+ const ARCHITECTURE_FILE = 'docs/architecture.md';
16
+ const PRACTICE_SKILL_ROOT = '.agents/skills';
17
+ const PRACTICE_SKILL_PREFIX = `${PRACTICE_SKILL_ROOT}/`;
18
+ const ARCHITECTURE_SKILL = 'dab-architecture-writing';
19
+ const DOC_SKILL = 'dab-doc-writing';
20
+ const NORMALIZE_NEXT = 'dab normalize';
21
+ const ARCHITECTURE_NEXT = `use ${ARCHITECTURE_SKILL}`;
22
+ const DOC_NEXT = `use ${DOC_SKILL}`;
23
+ const POLICY_NEXT = 'delete .agents/rules/docs-bilingual.md, then run dab normalize';
24
+ const MANUAL_PATH_NEXT = 'resolve the path conflict manually';
25
+ const LINK_NEXT = 'fix the link';
26
+ /**
27
+ * Practice warnings that describe project policy or preserved user content.
28
+ * They stay informational under `--strict`; every other practice warning fails.
29
+ */
30
+ export const PRACTICE_NON_STRICT_WARNING_CODES = new Set([
31
+ 'practice.docs-bilingual-policy',
32
+ MANAGED_SKILL_DIAGNOSTIC_CODES.modified,
33
+ MANAGED_SKILL_DIAGNOSTIC_CODES.legacy,
34
+ MANAGED_SKILL_DIAGNOSTIC_CODES.newer,
35
+ MANAGED_SKILL_DIAGNOSTIC_CODES.mismatch,
36
+ MANAGED_SKILL_DIAGNOSTIC_CODES.unversioned,
37
+ ]);
38
+ const TODO_MARKER = /^[ \t]*TODO(?:\(owner\))?:/;
39
+ const FENCE_OPEN = /^ {0,3}(`{3,}|~{3,})(.*)$/;
40
+ const HEADING_LINE = /^ {0,3}#{1,6}\s/;
41
+ const HTML_COMMENT = /<!--[\s\S]*?-->/g;
42
+ const ARCHITECTURE_SECTIONS = [
43
+ ['System purpose', '系统目的'],
44
+ ['Composition', '组成'],
45
+ ['Runtime flow', '运行时流程'],
46
+ ['Module boundaries', '模块边界'],
47
+ ['Extension points', '扩展点'],
48
+ ['Invariants', '不变量'],
49
+ ];
50
+ function isDocsPath(path) {
51
+ return PRACTICE_DOC_FILES.includes(path);
52
+ }
53
+ function isAuthoringSkillPath(path) {
54
+ return path.startsWith(PRACTICE_SKILL_PREFIX)
55
+ && path.slice(PRACTICE_SKILL_PREFIX.length).startsWith('dab-');
56
+ }
57
+ export function isAuthoringSkillName(name) {
58
+ return AUTHORING_SKILL_NAMES.includes(name);
59
+ }
60
+ function skillDirectory(name) {
61
+ return `${PRACTICE_SKILL_ROOT}/${name}`;
62
+ }
63
+ function skillFile(name) {
64
+ return `${skillDirectory(name)}/SKILL.md`;
65
+ }
66
+ function skillNameFromPath(path) {
67
+ const [name = path] = path.slice(PRACTICE_SKILL_PREFIX.length).split('/');
68
+ return name;
69
+ }
70
+ function languageDetail(language) {
71
+ return `Language: ${language.value} (source: ${language.source})`;
72
+ }
73
+ function docsDetail(language, extra) {
74
+ return extra === undefined
75
+ ? languageDetail(language)
76
+ : `${languageDetail(language)}; ${extra}`;
77
+ }
78
+ /**
79
+ * Run the shared authored planner with only the practice packs. The planner is
80
+ * read-only; detect never plans core scaffolding or Claude target output.
81
+ */
82
+ async function planPractice(input, packs) {
83
+ return planAuthoredScaffold({
84
+ root: input.root,
85
+ scope: input.scope,
86
+ scan: input.scan,
87
+ profile: input.manifest?.profile ?? 'generic',
88
+ mode: input.mode,
89
+ version: input.version,
90
+ env: process.env,
91
+ ...(input.manifest === undefined ? {} : { manifest: input.manifest }),
92
+ }, packs);
93
+ }
94
+ function practiceLanguage(root, manifest) {
95
+ return resolveDocsLanguage({
96
+ root,
97
+ configured: manifest?.docs.language ?? 'auto',
98
+ env: process.env,
99
+ });
100
+ }
101
+ function addParentProblem(collector, language, group, problem) {
102
+ const docs = group === 'docs';
103
+ collector.addDiagnostic({
104
+ code: docs ? 'practice.docs-path' : 'practice.skill-path',
105
+ severity: 'error',
106
+ message: problem.message,
107
+ file: problem.path,
108
+ hint: 'Resolve the path conflict manually; DAB never writes through it.',
109
+ });
110
+ collector.addEntry({
111
+ group: docs ? 'Documentation' : 'Skills',
112
+ label: problem.path,
113
+ status: 'error',
114
+ detail: docs ? docsDetail(language, problem.message) : problem.message,
115
+ next: MANUAL_PATH_NEXT,
116
+ });
117
+ }
118
+ class PracticeCollector {
119
+ entries = [];
120
+ diagnostics = [];
121
+ seen = new Set();
122
+ addDiagnostic(diagnostic) {
123
+ this.diagnostics.push(diagnostic);
124
+ }
125
+ addEntry(entry) {
126
+ const key = [
127
+ entry.group,
128
+ entry.label,
129
+ entry.status,
130
+ entry.detail ?? '',
131
+ entry.next ?? '',
132
+ ].join('\0');
133
+ if (this.seen.has(key)) {
134
+ return;
135
+ }
136
+ this.seen.add(key);
137
+ this.entries.push(entry);
138
+ }
139
+ }
140
+ function conflictCode(path) {
141
+ if (isDocsPath(path)) {
142
+ return 'practice.docs-path';
143
+ }
144
+ if (isAuthoringSkillPath(path)) {
145
+ return 'practice.skill-path';
146
+ }
147
+ return undefined;
148
+ }
149
+ /**
150
+ * The planner reports declared practice path conflicts as `path.conflict`.
151
+ * Detect maps only pack-owned paths into the practice path codes and leaves
152
+ * unrelated diagnostics untouched.
153
+ */
154
+ function reportPlanConflicts(plan, collector) {
155
+ const conflicted = new Set();
156
+ for (const item of plan.diagnostics) {
157
+ const file = item.file;
158
+ const code = file === undefined ? undefined : conflictCode(file);
159
+ if (file === undefined || code === undefined) {
160
+ collector.addDiagnostic(item);
161
+ continue;
162
+ }
163
+ conflicted.add(file);
164
+ const diagnostic = {
165
+ code,
166
+ severity: 'error',
167
+ message: item.message,
168
+ file,
169
+ ...(item.line === undefined ? {} : { line: item.line }),
170
+ hint: 'Resolve the path conflict manually; DAB never writes through it.',
171
+ };
172
+ collector.addDiagnostic(diagnostic);
173
+ collector.addEntry({
174
+ group: code === 'practice.docs-path' ? 'Documentation' : 'Skills',
175
+ label: code === 'practice.docs-path' ? file : skillNameFromPath(file),
176
+ status: 'error',
177
+ detail: code === 'practice.docs-path'
178
+ ? docsDetail(plan.docsLanguage, item.message)
179
+ : item.message,
180
+ next: MANUAL_PATH_NEXT,
181
+ });
182
+ }
183
+ return conflicted;
184
+ }
185
+ async function skillFileState(root, name, directory) {
186
+ const file = skillFile(name);
187
+ const absolute = join(root, file);
188
+ const problem = await authoredFileProblem(root, file);
189
+ if (problem !== undefined) {
190
+ return { kind: 'conflict', message: problem };
191
+ }
192
+ if ((await probeLstat(absolute)).info === null) {
193
+ return { kind: 'missing' };
194
+ }
195
+ // Existing skill discovery owns invalid frontmatter and unknown fields. The
196
+ // practice check must not add a second entry for the same broken skill.
197
+ const validated = await validateSkillSource(root, directory);
198
+ if (validated.diagnostics.some((item) => item.severity === 'error')) {
199
+ return { kind: 'invalid', diagnostics: validated.diagnostics };
200
+ }
201
+ return { kind: 'present' };
202
+ }
203
+ function stripInlineCode(line) {
204
+ return line.replace(/(`+)(.*?)\1/g, (full) => ' '.repeat(full.length));
205
+ }
206
+ /**
207
+ * Finds line-start `TODO:` or `TODO(owner):` markers outside fenced code and
208
+ * inline code. Mid-line layout notes in generated docs are not markers.
209
+ */
210
+ export function todoMarkerLines(text) {
211
+ const lines = text.replace(/\r\n?/g, '\n').split('\n');
212
+ const markers = [];
213
+ let fence;
214
+ for (let index = 0; index < lines.length; index += 1) {
215
+ const line = lines[index] ?? '';
216
+ const opening = FENCE_OPEN.exec(line);
217
+ if (opening !== null) {
218
+ const marker = opening[1] ?? '';
219
+ if (fence === undefined) {
220
+ fence = { marker: marker[0] ?? '`', length: marker.length };
221
+ }
222
+ else if ((marker[0] ?? '') === fence.marker
223
+ && marker.length >= fence.length
224
+ && (opening[2] ?? '').trim().length === 0) {
225
+ fence = undefined;
226
+ }
227
+ continue;
228
+ }
229
+ if (fence !== undefined) {
230
+ continue;
231
+ }
232
+ if (TODO_MARKER.test(stripInlineCode(line))) {
233
+ markers.push(index + 1);
234
+ }
235
+ }
236
+ return markers;
237
+ }
238
+ function headingExists(text, anchor) {
239
+ const slug = slugifyHeading(anchor);
240
+ return scanMarkdown(text).headings.some((heading) => slugifyHeading(heading.text) === slug);
241
+ }
242
+ async function linkDiagnostics(root, file, text) {
243
+ const diagnostics = [];
244
+ const absolute = join(root, file);
245
+ const linkError = (line, message) => ({
246
+ code: 'practice.docs-link',
247
+ severity: 'error',
248
+ message,
249
+ file,
250
+ line,
251
+ hint: 'Fix the link target; detect only scans the documentation pack files.',
252
+ });
253
+ for (const link of scanMarkdown(text).links) {
254
+ if (link.external) {
255
+ continue;
256
+ }
257
+ if (link.anchorOnly) {
258
+ if (!headingExists(text, link.target.slice(1))) {
259
+ diagnostics.push(linkError(link.line, `same-file anchor does not exist: ${link.target}`));
260
+ }
261
+ continue;
262
+ }
263
+ const [base = '', ...anchorParts] = link.target.split('#');
264
+ const anchor = anchorParts.join('#');
265
+ if (base.startsWith('/') || base.startsWith('~')) {
266
+ diagnostics.push(linkError(link.line, `absolute links are not allowed: ${link.target}`));
267
+ continue;
268
+ }
269
+ const target = resolve(dirname(absolute), base);
270
+ if (!isPathInside(root, target)) {
271
+ diagnostics.push(linkError(link.line, `link escapes the repository: ${link.target}`));
272
+ continue;
273
+ }
274
+ // A path whose parent is a regular file raises ENOTDIR rather than ENOENT.
275
+ // It is a broken link, not an internal error.
276
+ const targetState = await probeLstat(target);
277
+ if (targetState.problem !== undefined) {
278
+ diagnostics.push(linkError(link.line, `link target cannot be resolved (${targetState.problem}): ${link.target}`));
279
+ continue;
280
+ }
281
+ if (targetState.info === null) {
282
+ diagnostics.push(linkError(link.line, `link target does not exist: ${link.target}`));
283
+ continue;
284
+ }
285
+ if (anchor.length === 0 || !target.endsWith('.md')) {
286
+ continue;
287
+ }
288
+ try {
289
+ if (!headingExists(await readTextFile(target), anchor)) {
290
+ diagnostics.push(linkError(link.line, `link anchor does not exist: ${link.target}`));
291
+ }
292
+ }
293
+ catch {
294
+ // An unreadable target is already reported by the missing/unreadable file.
295
+ }
296
+ }
297
+ return diagnostics;
298
+ }
299
+ /**
300
+ * Fixed architecture sections that carry project evidence. Project facts and
301
+ * Verification are excluded because the skeleton or command list owns them.
302
+ */
303
+ function emptyArchitectureSections(text) {
304
+ const normalized = text.replace(/\r\n?/g, '\n');
305
+ const lines = normalized.split('\n');
306
+ const headings = scanMarkdown(normalized).headings;
307
+ const empty = [];
308
+ headings.forEach((heading, index) => {
309
+ const fixed = ARCHITECTURE_SECTIONS.find(([english, chinese]) => heading.text === english || heading.text === chinese);
310
+ if (fixed === undefined) {
311
+ return;
312
+ }
313
+ const next = headings.slice(index + 1).find((item) => item.level <= heading.level);
314
+ const end = next === undefined ? lines.length : next.line - 1;
315
+ const body = lines
316
+ .slice(heading.line, end)
317
+ .join('\n')
318
+ .replace(HTML_COMMENT, '');
319
+ const meaningful = body
320
+ .split('\n')
321
+ .filter((line) => line.trim().length > 0 && !HEADING_LINE.test(line));
322
+ if (meaningful.length === 0) {
323
+ empty.push({ section: heading.text, line: heading.line });
324
+ }
325
+ });
326
+ return empty;
327
+ }
328
+ function addDocsMissing(collector, language, file, next, reportDiagnostic = true) {
329
+ if (reportDiagnostic) {
330
+ collector.addDiagnostic({
331
+ code: 'practice.docs-missing',
332
+ severity: 'warning',
333
+ message: `documentation file is missing: ${file}`,
334
+ file,
335
+ hint: 'Run dab normalize to create the missing documentation skeleton.',
336
+ });
337
+ }
338
+ collector.addEntry({
339
+ group: 'Documentation',
340
+ label: file,
341
+ status: 'missing',
342
+ detail: languageDetail(language),
343
+ ...(next === undefined ? {} : { next }),
344
+ });
345
+ }
346
+ function addDocsPath(collector, language, file, message, next) {
347
+ collector.addDiagnostic({
348
+ code: 'practice.docs-path',
349
+ severity: 'error',
350
+ message,
351
+ file,
352
+ hint: 'Resolve the path conflict manually; DAB never writes through it.',
353
+ });
354
+ collector.addEntry({
355
+ group: 'Documentation',
356
+ label: file,
357
+ status: 'error',
358
+ detail: docsDetail(language, message),
359
+ ...(next === undefined ? {} : { next }),
360
+ });
361
+ }
362
+ function addPolicySkip(collector, language, file, next) {
363
+ collector.addDiagnostic({
364
+ code: 'practice.docs-bilingual-policy',
365
+ severity: 'warning',
366
+ message: `project bilingual rule skips single-language documentation: ${file}`,
367
+ file,
368
+ hint: 'Remove .agents/rules/docs-bilingual.md to let DAB scaffold single-language docs.',
369
+ });
370
+ collector.addEntry({
371
+ group: 'Documentation',
372
+ label: file,
373
+ status: 'warn',
374
+ detail: docsDetail(language, 'skipped by .agents/rules/docs-bilingual.md'),
375
+ ...(next === undefined ? {} : { next }),
376
+ });
377
+ }
378
+ function addSkillMissing(collector, name, next, reportDiagnostic = true) {
379
+ if (reportDiagnostic) {
380
+ collector.addDiagnostic({
381
+ code: 'practice.skill-missing',
382
+ severity: 'warning',
383
+ message: `authoring skill is missing: ${name}`,
384
+ file: skillFile(name),
385
+ hint: 'Run dab normalize to create the missing authoring skill.',
386
+ });
387
+ }
388
+ collector.addEntry({
389
+ group: 'Skills',
390
+ label: name,
391
+ status: 'missing',
392
+ ...(next === undefined ? {} : { next }),
393
+ });
394
+ }
395
+ function addSkillPath(collector, name, file, message) {
396
+ collector.addDiagnostic({
397
+ code: 'practice.skill-path',
398
+ severity: 'error',
399
+ message,
400
+ file,
401
+ hint: 'Resolve the path conflict manually; DAB never writes through it.',
402
+ });
403
+ collector.addEntry({
404
+ group: 'Skills',
405
+ label: name,
406
+ status: 'error',
407
+ detail: message,
408
+ next: MANUAL_PATH_NEXT,
409
+ });
410
+ }
411
+ function addSkillCurrent(collector, name, version) {
412
+ collector.addEntry({
413
+ group: 'Skills',
414
+ label: name,
415
+ status: 'ok',
416
+ detail: `version ${version}`,
417
+ });
418
+ }
419
+ function addSkillWarning(collector, name, detail, next) {
420
+ collector.addEntry({
421
+ group: 'Skills',
422
+ label: name,
423
+ status: 'warn',
424
+ detail,
425
+ ...(next === undefined ? {} : { next }),
426
+ });
427
+ }
428
+ function addSkillStale(collector, name, markerVersion, currentVersion) {
429
+ collector.addDiagnostic({
430
+ code: 'practice.skill-stale',
431
+ severity: 'warning',
432
+ message: `authoring skill version ${markerVersion} is older than ${currentVersion}`,
433
+ file: skillFile(name),
434
+ hint: 'Run dab normalize to update the unmodified managed skill; DAB never overwrites modified skills.',
435
+ });
436
+ collector.addEntry({
437
+ group: 'Skills',
438
+ label: name,
439
+ status: 'warn',
440
+ detail: `version ${markerVersion} is older than ${currentVersion}`,
441
+ next: NORMALIZE_NEXT,
442
+ });
443
+ }
444
+ function managedDiagnostic(diagnostics, code, file) {
445
+ return diagnostics.find((item) => item.code === code && item.file === file);
446
+ }
447
+ /**
448
+ * Map the shared managed-skill state onto detect. Diagnostics for preserved
449
+ * states come from `planManagedSkills`, so detect and normalize agree on the
450
+ * hash/version algorithm; only the strict stale state has no plan diagnostic.
451
+ */
452
+ function addManagedSkillState(collector, state, version, diagnostics) {
453
+ switch (state.status) {
454
+ case 'current':
455
+ addSkillCurrent(collector, state.name, state.version ?? version);
456
+ return;
457
+ case 'stale':
458
+ addSkillStale(collector, state.name, state.version ?? 'unknown', version);
459
+ return;
460
+ case 'newer':
461
+ addSkillWarning(collector, state.name, `version ${state.version ?? 'unknown'} is newer or cannot be compared; preserved`);
462
+ return;
463
+ case 'modified':
464
+ addSkillWarning(collector, state.name, 'content differs from the marker hash; preserved');
465
+ return;
466
+ case 'legacy': {
467
+ const legacy = managedDiagnostic(diagnostics, MANAGED_SKILL_DIAGNOSTIC_CODES.legacy, state.file);
468
+ // An info diagnostic means the body equals the current template, so
469
+ // normalize will migrate it. That is still a warn row with no auto-next.
470
+ addSkillWarning(collector, state.name, legacy?.severity === 'info'
471
+ ? `legacy marker version ${state.version ?? 'unknown'} will be migrated by dab normalize`
472
+ : `legacy marker version ${state.version ?? 'unknown'} has no hash; preserved`);
473
+ return;
474
+ }
475
+ case 'unversioned': {
476
+ const mismatch = managedDiagnostic(diagnostics, MANAGED_SKILL_DIAGNOSTIC_CODES.mismatch, state.file);
477
+ addSkillWarning(collector, state.name, mismatch === undefined ? 'no dab-skill marker; preserved' : mismatch.message);
478
+ return;
479
+ }
480
+ case 'missing':
481
+ // Existing skill discovery reports a directory without SKILL.md.
482
+ return;
483
+ }
484
+ }
485
+ function addSkillFileProblem(collector, name, message) {
486
+ addSkillPath(collector, name, skillFile(name), message);
487
+ }
488
+ /**
489
+ * Read-only practice checks for an initialized project. Docs and authoring
490
+ * skills are inspected through the same packs normalize uses; only the two
491
+ * documentation files are parsed for links, TODO markers and empty sections.
492
+ */
493
+ export async function checkPracticePackages(root, scope, manifest, paths, version) {
494
+ // The packs own `.agents/skills/dab-*` targets, not the manifest `paths`
495
+ // values, so detect follows the pack layout to stay aligned with normalize.
496
+ void paths;
497
+ if (scope !== 'project') {
498
+ return { entries: [], diagnostics: [] };
499
+ }
500
+ const scan = await scanProject(root);
501
+ const problems = await practiceParentProblems(root);
502
+ const packs = [];
503
+ if (problems.docs === undefined) {
504
+ packs.push(documentationPack);
505
+ }
506
+ if (problems.skills === undefined) {
507
+ packs.push(authoringPack);
508
+ }
509
+ const plan = packs.length === 0
510
+ ? undefined
511
+ : await planPractice({ root, scope, scan, manifest, mode: 'converge', version }, packs);
512
+ const collector = new PracticeCollector();
513
+ const conflicted = plan === undefined ? new Set() : reportPlanConflicts(plan, collector);
514
+ const language = plan?.docsLanguage ?? await practiceLanguage(root, manifest);
515
+ const policySkipped = new Set(plan?.policySkipped ?? []);
516
+ if (problems.docs !== undefined) {
517
+ addParentProblem(collector, language, 'docs', problems.docs);
518
+ }
519
+ if (problems.skills !== undefined) {
520
+ addParentProblem(collector, language, 'skills', problems.skills);
521
+ }
522
+ for (const file of problems.docs === undefined ? PRACTICE_DOC_FILES : []) {
523
+ if (conflicted.has(file)) {
524
+ continue;
525
+ }
526
+ const state = await probeLstat(join(root, file));
527
+ if (state.problem !== undefined) {
528
+ addDocsPath(collector, language, file, problemMessage(file, state.problem), MANUAL_PATH_NEXT);
529
+ continue;
530
+ }
531
+ if (state.info === null) {
532
+ if (policySkipped.has(file)) {
533
+ addPolicySkip(collector, language, file, POLICY_NEXT);
534
+ }
535
+ else {
536
+ addDocsMissing(collector, language, file, NORMALIZE_NEXT);
537
+ }
538
+ continue;
539
+ }
540
+ const problem = await authoredFileProblem(root, file);
541
+ if (problem !== undefined) {
542
+ addDocsPath(collector, language, file, problem, MANUAL_PATH_NEXT);
543
+ continue;
544
+ }
545
+ let text;
546
+ try {
547
+ text = await readTextFile(join(root, file));
548
+ }
549
+ catch (error) {
550
+ addDocsPath(collector, language, file, `documentation file cannot be read: ${error instanceof Error ? error.message : String(error)}`, MANUAL_PATH_NEXT);
551
+ continue;
552
+ }
553
+ let status = 'ok';
554
+ for (const diagnostic of await linkDiagnostics(root, file, text)) {
555
+ collector.addDiagnostic(diagnostic);
556
+ collector.addEntry({
557
+ group: 'Documentation',
558
+ label: file,
559
+ status: 'error',
560
+ detail: docsDetail(language, diagnostic.message),
561
+ next: LINK_NEXT,
562
+ });
563
+ status = 'error';
564
+ }
565
+ const todoLines = todoMarkerLines(text);
566
+ for (const line of todoLines) {
567
+ collector.addDiagnostic({
568
+ code: 'practice.docs-todo',
569
+ severity: 'warning',
570
+ message: 'documentation TODO marker remains',
571
+ file,
572
+ line,
573
+ hint: 'Use the authoring skill for this document to replace the TODO with evidence.',
574
+ });
575
+ }
576
+ if (todoLines.length > 0) {
577
+ collector.addEntry({
578
+ group: 'Documentation',
579
+ label: file,
580
+ status: 'warn',
581
+ detail: docsDetail(language, `TODO markers at line ${todoLines.join(', ')}`),
582
+ next: file === ARCHITECTURE_FILE ? ARCHITECTURE_NEXT : DOC_NEXT,
583
+ });
584
+ if (status !== 'error') {
585
+ status = 'warn';
586
+ }
587
+ }
588
+ if (file === ARCHITECTURE_FILE) {
589
+ const empty = emptyArchitectureSections(text);
590
+ for (const section of empty) {
591
+ collector.addDiagnostic({
592
+ code: 'practice.docs-empty',
593
+ severity: 'warning',
594
+ message: `architecture section is empty: ${section.section}`,
595
+ file,
596
+ line: section.line,
597
+ hint: 'Use the dab-architecture-writing skill to fill the section with evidence.',
598
+ });
599
+ }
600
+ if (empty.length > 0) {
601
+ collector.addEntry({
602
+ group: 'Documentation',
603
+ label: file,
604
+ status: 'warn',
605
+ detail: docsDetail(language, `empty sections: ${empty.map((item) => item.section).join(', ')}`),
606
+ next: ARCHITECTURE_NEXT,
607
+ });
608
+ if (status !== 'error') {
609
+ status = 'warn';
610
+ }
611
+ }
612
+ }
613
+ if (status === 'ok') {
614
+ collector.addEntry({
615
+ group: 'Documentation',
616
+ label: file,
617
+ status: 'ok',
618
+ detail: languageDetail(language),
619
+ });
620
+ }
621
+ }
622
+ for (const name of problems.skills === undefined ? AUTHORING_SKILL_NAMES : []) {
623
+ const directory = skillDirectory(name);
624
+ if (conflicted.has(directory) || conflicted.has(skillFile(name))) {
625
+ continue;
626
+ }
627
+ const state = await authoredDirectoryState(root, directory);
628
+ if (state.kind === 'missing') {
629
+ addSkillMissing(collector, name, NORMALIZE_NEXT);
630
+ continue;
631
+ }
632
+ if (state.kind === 'conflict') {
633
+ addSkillPath(collector, name, directory, state.message);
634
+ continue;
635
+ }
636
+ const fileState = await skillFileState(root, name, state.absolute);
637
+ if (fileState.kind === 'missing') {
638
+ // Existing skill discovery reports a skill directory without SKILL.md.
639
+ continue;
640
+ }
641
+ if (fileState.kind === 'invalid') {
642
+ // Existing skill discovery owns the invalid frontmatter error.
643
+ continue;
644
+ }
645
+ if (fileState.kind === 'conflict') {
646
+ addSkillFileProblem(collector, name, fileState.message);
647
+ continue;
648
+ }
649
+ const managed = await planManagedSkills({ root, version, names: [name] });
650
+ for (const diagnostic of managed.diagnostics) {
651
+ collector.addDiagnostic(diagnostic);
652
+ }
653
+ const managedState = managed.states.find((item) => item.name === name);
654
+ if (managedState === undefined) {
655
+ // A path or read problem is a blocking error; the plan has no state.
656
+ const error = managed.diagnostics.find((item) => item.severity === 'error');
657
+ if (error !== undefined) {
658
+ collector.addEntry({
659
+ group: 'Skills',
660
+ label: name,
661
+ status: 'error',
662
+ detail: error.message,
663
+ next: MANUAL_PATH_NEXT,
664
+ });
665
+ }
666
+ continue;
667
+ }
668
+ addManagedSkillState(collector, managedState, version, managed.diagnostics);
669
+ }
670
+ return { entries: collector.entries, diagnostics: collector.diagnostics };
671
+ }
672
+ /**
673
+ * Read-only practice entries for an uninitialized or partial project. The
674
+ * planner runs in adoption mode with the same packs normalize would use; a
675
+ * valid manifest supplies its profile and docs language, otherwise detect
676
+ * displays the default generic + auto view without suggesting writes.
677
+ */
678
+ export async function preflightPractice(input) {
679
+ if (input.scope !== 'project') {
680
+ return { entries: [], diagnostics: [] };
681
+ }
682
+ const problems = await practiceParentProblems(input.root);
683
+ const packs = [];
684
+ if (problems.docs === undefined) {
685
+ packs.push(documentationPack);
686
+ }
687
+ if (problems.skills === undefined) {
688
+ packs.push(authoringPack);
689
+ }
690
+ const plan = packs.length === 0
691
+ ? undefined
692
+ : await planPractice({
693
+ root: input.root,
694
+ scope: input.scope,
695
+ scan: input.scan,
696
+ mode: 'adoption',
697
+ version: input.version,
698
+ ...(input.manifest === undefined ? {} : { manifest: input.manifest }),
699
+ }, packs);
700
+ const collector = new PracticeCollector();
701
+ const conflicted = plan === undefined ? new Set() : reportPlanConflicts(plan, collector);
702
+ const language = plan?.docsLanguage ?? await practiceLanguage(input.root, input.manifest);
703
+ const policySkipped = new Set(plan?.policySkipped ?? []);
704
+ if (problems.docs !== undefined) {
705
+ addParentProblem(collector, language, 'docs', problems.docs);
706
+ }
707
+ if (problems.skills !== undefined) {
708
+ addParentProblem(collector, language, 'skills', problems.skills);
709
+ }
710
+ const next = input.suppressNext ? undefined : NORMALIZE_NEXT;
711
+ for (const file of problems.docs === undefined ? PRACTICE_DOC_FILES : []) {
712
+ if (conflicted.has(file)) {
713
+ continue;
714
+ }
715
+ const state = await probeLstat(join(input.root, file));
716
+ if (state.problem !== undefined) {
717
+ addDocsPath(collector, language, file, problemMessage(file, state.problem), MANUAL_PATH_NEXT);
718
+ continue;
719
+ }
720
+ if (state.info !== null) {
721
+ collector.addEntry({
722
+ group: 'Documentation',
723
+ label: file,
724
+ status: 'ok',
725
+ detail: languageDetail(language),
726
+ });
727
+ continue;
728
+ }
729
+ if (policySkipped.has(file)) {
730
+ addPolicySkip(collector, language, file, input.suppressNext ? undefined : POLICY_NEXT);
731
+ continue;
732
+ }
733
+ // Preflight is an informational view: missing entries do not produce
734
+ // diagnostics until a normalized project is actually checked.
735
+ addDocsMissing(collector, language, file, next, false);
736
+ }
737
+ for (const name of problems.skills === undefined ? AUTHORING_SKILL_NAMES : []) {
738
+ const directory = skillDirectory(name);
739
+ if (conflicted.has(directory) || conflicted.has(skillFile(name))) {
740
+ continue;
741
+ }
742
+ const state = await authoredDirectoryState(input.root, directory);
743
+ if (state.kind === 'missing') {
744
+ addSkillMissing(collector, name, next, false);
745
+ continue;
746
+ }
747
+ if (state.kind === 'conflict') {
748
+ addSkillPath(collector, name, directory, state.message);
749
+ continue;
750
+ }
751
+ const fileState = await skillFileState(input.root, name, state.absolute);
752
+ if (fileState.kind === 'conflict') {
753
+ addSkillFileProblem(collector, name, fileState.message);
754
+ continue;
755
+ }
756
+ if (fileState.kind === 'invalid') {
757
+ // Preflight does not run source discovery, so it reports the invalid
758
+ // source itself instead of claiming the directory is preserved.
759
+ for (const diagnostic of fileState.diagnostics) {
760
+ collector.addDiagnostic(diagnostic);
761
+ }
762
+ continue;
763
+ }
764
+ if (fileState.kind === 'missing') {
765
+ collector.addEntry({
766
+ group: 'Skills',
767
+ label: name,
768
+ status: 'warn',
769
+ detail: 'skill directory has no SKILL.md',
770
+ });
771
+ continue;
772
+ }
773
+ collector.addEntry({
774
+ group: 'Skills',
775
+ label: name,
776
+ status: 'ok',
777
+ detail: 'authored skill directory is preserved',
778
+ });
779
+ }
780
+ return { entries: collector.entries, diagnostics: collector.diagnostics };
781
+ }
782
+ //# sourceMappingURL=practice.js.map