Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(cdp): keep current renderer notifications live during navigation
ldm032de27264060
diff --git a/moli-protocol-server/src/cdp_scheduler/protocol_residence.rs b/moli-protocol-server/src/cdp_scheduler/protocol_residence.rsindex cd7f925b9..ad3559bba 100644--- a/moli-protocol-server/src/cdp_scheduler/protocol_residence.rs+++ b/moli-protocol-server/src/cdp_scheduler/protocol_residence.rs@@ -110,8 +110,13 @@ pub(super) enum ClientTurnPredecessor {impl ProtocolSchedulerResidence {pub(super) fn navigation_dependency(&self) -> ProtocolNavigationDependency {match self {+ // These events were already authorized and projected from a+ // concrete renderer publication. Their exact load and client-turn+ // predecessors control delivery. A new NavigationRequest on the+ // same target must not also suspend the active document's output+ // until that unrelated response arrives.+ Self::RendererOutputPublication(_) => ProtocolNavigationDependency::Independent,Self::ProtocolWork { work, .. } => work.navigation_dependency(),- Self::RendererOutputPublication(_) => ProtocolNavigationDependency::AfterLoad,}}diff --git a/moli-protocol-server/src/cdp_scheduler/tests.rs b/moli-protocol-server/src/cdp_scheduler/tests.rsindex ccebb9674..86fb45a8d 100644--- a/moli-protocol-server/src/cdp_scheduler/tests.rs+++ b/moli-protocol-server/src/cdp_scheduler/tests.rs@@ -1120,6 +1120,88 @@ fn concrete_protocol_output_rejects_missing_earlier_publication() {);}+#[tokio::test]+async fn pending_navigation_does_not_suspend_projected_renderer_notifications() {+ let publication = post_load_renderer_publication(PageId::new_for_testing(7), 1);+ let mut conn = CdpConnection::new();+ let navigation = arm_background_navigation_request(&mut conn, "LOADER-next");+ let mut scheduler = CdpScheduler::new(conn);+ let message = json!({"method": "Runtime.consoleAPICalled", "params": {"marker": "current"}});+ scheduler.queues.enqueue_renderer_output_publication(+ publication_cursor(&publication),+ ProtocolOutputSequence::from_messages(vec![message.clone()]),+ Vec::new(),+ Some(future_load_candidate(&publication)),+ );++ assert_eq!(+ scheduler.next_protocol_scheduler_step(),+ ProtocolSchedulerStep::SatisfyClientTurnPredecessor+ );+ scheduler.satisfy_front_protocol_residence_client_turn_predecessor();+ assert_eq!(+ scheduler.next_protocol_scheduler_step(),+ ProtocolSchedulerStep::CompleteReadyResidence+ );+ assert_eq!(+ scheduler+ .complete_next_protocol_residence()+ .await+ .into_messages(),+ vec![message]+ );+ assert!(+ scheduler+ .conn+ .has_inflight_background_navigation_for_target(navigation.target_id())+ );+ assert_eq!(scheduler.queues.protocol_residence_len(), 0);+}++#[tokio::test]+async fn navigation_notification_snapshot_preserves_exact_load_predecessors() {+ let publication = post_load_renderer_publication(PageId::new_for_testing(7), 1);+ let cursor = publication_cursor(&publication);+ let load = deferred_main_document_load_observation_id(1);+ let mut conn = CdpConnection::new();+ let navigation = arm_background_navigation_request(&mut conn, "LOADER-next");+ let mut scheduler = CdpScheduler::new(conn);+ let message = json!({"method": "Runtime.consoleAPICalled", "params": {"marker": "after-load"}});+ scheduler.queues.enqueue_renderer_output_publication(+ cursor,+ ProtocolOutputSequence::from_messages(vec![message.clone()]),+ vec![load],+ None,+ );+ assert_eq!(+ scheduler.next_protocol_scheduler_step(),+ ProtocolSchedulerStep::Wait+ );+ let fence = RendererOutputFence::new_for_test(cursor);+ assert!(+ scheduler+ .complete_renderer_output_predecessor_before_runtime_response(&fence)+ .await+ .is_empty()+ );+ assert_eq!(scheduler.queues.protocol_residence_len(), 1);++ scheduler.queues.satisfy_load_predecessor(load);+ assert_eq!(+ scheduler+ .complete_renderer_output_predecessor_before_runtime_response(&fence)+ .await+ .into_messages(),+ vec![message]+ );+ assert!(+ scheduler+ .conn+ .has_inflight_background_navigation_for_target(navigation.target_id())+ );+ assert_eq!(scheduler.queues.protocol_residence_len(), 0);+}+#[tokio::test]async fn background_navigation_blocks_only_its_target_protocol_residences() {let mut conn = CdpConnection::new();diff --git a/moli-protocol/src/conn/state/devtools_renderer_channel.rs b/moli-protocol/src/conn/state/devtools_renderer_channel.rsindex 1040b3d68..b1cc17afc 100644--- a/moli-protocol/src/conn/state/devtools_renderer_channel.rs+++ b/moli-protocol/src/conn/state/devtools_renderer_channel.rs@@ -356,16 +356,22 @@ impl DevToolsRendererChannel {return Err(DevToolsRendererChannelError::MismatchedAgent);}if self.output_is_suspended() {- let releases_current_prefix = batches- .iter()- .any(RendererRuntimeInspectorMessageBatch::has_renderer_protocol_response);+ let releases_current_prefix = self+ .latest_navigation+ .as_ref()+ .is_none_or(|latest| !latest.attachment_committed)+ || batches+ .iter()+ .any(RendererRuntimeInspectorMessageBatch::has_renderer_protocol_response);self.buffer_output(attachment_id, batches);if releases_current_prefix {- // Main ingress remains suspended, but Chromium's existing- // renderer session pipe can still return IO responses until- // endpoint replacement. Release the whole current-attachment- // prefix so the response cannot overtake notifications that- // preceded it in the same renderer journal.+ // Starting a navigation does not retire the current document.+ // Its Inspector pipe must still deliver asynchronous output+ // while the response is pending, including a load that ends+ // without a replacement document. Candidate output stays+ // suspended across cutover; an IO response can release its+ // exact current-attachment prefix without overtaking earlier+ // notifications in the same renderer journal.return Ok(self.take_buffered_current_output(current));}return Ok(Vec::new());@@ -827,12 +833,11 @@ mod tests {.attach_candidate(&request, new_agent).expect("candidate");- assert!(- channel- .route_current_output(old_attachment.id(), vec![batch(old_agent, "old")])- .expect("route old output")- .is_empty()- );+ let old_output = channel+ .route_current_output(old_attachment.id(), vec![batch(old_agent, "old")])+ .expect("route old output before cutover");+ assert_eq!(old_output.len(), 1);+ assert_eq!(batch_marker(&old_output[0]), Some("old"));assert!(channel.route_candidate_output(&candidate, vec![batch(new_agent, "new")])@@ -842,6 +847,17 @@ mod tests {channel.commit_candidate(candidate).expect("candidate commit");+ assert_eq!(+ channel.route_current_output(old_attachment.id(), vec![batch(old_agent, "stale")]),+ Err(DevToolsRendererChannelError::StaleAttachment)+ );+ let new_attachment = channel.current().expect("new attachment");+ assert!(+ channel+ .route_current_output(new_attachment.id(), vec![batch(new_agent, "committed")])+ .expect("route new output during cutover")+ .is_empty()+ );assert!(channel.navigation_finished(&request)@@ -851,12 +867,13 @@ mod tests {assert_eq!(channel.current().unwrap().agent_token(), new_agent);let released = channel.take_released_output();- assert_eq!(released.len(), 1);+ assert_eq!(released.len(), 2);assert_eq!(batch_marker(&released[0]), Some("new"));+ assert_eq!(batch_marker(&released[1]), Some("committed"));}#[test]- fn failed_navigation_releases_buffered_current_output() {+ fn failed_navigation_does_not_delay_or_repeat_current_output() {let agent = RendererDevToolsAgentToken::allocate();let request = navigation(1);let mut channel = DevToolsRendererChannel::default();@@ -865,12 +882,11 @@ mod tests {channel.navigation_started(request.clone()).expect("navigation start");- assert!(- channel- .route_current_output(attachment.id(), vec![batch(agent, "retained")])- .expect("route output")- .is_empty()- );+ let released = channel+ .route_current_output(attachment.id(), vec![batch(agent, "retained")])+ .expect("route output while the response is pending");+ assert_eq!(released.len(), 1);+ assert_eq!(batch_marker(&released[0]), Some("retained"));assert!(channel@@ -878,21 +894,27 @@ mod tests {.expect("navigation finish"));assert_eq!(channel.current(), Some(attachment));- let released = channel.take_released_output();- assert_eq!(released.len(), 1);- assert_eq!(batch_marker(&released[0]), Some("retained"));+ assert!(channel.take_released_output().is_empty());}#[test]- fn current_session_response_releases_its_buffered_prefix_during_navigation() {+ fn committed_session_response_releases_its_buffered_prefix_during_navigation() {let agent = RendererDevToolsAgentToken::allocate();let request = navigation(1);let mut channel = DevToolsRendererChannel::default();- channel.attach_current(agent).expect("current attach");- let attachment = channel.current().expect("current attachment");+ channel+ .attach_current(RendererDevToolsAgentToken::allocate())+ .expect("initial attach");channel.navigation_started(request.clone()).expect("navigation start");+ let candidate = channel+ .attach_candidate(&request, agent)+ .expect("candidate");+ channel+ .commit_candidate(candidate)+ .expect("candidate commit");+ let attachment = channel.current().expect("committed attachment");assert!(channel@@ -911,6 +933,61 @@ mod tests {assert!(channel.take_released_output().is_empty());}+ #[test]+ fn overlapping_navigation_keeps_current_notifications_live() {+ let agent = RendererDevToolsAgentToken::allocate();+ let first = navigation(1);+ let second = navigation(2);+ let mut channel = DevToolsRendererChannel::default();+ channel.attach_current(agent).expect("initial attach");+ let attachment = channel.current().unwrap();+ channel.navigation_started(first.clone()).unwrap();+ channel.navigation_started(second.clone()).unwrap();+ assert_eq!(channel.navigation_finished(&first), Ok(false));+ assert!(channel.output_is_suspended());++ let output = channel+ .route_current_output(attachment.id(), vec![batch(agent, "still-current")])+ .unwrap();+ assert_eq!(output.len(), 1);+ assert_eq!(batch_marker(&output[0]), Some("still-current"));+ channel.navigation_finished(&second).unwrap();+ assert!(channel.take_released_output().is_empty());+ }++ #[test]+ fn rolled_back_candidate_does_not_suppress_or_mix_current_notifications() {+ let old_agent = RendererDevToolsAgentToken::allocate();+ let new_agent = RendererDevToolsAgentToken::allocate();+ let request = navigation(1);+ let mut channel = DevToolsRendererChannel::default();+ channel.attach_current(old_agent).unwrap();+ let old_attachment = channel.current().unwrap();+ channel.navigation_started(request.clone()).unwrap();+ let candidate = channel.attach_candidate(&request, new_agent).unwrap();+ let transaction = channel.commit_candidate_transaction(candidate).unwrap();+ let new_attachment = channel.current().unwrap();+ assert!(+ channel+ .route_current_output(new_attachment.id(), vec![batch(new_agent, "abandoned")])+ .unwrap()+ .is_empty()+ );+ channel.rollback_committed_candidate(transaction).unwrap();++ let output = channel+ .route_current_output(old_attachment.id(), vec![batch(old_agent, "restored")])+ .unwrap();+ assert_eq!(output.len(), 1);+ assert_eq!(batch_marker(&output[0]), Some("restored"));+ assert_eq!(+ channel.route_current_output(new_attachment.id(), vec![batch(new_agent, "stale")]),+ Err(DevToolsRendererChannelError::StaleAttachment)+ );+ channel.navigation_finished(&request).unwrap();+ assert!(channel.take_released_output().is_empty());+ }+#[test]fn stale_attachment_and_mismatched_agent_are_rejected() {let agent = RendererDevToolsAgentToken::allocate();