Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(fetch): preserve typed readiness timeout phases

ldm008d8461c2694
diff --git a/moli-core/src/runtime/mod.rs b/moli-core/src/runtime/mod.rs
index 2bfaa1a40..d1e80f59d 100644
--- a/moli-core/src/runtime/mod.rs
+++ b/moli-core/src/runtime/mod.rs
@@ -671,11 +671,15 @@ impl Browser {
deadline: FetchDeadline,
) -> Result<crate::page::RendererDocumentQuerySelectorNode> {
let loader = self.resource_request_client();
- let remaining = deadline.remaining();
+ // The absolute deadline is the timeout authority. The renderer keeps
+ // its own relative timeout only as a safety bound; giving both layers
+ // the same remaining duration would make their timers race and could
+ // discard the typed fetch phase when the renderer wins.
+ let command_timeout = deadline.timeout();
deadline
.wait(
FetchTimeoutPhase::WaitingForSelector,
- page.wait_for_selector(&loader, selector, remaining),
+ page.wait_for_selector(&loader, selector, command_timeout),
)
.await
}
@@ -699,11 +703,11 @@ impl Browser {
deadline: FetchDeadline,
) -> Result<()> {
let loader = self.resource_request_client();
- let remaining = deadline.remaining();
+ let command_timeout = deadline.timeout();
deadline
.wait(
FetchTimeoutPhase::WaitingForScript,
- page.wait_for_script_truthy(&loader, expression, remaining),
+ page.wait_for_script_truthy(&loader, expression, command_timeout),
)
.await
}
@@ -726,11 +730,11 @@ impl Browser {
deadline: FetchDeadline,
) -> Result<()> {
let loader = self.resource_request_client();
- let remaining = deadline.remaining();
+ let command_timeout = deadline.timeout();
deadline
.wait(
FetchTimeoutPhase::WaitingForSubresourceResponse,
- page.wait_for_subresource_response(&loader, criteria, remaining),
+ page.wait_for_subresource_response(&loader, criteria, command_timeout),
)
.await
}
diff --git a/moli/src/app.rs b/moli/src/app.rs
index 180a0c2dd..9ba8841b2 100644
--- a/moli/src/app.rs
+++ b/moli/src/app.rs
@@ -14,7 +14,8 @@ use anyhow::Result;
use anyhow::{Context, anyhow};
use clap::Parser;
use moli_core::runtime::{
- Browser, FetchedDocument, NavigationRuntimeConfig, storage_partition::StoragePartitionState,
+ Browser, FetchReadinessTimeout, FetchedDocument, NavigationRuntimeConfig,
+ storage_partition::StoragePartitionState,
};
use moli_fetch::{NetworkFetchFailureContext, Request};
use moli_protocol_server::ProtocolServer;
@@ -176,10 +177,13 @@ fn with_fetch_context(error: anyhow::Error, url: &str) -> anyhow::Error {
if error.is::<CliFetchFailureContext>() {
return error;
}
- let reason = error
- .downcast_ref::<NetworkFetchFailureContext>()
- .map(|failure| failure.reason().to_owned())
- .unwrap_or_else(|| error.to_string());
+ let reason = if let Some(failure) = error.downcast_ref::<NetworkFetchFailureContext>() {
+ failure.reason().to_owned()
+ } else if let Some(timeout) = error.downcast_ref::<FetchReadinessTimeout>() {
+ timeout.to_string()
+ } else {
+ error.to_string()
+ };
with_fetch_context_reason(error, url, reason)
}
@@ -239,6 +243,8 @@ fn finalize_fetch_browser(browser: Browser) {
#[cfg(test)]
mod tests {
use super::{with_fetch_context, write_error_report};
+ use moli_core::runtime::{FetchReadinessTimeout, FetchTimeoutPhase};
+ use std::time::Duration;
#[test]
fn fetch_report_has_one_reason_line_without_rendering_the_source_chain() {
@@ -258,6 +264,28 @@ mod tests {
assert!(!report.contains("Caused by:"));
}
+ #[test]
+ fn fetch_report_selects_the_typed_readiness_timeout_through_outer_context() {
+ let error = anyhow::Error::new(FetchReadinessTimeout::new(
+ Duration::from_millis(4000),
+ FetchTimeoutPhase::WaitingForSelector,
+ ))
+ .context("failed while waiting for selector `#target`");
+ let error = with_fetch_context(error, "https://example.test/");
+ let mut report = Vec::new();
+
+ write_error_report(&mut report, &error).expect("report should write");
+ let report = String::from_utf8(report).expect("report should be UTF-8");
+
+ assert_eq!(
+ report,
+ "Error: failed to fetch `https://example.test/`\n\
+ Reason: fetch readiness timed out after 4000 ms while waiting for a selector\n"
+ );
+ assert!(!report.contains("failed while waiting for selector"));
+ assert!(!report.contains("Caused by:"));
+ }
+
#[test]
fn non_fetch_report_retains_the_anyhow_context_chain() {
let error = anyhow::anyhow!("inner cause").context("outer context");
diff --git a/moli/tests/fetch_cli.rs b/moli/tests/fetch_cli.rs
index eb83b4a3d..a2d5756eb 100644
--- a/moli/tests/fetch_cli.rs
+++ b/moli/tests/fetch_cli.rs
@@ -1934,7 +1934,9 @@ fn cli_readiness_plan_does_not_restart_timeout_for_script() -> Result<()> {
let stderr = clean_output(&output.stderr);
assert!(stdout.is_empty(), "stdout={stdout}");
assert!(
- stderr.contains("failed while waiting for script to become truthy"),
+ stderr.contains(
+ "fetch readiness timed out after 700 ms while waiting for a script to become truthy"
+ ),
"the shared deadline should expire during the final plan phase: stderr={stderr}"
);
Ok(())
@@ -1948,15 +1950,15 @@ fn cli_readiness_timeout_identifies_response_selector_and_script_phases() -> Res
let cases: [(&[&str], &str); 3] = [
(
&["--wait-response-url", "/response-that-never-arrives"],
- "failed while waiting for subresource response",
+ "fetch readiness timed out after 350 ms while waiting for a subresource response",
),
(
&["--wait-selector", "#selector-that-never-appears"],
- "failed while waiting for selector `#selector-that-never-appears`",
+ "fetch readiness timed out after 350 ms while waiting for a selector",
),
(
&["--wait-script", "globalThis.scriptThatNeverBecomesTruthy"],
- "failed while waiting for script to become truthy",
+ "fetch readiness timed out after 350 ms while waiting for a script to become truthy",
),
];
@@ -2495,7 +2497,7 @@ fn cli_post_readiness_wait_cannot_restart_an_exhausted_best_effort_deadline() ->
"stderr={stderr}"
);
assert!(
- stderr.contains("failed while waiting for selector `#never-appears`"),
+ stderr.contains("fetch readiness timed out after 1000 ms while waiting for a selector"),
"stderr={stderr}"
);
assert!(
@@ -3027,7 +3029,9 @@ fn cli_http_error_replacement_and_post_waits_share_one_deadline() -> Result<()>
let stderr = clean_output(&output.stderr);
assert!(stdout.is_empty(), "stdout={stdout}");
assert!(
- stderr.contains("failed while waiting for script to become truthy"),
+ stderr.contains(
+ "fetch readiness timed out after 850 ms while waiting for a script to become truthy"
+ ),
"the replacement and earlier post waits should leave the script phase with only the remaining budget: stderr={stderr}"
);
Ok(())
@@ -3331,10 +3335,9 @@ fn cli_slow_streaming_500_and_page_wait_share_one_total_timeout() -> Result<()>
let stderr = clean_output(&output.stderr);
assert!(stdout.is_empty(), "stdout={stdout}");
assert!(
- stderr.contains("failed while waiting for selector"),
+ stderr.contains("fetch readiness timed out after 4000 ms while waiting for a selector"),
"stderr={stderr}"
);
- assert!(stderr.contains("timed out"), "stderr={stderr}");
Ok(())
}