feat: add system widgets and a persistent native Console with bare assignments
Verify Tessera GUI / verify (push) Failing after 7m17s

This commit is contained in:
2026-10-20 18:36:48 +08:00
parent 1d4b1d3c86
commit 53e8d7b287
15 changed files with 1549 additions and 245 deletions
+218 -27
View File
@@ -25,9 +25,17 @@ pub const API_ADDRESS: &str = "127.0.0.1:4737";
const MAX_CODE_SIZE: u64 = endor_host::MAX_SOURCE_BYTES as u64;
type Notify = Arc<dyn Fn() + Send + Sync>;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum RequestKind {
Code,
Expression,
Repl,
ResetRepl,
}
struct BuildRequest {
request: ObservedRequest,
evaluate: bool,
kind: RequestKind,
source: String,
namespace: String,
submission: u64,
@@ -82,8 +90,9 @@ fn start_server_observed(
thread::Builder::new()
.name("tessera-endor-worker".into())
.spawn(move || {
let mut worker = Worker::default();
for job in receiver {
let response = build(&job, &worker_namespace, &worker_notify);
let response = worker.run(&job, &worker_namespace, &worker_notify);
let _ = job.request.respond(response);
}
})
@@ -198,16 +207,20 @@ fn handle(
.unwrap_or(request.url())
.to_owned();
let evaluation = path
.strip_suffix("/evaluate")
.and_then(|prefix| prefix.strip_prefix("/v1/namespaces/"));
if (method == Method::Put || method == Method::Post)
&& let Some(addressed) = namespace_from_path(&path)
.filter(|_| method == Method::Put)
.or(evaluation
.filter(|name| method == Method::Post && !name.is_empty() && !name.contains('/')))
{
submit(request, addressed, namespace, sender, evaluation.is_some());
let evaluation = namespace_from_route(&path, "/evaluate");
let repl = namespace_from_route(&path, "/repl");
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))
.or(evaluation.map(|name| (name, RequestKind::Expression)))
} else if method == Method::Delete {
repl.map(|name| (name, RequestKind::ResetRepl))
} else {
None
};
if let Some((addressed, kind)) = route {
submit(request, addressed, namespace, sender, kind);
return;
}
let response = if method == Method::Get && path == "/agent" {
@@ -256,6 +269,20 @@ fn code_request(
"X-Tessera-Revision",
&current.revision.to_string(),
);
add_header(
&mut response,
"X-Tessera-Source-Kind",
if current
.document
.root
.as_ref()
.is_some_and(|root| root.kind == "repl-session")
{
"repl-transcript"
} else {
"source-snapshot"
},
);
return response;
}
drop(current);
@@ -290,7 +317,7 @@ fn submit(
addressed: &str,
namespace: &Arc<RwLock<NamespaceState>>,
sender: &SyncSender<BuildRequest>,
evaluate: bool,
kind: RequestKind,
) {
let source = match read_code(&mut request) {
Ok(source) => source,
@@ -311,7 +338,11 @@ fn submit(
let _ = request.respond(error_response(StatusCode(404), "namespace is not active"));
return;
}
let submission = match current.next_submission() {
let submission = match if kind == RequestKind::Code {
current.next_submission()
} else {
Ok(current.submission)
} {
Ok(id) => id,
Err(error) => {
let _ = request.respond(error_response(StatusCode(500), error.to_string()));
@@ -320,7 +351,7 @@ fn submit(
};
let job = BuildRequest {
request,
evaluate,
kind,
source,
namespace: addressed.into(),
submission,
@@ -329,7 +360,7 @@ fn submit(
// A rejected queue entry never invalidates accepted work. The worker
// uses this same lock when committing, so publication is atomic.
Ok(()) => {
if !evaluate {
if kind == RequestKind::Code {
current.submission = submission;
}
}
@@ -347,12 +378,101 @@ fn submit(
}
}
#[derive(Default)]
struct Worker {
repl: Option<(u64, endor_host::ReplSession)>,
next_session: u64,
}
impl Worker {
fn run(
&mut self,
job: &BuildRequest,
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;
}
return build(job, namespace, notify);
}
let epoch = match namespace.read() {
Ok(current) => current.submission,
Err(_) => return error_response(StatusCode(500), "namespace lock is poisoned"),
};
if epoch != job.submission {
return superseded(job.submission);
}
if job.kind == RequestKind::ResetRepl {
self.repl = None;
return json(
StatusCode(200),
serde_json::json!({"ok":true,"execution":"repl-reset","persistent":true,"session_reset":true,"message":"Console memory cleared; displayed page preserved"}),
);
}
if self
.repl
.as_ref()
.is_none_or(|(current, _)| *current != epoch)
{
self.next_session += 1;
match endor_host::ReplSession::new(self.next_session) {
Ok(session) => self.repl = Some((epoch, session)),
Err(error) => return error_response(StatusCode(500), error.to_string()),
}
}
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)
}));
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);
}
match result {
Ok(Ok(result)) => {
let rendered = result.document.is_some();
if let Some(document) = result.document {
match current.commit(epoch, document) {
Ok(Some(_)) => {}
Ok(None) => return superseded(job.submission),
Err(error) => return error_response(StatusCode(500), error.to_string()),
}
}
let revision = current.revision;
drop(current);
if rendered {
notify();
}
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}),
)
}
Ok(Err(error)) => compile_error(error),
Err(_) => {
drop(current);
self.repl = None;
json(
StatusCode(500),
serde_json::json!({"ok":false,"session_reset":true,"error":"Console execution failed internally; session discarded, displayed page preserved"}),
)
}
}
}
}
fn build(
job: &BuildRequest,
namespace: &Arc<RwLock<NamespaceState>>,
notify: &Notify,
) -> Response<std::io::Cursor<Vec<u8>>> {
if job.evaluate {
if job.kind == RequestKind::Expression {
return match catch_unwind(AssertUnwindSafe(|| endor_host::evaluate(&job.source))) {
Ok(Ok(value)) => json(
StatusCode(200),
@@ -408,11 +528,14 @@ fn superseded(submission: u64) -> Response<std::io::Cursor<Vec<u8>>> {
)
}
fn namespace_from_path(path: &str) -> Option<&str> {
fn namespace_from_route<'a>(path: &'a str, suffix: &str) -> Option<&'a str> {
path.strip_prefix("/v1/namespaces/")?
.strip_suffix("/code")
.strip_suffix(suffix)
.filter(|namespace| !namespace.is_empty() && !namespace.contains('/'))
}
fn namespace_from_path(path: &str) -> Option<&str> {
namespace_from_route(path, "/code")
}
fn read_code(request: &mut Request) -> Result<String> {
if request
@@ -456,13 +579,34 @@ Send PUT bodies as `text/plain; charset=utf-8`.
```endor
{example}```
## Isolated expression evaluation
## Live Console session
`POST http://{address}/v1/namespaces/{name}/evaluate` accepts one Endor expression
as text/plain and returns its value. It uses a fresh VM without ui or prior bindings,
does not publish a page or change the revision, and shares the bounded worker queue.
Persistent REPL sessions are not available. The Console is an activity transcript,
not a persistent VM; history is never replayed. PUT remains a complete source replacement.
`POST http://{address}/v1/namespaces/{name}/repl` accepts statements, function
definitions and expressions as text/plain. Consecutive requests use one real VM
and globals dictionary on the execution worker. `title = ui.text("Hello")` creates
an object without displaying it. A later `page.render(root)` publishes its page.
`let title = ...` is also supported. `_` retains the actual last value. No input
history is replayed. Responses include session/input IDs, value, binding count,
rendered flag and revision. Without render the displayed page/revision stay unchanged.
`DELETE http://{address}/v1/namespaces/{name}/repl` resets Console objects and bindings,
leaving the displayed native snapshot intact. Parsing/compilation happen before
execution. On runtime error earlier statements in that input may remain live, but
its pending render is discarded; previous displayed page is preserved.
Session limits: 256 executed inputs, 1 MiB transcript, 10000 cumulative AST nodes,
4096 UI nodes and 64 pages. Reset to reclaim them. 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.
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.
The compatibility `POST http://{address}/v1/namespaces/{name}/evaluate` endpoint
still accepts one isolated expression in a fresh VM without ui or previous bindings.
It does not publish a page or change revision. Console does not use this endpoint.
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.
## UI host object
@@ -491,8 +635,8 @@ 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;
events, persistent VM state and process isolation remain
future work. It is not an untrusted-code sandbox.
event callbacks and process isolation remain future work. Console retains objects
only in its explicitly resettable in-memory VM. It is not an untrusted-code sandbox.
"##,
name = namespace.name,
revision = namespace.revision,
@@ -608,6 +752,53 @@ mod tests {
}
}
#[test]
fn console_keeps_memory_until_reset_or_complete_put_without_erasing_the_page() {
let server = Arc::new(Server::http("127.0.0.1:0").unwrap());
let namespace = Arc::new(RwLock::new(NamespaceState::default()));
let api = start_server(server.clone(), namespace.clone(), Arc::new(|| {})).unwrap();
let client = TestServer {
address: server.server_addr().to_ip().unwrap(),
server,
api: Some(api),
};
let repl = "/v1/namespaces/app.main/repl";
let code = "/v1/namespaces/app.main/code";
let created = client.request("POST", repl, "title = ui.text(\"中文\")");
assert_eq!(created.0, 200, "{}", created.2);
let created: serde_json::Value = serde_json::from_str(&created.2).unwrap();
assert_eq!(created["rendered"], false);
assert_eq!(created["revision"], 0);
let read: serde_json::Value =
serde_json::from_str(&client.request("POST", repl, "title").2).unwrap();
assert_eq!(read["value"], created["value"]);
assert_eq!(
client.request("POST", repl, "ui.page().render(title)").0,
200
);
let snapshot = client.request("GET", code, "");
assert!(
snapshot
.1
.to_lowercase()
.contains("x-tessera-source-kind: repl-transcript")
);
assert_eq!(namespace.read().unwrap().revision, 1);
assert_eq!(client.request("DELETE", repl, "").0, 200);
assert_eq!(client.request("GET", code, "").2, snapshot.2);
assert_eq!(client.request("POST", repl, "title").0, 422);
assert_eq!(namespace.read().unwrap().revision, 1);
assert_eq!(client.request("POST", repl, "retained = 42").0, 200);
assert_eq!(
client
.request("PUT", code, include_str!("../examples/page.endor"))
.0,
200
);
assert_eq!(client.request("POST", repl, "retained").0, 422);
assert_eq!(namespace.read().unwrap().revision, 2);
}
#[test]
fn http_executes_real_endor_preserves_failed_updates_and_supports_delete() {
let server = Arc::new(Server::http("127.0.0.1:0").unwrap());