diff --git a/cli/src/native/actions.rs b/cli/src/native/actions.rs index 02c2174..7ccd491 100644 --- a/cli/src/native/actions.rs +++ b/cli/src/native/actions.rs @@ -3148,21 +3148,31 @@ async fn handle_download(cmd: &Value, state: &mut DaemonState) -> Result { - let is_this_session = event.session_id.as_deref() == Some(&session_id); + // Browser-domain download events may arrive without a sessionId + // or with a different sessionId than the page session, so we + // accept them regardless. Page-domain events are matched by + // session to avoid cross-tab confusion. + let is_page_session = event.session_id.as_deref() == Some(&session_id); + let is_download_event = |method: &str, browser_method: &str, page_method: &str| { + method == browser_method || (method == page_method && is_page_session) + }; + // Capture the GUID from downloadWillBegin - if is_this_session - && (event.method == "Browser.downloadWillBegin" - || event.method == "Page.downloadWillBegin") - { + if is_download_event( + &event.method, + "Browser.downloadWillBegin", + "Page.downloadWillBegin", + ) { if let Some(guid) = event.params.get("guid").and_then(|v| v.as_str()) { downloaded_guid = Some(guid.to_string()); } } // Check for download completion or cancellation - if is_this_session - && (event.method == "Browser.downloadProgress" - || event.method == "Page.downloadProgress") - { + if is_download_event( + &event.method, + "Browser.downloadProgress", + "Page.downloadProgress", + ) { match event.params.get("state").and_then(|v| v.as_str()) { Some("completed") => break, Some("canceled") => { @@ -3182,13 +3192,30 @@ async fn handle_download(cmd: &Value, state: &mut DaemonState) -> Result Result { - if event.method == "Page.downloadProgress" - && event.session_id.as_deref() == Some(&session_id) + // Browser-domain events may arrive without a sessionId; + // Page-domain events are matched by session. + let is_page_session = event.session_id.as_deref() == Some(&session_id); + let is_progress = event.method == "Browser.downloadProgress" + || (event.method == "Page.downloadProgress" && is_page_session); + + if is_progress && event.params.get("state").and_then(|v| v.as_str()) == Some("completed") { let path = cmd