I let our programs advertise a protocol version they didn't implement
Last week I found out that two of our services had been telling each other they supported a protocol version that neither of them implemented. They connected fine, negotiated fine, and agreed on a number that described neither one. This post covers how that happened, why it took a few small decisions in a row to get there, and what I’m changing.
The setup
We have two programs that talk over a versioned protocol: an agent that runs on machines, and a controller that coordinates them. They are deployed separately. Their message types live in a shared contracts crate that both of them pin as a dependency.
The protocol has a single integer version. The contracts documentation is clear about what that number means. When a peer advertises version N, it promises: “You can send me anything defined in versions 1 through N and I’ll handle it, and I’ll send you anything from 1 through N that’s mine to send.” Each version is its own file describing what it adds, and comes with a gating method (carries_<feature>()) so the other side can check whether a peer takes a frame or command before sending it.
That design is fine. What I built on top of it was not.
What I did wrong
1. I put the “version this build speaks” in a crate that doesn’t speak
In the contracts crate I wrote roughly this:
/// The version of the protocol this build speaks.
pub const PROTOCOL_VERSION: ProtocolVersion =
ProtocolVersion::greatest(&[&EARLIER_VERSIONS, &FILED_VERSIONS]);
pub const SUPPORTED_VERSIONS: VersionRange =
VersionRange::new(MIN_SUPPORTED_VERSION, PROTOCOL_VERSION);
It computes the version automatically from the newest version file present. I liked that because adding a version meant adding one file and nothing else, so parallel changes never conflicted.
But a types crate isn’t “a build that speaks” anything. It gets compiled into two different programs, and each one implements its half of the protocol on its own schedule. The crate can name versions and offer gates for them. It has no way to know what either program actually implements. The constant was in the wrong place from the start.
2. Both programs advertised that constant as their own
The agent built its opening handshake from it:
let hello = Hello {
protocol_version: PROTOCOL_VERSION,
// ...
};
The controller built its identity and its served range the same way. Neither program had a version constant of its own. So whatever the contracts pin said became what each program claimed about itself.
3. The test that should have caught it checked the constant against itself
The agent had a handshake test asserting that the hello carries PROTOCOL_VERSION. The test built the hello from PROTOCOL_VERSION, so it could never fail. It proved the code was wired up, not that the claim was true.
4. Negotiation trusted the inputs, which is what it should do
The negotiation function accepts the version the peer offers if it falls inside the served range. Both sides pinned the same contracts commit, so the agent offered the newest version and the controller’s range ended at that same number. Every connection settled on a version neither side held.
The negotiation logic itself was correct. The bug was in what I fed it.
5. I moved the pins past the code, twice, and wrote the gap down while doing it
A run of new protocol versions landed in the contracts crate over a few days. Each one put the contract in first and left the consumer-side work on separate branches.
First, I bumped both consumers forward several versions in a change I described as adding “only what the move needs to build”. My own commit messages said the agent would handle a new message “as an agent from before this version would”, and that the controller would leave another new message “unread”. In that same change, both programs’ advertised version went up to cover those features. I wrote down the contradiction and shipped it anyway. The bar I actually held myself to was “it compiles”, not “it matches the protocol”.
Second, I made a one-file commit that moved the submodule pointer forward by seven more versions. It contained no code. Because the advertised version came from the pin, that one-line bump raised two programs’ claims about themselves by seven versions.
What it caused
- The version number stopped meaning anything. Both sides were advertising a version well past what they implemented, and in places each had implemented a different partial subset of the versions in between.
- A trap for future work. Once someone writes real
carries_*gates, each side will read the negotiated version as a promise from the other side. It will then send frames the peer rejects as unknown commands or unknown keys. That is exactly the failure the version exists to prevent. - Rollout safety was gone. The whole point of versioning was to let an updated controller serve older agents and the other way around. With the number tied to the dependency pin, an agent and a controller built from different branches could advertise the same number while holding different features.
- User-visible harm. While auditing, we found the controller had never sent some commands from long-supported versions, so some status data in checked-out files had gone stale.
- Cleanup cost. We had to audit every version in the gap on both sides, file one follow-up per version, and work out how to honestly advertise the last version we really support when parts of it have since been replaced by a later design.
Where I should have stopped
- When I defined the constant. I should have asked which build “this build” referred to. A shared types crate is compiled into more than one program, so it can’t state what any of them implement.
- When I wrote “only what the move needs to build”. That phrase was a warning sign. If my commit message says a program behaves like it’s on an older version, that program can’t advertise the newer one. Either I implement the versions I’m crossing, or I take the new types and keep advertising the old version. If the code gives me no way to do the second, that’s a defect to file before going further.
- When a one-line pin bump changed what two programs claim about themselves. A reviewer, including me reviewing my own work, should reject that on sight.
- In review and CI. Nothing anywhere compared the advertised version with the implemented features. Our pin-consistency check confirmed that all repos pinned the same contracts commit. That checks that the number is consistent, not that the claim behind it is true.
What I’m doing differently
-
Each consumer owns its advertised version. The agent and the controller each get their own advertised-version constant in their own code. The contracts crate names versions and provides gates, and stops claiming any version that something “speaks”. The hello and the controller’s served range are built from the consumer’s own constant.
-
The claim gets tested where it’s made. Each consumer keeps a table with one row per version, from the minimum it serves up to the version it advertises. Each row points to a test that exercises one of that version’s features through that consumer’s own code: a frame sent, a command handled, a field read. A guard test fails if the advertised constant goes past the table’s last row, or if the contracts define a version the table doesn’t account for. The expected values come from the version definitions, never from the constant under test.
-
A pin move is not a version move. Bumping the contracts dependency is an ordinary dependency update. The new types come in, the advertised version stays put, and the existing
carries_*gating keeps the program behaving as the version it actually claims. The advertised version only goes up in the change that implements that version and adds its table row, never in a pins-only commit. -
Negotiation stays as it is. Accepting the offered version within the served range is correct once each side’s number is honest.
-
The design now says it outright. We added an explicit requirement that each program advertises only a version whose every feature it implements, kept separate from whatever contracts commit it pins. Strictly, the protocol docs already said this. But any versioned protocol shared by independently deployed programs through a common types crate should state it from day one.
The broader lesson
I already had a few rules that should have caught this: treat a cheap fix as a red flag, never test a constant against itself, and “done” means verified end to end or a follow-up is filed. None of them fired, because I had scoped each one too narrowly. I watched for “for now” in commit messages but not for “only what it needs to compile”. I thought about self-referential tests for timeouts and defaults, not for capability claims. And I didn’t see a dependency bump as something that could change what a program claims to do.
So here is the rule I’m adding:
A capability a program advertises is a claim about that program’s own code. It is never a value inherited from a shared dependency. A dependency update that raises such a claim without implementing code is a defect on its face, however cleanly it compiles.
The compiler told me the types lined up. I treated that as proof the programs agreed on behaviour, and it wasn’t.