Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(history): preserve browser session length across child pushes

ldm0f013d6db751f
diff --git a/moli-protocol/src/conn/browser_context/target_session_owner.rs b/moli-protocol/src/conn/browser_context/target_session_owner.rs
index 7caf312c4..1488e05fe 100644
--- a/moli-protocol/src/conn/browser_context/target_session_owner.rs
+++ b/moli-protocol/src/conn/browser_context/target_session_owner.rs
@@ -180,6 +180,7 @@ pub(crate) struct TargetNavigationLoadInputs {
(bool, Option<moli_core::page::SubresourceResourceType>),
pub(crate) permission_overrides: Vec<moli_core::page::PermissionOverrideRegistration>,
main_document_commit_seed: Option<RendererMainDocumentCommitSeed>,
+ session_history_length: Option<usize>,
}
impl TargetNavigationLoadInputs {
@@ -196,9 +197,11 @@ impl TargetNavigationLoadInputs {
final_url: &Url,
network_error_page: Option<&NetworkErrorPageNavigation>,
) -> Option<RendererMainDocumentCommit> {
- self.main_document_commit_seed
- .as_ref()
- .map(|seed| seed.resolve(final_url, network_error_page))
+ self.main_document_commit_seed.as_ref().map(|seed| {
+ let mut commit = seed.resolve(final_url, network_error_page);
+ commit.session_history_length = self.session_history_length;
+ commit
+ })
}
pub(crate) fn page_storage_handles(&self) -> BrowserContextPageStorageHandles {
@@ -337,6 +340,12 @@ impl TargetNavigationLoadInputs {
.subresource_interception_config(),
permission_overrides: Vec::new(),
main_document_commit_seed: None,
+ session_history_length: Some(
+ target
+ .owner_state
+ .navigation_history_state
+ .length_after_navigation(),
+ ),
}
}
@@ -402,6 +411,7 @@ impl TargetNavigationLoadInputs {
fetch_subresource_interception: (false, None),
permission_overrides: Vec::new(),
main_document_commit_seed: None,
+ session_history_length: None,
}
}
@@ -3280,6 +3290,7 @@ mod tests {
security_origin: "https://nav.example".to_owned(),
secure_context_type: "Secure".to_owned(),
timestamp: 0.0,
+ session_history_length: None,
};
{
let mut owner = TargetSessionOwnerMut {
diff --git a/moli-protocol/src/conn/dispatch_tests.rs b/moli-protocol/src/conn/dispatch_tests.rs
index 7682cb844..059c4aab2 100644
--- a/moli-protocol/src/conn/dispatch_tests.rs
+++ b/moli-protocol/src/conn/dispatch_tests.rs
@@ -2414,6 +2414,7 @@ async fn stale_initial_document_page_build_does_not_overwrite_committed_page() {
security_origin: "null".to_owned(),
secure_context_type: "InsecureScheme".to_owned(),
timestamp: 0.0,
+ session_history_length: None,
};
conn.commit_loaded_navigation_target_identity_for_owner(
&owner,
diff --git a/moli-protocol/src/conn/state/navigation.rs b/moli-protocol/src/conn/state/navigation.rs
index e1d777e19..0574546da 100644
--- a/moli-protocol/src/conn/state/navigation.rs
+++ b/moli-protocol/src/conn/state/navigation.rs
@@ -47,6 +47,21 @@ impl TargetNavigationHistoryState {
self.entries.is_empty()
}
+ pub(crate) fn length_after_navigation(&self) -> usize {
+ match self.pending_update {
+ Some(
+ PendingNavigationHistoryUpdate::ReplaceCurrent
+ | PendingNavigationHistoryUpdate::ReplaceInitialEmptyDocument,
+ ) => self.entries.len().max(1),
+ Some(PendingNavigationHistoryUpdate::TraverseToEntry(entry_id))
+ if self.entries.iter().any(|entry| entry.id == entry_id) =>
+ {
+ self.entries.len()
+ }
+ _ => self.current_index.map_or(1, |index| index + 2),
+ }
+ }
+
pub(crate) fn allocate_entry_id(&mut self) -> i32 {
let id = self.next_entry_id;
self.next_entry_id = self
diff --git a/moli-protocol/src/conn/state/navigation_outcome.rs b/moli-protocol/src/conn/state/navigation_outcome.rs
index 052152fff..2045ee1e5 100644
--- a/moli-protocol/src/conn/state/navigation_outcome.rs
+++ b/moli-protocol/src/conn/state/navigation_outcome.rs
@@ -145,6 +145,7 @@ impl RendererMainDocumentCommitSeed {
security_origin,
secure_context_type,
timestamp: self.timestamp,
+ session_history_length: None,
}
}
}
diff --git a/moli-protocol/src/domains/page/tests/navigation.rs b/moli-protocol/src/domains/page/tests/navigation.rs
index b6fca0752..e5652c5d3 100644
--- a/moli-protocol/src/domains/page/tests/navigation.rs
+++ b/moli-protocol/src/domains/page/tests/navigation.rs
@@ -359,6 +359,103 @@ async fn renderer_fragment_navigation_preserves_initial_document_residence() {
);
}
+#[tokio::test(flavor = "multi_thread")]
+async fn navigation_bootstraps_browser_history_length_before_author_scripts() {
+ async fn page() -> impl axum::response::IntoResponse {
+ (
+ [(axum::http::header::CONTENT_TYPE.as_str(), "text/html")],
+ "<!doctype html><script>globalThis.initialHistoryLength=history.length</script>",
+ )
+ }
+ let listener = TcpListener::bind("127.0.0.1:0")
+ .await
+ .expect("bind history bootstrap server");
+ let addr = listener
+ .local_addr()
+ .expect("history bootstrap server address");
+ let server = tokio::spawn(async move {
+ axum::serve(
+ listener,
+ axum::Router::new().route("/{page}", axum::routing::get(page)),
+ )
+ .await
+ .unwrap();
+ });
+ let mut ctx = TestContext::new();
+ load_bc_with_session(&mut ctx, "BID-1", "TID-1", "SID-1", "about:blank");
+ let first_url = format!("http://{addr}/first");
+ let second_url = format!("http://{addr}/second");
+ let third_url = format!("http://{addr}/third");
+ let mut first_entry_id = None;
+
+ for step in 0..5 {
+ ctx.sent.clear();
+ let (method, params) = match step {
+ 0 => ("Page.navigate", json!({ "url": first_url })),
+ 1 => ("Page.navigate", json!({ "url": second_url })),
+ 2 => ("Page.reload", json!({})),
+ 3 => (
+ "Page.navigateToHistoryEntry",
+ json!({ "entryId": first_entry_id }),
+ ),
+ _ => ("Page.navigate", json!({ "url": third_url })),
+ };
+ ctx.process_async(json!({
+ "id": 9210,
+ "method": method,
+ "sessionId": "SID-1",
+ "params": params
+ }))
+ .await;
+ let response = take_response_by_id(&mut ctx, 9210);
+ assert!(response["error"].is_null(), "{method}: {response}");
+ wait_until_message(
+ &mut ctx,
+ Some("SID-1"),
+ "history bootstrap document DOMContentLoaded",
+ |message| message["method"] == json!("Page.domContentEventFired"),
+ )
+ .await;
+
+ ctx.process_async(json!({
+ "id": 9211,
+ "method": "Page.getNavigationHistory",
+ "sessionId": "SID-1"
+ }))
+ .await;
+ let history = take_response_by_id(&mut ctx, 9211);
+ let entries = history["result"]["entries"]
+ .as_array()
+ .expect("history entries");
+ let expected_length = if step == 0 { 2 } else { 3 };
+ assert_eq!(entries.len(), expected_length, "{method}");
+ if step == 0 {
+ first_entry_id = entries[1]["id"].as_i64();
+ }
+
+ ctx.process_async(json!({
+ "id": 9212,
+ "method": "Runtime.evaluate",
+ "sessionId": "SID-1",
+ "params": {
+ "expression": "({initial:initialHistoryLength, current:history.length})",
+ "returnByValue": true
+ }
+ }))
+ .await;
+ let state = take_response_by_id(&mut ctx, 9212);
+ assert_eq!(
+ state["result"]["result"]["value"],
+ json!({
+ "initial": expected_length,
+ "current": expected_length
+ }),
+ "{method}: {state}"
+ );
+ }
+ server.abort();
+}
+
#[tokio::test(flavor = "multi_thread")]
async fn navigation_history_supports_playwright_back_forward_commands() {
let mut ctx = TestContext::new();
diff --git a/moli-renderer-v8/src/context_bootstrap.rs b/moli-renderer-v8/src/context_bootstrap.rs
index 5cd9a3fce..3c6bf677d 100644
--- a/moli-renderer-v8/src/context_bootstrap.rs
+++ b/moli-renderer-v8/src/context_bootstrap.rs
@@ -324,6 +324,7 @@ pub(crate) use self::navigation_events::dispatch_srcdoc_navigation_navigate_even
pub(crate) use self::navigation_mutation::apply_local_window_location_navigation;
pub(crate) use self::navigation_restore::{
install_navigation_bootstrap_entry, install_navigation_bootstrap_entry_for_holder,
+ install_session_history_length,
};
pub(crate) use self::navigation_traversal::queue_top_level_history_traversal_by_delta;
pub(crate) use self::navigator_runtime::install_worker_navigator_runtime_state;
diff --git a/moli-renderer-v8/src/context_bootstrap/history_mutation.rs b/moli-renderer-v8/src/context_bootstrap/history_mutation.rs
index 289a16518..2dd2c519a 100644
--- a/moli-renderer-v8/src/context_bootstrap/history_mutation.rs
+++ b/moli-renderer-v8/src/context_bootstrap/history_mutation.rs
@@ -16,7 +16,7 @@ use super::navigation_events::{
run_navigation_precommit_deferred_handlers,
};
use super::navigation_lifecycle::finish_navigation_error_events;
-use super::navigation_projection::set_history_length_at_least_visible_entries;
+use super::navigation_projection::set_history_length_after_push;
use super::navigation_result::{
cancel_pending_same_document_navigation_finishes,
cancel_pending_same_document_navigation_finishes_including_reentrant,
@@ -211,7 +211,7 @@ fn mutate_history_object<'s>(
let _ = next_entries.set_index(scope, next_index, entry.into());
set_history_entries(scope, history, next_entries);
set_history_index(scope, history, next_index);
- set_history_length_at_least_visible_entries(scope, history, next_entries);
+ set_history_length_after_push(scope, history, entries, next_entries);
entry
}
HistoryMutationKind::Replace => {
diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_mutation.rs b/moli-renderer-v8/src/context_bootstrap/navigation_mutation.rs
index 3ecf9e8e5..de4c62a85 100644
--- a/moli-renderer-v8/src/context_bootstrap/navigation_mutation.rs
+++ b/moli-renderer-v8/src/context_bootstrap/navigation_mutation.rs
@@ -20,7 +20,7 @@ use super::navigation_events::{
refresh_navigation_destination_indexes,
};
use super::navigation_projection::{
- build_visible_navigation_entries_array, set_history_length_at_least_visible_entries,
+ build_visible_navigation_entries_array, set_history_length_after_push,
set_history_length_from_visible_entries,
};
use super::navigation_serialize::{
diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_mutation/local.rs b/moli-renderer-v8/src/context_bootstrap/navigation_mutation/local.rs
index 91c8ddd0c..97c396021 100644
--- a/moli-renderer-v8/src/context_bootstrap/navigation_mutation/local.rs
+++ b/moli-renderer-v8/src/context_bootstrap/navigation_mutation/local.rs
@@ -44,7 +44,7 @@ pub(crate) fn apply_local_window_location_navigation<'s>(
let _ = next_entries.set_index(scope, next_index, next_entry.into());
set_history_entries(scope, history, next_entries);
set_history_index(scope, history, next_index);
- set_history_length_at_least_visible_entries(scope, history, next_entries);
+ set_history_length_after_push(scope, history, entries, next_entries);
set_history_state(scope, history, state);
set_navigation_current_entry(scope, navigation, next_entry);
dispatch_navigation_currententrychange(scope, navigation, previous_entry, Some("push"));
diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_mutation/same_document.rs b/moli-renderer-v8/src/context_bootstrap/navigation_mutation/same_document.rs
index 524ee9da2..71d60d477 100644
--- a/moli-renderer-v8/src/context_bootstrap/navigation_mutation/same_document.rs
+++ b/moli-renderer-v8/src/context_bootstrap/navigation_mutation/same_document.rs
@@ -62,7 +62,7 @@ pub(in crate::context_bootstrap) fn update_navigation_current_entry_for_same_doc
let _ = next_entries.set_index(scope, next_index, next_entry.into());
set_history_entries(scope, history, next_entries);
set_history_index(scope, history, next_index);
- set_history_length_at_least_visible_entries(scope, history, next_entries);
+ set_history_length_after_push(scope, history, entries, next_entries);
set_history_state(scope, history, history_state);
set_navigation_current_entry(scope, navigation, next_entry);
}
@@ -154,7 +154,7 @@ pub(in crate::context_bootstrap) fn apply_navigation_navigate_same_document<'s>(
let _ = next_entries.set_index(scope, next_index, next_entry.into());
set_history_entries(scope, history, next_entries);
set_history_index(scope, history, next_index);
- set_history_length_at_least_visible_entries(scope, history, next_entries);
+ set_history_length_after_push(scope, history, entries, next_entries);
set_history_state(scope, history, history_state);
set_navigation_current_entry(scope, navigation, next_entry);
if let Some(location) = window_location_for_holder(scope, owner) {
diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_projection.rs b/moli-renderer-v8/src/context_bootstrap/navigation_projection.rs
index 1598fe068..cf856996f 100644
--- a/moli-renderer-v8/src/context_bootstrap/navigation_projection.rs
+++ b/moli-renderer-v8/src/context_bootstrap/navigation_projection.rs
@@ -230,16 +230,23 @@ pub(super) fn set_history_length_from_visible_entries<'s>(
set_top_history_length_at_least(scope, history, length);
}
-pub(super) fn set_history_length_at_least_visible_entries<'s>(
+pub(super) fn set_history_length_after_push<'s>(
scope: &mut v8::PinScope<'s, '_>,
history: v8::Local<'s, v8::Object>,
+ previous_entries: v8::Local<'s, v8::Array>,
entries: v8::Local<'s, v8::Array>,
) {
let length = history_length_floor_from_visible_entries(scope, history, entries);
let current_length = history_length_number(scope, history)
.unwrap_or(0.0)
.max(0.0);
- let length = current_length.max(length);
+ // Navigation entries can omit earlier Documents and other frames' steps.
+ // Add newly appended entries to the existing joint length; replacing a
+ // forward entry after traversal adds no step.
+ let previous_length =
+ history_length_floor_from_visible_entries(scope, history, previous_entries);
+ let added_entries = entries.length().saturating_sub(previous_entries.length());
+ let length = (current_length.max(previous_length) + f64::from(added_entries)).max(length);
set_history_length(scope, history, length);
set_top_history_length_at_least(scope, history, length);
}
diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_restore.rs b/moli-renderer-v8/src/context_bootstrap/navigation_restore.rs
index 0461e1d9a..7ea1f1ac7 100644
--- a/moli-renderer-v8/src/context_bootstrap/navigation_restore.rs
+++ b/moli-renderer-v8/src/context_bootstrap/navigation_restore.rs
@@ -3,8 +3,8 @@ use super::navigation_activation::{
set_navigation_current_entry,
};
use super::navigation_entry::{
- create_navigation_entry, set_history_entries, set_history_index, set_history_state,
- set_navigation_entry_document_id,
+ create_navigation_entry, set_history_entries, set_history_index, set_history_length,
+ set_history_state, set_navigation_entry_document_id,
};
use super::navigation_entry_state::clone_history_entry_state;
use super::navigation_projection::set_history_length_from_visible_entries;
@@ -15,6 +15,13 @@ use moli_page_types::{
NavigationHistoryDocumentId, NavigationHistoryEntryId, NavigationHistoryEntryKey,
};
+pub(crate) fn install_session_history_length(scope: &mut v8::PinScope<'_, '_>, length: usize) {
+ let global = scope.get_current_context().global(scope);
+ if let Some(history) = window_history_for_holder(scope, global) {
+ set_history_length(scope, history, length as f64);
+ }
+}
+
pub(crate) fn install_navigation_bootstrap_entry(
scope: &mut v8::PinScope<'_, '_>,
entry_seed: &NavigationHistoryEntrySeed,
diff --git a/moli-renderer-v8/src/runtime/page_surface.rs b/moli-renderer-v8/src/runtime/page_surface.rs
index 2986ce787..5710a952d 100644
--- a/moli-renderer-v8/src/runtime/page_surface.rs
+++ b/moli-renderer-v8/src/runtime/page_surface.rs
@@ -813,6 +813,8 @@ pub struct RendererMainDocumentCommit {
pub security_origin: String,
pub secure_context_type: String,
pub timestamp: f64,
+ /// Browser-owned session-history length at this document's commit.
+ pub session_history_length: Option<usize>,
}
#[derive(Debug, Clone, Default, PartialEq)]
diff --git a/moli-renderer-v8/src/runtime/page_vm/mod.rs b/moli-renderer-v8/src/runtime/page_vm/mod.rs
index f2f17ba41..f3205e425 100644
--- a/moli-renderer-v8/src/runtime/page_vm/mod.rs
+++ b/moli-renderer-v8/src/runtime/page_vm/mod.rs
@@ -4405,6 +4405,14 @@ impl PageVm {
page_vm
.vm_mut()
.install_navigation_bootstrap_entry(env.navigation_bootstrap_entry.clone());
+ if env.navigation_bootstrap_entry.is_none()
+ && let Some(length) = env
+ .main_document_commit
+ .as_ref()
+ .and_then(|commit| commit.session_history_length)
+ {
+ page_vm.vm_mut().install_session_history_length(length);
+ }
Ok(page_vm)
}
diff --git a/moli-renderer-v8/src/script_vm.rs b/moli-renderer-v8/src/script_vm.rs
index e0830df64..9d852ead3 100644
--- a/moli-renderer-v8/src/script_vm.rs
+++ b/moli-renderer-v8/src/script_vm.rs
@@ -5845,6 +5845,13 @@ impl ScriptVm {
})
}
+ pub(super) fn install_session_history_length(&mut self, length: usize) {
+ let _ = self.with_default_context_scope(|scope, _runtime_ptr| {
+ super::context_bootstrap::install_session_history_length(scope, length);
+ Ok(())
+ });
+ }
+
pub(super) fn install_navigation_bootstrap_entry(
&mut self,
entry_seed: Option<super::native_bridge::NavigationHistoryEntrySeed>,
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
index c310cc9db..36654d105 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
@@ -10352,6 +10352,94 @@ fn no_src_iframe_initial_about_blank_load_is_synchronous_at_connection() {
);
}
+#[tokio::test]
+async fn child_history_push_preserves_existing_top_history_length() {
+ let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");
+ for parent_steps in [0, 1, 3] {
+ let mut vm = new_storage_page_task_executor_test_vm_with_loader(
+ "https://joint-child-length.test/page.html",
+ &loader,
+ );
+ for _ in 0..parent_steps {
+ vm.eval("history.pushState(null, ''); 'pushed'")
+ .expect("parent history should advance");
+ }
+ vm.eval(
+ r#"
+(() => {
+ const frame = document.createElement('iframe');
+ frame.srcdoc = '<p>child</p>';
+ (document.body || document.documentElement || document).appendChild(frame);
+ return 'ready';
+})()
+"#,
+ )
+ .expect("child history frame setup should evaluate");
+ vm.drain_ready_page_task_executor_turns_for_setup(&loader, 128)
+ .await
+ .expect("child should finish loading");
+
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const child = document.querySelector('iframe').contentWindow;
+ const lengths = [];
+ const snapshot = () => lengths.push(history.length, child.history.length);
+ snapshot();
+ child.history.replaceState({ step: 0 }, '');
+ snapshot();
+ child.history.pushState({ step: 1 }, '');
+ snapshot();
+ child.history.pushState({ step: 2 }, '');
+ snapshot();
+ child.history.replaceState({ step: 3 }, '');
+ snapshot();
+ return lengths.concat(child.history.state.step).join('|');
+})()
+"#,
+ )
+ .expect("child history mutations should evaluate");
+ let initial_length = parent_steps + 1;
+ let first_push_length = initial_length + 1;
+ let second_push_length = initial_length + 2;
+ assert_eq!(
+ result,
+ format!(
+ "{initial_length}|{initial_length}|{initial_length}|{initial_length}|\
+ {first_push_length}|{first_push_length}|{second_push_length}|{second_push_length}|\
+ {second_push_length}|{second_push_length}|3"
+ ),
+ "parent history steps: {parent_steps}"
+ );
+
+ vm.eval("document.querySelector('iframe').contentWindow.history.back(); 'queued'")
+ .expect("child history back should queue traversal");
+ vm.drain_ready_page_task_executor_turns_for_setup(&loader, 128)
+ .await
+ .expect("child history traversal should finish");
+ assert_eq!(
+ vm.eval("document.querySelector('iframe').contentWindow.history.state.step")
+ .expect("back should restore the first pushed state"),
+ "1"
+ );
+ assert_eq!(
+ vm.eval(
+ r#"
+(() => {
+ const child = document.querySelector('iframe').contentWindow;
+ child.history.pushState({ step: 4 }, '');
+ return [history.length, child.history.length, child.history.state.step].join('|');
+})()
+"#,
+ )
+ .expect("push after back should replace the forward history step"),
+ format!("{second_push_length}|{second_push_length}|4"),
+ "parent history steps: {parent_steps}"
+ );
+ }
+}
+
#[tokio::test]
async fn top_history_back_routes_to_child_joint_history_entry() {
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("loader");