Skip to content

Replay per-node consensus streams with a group scheduler - #8735

Merged
Amaury Chamayou (achamayou) merged 16 commits into
mainfrom
cjen1-msft-symmetrical-bassoon
Nov 19, 2026
Merged

Amaury Chamayou (achamayou) merged 16 commits into
mainfrom
cjen1-msft-symmetrical-bassoon

Conversation

@cjen1-msft

@cjen1-msft cjen1-msft commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

The Lean consensus replayer needed one globally ordered event stream, which only the single-threaded cft_driver can produce. Real nodes are separate multi-threaded processes with unsynchronised clocks, so their traces could not be replayed. This PR makes the replayer take one file of {pid, m_ts, msg} records from all nodes, in any order, and work out a valid order itself, so that traces from real networks can be checked against the model.

Within a node no clock is needed: consensus emits every event while holding the node's consensus lock, so the per-process counter m_ts already gives the order of its critical sections, whichever threads ran them. The existing reduction rules turn each node's stream into groups, one per lock hold. Across nodes, replay runs groups in wall-clock order (ts, when records carry it), but a group waits if it is not yet enabled (a receive whose packet is not yet sent, or an init_as_backup whose prefix is not yet committed), so clock skew between hosts cannot cause a false rejection.

The trace envelope now names the process identifier pid instead of process_id, and the per-process counter m_ts instead of h_ts, since it is monotonic where the event time is not.

Replaying real-node traces is not part of CI yet; #8732 adds that. #8779 separately adds a trace field that would let replay check the lock assumption instead of relying on it.

Alternatives

  • Sort all events by wall-clock time: fails from about 0.2 ms of skew between two nodes, with an unrelated model mismatch as the error.
  • Schedule by enabledness alone, with no clock: same verdict, but replays far from the order things happened in, which is slower and harder to debug.
  • Build a causal timeline before replay by matching each receive to its send: duplicates the model's packet matching outside the model.
Why the verdict does not depend on the order chosen

A node's state changes only through its own groups, which run in its lock order. Other nodes only add packets for it to receive, and a committed prefix stays committed, so a group that is enabled stays enabled until it runs.

Replay checks that a trace is consistent with the model, not that one thread could have produced its order. A trace from a process that hosts several nodes, as cft_driver does, is therefore accepted even if its records are rearranged so that a receive precedes its send; a real capture cannot have that order.

Other behaviour changes and diagnostics
  • The reduction rules are unchanged apart from bootstrap and snapshot hooks; a separate driver runs one rule at a time over each node's stream and takes what it emits as a group.
  • Snapshot-install hooks run outside the lock before a joiner's init_as_backup, so they form no group; a tick or nomination between a hook and the initialization replays as an ordinary group instead of being absorbed.
  • Bootstrap accepts a nomination, as well as ticks, before genesis.
  • If no group can run, the error lists first the groups that can never run (a receive whose packet is never sent), then those waiting for another node.
  • --schedule prints the groups in the order they ran, ending with the one that failed.
  • A repeated m_ts within a process, a node traced by two processes, or anything other than exactly one bootstrapping node is rejected. The JSON summary reports groups instead of instructions.

@cjen1-msft
cjen1-msft force-pushed the cjen1-msft-symmetrical-bassoon branch 2 times, most recently from 10a67ea to dd60783 Compare November 10, 2026 18:21
@cjen1-msft
cjen1-msft added this pull request to stack #8730 November 12, 2026 13:22
@cjen1-msft
cjen1-msft force-pushed the cjen1-msft-symmetrical-bassoon branch 2 times, most recently from 08efc82 to f561b3c Compare November 12, 2026 18:40
@cjen1-msft
cjen1-msft force-pushed the cjen1-msft-symmetrical-bassoon branch from f561b3c to f581f30 Compare November 13, 2026 15:56
@cjen1-msft
cjen1-msft force-pushed the cjen1-msft-symmetrical-bassoon branch from 4786eb5 to b785699 Compare November 13, 2026 18:02
Base automatically changed from cjen1-msft-scaling-tribble to main November 16, 2026 15:54
@cjen1-msft
cjen1-msft force-pushed the cjen1-msft-symmetrical-bassoon branch from 917310c to 4ac693d Compare November 16, 2026 16:37
@cjen1-msft
cjen1-msft marked this pull request as ready for review November 16, 2026 18:32
@cjen1-msft
cjen1-msft requested a review from a team as a code owner November 16, 2026 18:32
Copilot AI balanced review requested due to automatic review settings November 16, 2026 18:32
@cjen1-msft
cjen1-msft marked this pull request as draft November 16, 2026 18:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Waiting receive retries repeatedly render and sort the full pending queue for diagnostics that are discarded, adding substantial avoidable work even when replay succeeds.

1 open issue
What changed in this PR

This PR extends CCF's Lean consensus replayer from a globally ordered driver trace to per-node streams, allowing traces from separate processes to be scheduled against the model despite clock skew.

Changes:

  • Orders records by process-local counters, reduces node streams into atomic groups, and schedules groups when their dependencies are satisfied.
  • Adds schedule diagnostics, stream validation, and updated bootstrap and snapshot-hook handling.
  • Updates fixtures and CI checks for shuffled records and alternative cross-process schedules.

Custom instructions used

Applied the supplied CCF repository guidance; no additional repository instruction files were loaded.

File Description
lean/​consensus/​replay/​ReplayMain.lean Uses group scheduling and adds --schedule output.
lean/​consensus/​replay/​README.md Documents stream ordering and scheduling contracts.
lean/​consensus/​replay/​fixtures/​valid/​replication.ndjson Adds process identifiers and ordering counters.
lean/​consensus/​replay/​fixtures/​valid/​rejected-replicate.ndjson Adapts rejected-replication control to the input format.
lean/​consensus/​replay/​fixtures/​valid/​interleaved.ndjson Adds cross-process clock-skew coverage.
lean/​consensus/​replay/​fixtures/​valid/​bootstrap.ndjson Adds ordering metadata and pre-genesis nomination coverage.
lean/​consensus/​replay/​fixtures/​README.md Explains updated controls and overlap rejection.
lean/​consensus/​replay/​fixtures/​invalid/​unsupported-snapshot-field.ndjson Updates input metadata for unsupported-field rejection.
lean/​consensus/​replay/​fixtures/​invalid/​ungrouped-event.ndjson Updates input metadata for ungrouped-event rejection.
lean/​consensus/​replay/​fixtures/​invalid/​uncommitted-snapshot.ndjson Updates input metadata for uncommitted-snapshot rejection.
lean/​consensus/​replay/​fixtures/​invalid/​snapshot-mismatch.ndjson Updates input metadata for state-mismatch rejection.
lean/​consensus/​replay/​fixtures/​invalid/​retirement-signature.ndjson Updates input metadata for invalid retirement coverage.
lean/​consensus/​replay/​fixtures/​invalid/​retirement-seqno-mismatch.ndjson Preserves retirement sequence-mismatch coverage.
lean/​consensus/​replay/​fixtures/​invalid/​retirement-orphan.ndjson Preserves orphan-retirement rejection coverage.
lean/​consensus/​replay/​fixtures/​invalid/​retirement-nonadjacent.ndjson Preserves retirement adjacency checks.
lean/​consensus/​replay/​fixtures/​invalid/​retirement-node-mismatch.ndjson Preserves retirement node-mismatch coverage.
lean/​consensus/​replay/​fixtures/​invalid/​process-order.ndjson Adds overlapping same-process group rejection.
lean/​consensus/​replay/​fixtures/​invalid/​packet-mismatch.ndjson Updates input metadata for packet-mismatch rejection.
lean/​consensus/​replay/​fixtures/​invalid/​missing-packet.ndjson Updates input metadata for missing-packet rejection.
lean/​consensus/​replay/​fixtures/​invalid/​malformed-append.ndjson Updates input metadata for malformed-append rejection.
lean/​consensus/​replay/​fixtures/​invalid/​disabled-append.ndjson Updates input metadata for disabled-append rejection.
lean/​consensus/​replay/​fixtures/​invalid/​bootstrap-flush-send.ndjson Preserves invalid genesis-flush send coverage.
lean/​consensus/​replay/​fixtures/​invalid/​bootstrap-event.ndjson Preserves invalid pre-genesis event coverage.
lean/​consensus/​replay/​check-order.py Compares replay results across record and schedule variations.
lean/​consensus/​Consensus/​Tests/​ReplayStreams.lean Checks stream validation, waiting receives, and atomic groups.
lean/​consensus/​Consensus/​Replay/​Reduction.lean Separates snapshot hooks and exposes per-group rules.
lean/​consensus/​Consensus/​Replay/​Interpreter.lean Adds dependency-aware scheduling and blocked-group diagnostics.
lean/​consensus/​Consensus/​Replay/​Input.lean Validates ordering keys and builds per-node streams.
lean/​consensus/​Consensus/​Replay/​Groups.lean Builds process queues and rejects overlapping groups.
lean/​consensus/​Consensus.lean Imports group reduction and stream checks.
.github/​workflows/​lean.yml Checks native scenario trace order independence.
.github/​workflows/​lean-package.yml Checks fixture trace order independence.

Review effort: Balanced

@cjen1-msft
cjen1-msft force-pushed the cjen1-msft-symmetrical-bassoon branch from 1a5246d to 5ed9777 Compare November 17, 2026 13:40
@cjen1-msft
cjen1-msft marked this pull request as ready for review November 17, 2026 13:41
Copilot AI balanced review requested due to automatic review settings November 17, 2026 13:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Snapshot hooks can incorrectly start an initialization group, causing the process-overlap check to reject a tick even when the actual lock holds are disjoint.

2 open issues

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Discarding bootstrap removes the genesis replicate lock-hold span from overlap validation, allowing an impossible same-process tick interleaving to be accepted.

1 open issue
1 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 18, 2026 15:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The scheduler relies on lock-group boundaries and order-independent enabledness that need maintainer review and real multithreaded trace validation beyond CI’s synthetic and single-threaded coverage.

1 open issue

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 18, 2026 17:02
cjen1-msft and others added 16 commits November 18, 2026 17:04
Replay takes one NDJSON file of {process_id, h_ts, msg} records from all
nodes in any order. It orders each process's records by h_ts, splits them
into per-node streams, and reduces each stream into groups by grammar
alone. A scheduler repeatedly runs, atomically, the next group of any node
whose group is enabled, preferring the earliest h_ts. Receives wait for a
matching queued packet and init_as_backup for a committed source; replay
fails if groups remain but none is enabled, listing the waiting groups.

Fixtures gain process_id and h_ts. The interleaved control records the
joiner's events among the leader's, including a same-name event inside
the leader's group, receives before their sends, and initialization
before the leader commits.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
check-order.py replays each trace after shuffling its records, and after
also moving each node into its own process with a random, gapped h_ts
counter, which changes the order in which enabled groups run. CI runs it
on the fixtures and on the cft_scenario_test captures.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Each node's events are still reduced in h_ts order, which is the order of
its lock holds. Replay now treats h_ts as the order of execution within a
process, prefers the earliest wall-clock time among processes, and waits
only for a group that is not enabled, so clock skew cannot fail a trace.
Snapshot hooks, which run outside the lock, no longer absorb the groups
that fall before the initialization, and bootstrap accepts a nomination
before genesis.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Reduction.lean now only defines the rules, each consuming one lock hold of
a node. Groups.lean peeks a node's next event, runs one rule from it and
takes what the rule emitted as a group, so the rules track neither the
group's node nor where it starts and ends.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
A snapshot hook is valid if its node initializes later, which the hook rule
can read from the node's remaining events, so the rules no longer track
whether the node is initialized or a hook is pending.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The snapshot hooks stay in the init_as_backup rule, which now stops after
them when the initialization does not directly follow and requires it
later in the node's events. The rules otherwise differ from main only in
accepting a nomination before genesis.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
A group that waits is retried after every other group that runs. Its
reason rendered and sorted the whole pending queue between its sender and
receiver each time, and was discarded whenever another group could run.
The waiting check now returns the reason as a thunk, forced only by the
stuck report.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Snapshot hooks run outside the consensus lock, so they are not part of the
initialization's lock hold even when it directly follows them in the
node's events. Grouping them made the group start at the hook, and another
node's group between hook and initialization in the same process was then
rejected as overlapping. Hooks now never emit, so the group starts at
init_as_backup.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The fixtures and captures exercise input ordering and scheduling through
the replayer itself, so the hand-built groups and the replay wrapper they
needed are removed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Exported records carry pid instead of process_id, and the consensus
replayer, its fixtures and the capture tests follow.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The per-process counter in exported records is monotonic, unlike the
event time, so it is named m_ts instead of h_ts. The consensus replayer,
its fixtures and the capture tests follow.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The step rule now tries bootstrap like any other rule, at a node's first
event, and records the bootstrapping node in the rule state, so the driver
only repeats the step rule.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Events and groups carry m_ts rather than time, and each field says which
trace field it comes from.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Real-node traces, which exercise the cross-process scheduler directly, are
to be replayed in CI by a follow-up, so the script and its two workflow
steps are removed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Groups were queued per process, with overlapping groups rejected, so that
a trace edited into an order one thread could not have produced was
refused. A real capture cannot have that order, and the model already
rejects a group it is not ready for, so each node's groups now form their
own queue.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@cjen1-msft
cjen1-msft force-pushed the cjen1-msft-symmetrical-bassoon branch from e6de9e0 to c799d4e Compare November 18, 2026 17:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The scheduler’s soundness depends on inferred lock holds and order independence that need model-owner review, while real multithreaded, clock-skewed replay validation remains outstanding.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@achamayou
Amaury Chamayou (achamayou) added this pull request to the merge queue Nov 19, 2026
Merged via the queue into main with commit 1352217 Nov 19, 2026
26 checks passed
@achamayou
Amaury Chamayou (achamayou) deleted the cjen1-msft-symmetrical-bassoon branch November 19, 2026 08:38
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.

3 participants