Skip to content

fix(codegen): find postal union arms by shape and pin their names - #1939

Merged
bokelley merged 1 commit into
nextfrom
fix/postal-union-arm-order-codegen
Nov 22, 2026
Merged

bokelley merged 1 commit into
nextfrom
fix/postal-union-arm-order-codegen

Conversation

@bokelley

Copy link
Copy Markdown
Contributor

Summary

adcp's Generate Python SDK check (validate-schema-bundle.yml, which checks out this repo's next) fails on every adcp PR whose bundle contains adcontextprotocol/adcp#9049:

File "sdk/scripts/post_generate_fixes.py", line 3258, in fix_postal_union_arm_order
  raise RuntimeError("generated postal_area.py has an unexpected outer union shape")

The cause is generator renumbering, not a schema defect. datamodel-codegen names classes across the whole schema tree and walks bundled/ before core/. #9049 makes four bundled protocol schemas (get-adcp-capabilities-response, get-principal-response, sync-principal-request, sync-principal-response) inline core/postal-country-system.json. Those modules now take PostalArea through PostalArea71, so core/postal_area.py's outer arms move from PostalArea1 | PostalArea2 to PostalArea40 | PostalArea41.

The fixer string-matched the old names. Renaming the match isn't enough either: aliases.PostalArea imports PostalArea1 and PostalArea2 from _generated, and the deprecated GeoPostalArea resolves to PostalArea2. With the shifted names, import adcp.types fails next.

Change

fix_postal_union_arm_order (scripts/post_generate_fixes.py) now:

  1. Finds the arms by shape. It reads the two arms off the outer PostalArea root, and the legacy arm is the plain model whose system is the legacy_postal_system enum and which declares no country (_postal_union_arms, _is_legacy_postal_arm). It still raises "unexpected outer union shape" for anything other than one legacy arm plus one other arm.
  2. Pins their names. The native arm is renamed PostalArea1 and the legacy arm PostalArea2, by identifier token via the existing _rename_code_names, so strings and comments are untouched and a swap works. It refuses if a target name is already taken.
  3. Keeps the arm-order guarantee. It rewrites PostalArea1 | PostalArea2 to PostalArea2 | PostalArea1, so legacy payloads without country don't get a defaulted country injected.

This stays one fix, so post_generation_manifest.json doesn't change: the fix fires on both today's bundle and the new one. On the current pinned bundle (3.3.0-rc.0) the generated tree is byte-identical.

Refs #1935 (this covers item 2; the selector.dimension_systems matching in item 1 is separate).

Verification

  • New and updated tests in tests/test_code_generation.py:
    • the old numbering;
    • the #9049 numbering (PostalArea40/41), with prefix decoys (PostalArea410, PostalArea4012 | PostalArea4013) and PostalArea40 inside strings and comments that must stay unchanged;
    • legacy chosen by shape, not number;
    • swapped numbering;
    • a pin clash;
    • rejection of a union with no legacy arm and of a three-arm union.
  • pytest tests/test_code_generation.py tests/test_lazy_types.py: 178 passed. make lint and make typecheck-all pass.
  • Ran the adcp job steps locally, verbatim from validate-schema-bundle.yml, against bundles built from adcp main and from adcp#9170's head. Output is in the PR comments.

🤖 Generated with Claude Code

datamodel-codegen names classes across the whole schema tree and walks
bundled/ before core/. adcp#9049 made four bundled protocol schemas
inline postal-country-system.json, so those modules now take PostalArea
through PostalArea71 and core/postal_area.py's outer arms shift from
PostalArea1 | PostalArea2 to PostalArea40 | PostalArea41.

fix_postal_union_arm_order string-matched the old names, so every adcp
"Generate Python SDK" run on a bundle containing #9049 failed with
"generated postal_area.py has an unexpected outer union shape". Matching
the new names alone is not enough: aliases.PostalArea imports
PostalArea1/PostalArea2 and the deprecated GeoPostalArea resolves to
PostalArea2, so import adcp.types fails next.

The fix now picks the legacy arm by shape (a plain model whose system is
the legacy_postal_system enum, with no country), pins the arms back to
PostalArea1 (native) and PostalArea2 (legacy) by identifier-token rename,
and then puts the legacy arm first. It still refuses any other outer
shape, and refuses a pin that would clash with an existing name. On the
current bundle the generated tree is unchanged.

Refs #1935

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@aao-secretariat aao-secretariat Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ladon verdict: Approve

Approve — codegen fix with comprehensive tests, no blocking or medium findings.

This PR reworks fix_postal_union_arm_order in scripts/post_generate_fixes.py to locate postal union arms by shape (_postal_union_arms/_is_legacy_postal_arm) and pin them to PostalArea1/PostalArea2, surviving datamodel-codegen renumbering (adcp#9049). Verified:

  • No wire-shape, public-API, or import-layering impact; generated tree is byte-identical on the pinned bundle and validated against the real #9049 bundle.
  • Fail-closes with RuntimeError on pin clash or unexpected union shape rather than emitting wrong code; reverse-ordered token-level rename leaves strings/comments untouched; idempotent.
  • Tests are comprehensive (renumbering, prefix decoys, swap, clash, 0/3-arm rejection).

Decision path: no critical/high/medium findings; high_risk false; gated_paths false; no author team gate; no prior decision. Rows 1–8 do not fire → row 9 approve.

@bokelley

Copy link
Copy Markdown
Contributor Author

Evidence: adcp "Generate Python SDK" job, run locally

The job's validate-schema-bundle.yml hard-codes ref: next, so it can't be pointed at this branch. I ran its Generate and test from the PR bundle step locally instead. The script was extracted verbatim from the workflow, run in a fresh SDK checkout and a fresh Python 3.12 venv (pip install -e '.[dev]'), against bundles built with scripts/build-schema-pr-bundle.sh. The runs were serial: the step serves its bundle on a fixed port (8765), so overlapping runs serve each other's tarballs.

adcp bundle SDK job step
adcontextprotocol/adcp#9170 head b386b2b19f this PR 576a56fda exit 0. Regenerated, validate-generated passed, 141 tests passed
adcontextprotocol/adcp#9170 head b386b2b19f next fa324dc02 exit 2: RuntimeError: generated postal_area.py has an unexpected outer union shape
adcp 034bee5234 (parent of adcp#9049) next fa324dc02 exit 0. 135 tests passed
adcp a1cebda977 (adcp#9049) next fa324dc02 exit 2: same RuntimeError

Key lines from the passing run (#9170 bundle + this PR):

SDK ref 576a56fda -> 576a56fdaef6f081c62ee0bf4028f17602c61c19
=== running job step (protocol commit b386b2b19fe0cfee932ed3b8b2935b3226345a47) ===
latest.tgz: OK
SDK bundle key '3.3.0-rc.0'; candidate bundle key '3.3.0-rc.2'
Running fix_postal_union_arm_order...
  core/postal_area.py: pinned postal union arms PostalArea40 -> PostalArea1, PostalArea41 -> PostalArea2
  core/postal_area.py: reordered 2 postal union annotation(s) to prefer legacy arm PostalArea2 over native arm PostalArea1
✓ Schemas regenerated successfully
Running fix_postal_union_arm_order...
  core/postal_area.py: pinned postal union arms PostalArea40 -> PostalArea1, PostalArea41 -> PostalArea2
  core/postal_area.py: reordered 2 postal union annotation(s) to prefer legacy arm PostalArea2 over native arm PostalArea1
✓ Generated code validation passed
141 passed in 38.29s
=== job step exit 0 ===
- SDK commit: `576a56fdaef6f081c62ee0bf4028f17602c61c19`
- Protocol commit: `b386b2b19fe0cfee932ed3b8b2935b3226345a47`
- Bundle SHA-256: `b2cc9bb15f4b28a98403806c7b7ad01efe419215bd5e68aca26e2904e7eec83f`

This matches adcp CI. The last pass was the Version Packages head on 034bee5234 (run 44914991728) and the first failure was the Version Packages head on a1cebda977 (run 44923456186).

@bokelley
bokelley merged commit 1ce27d9 into next Nov 22, 2026
41 checks passed
@bokelley
bokelley deleted the fix/postal-union-arm-order-codegen branch November 22, 2026 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant