Files
reverse-engineering-anything/src/domain/bridgeCandidateProjection.ts
T
morluto 62d6cfac92 refactor(domain): name known coverage once and derive local coverage enums from it (#11428)
* refactor(domain): name known coverage once and derive six native coverage enums from it

knownCoverageStatusSchema in src/domain/coverage.ts replaces the four per-domain coverageStatusSchema.exclude(["unknown"]) copies (Android, Flutter, GPU, model) and six plain ["complete", "partial"] enums in native reports (Apple IPS, PE certificates and imports, Windows shortcuts, native call observation, NativeAOT metadata recovery). Advertised schemas are unchanged. local-completeness-enum: 91 -> 85.

* refactor(domain): use the shared known coverage schema for ADB device analysis

* refactor(domain): derive pcap, firmware, Apktool, JEB and WASM interface coverage from known coverage (#11430)

Each producer decides coverage from what it examined (dissected-field gaps, unpublished firmware entries, string-table facts, a short JEB unit page, a WABT budget stop), so none reports unknown. Advertised schemas are unchanged. local-completeness-enum: 85 -> 79.

* refactor(domain): derive eleven checked coverage enums from known coverage (#11438)

* refactor(domain): derive eleven checked coverage enums from known coverage

* refactor(domain): derive coverage-plus-outcome enums from the coverage owner (#11440)
2026-11-16 04:15:59 +08:00

98 lines
3.3 KiB
TypeScript

import { z } from "zod";
import { knownCoverageStatusSchema } from "./coverage.js";
/** Coverage metadata for the bounded projection of candidate relationships. */
export const bridgeCandidateCoverageSchema = z.strictObject({
status: knownCoverageStatusSchema,
total_candidates: z.int().nonnegative(),
emitted_candidates: z.int().nonnegative(),
omitted_candidates: z.int().nonnegative(),
});
// The serialized candidate array is the retained representation; accounting
// its exact UTF-8 JSON size bounds both array objects and repeated path strings.
/** Maximum UTF-8 JSON size of a retained candidate array. */
export const MAX_BRIDGE_CANDIDATE_JSON_BYTES = 2 * 1024 * 1024;
type PathComponent = { readonly path: string };
const jsonText = (value: unknown): string => {
const serialized = JSON.stringify(value);
if (serialized === undefined)
throw new TypeError("Bridge candidate is not JSON serializable");
return serialized;
};
/** Retain a deterministic prefix of grouped Cartesian candidates within a byte budget. */
export const projectCartesianCandidates = <
TLeft extends PathComponent,
TRight extends PathComponent,
TCandidate,
>(input: {
readonly groups: readonly {
readonly left: readonly TLeft[];
readonly right: readonly TRight[];
}[];
readonly createCandidate: (leftPath: string, right: TRight) => TCandidate;
}) => {
const total = input.groups.reduce(
(sum, group) => sum + group.left.length * group.right.length,
0,
);
const preparedGroups = input.groups.map((group) => ({
left: group.left.map((value) => ({
value,
pathBytes: Buffer.byteLength(jsonText(value.path)),
})),
right: group.right.map((value) => {
const emptyLeftCandidate = jsonText(input.createCandidate("", value));
return {
value,
rowOverhead: Buffer.byteLength(emptyLeftCandidate) - 2,
};
}),
}));
let estimatedFullBytes = 2 + Math.max(0, total - 1); // JSON array brackets and commas.
for (const group of preparedGroups) {
estimatedFullBytes +=
group.left.length *
group.right.reduce((sum, { rowOverhead }) => sum + rowOverhead, 0) +
group.right.length *
group.left.reduce((sum, { pathBytes }) => sum + pathBytes, 0);
}
const shouldEmitAll = estimatedFullBytes <= MAX_BRIDGE_CANDIDATE_JSON_BYTES;
const candidates: TCandidate[] = [];
let estimatedRetainedBytes = 2;
candidateLoop: for (const group of preparedGroups) {
for (const left of group.left) {
for (const right of group.right) {
const candidateBytes = right.rowOverhead + left.pathBytes;
const additionalBytes =
candidateBytes + (candidates.length > 0 ? 1 : 0);
if (
!shouldEmitAll &&
estimatedRetainedBytes + additionalBytes >
MAX_BRIDGE_CANDIDATE_JSON_BYTES
)
break candidateLoop;
candidates.push(input.createCandidate(left.value.path, right.value));
estimatedRetainedBytes += additionalBytes;
}
}
}
return {
candidates,
coverage: {
status:
candidates.length === total
? ("complete" as const)
: ("partial" as const),
total_candidates: total,
emitted_candidates: candidates.length,
omitted_candidates: total - candidates.length,
},
};
};