Skip to content

Use a low-level model for trace validation and prove refine Consensus/Model.lean - #8787

Closed
cjen1-msft wants to merge 25 commits into
microsoft:mainfrom
cjen1-msft:cjen1-msft-psychic-fishstick
Closed

cjen1-msft wants to merge 25 commits into
microsoft:mainfrom
cjen1-msft:cjen1-msft-psychic-fishstick

Conversation

@cjen1-msft

Copy link
Copy Markdown
Contributor

Trace validation now checks each ccf.cft trace event against M_tv, a Lean model of cft.h with one step per trace point, and proves that every accepted trace is explained by one execution of the existing consensus model: at each lock-taking event, the node's traced state is related to its state in that execution.

The previous replayer grouped events with a grammar and ran the consensus model once per lock hold, comparing one state snapshot per group (about 40% of the snapshots in a trace) and ignoring several event fields. It could not follow real multi-process nodes without extra ordering rules. The new validator reads one stream per node, never relies on cross-node order, and requires every field of every event to be either compared with the model or consumed as that step's input.

What to review

The reviewed surface is about 800 lines; the proofs and M_tv are machine-checked against it and not meant for line-by-line review. Decoding a trace file and splitting it into per-node streams is trusted.

  • TraceValidation.lean: the one theorem, accepted_refines. If the validator accepts, the observed events are exactly the input events, each is consistent with the declared state, and there is one execution of Consensus.Model.transitionSystem in which, at every lock-taking event in order, the node's model state is related to its declared state. Events inside a lock hold are checked against M_tv only; the model has no states there.
  • CCF/Observation.lean: the generic refinement statement.
  • State.lean and Abs.lean: the declared cft.h state and its mapping to the model state.
  • Event.lean, Action.lean, Observe.lean: decoding, and what each trace point is compared with.

Consensus/Model*, Properties* and Proofs/* are unchanged; properties are still stated of the model only.

Things to draw attention to

  • The state mapping is a relation (related), not equality with a projection (abs). A node holds only the ledger after its snapshot base, so the model log is related to it after dropping base entries, with a signature at the base. Send and match indexes are related only for a leader's peers, and vote sets only in the role that collects them. A node with no ledger entry yet is related to any model state that has committed nothing.
  • Validation rejects nine behaviours a correct node can produce, because the model does not allow them; other rejections are malformed traces or behaviour a correct node never shows. Each of the nine has its own diagnostic and a reachable scenario in the replay README. Eight are differences between cft.h and the model (for example cft.h keeps a peer across a rollback). The ninth is a liveness gap in the implementation: a primary that joined from a snapshot sends entries below its snapshot to a lagging follower, the host cannot read them and drops the message, so that follower cannot catch up from that primary.
  • C++ changes. replicate() decides whether the leader is alone before running the batch's hooks, so a signature batched before a configuration adding the first peer commits, as the model does. Trace events additionally carry voted_for, votes_for_me, leader_id, is_new_follower and ticking on the trace points that use them (about +4.5% on a tick-dominated trace).
  • The old replayer is deleted, with its fixtures. The executable keeps the name consensus-replay; --refine additionally runs the model in lock step at every lock release.
  • lake check runs in CI only and has not been run locally.
Evidence
  • #print axioms Consensus.TraceValidation.accepted_refines: propext, Classical.choice, Quot.sound. No sorry.
  • lake build --wfail, check-imports, lake lint, Lean format, ASCII, copyright, prettier, C++ and Python format checks pass.
  • cft_test 56/56, tracing_test 6/6, all 60 consensus scenarios pass.
  • With --refine: 60 driver traces, 8 captures from real multi-process nodes (joins, elections, snapshot joins including one spanning two terms, backup and primary retirement, nomination, restart from own ledger) and 3 valid fixtures accepted; 12 invalid fixtures rejected.
  • A mutation comparison on an earlier revision of this branch killed 95.8% of 133,906 single-field and structural mutants of the driver traces, against 52.7% for the old replayer; the survivors examined were equivalent traces. It has not been re-run on the final revision.
Size

About 28,700 lines added and 1,400 removed. Proofs are about 22,000 lines, M_tv about 2,200, the validator about 700, the generic framework in lean/common about 1,400.

cjen1-msft and others added 25 commits November 17, 2026 17:14
A batch from a leader alone in its configuration which held a signature
followed by a configuration adding a node left the signature uncommitted
until the new node acknowledged: replicate() decided whether to commit
after the batch's hooks had added the node. Decide before them, as the
Lean model commits the signature within the batch.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Move Aft's nested Votes struct to aft::Votes in cft_types.h so the trace
serialisation can name it, and add msgpack encoders for votes_for_me
(array ordered by configuration index of {idx, quorum, sorted votes}) and
for optional node IDs (nil when absent).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ccf.cft events now carry voted_for, votes_for_me, leader_id,
is_new_follower and ticking as top-level fields next to state, each only
on the trace points whose lock hold reads or writes it, with its value
when the trace point executes. Plain tick records gain only ticking,
which the election-timeout check reads.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Trace validation needs a protocol-independent statement of what an accepted
trace means, and a framework that turns per-hold simulation into that
statement. CCF.Observation states refinement of a specification at observed
trace events; CCF.Sections models event-granular nodes whose lock holds are
checked at release, and CCF.Proofs.Sections/SectionsObservation prove that
accepted section runs refine the clean network at those events.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This is the part of trace validation a reviewer reads: the declared state
(one field per cft.h member, with the ledger the node holds after its
snapshot base), the decoded trace points, the actions they map to, what each
trace point is compared with, and the state refinement mapping `abs` /
`related` from a clean node state to a declared state. Everything later in
the series is checked against these definitions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
To validate a trace, each trace point must be explained by the code between
it and the previous one. M_tv mirrors the cft.h handlers statement by
statement over a full-ledger internal state (the declared suffix plus a
ghost prefix for snapshot joiners), runs them as event-granular lock-hold
sections, and checks at every release the local invariant the proofs use,
that a snapshot base is a committed signature, and that the hold does not
depend on ledger entries below the base. Rejections of behaviour cft.h can
exhibit but the clean model cannot express are labelled as known
implementation and specification differences.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The old replayer checked driver observations against direct clean-model
transitions and could not follow real nodes or cft.h's own control flow.
The validator splits a trace into per-node streams, runs them through M_tv
with lock-hold sections, and compares every trace point with the declared
state; `--refine` additionally runs every release in lock step against the
clean model. The old Replay modules, their test and fixtures are replaced by
fixtures captured from the native driver, and CI validates them and the
scenario captures with `--refine`.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The proof that accepted traces refine the clean model reduces to one
obligation per cft.h handler. This adds the reduction: Hoare-style rules for
M_tv's handler monad, the obligation statement (`HoldSound` over the proof
relation `Rel`), the section-level simulation that discharges the framework
side from the obligations, independence of the ghost ledger prefix below a
snapshot base, and the validator's scheduling facts. It also adds the
lemma libraries several handler proofs share (AppendEntries frames, lifecycle
notes, vote counting, and `update_commit`).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
`init_as_backup`, `force_become_primary`, the out-of-lock snapshot hook and
dropping pending messages each refine zero or one clean step. For a snapshot
joiner, the ghost prefix taken from a donor's committed log satisfies the
clean network's guard on `initAsBackup`.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
A follower's `recv_append_entries` (term check, previous-index check,
conflict scan and rollback, append, commit and response) refines the clean
`receiveAppendEntries` step, keeping the invariant and the relation, with
every sent message satisfying the message invariant.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
RequestVote, RequestPreVote, their responses and ProposeRequestVote refine
the clean vote steps, including becoming leader when a quorum is reached in
every active configuration.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
A leader's handling of an AppendEntries response (match and sent index
updates, commit, retirement and successor nomination) and the nomination
trace point refine the clean steps.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
`periodic` refines the clean timeout steps: heartbeats, CheckQuorum
step-down, and election and pre-vote timeouts, with the timers that no trace
point records read from the node's next event.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
A leader's `replicate` of a batch (entry admission, configuration and
retirement hooks, peer creation, flushing AppendEntries and committing)
refines the clean append and send steps.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
TraceValidation.lean states the one reviewed theorem, `accepted_refines`:
an accepted trace is a behaviour of `Consensus.Model.transitionSystem` under
the reviewed state mapping `related`, at every lock-taking event. Its proof
assembles the fourteen handler obligations and the section framework, and
uses only the standard axioms.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ation

The consensus README, the replay README and the architecture docs now
describe the M_tv validator and what acceptance means. The replay README
lists the behaviours of cft.h that validation rejects because the clean model
cannot express them, with a reachable scenario for each, and what validation
tolerates, so a rejected real capture can be triaged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ines

`streamsMatch` compared only trace line numbers, so `accepted_refines` did
not say that the observed events are the input: observations with the right
lines but other events, or for nodes without a stream, satisfied it. It now
says that every observation is of a stream's node and that each node's
observed events are its stream's events, in order.

The validator takes each scheduled action's event from the streams
(`restream`) before certifying, so this holds by construction; the proof
does not need a lawful equality on JSON.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Only the label and the `aft::Aft` variables were required to be present, so
an event could omit a field that is a projection of the declared state, such
as `replicate_entry.seqno` or a peer's `match_idx`, or even `state`, and
still be accepted. `Observe.traceFields` now lists the fields of each trace
point as `cft_trace_msgpack.h` declares them, with `recv_request_vote` split
by message type; an event must carry exactly those. The `aft::Aft` variable
projections no longer need their own per-point table.

Add invalid fixtures for a missing `seqno` and a missing `match_idx`.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
A node joining from a snapshot taken after an election receives a term
history spanning several views. fillSnapshot rejected such snapshots,
assuming every entry up to the base was in the snapshot's view, so real
services were rejected. Take the joiner's view history up to the base from
the donor, which equals the snapshot's because both assign each committed
index its entry's term, and check only that the base is a signature in the
snapshot's view.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The files directly in TraceValidation/ should be exactly the reviewed
surface. Move the model of cft.h (handler monad, handlers, machine,
invariant, full-ledger state) to TraceValidation/MTv/ and the executable
validator and lock-step check to TraceValidation/Validator/. Only module
paths change.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…duler

The decoder and the split into per-node streams are trusted, not proved,
so they belong to the reviewed surface; the scheduler is covered by the
theorem and does not. Move Env and environment from the validator and Abs
into a reviewed Input module with one load function the CLI uses, and state
the refinement over the streams' nodes instead of the verdict's keys (equal
by validate_sound), so that the theorem reads only validate and its
observations from unreviewed code. Each reviewed header now says what a
reviewer checks it against.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The files directly in TraceValidation/ are the reviewed surface, so a
reviewer must be able to read them without M_tv, the validator or the
proofs. Add that as an architecture rule checked by check-imports: they
may import model modules, lean/common definitions other than CCF.Proofs,
the TraceValidation entrypoint and each other.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
State in the layout policy, the consensus README and the replay README
that the reviewed surface is TraceValidation.lean and the files directly
in TraceValidation/, and that MTv/, Validator/ and Proofs/ are not
reviewed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@cjen1-msft cjen1-msft closed this Nov 18, 2026
@cjen1-msft

Copy link
Copy Markdown
Contributor Author

Changes needed to fully align the model and the implementation

Behaviours validation rejects today

# Difference Suggested change
S1, S2 cft.h keeps a peer in all_other_nodes after a rollback removes the configuration that added it; the model derives peers from the log. Implementation: prune all_other_nodes in rollback() to the nodes of the remaining configurations.
S3 cft.h starts an election when ticking and not retired-committed; the model also requires a role and membership of a signed configuration. Implementation: apply the model's canStartElection condition in periodic(), or clear ticking when a rollback removes the node from every configuration.
S4 When one commit covers a peer's retirement and the leader's own, cft.h nominates a successor after erasing that peer; the model nominates from the peers before the commit. Model: nominate from the peers after the commit, so a node whose retirement just committed is never nominated.
S5 hooks.h confirms a retirement again whenever a retired-committed node's record is rewritten; the model confirms only completed, unconfirmed retirements. Implementation: emit retired_committed from the hook only when the node's status changes to it.
S6 Removing the only node of a one-node service adds an empty configuration; the model requires a non-empty one. Implementation: reject removal of the last trusted node in governance.
S7 cft.h ignores a granted pre-vote from a node in no active configuration; the model counts the pre-vote of any eligible peer. Model: count votes and pre-votes only from members of the active configurations.
S8 cft.h ignores propose_request_vote on a node that is not ticking; the model starts an election. Implementation: act on a proposal whenever the node may start an election, whether or not it is ticking.
S9 A primary that joined from a snapshot sends AppendEntries below its snapshot base; the host cannot read those entries and drops the message, so the follower never catches up from it. Implementation: this is a liveness gap, not a model difference. Detect a follower below the primary's base and have it rejoin from a snapshot, or step down in favour of a node that holds the entries.

Trace and tolerance gaps

Gap Change that removes it
A snapshot's configuration is reported at the version of the last nodes-table write (T1). Implementation: report the index at which the configuration took effect.
The snapshot's view history is not traced (T3). Implementation: add the term history to init_as_backup.
is_new_follower is set after the become_follower trace point; the switch to retired-committed and the start of ticking emit no event; force_become_primary is traced only as become_leader. Implementation: trace each at the point it happens.
Lock holds are recovered from the event order. Implementation: number lock holds in trace events (#8779).
An election timeout with no configurations is a no-op in cft.h and a pre-vote in the model (T4). Model: make it a no-op.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant