@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,543 @@
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { normalizeAccountId } from "autobot/plugin-sdk/account-id";
5
+ import type { AutoBotConfig } from "autobot/plugin-sdk/config-contracts";
6
+ import { loadJsonFile, saveJsonFile } from "autobot/plugin-sdk/json-store";
7
+ import {
8
+ requiresExplicitMatrixDefaultAccount,
9
+ resolveMatrixDefaultOrOnlyAccountId,
10
+ } from "../../account-selection.js";
11
+ import { getMatrixRuntime } from "../../runtime.js";
12
+ import {
13
+ resolveMatrixAccountStorageRoot,
14
+ resolveMatrixLegacyFlatStoragePaths,
15
+ } from "../../storage-paths.js";
16
+ import type { MatrixAuth } from "./types.js";
17
+ import type { MatrixStoragePaths } from "./types.js";
18
+
19
+ const DEFAULT_ACCOUNT_KEY = "default";
20
+ const STORAGE_META_FILENAME = "storage-meta.json";
21
+ const THREAD_BINDINGS_FILENAME = "thread-bindings.json";
22
+ const LEGACY_CRYPTO_MIGRATION_FILENAME = "legacy-crypto-migration.json";
23
+ const RECOVERY_KEY_FILENAME = "recovery-key.json";
24
+ const IDB_SNAPSHOT_FILENAME = "crypto-idb-snapshot.json";
25
+ const STARTUP_VERIFICATION_FILENAME = "startup-verification.json";
26
+
27
+ type LegacyMoveRecord = {
28
+ sourcePath: string;
29
+ targetPath: string;
30
+ label: string;
31
+ };
32
+
33
+ type StoredRootMetadata = {
34
+ homeserver?: string;
35
+ userId?: string;
36
+ accountId?: string;
37
+ accessTokenHash?: string;
38
+ deviceId?: string | null;
39
+ currentTokenStateClaimed?: boolean;
40
+ createdAt?: string;
41
+ };
42
+
43
+ function resolveLegacyStoragePaths(env: NodeJS.ProcessEnv = process.env): {
44
+ storagePath: string;
45
+ cryptoPath: string;
46
+ } {
47
+ const stateDir = getMatrixRuntime().state.resolveStateDir(env, os.homedir);
48
+ const legacy = resolveMatrixLegacyFlatStoragePaths(stateDir);
49
+ return { storagePath: legacy.storagePath, cryptoPath: legacy.cryptoPath };
50
+ }
51
+
52
+ function assertLegacyMigrationAccountSelection(params: { accountKey: string }): void {
53
+ const cfg = getMatrixRuntime().config.current() as AutoBotConfig;
54
+ if (!cfg.channels?.matrix || typeof cfg.channels.matrix !== "object") {
55
+ return;
56
+ }
57
+ if (requiresExplicitMatrixDefaultAccount(cfg)) {
58
+ throw new Error(
59
+ "Legacy Matrix client storage cannot be migrated automatically because multiple Matrix accounts are configured and channels.matrix.defaultAccount is not set.",
60
+ );
61
+ }
62
+
63
+ const selectedAccountId = normalizeAccountId(resolveMatrixDefaultOrOnlyAccountId(cfg));
64
+ const currentAccountId = normalizeAccountId(params.accountKey);
65
+ if (selectedAccountId !== currentAccountId) {
66
+ throw new Error(
67
+ `Legacy Matrix client storage targets account "${selectedAccountId}", but the current client is starting account "${currentAccountId}". Start the selected account first so flat legacy storage is not migrated into the wrong account directory.`,
68
+ );
69
+ }
70
+ }
71
+
72
+ function scoreStorageRoot(rootDir: string): number {
73
+ let score = 0;
74
+ if (fs.existsSync(path.join(rootDir, "bot-storage.json"))) {
75
+ score += 8;
76
+ }
77
+ if (fs.existsSync(path.join(rootDir, "crypto"))) {
78
+ score += 8;
79
+ }
80
+ if (fs.existsSync(path.join(rootDir, THREAD_BINDINGS_FILENAME))) {
81
+ score += 4;
82
+ }
83
+ if (fs.existsSync(path.join(rootDir, LEGACY_CRYPTO_MIGRATION_FILENAME))) {
84
+ score += 3;
85
+ }
86
+ if (fs.existsSync(path.join(rootDir, RECOVERY_KEY_FILENAME))) {
87
+ score += 2;
88
+ }
89
+ if (fs.existsSync(path.join(rootDir, IDB_SNAPSHOT_FILENAME))) {
90
+ score += 2;
91
+ }
92
+ if (fs.existsSync(path.join(rootDir, STORAGE_META_FILENAME))) {
93
+ score += 1;
94
+ }
95
+ return score;
96
+ }
97
+
98
+ function resolveStorageRootMtimeMs(rootDir: string): number {
99
+ try {
100
+ return fs.statSync(rootDir).mtimeMs;
101
+ } catch {
102
+ return 0;
103
+ }
104
+ }
105
+
106
+ function readStoredRootMetadata(rootDir: string): StoredRootMetadata {
107
+ const metadata: StoredRootMetadata = {};
108
+
109
+ const parsed = loadJsonFile<Partial<StoredRootMetadata>>(
110
+ path.join(rootDir, STORAGE_META_FILENAME),
111
+ );
112
+ if (parsed) {
113
+ if (typeof parsed.homeserver === "string" && parsed.homeserver.trim()) {
114
+ metadata.homeserver = parsed.homeserver.trim();
115
+ }
116
+ if (typeof parsed.userId === "string" && parsed.userId.trim()) {
117
+ metadata.userId = parsed.userId.trim();
118
+ }
119
+ if (typeof parsed.accountId === "string" && parsed.accountId.trim()) {
120
+ metadata.accountId = parsed.accountId.trim();
121
+ }
122
+ if (typeof parsed.accessTokenHash === "string" && parsed.accessTokenHash.trim()) {
123
+ metadata.accessTokenHash = parsed.accessTokenHash.trim();
124
+ }
125
+ if (typeof parsed.deviceId === "string" && parsed.deviceId.trim()) {
126
+ metadata.deviceId = parsed.deviceId.trim();
127
+ }
128
+ if (parsed.currentTokenStateClaimed === true) {
129
+ metadata.currentTokenStateClaimed = true;
130
+ }
131
+ if (typeof parsed.createdAt === "string" && parsed.createdAt.trim()) {
132
+ metadata.createdAt = parsed.createdAt.trim();
133
+ }
134
+ }
135
+
136
+ const verification = loadJsonFile<{ deviceId?: unknown }>(
137
+ path.join(rootDir, STARTUP_VERIFICATION_FILENAME),
138
+ );
139
+ if (
140
+ !metadata.deviceId &&
141
+ typeof verification?.deviceId === "string" &&
142
+ verification.deviceId.trim()
143
+ ) {
144
+ metadata.deviceId = verification.deviceId.trim();
145
+ }
146
+
147
+ return metadata;
148
+ }
149
+
150
+ function isCompatibleStorageRoot(params: {
151
+ candidateRootDir: string;
152
+ homeserver: string;
153
+ userId: string;
154
+ accountKey: string;
155
+ deviceId?: string | null;
156
+ requireExplicitDeviceMatch?: boolean;
157
+ }): boolean {
158
+ const metadata = readStoredRootMetadata(params.candidateRootDir);
159
+ if (metadata.homeserver && metadata.homeserver !== params.homeserver) {
160
+ return false;
161
+ }
162
+ if (metadata.userId && metadata.userId !== params.userId) {
163
+ return false;
164
+ }
165
+ if (
166
+ metadata.accountId &&
167
+ normalizeAccountId(metadata.accountId) !== normalizeAccountId(params.accountKey)
168
+ ) {
169
+ return false;
170
+ }
171
+ if (
172
+ params.deviceId &&
173
+ metadata.deviceId &&
174
+ metadata.deviceId.trim() &&
175
+ metadata.deviceId.trim() !== params.deviceId.trim()
176
+ ) {
177
+ return false;
178
+ }
179
+ if (
180
+ params.requireExplicitDeviceMatch &&
181
+ params.deviceId &&
182
+ (!metadata.deviceId || metadata.deviceId.trim() !== params.deviceId.trim())
183
+ ) {
184
+ return false;
185
+ }
186
+ return true;
187
+ }
188
+
189
+ function resolvePreferredMatrixStorageRoot(params: {
190
+ canonicalRootDir: string;
191
+ canonicalTokenHash: string;
192
+ homeserver: string;
193
+ userId: string;
194
+ accountKey: string;
195
+ deviceId?: string | null;
196
+ }): {
197
+ rootDir: string;
198
+ tokenHash: string;
199
+ } {
200
+ const parentDir = path.dirname(params.canonicalRootDir);
201
+ const bestCurrentScore = scoreStorageRoot(params.canonicalRootDir);
202
+ let best = {
203
+ rootDir: params.canonicalRootDir,
204
+ tokenHash: params.canonicalTokenHash,
205
+ score: bestCurrentScore,
206
+ mtimeMs: resolveStorageRootMtimeMs(params.canonicalRootDir),
207
+ };
208
+
209
+ // Without a confirmed device identity, reusing a populated sibling root after
210
+ // token rotation can silently bind this run to the wrong Matrix device state.
211
+ if (!params.deviceId?.trim()) {
212
+ return {
213
+ rootDir: best.rootDir,
214
+ tokenHash: best.tokenHash,
215
+ };
216
+ }
217
+
218
+ const canonicalMetadata = readStoredRootMetadata(params.canonicalRootDir);
219
+ if (
220
+ canonicalMetadata.accessTokenHash === params.canonicalTokenHash &&
221
+ canonicalMetadata.deviceId?.trim() === params.deviceId.trim() &&
222
+ canonicalMetadata.currentTokenStateClaimed === true
223
+ ) {
224
+ return {
225
+ rootDir: best.rootDir,
226
+ tokenHash: best.tokenHash,
227
+ };
228
+ }
229
+
230
+ let siblingEntries: fs.Dirent[] = [];
231
+ try {
232
+ siblingEntries = fs.readdirSync(parentDir, { withFileTypes: true });
233
+ } catch {
234
+ return {
235
+ rootDir: best.rootDir,
236
+ tokenHash: best.tokenHash,
237
+ };
238
+ }
239
+
240
+ for (const entry of siblingEntries) {
241
+ if (!entry.isDirectory()) {
242
+ continue;
243
+ }
244
+ if (entry.name === params.canonicalTokenHash) {
245
+ continue;
246
+ }
247
+ const candidateRootDir = path.join(parentDir, entry.name);
248
+ if (
249
+ !isCompatibleStorageRoot({
250
+ candidateRootDir,
251
+ homeserver: params.homeserver,
252
+ userId: params.userId,
253
+ accountKey: params.accountKey,
254
+ deviceId: params.deviceId,
255
+ // Once auth resolves a concrete device, only sibling roots that explicitly
256
+ // declare that same device are safe to reuse across token rotations.
257
+ requireExplicitDeviceMatch: Boolean(params.deviceId),
258
+ })
259
+ ) {
260
+ continue;
261
+ }
262
+ const candidateScore = scoreStorageRoot(candidateRootDir);
263
+ if (candidateScore <= 0) {
264
+ continue;
265
+ }
266
+ const candidateMtimeMs = resolveStorageRootMtimeMs(candidateRootDir);
267
+ if (
268
+ candidateScore > best.score ||
269
+ (best.rootDir !== params.canonicalRootDir &&
270
+ candidateScore === best.score &&
271
+ candidateMtimeMs > best.mtimeMs)
272
+ ) {
273
+ best = {
274
+ rootDir: candidateRootDir,
275
+ tokenHash: entry.name,
276
+ score: candidateScore,
277
+ mtimeMs: candidateMtimeMs,
278
+ };
279
+ }
280
+ }
281
+
282
+ return {
283
+ rootDir: best.rootDir,
284
+ tokenHash: best.tokenHash,
285
+ };
286
+ }
287
+
288
+ export function resolveMatrixStoragePaths(params: {
289
+ homeserver: string;
290
+ userId: string;
291
+ accessToken: string;
292
+ accountId?: string | null;
293
+ deviceId?: string | null;
294
+ env?: NodeJS.ProcessEnv;
295
+ stateDir?: string;
296
+ }): MatrixStoragePaths {
297
+ const env = params.env ?? process.env;
298
+ const stateDir = params.stateDir ?? getMatrixRuntime().state.resolveStateDir(env, os.homedir);
299
+ const canonical = resolveMatrixAccountStorageRoot({
300
+ stateDir,
301
+ homeserver: params.homeserver,
302
+ userId: params.userId,
303
+ accessToken: params.accessToken,
304
+ accountId: params.accountId,
305
+ });
306
+ const { rootDir, tokenHash } = resolvePreferredMatrixStorageRoot({
307
+ canonicalRootDir: canonical.rootDir,
308
+ canonicalTokenHash: canonical.tokenHash,
309
+ homeserver: params.homeserver,
310
+ userId: params.userId,
311
+ accountKey: canonical.accountKey,
312
+ deviceId: params.deviceId,
313
+ });
314
+ return {
315
+ rootDir,
316
+ storagePath: path.join(rootDir, "bot-storage.json"),
317
+ cryptoPath: path.join(rootDir, "crypto"),
318
+ metaPath: path.join(rootDir, STORAGE_META_FILENAME),
319
+ recoveryKeyPath: path.join(rootDir, "recovery-key.json"),
320
+ idbSnapshotPath: path.join(rootDir, IDB_SNAPSHOT_FILENAME),
321
+ accountKey: canonical.accountKey,
322
+ tokenHash,
323
+ };
324
+ }
325
+
326
+ export function resolveMatrixStateFilePath(params: {
327
+ auth: MatrixAuth;
328
+ filename: string;
329
+ accountId?: string | null;
330
+ env?: NodeJS.ProcessEnv;
331
+ stateDir?: string;
332
+ }): string {
333
+ const storagePaths = resolveMatrixStoragePaths({
334
+ homeserver: params.auth.homeserver,
335
+ userId: params.auth.userId,
336
+ accessToken: params.auth.accessToken,
337
+ accountId: params.accountId ?? params.auth.accountId,
338
+ deviceId: params.auth.deviceId,
339
+ env: params.env,
340
+ stateDir: params.stateDir,
341
+ });
342
+ return path.join(storagePaths.rootDir, params.filename);
343
+ }
344
+
345
+ export async function maybeMigrateLegacyStorage(params: {
346
+ storagePaths: MatrixStoragePaths;
347
+ env?: NodeJS.ProcessEnv;
348
+ }): Promise<void> {
349
+ const legacy = resolveLegacyStoragePaths(params.env);
350
+ const hasLegacyStorage = fs.existsSync(legacy.storagePath);
351
+ const hasLegacyCrypto = fs.existsSync(legacy.cryptoPath);
352
+ if (!hasLegacyStorage && !hasLegacyCrypto) {
353
+ return;
354
+ }
355
+ const hasTargetStorage = fs.existsSync(params.storagePaths.storagePath);
356
+ const hasTargetCrypto = fs.existsSync(params.storagePaths.cryptoPath);
357
+ // Continue partial migrations one artifact at a time; only skip items whose targets already exist.
358
+ const shouldMigrateStorage = hasLegacyStorage && !hasTargetStorage;
359
+ const shouldMigrateCrypto = hasLegacyCrypto && !hasTargetCrypto;
360
+ if (!shouldMigrateStorage && !shouldMigrateCrypto) {
361
+ return;
362
+ }
363
+
364
+ assertLegacyMigrationAccountSelection({
365
+ accountKey: params.storagePaths.accountKey,
366
+ });
367
+
368
+ const logger = getMatrixRuntime().logging.getChildLogger({ module: "matrix-storage" });
369
+ const { maybeCreateMatrixMigrationSnapshot } = await import("./migration-snapshot.runtime.js");
370
+ await maybeCreateMatrixMigrationSnapshot({
371
+ trigger: "matrix-client-fallback",
372
+ env: params.env,
373
+ log: logger,
374
+ });
375
+ fs.mkdirSync(params.storagePaths.rootDir, { recursive: true });
376
+ const moved: LegacyMoveRecord[] = [];
377
+ const skippedExistingTargets: string[] = [];
378
+ try {
379
+ if (shouldMigrateStorage) {
380
+ moveLegacyStoragePathOrThrow({
381
+ sourcePath: legacy.storagePath,
382
+ targetPath: params.storagePaths.storagePath,
383
+ label: "sync store",
384
+ moved,
385
+ });
386
+ } else if (hasLegacyStorage) {
387
+ skippedExistingTargets.push(
388
+ `- sync store remains at ${legacy.storagePath} because ${params.storagePaths.storagePath} already exists`,
389
+ );
390
+ }
391
+ if (shouldMigrateCrypto) {
392
+ moveLegacyStoragePathOrThrow({
393
+ sourcePath: legacy.cryptoPath,
394
+ targetPath: params.storagePaths.cryptoPath,
395
+ label: "crypto store",
396
+ moved,
397
+ });
398
+ } else if (hasLegacyCrypto) {
399
+ skippedExistingTargets.push(
400
+ `- crypto store remains at ${legacy.cryptoPath} because ${params.storagePaths.cryptoPath} already exists`,
401
+ );
402
+ }
403
+ } catch (err) {
404
+ const rollbackError = rollbackLegacyMoves(moved);
405
+ throw new Error(
406
+ rollbackError
407
+ ? `Failed migrating legacy Matrix client storage: ${String(err)}. Rollback also failed: ${rollbackError}`
408
+ : `Failed migrating legacy Matrix client storage: ${String(err)}`,
409
+ { cause: err },
410
+ );
411
+ }
412
+ if (moved.length > 0) {
413
+ logger.info(
414
+ `matrix: migrated legacy client storage into ${params.storagePaths.rootDir}\n${moved
415
+ .map((entry) => `- ${entry.label}: ${entry.sourcePath} -> ${entry.targetPath}`)
416
+ .join("\n")}`,
417
+ );
418
+ }
419
+ if (skippedExistingTargets.length > 0) {
420
+ logger.warn?.(
421
+ `matrix: legacy client storage still exists in the flat path because some account-scoped targets already existed.\n${skippedExistingTargets.join("\n")}`,
422
+ );
423
+ }
424
+ }
425
+
426
+ function moveLegacyStoragePathOrThrow(params: {
427
+ sourcePath: string;
428
+ targetPath: string;
429
+ label: string;
430
+ moved: LegacyMoveRecord[];
431
+ }): void {
432
+ if (!fs.existsSync(params.sourcePath)) {
433
+ return;
434
+ }
435
+ if (fs.existsSync(params.targetPath)) {
436
+ throw new Error(
437
+ `legacy Matrix ${params.label} target already exists (${params.targetPath}); refusing to overwrite it automatically`,
438
+ );
439
+ }
440
+ fs.renameSync(params.sourcePath, params.targetPath);
441
+ params.moved.push({
442
+ sourcePath: params.sourcePath,
443
+ targetPath: params.targetPath,
444
+ label: params.label,
445
+ });
446
+ }
447
+
448
+ function rollbackLegacyMoves(moved: LegacyMoveRecord[]): string | null {
449
+ for (const entry of moved.toReversed()) {
450
+ try {
451
+ if (!fs.existsSync(entry.targetPath) || fs.existsSync(entry.sourcePath)) {
452
+ continue;
453
+ }
454
+ fs.renameSync(entry.targetPath, entry.sourcePath);
455
+ } catch (err) {
456
+ return `${entry.label} (${entry.targetPath} -> ${entry.sourcePath}): ${String(err)}`;
457
+ }
458
+ }
459
+ return null;
460
+ }
461
+
462
+ function writeStoredRootMetadata(
463
+ metaPath: string,
464
+ payload: {
465
+ homeserver?: string;
466
+ userId?: string;
467
+ accountId: string;
468
+ accessTokenHash?: string;
469
+ deviceId: string | null;
470
+ currentTokenStateClaimed: boolean;
471
+ createdAt: string;
472
+ },
473
+ ): boolean {
474
+ try {
475
+ saveJsonFile(metaPath, payload);
476
+ return true;
477
+ } catch {
478
+ return false;
479
+ }
480
+ }
481
+
482
+ export function writeStorageMeta(params: {
483
+ storagePaths: MatrixStoragePaths;
484
+ homeserver: string;
485
+ userId: string;
486
+ accountId?: string | null;
487
+ deviceId?: string | null;
488
+ currentTokenStateClaimed?: boolean;
489
+ }): boolean {
490
+ const existing = readStoredRootMetadata(params.storagePaths.rootDir);
491
+ return writeStoredRootMetadata(params.storagePaths.metaPath, {
492
+ homeserver: params.homeserver,
493
+ userId: params.userId,
494
+ accountId: params.accountId ?? DEFAULT_ACCOUNT_KEY,
495
+ accessTokenHash: params.storagePaths.tokenHash,
496
+ deviceId: params.deviceId ?? null,
497
+ currentTokenStateClaimed:
498
+ params.currentTokenStateClaimed ?? existing.currentTokenStateClaimed === true,
499
+ createdAt: existing.createdAt ?? new Date().toISOString(),
500
+ });
501
+ }
502
+
503
+ export function claimCurrentTokenStorageState(params: { rootDir: string }): boolean {
504
+ const metadata = readStoredRootMetadata(params.rootDir);
505
+ if (!metadata.accessTokenHash?.trim()) {
506
+ return false;
507
+ }
508
+ return writeStoredRootMetadata(path.join(params.rootDir, STORAGE_META_FILENAME), {
509
+ homeserver: metadata.homeserver,
510
+ userId: metadata.userId,
511
+ accountId: metadata.accountId ?? DEFAULT_ACCOUNT_KEY,
512
+ accessTokenHash: metadata.accessTokenHash,
513
+ deviceId: metadata.deviceId ?? null,
514
+ currentTokenStateClaimed: true,
515
+ createdAt: metadata.createdAt ?? new Date().toISOString(),
516
+ });
517
+ }
518
+
519
+ export function repairCurrentTokenStorageMetaDeviceId(params: {
520
+ homeserver: string;
521
+ userId: string;
522
+ accessToken: string;
523
+ accountId?: string | null;
524
+ deviceId: string;
525
+ env?: NodeJS.ProcessEnv;
526
+ stateDir?: string;
527
+ }): boolean {
528
+ const storagePaths = resolveMatrixStoragePaths({
529
+ homeserver: params.homeserver,
530
+ userId: params.userId,
531
+ accessToken: params.accessToken,
532
+ accountId: params.accountId,
533
+ env: params.env,
534
+ stateDir: params.stateDir,
535
+ });
536
+ return writeStorageMeta({
537
+ storagePaths,
538
+ homeserver: params.homeserver,
539
+ userId: params.userId,
540
+ accountId: params.accountId,
541
+ deviceId: params.deviceId,
542
+ });
543
+ }
@@ -0,0 +1,50 @@
1
+ import type { PinnedDispatcherPolicy } from "autobot/plugin-sdk/ssrf-dispatcher";
2
+ import type { SsrFPolicy } from "../../runtime-api.js";
3
+
4
+ export type MatrixResolvedConfig = {
5
+ homeserver: string;
6
+ userId: string;
7
+ accessToken?: string;
8
+ deviceId?: string;
9
+ password?: string;
10
+ deviceName?: string;
11
+ initialSyncLimit?: number;
12
+ encryption?: boolean;
13
+ allowPrivateNetwork?: boolean;
14
+ ssrfPolicy?: SsrFPolicy;
15
+ dispatcherPolicy?: PinnedDispatcherPolicy;
16
+ };
17
+
18
+ /**
19
+ * Authenticated Matrix configuration.
20
+ * Note: deviceId is NOT included here because it's implicit in the accessToken.
21
+ * Matrix storage reuses the most complete account-scoped root it can find for the
22
+ * same homeserver/user/account tuple so token refreshes do not strand prior state.
23
+ * If the device identity itself changes or crypto storage is lost, crypto state may
24
+ * still need to be recreated together with the new access token.
25
+ */
26
+ export type MatrixAuth = {
27
+ accountId: string;
28
+ homeserver: string;
29
+ userId: string;
30
+ accessToken: string;
31
+ password?: string;
32
+ deviceId?: string;
33
+ deviceName?: string;
34
+ initialSyncLimit?: number;
35
+ encryption?: boolean;
36
+ allowPrivateNetwork?: boolean;
37
+ ssrfPolicy?: SsrFPolicy;
38
+ dispatcherPolicy?: PinnedDispatcherPolicy;
39
+ };
40
+
41
+ export type MatrixStoragePaths = {
42
+ rootDir: string;
43
+ storagePath: string;
44
+ cryptoPath: string;
45
+ metaPath: string;
46
+ recoveryKeyPath: string;
47
+ idbSnapshotPath: string;
48
+ accountKey: string;
49
+ tokenHash: string;
50
+ };
@@ -0,0 +1,76 @@
1
+ import {
2
+ assertHttpUrlTargetsPrivateNetwork,
3
+ type LookupFn,
4
+ } from "autobot/plugin-sdk/ssrf-runtime";
5
+ import { isPrivateOrLoopbackHost } from "./private-network-host.js";
6
+
7
+ const MATRIX_HTTP_HOMESERVER_ERROR =
8
+ "Matrix homeserver must use https:// unless it targets a private or loopback host";
9
+
10
+ function cleanString(value: unknown, requiredMessage: string): string {
11
+ const trimmed = typeof value === "string" ? value.trim() : "";
12
+ if (!trimmed) {
13
+ throw new Error(requiredMessage);
14
+ }
15
+ return trimmed;
16
+ }
17
+
18
+ export function validateMatrixHomeserverUrl(
19
+ homeserver: string,
20
+ opts?: { allowPrivateNetwork?: boolean },
21
+ ): string {
22
+ const trimmed = cleanString(homeserver, "Matrix homeserver is required (matrix.homeserver)");
23
+
24
+ let parsed: URL;
25
+ try {
26
+ parsed = new URL(trimmed);
27
+ } catch {
28
+ throw new Error("Matrix homeserver must be a valid http(s) URL");
29
+ }
30
+
31
+ if (parsed.protocol !== "https:" && parsed.protocol !== "http:") {
32
+ throw new Error("Matrix homeserver must use http:// or https://");
33
+ }
34
+ if (!parsed.hostname) {
35
+ throw new Error("Matrix homeserver must include a hostname");
36
+ }
37
+ if (parsed.username || parsed.password) {
38
+ throw new Error("Matrix homeserver URL must not include embedded credentials");
39
+ }
40
+ if (parsed.search || parsed.hash) {
41
+ throw new Error("Matrix homeserver URL must not include query strings or fragments");
42
+ }
43
+ if (
44
+ parsed.protocol === "http:" &&
45
+ opts?.allowPrivateNetwork !== true &&
46
+ !isPrivateOrLoopbackHost(parsed.hostname)
47
+ ) {
48
+ throw new Error(MATRIX_HTTP_HOMESERVER_ERROR);
49
+ }
50
+
51
+ return trimmed;
52
+ }
53
+
54
+ export async function resolveValidatedMatrixHomeserverUrl(
55
+ homeserver: string,
56
+ opts?: {
57
+ dangerouslyAllowPrivateNetwork?: boolean;
58
+ allowPrivateNetwork?: boolean;
59
+ lookupFn?: LookupFn;
60
+ },
61
+ ): Promise<string> {
62
+ const allowPrivateNetwork =
63
+ typeof opts?.dangerouslyAllowPrivateNetwork === "boolean"
64
+ ? opts.dangerouslyAllowPrivateNetwork
65
+ : opts?.allowPrivateNetwork;
66
+ const normalized = validateMatrixHomeserverUrl(homeserver, {
67
+ allowPrivateNetwork,
68
+ });
69
+ await assertHttpUrlTargetsPrivateNetwork(normalized, {
70
+ dangerouslyAllowPrivateNetwork: opts?.dangerouslyAllowPrivateNetwork,
71
+ allowPrivateNetwork,
72
+ lookupFn: opts?.lookupFn,
73
+ errorMessage: MATRIX_HTTP_HOMESERVER_ERROR,
74
+ });
75
+ return normalized;
76
+ }