feat: retain HTTP input requests across cooperative refresh turns

This commit is contained in:
2026-10-23 02:02:43 +08:00
parent dfc69b4777
commit 7289df036f
3 changed files with 174 additions and 49 deletions
+3
View File
@@ -533,3 +533,6 @@ Idle timer delivery now processes at most one ready callback (or one ineligible
REPL and event mutations now have incremental refresh entry points. They execute source/callbacks once, retain scalar response metadata, then park layout with the current viewport target. While pending, mutation, command extraction and garbage collection wait; layout can be retargeted by a newer native size. Synchronous helpers remain for host regression coverage. Discarding a ReplSession explicitly cancels any still-pending bridge layout before the Candidate releases service/global roots. This does not preempt source/callback execution, structural preparation or paint.
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.
+3
View File
@@ -188,3 +188,6 @@ Worker idle polls use ReplSession::poll_tasks_incremental with the current viewp
REPL and event mutations now have incremental refresh entry points. They execute source/callbacks once, retain scalar response metadata, then park layout with the current viewport target. While pending, mutation, command extraction and garbage collection wait; layout can be retargeted by a newer native size. Synchronous helpers remain for host regression coverage. Discarding a ReplSession explicitly cancels any still-pending bridge layout before the Candidate releases service/global roots. This does not preempt source/callback execution, structural preparation or paint.
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.
+168 -49
View File
@@ -92,7 +92,18 @@ fn start_server_observed(
.name("tessera-endor-worker".into())
.spawn(move || {
let mut worker = Worker::default();
let mut pending: Option<(BuildRequest, Box<PendingInput>)> = None;
loop {
if let Some((job, input)) = pending.take() {
match worker.resume_request(&job, input, &worker_namespace, &worker_notify) {
RequestProgress::Pending(input) => {
pending = Some((job, input));
thread::sleep(std::time::Duration::from_millis(1));
}
RequestProgress::Complete(response) => { let _ = job.request.respond(response); }
}
continue;
}
worker.refresh_viewport(&worker_namespace, &worker_notify);
let wait = if worker
.repl
@@ -105,8 +116,10 @@ fn start_server_observed(
};
match receiver.recv_timeout(wait) {
Ok(job) => {
let response = worker.run(&job, &worker_namespace, &worker_notify);
let _ = job.request.respond(response);
match worker.begin_request(&job, &worker_namespace, &worker_notify) {
RequestProgress::Pending(input) => pending = Some((job, input)),
RequestProgress::Complete(response) => { let _ = job.request.respond(response); }
}
}
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => break,
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
@@ -433,6 +446,40 @@ fn submit(
}
}
type HttpResponse = Response<std::io::Cursor<Vec<u8>>>;
type InputOutcome = std::thread::Result<Result<(endor_host::ReplResult, Vec<crate::window::Command>)>>;
struct PendingInput {
epoch: u64,
id: u64,
event: bool,
result: endor_host::ReplResult,
}
enum RequestProgress {
Pending(Box<PendingInput>),
Complete(HttpResponse),
}
fn current_viewports(current: &NamespaceState) -> std::collections::BTreeMap<u64, crate::layout::Size> {
let mut viewports: std::collections::BTreeMap<_, _> = current
.window_viewports
.iter()
.filter(|(id, _)| {
**id == crate::window::MAIN_WINDOW
|| (current.runtime_session != 0
&& current.document.windows.get(id).is_some_and(|view| {
view.frame.session == current.runtime_session
}))
})
.map(|(id, size)| (*id, *size))
.collect();
if let Some(viewport) = current.viewport {
viewports.insert(crate::window::MAIN_WINDOW, viewport);
}
viewports
}
#[derive(Default)]
struct Worker {
repl: Option<(u64, endor_host::ReplSession)>,
@@ -523,21 +570,7 @@ impl Worker {
) {
let (epoch, viewports) = match namespace.read() {
Ok(current) => {
let mut viewports: std::collections::BTreeMap<_, _> = current
.window_viewports
.iter()
.filter(|(id, _)| {
**id == crate::window::MAIN_WINDOW
|| (current.runtime_session != 0
&& current.document.windows.get(id).is_some_and(|view| {
view.frame.session == current.runtime_session
}))
})
.map(|(id, size)| (*id, *size))
.collect();
if let Some(viewport) = current.viewport {
viewports.insert(crate::window::MAIN_WINDOW, viewport);
}
let viewports = current_viewports(&current);
(current.submission, viewports)
}
Err(_) => return,
@@ -626,17 +659,17 @@ impl Worker {
}
}
fn run(
fn begin_request(
&mut self,
job: &BuildRequest,
namespace: &Arc<RwLock<NamespaceState>>,
notify: &Notify,
) -> Response<std::io::Cursor<Vec<u8>>> {
) -> RequestProgress {
if job.kind == RequestKind::Code {
return self.replace_program(job, namespace, notify);
return RequestProgress::Complete(self.replace_program(job, namespace, notify));
}
if job.kind == RequestKind::Expression {
return build(job, namespace, notify);
return RequestProgress::Complete(build(job, namespace, notify));
}
// Mutating requests serialize behind a pending refresh. Idle polling
// parks between quanta, but input never interleaves heap mutation with
@@ -644,10 +677,10 @@ impl Worker {
self.refresh_viewport_mode(namespace, notify, true);
let epoch = match namespace.read() {
Ok(current) => current.submission,
Err(_) => return error_response(StatusCode(500), "namespace lock is poisoned"),
Err(_) => return RequestProgress::Complete(error_response(StatusCode(500), "namespace lock is poisoned")),
};
if epoch != job.submission {
return superseded(job.submission);
return RequestProgress::Complete(superseded(job.submission));
}
if job.kind == RequestKind::ResetRepl {
self.repl = None;
@@ -662,15 +695,15 @@ impl Worker {
current.window_commands.clear();
}
notify();
return json(
return RequestProgress::Complete(json(
StatusCode(200),
serde_json::json!({"ok":true,"execution":"repl-reset","persistent":true,"session_reset":true,"message":"Console memory cleared; displayed page preserved"}),
);
));
}
let event = if job.kind == RequestKind::Event {
let input: crate::events::Input = match serde_json::from_str(&job.source) {
Ok(input) => input,
Err(error) => return error_response(StatusCode(400), error.to_string()),
Err(error) => return RequestProgress::Complete(error_response(StatusCode(400), error.to_string())),
};
if !matches!(
input.kind.as_str(),
@@ -683,20 +716,20 @@ impl Worker {
| "tab-drop"
| "window-close"
) {
return error_response(StatusCode(400), "unsupported event kind");
return RequestProgress::Complete(error_response(StatusCode(400), "unsupported event kind"));
}
if let Err(message) = input.validate() {
return error_response(StatusCode(400), message);
return RequestProgress::Complete(error_response(StatusCode(400), message));
}
let current = match namespace.read() {
Ok(current) => current,
Err(_) => return error_response(StatusCode(500), "namespace lock poisoned"),
Err(_) => return RequestProgress::Complete(error_response(StatusCode(500), "namespace lock poisoned")),
};
let frame = if input.window == crate::window::MAIN_WINDOW {
&current.document.frame
} else {
let Some(document) = current.document.windows.get(&input.window) else {
return error_response(StatusCode(409), "window is no longer active");
return RequestProgress::Complete(error_response(StatusCode(409), "window is no longer active"));
};
&document.frame
};
@@ -715,7 +748,7 @@ impl Worker {
.is_some_and(|(_, runtime)| runtime.is_faulted());
if stopped {
drop(current);
return self.close_inactive_window(&input, epoch, namespace, notify);
return RequestProgress::Complete(self.close_inactive_window(&input, epoch, namespace, notify));
}
}
if let Some(target) = &input.tab_drop {
@@ -739,10 +772,10 @@ impl Worker {
.as_ref()
.is_none_or(|(_, runtime)| !runtime.accepts_tab_drop(&input))
{
return error_response(
return RequestProgress::Complete(error_response(
StatusCode(409),
"drop targets an obsolete or inactive frame",
);
));
}
}
if frame.window != input.window
@@ -769,10 +802,10 @@ impl Worker {
|| (input.kind != "window-close" && !runtime.accepts_window(input.window))
})
{
return error_response(
return RequestProgress::Complete(error_response(
StatusCode(409),
"event targets an obsolete or inactive frame",
);
));
}
Some(input)
} else {
@@ -785,7 +818,7 @@ impl Worker {
{
self.next_session = match self.next_session.checked_add(1) {
Some(id) => id,
None => return error_response(StatusCode(500), "session identities exhausted"),
None => return RequestProgress::Complete(error_response(StatusCode(500), "session identities exhausted")),
};
let viewport = namespace.read().ok().and_then(|current| current.viewport);
match endor_host::ReplSession::new_with_viewport(self.next_session, viewport) {
@@ -797,28 +830,112 @@ impl Worker {
}
self.repl = Some((epoch, session));
}
Err(error) => return error_response(StatusCode(500), error.to_string()),
Err(error) => return RequestProgress::Complete(error_response(StatusCode(500), error.to_string())),
}
}
let viewports = match namespace.read() {
Ok(current) => current_viewports(&current),
Err(_) => return RequestProgress::Complete(error_response(StatusCode(500), "namespace lock is poisoned")),
};
let session = &mut self.repl.as_mut().expect("initialized REPL").1;
let id = session.id;
let result = catch_unwind(AssertUnwindSafe(|| -> Result<_> {
let result = if let Some(event) = &event {
session.dispatch(event)?
session.dispatch_incremental(event, &viewports)?
} else {
session.execute(&job.source, &job.namespace)?
session.execute_incremental(&job.source, &job.namespace, &viewports)?
};
Ok((result, session.take_window_commands()?))
let commands = if session.has_pending_refresh() { Vec::new() } else { session.take_window_commands()? };
Ok((result, commands))
}));
let faulted = session.is_faulted();
if session.has_pending_refresh() {
if let Ok(Ok((result, _))) = result {
return RequestProgress::Pending(Box::new(PendingInput { epoch, id, event: event.is_some(), result }));
}
// A failed start must never leave a request waiting on a broken VM.
self.repl = None;
}
RequestProgress::Complete(self.finish_input(job, namespace, notify, epoch, id, event.is_some(), faulted, result))
}
fn resume_request(
&mut self,
job: &BuildRequest,
mut input: Box<PendingInput>,
namespace: &Arc<RwLock<NamespaceState>>,
notify: &Notify,
) -> RequestProgress {
let viewports = match namespace.read() {
Ok(current) if current.submission == input.epoch || current.runtime_session == input.id => current_viewports(&current),
Ok(_) => {
self.repl = None;
return RequestProgress::Complete(superseded(job.submission));
}
Err(_) => {
self.repl = None;
return RequestProgress::Complete(error_response(StatusCode(500), "namespace lock is poisoned"));
}
};
let Some((epoch, session)) = &mut self.repl else {
return RequestProgress::Complete(error_response(StatusCode(409), "input runtime was discarded"));
};
if *epoch != input.epoch || session.id != input.id {
return RequestProgress::Complete(superseded(job.submission));
}
let resumed = catch_unwind(AssertUnwindSafe(|| -> Result<_> {
let document = session.resize_windows_incremental(&viewports, false)?;
if session.has_pending_refresh() {
return Ok(None);
}
Ok(Some((document, session.take_window_commands()?)))
}));
let faulted = session.is_faulted();
let result = match resumed {
Ok(Ok(None)) => return RequestProgress::Pending(input),
Ok(Ok(Some((document, commands)))) => {
input.result.document = document;
Ok(Ok((input.result, commands)))
}
Ok(Err(error)) => Ok(Err(error)),
Err(panic) => Err(panic),
};
RequestProgress::Complete(self.finish_input(job, namespace, notify, input.epoch, input.id, input.event, faulted, result))
}
#[allow(clippy::too_many_arguments)]
fn finish_input(
&mut self,
job: &BuildRequest,
namespace: &Arc<RwLock<NamespaceState>>,
notify: &Notify,
epoch: u64,
id: u64,
event: bool,
faulted: bool,
result: InputOutcome,
) -> HttpResponse {
let Ok(mut current) = namespace.write() else {
return error_response(StatusCode(500), "namespace lock is poisoned");
};
if current.submission != epoch {
drop(current);
self.repl = None;
return superseded(job.submission);
}
let epoch = if current.submission != epoch {
if current.runtime_session != id {
drop(current);
self.repl = None;
return superseded(job.submission);
}
// Accepted PUTs invalidate queued inputs, not a mutation already
// executing on this owner. Settle it before building the candidate
// so a failed replacement can preserve a consistent live heap.
if let Some((owner, runtime)) = &mut self.repl
&& runtime.id == id
{
*owner = current.submission;
}
current.submission
} else {
epoch
};
match result {
Ok(Ok((result, commands))) => {
let projected = !commands.is_empty();
@@ -838,11 +955,11 @@ impl Worker {
}
json(
StatusCode(200),
serde_json::json!({"ok":true,"execution":if event.is_some() { "event" } else { "repl" },"persistent":true,"session":id,"input":result.input,"value":result.value,"bindings":result.bindings,"rendered":rendered,"revision":revision}),
serde_json::json!({"ok":true,"execution":if event { "event" } else { "repl" },"persistent":true,"session":id,"input":result.input,"value":result.value,"bindings":result.bindings,"rendered":rendered,"revision":revision}),
)
}
Ok(Err(error)) => {
if event.is_some() || faulted {
if event || faulted {
let mut document = current.document.clone();
// Preserve the first failure of this VM; subsequent
// rejected Console input must not hide the callback cause.
@@ -1090,7 +1207,7 @@ its pending render is discarded; previous displayed page is preserved.
Session limits: 256 executed inputs, 1 MiB transcript, 10000 cumulative AST nodes,
4096 retained visual objects and 64 retained pages. Aster collects unreachable objects/cycles between inputs; there is no fixed UI pool. No VM handles cross to the window.
A newer accepted complete-source PUT or code DELETE invalidates queued Console inputs.
A newer accepted complete-source PUT or code DELETE invalidates queued Console inputs. A running input settles before a queued PUT candidate is built; code DELETE revokes that runtime immediately.
A successful PUT retains its fresh VM as the live program for Console and GUI events;
a failed PUT preserves the previous program. DELETE releases the live context.
GET /code returns the source of the displayed snapshot: exact submitted source for
@@ -1174,7 +1291,7 @@ source_layout::start now returns either Complete(Draft) or Pending(Work). The ho
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. ReplSession uses this protocol through its saved refresh state. Viewport refresh supports cross-worker-turn scheduling and prepared-Mount rollback; callers must not drop a pending bridge layout as a substitute for cancellation.
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 and explicit REPL/event refresh still drain synchronously. 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 loads still drain synchronously. Explicit REPL/event refresh parks 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.
@@ -1185,6 +1302,8 @@ New viewport targets now coalesce pending refresh work. The runtime cancels only
Cancellation checkpoints own explicit VM roots until restored or released. Initial presentations may have no mount dictionary; cancellation restores that absence too. Saved metadata values retain object-valued attributes. A pending refresh can restart while keeping already completed window drafts; no onMount/onUnmount callback is replayed merely because the viewport target changed. Mount-owned tasks are cancelled immediately before an actual onUnmount callback, never during abandoned preparation.
REPL/event requests retain their HTTP request and scalar result until the complete window set settles. The worker advances one stage/quantum per turn, takes current native viewport targets each time and keeps later mutations queued. GET /code keeps returning the last complete publication. Response metadata and native commands publish together after settling; no source/event callback is replayed. A running input can finish before a newer queued PUT, preserving a consistent old runtime if that candidate fails; DELETE revokes the owner and prevents its late publication. Source execution/callbacks and structural preparation/paint remain atomic.
## UI host object
Positional arguments only: