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(renderer): model committed navigation state
ldm0412faf9153b3
diff --git a/moli-renderer-v8/src/runtime/mod.rs b/moli-renderer-v8/src/runtime/mod.rsindex aa52e458e..a3c9c0db8 100644--- a/moli-renderer-v8/src/runtime/mod.rs+++ b/moli-renderer-v8/src/runtime/mod.rs@@ -386,6 +386,10 @@ pub(crate) use self::page_vm::{AuthorizedCurrentPopupDocumentLoadCompletion, AuthorizedLiveMainModulepreloadFetchCompletion,CurrentChildDocumentLoadApplication,};+pub(in crate::runtime) use self::page_vm::{+ PageVmCommittedNavigationBootstrap, PageVmDocumentCommitPreparation,+ PageVmPreparedFollowedNavigationCommit,+};pub use self::phase_one::ExternalRawDocumentBodyStream;use self::phase_one::PendingPhaseOneResidence;pub(in crate::runtime) use self::phase_one::{@@ -593,6 +597,7 @@ pub(in crate::runtime) enum PageVmFollowNavigationTurnOutcome {outcome: page_vm::DocumentLifecycleTurnOutcome,},Download(RendererPendingDownloadActivation),+ #[cfg(test)]PendingPhaseOne(PageVmPendingPhaseOneNavigation),TriggeredNavigation {stage: PageVmInitStage,diff --git a/moli-renderer-v8/src/runtime/owner.rs b/moli-renderer-v8/src/runtime/owner.rsindex 993eafae7..54531301e 100644--- a/moli-renderer-v8/src/runtime/owner.rs+++ b/moli-renderer-v8/src/runtime/owner.rs@@ -5,7 +5,8 @@ use super::navigation::{};use super::owner_local::RendererAttachedPage;use super::owner_local_store::{- LivePageEntry, LivePageEntryCheckoutError, LivePageNavigationFailureRecipient,+ CommittedNavigationEntry, LivePageEntry, LivePageEntryCheckoutError,+ LivePageNavigationFailureRecipient, LivePageNavigationFollowEntryAdvance,LivePageNavigationFollowOutcome, LivePageNavigationFollowTurn,LivePagePendingNavigationCompletion, LivePagePendingNavigationPhaseOneAdvance,NavigationReplyPolicy, PendingPhaseOneEntryAdvance, RendererDisplacedOrdinaryTurn,@@ -383,6 +384,54 @@ impl std::fmt::Display for LivePageNavigationFailureDisposition {}}+impl LivePageNavigationFailureDisposition {+ fn publish_page_creation_failure(+ &self,+ token: RendererPageToken,+ ) -> Option<Result<PageCreationNavigationFailurePublication>> {+ match self {+ Self::PublishToPageCreation(failure) => Some(+ publish_page_navigation_failure_on_bound_owner_local_store(token, *failure),+ ),+ Self::ReturnToInitiator(_) | Self::ReportBackground(_) => None,+ }+ }++ fn into_dispatch_outcome(+ self,+ token: RendererPageToken,+ page_creation_publication: Option<Result<PageCreationNavigationFailurePublication>>,+ ) -> RenderRuntimeDispatchOutcome {+ match self {+ Self::PublishToPageCreation(failure) => {+ match page_creation_publication+ .expect("Page-creation failure disposition must publish before restoration")+ {+ Ok(PageCreationNavigationFailurePublication::Recorded) => {+ RenderRuntimeDispatchOutcome::PageCreationNavigationFailurePublished {+ token,+ failure,+ }+ }+ Ok(PageCreationNavigationFailurePublication::AlreadyRecorded) => {+ RenderRuntimeDispatchOutcome::BackgroundComplete(Ok(()))+ }+ Ok(PageCreationNavigationFailurePublication::NoActiveCreationObserver) => {+ RenderRuntimeDispatchOutcome::BackgroundComplete(Err(anyhow!(+ "unobserved background navigation failure: {failure}"+ )))+ }+ Err(error) => RenderRuntimeDispatchOutcome::BackgroundComplete(Err(error)),+ }+ }+ Self::ReturnToInitiator(error) => Err(error).into(),+ Self::ReportBackground(failure) => {+ RenderRuntimeDispatchOutcome::BackgroundComplete(Err(anyhow!(failure.to_string())))+ }+ }+ }+}+pub(super) struct RenderRuntimePendingTurn {/// `None` means the pending turn is detached/background work (originated/// from an idle-tick) and no caller is waiting on a reply.@@ -3248,6 +3297,29 @@ impl RendererOwnerHandle {Err(error).into()}+ fn finish_committed_navigation_failure(+ &self,+ token: RendererPageToken,+ mut entry: CommittedNavigationEntry,+ disposition: LivePageNavigationFailureDisposition,+ ) -> RenderRuntimeDispatchOutcome {+ // Page creation owns the strong failure observer while the stable Page+ // slot owns only its weak publisher. Publish before marking the slot+ // retiring so the parked creation turn observes this exact failure.+ let page_creation_publication = disposition.publish_page_creation_failure(token);+ tracing::warn!(+ page_id = token.page_id.as_u64(),+ failure = %disposition,+ "retiring page after committed navigation failed to bootstrap"+ );+ let cleanup_failure = disposition.to_string();+ entry.settle_standalone_navigation_follow(false);+ remove_page_on_bound_owner_local_store(token);+ let entry = entry.reject_and_retire(&cleanup_failure);+ restore_retiring_entry_after_command_on_bound_owner_local_store(token, entry);+ disposition.into_dispatch_outcome(token, page_creation_publication)+ }+async fn finish_live_page_navigation_failure(&self,token: RendererPageToken,@@ -3256,28 +3328,24 @@ impl RendererOwnerHandle {disposition: LivePageNavigationFailureDisposition,) -> RenderRuntimeDispatchOutcome {let had_uncommitted_page_vm = entry.has_uncommitted_page_vm();- let navigation_committed = entry- .vm- .as_ref()- .is_none_or(|page_vm| !page_vm.has_live_script_vm());- let should_retire_page = navigation_committed || retire_page_on_failure;+ debug_assert!(+ entry+ .active_page_vm()+ .is_some_and(PageVm::has_live_script_vm),+ "live navigation failure handling requires an active PageVm"+ );+ let should_retire_page = retire_page_on_failure;// Page creation owns the strong failure observer while the stable Page// slot owns only its weak publisher. Publish before requesting Page- // retirement: restoring a checked-out committed entry tears down that- // slot, but the parked creation turn must retain the concrete terminal- // and be woken instead of falling through to its generic timeout.- let page_creation_publication = match &disposition {- LivePageNavigationFailureDisposition::PublishToPageCreation(failure) => Some(- publish_page_navigation_failure_on_bound_owner_local_store(token, *failure),- ),- LivePageNavigationFailureDisposition::ReturnToInitiator(_)- | LivePageNavigationFailureDisposition::ReportBackground(_) => None,- };+ // retirement: restoring a checked-out entry into a retiring slot tears+ // it down, but the parked creation turn must retain the concrete+ // terminal and be woken instead of falling through to its timeout.+ let page_creation_publication = disposition.publish_page_creation_failure(token);if should_retire_page {tracing::warn!(page_id = token.page_id.as_u64(),failure = %disposition,- "retiring page after committed navigation failed to bootstrap"+ "retiring page after live navigation failure");// Mark the stable slot retiring while this turn still owns the// entry. Restoring then tears down the detached shell without ever@@ -3296,41 +3364,9 @@ impl RendererOwnerHandle {);remove_page_on_bound_owner_local_store(token);}- if navigation_committed {- let mut entry = entry.into_retiring_after_committed_navigation();- entry.settle_standalone_navigation_follow(false);- restore_retiring_entry_after_command_on_bound_owner_local_store(token, entry);- } else {- entry.settle_standalone_navigation_follow(false);- self.restore_live_page_entry(token, entry);- }- match disposition {- LivePageNavigationFailureDisposition::PublishToPageCreation(failure) => {- match page_creation_publication- .expect("Page-creation failure disposition must publish before retirement")- {- Ok(PageCreationNavigationFailurePublication::Recorded) => {- RenderRuntimeDispatchOutcome::PageCreationNavigationFailurePublished {- token,- failure,- }- }- Ok(PageCreationNavigationFailurePublication::AlreadyRecorded) => {- RenderRuntimeDispatchOutcome::BackgroundComplete(Ok(()))- }- Ok(PageCreationNavigationFailurePublication::NoActiveCreationObserver) => {- RenderRuntimeDispatchOutcome::BackgroundComplete(Err(anyhow!(- "unobserved background navigation failure: {failure}"- )))- }- Err(error) => RenderRuntimeDispatchOutcome::BackgroundComplete(Err(error)),- }- }- LivePageNavigationFailureDisposition::ReturnToInitiator(error) => Err(error).into(),- LivePageNavigationFailureDisposition::ReportBackground(failure) => {- RenderRuntimeDispatchOutcome::BackgroundComplete(Err(anyhow!(failure.to_string())))- }- }+ entry.settle_standalone_navigation_follow(false);+ self.restore_live_page_entry(token, entry);+ disposition.into_dispatch_outcome(token, page_creation_publication)}/// Compute the earliest Page-owned task deadline. This combines JavaScript@@ -5822,13 +5858,22 @@ impl RendererOwnerHandle {).await;}- let (entry, follow_result) =- follow_pending_location_navigation_one_turn_on_entry_via_local_task(- self.state.local_executor.clone(),- entry,- stage,- )- .await;+ let advance = follow_pending_location_navigation_one_turn_on_entry_via_local_task(+ self.state.local_executor.clone(),+ entry,+ stage,+ )+ .await;+ let (entry, follow_result) = match advance {+ LivePageNavigationFollowEntryAdvance::Live { entry, result } => (entry, result),+ LivePageNavigationFollowEntryAdvance::Committed { entry, error } => {+ return self.finish_committed_navigation_failure(+ token,+ entry,+ LivePageNavigationFailureDisposition::ReturnToInitiator(error),+ );+ }+ };let LivePageNavigationFollowTurn {outcome: follow_outcome,document_commit,diff --git a/moli-renderer-v8/src/runtime/owner_local_store/bound.rs b/moli-renderer-v8/src/runtime/owner_local_store/bound.rsindex 8276b42f5..e32cd0e2f 100644--- a/moli-renderer-v8/src/runtime/owner_local_store/bound.rs+++ b/moli-renderer-v8/src/runtime/owner_local_store/bound.rs@@ -218,14 +218,15 @@ impl<E, R> Drop for EntryLocalTaskGuard<E, R> {}}-pub(in crate::runtime) async fn run_entry_on_bound_owner_local_store_local_task<R, F>(+async fn run_typed_entry_on_bound_owner_local_store_local_task<E, R, F>(local_executor: JsLocalExecutor,- entry: LivePageEntry,+ entry: E,operation: F,-) -> (LivePageEntry, Result<R>)+) -> (E, Result<R>)where+ E: 'static,R: 'static,- F: for<'a> FnOnce(&'a mut LivePageEntry) -> EntryLocalTaskFuture<'a, R> + 'static,+ F: for<'a> FnOnce(&'a mut E) -> EntryLocalTaskFuture<'a, R> + 'static,{let (reply_tx, reply_rx) = oneshot::channel();// Construct the guard before spawning. If the local task is cancelled@@ -244,6 +245,18 @@ where.expect("entry local task guard must return the page entry on task termination")}+pub(in crate::runtime) async fn run_entry_on_bound_owner_local_store_local_task<R, F>(+ local_executor: JsLocalExecutor,+ entry: LivePageEntry,+ operation: F,+) -> (LivePageEntry, Result<R>)+where+ R: 'static,+ F: for<'a> FnOnce(&'a mut LivePageEntry) -> EntryLocalTaskFuture<'a, R> + 'static,+{+ run_typed_entry_on_bound_owner_local_store_local_task(local_executor, entry, operation).await+}+pub(in crate::runtime) fn take_entry_for_command_on_bound_owner_local_store(token: RendererPageToken,) -> Result<LivePageEntry> {@@ -609,56 +622,164 @@ pub(in crate::runtime) async fn advance_subresource_response_wait_turn_on_entry_.await}+enum NavigationLocalTaskEntry {+ Live(LivePageEntry),+ Committed(CommittedNavigationEntry),+ Transitioning,+}++impl NavigationLocalTaskEntry {+ fn live_mut(&mut self) -> &mut LivePageEntry {+ match self {+ Self::Live(entry) => entry,+ Self::Committed(_) | Self::Transitioning => {+ unreachable!("navigation task must be live at this boundary")+ }+ }+ }++ fn committed_mut(&mut self) -> &mut CommittedNavigationEntry {+ match self {+ Self::Committed(entry) => entry,+ Self::Live(_) | Self::Transitioning => {+ unreachable!("navigation task must be committed at this boundary")+ }+ }+ }++ fn commit(&mut self, prepared: PageVmPreparedFollowedNavigationCommit) -> Result<()> {+ let navigation = self.live_mut().commit_prepared_navigation(prepared)?;+ let Self::Live(entry) = std::mem::replace(self, Self::Transitioning) else {+ unreachable!("navigation commit must consume a live entry")+ };+ *self = Self::Committed(CommittedNavigationEntry::new(entry, navigation));+ Ok(())+ }++ fn finish_commit(&mut self) {+ let Self::Committed(entry) = std::mem::replace(self, Self::Transitioning) else {+ unreachable!("replacement bootstrap must complete a committed entry")+ };+ *self = Self::Live(entry.into_live());+ }++ async fn bootstrap_committed_navigation(&mut self) -> Result<LivePageNavigationFollowOutcome> {+ let bootstrap_outcome = self.committed_mut().bootstrap_replacement().await?;+ let completion = self+ .committed_mut()+ .install_bootstrap_outcome(bootstrap_outcome)?;+ self.finish_commit();+ match completion {+ CommittedNavigationBootstrapCompletion::ContinuePostParseLifecycle {+ page_tasks,+ stage,+ started,+ } => {+ let lifecycle = {+ let (page_vm, pending_document_lifecycle_turn) =+ self.live_mut().page_vm_and_document_lifecycle_turn_mut();+ page_vm+ .begin_post_parse_lifecycle_on_named_owner_lane(+ pending_document_lifecycle_turn,+ page_tasks,+ stage,+ started,+ )+ .await?+ };+ Ok(LivePageNavigationFollowOutcome::PostParseLifecycle {+ target_stage: stage,+ outcome: lifecycle,+ })+ }+ CommittedNavigationBootstrapCompletion::PendingPhaseOne { wake_token } => {+ Ok(LivePageNavigationFollowOutcome::PendingPhaseOne { wake_token })+ }+ CommittedNavigationBootstrapCompletion::TriggeredNavigation { stage } => {+ Ok(LivePageNavigationFollowOutcome::TriggeredNavigation { stage })+ }+ }+ }+}+pub(in crate::runtime) async fn follow_pending_location_navigation_one_turn_on_entry_via_local_task(local_executor: JsLocalExecutor,entry: LivePageEntry,stage: PageVmInitStage,-) -> (LivePageEntry, Result<LivePageNavigationFollowTurn>) {- run_entry_on_bound_owner_local_store_local_task(local_executor, entry, move |entry| {- Box::pin(async move {- let outcome = {- let (page_vm, pending_document_lifecycle_turn) =- entry.page_vm_and_document_lifecycle_turn_mut();- page_vm- .prepare_pending_location_navigation_document_commit_one_turn_async(- pending_document_lifecycle_turn,- stage,- )- .await- };- let outcome = match outcome? {- PageVmFollowNavigationTurnOutcome::Completed => {- LivePageNavigationFollowOutcome::Completed- }- PageVmFollowNavigationTurnOutcome::PostParseLifecycle {- target_stage,- outcome,- } => LivePageNavigationFollowOutcome::PostParseLifecycle {- target_stage,+) -> LivePageNavigationFollowEntryAdvance {+ let (entry, result) = run_typed_entry_on_bound_owner_local_store_local_task(+ local_executor,+ NavigationLocalTaskEntry::Live(entry),+ move |entry| {+ Box::pin(async move {+ let preparation = {+ let (page_vm, pending_document_lifecycle_turn) =+ entry.live_mut().page_vm_and_document_lifecycle_turn_mut();+ page_vm+ .prepare_pending_location_navigation_document_commit_one_turn_async(+ pending_document_lifecycle_turn,+ stage,+ )+ .await+ };+ let outcome = match preparation? {+ PageVmDocumentCommitPreparation::Prepared(prepared) => {+ entry.commit(*prepared)?;+ entry.bootstrap_committed_navigation().await?+ }+ PageVmDocumentCommitPreparation::Uncommitted(outcome) => match *outcome {+ PageVmFollowNavigationTurnOutcome::Completed => {+ LivePageNavigationFollowOutcome::Completed+ }+ PageVmFollowNavigationTurnOutcome::PostParseLifecycle {+ target_stage,+ outcome,+ } => LivePageNavigationFollowOutcome::PostParseLifecycle {+ target_stage,+ outcome,+ },+ PageVmFollowNavigationTurnOutcome::Download(download) => {+ LivePageNavigationFollowOutcome::Download(download)+ }+ #[cfg(test)]+ PageVmFollowNavigationTurnOutcome::PendingPhaseOne(pending) => {+ let wake_token = entry+ .live_mut()+ .install_new_pending_phase_one_navigation(pending)?;+ LivePageNavigationFollowOutcome::PendingPhaseOne { wake_token }+ }+ PageVmFollowNavigationTurnOutcome::TriggeredNavigation { stage } => {+ LivePageNavigationFollowOutcome::TriggeredNavigation { stage }+ }+ },+ };+ let document_commit = if entry.live_mut().has_uncommitted_page_vm() {+ Some(entry.live_mut().publish_replacement_document_commit()?)+ } else {+ None+ };+ Ok(LivePageNavigationFollowTurn {outcome,- },- PageVmFollowNavigationTurnOutcome::Download(download) => {- LivePageNavigationFollowOutcome::Download(download)- }- PageVmFollowNavigationTurnOutcome::PendingPhaseOne(pending) => {- let wake_token = entry.install_new_pending_phase_one_navigation(pending)?;- LivePageNavigationFollowOutcome::PendingPhaseOne { wake_token }- }- PageVmFollowNavigationTurnOutcome::TriggeredNavigation { stage } => {- LivePageNavigationFollowOutcome::TriggeredNavigation { stage }- }- };- let document_commit = entry- .has_uncommitted_page_vm()- .then(|| entry.publish_replacement_document_commit())- .transpose()?;- Ok(LivePageNavigationFollowTurn {- outcome,- document_commit,+ document_commit,+ })})- })- })- .await+ },+ )+ .await;+ match entry {+ NavigationLocalTaskEntry::Live(entry) => {+ LivePageNavigationFollowEntryAdvance::Live { entry, result }+ }+ NavigationLocalTaskEntry::Committed(entry) => {+ let error = result.err().unwrap_or_else(|| {+ anyhow!("committed navigation task completed without a replacement PageVm")+ });+ LivePageNavigationFollowEntryAdvance::Committed { entry, error }+ }+ NavigationLocalTaskEntry::Transitioning => {+ unreachable!("navigation task cannot return while changing typestate")+ }+ }}pub(in crate::runtime) fn remove_page_on_bound_owner_local_store(token: RendererPageToken) {diff --git a/moli-renderer-v8/src/runtime/owner_local_store/entry.rs b/moli-renderer-v8/src/runtime/owner_local_store/entry.rsindex fce59db56..27b57e985 100644--- a/moli-renderer-v8/src/runtime/owner_local_store/entry.rs+++ b/moli-renderer-v8/src/runtime/owner_local_store/entry.rs@@ -75,6 +75,31 @@ pub(in crate::runtime) struct LivePageEntry {pub(super) last_published_replacement_document: Option<PublishedReplacementDocument>,}+/// A checked-out Page whose source Document has committed away while its+/// replacement `PageVm` is still being bootstrapped.+///+/// This state never enters the stable Page scheduler. It exists only inside+/// the navigation local-task guard, so cancellation and panic return a typed+/// committed residence instead of an invalid [`LivePageEntry`].+pub(in crate::runtime) struct CommittedNavigationEntry {+ entry: LivePageEntry,+ navigation: Box<PageVmCommittedNavigationBootstrap>,+}++pub(in crate::runtime) enum CommittedNavigationBootstrapCompletion {+ ContinuePostParseLifecycle {+ page_tasks: Vec<crate::page_task_queue::PostParsePageOwnedWork>,+ stage: PageVmInitStage,+ started: Instant,+ },+ PendingPhaseOne {+ wake_token: RendererPageToken,+ },+ TriggeredNavigation {+ stage: PageVmInitStage,+ },+}+/// A checked-out Page entry whose final active `PageVm` has been consumed by/// teardown and which can therefore only return through the retiring/// residence boundary.@@ -86,6 +111,95 @@ pub(in crate::runtime) struct RetiringPageEntry {pub(super) entry: LivePageEntry,}+impl CommittedNavigationEntry {+ pub(super) fn new(+ mut entry: LivePageEntry,+ navigation: PageVmCommittedNavigationBootstrap,+ ) -> Self {+ entry.vm = None;+ Self {+ entry,+ navigation: Box::new(navigation),+ }+ }++ pub(in crate::runtime) async fn bootstrap_replacement(+ &mut self,+ ) -> Result<PageVmFollowedNavigationBuildOutcome> {+ self.navigation.bootstrap().await+ }++ pub(in crate::runtime) fn install_bootstrap_outcome(+ &mut self,+ outcome: PageVmFollowedNavigationBuildOutcome,+ ) -> Result<CommittedNavigationBootstrapCompletion> {+ let outcome = self.navigation.finalize_build_outcome(outcome)?;+ match outcome {+ PageVmFollowedNavigationBuildOutcome::ContinuePostParseLifecycle {+ page_vm,+ page_tasks,+ stage,+ started,+ } => {+ self.entry.install_resumed_phase_one_page_vm(page_vm);+ Ok(+ CommittedNavigationBootstrapCompletion::ContinuePostParseLifecycle {+ page_tasks,+ stage,+ started,+ },+ )+ }+ PageVmFollowedNavigationBuildOutcome::PendingPhaseOne(pending) => {+ let wake_token = self+ .entry+ .install_new_pending_phase_one_navigation(pending)?;+ Ok(CommittedNavigationBootstrapCompletion::PendingPhaseOne { wake_token })+ }+ PageVmFollowedNavigationBuildOutcome::TriggeredNavigation { page_vm, stage } => {+ self.entry.install_resumed_phase_one_page_vm(page_vm);+ Ok(CommittedNavigationBootstrapCompletion::TriggeredNavigation { stage })+ }+ PageVmFollowedNavigationBuildOutcome::Download(_) => unreachable!(+ "committed bootstrap finalization must reject outcomes without a replacement PageVm"+ ),+ }+ }++ pub(in crate::runtime) fn into_live(self) -> LivePageEntry {+ assert!(+ self.entry+ .active_page_vm()+ .is_some_and(PageVm::has_live_script_vm),+ "a completed committed navigation must produce a live replacement PageVm"+ );+ self.entry+ }++ pub(in crate::runtime) fn reject_and_retire(mut self, failure: &str) -> RetiringPageEntry {+ self.navigation.reject(failure);+ // Replacement installation and the Committed -> Live tag change are+ // synchronous, but a panic inside that narrow transition must still+ // retire any partially installed replacement instead of double+ // panicking in the task-guard cleanup path.+ if self.entry.active_page_vm().is_some() {+ tracing::error!(+ failure,+ "retiring a replacement PageVm left inside a committed navigation transition"+ );+ self.entry.close_for_context_teardown();+ self.entry.vm = None;+ }+ RetiringPageEntry::new(self.entry)+ }++ pub(in crate::runtime) fn settle_standalone_navigation_follow(&mut self, succeeded: bool) {+ self.entry+ .standalone_navigation_follow+ .settle(None, succeeded);+ }+}+impl RetiringPageEntry {pub(super) fn new(entry: LivePageEntry) -> Self {assert!(@@ -234,24 +348,21 @@ impl LivePageEntry {self.standalone_navigation_follow.settle(current, succeeded);}- /// Consume the inert PageVm shell left after a cross-document commit whose- /// replacement failed to bootstrap. The old ScriptVm is already gone, so- /// this entry can only return through the teardown-only residence path.- pub(in crate::runtime) fn into_retiring_after_committed_navigation(- mut self,- ) -> RetiringPageEntry {+ /// Commit the source Document synchronously. The navigation task guard+ /// must immediately move this entry into `CommittedNavigationEntry`+ /// before crossing another await boundary.+ pub(in crate::runtime) fn commit_prepared_navigation(+ &mut self,+ prepared: PageVmPreparedFollowedNavigationCommit,+ ) -> Result<PageVmCommittedNavigationBootstrap> {assert!(self.pending_phase_one_navigation.is_none(),- "a committed navigation shell cannot coexist with pending phase one"+ "a source navigation commit cannot coexist with pending phase one");- assert!(- self.vm- .as_ref()- .is_none_or(|page_vm| !page_vm.has_live_script_vm()),- "a committed navigation shell must not retain a live ScriptVm"- );- self.vm = None;- RetiringPageEntry::new(self)+ let navigation = self+ .page_vm_mut()+ .commit_prepared_followed_location_navigation(prepared)?;+ Ok(navigation)}/// A replacement PageVm becomes the active owner-local runtime before itsdiff --git a/moli-renderer-v8/src/runtime/owner_local_store/mod.rs b/moli-renderer-v8/src/runtime/owner_local_store/mod.rsindex b51b1b8ed..1ac11ccdd 100644--- a/moli-renderer-v8/src/runtime/owner_local_store/mod.rs+++ b/moli-renderer-v8/src/runtime/owner_local_store/mod.rs@@ -56,7 +56,8 @@ pub(in crate::runtime) use bound::*;#[cfg(test)]use entry::StandaloneNavigationFollowState;pub(in crate::runtime) use entry::{- LivePageEntry, PublishedReplacementDocument, RetiringPageEntry,+ CommittedNavigationBootstrapCompletion, CommittedNavigationEntry, LivePageEntry,+ PublishedReplacementDocument, RetiringPageEntry,};pub(in crate::runtime) use phase_one::{LivePagePendingNavigationPhaseOneAdvance, PendingPhaseOneEntryAdvance,@@ -433,6 +434,20 @@ pub(super) struct LivePageNavigationFollowTurn {pub(super) document_commit: Option<PublishedReplacementDocument>,}+/// Typed result of one checked-out navigation task. A task that ends before+/// commit returns a live entry; a task that fails, panics, or is cancelled+/// during replacement bootstrap returns the committed state explicitly.+pub(super) enum LivePageNavigationFollowEntryAdvance {+ Live {+ entry: LivePageEntry,+ result: Result<LivePageNavigationFollowTurn>,+ },+ Committed {+ entry: CommittedNavigationEntry,+ error: anyhow::Error,+ },+}+pub(super) struct RendererOwnerLocalContext {pub(super) owner_state: Arc<RendererOwnerState>,pub(super) render_runtime: crate::render_runtime::RenderRuntimeHandle,diff --git a/moli-renderer-v8/src/runtime/owner_local_store/tests.rs b/moli-renderer-v8/src/runtime/owner_local_store/tests.rsindex ba1903eda..f0b704684 100644--- a/moli-renderer-v8/src/runtime/owner_local_store/tests.rs+++ b/moli-renderer-v8/src/runtime/owner_local_store/tests.rs@@ -112,23 +112,20 @@ mod navigation_dispatch_tests {}#[test]- fn committed_navigation_transitions_to_retiring_before_settlement() {+ fn committed_navigation_settlement_does_not_require_a_live_vm() {let handoff = crate::page_task_queue::RendererTopLevelNavigationHandoff::new(1);for succeeded in [false, true] {- for state in [+ for mut state in [StandaloneNavigationFollowState::Idle,StandaloneNavigationFollowState::Following { handoff },StandaloneNavigationFollowState::FailedWithPendingNavigation { handoff },] {- let entry = phase_one_entry_shell_without_active_page_vm(state);- let mut entry = entry.into_retiring_after_committed_navigation();-- entry.settle_standalone_navigation_follow(succeeded);+ state.settle(None, succeeded);assert_eq!(- entry.entry.standalone_navigation_follow,+ state,StandaloneNavigationFollowState::Idle,- "a committed navigation shell must settle {state:?} to Idle after succeeded={succeeded}"+ "a committed navigation must settle to Idle after succeeded={succeeded}");}}diff --git a/moli-renderer-v8/src/runtime/page.rs b/moli-renderer-v8/src/runtime/page.rsindex 854a6253c..95c69d7e0 100644--- a/moli-renderer-v8/src/runtime/page.rs+++ b/moli-renderer-v8/src/runtime/page.rs@@ -1136,7 +1136,7 @@ pub(super) enum FollowedLocationNavigationBootstrapBoundary {#[cfg(test)]impl LoadedFollowedLocationNavigation {- fn fail_after_commit_for_test(&self) -> bool {+ fn has_header_for_test(&self, expected_name: &str) -> bool {let headers = match self {Self::NoDocument | Self::Download(_) => return false,Self::StreamingDocument { response, .. } => &response.headers,@@ -1144,9 +1144,9 @@ impl LoadedFollowedLocationNavigation {response_headers, ..} => response_headers,};- headers.iter().any(|(name, value)| {- name.eq_ignore_ascii_case("x-moli-test-fail-after-navigation-commit") && value == "1"- })+ headers+ .iter()+ .any(|(name, value)| name.eq_ignore_ascii_case(expected_name) && value == "1")}}@@ -1253,7 +1253,11 @@ pub(super) async fn bootstrap_committed_followed_location_navigation("committed location-navigation bootstrap must execute on the matching named owner lane");#[cfg(test)]- if loaded.fail_after_commit_for_test() {+ if loaded.has_header_for_test("x-moli-test-panic-after-navigation-commit") {+ panic!("injected panic after main navigation commit for testing");+ }+ #[cfg(test)]+ if loaded.has_header_for_test("x-moli-test-fail-after-navigation-commit") {return Err(anyhow!("injected failure after main navigation commit for testing"));diff --git a/moli-renderer-v8/src/runtime/page_vm/mod.rs b/moli-renderer-v8/src/runtime/page_vm/mod.rsindex 7ae3524e0..c61a492fe 100644--- a/moli-renderer-v8/src/runtime/page_vm/mod.rs+++ b/moli-renderer-v8/src/runtime/page_vm/mod.rs@@ -1708,6 +1708,120 @@ fn mark_followed_navigation_document_commit(}}+pub(in crate::runtime) enum PageVmDocumentCommitPreparation {+ Uncommitted(Box<PageVmFollowNavigationTurnOutcome>),+ Prepared(Box<PageVmPreparedFollowedNavigationCommit>),+}++pub(in crate::runtime) struct PageVmPreparedFollowedNavigationCommit {+ initiator_url: Url,+ navigation_handoff: crate::page_task_queue::RendererTopLevelNavigationHandoff,+ loaded: LoadedFollowedLocationNavigation,+ navigation_bootstrap_entry: Option<crate::native_bridge::NavigationHistoryEntrySeed>,+ reserved_service_worker_client_id: Option<crate::service_worker_runtime::ServiceWorkerClientId>,+ service_worker_client_navigate: Option<crate::types::ServiceWorkerClientNavigateContinuation>,+ stage: PageVmInitStage,+}++struct PageVmCommittedNavigationBootstrapPayload {+ page_id: PageId,+ local_executor: JsLocalExecutor,+ request_client: ResourceRequestClient,+ env: PageVmEnvConfig,+ runtime_hooks: PageVmRuntimeHooks,+ navigation_bootstrap_entry: Option<crate::native_bridge::NavigationHistoryEntrySeed>,+ loaded: LoadedFollowedLocationNavigation,+ stage: PageVmInitStage,+}++/// Owns all continuation state after the source Document has committed away+/// and until a replacement `PageVm` has been produced.+///+/// Failure metadata intentionally remains outside the consumed bootstrap+/// payload. If the local task is cancelled while awaiting the replacement,+/// the checked-out committed entry can still reject the ServiceWorker follow+/// and retire the Page without consulting the dead source `ScriptVm`.+pub(in crate::runtime) struct PageVmCommittedNavigationBootstrap {+ payload: Option<PageVmCommittedNavigationBootstrapPayload>,+ browser_context_runtime: RendererBrowserContextRuntime,+ initiator_url: Option<Url>,+ navigation_handoff: crate::page_task_queue::RendererTopLevelNavigationHandoff,+ reserved_service_worker_client_id: Option<crate::service_worker_runtime::ServiceWorkerClientId>,+ service_worker_client_navigate: Option<crate::types::ServiceWorkerClientNavigateContinuation>,+}++impl PageVmCommittedNavigationBootstrap {+ pub(in crate::runtime) async fn bootstrap(+ &mut self,+ ) -> Result<PageVmFollowedNavigationBuildOutcome> {+ let payload = self.payload.take().ok_or_else(|| {+ anyhow!("committed location-navigation bootstrap was already consumed")+ })?;+ bootstrap_committed_followed_location_navigation(+ payload.page_id,+ payload.local_executor,+ payload.request_client,+ payload.env,+ payload.runtime_hooks,+ payload.navigation_bootstrap_entry,+ self.reserved_service_worker_client_id,+ payload.stage,+ payload.loaded,+ crate::runtime::page::FollowedLocationNavigationBootstrapBoundary::DocumentCommit,+ )+ .await+ }++ pub(in crate::runtime) fn finalize_build_outcome(+ &mut self,+ mut outcome: PageVmFollowedNavigationBuildOutcome,+ ) -> Result<PageVmFollowedNavigationBuildOutcome> {+ mark_followed_navigation_document_commit(&mut outcome, self.navigation_handoff)?;+ match &mut outcome {+ PageVmFollowedNavigationBuildOutcome::ContinuePostParseLifecycle {+ page_vm, ..+ }+ | PageVmFollowedNavigationBuildOutcome::TriggeredNavigation { page_vm, .. } => {+ if let Some(continuation) = self.service_worker_client_navigate.take() {+ page_vm+ .vm_mut()+ .complete_pending_service_worker_client_navigate_after_follow(continuation);+ }+ self.reserved_service_worker_client_id = None;+ self.initiator_url = None;+ }+ PageVmFollowedNavigationBuildOutcome::PendingPhaseOne(pending) => {+ pending.metadata.service_worker_client_navigate =+ self.service_worker_client_navigate.take();+ pending.metadata.abort_reserved_service_worker_client_id =+ self.reserved_service_worker_client_id.take();+ pending.metadata.abort_navigation_initiator_url = self.initiator_url.take();+ }+ PageVmFollowedNavigationBuildOutcome::Download(download) => {+ return Err(anyhow!(+ "committed location-navigation bootstrap produced a download for {} without a replacement PageVm",+ download.url+ ));+ }+ }+ Ok(outcome)+ }++ pub(in crate::runtime) fn reject(&mut self, failure: &str) {+ let mut metadata = PageVmFollowedNavigationMetadata {+ service_worker_client_navigate: self.service_worker_client_navigate.take(),+ abort_reserved_service_worker_client_id: self.reserved_service_worker_client_id.take(),+ ..PageVmFollowedNavigationMetadata::default()+ };+ metadata.reject(+ None,+ &self.browser_context_runtime,+ format!("Cannot navigate to URL: {failure}"),+ );+ self.initiator_url = None;+ }+}+impl PageVm {pub(super) async fn run_ready_document_write_stylesheet_blocked_script(&mut self,@@ -2385,35 +2499,45 @@ impl PageVm {pending_document_lifecycle_turn: &mut Option<PendingDocumentLifecycleTurn>,stage: PageVmInitStage,) -> Result<PageVmFollowNavigationTurnOutcome> {- self.follow_pending_location_navigation_one_turn_at_boundary_async(- pending_document_lifecycle_turn,- stage,- crate::runtime::page::FollowedLocationNavigationBootstrapBoundary::ContinuePhaseOne,- )- .await+ match self+ .prepare_pending_location_navigation_one_turn_async(+ pending_document_lifecycle_turn,+ stage,+ )+ .await?+ {+ PageVmDocumentCommitPreparation::Uncommitted(outcome) => Ok(*outcome),+ PageVmDocumentCommitPreparation::Prepared(prepared) => {+ self.finish_prepared_followed_location_navigation_for_test(+ pending_document_lifecycle_turn,+ *prepared,+ )+ .await+ }+ }}pub(super) async fn prepare_pending_location_navigation_document_commit_one_turn_async(&mut self,pending_document_lifecycle_turn: &mut Option<PendingDocumentLifecycleTurn>,stage: PageVmInitStage,- ) -> Result<PageVmFollowNavigationTurnOutcome> {- self.follow_pending_location_navigation_one_turn_at_boundary_async(+ ) -> Result<PageVmDocumentCommitPreparation> {+ self.prepare_pending_location_navigation_one_turn_async(pending_document_lifecycle_turn,stage,- crate::runtime::page::FollowedLocationNavigationBootstrapBoundary::DocumentCommit,).await}- async fn follow_pending_location_navigation_one_turn_at_boundary_async(+ async fn prepare_pending_location_navigation_one_turn_async(&mut self,pending_document_lifecycle_turn: &mut Option<PendingDocumentLifecycleTurn>,stage: PageVmInitStage,- bootstrap_boundary: crate::runtime::page::FollowedLocationNavigationBootstrapBoundary,- ) -> Result<PageVmFollowNavigationTurnOutcome> {+ ) -> Result<PageVmDocumentCommitPreparation> {let Some(pending) = self.vm_mut().take_pending_location_navigation_with_seed() else {- return Ok(PageVmFollowNavigationTurnOutcome::Completed);+ return Ok(PageVmDocumentCommitPreparation::Uncommitted(Box::new(+ PageVmFollowNavigationTurnOutcome::Completed,+ )));};let initiator_url = self.vm().document_runtime.document_url().clone();let navigation_handoff = pending.handoff;@@ -2469,6 +2593,7 @@ impl PageVm {| Ok(PageVmFollowNavigationTurnOutcome::TriggeredNavigation { .. }) => self.vm_mut().complete_pending_service_worker_client_navigate_after_follow(continuation),+ #[cfg(test)]Ok(PageVmFollowNavigationTurnOutcome::PendingPhaseOne(_)) => {unreachable!("javascript: location navigation cannot park in asynchronous phase-one creation"@@ -2482,7 +2607,8 @@ impl PageVm {),}}- return outcome;+ return outcome+ .map(|outcome| PageVmDocumentCommitPreparation::Uncommitted(Box::new(outcome)));}let loaded = match load_followed_location_navigation(@@ -2514,7 +2640,9 @@ impl PageVm {reserved_service_worker_client_id,service_worker_client_navigate,);- return Ok(PageVmFollowNavigationTurnOutcome::Completed);+ return Ok(PageVmDocumentCommitPreparation::Uncommitted(Box::new(+ PageVmFollowNavigationTurnOutcome::Completed,+ )));}LoadedFollowedLocationNavigation::Download(download) => {self.abort_followed_navigation_without_document(@@ -2522,7 +2650,9 @@ impl PageVm {reserved_service_worker_client_id,service_worker_client_navigate,);- return Ok(PageVmFollowNavigationTurnOutcome::Download(download));+ return Ok(PageVmDocumentCommitPreparation::Uncommitted(Box::new(+ PageVmFollowNavigationTurnOutcome::Download(download),+ )));}loaded @ (LoadedFollowedLocationNavigation::StreamingDocument { .. }| LoadedFollowedLocationNavigation::ExternalDocument { .. }) => loaded,@@ -2543,13 +2673,41 @@ impl PageVm {);*pending_document_lifecycle_turn = None;- let mut outcome = match self+ Ok(PageVmDocumentCommitPreparation::Prepared(Box::new(+ PageVmPreparedFollowedNavigationCommit {+ initiator_url,+ navigation_handoff,+ loaded,+ navigation_bootstrap_entry: pending.entry_seed,+ reserved_service_worker_client_id,+ service_worker_client_navigate,+ stage,+ },+ )))+ }++ #[cfg(test)]+ async fn finish_prepared_followed_location_navigation_for_test(+ &mut self,+ pending_document_lifecycle_turn: &mut Option<PendingDocumentLifecycleTurn>,+ prepared: PageVmPreparedFollowedNavigationCommit,+ ) -> Result<PageVmFollowNavigationTurnOutcome> {+ let PageVmPreparedFollowedNavigationCommit {+ initiator_url,+ loaded,+ navigation_bootstrap_entry,+ reserved_service_worker_client_id,+ service_worker_client_navigate,+ stage,+ ..+ } = prepared;+ let outcome = match self.bootstrap_followed_location_navigation(loaded,- pending.entry_seed,+ navigation_bootstrap_entry,reserved_service_worker_client_id,stage,- bootstrap_boundary,+ crate::runtime::page::FollowedLocationNavigationBootstrapBoundary::ContinuePhaseOne,).await{@@ -2564,12 +2722,6 @@ impl PageVm {return Err(error);}};- if matches!(- bootstrap_boundary,- crate::runtime::page::FollowedLocationNavigationBootstrapBoundary::DocumentCommit- ) {- mark_followed_navigation_document_commit(&mut outcome, navigation_handoff)?;- }Ok(match outcome {PageVmFollowedNavigationBuildOutcome::ContinuePostParseLifecycle {page_vm,@@ -2631,6 +2783,60 @@ impl PageVm {})}+ pub(super) fn commit_prepared_followed_location_navigation(+ &mut self,+ prepared: PageVmPreparedFollowedNavigationCommit,+ ) -> Result<PageVmCommittedNavigationBootstrap> {+ let PageVmPreparedFollowedNavigationCommit {+ initiator_url,+ navigation_handoff,+ loaded,+ navigation_bootstrap_entry,+ reserved_service_worker_client_id,+ service_worker_client_navigate,+ stage,+ } = prepared;+ let env = self.followed_location_navigation_env();+ let runtime_hooks = self.runtime_hooks.clone().for_cross_document_commit();+ let browser_context_runtime = runtime_hooks.browser_context_runtime.clone();+ let local_executor = self.local_executor.clone();+ let request_client = self.request_client.clone();+ let page_id = self.page_id;+ let commit_result = (|| {+ ensure!(+ runtime_hooks.has_renderer_page_script_environment(),+ "owner-managed navigation commit requires a renderer Page script environment"+ );+ self.commit_main_window_proxy_navigation()+ })();+ if let Err(error) = commit_result {+ self.reject_failed_followed_location_navigation(+ &initiator_url,+ reserved_service_worker_client_id,+ service_worker_client_navigate,+ &error,+ );+ return Err(error);+ }+ Ok(PageVmCommittedNavigationBootstrap {+ payload: Some(PageVmCommittedNavigationBootstrapPayload {+ page_id,+ local_executor,+ request_client,+ env,+ runtime_hooks,+ navigation_bootstrap_entry,+ loaded,+ stage,+ }),+ browser_context_runtime,+ initiator_url: Some(initiator_url),+ navigation_handoff,+ reserved_service_worker_client_id,+ service_worker_client_navigate,+ })+ }+fn prepare_replacement_document_commit(&mut self,handoff: crate::page_task_queue::RendererTopLevelNavigationHandoff,@@ -2762,6 +2968,7 @@ impl PageVm {| Ok(PageVmFollowNavigationTurnOutcome::TriggeredNavigation { .. }) => self.vm_mut().complete_pending_service_worker_client_navigate_after_follow(continuation),+ #[cfg(test)]Ok(PageVmFollowNavigationTurnOutcome::PendingPhaseOne(_)) => {unreachable!("javascript: location navigation cannot park in asynchronous phase-one creation"@@ -2815,26 +3022,8 @@ impl PageVm {}}- async fn bootstrap_followed_location_navigation(- &mut self,- loaded: LoadedFollowedLocationNavigation,- navigation_bootstrap_entry: Option<crate::native_bridge::NavigationHistoryEntrySeed>,- reserved_service_worker_client_id: Option<- crate::service_worker_runtime::ServiceWorkerClientId,- >,- stage: PageVmInitStage,- boundary: crate::runtime::page::FollowedLocationNavigationBootstrapBoundary,- ) -> Result<PageVmFollowedNavigationBuildOutcome> {- debug_assert!(- is_on_named_owner_execution_lane_for(&self.local_executor),- "async followed location-navigation rebuild must execute on the matching named owner lane"- );- debug_assert!(matches!(- &loaded,- LoadedFollowedLocationNavigation::StreamingDocument { .. }- | LoadedFollowedLocationNavigation::ExternalDocument { .. }- ));- let env = PageVmEnvConfig {+ fn followed_location_navigation_env(&self) -> PageVmEnvConfig {+ PageVmEnvConfig {main_document_commit: None,web_storage: self.vm().web_storage_handles(),document_start_scripts: self.document_start_scripts.clone(),@@ -2876,7 +3065,30 @@ impl PageVm {top_level_storage_key: None,navigation_bootstrap_entry: None,reserved_service_worker_client_id: None,- };+ }+ }++ #[cfg(test)]+ async fn bootstrap_followed_location_navigation(+ &mut self,+ loaded: LoadedFollowedLocationNavigation,+ navigation_bootstrap_entry: Option<crate::native_bridge::NavigationHistoryEntrySeed>,+ reserved_service_worker_client_id: Option<+ crate::service_worker_runtime::ServiceWorkerClientId,+ >,+ stage: PageVmInitStage,+ boundary: crate::runtime::page::FollowedLocationNavigationBootstrapBoundary,+ ) -> Result<PageVmFollowedNavigationBuildOutcome> {+ debug_assert!(+ is_on_named_owner_execution_lane_for(&self.local_executor),+ "async followed location-navigation rebuild must execute on the matching named owner lane"+ );+ debug_assert!(matches!(+ &loaded,+ LoadedFollowedLocationNavigation::StreamingDocument { .. }+ | LoadedFollowedLocationNavigation::ExternalDocument { .. }+ ));+ let env = self.followed_location_navigation_env();let runtime_hooks = self.runtime_hooks.clone().for_cross_document_commit();if runtime_hooks.has_renderer_page_script_environment() {self.commit_main_window_proxy_navigation()?;@@ -4175,6 +4387,7 @@ impl PageVm {)}PageVmFollowNavigationTurnOutcome::Download(_) => true,+ #[cfg(test)]PageVmFollowNavigationTurnOutcome::PendingPhaseOne(_) => {unreachable!("javascript: location navigation cannot park in asynchronous phase-one creation"@@ -4309,6 +4522,7 @@ impl PageVm {)}PageVmFollowNavigationTurnOutcome::Download(_) => true,+ #[cfg(test)]PageVmFollowNavigationTurnOutcome::PendingPhaseOne(_) => {unreachable!("javascript: location navigation cannot park in asynchronous phase-one creation"diff --git a/moli-renderer-v8/src/runtime/tests.rs b/moli-renderer-v8/src/runtime/tests.rsindex d92650d1b..9dc0737eb 100644--- a/moli-renderer-v8/src/runtime/tests.rs+++ b/moli-renderer-v8/src/runtime/tests.rs@@ -3090,8 +3090,10 @@ document.close();.expect("document.open navigation identity test page should close");}-#[tokio::test(flavor = "current_thread")]-async fn committed_navigation_bootstrap_failure_retires_page_before_follow_settlement() {+async fn assert_committed_navigation_bootstrap_injection_retires_page(+ injection_header: &'static str,+ expected_failure: &str,+) {let runtime = JsRuntime::initialize();let loader =ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("default loader");@@ -3099,7 +3101,7 @@ async fn committed_navigation_bootstrap_failure_retires_page_before_follow_settl"/replacement","<!doctype html><main>replacement</main>","text/html",- vec![("x-moli-test-fail-after-navigation-commit", "1")],+ vec![(injection_header, "1")],Duration::ZERO,).await;@@ -3121,11 +3123,11 @@ async fn committed_navigation_bootstrap_failure_retires_page_before_follow_settl);let result = pending.await_ready().await;let Err(error) = result else {- panic!("the injected post-commit bootstrap failure must reject page creation")+ panic!("the injected post-commit bootstrap termination must reject page creation")};let failure = format!("{error:#}");assert!(- failure.contains("injected failure after main navigation commit for testing"),+ failure.contains(expected_failure),"page creation must report the injected post-commit failure: {failure}");server@@ -3151,6 +3153,24 @@ async fn committed_navigation_bootstrap_failure_retires_page_before_follow_settlassert_eq!(runtime.renderer_page_count_for_testing(), 0);}+#[tokio::test(flavor = "current_thread")]+async fn committed_navigation_bootstrap_failure_retires_page_before_follow_settlement() {+ assert_committed_navigation_bootstrap_injection_retires_page(+ "x-moli-test-fail-after-navigation-commit",+ "injected failure after main navigation commit for testing",+ )+ .await;+}++#[tokio::test(flavor = "current_thread")]+async fn committed_navigation_bootstrap_panic_returns_committed_entry_to_owner() {+ assert_committed_navigation_bootstrap_injection_retires_page(+ "x-moli-test-panic-after-navigation-commit",+ "local task panicked before restoring its page entry",+ )+ .await;+}+#[tokio::test(flavor = "current_thread")]async fn external_raw_streaming_delegates_post_load_meta_refresh_to_browser() {let runtime = JsRuntime::initialize();