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: centralize navigation-safe input completion
ldm01b6e2bf87cb9
diff --git a/moli-core/src/page/input_support.rs b/moli-core/src/page/input_support.rsindex 078a88000..93dd02071 100644--- a/moli-core/src/page/input_support.rs+++ b/moli-core/src/page/input_support.rs@@ -1,32 +1,12 @@use anyhow::Result;use super::{- CompletedPageCommand, Page, PendingPageCommand, RendererDragData, RendererInputDispatchOutcome,- RendererPageCommand, RendererPageReply, RendererPointerEventProperties, RendererTouchPoint,+ CompletedPageCommand, Page, PendingPageCommand, RendererCommandTurnCompletion,+ RendererDragData, RendererInputDispatchOutcome, RendererPageCommand, RendererPageReply,+ RendererPointerEventProperties, RendererTouchPoint,};-impl CompletedPageCommand {- /// Decodes an input reply after the Page that produced it was replaced.- ///- /// Renderer completion is the acknowledgement boundary for an admitted- /// input command. Callers should normally finish through [`PageInputExt`]- /// so the originating Page receives its refreshed state. Once that Page- /// is no longer current, however, its frozen reply remains valid and must- /// not be applied to the replacement Page.- pub fn into_detached_input_dispatch_outcome(self) -> Result<RendererInputDispatchOutcome> {- let (reply, _page_state) = self.into_output().into_reply_and_state();- decode_input_dispatch_outcome(reply, "detached input event page command")- }-- /// Decodes an admitted insert-text reply whose originating Page was- /// replaced before protocol delivery completed.- pub fn into_detached_insert_text_result(self) -> Result<bool> {- let (reply, _page_state) = self.into_output().into_reply_and_state();- decode_insert_text_result(reply)- }-}--fn decode_input_dispatch_outcome(+fn decode_input_dispatch_outcome_reply(reply: RendererPageReply,operation: &str,) -> Result<RendererInputDispatchOutcome> {@@ -38,7 +18,7 @@ fn decode_input_dispatch_outcome()}-fn decode_insert_text_result(reply: RendererPageReply) -> Result<bool> {+fn decode_insert_text_reply(reply: RendererPageReply) -> Result<bool> {expect_page_reply!(reply,"insert text page command",@@ -47,6 +27,30 @@ fn decode_insert_text_result(reply: RendererPageReply) -> Result<bool> {)}+fn settled_input_reply(completion: RendererCommandTurnCompletion) -> Result<RendererPageReply> {+ let (reply, _page_state, post_response_continuation) = completion.into_parts();+ anyhow::ensure!(+ post_response_continuation.is_none(),+ "input completion must consume its renderer turn before reply decoding"+ );+ Ok(reply)+}++/// Decodes a completed renderer turn shared by mouse, touch, drag, and key dispatches.+#[doc(hidden)]+pub fn decode_input_dispatch_outcome_completion(+ completion: RendererCommandTurnCompletion,+ operation: &str,+) -> Result<RendererInputDispatchOutcome> {+ decode_input_dispatch_outcome_reply(settled_input_reply(completion)?, operation)+}++/// Decodes the completed renderer turn returned by an insert-text command.+#[doc(hidden)]+pub fn decode_insert_text_completion(completion: RendererCommandTurnCompletion) -> Result<bool> {+ decode_insert_text_reply(settled_input_reply(completion)?)+}+#[allow(async_fn_in_trait)]pub trait PageInputExt {async fn dispatch_mouse_event_at_point_with_outcome_async(@@ -292,7 +296,7 @@ impl PageInputExt for Page {completion: CompletedPageCommand,) -> Result<RendererInputDispatchOutcome> {let reply = self.finish_page_command(completion);- decode_input_dispatch_outcome(reply, "mouse event page command")+ decode_input_dispatch_outcome_reply(reply, "mouse event page command")}async fn dispatch_touch_event_at_point_with_outcome_async(@@ -348,7 +352,7 @@ impl PageInputExt for Page {completion: CompletedPageCommand,) -> Result<RendererInputDispatchOutcome> {let reply = self.finish_page_command(completion);- decode_input_dispatch_outcome(reply, "touch event page command")+ decode_input_dispatch_outcome_reply(reply, "touch event page command")}async fn dispatch_drag_event_at_point_with_outcome_async(@@ -398,7 +402,7 @@ impl PageInputExt for Page {completion: CompletedPageCommand,) -> Result<RendererInputDispatchOutcome> {let reply = self.finish_page_command(completion);- decode_input_dispatch_outcome(reply, "drag event page command")+ decode_input_dispatch_outcome_reply(reply, "drag event page command")}async fn clear_active_drag_data_transfer_async(&mut self) -> Result<()> {@@ -437,7 +441,7 @@ impl PageInputExt for Page {completion: CompletedPageCommand,) -> Result<bool> {let reply = self.finish_page_command(completion);- decode_insert_text_result(reply)+ decode_insert_text_reply(reply)}async fn dispatch_key_event_with_outcome_async(@@ -489,7 +493,7 @@ impl PageInputExt for Page {completion: CompletedPageCommand,) -> Result<RendererInputDispatchOutcome> {let reply = self.finish_page_command(completion);- decode_input_dispatch_outcome(reply, "key event page command")+ decode_input_dispatch_outcome_reply(reply, "key event page command")}async fn dispatch_key_event_async(diff --git a/moli-core/src/page/mod.rs b/moli-core/src/page/mod.rsindex eaaeb0e38..2fd5628c6 100644--- a/moli-core/src/page/mod.rs+++ b/moli-core/src/page/mod.rs@@ -62,7 +62,9 @@ pub use dom_protocol_support::{MAX_DOM_OUTPUT_TREE_DEPTH, MAX_INSPECTOR_PROTOCOL_VALUE_DEPTH, MAX_JSON_OUTPUT_TREE_DEPTH,RENDERER_BACKEND_NODE_ID_START, is_renderer_backend_node_id,};-pub use input_support::PageInputExt;+pub use input_support::{+ PageInputExt, decode_input_dispatch_outcome_completion, decode_insert_text_completion,+};pub use moli_page_types::DomScrollIntoViewRect;pub use moli_page_types::{RendererDomDebuggerDomBreakpointType, RendererDomDebuggerEventListenerBreakpoint,diff --git a/moli-protocol/src/conn/renderer_command_turn.rs b/moli-protocol/src/conn/renderer_command_turn.rsindex bc820461e..62fe50ab1 100644--- a/moli-protocol/src/conn/renderer_command_turn.rs+++ b/moli-protocol/src/conn/renderer_command_turn.rs@@ -1,6 +1,37 @@-use moli_core::page::{RendererCommandTurnCompletion, RendererCommandTurnOutput};+use moli_core::page::{+ CompletedPageCommand, RendererCommandTurnCompletion, RendererCommandTurnOutput,+};-use super::CommandDispatchContext;+use super::{CdpConnection, CommandDispatchContext, TargetPageResidenceIdentity};++impl CdpConnection {+ /// Settles a renderer command turn against the Page residence that admitted it.+ ///+ /// A completed renderer turn is immutable even if navigation replaces its+ /// originating Page before protocol delivery resumes. Refresh the Page cache+ /// only while that exact residence is still current; otherwise preserve the+ /// output for domain-specific reply handling without installing stale state+ /// into the replacement Page.+ pub(crate) fn settle_page_command_turn_for_owner(+ &mut self,+ session_id: Option<&str>,+ owner: &TargetPageResidenceIdentity,+ completion: CompletedPageCommand,+ ) -> RendererCommandTurnOutput {+ if !self.target_page_residence_identity_is_current_for_session(session_id, owner) {+ return completion.into_output();+ }++ if let Ok(page) = self.loaded_page_mut_for_interruptible_protocol_access(session_id) {+ return page.finish_page_command_turn(completion);+ }++ // The renderer has already settled this immutable result. Losing the+ // protocol-side Page cache must not turn its acknowledgement into an+ // error or apply its state to a future replacement Page.+ completion.into_output()+ }+}impl CommandDispatchContext {/// Captures the exact concrete-stream predecessor and consumes the uniquediff --git a/moli-protocol/src/domains/input.rs b/moli-protocol/src/domains/input.rsindex d534dd876..c5dd0ae1b 100644--- a/moli-protocol/src/domains/input.rs+++ b/moli-protocol/src/domains/input.rs@@ -12,10 +12,11 @@ use crate::devtools_runtime::{};use crate::domains::command_output::CommandOutputPlan;use moli_core::page::{- CompletedPageCommand, Page, PageInputExt, PendingPageCommand, RendererDragData,- RendererDragDataItem, RendererDraggedFile, RendererInputDispatchOutcome,+ CompletedPageCommand, Page, PageInputExt, PendingPageCommand, RendererCommandTurnCompletion,+ RendererDragData, RendererDragDataItem, RendererDraggedFile, RendererInputDispatchOutcome,RendererPendingDownloadActivation, RendererPendingFileChooserActivation,- RendererPointerEventProperties, RendererTouchPoint,+ RendererPointerEventProperties, RendererTouchPoint, decode_input_dispatch_outcome_completion,+ decode_insert_text_completion,};use serde::Deserialize;#[cfg(test)]@@ -164,16 +165,6 @@ impl CompletedInputCommandDispatch {pub(crate) fn session_id(&self) -> Option<&str> {self.session_id.as_deref()}-- fn renderer_output_predecessor(&self) -> Option<moli_core::RendererOutputFence> {- match &self.completed {- CompletedInputOperation::Page(completed) => completed- .as_ref()- .as_ref()- .ok()- .and_then(CompletedPageCommand::renderer_output_predecessor),- }- }}impl PendingInputCommandStartError {@@ -1238,9 +1229,6 @@ async fn complete_pending_input_command(completed: CompletedInputCommandDispatch,command_context: &mut CommandDispatchContext,) -> CompletedInputCommandResult {- if let Some(predecessor) = completed.renderer_output_predecessor() {- command_context.set_renderer_output_predecessor(predecessor);- }let session_id = completed.session_id.as_deref();let owner = completed.owner;let completed_operation = completed.completed;@@ -1261,27 +1249,23 @@ async fn complete_pending_input_command(};}};- let outcome = finish_completed_input_command_for_owner(+ let completion = settle_completed_input_page_command(conn,session_id,&owner,result,- |page, result| match kind {- PendingInputCommandKind::DispatchMouseEvent => {- page.finish_dispatch_mouse_event_at_point_with_outcome(result)- }- PendingInputCommandKind::DispatchTouchEvent- | PendingInputCommandKind::SynthesizeTapGesture => {- page.finish_dispatch_touch_event_at_point_with_outcome(result)- }- PendingInputCommandKind::DispatchDragEvent => {- page.finish_dispatch_drag_event_at_point_with_outcome(result)- }- PendingInputCommandKind::DispatchKeyEvent- | PendingInputCommandKind::InsertText => unreachable!(),- },- CompletedPageCommand::into_detached_input_dispatch_outcome,+ command_context,);+ let operation = match kind {+ PendingInputCommandKind::DispatchMouseEvent => "mouse event page command",+ PendingInputCommandKind::DispatchTouchEvent+ | PendingInputCommandKind::SynthesizeTapGesture => "touch event page command",+ PendingInputCommandKind::DispatchDragEvent => "drag event page command",+ PendingInputCommandKind::DispatchKeyEvent | PendingInputCommandKind::InsertText => {+ unreachable!()+ }+ };+ let outcome = decode_input_dispatch_outcome_completion(completion, operation);match outcome {Ok(outcome) => {if let Err(error) = handle_input_dispatch_outcome_async(@@ -1319,14 +1303,15 @@ async fn complete_pending_input_command(};}};- let outcome = finish_completed_input_command_for_owner(+ let completion = settle_completed_input_page_command(conn,session_id,&owner,result,- Page::finish_dispatch_key_event_with_outcome,- CompletedPageCommand::into_detached_input_dispatch_outcome,+ command_context,);+ let outcome =+ decode_input_dispatch_outcome_completion(completion, "key event page command");match outcome {Ok(outcome) => {if let Err(error) = handle_input_dispatch_outcome_async(@@ -1364,14 +1349,14 @@ async fn complete_pending_input_command(};}};- let result = finish_completed_input_command_for_owner(+ let completion = settle_completed_input_page_command(conn,session_id,&owner,result,- Page::finish_insert_text_into_active_control,- CompletedPageCommand::into_detached_insert_text_result,+ command_context,);+ let result = decode_insert_text_completion(completion);match result {Ok(_) => Ok(DevToolsCommandResult::Empty),Err(error) => Err(DevToolsError::new(@@ -1389,28 +1374,15 @@ async fn complete_pending_input_command(}}-/// Finishes an input command against the exact Page residence that admitted-/// it. Navigation is allowed to suspend that Page while its renderer reply is-/// crossing protocol ingress, so completion bypasses the ordinary navigation-/// access barrier. If navigation has already installed a replacement Page,-/// consume the frozen reply without applying the old Page state to the new-/// residence.-fn finish_completed_input_command_for_owner<T>(+fn settle_completed_input_page_command(conn: &mut CdpConnection,session_id: Option<&str>,owner: &TargetPageResidenceIdentity,completion: CompletedPageCommand,- finish_current: impl FnOnce(&mut Page, CompletedPageCommand) -> anyhow::Result<T>,- finish_detached: impl FnOnce(CompletedPageCommand) -> anyhow::Result<T>,-) -> anyhow::Result<T> {- if !conn.target_page_residence_identity_is_current_for_session(session_id, owner) {- return finish_detached(completion);- }-- let page = conn- .loaded_page_mut_for_interruptible_protocol_access(session_id)- .map_err(anyhow::Error::msg)?;- finish_current(page, completion)+ command_context: &mut CommandDispatchContext,+) -> RendererCommandTurnCompletion {+ let output = conn.settle_page_command_turn_for_owner(session_id, owner, completion);+ command_context.consume_renderer_command_turn_output(output)}pub(crate) async fn complete_pending_input_command_output_plan(diff --git a/moli-protocol/src/domains/input/tests.rs b/moli-protocol/src/domains/input/tests.rsindex bf6750571..12b77be79 100644--- a/moli-protocol/src/domains/input/tests.rs+++ b/moli-protocol/src/domains/input/tests.rs@@ -1,5 +1,5 @@use super::*;-use crate::conn::{BrowserContext, CdpCommandTaskStep};+use crate::conn::{BrowserContext, CdpCommandTaskStep, CommandDispatchContext};use crate::testing::{TestContext, wait_until_frame_stopped_loading};use moli_core::LayoutPolicy;@@ -263,6 +263,64 @@ async fn coordinate_mouse_release_acknowledges_real_link_navigation() {.await;}+#[tokio::test(flavor = "multi_thread")]+async fn completed_mouse_event_does_not_restore_replaced_page_state() {+ let mut ctx = TestContext::new();+ with_loaded_document(&mut ctx, "<body>origin</body>").await;++ let original_owner = ctx+ .conn+ .target_page_residence_identity_for_session(None)+ .expect("the original Page should have a residence identity");+ let pending = loaded_page_mut(&mut ctx.conn, None)+ .expect("the original Page should be loaded")+ .start_dispatch_mouse_event_at_point_with_outcome(+ INPUT_HIT_X.into(),+ INPUT_HIT_Y.into(),+ "mousemove",+ -1,+ None,+ 0.0,+ 0.0,+ )+ .expect("the original Page should admit the mouse event");+ let completed = PendingInputCommandDispatch {+ command_id: Some(104),+ session_id: None,+ owner: original_owner.clone(),+ kind: PendingInputCommandKind::DispatchMouseEvent,+ pending: PendingInputOperation::Page(pending),+ }+ .wait()+ .await;++ let replacement_url = "data:text/html,<body>replacement</body>";+ ctx.install_navigation_fixture_for_session_owner(replacement_url, None)+ .await;+ let replacement_owner = ctx+ .conn+ .target_page_residence_identity_for_session(None)+ .expect("the replacement Page should have a residence identity");+ assert_ne!(original_owner, replacement_owner);++ let completed = complete_pending_input_command(+ &mut ctx.conn,+ completed,+ &mut CommandDispatchContext::default(),+ )+ .await;+ assert!(matches!(completed.result, Ok(DevToolsCommandResult::Empty)));+ assert!(completed.protocol_events.is_empty());+ assert!(+ ctx.conn+ .browser_context+ .as_ref()+ .and_then(|context| context.active_target.runtime_slot.loaded_page())+ .is_some_and(|page| page.final_url().as_str() == replacement_url),+ "settling the original input command must not install its Page state into the replacement"+ );+}+#[tokio::test(flavor = "multi_thread")]async fn coordinate_mouse_event_without_document_still_reports_no_document_loaded() {let mut ctx = TestContext::new();