mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 23:15:58 +00:00
* feat(web): deduplicate bundle findings across scripts by value and mechanism Per-script findings were deduplicated only within one script, so the same endpoint or route observed in N scripts appeared N times in the merged bundle, inflating the finding list and overstating the inventory. Merge now deduplicates routes and endpoints across scripts by (value, mechanism). The first occurrence keeps its location and order; later occurrences fold into optional occurrences/script_keys on the retained entry, so no script is silently dropped and single-script findings keep their original shape. The key uses the existing compositeKey helper (JSON) instead of a NUL join. bundleLimitations gains a line describing the cross-script merge. * fix(web): address Greptile review on cross-script bundle dedup - Accept merged endpoint occurrences/script_keys in the web-bundle view row schema so unfiltered pages no longer throw. - Match endpoint script_keys filters against retained script_keys, falling back to location.script_key for older records. - Generate distinct finding keys in the wide-result capacity fixtures so routes/endpoints still exercise a 130,000-entry merged result. - Append later script keys in place instead of rebuilding the list. - Compare the dedup limitation against its owning constant instead of freezing the explanatory sentence in the test. - Update the route-inventory guide for merged endpoints. * test(web): expect the cross-script endpoint merge in the semantic budget case Main added a regression where two budget-limited scripts both report /health. With findings merged across scripts, it is one entry whose occurrences and script_keys name both scripts, which keeps the case's per-script attribution. Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> --------- Co-authored-by: gensokyo-dev <gensokyo-dev@users.noreply.github.com> Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
208 lines
5.9 KiB
TypeScript
208 lines
5.9 KiB
TypeScript
import { expect, it } from "vitest";
|
|
import type { WebBundleAnalysis } from "../../../src/domain/webBundleAnalysis.js";
|
|
import { buildCapturedWebBundleAnalysis } from "../../../src/domain/webBundleAnalyzerResult.js";
|
|
import { isIncludedScript } from "../../../src/domain/webBundleAnalyzerState.js";
|
|
import type { WebBundleScriptAnalysis } from "../../../src/domain/webBundleScriptAnalysis.js";
|
|
import { createWebPageInspection } from "../../support/browser/webPageInspection.js";
|
|
|
|
type WideCollection =
|
|
| "edges"
|
|
| "routes"
|
|
| "endpoints"
|
|
| "webmcp_declarations"
|
|
| "inferences";
|
|
|
|
const EXPECTED_BOUNDS: Record<WideCollection, readonly [string, string]> = {
|
|
edges: ["./first.js", "./last.js"],
|
|
routes: ["/first", "/last"],
|
|
endpoints: ["/api/first", "/api/last"],
|
|
webmcp_declarations: ["firstTool", "lastTool"],
|
|
inferences: ["/api/first", "/api/last"],
|
|
};
|
|
|
|
const fillWide = <T>(
|
|
count: number,
|
|
first: T,
|
|
makeMiddle: (index: number) => T,
|
|
last: T,
|
|
): T[] => {
|
|
const items = new Array<T>(count);
|
|
items[0] = first;
|
|
for (let index = 1; index < count - 1; index += 1)
|
|
items[index] = makeMiddle(index);
|
|
items[count - 1] = last;
|
|
return items;
|
|
};
|
|
|
|
const buildWideScriptResult = (
|
|
collection: WideCollection,
|
|
scriptKey: string,
|
|
sha256: string,
|
|
count: number,
|
|
): WebBundleScriptAnalysis => {
|
|
const location = { script_key: scriptKey, line: 1, column: 0 };
|
|
const makeEdge = (specifier: string) => ({
|
|
from_script_key: scriptKey,
|
|
kind: "static_import" as const,
|
|
specifier,
|
|
resolved_url: null,
|
|
location,
|
|
});
|
|
const makeFinding = (value: string, mechanism: string) => ({
|
|
value,
|
|
mechanism,
|
|
location,
|
|
});
|
|
const makeWebMcp = (name: string) => ({
|
|
name,
|
|
description: null,
|
|
schema_property_names: [],
|
|
schema_property_names_coverage: "not_present" as const,
|
|
trust: "page-declared-untrusted" as const,
|
|
location,
|
|
});
|
|
const makeInference = (value: string) => ({
|
|
kind: "endpoint_candidate" as const,
|
|
value,
|
|
confidence: "low" as const,
|
|
basis: [
|
|
{
|
|
script_key: scriptKey,
|
|
artifact_sha256: sha256,
|
|
line: 1,
|
|
column: 0,
|
|
detector: "test",
|
|
},
|
|
],
|
|
});
|
|
return {
|
|
status: "analyzed",
|
|
script_key: scriptKey,
|
|
parse_status: "parsed",
|
|
visited_ast_nodes: count,
|
|
widened_return_references: false,
|
|
observations: {
|
|
edges:
|
|
collection === "edges"
|
|
? fillWide(
|
|
count,
|
|
makeEdge("./first.js"),
|
|
() => makeEdge("./mid.js"),
|
|
makeEdge("./last.js"),
|
|
)
|
|
: [],
|
|
routes:
|
|
collection === "routes"
|
|
? fillWide(
|
|
count,
|
|
makeFinding("/first", "router"),
|
|
(index) => makeFinding(`/mid-${index}`, "router"),
|
|
makeFinding("/last", "router"),
|
|
)
|
|
: [],
|
|
endpoints:
|
|
collection === "endpoints"
|
|
? fillWide(
|
|
count,
|
|
makeFinding("/api/first", "fetch"),
|
|
(index) => makeFinding(`/api/mid-${index}`, "fetch"),
|
|
makeFinding("/api/last", "fetch"),
|
|
)
|
|
: [],
|
|
webmcp_declarations:
|
|
collection === "webmcp_declarations"
|
|
? fillWide(
|
|
count,
|
|
makeWebMcp("firstTool"),
|
|
() => makeWebMcp("midTool"),
|
|
makeWebMcp("lastTool"),
|
|
)
|
|
: [],
|
|
unresolved_endpoints: [],
|
|
},
|
|
inferences:
|
|
collection === "inferences"
|
|
? fillWide(
|
|
count,
|
|
makeInference("/api/first"),
|
|
() => makeInference("/api/mid"),
|
|
makeInference("/api/last"),
|
|
)
|
|
: [],
|
|
};
|
|
};
|
|
|
|
const summarizeMerged = (
|
|
result: WebBundleAnalysis,
|
|
collection: WideCollection,
|
|
): readonly [number, string | null | undefined, string | null | undefined] => {
|
|
switch (collection) {
|
|
case "edges": {
|
|
const items = result.observations.chunks.edges;
|
|
return [items.length, items[0]?.specifier, items.at(-1)?.specifier];
|
|
}
|
|
case "routes": {
|
|
const items = result.observations.routes;
|
|
return [items.length, items[0]?.value, items.at(-1)?.value];
|
|
}
|
|
case "endpoints": {
|
|
const items = result.observations.endpoints;
|
|
return [items.length, items[0]?.value, items.at(-1)?.value];
|
|
}
|
|
case "webmcp_declarations": {
|
|
const items = result.observations.webmcp_declarations;
|
|
return [items.length, items[0]?.name, items.at(-1)?.name];
|
|
}
|
|
case "inferences": {
|
|
const items = result.inferences;
|
|
return [items.length, items[0]?.value, items.at(-1)?.value];
|
|
}
|
|
}
|
|
};
|
|
|
|
// Forked tests share the CLI/MCP main-thread stack, unlike domain threads.
|
|
it.each([
|
|
"edges",
|
|
"routes",
|
|
"endpoints",
|
|
"webmcp_declarations",
|
|
"inferences",
|
|
] as const)(
|
|
"merges wide %s script results without overflowing call arguments",
|
|
(collection) => {
|
|
const count = 130_000;
|
|
const scriptKey = `scr_${"a".repeat(64)}`;
|
|
const captured = createWebPageInspection([
|
|
{
|
|
source: "export {};\n",
|
|
scriptKey,
|
|
url: "https://app.example.test/app.js",
|
|
origin: "https://app.example.test",
|
|
cdpHash: "hash-0",
|
|
},
|
|
]);
|
|
const script = captured.scripts.items.find(isIncludedScript);
|
|
if (script === undefined)
|
|
throw new Error("Missing included script fixture");
|
|
|
|
const result = buildCapturedWebBundleAnalysis(
|
|
captured,
|
|
{ status: "not_requested", requested: 0, processed: 0, items: [] },
|
|
[
|
|
buildWideScriptResult(
|
|
collection,
|
|
scriptKey,
|
|
script.source.artifact.sha256,
|
|
count,
|
|
),
|
|
],
|
|
);
|
|
|
|
const [length, first, last] = summarizeMerged(result, collection);
|
|
const [expectedFirst, expectedLast] = EXPECTED_BOUNDS[collection];
|
|
expect(length).toBe(count);
|
|
expect(first).toBe(expectedFirst);
|
|
expect(last).toBe(expectedLast);
|
|
},
|
|
);
|