@gakr-gakr/matrix 0.1.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 (205) hide show
  1. package/CHANGELOG.md +285 -0
  2. package/SPEC-SUPPORT.md +116 -0
  3. package/api.ts +38 -0
  4. package/auth-presence.ts +56 -0
  5. package/autobot.plugin.json +28 -0
  6. package/channel-plugin-api.ts +3 -0
  7. package/cli-metadata.ts +11 -0
  8. package/contract-api.ts +17 -0
  9. package/doctor-contract-api.ts +1 -0
  10. package/helper-api.ts +3 -0
  11. package/index.ts +55 -0
  12. package/package.json +101 -0
  13. package/plugin-entry.handlers.runtime.ts +1 -0
  14. package/runtime-api.ts +72 -0
  15. package/runtime-heavy-api.ts +1 -0
  16. package/runtime-setter-api.ts +3 -0
  17. package/secret-contract-api.ts +5 -0
  18. package/setup-entry.ts +17 -0
  19. package/setup-plugin-api.ts +3 -0
  20. package/src/account-selection.ts +223 -0
  21. package/src/actions.ts +346 -0
  22. package/src/approval-auth.ts +25 -0
  23. package/src/approval-handler.runtime.ts +595 -0
  24. package/src/approval-ids.ts +6 -0
  25. package/src/approval-native.ts +348 -0
  26. package/src/approval-reaction-auth.ts +45 -0
  27. package/src/approval-reactions.ts +313 -0
  28. package/src/auth-precedence.ts +61 -0
  29. package/src/channel-account-paths.ts +97 -0
  30. package/src/channel.runtime.ts +17 -0
  31. package/src/channel.setup.ts +48 -0
  32. package/src/channel.ts +667 -0
  33. package/src/cli-metadata.ts +19 -0
  34. package/src/cli.ts +2298 -0
  35. package/src/config-adapter.ts +41 -0
  36. package/src/config-schema.ts +159 -0
  37. package/src/config-ui-hints.ts +56 -0
  38. package/src/directory-live.ts +238 -0
  39. package/src/doctor-contract.ts +287 -0
  40. package/src/doctor.ts +262 -0
  41. package/src/env-vars.ts +92 -0
  42. package/src/exec-approval-resolver.ts +23 -0
  43. package/src/exec-approvals.ts +293 -0
  44. package/src/group-mentions.ts +41 -0
  45. package/src/legacy-crypto-inspector-availability.ts +60 -0
  46. package/src/legacy-crypto.ts +531 -0
  47. package/src/legacy-state.ts +156 -0
  48. package/src/matrix/account-config.ts +175 -0
  49. package/src/matrix/accounts.ts +194 -0
  50. package/src/matrix/actions/client.ts +31 -0
  51. package/src/matrix/actions/devices.ts +34 -0
  52. package/src/matrix/actions/limits.ts +6 -0
  53. package/src/matrix/actions/messages.ts +129 -0
  54. package/src/matrix/actions/pins.ts +63 -0
  55. package/src/matrix/actions/polls.ts +109 -0
  56. package/src/matrix/actions/profile.ts +37 -0
  57. package/src/matrix/actions/reactions.ts +59 -0
  58. package/src/matrix/actions/room.ts +71 -0
  59. package/src/matrix/actions/summary.ts +88 -0
  60. package/src/matrix/actions/types.ts +63 -0
  61. package/src/matrix/actions/verification.ts +589 -0
  62. package/src/matrix/actions.ts +37 -0
  63. package/src/matrix/active-client.ts +26 -0
  64. package/src/matrix/async-lock.ts +18 -0
  65. package/src/matrix/backup-health.ts +124 -0
  66. package/src/matrix/client/config-runtime-api.ts +9 -0
  67. package/src/matrix/client/config-secret-input.runtime.ts +1 -0
  68. package/src/matrix/client/config.ts +853 -0
  69. package/src/matrix/client/create-client.ts +105 -0
  70. package/src/matrix/client/env-auth.ts +95 -0
  71. package/src/matrix/client/file-sync-store.ts +289 -0
  72. package/src/matrix/client/logging.ts +140 -0
  73. package/src/matrix/client/migration-snapshot.runtime.ts +1 -0
  74. package/src/matrix/client/private-network-host.ts +1 -0
  75. package/src/matrix/client/runtime.ts +4 -0
  76. package/src/matrix/client/shared.ts +316 -0
  77. package/src/matrix/client/storage.ts +543 -0
  78. package/src/matrix/client/types.ts +50 -0
  79. package/src/matrix/client/url-validation.ts +76 -0
  80. package/src/matrix/client-bootstrap.ts +173 -0
  81. package/src/matrix/client.ts +23 -0
  82. package/src/matrix/config-paths.ts +31 -0
  83. package/src/matrix/config-update.ts +292 -0
  84. package/src/matrix/credentials-read.ts +207 -0
  85. package/src/matrix/credentials-write.runtime.ts +35 -0
  86. package/src/matrix/credentials.ts +95 -0
  87. package/src/matrix/deps.ts +309 -0
  88. package/src/matrix/device-health.ts +31 -0
  89. package/src/matrix/direct-management.ts +349 -0
  90. package/src/matrix/direct-room.ts +128 -0
  91. package/src/matrix/draft-stream.ts +225 -0
  92. package/src/matrix/encryption-guidance.ts +24 -0
  93. package/src/matrix/errors.ts +21 -0
  94. package/src/matrix/format.ts +426 -0
  95. package/src/matrix/legacy-crypto-inspector.ts +95 -0
  96. package/src/matrix/media-errors.ts +20 -0
  97. package/src/matrix/media-text.ts +162 -0
  98. package/src/matrix/monitor/access-state.ts +145 -0
  99. package/src/matrix/monitor/ack-config.ts +27 -0
  100. package/src/matrix/monitor/allowlist.ts +92 -0
  101. package/src/matrix/monitor/auto-join.ts +86 -0
  102. package/src/matrix/monitor/config.ts +569 -0
  103. package/src/matrix/monitor/context-summary.ts +43 -0
  104. package/src/matrix/monitor/direct.ts +296 -0
  105. package/src/matrix/monitor/events.ts +397 -0
  106. package/src/matrix/monitor/handler.ts +2271 -0
  107. package/src/matrix/monitor/inbound-dedupe.ts +267 -0
  108. package/src/matrix/monitor/index.ts +540 -0
  109. package/src/matrix/monitor/legacy-crypto-restore.ts +139 -0
  110. package/src/matrix/monitor/location.ts +108 -0
  111. package/src/matrix/monitor/media.ts +119 -0
  112. package/src/matrix/monitor/mentions.ts +256 -0
  113. package/src/matrix/monitor/reaction-events.ts +197 -0
  114. package/src/matrix/monitor/recent-invite.ts +30 -0
  115. package/src/matrix/monitor/replies.ts +136 -0
  116. package/src/matrix/monitor/reply-context.ts +92 -0
  117. package/src/matrix/monitor/room-history.ts +301 -0
  118. package/src/matrix/monitor/room-info.ts +126 -0
  119. package/src/matrix/monitor/rooms.ts +52 -0
  120. package/src/matrix/monitor/route.ts +179 -0
  121. package/src/matrix/monitor/runtime-api.ts +28 -0
  122. package/src/matrix/monitor/startup-verification.ts +237 -0
  123. package/src/matrix/monitor/startup.ts +218 -0
  124. package/src/matrix/monitor/status.ts +120 -0
  125. package/src/matrix/monitor/sync-lifecycle.ts +91 -0
  126. package/src/matrix/monitor/task-runner.ts +38 -0
  127. package/src/matrix/monitor/test-events.ts +21 -0
  128. package/src/matrix/monitor/thread-context.ts +108 -0
  129. package/src/matrix/monitor/threads.ts +85 -0
  130. package/src/matrix/monitor/types.ts +30 -0
  131. package/src/matrix/monitor/verification-events.ts +643 -0
  132. package/src/matrix/monitor/verification-utils.ts +46 -0
  133. package/src/matrix/outbound-media-runtime.ts +1 -0
  134. package/src/matrix/poll-summary.ts +110 -0
  135. package/src/matrix/poll-types.ts +429 -0
  136. package/src/matrix/probe.runtime.ts +4 -0
  137. package/src/matrix/probe.ts +97 -0
  138. package/src/matrix/profile.ts +184 -0
  139. package/src/matrix/reaction-common.ts +147 -0
  140. package/src/matrix/sdk/crypto-bootstrap.ts +438 -0
  141. package/src/matrix/sdk/crypto-facade.ts +242 -0
  142. package/src/matrix/sdk/crypto-node.runtime.ts +17 -0
  143. package/src/matrix/sdk/crypto-runtime.ts +14 -0
  144. package/src/matrix/sdk/decrypt-bridge.ts +410 -0
  145. package/src/matrix/sdk/event-helpers.ts +83 -0
  146. package/src/matrix/sdk/http-client.ts +87 -0
  147. package/src/matrix/sdk/idb-persistence-lock.ts +51 -0
  148. package/src/matrix/sdk/idb-persistence.ts +286 -0
  149. package/src/matrix/sdk/logger.ts +108 -0
  150. package/src/matrix/sdk/read-response-with-limit.ts +19 -0
  151. package/src/matrix/sdk/recovery-key-store.ts +453 -0
  152. package/src/matrix/sdk/timeout-abort-signal.ts +1 -0
  153. package/src/matrix/sdk/transport-runtime-api.ts +18 -0
  154. package/src/matrix/sdk/transport.ts +352 -0
  155. package/src/matrix/sdk/types.ts +245 -0
  156. package/src/matrix/sdk/verification-manager.ts +795 -0
  157. package/src/matrix/sdk/verification-status.ts +23 -0
  158. package/src/matrix/sdk.ts +2152 -0
  159. package/src/matrix/send/client.ts +93 -0
  160. package/src/matrix/send/formatting.ts +189 -0
  161. package/src/matrix/send/media.ts +244 -0
  162. package/src/matrix/send/targets.ts +104 -0
  163. package/src/matrix/send/types.ts +131 -0
  164. package/src/matrix/send.ts +660 -0
  165. package/src/matrix/session-store-metadata.ts +108 -0
  166. package/src/matrix/startup-abort.ts +44 -0
  167. package/src/matrix/subagent-hooks.ts +308 -0
  168. package/src/matrix/sync-state.ts +27 -0
  169. package/src/matrix/target-ids.ts +79 -0
  170. package/src/matrix/thread-bindings-shared.ts +206 -0
  171. package/src/matrix/thread-bindings.ts +580 -0
  172. package/src/matrix-migration.runtime.ts +9 -0
  173. package/src/migration-config.ts +243 -0
  174. package/src/migration-snapshot-backup.ts +116 -0
  175. package/src/migration-snapshot.ts +53 -0
  176. package/src/onboarding.ts +775 -0
  177. package/src/outbound.ts +248 -0
  178. package/src/plugin-entry.runtime.js +115 -0
  179. package/src/plugin-entry.runtime.ts +70 -0
  180. package/src/profile-update.ts +71 -0
  181. package/src/record-shared.ts +3 -0
  182. package/src/resolve-targets.ts +175 -0
  183. package/src/resolver.runtime.ts +5 -0
  184. package/src/resolver.ts +21 -0
  185. package/src/runtime-api.ts +106 -0
  186. package/src/runtime.ts +13 -0
  187. package/src/secret-contract.ts +174 -0
  188. package/src/session-route.ts +126 -0
  189. package/src/setup-bootstrap.ts +102 -0
  190. package/src/setup-config.ts +222 -0
  191. package/src/setup-contract.ts +90 -0
  192. package/src/setup-core.ts +146 -0
  193. package/src/setup-dm-policy.ts +15 -0
  194. package/src/setup-surface.ts +4 -0
  195. package/src/startup-maintenance.ts +114 -0
  196. package/src/storage-paths.ts +92 -0
  197. package/src/thread-binding-api.ts +23 -0
  198. package/src/tool-actions.runtime.ts +1 -0
  199. package/src/tool-actions.ts +498 -0
  200. package/src/types.ts +257 -0
  201. package/subagent-hooks-api.ts +31 -0
  202. package/test-api.ts +21 -0
  203. package/thread-binding-api.ts +4 -0
  204. package/thread-bindings-runtime.ts +4 -0
  205. package/tsconfig.json +16 -0
@@ -0,0 +1,14 @@
1
+ import "fake-indexeddb/auto";
2
+
3
+ export { MatrixCryptoBootstrapper } from "./crypto-bootstrap.js";
4
+ export type { MatrixCryptoBootstrapResult } from "./crypto-bootstrap.js";
5
+ export { createMatrixCryptoFacade } from "./crypto-facade.js";
6
+ export type { MatrixCryptoFacade } from "./crypto-facade.js";
7
+ export { MatrixDecryptBridge } from "./decrypt-bridge.js";
8
+ export { persistIdbToDisk, restoreIdbFromDisk } from "./idb-persistence.js";
9
+ export { MatrixVerificationManager } from "./verification-manager.js";
10
+ export type { MatrixVerificationSummary } from "./verification-manager.js";
11
+ export {
12
+ isMatrixDeviceOwnerVerified,
13
+ isMatrixDeviceVerifiedInCurrentClient,
14
+ } from "./verification-status.js";
@@ -0,0 +1,410 @@
1
+ import { CryptoEvent } from "matrix-js-sdk/lib/crypto-api/CryptoEvent.js";
2
+ import { DecryptionFailureCode } from "matrix-js-sdk/lib/crypto-api/index.js";
3
+ import { MatrixEventEvent, type MatrixEvent } from "matrix-js-sdk/lib/matrix.js";
4
+ import { LogService, noop } from "./logger.js";
5
+
6
+ type MatrixDecryptIfNeededClient = {
7
+ decryptEventIfNeeded?: (
8
+ event: MatrixEvent,
9
+ opts?: {
10
+ isRetry?: boolean;
11
+ },
12
+ ) => Promise<void>;
13
+ };
14
+
15
+ type MatrixDecryptRetryState = {
16
+ event: MatrixEvent;
17
+ roomId: string;
18
+ eventId: string;
19
+ attempts: number;
20
+ inFlight: boolean;
21
+ timer: ReturnType<typeof setTimeout> | null;
22
+ };
23
+
24
+ type DecryptBridgeRawEvent = {
25
+ event_id: string;
26
+ };
27
+
28
+ type MatrixCryptoRetrySignalSource = {
29
+ on: (eventName: string, listener: (...args: unknown[]) => void) => void;
30
+ };
31
+
32
+ const MATRIX_DECRYPT_RETRY_BASE_DELAY_MS = 1_500;
33
+ const MATRIX_DECRYPT_RETRY_MAX_DELAY_MS = 30_000;
34
+ const MATRIX_DECRYPT_RETRY_MAX_ATTEMPTS = 8;
35
+
36
+ function resolveDecryptRetryKey(roomId: string, eventId: string): string | null {
37
+ if (!roomId || !eventId) {
38
+ return null;
39
+ }
40
+ return `${roomId}|${eventId}`;
41
+ }
42
+
43
+ function isDecryptionFailure(event: MatrixEvent): boolean {
44
+ return (
45
+ typeof (event as { isDecryptionFailure?: () => boolean }).isDecryptionFailure === "function" &&
46
+ (event as { isDecryptionFailure: () => boolean }).isDecryptionFailure()
47
+ );
48
+ }
49
+
50
+ function getDecryptionFailureReason(event: MatrixEvent): DecryptionFailureCode | null {
51
+ const reason = (event as { decryptionFailureReason?: unknown }).decryptionFailureReason;
52
+ return typeof reason === "string" && reason in DecryptionFailureCode
53
+ ? (reason as DecryptionFailureCode)
54
+ : null;
55
+ }
56
+
57
+ function shouldRetryDecryptionFailure(event: MatrixEvent): boolean {
58
+ if (!isDecryptionFailure(event)) {
59
+ return false;
60
+ }
61
+ const reason = getDecryptionFailureReason(event);
62
+ if (!reason) {
63
+ return true;
64
+ }
65
+ return (
66
+ reason === DecryptionFailureCode.MEGOLM_UNKNOWN_INBOUND_SESSION_ID ||
67
+ reason === DecryptionFailureCode.OLM_UNKNOWN_MESSAGE_INDEX ||
68
+ reason === DecryptionFailureCode.UNKNOWN_ERROR
69
+ );
70
+ }
71
+
72
+ export class MatrixDecryptBridge<TRawEvent extends DecryptBridgeRawEvent> {
73
+ private readonly trackedEncryptedEvents = new WeakSet<object>();
74
+ private readonly decryptedMessageDedupe = new Map<string, number>();
75
+ private readonly decryptRetries = new Map<string, MatrixDecryptRetryState>();
76
+ private readonly failedDecryptionsNotified = new Set<string>();
77
+ private readonly exhaustedDecryptRetries = new Set<string>();
78
+ private activeRetryRuns = 0;
79
+ private readonly retryIdleResolvers = new Set<() => void>();
80
+ private cryptoRetrySignalsBound = false;
81
+
82
+ constructor(
83
+ private readonly deps: {
84
+ client: MatrixDecryptIfNeededClient;
85
+ toRaw: (event: MatrixEvent) => TRawEvent;
86
+ emitDecryptedEvent: (roomId: string, event: TRawEvent) => void;
87
+ emitMessage: (roomId: string, event: TRawEvent) => void;
88
+ emitFailedDecryption: (roomId: string, event: TRawEvent, error: Error) => void;
89
+ },
90
+ ) {}
91
+
92
+ shouldEmitUnencryptedMessage(roomId: string, eventId: string): boolean {
93
+ if (!eventId) {
94
+ return true;
95
+ }
96
+ const key = `${roomId}|${eventId}`;
97
+ const createdAt = this.decryptedMessageDedupe.get(key);
98
+ if (createdAt === undefined) {
99
+ return true;
100
+ }
101
+ this.decryptedMessageDedupe.delete(key);
102
+ return false;
103
+ }
104
+
105
+ attachEncryptedEvent(event: MatrixEvent, roomId: string): void {
106
+ if (this.trackedEncryptedEvents.has(event)) {
107
+ return;
108
+ }
109
+ this.trackedEncryptedEvents.add(event);
110
+ event.on(MatrixEventEvent.Decrypted, (decryptedEvent: MatrixEvent, err?: Error) => {
111
+ this.handleEncryptedEventDecrypted({
112
+ roomId,
113
+ encryptedEvent: event,
114
+ decryptedEvent,
115
+ err,
116
+ });
117
+ });
118
+ if (shouldRetryDecryptionFailure(event)) {
119
+ const raw = this.deps.toRaw(event);
120
+ const eventId = raw.event_id || event.getId() || "";
121
+ this.scheduleDecryptRetry({ event, roomId, eventId });
122
+ }
123
+ }
124
+
125
+ retryPendingNow(reason: string): void {
126
+ const pending = Array.from(this.decryptRetries.entries());
127
+ if (pending.length === 0) {
128
+ return;
129
+ }
130
+ LogService.debug("MatrixClientLite", `Retrying pending decryptions due to ${reason}`);
131
+ for (const [retryKey, state] of pending) {
132
+ if (state.timer) {
133
+ clearTimeout(state.timer);
134
+ state.timer = null;
135
+ }
136
+ if (state.inFlight) {
137
+ continue;
138
+ }
139
+ this.runDecryptRetry(retryKey).catch(noop);
140
+ }
141
+ }
142
+
143
+ bindCryptoRetrySignals(crypto: MatrixCryptoRetrySignalSource | undefined): void {
144
+ if (!crypto || this.cryptoRetrySignalsBound) {
145
+ return;
146
+ }
147
+ this.cryptoRetrySignalsBound = true;
148
+
149
+ const trigger = (reason: string): void => {
150
+ this.retryPendingNow(reason);
151
+ };
152
+
153
+ crypto.on(CryptoEvent.KeyBackupDecryptionKeyCached, () => {
154
+ trigger("crypto.keyBackupDecryptionKeyCached");
155
+ });
156
+ crypto.on(CryptoEvent.RehydrationCompleted, () => {
157
+ trigger("dehydration.RehydrationCompleted");
158
+ });
159
+ crypto.on(CryptoEvent.DevicesUpdated, () => {
160
+ trigger("crypto.devicesUpdated");
161
+ });
162
+ crypto.on(CryptoEvent.KeysChanged, () => {
163
+ trigger("crossSigning.keysChanged");
164
+ });
165
+ }
166
+
167
+ stop(): void {
168
+ for (const retryKey of this.decryptRetries.keys()) {
169
+ this.clearDecryptRetry(retryKey);
170
+ }
171
+ }
172
+
173
+ async drainPendingDecryptions(reason: string): Promise<void> {
174
+ for (let attempts = 0; attempts < MATRIX_DECRYPT_RETRY_MAX_ATTEMPTS; attempts += 1) {
175
+ if (this.decryptRetries.size === 0) {
176
+ return;
177
+ }
178
+ this.retryPendingNow(reason);
179
+ await this.waitForActiveRetryRunsToFinish();
180
+ const hasPendingRetryTimers = Array.from(this.decryptRetries.values()).some(
181
+ (state) => state.timer || state.inFlight,
182
+ );
183
+ if (!hasPendingRetryTimers) {
184
+ return;
185
+ }
186
+ }
187
+ }
188
+
189
+ private handleEncryptedEventDecrypted(params: {
190
+ roomId: string;
191
+ encryptedEvent: MatrixEvent;
192
+ decryptedEvent: MatrixEvent;
193
+ err?: Error;
194
+ }): void {
195
+ const decryptedRoomId = params.decryptedEvent.getRoomId() || params.roomId;
196
+ const decryptedRaw = this.deps.toRaw(params.decryptedEvent);
197
+ const retryEventId = decryptedRaw.event_id || params.encryptedEvent.getId() || "";
198
+ const retryKey = resolveDecryptRetryKey(decryptedRoomId, retryEventId);
199
+
200
+ if (params.err) {
201
+ this.emitFailedDecryptionOnce(retryKey, decryptedRoomId, decryptedRaw, params.err);
202
+ if (shouldRetryDecryptionFailure(params.decryptedEvent)) {
203
+ this.scheduleDecryptRetry({
204
+ event: params.encryptedEvent,
205
+ roomId: decryptedRoomId,
206
+ eventId: retryEventId,
207
+ });
208
+ } else if (retryKey) {
209
+ this.clearDecryptRetry(retryKey);
210
+ }
211
+ return;
212
+ }
213
+
214
+ if (isDecryptionFailure(params.decryptedEvent)) {
215
+ this.emitFailedDecryptionOnce(
216
+ retryKey,
217
+ decryptedRoomId,
218
+ decryptedRaw,
219
+ new Error("Matrix event failed to decrypt"),
220
+ );
221
+ if (shouldRetryDecryptionFailure(params.decryptedEvent)) {
222
+ this.scheduleDecryptRetry({
223
+ event: params.encryptedEvent,
224
+ roomId: decryptedRoomId,
225
+ eventId: retryEventId,
226
+ });
227
+ } else if (retryKey) {
228
+ this.clearDecryptRetry(retryKey);
229
+ }
230
+ return;
231
+ }
232
+
233
+ if (retryKey) {
234
+ this.clearDecryptRetry(retryKey);
235
+ }
236
+ this.rememberDecryptedMessage(decryptedRoomId, decryptedRaw.event_id);
237
+ this.deps.emitDecryptedEvent(decryptedRoomId, decryptedRaw);
238
+ this.deps.emitMessage(decryptedRoomId, decryptedRaw);
239
+ }
240
+
241
+ private emitFailedDecryptionOnce(
242
+ retryKey: string | null,
243
+ roomId: string,
244
+ event: TRawEvent,
245
+ error: Error,
246
+ ): void {
247
+ if (retryKey) {
248
+ if (this.failedDecryptionsNotified.has(retryKey)) {
249
+ return;
250
+ }
251
+ this.failedDecryptionsNotified.add(retryKey);
252
+ }
253
+ this.deps.emitFailedDecryption(roomId, event, error);
254
+ }
255
+
256
+ private scheduleDecryptRetry(params: {
257
+ event: MatrixEvent;
258
+ roomId: string;
259
+ eventId: string;
260
+ }): void {
261
+ const retryKey = resolveDecryptRetryKey(params.roomId, params.eventId);
262
+ if (!retryKey) {
263
+ return;
264
+ }
265
+ const existing = this.decryptRetries.get(retryKey);
266
+ if (this.exhaustedDecryptRetries.has(retryKey)) {
267
+ return;
268
+ }
269
+ if (existing?.timer || existing?.inFlight) {
270
+ return;
271
+ }
272
+ const attempts = (existing?.attempts ?? 0) + 1;
273
+ if (attempts > MATRIX_DECRYPT_RETRY_MAX_ATTEMPTS) {
274
+ const retry = this.decryptRetries.get(retryKey);
275
+ if (retry?.timer) {
276
+ clearTimeout(retry.timer);
277
+ }
278
+ this.decryptRetries.delete(retryKey);
279
+ this.exhaustedDecryptRetries.add(retryKey);
280
+ LogService.debug(
281
+ "MatrixClientLite",
282
+ `Giving up decryption retry for ${params.eventId} in ${params.roomId} after ${attempts - 1} attempts`,
283
+ );
284
+ return;
285
+ }
286
+ const delayMs = Math.min(
287
+ MATRIX_DECRYPT_RETRY_BASE_DELAY_MS * 2 ** (attempts - 1),
288
+ MATRIX_DECRYPT_RETRY_MAX_DELAY_MS,
289
+ );
290
+ const next: MatrixDecryptRetryState = {
291
+ event: params.event,
292
+ roomId: params.roomId,
293
+ eventId: params.eventId,
294
+ attempts,
295
+ inFlight: false,
296
+ timer: null,
297
+ };
298
+ next.timer = setTimeout(() => {
299
+ this.runDecryptRetry(retryKey).catch(noop);
300
+ }, delayMs);
301
+ this.decryptRetries.set(retryKey, next);
302
+ }
303
+
304
+ private async runDecryptRetry(retryKey: string): Promise<void> {
305
+ const state = this.decryptRetries.get(retryKey);
306
+ if (!state || state.inFlight) {
307
+ return;
308
+ }
309
+
310
+ state.inFlight = true;
311
+ state.timer = null;
312
+ this.activeRetryRuns += 1;
313
+ const canDecrypt = typeof this.deps.client.decryptEventIfNeeded === "function";
314
+ if (!canDecrypt) {
315
+ this.clearDecryptRetry(retryKey);
316
+ this.activeRetryRuns = Math.max(0, this.activeRetryRuns - 1);
317
+ this.resolveRetryIdleIfNeeded();
318
+ return;
319
+ }
320
+
321
+ try {
322
+ await this.deps.client.decryptEventIfNeeded?.(state.event, {
323
+ isRetry: true,
324
+ });
325
+ } catch {
326
+ // Retry with backoff until we hit the configured retry cap.
327
+ } finally {
328
+ state.inFlight = false;
329
+ this.activeRetryRuns = Math.max(0, this.activeRetryRuns - 1);
330
+ this.resolveRetryIdleIfNeeded();
331
+ }
332
+
333
+ if (this.decryptRetries.get(retryKey) !== state) {
334
+ return;
335
+ }
336
+ if (isDecryptionFailure(state.event)) {
337
+ if (!shouldRetryDecryptionFailure(state.event)) {
338
+ this.clearDecryptRetry(retryKey);
339
+ return;
340
+ }
341
+ this.scheduleDecryptRetry(state);
342
+ return;
343
+ }
344
+
345
+ this.clearDecryptRetry(retryKey);
346
+ const raw = this.deps.toRaw(state.event);
347
+ this.rememberDecryptedMessage(state.roomId, raw.event_id);
348
+ this.deps.emitDecryptedEvent(state.roomId, raw);
349
+ this.deps.emitMessage(state.roomId, raw);
350
+ }
351
+
352
+ private clearDecryptRetry(retryKey: string): void {
353
+ const state = this.decryptRetries.get(retryKey);
354
+ if (state?.timer) {
355
+ clearTimeout(state.timer);
356
+ }
357
+ this.decryptRetries.delete(retryKey);
358
+ this.exhaustedDecryptRetries.delete(retryKey);
359
+ this.failedDecryptionsNotified.delete(retryKey);
360
+ }
361
+
362
+ private rememberDecryptedMessage(roomId: string, eventId: string): void {
363
+ if (!eventId) {
364
+ return;
365
+ }
366
+ const now = Date.now();
367
+ this.pruneDecryptedMessageDedupe(now);
368
+ this.decryptedMessageDedupe.set(`${roomId}|${eventId}`, now);
369
+ }
370
+
371
+ private pruneDecryptedMessageDedupe(now: number): void {
372
+ const ttlMs = 30_000;
373
+ for (const [key, createdAt] of this.decryptedMessageDedupe) {
374
+ if (now - createdAt > ttlMs) {
375
+ this.decryptedMessageDedupe.delete(key);
376
+ }
377
+ }
378
+ const maxEntries = 2048;
379
+ while (this.decryptedMessageDedupe.size > maxEntries) {
380
+ const oldest = this.decryptedMessageDedupe.keys().next().value;
381
+ if (oldest === undefined) {
382
+ break;
383
+ }
384
+ this.decryptedMessageDedupe.delete(oldest);
385
+ }
386
+ }
387
+
388
+ private async waitForActiveRetryRunsToFinish(): Promise<void> {
389
+ if (this.activeRetryRuns === 0) {
390
+ return;
391
+ }
392
+ await new Promise<void>((resolve) => {
393
+ this.retryIdleResolvers.add(resolve);
394
+ if (this.activeRetryRuns === 0) {
395
+ this.retryIdleResolvers.delete(resolve);
396
+ resolve();
397
+ }
398
+ });
399
+ }
400
+
401
+ private resolveRetryIdleIfNeeded(): void {
402
+ if (this.activeRetryRuns !== 0) {
403
+ return;
404
+ }
405
+ for (const resolve of this.retryIdleResolvers) {
406
+ resolve();
407
+ }
408
+ this.retryIdleResolvers.clear();
409
+ }
410
+ }
@@ -0,0 +1,83 @@
1
+ import type { MatrixEvent } from "matrix-js-sdk/lib/matrix.js";
2
+ import type { MatrixRawEvent } from "./types.js";
3
+
4
+ type MatrixEventContentMode = "current" | "original";
5
+
6
+ export function matrixEventToRaw(
7
+ event: MatrixEvent,
8
+ opts: { contentMode?: MatrixEventContentMode } = {},
9
+ ): MatrixRawEvent {
10
+ const unsigned = (event.getUnsigned?.() ?? {}) as {
11
+ age?: number;
12
+ redacted_because?: unknown;
13
+ };
14
+ const eventWithOriginalContent = event as {
15
+ getOriginalContent?: () => Record<string, unknown>;
16
+ };
17
+ const content =
18
+ opts.contentMode === "original"
19
+ ? (eventWithOriginalContent.getOriginalContent?.() ?? event.getContent?.() ?? {})
20
+ : (event.getContent?.() ?? eventWithOriginalContent.getOriginalContent?.() ?? {});
21
+ const raw: MatrixRawEvent = {
22
+ event_id: event.getId() ?? "",
23
+ sender: event.getSender() ?? "",
24
+ type: event.getType() ?? "",
25
+ origin_server_ts: event.getTs() ?? 0,
26
+ content: content || {},
27
+ unsigned,
28
+ };
29
+ const stateKey = resolveMatrixStateKey(event);
30
+ if (typeof stateKey === "string") {
31
+ raw.state_key = stateKey;
32
+ }
33
+ return raw;
34
+ }
35
+
36
+ export function parseMxc(url: string): { server: string; mediaId: string } | null {
37
+ const match = /^mxc:\/\/([^/]+)\/(.+)$/.exec(url.trim());
38
+ if (!match) {
39
+ return null;
40
+ }
41
+ return {
42
+ server: match[1],
43
+ mediaId: match[2],
44
+ };
45
+ }
46
+
47
+ export function buildHttpError(
48
+ statusCode: number,
49
+ bodyText: string,
50
+ ): Error & { statusCode: number } {
51
+ let message = `Matrix HTTP ${statusCode}`;
52
+ if (bodyText.trim()) {
53
+ try {
54
+ const parsed = JSON.parse(bodyText) as { error?: string };
55
+ if (typeof parsed.error === "string" && parsed.error.trim()) {
56
+ message = parsed.error.trim();
57
+ } else {
58
+ message = bodyText.slice(0, 500);
59
+ }
60
+ } catch {
61
+ message = bodyText.slice(0, 500);
62
+ }
63
+ }
64
+ return Object.assign(new Error(message), { statusCode });
65
+ }
66
+
67
+ function resolveMatrixStateKey(event: MatrixEvent): string | undefined {
68
+ const direct = event.getStateKey?.();
69
+ if (typeof direct === "string") {
70
+ return direct;
71
+ }
72
+ const wireContent = (
73
+ event as { getWireContent?: () => { state_key?: unknown } }
74
+ ).getWireContent?.();
75
+ if (wireContent && typeof wireContent.state_key === "string") {
76
+ return wireContent.state_key;
77
+ }
78
+ const rawEvent = (event as { event?: { state_key?: unknown } }).event;
79
+ if (rawEvent && typeof rawEvent.state_key === "string") {
80
+ return rawEvent.state_key;
81
+ }
82
+ return undefined;
83
+ }
@@ -0,0 +1,87 @@
1
+ import type { PinnedDispatcherPolicy } from "autobot/plugin-sdk/ssrf-dispatcher";
2
+ import type { SsrFPolicy } from "../../runtime-api.js";
3
+ import { buildHttpError } from "./event-helpers.js";
4
+ import { type HttpMethod, type QueryParams, performMatrixRequest } from "./transport.js";
5
+
6
+ type MatrixAuthedHttpClientParams = {
7
+ homeserver: string;
8
+ accessToken: string;
9
+ ssrfPolicy?: SsrFPolicy;
10
+ dispatcherPolicy?: PinnedDispatcherPolicy;
11
+ };
12
+
13
+ export class MatrixAuthedHttpClient {
14
+ private readonly homeserver: string;
15
+ private readonly accessToken: string;
16
+ private readonly ssrfPolicy?: SsrFPolicy;
17
+ private readonly dispatcherPolicy?: PinnedDispatcherPolicy;
18
+
19
+ constructor(params: MatrixAuthedHttpClientParams) {
20
+ this.homeserver = params.homeserver;
21
+ this.accessToken = params.accessToken;
22
+ this.ssrfPolicy = params.ssrfPolicy;
23
+ this.dispatcherPolicy = params.dispatcherPolicy;
24
+ }
25
+
26
+ async requestJson(params: {
27
+ method: HttpMethod;
28
+ endpoint: string;
29
+ qs?: QueryParams;
30
+ body?: unknown;
31
+ timeoutMs: number;
32
+ allowAbsoluteEndpoint?: boolean;
33
+ }): Promise<unknown> {
34
+ const { response, text } = await performMatrixRequest({
35
+ homeserver: this.homeserver,
36
+ accessToken: this.accessToken,
37
+ method: params.method,
38
+ endpoint: params.endpoint,
39
+ qs: params.qs,
40
+ body: params.body,
41
+ timeoutMs: params.timeoutMs,
42
+ ssrfPolicy: this.ssrfPolicy,
43
+ dispatcherPolicy: this.dispatcherPolicy,
44
+ allowAbsoluteEndpoint: params.allowAbsoluteEndpoint,
45
+ });
46
+ if (!response.ok) {
47
+ throw buildHttpError(response.status, text);
48
+ }
49
+ const contentType = response.headers.get("content-type") ?? "";
50
+ if (contentType.includes("application/json")) {
51
+ if (!text.trim()) {
52
+ return {};
53
+ }
54
+ return JSON.parse(text);
55
+ }
56
+ return text;
57
+ }
58
+
59
+ async requestRaw(params: {
60
+ method: HttpMethod;
61
+ endpoint: string;
62
+ qs?: QueryParams;
63
+ timeoutMs: number;
64
+ maxBytes?: number;
65
+ readIdleTimeoutMs?: number;
66
+ allowAbsoluteEndpoint?: boolean;
67
+ }): Promise<Buffer> {
68
+ const { response, buffer } = await performMatrixRequest({
69
+ homeserver: this.homeserver,
70
+ accessToken: this.accessToken,
71
+ method: params.method,
72
+ endpoint: params.endpoint,
73
+ qs: params.qs,
74
+ timeoutMs: params.timeoutMs,
75
+ raw: true,
76
+ maxBytes: params.maxBytes,
77
+ readIdleTimeoutMs: params.readIdleTimeoutMs,
78
+ ssrfPolicy: this.ssrfPolicy,
79
+ dispatcherPolicy: this.dispatcherPolicy,
80
+ allowAbsoluteEndpoint: params.allowAbsoluteEndpoint,
81
+ });
82
+ if (!response.ok) {
83
+ throw buildHttpError(response.status, buffer.toString("utf8"));
84
+ }
85
+ return buffer;
86
+ }
87
+ }
@@ -0,0 +1,51 @@
1
+ import type { FileLockOptions } from "autobot/plugin-sdk/file-lock";
2
+
3
+ export const MATRIX_IDB_PERSIST_INTERVAL_MS = 60_000;
4
+
5
+ const IDB_SNAPSHOT_LOCK_STALE_MS = 5 * 60_000;
6
+ const IDB_SNAPSHOT_LOCK_RETRY_BASE = {
7
+ factor: 2,
8
+ minTimeout: 50,
9
+ maxTimeout: 5_000,
10
+ randomize: true,
11
+ } satisfies Omit<FileLockOptions["retries"], "retries">;
12
+
13
+ function computeRetryDelayMs(retries: FileLockOptions["retries"], attempt: number): number {
14
+ return Math.min(
15
+ retries.maxTimeout,
16
+ Math.max(retries.minTimeout, retries.minTimeout * retries.factor ** attempt),
17
+ );
18
+ }
19
+
20
+ export function computeMinimumRetryWindowMs(retries: FileLockOptions["retries"]): number {
21
+ let total = 0;
22
+ const attempts = Math.max(1, retries.retries + 1);
23
+ for (let attempt = 0; attempt < attempts - 1; attempt += 1) {
24
+ total += computeRetryDelayMs(retries, attempt);
25
+ }
26
+ return total;
27
+ }
28
+
29
+ function resolveRetriesForMinimumWindowMs(
30
+ retries: Omit<FileLockOptions["retries"], "retries">,
31
+ minimumWindowMs: number,
32
+ ): FileLockOptions["retries"] {
33
+ const resolved: FileLockOptions["retries"] = {
34
+ ...retries,
35
+ retries: 0,
36
+ };
37
+ while (computeMinimumRetryWindowMs(resolved) < minimumWindowMs) {
38
+ resolved.retries += 1;
39
+ }
40
+ return resolved;
41
+ }
42
+
43
+ export const MATRIX_IDB_SNAPSHOT_LOCK_OPTIONS: FileLockOptions = {
44
+ // Wait longer than one periodic persist interval so a concurrent restore
45
+ // or large snapshot dump finishes instead of forcing warn-and-continue.
46
+ retries: resolveRetriesForMinimumWindowMs(
47
+ IDB_SNAPSHOT_LOCK_RETRY_BASE,
48
+ MATRIX_IDB_PERSIST_INTERVAL_MS,
49
+ ),
50
+ stale: IDB_SNAPSHOT_LOCK_STALE_MS,
51
+ };