ktg-plugin-marketplace/plugins/ultraplan-local/lib/validators/architecture-discovery.mjs
Kjell Tore Guttormsen 65c9242160 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>
2026-05-01 05:39:47 +02:00

94 lines
3.4 KiB
JavaScript

// 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);
}