Skip to content

Use Buf to guard protobuf wire compatibility - #1378

Draft
jwils wants to merge 2 commits into
mainfrom
joshuaw/buf-breaking-validation
Draft

jwils wants to merge 2 commits into
mainfrom
joshuaw/buf-breaking-validation

Conversation

@jwils

@jwils jwils commented Sep 11, 2026 •

Copy link
Copy Markdown
Collaborator

Why

Stable field numbers stop protobuf data from being misread, but nothing catches a type change at an existing number. Old messages are then decoded wrong: string to int32 decodes as 0, and int64 to int32 silently truncates. Maintaining those compatibility rules ourselves would duplicate Buf.

What

  • Before dumping a changed schema.proto, compile the existing and proposed schemas with Buf and apply its WIRE breaking-change rules.
  • Reject breaking changes. There is no flag to accept one, because protobuf has no schema version to bump.
  • Document which changes pass and which fail in the README.

How

The indexer reads binary messages, so WIRE checks what must stay compatible: field numbers, wire types, cardinality, package, and reservations. The stricter FILE, PACKAGE, and WIRE_JSON categories also protect generated source code or JSON names, so they reject routine evolution that is safe for ingestion:

Change FILE WIRE
Add a field pass pass
Remove a field (ElasticGraph emits reserved N;) fail pass
Rename a field with renamed_from fail pass
Rename a type fail pass
Int to JsonSafeLong (int32 to int64) fail pass
String to Int (string to int32) fail fail
Single value to list fail fail
Restore a removed field (drops its reserved line) fail fail

Both schemas are compiled at one logical path so file-level changes compare correctly. External imports resolve through the project's Buf configuration and are excluded from the comparison.

Restoring a removed field under its old name fails, because ElasticGraph reassigns its old number and drops the reservation. That also blocks restoring it with a different type. The README tells users to pick a new name.

Risk

Adds a development-time dependency on the Buf CLI whenever an existing schema.proto changes. First-time generation and unchanged schemas don't invoke Buf. CI now installs Buf 1.72.0 in every ci-check job, not only run_misc_checks, so the specs that use the real CLI run everywhere.

Testing

  • New :requires_buf specs run the real Buf CLI. They check the detector against each row above, and do two end-to-end dumps: a field removal goes through with reserved 2;, and Int to String is refused. Where Buf isn't installed they're skipped. With the config temporarily switched back to FILE, the remove, rename, and widen specs fail.
  • script/run_gem_specs elasticgraph-proto_ingestion: 140 examples, 0 failures, 100% line and branch coverage. The real-Buf specs also pass with Buf 1.72.0 and 1.75.0.
  • script/lint, script/type_check, script/spellcheck, script/update_ci_yaml --verify, and script/validate_readme_snippets -f elasticgraph-proto_ingestion/README.md (14 snippets) all pass.

References

@jwils
jwils added this pull request to stack #1381 September 11, 2026 20:58
@jwils jwils changed the title joshuaw/buf breaking validation Use Buf to guard protobuf schema compatibility Sep 11, 2026
@jwils
jwils removed this pull request from stack #1381 September 13, 2026 15:10
@jwils
jwils force-pushed the joshuaw/buf-breaking-validation branch from cd9bb00 to 4fd140d Compare September 13, 2026 15:24
jwils added a commit that referenced this pull request Sep 13, 2026
)

## Summary

Closes #1360.

`schema.proto` only had messages for the indexed types and the types
they reference, so a publisher could not send an event for a pure-source
type: one that feeds a `sourced_from` field but has no index of its own.
#1346 widened the JSON schema event envelope to accept these events, but
proto ingestion never got the same treatment. Nothing in an indexed
type's proto representation refers to a pure-source type, so the
traversal never reached it.

Both JSON and proto now use `Results#ingestible_types_by_name` to select
indexed document types and `sourced_from` source types, excluding
derived indexing types. `Results` resolves the completed schema once and
passes the shared map to both builders. Proto traverses referenced types
and emits abstract wrappers; JSON selects concrete event names.

The messages themselves needed no change, as #1360 predicted.
`proto_fields` builds from `indexing_fields_by_name_in_index`, which
does not depend on the type having an index:

```protobuf
message ComponentDesign {
  string id = 1;
  string component_id = 2;
  string designer_name = 3;
  // Next field number: 4
}
```

## Derived indexing types (a Buf-breaking change)

Derived indexing types are now excluded from the seed. ElasticGraph
builds their documents from the events of other types, so a publisher
never sends one, and the JSON schema artifact has never described them
either. Proto ingestion described them only because a derived type has
its own index, which put it in the old seed.

Removing a message is a breaking change under the Buf check that #1378
adds. No published schema depends on these messages yet, so this is the
moment to align the two formats.

## Abstract source types

A `sourced_from` source type can be an interface or a union. Such a type
keeps the `oneof` wrapper message it would get as an indexed abstract
type, and its subtypes get messages too.

Note the JSON event envelope does not accept an abstract type by name,
and it does not accept the concrete subtypes either, because they are
neither indexed nor source types. So an abstract source type is not
publishable over JSON today. That gap is tracked in #1386 and is not
addressed here.

## Notes

- No schema artifact churn. The root `Rakefile` does not register the
proto extension, and this repo commits no `schema.proto`.
- Proto schema generation now runs the `sourced_from` resolver, so it
can raise relationship and `sourced_from` validation errors that it
previously skipped. No existing proto spec defines a relationship, so no
fixture needed a repair.
@jwils
jwils force-pushed the joshuaw/buf-breaking-validation branch from 4fd140d to 26abc62 Compare October 2, 2026 17:15
Buf's FILE rules also protect generated source code and JSON names,
so they rejected removing a field, `renamed_from`, type renames, and
`int32` to `int64` widening. With no escape hatch, a schema could
never drop or rename a field once `schema.proto` existed. The indexer
reads binary messages, so the WIRE rules cover what must stay
compatible.

Add specs that run the real Buf CLI to pin which changes pass and
fail, install Buf for every CI job so they run, and update the README
and error guidance.

Co-authored-by: Josh Wilson <joshuaw@squareup.com>
Signed-off-by: Josh Wilson <joshuaw@squareup.com>
@jwils
jwils force-pushed the joshuaw/buf-breaking-validation branch from 26abc62 to d54e53f Compare November 21, 2026 03:42
@jwils jwils changed the title Use Buf to guard protobuf schema compatibility Use Buf to guard protobuf wire compatibility Nov 21, 2026

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.

1 participant