Skip to content

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

Merged
zlareb1 merged 16 commits into
ClickHouse:mainfrom
KaloyanNaumov:pr/codegen-stable-defaults
Dec 5, 2026
Merged

zlareb1 merged 16 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 — and nothing else, so no other struct's place in the emit order can rename it. Two owners that flatten to one name (FooBar.baz and Foo.bar_baz) stop generation with a ValueError; the registry has none today. Emitter.defaults became a dict keyed by helper name, 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_stop_generation — FooBar.baz and Foo.bar_baz both flatten to default_foo_bar_baz; either emit order stops generation with a ValueError naming the helper.
  • test_one_inserted_field_changes_only_its_own_lines_in_each_model_sdk — inserts a field, regenerates the four SDKs that emit output models and pins each diff: Rust 3, Python 1, TypeScript 1, Go 1 lines, each exactly (4 s locally). On the old naming the Rust diff was 9,083 lines.

sdk_rust_gen.py --check and --fixtures --check are current under rustfmt 1.85.1 (the codegen pin); tests/unit/test_sdk_rust_codegen.py and test_rust_sdk_release.py are 105 passed; cargo clippy --all-targets -- -D warnings is clean and cargo test is green across lib, client, generated contracts, models, schema mapping and registry contracts. The single generated wire_models.rs is 2,294,672 bytes, under the 4 MiB per-file bound the SDK static analysis job reads.

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

Medium Risk
Touches all generated Rust wire models and serde default wiring; behavior is intended to be identical after the one-time rename, but codegen output volume and naming invariants are critical for future schema edits.

Overview
Rust SDK codegen no longer assigns serde default helpers with a global counter (default_0, default_1, …). Each helper is named default_{struct}_{field} (e.g. default_response_label) using a new snake() helper, and Emitter.defaults is a dict keyed by that name instead of an append-only list.

Emit sites join em.defaults.values(); default_attr now takes model and field and fails generation if two different struct/field pairs would share the same flattened name.

This PR includes the one-time rename of generated helpers in schema_mapping.rs fixtures and the full wire_models.rs output (not in the diff snippet but described in the PR text).

Tests update expectations for the new names and add coverage that inserting a defaulted field does not rename other helpers, flatten collisions raise ValueError, and regenerating Rust/Python/TypeScript/Go after one mid-model field insertion stays a small local diff (Rust: 3 lines vs thousands under the old scheme).

Reviewed by Cursor Bugbot for commit 43e660b. 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 6:55pm UTC
loom-telemetry Ready Ready Preview Dec 5, 2026 6:55pm 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 previously approved these changes Dec 5, 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 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.

sdk/rust/src/wire_models.rs and wire_defaults.rs are regenerated with
scripts/codegen/sdk_rust_gen.py over the merged registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
zlareb1 and others added 5 commits December 5, 2026 23:16
The analyser's per-file limit is 4 MiB on main, and the one generated file
with stable helper names is 2.2 MiB, so the separate wire_defaults module
has no reason left. Revert it, pass the field to default_attr once (snake
already drops the r# prefix), and bound the Go diff for an inserted field
at its one line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
… bounds

A struct claims its name once and each field asks for one helper, so the
identical-body branch never ran; the loop suffixes on any taken name. The
cross-SDK test now expects the exact lines one inserted field adds, the
two-owner test checks each helper's literal, and the one-owner test, which
covered a call sequence the emitter never makes, is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
sdk/rust/src/wire_models.rs is regenerated with scripts/codegen/sdk_rust_gen.py
over the merged registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
The Python client and Ruby generators read no output-model field, so their
checks could not fail; the helper-subset assertion followed from the exact
Rust bound. The formatter comment now says why each formatter is skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>

@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 new naming keeps unrelated helper names steady until two fields flatten to the same name. Keep the existing helper stable when an earlier owner introduces a collision.

Comment thread scripts/codegen/sdk_rust_gen.py Resolved
zlareb1 and others added 3 commits December 6, 2026 00:09
A numbered suffix made the second owner's helper name depend on emit order,
so a colliding field added earlier renamed an existing helper. The name is
now a function of its struct and field alone, and a collision is an error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
sdk/rust/src/wire_models.rs is regenerated with scripts/codegen/sdk_rust_gen.py
over the merged registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
@zlareb1
zlareb1 merged commit 3421680 into ClickHouse:main Dec 5, 2026
74 of 75 checks passed
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