perf(entropy): bound working storage for run rankings (#12210)

This commit is contained in:
ShuaixinHuang authored and GitHub committed 2026-11-19 06:35:23 -05:00
1 parent ce02c899e8
commit 35bbba3023
2 files changed
+65 -3

No files matched your search

@@ -1805,3 +1805,47 @@ it("selects a unique content-deduplicated module by any retained path", () => {
});
}
});
it("ranks only the longest byte entropy runs with stable offset ties", () => {
const runs = Array.from({ length: 1024 }, (_, index) => ({
offset: Math.floor((1023 - index) / 2) * 256,
length: ((index % 19) + 1) * 256,
classification: index % 2 === 0 ? "high" : "low",
}));
runs.push({ offset: 0, length: 8192, classification: "high" });
runs.push({ offset: 0, length: 8192, classification: "low" });
const result = projectAnalysisView(
{
predicateType: "rea.byte-entropy",
evidenceId: `ev_${"1".repeat(64)}`,
operation: "inspect_byte_entropy",
limitations: [],
normalizedResult: {
artifact: {
path: "/tmp/fixture",
bytes: 262144,
sha256: "2".repeat(64),
},
block_size: 256,
window: { offset: 0, length: 262144 },
file_entropy: 4,
thresholds: { high: 7.5, low: 0.5 },
blocks: [],
runs,
limitations: [],
},
},
{ kind: "summary" },
);
expect(result).toMatchObject({
ok: true,
value: {
summary: {
run_count: runs.length,
top_runs: runs
.toSorted((a, b) => b.length - a.length || a.offset - b.offset)
.slice(0, 10),
},
},
});
});
+21 -3
View File
@@ -18,6 +18,26 @@ export const byteEntropySummarySchema = byteEntropySchema
top_runs: byteEntropySchema.shape.runs,
});
const TOP_RUN_LIMIT = 10;
type EntropyRun = ByteEntropy["runs"][number];
const compareRuns = (a: EntropyRun, b: EntropyRun): number =>
b.length - a.length || a.offset - b.offset;
const topRuns = (runs: readonly EntropyRun[]): EntropyRun[] => {
const retained: EntropyRun[] = [];
for (const run of runs) {
const last = retained[TOP_RUN_LIMIT - 1];
if (last !== undefined && compareRuns(run, last) >= 0) continue;
let index = retained.length;
while (index > 0 && compareRuns(run, retained[index - 1]!) < 0) index -= 1;
retained.splice(index, 0, run);
if (retained.length > TOP_RUN_LIMIT) retained.pop();
}
return retained;
};
/** Rank ten longest runs for triage; the full series remains in parent Evidence. */
export const projectByteEntropyView = (
parent: AnalysisViewParent,
@@ -51,9 +71,7 @@ export const projectByteEntropyView = (
thresholds: report.thresholds,
block_count: report.blocks.length,
run_count: report.runs.length,
top_runs: [...report.runs]
.sort((a, b) => b.length - a.length || a.offset - b.offset)
.slice(0, 10),
top_runs: topRuns(report.runs),
},
coverage: completeWithinViewCoverage(report.blocks.length),
limitations: [