@kumiai/rpc 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/classify.d.ts CHANGED
@@ -1,28 +1,38 @@
1
1
  import type { CommitHeader } from './crypto.js';
2
2
  /**
3
- * What the lane does with one frame from the commit log. Six rows, in EVALUATION order — the
3
+ * What the lane does with one frame from the commit log. Eight rows, in EVALUATION order — the
4
4
  * order is load-bearing, and this table is the `if` chain of {@link classifyCommit} plus the
5
5
  * port's answer to its last row:
6
6
  *
7
7
  * | Frame | Cursor |
8
8
  * |---|---|
9
- * | Not a commit at all (unreadable header) | advance (poison — never retry, never heal); settled first, before any epoch question |
9
+ * | A wire version this build cannot read at all | advance; heal — the group moved on to a format this build does not have |
10
+ * | Not a commit at all (no header) | advance (poison — never retry, never heal); settled first, before any epoch question |
10
11
  * | Framed at an epoch AHEAD of this peer's | advance; heal — the group moved on without it |
11
12
  * | Below this peer's epoch, with no recorded applied-commit | advance, no fork check, no unwrap attempt |
12
13
  * | Below this peer's epoch, with a record naming a different sequenceID | advance; the fork trigger |
14
+ * | At this peer's current epoch, with no authenticated committer | advance (poison — never retry, never heal) |
13
15
  * | At this peer's current epoch, committed by THIS peer | do not advance; heal |
14
16
  * | At this peer's current epoch, committed by another — handed to the port | applied: advance and record this epoch -> sequenceID for the fork check; refused by policy or entries unresolvable: advance (poison — never retry, never heal) |
15
17
  *
16
- * Five rows are decided here, reading bytes and decrypting nothing; only the last — a frame at
18
+ * Seven rows are decided here, reading bytes and decrypting nothing; only the last — a frame at
17
19
  * this peer's epoch authored by someone else — is handed to the MLS port, whose answer is the
18
- * sixth row. Malformed-poison settles at the top because headerless bytes cannot be asked the
19
- * epoch questions the other rows turn on.
20
+ * eighth row. Order: the unreadable-version row settles first, since such a frame can't even be
21
+ * asked if it's a commit; headerless-poison settles next, since non-commit bytes can't be asked
22
+ * the epoch questions the rest turn on.
23
+ *
24
+ * **The header carries two facts with different trust.** The epoch is cleartext and only the
25
+ * publisher's word. The committer is MLS-authenticated but recoverable only for a commit framed
26
+ * at this peer's OWN epoch — reading it needs that epoch's key material. So `ahead`, `history`
27
+ * and `fork` dispatch on the EPOCH ALONE (they're about frames this peer holds no key for),
28
+ * while every row at this peer's own epoch requires the AUTHENTICATED committer and refuses to
29
+ * fire without it. A classifier that demanded a committer before reading an epoch could never
30
+ * classify the one frame that says a peer fell behind.
20
31
  *
21
32
  * **Classify by epoch first, unwrap only what you can apply.** A frame's blob is sealed under
22
- * the epoch the commit is framed at, so a peer walking history (late joiner, rejoiner,
23
- * re-seeded) reaches frames it can never open — including the commit that added it. Unwrapping
24
- * is a consequence of "I can apply this frame", never a precondition of reading it; unwrapping
25
- * before classifying files ordinary history as a decryption failure.
33
+ * the epoch it's framed at, so a peer walking history (late joiner, rejoiner, re-seeded) reaches
34
+ * frames it can never open — including the commit that added it. Unwrapping before classifying
35
+ * would misfile ordinary history as a decryption failure.
26
36
  */
27
37
  export type CommitDisposition =
28
38
  /**
@@ -34,12 +44,23 @@ export type CommitDisposition =
34
44
  }
35
45
  /**
36
46
  * A frame framed at an epoch AHEAD of this peer's: the group advanced where this peer did
37
- * not, proof it has fallen out of the group's line. Advance, and heal.
47
+ * not, proof it has fallen out of the group's line. Advance, and heal. The only row that
48
+ * heals, since a fault only THIS peer has surfaces exactly here, later, once the group
49
+ * commits past it — a frame nobody can resolve instead blocks the whole group directly.
50
+ *
51
+ * Decided on the epoch alone (see the type doc), which here is UNAUTHENTICATED by necessity:
52
+ * a peer that fell behind holds no sender-data secret for the epoch it fell behind from, so
53
+ * requiring a committer would make this row unreachable — and an unreachable `ahead` means the
54
+ * peer files the group's entire future as poison and reports itself reconciled while
55
+ * permanently, silently stuck. Worse than the forgery this accepts.
38
56
  *
39
- * This is the only signal that a peer's own state is broken, so no other row needs to heal
40
- * a dropped frame: a frame nobody can resolve blocks the whole group (nobody applies it),
41
- * whereas a frame only THIS peer cannot resolve is a fault of its own — observable only
42
- * later, when the group commits past it and lands here.
57
+ * **Accepted despite being forgeable.** Anything that can publish on the commit topic (a
58
+ * removed member who keeps it forever, or the untrusted hub) can claim a high epoch and force
59
+ * every honest peer to heal at once — the same costly recovery storm `own-unmerged` refuses to
60
+ * fund. Accepted because: no authentication can close it (a peer past the gap has no key to
61
+ * check the far side with); a liar can only TRIGGER a heal, never suppress one (honest
62
+ * ahead-frames are classified independently); and it is bounded to one heal per frame, since
63
+ * the frame is stepped over before the heal fires and is never re-read.
43
64
  */
44
65
  | {
45
66
  row: 'ahead';
@@ -48,15 +69,21 @@ export type CommitDisposition =
48
69
  * A frame from an epoch BELOW this peer's with no applied-commit record: pre-join,
49
70
  * pre-rejoin, or re-seeded history. Advance, no fork check, no unwrap. Neither fork nor
50
71
  * poison — every healthy peer reads some.
72
+ *
73
+ * On the epoch alone, same as `ahead` and with far less at stake: no committer is recoverable
74
+ * for an epoch this peer has ratcheted past either, and the frame is stepped over untouched —
75
+ * a lie about the epoch only changes how a liar's frame gets ignored.
51
76
  */
52
77
  | {
53
78
  row: 'history';
54
79
  }
55
80
  /**
56
81
  * Two different commits at one epoch: this peer applied `appliedSequenceID`, the log now
57
- * carries another. The hub accepted both — only possible by serving different logs to
58
- * different members. Advance, and heal if on the losing branch: the branch whose commit
59
- * carries the HIGHER sequenceID, a tiebreak both sides evaluate alone once they see both.
82
+ * carries another. Reached on the epoch alone and settled on sequenceIDs, which are the
83
+ * hub's own chaining, not the commit's word — the committer is neither read nor available.
84
+ * The hub accepted both — only possible by serving different logs to different members.
85
+ * Advance, and heal if on the losing branch: the branch whose commit carries the HIGHER
86
+ * sequenceID, a tiebreak both sides evaluate alone once they see both.
60
87
  */
61
88
  | {
62
89
  row: 'fork';
@@ -74,36 +101,57 @@ export type CommitDisposition =
74
101
  * removed one, who keeps the commit topic forever) publishes one well-formed policy-refused
75
102
  * commit and every honest peer heals at once. Authorship stops the overlap.
76
103
  *
77
- * Read the committer from the commit itself, where MLS authenticates it — NEVER from the
78
- * frame's transport sender, the untrusted hub's word. A hub that stamped each recipient's
79
- * own DID onto one poison frame would otherwise heal the whole group at will.
104
+ * The committer read is the MLS-authenticated one from the commit itself — NEVER the frame's
105
+ * transport sender (the untrusted hub's word), and never a fallback when authentication comes
106
+ * back empty (that header is `poison`, per the type doc's trust split). Softening either would
107
+ * let a forged frame heal this peer, or the whole group, on demand.
80
108
  */
81
109
  | {
82
110
  row: 'own-unmerged';
83
111
  }
84
112
  /**
85
- * Not a commit at all. Advance — poison is stepped over, never retried. It is the LAST
86
- * resort, never the fallback for "I could not apply this": a crash victim's own commit
87
- * misfiled as malformed would walk the peer to the log's end stuck at its epoch forever.
113
+ * A frame there is nothing further to do with. Advance — poison is stepped over, never
114
+ * retried. It is the LAST resort, never the fallback for "I could not apply this": a crash
115
+ * victim's own commit misfiled as malformed would walk the peer to the log's end stuck at its
116
+ * epoch forever.
88
117
  *
89
- * Two more cases are filed here on the port's answer, not this classification: a
90
- * policy-refused commit, and a commit whose ledger entries will not resolve. Both advance,
91
- * NEITHER heals — anything else hands any member a group-wide recovery storm per publish.
118
+ * THREE classifications land here — one row, because the cursor treatment is identical:
119
+ * - **Not a commit at all**: no header. Undecodable bytes, or a message of another kind.
120
+ * - **A commit at this peer's own epoch whose committer will not authenticate**: nothing
121
+ * honest looks like this. Must NOT fall through to `apply` (the port can't process it, and a
122
+ * throw there wedges the lane forever) or to `own-unmerged` (see that row).
123
+ * - **Policy-refused, or ledger entries that will not resolve** — filed here on the MLS port's
124
+ * answer, not this classification.
125
+ * None of the three heals — anything else hands any member a group-wide recovery storm per
126
+ * publish (same cost as forging `ahead`, but closable here, so there's no reason to pay it).
92
127
  *
93
- * Dropping an unresolvable-entries commit leaves this peer at an epoch it did not pass, and
94
- * it may still commit from there onto a branch the group ignores. Known, accepted, bounded,
95
- * and not attacker-reachable: bodies are sealed under the commit's epoch, which every member
96
- * at that epoch holds, so a frame resolves for all or none — a peer that alone cannot resolve
97
- * one was already on another branch. And the group's next commit is `ahead`, which heals it.
98
- *
99
- * Refusing to commit from such an epoch is the obvious guard and WORSE THAN THE BUG: when
100
- * nobody can resolve the frame, every honest member skipped that epoch and would refuse,
101
- * while the publisher sits an epoch ahead alone — no one can publish the commit that unsticks
102
- * the group, and one unresolvable frame kills it permanently.
128
+ * Dropping an unresolvable-entries commit leaves this peer at an epoch it did not pass, able
129
+ * to commit onto a branch the group ignores. Accepted: bodies are sealed under the commit's
130
+ * epoch, which every member at that epoch holds, so a frame resolves for all or none — a peer
131
+ * that alone can't resolve one was already on another branch, and the group's next commit is
132
+ * `ahead`, which heals it. Refusing to commit from such an epoch looks safer and is WORSE: every
133
+ * honest member skipped that epoch too and would also refuse, so nobody can publish the commit
134
+ * that unsticks the group — one unresolvable frame would kill it permanently instead.
103
135
  */
104
136
  | {
105
137
  row: 'poison';
106
138
  };
139
+ /**
140
+ * A frame on the commit topic whose handshake version this build does not know: unreadable, in
141
+ * the one place where being unreadable is itself evidence.
142
+ *
143
+ * Passed to {@link classifyCommit} in the header's place, because there IS no header — the bytes
144
+ * behind the version byte are a format this build has never seen, and reading them would be
145
+ * guessing. It is not a `null` header: `null` means "readable bytes that are not a commit", which
146
+ * is poison. This means "the group is speaking a language I do not have", which is `ahead`.
147
+ */
148
+ export declare const UNKNOWN_FRAME_VERSION = "unknown-frame-version";
149
+ /**
150
+ * What the lane could get out of one frame before classifying it: the commit's own header,
151
+ * `null` for bytes that are not a commit, or {@link UNKNOWN_FRAME_VERSION} for a frame this
152
+ * build cannot read at all.
153
+ */
154
+ export type CommitFrameEvidence = CommitHeader | null | typeof UNKNOWN_FRAME_VERSION;
107
155
  /** What the classifier reads about this peer. Nothing here needs the network or a key. */
108
156
  export type CommitClassifierState = {
109
157
  /** This peer's own identity — the DID that authenticates a commit this peer authored. */
@@ -114,11 +162,10 @@ export type CommitClassifierState = {
114
162
  * The sequenceID of the commit this peer enacted at each epoch it has passed.
115
163
  *
116
164
  * In memory, DELIBERATELY. A restarted peer holds none and reads history as history: it can
117
- * MISS a fork but can never invent one — the safe direction, since inventing forks would
118
- * turn every late joiner/rejoiner/re-seeded peer into a recovery storm on first pull, while a
119
- * missed fork is re-detected by the trim and `ahead` triggers on the next published frame.
120
- * Durability needs a second host-provided store with nothing yet to spend it on: fork
121
- * RESOLUTION (losing branch rejoining and re-enacting) is not built. Revisit when it is.
165
+ * MISS a fork but can never invent one — the safe direction, since a missed fork is
166
+ * re-detected by the trim (`ahead` triggers on the next published frame), while inventing
167
+ * forks would turn every late joiner/rejoiner/re-seeded peer into a recovery storm on first
168
+ * pull. Durability needs a store; not built, since fork RESOLUTION isn't built either.
122
169
  */
123
170
  appliedByEpoch: ReadonlyMap<number, string>;
124
171
  };
@@ -126,7 +173,10 @@ export type CommitClassifierState = {
126
173
  * Classify one frame of the commit log against this peer's state, before anything is
127
174
  * applied and before anything is decrypted.
128
175
  *
129
- * `header` is the commit's own epoch and committer, read out of the commit bytes by the MLS
130
- * port — `null` for bytes that are not a commit at all.
176
+ * `header` is what the commit says about itself, read out of its own bytes by the MLS port:
177
+ * `null` for bytes that are not a commit at all, and otherwise the commit's epoch — always, it
178
+ * is cleartext — with `committerDID` present only where the port could MLS-authenticate one. It
179
+ * is {@link UNKNOWN_FRAME_VERSION} where the frame's wire version put the header itself out of
180
+ * reach.
131
181
  */
132
- export declare function classifyCommit(header: CommitHeader | null, sequenceID: string, state: CommitClassifierState): CommitDisposition;
182
+ export declare function classifyCommit(header: CommitFrameEvidence, sequenceID: string, state: CommitClassifierState): CommitDisposition;
package/lib/classify.js CHANGED
@@ -1,33 +1,45 @@
1
+ /**
2
+ * A frame on the commit topic whose handshake version this build does not know: unreadable, in
3
+ * the one place where being unreadable is itself evidence.
4
+ *
5
+ * Passed to {@link classifyCommit} in the header's place, because there IS no header — the bytes
6
+ * behind the version byte are a format this build has never seen, and reading them would be
7
+ * guessing. It is not a `null` header: `null` means "readable bytes that are not a commit", which
8
+ * is poison. This means "the group is speaking a language I do not have", which is `ahead`.
9
+ */ export const UNKNOWN_FRAME_VERSION = 'unknown-frame-version';
1
10
  /**
2
11
  * Classify one frame of the commit log against this peer's state, before anything is
3
12
  * applied and before anything is decrypted.
4
13
  *
5
- * `header` is the commit's own epoch and committer, read out of the commit bytes by the MLS
6
- * port — `null` for bytes that are not a commit at all.
14
+ * `header` is what the commit says about itself, read out of its own bytes by the MLS port:
15
+ * `null` for bytes that are not a commit at all, and otherwise the commit's epoch — always, it
16
+ * is cleartext — with `committerDID` present only where the port could MLS-authenticate one. It
17
+ * is {@link UNKNOWN_FRAME_VERSION} where the frame's wire version put the header itself out of
18
+ * reach.
7
19
  */ export function classifyCommit(header, sequenceID, state) {
8
- // Headerless bytes cannot be asked the questions below, so settle first. Overlaps no other
9
- // row: an unreadable frame is not somebody's commit, least of all this peer's own.
20
+ // Settled above every other row, including the headerless one: bytes in an unknown format
21
+ // can't be asked whether they're a commit. `ahead`, not `poison` — see UNKNOWN_FRAME_VERSION's
22
+ // doc for why poison is uniquely dangerous here (no "next frame" to heal off of after a
23
+ // version bump) and the `ahead` row's doc for why the resulting forgeability is accepted.
24
+ if (header === UNKNOWN_FRAME_VERSION) return {
25
+ row: 'ahead'
26
+ };
27
+ // Bytes that are not a commit can't be asked the epoch questions below, so settle next.
10
28
  if (header == null) return {
11
29
  row: 'poison'
12
30
  };
13
- // AHEAD, settled before `history`: the two are otherwise indistinguishable, since a peer
14
- // holds no applied-commit record for an epoch it never reached any more than for one it was
15
- // never part of — but the first is a peer fallen out of the group, the second is history.
16
- //
17
- // Cannot fire for a healthy peer: commits advance epochs and each compare-and-sets at the
18
- // head, so the log runs in non-decreasing epoch order and a peer walking it advances in
19
- // lockstep — the next frame is never ahead. A Welcome joiner at epoch N reads all frames
20
- // below N as history, applies N, and rises with the log. So a frame ahead of this peer means
21
- // exactly one thing: the group advanced at an epoch this peer did not (trimmed frames, or a
22
- // frame it alone could not apply). Advance, and heal.
31
+ // Settled before `history`, which is otherwise indistinguishable (no applied-commit record
32
+ // either way) — but this is a peer fallen out of the group, cannot happen to a healthy one
33
+ // (the log runs in non-decreasing epoch order, so a peer walking it is never ahead of it), and
34
+ // is decided on the epoch alone. See the row's doc for what that costs and why it's accepted.
23
35
  if (header.epoch > state.epoch) return {
24
36
  row: 'ahead'
25
37
  };
38
+ // Below this peer's epoch: also on the epoch alone, for the same reason.
26
39
  if (header.epoch < state.epoch) {
27
40
  const applied = state.appliedByEpoch.get(header.epoch);
28
- // No record for that epoch -> history, not fork. "A commit at an epoch the peer already
29
- // passed" is NOT the fork test: a late joiner, rejoiner, or re-seeded peer walks epochs it
30
- // never passed, and would falsely diagnose a fork on first pull.
41
+ // No record for that epoch -> history, not fork: a late joiner/rejoiner/re-seeded peer
42
+ // walks epochs it never passed, and would falsely diagnose a fork on first pull otherwise.
31
43
  if (applied == null || applied === sequenceID) return {
32
44
  row: 'history'
33
45
  };
@@ -37,8 +49,12 @@
37
49
  branch: sequenceID < applied ? 'losing' : 'winning'
38
50
  };
39
51
  }
40
- // At this peer's epoch. Discriminate by authorship, NOT applicability: a peer can never
41
- // apply its own commit, and a frame it merely fails to apply is not a reason to heal.
52
+ // At this peer's epoch — the only place the committer is both required and available (see
53
+ // the type doc's trust split). No committer here is `poison`; must not fall through.
54
+ if (header.committerDID == null) return {
55
+ row: 'poison'
56
+ };
57
+ // Discriminate by authorship, NOT applicability — see `own-unmerged`'s doc.
42
58
  if (header.committerDID === state.localDID) return {
43
59
  row: 'own-unmerged'
44
60
  };
@@ -2,7 +2,7 @@
2
2
  * The commit lane's frame: an MLS Commit plus the ledger-entry bodies that Commit enacts,
3
3
  * sealed under the epoch secret the Commit is framed at.
4
4
  *
5
- * [ commitLength(4, LE) | commit bytes | sealed entry blob... ]
5
+ * [ VERSION(1) | commitLength(4, LE) | commit bytes | sealed entry blob... ]
6
6
  *
7
7
  * Length-delimited halves, so the commit half is read WITHOUT touching the blob. Body
8
8
  * delivery is atomic with the commit: a peer is never told of a body before the commit that
@@ -15,6 +15,24 @@
15
15
  * The blob is opened by {@link "ledger-entries".createLedgerEntryResolver}, only for a commit
16
16
  * the peer is applying.
17
17
  */
18
+ /**
19
+ * Current commit-frame format version.
20
+ *
21
+ * Exists because WITHOUT it, a later frame with a third section would decode here SUCCESSFULLY,
22
+ * silently swallowed into `sealedEntries` — a reader that accepts corrupt input can't be taught
23
+ * later to reject it, so the rule has to ship in the first build.
24
+ *
25
+ * BUMPING THIS STRANDS EVERY OLD PEER; the lane routes an unknown version to
26
+ * {@link "classify".classifyCommit} as {@link "classify".UNKNOWN_FRAME_VERSION} (same as an
27
+ * unknown handshake version) so the strand is a loud heal, not a silent stall —
28
+ * {@link isUnsupportedCommitFrameVersion} is what lets the lane tell that failure apart from
29
+ * bytes that are simply not a frame. This byte is read BEFORE the commit bytes are extracted, so
30
+ * unlike the sealed ledger-entry blob (whose failure lands after the commit is read, poisoning
31
+ * one commit and healing off the next), there is no next frame to fall back to — bump this only
32
+ * when the frame's SHAPE itself changes (a third section, a wider length field), and only with a
33
+ * responder that still answers old peers' rendezvous requests in a version they can read.
34
+ */
35
+ export declare const COMMIT_FRAME_VERSION = 1;
18
36
  /** A commit frame's two halves, as bytes. The blob is opaque here — sealed, unread. */
19
37
  export type CommitFrame = {
20
38
  /** The framed MLS Commit. */
@@ -25,8 +43,26 @@ export type CommitFrame = {
25
43
  /** Frame a Commit with the sealed blob of the entry bodies it enacts. */
26
44
  export declare function encodeCommitFrame(commit: Uint8Array, sealedEntries: Uint8Array): Uint8Array;
27
45
  /**
28
- * Split a commit frame into its two halves. Throws only on bytes that are not a frame at
29
- * all — too short, or a length that runs past the end. An unopenable blob is not a
30
- * decoding failure: this never looks at the blob.
46
+ * Thrown by {@link decodeCommitFrame} for an unknown version byte, and ONLY for that: the lane
47
+ * must treat it differently from "not a frame" (dropped) — a frame from the future goes to the
48
+ * classifier to raise a heal, so it needs a type to branch on, not a message string.
49
+ */
50
+ export declare class UnsupportedCommitFrameVersionError extends Error {
51
+ #private;
52
+ constructor(version: number);
53
+ /** The version byte read off the frame — the one this build does not know. */
54
+ get version(): number;
55
+ }
56
+ /**
57
+ * Whether a {@link decodeCommitFrame} failure was an unknown VERSION, not just non-frame bytes.
58
+ * Checked by class, not name, since the error is thrown in-package — mirrors
59
+ * {@link "crypto".isMissingLedgerEntries}.
60
+ */
61
+ export declare function isUnsupportedCommitFrameVersion(error: unknown): boolean;
62
+ /**
63
+ * Split a commit frame into its two halves. Throws on bytes that are not a frame at all (too
64
+ * short, or a length past the end) and on an unknown version — as
65
+ * {@link UnsupportedCommitFrameVersionError}, so the caller can route it to the heal instead of
66
+ * dropping it. Never looks at the blob, so an unopenable one is not a decoding failure.
31
67
  */
32
68
  export declare function decodeCommitFrame(frame: Uint8Array): CommitFrame;
@@ -2,7 +2,7 @@
2
2
  * The commit lane's frame: an MLS Commit plus the ledger-entry bodies that Commit enacts,
3
3
  * sealed under the epoch secret the Commit is framed at.
4
4
  *
5
- * [ commitLength(4, LE) | commit bytes | sealed entry blob... ]
5
+ * [ VERSION(1) | commitLength(4, LE) | commit bytes | sealed entry blob... ]
6
6
  *
7
7
  * Length-delimited halves, so the commit half is read WITHOUT touching the blob. Body
8
8
  * delivery is atomic with the commit: a peer is never told of a body before the commit that
@@ -14,30 +14,77 @@
14
14
  * it); an unopenable blob is history, not poison — read the commit half, step over the frame.
15
15
  * The blob is opened by {@link "ledger-entries".createLedgerEntryResolver}, only for a commit
16
16
  * the peer is applying.
17
- */ const COMMIT_LENGTH_BYTES = 4;
17
+ */ /**
18
+ * Current commit-frame format version.
19
+ *
20
+ * Exists because WITHOUT it, a later frame with a third section would decode here SUCCESSFULLY,
21
+ * silently swallowed into `sealedEntries` — a reader that accepts corrupt input can't be taught
22
+ * later to reject it, so the rule has to ship in the first build.
23
+ *
24
+ * BUMPING THIS STRANDS EVERY OLD PEER; the lane routes an unknown version to
25
+ * {@link "classify".classifyCommit} as {@link "classify".UNKNOWN_FRAME_VERSION} (same as an
26
+ * unknown handshake version) so the strand is a loud heal, not a silent stall —
27
+ * {@link isUnsupportedCommitFrameVersion} is what lets the lane tell that failure apart from
28
+ * bytes that are simply not a frame. This byte is read BEFORE the commit bytes are extracted, so
29
+ * unlike the sealed ledger-entry blob (whose failure lands after the commit is read, poisoning
30
+ * one commit and healing off the next), there is no next frame to fall back to — bump this only
31
+ * when the frame's SHAPE itself changes (a third section, a wider length field), and only with a
32
+ * responder that still answers old peers' rendezvous requests in a version they can read.
33
+ */ export const COMMIT_FRAME_VERSION = 1;
34
+ const VERSION_BYTES = 1;
35
+ const COMMIT_LENGTH_BYTES = 4;
36
+ const HEADER_BYTES = VERSION_BYTES + COMMIT_LENGTH_BYTES;
18
37
  /** Frame a Commit with the sealed blob of the entry bodies it enacts. */ export function encodeCommitFrame(commit, sealedEntries) {
19
- const frame = new Uint8Array(COMMIT_LENGTH_BYTES + commit.length + sealedEntries.length);
20
- new DataView(frame.buffer).setUint32(0, commit.length, true);
21
- frame.set(commit, COMMIT_LENGTH_BYTES);
22
- frame.set(sealedEntries, COMMIT_LENGTH_BYTES + commit.length);
38
+ const frame = new Uint8Array(HEADER_BYTES + commit.length + sealedEntries.length);
39
+ frame[0] = COMMIT_FRAME_VERSION;
40
+ new DataView(frame.buffer).setUint32(VERSION_BYTES, commit.length, true);
41
+ frame.set(commit, HEADER_BYTES);
42
+ frame.set(sealedEntries, HEADER_BYTES + commit.length);
23
43
  return frame;
24
44
  }
25
45
  /**
26
- * Split a commit frame into its two halves. Throws only on bytes that are not a frame at
27
- * all — too short, or a length that runs past the end. An unopenable blob is not a
28
- * decoding failure: this never looks at the blob.
46
+ * Thrown by {@link decodeCommitFrame} for an unknown version byte, and ONLY for that: the lane
47
+ * must treat it differently from "not a frame" (dropped) — a frame from the future goes to the
48
+ * classifier to raise a heal, so it needs a type to branch on, not a message string.
49
+ */ export class UnsupportedCommitFrameVersionError extends Error {
50
+ #version;
51
+ constructor(version){
52
+ super(`unsupported commit frame version: ${version}`);
53
+ this.name = 'UnsupportedCommitFrameVersionError';
54
+ this.#version = version;
55
+ }
56
+ /** The version byte read off the frame — the one this build does not know. */ get version() {
57
+ return this.#version;
58
+ }
59
+ }
60
+ /**
61
+ * Whether a {@link decodeCommitFrame} failure was an unknown VERSION, not just non-frame bytes.
62
+ * Checked by class, not name, since the error is thrown in-package — mirrors
63
+ * {@link "crypto".isMissingLedgerEntries}.
64
+ */ export function isUnsupportedCommitFrameVersion(error) {
65
+ return error instanceof UnsupportedCommitFrameVersionError;
66
+ }
67
+ /**
68
+ * Split a commit frame into its two halves. Throws on bytes that are not a frame at all (too
69
+ * short, or a length past the end) and on an unknown version — as
70
+ * {@link UnsupportedCommitFrameVersionError}, so the caller can route it to the heal instead of
71
+ * dropping it. Never looks at the blob, so an unopenable one is not a decoding failure.
29
72
  */ export function decodeCommitFrame(frame) {
30
- if (frame.length < COMMIT_LENGTH_BYTES) {
73
+ if (frame.length < HEADER_BYTES) {
31
74
  throw new Error('commit frame is too short');
32
75
  }
76
+ const version = frame[0];
77
+ if (version !== COMMIT_FRAME_VERSION) {
78
+ throw new UnsupportedCommitFrameVersionError(version);
79
+ }
33
80
  const view = new DataView(frame.buffer, frame.byteOffset, frame.byteLength);
34
- const commitLength = view.getUint32(0, true);
35
- const blobStart = COMMIT_LENGTH_BYTES + commitLength;
81
+ const commitLength = view.getUint32(VERSION_BYTES, true);
82
+ const blobStart = HEADER_BYTES + commitLength;
36
83
  if (frame.length < blobStart) {
37
84
  throw new Error('commit frame is truncated: the commit runs past the end of the frame');
38
85
  }
39
86
  return {
40
- commit: frame.subarray(COMMIT_LENGTH_BYTES, blobStart),
87
+ commit: frame.subarray(HEADER_BYTES, blobStart),
41
88
  sealedEntries: frame.subarray(blobStart)
42
89
  };
43
90
  }