mirror of
https://github.com/lexmount/moli.git
synced 2026-11-21 00:03:15 +00:00
fix(webmcp): separate submission and response lifetimes
Cancel same-stack form removal after requestSubmit for every respondWith input, while retaining responses captured during submit dispatch or already handed to asynchronous response processing. Track submission phases through a native microtask continuation instead of inspecting Promise state. Cover scalar, fulfilled and pending native Promise, and thenable responses, manual and automatic submission, and late resolution after cancellation. Keep the existing form unmount and original WebMCP WPT assertions intact.
This commit is contained in:
@@ -129,13 +129,10 @@ pub(crate) fn note_mutation(
|
||||
if !invalidated_forms.contains(&form.handle) {
|
||||
continue;
|
||||
}
|
||||
let protected = match &form.state {
|
||||
FormInvocationState::Submitting => true,
|
||||
FormInvocationState::Responding(response) => {
|
||||
v8::Local::new(scope, response).state() == v8::PromiseState::Pending
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
let protected = matches!(
|
||||
form.state,
|
||||
FormInvocationState::Submitting | FormInvocationState::Responding
|
||||
);
|
||||
if !protected {
|
||||
form.state = FormInvocationState::Invalidated;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! Declarative execution uses native control state and the normal submit path.
|
||||
use super::super::bindings::document_owner;
|
||||
use super::super::state::{FormInvocation, FormInvocationState, PendingInvocation, ToolExecutor};
|
||||
use super::super::tasks::invocation_context;
|
||||
use super::super::{execution, navigation};
|
||||
use super::schema;
|
||||
use crate::{
|
||||
@@ -197,7 +198,11 @@ fn form_is_current(host: &PageScriptHost, id: u64, form: DomHandle) -> bool {
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn begin_form_submit(host: &mut PageScriptHost, form: DomHandle) -> Option<u64> {
|
||||
pub(crate) fn begin_form_submit(
|
||||
scope: &mut v8::PinScope<'_, '_>,
|
||||
host: &mut PageScriptHost,
|
||||
form: DomHandle,
|
||||
) -> Option<u64> {
|
||||
let store = &mut host.native_bridge_mut().web_mcp;
|
||||
let id = store.pending_form_id(form)?;
|
||||
let active = store.pending.get_mut(&id)?.form.as_mut()?;
|
||||
@@ -205,9 +210,39 @@ pub(crate) fn begin_form_submit(host: &mut PageScriptHost, form: DomHandle) -> O
|
||||
return None;
|
||||
}
|
||||
active.state = FormInvocationState::Submitting;
|
||||
// Hand response processing over at the submission's microtask checkpoint.
|
||||
// Queue before author callbacks, which can themselves await the submission.
|
||||
// Synchronous removal after requestSubmit() still cancels; once this job
|
||||
// starts processing a captured response, registration can retire separately.
|
||||
let data = v8::BigInt::new_from_u64(scope, id);
|
||||
let callback = v8::Function::builder(submission_checkpoint_callback)
|
||||
.data(data.into())
|
||||
.build(scope)
|
||||
.expect("WebMCP submission checkpoint");
|
||||
scope.enqueue_microtask(callback);
|
||||
Some(id)
|
||||
}
|
||||
|
||||
fn submission_checkpoint_callback<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
args: v8::FunctionCallbackArguments<'s>,
|
||||
_rv: v8::ReturnValue<'_, v8::Value>,
|
||||
) {
|
||||
let Some((host_ptr, id)) = invocation_context(scope, &args) else {
|
||||
return;
|
||||
};
|
||||
if let Some(form) = unsafe { &mut *host_ptr }
|
||||
.native_bridge_mut()
|
||||
.web_mcp
|
||||
.pending
|
||||
.get_mut(&id)
|
||||
.and_then(|pending| pending.form.as_mut())
|
||||
&& matches!(form.state, FormInvocationState::ResponseQueued)
|
||||
{
|
||||
form.state = FormInvocationState::Responding;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn finish_form_submit<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
host_ptr: *mut PageScriptHost,
|
||||
@@ -227,7 +262,7 @@ pub(crate) fn finish_form_submit<'s>(
|
||||
if let Some(response) =
|
||||
crate::context_bootstrap::events::take_submit_event_response(scope, event)
|
||||
{
|
||||
form.state = FormInvocationState::Responding(v8::Global::new(scope, response));
|
||||
form.state = FormInvocationState::ResponseQueued;
|
||||
execution::await_response(scope, host_ptr, id, response);
|
||||
} else {
|
||||
let form_handle = form.handle;
|
||||
|
||||
@@ -113,7 +113,8 @@ pub(super) enum FormInvocationState {
|
||||
Filling,
|
||||
Ready,
|
||||
Submitting,
|
||||
Responding(v8::Global<v8::Promise>),
|
||||
ResponseQueued,
|
||||
Responding,
|
||||
Invalidated,
|
||||
Navigating,
|
||||
}
|
||||
|
||||
@@ -230,6 +230,7 @@ fn submit_form_with_submit_event_inner(
|
||||
user_initiated: bool,
|
||||
) -> bool {
|
||||
let invocation = crate::context_bootstrap::web_mcp::begin_form_submit(
|
||||
scope,
|
||||
unsafe { &mut *runtime_ptr },
|
||||
form_handle,
|
||||
);
|
||||
|
||||
@@ -300,6 +300,89 @@ fn web_mcp_captured_form_response_survives_removal_and_unrelated_mutations() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn web_mcp_manual_submission_removal_cancels_before_response_processing() {
|
||||
for response in [
|
||||
"'result'",
|
||||
"Promise.resolve('result')",
|
||||
"new Promise(resolve => globalThis.finish = resolve)",
|
||||
"({then(resolve) {globalThis.finish = resolve}})",
|
||||
] {
|
||||
let mut vm = new_storage_page_task_executor_test_vm("https://tools.test/");
|
||||
vm.eval(&format!(r#"
|
||||
document.body.innerHTML='<form toolname=respond tooldescription=Respond><button>Submit</button></form>';
|
||||
globalThis.form=document.querySelector('form');
|
||||
globalThis.probe='pending';
|
||||
form.addEventListener('submit', event=>{{
|
||||
event.preventDefault();
|
||||
event.respondWith({response});
|
||||
}});
|
||||
"#)).unwrap();
|
||||
vm.eval_after_selected_page_tasks("undefined").unwrap();
|
||||
vm.eval("document.modelContext.getTools().then(([tool])=>document.modelContext.executeTool(tool)).then(value=>probe=value,error=>probe=error.name)").unwrap();
|
||||
assert_eq!(
|
||||
vm.eval_after_selected_page_tasks(
|
||||
"document.activeElement===form.querySelector('button')"
|
||||
)
|
||||
.unwrap(),
|
||||
"true",
|
||||
"{response}"
|
||||
);
|
||||
vm.eval("form.requestSubmit(); form.remove()").unwrap();
|
||||
assert_eq!(
|
||||
vm.eval_after_selected_page_tasks("probe+'|'+form.matches(':tool-form-active')")
|
||||
.unwrap(),
|
||||
"UnknownError|false",
|
||||
"{response}"
|
||||
);
|
||||
vm.eval("if (typeof finish==='function') finish('late result')")
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
vm.eval_after_selected_page_tasks("probe").unwrap(),
|
||||
"UnknownError",
|
||||
"{response}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn web_mcp_form_response_survives_removal_after_submission_yields() {
|
||||
for autosubmit in [false, true] {
|
||||
for response in [
|
||||
"'result'",
|
||||
"Promise.resolve('result')",
|
||||
"new Promise(resolve => globalThis.finish = resolve)",
|
||||
"({then(resolve) {globalThis.finish = resolve}})",
|
||||
] {
|
||||
let mut vm = new_storage_page_task_executor_test_vm("https://tools.test/");
|
||||
let attribute = if autosubmit { "toolautosubmit" } else { "" };
|
||||
vm.eval(&format!(r#"
|
||||
document.body.innerHTML='<form toolname=respond tooldescription=Respond {attribute}><button>Submit</button></form>';
|
||||
globalThis.form=document.querySelector('form');
|
||||
globalThis.probe='pending';
|
||||
form.addEventListener('submit', event=>{{
|
||||
event.preventDefault();
|
||||
event.respondWith({response});
|
||||
}});
|
||||
"#)).unwrap();
|
||||
vm.eval_after_selected_page_tasks("undefined").unwrap();
|
||||
vm.eval("document.modelContext.getTools().then(([tool])=>document.modelContext.executeTool(tool)).then(value=>probe=value,error=>probe=error.name)").unwrap();
|
||||
vm.eval_after_selected_page_tasks("undefined").unwrap();
|
||||
if !autosubmit {
|
||||
vm.eval("form.requestSubmit()").unwrap();
|
||||
}
|
||||
vm.eval("form.remove(); if (typeof finish==='function') finish('result')")
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
vm.eval_after_selected_page_tasks("probe+'|'+form.matches(':tool-form-active')")
|
||||
.unwrap(),
|
||||
"result|false",
|
||||
"autosubmit={autosubmit}, response={response}"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn web_mcp_removed_form_releases_its_registration_synchronously() {
|
||||
let mut vm = new_storage_page_task_executor_test_vm("https://tools.test/");
|
||||
|
||||
Reference in New Issue
Block a user