|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Enforce that schema model/types are generated from intentproof-spec only. |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +sdk_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 6 | +cd "${sdk_root}" |
| 7 | + |
| 8 | +python3 - <<'PY' |
| 9 | +from __future__ import annotations |
| 10 | +
|
| 11 | +import ast |
| 12 | +import json |
| 13 | +from pathlib import Path |
| 14 | +
|
| 15 | +repo = Path.cwd() |
| 16 | +generated = repo / "src" / "intentproof" / "generated" |
| 17 | +types_py = repo / "src" / "intentproof" / "types.py" |
| 18 | +fingerprint = generated / "spec_fingerprint.json" |
| 19 | +
|
| 20 | +required_generated_files = { |
| 21 | + generated / "__init__.py", |
| 22 | + generated / "execution_event.py", |
| 23 | + generated / "intentproof_config.py", |
| 24 | + generated / "normative_schemas.py", |
| 25 | + fingerprint, |
| 26 | +} |
| 27 | +missing = [str(p.relative_to(repo)) for p in sorted(required_generated_files) if not p.is_file()] |
| 28 | +if missing: |
| 29 | + raise SystemExit( |
| 30 | + "check-no-handwritten-model-types: missing required generated artifacts:\n" |
| 31 | + + "\n".join(f" - {m}" for m in missing) |
| 32 | + ) |
| 33 | +
|
| 34 | +fp = json.loads(fingerprint.read_text(encoding="utf-8")) |
| 35 | +if not isinstance(fp.get("specVersion"), str) or not fp["specVersion"].strip(): |
| 36 | + raise SystemExit("check-no-handwritten-model-types: generated/spec_fingerprint.json missing specVersion") |
| 37 | +if not isinstance(fp.get("files"), dict) or not fp["files"]: |
| 38 | + raise SystemExit("check-no-handwritten-model-types: generated/spec_fingerprint.json missing per-schema hashes") |
| 39 | +
|
| 40 | +schema_model_names = { |
| 41 | + "ExecutionError", |
| 42 | + "IntentProofExecutionEventV1", |
| 43 | + "IntentProofRuntimeConfigV1", |
| 44 | + "JsonValue", |
| 45 | + "Status", |
| 46 | + "WrapOptionsV1", |
| 47 | +} |
| 48 | +
|
| 49 | +offenders: list[str] = [] |
| 50 | +for py in (repo / "src").rglob("*.py"): |
| 51 | + if py.is_relative_to(generated): |
| 52 | + continue |
| 53 | + tree = ast.parse(py.read_text(encoding="utf-8"), filename=str(py)) |
| 54 | + for node in ast.walk(tree): |
| 55 | + if isinstance(node, ast.ClassDef) and node.name in schema_model_names: |
| 56 | + offenders.append(f"{py.relative_to(repo)}:{node.lineno}:{node.name}") |
| 57 | +if offenders: |
| 58 | + raise SystemExit( |
| 59 | + "check-no-handwritten-model-types: schema model class definitions must live only in " |
| 60 | + "src/intentproof/generated:\n" |
| 61 | + + "\n".join(f" - {line}" for line in offenders) |
| 62 | + ) |
| 63 | +
|
| 64 | +tree = ast.parse(types_py.read_text(encoding="utf-8"), filename=str(types_py)) |
| 65 | +imports: dict[str, tuple[str, str]] = {} |
| 66 | +for node in tree.body: |
| 67 | + if isinstance(node, ast.ImportFrom) and node.module in { |
| 68 | + "intentproof.generated.execution_event", |
| 69 | + "intentproof.generated.intentproof_config", |
| 70 | + }: |
| 71 | + for alias in node.names: |
| 72 | + imports[alias.asname or alias.name] = (node.module, alias.name) |
| 73 | +
|
| 74 | +required_import_aliases = { |
| 75 | + "ExecutionError": ("intentproof.generated.execution_event", "ExecutionError"), |
| 76 | + "IntentProofExecutionEventV1": ("intentproof.generated.execution_event", "IntentProofExecutionEventV1"), |
| 77 | + "JsonValue": ("intentproof.generated.execution_event", "JsonValue"), |
| 78 | + "Status": ("intentproof.generated.execution_event", "Status"), |
| 79 | + "IntentProofRuntimeConfigV1": ("intentproof.generated.intentproof_config", "IntentProofRuntimeConfigV1"), |
| 80 | + "IntentProofWrapOptionsV1": ("intentproof.generated.intentproof_config", "WrapOptionsV1"), |
| 81 | +} |
| 82 | +for alias, expected in required_import_aliases.items(): |
| 83 | + if imports.get(alias) != expected: |
| 84 | + raise SystemExit( |
| 85 | + "check-no-handwritten-model-types: src/intentproof/types.py must import " |
| 86 | + f"{alias} from generated models ({expected[0]}::{expected[1]})" |
| 87 | + ) |
| 88 | +
|
| 89 | +assignments: dict[str, str] = {} |
| 90 | +for node in tree.body: |
| 91 | + if isinstance(node, ast.Assign) and len(node.targets) == 1 and isinstance(node.targets[0], ast.Name): |
| 92 | + target = node.targets[0].id |
| 93 | + if isinstance(node.value, ast.Name): |
| 94 | + assignments[target] = node.value.id |
| 95 | +
|
| 96 | +required_aliases = { |
| 97 | + "ExecutionEvent": "IntentProofExecutionEventV1", |
| 98 | + "ExecutionErrorSnapshot": "ExecutionError", |
| 99 | +} |
| 100 | +for alias, expected in required_aliases.items(): |
| 101 | + if assignments.get(alias) != expected: |
| 102 | + raise SystemExit( |
| 103 | + "check-no-handwritten-model-types: src/intentproof/types.py must alias " |
| 104 | + f"{alias} = {expected}" |
| 105 | + ) |
| 106 | +
|
| 107 | +print("OK: no handwritten schema model/types; bridge aliases map to generated modules") |
| 108 | +PY |
0 commit comments