feat: retain managed visual objects and dispatch native input through Endor
Verify Tessera GUI / verify (push) Successful in 9m28s
Verify Tessera GUI / verify (push) Successful in 9m28s
This commit is contained in:
+174
-16
@@ -27,6 +27,7 @@ type Notify = Arc<dyn Fn() + Send + Sync>;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum RequestKind {
|
||||
Event,
|
||||
Code,
|
||||
Expression,
|
||||
Repl,
|
||||
@@ -91,9 +92,23 @@ fn start_server_observed(
|
||||
.name("tessera-endor-worker".into())
|
||||
.spawn(move || {
|
||||
let mut worker = Worker::default();
|
||||
for job in receiver {
|
||||
let response = worker.run(&job, &worker_namespace, &worker_notify);
|
||||
let _ = job.request.respond(response);
|
||||
loop {
|
||||
match receiver.recv_timeout(std::time::Duration::from_millis(100)) {
|
||||
Ok(job) => {
|
||||
let response = worker.run(&job, &worker_namespace, &worker_notify);
|
||||
let _ = job.request.respond(response);
|
||||
}
|
||||
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => break,
|
||||
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
|
||||
if worker.repl.as_ref().is_some_and(|(epoch, _)| {
|
||||
worker_namespace
|
||||
.read()
|
||||
.is_ok_and(|current| current.submission != *epoch)
|
||||
}) {
|
||||
worker.repl = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.context("start Aster execution worker")?;
|
||||
@@ -209,10 +224,13 @@ fn handle(
|
||||
|
||||
let evaluation = namespace_from_route(&path, "/evaluate");
|
||||
let repl = namespace_from_route(&path, "/repl");
|
||||
let event = namespace_from_route(&path, "/events");
|
||||
let route = if method == Method::Put {
|
||||
namespace_from_path(&path).map(|name| (name, RequestKind::Code))
|
||||
} else if method == Method::Post {
|
||||
repl.map(|name| (name, RequestKind::Repl))
|
||||
event
|
||||
.map(|name| (name, RequestKind::Event))
|
||||
.or_else(|| repl.map(|name| (name, RequestKind::Repl)))
|
||||
.or(evaluation.map(|name| (name, RequestKind::Expression)))
|
||||
} else if method == Method::Delete {
|
||||
repl.map(|name| (name, RequestKind::ResetRepl))
|
||||
@@ -390,10 +408,10 @@ impl Worker {
|
||||
namespace: &Arc<RwLock<NamespaceState>>,
|
||||
notify: &Notify,
|
||||
) -> Response<std::io::Cursor<Vec<u8>>> {
|
||||
if matches!(job.kind, RequestKind::Code | RequestKind::Expression) {
|
||||
if job.kind == RequestKind::Code {
|
||||
self.repl = None;
|
||||
}
|
||||
if job.kind == RequestKind::Code {
|
||||
return self.replace_program(job, namespace, notify);
|
||||
}
|
||||
if job.kind == RequestKind::Expression {
|
||||
return build(job, namespace, notify);
|
||||
}
|
||||
let epoch = match namespace.read() {
|
||||
@@ -405,11 +423,49 @@ impl Worker {
|
||||
}
|
||||
if job.kind == RequestKind::ResetRepl {
|
||||
self.repl = None;
|
||||
if let Ok(mut current) = namespace.write() {
|
||||
current.document.frame.session = 0;
|
||||
current.document.frame.focus = None;
|
||||
}
|
||||
notify();
|
||||
return 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()),
|
||||
};
|
||||
if !matches!(input.kind.as_str(), "click" | "focus") {
|
||||
return error_response(StatusCode(400), "unsupported event kind");
|
||||
}
|
||||
let current = match namespace.read() {
|
||||
Ok(current) => current,
|
||||
Err(_) => return error_response(StatusCode(500), "namespace lock poisoned"),
|
||||
};
|
||||
let frame = ¤t.document.frame;
|
||||
if frame.session != input.session
|
||||
|| frame.layout_version != input.layout_version
|
||||
|| frame.fault.is_some()
|
||||
|| !frame
|
||||
.mounts
|
||||
.iter()
|
||||
.any(|mount| mount.id == input.mount && mount.interactive)
|
||||
|| self.repl.as_ref().is_none_or(|(context, runtime)| {
|
||||
*context != epoch || runtime.id != input.session
|
||||
})
|
||||
{
|
||||
return error_response(
|
||||
StatusCode(409),
|
||||
"event targets an obsolete or inactive frame",
|
||||
);
|
||||
}
|
||||
Some(input)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if self
|
||||
.repl
|
||||
.as_ref()
|
||||
@@ -424,8 +480,13 @@ impl Worker {
|
||||
let session = &mut self.repl.as_mut().expect("initialized REPL").1;
|
||||
let id = session.id;
|
||||
let result = catch_unwind(AssertUnwindSafe(|| {
|
||||
session.execute(&job.source, &job.namespace)
|
||||
if let Some(event) = &event {
|
||||
session.dispatch(event)
|
||||
} else {
|
||||
session.execute(&job.source, &job.namespace)
|
||||
}
|
||||
}));
|
||||
let faulted = session.is_faulted();
|
||||
let Ok(mut current) = namespace.write() else {
|
||||
return error_response(StatusCode(500), "namespace lock is poisoned");
|
||||
};
|
||||
@@ -451,13 +512,27 @@ impl Worker {
|
||||
}
|
||||
json(
|
||||
StatusCode(200),
|
||||
serde_json::json!({"ok":true,"execution":"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.is_some() { "event" } else { "repl" },"persistent":true,"session":id,"input":result.input,"value":result.value,"bindings":result.bindings,"rendered":rendered,"revision":revision}),
|
||||
)
|
||||
}
|
||||
Ok(Err(error)) => compile_error(error),
|
||||
Ok(Err(error)) => {
|
||||
if event.is_some() || faulted {
|
||||
let mut document = current.document.clone();
|
||||
document.frame.fault = Some(format!("{error:#}").chars().take(1024).collect());
|
||||
let _ = current.commit(epoch, document);
|
||||
drop(current);
|
||||
notify();
|
||||
}
|
||||
compile_error(error)
|
||||
}
|
||||
Err(_) => {
|
||||
let mut document = current.document.clone();
|
||||
document.frame.fault =
|
||||
Some("Runtime discarded after an internal execution failure".into());
|
||||
let _ = current.commit(epoch, document);
|
||||
drop(current);
|
||||
self.repl = None;
|
||||
notify();
|
||||
json(
|
||||
StatusCode(500),
|
||||
serde_json::json!({"ok":false,"session_reset":true,"error":"Console execution failed internally; session discarded, displayed page preserved"}),
|
||||
@@ -465,6 +540,66 @@ impl Worker {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn replace_program(
|
||||
&mut self,
|
||||
job: &BuildRequest,
|
||||
namespace: &Arc<RwLock<NamespaceState>>,
|
||||
notify: &Notify,
|
||||
) -> Response<std::io::Cursor<Vec<u8>>> {
|
||||
if namespace
|
||||
.read()
|
||||
.is_ok_and(|current| current.submission != job.submission)
|
||||
{
|
||||
return superseded(job.submission);
|
||||
}
|
||||
self.next_session = match self.next_session.checked_add(1) {
|
||||
Some(id) => id,
|
||||
None => return error_response(StatusCode(500), "session identities exhausted"),
|
||||
};
|
||||
let id = self.next_session;
|
||||
let result = catch_unwind(AssertUnwindSafe(|| {
|
||||
endor_host::ReplSession::load(&job.source, &job.namespace, job.submission, id)
|
||||
}));
|
||||
let Ok(mut current) = namespace.write() else {
|
||||
return error_response(StatusCode(500), "namespace lock poisoned");
|
||||
};
|
||||
if current.submission != job.submission {
|
||||
return superseded(job.submission);
|
||||
}
|
||||
match result {
|
||||
Ok(Ok((runtime, document))) => match current.commit(job.submission, document) {
|
||||
Ok(Some(revision)) => {
|
||||
self.repl = Some((job.submission, runtime));
|
||||
drop(current);
|
||||
notify();
|
||||
json(
|
||||
StatusCode(200),
|
||||
serde_json::json!({"ok":true,"namespace":job.namespace,"submission":job.submission,"revision":revision,"diagnostics":[],"session":id,"persistent":true}),
|
||||
)
|
||||
}
|
||||
Ok(None) => superseded(job.submission),
|
||||
Err(error) => error_response(StatusCode(500), error.to_string()),
|
||||
},
|
||||
failed => {
|
||||
// The candidate never mutated the displayed program's heap.
|
||||
// Restore that program's event epoch when a replacement fails.
|
||||
if let Some((epoch, runtime)) = &mut self.repl
|
||||
&& current.document.frame.session == runtime.id
|
||||
{
|
||||
*epoch = job.submission;
|
||||
}
|
||||
drop(current);
|
||||
match failed {
|
||||
Ok(Err(error)) => compile_error(error),
|
||||
_ => error_response(
|
||||
StatusCode(500),
|
||||
"candidate failed internally; previous runtime preserved",
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn build(
|
||||
@@ -595,9 +730,10 @@ execution. On runtime error earlier statements in that input may remain live, bu
|
||||
its pending render is discarded; previous displayed page is preserved.
|
||||
|
||||
Session limits: 256 executed inputs, 1 MiB transcript, 10000 cumulative AST nodes,
|
||||
4096 retained UI nodes and 64 retained pages. Unused slots are reclaimed between inputs; reset releases the session. No VM handles cross to the window.
|
||||
A newer accepted complete-source PUT or code DELETE invalidates queued Console
|
||||
inputs and resets the live context. They retain their existing snapshot semantics.
|
||||
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 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
|
||||
PUT, an annotated Console transcript after a REPL render. X-Tessera-Source-Kind
|
||||
identifies source-snapshot versus repl-transcript; a transcript is not a replay log.
|
||||
@@ -608,6 +744,25 @@ It does not publish a page or change revision. Console does not use this endpoin
|
||||
All execution endpoints share the eight-entry queue. These are in-process bounded
|
||||
host executions, not a complete untrusted-code sandbox or a process-isolation design.
|
||||
|
||||
## Managed visual types
|
||||
|
||||
`Object`, `Type`, `Function`, `Block`, `ContainerBlock`, `TextBlock`, `RectBlock`,
|
||||
`Spacer`, `Row`, `Column`, `Button`, `Page`, `Mount`, and `Event` are managed nominal
|
||||
Type objects. Use `page = Page()` and `label = TextBlock("Hello")` in Endor source.
|
||||
The existing ui constructors return these same typed objects. `page.type`,
|
||||
`Page.base`, `Page.name`, `isInstance(page, Block)` and `isSubtype(Page, Block)`
|
||||
expose actual type relations. Types, bases, instances and native constructors live
|
||||
in the same GC graph. Ordinary source classes support `class Child: Base`, typed field
|
||||
declarations, explicit first `self` parameters, positional field construction and
|
||||
member assignment. Inherited methods bind the actual receiver; overridden methods
|
||||
are dynamically selected through self. Native allocator subclasses and traits remain
|
||||
pending. Native Block/Page methods use the same type method tables and binding path.
|
||||
|
||||
Native input POST /events is a transport envelope, not Endor source. The window
|
||||
sends current session, layout_version and Mount identity; stale positions fail
|
||||
without calling a callback. Native mouse, Tab, Enter and Space delegate to the same
|
||||
managed click/focus methods used by code. Events appear in Network, not Console source history.
|
||||
|
||||
## UI host object
|
||||
|
||||
Positional arguments only:
|
||||
@@ -634,8 +789,11 @@ work (409); a full eight-entry queue returns 503. DELETE immediately clears the
|
||||
document. GET remains available while Aster executes. The window redraws from an
|
||||
owned snapshot; resize does not rerun Endor. Text uses system fonts, antialiasing
|
||||
and font fallback; Chinese needs an installed CJK font. Size 1 is 14 logical pixels
|
||||
with 20-pixel line height; larger sizes multiply both. This first stage builds static UI;
|
||||
event callbacks and process isolation remain future work. Console retains objects
|
||||
with 20-pixel line height; larger sizes multiply both. Native Block methods include
|
||||
setText/setColor, container append/remove/replace/move, Page open/close/navigation,
|
||||
and code-driven button.onClick/click. ui.state and ui.textOf provide explicit text
|
||||
binding. Mouse routing, tab chrome/multiple windows and incremental layout remain
|
||||
in progress; process isolation is future work. Console retains objects
|
||||
only in its explicitly resettable in-memory VM. It is not an untrusted-code sandbox.
|
||||
"##,
|
||||
name = namespace.name,
|
||||
|
||||
Reference in New Issue
Block a user