Mizan codegen substrate: Rust kernel + Rust codegen binary, JS generator deleted

The Mizan codegen substrate moves off JavaScript template-literal emission
onto a compiled Rust binary that consumes the same OpenAPI + x-mizan-* IR
the JS substrate consumed. Three structural wins fall out of one move:

1. Moat closes. The codegen logic (how `affects` becomes auto-invalidation,
   how named contexts collapse onto bundled fetches, how the registry-to-
   Provider mapping is shaped) ships compiled instead of as source bytes
   in every consumer's node_modules.

2. Pattern F (lines.push append-walls) becomes structurally unauthorable.
   The emit substrate is askama templates in templates/<target>/*.j2 —
   actual target-language files with {{ ... }} substitution markers,
   syntax-highlighted natively, type-checked against the render context
   structs at compile time. The Rust emit modules build typed render
   contexts and call .render(); no string-builder surface exists.

3. OpenAPI `default`-bearing fields now emit as non-optional in TS / Python
   / Rust — the server always populates them, so consumer code reads them
   without nullable checks. Surfaced by Blazr's typecheck on regeneration.

Layout:
  frontends/mizan-rust/        — Rust port of @mizan/base; #[cfg(feature="pyo3")]
                                 exposes PyMizanClient for the Python target.
  protocol/mizan-codegen/      — codegen binary source + askama templates.
  protocol/mizan-generate/     — npm-package shim. bin/launcher.mjs dispatches
                                 to the platform-appropriate prebuilt binary.
                                 Old generator/ JS tree deleted.
  tests/rust/                  — wire-parity drivers. drive_kernel exercises
                                 raw client.call() / fetch_context(); drive_emitted
                                 exercises the typed crate the codegen emits.
  tests/afi/afi_codegen_app.py — codegen entrypoint module (imports + registers).
  backends/mizan-fastapi/.../schema.py — adds outputNullable so the Rust
                                 codegen can wrap T | None responses in Option<T>.

Verification:
  - 20 mizan-codegen tests green (IR deserialization, byte-equivalent
    parity vs JS baseline for stage1/rust/python/react/vue/svelte,
    structural test for channels).
  - tests/rust/run_wire_parity.py — 12/12 probes green via the Rust binary
    driving the FastAPI fixture end-to-end.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-17 18:26:32 -04:00
parent c15c6f3e14
commit 43bcf3f26f
114 changed files with 11090 additions and 2342 deletions

View File

@@ -0,0 +1,67 @@
# AUTO-GENERATED by mizan — do not edit
from __future__ import annotations
from collections.abc import Callable
from typing import Any
# Built from frontends/mizan-rust with `maturin develop --features pyo3`.
from mizan_rust import PyMizanClient, PyContextSubscription
from .types import * # noqa: F401, F403
from .types import BaseModel # re-import for the synthesized ContextData classes
class MizanClient:
"""Typed Python facade over the PyO3 mizan-rust kernel."""
def __init__(self, base_url: str, *, session: bool = False,
csrf_cookie_name: str = "csrftoken",
csrf_header_name: str = "X-CSRFToken") -> None:
self._inner = PyMizanClient(
base_url,
session=session,
csrf_cookie_name=csrf_cookie_name,
csrf_header_name=csrf_header_name,
)
def fetch_user_context(self, user_id: int) -> "UserContextData":
raw = self._inner.fetch_context("user", {"user_id": user_id})
return UserContextData(**raw)
def subscribe_user_context(self, user_id: int,
callback: Callable[[dict[str, Any]], None]) -> PyContextSubscription:
return self._inner.subscribe_context("user", {"user_id": user_id}, callback)
def call_echo(self, args: EchoInput) -> EchoOutput:
raw = self._inner.call("echo", args.model_dump())
return EchoOutput(**raw)
def call_whoami(self) -> WhoamiOutput:
raw = self._inner.call("whoami", {})
return WhoamiOutput(**raw)
def call_update_profile(self, args: UpdateProfileInput) -> UpdateProfileOutput:
raw = self._inner.call("update_profile", args.model_dump())
return UpdateProfileOutput(**raw)
def call_find_user(self, args: FindUserInput) -> FindUserOutput | None:
raw = self._inner.call("find_user", args.model_dump())
return FindUserOutput(**raw) if raw is not None else None
def call_rename_user(self, args: RenameUserInput) -> RenameUserOutput:
raw = self._inner.call("rename_user", args.model_dump())
return RenameUserOutput(**raw)
def invalidate(self, context: str) -> None:
self._inner.invalidate(context)
def invalidate_scoped(self, context: str, params: dict[str, Any]) -> None:
self._inner.invalidate_scoped(context, params)
# ── Context data shapes (per-context bundle) ──────────────────────────────
class UserContextData(BaseModel):
"""Bundled return of fetch_user_context."""
user_profile: UserProfileOutput
user_orders: UserOrdersOutput