Replace inter-service _handlers dicts with declarative sx defquery/defaction

The inter-service data layer (fetch_data/call_action) was the least
structured part of the codebase — Python _handlers dicts with ad-hoc
param extraction scattered across 16 route files. This replaces them
with declarative .sx query/action definitions that make the entire
inter-service protocol self-describing and greppable.

Infrastructure:
- defquery/defaction special forms in the sx evaluator
- Query/action registry with load, lookup, and schema introspection
- Query executor using async_eval with I/O primitives
- Blueprint factories (create_data_blueprint/create_action_blueprint)
  with sx-first dispatch and Python fallback
- /internal/schema endpoint on every service
- parse-datetime and split-ids primitives for type coercion

Service extractions:
- LikesService (toggle, is_liked, liked_slugs, liked_ids)
- PageConfigService (ensure, get_by_container, get_by_id, get_batch, update)
- RelationsService (wraps module-level functions)
- AccountDataService (user_by_email, newsletters)
- CartItemsService, MarketDataService (raw SQLAlchemy lookups)

50 of 54 handlers converted to sx, 4 Python fallbacks remain
(ghost-sync/push-member, clear-cart-for-order, create-order).
Net: -1,383 lines Python, +251 lines modified.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-04 08:13:50 +00:00
parent e53e8cc1f7
commit 1f36987f77
54 changed files with 1599 additions and 1382 deletions

View File

@@ -553,6 +553,75 @@ def _sf_defhandler(expr: list, env: dict) -> HandlerDef:
return handler
def _parse_key_params(params_expr: list) -> list[str]:
"""Parse ``(&key param1 param2 ...)`` into a list of param name strings."""
params: list[str] = []
in_key = False
for p in params_expr:
if isinstance(p, Symbol):
if p.name == "&key":
in_key = True
continue
if in_key:
params.append(p.name)
elif isinstance(p, str):
params.append(p)
return params
def _sf_defquery(expr: list, env: dict):
"""``(defquery name (&key param...) "docstring" body)``"""
from .types import QueryDef
if len(expr) < 4:
raise EvalError("defquery requires name, params, and body")
name_sym = expr[1]
if not isinstance(name_sym, Symbol):
raise EvalError(f"defquery name must be symbol, got {type(name_sym).__name__}")
params_expr = expr[2]
if not isinstance(params_expr, list):
raise EvalError("defquery params must be a list")
params = _parse_key_params(params_expr)
# Optional docstring before body
if len(expr) >= 5 and isinstance(expr[3], str):
doc = expr[3]
body = expr[4]
else:
doc = ""
body = expr[3]
qdef = QueryDef(
name=name_sym.name, params=params, doc=doc,
body=body, closure=dict(env),
)
env[f"query:{name_sym.name}"] = qdef
return qdef
def _sf_defaction(expr: list, env: dict):
"""``(defaction name (&key param...) "docstring" body)``"""
from .types import ActionDef
if len(expr) < 4:
raise EvalError("defaction requires name, params, and body")
name_sym = expr[1]
if not isinstance(name_sym, Symbol):
raise EvalError(f"defaction name must be symbol, got {type(name_sym).__name__}")
params_expr = expr[2]
if not isinstance(params_expr, list):
raise EvalError("defaction params must be a list")
params = _parse_key_params(params_expr)
if len(expr) >= 5 and isinstance(expr[3], str):
doc = expr[3]
body = expr[4]
else:
doc = ""
body = expr[3]
adef = ActionDef(
name=name_sym.name, params=params, doc=doc,
body=body, closure=dict(env),
)
env[f"action:{name_sym.name}"] = adef
return adef
def _sf_set_bang(expr: list, env: dict) -> Any:
"""``(set! name value)`` — mutate existing binding."""
if len(expr) != 3:
@@ -737,6 +806,8 @@ _SPECIAL_FORMS: dict[str, Any] = {
"quasiquote": _sf_quasiquote,
"defhandler": _sf_defhandler,
"defpage": _sf_defpage,
"defquery": _sf_defquery,
"defaction": _sf_defaction,
}

View File

@@ -516,6 +516,23 @@ def prim_parse_int(val: Any, default: Any = 0) -> int | Any:
return default
@register_primitive("parse-datetime")
def prim_parse_datetime(val: Any) -> Any:
"""``(parse-datetime "2024-01-15T10:00:00")`` → datetime object."""
from datetime import datetime
if not val or val is NIL:
return NIL
return datetime.fromisoformat(str(val))
@register_primitive("split-ids")
def prim_split_ids(val: Any) -> list[int]:
"""``(split-ids "1,2,3")`` → [1, 2, 3]. Parse comma-separated int IDs."""
if not val or val is NIL:
return []
return [int(x.strip()) for x in str(val).split(",") if x.strip()]
# ---------------------------------------------------------------------------
# Assertions
# ---------------------------------------------------------------------------

View File

@@ -0,0 +1,70 @@
"""
Execute defquery / defaction definitions.
Unlike fragment handlers (which produce SX markup via ``async_eval_to_sx``),
query/action defs produce **data** (dicts, lists, scalars) that get
JSON-serialized by the calling blueprint. Uses ``async_eval()`` with
the I/O primitive pipeline so ``(service ...)`` calls are awaited inline.
"""
from __future__ import annotations
from typing import Any
from .types import QueryDef, ActionDef, NIL
async def execute_query(query_def: QueryDef, params: dict[str, str]) -> Any:
"""Execute a defquery and return a JSON-serializable result.
Parameters are bound from request query string args.
"""
from .jinja_bridge import get_component_env, _get_request_context
from .async_eval import async_eval
env = dict(get_component_env())
env.update(query_def.closure)
# Bind params from request args (try kebab-case and snake_case)
for param in query_def.params:
snake = param.replace("-", "_")
val = params.get(param, params.get(snake, NIL))
# Coerce type=int for common patterns
if isinstance(val, str) and val.lstrip("-").isdigit():
val = int(val)
env[param] = val
ctx = _get_request_context()
result = await async_eval(query_def.body, env, ctx)
return _normalize(result)
async def execute_action(action_def: ActionDef, payload: dict[str, Any]) -> Any:
"""Execute a defaction and return a JSON-serializable result.
Parameters are bound from the JSON request body.
"""
from .jinja_bridge import get_component_env, _get_request_context
from .async_eval import async_eval
env = dict(get_component_env())
env.update(action_def.closure)
# Bind params from JSON payload (try kebab-case and snake_case)
for param in action_def.params:
snake = param.replace("-", "_")
val = payload.get(param, payload.get(snake, NIL))
env[param] = val
ctx = _get_request_context()
result = await async_eval(action_def.body, env, ctx)
return _normalize(result)
def _normalize(value: Any) -> Any:
"""Ensure result is JSON-serializable (strip NIL, convert sets, etc)."""
if value is NIL or value is None:
return None
if isinstance(value, set):
return list(value)
return value

180
shared/sx/query_registry.py Normal file
View File

@@ -0,0 +1,180 @@
"""
Registry for defquery / defaction definitions.
Mirrors the pattern in ``handlers.py`` but for inter-service data queries
and action endpoints. Each service loads its ``.sx`` files at startup,
and the registry makes them available for dispatch by the query blueprint.
Usage::
from shared.sx.query_registry import load_query_file, get_query
load_query_file("events/queries.sx", "events")
qdef = get_query("events", "pending-entries")
"""
from __future__ import annotations
import logging
import os
from typing import Any
from .types import QueryDef, ActionDef
logger = logging.getLogger("sx.query_registry")
# ---------------------------------------------------------------------------
# Registry — service → name → QueryDef / ActionDef
# ---------------------------------------------------------------------------
_QUERY_REGISTRY: dict[str, dict[str, QueryDef]] = {}
_ACTION_REGISTRY: dict[str, dict[str, ActionDef]] = {}
def register_query(service: str, qdef: QueryDef) -> None:
if service not in _QUERY_REGISTRY:
_QUERY_REGISTRY[service] = {}
_QUERY_REGISTRY[service][qdef.name] = qdef
logger.debug("Registered query %s:%s", service, qdef.name)
def register_action(service: str, adef: ActionDef) -> None:
if service not in _ACTION_REGISTRY:
_ACTION_REGISTRY[service] = {}
_ACTION_REGISTRY[service][adef.name] = adef
logger.debug("Registered action %s:%s", service, adef.name)
def get_query(service: str, name: str) -> QueryDef | None:
return _QUERY_REGISTRY.get(service, {}).get(name)
def get_action(service: str, name: str) -> ActionDef | None:
return _ACTION_REGISTRY.get(service, {}).get(name)
def get_all_queries(service: str) -> dict[str, QueryDef]:
return dict(_QUERY_REGISTRY.get(service, {}))
def get_all_actions(service: str) -> dict[str, ActionDef]:
return dict(_ACTION_REGISTRY.get(service, {}))
def clear(service: str | None = None) -> None:
if service is None:
_QUERY_REGISTRY.clear()
_ACTION_REGISTRY.clear()
else:
_QUERY_REGISTRY.pop(service, None)
_ACTION_REGISTRY.pop(service, None)
# ---------------------------------------------------------------------------
# Loading — parse .sx files and collect QueryDef / ActionDef instances
# ---------------------------------------------------------------------------
def load_query_file(filepath: str, service_name: str) -> list[QueryDef]:
"""Parse an .sx file and register any defquery definitions."""
from .parser import parse_all
from .evaluator import _eval
from .jinja_bridge import get_component_env
with open(filepath, encoding="utf-8") as f:
source = f.read()
env = dict(get_component_env())
exprs = parse_all(source)
queries: list[QueryDef] = []
for expr in exprs:
_eval(expr, env)
for val in env.values():
if isinstance(val, QueryDef):
register_query(service_name, val)
queries.append(val)
return queries
def load_action_file(filepath: str, service_name: str) -> list[ActionDef]:
"""Parse an .sx file and register any defaction definitions."""
from .parser import parse_all
from .evaluator import _eval
from .jinja_bridge import get_component_env
with open(filepath, encoding="utf-8") as f:
source = f.read()
env = dict(get_component_env())
exprs = parse_all(source)
actions: list[ActionDef] = []
for expr in exprs:
_eval(expr, env)
for val in env.values():
if isinstance(val, ActionDef):
register_action(service_name, val)
actions.append(val)
return actions
def load_query_dir(directory: str, service_name: str) -> list[QueryDef]:
"""Load all .sx files from a directory and register queries."""
import glob as glob_mod
queries: list[QueryDef] = []
for filepath in sorted(glob_mod.glob(os.path.join(directory, "*.sx"))):
queries.extend(load_query_file(filepath, service_name))
return queries
def load_action_dir(directory: str, service_name: str) -> list[ActionDef]:
"""Load all .sx files from a directory and register actions."""
import glob as glob_mod
actions: list[ActionDef] = []
for filepath in sorted(glob_mod.glob(os.path.join(directory, "*.sx"))):
actions.extend(load_action_file(filepath, service_name))
return actions
def load_service_protocols(service_name: str, base_dir: str) -> None:
"""Load queries.sx and actions.sx from a service's base directory."""
queries_path = os.path.join(base_dir, "queries.sx")
actions_path = os.path.join(base_dir, "actions.sx")
if os.path.exists(queries_path):
load_query_file(queries_path, service_name)
logger.info("Loaded queries for %s from %s", service_name, queries_path)
if os.path.exists(actions_path):
load_action_file(actions_path, service_name)
logger.info("Loaded actions for %s from %s", service_name, actions_path)
# ---------------------------------------------------------------------------
# Schema — introspection for /internal/schema
# ---------------------------------------------------------------------------
def schema_for_service(service: str) -> dict[str, Any]:
"""Return a JSON-serializable schema of all queries and actions."""
queries = []
for qdef in _QUERY_REGISTRY.get(service, {}).values():
queries.append({
"name": qdef.name,
"params": list(qdef.params),
"doc": qdef.doc,
})
actions = []
for adef in _ACTION_REGISTRY.get(service, {}).values():
actions.append({
"name": adef.name,
"params": list(adef.params),
"doc": adef.doc,
})
return {
"service": service,
"queries": sorted(queries, key=lambda q: q["name"]),
"actions": sorted(actions, key=lambda a: a["name"]),
}

View File

@@ -240,9 +240,47 @@ class PageDef:
return f"<page:{self.name} path={self.path!r}>"
# ---------------------------------------------------------------------------
# QueryDef / ActionDef
# ---------------------------------------------------------------------------
@dataclass
class QueryDef:
"""A declarative data query defined in an .sx file.
Created by ``(defquery name (&key param...) "docstring" body)``.
The body is evaluated with async I/O primitives to produce JSON data.
"""
name: str
params: list[str] # keyword parameter names
doc: str # docstring
body: Any # unevaluated s-expression body
closure: dict[str, Any] = field(default_factory=dict)
def __repr__(self):
return f"<query:{self.name}({', '.join(self.params)})>"
@dataclass
class ActionDef:
"""A declarative action defined in an .sx file.
Created by ``(defaction name (&key param...) "docstring" body)``.
The body is evaluated with async I/O primitives to produce JSON data.
"""
name: str
params: list[str] # keyword parameter names
doc: str # docstring
body: Any # unevaluated s-expression body
closure: dict[str, Any] = field(default_factory=dict)
def __repr__(self):
return f"<action:{self.name}({', '.join(self.params)})>"
# ---------------------------------------------------------------------------
# Type alias
# ---------------------------------------------------------------------------
# An s-expression value after evaluation
SExp = int | float | str | bool | Symbol | Keyword | Lambda | Macro | Component | HandlerDef | RelationDef | PageDef | list | dict | _Nil | None
SExp = int | float | str | bool | Symbol | Keyword | Lambda | Macro | Component | HandlerDef | RelationDef | PageDef | QueryDef | ActionDef | list | dict | _Nil | None