TT Lab
Blog Get started
Learn Learning paths Courses

I renumbered a field and the old client silently read the wrong value

Build the contract checker

Continue in TT Lab

Goal

You compare two .proto files and build protocheck.py, which prints wire-compatibility violations one line at a time. Adding the rules one by one, you get it to pass the 8 fixture pairs and the pairs the grader keeps hidden, and finally build check-all.sh, which runs per directory, finishing it in a form that can go into CI.

Why it matters

In the previous lab you saw that if you change one number, the old client reads a wrong value without error. That change does not stand out in code review — the diff shows only two numbers changed, and both compilation and tests pass. It is not the kind of mistake a person can catch every time. So contract checks must be done by a script, not by a reviewer. All the checker's rules come from the official documentation's list of "safe changes and unsafe changes", and what you build is a transcription of that list into a form a machine can read.

Steps

  1. Create /root/grpc/compat/protocheck.py so that python3 protocheck.py --dump <file.proto> prints, one per line, the fields of each message as <message> <number> <type> <name> and the reservations as <message> reserved <number> and <message> reserved "<name>". Ignore comments (//, /* */), whitespace, and options ([deprecated=true]), expand 9 to 11 into three numbers, and call nested messages Outer.Inner.
  2. In python3 protocheck.py <old.proto> <new.proto>, add the first rule — if an old number is not in the new file and is not reserved either, REMOVED_NOT_RESERVED <message>.<name>=<number>. If there are violations, exit 1; if none, print OK on the last line and exit 0.
  3. If the same name moved to a different number, RENUMBERED <message>.<name> <old-number>-><new-number>.
  4. If the number and name are the same but the type differs, TYPE_CHANGED <message>.<name>=<number> <old-type>-><new-type>; if the same number has a different name and type, NUMBER_REUSED <message>#<number> <old-name>:<old-type>-><new-name>:<new-type>.
  5. If the new file uses as a field a number that the old file reserved (including ranges), REUSED_RESERVED <message>#<number> <name>.
  6. If only the name differs with the same number and type, print WARN RENAMED <message>#<number> <old-name>-><new-name> but do not affect the exit code (if there are only warnings, OK · exit 0).
  7. Run all 8 pairs in /opt/app/grpc/compat/ and, in /root/grpc/compat/07-report.txt, write <case> OK or <case> FAIL, one per line. Each time it grades, the grader also feeds your checker hidden pairs (one per rule) with random names, numbers, and types.
  8. Write /root/grpc/compat/check-all.sh <dir> — for each subdirectory under it that has old.proto and new.proto, run the checker and print <case>: OK / <case>: FAIL, and exit 1 if there is even one FAIL. The grader builds and runs directories whose subdirectory names, count, and verdicts differ every time.

Notes

Build a parser that reads .proto

Create /root/grpc/compat/protocheck.py so that --dump <file.proto> prints, one per line, the fields as <message> <number> <type> <name> and the reservations as <message> reserved <number> / <message> reserved "<name>".

Direction: you can trust the checker only if the parser is lenient — filter out comments, whitespace, and options, and call nested messages Outer.Inner.

Tools: erasing // and /* */ comments with re.sub, finding message <name> { and counting braces (recursing for the inside), and a regex that catches one field line (<type> <name> = <number>;).

Specifics: split reserved 9 to 11, 15; by commas and, if there is a to, expand it as a range. Discard a leading optional/repeated and a trailing [deprecated=true]. The grader also feeds it never-before-seen files that mix these.

Catch numbers deleted without reservation

In python3 protocheck.py <old> <new>, add the rule REMOVED_NOT_RESERVED <message>.<name>=<number>. If there are violations, exit 1; if none, OK on the last line · exit 0.

Direction: for each message, look at the old file's numbers one by one and check whether each is in the new file, and if not, whether it is in the new file's reserved.

Tools: the fields and reserved sets from the step 1 parse result, and the exit code (sys.exit).

Specifics: remove_no_reserved and nested (Customer.tier) must fail, and safe_add and safe_reserved must be OK. If there are no violations, OK on the last line and exit 0; if there are, exit 1.

Catch numbers that moved

Add the rule RENUMBERED <message>.<name> <old-number>-><new-number>.

Direction: if the same name moved to a different number, that is the same as deleting it and creating a new one. In that case, report that number in one line only.

Tools: a "name → number" dictionary of that message in the new file (if you build it over the whole file, you misreport the same name in another message as a number move).

Specifics: for a number reported as RENUMBERED, do not run other checks such as REMOVED (one line per cause). The renumber fixture gives two lines, for id and qty.

Catch type changes and number reuse

Add the rules TYPE_CHANGED <message>.<name>=<number> <old-type>-><new-type> and NUMBER_REUSED <message>#<number> <old-name>:<old-type>-><new-name>:<new-type>.

Direction: if the same number is on both sides but the type differs, an old parser reads those bytes with a different meaning. If the name differs too, it is number reuse.

Tools: comparing the (name, type) pairs of the same number.

Specifics: the number remains in the new file, so it is not REMOVED. A type change inside a nested message (Order.Item) must also be caught by the per-message comparison.

Catch reserved numbers being taken out and used

Add the rule REUSED_RESERVED <message>#<number> <name>. The old file's reserved ranges (10 to 12) are included too.

Direction: reserved is not a promise only to protoc — the checker must also remember the old file's reservations.

Tools: the set of reserved numbers you expanded in step 1, and in.

Specifics: if a field number in the new file is in the old file's reserved (ranges included), it is a violation. With reserved 10 to 12, using 11 fails and 13 is OK.

A rename is only a warning

If only the name differs with the same number and type, print WARN RENAMED <message>#<number> <old-name>-><new-name> but do not affect the exit code. If there are only warnings, OK · exit 0.

Direction: there are no names on the binary wire, so a change of name alone is a warning, not an error (you only need to tell the people who use JSON or text formats).

Tools: keep a warning list and an error list separately, and let the exit code look only at the error list.

Specifics: print warnings first, and if there are no errors, OK at the end. rename_only is one WARN line + OK, and a pair where the name and type changed together produces WARN and TYPE_CHANGED together and exit 1.

Run the 8 fixture pairs and the hidden pairs

Run all 8 pairs in /opt/app/grpc/compat/ and, in /root/grpc/compat/07-report.txt, write <case> OK or <case> FAIL, one per line. The grader compares this report with your checker's actual verdicts, and each time it grades it also feeds your checker newly made hidden pairs (one per rule).

Direction: you do not write the report by hand; you transcribe the exit codes your checker produced.

Tools: a shell loop over the fixture directories, and exit codes.

Specifics: the grader reruns your checker and compares it with the report, and each time it grades it also feeds newly made hidden pairs (one per rule, with random message names, field names, numbers, and decorations) — a checker that memorized and hard-coded the fixture results fails there.

A directory-level check script

Write /root/grpc/compat/check-all.sh <dir> — for each subdirectory that has old.proto and new.proto, run the checker and print <case>: OK / <case>: FAIL, and exit 1 if there is even one FAIL.

Direction: this is the shape that goes into CI — sweep only the directory given as the argument, and end with 1 if even one is FAIL.

Tools: a "$DIR"/*/ loop, [ -f … ] to check that both files exist, a variable that counts failures, and $(dirname "$0") to find where the script is.

Specifics: look only at subdirectories that have both files (skip files and empty directories). The grader builds and runs directories whose names, count, and verdicts differ every time — if you hard-code paths or names, you fail.