Add Bazel targets for on-demand schemas and Python SDK generation (#54096)

## What changed

- Add `//bazel/schema:app-server` to generate stable and experimental schemas from Rust sources without depending on committed precomputed bundles.
- Add `//sdk/python:generated` to generate Python SDK models, the notification registry, and API methods from the stable schema in scratch space, using dependencies from `uv.lock`.
- Add `generate-types --no-format` to skip formatters for build outputs while preserving formatting by default.

## Testing

Extend contract generation tests to regenerate in a temporary SDK copy, checking byte equality for formatted output and AST equality for unformatted output.

GitOrigin-RevId: 9ca7daa8ac0a33c4106a9e95c2f48bbded07df21
This commit is contained in:
Dmitriy Kovalenko
2026-10-14 20:59:31 +00:00
committed by copyberry
parent 12fd621f62
commit bc13ff239c
9 changed files with 298 additions and 27 deletions
+31 -6
View File
@@ -1,7 +1,9 @@
from __future__ import annotations
import ast
import os
import runpy
import shutil
import subprocess
import sys
from pathlib import Path
@@ -36,23 +38,46 @@ def _snapshot_targets(root: Path) -> dict[str, dict[str, bytes] | bytes | None]:
return {str(rel_path): _snapshot_target(root, rel_path) for rel_path in GENERATED_TARGETS}
def test_generated_files_are_up_to_date():
"""Regenerating from repository schemas should leave reviewed artifacts unchanged."""
@pytest.mark.parametrize("format_output", [True, False])
def test_generated_files_are_up_to_date(tmp_path, format_output):
"""Both generation modes preserve the reviewed API, with optional formatting."""
before = _snapshot_targets(ROOT)
sdk = tmp_path / "sdk" / "python"
sdk.mkdir(parents=True)
for directory in ("src", "scripts"):
shutil.copytree(
ROOT / directory, sdk / directory, ignore=shutil.ignore_patterns("__pycache__")
)
for name in ("pyproject.toml", "release_version.py"):
shutil.copyfile(ROOT / name, sdk / name)
env = os.environ.copy()
python_bin = str(Path(sys.executable).parent)
env["PATH"] = f"{python_bin}{os.pathsep}{env.get('PATH', '')}"
command = [
sys.executable,
"scripts/update_sdk_artifacts.py",
"generate-types",
"--schema-dir",
str(ROOT.parents[1] / "codex-rs/app-server-protocol/schema/json"),
]
if not format_output:
command.append("--no-format")
subprocess.run(
[sys.executable, "scripts/update_sdk_artifacts.py", "generate-types"],
cwd=ROOT,
command,
cwd=sdk,
check=True,
env=env,
)
after = _snapshot_targets(ROOT)
assert before == after, "Generated files drifted after regeneration"
after = _snapshot_targets(sdk)
if format_output:
assert before == after, "Generated files drifted after regeneration"
else:
assert [ast.dump(ast.parse((ROOT / path).read_bytes())) for path in GENERATED_TARGETS] == [
ast.dump(ast.parse((sdk / path).read_bytes())) for path in GENERATED_TARGETS
]
@pytest.mark.parametrize("mode", ["repository", "scratch", "experimental"])