Repository navigation
Expand file tree
/
Copy pathjavascriptExportShapeComparison.ts
More file actions
246 lines (233 loc) · 8.68 KB
/
Copy pathjavascriptExportShapeComparison.ts
File metadata and controls
246 lines (233 loc) · 8.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { uniqueSorted } from "../canonicalOrdering.js";
import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js";
import {
canonicalExportShapeValue,
digestExportShapeValue,
} from "./javascriptExportShapeComparisonIdentity.js";
import {
javaScriptExportShapeComparisonResultSchema,
type JavaScriptExportShapeComparisonChange,
type JavaScriptExportShapeComparisonResult,
} from "./javascriptExportShapeComparisonSchemas.js";
import {
retainJavaScriptExportShapes,
selectJavaScriptExport,
type JavaScriptExportShapeSideInput,
type RetainedJavaScriptExportShapes,
type SelectedJavaScriptExport,
} from "./javascriptExportShapeSelection.js";
import {
buildJavaScriptExportShapeChanges,
buildJavaScriptExportShapeInventories,
hasPartialJavaScriptExportPropertyCoverage,
pairJavaScriptExportShapeVariants,
type JavaScriptExportShapePairing,
} from "./javascriptExportShapeVariants.js";
/** Pure comparison input after application-layer Evidence authentication. */
export interface JavaScriptExportShapeComparisonProjectionInput {
readonly left: JavaScriptExportShapeSideInput;
readonly right: JavaScriptExportShapeSideInput;
}
/** Compare exact selected export return shapes without executing JavaScript. */
export const compareJavaScriptExportShapes = (
input: JavaScriptExportShapeComparisonProjectionInput,
): JavaScriptExportShapeComparisonResult => {
const left = selectJavaScriptExport(input.left);
const right = selectJavaScriptExport(input.right);
const leftRetained = retainJavaScriptExportShapes(left);
const rightRetained = retainJavaScriptExportShapes(right);
const pairing = pairJavaScriptExportShapeVariants(
leftRetained.shapes,
rightRetained.shapes,
);
const evidenceLinks = evidencePair(
input.left.evidenceId,
input.right.evidenceId,
);
const allChanges = sortChanges(
buildJavaScriptExportShapeChanges({
leftSelection: left,
rightSelection: right,
pairing,
leftShapes: leftRetained.shapes,
rightShapes: rightRetained.shapes,
evidenceLinks,
}),
);
const changes = allChanges;
const omittedChanges = 0;
const propertyInventories = buildJavaScriptExportShapeInventories({
pairing,
leftShapes: leftRetained.shapes,
rightShapes: rightRetained.shapes,
});
const coverage = comparisonCoverage({
input,
left,
right,
leftRetained,
rightRetained,
pairing,
allChanges,
omittedChanges,
});
const limitations = comparisonLimitations(left, right, coverage.status);
const semantic = {
left: left.selection,
right: right.selection,
property_inventories: propertyInventories,
summary: summarize(allChanges),
changes,
coverage,
evidence_links: evidenceLinks,
limitations,
};
return javaScriptExportShapeComparisonResultSchema.parse({
...semantic,
comparison_id: `jesc_${digestExportShapeValue(semantic)}`,
});
};
const sortChanges = (
changes: readonly JavaScriptExportShapeComparisonChange[],
): JavaScriptExportShapeComparisonChange[] =>
changes
.map((change, index) => ({
change,
index,
sortKey: canonicalExportShapeValue({
discriminant: change.discriminant,
path: change.path,
status: change.status,
left_source_range: change.left_source_range,
right_source_range: change.right_source_range,
change_id: change.change_id,
}),
}))
.sort(
(left, right) =>
compareUnicodeCodePoints(left.sortKey, right.sortKey) ||
left.index - right.index,
)
.map(({ change }) => change);
interface CoverageInput {
readonly input: JavaScriptExportShapeComparisonProjectionInput;
readonly left: SelectedJavaScriptExport;
readonly right: SelectedJavaScriptExport;
readonly leftRetained: RetainedJavaScriptExportShapes;
readonly rightRetained: RetainedJavaScriptExportShapes;
readonly pairing: JavaScriptExportShapePairing;
readonly allChanges: readonly JavaScriptExportShapeComparisonChange[];
readonly omittedChanges: number;
}
const comparisonCoverage = (context: CoverageInput) => {
const leftSource = context.left.projection?.return_shape_coverage;
const rightSource = context.right.projection?.return_shape_coverage;
const omittedCandidates =
context.left.selection.omitted_candidates +
context.right.selection.omitted_candidates;
const truncated = comparisonTruncated(context, omittedCandidates);
const partial = comparisonPartial(context);
return {
status: truncated
? ("truncated" as const)
: partial
? ("partial" as const)
: ("complete-within-inputs" as const),
left_graph_status: context.input.left.graph.coverage.status,
right_graph_status: context.input.right.graph.coverage.status,
paired_variants: context.pairing.pairs.length,
unpaired_left_variants: context.pairing.unpairedLeft.length,
unpaired_right_variants: context.pairing.unpairedRight.length,
omitted_left_variants: context.leftRetained.omitted,
omitted_right_variants: context.rightRetained.omitted,
left_source_omitted_variants: leftSource?.omitted_return_sites ?? null,
right_source_omitted_variants: rightSource?.omitted_return_sites ?? null,
left_omitted_fields: leftSource?.omitted_fields ?? 0,
right_omitted_fields: rightSource?.omitted_fields ?? 0,
left_omitted_property_coverage: leftSource?.omitted_property_coverage ?? 0,
right_omitted_property_coverage:
rightSource?.omitted_property_coverage ?? 0,
omitted_candidates: omittedCandidates,
omitted_changes: context.omittedChanges,
};
};
const comparisonTruncated = (
context: CoverageInput,
omittedCandidates: number,
): boolean =>
context.leftRetained.omitted > 0 ||
context.rightRetained.omitted > 0 ||
omittedCandidates > 0 ||
context.omittedChanges > 0 ||
sourceProjectionTruncated(context.left) ||
sourceProjectionTruncated(context.right);
const sourceProjectionTruncated = (
selection: SelectedJavaScriptExport,
): boolean => {
const coverage = selection.projection?.return_shape_coverage;
if (coverage === undefined) return false;
return (
coverage.status === "truncated" ||
coverage.omitted_fields > 0 ||
coverage.omitted_property_coverage > 0 ||
coverage.projection_complete === false
);
};
const comparisonPartial = (context: CoverageInput): boolean =>
selectionPartial(context.left) ||
selectionPartial(context.right) ||
context.input.left.graph.coverage.status !== "complete" ||
context.input.right.graph.coverage.status !== "complete" ||
hasPartialJavaScriptExportPropertyCoverage(context.leftRetained.shapes) ||
hasPartialJavaScriptExportPropertyCoverage(context.rightRetained.shapes) ||
context.pairing.unpairedLeft.length > 0 ||
context.pairing.unpairedRight.length > 0 ||
context.allChanges.some(({ status }) => status === "unknown");
const selectionPartial = (selection: SelectedJavaScriptExport): boolean => {
const coverage = selection.projection?.return_shape_coverage;
return (
selection.selection.status !== "selected" ||
!selection.observationComplete ||
coverage?.status !== "complete" ||
coverage.projection_complete !== true
);
};
const comparisonLimitations = (
left: SelectedJavaScriptExport,
right: SelectedJavaScriptExport,
status: "complete-within-inputs" | "partial" | "truncated",
): string[] =>
uniqueSorted([
"Return shapes are inferred from inert syntax and do not prove runtime behavior.",
"Return variants are paired only by unique exact literal discriminants, never by source order.",
"Dynamic values, incomplete object coverage, and ambiguous variants remain unknown.",
"This static comparison cannot establish runtime semantics; run behavioral probes directly against the relevant application versions when that evidence is required.",
...left.limitations,
...right.limitations,
...(status === "truncated"
? [
"Comparison output or source return-shape recovery reached an explicit limit.",
]
: []),
...(status === "partial"
? [
"At least one selector, graph, return shape, or property boundary is incomplete.",
]
: []),
]);
const summarize = (
changes: readonly JavaScriptExportShapeComparisonChange[],
): JavaScriptExportShapeComparisonResult["summary"] => ({
added: countStatus(changes, "added"),
removed: countStatus(changes, "removed"),
changed: countStatus(changes, "changed"),
unknown: countStatus(changes, "unknown"),
});
const countStatus = (
changes: readonly JavaScriptExportShapeComparisonChange[],
status: JavaScriptExportShapeComparisonChange["status"],
): number => changes.filter((change) => change.status === status).length;
const evidencePair = (left: string, right: string): [string, string] => [
left,
right,
];