@941design/marmot-engine 0.0.0-stage → 0.2.0-alpha.1

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 (198) hide show
  1. package/CHANGELOG.md +477 -0
  2. package/LICENSE +53 -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 +93 -0
  55. package/dist/core/components/ids.js +92 -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 +56 -0
  91. package/dist/core/group.js +95 -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 +29 -0
  95. package/dist/core/index.js +27 -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 +117 -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 +66 -0
  107. package/dist/core/key-package.js +147 -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-profile.d.ts +262 -0
  123. package/dist/core/protocol-profile.js +535 -0
  124. package/dist/core/protocol.d.ts +51 -0
  125. package/dist/core/protocol.js +41 -0
  126. package/dist/core/relay-lists.d.ts +52 -0
  127. package/dist/core/relay-lists.js +86 -0
  128. package/dist/core/retained-history.d.ts +78 -0
  129. package/dist/core/retained-history.js +59 -0
  130. package/dist/core/transport.d.ts +43 -0
  131. package/dist/core/transport.js +21 -0
  132. package/dist/core/welcome-event.d.ts +61 -0
  133. package/dist/core/welcome-event.js +116 -0
  134. package/dist/core/welcome-join.d.ts +155 -0
  135. package/dist/core/welcome-join.js +243 -0
  136. package/dist/core/welcome.d.ts +3 -0
  137. package/dist/core/welcome.js +6 -0
  138. package/dist/engine/admin-policy.d.ts +75 -0
  139. package/dist/engine/admin-policy.js +137 -0
  140. package/dist/engine/auto-committer.d.ts +39 -0
  141. package/dist/engine/auto-committer.js +42 -0
  142. package/dist/engine/delivered-payloads.d.ts +61 -0
  143. package/dist/engine/delivered-payloads.js +84 -0
  144. package/dist/engine/disband-request.d.ts +41 -0
  145. package/dist/engine/disband-request.js +86 -0
  146. package/dist/engine/disband-tombstone.d.ts +14 -0
  147. package/dist/engine/disband-tombstone.js +66 -0
  148. package/dist/engine/fork-recovery.d.ts +672 -0
  149. package/dist/engine/fork-recovery.js +1615 -0
  150. package/dist/engine/group-engine.d.ts +571 -0
  151. package/dist/engine/group-engine.js +5134 -0
  152. package/dist/engine/history-tree.d.ts +232 -0
  153. package/dist/engine/history-tree.js +651 -0
  154. package/dist/engine/index.d.ts +16 -0
  155. package/dist/engine/index.js +15 -0
  156. package/dist/engine/ingest-disposition.d.ts +5 -0
  157. package/dist/engine/ingest-disposition.js +63 -0
  158. package/dist/engine/ingest.d.ts +390 -0
  159. package/dist/engine/ingest.js +1258 -0
  160. package/dist/engine/ingestion-pool.d.ts +74 -0
  161. package/dist/engine/ingestion-pool.js +95 -0
  162. package/dist/engine/message-dedup.d.ts +16 -0
  163. package/dist/engine/message-dedup.js +20 -0
  164. package/dist/engine/own-commit-stamp.d.ts +30 -0
  165. package/dist/engine/own-commit-stamp.js +71 -0
  166. package/dist/engine/retained-store.d.ts +79 -0
  167. package/dist/engine/retained-store.js +133 -0
  168. package/dist/engine/sender-ratchet.d.ts +50 -0
  169. package/dist/engine/sender-ratchet.js +196 -0
  170. package/dist/engine/state-notifications.d.ts +122 -0
  171. package/dist/engine/state-notifications.js +173 -0
  172. package/dist/engine/tree-convergence.d.ts +41 -0
  173. package/dist/engine/tree-convergence.js +90 -0
  174. package/dist/engine/types.d.ts +366 -0
  175. package/dist/engine/types.js +14 -0
  176. package/dist/engine/wire-format.d.ts +77 -0
  177. package/dist/engine/wire-format.js +120 -0
  178. package/dist/index.d.ts +12 -0
  179. package/dist/index.js +1 -0
  180. package/dist/mls.d.ts +11 -0
  181. package/dist/mls.js +11 -0
  182. package/dist/utils/debug.d.ts +3 -0
  183. package/dist/utils/debug.js +3 -0
  184. package/dist/utils/encoding.d.ts +38 -0
  185. package/dist/utils/encoding.js +91 -0
  186. package/dist/utils/key-value.d.ts +18 -0
  187. package/dist/utils/key-value.js +2 -0
  188. package/dist/utils/nostr.d.ts +35 -0
  189. package/dist/utils/nostr.js +26 -0
  190. package/dist/utils/relay-url.d.ts +10 -0
  191. package/dist/utils/relay-url.js +23 -0
  192. package/dist/utils/tag-cardinality.d.ts +50 -0
  193. package/dist/utils/tag-cardinality.js +91 -0
  194. package/dist/utils/timestamp.d.ts +50 -0
  195. package/dist/utils/timestamp.js +95 -0
  196. package/dist/version.d.ts +112 -0
  197. package/dist/version.js +112 -0
  198. package/package.json +68 -3
@@ -0,0 +1,42 @@
1
+ /** @module @category Engine */
2
+ /**
3
+ * Decides whether *this* client auto-commits the round's `self_remove`
4
+ * proposals (Marmot v2 `protocol-core/member-departure.md`, darkmatter
5
+ * `auto_committer.rs`).
6
+ *
7
+ * A `self_remove`-only commit may carry several departures at once, so the
8
+ * decision is over the whole leaver set rather than one proposal. Every
9
+ * remaining member runs this independently on the same inputs, so the choice is
10
+ * deterministic and exactly one member commits — which is what stops concurrent
11
+ * committers from forking the group. The rule:
12
+ *
13
+ * 1. A leaver never commits the batch (RFC 9420 §12.2 — a committer cannot
14
+ * remove their own leaf).
15
+ * 2. If any leaver is still an active admin the batch is refused: an admin MUST
16
+ * drop admin before leaving, so it would produce an invalid commit.
17
+ * `anyLeaverIsActiveAdmin` is fail-closed — pass `true` when the admin set or
18
+ * a leaver's identity cannot be read, so an unreadable state never commits.
19
+ * 3. Otherwise the eligible committers are all current members except the
20
+ * leavers, and only the one with the lowest MLS leaf index commits.
21
+ *
22
+ * There is no fallback timer: if the lowest-index eligible member is offline,
23
+ * the commit simply waits until they (or a re-evaluated set) act.
24
+ */
25
+ export function decideAutoCommit(params) {
26
+ const { leaverLeafIndices, ownLeafIndex, memberLeafIndices, anyLeaverIsActiveAdmin, } = params;
27
+ const leavers = new Set(leaverLeafIndices);
28
+ if (leavers.size === 0)
29
+ return "observe";
30
+ // (1) A leaver never commits the batch.
31
+ if (leavers.has(ownLeafIndex))
32
+ return "observe";
33
+ // (2) An admin must leave the admin set before self-removing; never
34
+ // auto-commit a batch containing an admin's self_remove (fail-closed).
35
+ if (anyLeaverIsActiveAdmin)
36
+ return "observe";
37
+ // (3) Lowest-leaf-index eligible member (all members except the leavers) commits.
38
+ const eligible = memberLeafIndices.filter((i) => !leavers.has(i));
39
+ if (eligible.length === 0)
40
+ return "observe";
41
+ return ownLeafIndex === Math.min(...eligible) ? "commit" : "observe";
42
+ }
@@ -0,0 +1,61 @@
1
+ /** @module @category Engine */
2
+ import type { MlsMessage } from "ts-mls";
3
+ /**
4
+ * One application payload delivered (yielded `processed` / `accepted`) on a
5
+ * specific branch state. Remembered so that if a later convergence rewind
6
+ * abandons the branch it decrypted on, the payload can be reported
7
+ * `invalidated` (`protocol-core/inbound-processing.md`, `convergence.md`).
8
+ */
9
+ export interface DeliveredAppPayload<TEnvelope> {
10
+ /** Epoch of the state the payload decrypted against. */
11
+ epoch: number;
12
+ /** Hex confirmation tag of that state — its branch+epoch identity. */
13
+ stateTag: string;
14
+ envelope: TEnvelope;
15
+ /** The MLS application message (encrypted wrapper). */
16
+ message: MlsMessage;
17
+ /** The decrypted Marmot app payload bytes, so a retraction can name it. */
18
+ payload: Uint8Array;
19
+ }
20
+ /**
21
+ * A bounded ledger of application payloads already delivered as `accepted`,
22
+ * keyed by the branch state they decrypted on.
23
+ *
24
+ * Marmot v2 delivers app payloads eagerly — there is no settle-then-release
25
+ * gate yet (that is B5) — so when a convergence fork rewind abandons a branch,
26
+ * the payloads delivered on it MUST be retracted with an `invalidated`
27
+ * notification. This ledger lets the engine find exactly those payloads on a
28
+ * rewind.
29
+ *
30
+ * It holds no protocol state of its own; entries are pruned only below the
31
+ * oldest state still named by retained history or the fork tree. A finite,
32
+ * pruned tree can bound this ledger. With an unpruned full-history tree — and
33
+ * especially `maxRewindCommits: Infinity` — correctness requires unbounded
34
+ * retention until tree pruning exists. Mirrors the bookkeeping darkmatter does in
35
+ * `distributed_convergence.rs` (`AppMessageInvalidated`).
36
+ */
37
+ export declare class DeliveredPayloadLedger<TEnvelope> {
38
+ #private;
39
+ /** Number of remembered payloads. */
40
+ get size(): number;
41
+ /** Whether this exact MLS message is recorded against `stateTag`. */
42
+ has(stateTag: string, message: MlsMessage): boolean;
43
+ /** Retained transport envelopes used as witnesses by later fork passes. */
44
+ envelopes(): TEnvelope[];
45
+ /** Remembers a delivered application payload. */
46
+ record(entry: DeliveredAppPayload<TEnvelope>): void;
47
+ /**
48
+ * Removes and returns the payloads abandoned by a rewind to a canonical
49
+ * branch: those delivered strictly after `forkEpoch` whose delivery state is
50
+ * not on the canonical chain (`canonicalTags`). Payloads that decrypted on
51
+ * the canonical branch, and any at or below the fork epoch (shared history),
52
+ * are retained.
53
+ */
54
+ invalidatedByRewind(forkEpoch: number, canonicalTags: ReadonlySet<string>): DeliveredAppPayload<TEnvelope>[];
55
+ /**
56
+ * Drops entries below the caller's tree-aware correctness horizon. The
57
+ * engine supplies `min(retained anchor, oldest tree-node epoch)` so no
58
+ * payload still nameable by a fork candidate loses its retraction record.
59
+ */
60
+ pruneBelow(epoch: number): void;
61
+ }
@@ -0,0 +1,84 @@
1
+ /**
2
+ * A bounded ledger of application payloads already delivered as `accepted`,
3
+ * keyed by the branch state they decrypted on.
4
+ *
5
+ * Marmot v2 delivers app payloads eagerly — there is no settle-then-release
6
+ * gate yet (that is B5) — so when a convergence fork rewind abandons a branch,
7
+ * the payloads delivered on it MUST be retracted with an `invalidated`
8
+ * notification. This ledger lets the engine find exactly those payloads on a
9
+ * rewind.
10
+ *
11
+ * It holds no protocol state of its own; entries are pruned only below the
12
+ * oldest state still named by retained history or the fork tree. A finite,
13
+ * pruned tree can bound this ledger. With an unpruned full-history tree — and
14
+ * especially `maxRewindCommits: Infinity` — correctness requires unbounded
15
+ * retention until tree pruning exists. Mirrors the bookkeeping darkmatter does in
16
+ * `distributed_convergence.rs` (`AppMessageInvalidated`).
17
+ */
18
+ export class DeliveredPayloadLedger {
19
+ #entries = new Map();
20
+ /** Number of remembered payloads. */
21
+ get size() {
22
+ let size = 0;
23
+ for (const branch of this.#entries.values())
24
+ size += branch.size;
25
+ return size;
26
+ }
27
+ /** Whether this exact MLS message is recorded against `stateTag`. */
28
+ has(stateTag, message) {
29
+ return this.#entries.get(stateTag)?.has(message) ?? false;
30
+ }
31
+ /** Retained transport envelopes used as witnesses by later fork passes. */
32
+ envelopes() {
33
+ const envelopes = [];
34
+ for (const branch of this.#entries.values())
35
+ for (const entry of branch.values())
36
+ envelopes.push(entry.envelope);
37
+ return envelopes;
38
+ }
39
+ /** Remembers a delivered application payload. */
40
+ record(entry) {
41
+ let branch = this.#entries.get(entry.stateTag);
42
+ if (!branch) {
43
+ branch = new Map();
44
+ this.#entries.set(entry.stateTag, branch);
45
+ }
46
+ if (!branch.has(entry.message))
47
+ branch.set(entry.message, entry);
48
+ }
49
+ /**
50
+ * Removes and returns the payloads abandoned by a rewind to a canonical
51
+ * branch: those delivered strictly after `forkEpoch` whose delivery state is
52
+ * not on the canonical chain (`canonicalTags`). Payloads that decrypted on
53
+ * the canonical branch, and any at or below the fork epoch (shared history),
54
+ * are retained.
55
+ */
56
+ invalidatedByRewind(forkEpoch, canonicalTags) {
57
+ const invalidated = [];
58
+ for (const [stateTag, branch] of this.#entries) {
59
+ for (const [message, entry] of branch) {
60
+ if (entry.epoch > forkEpoch && !canonicalTags.has(entry.stateTag)) {
61
+ invalidated.push(entry);
62
+ branch.delete(message);
63
+ }
64
+ }
65
+ if (branch.size === 0)
66
+ this.#entries.delete(stateTag);
67
+ }
68
+ return invalidated;
69
+ }
70
+ /**
71
+ * Drops entries below the caller's tree-aware correctness horizon. The
72
+ * engine supplies `min(retained anchor, oldest tree-node epoch)` so no
73
+ * payload still nameable by a fork candidate loses its retraction record.
74
+ */
75
+ pruneBelow(epoch) {
76
+ for (const [stateTag, branch] of this.#entries) {
77
+ for (const [message, entry] of branch)
78
+ if (entry.epoch < epoch)
79
+ branch.delete(message);
80
+ if (branch.size === 0)
81
+ this.#entries.delete(stateTag);
82
+ }
83
+ }
84
+ }
@@ -0,0 +1,41 @@
1
+ /** @module @category Engine */
2
+ export type DisbandFailureReason = "NoLongerMember" | "NoLongerAdmin";
3
+ export type DisbandRequest = {
4
+ status: "pending";
5
+ requestedAtMs: number;
6
+ lastPreparedEpoch: number | null;
7
+ } | {
8
+ status: "failed";
9
+ reason: DisbandFailureReason;
10
+ requestedAtMs: number;
11
+ lastPreparedEpoch: null;
12
+ };
13
+ export declare function disbandRequestKey(groupIdHex: string): string;
14
+ export interface StoredDisbandConvergence {
15
+ readonly generation: number;
16
+ readonly baseEpoch: number;
17
+ readonly openedAtWallMs: number;
18
+ readonly deadlineWallMs: number;
19
+ readonly lastRelevantInputWallMs: number;
20
+ readonly candidates: readonly {
21
+ readonly commitDigest: string;
22
+ readonly actorPubkey: string;
23
+ readonly sourceEpoch: number;
24
+ readonly parentTag: string;
25
+ readonly childTag: string;
26
+ readonly commitMessage: string;
27
+ readonly resultingState: string;
28
+ /**
29
+ * The commit's authenticated member sender at the parent (seam,
30
+ * epic-marmot-rewind-takeover-stale-leaf-snapshot AC-FIX-9), whatever its
31
+ * wire framing. Absent on a record written before it was stored, and for
32
+ * a non-member sender.
33
+ */
34
+ readonly senderLeafIndex?: number;
35
+ }[];
36
+ }
37
+ export declare function disbandConvergenceKey(groupIdHex: string): string;
38
+ export declare function encodeDisbandConvergence(record: StoredDisbandConvergence): Uint8Array;
39
+ export declare function decodeDisbandConvergence(data: Uint8Array): StoredDisbandConvergence;
40
+ export declare function encodeDisbandRequest(request: DisbandRequest): Uint8Array;
41
+ export declare function decodeDisbandRequest(data: Uint8Array): DisbandRequest;
@@ -0,0 +1,86 @@
1
+ /** @module @category Engine */
2
+ export function disbandRequestKey(groupIdHex) {
3
+ return `${groupIdHex}/disband/request`;
4
+ }
5
+ export function disbandConvergenceKey(groupIdHex) {
6
+ return `${groupIdHex}/disband/convergence`;
7
+ }
8
+ export function encodeDisbandConvergence(record) {
9
+ return new TextEncoder().encode(JSON.stringify({ version: 1, ...record }));
10
+ }
11
+ export function decodeDisbandConvergence(data) {
12
+ let value;
13
+ try {
14
+ value = JSON.parse(new TextDecoder().decode(data));
15
+ }
16
+ catch {
17
+ throw new Error("Invalid disband convergence encoding");
18
+ }
19
+ const record = value;
20
+ if (!record ||
21
+ record.version !== 1 ||
22
+ !Number.isSafeInteger(record.generation) ||
23
+ !Number.isSafeInteger(record.baseEpoch) ||
24
+ typeof record.openedAtWallMs !== "number" ||
25
+ typeof record.deadlineWallMs !== "number" ||
26
+ typeof record.lastRelevantInputWallMs !== "number" ||
27
+ !Array.isArray(record.candidates))
28
+ throw new Error("Invalid disband convergence record");
29
+ for (const candidate of record.candidates) {
30
+ if (!/^[0-9a-f]{64}$/i.test(candidate.commitDigest) ||
31
+ !/^[0-9a-f]{64}$/i.test(candidate.actorPubkey) ||
32
+ !Number.isSafeInteger(candidate.sourceEpoch) ||
33
+ typeof candidate.parentTag !== "string" ||
34
+ typeof candidate.childTag !== "string" ||
35
+ typeof candidate.commitMessage !== "string" ||
36
+ typeof candidate.resultingState !== "string" ||
37
+ !/^[0-9a-f]*$/i.test(candidate.commitMessage) ||
38
+ !/^[0-9a-f]*$/i.test(candidate.resultingState) ||
39
+ (candidate.senderLeafIndex !== undefined &&
40
+ !(Number.isSafeInteger(candidate.senderLeafIndex) &&
41
+ candidate.senderLeafIndex >= 0)))
42
+ throw new Error("Invalid disband convergence record");
43
+ }
44
+ return record;
45
+ }
46
+ export function encodeDisbandRequest(request) {
47
+ return new TextEncoder().encode(JSON.stringify({ version: 1, ...request }));
48
+ }
49
+ export function decodeDisbandRequest(data) {
50
+ let value;
51
+ try {
52
+ value = JSON.parse(new TextDecoder().decode(data));
53
+ }
54
+ catch {
55
+ throw new Error("Invalid disband request encoding");
56
+ }
57
+ if (!value || typeof value !== "object")
58
+ throw new Error("Invalid disband request record");
59
+ const record = value;
60
+ if (record.version !== 1 ||
61
+ typeof record.requestedAtMs !== "number" ||
62
+ !Number.isFinite(record.requestedAtMs) ||
63
+ (record.lastPreparedEpoch !== null &&
64
+ (typeof record.lastPreparedEpoch !== "number" ||
65
+ !Number.isSafeInteger(record.lastPreparedEpoch) ||
66
+ record.lastPreparedEpoch < 0)))
67
+ throw new Error("Invalid disband request record");
68
+ if (record.status === "pending") {
69
+ return {
70
+ status: "pending",
71
+ requestedAtMs: record.requestedAtMs,
72
+ lastPreparedEpoch: record.lastPreparedEpoch,
73
+ };
74
+ }
75
+ if (record.status === "failed" &&
76
+ (record.reason === "NoLongerMember" || record.reason === "NoLongerAdmin") &&
77
+ record.lastPreparedEpoch === null) {
78
+ return {
79
+ status: "failed",
80
+ reason: record.reason,
81
+ requestedAtMs: record.requestedAtMs,
82
+ lastPreparedEpoch: null,
83
+ };
84
+ }
85
+ throw new Error("Invalid disband request record");
86
+ }
@@ -0,0 +1,14 @@
1
+ export type DisbandNotificationState = "pending" | "delivered";
2
+ /** Authenticated, read-only evidence retained after live MLS state is erased. */
3
+ export interface DisbandTombstone {
4
+ readonly groupId: Uint8Array;
5
+ readonly selectedEpoch: number;
6
+ readonly commitDigest: Uint8Array;
7
+ readonly actorPubkey: string;
8
+ readonly notificationState: DisbandNotificationState;
9
+ }
10
+ export declare function disbandTombstoneKey(groupIdHex: string): string;
11
+ /** Non-secret, scrubbed MLS shell used only to construct the terminal facade. */
12
+ export declare function disbandRegistryStateKey(groupIdHex: string): string;
13
+ export declare function encodeDisbandTombstone(tombstone: DisbandTombstone): Uint8Array;
14
+ export declare function decodeDisbandTombstone(data: Uint8Array): DisbandTombstone;
@@ -0,0 +1,66 @@
1
+ /** @module @category Engine */
2
+ import { bytesToHex, hexToBytes } from "@noble/hashes/utils.js";
3
+ export function disbandTombstoneKey(groupIdHex) {
4
+ return `${groupIdHex}/disband/terminal`;
5
+ }
6
+ /** Non-secret, scrubbed MLS shell used only to construct the terminal facade. */
7
+ export function disbandRegistryStateKey(groupIdHex) {
8
+ return `${groupIdHex}/disband/registry-state`;
9
+ }
10
+ export function encodeDisbandTombstone(tombstone) {
11
+ validateTombstone(tombstone);
12
+ const stored = {
13
+ version: 1,
14
+ groupId: bytesToHex(tombstone.groupId),
15
+ selectedEpoch: tombstone.selectedEpoch,
16
+ commitDigest: bytesToHex(tombstone.commitDigest),
17
+ actorPubkey: tombstone.actorPubkey,
18
+ notificationState: tombstone.notificationState,
19
+ };
20
+ return new TextEncoder().encode(JSON.stringify(stored));
21
+ }
22
+ export function decodeDisbandTombstone(data) {
23
+ let value;
24
+ try {
25
+ value = JSON.parse(new TextDecoder().decode(data));
26
+ }
27
+ catch {
28
+ throw new Error("Invalid disband tombstone encoding");
29
+ }
30
+ if (!value || typeof value !== "object")
31
+ throw new Error("Invalid disband tombstone record");
32
+ const record = value;
33
+ if (record.version !== 1 ||
34
+ typeof record.groupId !== "string" ||
35
+ typeof record.selectedEpoch !== "number" ||
36
+ typeof record.commitDigest !== "string" ||
37
+ typeof record.actorPubkey !== "string" ||
38
+ (record.notificationState !== "pending" &&
39
+ record.notificationState !== "delivered"))
40
+ throw new Error("Invalid disband tombstone record");
41
+ let tombstone;
42
+ try {
43
+ tombstone = {
44
+ groupId: hexToBytes(record.groupId),
45
+ selectedEpoch: record.selectedEpoch,
46
+ commitDigest: hexToBytes(record.commitDigest),
47
+ actorPubkey: record.actorPubkey,
48
+ notificationState: record.notificationState,
49
+ };
50
+ }
51
+ catch {
52
+ throw new Error("Invalid disband tombstone record");
53
+ }
54
+ validateTombstone(tombstone);
55
+ return tombstone;
56
+ }
57
+ function validateTombstone(tombstone) {
58
+ if (tombstone.groupId.length === 0 ||
59
+ !Number.isSafeInteger(tombstone.selectedEpoch) ||
60
+ tombstone.selectedEpoch < 0 ||
61
+ tombstone.commitDigest.length !== 32 ||
62
+ !/^[0-9a-f]{64}$/i.test(tombstone.actorPubkey) ||
63
+ (tombstone.notificationState !== "pending" &&
64
+ tombstone.notificationState !== "delivered"))
65
+ throw new Error("Invalid disband tombstone record");
66
+ }