Skip to content

Serde default helpers are named from their struct and field - #2094

Open
KaloyanNaumov wants to merge 7 commits into
ClickHouse:mainfrom
KaloyanNaumov:pr/codegen-stable-defaults
Open

KaloyanNaumov wants to merge 7 commits into
ClickHouse:mainfrom
KaloyanNaumov:pr/codegen-stable-defaults

Conversation

@KaloyanNaumov

@KaloyanNaumov KaloyanNaumov commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Serde default helpers are named from their struct and field

Closes #2093.

sdk_rust_gen.py named each serde default helper with a global counter ("default_" + str(len(self.defaults))), so the 2,578 helpers in wire_models.rs were positional: one inserted defaulted field renamed every helper after it — 9,921 changed lines for a five-line insertion, 9,916 of them pure renaming.

Each helper is now named for the struct and field it serves — default_context_pack_output_blocks — with a _2, _3 suffix only when a genuine name collision arises (none today). Emitter.defaults became a dict keyed by helper name so the naming function still owns uniqueness, and both emit sites join its values.

This PR carries the one-time renumber: 2,578 helper names move once, ~10,370 changed lines in wire_models.rs and 28 in tests/schema_mapping.rs. Names-only it is identical — stripping default_[a-z0-9_]+ and whitespace from old and new compares equal, the whitespace-blind compare being needed because longer names re-wrap a few attributes. This is the last time this renumber happens; every later insertion changes only its own field.

Tests, each red against the old naming and green here:

  • test_inserting_a_defaulted_field_renames_no_other_helper — two model sets differing by one defaulted field; every untouched field keeps its helper name.
  • test_two_struct_fields_flattening_alike_take_a_suffix — FooBar.baz and Foo.bar_baz both flatten to default_foo_bar_baz, so the second takes _2; each declaration's attribute and both helper bodies are asserted, so the suffix is required rather than cosmetic.
  • test_a_second_literal_for_the_same_struct_field_takes_a_suffix — one owner asking twice (the dedupe loop), which an earlier version of this test mislabelled as a collision.
  • test_one_inserted_field_changes_only_its_own_lines_in_every_sdk — inserts a field, regenerates all six SDKs and bounds each diff: Rust ≤6, Python ≤2, Python client 0, TypeScript ≤2, Ruby 0, Go ≤4. Red on the old naming gave assert 9083 <= 6.

sdk_rust_gen.py --check and --fixtures --check are current under rustfmt 1.85.1 (the codegen pin); the six generated-SDK suites are 78 passed; cargo test --locked is green across lib, client, generated contracts, models, schema mapping and registry contracts.

One note for the CI reader: sdk/rust/tests/client.rs has a pre-existing timing flake — on a loaded machine the worker can return at its 5 s deadline after the listener is dropped, which surfaces as tcp connect error 127.0.0.1:…. One run failed that way, then the suite passed 164/164 twice. A red client run is not this change; ask for a rerun.


Note

Low Risk
Codegen and generated Rust SDK layout only; behavior of defaults is unchanged aside from stable naming and file split.

Overview
Renames generated Rust serde default helpers from positional names (default_0, …) to stable names derived from struct and field (default_knowledge_add_output_probe_stable), with _2 suffixes only on real collisions. Emitter.defaults is now a dict keyed by helper name so attributes stay tied to the right function.

Splits serde default functions into a new generated wire_defaults.rs, imported from wire_models.rs, so neither file exceeds a static analyser’s per-file size limit. Full registry generation writes both files; --out previews still emit a single artifact with helpers inlined. Release packaging and docs list wire_defaults.rs as a owned/generated artifact.

This PR includes the one-time bulk rename of ~2,578 helpers in generated output. New unit tests lock in naming stability (inserting a field doesn’t rename unrelated helpers), suffix rules, and bounded line churn when a model gains one defaulted field across SDK generators.

Reviewed by Cursor Bugbot for commit 0c73ebd. Bugbot is set up for automated code reviews on this repo. Configure here.

`default_attr` named each helper from a running counter, so a defaulted
field inserted mid-file renumbered every later `#[serde(default =
"default_N")]`.  Adding one field to `ContextPackOutput` and regenerating
changed 9,921 lines of `sdk/rust/src/wire_models.rs`; after normalising
`default_\d+` the same diff is 5 lines.

A helper is now `default_<struct>_<field>` in snake case, with a numeric
suffix only when two helpers genuinely collide.  `Emitter.defaults` stays
keyed by the helper name, so the naming function still owns uniqueness.

This regenerates `wire_models.rs` and the schema fixtures once: 2,578 helper
names move together, and the file is structurally identical to the old one
once names and their line wrapping are ignored.  It is the last time they
move; the name no longer depends on position in the file.

The regression test generates two model sets that differ by one defaulted
field and asserts every untouched helper keeps its name.  A second test runs
an inserted field through all six SDK generators and holds each artifact to
a diff of that field's own lines, so a named helper that ever starts to
churn in another language fails here.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
@vercel

vercel Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
loom-docs Ready Ready Preview Dec 5, 2026 2:12am UTC
loom-telemetry Ready Ready Preview Dec 5, 2026 2:12am UTC

Request Review

@KaloyanNaumov
KaloyanNaumov deleted the pr/codegen-stable-defaults branch December 3, 2026 15:17
@KaloyanNaumov
KaloyanNaumov restored the pr/codegen-stable-defaults branch December 3, 2026 15:25
@KaloyanNaumov KaloyanNaumov reopened this Dec 3, 2026
`test_a_genuine_helper_name_collision_takes_a_suffix` asked `default_attr`
twice for the same `Contract.value` with different literals.  That is one
owner requesting two helpers, a case the emitter never meets from a real
schema, so the test named for a collision could not fail if two distinct
struct/field pairs stopped being told apart.

Two owners collide when their snake-cased names coincide: `FooBar.baz` and
`Foo.bar_baz` both flatten to `default_foo_bar_baz`.  The new test emits
both structs with different literals and asserts the second helper is
`default_foo_bar_baz_2` with a body of its own.  Flipping the base name
back to `"default_" + str(len(self.defaults))` fails it on the first
attribute, as it fails the renamed same-owner test.

The same-owner case is kept under a name that says what it does: a second
literal for one struct field takes a suffix.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
@zlareb1 zlareb1 self-assigned this Dec 3, 2026

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No blocking findings. The 2,578 generated helpers and seven fixture helpers retain their original bodies and field bindings. The focused generator suite passed all 53 tests, including suffix collisions and the six-SDK insertion check.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The generator preserves the existing defaults, but the regenerated Rust file exceeds CodeQL’s per-file limit. The Rust analysis stops before extraction.

Comment thread scripts/codegen/sdk_rust_gen.py Resolved
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
@KaloyanNaumov

Copy link
Copy Markdown
Contributor Author

Fixed as you suggested — the helpers now live in their own module.

  • sdk/rust/src/wire_defaults.rs (new, generated): the 2,578 default_* helpers as pub(crate) fn.
  • sdk/rust/src/wire_models.rs: every public path, OP_POLICIES, NAMESPACE_HINT_EXCLUSIONS and every default = "default_<model>_<field>" attribute unchanged; one glob import added.
  • lib.rs declares the module; --out previews stay single-file; the release manifest requires the new file.

wire_models.rs 1,831,593 bytes (was 2,153,729) — 265,559 under the 2 MiB limit; the helpers file is 350,812.

Proofs: all five artifacts --check current; --fixtures --check current; cargo fmt --check exit 0; cargo check --all-targets --locked finished clean; 2,578 helpers with equal names, all attributes resolving, and bodies token-identical (205 differ only in rustfmt wrapping); registry_contracts 379 passed, schema_mapping 3 passed, test_sdk_rust_codegen.py 54 passed.

zlareb1
zlareb1 previously approved these changes Dec 3, 2026

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The generated defaults are in their own module. Both files are under CodeQL’s size limit, and the public model paths stay the same. All 105 focused generator and release-verifier tests passed.

@zlareb1 zlareb1 mentioned this pull request Dec 3, 2026
2 tasks done

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The pinned Biome check finds the same unformatted dashboard assertion in this run and on clean main. The separate repair formats that assertion. Failed job · Repair #2107

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

GitHub reports a merge conflict with main in sdk/rust/src/wire_models.rs. Please reconcile the wire schema changes and regenerate the Rust models so CI can validate the combined tree.

main grew the registry (17 commits since 9e82a75; registry_contracts 379 ->
380) while this branch moved the serde default helpers into src/wire_defaults.rs,
so the only conflicted path was the generated src/wire_models.rs: 54 hunks, all
inside the RunCompleteOutput field block where main added attributes the branch
had renamed.

Resolved by taking main's registry-correct output and regenerating; no generated
file was hand-edited:

  git checkout --theirs -- sdk/rust/src/wire_models.rs
  python -m scripts.codegen.sdk_rust_gen

Both modules current after the merge: wire_models.rs 1,837,804 B (259,348 B under
CodeQL's 2,097,152 B per-file limit), wire_defaults.rs 351,866 B. 2,586 serde
helpers, 2,586 default attributes, every attribute resolving across the module
boundary; lib.rs keeps `mod wire_defaults;` beside `mod wire_models;`, so the
public model paths are unchanged.
@cursor

cursor Bot commented Dec 4, 2026

Copy link
Copy Markdown

This PR is too large for Bugbot to review. It changes 20,963 lines and 1,147,922 characters. Split the change into smaller pull requests to get a review.

zlareb1
zlareb1 previously approved these changes Dec 4, 2026

@zlareb1 zlareb1 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The generated Rust types keep their Serde defaults. The crate verification includes the new helper module.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The enrollment spend assertion in this unit job also fails on the clean CI base. It passes with the fix in #2132 and on current main. The existing run still fails. Failed job Merged repair

@zlareb1

zlareb1 commented Dec 4, 2026

Copy link
Copy Markdown
Member

@KaloyanNaumov please fix merge conflicts

# Conflicts:
#	sdk/rust/src/wire_models.rs
zlareb1
zlareb1 previously approved these changes Dec 4, 2026

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The regenerated Rust models and default helpers still fit in separate files. The new worker mode is a Serde enum with the API’s string values. Both generator freshness checks passed, and all 55 focused generator tests passed.

Regenerate sdk/rust/src/wire_models.rs and sdk/rust/src/wire_defaults.rs
with the Rust generator instead of merging them by hand.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
Assisted-by: Claude Code:claude-sonnet-5-5
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
@KaloyanNaumov

Copy link
Copy Markdown
Contributor Author

Merged main (8c9e1dd) as 0c73ebd and regenerated wire_models.rs and wire_defaults.rs with sdk_rust_gen.py, not by hand. Every codegen --check is clean, and the touched tests pass. The "introduced dependency vulnerabilities" failure is not an advisory: the job reports "Dependency snapshots are incomplete and snapshot inputs changed", which it raises for any change under sdk/rust/ when the head has no dependency snapshot. This PR changes no Cargo or Maven file. Please re-review @zlareb1.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The generated Rust models and default helpers are each below the CodeQL source limit. The generator matches the checked-in files, and all 55 focused generator tests passed.

This branch has not been deployed

No deployments
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.

A Rust default helper's name comes from a global counter, so one inserted field renames all later helpers

2 participants