From 175421ece3c469317d696879cf8c10f36977115f Mon Sep 17 00:00:00 2001 From: Codex Bot Date: Fri, 23 Oct 2026 01:14:08 +0800 Subject: [PATCH] refactor: retain bridge finalization context across layout pauses --- README.md | 3 + src/README.md | 3 + src/api.rs | 3 + src/endor_host.rs | 50 +++++++++++++++ src/source_layout.rs | 32 ---------- src/ui_bridge.rs | 146 +++++++++++++++++++++++++++++++++++++------ 6 files changed, 187 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 7397d6d..f375cad 100644 --- a/README.md +++ b/README.md @@ -503,3 +503,6 @@ The source layout driver now uses Aster's resumable read-only call API with a so source_layout::start now returns either Complete(Draft) or Pending(Work). The host-owned Work retains the unpublished frame, derived runtime/cache snapshots, convergence plan and prepare root without borrowing caller frame/cache variables. Work::resume consumes the saved job and advances the same VM continuation; Work::cancel closes that continuation and releases the prepare root without merging a draft. A second start is rejected while any read-only execution is active. Every pending Work must be resumed or explicitly cancelled with its owning VM; dropping it is not cancellation. The current layout_frame adapter still drains jobs synchronously. Mount preparation/lifecycle cancellation and parking jobs in the execution worker remain unfinished. + + +UiBridge::begin_layout_frame/resume_layout_frame now expose PendingLayout around the source job. This bridge-level continuation also owns its target window, page snapshot, tab projection, viewport restoration and retained lifecycle hook root. Only the Complete branch projects scroll/focus, captures dependency versions, merges derived caches, commits Mount geometry and runs deferred hooks. Resume restores the saved window context, and failure releases hook roots. layout_frame remains a synchronous adapter over this protocol. Cross-worker-turn scheduling and cancellation/rollback of prepared mounts are still unfinished; callers must not drop a pending bridge layout as a substitute for cancellation. diff --git a/src/README.md b/src/README.md index a633f5f..a0f518a 100644 --- a/src/README.md +++ b/src/README.md @@ -155,3 +155,6 @@ The source layout driver now uses Aster's resumable read-only call API with a so source_layout::start now returns either Complete(Draft) or Pending(Work). The host-owned Work retains the unpublished frame, derived runtime/cache snapshots, convergence plan and prepare root without borrowing caller frame/cache variables. Work::resume consumes the saved job and advances the same VM continuation; Work::cancel closes that continuation and releases the prepare root without merging a draft. A second start is rejected while any read-only execution is active. Every pending Work must be resumed or explicitly cancelled with its owning VM; dropping it is not cancellation. The current layout_frame adapter still drains jobs synchronously. Mount preparation/lifecycle cancellation and parking jobs in the execution worker remain unfinished. + + +UiBridge::begin_layout_frame/resume_layout_frame now expose PendingLayout around the source job. This bridge-level continuation also owns its target window, page snapshot, tab projection, viewport restoration and retained lifecycle hook root. Only the Complete branch projects scroll/focus, captures dependency versions, merges derived caches, commits Mount geometry and runs deferred hooks. Resume restores the saved window context, and failure releases hook roots. layout_frame remains a synchronous adapter over this protocol. Cross-worker-turn scheduling and cancellation/rollback of prepared mounts are still unfinished; callers must not drop a pending bridge layout as a substitute for cancellation. diff --git a/src/api.rs b/src/api.rs index ad023e3..9d36008 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1133,6 +1133,9 @@ The source layout driver now uses Aster's resumable read-only call API with a so source_layout::start now returns either Complete(Draft) or Pending(Work). The host-owned Work retains the unpublished frame, derived runtime/cache snapshots, convergence plan and prepare root without borrowing caller frame/cache variables. Work::resume consumes the saved job and advances the same VM continuation; Work::cancel closes that continuation and releases the prepare root without merging a draft. A second start is rejected while any read-only execution is active. Every pending Work must be resumed or explicitly cancelled with its owning VM; dropping it is not cancellation. The current layout_frame adapter still drains jobs synchronously. Mount preparation/lifecycle cancellation and parking jobs in the execution worker remain unfinished. + +UiBridge::begin_layout_frame/resume_layout_frame now expose PendingLayout around the source job. This bridge-level continuation also owns its target window, page snapshot, tab projection, viewport restoration and retained lifecycle hook root. Only the Complete branch projects scroll/focus, captures dependency versions, merges derived caches, commits Mount geometry and runs deferred hooks. Resume restores the saved window context, and failure releases hook roots. layout_frame remains a synchronous adapter over this protocol. Cross-worker-turn scheduling and cancellation/rollback of prepared mounts are still unfinished; callers must not drop a pending bridge layout as a substitute for cancellation. + ## UI host object Positional arguments only: diff --git a/src/endor_host.rs b/src/endor_host.rs index 46169d1..ecc9829 100644 --- a/src/endor_host.rs +++ b/src/endor_host.rs @@ -3889,6 +3889,56 @@ page.render(block) ); } + #[test] + fn bridge_layout_can_return_pending_before_committing_a_complete_frame() { + let children = std::iter::repeat_n("item", 40) + .collect::>() + .join(", "); + let source = format!( + "class Wide: Row {{\n function measure(self: Wide, mount: Mount, constraints: Constraints): MeasureResult {{ return MeasureResult(340, 60) }}\n}}\nitem = RectBlock(2, 20)\npage = Page()\npage.render(Wide(0, 0, {children}))" + ); + let (mut session, _) = ReplSession::load(&source, "app.main", 270, 1).unwrap(); + session + .bridge + .set_viewport( + &mut session.candidate.vm, + Some(crate::layout::Size { + width: 500.0, + height: 300.0, + }), + ) + .unwrap(); + let root = session + .bridge + .prepare_frame(&mut session.candidate.vm) + .unwrap() + .unwrap(); + let mut progress = session + .bridge + .begin_layout_frame(&mut session.candidate.vm, root) + .unwrap(); + assert!(matches!( + progress, + crate::ui_bridge::LayoutProgress::Pending(_) + )); + assert!(session.candidate.vm.has_suspended_call()); + loop { + match progress { + crate::ui_bridge::LayoutProgress::Pending(pending) => { + progress = session + .bridge + .resume_layout_frame(&mut session.candidate.vm, *pending) + .unwrap(); + } + crate::ui_bridge::LayoutProgress::Complete(frame) => { + assert_eq!(frame.mounts.last().unwrap().x, 78.0); + assert!(!session.candidate.vm.has_suspended_call()); + break; + } + } + } + } + #[test] fn layout_slices_preserve_all_shared_occurrences() { let children = std::iter::repeat_n("item", 40) diff --git a/src/source_layout.rs b/src/source_layout.rs index 23c7ba4..c8d8665 100644 --- a/src/source_layout.rs +++ b/src/source_layout.rs @@ -929,35 +929,3 @@ fn finish(vm: &mut Vm, mut work: Work, value: Value, reads: Vec) -> R .collect(); Ok(Progress::Complete(work.state)) } - -pub fn render( - vm: &mut Vm, - desktop: ObjectRef, - mounts: Vec, - root: Arc, - frame: &mut Frame, - runtime: &mut LayoutRuntime, - cache: &mut Cache, -) -> Result> { - let state = Draft { - frame: frame.clone(), - runtime: runtime.clone(), - cache: cache.clone(), - reads: BTreeSet::new(), - }; - let mut progress = start(vm, desktop, mounts, root, state, 256)?; - loop { - match progress { - Progress::Pending(work) => { - std::thread::yield_now(); - progress = work.resume(vm, 256)?; - } - Progress::Complete(state) => { - *frame = state.frame; - *runtime = state.runtime; - *cache = state.cache; - return Ok(state.reads); - } - } - } -} diff --git a/src/ui_bridge.rs b/src/ui_bridge.rs index 7cdee29..8983c57 100644 --- a/src/ui_bridge.rs +++ b/src/ui_bridge.rs @@ -1701,6 +1701,26 @@ impl NativeCallable for Function { } } +#[must_use = "pending layout must be resumed on its owning VM"] +pub enum LayoutProgress { + Pending(Box), + Complete(Box), +} + +struct LayoutContext { + root: Arc, + tabs: Vec, + full_viewport: Option, + desktop: ObjectRef, + window: u64, + hooks: Option, +} + +pub struct PendingLayout { + work: crate::source_layout::Work, + context: LayoutContext, +} + pub struct UiBridge { pub ui: ObjectRef, session: Shared, @@ -2462,11 +2482,15 @@ impl UiBridge { Ok(()) } - pub fn layout_frame( + pub fn begin_layout_frame( &self, vm: &mut Vm, root: Option>, - ) -> Result { + ) -> Result { + anyhow::ensure!( + !vm.has_suspended_call() && !vm.object_store().is_read_only(), + "cannot prepare another frame during suspended layout" + ); let Some(root) = root else { self.session .lock() @@ -2496,9 +2520,9 @@ impl UiBridge { } }; self.commit_frame(vm, None, &frame)?; - return Ok(frame); + return Ok(LayoutProgress::Complete(Box::new(frame))); }; - let (mut layout, mut cache, desktop, page, nodes) = { + let (mut layout, cache, desktop, page, nodes) = { let state = self .session .lock() @@ -2548,16 +2572,88 @@ impl UiBridge { .synchronize(vm, desktop, page, nodes, &frame, true)?; (hooks, state.mounts.ordered()) }; + let context = LayoutContext { + root: root.clone(), + tabs, + full_viewport, + desktop, + window: frame.window, + hooks, + }; + let draft = crate::source_layout::Draft { + frame, + runtime: layout, + cache, + reads: std::collections::BTreeSet::new(), + }; + let progress = crate::source_layout::start(vm, desktop, mounts, root, draft, 256); + self.accept_layout_progress(vm, context, progress) + } + + pub fn resume_layout_frame( + &self, + vm: &mut Vm, + pending: PendingLayout, + ) -> Result { + let PendingLayout { work, context } = pending; + if let Err(error) = self.select_window(context.window) { + let cancelled = work.cancel(vm); + let released = context.hooks.map(|hooks| vm.release(hooks)).transpose(); + cancelled?; + released?; + return Err(error); + } + let progress = work.resume(vm, 256); + self.accept_layout_progress(vm, context, progress) + } + + fn accept_layout_progress( + &self, + vm: &mut Vm, + context: LayoutContext, + progress: Result, + ) -> Result { + match progress { + Ok(crate::source_layout::Progress::Pending(work)) => { + Ok(LayoutProgress::Pending(Box::new(PendingLayout { + work, + context, + }))) + } + Ok(crate::source_layout::Progress::Complete(draft)) => self + .finish_layout_frame(vm, context, draft) + .map(|frame| LayoutProgress::Complete(Box::new(frame))), + Err(error) => { + if let Some(hooks) = context.hooks { + vm.release(hooks)?; + } + Err(error) + } + } + } + + fn finish_layout_frame( + &self, + vm: &mut Vm, + context: LayoutContext, + draft: crate::source_layout::Draft, + ) -> Result { + let LayoutContext { + root, + tabs, + full_viewport, + desktop, + window, + hooks, + } = context; + let crate::source_layout::Draft { + mut frame, + runtime: mut layout, + cache, + reads, + } = draft; let result = (|| { - let reads = crate::source_layout::render( - vm, - desktop, - mounts, - root.clone(), - &mut frame, - &mut layout, - &mut cache, - )?; + self.select_window(window)?; { let state = self .session @@ -2568,11 +2664,7 @@ impl UiBridge { // Native layout may replace the structural Frame; attach chrome only // after either layout path has produced the final page geometry. frame.tabs = tabs; - frame.window = self - .session - .lock() - .map_err(|_| anyhow::anyhow!("UI session poisoned"))? - .current; + frame.window = window; // Capture versions before lifecycle hooks can change a dependency. let reads = reads .into_iter() @@ -2616,6 +2708,24 @@ impl UiBridge { } result } + + pub fn layout_frame( + &self, + vm: &mut Vm, + root: Option>, + ) -> Result { + let mut progress = self.begin_layout_frame(vm, root)?; + loop { + match progress { + LayoutProgress::Pending(pending) => { + std::thread::yield_now(); + progress = self.resume_layout_frame(vm, *pending)?; + } + LayoutProgress::Complete(frame) => return Ok(*frame), + } + } + } + pub fn rendered(&self, vm: &Vm) -> Result>> { let state = self .session