Files
reverse-engineering-anything/src/domain/analysisView/artifactExtractionView.ts
T

297 lines
9.6 KiB
TypeScript

import { viewParentIdentity } from "./viewParentIdentity.js";
import { z } from "zod";
import {
artifactExtractionResultSchema,
integrityContradictionSchema,
type ArtifactExtractionResult,
} from "../artifactGraph.js";
import { boundedList, byteBoundedList } from "../boundedList.js";
import { prefixedDigestSchema } from "../digests.js";
import { evidenceSubjectTarget } from "../evidence.js";
import { ok } from "../result.js";
import {
byteBoundedItemsPageOwner,
selectByteBoundedViewRows,
type ByteBoundedPageOwner,
} from "./byteBoundedViewRows.js";
import {
invalidAnalysisViewSubject,
invalidAnalysisView,
} from "./analysisViewErrors.js";
import {
createAnalysisViewResultReader,
type AnalysisViewParent,
} from "./analysisViewParent.js";
import {
boundedViewLimitations,
completeWithinViewCoverage,
byteBoundedItemsPage,
} from "./analysisViewCoverage.js";
import {
ownedView,
type AdmittedView,
type SummaryAnalysisView,
} from "./analysisViewShape.js";
import {
compileInventoryPathGlob,
inventoryPathGlobSchema,
} from "./inventoryPathGlob.js";
import { filterSet, pickFields } from "./selectViewRows.js";
export const EXTRACT_ARTIFACT_OPERATION = "extract_artifact" as const;
type Extracted = ArtifactExtractionResult["artifacts"][number];
/** Rows of a collection that a summary samples before counting the rest. */
const SUMMARY_SAMPLE = 16;
/**
* Serialized bytes each sampled collection may use. A contradiction can carry
* signer names of tens of kilobytes; it is counted and paged instead.
*/
const SUMMARY_SAMPLE_BYTES = 4096;
const count = z.int().nonnegative();
const page = {
kind: z.literal("artifact-extraction"),
offset: z.int().nonnegative().default(0),
limit: z.int().positive().default(64),
};
/** Fields of one written file that a page may select. */
export const extractedArtifactFieldSchema =
artifactExtractionResultSchema.shape.artifacts.element.keyof();
/** One page of a retained extraction's written files or its other lists. */
export const artifactExtractionViewRequestSchema = z.discriminatedUnion(
"collection",
[
z.strictObject({
...page,
collection: z.literal("artifacts"),
relative_paths: z
.array(z.string())
.optional()
.describe("Exact written paths relative to output_root"),
path_globs: z
.array(inventoryPathGlobSchema)
.optional()
.describe(
'Whole-path globs over written relative paths, any of which may match: * and ? stay within one segment and a ** segment spans segments, e.g. ["lib/*/*.so"] or ["assets/**"]; [] matches none.',
),
artifact_ids: z.array(prefixedDigestSchema("art")).optional(),
fields: z
.array(extractedArtifactFieldSchema)
.min(1)
.optional()
.describe("Row fields to return beside index; omit for every field"),
}),
z.strictObject({
...page,
collection: z.enum(["integrity_contradictions", "residual_paths"]),
}),
],
);
type ExtractionView = z.output<typeof artifactExtractionViewRequestSchema>;
const sampled = <Item extends z.ZodType>(item: Item, description: string) =>
z
.strictObject({ items: z.array(item), omitted: count })
.describe(
`${description} in order, at most ${String(SUMMARY_SAMPLE)} within ${String(SUMMARY_SAMPLE_BYTES)} serialized bytes; omitted counts the rest, which the collection pages`,
);
const resultShape = artifactExtractionResultSchema.shape;
/** The extraction's identity, verification and counts in place of its lists. */
export const artifactExtractionSummarySchema = z.strictObject({
effective_limits: resultShape.effective_limits,
manifest: resultShape.manifest,
extraction_manifest: resultShape.extraction_manifest
.omit({ selected_occurrence_ids: true })
.extend({ selected_occurrences: count }),
output_root: resultShape.output_root,
containment_verified: resultShape.containment_verified,
cleanup: resultShape.cleanup.omit({ residual_paths: true }).extend({
residual_paths: sampled(
resultShape.cleanup.shape.residual_paths.element,
"Paths cleanup could not remove",
),
}),
coverage: resultShape.coverage,
counts: z.strictObject({
artifacts: count,
created: count,
bytes_written: count,
integrity_contradictions: count,
}),
provenance: sampled(
resultShape.provenance.element,
"Commands that produced the extraction",
),
integrity_contradictions: sampled(
integrityContradictionSchema,
"Retained integrity contradictions",
),
});
/** One written file with its position in the complete extraction. */
export const extractedArtifactRowSchema = resultShape.artifacts.element
.partial()
.extend({ index: count });
const readExtraction = createAnalysisViewResultReader(
artifactExtractionResultSchema,
);
/** A leading sample bounded by count and bytes; an oversized row ends it. */
const sample = <Item>(items: readonly Item[]) => {
const listed = boundedList(items, SUMMARY_SAMPLE);
const kept = byteBoundedList(listed.items, SUMMARY_SAMPLE_BYTES).items;
return { items: kept, omitted: items.length - kept.length };
};
const summarize = (
extraction: ArtifactExtractionResult,
): z.output<typeof artifactExtractionSummarySchema> => {
let created = 0;
let bytesWritten = 0;
for (const artifact of extraction.artifacts) {
if (artifact.created) created += 1;
bytesWritten += artifact.bytes_written;
}
const { selected_occurrence_ids, ...manifest } =
extraction.extraction_manifest;
const { residual_paths, ...cleanup } = extraction.cleanup;
return {
...(extraction.effective_limits === undefined
? {}
: { effective_limits: extraction.effective_limits }),
manifest: extraction.manifest,
extraction_manifest: {
...manifest,
selected_occurrences: selected_occurrence_ids.length,
},
output_root: extraction.output_root,
containment_verified: extraction.containment_verified,
cleanup: { ...cleanup, residual_paths: sample(residual_paths) },
coverage: extraction.coverage,
counts: {
artifacts: extraction.artifacts.length,
created,
bytes_written: bytesWritten,
integrity_contradictions: extraction.integrity_contradictions.length,
},
provenance: sample(extraction.provenance),
integrity_contradictions: sample(extraction.integrity_contradictions),
};
};
/** Exact selectors intersect; an empty selector matches nothing. */
const artifactMatcher = (
view: Extract<ExtractionView, { collection: "artifacts" }>,
) => {
const selectors = [view.relative_paths, view.path_globs, view.artifact_ids];
if (selectors.every((selector) => selector === undefined)) return undefined;
const paths = filterSet(view.relative_paths);
const ids = filterSet(view.artifact_ids);
const globs = view.path_globs?.map(compileInventoryPathGlob);
return (row: Extracted) =>
(paths === undefined || paths.has(row.relative_path)) &&
(ids === undefined || ids.has(row.artifact_id)) &&
(globs === undefined || globs.some((glob) => glob(row.relative_path)));
};
/** One byte-bounded page of the selected collection. */
const collectionPage = (
extraction: ArtifactExtractionResult,
view: ExtractionView,
owner: ByteBoundedPageOwner,
) => {
if (view.collection === "artifacts") {
const fields = filterSet(view.fields);
return selectByteBoundedViewRows(
extraction.artifacts,
view,
{
matches: artifactMatcher(view),
project: (row, index): z.output<typeof extractedArtifactRowSchema> => ({
index,
...(fields === undefined ? row : pickFields(row, fields)),
}),
},
owner,
);
}
const rows: readonly (
| string
| ArtifactExtractionResult["integrity_contradictions"][number]
)[] =
view.collection === "residual_paths"
? extraction.cleanup.residual_paths
: extraction.integrity_contradictions;
return selectByteBoundedViewRows(
rows,
view,
{ matches: undefined, project: (row) => row },
owner,
);
};
/** Project a retained extraction's summary or one page of its lists. */
export const projectArtifactExtractionView = (
parent: AnalysisViewParent,
request: AdmittedView,
) => {
const admitted = ownedView<SummaryAnalysisView | ExtractionView>(
request,
"summary",
"artifact-extraction",
);
if (admitted === undefined)
return invalidAnalysisView(
"Select an artifact-extraction or summary view.",
"kind",
);
const parsed = readExtraction(parent);
if (!parsed.ok) return parsed;
const subject = evidenceSubjectTarget(parent.subject);
if (subject === undefined)
return invalidAnalysisViewSubject(
"Artifact extraction views require the recorded artifact subject identity.",
);
const extraction = parsed.value;
const shared = {
...viewParentIdentity(parent, EXTRACT_ARTIFACT_OPERATION),
artifact: { path: subject.path, sha256: subject.sha256 },
limitations: boundedViewLimitations(parent),
unknowns: [
"Written files reflect the extraction when it ran; later changes under output_root are not observed.",
],
};
if (admitted.kind === "summary")
return ok({
...shared,
kind: "summary" as const,
view: admitted,
summary: summarize(extraction),
coverage: completeWithinViewCoverage(1),
});
const view = admitted;
const context = {
...shared,
kind: "artifact-extraction" as const,
view,
output_root: extraction.output_root,
};
const owner = byteBoundedItemsPageOwner(
parent.evidenceId,
view.collection,
context,
);
const page = collectionPage(extraction, view, owner);
if (!page.ok) return page;
return ok(byteBoundedItemsPage(context, page.value));
};