Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(renderer): fence command output at its exact cursor
ldm0bc11eeb2d534
diff --git a/moli-renderer-v8/src/runtime/owner.rs b/moli-renderer-v8/src/runtime/owner.rsindex cffc15851..9593214f9 100644--- a/moli-renderer-v8/src/runtime/owner.rs+++ b/moli-renderer-v8/src/runtime/owner.rs@@ -995,6 +995,8 @@ pub(super) struct RendererOwnerState {context_shutdown_notify: tokio::sync::Notify,#[cfg(test)]command_dispatch_gate: Mutex<Option<RendererCommandDispatchGateForTesting>>,+ #[cfg(test)]+ publish_next_command_output_before_settlement: std::sync::atomic::AtomicBool,#[cfg(debug_assertions)]pub(super) owner_local_thread_id: Mutex<Option<ThreadId>>,}@@ -1797,6 +1799,10 @@ impl RendererOwnerHandle {context_shutdown_notify: tokio::sync::Notify::new(),#[cfg(test)]command_dispatch_gate: Mutex::new(None),+ #[cfg(test)]+ publish_next_command_output_before_settlement: std::sync::atomic::AtomicBool::new(+ false,+ ),#[cfg(debug_assertions)]owner_local_thread_id: Mutex::new(None),});@@ -2330,6 +2336,17 @@ impl RendererOwnerHandle {(entered_rx, release_tx)}+ #[cfg(test)]+ pub(crate) fn publish_next_command_output_before_settlement_for_testing(&self) {+ assert!(+ !self+ .state+ .publish_next_command_output_before_settlement+ .swap(true, Ordering::AcqRel),+ "renderer command output publication test hook already installed"+ );+ }+#[cfg(test)]pub(crate) fn close_command_admission_for_testing(&self) {self.render_runtime.close_admission();@@ -4705,8 +4722,45 @@ impl RendererOwnerHandle {) -> RenderRuntimeDispatchOutcome {let (runtime_command_output, runtime_session_response) =entry.page_vm_mut().take_runtime_command_settlement();- let command_produced_records = !turn_records.is_empty();- entry.page_vm().append_renderer_output_records(turn_records);+ let expected_command_output_cursor = if turn_records.is_empty() {+ None+ } else {+ Some(+ entry+ .page_vm()+ .append_renderer_command_output_records(turn_records),+ )+ };+ #[cfg(test)]+ if self+ .state+ .publish_next_command_output_before_settlement+ .swap(false, Ordering::AcqRel)+ {+ assert!(+ expected_command_output_cursor.is_some(),+ "the command output publication test hook requires command-owned records"+ );+ let published = entry+ .page_vm()+ .renderer_page_script_environment()+ .expect("a live Page command must retain its script environment")+ .output_journal()+ .publish_pending();+ assert!(+ published.is_some(),+ "the command output publication test hook must settle a concrete batch"+ );+ entry.page_vm().append_renderer_output_records(vec![+ PendingRendererOutputRecord::observation(+ None,+ RendererProtocolObservation::RuntimeLifecycleError {+ text: "test trailing output publication".to_owned(),+ execution_context_id: None,+ },+ ),+ ]);+ }let (mut entry, page_state_result) = commit_page_state_on_entry_via_local_task_with_policy(self.state.local_executor.clone(),entry,@@ -4714,24 +4768,35 @@ impl RendererOwnerHandle {).await;let concrete_output = entry.page_vm_mut().settle_renderer_output_publication();- assert!(- !command_produced_records || concrete_output.is_some(),- "renderer command records must settle into the concrete Page stream before completion"- );// Chromium flushes every notification already queued by the current- // DevTools command before exposing its response. The concrete batch- // settled here is the exact Moli equivalent of that queue- // prefix. Do not require a Runtime-only causal marker: DOM commands- // and WebAPI side effects (for example mutation, CSP and Log records)- // are produced during this owner turn but do not all carry a- // `RendererRuntimeCommandCausalIdentity`.+ // DevTools command before exposing its response. Appending under the+ // journal lock freezes the exact publication cursor that must contain+ // this command's records. An attachment-scoped producer can publish+ // that batch while page-state capture is awaiting its local task, and+ // can even publish later batches before this owner resumes. The+ // response must still fence only the exact command batch, while the+ // published tail proves that batch was not lost.//- // This remains a narrow fence. It names this command turn's one- // publication, not a Page-wide watermark, and a later independent- // task necessarily settles into a later stream sequence.- let renderer_output_cursor = concrete_output- .as_ref()- .map(RendererOutputPublication::cursor);+ // Do not require a Runtime-only causal marker: DOM commands and WebAPI+ // side effects (for example mutation, CSP and Log records) are+ // produced during this owner turn but do not all carry a+ // `RendererRuntimeCommandCausalIdentity`.+ let renderer_output_cursor = match expected_command_output_cursor {+ Some(expected) => {+ let published_tail = entry.page_vm().renderer_output_tail_cursor();+ assert!(+ published_tail.is_some_and(|cursor| {+ cursor.stream() == expected.stream()+ && cursor.sequence() >= expected.sequence()+ }),+ "renderer command records must settle into the concrete Page stream before completion"+ );+ Some(expected)+ }+ None => concrete_output+ .as_ref()+ .map(RendererOutputPublication::cursor),+ };let renderer_output_predecessor = renderer_output_cursor.map(|cursor| entry.page_vm().declare_renderer_output_fence(cursor));self.restore_live_page_entry(token, entry);diff --git a/moli-renderer-v8/src/runtime/page.rs b/moli-renderer-v8/src/runtime/page.rsindex 49d3cb94f..9a68db8bb 100644--- a/moli-renderer-v8/src/runtime/page.rs+++ b/moli-renderer-v8/src/runtime/page.rs@@ -194,6 +194,13 @@ impl JsRuntime {.install_command_dispatch_gate_for_testing()}+ #[cfg(test)]+ pub(crate) fn publish_next_command_output_before_owner_settlement_for_testing(&self) {+ self.inner+ .renderer_owner+ .publish_next_command_output_before_settlement_for_testing();+ }+#[cfg(test)]pub(crate) fn close_owner_command_admission_for_testing(&self) {self.innerdiff --git a/moli-renderer-v8/src/runtime/page_vm/mod.rs b/moli-renderer-v8/src/runtime/page_vm/mod.rsindex 5c0f8acf1..fa2bd5c66 100644--- a/moli-renderer-v8/src/runtime/page_vm/mod.rs+++ b/moli-renderer-v8/src/runtime/page_vm/mod.rs@@ -1806,6 +1806,13 @@ impl PageVm {self.vm().append_renderer_output_records(records);}+ pub(super) fn append_renderer_command_output_records(+ &self,+ records: Vec<PendingRendererOutputRecord>,+ ) -> crate::runtime::RendererOutputCursor {+ self.vm().append_renderer_command_output_records(records)+ }+pub(super) fn renderer_output_tail_cursor(&self,) -> Option<crate::runtime::RendererOutputCursor> {diff --git a/moli-renderer-v8/src/runtime/protocol_output/turn_journal.rs b/moli-renderer-v8/src/runtime/protocol_output/turn_journal.rsindex 09c087a9e..8e34fd5a1 100644--- a/moli-renderer-v8/src/runtime/protocol_output/turn_journal.rs+++ b/moli-renderer-v8/src/runtime/protocol_output/turn_journal.rs@@ -146,6 +146,26 @@ impl RendererTurnOutputJournal {state.records.extend(records);}+ /// Appends one non-empty owner-command batch and atomically captures the+ /// cursor of the publication that must contain it.+ pub(crate) fn append_command_records(+ &self,+ records: Vec<PendingRendererOutputRecord>,+ ) -> RendererOutputCursor {+ assert!(+ !records.is_empty(),+ "renderer command output cursor requires at least one record"+ );+ let mut state = self.state.lock();+ assert!(+ !state.closed,+ "renderer output cannot be appended after stream closure"+ );+ let expected_cursor = RendererOutputCursor::new(state.stream, state.next_sequence);+ state.records.extend(records);+ expected_cursor+ }+#[cfg(test)]pub(crate) fn settle(&self) -> Option<RendererOutputPublication> {let mut state = self.state.lock();diff --git a/moli-renderer-v8/src/runtime/tests.rs b/moli-renderer-v8/src/runtime/tests.rsindex a88ec791f..5066bda7d 100644--- a/moli-renderer-v8/src/runtime/tests.rs+++ b/moli-renderer-v8/src/runtime/tests.rs@@ -10005,6 +10005,105 @@ async fn inspector_output_flushes_v8_state_for_commands_and_notifications() {.expect("inspector state output page should close");}+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]+async fn owner_command_fences_exact_cursor_when_later_output_is_already_published() {+ let runtime = JsRuntime::initialize();+ let (output_tx, mut output_rx) = renderer_external_activity_test_channel();+ runtime.set_renderer_output_transport_sender(output_tx);+ let loader =+ ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).expect("default loader");+ let url = url::Url::parse("https://example.test/concurrent-command-output").unwrap();+ let mut page =+ create_test_html_page(&runtime, &loader, url, "<!doctype html><body>before</body>").await;++ let (enable_messages, _) = dispatch_runtime_protocol_with_output_for_test(+ &page,+ serde_json::json!({+ "id": 711,+ "method": "Runtime.enable",+ }),+ )+ .await+ .expect("Runtime.enable should dispatch");+ assert!(+ runtime_protocol_response_by_id(&enable_messages, 711)+ .is_some_and(|message| message.get("error").is_none()),+ "Runtime.enable should succeed before the command-output race: {enable_messages:?}"+ );+ output_rx.drain();++ runtime.publish_next_command_output_before_owner_settlement_for_testing();+ let pending_evaluate = page+ .enqueue_async_command(RendererPageCommand::EvaluateExpression {+ expression: "console.log('concurrent-command-output'); 'done'".to_owned(),+ await_promise: false,+ })+ .expect("Runtime.evaluate should enqueue");+ let concurrent_publication = tokio::time::timeout(Duration::from_secs(3), output_rx.recv())+ .await+ .expect("the test publication should not hang")+ .expect("the test hook should settle the pending owner-command records");+ assert!(+ concurrent_publication+ .records()+ .iter()+ .any(|record| matches!(+ record.item(),+ RendererOutputItem::Observation(RendererProtocolObservation::RuntimeInspector(batch))+ if batch.messages.iter().any(|message| matches!(+ message,+ RendererRuntimeInspectorMessage::Protocol(message)+ if message.get("method")+ == Some(&serde_json::json!("Runtime.consoleAPICalled"))+ ))+ )),+ "the early publication must own the Runtime notification emitted by the owner command"+ );+ let trailing_publication = tokio::time::timeout(Duration::from_secs(3), output_rx.recv())+ .await+ .expect("the trailing test publication should not hang")+ .expect("final owner settlement should publish a later independent batch");+ assert!(+ trailing_publication.records().iter().any(|record| matches!(+ record.item(),+ RendererOutputItem::Observation(RendererProtocolObservation::RuntimeLifecycleError {+ text,+ execution_context_id: None,+ }) if text == "test trailing output publication"+ )),+ "final Page settlement should publish the independent trailing test output"+ );+ assert_eq!(+ trailing_publication.cursor().sequence(),+ concurrent_publication.cursor().sequence() + 1,+ "the independent output must follow the command publication"+ );++ let evaluate_output = tokio::time::timeout(Duration::from_secs(3), pending_evaluate.wait())+ .await+ .expect("owner command should not hang after the IO publication")+ .expect("owner command should complete after its records were settled by IO");+ assert_eq!(+ evaluate_output+ .renderer_output_predecessor()+ .expect("owner response must retain the concurrently settled prefix")+ .cursor(),+ concurrent_publication.cursor()+ );+ assert_ne!(+ evaluate_output+ .renderer_output_predecessor()+ .expect("owner response must retain an exact output prefix")+ .cursor(),+ trailing_publication.cursor(),+ "the response fence must not drift to output published after its command batch"+ );++ page.close_async()+ .await+ .expect("concurrent command output page should close");+}+#[tokio::test(flavor = "multi_thread")]async fn page_diagnostics_snapshot_is_read_only_for_current_inspector_output() {let runtime = JsRuntime::initialize();diff --git a/moli-renderer-v8/src/script_vm.rs b/moli-renderer-v8/src/script_vm.rsindex 4879bcf51..2664f756d 100644--- a/moli-renderer-v8/src/script_vm.rs+++ b/moli-renderer-v8/src/script_vm.rs@@ -6625,6 +6625,17 @@ impl ScriptVm {environment.output_journal().append_records(records);}+ pub(super) fn append_renderer_command_output_records(+ &self,+ records: Vec<crate::runtime::PendingRendererOutputRecord>,+ ) -> crate::runtime::RendererOutputCursor {+ let environment = self+ .renderer_page_script_environment+ .as_ref()+ .expect("a live Page command must have a renderer output journal");+ environment.output_journal().append_command_records(records)+ }+pub(super) fn has_renderer_output_journal(&self) -> bool {self.renderer_page_script_environment.is_some()}