Skip to content

Commit da60c22

Browse files
rustyconoverclaude
andcommitted
codegen: emit C++ Build*Params builders from VgiProtocol
Adds vgi.codegen.cpp_request_builders, which generates vgi/src/generated/vgi_request_builders.hpp — one inline BuildXxxParams() per RPC method, taking std::optional<T> for nullable fields. Same source-of-truth pattern as cpp_schemas: walks VgiProtocol via _common.collect_schemas() so the C++ builders can't drift from the wire schemas (three such drift bugs in one bug-hunt session). Skips one Complex method (catalog_table_create — list<list<int32>> constraints) which stays hand-coded for now. Drift + determinism enforced by tests/test_generated_cpp_request_builders.py (mirrors the existing test_generated_cpp_schemas.py shape). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 2d130e5 commit da60c22

2 files changed

Lines changed: 508 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""Drift + determinism tests for `vgi.codegen.cpp_request_builders`.
2+
3+
Sibling of ``test_generated_cpp_schemas.py``. Enforces that
4+
``vgi/src/generated/vgi_request_builders.hpp`` in the sibling ``vgi`` repo
5+
matches what the generator would emit right now. When it fails, the error
6+
message prints the regeneration command.
7+
8+
If the ``vgi`` repo isn't present next to vgi-python, the drift test is
9+
skipped (the determinism test still runs — it needs no external repo).
10+
"""
11+
12+
from __future__ import annotations
13+
14+
import io
15+
import os
16+
from pathlib import Path
17+
18+
import pytest
19+
20+
from vgi.codegen.cpp_request_builders import emit
21+
22+
23+
def _vgi_generated_path() -> Path:
24+
"""Locate ``vgi/src/generated/vgi_request_builders.hpp`` next to this repo."""
25+
override = os.environ.get("VGI_REQUEST_BUILDERS_HPP")
26+
if override:
27+
return Path(override)
28+
return Path(__file__).resolve().parents[2] / "vgi" / "src" / "generated" / "vgi_request_builders.hpp"
29+
30+
31+
_REGEN_HINT = (
32+
"To regenerate, run:\n"
33+
" uv run --project ~/Development/vgi-python python -m vgi.codegen.cpp_request_builders \\\n"
34+
" > ~/Development/vgi/src/generated/vgi_request_builders.hpp"
35+
)
36+
37+
38+
def test_generator_is_deterministic() -> None:
39+
"""Running the generator twice produces byte-identical output."""
40+
out1 = io.StringIO()
41+
emit(out1)
42+
out2 = io.StringIO()
43+
emit(out2)
44+
assert out1.getvalue() == out2.getvalue(), (
45+
"generator is non-deterministic — sorting or collection order is unstable"
46+
)
47+
48+
49+
def test_checked_in_generated_hpp_matches_generator() -> None:
50+
"""The .hpp checked into the vgi repo must match the current generator output."""
51+
path = _vgi_generated_path()
52+
if not path.exists():
53+
pytest.skip(f"{path} not found; set VGI_REQUEST_BUILDERS_HPP or check out vgi repo next to vgi-python")
54+
55+
expected = io.StringIO()
56+
emit(expected)
57+
actual = path.read_text()
58+
59+
if expected.getvalue() != actual:
60+
# Identify the first divergence to make the failure scannable.
61+
exp_lines = expected.getvalue().splitlines()
62+
act_lines = actual.splitlines()
63+
diff_index = next(
64+
(i for i, (e, a) in enumerate(zip(exp_lines, act_lines, strict=False)) if e != a),
65+
min(len(exp_lines), len(act_lines)),
66+
)
67+
context = "\n".join(
68+
f" {tag} {line}"
69+
for tag, line in (
70+
("-", act_lines[diff_index] if diff_index < len(act_lines) else "(EOF)"),
71+
("+", exp_lines[diff_index] if diff_index < len(exp_lines) else "(EOF)"),
72+
)
73+
)
74+
raise AssertionError(
75+
f"{path} differs from generator output starting at line {diff_index + 1}:\n{context}\n{_REGEN_HINT}",
76+
)
77+
78+
79+
def test_emits_at_least_a_few_builders() -> None:
80+
"""Sanity check — generator should produce a non-trivial number of inline definitions.
81+
82+
Guards against a future refactor that accidentally turns the generator into a no-op.
83+
The exact count drifts as the protocol evolves, so we only assert "at least 30".
84+
"""
85+
out = io.StringIO()
86+
emit(out)
87+
text = out.getvalue()
88+
count = text.count("\ninline std::shared_ptr<arrow::RecordBatch> Build")
89+
assert count >= 30, f"generator emitted only {count} builders — expected at least 30"

0 commit comments

Comments
 (0)