feat: bhl audit — детерминированный движок аудита (BHL-026)
- встроенные проверки: команда качества, lockfile, CI-конфиг по Effective Spec - subject: отпечаток дерева SHA-256 + git revision/dirty (git из PATH — узкое исключение sonarjs/no-os-command-from-path в eslint.config.mjs) - evidence с fingerprint байтов; FAIL доказателен (отсутствие — наблюдение) - отчёт проходит report validate; повторный аудит идентичен (тесты) - требования без проверки → UNKNOWN/semantic/low; пакет 41/41
This commit is contained in:
+110
-3
@@ -1,5 +1,6 @@
|
||||
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { runAudit, writeAuditReport } from './audit.ts';
|
||||
import { emit, ExitCode, failEnvelope, okEnvelope } from './envelope.ts';
|
||||
import { fetchSpec, verifyCache } from './fetch.ts';
|
||||
import { renderReport, renderSpec } from './render.ts';
|
||||
@@ -9,7 +10,7 @@ import { validateReport } from './report.ts';
|
||||
import { validateSpec } from './validate.ts';
|
||||
|
||||
const USAGE_MESSAGE =
|
||||
'Usage: bhl <version|validate|spec|profile|report|manifest|audit> [args]; ' +
|
||||
'Usage: bhl <version|validate|spec|profile|report|manifest|fetch|verify|audit> [args]; ' +
|
||||
'spec list [--spec-root <path>] [--domain <id>] [--capability <id>]; ' +
|
||||
'spec show <req-id> [--spec-root <path>]; ' +
|
||||
'spec render --spec-root <path> --out <dir>; ' +
|
||||
@@ -19,6 +20,8 @@ const USAGE_MESSAGE =
|
||||
'manifest validate <manifest.yaml> [--spec-root <path>]; ' +
|
||||
'fetch <release-dir> --version <semver> --cache <dir>; ' +
|
||||
'verify --version <semver> --cache <dir>; ' +
|
||||
'audit <project-dir> --out <report.json> [--spec-root <path>] [--profile <id>] ' +
|
||||
'[--preset <id>] [--generated-at <iso>]; ' +
|
||||
'JSON envelope on stdout; exit codes: 0 ok, 1 validation failed, 2 usage, ' +
|
||||
'3 not implemented, 4 internal.';
|
||||
|
||||
@@ -461,6 +464,111 @@ function runVerify(command: string, rest: readonly string[]): number {
|
||||
return ExitCode.Ok;
|
||||
}
|
||||
|
||||
const AUDIT_FLAGS: Readonly<
|
||||
Record<string, 'spec-root' | 'out' | 'generated-at' | 'profile' | 'preset'>
|
||||
> = {
|
||||
'--spec-root': 'spec-root',
|
||||
'--out': 'out',
|
||||
'--generated-at': 'generated-at',
|
||||
'--profile': 'profile',
|
||||
'--preset': 'preset',
|
||||
};
|
||||
|
||||
interface AuditFlags {
|
||||
root: string;
|
||||
out?: string;
|
||||
generatedAt?: string;
|
||||
profiles: string[];
|
||||
presets: string[];
|
||||
positional: string[];
|
||||
}
|
||||
|
||||
function takeAuditFlag(
|
||||
args: readonly string[],
|
||||
cursor: number,
|
||||
target: AuditFlags,
|
||||
arg: string,
|
||||
): { next: number } | { error: string } {
|
||||
const flag = AUDIT_FLAGS[arg];
|
||||
const value = args[cursor + 1];
|
||||
if (flag === undefined || value === undefined) {
|
||||
return { error: `missing value for ${arg}` };
|
||||
}
|
||||
if (flag === 'spec-root') {
|
||||
target.root = value;
|
||||
} else if (flag === 'out') {
|
||||
target.out = value;
|
||||
} else if (flag === 'generated-at') {
|
||||
target.generatedAt = value;
|
||||
} else if (flag === 'profile') {
|
||||
target.profiles.push(value);
|
||||
} else {
|
||||
target.presets.push(value);
|
||||
}
|
||||
return { next: cursor + 2 };
|
||||
}
|
||||
|
||||
function parseAuditArgs(args: readonly string[]): AuditFlags | { error: string } {
|
||||
const target: AuditFlags = { root: 'spec', profiles: [], presets: [], positional: [] };
|
||||
let cursor = 0;
|
||||
while (cursor < args.length) {
|
||||
const arg = args[cursor] ?? '';
|
||||
if (AUDIT_FLAGS[arg] !== undefined) {
|
||||
const taken = takeAuditFlag(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 runAuditCommand(command: string, rest: readonly string[]): number {
|
||||
const parsed = parseAuditArgs(rest);
|
||||
if ('error' in parsed) {
|
||||
emit(failEnvelope(command, 'USAGE', parsed.error));
|
||||
return ExitCode.Usage;
|
||||
}
|
||||
const projectDir = parsed.positional[0];
|
||||
const out = parsed.out;
|
||||
if (projectDir === undefined || parsed.positional.length !== 1 || out === undefined) {
|
||||
emit(failEnvelope(command, 'USAGE', 'audit requires <project-dir> and --out <report.json>'));
|
||||
return ExitCode.Usage;
|
||||
}
|
||||
const generatedAt = parsed.generatedAt ?? new Date().toISOString().slice(0, 10);
|
||||
const result = runAudit({
|
||||
projectDir,
|
||||
specRoot: parsed.root,
|
||||
profileIds: parsed.profiles,
|
||||
presetIds: parsed.presets,
|
||||
generatedAt,
|
||||
});
|
||||
if (!result.ok || result.report === undefined) {
|
||||
return emitValidationFailure(
|
||||
command,
|
||||
{ project: projectDir, errors: result.diagnostics },
|
||||
result.diagnostics.length,
|
||||
);
|
||||
}
|
||||
mkdirSync(path.dirname(out), { recursive: true });
|
||||
writeAuditReport(result, out);
|
||||
emit(
|
||||
okEnvelope(command, {
|
||||
project: projectDir,
|
||||
out,
|
||||
summary: result.report['summary'],
|
||||
}),
|
||||
);
|
||||
return ExitCode.Ok;
|
||||
}
|
||||
|
||||
function runManifest(command: string, rest: readonly string[]): number {
|
||||
const [sub, ...args] = rest;
|
||||
if (sub !== 'validate') {
|
||||
@@ -607,8 +715,7 @@ export function run(argv: readonly string[]): number {
|
||||
return runVerify(command, rest);
|
||||
}
|
||||
if (command === 'audit') {
|
||||
emit(failEnvelope(command, 'NOT_IMPLEMENTED', `${command} is not implemented yet`));
|
||||
return ExitCode.NotImplemented;
|
||||
return runAuditCommand(command, rest);
|
||||
}
|
||||
emit(failEnvelope(command, 'USAGE', USAGE_MESSAGE));
|
||||
return ExitCode.Usage;
|
||||
|
||||
Reference in New Issue
Block a user