@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,44 @@
1
+ /** @module @category Core - App Components */
2
+ import { isLoopbackHost, rejectNonRoutableHost } from "./host-safety.js";
3
+ /**
4
+ * Validates and normalizes an `https` (optionally loopback-`http`) URL the way
5
+ * the darkmatter `validate_and_normalize_*` helpers do: no credentials, no
6
+ * fragment, a routable host, and length bounds. Returns the WHATWG-normalized
7
+ * URL. Both this and the Rust `url` crate implement the WHATWG URL Standard, so
8
+ * normalized output matches across implementations for ordinary URLs.
9
+ *
10
+ * Note (darkmatter parity): query strings are accepted and preserved (#374 —
11
+ * rejecting them forked commit acceptance), and the WHATWG trailing `/` is
12
+ * kept, never stripped (the serializer's output is the stored form).
13
+ */
14
+ export function validateAndNormalizeHttpsUrl(raw, opts) {
15
+ const { label, maxLen } = opts;
16
+ const trimmed = opts.trimInput ? raw.trim() : raw;
17
+ if (trimmed.length === 0)
18
+ throw new Error(`${label} must not be empty`);
19
+ if (utf8Len(trimmed) > maxLen)
20
+ throw new Error(`${label} exceeds ${maxLen} bytes`);
21
+ if (!URL.canParse(trimmed))
22
+ throw new Error(`${label} is invalid`);
23
+ const url = new URL(trimmed);
24
+ if (url.username !== "" || url.password !== "")
25
+ throw new Error(`${label} must not include credentials`);
26
+ if (url.hash !== "")
27
+ throw new Error(`${label} must not include a fragment`);
28
+ const scheme = url.protocol.replace(/:$/, "");
29
+ const isLoopbackHttp = scheme === "http" && opts.allowLoopbackHttp && isLoopbackHost(url.hostname);
30
+ if (scheme === "https") {
31
+ rejectNonRoutableHost(url.hostname, label);
32
+ }
33
+ else if (!isLoopbackHttp) {
34
+ throw new Error(`${label} scheme must be https`);
35
+ }
36
+ // WHATWG parse-and-serialize is the stored form; keep the trailing `/`.
37
+ const normalized = url.toString();
38
+ if (utf8Len(normalized) > maxLen)
39
+ throw new Error(`${label} exceeds ${maxLen} bytes`);
40
+ return normalized;
41
+ }
42
+ function utf8Len(s) {
43
+ return new TextEncoder().encode(s).length;
44
+ }
@@ -0,0 +1,84 @@
1
+ /** @module @category Core - Convergence */
2
+ import { type GroupLifecycleState } from "./group-lifecycle.js";
3
+ /**
4
+ * Convergence status (Marmot v2 `protocol-core/group-state.md` §Convergence
5
+ * status) — a *derived* view of how convergence is progressing within the
6
+ * authoritative lifecycle state. It is computed from stored input and policy; it
7
+ * is never stored state and never a claim made by the transport.
8
+ *
9
+ * Ported from darkmatter `cgka-engine/src/canonicalization.rs`
10
+ * (`ConvergenceStatus`, `convergence_status_for_result`). The lifecycle state
11
+ * ({@link GroupLifecycleState}) stays authoritative; this is the settle-window
12
+ * spine layered on top that gates when queued outbound work may be released.
13
+ */
14
+ export declare const convergenceStatuses: {
15
+ /** Convergence-relevant input is still arriving, or the quiescence window has not elapsed. */
16
+ readonly syncing: "Syncing";
17
+ /**
18
+ * The quiescence window elapsed, but unresolved convergence work remains —
19
+ * e.g. a child commit whose parent has not been retained or fetched yet.
20
+ */
21
+ readonly resolving: "Resolving";
22
+ /** Candidate processing reached a fixed point and the selected branch, if any, was applied. */
23
+ readonly settled: "Settled";
24
+ /** Candidate processing cannot safely continue without a repair path or missing retained material. */
25
+ readonly blocked: "Blocked";
26
+ };
27
+ /** A convergence status value. */
28
+ export type ConvergenceStatus = (typeof convergenceStatuses)[keyof typeof convergenceStatuses];
29
+ /** The inputs from which {@link deriveConvergenceStatus} computes the status. */
30
+ export interface ConvergenceStatusInput {
31
+ /** Current wall-clock time (ms). */
32
+ nowMs: number;
33
+ /**
34
+ * Timestamp (ms) of the most recent convergence-relevant input (a commit, or
35
+ * fork material entering recovery). Non-convergence traffic (application
36
+ * messages, lone proposals) MUST NOT advance this.
37
+ */
38
+ lastConvergenceRelevantInputMs: number;
39
+ /** The quiescence window (`settlementQuiescenceMs` from the convergence policy). */
40
+ settlementQuiescenceMs: number;
41
+ /**
42
+ * Whether the last convergence pass left a non-proposal input message without
43
+ * a disposition (e.g. a child commit whose parent has not been materialized).
44
+ * A lone uncommitted proposal does NOT count — commits are the consensus log
45
+ * and a proposal only takes effect once a commit consumes it
46
+ * (`convergence.md`), so an un-dispositioned proposal must not pin the pass in
47
+ * `Resolving` (darkmatter#154).
48
+ */
49
+ hasUnresolvedInput: boolean;
50
+ /**
51
+ * Whether convergence hit a blocking error that retained material cannot clear
52
+ * — the `MissingRetainedAnchor` condition (`has_blocking_convergence_error`).
53
+ */
54
+ hasBlockingError: boolean;
55
+ }
56
+ /**
57
+ * Derives the {@link ConvergenceStatus} for a convergence pass, mirroring
58
+ * darkmatter `convergence_status_for_result`. The order is load-bearing: the
59
+ * quiescence window gates first (`Syncing`), then unresolved input
60
+ * (`Resolving`) takes precedence over a blocking error (`Blocked`), and a clean
61
+ * fixed point is `Settled`.
62
+ */
63
+ export declare function deriveConvergenceStatus(input: ConvergenceStatusInput): ConvergenceStatus;
64
+ /**
65
+ * Whether `status` may legally appear in `lifecycle` (`group-state.md` table).
66
+ * `PendingPublish` and `Merging` are local-publish states, not convergence
67
+ * passes, so no convergence status is meaningful in them — this returns `false`.
68
+ */
69
+ export declare function isConvergenceStatusLegal(status: ConvergenceStatus, lifecycle: GroupLifecycleState): boolean;
70
+ /**
71
+ * Whether a client SHOULD queue a local outbound intent rather than preparing it
72
+ * now (`group-state.md` §Local actions during convergence). Outbound work is
73
+ * released only once convergence is `Settled`; `Syncing`, `Resolving`, and
74
+ * `Blocked` all queue, because a state that MAY lose branch selection or require
75
+ * repair must not have payloads encrypted or commits staged against it.
76
+ */
77
+ export declare function shouldQueueOutbound(status: ConvergenceStatus): boolean;
78
+ /**
79
+ * Whether queued outbound work may be released and prepared now: convergence is
80
+ * `Settled` AND the lifecycle allows outbound work. Per the legal-combination
81
+ * table `Settled` only appears in `Stable`, so this is `Settled` + `Stable`;
82
+ * both are checked so a caller passing an inconsistent pair fails closed.
83
+ */
84
+ export declare function mayReleaseOutbound(status: ConvergenceStatus, lifecycle: GroupLifecycleState): boolean;
@@ -0,0 +1,82 @@
1
+ /** @module @category Core - Convergence */
2
+ import { groupLifecycleStates, } from "./group-lifecycle.js";
3
+ /**
4
+ * Convergence status (Marmot v2 `protocol-core/group-state.md` §Convergence
5
+ * status) — a *derived* view of how convergence is progressing within the
6
+ * authoritative lifecycle state. It is computed from stored input and policy; it
7
+ * is never stored state and never a claim made by the transport.
8
+ *
9
+ * Ported from darkmatter `cgka-engine/src/canonicalization.rs`
10
+ * (`ConvergenceStatus`, `convergence_status_for_result`). The lifecycle state
11
+ * ({@link GroupLifecycleState}) stays authoritative; this is the settle-window
12
+ * spine layered on top that gates when queued outbound work may be released.
13
+ */
14
+ export const convergenceStatuses = {
15
+ /** Convergence-relevant input is still arriving, or the quiescence window has not elapsed. */
16
+ syncing: "Syncing",
17
+ /**
18
+ * The quiescence window elapsed, but unresolved convergence work remains —
19
+ * e.g. a child commit whose parent has not been retained or fetched yet.
20
+ */
21
+ resolving: "Resolving",
22
+ /** Candidate processing reached a fixed point and the selected branch, if any, was applied. */
23
+ settled: "Settled",
24
+ /** Candidate processing cannot safely continue without a repair path or missing retained material. */
25
+ blocked: "Blocked",
26
+ };
27
+ /**
28
+ * Derives the {@link ConvergenceStatus} for a convergence pass, mirroring
29
+ * darkmatter `convergence_status_for_result`. The order is load-bearing: the
30
+ * quiescence window gates first (`Syncing`), then unresolved input
31
+ * (`Resolving`) takes precedence over a blocking error (`Blocked`), and a clean
32
+ * fixed point is `Settled`.
33
+ */
34
+ export function deriveConvergenceStatus(input) {
35
+ const elapsed = Math.max(0, input.nowMs - input.lastConvergenceRelevantInputMs);
36
+ if (elapsed < input.settlementQuiescenceMs)
37
+ return convergenceStatuses.syncing;
38
+ if (input.hasUnresolvedInput)
39
+ return convergenceStatuses.resolving;
40
+ if (input.hasBlockingError)
41
+ return convergenceStatuses.blocked;
42
+ return convergenceStatuses.settled;
43
+ }
44
+ const S = groupLifecycleStates;
45
+ /**
46
+ * The legal (convergence status → lifecycle state) combinations from
47
+ * `group-state.md`. The lifecycle state is authoritative; a status that does
48
+ * not pair with the current lifecycle indicates a coordination bug.
49
+ */
50
+ const LEGAL_COMBINATIONS = {
51
+ [convergenceStatuses.syncing]: [S.stable, S.recovering],
52
+ [convergenceStatuses.resolving]: [S.stable, S.recovering],
53
+ [convergenceStatuses.settled]: [S.stable],
54
+ [convergenceStatuses.blocked]: [S.recovering, S.unrecoverable],
55
+ };
56
+ /**
57
+ * Whether `status` may legally appear in `lifecycle` (`group-state.md` table).
58
+ * `PendingPublish` and `Merging` are local-publish states, not convergence
59
+ * passes, so no convergence status is meaningful in them — this returns `false`.
60
+ */
61
+ export function isConvergenceStatusLegal(status, lifecycle) {
62
+ return LEGAL_COMBINATIONS[status].includes(lifecycle);
63
+ }
64
+ /**
65
+ * Whether a client SHOULD queue a local outbound intent rather than preparing it
66
+ * now (`group-state.md` §Local actions during convergence). Outbound work is
67
+ * released only once convergence is `Settled`; `Syncing`, `Resolving`, and
68
+ * `Blocked` all queue, because a state that MAY lose branch selection or require
69
+ * repair must not have payloads encrypted or commits staged against it.
70
+ */
71
+ export function shouldQueueOutbound(status) {
72
+ return status !== convergenceStatuses.settled;
73
+ }
74
+ /**
75
+ * Whether queued outbound work may be released and prepared now: convergence is
76
+ * `Settled` AND the lifecycle allows outbound work. Per the legal-combination
77
+ * table `Settled` only appears in `Stable`, so this is `Settled` + `Stable`;
78
+ * both are checked so a caller passing an inconsistent pair fails closed.
79
+ */
80
+ export function mayReleaseOutbound(status, lifecycle) {
81
+ return status === convergenceStatuses.settled && lifecycle === S.stable;
82
+ }