A channel's message slots are named from the client, on every backend
The IR called them react-message and django-message, so a FastAPI channel had to declare a DjangoMessage. They are client-message and server-message now, and the direction words hold wherever a channel is declared: Params / ClientMessage / ServerMessage, with mizan-core deriving <Pascal>Params and friends so no backend names a type itself. Django's ReactChannel and FastAPI's ReactChannel are both Channel. mizan-fastapi never registered a channels extension, so build_ir() emitted no channel at all and every payload type was invisible to codegen. It registers one now. RegistryExtension is an ABC requiring all(), which is what the IR reads — an extension that cannot enumerate its registrations no longer exists. The gate that should have caught the rename could not: tests/afi registered no channel because mizan-rust had no channel registry to register one in, so a five-package rename of the wire contract passed byte-parity without a channel byte crossing it. mizan-rust grows ChannelSlotKind, a CHANNELS slice, a #[mizan::channel] macro, and KDL emission whose wire_to_pascal matches Python's split; the AFI fixture now carries a channel with every slot and one with a single slot, so all three backends prove the contract byte for byte. MizanChannel held three Option<String> beside three has_*() predicates and unwrapped them with defaults; it holds an ordered slot vector, so an absent slot is absent rather than defaulted. The channels target emitted a React hooks file that a stage1-only consumer could not compile — react emits that now. The codegen's parity tests byte-compared emitted source against baselines without ever compiling it: they compile the generated crate and run its tests, import the generated Python package and call every method, and typecheck each TypeScript target against a consumer. Also fixed at source: app_visitor printed its import diagnostic to stdout, the stream export_mizan_ir writes KDL to, so a failed import silently corrupted the IR; the apps root was hardcoded to "apps"; _default_literal crashed build_ir on any non-JSON-serializable field default; Django and mizan-core derived Pascal names two different ways, disagreeing on every dotted channel name. ir.py builds a document and renders templates/ir/document.kdl.j2 rather than appending KDL strings with hand-tracked indentation, and named types resolve to a fixed point — a model reachable only through a union branch was referenced by a ref that no type block ever defined. The rest is the write-gate's own classifiers run over the standing tree: relative imports, silent swallows, Protocol contracts that should be ABCs, emitters hand-rendering target source, catch-all arms over closed enums, and comments narrating the project rather than the code. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,17 +1,7 @@
|
||||
"""
|
||||
Example FastAPI app for the e2e harness — mirrors the surface that
|
||||
examples/django-react-site/backend/testapp/clients.py exercises, minus
|
||||
Django-only features (forms, channels, ws-whoami, session-bound JWT).
|
||||
"""FastAPI app exposing the mizan server functions the e2e harness calls.
|
||||
|
||||
The fixture functions are designed to drive specific Playwright tests:
|
||||
- success-path RPC (echo, add, multiply)
|
||||
- auth requirements (whoami, staff_only, superuser_only, verified_only)
|
||||
- error codes (not_implemented_fn, buggy_fn, permission_check_fn)
|
||||
- a global context (current_user) for the bundled-fetch path
|
||||
|
||||
Anonymous access is the default — request.state.user is left unset so
|
||||
the auth-required functions return UNAUTHORIZED, matching the harness
|
||||
expectations for an anonymous browser session.
|
||||
`request.state.user` is never set here, so every auth-gated function below
|
||||
resolves against an anonymous caller.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -46,6 +36,10 @@ class MultiplyOutput(BaseModel):
|
||||
product: int
|
||||
|
||||
|
||||
class DivideOutput(BaseModel):
|
||||
quotient: float
|
||||
|
||||
|
||||
class UserOutput(BaseModel):
|
||||
email: str
|
||||
authenticated: bool
|
||||
@@ -56,7 +50,44 @@ class MessageOutput(BaseModel):
|
||||
message: str
|
||||
|
||||
|
||||
# ─── Fixture functions ──────────────────────────────────────────────────────
|
||||
# ─── Morph state ────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class MorphGroupMeta(BaseModel):
|
||||
"""Group summary. Its field set stays disjoint from MorphLayer's so the
|
||||
server's return-type slot resolver can tell the two morphs slots apart."""
|
||||
|
||||
id: int
|
||||
label: str
|
||||
count: int
|
||||
|
||||
|
||||
class MorphLayer(BaseModel):
|
||||
id: int
|
||||
group_id: int
|
||||
label: str
|
||||
value: float
|
||||
|
||||
|
||||
_morph_groups: list[MorphGroupMeta] = [
|
||||
MorphGroupMeta(id=1, label="face", count=2),
|
||||
]
|
||||
|
||||
|
||||
_morph_layers: list[MorphLayer] = [
|
||||
MorphLayer(id=1, group_id=1, label="brow", value=0.0),
|
||||
MorphLayer(id=2, group_id=1, label="jaw", value=0.0),
|
||||
]
|
||||
|
||||
|
||||
def _morph_layer(layer_id: int) -> MorphLayer:
|
||||
for layer in _morph_layers:
|
||||
if layer.id == layer_id:
|
||||
return layer
|
||||
raise ValueError(f"unknown morph layer id={layer_id}")
|
||||
|
||||
|
||||
# ─── Server functions ───────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@client
|
||||
@@ -79,7 +110,7 @@ def multiply(request, x: int, y: int) -> MultiplyOutput:
|
||||
|
||||
@client(auth=True)
|
||||
def whoami(request) -> UserOutput:
|
||||
"""Returns the authenticated user's identity. Anonymous → UNAUTHORIZED."""
|
||||
"""Returns the authenticated user's identity."""
|
||||
user = request.state.user
|
||||
return UserOutput(
|
||||
email=getattr(user, "email", ""),
|
||||
@@ -107,25 +138,31 @@ def _is_verified(request) -> bool:
|
||||
|
||||
@client(auth=_is_verified)
|
||||
def verified_only(request) -> MessageOutput:
|
||||
"""Verified-users-only endpoint. Anonymous → FORBIDDEN."""
|
||||
"""Verified-users-only endpoint."""
|
||||
return MessageOutput(message="verified access ok")
|
||||
|
||||
|
||||
@client
|
||||
def not_implemented_fn(request) -> MessageOutput:
|
||||
"""Always raises NotImplementedError → NOT_IMPLEMENTED."""
|
||||
raise NotImplementedError("This function is intentionally not implemented")
|
||||
_ECHO_TRANSFORMS = {"upper": str.upper, "lower": str.lower}
|
||||
|
||||
|
||||
@client
|
||||
def buggy_fn(request) -> MessageOutput:
|
||||
"""Always raises a generic exception → INTERNAL_ERROR."""
|
||||
raise RuntimeError("Intentional bug for e2e testing")
|
||||
def echo_transform(request, text: str, mode: str) -> EchoOutput:
|
||||
"""Echoes the text through the named transform."""
|
||||
transform = _ECHO_TRANSFORMS.get(mode)
|
||||
if transform is None:
|
||||
raise NotImplementedError(f"echo mode {mode!r} has no transform")
|
||||
return EchoOutput(message=transform(text))
|
||||
|
||||
|
||||
@client
|
||||
def divide(request, numerator: int, denominator: int) -> DivideOutput:
|
||||
"""Returns numerator / denominator."""
|
||||
return DivideOutput(quotient=numerator / denominator)
|
||||
|
||||
|
||||
@client
|
||||
def permission_check_fn(request, secret: str) -> MessageOutput:
|
||||
"""Wrong secret → FORBIDDEN; correct secret → success."""
|
||||
"""Grants access only for the correct secret."""
|
||||
if secret != "open-sesame":
|
||||
raise Forbidden("Invalid secret")
|
||||
return MessageOutput(message="access granted")
|
||||
@@ -133,7 +170,7 @@ def permission_check_fn(request, secret: str) -> MessageOutput:
|
||||
|
||||
@client(context="global")
|
||||
def current_user(request) -> UserOutput:
|
||||
"""The global context — auto-mounted at the React root."""
|
||||
"""The global context, auto-mounted at the React root."""
|
||||
user = getattr(getattr(request, "state", None), "user", None)
|
||||
return UserOutput(
|
||||
email=getattr(user, "email", "") if user else "",
|
||||
@@ -142,56 +179,24 @@ def current_user(request) -> UserOutput:
|
||||
)
|
||||
|
||||
|
||||
# ─── Merge protocol fixtures ────────────────────────────────────────────────
|
||||
|
||||
|
||||
class MorphGroupMeta(BaseModel):
|
||||
"""Group summary — narrower shape than MorphLayer. Listed alongside
|
||||
morph_layers so the server's slot resolver has to discriminate by
|
||||
return-type rather than by bundle order."""
|
||||
id: int
|
||||
label: str
|
||||
count: int
|
||||
|
||||
|
||||
class MorphLayer(BaseModel):
|
||||
id: int
|
||||
group_id: int
|
||||
label: str
|
||||
value: float
|
||||
|
||||
|
||||
_morph_groups: list[MorphGroupMeta] = [
|
||||
MorphGroupMeta(id=1, label="face", count=2),
|
||||
]
|
||||
|
||||
|
||||
_morph_layers: list[MorphLayer] = [
|
||||
MorphLayer(id=1, group_id=1, label="brow", value=0.0),
|
||||
MorphLayer(id=2, group_id=1, label="jaw", value=0.0),
|
||||
]
|
||||
|
||||
|
||||
@client(context="morphs")
|
||||
def morph_groups(request) -> list[MorphGroupMeta]:
|
||||
"""Summary-shape slot — server must route MorphLayer mutations away from here."""
|
||||
"""The group summaries in the morphs context bundle."""
|
||||
return list(_morph_groups)
|
||||
|
||||
|
||||
@client(context="morphs")
|
||||
def morph_layers(request) -> list[MorphLayer]:
|
||||
"""Detailed-shape slot — server routes MorphLayer mutations here."""
|
||||
"""The layer rows in the morphs context bundle."""
|
||||
return list(_morph_layers)
|
||||
|
||||
|
||||
@client(merge="morphs")
|
||||
def set_morph_value(request, id: int, value: float) -> MorphLayer:
|
||||
"""Mutation that returns the changed row; kernel splices into morph_layers."""
|
||||
for layer in _morph_layers:
|
||||
if layer.id == id:
|
||||
layer.value = value
|
||||
return layer
|
||||
raise ValueError(f"unknown morph layer id={id}")
|
||||
"""Sets one layer's value and returns the changed row for the kernel to splice."""
|
||||
layer = _morph_layer(id)
|
||||
layer.value = value
|
||||
return layer
|
||||
|
||||
|
||||
# ─── Registration ───────────────────────────────────────────────────────────
|
||||
@@ -204,8 +209,8 @@ register(whoami, "whoami")
|
||||
register(staff_only, "staff_only")
|
||||
register(superuser_only, "superuser_only")
|
||||
register(verified_only, "verified_only")
|
||||
register(not_implemented_fn, "not_implemented_fn")
|
||||
register(buggy_fn, "buggy_fn")
|
||||
register(echo_transform, "echo_transform")
|
||||
register(divide, "divide")
|
||||
register(permission_check_fn, "permission_check_fn")
|
||||
register(current_user, "current_user")
|
||||
register(morph_groups, "morph_groups")
|
||||
|
||||
Reference in New Issue
Block a user