Craft Anything: understand how any software works, and craft your own.
Rust
Live coverage and module traces record the scripts they show (#429)
BFlat51f48fe6f0df
diff --git a/crates/engine/src/tools/browser_tools/tests.rs b/crates/engine/src/tools/browser_tools/tests.rsindex 395354bf..c6c9b3da 100644--- a/crates/engine/src/tools/browser_tools/tests.rs+++ b/crates/engine/src/tools/browser_tools/tests.rs@@ -177,6 +177,13 @@ fn serve_ws(stream: &mut TcpStream) {{"url": "https://user:[email protected]/assets/main.js", "type": "Script", "mimeType": "application/javascript"}]}}),"Page.captureScreenshot" => json!({"data": tiny_png_b64()}),+ "Profiler.takePreciseCoverage" => {+ let scripts: Vec<Value> = ["a", "b", "c"]+ .iter()+ .map(|n| json!({"scriptId": n, "url": format!("https://app.test/{n}.js"), "functions": [{"isBlockCoverage": false, "ranges": [{"count": 1}]}]}))+ .collect();+ json!({"result": scripts})+ }"Debugger.getScriptSource" => json!({"scriptSource": bundle}),"Network.getResponseBody" => json!({"body": "{\"id\":1,\"secret\":\"x\"}", "base64Encoded": false}),// One accessible element: a button named "Sign in".@@ -916,3 +923,26 @@ fn the_recorder_refuses_a_bad_script_before_it_needs_a_browser() {assert_eq!(walkthrough(&e, bad).unwrap_err().code, ErrorCode::BadInput);}}++/// #331: `limit: 0` showed one script but recorded none examined; a page shows at least one, and+/// what it examined is what it shows.+#[test]+fn coverage_records_the_scripts_it_shows() {+ let (url, _) = fake_cdp();+ let e = Engine::new(crate::testing::session());+ let run = |offset: u64, limit: u64| {+ e.call(+ "web_coverage_measure",+ json!({"cdp_endpoint": url, "watch_ms": 1, "offset": offset, "limit": limit}),+ CallOptions::new(Caller::Mcp).with_consent(crate::Consent::Granted),+ )+ .map(|o| o.to_json())+ .unwrap()+ };+ for (offset, limit, shown) in [(0, 0, 1), (0, 1, 1), (1, 1, 1), (2, 5, 1), (0, 500, 3)] {+ let v = run(offset, limit);+ assert_eq!(v["result"]["scripts_total"], 3, "{v}");+ assert_eq!(v["result"]["scripts"].as_array().map(Vec::len), Some(shown), "{offset}/{limit}");+ assert_eq!(v["coverage"]["examined"], shown, "{offset}/{limit}");+ }+}diff --git a/crates/engine/src/tools/web_runtime_tools.rs b/crates/engine/src/tools/web_runtime_tools.rsindex 6412fa0b..e3deebbe 100644--- a/crates/engine/src/tools/web_runtime_tools.rs+++ b/crates/engine/src/tools/web_runtime_tools.rs@@ -133,9 +133,11 @@ fn d_limit() -> usize {fn web_coverage_measure(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, ToolError> {let p: ExecutionParams = parse_params(params)?;+ // A page shows at least one script (#331).+ let limit = p.limit.max(1);let dl = deadline(ctx, p.timeout_secs)?;let (browser, mut client, page_url) = connect(ctx, p.cdp_endpoint.as_deref(), p.launch, p.headless, p.no_sandbox, p.target_id.as_deref(), &dl)?;- let result = run_coverage(&mut client, &page_url, p.watch_ms, p.offset, p.limit, ctx, &dl);+ let result = run_coverage(&mut client, &page_url, p.watch_ms, p.offset, limit, ctx, &dl);browser_tools::cleanup(&mut client, &dl);let (result, total, covered) = result?;let summary = format!("Coverage on {page_url}: {covered} of {total} script(s) executed code during the window.");@@ -144,7 +146,7 @@ fn web_coverage_measure(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput,Subject { kind: "website".into(), sha256: None, name: Some(page_url.clone()) },browser_tools::browser_env(ctx.session.clean_room(), &browser),)- .params(json!({"endpoint": browser.endpoint.origin, "watch_ms": p.watch_ms, "offset": p.offset, "limit": p.limit}))+ .params(json!({"endpoint": browser.endpoint.origin, "watch_ms": p.watch_ms, "offset": p.offset, "limit": limit})).result(result).summary(summary).confidence(Confidence::Observed)@@ -159,7 +161,7 @@ fn web_coverage_measure(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput,)).coverage(Coverage {status: CoverageStatus::Partial,- examined: Some(total.min(p.offset.saturating_add(p.limit)) as u64),+ examined: Some(total.saturating_sub(p.offset).min(limit) as u64),total: Some(total as u64),next_offset: None,})@@ -222,7 +224,7 @@ fn run_coverage(}scripts.sort_by(|a, b| a["url"].as_str().unwrap_or("").cmp(b["url"].as_str().unwrap_or("")));let total = scripts.len();- let page: Vec<Value> = scripts.into_iter().skip(offset).take(limit.max(1)).collect();+ let page: Vec<Value> = scripts.into_iter().skip(offset).take(limit).collect();Ok((json!({"origin": page_url, "scripts": page, "scripts_total": total, "scripts_with_execution": covered}), total, covered))}@@ -336,6 +338,7 @@ fn d_window() -> u64 {fn web_modules_trace(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, ToolError> {let p: ModuleImportsParams = parse_params(params)?;+ let limit = p.limit.max(1);let dl = deadline(ctx, p.timeout_secs)?;let (browser, mut client, page_url) = connect(ctx, p.cdp_endpoint.as_deref(), p.launch, p.headless, p.no_sandbox, p.target_id.as_deref(), &dl)?;let flags = CaptureFlags { script_sources: true, ..CaptureFlags::default() };@@ -374,17 +377,17 @@ fn web_modules_trace(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, Tooedges.sort_by(|a, b| (a["from"].as_str(), a["to"].as_str()).cmp(&(b["from"].as_str(), b["to"].as_str())));let total_nodes = nodes.len();let total_edges = edges.len();- let node_page: Vec<Value> = nodes.into_iter().skip(p.offset).take(p.limit.max(1)).collect();+ let node_page: Vec<Value> = nodes.into_iter().skip(p.offset).take(limit).collect();let summary = format!("Module graph of {page_url}: {total_nodes} script(s), {total_edges} import edge(s), {} unresolved.", unresolved.len());let ev = EvidenceBuilder::new("web_modules_trace", Subject { kind: "website".into(), sha256: None, name: Some(page_url.clone()) }, browser_tools::browser_env(ctx.session.clean_room(), &browser))- .params(json!({"endpoint": browser.endpoint.origin, "offset": p.offset, "limit": p.limit}))+ .params(json!({"endpoint": browser.endpoint.origin, "offset": p.offset, "limit": limit})).result(json!({"origin": page_url, "nodes": node_page, "nodes_total": total_nodes, "edges": edges, "unresolved": unresolved})).summary(summary).confidence(Confidence::Derived).authority(Authority::EarlierRecord).limitation(Limitation::new("static_resolution", "Imports are read statically from the live sources; relative specifiers are resolved by URL, bare specifiers need the page's import map, and computed imports are reported as non-literal.")).limitation(Limitation::new("live_subset", "Only scripts the page had loaded during the window are included."))- .coverage(if p.offset == 0 && p.limit >= total_nodes { Coverage::complete(total_nodes as u64) } else { Coverage::page(p.limit.min(total_nodes.saturating_sub(p.offset)) as u64, total_nodes as u64, p.offset as u64) })+ .coverage(if p.offset == 0 && limit >= total_nodes { Coverage::complete(total_nodes as u64) } else { Coverage::page(limit.min(total_nodes.saturating_sub(p.offset)) as u64, total_nodes as u64, p.offset as u64) }).location(Location::new("url", page_url)).build()?;Ok(ToolOutput::evidence(ev))