Skip to content

armgit / moli

Public Imported from GitHub
0

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

refactor(protocol): simplify renderer navigation state

ldm094ac141e7c00
diff --git a/moli-protocol/src/conn/state/devtools_renderer_channel.rs b/moli-protocol/src/conn/state/devtools_renderer_channel.rs
index 8dfee565f..b1daf3c91 100644
--- a/moli-protocol/src/conn/state/devtools_renderer_channel.rs
+++ b/moli-protocol/src/conn/state/devtools_renderer_channel.rs
@@ -91,27 +91,14 @@ pub(crate) struct DevToolsRendererChannel {
lifecycle: DevToolsRendererChannelLifecycle,
current: Option<RendererAgentAttachment>,
inflight_cross_document_navigations: HashSet<DocumentNavigationToken>,
- suspended_attachment: Option<RendererAgentAttachment>,
- latest_started_navigation: Option<DocumentNavigationToken>,
- committed_latest_navigation: Option<DocumentNavigationToken>,
+ latest_navigation: Option<LatestChannelNavigation>,
buffered_output: Vec<BufferedRendererInspectorBatch>,
}
-#[derive(Clone, Copy, Debug, Eq, PartialEq)]
-pub(crate) struct RendererChannelResume {
- suspended_attachment: Option<RendererAgentAttachment>,
- current_attachment: Option<RendererAgentAttachment>,
-}
-
-impl RendererChannelResume {
- #[cfg(test)]
- pub(crate) fn replacement(
- self,
- ) -> Option<(RendererAgentAttachmentId, RendererAgentAttachmentId)> {
- let suspended = self.suspended_attachment?;
- let current = self.current_attachment?;
- (suspended.id() != current.id()).then_some((suspended.id(), current.id()))
- }
+#[derive(Debug)]
+struct LatestChannelNavigation {
+ token: DocumentNavigationToken,
+ attachment_committed: bool,
}
#[derive(Debug)]
@@ -140,18 +127,16 @@ impl DevToolsRendererChannel {
navigation: DocumentNavigationToken,
) -> Result<(), DevToolsRendererChannelError> {
self.ensure_open()?;
- let was_suspended = self.output_is_suspended();
if !self
.inflight_cross_document_navigations
.insert(navigation.clone())
{
return Err(DevToolsRendererChannelError::DuplicateNavigation);
}
- if !was_suspended {
- self.suspended_attachment = self.current;
- }
- self.latest_started_navigation = Some(navigation);
- self.committed_latest_navigation = None;
+ self.latest_navigation = Some(LatestChannelNavigation {
+ token: navigation,
+ attachment_committed: false,
+ });
Ok(())
}
@@ -192,15 +177,17 @@ impl DevToolsRendererChannel {
{
return Err(DevToolsRendererChannelError::UnknownNavigation);
}
- if self.latest_started_navigation.as_ref() != Some(candidate.navigation()) {
- return Err(DevToolsRendererChannelError::SupersededNavigation);
- }
- if self.committed_latest_navigation.as_ref() == Some(candidate.navigation()) {
+ let latest = self
+ .latest_navigation
+ .as_mut()
+ .filter(|latest| &latest.token == candidate.navigation())
+ .ok_or(DevToolsRendererChannelError::SupersededNavigation)?;
+ if latest.attachment_committed {
return Err(DevToolsRendererChannelError::NavigationAlreadyCommitted);
}
self.inflight_cross_document_navigations
.retain(|navigation| navigation == candidate.navigation());
- self.committed_latest_navigation = Some(candidate.navigation.clone());
+ latest.attachment_committed = true;
let current = candidate.attachment;
let previous = self.current.replace(current);
Ok(CommittedRendererAgentAttachment {
@@ -215,13 +202,18 @@ impl DevToolsRendererChannel {
transaction: CommittedRendererAgentAttachment,
) -> Result<(), DevToolsRendererChannelError> {
self.ensure_open()?;
- if self.committed_latest_navigation.as_ref() != Some(transaction.navigation())
- || self.current != Some(transaction.current())
- {
+ let latest = self
+ .latest_navigation
+ .as_mut()
+ .filter(|latest| {
+ latest.attachment_committed && &latest.token == transaction.navigation()
+ })
+ .ok_or(DevToolsRendererChannelError::CommittedCandidateMismatch)?;
+ if self.current != Some(transaction.current()) {
return Err(DevToolsRendererChannelError::CommittedCandidateMismatch);
}
self.current = transaction.previous;
- self.committed_latest_navigation = None;
+ latest.attachment_committed = false;
Ok(())
}
@@ -253,7 +245,9 @@ impl DevToolsRendererChannel {
{
return Err(DevToolsRendererChannelError::UnknownNavigation);
}
- if self.latest_started_navigation.as_ref() != Some(candidate.navigation()) {
+ if self.latest_navigation.as_ref().map(|latest| &latest.token)
+ != Some(candidate.navigation())
+ {
return Err(DevToolsRendererChannelError::SupersededNavigation);
}
if batches
@@ -266,20 +260,14 @@ impl DevToolsRendererChannel {
Ok(Vec::new())
}
+ /// Returns whether this completion made the channel usable again.
pub(crate) fn navigation_finished(
&mut self,
navigation: &DocumentNavigationToken,
- ) -> Result<Option<RendererChannelResume>, DevToolsRendererChannelError> {
+ ) -> Result<bool, DevToolsRendererChannelError> {
self.ensure_open()?;
- if !self.inflight_cross_document_navigations.remove(navigation)
- || self.output_is_suspended()
- {
- return Ok(None);
- }
- Ok(Some(RendererChannelResume {
- suspended_attachment: self.suspended_attachment.take(),
- current_attachment: self.current,
- }))
+ Ok(self.inflight_cross_document_navigations.remove(navigation)
+ && !self.output_is_suspended())
}
pub(crate) fn output_is_suspended(&self) -> bool {
@@ -320,9 +308,7 @@ impl DevToolsRendererChannel {
}
self.lifecycle = DevToolsRendererChannelLifecycle::Closed(reason);
self.inflight_cross_document_navigations.clear();
- self.suspended_attachment = None;
- self.latest_started_navigation = None;
- self.committed_latest_navigation = None;
+ self.latest_navigation = None;
self.buffered_output.clear();
self.current.take()
}
@@ -550,8 +536,7 @@ mod tests {
assert!(
channel
.navigation_finished(&request)
- .expect("navigation finish")
- .is_some(),
+ .expect("navigation finish"),
"a failed load finishes without committing its candidate"
);
@@ -605,9 +590,8 @@ mod tests {
channel
.navigation_finished(&request_b)
.expect("committed navigation finish")
- .is_some()
);
- assert_eq!(channel.navigation_finished(&request_a), Ok(None));
+ assert_eq!(channel.navigation_finished(&request_a), Ok(false));
}
#[test]
@@ -637,8 +621,12 @@ mod tests {
.rollback_committed_candidate(transaction)
.expect("matching transaction should roll back");
assert_eq!(channel.current(), Some(initial));
- assert_eq!(
- channel.committed_latest_navigation, None,
+ assert!(
+ !channel
+ .latest_navigation
+ .as_ref()
+ .unwrap()
+ .attachment_committed,
"a rolled-back candidate is no longer committed"
);
assert!(
@@ -649,7 +637,6 @@ mod tests {
channel
.navigation_finished(&request)
.expect("rolled-back navigation finish")
- .is_some()
);
}
@@ -668,13 +655,12 @@ mod tests {
assert_eq!(channel.inflight_navigation_count(), 2);
assert!(channel.output_is_suspended());
- assert_eq!(channel.navigation_finished(&request_b), Ok(None));
+ assert_eq!(channel.navigation_finished(&request_b), Ok(false));
assert!(channel.output_is_suspended());
assert!(
channel
.navigation_finished(&request_a)
.expect("final overlapping navigation")
- .is_some()
);
assert!(!channel.output_is_suspended());
}
@@ -695,7 +681,7 @@ mod tests {
channel.attach_candidate(&unknown, RendererDevToolsAgentToken::allocate()),
Err(DevToolsRendererChannelError::UnknownNavigation)
));
- assert_eq!(channel.navigation_finished(&unknown), Ok(None));
+ assert_eq!(channel.navigation_finished(&unknown), Ok(false));
let first = channel
.attach_candidate(&request, RendererDevToolsAgentToken::allocate())
@@ -710,6 +696,41 @@ mod tests {
);
}
+ #[test]
+ fn new_navigation_resets_commit_phase_but_preserves_rollback_identity() {
+ let mut channel = DevToolsRendererChannel::default();
+ let first = navigation(1);
+ let second = navigation(2);
+ channel.navigation_started(first.clone()).unwrap();
+ let candidate = channel
+ .attach_candidate(&first, RendererDevToolsAgentToken::allocate())
+ .unwrap();
+ let first_commit = channel.commit_candidate_transaction(candidate).unwrap();
+ let first_attachment = first_commit.current();
+
+ channel.navigation_started(second.clone()).unwrap();
+ assert_eq!(
+ channel.rollback_committed_candidate(first_commit),
+ Err(DevToolsRendererChannelError::CommittedCandidateMismatch)
+ );
+ assert_eq!(channel.current(), Some(first_attachment));
+
+ let candidate = channel
+ .attach_candidate(&second, RendererDevToolsAgentToken::allocate())
+ .unwrap();
+ let second_commit = channel.commit_candidate_transaction(candidate).unwrap();
+ assert_eq!(second_commit.previous(), Some(first_attachment));
+ channel.rollback_committed_candidate(second_commit).unwrap();
+ assert_eq!(channel.current(), Some(first_attachment));
+ let retry = channel
+ .attach_candidate(&second, RendererDevToolsAgentToken::allocate())
+ .unwrap();
+ assert_eq!(channel.commit_candidate(retry), Ok(Some(first_attachment)));
+ assert_eq!(channel.navigation_finished(&second), Ok(true));
+ assert_eq!(channel.navigation_finished(&second), Ok(false));
+ assert_eq!(channel.navigation_finished(&first), Ok(false));
+ }
+
#[test]
fn closed_channel_cannot_attach_or_restart() {
let request = navigation(1);
@@ -792,14 +813,13 @@ mod tests {
channel
.commit_candidate(candidate)
.expect("candidate commit");
- let resume = channel
- .navigation_finished(&request)
- .expect("navigation finish")
- .expect("channel resume");
- assert_eq!(
- resume.replacement(),
- Some((old_attachment.id(), channel.current().unwrap().id()))
+ assert!(
+ channel
+ .navigation_finished(&request)
+ .expect("navigation finish")
);
+ assert_ne!(old_attachment.id(), channel.current().unwrap().id());
+ assert_eq!(channel.current().unwrap().agent_token(), new_agent);
let released = channel.take_released_output();
assert_eq!(released.len(), 1);
@@ -823,11 +843,12 @@ mod tests {
.is_empty()
);
- let resume = channel
- .navigation_finished(&request)
- .expect("navigation finish")
- .expect("channel resume");
- assert_eq!(resume.replacement(), None);
+ assert!(
+ channel
+ .navigation_finished(&request)
+ .expect("navigation finish")
+ );
+ assert_eq!(channel.current(), Some(attachment));
let released = channel.take_released_output();
assert_eq!(released.len(), 1);
assert_eq!(batch_marker(&released[0]), Some("retained"));
diff --git a/moli-protocol/src/conn/state/page_slot.rs b/moli-protocol/src/conn/state/page_slot.rs
index 028355a83..1df1f0401 100644
--- a/moli-protocol/src/conn/state/page_slot.rs
+++ b/moli-protocol/src/conn/state/page_slot.rs
@@ -78,6 +78,13 @@ pub(crate) enum BackgroundNavigationClaimError {
AlreadyClaimed,
}
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum BackgroundWorkState {
+ NotStarted,
+ Running,
+ ResultReceived,
+}
+
/// The target-owned lifetime of one cross-Document navigation request.
///
/// The exact token remains here from navigation admission until the request
@@ -90,7 +97,7 @@ pub(crate) struct PendingNavigationRequest {
token: DocumentNavigationToken,
page_attachment_id: TargetPageAttachmentId,
cancellation_handles: Vec<moli_fetch::FetchCancelHandle>,
- background_completion_pending: bool,
+ background_work: BackgroundWorkState,
committed: bool,
}
@@ -100,7 +107,7 @@ impl PendingNavigationRequest {
token,
page_attachment_id: TargetPageAttachmentId::allocate(),
cancellation_handles: vec![moli_fetch::FetchCancelHandle::new()],
- background_completion_pending: false,
+ background_work: BackgroundWorkState::NotStarted,
committed: false,
}
}
@@ -114,7 +121,7 @@ impl PendingNavigationRequest {
}
fn settle_background_completion(&mut self) {
- self.background_completion_pending = false;
+ self.background_work = BackgroundWorkState::ResultReceived;
self.cancellation_handles.clear();
}
@@ -696,16 +703,22 @@ impl TargetPageSlot {
.as_mut()
.filter(|request| request.matches(token) && !request.committed)
.ok_or(BackgroundNavigationClaimError::Stale)?;
- if request.background_completion_pending {
- return Err(BackgroundNavigationClaimError::AlreadyClaimed);
+ match request.background_work {
+ BackgroundWorkState::NotStarted => {}
+ BackgroundWorkState::Running => {
+ return Err(BackgroundNavigationClaimError::AlreadyClaimed);
+ }
+ BackgroundWorkState::ResultReceived => {
+ return Err(BackgroundNavigationClaimError::Stale);
+ }
}
let cancellation = request
.cancellation_handle()
- .ok_or(BackgroundNavigationClaimError::Stale)?;
+ .expect("an unstarted navigation owns cancellation authority");
if let Some(additional_cancellation) = additional_cancellation {
request.cancellation_handles.push(additional_cancellation);
}
- request.background_completion_pending = true;
+ request.background_work = BackgroundWorkState::Running;
Ok(cancellation)
}
@@ -713,11 +726,9 @@ impl TargetPageSlot {
&mut self,
token: &DocumentNavigationToken,
) -> bool {
- let Some(request) = self
- .pending_navigation_request
- .as_mut()
- .filter(|request| request.matches(token) && request.background_completion_pending)
- else {
+ let Some(request) = self.pending_navigation_request.as_mut().filter(|request| {
+ request.matches(token) && request.background_work == BackgroundWorkState::Running
+ }) else {
return false;
};
request.settle_background_completion();
@@ -730,7 +741,7 @@ impl TargetPageSlot {
pub(crate) fn has_inflight_background_navigation(&self) -> bool {
self.pending_navigation_request
.as_ref()
- .is_some_and(|request| request.background_completion_pending)
+ .is_some_and(|request| request.background_work == BackgroundWorkState::Running)
}
pub(crate) fn bind_pending_document_navigation_renderer_page(
@@ -839,7 +850,7 @@ impl TargetPageSlot {
};
self.committed_document_navigation = Some(token.clone());
request.committed = true;
- if !request.background_completion_pending {
+ if request.background_work != BackgroundWorkState::Running {
request.retire_without_cancellation();
self.pending_navigation_request = None;
}
diff --git a/moli-protocol/src/conn/state/page_slot/navigation_claim_tests.rs b/moli-protocol/src/conn/state/page_slot/navigation_claim_tests.rs
index 6f74c7b2f..5770dcab6 100644
--- a/moli-protocol/src/conn/state/page_slot/navigation_claim_tests.rs
+++ b/moli-protocol/src/conn/state/page_slot/navigation_claim_tests.rs
@@ -1,6 +1,40 @@
use super::*;
use moli_fetch::FetchCancelHandle;
+#[test]
+fn background_phase_tracks_result_receipt_independently_of_document_commit() {
+ for commit_first in [false, true] {
+ let mut slot = TargetPageSlot::default();
+ let token = slot.start_document_navigation("target".into(), "loader".into());
+ assert_eq!(
+ slot.pending_navigation_request
+ .as_ref()
+ .unwrap()
+ .background_work,
+ BackgroundWorkState::NotStarted
+ );
+ assert!(!slot.settle_background_navigation_completion(&token));
+ slot.claim_background_navigation_completion(&token, None)
+ .unwrap();
+ if commit_first {
+ assert!(slot.commit_pending_document_navigation_if_matches(&token));
+ }
+ let request = slot.pending_navigation_request.as_ref().unwrap();
+ assert_eq!(request.background_work, BackgroundWorkState::Running);
+ assert_eq!(request.committed, commit_first);
+ assert!(slot.settle_background_navigation_completion(&token));
+ assert!(!slot.settle_background_navigation_completion(&token));
+ if !commit_first {
+ let request = slot.pending_navigation_request.as_ref().unwrap();
+ assert_eq!(request.background_work, BackgroundWorkState::ResultReceived);
+ assert!(!request.committed);
+ assert!(request.cancellation_handles.is_empty());
+ assert!(slot.commit_pending_document_navigation_if_matches(&token));
+ }
+ assert!(slot.pending_navigation_request.is_none());
+ }
+}
+
#[test]
fn claim_requires_the_exact_live_request() {
let mut slot = TargetPageSlot::default();
diff --git a/moli-protocol/src/conn/state/runtime_slot.rs b/moli-protocol/src/conn/state/runtime_slot.rs
index 419096828..7a2833dcc 100644
--- a/moli-protocol/src/conn/state/runtime_slot.rs
+++ b/moli-protocol/src/conn/state/runtime_slot.rs
@@ -454,9 +454,8 @@ impl TargetRuntimeSlot {
&mut self,
token: &DocumentNavigationToken,
) -> Result<FinishedRendererDocumentNavigation, DevToolsRendererChannelError> {
- let resume = self.devtools_renderer_channel.navigation_finished(token)?;
- let renderer_call_replacements = resume
- .is_some()
+ let resumed = self.devtools_renderer_channel.navigation_finished(token)?;
+ let renderer_call_replacements = resumed
.then(|| std::mem::take(&mut self.pending_renderer_call_replacements))
.filter(|replacements| !replacements.is_empty());
Ok(FinishedRendererDocumentNavigation {