feat: separate candidate program execution from initial layout continuation

This commit is contained in:
2026-10-23 10:52:01 +08:00
parent 470454caa6
commit 799fe9f3c7
3 changed files with 42 additions and 9 deletions
+4 -1
View File
@@ -511,7 +511,7 @@ UiBridge::begin_layout_frame/resume_layout_frame now expose PendingLayout around
ReplSession now owns a resumable RefreshWork containing window traversal, pending bridge layout, unpublished per-window documents and the cumulative refresh work budget. poll_refresh advances one refresh stage or VM quantum; only a complete stable pass replaces the session's full document map. Synchronous callers drain the same state machine. Failure stops the domain, releases any pending bridge work and preserves the externally published picture. refreshYields counts cooperative refresh returns, not wall-clock time. Only still-pending Mount preparation is rolled back; completed callbacks remain committed.
Viewport refresh now advances through ReplSession's saved refresh state on worker idle turns. A turn advances one traversal stage or a 256-instruction layout quantum; a pending job shortens the next worker wait to 1 ms. Tasks/window commands and document publication wait for complete refresh. ui.stats().refreshYields reports cooperative idle returns. Mutating requests first drain the existing job, so they cannot run inside its read-only scope. Mutating requests are serialized; newer viewport targets coalesce by cancelling unpublished layout. Initial loads still drain synchronously. Explicit REPL/event refresh can park across worker turns. Idle task delivery starts at most one ready callback per turn and parks its subsequent layout; callbacks themselves and individual Rust measurement/paint calls remain atomic.
Viewport refresh now advances through ReplSession's saved refresh state on worker idle turns. A turn advances one traversal stage or a 256-instruction layout quantum; a pending job shortens the next worker wait to 1 ms. Tasks/window commands and document publication wait for complete refresh. ui.stats().refreshYields reports cooperative idle returns. Mutating requests first drain the existing job, so they cannot run inside its read-only scope. Mutating requests are serialized; newer viewport targets coalesce by cancelling unpublished layout. Initial source execution remains atomic; its layout can park across worker turns. Explicit REPL/event refresh can park across worker turns. Idle task delivery starts at most one ready callback per turn and parks its subsequent layout; callbacks themselves and individual Rust measurement/paint calls remain atomic.
Runtime mutation entry points reject direct calls while refresh work is pending, before executing source or changing metadata; task polling defers. The API worker drains the saved refresh before accepting those mutations, keeping the runtime usable. Incremental refresh keeps the prior complete document map until a stable pass, including across window traversal boundaries where no VM read-only frame is currently active.
@@ -536,3 +536,6 @@ REPL and event mutations now have incremental refresh entry points. They execute
The API worker now owns a pending HTTP request plus its scalar result while REPL/event layout is parked. It advances the layout with current native viewport targets and responds only after the stable full-window publication and command extraction. Later mutation requests remain queued; GET /code can observe the last complete publication. A newer accepted PUT invalidates queued inputs but lets the already-running owner settle before candidate construction, so failed replacement can preserve its consistent heap. DELETE revokes that owner and prevents late publication. This queue ordering does not roll back already-executed source effects.
Complete-source candidates now separate one-time compilation/source execution from initial layout. load_incremental owns the isolated VM while its first layout is pending; the current live VM stays separate. Cancellation uses the same Mount checkpoint/continuation cleanup as later refresh. Synchronous load helpers remain for host regression coverage. This does not yet slice parsing, compilation, source execution, structural preparation or paint.
+4 -1
View File
@@ -163,7 +163,7 @@ UiBridge::begin_layout_frame/resume_layout_frame now expose PendingLayout around
ReplSession now owns a resumable RefreshWork containing window traversal, pending bridge layout, unpublished per-window documents and the cumulative refresh work budget. poll_refresh advances one refresh stage or VM quantum; only a complete stable pass replaces the session's full document map. Synchronous callers drain the same state machine. Failure stops the domain, releases any pending bridge work and preserves the externally published picture. refreshYields counts cooperative refresh returns, not wall-clock time. Only still-pending Mount preparation is rolled back; completed callbacks remain committed.
Viewport refresh now advances through ReplSession's saved refresh state on worker idle turns. A turn advances one traversal stage or a 256-instruction layout quantum; a pending job shortens the next worker wait to 1 ms. Tasks/window commands and document publication wait for complete refresh. ui.stats().refreshYields reports cooperative idle returns. Mutating requests first drain the existing job, so they cannot run inside its read-only scope. Mutating requests are serialized; newer viewport targets coalesce by cancelling unpublished layout. Initial loads still drain synchronously. Explicit REPL/event refresh can park across worker turns. Idle tasks deliver at most one callback per turn and park subsequent layout; callbacks and individual native nodes remain atomic. Large intrinsic trees use the sliced driver; small intrinsic trees keep the fast path.
Viewport refresh now advances through ReplSession's saved refresh state on worker idle turns. A turn advances one traversal stage or a 256-instruction layout quantum; a pending job shortens the next worker wait to 1 ms. Tasks/window commands and document publication wait for complete refresh. ui.stats().refreshYields reports cooperative idle returns. Mutating requests first drain the existing job, so they cannot run inside its read-only scope. Mutating requests are serialized; newer viewport targets coalesce by cancelling unpublished layout. Initial source execution remains atomic; its layout can park across worker turns. Explicit REPL/event refresh can park across worker turns. Idle tasks deliver at most one callback per turn and park subsequent layout; callbacks and individual native nodes remain atomic. Large intrinsic trees use the sliced driver; small intrinsic trees keep the fast path.
Runtime mutation entry points reject direct calls while refresh work is pending, before executing source or changing metadata; task polling defers. The API worker drains the saved refresh before accepting those mutations, keeping the runtime usable. Incremental refresh keeps the prior complete document map until a stable pass, including across window traversal boundaries where no VM read-only frame is currently active.
@@ -191,3 +191,6 @@ REPL and event mutations now have incremental refresh entry points. They execute
The API worker now owns a pending HTTP request plus its scalar result while REPL/event layout is parked. It advances the layout with current native viewport targets and responds only after the stable full-window publication and command extraction. Later mutation requests remain queued; GET /code can observe the last complete publication. A newer accepted PUT invalidates queued inputs but lets the already-running owner settle before candidate construction, so failed replacement can preserve its consistent heap. DELETE revokes that owner and prevents late publication. This queue ordering does not roll back already-executed source effects.
Complete-source candidates now separate one-time compilation/source execution from initial layout. load_incremental owns the isolated VM while its first layout is pending; the current live VM stays separate. Cancellation uses the same Mount checkpoint/continuation cleanup as later refresh. Synchronous load helpers remain for host regression coverage. This does not yet slice parsing, compilation, source execution, structural preparation or paint.
+34 -7
View File
@@ -240,6 +240,7 @@ pub struct ReplResult {
impl ReplSession {
/// Compile once into an isolated candidate, then keep its actual heap and
/// callback environments alive after the first successful publication.
#[cfg(test)]
pub fn load(
source: &str,
namespace: &str,
@@ -249,6 +250,7 @@ impl ReplSession {
Self::load_with_viewport(source, namespace, submission, id, None)
}
#[cfg(test)]
pub fn load_with_viewport(
source: &str,
namespace: &str,
@@ -256,6 +258,37 @@ impl ReplSession {
id: u64,
viewport: Option<crate::layout::Size>,
) -> Result<(Self, RenderDocument)> {
let mut runtime = Self::prepare_program(source, namespace, submission, id, viewport)?;
let document = runtime.refresh_document()?
.ok_or_else(|| anyhow::anyhow!("program did not publish a Page"))?;
runtime.bridge.reclaim(&mut runtime.candidate.vm, &mut runtime.candidate.roots)?;
Ok((runtime, document))
}
pub fn load_incremental(
source: &str,
namespace: &str,
submission: u64,
id: u64,
viewport: Option<crate::layout::Size>,
) -> Result<(Self, Option<RenderDocument>)> {
let mut runtime = Self::prepare_program(source, namespace, submission, id, viewport)?;
let viewports = viewport.map(|size| (crate::window::MAIN_WINDOW, size)).into_iter().collect();
let document = runtime.refresh_after_mutation(Some(&viewports))?;
if !runtime.has_pending_refresh() {
anyhow::ensure!(document.is_some(), "program did not publish a Page");
runtime.bridge.reclaim(&mut runtime.candidate.vm, &mut runtime.candidate.roots)?;
}
Ok((runtime, document))
}
fn prepare_program(
source: &str,
namespace: &str,
submission: u64,
id: u64,
viewport: Option<crate::layout::Size>,
) -> Result<Self> {
if source.len() > MAX_SOURCE_BYTES {
bail!("source exceeds the 1 MiB limit");
}
@@ -317,13 +350,7 @@ impl ReplSession {
runtime.source = source.into();
runtime.ast_nodes = budget.nodes;
runtime.program_root = Some(architecture(&unit, &plan, namespace, submission));
let document = runtime
.refresh_document()?
.ok_or_else(|| anyhow::anyhow!("program did not publish a Page"))?;
runtime
.bridge
.reclaim(&mut runtime.candidate.vm, &mut runtime.candidate.roots)?;
Ok((runtime, document))
Ok(runtime)
}
pub fn new_with_viewport(id: u64, viewport: Option<crate::layout::Size>) -> Result<Self> {