feat(ultraplan-local): Spor 1 wave 2 — 5 validators + doc-consistency, 108 tests grønn [skip-docs]
5 nye validator-moduler (alle m/ CLI-shim for invokering fra commands): - brief-validator.mjs — frontmatter (type, brief_version, task, slug, research_topics, research_status), state machine (research_topics > 0 + skipped requires brief_quality: partial), body sections (Intent/Goal/Success Criteria) - research-validator.mjs — type=ultraresearch-brief, confidence ∈ [0,1], dimensions ≥ 1, body sections, --dir mode for batch validering - plan-validator.mjs — wrapper over plan-schema + manifest-yaml; håndhever step-count == manifest-count, plan_version=1.7 - progress-validator.mjs — schema_version, status enum, current_step in range, step shape, checkResumeReadiness - architecture-discovery.mjs — EKSTERN KONTRAKT: drift-WARN ikke drift-FAIL; tolererer non-canonical filnavn, surfacer loose files som warnings Doc-consistency-test pinning prose vs source-of-truth: - agents/*.md count == CLAUDE.md agent-tabell rader - commands/*.md mentioned i CLAUDE.md - command frontmatter.name == filnavn - templates/plan-template.md plan_version 1.7 invariant - settings.json kun kjente scopes (ultraplan, ultraresearch) - settings.json ingen exploration eller agentTeam (vestigial guard etter Spor 0) - CLAUDE.md refererer alle 4 pipeline-commands Wave 1 + Wave 2 = 108 tester grønn. [skip-docs]: Test-infrastrukturen er ikke user-facing før Spor 1 wiring lander; README/CLAUDE.md oppdateres når commands faktisk endrer atferd (neste commit). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
7219a5fe20
commit
65c9242160
11 changed files with 999 additions and 0 deletions
|
|
@ -0,0 +1,94 @@
|
|||
// lib/validators/architecture-discovery.mjs
|
||||
// EXTERNAL CONTRACT — drift-WARN, never drift-FAIL.
|
||||
//
|
||||
// The architecture/ directory is owned by the separate `ultra-cc-architect`
|
||||
// plugin. ultraplan-local validates only DISCOVERY (file present at canonical
|
||||
// path) and tolerates internal-format drift via warnings.
|
||||
//
|
||||
// Never read body content beyond first heading. Never assert frontmatter shape.
|
||||
|
||||
import { existsSync, readdirSync, statSync, readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { issue } from '../util/result.mjs';
|
||||
|
||||
const CANONICAL_OVERVIEW = 'overview.md';
|
||||
const CANONICAL_GAPS = 'gaps.md';
|
||||
const KNOWN_ALTERNATIVES = ['architecture-overview.md', 'overview.markdown', 'README.md'];
|
||||
|
||||
export function discoverArchitecture(projectDir) {
|
||||
const archDir = projectDir ? join(projectDir, 'architecture') : null;
|
||||
const result = {
|
||||
found: false,
|
||||
overview: null,
|
||||
gaps: null,
|
||||
looseFiles: [],
|
||||
warnings: [],
|
||||
};
|
||||
|
||||
if (!archDir || !existsSync(archDir) || !statSync(archDir).isDirectory()) {
|
||||
return result;
|
||||
}
|
||||
|
||||
const overviewPath = join(archDir, CANONICAL_OVERVIEW);
|
||||
if (existsSync(overviewPath) && statSync(overviewPath).isFile()) {
|
||||
result.found = true;
|
||||
result.overview = overviewPath;
|
||||
} else {
|
||||
for (const alt of KNOWN_ALTERNATIVES) {
|
||||
const altPath = join(archDir, alt);
|
||||
if (existsSync(altPath) && statSync(altPath).isFile()) {
|
||||
result.found = true;
|
||||
result.overview = altPath;
|
||||
result.warnings.push(issue(
|
||||
'ARCH_NON_CANONICAL_OVERVIEW',
|
||||
`Architecture file at non-canonical path: ${alt}`,
|
||||
`Canonical contract is architecture/overview.md. The ultra-cc-architect plugin may have drifted; this is a warning, not a blocker.`,
|
||||
));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const gapsPath = join(archDir, CANONICAL_GAPS);
|
||||
if (existsSync(gapsPath) && statSync(gapsPath).isFile()) result.gaps = gapsPath;
|
||||
|
||||
const all = readdirSync(archDir).filter(f => /\.md$/i.test(f));
|
||||
result.looseFiles = all
|
||||
.filter(f => f !== CANONICAL_OVERVIEW && f !== CANONICAL_GAPS && !KNOWN_ALTERNATIVES.includes(f))
|
||||
.map(f => join(archDir, f));
|
||||
|
||||
if (result.looseFiles.length > 0) {
|
||||
result.warnings.push(issue(
|
||||
'ARCH_LOOSE_FILES',
|
||||
`Found ${result.looseFiles.length} unrecognized architecture file(s)`,
|
||||
`Architecture contract expects overview.md (+ optional gaps.md). Loose files may indicate format drift in ultra-cc-architect.`,
|
||||
));
|
||||
}
|
||||
|
||||
if (result.found && result.overview) {
|
||||
try {
|
||||
const text = readFileSync(result.overview, 'utf-8');
|
||||
const firstHeading = text.match(/^#\s+(.+?)\s*$/m);
|
||||
result.firstHeading = firstHeading ? firstHeading[1] : null;
|
||||
} catch { /* ignore — only sniff */ }
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
const projectDir = process.argv[2];
|
||||
const wantJson = process.argv.includes('--json');
|
||||
if (!projectDir) {
|
||||
process.stderr.write('Usage: architecture-discovery.mjs <project-dir> [--json]\n');
|
||||
process.exit(2);
|
||||
}
|
||||
const r = discoverArchitecture(projectDir);
|
||||
if (wantJson) {
|
||||
process.stdout.write(JSON.stringify(r, null, 2) + '\n');
|
||||
} else {
|
||||
process.stdout.write(`architecture-discovery: ${r.found ? 'FOUND' : 'NONE'} ${r.overview || projectDir}\n`);
|
||||
for (const w of r.warnings) process.stderr.write(` WARN [${w.code}] ${w.message}\n`);
|
||||
}
|
||||
process.exit(0);
|
||||
}
|
||||
99
plugins/ultraplan-local/lib/validators/brief-validator.mjs
Normal file
99
plugins/ultraplan-local/lib/validators/brief-validator.mjs
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
// lib/validators/brief-validator.mjs
|
||||
// Validate ultrabrief frontmatter + body invariants.
|
||||
|
||||
import { readFileSync, existsSync } from 'node:fs';
|
||||
import { parseDocument } from '../util/frontmatter.mjs';
|
||||
import { issue, ok, fail } from '../util/result.mjs';
|
||||
|
||||
export const BRIEF_REQUIRED_FRONTMATTER = ['type', 'brief_version', 'task', 'slug', 'research_topics', 'research_status'];
|
||||
export const BRIEF_RESEARCH_STATUS_VALUES = ['pending', 'in_progress', 'complete', 'skipped'];
|
||||
export const BRIEF_BODY_SECTIONS = ['Intent', 'Goal', 'Success Criteria'];
|
||||
|
||||
export function validateBriefContent(text, opts = {}) {
|
||||
const strict = opts.strict !== false;
|
||||
const doc = parseDocument(text);
|
||||
if (!doc.valid) return doc;
|
||||
|
||||
const fm = doc.parsed.frontmatter || {};
|
||||
const body = doc.parsed.body || '';
|
||||
const errors = [];
|
||||
const warnings = [];
|
||||
|
||||
for (const k of BRIEF_REQUIRED_FRONTMATTER) {
|
||||
if (!(k in fm)) {
|
||||
errors.push(issue('BRIEF_MISSING_FIELD', `Required frontmatter field missing: ${k}`));
|
||||
}
|
||||
}
|
||||
|
||||
if (fm.type !== undefined && fm.type !== 'ultrabrief') {
|
||||
errors.push(issue('BRIEF_WRONG_TYPE', `frontmatter.type must be "ultrabrief", got "${fm.type}"`));
|
||||
}
|
||||
|
||||
if (fm.research_status !== undefined && !BRIEF_RESEARCH_STATUS_VALUES.includes(fm.research_status)) {
|
||||
errors.push(issue(
|
||||
'BRIEF_BAD_STATUS',
|
||||
`research_status "${fm.research_status}" not in [${BRIEF_RESEARCH_STATUS_VALUES.join(', ')}]`,
|
||||
));
|
||||
}
|
||||
|
||||
if (typeof fm.research_topics === 'number' && fm.research_topics > 0 && fm.research_status === 'skipped') {
|
||||
if (fm.brief_quality !== 'partial') {
|
||||
errors.push(issue(
|
||||
'BRIEF_STATE_INCOHERENT',
|
||||
`research_topics=${fm.research_topics} but research_status=skipped`,
|
||||
'Either set research_status to a real progress value, or mark brief_quality: partial.',
|
||||
));
|
||||
} else {
|
||||
warnings.push(issue(
|
||||
'BRIEF_PARTIAL_SKIPPED',
|
||||
`Brief has unresolved research topics (${fm.research_topics}) but is partial`,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
for (const section of BRIEF_BODY_SECTIONS) {
|
||||
const re = new RegExp(`^##\\s+${section}\\b`, 'm');
|
||||
if (!re.test(body)) {
|
||||
const issueObj = issue('BRIEF_MISSING_SECTION', `Required body section missing: ## ${section}`);
|
||||
if (strict) errors.push(issueObj);
|
||||
else warnings.push(issueObj);
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof fm.brief_version === 'string') {
|
||||
const m = fm.brief_version.match(/^(\d+)\.(\d+)$/);
|
||||
if (!m) {
|
||||
warnings.push(issue('BRIEF_VERSION_FORMAT', `brief_version "${fm.brief_version}" not in N.M form`));
|
||||
}
|
||||
}
|
||||
|
||||
return { valid: errors.length === 0, errors, warnings, parsed: { frontmatter: fm, body } };
|
||||
}
|
||||
|
||||
export function validateBrief(filePath, opts = {}) {
|
||||
if (!existsSync(filePath)) return fail(issue('BRIEF_NOT_FOUND', `File not found: ${filePath}`));
|
||||
let text;
|
||||
try { text = readFileSync(filePath, 'utf-8'); }
|
||||
catch (e) { return fail(issue('BRIEF_READ_ERROR', `Cannot read ${filePath}: ${e.message}`)); }
|
||||
const r = validateBriefContent(text, opts);
|
||||
return { ...r, parsed: { ...r.parsed, filePath } };
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
const args = process.argv.slice(2);
|
||||
const strict = !args.includes('--soft');
|
||||
const filePath = args.find(a => !a.startsWith('--'));
|
||||
if (!filePath) {
|
||||
process.stderr.write('Usage: brief-validator.mjs [--soft] <brief.md>\n');
|
||||
process.exit(2);
|
||||
}
|
||||
const r = validateBrief(filePath, { strict });
|
||||
if (args.includes('--json')) {
|
||||
process.stdout.write(JSON.stringify({ valid: r.valid, errors: r.errors, warnings: r.warnings }, null, 2) + '\n');
|
||||
} else {
|
||||
process.stdout.write(`brief-validator: ${r.valid ? 'PASS' : 'FAIL'} ${filePath}\n`);
|
||||
for (const e of r.errors) process.stderr.write(` ERROR [${e.code}] ${e.message}\n`);
|
||||
for (const w of r.warnings) process.stderr.write(` WARN [${w.code}] ${w.message}\n`);
|
||||
}
|
||||
process.exit(r.valid ? 0 : 1);
|
||||
}
|
||||
76
plugins/ultraplan-local/lib/validators/plan-validator.mjs
Normal file
76
plugins/ultraplan-local/lib/validators/plan-validator.mjs
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
// lib/validators/plan-validator.mjs
|
||||
// Wraps plan-schema (heading shape) + manifest-yaml (per-step Manifest blocks).
|
||||
// This is the JS equivalent of Phase 5.5 grep checks in planning-orchestrator.
|
||||
|
||||
import { readFileSync, existsSync } from 'node:fs';
|
||||
import { sliceSteps, validatePlanHeadings, extractPlanVersion } from '../parsers/plan-schema.mjs';
|
||||
import { validateAllManifests } from '../parsers/manifest-yaml.mjs';
|
||||
import { issue, fail } from '../util/result.mjs';
|
||||
|
||||
export function validatePlanContent(text, opts = {}) {
|
||||
const strict = opts.strict !== false;
|
||||
const headRes = validatePlanHeadings(text, { strict });
|
||||
const errors = [...headRes.errors];
|
||||
const warnings = [...headRes.warnings];
|
||||
|
||||
const steps = headRes.parsed?.steps || [];
|
||||
const sections = sliceSteps(text);
|
||||
const manRes = validateAllManifests(sections);
|
||||
errors.push(...manRes.errors);
|
||||
warnings.push(...manRes.warnings);
|
||||
|
||||
if (steps.length > 0 && manRes.parsed.length !== steps.length) {
|
||||
errors.push(issue(
|
||||
'PLAN_MANIFEST_COUNT_MISMATCH',
|
||||
`Step count (${steps.length}) does not equal manifest count (${manRes.parsed.length})`,
|
||||
));
|
||||
}
|
||||
|
||||
const planVersion = extractPlanVersion(text);
|
||||
if (planVersion === null) {
|
||||
warnings.push(issue('PLAN_NO_VERSION', 'No plan_version detected; current target is 1.7'));
|
||||
} else if (planVersion !== '1.7') {
|
||||
warnings.push(issue('PLAN_VERSION_MISMATCH', `plan_version=${planVersion}, current target is 1.7`));
|
||||
}
|
||||
|
||||
return {
|
||||
valid: errors.length === 0,
|
||||
errors,
|
||||
warnings,
|
||||
parsed: { steps, manifests: manRes.parsed, planVersion },
|
||||
};
|
||||
}
|
||||
|
||||
export function validatePlan(filePath, opts = {}) {
|
||||
if (!existsSync(filePath)) return fail(issue('PLAN_NOT_FOUND', `File not found: ${filePath}`));
|
||||
let text;
|
||||
try { text = readFileSync(filePath, 'utf-8'); }
|
||||
catch (e) { return fail(issue('PLAN_READ_ERROR', `Cannot read ${filePath}: ${e.message}`)); }
|
||||
const r = validatePlanContent(text, opts);
|
||||
return { ...r, parsed: { ...r.parsed, filePath } };
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
const args = process.argv.slice(2);
|
||||
const strict = !args.includes('--soft');
|
||||
const filePath = args.find(a => !a.startsWith('--'));
|
||||
if (!filePath) {
|
||||
process.stderr.write('Usage: plan-validator.mjs [--strict|--soft] <plan.md>\n');
|
||||
process.exit(2);
|
||||
}
|
||||
const r = validatePlan(filePath, { strict });
|
||||
if (args.includes('--json')) {
|
||||
process.stdout.write(JSON.stringify({
|
||||
valid: r.valid,
|
||||
errors: r.errors,
|
||||
warnings: r.warnings,
|
||||
steps: r.parsed?.steps?.length ?? 0,
|
||||
planVersion: r.parsed?.planVersion ?? null,
|
||||
}, null, 2) + '\n');
|
||||
} else {
|
||||
process.stdout.write(`plan-validator: ${r.valid ? 'READY' : 'FAIL'} ${filePath} (${r.parsed?.steps?.length ?? 0} steps)\n`);
|
||||
for (const e of r.errors) process.stderr.write(` ERROR [${e.code}] ${e.message}\n`);
|
||||
for (const w of r.warnings) process.stderr.write(` WARN [${w.code}] ${w.message}\n`);
|
||||
}
|
||||
process.exit(r.valid ? 0 : 1);
|
||||
}
|
||||
106
plugins/ultraplan-local/lib/validators/progress-validator.mjs
Normal file
106
plugins/ultraplan-local/lib/validators/progress-validator.mjs
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
// lib/validators/progress-validator.mjs
|
||||
// Validate progress.json shape + resume-readiness.
|
||||
|
||||
import { readFileSync, existsSync } from 'node:fs';
|
||||
import { issue, fail } from '../util/result.mjs';
|
||||
|
||||
export const PROGRESS_REQUIRED_TOP = ['schema_version', 'plan', 'plan_version', 'mode', 'status', 'total_steps', 'current_step', 'steps'];
|
||||
export const PROGRESS_VALID_STATUSES = ['pending', 'in_progress', 'completed', 'failed', 'partial'];
|
||||
|
||||
export function validateProgressContent(jsonText, opts = {}) {
|
||||
let parsed;
|
||||
try { parsed = JSON.parse(jsonText); }
|
||||
catch (e) {
|
||||
return fail(issue('PROGRESS_PARSE_ERROR', `Cannot parse JSON: ${e.message}`));
|
||||
}
|
||||
|
||||
return validateProgressObject(parsed, opts);
|
||||
}
|
||||
|
||||
export function validateProgressObject(parsed, opts = {}) {
|
||||
const errors = [];
|
||||
const warnings = [];
|
||||
|
||||
if (typeof parsed !== 'object' || parsed === null) {
|
||||
return fail(issue('PROGRESS_NOT_OBJECT', 'Progress payload is not an object'));
|
||||
}
|
||||
|
||||
for (const k of PROGRESS_REQUIRED_TOP) {
|
||||
if (!(k in parsed)) {
|
||||
errors.push(issue('PROGRESS_MISSING_FIELD', `Required field missing: ${k}`));
|
||||
}
|
||||
}
|
||||
|
||||
if (parsed.schema_version !== undefined && parsed.schema_version !== '1') {
|
||||
errors.push(issue('PROGRESS_SCHEMA_MISMATCH', `schema_version "${parsed.schema_version}" not supported (expected "1")`));
|
||||
}
|
||||
|
||||
if (parsed.status !== undefined && !PROGRESS_VALID_STATUSES.includes(parsed.status)) {
|
||||
errors.push(issue('PROGRESS_BAD_STATUS', `status "${parsed.status}" not in [${PROGRESS_VALID_STATUSES.join(', ')}]`));
|
||||
}
|
||||
|
||||
if (typeof parsed.total_steps === 'number' && typeof parsed.current_step === 'number') {
|
||||
if (parsed.current_step < 0 || parsed.current_step > parsed.total_steps) {
|
||||
errors.push(issue('PROGRESS_STEP_RANGE', `current_step=${parsed.current_step} outside [0, ${parsed.total_steps}]`));
|
||||
}
|
||||
}
|
||||
|
||||
if (parsed.steps && typeof parsed.steps === 'object') {
|
||||
const stepKeys = Object.keys(parsed.steps);
|
||||
if (typeof parsed.total_steps === 'number' && stepKeys.length !== parsed.total_steps) {
|
||||
warnings.push(issue(
|
||||
'PROGRESS_STEP_COUNT_MISMATCH',
|
||||
`total_steps=${parsed.total_steps} but steps map has ${stepKeys.length} entries`,
|
||||
));
|
||||
}
|
||||
for (const k of stepKeys) {
|
||||
const s = parsed.steps[k];
|
||||
if (s === null || typeof s !== 'object') {
|
||||
errors.push(issue('PROGRESS_STEP_SHAPE', `steps["${k}"] is not an object`));
|
||||
continue;
|
||||
}
|
||||
if (s.status !== undefined && !['completed', 'in_progress', 'failed', 'pending', 'deferred', 'skipped'].includes(s.status)) {
|
||||
warnings.push(issue('PROGRESS_STEP_BAD_STATUS', `steps["${k}"].status "${s.status}" unrecognized`));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { valid: errors.length === 0, errors, warnings, parsed };
|
||||
}
|
||||
|
||||
export function checkResumeReadiness(progressObj) {
|
||||
const errors = [];
|
||||
if (progressObj.status === 'completed') {
|
||||
return { valid: false, errors: [issue('PROGRESS_ALREADY_DONE', 'Run is already completed; nothing to resume')], warnings: [], parsed: progressObj };
|
||||
}
|
||||
if (typeof progressObj.current_step !== 'number') {
|
||||
errors.push(issue('PROGRESS_NO_CURRENT', 'No current_step in progress.json'));
|
||||
}
|
||||
return { valid: errors.length === 0, errors, warnings: [], parsed: progressObj };
|
||||
}
|
||||
|
||||
export function validateProgress(filePath, opts = {}) {
|
||||
if (!existsSync(filePath)) return fail(issue('PROGRESS_NOT_FOUND', `File not found: ${filePath}`));
|
||||
let text;
|
||||
try { text = readFileSync(filePath, 'utf-8'); }
|
||||
catch (e) { return fail(issue('PROGRESS_READ_ERROR', `Cannot read ${filePath}: ${e.message}`)); }
|
||||
return validateProgressContent(text, opts);
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
const args = process.argv.slice(2);
|
||||
const filePath = args.find(a => !a.startsWith('--'));
|
||||
if (!filePath) {
|
||||
process.stderr.write('Usage: progress-validator.mjs [--quick] <progress.json>\n');
|
||||
process.exit(2);
|
||||
}
|
||||
const r = validateProgress(filePath);
|
||||
if (args.includes('--json')) {
|
||||
process.stdout.write(JSON.stringify({ valid: r.valid, errors: r.errors, warnings: r.warnings }, null, 2) + '\n');
|
||||
} else {
|
||||
process.stdout.write(`progress-validator: ${r.valid ? 'PASS' : 'FAIL'} ${filePath}\n`);
|
||||
for (const e of r.errors) process.stderr.write(` ERROR [${e.code}] ${e.message}\n`);
|
||||
for (const w of r.warnings) process.stderr.write(` WARN [${w.code}] ${w.message}\n`);
|
||||
}
|
||||
process.exit(r.valid ? 0 : 1);
|
||||
}
|
||||
109
plugins/ultraplan-local/lib/validators/research-validator.mjs
Normal file
109
plugins/ultraplan-local/lib/validators/research-validator.mjs
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
// lib/validators/research-validator.mjs
|
||||
// Validate research-brief frontmatter + body invariants.
|
||||
|
||||
import { readFileSync, existsSync, readdirSync, statSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { parseDocument } from '../util/frontmatter.mjs';
|
||||
import { issue, fail } from '../util/result.mjs';
|
||||
|
||||
export const RESEARCH_REQUIRED_FRONTMATTER = ['type', 'created', 'question'];
|
||||
export const RESEARCH_BODY_SECTIONS = ['Executive Summary', 'Dimensions'];
|
||||
|
||||
export function validateResearchContent(text, opts = {}) {
|
||||
const strict = opts.strict !== false;
|
||||
const doc = parseDocument(text);
|
||||
if (!doc.valid) return doc;
|
||||
|
||||
const fm = doc.parsed.frontmatter || {};
|
||||
const body = doc.parsed.body || '';
|
||||
const errors = [];
|
||||
const warnings = [];
|
||||
|
||||
for (const k of RESEARCH_REQUIRED_FRONTMATTER) {
|
||||
if (!(k in fm)) errors.push(issue('RESEARCH_MISSING_FIELD', `Required frontmatter field missing: ${k}`));
|
||||
}
|
||||
|
||||
if (fm.type !== undefined && fm.type !== 'ultraresearch-brief') {
|
||||
errors.push(issue('RESEARCH_WRONG_TYPE', `frontmatter.type must be "ultraresearch-brief", got "${fm.type}"`));
|
||||
}
|
||||
|
||||
if (fm.confidence !== undefined) {
|
||||
if (typeof fm.confidence !== 'number' || fm.confidence < 0 || fm.confidence > 1) {
|
||||
errors.push(issue('RESEARCH_BAD_CONFIDENCE', `confidence must be number in [0,1], got ${fm.confidence}`));
|
||||
}
|
||||
} else {
|
||||
warnings.push(issue('RESEARCH_NO_CONFIDENCE', 'No confidence field — planner has no signal to weight findings'));
|
||||
}
|
||||
|
||||
if (fm.dimensions !== undefined && (typeof fm.dimensions !== 'number' || fm.dimensions < 1)) {
|
||||
errors.push(issue('RESEARCH_BAD_DIMENSIONS', `dimensions must be positive integer, got ${fm.dimensions}`));
|
||||
}
|
||||
|
||||
for (const section of RESEARCH_BODY_SECTIONS) {
|
||||
const re = new RegExp(`^##\\s+${section}\\b`, 'm');
|
||||
if (!re.test(body)) {
|
||||
const issueObj = issue('RESEARCH_MISSING_SECTION', `Required body section missing: ## ${section}`);
|
||||
if (strict) errors.push(issueObj);
|
||||
else warnings.push(issueObj);
|
||||
}
|
||||
}
|
||||
|
||||
return { valid: errors.length === 0, errors, warnings, parsed: { frontmatter: fm, body } };
|
||||
}
|
||||
|
||||
export function validateResearch(filePath, opts = {}) {
|
||||
if (!existsSync(filePath)) return fail(issue('RESEARCH_NOT_FOUND', `File not found: ${filePath}`));
|
||||
let text;
|
||||
try { text = readFileSync(filePath, 'utf-8'); }
|
||||
catch (e) { return fail(issue('RESEARCH_READ_ERROR', `Cannot read ${filePath}: ${e.message}`)); }
|
||||
const r = validateResearchContent(text, opts);
|
||||
return { ...r, parsed: { ...r.parsed, filePath } };
|
||||
}
|
||||
|
||||
export function validateResearchDir(dirPath, opts = {}) {
|
||||
if (!existsSync(dirPath) || !statSync(dirPath).isDirectory()) {
|
||||
return { valid: true, errors: [], warnings: [], parsed: { files: [] } };
|
||||
}
|
||||
const files = readdirSync(dirPath).filter(f => f.endsWith('.md')).sort();
|
||||
const errors = [];
|
||||
const warnings = [];
|
||||
const results = [];
|
||||
for (const f of files) {
|
||||
const r = validateResearch(join(dirPath, f), opts);
|
||||
for (const e of r.errors) errors.push(issue(e.code, `${f}: ${e.message}`, e.hint));
|
||||
for (const w of r.warnings) warnings.push(issue(w.code, `${f}: ${w.message}`, w.hint));
|
||||
results.push({ file: f, valid: r.valid });
|
||||
}
|
||||
return { valid: errors.length === 0, errors, warnings, parsed: { files: results } };
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
const args = process.argv.slice(2);
|
||||
const strict = !args.includes('--soft');
|
||||
const dirIdx = args.indexOf('--dir');
|
||||
if (dirIdx >= 0 && args[dirIdx + 1]) {
|
||||
const r = validateResearchDir(args[dirIdx + 1], { strict });
|
||||
if (args.includes('--json')) {
|
||||
process.stdout.write(JSON.stringify({ valid: r.valid, errors: r.errors, warnings: r.warnings, files: r.parsed.files }, null, 2) + '\n');
|
||||
} else {
|
||||
process.stdout.write(`research-validator (dir): ${r.valid ? 'PASS' : 'FAIL'} ${args[dirIdx + 1]}\n`);
|
||||
for (const e of r.errors) process.stderr.write(` ERROR [${e.code}] ${e.message}\n`);
|
||||
for (const w of r.warnings) process.stderr.write(` WARN [${w.code}] ${w.message}\n`);
|
||||
}
|
||||
process.exit(r.valid ? 0 : 1);
|
||||
}
|
||||
const filePath = args.find(a => !a.startsWith('--'));
|
||||
if (!filePath) {
|
||||
process.stderr.write('Usage: research-validator.mjs [--soft] <file.md> OR --dir <research-dir>\n');
|
||||
process.exit(2);
|
||||
}
|
||||
const r = validateResearch(filePath, { strict });
|
||||
if (args.includes('--json')) {
|
||||
process.stdout.write(JSON.stringify({ valid: r.valid, errors: r.errors, warnings: r.warnings }, null, 2) + '\n');
|
||||
} else {
|
||||
process.stdout.write(`research-validator: ${r.valid ? 'PASS' : 'FAIL'} ${filePath}\n`);
|
||||
for (const e of r.errors) process.stderr.write(` ERROR [${e.code}] ${e.message}\n`);
|
||||
for (const w of r.warnings) process.stderr.write(` WARN [${w.code}] ${w.message}\n`);
|
||||
}
|
||||
process.exit(r.valid ? 0 : 1);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue