feat(fleet): enforce reviewed trips and original fee coverage for dispatch
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import process from 'node:process';
|
||||
import { pathToFileURL } from 'node:url';
|
||||
|
||||
const moduleRoot = path.resolve(import.meta.dirname, '..');
|
||||
const sourceRoots = {
|
||||
@@ -12,6 +13,7 @@ const sourceRoots = {
|
||||
const internalPrefix = 'com.baiyue.lastleg.';
|
||||
|
||||
const sliceRules = [
|
||||
['fleet', /^com\.baiyue\.lastleg\.fleet(?:\.|$)/],
|
||||
['security-projection', /(?:^|\.)security\.(?:controller|dto|service)(?:\.|$)/],
|
||||
['dispatch-delivery', /(?:^|\.)(?:dispatch|delivery|outbound)(?:\.|$)/],
|
||||
['relabel-transfer-discard', /(?:^|\.)(?:relabel|transfer|discard)(?:\.|$)/],
|
||||
@@ -269,13 +271,34 @@ function renderDot(report, graph) {
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
function checkContract(report, contractPath) {
|
||||
const absolute = path.resolve(process.cwd(), contractPath);
|
||||
const contract = JSON.parse(fs.readFileSync(absolute, 'utf8'));
|
||||
export function contractViolations(report, contract) {
|
||||
const allowed = new Set(contract.allowedSliceEdges ?? []);
|
||||
const actual = report.sliceEdges.map(edge => `${edge.source}->${edge.target}`);
|
||||
const violations = actual.filter(edge => !allowed.has(edge)).map(edge => `new slice dependency: ${edge}`);
|
||||
const reviewed = new Set();
|
||||
const violations = [];
|
||||
if (contract.forbidPackageCycles) {
|
||||
for (const cycle of report.packageSccs) violations.push(`package cycle: ${cycle.join(' -> ')}`);
|
||||
}
|
||||
for (const edge of contract.reviewedClassDependencies ?? []) {
|
||||
if (!edge.source || !edge.target || !edge.reason?.trim() || /[*]/.test(edge.source + edge.target)) {
|
||||
violations.push('reviewed dependency requires exact classes and a rationale');
|
||||
continue;
|
||||
}
|
||||
reviewed.add(`${edge.source}->${edge.target}`);
|
||||
}
|
||||
const classesByName = new Map(report.classes.map(entry => [entry.fqcn, entry]));
|
||||
const governed = report.classDependencies.filter(edge => !reviewed.has(`${edge.source}->${edge.target}`));
|
||||
for (const edge of governed) {
|
||||
const from = classesByName.get(edge.source)?.slice, to = classesByName.get(edge.target)?.slice;
|
||||
if (from && to && from !== to && !allowed.has(`${from}->${to}`)) {
|
||||
violations.push(`unreviewed slice dependency: ${from}->${to}: ${edge.source}->${edge.target}`);
|
||||
}
|
||||
}
|
||||
for (const entry of report.classes) {
|
||||
if ((contract.forbiddenSlices ?? []).includes(entry.slice)) violations.push(`retired slice returned: ${entry.fqcn}`);
|
||||
for (const type of [entry.fqcn, ...entry.imports]) {
|
||||
if ((contract.forbiddenTypePrefixes ?? []).some(prefix => type.startsWith(prefix))) violations.push(`retired security type referenced: ${entry.fqcn} -> ${type}`);
|
||||
}
|
||||
}
|
||||
for (const [fqcn, expectedFile] of Object.entries(contract.requiredClassLocations ?? {})) {
|
||||
const actualClass = classesByName.get(fqcn);
|
||||
if (!actualClass) violations.push(`required class missing: ${fqcn}`);
|
||||
@@ -300,8 +323,11 @@ function checkContract(report, contractPath) {
|
||||
}
|
||||
}
|
||||
for (const [slice, rule] of Object.entries(contract.sliceSpecificRules ?? {})) {
|
||||
const outgoing = report.sliceEdges.filter(edge => edge.source === slice).map(edge => edge.target).sort();
|
||||
const incoming = report.sliceEdges.filter(edge => edge.target === slice).map(edge => edge.source).sort();
|
||||
// A reviewed class reference does not authorize any other consumer of that slice.
|
||||
const outgoing = [...new Set(governed.filter(edge => classesByName.get(edge.source)?.slice === slice)
|
||||
.map(edge => classesByName.get(edge.target)?.slice).filter(target => target && target !== slice))].sort();
|
||||
const incoming = [...new Set(governed.filter(edge => classesByName.get(edge.target)?.slice === slice)
|
||||
.map(edge => classesByName.get(edge.source)?.slice).filter(source => source && source !== slice))].sort();
|
||||
if (JSON.stringify(outgoing) !== JSON.stringify([...(rule.allowedOutgoing ?? [])].sort())) {
|
||||
violations.push(`${slice} outgoing dependencies changed: ${outgoing.join(', ')}`);
|
||||
}
|
||||
@@ -309,19 +335,25 @@ function checkContract(report, contractPath) {
|
||||
violations.push(`${slice} incoming dependencies changed: ${incoming.join(', ')}`);
|
||||
}
|
||||
}
|
||||
if (violations.length > 0) {
|
||||
process.stderr.write(`Lastleg dependency contract violations in ${contractPath}:\n${violations.map(item => `- ${item}`).join('\n')}\n`);
|
||||
process.exitCode = 1;
|
||||
}
|
||||
return violations;
|
||||
}
|
||||
|
||||
const options = parseArgs(process.argv.slice(2));
|
||||
const report = buildReport(options.sourceSet);
|
||||
if (options.check) checkContract(report, options.check);
|
||||
const rendered = options.format === 'json'
|
||||
? `${JSON.stringify(report, null, 2)}\n`
|
||||
: options.format === 'markdown'
|
||||
? renderMarkdown(report)
|
||||
: renderDot(report, options.graph);
|
||||
if (options.output) fs.writeFileSync(path.resolve(process.cwd(), options.output), rendered, 'utf8');
|
||||
else process.stdout.write(rendered);
|
||||
if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) {
|
||||
const options = parseArgs(process.argv.slice(2));
|
||||
const report = buildReport(options.sourceSet);
|
||||
if (options.check) {
|
||||
const contract = JSON.parse(fs.readFileSync(path.resolve(process.cwd(), options.check), 'utf8'));
|
||||
const violations = contractViolations(report, contract);
|
||||
if (violations.length) {
|
||||
process.stderr.write(`Lastleg dependency contract violations in ${options.check}:\n${violations.map(item => `- ${item}`).join('\n')}\n`);
|
||||
process.exitCode = 1;
|
||||
}
|
||||
}
|
||||
const rendered = options.format === 'json'
|
||||
? `${JSON.stringify(report, null, 2)}\n`
|
||||
: options.format === 'markdown'
|
||||
? renderMarkdown(report)
|
||||
: renderDot(report, options.graph);
|
||||
if (options.output) fs.writeFileSync(path.resolve(process.cwd(), options.output), rendered, 'utf8');
|
||||
else process.stdout.write(rendered);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user