Skip to main content

Module contract_compat

Module contract_compat 

Source
Expand description

#2485: is a service-contract change compatible, or breaking?

crate::contract reduces a service to a canonical text and an FNV-1a hash, and the deploy gate (ADR 0491) counts any difference as skew. A hash says whether a contract moved, never how. This module keeps the structure the hash throws away, so the toolchain can say whether a caller compiled against one contract still works against a callee providing another, and why.

Three pieces, all here so the deploy gate and the editor share one answer:

  1. ServiceShape: the contract as a tree, built by the same walk as crate::contract::service_normal_form and rendering back to exactly that text (ServiceShape::normal_form), so a stored shape can be checked against the hash it was recorded with.
  2. ServiceShape::to_text / ServiceShape::from_text: a compact, unambiguous text for the ledger and the build manifest. The normal form itself cannot be parsed back (AgeInt where Positive does not say where the type’s name stops and its base begins), so the ledger keeps this beside the hash instead.
  3. check_call: the classifier.

§The rule

Compatibility is about values on the wire, so the rules are the wire format’s own (reference/wire-format.md): a record is an object keyed by field name, an unknown key is ignored, a missing Option field is None, a bare value is accepted where an Option is expected, and a multi-argument call is an object keyed by parameter name. A request flows caller to callee and a response callee to caller; in both, the receiver must be able to decode everything the sender can produce. That one question, flows(sender, receiver), is asked of the request with the caller as sender and of the response with the callee as sender, which is how “add an optional request field” is compatible and “add a response variant” is breaking from a single definition.

The classifier errs towards breaking. A field missing from the sender is compatible only when it is an Option or the receiver’s field declares a default (#2689: the stored shape marks it; a shape recorded before the marker reads as having none). A wrong “compatible” lets a breaking deploy through; a wrong “breaking” only names a skew the gate already refuses.

Structs§

Accepted
#2554: what a callee accepts of the older contracts a lock lists for one service.
Compatibility
ContextAccepted
#2554: the older contracts one context’s services accept, from the contracts lock, with what was left out.
Field
Finding
One difference between the two contracts, and whether the call survives it.
ServiceShape
One on call service contract as a tree.
Variant

Enums§

Direction
Which half of the call a finding is about.
Shape
One type as it appears on the wire. Mirrors the Contract rendering of crate::contract: named types expand structurally, record fields and sum variants are sorted, predicates are a sorted set, and an opaque type’s predicate is left out.
Verdict

Functions§

accepted_for
Decide which of older (shape texts from the contracts lock) the callee whose service has current accepts.
check_call
Can a caller compiled against caller safely call a callee providing callee?
own_accepted
Which older contracts each of a context’s on call services accepts. older(service) is the lock’s shape texts for it. cap is how many a service may keep before it is reported.
own_contract_shapes
A context’s own on call shapes as ledger text, keyed by service name — the structured twin of crate::contract::own_contract_hashes, built from the same projection and the same table so each text hashes to the hash recorded beside it.