Split make_server_env, eliminate all runtime sx_ref imports, fix auth-menu tests

make_server_env split into 7 focused setup functions:
- setup_browser_stubs (22 DOM no-ops)
- setup_scope_env (18 scope primitives from sx_scope.ml)
- setup_evaluator_bridge (CEK eval-expr, trampoline, expand-macro, etc.)
- setup_introspection (type predicates, component/lambda accessors)
- setup_type_operations (string/env/dict/equality/parser helpers)
- setup_html_tags (~100 HTML tag functions)
- setup_io_env (query, action, helper IO bridge)

Eliminate ALL runtime sx_ref.py imports:
- sx/sxc/pages/helpers.py: 24 imports → _ocaml_helpers.py bridge
- sx/sxc/pages/sx_router.py: remove SX_USE_REF fallback
- shared/sx/query_registry.py: use register_components instead of eval

Unify JIT compilation: pre-compile list derived from allowlist
(no manual duplication), only compiler internals pre-compiled.

Fix test_components auth-menu: ~auth-menu → ~shared:fragments/auth-menu

Tests: 1114 OCaml, 29/29 components, 35/35 regression, 6/6 Playwright

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-24 17:23:09 +00:00
parent 6ef9688bd2
commit 3df8c41ca1
6 changed files with 439 additions and 445 deletions

View File

@@ -0,0 +1,222 @@
"""OCaml bridge for page helper SX functions.
Replaces the deleted sx_ref.py imports. Uses OcamlSync to call
SX functions defined in web/page-helpers.sx.
"""
from __future__ import annotations
import os
import logging
from typing import Any
_logger = logging.getLogger("sx.page_helpers")
_bridge = None
_loaded = False
def _get_bridge():
"""Get the shared OcamlSync bridge, loading page-helpers.sx on first use."""
global _bridge, _loaded
from shared.sx.ocaml_sync import OcamlSync
if _bridge is None:
_bridge = OcamlSync()
_bridge._ensure()
if not _loaded:
_loaded = True
# Load files needed by page-helpers.sx
base = os.path.join(os.path.dirname(__file__), "../../..")
for f in ["spec/parser.sx", "spec/render.sx",
"web/adapter-html.sx", "web/adapter-sx.sx",
"web/web-forms.sx", "web/page-helpers.sx"]:
path = os.path.join(base, f)
if os.path.isfile(path):
try:
_bridge.load(path)
except Exception as e:
_logger.warning("Failed to load %s: %s", f, e)
return _bridge
def _py_to_sx(val: Any) -> str:
"""Serialize a Python value to SX source text."""
if val is None:
return "nil"
if isinstance(val, bool):
return "true" if val else "false"
if isinstance(val, (int, float)):
return str(val)
if isinstance(val, str):
escaped = val.replace("\\", "\\\\").replace('"', '\\"').replace("\n", "\\n")
return f'"{escaped}"'
if isinstance(val, (list, tuple)):
items = " ".join(_py_to_sx(v) for v in val)
return f"(list {items})"
if isinstance(val, dict):
pairs = " ".join(f":{k} {_py_to_sx(v)}" for k, v in val.items())
return "{" + pairs + "}"
return f'"{val}"'
def _sx_to_py(text: str) -> Any:
"""Parse an SX result back to Python. Handles strings, dicts, lists, nil."""
text = text.strip()
if not text or text == "nil":
return None
if text == "true":
return True
if text == "false":
return False
# String result — the bridge already unescapes
if text.startswith('"') and text.endswith('"'):
return text[1:-1]
# For complex results, parse as SX and convert
from shared.sx.parser import parse_all
from shared.sx.types import Keyword, Symbol, NIL
exprs = parse_all(text)
if not exprs:
return text
def _convert(val):
if val is None or val is NIL:
return None
if isinstance(val, bool):
return val
if isinstance(val, (int, float)):
return val
if isinstance(val, str):
return val
if isinstance(val, Keyword):
return val.name
if isinstance(val, Symbol):
return val.name
if isinstance(val, dict):
return {k: _convert(v) for k, v in val.items()}
if isinstance(val, list):
return [_convert(v) for v in val]
return str(val)
return _convert(exprs[0])
def call_sx(fn_name: str, *args: Any) -> Any:
"""Call an SX function by name with Python args, return Python result."""
bridge = _get_bridge()
sx_args = " ".join(_py_to_sx(a) for a in args)
sx_expr = f"({fn_name} {sx_args})" if sx_args else f"({fn_name})"
result = bridge.eval(sx_expr)
return _sx_to_py(result)
def evaluate(source: str, env: Any = None) -> Any:
"""Evaluate SX source text, return result as SX string."""
bridge = _get_bridge()
return bridge.eval(source)
def load_file(path: str) -> None:
"""Load an .sx file into the bridge kernel."""
bridge = _get_bridge()
bridge.load(path)
def eval_in_env(expr_sx: str) -> str:
"""Evaluate an SX expression, return raw SX result string."""
bridge = _get_bridge()
return bridge.eval(expr_sx)
# ---- Drop-in replacements for sx_ref.py functions ----
def build_component_source(data: dict) -> str:
return call_sx("build-component-source", data) or ""
def categorize_special_forms(exprs: Any) -> dict:
return call_sx("categorize-special-forms", exprs) or {}
def build_reference_data(raw: dict, detail_keys: list | None = None) -> dict:
return call_sx("build-reference-data", raw, detail_keys or []) or {}
def build_attr_detail(slug: str) -> dict:
return call_sx("build-attr-detail", slug) or {}
def build_header_detail(slug: str) -> dict:
return call_sx("build-header-detail", slug) or {}
def build_event_detail(slug: str) -> dict:
return call_sx("build-event-detail", slug) or {}
def build_bundle_analysis(pages: Any, components: Any) -> dict:
return call_sx("build-bundle-analysis", pages, components) or {}
def build_routing_analysis(pages: Any, components: Any) -> dict:
return call_sx("build-routing-analysis", pages, components) or {}
def build_affinity_analysis(components: Any, pages: Any) -> dict:
return call_sx("build-affinity-analysis", components, pages) or {}
def make_env():
"""No-op — env is managed by the OCaml bridge."""
return {}
def eval_expr(expr, env=None):
"""Evaluate a parsed SX expression via OCaml bridge."""
from shared.sx.parser import serialize
bridge = _get_bridge()
sx_text = serialize(expr) if not isinstance(expr, str) else expr
result = bridge.eval(sx_text)
from shared.sx.parser import parse
return parse(result) if result else None
def trampoline(val):
"""No-op — OCaml bridge doesn't return thunks."""
return val
def call_lambda(fn, args, env=None):
"""Call a lambda via the OCaml bridge."""
from shared.sx.parser import serialize
bridge = _get_bridge()
parts = [serialize(fn)] + [serialize(a) for a in args]
result = bridge.eval(f"({' '.join(parts)})")
from shared.sx.parser import parse
return parse(result) if result else None
def render_to_html(expr, env=None):
"""Render an SX expression to HTML via OCaml bridge."""
from shared.sx.parser import serialize
bridge = _get_bridge()
sx_text = serialize(expr) if not isinstance(expr, str) else expr
return bridge.eval(f'(render-to-html (quote {sx_text}) (env))')
# Router/deps/engine helpers — these are loaded from .sx files
# and made available via eval. The try/except pattern in helpers.py
# falls back to loading the .sx file directly, which works.
# These stubs exist so the import doesn't fail.
split_path_segments = None
parse_route_pattern = None
match_route_segments = None
match_route = None
find_matching_route = None
make_route_segment = None
scan_refs = None
scan_components_from_source = None
transitive_deps = None
compute_all_deps = None
components_needed = None
page_component_bundle = None
page_css_classes = None
scan_io_refs = None
transitive_io_refs = None
compute_all_io_refs = None
component_pure_p = None
parse_time = None
parse_trigger_spec = None
default_trigger = None
parse_swap_spec = None
parse_retry_spec = None
filter_params = None

View File

@@ -45,7 +45,7 @@ def _component_source(name: str) -> str:
from shared.sx.jinja_bridge import get_component_env
from shared.sx.parser import serialize
from shared.sx.types import Component, Island
from shared.sx.ref.sx_ref import build_component_source
from ._ocaml_helpers import build_component_source
comp = get_component_env().get(name)
if isinstance(comp, Island):
@@ -109,7 +109,7 @@ def _special_forms_data() -> dict:
"""Parse special-forms.sx and return categorized form data."""
import os
from shared.sx.parser import parse_all
from shared.sx.ref.sx_ref import categorize_special_forms
from ._ocaml_helpers import categorize_special_forms
ref_dir = _ref_dir()
spec_path = os.path.join(ref_dir, "special-forms.sx")
@@ -125,7 +125,7 @@ def _reference_data(slug: str) -> dict:
REQUEST_HEADERS, RESPONSE_HEADERS,
EVENTS, JS_API, ATTR_DETAILS, HEADER_DETAILS,
)
from shared.sx.ref.sx_ref import build_reference_data
from ._ocaml_helpers import build_reference_data
# Build raw data dict and detail keys based on slug
if slug == "attributes":
@@ -202,7 +202,7 @@ def _js_translate_define(expr: list, name: str) -> str | None:
if _JS_SX_ENV is None:
from shared.sx.ref.run_js_sx import load_js_sx
_JS_SX_ENV = load_js_sx()
from shared.sx.ref.sx_ref import evaluate
from ._ocaml_helpers import evaluate
from shared.sx.types import Symbol
env = dict(_JS_SX_ENV)
env["_defines"] = [[name, expr]]
@@ -756,7 +756,7 @@ def _self_hosting_data(ref_dir: str) -> dict:
import os
from shared.sx.parser import parse_all
from shared.sx.types import Symbol
from shared.sx.ref.sx_ref import evaluate, make_env
from ._ocaml_helpers import evaluate, make_env
from shared.sx.ref.bootstrap_py import extract_defines, compile_ref_to_py, PyEmitter
try:
@@ -829,7 +829,7 @@ def _js_self_hosting_data(ref_dir: str) -> dict:
"""Run js.sx live: load into evaluator, translate all spec defines."""
import os
from shared.sx.types import Symbol
from shared.sx.ref.sx_ref import evaluate
from ._ocaml_helpers import evaluate
from shared.sx.ref.run_js_sx import load_js_sx
from shared.sx.ref.platform_js import extract_defines
@@ -881,7 +881,7 @@ def _bundle_analyzer_data() -> dict:
from shared.sx.deps import components_needed, scan_components_from_sx
from shared.sx.parser import serialize
from shared.sx.types import Component, Macro
from shared.sx.ref.sx_ref import build_bundle_analysis
from ._ocaml_helpers import build_bundle_analysis
env = get_component_env()
total_components = sum(1 for v in env.values() if isinstance(v, Component))
@@ -937,7 +937,7 @@ def _routing_analyzer_data() -> dict:
from shared.sx.pages import get_all_pages
from shared.sx.parser import serialize as sx_serialize
from shared.sx.helpers import _sx_literal
from shared.sx.ref.sx_ref import build_routing_analysis
from ._ocaml_helpers import build_routing_analysis
# I/O edge: extract page data from page registry
pages_raw = []
@@ -989,7 +989,7 @@ def _attr_detail_data(slug: str) -> dict:
"""Return attribute detail data for a specific attribute slug."""
from content.pages import ATTR_DETAILS
from shared.sx.helpers import sx_call
from shared.sx.ref.sx_ref import build_attr_detail
from ._ocaml_helpers import build_attr_detail
detail = ATTR_DETAILS.get(slug)
result = build_attr_detail(slug, detail)
@@ -1004,7 +1004,7 @@ def _header_detail_data(slug: str) -> dict:
"""Return header detail data for a specific header slug."""
from content.pages import HEADER_DETAILS
from shared.sx.helpers import sx_call
from shared.sx.ref.sx_ref import build_header_detail
from ._ocaml_helpers import build_header_detail
result = build_header_detail(slug, HEADER_DETAILS.get(slug))
demo_name = result.get("header-demo")
@@ -1017,7 +1017,7 @@ def _event_detail_data(slug: str) -> dict:
"""Return event detail data for a specific event slug."""
from content.pages import EVENT_DETAILS
from shared.sx.helpers import sx_call
from shared.sx.ref.sx_ref import build_event_detail
from ._ocaml_helpers import build_event_detail
result = build_event_detail(slug, EVENT_DETAILS.get(slug))
demo_name = result.get("event-demo")
@@ -1031,7 +1031,7 @@ def _run_spec_tests() -> dict:
import os
import time
from shared.sx.parser import parse_all
from shared.sx.ref.sx_ref import eval_expr as _eval, trampoline as _trampoline
from ._ocaml_helpers import eval_expr as _eval, trampoline as _trampoline
ref_dir = os.path.join(os.path.dirname(__file__), "..", "..", "shared", "sx", "ref")
if not os.path.isdir(ref_dir):
@@ -1105,7 +1105,7 @@ def _run_modular_tests(spec_name: str) -> dict:
import os
import time
from shared.sx.parser import parse_all
from shared.sx.ref.sx_ref import eval_expr as _eval, trampoline as _trampoline
from ._ocaml_helpers import eval_expr as _eval, trampoline as _trampoline
from shared.sx.types import Symbol, Keyword, Lambda, NIL
ref_dir = os.path.join(os.path.dirname(__file__), "..", "..", "shared", "sx", "ref")
@@ -1175,7 +1175,7 @@ def _run_modular_tests(spec_name: str) -> dict:
def render_html(sx_source):
try:
from shared.sx.ref.sx_ref import render_to_html as _render_to_html
from ._ocaml_helpers import render_to_html as _render_to_html
except ImportError:
return "<!-- render-to-html not available -->"
exprs = parse_all(sx_source)
@@ -1187,7 +1187,7 @@ def _run_modular_tests(spec_name: str) -> dict:
def _call_sx(fn, args, caller_env):
if isinstance(fn, Lambda):
from shared.sx.ref.sx_ref import call_lambda as _call_lambda
from ._ocaml_helpers import call_lambda as _call_lambda
return _trampoline(_call_lambda(fn, list(args), caller_env))
return fn(*args)
@@ -1259,7 +1259,7 @@ def _run_modular_tests(spec_name: str) -> dict:
# Load module functions from bootstrap
if sn == "router":
try:
from shared.sx.ref.sx_ref import (
from ._ocaml_helpers import (
split_path_segments,
parse_route_pattern,
match_route_segments,
@@ -1277,7 +1277,7 @@ def _run_modular_tests(spec_name: str) -> dict:
eval_file("router.sx")
elif sn == "deps":
try:
from shared.sx.ref.sx_ref import (
from ._ocaml_helpers import (
scan_refs, scan_components_from_source,
transitive_deps, compute_all_deps,
components_needed, page_component_bundle,
@@ -1302,7 +1302,7 @@ def _run_modular_tests(spec_name: str) -> dict:
env["test-env"] = lambda: env
elif sn == "engine":
try:
from shared.sx.ref.sx_ref import (
from ._ocaml_helpers import (
parse_time, parse_trigger_spec, default_trigger,
parse_swap_spec, parse_retry_spec, filter_params,
)
@@ -1345,7 +1345,7 @@ def _run_modular_tests(spec_name: str) -> dict:
env[stub] = _noop
# Load engine (orchestration depends on it)
try:
from shared.sx.ref.sx_ref import (
from ._ocaml_helpers import (
parse_time, parse_trigger_spec, default_trigger,
parse_swap_spec, parse_retry_spec, filter_params,
)
@@ -1459,7 +1459,7 @@ def _affinity_demo_data() -> dict:
from shared.sx.jinja_bridge import get_component_env
from shared.sx.types import Component
from shared.sx.pages import get_all_pages
from shared.sx.ref.sx_ref import build_affinity_analysis
from ._ocaml_helpers import build_affinity_analysis
# I/O edge: extract component data and page render plans
env = get_component_env()
@@ -1530,9 +1530,9 @@ def _prove_data() -> dict:
"""
import time
from shared.sx.parser import parse_all
from shared.sx.ref.sx_ref import evaluate
from ._ocaml_helpers import evaluate
from shared.sx.primitives import all_primitives
from shared.sx.ref.sx_ref import trampoline as _trampoline, call_lambda as _call_lambda
from ._ocaml_helpers import trampoline as _trampoline, call_lambda as _call_lambda
env = all_primitives()
@@ -1643,7 +1643,7 @@ def _page_helpers_demo_data() -> dict:
import os
import time
from shared.sx.parser import parse_all
from shared.sx.ref.sx_ref import (
from ._ocaml_helpers import (
categorize_special_forms, build_reference_data,
build_attr_detail, build_component_source,
build_routing_analysis,

View File

@@ -173,10 +173,7 @@ async def eval_sx_url(raw_path: str) -> Any:
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
from shared.sx.async_eval import async_eval
try:
page_ast = await async_eval(expr, env, ctx)