roadmapsmith 0.9.0 → 0.9.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.
@@ -1,730 +1,730 @@
1
- 'use strict';
2
-
3
- const path = require('path');
4
- const fs = require('fs');
5
- const { walkFiles, detectTestFrameworks } = require('../io');
6
- const { collectPluginContributions } = require('../config');
7
- const { escapeRegExp, tokenize } = require('../utils');
8
-
9
- const CONFIDENCE_RANK = { low: 0, medium: 1, high: 2 };
10
-
11
- const CODE_EXTENSIONS = new Set([
12
- '.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.py', '.go', '.rs', '.java', '.kt', '.swift', '.rb', '.php', '.cs'
13
- ]);
14
-
15
- // "docs" omitted from DOC_HINTS — it is a path prefix in scan tasks, not a doc-authoring keyword.
16
- const DOC_HINTS = ['readme', 'changelog', 'documentation', 'spec', 'diagram', 'runbook'];
17
- const CODE_HINTS = ['implement', 'add', 'create', 'build', 'refactor', 'fix', 'module', 'function', 'api', 'endpoint', 'command'];
18
- const GENERIC_TASK_TOKENS = new Set([
19
- // Action verbs too broad to be evidence signals
20
- 'implement', 'implementation', 'create', 'add', 'build', 'refactor', 'fix',
21
- 'detect', 'detection', 'support', 'handle', 'handler', 'update', 'check', 'run',
22
- 'process', 'processing', 'generate', 'generation', 'format', 'report',
23
- // Structural concepts shared by every codebase
24
- 'module', 'function', 'class', 'method', 'command', 'type', 'value', 'values',
25
- 'output', 'input', 'data',
26
- // Test vocabulary
27
- 'test', 'tests',
28
- // Infrastructure names present in nearly every Node/JS project
29
- 'config', 'configuration', 'package', 'json', 'project', 'roadmap',
30
- // Domain words specific to this tool that appear in non-feature source files
31
- 'confidence', 'profile', 'validation', 'evidence',
32
- // Package/module field names that appear naturally in any Node.js generator or config file
33
- 'main', 'exports', 'files', 'fields', 'without', 'field',
34
- // Terminology used in architecture/detection task descriptions that overlaps with source identifiers
35
- 'signals', 'directory', 'directories', 'headers', 'site', 'shebang',
36
- // Common directory names that appear in import paths — too generic for evidence
37
- 'src', 'lib',
38
- // Broad task-description verbs and nouns that pollute evidence matching across every codebase
39
- 'task', 'tasks', 'file', 'source', 'code', 'artifact', 'artifacts',
40
- 'generic', 'feature', 'features', 'section', 'sections',
41
- 'user', 'users', 'workflow', 'workflows', 'mode', 'modes', 'replace',
42
- // Tool-internal vocabulary that appears in non-feature implementation files
43
- 'audit', 'debug', 'signal', 'signals', 'log',
44
- // English stopwords and function words that appear everywhere — not useful as evidence signals
45
- 'only', 'must', 'what', 'which', 'kind', 'never', 'also', 'each',
46
- 'detected', 'generated', 'existing', 'available',
47
- // Tool-commentary vocabulary that appears in source comments but describes past/intended behavior
48
- 'phrases', 'conceptual',
49
- ]);
50
-
51
- const CANONICAL_FILES = {
52
- security: 'SECURITY.md',
53
- readme: 'README.md',
54
- changelog: 'CHANGELOG.md',
55
- license: 'LICENSE'
56
- };
57
-
58
- // The roadmap file must never be included in the evidence pool: its task descriptions
59
- // contain the exact vocabulary of the tasks being validated, which would cause every
60
- // task to validate itself.
61
- const SELF_REFERENTIAL_FILES = new Set(['ROADMAP.md']);
62
-
63
- // Maps task-ID namespace prefix to a predicate on (normalized) file paths.
64
- // When a task ID has a known namespace, at least one evidence file must satisfy
65
- // the predicate — otherwise generic token overlap alone cannot pass the task.
66
- const NAMESPACE_STRUCTURAL_PATTERNS = {
67
- cls: (p) => /classif(?:ier|y)|archetype/.test(p),
68
- dsg: (p) => /generator[/\\](?:domain|web|landing|profiles?)|(?:domain|web|landing)[/\\](?:profile|generator)/.test(p),
69
- evh2: (p) => p.includes('/validator/') || p.includes('\\validator\\'),
70
- cst: (p) => /smoke|integration[-_]test|e2e/.test(p),
71
- uxf: (p) => p.includes('/renderer/') || p.includes('\\renderer\\') || /renderer\.[jt]sx?$/.test(p),
72
- cfgo: (p) => /config[/\\]|schema[/\\]|config\.[jt]s$|schema\.[jt]s$/.test(p),
73
- doc3: (p) => /(?:^|[/\\])docs[/\\]|readme\.md$/i.test(p),
74
- };
75
-
76
- // Test fixture directories contain synthetic code created to drive test scenarios,
77
- // not real implementations. Including them pollutes the evidence pool with vocabulary
78
- // that was deliberately seeded for testing purposes (e.g. namespace-vocab fixtures).
79
- function isFixturePath(relativePath) {
80
- return /(?:^|[/\\])fixtures[/\\]/.test(relativePath);
81
- }
82
-
83
- function readFileIndex(projectRoot, files) {
84
- const index = [];
85
- for (const relativePath of files) {
86
- if (SELF_REFERENTIAL_FILES.has(relativePath)) continue;
87
- if (isFixturePath(relativePath)) continue;
88
-
89
- const absolutePath = path.resolve(projectRoot, relativePath);
90
- const ext = path.extname(relativePath).toLowerCase();
91
- let content = '';
92
- try {
93
- const buffer = fs.readFileSync(absolutePath);
94
- if (buffer.length > 512 * 1024) {
95
- continue;
96
- }
97
- content = buffer.toString('utf8');
98
- } catch {
99
- continue;
100
- }
101
-
102
- index.push({
103
- relativePath,
104
- absolutePath,
105
- ext,
106
- content,
107
- isTestFile: /(^|\/)(__tests__|tests)\//.test(relativePath) || /\.test\.|\.spec\.|_test\.go$/.test(relativePath)
108
- });
109
- }
110
- return index;
111
- }
112
-
113
- const KNOWN_PATH_ROOTS = [
114
- 'src/', 'lib/', 'bin/', 'test/', 'tests/', 'docs/', 'scripts/',
115
- 'packages/', 'apps/', 'tools/', '.github/', 'roadmap-skill/'
116
- ];
117
-
118
- function hasFileExtension(token) {
119
- const lastSegment = token.replace(/\\/g, '/').split('/').pop() || '';
120
- return /\.[A-Za-z0-9]{1,10}$/.test(lastSegment);
121
- }
122
-
123
- function isLikelyPath(token) {
124
- if (/^\.{1,2}\/|^\//.test(token)) return true;
125
- if (hasFileExtension(token)) return true;
126
- if (KNOWN_PATH_ROOTS.some((root) => token.startsWith(root))) return true;
127
- // The ">= 2 slashes" rule was intentionally removed: it caused conceptual slash phrases
128
- // like "code/test/artifact" or "build/test/deploy" to be treated as file paths.
129
- // Real multi-segment paths are caught by the extension or known-root rules above.
130
- return false;
131
- }
132
-
133
- // Matches standalone filenames without a slash — e.g. "roadmap-skill.config.json",
134
- // "package.json", "vite.config.ts". These are path references whose component tokens
135
- // (e.g. "roadmap", "skill") must be excluded from code evidence scoring to prevent
136
- // circular vocabulary: a task mentioning a filename would otherwise score hits in any
137
- // source file that happens to reference the same filename for unrelated reasons.
138
- // Numeric-only tokens like "1.0.0" or "v0.8" are excluded via the leading-digit guard.
139
- const STANDALONE_FILE_RE = /\b([A-Za-z][A-Za-z0-9_.+-]*\.[A-Za-z0-9]{2,10})\b/g;
140
- const KNOWN_FILE_EXTENSIONS = new Set([
141
- '.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.py', '.go', '.rs',
142
- '.java', '.kt', '.swift', '.rb', '.php', '.cs', '.json', '.yaml', '.yml',
143
- '.toml', '.md', '.txt', '.sh', '.bash', '.env', '.html', '.css', '.scss', '.lock'
144
- ]);
145
-
146
- function hasKnownFileExtension(token) {
147
- const lastDot = token.lastIndexOf('.');
148
- if (lastDot < 0) return false;
149
- return KNOWN_FILE_EXTENSIONS.has(token.slice(lastDot).toLowerCase());
150
- }
151
-
152
- function extractExplicitPaths(text) {
153
- const results = new Set();
154
- const quoted = String(text).match(/`([^`]+)`/g) || [];
155
- for (const token of quoted) {
156
- const clean = token.slice(1, -1);
157
- if (clean.includes('/') || clean.includes('\\') || clean.includes('.')) {
158
- results.add(clean);
159
- }
160
- }
161
-
162
- const pathTokens = String(text).match(/([A-Za-z0-9_.-]+\/[A-Za-z0-9_./-]+)/g) || [];
163
- for (const raw of pathTokens) {
164
- const token = raw.replace(/[.,;:!?)]+$/, '');
165
- if (isLikelyPath(token)) results.add(token);
166
- }
167
-
168
- return Array.from(results).sort((left, right) => left.localeCompare(right));
169
- }
170
-
171
- // Standalone filenames (no slash) mentioned in task prose — e.g. "roadmap-skill.config.json",
172
- // "package.json". These are filename *references*, NOT path-existence assertions: the author
173
- // is describing which file contains a feature, not asserting that the file must exist.
174
- // Used only for pathDerivedToken extraction (to prevent circular vocabulary), never for
175
- // findFilesByPathHints (which would pass any task whose config file already exists).
176
- function extractStandaloneFilenames(text) {
177
- const results = new Set();
178
- STANDALONE_FILE_RE.lastIndex = 0;
179
- let m = STANDALONE_FILE_RE.exec(String(text));
180
- while (m) {
181
- const token = m[1].replace(/[.,;:!?)]+$/, '');
182
- if (hasKnownFileExtension(token) && !token.startsWith('.')) {
183
- results.add(token);
184
- }
185
- m = STANDALONE_FILE_RE.exec(String(text));
186
- }
187
- return Array.from(results);
188
- }
189
-
190
- function extractSymbolHints(text) {
191
- const symbols = new Set();
192
- const patterns = [
193
- /(?:function|class|method|command)\s+([A-Za-z_][A-Za-z0-9_]*)/gi,
194
- /(?:function|module|class|command|method)\s+`([A-Za-z_][A-Za-z0-9_-]*)`/gi,
195
- /`([A-Za-z_][A-Za-z0-9_-]*)`\s+(?:function|module|class|command|method)/gi
196
- ];
197
-
198
- for (const pattern of patterns) {
199
- let match = pattern.exec(text);
200
- while (match) {
201
- symbols.add(match[1]);
202
- match = pattern.exec(text);
203
- }
204
- }
205
-
206
- return Array.from(symbols).sort((left, right) => left.localeCompare(right));
207
- }
208
-
209
- function isCodeTask(taskText) {
210
- const normalized = String(taskText).toLowerCase();
211
- return CODE_HINTS.some((hint) => normalized.includes(hint));
212
- }
213
-
214
- function isDocTask(taskText) {
215
- const normalized = String(taskText).toLowerCase();
216
- // Use word-boundary matching to avoid substring false positives (e.g. "specific" ≠ "spec").
217
- const hasDocKeyword = DOC_HINTS.some((hint) => new RegExp(`(?<![a-z])${hint}(?![a-z])`).test(normalized));
218
- if (!hasDocKeyword) return false;
219
- // Also require a creation/update verb so that policy tasks mentioning doc files
220
- // ("README must not be used as evidence") don't trigger doc-artifact evidence.
221
- return /\b(add|create|write|update|init|initialize|introduce|setup|document)\b/.test(normalized);
222
- }
223
-
224
- function findFilesByPathHints(pathHints, fileIndex) {
225
- const matches = [];
226
- for (const hint of pathHints) {
227
- const normalizedHint = hint.replace(/\\/g, '/');
228
- const direct = fileIndex.find((file) => file.relativePath === normalizedHint);
229
- if (direct) {
230
- matches.push(direct.relativePath);
231
- continue;
232
- }
233
-
234
- for (const file of fileIndex) {
235
- if (file.relativePath.endsWith(normalizedHint)) {
236
- matches.push(file.relativePath);
237
- }
238
- }
239
- }
240
- return Array.from(new Set(matches)).sort((left, right) => left.localeCompare(right));
241
- }
242
-
243
- function findFilesBySymbols(symbolHints, fileIndex) {
244
- const matches = new Set();
245
- for (const symbol of symbolHints) {
246
- const regex = new RegExp(`\\b${escapeRegExp(symbol)}\\b`, 'i');
247
- for (const file of fileIndex) {
248
- if (!CODE_EXTENSIONS.has(file.ext)) {
249
- continue;
250
- }
251
- if (regex.test(file.content)) {
252
- matches.add(file.relativePath);
253
- }
254
- }
255
- }
256
- return Array.from(matches).sort((left, right) => left.localeCompare(right));
257
- }
258
-
259
- // Tokens extracted from a referenced file path (e.g. "roadmap-skill" from
260
- // "roadmap-skill.config.json") must not be reused as code evidence signals.
261
- // Those tokens appear in any file that mentions the same path — creating circular
262
- // vocabulary where a task about "X in path/to/file" passes because the source
263
- // code references the same path for unrelated reasons.
264
- function extractPathDerivedTokens(pathHints) {
265
- const tokens = new Set();
266
- for (const hint of pathHints) {
267
- // Char-split: "roadmap-skill.config.json" → ["roadmap", "skill", "config", "json"]
268
- const parts = hint.replace(/[.\-_/\\]/g, ' ').toLowerCase().split(/\s+/).filter(Boolean);
269
- for (const part of parts) {
270
- if (part.length >= 3) tokens.add(part);
271
- }
272
- // Tokenizer-split: also adds compound tokens the char-split misses, e.g. "roadmap-skill"
273
- // (the tokenizer preserves hyphens in identifiers; the char-split strips them).
274
- for (const token of tokenize(hint)) {
275
- if (token.length >= 3) tokens.add(token);
276
- }
277
- }
278
- return tokens;
279
- }
280
-
281
- function findCodeEvidence(taskText, fileIndex, pathDerivedTokens = new Set()) {
282
- const tokens = tokenize(taskText)
283
- .filter((token) => token.length >= 3 && !GENERIC_TASK_TOKENS.has(token) && !token.endsWith('/') && !pathDerivedTokens.has(token))
284
- .slice(0, 8);
285
- if (tokens.length === 0) {
286
- return [];
287
- }
288
-
289
- const matches = [];
290
- for (const file of fileIndex) {
291
- if (!CODE_EXTENSIONS.has(file.ext) || file.isTestFile) {
292
- continue;
293
- }
294
-
295
- let score = 0;
296
- const lowered = file.content.toLowerCase();
297
- for (const token of tokens) {
298
- if (token.length < 3) {
299
- continue;
300
- }
301
- if (lowered.includes(token)) {
302
- score += 1;
303
- }
304
- }
305
-
306
- // Require more matches proportional to how many specific tokens the task has.
307
- // Tasks with 4+ meaningful tokens need 3 files to match to prevent vocabulary overlap.
308
- const threshold = tokens.length >= 4 ? 3 : tokens.length >= 2 ? 2 : 1;
309
- if (score >= threshold) {
310
- matches.push(file.relativePath);
311
- }
312
- }
313
-
314
- return matches.slice(0, 20);
315
- }
316
-
317
- function findTestEvidence(taskText, fileIndex) {
318
- const tokens = tokenize(taskText)
319
- .filter((token) => token.length >= 3 && !GENERIC_TASK_TOKENS.has(token) && !token.endsWith('/'))
320
- .slice(0, 8);
321
-
322
- if (tokens.length === 0) return [];
323
-
324
- // Only tokens of length >= 4 are used for import-reference matching.
325
- // Very short tokens (e.g. "app", "web") are too generic: they appear as substrings in
326
- // many import paths that have nothing to do with the feature being validated.
327
- // The single-short-token fallback below handles the narrow case of one-word module names.
328
- const importTokens = tokens.filter((token) => token.length >= 4);
329
-
330
- const matches = [];
331
-
332
- for (const file of fileIndex) {
333
- if (!file.isTestFile) continue;
334
-
335
- // A test file counts as evidence only when it imports a module whose path contains
336
- // one of the task's meaningful tokens. Content-keyword matching is intentionally absent:
337
- // test content (descriptions, literals) can contain future-task vocabulary,
338
- // producing self-referential false positives.
339
- //
340
- // Trailing slashes are NOT stripped: "app/" is a directory reference, not a module name.
341
- // "../src/app" (a real import) does not contain the string "app/" so it won't match.
342
- const importRefs = (
343
- file.content.match(/require\s*\(\s*['"`]([^'"`]+)['"`]\s*\)|from\s+['"`]([^'"`]+)['"`]/g) || []
344
- ).join(' ').toLowerCase();
345
-
346
- if (importTokens.length > 0 && importTokens.some((token) => importRefs.includes(token))) {
347
- matches.push(file.relativePath);
348
- continue;
349
- }
350
-
351
- // Narrow fallback: single very-short token (e.g. "app", "cli").
352
- // Import paths for these are too short to distinguish reliably, so fall back to a
353
- // content match — but only when there is exactly one such token (no multi-token dilution).
354
- if (tokens.length === 1 && tokens[0].length < 4) {
355
- const lowered = file.content.toLowerCase();
356
- if (lowered.includes(tokens[0])) {
357
- matches.push(file.relativePath);
358
- }
359
- }
360
- }
361
-
362
- return matches.slice(0, 20);
363
- }
364
-
365
- function findArtifactEvidence(taskText, fileIndex) {
366
- const normalized = String(taskText).toLowerCase();
367
- const files = [];
368
- const heuristicArtifacts = [];
369
-
370
- // Canonical file detection only applies to short tasks (≤8 words) that are about
371
- // creating or referencing that specific file. Long sentences that merely MENTION
372
- // "readme" or "security" in a policy/constraint context are excluded.
373
- const wordCount = normalized.trim().split(/\s+/).length;
374
- if (wordCount <= 8) {
375
- for (const [keyword, filename] of Object.entries(CANONICAL_FILES)) {
376
- // Use hyphen-aware word boundaries: "security-headers" must not match "security".
377
- if (new RegExp(`(?<![a-z-])${keyword}(?![a-z-])`).test(normalized)) {
378
- const hit = fileIndex.find(
379
- (f) => f.relativePath === filename || f.relativePath.endsWith('/' + filename)
380
- );
381
- if (hit) {
382
- files.push(hit.relativePath);
383
- heuristicArtifacts.push(hit.relativePath);
384
- }
385
- }
386
- }
387
- }
388
-
389
- if (!isDocTask(taskText)) {
390
- return { files, heuristicArtifacts };
391
- }
392
-
393
- const artifactPatterns = [
394
- /^README\.md$/i,
395
- /^CHANGELOG\.md$/i,
396
- /^docs\//i,
397
- /^artifacts\//i,
398
- /^dist\//i,
399
- /^build\//i
400
- ];
401
-
402
- for (const file of fileIndex) {
403
- if (artifactPatterns.some((pattern) => pattern.test(file.relativePath)) && !files.includes(file.relativePath)) {
404
- files.push(file.relativePath);
405
- }
406
- }
407
-
408
- return { files: files.slice(0, 20), heuristicArtifacts };
409
- }
410
-
411
- function extractTaskNamespace(taskId) {
412
- if (!taskId) return null;
413
- const match = String(taskId).match(/^([a-z][a-z0-9]*)-/);
414
- return match ? match[1] : null;
415
- }
416
-
417
- function isAcceptanceCriteria(taskId) {
418
- return /ph\d+[_-]st\d+[_-]exit/.test(String(taskId || ''));
419
- }
420
-
421
- // Gate: returns { applicable, passed, structuralFiles, reason }.
422
- // For namespaces with a defined structural pattern:
423
- // 1. If no files in fileIndex match the pattern → immediate fail.
424
- // 2. For acceptance-criteria tasks (phN-stN-exit IDs): path match alone is enough.
425
- // 3. For implementation tasks: feature tokens from task text must score ≥ ceil(n/2)
426
- // against namespace-matched files, preventing vocabulary overlap from generic
427
- // infrastructure code (io.js, generator/index.js) from serving as evidence.
428
- function checkNamespaceStructuralEvidence(taskId, taskText, fileIndex) {
429
- const namespace = extractTaskNamespace(taskId);
430
- if (!namespace || !NAMESPACE_STRUCTURAL_PATTERNS[namespace]) {
431
- return { applicable: false, passed: true, structuralFiles: [], reason: null };
432
- }
433
-
434
- const predicate = NAMESPACE_STRUCTURAL_PATTERNS[namespace];
435
- const namespaceFiles = fileIndex.filter((f) => predicate(f.relativePath));
436
-
437
- if (namespaceFiles.length === 0) {
438
- return {
439
- applicable: true,
440
- passed: false,
441
- structuralFiles: [],
442
- reason: `namespace "${namespace}" has no implementation files`,
443
- };
444
- }
445
-
446
- const featureTokens = tokenize(taskText)
447
- .filter((t) => t.length >= 4 && !GENERIC_TASK_TOKENS.has(t) && !t.endsWith('/'))
448
- .slice(0, 8);
449
-
450
- if (featureTokens.length === 0) {
451
- return {
452
- applicable: true,
453
- passed: true,
454
- structuralFiles: namespaceFiles.map((f) => f.relativePath),
455
- reason: null,
456
- };
457
- }
458
-
459
- let bestScore = 0;
460
- for (const nsFile of namespaceFiles) {
461
- const lowered = nsFile.content.toLowerCase();
462
- let score = 0;
463
- for (const token of featureTokens) {
464
- if (lowered.includes(token)) score++;
465
- }
466
- if (score > bestScore) bestScore = score;
467
- }
468
-
469
- const threshold = Math.max(1, Math.ceil(featureTokens.length / 2));
470
- if (bestScore >= threshold) {
471
- return {
472
- applicable: true,
473
- passed: true,
474
- structuralFiles: namespaceFiles.map((f) => f.relativePath),
475
- reason: null,
476
- };
477
- }
478
-
479
- return {
480
- applicable: true,
481
- passed: false,
482
- structuralFiles: namespaceFiles.map((f) => f.relativePath),
483
- reason: `structural token score ${bestScore}/${threshold} in "${namespace}" files — token overlap insufficient`,
484
- };
485
- }
486
-
487
- function evaluateRule(rule, task, context) {
488
- if (!rule) {
489
- return { passed: true, reasons: [], evidence: {} };
490
- }
491
-
492
- if (rule.when) {
493
- const regexp = new RegExp(rule.when, 'i');
494
- if (!regexp.test(task.text)) {
495
- return { passed: true, reasons: [], evidence: {} };
496
- }
497
- }
498
-
499
- if (typeof rule.check === 'function') {
500
- const custom = rule.check(task, context);
501
- if (!custom) {
502
- return { passed: true, reasons: [], evidence: {} };
503
- }
504
- return {
505
- passed: custom.passed !== false,
506
- reasons: Array.isArray(custom.reasons) ? custom.reasons : [],
507
- evidence: custom.evidence || {}
508
- };
509
- }
510
-
511
- const reasons = [];
512
- const evidence = {};
513
-
514
- if (rule.type === 'file-exists' && rule.path) {
515
- const hit = context.fileIndex.find((file) => file.relativePath === rule.path || file.relativePath.endsWith(rule.path));
516
- if (!hit) {
517
- reasons.push(rule.message || `missing file: ${rule.path}`);
518
- } else {
519
- evidence.file = hit.relativePath;
520
- }
521
- }
522
-
523
- if (rule.type === 'symbol' && rule.pattern) {
524
- const regex = new RegExp(rule.pattern, 'i');
525
- const hit = context.fileIndex.find((file) => regex.test(file.content));
526
- if (!hit) {
527
- reasons.push(rule.message || `missing symbol pattern: ${rule.pattern}`);
528
- } else {
529
- evidence.symbol = hit.relativePath;
530
- }
531
- }
532
-
533
- if (rule.type === 'artifact' && rule.path) {
534
- const hit = context.fileIndex.find((file) => file.relativePath.startsWith(rule.path) || file.relativePath === rule.path);
535
- if (!hit) {
536
- reasons.push(rule.message || `missing artifact: ${rule.path}`);
537
- } else {
538
- evidence.artifact = hit.relativePath;
539
- }
540
- }
541
-
542
- if (rule.type === 'test' && context.testFrameworks.length === 0) {
543
- reasons.push(rule.message || 'test framework not detected');
544
- }
545
-
546
- return {
547
- passed: reasons.length === 0,
548
- reasons,
549
- evidence
550
- };
551
- }
552
-
553
- function buildValidationContext(projectRoot, config, plugins) {
554
- const files = walkFiles(projectRoot);
555
- const fileIndex = readFileIndex(projectRoot, files);
556
- const testFrameworks = detectTestFrameworks(projectRoot, files);
557
-
558
- return {
559
- projectRoot,
560
- config,
561
- plugins,
562
- files,
563
- fileIndex,
564
- testFrameworks
565
- };
566
- }
567
-
568
- function validateTask(task, context, config, plugins) {
569
- const pathHints = extractExplicitPaths(task.text);
570
- const standaloneFilenames = extractStandaloneFilenames(task.text);
571
- const symbolHints = extractSymbolHints(task.text);
572
-
573
- const filesFromPaths = findFilesByPathHints(pathHints, context.fileIndex);
574
- const filesFromSymbols = findFilesBySymbols(symbolHints, context.fileIndex);
575
- // Combine path hints AND standalone filenames for token exclusion so that tokens
576
- // derived from any referenced filename (e.g. "roadmap-skill" from
577
- // "roadmap-skill.config.json") are excluded from code evidence scoring.
578
- const pathDerivedTokens = extractPathDerivedTokens([...pathHints, ...standaloneFilenames]);
579
- const filesFromCode = findCodeEvidence(task.text, context.fileIndex, pathDerivedTokens);
580
- const filesFromTests = findTestEvidence(task.text, context.fileIndex);
581
- const { files: filesFromArtifacts, heuristicArtifacts } = findArtifactEvidence(task.text, context.fileIndex);
582
-
583
- const structuralCheck = checkNamespaceStructuralEvidence(task.id, task.text, context.fileIndex);
584
-
585
- const evidence = {
586
- code: filesFromCode.length > 0 || filesFromSymbols.length > 0,
587
- test: filesFromTests.length > 0,
588
- artifact: filesFromArtifacts.length > 0,
589
- files: filesFromPaths,
590
- symbols: filesFromSymbols,
591
- codeFiles: filesFromCode,
592
- testFiles: filesFromTests,
593
- artifactFiles: filesFromArtifacts,
594
- heuristicArtifacts,
595
- structuralEvidence: structuralCheck.applicable ? structuralCheck.passed : null,
596
- structuralFiles: structuralCheck.structuralFiles,
597
- };
598
-
599
- const reasons = [];
600
- if (pathHints.length > 0 && filesFromPaths.length === 0) {
601
- reasons.push(`missing referenced file(s): ${pathHints.join(', ')}`);
602
- }
603
- if (symbolHints.length > 0 && filesFromSymbols.length === 0) {
604
- reasons.push(`missing symbol(s): ${symbolHints.join(', ')}`);
605
- }
606
-
607
- // Namespace-structural gate: for known namespaces, token overlap alone is insufficient.
608
- // The task must have evidence files whose paths match the namespace pattern.
609
- if (structuralCheck.applicable && !structuralCheck.passed) {
610
- reasons.push(structuralCheck.reason || `no structural evidence for namespace "${extractTaskNamespace(task.id)}"`);
611
- }
612
-
613
- const hasEvidence = evidence.code || evidence.test || evidence.artifact || evidence.files.length > 0;
614
- if (!hasEvidence && !structuralCheck.applicable) {
615
- reasons.push('no code, test, or artifact evidence found');
616
- } else if (!hasEvidence && structuralCheck.applicable && structuralCheck.passed) {
617
- reasons.push('no code, test, or artifact evidence found');
618
- }
619
-
620
- const requiresTest = context.testFrameworks.length > 0 && isCodeTask(task.text) && !isDocTask(task.text);
621
- if (requiresTest && !evidence.test) {
622
- reasons.push('missing test evidence');
623
- }
624
-
625
- const configuredRules = Array.isArray(config.validators) ? config.validators : [];
626
- const pluginRules = collectPluginContributions(plugins || [], 'registerValidators', context);
627
- for (const rule of [...configuredRules, ...pluginRules]) {
628
- const ruleResult = evaluateRule(rule, task, context);
629
- if (!ruleResult.passed) {
630
- reasons.push(...ruleResult.reasons);
631
- }
632
- }
633
-
634
- const uniqueReasons = Array.from(new Set(reasons));
635
- const attempted = hasEvidence || pathHints.length > 0 || symbolHints.length > 0;
636
-
637
- const evidenceCount = [evidence.code, evidence.test, evidence.artifact].filter(Boolean).length;
638
- const confidence = evidenceCount >= 2 ? 'high' : evidenceCount === 1 ? 'medium' : 'low';
639
-
640
- // True when the only passing evidence is artifact/doc files and the task is not a doc task.
641
- // Used by auditValidation to flag implementation tasks that pass solely via documentation.
642
- const evidenceIsDocOnly = !evidence.code && !evidence.test && evidence.artifact && !isDocTask(task.text);
643
-
644
- return {
645
- taskId: task.id,
646
- passed: uniqueReasons.length === 0,
647
- confidence,
648
- reasons: uniqueReasons,
649
- evidence,
650
- evidenceIsDocOnly,
651
- requiresTest,
652
- hasEvidence,
653
- attempted
654
- };
655
- }
656
-
657
- function validateTasks(tasks, context, config, plugins) {
658
- const result = {};
659
- for (const task of tasks) {
660
- result[task.id] = validateTask(task, context, config, plugins);
661
- }
662
- return result;
663
- }
664
-
665
- function auditValidation(tasks, results) {
666
- const checkedWithoutEvidence = [];
667
- const readyButUnchecked = [];
668
- const checkedWithWeakEvidence = [];
669
- const documentationOnlyEvidenceForImplementation = [];
670
- const checkedWithNoStructuralEvidence = [];
671
-
672
- for (const task of tasks) {
673
- const result = results[task.id];
674
- if (!result) continue;
675
-
676
- if (task.checked && !result.passed) {
677
- checkedWithoutEvidence.push({ task, result });
678
- }
679
-
680
- if (!task.checked && result.passed) {
681
- readyButUnchecked.push({ task, result });
682
- }
683
-
684
- if (task.checked && result.passed && result.confidence === 'low') {
685
- checkedWithWeakEvidence.push({ task, result });
686
- }
687
-
688
- if (task.checked && result.passed && result.evidenceIsDocOnly) {
689
- documentationOnlyEvidenceForImplementation.push({ task, result });
690
- }
691
-
692
- // Checked task that failed specifically because structural evidence is missing.
693
- if (task.checked && !result.passed && result.evidence.structuralEvidence === false) {
694
- checkedWithNoStructuralEvidence.push({ task, result });
695
- }
696
- }
697
-
698
- return {
699
- checkedWithoutEvidence,
700
- readyButUnchecked,
701
- checkedWithWeakEvidence,
702
- documentationOnlyEvidenceForImplementation,
703
- checkedWithNoStructuralEvidence,
704
- };
705
- }
706
-
707
- function applyMinimumConfidence(results, minimumConfidence) {
708
- const minRank = CONFIDENCE_RANK[minimumConfidence] ?? 0;
709
- if (minRank === 0) return;
710
- for (const result of Object.values(results)) {
711
- if ((CONFIDENCE_RANK[result.confidence] ?? 0) < minRank) {
712
- result.passed = false;
713
- result.reasons = [
714
- ...result.reasons,
715
- `validation confidence "${result.confidence}" is below required "${minimumConfidence}"`
716
- ];
717
- }
718
- }
719
- }
720
-
721
- module.exports = {
722
- auditValidation,
723
- buildValidationContext,
724
- validateTask,
725
- validateTasks,
726
- CONFIDENCE_RANK,
727
- applyMinimumConfidence,
728
- extractTaskNamespace,
729
- isAcceptanceCriteria,
730
- };
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+ const fs = require('fs');
5
+ const { walkFiles, detectTestFrameworks } = require('../io');
6
+ const { collectPluginContributions } = require('../config');
7
+ const { escapeRegExp, tokenize } = require('../utils');
8
+
9
+ const CONFIDENCE_RANK = { low: 0, medium: 1, high: 2 };
10
+
11
+ const CODE_EXTENSIONS = new Set([
12
+ '.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.py', '.go', '.rs', '.java', '.kt', '.swift', '.rb', '.php', '.cs'
13
+ ]);
14
+
15
+ // "docs" omitted from DOC_HINTS — it is a path prefix in scan tasks, not a doc-authoring keyword.
16
+ const DOC_HINTS = ['readme', 'changelog', 'documentation', 'spec', 'diagram', 'runbook'];
17
+ const CODE_HINTS = ['implement', 'add', 'create', 'build', 'refactor', 'fix', 'module', 'function', 'api', 'endpoint', 'command'];
18
+ const GENERIC_TASK_TOKENS = new Set([
19
+ // Action verbs too broad to be evidence signals
20
+ 'implement', 'implementation', 'create', 'add', 'build', 'refactor', 'fix',
21
+ 'detect', 'detection', 'support', 'handle', 'handler', 'update', 'check', 'run',
22
+ 'process', 'processing', 'generate', 'generation', 'format', 'report',
23
+ // Structural concepts shared by every codebase
24
+ 'module', 'function', 'class', 'method', 'command', 'type', 'value', 'values',
25
+ 'output', 'input', 'data',
26
+ // Test vocabulary
27
+ 'test', 'tests',
28
+ // Infrastructure names present in nearly every Node/JS project
29
+ 'config', 'configuration', 'package', 'json', 'project', 'roadmap',
30
+ // Domain words specific to this tool that appear in non-feature source files
31
+ 'confidence', 'profile', 'validation', 'evidence',
32
+ // Package/module field names that appear naturally in any Node.js generator or config file
33
+ 'main', 'exports', 'files', 'fields', 'without', 'field',
34
+ // Terminology used in architecture/detection task descriptions that overlaps with source identifiers
35
+ 'signals', 'directory', 'directories', 'headers', 'site', 'shebang',
36
+ // Common directory names that appear in import paths — too generic for evidence
37
+ 'src', 'lib',
38
+ // Broad task-description verbs and nouns that pollute evidence matching across every codebase
39
+ 'task', 'tasks', 'file', 'source', 'code', 'artifact', 'artifacts',
40
+ 'generic', 'feature', 'features', 'section', 'sections',
41
+ 'user', 'users', 'workflow', 'workflows', 'mode', 'modes', 'replace',
42
+ // Tool-internal vocabulary that appears in non-feature implementation files
43
+ 'audit', 'debug', 'signal', 'signals', 'log',
44
+ // English stopwords and function words that appear everywhere — not useful as evidence signals
45
+ 'only', 'must', 'what', 'which', 'kind', 'never', 'also', 'each',
46
+ 'detected', 'generated', 'existing', 'available',
47
+ // Tool-commentary vocabulary that appears in source comments but describes past/intended behavior
48
+ 'phrases', 'conceptual',
49
+ ]);
50
+
51
+ const CANONICAL_FILES = {
52
+ security: 'SECURITY.md',
53
+ readme: 'README.md',
54
+ changelog: 'CHANGELOG.md',
55
+ license: 'LICENSE'
56
+ };
57
+
58
+ // The roadmap file must never be included in the evidence pool: its task descriptions
59
+ // contain the exact vocabulary of the tasks being validated, which would cause every
60
+ // task to validate itself.
61
+ const SELF_REFERENTIAL_FILES = new Set(['ROADMAP.md']);
62
+
63
+ // Maps task-ID namespace prefix to a predicate on (normalized) file paths.
64
+ // When a task ID has a known namespace, at least one evidence file must satisfy
65
+ // the predicate — otherwise generic token overlap alone cannot pass the task.
66
+ const NAMESPACE_STRUCTURAL_PATTERNS = {
67
+ cls: (p) => /classif(?:ier|y)|archetype/.test(p),
68
+ dsg: (p) => /generator[/\\](?:domain|web|landing|profiles?)|(?:domain|web|landing)[/\\](?:profile|generator)/.test(p),
69
+ evh2: (p) => p.includes('/validator/') || p.includes('\\validator\\'),
70
+ cst: (p) => /smoke|integration[-_]test|e2e/.test(p),
71
+ uxf: (p) => p.includes('/renderer/') || p.includes('\\renderer\\') || /renderer\.[jt]sx?$/.test(p),
72
+ cfgo: (p) => /config[/\\]|schema[/\\]|config\.[jt]s$|schema\.[jt]s$/.test(p),
73
+ doc3: (p) => /(?:^|[/\\])docs[/\\]|readme\.md$/i.test(p),
74
+ };
75
+
76
+ // Test fixture directories contain synthetic code created to drive test scenarios,
77
+ // not real implementations. Including them pollutes the evidence pool with vocabulary
78
+ // that was deliberately seeded for testing purposes (e.g. namespace-vocab fixtures).
79
+ function isFixturePath(relativePath) {
80
+ return /(?:^|[/\\])fixtures[/\\]/.test(relativePath);
81
+ }
82
+
83
+ function readFileIndex(projectRoot, files) {
84
+ const index = [];
85
+ for (const relativePath of files) {
86
+ if (SELF_REFERENTIAL_FILES.has(relativePath)) continue;
87
+ if (isFixturePath(relativePath)) continue;
88
+
89
+ const absolutePath = path.resolve(projectRoot, relativePath);
90
+ const ext = path.extname(relativePath).toLowerCase();
91
+ let content = '';
92
+ try {
93
+ const buffer = fs.readFileSync(absolutePath);
94
+ if (buffer.length > 512 * 1024) {
95
+ continue;
96
+ }
97
+ content = buffer.toString('utf8');
98
+ } catch {
99
+ continue;
100
+ }
101
+
102
+ index.push({
103
+ relativePath,
104
+ absolutePath,
105
+ ext,
106
+ content,
107
+ isTestFile: /(^|\/)(__tests__|tests)\//.test(relativePath) || /\.test\.|\.spec\.|_test\.go$/.test(relativePath)
108
+ });
109
+ }
110
+ return index;
111
+ }
112
+
113
+ const KNOWN_PATH_ROOTS = [
114
+ 'src/', 'lib/', 'bin/', 'test/', 'tests/', 'docs/', 'scripts/',
115
+ 'packages/', 'apps/', 'tools/', '.github/', 'roadmap-skill/'
116
+ ];
117
+
118
+ function hasFileExtension(token) {
119
+ const lastSegment = token.replace(/\\/g, '/').split('/').pop() || '';
120
+ return /\.[A-Za-z0-9]{1,10}$/.test(lastSegment);
121
+ }
122
+
123
+ function isLikelyPath(token) {
124
+ if (/^\.{1,2}\/|^\//.test(token)) return true;
125
+ if (hasFileExtension(token)) return true;
126
+ if (KNOWN_PATH_ROOTS.some((root) => token.startsWith(root))) return true;
127
+ // The ">= 2 slashes" rule was intentionally removed: it caused conceptual slash phrases
128
+ // like "code/test/artifact" or "build/test/deploy" to be treated as file paths.
129
+ // Real multi-segment paths are caught by the extension or known-root rules above.
130
+ return false;
131
+ }
132
+
133
+ // Matches standalone filenames without a slash — e.g. "roadmap-skill.config.json",
134
+ // "package.json", "vite.config.ts". These are path references whose component tokens
135
+ // (e.g. "roadmap", "skill") must be excluded from code evidence scoring to prevent
136
+ // circular vocabulary: a task mentioning a filename would otherwise score hits in any
137
+ // source file that happens to reference the same filename for unrelated reasons.
138
+ // Numeric-only tokens like "1.0.0" or "v0.8" are excluded via the leading-digit guard.
139
+ const STANDALONE_FILE_RE = /\b([A-Za-z][A-Za-z0-9_.+-]*\.[A-Za-z0-9]{2,10})\b/g;
140
+ const KNOWN_FILE_EXTENSIONS = new Set([
141
+ '.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.py', '.go', '.rs',
142
+ '.java', '.kt', '.swift', '.rb', '.php', '.cs', '.json', '.yaml', '.yml',
143
+ '.toml', '.md', '.txt', '.sh', '.bash', '.env', '.html', '.css', '.scss', '.lock'
144
+ ]);
145
+
146
+ function hasKnownFileExtension(token) {
147
+ const lastDot = token.lastIndexOf('.');
148
+ if (lastDot < 0) return false;
149
+ return KNOWN_FILE_EXTENSIONS.has(token.slice(lastDot).toLowerCase());
150
+ }
151
+
152
+ function extractExplicitPaths(text) {
153
+ const results = new Set();
154
+ const quoted = String(text).match(/`([^`]+)`/g) || [];
155
+ for (const token of quoted) {
156
+ const clean = token.slice(1, -1);
157
+ if (clean.includes('/') || clean.includes('\\') || clean.includes('.')) {
158
+ results.add(clean);
159
+ }
160
+ }
161
+
162
+ const pathTokens = String(text).match(/([A-Za-z0-9_.-]+\/[A-Za-z0-9_./-]+)/g) || [];
163
+ for (const raw of pathTokens) {
164
+ const token = raw.replace(/[.,;:!?)]+$/, '');
165
+ if (isLikelyPath(token)) results.add(token);
166
+ }
167
+
168
+ return Array.from(results).sort((left, right) => left.localeCompare(right));
169
+ }
170
+
171
+ // Standalone filenames (no slash) mentioned in task prose — e.g. "roadmap-skill.config.json",
172
+ // "package.json". These are filename *references*, NOT path-existence assertions: the author
173
+ // is describing which file contains a feature, not asserting that the file must exist.
174
+ // Used only for pathDerivedToken extraction (to prevent circular vocabulary), never for
175
+ // findFilesByPathHints (which would pass any task whose config file already exists).
176
+ function extractStandaloneFilenames(text) {
177
+ const results = new Set();
178
+ STANDALONE_FILE_RE.lastIndex = 0;
179
+ let m = STANDALONE_FILE_RE.exec(String(text));
180
+ while (m) {
181
+ const token = m[1].replace(/[.,;:!?)]+$/, '');
182
+ if (hasKnownFileExtension(token) && !token.startsWith('.')) {
183
+ results.add(token);
184
+ }
185
+ m = STANDALONE_FILE_RE.exec(String(text));
186
+ }
187
+ return Array.from(results);
188
+ }
189
+
190
+ function extractSymbolHints(text) {
191
+ const symbols = new Set();
192
+ const patterns = [
193
+ /(?:function|class|method|command)\s+([A-Za-z_][A-Za-z0-9_]*)/gi,
194
+ /(?:function|module|class|command|method)\s+`([A-Za-z_][A-Za-z0-9_-]*)`/gi,
195
+ /`([A-Za-z_][A-Za-z0-9_-]*)`\s+(?:function|module|class|command|method)/gi
196
+ ];
197
+
198
+ for (const pattern of patterns) {
199
+ let match = pattern.exec(text);
200
+ while (match) {
201
+ symbols.add(match[1]);
202
+ match = pattern.exec(text);
203
+ }
204
+ }
205
+
206
+ return Array.from(symbols).sort((left, right) => left.localeCompare(right));
207
+ }
208
+
209
+ function isCodeTask(taskText) {
210
+ const normalized = String(taskText).toLowerCase();
211
+ return CODE_HINTS.some((hint) => normalized.includes(hint));
212
+ }
213
+
214
+ function isDocTask(taskText) {
215
+ const normalized = String(taskText).toLowerCase();
216
+ // Use word-boundary matching to avoid substring false positives (e.g. "specific" ≠ "spec").
217
+ const hasDocKeyword = DOC_HINTS.some((hint) => new RegExp(`(?<![a-z])${hint}(?![a-z])`).test(normalized));
218
+ if (!hasDocKeyword) return false;
219
+ // Also require a creation/update verb so that policy tasks mentioning doc files
220
+ // ("README must not be used as evidence") don't trigger doc-artifact evidence.
221
+ return /\b(add|create|write|update|init|initialize|introduce|setup|document)\b/.test(normalized);
222
+ }
223
+
224
+ function findFilesByPathHints(pathHints, fileIndex) {
225
+ const matches = [];
226
+ for (const hint of pathHints) {
227
+ const normalizedHint = hint.replace(/\\/g, '/');
228
+ const direct = fileIndex.find((file) => file.relativePath === normalizedHint);
229
+ if (direct) {
230
+ matches.push(direct.relativePath);
231
+ continue;
232
+ }
233
+
234
+ for (const file of fileIndex) {
235
+ if (file.relativePath.endsWith(normalizedHint)) {
236
+ matches.push(file.relativePath);
237
+ }
238
+ }
239
+ }
240
+ return Array.from(new Set(matches)).sort((left, right) => left.localeCompare(right));
241
+ }
242
+
243
+ function findFilesBySymbols(symbolHints, fileIndex) {
244
+ const matches = new Set();
245
+ for (const symbol of symbolHints) {
246
+ const regex = new RegExp(`\\b${escapeRegExp(symbol)}\\b`, 'i');
247
+ for (const file of fileIndex) {
248
+ if (!CODE_EXTENSIONS.has(file.ext)) {
249
+ continue;
250
+ }
251
+ if (regex.test(file.content)) {
252
+ matches.add(file.relativePath);
253
+ }
254
+ }
255
+ }
256
+ return Array.from(matches).sort((left, right) => left.localeCompare(right));
257
+ }
258
+
259
+ // Tokens extracted from a referenced file path (e.g. "roadmap-skill" from
260
+ // "roadmap-skill.config.json") must not be reused as code evidence signals.
261
+ // Those tokens appear in any file that mentions the same path — creating circular
262
+ // vocabulary where a task about "X in path/to/file" passes because the source
263
+ // code references the same path for unrelated reasons.
264
+ function extractPathDerivedTokens(pathHints) {
265
+ const tokens = new Set();
266
+ for (const hint of pathHints) {
267
+ // Char-split: "roadmap-skill.config.json" → ["roadmap", "skill", "config", "json"]
268
+ const parts = hint.replace(/[.\-_/\\]/g, ' ').toLowerCase().split(/\s+/).filter(Boolean);
269
+ for (const part of parts) {
270
+ if (part.length >= 3) tokens.add(part);
271
+ }
272
+ // Tokenizer-split: also adds compound tokens the char-split misses, e.g. "roadmap-skill"
273
+ // (the tokenizer preserves hyphens in identifiers; the char-split strips them).
274
+ for (const token of tokenize(hint)) {
275
+ if (token.length >= 3) tokens.add(token);
276
+ }
277
+ }
278
+ return tokens;
279
+ }
280
+
281
+ function findCodeEvidence(taskText, fileIndex, pathDerivedTokens = new Set()) {
282
+ const tokens = tokenize(taskText)
283
+ .filter((token) => token.length >= 3 && !GENERIC_TASK_TOKENS.has(token) && !token.endsWith('/') && !pathDerivedTokens.has(token))
284
+ .slice(0, 8);
285
+ if (tokens.length === 0) {
286
+ return [];
287
+ }
288
+
289
+ const matches = [];
290
+ for (const file of fileIndex) {
291
+ if (!CODE_EXTENSIONS.has(file.ext) || file.isTestFile) {
292
+ continue;
293
+ }
294
+
295
+ let score = 0;
296
+ const lowered = file.content.toLowerCase();
297
+ for (const token of tokens) {
298
+ if (token.length < 3) {
299
+ continue;
300
+ }
301
+ if (lowered.includes(token)) {
302
+ score += 1;
303
+ }
304
+ }
305
+
306
+ // Require more matches proportional to how many specific tokens the task has.
307
+ // Tasks with 4+ meaningful tokens need 3 files to match to prevent vocabulary overlap.
308
+ const threshold = tokens.length >= 4 ? 3 : tokens.length >= 2 ? 2 : 1;
309
+ if (score >= threshold) {
310
+ matches.push(file.relativePath);
311
+ }
312
+ }
313
+
314
+ return matches.slice(0, 20);
315
+ }
316
+
317
+ function findTestEvidence(taskText, fileIndex) {
318
+ const tokens = tokenize(taskText)
319
+ .filter((token) => token.length >= 3 && !GENERIC_TASK_TOKENS.has(token) && !token.endsWith('/'))
320
+ .slice(0, 8);
321
+
322
+ if (tokens.length === 0) return [];
323
+
324
+ // Only tokens of length >= 4 are used for import-reference matching.
325
+ // Very short tokens (e.g. "app", "web") are too generic: they appear as substrings in
326
+ // many import paths that have nothing to do with the feature being validated.
327
+ // The single-short-token fallback below handles the narrow case of one-word module names.
328
+ const importTokens = tokens.filter((token) => token.length >= 4);
329
+
330
+ const matches = [];
331
+
332
+ for (const file of fileIndex) {
333
+ if (!file.isTestFile) continue;
334
+
335
+ // A test file counts as evidence only when it imports a module whose path contains
336
+ // one of the task's meaningful tokens. Content-keyword matching is intentionally absent:
337
+ // test content (descriptions, literals) can contain future-task vocabulary,
338
+ // producing self-referential false positives.
339
+ //
340
+ // Trailing slashes are NOT stripped: "app/" is a directory reference, not a module name.
341
+ // "../src/app" (a real import) does not contain the string "app/" so it won't match.
342
+ const importRefs = (
343
+ file.content.match(/require\s*\(\s*['"`]([^'"`]+)['"`]\s*\)|from\s+['"`]([^'"`]+)['"`]/g) || []
344
+ ).join(' ').toLowerCase();
345
+
346
+ if (importTokens.length > 0 && importTokens.some((token) => importRefs.includes(token))) {
347
+ matches.push(file.relativePath);
348
+ continue;
349
+ }
350
+
351
+ // Narrow fallback: single very-short token (e.g. "app", "cli").
352
+ // Import paths for these are too short to distinguish reliably, so fall back to a
353
+ // content match — but only when there is exactly one such token (no multi-token dilution).
354
+ if (tokens.length === 1 && tokens[0].length < 4) {
355
+ const lowered = file.content.toLowerCase();
356
+ if (lowered.includes(tokens[0])) {
357
+ matches.push(file.relativePath);
358
+ }
359
+ }
360
+ }
361
+
362
+ return matches.slice(0, 20);
363
+ }
364
+
365
+ function findArtifactEvidence(taskText, fileIndex) {
366
+ const normalized = String(taskText).toLowerCase();
367
+ const files = [];
368
+ const heuristicArtifacts = [];
369
+
370
+ // Canonical file detection only applies to short tasks (≤8 words) that are about
371
+ // creating or referencing that specific file. Long sentences that merely MENTION
372
+ // "readme" or "security" in a policy/constraint context are excluded.
373
+ const wordCount = normalized.trim().split(/\s+/).length;
374
+ if (wordCount <= 8) {
375
+ for (const [keyword, filename] of Object.entries(CANONICAL_FILES)) {
376
+ // Use hyphen-aware word boundaries: "security-headers" must not match "security".
377
+ if (new RegExp(`(?<![a-z-])${keyword}(?![a-z-])`).test(normalized)) {
378
+ const hit = fileIndex.find(
379
+ (f) => f.relativePath === filename || f.relativePath.endsWith('/' + filename)
380
+ );
381
+ if (hit) {
382
+ files.push(hit.relativePath);
383
+ heuristicArtifacts.push(hit.relativePath);
384
+ }
385
+ }
386
+ }
387
+ }
388
+
389
+ if (!isDocTask(taskText)) {
390
+ return { files, heuristicArtifacts };
391
+ }
392
+
393
+ const artifactPatterns = [
394
+ /^README\.md$/i,
395
+ /^CHANGELOG\.md$/i,
396
+ /^docs\//i,
397
+ /^artifacts\//i,
398
+ /^dist\//i,
399
+ /^build\//i
400
+ ];
401
+
402
+ for (const file of fileIndex) {
403
+ if (artifactPatterns.some((pattern) => pattern.test(file.relativePath)) && !files.includes(file.relativePath)) {
404
+ files.push(file.relativePath);
405
+ }
406
+ }
407
+
408
+ return { files: files.slice(0, 20), heuristicArtifacts };
409
+ }
410
+
411
+ function extractTaskNamespace(taskId) {
412
+ if (!taskId) return null;
413
+ const match = String(taskId).match(/^([a-z][a-z0-9]*)-/);
414
+ return match ? match[1] : null;
415
+ }
416
+
417
+ function isAcceptanceCriteria(taskId) {
418
+ return /ph\d+[_-]st\d+[_-]exit/.test(String(taskId || ''));
419
+ }
420
+
421
+ // Gate: returns { applicable, passed, structuralFiles, reason }.
422
+ // For namespaces with a defined structural pattern:
423
+ // 1. If no files in fileIndex match the pattern → immediate fail.
424
+ // 2. For acceptance-criteria tasks (phN-stN-exit IDs): path match alone is enough.
425
+ // 3. For implementation tasks: feature tokens from task text must score ≥ ceil(n/2)
426
+ // against namespace-matched files, preventing vocabulary overlap from generic
427
+ // infrastructure code (io.js, generator/index.js) from serving as evidence.
428
+ function checkNamespaceStructuralEvidence(taskId, taskText, fileIndex) {
429
+ const namespace = extractTaskNamespace(taskId);
430
+ if (!namespace || !NAMESPACE_STRUCTURAL_PATTERNS[namespace]) {
431
+ return { applicable: false, passed: true, structuralFiles: [], reason: null };
432
+ }
433
+
434
+ const predicate = NAMESPACE_STRUCTURAL_PATTERNS[namespace];
435
+ const namespaceFiles = fileIndex.filter((f) => predicate(f.relativePath));
436
+
437
+ if (namespaceFiles.length === 0) {
438
+ return {
439
+ applicable: true,
440
+ passed: false,
441
+ structuralFiles: [],
442
+ reason: `namespace "${namespace}" has no implementation files`,
443
+ };
444
+ }
445
+
446
+ const featureTokens = tokenize(taskText)
447
+ .filter((t) => t.length >= 4 && !GENERIC_TASK_TOKENS.has(t) && !t.endsWith('/'))
448
+ .slice(0, 8);
449
+
450
+ if (featureTokens.length === 0) {
451
+ return {
452
+ applicable: true,
453
+ passed: true,
454
+ structuralFiles: namespaceFiles.map((f) => f.relativePath),
455
+ reason: null,
456
+ };
457
+ }
458
+
459
+ let bestScore = 0;
460
+ for (const nsFile of namespaceFiles) {
461
+ const lowered = nsFile.content.toLowerCase();
462
+ let score = 0;
463
+ for (const token of featureTokens) {
464
+ if (lowered.includes(token)) score++;
465
+ }
466
+ if (score > bestScore) bestScore = score;
467
+ }
468
+
469
+ const threshold = Math.max(1, Math.ceil(featureTokens.length / 2));
470
+ if (bestScore >= threshold) {
471
+ return {
472
+ applicable: true,
473
+ passed: true,
474
+ structuralFiles: namespaceFiles.map((f) => f.relativePath),
475
+ reason: null,
476
+ };
477
+ }
478
+
479
+ return {
480
+ applicable: true,
481
+ passed: false,
482
+ structuralFiles: namespaceFiles.map((f) => f.relativePath),
483
+ reason: `structural token score ${bestScore}/${threshold} in "${namespace}" files — token overlap insufficient`,
484
+ };
485
+ }
486
+
487
+ function evaluateRule(rule, task, context) {
488
+ if (!rule) {
489
+ return { passed: true, reasons: [], evidence: {} };
490
+ }
491
+
492
+ if (rule.when) {
493
+ const regexp = new RegExp(rule.when, 'i');
494
+ if (!regexp.test(task.text)) {
495
+ return { passed: true, reasons: [], evidence: {} };
496
+ }
497
+ }
498
+
499
+ if (typeof rule.check === 'function') {
500
+ const custom = rule.check(task, context);
501
+ if (!custom) {
502
+ return { passed: true, reasons: [], evidence: {} };
503
+ }
504
+ return {
505
+ passed: custom.passed !== false,
506
+ reasons: Array.isArray(custom.reasons) ? custom.reasons : [],
507
+ evidence: custom.evidence || {}
508
+ };
509
+ }
510
+
511
+ const reasons = [];
512
+ const evidence = {};
513
+
514
+ if (rule.type === 'file-exists' && rule.path) {
515
+ const hit = context.fileIndex.find((file) => file.relativePath === rule.path || file.relativePath.endsWith(rule.path));
516
+ if (!hit) {
517
+ reasons.push(rule.message || `missing file: ${rule.path}`);
518
+ } else {
519
+ evidence.file = hit.relativePath;
520
+ }
521
+ }
522
+
523
+ if (rule.type === 'symbol' && rule.pattern) {
524
+ const regex = new RegExp(rule.pattern, 'i');
525
+ const hit = context.fileIndex.find((file) => regex.test(file.content));
526
+ if (!hit) {
527
+ reasons.push(rule.message || `missing symbol pattern: ${rule.pattern}`);
528
+ } else {
529
+ evidence.symbol = hit.relativePath;
530
+ }
531
+ }
532
+
533
+ if (rule.type === 'artifact' && rule.path) {
534
+ const hit = context.fileIndex.find((file) => file.relativePath.startsWith(rule.path) || file.relativePath === rule.path);
535
+ if (!hit) {
536
+ reasons.push(rule.message || `missing artifact: ${rule.path}`);
537
+ } else {
538
+ evidence.artifact = hit.relativePath;
539
+ }
540
+ }
541
+
542
+ if (rule.type === 'test' && context.testFrameworks.length === 0) {
543
+ reasons.push(rule.message || 'test framework not detected');
544
+ }
545
+
546
+ return {
547
+ passed: reasons.length === 0,
548
+ reasons,
549
+ evidence
550
+ };
551
+ }
552
+
553
+ function buildValidationContext(projectRoot, config, plugins) {
554
+ const files = walkFiles(projectRoot);
555
+ const fileIndex = readFileIndex(projectRoot, files);
556
+ const testFrameworks = detectTestFrameworks(projectRoot, files);
557
+
558
+ return {
559
+ projectRoot,
560
+ config,
561
+ plugins,
562
+ files,
563
+ fileIndex,
564
+ testFrameworks
565
+ };
566
+ }
567
+
568
+ function validateTask(task, context, config, plugins) {
569
+ const pathHints = extractExplicitPaths(task.text);
570
+ const standaloneFilenames = extractStandaloneFilenames(task.text);
571
+ const symbolHints = extractSymbolHints(task.text);
572
+
573
+ const filesFromPaths = findFilesByPathHints(pathHints, context.fileIndex);
574
+ const filesFromSymbols = findFilesBySymbols(symbolHints, context.fileIndex);
575
+ // Combine path hints AND standalone filenames for token exclusion so that tokens
576
+ // derived from any referenced filename (e.g. "roadmap-skill" from
577
+ // "roadmap-skill.config.json") are excluded from code evidence scoring.
578
+ const pathDerivedTokens = extractPathDerivedTokens([...pathHints, ...standaloneFilenames]);
579
+ const filesFromCode = findCodeEvidence(task.text, context.fileIndex, pathDerivedTokens);
580
+ const filesFromTests = findTestEvidence(task.text, context.fileIndex);
581
+ const { files: filesFromArtifacts, heuristicArtifacts } = findArtifactEvidence(task.text, context.fileIndex);
582
+
583
+ const structuralCheck = checkNamespaceStructuralEvidence(task.id, task.text, context.fileIndex);
584
+
585
+ const evidence = {
586
+ code: filesFromCode.length > 0 || filesFromSymbols.length > 0,
587
+ test: filesFromTests.length > 0,
588
+ artifact: filesFromArtifacts.length > 0,
589
+ files: filesFromPaths,
590
+ symbols: filesFromSymbols,
591
+ codeFiles: filesFromCode,
592
+ testFiles: filesFromTests,
593
+ artifactFiles: filesFromArtifacts,
594
+ heuristicArtifacts,
595
+ structuralEvidence: structuralCheck.applicable ? structuralCheck.passed : null,
596
+ structuralFiles: structuralCheck.structuralFiles,
597
+ };
598
+
599
+ const reasons = [];
600
+ if (pathHints.length > 0 && filesFromPaths.length === 0) {
601
+ reasons.push(`missing referenced file(s): ${pathHints.join(', ')}`);
602
+ }
603
+ if (symbolHints.length > 0 && filesFromSymbols.length === 0) {
604
+ reasons.push(`missing symbol(s): ${symbolHints.join(', ')}`);
605
+ }
606
+
607
+ // Namespace-structural gate: for known namespaces, token overlap alone is insufficient.
608
+ // The task must have evidence files whose paths match the namespace pattern.
609
+ if (structuralCheck.applicable && !structuralCheck.passed) {
610
+ reasons.push(structuralCheck.reason || `no structural evidence for namespace "${extractTaskNamespace(task.id)}"`);
611
+ }
612
+
613
+ const hasEvidence = evidence.code || evidence.test || evidence.artifact || evidence.files.length > 0;
614
+ if (!hasEvidence && !structuralCheck.applicable) {
615
+ reasons.push('no code, test, or artifact evidence found');
616
+ } else if (!hasEvidence && structuralCheck.applicable && structuralCheck.passed) {
617
+ reasons.push('no code, test, or artifact evidence found');
618
+ }
619
+
620
+ const requiresTest = context.testFrameworks.length > 0 && isCodeTask(task.text) && !isDocTask(task.text);
621
+ if (requiresTest && !evidence.test) {
622
+ reasons.push('missing test evidence');
623
+ }
624
+
625
+ const configuredRules = Array.isArray(config.validators) ? config.validators : [];
626
+ const pluginRules = collectPluginContributions(plugins || [], 'registerValidators', context);
627
+ for (const rule of [...configuredRules, ...pluginRules]) {
628
+ const ruleResult = evaluateRule(rule, task, context);
629
+ if (!ruleResult.passed) {
630
+ reasons.push(...ruleResult.reasons);
631
+ }
632
+ }
633
+
634
+ const uniqueReasons = Array.from(new Set(reasons));
635
+ const attempted = hasEvidence || pathHints.length > 0 || symbolHints.length > 0;
636
+
637
+ const evidenceCount = [evidence.code, evidence.test, evidence.artifact].filter(Boolean).length;
638
+ const confidence = evidenceCount >= 2 ? 'high' : evidenceCount === 1 ? 'medium' : 'low';
639
+
640
+ // True when the only passing evidence is artifact/doc files and the task is not a doc task.
641
+ // Used by auditValidation to flag implementation tasks that pass solely via documentation.
642
+ const evidenceIsDocOnly = !evidence.code && !evidence.test && evidence.artifact && !isDocTask(task.text);
643
+
644
+ return {
645
+ taskId: task.id,
646
+ passed: uniqueReasons.length === 0,
647
+ confidence,
648
+ reasons: uniqueReasons,
649
+ evidence,
650
+ evidenceIsDocOnly,
651
+ requiresTest,
652
+ hasEvidence,
653
+ attempted
654
+ };
655
+ }
656
+
657
+ function validateTasks(tasks, context, config, plugins) {
658
+ const result = {};
659
+ for (const task of tasks) {
660
+ result[task.id] = validateTask(task, context, config, plugins);
661
+ }
662
+ return result;
663
+ }
664
+
665
+ function auditValidation(tasks, results) {
666
+ const checkedWithoutEvidence = [];
667
+ const readyButUnchecked = [];
668
+ const checkedWithWeakEvidence = [];
669
+ const documentationOnlyEvidenceForImplementation = [];
670
+ const checkedWithNoStructuralEvidence = [];
671
+
672
+ for (const task of tasks) {
673
+ const result = results[task.id];
674
+ if (!result) continue;
675
+
676
+ if (task.checked && !result.passed) {
677
+ checkedWithoutEvidence.push({ task, result });
678
+ }
679
+
680
+ if (!task.checked && result.passed) {
681
+ readyButUnchecked.push({ task, result });
682
+ }
683
+
684
+ if (task.checked && result.passed && result.confidence === 'low') {
685
+ checkedWithWeakEvidence.push({ task, result });
686
+ }
687
+
688
+ if (task.checked && result.passed && result.evidenceIsDocOnly) {
689
+ documentationOnlyEvidenceForImplementation.push({ task, result });
690
+ }
691
+
692
+ // Checked task that failed specifically because structural evidence is missing.
693
+ if (task.checked && !result.passed && result.evidence.structuralEvidence === false) {
694
+ checkedWithNoStructuralEvidence.push({ task, result });
695
+ }
696
+ }
697
+
698
+ return {
699
+ checkedWithoutEvidence,
700
+ readyButUnchecked,
701
+ checkedWithWeakEvidence,
702
+ documentationOnlyEvidenceForImplementation,
703
+ checkedWithNoStructuralEvidence,
704
+ };
705
+ }
706
+
707
+ function applyMinimumConfidence(results, minimumConfidence) {
708
+ const minRank = CONFIDENCE_RANK[minimumConfidence] ?? 0;
709
+ if (minRank === 0) return;
710
+ for (const result of Object.values(results)) {
711
+ if ((CONFIDENCE_RANK[result.confidence] ?? 0) < minRank) {
712
+ result.passed = false;
713
+ result.reasons = [
714
+ ...result.reasons,
715
+ `validation confidence "${result.confidence}" is below required "${minimumConfidence}"`
716
+ ];
717
+ }
718
+ }
719
+ }
720
+
721
+ module.exports = {
722
+ auditValidation,
723
+ buildValidationContext,
724
+ validateTask,
725
+ validateTasks,
726
+ CONFIDENCE_RANK,
727
+ applyMinimumConfidence,
728
+ extractTaskNamespace,
729
+ isAcceptanceCriteria,
730
+ };