Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(devtools): preserve nested Main response order

ldm02fe9e2ad9a89
diff --git a/moli-renderer-v8/src/devtools/ingress/main.rs b/moli-renderer-v8/src/devtools/ingress/main.rs
index 93292dc0d..e320a4a7a 100644
--- a/moli-renderer-v8/src/devtools/ingress/main.rs
+++ b/moli-renderer-v8/src/devtools/ingress/main.rs
@@ -674,9 +674,33 @@ mod tests {
use super::*;
use crate::{
devtools::pause::RendererInspectorPauseBridge,
- runtime::{RendererInspectorIngressTicket, RendererRuntimeInspectorAsyncCompletion},
+ runtime::{
+ RendererInspectorIngressTicket, RendererPageReply, RendererPageState,
+ RendererPerformanceMetricSnapshot, RendererRuntimeCommandOutput,
+ RendererRuntimeInspectorAsyncCompletion,
+ },
+ types::ScriptExecutionReport,
};
+ fn page_state() -> Arc<RendererPageState> {
+ let url = url::Url::parse("about:blank").expect("test URL");
+ Arc::new(RendererPageState {
+ requested_url: url.clone(),
+ navigation_initiator_url: None,
+ navigation_redirected: false,
+ navigation_redirect_count: 0,
+ final_url: url,
+ document_title: String::new(),
+ status: 200,
+ headers: Vec::new(),
+ script_execution: Arc::new(ScriptExecutionReport::default()),
+ idle_override: None,
+ service_worker_client_id: 0,
+ dedicated_worker_running_worker_isolate_count: 0,
+ performance_metric_snapshot: RendererPerformanceMetricSnapshot::default(),
+ })
+ }
+
fn ingress() -> RendererInspectorMainIngress {
let pause_bridge = RendererInspectorPauseBridge::default();
RendererInspectorMainIngress::new(
@@ -870,10 +894,24 @@ mod tests {
"the V8 command must remain behind the Page agent first-dispatch boundary"
);
- ingress.first_dispatch_guard(&page).release();
+ let output = RendererCommandTurnOutput::new(
+ RendererPageReply::Unit,
+ page_state(),
+ RendererRuntimeCommandOutput::default(),
+ None,
+ None,
+ )
+ .expect("test Page-agent output")
+ .hold_until_protocol_handoff(ingress.first_dispatch_guard(&page));
+ assert!(
+ ingress.claim_for_pause().is_none(),
+ "settled Page output must retain the lane until protocol consumes it"
+ );
+
+ let (_completion, _predecessor) = output.into_completion_and_predecessor();
let v8 = ingress
.claim_for_pause()
- .expect("the V8 command should follow Page first-dispatch");
+ .expect("the V8 command should follow the Page protocol handoff");
assert_eq!(
v8.nested_dispatch(),
RendererDevToolsMainNestedDispatch::InspectorSession
diff --git a/moli-renderer-v8/src/runtime/nested_main.rs b/moli-renderer-v8/src/runtime/nested_main.rs
index 50bcc058a..5c37b0a21 100644
--- a/moli-renderer-v8/src/runtime/nested_main.rs
+++ b/moli-renderer-v8/src/runtime/nested_main.rs
@@ -52,7 +52,7 @@ pub(super) fn bind_active_nested_main_page(entry: &mut LivePageEntry) -> ActiveN
pub(crate) fn dispatch_nested_main_page_command(
command: RendererPageCommand,
- mut first_dispatch: RendererInspectorMainFirstDispatchGuard,
+ first_dispatch: RendererInspectorMainFirstDispatchGuard,
) -> Result<RendererCommandTurnOutput> {
let active = ACTIVE_NESTED_MAIN_PAGE
.try_with(|active| active.borrow().clone())
@@ -60,9 +60,6 @@ pub(crate) fn dispatch_nested_main_page_command(
.flatten()
.ok_or_else(|| anyhow!("nested Main receiver has no active Page owner stack"))?;
- // Release the per-session lane at the actual agent dispatch boundary, not
- // when the pause loop merely claims the queued command.
- let _post_dispatch_wake = first_dispatch.release_for_dispatch();
// SAFETY: `bind_active_nested_main_page` installs this pointer immediately
// around the owner-local Page dispatch that can enter V8. A normal debugger
// pause synchronously suspends that outer dispatch, and this callback runs
@@ -73,11 +70,18 @@ pub(crate) fn dispatch_nested_main_page_command(
.ok_or_else(|| anyhow!("nested Main Page pointer was unexpectedly null"))?;
let reply: RendererPageReply = page_vm.dispatch_renderer_page_command(command)?;
let page_state = active.entry_slot.active_page_state()?;
- RendererCommandTurnOutput::new(
+ let output = RendererCommandTurnOutput::new(
reply,
page_state,
RendererRuntimeCommandOutput::default(),
None,
None,
- )
+ )?;
+ // Chromium's synchronous non-V8 agent dispatch sends its response before
+ // returning to the nested Main receiver. Moli decodes the typed reply in
+ // the protocol actor, so retain the receiver slot with the immutable
+ // result. Consuming or abandoning that result releases fail-open; the
+ // actor cannot observe a later renderer publication before it has handled
+ // this handoff.
+ Ok(output.hold_until_protocol_handoff(first_dispatch))
}
diff --git a/moli-renderer-v8/src/runtime/page_surface.rs b/moli-renderer-v8/src/runtime/page_surface.rs
index 8b4c9125a..eef06e752 100644
--- a/moli-renderer-v8/src/runtime/page_surface.rs
+++ b/moli-renderer-v8/src/runtime/page_surface.rs
@@ -1,4 +1,5 @@
use super::*;
+use crate::devtools::ingress::main::RendererInspectorMainFirstDispatchGuard;
use crate::native_bridge::{
PendingRuntimeObservableConsoleSourceEvent, RuntimeObservableContextToken,
};
@@ -1573,6 +1574,13 @@ impl RendererCommandTurnCompletion {
pub struct RendererCommandTurnOutput {
completion: RendererCommandTurnCompletion,
renderer_output_predecessor: Option<RendererOutputFence>,
+ // A nested synchronous DevTools agent call must keep its per-session Main
+ // receiver slot until the protocol actor owns the settled result. This is
+ // deliberately distinct from a post-response continuation: dropping the
+ // result at the protocol handoff is enough to preserve receiver order,
+ // while waiting for the frontend response would deadlock multi-stage
+ // commands that need to enter the same receiver again.
+ protocol_handoff: Option<Box<RendererInspectorMainFirstDispatchGuard>>,
}
impl RendererCommandTurnOutput {
@@ -1604,9 +1612,22 @@ impl RendererCommandTurnOutput {
post_response_continuation,
},
renderer_output_predecessor,
+ protocol_handoff: None,
})
}
+ pub(crate) fn hold_until_protocol_handoff(
+ mut self,
+ handoff: RendererInspectorMainFirstDispatchGuard,
+ ) -> Self {
+ debug_assert!(
+ self.protocol_handoff.is_none(),
+ "a renderer command turn may hold only one protocol handoff"
+ );
+ self.protocol_handoff = Some(Box::new(handoff));
+ self
+ }
+
#[doc(hidden)]
pub fn bind_renderer_agent_attachment(&mut self, id: RendererAgentAttachmentId) {
if let RendererPageReply::RuntimeInspectorProtocolMessages(output) =
@@ -1646,7 +1667,12 @@ impl RendererCommandTurnOutput {
pub fn into_completion_and_predecessor(
self,
) -> (RendererCommandTurnCompletion, Option<RendererOutputFence>) {
- (self.completion, self.renderer_output_predecessor)
+ let Self {
+ completion,
+ renderer_output_predecessor,
+ protocol_handoff: _,
+ } = self;
+ (completion, renderer_output_predecessor)
}
pub fn into_reply_and_state(self) -> (RendererPageReply, Arc<RendererPageState>) {