B4: Revision State Machine
ADR 0003 section 9 step B4 (issue aien-dev/synapse#28). Section 5.3 ("States") and need N14: a revision is Private (visible only to its owner's delegation chain) or Draft (visible within Read scope), then Proposed, then Verified (every claim and required action discharged on its exact tree), then Canonical (promoted by quorum, immutable). Revisions never change state backwards; replacing one creates a new revision and an obsolescence marker (B1). Builds on B1 (revisions, admission witnesses, chain dominance) and F1 (capability chains).
Code: crates/synapse-core/src/revision_state.rs (RevisionState, the order rule, the owner-move rule), crates/synapse-auth/src/state_move.rs (SignedStateMove: the mover's signed statement and its check against the chain leaf, issue #89), crates/synapse-store/src/revision.rs (state in the ledger, visibility, moves), crates/synapse-store/src/{lib,codec}.rs (WAL kind 9, replay, checkpoint), crates/synapse-api/src/lib.rs (advance_revision_state, read_revision). Tests: unit tests in crates/synapse-core/src/revision_state.rs, crates/synapse-auth/src/state_move.rs, and crates/synapse-store/src/revision.rs (b4_tests, including the b89_ tests), crates/synapse-store/tests/b4_revision_states.rs (with the shared real-F1-link fixture tests/common/mod.rs), crates/synapse-api/tests/b4_revision_states.rs. Evidence: evidence/b4-revision-states/.
1. Model and invariants
- States and order.
Private < Draft < Proposed < Verified < Canonical. The only moves are five forward edges:Private -> Draft,Private -> Proposed,Draft -> Proposed,Proposed -> Verified,Verified -> Canonical. Every edge strictly raises the rank; a move backwards, in place, or skipping a state isRevisionStateMoveInvalid. - Owner chain. A chain owns a revision when it is, or dominates, the chain of the revision's first admission witness (B1): the key that submitted it and the keys its grant descends from. A sub-delegate of the submitter (a chain that descends from it) is not an owner, and neither is a sibling or an outsider. Section 5.3 names "the key that submitted it" but does not say what a later identical submission (a B1 join) grants; B4 takes the most restrictive reading: a join adds a witness and an attestation, never an owner.
- Initial state. Every admission starts
Private(fails closed; see section 5 on the grant default). A second admission of identical content (B1 join) adds a witness and leaves the state and the owner chain unchanged: a joiner outside the owner chain cannot read the revision (so not the owner's witness or attestation), cannot find it in the change, and cannot move it; once the owner shares it, the joiner may read it but still not move it. - Visibility. A
Privaterevision is visible only to its owner chains; any other state is visible to every chain the caller'sReadscope allows. A revision a chain may not see behaves exactly like one that does not exist: reads, moves, parent references, and obsolescence markers naming it are refused withChangesetNotFoundand the same message as for an unknown revision. Change status seen by a chain lists only the revisions and markers it may see, and recomputes live, obsolete, and divergent from them. - Who may move. The owner chain may move its revision to
DraftorProposed. A move toVerifiedis refused to the owner (RevisionStateMoveInvalid): it needs claims (B3) and required-action receipts (E1) discharged on the exact tree, which no chain can assert by itself. Any move toCanonicalis refused withCanonicalOutsidePromotionQueue, checked first, whatever the revision or chain:Canonicalis reached only through the promotion queue's compare-and-swap (E2). - Determinism and replay. The ledger is a pure function of the kind 7, 8, and 9 records in sequence order. Kind 9 is permanently the owner-move record:
VerifiedandCanonicalcome only from the evidence-bearing records of B3, E1, and E2, never from kind 9. The WAL seal is unkeyed (CRC32C and an unkeyed hash chain detect corruption, not forgery), so each kind 9 record carries the mover's signature (issue #89) and replay applies it only when (1) the Ed25519 signature verifies strictly over the signed statement under the record'smover_key, andmover_keyis the audience of the chain's leaf link (the record carries that link; its digest must be the chain's last digest, its parent the previous digest or the root parent, its repository the record's), and (2) it is an owner move:Private -> Draft,Private -> Proposed, orDraft -> Proposed, by a chain that is or dominates the first admission's chain (#88). Both rules are pure functions of the record and the log that never change, so an old log keeps its meaning. Any other record (forged or corrupt) is kept in the log and in the state root but changes nothing, so replay, a checkpoint, and a live store rebuild the same states and no record sequence moves a revision backwards or pastProposed. - Unverified moves on recovery (issue #89). A kind 9 record whose signature does not verify or whose signer is not the chain leaf's audience cannot come from a store call (the store refuses such a move before writing), so it is evidence of tampering.
StrictFailClosedrecovery refuses the store withRevisionStateMoveUnverified, naming the count and the first such record;TruncateTornTailopens it, keeps the record as a no-op, and counts it.SynapseStore::state_move_replay_report()returnsStateMoveReplayReport { signature_invalid, signer_not_chain_leaf, not_owner_move, first_unverified, dropped_by_compaction }; all zero for a log written only by stores. Way out (review of PR #100):checkpoint_and_compactleaves unverified kind 9 records out of the checkpoint (they change no state), so an operator who opens the store withTruncateTornTailand compacts gets a store strict recovery accepts again, with the same revision states; the compacting process reports the count indropped_by_compaction(not persisted). Verified no-ops are kept. The strict refusal message says so. A verified move that fails the owner rule stays an ordinary counted no-op in both policies (as on #88): it depends on ledger state, while the signature check depends only on the record.
2. Operations
| Operation | Store | API (F1 chained request) |
|---|---|---|
| Move | advance_revision_state(state_move: SignedStateMove, now_ms) -> WalSeq | advance_revision_state(req, now_ms): capability Propose; payload = RevisionStateMovePayload (built with RevisionStateMovePayload::sign_new); chain = the request's chain; mover key = the request's agent key; leaf link = the request chain's leaf |
| Read | get_revision_visible_to(repo, id, viewer), change_status_visible_to(repo, change_id, viewer) | read_revision(req, now_ms): capability Read; payload = the 32-byte ChangesetId; every mutation path of the revision must be in the leaf's path scope, otherwise ChangesetNotFound |
| Server-side read | get_revision(repo, id), change_status(repo, change_id) (unfiltered) | through store() |
Move check order: to == Canonical (CanonicalOutsidePromotionQueue), then the mover signature and leaf binding (AuthSignatureInvalid: a payload signed by another key, over another chain, state, or time, or with a flipped bit), then visibility (ChangesetNotFound), then owner chain (AuthChainNotDominant), then the owner-move rule (RevisionStateMoveInvalid). A store still writing v1 JSON records refuses moves with EnvelopeVersionUnsupported, as for B1 records. Nothing is written on a refusal.
3. Canonical binary layout (principle P5)
All integers little-endian; str = u32 len | UTF-8; trailing bytes refused.
- State tag:
Private= 1,Draft= 2,Proposed= 3,Verified= 4,Canonical= 5; 0 and 6..=255 are not states. - Signed statement (issue #89):
"SYNAPSE_STATE_MOVE_SIGN_V1\0" | str repo | changeset_id [32] | to u8 | chain (u8 count | link digest [32]*) | signed_at_ms u64, signed with Ed25519 and checked withverify_strict(ed25519-dalek, as for every other signature here). - WAL kind 9
RevisionStateMoved(signed layout, issue #89):repo str | changeset_id [32] | to u8 | u32 len | chain | moved_at_ms u64 | signed_at_ms u64 | u32 len | leaf_link | mover_key [32] | signature [64];leaf_linkis the F1 link encoding. Binary only (no v1 JSON form); an unknown state tag isCodecRefusal::UnknownTag { field: "revision state" }, a malformed leaf link isEmbeddedMalformed, a cut record isTruncated, extra bytes areTrailingBytes. The unsigned layout of #88 (ending atmoved_at_ms) is not accepted: it decodes asTruncated. #88 was never merged, so no store holds it, and a second kind 9 layout would keep forgeable records readable. RevisionStateMovePayload: exactlychangeset_id [32] | to u8 | signed_at_ms u64 | signature [64](105 bytes); anything else (including the 33-byte #88 payload), or an unknown tag, isEnvelopeMalformed.- Checkpoint v2: the revision section of B1 now holds kind 7, 8, or 9 records.
- State root: kind 9 records join the revision part of the root (B1) with digest input
u64 repo len | repo | changeset_id | tag | chain | moved_at_ms | signed_at_ms | u64 leaf link len | leaf_link | mover_key | signature; stores without revision records keep every existing root.
4. Acceptance
| Acceptance (issue #28) | Test |
|---|---|
| Over random operation sequences no revision moves backwards | b4_property_random_operation_sequences_never_move_a_revision_backwards (store: 12 seeded cases of 40 random admissions, joins, moves by any chain to any state, reopens, and checkpoint-compact-reopens, checked against a model, under the EveryCommit and group-commit fsync policies); b4_property_forged_record_sequences_never_move_a_revision_backwards (ledger: 200 seeded cases of 60 raw records including forged moves, rebuilt from the log); b4_replay_refuses_a_forged_move_to_verified; b4_property_random_move_sequences_never_move_backwards (core: 2000 cases of 32 moves) |
A Private revision is invisible to every key outside its owner chain | b4_private_revision_is_invisible_to_every_key_outside_its_owner_chain (read, change status, parent, marker, and move, for the delegation tree and 200 random chains, after replay and a checkpoint); b4_api_private_revision_is_invisible_outside_its_owner_chain_with_real_f1_chains; joins: b4_joining_identical_content_grants_no_ownership_of_a_private_revision (also after replay and a checkpoint), b4_api_joining_identical_content_grants_no_ownership_of_a_private_revision |
Any move to Canonical outside the promotion queue is refused | b4_any_move_to_canonical_outside_the_promotion_queue_is_refused; b4_api_move_to_canonical_outside_the_promotion_queue_is_refused (including a grant with Publish and CanonicalViaPromotionQueue); b4_owner_moves_never_reach_verified_or_canonical |
| Acceptance (issue #89) | Test |
|---|---|
| A forged or resealed kind 9 record with a bad signature or a non-leaf signer changes nothing on replay | b89_forged_owner_move_resealed_in_the_wal_changes_nothing_on_replay (a broken-signature and a validly-signed-by-another-key owner move written through the store's own framing: torn-tail recovery keeps both as counted no-ops, strict recovery refuses the store with RevisionStateMoveUnverified, both before and after checkpoint and compaction); the ledger property test now mixes both forgeries into its 200 cases and checks the replay report; b89_api_state_moves_must_be_signed_by_the_request_agent (API: a payload signed by another key, over another chain, state, or time, or with a flipped bit, is AuthSignatureInvalid and writes nothing) |
| A legitimate signed move round-trips write, replay, checkpoint, and compaction, and the state root covers it | b89_signed_owner_move_round_trips_replay_checkpoint_and_compaction; b89_state_root_record_digest_covers_the_signature_fields |
| Review of PR #100: a torn-tail store can be compacted back into one strict recovery accepts | b89_torn_tail_compaction_drops_unverified_moves_so_strict_recovery_accepts_the_store (red on 30353d6: red-green/b89-red-compaction.txt) |
Review of PR #100: verify() alone accepts a forger-minted leaf link; the owner rule refuses it | b89_a_forger_minted_leaf_link_verifies_but_never_owns_a_revision (a self-minted root link and a self-appended link under the owner chain both verify; plan_move refuses both with AuthChainNotDominant and replay keeps them as no-ops; fails if ownership is widened to descendant chains) |
| Strict codec: malformed or truncated records refused typed | b4_state_move_records_round_trip_and_malformed_bytes_are_refused_typed (signed layout: every cut, trailing bytes, unknown tag, the #88 layout, a malformed leaf link); the API payload loop in b4_api_* (empty, 33-byte #88 payload, bad tag, 104 and 106 bytes); crates/synapse-auth/src/state_move.rs unit tests (a move signed by the leaf verifies for root and derived chains; a flipped bit, another signer, a link that is not the chain leaf, and a link of another repository are refused) |
Also: b4_state_move_records_round_trip_and_malformed_bytes_are_refused_typed, b4_state_tags_round_trip_and_unknown_tags_are_not_states, b4_every_allowed_move_raises_the_rank_and_every_other_pair_is_refused. The B1 prune tests now share the orchestrator's revision (Private -> Draft) before checking that other chains are refused for dominance, since a Private revision is now unknown to them.
Red before green: each acceptance test was run against the B4 commit with one mutation that removes the behaviour it proves (evidence/b4-revision-states/red-green/red.txt), and fails; green.txt holds the passing runs. The join tests (review of PR #88) were committed before their fix and fail on the earlier implementation (red-join.txt); so was the replay test b4_replay_refuses_a_forged_move_to_verified (red-replay.txt). Issue #89: b89_forged_owner_move_resealed_in_the_wal_changes_nothing_on_replay fails on the #88 head cf3879f, where a resealed unsigned owner move publishes a Private revision on replay (red-green/b89-red.txt); b89-green.txt holds the passing runs on this branch; the review fixes have b89-red-compaction.txt and b89-green-review.txt.
5. Limits and deviations
- Grant default. Section 5.3 says the grant sets the default between
PrivateandDraft. F1's link caveats have no such field, so every admission startsPrivateand the owner moves it toDraft. Adding the default needs a new F1 caveat field and link version; it is not part of B4. Verifiedis not reachable yet: it is recorded when claims (B3) and required-action receipts (E1) are discharged on the exact tree.Canonicalis reached only by the promotion queue (E2), which does not exist yet;CanonicalPromoted(the canonical reference head) is unchanged and does not consult revision states.- An identical-content admission from a chain that may not see the existing
Privaterevision joins it (B1 identity covers content only) and reportsjoined_existing_revision. That reveals only that the same content already exists under the change (which the joiner already holds); the joiner gains no ownership, cannot read the owner's witnesses or attestations, and cannot publish the revision. - The cost of that restrictive reading: a joiner outside the owner chain cannot read, name as a parent, or mark the revision it submitted itself until the owner shares it. The reverse exposure remains: when the owner publishes, every joiner's attestation (its authorship) becomes visible with the revision without that joiner's consent; this matters only for a joiner the owner could not already see (a change-scope joiner outside the owner's subtree). Low.
- Signed moves (issue #89), what they do not cover; closed by the revision seal (issue #101). Replay binds a move to the key the chain's leaf link names, but the chain digests themselves come from the unkeyed log, and no signature in a kind 7 or 8 record covers its
chainfield. An attacker who can rewrite and reseal the whole WAL could change a kind 7 admission's chain field to links of their own making and then sign moves under those links. Issue #101 chose option 2 of its three, a keyed log seal, see section 6: a store opened withSynapseStore::open_sealed(orSynapseService::open_sealed) and the server's machine key refuses such a rewrite. A store opened withopenkeeps the old behaviour and stays exposed to it. - Kinds 7 and 8 are not re-verified at replay. Their envelope and marker signatures were checked on admission; re-checking only the signature on replay would not bind the unsigned
chainfield next to it. In a sealed store the seal covers every field of the record (chain included), so re-verifying the signatures adds nothing against a rewrite without the server key; option 1 of issue #101 (carrying the full links and binding the chain under the agent's signature) is not done. - Mover time. The issue asks for
moved_at_msunder the signature. The agent cannot sign the server's clock, so the mover signs its ownsigned_at_ms(the precedent of obsolescence markers: signedsigned_at_ms, serverrecorded_at_ms); the record keeps both, and the state root covers both. signed_at_msis self-reported. It is never checked against the server clock or the leaf link'sissued_at_ms/expires_at_mswindow, on the API path or at replay, so it is not evidence of when the mover acted. Replay does not re-check revocation or expiry of the leaf link either: a stolen leaf key whose link was later revoked, together with write access to the WAL, yields a move that verifies. That needs both at once; low.verify()binds, it does not authorize.SignedStateMove::verifynever checks the leaf link's issuer or caveats; it is safe only together with the owner rule (the chain is or dominates the first admission's chain), which pins the leaf to an admitted link. Its doc comment states this precondition for any future caller; a descendant chain is not enough.- No from-state or nonce under the signature. The signed statement names the target state, not the state moved from, and carries no nonce or sequence number. That is harmless while owner moves only go forward (a replayed signature is an in-place or backward move, both refused). A later step that adds a downward transition (for example a rejection back to
Draft) makes old signed moves replayable by anyone who can append to the WAL, and must bind the from-state or a nonce. - Merge order. The kind 9 layout changed in place, without a version marker, because #88 never shipped. #88 must not be merged and deployed on its own and then written to: such a store would not open under this change (
Truncated). Merge #88 and this PR together. - Server-originated moves. No server path moves revision states on this base (
Verifiedis not reachable;Canonicalwaits for E2). Server-signedVerifiedandCanonicalmoves need the server machine key of B5 (PR #95) and are a follow-up there; kind 9 stays the owner-move record. - Change-existence oracle (B1, low): when every revision of a change is another owner's
Privaterevision, a chain that neither descends from the creating chain nor names the change getsAuthChangeOutOfScopewhere an unknown change would admit it as creator.ChangeIds are 16 random bytes, so this needs the ID. - Upgrade: revisions admitted before B4 replay as
Private(no kind 9 record), so after the upgrade they are invisible to non-owners until an owner moves them. This fails closed. - Store-level
get_revisionandchange_status, andSynapseService::store(), are unfiltered server-side reads; callers acting for an agent use the_visible_toreads orread_revision. Subscriptions, search, and predictions (section 5.7) do not exist yet and must filter the same way when they are built. - Revision reads are not snapshot-bounded yet (as in B1).
- No performance figures: B4 has no hot-path acceptance, and none were measured.
6. Revision seal (issue #101)
Code: crates/synapse-store/src/seal.rs (RevisionSeal, RevisionSealKey, the head and signature), crates/synapse-store/src/revision.rs (checking at replay, sealing on write, compaction), crates/synapse-store/src/codec.rs (the trailer), crates/synapse-store/src/lib.rs (open_sealed, strict refusal). Tests: the s101_ tests in crates/synapse-store/src/revision.rs.
- Choice. Of the options in issue #101 (1: re-verify kinds 7 and 8 and bind the chain into what is verified; 2: a keyed seal by the server machine key; 3: both), this is option 2. Option 1 alone does not close the attack: the agent's authorship signature does not cover the chain, and the store holds no repository trust root at replay, so a rewriter could still attach a self-minted chain whose leaf is the author's key. Closing it that way needs a new signed envelope layout (a protocol change for every agent) and trust roots in the store. The seal covers every field of kinds 7, 8, 9, and 11 with no client change.
- What is sealed. Every revision record a store opened with a
RevisionSealKeywrites. The seal is an Ed25519 signature (ed25519-dalek,verify_strict, small-order keys refused) by that key over a running head: head 0 is 32 zero bytes, head n =SHA-256("SYNAPSE_REVISION_SEAL_HEAD_V1\0" | head n-1 | seq u64 | record digest), signature n over"SYNAPSE_REVISION_SEAL_SIGN_V1\0" | head n. The record digest is the state-root record digest of section 3 (every field,chainincluded; the seal itself excluded). The store writes the record at exactly the sequence it sealed for (thechecked_atrule of B5). - Layout. An optional trailer after the record body:
1 u8 | server_key [32] | signature [64]. An unsealed record ends at its body, so every existing record still decodes. Bytes after a body that do not open with tag 1 areTrailingBytes; a cut trailer isTruncated. - Replay. With a seal key, a revision record is applied only when its seal names that key and verifies against the head so far; otherwise it stays in the log and the state root, changes nothing (an unsealed kind 7 does not even make its blobs visible), and does not move the head. So a changed, inserted, or reordered revision record, or one removed from the middle, breaks the chain from that record on.
StrictFailClosedrefuses the store withRevisionRecordUnsealed;TruncateTornTailopens it and reportsSealReplayReport { unsealed, first_unsealed, dropped_by_compaction }(SynapseStore::seal_replay_report).checkpoint_and_compactthen leaves out exactly the unsealed records (sealed ones always stay, so the chain stays whole), and strict recovery accepts the store again. Without a seal key, seals are carried through checkpoints but not checked. - State root. Unchanged: the seal is not part of the record digest, so a sealed store and an unsealed store that ran the same operations have the same root.
Limits:
- Opt in.
SynapseStore::openandSynapseService::opendo not seal or check. Whoever deploys a server must open withopen_sealedand the server machine key; the key never comes from the log. - Existing stores. A store with revision records written before it was sealed is refused by strict sealed recovery (its records cannot be told from forgeries). Torn-tail recovery plus a compaction drops them. A migration that adopts existing records under an operator's explicit decision is a follow-up.
- Rollback. Removing revision records from the end of the log (or replacing the whole store with an older copy) is not detected: the seal proves records were written by the key holder, not that none came after. That needs an external anchor (a monotonic counter or a published head), as for anti-rollback elsewhere.
- Key compromise and rotation. Whoever holds the server key can seal anything; this is the same trust as B5 derivations. One key per store: a rotated key makes strict recovery refuse every record sealed under the old key. Accepting retired keys, or checking the seal key against the identity document (
server_keyat the sequence, as B5 does), is a follow-up. - Replica verification.
verify_revision_log(replicas, B5) does not check seals. - Cost. One Ed25519 signature per revision write and one or two verifications per revision record on replay (two for kinds 7 and 11). Not measured.