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(protocol): claim navigation work before reserving pages
ldm0f0a82869f03d
diff --git a/moli-protocol/src/conn.rs b/moli-protocol/src/conn.rsindex 4dda1e1a6..033fbd107 100644--- a/moli-protocol/src/conn.rs+++ b/moli-protocol/src/conn.rs@@ -547,29 +547,22 @@ pub use scheduler_state::{CdpRendererOwnerTurnOutcome, CdpSchedulerEvent, CdpTurpub(crate) use site_data_manager_surface::{BrowserContextReservedSiteDataOwnerState, BrowserContextSiteDataManagerOwnerState,};-pub use state::{- BrowserContext, BrowserWindowBounds, DevToolsPageResidenceIdentity, DocumentStartScript,- DownloadNavigation, EmulatedDeviceMetrics, EmulatedGeolocationOverride,- EmulatedGeolocationOverrideState, EmulatedMediaOverrides, IsolatedWorldDefinition,- LoadedNavigation, NavigationDispatchState, NavigationLoadOutcome, NavigationRequestLoadPolicy,- PageNavigationHistoryEntry, PageTargetHost, PendingNavigationHistoryUpdate,- RuntimeBindingDefinition, TargetInfo, URL_BASE,-};pub(crate) use state::{- BrowserContextPageStorageHandles, BrowserContextResourceStorageHandles,- BrowserContextStoragePartitionHandles, CommittedRendererAgentAttachment,- CommittedRendererDocumentBinding, CompletedDownloadBody, CompletedDownloadBodyArtifact,- DedicatedWorkerMainScriptOutcome, DedicatedWorkerMainScriptSnapshot,- DedicatedWorkerTargetState, DevToolsBrowserIdentityOverride, DevToolsConsoleOutputSessionState,- DevToolsEmulationSessionState, DevToolsLogViolationThreshold, DocumentNavigationToken,- DuplicatePendingRendererCommand, EffectiveTargetEmulationState,- EffectiveTargetEmulationStateDelta, EmulatedNetworkConditions, EmulatedViewportSurface,- InspectorCommandDispatch, NETWORK_ERROR_PAGE_URL, NavigationResultProjection,- NavigationSourceDocumentSecurityContext, NetworkErrorPageNavigation, PageScreencastConfig,- PageScreencastFormat, PendingBidiChannelListener, PendingInspectorAwait,- PendingRendererCommandKey, PerformanceTimeDomain, PreparedRendererCallDispatch, ProfilerAction,- ProfilerInspectorCommand, RendererCommandCorrelation, RendererCommandDescriptor,- RendererCommandReplay, RendererDocumentLifecycleObservation, RendererDocumentLifecycleObserver,+ BackgroundNavigationClaimError, BrowserContextPageStorageHandles,+ BrowserContextResourceStorageHandles, BrowserContextStoragePartitionHandles,+ CommittedRendererAgentAttachment, CommittedRendererDocumentBinding, CompletedDownloadBody,+ CompletedDownloadBodyArtifact, DedicatedWorkerMainScriptOutcome,+ DedicatedWorkerMainScriptSnapshot, DedicatedWorkerTargetState, DevToolsBrowserIdentityOverride,+ DevToolsConsoleOutputSessionState, DevToolsEmulationSessionState,+ DevToolsLogViolationThreshold, DocumentNavigationToken, DuplicatePendingRendererCommand,+ EffectiveTargetEmulationState, EffectiveTargetEmulationStateDelta, EmulatedNetworkConditions,+ EmulatedViewportSurface, InspectorCommandDispatch, NETWORK_ERROR_PAGE_URL,+ NavigationResultProjection, NavigationSourceDocumentSecurityContext,+ NetworkErrorPageNavigation, PageScreencastConfig, PageScreencastFormat,+ PendingBidiChannelListener, PendingInspectorAwait, PendingRendererCommandKey,+ PerformanceTimeDomain, PreparedRendererCallDispatch, ProfilerAction, ProfilerInspectorCommand,+ RendererCommandCorrelation, RendererCommandDescriptor, RendererCommandReplay,+ RendererDocumentLifecycleObservation, RendererDocumentLifecycleObserver,RendererMainDocumentCommitSeed, RendererPageResidenceIdentity,ServiceWorkerRuntimeExceptionSnapshot, ServiceWorkerTargetState, SharedWorkerTargetState,SiteDataClearOptions, TargetIdentityState, TargetInitialEmptyDocumentCreator, TargetOwnerState,@@ -583,6 +576,14 @@ pub(crate) use state::{TargetServiceWorkerVersionRetirement, TargetSharedWorkerProtocolAttachmentIdentity,TargetSharedWorkerProtocolAttachmentRetirement, TargetWindowSurfaceState,};+pub use state::{+ BrowserContext, BrowserWindowBounds, DevToolsPageResidenceIdentity, DocumentStartScript,+ DownloadNavigation, EmulatedDeviceMetrics, EmulatedGeolocationOverride,+ EmulatedGeolocationOverrideState, EmulatedMediaOverrides, IsolatedWorldDefinition,+ LoadedNavigation, NavigationDispatchState, NavigationLoadOutcome, NavigationRequestLoadPolicy,+ PageNavigationHistoryEntry, PageTargetHost, PendingNavigationHistoryUpdate,+ RuntimeBindingDefinition, TargetInfo, URL_BASE,+};#[cfg(test)]pub(crate) use state::{DevToolsSessionState, TargetJavaScriptDialog, TargetJavaScriptDialogScopeObserver,@@ -1587,26 +1588,6 @@ impl CdpConnection {.document_navigation_cancellation_handle(token)}- pub(crate) fn arm_background_navigation_completion(- &mut self,- token: &DocumentNavigationToken,- additional_cancellation: Option<moli_fetch::FetchCancelHandle>,- ) -> bool {- let browser_context_id = self- .browser_context_id_for_target(&token.target_id)- .map(str::to_owned);- let Some(browser_context_id) = browser_context_id else {- if let Some(cancellation) = additional_cancellation {- cancellation.cancel();- }- return false;- };- self.browser_context_by_id_mut(&browser_context_id)- .is_some_and(|browser_context| {- browser_context.arm_background_navigation_completion(token, additional_cancellation)- })- }-pub(crate) fn settle_background_navigation_completion(&mut self,token: &DocumentNavigationToken,diff --git a/moli-protocol/src/conn/runtime_load.rs b/moli-protocol/src/conn/runtime_load.rsindex 1cfc9324e..f6279e20d 100644--- a/moli-protocol/src/conn/runtime_load.rs+++ b/moli-protocol/src/conn/runtime_load.rs@@ -614,6 +614,13 @@ fn complete_body_capture(}}+/// Inputs captured by the same synchronous transition that claims completion.+/// The target-owned request retains the claim until completion or supersession.+struct BackgroundNavigationClaim {+ load_inputs: TargetNavigationLoadInputs,+ cancellation: FetchCancelHandle,+}+pub(crate) struct BackgroundNavigationLoadJob {engine: NavigationEngine,page_reservation: RendererPageReservationToken,@@ -1477,6 +1484,39 @@ async fn build_navigation_from_streaming_raw_response_with_engine_async(}impl CdpConnection {+ fn claim_background_navigation(+ &mut self,+ token: &DocumentNavigationToken,+ navigation: &NavigationDispatchState,+ additional_cancellation: Option<FetchCancelHandle>,+ ) -> Result<BackgroundNavigationClaim, BackgroundNavigationClaimError> {+ if token.target_id != navigation.frame_id || token.loader_id != navigation.loader_id {+ return Err(BackgroundNavigationClaimError::Stale);+ }+ let (browser_context_id, _) = self+ .target_owner_identity_for_owner(&navigation.owner)+ .filter(|(_, target_id)| target_id.as_deref() == Some(token.target_id.as_str()))+ .ok_or(BackgroundNavigationClaimError::Stale)?;+ let slot = &mut self+ .browser_context_by_id_mut(&browser_context_id)+ .and_then(|context| context.page_target_mut(&token.target_id))+ .ok_or(BackgroundNavigationClaimError::Stale)?+ .runtime_slot;+ // This is the only pending-request lookup. Capture replacement authority+ // from its returned cancellation handle, never from an ambient request.+ let cancellation =+ slot.claim_background_navigation_completion(token, additional_cancellation)?;+ let document_replacement = slot+ .loaded_page()+ .map(|page| page.document_replacement(cancellation.clone()));+ let mut load_inputs = self.navigation_request_load_inputs(navigation);+ load_inputs.document_replacement = document_replacement;+ Ok(BackgroundNavigationClaim {+ load_inputs,+ cancellation,+ })+ }+fn navigation_load_inputs_for_navigation(&self,token: Option<&DocumentNavigationToken>,@@ -2275,6 +2315,23 @@ impl CdpConnection {let load_inputs = self.navigation_load_inputs_for_navigation(Some(token), navigation).ok()?;+ Some(self.navigation_load_job_with_inputs(+ navigation,+ load_inputs,+ cancellation,+ body_progress_source,+ early_result,+ ))+ }++ fn navigation_load_job_with_inputs(+ &mut self,+ navigation: &NavigationDispatchState,+ load_inputs: TargetNavigationLoadInputs,+ cancellation: FetchCancelHandle,+ body_progress_source: MainDocumentBodyProgressSource,+ early_result: Option<BackgroundNavigationEarlyResult>,+ ) -> BackgroundNavigationLoadJob {// Ensure the target's resident browser resource runtime exists before// cloning its engine handle. The job shares the target Page policy and// retains this exact transport/cache owner for the captured navigation.@@ -2283,7 +2340,7 @@ impl CdpConnection {let engine = self.navigation_engine_handle_for_load_inputs(&load_inputs);let page_reservation =self.reserve_renderer_page_for_owner(&navigation.owner, &load_inputs, &engine);- Some(BackgroundNavigationLoadJob {+ BackgroundNavigationLoadJob {engine,page_reservation,cancellation,@@ -2295,7 +2352,7 @@ impl CdpConnection {request_headers: navigation.request_headers.clone(),body_progress_source,shared_resource_runtime,- })+ }}pub(crate) fn background_navigation_load_job_for_navigation(@@ -2304,15 +2361,18 @@ impl CdpConnection {navigation: &NavigationDispatchState,body_progress_source: MainDocumentBodyProgressSource,early_result: Option<BackgroundNavigationEarlyResult>,- ) -> Option<BackgroundNavigationLoadJob> {- let job = self.navigation_load_job_for_navigation(- token,+ ) -> Result<BackgroundNavigationLoadJob, BackgroundNavigationClaimError> {+ let BackgroundNavigationClaim {+ load_inputs,+ cancellation,+ } = self.claim_background_navigation(token, navigation, None)?;+ Ok(self.navigation_load_job_with_inputs(navigation,+ load_inputs,+ cancellation,body_progress_source,early_result,- )?;- self.arm_background_navigation_completion(token, None)- .then_some(job)+ ))}pub(crate) fn background_streaming_response_navigation_load_job_for_navigation(@@ -2324,21 +2384,20 @@ impl CdpConnection {response_code: Option<u16>,response_headers_override: Vec<(String, Vec<u8>)>,body_progress_source: MainDocumentBodyProgressSource,- ) -> Option<BackgroundStreamingResponseNavigationLoadJob> {- // Reject stale work before reserving a renderer Page or arming completion.+ ) -> Result<BackgroundStreamingResponseNavigationLoadJob, BackgroundNavigationClaimError> {+ // Reject stale or duplicate work before reserving a renderer Page.// Dropping the rejected response cancels only its own transport.- let load_inputs = self- .navigation_load_inputs_for_navigation(Some(token), navigation)- .ok()?;- if !self.arm_background_navigation_completion(token, Some(response.cancellation_handle())) {- return None;- }+ let BackgroundNavigationClaim { load_inputs, .. } = self.claim_background_navigation(+ token,+ navigation,+ Some(response.cancellation_handle()),+ )?;let shared_resource_runtime =self.shared_resource_runtime_for_navigation_load_inputs(&load_inputs);let engine = self.navigation_engine_handle_for_load_inputs(&load_inputs);let page_reservation =self.reserve_renderer_page_for_owner(&navigation.owner, &load_inputs, &engine);- Some(BackgroundStreamingResponseNavigationLoadJob {+ Ok(BackgroundStreamingResponseNavigationLoadJob {engine,page_reservation,load_inputs,diff --git a/moli-protocol/src/conn/state/browser_context.rs b/moli-protocol/src/conn/state/browser_context.rsindex 952bc8520..18adbdd4a 100644--- a/moli-protocol/src/conn/state/browser_context.rs+++ b/moli-protocol/src/conn/state/browser_context.rs@@ -1177,22 +1177,6 @@ impl BrowserContext {})}- pub(crate) fn arm_background_navigation_completion(- &mut self,- token: &DocumentNavigationToken,- additional_cancellation: Option<moli_fetch::FetchCancelHandle>,- ) -> bool {- let Some(target) = self.page_target_mut(&token.target_id) else {- if let Some(cancellation) = additional_cancellation {- cancellation.cancel();- }- return false;- };- target- .runtime_slot- .arm_background_navigation_completion(token, additional_cancellation)- }-pub(crate) fn settle_background_navigation_completion(&mut self,token: &DocumentNavigationToken,diff --git a/moli-protocol/src/conn/state/mod.rs b/moli-protocol/src/conn/state/mod.rsindex 3751644d6..b54a34a86 100644--- a/moli-protocol/src/conn/state/mod.rs+++ b/moli-protocol/src/conn/state/mod.rs@@ -66,8 +66,8 @@ pub(crate) use page_resource::MainDocumentResourceSnapshot;#[cfg(test)]pub(crate) use page_slot::TargetPageSlot;pub(crate) use page_slot::{- CommittedRendererDocumentBinding, DocumentNavigationToken, InitialDocumentPageBuildWaiter,- RendererDocumentLifecycleWaiterId, TargetPageAbsenceReason,+ BackgroundNavigationClaimError, CommittedRendererDocumentBinding, DocumentNavigationToken,+ InitialDocumentPageBuildWaiter, RendererDocumentLifecycleWaiterId, TargetPageAbsenceReason,};pub use page_slot::{DocumentStartScript, IsolatedWorldDefinition, RuntimeBindingDefinition};diff --git a/moli-protocol/src/conn/state/page_slot.rs b/moli-protocol/src/conn/state/page_slot.rsindex 31d323344..028355a83 100644--- a/moli-protocol/src/conn/state/page_slot.rs+++ b/moli-protocol/src/conn/state/page_slot.rs@@ -70,6 +70,14 @@ impl Hash for DocumentNavigationToken {}}+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub(crate) enum BackgroundNavigationClaimError {+ /// No matching request remains eligible to start background work.+ Stale,+ /// The matching request already has an owner for its completion.+ AlreadyClaimed,+}+/// The target-owned lifetime of one cross-Document navigation request.////// The exact token remains here from navigation admission until the request@@ -101,21 +109,8 @@ impl PendingNavigationRequest {self.token == *token}- fn cancellation_handle(&self) -> moli_fetch::FetchCancelHandle {- self.cancellation_handles- .first()- .expect("a pending navigation request must own cancellation authority")- .clone()- }-- fn arm_background_completion(- &mut self,- additional_cancellation: Option<moli_fetch::FetchCancelHandle>,- ) {- if let Some(cancellation) = additional_cancellation {- self.cancellation_handles.push(cancellation);- }- self.background_completion_pending = true;+ fn cancellation_handle(&self) -> Option<moli_fetch::FetchCancelHandle> {+ self.cancellation_handles.first().cloned()}fn settle_background_completion(&mut self) {@@ -685,24 +680,33 @@ impl TargetPageSlot {self.pending_navigation_request.as_ref().filter(|request| request.matches(token) && !request.committed)- .map(PendingNavigationRequest::cancellation_handle)+ .and_then(PendingNavigationRequest::cancellation_handle)}- pub(crate) fn arm_background_navigation_completion(+ /// Claims the exact request's sole background completion and returns its+ /// cancellation authority. Rejection leaves the request unchanged; the+ /// caller still owns (and must drop) any rejected response.+ pub(crate) fn claim_background_navigation_completion(&mut self,token: &DocumentNavigationToken,additional_cancellation: Option<moli_fetch::FetchCancelHandle>,- ) -> bool {- let Some(request) = self.pending_navigation_request.as_mut().filter(|request| {- request.matches(token) && !request.committed && !request.background_completion_pending- }) else {- if let Some(cancellation) = additional_cancellation {- cancellation.cancel();- }- return false;- };- request.arm_background_completion(additional_cancellation);- true+ ) -> Result<moli_fetch::FetchCancelHandle, BackgroundNavigationClaimError> {+ let request = self+ .pending_navigation_request+ .as_mut()+ .filter(|request| request.matches(token) && !request.committed)+ .ok_or(BackgroundNavigationClaimError::Stale)?;+ if request.background_completion_pending {+ return Err(BackgroundNavigationClaimError::AlreadyClaimed);+ }+ let cancellation = request+ .cancellation_handle()+ .ok_or(BackgroundNavigationClaimError::Stale)?;+ if let Some(additional_cancellation) = additional_cancellation {+ request.cancellation_handles.push(additional_cancellation);+ }+ request.background_completion_pending = true;+ Ok(cancellation)}pub(crate) fn settle_background_navigation_completion(@@ -1484,6 +1488,9 @@ impl TargetPageSlot {}}+#[cfg(test)]+mod navigation_claim_tests;+#[cfg(test)]mod page_residence_tests {use std::{diff --git a/moli-protocol/src/conn/state/page_slot/navigation_claim_tests.rs b/moli-protocol/src/conn/state/page_slot/navigation_claim_tests.rsnew file mode 100644index 000000000..6f74c7b2f--- /dev/null+++ b/moli-protocol/src/conn/state/page_slot/navigation_claim_tests.rs@@ -0,0 +1,107 @@+use super::*;+use moli_fetch::FetchCancelHandle;++#[test]+fn claim_requires_the_exact_live_request() {+ let mut slot = TargetPageSlot::default();+ let old = slot.start_document_navigation("target".into(), "loader".into());+ let current = slot.start_document_navigation("target".into(), "loader".into());+ for rejected in [+ old,+ DocumentNavigationToken {+ target_id: "other target".into(),+ ..current.clone()+ },+ DocumentNavigationToken {+ loader_id: "other loader".into(),+ ..current.clone()+ },+ ] {+ assert_eq!(+ slot.claim_background_navigation_completion(&rejected, None)+ .unwrap_err(),+ BackgroundNavigationClaimError::Stale+ );+ assert!(!slot.has_inflight_background_navigation());+ }+ let cancellation = slot+ .claim_background_navigation_completion(¤t, None)+ .unwrap();+ slot.clear_document_navigation_state();+ assert!(cancellation.is_cancelled());+ assert_eq!(+ slot.claim_background_navigation_completion(¤t, None)+ .unwrap_err(),+ BackgroundNavigationClaimError::Stale+ );+}++#[test]+fn duplicate_claim_does_not_take_the_rejected_transports_cancellation() {+ let mut slot = TargetPageSlot::default();+ let token = slot.start_document_navigation("target".into(), "loader".into());+ let response = FetchCancelHandle::new();+ let duplicate = FetchCancelHandle::new();+ let cancellation = slot+ .claim_background_navigation_completion(&token, Some(response.clone()))+ .unwrap();+ assert_eq!(+ slot.claim_background_navigation_completion(&token, Some(duplicate.clone()))+ .unwrap_err(),+ BackgroundNavigationClaimError::AlreadyClaimed+ );+ assert!(!cancellation.is_cancelled());+ assert!(!response.is_cancelled());+ slot.clear_document_navigation_state();+ assert!(cancellation.is_cancelled());+ assert!(response.is_cancelled());+ assert!(+ !duplicate.is_cancelled(),+ "the rejected response remains caller-owned"+ );+}++#[test]+fn committed_request_is_stale_even_while_background_completion_is_pending() {+ for background in [false, true] {+ let mut slot = TargetPageSlot::default();+ let token = slot.start_document_navigation("target".into(), "loader".into());+ let cancellation = slot+ .document_navigation_cancellation_handle(&token)+ .unwrap();+ if background {+ slot.claim_background_navigation_completion(&token, None)+ .unwrap();+ }+ assert!(slot.commit_pending_document_navigation_if_matches(&token));+ assert_eq!(+ slot.claim_background_navigation_completion(&token, None)+ .unwrap_err(),+ BackgroundNavigationClaimError::Stale+ );+ assert_eq!(slot.has_inflight_background_navigation(), background);+ assert_eq!(+ slot.settle_background_navigation_completion(&token),+ background+ );+ assert!(!slot.has_inflight_background_navigation());+ assert!(!cancellation.is_cancelled());+ }+}++#[test]+fn settled_request_cannot_be_claimed_again_before_its_commit_is_drained() {+ let mut slot = TargetPageSlot::default();+ let token = slot.start_document_navigation("target".into(), "loader".into());+ let cancellation = slot+ .claim_background_navigation_completion(&token, None)+ .unwrap();+ assert!(slot.settle_background_navigation_completion(&token));+ assert_eq!(+ slot.claim_background_navigation_completion(&token, None)+ .unwrap_err(),+ BackgroundNavigationClaimError::Stale+ );+ assert!(slot.commit_pending_document_navigation_if_matches(&token));+ assert!(!cancellation.is_cancelled());+}diff --git a/moli-protocol/src/conn/state/runtime_slot.rs b/moli-protocol/src/conn/state/runtime_slot.rsindex 665372e43..419096828 100644--- a/moli-protocol/src/conn/state/runtime_slot.rs+++ b/moli-protocol/src/conn/state/runtime_slot.rs@@ -553,13 +553,13 @@ impl TargetRuntimeSlot {.document_navigation_cancellation_handle(token)}- pub(crate) fn arm_background_navigation_completion(+ pub(crate) fn claim_background_navigation_completion(&mut self,token: &DocumentNavigationToken,additional_cancellation: Option<moli_fetch::FetchCancelHandle>,- ) -> bool {+ ) -> Result<moli_fetch::FetchCancelHandle, super::BackgroundNavigationClaimError> {self.page_slot- .arm_background_navigation_completion(token, additional_cancellation)+ .claim_background_navigation_completion(token, additional_cancellation)}pub(crate) fn settle_background_navigation_completion(diff --git a/moli-protocol/src/conn/tests/mod.rs b/moli-protocol/src/conn/tests/mod.rsindex 5ac234020..219ede092 100644--- a/moli-protocol/src/conn/tests/mod.rs+++ b/moli-protocol/src/conn/tests/mod.rs@@ -460,8 +460,13 @@ fn navigation_gate_resolves_websocket_events_to_their_session_target() {let navigation_a = target_a.start_document_navigation_for_active_target("LOADER-A".to_owned()).expect("target A should accept a navigation request");+ target_a+ .active_page_target_mut()+ .runtime_slot+ .page_slot_mut()+ .claim_background_navigation_completion(&navigation_a, None)+ .unwrap();conn.install_browser_context_fixture_for_test(target_a);- assert!(conn.arm_background_navigation_completion(&navigation_a, None));let mut target_b = BrowserContext::new("BID-B".to_owned());target_b.set_active_target_id("TID-B");diff --git a/moli-protocol/src/conn/tests/navigation_error.rs b/moli-protocol/src/conn/tests/navigation_error.rsindex f6bf27262..ceec764b9 100644--- a/moli-protocol/src/conn/tests/navigation_error.rs+++ b/moli-protocol/src/conn/tests/navigation_error.rs@@ -1,7 +1,8 @@use super::*;use crate::conn::{- CommandOwnerScope, DocumentNavigationToken, NavigationLoadOutcome, NavigationNetworkError,- NavigationNetworkErrorKind, NavigationRequestLoadPolicy,+ BackgroundNavigationClaimError, CommandOwnerScope, DocumentNavigationToken,+ NavigationLoadOutcome, NavigationNetworkError, NavigationNetworkErrorKind,+ NavigationRequestLoadPolicy,};use moli_core::page::{SubresourceAuthCredentials, SubresourceAuthScheme, SubresourceAuthTarget};@@ -146,7 +147,10 @@ fn streaming_response_job(token: &DocumentNavigationToken,navigation: &NavigationDispatchState,) -> (- Option<crate::conn::runtime_load::BackgroundStreamingResponseNavigationLoadJob>,+ Result<+ crate::conn::runtime_load::BackgroundStreamingResponseNavigationLoadJob,+ BackgroundNavigationClaimError,+ >,moli_fetch::FetchCancelHandle,) {let crate::conn::DocumentBodySource::StreamingRaw { response, .. } =@@ -235,18 +239,26 @@ async fn superseded_streaming_response_cannot_arm_the_current_navigation() {let (job, stale_cancellation) = streaming_response_job(&mut ctx.conn, &stale, &navigation);assert!(- job.is_none(),+ matches!(job, Err(BackgroundNavigationClaimError::Stale)),"stale responses must not create renderer preparation jobs");assert!(stale_cancellation.is_cancelled());assert!(!current_cancellation.is_cancelled());assert!(!ctx.conn.has_inflight_background_navigation());- let (job, _) = streaming_response_job(&mut ctx.conn, ¤t, &navigation);- assert!(- job.is_some(),- "the matching response must still be accepted"- );+ let (job, current_response_cancellation) =+ streaming_response_job(&mut ctx.conn, ¤t, &navigation);+ assert!(job.is_ok(), "the matching response must still be accepted");+ assert!(ctx.conn.has_inflight_background_navigation());++ let (stale_job, stale_cancellation) =+ streaming_response_job(&mut ctx.conn, &stale, &navigation);+ assert!(matches!(+ stale_job,+ Err(BackgroundNavigationClaimError::Stale)+ ));+ assert!(stale_cancellation.is_cancelled());+ assert!(!current_response_cancellation.is_cancelled());assert!(ctx.conn.has_inflight_background_navigation());}@@ -254,16 +266,228 @@ async fn superseded_streaming_response_cannot_arm_the_current_navigation() {async fn duplicate_streaming_response_does_not_cancel_the_armed_navigation() {let (mut ctx, token, navigation) = navigation_fixture();let (first, first_cancellation) = streaming_response_job(&mut ctx.conn, &token, &navigation);- assert!(first.is_some());+ assert!(first.is_ok());let (duplicate, duplicate_cancellation) =streaming_response_job(&mut ctx.conn, &token, &navigation);assert!(- duplicate.is_none(),- "an already armed request must not spawn another job"+ matches!(+ duplicate,+ Err(BackgroundNavigationClaimError::AlreadyClaimed)+ ),+ "an already claimed request must not spawn another job");assert!(duplicate_cancellation.is_cancelled());assert!(!first_cancellation.is_cancelled());assert!(ctx.conn.has_inflight_background_navigation());+ assert!(matches!(+ ctx.conn.background_navigation_load_job_for_navigation(+ &token,+ &navigation,+ Default::default(),+ None,+ ),+ Err(BackgroundNavigationClaimError::AlreadyClaimed)+ ));+ assert!(!first_cancellation.is_cancelled());+}++#[tokio::test]+async fn duplicate_background_jobs_preserve_the_original_renderer_reservation() {+ let (mut ctx, token, navigation) = navigation_fixture();+ let original_page = crate::conn::RendererPageResidenceIdentity::new(+ moli_core::RendererOwnerLocalHostId::new_for_testing(41),+ moli_core::PageId::new_for_testing(17),+ );+ let slot = ctx+ .conn+ .runtime_session_owner_slot_mut(Some("SID-1"))+ .unwrap();+ let cancellation = slot+ .claim_background_navigation_completion(&token, None)+ .unwrap();+ assert!(+ slot.page_slot_mut()+ .bind_pending_document_navigation_renderer_page(&token, original_page)+ );++ assert!(matches!(+ ctx.conn.background_navigation_load_job_for_navigation(+ &token,+ &navigation,+ Default::default(),+ None,+ ),+ Err(BackgroundNavigationClaimError::AlreadyClaimed)+ ));+ let (response, response_cancellation) =+ streaming_response_job(&mut ctx.conn, &token, &navigation);+ assert!(matches!(+ response,+ Err(BackgroundNavigationClaimError::AlreadyClaimed)+ ));+ assert!(response_cancellation.is_cancelled());+ assert!(!cancellation.is_cancelled());+ assert!(ctx.conn.has_inflight_background_navigation());+ assert!(+ ctx.conn+ .runtime_session_owner_slot(Some("SID-1"))+ .unwrap()+ .page_slot()+ .routes_renderer_page(original_page),+ "rejecting duplicate work must not reserve a new renderer Page and replace the original binding"+ );+}++#[tokio::test]+async fn background_claim_checks_dispatch_identity_and_owner_before_claiming() {+ let (mut ctx, token, navigation) = navigation_fixture();+ let mut other = BrowserContext::new("BID-other".to_owned());+ other.set_active_target_id("TID-other");+ other.attach_active_session("SID-other");+ other+ .start_document_navigation_for_active_target(token.loader_id.clone())+ .unwrap();+ ctx.conn+ .push_inactive_browser_context_fixture_for_test(other);+ let cancellation = ctx+ .conn+ .document_navigation_cancellation_handle(&token)+ .unwrap();+ for mismatch in [+ NavigationDispatchState {+ frame_id: "other frame".into(),+ ..navigation.clone()+ },+ NavigationDispatchState {+ loader_id: "other loader".into(),+ ..navigation.clone()+ },+ NavigationDispatchState {+ owner: CommandOwnerScope::for_session("missing session"),+ ..navigation.clone()+ },+ NavigationDispatchState {+ owner: CommandOwnerScope::for_session("SID-other"),+ ..navigation.clone()+ },+ ] {+ let (response, response_cancellation) =+ streaming_response_job(&mut ctx.conn, &token, &mismatch);+ assert!(matches!(+ response,+ Err(BackgroundNavigationClaimError::Stale)+ ));+ assert!(response_cancellation.is_cancelled());+ assert!(matches!(+ ctx.conn.background_navigation_load_job_for_navigation(+ &token,+ &mismatch,+ Default::default(),+ None,+ ),+ Err(BackgroundNavigationClaimError::Stale)+ ));+ assert!(!ctx.conn.has_inflight_background_navigation());+ assert!(!cancellation.is_cancelled());+ }+ let job = ctx+ .conn+ .background_navigation_load_job_for_navigation(+ &token,+ &navigation,+ Default::default(),+ None,+ )+ .expect("rejected claims must leave the matching navigation available");+ assert!(+ ctx.conn+ .has_inflight_background_navigation_for_target(&token.target_id)+ );+ assert!(+ !ctx.conn+ .has_inflight_background_navigation_for_target("TID-other")+ );+ drop(job);+}++#[tokio::test]+async fn streaming_claim_rejects_committed_and_cleared_requests() {+ for (background, committed) in [(false, false), (false, true), (true, true)] {+ let (mut ctx, token, navigation) = navigation_fixture();+ let slot = ctx+ .conn+ .runtime_session_owner_slot_mut(Some("SID-1"))+ .unwrap()+ .page_slot_mut();+ if background {+ slot.claim_background_navigation_completion(&token, None)+ .unwrap();+ }+ if committed {+ assert!(slot.commit_pending_document_navigation_if_matches(&token));+ } else {+ slot.clear_document_navigation_state();+ }+ let (response, response_cancellation) =+ streaming_response_job(&mut ctx.conn, &token, &navigation);+ assert!(matches!(+ response,+ Err(BackgroundNavigationClaimError::Stale)+ ));+ assert!(response_cancellation.is_cancelled());+ assert_eq!(ctx.conn.has_inflight_background_navigation(), background);+ }+}++#[tokio::test]+async fn background_claims_are_target_owned_in_inactive_browser_contexts() {+ let (mut ctx, token, navigation) = navigation_fixture();+ let mut other = BrowserContext::new("BID-other".to_owned());+ other.set_active_target_id("TID-other");+ other.attach_active_session("SID-other");+ let other_token = other+ .start_document_navigation_for_active_target(token.loader_id.clone())+ .unwrap();+ ctx.conn+ .push_inactive_browser_context_fixture_for_test(other);+ let other_navigation = NavigationDispatchState {+ owner: CommandOwnerScope::for_session("SID-other"),+ frame_id: other_token.target_id.clone(),+ session_id: Some("SID-other".into()),+ ..navigation.clone()+ };+ let (other_job, other_cancellation) =+ streaming_response_job(&mut ctx.conn, &other_token, &other_navigation);+ assert!(other_job.is_ok());+ assert!(+ !ctx.conn+ .has_inflight_background_navigation_for_target(&token.target_id)+ );+ let job = ctx+ .conn+ .background_navigation_load_job_for_navigation(+ &token,+ &navigation,+ Default::default(),+ None,+ )+ .unwrap();+ ctx.conn+ .runtime_session_owner_slot_mut(Some("SID-1"))+ .unwrap()+ .page_slot_mut()+ .clear_document_navigation_state();+ assert!(!other_cancellation.is_cancelled());+ assert!(+ ctx.conn+ .has_inflight_background_navigation_for_target(&other_token.target_id)+ );+ assert!(!ctx.conn.settle_background_navigation_completion(&token));+ assert!(+ ctx.conn+ .has_inflight_background_navigation_for_target(&other_token.target_id)+ );+ drop(job);}#[tokio::test(flavor = "multi_thread")]diff --git a/moli-protocol/src/domains/fetch/commands.rs b/moli-protocol/src/domains/fetch/commands.rsindex 6a4d2bce4..74698f38c 100644--- a/moli-protocol/src/domains/fetch/commands.rs+++ b/moli-protocol/src/domains/fetch/commands.rs@@ -1,6 +1,7 @@use crate::conn::{- BackgroundNavigationBodyCompletionSink, CapturedBody, CdpConnection, Cmd, CommandOwnerScope,- DEFAULT_LOADER_ID, PendingStreamingDocumentResponseNavigation, monotonic_timestamp_seconds,+ BackgroundNavigationBodyCompletionSink, BackgroundNavigationClaimError, CapturedBody,+ CdpConnection, Cmd, CommandOwnerScope, DEFAULT_LOADER_ID,+ PendingStreamingDocumentResponseNavigation, monotonic_timestamp_seconds,};use crate::devtools_runtime::{DevToolsAuthChallengeAction, DevToolsCommand, DevToolsContinueInterceptedRequestCommand,@@ -1874,7 +1875,7 @@ fn continue_streaming_document_response_in_background(network_observation_journal,body_progress_source,} = pending;- let Some(job) = conn.background_streaming_response_navigation_load_job_for_navigation(+ let job = match conn.background_streaming_response_navigation_load_job_for_navigation(&document_navigation_token,&navigation,response,@@ -1882,19 +1883,17 @@ fn continue_streaming_document_response_in_background(response_code,response_headers,body_progress_source,- ) else {- // An already-armed current request keeps its original completion.- if !conn.accepts_pending_document_navigation_for_owner(- &navigation.owner,- &document_navigation_token,- ) {+ ) {+ Ok(job) => job,+ Err(BackgroundNavigationClaimError::AlreadyClaimed) => return,+ Err(BackgroundNavigationClaimError::Stale) => {let _ = sender.send(page::BackgroundNavigationCompletion::new(document_navigation_token,navigation,Err(anyhow::anyhow!(moli_fetch::NET_ERR_ABORTED_ERROR_TEXT)),));+ return;}- return;};tokio::task::spawn_local(async move {let body_completion_sink = BackgroundNavigationBodyCompletionSink::new(diff --git a/moli-protocol/src/domains/page/navigation.rs b/moli-protocol/src/domains/page/navigation.rsindex a1478aeb8..cd7890d1f 100644--- a/moli-protocol/src/domains/page/navigation.rs+++ b/moli-protocol/src/domains/page/navigation.rs@@ -17,11 +17,12 @@ use serde_json::{Value, json};use url::Url;use crate::conn::{- BackgroundNavigationLoadJob, BackgroundProtocolEvent, CapturedBody, CdpConnection,- CdpSessionRoute, Cmd, CommandDispatchContext, CommandOwnerScope, DocumentNavigationToken,- FetchRequestStage, NavigationDispatchState, NavigationLoadOutcome, NavigationRequestLoadPolicy,- NavigationResultProjection, NavigationSourceDocumentSecurityContext, PendingFetchNavigation,- ResponseStageUrlMatchPolicy, monotonic_timestamp_seconds,+ BackgroundNavigationClaimError, BackgroundNavigationLoadJob, BackgroundProtocolEvent,+ CapturedBody, CdpConnection, CdpSessionRoute, Cmd, CommandDispatchContext, CommandOwnerScope,+ DocumentNavigationToken, FetchRequestStage, NavigationDispatchState, NavigationLoadOutcome,+ NavigationRequestLoadPolicy, NavigationResultProjection,+ NavigationSourceDocumentSecurityContext, PendingFetchNavigation, ResponseStageUrlMatchPolicy,+ monotonic_timestamp_seconds,};use moli_cookie_jar::{NetworkCookieRequestContext, StoredCookieQueryReport};@@ -2924,40 +2925,48 @@ fn start_navigate_to_url_command_with_background_policy_and_request()})});- let Some(job) = conn.background_navigation_load_job_for_navigation(+ match conn.background_navigation_load_job_for_navigation(&document_navigation_token,&completion_state,body_progress_source,early_result,- ) else {- return NavigateCommandStart::CompletePlan(CommandOutputPlan::error(- -31998,- "NavigationRequestNotCurrent",- ));- };- tokio::task::spawn_local(async move {- let body_completion_sink = crate::conn::BackgroundNavigationBodyCompletionSink::new(- sender.clone(),- document_navigation_token.clone(),- completion_state.clone(),- );- let (navigation, early_result_sent) = job.run(Some(body_completion_sink)).await;- if early_result_sent {- completion_state.navigate_id = None;+ ) {+ Err(BackgroundNavigationClaimError::Stale) => {+ return NavigateCommandStart::CompletePlan(CommandOutputPlan::error(+ -31998,+ "NavigationRequestNotCurrent",+ ));}- if moli_trace::cdp_nav_timing_enabled() {- tracing::info!(- target: "moli_cdp_nav_timing",- url = %completion_state.requested_url,- stage = "background_lifecycle_completion_send",- );+ Err(BackgroundNavigationClaimError::AlreadyClaimed) => {+ // The original job owns both completion and the command reply.}- let _ = sender.send(BackgroundNavigationCompletion::new(- document_navigation_token,- completion_state,- navigation,- ));- });+ Ok(job) => {+ tokio::task::spawn_local(async move {+ let body_completion_sink =+ crate::conn::BackgroundNavigationBodyCompletionSink::new(+ sender.clone(),+ document_navigation_token.clone(),+ completion_state.clone(),+ );+ let (navigation, early_result_sent) = job.run(Some(body_completion_sink)).await;+ if early_result_sent {+ completion_state.navigate_id = None;+ }+ if moli_trace::cdp_nav_timing_enabled() {+ tracing::info!(+ target: "moli_cdp_nav_timing",+ url = %completion_state.requested_url,+ stage = "background_lifecycle_completion_send",+ );+ }+ let _ = sender.send(BackgroundNavigationCompletion::new(+ document_navigation_token,+ completion_state,+ navigation,+ ));+ });+ }+ }let mut output = CommandOutputBuffer::default();output.extend_background_events_after_messages(out);return NavigateCommandStart::CompleteImmediate(output.into_plan());diff --git a/moli-protocol/src/test_support.rs b/moli-protocol/src/test_support.rsindex f147819a2..c655efafd 100644--- a/moli-protocol/src/test_support.rs+++ b/moli-protocol/src/test_support.rs@@ -50,17 +50,19 @@ pub fn arm_background_navigation_request_for_target(target_id: &str,loader_id: &str,) -> BackgroundNavigationRequestFixture {- let token = conn+ let context = conn.browser_context.as_mut()- .and_then(|context| {- context.start_document_navigation_for_target(target_id, loader_id.to_owned())- })+ .expect("a navigation request fixture requires a browser context");+ let token = context+ .start_document_navigation_for_target(target_id, loader_id.to_owned()).expect("the active target must accept a navigation request fixture");- let cancellation = conn- .document_navigation_cancellation_handle(&token)- .expect("the target-owned request must expose its cancellation handle");- assert!(conn.arm_background_navigation_completion(&token, None));+ let cancellation = context+ .page_target_mut(target_id)+ .expect("the fixture's target must remain installed")+ .runtime_slot+ .claim_background_navigation_completion(&token, None)+ .expect("the fixture must claim its new request's background completion");BackgroundNavigationRequestFixture {token,cancellation,