feat(analysis): add opt-in retained inventory and WASM views (#2122)
Add opt-in retained inventory and WASM Evidence views with exact nested output contracts and a small static WASM descriptor. Preserve old complete CLI/MCP workflows, producer choices, original authenticated Evidence and cleanup ownership. Validated with real public applications, genuine old Evidence migration, 123 focused assertions, 11 synchronized description checks and 42 installed-package WABT cases. Detailed generated experiment JSON stays outside the final tree; concise findings and reproduction inputs remain. Main-existing completion/package CI failures are separately attributed and left unchanged. Refs #2016 Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
parent
3159732597
commit
58fb372071
@@ -8,12 +8,20 @@ import {
|
||||
} from "../analysisErrorCore.js";
|
||||
import { digestCanonicalValue } from "../canonicalDigest.js";
|
||||
import { digestSchema, prefixedDigestSchema } from "../digests.js";
|
||||
import { evidenceSchema } from "../evidence.js";
|
||||
import { evidenceSchema, type Evidence } from "../evidence.js";
|
||||
import { jsonObjectSchema, jsonValueSchema } from "../jsonValue.js";
|
||||
import { err, ok, type Result } from "../result.js";
|
||||
import type { AnalysisError } from "../analysisErrorBase.js";
|
||||
import { binaryLayoutSchema } from "../native/binaryLayout.js";
|
||||
import { functionDossierSchema } from "../hopperValues.js";
|
||||
import {
|
||||
artifactGraphManifestSchema,
|
||||
artifactNodeSchema,
|
||||
artifactOccurrenceSchema,
|
||||
artifactEdgeSchema,
|
||||
integrityContradictionSchema,
|
||||
} from "../artifactGraph.js";
|
||||
import { artifactInspectionResultSchema } from "../artifactInspection.js";
|
||||
import {
|
||||
parseOwnedJavaScriptApplicationAnalysisSteps,
|
||||
type JavaScriptApplicationAnalysisResult,
|
||||
@@ -27,6 +35,13 @@ import {
|
||||
apiSurfaceViewSchema,
|
||||
} from "./webNetworkApiSurfaceView.js";
|
||||
import { projectNativeFunctionView } from "./nativeFunctionView.js";
|
||||
import { projectArtifactInventoryView } from "./artifactInventoryView.js";
|
||||
import {
|
||||
projectWasmArtifactView,
|
||||
wasmViewRequestSchema,
|
||||
wasmViewItemSchema,
|
||||
wasmSummarySchema,
|
||||
} from "./wasmArtifactView.js";
|
||||
import type { WebNetworkApiSurfaceAccumulator } from "./WebNetworkApiSurfaceAccumulator.js";
|
||||
|
||||
/** Public name of the selected-view workflow. */
|
||||
@@ -108,6 +123,28 @@ const nativeViewSchema = z.strictObject({
|
||||
limit: z.number().int().positive().default(64),
|
||||
});
|
||||
|
||||
/** Original inventory records in stable order, optionally matching exact paths. */
|
||||
const inventoryViewSchema = z.discriminatedUnion("collection", [
|
||||
z.strictObject({
|
||||
kind: z.literal("inventory"),
|
||||
collection: z.literal("occurrences"),
|
||||
logical_paths: z
|
||||
.array(z.string())
|
||||
.optional()
|
||||
.describe(
|
||||
"Exact recorded logical paths. Omitted selects all available occurrences; an empty array selects none. This does not resolve filesystem paths.",
|
||||
),
|
||||
offset: z.number().int().nonnegative().default(0),
|
||||
limit: z.number().int().positive().default(64),
|
||||
}),
|
||||
z.strictObject({
|
||||
kind: z.literal("inventory"),
|
||||
collection: z.enum(["nodes", "edges", "integrity_contradictions"]),
|
||||
offset: z.number().int().nonnegative().default(0),
|
||||
limit: z.number().int().positive().default(64),
|
||||
}),
|
||||
]);
|
||||
|
||||
/** Caller-selected projection of already completed analysis Evidence. */
|
||||
export const analysisViewRequestSchema = z.discriminatedUnion("kind", [
|
||||
summaryViewSchema,
|
||||
@@ -115,6 +152,8 @@ export const analysisViewRequestSchema = z.discriminatedUnion("kind", [
|
||||
itemViewSchema,
|
||||
pageViewSchema,
|
||||
nativeViewSchema,
|
||||
inventoryViewSchema,
|
||||
wasmViewRequestSchema,
|
||||
apiSurfaceViewSchema,
|
||||
]);
|
||||
|
||||
@@ -192,30 +231,30 @@ const javascriptSummarySchema = z.strictObject({
|
||||
limitation_count: z.number().int().nonnegative(),
|
||||
});
|
||||
|
||||
const summaryViewResultFields = {
|
||||
kind: z.literal("summary"),
|
||||
view: summaryViewSchema,
|
||||
parent_evidence_id: prefixedDigestSchema("ev"),
|
||||
parent_digest: digestSchema,
|
||||
view_digest: digestSchema,
|
||||
artifact: artifactSchema,
|
||||
coverage: coverageSchema,
|
||||
limitations: z.array(z.string()),
|
||||
unknowns: z.array(z.string()),
|
||||
} as const;
|
||||
|
||||
/** Summary `inspect_binary_layout` detail returns; no other view kind. */
|
||||
export const binaryLayoutSummaryViewResultSchema = z.strictObject({
|
||||
...summaryViewResultFields,
|
||||
summary: layoutSummarySchema,
|
||||
parent_operation: z.literal("inspect_binary_layout"),
|
||||
});
|
||||
|
||||
/** Summary `analyze_javascript_application` detail returns; no other view kind. */
|
||||
export const javascriptApplicationSummaryViewResultSchema = z.strictObject({
|
||||
...summaryViewResultFields,
|
||||
summary: javascriptSummarySchema,
|
||||
parent_operation: z.literal("analyze_javascript_application"),
|
||||
const inventorySummarySchema = z.strictObject({
|
||||
manifest: artifactGraphManifestSchema,
|
||||
available_counts: z.strictObject({
|
||||
nodes: z.number().int().nonnegative(),
|
||||
occurrences: z.number().int().nonnegative(),
|
||||
edges: z.number().int().nonnegative(),
|
||||
integrity_contradictions: z.number().int().nonnegative(),
|
||||
}),
|
||||
observed_entry_kind_counts: z.strictObject({
|
||||
file: z.number().int().nonnegative(),
|
||||
directory: z.number().int().nonnegative(),
|
||||
symlink: z.number().int().nonnegative(),
|
||||
slice: z.number().int().nonnegative(),
|
||||
}),
|
||||
source_complete: z.boolean(),
|
||||
inventory_evidence_ids: z.array(prefixedDigestSchema("ev")).min(1),
|
||||
inspection: z
|
||||
.strictObject({
|
||||
coverage: artifactInspectionResultSchema.shape.coverage,
|
||||
unexplored_branches:
|
||||
artifactInspectionResultSchema.shape.unexplored_branches,
|
||||
})
|
||||
.nullable(),
|
||||
limitation_count: z.number().int().nonnegative(),
|
||||
});
|
||||
|
||||
const viewResultBase = {
|
||||
@@ -224,6 +263,9 @@ const viewResultBase = {
|
||||
"inspect_binary_layout",
|
||||
"analyze_javascript_application",
|
||||
"analyze_function",
|
||||
"inspect_artifact",
|
||||
"inventory_artifact",
|
||||
"inspect_wasm_artifact",
|
||||
]),
|
||||
parent_digest: digestSchema,
|
||||
view_digest: digestSchema,
|
||||
@@ -233,6 +275,33 @@ const viewResultBase = {
|
||||
unknowns: z.array(z.string()),
|
||||
} as const;
|
||||
|
||||
const summaryViewResultBase = {
|
||||
kind: z.literal("summary"),
|
||||
view: summaryViewSchema,
|
||||
...viewResultBase,
|
||||
} as const;
|
||||
|
||||
/** Summary `inspect_binary_layout` detail returns; no other view kind. */
|
||||
export const binaryLayoutSummaryViewResultSchema = z.strictObject({
|
||||
...summaryViewResultBase,
|
||||
summary: layoutSummarySchema,
|
||||
parent_operation: z.literal("inspect_binary_layout"),
|
||||
});
|
||||
|
||||
/** Summary `analyze_javascript_application` detail returns; no other view kind. */
|
||||
export const javascriptApplicationSummaryViewResultSchema = z.strictObject({
|
||||
...summaryViewResultBase,
|
||||
summary: javascriptSummarySchema,
|
||||
parent_operation: z.literal("analyze_javascript_application"),
|
||||
});
|
||||
|
||||
/** Exact summary carrier for artifact inspection's optional delivery contract. */
|
||||
export const artifactInventorySummaryViewResultSchema = z.strictObject({
|
||||
...summaryViewResultBase,
|
||||
parent_operation: z.enum(["inspect_artifact", "inventory_artifact"]),
|
||||
summary: inventorySummarySchema,
|
||||
});
|
||||
|
||||
/** Projected facts for one selected view of retained analysis Evidence. */
|
||||
export const analysisViewResultSchema = z.discriminatedUnion("kind", [
|
||||
z.strictObject({
|
||||
@@ -247,10 +316,13 @@ export const analysisViewResultSchema = z.discriminatedUnion("kind", [
|
||||
}),
|
||||
}),
|
||||
z.strictObject({
|
||||
kind: z.literal("summary"),
|
||||
view: summaryViewSchema,
|
||||
summary: z.union([layoutSummarySchema, javascriptSummarySchema]),
|
||||
...viewResultBase,
|
||||
...summaryViewResultBase,
|
||||
summary: z.union([
|
||||
layoutSummarySchema,
|
||||
javascriptSummarySchema,
|
||||
inventorySummarySchema,
|
||||
wasmSummarySchema,
|
||||
]),
|
||||
}),
|
||||
z.strictObject({
|
||||
kind: z.literal("facet"),
|
||||
@@ -277,6 +349,28 @@ export const analysisViewResultSchema = z.discriminatedUnion("kind", [
|
||||
item: jsonValueSchema,
|
||||
...viewResultBase,
|
||||
}),
|
||||
z.strictObject({
|
||||
kind: z.literal("wasm"),
|
||||
view: wasmViewRequestSchema,
|
||||
item: wasmViewItemSchema,
|
||||
...viewResultBase,
|
||||
}),
|
||||
z.strictObject({
|
||||
kind: z.literal("inventory"),
|
||||
view: inventoryViewSchema,
|
||||
manifest: artifactGraphManifestSchema,
|
||||
inventory_evidence_ids: z.array(prefixedDigestSchema("ev")).min(1),
|
||||
source_complete: z.boolean(),
|
||||
items: z.array(
|
||||
z.union([
|
||||
artifactNodeSchema,
|
||||
artifactOccurrenceSchema,
|
||||
artifactEdgeSchema,
|
||||
integrityContradictionSchema,
|
||||
]),
|
||||
),
|
||||
...viewResultBase,
|
||||
}),
|
||||
]);
|
||||
|
||||
export type AnalysisViewRequest = z.output<typeof analysisViewRequestSchema>;
|
||||
@@ -291,6 +385,8 @@ export interface AnalysisViewParent {
|
||||
readonly evidenceId: string;
|
||||
readonly operation: string;
|
||||
readonly normalizedResult: unknown;
|
||||
/** Preserve the authenticated source identity, including explicit absence. */
|
||||
readonly subject?: Evidence["subject"];
|
||||
readonly limitations: readonly string[];
|
||||
readonly artifact?: { readonly path: string; readonly sha256: string } | null;
|
||||
/** Parent Evidence parameters, when the parent operation records them. */
|
||||
@@ -319,7 +415,12 @@ export type UnsignedAnalysisView =
|
||||
| Omit<
|
||||
Extract<AnalysisViewResult, { readonly kind: "native" }>,
|
||||
"view_digest"
|
||||
>;
|
||||
>
|
||||
| Omit<
|
||||
Extract<AnalysisViewResult, { readonly kind: "inventory" }>,
|
||||
"view_digest"
|
||||
>
|
||||
| Omit<Extract<AnalysisViewResult, { readonly kind: "wasm" }>, "view_digest">;
|
||||
|
||||
/** Attach the content digest that must change when projected bytes change. */
|
||||
export const sealAnalysisView = (
|
||||
@@ -364,7 +465,7 @@ const unsupportedParent = (
|
||||
new AnalysisUnsupportedTargetError(
|
||||
INSPECT_ANALYSIS_VIEW_OPERATION,
|
||||
parentArtifactPath(parent),
|
||||
`Parent Evidence operation ${JSON.stringify(parent.operation)} is not inspect_binary_layout, analyze_javascript_application or analyze_function.`,
|
||||
`Parent Evidence operation ${JSON.stringify(parent.operation)} does not support analysis views. Use completed binary layout, JavaScript application, native function, artifact inventory/inspection, WASM, or web network capture Evidence.`,
|
||||
);
|
||||
|
||||
const parentArtifactPath = (parent: AnalysisViewParent): string => {
|
||||
@@ -459,5 +560,16 @@ export const projectAnalysisView = (
|
||||
const projected = projectNativeFunctionView(parent, native.data, view);
|
||||
return projected.ok ? ok(sealAnalysisView(projected.value)) : projected;
|
||||
}
|
||||
if (parent.operation === "inspect_wasm_artifact") {
|
||||
const projected = projectWasmArtifactView(parent, view);
|
||||
return projected.ok ? ok(sealAnalysisView(projected.value)) : projected;
|
||||
}
|
||||
if (
|
||||
parent.operation === "inspect_artifact" ||
|
||||
parent.operation === "inventory_artifact"
|
||||
) {
|
||||
const projected = projectArtifactInventoryView(parent, view);
|
||||
return projected.ok ? ok(sealAnalysisView(projected.value)) : projected;
|
||||
}
|
||||
return err(unsupportedParent(parent));
|
||||
};
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import type { AnalysisError } from "../analysisErrorBase.js";
|
||||
import { err, ok, type Result } from "../result.js";
|
||||
import {
|
||||
analysisViewInputError,
|
||||
type AnalysisViewCoverage,
|
||||
} from "./analysisView.js";
|
||||
import { pageViewCoverage } from "./analysisViewCoverage.js";
|
||||
|
||||
const viewError = (message: string): AnalysisError =>
|
||||
analysisViewInputError([
|
||||
{ path: ["view"], reason: "invalid_value", message },
|
||||
]);
|
||||
|
||||
/** Slice text by UTF-16 code units without losing a partial Unicode character. */
|
||||
export const analysisTextPage = (
|
||||
body: string,
|
||||
offset: number,
|
||||
limit: number,
|
||||
label = "Pseudocode",
|
||||
): Result<
|
||||
{
|
||||
readonly item: { readonly text: string; readonly unit: "utf16-code-units" };
|
||||
readonly coverage: AnalysisViewCoverage;
|
||||
},
|
||||
AnalysisError
|
||||
> => {
|
||||
const start = Math.min(offset, body.length);
|
||||
const high = (n: number) => n >= 0xd800 && n <= 0xdbff;
|
||||
const low = (n: number) => n >= 0xdc00 && n <= 0xdfff;
|
||||
if (
|
||||
start > 0 &&
|
||||
start < body.length &&
|
||||
high(body.charCodeAt(start - 1)) &&
|
||||
low(body.charCodeAt(start))
|
||||
)
|
||||
return err(viewError(`${label} offset splits a UTF-16 surrogate pair.`));
|
||||
let end = Math.min(start + limit, body.length);
|
||||
if (
|
||||
end > start &&
|
||||
end < body.length &&
|
||||
high(body.charCodeAt(end - 1)) &&
|
||||
low(body.charCodeAt(end))
|
||||
)
|
||||
end--;
|
||||
if (end === start && start < body.length)
|
||||
return err(
|
||||
viewError(
|
||||
`Increase ${label.toLowerCase()} limit to include the next complete Unicode character.`,
|
||||
),
|
||||
);
|
||||
return ok({
|
||||
item: { text: body.slice(start, end), unit: "utf16-code-units" },
|
||||
coverage: pageViewCoverage(start, end - start, body.length),
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,203 @@
|
||||
import { z } from "zod";
|
||||
import { AnalysisInputError } from "../analysisErrorCore.js";
|
||||
import type { AnalysisError } from "../analysisErrorBase.js";
|
||||
import { uniqueSorted } from "../canonicalOrdering.js";
|
||||
import { err, ok, type Result } from "../result.js";
|
||||
import {
|
||||
parseArtifactInventoryEvidence,
|
||||
parseArtifactInventoryResult,
|
||||
validateArtifactInventorySubject,
|
||||
type InventorySet,
|
||||
} from "../artifactInventoryEvidence.js";
|
||||
import {
|
||||
artifactInspectionResultSchema,
|
||||
type ArtifactInspectionResult,
|
||||
} from "../artifactInspection.js";
|
||||
import type {
|
||||
AnalysisViewParent,
|
||||
AnalysisViewRequest,
|
||||
UnsignedAnalysisView,
|
||||
} from "./analysisView.js";
|
||||
import {
|
||||
completeWithinViewCoverage,
|
||||
pageViewCoverage,
|
||||
} from "./analysisViewCoverage.js";
|
||||
|
||||
const inputError = (message: string): AnalysisInputError =>
|
||||
new AnalysisInputError("inspect_analysis_view", undefined, [
|
||||
{
|
||||
path: ["source"],
|
||||
reason: "invalid_value",
|
||||
message,
|
||||
},
|
||||
]);
|
||||
|
||||
interface InventorySource {
|
||||
readonly inventory: InventorySet;
|
||||
readonly evidenceIds: readonly string[];
|
||||
readonly inspection: ArtifactInspectionResult | null;
|
||||
}
|
||||
|
||||
/** Reuse the owning inventory integrity/completeness parser for both carriers. */
|
||||
const parseSource = (
|
||||
parent: AnalysisViewParent,
|
||||
): Result<InventorySource, AnalysisError> => {
|
||||
try {
|
||||
if (parent.operation === "inventory_artifact")
|
||||
return ok({
|
||||
inventory: parseArtifactInventoryResult(
|
||||
parent.normalizedResult,
|
||||
parent.subject ?? null,
|
||||
),
|
||||
evidenceIds: [parent.evidenceId],
|
||||
inspection: null,
|
||||
});
|
||||
const inspection = artifactInspectionResultSchema.parse(
|
||||
parent.normalizedResult,
|
||||
);
|
||||
for (const step of inspection.substeps) {
|
||||
if (
|
||||
step.evidence_id !== step.evidence.evidence_id ||
|
||||
step.evidence.operation !== step.operation
|
||||
)
|
||||
throw new TypeError(
|
||||
"Inspection substep identity differs from its source Evidence",
|
||||
);
|
||||
}
|
||||
const parsed = parseArtifactInventoryEvidence(
|
||||
inspection.substeps.map(({ evidence }) => evidence),
|
||||
);
|
||||
const manifest = parsed.inventory.manifest;
|
||||
validateArtifactInventorySubject(parent.subject ?? null, manifest);
|
||||
if (
|
||||
inspection.subject.manifest_id !== manifest.manifest_id ||
|
||||
inspection.subject.root_artifact_id !== manifest.root_artifact_id ||
|
||||
inspection.subject.root_sha256 !== manifest.root_sha256 ||
|
||||
inspection.subject.root_format !== manifest.root_format
|
||||
)
|
||||
throw new TypeError(
|
||||
"Inspection subject differs from its inventory manifest",
|
||||
);
|
||||
return ok({
|
||||
inventory: parsed.inventory,
|
||||
evidenceIds: parsed.evidence.map(({ evidence_id }) => evidence_id),
|
||||
inspection,
|
||||
});
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof TypeError) && !(cause instanceof z.ZodError))
|
||||
throw cause;
|
||||
return err(inputError(cause.message));
|
||||
}
|
||||
};
|
||||
|
||||
/** Project exact source records; exhausted pages cover available records, not runtime behavior. */
|
||||
export const projectArtifactInventoryView = (
|
||||
parent: AnalysisViewParent,
|
||||
view: AnalysisViewRequest,
|
||||
): Result<UnsignedAnalysisView, AnalysisError> => {
|
||||
const operation = parent.operation;
|
||||
if (operation !== "inventory_artifact" && operation !== "inspect_artifact")
|
||||
return err(
|
||||
inputError(
|
||||
"Inventory views require inventory_artifact or inspect_artifact Evidence.",
|
||||
),
|
||||
);
|
||||
if (view.kind !== "summary" && view.kind !== "inventory")
|
||||
return err(
|
||||
inputError("Artifact Evidence supports summary and inventory views."),
|
||||
);
|
||||
const parsed = parseSource(parent);
|
||||
if (!parsed.ok) return parsed;
|
||||
const { inventory, evidenceIds, inspection } = parsed.value;
|
||||
const limitations = uniqueSorted([
|
||||
...parent.limitations,
|
||||
...inventory.limitations,
|
||||
...(inspection?.limitations ?? []),
|
||||
]);
|
||||
const shared = {
|
||||
parent_evidence_id: parent.evidenceId,
|
||||
parent_operation: operation,
|
||||
parent_digest: parent.evidenceId.slice(3),
|
||||
artifact: {
|
||||
path: parent.artifact?.path ?? "",
|
||||
sha256: inventory.manifest.root_sha256,
|
||||
},
|
||||
limitations,
|
||||
unknowns: [
|
||||
"Inventory does not establish application runtime behavior or complete format-specific analysis.",
|
||||
...(inventory.complete
|
||||
? []
|
||||
: [
|
||||
"Parent inventory is partial; counts and exhausted pages cover only available records. Missing matches do not establish absence.",
|
||||
]),
|
||||
],
|
||||
} satisfies Pick<
|
||||
UnsignedAnalysisView,
|
||||
| "parent_evidence_id"
|
||||
| "parent_operation"
|
||||
| "parent_digest"
|
||||
| "artifact"
|
||||
| "limitations"
|
||||
| "unknowns"
|
||||
>;
|
||||
if (view.kind === "summary") {
|
||||
const counts = { file: 0, directory: 0, symlink: 0, slice: 0 };
|
||||
for (const occurrence of inventory.occurrences)
|
||||
counts[occurrence.entry_kind]++;
|
||||
const examined =
|
||||
inventory.nodes.length +
|
||||
inventory.occurrences.length +
|
||||
inventory.edges.length;
|
||||
return ok({
|
||||
kind: "summary",
|
||||
view,
|
||||
summary: {
|
||||
manifest: inventory.manifest,
|
||||
available_counts: {
|
||||
nodes: inventory.nodes.length,
|
||||
occurrences: inventory.occurrences.length,
|
||||
edges: inventory.edges.length,
|
||||
integrity_contradictions: inventory.integrityContradictions.length,
|
||||
},
|
||||
observed_entry_kind_counts: counts,
|
||||
source_complete: inventory.complete,
|
||||
inventory_evidence_ids: [...evidenceIds],
|
||||
inspection:
|
||||
inspection === null
|
||||
? null
|
||||
: {
|
||||
coverage: inspection.coverage,
|
||||
unexplored_branches: inspection.unexplored_branches,
|
||||
},
|
||||
limitation_count: limitations.length,
|
||||
},
|
||||
coverage: completeWithinViewCoverage(examined, examined),
|
||||
...shared,
|
||||
});
|
||||
}
|
||||
const paths =
|
||||
view.collection === "occurrences" && view.logical_paths !== undefined
|
||||
? new Set(view.logical_paths)
|
||||
: undefined;
|
||||
const rows =
|
||||
view.collection === "occurrences" && paths !== undefined
|
||||
? inventory.occurrences.filter((row) => paths.has(row.logical_path))
|
||||
: view.collection === "occurrences"
|
||||
? inventory.occurrences
|
||||
: view.collection === "nodes"
|
||||
? inventory.nodes
|
||||
: view.collection === "edges"
|
||||
? inventory.edges
|
||||
: inventory.integrityContradictions;
|
||||
const items = rows.slice(view.offset, view.offset + view.limit);
|
||||
return ok({
|
||||
kind: "inventory",
|
||||
view,
|
||||
manifest: inventory.manifest,
|
||||
inventory_evidence_ids: [...evidenceIds],
|
||||
source_complete: inventory.complete,
|
||||
items,
|
||||
coverage: pageViewCoverage(view.offset, items.length, rows.length),
|
||||
...shared,
|
||||
});
|
||||
};
|
||||
@@ -156,6 +156,14 @@ export const projectBinaryLayoutView = (
|
||||
return err(
|
||||
layoutIncompatible("Native views require analyze_function Evidence."),
|
||||
);
|
||||
if (view.kind === "inventory")
|
||||
return err(
|
||||
layoutIncompatible("Inventory views require artifact Evidence."),
|
||||
);
|
||||
if (view.kind === "wasm")
|
||||
return err(
|
||||
layoutIncompatible("WASM views require inspect_wasm_artifact Evidence."),
|
||||
);
|
||||
if (view.kind === "summary")
|
||||
return ok({
|
||||
kind: "summary",
|
||||
|
||||
@@ -247,6 +247,12 @@ export const projectJavaScriptApplicationView = (
|
||||
return err(
|
||||
jsIncompatible("Native views require analyze_function Evidence."),
|
||||
);
|
||||
if (view.kind === "inventory")
|
||||
return err(jsIncompatible("Inventory views require artifact Evidence."));
|
||||
if (view.kind === "wasm")
|
||||
return err(
|
||||
jsIncompatible("WASM views require inspect_wasm_artifact Evidence."),
|
||||
);
|
||||
if (view.kind === "summary")
|
||||
return ok({
|
||||
kind: "summary",
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { analysisTextPage } from "./analysisViewText.js";
|
||||
import type { AnalysisError } from "../analysisErrorBase.js";
|
||||
import { uniqueSorted } from "../canonicalOrdering.js";
|
||||
import type { FunctionDossier } from "../hopperValues.js";
|
||||
@@ -5,7 +6,6 @@ import { jsonValueSchema } from "../jsonValue.js";
|
||||
import { err, ok, type Result } from "../result.js";
|
||||
import {
|
||||
analysisViewInputError,
|
||||
type AnalysisViewCoverage,
|
||||
type AnalysisViewParent,
|
||||
type AnalysisViewRequest,
|
||||
type UnsignedAnalysisView,
|
||||
@@ -59,47 +59,6 @@ const parentFields = (
|
||||
};
|
||||
};
|
||||
|
||||
const pseudocodePage = (
|
||||
body: string,
|
||||
offset: number,
|
||||
limit: number,
|
||||
): Result<
|
||||
{
|
||||
readonly item: { readonly text: string; readonly unit: "utf16-code-units" };
|
||||
readonly coverage: AnalysisViewCoverage;
|
||||
},
|
||||
AnalysisError
|
||||
> => {
|
||||
const start = Math.min(offset, body.length);
|
||||
const high = (n: number) => n >= 0xd800 && n <= 0xdbff;
|
||||
const low = (n: number) => n >= 0xdc00 && n <= 0xdfff;
|
||||
if (
|
||||
start > 0 &&
|
||||
start < body.length &&
|
||||
high(body.charCodeAt(start - 1)) &&
|
||||
low(body.charCodeAt(start))
|
||||
)
|
||||
return err(viewError("Pseudocode offset splits a UTF-16 surrogate pair."));
|
||||
let end = Math.min(start + limit, body.length);
|
||||
if (
|
||||
end > start &&
|
||||
end < body.length &&
|
||||
high(body.charCodeAt(end - 1)) &&
|
||||
low(body.charCodeAt(end))
|
||||
)
|
||||
end--;
|
||||
if (end === start && start < body.length)
|
||||
return err(
|
||||
viewError(
|
||||
"Increase pseudocode limit to include the next complete Unicode character.",
|
||||
),
|
||||
);
|
||||
return ok({
|
||||
item: { text: body.slice(start, end), unit: "utf16-code-units" },
|
||||
coverage: pageViewCoverage(start, end - start, body.length),
|
||||
});
|
||||
};
|
||||
|
||||
const nativeRows = (
|
||||
dossier: FunctionDossier,
|
||||
facet: Exclude<
|
||||
@@ -193,7 +152,7 @@ export const projectNativeFunctionView = (
|
||||
return ok(singleton(status));
|
||||
}
|
||||
if (view.facet === "pseudocode") {
|
||||
const page = pseudocodePage(dossier.pseudocode, view.offset, view.limit);
|
||||
const page = analysisTextPage(dossier.pseudocode, view.offset, view.limit);
|
||||
return page.ok
|
||||
? ok({ kind: "native", view, ...page.value, ...common })
|
||||
: page;
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
import { z } from "zod";
|
||||
import type { AnalysisError } from "../analysisErrorBase.js";
|
||||
import { uniqueSorted } from "../canonicalOrdering.js";
|
||||
import { err, ok, type Result } from "../result.js";
|
||||
import {
|
||||
wasmArtifactSchema,
|
||||
wasmArtifactMatchesSubject,
|
||||
} from "../wasm/wasmArtifact.js";
|
||||
import { analysisInputErrorFromIssues } from "../inputIssueProjection.js";
|
||||
import {
|
||||
analysisViewInputError,
|
||||
type AnalysisViewParent,
|
||||
type AnalysisViewRequest,
|
||||
type UnsignedAnalysisView,
|
||||
} from "./analysisView.js";
|
||||
import {
|
||||
completeWithinViewCoverage,
|
||||
pageViewCoverage,
|
||||
} from "./analysisViewCoverage.js";
|
||||
import { analysisTextPage } from "./analysisViewText.js";
|
||||
|
||||
/** Source forms and text from completed WASM Evidence; this never executes a VM. */
|
||||
export const wasmViewRequestSchema = z.strictObject({
|
||||
kind: z.literal("wasm"),
|
||||
facet: z.enum([
|
||||
"sections",
|
||||
"imports",
|
||||
"exports",
|
||||
"candidates",
|
||||
"glue",
|
||||
"wat",
|
||||
"headers",
|
||||
"details",
|
||||
"tool_profile",
|
||||
]),
|
||||
offset: z.number().int().nonnegative().default(0),
|
||||
limit: z.number().int().positive().default(64),
|
||||
});
|
||||
|
||||
export const wasmViewItemSchema = z.union([
|
||||
wasmArtifactSchema.shape.sections,
|
||||
wasmArtifactSchema.shape.imports,
|
||||
wasmArtifactSchema.shape.candidates,
|
||||
wasmArtifactSchema.shape.glue,
|
||||
wasmArtifactSchema.shape.tool_profile,
|
||||
z.strictObject({ text: z.string(), unit: z.literal("utf16-code-units") }),
|
||||
]);
|
||||
|
||||
/** Counts are observations of the completed producer, independent of runtime semantics. */
|
||||
export const wasmSummarySchema = z.strictObject({
|
||||
bytes: wasmArtifactSchema.shape.artifact.shape.bytes,
|
||||
validation: wasmArtifactSchema.shape.validation,
|
||||
available_counts: z.strictObject({
|
||||
sections: z.number().int().nonnegative(),
|
||||
imports: z.number().int().nonnegative(),
|
||||
exports: z.number().int().nonnegative(),
|
||||
candidates: z.number().int().nonnegative(),
|
||||
glue: z.number().int().nonnegative(),
|
||||
}),
|
||||
wat: wasmArtifactSchema.shape.wat
|
||||
.omit({ text: true })
|
||||
.extend({ code_units: z.number().int().nonnegative() }),
|
||||
tool_profile: wasmArtifactSchema.shape.tool_profile,
|
||||
runtime_execution: wasmArtifactSchema.shape.runtime_execution,
|
||||
network_fetch: wasmArtifactSchema.shape.network_fetch,
|
||||
limitation_count: z.number().int().nonnegative(),
|
||||
});
|
||||
|
||||
/** Read the exact retained WABT forms with source identity and explicit uncertainty. */
|
||||
export const projectWasmArtifactView = (
|
||||
parent: AnalysisViewParent,
|
||||
view: AnalysisViewRequest,
|
||||
): Result<UnsignedAnalysisView, AnalysisError> => {
|
||||
if (view.kind !== "summary" && view.kind !== "wasm")
|
||||
return err(
|
||||
analysisViewInputError([
|
||||
{
|
||||
path: ["view"],
|
||||
reason: "invalid_value",
|
||||
message: "WASM Evidence supports summary and wasm views.",
|
||||
},
|
||||
]),
|
||||
);
|
||||
const parsed = wasmArtifactSchema.safeParse(parent.normalizedResult);
|
||||
if (!parsed.success)
|
||||
return err(
|
||||
analysisInputErrorFromIssues(
|
||||
"inspect_analysis_view",
|
||||
parsed.error.issues.map((issue) => ({
|
||||
...issue,
|
||||
path: ["source", "normalized_result", ...issue.path],
|
||||
})),
|
||||
{ source: { normalized_result: parent.normalizedResult } },
|
||||
{ cause: parsed.error },
|
||||
),
|
||||
);
|
||||
const report = parsed.data;
|
||||
if (
|
||||
!wasmArtifactMatchesSubject(
|
||||
report.artifact,
|
||||
parent.subject == null
|
||||
? null
|
||||
: {
|
||||
path: parent.subject.local_path,
|
||||
sha256: parent.subject.digest.sha256,
|
||||
},
|
||||
)
|
||||
)
|
||||
return err(
|
||||
analysisViewInputError([
|
||||
{
|
||||
path: ["source", "subject"],
|
||||
reason: "invalid_value",
|
||||
message:
|
||||
"WASM parent Evidence subject must identify the report's exact path and SHA-256.",
|
||||
},
|
||||
]),
|
||||
);
|
||||
const limitations = uniqueSorted([
|
||||
...parent.limitations,
|
||||
...report.limitations,
|
||||
]);
|
||||
const shared = {
|
||||
parent_evidence_id: parent.evidenceId,
|
||||
parent_operation: "inspect_wasm_artifact" as const,
|
||||
parent_digest: parent.evidenceId.slice(3),
|
||||
artifact: { path: report.artifact.path, sha256: report.artifact.sha256 },
|
||||
limitations,
|
||||
unknowns: [
|
||||
"Decoded WAT is not original source. Runtime behavior and network acquisition were not performed.",
|
||||
...(report.glue.some((glue) => glue.parse_status !== "complete")
|
||||
? [
|
||||
"Selected glue parsing is incomplete; unmatched references do not establish absence.",
|
||||
]
|
||||
: []),
|
||||
],
|
||||
};
|
||||
if (view.kind === "summary")
|
||||
return ok({
|
||||
kind: "summary",
|
||||
view,
|
||||
...shared,
|
||||
summary: {
|
||||
bytes: report.artifact.bytes,
|
||||
validation: report.validation,
|
||||
available_counts: {
|
||||
sections: report.sections.length,
|
||||
imports: report.imports.length,
|
||||
exports: report.exports.length,
|
||||
candidates: report.candidates.length,
|
||||
glue: report.glue.length,
|
||||
},
|
||||
wat: {
|
||||
sha256: report.wat.sha256,
|
||||
representation: report.wat.representation,
|
||||
code_units: report.wat.text.length,
|
||||
},
|
||||
tool_profile: report.tool_profile,
|
||||
runtime_execution: report.runtime_execution,
|
||||
network_fetch: report.network_fetch,
|
||||
limitation_count: limitations.length,
|
||||
},
|
||||
coverage: completeWithinViewCoverage(
|
||||
report.sections.length + report.imports.length + report.exports.length,
|
||||
report.sections.length + report.imports.length + report.exports.length,
|
||||
),
|
||||
});
|
||||
if (view.facet === "tool_profile") {
|
||||
if (view.offset !== 0)
|
||||
return err(
|
||||
analysisViewInputError([
|
||||
{
|
||||
path: ["view", "offset"],
|
||||
reason: "invalid_value",
|
||||
message: "Singleton tool_profile requires offset 0.",
|
||||
},
|
||||
]),
|
||||
);
|
||||
return ok({
|
||||
kind: "wasm",
|
||||
view,
|
||||
item: report.tool_profile,
|
||||
coverage: completeWithinViewCoverage(1, 1),
|
||||
...shared,
|
||||
});
|
||||
}
|
||||
if (
|
||||
view.facet === "wat" ||
|
||||
view.facet === "headers" ||
|
||||
view.facet === "details"
|
||||
) {
|
||||
const text = view.facet === "wat" ? report.wat.text : report[view.facet];
|
||||
const page = analysisTextPage(text, view.offset, view.limit, view.facet);
|
||||
return page.ok
|
||||
? ok({ kind: "wasm", view, ...page.value, ...shared })
|
||||
: page;
|
||||
}
|
||||
const rows = report[view.facet];
|
||||
const item = rows.slice(view.offset, view.offset + view.limit);
|
||||
return ok({
|
||||
kind: "wasm",
|
||||
view,
|
||||
item,
|
||||
coverage: pageViewCoverage(view.offset, item.length, rows.length),
|
||||
...shared,
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user