@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,232 @@
1
+ import { type ClientState, type MlsMessage } from "ts-mls";
2
+ import type { GenericKeyValueStore } from "../utils/key-value.js";
3
+ import { type OwnCommitConvergenceStamp } from "./own-commit-stamp.js";
4
+ /** The store backing a tree's heavy material. A `GenericKeyValueStore<Uint8Array>`. */
5
+ export type HistoryTreeStore = GenericKeyValueStore<Uint8Array>;
6
+ /**
7
+ * A delivered application message kept as a convergence witness input
8
+ * (`convergence.md` "App-payload witnesses"). Persisted beside the tree's
9
+ * nodes so a restarted engine can still weigh a branch by the application
10
+ * messages it had delivered before the restart — upstream MDK re-admits its
11
+ * stored processed application messages for witness scoring the same way.
12
+ */
13
+ export interface PersistedWitness {
14
+ /** Hex SHA-256 of the message's encoded bytes: the record identity. */
15
+ digest: string;
16
+ /** Epoch of the state the message decrypted against. */
17
+ epoch: number;
18
+ /** Hex confirmation tag of that state — its branch+epoch identity. */
19
+ stateTag: string;
20
+ /** The MLS application message (the encrypted wrapper, not the transport envelope). */
21
+ message: MlsMessage;
22
+ }
23
+ /**
24
+ * Protocol-level metadata for one edge — the commit that produced a node from
25
+ * its parent. The decrypted commit bytes themselves are retained separately
26
+ * (see {@link GroupHistoryTree.commitBytesOf}) so the tree is self-contained and
27
+ * replayable without the transport's event store.
28
+ */
29
+ export interface HistoryEdge {
30
+ /** SHA-256 (32 bytes) of the commit MLS message bytes — the edge identity. */
31
+ commitDigest: Uint8Array;
32
+ /**
33
+ * The committer's authenticated MLS leaf index at the parent — what
34
+ * processing the commit there yielded, independent of its wire framing
35
+ * (a `PrivateMessage` commit included). `undefined` only for a non-member
36
+ * (external) sender, or an edge recorded before senders were attributed
37
+ * this way.
38
+ */
39
+ senderLeafIndex?: number;
40
+ }
41
+ /**
42
+ * An edge plus its child's pre-serialized snapshot, captured at the moment the
43
+ * child state is produced (see {@link GroupHistoryTree.recordEdge}). Fork
44
+ * recovery emits these so loser branches are retained with pristine snapshots.
45
+ */
46
+ export interface EdgeSnapshot {
47
+ /** Parent node tag (must already be in the tree). */
48
+ parentTag: string;
49
+ /** Child node tag (hex of the child state's confirmation tag). */
50
+ childTag: string;
51
+ /** The child state's MLS epoch. */
52
+ childEpoch: number;
53
+ /** Serialized commit `MlsMessage` that produced the child. */
54
+ commitBytes: Uint8Array;
55
+ /** SHA-256 of `commitBytes`. */
56
+ commitDigest: Uint8Array;
57
+ /** Serialized child `ClientState`, captured before any secret zeroing. */
58
+ childSnapshot: Uint8Array;
59
+ /** The committer's authenticated MLS leaf index (see {@link HistoryEdge.senderLeafIndex}). */
60
+ senderLeafIndex?: number;
61
+ }
62
+ /**
63
+ * A node in the group history tree: one MLS group state. The node id is the hex
64
+ * of the state's MLS `confirmationTag`, which is unique per state (it is a MAC
65
+ * over the confirmed transcript hash, so two same-epoch forks have distinct
66
+ * tags). Every non-root node has exactly one parent — the state its commit was
67
+ * applied to — so the structure is a tree rooted at the welcome/creation state;
68
+ * more than one child marks a fork.
69
+ */
70
+ export interface HistoryNode {
71
+ /** Node id: hex of the MLS confirmation tag. */
72
+ tag: string;
73
+ /** The MLS epoch this state sits at. */
74
+ epoch: number;
75
+ /** Parent node tag, or `undefined` for the root. */
76
+ parentTag?: string;
77
+ /** Child node tags. More than one means a fork was observed at this node. */
78
+ childTags: string[];
79
+ /** The commit edge from the parent. `undefined` only for the root. */
80
+ edge?: HistoryEdge;
81
+ }
82
+ /**
83
+ * The retained group history tree (Marmot v2 full-fork history). Holds every
84
+ * group state ever observed — the canonical branch and every fork — as a tree
85
+ * of {@link ClientState} snapshots linked by commit edges. No pruning is
86
+ * performed: the tree retains everything.
87
+ *
88
+ * The **light index** (per-node epoch/parent/edge metadata) is always resident.
89
+ * **Heavy material** (serialized snapshots + commit bytes) is kept in a bounded
90
+ * in-memory LRU and otherwise fetched from a backing key-value store on demand,
91
+ * so memory stays bounded even as the tree grows without limit. Newly recorded
92
+ * nodes are pinned in memory until {@link flush} persists them.
93
+ *
94
+ * Snapshots are stored as bytes, never as live `ClientState` objects: ts-mls
95
+ * zeroes a parent state's consumed secrets in place when a commit is processed
96
+ * from it, so a retained live object could be corrupted out from under a
97
+ * sibling-branch replay. Re-deriving a state ({@link stateAt}) decodes a fresh,
98
+ * independent object every call.
99
+ */
100
+ export declare class GroupHistoryTree {
101
+ #private;
102
+ /** Seeds an empty tree, optionally with a root {@link ClientState}. */
103
+ constructor(root?: ClientState, options?: {
104
+ snapshotCacheSize?: number;
105
+ });
106
+ /** The root node tag (the welcome/creation state), or `undefined` if empty. */
107
+ get rootTag(): string | undefined;
108
+ /** Number of nodes (states) retained in the tree. */
109
+ get size(): number;
110
+ /** Oldest epoch still named by the tree, or `undefined` when it is empty. */
111
+ oldestEpoch(): number | undefined;
112
+ /** Whether unflushed changes are pending. */
113
+ get isDirty(): boolean;
114
+ /**
115
+ * Sets the root from a {@link ClientState}. The root carries no commit edge.
116
+ * Throws if a different root is already set (a tree has exactly one root).
117
+ */
118
+ setRoot(state: ClientState): string;
119
+ /** Whether a node with `tag` exists. */
120
+ hasNode(tag: string): boolean;
121
+ /** Returns a read-only view of a node, or `undefined` if absent. */
122
+ node(tag: string): HistoryNode | undefined;
123
+ /** All node tags, in insertion order. */
124
+ tags(): string[];
125
+ /** The epoch of a node, or `undefined` if absent. */
126
+ epochOf(tag: string): number | undefined;
127
+ /** The parent tag of a node, or `undefined` for the root / absent node. */
128
+ parentOf(tag: string): string | undefined;
129
+ /** Child tags of a node (empty for a tip or an absent node). */
130
+ childrenOf(tag: string): string[];
131
+ /** Whether a node is a tip (exists and has no children). */
132
+ isTip(tag: string): boolean;
133
+ /** All tip tags (leaf states) — the candidate branches for convergence. */
134
+ tips(): string[];
135
+ /** All node tags sitting at `epoch`. */
136
+ nodesAtEpoch(epoch: number): string[];
137
+ /**
138
+ * The path from the root to `tag` (inclusive of both), or `undefined` if the
139
+ * node is absent or its chain to the root is broken.
140
+ */
141
+ path(tag: string): string[] | undefined;
142
+ /** Ancestor tags of a node, nearest-first (excludes the node itself). */
143
+ ancestors(tag: string): string[];
144
+ /**
145
+ * The lowest common ancestor of two nodes (the fork point), or `undefined` if
146
+ * they share no ancestor (e.g. live in different trees).
147
+ */
148
+ lowestCommonAncestor(a: string, b: string): string | undefined;
149
+ /**
150
+ * The serialized `ClientState` snapshot bytes for a node, or `undefined` if
151
+ * absent. Served from the in-memory cache, else fetched from the store.
152
+ */
153
+ snapshotOf(tag: string): Promise<Uint8Array | undefined>;
154
+ /**
155
+ * Rehydrates a node's state into a fresh, independent {@link ClientState}, or
156
+ * `undefined` if no snapshot is retained. Each call decodes a new object, so
157
+ * callers may mutate/advance it without affecting the tree.
158
+ */
159
+ stateAt(tag: string): Promise<ClientState | undefined>;
160
+ /** The serialized commit bytes that produced a node, or `undefined`. */
161
+ commitBytesOf(childTag: string): Promise<Uint8Array | undefined>;
162
+ /** Confirmation-time evidence for a locally-authored commit, if stamped. */
163
+ ownCommitStampOf(childTag: string): Promise<OwnCommitConvergenceStamp | undefined>;
164
+ /** Decodes the commit `MlsMessage` that produced a node, or `undefined`. */
165
+ commitMessageOf(childTag: string): Promise<MlsMessage | undefined>;
166
+ /**
167
+ * Records a commit applied from a retained parent node to its resulting child
168
+ * state, adding (or linking) the child node. Idempotent on the child tag: a
169
+ * duplicate commit re-links without overwriting. Throws if the parent is not
170
+ * already in the tree.
171
+ *
172
+ * @returns the child node tag.
173
+ */
174
+ recordCommit(parentTag: string, commitMessage: MlsMessage, childState: ClientState, senderLeafIndex?: number, ownCommitStamp?: OwnCommitConvergenceStamp): string;
175
+ /**
176
+ * Records an edge from a snapshot captured at branch-build time. Unlike
177
+ * {@link recordCommit}, the child snapshot is supplied pre-serialized — fork
178
+ * recovery serializes each branch state the instant it is produced, before
179
+ * ts-mls can zero that state's secrets when exploring its children. Idempotent
180
+ * on the child tag. Returns `false` (without recording) when the parent is not
181
+ * yet in the tree, so a batch can skip a dangling edge instead of throwing.
182
+ */
183
+ recordEdge(edge: EdgeSnapshot): boolean;
184
+ /**
185
+ * Replaces a node's retained snapshot — used after staging a proposal onto a
186
+ * node, which updates its `unappliedProposals` without advancing the epoch.
187
+ * The new state's confirmation tag MUST equal the node tag (staging a proposal
188
+ * does not change it). Throws if the node is absent or the tag would change.
189
+ */
190
+ updateSnapshot(tag: string, state: ClientState): void;
191
+ /**
192
+ * Keeps a delivered application message as a witness input, persisted on the
193
+ * next {@link flush} and reloaded by {@link load}. Idempotent on the message.
194
+ *
195
+ * @returns the record digest.
196
+ */
197
+ recordWitness(epoch: number, stateTag: string, message: MlsMessage): string;
198
+ /** The retained witness inputs, in the order they were recorded (loaded ones in key order). */
199
+ witnesses(): readonly PersistedWitness[];
200
+ /** Forgets every witness input delivered below `epoch` (see the engine's `#pruneDelivered` for the bound). */
201
+ pruneWitnessesBelow(epoch: number): void;
202
+ /**
203
+ * Forgets the witness inputs a rewind abandons: those delivered strictly
204
+ * after `forkEpoch` on a state outside `canonicalTags` (the same rule as the
205
+ * delivered-payload ledger's `invalidatedByRewind`).
206
+ */
207
+ invalidateWitnessesByRewind(forkEpoch: number, canonicalTags: ReadonlySet<string>): void;
208
+ /**
209
+ * Binds a backing store to a fresh, in-memory tree so its nodes can be flushed
210
+ * and its heavy material later evicted/reloaded. Marks every current node
211
+ * dirty so the first {@link flush} persists the whole tree. A no-op if the
212
+ * same store is already bound (e.g. on a tree loaded via {@link load}).
213
+ */
214
+ bindStore(store: HistoryTreeStore): void;
215
+ /**
216
+ * Persists all unflushed nodes incrementally: each dirty node's light edge
217
+ * record, snapshot, and commit bytes are written under its own keys, plus the
218
+ * meta (root) record. Append-only — already-persisted nodes are untouched, so
219
+ * a save costs O(new nodes), not O(tree). Requires a bound store.
220
+ */
221
+ flush(): Promise<void>;
222
+ /**
223
+ * Loads a tree's light index from a store (heavy material stays lazy). Returns
224
+ * `undefined` if no tree is persisted under `gid`. The store stays bound, so
225
+ * snapshots/commit bytes are fetched on demand.
226
+ */
227
+ static load(store: HistoryTreeStore, gid: string, options?: {
228
+ snapshotCacheSize?: number;
229
+ }): Promise<GroupHistoryTree | undefined>;
230
+ /** Removes every persisted key for a group's tree from the store. */
231
+ static purge(store: HistoryTreeStore, gid: string): Promise<void>;
232
+ }