diff --git a/src/domain/analysisView/analysisView.test.ts b/src/domain/analysisView/analysisView.test.ts index 5ec964ce39..f35d748465 100644 --- a/src/domain/analysisView/analysisView.test.ts +++ b/src/domain/analysisView/analysisView.test.ts @@ -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), + }, + }, + }); +}); diff --git a/src/domain/analysisView/byteEntropyView.ts b/src/domain/analysisView/byteEntropyView.ts index c204ea46e6..78a2412e34 100644 --- a/src/domain/analysisView/byteEntropyView.ts +++ b/src/domain/analysisView/byteEntropyView.ts @@ -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: [