@qvac/core 0.3.3 → 0.4.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 (169) hide show
  1. package/README.md +19 -17
  2. package/dist-lib/lib/abort-controller-node.d.ts +4 -0
  3. package/dist-lib/lib/abort-controller-node.js +1 -0
  4. package/dist-lib/lib/api/agents.d.ts +3 -3
  5. package/dist-lib/lib/api/agents.js +31 -20
  6. package/dist-lib/lib/api/approval-rules.d.ts +2 -2
  7. package/dist-lib/lib/api/approval-rules.js +2 -2
  8. package/dist-lib/lib/api/attachments.d.ts +2 -2
  9. package/dist-lib/lib/api/attachments.js +14 -25
  10. package/dist-lib/lib/api/chat-groups.d.ts +2 -2
  11. package/dist-lib/lib/api/chat-groups.js +9 -9
  12. package/dist-lib/lib/api/chat.d.ts +4 -2
  13. package/dist-lib/lib/api/chat.js +18 -18
  14. package/dist-lib/lib/api/chunks.d.ts +2 -2
  15. package/dist-lib/lib/api/chunks.js +27 -36
  16. package/dist-lib/lib/api/current-mesh.d.ts +7 -15
  17. package/dist-lib/lib/api/current-mesh.js +7 -22
  18. package/dist-lib/lib/api/devices.d.ts +4 -3
  19. package/dist-lib/lib/api/devices.js +18 -18
  20. package/dist-lib/lib/api/execution.d.ts +2 -2
  21. package/dist-lib/lib/api/execution.js +6 -6
  22. package/dist-lib/lib/api/files.d.ts +2 -2
  23. package/dist-lib/lib/api/files.js +4 -4
  24. package/dist-lib/lib/api/index.d.ts +12 -11
  25. package/dist-lib/lib/api/index.js +15 -13
  26. package/dist-lib/lib/api/inference-calls.d.ts +1 -1
  27. package/dist-lib/lib/api/inference-calls.js +2 -2
  28. package/dist-lib/lib/api/knowledge.d.ts +2 -2
  29. package/dist-lib/lib/api/knowledge.js +22 -11
  30. package/dist-lib/lib/api/local-files.d.ts +3 -0
  31. package/dist-lib/lib/api/local-files.js +23 -0
  32. package/dist-lib/lib/api/members.d.ts +6 -2
  33. package/dist-lib/lib/api/members.js +24 -17
  34. package/dist-lib/lib/api/mesh.d.ts +4 -5
  35. package/dist-lib/lib/api/mesh.js +7 -9
  36. package/dist-lib/lib/api/notifications.d.ts +2 -13
  37. package/dist-lib/lib/api/notifications.js +20 -52
  38. package/dist-lib/lib/api/rag.d.ts +2 -2
  39. package/dist-lib/lib/api/rag.js +4 -4
  40. package/dist-lib/lib/api/range.d.ts +11 -0
  41. package/dist-lib/lib/api/range.js +10 -0
  42. package/dist-lib/lib/api/sync.d.ts +2 -2
  43. package/dist-lib/lib/api/sync.js +3 -3
  44. package/dist-lib/lib/api/tool-activity.d.ts +2 -2
  45. package/dist-lib/lib/api/tool-activity.js +11 -10
  46. package/dist-lib/lib/api/tool-configs.d.ts +2 -2
  47. package/dist-lib/lib/api/tool-configs.js +6 -7
  48. package/dist-lib/lib/api/upload-frames.d.ts +9 -0
  49. package/dist-lib/lib/api/upload-frames.js +17 -0
  50. package/dist-lib/lib/api/watchable.d.ts +1 -6
  51. package/dist-lib/lib/api/watchable.js +4 -13
  52. package/dist-lib/lib/assistant/history.d.ts +3 -3
  53. package/dist-lib/lib/assistant-client.js +3 -2
  54. package/dist-lib/lib/assistant-tree.js +1 -1
  55. package/dist-lib/lib/assistant.d.ts +16 -14
  56. package/dist-lib/lib/assistant.js +166 -137
  57. package/dist-lib/lib/collabora/convert.js +1 -1
  58. package/dist-lib/lib/core/agent.d.ts +5 -7
  59. package/dist-lib/lib/core/agent.js +26 -6
  60. package/dist-lib/lib/core/archived-mesh.js +1 -0
  61. package/dist-lib/lib/core/chat-order.d.ts +20 -0
  62. package/dist-lib/lib/core/chat-order.js +96 -0
  63. package/dist-lib/lib/core/chat.d.ts +0 -5
  64. package/dist-lib/lib/core/chat.js +1 -3
  65. package/dist-lib/lib/core/chunk.d.ts +7 -14
  66. package/dist-lib/lib/core/chunk.js +17 -35
  67. package/dist-lib/lib/core/contract/agents.js +8 -0
  68. package/dist-lib/lib/core/contract/compute.d.ts +1 -1
  69. package/dist-lib/lib/core/contract/compute.js +1 -0
  70. package/dist-lib/lib/core/contract/joined-meshes.js +4 -3
  71. package/dist-lib/lib/core/contract/members.js +4 -3
  72. package/dist-lib/lib/core/contract/writers.js +5 -4
  73. package/dist-lib/lib/core/departures.js +5 -4
  74. package/dist-lib/lib/core/device-invites.js +2 -1
  75. package/dist-lib/lib/core/files/attachments.d.ts +8 -1
  76. package/dist-lib/lib/core/files/index.d.ts +4 -4
  77. package/dist-lib/lib/core/files/index.js +18 -8
  78. package/dist-lib/lib/core/files/local-index.d.ts +16 -0
  79. package/dist-lib/lib/core/files/local-index.js +38 -0
  80. package/dist-lib/lib/core/files/processing/document.js +2 -1
  81. package/dist-lib/lib/core/files/processing/ownership.js +6 -3
  82. package/dist-lib/lib/core/files/records/chunks.js +1 -0
  83. package/dist-lib/lib/core/files/records/reclaim.js +1 -0
  84. package/dist-lib/lib/core/files/records/routing.js +2 -1
  85. package/dist-lib/lib/core/files/store/blob.d.ts +1 -1
  86. package/dist-lib/lib/core/files/store/blob.js +6 -3
  87. package/dist-lib/lib/core/files/store/index.d.ts +1 -1
  88. package/dist-lib/lib/core/files/store/reference.js +4 -3
  89. package/dist-lib/lib/core/files/store/server.d.ts +8 -4
  90. package/dist-lib/lib/core/files/store/server.js +16 -12
  91. package/dist-lib/lib/core/files/store/sweep.js +2 -1
  92. package/dist-lib/lib/core/files/upload.d.ts +1 -1
  93. package/dist-lib/lib/core/hosted-agents.js +1 -1
  94. package/dist-lib/lib/core/image/cache-sweep.d.ts +1 -0
  95. package/dist-lib/lib/core/image/cache-sweep.js +7 -3
  96. package/dist-lib/lib/core/local/files.d.ts +24 -0
  97. package/dist-lib/lib/core/local/files.js +154 -0
  98. package/dist-lib/lib/core/local/index.d.ts +21 -5
  99. package/dist-lib/lib/core/local/index.js +75 -45
  100. package/dist-lib/lib/core/membership.d.ts +4 -1
  101. package/dist-lib/lib/core/membership.js +17 -2
  102. package/dist-lib/lib/core/mesh.d.ts +5 -0
  103. package/dist-lib/lib/core/mesh.js +11 -5
  104. package/dist-lib/lib/core/migrations/agent-indexes.d.ts +2 -0
  105. package/dist-lib/lib/core/migrations/agent-indexes.js +30 -0
  106. package/dist-lib/lib/core/migrations/file-index.d.ts +2 -0
  107. package/dist-lib/lib/core/migrations/file-index.js +27 -0
  108. package/dist-lib/lib/core/migrations/index.js +3 -1
  109. package/dist-lib/lib/core/notification.js +5 -5
  110. package/dist-lib/lib/core/range.d.ts +18 -0
  111. package/dist-lib/lib/core/range.js +17 -0
  112. package/dist-lib/lib/core/sync/index.js +39 -32
  113. package/dist-lib/lib/core/sync-status.js +3 -2
  114. package/dist-lib/lib/core/tool-activity.d.ts +2 -5
  115. package/dist-lib/lib/core/tool-activity.js +4 -7
  116. package/dist-lib/lib/core.d.ts +48 -29
  117. package/dist-lib/lib/core.js +465 -359
  118. package/dist-lib/lib/knowledge-search.d.ts +2 -2
  119. package/dist-lib/lib/kv-cache.js +1 -1
  120. package/dist-lib/lib/log.js +2 -3
  121. package/dist-lib/lib/mesh-scoped-engine.d.ts +8 -1
  122. package/dist-lib/lib/mesh-scoped-engine.js +62 -15
  123. package/dist-lib/lib/model-download.d.ts +14 -5
  124. package/dist-lib/lib/model-download.js +25 -17
  125. package/dist-lib/lib/oauth/token-exchange-core.js +1 -1
  126. package/dist-lib/lib/run-transcript.d.ts +2 -2
  127. package/dist-lib/lib/run-transcript.js +3 -2
  128. package/dist-lib/lib/task.d.ts +1 -0
  129. package/dist-lib/lib/task.js +3 -2
  130. package/dist-lib/schema/build.js +4 -10
  131. package/dist-lib/schema/engine.js +261 -91
  132. package/dist-lib/schema/helpers.d.ts +4 -0
  133. package/dist-lib/schema/helpers.js +20 -0
  134. package/dist-lib/schema/local.d.ts +1 -1
  135. package/dist-lib/schema/local.js +70 -2
  136. package/dist-lib/schema/qvac.js +30 -0
  137. package/dist-lib/spec/bind.js +20 -3
  138. package/dist-lib/spec/calls.js +19 -15
  139. package/dist-lib/spec/capabilities.d.ts +42 -34
  140. package/dist-lib/spec/hrpc/hrpc.json +82 -30
  141. package/dist-lib/spec/hrpc/index.js +106 -57
  142. package/dist-lib/spec/hrpc/messages.js +5420 -5019
  143. package/dist-lib/spec/hyperschema/index.js +5424 -5023
  144. package/dist-lib/spec/hyperschema/schema.json +3202 -2666
  145. package/dist-lib/spec/hyperschema/types.d.ts +455 -319
  146. package/dist-lib/spec/local/hyperdb/db.json +119 -1
  147. package/dist-lib/spec/local/hyperdb/index.js +414 -2
  148. package/dist-lib/spec/local/hyperdb/messages.js +505 -124
  149. package/dist-lib/spec/local/hyperschema/index.js +203 -5
  150. package/dist-lib/spec/local/hyperschema/schema.json +136 -1
  151. package/dist-lib/spec/local/hyperschema/types.d.ts +32 -1
  152. package/dist-lib/spec/mesh/hyperdb/db.json +55 -3
  153. package/dist-lib/spec/mesh/hyperdb/index.js +119 -3
  154. package/dist-lib/spec/mesh/hyperdb/messages.js +244 -172
  155. package/dist-lib/spec/mesh/hyperdispatch/dispatch.json +7 -1
  156. package/dist-lib/spec/mesh/hyperdispatch/index.js +18 -1
  157. package/dist-lib/spec/mesh/hyperdispatch/messages.js +74 -2
  158. package/dist-lib/spec/mesh/hyperschema/index.js +74 -2
  159. package/dist-lib/spec/mesh/hyperschema/schema.json +27 -1
  160. package/dist-lib/spec/mesh/hyperschema/types.d.ts +9 -1
  161. package/package.json +10 -6
  162. package/tui/app.mjs +376 -151
  163. package/tui/boot.mjs +2 -1
  164. package/tui/lib/busy-words.mjs +2 -1
  165. package/tui/lib/knowledge-command.mjs +11 -10
  166. package/tui/lib/knowledge-import.mjs +7 -5
  167. package/tui/lib/render.mjs +4 -3
  168. package/tui/lib/usage.mjs +2 -1
  169. package/tui/main.mjs +2 -1
@@ -13,12 +13,14 @@ import ScopeLock from 'scope-lock';
13
13
  import { createApi } from './api/index.js';
14
14
  import { NO_SUCH_MESH } from './api/current-mesh.js';
15
15
  import { BLIND_PEER_KEYS, BLIND_RELAY_KEYS } from './core/blind-peers.js';
16
- import { CoreBlobServer, parseBlobReferenceCoreKey, MemoryVectorIndex } from './core/files/index.js';
16
+ import { CoreBlobServer, MemoryVectorIndex } from './core/files/index.js';
17
17
  import { getChat, removeChatAttachments, removeChatKnowledge } from './core/chat.js';
18
+ import { rebuildChatOrder, refreshChatOrder } from './core/chat-order.js';
18
19
  import { createFsDriveHost } from './core/fs-drives.js';
19
20
  import { sweepDriveStaging } from './fs-drive-bridge.js';
20
21
  import { closeCollaboraWorker } from './collabora/worker.js';
21
22
  import { decodeMeshInvite, encodeMeshInvite } from './core/invite.js';
23
+ import { fileKeyOf, knownFile, referencedFile } from './core/files/local-index.js';
22
24
  import { openLocalStore } from './core/local/index.js';
23
25
  import { getAgent, ownDefaultAgents } from './core/agent.js';
24
26
  import { countContents, migrateContents, withArchivedMesh } from './core/archived-mesh.js';
@@ -34,9 +36,10 @@ import { createSkillOverlayReceiver } from './core/skill-overlay-receiver.js';
34
36
  import { createSkillSecretReceiver, HOST_REVOKED, NO_SUCH_AGENT, NOT_THE_HOST } from './core/skill-secrets.js';
35
37
  import { attestMemberClaim, claimAttestation, openIdentity } from './core/identity.js';
36
38
  import { openRecoveryCore, publishRecovery, readRecovery, recoveryKeys } from './core/account-recovery.js';
37
- import { attestUnattestedDevices, claimOwnMembership, renameFoundedMember } from './core/membership.js';
39
+ import { attestUnattestedDevices, claimOwnMembership, legacyNameStamp, namedAt, renameFoundedMember } from './core/membership.js';
38
40
  import { openDeviceInvites } from './core/device-invites.js';
39
41
  import { runMigrations } from './core/migrations/index.js';
42
+ import { backfillFileIndex } from './core/migrations/file-index.js';
40
43
  import { advertiseDeviceVersion } from './core/devices.js';
41
44
  import { OtaConfigReader, configCoreFor, resolveConfigChannel, vendoredMinSupportedVersion } from './core/ota-config.js';
42
45
  import { updateRequired } from './core/min-version.js';
@@ -52,6 +55,7 @@ import { unref } from './unref.js';
52
55
  import { local } from '../spec/index.js';
53
56
  const QvacState = local.getEnum('@local/qvac-state');
54
57
  const top = new Top();
58
+ const FILE_GC_INTERVAL_MS = 24 * 60 * 60 * 1000;
55
59
  const SWARM_KEYPAIR_NAME = 'qvac-swarm-keypair';
56
60
  const QVAC_CORE_NAMESPACE = 'qvac-core';
57
61
  const MESH_JOIN_CANCELED = 'Mesh join canceled';
@@ -100,6 +104,9 @@ export class Core extends ReadyResource {
100
104
  _localDevice;
101
105
  _blindPeerKeys;
102
106
  _blindPeeringOpts;
107
+ _fileGc;
108
+ _fileGcTimer = null;
109
+ _fileGcRun = null;
103
110
  _meshSeed;
104
111
  _meshKey;
105
112
  _additionalNodes;
@@ -120,6 +127,7 @@ export class Core extends ReadyResource {
120
127
  _indexingLock = new ScopeLock();
121
128
  _meshTransitionLock = new ScopeLock();
122
129
  _opening = new Map();
130
+ _retiring = new Map();
123
131
  _skills;
124
132
  _skillCatalog;
125
133
  _skillManagement;
@@ -132,8 +140,7 @@ export class Core extends ReadyResource {
132
140
  _closeListeners;
133
141
  _closeSignaled;
134
142
  _meshes;
135
- _blobCoreMeshes = new Map();
136
- _activeMeshKey;
143
+ _accountId;
137
144
  _offlineBoot = new ReadyGuard();
138
145
  onlineBoot = new ReadyGuard();
139
146
  _candidateMesh;
@@ -164,12 +171,13 @@ export class Core extends ReadyResource {
164
171
  _kickedPending;
165
172
  _peers;
166
173
  _statusListeners;
174
+ _listing;
167
175
  _diagnosticsProviders;
168
176
  _inferenceCalls = null;
169
177
  _systemInfo;
170
178
  _statusError;
171
179
  _upgradeRequired;
172
- constructor(transport, { storagePath, logger = null, spec = openLocalSpec, bootstrapNodes = undefined, device = undefined, app = undefined, minSupportedVersion = undefined, otaConfigCore = undefined, blindPeerKeys = undefined, blindPeeringOpts = undefined, meshSeed = undefined, meshKey = undefined, additionalNodes = undefined, listModels = undefined, deleteModel = undefined, clearKvCache = undefined, deleteChatKvCache = undefined, compactChat = undefined, modelCatalogWatch = undefined, retryModelDownload = undefined, transcribe = undefined, speak = undefined, speakStream = undefined, embedder = undefined, extractors = undefined, skills = undefined, skillCatalog = undefined, tools = undefined, hostsModelSource = false, vectorIndex = undefined, apiLifecycle = undefined }) {
180
+ constructor(transport, { storagePath, logger = null, spec = openLocalSpec, bootstrapNodes = undefined, device = undefined, app = undefined, minSupportedVersion = undefined, otaConfigCore = undefined, blindPeerKeys = undefined, blindPeeringOpts = undefined, fileGc = null, meshSeed = undefined, meshKey = undefined, additionalNodes = undefined, listModels = undefined, deleteModel = undefined, clearKvCache = undefined, deleteChatKvCache = undefined, compactChat = undefined, modelCatalogWatch = undefined, retryModelDownload = undefined, transcribe = undefined, speak = undefined, speakStream = undefined, embedder = undefined, extractors = undefined, skills = undefined, skillCatalog = undefined, tools = undefined, hostsModelSource = false, vectorIndex = undefined, apiLifecycle = undefined }) {
173
181
  super();
174
182
  this._transport = transport;
175
183
  this._apiLifecycle = apiLifecycle ?? {
@@ -205,6 +213,7 @@ export class Core extends ReadyResource {
205
213
  this._localDevice = null;
206
214
  this._blindPeerKeys = blindPeerKeys;
207
215
  this._blindPeeringOpts = blindPeeringOpts;
216
+ this._fileGc = fileGc;
208
217
  this._meshSeed = meshSeed;
209
218
  this._meshKey = meshKey;
210
219
  this._additionalNodes = additionalNodes;
@@ -243,7 +252,7 @@ export class Core extends ReadyResource {
243
252
  listener();
244
253
  });
245
254
  this._meshes = new Map();
246
- this._activeMeshKey = null;
255
+ this._accountId = null;
247
256
  this._candidateMesh = null;
248
257
  this._cancelCandidateJoin = null;
249
258
  this._joinCancellation = null;
@@ -268,11 +277,12 @@ export class Core extends ReadyResource {
268
277
  onrequest: (request, from) => this._serveInferenceCalls(request, from)
269
278
  });
270
279
  this._minting = false;
271
- this._leaving = false;
280
+ this._leaving = null;
272
281
  this._kicked = false;
273
282
  this._kickedPending = false;
274
283
  this._peers = new Set();
275
284
  this._statusListeners = new Set();
285
+ this._listing = null;
276
286
  this._diagnosticsProviders = new Set();
277
287
  this._systemInfo = null;
278
288
  this._statusError = null;
@@ -288,7 +298,7 @@ export class Core extends ReadyResource {
288
298
  return this.state === QvacState.SUSPEND;
289
299
  }
290
300
  get mesh() {
291
- return this._currentMesh();
301
+ return this._accountMesh();
292
302
  }
293
303
  async meshReady() {
294
304
  await this._awaitMesh();
@@ -297,7 +307,7 @@ export class Core extends ReadyResource {
297
307
  return this._peers.size;
298
308
  }
299
309
  get peerDeviceIds() {
300
- const ids = [...this._peers].map((conn) => conn.remotePublicKey.toString('hex'));
310
+ const ids = [...this._peers].map((conn) => b4a.toHex(conn.remotePublicKey));
301
311
  return [...new Set(ids)].sort();
302
312
  }
303
313
  async _open() {
@@ -314,7 +324,7 @@ export class Core extends ReadyResource {
314
324
  this._openSwarm(this._deviceKeyPair);
315
325
  this._departures = new PendingDepartures(this.store, this.local, {
316
326
  swarm: this._swarm,
317
- isPooled: (key) => this._currentMesh(key.toString('hex')) !== null,
327
+ isPooled: (key) => this._pooled(b4a.toHex(key)) !== null,
318
328
  logger: this._log,
319
329
  blindPeerKeys: this._resolveBlindPeerKeys(),
320
330
  blindPeeringOpts: this._blindPeeringOpts,
@@ -326,7 +336,7 @@ export class Core extends ReadyResource {
326
336
  blindPeeringOpts: this._blindPeeringOpts
327
337
  });
328
338
  void sweepDriveStaging();
329
- this._blobServer = new CoreBlobServer(this.store, (coreKey) => this._meshForBlobCore(coreKey));
339
+ this._blobServer = new CoreBlobServer(this.store, (coreKey, blockOffset, meshKey) => this._meshForBlobCore(coreKey, blockOffset, meshKey), { onRead: (mesh, coreKey, blockOffset) => this._servedFile(mesh, coreKey, blockOffset) });
330
340
  this._blobServer.on('rebound', (port) => {
331
341
  logger.warn('core', 'blob server rebound, links minted before now are stale', { port });
332
342
  });
@@ -337,19 +347,18 @@ export class Core extends ReadyResource {
337
347
  const meshStatus = this._meshStatusSource();
338
348
  this._api = createApi({
339
349
  state: this.local,
340
- notificationLifetime: this,
341
- mesh: this._currentMeshAccessor(meshStatus),
350
+ mesh: this._meshPool(),
342
351
  meshStatus,
343
352
  network: this._networkStatusSource(),
344
353
  diagnostics: this._diagnosticsSource(),
345
354
  inferenceCalls: {
346
- list: (chatId) => this._listInferenceCalls(chatId),
355
+ list: (meshId, chatId) => this._listInferenceCalls(meshId, chatId),
347
356
  forget: async (chatId) => void (await this._inferenceCalls?.forget(chatId))
348
357
  },
349
358
  logs: this._logSource(),
350
359
  deviceId: this.devicePublicKey,
351
360
  lifecycle: this._apiLifecycle,
352
- ragIndex: () => this._activeEntry()?.vectorIndex ?? null,
361
+ ragIndex: (meshId) => this._meshes.get(meshId)?.vectorIndex ?? null,
353
362
  ragEmbed: () => this._indexedEmbedder,
354
363
  platform: () => this._localDevice?.platform ?? null,
355
364
  computeLender: () => this._computeLender(),
@@ -371,7 +380,7 @@ export class Core extends ReadyResource {
371
380
  status: 'unsupported',
372
381
  error: 'No harness KV cache deletion source is bound'
373
382
  }),
374
- compactChat: (chatId) => this._compactChat?.(chatId) ??
383
+ compactChat: (chatId, meshId) => this._compactChat?.(chatId, meshId) ??
375
384
  Promise.resolve({ compacted: false, reason: 'unsupported' }),
376
385
  modelCatalogWatch: this._hostsModelSource || this._modelCatalogWatch
377
386
  ? () => this._watchModelCatalog()
@@ -395,7 +404,7 @@ export class Core extends ReadyResource {
395
404
  }
396
405
  : null,
397
406
  deviceHost: {
398
- removeWriter: (writerKey, deviceId) => this.removeDeviceWriter(writerKey, deviceId),
407
+ removeWriter: (writerKey, deviceId, mesh) => this.removeDeviceWriter(writerKey, deviceId, mesh),
399
408
  renameLocal: (name) => this.renameLocalDevice(name),
400
409
  saveProfileLocal: (capabilityProfile, recommendedModelName, platform) => this.saveDeviceProfileLocal(capabilityProfile, recommendedModelName, platform),
401
410
  readLocalProfile: () => this._localDevice
@@ -487,7 +496,7 @@ export class Core extends ReadyResource {
487
496
  }
488
497
  }
489
498
  async _openBootMesh() {
490
- if (this._blockedByMinVersion() || this._currentMesh() || this._minting)
499
+ if (this._blockedByMinVersion() || this._accountMesh() || this._minting)
491
500
  return;
492
501
  try {
493
502
  const mesh = await this._switchToMesh({
@@ -495,7 +504,6 @@ export class Core extends ReadyResource {
495
504
  baseKey: this._meshKey,
496
505
  account: true
497
506
  });
498
- void this._openAccountBeside(mesh).catch(noop);
499
507
  }
500
508
  catch (err) {
501
509
  if (this.closing)
@@ -507,30 +515,13 @@ export class Core extends ReadyResource {
507
515
  this._log?.error(['p2p', 'mesh'], 'mesh open failed', err);
508
516
  }
509
517
  }
510
- async _openAccountBeside(active) {
511
- const home = await this._homeMeshKey();
512
- if (!home || home.equals(active.key))
513
- return;
514
- await this._meshTransitionLock.lock();
515
- try {
516
- if (this.closing || this._blockedByMinVersion())
517
- return;
518
- const mesh = await this._openPooledMesh(home.toString('hex')).catch(() => null);
519
- if (mesh && !mesh.ourMemberId)
520
- await mesh.refreshOurMember();
521
- }
522
- finally {
523
- this._meshTransitionLock.unlock();
524
- }
525
- this._notifyStatus();
526
- }
527
518
  async _runBootOnline() {
528
519
  void this.onlineBoot.ready().catch(noop);
529
520
  try {
530
521
  await this._offlineBoot.ready();
531
522
  if (this.closing)
532
523
  return;
533
- const mesh = this._currentMesh();
524
+ const mesh = this._accountMesh();
534
525
  if (mesh)
535
526
  this._bringMeshOnline(mesh);
536
527
  }
@@ -544,19 +535,16 @@ export class Core extends ReadyResource {
544
535
  this._peerConfigCore();
545
536
  if (!this.closing)
546
537
  this._departures?.flush();
538
+ if (!this.closing)
539
+ this._startFileGc({ now: true });
547
540
  this.onlineBoot.exit();
548
541
  }
549
542
  }
550
543
  async _reopenMesh() {
551
544
  try {
552
- if (this.closing || this._currentMesh() || this._minting)
545
+ if (this.closing || this._accountMesh() || this._minting)
553
546
  return;
554
- const mesh = await this._startMesh({
555
- seed: this._meshSeed,
556
- baseKey: this._meshKey,
557
- account: true
558
- });
559
- void this._openAccountBeside(mesh).catch(noop);
547
+ await this._startMesh({ seed: this._meshSeed, baseKey: this._meshKey, account: true });
560
548
  }
561
549
  catch (err) {
562
550
  if (this.closing)
@@ -610,7 +598,7 @@ export class Core extends ReadyResource {
610
598
  const length = config.core.length;
611
599
  this._log?.info(['p2p', 'config'], `ota config ${event}`, {
612
600
  lane: this._app?.channel ?? null,
613
- discoveryKey: config.core.discoveryKey.toString('hex').slice(0, 8),
601
+ discoveryKey: b4a.toHex(config.core.discoveryKey).slice(0, 8),
614
602
  length,
615
603
  floor,
616
604
  source: length > 0 && length >= (this._otaConfigCore?.minLength ?? 1) ? 'published' : 'vendored',
@@ -620,7 +608,7 @@ export class Core extends ReadyResource {
620
608
  }
621
609
  _peerConfigCore() {
622
610
  const config = this._otaConfig;
623
- if (!config || this._configPeering || this._currentMesh() || !this._swarm)
611
+ if (!config || this._configPeering || this._accountMesh() || !this._swarm)
624
612
  return;
625
613
  const keys = this._resolveBlindPeerKeys();
626
614
  if (keys.length === 0)
@@ -730,6 +718,7 @@ export class Core extends ReadyResource {
730
718
  if (!this.opened)
731
719
  await this.ready();
732
720
  logger.info('core', 'suspending');
721
+ await this._stopFileGc();
733
722
  for (const { mesh } of this._meshes.values())
734
723
  await mesh.suspend();
735
724
  if (this._departures)
@@ -773,6 +762,7 @@ export class Core extends ReadyResource {
773
762
  if (this._departures)
774
763
  await this._departures.resume();
775
764
  this._departures?.flush();
765
+ this._startFileGc();
776
766
  if (this._swarm)
777
767
  void this._refreshTopics(this._swarm).catch(noop);
778
768
  void this._pushIdentityPhrase();
@@ -794,6 +784,7 @@ export class Core extends ReadyResource {
794
784
  this.onlineBoot.destroy(new Error('core is closing'));
795
785
  this._signalClose();
796
786
  this._notifyStatus();
787
+ await this._stopFileGc();
797
788
  if (this._stream)
798
789
  this._stream.destroy();
799
790
  for (const client of this._clients)
@@ -820,7 +811,7 @@ export class Core extends ReadyResource {
820
811
  logger.info('core', 'closing swarm');
821
812
  await this._closeSwarm();
822
813
  this._meshes.clear();
823
- this._activeMeshKey = null;
814
+ this._accountId = null;
824
815
  logger.info('core', 'closing local');
825
816
  await this.local?.close();
826
817
  this._systemInfo?.destroy();
@@ -838,18 +829,18 @@ export class Core extends ReadyResource {
838
829
  });
839
830
  this._swarm.on('connection', (conn) => this._onConnection(conn));
840
831
  this._log?.info(['p2p', 'swarm'], 'swarm created', {
841
- deviceKey: keyPair.publicKey.toString('hex').slice(0, 8)
832
+ deviceKey: b4a.toHex(keyPair.publicKey).slice(0, 8)
842
833
  });
843
834
  for (const node of this._additionalNodes ?? [])
844
835
  this._swarm.dht.addNode(node);
845
836
  }
846
837
  _resolveBlindPeerKeys(extra = []) {
847
838
  const defaults = this._bootstrapNodes ? [] : BLIND_PEER_KEYS;
848
- const merged = new Map(defaults.map((key) => [key.toString('hex'), key]));
839
+ const merged = new Map(defaults.map((key) => [b4a.toHex(key), key]));
849
840
  for (const key of this._blindPeerKeys ?? [])
850
- merged.set(key.toString('hex'), key);
841
+ merged.set(b4a.toHex(key), key);
851
842
  for (const key of extra)
852
- merged.set(key.toString('hex'), key);
843
+ merged.set(b4a.toHex(key), key);
853
844
  return [...merged.values()];
854
845
  }
855
846
  async _startMesh(opts = {}) {
@@ -862,7 +853,7 @@ export class Core extends ReadyResource {
862
853
  throw new Error('core is closing');
863
854
  if (this._blockedByMinVersion())
864
855
  throw new Error(MESH_BELOW_FLOOR);
865
- const previous = this._currentMesh();
856
+ const previous = this._accountMesh();
866
857
  if (previous)
867
858
  await this._retirePooledMesh(previous);
868
859
  const mesh = await this._openMesh(opts);
@@ -875,7 +866,7 @@ export class Core extends ReadyResource {
875
866
  this._withdrawFromMesh(mesh, halted);
876
867
  throw halted;
877
868
  }
878
- await this._activateMesh(mesh);
869
+ await this._adoptAccount(mesh);
879
870
  return mesh;
880
871
  }
881
872
  async _openMesh(opts = {}) {
@@ -894,7 +885,8 @@ export class Core extends ReadyResource {
894
885
  blindPeerKeys: this._resolveBlindPeerKeys(blindPeerKeys),
895
886
  blindPeeringOpts: this._blindPeeringOpts,
896
887
  wakeup: this._wakeup,
897
- attachmentUrl: (attachment) => this._linkAttachment(attachment, mesh)
888
+ attachmentUrl: (attachment) => this._linkAttachment(attachment, mesh),
889
+ fileIndex: this._fileIndexFor(() => mesh)
898
890
  });
899
891
  try {
900
892
  await mesh.ready();
@@ -932,7 +924,7 @@ export class Core extends ReadyResource {
932
924
  id: identity.publicKey,
933
925
  name: identity.displayName ?? local.name,
934
926
  joinedAt: now,
935
- updatedAt: now
927
+ updatedAt: namedAt(identity)
936
928
  },
937
929
  proof: identity.proof
938
930
  },
@@ -942,20 +934,20 @@ export class Core extends ReadyResource {
942
934
  async publishAccountRecovery(mnemonic) {
943
935
  if (!this.opened)
944
936
  await this.ready();
945
- const home = await this.local.homeMesh(this._deviceKeyPair.publicKey);
946
- if (!home)
937
+ const account = await this.local.accountMesh(this._deviceKeyPair.publicKey);
938
+ if (!account)
947
939
  throw new Error(NO_ACCOUNT_TO_RECOVER);
948
- return this._publishRecovery(mnemonic, {
949
- meshKey: home.key,
950
- seed: home.seed,
951
- createdAt: Date.now()
952
- });
940
+ const record = { meshKey: account.key, seed: account.seed, createdAt: Date.now() };
941
+ return this._publishRecovery(mnemonic, record);
953
942
  }
954
943
  async accountRecoverable() {
955
944
  if (!this.opened)
956
945
  await this.ready();
957
- const home = await this.local.homeMesh(this._deviceKeyPair.publicKey);
958
- return home !== null && (await this.local.readAccountRecovery(home.key)) !== null;
946
+ const account = await this.local.accountMesh(this._deviceKeyPair.publicKey);
947
+ if (!account)
948
+ return false;
949
+ const recovery = await this.local.readAccountRecovery(account.key);
950
+ return recovery !== null;
959
951
  }
960
952
  async _publishRecovery(mnemonic, record) {
961
953
  const identity = await openIdentity(mnemonic);
@@ -1033,19 +1025,19 @@ export class Core extends ReadyResource {
1033
1025
  }
1034
1026
  async _recoverAccount(identity, record) {
1035
1027
  const deviceId = this._deviceKeyPair.publicKey;
1036
- const pooled = this._currentMesh(record.meshKey.toString('hex'));
1028
+ const meshId = b4a.toHex(record.meshKey);
1029
+ await this._opening.get(meshId)?.catch(noop);
1030
+ const pooled = this._pooled(meshId);
1037
1031
  if (pooled?.writable) {
1038
- if (!this._isActive(pooled))
1039
- await this._handOffActive(pooled, this._activeEntry());
1032
+ if (!this._isAccount(pooled))
1033
+ await this._adoptAccount(pooled);
1040
1034
  }
1041
1035
  else {
1042
- if (pooled)
1043
- await this._retirePooledMesh(pooled);
1044
- await this._joinMesh({ seed: record.seed, baseKey: record.meshKey, fastForward: null });
1036
+ await this._joinMesh({ seed: record.seed, baseKey: record.meshKey, fastForward: null }, pooled);
1045
1037
  }
1046
- const mesh = this._currentMesh();
1038
+ const mesh = this._accountMesh();
1047
1039
  if (!mesh?.key.equals(record.meshKey))
1048
- throw new Error('the recovered account did not activate');
1040
+ throw new Error('the recovered account did not adopt');
1049
1041
  if (this._identityPublicKey?.equals(identity.identityPublicKey))
1050
1042
  return;
1051
1043
  const proof = await identity.bootstrap(deviceId);
@@ -1108,9 +1100,15 @@ export class Core extends ReadyResource {
1108
1100
  const attestation = claimAttestation(mesh.publicKey, deviceId, mesh.localWriterKey);
1109
1101
  const known = await this._knownMember(mesh);
1110
1102
  if (known) {
1111
- const displayName = (await this.local.readIdentity())?.displayName;
1103
+ const identity = await this.local.readIdentity();
1104
+ const ours = identity?.displayName && namedAt(identity) >= known.updatedAt;
1112
1105
  return {
1113
- member: { id: known.id, name: displayName ?? known.name, joinedAt: now, updatedAt: now },
1106
+ member: {
1107
+ id: known.id,
1108
+ name: ours ? identity.displayName : known.name,
1109
+ joinedAt: now,
1110
+ updatedAt: ours ? namedAt(identity) : known.updatedAt
1111
+ },
1114
1112
  proof: await attestMemberClaim(attestation, this._deviceKeyPair, known.proof)
1115
1113
  };
1116
1114
  }
@@ -1123,7 +1121,7 @@ export class Core extends ReadyResource {
1123
1121
  id: identity.publicKey,
1124
1122
  name: identity.displayName ?? local.name,
1125
1123
  joinedAt: now,
1126
- updatedAt: now
1124
+ updatedAt: namedAt(identity)
1127
1125
  },
1128
1126
  proof
1129
1127
  };
@@ -1233,7 +1231,7 @@ export class Core extends ReadyResource {
1233
1231
  async _settleMembership(mesh, { migrate = false } = {}) {
1234
1232
  if (!this._localDevice)
1235
1233
  return;
1236
- const entry = this._meshes.get(this._poolKey(mesh));
1234
+ const entry = this._meshes.get(this._meshIdOf(mesh));
1237
1235
  if (entry?.mesh !== mesh)
1238
1236
  return;
1239
1237
  if (!(await entry.memberLock.lock()))
@@ -1248,7 +1246,7 @@ export class Core extends ReadyResource {
1248
1246
  const context = { deviceKeyPair: this._deviceKeyPair, log: this._log };
1249
1247
  if (migrate) {
1250
1248
  await runMigrations(mesh, { ...context, ...this._migrationContext() }, {
1251
- catalog: await this._isHomeMesh(mesh)
1249
+ catalog: await this._isStoredAccount(mesh)
1252
1250
  });
1253
1251
  }
1254
1252
  await this._claimOwnMembership(mesh, context);
@@ -1263,9 +1261,41 @@ export class Core extends ReadyResource {
1263
1261
  }
1264
1262
  }
1265
1263
  async _renameFoundedMember(mesh, context) {
1266
- const identity = await this.local.readIdentity();
1267
- if (identity)
1268
- await renameFoundedMember(mesh, context, identity);
1264
+ let identity = await this.local.readIdentity();
1265
+ if (!identity)
1266
+ return;
1267
+ const adopt = (name, at) => this._adoptName(name, at);
1268
+ if (!identity.displayName || identity.displayNameAt) {
1269
+ await renameFoundedMember(mesh, context, identity, adopt);
1270
+ return;
1271
+ }
1272
+ if (!this._isAccount(mesh))
1273
+ return;
1274
+ const stamp = await legacyNameStamp(mesh, context, identity);
1275
+ if (stamp) {
1276
+ const stamped = await this.local.saveDisplayName(identity.displayName, stamp);
1277
+ identity = stamped ?? identity;
1278
+ }
1279
+ await renameFoundedMember(mesh, context, identity, adopt);
1280
+ const named = await this.local.readIdentity();
1281
+ if (named?.displayNameAt) {
1282
+ void this._followAccountName(mesh);
1283
+ }
1284
+ }
1285
+ async _adoptName(name, namedAt) {
1286
+ await this.local.saveDisplayName(name, namedAt);
1287
+ this._notifyStatus();
1288
+ }
1289
+ async _followAccountName(account) {
1290
+ const context = { deviceKeyPair: this._deviceKeyPair, log: this._log };
1291
+ const rename = (mesh) => this._renameFoundedMember(mesh, context).catch((err) => this._log?.warn(['p2p', 'mesh'], 'naming the member in this mesh failed', err));
1292
+ if (account.writable) {
1293
+ await rename(account);
1294
+ }
1295
+ for (const { mesh } of this._meshes.values()) {
1296
+ if (mesh.writable && mesh !== account)
1297
+ void rename(mesh);
1298
+ }
1269
1299
  }
1270
1300
  async _claimOwnMembership(mesh, context) {
1271
1301
  if (mesh.ourMemberId)
@@ -1287,8 +1317,14 @@ export class Core extends ReadyResource {
1287
1317
  const member = await entry.mesh.view
1288
1318
  .get('@qvac/members', { id: own.memberId })
1289
1319
  .catch(noop);
1290
- if (member)
1291
- return { id: member.id, name: member.name, proof: own.memberProof };
1320
+ if (member) {
1321
+ return {
1322
+ id: member.id,
1323
+ name: member.name,
1324
+ updatedAt: member.updatedAt,
1325
+ proof: own.memberProof
1326
+ };
1327
+ }
1292
1328
  }
1293
1329
  return null;
1294
1330
  }
@@ -1321,22 +1357,22 @@ export class Core extends ReadyResource {
1321
1357
  }
1322
1358
  }
1323
1359
  async _ensureNotRevoked(mesh) {
1324
- const wasActive = this._isActive(mesh);
1360
+ const wasAccount = this._isAccount(mesh);
1325
1361
  try {
1326
1362
  const revocation = await mesh.checkResumedRevocation();
1327
1363
  if (!revocation || !this._isPooled(mesh))
1328
1364
  return;
1329
- if (!wasActive || !this._isActive(mesh)) {
1365
+ if (!wasAccount || !this._isAccount(mesh)) {
1330
1366
  if (revocation !== 'kicked')
1331
1367
  return;
1332
1368
  this._log?.info(['p2p', 'mesh'], 'a sibling mesh kicked this device, closing it');
1333
1369
  await this._departRevokedMesh(mesh);
1334
1370
  return;
1335
1371
  }
1336
- this._log?.info(['p2p', 'mesh'], `resumed already revoked (${revocation} while offline), replacing the active mesh`);
1372
+ this._log?.info(['p2p', 'mesh'], `resumed already revoked (${revocation} while offline), replacing the account`);
1337
1373
  if (revocation === 'kicked')
1338
1374
  this._kickedPending = true;
1339
- await this._replaceRevokedActiveMesh(mesh);
1375
+ await this._replaceRevokedAccount(mesh);
1340
1376
  }
1341
1377
  catch (err) {
1342
1378
  this._log?.warn(['p2p', 'mesh'], 'resumed revocation check failed', err);
@@ -1348,15 +1384,29 @@ export class Core extends ReadyResource {
1348
1384
  await this._swarm.leave(mesh.discoveryKey).catch(() => { });
1349
1385
  await mesh.close();
1350
1386
  }
1351
- async _retirePooledMesh(mesh) {
1352
- const entry = this._meshes.get(this._poolKey(mesh));
1387
+ _retirePooledMesh(mesh) {
1388
+ const key = this._meshIdOf(mesh);
1389
+ const retiring = this._retirePooled(mesh).finally(() => {
1390
+ if (this._retiring.get(key) === retiring)
1391
+ this._retiring.delete(key);
1392
+ });
1393
+ this._retiring.set(key, retiring);
1394
+ return retiring;
1395
+ }
1396
+ async _retirePooled(mesh) {
1397
+ const entry = this._meshes.get(this._meshIdOf(mesh));
1353
1398
  this._unpoolMesh(mesh);
1354
1399
  try {
1400
+ this._notifyStatus();
1401
+ await this.listMeshes().catch(noop);
1355
1402
  await this._stopMeshServices(entry?.mesh === mesh ? entry : null);
1356
1403
  }
1357
1404
  finally {
1358
1405
  await this._retireMesh(mesh);
1359
1406
  }
1407
+ if (!(await this.local.readMeshSession(mesh.key))) {
1408
+ await this.local.forgetMesh(mesh.key);
1409
+ }
1360
1410
  }
1361
1411
  async _stopMeshServices(entry) {
1362
1412
  if (!entry)
@@ -1391,36 +1441,26 @@ export class Core extends ReadyResource {
1391
1441
  _onKicked(mesh) {
1392
1442
  if (this.closing || this._leaving)
1393
1443
  return;
1394
- if (!this._isActive(mesh)) {
1444
+ if (!this._isAccount(mesh)) {
1395
1445
  this._log?.info(['p2p', 'mesh'], 'our writer was revoked on a sibling mesh, closing it');
1396
1446
  this._departRevokedMesh(mesh).catch((err) => this._log?.warn(['p2p', 'mesh'], 'closing the revoked sibling mesh failed', err));
1397
1447
  return;
1398
1448
  }
1399
- this._log?.info(['p2p', 'mesh'], 'our writer was revoked (kicked), replacing the active mesh');
1449
+ this._log?.info(['p2p', 'mesh'], 'our writer was revoked (kicked), replacing the account');
1400
1450
  this._kicked = true;
1401
1451
  this._kickedPending = true;
1402
- this._replaceRevokedActiveMesh(mesh).catch((err) => this._log?.error(['p2p', 'mesh'], 'kick-driven replacement failed', err));
1452
+ this._replaceRevokedAccount(mesh).catch((err) => this._log?.error(['p2p', 'mesh'], 'kick-driven replacement failed', err));
1403
1453
  }
1404
- async _replaceRevokedActiveMesh(mesh) {
1454
+ async _replaceRevokedAccount(mesh) {
1405
1455
  this._minting = true;
1406
1456
  this._notifyStatus();
1407
- let fallback = null;
1408
1457
  try {
1409
- const others = (await this.local.listMeshes()).filter((row) => !row.key.equals(mesh.key));
1410
1458
  await this._archiveMembership(mesh);
1411
- fallback = await this._writableFallback(others);
1412
- if (fallback) {
1413
- await this._handOffActive(fallback, this._activeEntry());
1414
- await this._retirePooledMesh(mesh);
1415
- this.emit('mesh-changed', fallback);
1416
- }
1417
1459
  }
1418
1460
  finally {
1419
1461
  this._minting = false;
1420
- this._notifyStatus();
1421
1462
  }
1422
- if (!fallback)
1423
- await this._requestFreshMesh();
1463
+ await this._requestFreshMesh();
1424
1464
  }
1425
1465
  async _departRevokedMesh(mesh) {
1426
1466
  await this._archiveMembership(mesh);
@@ -1495,19 +1535,17 @@ export class Core extends ReadyResource {
1495
1535
  throw new Error('Core is closing');
1496
1536
  if (this._leaving || this._minting)
1497
1537
  throw new Error('Mesh transition already in progress');
1498
- const pooled = this._currentMesh(invite.baseKey.toString('hex'));
1499
- if (pooled) {
1500
- if (pooled.writable) {
1501
- if (!this._isActive(pooled))
1502
- await this._handOffActive(pooled, this._activeEntry());
1503
- return;
1504
- }
1505
- await this._retirePooledMesh(pooled);
1538
+ await this._opening.get(b4a.toHex(invite.baseKey))?.catch(noop);
1539
+ const pooled = this._pooled(b4a.toHex(invite.baseKey));
1540
+ if (pooled?.writable) {
1541
+ if (!invite.memberInvite && !this._isAccount(pooled))
1542
+ await this._adoptAccount(pooled);
1543
+ return;
1506
1544
  }
1507
1545
  ownsTransition = true;
1508
1546
  this._minting = true;
1509
1547
  this._notifyStatus();
1510
- await this._joinMesh(invite);
1548
+ await this._joinMesh(invite, pooled);
1511
1549
  }
1512
1550
  finally {
1513
1551
  if (ownsTransition)
@@ -1526,31 +1564,53 @@ export class Core extends ReadyResource {
1526
1564
  if (this._joinCancellation?.canceled)
1527
1565
  throw new Error(MESH_JOIN_CANCELED);
1528
1566
  }
1529
- async _joinMesh(invite) {
1567
+ async _joinMesh(invite, retire = null) {
1530
1568
  const cancellation = { canceled: false, accepting: true };
1531
1569
  this._joinCancellation = cancellation;
1570
+ const meshId = b4a.toHex(invite.baseKey);
1571
+ const claim = Promise.withResolvers();
1572
+ claim.promise.catch(noop);
1573
+ this._opening.set(meshId, claim.promise);
1532
1574
  try {
1533
- await this._runJoinMesh(invite);
1575
+ if (retire) {
1576
+ await this._retirePooledMesh(retire);
1577
+ }
1578
+ await this._runJoinMesh(invite, claim);
1579
+ }
1580
+ catch (error) {
1581
+ claim.reject(error);
1582
+ throw error;
1534
1583
  }
1535
1584
  finally {
1585
+ if (this._opening.get(meshId) === claim.promise)
1586
+ this._opening.delete(meshId);
1536
1587
  if (this._joinCancellation === cancellation)
1537
1588
  this._joinCancellation = null;
1538
1589
  }
1539
1590
  }
1540
- async _runJoinMesh(invite) {
1591
+ async _runJoinMesh(invite, claim) {
1541
1592
  const deviceId = this._deviceKeyPair.publicKey;
1542
- const source = this._currentMesh();
1593
+ const source = this._accountMesh();
1543
1594
  for (const node of invite.additionalNodes ?? [])
1544
1595
  this._swarm?.dht.addNode(node);
1545
1596
  await this._departures?.park(invite.baseKey);
1546
1597
  try {
1547
- await this._joinParkedMesh(invite, source, deviceId);
1598
+ await this._joinParkedMesh(invite, source, deviceId, claim);
1548
1599
  }
1549
1600
  finally {
1550
1601
  this._departures?.unpark(invite.baseKey);
1551
1602
  }
1552
1603
  }
1553
- async _joinParkedMesh(invite, source, deviceId) {
1604
+ async _joinParkedMesh(invite, source, deviceId, claim) {
1605
+ const mesh = await this._joinCandidate(invite, source, deviceId);
1606
+ if (mesh) {
1607
+ claim.resolve(mesh);
1608
+ }
1609
+ else {
1610
+ claim.reject(new Error(MESH_BELOW_FLOOR));
1611
+ }
1612
+ }
1613
+ async _joinCandidate(invite, source, deviceId) {
1554
1614
  const session = this.local.createCandidateSession(invite.seed, invite.baseKey, invite.compute);
1555
1615
  let candidate;
1556
1616
  try {
@@ -1573,21 +1633,25 @@ export class Core extends ReadyResource {
1573
1633
  await this._discardCandidateMesh();
1574
1634
  throw error;
1575
1635
  }
1576
- const outgoing = invite.memberInvite ? this._activeEntry() : null;
1636
+ return this._landJoinedMesh(invite, candidate, source, deviceId);
1637
+ }
1638
+ async _landJoinedMesh(invite, candidate, source, deviceId) {
1577
1639
  if (!invite.memberInvite)
1578
1640
  await this._departOldAccount(candidate.key, source);
1579
1641
  if (this._blockedByMinVersion()) {
1580
- await this.local.setActiveMesh(deviceId, candidate.key);
1642
+ if (!invite.memberInvite)
1643
+ await this.local.setAccountMesh(deviceId, candidate.key);
1581
1644
  await this._retireMesh(candidate).catch((err) => this._log?.warn(['p2p', 'mesh'], 'withholding the joined mesh failed', err));
1582
- return;
1645
+ return null;
1583
1646
  }
1584
- if (candidate.compute) {
1647
+ if (invite.memberInvite || candidate.compute) {
1648
+ this._candidateMesh = null;
1585
1649
  this._wireMesh(this._poolEntry(candidate));
1586
1650
  this._bringMeshOnline(candidate);
1587
1651
  this._notifyStatus();
1588
1652
  }
1589
1653
  else {
1590
- await this._handOffActive(candidate, outgoing);
1654
+ await this._adoptAccount(candidate);
1591
1655
  }
1592
1656
  void this._departures?.drop(candidate.key).catch(noop);
1593
1657
  void this._advertiseDeviceProfile(candidate, deviceId);
@@ -1597,22 +1661,25 @@ export class Core extends ReadyResource {
1597
1661
  void this._seedMemberDefaults(candidate);
1598
1662
  }
1599
1663
  this.emit('mesh-changed', candidate);
1664
+ return candidate;
1600
1665
  }
1601
1666
  async _departOldAccount(joined, source) {
1602
- const account = (await this._homeMeshKey()) ?? source?.key;
1667
+ const account = (await this._storedAccountKey()) ?? source?.key;
1668
+ if (account?.equals(joined))
1669
+ return;
1603
1670
  const rows = (await this.local.listMeshes()).filter((row) => !row.key.equals(joined));
1604
1671
  const rooms = rows.filter((row) => !account?.equals(row.key));
1605
1672
  if (account)
1606
- await this._accountMesh();
1673
+ await this._writableAccount();
1607
1674
  for (const room of rooms)
1608
1675
  await this._departIfPooled(room.key, 'room');
1609
- await this.local.adoptHomeMesh(this._deviceKeyPair.publicKey, joined, { account });
1676
+ await this.local.adoptAccountMesh(this._deviceKeyPair.publicKey, joined, { account });
1610
1677
  if (account)
1611
1678
  await this._departIfPooled(account, 'account');
1612
1679
  }
1613
1680
  async _departIfPooled(key, kind) {
1614
- const meshId = key.toString('hex');
1615
- const mesh = this._currentMesh(meshId);
1681
+ const meshId = b4a.toHex(key);
1682
+ const mesh = this._pooled(meshId);
1616
1683
  if (!mesh) {
1617
1684
  this._log?.warn(['p2p', 'mesh'], `the old ${kind} was closed, so its writer was not revoked`, {
1618
1685
  meshId
@@ -1665,7 +1732,7 @@ export class Core extends ReadyResource {
1665
1732
  }
1666
1733
  }
1667
1734
  async _registerRoom(meshKey, shared, held) {
1668
- const registry = held ?? (await this._accountMesh());
1735
+ const registry = held ?? (await this._writableAccount());
1669
1736
  if (!registry)
1670
1737
  throw new Error(ROOM_WITHOUT_ACCOUNT);
1671
1738
  const ours = registry.ourMemberId;
@@ -1696,12 +1763,12 @@ export class Core extends ReadyResource {
1696
1763
  this._log?.warn(['p2p', 'mesh'], 'releasing the joined room failed', error);
1697
1764
  }
1698
1765
  }
1699
- async _accountMesh() {
1766
+ async _writableAccount() {
1700
1767
  if (this.closing)
1701
1768
  return null;
1702
- const home = await this._homeMeshKey();
1703
- const held = home ? this._currentMesh(home.toString('hex')) : null;
1704
- const mesh = home ? await this._openPooledMesh(home.toString('hex')).catch(noop) : null;
1769
+ const key = await this._storedAccountKey();
1770
+ const held = key ? this._pooled(b4a.toHex(key)) : null;
1771
+ const mesh = key ? await this._openPooledMesh(b4a.toHex(key)).catch(noop) : null;
1705
1772
  if (mesh && !mesh.writable) {
1706
1773
  await Promise.race([mesh.waitForWritable(), this._closeSignal, after(ACCOUNT_WRITABLE_MS)]);
1707
1774
  }
@@ -1728,7 +1795,8 @@ export class Core extends ReadyResource {
1728
1795
  blindPeeringOpts: this._blindPeeringOpts,
1729
1796
  bootFrom: invite.fastForward,
1730
1797
  wakeup: this._wakeup,
1731
- attachmentUrl: (attachment) => this._linkAttachment(attachment, candidate)
1798
+ attachmentUrl: (attachment) => this._linkAttachment(attachment, candidate),
1799
+ fileIndex: this._fileIndexFor(() => candidate)
1732
1800
  });
1733
1801
  this._candidateMesh = candidate;
1734
1802
  if (this._swarm)
@@ -1823,7 +1891,7 @@ export class Core extends ReadyResource {
1823
1891
  _onMeshWritable(mesh) {
1824
1892
  if (!this._isPooled(mesh))
1825
1893
  return;
1826
- if (this._isActive(mesh))
1894
+ if (this._isAccount(mesh))
1827
1895
  this._kicked = false;
1828
1896
  this._notifyStatus();
1829
1897
  }
@@ -1831,7 +1899,7 @@ export class Core extends ReadyResource {
1831
1899
  if (this.closing || !this._isPooled(mesh))
1832
1900
  return;
1833
1901
  this._log?.error(['p2p', 'mesh'], 'mesh autobee died - retiring it', err);
1834
- if (this._isActive(mesh)) {
1902
+ if (this._isAccount(mesh)) {
1835
1903
  this._statusError = statusErrorMessage('the local database died', err);
1836
1904
  }
1837
1905
  this._retirePooledMesh(mesh).catch((retireErr) => {
@@ -1856,6 +1924,10 @@ export class Core extends ReadyResource {
1856
1924
  mesh.on('kicked', () => this._onKicked(mesh));
1857
1925
  mesh.on('unattested-device', () => void this._settleMembership(mesh));
1858
1926
  mesh.on('members-dirty', () => void this._revalidateLoans());
1927
+ mesh.on('members-dirty', () => {
1928
+ if (this._isAccount(mesh))
1929
+ void this._followAccountName(mesh);
1930
+ });
1859
1931
  mesh.on('rooms-dirty', () => void this._followJoinedRooms(mesh));
1860
1932
  mesh.on('membership-tallied', () => this._notifyStatus());
1861
1933
  mesh.on('membership', (own) => {
@@ -1867,46 +1939,34 @@ export class Core extends ReadyResource {
1867
1939
  mesh.autobee.on('close', handlers.dead);
1868
1940
  mesh.autobee.on('interrupt', handlers.stalled);
1869
1941
  this._startMeshServices(entry);
1942
+ this._startEntryIndexing(entry);
1870
1943
  }
1871
- async _handOffActive(incoming, outgoing) {
1872
- const stopped = outgoing && outgoing.mesh !== incoming ? outgoing : null;
1873
- if (stopped) {
1874
- await this._stopEntryIndexing(stopped);
1875
- this._releaseVectorIndex(stopped);
1876
- }
1877
- try {
1878
- await this._activateMesh(incoming);
1879
- }
1880
- catch (err) {
1881
- if (stopped)
1882
- this._startEntryIndexing(stopped);
1883
- throw err;
1884
- }
1885
- }
1886
- async _activateMesh(mesh) {
1944
+ async _adoptAccount(mesh) {
1887
1945
  if (this._candidateMesh === mesh)
1888
1946
  this._candidateMesh = null;
1889
- await this.local.setActiveMesh(this._deviceKeyPair.publicKey, mesh.key);
1947
+ await this.local.setAccountMesh(this._deviceKeyPair.publicKey, mesh.key);
1890
1948
  this._wireMesh(this._poolEntry(mesh));
1891
- this._activeMeshKey = this._poolKey(mesh);
1949
+ this._accountId = this._meshIdOf(mesh);
1892
1950
  this._statusError = null;
1893
1951
  this._upgradeRequired = null;
1894
1952
  void this._closeConfigPeering().catch(noop);
1895
1953
  if (mesh.writable)
1896
1954
  this._kicked = false;
1897
- this._startIndexing();
1898
1955
  this._notifyStatus();
1899
1956
  }
1900
- _currentMesh(meshKey = this._activeMeshKey) {
1957
+ _pooled(meshKey) {
1901
1958
  if (meshKey === null)
1902
1959
  return null;
1903
1960
  return this._meshes.get(meshKey)?.mesh ?? null;
1904
1961
  }
1905
- _poolKey(mesh) {
1906
- return mesh.key.toString('hex');
1962
+ _accountMesh() {
1963
+ return this._pooled(this._accountId);
1964
+ }
1965
+ _meshIdOf(mesh) {
1966
+ return b4a.toHex(mesh.key);
1907
1967
  }
1908
1968
  _poolEntry(mesh) {
1909
- const key = this._poolKey(mesh);
1969
+ const key = this._meshIdOf(mesh);
1910
1970
  const existing = this._meshes.get(key);
1911
1971
  if (existing?.mesh === mesh)
1912
1972
  return existing;
@@ -1921,57 +1981,123 @@ export class Core extends ReadyResource {
1921
1981
  this._meshes.set(key, entry);
1922
1982
  return entry;
1923
1983
  }
1924
- _meshFor(meshId) {
1925
- return meshId ? this._currentMesh(meshId) : this._currentMesh();
1926
- }
1927
- _activeEntry() {
1928
- if (this._activeMeshKey === null)
1929
- return null;
1930
- return this._meshes.get(this._activeMeshKey) ?? null;
1931
- }
1932
- _meshForBlobCore(coreKey) {
1933
- if (!coreKey)
1934
- return this._currentMesh();
1984
+ async _meshForBlobCore(coreKey, blockOffset, meshKey) {
1935
1985
  for (const { mesh } of this._meshes.values()) {
1936
- if (mesh.blobs.core.key.toString('hex') === coreKey)
1986
+ if (b4a.toHex(mesh.blobs.core.key) === coreKey)
1937
1987
  return mesh;
1938
1988
  }
1939
- return this._currentMesh(this._blobCoreMeshes.get(coreKey) ?? null);
1989
+ if (meshKey)
1990
+ return this._pooled(b4a.toHex(meshKey));
1991
+ if (blockOffset === undefined)
1992
+ return null;
1993
+ const indexed = await this.local.files.meshOf(Buffer.from(coreKey, 'hex'), blockOffset);
1994
+ return indexed ? this._pooled(b4a.toHex(indexed)) : null;
1940
1995
  }
1941
- _linkAttachment(attachment, mesh) {
1942
- const url = this._blobServer?.link(attachment, mesh) ?? null;
1943
- if (url) {
1944
- const coreKey = parseBlobReferenceCoreKey(attachment.blobId);
1945
- if (coreKey)
1946
- this._blobCoreMeshes.set(coreKey, this._poolKey(mesh));
1996
+ _fileIndexFor(mesh) {
1997
+ const owner = () => ({ meshKey: mesh().key, ownCoreKey: mesh().blobs.core.key });
1998
+ const known = (chunks) => chunks.map((chunk) => knownFile(chunk, owner())).filter((file) => file !== null);
1999
+ return {
2000
+ record: (chunks) => this.local.files.record(known(chunks)),
2001
+ refresh: (chunks) => this.local.files.refresh(known(chunks)),
2002
+ read: (reference) => {
2003
+ const file = referencedFile(reference, owner());
2004
+ if (!file)
2005
+ return;
2006
+ this.local.files
2007
+ .read(file)
2008
+ .catch((err) => this._log?.warn(['files'], 'recording a file read failed', err));
2009
+ },
2010
+ markCleared: async (blobId) => {
2011
+ const key = fileKeyOf(blobId, owner());
2012
+ if (key)
2013
+ await this.local.files.markCleared(key);
2014
+ }
2015
+ };
2016
+ }
2017
+ async _indexMeshFiles(mesh) {
2018
+ try {
2019
+ await mesh.autobee.update();
2020
+ if (await this.local.files.backfilled(mesh.key))
2021
+ return;
2022
+ await backfillFileIndex(mesh);
2023
+ if (!mesh.closing)
2024
+ await this.local.files.markBackfilled(mesh.key);
2025
+ }
2026
+ catch (err) {
2027
+ if (!mesh.closing)
2028
+ this._log?.warn(['files'], 'indexing the mesh files failed', err);
2029
+ }
2030
+ }
2031
+ _servedFile(mesh, coreKey, blockOffset) {
2032
+ this.local.files
2033
+ .touch({ meshKey: mesh.key, coreKey, blockOffset })
2034
+ .catch((err) => this._log?.warn(['files'], 'recording a file read failed', err));
2035
+ }
2036
+ async clearOldFiles({ olderThanMs, includeOwn = false }) {
2037
+ if (!this.opened)
2038
+ await this.ready();
2039
+ let cleared = 0;
2040
+ for (const file of await this.local.files.stale(Date.now() - olderThanMs)) {
2041
+ if (this.closing || this.suspended)
2042
+ break;
2043
+ if (file.own && !includeOwn)
2044
+ continue;
2045
+ const mesh = this._pooled(b4a.toHex(file.meshKey));
2046
+ if (!mesh)
2047
+ continue;
2048
+ const done = await mesh.files.clear(file.blobId, file.attachmentId ?? file.blobId);
2049
+ if (!done)
2050
+ continue;
2051
+ await this.local.files.markCleared(file);
2052
+ cleared++;
1947
2053
  }
1948
- return url;
2054
+ return cleared;
2055
+ }
2056
+ _startFileGc({ now = false } = {}) {
2057
+ const gc = this._fileGc;
2058
+ if (!gc || this._fileGcTimer || this.suspended)
2059
+ return;
2060
+ const run = () => {
2061
+ if (this._fileGcRun || this.closing || this.suspended)
2062
+ return;
2063
+ this._fileGcRun = this.clearOldFiles({ olderThanMs: gc.maxAgeMs, includeOwn: gc.includeOwn });
2064
+ this._fileGcRun
2065
+ .then((cleared) => this._log?.info(['files'], 'cleared files not read lately', { cleared }))
2066
+ .catch((err) => this._log?.warn(['files'], 'clearing old files failed', err))
2067
+ .finally(() => (this._fileGcRun = null));
2068
+ };
2069
+ this._fileGcTimer = setInterval(run, gc.intervalMs ?? FILE_GC_INTERVAL_MS);
2070
+ unref(this._fileGcTimer);
2071
+ if (now)
2072
+ run();
2073
+ }
2074
+ async _stopFileGc() {
2075
+ if (this._fileGcTimer)
2076
+ clearInterval(this._fileGcTimer);
2077
+ this._fileGcTimer = null;
2078
+ await this._fileGcRun?.catch(noop);
2079
+ }
2080
+ _linkAttachment(attachment, mesh) {
2081
+ return this._blobServer?.link(attachment, mesh) ?? null;
1949
2082
  }
1950
2083
  _isPooled(mesh) {
1951
- return !mesh.closing && this._meshes.get(this._poolKey(mesh))?.mesh === mesh;
2084
+ return !mesh.closing && this._meshes.get(this._meshIdOf(mesh))?.mesh === mesh;
1952
2085
  }
1953
- _isActive(mesh) {
1954
- return this._currentMesh() === mesh;
2086
+ _isAccount(mesh) {
2087
+ return this._accountMesh() === mesh;
1955
2088
  }
1956
2089
  _unpoolMesh(mesh) {
1957
- const key = this._poolKey(mesh);
2090
+ const key = this._meshIdOf(mesh);
1958
2091
  const entry = this._meshes.get(key);
1959
- let wasActive = false;
2092
+ let wasAccount = false;
1960
2093
  if (entry?.mesh === mesh) {
1961
2094
  this._unwireMesh(entry);
1962
2095
  this._meshes.delete(key);
1963
- if (this._activeMeshKey === key) {
1964
- this._activeMeshKey = null;
1965
- wasActive = true;
1966
- }
1967
- for (const [coreKey, poolKey] of this._blobCoreMeshes) {
1968
- if (poolKey === key)
1969
- this._blobCoreMeshes.delete(coreKey);
1970
- }
2096
+ wasAccount = this._accountId === key;
1971
2097
  }
1972
2098
  if (this._candidateMesh === mesh)
1973
2099
  this._candidateMesh = null;
1974
- if (wasActive)
2100
+ if (wasAccount)
1975
2101
  this._notifyStatus();
1976
2102
  }
1977
2103
  _unwireMesh(entry) {
@@ -1987,12 +2113,18 @@ export class Core extends ReadyResource {
1987
2113
  return null;
1988
2114
  if (!this.opened)
1989
2115
  await this.ready();
1990
- return this._currentMesh();
2116
+ return this._accountMesh();
1991
2117
  }
1992
2118
  _startMeshServices(entry) {
1993
2119
  const { mesh } = entry;
1994
2120
  const deviceId = this._deviceKeyPair.publicKey;
1995
2121
  mesh.on('notification', (event) => this.emit('notification', event));
2122
+ const built = rebuildChatOrder(mesh, this.local).catch((error) => this._log?.warn(['p2p', 'mesh'], 'building the chat order failed', error));
2123
+ mesh.chatOrderBuilt = built;
2124
+ mesh.syncChatOrder = async (chatIds) => {
2125
+ await built;
2126
+ await refreshChatOrder(mesh, this.local, chatIds);
2127
+ };
1996
2128
  void mesh.refreshMembership().catch((error) => {
1997
2129
  this._log?.warn(['p2p', 'mesh'], 'tallying membership failed', error);
1998
2130
  });
@@ -2022,6 +2154,7 @@ export class Core extends ReadyResource {
2022
2154
  }
2023
2155
  };
2024
2156
  void sweep();
2157
+ void this._indexMeshFiles(mesh);
2025
2158
  mesh.autobee.on('move-to', () => void sweep());
2026
2159
  entry.hostedAgents = new HostedAgents({
2027
2160
  mesh,
@@ -2064,7 +2197,7 @@ export class Core extends ReadyResource {
2064
2197
  await snap.close();
2065
2198
  }
2066
2199
  }
2067
- async _announceDeparture(mesh = this._currentMesh(), strict = false) {
2200
+ async _announceDeparture(mesh = this._accountMesh(), strict = false) {
2068
2201
  if (!mesh?.writable) {
2069
2202
  if (strict)
2070
2203
  throw new Error('Mesh departure requires a writable source');
@@ -2084,24 +2217,21 @@ export class Core extends ReadyResource {
2084
2217
  throw error;
2085
2218
  }
2086
2219
  }
2087
- async leaveMesh() {
2220
+ async leaveMesh(meshId) {
2088
2221
  if (this.closing)
2089
2222
  return;
2090
- const mesh = await this._awaitMesh();
2091
- if (!mesh)
2092
- return;
2093
- if (this._leaving)
2223
+ if (!this.opened)
2224
+ await this.ready();
2225
+ if (this._leaving === meshId)
2094
2226
  return;
2095
- if (this._minting)
2227
+ if (this._leaving || this._minting)
2096
2228
  throw new Error('Mesh transition already in progress');
2097
- const departing = this._currentMesh();
2098
- if (!departing)
2099
- return;
2229
+ const departing = await this._openPooledMesh(meshId);
2100
2230
  const others = (await this.local.listMeshes()).filter((row) => !row.key.equals(departing.key));
2101
- if (others.length > 0 && (await this._isHomeMesh(departing))) {
2231
+ const leavingAccount = await this._isStoredAccount(departing);
2232
+ if (others.length > 0 && leavingAccount)
2102
2233
  throw new Error(LEAVE_HOME_REFUSED);
2103
- }
2104
- this._leaving = true;
2234
+ this._leaving = meshId;
2105
2235
  this._notifyStatus();
2106
2236
  try {
2107
2237
  if (await this._departureHasAudience(departing)) {
@@ -2110,9 +2240,7 @@ export class Core extends ReadyResource {
2110
2240
  await this._announceDeparture(departing);
2111
2241
  await this._releaseJoinedRoom(departing.key);
2112
2242
  await this._archiveMembership(departing);
2113
- const fallback = await this._writableFallback(others);
2114
- if (fallback) {
2115
- await this._handOffActive(fallback, this._activeEntry());
2243
+ if (!leavingAccount) {
2116
2244
  await this._retirePooledMesh(departing);
2117
2245
  this._departures?.flush();
2118
2246
  return;
@@ -2124,39 +2252,35 @@ export class Core extends ReadyResource {
2124
2252
  throw error;
2125
2253
  }
2126
2254
  finally {
2127
- this._leaving = false;
2255
+ this._leaving = null;
2128
2256
  this._notifyStatus();
2129
2257
  }
2130
2258
  }
2131
- async _isHomeMesh(mesh) {
2132
- const home = await this._homeMeshKey();
2133
- return home !== undefined && home.equals(mesh.key);
2259
+ async _isStoredAccount(mesh) {
2260
+ const key = await this._storedAccountKey();
2261
+ return key !== undefined && key.equals(mesh.key);
2134
2262
  }
2135
- async _homeMeshKey() {
2136
- return (await this.local.homeMesh(this._deviceKeyPair.publicKey))?.key;
2263
+ async _storedAccountKey() {
2264
+ return (await this.local.accountMesh(this._deviceKeyPair.publicKey))?.key;
2137
2265
  }
2138
- async _writableFallback(rows) {
2139
- const home = await this._homeMeshKey();
2140
- const preferred = (row) => Number(!!home?.equals(row.key));
2141
- for (const row of [...rows].sort((a, b) => preferred(b) - preferred(a))) {
2142
- const meshId = row.key.toString('hex');
2143
- const held = this._currentMesh(meshId);
2144
- const mesh = await this._openPooledMesh(meshId).catch(noop);
2145
- if (mesh?.writable)
2146
- return mesh;
2147
- if (mesh && !held)
2148
- await this._retirePooledMesh(mesh).catch(noop);
2149
- }
2150
- return null;
2266
+ listMeshes() {
2267
+ if (this._listing)
2268
+ return this._listing;
2269
+ const listing = this._readMeshList().finally(() => {
2270
+ if (this._listing === listing)
2271
+ this._listing = null;
2272
+ });
2273
+ this._listing = listing;
2274
+ return listing;
2151
2275
  }
2152
- async listMeshes() {
2276
+ async _readMeshList() {
2153
2277
  if (!this.opened)
2154
2278
  await this.ready();
2155
- const home = await this._homeMeshKey();
2279
+ const accountKey = await this._storedAccountKey();
2156
2280
  const rows = await this.local.listMeshes();
2157
- const registry = home ? this._currentMesh(home.toString('hex')) : null;
2281
+ const registry = accountKey ? this._pooled(b4a.toHex(accountKey)) : null;
2158
2282
  const rooms = registry ? await this._ownRooms(registry) : new Map();
2159
- return { meshes: rows.map((row) => this._meshEntry(row, home, rooms)) };
2283
+ return { meshes: rows.map((row) => this._meshEntry(row, accountKey, rooms)) };
2160
2284
  }
2161
2285
  async listArchivedMeshes() {
2162
2286
  if (!this.opened)
@@ -2164,9 +2288,9 @@ export class Core extends ReadyResource {
2164
2288
  const rows = await this.local.listArchivedMeshes();
2165
2289
  return {
2166
2290
  meshes: rows.map((row) => ({
2167
- meshId: row.key.toString('hex'),
2291
+ meshId: b4a.toHex(row.key),
2168
2292
  leftAt: row.leftAt,
2169
- wasHome: row.wasHome,
2293
+ wasAccount: row.wasHome,
2170
2294
  migratedAt: row.migratedAt
2171
2295
  }))
2172
2296
  };
@@ -2175,8 +2299,8 @@ export class Core extends ReadyResource {
2175
2299
  if (!this.opened)
2176
2300
  await this.ready();
2177
2301
  const row = await this._archivedRow(meshId);
2178
- const home = await this._homeMeshKey();
2179
- const account = home ? this._currentMesh(home.toString('hex')) : null;
2302
+ const key = await this._storedAccountKey();
2303
+ const account = key ? this._pooled(b4a.toHex(key)) : null;
2180
2304
  const counts = await this._withArchivedMesh(row, (mesh) => countContents(mesh, account));
2181
2305
  return { meshId, ...counts };
2182
2306
  }
@@ -2192,7 +2316,7 @@ export class Core extends ReadyResource {
2192
2316
  const none = { chats: 0, agents: 0, knowledge: 0 };
2193
2317
  return { meshId, migrated: false, migratedAt: row.migratedAt, ...none };
2194
2318
  }
2195
- const account = await this._accountMesh();
2319
+ const account = await this._writableAccount();
2196
2320
  if (!account)
2197
2321
  throw new Error(NO_WRITABLE_ACCOUNT);
2198
2322
  const counts = await this._withArchivedMesh(row, (mesh) => migrateContents(mesh, account));
@@ -2214,7 +2338,7 @@ export class Core extends ReadyResource {
2214
2338
  return row;
2215
2339
  }
2216
2340
  _withArchivedMesh(row, fn) {
2217
- const pooled = this._currentMesh(row.key.toString('hex'));
2341
+ const pooled = this._pooled(b4a.toHex(row.key));
2218
2342
  if (pooled)
2219
2343
  return fn(pooled);
2220
2344
  return withArchivedMesh(this.store, row, { storage: this.storage, deviceId: this._deviceKeyPair.publicKey, logger: this._log }, fn);
@@ -2226,7 +2350,7 @@ export class Core extends ReadyResource {
2226
2350
  return rooms;
2227
2351
  for await (const room of registry.view.find(JOINED_MESHES)) {
2228
2352
  if (!room.leftAt && room.memberId.equals(ours))
2229
- rooms.set(room.meshKey.toString('hex'), room);
2353
+ rooms.set(b4a.toHex(room.meshKey), room);
2230
2354
  }
2231
2355
  return rooms;
2232
2356
  }
@@ -2241,7 +2365,7 @@ export class Core extends ReadyResource {
2241
2365
  if (!this.opened)
2242
2366
  await this.ready();
2243
2367
  const meshKey = this._meshKeyOf(meshId);
2244
- const registry = await this._accountMesh();
2368
+ const registry = await this._writableAccount();
2245
2369
  if (!registry)
2246
2370
  throw new Error(ROOM_WITHOUT_ACCOUNT);
2247
2371
  const ours = registry.ourMemberId;
@@ -2278,7 +2402,7 @@ export class Core extends ReadyResource {
2278
2402
  await this.ready();
2279
2403
  await this._meshTransitionLock.lock();
2280
2404
  try {
2281
- const registry = await this._accountMesh();
2405
+ const registry = await this._writableAccount();
2282
2406
  if (!registry)
2283
2407
  throw new Error(ROOM_WITHOUT_ACCOUNT);
2284
2408
  const resolved = await this._resolveSharedRoom(registry);
@@ -2289,7 +2413,7 @@ export class Core extends ReadyResource {
2289
2413
  }
2290
2414
  }
2291
2415
  async _occupancyNotice(meshId) {
2292
- const mesh = this._currentMesh(meshId);
2416
+ const mesh = this._pooled(meshId);
2293
2417
  if (!mesh)
2294
2418
  return undefined;
2295
2419
  if (!mesh.ourMemberId)
@@ -2320,7 +2444,7 @@ export class Core extends ReadyResource {
2320
2444
  await this._unmarkSharedRoom(registry, loser);
2321
2445
  if (losers.length > 0)
2322
2446
  this._notifyStatus();
2323
- return { meshId: winner.meshKey.toString('hex'), notice: unmarkedNotice(losers) };
2447
+ return { meshId: b4a.toHex(winner.meshKey), notice: unmarkedNotice(losers) };
2324
2448
  }
2325
2449
  async _unmarkSharedRoom(registry, room) {
2326
2450
  await registry.dispatch('put-joined-mesh', {
@@ -2342,7 +2466,7 @@ export class Core extends ReadyResource {
2342
2466
  throw new Error(MESH_BELOW_FLOOR);
2343
2467
  if (this._leaving || this._minting)
2344
2468
  throw new Error('Mesh transition already in progress');
2345
- const registry = resolved ?? (await this._accountMesh());
2469
+ const registry = resolved ?? (await this._writableAccount());
2346
2470
  if (!registry)
2347
2471
  throw new Error(ROOM_WITHOUT_ACCOUNT);
2348
2472
  const mesh = await this._openMesh({ new: true, room: true, compute });
@@ -2356,7 +2480,7 @@ export class Core extends ReadyResource {
2356
2480
  throw error;
2357
2481
  }
2358
2482
  this._notifyStatus();
2359
- return this._poolKey(mesh);
2483
+ return this._meshIdOf(mesh);
2360
2484
  }
2361
2485
  async _discardRoom(mesh) {
2362
2486
  try {
@@ -2369,36 +2493,21 @@ export class Core extends ReadyResource {
2369
2493
  }
2370
2494
  async openMesh(meshId) {
2371
2495
  await this._openPooledMesh(meshId);
2372
- this._notifyStatus();
2373
- }
2374
- async selectMesh(meshId) {
2375
- if (!this.opened)
2376
- await this.ready();
2377
- await this._meshTransitionLock.lock();
2378
- try {
2379
- if (this.closing)
2380
- throw new Error('Core is closing');
2381
- if (this._leaving || this._minting)
2382
- throw new Error('Mesh transition already in progress');
2383
- if (meshId === this._activeMeshKey)
2384
- return;
2385
- const outgoing = this._activeEntry();
2386
- await this._handOffActive(await this._openPooledMesh(meshId), outgoing);
2387
- }
2388
- finally {
2389
- this._meshTransitionLock.unlock();
2390
- }
2391
2496
  }
2392
2497
  async closeMesh(meshId) {
2393
2498
  if (!this.opened)
2394
2499
  await this.ready();
2395
2500
  await this._meshTransitionLock.lock();
2396
2501
  try {
2397
- if (meshId === this._activeMeshKey)
2398
- throw new Error('The active mesh cannot be closed');
2399
- const mesh = this._currentMesh(meshId) ?? (await this._opening.get(meshId)?.catch(noop));
2400
- if (!mesh)
2502
+ if (meshId === this._accountId)
2503
+ throw new Error('The account mesh cannot be closed');
2504
+ const key = this._meshKeyOf(meshId);
2505
+ const mesh = this._pooled(meshId) ?? (await this._opening.get(meshId)?.catch(noop)) ?? null;
2506
+ if (!mesh) {
2507
+ if (!(await this.local.readMeshSession(key)))
2508
+ throw new Error(`${NO_SUCH_MESH}: ${meshId}`);
2401
2509
  return;
2510
+ }
2402
2511
  await this._retirePooledMesh(mesh);
2403
2512
  }
2404
2513
  finally {
@@ -2413,13 +2522,21 @@ export class Core extends ReadyResource {
2413
2522
  throw new Error('Core is closing');
2414
2523
  if (this._blockedByMinVersion())
2415
2524
  throw new Error(MESH_BELOW_FLOOR);
2416
- const pooled = this._currentMesh(meshId);
2525
+ const pooled = this._pooled(meshId);
2417
2526
  if (pooled)
2418
2527
  return pooled;
2528
+ const retiring = this._retiring.get(meshId);
2529
+ if (retiring) {
2530
+ await retiring.catch(noop);
2531
+ return this._openPooledMesh(meshId);
2532
+ }
2419
2533
  const inflight = this._opening.get(meshId);
2420
2534
  if (inflight)
2421
2535
  return inflight;
2422
- const opening = this._openRecordedMesh(meshId).finally(() => this._opening.delete(meshId));
2536
+ const opening = this._openRecordedMesh(meshId).finally(() => {
2537
+ if (this._opening.get(meshId) === opening)
2538
+ this._opening.delete(meshId);
2539
+ });
2423
2540
  this._opening.set(meshId, opening);
2424
2541
  return opening;
2425
2542
  }
@@ -2428,22 +2545,27 @@ export class Core extends ReadyResource {
2428
2545
  if (!session)
2429
2546
  throw new Error(`${NO_SUCH_MESH}: ${meshId}`);
2430
2547
  const mesh = await this._openMeshSession(session);
2548
+ const halted = mesh.interrupted;
2549
+ if (halted) {
2550
+ this._withdrawFromMesh(mesh, halted);
2551
+ throw halted;
2552
+ }
2431
2553
  this._wireMesh(this._poolEntry(mesh));
2432
2554
  this._bringMeshOnline(mesh);
2555
+ this._notifyStatus();
2433
2556
  return mesh;
2434
2557
  }
2435
- _meshEntry(row, home, rooms) {
2436
- const meshId = row.key.toString('hex');
2437
- const mesh = this._currentMesh(meshId);
2438
- const active = meshId === this._activeMeshKey;
2558
+ _meshEntry(row, accountKey, rooms) {
2559
+ const meshId = b4a.toHex(row.key);
2560
+ const mesh = this._pooled(meshId);
2561
+ const account = !!accountKey?.equals(row.key);
2439
2562
  const room = rooms.get(meshId);
2440
2563
  return {
2441
2564
  meshId,
2442
- state: this._meshEntryState(mesh, active),
2565
+ state: this._meshEntryState(mesh, account),
2443
2566
  writable: mesh?.writable ?? false,
2444
2567
  open: mesh !== null,
2445
- active,
2446
- home: !!home?.equals(row.key),
2568
+ account,
2447
2569
  label: room?.label || undefined,
2448
2570
  members: mesh?.membership.members ?? 0,
2449
2571
  shared: room?.shared ?? false,
@@ -2451,8 +2573,8 @@ export class Core extends ReadyResource {
2451
2573
  compute: row.compute ?? false
2452
2574
  };
2453
2575
  }
2454
- _meshEntryState(mesh, active) {
2455
- if (active)
2576
+ _meshEntryState(mesh, account) {
2577
+ if (account)
2456
2578
  return this._statusState();
2457
2579
  if (!mesh)
2458
2580
  return 'idle';
@@ -2463,11 +2585,11 @@ export class Core extends ReadyResource {
2463
2585
  throw new Error(`${NO_SUCH_MESH}: ${meshId}`);
2464
2586
  return Buffer.from(meshId, 'hex');
2465
2587
  }
2466
- async removeDeviceWriter(writerKey, deviceId) {
2467
- const mesh = await this._awaitMesh();
2468
- if (!mesh)
2469
- throw new Error('No active mesh');
2470
- await mesh.dispatch('remove-writer', { writerKey, deviceId, revokedAt: Date.now() });
2588
+ async removeDeviceWriter(writerKey, deviceId, mesh) {
2589
+ const target = mesh ?? (await this._awaitMesh());
2590
+ if (!target)
2591
+ throw new Error('this device holds no account mesh');
2592
+ await target.dispatch('remove-writer', { writerKey, deviceId, revokedAt: Date.now() });
2471
2593
  }
2472
2594
  async renameLocalDevice(name) {
2473
2595
  const publicKey = this.devicePublicKey;
@@ -2476,6 +2598,16 @@ export class Core extends ReadyResource {
2476
2598
  if (this._localDevice)
2477
2599
  this._localDevice = { ...this._localDevice, name };
2478
2600
  }
2601
+ async account() {
2602
+ if (!this.opened)
2603
+ await this.ready();
2604
+ const key = await this._storedAccountKey();
2605
+ return {
2606
+ meshId: key ? b4a.toHex(key) : undefined,
2607
+ writable: this._accountMesh()?.writable ?? false,
2608
+ displayName: (await this.displayName()) ?? undefined
2609
+ };
2610
+ }
2479
2611
  async displayName() {
2480
2612
  const identity = await this.local.readIdentity();
2481
2613
  if (!identity)
@@ -2483,8 +2615,8 @@ export class Core extends ReadyResource {
2483
2615
  return identity.displayName ?? (await this._accountMemberName(identity.publicKey));
2484
2616
  }
2485
2617
  async _accountMemberName(identityPublicKey) {
2486
- const home = await this._homeMeshKey();
2487
- const mesh = home ? this._currentMesh(home.toString('hex')) : null;
2618
+ const key = await this._storedAccountKey();
2619
+ const mesh = key ? this._pooled(b4a.toHex(key)) : null;
2488
2620
  if (!mesh)
2489
2621
  return null;
2490
2622
  const ours = mesh.ourMemberId;
@@ -2493,25 +2625,6 @@ export class Core extends ReadyResource {
2493
2625
  const member = await mesh.view.get('@qvac/members', { id: ours });
2494
2626
  return member?.name ?? null;
2495
2627
  }
2496
- async setDisplayName(name) {
2497
- const displayName = name.trim();
2498
- if (!displayName)
2499
- throw new Error('a display name is required');
2500
- const identity = await this.local.saveDisplayName(displayName);
2501
- if (!identity)
2502
- throw new Error('no identity to name: this device holds no account yet');
2503
- const context = { deviceKeyPair: this._deviceKeyPair, log: this._log };
2504
- for (const { mesh } of this._meshes.values()) {
2505
- if (!mesh.writable)
2506
- continue;
2507
- try {
2508
- await renameFoundedMember(mesh, context, identity);
2509
- }
2510
- catch (err) {
2511
- this._log?.warn(['p2p', 'mesh'], 'naming the member in this mesh failed', err);
2512
- }
2513
- }
2514
- }
2515
2628
  async saveDeviceProfileLocal(capabilityProfile, recommendedModelName, platform) {
2516
2629
  const publicKey = this.devicePublicKey;
2517
2630
  if (publicKey) {
@@ -2539,7 +2652,7 @@ export class Core extends ReadyResource {
2539
2652
  const meshKey = this._meshKeyOf(meshId);
2540
2653
  if (await this.local.readMeshSession(meshKey))
2541
2654
  return;
2542
- const registry = await this._accountMesh();
2655
+ const registry = await this._writableAccount();
2543
2656
  if (registry && (await this._isOwnRoomIn(registry, meshKey))) {
2544
2657
  throw new Error(ROOM_HELD_BY_SIBLING);
2545
2658
  }
@@ -2551,12 +2664,12 @@ export class Core extends ReadyResource {
2551
2664
  return null;
2552
2665
  if (!this.opened)
2553
2666
  await this.ready();
2554
- const target = opts.meshId ??
2555
- (opts.memberInvite ? null : ((await this._homeMeshKey())?.toString('hex') ?? null));
2667
+ const accountKey = opts.memberInvite ? null : await this._storedAccountKey();
2668
+ const target = opts.meshId ?? (accountKey ? b4a.toHex(accountKey) : null);
2556
2669
  const mesh = target ? await this._openPooledMesh(target) : await this._awaitMesh();
2557
2670
  if (!mesh)
2558
2671
  return null;
2559
- const registry = await this._accountMesh();
2672
+ const registry = await this._writableAccount();
2560
2673
  if (!registry?.ourMemberId)
2561
2674
  throw new Error(INVITE_ACCOUNT_UNREADABLE);
2562
2675
  const room = await this._isOwnRoomIn(registry, mesh.key);
@@ -2567,10 +2680,10 @@ export class Core extends ReadyResource {
2567
2680
  else {
2568
2681
  if (room)
2569
2682
  throw new Error(DEVICE_INVITE_ROOM_REFUSED);
2570
- const home = await this._homeMeshKey();
2571
- if (!home)
2683
+ const key = await this._storedAccountKey();
2684
+ if (!key)
2572
2685
  throw new Error(INVITE_ACCOUNT_UNREADABLE);
2573
- if (!home.equals(mesh.key))
2686
+ if (!key.equals(mesh.key))
2574
2687
  throw new Error(DEVICE_INVITE_ROOM_REFUSED);
2575
2688
  }
2576
2689
  const auto = mesh.autobee;
@@ -2622,7 +2735,7 @@ export class Core extends ReadyResource {
2622
2735
  async pushSkillSecret(request) {
2623
2736
  if (!this.opened)
2624
2737
  await this.ready();
2625
- const mesh = this._currentMesh();
2738
+ const mesh = this._accountMesh();
2626
2739
  const deviceId = this.devicePublicKey;
2627
2740
  if (!mesh || !deviceId)
2628
2741
  throw new Error(NOT_THE_HOST);
@@ -2640,7 +2753,7 @@ export class Core extends ReadyResource {
2640
2753
  return this._secretHandoff.send(agent.providerDeviceId, request);
2641
2754
  }
2642
2755
  async _storeHandedOffSecret(request, fromDeviceId) {
2643
- const mesh = this._currentMesh();
2756
+ const mesh = this._accountMesh();
2644
2757
  const deviceId = this.devicePublicKey;
2645
2758
  if (!mesh || !deviceId)
2646
2759
  throw new Error(NOT_THE_HOST);
@@ -2664,10 +2777,10 @@ export class Core extends ReadyResource {
2664
2777
  return;
2665
2778
  if (!room.memberId.equals(ours))
2666
2779
  continue;
2667
- const meshId = room.meshKey.toString('hex');
2780
+ const meshId = b4a.toHex(room.meshKey);
2668
2781
  if (this._following.has(meshId))
2669
2782
  continue;
2670
- const held = this._currentMesh(meshId);
2783
+ const held = this._pooled(meshId);
2671
2784
  this._following.add(meshId);
2672
2785
  try {
2673
2786
  if (room.leftAt) {
@@ -2699,8 +2812,8 @@ export class Core extends ReadyResource {
2699
2812
  await this.local.recordDeparture(room.key, { creator: room.minted });
2700
2813
  }
2701
2814
  await this._announceDeparture(room);
2702
- if (this._isActive(room))
2703
- await this._replaceRevokedActiveMesh(room);
2815
+ if (this._isAccount(room))
2816
+ await this._replaceRevokedAccount(room);
2704
2817
  else
2705
2818
  await this._departRevokedMesh(room);
2706
2819
  this._departures?.flush();
@@ -2723,8 +2836,7 @@ export class Core extends ReadyResource {
2723
2836
  return;
2724
2837
  const session = this.local.createCandidateSession(seed, meshKey);
2725
2838
  await this.local.recordMesh(this._deviceKeyPair.publicKey, meshKey, session);
2726
- await this._openPooledMesh(meshKey.toString('hex'));
2727
- this._notifyStatus();
2839
+ await this._openPooledMesh(b4a.toHex(meshKey));
2728
2840
  }
2729
2841
  async _requestRoomSeed(memberMesh, meshKey, ours) {
2730
2842
  const self = this._deviceKeyPair?.publicKey;
@@ -2768,13 +2880,13 @@ export class Core extends ReadyResource {
2768
2880
  return this._harnessLoan.revalidate((device) => this._sharesComputeMesh(Buffer.from(device)));
2769
2881
  }
2770
2882
  async _computeLender() {
2771
- const self = this.devicePublicKey?.toString('hex');
2883
+ const self = this.devicePublicKey ? b4a.toHex(this.devicePublicKey) : undefined;
2772
2884
  const lenders = new Map();
2773
2885
  for (const entry of this._meshes.values()) {
2774
2886
  if (!entry.mesh.compute)
2775
2887
  continue;
2776
2888
  for await (const device of entry.mesh.view.find('@qvac/devices')) {
2777
- const id = device.id.toString('hex');
2889
+ const id = b4a.toHex(device.id);
2778
2890
  if (device.revoked || id === self)
2779
2891
  continue;
2780
2892
  lenders.set(id, device.id);
@@ -2843,12 +2955,11 @@ export class Core extends ReadyResource {
2843
2955
  return this._runLocalSkillOverlayOp(op);
2844
2956
  return this._skillOverlayHandoff.send(host, { ...op, agentId });
2845
2957
  }
2846
- async _resolveAgentHost(agentId) {
2958
+ async _resolveAgentHost(agentId, mesh = this._accountMesh()) {
2847
2959
  if (!agentId)
2848
2960
  return null;
2849
2961
  if (!this.opened)
2850
2962
  await this.ready();
2851
- const mesh = this._currentMesh();
2852
2963
  const deviceId = this.devicePublicKey;
2853
2964
  if (!mesh || !deviceId)
2854
2965
  throw new Error(NOT_THE_HOST);
@@ -2864,15 +2975,15 @@ export class Core extends ReadyResource {
2864
2975
  throw new Error(HOST_REVOKED);
2865
2976
  return agent.providerDeviceId;
2866
2977
  }
2867
- async _listInferenceCalls(chatId) {
2868
- const mesh = await this._awaitMesh();
2978
+ async _listInferenceCalls(meshId, chatId) {
2979
+ const mesh = await this._openPooledMesh(meshId);
2869
2980
  const store = this._inferenceCalls;
2870
2981
  const own = store?.list(chatId) ?? { calls: [], truncated: false };
2871
2982
  const local = own.calls;
2872
- const chat = mesh ? await getChat(mesh, chatId) : null;
2983
+ const chat = await getChat(mesh, chatId);
2873
2984
  if (!chat)
2874
2985
  return own;
2875
- const host = await this._resolveAgentHost(chat.agentId).catch((err) => {
2986
+ const host = await this._resolveAgentHost(chat.agentId, mesh).catch((err) => {
2876
2987
  if (local.length === 0)
2877
2988
  throw err;
2878
2989
  this._log?.warn(['api'], 'inspector host unresolved, showing this device only', {
@@ -2900,7 +3011,7 @@ export class Core extends ReadyResource {
2900
3011
  return [{ runId: entry.runId, at: entry.at, call: call }];
2901
3012
  }
2902
3013
  async _serveInferenceCalls(request, fromDeviceId) {
2903
- const mesh = this._currentMesh();
3014
+ const mesh = (await this._meshHoldingChat(request.chatId)) ?? this._accountMesh();
2904
3015
  const deviceId = this.devicePublicKey;
2905
3016
  if (!mesh || !deviceId)
2906
3017
  throw new Error(NOT_THE_HOST);
@@ -2914,8 +3025,14 @@ export class Core extends ReadyResource {
2914
3025
  });
2915
3026
  return receive(request, fromDeviceId);
2916
3027
  }
3028
+ async _meshHoldingChat(chatId) {
3029
+ for (const { mesh } of this._meshes.values())
3030
+ if (await getChat(mesh, chatId))
3031
+ return mesh;
3032
+ return null;
3033
+ }
2917
3034
  async _serveSkillOverlayOp(op, fromDeviceId) {
2918
- const mesh = this._currentMesh();
3035
+ const mesh = this._accountMesh();
2919
3036
  const deviceId = this.devicePublicKey;
2920
3037
  if (!mesh || !deviceId)
2921
3038
  throw new Error(NOT_THE_HOST);
@@ -2986,7 +3103,7 @@ export class Core extends ReadyResource {
2986
3103
  return;
2987
3104
  }
2988
3105
  this._trackPeerConnection(conn);
2989
- const remoteId = conn.remotePublicKey.toString('hex').slice(0, 8);
3106
+ const remoteId = b4a.toHex(conn.remotePublicKey).slice(0, 8);
2990
3107
  const start = Date.now();
2991
3108
  let bytes = 0;
2992
3109
  let connErr = null;
@@ -3036,21 +3153,15 @@ export class Core extends ReadyResource {
3036
3153
  });
3037
3154
  this._notifyStatus();
3038
3155
  }
3039
- _currentMeshAccessor(status) {
3040
- return Object.assign((meshId) => this._meshFor(meshId), {
3041
- selection: {
3042
- key: () => this._activeMeshKey,
3043
- watch: (listener) => status.watch(listener),
3044
- unwatch: (listener) => status.unwatch(listener)
3045
- }
3046
- });
3156
+ _meshPool() {
3157
+ return (meshId) => this._pooled(meshId) ?? this._openPooledMesh(meshId);
3047
3158
  }
3048
3159
  _meshStatusSource() {
3049
3160
  return {
3050
3161
  watch: (listener) => void this._statusListeners.add(listener),
3051
3162
  unwatch: (listener) => void this._statusListeners.delete(listener),
3052
3163
  snapshot: () => this._statusSnapshot(),
3053
- leave: () => this.leaveMesh(),
3164
+ leave: (meshId) => this.leaveMesh(meshId),
3054
3165
  acknowledgeKicked: () => this.acknowledgeKicked(),
3055
3166
  join: (invite) => this.joinMesh(invite),
3056
3167
  cancelJoin: () => this.cancelMeshJoin(),
@@ -3069,8 +3180,8 @@ export class Core extends ReadyResource {
3069
3180
  };
3070
3181
  },
3071
3182
  list: () => this.listMeshes(),
3183
+ account: () => this.account(),
3072
3184
  open: (meshId) => this.openMesh(meshId),
3073
- select: (meshId) => this.selectMesh(meshId),
3074
3185
  close: (meshId) => this.closeMesh(meshId),
3075
3186
  label: (meshId, label) => this.setMeshLabel(meshId, label),
3076
3187
  createRoom: (opts) => this.createRoom(opts),
@@ -3080,8 +3191,7 @@ export class Core extends ReadyResource {
3080
3191
  recoverable: () => this.accountRecoverable(),
3081
3192
  publishRecovery: async (phrase) => void (await this.publishAccountRecovery(phrase)),
3082
3193
  recover: (phrase, timeout) => this.recoverAccount(phrase, { timeout }),
3083
- displayName: () => this.displayName(),
3084
- setDisplayName: (name) => this.setDisplayName(name)
3194
+ displayName: () => this.displayName()
3085
3195
  };
3086
3196
  }
3087
3197
  _networkStatusSource() {
@@ -3304,8 +3414,7 @@ export class Core extends ReadyResource {
3304
3414
  }
3305
3415
  }
3306
3416
  _startIndexing() {
3307
- const entry = this._activeEntry();
3308
- if (entry)
3417
+ for (const entry of this._meshes.values())
3309
3418
  this._startEntryIndexing(entry);
3310
3419
  }
3311
3420
  _startEntryIndexing(entry) {
@@ -3386,16 +3495,16 @@ export class Core extends ReadyResource {
3386
3495
  const root = this._identityPublicKey;
3387
3496
  return {
3388
3497
  ...mesh.membership,
3389
- memberId: memberId?.toString('hex') ?? null,
3498
+ memberId: memberId ? b4a.toHex(memberId) : null,
3390
3499
  mintedOurRoot: !!memberId && !!root && b4a.equals(memberId, root)
3391
3500
  };
3392
3501
  }
3393
3502
  _diagnostics() {
3394
- const mesh = this._currentMesh();
3503
+ const mesh = this._accountMesh();
3395
3504
  const swarm = this._swarm;
3396
3505
  const dht = swarm ? swarm.dht : null;
3397
3506
  const system = this._systemInfo?.snapshot() ?? { cpu: null, gpu: null };
3398
- const publicKey = this.devicePublicKey?.toString('hex');
3507
+ const publicKey = this.devicePublicKey ? b4a.toHex(this.devicePublicKey) : undefined;
3399
3508
  const device = this._localDevice;
3400
3509
  return {
3401
3510
  device: publicKey,
@@ -3410,8 +3519,8 @@ export class Core extends ReadyResource {
3410
3519
  state: this._statusState(),
3411
3520
  mesh: mesh
3412
3521
  ? {
3413
- key: mesh.key.toString('hex'),
3414
- discoveryKey: mesh.discoveryKey.toString('hex'),
3522
+ key: b4a.toHex(mesh.key),
3523
+ discoveryKey: b4a.toHex(mesh.discoveryKey),
3415
3524
  writable: mesh.writable,
3416
3525
  localLength: mesh.localLength,
3417
3526
  systemLength: mesh.autobee.system.bee.head()?.length ?? 0,
@@ -3445,14 +3554,10 @@ export class Core extends ReadyResource {
3445
3554
  }
3446
3555
  _statusSnapshot() {
3447
3556
  const peerDeviceIds = [...this.peerDeviceIds];
3448
- const mesh = this._currentMesh();
3449
3557
  return {
3450
3558
  state: this._statusState(),
3451
- writable: mesh?.writable ?? false,
3452
3559
  peerCount: peerDeviceIds.length,
3453
3560
  peerDeviceIds,
3454
- meshKey: mesh?.key,
3455
- discoveryKey: mesh?.discoveryKey,
3456
3561
  kickedPending: this._kickedPending,
3457
3562
  error: this._statusError ?? undefined
3458
3563
  };
@@ -3474,12 +3579,13 @@ export class Core extends ReadyResource {
3474
3579
  return 'needs-upgrade';
3475
3580
  if (this._minting)
3476
3581
  return 'joining';
3477
- const mesh = this._currentMesh();
3478
- if (mesh)
3479
- return mesh.writable ? 'joined' : 'joining';
3582
+ const account = this._accountMesh();
3583
+ if (account)
3584
+ return account.writable ? 'joined' : 'joining';
3480
3585
  return 'idle';
3481
3586
  }
3482
3587
  _notifyStatus() {
3588
+ this._listing = null;
3483
3589
  for (const listener of [...this._statusListeners])
3484
3590
  listener();
3485
3591
  }
@@ -3490,7 +3596,7 @@ function after(ms) {
3490
3596
  function unmarkedNotice(losers) {
3491
3597
  if (losers.length === 0)
3492
3598
  return undefined;
3493
- const ids = losers.map((room) => room.meshKey.toString('hex').slice(0, 8)).join(', ');
3599
+ const ids = losers.map((room) => b4a.toHex(room.meshKey).slice(0, 8)).join(', ');
3494
3600
  return `another device of yours opened a shared room at the same moment, so ${ids} is an ordinary room now`;
3495
3601
  }
3496
3602
  function noop() { }