To test whether Protobuf unknown fields survive a binary relay, send a message written with the old schema through the candidate relay, then read its output with the old schema. Assert that the retired field is still present and has its original value. A successful parse at the relay is not enough: the relay can forward every field it recognizes and still lose the one an older consumer needs.
Last reviewed: 2026-11-22 (Asia/Shanghai).
Why a reserved tag still travels
Suppose an agent removes legacy_queue from a job schema. The relay no longer uses it, but a worker on the previous release still does. When the new relay parses an old binary job, field number 2 is unknown to the new schema. The proto3 language guide
documents that binary parsing retains unknown fields for serialization. Updating a known field on the parsed message and serializing it can therefore preserve the old queue value without giving the new relay a generated accessor for it.
Reserve the retired number and name in the candidate schema. The number reservation prevents a future change from assigning tag 2 a new meaning. The name reservation prevents its reuse in the schema, which matters if the message has also been represented in a format that uses field names. Neither reservation deletes old bytes or makes a JSON reader accept a removed key. These are constraints on future declarations, not instructions to the binary parser.
The review has two separate questions: did the schema prevent reuse, and did the actual relay preserve what it didn’t understand? Compilation can answer the first. It cannot answer the second.
Give the old writer and new relay different schemas
Use a compatible protoc and Python protobuf runtime, preferably the pair pinned for the project. In an empty working directory, create baseline/ and candidate/. The baseline is the schema used by both the old writer and the old reader. Use this as baseline/job.proto:
syntax = "proto3";
package queue_demo;
message Job {
string job_id = 1;
optional string legacy_queue = 2;
string relay_note = 3;
}
Use this as candidate/job.proto:
syntax = "proto3";
package queue_demo;
message Job {
reserved 2;
reserved "legacy_queue";
string job_id = 1;
string relay_note = 3;
}
Both versions intentionally have the same message name. Only the old schema declares legacy_queue. The optional label makes it possible for the baseline reader to check presence separately from the value; testing only a default string can hide an absent field. The Python generated-code guide
documents the generated module path and how HasField() works for fields with explicit presence.
Generate the modules into separate directories. Keep the fixture script outside both directories and use a fresh Python process for each step, so no process imports both definitions of queue_demo.Job.
protoc --proto_path=baseline --python_out=baseline baseline/job.proto
protoc --proto_path=candidate --python_out=candidate candidate/job.proto
Replay the same job through two relay implementations
The fixture writes west into the retired field and asks the relay to set relay_note. The note is a control: the check should fail if it accidentally reads the original file and skips the relay. The rebuild mode is a negative control. It copies job_id into a fresh object and adds the same note, so the known fields still look right even when the retired one disappears.
Use this script as replay.py in the working directory:
import sys
from pathlib import Path
from job_pb2 import Job
mode, filename, *extra = sys.argv[1:]
input_path = Path(filename)
if mode == "write":
job = Job(job_id="job-17", legacy_queue="west")
input_path.write_bytes(job.SerializeToString())
else:
job = Job()
job.ParseFromString(input_path.read_bytes())
if mode in ("relay", "rebuild"):
if mode == "rebuild":
job = Job(job_id=job.job_id)
job.relay_note = "checked"
Path(extra[0]).write_bytes(job.SerializeToString())
elif mode == "read":
if (job.job_id, job.relay_note) != ("job-17", "checked"):
raise SystemExit("FAIL: known fields do not match")
if not job.HasField("legacy_queue"):
raise SystemExit("FAIL: retired field is absent")
if job.legacy_queue != "west":
raise SystemExit("FAIL: retired field value changed")
print("PASS: old reader recovered the retired field")
else:
raise SystemExit("unknown fixture mode")
Run the writer, the binary relay, and the baseline reader in that order. PYTHONPATH selects the generated schema for each process.
PYTHONPATH=baseline python3 replay.py write original.bin
PYTHONPATH=candidate python3 replay.py relay original.bin forwarded.bin
PYTHONPATH=baseline python3 replay.py read forwarded.bin
The final command should print PASS: old reader recovered the retired field and exit zero. Next, run the negative control separately:
PYTHONPATH=candidate python3 replay.py rebuild original.bin rebuilt.bin
PYTHONPATH=baseline python3 replay.py read rebuilt.bin
The last command should exit nonzero with FAIL: retired field is absent. A missing file or an import error is not the expected failure. If both reads pass, check which relay mode ran and which file each reader received. The goal is the same as in fault detection for agent-written tests
: prove that the check rejects the specific mistake it is meant to catch.
Find the first hop that loses the value
Replace the passing relay command with the candidate path the job actually takes. Test each hop by feeding its output to the baseline reader, with expected values for any known fields that hop legitimately changes. The small fixture isolates library behavior; it cannot tell you whether an adapter routes the real message through a dictionary, constructs a new message from selected properties, or writes JSON for an intermediate queue.
If the retired field disappears after rebuilding a message, keep the parsed message and change only the fields the relay owns. If the implementation needs a separate object of the same message type, the proto3 guide recommends message-level copying such as CopyFrom() rather than a loop over known fields. Re-run the fixture after changing that code. Compare decoded presence and value, not whole-file equality: the relay deliberately changes the note, and field ordering in the serialized output is not the behavior under test.
If the field disappears at a JSON boundary, the repair is different. ProtoJSON does not carry binary unknown fields through a conversion. If an older JSON writer emits a removed key, a reader can reject it by default or discard it with an ignore-unknown option. Neither outcome preserves it. If the downstream contract requires the value, retain the field in the schema used at that boundary or keep the relay binary. Treat a mandatory JSON transport as a separate compatibility path and test it with its actual reader settings.
Decide when it is safe to stop writing the field
Preserving a field that arrived on an old message does not give new writers a way to set it after removing the declaration. If a new producer can originate jobs while an old worker still needs legacy_queue, a passing relay test does not make the rollout safe. Keep the field on that producer until the worker can handle a message without it, or arrange a rollout that prevents that pairing. Include rollback readers and replay from queues or archives in the decision; the age of the deployment is not necessarily the age of the oldest message it can read.
For the pull request, start with one synthetic message from the oldest schema still supported for replay. Require two results: the candidate relay preserves the retired value when it is present, and the supported consumer behaves as intended when a new writer omits it. Keep both assertions tied to named producer, relay, and reader versions. Ask for those exact checks and a restriction against changing serialization format in the coding-agent task brief . A reviewer can then tell whether the patch retires an unused accessor or changes what a downstream worker receives.
Sources checked
- Language Guide (proto 3) : reserved numbers and names, binary schema changes, and retention of unknown fields.
- ProtoJSON Format : schema evolution and the difference between ignoring and preserving unknown data.
- Python Generated Code Guide : compiler output layout, generated message classes, and explicit field presence.