OCaml evaluator for page dispatch + handler aser, 83/83 Playwright tests

Major architectural change: page function dispatch and handler execution
now go through the OCaml kernel instead of the Python bootstrapped evaluator.

OCaml integration:
- Page dispatch: bridge.eval() evaluates SX URL expressions (geography, marshes, etc.)
- Handler aser: bridge.aser() serializes handler responses as SX wire format
- _ensure_components loads all .sx files into OCaml kernel (spec, web adapter, handlers)
- defhandler/defpage registered as no-op special forms so handler files load
- helper IO primitive dispatches to Python page helpers + IO handlers
- ok-raw response format for SX wire format (no double-escaping)
- Natural list serialization in eval (no (list ...) wrapper)
- Clean pipe: _read_until_ok always sends io-response on error

SX adapter (aser):
- scope-emit!/scope-peek aliases to avoid CEK special form conflict
- aser-fragment/aser-call: strings starting with "(" pass through unserialized
- Registered cond-scheme?, is-else-clause?, primitive?, get-primitive in kernel
- random-int, parse-int as kernel primitives; json-encode, into via IO bridge

Handler migration:
- All IO calls converted to (helper "name" args...) pattern
- request-arg, request-form, state-get, state-set!, now, component-source etc.
- Fixed bare (effect ...) in island bodies leaking disposer functions as text
- Fixed lower-case → lower, ~search-results → ~examples/search-results

Reactive islands:
- sx-hydrate-islands called after client-side navigation swap
- force-dispose-islands-in for outerHTML swaps (clears hydration markers)
- clear-processed! platform primitive for re-hydration

Content restructuring:
- Design, event bridge, named stores, phase 2 consolidated into reactive overview
- Marshes split into overview + 5 example sub-pages
- Nav links use sx-get/sx-target for client-side navigation

Playwright test suite (sx/tests/test_demos.py):
- 83 tests covering hypermedia demos, reactive islands, marshes, spec explorer
- Server-side rendering, handler interactions, island hydration, navigation

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-18 17:22:51 +00:00
parent 5b6e883e6d
commit 71c2003a60
33 changed files with 1848 additions and 852 deletions

View File

@@ -1,7 +1,7 @@
;; SX docs — documentation page components
(defcomp ~docs/page (&key title &rest children)
(div :class "max-w-4xl mx-auto px-6 py-8"
(div :class "max-w-4xl mx-auto px-6 pb-8 pt-4"
(div :class "prose prose-stone max-w-none space-y-6" children)))
(defcomp ~docs/section (&key title id &rest children)

View File

@@ -7,7 +7,7 @@
;; ---------------------------------------------------------------------------
(defhandler ref-time (&key)
(let ((now (format-time (now) "%H:%M:%S")))
(let ((now (helper "now" "%H:%M:%S")))
(span :class "text-stone-800 text-sm"
"Server time: " (strong now))))
@@ -49,7 +49,7 @@
;; ---------------------------------------------------------------------------
(defhandler ref-trigger-search (&key)
(let ((q (or (request-arg "q") "")))
(let ((q (helper "request-arg" "q" "")))
(if (empty? q)
(span :class "text-stone-400 text-sm" "Start typing to trigger a search.")
(span :class "text-stone-800 text-sm"
@@ -60,7 +60,7 @@
;; ---------------------------------------------------------------------------
(defhandler ref-swap-item (&key)
(let ((now (format-time (now) "%H:%M:%S")))
(let ((now (helper "now" "%H:%M:%S")))
(div :class "text-sm text-violet-700"
"New item (" now ")")))
@@ -69,7 +69,7 @@
;; ---------------------------------------------------------------------------
(defhandler ref-oob (&key)
(let ((now (format-time (now) "%H:%M:%S")))
(let ((now (helper "now" "%H:%M:%S")))
(<>
(span :class "text-emerald-700 text-sm"
"Main updated at " now)
@@ -82,7 +82,7 @@
;; ---------------------------------------------------------------------------
(defhandler ref-select-page (&key)
(let ((now (format-time (now) "%H:%M:%S")))
(let ((now (helper "now" "%H:%M:%S")))
(<>
(div :id "the-header"
(h3 "Page header — not selected"))
@@ -98,7 +98,7 @@
(defhandler ref-slow-echo (&key)
(sleep 800)
(let ((q (or (request-arg "q") "")))
(let ((q (helper "request-arg" "q" "")))
(span :class "text-stone-800 text-sm"
"Echo: " (strong q))))
@@ -116,7 +116,7 @@
;; ---------------------------------------------------------------------------
(defhandler ref-echo-headers (&key)
(let ((headers (request-headers :prefix "X-")))
(let ((headers (helper "request-headers" :prefix "X-")))
(if (empty? headers)
(span :class "text-stone-400 text-sm" "No custom headers received.")
(ul :class "text-sm text-stone-700 space-y-1"
@@ -129,7 +129,7 @@
;; ---------------------------------------------------------------------------
(defhandler ref-echo-vals (&key)
(let ((vals (request-args)))
(let ((vals (helper "request-args")))
(if (empty? vals)
(span :class "text-stone-400 text-sm" "No values received.")
(ul :class "text-sm text-stone-700 space-y-1"

View File

@@ -29,5 +29,8 @@ def _load_sx_page_files() -> None:
helpers = get_page_helpers("sx")
for name, fn in helpers.items():
PRIMITIVES[name] = fn
# helper is registered as an IO primitive in primitives_io.py,
# intercepted by async_eval before hitting the CEK machine.
import logging; logging.getLogger("sx.pages").info("Injected %d page helpers as primitives: %s", len(helpers), list(helpers.keys())[:5])
load_page_dir(os.path.dirname(__file__), "sx")

View File

@@ -369,17 +369,11 @@
:path "/language/specs/explore/<slug>"
:auth :public
:layout :sx-docs
:data (helper "spec-explorer-data-by-slug" slug)
:content (~layouts/doc :path (str "/sx/(language.(spec.(explore." slug ")))")
(let ((spec (find-spec slug)))
(if spec
(let ((data (spec-explorer-data
(get spec "filename")
(get spec "title")
(get spec "desc"))))
(if data
(~specs-explorer/spec-explorer-content :data data)
(~specs/not-found :slug slug)))
(~specs/not-found :slug slug)))))
(if data
(~specs-explorer/spec-explorer-content :data data)
(~specs/not-found :slug slug))))
;; ---------------------------------------------------------------------------
;; Bootstrappers section

View File

@@ -35,6 +35,7 @@ def _register_sx_helpers() -> None:
"prove-data": _prove_data,
"page-helpers-demo-data": _page_helpers_demo_data,
"spec-explorer-data": _spec_explorer_data,
"spec-explorer-data-by-slug": _spec_explorer_data_by_slug,
"handler-source": _handler_source,
})
@@ -329,6 +330,35 @@ def _collect_symbols(expr) -> set[str]:
return result
_SPEC_SLUG_MAP = {
"parser": ("parser.sx", "Parser", "Tokenization and parsing"),
"evaluator": ("eval.sx", "Evaluator", "Tree-walking evaluation"),
"primitives": ("primitives.sx", "Primitives", "Built-in pure functions"),
"render": ("render.sx", "Renderer", "Three rendering modes"),
"special-forms": ("special-forms.sx", "Special Forms", "Special form dispatch"),
"signals": ("signals.sx", "Signals", "Fine-grained reactive primitives"),
"adapter-dom": ("adapter-dom.sx", "DOM Adapter", "Client-side DOM rendering"),
"adapter-html": ("adapter-html.sx", "HTML Adapter", "Server-side HTML rendering"),
"adapter-sx": ("adapter-sx.sx", "SX Adapter", "SX wire format serialization"),
"engine": ("engine.sx", "SxEngine", "Pure logic for the browser engine"),
"orchestration": ("orchestration.sx", "Orchestration", "Browser lifecycle"),
"boot": ("boot.sx", "Boot", "Browser initialization"),
"router": ("router.sx", "Router", "URL parsing and route matching"),
"boundary": ("boundary.sx", "Boundary", "Language/platform boundary"),
"continuations": ("continuations.sx", "Continuations", "Delimited continuations"),
"types": ("types.sx", "Types", "Optional gradual type system"),
}
def _spec_explorer_data_by_slug(slug: str) -> dict | None:
"""Look up spec by slug and return explorer data."""
entry = _SPEC_SLUG_MAP.get(slug)
if not entry:
return None
filename, title, desc = entry
return _spec_explorer_data(filename, title, desc)
def _spec_explorer_data(filename: str, title: str = "", desc: str = "") -> dict | None:
"""Parse a spec file into structured metadata for the spec explorer.

View File

@@ -150,16 +150,40 @@ async def eval_sx_url(raw_path: str) -> Any:
page_ast = expr
else:
import os
if os.environ.get("SX_USE_REF") == "1":
from shared.sx.ref.async_eval_ref import async_eval
else:
from shared.sx.async_eval import async_eval
use_ocaml = os.environ.get("SX_USE_OCAML") == "1"
try:
page_ast = await async_eval(expr, env, ctx)
except Exception as e:
logger.error("SX URL page-fn eval failed for %s: %s", raw_path, e, exc_info=True)
return None
if use_ocaml:
# OCaml kernel — the universal evaluator
try:
from shared.sx.ocaml_bridge import get_bridge
from shared.sx.parser import serialize, parse_all
bridge = await get_bridge()
sx_text = serialize(expr)
ocaml_ctx = {"_helper_service": "sx"}
result_text = await bridge.eval(sx_text, ctx=ocaml_ctx)
if result_text:
parsed = parse_all(result_text)
page_ast = parsed[0] if parsed else []
else:
page_ast = []
except Exception as e:
logger.error("SX URL page-fn eval (OCaml) failed for %s: %s",
raw_path, e, exc_info=True)
return None
else:
# Python fallback
if os.environ.get("SX_USE_REF") == "1":
from shared.sx.ref.async_eval_ref import async_eval
else:
from shared.sx.async_eval import async_eval
try:
page_ast = await async_eval(expr, env, ctx)
except Exception as e:
logger.error("SX URL page-fn eval failed for %s: %s",
raw_path, e, exc_info=True)
return None
if page_ast is None:
page_ast = []