feat(cli): summarize committed reference-source inventories (#2768)
This commit is contained in:
@@ -2,6 +2,7 @@ import { z } from "incur";
|
||||
|
||||
import type { DirectAnalysis } from "../composition/directAnalysis.js";
|
||||
import type { ReferenceSourcePolicy } from "../domain/referenceSourcePolicy.js";
|
||||
import { createHistoricalSourceSummary } from "../domain/referenceSourceSummary.js";
|
||||
import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js";
|
||||
import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";
|
||||
import { importReferenceSource } from "../application/ReferenceSourceImport.js";
|
||||
@@ -241,11 +242,48 @@ const registerReferenceSourceCommand = (
|
||||
.string()
|
||||
.describe("Local source directory to import as historical reference"),
|
||||
}),
|
||||
run: ({ args }) =>
|
||||
options: z.object({
|
||||
detail: z
|
||||
.enum(["full", "summary"])
|
||||
.default("full")
|
||||
.describe(
|
||||
"Complete committed graph or compact historical-source summary",
|
||||
),
|
||||
outlineDepth: z
|
||||
.number()
|
||||
.int()
|
||||
.nonnegative()
|
||||
.safe()
|
||||
.optional()
|
||||
.describe(
|
||||
"Summary outline path depth (default: 2; 0 omits entries); requires --detail summary",
|
||||
),
|
||||
}),
|
||||
run: ({ args, options }) =>
|
||||
logCliCommand(
|
||||
logger,
|
||||
"import-reference-source",
|
||||
async () => {
|
||||
if (
|
||||
options.outlineDepth !== undefined &&
|
||||
options.detail !== "summary"
|
||||
)
|
||||
return {
|
||||
error: "Import failed",
|
||||
...projectAnalysisError(
|
||||
new AnalysisInputError(
|
||||
"import-reference-source",
|
||||
{ userMessage: "--outline-depth requires --detail summary" },
|
||||
[
|
||||
{
|
||||
path: ["outlineDepth"],
|
||||
reason: "invalid_value",
|
||||
message: "--outline-depth requires --detail summary",
|
||||
},
|
||||
],
|
||||
),
|
||||
),
|
||||
};
|
||||
const config = parseConfig(environment);
|
||||
if (!config.ok)
|
||||
return {
|
||||
@@ -255,6 +293,9 @@ const registerReferenceSourceCommand = (
|
||||
return runReferenceSourceImport(
|
||||
args.root,
|
||||
config.value.referenceSourcePolicy,
|
||||
options.detail === "summary"
|
||||
? (options.outlineDepth ?? 2)
|
||||
: undefined,
|
||||
);
|
||||
},
|
||||
isReferenceSourceImportCliFailure,
|
||||
@@ -265,6 +306,7 @@ const registerReferenceSourceCommand = (
|
||||
const runReferenceSourceImport = async (
|
||||
root: string,
|
||||
policy: ReferenceSourcePolicy,
|
||||
outlineDepth: number | undefined,
|
||||
): Promise<unknown> => {
|
||||
const resources = new ArtifactResourceScope();
|
||||
let outcome:
|
||||
@@ -346,7 +388,9 @@ const runReferenceSourceImport = async (
|
||||
cause: closeOutcome.cause,
|
||||
}),
|
||||
};
|
||||
return imported.value;
|
||||
return outlineDepth === undefined
|
||||
? imported.value
|
||||
: createHistoricalSourceSummary(imported.value, outlineDepth);
|
||||
};
|
||||
|
||||
const mergeReferenceSourceCleanup = (
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
createHistoricalSourceGraph,
|
||||
createHistoricalSourceManifest,
|
||||
type HistoricalSourceGraphInput,
|
||||
} from "./referenceSourceGraph.js";
|
||||
import { createHistoricalSourceSummary } from "./referenceSourceSummary.js";
|
||||
|
||||
const input = (
|
||||
entries: HistoricalSourceGraphInput["entries"],
|
||||
): HistoricalSourceGraphInput => ({
|
||||
schema: "HistoricalSourceGraph",
|
||||
authority: "historical-reference",
|
||||
root_alias: "$REFERENCE_ROOT",
|
||||
inventory_state: "unknown",
|
||||
entries,
|
||||
relationships: [],
|
||||
parse_failures: [],
|
||||
exclusions: [],
|
||||
languages: [],
|
||||
manifests: [],
|
||||
vcs: null,
|
||||
provenance: { importer: "fixture", importer_version: null, caller: "test" },
|
||||
limitations: ["Inventory coverage is unknown"],
|
||||
});
|
||||
|
||||
describe("committed historical-source summaries", () => {
|
||||
it("preserves unknown coverage, sizes, directories and missing symlinks without mutating the graph", () => {
|
||||
const graph = createHistoricalSourceGraph(
|
||||
input([
|
||||
{
|
||||
path: "a",
|
||||
kind: "directory",
|
||||
tree_state: "unknown",
|
||||
classifications: ["unknown"],
|
||||
limitations: ["Not enumerated"],
|
||||
},
|
||||
{
|
||||
path: "b",
|
||||
kind: "file",
|
||||
sha256: null,
|
||||
size: null,
|
||||
content_state: "unreadable",
|
||||
language: null,
|
||||
classifications: ["unknown"],
|
||||
limitations: ["Not readable"],
|
||||
},
|
||||
{
|
||||
path: "c",
|
||||
kind: "symlink",
|
||||
target: "missing",
|
||||
target_state: "missing",
|
||||
classifications: ["unknown"],
|
||||
limitations: [],
|
||||
},
|
||||
]),
|
||||
);
|
||||
const original = structuredClone(graph);
|
||||
const summary = createHistoricalSourceSummary(graph, 1);
|
||||
expect(summary).toMatchObject({
|
||||
manifest: createHistoricalSourceManifest(original),
|
||||
inventory_state: "unknown",
|
||||
vcs: null,
|
||||
provenance: original.provenance,
|
||||
file_sizes: { known_bytes: "0", unknown_file_count: 1 },
|
||||
counts: { entries: 3, files: 1, directories: 1, symlinks: 1 },
|
||||
directory_tree_states: { unknown: 1, enumerated: 0 },
|
||||
file_content_states: { unreadable: 1, hashed: 0 },
|
||||
limitations: original.limitations,
|
||||
outline: {
|
||||
entries: [
|
||||
original.entries[0],
|
||||
expect.objectContaining({ size: null, content_state: "unreadable" }),
|
||||
original.entries[2],
|
||||
],
|
||||
omitted_entry_count: 0,
|
||||
},
|
||||
});
|
||||
expect(summary.outline.entries[1]).not.toHaveProperty("sha256");
|
||||
expect(graph).toEqual(original);
|
||||
});
|
||||
|
||||
it("sums known bytes losslessly beyond the JSON safe integer range", () => {
|
||||
const graph = createHistoricalSourceGraph(
|
||||
input(
|
||||
["a", "b"].map((path) => ({
|
||||
path,
|
||||
kind: "file",
|
||||
sha256: "a".repeat(64),
|
||||
size: Number.MAX_SAFE_INTEGER,
|
||||
content_state: "hashed",
|
||||
language: null,
|
||||
classifications: ["unknown"],
|
||||
limitations: [],
|
||||
})),
|
||||
),
|
||||
);
|
||||
expect(createHistoricalSourceSummary(graph, 0)).toMatchObject({
|
||||
file_sizes: { known_bytes: "18014398509481982", unknown_file_count: 0 },
|
||||
outline: { entries: [], omitted_entry_count: 2 },
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,93 @@
|
||||
import {
|
||||
createHistoricalSourceManifest,
|
||||
type HistoricalSourceGraph,
|
||||
} from "./referenceSourceGraph.js";
|
||||
|
||||
type Entry = HistoricalSourceGraph["entries"][number];
|
||||
|
||||
const outlineEntry = (entry: Entry) => {
|
||||
if (entry.kind !== "file") return entry;
|
||||
return {
|
||||
path: entry.path,
|
||||
kind: entry.kind,
|
||||
language: entry.language,
|
||||
size: entry.size,
|
||||
content_state: entry.content_state,
|
||||
classifications: entry.classifications,
|
||||
limitations: entry.limitations,
|
||||
};
|
||||
};
|
||||
|
||||
/** Project one committed inventory without rereading or reinterpreting source. */
|
||||
export const createHistoricalSourceSummary = (
|
||||
graph: HistoricalSourceGraph,
|
||||
outlineDepth: number,
|
||||
) => {
|
||||
const manifest = createHistoricalSourceManifest(graph);
|
||||
const counts = {
|
||||
entries: graph.entries.length,
|
||||
files: 0,
|
||||
directories: 0,
|
||||
symlinks: 0,
|
||||
relationships: graph.relationships.length,
|
||||
parse_failures: graph.parse_failures.length,
|
||||
exclusions: graph.exclusions.length,
|
||||
};
|
||||
const contentStates = {
|
||||
hashed: 0,
|
||||
"redacted-secret": 0,
|
||||
excluded: 0,
|
||||
unreadable: 0,
|
||||
unknown: 0,
|
||||
};
|
||||
const treeStates = {
|
||||
enumerated: 0,
|
||||
partial: 0,
|
||||
excluded: 0,
|
||||
unreadable: 0,
|
||||
unknown: 0,
|
||||
};
|
||||
let knownBytes = 0n;
|
||||
let unknownSizeFiles = 0;
|
||||
const outline: ReturnType<typeof outlineEntry>[] = [];
|
||||
for (const entry of graph.entries) {
|
||||
if (entry.kind === "file") {
|
||||
counts.files += 1;
|
||||
contentStates[entry.content_state] += 1;
|
||||
if (entry.size === null) unknownSizeFiles += 1;
|
||||
else knownBytes += BigInt(entry.size);
|
||||
} else if (entry.kind === "directory") {
|
||||
counts.directories += 1;
|
||||
treeStates[entry.tree_state] += 1;
|
||||
} else counts.symlinks += 1;
|
||||
// Canonical entries already use Unicode code-point order; filtering preserves it.
|
||||
if (entry.path.split("/").length <= outlineDepth)
|
||||
outline.push(outlineEntry(entry));
|
||||
}
|
||||
return {
|
||||
schema: "HistoricalSourceSummary" as const,
|
||||
authority: graph.authority,
|
||||
root_alias: graph.root_alias,
|
||||
manifest,
|
||||
inventory_state: graph.inventory_state,
|
||||
provenance: graph.provenance,
|
||||
vcs: graph.vcs,
|
||||
languages: graph.languages,
|
||||
manifests: graph.manifests,
|
||||
counts,
|
||||
file_content_states: contentStates,
|
||||
directory_tree_states: treeStates,
|
||||
file_sizes: {
|
||||
// Decimal text keeps large totals exact; this covers only known inventoried sizes.
|
||||
known_bytes: knownBytes.toString(),
|
||||
unknown_file_count: unknownSizeFiles,
|
||||
},
|
||||
exclusions: graph.exclusions,
|
||||
limitations: graph.limitations,
|
||||
outline: {
|
||||
depth: outlineDepth,
|
||||
entries: outline,
|
||||
omitted_entry_count: graph.entries.length - outline.length,
|
||||
},
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user