anthropics / anthropics/buffa

buffa-build: path-scoped preserve_unknown_fields_in for per-message unknown-field preservation

Aperta
#281 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
enhancement
Lingua principale
Rust
Stelle
883
Fork
88
Merge medio
3g 19h
PR unite (30g)
42

Descrizione

## Motivation

`preserve_unknown_fields(bool)` is currently all-or-nothing. Disabling it is a legitimate memory optimization (drops the `UnknownFields` Vec header from every message), but it also silently removes round-trip fidelity everywhere — including on the handful of message types that actually get re-encoded, forwarded, or persisted. #269 is a concrete case: a client disabled preservation globally for memory and then lost unknown closed-enum values on forwarding paths.

A path-scoped variant lets a build keep the memory savings globally while retaining wire fidelity on the specific types that need it:

```rust
buffa_build::Config::new()
.preserve_unknown_fields(false) // global default
.preserve_unknown_fields_in(&[".wa.CallLogRecord", // per-message overrides
".wa.SyncdMutation"])
```

## Shape

- Follows the existing `_in` convention (`bytes_type_in`, `string_type_in`, `unbox_oneof_in`): proto-path prefixes resolved through `matches_proto_prefix`, global default first, last matching rule wins.
- **Granularity is per-message.** The flag gates whether the message struct carries `__buffa_unknown_fields: UnknownFields` at all, so rules match message FQNs and package prefixes. Per-field granularity is not representable in the storage model.
- Nested/child messages resolve independently: a preserved outer message retains only its own-level unknown fields; each nested type is governed by its own matching rule.

## Implementation notes

The global bool is consulted in several places today — struct field emission (`message.rs`), the decode arms (including the closed-enum unknown route), encode, the view decoders, and the JSON/extension-range interaction. The change is mechanical: resolve the flag per message from `(global, rules)` instead of reading one bool, and thread that resolution through the existing call sites.

## Priority

Low. The default configuration already preserves unknown fields everywhere; this only benefits builds that opt out for memory and want selective fidelity back. For the #269 use case specifically, a scoped closed-enum openness override (discussed on that issue) is the more direct fix; this option is complementary.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.