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(protocol): return aborted CDP navigation results
ldm01d33f516f4be
diff --git a/moli-protocol/src/conn.rs b/moli-protocol/src/conn.rsindex 464b0c233..ad6780e1b 100644--- a/moli-protocol/src/conn.rs+++ b/moli-protocol/src/conn.rs@@ -2816,9 +2816,7 @@ impl CdpConnection {let is_current = completion.is_current_for_connection(self);let (token, state, navigation, engine) = completion.into_parts();if !is_current {- if state.navigate_id.is_some() {- out.push_error_after_messages(-32000, "Navigation aborted");- }+ crate::domains::page::push_superseded_navigation_result(out, &state);return;}let navigation_session_id = state.navigate_session_id.clone();diff --git a/moli-protocol/src/conn/tests/mod.rs b/moli-protocol/src/conn/tests/mod.rsindex 4d69da965..086a94ed9 100644--- a/moli-protocol/src/conn/tests/mod.rs+++ b/moli-protocol/src/conn/tests/mod.rs@@ -1189,10 +1189,17 @@ async fn materialized_navigation_completion_drops_stale_token() {assert_eq!(out.len(), 1, "stale completion must emit terminal reply");let reply = &out[0];assert_eq!(reply["id"], serde_json::json!(7));- assert_eq!(reply["error"]["code"], serde_json::json!(-32000));+ assert!(+ reply.get("error").is_none(),+ "CDP reports a superseded Page.navigate as a successful command: {reply:#?}"+ );assert_eq!(- reply["error"]["message"],- serde_json::json!("Navigation aborted")+ reply["result"],+ serde_json::json!({+ "frameId": "TID-nav",+ "errorText": "net::ERR_ABORTED",+ "isDownload": false+ }));assert!(reply.get("method").is_none(),diff --git a/moli-protocol/src/devtools_runtime.rs b/moli-protocol/src/devtools_runtime.rsindex 38b35b47f..b765279f1 100644--- a/moli-protocol/src/devtools_runtime.rs+++ b/moli-protocol/src/devtools_runtime.rs@@ -1419,6 +1419,8 @@ pub struct DevToolsNavigateResult {pub frame_id: Option<DevToolsFrameId>,pub loader_id: Option<DevToolsLoaderId>,pub url: String,+ pub error_text: Option<String>,+ pub is_download: Option<bool>,}#[derive(Debug, Clone, PartialEq, Eq)]diff --git a/moli-protocol/src/domains/command_output.rs b/moli-protocol/src/domains/command_output.rsindex 3068ec47a..728925719 100644--- a/moli-protocol/src/domains/command_output.rs+++ b/moli-protocol/src/domains/command_output.rs@@ -1610,6 +1610,12 @@ fn cdp_result_payload_from_devtools_result(result: DevToolsCommandResult) -> Valif let Some(loader_id) = result.loader_id {payload["loaderId"] = json!(loader_id.into_string());}+ if let Some(error_text) = result.error_text {+ payload["errorText"] = json!(error_text);+ }+ if let Some(is_download) = result.is_download {+ payload["isDownload"] = json!(is_download);+ }payload}DevToolsCommandResult::GetNavigationHistory(result) => json!({@@ -2269,6 +2275,8 @@ mod tests {frame_id: Some(crate::devtools_runtime::DevToolsFrameId::from("FRAME-1")),loader_id: Some(crate::devtools_runtime::DevToolsLoaderId::from("LOADER-1")),url: "https://example.test/".to_owned(),+ error_text: None,+ is_download: None,},),)@@ -2287,6 +2295,37 @@ mod tests {);}+ #[test]+ fn command_output_plan_serializes_aborted_cdp_navigate_as_result() {+ let mut out = Vec::new();+ CommandOutputPlan::from_devtools_result(+ crate::devtools_runtime::DevToolsCommandResult::Navigate(+ crate::devtools_runtime::DevToolsNavigateResult {+ navigation_id: None,+ frame_id: Some(crate::devtools_runtime::DevToolsFrameId::from("FRAME-1")),+ loader_id: None,+ url: "https://superseded.example.test/".to_owned(),+ error_text: Some("net::ERR_ABORTED".to_owned()),+ is_download: Some(false),+ },+ ),+ )+ .emit_into(&mut out, Some(13), Some("SID-1"));++ assert_eq!(+ out,+ vec![json!({+ "id": 13,+ "result": {+ "frameId": "FRAME-1",+ "errorText": "net::ERR_ABORTED",+ "isDownload": false+ },+ "sessionId": "SID-1"+ })]+ );+ }+#[test]fn command_output_plan_serializes_devtools_script_value_result() {let mut out = Vec::new();diff --git a/moli-protocol/src/domains/fetch/navigation.rs b/moli-protocol/src/domains/fetch/navigation.rsindex 911494025..33d2dcd43 100644--- a/moli-protocol/src/domains/fetch/navigation.rs+++ b/moli-protocol/src/domains/fetch/navigation.rs@@ -403,7 +403,7 @@ pub(super) async fn complete_tokened_materialized_navigation_into_buffer_async() {let Some(token) = token else {if navigation_state.navigate_id.is_some() {- out.push_error_after_messages(-32000, "Navigation aborted");+ page::push_superseded_navigation_result(out, &navigation_state);} else {tracing::warn!(session_id = navigation_state.navigate_session_id.as_deref(),diff --git a/moli-protocol/src/domains/page.rs b/moli-protocol/src/domains/page.rsindex 68deff864..46d237509 100644--- a/moli-protocol/src/domains/page.rs+++ b/moli-protocol/src/domains/page.rs@@ -103,7 +103,7 @@ pub(crate) use navigation::emit_prepared_child_frame_tree_background_events;pub(crate) use navigation::navigation_cookie_access_report;pub(crate) use navigation::{MaterializedNavigationCompletion, complete_materialized_navigation_into_buffer_async,- emit_prepared_child_frame_activity,+ emit_prepared_child_frame_activity, push_superseded_navigation_result,};use prepared_navigation::{PagePreparedSameDocumentNavigation, PagePreparedTopLevelLocationNavigation,diff --git a/moli-protocol/src/domains/page/navigation.rs b/moli-protocol/src/domains/page/navigation.rsindex a2feeabfa..e788c3bb3 100644--- a/moli-protocol/src/domains/page/navigation.rs+++ b/moli-protocol/src/domains/page/navigation.rs@@ -15,6 +15,7 @@ use moli_core::page::{SameDocumentHistoryUpdate,};use moli_core::runtime::NavigationEngine;+use moli_fetch::NET_ERR_ABORTED_ERROR_TEXT;use moli_url_policy::{LocalFileNavigationAccess, route_navigation_url};use serde_json::{Value, json};use url::Url;@@ -220,6 +221,8 @@ struct DirectNavigationResult {frame_id: Option<DevToolsFrameId>,loader_id: Option<DevToolsLoaderId>,navigation_id: Option<DevToolsNavigationId>,+ error_text: Option<String>,+ is_download: Option<bool>,}impl DirectNavigationResult {@@ -249,6 +252,8 @@ impl DirectNavigationResult {loader_id.map(DevToolsLoaderId::from)},navigation_id,+ error_text: None,+ is_download: None,}}@@ -260,6 +265,8 @@ impl DirectNavigationResult {frame_id: None,loader_id: None,navigation_id: None,+ error_text: None,+ is_download: None,}}@@ -271,6 +278,8 @@ impl DirectNavigationResult {frame_id: None,loader_id: None,navigation_id: None,+ error_text: None,+ is_download: None,}}@@ -297,6 +306,13 @@ impl DirectNavigationResult {self.url = url;}+ fn set_cdp_navigation_aborted(&mut self, frame_id: &str) {+ self.frame_id = Some(DevToolsFrameId::from(frame_id));+ self.loader_id = None;+ self.error_text = Some(NET_ERR_ABORTED_ERROR_TEXT.to_owned());+ self.is_download = Some(false);+ }+fn ensure_navigation_id_from_loader(&mut self, loader_id: Option<String>) {if self.navigation_id.is_none() {self.navigation_id = loader_id@@ -319,6 +335,8 @@ impl DirectNavigationResult {frame_id: self.frame_id,loader_id: self.loader_id,url: self.url,+ error_text: self.error_text,+ is_download: self.is_download,})}}@@ -791,6 +809,8 @@ fn cdp_navigate_result_payload(frame_id: frame_id.map(DevToolsFrameId::from),loader_id: loader_id.map(DevToolsLoaderId::from),url: url.to_owned(),+ error_text: None,+ is_download: None,},))}@@ -1095,6 +1115,8 @@ fn start_protocol_neutral_navigation_command(frame_id: None,loader_id: None,navigation_id: None,+ error_text: None,+ is_download: None,},};let reloaded_after_crash_session_ids =@@ -1299,6 +1321,10 @@ fn direct_navigation_result_from_completed_load(completed.state.navigate_session_id.as_deref(),&completed.token,) {+ if superseded_cdp_page_navigate_payload(&completed.state).is_some() {+ result.set_cdp_navigation_aborted(&completed.state.frame_id);+ return Ok(());+ }return Err(DevToolsError::new(DevToolsErrorKind::Internal,"Navigation aborted",@@ -1340,6 +1366,10 @@ fn direct_navigation_result_from_fetch_continuation(result: &mut DirectNavigationResult,) -> Result<(), DevToolsError> {if completed.pending.document_navigation_token.is_none() {+ if superseded_cdp_page_navigate_payload(&completed.pending.navigation).is_some() {+ result.set_cdp_navigation_aborted(&completed.pending.navigation.frame_id);+ return Ok(());+ }return Err(DevToolsError::new(DevToolsErrorKind::Internal,"Navigation aborted",@@ -1352,6 +1382,36 @@ fn direct_navigation_result_from_fetch_continuation(Ok(())}+fn superseded_cdp_page_navigate_payload(state: &NavigationDispatchState) -> Option<Value> {+ if state.result_projection.protocol() != DevToolsProtocol::Cdp {+ return None;+ }+ let mut result = state.result_projection.payload().clone();+ let payload = result.as_object_mut()?;+ if !payload.contains_key("frameId") {+ return None;+ }+ payload.insert("frameId".to_owned(), json!(state.frame_id));+ payload.remove("loaderId");+ payload.insert("errorText".to_owned(), json!(NET_ERR_ABORTED_ERROR_TEXT));+ payload.insert("isDownload".to_owned(), json!(false));+ Some(result)+}++pub(crate) fn push_superseded_navigation_result(+ out: &mut CommandOutputBuffer,+ state: &NavigationDispatchState,+) {+ if state.navigate_id.is_none() {+ return;+ }+ if let Some(result) = superseded_cdp_page_navigate_payload(state) {+ out.push_result_after_messages(result);+ } else {+ out.push_error_after_messages(-32000, "Navigation aborted");+ }+}+fn fill_navigation_id_from_current_loader_for_route(conn: &mut CdpConnection,route: &CdpSessionRoute,diff --git a/moli/src/protocol_server/tests/websocket.rs b/moli/src/protocol_server/tests/websocket.rsindex 123d1018d..8ebf244a4 100644--- a/moli/src/protocol_server/tests/websocket.rs+++ b/moli/src/protocol_server/tests/websocket.rs@@ -2086,11 +2086,20 @@ async fn websocket_cdp_replacement_retires_hanging_precommit_navigation() {.iter().find(|message| message["id"] == json!(6_u64)).expect("superseded Page.navigate response");- assert_eq!(superseded_response["error"]["code"], json!(-32000));+ assert!(+ superseded_response.get("error").is_none(),+ "Chromium reports a superseded Page.navigate as a successful command: {superseded_response:#?}"+ );+ assert_eq!(+ superseded_response["result"]["frameId"],+ json!(session.target_id)+ );assert_eq!(- superseded_response["error"]["message"],- json!("Navigation aborted")+ superseded_response["result"]["errorText"],+ json!("net::ERR_ABORTED"));+ assert_eq!(superseded_response["result"]["isDownload"], json!(false));+ assert!(superseded_response["result"].get("loaderId").is_none());let replacement_document = cdp_runtime_evaluate_string(&mut socket,&session.session_id,