refactor(runtime): retain complete reconciliation outputs

This commit is contained in:
morluto
2026-09-29 12:16:32 +08:00
parent 7b3c797237
commit 79ea571512
16 changed files with 106 additions and 190 deletions
@@ -208,9 +208,6 @@ export const applicationVersionComparisonResultSchema = z
});
});
export type CompareApplicationVersionsInput = z.infer<
typeof compareApplicationVersionsInputSchema
>;
export type ApplicationVersionComparisonItem = z.infer<
typeof comparisonItemSchema
>;
+4 -10
View File
@@ -25,21 +25,17 @@ export const classifyStaticLoadStates = (
runtimeEntities: readonly RuntimeReconciliationEntity[],
matches: readonly JavaScriptRuntimeReconciliationItem[],
options: {
readonly maximumStates: number;
readonly reconciliationComplete: boolean;
},
): StaticLoadStateProjection => {
const states: JavaScriptStaticLoadState[] = [];
let totalStates = 0;
for (const layer of layers) {
const projection = classifyLayer(layer, runtimeEntities, matches, {
reconciliationComplete: options.reconciliationComplete,
maximumStates: Math.max(0, options.maximumStates - states.length),
});
totalStates += projection.total;
states.push(...projection.states);
}
return { states, omittedStates: totalStates - states.length };
return { states, omittedStates: 0 };
};
const classifyLayer = (
@@ -48,11 +44,9 @@ const classifyLayer = (
matches: readonly JavaScriptRuntimeReconciliationItem[],
options: {
readonly reconciliationComplete: boolean;
readonly maximumStates: number;
},
): { readonly states: JavaScriptStaticLoadState[]; readonly total: number } => {
): { readonly states: JavaScriptStaticLoadState[] } => {
const nodes = layer.graph.nodes.filter(isLoadStateNode);
if (options.maximumStates === 0) return { states: [], total: nodes.length };
const direct = new Map<string, string[]>();
for (const match of matches)
if (
@@ -73,7 +67,7 @@ const classifyLayer = (
scopedEntities.every(({ capture }) =>
captureScriptsComplete(capture.evidence.evidence_id, runtimeEntities),
);
const states = nodes.slice(0, options.maximumStates).map((node) => {
const states = nodes.map((node) => {
const runtimeNodeIds = uniqueSorted(direct.get(node.node_id) ?? []);
if (runtimeNodeIds.length > 0)
return state(layer, node, {
@@ -102,7 +96,7 @@ const classifyLayer = (
: "static-or-runtime-coverage-incomplete",
});
});
return { states, total: nodes.length };
return { states };
};
const residentNodes = (
@@ -18,23 +18,18 @@ export const reconcileJavaScriptRuntime = (
const parsed = reconcileJavaScriptRuntimeInputSchema.parse(input);
const layers = parseStaticLayers(parsed.static_layers);
const captures = parseRuntimeCaptures(parsed.runtime_observations);
const runtime = projectRuntimeCaptures(
captures,
parsed.limits.max_runtime_entities,
);
const runtime = projectRuntimeCaptures(captures);
const candidates = collectStaticRuntimeCandidates(layers);
const matching = reconcileRuntimeEntities({
entities: runtime.entities,
candidates,
layers,
maximumItems: parsed.limits.max_reconciliation_items,
});
const loadStates = classifyStaticLoadStates(
layers,
runtime.entities,
matching.items,
{
maximumStates: parsed.limits.max_static_load_states,
reconciliationComplete:
runtime.omittedEntities === 0 && matching.omittedItems === 0,
},
@@ -67,7 +67,7 @@ export const buildReconciledApplicationGraph = (
: []),
...(facts.outputTruncated
? [
"The reconciled graph omitted entities or classifications at caller limits.",
"The reconciled graph omitted entities or classifications during projection.",
]
: []),
]),
@@ -27,24 +27,21 @@ export const reconcileRuntimeEntities = (input: {
readonly entities: readonly RuntimeReconciliationEntity[];
readonly candidates: readonly StaticRuntimeCandidate[];
readonly layers: readonly ParsedStaticLayer[];
readonly maximumItems: number;
}): RuntimeMatchingProjection => {
const index = indexCandidates(input.candidates);
const evaluated = input.entities.map((entity) =>
reconcileEntity(entity, index, input.layers),
);
const retained = evaluated
.sort((left, right) =>
compareCodePoints(
left.item.reconciliation_id,
right.item.reconciliation_id,
),
)
.slice(0, input.maximumItems);
const retained = evaluated.sort((left, right) =>
compareCodePoints(
left.item.reconciliation_id,
right.item.reconciliation_id,
),
);
return {
items: retained.map(({ item }) => item),
edges: retained.flatMap(({ edge }) => (edge === null ? [] : [edge])),
omittedItems: evaluated.length - retained.length,
omittedItems: 0,
};
};
@@ -419,11 +416,10 @@ const uniqueCandidateReferences = (
if (seen.has(key)) continue;
seen.add(key);
count += 1;
if (items.length < 1_000)
items.push({
static_layer_id: layer.layerId,
static_node_id: node.node_id,
});
items.push({
static_layer_id: layer.layerId,
static_node_id: node.node_id,
});
}
return { count, items };
};
@@ -198,7 +198,7 @@ const reconciliationLimitations = (completion: CompletionFlags): string[] =>
: []),
...(completion.outputTruncated
? [
"Caller limits omitted runtime entities or reconciliation classifications.",
"Runtime reconciliation omitted entities or classifications during projection.",
]
: []),
]);
@@ -31,16 +31,9 @@ export interface RuntimeProjection {
/** Project authorized passive capture metadata into capture-scoped JAG nodes. */
export const projectRuntimeCaptures = (
captures: readonly ParsedRuntimeCapture[],
maximumEntities: number,
): RuntimeProjection => {
if (maximumEntities < captures.length)
throw new TypeError(
"Runtime entity limit must admit one target for every capture",
);
const all = captures.flatMap(projectCapture);
const targets = all.filter(({ kind }) => kind === "target");
const nonTargets = all.filter(({ kind }) => kind !== "target");
const retained = [...targets, ...nonTargets].slice(0, maximumEntities);
const retained = all;
const retainedIds = new Set(retained.map(({ node }) => node.node_id));
const nodes = retained.map(({ node }) => node);
const edges = captures
@@ -59,7 +52,7 @@ export const projectRuntimeCaptures = (
nodes,
edges,
targetNodeByEvidenceId,
omittedEntities: all.length - retained.length,
omittedEntities: 0,
};
};
@@ -80,7 +80,6 @@ const staticLayerSchema = z.strictObject({
analysis: evidenceSchema,
runtime_mappings: z
.array(runtimeLocationMappingSchema)
.max(32)
.overwrite(normalizeRuntimeMappings)
.default([]),
});
@@ -103,22 +102,11 @@ function runtimeMappingKey(mapping: RuntimeLocationMapping): string {
: `url-prefix\0${mapping.prefix}\0${mapping.artifact_prefix}`;
}
const reconciliationLimitsSchema = z.strictObject({
max_runtime_entities: z.number().int().min(1).max(50_000).default(10_000),
max_reconciliation_items: z.number().int().min(1).max(50_000).default(20_000),
max_static_load_states: z.number().int().min(1).max(50_000).default(20_000),
});
/** Evidence-backed static layers and passive captures to reconcile. */
export const reconcileJavaScriptRuntimeInputSchema = z
.strictObject({
static_layers: z.array(staticLayerSchema).min(1).max(8),
runtime_observations: z.array(evidenceSchema).min(1).max(32),
limits: reconciliationLimitsSchema.default({
max_runtime_entities: 10_000,
max_reconciliation_items: 20_000,
max_static_load_states: 20_000,
}),
static_layers: z.array(staticLayerSchema).min(1),
runtime_observations: z.array(evidenceSchema).min(1),
})
.superRefine((input, context) => {
if (
@@ -148,12 +136,6 @@ export const reconcileJavaScriptRuntimeInputSchema = z
path: ["runtime_observations"],
message: "Runtime observation Evidence must be unique",
});
if (input.limits.max_runtime_entities < input.runtime_observations.length)
context.addIssue({
code: "custom",
path: ["limits", "max_runtime_entities"],
message: "Runtime entity limit must retain one target per observation",
});
});
const layerSummarySchema = z.strictObject({
@@ -164,7 +146,7 @@ const layerSummarySchema = z.strictObject({
root_artifact_sha256: digestSchema,
input_path: z.string().min(1).max(16_384),
format: z.enum(["asar", "directory"]),
runtime_mappings: z.array(runtimeLocationMappingSchema).max(32),
runtime_mappings: z.array(runtimeLocationMappingSchema),
});
const captureSummarySchema = z.strictObject({
@@ -209,15 +191,13 @@ const reconciliationItemShape = {
]),
confidence: z.enum(["exact", "high", "medium", "low", "unknown"]),
reason: reconciliationReasonSchema,
candidate_static_count: z.number().int().min(0).max(800_000),
candidate_static_nodes: z
.array(
z.strictObject({
static_layer_id: z.string().regex(/^jrl_[a-f0-9]{64}$/u),
static_node_id: nodeIdSchema,
}),
)
.max(1_000),
candidate_static_count: z.number().int().min(0),
candidate_static_nodes: z.array(
z.strictObject({
static_layer_id: z.string().regex(/^jrl_[a-f0-9]{64}$/u),
static_node_id: nodeIdSchema,
}),
),
};
const reconciliationItemSchema = z.union([
z.strictObject({
@@ -244,7 +224,7 @@ const staticLoadStateSchema = z.strictObject({
"not-observed-in-capture",
"unknown",
]),
runtime_node_ids: z.array(nodeIdSchema).max(10_000),
runtime_node_ids: z.array(nodeIdSchema),
reason: z.enum([
"runtime-script-correspondence",
"containing-asset-was-loaded",
@@ -257,8 +237,8 @@ const staticLoadStateSchema = z.strictObject({
/** Deterministic static/passive-runtime reconciliation with a combined JAG. */
export const javascriptRuntimeReconciliationResultSchema = z.strictObject({
reconciliation_id: z.string().regex(/^jrr_[a-f0-9]{64}$/u),
static_layers: z.array(layerSummarySchema).min(1).max(8),
runtime_captures: z.array(captureSummarySchema).min(1).max(32),
static_layers: z.array(layerSummarySchema).min(1),
runtime_captures: z.array(captureSummarySchema).min(1),
graph: javascriptApplicationGraphSchema,
summary: z.strictObject({
runtime_targets: z.number().int().min(0),
@@ -274,8 +254,8 @@ export const javascriptRuntimeReconciliationResultSchema = z.strictObject({
static_not_observed: z.number().int().min(0),
static_unknown: z.number().int().min(0),
}),
reconciliations: z.array(reconciliationItemSchema).max(50_000),
static_load_states: z.array(staticLoadStateSchema).max(50_000),
reconciliations: z.array(reconciliationItemSchema),
static_load_states: z.array(staticLoadStateSchema),
source_map_authority: z.strictObject({
used_for_primary_matching: z.literal(false),
static_layer_count: z.number().int().min(0),
@@ -290,8 +270,8 @@ export const javascriptRuntimeReconciliationResultSchema = z.strictObject({
omitted_static_load_states: z.number().int().min(0),
omitted_graph_items: z.number().int().min(0),
}),
evidence_links: z.array(evidenceIdSchema).min(2).max(40),
limitations: z.array(boundedTextSchema).max(1_000),
evidence_links: z.array(evidenceIdSchema).min(2),
limitations: z.array(boundedTextSchema),
});
export type ReconcileJavaScriptRuntimeInput = z.infer<
@@ -118,9 +118,6 @@ export const sourceToBundleComparisonResultSchema = z.strictObject({
limitations: z.array(boundedTextSchema).max(1_000),
});
export type CompareSourceToBundleInput = z.infer<
typeof compareSourceToBundleInputSchema
>;
export type SourceToBundleComparisonResult = z.infer<
typeof sourceToBundleComparisonResultSchema
>;