roadmapsmith 0.5.1 → 0.7.0

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,420 +1,464 @@
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 CODE_EXTENSIONS = new Set([
10
- '.js', '.cjs', '.mjs', '.ts', '.tsx', '.jsx', '.py', '.go', '.rs', '.java', '.kt', '.swift', '.rb', '.php', '.cs'
11
- ]);
12
-
13
- const DOC_HINTS = ['readme', 'changelog', 'docs', 'documentation', 'spec', 'diagram', 'runbook'];
14
- const CODE_HINTS = ['implement', 'add', 'create', 'build', 'refactor', 'fix', 'module', 'function', 'api', 'endpoint', 'command'];
15
- const GENERIC_TASK_TOKENS = new Set([
16
- 'implement',
17
- 'implementation',
18
- 'module',
19
- 'function',
20
- 'class',
21
- 'method',
22
- 'command',
23
- 'create',
24
- 'add',
25
- 'build',
26
- 'refactor',
27
- 'fix',
28
- 'test',
29
- 'tests'
30
- ]);
31
-
32
- function readFileIndex(projectRoot, files) {
33
- const index = [];
34
- for (const relativePath of files) {
35
- const absolutePath = path.resolve(projectRoot, relativePath);
36
- const ext = path.extname(relativePath).toLowerCase();
37
- let content = '';
38
- try {
39
- const buffer = fs.readFileSync(absolutePath);
40
- if (buffer.length > 512 * 1024) {
41
- continue;
42
- }
43
- content = buffer.toString('utf8');
44
- } catch {
45
- continue;
46
- }
47
-
48
- index.push({
49
- relativePath,
50
- absolutePath,
51
- ext,
52
- content,
53
- isTestFile: /(^|\/)(__tests__|tests)\//.test(relativePath) || /\.test\.|\.spec\.|_test\.go$/.test(relativePath)
54
- });
55
- }
56
- return index;
57
- }
58
-
59
- const KNOWN_PATH_ROOTS = [
60
- 'src/', 'lib/', 'bin/', 'test/', 'tests/', 'docs/', 'scripts/',
61
- 'packages/', 'apps/', 'tools/', '.github/', 'roadmap-skill/'
62
- ];
63
-
64
- function hasFileExtension(token) {
65
- const lastSegment = token.replace(/\\/g, '/').split('/').pop() || '';
66
- return /\.[A-Za-z0-9]{1,10}$/.test(lastSegment);
67
- }
68
-
69
- function isLikelyPath(token) {
70
- if (/^\.{1,2}\/|^\//.test(token)) return true;
71
- if (hasFileExtension(token)) return true;
72
- if (KNOWN_PATH_ROOTS.some((root) => token.startsWith(root))) return true;
73
- if ((token.match(/\//g) || []).length >= 2) return true;
74
- return false;
75
- }
76
-
77
- function extractExplicitPaths(text) {
78
- const results = new Set();
79
- const quoted = String(text).match(/`([^`]+)`/g) || [];
80
- for (const token of quoted) {
81
- const clean = token.slice(1, -1);
82
- if (clean.includes('/') || clean.includes('\\') || clean.includes('.')) {
83
- results.add(clean);
84
- }
85
- }
86
-
87
- const pathTokens = String(text).match(/([A-Za-z0-9_.-]+\/[A-Za-z0-9_./-]+)/g) || [];
88
- for (const raw of pathTokens) {
89
- const token = raw.replace(/[.,;:!?)]+$/, '');
90
- if (isLikelyPath(token)) results.add(token);
91
- }
92
-
93
- return Array.from(results).sort((left, right) => left.localeCompare(right));
94
- }
95
-
96
- function extractSymbolHints(text) {
97
- const symbols = new Set();
98
- const patterns = [
99
- /(?:function|class|method|command)\s+([A-Za-z_][A-Za-z0-9_]*)/gi,
100
- /(?:function|module|class|command|method)\s+`([A-Za-z_][A-Za-z0-9_-]*)`/gi,
101
- /`([A-Za-z_][A-Za-z0-9_-]*)`\s+(?:function|module|class|command|method)/gi
102
- ];
103
-
104
- for (const pattern of patterns) {
105
- let match = pattern.exec(text);
106
- while (match) {
107
- symbols.add(match[1]);
108
- match = pattern.exec(text);
109
- }
110
- }
111
-
112
- return Array.from(symbols).sort((left, right) => left.localeCompare(right));
113
- }
114
-
115
- function isCodeTask(taskText) {
116
- const normalized = String(taskText).toLowerCase();
117
- return CODE_HINTS.some((hint) => normalized.includes(hint));
118
- }
119
-
120
- function isDocTask(taskText) {
121
- const normalized = String(taskText).toLowerCase();
122
- return DOC_HINTS.some((hint) => normalized.includes(hint));
123
- }
124
-
125
- function findFilesByPathHints(pathHints, fileIndex) {
126
- const matches = [];
127
- for (const hint of pathHints) {
128
- const normalizedHint = hint.replace(/\\/g, '/');
129
- const direct = fileIndex.find((file) => file.relativePath === normalizedHint);
130
- if (direct) {
131
- matches.push(direct.relativePath);
132
- continue;
133
- }
134
-
135
- for (const file of fileIndex) {
136
- if (file.relativePath.endsWith(normalizedHint)) {
137
- matches.push(file.relativePath);
138
- }
139
- }
140
- }
141
- return Array.from(new Set(matches)).sort((left, right) => left.localeCompare(right));
142
- }
143
-
144
- function findFilesBySymbols(symbolHints, fileIndex) {
145
- const matches = new Set();
146
- for (const symbol of symbolHints) {
147
- const regex = new RegExp(`\\b${escapeRegExp(symbol)}\\b`, 'i');
148
- for (const file of fileIndex) {
149
- if (!CODE_EXTENSIONS.has(file.ext)) {
150
- continue;
151
- }
152
- if (regex.test(file.content)) {
153
- matches.add(file.relativePath);
154
- }
155
- }
156
- }
157
- return Array.from(matches).sort((left, right) => left.localeCompare(right));
158
- }
159
-
160
- function findCodeEvidence(taskText, fileIndex) {
161
- const tokens = tokenize(taskText)
162
- .filter((token) => token.length >= 3 && !GENERIC_TASK_TOKENS.has(token))
163
- .slice(0, 8);
164
- if (tokens.length === 0) {
165
- return [];
166
- }
167
-
168
- const matches = [];
169
- for (const file of fileIndex) {
170
- if (!CODE_EXTENSIONS.has(file.ext) || file.isTestFile) {
171
- continue;
172
- }
173
-
174
- let score = 0;
175
- const lowered = file.content.toLowerCase();
176
- for (const token of tokens) {
177
- if (token.length < 3) {
178
- continue;
179
- }
180
- if (lowered.includes(token)) {
181
- score += 1;
182
- }
183
- }
184
-
185
- const threshold = tokens.length === 1 ? 1 : 2;
186
- if (score >= threshold) {
187
- matches.push(file.relativePath);
188
- }
189
- }
190
-
191
- return matches.slice(0, 20);
192
- }
193
-
194
- function findTestEvidence(taskText, fileIndex) {
195
- const tokens = tokenize(taskText)
196
- .filter((token) => token.length >= 3 && !GENERIC_TASK_TOKENS.has(token))
197
- .slice(0, 8);
198
- const matches = [];
199
-
200
- for (const file of fileIndex) {
201
- if (!file.isTestFile) {
202
- continue;
203
- }
204
- const lowered = file.content.toLowerCase();
205
- const hasMatch = tokens.some((token) => lowered.includes(token));
206
- if (hasMatch) {
207
- matches.push(file.relativePath);
208
- }
209
- }
210
-
211
- return matches.slice(0, 20);
212
- }
213
-
214
- function findArtifactEvidence(taskText, fileIndex) {
215
- const normalized = String(taskText).toLowerCase();
216
- const matches = [];
217
-
218
- if (!isDocTask(taskText) && !normalized.includes('artifact') && !normalized.includes('release')) {
219
- return matches;
220
- }
221
-
222
- const artifactPatterns = [
223
- /^README\.md$/i,
224
- /^CHANGELOG\.md$/i,
225
- /^docs\//i,
226
- /^artifacts\//i,
227
- /^dist\//i,
228
- /^build\//i
229
- ];
230
-
231
- for (const file of fileIndex) {
232
- if (artifactPatterns.some((pattern) => pattern.test(file.relativePath))) {
233
- matches.push(file.relativePath);
234
- }
235
- }
236
-
237
- return matches.slice(0, 20);
238
- }
239
-
240
- function evaluateRule(rule, task, context) {
241
- if (!rule) {
242
- return { passed: true, reasons: [], evidence: {} };
243
- }
244
-
245
- if (rule.when) {
246
- const regexp = new RegExp(rule.when, 'i');
247
- if (!regexp.test(task.text)) {
248
- return { passed: true, reasons: [], evidence: {} };
249
- }
250
- }
251
-
252
- if (typeof rule.check === 'function') {
253
- const custom = rule.check(task, context);
254
- if (!custom) {
255
- return { passed: true, reasons: [], evidence: {} };
256
- }
257
- return {
258
- passed: custom.passed !== false,
259
- reasons: Array.isArray(custom.reasons) ? custom.reasons : [],
260
- evidence: custom.evidence || {}
261
- };
262
- }
263
-
264
- const reasons = [];
265
- const evidence = {};
266
-
267
- if (rule.type === 'file-exists' && rule.path) {
268
- const hit = context.fileIndex.find((file) => file.relativePath === rule.path || file.relativePath.endsWith(rule.path));
269
- if (!hit) {
270
- reasons.push(rule.message || `missing file: ${rule.path}`);
271
- } else {
272
- evidence.file = hit.relativePath;
273
- }
274
- }
275
-
276
- if (rule.type === 'symbol' && rule.pattern) {
277
- const regex = new RegExp(rule.pattern, 'i');
278
- const hit = context.fileIndex.find((file) => regex.test(file.content));
279
- if (!hit) {
280
- reasons.push(rule.message || `missing symbol pattern: ${rule.pattern}`);
281
- } else {
282
- evidence.symbol = hit.relativePath;
283
- }
284
- }
285
-
286
- if (rule.type === 'artifact' && rule.path) {
287
- const hit = context.fileIndex.find((file) => file.relativePath.startsWith(rule.path) || file.relativePath === rule.path);
288
- if (!hit) {
289
- reasons.push(rule.message || `missing artifact: ${rule.path}`);
290
- } else {
291
- evidence.artifact = hit.relativePath;
292
- }
293
- }
294
-
295
- if (rule.type === 'test' && context.testFrameworks.length === 0) {
296
- reasons.push(rule.message || 'test framework not detected');
297
- }
298
-
299
- return {
300
- passed: reasons.length === 0,
301
- reasons,
302
- evidence
303
- };
304
- }
305
-
306
- function buildValidationContext(projectRoot, config, plugins) {
307
- const files = walkFiles(projectRoot);
308
- const fileIndex = readFileIndex(projectRoot, files);
309
- const testFrameworks = detectTestFrameworks(projectRoot, files);
310
-
311
- return {
312
- projectRoot,
313
- config,
314
- plugins,
315
- files,
316
- fileIndex,
317
- testFrameworks
318
- };
319
- }
320
-
321
- function validateTask(task, context, config, plugins) {
322
- const pathHints = extractExplicitPaths(task.text);
323
- const symbolHints = extractSymbolHints(task.text);
324
-
325
- const filesFromPaths = findFilesByPathHints(pathHints, context.fileIndex);
326
- const filesFromSymbols = findFilesBySymbols(symbolHints, context.fileIndex);
327
- const filesFromCode = findCodeEvidence(task.text, context.fileIndex);
328
- const filesFromTests = findTestEvidence(task.text, context.fileIndex);
329
- const filesFromArtifacts = findArtifactEvidence(task.text, context.fileIndex);
330
-
331
- const evidence = {
332
- code: filesFromCode.length > 0 || filesFromSymbols.length > 0,
333
- test: filesFromTests.length > 0,
334
- artifact: filesFromArtifacts.length > 0,
335
- files: filesFromPaths,
336
- symbols: filesFromSymbols,
337
- codeFiles: filesFromCode,
338
- testFiles: filesFromTests,
339
- artifactFiles: filesFromArtifacts
340
- };
341
-
342
- const reasons = [];
343
- if (pathHints.length > 0 && filesFromPaths.length === 0) {
344
- reasons.push(`missing referenced file(s): ${pathHints.join(', ')}`);
345
- }
346
- if (symbolHints.length > 0 && filesFromSymbols.length === 0) {
347
- reasons.push(`missing symbol(s): ${symbolHints.join(', ')}`);
348
- }
349
-
350
- const hasEvidence = evidence.code || evidence.test || evidence.artifact || evidence.files.length > 0;
351
- if (!hasEvidence) {
352
- reasons.push('no code, test, or artifact evidence found');
353
- }
354
-
355
- const requiresTest = context.testFrameworks.length > 0 && isCodeTask(task.text) && !isDocTask(task.text);
356
- if (requiresTest && !evidence.test) {
357
- reasons.push('missing test evidence');
358
- }
359
-
360
- const configuredRules = Array.isArray(config.validators) ? config.validators : [];
361
- const pluginRules = collectPluginContributions(plugins || [], 'registerValidators', context);
362
- for (const rule of [...configuredRules, ...pluginRules]) {
363
- const ruleResult = evaluateRule(rule, task, context);
364
- if (!ruleResult.passed) {
365
- reasons.push(...ruleResult.reasons);
366
- }
367
- }
368
-
369
- const uniqueReasons = Array.from(new Set(reasons));
370
-
371
- return {
372
- taskId: task.id,
373
- passed: uniqueReasons.length === 0,
374
- reasons: uniqueReasons,
375
- evidence,
376
- requiresTest,
377
- hasEvidence,
378
- attempted: hasEvidence || pathHints.length > 0 || symbolHints.length > 0
379
- };
380
- }
381
-
382
- function validateTasks(tasks, context, config, plugins) {
383
- const result = {};
384
- for (const task of tasks) {
385
- result[task.id] = validateTask(task, context, config, plugins);
386
- }
387
- return result;
388
- }
389
-
390
- function auditValidation(tasks, results) {
391
- const checkedWithoutEvidence = [];
392
- const readyButUnchecked = [];
393
-
394
- for (const task of tasks) {
395
- const result = results[task.id];
396
- if (!result) {
397
- continue;
398
- }
399
-
400
- if (task.checked && !result.passed) {
401
- checkedWithoutEvidence.push({ task, result });
402
- }
403
-
404
- if (!task.checked && result.passed) {
405
- readyButUnchecked.push({ task, result });
406
- }
407
- }
408
-
409
- return {
410
- checkedWithoutEvidence,
411
- readyButUnchecked
412
- };
413
- }
414
-
415
- module.exports = {
416
- auditValidation,
417
- buildValidationContext,
418
- validateTask,
419
- validateTasks
420
- };
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
+ const DOC_HINTS = ['readme', 'changelog', 'docs', 'documentation', 'spec', 'diagram', 'runbook'];
16
+ const CODE_HINTS = ['implement', 'add', 'create', 'build', 'refactor', 'fix', 'module', 'function', 'api', 'endpoint', 'command'];
17
+ const GENERIC_TASK_TOKENS = new Set([
18
+ 'implement',
19
+ 'implementation',
20
+ 'module',
21
+ 'function',
22
+ 'class',
23
+ 'method',
24
+ 'command',
25
+ 'create',
26
+ 'add',
27
+ 'build',
28
+ 'refactor',
29
+ 'fix',
30
+ 'test',
31
+ 'tests'
32
+ ]);
33
+
34
+ const CANONICAL_FILES = {
35
+ security: 'SECURITY.md',
36
+ readme: 'README.md',
37
+ changelog: 'CHANGELOG.md',
38
+ license: 'LICENSE'
39
+ };
40
+
41
+ function readFileIndex(projectRoot, files) {
42
+ const index = [];
43
+ for (const relativePath of files) {
44
+ const absolutePath = path.resolve(projectRoot, relativePath);
45
+ const ext = path.extname(relativePath).toLowerCase();
46
+ let content = '';
47
+ try {
48
+ const buffer = fs.readFileSync(absolutePath);
49
+ if (buffer.length > 512 * 1024) {
50
+ continue;
51
+ }
52
+ content = buffer.toString('utf8');
53
+ } catch {
54
+ continue;
55
+ }
56
+
57
+ index.push({
58
+ relativePath,
59
+ absolutePath,
60
+ ext,
61
+ content,
62
+ isTestFile: /(^|\/)(__tests__|tests)\//.test(relativePath) || /\.test\.|\.spec\.|_test\.go$/.test(relativePath)
63
+ });
64
+ }
65
+ return index;
66
+ }
67
+
68
+ const KNOWN_PATH_ROOTS = [
69
+ 'src/', 'lib/', 'bin/', 'test/', 'tests/', 'docs/', 'scripts/',
70
+ 'packages/', 'apps/', 'tools/', '.github/', 'roadmap-skill/'
71
+ ];
72
+
73
+ function hasFileExtension(token) {
74
+ const lastSegment = token.replace(/\\/g, '/').split('/').pop() || '';
75
+ return /\.[A-Za-z0-9]{1,10}$/.test(lastSegment);
76
+ }
77
+
78
+ function isLikelyPath(token) {
79
+ if (/^\.{1,2}\/|^\//.test(token)) return true;
80
+ if (hasFileExtension(token)) return true;
81
+ if (KNOWN_PATH_ROOTS.some((root) => token.startsWith(root))) return true;
82
+ if ((token.match(/\//g) || []).length >= 2) return true;
83
+ return false;
84
+ }
85
+
86
+ function extractExplicitPaths(text) {
87
+ const results = new Set();
88
+ const quoted = String(text).match(/`([^`]+)`/g) || [];
89
+ for (const token of quoted) {
90
+ const clean = token.slice(1, -1);
91
+ if (clean.includes('/') || clean.includes('\\') || clean.includes('.')) {
92
+ results.add(clean);
93
+ }
94
+ }
95
+
96
+ const pathTokens = String(text).match(/([A-Za-z0-9_.-]+\/[A-Za-z0-9_./-]+)/g) || [];
97
+ for (const raw of pathTokens) {
98
+ const token = raw.replace(/[.,;:!?)]+$/, '');
99
+ if (isLikelyPath(token)) results.add(token);
100
+ }
101
+
102
+ return Array.from(results).sort((left, right) => left.localeCompare(right));
103
+ }
104
+
105
+ function extractSymbolHints(text) {
106
+ const symbols = new Set();
107
+ const patterns = [
108
+ /(?:function|class|method|command)\s+([A-Za-z_][A-Za-z0-9_]*)/gi,
109
+ /(?:function|module|class|command|method)\s+`([A-Za-z_][A-Za-z0-9_-]*)`/gi,
110
+ /`([A-Za-z_][A-Za-z0-9_-]*)`\s+(?:function|module|class|command|method)/gi
111
+ ];
112
+
113
+ for (const pattern of patterns) {
114
+ let match = pattern.exec(text);
115
+ while (match) {
116
+ symbols.add(match[1]);
117
+ match = pattern.exec(text);
118
+ }
119
+ }
120
+
121
+ return Array.from(symbols).sort((left, right) => left.localeCompare(right));
122
+ }
123
+
124
+ function isCodeTask(taskText) {
125
+ const normalized = String(taskText).toLowerCase();
126
+ return CODE_HINTS.some((hint) => normalized.includes(hint));
127
+ }
128
+
129
+ function isDocTask(taskText) {
130
+ const normalized = String(taskText).toLowerCase();
131
+ return DOC_HINTS.some((hint) => normalized.includes(hint));
132
+ }
133
+
134
+ function findFilesByPathHints(pathHints, fileIndex) {
135
+ const matches = [];
136
+ for (const hint of pathHints) {
137
+ const normalizedHint = hint.replace(/\\/g, '/');
138
+ const direct = fileIndex.find((file) => file.relativePath === normalizedHint);
139
+ if (direct) {
140
+ matches.push(direct.relativePath);
141
+ continue;
142
+ }
143
+
144
+ for (const file of fileIndex) {
145
+ if (file.relativePath.endsWith(normalizedHint)) {
146
+ matches.push(file.relativePath);
147
+ }
148
+ }
149
+ }
150
+ return Array.from(new Set(matches)).sort((left, right) => left.localeCompare(right));
151
+ }
152
+
153
+ function findFilesBySymbols(symbolHints, fileIndex) {
154
+ const matches = new Set();
155
+ for (const symbol of symbolHints) {
156
+ const regex = new RegExp(`\\b${escapeRegExp(symbol)}\\b`, 'i');
157
+ for (const file of fileIndex) {
158
+ if (!CODE_EXTENSIONS.has(file.ext)) {
159
+ continue;
160
+ }
161
+ if (regex.test(file.content)) {
162
+ matches.add(file.relativePath);
163
+ }
164
+ }
165
+ }
166
+ return Array.from(matches).sort((left, right) => left.localeCompare(right));
167
+ }
168
+
169
+ function findCodeEvidence(taskText, fileIndex) {
170
+ const tokens = tokenize(taskText)
171
+ .filter((token) => token.length >= 3 && !GENERIC_TASK_TOKENS.has(token))
172
+ .slice(0, 8);
173
+ if (tokens.length === 0) {
174
+ return [];
175
+ }
176
+
177
+ const matches = [];
178
+ for (const file of fileIndex) {
179
+ if (!CODE_EXTENSIONS.has(file.ext) || file.isTestFile) {
180
+ continue;
181
+ }
182
+
183
+ let score = 0;
184
+ const lowered = file.content.toLowerCase();
185
+ for (const token of tokens) {
186
+ if (token.length < 3) {
187
+ continue;
188
+ }
189
+ if (lowered.includes(token)) {
190
+ score += 1;
191
+ }
192
+ }
193
+
194
+ const threshold = tokens.length === 1 ? 1 : 2;
195
+ if (score >= threshold) {
196
+ matches.push(file.relativePath);
197
+ }
198
+ }
199
+
200
+ return matches.slice(0, 20);
201
+ }
202
+
203
+ function findTestEvidence(taskText, fileIndex) {
204
+ const tokens = tokenize(taskText)
205
+ .filter((token) => token.length >= 3 && !GENERIC_TASK_TOKENS.has(token))
206
+ .slice(0, 8);
207
+ const matches = [];
208
+
209
+ for (const file of fileIndex) {
210
+ if (!file.isTestFile) {
211
+ continue;
212
+ }
213
+ const lowered = file.content.toLowerCase();
214
+ const hasMatch = tokens.some((token) => lowered.includes(token));
215
+ if (hasMatch) {
216
+ matches.push(file.relativePath);
217
+ }
218
+ }
219
+
220
+ return matches.slice(0, 20);
221
+ }
222
+
223
+ function findArtifactEvidence(taskText, fileIndex) {
224
+ const normalized = String(taskText).toLowerCase();
225
+ const files = [];
226
+ const heuristicArtifacts = [];
227
+
228
+ for (const [keyword, filename] of Object.entries(CANONICAL_FILES)) {
229
+ if (normalized.includes(keyword)) {
230
+ const hit = fileIndex.find(
231
+ (f) => f.relativePath === filename || f.relativePath.endsWith('/' + filename)
232
+ );
233
+ if (hit) {
234
+ files.push(hit.relativePath);
235
+ heuristicArtifacts.push(hit.relativePath);
236
+ }
237
+ }
238
+ }
239
+
240
+ if (!isDocTask(taskText) && !normalized.includes('artifact') && !normalized.includes('release')) {
241
+ return { files, heuristicArtifacts };
242
+ }
243
+
244
+ const artifactPatterns = [
245
+ /^README\.md$/i,
246
+ /^CHANGELOG\.md$/i,
247
+ /^docs\//i,
248
+ /^artifacts\//i,
249
+ /^dist\//i,
250
+ /^build\//i
251
+ ];
252
+
253
+ for (const file of fileIndex) {
254
+ if (artifactPatterns.some((pattern) => pattern.test(file.relativePath)) && !files.includes(file.relativePath)) {
255
+ files.push(file.relativePath);
256
+ }
257
+ }
258
+
259
+ return { files: files.slice(0, 20), heuristicArtifacts };
260
+ }
261
+
262
+ function evaluateRule(rule, task, context) {
263
+ if (!rule) {
264
+ return { passed: true, reasons: [], evidence: {} };
265
+ }
266
+
267
+ if (rule.when) {
268
+ const regexp = new RegExp(rule.when, 'i');
269
+ if (!regexp.test(task.text)) {
270
+ return { passed: true, reasons: [], evidence: {} };
271
+ }
272
+ }
273
+
274
+ if (typeof rule.check === 'function') {
275
+ const custom = rule.check(task, context);
276
+ if (!custom) {
277
+ return { passed: true, reasons: [], evidence: {} };
278
+ }
279
+ return {
280
+ passed: custom.passed !== false,
281
+ reasons: Array.isArray(custom.reasons) ? custom.reasons : [],
282
+ evidence: custom.evidence || {}
283
+ };
284
+ }
285
+
286
+ const reasons = [];
287
+ const evidence = {};
288
+
289
+ if (rule.type === 'file-exists' && rule.path) {
290
+ const hit = context.fileIndex.find((file) => file.relativePath === rule.path || file.relativePath.endsWith(rule.path));
291
+ if (!hit) {
292
+ reasons.push(rule.message || `missing file: ${rule.path}`);
293
+ } else {
294
+ evidence.file = hit.relativePath;
295
+ }
296
+ }
297
+
298
+ if (rule.type === 'symbol' && rule.pattern) {
299
+ const regex = new RegExp(rule.pattern, 'i');
300
+ const hit = context.fileIndex.find((file) => regex.test(file.content));
301
+ if (!hit) {
302
+ reasons.push(rule.message || `missing symbol pattern: ${rule.pattern}`);
303
+ } else {
304
+ evidence.symbol = hit.relativePath;
305
+ }
306
+ }
307
+
308
+ if (rule.type === 'artifact' && rule.path) {
309
+ const hit = context.fileIndex.find((file) => file.relativePath.startsWith(rule.path) || file.relativePath === rule.path);
310
+ if (!hit) {
311
+ reasons.push(rule.message || `missing artifact: ${rule.path}`);
312
+ } else {
313
+ evidence.artifact = hit.relativePath;
314
+ }
315
+ }
316
+
317
+ if (rule.type === 'test' && context.testFrameworks.length === 0) {
318
+ reasons.push(rule.message || 'test framework not detected');
319
+ }
320
+
321
+ return {
322
+ passed: reasons.length === 0,
323
+ reasons,
324
+ evidence
325
+ };
326
+ }
327
+
328
+ function buildValidationContext(projectRoot, config, plugins) {
329
+ const files = walkFiles(projectRoot);
330
+ const fileIndex = readFileIndex(projectRoot, files);
331
+ const testFrameworks = detectTestFrameworks(projectRoot, files);
332
+
333
+ return {
334
+ projectRoot,
335
+ config,
336
+ plugins,
337
+ files,
338
+ fileIndex,
339
+ testFrameworks
340
+ };
341
+ }
342
+
343
+ function validateTask(task, context, config, plugins) {
344
+ const pathHints = extractExplicitPaths(task.text);
345
+ const symbolHints = extractSymbolHints(task.text);
346
+
347
+ const filesFromPaths = findFilesByPathHints(pathHints, context.fileIndex);
348
+ const filesFromSymbols = findFilesBySymbols(symbolHints, context.fileIndex);
349
+ const filesFromCode = findCodeEvidence(task.text, context.fileIndex);
350
+ const filesFromTests = findTestEvidence(task.text, context.fileIndex);
351
+ const { files: filesFromArtifacts, heuristicArtifacts } = findArtifactEvidence(task.text, context.fileIndex);
352
+
353
+ const evidence = {
354
+ code: filesFromCode.length > 0 || filesFromSymbols.length > 0,
355
+ test: filesFromTests.length > 0,
356
+ artifact: filesFromArtifacts.length > 0,
357
+ files: filesFromPaths,
358
+ symbols: filesFromSymbols,
359
+ codeFiles: filesFromCode,
360
+ testFiles: filesFromTests,
361
+ artifactFiles: filesFromArtifacts,
362
+ heuristicArtifacts
363
+ };
364
+
365
+ const reasons = [];
366
+ if (pathHints.length > 0 && filesFromPaths.length === 0) {
367
+ reasons.push(`missing referenced file(s): ${pathHints.join(', ')}`);
368
+ }
369
+ if (symbolHints.length > 0 && filesFromSymbols.length === 0) {
370
+ reasons.push(`missing symbol(s): ${symbolHints.join(', ')}`);
371
+ }
372
+
373
+ const hasEvidence = evidence.code || evidence.test || evidence.artifact || evidence.files.length > 0;
374
+ if (!hasEvidence) {
375
+ reasons.push('no code, test, or artifact evidence found');
376
+ }
377
+
378
+ const requiresTest = context.testFrameworks.length > 0 && isCodeTask(task.text) && !isDocTask(task.text);
379
+ if (requiresTest && !evidence.test) {
380
+ reasons.push('missing test evidence');
381
+ }
382
+
383
+ const configuredRules = Array.isArray(config.validators) ? config.validators : [];
384
+ const pluginRules = collectPluginContributions(plugins || [], 'registerValidators', context);
385
+ for (const rule of [...configuredRules, ...pluginRules]) {
386
+ const ruleResult = evaluateRule(rule, task, context);
387
+ if (!ruleResult.passed) {
388
+ reasons.push(...ruleResult.reasons);
389
+ }
390
+ }
391
+
392
+ const uniqueReasons = Array.from(new Set(reasons));
393
+ const attempted = hasEvidence || pathHints.length > 0 || symbolHints.length > 0;
394
+
395
+ const evidenceCount = [evidence.code, evidence.test, evidence.artifact].filter(Boolean).length;
396
+ const confidence = evidenceCount >= 2 ? 'high' : evidenceCount === 1 ? 'medium' : 'low';
397
+
398
+ return {
399
+ taskId: task.id,
400
+ passed: uniqueReasons.length === 0,
401
+ confidence,
402
+ reasons: uniqueReasons,
403
+ evidence,
404
+ requiresTest,
405
+ hasEvidence,
406
+ attempted
407
+ };
408
+ }
409
+
410
+ function validateTasks(tasks, context, config, plugins) {
411
+ const result = {};
412
+ for (const task of tasks) {
413
+ result[task.id] = validateTask(task, context, config, plugins);
414
+ }
415
+ return result;
416
+ }
417
+
418
+ function auditValidation(tasks, results) {
419
+ const checkedWithoutEvidence = [];
420
+ const readyButUnchecked = [];
421
+
422
+ for (const task of tasks) {
423
+ const result = results[task.id];
424
+ if (!result) {
425
+ continue;
426
+ }
427
+
428
+ if (task.checked && !result.passed) {
429
+ checkedWithoutEvidence.push({ task, result });
430
+ }
431
+
432
+ if (!task.checked && result.passed) {
433
+ readyButUnchecked.push({ task, result });
434
+ }
435
+ }
436
+
437
+ return {
438
+ checkedWithoutEvidence,
439
+ readyButUnchecked
440
+ };
441
+ }
442
+
443
+ function applyMinimumConfidence(results, minimumConfidence) {
444
+ const minRank = CONFIDENCE_RANK[minimumConfidence] ?? 0;
445
+ if (minRank === 0) return;
446
+ for (const result of Object.values(results)) {
447
+ if ((CONFIDENCE_RANK[result.confidence] ?? 0) < minRank) {
448
+ result.passed = false;
449
+ result.reasons = [
450
+ ...result.reasons,
451
+ `validation confidence "${result.confidence}" is below required "${minimumConfidence}"`
452
+ ];
453
+ }
454
+ }
455
+ }
456
+
457
+ module.exports = {
458
+ auditValidation,
459
+ buildValidationContext,
460
+ validateTask,
461
+ validateTasks,
462
+ CONFIDENCE_RANK,
463
+ applyMinimumConfidence
464
+ };