Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
refactor(renderer): bind document write loads to exact owners
ldm0b6f798dfa9f8
diff --git a/moli-renderer-v8/src/document_runtime/document_write.rs b/moli-renderer-v8/src/document_runtime/document_write.rsindex 38aecd58e..e639d0598 100644--- a/moli-renderer-v8/src/document_runtime/document_write.rs+++ b/moli-renderer-v8/src/document_runtime/document_write.rs@@ -1183,7 +1183,8 @@ impl DocumentRuntime {let load_id = self.next_document_write_external_script_load_id;self.next_document_write_external_script_load_id = self.next_document_write_external_script_load_id- .wrapping_add(1);+ .checked_add(1)+ .expect("document.write external-script load id space exhausted");load_id}@@ -1244,11 +1245,8 @@ impl DocumentRuntime {continue;}let load_id = self.allocate_document_write_external_script_load_id();- let target = crate::types::DocumentWriteExternalScriptFetchTarget::new(- task_owner,- load_id,- self.runtime_reset_generation,- );+ let target =+ crate::types::DocumentWriteExternalScriptFetchTarget::new(task_owner, load_id);let script = request.to_preload_script();let resource_type_hint = request.resource_type_hint();self.document_write_script_preloads.insert(@@ -1393,11 +1391,7 @@ impl DocumentRuntime {};let load_id = self.allocate_document_write_external_script_load_id();(- crate::types::DocumentWriteExternalScriptFetchTarget::new(- task_owner,- load_id,- self.runtime_reset_generation,- ),+ crate::types::DocumentWriteExternalScriptFetchTarget::new(task_owner, load_id),false,None,)@@ -1686,7 +1680,6 @@ impl DocumentRuntime {}if unsafe { &*host_ptr }.current_main_document_task_owner()!= Some(completion_target.task_owner())- || self.runtime_reset_generation != completion_target.runtime_generation(){return super::DocumentWriteExternalScriptLoadApplication::RejectedStaleTarget;}@@ -1697,7 +1690,6 @@ impl DocumentRuntime {if pending.target != completion_target|| unsafe { &*host_ptr }.current_main_document_task_owner()!= Some(completion_target.task_owner())- || self.runtime_reset_generation != completion_target.runtime_generation(){self.pending_document_write_external_script_load = Some(pending);return super::DocumentWriteExternalScriptLoadApplication::RejectedStaleTarget;@@ -1808,7 +1800,6 @@ impl DocumentRuntime {if unsafe { &*host_ptr }.current_main_document_task_owner()!= Some(completion_target.task_owner())- || self.runtime_reset_generation != completion_target.runtime_generation(){return super::DocumentWriteExternalScriptLoadApplication::SupersededDuringApplication;}@@ -1829,7 +1820,6 @@ impl DocumentRuntime {let _ = self.finish_root_document_parser_stream_if_ready(scope, host_ptr);if unsafe { &*host_ptr }.current_main_document_task_owner()== Some(completion_target.task_owner())- && self.runtime_reset_generation == completion_target.runtime_generation(){super::DocumentWriteExternalScriptLoadApplication::Applied} else {@@ -2756,6 +2746,17 @@ mod tests {assert!(parser.has_exclusive_stream_handle());}+ #[test]+ #[should_panic(expected = "document.write external-script load id space exhausted")]+ fn document_write_external_script_load_ids_never_wrap() {+ let document_url = Url::parse("https://document-write.test/load-id.html").unwrap();+ let document = HtmlParser.parse(document_url, "<!doctype html>".to_owned());+ let mut runtime = DocumentRuntime::new(&document);+ runtime.next_document_write_external_script_load_id = u64::MAX;++ let _ = runtime.allocate_document_write_external_script_load_id();+ }+#[test]fn document_write_deferred_handoff_claims_parser_pending_before_start() {for (index, (html, kind, mode)) in [diff --git a/moli-renderer-v8/src/runtime/page_vm/page_resource_completion/main_document.rs b/moli-renderer-v8/src/runtime/page_vm/page_resource_completion/main_document.rsindex 9e733be91..d2c7b127f 100644--- a/moli-renderer-v8/src/runtime/page_vm/page_resource_completion/main_document.rs+++ b/moli-renderer-v8/src/runtime/page_vm/page_resource_completion/main_document.rs@@ -13,7 +13,7 @@ use crate::{use super::super::PageVm;/// Proof that the Page lane executor matched one `document.write()` fetch-/// terminal against its complete current root/main-Document/runtime target.+/// terminal against its complete current root/main-Document/load target.pub(crate) struct AuthorizedCurrentDocumentWriteExternalScriptLoadCompletion(DocumentWriteExternalScriptLoadCompletion,);diff --git a/moli-renderer-v8/src/runtime/phase_one/document_write_resource_tests.rs b/moli-renderer-v8/src/runtime/phase_one/document_write_resource_tests.rsindex bd6bf40f7..0c4dfd83b 100644--- a/moli-renderer-v8/src/runtime/phase_one/document_write_resource_tests.rs+++ b/moli-renderer-v8/src/runtime/phase_one/document_write_resource_tests.rs@@ -946,8 +946,8 @@ document.close();}#[test]-fn runtime_generation_mismatch_is_rejected_by_the_page_arbiter() {- super::tests::run_phase_one_large_stack_test("document-write-stale-runtime-generation", || {+fn load_id_mismatch_is_rejected_by_the_page_arbiter() {+ super::tests::run_phase_one_large_stack_test("document-write-stale-load-id", || {let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()@@ -973,12 +973,14 @@ fn runtime_generation_mismatch_is_rejected_by_the_page_arbiter() {.expect("current pending load should expose its exact target");let stale_target = DocumentWriteExternalScriptFetchTarget::new(current_target.task_owner(),- current_target.load_id(),- current_target.runtime_generation().wrapping_add(1),+ current_target+ .load_id()+ .checked_add(1)+ .expect("test load id should have a successor"),);let stale_completion = DocumentWriteExternalScriptLoadCompletion::new(stale_target,- Ok("globalThis.__staleGenerationScriptRan = true;".to_owned()),+ Ok("globalThis.__staleLoadIdScriptRan = true;".to_owned()),None,DocumentWriteExternalScriptNetworkAttribution::new(document_url,@@ -992,23 +994,23 @@ fn runtime_generation_mismatch_is_rejected_by_the_page_arbiter() {root_document,stale_completion,))- .expect("stale generation terminal should enter the test queue");+ .expect("stale load-id terminal should enter the test queue");let executor = pending.runtime.page_vm.local_executor.clone();let (pending, outcome) = super::access::run_named_owner_local_task(executor,- "document.write stale-generation arbitration channel closed",+ "document.write stale-load-id arbitration channel closed",async move {let outcome = pending.runtime.page_vm.apply_one_page_resource_terminal_owner_admission_for_test(&mut stale_queue)?- .expect("stale generation terminal should consume one bounded turn");+ .expect("stale load-id terminal should consume one bounded turn");Ok((pending, outcome))},).await- .expect("stale generation arbitration should succeed");+ .expect("stale load-id arbitration should succeed");assert_eq!(outcome.action.document_effect,PageResourceCompletionDocumentEffect::DiscardedStaleOwner {@@ -1027,16 +1029,16 @@ fn runtime_generation_mismatch_is_rejected_by_the_page_arbiter() {.vm().current_document_write_external_script_fetch_target(),Some(current_target),- "a mismatched generation must not consume the current pending load"+ "a mismatched load id must not consume the current pending load");let (pending, result) = evaluate_pending_on_owner_local_task(pending,- "String(globalThis.__staleGenerationScriptRan)",+ "String(globalThis.__staleLoadIdScriptRan)",).await;assert_eq!(result.get("value").and_then(serde_json::Value::as_str), Some("undefined"));// Keep the pending page, and therefore its request client, alive until the server has- // accepted and served the current-generation script. Dropping it before this await can+ // accepted and served the current script load. Dropping it before this await can// cancel the connect while the server is still blocked in `accept()`.server.await@@ -1106,7 +1108,7 @@ fn root_document_namespace_rejects_cross_page_target_collision() {.expect("current PageVm should expose its pending exact target");assert_eq!(retired_target, current_target,- "fresh PageVms naturally reuse their PageVm-local owner/load/generation namespace"+ "fresh PageVms naturally reuse their PageVm-local owner/load namespace");assert_ne!(retired_root, current_root,@@ -1267,10 +1269,6 @@ globalThis.__documentWriteEvents.push('inline-after');first_target.task_owner(),"both sequential loads belong to the same exact Document");- assert_eq!(- second_target.runtime_generation(),- first_target.runtime_generation()- );assert!(second_target.load_id() > first_target.load_id(),"sequential loads need distinct monotonic request identities"diff --git a/moli-renderer-v8/src/script_vm/subresource_fetch.rs b/moli-renderer-v8/src/script_vm/subresource_fetch.rsindex 616419944..abd9ca7b3 100644--- a/moli-renderer-v8/src/script_vm/subresource_fetch.rs+++ b/moli-renderer-v8/src/script_vm/subresource_fetch.rs@@ -5821,7 +5821,6 @@ impl ScriptVm {self.document_runtime.has_document_write_external_script_fetch_target(expected)&& self.current_main_document_task_owner() == Some(expected.task_owner())- && self.document_runtime.runtime_reset_generation() == expected.runtime_generation()}pub(crate) fn apply_current_document_write_external_script_load_completion(diff --git a/moli-renderer-v8/src/types.rs b/moli-renderer-v8/src/types.rsindex 1fb8bc16d..bcdadf924 100644--- a/moli-renderer-v8/src/types.rs+++ b/moli-renderer-v8/src/types.rs@@ -944,19 +944,16 @@ pub(super) struct XhrStreamingChunkDelivery<'s> {pub(crate) struct DocumentWriteExternalScriptFetchTarget {task_owner: crate::frame_owner_model::FrameDocumentTaskOwner,load_id: u64,- runtime_generation: u64,}impl DocumentWriteExternalScriptFetchTarget {pub(crate) fn new(task_owner: crate::frame_owner_model::FrameDocumentTaskOwner,load_id: u64,- runtime_generation: u64,) -> Self {Self {task_owner,load_id,- runtime_generation,}}@@ -967,10 +964,6 @@ impl DocumentWriteExternalScriptFetchTarget {pub(crate) fn load_id(self) -> u64 {self.load_id}-- pub(crate) fn runtime_generation(self) -> u64 {- self.runtime_generation- }}/// Producer-captured Network attribution for a `document.write()` script.@@ -1052,7 +1045,7 @@ impl DocumentWriteExternalScriptLoadCompletion {crate::frame_owner_model::DocumentId(79),);Self::new(- DocumentWriteExternalScriptFetchTarget::new(task_owner, load_id, 0),+ DocumentWriteExternalScriptFetchTarget::new(task_owner, load_id),Ok("window.documentWriteExternalScriptLoaded = true".to_owned()),None,DocumentWriteExternalScriptNetworkAttribution::new(