Skip to content

Write HTML. Render video. Built for agents.

fix(inspector): a component report under budget keeps the animation code before CSS candidates (#6003)

Xuanru Li41af1add0da9
diff --git a/packages/inspector/src/mapInspection.ts b/packages/inspector/src/mapInspection.ts
index 2157e1ea25..07101e69b3 100644
--- a/packages/inspector/src/mapInspection.ts
+++ b/packages/inspector/src/mapInspection.ts
@@ -6,6 +6,22 @@ const coverage = [
"CSS selector matches are candidates, not a full cascade or dependency graph. HTML is the authored opening tag when uniquely located.",
"Direct constructors, other animation systems, iframe contents, removed animations and unexecuted paths may be absent; missing origins are reported.",
];
+const KEPT_FIRST_UNDER_BUDGET = [
+ "animation creation",
+ "animation caller",
+ "timeline attachment",
+ "html opening tag",
+ "animation source dependency",
+ "matched CSS candidate",
+];
+const rank = (kind: string) => {
+ const at = KEPT_FIRST_UNDER_BUDGET.indexOf(kind);
+ return at === -1 ? KEPT_FIRST_UNDER_BUDGET.length : at;
+};
+const priority = (snippet: Snippet) => Math.min(...snippet.kind.map(rank));
+const byPosition = (a: Snippet, b: Snippet) =>
+ a.file.localeCompare(b.file) || a.startLine - b.startLine;
+
export function mapInspection(
raw: RuntimeResponse,
request: InspectRequest,
@@ -71,7 +87,7 @@ export function mapInspection(
});
const merged = mergeSnippets(collected, sources);
for (const snippet of merged) catalog.set(snippet.id, snippet);
- let selected = merged;
+ let selected = [...merged].sort((a, b) => priority(a) - priority(b));
if (request.snippets)
selected = request.snippets.map((id) => {
const snippet = catalog.get(id);
@@ -122,6 +138,7 @@ export function mapInspection(
break;
}
}
+ if (!request.snippets) response.snippets.sort(byPosition);
trimCandidates(maxBytes, 0);
if (Buffer.byteLength(JSON.stringify(response)) > maxBytes)
throw new Error("metadata_exceeds_budget: use fewer targets or increase maxBytes");
diff --git a/packages/inspector/tests/mapInspection.test.ts b/packages/inspector/tests/mapInspection.test.ts
index 2667fcc2d8..a9e4e12069 100644
--- a/packages/inspector/tests/mapInspection.test.ts
+++ b/packages/inspector/tests/mapInspection.test.ts
@@ -119,3 +119,90 @@ describe("components report budget", () => {
}
});
});
+
+describe("components report priority", () => {
+ const css = Array.from({ length: 6 }, (_, n) => `.w-dot.n${n}{${"color:red;".repeat(30)}}`).join(
+ "\n",
+ );
+ const call = `tl.to(".w-dot", { x: 100, duration: 1 }, 2);`;
+
+ function withAnimation(
+ request: Parameters<typeof mapInspection>[1],
+ catalog = new Map<string, Snippet>(),
+ ) {
+ const dir = mkdtempSync(join(tmpdir(), "inspect-priority-"));
+ const html = `<html><head><style>\n${css}\n</style></head><body>\n<script>\nconst tl = gsap.timeline();\n${call}\n</script>\n</body></html>`;
+ writeFileSync(join(dir, "index.html"), html);
+ const sources = new Sources(dir);
+ const start = html.indexOf(call);
+ const creation = sources.slice(
+ "index.html",
+ start,
+ start + call.length,
+ "animation creation",
+ 0,
+ )!;
+ const raw: RuntimeResponse = {
+ results: [
+ {
+ target: { selector: ".w-dot" },
+ status: "found",
+ candidates: [element(0)],
+ animations: [{ stack: "s", attachmentStacks: [], targets: [element(0)] } as never],
+ elements: [],
+ css: sources.cssRules.map((rule) => rule.id),
+ truncated: false,
+ },
+ ],
+ recorded: 1,
+ recordLimitReached: false,
+ hooked: true,
+ };
+ const report = mapInspection(raw, request, {
+ sources,
+ map: () => [creation],
+ catalog,
+ generation: "g",
+ sourceVersion: "v",
+ reused: false,
+ start: performance.now(),
+ });
+ rmSync(dir, { recursive: true, force: true });
+ return { report, creation };
+ }
+
+ it("keeps the code that makes an animation before CSS candidates that sit earlier in the file", () => {
+ const { report, creation } = withAnimation({
+ targets: [{ selector: ".w-dot" }],
+ maxBytes: 2600,
+ });
+ expect(report.snippets.map((s) => s.id)).toContain(creation.id);
+ expect(report.omittedCount).toBeGreaterThan(0);
+ for (const s of report.snippets.filter((s) => s.id !== creation.id))
+ expect(s.kind).toEqual(["matched CSS candidate"]);
+ });
+
+ it("returns the snippets it keeps in file order", () => {
+ const { report } = withAnimation({ targets: [{ selector: ".w-dot" }], maxBytes: 256000 });
+ const lines = report.snippets.map((s) => s.startLine);
+ expect(lines).toEqual([...lines].sort((a, b) => a - b));
+ expect(report.snippets.at(-1)?.kind).toEqual(["animation creation"]);
+ expect(report.omittedCount).toBe(0);
+ });
+
+ it("returns explicit snippet IDs in request order rather than file order", () => {
+ const catalog = new Map<string, Snippet>();
+ const { report } = withAnimation(
+ { targets: [{ selector: ".w-dot" }], maxBytes: 256000 },
+ catalog,
+ );
+ const ids = report.snippets.map((snippet) => snippet.id).reverse();
+ expect(ids).toHaveLength(2);
+ const { report: fetched } = withAnimation(
+ { targets: [], snippets: ids, maxBytes: 256000 },
+ catalog,
+ );
+ expect(fetched.snippets.map((snippet) => snippet.id)).toEqual(ids);
+ expect(fetched.omittedCount).toBe(0);
+ });
+});