feat: spec list/show и manifest validate (BHL-022)

- bhl spec list [--domain|--capability], spec show <req-id> (NOT_FOUND)
- bhl manifest validate: схема + висячие профили/параметры/требования,
  неутверждённое исключение отклоняется
- e2e-тесты на examples/synthetic-spec; пакет 29/29
This commit is contained in:
Suslov-MD
2026-11-29 00:37:52 +03:00
parent f04c4b6481
commit 224ea16ae5
6 changed files with 766 additions and 10 deletions
+158 -2
View File
@@ -1,13 +1,17 @@
import { readFileSync } from 'node:fs';
import { emit, ExitCode, failEnvelope, okEnvelope } from './envelope.ts';
import { resolveProfiles } from './resolve.ts';
import { specList, specShow, validateManifest } from './spec.ts';
import { validateReport } from './report.ts';
import { validateSpec } from './validate.ts';
const USAGE_MESSAGE =
'Usage: bhl <version|validate|spec|profile|report|audit> [args]; ' +
'Usage: bhl <version|validate|spec|profile|report|manifest|audit> [args]; ' +
'spec list [--spec-root <path>] [--domain <id>] [--capability <id>]; ' +
'spec show <req-id> [--spec-root <path>]; ' +
'profile resolve <spec-root> [--profile <id>] [--preset <id>]; ' +
'report validate <report.json> [--spec-root <path>]; ' +
'manifest validate <manifest.yaml> [--spec-root <path>]; ' +
'JSON envelope on stdout; exit codes: 0 ok, 1 validation failed, 2 usage, ' +
'3 not implemented, 4 internal.';
@@ -169,6 +173,152 @@ function parseReportArgs(
return { file: positional[0] ?? '', specRoot };
}
interface SpecFlags {
root: string;
domain?: string;
capability?: string;
positional: string[];
}
function takeNamedFlag(
args: readonly string[],
cursor: number,
target: SpecFlags,
arg: string,
): { next: number } | { error: string } {
const value = args[cursor + 1];
if (value === undefined) {
return { error: `missing value for ${arg}` };
}
if (arg === '--spec-root') {
target.root = value;
} else if (arg === '--domain') {
target.domain = value;
} else {
target.capability = value;
}
return { next: cursor + 2 };
}
function parseSpecArgs(args: readonly string[]): SpecFlags | { error: string } {
const target: SpecFlags = { root: 'spec', positional: [] };
let cursor = 0;
while (cursor < args.length) {
const arg = args[cursor] ?? '';
if (arg === '--spec-root' || arg === '--domain' || arg === '--capability') {
const taken = takeNamedFlag(args, cursor, target, arg);
if ('error' in taken) {
return taken;
}
cursor = taken.next;
continue;
}
if (arg.startsWith('--')) {
return { error: `unknown flag ${arg}` };
}
target.positional.push(arg);
cursor += 1;
}
return target;
}
function emitValidationFailure(
command: string,
data: Record<string, unknown>,
count: number,
code = 'VALIDATION_FAILED',
): number {
emit({
ok: false,
command,
data,
error: { code, message: `${String(count)} diagnostic(s)` },
});
return ExitCode.ValidationFailed;
}
function emitSpecList(parsed: SpecFlags): number {
const result = specList(parsed.root, parsed.domain, parsed.capability);
const data = { root: parsed.root, requirements: result.requirements, errors: result.diagnostics };
if (!result.ok) {
return emitValidationFailure('spec', data, result.diagnostics.length);
}
emit(okEnvelope('spec', data));
return ExitCode.Ok;
}
function emitSpecShow(parsed: SpecFlags): number {
const id = parsed.positional[0];
if (id === undefined || parsed.positional.length !== 1) {
emit(failEnvelope('spec', 'USAGE', 'spec show requires exactly one requirement id'));
return ExitCode.Usage;
}
const result = specShow(parsed.root, id);
if (!result.ok) {
const code =
result.diagnostics[0]?.code === 'unknown-requirement' ? 'NOT_FOUND' : 'VALIDATION_FAILED';
return emitValidationFailure(
'spec',
{ root: parsed.root, errors: result.diagnostics },
result.diagnostics.length,
code,
);
}
emit(okEnvelope('spec', { root: parsed.root, requirement: result.requirement }));
return ExitCode.Ok;
}
function runSpec(command: string, rest: readonly string[]): number {
const [sub, ...args] = rest;
if (sub !== 'list' && sub !== 'show') {
emit(failEnvelope(command, 'USAGE', 'spec requires the subcommand list or show'));
return ExitCode.Usage;
}
const parsed = parseSpecArgs(args);
if ('error' in parsed) {
emit(failEnvelope(command, 'USAGE', parsed.error));
return ExitCode.Usage;
}
if (sub === 'list') {
return emitSpecList(parsed);
}
return emitSpecShow(parsed);
}
function runManifest(command: string, rest: readonly string[]): number {
const [sub, ...args] = rest;
if (sub !== 'validate') {
emit(failEnvelope(command, 'USAGE', 'manifest requires the subcommand validate'));
return ExitCode.Usage;
}
const parsed = parseSpecArgs(args);
if ('error' in parsed) {
emit(failEnvelope(command, 'USAGE', parsed.error));
return ExitCode.Usage;
}
const file = parsed.positional[0];
if (file === undefined || parsed.positional.length !== 1) {
emit(failEnvelope(command, 'USAGE', 'manifest validate requires exactly one manifest file'));
return ExitCode.Usage;
}
const result = validateManifest(file, parsed.root);
const data = { file, spec_root: parsed.root, errors: result.diagnostics };
if (result.ok) {
emit(okEnvelope(command, data));
return ExitCode.Ok;
}
emit({
ok: false,
command,
data,
error: {
code: 'VALIDATION_FAILED',
message: `${String(result.diagnostics.length)} diagnostic(s)`,
},
});
return ExitCode.ValidationFailed;
}
function runReport(command: string, rest: readonly string[]): number {
const [sub, ...args] = rest;
if (sub !== 'validate') {
@@ -239,10 +389,16 @@ export function run(argv: readonly string[]): number {
if (command === 'validate') {
return runValidate(command, rest);
}
if (command === 'spec') {
return runSpec(command, rest);
}
if (command === 'manifest') {
return runManifest(command, rest);
}
if (command === 'report') {
return runReport(command, rest);
}
if (command === 'spec' || command === 'audit') {
if (command === 'audit') {
emit(failEnvelope(command, 'NOT_IMPLEMENTED', `${command} is not implemented yet`));
return ExitCode.NotImplemented;
}