@941design/marmot-engine 0.0.0-stage → 0.2.0-alpha.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.
Files changed (196) hide show
  1. package/CHANGELOG.md +434 -0
  2. package/LICENSE +49 -0
  3. package/README.md +33 -2
  4. package/dist/audit/emitter.d.ts +17 -0
  5. package/dist/audit/emitter.js +30 -0
  6. package/dist/audit/helpers.d.ts +16 -0
  7. package/dist/audit/helpers.js +103 -0
  8. package/dist/audit/index.d.ts +6 -0
  9. package/dist/audit/index.js +6 -0
  10. package/dist/audit/recorder.d.ts +13 -0
  11. package/dist/audit/recorder.js +24 -0
  12. package/dist/audit/sink.d.ts +16 -0
  13. package/dist/audit/sink.js +27 -0
  14. package/dist/audit/types-internal.d.ts +7 -0
  15. package/dist/audit/types-internal.js +1 -0
  16. package/dist/audit/types.d.ts +289 -0
  17. package/dist/audit/types.js +2 -0
  18. package/dist/core/account-identity-proof.d.ts +165 -0
  19. package/dist/core/account-identity-proof.js +277 -0
  20. package/dist/core/application-rumor.d.ts +45 -0
  21. package/dist/core/application-rumor.js +120 -0
  22. package/dist/core/auth-service.d.ts +2 -0
  23. package/dist/core/auth-service.js +52 -0
  24. package/dist/core/binary.d.ts +142 -0
  25. package/dist/core/binary.js +363 -0
  26. package/dist/core/capabilities.d.ts +51 -0
  27. package/dist/core/capabilities.js +102 -0
  28. package/dist/core/client-state.d.ts +119 -0
  29. package/dist/core/client-state.js +226 -0
  30. package/dist/core/commit-authorization.d.ts +26 -0
  31. package/dist/core/commit-authorization.js +26 -0
  32. package/dist/core/components/admin-policy.d.ts +4 -0
  33. package/dist/core/components/admin-policy.js +69 -0
  34. package/dist/core/components/agent-text-stream.d.ts +51 -0
  35. package/dist/core/components/agent-text-stream.js +112 -0
  36. package/dist/core/components/app-components-list.d.ts +17 -0
  37. package/dist/core/components/app-components-list.js +35 -0
  38. package/dist/core/components/avatar-url.d.ts +12 -0
  39. package/dist/core/components/avatar-url.js +85 -0
  40. package/dist/core/components/bytes.d.ts +5 -0
  41. package/dist/core/components/bytes.js +18 -0
  42. package/dist/core/components/dictionary.d.ts +80 -0
  43. package/dist/core/components/dictionary.js +175 -0
  44. package/dist/core/components/disband-validation.d.ts +21 -0
  45. package/dist/core/components/disband-validation.js +173 -0
  46. package/dist/core/components/encrypted-media.d.ts +44 -0
  47. package/dist/core/components/encrypted-media.js +210 -0
  48. package/dist/core/components/group-lifecycle.d.ts +10 -0
  49. package/dist/core/components/group-lifecycle.js +32 -0
  50. package/dist/core/components/group-profile.d.ts +24 -0
  51. package/dist/core/components/group-profile.js +35 -0
  52. package/dist/core/components/host-safety.d.ts +9 -0
  53. package/dist/core/components/host-safety.js +148 -0
  54. package/dist/core/components/ids.d.ts +74 -0
  55. package/dist/core/components/ids.js +73 -0
  56. package/dist/core/components/index.d.ts +14 -0
  57. package/dist/core/components/index.js +14 -0
  58. package/dist/core/components/integrity.d.ts +141 -0
  59. package/dist/core/components/integrity.js +293 -0
  60. package/dist/core/components/message-retention.d.ts +17 -0
  61. package/dist/core/components/message-retention.js +26 -0
  62. package/dist/core/components/nostr-routing.d.ts +8 -0
  63. package/dist/core/components/nostr-routing.js +92 -0
  64. package/dist/core/components/url.d.ts +26 -0
  65. package/dist/core/components/url.js +44 -0
  66. package/dist/core/convergence-status.d.ts +84 -0
  67. package/dist/core/convergence-status.js +82 -0
  68. package/dist/core/convergence.d.ts +567 -0
  69. package/dist/core/convergence.js +1150 -0
  70. package/dist/core/credential.d.ts +16 -0
  71. package/dist/core/credential.js +64 -0
  72. package/dist/core/default-capabilities.d.ts +9 -0
  73. package/dist/core/default-capabilities.js +34 -0
  74. package/dist/core/extensions.d.ts +9 -0
  75. package/dist/core/extensions.js +5 -0
  76. package/dist/core/grease.d.ts +2 -0
  77. package/dist/core/grease.js +6 -0
  78. package/dist/core/group-event.d.ts +18 -0
  79. package/dist/core/group-event.js +30 -0
  80. package/dist/core/group-lifecycle.d.ts +42 -0
  81. package/dist/core/group-lifecycle.js +66 -0
  82. package/dist/core/group-members.d.ts +37 -0
  83. package/dist/core/group-members.js +99 -0
  84. package/dist/core/group-message-classify.d.ts +42 -0
  85. package/dist/core/group-message-classify.js +57 -0
  86. package/dist/core/group-message-crypto.d.ts +53 -0
  87. package/dist/core/group-message-crypto.js +140 -0
  88. package/dist/core/group-message.d.ts +5 -0
  89. package/dist/core/group-message.js +11 -0
  90. package/dist/core/group.d.ts +45 -0
  91. package/dist/core/group.js +78 -0
  92. package/dist/core/inbound.d.ts +102 -0
  93. package/dist/core/inbound.js +79 -0
  94. package/dist/core/index.d.ts +28 -0
  95. package/dist/core/index.js +26 -0
  96. package/dist/core/key-package-eligibility.d.ts +31 -0
  97. package/dist/core/key-package-eligibility.js +116 -0
  98. package/dist/core/key-package-event-decode.d.ts +43 -0
  99. package/dist/core/key-package-event-decode.js +118 -0
  100. package/dist/core/key-package-event-delete.d.ts +16 -0
  101. package/dist/core/key-package-event-delete.js +46 -0
  102. package/dist/core/key-package-event-encode.d.ts +28 -0
  103. package/dist/core/key-package-event-encode.js +102 -0
  104. package/dist/core/key-package-event.d.ts +4 -0
  105. package/dist/core/key-package-event.js +8 -0
  106. package/dist/core/key-package.d.ts +55 -0
  107. package/dist/core/key-package.js +115 -0
  108. package/dist/core/leaf-succession.d.ts +83 -0
  109. package/dist/core/leaf-succession.js +97 -0
  110. package/dist/core/media/canonical.d.ts +34 -0
  111. package/dist/core/media/canonical.js +57 -0
  112. package/dist/core/media/crypto.d.ts +79 -0
  113. package/dist/core/media/crypto.js +215 -0
  114. package/dist/core/media/imeta.d.ts +33 -0
  115. package/dist/core/media/imeta.js +188 -0
  116. package/dist/core/media/locator.d.ts +44 -0
  117. package/dist/core/media/locator.js +69 -0
  118. package/dist/core/media/types.d.ts +83 -0
  119. package/dist/core/media/types.js +12 -0
  120. package/dist/core/media.d.ts +6 -0
  121. package/dist/core/media.js +12 -0
  122. package/dist/core/protocol.d.ts +51 -0
  123. package/dist/core/protocol.js +41 -0
  124. package/dist/core/relay-lists.d.ts +52 -0
  125. package/dist/core/relay-lists.js +86 -0
  126. package/dist/core/retained-history.d.ts +78 -0
  127. package/dist/core/retained-history.js +59 -0
  128. package/dist/core/transport.d.ts +43 -0
  129. package/dist/core/transport.js +21 -0
  130. package/dist/core/welcome-event.d.ts +61 -0
  131. package/dist/core/welcome-event.js +116 -0
  132. package/dist/core/welcome-join.d.ts +155 -0
  133. package/dist/core/welcome-join.js +243 -0
  134. package/dist/core/welcome.d.ts +3 -0
  135. package/dist/core/welcome.js +6 -0
  136. package/dist/engine/admin-policy.d.ts +58 -0
  137. package/dist/engine/admin-policy.js +137 -0
  138. package/dist/engine/auto-committer.d.ts +39 -0
  139. package/dist/engine/auto-committer.js +42 -0
  140. package/dist/engine/delivered-payloads.d.ts +61 -0
  141. package/dist/engine/delivered-payloads.js +84 -0
  142. package/dist/engine/disband-request.d.ts +41 -0
  143. package/dist/engine/disband-request.js +86 -0
  144. package/dist/engine/disband-tombstone.d.ts +14 -0
  145. package/dist/engine/disband-tombstone.js +66 -0
  146. package/dist/engine/fork-recovery.d.ts +672 -0
  147. package/dist/engine/fork-recovery.js +1615 -0
  148. package/dist/engine/group-engine.d.ts +571 -0
  149. package/dist/engine/group-engine.js +5112 -0
  150. package/dist/engine/history-tree.d.ts +232 -0
  151. package/dist/engine/history-tree.js +651 -0
  152. package/dist/engine/index.d.ts +16 -0
  153. package/dist/engine/index.js +15 -0
  154. package/dist/engine/ingest-disposition.d.ts +5 -0
  155. package/dist/engine/ingest-disposition.js +63 -0
  156. package/dist/engine/ingest.d.ts +390 -0
  157. package/dist/engine/ingest.js +1258 -0
  158. package/dist/engine/ingestion-pool.d.ts +74 -0
  159. package/dist/engine/ingestion-pool.js +95 -0
  160. package/dist/engine/message-dedup.d.ts +16 -0
  161. package/dist/engine/message-dedup.js +20 -0
  162. package/dist/engine/own-commit-stamp.d.ts +30 -0
  163. package/dist/engine/own-commit-stamp.js +71 -0
  164. package/dist/engine/retained-store.d.ts +79 -0
  165. package/dist/engine/retained-store.js +133 -0
  166. package/dist/engine/sender-ratchet.d.ts +50 -0
  167. package/dist/engine/sender-ratchet.js +196 -0
  168. package/dist/engine/state-notifications.d.ts +122 -0
  169. package/dist/engine/state-notifications.js +173 -0
  170. package/dist/engine/tree-convergence.d.ts +41 -0
  171. package/dist/engine/tree-convergence.js +90 -0
  172. package/dist/engine/types.d.ts +366 -0
  173. package/dist/engine/types.js +14 -0
  174. package/dist/engine/wire-format.d.ts +77 -0
  175. package/dist/engine/wire-format.js +120 -0
  176. package/dist/index.d.ts +12 -0
  177. package/dist/index.js +1 -0
  178. package/dist/mls.d.ts +11 -0
  179. package/dist/mls.js +11 -0
  180. package/dist/utils/debug.d.ts +3 -0
  181. package/dist/utils/debug.js +3 -0
  182. package/dist/utils/encoding.d.ts +38 -0
  183. package/dist/utils/encoding.js +91 -0
  184. package/dist/utils/key-value.d.ts +18 -0
  185. package/dist/utils/key-value.js +2 -0
  186. package/dist/utils/nostr.d.ts +35 -0
  187. package/dist/utils/nostr.js +26 -0
  188. package/dist/utils/relay-url.d.ts +10 -0
  189. package/dist/utils/relay-url.js +23 -0
  190. package/dist/utils/tag-cardinality.d.ts +50 -0
  191. package/dist/utils/tag-cardinality.js +91 -0
  192. package/dist/utils/timestamp.d.ts +50 -0
  193. package/dist/utils/timestamp.js +95 -0
  194. package/dist/version.d.ts +112 -0
  195. package/dist/version.js +112 -0
  196. package/package.json +68 -3
@@ -0,0 +1,63 @@
1
+ /** @module @category Engine */
2
+ import { disposition, inputCategories, } from "../core/inbound.js";
3
+ /** Maps an {@link IngestResult} to its protocol-visible {@link Disposition}. */
4
+ export function ingestResultDisposition(result) {
5
+ switch (result.kind) {
6
+ case "processed":
7
+ return disposition.accepted();
8
+ case "rejected":
9
+ return disposition.stale(inputCategories.authorizationFailed);
10
+ case "deferred":
11
+ return disposition.deferred(result.reason);
12
+ case "refused":
13
+ return disposition.deferred("capacity");
14
+ case "invalidated":
15
+ return disposition.invalidated();
16
+ case "stateInvalidated":
17
+ // A withdrawn group-state notification is the state-side counterpart of
18
+ // an invalidated app payload (convergence.md calls withdrawal the
19
+ // counterpart of app-payload invalidation).
20
+ return disposition.invalidated();
21
+ case "appliedNotifications":
22
+ case "stateRevalidated":
23
+ return disposition.accepted();
24
+ case "leafEquivocation":
25
+ // Seam (`equivocationCredit`): evidence reported by branch selection,
26
+ // not an inbound input of its own.
27
+ return disposition.accepted();
28
+ case "autoCommit":
29
+ // A locally-staged self_remove-only commit (B6) — an accepted local action,
30
+ // not an inbound message disposition.
31
+ return disposition.accepted();
32
+ case "removed":
33
+ // A valid commit that legitimately removed us — accepted inbound; terminal
34
+ // for our membership (member-departure.md).
35
+ return disposition.accepted();
36
+ case "skipped": {
37
+ const reason = result.reason;
38
+ switch (reason) {
39
+ case "past-epoch":
40
+ return disposition.stale(inputCategories.alreadyApplied);
41
+ case "self-echo":
42
+ return disposition.stale(inputCategories.ownEcho);
43
+ case "duplicate":
44
+ return disposition.stale(inputCategories.duplicate);
45
+ case "wrong-wireformat":
46
+ case "invalid-app-payload":
47
+ return disposition.stale(inputCategories.invalidEncoding);
48
+ case "beyond-anchor":
49
+ case "missing-retained-anchor":
50
+ return disposition.stale(inputCategories.missingHistory);
51
+ case "self-evicted":
52
+ case "group-disbanded":
53
+ return disposition.stale(inputCategories.staleEpoch);
54
+ default: {
55
+ const exhaustive = reason;
56
+ return exhaustive;
57
+ }
58
+ }
59
+ }
60
+ case "unreadable":
61
+ return disposition.stale(inputCategories.invalidEncoding);
62
+ }
63
+ }
@@ -0,0 +1,390 @@
1
+ import { Debugger } from "debug";
2
+ import { type CiphersuiteImpl, type ClientState, type IncomingMessageCallback, MlsMessage, type ProcessMessageResult, type ProposalWithSender } from "ts-mls";
3
+ import type { ClientConfig } from "ts-mls";
4
+ import { type LeafEquivocation, type RemovedLeaf } from "../core/convergence.js";
5
+ import { type DeferredReason } from "../core/inbound.js";
6
+ import { type OpaqueHeldInfo } from "./fork-recovery.js";
7
+ import type { RetainedHistoryStore } from "./retained-store.js";
8
+ import { type StateNotification } from "./state-notifications.js";
9
+ import type { CommitRejection, DisbandCandidateEvidence, IngestResult, PeeledMessagePair } from "./types.js";
10
+ /** A message deferred this batch, remembered so terminal yields report it as
11
+ * `deferred` (retryable) rather than `unreadable` (terminal/malformed). */
12
+ export type DeferredEntry = {
13
+ message: MlsMessage;
14
+ reason: DeferredReason;
15
+ };
16
+ /** The applied outcome of a fork resolution, as the ingest loop consumes it. */
17
+ export type AppliedForkResolution<TEnvelope> = {
18
+ outcome: "recovered";
19
+ result: ProcessMessageResult;
20
+ /**
21
+ * The winning branch's tip commit MLS message, when the winner chain
22
+ * applied at least one commit to reach it (D-10/D-12) — lets the ingest
23
+ * loop attribute a `selfRemoved` notification to the exact commit that
24
+ * produced the `removedFromGroup` tombstone when the rewind lands on
25
+ * it. `undefined` only when the winner tip is the fork root itself (no
26
+ * chain applied), which never coincides with a fresh removal.
27
+ */
28
+ tipCommitMessage: MlsMessage | undefined;
29
+ /**
30
+ * The {@link StateNotification}s derived from the WHOLE applied winner
31
+ * chain (D-10/D-11) — one derivation per `ChainLink`, in chain order,
32
+ * concatenated. A rewind that adopts an N-commit branch applies N
33
+ * commits, so reporting only the tip's diff would silently drop every
34
+ * intermediate commit's membership/component changes (CR-07). Each
35
+ * entry stays attributed to the commit digest that produced it, so the
36
+ * caller can still group by commit.
37
+ *
38
+ * Computed and ledger-recorded by `#applyForkResolution` — the same
39
+ * shared rewind-apply path used by both pool-replay recovery and
40
+ * tree-fed re-convergence — so every one of them is withdrawable by a
41
+ * later rewind. `undefined` when the winner tip is the fork root itself
42
+ * (no chain applied, mirroring `tipCommitMessage`).
43
+ */
44
+ notifications: StateNotification[] | undefined;
45
+ /**
46
+ * App payloads abandoned by the rewind, to report as `invalidated` (M7).
47
+ * Each carries the fork node (`tag`/`epoch`) it had decrypted against so
48
+ * the retraction names its losing branch.
49
+ */
50
+ invalidated: {
51
+ envelope: TEnvelope;
52
+ message: MlsMessage;
53
+ payload: Uint8Array;
54
+ tag: string;
55
+ epoch: number;
56
+ }[];
57
+ /**
58
+ * Notifications withdrawn because the commit(s) that produced them were
59
+ * superseded by this rewind (D-11, CONV-03). Flat — not yet grouped by
60
+ * producing commit digest; the caller groups via
61
+ * {@link groupWithdrawnNotificationsByCommit} before yielding
62
+ * `stateInvalidated` results, since a rewind may supersede more than one
63
+ * commit at once.
64
+ */
65
+ withdrawnNotifications: StateNotification[];
66
+ /** Present only when canonical selection chose authenticated disband evidence. */
67
+ selectedTerminal?: DisbandCandidateEvidence;
68
+ /**
69
+ * Seam (convergence rejection reason): pool commits dropped as
70
+ * candidates for failing the authorization / commit-legality gate, with
71
+ * the gate; each is yielded `skipped` (`past-epoch`) carrying it as
72
+ * `droppedReason`. Present only when non-empty.
73
+ */
74
+ rejected?: ReadonlyMap<string, CommitRejection>;
75
+ /** Seam (`equivocationCredit`): the leaf equivocations selection found, whoever won. */
76
+ equivocations?: LeafEquivocation[];
77
+ /**
78
+ * Seam (`branchAwareFence`): hex digests of fenced pool commits judged
79
+ * unable to win — each is yielded `skipped` (`past-epoch`), never
80
+ * `unreadable`. Present only when non-empty.
81
+ * @internal
82
+ */
83
+ opaqueSkipped?: ReadonlySet<string>;
84
+ /**
85
+ * Seam (`branchAwareFence`): where each `opaqueSkipped` digest failed
86
+ * (parent node, its epoch, sender, cause) — what the engine needs to hold
87
+ * the skipped commit, which stays revisable. Present with `opaqueSkipped`.
88
+ * @internal
89
+ */
90
+ opaqueHeld?: ReadonlyMap<string, OpaqueHeldInfo>;
91
+ /**
92
+ * Seam (`branchAwareFence`): fenced or skipped commits replay discovered
93
+ * by decrypting undecryptable envelopes of this batch (indices into the
94
+ * `encrypted` list `resolveFork` was given) — the decision applies to
95
+ * that envelope, not only to the pool entries.
96
+ * @internal
97
+ */
98
+ opaqueEncrypted?: ReadonlyMap<string, {
99
+ indices: number[];
100
+ message: MlsMessage;
101
+ }>;
102
+ /**
103
+ * Seam (`branchAwareFence`): the winner's tip commit, when replay opened it from an undecryptable envelope.
104
+ * @internal
105
+ */
106
+ opaqueTipEnvelope?: {
107
+ indices: number[];
108
+ message: MlsMessage;
109
+ };
110
+ } | {
111
+ outcome: "superseded" | "skip";
112
+ /**
113
+ * Hex digests of pool commits fork recovery could not evaluate yet: they
114
+ * reference a proposal the fork root has not staged. Deferred, not
115
+ * dropped as `past-epoch`.
116
+ */
117
+ awaitingProposal?: ReadonlySet<string>;
118
+ /** Seam (convergence rejection reason): see the `recovered` outcome. */
119
+ rejected?: ReadonlyMap<string, CommitRejection>;
120
+ /**
121
+ * Hex digest → cause of pool commits whose replay showed this device lost
122
+ * its own path secrets (`isLostPathSecretsError`): each is yielded as a
123
+ * terminal `unreadable` carrying its cause, never `past-epoch`.
124
+ */
125
+ fenced?: ReadonlyMap<string, unknown>;
126
+ /**
127
+ * Seam (`equivocationCredit`): the leaf equivocations selection found
128
+ * (`superseded` only; with `branchAwareFence`, `skip` too).
129
+ */
130
+ equivocations?: LeafEquivocation[];
131
+ /**
132
+ * Seam (`branchAwareFence`): see the `recovered` outcome.
133
+ * @internal
134
+ */
135
+ opaqueSkipped?: ReadonlySet<string>;
136
+ /** @internal */
137
+ opaqueHeld?: ReadonlyMap<string, OpaqueHeldInfo>;
138
+ /** @internal */
139
+ opaqueEncrypted?: ReadonlyMap<string, {
140
+ indices: number[];
141
+ message: MlsMessage;
142
+ }>;
143
+ };
144
+ /**
145
+ * The engine-facing surface the ingest pipeline drives. State and lifecycle
146
+ * mutation stay owned by the engine; the pipeline only reads/advances through
147
+ * these hooks so the convergence rewind and `Unrecoverable` transition remain
148
+ * the engine's responsibility.
149
+ */
150
+ export interface IngestContext<TEnvelope> {
151
+ ciphersuite: CiphersuiteImpl;
152
+ /** ts-mls `ClientConfig` threaded into every `processMessage` call (AC-VENDOR-6). */
153
+ clientConfig?: ClientConfig;
154
+ peeler: {
155
+ peelGroupMessages(envelopes: TEnvelope[], state: ClientState): Promise<{
156
+ read: PeeledMessagePair<TEnvelope>[];
157
+ unreadable: TEnvelope[];
158
+ }>;
159
+ };
160
+ retained: RetainedHistoryStore;
161
+ /** The rollback horizon (`maxRewindCommits`) from the active convergence policy. */
162
+ maxRewindCommits: number;
163
+ log: Debugger;
164
+ getState(): ClientState;
165
+ /** True only for canonical protocol disband; Unrecoverable is not terminal. */
166
+ isDisbanded?: () => boolean;
167
+ setState(state: ClientState): void;
168
+ /**
169
+ * Records an applied commit on the canonical branch: updates retained history
170
+ * and the full-fork history tree. Replaces a direct `retained.record` so both
171
+ * stay in lockstep, with the freshly-produced child captured pristine.
172
+ * `senderLeafIndex` is the commit's authenticated sender
173
+ * (`authenticatedSenderOf` of the processing result), recorded on the
174
+ * history-tree edge whatever the commit's wire framing.
175
+ */
176
+ recordCommit(parentState: ClientState, message: MlsMessage, newState: ClientState, senderLeafIndex: number | undefined): void;
177
+ /**
178
+ * Seam: an inbound commit was applied in order, extending the canonical
179
+ * tip directly, with no fork pass. Upstream MDK applies every commit through
180
+ * a scheduled convergence pass and records its decision; the engine records
181
+ * the decision that pass settles with this commit as its only candidate.
182
+ * `senderLeafIndex` is the commit's authenticated sender, as for
183
+ * {@link recordCommit}.
184
+ */
185
+ commitAppliedInOrder?(parentState: ClientState, message: MlsMessage, newState: ClientState, senderLeafIndex: number | undefined): void;
186
+ /** Retains a fully validated terminal edge without advancing canonical state. */
187
+ admitDisbandCandidate(parentState: ClientState, message: MlsMessage, resultingState: ClientState, evidence: DisbandCandidateEvidence, senderLeafIndex: number | undefined): void;
188
+ /**
189
+ * Records that a proposal was staged onto the current state (its epoch and
190
+ * confirmation tag are unchanged), so the history tree's node snapshot picks
191
+ * up the new `unappliedProposals`.
192
+ */
193
+ recordProposalStaged(state: ClientState): void;
194
+ /**
195
+ * Builds the admin-verification callback against `state` (default: the
196
+ * current state).
197
+ */
198
+ createAdminCallback(state?: ClientState): IncomingMessageCallback;
199
+ /**
200
+ * Seam (epic-marmot-client S20, `removalConvergence`): a fresh copy of the
201
+ * state a held removal of the local leaf was applied to, while the engine
202
+ * still treats that removal as revisable; `undefined` otherwise (and always
203
+ * without the seam: upstream D-13).
204
+ */
205
+ removalParent?(): ClientState | undefined;
206
+ /**
207
+ * Seam (S20): records a commit that removed the local leaf from
208
+ * `parentState` — the removal now held, or a competing removal at the same
209
+ * parent, by its authenticated sender `senderLeafIndex`. Present only with
210
+ * the seam.
211
+ *
212
+ * Seam (`removalFinality`): `tombstone` is the commit's `removedFromGroup`
213
+ * state, from which the engine reads the removal's facts (every leaf it
214
+ * removes, ADR-017 R-1 on the tombstone).
215
+ */
216
+ recordRemoval?(parentState: ClientState, message: MlsMessage, senderLeafIndex: number | undefined, tombstone?: ClientState): void;
217
+ /**
218
+ * Seam (`branchAwareFence`): called for each envelope an opaque decision
219
+ * (a fenced or skipped pool-replay commit, or a wrapper of the winning tip
220
+ * commit replay opened from `encrypted`) made terminal, so the caller drops
221
+ * any retryable copy it holds. Present only with the seam, and never for any
222
+ * other skip.
223
+ * @internal
224
+ */
225
+ opaqueDecided?(envelope: TEnvelope): void;
226
+ /**
227
+ * Seam (`branchAwareFence`): called at the point the pipeline learns that no
228
+ * state it holds decrypts `envelope`, BEFORE any result of the pass is
229
+ * yielded, so a consumer that stops iterating cannot lose the id from the
230
+ * group's undecryptable set `U` (spec Design Decision 3a). Present only with
231
+ * the seam.
232
+ * @internal
233
+ */
234
+ undecryptable?(envelope: TEnvelope): void;
235
+ /**
236
+ * Seam (`branchAwareFence`): the pipeline decided the held commit `message`
237
+ * fenced and is about to yield its `unreadable`; the engine marks the record
238
+ * so an entry point whose delivery was interrupted can re-deliver it.
239
+ * @internal
240
+ */
241
+ fencedOpaque?(message: MlsMessage): void;
242
+ /**
243
+ * Seam (`branchAwareFence`): decides lost-path-secrets commits that failed at
244
+ * `parent`, the engine's shared opaque decision over the history tree and a
245
+ * held removal (the rule pool replay applies): `fenced` holds the hex
246
+ * digests that could win, or have no eligible processable branch to lose to
247
+ * (a candidate it cannot score fences too). With `hold`, the engine also
248
+ * keeps every other one as a held candidate; the caller then yields it
249
+ * `skipped`. `equivocations` is the candidate tree's report with every
250
+ * opaque head edge in it, yielded whichever way the decision goes. A `skipped` for a held candidate is provisional: a later `unreadable` for the same envelope is the fence verdict (group state, not a per-event disposition). Candidates
251
+ * the engine already holds join the tree, so a later call may repeat an
252
+ * earlier report; the consumer's once-only key absorbs the repeat. Present
253
+ * only with the seam; without it a commit fences on the spot, as before.
254
+ */
255
+ /**
256
+ * Seam (`branchAwareFence`): registers a commit that just failed the direct
257
+ * apply with the engine's held set, before the batch routes it, so a
258
+ * generator abandoned mid-batch cannot lose it (the next entry point
259
+ * settles it). A `skipped` for a held commit is PROVISIONAL: a later pass
260
+ * may yield `unreadable` for the same envelope, the fence verdict (the
261
+ * fence is group state, not a per-event disposition; Design Decision 6).
262
+ * Returns `false` when the engine's held set is full (the cap): the caller
263
+ * then fences the commit at once, yielding `unreadable` there as its final
264
+ * disposition (nothing was registered, so nothing is released). Registering
265
+ * a commit already held is a no-op that returns `true`. `parentTag` and
266
+ * `parentEpoch` name the tree node the commit failed at.
267
+ * @internal
268
+ */
269
+ holdOpaque?(candidate: {
270
+ parentTag: string;
271
+ parentEpoch: number;
272
+ envelope: TEnvelope;
273
+ message: MlsMessage;
274
+ senderLeafIndex: number | undefined;
275
+ cause: unknown;
276
+ /** Seam (`removalFinality`): the head's R-2 facts ({@link IngestContext.opaqueRemovalFacts}); absent: unknown. */
277
+ removed?: readonly RemovedLeaf[];
278
+ }): boolean;
279
+ /**
280
+ * Seam (`branchAwareFence`): the batch's routing decided a commit registered with {@link holdOpaque}.
281
+ * @internal
282
+ */
283
+ releaseOpaque?(message: MlsMessage): void;
284
+ /**
285
+ * Seam (`branchAwareFence`): the engine's shared opaque decision for the
286
+ * commits that failed at `parent` (`hold`: keep each skipped one held). The
287
+ * pipeline hands it the sites it cannot decide itself and reads back which
288
+ * candidates fence.
289
+ * @internal
290
+ */
291
+ decideOpaque?(batch: {
292
+ parent: ClientState;
293
+ candidates: {
294
+ envelope: TEnvelope;
295
+ message: MlsMessage;
296
+ senderLeafIndex: number | undefined;
297
+ cause: unknown;
298
+ /** Seam (`removalFinality`): the head's R-2 facts; absent: unknown. */
299
+ removed?: readonly RemovedLeaf[];
300
+ }[];
301
+ hold: boolean;
302
+ }): Promise<{
303
+ fenced: ReadonlySet<string>;
304
+ equivocations: LeafEquivocation[];
305
+ }>;
306
+ /**
307
+ * Seam (`removalFinality`, ADR-017 R-2): an opaque head's removal facts from
308
+ * the proposals the admin callback captured at `parent` (the engine's
309
+ * `opaqueRemovalFactsOf`). Present only while the rule is effective at the
310
+ * fence: without it the pipeline reads no facts.
311
+ * @internal
312
+ */
313
+ opaqueRemovalFacts?(parent: ClientState, proposals: readonly ProposalWithSender[]): readonly RemovedLeaf[] | undefined;
314
+ /** Resolves a fork and applies the rewind (state + lifecycle) on success. */
315
+ resolveFork(forkEpoch: number, pool: MlsMessage[], encrypted: TEnvelope[], witnessEnvelopes: TEnvelope[]): Promise<AppliedForkResolution<TEnvelope>>;
316
+ /**
317
+ * Remembers an application payload delivered as `accepted` on the current
318
+ * branch state, so a later rewind that abandons that branch can retract it
319
+ * as `invalidated` (M7).
320
+ */
321
+ recordDeliveredAppPayload(epoch: number, stateTag: string, envelope: TEnvelope, message: MlsMessage, payload: Uint8Array): void;
322
+ /**
323
+ * Records the {@link StateNotification}s derived from an accepted commit,
324
+ * keyed by its `commitDigest` (D-10/D-11), so a later rewind that supersedes
325
+ * this commit can withdraw exactly these notifications.
326
+ */
327
+ recordStateNotifications(digest: Uint8Array, epoch: number, notifications: StateNotification[]): void;
328
+ /** Drives the group to the terminal `Unrecoverable` lifecycle state. */
329
+ toUnrecoverable(): void;
330
+ /**
331
+ * Content-derived inbound dedup (`inbound-processing.md`; reference
332
+ * `seen_message_ids` / `sent_message_ids`). Keyed on {@link contentDedupId} so
333
+ * the same MLS message re-wrapped in a fresh transport envelope is recognized
334
+ * across sources and restarts-within-process.
335
+ */
336
+ dedup: {
337
+ /**
338
+ * Classifies a peeled message against prior history: `duplicate` (this
339
+ * content was already terminally processed), `own-echo` (our own send
340
+ * replayed back), or `undefined` (fresh — process it).
341
+ */
342
+ classify(message: MlsMessage): "duplicate" | "own-echo" | undefined;
343
+ /** Records a content id as seen, once the message reaches a terminal apply. */
344
+ remember(message: MlsMessage): void;
345
+ };
346
+ }
347
+ /**
348
+ * Whether a decrypted application message is authentic: its inner Nostr event
349
+ * id is canonical AND its `pubkey` matches the MLS-authenticated sender's
350
+ * account identity (`foundation/identity.md`, `protocol-core/group-messaging.md`).
351
+ * MLS authenticates *who* sent the bytes (the sender leaf); this binds the inner
352
+ * author to that sender so a member can't forge another account's authorship.
353
+ * A failure — including an unattributable sender (no leaf index) or a
354
+ * non-conformant payload — is `invalid_encoding`; the message is dropped, never
355
+ * delivered.
356
+ *
357
+ * Uses `result.senderCredential`, not a fresh `getCredentialFromLeafIndex`
358
+ * lookup against the *current* tree: for a past-epoch `PrivateMessage`
359
+ * ts-mls decrypts through `historicalReceiverData` and resolves the sender
360
+ * credential against *that epoch's* ratchet tree before returning it here
361
+ * (seam-application-authorship-past-epoch-credential). Re-deriving the
362
+ * credential from the current tree would misattribute the message once the
363
+ * sender's leaf index is reused by a later member (M3 continued: this is
364
+ * what keeps a late message from being attributed to whoever now occupies
365
+ * that leaf).
366
+ */
367
+ export declare function isAuthenticApplicationMessage(result: ProcessMessageResult & {
368
+ kind: "applicationMessage";
369
+ }, log: Debugger, label: string): boolean;
370
+ /**
371
+ * Ingests transport envelopes and applies MLS messages to group state
372
+ * (Marmot v2 `protocol-core/inbound-processing.md`). Decrypts (retrying against
373
+ * retained states), splits commits from non-commits, applies in-order commits,
374
+ * routes past/future-epoch commits through convergence fork recovery, and
375
+ * retries out-of-order messages only while a pass made progress.
376
+ *
377
+ * This is the engine's `message_processor/ingest` seam, extracted from
378
+ * `MarmotGroupEngine` so the 400-line pipeline can be read and tested in
379
+ * isolation from send and lifecycle.
380
+ */
381
+ export declare function ingestEnvelopes<TEnvelope>(ctx: IngestContext<TEnvelope>, envelopes: TEnvelope[], options?: {
382
+ retryCount?: number;
383
+ maxRetries?: number;
384
+ _errors?: Array<{
385
+ envelope: TEnvelope;
386
+ error: unknown;
387
+ }>;
388
+ _deferred?: Map<TEnvelope, DeferredEntry>;
389
+ _decryptFailed?: Set<TEnvelope>;
390
+ }): AsyncGenerator<IngestResult<TEnvelope>>;